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/leds.h>
17 #include <linux/input.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/leds-pca9532.h>
22 #define PCA9532_REG_PSC(i) (0x2+(i)*2)
23 #define PCA9532_REG_PWM(i) (0x3+(i)*2)
24 #define PCA9532_REG_LS0 0x6
25 #define LED_REG(led) ((led>>2)+PCA9532_REG_LS0)
26 #define LED_NUM(led) (led & 0x3)
28 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
31 struct i2c_client
*client
;
32 struct pca9532_led leds
[16];
33 struct mutex update_lock
;
34 struct input_dev
*idev
;
35 struct work_struct work
;
40 static int pca9532_probe(struct i2c_client
*client
,
41 const struct i2c_device_id
*id
);
42 static int pca9532_remove(struct i2c_client
*client
);
44 static const struct i2c_device_id pca9532_id
[] = {
49 MODULE_DEVICE_TABLE(i2c
, pca9532_id
);
51 static struct i2c_driver pca9532_driver
= {
55 .probe
= pca9532_probe
,
56 .remove
= pca9532_remove
,
57 .id_table
= pca9532_id
,
60 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionaly,
61 * the clever Thecus people are using one pwm to drive the beeper. So,
62 * as a compromise we average one pwm to the values requested by all
63 * leds that are not ON/OFF.
65 static int pca9532_calcpwm(struct i2c_client
*client
, int pwm
, int blink
,
66 enum led_brightness value
)
68 int a
= 0, b
= 0, i
= 0;
69 struct pca9532_data
*data
= i2c_get_clientdata(client
);
70 for (i
= 0; i
< 16; i
++) {
71 if (data
->leds
[i
].type
== PCA9532_TYPE_LED
&&
72 data
->leds
[i
].state
== PCA9532_PWM0
+pwm
) {
74 b
+= data
->leds
[i
].ldev
.brightness
;
79 "fear of division by zero %d/%d, wanted %d\n",
87 data
->psc
[pwm
] = blink
;
91 static int pca9532_setpwm(struct i2c_client
*client
, int pwm
)
93 struct pca9532_data
*data
= i2c_get_clientdata(client
);
94 mutex_lock(&data
->update_lock
);
95 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(pwm
),
97 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(pwm
),
99 mutex_unlock(&data
->update_lock
);
103 /* Set LED routing */
104 static void pca9532_setled(struct pca9532_led
*led
)
106 struct i2c_client
*client
= led
->client
;
107 struct pca9532_data
*data
= i2c_get_clientdata(client
);
110 mutex_lock(&data
->update_lock
);
111 reg
= i2c_smbus_read_byte_data(client
, LED_REG(led
->id
));
113 reg
= reg
& ~(0x3<<LED_NUM(led
->id
)*2);
114 /* set the new value */
115 reg
= reg
| (led
->state
<< LED_NUM(led
->id
)*2);
116 i2c_smbus_write_byte_data(client
, LED_REG(led
->id
), reg
);
117 mutex_unlock(&data
->update_lock
);
120 static void pca9532_set_brightness(struct led_classdev
*led_cdev
,
121 enum led_brightness value
)
124 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
126 if (value
== LED_OFF
)
127 led
->state
= PCA9532_OFF
;
128 else if (value
== LED_FULL
)
129 led
->state
= PCA9532_ON
;
131 led
->state
= PCA9532_PWM0
; /* Thecus: hardcode one pwm */
132 err
= pca9532_calcpwm(led
->client
, 0, 0, value
);
134 return; /* XXX: led api doesn't allow error code? */
136 schedule_work(&led
->work
);
139 static int pca9532_set_blink(struct led_classdev
*led_cdev
,
140 unsigned long *delay_on
, unsigned long *delay_off
)
142 struct pca9532_led
*led
= ldev_to_led(led_cdev
);
143 struct i2c_client
*client
= led
->client
;
147 if (*delay_on
== 0 && *delay_off
== 0) {
148 /* led subsystem ask us for a blink rate */
152 if (*delay_on
!= *delay_off
|| *delay_on
> 1690 || *delay_on
< 6)
155 /* Thecus specific: only use PSC/PWM 0 */
156 psc
= (*delay_on
* 152-1)/1000;
157 err
= pca9532_calcpwm(client
, 0, psc
, led_cdev
->brightness
);
160 schedule_work(&led
->work
);
164 static int pca9532_event(struct input_dev
*dev
, unsigned int type
,
165 unsigned int code
, int value
)
167 struct pca9532_data
*data
= input_get_drvdata(dev
);
169 if (!(type
== EV_SND
&& (code
== SND_BELL
|| code
== SND_TONE
)))
172 /* XXX: allow different kind of beeps with psc/pwm modifications */
173 if (value
> 1 && value
< 32767)
178 schedule_work(&data
->work
);
183 static void pca9532_input_work(struct work_struct
*work
)
185 struct pca9532_data
*data
;
186 data
= container_of(work
, struct pca9532_data
, work
);
187 mutex_lock(&data
->update_lock
);
188 i2c_smbus_write_byte_data(data
->client
, PCA9532_REG_PWM(1),
190 mutex_unlock(&data
->update_lock
);
193 static void pca9532_led_work(struct work_struct
*work
)
195 struct pca9532_led
*led
;
196 led
= container_of(work
, struct pca9532_led
, work
);
197 if (led
->state
== PCA9532_PWM0
)
198 pca9532_setpwm(led
->client
, 0);
202 static int pca9532_configure(struct i2c_client
*client
,
203 struct pca9532_data
*data
, struct pca9532_platform_data
*pdata
)
207 for (i
= 0; i
< 2; i
++) {
208 data
->pwm
[i
] = pdata
->pwm
[i
];
209 data
->psc
[i
] = pdata
->psc
[i
];
210 i2c_smbus_write_byte_data(client
, PCA9532_REG_PWM(i
),
212 i2c_smbus_write_byte_data(client
, PCA9532_REG_PSC(i
),
216 for (i
= 0; i
< 16; i
++) {
217 struct pca9532_led
*led
= &data
->leds
[i
];
218 struct pca9532_led
*pled
= &pdata
->leds
[i
];
219 led
->client
= client
;
221 led
->type
= pled
->type
;
223 case PCA9532_TYPE_NONE
:
225 case PCA9532_TYPE_LED
:
226 led
->state
= pled
->state
;
227 led
->name
= pled
->name
;
228 led
->ldev
.name
= led
->name
;
229 led
->ldev
.brightness
= LED_OFF
;
230 led
->ldev
.brightness_set
= pca9532_set_brightness
;
231 led
->ldev
.blink_set
= pca9532_set_blink
;
232 INIT_WORK(&led
->work
, pca9532_led_work
);
233 err
= led_classdev_register(&client
->dev
, &led
->ldev
);
235 dev_err(&client
->dev
,
236 "couldn't register LED %s\n",
242 case PCA9532_TYPE_N2100_BEEP
:
244 led
->state
= PCA9532_PWM1
;
246 data
->idev
= input_allocate_device();
247 if (data
->idev
== NULL
) {
251 data
->idev
->name
= pled
->name
;
252 data
->idev
->phys
= "i2c/pca9532";
253 data
->idev
->id
.bustype
= BUS_HOST
;
254 data
->idev
->id
.vendor
= 0x001f;
255 data
->idev
->id
.product
= 0x0001;
256 data
->idev
->id
.version
= 0x0100;
257 data
->idev
->evbit
[0] = BIT_MASK(EV_SND
);
258 data
->idev
->sndbit
[0] = BIT_MASK(SND_BELL
) |
260 data
->idev
->event
= pca9532_event
;
261 input_set_drvdata(data
->idev
, data
);
262 INIT_WORK(&data
->work
, pca9532_input_work
);
263 err
= input_register_device(data
->idev
);
265 input_free_device(data
->idev
);
266 cancel_work_sync(&data
->work
);
277 for (i
= i
- 1; i
>= 0; i
--)
278 switch (data
->leds
[i
].type
) {
279 case PCA9532_TYPE_NONE
:
281 case PCA9532_TYPE_LED
:
282 led_classdev_unregister(&data
->leds
[i
].ldev
);
283 cancel_work_sync(&data
->leds
[i
].work
);
285 case PCA9532_TYPE_N2100_BEEP
:
286 if (data
->idev
!= NULL
) {
287 input_unregister_device(data
->idev
);
288 input_free_device(data
->idev
);
289 cancel_work_sync(&data
->work
);
298 static int pca9532_probe(struct i2c_client
*client
,
299 const struct i2c_device_id
*id
)
301 struct pca9532_data
*data
= i2c_get_clientdata(client
);
302 struct pca9532_platform_data
*pca9532_pdata
= client
->dev
.platform_data
;
308 if (!i2c_check_functionality(client
->adapter
,
309 I2C_FUNC_SMBUS_BYTE_DATA
))
312 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
316 dev_info(&client
->dev
, "setting platform data\n");
317 i2c_set_clientdata(client
, data
);
318 data
->client
= client
;
319 mutex_init(&data
->update_lock
);
321 err
= pca9532_configure(client
, data
, pca9532_pdata
);
324 i2c_set_clientdata(client
, NULL
);
330 static int pca9532_remove(struct i2c_client
*client
)
332 struct pca9532_data
*data
= i2c_get_clientdata(client
);
334 for (i
= 0; i
< 16; i
++)
335 switch (data
->leds
[i
].type
) {
336 case PCA9532_TYPE_NONE
:
338 case PCA9532_TYPE_LED
:
339 led_classdev_unregister(&data
->leds
[i
].ldev
);
340 cancel_work_sync(&data
->leds
[i
].work
);
342 case PCA9532_TYPE_N2100_BEEP
:
343 if (data
->idev
!= NULL
) {
344 input_unregister_device(data
->idev
);
345 input_free_device(data
->idev
);
346 cancel_work_sync(&data
->work
);
353 i2c_set_clientdata(client
, NULL
);
357 static int __init
pca9532_init(void)
359 return i2c_add_driver(&pca9532_driver
);
362 static void __exit
pca9532_exit(void)
364 i2c_del_driver(&pca9532_driver
);
367 MODULE_AUTHOR("Riku Voipio");
368 MODULE_LICENSE("GPL");
369 MODULE_DESCRIPTION("PCA 9532 LED dimmer");
371 module_init(pca9532_init
);
372 module_exit(pca9532_exit
);