Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007-2008 Extreme Engineering Solutions, Inc. |
| 3 | * |
| 4 | * Author: Nate Case <ncase@xes-inc.com> |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of version 2 of |
| 7 | * the GNU General Public License. See the file COPYING in the main |
| 8 | * directory of this archive for more details. |
| 9 | * |
| 10 | * LED driver for various PCA955x I2C LED drivers |
| 11 | * |
| 12 | * Supported devices: |
| 13 | * |
| 14 | * Device Description 7-bit slave address |
| 15 | * ------ ----------- ------------------- |
| 16 | * PCA9550 2-bit driver 0x60 .. 0x61 |
| 17 | * PCA9551 8-bit driver 0x60 .. 0x67 |
| 18 | * PCA9552 16-bit driver 0x60 .. 0x67 |
| 19 | * PCA9553/01 4-bit driver 0x62 |
| 20 | * PCA9553/02 4-bit driver 0x63 |
| 21 | * |
| 22 | * Philips PCA955x LED driver chips follow a register map as shown below: |
| 23 | * |
| 24 | * Control Register Description |
| 25 | * ---------------- ----------- |
| 26 | * 0x0 Input register 0 |
| 27 | * .. |
| 28 | * NUM_INPUT_REGS - 1 Last Input register X |
| 29 | * |
| 30 | * NUM_INPUT_REGS Frequency prescaler 0 |
| 31 | * NUM_INPUT_REGS + 1 PWM register 0 |
| 32 | * NUM_INPUT_REGS + 2 Frequency prescaler 1 |
| 33 | * NUM_INPUT_REGS + 3 PWM register 1 |
| 34 | * |
| 35 | * NUM_INPUT_REGS + 4 LED selector 0 |
| 36 | * NUM_INPUT_REGS + 4 |
| 37 | * + NUM_LED_REGS - 1 Last LED selector |
| 38 | * |
| 39 | * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many |
| 40 | * bits the chip supports. |
| 41 | */ |
| 42 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 43 | #include <linux/acpi.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 44 | #include <linux/ctype.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 45 | #include <linux/delay.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 46 | #include <linux/err.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 47 | #include <linux/gpio.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 48 | #include <linux/i2c.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 49 | #include <linux/leds.h> |
| 50 | #include <linux/module.h> |
| 51 | #include <linux/of_device.h> |
| 52 | #include <linux/of.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 54 | #include <linux/string.h> |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 55 | |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 56 | #include <dt-bindings/leds/leds-pca955x.h> |
| 57 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 58 | /* LED select registers determine the source that drives LED outputs */ |
| 59 | #define PCA955X_LS_LED_ON 0x0 /* Output LOW */ |
| 60 | #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */ |
| 61 | #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */ |
| 62 | #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */ |
| 63 | |
| 64 | enum pca955x_type { |
| 65 | pca9550, |
| 66 | pca9551, |
| 67 | pca9552, |
| 68 | pca9553, |
| 69 | }; |
| 70 | |
| 71 | struct pca955x_chipdef { |
| 72 | int bits; |
| 73 | u8 slv_addr; /* 7-bit slave address mask */ |
| 74 | int slv_addr_shift; /* Number of bits to ignore */ |
| 75 | }; |
| 76 | |
| 77 | static struct pca955x_chipdef pca955x_chipdefs[] = { |
| 78 | [pca9550] = { |
| 79 | .bits = 2, |
| 80 | .slv_addr = /* 110000x */ 0x60, |
| 81 | .slv_addr_shift = 1, |
| 82 | }, |
| 83 | [pca9551] = { |
| 84 | .bits = 8, |
| 85 | .slv_addr = /* 1100xxx */ 0x60, |
| 86 | .slv_addr_shift = 3, |
| 87 | }, |
| 88 | [pca9552] = { |
| 89 | .bits = 16, |
| 90 | .slv_addr = /* 1100xxx */ 0x60, |
| 91 | .slv_addr_shift = 3, |
| 92 | }, |
| 93 | [pca9553] = { |
| 94 | .bits = 4, |
| 95 | .slv_addr = /* 110001x */ 0x62, |
| 96 | .slv_addr_shift = 1, |
| 97 | }, |
| 98 | }; |
| 99 | |
| 100 | static const struct i2c_device_id pca955x_id[] = { |
| 101 | { "pca9550", pca9550 }, |
| 102 | { "pca9551", pca9551 }, |
| 103 | { "pca9552", pca9552 }, |
| 104 | { "pca9553", pca9553 }, |
| 105 | { } |
| 106 | }; |
| 107 | MODULE_DEVICE_TABLE(i2c, pca955x_id); |
| 108 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 109 | static const struct acpi_device_id pca955x_acpi_ids[] = { |
| 110 | { "PCA9550", pca9550 }, |
| 111 | { "PCA9551", pca9551 }, |
| 112 | { "PCA9552", pca9552 }, |
| 113 | { "PCA9553", pca9553 }, |
| 114 | { } |
| 115 | }; |
| 116 | MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids); |
| 117 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 118 | struct pca955x { |
| 119 | struct mutex lock; |
| 120 | struct pca955x_led *leds; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 121 | struct pca955x_chipdef *chipdef; |
| 122 | struct i2c_client *client; |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 123 | #ifdef CONFIG_LEDS_PCA955X_GPIO |
| 124 | struct gpio_chip gpio; |
| 125 | #endif |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | struct pca955x_led { |
| 129 | struct pca955x *pca955x; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 130 | struct led_classdev led_cdev; |
| 131 | int led_num; /* 0 .. 15 potentially */ |
| 132 | char name[32]; |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 133 | u32 type; |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 134 | const char *default_trigger; |
| 135 | }; |
| 136 | |
| 137 | struct pca955x_platform_data { |
| 138 | struct pca955x_led *leds; |
| 139 | int num_leds; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /* 8 bits per input register */ |
| 143 | static inline int pca95xx_num_input_regs(int bits) |
| 144 | { |
| 145 | return (bits + 7) / 8; |
| 146 | } |
| 147 | |
| 148 | /* 4 bits per LED selector register */ |
| 149 | static inline int pca95xx_num_led_regs(int bits) |
| 150 | { |
| 151 | return (bits + 3) / 4; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Return an LED selector register value based on an existing one, with |
| 156 | * the appropriate 2-bit state value set for the given LED number (0-3). |
| 157 | */ |
| 158 | static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state) |
| 159 | { |
| 160 | return (oldval & (~(0x3 << (led_num << 1)))) | |
| 161 | ((state & 0x3) << (led_num << 1)); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Write to frequency prescaler register, used to program the |
| 166 | * period of the PWM output. period = (PSCx + 1) / 38 |
| 167 | */ |
| 168 | static void pca955x_write_psc(struct i2c_client *client, int n, u8 val) |
| 169 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 170 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 171 | |
| 172 | i2c_smbus_write_byte_data(client, |
| 173 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n, |
| 174 | val); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Write to PWM register, which determines the duty cycle of the |
| 179 | * output. LED is OFF when the count is less than the value of this |
| 180 | * register, and ON when it is greater. If PWMx == 0, LED is always OFF. |
| 181 | * |
| 182 | * Duty cycle is (256 - PWMx) / 256 |
| 183 | */ |
| 184 | static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val) |
| 185 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 186 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 187 | |
| 188 | i2c_smbus_write_byte_data(client, |
| 189 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n, |
| 190 | val); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Write to LED selector register, which determines the source that |
| 195 | * drives the LED output. |
| 196 | */ |
| 197 | static void pca955x_write_ls(struct i2c_client *client, int n, u8 val) |
| 198 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 199 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 200 | |
| 201 | i2c_smbus_write_byte_data(client, |
| 202 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n, |
| 203 | val); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Read the LED selector register, which determines the source that |
| 208 | * drives the LED output. |
| 209 | */ |
| 210 | static u8 pca955x_read_ls(struct i2c_client *client, int n) |
| 211 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 212 | struct pca955x *pca955x = i2c_get_clientdata(client); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 213 | |
| 214 | return (u8) i2c_smbus_read_byte_data(client, |
| 215 | pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n); |
| 216 | } |
| 217 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 218 | static int pca955x_led_set(struct led_classdev *led_cdev, |
| 219 | enum led_brightness value) |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 220 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 221 | struct pca955x_led *pca955x_led; |
| 222 | struct pca955x *pca955x; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 223 | u8 ls; |
| 224 | int chip_ls; /* which LSx to use (0-3 potentially) */ |
| 225 | int ls_led; /* which set of bits within LSx to use (0-3) */ |
| 226 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 227 | pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev); |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 228 | pca955x = pca955x_led->pca955x; |
| 229 | |
| 230 | chip_ls = pca955x_led->led_num / 4; |
| 231 | ls_led = pca955x_led->led_num % 4; |
| 232 | |
| 233 | mutex_lock(&pca955x->lock); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 234 | |
| 235 | ls = pca955x_read_ls(pca955x->client, chip_ls); |
| 236 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 237 | switch (value) { |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 238 | case LED_FULL: |
| 239 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); |
| 240 | break; |
| 241 | case LED_OFF: |
| 242 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF); |
| 243 | break; |
| 244 | case LED_HALF: |
| 245 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0); |
| 246 | break; |
| 247 | default: |
| 248 | /* |
| 249 | * Use PWM1 for all other values. This has the unwanted |
| 250 | * side effect of making all LEDs on the chip share the |
| 251 | * same brightness level if set to a value other than |
| 252 | * OFF, HALF, or FULL. But, this is probably better than |
| 253 | * just turning off for all other values. |
| 254 | */ |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 255 | pca955x_write_pwm(pca955x->client, 1, |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 256 | 255 - value); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 257 | ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | pca955x_write_ls(pca955x->client, chip_ls, ls); |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 262 | |
| 263 | mutex_unlock(&pca955x->lock); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 264 | |
Andrew Lunn | c3482b8 | 2015-08-20 12:32:35 +0200 | [diff] [blame] | 265 | return 0; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 268 | #ifdef CONFIG_LEDS_PCA955X_GPIO |
| 269 | /* |
| 270 | * Read the INPUT register, which contains the state of LEDs. |
| 271 | */ |
| 272 | static u8 pca955x_read_input(struct i2c_client *client, int n) |
| 273 | { |
| 274 | return (u8)i2c_smbus_read_byte_data(client, n); |
| 275 | } |
| 276 | |
| 277 | static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset) |
| 278 | { |
| 279 | struct pca955x *pca955x = gpiochip_get_data(gc); |
| 280 | struct pca955x_led *led = &pca955x->leds[offset]; |
| 281 | |
| 282 | if (led->type == PCA955X_TYPE_GPIO) |
| 283 | return 0; |
| 284 | |
| 285 | return -EBUSY; |
| 286 | } |
| 287 | |
| 288 | static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, |
| 289 | int val) |
| 290 | { |
| 291 | struct pca955x *pca955x = gpiochip_get_data(gc); |
| 292 | struct pca955x_led *led = &pca955x->leds[offset]; |
| 293 | |
| 294 | if (val) |
| 295 | pca955x_led_set(&led->led_cdev, LED_FULL); |
| 296 | else |
| 297 | pca955x_led_set(&led->led_cdev, LED_OFF); |
| 298 | } |
| 299 | |
| 300 | static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset) |
| 301 | { |
| 302 | struct pca955x *pca955x = gpiochip_get_data(gc); |
| 303 | struct pca955x_led *led = &pca955x->leds[offset]; |
| 304 | u8 reg = pca955x_read_input(pca955x->client, led->led_num / 8); |
| 305 | |
| 306 | return !!(reg & (1 << (led->led_num % 8))); |
| 307 | } |
| 308 | |
| 309 | static int pca955x_gpio_direction_input(struct gpio_chip *gc, |
| 310 | unsigned int offset) |
| 311 | { |
| 312 | /* To use as input ensure pin is not driven */ |
| 313 | pca955x_gpio_set_value(gc, offset, 0); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int pca955x_gpio_direction_output(struct gpio_chip *gc, |
| 319 | unsigned int offset, int val) |
| 320 | { |
| 321 | pca955x_gpio_set_value(gc, offset, val); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | #endif /* CONFIG_LEDS_PCA955X_GPIO */ |
| 326 | |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 327 | #if IS_ENABLED(CONFIG_OF) |
| 328 | static struct pca955x_platform_data * |
| 329 | pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip) |
| 330 | { |
| 331 | struct device_node *np = client->dev.of_node; |
| 332 | struct device_node *child; |
| 333 | struct pca955x_platform_data *pdata; |
| 334 | int count; |
| 335 | |
| 336 | count = of_get_child_count(np); |
| 337 | if (!count || count > chip->bits) |
| 338 | return ERR_PTR(-ENODEV); |
| 339 | |
| 340 | pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); |
| 341 | if (!pdata) |
| 342 | return ERR_PTR(-ENOMEM); |
| 343 | |
| 344 | pdata->leds = devm_kzalloc(&client->dev, |
| 345 | sizeof(struct pca955x_led) * chip->bits, |
| 346 | GFP_KERNEL); |
| 347 | if (!pdata->leds) |
| 348 | return ERR_PTR(-ENOMEM); |
| 349 | |
| 350 | for_each_child_of_node(np, child) { |
| 351 | const char *name; |
| 352 | u32 reg; |
| 353 | int res; |
| 354 | |
| 355 | res = of_property_read_u32(child, "reg", ®); |
| 356 | if ((res != 0) || (reg >= chip->bits)) |
| 357 | continue; |
| 358 | |
| 359 | if (of_property_read_string(child, "label", &name)) |
| 360 | name = child->name; |
| 361 | |
| 362 | snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name), |
| 363 | "%s", name); |
| 364 | |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 365 | pdata->leds[reg].type = PCA955X_TYPE_LED; |
| 366 | of_property_read_u32(child, "type", &pdata->leds[reg].type); |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 367 | of_property_read_string(child, "linux,default-trigger", |
| 368 | &pdata->leds[reg].default_trigger); |
| 369 | } |
| 370 | |
| 371 | pdata->num_leds = chip->bits; |
| 372 | |
| 373 | return pdata; |
| 374 | } |
| 375 | |
| 376 | static const struct of_device_id of_pca955x_match[] = { |
| 377 | { .compatible = "nxp,pca9550", .data = (void *)pca9550 }, |
| 378 | { .compatible = "nxp,pca9551", .data = (void *)pca9551 }, |
| 379 | { .compatible = "nxp,pca9552", .data = (void *)pca9552 }, |
| 380 | { .compatible = "nxp,pca9553", .data = (void *)pca9553 }, |
| 381 | {}, |
| 382 | }; |
| 383 | |
| 384 | MODULE_DEVICE_TABLE(of, of_pca955x_match); |
| 385 | #else |
| 386 | static struct pca955x_platform_data * |
| 387 | pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip) |
| 388 | { |
| 389 | return ERR_PTR(-ENODEV); |
| 390 | } |
| 391 | #endif |
| 392 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 393 | static int pca955x_probe(struct i2c_client *client, |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 394 | const struct i2c_device_id *id) |
| 395 | { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 396 | struct pca955x *pca955x; |
| 397 | struct pca955x_led *pca955x_led; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 398 | struct pca955x_chipdef *chip; |
| 399 | struct i2c_adapter *adapter; |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 400 | int i, err; |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 401 | struct pca955x_platform_data *pdata; |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 402 | int ngpios = 0; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 403 | |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 404 | if (id) { |
| 405 | chip = &pca955x_chipdefs[id->driver_data]; |
| 406 | } else { |
| 407 | const struct acpi_device_id *acpi_id; |
| 408 | |
| 409 | acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev); |
| 410 | if (!acpi_id) |
| 411 | return -ENODEV; |
| 412 | chip = &pca955x_chipdefs[acpi_id->driver_data]; |
| 413 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 414 | adapter = to_i2c_adapter(client->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 415 | pdata = dev_get_platdata(&client->dev); |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 416 | if (!pdata) { |
| 417 | pdata = pca955x_pdata_of_init(client, chip); |
| 418 | if (IS_ERR(pdata)) |
| 419 | return PTR_ERR(pdata); |
| 420 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 421 | |
| 422 | /* Make sure the slave address / chip type combo given is possible */ |
| 423 | if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) != |
| 424 | chip->slv_addr) { |
| 425 | dev_err(&client->dev, "invalid slave address %02x\n", |
| 426 | client->addr); |
| 427 | return -ENODEV; |
| 428 | } |
| 429 | |
Sachin Kamat | b40b0c1 | 2012-11-25 12:21:47 +0530 | [diff] [blame] | 430 | dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at " |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 431 | "slave address 0x%02x\n", |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 432 | client->name, chip->bits, client->addr); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 433 | |
Tin Huynh | aace34c | 2017-05-22 16:19:20 +0700 | [diff] [blame] | 434 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 435 | return -EIO; |
| 436 | |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 437 | if (pdata->num_leds != chip->bits) { |
| 438 | dev_err(&client->dev, |
| 439 | "board info claims %d LEDs on a %d-bit chip\n", |
| 440 | pdata->num_leds, chip->bits); |
| 441 | return -ENODEV; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Bryan Wu | 73759f6 | 2012-07-04 12:09:05 +0800 | [diff] [blame] | 444 | pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL); |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 445 | if (!pca955x) |
| 446 | return -ENOMEM; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 447 | |
Bryan Wu | 73759f6 | 2012-07-04 12:09:05 +0800 | [diff] [blame] | 448 | pca955x->leds = devm_kzalloc(&client->dev, |
| 449 | sizeof(*pca955x_led) * chip->bits, GFP_KERNEL); |
| 450 | if (!pca955x->leds) |
| 451 | return -ENOMEM; |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 452 | |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 453 | i2c_set_clientdata(client, pca955x); |
| 454 | |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 455 | mutex_init(&pca955x->lock); |
| 456 | pca955x->client = client; |
| 457 | pca955x->chipdef = chip; |
| 458 | |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 459 | for (i = 0; i < chip->bits; i++) { |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 460 | pca955x_led = &pca955x->leds[i]; |
| 461 | pca955x_led->led_num = i; |
| 462 | pca955x_led->pca955x = pca955x; |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 463 | pca955x_led->type = pdata->leds[i].type; |
Sven Wegener | 95bf14b | 2008-10-03 15:23:48 -0700 | [diff] [blame] | 464 | |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 465 | switch (pca955x_led->type) { |
| 466 | case PCA955X_TYPE_NONE: |
| 467 | break; |
| 468 | case PCA955X_TYPE_GPIO: |
| 469 | ngpios++; |
| 470 | break; |
| 471 | case PCA955X_TYPE_LED: |
| 472 | /* |
| 473 | * Platform data can specify LED names and |
| 474 | * default triggers |
| 475 | */ |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 476 | if (pdata->leds[i].name) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 477 | snprintf(pca955x_led->name, |
| 478 | sizeof(pca955x_led->name), "pca955x:%s", |
| 479 | pdata->leds[i].name); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 480 | if (pdata->leds[i].default_trigger) |
Alexander Stein | e7e11d8 | 2012-05-29 15:07:30 -0700 | [diff] [blame] | 481 | pca955x_led->led_cdev.default_trigger = |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 482 | pdata->leds[i].default_trigger; |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 483 | |
| 484 | pca955x_led->led_cdev.name = pca955x_led->name; |
| 485 | pca955x_led->led_cdev.brightness_set_blocking = |
| 486 | pca955x_led_set; |
| 487 | |
| 488 | err = devm_led_classdev_register(&client->dev, |
| 489 | &pca955x_led->led_cdev); |
| 490 | if (err) |
| 491 | return err; |
| 492 | |
| 493 | /* Turn off LED */ |
| 494 | pca955x_led_set(&pca955x_led->led_cdev, LED_OFF); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 495 | } |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 498 | /* PWM0 is used for half brightness or 50% duty cycle */ |
| 499 | pca955x_write_pwm(client, 0, 255-LED_HALF); |
| 500 | |
| 501 | /* PWM1 is used for variable brightness, default to OFF */ |
| 502 | pca955x_write_pwm(client, 1, 0); |
| 503 | |
| 504 | /* Set to fast frequency so we do not see flashing */ |
| 505 | pca955x_write_psc(client, 0, 0); |
| 506 | pca955x_write_psc(client, 1, 0); |
| 507 | |
Cédric Le Goater | 561099a1 | 2017-08-08 15:42:39 +0200 | [diff] [blame^] | 508 | #ifdef CONFIG_LEDS_PCA955X_GPIO |
| 509 | if (ngpios) { |
| 510 | pca955x->gpio.label = "gpio-pca955x"; |
| 511 | pca955x->gpio.direction_input = pca955x_gpio_direction_input; |
| 512 | pca955x->gpio.direction_output = pca955x_gpio_direction_output; |
| 513 | pca955x->gpio.set = pca955x_gpio_set_value; |
| 514 | pca955x->gpio.get = pca955x_gpio_get_value; |
| 515 | pca955x->gpio.request = pca955x_gpio_request_pin; |
| 516 | pca955x->gpio.can_sleep = 1; |
| 517 | pca955x->gpio.base = -1; |
| 518 | pca955x->gpio.ngpio = ngpios; |
| 519 | pca955x->gpio.parent = &client->dev; |
| 520 | pca955x->gpio.owner = THIS_MODULE; |
| 521 | |
| 522 | err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio, |
| 523 | pca955x); |
| 524 | if (err) { |
| 525 | /* Use data->gpio.dev as a flag for freeing gpiochip */ |
| 526 | pca955x->gpio.parent = NULL; |
| 527 | dev_warn(&client->dev, "could not add gpiochip\n"); |
| 528 | return err; |
| 529 | } |
| 530 | dev_info(&client->dev, "gpios %i...%i\n", |
| 531 | pca955x->gpio.base, pca955x->gpio.base + |
| 532 | pca955x->gpio.ngpio - 1); |
| 533 | } |
| 534 | #endif |
| 535 | |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 536 | return 0; |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static struct i2c_driver pca955x_driver = { |
| 540 | .driver = { |
| 541 | .name = "leds-pca955x", |
Tin Huynh | 44b3e31 | 2016-12-02 11:39:13 +0700 | [diff] [blame] | 542 | .acpi_match_table = ACPI_PTR(pca955x_acpi_ids), |
Cédric Le Goater | ed1f4b9 | 2017-08-08 15:42:37 +0200 | [diff] [blame] | 543 | .of_match_table = of_match_ptr(of_pca955x_match), |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 544 | }, |
| 545 | .probe = pca955x_probe, |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 546 | .id_table = pca955x_id, |
| 547 | }; |
| 548 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 549 | module_i2c_driver(pca955x_driver); |
Nate Case | f46e920 | 2008-07-16 22:49:55 +0100 | [diff] [blame] | 550 | |
| 551 | MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>"); |
| 552 | MODULE_DESCRIPTION("PCA955x LED driver"); |
| 553 | MODULE_LICENSE("GPL v2"); |