blk-mq: always free hctx after request queue is freed
[linux/fpc-iii.git] / drivers / regulator / wm831x-dcdc.c
blob12b422373580c394b0d978763763150fcccd724d
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_notifier_call_chain(dcdc->regulator,
187 REGULATOR_EVENT_UNDER_VOLTAGE,
188 NULL);
190 return IRQ_HANDLED;
193 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
195 struct wm831x_dcdc *dcdc = data;
197 regulator_notifier_call_chain(dcdc->regulator,
198 REGULATOR_EVENT_OVER_CURRENT,
199 NULL);
201 return IRQ_HANDLED;
205 * BUCKV specifics
208 static const struct regulator_linear_range wm831x_buckv_ranges[] = {
209 REGULATOR_LINEAR_RANGE(600000, 0, 0x7, 0),
210 REGULATOR_LINEAR_RANGE(600000, 0x8, 0x68, 12500),
213 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
215 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
217 if (state == dcdc->dvs_gpio_state)
218 return 0;
220 dcdc->dvs_gpio_state = state;
221 gpio_set_value(dcdc->dvs_gpio, state);
223 /* Should wait for DVS state change to be asserted if we have
224 * a GPIO for it, for now assume the device is configured
225 * for the fastest possible transition.
228 return 0;
231 static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
232 unsigned vsel)
234 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
235 struct wm831x *wm831x = dcdc->wm831x;
236 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
237 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
238 int ret;
240 /* If this value is already set then do a GPIO update if we can */
241 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
242 return wm831x_buckv_set_dvs(rdev, 0);
244 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
245 return wm831x_buckv_set_dvs(rdev, 1);
247 /* Always set the ON status to the minimum voltage */
248 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
249 if (ret < 0)
250 return ret;
251 dcdc->on_vsel = vsel;
253 if (!dcdc->dvs_gpio)
254 return ret;
256 /* Kick the voltage transition now */
257 ret = wm831x_buckv_set_dvs(rdev, 0);
258 if (ret < 0)
259 return ret;
262 * If this VSEL is higher than the last one we've seen then
263 * remember it as the DVS VSEL. This is optimised for CPUfreq
264 * usage where we want to get to the highest voltage very
265 * quickly.
267 if (vsel > dcdc->dvs_vsel) {
268 ret = wm831x_set_bits(wm831x, dvs_reg,
269 WM831X_DC1_DVS_VSEL_MASK,
270 vsel);
271 if (ret == 0)
272 dcdc->dvs_vsel = vsel;
273 else
274 dev_warn(wm831x->dev,
275 "Failed to set DCDC DVS VSEL: %d\n", ret);
278 return 0;
281 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
282 int uV)
284 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
285 struct wm831x *wm831x = dcdc->wm831x;
286 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
287 int vsel;
289 vsel = regulator_map_voltage_linear_range(rdev, uV, uV);
290 if (vsel < 0)
291 return vsel;
293 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
296 static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
298 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
300 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
301 return dcdc->dvs_vsel;
302 else
303 return dcdc->on_vsel;
306 /* Current limit options */
307 static const unsigned int wm831x_dcdc_ilim[] = {
308 125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000
311 static const struct regulator_ops wm831x_buckv_ops = {
312 .set_voltage_sel = wm831x_buckv_set_voltage_sel,
313 .get_voltage_sel = wm831x_buckv_get_voltage_sel,
314 .list_voltage = regulator_list_voltage_linear_range,
315 .map_voltage = regulator_map_voltage_linear_range,
316 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
317 .set_current_limit = regulator_set_current_limit_regmap,
318 .get_current_limit = regulator_get_current_limit_regmap,
320 .is_enabled = regulator_is_enabled_regmap,
321 .enable = regulator_enable_regmap,
322 .disable = regulator_disable_regmap,
323 .get_status = wm831x_dcdc_get_status,
324 .get_mode = wm831x_dcdc_get_mode,
325 .set_mode = wm831x_dcdc_set_mode,
326 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
330 * Set up DVS control. We just log errors since we can still run
331 * (with reduced performance) if we fail.
333 static void wm831x_buckv_dvs_init(struct platform_device *pdev,
334 struct wm831x_dcdc *dcdc,
335 struct wm831x_buckv_pdata *pdata)
337 struct wm831x *wm831x = dcdc->wm831x;
338 int ret;
339 u16 ctrl;
341 if (!pdata || !pdata->dvs_gpio)
342 return;
344 /* gpiolib won't let us read the GPIO status so pick the higher
345 * of the two existing voltages so we take it as platform data.
347 dcdc->dvs_gpio_state = pdata->dvs_init_state;
349 ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio,
350 dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
351 "DCDC DVS");
352 if (ret < 0) {
353 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
354 dcdc->name, ret);
355 return;
358 dcdc->dvs_gpio = pdata->dvs_gpio;
360 switch (pdata->dvs_control_src) {
361 case 1:
362 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
363 break;
364 case 2:
365 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
366 break;
367 default:
368 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
369 pdata->dvs_control_src, dcdc->name);
370 return;
373 /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
374 * to make bootstrapping a bit smoother.
376 if (!dcdc->dvs_vsel) {
377 ret = wm831x_set_bits(wm831x,
378 dcdc->base + WM831X_DCDC_DVS_CONTROL,
379 WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
380 if (ret == 0)
381 dcdc->dvs_vsel = dcdc->on_vsel;
382 else
383 dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
384 ret);
387 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
388 WM831X_DC1_DVS_SRC_MASK, ctrl);
389 if (ret < 0) {
390 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
391 dcdc->name, ret);
395 static int wm831x_buckv_probe(struct platform_device *pdev)
397 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
398 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
399 struct regulator_config config = { };
400 int id;
401 struct wm831x_dcdc *dcdc;
402 struct resource *res;
403 int ret, irq;
405 if (pdata && pdata->wm831x_num)
406 id = (pdata->wm831x_num * 10) + 1;
407 else
408 id = 0;
409 id = pdev->id - id;
411 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
413 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
414 GFP_KERNEL);
415 if (!dcdc)
416 return -ENOMEM;
418 dcdc->wm831x = wm831x;
420 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
421 if (res == NULL) {
422 dev_err(&pdev->dev, "No REG resource\n");
423 ret = -EINVAL;
424 goto err;
426 dcdc->base = res->start;
428 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
429 dcdc->desc.name = dcdc->name;
431 snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
432 "DC%dVDD", id + 1);
433 dcdc->desc.supply_name = dcdc->supply_name;
435 dcdc->desc.id = id;
436 dcdc->desc.type = REGULATOR_VOLTAGE;
437 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
438 dcdc->desc.linear_ranges = wm831x_buckv_ranges;
439 dcdc->desc.n_linear_ranges = ARRAY_SIZE(wm831x_buckv_ranges);
440 dcdc->desc.ops = &wm831x_buckv_ops;
441 dcdc->desc.owner = THIS_MODULE;
442 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
443 dcdc->desc.enable_mask = 1 << id;
444 dcdc->desc.csel_reg = dcdc->base + WM831X_DCDC_CONTROL_2;
445 dcdc->desc.csel_mask = WM831X_DC1_HC_THR_MASK;
446 dcdc->desc.n_current_limits = ARRAY_SIZE(wm831x_dcdc_ilim);
447 dcdc->desc.curr_table = wm831x_dcdc_ilim;
449 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
450 if (ret < 0) {
451 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
452 goto err;
454 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
456 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
457 if (ret < 0) {
458 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
459 goto err;
461 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
463 if (pdata && pdata->dcdc[id])
464 wm831x_buckv_dvs_init(pdev, dcdc,
465 pdata->dcdc[id]->driver_data);
467 config.dev = pdev->dev.parent;
468 if (pdata)
469 config.init_data = pdata->dcdc[id];
470 config.driver_data = dcdc;
471 config.regmap = wm831x->regmap;
473 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
474 &config);
475 if (IS_ERR(dcdc->regulator)) {
476 ret = PTR_ERR(dcdc->regulator);
477 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
478 id + 1, ret);
479 goto err;
482 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
483 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
484 wm831x_dcdc_uv_irq,
485 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
486 dcdc->name, dcdc);
487 if (ret != 0) {
488 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
489 irq, ret);
490 goto err;
493 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
494 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
495 wm831x_dcdc_oc_irq,
496 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
497 dcdc->name, dcdc);
498 if (ret != 0) {
499 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
500 irq, ret);
501 goto err;
504 platform_set_drvdata(pdev, dcdc);
506 return 0;
508 err:
509 return ret;
512 static struct platform_driver wm831x_buckv_driver = {
513 .probe = wm831x_buckv_probe,
514 .driver = {
515 .name = "wm831x-buckv",
520 * BUCKP specifics
523 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
525 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
526 struct wm831x *wm831x = dcdc->wm831x;
527 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
528 int sel;
530 sel = regulator_map_voltage_linear(rdev, uV, uV);
531 if (sel < 0)
532 return sel;
534 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
537 static const struct regulator_ops wm831x_buckp_ops = {
538 .set_voltage_sel = regulator_set_voltage_sel_regmap,
539 .get_voltage_sel = regulator_get_voltage_sel_regmap,
540 .list_voltage = regulator_list_voltage_linear,
541 .map_voltage = regulator_map_voltage_linear,
542 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
544 .is_enabled = regulator_is_enabled_regmap,
545 .enable = regulator_enable_regmap,
546 .disable = regulator_disable_regmap,
547 .get_status = wm831x_dcdc_get_status,
548 .get_mode = wm831x_dcdc_get_mode,
549 .set_mode = wm831x_dcdc_set_mode,
550 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
553 static int wm831x_buckp_probe(struct platform_device *pdev)
555 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
556 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
557 struct regulator_config config = { };
558 int id;
559 struct wm831x_dcdc *dcdc;
560 struct resource *res;
561 int ret, irq;
563 if (pdata && pdata->wm831x_num)
564 id = (pdata->wm831x_num * 10) + 1;
565 else
566 id = 0;
567 id = pdev->id - id;
569 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
571 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
572 GFP_KERNEL);
573 if (!dcdc)
574 return -ENOMEM;
576 dcdc->wm831x = wm831x;
578 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
579 if (res == NULL) {
580 dev_err(&pdev->dev, "No REG resource\n");
581 ret = -EINVAL;
582 goto err;
584 dcdc->base = res->start;
586 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
587 dcdc->desc.name = dcdc->name;
589 snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
590 "DC%dVDD", id + 1);
591 dcdc->desc.supply_name = dcdc->supply_name;
593 dcdc->desc.id = id;
594 dcdc->desc.type = REGULATOR_VOLTAGE;
595 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
596 dcdc->desc.ops = &wm831x_buckp_ops;
597 dcdc->desc.owner = THIS_MODULE;
598 dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
599 dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
600 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
601 dcdc->desc.enable_mask = 1 << id;
602 dcdc->desc.min_uV = 850000;
603 dcdc->desc.uV_step = 25000;
605 config.dev = pdev->dev.parent;
606 if (pdata)
607 config.init_data = pdata->dcdc[id];
608 config.driver_data = dcdc;
609 config.regmap = wm831x->regmap;
611 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
612 &config);
613 if (IS_ERR(dcdc->regulator)) {
614 ret = PTR_ERR(dcdc->regulator);
615 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
616 id + 1, ret);
617 goto err;
620 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
621 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
622 wm831x_dcdc_uv_irq,
623 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
624 dcdc->name, dcdc);
625 if (ret != 0) {
626 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
627 irq, ret);
628 goto err;
631 platform_set_drvdata(pdev, dcdc);
633 return 0;
635 err:
636 return ret;
639 static struct platform_driver wm831x_buckp_driver = {
640 .probe = wm831x_buckp_probe,
641 .driver = {
642 .name = "wm831x-buckp",
647 * DCDC boost convertors
650 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
652 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
653 struct wm831x *wm831x = dcdc->wm831x;
654 int ret;
656 /* First, check for errors */
657 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
658 if (ret < 0)
659 return ret;
661 if (ret & (1 << rdev_get_id(rdev))) {
662 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
663 rdev_get_id(rdev) + 1);
664 return REGULATOR_STATUS_ERROR;
667 /* Is the regulator on? */
668 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
669 if (ret < 0)
670 return ret;
671 if (ret & (1 << rdev_get_id(rdev)))
672 return REGULATOR_STATUS_ON;
673 else
674 return REGULATOR_STATUS_OFF;
677 static const struct regulator_ops wm831x_boostp_ops = {
678 .get_status = wm831x_boostp_get_status,
680 .is_enabled = regulator_is_enabled_regmap,
681 .enable = regulator_enable_regmap,
682 .disable = regulator_disable_regmap,
685 static int wm831x_boostp_probe(struct platform_device *pdev)
687 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
688 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
689 struct regulator_config config = { };
690 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
691 struct wm831x_dcdc *dcdc;
692 struct resource *res;
693 int ret, irq;
695 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
697 if (pdata == NULL || pdata->dcdc[id] == NULL)
698 return -ENODEV;
700 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
701 if (!dcdc)
702 return -ENOMEM;
704 dcdc->wm831x = wm831x;
706 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
707 if (res == NULL) {
708 dev_err(&pdev->dev, "No REG resource\n");
709 return -EINVAL;
711 dcdc->base = res->start;
713 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
714 dcdc->desc.name = dcdc->name;
715 dcdc->desc.id = id;
716 dcdc->desc.type = REGULATOR_VOLTAGE;
717 dcdc->desc.ops = &wm831x_boostp_ops;
718 dcdc->desc.owner = THIS_MODULE;
719 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
720 dcdc->desc.enable_mask = 1 << id;
722 config.dev = pdev->dev.parent;
723 if (pdata)
724 config.init_data = pdata->dcdc[id];
725 config.driver_data = dcdc;
726 config.regmap = wm831x->regmap;
728 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
729 &config);
730 if (IS_ERR(dcdc->regulator)) {
731 ret = PTR_ERR(dcdc->regulator);
732 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
733 id + 1, ret);
734 return ret;
737 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
738 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
739 wm831x_dcdc_uv_irq,
740 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
741 dcdc->name,
742 dcdc);
743 if (ret != 0) {
744 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
745 irq, ret);
746 return ret;
749 platform_set_drvdata(pdev, dcdc);
751 return 0;
754 static struct platform_driver wm831x_boostp_driver = {
755 .probe = wm831x_boostp_probe,
756 .driver = {
757 .name = "wm831x-boostp",
762 * External Power Enable
764 * These aren't actually DCDCs but look like them in hardware so share
765 * code.
768 #define WM831X_EPE_BASE 6
770 static const struct regulator_ops wm831x_epe_ops = {
771 .is_enabled = regulator_is_enabled_regmap,
772 .enable = regulator_enable_regmap,
773 .disable = regulator_disable_regmap,
774 .get_status = wm831x_dcdc_get_status,
777 static int wm831x_epe_probe(struct platform_device *pdev)
779 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
780 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
781 struct regulator_config config = { };
782 int id = pdev->id % ARRAY_SIZE(pdata->epe);
783 struct wm831x_dcdc *dcdc;
784 int ret;
786 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
788 dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
789 if (!dcdc)
790 return -ENOMEM;
792 dcdc->wm831x = wm831x;
794 /* For current parts this is correct; probably need to revisit
795 * in future.
797 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
798 dcdc->desc.name = dcdc->name;
799 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
800 dcdc->desc.ops = &wm831x_epe_ops;
801 dcdc->desc.type = REGULATOR_VOLTAGE;
802 dcdc->desc.owner = THIS_MODULE;
803 dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
804 dcdc->desc.enable_mask = 1 << dcdc->desc.id;
806 config.dev = pdev->dev.parent;
807 if (pdata)
808 config.init_data = pdata->epe[id];
809 config.driver_data = dcdc;
810 config.regmap = wm831x->regmap;
812 dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
813 &config);
814 if (IS_ERR(dcdc->regulator)) {
815 ret = PTR_ERR(dcdc->regulator);
816 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
817 id + 1, ret);
818 goto err;
821 platform_set_drvdata(pdev, dcdc);
823 return 0;
825 err:
826 return ret;
829 static struct platform_driver wm831x_epe_driver = {
830 .probe = wm831x_epe_probe,
831 .driver = {
832 .name = "wm831x-epe",
836 static struct platform_driver * const drivers[] = {
837 &wm831x_buckv_driver,
838 &wm831x_buckp_driver,
839 &wm831x_boostp_driver,
840 &wm831x_epe_driver,
843 static int __init wm831x_dcdc_init(void)
845 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
847 subsys_initcall(wm831x_dcdc_init);
849 static void __exit wm831x_dcdc_exit(void)
851 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
853 module_exit(wm831x_dcdc_exit);
855 /* Module information */
856 MODULE_AUTHOR("Mark Brown");
857 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
858 MODULE_LICENSE("GPL");
859 MODULE_ALIAS("platform:wm831x-buckv");
860 MODULE_ALIAS("platform:wm831x-buckp");
861 MODULE_ALIAS("platform:wm831x-boostp");
862 MODULE_ALIAS("platform:wm831x-epe");