xref: /linux/drivers/regulator/wm8400-regulator.c (revision c54a155d4d20e35d41bb2922ed4a6c615d177c4f)
142fad570SMark Brown /*
242fad570SMark Brown  * Regulator support for WM8400
342fad570SMark Brown  *
442fad570SMark Brown  * Copyright 2008 Wolfson Microelectronics PLC.
542fad570SMark Brown  *
642fad570SMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
742fad570SMark Brown  *
842fad570SMark Brown  * This program is free software; you can redistribute it and/or
942fad570SMark Brown  * modify it under the terms of the GNU General Public License as
1042fad570SMark Brown  * published by the Free Software Foundation; either version 2 of the
1142fad570SMark Brown  * License, or (at your option) any later version.
1242fad570SMark Brown  *
1342fad570SMark Brown  */
1442fad570SMark Brown 
1542fad570SMark Brown #include <linux/bug.h>
1642fad570SMark Brown #include <linux/err.h>
1742fad570SMark Brown #include <linux/kernel.h>
1865602c32SPaul Gortmaker #include <linux/module.h>
1942fad570SMark Brown #include <linux/regulator/driver.h>
2042fad570SMark Brown #include <linux/mfd/wm8400-private.h>
2142fad570SMark Brown 
22216765d9SMark Brown static int wm8400_ldo_list_voltage(struct regulator_dev *dev,
23216765d9SMark Brown 				   unsigned selector)
24216765d9SMark Brown {
25216765d9SMark Brown 	if (selector > WM8400_LDO1_VSEL_MASK)
26216765d9SMark Brown 		return -EINVAL;
27216765d9SMark Brown 
28216765d9SMark Brown 	if (selector < 15)
29216765d9SMark Brown 		return 900000 + (selector * 50000);
30216765d9SMark Brown 	else
31216765d9SMark Brown 		return 1600000 + ((selector - 14) * 100000);
32216765d9SMark Brown }
33216765d9SMark Brown 
34*c54a155dSMark Brown static int wm8400_ldo_map_voltage(struct regulator_dev *dev,
35*c54a155dSMark Brown 				  int min_uV, int max_uV)
3642fad570SMark Brown {
3742fad570SMark Brown 	u16 val;
3842fad570SMark Brown 
3942fad570SMark Brown 	if (min_uV < 900000 || min_uV > 3300000)
4042fad570SMark Brown 		return -EINVAL;
4142fad570SMark Brown 
4242fad570SMark Brown 	if (min_uV < 1700000) {
4342fad570SMark Brown 		/* Steps of 50mV from 900mV;  */
44e9a15c8cSAxel Lin 		val = DIV_ROUND_UP(min_uV - 900000, 50000);
4542fad570SMark Brown 
4642fad570SMark Brown 		if ((val * 50000) + 900000 > max_uV)
4742fad570SMark Brown 			return -EINVAL;
4842fad570SMark Brown 		BUG_ON((val * 50000) + 900000 < min_uV);
4942fad570SMark Brown 	} else {
5042fad570SMark Brown 		/* Steps of 100mV from 1700mV */
51e9a15c8cSAxel Lin 		val = DIV_ROUND_UP(min_uV - 1700000, 100000);
5242fad570SMark Brown 
5342fad570SMark Brown 		if ((val * 100000) + 1700000 > max_uV)
5442fad570SMark Brown 			return -EINVAL;
5542fad570SMark Brown 		BUG_ON((val * 100000) + 1700000 < min_uV);
5642fad570SMark Brown 
5742fad570SMark Brown 		val += 0xf;
5842fad570SMark Brown 	}
5942fad570SMark Brown 
607ce8a2a0SMark Brown 	return val;
6142fad570SMark Brown }
6242fad570SMark Brown 
63*c54a155dSMark Brown static struct regulator_ops wm8400_ldo_ops = {
64*c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
65*c54a155dSMark Brown 	.enable = regulator_enable_regmap,
66*c54a155dSMark Brown 	.disable = regulator_disable_regmap,
67*c54a155dSMark Brown 	.list_voltage = wm8400_ldo_list_voltage,
68*c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
69*c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
70*c54a155dSMark Brown 	.map_voltage = wm8400_ldo_map_voltage,
71*c54a155dSMark Brown };
7242fad570SMark Brown 
7342fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
7442fad570SMark Brown {
7542fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
7642fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
7742fad570SMark Brown 	u16 data[2];
7842fad570SMark Brown 	int ret;
7942fad570SMark Brown 
8042fad570SMark Brown 	ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
8142fad570SMark Brown 				data);
8242fad570SMark Brown 	if (ret != 0)
8342fad570SMark Brown 		return 0;
8442fad570SMark Brown 
8542fad570SMark Brown 	/* Datasheet: hibernate */
8642fad570SMark Brown 	if (data[0] & WM8400_DC1_SLEEP)
8742fad570SMark Brown 		return REGULATOR_MODE_STANDBY;
8842fad570SMark Brown 
8942fad570SMark Brown 	/* Datasheet: standby */
9042fad570SMark Brown 	if (!(data[0] & WM8400_DC1_ACTIVE))
9142fad570SMark Brown 		return REGULATOR_MODE_IDLE;
9242fad570SMark Brown 
9342fad570SMark Brown 	/* Datasheet: active with or without force PWM */
9442fad570SMark Brown 	if (data[1] & WM8400_DC1_FRC_PWM)
9542fad570SMark Brown 		return REGULATOR_MODE_FAST;
9642fad570SMark Brown 	else
9742fad570SMark Brown 		return REGULATOR_MODE_NORMAL;
9842fad570SMark Brown }
9942fad570SMark Brown 
10042fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
10142fad570SMark Brown {
10242fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
10342fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
10442fad570SMark Brown 	int ret;
10542fad570SMark Brown 
10642fad570SMark Brown 	switch (mode) {
10742fad570SMark Brown 	case REGULATOR_MODE_FAST:
10842fad570SMark Brown 		/* Datasheet: active with force PWM */
10942fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
11042fad570SMark Brown 				      WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
11142fad570SMark Brown 		if (ret != 0)
11242fad570SMark Brown 			return ret;
11342fad570SMark Brown 
11442fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
11542fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
11642fad570SMark Brown 				       WM8400_DC1_ACTIVE);
11742fad570SMark Brown 
11842fad570SMark Brown 	case REGULATOR_MODE_NORMAL:
11942fad570SMark Brown 		/* Datasheet: active */
12042fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
12142fad570SMark Brown 				      WM8400_DC1_FRC_PWM, 0);
12242fad570SMark Brown 		if (ret != 0)
12342fad570SMark Brown 			return ret;
12442fad570SMark Brown 
12542fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
12642fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
12742fad570SMark Brown 				       WM8400_DC1_ACTIVE);
12842fad570SMark Brown 
12942fad570SMark Brown 	case REGULATOR_MODE_IDLE:
13042fad570SMark Brown 		/* Datasheet: standby */
13142fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
13242fad570SMark Brown 				      WM8400_DC1_ACTIVE, 0);
13342fad570SMark Brown 		if (ret != 0)
13442fad570SMark Brown 			return ret;
13542fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
13642fad570SMark Brown 				       WM8400_DC1_SLEEP, 0);
13742fad570SMark Brown 
13842fad570SMark Brown 	default:
13942fad570SMark Brown 		return -EINVAL;
14042fad570SMark Brown 	}
14142fad570SMark Brown }
14242fad570SMark Brown 
14342fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
14442fad570SMark Brown 						 int input_uV, int output_uV,
14542fad570SMark Brown 						 int load_uA)
14642fad570SMark Brown {
14742fad570SMark Brown 	return REGULATOR_MODE_NORMAL;
14842fad570SMark Brown }
14942fad570SMark Brown 
15042fad570SMark Brown static struct regulator_ops wm8400_dcdc_ops = {
151*c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
152*c54a155dSMark Brown 	.enable = regulator_enable_regmap,
153*c54a155dSMark Brown 	.disable = regulator_disable_regmap,
154*c54a155dSMark Brown 	.list_voltage = regulator_list_voltage_linear,
155*c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
156*c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
15742fad570SMark Brown 	.get_mode = wm8400_dcdc_get_mode,
15842fad570SMark Brown 	.set_mode = wm8400_dcdc_set_mode,
15942fad570SMark Brown 	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
16042fad570SMark Brown };
16142fad570SMark Brown 
16242fad570SMark Brown static struct regulator_desc regulators[] = {
16342fad570SMark Brown 	{
16442fad570SMark Brown 		.name = "LDO1",
16542fad570SMark Brown 		.id = WM8400_LDO1,
16642fad570SMark Brown 		.ops = &wm8400_ldo_ops,
167*c54a155dSMark Brown 		.enable_reg = WM8400_LDO1_CONTROL,
168*c54a155dSMark Brown 		.enable_mask = WM8400_LDO1_ENA,
169216765d9SMark Brown 		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
170*c54a155dSMark Brown 		.vsel_reg = WM8400_LDO1_CONTROL,
171*c54a155dSMark Brown 		.vsel_mask = WM8400_LDO1_VSEL_MASK,
17242fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
17342fad570SMark Brown 		.owner = THIS_MODULE,
17442fad570SMark Brown 	},
17542fad570SMark Brown 	{
17642fad570SMark Brown 		.name = "LDO2",
17742fad570SMark Brown 		.id = WM8400_LDO2,
17842fad570SMark Brown 		.ops = &wm8400_ldo_ops,
179*c54a155dSMark Brown 		.enable_reg = WM8400_LDO2_CONTROL,
180*c54a155dSMark Brown 		.enable_mask = WM8400_LDO2_ENA,
181216765d9SMark Brown 		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
18242fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
183*c54a155dSMark Brown 		.vsel_reg = WM8400_LDO2_CONTROL,
184*c54a155dSMark Brown 		.vsel_mask = WM8400_LDO2_VSEL_MASK,
18542fad570SMark Brown 		.owner = THIS_MODULE,
18642fad570SMark Brown 	},
18742fad570SMark Brown 	{
18842fad570SMark Brown 		.name = "LDO3",
18942fad570SMark Brown 		.id = WM8400_LDO3,
19042fad570SMark Brown 		.ops = &wm8400_ldo_ops,
191*c54a155dSMark Brown 		.enable_reg = WM8400_LDO3_CONTROL,
192*c54a155dSMark Brown 		.enable_mask = WM8400_LDO3_ENA,
193216765d9SMark Brown 		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
194*c54a155dSMark Brown 		.vsel_reg = WM8400_LDO3_CONTROL,
195*c54a155dSMark Brown 		.vsel_mask = WM8400_LDO3_VSEL_MASK,
19642fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
19742fad570SMark Brown 		.owner = THIS_MODULE,
19842fad570SMark Brown 	},
19942fad570SMark Brown 	{
20042fad570SMark Brown 		.name = "LDO4",
20142fad570SMark Brown 		.id = WM8400_LDO4,
20242fad570SMark Brown 		.ops = &wm8400_ldo_ops,
203*c54a155dSMark Brown 		.enable_reg = WM8400_LDO4_CONTROL,
204*c54a155dSMark Brown 		.enable_mask = WM8400_LDO4_ENA,
205216765d9SMark Brown 		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
206*c54a155dSMark Brown 		.vsel_reg = WM8400_LDO4_CONTROL,
207*c54a155dSMark Brown 		.vsel_mask = WM8400_LDO4_VSEL_MASK,
20842fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
20942fad570SMark Brown 		.owner = THIS_MODULE,
21042fad570SMark Brown 	},
21142fad570SMark Brown 	{
21242fad570SMark Brown 		.name = "DCDC1",
21342fad570SMark Brown 		.id = WM8400_DCDC1,
21442fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
215*c54a155dSMark Brown 		.enable_reg = WM8400_DCDC1_CONTROL_1,
216*c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
217216765d9SMark Brown 		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
218*c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC1_CONTROL_1,
219*c54a155dSMark Brown 		.vsel_mask = WM8400_DC1_VSEL_MASK,
220*c54a155dSMark Brown 		.min_uV = 850000,
221*c54a155dSMark Brown 		.uV_step = 25000,
22242fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
22342fad570SMark Brown 		.owner = THIS_MODULE,
22442fad570SMark Brown 	},
22542fad570SMark Brown 	{
22642fad570SMark Brown 		.name = "DCDC2",
22742fad570SMark Brown 		.id = WM8400_DCDC2,
22842fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
229*c54a155dSMark Brown 		.enable_reg = WM8400_DCDC2_CONTROL_1,
230*c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
231216765d9SMark Brown 		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
232*c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC2_CONTROL_1,
233*c54a155dSMark Brown 		.vsel_mask = WM8400_DC2_VSEL_MASK,
234*c54a155dSMark Brown 		.min_uV = 850000,
235*c54a155dSMark Brown 		.uV_step = 25000,
23642fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
23742fad570SMark Brown 		.owner = THIS_MODULE,
23842fad570SMark Brown 	},
23942fad570SMark Brown };
24042fad570SMark Brown 
2415dbdf735SUwe Kleine-König static int __devinit wm8400_regulator_probe(struct platform_device *pdev)
24242fad570SMark Brown {
2431ad02bbcSDmitry Torokhov 	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
244c172708dSMark Brown 	struct regulator_config config = { };
24542fad570SMark Brown 	struct regulator_dev *rdev;
24642fad570SMark Brown 
247c172708dSMark Brown 	config.dev = &pdev->dev;
248c172708dSMark Brown 	config.init_data = pdev->dev.platform_data;
249c172708dSMark Brown 	config.driver_data = wm8400;
250*c54a155dSMark Brown 	config.regmap = wm8400->regmap;
25142fad570SMark Brown 
252c172708dSMark Brown 	rdev = regulator_register(&regulators[pdev->id], &config);
25342fad570SMark Brown 	if (IS_ERR(rdev))
25442fad570SMark Brown 		return PTR_ERR(rdev);
25542fad570SMark Brown 
2561ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, rdev);
2571ad02bbcSDmitry Torokhov 
25842fad570SMark Brown 	return 0;
25942fad570SMark Brown }
26042fad570SMark Brown 
26142fad570SMark Brown static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
26242fad570SMark Brown {
26342fad570SMark Brown 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
26442fad570SMark Brown 
2651ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
26642fad570SMark Brown 	regulator_unregister(rdev);
26742fad570SMark Brown 
26842fad570SMark Brown 	return 0;
26942fad570SMark Brown }
27042fad570SMark Brown 
27142fad570SMark Brown static struct platform_driver wm8400_regulator_driver = {
27242fad570SMark Brown 	.driver = {
27342fad570SMark Brown 		.name = "wm8400-regulator",
27442fad570SMark Brown 	},
27542fad570SMark Brown 	.probe = wm8400_regulator_probe,
27642fad570SMark Brown 	.remove = __devexit_p(wm8400_regulator_remove),
27742fad570SMark Brown };
27842fad570SMark Brown 
27942fad570SMark Brown /**
28042fad570SMark Brown  * wm8400_register_regulator - enable software control of a WM8400 regulator
28142fad570SMark Brown  *
28242fad570SMark Brown  * This function enables software control of a WM8400 regulator via
28342fad570SMark Brown  * the regulator API.  It is intended to be called from the
28442fad570SMark Brown  * platform_init() callback of the WM8400 MFD driver.
28542fad570SMark Brown  *
28642fad570SMark Brown  * @param dev      The WM8400 device to operate on.
28742fad570SMark Brown  * @param reg      The regulator to control.
28842fad570SMark Brown  * @param initdata Regulator initdata for the regulator.
28942fad570SMark Brown  */
29042fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg,
29142fad570SMark Brown 			      struct regulator_init_data *initdata)
29242fad570SMark Brown {
2931909e2f6SGreg Kroah-Hartman 	struct wm8400 *wm8400 = dev_get_drvdata(dev);
29442fad570SMark Brown 
29542fad570SMark Brown 	if (wm8400->regulators[reg].name)
29642fad570SMark Brown 		return -EBUSY;
29742fad570SMark Brown 
29842fad570SMark Brown 	initdata->driver_data = wm8400;
29942fad570SMark Brown 
30042fad570SMark Brown 	wm8400->regulators[reg].name = "wm8400-regulator";
30142fad570SMark Brown 	wm8400->regulators[reg].id = reg;
30242fad570SMark Brown 	wm8400->regulators[reg].dev.parent = dev;
30342fad570SMark Brown 	wm8400->regulators[reg].dev.platform_data = initdata;
30442fad570SMark Brown 
30542fad570SMark Brown 	return platform_device_register(&wm8400->regulators[reg]);
30642fad570SMark Brown }
30742fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator);
30842fad570SMark Brown 
30942fad570SMark Brown static int __init wm8400_regulator_init(void)
31042fad570SMark Brown {
31142fad570SMark Brown 	return platform_driver_register(&wm8400_regulator_driver);
31242fad570SMark Brown }
3135a1b22beSMark Brown subsys_initcall(wm8400_regulator_init);
31442fad570SMark Brown 
31542fad570SMark Brown static void __exit wm8400_regulator_exit(void)
31642fad570SMark Brown {
31742fad570SMark Brown 	platform_driver_unregister(&wm8400_regulator_driver);
31842fad570SMark Brown }
31942fad570SMark Brown module_exit(wm8400_regulator_exit);
32042fad570SMark Brown 
32142fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
32242fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver");
32342fad570SMark Brown MODULE_LICENSE("GPL");
32442fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator");
325