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>
26 #define PCA9532_REG_INPUT(i) ((i) >> 3)
27 #define PCA9532_REG_OFFSET(m) ((m) >> 4)
28 #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
29 #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
30 #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
31 #define LED_NUM(led) (led & 0x3)
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
[] = {
89 static struct i2c_driver pca9532_driver
= {
91 .name
= "leds-pca953x",
93 .probe
= pca9532_probe
,
94 .remove
= pca9532_remove
,
95 .id_table
= pca9532_id
,
98 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
99 * the clever Thecus people are using one pwm to drive the beeper. So,
100 * as a compromise we average one pwm to the values requested by all
101 * leds that are not ON/OFF.
103 static int pca9532_calcpwm(struct i2c_client
*client
, int pwm
, int blink
,
104 enum led_brightness value
)
106 int a
= 0, b
= 0, i
= 0;
107 struct pca9532_data
*data
= i2c_get_clientdata(client
);
108 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
109 if (data
->leds
[i
].type
== PCA9532_TYPE_LED
&&
110 data
->leds
[i
].state
== PCA9532_PWM0
+pwm
) {
112 b
+= data
->leds
[i
].ldev
.brightness
;
116 dev_err(&client
->dev
,
117 "fear of division by zero %d/%d, wanted %d\n",
125 data
->psc
[pwm
] = blink
;
129 static int pca9532_setpwm(struct i2c_client
*client
, int pwm
)
131 struct pca9532_data
*data
= i2c_get_clientdata(client
);
132 u8 maxleds
= data
->chip_info
->num_leds
;
134 mutex_lock(&data
->update_lock
);
135 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, pwm
),
137 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, pwm
),
139 mutex_unlock(&data
->update_lock
);
143 /* Set LED routing */
144 static void pca9532_setled(struct pca9532_led
*led
)
146 struct i2c_client
*client
= led
->client
;
147 struct pca9532_data
*data
= i2c_get_clientdata(client
);
148 u8 maxleds
= data
->chip_info
->num_leds
;
151 mutex_lock(&data
->update_lock
);
152 reg
= i2c_smbus_read_byte_data(client
, LED_REG(maxleds
, led
->id
));
154 reg
= reg
& ~(0x3<<LED_NUM(led
->id
)*2);
155 /* set the new value */
156 reg
= reg
| (led
->state
<< LED_NUM(led
->id
)*2);
157 i2c_smbus_write_byte_data(client
, LED_REG(maxleds
, led
->id
), reg
);
158 mutex_unlock(&data
->update_lock
);
161 static void pca9532_set_brightness(struct led_classdev
*led_cdev
,
162 enum led_brightness value
)
165 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
167 if (value
== LED_OFF
)
168 led
->state
= PCA9532_OFF
;
169 else if (value
== LED_FULL
)
170 led
->state
= PCA9532_ON
;
172 led
->state
= PCA9532_PWM0
; /* Thecus: hardcode one pwm */
173 err
= pca9532_calcpwm(led
->client
, 0, 0, value
);
175 return; /* XXX: led api doesn't allow error code? */
177 schedule_work(&led
->work
);
180 static int pca9532_set_blink(struct led_classdev
*led_cdev
,
181 unsigned long *delay_on
, unsigned long *delay_off
)
183 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
184 struct i2c_client
*client
= led
->client
;
188 if (*delay_on
== 0 && *delay_off
== 0) {
189 /* led subsystem ask us for a blink rate */
193 if (*delay_on
!= *delay_off
|| *delay_on
> 1690 || *delay_on
< 6)
196 /* Thecus specific: only use PSC/PWM 0 */
197 psc
= (*delay_on
* 152-1)/1000;
198 err
= pca9532_calcpwm(client
, 0, psc
, led_cdev
->brightness
);
201 schedule_work(&led
->work
);
205 static int pca9532_event(struct input_dev
*dev
, unsigned int type
,
206 unsigned int code
, int value
)
208 struct pca9532_data
*data
= input_get_drvdata(dev
);
210 if (!(type
== EV_SND
&& (code
== SND_BELL
|| code
== SND_TONE
)))
213 /* XXX: allow different kind of beeps with psc/pwm modifications */
214 if (value
> 1 && value
< 32767)
219 schedule_work(&data
->work
);
224 static void pca9532_input_work(struct work_struct
*work
)
226 struct pca9532_data
*data
=
227 container_of(work
, struct pca9532_data
, work
);
228 u8 maxleds
= data
->chip_info
->num_leds
;
230 mutex_lock(&data
->update_lock
);
231 i2c_smbus_write_byte_data(data
->client
, PCA9532_REG_PWM(maxleds
, 1),
233 mutex_unlock(&data
->update_lock
);
236 static void pca9532_led_work(struct work_struct
*work
)
238 struct pca9532_led
*led
;
239 led
= container_of(work
, struct pca9532_led
, work
);
240 if (led
->state
== PCA9532_PWM0
)
241 pca9532_setpwm(led
->client
, 0);
245 #ifdef CONFIG_LEDS_PCA9532_GPIO
246 static int pca9532_gpio_request_pin(struct gpio_chip
*gc
, unsigned offset
)
248 struct pca9532_data
*data
= container_of(gc
, struct pca9532_data
, gpio
);
249 struct pca9532_led
*led
= &data
->leds
[offset
];
251 if (led
->type
== PCA9532_TYPE_GPIO
)
257 static void pca9532_gpio_set_value(struct gpio_chip
*gc
, unsigned offset
, int val
)
259 struct pca9532_data
*data
= container_of(gc
, struct pca9532_data
, gpio
);
260 struct pca9532_led
*led
= &data
->leds
[offset
];
263 led
->state
= PCA9532_ON
;
265 led
->state
= PCA9532_OFF
;
270 static int pca9532_gpio_get_value(struct gpio_chip
*gc
, unsigned offset
)
272 struct pca9532_data
*data
= container_of(gc
, struct pca9532_data
, gpio
);
275 reg
= i2c_smbus_read_byte_data(data
->client
, PCA9532_REG_INPUT(offset
));
277 return !!(reg
& (1 << (offset
% 8)));
280 static int pca9532_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
282 /* To use as input ensure pin is not driven */
283 pca9532_gpio_set_value(gc
, offset
, 0);
288 static int pca9532_gpio_direction_output(struct gpio_chip
*gc
, unsigned offset
, int val
)
290 pca9532_gpio_set_value(gc
, offset
, val
);
294 #endif /* CONFIG_LEDS_PCA9532_GPIO */
296 static int pca9532_destroy_devices(struct pca9532_data
*data
, int n_devs
)
304 switch (data
->leds
[i
].type
) {
305 case PCA9532_TYPE_NONE
:
306 case PCA9532_TYPE_GPIO
:
308 case PCA9532_TYPE_LED
:
309 led_classdev_unregister(&data
->leds
[i
].ldev
);
310 cancel_work_sync(&data
->leds
[i
].work
);
312 case PCA9532_TYPE_N2100_BEEP
:
313 if (data
->idev
!= NULL
) {
314 cancel_work_sync(&data
->work
);
321 #ifdef CONFIG_LEDS_PCA9532_GPIO
322 if (data
->gpio
.dev
) {
323 int err
= gpiochip_remove(&data
->gpio
);
325 dev_err(&data
->client
->dev
, "%s failed, %d\n",
326 "gpiochip_remove()", err
);
335 static int pca9532_configure(struct i2c_client
*client
,
336 struct pca9532_data
*data
, struct pca9532_platform_data
*pdata
)
340 u8 maxleds
= data
->chip_info
->num_leds
;
342 for (i
= 0; i
< 2; i
++) {
343 data
->pwm
[i
] = pdata
->pwm
[i
];
344 data
->psc
[i
] = pdata
->psc
[i
];
345 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(maxleds
, i
),
347 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(maxleds
, i
),
351 for (i
= 0; i
< data
->chip_info
->num_leds
; i
++) {
352 struct pca9532_led
*led
= &data
->leds
[i
];
353 struct pca9532_led
*pled
= &pdata
->leds
[i
];
354 led
->client
= client
;
356 led
->type
= pled
->type
;
358 case PCA9532_TYPE_NONE
:
360 case PCA9532_TYPE_GPIO
:
363 case PCA9532_TYPE_LED
:
364 led
->state
= pled
->state
;
365 led
->name
= pled
->name
;
366 led
->ldev
.name
= led
->name
;
367 led
->ldev
.brightness
= LED_OFF
;
368 led
->ldev
.brightness_set
= pca9532_set_brightness
;
369 led
->ldev
.blink_set
= pca9532_set_blink
;
370 INIT_WORK(&led
->work
, pca9532_led_work
);
371 err
= led_classdev_register(&client
->dev
, &led
->ldev
);
373 dev_err(&client
->dev
,
374 "couldn't register LED %s\n",
380 case PCA9532_TYPE_N2100_BEEP
:
382 led
->state
= PCA9532_PWM1
;
384 data
->idev
= devm_input_allocate_device(&client
->dev
);
385 if (data
->idev
== NULL
) {
389 data
->idev
->name
= pled
->name
;
390 data
->idev
->phys
= "i2c/pca9532";
391 data
->idev
->id
.bustype
= BUS_HOST
;
392 data
->idev
->id
.vendor
= 0x001f;
393 data
->idev
->id
.product
= 0x0001;
394 data
->idev
->id
.version
= 0x0100;
395 data
->idev
->evbit
[0] = BIT_MASK(EV_SND
);
396 data
->idev
->sndbit
[0] = BIT_MASK(SND_BELL
) |
398 data
->idev
->event
= pca9532_event
;
399 input_set_drvdata(data
->idev
, data
);
400 INIT_WORK(&data
->work
, pca9532_input_work
);
401 err
= input_register_device(data
->idev
);
403 cancel_work_sync(&data
->work
);
411 #ifdef CONFIG_LEDS_PCA9532_GPIO
413 data
->gpio
.label
= "gpio-pca9532";
414 data
->gpio
.direction_input
= pca9532_gpio_direction_input
;
415 data
->gpio
.direction_output
= pca9532_gpio_direction_output
;
416 data
->gpio
.set
= pca9532_gpio_set_value
;
417 data
->gpio
.get
= pca9532_gpio_get_value
;
418 data
->gpio
.request
= pca9532_gpio_request_pin
;
419 data
->gpio
.can_sleep
= 1;
420 data
->gpio
.base
= pdata
->gpio_base
;
421 data
->gpio
.ngpio
= data
->chip_info
->num_leds
;
422 data
->gpio
.dev
= &client
->dev
;
423 data
->gpio
.owner
= THIS_MODULE
;
425 err
= gpiochip_add(&data
->gpio
);
427 /* Use data->gpio.dev as a flag for freeing gpiochip */
428 data
->gpio
.dev
= NULL
;
429 dev_warn(&client
->dev
, "could not add gpiochip\n");
431 dev_info(&client
->dev
, "gpios %i...%i\n",
432 data
->gpio
.base
, data
->gpio
.base
+
433 data
->gpio
.ngpio
- 1);
441 pca9532_destroy_devices(data
, i
);
445 static int pca9532_probe(struct i2c_client
*client
,
446 const struct i2c_device_id
*id
)
448 struct pca9532_data
*data
= i2c_get_clientdata(client
);
449 struct pca9532_platform_data
*pca9532_pdata
=
450 dev_get_platdata(&client
->dev
);
455 if (!i2c_check_functionality(client
->adapter
,
456 I2C_FUNC_SMBUS_BYTE_DATA
))
459 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
463 data
->chip_info
= &pca9532_chip_info_tbl
[id
->driver_data
];
465 dev_info(&client
->dev
, "setting platform data\n");
466 i2c_set_clientdata(client
, data
);
467 data
->client
= client
;
468 mutex_init(&data
->update_lock
);
470 return pca9532_configure(client
, data
, pca9532_pdata
);
473 static int pca9532_remove(struct i2c_client
*client
)
475 struct pca9532_data
*data
= i2c_get_clientdata(client
);
478 err
= pca9532_destroy_devices(data
, data
->chip_info
->num_leds
);
485 module_i2c_driver(pca9532_driver
);
487 MODULE_AUTHOR("Riku Voipio");
488 MODULE_LICENSE("GPL");
489 MODULE_DESCRIPTION("PCA 9532 LED dimmer");