wm8400-regulator.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Regulator support for WM8400
 *
 * Copyright 2008 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/bug.h>
#include <linux/err.h>
#include <linux/kernel.h>
18
#include <linux/module.h>
19 20 21
#include <linux/regulator/driver.h>
#include <linux/mfd/wm8400-private.h>

22 23 24 25 26 27 28 29 30
static int wm8400_ldo_list_voltage(struct regulator_dev *dev,
				   unsigned selector)
{
	if (selector > WM8400_LDO1_VSEL_MASK)
		return -EINVAL;

	if (selector < 15)
		return 900000 + (selector * 50000);
	else
31
		return 1700000 + ((selector - 15) * 100000);
32 33
}

M
Mark Brown 已提交
34 35
static int wm8400_ldo_map_voltage(struct regulator_dev *dev,
				  int min_uV, int max_uV)
36 37
{
	u16 val;
38
	int volt;
39 40 41 42

	if (min_uV < 900000 || min_uV > 3300000)
		return -EINVAL;

43
	if (min_uV < 1700000) /* Steps of 50mV from 900mV;  */
44
		val = DIV_ROUND_UP(min_uV - 900000, 50000);
45 46
	else /* Steps of 100mV from 1700mV */
		val = DIV_ROUND_UP(min_uV - 1700000, 100000) + 15;
47

48 49 50
	volt = wm8400_ldo_list_voltage(dev, val);
	if (volt < min_uV || volt > max_uV)
		return -EINVAL;
51

M
Mark Brown 已提交
52
	return val;
53 54 55
}

static struct regulator_ops wm8400_ldo_ops = {
M
Mark Brown 已提交
56 57 58
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
59
	.list_voltage = wm8400_ldo_list_voltage,
M
Mark Brown 已提交
60 61 62
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
	.map_voltage = wm8400_ldo_map_voltage,
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 89 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
};

static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
{
	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
	u16 data[2];
	int ret;

	ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
				data);
	if (ret != 0)
		return 0;

	/* Datasheet: hibernate */
	if (data[0] & WM8400_DC1_SLEEP)
		return REGULATOR_MODE_STANDBY;

	/* Datasheet: standby */
	if (!(data[0] & WM8400_DC1_ACTIVE))
		return REGULATOR_MODE_IDLE;

	/* Datasheet: active with or without force PWM */
	if (data[1] & WM8400_DC1_FRC_PWM)
		return REGULATOR_MODE_FAST;
	else
		return REGULATOR_MODE_NORMAL;
}

static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
{
	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
	int ret;

	switch (mode) {
	case REGULATOR_MODE_FAST:
		/* Datasheet: active with force PWM */
		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
				      WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
		if (ret != 0)
			return ret;

		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
				       WM8400_DC1_ACTIVE);

	case REGULATOR_MODE_NORMAL:
		/* Datasheet: active */
		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
				      WM8400_DC1_FRC_PWM, 0);
		if (ret != 0)
			return ret;

		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
				       WM8400_DC1_ACTIVE);

	case REGULATOR_MODE_IDLE:
		/* Datasheet: standby */
		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
				      WM8400_DC1_ACTIVE, 0);
		if (ret != 0)
			return ret;
		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
				       WM8400_DC1_SLEEP, 0);

	default:
		return -EINVAL;
	}
}

static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
						 int input_uV, int output_uV,
						 int load_uA)
{
	return REGULATOR_MODE_NORMAL;
}

static struct regulator_ops wm8400_dcdc_ops = {
M
Mark Brown 已提交
143 144 145 146
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.list_voltage = regulator_list_voltage_linear,
147
	.map_voltage = regulator_map_voltage_linear,
M
Mark Brown 已提交
148 149
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
150 151 152 153 154 155 156 157 158 159
	.get_mode = wm8400_dcdc_get_mode,
	.set_mode = wm8400_dcdc_set_mode,
	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
};

static struct regulator_desc regulators[] = {
	{
		.name = "LDO1",
		.id = WM8400_LDO1,
		.ops = &wm8400_ldo_ops,
M
Mark Brown 已提交
160 161
		.enable_reg = WM8400_LDO1_CONTROL,
		.enable_mask = WM8400_LDO1_ENA,
162
		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
M
Mark Brown 已提交
163 164
		.vsel_reg = WM8400_LDO1_CONTROL,
		.vsel_mask = WM8400_LDO1_VSEL_MASK,
165 166 167 168 169 170 171
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
	},
	{
		.name = "LDO2",
		.id = WM8400_LDO2,
		.ops = &wm8400_ldo_ops,
M
Mark Brown 已提交
172 173
		.enable_reg = WM8400_LDO2_CONTROL,
		.enable_mask = WM8400_LDO2_ENA,
174
		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
175
		.type = REGULATOR_VOLTAGE,
M
Mark Brown 已提交
176 177
		.vsel_reg = WM8400_LDO2_CONTROL,
		.vsel_mask = WM8400_LDO2_VSEL_MASK,
178 179 180 181 182 183
		.owner = THIS_MODULE,
	},
	{
		.name = "LDO3",
		.id = WM8400_LDO3,
		.ops = &wm8400_ldo_ops,
M
Mark Brown 已提交
184 185
		.enable_reg = WM8400_LDO3_CONTROL,
		.enable_mask = WM8400_LDO3_ENA,
186
		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
M
Mark Brown 已提交
187 188
		.vsel_reg = WM8400_LDO3_CONTROL,
		.vsel_mask = WM8400_LDO3_VSEL_MASK,
189 190 191 192 193 194 195
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
	},
	{
		.name = "LDO4",
		.id = WM8400_LDO4,
		.ops = &wm8400_ldo_ops,
M
Mark Brown 已提交
196 197
		.enable_reg = WM8400_LDO4_CONTROL,
		.enable_mask = WM8400_LDO4_ENA,
198
		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
M
Mark Brown 已提交
199 200
		.vsel_reg = WM8400_LDO4_CONTROL,
		.vsel_mask = WM8400_LDO4_VSEL_MASK,
201 202 203 204 205 206 207
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
	},
	{
		.name = "DCDC1",
		.id = WM8400_DCDC1,
		.ops = &wm8400_dcdc_ops,
M
Mark Brown 已提交
208 209
		.enable_reg = WM8400_DCDC1_CONTROL_1,
		.enable_mask = WM8400_DC1_ENA_MASK,
210
		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
M
Mark Brown 已提交
211 212 213 214
		.vsel_reg = WM8400_DCDC1_CONTROL_1,
		.vsel_mask = WM8400_DC1_VSEL_MASK,
		.min_uV = 850000,
		.uV_step = 25000,
215 216 217 218 219 220 221
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
	},
	{
		.name = "DCDC2",
		.id = WM8400_DCDC2,
		.ops = &wm8400_dcdc_ops,
M
Mark Brown 已提交
222 223
		.enable_reg = WM8400_DCDC2_CONTROL_1,
		.enable_mask = WM8400_DC1_ENA_MASK,
224
		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
M
Mark Brown 已提交
225 226 227 228
		.vsel_reg = WM8400_DCDC2_CONTROL_1,
		.vsel_mask = WM8400_DC2_VSEL_MASK,
		.min_uV = 850000,
		.uV_step = 25000,
229 230 231 232 233
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
	},
};

234
static int __devinit wm8400_regulator_probe(struct platform_device *pdev)
235
{
236
	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
237
	struct regulator_config config = { };
238 239
	struct regulator_dev *rdev;

240 241 242
	config.dev = &pdev->dev;
	config.init_data = pdev->dev.platform_data;
	config.driver_data = wm8400;
M
Mark Brown 已提交
243
	config.regmap = wm8400->regmap;
244

245
	rdev = regulator_register(&regulators[pdev->id], &config);
246 247 248
	if (IS_ERR(rdev))
		return PTR_ERR(rdev);

249 250
	platform_set_drvdata(pdev, rdev);

251 252 253 254 255 256 257
	return 0;
}

static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
{
	struct regulator_dev *rdev = platform_get_drvdata(pdev);

258
	platform_set_drvdata(pdev, NULL);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	regulator_unregister(rdev);

	return 0;
}

static struct platform_driver wm8400_regulator_driver = {
	.driver = {
		.name = "wm8400-regulator",
	},
	.probe = wm8400_regulator_probe,
	.remove = __devexit_p(wm8400_regulator_remove),
};

/**
 * wm8400_register_regulator - enable software control of a WM8400 regulator
 *
 * This function enables software control of a WM8400 regulator via
 * the regulator API.  It is intended to be called from the
 * platform_init() callback of the WM8400 MFD driver.
 *
 * @param dev      The WM8400 device to operate on.
 * @param reg      The regulator to control.
 * @param initdata Regulator initdata for the regulator.
 */
int wm8400_register_regulator(struct device *dev, int reg,
			      struct regulator_init_data *initdata)
{
286
	struct wm8400 *wm8400 = dev_get_drvdata(dev);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	if (wm8400->regulators[reg].name)
		return -EBUSY;

	initdata->driver_data = wm8400;

	wm8400->regulators[reg].name = "wm8400-regulator";
	wm8400->regulators[reg].id = reg;
	wm8400->regulators[reg].dev.parent = dev;
	wm8400->regulators[reg].dev.platform_data = initdata;

	return platform_device_register(&wm8400->regulators[reg]);
}
EXPORT_SYMBOL_GPL(wm8400_register_regulator);

static int __init wm8400_regulator_init(void)
{
	return platform_driver_register(&wm8400_regulator_driver);
}
306
subsys_initcall(wm8400_regulator_init);
307 308 309 310 311 312 313 314 315 316 317

static void __exit wm8400_regulator_exit(void)
{
	platform_driver_unregister(&wm8400_regulator_driver);
}
module_exit(wm8400_regulator_exit);

MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_DESCRIPTION("WM8400 regulator driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:wm8400-regulator");