blob: 58f51bec13f25d2f47a488ca53fc1099bb683762 [file] [log] [blame]
Mark Brown42fad572008-09-11 11:12:01 +01001/*
2 * Regulator support for WM8400
3 *
4 * Copyright 2008 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 */
14
15#include <linux/bug.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040018#include <linux/module.h>
Mark Brown42fad572008-09-11 11:12:01 +010019#include <linux/regulator/driver.h>
20#include <linux/mfd/wm8400-private.h>
21
Mark Brown6692e432013-07-02 23:26:36 +010022static const struct regulator_linear_range wm8400_ldo_ranges[] = {
23 { .min_uV = 900000, .max_uV = 1600000, .min_sel = 0, .max_sel = 14,
24 .uV_step = 50000 },
25 { .min_uV = 1700000, .max_uV = 3300000, .min_sel = 15, .max_sel = 31,
26 .uV_step = 100000 },
27};
Mark Brown42fad572008-09-11 11:12:01 +010028
Mark Brownc54a1552012-05-10 00:12:09 +010029static struct regulator_ops wm8400_ldo_ops = {
30 .is_enabled = regulator_is_enabled_regmap,
31 .enable = regulator_enable_regmap,
32 .disable = regulator_disable_regmap,
Mark Brown6692e432013-07-02 23:26:36 +010033 .list_voltage = regulator_list_voltage_linear_range,
Mark Brownc54a1552012-05-10 00:12:09 +010034 .get_voltage_sel = regulator_get_voltage_sel_regmap,
35 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown6692e432013-07-02 23:26:36 +010036 .map_voltage = regulator_map_voltage_linear_range,
Mark Brownc54a1552012-05-10 00:12:09 +010037};
Mark Brown42fad572008-09-11 11:12:01 +010038
39static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
40{
41 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
42 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
43 u16 data[2];
44 int ret;
45
46 ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
47 data);
48 if (ret != 0)
49 return 0;
50
51 /* Datasheet: hibernate */
52 if (data[0] & WM8400_DC1_SLEEP)
53 return REGULATOR_MODE_STANDBY;
54
55 /* Datasheet: standby */
56 if (!(data[0] & WM8400_DC1_ACTIVE))
57 return REGULATOR_MODE_IDLE;
58
59 /* Datasheet: active with or without force PWM */
60 if (data[1] & WM8400_DC1_FRC_PWM)
61 return REGULATOR_MODE_FAST;
62 else
63 return REGULATOR_MODE_NORMAL;
64}
65
66static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
67{
68 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
69 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
70 int ret;
71
72 switch (mode) {
73 case REGULATOR_MODE_FAST:
74 /* Datasheet: active with force PWM */
75 ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
76 WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
77 if (ret != 0)
78 return ret;
79
80 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
81 WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
82 WM8400_DC1_ACTIVE);
83
84 case REGULATOR_MODE_NORMAL:
85 /* Datasheet: active */
86 ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
87 WM8400_DC1_FRC_PWM, 0);
88 if (ret != 0)
89 return ret;
90
91 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
92 WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
93 WM8400_DC1_ACTIVE);
94
95 case REGULATOR_MODE_IDLE:
96 /* Datasheet: standby */
Mark Brown42fad572008-09-11 11:12:01 +010097 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
Axel Lin40013762012-08-01 09:15:11 +080098 WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0);
Mark Brown42fad572008-09-11 11:12:01 +010099 default:
100 return -EINVAL;
101 }
102}
103
104static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
105 int input_uV, int output_uV,
106 int load_uA)
107{
108 return REGULATOR_MODE_NORMAL;
109}
110
111static struct regulator_ops wm8400_dcdc_ops = {
Mark Brownc54a1552012-05-10 00:12:09 +0100112 .is_enabled = regulator_is_enabled_regmap,
113 .enable = regulator_enable_regmap,
114 .disable = regulator_disable_regmap,
115 .list_voltage = regulator_list_voltage_linear,
Axel Lin27eeabb2012-05-31 17:40:22 +0800116 .map_voltage = regulator_map_voltage_linear,
Mark Brownc54a1552012-05-10 00:12:09 +0100117 .get_voltage_sel = regulator_get_voltage_sel_regmap,
118 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Mark Brown42fad572008-09-11 11:12:01 +0100119 .get_mode = wm8400_dcdc_get_mode,
120 .set_mode = wm8400_dcdc_set_mode,
121 .get_optimum_mode = wm8400_dcdc_get_optimum_mode,
122};
123
124static struct regulator_desc regulators[] = {
125 {
126 .name = "LDO1",
127 .id = WM8400_LDO1,
128 .ops = &wm8400_ldo_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100129 .enable_reg = WM8400_LDO1_CONTROL,
130 .enable_mask = WM8400_LDO1_ENA,
Mark Brown216765d2009-03-10 16:28:35 +0000131 .n_voltages = WM8400_LDO1_VSEL_MASK + 1,
Mark Brown6692e432013-07-02 23:26:36 +0100132 .linear_ranges = wm8400_ldo_ranges,
133 .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
Mark Brownc54a1552012-05-10 00:12:09 +0100134 .vsel_reg = WM8400_LDO1_CONTROL,
135 .vsel_mask = WM8400_LDO1_VSEL_MASK,
Mark Brown42fad572008-09-11 11:12:01 +0100136 .type = REGULATOR_VOLTAGE,
137 .owner = THIS_MODULE,
138 },
139 {
140 .name = "LDO2",
141 .id = WM8400_LDO2,
142 .ops = &wm8400_ldo_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100143 .enable_reg = WM8400_LDO2_CONTROL,
144 .enable_mask = WM8400_LDO2_ENA,
Mark Brown216765d2009-03-10 16:28:35 +0000145 .n_voltages = WM8400_LDO2_VSEL_MASK + 1,
Mark Brown6692e432013-07-02 23:26:36 +0100146 .linear_ranges = wm8400_ldo_ranges,
147 .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
Mark Brown42fad572008-09-11 11:12:01 +0100148 .type = REGULATOR_VOLTAGE,
Mark Brownc54a1552012-05-10 00:12:09 +0100149 .vsel_reg = WM8400_LDO2_CONTROL,
150 .vsel_mask = WM8400_LDO2_VSEL_MASK,
Mark Brown42fad572008-09-11 11:12:01 +0100151 .owner = THIS_MODULE,
152 },
153 {
154 .name = "LDO3",
155 .id = WM8400_LDO3,
156 .ops = &wm8400_ldo_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100157 .enable_reg = WM8400_LDO3_CONTROL,
158 .enable_mask = WM8400_LDO3_ENA,
Mark Brown216765d2009-03-10 16:28:35 +0000159 .n_voltages = WM8400_LDO3_VSEL_MASK + 1,
Mark Brown6692e432013-07-02 23:26:36 +0100160 .linear_ranges = wm8400_ldo_ranges,
161 .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
Mark Brownc54a1552012-05-10 00:12:09 +0100162 .vsel_reg = WM8400_LDO3_CONTROL,
163 .vsel_mask = WM8400_LDO3_VSEL_MASK,
Mark Brown42fad572008-09-11 11:12:01 +0100164 .type = REGULATOR_VOLTAGE,
165 .owner = THIS_MODULE,
166 },
167 {
168 .name = "LDO4",
169 .id = WM8400_LDO4,
170 .ops = &wm8400_ldo_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100171 .enable_reg = WM8400_LDO4_CONTROL,
172 .enable_mask = WM8400_LDO4_ENA,
Mark Brown216765d2009-03-10 16:28:35 +0000173 .n_voltages = WM8400_LDO4_VSEL_MASK + 1,
Mark Brown6692e432013-07-02 23:26:36 +0100174 .linear_ranges = wm8400_ldo_ranges,
175 .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges),
Mark Brownc54a1552012-05-10 00:12:09 +0100176 .vsel_reg = WM8400_LDO4_CONTROL,
177 .vsel_mask = WM8400_LDO4_VSEL_MASK,
Mark Brown42fad572008-09-11 11:12:01 +0100178 .type = REGULATOR_VOLTAGE,
179 .owner = THIS_MODULE,
180 },
181 {
182 .name = "DCDC1",
183 .id = WM8400_DCDC1,
184 .ops = &wm8400_dcdc_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100185 .enable_reg = WM8400_DCDC1_CONTROL_1,
186 .enable_mask = WM8400_DC1_ENA_MASK,
Mark Brown216765d2009-03-10 16:28:35 +0000187 .n_voltages = WM8400_DC1_VSEL_MASK + 1,
Mark Brownc54a1552012-05-10 00:12:09 +0100188 .vsel_reg = WM8400_DCDC1_CONTROL_1,
189 .vsel_mask = WM8400_DC1_VSEL_MASK,
190 .min_uV = 850000,
191 .uV_step = 25000,
Mark Brown42fad572008-09-11 11:12:01 +0100192 .type = REGULATOR_VOLTAGE,
193 .owner = THIS_MODULE,
194 },
195 {
196 .name = "DCDC2",
197 .id = WM8400_DCDC2,
198 .ops = &wm8400_dcdc_ops,
Mark Brownc54a1552012-05-10 00:12:09 +0100199 .enable_reg = WM8400_DCDC2_CONTROL_1,
200 .enable_mask = WM8400_DC1_ENA_MASK,
Mark Brown216765d2009-03-10 16:28:35 +0000201 .n_voltages = WM8400_DC2_VSEL_MASK + 1,
Mark Brownc54a1552012-05-10 00:12:09 +0100202 .vsel_reg = WM8400_DCDC2_CONTROL_1,
203 .vsel_mask = WM8400_DC2_VSEL_MASK,
204 .min_uV = 850000,
205 .uV_step = 25000,
Mark Brown42fad572008-09-11 11:12:01 +0100206 .type = REGULATOR_VOLTAGE,
207 .owner = THIS_MODULE,
208 },
209};
210
Bill Pembertona5023572012-11-19 13:22:22 -0500211static int wm8400_regulator_probe(struct platform_device *pdev)
Mark Brown42fad572008-09-11 11:12:01 +0100212{
Dmitry Torokhov1ad02bb2010-02-25 01:55:37 -0800213 struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]);
Mark Brownc1727082012-04-04 00:50:22 +0100214 struct regulator_config config = { };
Mark Brown42fad572008-09-11 11:12:01 +0100215 struct regulator_dev *rdev;
216
Mark Brownc1727082012-04-04 00:50:22 +0100217 config.dev = &pdev->dev;
Jingoo Handff91d02013-07-30 17:20:47 +0900218 config.init_data = dev_get_platdata(&pdev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100219 config.driver_data = wm8400;
Mark Brownc54a1552012-05-10 00:12:09 +0100220 config.regmap = wm8400->regmap;
Mark Brown42fad572008-09-11 11:12:01 +0100221
Mark Brownc1727082012-04-04 00:50:22 +0100222 rdev = regulator_register(&regulators[pdev->id], &config);
Mark Brown42fad572008-09-11 11:12:01 +0100223 if (IS_ERR(rdev))
224 return PTR_ERR(rdev);
225
Dmitry Torokhov1ad02bb2010-02-25 01:55:37 -0800226 platform_set_drvdata(pdev, rdev);
227
Mark Brown42fad572008-09-11 11:12:01 +0100228 return 0;
229}
230
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500231static int wm8400_regulator_remove(struct platform_device *pdev)
Mark Brown42fad572008-09-11 11:12:01 +0100232{
233 struct regulator_dev *rdev = platform_get_drvdata(pdev);
234
235 regulator_unregister(rdev);
236
237 return 0;
238}
239
240static struct platform_driver wm8400_regulator_driver = {
241 .driver = {
242 .name = "wm8400-regulator",
243 },
244 .probe = wm8400_regulator_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500245 .remove = wm8400_regulator_remove,
Mark Brown42fad572008-09-11 11:12:01 +0100246};
247
248/**
249 * wm8400_register_regulator - enable software control of a WM8400 regulator
250 *
251 * This function enables software control of a WM8400 regulator via
252 * the regulator API. It is intended to be called from the
253 * platform_init() callback of the WM8400 MFD driver.
254 *
255 * @param dev The WM8400 device to operate on.
256 * @param reg The regulator to control.
257 * @param initdata Regulator initdata for the regulator.
258 */
259int wm8400_register_regulator(struct device *dev, int reg,
260 struct regulator_init_data *initdata)
261{
Greg Kroah-Hartman1909e2f2009-04-30 15:21:37 -0700262 struct wm8400 *wm8400 = dev_get_drvdata(dev);
Mark Brown42fad572008-09-11 11:12:01 +0100263
264 if (wm8400->regulators[reg].name)
265 return -EBUSY;
266
267 initdata->driver_data = wm8400;
268
269 wm8400->regulators[reg].name = "wm8400-regulator";
270 wm8400->regulators[reg].id = reg;
271 wm8400->regulators[reg].dev.parent = dev;
Mark Brown42fad572008-09-11 11:12:01 +0100272 wm8400->regulators[reg].dev.platform_data = initdata;
273
274 return platform_device_register(&wm8400->regulators[reg]);
275}
276EXPORT_SYMBOL_GPL(wm8400_register_regulator);
277
278static int __init wm8400_regulator_init(void)
279{
280 return platform_driver_register(&wm8400_regulator_driver);
281}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100282subsys_initcall(wm8400_regulator_init);
Mark Brown42fad572008-09-11 11:12:01 +0100283
284static void __exit wm8400_regulator_exit(void)
285{
286 platform_driver_unregister(&wm8400_regulator_driver);
287}
288module_exit(wm8400_regulator_exit);
289
290MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
291MODULE_DESCRIPTION("WM8400 regulator driver");
292MODULE_LICENSE("GPL");
293MODULE_ALIAS("platform:wm8400-regulator");