1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
4 *
5 * Author: Nate Case <ncase@xes-inc.com>
6 *
7 * LED driver for various PCA955x I2C LED drivers
8 *
9 * Supported devices:
10 *
11 * Device Description 7-bit slave address
12 * ------ ----------- -------------------
13 * PCA9550 2-bit driver 0x60 .. 0x61
14 * PCA9551 8-bit driver 0x60 .. 0x67
15 * PCA9552 16-bit driver 0x60 .. 0x67
16 * PCA9553/01 4-bit driver 0x62
17 * PCA9553/02 4-bit driver 0x63
18 *
19 * Philips PCA955x LED driver chips follow a register map as shown below:
20 *
21 * Control Register Description
22 * ---------------- -----------
23 * 0x0 Input register 0
24 * ..
25 * NUM_INPUT_REGS - 1 Last Input register X
26 *
27 * NUM_INPUT_REGS Frequency prescaler 0
28 * NUM_INPUT_REGS + 1 PWM register 0
29 * NUM_INPUT_REGS + 2 Frequency prescaler 1
30 * NUM_INPUT_REGS + 3 PWM register 1
31 *
32 * NUM_INPUT_REGS + 4 LED selector 0
33 * NUM_INPUT_REGS + 4
34 * + NUM_LED_REGS - 1 Last LED selector
35 *
36 * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
37 * bits the chip supports.
38 */
39
40 #include <linux/bitops.h>
41 #include <linux/ctype.h>
42 #include <linux/delay.h>
43 #include <linux/err.h>
44 #include <linux/gpio/driver.h>
45 #include <linux/i2c.h>
46 #include <linux/leds.h>
47 #include <linux/module.h>
48 #include <linux/of.h>
49 #include <linux/property.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52
53 #include <dt-bindings/leds/leds-pca955x.h>
54
55 /* LED select registers determine the source that drives LED outputs */
56 #define PCA955X_LS_LED_ON 0x0 /* Output LOW */
57 #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
58 #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
59 #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
60
61 #define PCA955X_GPIO_INPUT LED_OFF
62 #define PCA955X_GPIO_HIGH LED_OFF
63 #define PCA955X_GPIO_LOW LED_FULL
64
65 #define PCA955X_BLINK_DEFAULT_MS 1000
66
67 enum pca955x_type {
68 pca9550,
69 pca9551,
70 pca9552,
71 ibm_pca9552,
72 pca9553,
73 };
74
75 struct pca955x_chipdef {
76 int bits;
77 u8 slv_addr; /* 7-bit slave address mask */
78 int slv_addr_shift; /* Number of bits to ignore */
79 int blink_div; /* PSC divider */
80 };
81
82 static struct pca955x_chipdef pca955x_chipdefs[] = {
83 [pca9550] = {
84 .bits = 2,
85 .slv_addr = /* 110000x */ 0x60,
86 .slv_addr_shift = 1,
87 .blink_div = 44,
88 },
89 [pca9551] = {
90 .bits = 8,
91 .slv_addr = /* 1100xxx */ 0x60,
92 .slv_addr_shift = 3,
93 .blink_div = 38,
94 },
95 [pca9552] = {
96 .bits = 16,
97 .slv_addr = /* 1100xxx */ 0x60,
98 .slv_addr_shift = 3,
99 .blink_div = 44,
100 },
101 [ibm_pca9552] = {
102 .bits = 16,
103 .slv_addr = /* 0110xxx */ 0x30,
104 .slv_addr_shift = 3,
105 .blink_div = 44,
106 },
107 [pca9553] = {
108 .bits = 4,
109 .slv_addr = /* 110001x */ 0x62,
110 .slv_addr_shift = 1,
111 .blink_div = 44,
112 },
113 };
114
115 static const struct i2c_device_id pca955x_id[] = {
116 { "pca9550", pca9550 },
117 { "pca9551", pca9551 },
118 { "pca9552", pca9552 },
119 { "ibm-pca9552", ibm_pca9552 },
120 { "pca9553", pca9553 },
121 { }
122 };
123 MODULE_DEVICE_TABLE(i2c, pca955x_id);
124
125 struct pca955x {
126 struct mutex lock;
127 struct pca955x_led *leds;
128 struct pca955x_chipdef *chipdef;
129 struct i2c_client *client;
130 unsigned long active_blink;
131 unsigned long active_pins;
132 unsigned long blink_period;
133 #ifdef CONFIG_LEDS_PCA955X_GPIO
134 struct gpio_chip gpio;
135 #endif
136 };
137
138 struct pca955x_led {
139 struct pca955x *pca955x;
140 struct led_classdev led_cdev;
141 int led_num; /* 0 .. 15 potentially */
142 u32 type;
143 enum led_default_state default_state;
144 struct fwnode_handle *fwnode;
145 };
146
147 #define led_to_pca955x(l) container_of(l, struct pca955x_led, led_cdev)
148
149 struct pca955x_platform_data {
150 struct pca955x_led *leds;
151 int num_leds;
152 };
153
154 /* 8 bits per input register */
pca955x_num_input_regs(int bits)155 static inline int pca955x_num_input_regs(int bits)
156 {
157 return (bits + 7) / 8;
158 }
159
160 /* 4 bits per LED selector register */
pca955x_num_led_regs(int bits)161 static inline int pca955x_num_led_regs(int bits)
162 {
163 return (bits + 3) / 4;
164 }
165
166 /*
167 * Return an LED selector register value based on an existing one, with
168 * the appropriate 2-bit state value set for the given LED number (0-3).
169 */
pca955x_ledsel(u8 oldval,int led_num,int state)170 static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
171 {
172 return (oldval & (~(0x3 << (led_num << 1)))) |
173 ((state & 0x3) << (led_num << 1));
174 }
175
pca955x_ledstate(u8 ls,int led_num)176 static inline int pca955x_ledstate(u8 ls, int led_num)
177 {
178 return (ls >> (led_num << 1)) & 0x3;
179 }
180
181 /*
182 * Write to frequency prescaler register, used to program the
183 * period of the PWM output. period = (PSCx + 1) / coeff
184 * Where for pca9551 chips coeff = 38 and for all other chips coeff = 44
185 */
pca955x_write_psc(struct pca955x * pca955x,int n,u8 val)186 static int pca955x_write_psc(struct pca955x *pca955x, int n, u8 val)
187 {
188 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
189 int ret;
190
191 ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
192 if (ret < 0)
193 dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
194 val, ret);
195 return ret;
196 }
197
198 /*
199 * Write to PWM register, which determines the duty cycle of the
200 * output. LED is OFF when the count is less than the value of this
201 * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
202 *
203 * Duty cycle is (256 - PWMx) / 256
204 */
pca955x_write_pwm(struct pca955x * pca955x,int n,u8 val)205 static int pca955x_write_pwm(struct pca955x *pca955x, int n, u8 val)
206 {
207 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
208 int ret;
209
210 ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
211 if (ret < 0)
212 dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
213 val, ret);
214 return ret;
215 }
216
217 /*
218 * Write to LED selector register, which determines the source that
219 * drives the LED output.
220 */
pca955x_write_ls(struct pca955x * pca955x,int n,u8 val)221 static int pca955x_write_ls(struct pca955x *pca955x, int n, u8 val)
222 {
223 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
224 int ret;
225
226 ret = i2c_smbus_write_byte_data(pca955x->client, cmd, val);
227 if (ret < 0)
228 dev_err(&pca955x->client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", __func__, n,
229 val, ret);
230 return ret;
231 }
232
233 /*
234 * Read the LED selector register, which determines the source that
235 * drives the LED output.
236 */
pca955x_read_ls(struct pca955x * pca955x,int n,u8 * val)237 static int pca955x_read_ls(struct pca955x *pca955x, int n, u8 *val)
238 {
239 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 4 + n;
240 int ret;
241
242 ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
243 if (ret < 0) {
244 dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
245 return ret;
246 }
247 *val = (u8)ret;
248 return 0;
249 }
250
pca955x_read_pwm(struct pca955x * pca955x,int n,u8 * val)251 static int pca955x_read_pwm(struct pca955x *pca955x, int n, u8 *val)
252 {
253 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
254 int ret;
255
256 ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
257 if (ret < 0) {
258 dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
259 return ret;
260 }
261 *val = (u8)ret;
262 return 0;
263 }
264
pca955x_read_psc(struct pca955x * pca955x,int n,u8 * val)265 static int pca955x_read_psc(struct pca955x *pca955x, int n, u8 *val)
266 {
267 u8 cmd = pca955x_num_input_regs(pca955x->chipdef->bits) + (2 * n);
268 int ret;
269
270 ret = i2c_smbus_read_byte_data(pca955x->client, cmd);
271 if (ret < 0) {
272 dev_err(&pca955x->client->dev, "%s: reg 0x%x, err %d\n", __func__, n, ret);
273 return ret;
274 }
275 *val = (u8)ret;
276 return 0;
277 }
278
pca955x_led_get(struct led_classdev * led_cdev)279 static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
280 {
281 struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
282 struct pca955x *pca955x = pca955x_led->pca955x;
283 u8 ls, pwm;
284 int ret;
285
286 ret = pca955x_read_ls(pca955x, pca955x_led->led_num / 4, &ls);
287 if (ret)
288 return ret;
289
290 switch (pca955x_ledstate(ls, pca955x_led->led_num % 4)) {
291 case PCA955X_LS_LED_ON:
292 case PCA955X_LS_BLINK0:
293 ret = LED_FULL;
294 break;
295 case PCA955X_LS_LED_OFF:
296 ret = LED_OFF;
297 break;
298 case PCA955X_LS_BLINK1:
299 ret = pca955x_read_pwm(pca955x, 1, &pwm);
300 if (ret)
301 return ret;
302 ret = 255 - pwm;
303 break;
304 }
305
306 return ret;
307 }
308
pca955x_led_set(struct led_classdev * led_cdev,enum led_brightness value)309 static int pca955x_led_set(struct led_classdev *led_cdev,
310 enum led_brightness value)
311 {
312 struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
313 struct pca955x *pca955x = pca955x_led->pca955x;
314 int reg = pca955x_led->led_num / 4;
315 int bit = pca955x_led->led_num % 4;
316 u8 ls;
317 int ret;
318
319 mutex_lock(&pca955x->lock);
320
321 ret = pca955x_read_ls(pca955x, reg, &ls);
322 if (ret)
323 goto out;
324
325 if (test_bit(pca955x_led->led_num, &pca955x->active_blink)) {
326 if (value == LED_OFF) {
327 clear_bit(pca955x_led->led_num, &pca955x->active_blink);
328 ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
329 } else {
330 /* No variable brightness for blinking LEDs */
331 goto out;
332 }
333 } else {
334 switch (value) {
335 case LED_FULL:
336 ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_ON);
337 break;
338 case LED_OFF:
339 ls = pca955x_ledsel(ls, bit, PCA955X_LS_LED_OFF);
340 break;
341 default:
342 /*
343 * Use PWM1 for all other values. This has the unwanted
344 * side effect of making all LEDs on the chip share the
345 * same brightness level if set to a value other than
346 * OFF or FULL. But, this is probably better than just
347 * turning off for all other values.
348 */
349 ret = pca955x_write_pwm(pca955x, 1, 255 - value);
350 if (ret)
351 goto out;
352 ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK1);
353 break;
354 }
355 }
356
357 ret = pca955x_write_ls(pca955x, reg, ls);
358
359 out:
360 mutex_unlock(&pca955x->lock);
361
362 return ret;
363 }
364
pca955x_period_to_psc(struct pca955x * pca955x,unsigned long p)365 static u8 pca955x_period_to_psc(struct pca955x *pca955x, unsigned long p)
366 {
367 p *= pca955x->chipdef->blink_div;
368 p /= MSEC_PER_SEC;
369 p -= 1;
370
371 return p;
372 }
373
pca955x_psc_to_period(struct pca955x * pca955x,u8 psc)374 static unsigned long pca955x_psc_to_period(struct pca955x *pca955x, u8 psc)
375 {
376 unsigned long p = psc;
377
378 p += 1;
379 p *= MSEC_PER_SEC;
380 p /= pca955x->chipdef->blink_div;
381
382 return p;
383 }
384
pca955x_led_blink(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)385 static int pca955x_led_blink(struct led_classdev *led_cdev,
386 unsigned long *delay_on, unsigned long *delay_off)
387 {
388 struct pca955x_led *pca955x_led = led_to_pca955x(led_cdev);
389 struct pca955x *pca955x = pca955x_led->pca955x;
390 unsigned long p = *delay_on + *delay_off;
391 int ret = 0;
392
393 mutex_lock(&pca955x->lock);
394
395 if (p) {
396 if (*delay_on != *delay_off) {
397 ret = -EINVAL;
398 goto out;
399 }
400
401 if (p < pca955x_psc_to_period(pca955x, 0) ||
402 p > pca955x_psc_to_period(pca955x, 0xff)) {
403 ret = -EINVAL;
404 goto out;
405 }
406 } else {
407 p = pca955x->active_blink ? pca955x->blink_period :
408 PCA955X_BLINK_DEFAULT_MS;
409 }
410
411 if (!pca955x->active_blink ||
412 pca955x->active_blink == BIT(pca955x_led->led_num) ||
413 pca955x->blink_period == p) {
414 u8 psc = pca955x_period_to_psc(pca955x, p);
415
416 if (!test_and_set_bit(pca955x_led->led_num,
417 &pca955x->active_blink)) {
418 u8 ls;
419 int reg = pca955x_led->led_num / 4;
420 int bit = pca955x_led->led_num % 4;
421
422 ret = pca955x_read_ls(pca955x, reg, &ls);
423 if (ret)
424 goto out;
425
426 ls = pca955x_ledsel(ls, bit, PCA955X_LS_BLINK0);
427 ret = pca955x_write_ls(pca955x, reg, ls);
428 if (ret)
429 goto out;
430
431 /*
432 * Force 50% duty cycle to maintain the specified
433 * blink rate.
434 */
435 ret = pca955x_write_pwm(pca955x, 0, 128);
436 if (ret)
437 goto out;
438 }
439
440 if (pca955x->blink_period != p) {
441 pca955x->blink_period = p;
442 ret = pca955x_write_psc(pca955x, 0, psc);
443 if (ret)
444 goto out;
445 }
446
447 p = pca955x_psc_to_period(pca955x, psc);
448 p /= 2;
449 *delay_on = p;
450 *delay_off = p;
451 } else {
452 ret = -EBUSY;
453 }
454
455 out:
456 mutex_unlock(&pca955x->lock);
457
458 return ret;
459 }
460
461 #ifdef CONFIG_LEDS_PCA955X_GPIO
462 /*
463 * Read the INPUT register, which contains the state of LEDs.
464 */
pca955x_read_input(struct i2c_client * client,int n,u8 * val)465 static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
466 {
467 int ret = i2c_smbus_read_byte_data(client, n);
468
469 if (ret < 0) {
470 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
471 __func__, n, ret);
472 return ret;
473 }
474 *val = (u8)ret;
475 return 0;
476
477 }
478
pca955x_gpio_request_pin(struct gpio_chip * gc,unsigned int offset)479 static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
480 {
481 struct pca955x *pca955x = gpiochip_get_data(gc);
482
483 return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
484 }
485
pca955x_gpio_free_pin(struct gpio_chip * gc,unsigned int offset)486 static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
487 {
488 struct pca955x *pca955x = gpiochip_get_data(gc);
489
490 clear_bit(offset, &pca955x->active_pins);
491 }
492
pca955x_set_value(struct gpio_chip * gc,unsigned int offset,int val)493 static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
494 int val)
495 {
496 struct pca955x *pca955x = gpiochip_get_data(gc);
497 struct pca955x_led *led = &pca955x->leds[offset];
498
499 if (val)
500 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
501
502 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
503 }
504
pca955x_gpio_set_value(struct gpio_chip * gc,unsigned int offset,int val)505 static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
506 int val)
507 {
508 pca955x_set_value(gc, offset, val);
509 }
510
pca955x_gpio_get_value(struct gpio_chip * gc,unsigned int offset)511 static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
512 {
513 struct pca955x *pca955x = gpiochip_get_data(gc);
514 struct pca955x_led *led = &pca955x->leds[offset];
515 u8 reg = 0;
516
517 /* There is nothing we can do about errors */
518 pca955x_read_input(pca955x->client, led->led_num / 8, ®);
519
520 return !!(reg & (1 << (led->led_num % 8)));
521 }
522
pca955x_gpio_direction_input(struct gpio_chip * gc,unsigned int offset)523 static int pca955x_gpio_direction_input(struct gpio_chip *gc,
524 unsigned int offset)
525 {
526 struct pca955x *pca955x = gpiochip_get_data(gc);
527 struct pca955x_led *led = &pca955x->leds[offset];
528
529 /* To use as input ensure pin is not driven. */
530 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
531 }
532
pca955x_gpio_direction_output(struct gpio_chip * gc,unsigned int offset,int val)533 static int pca955x_gpio_direction_output(struct gpio_chip *gc,
534 unsigned int offset, int val)
535 {
536 return pca955x_set_value(gc, offset, val);
537 }
538 #endif /* CONFIG_LEDS_PCA955X_GPIO */
539
540 static struct pca955x_platform_data *
pca955x_get_pdata(struct i2c_client * client,struct pca955x_chipdef * chip)541 pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
542 {
543 struct pca955x_platform_data *pdata;
544 struct pca955x_led *led;
545 struct fwnode_handle *child;
546 int count;
547
548 count = device_get_child_node_count(&client->dev);
549 if (count > chip->bits)
550 return ERR_PTR(-ENODEV);
551
552 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
553 if (!pdata)
554 return ERR_PTR(-ENOMEM);
555
556 pdata->leds = devm_kcalloc(&client->dev,
557 chip->bits, sizeof(struct pca955x_led),
558 GFP_KERNEL);
559 if (!pdata->leds)
560 return ERR_PTR(-ENOMEM);
561
562 device_for_each_child_node(&client->dev, child) {
563 u32 reg;
564 int res;
565
566 res = fwnode_property_read_u32(child, "reg", ®);
567 if ((res != 0) || (reg >= chip->bits))
568 continue;
569
570 led = &pdata->leds[reg];
571 led->type = PCA955X_TYPE_LED;
572 led->fwnode = child;
573 led->default_state = led_init_default_state_get(child);
574
575 fwnode_property_read_u32(child, "type", &led->type);
576 }
577
578 pdata->num_leds = chip->bits;
579
580 return pdata;
581 }
582
583 static const struct of_device_id of_pca955x_match[] = {
584 { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
585 { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
586 { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
587 { .compatible = "ibm,pca9552", .data = (void *)ibm_pca9552 },
588 { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
589 {},
590 };
591 MODULE_DEVICE_TABLE(of, of_pca955x_match);
592
pca955x_probe(struct i2c_client * client)593 static int pca955x_probe(struct i2c_client *client)
594 {
595 struct pca955x *pca955x;
596 struct pca955x_led *pca955x_led;
597 struct pca955x_chipdef *chip;
598 struct led_classdev *led;
599 struct led_init_data init_data;
600 struct i2c_adapter *adapter;
601 int i, bit, err, nls, reg;
602 u8 ls1[4];
603 u8 ls2[4];
604 struct pca955x_platform_data *pdata;
605 u8 psc0;
606 bool keep_psc0 = false;
607 bool set_default_label = false;
608 char default_label[8];
609 enum pca955x_type chip_type;
610 const void *md = device_get_match_data(&client->dev);
611
612 if (md) {
613 chip_type = (enum pca955x_type)md;
614 } else {
615 const struct i2c_device_id *id = i2c_match_id(pca955x_id,
616 client);
617
618 if (id) {
619 chip_type = (enum pca955x_type)id->driver_data;
620 } else {
621 dev_err(&client->dev, "unknown chip\n");
622 return -ENODEV;
623 }
624 }
625
626 chip = &pca955x_chipdefs[chip_type];
627 adapter = client->adapter;
628 pdata = dev_get_platdata(&client->dev);
629 if (!pdata) {
630 pdata = pca955x_get_pdata(client, chip);
631 if (IS_ERR(pdata))
632 return PTR_ERR(pdata);
633 }
634
635 /* Make sure the slave address / chip type combo given is possible */
636 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
637 chip->slv_addr) {
638 dev_err(&client->dev, "invalid slave address %02x\n",
639 client->addr);
640 return -ENODEV;
641 }
642
643 dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
644 "slave address 0x%02x\n", client->name, chip->bits,
645 client->addr);
646
647 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
648 return -EIO;
649
650 if (pdata->num_leds != chip->bits) {
651 dev_err(&client->dev,
652 "board info claims %d LEDs on a %d-bit chip\n",
653 pdata->num_leds, chip->bits);
654 return -ENODEV;
655 }
656
657 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
658 if (!pca955x)
659 return -ENOMEM;
660
661 pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
662 sizeof(*pca955x_led), GFP_KERNEL);
663 if (!pca955x->leds)
664 return -ENOMEM;
665
666 i2c_set_clientdata(client, pca955x);
667
668 mutex_init(&pca955x->lock);
669 pca955x->client = client;
670 pca955x->chipdef = chip;
671 pca955x->blink_period = PCA955X_BLINK_DEFAULT_MS;
672
673 init_data.devname_mandatory = false;
674 init_data.devicename = "pca955x";
675
676 nls = pca955x_num_led_regs(chip->bits);
677 /* use auto-increment feature to read all the led selectors at once */
678 err = i2c_smbus_read_i2c_block_data(client,
679 0x10 | (pca955x_num_input_regs(chip->bits) + 4), nls,
680 ls1);
681 if (err < 0)
682 return err;
683
684 for (i = 0; i < nls; ++i)
685 ls2[i] = ls1[i];
686
687 for (i = 0; i < chip->bits; i++) {
688 pca955x_led = &pca955x->leds[i];
689 pca955x_led->led_num = i;
690 pca955x_led->pca955x = pca955x;
691 pca955x_led->type = pdata->leds[i].type;
692
693 switch (pca955x_led->type) {
694 case PCA955X_TYPE_NONE:
695 case PCA955X_TYPE_GPIO:
696 break;
697 case PCA955X_TYPE_LED:
698 bit = i % 4;
699 reg = i / 4;
700 led = &pca955x_led->led_cdev;
701 led->brightness_set_blocking = pca955x_led_set;
702 led->brightness_get = pca955x_led_get;
703 led->blink_set = pca955x_led_blink;
704
705 if (pdata->leds[i].default_state == LEDS_DEFSTATE_OFF)
706 ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_OFF);
707 else if (pdata->leds[i].default_state == LEDS_DEFSTATE_ON)
708 ls2[reg] = pca955x_ledsel(ls2[reg], bit, PCA955X_LS_LED_ON);
709 else if (pca955x_ledstate(ls2[reg], bit) == PCA955X_LS_BLINK0) {
710 keep_psc0 = true;
711 set_bit(i, &pca955x->active_blink);
712 }
713
714 init_data.fwnode = pdata->leds[i].fwnode;
715
716 if (is_of_node(init_data.fwnode)) {
717 if (to_of_node(init_data.fwnode)->name[0] ==
718 '\0')
719 set_default_label = true;
720 else
721 set_default_label = false;
722 } else {
723 set_default_label = true;
724 }
725
726 if (set_default_label) {
727 snprintf(default_label, sizeof(default_label),
728 "%d", i);
729 init_data.default_label = default_label;
730 } else {
731 init_data.default_label = NULL;
732 }
733
734 err = devm_led_classdev_register_ext(&client->dev, led,
735 &init_data);
736 if (err)
737 return err;
738
739 set_bit(i, &pca955x->active_pins);
740 }
741 }
742
743 for (i = 0; i < nls; ++i) {
744 if (ls1[i] != ls2[i]) {
745 err = pca955x_write_ls(pca955x, i, ls2[i]);
746 if (err)
747 return err;
748 }
749 }
750
751 if (keep_psc0) {
752 err = pca955x_read_psc(pca955x, 0, &psc0);
753 } else {
754 psc0 = pca955x_period_to_psc(pca955x, pca955x->blink_period);
755 err = pca955x_write_psc(pca955x, 0, psc0);
756 }
757
758 if (err)
759 return err;
760
761 pca955x->blink_period = pca955x_psc_to_period(pca955x, psc0);
762
763 /* Set PWM1 to fast frequency so we do not see flashing */
764 err = pca955x_write_psc(pca955x, 1, 0);
765 if (err)
766 return err;
767
768 #ifdef CONFIG_LEDS_PCA955X_GPIO
769 pca955x->gpio.label = "gpio-pca955x";
770 pca955x->gpio.direction_input = pca955x_gpio_direction_input;
771 pca955x->gpio.direction_output = pca955x_gpio_direction_output;
772 pca955x->gpio.set = pca955x_gpio_set_value;
773 pca955x->gpio.get = pca955x_gpio_get_value;
774 pca955x->gpio.request = pca955x_gpio_request_pin;
775 pca955x->gpio.free = pca955x_gpio_free_pin;
776 pca955x->gpio.can_sleep = 1;
777 pca955x->gpio.base = -1;
778 pca955x->gpio.ngpio = chip->bits;
779 pca955x->gpio.parent = &client->dev;
780 pca955x->gpio.owner = THIS_MODULE;
781
782 err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
783 pca955x);
784 if (err) {
785 /* Use data->gpio.dev as a flag for freeing gpiochip */
786 pca955x->gpio.parent = NULL;
787 dev_warn(&client->dev, "could not add gpiochip\n");
788 return err;
789 }
790 dev_info(&client->dev, "gpios %i...%i\n",
791 pca955x->gpio.base, pca955x->gpio.base +
792 pca955x->gpio.ngpio - 1);
793 #endif
794
795 return 0;
796 }
797
798 static struct i2c_driver pca955x_driver = {
799 .driver = {
800 .name = "leds-pca955x",
801 .of_match_table = of_pca955x_match,
802 },
803 .probe = pca955x_probe,
804 .id_table = pca955x_id,
805 };
806
807 module_i2c_driver(pca955x_driver);
808
809 MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
810 MODULE_DESCRIPTION("PCA955x LED driver");
811 MODULE_LICENSE("GPL v2");
812