blob: 364b7d8ac54ddc2ad036ecc81116b227bc2d2dcc [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);
Axel Lin6085d4d2012-03-29 15:04:22 +0800108 /* 1.7-3.3V in 100mV steps */
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100109 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 Brownd1c6b4f2009-07-28 15:22:02 +0100163static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
164{
165 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
166 struct wm831x *wm831x = ldo->wm831x;
167 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
168 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin9a767d42009-10-16 14:16:15 +0200169 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100170
171 ret = wm831x_reg_read(wm831x, on_reg);
172 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200173 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100174
175 if (!(ret & WM831X_LDO1_ON_MODE))
176 return REGULATOR_MODE_NORMAL;
177
178 ret = wm831x_reg_read(wm831x, ctrl_reg);
179 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200180 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100181
182 if (ret & WM831X_LDO1_LP_MODE)
183 return REGULATOR_MODE_STANDBY;
184 else
185 return REGULATOR_MODE_IDLE;
186}
187
188static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
189 unsigned int mode)
190{
191 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
192 struct wm831x *wm831x = ldo->wm831x;
193 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
194 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
195 int ret;
196
197
198 switch (mode) {
199 case REGULATOR_MODE_NORMAL:
200 ret = wm831x_set_bits(wm831x, on_reg,
201 WM831X_LDO1_ON_MODE, 0);
202 if (ret < 0)
203 return ret;
204 break;
205
206 case REGULATOR_MODE_IDLE:
207 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800208 WM831X_LDO1_LP_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100209 if (ret < 0)
210 return ret;
211
212 ret = wm831x_set_bits(wm831x, on_reg,
213 WM831X_LDO1_ON_MODE,
214 WM831X_LDO1_ON_MODE);
215 if (ret < 0)
216 return ret;
Axel Line2609992010-09-06 16:48:13 +0800217 break;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100218
219 case REGULATOR_MODE_STANDBY:
220 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800221 WM831X_LDO1_LP_MODE,
222 WM831X_LDO1_LP_MODE);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100223 if (ret < 0)
224 return ret;
225
226 ret = wm831x_set_bits(wm831x, on_reg,
227 WM831X_LDO1_ON_MODE,
228 WM831X_LDO1_ON_MODE);
229 if (ret < 0)
230 return ret;
231 break;
232
233 default:
234 return -EINVAL;
235 }
236
237 return 0;
238}
239
240static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
241{
242 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
243 struct wm831x *wm831x = ldo->wm831x;
244 int mask = 1 << rdev_get_id(rdev);
245 int ret;
246
247 /* Is the regulator on? */
248 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
249 if (ret < 0)
250 return ret;
251 if (!(ret & mask))
252 return REGULATOR_STATUS_OFF;
253
254 /* Is it reporting under voltage? */
255 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
256 if (ret & mask)
257 return REGULATOR_STATUS_ERROR;
258
259 ret = wm831x_gp_ldo_get_mode(rdev);
260 if (ret < 0)
261 return ret;
262 else
263 return regulator_mode_to_status(ret);
264}
265
266static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
267 int input_uV,
268 int output_uV, int load_uA)
269{
270 if (load_uA < 20000)
271 return REGULATOR_MODE_STANDBY;
272 if (load_uA < 50000)
273 return REGULATOR_MODE_IDLE;
274 return REGULATOR_MODE_NORMAL;
275}
276
277
278static struct regulator_ops wm831x_gp_ldo_ops = {
279 .list_voltage = wm831x_gp_ldo_list_voltage,
Mark Brownac663b42012-04-15 11:55:55 +0100280 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100281 .set_voltage = wm831x_gp_ldo_set_voltage,
282 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
283 .get_mode = wm831x_gp_ldo_get_mode,
284 .set_mode = wm831x_gp_ldo_set_mode,
285 .get_status = wm831x_gp_ldo_get_status,
286 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
287
288 .is_enabled = wm831x_ldo_is_enabled,
289 .enable = wm831x_ldo_enable,
290 .disable = wm831x_ldo_disable,
291};
292
293static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
294{
295 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
296 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
Mark Brownc172708d2012-04-04 00:50:22 +0100297 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100298 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100299 struct wm831x_ldo *ldo;
300 struct resource *res;
301 int ret, irq;
302
Mark Brown137a6352011-07-25 22:20:29 +0100303 if (pdata && pdata->wm831x_num)
304 id = (pdata->wm831x_num * 10) + 1;
305 else
306 id = 0;
307 id = pdev->id - id;
308
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100309 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
310
311 if (pdata == NULL || pdata->ldo[id] == NULL)
312 return -ENODEV;
313
Mark Brownfded2f42011-12-15 02:11:14 +0800314 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100315 if (ldo == NULL) {
316 dev_err(&pdev->dev, "Unable to allocate private data\n");
317 return -ENOMEM;
318 }
319
320 ldo->wm831x = wm831x;
321
322 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
323 if (res == NULL) {
324 dev_err(&pdev->dev, "No I/O resource\n");
325 ret = -EINVAL;
326 goto err;
327 }
328 ldo->base = res->start;
329
330 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
331 ldo->desc.name = ldo->name;
332 ldo->desc.id = id;
333 ldo->desc.type = REGULATOR_VOLTAGE;
334 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
335 ldo->desc.ops = &wm831x_gp_ldo_ops;
336 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100337 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
338 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100339
Mark Brownc172708d2012-04-04 00:50:22 +0100340 config.dev = pdev->dev.parent;
341 config.init_data = pdata->ldo[id];
342 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100343 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100344
345 ldo->regulator = regulator_register(&ldo->desc, &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100346 if (IS_ERR(ldo->regulator)) {
347 ret = PTR_ERR(ldo->regulator);
348 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
349 id + 1, ret);
350 goto err;
351 }
352
353 irq = platform_get_irq_byname(pdev, "UV");
Mark Browndfda9c22011-03-01 16:50:43 +0000354 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
355 IRQF_TRIGGER_RISING, ldo->name,
356 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100357 if (ret != 0) {
358 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
359 irq, ret);
360 goto err_regulator;
361 }
362
363 platform_set_drvdata(pdev, ldo);
364
365 return 0;
366
367err_regulator:
368 regulator_unregister(ldo->regulator);
369err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100370 return ret;
371}
372
373static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
374{
375 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100376
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800377 platform_set_drvdata(pdev, NULL);
378
Mark Browndfda9c22011-03-01 16:50:43 +0000379 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100380 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100381
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);
Axel Lin6085d4d2012-03-29 15:04:22 +0800408 /* 1.7-3.5V in 100mV steps */
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100409 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,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000416 int min_uV, int max_uV,
417 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100418{
419 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
420 struct wm831x *wm831x = ldo->wm831x;
421 int vsel, ret;
422
423 if (min_uV < 1000000)
424 vsel = 0;
425 else if (min_uV < 1700000)
426 vsel = ((min_uV - 1000000) / 50000);
427 else
428 vsel = ((min_uV - 1700000) / 100000)
429 + WM831X_ALDO_SELECTOR_LOW + 1;
430
431 ret = wm831x_aldo_list_voltage(rdev, vsel);
432 if (ret < 0)
433 return ret;
434 if (ret < min_uV || ret > max_uV)
435 return -EINVAL;
436
Mark Brown3a93f2a2010-11-10 14:38:29 +0000437 *selector = vsel;
438
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100439 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
440}
441
442static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000443 int min_uV, int max_uV, unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100444{
445 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
446 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
447
Mark Brown3a93f2a2010-11-10 14:38:29 +0000448 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
449 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100450}
451
452static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
453 int uV)
454{
455 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
456 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000457 unsigned int selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100458
Mark Brown3a93f2a2010-11-10 14:38:29 +0000459 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100460}
461
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100462static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
463{
464 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
465 struct wm831x *wm831x = ldo->wm831x;
466 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100467 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100468
469 ret = wm831x_reg_read(wm831x, on_reg);
470 if (ret < 0)
471 return 0;
472
473 if (ret & WM831X_LDO7_ON_MODE)
474 return REGULATOR_MODE_IDLE;
475 else
476 return REGULATOR_MODE_NORMAL;
477}
478
479static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
480 unsigned int mode)
481{
482 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
483 struct wm831x *wm831x = ldo->wm831x;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100484 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
485 int ret;
486
487
488 switch (mode) {
489 case REGULATOR_MODE_NORMAL:
Axel Line841a362012-03-29 15:02:55 +0800490 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100491 if (ret < 0)
492 return ret;
493 break;
494
495 case REGULATOR_MODE_IDLE:
Axel Line841a362012-03-29 15:02:55 +0800496 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100497 WM831X_LDO7_ON_MODE);
498 if (ret < 0)
499 return ret;
500 break;
501
502 default:
503 return -EINVAL;
504 }
505
506 return 0;
507}
508
509static int wm831x_aldo_get_status(struct regulator_dev *rdev)
510{
511 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
512 struct wm831x *wm831x = ldo->wm831x;
513 int mask = 1 << rdev_get_id(rdev);
514 int ret;
515
516 /* Is the regulator on? */
517 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
518 if (ret < 0)
519 return ret;
520 if (!(ret & mask))
521 return REGULATOR_STATUS_OFF;
522
523 /* Is it reporting under voltage? */
524 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
525 if (ret & mask)
526 return REGULATOR_STATUS_ERROR;
527
528 ret = wm831x_aldo_get_mode(rdev);
529 if (ret < 0)
530 return ret;
531 else
532 return regulator_mode_to_status(ret);
533}
534
535static struct regulator_ops wm831x_aldo_ops = {
536 .list_voltage = wm831x_aldo_list_voltage,
Mark Brownac663b42012-04-15 11:55:55 +0100537 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100538 .set_voltage = wm831x_aldo_set_voltage,
539 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
540 .get_mode = wm831x_aldo_get_mode,
541 .set_mode = wm831x_aldo_set_mode,
542 .get_status = wm831x_aldo_get_status,
543
544 .is_enabled = wm831x_ldo_is_enabled,
545 .enable = wm831x_ldo_enable,
546 .disable = wm831x_ldo_disable,
547};
548
549static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
550{
551 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
552 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
Mark Brownc172708d2012-04-04 00:50:22 +0100553 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100554 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100555 struct wm831x_ldo *ldo;
556 struct resource *res;
557 int ret, irq;
558
Mark Brown137a6352011-07-25 22:20:29 +0100559 if (pdata && pdata->wm831x_num)
560 id = (pdata->wm831x_num * 10) + 1;
561 else
562 id = 0;
563 id = pdev->id - id;
564
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100565 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
566
567 if (pdata == NULL || pdata->ldo[id] == NULL)
568 return -ENODEV;
569
Mark Brownfded2f42011-12-15 02:11:14 +0800570 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100571 if (ldo == NULL) {
572 dev_err(&pdev->dev, "Unable to allocate private data\n");
573 return -ENOMEM;
574 }
575
576 ldo->wm831x = wm831x;
577
578 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
579 if (res == NULL) {
580 dev_err(&pdev->dev, "No I/O resource\n");
581 ret = -EINVAL;
582 goto err;
583 }
584 ldo->base = res->start;
585
586 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
587 ldo->desc.name = ldo->name;
588 ldo->desc.id = id;
589 ldo->desc.type = REGULATOR_VOLTAGE;
590 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
591 ldo->desc.ops = &wm831x_aldo_ops;
592 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100593 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
594 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100595
Mark Brownc172708d2012-04-04 00:50:22 +0100596 config.dev = pdev->dev.parent;
597 config.init_data = pdata->ldo[id];
598 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100599 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100600
601 ldo->regulator = regulator_register(&ldo->desc, &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100602 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");
Mark Browndfda9c22011-03-01 16:50:43 +0000610 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
611 IRQF_TRIGGER_RISING, ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100612 if (ret != 0) {
613 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
614 irq, ret);
615 goto err_regulator;
616 }
617
618 platform_set_drvdata(pdev, ldo);
619
620 return 0;
621
622err_regulator:
623 regulator_unregister(ldo->regulator);
624err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100625 return ret;
626}
627
628static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
629{
630 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100631
Mark Browndfda9c22011-03-01 16:50:43 +0000632 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100633 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100634
635 return 0;
636}
637
638static struct platform_driver wm831x_aldo_driver = {
639 .probe = wm831x_aldo_probe,
640 .remove = __devexit_p(wm831x_aldo_remove),
641 .driver = {
642 .name = "wm831x-aldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800643 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100644 },
645};
646
647/*
648 * Alive LDO
649 */
650
651#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
652
653static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
654 unsigned int selector)
655{
656 /* 0.8-1.55V in 50mV steps */
657 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
658 return 800000 + (selector * 50000);
659 return -EINVAL;
660}
661
662static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
663 int reg,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000664 int min_uV, int max_uV,
665 unsigned *selector)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100666{
667 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
668 struct wm831x *wm831x = ldo->wm831x;
669 int vsel, ret;
670
671 vsel = (min_uV - 800000) / 50000;
672
673 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
674 if (ret < 0)
675 return ret;
676 if (ret < min_uV || ret > max_uV)
677 return -EINVAL;
678
Mark Brown3a93f2a2010-11-10 14:38:29 +0000679 *selector = vsel;
680
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100681 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
682}
683
684static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
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 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
690
Mark Brown3a93f2a2010-11-10 14:38:29 +0000691 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
692 selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100693}
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;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000700 unsigned selector;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100701
Mark Brown3a93f2a2010-11-10 14:38:29 +0000702 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100703}
704
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100705static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
706{
707 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
708 struct wm831x *wm831x = ldo->wm831x;
709 int mask = 1 << rdev_get_id(rdev);
710 int ret;
711
712 /* Is the regulator on? */
713 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
714 if (ret < 0)
715 return ret;
716 if (ret & mask)
717 return REGULATOR_STATUS_ON;
718 else
719 return REGULATOR_STATUS_OFF;
720}
721
722static struct regulator_ops wm831x_alive_ldo_ops = {
723 .list_voltage = wm831x_alive_ldo_list_voltage,
Mark Brownac663b42012-04-15 11:55:55 +0100724 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100725 .set_voltage = wm831x_alive_ldo_set_voltage,
726 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
727 .get_status = wm831x_alive_ldo_get_status,
728
729 .is_enabled = wm831x_ldo_is_enabled,
730 .enable = wm831x_ldo_enable,
731 .disable = wm831x_ldo_disable,
732};
733
734static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
735{
736 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
737 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
Mark Brownc172708d2012-04-04 00:50:22 +0100738 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100739 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100740 struct wm831x_ldo *ldo;
741 struct resource *res;
742 int ret;
743
Mark Brown137a6352011-07-25 22:20:29 +0100744 if (pdata && pdata->wm831x_num)
745 id = (pdata->wm831x_num * 10) + 1;
746 else
747 id = 0;
748 id = pdev->id - id;
749
750
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100751 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
752
753 if (pdata == NULL || pdata->ldo[id] == NULL)
754 return -ENODEV;
755
Mark Brownfded2f42011-12-15 02:11:14 +0800756 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100757 if (ldo == NULL) {
758 dev_err(&pdev->dev, "Unable to allocate private data\n");
759 return -ENOMEM;
760 }
761
762 ldo->wm831x = wm831x;
763
764 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
765 if (res == NULL) {
766 dev_err(&pdev->dev, "No I/O resource\n");
767 ret = -EINVAL;
768 goto err;
769 }
770 ldo->base = res->start;
771
772 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
773 ldo->desc.name = ldo->name;
774 ldo->desc.id = id;
775 ldo->desc.type = REGULATOR_VOLTAGE;
776 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
777 ldo->desc.ops = &wm831x_alive_ldo_ops;
778 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100779 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
780 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100781
Mark Brownc172708d2012-04-04 00:50:22 +0100782 config.dev = pdev->dev.parent;
783 config.init_data = pdata->ldo[id];
784 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100785 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100786
787 ldo->regulator = regulator_register(&ldo->desc, &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100788 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
799err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100800 return ret;
801}
802
803static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
804{
805 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
806
807 regulator_unregister(ldo->regulator);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100808
809 return 0;
810}
811
812static struct platform_driver wm831x_alive_ldo_driver = {
813 .probe = wm831x_alive_ldo_probe,
814 .remove = __devexit_p(wm831x_alive_ldo_remove),
815 .driver = {
816 .name = "wm831x-alive-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800817 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100818 },
819};
820
821static int __init wm831x_ldo_init(void)
822{
823 int ret;
824
825 ret = platform_driver_register(&wm831x_gp_ldo_driver);
826 if (ret != 0)
827 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
828
829 ret = platform_driver_register(&wm831x_aldo_driver);
830 if (ret != 0)
831 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
832
833 ret = platform_driver_register(&wm831x_alive_ldo_driver);
834 if (ret != 0)
835 pr_err("Failed to register WM831x alive LDO driver: %d\n",
836 ret);
837
838 return 0;
839}
840subsys_initcall(wm831x_ldo_init);
841
842static void __exit wm831x_ldo_exit(void)
843{
844 platform_driver_unregister(&wm831x_alive_ldo_driver);
845 platform_driver_unregister(&wm831x_aldo_driver);
846 platform_driver_unregister(&wm831x_gp_ldo_driver);
847}
848module_exit(wm831x_ldo_exit);
849
850/* Module information */
851MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
852MODULE_DESCRIPTION("WM831x LDO driver");
853MODULE_LICENSE("GPL");
854MODULE_ALIAS("platform:wm831x-ldo");
855MODULE_ALIAS("platform:wm831x-aldo");
856MODULE_ALIAS("platform:wm831x-aliveldo");