blob: dbfaf5945e48a683c20cd87ff185dfc816b789e7 [file] [log] [blame]
Mark Browne4ee8312009-07-28 15:21:49 +01001/*
2 * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/bitops.h>
18#include <linux/err.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
Mark Browne24a04c2009-09-22 08:47:11 -070022#include <linux/regulator/machine.h>
23#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mark Browne4ee8312009-07-28 15:21:49 +010025
26#include <linux/mfd/wm831x/core.h>
27#include <linux/mfd/wm831x/regulator.h>
28#include <linux/mfd/wm831x/pdata.h>
29
30#define WM831X_BUCKV_MAX_SELECTOR 0x68
31#define WM831X_BUCKP_MAX_SELECTOR 0x66
32
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
37
38#define WM831X_DCDC_MAX_NAME 6
39
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
Mark Browne24a04c2009-09-22 08:47:11 -070045#define WM831X_DCDC_DVS_CONTROL 4
Mark Browne4ee8312009-07-28 15:21:49 +010046
47/*
48 * Shared
49 */
50
51struct wm831x_dcdc {
52 char name[WM831X_DCDC_MAX_NAME];
53 struct regulator_desc desc;
54 int base;
55 struct wm831x *wm831x;
56 struct regulator_dev *regulator;
Mark Browne24a04c2009-09-22 08:47:11 -070057 int dvs_gpio;
58 int dvs_gpio_state;
59 int on_vsel;
60 int dvs_vsel;
Mark Browne4ee8312009-07-28 15:21:49 +010061};
62
63static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
64{
65 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
66 struct wm831x *wm831x = dcdc->wm831x;
67 int mask = 1 << rdev_get_id(rdev);
68 int reg;
69
70 reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
71 if (reg < 0)
72 return reg;
73
74 if (reg & mask)
75 return 1;
76 else
77 return 0;
78}
79
80static int wm831x_dcdc_enable(struct regulator_dev *rdev)
81{
82 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
83 struct wm831x *wm831x = dcdc->wm831x;
84 int mask = 1 << rdev_get_id(rdev);
85
86 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
87}
88
89static int wm831x_dcdc_disable(struct regulator_dev *rdev)
90{
91 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
92 struct wm831x *wm831x = dcdc->wm831x;
93 int mask = 1 << rdev_get_id(rdev);
94
95 return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
96}
97
98static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
99
100{
101 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
102 struct wm831x *wm831x = dcdc->wm831x;
103 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
104 int val;
105
106 val = wm831x_reg_read(wm831x, reg);
107 if (val < 0)
108 return val;
109
110 val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
111
112 switch (val) {
113 case WM831X_DCDC_MODE_FAST:
114 return REGULATOR_MODE_FAST;
115 case WM831X_DCDC_MODE_NORMAL:
116 return REGULATOR_MODE_NORMAL;
117 case WM831X_DCDC_MODE_STANDBY:
118 return REGULATOR_MODE_STANDBY;
119 case WM831X_DCDC_MODE_IDLE:
120 return REGULATOR_MODE_IDLE;
121 default:
122 BUG();
123 }
124}
125
126static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
127 unsigned int mode)
128{
129 int val;
130
131 switch (mode) {
132 case REGULATOR_MODE_FAST:
133 val = WM831X_DCDC_MODE_FAST;
134 break;
135 case REGULATOR_MODE_NORMAL:
136 val = WM831X_DCDC_MODE_NORMAL;
137 break;
138 case REGULATOR_MODE_STANDBY:
139 val = WM831X_DCDC_MODE_STANDBY;
140 break;
141 case REGULATOR_MODE_IDLE:
142 val = WM831X_DCDC_MODE_IDLE;
143 break;
144 default:
145 return -EINVAL;
146 }
147
148 return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
149 val << WM831X_DC1_ON_MODE_SHIFT);
150}
151
152static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
153{
154 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
155 struct wm831x *wm831x = dcdc->wm831x;
156 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
157
158 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
159}
160
161static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
162 unsigned int mode)
163{
164 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
165 struct wm831x *wm831x = dcdc->wm831x;
166 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
167
168 return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
169}
170
171static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
172{
173 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
174 struct wm831x *wm831x = dcdc->wm831x;
175 int ret;
176
177 /* First, check for errors */
178 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
179 if (ret < 0)
180 return ret;
181
182 if (ret & (1 << rdev_get_id(rdev))) {
183 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
184 rdev_get_id(rdev) + 1);
185 return REGULATOR_STATUS_ERROR;
186 }
187
188 /* DCDC1 and DCDC2 can additionally detect high voltage/current */
189 if (rdev_get_id(rdev) < 2) {
190 if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
191 dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
192 rdev_get_id(rdev) + 1);
193 return REGULATOR_STATUS_ERROR;
194 }
195
196 if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
197 dev_dbg(wm831x->dev, "DCDC%d over current\n",
198 rdev_get_id(rdev) + 1);
199 return REGULATOR_STATUS_ERROR;
200 }
201 }
202
203 /* Is the regulator on? */
204 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
205 if (ret < 0)
206 return ret;
207 if (!(ret & (1 << rdev_get_id(rdev))))
208 return REGULATOR_STATUS_OFF;
209
210 /* TODO: When we handle hardware control modes so we can report the
211 * current mode. */
212 return REGULATOR_STATUS_ON;
213}
214
215static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
216{
217 struct wm831x_dcdc *dcdc = data;
218
219 regulator_notifier_call_chain(dcdc->regulator,
220 REGULATOR_EVENT_UNDER_VOLTAGE,
221 NULL);
222
223 return IRQ_HANDLED;
224}
225
226static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
227{
228 struct wm831x_dcdc *dcdc = data;
229
230 regulator_notifier_call_chain(dcdc->regulator,
231 REGULATOR_EVENT_OVER_CURRENT,
232 NULL);
233
234 return IRQ_HANDLED;
235}
236
237/*
238 * BUCKV specifics
239 */
240
241static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
242 unsigned selector)
243{
244 if (selector <= 0x8)
245 return 600000;
246 if (selector <= WM831X_BUCKV_MAX_SELECTOR)
247 return 600000 + ((selector - 0x8) * 12500);
248 return -EINVAL;
249}
250
Mark Browne24a04c2009-09-22 08:47:11 -0700251static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
252 int min_uV, int max_uV)
Mark Browne4ee8312009-07-28 15:21:49 +0100253{
Mark Browne4ee8312009-07-28 15:21:49 +0100254 u16 vsel;
255
256 if (min_uV < 600000)
257 vsel = 0;
258 else if (min_uV <= 1800000)
259 vsel = ((min_uV - 600000) / 12500) + 8;
260 else
261 return -EINVAL;
262
263 if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
264 return -EINVAL;
265
Mark Browne24a04c2009-09-22 08:47:11 -0700266 return vsel;
267}
268
269static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
270 int min_uV, int max_uV)
271{
272 u16 vsel;
273
274 if (max_uV < 600000 || max_uV > 1800000)
275 return -EINVAL;
276
277 vsel = ((max_uV - 600000) / 12500) + 8;
278
279 if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
280 wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
281 return -EINVAL;
282
283 return vsel;
284}
285
286static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
287{
288 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
289
290 if (state == dcdc->dvs_gpio_state)
291 return 0;
292
293 dcdc->dvs_gpio_state = state;
294 gpio_set_value(dcdc->dvs_gpio, state);
295
296 /* Should wait for DVS state change to be asserted if we have
297 * a GPIO for it, for now assume the device is configured
298 * for the fastest possible transition.
299 */
300
301 return 0;
Mark Browne4ee8312009-07-28 15:21:49 +0100302}
303
304static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
Mark Browne24a04c2009-09-22 08:47:11 -0700305 int min_uV, int max_uV)
Mark Browne4ee8312009-07-28 15:21:49 +0100306{
307 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne24a04c2009-09-22 08:47:11 -0700308 struct wm831x *wm831x = dcdc->wm831x;
309 int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
310 int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
311 int vsel, ret;
Mark Browne4ee8312009-07-28 15:21:49 +0100312
Mark Browne24a04c2009-09-22 08:47:11 -0700313 vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
314 if (vsel < 0)
315 return vsel;
316
317 /* If this value is already set then do a GPIO update if we can */
318 if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
319 return wm831x_buckv_set_dvs(rdev, 0);
320
321 if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
322 return wm831x_buckv_set_dvs(rdev, 1);
323
324 /* Always set the ON status to the minimum voltage */
325 ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
326 if (ret < 0)
327 return ret;
328 dcdc->on_vsel = vsel;
329
330 if (!dcdc->dvs_gpio)
331 return ret;
332
333 /* Kick the voltage transition now */
334 ret = wm831x_buckv_set_dvs(rdev, 0);
335 if (ret < 0)
336 return ret;
337
338 /* Set the high voltage as the DVS voltage. This is optimised
339 * for CPUfreq usage, most processors will keep the maximum
340 * voltage constant and lower the minimum with the frequency. */
341 vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
342 if (vsel < 0) {
343 /* This should never happen - at worst the same vsel
344 * should be chosen */
345 WARN_ON(vsel < 0);
346 return 0;
347 }
348
349 /* Don't bother if it's the same VSEL we're already using */
350 if (vsel == dcdc->on_vsel)
351 return 0;
352
353 ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
354 if (ret == 0)
355 dcdc->dvs_vsel = vsel;
356 else
357 dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
358 ret);
359
360 return 0;
Mark Browne4ee8312009-07-28 15:21:49 +0100361}
362
363static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
Mark Browne24a04c2009-09-22 08:47:11 -0700364 int uV)
Mark Browne4ee8312009-07-28 15:21:49 +0100365{
366 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne24a04c2009-09-22 08:47:11 -0700367 struct wm831x *wm831x = dcdc->wm831x;
Mark Browne4ee8312009-07-28 15:21:49 +0100368 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
Mark Browne24a04c2009-09-22 08:47:11 -0700369 int vsel;
Mark Browne4ee8312009-07-28 15:21:49 +0100370
Mark Browne24a04c2009-09-22 08:47:11 -0700371 vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
372 if (vsel < 0)
373 return vsel;
374
375 return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
Mark Browne4ee8312009-07-28 15:21:49 +0100376}
377
378static int wm831x_buckv_get_voltage(struct regulator_dev *rdev)
379{
380 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
Mark Browne4ee8312009-07-28 15:21:49 +0100381
Mark Browne24a04c2009-09-22 08:47:11 -0700382 if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
383 return wm831x_buckv_list_voltage(rdev, dcdc->dvs_vsel);
384 else
385 return wm831x_buckv_list_voltage(rdev, dcdc->on_vsel);
Mark Browne4ee8312009-07-28 15:21:49 +0100386}
387
388/* Current limit options */
389static u16 wm831x_dcdc_ilim[] = {
390 125, 250, 375, 500, 625, 750, 875, 1000
391};
392
393static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
394 int min_uA, int max_uA)
395{
396 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
397 struct wm831x *wm831x = dcdc->wm831x;
398 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
399 int i;
400
401 for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
402 if (max_uA <= wm831x_dcdc_ilim[i])
403 break;
404 }
405 if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
406 return -EINVAL;
407
408 return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
409}
410
411static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
412{
413 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
414 struct wm831x *wm831x = dcdc->wm831x;
415 u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
416 int val;
417
418 val = wm831x_reg_read(wm831x, reg);
419 if (val < 0)
420 return val;
421
422 return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
423}
424
425static struct regulator_ops wm831x_buckv_ops = {
426 .set_voltage = wm831x_buckv_set_voltage,
427 .get_voltage = wm831x_buckv_get_voltage,
428 .list_voltage = wm831x_buckv_list_voltage,
429 .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
430 .set_current_limit = wm831x_buckv_set_current_limit,
431 .get_current_limit = wm831x_buckv_get_current_limit,
432
433 .is_enabled = wm831x_dcdc_is_enabled,
434 .enable = wm831x_dcdc_enable,
435 .disable = wm831x_dcdc_disable,
436 .get_status = wm831x_dcdc_get_status,
437 .get_mode = wm831x_dcdc_get_mode,
438 .set_mode = wm831x_dcdc_set_mode,
439 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
440};
441
Mark Browne24a04c2009-09-22 08:47:11 -0700442/*
443 * Set up DVS control. We just log errors since we can still run
444 * (with reduced performance) if we fail.
445 */
446static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
447 struct wm831x_buckv_pdata *pdata)
448{
449 struct wm831x *wm831x = dcdc->wm831x;
450 int ret;
451 u16 ctrl;
452
453 if (!pdata || !pdata->dvs_gpio)
454 return;
455
456 switch (pdata->dvs_control_src) {
457 case 1:
458 ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
459 break;
460 case 2:
461 ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
462 break;
463 default:
464 dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
465 pdata->dvs_control_src, dcdc->name);
466 return;
467 }
468
469 ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
470 WM831X_DC1_DVS_SRC_MASK, ctrl);
471 if (ret < 0) {
472 dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
473 dcdc->name, ret);
474 return;
475 }
476
477 ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
478 if (ret < 0) {
479 dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
480 dcdc->name, ret);
481 return;
482 }
483
484 /* gpiolib won't let us read the GPIO status so pick the higher
485 * of the two existing voltages so we take it as platform data.
486 */
487 dcdc->dvs_gpio_state = pdata->dvs_init_state;
488
489 ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
490 if (ret < 0) {
491 dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
492 dcdc->name, ret);
493 gpio_free(pdata->dvs_gpio);
494 return;
495 }
496
497 dcdc->dvs_gpio = pdata->dvs_gpio;
498}
499
Mark Browne4ee8312009-07-28 15:21:49 +0100500static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
501{
502 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
503 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
504 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
505 struct wm831x_dcdc *dcdc;
506 struct resource *res;
507 int ret, irq;
508
509 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
510
511 if (pdata == NULL || pdata->dcdc[id] == NULL)
512 return -ENODEV;
513
514 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
515 if (dcdc == NULL) {
516 dev_err(&pdev->dev, "Unable to allocate private data\n");
517 return -ENOMEM;
518 }
519
520 dcdc->wm831x = wm831x;
521
522 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
523 if (res == NULL) {
524 dev_err(&pdev->dev, "No I/O resource\n");
525 ret = -EINVAL;
526 goto err;
527 }
528 dcdc->base = res->start;
529
530 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
531 dcdc->desc.name = dcdc->name;
532 dcdc->desc.id = id;
533 dcdc->desc.type = REGULATOR_VOLTAGE;
534 dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
535 dcdc->desc.ops = &wm831x_buckv_ops;
536 dcdc->desc.owner = THIS_MODULE;
537
Mark Browne24a04c2009-09-22 08:47:11 -0700538 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
539 if (ret < 0) {
540 dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
541 goto err;
542 }
543 dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
544
545 ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
546 if (ret < 0) {
547 dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
548 goto err;
549 }
550 dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
551
552 if (pdata->dcdc[id])
553 wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
554
Mark Browne4ee8312009-07-28 15:21:49 +0100555 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
556 pdata->dcdc[id], dcdc);
557 if (IS_ERR(dcdc->regulator)) {
558 ret = PTR_ERR(dcdc->regulator);
559 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
560 id + 1, ret);
561 goto err;
562 }
563
564 irq = platform_get_irq_byname(pdev, "UV");
565 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
566 IRQF_TRIGGER_RISING, dcdc->name,
567 dcdc);
568 if (ret != 0) {
569 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
570 irq, ret);
571 goto err_regulator;
572 }
573
574 irq = platform_get_irq_byname(pdev, "HC");
575 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq,
576 IRQF_TRIGGER_RISING, dcdc->name,
577 dcdc);
578 if (ret != 0) {
579 dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
580 irq, ret);
581 goto err_uv;
582 }
583
584 platform_set_drvdata(pdev, dcdc);
585
586 return 0;
587
588err_uv:
589 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
590err_regulator:
591 regulator_unregister(dcdc->regulator);
592err:
Mark Browne24a04c2009-09-22 08:47:11 -0700593 if (dcdc->dvs_gpio)
594 gpio_free(dcdc->dvs_gpio);
Mark Browne4ee8312009-07-28 15:21:49 +0100595 kfree(dcdc);
596 return ret;
597}
598
599static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
600{
601 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
602 struct wm831x *wm831x = dcdc->wm831x;
603
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800604 platform_set_drvdata(pdev, NULL);
605
Mark Browne4ee8312009-07-28 15:21:49 +0100606 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc);
607 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
608 regulator_unregister(dcdc->regulator);
Mark Browne24a04c2009-09-22 08:47:11 -0700609 if (dcdc->dvs_gpio)
610 gpio_free(dcdc->dvs_gpio);
Mark Browne4ee8312009-07-28 15:21:49 +0100611 kfree(dcdc);
612
613 return 0;
614}
615
616static struct platform_driver wm831x_buckv_driver = {
617 .probe = wm831x_buckv_probe,
618 .remove = __devexit_p(wm831x_buckv_remove),
619 .driver = {
620 .name = "wm831x-buckv",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800621 .owner = THIS_MODULE,
Mark Browne4ee8312009-07-28 15:21:49 +0100622 },
623};
624
625/*
626 * BUCKP specifics
627 */
628
629static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
630 unsigned selector)
631{
632 if (selector <= WM831X_BUCKP_MAX_SELECTOR)
633 return 850000 + (selector * 25000);
634 else
635 return -EINVAL;
636}
637
638static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
639 int min_uV, int max_uV)
640{
641 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
642 struct wm831x *wm831x = dcdc->wm831x;
643 u16 vsel;
644
645 if (min_uV <= 34000000)
646 vsel = (min_uV - 850000) / 25000;
647 else
648 return -EINVAL;
649
650 if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
651 return -EINVAL;
652
653 return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
654}
655
656static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
657 int min_uV, int max_uV)
658{
659 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
660 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
661
662 return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV);
663}
664
665static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
666 int uV)
667{
668 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
669 u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
670
671 return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV);
672}
673
674static int wm831x_buckp_get_voltage(struct regulator_dev *rdev)
675{
676 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
677 struct wm831x *wm831x = dcdc->wm831x;
678 u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
679 int val;
680
681 val = wm831x_reg_read(wm831x, reg);
682 if (val < 0)
683 return val;
684
685 return wm831x_buckp_list_voltage(rdev, val & WM831X_DC3_ON_VSEL_MASK);
686}
687
688static struct regulator_ops wm831x_buckp_ops = {
689 .set_voltage = wm831x_buckp_set_voltage,
690 .get_voltage = wm831x_buckp_get_voltage,
691 .list_voltage = wm831x_buckp_list_voltage,
692 .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
693
694 .is_enabled = wm831x_dcdc_is_enabled,
695 .enable = wm831x_dcdc_enable,
696 .disable = wm831x_dcdc_disable,
697 .get_status = wm831x_dcdc_get_status,
698 .get_mode = wm831x_dcdc_get_mode,
699 .set_mode = wm831x_dcdc_set_mode,
700 .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
701};
702
703static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
704{
705 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
706 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
707 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
708 struct wm831x_dcdc *dcdc;
709 struct resource *res;
710 int ret, irq;
711
712 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
713
714 if (pdata == NULL || pdata->dcdc[id] == NULL)
715 return -ENODEV;
716
717 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
718 if (dcdc == NULL) {
719 dev_err(&pdev->dev, "Unable to allocate private data\n");
720 return -ENOMEM;
721 }
722
723 dcdc->wm831x = wm831x;
724
725 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
726 if (res == NULL) {
727 dev_err(&pdev->dev, "No I/O resource\n");
728 ret = -EINVAL;
729 goto err;
730 }
731 dcdc->base = res->start;
732
733 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
734 dcdc->desc.name = dcdc->name;
735 dcdc->desc.id = id;
736 dcdc->desc.type = REGULATOR_VOLTAGE;
737 dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
738 dcdc->desc.ops = &wm831x_buckp_ops;
739 dcdc->desc.owner = THIS_MODULE;
740
741 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
742 pdata->dcdc[id], dcdc);
743 if (IS_ERR(dcdc->regulator)) {
744 ret = PTR_ERR(dcdc->regulator);
745 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
746 id + 1, ret);
747 goto err;
748 }
749
750 irq = platform_get_irq_byname(pdev, "UV");
751 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
752 IRQF_TRIGGER_RISING, dcdc->name,
753 dcdc);
754 if (ret != 0) {
755 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
756 irq, ret);
757 goto err_regulator;
758 }
759
760 platform_set_drvdata(pdev, dcdc);
761
762 return 0;
763
764err_regulator:
765 regulator_unregister(dcdc->regulator);
766err:
767 kfree(dcdc);
768 return ret;
769}
770
771static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
772{
773 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
774 struct wm831x *wm831x = dcdc->wm831x;
775
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800776 platform_set_drvdata(pdev, NULL);
777
Mark Browne4ee8312009-07-28 15:21:49 +0100778 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
779 regulator_unregister(dcdc->regulator);
780 kfree(dcdc);
781
782 return 0;
783}
784
785static struct platform_driver wm831x_buckp_driver = {
786 .probe = wm831x_buckp_probe,
787 .remove = __devexit_p(wm831x_buckp_remove),
788 .driver = {
789 .name = "wm831x-buckp",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800790 .owner = THIS_MODULE,
Mark Browne4ee8312009-07-28 15:21:49 +0100791 },
792};
793
Mark Brown8267a9b2009-07-28 15:22:23 +0100794/*
Mark Brown13048502009-07-28 15:23:16 +0100795 * DCDC boost convertors
796 */
797
798static int wm831x_boostp_get_status(struct regulator_dev *rdev)
799{
800 struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
801 struct wm831x *wm831x = dcdc->wm831x;
802 int ret;
803
804 /* First, check for errors */
805 ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
806 if (ret < 0)
807 return ret;
808
809 if (ret & (1 << rdev_get_id(rdev))) {
810 dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
811 rdev_get_id(rdev) + 1);
812 return REGULATOR_STATUS_ERROR;
813 }
814
815 /* Is the regulator on? */
816 ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
817 if (ret < 0)
818 return ret;
819 if (ret & (1 << rdev_get_id(rdev)))
820 return REGULATOR_STATUS_ON;
821 else
822 return REGULATOR_STATUS_OFF;
823}
824
825static struct regulator_ops wm831x_boostp_ops = {
826 .get_status = wm831x_boostp_get_status,
827
828 .is_enabled = wm831x_dcdc_is_enabled,
829 .enable = wm831x_dcdc_enable,
830 .disable = wm831x_dcdc_disable,
831};
832
833static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
834{
835 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
836 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
837 int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
838 struct wm831x_dcdc *dcdc;
839 struct resource *res;
840 int ret, irq;
841
842 dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
843
844 if (pdata == NULL || pdata->dcdc[id] == NULL)
845 return -ENODEV;
846
847 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
848 if (dcdc == NULL) {
849 dev_err(&pdev->dev, "Unable to allocate private data\n");
850 return -ENOMEM;
851 }
852
853 dcdc->wm831x = wm831x;
854
855 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
856 if (res == NULL) {
857 dev_err(&pdev->dev, "No I/O resource\n");
858 ret = -EINVAL;
859 goto err;
860 }
861 dcdc->base = res->start;
862
863 snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
864 dcdc->desc.name = dcdc->name;
865 dcdc->desc.id = id;
866 dcdc->desc.type = REGULATOR_VOLTAGE;
867 dcdc->desc.ops = &wm831x_boostp_ops;
868 dcdc->desc.owner = THIS_MODULE;
869
870 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
871 pdata->dcdc[id], dcdc);
872 if (IS_ERR(dcdc->regulator)) {
873 ret = PTR_ERR(dcdc->regulator);
874 dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
875 id + 1, ret);
876 goto err;
877 }
878
879 irq = platform_get_irq_byname(pdev, "UV");
880 ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
881 IRQF_TRIGGER_RISING, dcdc->name,
882 dcdc);
883 if (ret != 0) {
884 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
885 irq, ret);
886 goto err_regulator;
887 }
888
889 platform_set_drvdata(pdev, dcdc);
890
891 return 0;
892
893err_regulator:
894 regulator_unregister(dcdc->regulator);
895err:
896 kfree(dcdc);
897 return ret;
898}
899
900static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
901{
902 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
903 struct wm831x *wm831x = dcdc->wm831x;
904
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800905 platform_set_drvdata(pdev, NULL);
906
Mark Brown13048502009-07-28 15:23:16 +0100907 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
908 regulator_unregister(dcdc->regulator);
909 kfree(dcdc);
910
911 return 0;
912}
913
914static struct platform_driver wm831x_boostp_driver = {
915 .probe = wm831x_boostp_probe,
916 .remove = __devexit_p(wm831x_boostp_remove),
917 .driver = {
918 .name = "wm831x-boostp",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800919 .owner = THIS_MODULE,
Mark Brown13048502009-07-28 15:23:16 +0100920 },
921};
922
923/*
Mark Brown8267a9b2009-07-28 15:22:23 +0100924 * External Power Enable
925 *
926 * These aren't actually DCDCs but look like them in hardware so share
927 * code.
928 */
929
930#define WM831X_EPE_BASE 6
931
932static struct regulator_ops wm831x_epe_ops = {
933 .is_enabled = wm831x_dcdc_is_enabled,
934 .enable = wm831x_dcdc_enable,
935 .disable = wm831x_dcdc_disable,
936 .get_status = wm831x_dcdc_get_status,
937};
938
939static __devinit int wm831x_epe_probe(struct platform_device *pdev)
940{
941 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
942 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
943 int id = pdev->id % ARRAY_SIZE(pdata->epe);
944 struct wm831x_dcdc *dcdc;
945 int ret;
946
947 dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
948
949 if (pdata == NULL || pdata->epe[id] == NULL)
950 return -ENODEV;
951
952 dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
953 if (dcdc == NULL) {
954 dev_err(&pdev->dev, "Unable to allocate private data\n");
955 return -ENOMEM;
956 }
957
958 dcdc->wm831x = wm831x;
959
960 /* For current parts this is correct; probably need to revisit
961 * in future.
962 */
963 snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
964 dcdc->desc.name = dcdc->name;
965 dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
966 dcdc->desc.ops = &wm831x_epe_ops;
967 dcdc->desc.type = REGULATOR_VOLTAGE;
968 dcdc->desc.owner = THIS_MODULE;
969
970 dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
971 pdata->epe[id], dcdc);
972 if (IS_ERR(dcdc->regulator)) {
973 ret = PTR_ERR(dcdc->regulator);
974 dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
975 id + 1, ret);
976 goto err;
977 }
978
979 platform_set_drvdata(pdev, dcdc);
980
981 return 0;
982
983err:
984 kfree(dcdc);
985 return ret;
986}
987
988static __devexit int wm831x_epe_remove(struct platform_device *pdev)
989{
990 struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
991
Dmitry Torokhoveb66d562010-02-23 23:38:39 -0800992 platform_set_drvdata(pdev, NULL);
993
Mark Brown8267a9b2009-07-28 15:22:23 +0100994 regulator_unregister(dcdc->regulator);
995 kfree(dcdc);
996
997 return 0;
998}
999
1000static struct platform_driver wm831x_epe_driver = {
1001 .probe = wm831x_epe_probe,
1002 .remove = __devexit_p(wm831x_epe_remove),
1003 .driver = {
1004 .name = "wm831x-epe",
Dmitry Torokhoveb66d562010-02-23 23:38:39 -08001005 .owner = THIS_MODULE,
Mark Brown8267a9b2009-07-28 15:22:23 +01001006 },
1007};
1008
Mark Browne4ee8312009-07-28 15:21:49 +01001009static int __init wm831x_dcdc_init(void)
1010{
1011 int ret;
1012 ret = platform_driver_register(&wm831x_buckv_driver);
1013 if (ret != 0)
1014 pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1015
1016 ret = platform_driver_register(&wm831x_buckp_driver);
1017 if (ret != 0)
1018 pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1019
Mark Brown13048502009-07-28 15:23:16 +01001020 ret = platform_driver_register(&wm831x_boostp_driver);
1021 if (ret != 0)
1022 pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
1023
Mark Brown8267a9b2009-07-28 15:22:23 +01001024 ret = platform_driver_register(&wm831x_epe_driver);
1025 if (ret != 0)
1026 pr_err("Failed to register WM831x EPE driver: %d\n", ret);
1027
Mark Browne4ee8312009-07-28 15:21:49 +01001028 return 0;
1029}
1030subsys_initcall(wm831x_dcdc_init);
1031
1032static void __exit wm831x_dcdc_exit(void)
1033{
Mark Brown8267a9b2009-07-28 15:22:23 +01001034 platform_driver_unregister(&wm831x_epe_driver);
Mark Brown13048502009-07-28 15:23:16 +01001035 platform_driver_unregister(&wm831x_boostp_driver);
Mark Browne4ee8312009-07-28 15:21:49 +01001036 platform_driver_unregister(&wm831x_buckp_driver);
1037 platform_driver_unregister(&wm831x_buckv_driver);
1038}
1039module_exit(wm831x_dcdc_exit);
1040
1041/* Module information */
1042MODULE_AUTHOR("Mark Brown");
1043MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1044MODULE_LICENSE("GPL");
1045MODULE_ALIAS("platform:wm831x-buckv");
1046MODULE_ALIAS("platform:wm831x-buckp");