blob: 02ff691cdb8b291b44ecc1569339a9251aa59987 [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>
Lee Jones30aa4b22013-06-07 17:11:27 +010019#include <linux/of.h>
Lee Jonesd1a82002013-03-28 16:11:01 +000020#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
Lee Jones30aa4b22013-06-07 17:11:27 +010023#include <linux/regulator/of_regulator.h>
Lee Jonesd1a82002013-03-28 16:11:01 +000024#include <linux/mfd/abx500.h>
25#include <linux/mfd/abx500/ab8500.h>
26#include <linux/regulator/ab8500.h>
27
28/**
29 * struct ab8500_ext_regulator_info - ab8500 regulator information
30 * @dev: device pointer
31 * @desc: regulator description
32 * @rdev: regulator device
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000033 * @cfg: regulator configuration (extension of regulator FW configuration)
Lee Jonesd1a82002013-03-28 16:11:01 +000034 * @update_bank: bank to control on/off
35 * @update_reg: register to control on/off
36 * @update_mask: mask to enable/disable and set mode of regulator
37 * @update_val: bits holding the regulator current mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000038 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
Lee Jonesd1a82002013-03-28 16:11:01 +000039 * normally this means high power mode
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000040 * @update_val_lp: bits to set EN pin active and LPn pin active
41 * normally this means low power mode
42 * @update_val_hw: bits to set regulator pins in HW control
43 * SysClkReq pins and logic will choose mode
Lee Jonesd1a82002013-03-28 16:11:01 +000044 */
45struct ab8500_ext_regulator_info {
46 struct device *dev;
47 struct regulator_desc desc;
48 struct regulator_dev *rdev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000049 struct ab8500_ext_regulator_cfg *cfg;
Lee Jonesd1a82002013-03-28 16:11:01 +000050 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 ab8500_ext_regulator_enable(struct regulator_dev *rdev)
60{
61 int ret;
62 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
63 u8 regval;
64
65 if (info == NULL) {
66 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
67 return -EINVAL;
68 }
69
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000070 /*
Axel Lin73847442013-04-16 23:33:00 +080071 * To satisfy both HW high power request and SW request, the regulator
72 * must be on in high power.
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000073 */
74 if (info->cfg && info->cfg->hwreq)
Axel Lin73847442013-04-16 23:33:00 +080075 regval = info->update_val_hp;
76 else
77 regval = info->update_val;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000078
79 ret = abx500_mask_and_set_register_interruptible(info->dev,
80 info->update_bank, info->update_reg,
Axel Lin73847442013-04-16 23:33:00 +080081 info->update_mask, regval);
Axel Lin37daa8a2013-04-02 20:56:16 +080082 if (ret < 0) {
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000083 dev_err(rdev_get_dev(info->rdev),
Axel Lin73847442013-04-16 23:33:00 +080084 "couldn't set enable bits for regulator\n");
Axel Lin37daa8a2013-04-02 20:56:16 +080085 return ret;
86 }
Bengt Jonsson18bc2b32013-03-28 16:11:06 +000087
Axel Lin73847442013-04-16 23:33:00 +080088 dev_dbg(rdev_get_dev(rdev),
89 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
90 info->desc.name, info->update_bank, info->update_reg,
91 info->update_mask, regval);
92
93 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +000094}
95
96static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
97{
98 int ret;
99 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000100 u8 regval;
Lee Jonesd1a82002013-03-28 16:11:01 +0000101
102 if (info == NULL) {
103 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
104 return -EINVAL;
105 }
106
Axel Lin73847442013-04-16 23:33:00 +0800107 /*
108 * Set the regulator in HW request mode if configured
109 */
110 if (info->cfg && info->cfg->hwreq)
111 regval = info->update_val_hw;
112 else
113 regval = 0;
114
115 ret = abx500_mask_and_set_register_interruptible(info->dev,
116 info->update_bank, info->update_reg,
117 info->update_mask, regval);
118 if (ret < 0) {
119 dev_err(rdev_get_dev(info->rdev),
120 "couldn't set disable bits for regulator\n");
121 return ret;
122 }
Lee Jonesd1a82002013-03-28 16:11:01 +0000123
124 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
125 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
126 info->desc.name, info->update_bank, info->update_reg,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000127 info->update_mask, regval);
Lee Jonesd1a82002013-03-28 16:11:01 +0000128
Axel Lin73847442013-04-16 23:33:00 +0800129 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000130}
131
132static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
133{
134 int ret;
135 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
136 u8 regval;
137
138 if (info == NULL) {
139 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
140 return -EINVAL;
141 }
142
143 ret = abx500_get_register_interruptible(info->dev,
144 info->update_bank, info->update_reg, &regval);
145 if (ret < 0) {
146 dev_err(rdev_get_dev(rdev),
147 "couldn't read 0x%x register\n", info->update_reg);
148 return ret;
149 }
150
151 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
152 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
153 info->desc.name, info->update_bank, info->update_reg,
154 info->update_mask, regval);
155
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000156 if (((regval & info->update_mask) == info->update_val_lp) ||
157 ((regval & info->update_mask) == info->update_val_hp))
Axel Lin9ab51a02013-04-07 23:13:39 +0800158 return 1;
Lee Jonesd1a82002013-03-28 16:11:01 +0000159 else
Axel Lin9ab51a02013-04-07 23:13:39 +0800160 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000161}
162
163static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
164 unsigned int mode)
165{
166 int ret = 0;
167 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
Axel Lin66511fa2013-04-16 23:29:12 +0800168 u8 regval;
Lee Jonesd1a82002013-03-28 16:11:01 +0000169
170 if (info == NULL) {
171 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
172 return -EINVAL;
173 }
174
175 switch (mode) {
176 case REGULATOR_MODE_NORMAL:
Axel Lin66511fa2013-04-16 23:29:12 +0800177 regval = info->update_val_hp;
Lee Jonesd1a82002013-03-28 16:11:01 +0000178 break;
179 case REGULATOR_MODE_IDLE:
Axel Lin66511fa2013-04-16 23:29:12 +0800180 regval = info->update_val_lp;
Lee Jonesd1a82002013-03-28 16:11:01 +0000181 break;
182
183 default:
184 return -EINVAL;
185 }
186
Axel Lin66511fa2013-04-16 23:29:12 +0800187 /* If regulator is enabled and info->cfg->hwreq is set, the regulator
188 must be on in high power, so we don't need to write the register with
189 the same value.
190 */
191 if (ab8500_ext_regulator_is_enabled(rdev) &&
192 !(info->cfg && info->cfg->hwreq)) {
193 ret = abx500_mask_and_set_register_interruptible(info->dev,
194 info->update_bank, info->update_reg,
195 info->update_mask, regval);
196 if (ret < 0) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000197 dev_err(rdev_get_dev(rdev),
198 "Could not set regulator mode.\n");
Axel Lin66511fa2013-04-16 23:29:12 +0800199 return ret;
200 }
Lee Jonesd1a82002013-03-28 16:11:01 +0000201
202 dev_dbg(rdev_get_dev(rdev),
203 "%s-set_mode (bank, reg, mask, value): "
204 "0x%x, 0x%x, 0x%x, 0x%x\n",
205 info->desc.name, info->update_bank, info->update_reg,
206 info->update_mask, regval);
207 }
208
Axel Lin66511fa2013-04-16 23:29:12 +0800209 info->update_val = regval;
210
211 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000212}
213
214static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
215{
216 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
217 int ret;
218
219 if (info == NULL) {
220 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
221 return -EINVAL;
222 }
223
224 if (info->update_val == info->update_val_hp)
225 ret = REGULATOR_MODE_NORMAL;
226 else if (info->update_val == info->update_val_lp)
227 ret = REGULATOR_MODE_IDLE;
228 else
229 ret = -EINVAL;
230
231 return ret;
232}
233
Lee Jones33fb8802013-06-07 17:11:25 +0100234static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
235 int max_uV, unsigned *selector)
236{
237 struct regulation_constraints *regu_constraints = rdev->constraints;
238
239 if (!regu_constraints) {
240 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
241 return -EINVAL;
242 }
243
244 if (regu_constraints->min_uV == min_uV &&
245 regu_constraints->max_uV == max_uV)
246 return 0;
247
248 dev_err(rdev_get_dev(rdev),
249 "Requested min %duV max %duV != constrained min %duV max %duV\n",
250 min_uV, max_uV,
251 regu_constraints->min_uV, regu_constraints->max_uV);
252
253 return -EINVAL;
254}
255
Lee Jonesd1a82002013-03-28 16:11:01 +0000256static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
257 unsigned selector)
258{
259 struct regulation_constraints *regu_constraints = rdev->constraints;
260
261 if (regu_constraints == NULL) {
262 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
263 return -EINVAL;
264 }
265 /* return the uV for the fixed regulators */
266 if (regu_constraints->min_uV && regu_constraints->max_uV) {
267 if (regu_constraints->min_uV == regu_constraints->max_uV)
268 return regu_constraints->min_uV;
269 }
270 return -EINVAL;
271}
272
273static struct regulator_ops ab8500_ext_regulator_ops = {
274 .enable = ab8500_ext_regulator_enable,
275 .disable = ab8500_ext_regulator_disable,
276 .is_enabled = ab8500_ext_regulator_is_enabled,
277 .set_mode = ab8500_ext_regulator_set_mode,
278 .get_mode = ab8500_ext_regulator_get_mode,
Lee Jones33fb8802013-06-07 17:11:25 +0100279 .set_voltage = ab8500_ext_set_voltage,
Lee Jonesd1a82002013-03-28 16:11:01 +0000280 .list_voltage = ab8500_ext_list_voltage,
281};
282
Lee Jonesd1a82002013-03-28 16:11:01 +0000283static struct ab8500_ext_regulator_info
284 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
285 [AB8500_EXT_SUPPLY1] = {
286 .desc = {
287 .name = "VEXTSUPPLY1",
288 .ops = &ab8500_ext_regulator_ops,
289 .type = REGULATOR_VOLTAGE,
290 .id = AB8500_EXT_SUPPLY1,
291 .owner = THIS_MODULE,
292 .n_voltages = 1,
293 },
294 .update_bank = 0x04,
295 .update_reg = 0x08,
296 .update_mask = 0x03,
297 .update_val = 0x01,
298 .update_val_hp = 0x01,
299 .update_val_lp = 0x03,
300 .update_val_hw = 0x02,
301 },
302 [AB8500_EXT_SUPPLY2] = {
303 .desc = {
304 .name = "VEXTSUPPLY2",
305 .ops = &ab8500_ext_regulator_ops,
306 .type = REGULATOR_VOLTAGE,
307 .id = AB8500_EXT_SUPPLY2,
308 .owner = THIS_MODULE,
309 .n_voltages = 1,
310 },
311 .update_bank = 0x04,
312 .update_reg = 0x08,
313 .update_mask = 0x0c,
314 .update_val = 0x04,
315 .update_val_hp = 0x04,
316 .update_val_lp = 0x0c,
317 .update_val_hw = 0x08,
318 },
319 [AB8500_EXT_SUPPLY3] = {
320 .desc = {
321 .name = "VEXTSUPPLY3",
322 .ops = &ab8500_ext_regulator_ops,
323 .type = REGULATOR_VOLTAGE,
324 .id = AB8500_EXT_SUPPLY3,
325 .owner = THIS_MODULE,
326 .n_voltages = 1,
327 },
328 .update_bank = 0x04,
329 .update_reg = 0x08,
330 .update_mask = 0x30,
331 .update_val = 0x10,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000332 .update_val_hp = 0x10,
333 .update_val_lp = 0x30,
334 .update_val_hw = 0x20,
Lee Jonesd1a82002013-03-28 16:11:01 +0000335 },
336};
337
Lee Jones30aa4b22013-06-07 17:11:27 +0100338static struct of_regulator_match ab8500_ext_regulator_match[] = {
339 { .name = "ab8500_ext1", .driver_data = (void *) AB8500_EXT_SUPPLY1, },
340 { .name = "ab8500_ext2", .driver_data = (void *) AB8500_EXT_SUPPLY2, },
341 { .name = "ab8500_ext3", .driver_data = (void *) AB8500_EXT_SUPPLY3, },
342};
343
Sachin Kamat08d49f42013-06-26 10:13:37 +0530344static int ab8500_ext_regulator_probe(struct platform_device *pdev)
Lee Jonesd1a82002013-03-28 16:11:01 +0000345{
346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
347 struct ab8500_platform_data *ppdata;
348 struct ab8500_regulator_platform_data *pdata;
Lee Jones30aa4b22013-06-07 17:11:27 +0100349 struct device_node *np = pdev->dev.of_node;
Lee Jonesd1a82002013-03-28 16:11:01 +0000350 struct regulator_config config = { };
351 int i, err;
352
Lee Jones30aa4b22013-06-07 17:11:27 +0100353 if (np) {
354 err = of_regulator_match(&pdev->dev, np,
355 ab8500_ext_regulator_match,
356 ARRAY_SIZE(ab8500_ext_regulator_match));
357 if (err < 0) {
358 dev_err(&pdev->dev,
359 "Error parsing regulator init data: %d\n", err);
360 return err;
361 }
362 }
363
Lee Jonesd1a82002013-03-28 16:11:01 +0000364 if (!ab8500) {
365 dev_err(&pdev->dev, "null mfd parent\n");
366 return -EINVAL;
367 }
Lee Jones30aa4b22013-06-07 17:11:27 +0100368
Lee Jonesd1a82002013-03-28 16:11:01 +0000369 ppdata = dev_get_platdata(ab8500->dev);
370 if (!ppdata) {
371 dev_err(&pdev->dev, "null parent pdata\n");
372 return -EINVAL;
373 }
374
375 pdata = ppdata->regulator;
376 if (!pdata) {
377 dev_err(&pdev->dev, "null pdata\n");
378 return -EINVAL;
379 }
380
381 /* make sure the platform data has the correct size */
382 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
383 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
384 return -EINVAL;
385 }
386
387 /* check for AB8500 2.x */
Bengt Jonssona6324702013-03-28 16:11:13 +0000388 if (is_ab8500_2p0_or_earlier(ab8500)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000389 struct ab8500_ext_regulator_info *info;
390
391 /* VextSupply3LPn is inverted on AB8500 2.x */
392 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
393 info->update_val = 0x30;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000394 info->update_val_hp = 0x30;
395 info->update_val_lp = 0x10;
Lee Jonesd1a82002013-03-28 16:11:01 +0000396 }
397
398 /* register all regulators */
399 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
400 struct ab8500_ext_regulator_info *info = NULL;
401
402 /* assign per-regulator data */
403 info = &ab8500_ext_regulator_info[i];
404 info->dev = &pdev->dev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000405 info->cfg = (struct ab8500_ext_regulator_cfg *)
406 pdata->ext_regulator[i].driver_data;
Lee Jonesd1a82002013-03-28 16:11:01 +0000407
408 config.dev = &pdev->dev;
Lee Jonesd1a82002013-03-28 16:11:01 +0000409 config.driver_data = info;
Lee Jones30aa4b22013-06-07 17:11:27 +0100410 config.of_node = ab8500_ext_regulator_match[i].of_node;
411 config.init_data = (np) ?
412 ab8500_ext_regulator_match[i].init_data :
413 &pdata->ext_regulator[i];
Lee Jonesd1a82002013-03-28 16:11:01 +0000414
Lee Jonesd1a82002013-03-28 16:11:01 +0000415 /* register regulator with framework */
416 info->rdev = regulator_register(&info->desc, &config);
Lee Jonesd1a82002013-03-28 16:11:01 +0000417 if (IS_ERR(info->rdev)) {
418 err = PTR_ERR(info->rdev);
419 dev_err(&pdev->dev, "failed to register regulator %s\n",
420 info->desc.name);
421 /* when we fail, un-register all earlier regulators */
422 while (--i >= 0) {
423 info = &ab8500_ext_regulator_info[i];
424 regulator_unregister(info->rdev);
425 }
426 return err;
427 }
428
429 dev_dbg(rdev_get_dev(info->rdev),
430 "%s-probed\n", info->desc.name);
431 }
432
433 return 0;
434}
435
Sachin Kamat08d49f42013-06-26 10:13:37 +0530436static int ab8500_ext_regulator_remove(struct platform_device *pdev)
Lee Jonesd1a82002013-03-28 16:11:01 +0000437{
438 int i;
439
440 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
441 struct ab8500_ext_regulator_info *info = NULL;
442 info = &ab8500_ext_regulator_info[i];
443
444 dev_vdbg(rdev_get_dev(info->rdev),
445 "%s-remove\n", info->desc.name);
446
447 regulator_unregister(info->rdev);
448 }
Lee Jones5a49b4a2013-06-07 17:11:26 +0100449
450 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000451}
452
Lee Jones5a49b4a2013-06-07 17:11:26 +0100453static struct platform_driver ab8500_ext_regulator_driver = {
454 .probe = ab8500_ext_regulator_probe,
455 .remove = ab8500_ext_regulator_remove,
456 .driver = {
457 .name = "ab8500-ext-regulator",
458 .owner = THIS_MODULE,
459 },
460};
461
462static int __init ab8500_ext_regulator_init(void)
463{
464 int ret;
465
466 ret = platform_driver_register(&ab8500_ext_regulator_driver);
467 if (ret)
468 pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
469
470 return ret;
471}
472subsys_initcall(ab8500_ext_regulator_init);
473
474static void __exit ab8500_ext_regulator_exit(void)
475{
476 platform_driver_unregister(&ab8500_ext_regulator_driver);
477}
478module_exit(ab8500_ext_regulator_exit);
479
Lee Jonesd1a82002013-03-28 16:11:01 +0000480MODULE_LICENSE("GPL v2");
481MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
482MODULE_DESCRIPTION("AB8500 external regulator driver");
483MODULE_ALIAS("platform:ab8500-ext-regulator");