blob: f1e4ab0f9fda8e02d6857c705f8fc1032fc7f23b [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;
Mark Brown137a6352011-07-25 22:20:29 +0100313 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100314 struct wm831x_ldo *ldo;
315 struct resource *res;
316 int ret, irq;
317
Mark Brown137a6352011-07-25 22:20:29 +0100318 if (pdata && pdata->wm831x_num)
319 id = (pdata->wm831x_num * 10) + 1;
320 else
321 id = 0;
322 id = pdev->id - id;
323
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100324 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
325
326 if (pdata == NULL || pdata->ldo[id] == NULL)
327 return -ENODEV;
328
Mark Brownfded2f42011-12-15 02:11:14 +0800329 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100330 if (ldo == NULL) {
331 dev_err(&pdev->dev, "Unable to allocate private data\n");
332 return -ENOMEM;
333 }
334
335 ldo->wm831x = wm831x;
336
337 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
338 if (res == NULL) {
339 dev_err(&pdev->dev, "No I/O resource\n");
340 ret = -EINVAL;
341 goto err;
342 }
343 ldo->base = res->start;
344
345 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
346 ldo->desc.name = ldo->name;
347 ldo->desc.id = id;
348 ldo->desc.type = REGULATOR_VOLTAGE;
349 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
350 ldo->desc.ops = &wm831x_gp_ldo_ops;
351 ldo->desc.owner = THIS_MODULE;
352
353 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530354 pdata->ldo[id], ldo, NULL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100355 if (IS_ERR(ldo->regulator)) {
356 ret = PTR_ERR(ldo->regulator);
357 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
358 id + 1, ret);
359 goto err;
360 }
361
362 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000363 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
364 IRQF_TRIGGER_RISING, ldo->name,
365 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100366 if (ret != 0) {
367 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
368 irq, ret);
369 goto err_regulator;
370 }
371
372 platform_set_drvdata(pdev, ldo);
373
374 return 0;
375
376err_regulator:
377 regulator_unregister(ldo->regulator);
378err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100379 return ret;
380}
381
382static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
383{
384 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100385
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800386 platform_set_drvdata(pdev, NULL);
387
Mark Browndfda9c22011-03-01 16:50:43 +0000388 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100389 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100390
391 return 0;
392}
393
394static struct platform_driver wm831x_gp_ldo_driver = {
395 .probe = wm831x_gp_ldo_probe,
396 .remove = __devexit_p(wm831x_gp_ldo_remove),
397 .driver = {
398 .name = "wm831x-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800399 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100400 },
401};
402
403/*
404 * Analogue LDOs
405 */
406
407
408#define WM831X_ALDO_SELECTOR_LOW 0xc
409#define WM831X_ALDO_MAX_SELECTOR 0x1f
410
411static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
412 unsigned int selector)
413{
414 /* 1-1.6V in 50mV steps */
415 if (selector <= WM831X_ALDO_SELECTOR_LOW)
416 return 1000000 + (selector * 50000);
417 /* 1.7-3.5V in 50mV steps */
418 if (selector <= WM831X_ALDO_MAX_SELECTOR)
419 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
420 * 100000);
421 return -EINVAL;
422}
423
424static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000425 int min_uV, int max_uV,
426 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100427{
428 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
429 struct wm831x *wm831x = ldo->wm831x;
430 int vsel, ret;
431
432 if (min_uV < 1000000)
433 vsel = 0;
434 else if (min_uV < 1700000)
435 vsel = ((min_uV - 1000000) / 50000);
436 else
437 vsel = ((min_uV - 1700000) / 100000)
438 + WM831X_ALDO_SELECTOR_LOW + 1;
439
440 ret = wm831x_aldo_list_voltage(rdev, vsel);
441 if (ret < 0)
442 return ret;
443 if (ret < min_uV || ret > max_uV)
444 return -EINVAL;
445
Mark Brown3a93f2a2010-11-10 14:38:29 +0000446 *selector = vsel;
447
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100448 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
449}
450
451static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000452 int min_uV, int max_uV, unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100453{
454 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
455 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
456
Mark Brown3a93f2a2010-11-10 14:38:29 +0000457 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
458 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100459}
460
461static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
462 int uV)
463{
464 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
465 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000466 unsigned int selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100467
Mark Brown3a93f2a2010-11-10 14:38:29 +0000468 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100469}
470
Mark Brownafb8bb82010-12-10 17:28:08 +0000471static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100472{
473 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
474 struct wm831x *wm831x = ldo->wm831x;
475 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
476 int ret;
477
478 ret = wm831x_reg_read(wm831x, reg);
479 if (ret < 0)
480 return ret;
481
482 ret &= WM831X_LDO7_ON_VSEL_MASK;
483
Mark Brownafb8bb82010-12-10 17:28:08 +0000484 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100485}
486
487static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
488{
489 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
490 struct wm831x *wm831x = ldo->wm831x;
491 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100492 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100493
494 ret = wm831x_reg_read(wm831x, on_reg);
495 if (ret < 0)
496 return 0;
497
498 if (ret & WM831X_LDO7_ON_MODE)
499 return REGULATOR_MODE_IDLE;
500 else
501 return REGULATOR_MODE_NORMAL;
502}
503
504static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
505 unsigned int mode)
506{
507 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
508 struct wm831x *wm831x = ldo->wm831x;
509 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
510 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
511 int ret;
512
513
514 switch (mode) {
515 case REGULATOR_MODE_NORMAL:
516 ret = wm831x_set_bits(wm831x, on_reg,
517 WM831X_LDO7_ON_MODE, 0);
518 if (ret < 0)
519 return ret;
520 break;
521
522 case REGULATOR_MODE_IDLE:
523 ret = wm831x_set_bits(wm831x, ctrl_reg,
524 WM831X_LDO7_ON_MODE,
525 WM831X_LDO7_ON_MODE);
526 if (ret < 0)
527 return ret;
528 break;
529
530 default:
531 return -EINVAL;
532 }
533
534 return 0;
535}
536
537static int wm831x_aldo_get_status(struct regulator_dev *rdev)
538{
539 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
540 struct wm831x *wm831x = ldo->wm831x;
541 int mask = 1 << rdev_get_id(rdev);
542 int ret;
543
544 /* Is the regulator on? */
545 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
546 if (ret < 0)
547 return ret;
548 if (!(ret & mask))
549 return REGULATOR_STATUS_OFF;
550
551 /* Is it reporting under voltage? */
552 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
553 if (ret & mask)
554 return REGULATOR_STATUS_ERROR;
555
556 ret = wm831x_aldo_get_mode(rdev);
557 if (ret < 0)
558 return ret;
559 else
560 return regulator_mode_to_status(ret);
561}
562
563static struct regulator_ops wm831x_aldo_ops = {
564 .list_voltage = wm831x_aldo_list_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000565 .get_voltage_sel = wm831x_aldo_get_voltage_sel,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100566 .set_voltage = wm831x_aldo_set_voltage,
567 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
568 .get_mode = wm831x_aldo_get_mode,
569 .set_mode = wm831x_aldo_set_mode,
570 .get_status = wm831x_aldo_get_status,
571
572 .is_enabled = wm831x_ldo_is_enabled,
573 .enable = wm831x_ldo_enable,
574 .disable = wm831x_ldo_disable,
575};
576
577static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
578{
579 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
580 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
Mark Brown137a6352011-07-25 22:20:29 +0100581 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100582 struct wm831x_ldo *ldo;
583 struct resource *res;
584 int ret, irq;
585
Mark Brown137a6352011-07-25 22:20:29 +0100586 if (pdata && pdata->wm831x_num)
587 id = (pdata->wm831x_num * 10) + 1;
588 else
589 id = 0;
590 id = pdev->id - id;
591
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100592 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
593
594 if (pdata == NULL || pdata->ldo[id] == NULL)
595 return -ENODEV;
596
Mark Brownfded2f42011-12-15 02:11:14 +0800597 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100598 if (ldo == NULL) {
599 dev_err(&pdev->dev, "Unable to allocate private data\n");
600 return -ENOMEM;
601 }
602
603 ldo->wm831x = wm831x;
604
605 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
606 if (res == NULL) {
607 dev_err(&pdev->dev, "No I/O resource\n");
608 ret = -EINVAL;
609 goto err;
610 }
611 ldo->base = res->start;
612
613 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
614 ldo->desc.name = ldo->name;
615 ldo->desc.id = id;
616 ldo->desc.type = REGULATOR_VOLTAGE;
617 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
618 ldo->desc.ops = &wm831x_aldo_ops;
619 ldo->desc.owner = THIS_MODULE;
620
621 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530622 pdata->ldo[id], ldo, NULL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100623 if (IS_ERR(ldo->regulator)) {
624 ret = PTR_ERR(ldo->regulator);
625 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
626 id + 1, ret);
627 goto err;
628 }
629
630 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000631 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
632 IRQF_TRIGGER_RISING, ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100633 if (ret != 0) {
634 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
635 irq, ret);
636 goto err_regulator;
637 }
638
639 platform_set_drvdata(pdev, ldo);
640
641 return 0;
642
643err_regulator:
644 regulator_unregister(ldo->regulator);
645err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100646 return ret;
647}
648
649static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
650{
651 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100652
Mark Browndfda9c22011-03-01 16:50:43 +0000653 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100654 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100655
656 return 0;
657}
658
659static struct platform_driver wm831x_aldo_driver = {
660 .probe = wm831x_aldo_probe,
661 .remove = __devexit_p(wm831x_aldo_remove),
662 .driver = {
663 .name = "wm831x-aldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800664 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100665 },
666};
667
668/*
669 * Alive LDO
670 */
671
672#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
673
674static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
675 unsigned int selector)
676{
677 /* 0.8-1.55V in 50mV steps */
678 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
679 return 800000 + (selector * 50000);
680 return -EINVAL;
681}
682
683static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
684 int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000685 int min_uV, int max_uV,
686 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100687{
688 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
689 struct wm831x *wm831x = ldo->wm831x;
690 int vsel, ret;
691
692 vsel = (min_uV - 800000) / 50000;
693
694 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
695 if (ret < 0)
696 return ret;
697 if (ret < min_uV || ret > max_uV)
698 return -EINVAL;
699
Mark Brown3a93f2a2010-11-10 14:38:29 +0000700 *selector = vsel;
701
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100702 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
703}
704
705static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000706 int min_uV, int max_uV,
707 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100708{
709 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
710 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
711
Mark Brown3a93f2a2010-11-10 14:38:29 +0000712 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
713 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100714}
715
716static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
717 int uV)
718{
719 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
720 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000721 unsigned selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100722
Mark Brown3a93f2a2010-11-10 14:38:29 +0000723 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100724}
725
Mark Brownafb8bb82010-12-10 17:28:08 +0000726static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100727{
728 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
729 struct wm831x *wm831x = ldo->wm831x;
730 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
731 int ret;
732
733 ret = wm831x_reg_read(wm831x, reg);
734 if (ret < 0)
735 return ret;
736
737 ret &= WM831X_LDO11_ON_VSEL_MASK;
738
Mark Brownafb8bb82010-12-10 17:28:08 +0000739 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100740}
741
742static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
743{
744 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
745 struct wm831x *wm831x = ldo->wm831x;
746 int mask = 1 << rdev_get_id(rdev);
747 int ret;
748
749 /* Is the regulator on? */
750 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
751 if (ret < 0)
752 return ret;
753 if (ret & mask)
754 return REGULATOR_STATUS_ON;
755 else
756 return REGULATOR_STATUS_OFF;
757}
758
759static struct regulator_ops wm831x_alive_ldo_ops = {
760 .list_voltage = wm831x_alive_ldo_list_voltage,
Mark Brownafb8bb82010-12-10 17:28:08 +0000761 .get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100762 .set_voltage = wm831x_alive_ldo_set_voltage,
763 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
764 .get_status = wm831x_alive_ldo_get_status,
765
766 .is_enabled = wm831x_ldo_is_enabled,
767 .enable = wm831x_ldo_enable,
768 .disable = wm831x_ldo_disable,
769};
770
771static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
772{
773 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
774 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
Mark Brown137a6352011-07-25 22:20:29 +0100775 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100776 struct wm831x_ldo *ldo;
777 struct resource *res;
778 int ret;
779
Mark Brown137a6352011-07-25 22:20:29 +0100780 if (pdata && pdata->wm831x_num)
781 id = (pdata->wm831x_num * 10) + 1;
782 else
783 id = 0;
784 id = pdev->id - id;
785
786
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100787 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
788
789 if (pdata == NULL || pdata->ldo[id] == NULL)
790 return -ENODEV;
791
Mark Brownfded2f42011-12-15 02:11:14 +0800792 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100793 if (ldo == NULL) {
794 dev_err(&pdev->dev, "Unable to allocate private data\n");
795 return -ENOMEM;
796 }
797
798 ldo->wm831x = wm831x;
799
800 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
801 if (res == NULL) {
802 dev_err(&pdev->dev, "No I/O resource\n");
803 ret = -EINVAL;
804 goto err;
805 }
806 ldo->base = res->start;
807
808 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
809 ldo->desc.name = ldo->name;
810 ldo->desc.id = id;
811 ldo->desc.type = REGULATOR_VOLTAGE;
812 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
813 ldo->desc.ops = &wm831x_alive_ldo_ops;
814 ldo->desc.owner = THIS_MODULE;
815
816 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530817 pdata->ldo[id], ldo, NULL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100818 if (IS_ERR(ldo->regulator)) {
819 ret = PTR_ERR(ldo->regulator);
820 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
821 id + 1, ret);
822 goto err;
823 }
824
825 platform_set_drvdata(pdev, ldo);
826
827 return 0;
828
829err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100830 return ret;
831}
832
833static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
834{
835 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
836
837 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100838
839 return 0;
840}
841
842static struct platform_driver wm831x_alive_ldo_driver = {
843 .probe = wm831x_alive_ldo_probe,
844 .remove = __devexit_p(wm831x_alive_ldo_remove),
845 .driver = {
846 .name = "wm831x-alive-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800847 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100848 },
849};
850
851static int __init wm831x_ldo_init(void)
852{
853 int ret;
854
855 ret = platform_driver_register(&wm831x_gp_ldo_driver);
856 if (ret != 0)
857 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
858
859 ret = platform_driver_register(&wm831x_aldo_driver);
860 if (ret != 0)
861 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
862
863 ret = platform_driver_register(&wm831x_alive_ldo_driver);
864 if (ret != 0)
865 pr_err("Failed to register WM831x alive LDO driver: %d\n",
866 ret);
867
868 return 0;
869}
870subsys_initcall(wm831x_ldo_init);
871
872static void __exit wm831x_ldo_exit(void)
873{
874 platform_driver_unregister(&wm831x_alive_ldo_driver);
875 platform_driver_unregister(&wm831x_aldo_driver);
876 platform_driver_unregister(&wm831x_gp_ldo_driver);
877}
878module_exit(wm831x_ldo_exit);
879
880/* Module information */
881MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
882MODULE_DESCRIPTION("WM831x LDO driver");
883MODULE_LICENSE("GPL");
884MODULE_ALIAS("platform:wm831x-ldo");
885MODULE_ALIAS("platform:wm831x-aldo");
886MODULE_ALIAS("platform:wm831x-aliveldo");