1 // 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>
8 
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>
17 #include <linux/slab.h>
18 
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/regulator.h>
21 #include <linux/mfd/wm831x/pdata.h>
22 
23 #define WM831X_LDO_MAX_NAME 9
24 
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 
32 struct wm831x_ldo {
33 	char name[WM831X_LDO_MAX_NAME];
34 	char supply_name[WM831X_LDO_MAX_NAME];
35 	struct regulator_desc desc;
36 	int base;
37 	struct wm831x *wm831x;
38 	struct regulator_dev *regulator;
39 };
40 
41 /*
42  * Shared
43  */
44 
wm831x_ldo_uv_irq(int irq,void * data)45 static 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 
60 static const struct linear_range wm831x_gp_ldo_ranges[] = {
61 	REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
62 	REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
63 };
64 
wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev * rdev,int uV)65 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
66 					     int uV)
67 {
68 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
69 	struct wm831x *wm831x = ldo->wm831x;
70 	int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
71 
72 	sel = regulator_map_voltage_linear_range(rdev, uV, uV);
73 	if (sel < 0)
74 		return sel;
75 
76 	return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
77 }
78 
wm831x_gp_ldo_get_mode(struct regulator_dev * rdev)79 static 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;
85 	int ret;
86 
87 	ret = wm831x_reg_read(wm831x, on_reg);
88 	if (ret < 0)
89 		return ret;
90 
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)
96 		return ret;
97 
98 	if (ret & WM831X_LDO1_LP_MODE)
99 		return REGULATOR_MODE_STANDBY;
100 	else
101 		return REGULATOR_MODE_IDLE;
102 }
103 
wm831x_gp_ldo_set_mode(struct regulator_dev * rdev,unsigned int mode)104 static 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,
124 				      WM831X_LDO1_LP_MODE, 0);
125 		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;
133 		break;
134 
135 	case REGULATOR_MODE_STANDBY:
136 		ret = wm831x_set_bits(wm831x, ctrl_reg,
137 				      WM831X_LDO1_LP_MODE,
138 				      WM831X_LDO1_LP_MODE);
139 		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 
wm831x_gp_ldo_get_status(struct regulator_dev * rdev)156 static 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);
172 	if (ret < 0)
173 		return ret;
174 	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 
wm831x_gp_ldo_get_optimum_mode(struct regulator_dev * rdev,int input_uV,int output_uV,int load_uA)184 static 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 
196 static const struct regulator_ops wm831x_gp_ldo_ops = {
197 	.list_voltage = regulator_list_voltage_linear_range,
198 	.map_voltage = regulator_map_voltage_linear_range,
199 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
200 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
201 	.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,
206 	.get_bypass = regulator_get_bypass_regmap,
207 	.set_bypass = regulator_set_bypass_regmap,
208 
209 	.is_enabled = regulator_is_enabled_regmap,
210 	.enable = regulator_enable_regmap,
211 	.disable = regulator_disable_regmap,
212 };
213 
wm831x_gp_ldo_probe(struct platform_device * pdev)214 static int wm831x_gp_ldo_probe(struct platform_device *pdev)
215 {
216 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
217 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
218 	struct regulator_config config = { };
219 	int id;
220 	struct wm831x_ldo *ldo;
221 	struct resource *res;
222 	int ret, irq;
223 
224 	if (pdata && pdata->wm831x_num)
225 		id = (pdata->wm831x_num * 10) + 1;
226 	else
227 		id = 0;
228 	id = pdev->id - id;
229 
230 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
231 
232 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
233 	if (!ldo)
234 		return -ENOMEM;
235 
236 	ldo->wm831x = wm831x;
237 
238 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
239 	if (res == NULL) {
240 		dev_err(&pdev->dev, "No REG resource\n");
241 		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;
248 
249 	snprintf(ldo->supply_name, sizeof(ldo->supply_name),
250 		 "LDO%dVDD", id + 1);
251 	ldo->desc.supply_name = ldo->supply_name;
252 
253 	ldo->desc.id = id;
254 	ldo->desc.type = REGULATOR_VOLTAGE;
255 	ldo->desc.n_voltages = 32;
256 	ldo->desc.ops = &wm831x_gp_ldo_ops;
257 	ldo->desc.owner = THIS_MODULE;
258 	ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
259 	ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
260 	ldo->desc.enable_reg = WM831X_LDO_ENABLE;
261 	ldo->desc.enable_mask = 1 << id;
262 	ldo->desc.bypass_reg = ldo->base;
263 	ldo->desc.bypass_mask = WM831X_LDO1_SWI;
264 	ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
265 	ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
266 
267 	config.dev = pdev->dev.parent;
268 	if (pdata)
269 		config.init_data = pdata->ldo[id];
270 	config.driver_data = ldo;
271 	config.regmap = wm831x->regmap;
272 
273 	ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
274 						 &config);
275 	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 
282 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
283 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
284 					wm831x_ldo_uv_irq,
285 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
286 					ldo->name,
287 					ldo);
288 	if (ret != 0) {
289 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
290 			irq, ret);
291 		goto err;
292 	}
293 
294 	platform_set_drvdata(pdev, ldo);
295 
296 	return 0;
297 
298 err:
299 	return ret;
300 }
301 
302 static struct platform_driver wm831x_gp_ldo_driver = {
303 	.probe = wm831x_gp_ldo_probe,
304 	.driver		= {
305 		.name	= "wm831x-ldo",
306 	},
307 };
308 
309 /*
310  * Analogue LDOs
311  */
312 
313 static const struct linear_range wm831x_aldo_ranges[] = {
314 	REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
315 	REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
316 };
317 
wm831x_aldo_set_suspend_voltage(struct regulator_dev * rdev,int uV)318 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
319 					     int uV)
320 {
321 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
322 	struct wm831x *wm831x = ldo->wm831x;
323 	int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
324 
325 	sel = regulator_map_voltage_linear_range(rdev, uV, uV);
326 	if (sel < 0)
327 		return sel;
328 
329 	return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
330 }
331 
wm831x_aldo_get_mode(struct regulator_dev * rdev)332 static 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;
337 	int ret;
338 
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 
wm831x_aldo_set_mode(struct regulator_dev * rdev,unsigned int mode)349 static 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;
354 	int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
355 	int ret;
356 
357 
358 	switch (mode) {
359 	case REGULATOR_MODE_NORMAL:
360 		ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
361 		if (ret < 0)
362 			return ret;
363 		break;
364 
365 	case REGULATOR_MODE_IDLE:
366 		ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
367 				      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 
wm831x_aldo_get_status(struct regulator_dev * rdev)379 static 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);
395 	if (ret < 0)
396 		return ret;
397 	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 
407 static const struct regulator_ops wm831x_aldo_ops = {
408 	.list_voltage = regulator_list_voltage_linear_range,
409 	.map_voltage = regulator_map_voltage_linear_range,
410 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
411 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
412 	.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,
416 	.set_bypass = regulator_set_bypass_regmap,
417 	.get_bypass = regulator_get_bypass_regmap,
418 
419 	.is_enabled = regulator_is_enabled_regmap,
420 	.enable = regulator_enable_regmap,
421 	.disable = regulator_disable_regmap,
422 };
423 
wm831x_aldo_probe(struct platform_device * pdev)424 static int wm831x_aldo_probe(struct platform_device *pdev)
425 {
426 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
427 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
428 	struct regulator_config config = { };
429 	int id;
430 	struct wm831x_ldo *ldo;
431 	struct resource *res;
432 	int ret, irq;
433 
434 	if (pdata && pdata->wm831x_num)
435 		id = (pdata->wm831x_num * 10) + 1;
436 	else
437 		id = 0;
438 	id = pdev->id - id;
439 
440 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
441 
442 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
443 	if (!ldo)
444 		return -ENOMEM;
445 
446 	ldo->wm831x = wm831x;
447 
448 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
449 	if (res == NULL) {
450 		dev_err(&pdev->dev, "No REG resource\n");
451 		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;
458 
459 	snprintf(ldo->supply_name, sizeof(ldo->supply_name),
460 		 "LDO%dVDD", id + 1);
461 	ldo->desc.supply_name = ldo->supply_name;
462 
463 	ldo->desc.id = id;
464 	ldo->desc.type = REGULATOR_VOLTAGE;
465 	ldo->desc.n_voltages = 32;
466 	ldo->desc.linear_ranges = wm831x_aldo_ranges;
467 	ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
468 	ldo->desc.ops = &wm831x_aldo_ops;
469 	ldo->desc.owner = THIS_MODULE;
470 	ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
471 	ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
472 	ldo->desc.enable_reg = WM831X_LDO_ENABLE;
473 	ldo->desc.enable_mask = 1 << id;
474 	ldo->desc.bypass_reg = ldo->base;
475 	ldo->desc.bypass_mask = WM831X_LDO7_SWI;
476 
477 	config.dev = pdev->dev.parent;
478 	if (pdata)
479 		config.init_data = pdata->ldo[id];
480 	config.driver_data = ldo;
481 	config.regmap = wm831x->regmap;
482 
483 	ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
484 						 &config);
485 	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 
492 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
493 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
494 					wm831x_ldo_uv_irq,
495 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
496 					ldo->name, ldo);
497 	if (ret != 0) {
498 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
499 			irq, ret);
500 		goto err;
501 	}
502 
503 	platform_set_drvdata(pdev, ldo);
504 
505 	return 0;
506 
507 err:
508 	return ret;
509 }
510 
511 static struct platform_driver wm831x_aldo_driver = {
512 	.probe = wm831x_aldo_probe,
513 	.driver		= {
514 		.name	= "wm831x-aldo",
515 	},
516 };
517 
518 /*
519  * Alive LDO
520  */
521 
522 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
523 
wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev * rdev,int uV)524 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
525 					     int uV)
526 {
527 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
528 	struct wm831x *wm831x = ldo->wm831x;
529 	int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
530 
531 	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);
536 }
537 
wm831x_alive_ldo_get_status(struct regulator_dev * rdev)538 static 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 
555 static const struct regulator_ops wm831x_alive_ldo_ops = {
556 	.list_voltage = regulator_list_voltage_linear,
557 	.map_voltage = regulator_map_voltage_linear,
558 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
559 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
560 	.set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
561 	.get_status = wm831x_alive_ldo_get_status,
562 
563 	.is_enabled = regulator_is_enabled_regmap,
564 	.enable = regulator_enable_regmap,
565 	.disable = regulator_disable_regmap,
566 };
567 
wm831x_alive_ldo_probe(struct platform_device * pdev)568 static int wm831x_alive_ldo_probe(struct platform_device *pdev)
569 {
570 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
571 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
572 	struct regulator_config config = { };
573 	int id;
574 	struct wm831x_ldo *ldo;
575 	struct resource *res;
576 	int ret;
577 
578 	if (pdata && pdata->wm831x_num)
579 		id = (pdata->wm831x_num * 10) + 1;
580 	else
581 		id = 0;
582 	id = pdev->id - id;
583 
584 
585 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
586 
587 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
588 	if (!ldo)
589 		return -ENOMEM;
590 
591 	ldo->wm831x = wm831x;
592 
593 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
594 	if (res == NULL) {
595 		dev_err(&pdev->dev, "No REG resource\n");
596 		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;
603 
604 	snprintf(ldo->supply_name, sizeof(ldo->supply_name),
605 		 "LDO%dVDD", id + 1);
606 	ldo->desc.supply_name = ldo->supply_name;
607 
608 	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;
613 	ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
614 	ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
615 	ldo->desc.enable_reg = WM831X_LDO_ENABLE;
616 	ldo->desc.enable_mask = 1 << id;
617 	ldo->desc.min_uV = 800000;
618 	ldo->desc.uV_step = 50000;
619 	ldo->desc.enable_time = 1000;
620 
621 	config.dev = pdev->dev.parent;
622 	if (pdata)
623 		config.init_data = pdata->ldo[id];
624 	config.driver_data = ldo;
625 	config.regmap = wm831x->regmap;
626 
627 	ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
628 						 &config);
629 	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 
640 err:
641 	return ret;
642 }
643 
644 static struct platform_driver wm831x_alive_ldo_driver = {
645 	.probe = wm831x_alive_ldo_probe,
646 	.driver		= {
647 		.name	= "wm831x-alive-ldo",
648 	},
649 };
650 
651 static struct platform_driver * const drivers[] = {
652 	&wm831x_gp_ldo_driver,
653 	&wm831x_aldo_driver,
654 	&wm831x_alive_ldo_driver,
655 };
656 
wm831x_ldo_init(void)657 static int __init wm831x_ldo_init(void)
658 {
659 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
660 }
661 subsys_initcall(wm831x_ldo_init);
662 
wm831x_ldo_exit(void)663 static void __exit wm831x_ldo_exit(void)
664 {
665 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
666 }
667 module_exit(wm831x_ldo_exit);
668 
669 /* Module information */
670 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
671 MODULE_DESCRIPTION("WM831x LDO driver");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:wm831x-ldo");
674 MODULE_ALIAS("platform:wm831x-aldo");
675 MODULE_ALIAS("platform:wm831x-aliveldo");
676