blob: adc561eb59d2ed022873bacf35501b9462e19bbc [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>
16#include <linux/leds.h>
17#include <linux/input.h>
18#include <linux/mutex.h>
Riku Voipio934cd3f2008-12-03 08:21:36 +000019#include <linux/workqueue.h>
Riku Voipioe14fa822008-05-31 14:43:41 +010020#include <linux/leds-pca9532.h>
21
Riku Voipioe14fa822008-05-31 14:43:41 +010022#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)
27
28#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
29
30struct pca9532_data {
31 struct i2c_client *client;
32 struct pca9532_led leds[16];
33 struct mutex update_lock;
Richard Purdie85c52042009-09-07 14:35:04 +010034 struct input_dev *idev;
Antonio Ospite07172d22009-06-19 13:53:07 +020035 struct work_struct work;
Riku Voipioe14fa822008-05-31 14:43:41 +010036 u8 pwm[2];
37 u8 psc[2];
38};
39
40static int pca9532_probe(struct i2c_client *client,
41 const struct i2c_device_id *id);
42static int pca9532_remove(struct i2c_client *client);
43
44static const struct i2c_device_id pca9532_id[] = {
45 { "pca9532", 0 },
46 { }
47};
48
49MODULE_DEVICE_TABLE(i2c, pca9532_id);
50
51static struct i2c_driver pca9532_driver = {
52 .driver = {
Richard Purdie85c52042009-09-07 14:35:04 +010053 .name = "pca9532",
Riku Voipioe14fa822008-05-31 14:43:41 +010054 },
Richard Purdie85c52042009-09-07 14:35:04 +010055 .probe = pca9532_probe,
Riku Voipioe14fa822008-05-31 14:43:41 +010056 .remove = pca9532_remove,
57 .id_table = pca9532_id,
58};
59
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.
64 * */
Riku Voipio934cd3f2008-12-03 08:21:36 +000065static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
Riku Voipioe14fa822008-05-31 14:43:41 +010066 enum led_brightness value)
67{
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) {
73 a++;
74 b += data->leds[i].ldev.brightness;
75 }
76 }
77 if (a == 0) {
78 dev_err(&client->dev,
79 "fear of division by zero %d/%d, wanted %d\n",
80 b, a, value);
81 return -EINVAL;
82 }
83 b = b/a;
84 if (b > 0xFF)
85 return -EINVAL;
Riku Voipioe14fa822008-05-31 14:43:41 +010086 data->pwm[pwm] = b;
Antonio Ospite07172d22009-06-19 13:53:07 +020087 data->psc[pwm] = blink;
88 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +000089}
90
91static int pca9532_setpwm(struct i2c_client *client, int pwm)
92{
Antonio Ospite07172d22009-06-19 13:53:07 +020093 struct pca9532_data *data = i2c_get_clientdata(client);
94 mutex_lock(&data->update_lock);
Riku Voipioe14fa822008-05-31 14:43:41 +010095 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(pwm),
96 data->pwm[pwm]);
Riku Voipioe14fa822008-05-31 14:43:41 +010097 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(pwm),
98 data->psc[pwm]);
99 mutex_unlock(&data->update_lock);
100 return 0;
101}
102
103/* Set LED routing */
104static void pca9532_setled(struct pca9532_led *led)
105{
106 struct i2c_client *client = led->client;
107 struct pca9532_data *data = i2c_get_clientdata(client);
108 char reg;
109
110 mutex_lock(&data->update_lock);
111 reg = i2c_smbus_read_byte_data(client, LED_REG(led->id));
112 /* zero led bits */
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);
118}
119
120static void pca9532_set_brightness(struct led_classdev *led_cdev,
121 enum led_brightness value)
122{
123 int err = 0;
124 struct pca9532_led *led = ldev_to_led(led_cdev);
125
126 if (value == LED_OFF)
127 led->state = PCA9532_OFF;
128 else if (value == LED_FULL)
129 led->state = PCA9532_ON;
130 else {
131 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
Antonio Ospite07172d22009-06-19 13:53:07 +0200132 err = pca9532_calcpwm(led->client, 0, 0, value);
Riku Voipioe14fa822008-05-31 14:43:41 +0100133 if (err)
134 return; /* XXX: led api doesn't allow error code? */
135 }
Antonio Ospite07172d22009-06-19 13:53:07 +0200136 schedule_work(&led->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100137}
138
139static int pca9532_set_blink(struct led_classdev *led_cdev,
140 unsigned long *delay_on, unsigned long *delay_off)
141{
142 struct pca9532_led *led = ldev_to_led(led_cdev);
143 struct i2c_client *client = led->client;
144 int psc;
Antonio Ospite07172d22009-06-19 13:53:07 +0200145 int err = 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100146
147 if (*delay_on == 0 && *delay_off == 0) {
148 /* led subsystem ask us for a blink rate */
Richard Purdie85c52042009-09-07 14:35:04 +0100149 *delay_on = 1000;
Riku Voipioe14fa822008-05-31 14:43:41 +0100150 *delay_off = 1000;
151 }
152 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
153 return -EINVAL;
154
155 /* Thecus specific: only use PSC/PWM 0 */
156 psc = (*delay_on * 152-1)/1000;
Antonio Ospite07172d22009-06-19 13:53:07 +0200157 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
158 if (err)
159 return err;
160 schedule_work(&led->work);
161 return 0;
Riku Voipioe14fa822008-05-31 14:43:41 +0100162}
163
Sven Wegener0d733572008-11-17 14:33:41 +0000164static int pca9532_event(struct input_dev *dev, unsigned int type,
165 unsigned int code, int value)
Riku Voipioe14fa822008-05-31 14:43:41 +0100166{
167 struct pca9532_data *data = input_get_drvdata(dev);
168
Riku Voipio7fbc3a92009-03-03 22:13:06 +0200169 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
Riku Voipioe14fa822008-05-31 14:43:41 +0100170 return -1;
171
172 /* XXX: allow different kind of beeps with psc/pwm modifications */
173 if (value > 1 && value < 32767)
174 data->pwm[1] = 127;
175 else
176 data->pwm[1] = 0;
177
Antonio Ospite07172d22009-06-19 13:53:07 +0200178 schedule_work(&data->work);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000179
Antonio Ospite07172d22009-06-19 13:53:07 +0200180 return 0;
Riku Voipio934cd3f2008-12-03 08:21:36 +0000181}
182
183static void pca9532_input_work(struct work_struct *work)
184{
Antonio Ospite07172d22009-06-19 13:53:07 +0200185 struct pca9532_data *data;
186 data = container_of(work, struct pca9532_data, work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100187 mutex_lock(&data->update_lock);
188 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(1),
189 data->pwm[1]);
190 mutex_unlock(&data->update_lock);
Riku Voipio934cd3f2008-12-03 08:21:36 +0000191}
Riku Voipioe14fa822008-05-31 14:43:41 +0100192
Riku Voipio934cd3f2008-12-03 08:21:36 +0000193static void pca9532_led_work(struct work_struct *work)
194{
Antonio Ospite07172d22009-06-19 13:53:07 +0200195 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);
199 pca9532_setled(led);
Riku Voipioe14fa822008-05-31 14:43:41 +0100200}
201
202static int pca9532_configure(struct i2c_client *client,
203 struct pca9532_data *data, struct pca9532_platform_data *pdata)
204{
205 int i, err = 0;
206
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),
211 data->pwm[i]);
212 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(i),
213 data->psc[i]);
214 }
215
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;
220 led->id = i;
221 led->type = pled->type;
222 switch (led->type) {
223 case PCA9532_TYPE_NONE:
224 break;
225 case PCA9532_TYPE_LED:
226 led->state = pled->state;
Richard Purdie85c52042009-09-07 14:35:04 +0100227 led->name = pled->name;
Riku Voipioe14fa822008-05-31 14:43:41 +0100228 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;
Antonio Ospite07172d22009-06-19 13:53:07 +0200232 INIT_WORK(&led->work, pca9532_led_work);
Sven Wegenerf785d022008-12-03 08:12:53 +0000233 err = led_classdev_register(&client->dev, &led->ldev);
234 if (err < 0) {
Riku Voipioe14fa822008-05-31 14:43:41 +0100235 dev_err(&client->dev,
236 "couldn't register LED %s\n",
237 led->name);
238 goto exit;
239 }
240 pca9532_setled(led);
241 break;
242 case PCA9532_TYPE_N2100_BEEP:
243 BUG_ON(data->idev);
244 led->state = PCA9532_PWM1;
245 pca9532_setled(led);
246 data->idev = input_allocate_device();
247 if (data->idev == NULL) {
248 err = -ENOMEM;
249 goto exit;
250 }
251 data->idev->name = pled->name;
252 data->idev->phys = "i2c/pca9532";
253 data->idev->id.bustype = BUS_HOST;
Richard Purdie85c52042009-09-07 14:35:04 +0100254 data->idev->id.vendor = 0x001f;
Riku Voipioe14fa822008-05-31 14:43:41 +0100255 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) |
259 BIT_MASK(SND_TONE);
260 data->idev->event = pca9532_event;
261 input_set_drvdata(data->idev, data);
Antonio Ospite07172d22009-06-19 13:53:07 +0200262 INIT_WORK(&data->work, pca9532_input_work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100263 err = input_register_device(data->idev);
264 if (err) {
265 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200266 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100267 data->idev = NULL;
268 goto exit;
269 }
270 break;
271 }
272 }
273 return 0;
274
275exit:
276 if (i > 0)
277 for (i = i - 1; i >= 0; i--)
278 switch (data->leds[i].type) {
279 case PCA9532_TYPE_NONE:
280 break;
281 case PCA9532_TYPE_LED:
282 led_classdev_unregister(&data->leds[i].ldev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200283 cancel_work_sync(&data->leds[i].work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100284 break;
285 case PCA9532_TYPE_N2100_BEEP:
286 if (data->idev != NULL) {
287 input_unregister_device(data->idev);
288 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200289 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100290 data->idev = NULL;
291 }
292 break;
293 }
294
295 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100296}
297
298static int pca9532_probe(struct i2c_client *client,
299 const struct i2c_device_id *id)
300{
301 struct pca9532_data *data = i2c_get_clientdata(client);
302 struct pca9532_platform_data *pca9532_pdata = client->dev.platform_data;
Sven Wegenerf785d022008-12-03 08:12:53 +0000303 int err;
304
305 if (!pca9532_pdata)
306 return -EIO;
Riku Voipioe14fa822008-05-31 14:43:41 +0100307
308 if (!i2c_check_functionality(client->adapter,
309 I2C_FUNC_SMBUS_BYTE_DATA))
310 return -EIO;
311
Sven Wegenerf785d022008-12-03 08:12:53 +0000312 data = kzalloc(sizeof(*data), GFP_KERNEL);
Riku Voipioe14fa822008-05-31 14:43:41 +0100313 if (!data)
314 return -ENOMEM;
315
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);
320
Sven Wegenerf785d022008-12-03 08:12:53 +0000321 err = pca9532_configure(client, data, pca9532_pdata);
322 if (err) {
323 kfree(data);
324 i2c_set_clientdata(client, NULL);
325 }
Riku Voipioe14fa822008-05-31 14:43:41 +0100326
Sven Wegenerf785d022008-12-03 08:12:53 +0000327 return err;
Riku Voipioe14fa822008-05-31 14:43:41 +0100328}
329
330static int pca9532_remove(struct i2c_client *client)
331{
332 struct pca9532_data *data = i2c_get_clientdata(client);
333 int i;
334 for (i = 0; i < 16; i++)
335 switch (data->leds[i].type) {
336 case PCA9532_TYPE_NONE:
337 break;
338 case PCA9532_TYPE_LED:
339 led_classdev_unregister(&data->leds[i].ldev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200340 cancel_work_sync(&data->leds[i].work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100341 break;
342 case PCA9532_TYPE_N2100_BEEP:
343 if (data->idev != NULL) {
344 input_unregister_device(data->idev);
345 input_free_device(data->idev);
Antonio Ospite07172d22009-06-19 13:53:07 +0200346 cancel_work_sync(&data->work);
Riku Voipioe14fa822008-05-31 14:43:41 +0100347 data->idev = NULL;
348 }
349 break;
350 }
351
352 kfree(data);
353 i2c_set_clientdata(client, NULL);
354 return 0;
355}
356
357static int __init pca9532_init(void)
358{
359 return i2c_add_driver(&pca9532_driver);
360}
361
362static void __exit pca9532_exit(void)
363{
364 i2c_del_driver(&pca9532_driver);
365}
366
Riku Voipiob26e0ed2009-03-03 21:37:17 +0200367MODULE_AUTHOR("Riku Voipio");
Riku Voipioe14fa822008-05-31 14:43:41 +0100368MODULE_LICENSE("GPL");
369MODULE_DESCRIPTION("PCA 9532 LED dimmer");
370
371module_init(pca9532_init);
372module_exit(pca9532_exit);
373