blob: 2220cf8defb1625a1c161f026cf5722fd7356ea2 [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,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000116 int min_uV, int max_uV,
117 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100118{
119 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
120 struct wm831x *wm831x = ldo->wm831x;
121 int vsel, ret;
122
123 if (min_uV < 900000)
124 vsel = 0;
125 else if (min_uV < 1700000)
126 vsel = ((min_uV - 900000) / 50000);
127 else
128 vsel = ((min_uV - 1700000) / 100000)
129 + WM831X_GP_LDO_SELECTOR_LOW + 1;
130
131 ret = wm831x_gp_ldo_list_voltage(rdev, vsel);
132 if (ret < 0)
133 return ret;
134 if (ret < min_uV || ret > max_uV)
135 return -EINVAL;
136
Mark Brown3a93f2a2010-11-10 14:38:29 +0000137 *selector = vsel;
138
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100139 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
140}
141
142static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000143 int min_uV, int max_uV,
144 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100145{
146 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
147 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
148
Mark Brown3a93f2a2010-11-10 14:38:29 +0000149 return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
150 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100151}
152
153static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
154 int uV)
155{
156 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
157 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000158 unsigned int selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100159
Mark Brown3a93f2a2010-11-10 14:38:29 +0000160 return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100161}
162
Mark Brownafb8bb82010-12-10 17:28:08 +0000163static int wm831x_gp_ldo_get_voltage_sel(struct regulator_dev *rdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100164{
165 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
166 struct wm831x *wm831x = ldo->wm831x;
167 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
168 int ret;
169
170 ret = wm831x_reg_read(wm831x, reg);
171 if (ret < 0)
172 return ret;
173
174 ret &= WM831X_LDO1_ON_VSEL_MASK;
175
Mark Brownafb8bb82010-12-10 17:28:08 +0000176 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100177}
178
179static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
180{
181 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
182 struct wm831x *wm831x = ldo->wm831x;
183 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
184 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin9a767d42009-10-16 14:16:15 +0200185 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100186
187 ret = wm831x_reg_read(wm831x, on_reg);
188 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200189 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100190
191 if (!(ret & WM831X_LDO1_ON_MODE))
192 return REGULATOR_MODE_NORMAL;
193
194 ret = wm831x_reg_read(wm831x, ctrl_reg);
195 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200196 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100197
198 if (ret & WM831X_LDO1_LP_MODE)
199 return REGULATOR_MODE_STANDBY;
200 else
201 return REGULATOR_MODE_IDLE;
202}
203
204static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
205 unsigned int mode)
206{
207 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
208 struct wm831x *wm831x = ldo->wm831x;
209 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
210 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
211 int ret;
212
213
214 switch (mode) {
215 case REGULATOR_MODE_NORMAL:
216 ret = wm831x_set_bits(wm831x, on_reg,
217 WM831X_LDO1_ON_MODE, 0);
218 if (ret < 0)
219 return ret;
220 break;
221
222 case REGULATOR_MODE_IDLE:
223 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800224 WM831X_LDO1_LP_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100225 if (ret < 0)
226 return ret;
227
228 ret = wm831x_set_bits(wm831x, on_reg,
229 WM831X_LDO1_ON_MODE,
230 WM831X_LDO1_ON_MODE);
231 if (ret < 0)
232 return ret;
Axel Line2609992010-09-06 16:48:13 +0800233 break;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100234
235 case REGULATOR_MODE_STANDBY:
236 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800237 WM831X_LDO1_LP_MODE,
238 WM831X_LDO1_LP_MODE);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100239 if (ret < 0)
240 return ret;
241
242 ret = wm831x_set_bits(wm831x, on_reg,
243 WM831X_LDO1_ON_MODE,
244 WM831X_LDO1_ON_MODE);
245 if (ret < 0)
246 return ret;
247 break;
248
249 default:
250 return -EINVAL;
251 }
252
253 return 0;
254}
255
256static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
257{
258 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
259 struct wm831x *wm831x = ldo->wm831x;
260 int mask = 1 << rdev_get_id(rdev);
261 int ret;
262
263 /* Is the regulator on? */
264 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
265 if (ret < 0)
266 return ret;
267 if (!(ret & mask))
268 return REGULATOR_STATUS_OFF;
269
270 /* Is it reporting under voltage? */
271 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
272 if (ret & mask)
273 return REGULATOR_STATUS_ERROR;
274
275 ret = wm831x_gp_ldo_get_mode(rdev);
276 if (ret < 0)
277 return ret;
278 else
279 return regulator_mode_to_status(ret);
280}
281
282static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
283 int input_uV,
284 int output_uV, int load_uA)
285{
286 if (load_uA < 20000)
287 return REGULATOR_MODE_STANDBY;
288 if (load_uA < 50000)
289 return REGULATOR_MODE_IDLE;
290 return REGULATOR_MODE_NORMAL;
291}
292
293
294static struct regulator_ops wm831x_gp_ldo_ops = {
295 .list_voltage = wm831x_gp_ldo_list_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000296 .get_voltage_sel = wm831x_gp_ldo_get_voltage_sel,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100297 .set_voltage = wm831x_gp_ldo_set_voltage,
298 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
299 .get_mode = wm831x_gp_ldo_get_mode,
300 .set_mode = wm831x_gp_ldo_set_mode,
301 .get_status = wm831x_gp_ldo_get_status,
302 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
303
304 .is_enabled = wm831x_ldo_is_enabled,
305 .enable = wm831x_ldo_enable,
306 .disable = wm831x_ldo_disable,
307};
308
309static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
310{
311 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
312 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
313 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
314 struct wm831x_ldo *ldo;
315 struct resource *res;
316 int ret, irq;
317
318 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
319
320 if (pdata == NULL || pdata->ldo[id] == NULL)
321 return -ENODEV;
322
323 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
324 if (ldo == NULL) {
325 dev_err(&pdev->dev, "Unable to allocate private data\n");
326 return -ENOMEM;
327 }
328
329 ldo->wm831x = wm831x;
330
331 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
332 if (res == NULL) {
333 dev_err(&pdev->dev, "No I/O resource\n");
334 ret = -EINVAL;
335 goto err;
336 }
337 ldo->base = res->start;
338
339 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
340 ldo->desc.name = ldo->name;
341 ldo->desc.id = id;
342 ldo->desc.type = REGULATOR_VOLTAGE;
343 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
344 ldo->desc.ops = &wm831x_gp_ldo_ops;
345 ldo->desc.owner = THIS_MODULE;
346
347 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
348 pdata->ldo[id], ldo);
349 if (IS_ERR(ldo->regulator)) {
350 ret = PTR_ERR(ldo->regulator);
351 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
352 id + 1, ret);
353 goto err;
354 }
355
356 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000357 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
358 IRQF_TRIGGER_RISING, ldo->name,
359 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100360 if (ret != 0) {
361 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
362 irq, ret);
363 goto err_regulator;
364 }
365
366 platform_set_drvdata(pdev, ldo);
367
368 return 0;
369
370err_regulator:
371 regulator_unregister(ldo->regulator);
372err:
373 kfree(ldo);
374 return ret;
375}
376
377static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
378{
379 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100380
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800381 platform_set_drvdata(pdev, NULL);
382
Mark Browndfda9c22011-03-01 16:50:43 +0000383 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100384 regulator_unregister(ldo->regulator);
385 kfree(ldo);
386
387 return 0;
388}
389
390static struct platform_driver wm831x_gp_ldo_driver = {
391 .probe = wm831x_gp_ldo_probe,
392 .remove = __devexit_p(wm831x_gp_ldo_remove),
393 .driver = {
394 .name = "wm831x-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800395 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100396 },
397};
398
399/*
400 * Analogue LDOs
401 */
402
403
404#define WM831X_ALDO_SELECTOR_LOW 0xc
405#define WM831X_ALDO_MAX_SELECTOR 0x1f
406
407static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
408 unsigned int selector)
409{
410 /* 1-1.6V in 50mV steps */
411 if (selector <= WM831X_ALDO_SELECTOR_LOW)
412 return 1000000 + (selector * 50000);
413 /* 1.7-3.5V in 50mV steps */
414 if (selector <= WM831X_ALDO_MAX_SELECTOR)
415 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
416 * 100000);
417 return -EINVAL;
418}
419
420static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000421 int min_uV, int max_uV,
422 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100423{
424 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
425 struct wm831x *wm831x = ldo->wm831x;
426 int vsel, ret;
427
428 if (min_uV < 1000000)
429 vsel = 0;
430 else if (min_uV < 1700000)
431 vsel = ((min_uV - 1000000) / 50000);
432 else
433 vsel = ((min_uV - 1700000) / 100000)
434 + WM831X_ALDO_SELECTOR_LOW + 1;
435
436 ret = wm831x_aldo_list_voltage(rdev, vsel);
437 if (ret < 0)
438 return ret;
439 if (ret < min_uV || ret > max_uV)
440 return -EINVAL;
441
Mark Brown3a93f2a2010-11-10 14:38:29 +0000442 *selector = vsel;
443
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100444 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
445}
446
447static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000448 int min_uV, int max_uV, unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100449{
450 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
451 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
452
Mark Brown3a93f2a2010-11-10 14:38:29 +0000453 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
454 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100455}
456
457static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
458 int uV)
459{
460 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
461 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000462 unsigned int selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100463
Mark Brown3a93f2a2010-11-10 14:38:29 +0000464 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100465}
466
Mark Brownafb8bb82010-12-10 17:28:08 +0000467static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100468{
469 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
470 struct wm831x *wm831x = ldo->wm831x;
471 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
472 int ret;
473
474 ret = wm831x_reg_read(wm831x, reg);
475 if (ret < 0)
476 return ret;
477
478 ret &= WM831X_LDO7_ON_VSEL_MASK;
479
Mark Brownafb8bb82010-12-10 17:28:08 +0000480 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100481}
482
483static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
484{
485 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
486 struct wm831x *wm831x = ldo->wm831x;
487 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100488 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100489
490 ret = wm831x_reg_read(wm831x, on_reg);
491 if (ret < 0)
492 return 0;
493
494 if (ret & WM831X_LDO7_ON_MODE)
495 return REGULATOR_MODE_IDLE;
496 else
497 return REGULATOR_MODE_NORMAL;
498}
499
500static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
501 unsigned int mode)
502{
503 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
504 struct wm831x *wm831x = ldo->wm831x;
505 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
506 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
507 int ret;
508
509
510 switch (mode) {
511 case REGULATOR_MODE_NORMAL:
512 ret = wm831x_set_bits(wm831x, on_reg,
513 WM831X_LDO7_ON_MODE, 0);
514 if (ret < 0)
515 return ret;
516 break;
517
518 case REGULATOR_MODE_IDLE:
519 ret = wm831x_set_bits(wm831x, ctrl_reg,
520 WM831X_LDO7_ON_MODE,
521 WM831X_LDO7_ON_MODE);
522 if (ret < 0)
523 return ret;
524 break;
525
526 default:
527 return -EINVAL;
528 }
529
530 return 0;
531}
532
533static int wm831x_aldo_get_status(struct regulator_dev *rdev)
534{
535 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
536 struct wm831x *wm831x = ldo->wm831x;
537 int mask = 1 << rdev_get_id(rdev);
538 int ret;
539
540 /* Is the regulator on? */
541 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
542 if (ret < 0)
543 return ret;
544 if (!(ret & mask))
545 return REGULATOR_STATUS_OFF;
546
547 /* Is it reporting under voltage? */
548 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
549 if (ret & mask)
550 return REGULATOR_STATUS_ERROR;
551
552 ret = wm831x_aldo_get_mode(rdev);
553 if (ret < 0)
554 return ret;
555 else
556 return regulator_mode_to_status(ret);
557}
558
559static struct regulator_ops wm831x_aldo_ops = {
560 .list_voltage = wm831x_aldo_list_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000561 .get_voltage_sel = wm831x_aldo_get_voltage_sel,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100562 .set_voltage = wm831x_aldo_set_voltage,
563 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
564 .get_mode = wm831x_aldo_get_mode,
565 .set_mode = wm831x_aldo_set_mode,
566 .get_status = wm831x_aldo_get_status,
567
568 .is_enabled = wm831x_ldo_is_enabled,
569 .enable = wm831x_ldo_enable,
570 .disable = wm831x_ldo_disable,
571};
572
573static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
574{
575 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
576 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
577 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
578 struct wm831x_ldo *ldo;
579 struct resource *res;
580 int ret, irq;
581
582 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
583
584 if (pdata == NULL || pdata->ldo[id] == NULL)
585 return -ENODEV;
586
587 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
588 if (ldo == NULL) {
589 dev_err(&pdev->dev, "Unable to allocate private data\n");
590 return -ENOMEM;
591 }
592
593 ldo->wm831x = wm831x;
594
595 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
596 if (res == NULL) {
597 dev_err(&pdev->dev, "No I/O resource\n");
598 ret = -EINVAL;
599 goto err;
600 }
601 ldo->base = res->start;
602
603 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
604 ldo->desc.name = ldo->name;
605 ldo->desc.id = id;
606 ldo->desc.type = REGULATOR_VOLTAGE;
607 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
608 ldo->desc.ops = &wm831x_aldo_ops;
609 ldo->desc.owner = THIS_MODULE;
610
611 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
612 pdata->ldo[id], ldo);
613 if (IS_ERR(ldo->regulator)) {
614 ret = PTR_ERR(ldo->regulator);
615 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
616 id + 1, ret);
617 goto err;
618 }
619
620 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000621 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
622 IRQF_TRIGGER_RISING, ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100623 if (ret != 0) {
624 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
625 irq, ret);
626 goto err_regulator;
627 }
628
629 platform_set_drvdata(pdev, ldo);
630
631 return 0;
632
633err_regulator:
634 regulator_unregister(ldo->regulator);
635err:
636 kfree(ldo);
637 return ret;
638}
639
640static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
641{
642 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100643
Mark Browndfda9c22011-03-01 16:50:43 +0000644 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100645 regulator_unregister(ldo->regulator);
646 kfree(ldo);
647
648 return 0;
649}
650
651static struct platform_driver wm831x_aldo_driver = {
652 .probe = wm831x_aldo_probe,
653 .remove = __devexit_p(wm831x_aldo_remove),
654 .driver = {
655 .name = "wm831x-aldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800656 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100657 },
658};
659
660/*
661 * Alive LDO
662 */
663
664#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
665
666static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
667 unsigned int selector)
668{
669 /* 0.8-1.55V in 50mV steps */
670 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
671 return 800000 + (selector * 50000);
672 return -EINVAL;
673}
674
675static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
676 int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000677 int min_uV, int max_uV,
678 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100679{
680 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
681 struct wm831x *wm831x = ldo->wm831x;
682 int vsel, ret;
683
684 vsel = (min_uV - 800000) / 50000;
685
686 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
687 if (ret < 0)
688 return ret;
689 if (ret < min_uV || ret > max_uV)
690 return -EINVAL;
691
Mark Brown3a93f2a2010-11-10 14:38:29 +0000692 *selector = vsel;
693
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100694 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
695}
696
697static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000698 int min_uV, int max_uV,
699 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100700{
701 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
702 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
703
Mark Brown3a93f2a2010-11-10 14:38:29 +0000704 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
705 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100706}
707
708static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
709 int uV)
710{
711 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
712 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000713 unsigned selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100714
Mark Brown3a93f2a2010-11-10 14:38:29 +0000715 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100716}
717
Mark Brownafb8bb82010-12-10 17:28:08 +0000718static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100719{
720 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
721 struct wm831x *wm831x = ldo->wm831x;
722 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
723 int ret;
724
725 ret = wm831x_reg_read(wm831x, reg);
726 if (ret < 0)
727 return ret;
728
729 ret &= WM831X_LDO11_ON_VSEL_MASK;
730
Mark Brownafb8bb82010-12-10 17:28:08 +0000731 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100732}
733
734static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
735{
736 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
737 struct wm831x *wm831x = ldo->wm831x;
738 int mask = 1 << rdev_get_id(rdev);
739 int ret;
740
741 /* Is the regulator on? */
742 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
743 if (ret < 0)
744 return ret;
745 if (ret & mask)
746 return REGULATOR_STATUS_ON;
747 else
748 return REGULATOR_STATUS_OFF;
749}
750
751static struct regulator_ops wm831x_alive_ldo_ops = {
752 .list_voltage = wm831x_alive_ldo_list_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000753 .get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100754 .set_voltage = wm831x_alive_ldo_set_voltage,
755 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
756 .get_status = wm831x_alive_ldo_get_status,
757
758 .is_enabled = wm831x_ldo_is_enabled,
759 .enable = wm831x_ldo_enable,
760 .disable = wm831x_ldo_disable,
761};
762
763static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
764{
765 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
766 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
767 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
768 struct wm831x_ldo *ldo;
769 struct resource *res;
770 int ret;
771
772 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
773
774 if (pdata == NULL || pdata->ldo[id] == NULL)
775 return -ENODEV;
776
777 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
778 if (ldo == NULL) {
779 dev_err(&pdev->dev, "Unable to allocate private data\n");
780 return -ENOMEM;
781 }
782
783 ldo->wm831x = wm831x;
784
785 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
786 if (res == NULL) {
787 dev_err(&pdev->dev, "No I/O resource\n");
788 ret = -EINVAL;
789 goto err;
790 }
791 ldo->base = res->start;
792
793 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
794 ldo->desc.name = ldo->name;
795 ldo->desc.id = id;
796 ldo->desc.type = REGULATOR_VOLTAGE;
797 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
798 ldo->desc.ops = &wm831x_alive_ldo_ops;
799 ldo->desc.owner = THIS_MODULE;
800
801 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
802 pdata->ldo[id], ldo);
803 if (IS_ERR(ldo->regulator)) {
804 ret = PTR_ERR(ldo->regulator);
805 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
806 id + 1, ret);
807 goto err;
808 }
809
810 platform_set_drvdata(pdev, ldo);
811
812 return 0;
813
814err:
815 kfree(ldo);
816 return ret;
817}
818
819static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
820{
821 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
822
823 regulator_unregister(ldo->regulator);
824 kfree(ldo);
825
826 return 0;
827}
828
829static struct platform_driver wm831x_alive_ldo_driver = {
830 .probe = wm831x_alive_ldo_probe,
831 .remove = __devexit_p(wm831x_alive_ldo_remove),
832 .driver = {
833 .name = "wm831x-alive-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800834 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100835 },
836};
837
838static int __init wm831x_ldo_init(void)
839{
840 int ret;
841
842 ret = platform_driver_register(&wm831x_gp_ldo_driver);
843 if (ret != 0)
844 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
845
846 ret = platform_driver_register(&wm831x_aldo_driver);
847 if (ret != 0)
848 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
849
850 ret = platform_driver_register(&wm831x_alive_ldo_driver);
851 if (ret != 0)
852 pr_err("Failed to register WM831x alive LDO driver: %d\n",
853 ret);
854
855 return 0;
856}
857subsys_initcall(wm831x_ldo_init);
858
859static void __exit wm831x_ldo_exit(void)
860{
861 platform_driver_unregister(&wm831x_alive_ldo_driver);
862 platform_driver_unregister(&wm831x_aldo_driver);
863 platform_driver_unregister(&wm831x_gp_ldo_driver);
864}
865module_exit(wm831x_ldo_exit);
866
867/* Module information */
868MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
869MODULE_DESCRIPTION("WM831x LDO driver");
870MODULE_LICENSE("GPL");
871MODULE_ALIAS("platform:wm831x-ldo");
872MODULE_ALIAS("platform:wm831x-aldo");
873MODULE_ALIAS("platform:wm831x-aliveldo");