blob: 46d6700467b57007fbb277283207cfbbc4456fc0 [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
Mark Brownf1aba132012-03-28 21:40:20 +010028#define WM831X_LDO_MAX_NAME 9
Mark Brownd1c6b4f2009-07-28 15:22:02 +010029
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];
Mark Brownf1aba132012-03-28 21:40:20 +010039 char supply_name[WM831X_LDO_MAX_NAME];
Mark Brownd1c6b4f2009-07-28 15:22:02 +010040 struct regulator_desc desc;
41 int base;
42 struct wm831x *wm831x;
43 struct regulator_dev *regulator;
44};
45
46/*
47 * Shared
48 */
49
Mark Brownd1c6b4f2009-07-28 15:22:02 +010050static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
51{
52 struct wm831x_ldo *ldo = data;
53
54 regulator_notifier_call_chain(ldo->regulator,
55 REGULATOR_EVENT_UNDER_VOLTAGE,
56 NULL);
57
58 return IRQ_HANDLED;
59}
60
61/*
62 * General purpose LDOs
63 */
64
Mark Brown5ff26a12013-07-02 23:35:22 +010065static const struct regulator_linear_range wm831x_gp_ldo_ranges[] = {
Axel Lin8828bae2013-10-11 09:32:18 +080066 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
67 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
Mark Brown5ff26a12013-07-02 23:35:22 +010068};
Mark Brownd1c6b4f2009-07-28 15:22:02 +010069
70static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
71 int uV)
72{
73 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +080074 struct wm831x *wm831x = ldo->wm831x;
75 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010076
Mark Brown5ff26a12013-07-02 23:35:22 +010077 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
Axel Lin0a479682012-06-12 10:46:43 +080078 if (sel < 0)
79 return sel;
80
81 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +010082}
83
Mark Brownd1c6b4f2009-07-28 15:22:02 +010084static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
85{
86 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
87 struct wm831x *wm831x = ldo->wm831x;
88 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
89 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin9a767d42009-10-16 14:16:15 +020090 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010091
92 ret = wm831x_reg_read(wm831x, on_reg);
93 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +020094 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010095
96 if (!(ret & WM831X_LDO1_ON_MODE))
97 return REGULATOR_MODE_NORMAL;
98
99 ret = wm831x_reg_read(wm831x, ctrl_reg);
100 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +0200101 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100102
103 if (ret & WM831X_LDO1_LP_MODE)
104 return REGULATOR_MODE_STANDBY;
105 else
106 return REGULATOR_MODE_IDLE;
107}
108
109static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
110 unsigned int mode)
111{
112 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
113 struct wm831x *wm831x = ldo->wm831x;
114 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
115 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
116 int ret;
117
118
119 switch (mode) {
120 case REGULATOR_MODE_NORMAL:
121 ret = wm831x_set_bits(wm831x, on_reg,
122 WM831X_LDO1_ON_MODE, 0);
123 if (ret < 0)
124 return ret;
125 break;
126
127 case REGULATOR_MODE_IDLE:
128 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800129 WM831X_LDO1_LP_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100130 if (ret < 0)
131 return ret;
132
133 ret = wm831x_set_bits(wm831x, on_reg,
134 WM831X_LDO1_ON_MODE,
135 WM831X_LDO1_ON_MODE);
136 if (ret < 0)
137 return ret;
Axel Line2609992010-09-06 16:48:13 +0800138 break;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100139
140 case REGULATOR_MODE_STANDBY:
141 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800142 WM831X_LDO1_LP_MODE,
143 WM831X_LDO1_LP_MODE);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100144 if (ret < 0)
145 return ret;
146
147 ret = wm831x_set_bits(wm831x, on_reg,
148 WM831X_LDO1_ON_MODE,
149 WM831X_LDO1_ON_MODE);
150 if (ret < 0)
151 return ret;
152 break;
153
154 default:
155 return -EINVAL;
156 }
157
158 return 0;
159}
160
161static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
162{
163 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
164 struct wm831x *wm831x = ldo->wm831x;
165 int mask = 1 << rdev_get_id(rdev);
166 int ret;
167
168 /* Is the regulator on? */
169 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
170 if (ret < 0)
171 return ret;
172 if (!(ret & mask))
173 return REGULATOR_STATUS_OFF;
174
175 /* Is it reporting under voltage? */
176 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
Axel Lin363506c2012-07-31 21:28:26 +0800177 if (ret < 0)
178 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100179 if (ret & mask)
180 return REGULATOR_STATUS_ERROR;
181
182 ret = wm831x_gp_ldo_get_mode(rdev);
183 if (ret < 0)
184 return ret;
185 else
186 return regulator_mode_to_status(ret);
187}
188
189static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
190 int input_uV,
191 int output_uV, int load_uA)
192{
193 if (load_uA < 20000)
194 return REGULATOR_MODE_STANDBY;
195 if (load_uA < 50000)
196 return REGULATOR_MODE_IDLE;
197 return REGULATOR_MODE_NORMAL;
198}
199
200
201static struct regulator_ops wm831x_gp_ldo_ops = {
Mark Brown5ff26a12013-07-02 23:35:22 +0100202 .list_voltage = regulator_list_voltage_linear_range,
203 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownac663b42012-04-15 11:55:55 +0100204 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800205 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100206 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
207 .get_mode = wm831x_gp_ldo_get_mode,
208 .set_mode = wm831x_gp_ldo_set_mode,
209 .get_status = wm831x_gp_ldo_get_status,
210 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
Mark Brown22c5fb62012-08-27 21:52:29 -0700211 .get_bypass = regulator_get_bypass_regmap,
212 .set_bypass = regulator_set_bypass_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100213
Mark Brownca8c3612012-04-15 12:38:52 +0100214 .is_enabled = regulator_is_enabled_regmap,
215 .enable = regulator_enable_regmap,
216 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100217};
218
Bill Pembertona5023572012-11-19 13:22:22 -0500219static int wm831x_gp_ldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100220{
221 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900222 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100223 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100224 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100225 struct wm831x_ldo *ldo;
226 struct resource *res;
227 int ret, irq;
228
Mark Brown137a6352011-07-25 22:20:29 +0100229 if (pdata && pdata->wm831x_num)
230 id = (pdata->wm831x_num * 10) + 1;
231 else
232 id = 0;
233 id = pdev->id - id;
234
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100235 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
236
Mark Brownfded2f42011-12-15 02:11:14 +0800237 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100238 if (ldo == NULL) {
239 dev_err(&pdev->dev, "Unable to allocate private data\n");
240 return -ENOMEM;
241 }
242
243 ldo->wm831x = wm831x;
244
Mark Brown56560982012-08-07 19:42:47 +0100245 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100246 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100247 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100248 ret = -EINVAL;
249 goto err;
250 }
251 ldo->base = res->start;
252
253 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
254 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100255
256 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
257 "LDO%dVDD", id + 1);
258 ldo->desc.supply_name = ldo->supply_name;
259
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100260 ldo->desc.id = id;
261 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100262 ldo->desc.n_voltages = 32;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100263 ldo->desc.ops = &wm831x_gp_ldo_ops;
264 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100265 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
266 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100267 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
268 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700269 ldo->desc.bypass_reg = ldo->base;
270 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
Mark Brown5ff26a12013-07-02 23:35:22 +0100271 ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
272 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100273
Mark Brownc1727082012-04-04 00:50:22 +0100274 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100275 if (pdata)
276 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100277 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100278 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100279
Mark Brownfc7c60e2013-08-31 12:00:17 +0100280 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
281 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100282 if (IS_ERR(ldo->regulator)) {
283 ret = PTR_ERR(ldo->regulator);
284 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
285 id + 1, ret);
286 goto err;
287 }
288
Mark Browncd997582012-05-14 23:14:24 +0200289 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100290 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
291 wm831x_ldo_uv_irq,
292 IRQF_TRIGGER_RISING, ldo->name,
293 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100294 if (ret != 0) {
295 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
296 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100297 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100298 }
299
300 platform_set_drvdata(pdev, ldo);
301
302 return 0;
303
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100304err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100305 return ret;
306}
307
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100308static struct platform_driver wm831x_gp_ldo_driver = {
309 .probe = wm831x_gp_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100310 .driver = {
311 .name = "wm831x-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800312 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100313 },
314};
315
316/*
317 * Analogue LDOs
318 */
319
Mark Brown5ff26a12013-07-02 23:35:22 +0100320static const struct regulator_linear_range wm831x_aldo_ranges[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800321 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
322 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
Mark Brown5ff26a12013-07-02 23:35:22 +0100323};
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100324
325static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
326 int uV)
327{
328 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800329 struct wm831x *wm831x = ldo->wm831x;
330 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100331
Mark Brown5ff26a12013-07-02 23:35:22 +0100332 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
Axel Lin0a479682012-06-12 10:46:43 +0800333 if (sel < 0)
334 return sel;
335
336 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100337}
338
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100339static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
340{
341 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
342 struct wm831x *wm831x = ldo->wm831x;
343 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100344 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100345
346 ret = wm831x_reg_read(wm831x, on_reg);
347 if (ret < 0)
348 return 0;
349
350 if (ret & WM831X_LDO7_ON_MODE)
351 return REGULATOR_MODE_IDLE;
352 else
353 return REGULATOR_MODE_NORMAL;
354}
355
356static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
357 unsigned int mode)
358{
359 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
360 struct wm831x *wm831x = ldo->wm831x;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100361 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
362 int ret;
363
364
365 switch (mode) {
366 case REGULATOR_MODE_NORMAL:
Axel Line841a362012-03-29 15:02:55 +0800367 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100368 if (ret < 0)
369 return ret;
370 break;
371
372 case REGULATOR_MODE_IDLE:
Axel Line841a362012-03-29 15:02:55 +0800373 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100374 WM831X_LDO7_ON_MODE);
375 if (ret < 0)
376 return ret;
377 break;
378
379 default:
380 return -EINVAL;
381 }
382
383 return 0;
384}
385
386static int wm831x_aldo_get_status(struct regulator_dev *rdev)
387{
388 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
389 struct wm831x *wm831x = ldo->wm831x;
390 int mask = 1 << rdev_get_id(rdev);
391 int ret;
392
393 /* Is the regulator on? */
394 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
395 if (ret < 0)
396 return ret;
397 if (!(ret & mask))
398 return REGULATOR_STATUS_OFF;
399
400 /* Is it reporting under voltage? */
401 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
Axel Lin363506c2012-07-31 21:28:26 +0800402 if (ret < 0)
403 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100404 if (ret & mask)
405 return REGULATOR_STATUS_ERROR;
406
407 ret = wm831x_aldo_get_mode(rdev);
408 if (ret < 0)
409 return ret;
410 else
411 return regulator_mode_to_status(ret);
412}
413
414static struct regulator_ops wm831x_aldo_ops = {
Mark Brown5ff26a12013-07-02 23:35:22 +0100415 .list_voltage = regulator_list_voltage_linear_range,
416 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownac663b42012-04-15 11:55:55 +0100417 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800418 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100419 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
420 .get_mode = wm831x_aldo_get_mode,
421 .set_mode = wm831x_aldo_set_mode,
422 .get_status = wm831x_aldo_get_status,
Mark Brown22c5fb62012-08-27 21:52:29 -0700423 .set_bypass = regulator_set_bypass_regmap,
424 .get_bypass = regulator_get_bypass_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100425
Mark Brownca8c3612012-04-15 12:38:52 +0100426 .is_enabled = regulator_is_enabled_regmap,
427 .enable = regulator_enable_regmap,
428 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100429};
430
Bill Pembertona5023572012-11-19 13:22:22 -0500431static int wm831x_aldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100432{
433 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900434 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100435 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100436 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100437 struct wm831x_ldo *ldo;
438 struct resource *res;
439 int ret, irq;
440
Mark Brown137a6352011-07-25 22:20:29 +0100441 if (pdata && pdata->wm831x_num)
442 id = (pdata->wm831x_num * 10) + 1;
443 else
444 id = 0;
445 id = pdev->id - id;
446
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100447 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
448
Mark Brownfded2f42011-12-15 02:11:14 +0800449 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100450 if (ldo == NULL) {
451 dev_err(&pdev->dev, "Unable to allocate private data\n");
452 return -ENOMEM;
453 }
454
455 ldo->wm831x = wm831x;
456
Mark Brown56560982012-08-07 19:42:47 +0100457 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100458 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100459 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100460 ret = -EINVAL;
461 goto err;
462 }
463 ldo->base = res->start;
464
465 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
466 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100467
468 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
469 "LDO%dVDD", id + 1);
470 ldo->desc.supply_name = ldo->supply_name;
471
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100472 ldo->desc.id = id;
473 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100474 ldo->desc.n_voltages = 32;
475 ldo->desc.linear_ranges = wm831x_aldo_ranges;
476 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100477 ldo->desc.ops = &wm831x_aldo_ops;
478 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100479 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
480 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100481 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
482 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700483 ldo->desc.bypass_reg = ldo->base;
484 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100485
Mark Brownc1727082012-04-04 00:50:22 +0100486 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100487 if (pdata)
488 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100489 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100490 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100491
Mark Brownfc7c60e2013-08-31 12:00:17 +0100492 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
493 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100494 if (IS_ERR(ldo->regulator)) {
495 ret = PTR_ERR(ldo->regulator);
496 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
497 id + 1, ret);
498 goto err;
499 }
500
Mark Browncd997582012-05-14 23:14:24 +0200501 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100502 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
503 wm831x_ldo_uv_irq,
504 IRQF_TRIGGER_RISING, ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100505 if (ret != 0) {
506 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
507 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100508 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100509 }
510
511 platform_set_drvdata(pdev, ldo);
512
513 return 0;
514
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100515err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100516 return ret;
517}
518
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100519static struct platform_driver wm831x_aldo_driver = {
520 .probe = wm831x_aldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100521 .driver = {
522 .name = "wm831x-aldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800523 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100524 },
525};
526
527/*
528 * Alive LDO
529 */
530
531#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
532
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100533static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
534 int uV)
535{
536 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800537 struct wm831x *wm831x = ldo->wm831x;
538 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100539
Axel Lin0a479682012-06-12 10:46:43 +0800540 sel = regulator_map_voltage_linear(rdev, uV, uV);
541 if (sel < 0)
542 return sel;
543
544 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100545}
546
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100547static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
548{
549 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
550 struct wm831x *wm831x = ldo->wm831x;
551 int mask = 1 << rdev_get_id(rdev);
552 int ret;
553
554 /* Is the regulator on? */
555 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
556 if (ret < 0)
557 return ret;
558 if (ret & mask)
559 return REGULATOR_STATUS_ON;
560 else
561 return REGULATOR_STATUS_OFF;
562}
563
564static struct regulator_ops wm831x_alive_ldo_ops = {
Mark Brownd31e9542012-05-09 21:53:32 +0100565 .list_voltage = regulator_list_voltage_linear,
Axel Linc2543b52012-05-31 17:39:00 +0800566 .map_voltage = regulator_map_voltage_linear,
Mark Brownac663b42012-04-15 11:55:55 +0100567 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800568 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100569 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
570 .get_status = wm831x_alive_ldo_get_status,
571
Mark Brownca8c3612012-04-15 12:38:52 +0100572 .is_enabled = regulator_is_enabled_regmap,
573 .enable = regulator_enable_regmap,
574 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100575};
576
Bill Pembertona5023572012-11-19 13:22:22 -0500577static int wm831x_alive_ldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100578{
579 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900580 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100581 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100582 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100583 struct wm831x_ldo *ldo;
584 struct resource *res;
585 int ret;
586
Mark Brown137a6352011-07-25 22:20:29 +0100587 if (pdata && pdata->wm831x_num)
588 id = (pdata->wm831x_num * 10) + 1;
589 else
590 id = 0;
591 id = pdev->id - id;
592
593
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100594 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
595
Mark Brownfded2f42011-12-15 02:11:14 +0800596 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100597 if (ldo == NULL) {
598 dev_err(&pdev->dev, "Unable to allocate private data\n");
599 return -ENOMEM;
600 }
601
602 ldo->wm831x = wm831x;
603
Mark Brown56560982012-08-07 19:42:47 +0100604 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100605 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100606 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100607 ret = -EINVAL;
608 goto err;
609 }
610 ldo->base = res->start;
611
612 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
613 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100614
615 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
616 "LDO%dVDD", id + 1);
617 ldo->desc.supply_name = ldo->supply_name;
618
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100619 ldo->desc.id = id;
620 ldo->desc.type = REGULATOR_VOLTAGE;
621 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
622 ldo->desc.ops = &wm831x_alive_ldo_ops;
623 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100624 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
625 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100626 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
627 ldo->desc.enable_mask = 1 << id;
Mark Brownd31e9542012-05-09 21:53:32 +0100628 ldo->desc.min_uV = 800000;
629 ldo->desc.uV_step = 50000;
Mark Browneefaa3c2012-06-27 15:00:02 +0100630 ldo->desc.enable_time = 1000;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100631
Mark Brownc1727082012-04-04 00:50:22 +0100632 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100633 if (pdata)
634 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100635 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100636 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100637
Mark Brownfc7c60e2013-08-31 12:00:17 +0100638 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
639 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100640 if (IS_ERR(ldo->regulator)) {
641 ret = PTR_ERR(ldo->regulator);
642 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
643 id + 1, ret);
644 goto err;
645 }
646
647 platform_set_drvdata(pdev, ldo);
648
649 return 0;
650
651err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100652 return ret;
653}
654
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100655static struct platform_driver wm831x_alive_ldo_driver = {
656 .probe = wm831x_alive_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100657 .driver = {
658 .name = "wm831x-alive-ldo",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800659 .owner = THIS_MODULE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100660 },
661};
662
663static int __init wm831x_ldo_init(void)
664{
665 int ret;
666
667 ret = platform_driver_register(&wm831x_gp_ldo_driver);
668 if (ret != 0)
669 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
670
671 ret = platform_driver_register(&wm831x_aldo_driver);
672 if (ret != 0)
673 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
674
675 ret = platform_driver_register(&wm831x_alive_ldo_driver);
676 if (ret != 0)
677 pr_err("Failed to register WM831x alive LDO driver: %d\n",
678 ret);
679
680 return 0;
681}
682subsys_initcall(wm831x_ldo_init);
683
684static void __exit wm831x_ldo_exit(void)
685{
686 platform_driver_unregister(&wm831x_alive_ldo_driver);
687 platform_driver_unregister(&wm831x_aldo_driver);
688 platform_driver_unregister(&wm831x_gp_ldo_driver);
689}
690module_exit(wm831x_ldo_exit);
691
692/* Module information */
693MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
694MODULE_DESCRIPTION("WM831x LDO driver");
695MODULE_LICENSE("GPL");
696MODULE_ALIAS("platform:wm831x-ldo");
697MODULE_ALIAS("platform:wm831x-aldo");
698MODULE_ALIAS("platform:wm831x-aliveldo");