blob: e4a6f888484ece31171d83a1e3b0a7334fe10e27 [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
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100201static const 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);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530238 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100239 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100240
241 ldo->wm831x = wm831x;
242
Mark Brown56560982012-08-07 19:42:47 +0100243 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100244 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100245 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100246 ret = -EINVAL;
247 goto err;
248 }
249 ldo->base = res->start;
250
251 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
252 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100253
254 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
255 "LDO%dVDD", id + 1);
256 ldo->desc.supply_name = ldo->supply_name;
257
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100258 ldo->desc.id = id;
259 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100260 ldo->desc.n_voltages = 32;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100261 ldo->desc.ops = &wm831x_gp_ldo_ops;
262 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100263 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
264 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100265 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
266 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700267 ldo->desc.bypass_reg = ldo->base;
268 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
Mark Brown5ff26a12013-07-02 23:35:22 +0100269 ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
270 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100271
Mark Brownc1727082012-04-04 00:50:22 +0100272 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100273 if (pdata)
274 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100275 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100276 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100277
Mark Brownfc7c60e2013-08-31 12:00:17 +0100278 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
279 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100280 if (IS_ERR(ldo->regulator)) {
281 ret = PTR_ERR(ldo->regulator);
282 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
283 id + 1, ret);
284 goto err;
285 }
286
Mark Browncd997582012-05-14 23:14:24 +0200287 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100288 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
289 wm831x_ldo_uv_irq,
Fabio Estevam29454732015-06-01 22:33:53 -0300290 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
291 ldo->name,
Mark Brown41c7a872013-08-31 11:50:00 +0100292 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100293 if (ret != 0) {
294 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
295 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100296 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100297 }
298
299 platform_set_drvdata(pdev, ldo);
300
301 return 0;
302
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100303err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100304 return ret;
305}
306
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100307static struct platform_driver wm831x_gp_ldo_driver = {
308 .probe = wm831x_gp_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100309 .driver = {
310 .name = "wm831x-ldo",
311 },
312};
313
314/*
315 * Analogue LDOs
316 */
317
Mark Brown5ff26a12013-07-02 23:35:22 +0100318static const struct regulator_linear_range wm831x_aldo_ranges[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800319 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
320 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
Mark Brown5ff26a12013-07-02 23:35:22 +0100321};
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100322
323static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
324 int uV)
325{
326 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800327 struct wm831x *wm831x = ldo->wm831x;
328 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100329
Mark Brown5ff26a12013-07-02 23:35:22 +0100330 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
Axel Lin0a479682012-06-12 10:46:43 +0800331 if (sel < 0)
332 return sel;
333
334 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100335}
336
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100337static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
338{
339 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
340 struct wm831x *wm831x = ldo->wm831x;
341 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100342 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100343
344 ret = wm831x_reg_read(wm831x, on_reg);
345 if (ret < 0)
346 return 0;
347
348 if (ret & WM831X_LDO7_ON_MODE)
349 return REGULATOR_MODE_IDLE;
350 else
351 return REGULATOR_MODE_NORMAL;
352}
353
354static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
355 unsigned int mode)
356{
357 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
358 struct wm831x *wm831x = ldo->wm831x;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100359 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
360 int ret;
361
362
363 switch (mode) {
364 case REGULATOR_MODE_NORMAL:
Axel Line841a362012-03-29 15:02:55 +0800365 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100366 if (ret < 0)
367 return ret;
368 break;
369
370 case REGULATOR_MODE_IDLE:
Axel Line841a362012-03-29 15:02:55 +0800371 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100372 WM831X_LDO7_ON_MODE);
373 if (ret < 0)
374 return ret;
375 break;
376
377 default:
378 return -EINVAL;
379 }
380
381 return 0;
382}
383
384static int wm831x_aldo_get_status(struct regulator_dev *rdev)
385{
386 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
387 struct wm831x *wm831x = ldo->wm831x;
388 int mask = 1 << rdev_get_id(rdev);
389 int ret;
390
391 /* Is the regulator on? */
392 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
393 if (ret < 0)
394 return ret;
395 if (!(ret & mask))
396 return REGULATOR_STATUS_OFF;
397
398 /* Is it reporting under voltage? */
399 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
Axel Lin363506c2012-07-31 21:28:26 +0800400 if (ret < 0)
401 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100402 if (ret & mask)
403 return REGULATOR_STATUS_ERROR;
404
405 ret = wm831x_aldo_get_mode(rdev);
406 if (ret < 0)
407 return ret;
408 else
409 return regulator_mode_to_status(ret);
410}
411
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100412static const struct regulator_ops wm831x_aldo_ops = {
Mark Brown5ff26a12013-07-02 23:35:22 +0100413 .list_voltage = regulator_list_voltage_linear_range,
414 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownac663b42012-04-15 11:55:55 +0100415 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800416 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100417 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
418 .get_mode = wm831x_aldo_get_mode,
419 .set_mode = wm831x_aldo_set_mode,
420 .get_status = wm831x_aldo_get_status,
Mark Brown22c5fb62012-08-27 21:52:29 -0700421 .set_bypass = regulator_set_bypass_regmap,
422 .get_bypass = regulator_get_bypass_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100423
Mark Brownca8c3612012-04-15 12:38:52 +0100424 .is_enabled = regulator_is_enabled_regmap,
425 .enable = regulator_enable_regmap,
426 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100427};
428
Bill Pembertona5023572012-11-19 13:22:22 -0500429static int wm831x_aldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100430{
431 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900432 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100433 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100434 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100435 struct wm831x_ldo *ldo;
436 struct resource *res;
437 int ret, irq;
438
Mark Brown137a6352011-07-25 22:20:29 +0100439 if (pdata && pdata->wm831x_num)
440 id = (pdata->wm831x_num * 10) + 1;
441 else
442 id = 0;
443 id = pdev->id - id;
444
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100445 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
446
Mark Brownfded2f42011-12-15 02:11:14 +0800447 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530448 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100449 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100450
451 ldo->wm831x = wm831x;
452
Mark Brown56560982012-08-07 19:42:47 +0100453 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100454 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100455 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100456 ret = -EINVAL;
457 goto err;
458 }
459 ldo->base = res->start;
460
461 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
462 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100463
464 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
465 "LDO%dVDD", id + 1);
466 ldo->desc.supply_name = ldo->supply_name;
467
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100468 ldo->desc.id = id;
469 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100470 ldo->desc.n_voltages = 32;
471 ldo->desc.linear_ranges = wm831x_aldo_ranges;
472 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100473 ldo->desc.ops = &wm831x_aldo_ops;
474 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100475 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
476 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100477 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
478 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700479 ldo->desc.bypass_reg = ldo->base;
480 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100481
Mark Brownc1727082012-04-04 00:50:22 +0100482 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100483 if (pdata)
484 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100485 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100486 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100487
Mark Brownfc7c60e2013-08-31 12:00:17 +0100488 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
489 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100490 if (IS_ERR(ldo->regulator)) {
491 ret = PTR_ERR(ldo->regulator);
492 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
493 id + 1, ret);
494 goto err;
495 }
496
Mark Browncd997582012-05-14 23:14:24 +0200497 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100498 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
499 wm831x_ldo_uv_irq,
Fabio Estevam29454732015-06-01 22:33:53 -0300500 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
501 ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100502 if (ret != 0) {
503 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
504 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100505 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100506 }
507
508 platform_set_drvdata(pdev, ldo);
509
510 return 0;
511
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100512err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100513 return ret;
514}
515
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100516static struct platform_driver wm831x_aldo_driver = {
517 .probe = wm831x_aldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100518 .driver = {
519 .name = "wm831x-aldo",
520 },
521};
522
523/*
524 * Alive LDO
525 */
526
527#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
528
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100529static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
530 int uV)
531{
532 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800533 struct wm831x *wm831x = ldo->wm831x;
534 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100535
Axel Lin0a479682012-06-12 10:46:43 +0800536 sel = regulator_map_voltage_linear(rdev, uV, uV);
537 if (sel < 0)
538 return sel;
539
540 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100541}
542
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100543static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
544{
545 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
546 struct wm831x *wm831x = ldo->wm831x;
547 int mask = 1 << rdev_get_id(rdev);
548 int ret;
549
550 /* Is the regulator on? */
551 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
552 if (ret < 0)
553 return ret;
554 if (ret & mask)
555 return REGULATOR_STATUS_ON;
556 else
557 return REGULATOR_STATUS_OFF;
558}
559
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100560static const struct regulator_ops wm831x_alive_ldo_ops = {
Mark Brownd31e9542012-05-09 21:53:32 +0100561 .list_voltage = regulator_list_voltage_linear,
Axel Linc2543b52012-05-31 17:39:00 +0800562 .map_voltage = regulator_map_voltage_linear,
Mark Brownac663b42012-04-15 11:55:55 +0100563 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800564 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100565 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
566 .get_status = wm831x_alive_ldo_get_status,
567
Mark Brownca8c3612012-04-15 12:38:52 +0100568 .is_enabled = regulator_is_enabled_regmap,
569 .enable = regulator_enable_regmap,
570 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100571};
572
Bill Pembertona5023572012-11-19 13:22:22 -0500573static int wm831x_alive_ldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100574{
575 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900576 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100577 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100578 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100579 struct wm831x_ldo *ldo;
580 struct resource *res;
581 int ret;
582
Mark Brown137a6352011-07-25 22:20:29 +0100583 if (pdata && pdata->wm831x_num)
584 id = (pdata->wm831x_num * 10) + 1;
585 else
586 id = 0;
587 id = pdev->id - id;
588
589
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100590 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
591
Mark Brownfded2f42011-12-15 02:11:14 +0800592 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530593 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100594 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100595
596 ldo->wm831x = wm831x;
597
Mark Brown56560982012-08-07 19:42:47 +0100598 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100599 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100600 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100601 ret = -EINVAL;
602 goto err;
603 }
604 ldo->base = res->start;
605
606 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
607 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100608
609 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
610 "LDO%dVDD", id + 1);
611 ldo->desc.supply_name = ldo->supply_name;
612
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100613 ldo->desc.id = id;
614 ldo->desc.type = REGULATOR_VOLTAGE;
615 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
616 ldo->desc.ops = &wm831x_alive_ldo_ops;
617 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100618 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
619 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100620 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
621 ldo->desc.enable_mask = 1 << id;
Mark Brownd31e9542012-05-09 21:53:32 +0100622 ldo->desc.min_uV = 800000;
623 ldo->desc.uV_step = 50000;
Mark Browneefaa3c2012-06-27 15:00:02 +0100624 ldo->desc.enable_time = 1000;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100625
Mark Brownc1727082012-04-04 00:50:22 +0100626 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100627 if (pdata)
628 config.init_data = pdata->ldo[id];
Mark Brownc1727082012-04-04 00:50:22 +0100629 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100630 config.regmap = wm831x->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100631
Mark Brownfc7c60e2013-08-31 12:00:17 +0100632 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
633 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100634 if (IS_ERR(ldo->regulator)) {
635 ret = PTR_ERR(ldo->regulator);
636 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
637 id + 1, ret);
638 goto err;
639 }
640
641 platform_set_drvdata(pdev, ldo);
642
643 return 0;
644
645err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100646 return ret;
647}
648
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100649static struct platform_driver wm831x_alive_ldo_driver = {
650 .probe = wm831x_alive_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100651 .driver = {
652 .name = "wm831x-alive-ldo",
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100653 },
654};
655
Thierry Reding92a513b2015-12-02 17:32:53 +0100656static struct platform_driver * const drivers[] = {
657 &wm831x_gp_ldo_driver,
658 &wm831x_aldo_driver,
659 &wm831x_alive_ldo_driver,
660};
661
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100662static int __init wm831x_ldo_init(void)
663{
Thierry Reding92a513b2015-12-02 17:32:53 +0100664 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100665}
666subsys_initcall(wm831x_ldo_init);
667
668static void __exit wm831x_ldo_exit(void)
669{
Thierry Reding92a513b2015-12-02 17:32:53 +0100670 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100671}
672module_exit(wm831x_ldo_exit);
673
674/* Module information */
675MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
676MODULE_DESCRIPTION("WM831x LDO driver");
677MODULE_LICENSE("GPL");
678MODULE_ALIAS("platform:wm831x-ldo");
679MODULE_ALIAS("platform:wm831x-aldo");
680MODULE_ALIAS("platform:wm831x-aliveldo");