xref: /linux/drivers/regulator/wm8400-regulator.c (revision 6692e432a9d59b55edbfdb4ed388e03c7f17e43c)
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 
22*6692e432SMark Brown static const struct regulator_linear_range wm8400_ldo_ranges[] = {
23*6692e432SMark Brown 	{ .min_uV =  900000, .max_uV = 1600000, .min_sel = 0, .max_sel = 14,
24*6692e432SMark Brown 	  .uV_step =  50000 },
25*6692e432SMark Brown 	{ .min_uV = 1700000, .max_uV = 3300000, .min_sel = 15, .max_sel = 31,
26*6692e432SMark Brown 	  .uV_step = 100000 },
27*6692e432SMark Brown };
2842fad570SMark Brown 
29c54a155dSMark Brown static struct regulator_ops wm8400_ldo_ops = {
30c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
31c54a155dSMark Brown 	.enable = regulator_enable_regmap,
32c54a155dSMark Brown 	.disable = regulator_disable_regmap,
33*6692e432SMark Brown 	.list_voltage = regulator_list_voltage_linear_range,
34c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
35c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
36*6692e432SMark Brown 	.map_voltage = regulator_map_voltage_linear_range,
37c54a155dSMark Brown };
3842fad570SMark Brown 
3942fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
4042fad570SMark Brown {
4142fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
4242fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
4342fad570SMark Brown 	u16 data[2];
4442fad570SMark Brown 	int ret;
4542fad570SMark Brown 
4642fad570SMark Brown 	ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
4742fad570SMark Brown 				data);
4842fad570SMark Brown 	if (ret != 0)
4942fad570SMark Brown 		return 0;
5042fad570SMark Brown 
5142fad570SMark Brown 	/* Datasheet: hibernate */
5242fad570SMark Brown 	if (data[0] & WM8400_DC1_SLEEP)
5342fad570SMark Brown 		return REGULATOR_MODE_STANDBY;
5442fad570SMark Brown 
5542fad570SMark Brown 	/* Datasheet: standby */
5642fad570SMark Brown 	if (!(data[0] & WM8400_DC1_ACTIVE))
5742fad570SMark Brown 		return REGULATOR_MODE_IDLE;
5842fad570SMark Brown 
5942fad570SMark Brown 	/* Datasheet: active with or without force PWM */
6042fad570SMark Brown 	if (data[1] & WM8400_DC1_FRC_PWM)
6142fad570SMark Brown 		return REGULATOR_MODE_FAST;
6242fad570SMark Brown 	else
6342fad570SMark Brown 		return REGULATOR_MODE_NORMAL;
6442fad570SMark Brown }
6542fad570SMark Brown 
6642fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
6742fad570SMark Brown {
6842fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
6942fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
7042fad570SMark Brown 	int ret;
7142fad570SMark Brown 
7242fad570SMark Brown 	switch (mode) {
7342fad570SMark Brown 	case REGULATOR_MODE_FAST:
7442fad570SMark Brown 		/* Datasheet: active with force PWM */
7542fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
7642fad570SMark Brown 				      WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
7742fad570SMark Brown 		if (ret != 0)
7842fad570SMark Brown 			return ret;
7942fad570SMark Brown 
8042fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
8142fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
8242fad570SMark Brown 				       WM8400_DC1_ACTIVE);
8342fad570SMark Brown 
8442fad570SMark Brown 	case REGULATOR_MODE_NORMAL:
8542fad570SMark Brown 		/* Datasheet: active */
8642fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
8742fad570SMark Brown 				      WM8400_DC1_FRC_PWM, 0);
8842fad570SMark Brown 		if (ret != 0)
8942fad570SMark Brown 			return ret;
9042fad570SMark Brown 
9142fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
9242fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
9342fad570SMark Brown 				       WM8400_DC1_ACTIVE);
9442fad570SMark Brown 
9542fad570SMark Brown 	case REGULATOR_MODE_IDLE:
9642fad570SMark Brown 		/* Datasheet: standby */
9742fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
984001376eSAxel Lin 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0);
9942fad570SMark Brown 	default:
10042fad570SMark Brown 		return -EINVAL;
10142fad570SMark Brown 	}
10242fad570SMark Brown }
10342fad570SMark Brown 
10442fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
10542fad570SMark Brown 						 int input_uV, int output_uV,
10642fad570SMark Brown 						 int load_uA)
10742fad570SMark Brown {
10842fad570SMark Brown 	return REGULATOR_MODE_NORMAL;
10942fad570SMark Brown }
11042fad570SMark Brown 
11142fad570SMark Brown static struct regulator_ops wm8400_dcdc_ops = {
112c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
113c54a155dSMark Brown 	.enable = regulator_enable_regmap,
114c54a155dSMark Brown 	.disable = regulator_disable_regmap,
115c54a155dSMark Brown 	.list_voltage = regulator_list_voltage_linear,
11627eeabb7SAxel Lin 	.map_voltage = regulator_map_voltage_linear,
117c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
118c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
11942fad570SMark Brown 	.get_mode = wm8400_dcdc_get_mode,
12042fad570SMark Brown 	.set_mode = wm8400_dcdc_set_mode,
12142fad570SMark Brown 	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
12242fad570SMark Brown };
12342fad570SMark Brown 
12442fad570SMark Brown static struct regulator_desc regulators[] = {
12542fad570SMark Brown 	{
12642fad570SMark Brown 		.name = "LDO1",
12742fad570SMark Brown 		.id = WM8400_LDO1,
12842fad570SMark Brown 		.ops = &wm8400_ldo_ops,
129c54a155dSMark Brown 		.enable_reg = WM8400_LDO1_CONTROL,
130c54a155dSMark Brown 		.enable_mask = WM8400_LDO1_ENA,
131216765d9SMark Brown 		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
132*6692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
133*6692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
134c54a155dSMark Brown 		.vsel_reg = WM8400_LDO1_CONTROL,
135c54a155dSMark Brown 		.vsel_mask = WM8400_LDO1_VSEL_MASK,
13642fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
13742fad570SMark Brown 		.owner = THIS_MODULE,
13842fad570SMark Brown 	},
13942fad570SMark Brown 	{
14042fad570SMark Brown 		.name = "LDO2",
14142fad570SMark Brown 		.id = WM8400_LDO2,
14242fad570SMark Brown 		.ops = &wm8400_ldo_ops,
143c54a155dSMark Brown 		.enable_reg = WM8400_LDO2_CONTROL,
144c54a155dSMark Brown 		.enable_mask = WM8400_LDO2_ENA,
145216765d9SMark Brown 		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
146*6692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
147*6692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
14842fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
149c54a155dSMark Brown 		.vsel_reg = WM8400_LDO2_CONTROL,
150c54a155dSMark Brown 		.vsel_mask = WM8400_LDO2_VSEL_MASK,
15142fad570SMark Brown 		.owner = THIS_MODULE,
15242fad570SMark Brown 	},
15342fad570SMark Brown 	{
15442fad570SMark Brown 		.name = "LDO3",
15542fad570SMark Brown 		.id = WM8400_LDO3,
15642fad570SMark Brown 		.ops = &wm8400_ldo_ops,
157c54a155dSMark Brown 		.enable_reg = WM8400_LDO3_CONTROL,
158c54a155dSMark Brown 		.enable_mask = WM8400_LDO3_ENA,
159216765d9SMark Brown 		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
160*6692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
161*6692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
162c54a155dSMark Brown 		.vsel_reg = WM8400_LDO3_CONTROL,
163c54a155dSMark Brown 		.vsel_mask = WM8400_LDO3_VSEL_MASK,
16442fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
16542fad570SMark Brown 		.owner = THIS_MODULE,
16642fad570SMark Brown 	},
16742fad570SMark Brown 	{
16842fad570SMark Brown 		.name = "LDO4",
16942fad570SMark Brown 		.id = WM8400_LDO4,
17042fad570SMark Brown 		.ops = &wm8400_ldo_ops,
171c54a155dSMark Brown 		.enable_reg = WM8400_LDO4_CONTROL,
172c54a155dSMark Brown 		.enable_mask = WM8400_LDO4_ENA,
173216765d9SMark Brown 		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
174*6692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
175*6692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
176c54a155dSMark Brown 		.vsel_reg = WM8400_LDO4_CONTROL,
177c54a155dSMark Brown 		.vsel_mask = WM8400_LDO4_VSEL_MASK,
17842fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
17942fad570SMark Brown 		.owner = THIS_MODULE,
18042fad570SMark Brown 	},
18142fad570SMark Brown 	{
18242fad570SMark Brown 		.name = "DCDC1",
18342fad570SMark Brown 		.id = WM8400_DCDC1,
18442fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
185c54a155dSMark Brown 		.enable_reg = WM8400_DCDC1_CONTROL_1,
186c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
187216765d9SMark Brown 		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
188c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC1_CONTROL_1,
189c54a155dSMark Brown 		.vsel_mask = WM8400_DC1_VSEL_MASK,
190c54a155dSMark Brown 		.min_uV = 850000,
191c54a155dSMark Brown 		.uV_step = 25000,
19242fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
19342fad570SMark Brown 		.owner = THIS_MODULE,
19442fad570SMark Brown 	},
19542fad570SMark Brown 	{
19642fad570SMark Brown 		.name = "DCDC2",
19742fad570SMark Brown 		.id = WM8400_DCDC2,
19842fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
199c54a155dSMark Brown 		.enable_reg = WM8400_DCDC2_CONTROL_1,
200c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
201216765d9SMark Brown 		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
202c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC2_CONTROL_1,
203c54a155dSMark Brown 		.vsel_mask = WM8400_DC2_VSEL_MASK,
204c54a155dSMark Brown 		.min_uV = 850000,
205c54a155dSMark Brown 		.uV_step = 25000,
20642fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
20742fad570SMark Brown 		.owner = THIS_MODULE,
20842fad570SMark Brown 	},
20942fad570SMark Brown };
21042fad570SMark Brown 
211a5023574SBill Pemberton static int wm8400_regulator_probe(struct platform_device *pdev)
21242fad570SMark Brown {
2131ad02bbcSDmitry Torokhov 	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
214c172708dSMark Brown 	struct regulator_config config = { };
21542fad570SMark Brown 	struct regulator_dev *rdev;
21642fad570SMark Brown 
217c172708dSMark Brown 	config.dev = &pdev->dev;
218c172708dSMark Brown 	config.init_data = pdev->dev.platform_data;
219c172708dSMark Brown 	config.driver_data = wm8400;
220c54a155dSMark Brown 	config.regmap = wm8400->regmap;
22142fad570SMark Brown 
222c172708dSMark Brown 	rdev = regulator_register(&regulators[pdev->id], &config);
22342fad570SMark Brown 	if (IS_ERR(rdev))
22442fad570SMark Brown 		return PTR_ERR(rdev);
22542fad570SMark Brown 
2261ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, rdev);
2271ad02bbcSDmitry Torokhov 
22842fad570SMark Brown 	return 0;
22942fad570SMark Brown }
23042fad570SMark Brown 
2318dc995f5SBill Pemberton static int wm8400_regulator_remove(struct platform_device *pdev)
23242fad570SMark Brown {
23342fad570SMark Brown 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
23442fad570SMark Brown 
23542fad570SMark Brown 	regulator_unregister(rdev);
23642fad570SMark Brown 
23742fad570SMark Brown 	return 0;
23842fad570SMark Brown }
23942fad570SMark Brown 
24042fad570SMark Brown static struct platform_driver wm8400_regulator_driver = {
24142fad570SMark Brown 	.driver = {
24242fad570SMark Brown 		.name = "wm8400-regulator",
24342fad570SMark Brown 	},
24442fad570SMark Brown 	.probe = wm8400_regulator_probe,
2455eb9f2b9SBill Pemberton 	.remove = wm8400_regulator_remove,
24642fad570SMark Brown };
24742fad570SMark Brown 
24842fad570SMark Brown /**
24942fad570SMark Brown  * wm8400_register_regulator - enable software control of a WM8400 regulator
25042fad570SMark Brown  *
25142fad570SMark Brown  * This function enables software control of a WM8400 regulator via
25242fad570SMark Brown  * the regulator API.  It is intended to be called from the
25342fad570SMark Brown  * platform_init() callback of the WM8400 MFD driver.
25442fad570SMark Brown  *
25542fad570SMark Brown  * @param dev      The WM8400 device to operate on.
25642fad570SMark Brown  * @param reg      The regulator to control.
25742fad570SMark Brown  * @param initdata Regulator initdata for the regulator.
25842fad570SMark Brown  */
25942fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg,
26042fad570SMark Brown 			      struct regulator_init_data *initdata)
26142fad570SMark Brown {
2621909e2f6SGreg Kroah-Hartman 	struct wm8400 *wm8400 = dev_get_drvdata(dev);
26342fad570SMark Brown 
26442fad570SMark Brown 	if (wm8400->regulators[reg].name)
26542fad570SMark Brown 		return -EBUSY;
26642fad570SMark Brown 
26742fad570SMark Brown 	initdata->driver_data = wm8400;
26842fad570SMark Brown 
26942fad570SMark Brown 	wm8400->regulators[reg].name = "wm8400-regulator";
27042fad570SMark Brown 	wm8400->regulators[reg].id = reg;
27142fad570SMark Brown 	wm8400->regulators[reg].dev.parent = dev;
27242fad570SMark Brown 	wm8400->regulators[reg].dev.platform_data = initdata;
27342fad570SMark Brown 
27442fad570SMark Brown 	return platform_device_register(&wm8400->regulators[reg]);
27542fad570SMark Brown }
27642fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator);
27742fad570SMark Brown 
27842fad570SMark Brown static int __init wm8400_regulator_init(void)
27942fad570SMark Brown {
28042fad570SMark Brown 	return platform_driver_register(&wm8400_regulator_driver);
28142fad570SMark Brown }
2825a1b22beSMark Brown subsys_initcall(wm8400_regulator_init);
28342fad570SMark Brown 
28442fad570SMark Brown static void __exit wm8400_regulator_exit(void)
28542fad570SMark Brown {
28642fad570SMark Brown 	platform_driver_unregister(&wm8400_regulator_driver);
28742fad570SMark Brown }
28842fad570SMark Brown module_exit(wm8400_regulator_exit);
28942fad570SMark Brown 
29042fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
29142fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver");
29242fad570SMark Brown MODULE_LICENSE("GPL");
29342fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator");
294