xref: /linux/drivers/regulator/wm8400-regulator.c (revision e08abeca39673e1045ca1e5a90bd7fef69d0fe8f)
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 
226692e432SMark Brown static const struct regulator_linear_range wm8400_ldo_ranges[] = {
238828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
248828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
256692e432SMark Brown };
2642fad570SMark Brown 
27b0d6dd3bSJulia Lawall static const struct regulator_ops wm8400_ldo_ops = {
28c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
29c54a155dSMark Brown 	.enable = regulator_enable_regmap,
30c54a155dSMark Brown 	.disable = regulator_disable_regmap,
316692e432SMark Brown 	.list_voltage = regulator_list_voltage_linear_range,
32c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
33c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
346692e432SMark Brown 	.map_voltage = regulator_map_voltage_linear_range,
35c54a155dSMark Brown };
3642fad570SMark Brown 
3742fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
3842fad570SMark Brown {
39*e08abecaSAxel Lin 	struct regmap *rmap = rdev_get_regmap(dev);
4042fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
4142fad570SMark Brown 	u16 data[2];
4242fad570SMark Brown 	int ret;
4342fad570SMark Brown 
44*e08abecaSAxel Lin 	ret = regmap_bulk_read(rmap, WM8400_DCDC1_CONTROL_1 + offset, data, 2);
4542fad570SMark Brown 	if (ret != 0)
4642fad570SMark Brown 		return 0;
4742fad570SMark Brown 
4842fad570SMark Brown 	/* Datasheet: hibernate */
4942fad570SMark Brown 	if (data[0] & WM8400_DC1_SLEEP)
5042fad570SMark Brown 		return REGULATOR_MODE_STANDBY;
5142fad570SMark Brown 
5242fad570SMark Brown 	/* Datasheet: standby */
5342fad570SMark Brown 	if (!(data[0] & WM8400_DC1_ACTIVE))
5442fad570SMark Brown 		return REGULATOR_MODE_IDLE;
5542fad570SMark Brown 
5642fad570SMark Brown 	/* Datasheet: active with or without force PWM */
5742fad570SMark Brown 	if (data[1] & WM8400_DC1_FRC_PWM)
5842fad570SMark Brown 		return REGULATOR_MODE_FAST;
5942fad570SMark Brown 	else
6042fad570SMark Brown 		return REGULATOR_MODE_NORMAL;
6142fad570SMark Brown }
6242fad570SMark Brown 
6342fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
6442fad570SMark Brown {
65*e08abecaSAxel Lin 	struct regmap *rmap = rdev_get_regmap(dev);
6642fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
6742fad570SMark Brown 	int ret;
6842fad570SMark Brown 
6942fad570SMark Brown 	switch (mode) {
7042fad570SMark Brown 	case REGULATOR_MODE_FAST:
7142fad570SMark Brown 		/* Datasheet: active with force PWM */
72*e08abecaSAxel Lin 		ret = regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_2 + offset,
7342fad570SMark Brown 				      WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
7442fad570SMark Brown 		if (ret != 0)
7542fad570SMark Brown 			return ret;
7642fad570SMark Brown 
77*e08abecaSAxel Lin 		return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset,
7842fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
7942fad570SMark Brown 				       WM8400_DC1_ACTIVE);
8042fad570SMark Brown 
8142fad570SMark Brown 	case REGULATOR_MODE_NORMAL:
8242fad570SMark Brown 		/* Datasheet: active */
83*e08abecaSAxel Lin 		ret = regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_2 + offset,
8442fad570SMark Brown 				      WM8400_DC1_FRC_PWM, 0);
8542fad570SMark Brown 		if (ret != 0)
8642fad570SMark Brown 			return ret;
8742fad570SMark Brown 
88*e08abecaSAxel Lin 		return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset,
8942fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
9042fad570SMark Brown 				       WM8400_DC1_ACTIVE);
9142fad570SMark Brown 
9242fad570SMark Brown 	case REGULATOR_MODE_IDLE:
9342fad570SMark Brown 		/* Datasheet: standby */
94*e08abecaSAxel Lin 		return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset,
954001376eSAxel Lin 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0);
9642fad570SMark Brown 	default:
9742fad570SMark Brown 		return -EINVAL;
9842fad570SMark Brown 	}
9942fad570SMark Brown }
10042fad570SMark Brown 
10142fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
10242fad570SMark Brown 						 int input_uV, int output_uV,
10342fad570SMark Brown 						 int load_uA)
10442fad570SMark Brown {
10542fad570SMark Brown 	return REGULATOR_MODE_NORMAL;
10642fad570SMark Brown }
10742fad570SMark Brown 
108b0d6dd3bSJulia Lawall static const struct regulator_ops wm8400_dcdc_ops = {
109c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
110c54a155dSMark Brown 	.enable = regulator_enable_regmap,
111c54a155dSMark Brown 	.disable = regulator_disable_regmap,
112c54a155dSMark Brown 	.list_voltage = regulator_list_voltage_linear,
11327eeabb7SAxel Lin 	.map_voltage = regulator_map_voltage_linear,
114c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
115c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
11642fad570SMark Brown 	.get_mode = wm8400_dcdc_get_mode,
11742fad570SMark Brown 	.set_mode = wm8400_dcdc_set_mode,
11842fad570SMark Brown 	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
11942fad570SMark Brown };
12042fad570SMark Brown 
12142fad570SMark Brown static struct regulator_desc regulators[] = {
12242fad570SMark Brown 	{
12342fad570SMark Brown 		.name = "LDO1",
12442fad570SMark Brown 		.id = WM8400_LDO1,
12542fad570SMark Brown 		.ops = &wm8400_ldo_ops,
126c54a155dSMark Brown 		.enable_reg = WM8400_LDO1_CONTROL,
127c54a155dSMark Brown 		.enable_mask = WM8400_LDO1_ENA,
128216765d9SMark Brown 		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
1296692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
1306692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
131c54a155dSMark Brown 		.vsel_reg = WM8400_LDO1_CONTROL,
132c54a155dSMark Brown 		.vsel_mask = WM8400_LDO1_VSEL_MASK,
13342fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
13442fad570SMark Brown 		.owner = THIS_MODULE,
13542fad570SMark Brown 	},
13642fad570SMark Brown 	{
13742fad570SMark Brown 		.name = "LDO2",
13842fad570SMark Brown 		.id = WM8400_LDO2,
13942fad570SMark Brown 		.ops = &wm8400_ldo_ops,
140c54a155dSMark Brown 		.enable_reg = WM8400_LDO2_CONTROL,
141c54a155dSMark Brown 		.enable_mask = WM8400_LDO2_ENA,
142216765d9SMark Brown 		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
1436692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
1446692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
14542fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
146c54a155dSMark Brown 		.vsel_reg = WM8400_LDO2_CONTROL,
147c54a155dSMark Brown 		.vsel_mask = WM8400_LDO2_VSEL_MASK,
14842fad570SMark Brown 		.owner = THIS_MODULE,
14942fad570SMark Brown 	},
15042fad570SMark Brown 	{
15142fad570SMark Brown 		.name = "LDO3",
15242fad570SMark Brown 		.id = WM8400_LDO3,
15342fad570SMark Brown 		.ops = &wm8400_ldo_ops,
154c54a155dSMark Brown 		.enable_reg = WM8400_LDO3_CONTROL,
155c54a155dSMark Brown 		.enable_mask = WM8400_LDO3_ENA,
156216765d9SMark Brown 		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
1576692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
1586692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
159c54a155dSMark Brown 		.vsel_reg = WM8400_LDO3_CONTROL,
160c54a155dSMark Brown 		.vsel_mask = WM8400_LDO3_VSEL_MASK,
16142fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
16242fad570SMark Brown 		.owner = THIS_MODULE,
16342fad570SMark Brown 	},
16442fad570SMark Brown 	{
16542fad570SMark Brown 		.name = "LDO4",
16642fad570SMark Brown 		.id = WM8400_LDO4,
16742fad570SMark Brown 		.ops = &wm8400_ldo_ops,
168c54a155dSMark Brown 		.enable_reg = WM8400_LDO4_CONTROL,
169c54a155dSMark Brown 		.enable_mask = WM8400_LDO4_ENA,
170216765d9SMark Brown 		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
1716692e432SMark Brown 		.linear_ranges = wm8400_ldo_ranges,
1726692e432SMark Brown 		.n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
173c54a155dSMark Brown 		.vsel_reg = WM8400_LDO4_CONTROL,
174c54a155dSMark Brown 		.vsel_mask = WM8400_LDO4_VSEL_MASK,
17542fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
17642fad570SMark Brown 		.owner = THIS_MODULE,
17742fad570SMark Brown 	},
17842fad570SMark Brown 	{
17942fad570SMark Brown 		.name = "DCDC1",
18042fad570SMark Brown 		.id = WM8400_DCDC1,
18142fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
182c54a155dSMark Brown 		.enable_reg = WM8400_DCDC1_CONTROL_1,
183c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
184216765d9SMark Brown 		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
185c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC1_CONTROL_1,
186c54a155dSMark Brown 		.vsel_mask = WM8400_DC1_VSEL_MASK,
187c54a155dSMark Brown 		.min_uV = 850000,
188c54a155dSMark Brown 		.uV_step = 25000,
18942fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
19042fad570SMark Brown 		.owner = THIS_MODULE,
19142fad570SMark Brown 	},
19242fad570SMark Brown 	{
19342fad570SMark Brown 		.name = "DCDC2",
19442fad570SMark Brown 		.id = WM8400_DCDC2,
19542fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
196c54a155dSMark Brown 		.enable_reg = WM8400_DCDC2_CONTROL_1,
197c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
198216765d9SMark Brown 		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
199c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC2_CONTROL_1,
200c54a155dSMark Brown 		.vsel_mask = WM8400_DC2_VSEL_MASK,
201c54a155dSMark Brown 		.min_uV = 850000,
202c54a155dSMark Brown 		.uV_step = 25000,
20342fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
20442fad570SMark Brown 		.owner = THIS_MODULE,
20542fad570SMark Brown 	},
20642fad570SMark Brown };
20742fad570SMark Brown 
208a5023574SBill Pemberton static int wm8400_regulator_probe(struct platform_device *pdev)
20942fad570SMark Brown {
2101ad02bbcSDmitry Torokhov 	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
211c172708dSMark Brown 	struct regulator_config config = { };
21242fad570SMark Brown 	struct regulator_dev *rdev;
21342fad570SMark Brown 
214c172708dSMark Brown 	config.dev = &pdev->dev;
215dff91d0bSJingoo Han 	config.init_data = dev_get_platdata(&pdev->dev);
216c172708dSMark Brown 	config.driver_data = wm8400;
217c54a155dSMark Brown 	config.regmap = wm8400->regmap;
21842fad570SMark Brown 
219eb8b3c83SMark Brown 	rdev = devm_regulator_register(&pdev->dev, &regulators[pdev->id],
220eb8b3c83SMark Brown 				       &config);
22142fad570SMark Brown 	if (IS_ERR(rdev))
22242fad570SMark Brown 		return PTR_ERR(rdev);
22342fad570SMark Brown 
2241ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, rdev);
2251ad02bbcSDmitry Torokhov 
22642fad570SMark Brown 	return 0;
22742fad570SMark Brown }
22842fad570SMark Brown 
22942fad570SMark Brown static struct platform_driver wm8400_regulator_driver = {
23042fad570SMark Brown 	.driver = {
23142fad570SMark Brown 		.name = "wm8400-regulator",
23242fad570SMark Brown 	},
23342fad570SMark Brown 	.probe = wm8400_regulator_probe,
23442fad570SMark Brown };
23542fad570SMark Brown 
23642fad570SMark Brown /**
23742fad570SMark Brown  * wm8400_register_regulator - enable software control of a WM8400 regulator
23842fad570SMark Brown  *
23942fad570SMark Brown  * This function enables software control of a WM8400 regulator via
24042fad570SMark Brown  * the regulator API.  It is intended to be called from the
24142fad570SMark Brown  * platform_init() callback of the WM8400 MFD driver.
24242fad570SMark Brown  *
24342fad570SMark Brown  * @param dev      The WM8400 device to operate on.
24442fad570SMark Brown  * @param reg      The regulator to control.
24542fad570SMark Brown  * @param initdata Regulator initdata for the regulator.
24642fad570SMark Brown  */
24742fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg,
24842fad570SMark Brown 			      struct regulator_init_data *initdata)
24942fad570SMark Brown {
2501909e2f6SGreg Kroah-Hartman 	struct wm8400 *wm8400 = dev_get_drvdata(dev);
25142fad570SMark Brown 
25242fad570SMark Brown 	if (wm8400->regulators[reg].name)
25342fad570SMark Brown 		return -EBUSY;
25442fad570SMark Brown 
25542fad570SMark Brown 	initdata->driver_data = wm8400;
25642fad570SMark Brown 
25742fad570SMark Brown 	wm8400->regulators[reg].name = "wm8400-regulator";
25842fad570SMark Brown 	wm8400->regulators[reg].id = reg;
25942fad570SMark Brown 	wm8400->regulators[reg].dev.parent = dev;
26042fad570SMark Brown 	wm8400->regulators[reg].dev.platform_data = initdata;
26142fad570SMark Brown 
26242fad570SMark Brown 	return platform_device_register(&wm8400->regulators[reg]);
26342fad570SMark Brown }
26442fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator);
26542fad570SMark Brown 
26642fad570SMark Brown static int __init wm8400_regulator_init(void)
26742fad570SMark Brown {
26842fad570SMark Brown 	return platform_driver_register(&wm8400_regulator_driver);
26942fad570SMark Brown }
2705a1b22beSMark Brown subsys_initcall(wm8400_regulator_init);
27142fad570SMark Brown 
27242fad570SMark Brown static void __exit wm8400_regulator_exit(void)
27342fad570SMark Brown {
27442fad570SMark Brown 	platform_driver_unregister(&wm8400_regulator_driver);
27542fad570SMark Brown }
27642fad570SMark Brown module_exit(wm8400_regulator_exit);
27742fad570SMark Brown 
27842fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
27942fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver");
28042fad570SMark Brown MODULE_LICENSE("GPL");
28142fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator");
282