blob: 20680f30a968e2599acb7ea8f38f320e1f3d7e35 [file] [log] [blame]
Lee Jonesd1a82002013-03-28 16:11:01 +00001/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 *
6 * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
7 *
8 * This file is based on drivers/regulator/ab8500.c
9 *
10 * AB8500 external regulators
11 *
12 * ab8500-ext supports the following regulators:
13 * - VextSupply3
14 */
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/mfd/abx500.h>
23#include <linux/mfd/abx500/ab8500.h>
24#include <linux/regulator/ab8500.h>
25
26/**
27 * struct ab8500_ext_regulator_info - ab8500 regulator information
28 * @dev: device pointer
29 * @desc: regulator description
30 * @rdev: regulator device
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000031 * @cfg: regulator configuration (extension of regulator FW configuration)
Lee Jonesd1a82002013-03-28 16:11:01 +000032 * @update_bank: bank to control on/off
33 * @update_reg: register to control on/off
34 * @update_mask: mask to enable/disable and set mode of regulator
35 * @update_val: bits holding the regulator current mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000036 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
Lee Jonesd1a82002013-03-28 16:11:01 +000037 * normally this means high power mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000038 * @update_val_lp: bits to set EN pin active and LPn pin active
39 * normally this means low power mode
40 * @update_val_hw: bits to set regulator pins in HW control
41 * SysClkReq pins and logic will choose mode
Lee Jonesd1a82002013-03-28 16:11:01 +000042 */
43struct ab8500_ext_regulator_info {
44 struct device *dev;
45 struct regulator_desc desc;
46 struct regulator_dev *rdev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000047 struct ab8500_ext_regulator_cfg *cfg;
Lee Jonesd1a82002013-03-28 16:11:01 +000048 u8 update_bank;
49 u8 update_reg;
50 u8 update_mask;
51 u8 update_val;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000052 u8 update_val_hp;
53 u8 update_val_lp;
54 u8 update_val_hw;
Lee Jonesd1a82002013-03-28 16:11:01 +000055};
56
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000057static int enable(struct ab8500_ext_regulator_info *info, u8 *regval)
Lee Jonesd1a82002013-03-28 16:11:01 +000058{
59 int ret;
Lee Jonesd1a82002013-03-28 16:11:01 +000060
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000061 *regval = info->update_val;
62
63 /*
64 * To satisfy both HW high power request and SW request, the regulator
65 * must be on in high power.
66 */
67 if (info->cfg && info->cfg->hwreq)
68 *regval = info->update_val_hp;
Lee Jonesd1a82002013-03-28 16:11:01 +000069
70 ret = abx500_mask_and_set_register_interruptible(info->dev,
71 info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000072 info->update_mask, *regval);
Axel Lin37daa8a2013-04-02 20:56:16 +080073 if (ret < 0) {
Lee Jonesd1a82002013-03-28 16:11:01 +000074 dev_err(rdev_get_dev(info->rdev),
75 "couldn't set enable bits for regulator\n");
Axel Lin37daa8a2013-04-02 20:56:16 +080076 return ret;
77 }
Lee Jonesd1a82002013-03-28 16:11:01 +000078
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000079 return ret;
80}
81
82static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
83{
84 int ret;
85 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
86 u8 regval;
87
88 if (info == NULL) {
89 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
90 return -EINVAL;
91 }
92
93 ret = enable(info, &regval);
94
Lee Jonesd1a82002013-03-28 16:11:01 +000095 dev_dbg(rdev_get_dev(rdev), "%s-enable (bank, reg, mask, value):"
96 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
97 info->desc.name, info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000098 info->update_mask, regval);
99
100 return ret;
101}
102
103static int disable(struct ab8500_ext_regulator_info *info, u8 *regval)
104{
105 int ret;
106
107 *regval = 0x0;
108
109 /*
110 * Set the regulator in HW request mode if configured
111 */
112 if (info->cfg && info->cfg->hwreq)
113 *regval = info->update_val_hw;
114
115 ret = abx500_mask_and_set_register_interruptible(info->dev,
116 info->update_bank, info->update_reg,
117 info->update_mask, *regval);
Axel Lin37daa8a2013-04-02 20:56:16 +0800118 if (ret < 0) {
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000119 dev_err(rdev_get_dev(info->rdev),
120 "couldn't set disable bits for regulator\n");
Axel Lin37daa8a2013-04-02 20:56:16 +0800121 return ret;
122 }
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000123
Lee Jonesd1a82002013-03-28 16:11:01 +0000124 return ret;
125}
126
127static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
128{
129 int ret;
130 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000131 u8 regval;
Lee Jonesd1a82002013-03-28 16:11:01 +0000132
133 if (info == NULL) {
134 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
135 return -EINVAL;
136 }
137
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000138 ret = disable(info, &regval);
Lee Jonesd1a82002013-03-28 16:11:01 +0000139
140 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
141 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
142 info->desc.name, info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000143 info->update_mask, regval);
Lee Jonesd1a82002013-03-28 16:11:01 +0000144
145 return ret;
146}
147
148static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
149{
150 int ret;
151 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
152 u8 regval;
153
154 if (info == NULL) {
155 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
156 return -EINVAL;
157 }
158
159 ret = abx500_get_register_interruptible(info->dev,
160 info->update_bank, info->update_reg, &regval);
161 if (ret < 0) {
162 dev_err(rdev_get_dev(rdev),
163 "couldn't read 0x%x register\n", info->update_reg);
164 return ret;
165 }
166
167 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
168 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
169 info->desc.name, info->update_bank, info->update_reg,
170 info->update_mask, regval);
171
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000172 if (((regval & info->update_mask) == info->update_val_lp) ||
173 ((regval & info->update_mask) == info->update_val_hp))
Axel Lin9ab51a02013-04-07 23:13:39 +0800174 return 1;
Lee Jonesd1a82002013-03-28 16:11:01 +0000175 else
Axel Lin9ab51a02013-04-07 23:13:39 +0800176 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000177}
178
179static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
180 unsigned int mode)
181{
182 int ret = 0;
183 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
184
185 if (info == NULL) {
186 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
187 return -EINVAL;
188 }
189
190 switch (mode) {
191 case REGULATOR_MODE_NORMAL:
192 info->update_val = info->update_val_hp;
193 break;
194 case REGULATOR_MODE_IDLE:
195 info->update_val = info->update_val_lp;
196 break;
197
198 default:
199 return -EINVAL;
200 }
201
Axel Lin9ab51a02013-04-07 23:13:39 +0800202 if (ab8500_ext_regulator_is_enabled(rdev)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000203 u8 regval;
204
205 ret = enable(info, &regval);
206 if (ret < 0)
207 dev_err(rdev_get_dev(rdev),
208 "Could not set regulator mode.\n");
209
210 dev_dbg(rdev_get_dev(rdev),
211 "%s-set_mode (bank, reg, mask, value): "
212 "0x%x, 0x%x, 0x%x, 0x%x\n",
213 info->desc.name, info->update_bank, info->update_reg,
214 info->update_mask, regval);
215 }
216
217 return ret;
218}
219
220static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
221{
222 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
223 int ret;
224
225 if (info == NULL) {
226 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
227 return -EINVAL;
228 }
229
230 if (info->update_val == info->update_val_hp)
231 ret = REGULATOR_MODE_NORMAL;
232 else if (info->update_val == info->update_val_lp)
233 ret = REGULATOR_MODE_IDLE;
234 else
235 ret = -EINVAL;
236
237 return ret;
238}
239
Lee Jonesd1a82002013-03-28 16:11:01 +0000240static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
241 unsigned selector)
242{
243 struct regulation_constraints *regu_constraints = rdev->constraints;
244
245 if (regu_constraints == NULL) {
246 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
247 return -EINVAL;
248 }
249 /* return the uV for the fixed regulators */
250 if (regu_constraints->min_uV && regu_constraints->max_uV) {
251 if (regu_constraints->min_uV == regu_constraints->max_uV)
252 return regu_constraints->min_uV;
253 }
254 return -EINVAL;
255}
256
257static struct regulator_ops ab8500_ext_regulator_ops = {
258 .enable = ab8500_ext_regulator_enable,
259 .disable = ab8500_ext_regulator_disable,
260 .is_enabled = ab8500_ext_regulator_is_enabled,
261 .set_mode = ab8500_ext_regulator_set_mode,
262 .get_mode = ab8500_ext_regulator_get_mode,
Lee Jonesd1a82002013-03-28 16:11:01 +0000263 .list_voltage = ab8500_ext_list_voltage,
264};
265
Lee Jonesd1a82002013-03-28 16:11:01 +0000266static struct ab8500_ext_regulator_info
267 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
268 [AB8500_EXT_SUPPLY1] = {
269 .desc = {
270 .name = "VEXTSUPPLY1",
271 .ops = &ab8500_ext_regulator_ops,
272 .type = REGULATOR_VOLTAGE,
273 .id = AB8500_EXT_SUPPLY1,
274 .owner = THIS_MODULE,
275 .n_voltages = 1,
276 },
277 .update_bank = 0x04,
278 .update_reg = 0x08,
279 .update_mask = 0x03,
280 .update_val = 0x01,
281 .update_val_hp = 0x01,
282 .update_val_lp = 0x03,
283 .update_val_hw = 0x02,
284 },
285 [AB8500_EXT_SUPPLY2] = {
286 .desc = {
287 .name = "VEXTSUPPLY2",
288 .ops = &ab8500_ext_regulator_ops,
289 .type = REGULATOR_VOLTAGE,
290 .id = AB8500_EXT_SUPPLY2,
291 .owner = THIS_MODULE,
292 .n_voltages = 1,
293 },
294 .update_bank = 0x04,
295 .update_reg = 0x08,
296 .update_mask = 0x0c,
297 .update_val = 0x04,
298 .update_val_hp = 0x04,
299 .update_val_lp = 0x0c,
300 .update_val_hw = 0x08,
301 },
302 [AB8500_EXT_SUPPLY3] = {
303 .desc = {
304 .name = "VEXTSUPPLY3",
305 .ops = &ab8500_ext_regulator_ops,
306 .type = REGULATOR_VOLTAGE,
307 .id = AB8500_EXT_SUPPLY3,
308 .owner = THIS_MODULE,
309 .n_voltages = 1,
310 },
311 .update_bank = 0x04,
312 .update_reg = 0x08,
313 .update_mask = 0x30,
314 .update_val = 0x10,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000315 .update_val_hp = 0x10,
316 .update_val_lp = 0x30,
317 .update_val_hw = 0x20,
Lee Jonesd1a82002013-03-28 16:11:01 +0000318 },
319};
320
321int ab8500_ext_regulator_init(struct platform_device *pdev)
322{
323 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
324 struct ab8500_platform_data *ppdata;
325 struct ab8500_regulator_platform_data *pdata;
326 struct regulator_config config = { };
327 int i, err;
328
329 if (!ab8500) {
330 dev_err(&pdev->dev, "null mfd parent\n");
331 return -EINVAL;
332 }
333 ppdata = dev_get_platdata(ab8500->dev);
334 if (!ppdata) {
335 dev_err(&pdev->dev, "null parent pdata\n");
336 return -EINVAL;
337 }
338
339 pdata = ppdata->regulator;
340 if (!pdata) {
341 dev_err(&pdev->dev, "null pdata\n");
342 return -EINVAL;
343 }
344
345 /* make sure the platform data has the correct size */
346 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
347 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
348 return -EINVAL;
349 }
350
351 /* check for AB8500 2.x */
Bengt Jonssona6324702013-03-28 16:11:13 +0000352 if (is_ab8500_2p0_or_earlier(ab8500)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000353 struct ab8500_ext_regulator_info *info;
354
355 /* VextSupply3LPn is inverted on AB8500 2.x */
356 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
357 info->update_val = 0x30;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000358 info->update_val_hp = 0x30;
359 info->update_val_lp = 0x10;
Lee Jonesd1a82002013-03-28 16:11:01 +0000360 }
361
362 /* register all regulators */
363 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
364 struct ab8500_ext_regulator_info *info = NULL;
365
366 /* assign per-regulator data */
367 info = &ab8500_ext_regulator_info[i];
368 info->dev = &pdev->dev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000369 info->cfg = (struct ab8500_ext_regulator_cfg *)
370 pdata->ext_regulator[i].driver_data;
Lee Jonesd1a82002013-03-28 16:11:01 +0000371
372 config.dev = &pdev->dev;
373 config.init_data = &pdata->ext_regulator[i];
374 config.driver_data = info;
375
Lee Jonesd1a82002013-03-28 16:11:01 +0000376 /* register regulator with framework */
377 info->rdev = regulator_register(&info->desc, &config);
Lee Jonesd1a82002013-03-28 16:11:01 +0000378 if (IS_ERR(info->rdev)) {
379 err = PTR_ERR(info->rdev);
380 dev_err(&pdev->dev, "failed to register regulator %s\n",
381 info->desc.name);
382 /* when we fail, un-register all earlier regulators */
383 while (--i >= 0) {
384 info = &ab8500_ext_regulator_info[i];
385 regulator_unregister(info->rdev);
386 }
387 return err;
388 }
389
390 dev_dbg(rdev_get_dev(info->rdev),
391 "%s-probed\n", info->desc.name);
392 }
393
394 return 0;
395}
396
Axel Lin3480c0c2013-04-11 12:04:18 +0800397void ab8500_ext_regulator_exit(struct platform_device *pdev)
Lee Jonesd1a82002013-03-28 16:11:01 +0000398{
399 int i;
400
401 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
402 struct ab8500_ext_regulator_info *info = NULL;
403 info = &ab8500_ext_regulator_info[i];
404
405 dev_vdbg(rdev_get_dev(info->rdev),
406 "%s-remove\n", info->desc.name);
407
408 regulator_unregister(info->rdev);
409 }
Lee Jonesd1a82002013-03-28 16:11:01 +0000410}
411
412MODULE_LICENSE("GPL v2");
413MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
414MODULE_DESCRIPTION("AB8500 external regulator driver");
415MODULE_ALIAS("platform:ab8500-ext-regulator");