Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / leds / leds-wm831x-status.c
1 /*
2  * LED driver for WM831x status LEDs
3  *
4  * Copyright(C) 2009 Wolfson Microelectronics PLC.
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 version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/mfd/wm831x/core.h>
19 #include <linux/mfd/wm831x/pdata.h>
20 #include <linux/mfd/wm831x/status.h>
21 #include <linux/module.h>
22
23
24 struct wm831x_status {
25         struct led_classdev cdev;
26         struct wm831x *wm831x;
27         struct work_struct work;
28         struct mutex mutex;
29
30         spinlock_t value_lock;
31         int reg;     /* Control register */
32         int reg_val; /* Control register value */
33
34         int blink;
35         int blink_time;
36         int blink_cyc;
37         int src;
38         enum led_brightness brightness;
39 };
40
41 #define to_wm831x_status(led_cdev) \
42         container_of(led_cdev, struct wm831x_status, cdev)
43
44 static void wm831x_status_work(struct work_struct *work)
45 {
46         struct wm831x_status *led = container_of(work, struct wm831x_status,
47                                                  work);
48         unsigned long flags;
49
50         mutex_lock(&led->mutex);
51
52         led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK |
53                           WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK);
54
55         spin_lock_irqsave(&led->value_lock, flags);
56
57         led->reg_val |= led->src << WM831X_LED_SRC_SHIFT;
58         if (led->blink) {
59                 led->reg_val |= 2 << WM831X_LED_MODE_SHIFT;
60                 led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT;
61                 led->reg_val |= led->blink_cyc;
62         } else {
63                 if (led->brightness != LED_OFF)
64                         led->reg_val |= 1 << WM831X_LED_MODE_SHIFT;
65         }
66
67         spin_unlock_irqrestore(&led->value_lock, flags);
68
69         wm831x_reg_write(led->wm831x, led->reg, led->reg_val);
70
71         mutex_unlock(&led->mutex);
72 }
73
74 static void wm831x_status_set(struct led_classdev *led_cdev,
75                            enum led_brightness value)
76 {
77         struct wm831x_status *led = to_wm831x_status(led_cdev);
78         unsigned long flags;
79
80         spin_lock_irqsave(&led->value_lock, flags);
81         led->brightness = value;
82         if (value == LED_OFF)
83                 led->blink = 0;
84         schedule_work(&led->work);
85         spin_unlock_irqrestore(&led->value_lock, flags);
86 }
87
88 static int wm831x_status_blink_set(struct led_classdev *led_cdev,
89                                    unsigned long *delay_on,
90                                    unsigned long *delay_off)
91 {
92         struct wm831x_status *led = to_wm831x_status(led_cdev);
93         unsigned long flags;
94         int ret = 0;
95
96         /* Pick some defaults if we've not been given times */
97         if (*delay_on == 0 && *delay_off == 0) {
98                 *delay_on = 250;
99                 *delay_off = 250;
100         }
101
102         spin_lock_irqsave(&led->value_lock, flags);
103
104         /* We only have a limited selection of settings, see if we can
105          * support the configuration we're being given */
106         switch (*delay_on) {
107         case 1000:
108                 led->blink_time = 0;
109                 break;
110         case 250:
111                 led->blink_time = 1;
112                 break;
113         case 125:
114                 led->blink_time = 2;
115                 break;
116         case 62:
117         case 63:
118                 /* Actually 62.5ms */
119                 led->blink_time = 3;
120                 break;
121         default:
122                 ret = -EINVAL;
123                 break;
124         }
125
126         if (ret == 0) {
127                 switch (*delay_off / *delay_on) {
128                 case 1:
129                         led->blink_cyc = 0;
130                         break;
131                 case 3:
132                         led->blink_cyc = 1;
133                         break;
134                 case 4:
135                         led->blink_cyc = 2;
136                         break;
137                 case 8:
138                         led->blink_cyc = 3;
139                         break;
140                 default:
141                         ret = -EINVAL;
142                         break;
143                 }
144         }
145
146         if (ret == 0)
147                 led->blink = 1;
148         else
149                 led->blink = 0;
150
151         /* Always update; if we fail turn off blinking since we expect
152          * a software fallback. */
153         schedule_work(&led->work);
154
155         spin_unlock_irqrestore(&led->value_lock, flags);
156
157         return ret;
158 }
159
160 static const char *led_src_texts[] = {
161         "otp",
162         "power",
163         "charger",
164         "soft",
165 };
166
167 static ssize_t wm831x_status_src_show(struct device *dev,
168                                       struct device_attribute *attr, char *buf)
169 {
170         struct led_classdev *led_cdev = dev_get_drvdata(dev);
171         struct wm831x_status *led = to_wm831x_status(led_cdev);
172         int i;
173         ssize_t ret = 0;
174
175         mutex_lock(&led->mutex);
176
177         for (i = 0; i < ARRAY_SIZE(led_src_texts); i++)
178                 if (i == led->src)
179                         ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]);
180                 else
181                         ret += sprintf(&buf[ret], "%s ", led_src_texts[i]);
182
183         mutex_unlock(&led->mutex);
184
185         ret += sprintf(&buf[ret], "\n");
186
187         return ret;
188 }
189
190 static ssize_t wm831x_status_src_store(struct device *dev,
191                                        struct device_attribute *attr,
192                                        const char *buf, size_t size)
193 {
194         struct led_classdev *led_cdev = dev_get_drvdata(dev);
195         struct wm831x_status *led = to_wm831x_status(led_cdev);
196         char name[20];
197         int i;
198         size_t len;
199
200         name[sizeof(name) - 1] = '\0';
201         strncpy(name, buf, sizeof(name) - 1);
202         len = strlen(name);
203
204         if (len && name[len - 1] == '\n')
205                 name[len - 1] = '\0';
206
207         for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) {
208                 if (!strcmp(name, led_src_texts[i])) {
209                         mutex_lock(&led->mutex);
210
211                         led->src = i;
212                         schedule_work(&led->work);
213
214                         mutex_unlock(&led->mutex);
215                 }
216         }
217
218         return size;
219 }
220
221 static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store);
222
223 static int wm831x_status_probe(struct platform_device *pdev)
224 {
225         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
226         struct wm831x_pdata *chip_pdata;
227         struct wm831x_status_pdata pdata;
228         struct wm831x_status *drvdata;
229         struct resource *res;
230         int id = pdev->id % ARRAY_SIZE(chip_pdata->status);
231         int ret;
232
233         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
234         if (res == NULL) {
235                 dev_err(&pdev->dev, "No I/O resource\n");
236                 ret = -EINVAL;
237                 goto err;
238         }
239
240         drvdata = kzalloc(sizeof(struct wm831x_status), GFP_KERNEL);
241         if (!drvdata)
242                 return -ENOMEM;
243         dev_set_drvdata(&pdev->dev, drvdata);
244
245         drvdata->wm831x = wm831x;
246         drvdata->reg = res->start;
247
248         if (wm831x->dev->platform_data)
249                 chip_pdata = wm831x->dev->platform_data;
250         else
251                 chip_pdata = NULL;
252
253         memset(&pdata, 0, sizeof(pdata));
254         if (chip_pdata && chip_pdata->status[id])
255                 memcpy(&pdata, chip_pdata->status[id], sizeof(pdata));
256         else
257                 pdata.name = dev_name(&pdev->dev);
258
259         mutex_init(&drvdata->mutex);
260         INIT_WORK(&drvdata->work, wm831x_status_work);
261         spin_lock_init(&drvdata->value_lock);
262
263         /* We cache the configuration register and read startup values
264          * from it. */
265         drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg);
266
267         if (drvdata->reg_val & WM831X_LED_MODE_MASK)
268                 drvdata->brightness = LED_FULL;
269         else
270                 drvdata->brightness = LED_OFF;
271
272         /* Set a default source if configured, otherwise leave the
273          * current hardware setting.
274          */
275         if (pdata.default_src == WM831X_STATUS_PRESERVE) {
276                 drvdata->src = drvdata->reg_val;
277                 drvdata->src &= WM831X_LED_SRC_MASK;
278                 drvdata->src >>= WM831X_LED_SRC_SHIFT;
279         } else {
280                 drvdata->src = pdata.default_src - 1;
281         }
282
283         drvdata->cdev.name = pdata.name;
284         drvdata->cdev.default_trigger = pdata.default_trigger;
285         drvdata->cdev.brightness_set = wm831x_status_set;
286         drvdata->cdev.blink_set = wm831x_status_blink_set;
287
288         ret = led_classdev_register(wm831x->dev, &drvdata->cdev);
289         if (ret < 0) {
290                 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
291                 goto err_led;
292         }
293
294         ret = device_create_file(drvdata->cdev.dev, &dev_attr_src);
295         if (ret != 0)
296                 dev_err(&pdev->dev,
297                         "No source control for LED: %d\n", ret);
298
299         return 0;
300
301 err_led:
302         led_classdev_unregister(&drvdata->cdev);
303         kfree(drvdata);
304 err:
305         return ret;
306 }
307
308 static int wm831x_status_remove(struct platform_device *pdev)
309 {
310         struct wm831x_status *drvdata = platform_get_drvdata(pdev);
311
312         device_remove_file(drvdata->cdev.dev, &dev_attr_src);
313         led_classdev_unregister(&drvdata->cdev);
314         kfree(drvdata);
315
316         return 0;
317 }
318
319 static struct platform_driver wm831x_status_driver = {
320         .driver = {
321                    .name = "wm831x-status",
322                    .owner = THIS_MODULE,
323                    },
324         .probe = wm831x_status_probe,
325         .remove = wm831x_status_remove,
326 };
327
328 static int __devinit wm831x_status_init(void)
329 {
330         return platform_driver_register(&wm831x_status_driver);
331 }
332 module_init(wm831x_status_init);
333
334 static void wm831x_status_exit(void)
335 {
336         platform_driver_unregister(&wm831x_status_driver);
337 }
338 module_exit(wm831x_status_exit);
339
340 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
341 MODULE_DESCRIPTION("WM831x status LED driver");
342 MODULE_LICENSE("GPL");
343 MODULE_ALIAS("platform:wm831x-status");