1 // SPDX-License-Identifier: GPL-2.0-only
3 * pca9532.c - 16-bit Led dimmer
5 * Copyright (C) 2011 Jan Weitzel
6 * Copyright (C) 2008 Riku Voipio
8 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/leds.h>
15 #include <linux/input.h>
16 #include <linux/mutex.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds-pca9532.h>
19 #include <linux/gpio/driver.h>
21 #include <linux/of_device.h>
24 #define PCA9532_REG_INPUT(i) ((i) >> 3)
25 #define PCA9532_REG_OFFSET(m) ((m) >> 4)
26 #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
27 #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
28 #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
29 #define LED_NUM(led) (led & 0x3)
30 #define LED_SHIFT(led) (LED_NUM(led) * 2)
31 #define LED_MASK(led) (0x3 << LED_SHIFT(led))
33 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
35 struct pca9532_chip_info
{
40 struct i2c_client
*client
;
41 struct pca9532_led leds
[16];
42 struct mutex update_lock
;
43 struct input_dev
*idev
;
44 struct work_struct work
;
45 #ifdef CONFIG_LEDS_PCA9532_GPIO
46 struct gpio_chip gpio
;
48 const struct pca9532_chip_info
*chip_info
;
53 static int pca9532_probe(struct i2c_client
*client
,
54 const struct i2c_device_id
*id
);
55 static int pca9532_remove(struct i2c_client
*client
);
64 static const struct i2c_device_id pca9532_id
[] = {
65 { "pca9530", pca9530
},
66 { "pca9531", pca9531
},
67 { "pca9532", pca9532
},
68 { "pca9533", pca9533
},
72 MODULE_DEVICE_TABLE(i2c
, pca9532_id
);
74 static const struct pca9532_chip_info pca9532_chip_info_tbl
[] = {
90 static const struct of_device_id of_pca9532_leds_match
[] = {
91 { .compatible
= "nxp,pca9530", .data
= (void *)pca9530
},
92 { .compatible
= "nxp,pca9531", .data
= (void *)pca9531
},
93 { .compatible
= "nxp,pca9532", .data
= (void *)pca9532
},
94 { .compatible
= "nxp,pca9533", .data
= (void *)pca9533
},
98 MODULE_DEVICE_TABLE(of
, of_pca9532_leds_match
);
101 static struct i2c_driver pca9532_driver
= {
103 .name
= "leds-pca953x",
104 .of_match_table
= of_match_ptr(of_pca9532_leds_match
),
106 .probe
= pca9532_probe
,
107 .remove
= pca9532_remove
,
108 .id_table
= pca9532_id
,
111 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
112 * the clever Thecus people are using one pwm to drive the beeper. So,
113 * as a compromise we average one pwm to the values requested by all
114 * leds that are not ON/OFF.
116 static int pca9532_calcpwm(struct i2c_client
*client
, int pwm
, int blink
,
117 enum led_brightness value
)
119 int a
= 0, b
= 0, i
= 0;
120 struct pca9532_data
*data
= i2c_get_clientdata(client
);
121 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
122 if (data
->leds
[i
].type
== PCA9532_TYPE_LED
&&
123 data
->leds
[i
].state
== PCA9532_PWM0
+pwm
) {
125 b
+= data
->leds
[i
].ldev
.brightness
;
129 dev_err(&client
->dev
,
130 "fear of division by zero %d/%d, wanted %d\n",
138 data
->psc
[pwm
] = blink
;
142 static int pca9532_setpwm(struct i2c_client
*client
, int pwm
)
144 struct pca9532_data
*data
= i2c_get_clientdata(client
);
145 u8 maxleds
= data
->chip_info
->num_leds
;
147 mutex_lock(&data
->update_lock
);
148 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, pwm
),
150 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, pwm
),
152 mutex_unlock(&data
->update_lock
);
156 /* Set LED routing */
157 static void pca9532_setled(struct pca9532_led
*led
)
159 struct i2c_client
*client
= led
->client
;
160 struct pca9532_data
*data
= i2c_get_clientdata(client
);
161 u8 maxleds
= data
->chip_info
->num_leds
;
164 mutex_lock(&data
->update_lock
);
165 reg
= i2c_smbus_read_byte_data(client
, LED_REG(maxleds
, led
->id
));
167 reg
= reg
& ~LED_MASK(led
->id
);
168 /* set the new value */
169 reg
= reg
| (led
->state
<< LED_SHIFT(led
->id
));
170 i2c_smbus_write_byte_data(client
, LED_REG(maxleds
, led
->id
), reg
);
171 mutex_unlock(&data
->update_lock
);
174 static int pca9532_set_brightness(struct led_classdev
*led_cdev
,
175 enum led_brightness value
)
178 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
180 if (value
== LED_OFF
)
181 led
->state
= PCA9532_OFF
;
182 else if (value
== LED_FULL
)
183 led
->state
= PCA9532_ON
;
185 led
->state
= PCA9532_PWM0
; /* Thecus: hardcode one pwm */
186 err
= pca9532_calcpwm(led
->client
, 0, 0, value
);
190 if (led
->state
== PCA9532_PWM0
)
191 pca9532_setpwm(led
->client
, 0);
196 static int pca9532_set_blink(struct led_classdev
*led_cdev
,
197 unsigned long *delay_on
, unsigned long *delay_off
)
199 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
200 struct i2c_client
*client
= led
->client
;
204 if (*delay_on
== 0 && *delay_off
== 0) {
205 /* led subsystem ask us for a blink rate */
209 if (*delay_on
!= *delay_off
|| *delay_on
> 1690 || *delay_on
< 6)
212 /* Thecus specific: only use PSC/PWM 0 */
213 psc
= (*delay_on
* 152-1)/1000;
214 err
= pca9532_calcpwm(client
, 0, psc
, led_cdev
->brightness
);
217 if (led
->state
== PCA9532_PWM0
)
218 pca9532_setpwm(led
->client
, 0);
224 static int pca9532_event(struct input_dev
*dev
, unsigned int type
,
225 unsigned int code
, int value
)
227 struct pca9532_data
*data
= input_get_drvdata(dev
);
229 if (!(type
== EV_SND
&& (code
== SND_BELL
|| code
== SND_TONE
)))
232 /* XXX: allow different kind of beeps with psc/pwm modifications */
233 if (value
> 1 && value
< 32767)
238 schedule_work(&data
->work
);
243 static void pca9532_input_work(struct work_struct
*work
)
245 struct pca9532_data
*data
=
246 container_of(work
, struct pca9532_data
, work
);
247 u8 maxleds
= data
->chip_info
->num_leds
;
249 mutex_lock(&data
->update_lock
);
250 i2c_smbus_write_byte_data(data
->client
, PCA9532_REG_PWM(maxleds
, 1),
252 mutex_unlock(&data
->update_lock
);
255 static enum pca9532_state
pca9532_getled(struct pca9532_led
*led
)
257 struct i2c_client
*client
= led
->client
;
258 struct pca9532_data
*data
= i2c_get_clientdata(client
);
259 u8 maxleds
= data
->chip_info
->num_leds
;
261 enum pca9532_state ret
;
263 mutex_lock(&data
->update_lock
);
264 reg
= i2c_smbus_read_byte_data(client
, LED_REG(maxleds
, led
->id
));
265 ret
= (reg
& LED_MASK(led
->id
)) >> LED_SHIFT(led
->id
);
266 mutex_unlock(&data
->update_lock
);
270 #ifdef CONFIG_LEDS_PCA9532_GPIO
271 static int pca9532_gpio_request_pin(struct gpio_chip
*gc
, unsigned offset
)
273 struct pca9532_data
*data
= gpiochip_get_data(gc
);
274 struct pca9532_led
*led
= &data
->leds
[offset
];
276 if (led
->type
== PCA9532_TYPE_GPIO
)
282 static void pca9532_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int val
)
284 struct pca9532_data
*data
= gpiochip_get_data(gc
);
285 struct pca9532_led
*led
= &data
->leds
[offset
];
288 led
->state
= PCA9532_ON
;
290 led
->state
= PCA9532_OFF
;
295 static int pca9532_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
297 struct pca9532_data
*data
= gpiochip_get_data(gc
);
300 reg
= i2c_smbus_read_byte_data(data
->client
, PCA9532_REG_INPUT(offset
));
302 return !!(reg
& (1 << (offset
% 8)));
305 static int pca9532_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
307 /* To use as input ensure pin is not driven */
308 pca9532_gpio_set_value(gc
, offset
, 0);
313 static int pca9532_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
, int val
)
315 pca9532_gpio_set_value(gc
, offset
, val
);
319 #endif /* CONFIG_LEDS_PCA9532_GPIO */
321 static int pca9532_destroy_devices(struct pca9532_data
*data
, int n_devs
)
329 switch (data
->leds
[i
].type
) {
330 case PCA9532_TYPE_NONE
:
331 case PCA9532_TYPE_GPIO
:
333 case PCA9532_TYPE_LED
:
334 led_classdev_unregister(&data
->leds
[i
].ldev
);
336 case PCA9532_TYPE_N2100_BEEP
:
337 if (data
->idev
!= NULL
) {
338 cancel_work_sync(&data
->work
);
345 #ifdef CONFIG_LEDS_PCA9532_GPIO
346 if (data
->gpio
.parent
)
347 gpiochip_remove(&data
->gpio
);
353 static int pca9532_configure(struct i2c_client
*client
,
354 struct pca9532_data
*data
, struct pca9532_platform_data
*pdata
)
358 u8 maxleds
= data
->chip_info
->num_leds
;
360 for (i
= 0; i
< 2; i
++) {
361 data
->pwm
[i
] = pdata
->pwm
[i
];
362 data
->psc
[i
] = pdata
->psc
[i
];
363 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, i
),
365 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, i
),
369 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
370 struct pca9532_led
*led
= &data
->leds
[i
];
371 struct pca9532_led
*pled
= &pdata
->leds
[i
];
372 led
->client
= client
;
374 led
->type
= pled
->type
;
376 case PCA9532_TYPE_NONE
:
378 case PCA9532_TYPE_GPIO
:
381 case PCA9532_TYPE_LED
:
382 if (pled
->state
== PCA9532_KEEP
)
383 led
->state
= pca9532_getled(led
);
385 led
->state
= pled
->state
;
386 led
->name
= pled
->name
;
387 led
->ldev
.name
= led
->name
;
388 led
->ldev
.default_trigger
= pled
->default_trigger
;
389 led
->ldev
.brightness
= LED_OFF
;
390 led
->ldev
.brightness_set_blocking
=
391 pca9532_set_brightness
;
392 led
->ldev
.blink_set
= pca9532_set_blink
;
393 err
= led_classdev_register(&client
->dev
, &led
->ldev
);
395 dev_err(&client
->dev
,
396 "couldn't register LED %s\n",
402 case PCA9532_TYPE_N2100_BEEP
:
404 led
->state
= PCA9532_PWM1
;
406 data
->idev
= devm_input_allocate_device(&client
->dev
);
407 if (data
->idev
== NULL
) {
411 data
->idev
->name
= pled
->name
;
412 data
->idev
->phys
= "i2c/pca9532";
413 data
->idev
->id
.bustype
= BUS_HOST
;
414 data
->idev
->id
.vendor
= 0x001f;
415 data
->idev
->id
.product
= 0x0001;
416 data
->idev
->id
.version
= 0x0100;
417 data
->idev
->evbit
[0] = BIT_MASK(EV_SND
);
418 data
->idev
->sndbit
[0] = BIT_MASK(SND_BELL
) |
420 data
->idev
->event
= pca9532_event
;
421 input_set_drvdata(data
->idev
, data
);
422 INIT_WORK(&data
->work
, pca9532_input_work
);
423 err
= input_register_device(data
->idev
);
425 cancel_work_sync(&data
->work
);
433 #ifdef CONFIG_LEDS_PCA9532_GPIO
435 data
->gpio
.label
= "gpio-pca9532";
436 data
->gpio
.direction_input
= pca9532_gpio_direction_input
;
437 data
->gpio
.direction_output
= pca9532_gpio_direction_output
;
438 data
->gpio
.set
= pca9532_gpio_set_value
;
439 data
->gpio
.get
= pca9532_gpio_get_value
;
440 data
->gpio
.request
= pca9532_gpio_request_pin
;
441 data
->gpio
.can_sleep
= 1;
442 data
->gpio
.base
= pdata
->gpio_base
;
443 data
->gpio
.ngpio
= data
->chip_info
->num_leds
;
444 data
->gpio
.parent
= &client
->dev
;
445 data
->gpio
.owner
= THIS_MODULE
;
447 err
= gpiochip_add_data(&data
->gpio
, data
);
449 /* Use data->gpio.dev as a flag for freeing gpiochip */
450 data
->gpio
.parent
= NULL
;
451 dev_warn(&client
->dev
, "could not add gpiochip\n");
453 dev_info(&client
->dev
, "gpios %i...%i\n",
454 data
->gpio
.base
, data
->gpio
.base
+
455 data
->gpio
.ngpio
- 1);
463 pca9532_destroy_devices(data
, i
);
467 static struct pca9532_platform_data
*
468 pca9532_of_populate_pdata(struct device
*dev
, struct device_node
*np
)
470 struct pca9532_platform_data
*pdata
;
471 struct device_node
*child
;
476 devid
= (int)(uintptr_t)of_device_get_match_data(dev
);
477 maxleds
= pca9532_chip_info_tbl
[devid
].num_leds
;
479 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
481 return ERR_PTR(-ENOMEM
);
483 of_property_read_u8_array(np
, "nxp,pwm", &pdata
->pwm
[0],
484 ARRAY_SIZE(pdata
->pwm
));
485 of_property_read_u8_array(np
, "nxp,psc", &pdata
->psc
[0],
486 ARRAY_SIZE(pdata
->psc
));
488 for_each_available_child_of_node(np
, child
) {
489 if (of_property_read_string(child
, "label",
490 &pdata
->leds
[i
].name
))
491 pdata
->leds
[i
].name
= child
->name
;
492 of_property_read_u32(child
, "type", &pdata
->leds
[i
].type
);
493 of_property_read_string(child
, "linux,default-trigger",
494 &pdata
->leds
[i
].default_trigger
);
495 if (!of_property_read_string(child
, "default-state", &state
)) {
496 if (!strcmp(state
, "on"))
497 pdata
->leds
[i
].state
= PCA9532_ON
;
498 else if (!strcmp(state
, "keep"))
499 pdata
->leds
[i
].state
= PCA9532_KEEP
;
501 if (++i
>= maxleds
) {
510 static int pca9532_probe(struct i2c_client
*client
,
511 const struct i2c_device_id
*id
)
514 struct pca9532_data
*data
= i2c_get_clientdata(client
);
515 struct pca9532_platform_data
*pca9532_pdata
=
516 dev_get_platdata(&client
->dev
);
517 struct device_node
*np
= dev_of_node(&client
->dev
);
519 if (!pca9532_pdata
) {
522 pca9532_of_populate_pdata(&client
->dev
, np
);
523 if (IS_ERR(pca9532_pdata
))
524 return PTR_ERR(pca9532_pdata
);
526 dev_err(&client
->dev
, "no platform data\n");
529 devid
= (int)(uintptr_t)of_device_get_match_data(&client
->dev
);
531 devid
= id
->driver_data
;
534 if (!i2c_check_functionality(client
->adapter
,
535 I2C_FUNC_SMBUS_BYTE_DATA
))
538 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
542 data
->chip_info
= &pca9532_chip_info_tbl
[devid
];
544 dev_info(&client
->dev
, "setting platform data\n");
545 i2c_set_clientdata(client
, data
);
546 data
->client
= client
;
547 mutex_init(&data
->update_lock
);
549 return pca9532_configure(client
, data
, pca9532_pdata
);
552 static int pca9532_remove(struct i2c_client
*client
)
554 struct pca9532_data
*data
= i2c_get_clientdata(client
);
556 return pca9532_destroy_devices(data
, data
->chip_info
->num_leds
);
559 module_i2c_driver(pca9532_driver
);
561 MODULE_AUTHOR("Riku Voipio");
562 MODULE_LICENSE("GPL");
563 MODULE_DESCRIPTION("PCA 9532 LED dimmer");