Merge branch 'tracing/core' into perf/core
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / leds / leds-pca9532.c
1 /*
2  * pca9532.c - 16-bit Led dimmer
3  *
4  * Copyright (C) 2008 Riku Voipio
5  *
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>
19 #include <linux/workqueue.h>
20 #include <linux/leds-pca9532.h>
21
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)
27
28 #define ldev_to_led(c)       container_of(c, struct pca9532_led, ldev)
29
30 struct pca9532_data {
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;
36         u8 pwm[2];
37         u8 psc[2];
38 };
39
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);
43
44 static const struct i2c_device_id pca9532_id[] = {
45         { "pca9532", 0 },
46         { }
47 };
48
49 MODULE_DEVICE_TABLE(i2c, pca9532_id);
50
51 static struct i2c_driver pca9532_driver = {
52         .driver = {
53                 .name = "pca9532",
54         },
55         .probe = pca9532_probe,
56         .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  * */
65 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
66         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;
86         data->pwm[pwm] = b;
87         data->psc[pwm] = blink;
88         return 0;
89 }
90
91 static int pca9532_setpwm(struct i2c_client *client, int pwm)
92 {
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),
96                 data->pwm[pwm]);
97         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 */
104 static 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
120 static 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 */
132                 err = pca9532_calcpwm(led->client, 0, 0, value);
133                 if (err)
134                         return; /* XXX: led api doesn't allow error code? */
135         }
136         schedule_work(&led->work);
137 }
138
139 static 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;
145         int err = 0;
146
147         if (*delay_on == 0 && *delay_off == 0) {
148         /* led subsystem ask us for a blink rate */
149                 *delay_on = 1000;
150                 *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;
157         err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
158         if (err)
159                 return err;
160         schedule_work(&led->work);
161         return 0;
162 }
163
164 static int pca9532_event(struct input_dev *dev, unsigned int type,
165         unsigned int code, int value)
166 {
167         struct pca9532_data *data = input_get_drvdata(dev);
168
169         if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
170                 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
178         schedule_work(&data->work);
179
180         return 0;
181 }
182
183 static void pca9532_input_work(struct work_struct *work)
184 {
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),
189                 data->pwm[1]);
190         mutex_unlock(&data->update_lock);
191 }
192
193 static void pca9532_led_work(struct work_struct *work)
194 {
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);
199         pca9532_setled(led);
200 }
201
202 static 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;
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);
234                         if (err < 0) {
235                                 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;
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) |
259                                                 BIT_MASK(SND_TONE);
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);
264                         if (err) {
265                                 input_free_device(data->idev);
266                                 cancel_work_sync(&data->work);
267                                 data->idev = NULL;
268                                 goto exit;
269                         }
270                         break;
271                 }
272         }
273         return 0;
274
275 exit:
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);
283                                 cancel_work_sync(&data->leds[i].work);
284                                 break;
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);
290                                         data->idev = NULL;
291                                 }
292                                 break;
293                         }
294
295         return err;
296 }
297
298 static 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;
303         int err;
304
305         if (!pca9532_pdata)
306                 return -EIO;
307
308         if (!i2c_check_functionality(client->adapter,
309                 I2C_FUNC_SMBUS_BYTE_DATA))
310                 return -EIO;
311
312         data = kzalloc(sizeof(*data), GFP_KERNEL);
313         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
321         err = pca9532_configure(client, data, pca9532_pdata);
322         if (err) {
323                 kfree(data);
324                 i2c_set_clientdata(client, NULL);
325         }
326
327         return err;
328 }
329
330 static 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);
340                         cancel_work_sync(&data->leds[i].work);
341                         break;
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);
347                                 data->idev = NULL;
348                         }
349                         break;
350                 }
351
352         kfree(data);
353         i2c_set_clientdata(client, NULL);
354         return 0;
355 }
356
357 static int __init pca9532_init(void)
358 {
359         return i2c_add_driver(&pca9532_driver);
360 }
361
362 static void __exit pca9532_exit(void)
363 {
364         i2c_del_driver(&pca9532_driver);
365 }
366
367 MODULE_AUTHOR("Riku Voipio");
368 MODULE_LICENSE("GPL");
369 MODULE_DESCRIPTION("PCA 9532 LED dimmer");
370
371 module_init(pca9532_init);
372 module_exit(pca9532_exit);
373