blob: e091b189ecc0200537a6c614091b43701cf85030 [file] [log] [blame]
Axel Lin3cad5fc2019-04-17 22:16:30 +08001// SPDX-License-Identifier: GPL-2.0+
2//
3// wm831x-ldo.c -- LDO driver for the WM831x series
4//
5// Copyright 2009 Wolfson Microelectronics PLC.
6//
7// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brownd1c6b4f2009-07-28 15:22:02 +01008
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/init.h>
12#include <linux/bitops.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mark Brownd1c6b4f2009-07-28 15:22:02 +010018
19#include <linux/mfd/wm831x/core.h>
20#include <linux/mfd/wm831x/regulator.h>
21#include <linux/mfd/wm831x/pdata.h>
22
Mark Brownf1aba132012-03-28 21:40:20 +010023#define WM831X_LDO_MAX_NAME 9
Mark Brownd1c6b4f2009-07-28 15:22:02 +010024
25#define WM831X_LDO_CONTROL 0
26#define WM831X_LDO_ON_CONTROL 1
27#define WM831X_LDO_SLEEP_CONTROL 2
28
29#define WM831X_ALIVE_LDO_ON_CONTROL 0
30#define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
31
32struct wm831x_ldo {
33 char name[WM831X_LDO_MAX_NAME];
Mark Brownf1aba132012-03-28 21:40:20 +010034 char supply_name[WM831X_LDO_MAX_NAME];
Mark Brownd1c6b4f2009-07-28 15:22:02 +010035 struct regulator_desc desc;
36 int base;
37 struct wm831x *wm831x;
38 struct regulator_dev *regulator;
39};
40
41/*
42 * Shared
43 */
44
Mark Brownd1c6b4f2009-07-28 15:22:02 +010045static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
46{
47 struct wm831x_ldo *ldo = data;
48
49 regulator_notifier_call_chain(ldo->regulator,
50 REGULATOR_EVENT_UNDER_VOLTAGE,
51 NULL);
52
53 return IRQ_HANDLED;
54}
55
56/*
57 * General purpose LDOs
58 */
59
Matti Vaittinen60ab7f42020-05-08 18:43:36 +030060static const struct linear_range wm831x_gp_ldo_ranges[] = {
Axel Lin8828bae2013-10-11 09:32:18 +080061 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
62 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
Mark Brown5ff26a12013-07-02 23:35:22 +010063};
Mark Brownd1c6b4f2009-07-28 15:22:02 +010064
65static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
66 int uV)
67{
68 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +080069 struct wm831x *wm831x = ldo->wm831x;
70 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010071
Mark Brown5ff26a12013-07-02 23:35:22 +010072 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
Axel Lin0a479682012-06-12 10:46:43 +080073 if (sel < 0)
74 return sel;
75
76 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +010077}
78
Mark Brownd1c6b4f2009-07-28 15:22:02 +010079static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
80{
81 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
82 struct wm831x *wm831x = ldo->wm831x;
83 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
84 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin9a767d42009-10-16 14:16:15 +020085 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010086
87 ret = wm831x_reg_read(wm831x, on_reg);
88 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +020089 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010090
91 if (!(ret & WM831X_LDO1_ON_MODE))
92 return REGULATOR_MODE_NORMAL;
93
94 ret = wm831x_reg_read(wm831x, ctrl_reg);
95 if (ret < 0)
Roel Kluin9a767d42009-10-16 14:16:15 +020096 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +010097
98 if (ret & WM831X_LDO1_LP_MODE)
99 return REGULATOR_MODE_STANDBY;
100 else
101 return REGULATOR_MODE_IDLE;
102}
103
104static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
105 unsigned int mode)
106{
107 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
108 struct wm831x *wm831x = ldo->wm831x;
109 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
110 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
111 int ret;
112
113
114 switch (mode) {
115 case REGULATOR_MODE_NORMAL:
116 ret = wm831x_set_bits(wm831x, on_reg,
117 WM831X_LDO1_ON_MODE, 0);
118 if (ret < 0)
119 return ret;
120 break;
121
122 case REGULATOR_MODE_IDLE:
123 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800124 WM831X_LDO1_LP_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100125 if (ret < 0)
126 return ret;
127
128 ret = wm831x_set_bits(wm831x, on_reg,
129 WM831X_LDO1_ON_MODE,
130 WM831X_LDO1_ON_MODE);
131 if (ret < 0)
132 return ret;
Axel Line2609992010-09-06 16:48:13 +0800133 break;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100134
135 case REGULATOR_MODE_STANDBY:
136 ret = wm831x_set_bits(wm831x, ctrl_reg,
Axel Line2609992010-09-06 16:48:13 +0800137 WM831X_LDO1_LP_MODE,
138 WM831X_LDO1_LP_MODE);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100139 if (ret < 0)
140 return ret;
141
142 ret = wm831x_set_bits(wm831x, on_reg,
143 WM831X_LDO1_ON_MODE,
144 WM831X_LDO1_ON_MODE);
145 if (ret < 0)
146 return ret;
147 break;
148
149 default:
150 return -EINVAL;
151 }
152
153 return 0;
154}
155
156static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
157{
158 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
159 struct wm831x *wm831x = ldo->wm831x;
160 int mask = 1 << rdev_get_id(rdev);
161 int ret;
162
163 /* Is the regulator on? */
164 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
165 if (ret < 0)
166 return ret;
167 if (!(ret & mask))
168 return REGULATOR_STATUS_OFF;
169
170 /* Is it reporting under voltage? */
171 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
Axel Lin363506c2012-07-31 21:28:26 +0800172 if (ret < 0)
173 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100174 if (ret & mask)
175 return REGULATOR_STATUS_ERROR;
176
177 ret = wm831x_gp_ldo_get_mode(rdev);
178 if (ret < 0)
179 return ret;
180 else
181 return regulator_mode_to_status(ret);
182}
183
184static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
185 int input_uV,
186 int output_uV, int load_uA)
187{
188 if (load_uA < 20000)
189 return REGULATOR_MODE_STANDBY;
190 if (load_uA < 50000)
191 return REGULATOR_MODE_IDLE;
192 return REGULATOR_MODE_NORMAL;
193}
194
195
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100196static const struct regulator_ops wm831x_gp_ldo_ops = {
Mark Brown5ff26a12013-07-02 23:35:22 +0100197 .list_voltage = regulator_list_voltage_linear_range,
198 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownac663b42012-04-15 11:55:55 +0100199 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800200 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100201 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
202 .get_mode = wm831x_gp_ldo_get_mode,
203 .set_mode = wm831x_gp_ldo_set_mode,
204 .get_status = wm831x_gp_ldo_get_status,
205 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
Mark Brown22c5fb62012-08-27 21:52:29 -0700206 .get_bypass = regulator_get_bypass_regmap,
207 .set_bypass = regulator_set_bypass_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100208
Mark Brownca8c3612012-04-15 12:38:52 +0100209 .is_enabled = regulator_is_enabled_regmap,
210 .enable = regulator_enable_regmap,
211 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100212};
213
Bill Pembertona5023572012-11-19 13:22:22 -0500214static int wm831x_gp_ldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100215{
216 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900217 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100218 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100219 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100220 struct wm831x_ldo *ldo;
221 struct resource *res;
222 int ret, irq;
223
Mark Brown137a6352011-07-25 22:20:29 +0100224 if (pdata && pdata->wm831x_num)
225 id = (pdata->wm831x_num * 10) + 1;
226 else
227 id = 0;
228 id = pdev->id - id;
229
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100230 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
231
Mark Brownfded2f42011-12-15 02:11:14 +0800232 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530233 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100234 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100235
236 ldo->wm831x = wm831x;
237
Mark Brown56560982012-08-07 19:42:47 +0100238 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100239 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100240 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100241 ret = -EINVAL;
242 goto err;
243 }
244 ldo->base = res->start;
245
246 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
247 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100248
249 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
250 "LDO%dVDD", id + 1);
251 ldo->desc.supply_name = ldo->supply_name;
252
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100253 ldo->desc.id = id;
254 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100255 ldo->desc.n_voltages = 32;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100256 ldo->desc.ops = &wm831x_gp_ldo_ops;
257 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100258 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
259 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100260 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
261 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700262 ldo->desc.bypass_reg = ldo->base;
263 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
Mark Brown5ff26a12013-07-02 23:35:22 +0100264 ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
265 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100266
Mark Brownc172708d2012-04-04 00:50:22 +0100267 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100268 if (pdata)
269 config.init_data = pdata->ldo[id];
Mark Brownc172708d2012-04-04 00:50:22 +0100270 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100271 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100272
Mark Brownfc7c60e2013-08-31 12:00:17 +0100273 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
274 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100275 if (IS_ERR(ldo->regulator)) {
276 ret = PTR_ERR(ldo->regulator);
277 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
278 id + 1, ret);
279 goto err;
280 }
281
Mark Browncd997582012-05-14 23:14:24 +0200282 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100283 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
284 wm831x_ldo_uv_irq,
Fabio Estevam29454732015-06-01 22:33:53 -0300285 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
286 ldo->name,
Mark Brown41c7a872013-08-31 11:50:00 +0100287 ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100288 if (ret != 0) {
289 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
290 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100291 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100292 }
293
294 platform_set_drvdata(pdev, ldo);
295
296 return 0;
297
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100298err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100299 return ret;
300}
301
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100302static struct platform_driver wm831x_gp_ldo_driver = {
303 .probe = wm831x_gp_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100304 .driver = {
305 .name = "wm831x-ldo",
306 },
307};
308
309/*
310 * Analogue LDOs
311 */
312
Matti Vaittinen60ab7f42020-05-08 18:43:36 +0300313static const struct linear_range wm831x_aldo_ranges[] = {
Axel Lin8828bae2013-10-11 09:32:18 +0800314 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
315 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
Mark Brown5ff26a12013-07-02 23:35:22 +0100316};
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100317
318static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
319 int uV)
320{
321 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800322 struct wm831x *wm831x = ldo->wm831x;
323 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100324
Mark Brown5ff26a12013-07-02 23:35:22 +0100325 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
Axel Lin0a479682012-06-12 10:46:43 +0800326 if (sel < 0)
327 return sel;
328
329 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100330}
331
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100332static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
333{
334 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
335 struct wm831x *wm831x = ldo->wm831x;
336 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
Roel Kluin6f17c652009-12-15 20:07:31 +0100337 int ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100338
339 ret = wm831x_reg_read(wm831x, on_reg);
340 if (ret < 0)
341 return 0;
342
343 if (ret & WM831X_LDO7_ON_MODE)
344 return REGULATOR_MODE_IDLE;
345 else
346 return REGULATOR_MODE_NORMAL;
347}
348
349static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
350 unsigned int mode)
351{
352 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
353 struct wm831x *wm831x = ldo->wm831x;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100354 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
355 int ret;
356
357
358 switch (mode) {
359 case REGULATOR_MODE_NORMAL:
Axel Line841a362012-03-29 15:02:55 +0800360 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100361 if (ret < 0)
362 return ret;
363 break;
364
365 case REGULATOR_MODE_IDLE:
Axel Line841a362012-03-29 15:02:55 +0800366 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100367 WM831X_LDO7_ON_MODE);
368 if (ret < 0)
369 return ret;
370 break;
371
372 default:
373 return -EINVAL;
374 }
375
376 return 0;
377}
378
379static int wm831x_aldo_get_status(struct regulator_dev *rdev)
380{
381 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
382 struct wm831x *wm831x = ldo->wm831x;
383 int mask = 1 << rdev_get_id(rdev);
384 int ret;
385
386 /* Is the regulator on? */
387 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
388 if (ret < 0)
389 return ret;
390 if (!(ret & mask))
391 return REGULATOR_STATUS_OFF;
392
393 /* Is it reporting under voltage? */
394 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
Axel Lin363506c2012-07-31 21:28:26 +0800395 if (ret < 0)
396 return ret;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100397 if (ret & mask)
398 return REGULATOR_STATUS_ERROR;
399
400 ret = wm831x_aldo_get_mode(rdev);
401 if (ret < 0)
402 return ret;
403 else
404 return regulator_mode_to_status(ret);
405}
406
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100407static const struct regulator_ops wm831x_aldo_ops = {
Mark Brown5ff26a12013-07-02 23:35:22 +0100408 .list_voltage = regulator_list_voltage_linear_range,
409 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownac663b42012-04-15 11:55:55 +0100410 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800411 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100412 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
413 .get_mode = wm831x_aldo_get_mode,
414 .set_mode = wm831x_aldo_set_mode,
415 .get_status = wm831x_aldo_get_status,
Mark Brown22c5fb62012-08-27 21:52:29 -0700416 .set_bypass = regulator_set_bypass_regmap,
417 .get_bypass = regulator_get_bypass_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100418
Mark Brownca8c3612012-04-15 12:38:52 +0100419 .is_enabled = regulator_is_enabled_regmap,
420 .enable = regulator_enable_regmap,
421 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100422};
423
Bill Pembertona5023572012-11-19 13:22:22 -0500424static int wm831x_aldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100425{
426 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900427 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100428 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100429 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100430 struct wm831x_ldo *ldo;
431 struct resource *res;
432 int ret, irq;
433
Mark Brown137a6352011-07-25 22:20:29 +0100434 if (pdata && pdata->wm831x_num)
435 id = (pdata->wm831x_num * 10) + 1;
436 else
437 id = 0;
438 id = pdev->id - id;
439
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100440 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
441
Mark Brownfded2f42011-12-15 02:11:14 +0800442 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530443 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100444 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100445
446 ldo->wm831x = wm831x;
447
Mark Brown56560982012-08-07 19:42:47 +0100448 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100449 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100450 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100451 ret = -EINVAL;
452 goto err;
453 }
454 ldo->base = res->start;
455
456 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
457 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100458
459 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
460 "LDO%dVDD", id + 1);
461 ldo->desc.supply_name = ldo->supply_name;
462
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100463 ldo->desc.id = id;
464 ldo->desc.type = REGULATOR_VOLTAGE;
Mark Brown5ff26a12013-07-02 23:35:22 +0100465 ldo->desc.n_voltages = 32;
466 ldo->desc.linear_ranges = wm831x_aldo_ranges;
467 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100468 ldo->desc.ops = &wm831x_aldo_ops;
469 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100470 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
471 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100472 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
473 ldo->desc.enable_mask = 1 << id;
Mark Brown22c5fb62012-08-27 21:52:29 -0700474 ldo->desc.bypass_reg = ldo->base;
475 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100476
Mark Brownc172708d2012-04-04 00:50:22 +0100477 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100478 if (pdata)
479 config.init_data = pdata->ldo[id];
Mark Brownc172708d2012-04-04 00:50:22 +0100480 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100481 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100482
Mark Brownfc7c60e2013-08-31 12:00:17 +0100483 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
484 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100485 if (IS_ERR(ldo->regulator)) {
486 ret = PTR_ERR(ldo->regulator);
487 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
488 id + 1, ret);
489 goto err;
490 }
491
Mark Browncd997582012-05-14 23:14:24 +0200492 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
Mark Brown41c7a872013-08-31 11:50:00 +0100493 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
494 wm831x_ldo_uv_irq,
Fabio Estevam29454732015-06-01 22:33:53 -0300495 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
496 ldo->name, ldo);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100497 if (ret != 0) {
498 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
499 irq, ret);
Mark Brownfc7c60e2013-08-31 12:00:17 +0100500 goto err;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100501 }
502
503 platform_set_drvdata(pdev, ldo);
504
505 return 0;
506
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100507err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100508 return ret;
509}
510
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100511static struct platform_driver wm831x_aldo_driver = {
512 .probe = wm831x_aldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100513 .driver = {
514 .name = "wm831x-aldo",
515 },
516};
517
518/*
519 * Alive LDO
520 */
521
522#define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
523
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100524static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
525 int uV)
526{
527 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
Axel Lin0a479682012-06-12 10:46:43 +0800528 struct wm831x *wm831x = ldo->wm831x;
529 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100530
Axel Lin0a479682012-06-12 10:46:43 +0800531 sel = regulator_map_voltage_linear(rdev, uV, uV);
532 if (sel < 0)
533 return sel;
534
535 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100536}
537
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100538static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
539{
540 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
541 struct wm831x *wm831x = ldo->wm831x;
542 int mask = 1 << rdev_get_id(rdev);
543 int ret;
544
545 /* Is the regulator on? */
546 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
547 if (ret < 0)
548 return ret;
549 if (ret & mask)
550 return REGULATOR_STATUS_ON;
551 else
552 return REGULATOR_STATUS_OFF;
553}
554
Julia Lawallb0d6dd32015-12-19 16:31:24 +0100555static const struct regulator_ops wm831x_alive_ldo_ops = {
Mark Brownd31e9542012-05-09 21:53:32 +0100556 .list_voltage = regulator_list_voltage_linear,
Axel Linc2543b52012-05-31 17:39:00 +0800557 .map_voltage = regulator_map_voltage_linear,
Mark Brownac663b42012-04-15 11:55:55 +0100558 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lin0a479682012-06-12 10:46:43 +0800559 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100560 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
561 .get_status = wm831x_alive_ldo_get_status,
562
Mark Brownca8c3612012-04-15 12:38:52 +0100563 .is_enabled = regulator_is_enabled_regmap,
564 .enable = regulator_enable_regmap,
565 .disable = regulator_disable_regmap,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100566};
567
Bill Pembertona5023572012-11-19 13:22:22 -0500568static int wm831x_alive_ldo_probe(struct platform_device *pdev)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100569{
570 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Handff91d02013-07-30 17:20:47 +0900571 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100572 struct regulator_config config = { };
Mark Brown137a6352011-07-25 22:20:29 +0100573 int id;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100574 struct wm831x_ldo *ldo;
575 struct resource *res;
576 int ret;
577
Mark Brown137a6352011-07-25 22:20:29 +0100578 if (pdata && pdata->wm831x_num)
579 id = (pdata->wm831x_num * 10) + 1;
580 else
581 id = 0;
582 id = pdev->id - id;
583
584
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100585 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
586
Mark Brownfded2f42011-12-15 02:11:14 +0800587 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
Sachin Kamatfae3b832014-02-20 14:23:22 +0530588 if (!ldo)
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100589 return -ENOMEM;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100590
591 ldo->wm831x = wm831x;
592
Mark Brown56560982012-08-07 19:42:47 +0100593 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100594 if (res == NULL) {
Mark Brown56560982012-08-07 19:42:47 +0100595 dev_err(&pdev->dev, "No REG resource\n");
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100596 ret = -EINVAL;
597 goto err;
598 }
599 ldo->base = res->start;
600
601 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
602 ldo->desc.name = ldo->name;
Mark Brownf1aba132012-03-28 21:40:20 +0100603
604 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
605 "LDO%dVDD", id + 1);
606 ldo->desc.supply_name = ldo->supply_name;
607
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100608 ldo->desc.id = id;
609 ldo->desc.type = REGULATOR_VOLTAGE;
610 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
611 ldo->desc.ops = &wm831x_alive_ldo_ops;
612 ldo->desc.owner = THIS_MODULE;
Mark Brownac663b42012-04-15 11:55:55 +0100613 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
614 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
Mark Brownca8c3612012-04-15 12:38:52 +0100615 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
616 ldo->desc.enable_mask = 1 << id;
Mark Brownd31e9542012-05-09 21:53:32 +0100617 ldo->desc.min_uV = 800000;
618 ldo->desc.uV_step = 50000;
Mark Browneefaa3c2012-06-27 15:00:02 +0100619 ldo->desc.enable_time = 1000;
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100620
Mark Brownc172708d2012-04-04 00:50:22 +0100621 config.dev = pdev->dev.parent;
Mark Brownb7ca8782012-05-10 00:41:02 +0100622 if (pdata)
623 config.init_data = pdata->ldo[id];
Mark Brownc172708d2012-04-04 00:50:22 +0100624 config.driver_data = ldo;
Mark Brownac663b42012-04-15 11:55:55 +0100625 config.regmap = wm831x->regmap;
Mark Brownc172708d2012-04-04 00:50:22 +0100626
Mark Brownfc7c60e2013-08-31 12:00:17 +0100627 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
628 &config);
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100629 if (IS_ERR(ldo->regulator)) {
630 ret = PTR_ERR(ldo->regulator);
631 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
632 id + 1, ret);
633 goto err;
634 }
635
636 platform_set_drvdata(pdev, ldo);
637
638 return 0;
639
640err:
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100641 return ret;
642}
643
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100644static struct platform_driver wm831x_alive_ldo_driver = {
645 .probe = wm831x_alive_ldo_probe,
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100646 .driver = {
647 .name = "wm831x-alive-ldo",
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100648 },
649};
650
Thierry Reding92a513b2015-12-02 17:32:53 +0100651static struct platform_driver * const drivers[] = {
652 &wm831x_gp_ldo_driver,
653 &wm831x_aldo_driver,
654 &wm831x_alive_ldo_driver,
655};
656
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100657static int __init wm831x_ldo_init(void)
658{
Thierry Reding92a513b2015-12-02 17:32:53 +0100659 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100660}
661subsys_initcall(wm831x_ldo_init);
662
663static void __exit wm831x_ldo_exit(void)
664{
Thierry Reding92a513b2015-12-02 17:32:53 +0100665 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Mark Brownd1c6b4f2009-07-28 15:22:02 +0100666}
667module_exit(wm831x_ldo_exit);
668
669/* Module information */
670MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
671MODULE_DESCRIPTION("WM831x LDO driver");
672MODULE_LICENSE("GPL");
673MODULE_ALIAS("platform:wm831x-ldo");
674MODULE_ALIAS("platform:wm831x-aldo");
675MODULE_ALIAS("platform:wm831x-aliveldo");