xref: /linux/drivers/regulator/wm8400-regulator.c (revision a5023574d120ca3b9337cedd4e27de90cae9aff7)
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
3174e20e56SAxel Lin 		return 1700000 + ((selector - 15) * 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;
3838c20eb2SAxel Lin 	int volt;
3942fad570SMark Brown 
4042fad570SMark Brown 	if (min_uV < 900000 || min_uV > 3300000)
4142fad570SMark Brown 		return -EINVAL;
4242fad570SMark Brown 
4338c20eb2SAxel Lin 	if (min_uV < 1700000) /* Steps of 50mV from 900mV;  */
44e9a15c8cSAxel Lin 		val = DIV_ROUND_UP(min_uV - 900000, 50000);
4538c20eb2SAxel Lin 	else /* Steps of 100mV from 1700mV */
4638c20eb2SAxel Lin 		val = DIV_ROUND_UP(min_uV - 1700000, 100000) + 15;
4742fad570SMark Brown 
4838c20eb2SAxel Lin 	volt = wm8400_ldo_list_voltage(dev, val);
4938c20eb2SAxel Lin 	if (volt < min_uV || volt > max_uV)
5042fad570SMark Brown 		return -EINVAL;
5142fad570SMark Brown 
527ce8a2a0SMark Brown 	return val;
5342fad570SMark Brown }
5442fad570SMark Brown 
55c54a155dSMark Brown static struct regulator_ops wm8400_ldo_ops = {
56c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
57c54a155dSMark Brown 	.enable = regulator_enable_regmap,
58c54a155dSMark Brown 	.disable = regulator_disable_regmap,
59c54a155dSMark Brown 	.list_voltage = wm8400_ldo_list_voltage,
60c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
61c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
62c54a155dSMark Brown 	.map_voltage = wm8400_ldo_map_voltage,
63c54a155dSMark Brown };
6442fad570SMark Brown 
6542fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
6642fad570SMark Brown {
6742fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
6842fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
6942fad570SMark Brown 	u16 data[2];
7042fad570SMark Brown 	int ret;
7142fad570SMark Brown 
7242fad570SMark Brown 	ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
7342fad570SMark Brown 				data);
7442fad570SMark Brown 	if (ret != 0)
7542fad570SMark Brown 		return 0;
7642fad570SMark Brown 
7742fad570SMark Brown 	/* Datasheet: hibernate */
7842fad570SMark Brown 	if (data[0] & WM8400_DC1_SLEEP)
7942fad570SMark Brown 		return REGULATOR_MODE_STANDBY;
8042fad570SMark Brown 
8142fad570SMark Brown 	/* Datasheet: standby */
8242fad570SMark Brown 	if (!(data[0] & WM8400_DC1_ACTIVE))
8342fad570SMark Brown 		return REGULATOR_MODE_IDLE;
8442fad570SMark Brown 
8542fad570SMark Brown 	/* Datasheet: active with or without force PWM */
8642fad570SMark Brown 	if (data[1] & WM8400_DC1_FRC_PWM)
8742fad570SMark Brown 		return REGULATOR_MODE_FAST;
8842fad570SMark Brown 	else
8942fad570SMark Brown 		return REGULATOR_MODE_NORMAL;
9042fad570SMark Brown }
9142fad570SMark Brown 
9242fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
9342fad570SMark Brown {
9442fad570SMark Brown 	struct wm8400 *wm8400 = rdev_get_drvdata(dev);
9542fad570SMark Brown 	int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
9642fad570SMark Brown 	int ret;
9742fad570SMark Brown 
9842fad570SMark Brown 	switch (mode) {
9942fad570SMark Brown 	case REGULATOR_MODE_FAST:
10042fad570SMark Brown 		/* Datasheet: active with force PWM */
10142fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
10242fad570SMark Brown 				      WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
10342fad570SMark Brown 		if (ret != 0)
10442fad570SMark Brown 			return ret;
10542fad570SMark Brown 
10642fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
10742fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
10842fad570SMark Brown 				       WM8400_DC1_ACTIVE);
10942fad570SMark Brown 
11042fad570SMark Brown 	case REGULATOR_MODE_NORMAL:
11142fad570SMark Brown 		/* Datasheet: active */
11242fad570SMark Brown 		ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
11342fad570SMark Brown 				      WM8400_DC1_FRC_PWM, 0);
11442fad570SMark Brown 		if (ret != 0)
11542fad570SMark Brown 			return ret;
11642fad570SMark Brown 
11742fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
11842fad570SMark Brown 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
11942fad570SMark Brown 				       WM8400_DC1_ACTIVE);
12042fad570SMark Brown 
12142fad570SMark Brown 	case REGULATOR_MODE_IDLE:
12242fad570SMark Brown 		/* Datasheet: standby */
12342fad570SMark Brown 		return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
1244001376eSAxel Lin 				       WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0);
12542fad570SMark Brown 	default:
12642fad570SMark Brown 		return -EINVAL;
12742fad570SMark Brown 	}
12842fad570SMark Brown }
12942fad570SMark Brown 
13042fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
13142fad570SMark Brown 						 int input_uV, int output_uV,
13242fad570SMark Brown 						 int load_uA)
13342fad570SMark Brown {
13442fad570SMark Brown 	return REGULATOR_MODE_NORMAL;
13542fad570SMark Brown }
13642fad570SMark Brown 
13742fad570SMark Brown static struct regulator_ops wm8400_dcdc_ops = {
138c54a155dSMark Brown 	.is_enabled = regulator_is_enabled_regmap,
139c54a155dSMark Brown 	.enable = regulator_enable_regmap,
140c54a155dSMark Brown 	.disable = regulator_disable_regmap,
141c54a155dSMark Brown 	.list_voltage = regulator_list_voltage_linear,
14227eeabb7SAxel Lin 	.map_voltage = regulator_map_voltage_linear,
143c54a155dSMark Brown 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
144c54a155dSMark Brown 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
14542fad570SMark Brown 	.get_mode = wm8400_dcdc_get_mode,
14642fad570SMark Brown 	.set_mode = wm8400_dcdc_set_mode,
14742fad570SMark Brown 	.get_optimum_mode = wm8400_dcdc_get_optimum_mode,
14842fad570SMark Brown };
14942fad570SMark Brown 
15042fad570SMark Brown static struct regulator_desc regulators[] = {
15142fad570SMark Brown 	{
15242fad570SMark Brown 		.name = "LDO1",
15342fad570SMark Brown 		.id = WM8400_LDO1,
15442fad570SMark Brown 		.ops = &wm8400_ldo_ops,
155c54a155dSMark Brown 		.enable_reg = WM8400_LDO1_CONTROL,
156c54a155dSMark Brown 		.enable_mask = WM8400_LDO1_ENA,
157216765d9SMark Brown 		.n_voltages = WM8400_LDO1_VSEL_MASK + 1,
158c54a155dSMark Brown 		.vsel_reg = WM8400_LDO1_CONTROL,
159c54a155dSMark Brown 		.vsel_mask = WM8400_LDO1_VSEL_MASK,
16042fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
16142fad570SMark Brown 		.owner = THIS_MODULE,
16242fad570SMark Brown 	},
16342fad570SMark Brown 	{
16442fad570SMark Brown 		.name = "LDO2",
16542fad570SMark Brown 		.id = WM8400_LDO2,
16642fad570SMark Brown 		.ops = &wm8400_ldo_ops,
167c54a155dSMark Brown 		.enable_reg = WM8400_LDO2_CONTROL,
168c54a155dSMark Brown 		.enable_mask = WM8400_LDO2_ENA,
169216765d9SMark Brown 		.n_voltages = WM8400_LDO2_VSEL_MASK + 1,
17042fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
171c54a155dSMark Brown 		.vsel_reg = WM8400_LDO2_CONTROL,
172c54a155dSMark Brown 		.vsel_mask = WM8400_LDO2_VSEL_MASK,
17342fad570SMark Brown 		.owner = THIS_MODULE,
17442fad570SMark Brown 	},
17542fad570SMark Brown 	{
17642fad570SMark Brown 		.name = "LDO3",
17742fad570SMark Brown 		.id = WM8400_LDO3,
17842fad570SMark Brown 		.ops = &wm8400_ldo_ops,
179c54a155dSMark Brown 		.enable_reg = WM8400_LDO3_CONTROL,
180c54a155dSMark Brown 		.enable_mask = WM8400_LDO3_ENA,
181216765d9SMark Brown 		.n_voltages = WM8400_LDO3_VSEL_MASK + 1,
182c54a155dSMark Brown 		.vsel_reg = WM8400_LDO3_CONTROL,
183c54a155dSMark Brown 		.vsel_mask = WM8400_LDO3_VSEL_MASK,
18442fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
18542fad570SMark Brown 		.owner = THIS_MODULE,
18642fad570SMark Brown 	},
18742fad570SMark Brown 	{
18842fad570SMark Brown 		.name = "LDO4",
18942fad570SMark Brown 		.id = WM8400_LDO4,
19042fad570SMark Brown 		.ops = &wm8400_ldo_ops,
191c54a155dSMark Brown 		.enable_reg = WM8400_LDO4_CONTROL,
192c54a155dSMark Brown 		.enable_mask = WM8400_LDO4_ENA,
193216765d9SMark Brown 		.n_voltages = WM8400_LDO4_VSEL_MASK + 1,
194c54a155dSMark Brown 		.vsel_reg = WM8400_LDO4_CONTROL,
195c54a155dSMark Brown 		.vsel_mask = WM8400_LDO4_VSEL_MASK,
19642fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
19742fad570SMark Brown 		.owner = THIS_MODULE,
19842fad570SMark Brown 	},
19942fad570SMark Brown 	{
20042fad570SMark Brown 		.name = "DCDC1",
20142fad570SMark Brown 		.id = WM8400_DCDC1,
20242fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
203c54a155dSMark Brown 		.enable_reg = WM8400_DCDC1_CONTROL_1,
204c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
205216765d9SMark Brown 		.n_voltages = WM8400_DC1_VSEL_MASK + 1,
206c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC1_CONTROL_1,
207c54a155dSMark Brown 		.vsel_mask = WM8400_DC1_VSEL_MASK,
208c54a155dSMark Brown 		.min_uV = 850000,
209c54a155dSMark Brown 		.uV_step = 25000,
21042fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
21142fad570SMark Brown 		.owner = THIS_MODULE,
21242fad570SMark Brown 	},
21342fad570SMark Brown 	{
21442fad570SMark Brown 		.name = "DCDC2",
21542fad570SMark Brown 		.id = WM8400_DCDC2,
21642fad570SMark Brown 		.ops = &wm8400_dcdc_ops,
217c54a155dSMark Brown 		.enable_reg = WM8400_DCDC2_CONTROL_1,
218c54a155dSMark Brown 		.enable_mask = WM8400_DC1_ENA_MASK,
219216765d9SMark Brown 		.n_voltages = WM8400_DC2_VSEL_MASK + 1,
220c54a155dSMark Brown 		.vsel_reg = WM8400_DCDC2_CONTROL_1,
221c54a155dSMark Brown 		.vsel_mask = WM8400_DC2_VSEL_MASK,
222c54a155dSMark Brown 		.min_uV = 850000,
223c54a155dSMark Brown 		.uV_step = 25000,
22442fad570SMark Brown 		.type = REGULATOR_VOLTAGE,
22542fad570SMark Brown 		.owner = THIS_MODULE,
22642fad570SMark Brown 	},
22742fad570SMark Brown };
22842fad570SMark Brown 
229*a5023574SBill Pemberton static int wm8400_regulator_probe(struct platform_device *pdev)
23042fad570SMark Brown {
2311ad02bbcSDmitry Torokhov 	struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
232c172708dSMark Brown 	struct regulator_config config = { };
23342fad570SMark Brown 	struct regulator_dev *rdev;
23442fad570SMark Brown 
235c172708dSMark Brown 	config.dev = &pdev->dev;
236c172708dSMark Brown 	config.init_data = pdev->dev.platform_data;
237c172708dSMark Brown 	config.driver_data = wm8400;
238c54a155dSMark Brown 	config.regmap = wm8400->regmap;
23942fad570SMark Brown 
240c172708dSMark Brown 	rdev = regulator_register(&regulators[pdev->id], &config);
24142fad570SMark Brown 	if (IS_ERR(rdev))
24242fad570SMark Brown 		return PTR_ERR(rdev);
24342fad570SMark Brown 
2441ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, rdev);
2451ad02bbcSDmitry Torokhov 
24642fad570SMark Brown 	return 0;
24742fad570SMark Brown }
24842fad570SMark Brown 
24942fad570SMark Brown static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
25042fad570SMark Brown {
25142fad570SMark Brown 	struct regulator_dev *rdev = platform_get_drvdata(pdev);
25242fad570SMark Brown 
2531ad02bbcSDmitry Torokhov 	platform_set_drvdata(pdev, NULL);
25442fad570SMark Brown 	regulator_unregister(rdev);
25542fad570SMark Brown 
25642fad570SMark Brown 	return 0;
25742fad570SMark Brown }
25842fad570SMark Brown 
25942fad570SMark Brown static struct platform_driver wm8400_regulator_driver = {
26042fad570SMark Brown 	.driver = {
26142fad570SMark Brown 		.name = "wm8400-regulator",
26242fad570SMark Brown 	},
26342fad570SMark Brown 	.probe = wm8400_regulator_probe,
2645eb9f2b9SBill Pemberton 	.remove = wm8400_regulator_remove,
26542fad570SMark Brown };
26642fad570SMark Brown 
26742fad570SMark Brown /**
26842fad570SMark Brown  * wm8400_register_regulator - enable software control of a WM8400 regulator
26942fad570SMark Brown  *
27042fad570SMark Brown  * This function enables software control of a WM8400 regulator via
27142fad570SMark Brown  * the regulator API.  It is intended to be called from the
27242fad570SMark Brown  * platform_init() callback of the WM8400 MFD driver.
27342fad570SMark Brown  *
27442fad570SMark Brown  * @param dev      The WM8400 device to operate on.
27542fad570SMark Brown  * @param reg      The regulator to control.
27642fad570SMark Brown  * @param initdata Regulator initdata for the regulator.
27742fad570SMark Brown  */
27842fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg,
27942fad570SMark Brown 			      struct regulator_init_data *initdata)
28042fad570SMark Brown {
2811909e2f6SGreg Kroah-Hartman 	struct wm8400 *wm8400 = dev_get_drvdata(dev);
28242fad570SMark Brown 
28342fad570SMark Brown 	if (wm8400->regulators[reg].name)
28442fad570SMark Brown 		return -EBUSY;
28542fad570SMark Brown 
28642fad570SMark Brown 	initdata->driver_data = wm8400;
28742fad570SMark Brown 
28842fad570SMark Brown 	wm8400->regulators[reg].name = "wm8400-regulator";
28942fad570SMark Brown 	wm8400->regulators[reg].id = reg;
29042fad570SMark Brown 	wm8400->regulators[reg].dev.parent = dev;
29142fad570SMark Brown 	wm8400->regulators[reg].dev.platform_data = initdata;
29242fad570SMark Brown 
29342fad570SMark Brown 	return platform_device_register(&wm8400->regulators[reg]);
29442fad570SMark Brown }
29542fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator);
29642fad570SMark Brown 
29742fad570SMark Brown static int __init wm8400_regulator_init(void)
29842fad570SMark Brown {
29942fad570SMark Brown 	return platform_driver_register(&wm8400_regulator_driver);
30042fad570SMark Brown }
3015a1b22beSMark Brown subsys_initcall(wm8400_regulator_init);
30242fad570SMark Brown 
30342fad570SMark Brown static void __exit wm8400_regulator_exit(void)
30442fad570SMark Brown {
30542fad570SMark Brown 	platform_driver_unregister(&wm8400_regulator_driver);
30642fad570SMark Brown }
30742fad570SMark Brown module_exit(wm8400_regulator_exit);
30842fad570SMark Brown 
30942fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
31042fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver");
31142fad570SMark Brown MODULE_LICENSE("GPL");
31242fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator");
313