blob: 5e604a2ad7cdf1958de1721580898bf103c5d09b [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
240static int ab8500_ext_fixed_get_voltage(struct regulator_dev *rdev)
241{
242 struct regulation_constraints *regu_constraints = rdev->constraints;
243
244 if (regu_constraints == NULL) {
245 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
246 return -EINVAL;
247 }
248 if (regu_constraints->min_uV && regu_constraints->max_uV) {
249 if (regu_constraints->min_uV == regu_constraints->max_uV)
250 return regu_constraints->min_uV;
251 }
252 return -EINVAL;
253}
254
255static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
256 unsigned selector)
257{
258 struct regulation_constraints *regu_constraints = rdev->constraints;
259
260 if (regu_constraints == NULL) {
261 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
262 return -EINVAL;
263 }
264 /* return the uV for the fixed regulators */
265 if (regu_constraints->min_uV && regu_constraints->max_uV) {
266 if (regu_constraints->min_uV == regu_constraints->max_uV)
267 return regu_constraints->min_uV;
268 }
269 return -EINVAL;
270}
271
272static struct regulator_ops ab8500_ext_regulator_ops = {
273 .enable = ab8500_ext_regulator_enable,
274 .disable = ab8500_ext_regulator_disable,
275 .is_enabled = ab8500_ext_regulator_is_enabled,
276 .set_mode = ab8500_ext_regulator_set_mode,
277 .get_mode = ab8500_ext_regulator_get_mode,
278 .get_voltage = ab8500_ext_fixed_get_voltage,
279 .list_voltage = ab8500_ext_list_voltage,
280};
281
Lee Jonesd1a82002013-03-28 16:11:01 +0000282static struct ab8500_ext_regulator_info
283 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
284 [AB8500_EXT_SUPPLY1] = {
285 .desc = {
286 .name = "VEXTSUPPLY1",
287 .ops = &ab8500_ext_regulator_ops,
288 .type = REGULATOR_VOLTAGE,
289 .id = AB8500_EXT_SUPPLY1,
290 .owner = THIS_MODULE,
291 .n_voltages = 1,
292 },
293 .update_bank = 0x04,
294 .update_reg = 0x08,
295 .update_mask = 0x03,
296 .update_val = 0x01,
297 .update_val_hp = 0x01,
298 .update_val_lp = 0x03,
299 .update_val_hw = 0x02,
300 },
301 [AB8500_EXT_SUPPLY2] = {
302 .desc = {
303 .name = "VEXTSUPPLY2",
304 .ops = &ab8500_ext_regulator_ops,
305 .type = REGULATOR_VOLTAGE,
306 .id = AB8500_EXT_SUPPLY2,
307 .owner = THIS_MODULE,
308 .n_voltages = 1,
309 },
310 .update_bank = 0x04,
311 .update_reg = 0x08,
312 .update_mask = 0x0c,
313 .update_val = 0x04,
314 .update_val_hp = 0x04,
315 .update_val_lp = 0x0c,
316 .update_val_hw = 0x08,
317 },
318 [AB8500_EXT_SUPPLY3] = {
319 .desc = {
320 .name = "VEXTSUPPLY3",
321 .ops = &ab8500_ext_regulator_ops,
322 .type = REGULATOR_VOLTAGE,
323 .id = AB8500_EXT_SUPPLY3,
324 .owner = THIS_MODULE,
325 .n_voltages = 1,
326 },
327 .update_bank = 0x04,
328 .update_reg = 0x08,
329 .update_mask = 0x30,
330 .update_val = 0x10,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000331 .update_val_hp = 0x10,
332 .update_val_lp = 0x30,
333 .update_val_hw = 0x20,
Lee Jonesd1a82002013-03-28 16:11:01 +0000334 },
335};
336
337int ab8500_ext_regulator_init(struct platform_device *pdev)
338{
339 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
340 struct ab8500_platform_data *ppdata;
341 struct ab8500_regulator_platform_data *pdata;
342 struct regulator_config config = { };
343 int i, err;
344
345 if (!ab8500) {
346 dev_err(&pdev->dev, "null mfd parent\n");
347 return -EINVAL;
348 }
349 ppdata = dev_get_platdata(ab8500->dev);
350 if (!ppdata) {
351 dev_err(&pdev->dev, "null parent pdata\n");
352 return -EINVAL;
353 }
354
355 pdata = ppdata->regulator;
356 if (!pdata) {
357 dev_err(&pdev->dev, "null pdata\n");
358 return -EINVAL;
359 }
360
361 /* make sure the platform data has the correct size */
362 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
363 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
364 return -EINVAL;
365 }
366
367 /* check for AB8500 2.x */
Bengt Jonssona6324702013-03-28 16:11:13 +0000368 if (is_ab8500_2p0_or_earlier(ab8500)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000369 struct ab8500_ext_regulator_info *info;
370
371 /* VextSupply3LPn is inverted on AB8500 2.x */
372 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
373 info->update_val = 0x30;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000374 info->update_val_hp = 0x30;
375 info->update_val_lp = 0x10;
Lee Jonesd1a82002013-03-28 16:11:01 +0000376 }
377
378 /* register all regulators */
379 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
380 struct ab8500_ext_regulator_info *info = NULL;
381
382 /* assign per-regulator data */
383 info = &ab8500_ext_regulator_info[i];
384 info->dev = &pdev->dev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000385 info->cfg = (struct ab8500_ext_regulator_cfg *)
386 pdata->ext_regulator[i].driver_data;
Lee Jonesd1a82002013-03-28 16:11:01 +0000387
388 config.dev = &pdev->dev;
389 config.init_data = &pdata->ext_regulator[i];
390 config.driver_data = info;
391
Lee Jonesbd44e2c2013-04-02 13:24:10 +0100392 if ((is_ab9540(ab8500) || is_ab8540(ab8500)) &&
Lee Jones0fe17e22013-04-02 13:24:06 +0100393 ((info->desc.id == AB8500_EXT_SUPPLY1) ||
394 (info->desc.id == AB8500_EXT_SUPPLY2) ||
395 (info->desc.id == AB8500_EXT_SUPPLY3)))
396 info->desc.ops = &ab8500_ext_regulator_ops;
397
Lee Jonesd1a82002013-03-28 16:11:01 +0000398 /* register regulator with framework */
399 info->rdev = regulator_register(&info->desc, &config);
400
401 if (IS_ERR(info->rdev)) {
402 err = PTR_ERR(info->rdev);
403 dev_err(&pdev->dev, "failed to register regulator %s\n",
404 info->desc.name);
405 /* when we fail, un-register all earlier regulators */
406 while (--i >= 0) {
407 info = &ab8500_ext_regulator_info[i];
408 regulator_unregister(info->rdev);
409 }
410 return err;
411 }
412
413 dev_dbg(rdev_get_dev(info->rdev),
414 "%s-probed\n", info->desc.name);
415 }
416
417 return 0;
418}
419
420int ab8500_ext_regulator_exit(struct platform_device *pdev)
421{
422 int i;
423
424 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
425 struct ab8500_ext_regulator_info *info = NULL;
426 info = &ab8500_ext_regulator_info[i];
427
428 dev_vdbg(rdev_get_dev(info->rdev),
429 "%s-remove\n", info->desc.name);
430
431 regulator_unregister(info->rdev);
432 }
433
434 return 0;
435}
436
437MODULE_LICENSE("GPL v2");
438MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
439MODULE_DESCRIPTION("AB8500 external regulator driver");
440MODULE_ALIAS("platform:ab8500-ext-regulator");