2 * pca9532.c - 16-bit Led dimmer
4 * Copyright (C) 2008 Riku Voipio
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * Datasheet: http://www.nxp.com/acrobat/datasheets/PCA9532_3.pdf
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
17 #include <linux/leds.h>
18 #include <linux/input.h>
19 #include <linux/mutex.h>
20 #include <linux/workqueue.h>
21 #include <linux/leds-pca9532.h>
23 #define PCA9532_REG_PSC(i) (0x2+(i)*2)
24 #define PCA9532_REG_PWM(i) (0x3+(i)*2)
25 #define PCA9532_REG_LS0 0x6
26 #define LED_REG(led) ((led>>2)+PCA9532_REG_LS0)
27 #define LED_NUM(led) (led & 0x3)
29 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
32 struct i2c_client
*client
;
33 struct pca9532_led leds
[16];
34 struct mutex update_lock
;
35 struct input_dev
*idev
;
36 struct work_struct work
;
41 static int pca9532_probe(struct i2c_client
*client
,
42 const struct i2c_device_id
*id
);
43 static int pca9532_remove(struct i2c_client
*client
);
45 static const struct i2c_device_id pca9532_id
[] = {
50 MODULE_DEVICE_TABLE(i2c
, pca9532_id
);
52 static struct i2c_driver pca9532_driver
= {
56 .probe
= pca9532_probe
,
57 .remove
= pca9532_remove
,
58 .id_table
= pca9532_id
,
61 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionaly,
62 * the clever Thecus people are using one pwm to drive the beeper. So,
63 * as a compromise we average one pwm to the values requested by all
64 * leds that are not ON/OFF.
66 static int pca9532_calcpwm(struct i2c_client
*client
, int pwm
, int blink
,
67 enum led_brightness value
)
69 int a
= 0, b
= 0, i
= 0;
70 struct pca9532_data
*data
= i2c_get_clientdata(client
);
71 for (i
= 0; i
< 16; i
++) {
72 if (data
->leds
[i
].type
== PCA9532_TYPE_LED
&&
73 data
->leds
[i
].state
== PCA9532_PWM0
+pwm
) {
75 b
+= data
->leds
[i
].ldev
.brightness
;
80 "fear of division by zero %d/%d, wanted %d\n",
88 data
->psc
[pwm
] = blink
;
92 static int pca9532_setpwm(struct i2c_client
*client
, int pwm
)
94 struct pca9532_data
*data
= i2c_get_clientdata(client
);
95 mutex_lock(&data
->update_lock
);
96 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(pwm
),
98 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(pwm
),
100 mutex_unlock(&data
->update_lock
);
104 /* Set LED routing */
105 static void pca9532_setled(struct pca9532_led
*led
)
107 struct i2c_client
*client
= led
->client
;
108 struct pca9532_data
*data
= i2c_get_clientdata(client
);
111 mutex_lock(&data
->update_lock
);
112 reg
= i2c_smbus_read_byte_data(client
, LED_REG(led
->id
));
114 reg
= reg
& ~(0x3<<LED_NUM(led
->id
)*2);
115 /* set the new value */
116 reg
= reg
| (led
->state
<< LED_NUM(led
->id
)*2);
117 i2c_smbus_write_byte_data(client
, LED_REG(led
->id
), reg
);
118 mutex_unlock(&data
->update_lock
);
121 static void pca9532_set_brightness(struct led_classdev
*led_cdev
,
122 enum led_brightness value
)
125 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
127 if (value
== LED_OFF
)
128 led
->state
= PCA9532_OFF
;
129 else if (value
== LED_FULL
)
130 led
->state
= PCA9532_ON
;
132 led
->state
= PCA9532_PWM0
; /* Thecus: hardcode one pwm */
133 err
= pca9532_calcpwm(led
->client
, 0, 0, value
);
135 return; /* XXX: led api doesn't allow error code? */
137 schedule_work(&led
->work
);
140 static int pca9532_set_blink(struct led_classdev
*led_cdev
,
141 unsigned long *delay_on
, unsigned long *delay_off
)
143 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
144 struct i2c_client
*client
= led
->client
;
148 if (*delay_on
== 0 && *delay_off
== 0) {
149 /* led subsystem ask us for a blink rate */
153 if (*delay_on
!= *delay_off
|| *delay_on
> 1690 || *delay_on
< 6)
156 /* Thecus specific: only use PSC/PWM 0 */
157 psc
= (*delay_on
* 152-1)/1000;
158 err
= pca9532_calcpwm(client
, 0, psc
, led_cdev
->brightness
);
161 schedule_work(&led
->work
);
165 static int pca9532_event(struct input_dev
*dev
, unsigned int type
,
166 unsigned int code
, int value
)
168 struct pca9532_data
*data
= input_get_drvdata(dev
);
170 if (!(type
== EV_SND
&& (code
== SND_BELL
|| code
== SND_TONE
)))
173 /* XXX: allow different kind of beeps with psc/pwm modifications */
174 if (value
> 1 && value
< 32767)
179 schedule_work(&data
->work
);
184 static void pca9532_input_work(struct work_struct
*work
)
186 struct pca9532_data
*data
;
187 data
= container_of(work
, struct pca9532_data
, work
);
188 mutex_lock(&data
->update_lock
);
189 i2c_smbus_write_byte_data(data
->client
, PCA9532_REG_PWM(1),
191 mutex_unlock(&data
->update_lock
);
194 static void pca9532_led_work(struct work_struct
*work
)
196 struct pca9532_led
*led
;
197 led
= container_of(work
, struct pca9532_led
, work
);
198 if (led
->state
== PCA9532_PWM0
)
199 pca9532_setpwm(led
->client
, 0);
203 static int pca9532_configure(struct i2c_client
*client
,
204 struct pca9532_data
*data
, struct pca9532_platform_data
*pdata
)
208 for (i
= 0; i
< 2; i
++) {
209 data
->pwm
[i
] = pdata
->pwm
[i
];
210 data
->psc
[i
] = pdata
->psc
[i
];
211 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(i
),
213 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(i
),
217 for (i
= 0; i
< 16; i
++) {
218 struct pca9532_led
*led
= &data
->leds
[i
];
219 struct pca9532_led
*pled
= &pdata
->leds
[i
];
220 led
->client
= client
;
222 led
->type
= pled
->type
;
224 case PCA9532_TYPE_NONE
:
226 case PCA9532_TYPE_LED
:
227 led
->state
= pled
->state
;
228 led
->name
= pled
->name
;
229 led
->ldev
.name
= led
->name
;
230 led
->ldev
.brightness
= LED_OFF
;
231 led
->ldev
.brightness_set
= pca9532_set_brightness
;
232 led
->ldev
.blink_set
= pca9532_set_blink
;
233 INIT_WORK(&led
->work
, pca9532_led_work
);
234 err
= led_classdev_register(&client
->dev
, &led
->ldev
);
236 dev_err(&client
->dev
,
237 "couldn't register LED %s\n",
243 case PCA9532_TYPE_N2100_BEEP
:
245 led
->state
= PCA9532_PWM1
;
247 data
->idev
= input_allocate_device();
248 if (data
->idev
== NULL
) {
252 data
->idev
->name
= pled
->name
;
253 data
->idev
->phys
= "i2c/pca9532";
254 data
->idev
->id
.bustype
= BUS_HOST
;
255 data
->idev
->id
.vendor
= 0x001f;
256 data
->idev
->id
.product
= 0x0001;
257 data
->idev
->id
.version
= 0x0100;
258 data
->idev
->evbit
[0] = BIT_MASK(EV_SND
);
259 data
->idev
->sndbit
[0] = BIT_MASK(SND_BELL
) |
261 data
->idev
->event
= pca9532_event
;
262 input_set_drvdata(data
->idev
, data
);
263 INIT_WORK(&data
->work
, pca9532_input_work
);
264 err
= input_register_device(data
->idev
);
266 input_free_device(data
->idev
);
267 cancel_work_sync(&data
->work
);
278 for (i
= i
- 1; i
>= 0; i
--)
279 switch (data
->leds
[i
].type
) {
280 case PCA9532_TYPE_NONE
:
282 case PCA9532_TYPE_LED
:
283 led_classdev_unregister(&data
->leds
[i
].ldev
);
284 cancel_work_sync(&data
->leds
[i
].work
);
286 case PCA9532_TYPE_N2100_BEEP
:
287 if (data
->idev
!= NULL
) {
288 input_unregister_device(data
->idev
);
289 input_free_device(data
->idev
);
290 cancel_work_sync(&data
->work
);
299 static int pca9532_probe(struct i2c_client
*client
,
300 const struct i2c_device_id
*id
)
302 struct pca9532_data
*data
= i2c_get_clientdata(client
);
303 struct pca9532_platform_data
*pca9532_pdata
= client
->dev
.platform_data
;
309 if (!i2c_check_functionality(client
->adapter
,
310 I2C_FUNC_SMBUS_BYTE_DATA
))
313 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
317 dev_info(&client
->dev
, "setting platform data\n");
318 i2c_set_clientdata(client
, data
);
319 data
->client
= client
;
320 mutex_init(&data
->update_lock
);
322 err
= pca9532_configure(client
, data
, pca9532_pdata
);
329 static int pca9532_remove(struct i2c_client
*client
)
331 struct pca9532_data
*data
= i2c_get_clientdata(client
);
333 for (i
= 0; i
< 16; i
++)
334 switch (data
->leds
[i
].type
) {
335 case PCA9532_TYPE_NONE
:
337 case PCA9532_TYPE_LED
:
338 led_classdev_unregister(&data
->leds
[i
].ldev
);
339 cancel_work_sync(&data
->leds
[i
].work
);
341 case PCA9532_TYPE_N2100_BEEP
:
342 if (data
->idev
!= NULL
) {
343 input_unregister_device(data
->idev
);
344 input_free_device(data
->idev
);
345 cancel_work_sync(&data
->work
);
355 static int __init
pca9532_init(void)
357 return i2c_add_driver(&pca9532_driver
);
360 static void __exit
pca9532_exit(void)
362 i2c_del_driver(&pca9532_driver
);
365 MODULE_AUTHOR("Riku Voipio");
366 MODULE_LICENSE("GPL");
367 MODULE_DESCRIPTION("PCA 9532 LED dimmer");
369 module_init(pca9532_init
);
370 module_exit(pca9532_exit
);