blob: 9edf8f692341d89ed3645459da80be221c94eeec [file] [log] [blame]
Mark Brownd1c6b4f2009-07-28 15:22:02 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownd1c6b4f2009-07-28 15:22:02 +010023
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
37struct 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
49static 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
66static 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
75static 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
84static 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
102static 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
115static 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
139static 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
148static 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
157static 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
173static 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;
Roel Kluin9a767d42009-10-16 14:16:15 +0200179 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100180
181 ret = wm831x_reg_read(wm831x, on_reg);
182 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200183 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100184
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)
Roel Kluin9a767d42009-10-16 14:16:15 +0200190 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100191
192 if (ret & WM831X_LDO1_LP_MODE)
193 return REGULATOR_MODE_STANDBY;
194 else
195 return REGULATOR_MODE_IDLE;
196}
197
198static 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,
Axel Line2609992010-09-06 16:48:13 +0800218 WM831X_LDO1_LP_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100219 if (ret < 0)
220 return ret;
221
222 ret = wm831x_set_bits(wm831x, on_reg,
223 WM831X_LDO1_ON_MODE,
224 WM831X_LDO1_ON_MODE);
225 if (ret < 0)
226 return ret;
Axel Line2609992010-09-06 16:48:13 +0800227 break;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100228
229 case REGULATOR_MODE_STANDBY:
230 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800231 WM831X_LDO1_LP_MODE,
232 WM831X_LDO1_LP_MODE);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100233 if (ret < 0)
234 return ret;
235
236 ret = wm831x_set_bits(wm831x, on_reg,
237 WM831X_LDO1_ON_MODE,
238 WM831X_LDO1_ON_MODE);
239 if (ret < 0)
240 return ret;
241 break;
242
243 default:
244 return -EINVAL;
245 }
246
247 return 0;
248}
249
250static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
251{
252 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
253 struct wm831x *wm831x = ldo->wm831x;
254 int mask = 1 << rdev_get_id(rdev);
255 int ret;
256
257 /* Is the regulator on? */
258 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
259 if (ret < 0)
260 return ret;
261 if (!(ret & mask))
262 return REGULATOR_STATUS_OFF;
263
264 /* Is it reporting under voltage? */
265 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
266 if (ret & mask)
267 return REGULATOR_STATUS_ERROR;
268
269 ret = wm831x_gp_ldo_get_mode(rdev);
270 if (ret < 0)
271 return ret;
272 else
273 return regulator_mode_to_status(ret);
274}
275
276static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
277 int input_uV,
278 int output_uV, int load_uA)
279{
280 if (load_uA < 20000)
281 return REGULATOR_MODE_STANDBY;
282 if (load_uA < 50000)
283 return REGULATOR_MODE_IDLE;
284 return REGULATOR_MODE_NORMAL;
285}
286
287
288static struct regulator_ops wm831x_gp_ldo_ops = {
289 .list_voltage = wm831x_gp_ldo_list_voltage,
290 .get_voltage = wm831x_gp_ldo_get_voltage,
291 .set_voltage = wm831x_gp_ldo_set_voltage,
292 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
293 .get_mode = wm831x_gp_ldo_get_mode,
294 .set_mode = wm831x_gp_ldo_set_mode,
295 .get_status = wm831x_gp_ldo_get_status,
296 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
297
298 .is_enabled = wm831x_ldo_is_enabled,
299 .enable = wm831x_ldo_enable,
300 .disable = wm831x_ldo_disable,
301};
302
303static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
304{
305 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
306 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
307 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
308 struct wm831x_ldo *ldo;
309 struct resource *res;
310 int ret, irq;
311
312 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
313
314 if (pdata == NULL || pdata->ldo[id] == NULL)
315 return -ENODEV;
316
317 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
318 if (ldo == NULL) {
319 dev_err(&pdev->dev, "Unable to allocate private data\n");
320 return -ENOMEM;
321 }
322
323 ldo->wm831x = wm831x;
324
325 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
326 if (res == NULL) {
327 dev_err(&pdev->dev, "No I/O resource\n");
328 ret = -EINVAL;
329 goto err;
330 }
331 ldo->base = res->start;
332
333 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
334 ldo->desc.name = ldo->name;
335 ldo->desc.id = id;
336 ldo->desc.type = REGULATOR_VOLTAGE;
337 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
338 ldo->desc.ops = &wm831x_gp_ldo_ops;
339 ldo->desc.owner = THIS_MODULE;
340
341 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
342 pdata->ldo[id], ldo);
343 if (IS_ERR(ldo->regulator)) {
344 ret = PTR_ERR(ldo->regulator);
345 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
346 id + 1, ret);
347 goto err;
348 }
349
350 irq = platform_get_irq_byname(pdev, "UV");
351 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
352 IRQF_TRIGGER_RISING, ldo->name,
353 ldo);
354 if (ret != 0) {
355 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
356 irq, ret);
357 goto err_regulator;
358 }
359
360 platform_set_drvdata(pdev, ldo);
361
362 return 0;
363
364err_regulator:
365 regulator_unregister(ldo->regulator);
366err:
367 kfree(ldo);
368 return ret;
369}
370
371static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
372{
373 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
374 struct wm831x *wm831x = ldo->wm831x;
375
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800376 platform_set_drvdata(pdev, NULL);
377
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100378 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
379 regulator_unregister(ldo->regulator);
380 kfree(ldo);
381
382 return 0;
383}
384
385static struct platform_driver wm831x_gp_ldo_driver = {
386 .probe = wm831x_gp_ldo_probe,
387 .remove = __devexit_p(wm831x_gp_ldo_remove),
388 .driver = {
389 .name = "wm831x-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800390 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100391 },
392};
393
394/*
395 * Analogue LDOs
396 */
397
398
399#define WM831X_ALDO_SELECTOR_LOW 0xc
400#define WM831X_ALDO_MAX_SELECTOR 0x1f
401
402static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
403 unsigned int selector)
404{
405 /* 1-1.6V in 50mV steps */
406 if (selector <= WM831X_ALDO_SELECTOR_LOW)
407 return 1000000 + (selector * 50000);
408 /* 1.7-3.5V in 50mV steps */
409 if (selector <= WM831X_ALDO_MAX_SELECTOR)
410 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
411 * 100000);
412 return -EINVAL;
413}
414
415static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
416 int min_uV, int max_uV)
417{
418 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
419 struct wm831x *wm831x = ldo->wm831x;
420 int vsel, ret;
421
422 if (min_uV < 1000000)
423 vsel = 0;
424 else if (min_uV < 1700000)
425 vsel = ((min_uV - 1000000) / 50000);
426 else
427 vsel = ((min_uV - 1700000) / 100000)
428 + WM831X_ALDO_SELECTOR_LOW + 1;
429
430 ret = wm831x_aldo_list_voltage(rdev, vsel);
431 if (ret < 0)
432 return ret;
433 if (ret < min_uV || ret > max_uV)
434 return -EINVAL;
435
436 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
437}
438
439static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
440 int min_uV, int max_uV)
441{
442 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
443 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
444
445 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV);
446}
447
448static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
449 int uV)
450{
451 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
452 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
453
454 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV);
455}
456
457static int wm831x_aldo_get_voltage(struct regulator_dev *rdev)
458{
459 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
460 struct wm831x *wm831x = ldo->wm831x;
461 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
462 int ret;
463
464 ret = wm831x_reg_read(wm831x, reg);
465 if (ret < 0)
466 return ret;
467
468 ret &= WM831X_LDO7_ON_VSEL_MASK;
469
470 return wm831x_aldo_list_voltage(rdev, ret);
471}
472
473static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
474{
475 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
476 struct wm831x *wm831x = ldo->wm831x;
477 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100478 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100479
480 ret = wm831x_reg_read(wm831x, on_reg);
481 if (ret < 0)
482 return 0;
483
484 if (ret & WM831X_LDO7_ON_MODE)
485 return REGULATOR_MODE_IDLE;
486 else
487 return REGULATOR_MODE_NORMAL;
488}
489
490static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
491 unsigned int mode)
492{
493 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
494 struct wm831x *wm831x = ldo->wm831x;
495 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
496 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
497 int ret;
498
499
500 switch (mode) {
501 case REGULATOR_MODE_NORMAL:
502 ret = wm831x_set_bits(wm831x, on_reg,
503 WM831X_LDO7_ON_MODE, 0);
504 if (ret < 0)
505 return ret;
506 break;
507
508 case REGULATOR_MODE_IDLE:
509 ret = wm831x_set_bits(wm831x, ctrl_reg,
510 WM831X_LDO7_ON_MODE,
511 WM831X_LDO7_ON_MODE);
512 if (ret < 0)
513 return ret;
514 break;
515
516 default:
517 return -EINVAL;
518 }
519
520 return 0;
521}
522
523static int wm831x_aldo_get_status(struct regulator_dev *rdev)
524{
525 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
526 struct wm831x *wm831x = ldo->wm831x;
527 int mask = 1 << rdev_get_id(rdev);
528 int ret;
529
530 /* Is the regulator on? */
531 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
532 if (ret < 0)
533 return ret;
534 if (!(ret & mask))
535 return REGULATOR_STATUS_OFF;
536
537 /* Is it reporting under voltage? */
538 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
539 if (ret & mask)
540 return REGULATOR_STATUS_ERROR;
541
542 ret = wm831x_aldo_get_mode(rdev);
543 if (ret < 0)
544 return ret;
545 else
546 return regulator_mode_to_status(ret);
547}
548
549static struct regulator_ops wm831x_aldo_ops = {
550 .list_voltage = wm831x_aldo_list_voltage,
551 .get_voltage = wm831x_aldo_get_voltage,
552 .set_voltage = wm831x_aldo_set_voltage,
553 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
554 .get_mode = wm831x_aldo_get_mode,
555 .set_mode = wm831x_aldo_set_mode,
556 .get_status = wm831x_aldo_get_status,
557
558 .is_enabled = wm831x_ldo_is_enabled,
559 .enable = wm831x_ldo_enable,
560 .disable = wm831x_ldo_disable,
561};
562
563static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
564{
565 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
566 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
567 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
568 struct wm831x_ldo *ldo;
569 struct resource *res;
570 int ret, irq;
571
572 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
573
574 if (pdata == NULL || pdata->ldo[id] == NULL)
575 return -ENODEV;
576
577 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
578 if (ldo == NULL) {
579 dev_err(&pdev->dev, "Unable to allocate private data\n");
580 return -ENOMEM;
581 }
582
583 ldo->wm831x = wm831x;
584
585 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
586 if (res == NULL) {
587 dev_err(&pdev->dev, "No I/O resource\n");
588 ret = -EINVAL;
589 goto err;
590 }
591 ldo->base = res->start;
592
593 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
594 ldo->desc.name = ldo->name;
595 ldo->desc.id = id;
596 ldo->desc.type = REGULATOR_VOLTAGE;
597 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
598 ldo->desc.ops = &wm831x_aldo_ops;
599 ldo->desc.owner = THIS_MODULE;
600
601 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
602 pdata->ldo[id], ldo);
603 if (IS_ERR(ldo->regulator)) {
604 ret = PTR_ERR(ldo->regulator);
605 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
606 id + 1, ret);
607 goto err;
608 }
609
610 irq = platform_get_irq_byname(pdev, "UV");
611 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
612 IRQF_TRIGGER_RISING, ldo->name,
613 ldo);
614 if (ret != 0) {
615 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
616 irq, ret);
617 goto err_regulator;
618 }
619
620 platform_set_drvdata(pdev, ldo);
621
622 return 0;
623
624err_regulator:
625 regulator_unregister(ldo->regulator);
626err:
627 kfree(ldo);
628 return ret;
629}
630
631static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
632{
633 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
634 struct wm831x *wm831x = ldo->wm831x;
635
636 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
637 regulator_unregister(ldo->regulator);
638 kfree(ldo);
639
640 return 0;
641}
642
643static struct platform_driver wm831x_aldo_driver = {
644 .probe = wm831x_aldo_probe,
645 .remove = __devexit_p(wm831x_aldo_remove),
646 .driver = {
647 .name = "wm831x-aldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800648 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100649 },
650};
651
652/*
653 * Alive LDO
654 */
655
656#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
657
658static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
659 unsigned int selector)
660{
661 /* 0.8-1.55V in 50mV steps */
662 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
663 return 800000 + (selector * 50000);
664 return -EINVAL;
665}
666
667static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
668 int reg,
669 int min_uV, int max_uV)
670{
671 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
672 struct wm831x *wm831x = ldo->wm831x;
673 int vsel, ret;
674
675 vsel = (min_uV - 800000) / 50000;
676
677 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
678 if (ret < 0)
679 return ret;
680 if (ret < min_uV || ret > max_uV)
681 return -EINVAL;
682
683 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
684}
685
686static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
687 int min_uV, int max_uV)
688{
689 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
690 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
691
692 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
693}
694
695static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
696 int uV)
697{
698 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
699 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
700
701 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV);
702}
703
704static int wm831x_alive_ldo_get_voltage(struct regulator_dev *rdev)
705{
706 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
707 struct wm831x *wm831x = ldo->wm831x;
708 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
709 int ret;
710
711 ret = wm831x_reg_read(wm831x, reg);
712 if (ret < 0)
713 return ret;
714
715 ret &= WM831X_LDO11_ON_VSEL_MASK;
716
717 return wm831x_alive_ldo_list_voltage(rdev, ret);
718}
719
720static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
721{
722 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
723 struct wm831x *wm831x = ldo->wm831x;
724 int mask = 1 << rdev_get_id(rdev);
725 int ret;
726
727 /* Is the regulator on? */
728 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
729 if (ret < 0)
730 return ret;
731 if (ret & mask)
732 return REGULATOR_STATUS_ON;
733 else
734 return REGULATOR_STATUS_OFF;
735}
736
737static struct regulator_ops wm831x_alive_ldo_ops = {
738 .list_voltage = wm831x_alive_ldo_list_voltage,
739 .get_voltage = wm831x_alive_ldo_get_voltage,
740 .set_voltage = wm831x_alive_ldo_set_voltage,
741 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
742 .get_status = wm831x_alive_ldo_get_status,
743
744 .is_enabled = wm831x_ldo_is_enabled,
745 .enable = wm831x_ldo_enable,
746 .disable = wm831x_ldo_disable,
747};
748
749static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
750{
751 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
752 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
753 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
754 struct wm831x_ldo *ldo;
755 struct resource *res;
756 int ret;
757
758 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
759
760 if (pdata == NULL || pdata->ldo[id] == NULL)
761 return -ENODEV;
762
763 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
764 if (ldo == NULL) {
765 dev_err(&pdev->dev, "Unable to allocate private data\n");
766 return -ENOMEM;
767 }
768
769 ldo->wm831x = wm831x;
770
771 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
772 if (res == NULL) {
773 dev_err(&pdev->dev, "No I/O resource\n");
774 ret = -EINVAL;
775 goto err;
776 }
777 ldo->base = res->start;
778
779 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
780 ldo->desc.name = ldo->name;
781 ldo->desc.id = id;
782 ldo->desc.type = REGULATOR_VOLTAGE;
783 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
784 ldo->desc.ops = &wm831x_alive_ldo_ops;
785 ldo->desc.owner = THIS_MODULE;
786
787 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
788 pdata->ldo[id], ldo);
789 if (IS_ERR(ldo->regulator)) {
790 ret = PTR_ERR(ldo->regulator);
791 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
792 id + 1, ret);
793 goto err;
794 }
795
796 platform_set_drvdata(pdev, ldo);
797
798 return 0;
799
800err:
801 kfree(ldo);
802 return ret;
803}
804
805static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
806{
807 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
808
809 regulator_unregister(ldo->regulator);
810 kfree(ldo);
811
812 return 0;
813}
814
815static struct platform_driver wm831x_alive_ldo_driver = {
816 .probe = wm831x_alive_ldo_probe,
817 .remove = __devexit_p(wm831x_alive_ldo_remove),
818 .driver = {
819 .name = "wm831x-alive-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800820 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100821 },
822};
823
824static int __init wm831x_ldo_init(void)
825{
826 int ret;
827
828 ret = platform_driver_register(&wm831x_gp_ldo_driver);
829 if (ret != 0)
830 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
831
832 ret = platform_driver_register(&wm831x_aldo_driver);
833 if (ret != 0)
834 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
835
836 ret = platform_driver_register(&wm831x_alive_ldo_driver);
837 if (ret != 0)
838 pr_err("Failed to register WM831x alive LDO driver: %d\n",
839 ret);
840
841 return 0;
842}
843subsys_initcall(wm831x_ldo_init);
844
845static void __exit wm831x_ldo_exit(void)
846{
847 platform_driver_unregister(&wm831x_alive_ldo_driver);
848 platform_driver_unregister(&wm831x_aldo_driver);
849 platform_driver_unregister(&wm831x_gp_ldo_driver);
850}
851module_exit(wm831x_ldo_exit);
852
853/* Module information */
854MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
855MODULE_DESCRIPTION("WM831x LDO driver");
856MODULE_LICENSE("GPL");
857MODULE_ALIAS("platform:wm831x-ldo");
858MODULE_ALIAS("platform:wm831x-aldo");
859MODULE_ALIAS("platform:wm831x-aliveldo");