xref: /linux/drivers/regulator/wm8400-regulator.c (revision 27eeabb7a1000de974e45cd007b3f5324dcee490)
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 
34c54a155dSMark Brown static int wm8400_ldo_map_voltage(struct regulator_dev *dev,
35c54a155dSMark 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 
63c54a155dSMark Brown static struct regulator_ops wm8400_ldo_ops = {
64c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
65c54a155dSMark Brown 	.enable = regulator_enable_regmap,
66c54a155dSMark Brown 	.disable = regulator_disable_regmap,
67c54a155dSMark Brown 	.list_voltage = wm8400_ldo_list_voltage,
68c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
69c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
70c54a155dSMark Brown 	.map_voltage = wm8400_ldo_map_voltage,
71c54a155dSMark 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 = {
151c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
152c54a155dSMark Brown 	.enable = regulator_enable_regmap,
153c54a155dSMark Brown 	.disable = regulator_disable_regmap,
154c54a155dSMark Brown 	.list_voltage = regulator_list_voltage_linear,
155*27eeabb7SAxel Lin 	.map_voltage = regulator_map_voltage_linear,
156c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
157c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
15842fad570SMark Brown 	.get_mode = wm8400_dcdc_get_mode,
15942fad570SMark Brown 	.set_mode = wm8400_dcdc_set_mode,
16042fad570SMark Brown 	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
16142fad570SMark Brown };
16242fad570SMark Brown 
16342fad570SMark Brown static struct regulator_desc regulators[] = {
16442fad570SMark Brown 	{
16542fad570SMark Brown 		.name = "LDO1",
16642fad570SMark Brown 		.id = WM8400_LDO1,
16742fad570SMark Brown 		.ops = &wm8400_ldo_ops,
168c54a155dSMark Brown 		.enable_reg = WM8400_LDO1_CONTROL,
169c54a155dSMark Brown 		.enable_mask = WM8400_LDO1_ENA,
170216765d9SMark Brown 		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
171c54a155dSMark Brown 		.vsel_reg = WM8400_LDO1_CONTROL,
172c54a155dSMark Brown 		.vsel_mask = WM8400_LDO1_VSEL_MASK,
17342fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
17442fad570SMark Brown 		.owner = THIS_MODULE,
17542fad570SMark Brown 	},
17642fad570SMark Brown 	{
17742fad570SMark Brown 		.name = "LDO2",
17842fad570SMark Brown 		.id = WM8400_LDO2,
17942fad570SMark Brown 		.ops = &wm8400_ldo_ops,
180c54a155dSMark Brown 		.enable_reg = WM8400_LDO2_CONTROL,
181c54a155dSMark Brown 		.enable_mask = WM8400_LDO2_ENA,
182216765d9SMark Brown 		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
18342fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
184c54a155dSMark Brown 		.vsel_reg = WM8400_LDO2_CONTROL,
185c54a155dSMark Brown 		.vsel_mask = WM8400_LDO2_VSEL_MASK,
18642fad570SMark Brown 		.owner = THIS_MODULE,
18742fad570SMark Brown 	},
18842fad570SMark Brown 	{
18942fad570SMark Brown 		.name = "LDO3",
19042fad570SMark Brown 		.id = WM8400_LDO3,
19142fad570SMark Brown 		.ops = &wm8400_ldo_ops,
192c54a155dSMark Brown 		.enable_reg = WM8400_LDO3_CONTROL,
193c54a155dSMark Brown 		.enable_mask = WM8400_LDO3_ENA,
194216765d9SMark Brown 		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
195c54a155dSMark Brown 		.vsel_reg = WM8400_LDO3_CONTROL,
196c54a155dSMark Brown 		.vsel_mask = WM8400_LDO3_VSEL_MASK,
19742fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
19842fad570SMark Brown 		.owner = THIS_MODULE,
19942fad570SMark Brown 	},
20042fad570SMark Brown 	{
20142fad570SMark Brown 		.name = "LDO4",
20242fad570SMark Brown 		.id = WM8400_LDO4,
20342fad570SMark Brown 		.ops = &wm8400_ldo_ops,
204c54a155dSMark Brown 		.enable_reg = WM8400_LDO4_CONTROL,
205c54a155dSMark Brown 		.enable_mask = WM8400_LDO4_ENA,
206216765d9SMark Brown 		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
207c54a155dSMark Brown 		.vsel_reg = WM8400_LDO4_CONTROL,
208c54a155dSMark Brown 		.vsel_mask = WM8400_LDO4_VSEL_MASK,
20942fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
21042fad570SMark Brown 		.owner = THIS_MODULE,
21142fad570SMark Brown 	},
21242fad570SMark Brown 	{
21342fad570SMark Brown 		.name = "DCDC1",
21442fad570SMark Brown 		.id = WM8400_DCDC1,
21542fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
216c54a155dSMark Brown 		.enable_reg = WM8400_DCDC1_CONTROL_1,
217c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
218216765d9SMark Brown 		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
219c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC1_CONTROL_1,
220c54a155dSMark Brown 		.vsel_mask = WM8400_DC1_VSEL_MASK,
221c54a155dSMark Brown 		.min_uV = 850000,
222c54a155dSMark Brown 		.uV_step = 25000,
22342fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
22442fad570SMark Brown 		.owner = THIS_MODULE,
22542fad570SMark Brown 	},
22642fad570SMark Brown 	{
22742fad570SMark Brown 		.name = "DCDC2",
22842fad570SMark Brown 		.id = WM8400_DCDC2,
22942fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
230c54a155dSMark Brown 		.enable_reg = WM8400_DCDC2_CONTROL_1,
231c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
232216765d9SMark Brown 		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
233c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC2_CONTROL_1,
234c54a155dSMark Brown 		.vsel_mask = WM8400_DC2_VSEL_MASK,
235c54a155dSMark Brown 		.min_uV = 850000,
236c54a155dSMark Brown 		.uV_step = 25000,
23742fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
23842fad570SMark Brown 		.owner = THIS_MODULE,
23942fad570SMark Brown 	},
24042fad570SMark Brown };
24142fad570SMark Brown 
2425dbdf735SUwe Kleine-König static int __devinit wm8400_regulator_probe(struct platform_device *pdev)
24342fad570SMark Brown {
2441ad02bbcSDmitry Torokhov 	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
245c172708dSMark Brown 	struct regulator_config config = { };
24642fad570SMark Brown 	struct regulator_dev *rdev;
24742fad570SMark Brown 
248c172708dSMark Brown 	config.dev = &pdev->dev;
249c172708dSMark Brown 	config.init_data = pdev->dev.platform_data;
250c172708dSMark Brown 	config.driver_data = wm8400;
251c54a155dSMark Brown 	config.regmap = wm8400->regmap;
25242fad570SMark Brown 
253c172708dSMark Brown 	rdev = regulator_register(&regulators[pdev->id], &config);
25442fad570SMark Brown 	if (IS_ERR(rdev))
25542fad570SMark Brown 		return PTR_ERR(rdev);
25642fad570SMark Brown 
2571ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, rdev);
2581ad02bbcSDmitry Torokhov 
25942fad570SMark Brown 	return 0;
26042fad570SMark Brown }
26142fad570SMark Brown 
26242fad570SMark Brown static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
26342fad570SMark Brown {
26442fad570SMark Brown 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
26542fad570SMark Brown 
2661ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
26742fad570SMark Brown 	regulator_unregister(rdev);
26842fad570SMark Brown 
26942fad570SMark Brown 	return 0;
27042fad570SMark Brown }
27142fad570SMark Brown 
27242fad570SMark Brown static struct platform_driver wm8400_regulator_driver = {
27342fad570SMark Brown 	.driver = {
27442fad570SMark Brown 		.name = "wm8400-regulator",
27542fad570SMark Brown 	},
27642fad570SMark Brown 	.probe = wm8400_regulator_probe,
27742fad570SMark Brown 	.remove = __devexit_p(wm8400_regulator_remove),
27842fad570SMark Brown };
27942fad570SMark Brown 
28042fad570SMark Brown /**
28142fad570SMark Brown  * wm8400_register_regulator - enable software control of a WM8400 regulator
28242fad570SMark Brown  *
28342fad570SMark Brown  * This function enables software control of a WM8400 regulator via
28442fad570SMark Brown  * the regulator API.  It is intended to be called from the
28542fad570SMark Brown  * platform_init() callback of the WM8400 MFD driver.
28642fad570SMark Brown  *
28742fad570SMark Brown  * @param dev      The WM8400 device to operate on.
28842fad570SMark Brown  * @param reg      The regulator to control.
28942fad570SMark Brown  * @param initdata Regulator initdata for the regulator.
29042fad570SMark Brown  */
29142fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg,
29242fad570SMark Brown 			      struct regulator_init_data *initdata)
29342fad570SMark Brown {
2941909e2f6SGreg Kroah-Hartman 	struct wm8400 *wm8400 = dev_get_drvdata(dev);
29542fad570SMark Brown 
29642fad570SMark Brown 	if (wm8400->regulators[reg].name)
29742fad570SMark Brown 		return -EBUSY;
29842fad570SMark Brown 
29942fad570SMark Brown 	initdata->driver_data = wm8400;
30042fad570SMark Brown 
30142fad570SMark Brown 	wm8400->regulators[reg].name = "wm8400-regulator";
30242fad570SMark Brown 	wm8400->regulators[reg].id = reg;
30342fad570SMark Brown 	wm8400->regulators[reg].dev.parent = dev;
30442fad570SMark Brown 	wm8400->regulators[reg].dev.platform_data = initdata;
30542fad570SMark Brown 
30642fad570SMark Brown 	return platform_device_register(&wm8400->regulators[reg]);
30742fad570SMark Brown }
30842fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator);
30942fad570SMark Brown 
31042fad570SMark Brown static int __init wm8400_regulator_init(void)
31142fad570SMark Brown {
31242fad570SMark Brown 	return platform_driver_register(&wm8400_regulator_driver);
31342fad570SMark Brown }
3145a1b22beSMark Brown subsys_initcall(wm8400_regulator_init);
31542fad570SMark Brown 
31642fad570SMark Brown static void __exit wm8400_regulator_exit(void)
31742fad570SMark Brown {
31842fad570SMark Brown 	platform_driver_unregister(&wm8400_regulator_driver);
31942fad570SMark Brown }
32042fad570SMark Brown module_exit(wm8400_regulator_exit);
32142fad570SMark Brown 
32242fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
32342fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver");
32442fad570SMark Brown MODULE_LICENSE("GPL");
32542fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator");
326