blob: 6682175fa9f7c4e3c5a91d99120c83b2003f6b9a [file] [log] [blame]
Riku Voipioe14fa822008-05-31 14:43:41 +01001/*
2 * pca9532.c - 16-bit Led dimmer
3 *
Riku Voipiob26e0ed2009-03-03 21:37:17 +02004 * Copyright (C) 2008 Riku Voipio
Riku Voipioe14fa822008-05-31 14:43:41 +01005 *
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.
9 *
10 * Datasheet: http://www.nxp.com/acrobat/datasheets/PCA9532_3.pdf
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010017#include <linux/leds.h>
18#include <linux/input.h>
19#include <linux/mutex.h>
Riku Voipio934cd3f2008-12-03 08:21:36 +000020#include <linux/workqueue.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010021#include <linux/leds-pca9532.h>
22
Riku Voipioe14fa822008-05-31 14:43:41 +010023#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)
28
29#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
30
31struct pca9532_data {
32 struct i2c_client *client;
33 struct pca9532_led leds[16];
34 struct mutex update_lock;
Richard Purdie85c52042009-09-07 14:35:04 +010035 struct input_dev *idev;
Antonio Ospite07172d22009-06-19 13:53:07 +020036 struct work_struct work;
Riku Voipioe14fa822008-05-31 14:43:41 +010037 u8 pwm[2];
38 u8 psc[2];
39};
40
41static int pca9532_probe(struct i2c_client *client,
42 const struct i2c_device_id *id);
43static int pca9532_remove(struct i2c_client *client);
44
45static const struct i2c_device_id pca9532_id[] = {
46 { "pca9532", 0 },
47 { }
48};
49
50MODULE_DEVICE_TABLE(i2c, pca9532_id);
51
52static struct i2c_driver pca9532_driver = {
53 .driver = {
Richard Purdie85c52042009-09-07 14:35:04 +010054 .name = "pca9532",
Riku Voipioe14fa822008-05-31 14:43:41 +010055 },
Richard Purdie85c52042009-09-07 14:35:04 +010056 .probe = pca9532_probe,
Riku Voipioe14fa822008-05-31 14:43:41 +010057 .remove = pca9532_remove,
58 .id_table = pca9532_id,
59};
60
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.
65 * */
Riku Voipio934cd3f2008-12-03 08:21:36 +000066static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
Riku Voipioe14fa822008-05-31 14:43:41 +010067 enum led_brightness value)
68{
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) {
74 a++;
75 b += data->leds[i].ldev.brightness;
76 }
77 }
78 if (a == 0) {
79 dev_err(&client->dev,
80 "fear of division by zero %d/%d, wanted %d\n",
81 b, a, value);
82 return -EINVAL;
83 }
84 b = b/a;
85 if (b > 0xFF)
86 return -EINVAL;
Riku Voipioe14fa822008-05-31 14:43:41 +010087 data->pwm[pwm] = b;
Antonio Ospite07172d22009-06-19 13:53:07 +020088 data->psc[pwm] = blink;
89 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +000090}
91
92static int pca9532_setpwm(struct i2c_client *client, int pwm)
93{
Antonio Ospite07172d22009-06-19 13:53:07 +020094 struct pca9532_data *data = i2c_get_clientdata(client);
95 mutex_lock(&data->update_lock);
Riku Voipioe14fa822008-05-31 14:43:41 +010096 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(pwm),
97 data->pwm[pwm]);
Riku Voipioe14fa822008-05-31 14:43:41 +010098 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(pwm),
99 data->psc[pwm]);
100 mutex_unlock(&data->update_lock);
101 return 0;
102}
103
104/* Set LED routing */
105static void pca9532_setled(struct pca9532_led *led)
106{
107 struct i2c_client *client = led->client;
108 struct pca9532_data *data = i2c_get_clientdata(client);
109 char reg;
110
111 mutex_lock(&data->update_lock);
112 reg = i2c_smbus_read_byte_data(client, LED_REG(led->id));
113 /* zero led bits */
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);
119}
120
121static void pca9532_set_brightness(struct led_classdev *led_cdev,
122 enum led_brightness value)
123{
124 int err = 0;
125 struct pca9532_led *led = ldev_to_led(led_cdev);
126
127 if (value == LED_OFF)
128 led->state = PCA9532_OFF;
129 else if (value == LED_FULL)
130 led->state = PCA9532_ON;
131 else {
132 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
Antonio Ospite07172d22009-06-19 13:53:07 +0200133 err = pca9532_calcpwm(led->client, 0, 0, value);
Riku Voipioe14fa822008-05-31 14:43:41 +0100134 if (err)
135 return; /* XXX: led api doesn't allow error code? */
136 }
Antonio Ospite07172d22009-06-19 13:53:07 +0200137 schedule_work(&led->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100138}
139
140static int pca9532_set_blink(struct led_classdev *led_cdev,
141 unsigned long *delay_on, unsigned long *delay_off)
142{
143 struct pca9532_led *led = ldev_to_led(led_cdev);
144 struct i2c_client *client = led->client;
145 int psc;
Antonio Ospite07172d22009-06-19 13:53:07 +0200146 int err = 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100147
148 if (*delay_on == 0 && *delay_off == 0) {
149 /* led subsystem ask us for a blink rate */
Richard Purdie85c52042009-09-07 14:35:04 +0100150 *delay_on = 1000;
Riku Voipioe14fa822008-05-31 14:43:41 +0100151 *delay_off = 1000;
152 }
153 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
154 return -EINVAL;
155
156 /* Thecus specific: only use PSC/PWM 0 */
157 psc = (*delay_on * 152-1)/1000;
Antonio Ospite07172d22009-06-19 13:53:07 +0200158 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
159 if (err)
160 return err;
161 schedule_work(&led->work);
162 return 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100163}
164
Sven Wegener0d733572008-11-17 14:33:41 +0000165static int pca9532_event(struct input_dev *dev, unsigned int type,
166 unsigned int code, int value)
Riku Voipioe14fa822008-05-31 14:43:41 +0100167{
168 struct pca9532_data *data = input_get_drvdata(dev);
169
Riku Voipio7fbc3a92009-03-03 22:13:06 +0200170 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
Riku Voipioe14fa822008-05-31 14:43:41 +0100171 return -1;
172
173 /* XXX: allow different kind of beeps with psc/pwm modifications */
174 if (value > 1 && value < 32767)
175 data->pwm[1] = 127;
176 else
177 data->pwm[1] = 0;
178
Antonio Ospite07172d22009-06-19 13:53:07 +0200179 schedule_work(&data->work);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000180
Antonio Ospite07172d22009-06-19 13:53:07 +0200181 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000182}
183
184static void pca9532_input_work(struct work_struct *work)
185{
Antonio Ospite07172d22009-06-19 13:53:07 +0200186 struct pca9532_data *data;
187 data = container_of(work, struct pca9532_data, work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100188 mutex_lock(&data->update_lock);
189 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(1),
190 data->pwm[1]);
191 mutex_unlock(&data->update_lock);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000192}
Riku Voipioe14fa822008-05-31 14:43:41 +0100193
Riku Voipio934cd3f2008-12-03 08:21:36 +0000194static void pca9532_led_work(struct work_struct *work)
195{
Antonio Ospite07172d22009-06-19 13:53:07 +0200196 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);
200 pca9532_setled(led);
Riku Voipioe14fa822008-05-31 14:43:41 +0100201}
202
203static int pca9532_configure(struct i2c_client *client,
204 struct pca9532_data *data, struct pca9532_platform_data *pdata)
205{
206 int i, err = 0;
207
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),
212 data->pwm[i]);
213 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(i),
214 data->psc[i]);
215 }
216
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;
221 led->id = i;
222 led->type = pled->type;
223 switch (led->type) {
224 case PCA9532_TYPE_NONE:
225 break;
226 case PCA9532_TYPE_LED:
227 led->state = pled->state;
Richard Purdie85c52042009-09-07 14:35:04 +0100228 led->name = pled->name;
Riku Voipioe14fa822008-05-31 14:43:41 +0100229 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;
Antonio Ospite07172d22009-06-19 13:53:07 +0200233 INIT_WORK(&led->work, pca9532_led_work);
Sven Wegenerf785d022008-12-03 08:12:53 +0000234 err = led_classdev_register(&client->dev, &led->ldev);
235 if (err < 0) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100236 dev_err(&client->dev,
237 "couldn't register LED %s\n",
238 led->name);
239 goto exit;
240 }
241 pca9532_setled(led);
242 break;
243 case PCA9532_TYPE_N2100_BEEP:
244 BUG_ON(data->idev);
245 led->state = PCA9532_PWM1;
246 pca9532_setled(led);
247 data->idev = input_allocate_device();
248 if (data->idev == NULL) {
249 err = -ENOMEM;
250 goto exit;
251 }
252 data->idev->name = pled->name;
253 data->idev->phys = "i2c/pca9532";
254 data->idev->id.bustype = BUS_HOST;
Richard Purdie85c52042009-09-07 14:35:04 +0100255 data->idev->id.vendor = 0x001f;
Riku Voipioe14fa822008-05-31 14:43:41 +0100256 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) |
260 BIT_MASK(SND_TONE);
261 data->idev->event = pca9532_event;
262 input_set_drvdata(data->idev, data);
Antonio Ospite07172d22009-06-19 13:53:07 +0200263 INIT_WORK(&data->work, pca9532_input_work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100264 err = input_register_device(data->idev);
265 if (err) {
266 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200267 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100268 data->idev = NULL;
269 goto exit;
270 }
271 break;
272 }
273 }
274 return 0;
275
276exit:
277 if (i > 0)
278 for (i = i - 1; i >= 0; i--)
279 switch (data->leds[i].type) {
280 case PCA9532_TYPE_NONE:
281 break;
282 case PCA9532_TYPE_LED:
283 led_classdev_unregister(&data->leds[i].ldev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200284 cancel_work_sync(&data->leds[i].work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100285 break;
286 case PCA9532_TYPE_N2100_BEEP:
287 if (data->idev != NULL) {
288 input_unregister_device(data->idev);
289 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200290 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100291 data->idev = NULL;
292 }
293 break;
294 }
295
296 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100297}
298
299static int pca9532_probe(struct i2c_client *client,
300 const struct i2c_device_id *id)
301{
302 struct pca9532_data *data = i2c_get_clientdata(client);
303 struct pca9532_platform_data *pca9532_pdata = client->dev.platform_data;
Sven Wegenerf785d022008-12-03 08:12:53 +0000304 int err;
305
306 if (!pca9532_pdata)
307 return -EIO;
Riku Voipioe14fa822008-05-31 14:43:41 +0100308
309 if (!i2c_check_functionality(client->adapter,
310 I2C_FUNC_SMBUS_BYTE_DATA))
311 return -EIO;
312
Sven Wegenerf785d022008-12-03 08:12:53 +0000313 data = kzalloc(sizeof(*data), GFP_KERNEL);
Riku Voipioe14fa822008-05-31 14:43:41 +0100314 if (!data)
315 return -ENOMEM;
316
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);
321
Sven Wegenerf785d022008-12-03 08:12:53 +0000322 err = pca9532_configure(client, data, pca9532_pdata);
323 if (err) {
324 kfree(data);
325 i2c_set_clientdata(client, NULL);
326 }
Riku Voipioe14fa822008-05-31 14:43:41 +0100327
Sven Wegenerf785d022008-12-03 08:12:53 +0000328 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100329}
330
331static int pca9532_remove(struct i2c_client *client)
332{
333 struct pca9532_data *data = i2c_get_clientdata(client);
334 int i;
335 for (i = 0; i < 16; i++)
336 switch (data->leds[i].type) {
337 case PCA9532_TYPE_NONE:
338 break;
339 case PCA9532_TYPE_LED:
340 led_classdev_unregister(&data->leds[i].ldev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200341 cancel_work_sync(&data->leds[i].work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100342 break;
343 case PCA9532_TYPE_N2100_BEEP:
344 if (data->idev != NULL) {
345 input_unregister_device(data->idev);
346 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200347 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100348 data->idev = NULL;
349 }
350 break;
351 }
352
353 kfree(data);
354 i2c_set_clientdata(client, NULL);
355 return 0;
356}
357
358static int __init pca9532_init(void)
359{
360 return i2c_add_driver(&pca9532_driver);
361}
362
363static void __exit pca9532_exit(void)
364{
365 i2c_del_driver(&pca9532_driver);
366}
367
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200368MODULE_AUTHOR("Riku Voipio");
Riku Voipioe14fa822008-05-31 14:43:41 +0100369MODULE_LICENSE("GPL");
370MODULE_DESCRIPTION("PCA 9532 LED dimmer");
371
372module_init(pca9532_init);
373module_exit(pca9532_exit);
374