blob: 57d43a10e6b08b0af4d698d70cbf76c52dab48b9 [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 * @is_enabled: status of regulator (on/off)
33 * @update_bank: bank to control on/off
34 * @update_reg: register to control on/off
35 * @update_mask: mask to enable/disable and set mode of regulator
36 * @update_val: bits holding the regulator current mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000037 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
Lee Jonesd1a82002013-03-28 16:11:01 +000038 * normally this means high power mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000039 * @update_val_lp: bits to set EN pin active and LPn pin active
40 * normally this means low power mode
41 * @update_val_hw: bits to set regulator pins in HW control
42 * SysClkReq pins and logic will choose mode
Lee Jonesd1a82002013-03-28 16:11:01 +000043 */
44struct ab8500_ext_regulator_info {
45 struct device *dev;
46 struct regulator_desc desc;
47 struct regulator_dev *rdev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000048 struct ab8500_ext_regulator_cfg *cfg;
Lee Jonesd1a82002013-03-28 16:11:01 +000049 bool is_enabled;
50 u8 update_bank;
51 u8 update_reg;
52 u8 update_mask;
53 u8 update_val;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000054 u8 update_val_hp;
55 u8 update_val_lp;
56 u8 update_val_hw;
Lee Jonesd1a82002013-03-28 16:11:01 +000057};
58
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000059static int enable(struct ab8500_ext_regulator_info *info, u8 *regval)
Lee Jonesd1a82002013-03-28 16:11:01 +000060{
61 int ret;
Lee Jonesd1a82002013-03-28 16:11:01 +000062
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000063 *regval = info->update_val;
64
65 /*
66 * To satisfy both HW high power request and SW request, the regulator
67 * must be on in high power.
68 */
69 if (info->cfg && info->cfg->hwreq)
70 *regval = info->update_val_hp;
Lee Jonesd1a82002013-03-28 16:11:01 +000071
72 ret = abx500_mask_and_set_register_interruptible(info->dev,
73 info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000074 info->update_mask, *regval);
Axel Lin37daa8a2013-04-02 20:56:16 +080075 if (ret < 0) {
Lee Jonesd1a82002013-03-28 16:11:01 +000076 dev_err(rdev_get_dev(info->rdev),
77 "couldn't set enable bits for regulator\n");
Axel Lin37daa8a2013-04-02 20:56:16 +080078 return ret;
79 }
Lee Jonesd1a82002013-03-28 16:11:01 +000080
81 info->is_enabled = true;
82
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000083 return ret;
84}
85
86static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
87{
88 int ret;
89 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
90 u8 regval;
91
92 if (info == NULL) {
93 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
94 return -EINVAL;
95 }
96
97 ret = enable(info, &regval);
98
Lee Jonesd1a82002013-03-28 16:11:01 +000099 dev_dbg(rdev_get_dev(rdev), "%s-enable (bank, reg, mask, value):"
100 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
101 info->desc.name, info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000102 info->update_mask, regval);
103
104 return ret;
105}
106
107static int disable(struct ab8500_ext_regulator_info *info, u8 *regval)
108{
109 int ret;
110
111 *regval = 0x0;
112
113 /*
114 * Set the regulator in HW request mode if configured
115 */
116 if (info->cfg && info->cfg->hwreq)
117 *regval = info->update_val_hw;
118
119 ret = abx500_mask_and_set_register_interruptible(info->dev,
120 info->update_bank, info->update_reg,
121 info->update_mask, *regval);
Axel Lin37daa8a2013-04-02 20:56:16 +0800122 if (ret < 0) {
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000123 dev_err(rdev_get_dev(info->rdev),
124 "couldn't set disable bits for regulator\n");
Axel Lin37daa8a2013-04-02 20:56:16 +0800125 return ret;
126 }
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000127
128 info->is_enabled = false;
Lee Jonesd1a82002013-03-28 16:11:01 +0000129
130 return ret;
131}
132
133static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
134{
135 int ret;
136 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000137 u8 regval;
Lee Jonesd1a82002013-03-28 16:11:01 +0000138
139 if (info == NULL) {
140 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
141 return -EINVAL;
142 }
143
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000144 ret = disable(info, &regval);
Lee Jonesd1a82002013-03-28 16:11:01 +0000145
146 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
147 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
148 info->desc.name, info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000149 info->update_mask, regval);
Lee Jonesd1a82002013-03-28 16:11:01 +0000150
151 return ret;
152}
153
154static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
155{
156 int ret;
157 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
158 u8 regval;
159
160 if (info == NULL) {
161 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
162 return -EINVAL;
163 }
164
165 ret = abx500_get_register_interruptible(info->dev,
166 info->update_bank, info->update_reg, &regval);
167 if (ret < 0) {
168 dev_err(rdev_get_dev(rdev),
169 "couldn't read 0x%x register\n", info->update_reg);
170 return ret;
171 }
172
173 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
174 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
175 info->desc.name, info->update_bank, info->update_reg,
176 info->update_mask, regval);
177
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000178 if (((regval & info->update_mask) == info->update_val_lp) ||
179 ((regval & info->update_mask) == info->update_val_hp))
Lee Jonesd1a82002013-03-28 16:11:01 +0000180 info->is_enabled = true;
181 else
182 info->is_enabled = false;
183
184 return info->is_enabled;
185}
186
187static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
188 unsigned int mode)
189{
190 int ret = 0;
191 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
192
193 if (info == NULL) {
194 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
195 return -EINVAL;
196 }
197
198 switch (mode) {
199 case REGULATOR_MODE_NORMAL:
200 info->update_val = info->update_val_hp;
201 break;
202 case REGULATOR_MODE_IDLE:
203 info->update_val = info->update_val_lp;
204 break;
205
206 default:
207 return -EINVAL;
208 }
209
210 if (info->is_enabled) {
211 u8 regval;
212
213 ret = enable(info, &regval);
214 if (ret < 0)
215 dev_err(rdev_get_dev(rdev),
216 "Could not set regulator mode.\n");
217
218 dev_dbg(rdev_get_dev(rdev),
219 "%s-set_mode (bank, reg, mask, value): "
220 "0x%x, 0x%x, 0x%x, 0x%x\n",
221 info->desc.name, info->update_bank, info->update_reg,
222 info->update_mask, regval);
223 }
224
225 return ret;
226}
227
228static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
229{
230 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
231 int ret;
232
233 if (info == NULL) {
234 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
235 return -EINVAL;
236 }
237
238 if (info->update_val == info->update_val_hp)
239 ret = REGULATOR_MODE_NORMAL;
240 else if (info->update_val == info->update_val_lp)
241 ret = REGULATOR_MODE_IDLE;
242 else
243 ret = -EINVAL;
244
245 return ret;
246}
247
248static int ab8500_ext_fixed_get_voltage(struct regulator_dev *rdev)
249{
250 struct regulation_constraints *regu_constraints = rdev->constraints;
251
252 if (regu_constraints == NULL) {
253 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
254 return -EINVAL;
255 }
256 if (regu_constraints->min_uV && regu_constraints->max_uV) {
257 if (regu_constraints->min_uV == regu_constraints->max_uV)
258 return regu_constraints->min_uV;
259 }
260 return -EINVAL;
261}
262
263static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
264 unsigned selector)
265{
266 struct regulation_constraints *regu_constraints = rdev->constraints;
267
268 if (regu_constraints == NULL) {
269 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
270 return -EINVAL;
271 }
272 /* return the uV for the fixed regulators */
273 if (regu_constraints->min_uV && regu_constraints->max_uV) {
274 if (regu_constraints->min_uV == regu_constraints->max_uV)
275 return regu_constraints->min_uV;
276 }
277 return -EINVAL;
278}
279
280static struct regulator_ops ab8500_ext_regulator_ops = {
281 .enable = ab8500_ext_regulator_enable,
282 .disable = ab8500_ext_regulator_disable,
283 .is_enabled = ab8500_ext_regulator_is_enabled,
284 .set_mode = ab8500_ext_regulator_set_mode,
285 .get_mode = ab8500_ext_regulator_get_mode,
286 .get_voltage = ab8500_ext_fixed_get_voltage,
287 .list_voltage = ab8500_ext_list_voltage,
288};
289
Lee Jonesd1a82002013-03-28 16:11:01 +0000290static struct ab8500_ext_regulator_info
291 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
292 [AB8500_EXT_SUPPLY1] = {
293 .desc = {
294 .name = "VEXTSUPPLY1",
295 .ops = &ab8500_ext_regulator_ops,
296 .type = REGULATOR_VOLTAGE,
297 .id = AB8500_EXT_SUPPLY1,
298 .owner = THIS_MODULE,
299 .n_voltages = 1,
300 },
301 .update_bank = 0x04,
302 .update_reg = 0x08,
303 .update_mask = 0x03,
304 .update_val = 0x01,
305 .update_val_hp = 0x01,
306 .update_val_lp = 0x03,
307 .update_val_hw = 0x02,
308 },
309 [AB8500_EXT_SUPPLY2] = {
310 .desc = {
311 .name = "VEXTSUPPLY2",
312 .ops = &ab8500_ext_regulator_ops,
313 .type = REGULATOR_VOLTAGE,
314 .id = AB8500_EXT_SUPPLY2,
315 .owner = THIS_MODULE,
316 .n_voltages = 1,
317 },
318 .update_bank = 0x04,
319 .update_reg = 0x08,
320 .update_mask = 0x0c,
321 .update_val = 0x04,
322 .update_val_hp = 0x04,
323 .update_val_lp = 0x0c,
324 .update_val_hw = 0x08,
325 },
326 [AB8500_EXT_SUPPLY3] = {
327 .desc = {
328 .name = "VEXTSUPPLY3",
329 .ops = &ab8500_ext_regulator_ops,
330 .type = REGULATOR_VOLTAGE,
331 .id = AB8500_EXT_SUPPLY3,
332 .owner = THIS_MODULE,
333 .n_voltages = 1,
334 },
335 .update_bank = 0x04,
336 .update_reg = 0x08,
337 .update_mask = 0x30,
338 .update_val = 0x10,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000339 .update_val_hp = 0x10,
340 .update_val_lp = 0x30,
341 .update_val_hw = 0x20,
Lee Jonesd1a82002013-03-28 16:11:01 +0000342 },
343};
344
345int ab8500_ext_regulator_init(struct platform_device *pdev)
346{
347 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
348 struct ab8500_platform_data *ppdata;
349 struct ab8500_regulator_platform_data *pdata;
350 struct regulator_config config = { };
351 int i, err;
352
353 if (!ab8500) {
354 dev_err(&pdev->dev, "null mfd parent\n");
355 return -EINVAL;
356 }
357 ppdata = dev_get_platdata(ab8500->dev);
358 if (!ppdata) {
359 dev_err(&pdev->dev, "null parent pdata\n");
360 return -EINVAL;
361 }
362
363 pdata = ppdata->regulator;
364 if (!pdata) {
365 dev_err(&pdev->dev, "null pdata\n");
366 return -EINVAL;
367 }
368
369 /* make sure the platform data has the correct size */
370 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
371 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
372 return -EINVAL;
373 }
374
375 /* check for AB8500 2.x */
Bengt Jonssona6324702013-03-28 16:11:13 +0000376 if (is_ab8500_2p0_or_earlier(ab8500)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000377 struct ab8500_ext_regulator_info *info;
378
379 /* VextSupply3LPn is inverted on AB8500 2.x */
380 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
381 info->update_val = 0x30;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000382 info->update_val_hp = 0x30;
383 info->update_val_lp = 0x10;
Lee Jonesd1a82002013-03-28 16:11:01 +0000384 }
385
386 /* register all regulators */
387 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
388 struct ab8500_ext_regulator_info *info = NULL;
389
390 /* assign per-regulator data */
391 info = &ab8500_ext_regulator_info[i];
392 info->dev = &pdev->dev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000393 info->cfg = (struct ab8500_ext_regulator_cfg *)
394 pdata->ext_regulator[i].driver_data;
Lee Jonesd1a82002013-03-28 16:11:01 +0000395
396 config.dev = &pdev->dev;
397 config.init_data = &pdata->ext_regulator[i];
398 config.driver_data = info;
399
Lee Jonesbd44e2c2013-04-02 13:24:10 +0100400 if ((is_ab9540(ab8500) || is_ab8540(ab8500)) &&
Lee Jones0fe17e22013-04-02 13:24:06 +0100401 ((info->desc.id == AB8500_EXT_SUPPLY1) ||
402 (info->desc.id == AB8500_EXT_SUPPLY2) ||
403 (info->desc.id == AB8500_EXT_SUPPLY3)))
404 info->desc.ops = &ab8500_ext_regulator_ops;
405
Lee Jonesd1a82002013-03-28 16:11:01 +0000406 /* register regulator with framework */
407 info->rdev = regulator_register(&info->desc, &config);
408
409 if (IS_ERR(info->rdev)) {
410 err = PTR_ERR(info->rdev);
411 dev_err(&pdev->dev, "failed to register regulator %s\n",
412 info->desc.name);
413 /* when we fail, un-register all earlier regulators */
414 while (--i >= 0) {
415 info = &ab8500_ext_regulator_info[i];
416 regulator_unregister(info->rdev);
417 }
418 return err;
419 }
420
421 dev_dbg(rdev_get_dev(info->rdev),
422 "%s-probed\n", info->desc.name);
423 }
424
425 return 0;
426}
427
428int ab8500_ext_regulator_exit(struct platform_device *pdev)
429{
430 int i;
431
432 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
433 struct ab8500_ext_regulator_info *info = NULL;
434 info = &ab8500_ext_regulator_info[i];
435
436 dev_vdbg(rdev_get_dev(info->rdev),
437 "%s-remove\n", info->desc.name);
438
439 regulator_unregister(info->rdev);
440 }
441
442 return 0;
443}
444
445MODULE_LICENSE("GPL v2");
446MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
447MODULE_DESCRIPTION("AB8500 external regulator driver");
448MODULE_ALIAS("platform:ab8500-ext-regulator");