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