wm831x-dcdc.c 25.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
 *
 * Copyright 2009 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
22 23
#include <linux/regulator/machine.h>
#include <linux/gpio.h>
24
#include <linux/slab.h>
25 26 27 28 29 30 31 32 33 34 35 36 37

#include <linux/mfd/wm831x/core.h>
#include <linux/mfd/wm831x/regulator.h>
#include <linux/mfd/wm831x/pdata.h>

#define WM831X_BUCKV_MAX_SELECTOR 0x68
#define WM831X_BUCKP_MAX_SELECTOR 0x66

#define WM831X_DCDC_MODE_FAST    0
#define WM831X_DCDC_MODE_NORMAL  1
#define WM831X_DCDC_MODE_IDLE    2
#define WM831X_DCDC_MODE_STANDBY 3

38
#define WM831X_DCDC_MAX_NAME 9
39 40 41 42 43 44

/* Register offsets in control block */
#define WM831X_DCDC_CONTROL_1     0
#define WM831X_DCDC_CONTROL_2     1
#define WM831X_DCDC_ON_CONFIG     2
#define WM831X_DCDC_SLEEP_CONTROL 3
45
#define WM831X_DCDC_DVS_CONTROL   4
46 47 48 49 50 51 52

/*
 * Shared
 */

struct wm831x_dcdc {
	char name[WM831X_DCDC_MAX_NAME];
53
	char supply_name[WM831X_DCDC_MAX_NAME];
54 55 56 57
	struct regulator_desc desc;
	int base;
	struct wm831x *wm831x;
	struct regulator_dev *regulator;
58 59 60 61
	int dvs_gpio;
	int dvs_gpio_state;
	int on_vsel;
	int dvs_vsel;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
};

static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)

{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
	int val;

	val = wm831x_reg_read(wm831x, reg);
	if (val < 0)
		return val;

	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;

	switch (val) {
	case WM831X_DCDC_MODE_FAST:
		return REGULATOR_MODE_FAST;
	case WM831X_DCDC_MODE_NORMAL:
		return REGULATOR_MODE_NORMAL;
	case WM831X_DCDC_MODE_STANDBY:
		return REGULATOR_MODE_STANDBY;
	case WM831X_DCDC_MODE_IDLE:
		return REGULATOR_MODE_IDLE;
	default:
		BUG();
89
		return -EINVAL;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	}
}

static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
				    unsigned int mode)
{
	int val;

	switch (mode) {
	case REGULATOR_MODE_FAST:
		val = WM831X_DCDC_MODE_FAST;
		break;
	case REGULATOR_MODE_NORMAL:
		val = WM831X_DCDC_MODE_NORMAL;
		break;
	case REGULATOR_MODE_STANDBY:
		val = WM831X_DCDC_MODE_STANDBY;
		break;
	case REGULATOR_MODE_IDLE:
		val = WM831X_DCDC_MODE_IDLE;
		break;
	default:
		return -EINVAL;
	}

	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
			       val << WM831X_DC1_ON_MODE_SHIFT);
}

static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;

	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
}

static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
					unsigned int mode)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;

	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
}

static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	int ret;

	/* First, check for errors */
	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
	if (ret < 0)
		return ret;

	if (ret & (1 << rdev_get_id(rdev))) {
		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
			rdev_get_id(rdev) + 1);
		return REGULATOR_STATUS_ERROR;
	}

	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
	if (rdev_get_id(rdev) < 2) {
		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
				rdev_get_id(rdev) + 1);
			return REGULATOR_STATUS_ERROR;
		}

		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
			dev_dbg(wm831x->dev, "DCDC%d over current\n",
				rdev_get_id(rdev) + 1);
			return REGULATOR_STATUS_ERROR;
		}
	}

	/* Is the regulator on? */
	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
	if (ret < 0)
		return ret;
	if (!(ret & (1 << rdev_get_id(rdev))))
		return REGULATOR_STATUS_OFF;

	/* TODO: When we handle hardware control modes so we can report the
	 * current mode. */
	return REGULATOR_STATUS_ON;
}

static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
{
	struct wm831x_dcdc *dcdc = data;

	regulator_notifier_call_chain(dcdc->regulator,
				      REGULATOR_EVENT_UNDER_VOLTAGE,
				      NULL);

	return IRQ_HANDLED;
}

static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
{
	struct wm831x_dcdc *dcdc = data;

	regulator_notifier_call_chain(dcdc->regulator,
				      REGULATOR_EVENT_OVER_CURRENT,
				      NULL);

	return IRQ_HANDLED;
}

/*
 * BUCKV specifics
 */

static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
				      unsigned selector)
{
	if (selector <= 0x8)
		return 600000;
	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
		return 600000 + ((selector - 0x8) * 12500);
	return -EINVAL;
}

218 219
static int wm831x_buckv_map_voltage(struct regulator_dev *rdev,
				   int min_uV, int max_uV)
220 221 222 223 224 225 226 227 228 229 230 231 232
{
	u16 vsel;

	if (min_uV < 600000)
		vsel = 0;
	else if (min_uV <= 1800000)
		vsel = ((min_uV - 600000) / 12500) + 8;
	else
		return -EINVAL;

	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
		return -EINVAL;

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	return vsel;
}

static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);

	if (state == dcdc->dvs_gpio_state)
		return 0;

	dcdc->dvs_gpio_state = state;
	gpio_set_value(dcdc->dvs_gpio, state);

	/* Should wait for DVS state change to be asserted if we have
	 * a GPIO for it, for now assume the device is configured
	 * for the fastest possible transition.
	 */

	return 0;
252 253
}

254 255
static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
					unsigned vsel)
256 257
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
258 259 260
	struct wm831x *wm831x = dcdc->wm831x;
	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
261
	int ret;
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	/* If this value is already set then do a GPIO update if we can */
	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
		return wm831x_buckv_set_dvs(rdev, 0);

	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
		return wm831x_buckv_set_dvs(rdev, 1);

	/* Always set the ON status to the minimum voltage */
	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
	if (ret < 0)
		return ret;
	dcdc->on_vsel = vsel;

	if (!dcdc->dvs_gpio)
		return ret;

	/* Kick the voltage transition now */
	ret = wm831x_buckv_set_dvs(rdev, 0);
	if (ret < 0)
		return ret;

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	/*
	 * If this VSEL is higher than the last one we've seen then
	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
	 * usage where we want to get to the highest voltage very
	 * quickly.
	 */
	if (vsel > dcdc->dvs_vsel) {
		ret = wm831x_set_bits(wm831x, dvs_reg,
				      WM831X_DC1_DVS_VSEL_MASK,
				      dcdc->dvs_vsel);
		if (ret == 0)
			dcdc->dvs_vsel = vsel;
		else
			dev_warn(wm831x->dev,
				 "Failed to set DCDC DVS VSEL: %d\n", ret);
299 300 301
	}

	return 0;
302 303 304
}

static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
305
					    int uV)
306 307
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
308
	struct wm831x *wm831x = dcdc->wm831x;
309
	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
310 311
	int vsel;

312
	vsel = wm831x_buckv_map_voltage(rdev, uV, uV);
313 314
	if (vsel < 0)
		return vsel;
315

316
	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
317 318
}

319
static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
320 321 322
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);

323
	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
324
		return dcdc->dvs_vsel;
325
	else
326
		return dcdc->on_vsel;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
}

/* Current limit options */
static u16 wm831x_dcdc_ilim[] = {
	125, 250, 375, 500, 625, 750, 875, 1000
};

static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
					   int min_uA, int max_uA)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
	int i;

	for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
343 344
		if ((min_uA <= wm831x_dcdc_ilim[i]) &&
		    (wm831x_dcdc_ilim[i] <= max_uA))
345 346 347 348 349
			break;
	}
	if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
		return -EINVAL;

350 351
	return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK,
			       i << WM831X_DC1_HC_THR_SHIFT);
352 353 354 355 356 357 358 359 360 361 362 363 364
}

static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
	int val;

	val = wm831x_reg_read(wm831x, reg);
	if (val < 0)
		return val;

365 366
	val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
	return wm831x_dcdc_ilim[val];
367 368 369
}

static struct regulator_ops wm831x_buckv_ops = {
370
	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
371
	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
372
	.list_voltage = wm831x_buckv_list_voltage,
373
	.map_voltage = wm831x_buckv_map_voltage,
374 375 376 377
	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
	.set_current_limit = wm831x_buckv_set_current_limit,
	.get_current_limit = wm831x_buckv_get_current_limit,

378 379 380
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
381 382 383 384 385 386
	.get_status = wm831x_dcdc_get_status,
	.get_mode = wm831x_dcdc_get_mode,
	.set_mode = wm831x_dcdc_set_mode,
	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
};

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * Set up DVS control.  We just log errors since we can still run
 * (with reduced performance) if we fail.
 */
static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
					    struct wm831x_buckv_pdata *pdata)
{
	struct wm831x *wm831x = dcdc->wm831x;
	int ret;
	u16 ctrl;

	if (!pdata || !pdata->dvs_gpio)
		return;

	/* gpiolib won't let us read the GPIO status so pick the higher
	 * of the two existing voltages so we take it as platform data.
	 */
	dcdc->dvs_gpio_state = pdata->dvs_init_state;

406 407 408
	ret = gpio_request_one(pdata->dvs_gpio,
			       dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
			       "DCDC DVS");
409
	if (ret < 0) {
410
		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
411 412 413 414 415
			dcdc->name, ret);
		return;
	}

	dcdc->dvs_gpio = pdata->dvs_gpio;
416 417 418 419 420 421 422 423 424 425 426 427 428 429

	switch (pdata->dvs_control_src) {
	case 1:
		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
		break;
	case 2:
		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
		break;
	default:
		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
			pdata->dvs_control_src, dcdc->name);
		return;
	}

430 431 432 433 434 435 436 437 438 439 440 441 442 443
	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
	 * to make bootstrapping a bit smoother.
	 */
	if (!dcdc->dvs_vsel) {
		ret = wm831x_set_bits(wm831x,
				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
		if (ret == 0)
			dcdc->dvs_vsel = dcdc->on_vsel;
		else
			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
				 ret);
	}

444 445 446 447 448 449
	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
			      WM831X_DC1_DVS_SRC_MASK, ctrl);
	if (ret < 0) {
		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
			dcdc->name, ret);
	}
450 451
}

452 453 454 455
static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
{
	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
456
	struct regulator_config config = { };
457
	int id;
458 459 460 461
	struct wm831x_dcdc *dcdc;
	struct resource *res;
	int ret, irq;

462 463 464 465 466 467
	if (pdata && pdata->wm831x_num)
		id = (pdata->wm831x_num * 10) + 1;
	else
		id = 0;
	id = pdev->id - id;

468 469
	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);

470 471
	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
			    GFP_KERNEL);
472 473 474 475 476 477 478
	if (dcdc == NULL) {
		dev_err(&pdev->dev, "Unable to allocate private data\n");
		return -ENOMEM;
	}

	dcdc->wm831x = wm831x;

479
	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
480
	if (res == NULL) {
481
		dev_err(&pdev->dev, "No REG resource\n");
482 483 484 485 486 487 488
		ret = -EINVAL;
		goto err;
	}
	dcdc->base = res->start;

	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
	dcdc->desc.name = dcdc->name;
489 490 491 492 493

	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
		 "DC%dVDD", id + 1);
	dcdc->desc.supply_name = dcdc->supply_name;

494 495 496 497 498
	dcdc->desc.id = id;
	dcdc->desc.type = REGULATOR_VOLTAGE;
	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
	dcdc->desc.ops = &wm831x_buckv_ops;
	dcdc->desc.owner = THIS_MODULE;
499 500
	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
	dcdc->desc.enable_mask = 1 << id;
501

502 503 504 505 506 507 508
	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
	if (ret < 0) {
		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
		goto err;
	}
	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;

509
	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
510 511 512 513 514 515
	if (ret < 0) {
		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
		goto err;
	}
	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;

516
	if (pdata && pdata->dcdc[id])
517 518
		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);

519
	config.dev = pdev->dev.parent;
520 521
	if (pdata)
		config.init_data = pdata->dcdc[id];
522
	config.driver_data = dcdc;
523
	config.regmap = wm831x->regmap;
524 525

	dcdc->regulator = regulator_register(&dcdc->desc, &config);
526 527 528 529 530 531 532
	if (IS_ERR(dcdc->regulator)) {
		ret = PTR_ERR(dcdc->regulator);
		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
			id + 1, ret);
		goto err;
	}

M
Mark Brown 已提交
533
	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
534 535
	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
536 537 538 539 540 541
	if (ret != 0) {
		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
			irq, ret);
		goto err_regulator;
	}

M
Mark Brown 已提交
542
	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
543 544
	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
				   IRQF_TRIGGER_RISING, dcdc->name, dcdc);
545 546 547 548 549 550 551 552 553 554 555
	if (ret != 0) {
		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
			irq, ret);
		goto err_uv;
	}

	platform_set_drvdata(pdev, dcdc);

	return 0;

err_uv:
M
Mark Brown 已提交
556 557
	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")),
		 dcdc);
558 559 560
err_regulator:
	regulator_unregister(dcdc->regulator);
err:
561 562
	if (dcdc->dvs_gpio)
		gpio_free(dcdc->dvs_gpio);
563 564 565 566 567 568
	return ret;
}

static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
{
	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
M
Mark Brown 已提交
569
	struct wm831x *wm831x = dcdc->wm831x;
570

571 572
	platform_set_drvdata(pdev, NULL);

M
Mark Brown 已提交
573 574 575 576
	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")),
			    dcdc);
	free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")),
			    dcdc);
577
	regulator_unregister(dcdc->regulator);
578 579
	if (dcdc->dvs_gpio)
		gpio_free(dcdc->dvs_gpio);
580 581 582 583 584 585 586 587 588

	return 0;
}

static struct platform_driver wm831x_buckv_driver = {
	.probe = wm831x_buckv_probe,
	.remove = __devexit_p(wm831x_buckv_remove),
	.driver		= {
		.name	= "wm831x-buckv",
589
		.owner	= THIS_MODULE,
590 591 592 593 594 595 596
	},
};

/*
 * BUCKP specifics
 */

597
static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
598 599 600 601
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
602 603 604 605 606
	int sel;

	sel = regulator_map_voltage_linear(rdev, uV, uV);
	if (sel < 0)
		return sel;
607

608
	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
609 610 611
}

static struct regulator_ops wm831x_buckp_ops = {
612
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
613
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
614
	.list_voltage = regulator_list_voltage_linear,
615
	.map_voltage = regulator_map_voltage_linear,
616 617
	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,

618 619 620
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
621 622 623 624 625 626 627 628 629 630
	.get_status = wm831x_dcdc_get_status,
	.get_mode = wm831x_dcdc_get_mode,
	.set_mode = wm831x_dcdc_set_mode,
	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
};

static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
{
	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
631
	struct regulator_config config = { };
632
	int id;
633 634 635 636
	struct wm831x_dcdc *dcdc;
	struct resource *res;
	int ret, irq;

637 638 639 640 641 642
	if (pdata && pdata->wm831x_num)
		id = (pdata->wm831x_num * 10) + 1;
	else
		id = 0;
	id = pdev->id - id;

643 644
	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);

645 646
	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
			    GFP_KERNEL);
647 648 649 650 651 652 653
	if (dcdc == NULL) {
		dev_err(&pdev->dev, "Unable to allocate private data\n");
		return -ENOMEM;
	}

	dcdc->wm831x = wm831x;

654
	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
655
	if (res == NULL) {
656
		dev_err(&pdev->dev, "No REG resource\n");
657 658 659 660 661 662 663
		ret = -EINVAL;
		goto err;
	}
	dcdc->base = res->start;

	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
	dcdc->desc.name = dcdc->name;
664 665 666 667 668

	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
		 "DC%dVDD", id + 1);
	dcdc->desc.supply_name = dcdc->supply_name;

669 670 671 672 673
	dcdc->desc.id = id;
	dcdc->desc.type = REGULATOR_VOLTAGE;
	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
	dcdc->desc.ops = &wm831x_buckp_ops;
	dcdc->desc.owner = THIS_MODULE;
674 675
	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
676 677
	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
	dcdc->desc.enable_mask = 1 << id;
678 679
	dcdc->desc.min_uV = 850000;
	dcdc->desc.uV_step = 25000;
680

681
	config.dev = pdev->dev.parent;
682 683
	if (pdata)
		config.init_data = pdata->dcdc[id];
684
	config.driver_data = dcdc;
685
	config.regmap = wm831x->regmap;
686 687

	dcdc->regulator = regulator_register(&dcdc->desc, &config);
688 689 690 691 692 693 694
	if (IS_ERR(dcdc->regulator)) {
		ret = PTR_ERR(dcdc->regulator);
		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
			id + 1, ret);
		goto err;
	}

M
Mark Brown 已提交
695
	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
696 697
	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
				   IRQF_TRIGGER_RISING,	dcdc->name, dcdc);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	if (ret != 0) {
		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
			irq, ret);
		goto err_regulator;
	}

	platform_set_drvdata(pdev, dcdc);

	return 0;

err_regulator:
	regulator_unregister(dcdc->regulator);
err:
	return ret;
}

static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
{
	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);

718 719
	platform_set_drvdata(pdev, NULL);

M
Mark Brown 已提交
720 721
	free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")),
			    dcdc);
722 723 724 725 726 727 728 729 730 731
	regulator_unregister(dcdc->regulator);

	return 0;
}

static struct platform_driver wm831x_buckp_driver = {
	.probe = wm831x_buckp_probe,
	.remove = __devexit_p(wm831x_buckp_remove),
	.driver		= {
		.name	= "wm831x-buckp",
732
		.owner	= THIS_MODULE,
733 734 735
	},
};

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
/*
 * DCDC boost convertors
 */

static int wm831x_boostp_get_status(struct regulator_dev *rdev)
{
	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
	struct wm831x *wm831x = dcdc->wm831x;
	int ret;

	/* First, check for errors */
	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
	if (ret < 0)
		return ret;

	if (ret & (1 << rdev_get_id(rdev))) {
		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
			rdev_get_id(rdev) + 1);
		return REGULATOR_STATUS_ERROR;
	}

	/* Is the regulator on? */
	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
	if (ret < 0)
		return ret;
	if (ret & (1 << rdev_get_id(rdev)))
		return REGULATOR_STATUS_ON;
	else
		return REGULATOR_STATUS_OFF;
}

static struct regulator_ops wm831x_boostp_ops = {
	.get_status = wm831x_boostp_get_status,

770 771 772
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
773 774 775 776 777 778
};

static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
{
	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
779
	struct regulator_config config = { };
780 781 782 783 784 785 786 787 788 789
	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
	struct wm831x_dcdc *dcdc;
	struct resource *res;
	int ret, irq;

	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);

	if (pdata == NULL || pdata->dcdc[id] == NULL)
		return -ENODEV;

790
	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
791 792 793 794 795 796 797
	if (dcdc == NULL) {
		dev_err(&pdev->dev, "Unable to allocate private data\n");
		return -ENOMEM;
	}

	dcdc->wm831x = wm831x;

798
	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
799
	if (res == NULL) {
800
		dev_err(&pdev->dev, "No REG resource\n");
801 802 803 804 805 806 807 808 809 810 811
		ret = -EINVAL;
		goto err;
	}
	dcdc->base = res->start;

	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
	dcdc->desc.name = dcdc->name;
	dcdc->desc.id = id;
	dcdc->desc.type = REGULATOR_VOLTAGE;
	dcdc->desc.ops = &wm831x_boostp_ops;
	dcdc->desc.owner = THIS_MODULE;
812 813
	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
	dcdc->desc.enable_mask = 1 << id;
814

815
	config.dev = pdev->dev.parent;
816 817
	if (pdata)
		config.init_data = pdata->dcdc[id];
818
	config.driver_data = dcdc;
819
	config.regmap = wm831x->regmap;
820 821

	dcdc->regulator = regulator_register(&dcdc->desc, &config);
822 823 824 825 826 827 828
	if (IS_ERR(dcdc->regulator)) {
		ret = PTR_ERR(dcdc->regulator);
		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
			id + 1, ret);
		goto err;
	}

M
Mark Brown 已提交
829
	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
830 831 832
	ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
				   IRQF_TRIGGER_RISING, dcdc->name,
				   dcdc);
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	if (ret != 0) {
		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
			irq, ret);
		goto err_regulator;
	}

	platform_set_drvdata(pdev, dcdc);

	return 0;

err_regulator:
	regulator_unregister(dcdc->regulator);
err:
	return ret;
}

static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
{
	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);

853 854
	platform_set_drvdata(pdev, NULL);

M
Mark Brown 已提交
855 856
	free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")),
		 dcdc);
857 858 859 860 861 862 863 864 865 866
	regulator_unregister(dcdc->regulator);

	return 0;
}

static struct platform_driver wm831x_boostp_driver = {
	.probe = wm831x_boostp_probe,
	.remove = __devexit_p(wm831x_boostp_remove),
	.driver		= {
		.name	= "wm831x-boostp",
867
		.owner	= THIS_MODULE,
868 869 870
	},
};

M
Mark Brown 已提交
871 872 873 874 875 876 877 878 879 880
/*
 * External Power Enable
 *
 * These aren't actually DCDCs but look like them in hardware so share
 * code.
 */

#define WM831X_EPE_BASE 6

static struct regulator_ops wm831x_epe_ops = {
881 882 883
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
M
Mark Brown 已提交
884 885 886 887 888 889 890
	.get_status = wm831x_dcdc_get_status,
};

static __devinit int wm831x_epe_probe(struct platform_device *pdev)
{
	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
891
	struct regulator_config config = { };
M
Mark Brown 已提交
892 893 894 895 896 897
	int id = pdev->id % ARRAY_SIZE(pdata->epe);
	struct wm831x_dcdc *dcdc;
	int ret;

	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);

898
	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
M
Mark Brown 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	if (dcdc == NULL) {
		dev_err(&pdev->dev, "Unable to allocate private data\n");
		return -ENOMEM;
	}

	dcdc->wm831x = wm831x;

	/* For current parts this is correct; probably need to revisit
	 * in future.
	 */
	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
	dcdc->desc.name = dcdc->name;
	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
	dcdc->desc.ops = &wm831x_epe_ops;
	dcdc->desc.type = REGULATOR_VOLTAGE;
	dcdc->desc.owner = THIS_MODULE;
915 916
	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
M
Mark Brown 已提交
917

918
	config.dev = pdev->dev.parent;
919 920
	if (pdata)
		config.init_data = pdata->epe[id];
921
	config.driver_data = dcdc;
922
	config.regmap = wm831x->regmap;
923 924

	dcdc->regulator = regulator_register(&dcdc->desc, &config);
M
Mark Brown 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	if (IS_ERR(dcdc->regulator)) {
		ret = PTR_ERR(dcdc->regulator);
		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
			id + 1, ret);
		goto err;
	}

	platform_set_drvdata(pdev, dcdc);

	return 0;

err:
	return ret;
}

static __devexit int wm831x_epe_remove(struct platform_device *pdev)
{
	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);

944
	platform_set_drvdata(pdev, NULL);
M
Mark Brown 已提交
945 946 947 948 949 950 951 952 953 954
	regulator_unregister(dcdc->regulator);

	return 0;
}

static struct platform_driver wm831x_epe_driver = {
	.probe = wm831x_epe_probe,
	.remove = __devexit_p(wm831x_epe_remove),
	.driver		= {
		.name	= "wm831x-epe",
955
		.owner	= THIS_MODULE,
M
Mark Brown 已提交
956 957 958
	},
};

959 960 961 962 963 964 965 966 967 968 969
static int __init wm831x_dcdc_init(void)
{
	int ret;
	ret = platform_driver_register(&wm831x_buckv_driver);
	if (ret != 0)
		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);

	ret = platform_driver_register(&wm831x_buckp_driver);
	if (ret != 0)
		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);

970 971 972 973
	ret = platform_driver_register(&wm831x_boostp_driver);
	if (ret != 0)
		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);

M
Mark Brown 已提交
974 975 976 977
	ret = platform_driver_register(&wm831x_epe_driver);
	if (ret != 0)
		pr_err("Failed to register WM831x EPE driver: %d\n", ret);

978 979 980 981 982 983
	return 0;
}
subsys_initcall(wm831x_dcdc_init);

static void __exit wm831x_dcdc_exit(void)
{
M
Mark Brown 已提交
984
	platform_driver_unregister(&wm831x_epe_driver);
985
	platform_driver_unregister(&wm831x_boostp_driver);
986 987 988 989 990 991 992 993 994 995 996
	platform_driver_unregister(&wm831x_buckp_driver);
	platform_driver_unregister(&wm831x_buckv_driver);
}
module_exit(wm831x_dcdc_exit);

/* Module information */
MODULE_AUTHOR("Mark Brown");
MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:wm831x-buckv");
MODULE_ALIAS("platform:wm831x-buckp");
997
MODULE_ALIAS("platform:wm831x-epe");