parport_pc: show the detection of a 2 serial port ITE8874 chip
[linux-2.6.git] / drivers / regulator / wm831x-ldo.c
1 /*
2  * wm831x-ldo.c  --  LDO driver for the WM831x series
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/slab.h>
23
24 #include <linux/mfd/wm831x/core.h>
25 #include <linux/mfd/wm831x/regulator.h>
26 #include <linux/mfd/wm831x/pdata.h>
27
28 #define WM831X_LDO_MAX_NAME 6
29
30 #define WM831X_LDO_CONTROL       0
31 #define WM831X_LDO_ON_CONTROL    1
32 #define WM831X_LDO_SLEEP_CONTROL 2
33
34 #define WM831X_ALIVE_LDO_ON_CONTROL    0
35 #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
36
37 struct wm831x_ldo {
38         char name[WM831X_LDO_MAX_NAME];
39         struct regulator_desc desc;
40         int base;
41         struct wm831x *wm831x;
42         struct regulator_dev *regulator;
43 };
44
45 /*
46  * Shared
47  */
48
49 static int wm831x_ldo_is_enabled(struct regulator_dev *rdev)
50 {
51         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
52         struct wm831x *wm831x = ldo->wm831x;
53         int mask = 1 << rdev_get_id(rdev);
54         int reg;
55
56         reg = wm831x_reg_read(wm831x, WM831X_LDO_ENABLE);
57         if (reg < 0)
58                 return reg;
59
60         if (reg & mask)
61                 return 1;
62         else
63                 return 0;
64 }
65
66 static int wm831x_ldo_enable(struct regulator_dev *rdev)
67 {
68         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
69         struct wm831x *wm831x = ldo->wm831x;
70         int mask = 1 << rdev_get_id(rdev);
71
72         return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, mask);
73 }
74
75 static int wm831x_ldo_disable(struct regulator_dev *rdev)
76 {
77         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
78         struct wm831x *wm831x = ldo->wm831x;
79         int mask = 1 << rdev_get_id(rdev);
80
81         return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, 0);
82 }
83
84 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
85 {
86         struct wm831x_ldo *ldo = data;
87
88         regulator_notifier_call_chain(ldo->regulator,
89                                       REGULATOR_EVENT_UNDER_VOLTAGE,
90                                       NULL);
91
92         return IRQ_HANDLED;
93 }
94
95 /*
96  * General purpose LDOs
97  */
98
99 #define WM831X_GP_LDO_SELECTOR_LOW 0xe
100 #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
101
102 static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
103                                       unsigned int selector)
104 {
105         /* 0.9-1.6V in 50mV steps */
106         if (selector <= WM831X_GP_LDO_SELECTOR_LOW)
107                 return 900000 + (selector * 50000);
108         /* 1.7-3.3V in 50mV steps */
109         if (selector <= WM831X_GP_LDO_MAX_SELECTOR)
110                 return 1600000 + ((selector - WM831X_GP_LDO_SELECTOR_LOW)
111                                   * 100000);
112         return -EINVAL;
113 }
114
115 static int wm831x_gp_ldo_set_voltage_int(struct regulator_dev *rdev, int reg,
116                                          int min_uV, int max_uV)
117 {
118         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
119         struct wm831x *wm831x = ldo->wm831x;
120         int vsel, ret;
121
122         if (min_uV < 900000)
123                 vsel = 0;
124         else if (min_uV < 1700000)
125                 vsel = ((min_uV - 900000) / 50000);
126         else
127                 vsel = ((min_uV - 1700000) / 100000)
128                         + WM831X_GP_LDO_SELECTOR_LOW + 1;
129
130         ret = wm831x_gp_ldo_list_voltage(rdev, vsel);
131         if (ret < 0)
132                 return ret;
133         if (ret < min_uV || ret > max_uV)
134                 return -EINVAL;
135
136         return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
137 }
138
139 static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
140                                      int min_uV, int max_uV)
141 {
142         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
143         int reg = ldo->base + WM831X_LDO_ON_CONTROL;
144
145         return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
146 }
147
148 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
149                                              int uV)
150 {
151         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
152         int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
153
154         return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV);
155 }
156
157 static int wm831x_gp_ldo_get_voltage(struct regulator_dev *rdev)
158 {
159         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
160         struct wm831x *wm831x = ldo->wm831x;
161         int reg = ldo->base + WM831X_LDO_ON_CONTROL;
162         int ret;
163
164         ret = wm831x_reg_read(wm831x, reg);
165         if (ret < 0)
166                 return ret;
167
168         ret &= WM831X_LDO1_ON_VSEL_MASK;
169
170         return wm831x_gp_ldo_list_voltage(rdev, ret);
171 }
172
173 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
174 {
175         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
176         struct wm831x *wm831x = ldo->wm831x;
177         int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
178         int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
179         int ret;
180
181         ret = wm831x_reg_read(wm831x, on_reg);
182         if (ret < 0)
183                 return ret;
184
185         if (!(ret & WM831X_LDO1_ON_MODE))
186                 return REGULATOR_MODE_NORMAL;
187
188         ret = wm831x_reg_read(wm831x, ctrl_reg);
189         if (ret < 0)
190                 return ret;
191
192         if (ret & WM831X_LDO1_LP_MODE)
193                 return REGULATOR_MODE_STANDBY;
194         else
195                 return REGULATOR_MODE_IDLE;
196 }
197
198 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
199                                   unsigned int mode)
200 {
201         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
202         struct wm831x *wm831x = ldo->wm831x;
203         int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
204         int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
205         int ret;
206
207
208         switch (mode) {
209         case REGULATOR_MODE_NORMAL:
210                 ret = wm831x_set_bits(wm831x, on_reg,
211                                       WM831X_LDO1_ON_MODE, 0);
212                 if (ret < 0)
213                         return ret;
214                 break;
215
216         case REGULATOR_MODE_IDLE:
217                 ret = wm831x_set_bits(wm831x, ctrl_reg,
218                                       WM831X_LDO1_LP_MODE,
219                                       WM831X_LDO1_LP_MODE);
220                 if (ret < 0)
221                         return ret;
222
223                 ret = wm831x_set_bits(wm831x, on_reg,
224                                       WM831X_LDO1_ON_MODE,
225                                       WM831X_LDO1_ON_MODE);
226                 if (ret < 0)
227                         return ret;
228
229         case REGULATOR_MODE_STANDBY:
230                 ret = wm831x_set_bits(wm831x, ctrl_reg,
231                                       WM831X_LDO1_LP_MODE, 0);
232                 if (ret < 0)
233                         return ret;
234
235                 ret = wm831x_set_bits(wm831x, on_reg,
236                                       WM831X_LDO1_ON_MODE,
237                                       WM831X_LDO1_ON_MODE);
238                 if (ret < 0)
239                         return ret;
240                 break;
241
242         default:
243                 return -EINVAL;
244         }
245
246         return 0;
247 }
248
249 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
250 {
251         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
252         struct wm831x *wm831x = ldo->wm831x;
253         int mask = 1 << rdev_get_id(rdev);
254         int ret;
255
256         /* Is the regulator on? */
257         ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
258         if (ret < 0)
259                 return ret;
260         if (!(ret & mask))
261                 return REGULATOR_STATUS_OFF;
262
263         /* Is it reporting under voltage? */
264         ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
265         if (ret & mask)
266                 return REGULATOR_STATUS_ERROR;
267
268         ret = wm831x_gp_ldo_get_mode(rdev);
269         if (ret < 0)
270                 return ret;
271         else
272                 return regulator_mode_to_status(ret);
273 }
274
275 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
276                                                    int input_uV,
277                                                    int output_uV, int load_uA)
278 {
279         if (load_uA < 20000)
280                 return REGULATOR_MODE_STANDBY;
281         if (load_uA < 50000)
282                 return REGULATOR_MODE_IDLE;
283         return REGULATOR_MODE_NORMAL;
284 }
285
286
287 static struct regulator_ops wm831x_gp_ldo_ops = {
288         .list_voltage = wm831x_gp_ldo_list_voltage,
289         .get_voltage = wm831x_gp_ldo_get_voltage,
290         .set_voltage = wm831x_gp_ldo_set_voltage,
291         .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
292         .get_mode = wm831x_gp_ldo_get_mode,
293         .set_mode = wm831x_gp_ldo_set_mode,
294         .get_status = wm831x_gp_ldo_get_status,
295         .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
296
297         .is_enabled = wm831x_ldo_is_enabled,
298         .enable = wm831x_ldo_enable,
299         .disable = wm831x_ldo_disable,
300 };
301
302 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
303 {
304         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
305         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
306         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
307         struct wm831x_ldo *ldo;
308         struct resource *res;
309         int ret, irq;
310
311         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
312
313         if (pdata == NULL || pdata->ldo[id] == NULL)
314                 return -ENODEV;
315
316         ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
317         if (ldo == NULL) {
318                 dev_err(&pdev->dev, "Unable to allocate private data\n");
319                 return -ENOMEM;
320         }
321
322         ldo->wm831x = wm831x;
323
324         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
325         if (res == NULL) {
326                 dev_err(&pdev->dev, "No I/O resource\n");
327                 ret = -EINVAL;
328                 goto err;
329         }
330         ldo->base = res->start;
331
332         snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
333         ldo->desc.name = ldo->name;
334         ldo->desc.id = id;
335         ldo->desc.type = REGULATOR_VOLTAGE;
336         ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
337         ldo->desc.ops = &wm831x_gp_ldo_ops;
338         ldo->desc.owner = THIS_MODULE;
339
340         ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
341                                              pdata->ldo[id], ldo);
342         if (IS_ERR(ldo->regulator)) {
343                 ret = PTR_ERR(ldo->regulator);
344                 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
345                         id + 1, ret);
346                 goto err;
347         }
348
349         irq = platform_get_irq_byname(pdev, "UV");
350         ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
351                                  IRQF_TRIGGER_RISING, ldo->name,
352                                  ldo);
353         if (ret != 0) {
354                 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
355                         irq, ret);
356                 goto err_regulator;
357         }
358
359         platform_set_drvdata(pdev, ldo);
360
361         return 0;
362
363 err_regulator:
364         regulator_unregister(ldo->regulator);
365 err:
366         kfree(ldo);
367         return ret;
368 }
369
370 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
371 {
372         struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
373         struct wm831x *wm831x = ldo->wm831x;
374
375         platform_set_drvdata(pdev, NULL);
376
377         wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
378         regulator_unregister(ldo->regulator);
379         kfree(ldo);
380
381         return 0;
382 }
383
384 static struct platform_driver wm831x_gp_ldo_driver = {
385         .probe = wm831x_gp_ldo_probe,
386         .remove = __devexit_p(wm831x_gp_ldo_remove),
387         .driver         = {
388                 .name   = "wm831x-ldo",
389                 .owner  = THIS_MODULE,
390         },
391 };
392
393 /*
394  * Analogue LDOs
395  */
396
397
398 #define WM831X_ALDO_SELECTOR_LOW 0xc
399 #define WM831X_ALDO_MAX_SELECTOR 0x1f
400
401 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
402                                       unsigned int selector)
403 {
404         /* 1-1.6V in 50mV steps */
405         if (selector <= WM831X_ALDO_SELECTOR_LOW)
406                 return 1000000 + (selector * 50000);
407         /* 1.7-3.5V in 50mV steps */
408         if (selector <= WM831X_ALDO_MAX_SELECTOR)
409                 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
410                                   * 100000);
411         return -EINVAL;
412 }
413
414 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
415                                          int min_uV, int max_uV)
416 {
417         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
418         struct wm831x *wm831x = ldo->wm831x;
419         int vsel, ret;
420
421         if (min_uV < 1000000)
422                 vsel = 0;
423         else if (min_uV < 1700000)
424                 vsel = ((min_uV - 1000000) / 50000);
425         else
426                 vsel = ((min_uV - 1700000) / 100000)
427                         + WM831X_ALDO_SELECTOR_LOW + 1;
428
429         ret = wm831x_aldo_list_voltage(rdev, vsel);
430         if (ret < 0)
431                 return ret;
432         if (ret < min_uV || ret > max_uV)
433                 return -EINVAL;
434
435         return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
436 }
437
438 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
439                                      int min_uV, int max_uV)
440 {
441         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
442         int reg = ldo->base + WM831X_LDO_ON_CONTROL;
443
444         return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV);
445 }
446
447 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
448                                              int uV)
449 {
450         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
451         int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
452
453         return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV);
454 }
455
456 static int wm831x_aldo_get_voltage(struct regulator_dev *rdev)
457 {
458         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
459         struct wm831x *wm831x = ldo->wm831x;
460         int reg = ldo->base + WM831X_LDO_ON_CONTROL;
461         int ret;
462
463         ret = wm831x_reg_read(wm831x, reg);
464         if (ret < 0)
465                 return ret;
466
467         ret &= WM831X_LDO7_ON_VSEL_MASK;
468
469         return wm831x_aldo_list_voltage(rdev, ret);
470 }
471
472 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
473 {
474         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
475         struct wm831x *wm831x = ldo->wm831x;
476         int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
477         int ret;
478
479         ret = wm831x_reg_read(wm831x, on_reg);
480         if (ret < 0)
481                 return 0;
482
483         if (ret & WM831X_LDO7_ON_MODE)
484                 return REGULATOR_MODE_IDLE;
485         else
486                 return REGULATOR_MODE_NORMAL;
487 }
488
489 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
490                                   unsigned int mode)
491 {
492         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
493         struct wm831x *wm831x = ldo->wm831x;
494         int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
495         int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
496         int ret;
497
498
499         switch (mode) {
500         case REGULATOR_MODE_NORMAL:
501                 ret = wm831x_set_bits(wm831x, on_reg,
502                                       WM831X_LDO7_ON_MODE, 0);
503                 if (ret < 0)
504                         return ret;
505                 break;
506
507         case REGULATOR_MODE_IDLE:
508                 ret = wm831x_set_bits(wm831x, ctrl_reg,
509                                       WM831X_LDO7_ON_MODE,
510                                       WM831X_LDO7_ON_MODE);
511                 if (ret < 0)
512                         return ret;
513                 break;
514
515         default:
516                 return -EINVAL;
517         }
518
519         return 0;
520 }
521
522 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
523 {
524         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
525         struct wm831x *wm831x = ldo->wm831x;
526         int mask = 1 << rdev_get_id(rdev);
527         int ret;
528
529         /* Is the regulator on? */
530         ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
531         if (ret < 0)
532                 return ret;
533         if (!(ret & mask))
534                 return REGULATOR_STATUS_OFF;
535
536         /* Is it reporting under voltage? */
537         ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
538         if (ret & mask)
539                 return REGULATOR_STATUS_ERROR;
540
541         ret = wm831x_aldo_get_mode(rdev);
542         if (ret < 0)
543                 return ret;
544         else
545                 return regulator_mode_to_status(ret);
546 }
547
548 static struct regulator_ops wm831x_aldo_ops = {
549         .list_voltage = wm831x_aldo_list_voltage,
550         .get_voltage = wm831x_aldo_get_voltage,
551         .set_voltage = wm831x_aldo_set_voltage,
552         .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
553         .get_mode = wm831x_aldo_get_mode,
554         .set_mode = wm831x_aldo_set_mode,
555         .get_status = wm831x_aldo_get_status,
556
557         .is_enabled = wm831x_ldo_is_enabled,
558         .enable = wm831x_ldo_enable,
559         .disable = wm831x_ldo_disable,
560 };
561
562 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
563 {
564         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
565         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
566         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
567         struct wm831x_ldo *ldo;
568         struct resource *res;
569         int ret, irq;
570
571         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
572
573         if (pdata == NULL || pdata->ldo[id] == NULL)
574                 return -ENODEV;
575
576         ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
577         if (ldo == NULL) {
578                 dev_err(&pdev->dev, "Unable to allocate private data\n");
579                 return -ENOMEM;
580         }
581
582         ldo->wm831x = wm831x;
583
584         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
585         if (res == NULL) {
586                 dev_err(&pdev->dev, "No I/O resource\n");
587                 ret = -EINVAL;
588                 goto err;
589         }
590         ldo->base = res->start;
591
592         snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
593         ldo->desc.name = ldo->name;
594         ldo->desc.id = id;
595         ldo->desc.type = REGULATOR_VOLTAGE;
596         ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
597         ldo->desc.ops = &wm831x_aldo_ops;
598         ldo->desc.owner = THIS_MODULE;
599
600         ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
601                                              pdata->ldo[id], ldo);
602         if (IS_ERR(ldo->regulator)) {
603                 ret = PTR_ERR(ldo->regulator);
604                 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
605                         id + 1, ret);
606                 goto err;
607         }
608
609         irq = platform_get_irq_byname(pdev, "UV");
610         ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
611                                  IRQF_TRIGGER_RISING, ldo->name,
612                                  ldo);
613         if (ret != 0) {
614                 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
615                         irq, ret);
616                 goto err_regulator;
617         }
618
619         platform_set_drvdata(pdev, ldo);
620
621         return 0;
622
623 err_regulator:
624         regulator_unregister(ldo->regulator);
625 err:
626         kfree(ldo);
627         return ret;
628 }
629
630 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
631 {
632         struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
633         struct wm831x *wm831x = ldo->wm831x;
634
635         wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
636         regulator_unregister(ldo->regulator);
637         kfree(ldo);
638
639         return 0;
640 }
641
642 static struct platform_driver wm831x_aldo_driver = {
643         .probe = wm831x_aldo_probe,
644         .remove = __devexit_p(wm831x_aldo_remove),
645         .driver         = {
646                 .name   = "wm831x-aldo",
647                 .owner  = THIS_MODULE,
648         },
649 };
650
651 /*
652  * Alive LDO
653  */
654
655 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
656
657 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
658                                       unsigned int selector)
659 {
660         /* 0.8-1.55V in 50mV steps */
661         if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
662                 return 800000 + (selector * 50000);
663         return -EINVAL;
664 }
665
666 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
667                                             int reg,
668                                             int min_uV, int max_uV)
669 {
670         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
671         struct wm831x *wm831x = ldo->wm831x;
672         int vsel, ret;
673
674         vsel = (min_uV - 800000) / 50000;
675
676         ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
677         if (ret < 0)
678                 return ret;
679         if (ret < min_uV || ret > max_uV)
680                 return -EINVAL;
681
682         return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
683 }
684
685 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
686                                      int min_uV, int max_uV)
687 {
688         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
689         int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
690
691         return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
692 }
693
694 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
695                                              int uV)
696 {
697         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
698         int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
699
700         return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV);
701 }
702
703 static int wm831x_alive_ldo_get_voltage(struct regulator_dev *rdev)
704 {
705         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
706         struct wm831x *wm831x = ldo->wm831x;
707         int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
708         int ret;
709
710         ret = wm831x_reg_read(wm831x, reg);
711         if (ret < 0)
712                 return ret;
713
714         ret &= WM831X_LDO11_ON_VSEL_MASK;
715
716         return wm831x_alive_ldo_list_voltage(rdev, ret);
717 }
718
719 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
720 {
721         struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
722         struct wm831x *wm831x = ldo->wm831x;
723         int mask = 1 << rdev_get_id(rdev);
724         int ret;
725
726         /* Is the regulator on? */
727         ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
728         if (ret < 0)
729                 return ret;
730         if (ret & mask)
731                 return REGULATOR_STATUS_ON;
732         else
733                 return REGULATOR_STATUS_OFF;
734 }
735
736 static struct regulator_ops wm831x_alive_ldo_ops = {
737         .list_voltage = wm831x_alive_ldo_list_voltage,
738         .get_voltage = wm831x_alive_ldo_get_voltage,
739         .set_voltage = wm831x_alive_ldo_set_voltage,
740         .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
741         .get_status = wm831x_alive_ldo_get_status,
742
743         .is_enabled = wm831x_ldo_is_enabled,
744         .enable = wm831x_ldo_enable,
745         .disable = wm831x_ldo_disable,
746 };
747
748 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
749 {
750         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
751         struct wm831x_pdata *pdata = wm831x->dev->platform_data;
752         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
753         struct wm831x_ldo *ldo;
754         struct resource *res;
755         int ret;
756
757         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
758
759         if (pdata == NULL || pdata->ldo[id] == NULL)
760                 return -ENODEV;
761
762         ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
763         if (ldo == NULL) {
764                 dev_err(&pdev->dev, "Unable to allocate private data\n");
765                 return -ENOMEM;
766         }
767
768         ldo->wm831x = wm831x;
769
770         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
771         if (res == NULL) {
772                 dev_err(&pdev->dev, "No I/O resource\n");
773                 ret = -EINVAL;
774                 goto err;
775         }
776         ldo->base = res->start;
777
778         snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
779         ldo->desc.name = ldo->name;
780         ldo->desc.id = id;
781         ldo->desc.type = REGULATOR_VOLTAGE;
782         ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
783         ldo->desc.ops = &wm831x_alive_ldo_ops;
784         ldo->desc.owner = THIS_MODULE;
785
786         ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
787                                              pdata->ldo[id], ldo);
788         if (IS_ERR(ldo->regulator)) {
789                 ret = PTR_ERR(ldo->regulator);
790                 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
791                         id + 1, ret);
792                 goto err;
793         }
794
795         platform_set_drvdata(pdev, ldo);
796
797         return 0;
798
799 err:
800         kfree(ldo);
801         return ret;
802 }
803
804 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
805 {
806         struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
807
808         regulator_unregister(ldo->regulator);
809         kfree(ldo);
810
811         return 0;
812 }
813
814 static struct platform_driver wm831x_alive_ldo_driver = {
815         .probe = wm831x_alive_ldo_probe,
816         .remove = __devexit_p(wm831x_alive_ldo_remove),
817         .driver         = {
818                 .name   = "wm831x-alive-ldo",
819                 .owner  = THIS_MODULE,
820         },
821 };
822
823 static int __init wm831x_ldo_init(void)
824 {
825         int ret;
826
827         ret = platform_driver_register(&wm831x_gp_ldo_driver);
828         if (ret != 0)
829                 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
830
831         ret = platform_driver_register(&wm831x_aldo_driver);
832         if (ret != 0)
833                 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
834
835         ret = platform_driver_register(&wm831x_alive_ldo_driver);
836         if (ret != 0)
837                 pr_err("Failed to register WM831x alive LDO driver: %d\n",
838                        ret);
839
840         return 0;
841 }
842 subsys_initcall(wm831x_ldo_init);
843
844 static void __exit wm831x_ldo_exit(void)
845 {
846         platform_driver_unregister(&wm831x_alive_ldo_driver);
847         platform_driver_unregister(&wm831x_aldo_driver);
848         platform_driver_unregister(&wm831x_gp_ldo_driver);
849 }
850 module_exit(wm831x_ldo_exit);
851
852 /* Module information */
853 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
854 MODULE_DESCRIPTION("WM831x LDO driver");
855 MODULE_LICENSE("GPL");
856 MODULE_ALIAS("platform:wm831x-ldo");
857 MODULE_ALIAS("platform:wm831x-aldo");
858 MODULE_ALIAS("platform:wm831x-aliveldo");