2 * pca9532.c - 16-bit Led dimmer
4 * Copyright (C) 2011 Jan Weitzel
5 * Copyright (C) 2008 Riku Voipio
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/leds.h>
19 #include <linux/input.h>
20 #include <linux/mutex.h>
21 #include <linux/workqueue.h>
22 #include <linux/leds-pca9532.h>
23 #include <linux/gpio.h>
25 #include <linux/of_device.h>
28 #define PCA9532_REG_INPUT(i) ((i) >> 3)
29 #define PCA9532_REG_OFFSET(m) ((m) >> 4)
30 #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
31 #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
32 #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
33 #define LED_NUM(led) (led & 0x3)
35 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
37 struct pca9532_chip_info
{
42 struct i2c_client
*client
;
43 struct pca9532_led leds
[16];
44 struct mutex update_lock
;
45 struct input_dev
*idev
;
46 struct work_struct work
;
47 #ifdef CONFIG_LEDS_PCA9532_GPIO
48 struct gpio_chip gpio
;
50 const struct pca9532_chip_info
*chip_info
;
55 static int pca9532_probe(struct i2c_client
*client
,
56 const struct i2c_device_id
*id
);
57 static int pca9532_remove(struct i2c_client
*client
);
66 static const struct i2c_device_id pca9532_id
[] = {
67 { "pca9530", pca9530
},
68 { "pca9531", pca9531
},
69 { "pca9532", pca9532
},
70 { "pca9533", pca9533
},
74 MODULE_DEVICE_TABLE(i2c
, pca9532_id
);
76 static const struct pca9532_chip_info pca9532_chip_info_tbl
[] = {
92 static const struct of_device_id of_pca9532_leds_match
[] = {
93 { .compatible
= "nxp,pca9530", .data
= (void *)pca9530
},
94 { .compatible
= "nxp,pca9531", .data
= (void *)pca9531
},
95 { .compatible
= "nxp,pca9532", .data
= (void *)pca9532
},
96 { .compatible
= "nxp,pca9533", .data
= (void *)pca9533
},
100 MODULE_DEVICE_TABLE(of
, of_pca9532_leds_match
);
103 static struct i2c_driver pca9532_driver
= {
105 .name
= "leds-pca953x",
106 .of_match_table
= of_match_ptr(of_pca9532_leds_match
),
108 .probe
= pca9532_probe
,
109 .remove
= pca9532_remove
,
110 .id_table
= pca9532_id
,
113 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
114 * the clever Thecus people are using one pwm to drive the beeper. So,
115 * as a compromise we average one pwm to the values requested by all
116 * leds that are not ON/OFF.
118 static int pca9532_calcpwm(struct i2c_client
*client
, int pwm
, int blink
,
119 enum led_brightness value
)
121 int a
= 0, b
= 0, i
= 0;
122 struct pca9532_data
*data
= i2c_get_clientdata(client
);
123 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
124 if (data
->leds
[i
].type
== PCA9532_TYPE_LED
&&
125 data
->leds
[i
].state
== PCA9532_PWM0
+pwm
) {
127 b
+= data
->leds
[i
].ldev
.brightness
;
131 dev_err(&client
->dev
,
132 "fear of division by zero %d/%d, wanted %d\n",
140 data
->psc
[pwm
] = blink
;
144 static int pca9532_setpwm(struct i2c_client
*client
, int pwm
)
146 struct pca9532_data
*data
= i2c_get_clientdata(client
);
147 u8 maxleds
= data
->chip_info
->num_leds
;
149 mutex_lock(&data
->update_lock
);
150 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, pwm
),
152 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, pwm
),
154 mutex_unlock(&data
->update_lock
);
158 /* Set LED routing */
159 static void pca9532_setled(struct pca9532_led
*led
)
161 struct i2c_client
*client
= led
->client
;
162 struct pca9532_data
*data
= i2c_get_clientdata(client
);
163 u8 maxleds
= data
->chip_info
->num_leds
;
166 mutex_lock(&data
->update_lock
);
167 reg
= i2c_smbus_read_byte_data(client
, LED_REG(maxleds
, led
->id
));
169 reg
= reg
& ~(0x3<<LED_NUM(led
->id
)*2);
170 /* set the new value */
171 reg
= reg
| (led
->state
<< LED_NUM(led
->id
)*2);
172 i2c_smbus_write_byte_data(client
, LED_REG(maxleds
, led
->id
), reg
);
173 mutex_unlock(&data
->update_lock
);
176 static int pca9532_set_brightness(struct led_classdev
*led_cdev
,
177 enum led_brightness value
)
180 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
182 if (value
== LED_OFF
)
183 led
->state
= PCA9532_OFF
;
184 else if (value
== LED_FULL
)
185 led
->state
= PCA9532_ON
;
187 led
->state
= PCA9532_PWM0
; /* Thecus: hardcode one pwm */
188 err
= pca9532_calcpwm(led
->client
, 0, 0, value
);
192 if (led
->state
== PCA9532_PWM0
)
193 pca9532_setpwm(led
->client
, 0);
198 static int pca9532_set_blink(struct led_classdev
*led_cdev
,
199 unsigned long *delay_on
, unsigned long *delay_off
)
201 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
202 struct i2c_client
*client
= led
->client
;
206 if (*delay_on
== 0 && *delay_off
== 0) {
207 /* led subsystem ask us for a blink rate */
211 if (*delay_on
!= *delay_off
|| *delay_on
> 1690 || *delay_on
< 6)
214 /* Thecus specific: only use PSC/PWM 0 */
215 psc
= (*delay_on
* 152-1)/1000;
216 err
= pca9532_calcpwm(client
, 0, psc
, led_cdev
->brightness
);
219 if (led
->state
== PCA9532_PWM0
)
220 pca9532_setpwm(led
->client
, 0);
226 static int pca9532_event(struct input_dev
*dev
, unsigned int type
,
227 unsigned int code
, int value
)
229 struct pca9532_data
*data
= input_get_drvdata(dev
);
231 if (!(type
== EV_SND
&& (code
== SND_BELL
|| code
== SND_TONE
)))
234 /* XXX: allow different kind of beeps with psc/pwm modifications */
235 if (value
> 1 && value
< 32767)
240 schedule_work(&data
->work
);
245 static void pca9532_input_work(struct work_struct
*work
)
247 struct pca9532_data
*data
=
248 container_of(work
, struct pca9532_data
, work
);
249 u8 maxleds
= data
->chip_info
->num_leds
;
251 mutex_lock(&data
->update_lock
);
252 i2c_smbus_write_byte_data(data
->client
, PCA9532_REG_PWM(maxleds
, 1),
254 mutex_unlock(&data
->update_lock
);
257 static enum pca9532_state
pca9532_getled(struct pca9532_led
*led
)
259 struct i2c_client
*client
= led
->client
;
260 struct pca9532_data
*data
= i2c_get_clientdata(client
);
261 u8 maxleds
= data
->chip_info
->num_leds
;
263 enum pca9532_state ret
;
265 mutex_lock(&data
->update_lock
);
266 reg
= i2c_smbus_read_byte_data(client
, LED_REG(maxleds
, led
->id
));
267 ret
= reg
>> LED_NUM(led
->id
)/2;
268 mutex_unlock(&data
->update_lock
);
272 #ifdef CONFIG_LEDS_PCA9532_GPIO
273 static int pca9532_gpio_request_pin(struct gpio_chip
*gc
, unsigned offset
)
275 struct pca9532_data
*data
= gpiochip_get_data(gc
);
276 struct pca9532_led
*led
= &data
->leds
[offset
];
278 if (led
->type
== PCA9532_TYPE_GPIO
)
284 static void pca9532_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int val
)
286 struct pca9532_data
*data
= gpiochip_get_data(gc
);
287 struct pca9532_led
*led
= &data
->leds
[offset
];
290 led
->state
= PCA9532_ON
;
292 led
->state
= PCA9532_OFF
;
297 static int pca9532_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
299 struct pca9532_data
*data
= gpiochip_get_data(gc
);
302 reg
= i2c_smbus_read_byte_data(data
->client
, PCA9532_REG_INPUT(offset
));
304 return !!(reg
& (1 << (offset
% 8)));
307 static int pca9532_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
309 /* To use as input ensure pin is not driven */
310 pca9532_gpio_set_value(gc
, offset
, 0);
315 static int pca9532_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
, int val
)
317 pca9532_gpio_set_value(gc
, offset
, val
);
321 #endif /* CONFIG_LEDS_PCA9532_GPIO */
323 static int pca9532_destroy_devices(struct pca9532_data
*data
, int n_devs
)
331 switch (data
->leds
[i
].type
) {
332 case PCA9532_TYPE_NONE
:
333 case PCA9532_TYPE_GPIO
:
335 case PCA9532_TYPE_LED
:
336 led_classdev_unregister(&data
->leds
[i
].ldev
);
338 case PCA9532_TYPE_N2100_BEEP
:
339 if (data
->idev
!= NULL
) {
340 cancel_work_sync(&data
->work
);
347 #ifdef CONFIG_LEDS_PCA9532_GPIO
348 if (data
->gpio
.parent
)
349 gpiochip_remove(&data
->gpio
);
355 static int pca9532_configure(struct i2c_client
*client
,
356 struct pca9532_data
*data
, struct pca9532_platform_data
*pdata
)
360 u8 maxleds
= data
->chip_info
->num_leds
;
362 for (i
= 0; i
< 2; i
++) {
363 data
->pwm
[i
] = pdata
->pwm
[i
];
364 data
->psc
[i
] = pdata
->psc
[i
];
365 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, i
),
367 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, i
),
371 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
372 struct pca9532_led
*led
= &data
->leds
[i
];
373 struct pca9532_led
*pled
= &pdata
->leds
[i
];
374 led
->client
= client
;
376 led
->type
= pled
->type
;
378 case PCA9532_TYPE_NONE
:
380 case PCA9532_TYPE_GPIO
:
383 case PCA9532_TYPE_LED
:
384 if (pled
->state
== PCA9532_KEEP
)
385 led
->state
= pca9532_getled(led
);
387 led
->state
= pled
->state
;
388 led
->name
= pled
->name
;
389 led
->ldev
.name
= led
->name
;
390 led
->ldev
.default_trigger
= pled
->default_trigger
;
391 led
->ldev
.brightness
= LED_OFF
;
392 led
->ldev
.brightness_set_blocking
=
393 pca9532_set_brightness
;
394 led
->ldev
.blink_set
= pca9532_set_blink
;
395 err
= led_classdev_register(&client
->dev
, &led
->ldev
);
397 dev_err(&client
->dev
,
398 "couldn't register LED %s\n",
404 case PCA9532_TYPE_N2100_BEEP
:
406 led
->state
= PCA9532_PWM1
;
408 data
->idev
= devm_input_allocate_device(&client
->dev
);
409 if (data
->idev
== NULL
) {
413 data
->idev
->name
= pled
->name
;
414 data
->idev
->phys
= "i2c/pca9532";
415 data
->idev
->id
.bustype
= BUS_HOST
;
416 data
->idev
->id
.vendor
= 0x001f;
417 data
->idev
->id
.product
= 0x0001;
418 data
->idev
->id
.version
= 0x0100;
419 data
->idev
->evbit
[0] = BIT_MASK(EV_SND
);
420 data
->idev
->sndbit
[0] = BIT_MASK(SND_BELL
) |
422 data
->idev
->event
= pca9532_event
;
423 input_set_drvdata(data
->idev
, data
);
424 INIT_WORK(&data
->work
, pca9532_input_work
);
425 err
= input_register_device(data
->idev
);
427 cancel_work_sync(&data
->work
);
435 #ifdef CONFIG_LEDS_PCA9532_GPIO
437 data
->gpio
.label
= "gpio-pca9532";
438 data
->gpio
.direction_input
= pca9532_gpio_direction_input
;
439 data
->gpio
.direction_output
= pca9532_gpio_direction_output
;
440 data
->gpio
.set
= pca9532_gpio_set_value
;
441 data
->gpio
.get
= pca9532_gpio_get_value
;
442 data
->gpio
.request
= pca9532_gpio_request_pin
;
443 data
->gpio
.can_sleep
= 1;
444 data
->gpio
.base
= pdata
->gpio_base
;
445 data
->gpio
.ngpio
= data
->chip_info
->num_leds
;
446 data
->gpio
.parent
= &client
->dev
;
447 data
->gpio
.owner
= THIS_MODULE
;
449 err
= gpiochip_add_data(&data
->gpio
, data
);
451 /* Use data->gpio.dev as a flag for freeing gpiochip */
452 data
->gpio
.parent
= NULL
;
453 dev_warn(&client
->dev
, "could not add gpiochip\n");
455 dev_info(&client
->dev
, "gpios %i...%i\n",
456 data
->gpio
.base
, data
->gpio
.base
+
457 data
->gpio
.ngpio
- 1);
465 pca9532_destroy_devices(data
, i
);
469 static struct pca9532_platform_data
*
470 pca9532_of_populate_pdata(struct device
*dev
, struct device_node
*np
)
472 struct pca9532_platform_data
*pdata
;
473 struct device_node
*child
;
474 const struct of_device_id
*match
;
479 match
= of_match_device(of_pca9532_leds_match
, dev
);
481 return ERR_PTR(-ENODEV
);
483 devid
= (int)(uintptr_t)match
->data
;
484 maxleds
= pca9532_chip_info_tbl
[devid
].num_leds
;
486 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
488 return ERR_PTR(-ENOMEM
);
490 for_each_child_of_node(np
, child
) {
491 if (of_property_read_string(child
, "label",
492 &pdata
->leds
[i
].name
))
493 pdata
->leds
[i
].name
= child
->name
;
494 of_property_read_u32(child
, "type", &pdata
->leds
[i
].type
);
495 of_property_read_string(child
, "linux,default-trigger",
496 &pdata
->leds
[i
].default_trigger
);
497 if (!of_property_read_string(child
, "default-state", &state
)) {
498 if (!strcmp(state
, "on"))
499 pdata
->leds
[i
].state
= PCA9532_ON
;
500 else if (!strcmp(state
, "keep"))
501 pdata
->leds
[i
].state
= PCA9532_KEEP
;
503 if (++i
>= maxleds
) {
512 static int pca9532_probe(struct i2c_client
*client
,
513 const struct i2c_device_id
*id
)
516 struct pca9532_data
*data
= i2c_get_clientdata(client
);
517 struct pca9532_platform_data
*pca9532_pdata
=
518 dev_get_platdata(&client
->dev
);
519 struct device_node
*np
= client
->dev
.of_node
;
521 if (!pca9532_pdata
) {
524 pca9532_of_populate_pdata(&client
->dev
, np
);
525 if (IS_ERR(pca9532_pdata
))
526 return PTR_ERR(pca9532_pdata
);
528 dev_err(&client
->dev
, "no platform data\n");
531 devid
= (int)(uintptr_t)of_match_device(
532 of_pca9532_leds_match
, &client
->dev
)->data
;
534 devid
= id
->driver_data
;
537 if (!i2c_check_functionality(client
->adapter
,
538 I2C_FUNC_SMBUS_BYTE_DATA
))
541 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
545 data
->chip_info
= &pca9532_chip_info_tbl
[devid
];
547 dev_info(&client
->dev
, "setting platform data\n");
548 i2c_set_clientdata(client
, data
);
549 data
->client
= client
;
550 mutex_init(&data
->update_lock
);
552 return pca9532_configure(client
, data
, pca9532_pdata
);
555 static int pca9532_remove(struct i2c_client
*client
)
557 struct pca9532_data
*data
= i2c_get_clientdata(client
);
560 err
= pca9532_destroy_devices(data
, data
->chip_info
->num_leds
);
567 module_i2c_driver(pca9532_driver
);
569 MODULE_AUTHOR("Riku Voipio");
570 MODULE_LICENSE("GPL");
571 MODULE_DESCRIPTION("PCA 9532 LED dimmer");