blob: 03faf9c698c4331a7b825608ecc2fc72d99c1ebc [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);
Axel Lin66511fa2013-04-16 23:29:12 +0800184 u8 regval;
Lee Jonesd1a82002013-03-28 16:11:01 +0000185
186 if (info == NULL) {
187 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
188 return -EINVAL;
189 }
190
191 switch (mode) {
192 case REGULATOR_MODE_NORMAL:
Axel Lin66511fa2013-04-16 23:29:12 +0800193 regval = info->update_val_hp;
Lee Jonesd1a82002013-03-28 16:11:01 +0000194 break;
195 case REGULATOR_MODE_IDLE:
Axel Lin66511fa2013-04-16 23:29:12 +0800196 regval = info->update_val_lp;
Lee Jonesd1a82002013-03-28 16:11:01 +0000197 break;
198
199 default:
200 return -EINVAL;
201 }
202
Axel Lin66511fa2013-04-16 23:29:12 +0800203 /* If regulator is enabled and info->cfg->hwreq is set, the regulator
204 must be on in high power, so we don't need to write the register with
205 the same value.
206 */
207 if (ab8500_ext_regulator_is_enabled(rdev) &&
208 !(info->cfg && info->cfg->hwreq)) {
209 ret = abx500_mask_and_set_register_interruptible(info->dev,
210 info->update_bank, info->update_reg,
211 info->update_mask, regval);
212 if (ret < 0) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000213 dev_err(rdev_get_dev(rdev),
214 "Could not set regulator mode.\n");
Axel Lin66511fa2013-04-16 23:29:12 +0800215 return ret;
216 }
Lee Jonesd1a82002013-03-28 16:11:01 +0000217
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
Axel Lin66511fa2013-04-16 23:29:12 +0800225 info->update_val = regval;
226
227 return 0;
Lee Jonesd1a82002013-03-28 16:11:01 +0000228}
229
230static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
231{
232 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
233 int ret;
234
235 if (info == NULL) {
236 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
237 return -EINVAL;
238 }
239
240 if (info->update_val == info->update_val_hp)
241 ret = REGULATOR_MODE_NORMAL;
242 else if (info->update_val == info->update_val_lp)
243 ret = REGULATOR_MODE_IDLE;
244 else
245 ret = -EINVAL;
246
247 return ret;
248}
249
Lee Jonesd1a82002013-03-28 16:11:01 +0000250static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
251 unsigned selector)
252{
253 struct regulation_constraints *regu_constraints = rdev->constraints;
254
255 if (regu_constraints == NULL) {
256 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
257 return -EINVAL;
258 }
259 /* return the uV for the fixed regulators */
260 if (regu_constraints->min_uV && regu_constraints->max_uV) {
261 if (regu_constraints->min_uV == regu_constraints->max_uV)
262 return regu_constraints->min_uV;
263 }
264 return -EINVAL;
265}
266
267static struct regulator_ops ab8500_ext_regulator_ops = {
268 .enable = ab8500_ext_regulator_enable,
269 .disable = ab8500_ext_regulator_disable,
270 .is_enabled = ab8500_ext_regulator_is_enabled,
271 .set_mode = ab8500_ext_regulator_set_mode,
272 .get_mode = ab8500_ext_regulator_get_mode,
Lee Jonesd1a82002013-03-28 16:11:01 +0000273 .list_voltage = ab8500_ext_list_voltage,
274};
275
Lee Jonesd1a82002013-03-28 16:11:01 +0000276static struct ab8500_ext_regulator_info
277 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
278 [AB8500_EXT_SUPPLY1] = {
279 .desc = {
280 .name = "VEXTSUPPLY1",
281 .ops = &ab8500_ext_regulator_ops,
282 .type = REGULATOR_VOLTAGE,
283 .id = AB8500_EXT_SUPPLY1,
284 .owner = THIS_MODULE,
285 .n_voltages = 1,
286 },
287 .update_bank = 0x04,
288 .update_reg = 0x08,
289 .update_mask = 0x03,
290 .update_val = 0x01,
291 .update_val_hp = 0x01,
292 .update_val_lp = 0x03,
293 .update_val_hw = 0x02,
294 },
295 [AB8500_EXT_SUPPLY2] = {
296 .desc = {
297 .name = "VEXTSUPPLY2",
298 .ops = &ab8500_ext_regulator_ops,
299 .type = REGULATOR_VOLTAGE,
300 .id = AB8500_EXT_SUPPLY2,
301 .owner = THIS_MODULE,
302 .n_voltages = 1,
303 },
304 .update_bank = 0x04,
305 .update_reg = 0x08,
306 .update_mask = 0x0c,
307 .update_val = 0x04,
308 .update_val_hp = 0x04,
309 .update_val_lp = 0x0c,
310 .update_val_hw = 0x08,
311 },
312 [AB8500_EXT_SUPPLY3] = {
313 .desc = {
314 .name = "VEXTSUPPLY3",
315 .ops = &ab8500_ext_regulator_ops,
316 .type = REGULATOR_VOLTAGE,
317 .id = AB8500_EXT_SUPPLY3,
318 .owner = THIS_MODULE,
319 .n_voltages = 1,
320 },
321 .update_bank = 0x04,
322 .update_reg = 0x08,
323 .update_mask = 0x30,
324 .update_val = 0x10,
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000325 .update_val_hp = 0x10,
326 .update_val_lp = 0x30,
327 .update_val_hw = 0x20,
Lee Jonesd1a82002013-03-28 16:11:01 +0000328 },
329};
330
331int ab8500_ext_regulator_init(struct platform_device *pdev)
332{
333 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
334 struct ab8500_platform_data *ppdata;
335 struct ab8500_regulator_platform_data *pdata;
336 struct regulator_config config = { };
337 int i, err;
338
339 if (!ab8500) {
340 dev_err(&pdev->dev, "null mfd parent\n");
341 return -EINVAL;
342 }
343 ppdata = dev_get_platdata(ab8500->dev);
344 if (!ppdata) {
345 dev_err(&pdev->dev, "null parent pdata\n");
346 return -EINVAL;
347 }
348
349 pdata = ppdata->regulator;
350 if (!pdata) {
351 dev_err(&pdev->dev, "null pdata\n");
352 return -EINVAL;
353 }
354
355 /* make sure the platform data has the correct size */
356 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
357 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
358 return -EINVAL;
359 }
360
361 /* check for AB8500 2.x */
Bengt Jonssona6324702013-03-28 16:11:13 +0000362 if (is_ab8500_2p0_or_earlier(ab8500)) {
Lee Jonesd1a82002013-03-28 16:11:01 +0000363 struct ab8500_ext_regulator_info *info;
364
365 /* VextSupply3LPn is inverted on AB8500 2.x */
366 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
367 info->update_val = 0x30;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000368 info->update_val_hp = 0x30;
369 info->update_val_lp = 0x10;
Lee Jonesd1a82002013-03-28 16:11:01 +0000370 }
371
372 /* register all regulators */
373 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
374 struct ab8500_ext_regulator_info *info = NULL;
375
376 /* assign per-regulator data */
377 info = &ab8500_ext_regulator_info[i];
378 info->dev = &pdev->dev;
Bengt Jonsson18bc2b32013-03-28 16:11:06 +0000379 info->cfg = (struct ab8500_ext_regulator_cfg *)
380 pdata->ext_regulator[i].driver_data;
Lee Jonesd1a82002013-03-28 16:11:01 +0000381
382 config.dev = &pdev->dev;
383 config.init_data = &pdata->ext_regulator[i];
384 config.driver_data = info;
385
Lee Jonesd1a82002013-03-28 16:11:01 +0000386 /* register regulator with framework */
387 info->rdev = regulator_register(&info->desc, &config);
Lee Jonesd1a82002013-03-28 16:11:01 +0000388 if (IS_ERR(info->rdev)) {
389 err = PTR_ERR(info->rdev);
390 dev_err(&pdev->dev, "failed to register regulator %s\n",
391 info->desc.name);
392 /* when we fail, un-register all earlier regulators */
393 while (--i >= 0) {
394 info = &ab8500_ext_regulator_info[i];
395 regulator_unregister(info->rdev);
396 }
397 return err;
398 }
399
400 dev_dbg(rdev_get_dev(info->rdev),
401 "%s-probed\n", info->desc.name);
402 }
403
404 return 0;
405}
406
Axel Lin3480c0c2013-04-11 12:04:18 +0800407void ab8500_ext_regulator_exit(struct platform_device *pdev)
Lee Jonesd1a82002013-03-28 16:11:01 +0000408{
409 int i;
410
411 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
412 struct ab8500_ext_regulator_info *info = NULL;
413 info = &ab8500_ext_regulator_info[i];
414
415 dev_vdbg(rdev_get_dev(info->rdev),
416 "%s-remove\n", info->desc.name);
417
418 regulator_unregister(info->rdev);
419 }
Lee Jonesd1a82002013-03-28 16:11:01 +0000420}
421
422MODULE_LICENSE("GPL v2");
423MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
424MODULE_DESCRIPTION("AB8500 external regulator driver");
425MODULE_ALIAS("platform:ab8500-ext-regulator");