Linux 5.1.15
[linux/fpc-iii.git] / drivers / regulator / wm831x-dcdc.c
blobd1873f94bca74313ed0502ba80f0a4b5b77047ce
1 /*
2 * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
22 #include <linux/regulator/machine.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
26 #include <linux/mfd/wm831x/core.h>
27 #include <linux/mfd/wm831x/regulator.h>
28 #include <linux/mfd/wm831x/pdata.h>
30 #define WM831X_BUCKV_MAX_SELECTOR 0x68
31 #define WM831X_BUCKP_MAX_SELECTOR 0x66
33 #define WM831X_DCDC_MODE_FAST 0
34 #define WM831X_DCDC_MODE_NORMAL 1
35 #define WM831X_DCDC_MODE_IDLE 2
36 #define WM831X_DCDC_MODE_STANDBY 3
38 #define WM831X_DCDC_MAX_NAME 9
40 /* Register offsets in control block */
41 #define WM831X_DCDC_CONTROL_1 0
42 #define WM831X_DCDC_CONTROL_2 1
43 #define WM831X_DCDC_ON_CONFIG 2
44 #define WM831X_DCDC_SLEEP_CONTROL 3
45 #define WM831X_DCDC_DVS_CONTROL 4
48 * Shared
51 struct wm831x_dcdc {
52 char name[WM831X_DCDC_MAX_NAME];
53 char supply_name[WM831X_DCDC_MAX_NAME];
54 struct regulator_desc desc;
55 int base;
56 struct wm831x *wm831x;
57 struct regulator_dev *regulator;
58 int dvs_gpio;
59 int dvs_gpio_state;
60 int on_vsel;
61 int dvs_vsel;
64 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
67 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
68 struct wm831x *wm831x = dcdc->wm831x;
69 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
70 int val;
72 val = wm831x_reg_read(wm831x, reg);
73 if (val < 0)
74 return val;
76 val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
78 switch (val) {
79 case WM831X_DCDC_MODE_FAST:
80 return REGULATOR_MODE_FAST;
81 case WM831X_DCDC_MODE_NORMAL:
82 return REGULATOR_MODE_NORMAL;
83 case WM831X_DCDC_MODE_STANDBY:
84 return REGULATOR_MODE_STANDBY;
85 case WM831X_DCDC_MODE_IDLE:
86 return REGULATOR_MODE_IDLE;
87 default:
88 BUG();
89 return -EINVAL;
93 static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
94 unsigned int mode)
96 int val;
98 switch (mode) {
99 case REGULATOR_MODE_FAST:
100 val = WM831X_DCDC_MODE_FAST;
101 break;
102 case REGULATOR_MODE_NORMAL:
103 val = WM831X_DCDC_MODE_NORMAL;
104 break;
105 case REGULATOR_MODE_STANDBY:
106 val = WM831X_DCDC_MODE_STANDBY;
107 break;
108 case REGULATOR_MODE_IDLE:
109 val = WM831X_DCDC_MODE_IDLE;
110 break;
111 default:
112 return -EINVAL;
115 return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
116 val << WM831X_DC1_ON_MODE_SHIFT);
119 static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
121 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
122 struct wm831x *wm831x = dcdc->wm831x;
123 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
125 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
128 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
129 unsigned int mode)
131 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
132 struct wm831x *wm831x = dcdc->wm831x;
133 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
135 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
138 static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
140 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
141 struct wm831x *wm831x = dcdc->wm831x;
142 int ret;
144 /* First, check for errors */
145 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
146 if (ret < 0)
147 return ret;
149 if (ret & (1 << rdev_get_id(rdev))) {
150 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
151 rdev_get_id(rdev) + 1);
152 return REGULATOR_STATUS_ERROR;
155 /* DCDC1 and DCDC2 can additionally detect high voltage/current */
156 if (rdev_get_id(rdev) < 2) {
157 if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
158 dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
159 rdev_get_id(rdev) + 1);
160 return REGULATOR_STATUS_ERROR;
163 if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
164 dev_dbg(wm831x->dev, "DCDC%d over current\n",
165 rdev_get_id(rdev) + 1);
166 return REGULATOR_STATUS_ERROR;
170 /* Is the regulator on? */
171 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
172 if (ret < 0)
173 return ret;
174 if (!(ret & (1 << rdev_get_id(rdev))))
175 return REGULATOR_STATUS_OFF;
177 /* TODO: When we handle hardware control modes so we can report the
178 * current mode. */
179 return REGULATOR_STATUS_ON;
182 static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
184 struct wm831x_dcdc *dcdc = data;
186 regulator_lock(dcdc->regulator);
187 regulator_notifier_call_chain(dcdc->regulator,
188 REGULATOR_EVENT_UNDER_VOLTAGE,
189 NULL);
190 regulator_unlock(dcdc->regulator);
192 return IRQ_HANDLED;
195 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
197 struct wm831x_dcdc *dcdc = data;
199 regulator_lock(dcdc->regulator);
200 regulator_notifier_call_chain(dcdc->regulator,
201 REGULATOR_EVENT_OVER_CURRENT,
202 NULL);
203 regulator_unlock(dcdc->regulator);
205 return IRQ_HANDLED;
209 * BUCKV specifics
212 static const struct regulator_linear_range wm831x_buckv_ranges[] = {
213 REGULATOR_LINEAR_RANGE(600000, 0, 0x7, 0),
214 REGULATOR_LINEAR_RANGE(600000, 0x8, 0x68, 12500),
217 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
219 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
221 if (state == dcdc->dvs_gpio_state)
222 return 0;
224 dcdc->dvs_gpio_state = state;
225 gpio_set_value(dcdc->dvs_gpio, state);
227 /* Should wait for DVS state change to be asserted if we have
228 * a GPIO for it, for now assume the device is configured
229 * for the fastest possible transition.
232 return 0;
235 static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
236 unsigned vsel)
238 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
239 struct wm831x *wm831x = dcdc->wm831x;
240 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
241 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
242 int ret;
244 /* If this value is already set then do a GPIO update if we can */
245 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
246 return wm831x_buckv_set_dvs(rdev, 0);
248 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
249 return wm831x_buckv_set_dvs(rdev, 1);
251 /* Always set the ON status to the minimum voltage */
252 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
253 if (ret < 0)
254 return ret;
255 dcdc->on_vsel = vsel;
257 if (!dcdc->dvs_gpio)
258 return ret;
260 /* Kick the voltage transition now */
261 ret = wm831x_buckv_set_dvs(rdev, 0);
262 if (ret < 0)
263 return ret;
266 * If this VSEL is higher than the last one we've seen then
267 * remember it as the DVS VSEL. This is optimised for CPUfreq
268 * usage where we want to get to the highest voltage very
269 * quickly.
271 if (vsel > dcdc->dvs_vsel) {
272 ret = wm831x_set_bits(wm831x, dvs_reg,
273 WM831X_DC1_DVS_VSEL_MASK,
274 vsel);
275 if (ret == 0)
276 dcdc->dvs_vsel = vsel;
277 else
278 dev_warn(wm831x->dev,
279 "Failed to set DCDC DVS VSEL: %d\n", ret);
282 return 0;
285 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
286 int uV)
288 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
289 struct wm831x *wm831x = dcdc->wm831x;
290 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
291 int vsel;
293 vsel = regulator_map_voltage_linear_range(rdev, uV, uV);
294 if (vsel < 0)
295 return vsel;
297 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
300 static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
302 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
304 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
305 return dcdc->dvs_vsel;
306 else
307 return dcdc->on_vsel;
310 /* Current limit options */
311 static const unsigned int wm831x_dcdc_ilim[] = {
312 125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000
315 static const struct regulator_ops wm831x_buckv_ops = {
316 .set_voltage_sel = wm831x_buckv_set_voltage_sel,
317 .get_voltage_sel = wm831x_buckv_get_voltage_sel,
318 .list_voltage = regulator_list_voltage_linear_range,
319 .map_voltage = regulator_map_voltage_linear_range,
320 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
321 .set_current_limit = regulator_set_current_limit_regmap,
322 .get_current_limit = regulator_get_current_limit_regmap,
324 .is_enabled = regulator_is_enabled_regmap,
325 .enable = regulator_enable_regmap,
326 .disable = regulator_disable_regmap,
327 .get_status = wm831x_dcdc_get_status,
328 .get_mode = wm831x_dcdc_get_mode,
329 .set_mode = wm831x_dcdc_set_mode,
330 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
334 * Set up DVS control. We just log errors since we can still run
335 * (with reduced performance) if we fail.
337 static void wm831x_buckv_dvs_init(struct platform_device *pdev,
338 struct wm831x_dcdc *dcdc,
339 struct wm831x_buckv_pdata *pdata)
341 struct wm831x *wm831x = dcdc->wm831x;
342 int ret;
343 u16 ctrl;
345 if (!pdata || !pdata->dvs_gpio)
346 return;
348 /* gpiolib won't let us read the GPIO status so pick the higher
349 * of the two existing voltages so we take it as platform data.
351 dcdc->dvs_gpio_state = pdata->dvs_init_state;
353 ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio,
354 dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
355 "DCDC DVS");
356 if (ret < 0) {
357 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
358 dcdc->name, ret);
359 return;
362 dcdc->dvs_gpio = pdata->dvs_gpio;
364 switch (pdata->dvs_control_src) {
365 case 1:
366 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
367 break;
368 case 2:
369 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
370 break;
371 default:
372 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
373 pdata->dvs_control_src, dcdc->name);
374 return;
377 /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
378 * to make bootstrapping a bit smoother.
380 if (!dcdc->dvs_vsel) {
381 ret = wm831x_set_bits(wm831x,
382 dcdc->base + WM831X_DCDC_DVS_CONTROL,
383 WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
384 if (ret == 0)
385 dcdc->dvs_vsel = dcdc->on_vsel;
386 else
387 dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
388 ret);
391 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
392 WM831X_DC1_DVS_SRC_MASK, ctrl);
393 if (ret < 0) {
394 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
395 dcdc->name, ret);
399 static int wm831x_buckv_probe(struct platform_device *pdev)
401 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
402 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
403 struct regulator_config config = { };
404 int id;
405 struct wm831x_dcdc *dcdc;
406 struct resource *res;
407 int ret, irq;
409 if (pdata && pdata->wm831x_num)
410 id = (pdata->wm831x_num * 10) + 1;
411 else
412 id = 0;
413 id = pdev->id - id;
415 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
417 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
418 GFP_KERNEL);
419 if (!dcdc)
420 return -ENOMEM;
422 dcdc->wm831x = wm831x;
424 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
425 if (res == NULL) {
426 dev_err(&pdev->dev, "No REG resource\n");
427 ret = -EINVAL;
428 goto err;
430 dcdc->base = res->start;
432 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
433 dcdc->desc.name = dcdc->name;
435 snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
436 "DC%dVDD", id + 1);
437 dcdc->desc.supply_name = dcdc->supply_name;
439 dcdc->desc.id = id;
440 dcdc->desc.type = REGULATOR_VOLTAGE;
441 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
442 dcdc->desc.linear_ranges = wm831x_buckv_ranges;
443 dcdc->desc.n_linear_ranges = ARRAY_SIZE(wm831x_buckv_ranges);
444 dcdc->desc.ops = &wm831x_buckv_ops;
445 dcdc->desc.owner = THIS_MODULE;
446 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
447 dcdc->desc.enable_mask = 1 << id;
448 dcdc->desc.csel_reg = dcdc->base + WM831X_DCDC_CONTROL_2;
449 dcdc->desc.csel_mask = WM831X_DC1_HC_THR_MASK;
450 dcdc->desc.n_current_limits = ARRAY_SIZE(wm831x_dcdc_ilim);
451 dcdc->desc.curr_table = wm831x_dcdc_ilim;
453 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
454 if (ret < 0) {
455 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
456 goto err;
458 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
460 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
461 if (ret < 0) {
462 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
463 goto err;
465 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
467 if (pdata && pdata->dcdc[id])
468 wm831x_buckv_dvs_init(pdev, dcdc,
469 pdata->dcdc[id]->driver_data);
471 config.dev = pdev->dev.parent;
472 if (pdata)
473 config.init_data = pdata->dcdc[id];
474 config.driver_data = dcdc;
475 config.regmap = wm831x->regmap;
477 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
478 &config);
479 if (IS_ERR(dcdc->regulator)) {
480 ret = PTR_ERR(dcdc->regulator);
481 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
482 id + 1, ret);
483 goto err;
486 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
487 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
488 wm831x_dcdc_uv_irq,
489 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
490 dcdc->name, dcdc);
491 if (ret != 0) {
492 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
493 irq, ret);
494 goto err;
497 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
498 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
499 wm831x_dcdc_oc_irq,
500 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
501 dcdc->name, dcdc);
502 if (ret != 0) {
503 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
504 irq, ret);
505 goto err;
508 platform_set_drvdata(pdev, dcdc);
510 return 0;
512 err:
513 return ret;
516 static struct platform_driver wm831x_buckv_driver = {
517 .probe = wm831x_buckv_probe,
518 .driver = {
519 .name = "wm831x-buckv",
524 * BUCKP specifics
527 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
529 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
530 struct wm831x *wm831x = dcdc->wm831x;
531 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
532 int sel;
534 sel = regulator_map_voltage_linear(rdev, uV, uV);
535 if (sel < 0)
536 return sel;
538 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
541 static const struct regulator_ops wm831x_buckp_ops = {
542 .set_voltage_sel = regulator_set_voltage_sel_regmap,
543 .get_voltage_sel = regulator_get_voltage_sel_regmap,
544 .list_voltage = regulator_list_voltage_linear,
545 .map_voltage = regulator_map_voltage_linear,
546 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
548 .is_enabled = regulator_is_enabled_regmap,
549 .enable = regulator_enable_regmap,
550 .disable = regulator_disable_regmap,
551 .get_status = wm831x_dcdc_get_status,
552 .get_mode = wm831x_dcdc_get_mode,
553 .set_mode = wm831x_dcdc_set_mode,
554 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
557 static int wm831x_buckp_probe(struct platform_device *pdev)
559 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
560 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
561 struct regulator_config config = { };
562 int id;
563 struct wm831x_dcdc *dcdc;
564 struct resource *res;
565 int ret, irq;
567 if (pdata && pdata->wm831x_num)
568 id = (pdata->wm831x_num * 10) + 1;
569 else
570 id = 0;
571 id = pdev->id - id;
573 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
575 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
576 GFP_KERNEL);
577 if (!dcdc)
578 return -ENOMEM;
580 dcdc->wm831x = wm831x;
582 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
583 if (res == NULL) {
584 dev_err(&pdev->dev, "No REG resource\n");
585 ret = -EINVAL;
586 goto err;
588 dcdc->base = res->start;
590 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
591 dcdc->desc.name = dcdc->name;
593 snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
594 "DC%dVDD", id + 1);
595 dcdc->desc.supply_name = dcdc->supply_name;
597 dcdc->desc.id = id;
598 dcdc->desc.type = REGULATOR_VOLTAGE;
599 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
600 dcdc->desc.ops = &wm831x_buckp_ops;
601 dcdc->desc.owner = THIS_MODULE;
602 dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
603 dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
604 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
605 dcdc->desc.enable_mask = 1 << id;
606 dcdc->desc.min_uV = 850000;
607 dcdc->desc.uV_step = 25000;
609 config.dev = pdev->dev.parent;
610 if (pdata)
611 config.init_data = pdata->dcdc[id];
612 config.driver_data = dcdc;
613 config.regmap = wm831x->regmap;
615 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
616 &config);
617 if (IS_ERR(dcdc->regulator)) {
618 ret = PTR_ERR(dcdc->regulator);
619 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
620 id + 1, ret);
621 goto err;
624 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
625 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
626 wm831x_dcdc_uv_irq,
627 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
628 dcdc->name, dcdc);
629 if (ret != 0) {
630 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
631 irq, ret);
632 goto err;
635 platform_set_drvdata(pdev, dcdc);
637 return 0;
639 err:
640 return ret;
643 static struct platform_driver wm831x_buckp_driver = {
644 .probe = wm831x_buckp_probe,
645 .driver = {
646 .name = "wm831x-buckp",
651 * DCDC boost convertors
654 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
656 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
657 struct wm831x *wm831x = dcdc->wm831x;
658 int ret;
660 /* First, check for errors */
661 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
662 if (ret < 0)
663 return ret;
665 if (ret & (1 << rdev_get_id(rdev))) {
666 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
667 rdev_get_id(rdev) + 1);
668 return REGULATOR_STATUS_ERROR;
671 /* Is the regulator on? */
672 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
673 if (ret < 0)
674 return ret;
675 if (ret & (1 << rdev_get_id(rdev)))
676 return REGULATOR_STATUS_ON;
677 else
678 return REGULATOR_STATUS_OFF;
681 static const struct regulator_ops wm831x_boostp_ops = {
682 .get_status = wm831x_boostp_get_status,
684 .is_enabled = regulator_is_enabled_regmap,
685 .enable = regulator_enable_regmap,
686 .disable = regulator_disable_regmap,
689 static int wm831x_boostp_probe(struct platform_device *pdev)
691 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
692 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
693 struct regulator_config config = { };
694 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
695 struct wm831x_dcdc *dcdc;
696 struct resource *res;
697 int ret, irq;
699 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
701 if (pdata == NULL || pdata->dcdc[id] == NULL)
702 return -ENODEV;
704 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
705 if (!dcdc)
706 return -ENOMEM;
708 dcdc->wm831x = wm831x;
710 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
711 if (res == NULL) {
712 dev_err(&pdev->dev, "No REG resource\n");
713 return -EINVAL;
715 dcdc->base = res->start;
717 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
718 dcdc->desc.name = dcdc->name;
719 dcdc->desc.id = id;
720 dcdc->desc.type = REGULATOR_VOLTAGE;
721 dcdc->desc.ops = &wm831x_boostp_ops;
722 dcdc->desc.owner = THIS_MODULE;
723 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
724 dcdc->desc.enable_mask = 1 << id;
726 config.dev = pdev->dev.parent;
727 if (pdata)
728 config.init_data = pdata->dcdc[id];
729 config.driver_data = dcdc;
730 config.regmap = wm831x->regmap;
732 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
733 &config);
734 if (IS_ERR(dcdc->regulator)) {
735 ret = PTR_ERR(dcdc->regulator);
736 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
737 id + 1, ret);
738 return ret;
741 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
742 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
743 wm831x_dcdc_uv_irq,
744 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
745 dcdc->name,
746 dcdc);
747 if (ret != 0) {
748 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
749 irq, ret);
750 return ret;
753 platform_set_drvdata(pdev, dcdc);
755 return 0;
758 static struct platform_driver wm831x_boostp_driver = {
759 .probe = wm831x_boostp_probe,
760 .driver = {
761 .name = "wm831x-boostp",
766 * External Power Enable
768 * These aren't actually DCDCs but look like them in hardware so share
769 * code.
772 #define WM831X_EPE_BASE 6
774 static const struct regulator_ops wm831x_epe_ops = {
775 .is_enabled = regulator_is_enabled_regmap,
776 .enable = regulator_enable_regmap,
777 .disable = regulator_disable_regmap,
778 .get_status = wm831x_dcdc_get_status,
781 static int wm831x_epe_probe(struct platform_device *pdev)
783 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
784 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
785 struct regulator_config config = { };
786 int id = pdev->id % ARRAY_SIZE(pdata->epe);
787 struct wm831x_dcdc *dcdc;
788 int ret;
790 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
792 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
793 if (!dcdc)
794 return -ENOMEM;
796 dcdc->wm831x = wm831x;
798 /* For current parts this is correct; probably need to revisit
799 * in future.
801 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
802 dcdc->desc.name = dcdc->name;
803 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
804 dcdc->desc.ops = &wm831x_epe_ops;
805 dcdc->desc.type = REGULATOR_VOLTAGE;
806 dcdc->desc.owner = THIS_MODULE;
807 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
808 dcdc->desc.enable_mask = 1 << dcdc->desc.id;
810 config.dev = pdev->dev.parent;
811 if (pdata)
812 config.init_data = pdata->epe[id];
813 config.driver_data = dcdc;
814 config.regmap = wm831x->regmap;
816 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
817 &config);
818 if (IS_ERR(dcdc->regulator)) {
819 ret = PTR_ERR(dcdc->regulator);
820 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
821 id + 1, ret);
822 goto err;
825 platform_set_drvdata(pdev, dcdc);
827 return 0;
829 err:
830 return ret;
833 static struct platform_driver wm831x_epe_driver = {
834 .probe = wm831x_epe_probe,
835 .driver = {
836 .name = "wm831x-epe",
840 static struct platform_driver * const drivers[] = {
841 &wm831x_buckv_driver,
842 &wm831x_buckp_driver,
843 &wm831x_boostp_driver,
844 &wm831x_epe_driver,
847 static int __init wm831x_dcdc_init(void)
849 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
851 subsys_initcall(wm831x_dcdc_init);
853 static void __exit wm831x_dcdc_exit(void)
855 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
857 module_exit(wm831x_dcdc_exit);
859 /* Module information */
860 MODULE_AUTHOR("Mark Brown");
861 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
862 MODULE_LICENSE("GPL");
863 MODULE_ALIAS("platform:wm831x-buckv");
864 MODULE_ALIAS("platform:wm831x-buckp");
865 MODULE_ALIAS("platform:wm831x-boostp");
866 MODULE_ALIAS("platform:wm831x-epe");