blob: 6fd0a056b35132d800bdd067998ea19fdc2b8ae9 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/err.h>
14#include <linux/string.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/mfd/pmic8901.h>
18#include <linux/regulator/driver.h>
Anirudh Ghayal9f77e962011-12-06 12:38:21 +053019#include <linux/mfd/pm8xxx/core.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070020#include <linux/regulator/pmic8901-regulator.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021
22/* Regulator types */
23#define REGULATOR_TYPE_LDO 0
24#define REGULATOR_TYPE_SMPS 1
25#define REGULATOR_TYPE_VS 2
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27/* Bank select/write macros */
28#define REGULATOR_BANK_SEL(n) ((n) << 4)
29#define REGULATOR_BANK_WRITE 0x80
30#define LDO_TEST_BANKS 7
31#define REGULATOR_BANK_MASK 0xF0
32
33/* Pin mask resource register programming */
34#define VREG_PMR_STATE_MASK 0x60
35#define VREG_PMR_STATE_HPM 0x60
36#define VREG_PMR_STATE_LPM 0x40
37#define VREG_PMR_STATE_OFF 0x20
38#define VREG_PMR_STATE_PIN_CTRL 0x20
39
40#define VREG_PMR_MODE_ACTION_MASK 0x10
41#define VREG_PMR_MODE_ACTION_SLEEP 0x10
42#define VREG_PMR_MODE_ACTION_OFF 0x00
43
44#define VREG_PMR_MODE_PIN_MASK 0x08
45#define VREG_PMR_MODE_PIN_MASKED 0x08
46
47#define VREG_PMR_CTRL_PIN2_MASK 0x04
48#define VREG_PMR_CTRL_PIN2_MASKED 0x04
49
50#define VREG_PMR_CTRL_PIN1_MASK 0x02
51#define VREG_PMR_CTRL_PIN1_MASKED 0x02
52
53#define VREG_PMR_CTRL_PIN0_MASK 0x01
54#define VREG_PMR_CTRL_PIN0_MASKED 0x01
55
56#define VREG_PMR_PIN_CTRL_ALL_MASK 0x1F
57#define VREG_PMR_PIN_CTRL_ALL_MASKED 0x1F
58
59#define REGULATOR_IS_EN(pmr_reg) \
60 ((pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_HPM || \
61 (pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_LPM)
62
63/* FTSMPS programming */
64
65/* CTRL register */
66#define SMPS_VCTRL_BAND_MASK 0xC0
67#define SMPS_VCTRL_BAND_OFF 0x00
68#define SMPS_VCTRL_BAND_1 0x40
69#define SMPS_VCTRL_BAND_2 0x80
70#define SMPS_VCTRL_BAND_3 0xC0
71#define SMPS_VCTRL_VPROG_MASK 0x3F
72
73#define SMPS_BAND_1_UV_MIN 350000
74#define SMPS_BAND_1_UV_MAX 650000
75#define SMPS_BAND_1_UV_STEP 6250
76
77#define SMPS_BAND_2_UV_MIN 700000
78#define SMPS_BAND_2_UV_MAX 1400000
79#define SMPS_BAND_2_UV_STEP 12500
80
81#define SMPS_BAND_3_UV_SETPOINT_MIN 1500000
82#define SMPS_BAND_3_UV_MIN 1400000
83#define SMPS_BAND_3_UV_MAX 3300000
84#define SMPS_BAND_3_UV_STEP 50000
85
86#define SMPS_UV_MIN SMPS_BAND_1_UV_MIN
87#define SMPS_UV_MAX SMPS_BAND_3_UV_MAX
88
89/* PWR_CNFG register */
90#define SMPS_PULL_DOWN_ENABLE_MASK 0x40
91#define SMPS_PULL_DOWN_ENABLE 0x40
92
93/* LDO programming */
94
95/* CTRL register */
96#define LDO_LOCAL_ENABLE_MASK 0x80
97#define LDO_LOCAL_ENABLE 0x80
98
99#define LDO_PULL_DOWN_ENABLE_MASK 0x40
100#define LDO_PULL_DOWN_ENABLE 0x40
101
102#define LDO_CTRL_VPROG_MASK 0x1F
103
104/* TEST register bank 2 */
105#define LDO_TEST_VPROG_UPDATE_MASK 0x08
106#define LDO_TEST_RANGE_SEL_MASK 0x04
107#define LDO_TEST_FINE_STEP_MASK 0x02
108#define LDO_TEST_FINE_STEP_SHIFT 1
109
110/* TEST register bank 4 */
111#define LDO_TEST_RANGE_EXT_MASK 0x01
112
113/* Allowable voltage ranges */
114#define PLDO_LOW_UV_MIN 750000
115#define PLDO_LOW_UV_MAX 1537500
116#define PLDO_LOW_FINE_STEP_UV 12500
117
118#define PLDO_NORM_UV_MIN 1500000
119#define PLDO_NORM_UV_MAX 3075000
120#define PLDO_NORM_FINE_STEP_UV 25000
121
122#define PLDO_HIGH_UV_MIN 1750000
123#define PLDO_HIGH_UV_MAX 4900000
124#define PLDO_HIGH_FINE_STEP_UV 50000
125
126#define NLDO_UV_MIN 750000
127#define NLDO_UV_MAX 1537500
128#define NLDO_FINE_STEP_UV 12500
129
130/* VS programming */
131
132/* CTRL register */
133#define VS_CTRL_ENABLE_MASK 0xC0
134#define VS_CTRL_DISABLE 0x00
135#define VS_CTRL_ENABLE 0x40
136#define VS_CTRL_USE_PMR 0xC0
137
138#define VS_PULL_DOWN_ENABLE_MASK 0x20
139#define VS_PULL_DOWN_ENABLE 0x20
140
141struct pm8901_vreg {
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530142 struct device *dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 struct pm8901_vreg_pdata *pdata;
144 struct regulator_dev *rdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 int hpm_min_load;
146 unsigned pc_vote;
147 unsigned optimum;
148 unsigned mode_initialized;
149 u16 ctrl_addr;
150 u16 pmr_addr;
151 u16 test_addr;
152 u16 pfm_ctrl_addr;
153 u16 pwr_cnfg_addr;
154 u8 type;
155 u8 ctrl_reg;
156 u8 pmr_reg;
157 u8 test_reg[LDO_TEST_BANKS];
158 u8 pfm_ctrl_reg;
159 u8 pwr_cnfg_reg;
160 u8 is_nmos;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 u8 state;
162};
163
164/*
165 * These are used to compensate for the PMIC 8901 v1 FTS regulators which
166 * output ~10% higher than the programmed set point.
167 */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530168#define IS_PMIC_8901_V1(rev) ((rev) == PM8XXX_REVISION_8901_1p0 || \
169 (rev) == PM8XXX_REVISION_8901_1p1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170
171#define PMIC_8901_V1_SCALE(uV) ((((uV) - 62100) * 23) / 25)
172
173#define PMIC_8901_V1_SCALE_INV(uV) (((uV) * 25) / 23 + 62100)
174
175/*
176 * Band 1 of PMIC 8901 SMPS regulators only supports set points with the 3 LSB's
177 * equal to 0. This is accomplished in the macro by truncating the bits.
178 */
179#define PM8901_SMPS_BAND_1_COMPENSATE(vprog) ((vprog) & 0xF8)
180
181#define LDO(_id, _ctrl_addr, _pmr_addr, _test_addr, _is_nmos) \
182 [_id] = { \
183 .ctrl_addr = _ctrl_addr, \
184 .pmr_addr = _pmr_addr, \
185 .test_addr = _test_addr, \
186 .type = REGULATOR_TYPE_LDO, \
187 .is_nmos = _is_nmos, \
188 .hpm_min_load = PM8901_VREG_LDO_300_HPM_MIN_LOAD, \
189 }
190
191#define SMPS(_id, _ctrl_addr, _pmr_addr, _pfm_ctrl_addr, _pwr_cnfg_addr) \
192 [_id] = { \
193 .ctrl_addr = _ctrl_addr, \
194 .pmr_addr = _pmr_addr, \
195 .pfm_ctrl_addr = _pfm_ctrl_addr, \
196 .pwr_cnfg_addr = _pwr_cnfg_addr, \
197 .type = REGULATOR_TYPE_SMPS, \
198 .hpm_min_load = PM8901_VREG_FTSMPS_HPM_MIN_LOAD, \
199 }
200
201#define VS(_id, _ctrl_addr, _pmr_addr) \
202 [_id] = { \
203 .ctrl_addr = _ctrl_addr, \
204 .pmr_addr = _pmr_addr, \
205 .type = REGULATOR_TYPE_VS, \
206 }
207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208static struct pm8901_vreg pm8901_vreg[] = {
209 /* id ctrl pmr tst n/p */
210 LDO(PM8901_VREG_ID_L0, 0x02F, 0x0AB, 0x030, 1),
211 LDO(PM8901_VREG_ID_L1, 0x031, 0x0AC, 0x032, 0),
212 LDO(PM8901_VREG_ID_L2, 0x033, 0x0AD, 0x034, 0),
213 LDO(PM8901_VREG_ID_L3, 0x035, 0x0AE, 0x036, 0),
214 LDO(PM8901_VREG_ID_L4, 0x037, 0x0AF, 0x038, 0),
215 LDO(PM8901_VREG_ID_L5, 0x039, 0x0B0, 0x03A, 0),
216 LDO(PM8901_VREG_ID_L6, 0x03B, 0x0B1, 0x03C, 0),
217
218 /* id ctrl pmr pfm pwr */
219 SMPS(PM8901_VREG_ID_S0, 0x05B, 0x0A6, 0x05C, 0x0E3),
220 SMPS(PM8901_VREG_ID_S1, 0x06A, 0x0A7, 0x06B, 0x0EC),
221 SMPS(PM8901_VREG_ID_S2, 0x079, 0x0A8, 0x07A, 0x0F1),
222 SMPS(PM8901_VREG_ID_S3, 0x088, 0x0A9, 0x089, 0x0F6),
223 SMPS(PM8901_VREG_ID_S4, 0x097, 0x0AA, 0x098, 0x0FB),
224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 /* id ctrl pmr */
226 VS(PM8901_VREG_ID_LVS0, 0x046, 0x0B2),
227 VS(PM8901_VREG_ID_LVS1, 0x048, 0x0B3),
228 VS(PM8901_VREG_ID_LVS2, 0x04A, 0x0B4),
229 VS(PM8901_VREG_ID_LVS3, 0x04C, 0x0B5),
230 VS(PM8901_VREG_ID_MVS0, 0x052, 0x0B6),
231 VS(PM8901_VREG_ID_USB_OTG, 0x055, 0x0B7),
232 VS(PM8901_VREG_ID_HDMI_MVS, 0x058, 0x0B8),
233};
234
235static void print_write_error(struct pm8901_vreg *vreg, int rc,
236 const char *func);
237
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530238static int pm8901_vreg_write(struct pm8901_vreg *vreg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 u16 addr, u8 val, u8 mask, u8 *reg_save)
240{
241 int rc = 0;
242 u8 reg;
243
244 reg = (*reg_save & ~mask) | (val & mask);
245 if (reg != *reg_save)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530246 rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 if (!rc)
248 *reg_save = reg;
249 return rc;
250}
251
252/* Set pin control bits based on new mode. */
253static int pm8901_vreg_select_pin_ctrl(struct pm8901_vreg *vreg, u8 *pmr_reg)
254{
255 *pmr_reg |= VREG_PMR_PIN_CTRL_ALL_MASKED;
256
257 if ((*pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_PIN_CTRL) {
258 if (vreg->pdata->pin_fn == PM8901_VREG_PIN_FN_MODE)
259 *pmr_reg = (*pmr_reg & ~VREG_PMR_STATE_MASK)
260 | VREG_PMR_STATE_LPM;
261 if (vreg->pdata->pin_ctrl & PM8901_VREG_PIN_CTRL_A0)
262 *pmr_reg &= ~VREG_PMR_CTRL_PIN0_MASKED;
263 if (vreg->pdata->pin_ctrl & PM8901_VREG_PIN_CTRL_A1)
264 *pmr_reg &= ~VREG_PMR_CTRL_PIN1_MASKED;
265 if (vreg->pdata->pin_ctrl & PM8901_VREG_PIN_CTRL_D0)
266 *pmr_reg &= ~VREG_PMR_CTRL_PIN2_MASKED;
267 }
268
269 return 0;
270}
271
272static int pm8901_vreg_enable(struct regulator_dev *dev)
273{
274 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 u8 val = VREG_PMR_STATE_HPM;
276 int rc;
277
278 if (!vreg->mode_initialized && vreg->pc_vote)
279 val = VREG_PMR_STATE_PIN_CTRL;
280 else if (vreg->optimum == REGULATOR_MODE_FAST)
281 val = VREG_PMR_STATE_HPM;
282 else if (vreg->pc_vote)
283 val = VREG_PMR_STATE_PIN_CTRL;
284 else if (vreg->optimum == REGULATOR_MODE_STANDBY)
285 val = VREG_PMR_STATE_LPM;
286
287 pm8901_vreg_select_pin_ctrl(vreg, &val);
288
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530289 rc = pm8901_vreg_write(vreg, vreg->pmr_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 val,
291 VREG_PMR_STATE_MASK | VREG_PMR_PIN_CTRL_ALL_MASK,
292 &vreg->pmr_reg);
293 if (rc)
294 print_write_error(vreg, rc, __func__);
295
296 return rc;
297}
298
299static int pm8901_vreg_disable(struct regulator_dev *dev)
300{
301 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 int rc;
303
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530304 rc = pm8901_vreg_write(vreg, vreg->pmr_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 VREG_PMR_STATE_OFF | VREG_PMR_PIN_CTRL_ALL_MASKED,
306 VREG_PMR_STATE_MASK | VREG_PMR_PIN_CTRL_ALL_MASK,
307 &vreg->pmr_reg);
308 if (rc)
309 print_write_error(vreg, rc, __func__);
310
311 return rc;
312}
313
314/*
315 * Cases that count as enabled:
316 *
317 * 1. PMR register has mode == HPM or LPM.
318 * 2. Any pin control bits are unmasked.
319 * 3. The regulator is an LDO and its local enable bit is set.
320 */
321static int _pm8901_vreg_is_enabled(struct pm8901_vreg *vreg)
322{
323 if ((vreg->type == REGULATOR_TYPE_LDO)
324 && (vreg->ctrl_reg & LDO_LOCAL_ENABLE_MASK))
325 return 1;
326 else if (vreg->type == REGULATOR_TYPE_VS) {
327 if ((vreg->ctrl_reg & VS_CTRL_ENABLE_MASK) == VS_CTRL_ENABLE)
328 return 1;
329 else if ((vreg->ctrl_reg & VS_CTRL_ENABLE_MASK)
330 == VS_CTRL_DISABLE)
331 return 0;
332 }
333
334 return REGULATOR_IS_EN(vreg->pmr_reg)
335 || ((vreg->pmr_reg & VREG_PMR_PIN_CTRL_ALL_MASK)
336 != VREG_PMR_PIN_CTRL_ALL_MASKED);
337}
338
339static int pm8901_vreg_is_enabled(struct regulator_dev *dev)
340{
341 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
342
343 return _pm8901_vreg_is_enabled(vreg);
344}
345
346static int pm8901_ldo_disable(struct regulator_dev *dev)
347{
348 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 int rc;
350
351 /* Disassert local enable bit in CTRL register. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530352 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, 0, LDO_LOCAL_ENABLE_MASK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 &vreg->ctrl_reg);
354 if (rc)
355 print_write_error(vreg, rc, __func__);
356
357 /* Disassert enable bit in PMR register. */
358 rc = pm8901_vreg_disable(dev);
359
360 return rc;
361}
362
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530363static int pm8901_pldo_set_voltage(struct pm8901_vreg *vreg, int uV)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364{
365 int vmin, rc = 0;
366 unsigned vprog, fine_step;
367 u8 range_ext, range_sel, fine_step_reg;
368
369 if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX)
370 return -EINVAL;
371
372 if (uV < PLDO_LOW_UV_MAX + PLDO_LOW_FINE_STEP_UV) {
373 vmin = PLDO_LOW_UV_MIN;
374 fine_step = PLDO_LOW_FINE_STEP_UV;
375 range_ext = 0;
376 range_sel = LDO_TEST_RANGE_SEL_MASK;
377 } else if (uV < PLDO_NORM_UV_MAX + PLDO_NORM_FINE_STEP_UV) {
378 vmin = PLDO_NORM_UV_MIN;
379 fine_step = PLDO_NORM_FINE_STEP_UV;
380 range_ext = 0;
381 range_sel = 0;
382 } else {
383 vmin = PLDO_HIGH_UV_MIN;
384 fine_step = PLDO_HIGH_FINE_STEP_UV;
385 range_ext = LDO_TEST_RANGE_EXT_MASK;
386 range_sel = 0;
387 }
388
389 vprog = (uV - vmin) / fine_step;
390 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
391 vprog >>= 1;
392
393 /*
394 * Disable program voltage update if range extension, range select,
395 * or fine step have changed and the regulator is enabled.
396 */
397 if (_pm8901_vreg_is_enabled(vreg) &&
398 (((range_ext ^ vreg->test_reg[4]) & LDO_TEST_RANGE_EXT_MASK)
399 || ((range_sel ^ vreg->test_reg[2]) & LDO_TEST_RANGE_SEL_MASK)
400 || ((fine_step_reg ^ vreg->test_reg[2])
401 & LDO_TEST_FINE_STEP_MASK))) {
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530402 rc = pm8901_vreg_write(vreg, vreg->test_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 REGULATOR_BANK_SEL(2) | REGULATOR_BANK_WRITE,
404 REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
405 &vreg->test_reg[2]);
406 if (rc)
407 goto bail;
408 }
409
410 /* Write new voltage. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530411 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, vprog,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
413 if (rc)
414 goto bail;
415
416 /* Write range extension. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530417 rc = pm8901_vreg_write(vreg, vreg->test_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 range_ext | REGULATOR_BANK_SEL(4)
419 | REGULATOR_BANK_WRITE,
420 LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
421 &vreg->test_reg[4]);
422 if (rc)
423 goto bail;
424
425 /* Write fine step, range select and program voltage update. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530426 rc = pm8901_vreg_write(vreg, vreg->test_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
428 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
429 LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
430 | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
431 &vreg->test_reg[2]);
432bail:
433 if (rc)
434 print_write_error(vreg, rc, __func__);
435
436 return rc;
437}
438
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530439static int pm8901_nldo_set_voltage(struct pm8901_vreg *vreg, int uV)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440{
441 unsigned vprog, fine_step_reg;
442 int rc;
443
444 if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX)
445 return -EINVAL;
446
447 vprog = (uV - NLDO_UV_MIN) / NLDO_FINE_STEP_UV;
448 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
449 vprog >>= 1;
450
451 /* Write new voltage. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530452 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, vprog,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
454 if (rc)
455 print_write_error(vreg, rc, __func__);
456
457 /* Write fine step. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530458 rc = pm8901_vreg_write(vreg, vreg->test_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 fine_step_reg | REGULATOR_BANK_SEL(2)
460 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
461 LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
462 | LDO_TEST_VPROG_UPDATE_MASK,
463 &vreg->test_reg[2]);
464 if (rc)
465 print_write_error(vreg, rc, __func__);
466
467 return rc;
468}
469
470static int pm8901_ldo_set_voltage(struct regulator_dev *dev,
471 int min_uV, int max_uV, unsigned *selector)
472{
473 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474
475 if (vreg->is_nmos)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530476 return pm8901_nldo_set_voltage(vreg, min_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 else
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530478 return pm8901_pldo_set_voltage(vreg, min_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479}
480
481static int pm8901_pldo_get_voltage(struct pm8901_vreg *vreg)
482{
483 int vmin, fine_step;
484 u8 range_ext, range_sel, vprog, fine_step_reg;
485
486 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
487 range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
488 range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
489 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
490
491 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
492
493 if (range_sel) {
494 /* low range mode */
495 fine_step = PLDO_LOW_FINE_STEP_UV;
496 vmin = PLDO_LOW_UV_MIN;
497 } else if (!range_ext) {
498 /* normal mode */
499 fine_step = PLDO_NORM_FINE_STEP_UV;
500 vmin = PLDO_NORM_UV_MIN;
501 } else {
502 /* high range mode */
503 fine_step = PLDO_HIGH_FINE_STEP_UV;
504 vmin = PLDO_HIGH_UV_MIN;
505 }
506
507 return fine_step * vprog + vmin;
508}
509
510static int pm8901_nldo_get_voltage(struct pm8901_vreg *vreg)
511{
512 u8 vprog, fine_step_reg;
513
514 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
515 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
516
517 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
518
519 return NLDO_FINE_STEP_UV * vprog + NLDO_UV_MIN;
520}
521
522static int pm8901_ldo_get_voltage(struct regulator_dev *dev)
523{
524 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
525
526 if (vreg->is_nmos)
527 return pm8901_nldo_get_voltage(vreg);
528 else
529 return pm8901_pldo_get_voltage(vreg);
530}
531
532/*
533 * Optimum mode programming:
534 * REGULATOR_MODE_FAST: Go to HPM (highest priority)
535 * REGULATOR_MODE_STANDBY: Go to pin ctrl mode if there are any pin ctrl
536 * votes, else go to LPM
537 *
538 * Pin ctrl mode voting via regulator set_mode:
539 * REGULATOR_MODE_IDLE: Go to pin ctrl mode if the optimum mode is LPM, else
540 * go to HPM
541 * REGULATOR_MODE_NORMAL: Go to LPM if it is the optimum mode, else go to HPM
542 */
543static int pm8901_vreg_set_mode(struct regulator_dev *dev, unsigned int mode)
544{
545 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 unsigned optimum = vreg->optimum;
547 unsigned pc_vote = vreg->pc_vote;
548 unsigned mode_initialized = vreg->mode_initialized;
549 u8 val = 0;
550 int rc = 0;
551
552 /* Determine new mode to go into. */
553 switch (mode) {
554 case REGULATOR_MODE_FAST:
555 val = VREG_PMR_STATE_HPM;
556 optimum = mode;
557 mode_initialized = 1;
558 break;
559
560 case REGULATOR_MODE_STANDBY:
561 if (pc_vote)
562 val = VREG_PMR_STATE_PIN_CTRL;
563 else
564 val = VREG_PMR_STATE_LPM;
565 optimum = mode;
566 mode_initialized = 1;
567 break;
568
569 case REGULATOR_MODE_IDLE:
570 if (pc_vote++)
571 goto done; /* already taken care of */
572
573 if (mode_initialized && optimum == REGULATOR_MODE_FAST)
574 val = VREG_PMR_STATE_HPM;
575 else
576 val = VREG_PMR_STATE_PIN_CTRL;
577 break;
578
579 case REGULATOR_MODE_NORMAL:
580 if (pc_vote && --pc_vote)
581 goto done; /* already taken care of */
582
583 if (optimum == REGULATOR_MODE_STANDBY)
584 val = VREG_PMR_STATE_LPM;
585 else
586 val = VREG_PMR_STATE_HPM;
587 break;
588
589 default:
590 pr_err("%s: unknown mode, mode=%u\n", __func__, mode);
591 return -EINVAL;
592 }
593
594 /* Set pin control bits based on new mode. */
595 pm8901_vreg_select_pin_ctrl(vreg, &val);
596
597 /* Only apply mode setting to hardware if currently enabled. */
598 if (pm8901_vreg_is_enabled(dev))
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530599 rc = pm8901_vreg_write(vreg, vreg->pmr_addr, val,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 VREG_PMR_STATE_MASK | VREG_PMR_PIN_CTRL_ALL_MASK,
601 &vreg->pmr_reg);
602
603 if (rc) {
604 print_write_error(vreg, rc, __func__);
605 return rc;
606 }
607
608done:
609 vreg->mode_initialized = mode_initialized;
610 vreg->optimum = optimum;
611 vreg->pc_vote = pc_vote;
612
613 return 0;
614}
615
616static unsigned int pm8901_vreg_get_mode(struct regulator_dev *dev)
617{
618 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
619 int pin_mask = VREG_PMR_CTRL_PIN0_MASK | VREG_PMR_CTRL_PIN1_MASK
620 | VREG_PMR_CTRL_PIN2_MASK;
621
622 if (!vreg->mode_initialized && vreg->pc_vote)
623 return REGULATOR_MODE_IDLE;
624 else if (((vreg->pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_OFF)
625 && ((vreg->pmr_reg & pin_mask) != pin_mask))
626 return REGULATOR_MODE_IDLE;
627 else if (((vreg->pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_LPM)
628 && ((vreg->pmr_reg & pin_mask) != pin_mask))
629 return REGULATOR_MODE_IDLE;
630 else if (vreg->optimum == REGULATOR_MODE_FAST)
631 return REGULATOR_MODE_FAST;
632 else if (vreg->pc_vote)
633 return REGULATOR_MODE_IDLE;
634 else if (vreg->optimum == REGULATOR_MODE_STANDBY)
635 return REGULATOR_MODE_STANDBY;
636 return REGULATOR_MODE_FAST;
637}
638
639unsigned int pm8901_vreg_get_optimum_mode(struct regulator_dev *dev,
640 int input_uV, int output_uV, int load_uA)
641{
642 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
643
644 if (load_uA <= 0) {
645 /*
646 * pm8901_vreg_get_optimum_mode is being called before consumers
647 * have specified their load currents via
648 * regulator_set_optimum_mode. Return whatever the existing mode
649 * is.
650 */
651 return pm8901_vreg_get_mode(dev);
652 }
653
654 if (load_uA >= vreg->hpm_min_load)
655 return REGULATOR_MODE_FAST;
656 return REGULATOR_MODE_STANDBY;
657}
658
659static int pm8901_smps_set_voltage(struct regulator_dev *dev,
660 int min_uV, int max_uV, unsigned *selector)
661{
662 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 int rc;
664 u8 val, band;
665
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530666 if (IS_PMIC_8901_V1(pm8xxx_get_revision(vreg->dev->parent)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 min_uV = PMIC_8901_V1_SCALE(min_uV);
668
669 if (min_uV < SMPS_BAND_1_UV_MIN || min_uV > SMPS_BAND_3_UV_MAX)
670 return -EINVAL;
671
672 /* Round down for set points in the gaps between bands. */
673 if (min_uV > SMPS_BAND_1_UV_MAX && min_uV < SMPS_BAND_2_UV_MIN)
674 min_uV = SMPS_BAND_1_UV_MAX;
675 else if (min_uV > SMPS_BAND_2_UV_MAX
676 && min_uV < SMPS_BAND_3_UV_SETPOINT_MIN)
677 min_uV = SMPS_BAND_2_UV_MAX;
678
679 if (min_uV < SMPS_BAND_2_UV_MIN) {
680 val = ((min_uV - SMPS_BAND_1_UV_MIN) / SMPS_BAND_1_UV_STEP);
681 val = PM8901_SMPS_BAND_1_COMPENSATE(val);
682 band = SMPS_VCTRL_BAND_1;
683 } else if (min_uV < SMPS_BAND_3_UV_SETPOINT_MIN) {
684 val = ((min_uV - SMPS_BAND_2_UV_MIN) / SMPS_BAND_2_UV_STEP);
685 band = SMPS_VCTRL_BAND_2;
686 } else {
687 val = ((min_uV - SMPS_BAND_3_UV_MIN) / SMPS_BAND_3_UV_STEP);
688 band = SMPS_VCTRL_BAND_3;
689 }
690
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530691 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, band | val,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 SMPS_VCTRL_BAND_MASK | SMPS_VCTRL_VPROG_MASK,
693 &vreg->ctrl_reg);
694 if (rc)
695 goto bail;
696
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530697 rc = pm8901_vreg_write(vreg, vreg->pfm_ctrl_addr, band | val,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 SMPS_VCTRL_BAND_MASK | SMPS_VCTRL_VPROG_MASK,
699 &vreg->pfm_ctrl_reg);
700bail:
701 if (rc)
702 print_write_error(vreg, rc, __func__);
703
704 return rc;
705}
706
707static int pm8901_smps_get_voltage(struct regulator_dev *dev)
708{
709 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 u8 vprog, band;
711 int ret = 0;
712
713 if ((vreg->pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_LPM) {
714 vprog = vreg->pfm_ctrl_reg & SMPS_VCTRL_VPROG_MASK;
715 band = vreg->pfm_ctrl_reg & SMPS_VCTRL_BAND_MASK;
716 } else {
717 vprog = vreg->ctrl_reg & SMPS_VCTRL_VPROG_MASK;
718 band = vreg->ctrl_reg & SMPS_VCTRL_BAND_MASK;
719 }
720
721 if (band == SMPS_VCTRL_BAND_1)
722 ret = vprog * SMPS_BAND_1_UV_STEP + SMPS_BAND_1_UV_MIN;
723 else if (band == SMPS_VCTRL_BAND_2)
724 ret = vprog * SMPS_BAND_2_UV_STEP + SMPS_BAND_2_UV_MIN;
725 else
726 ret = vprog * SMPS_BAND_3_UV_STEP + SMPS_BAND_3_UV_MIN;
727
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530728 if (IS_PMIC_8901_V1(pm8xxx_get_revision(vreg->dev->parent)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 ret = PMIC_8901_V1_SCALE_INV(ret);
730
731 return ret;
732}
733
734static int pm8901_vs_enable(struct regulator_dev *dev)
735{
736 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 int rc;
738
739 /* Assert enable bit in PMR register. */
740 rc = pm8901_vreg_enable(dev);
741
742 /* Make sure that switch is controlled via PMR register */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530743 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, VS_CTRL_USE_PMR,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 VS_CTRL_ENABLE_MASK, &vreg->ctrl_reg);
745 if (rc)
746 print_write_error(vreg, rc, __func__);
747
748 return rc;
749}
750
751static int pm8901_vs_disable(struct regulator_dev *dev)
752{
753 struct pm8901_vreg *vreg = rdev_get_drvdata(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 int rc;
755
756 /* Disassert enable bit in PMR register. */
757 rc = pm8901_vreg_disable(dev);
758
759 /* Make sure that switch is controlled via PMR register */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530760 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr, VS_CTRL_USE_PMR,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 VS_CTRL_ENABLE_MASK, &vreg->ctrl_reg);
762 if (rc)
763 print_write_error(vreg, rc, __func__);
764
765 return rc;
766}
767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768static struct regulator_ops pm8901_ldo_ops = {
769 .enable = pm8901_vreg_enable,
770 .disable = pm8901_ldo_disable,
771 .is_enabled = pm8901_vreg_is_enabled,
772 .set_voltage = pm8901_ldo_set_voltage,
773 .get_voltage = pm8901_ldo_get_voltage,
774 .set_mode = pm8901_vreg_set_mode,
775 .get_mode = pm8901_vreg_get_mode,
776 .get_optimum_mode = pm8901_vreg_get_optimum_mode,
777};
778
779static struct regulator_ops pm8901_smps_ops = {
780 .enable = pm8901_vreg_enable,
781 .disable = pm8901_vreg_disable,
782 .is_enabled = pm8901_vreg_is_enabled,
783 .set_voltage = pm8901_smps_set_voltage,
784 .get_voltage = pm8901_smps_get_voltage,
785 .set_mode = pm8901_vreg_set_mode,
786 .get_mode = pm8901_vreg_get_mode,
787 .get_optimum_mode = pm8901_vreg_get_optimum_mode,
788};
789
790static struct regulator_ops pm8901_vs_ops = {
791 .enable = pm8901_vs_enable,
792 .disable = pm8901_vs_disable,
793 .is_enabled = pm8901_vreg_is_enabled,
794 .set_mode = pm8901_vreg_set_mode,
795 .get_mode = pm8901_vreg_get_mode,
796};
797
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798#define VREG_DESCRIP(_id, _name, _ops) \
799 [_id] = { \
800 .name = _name, \
801 .id = _id, \
802 .ops = _ops, \
803 .type = REGULATOR_VOLTAGE, \
804 .owner = THIS_MODULE, \
805 }
806
807static struct regulator_desc pm8901_vreg_descrip[] = {
808 VREG_DESCRIP(PM8901_VREG_ID_L0, "8901_l0", &pm8901_ldo_ops),
809 VREG_DESCRIP(PM8901_VREG_ID_L1, "8901_l1", &pm8901_ldo_ops),
810 VREG_DESCRIP(PM8901_VREG_ID_L2, "8901_l2", &pm8901_ldo_ops),
811 VREG_DESCRIP(PM8901_VREG_ID_L3, "8901_l3", &pm8901_ldo_ops),
812 VREG_DESCRIP(PM8901_VREG_ID_L4, "8901_l4", &pm8901_ldo_ops),
813 VREG_DESCRIP(PM8901_VREG_ID_L5, "8901_l5", &pm8901_ldo_ops),
814 VREG_DESCRIP(PM8901_VREG_ID_L6, "8901_l6", &pm8901_ldo_ops),
815
816 VREG_DESCRIP(PM8901_VREG_ID_S0, "8901_s0", &pm8901_smps_ops),
817 VREG_DESCRIP(PM8901_VREG_ID_S1, "8901_s1", &pm8901_smps_ops),
818 VREG_DESCRIP(PM8901_VREG_ID_S2, "8901_s2", &pm8901_smps_ops),
819 VREG_DESCRIP(PM8901_VREG_ID_S3, "8901_s3", &pm8901_smps_ops),
820 VREG_DESCRIP(PM8901_VREG_ID_S4, "8901_s4", &pm8901_smps_ops),
821
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 VREG_DESCRIP(PM8901_VREG_ID_LVS0, "8901_lvs0", &pm8901_vs_ops),
823 VREG_DESCRIP(PM8901_VREG_ID_LVS1, "8901_lvs1", &pm8901_vs_ops),
824 VREG_DESCRIP(PM8901_VREG_ID_LVS2, "8901_lvs2", &pm8901_vs_ops),
825 VREG_DESCRIP(PM8901_VREG_ID_LVS3, "8901_lvs3", &pm8901_vs_ops),
826 VREG_DESCRIP(PM8901_VREG_ID_MVS0, "8901_mvs0", &pm8901_vs_ops),
827 VREG_DESCRIP(PM8901_VREG_ID_USB_OTG, "8901_usb_otg", &pm8901_vs_ops),
828 VREG_DESCRIP(PM8901_VREG_ID_HDMI_MVS, "8901_hdmi_mvs", &pm8901_vs_ops),
829};
830
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530831static int pm8901_init_ldo(struct pm8901_vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832{
833 int rc = 0, i;
834 u8 bank;
835
836 /* Store current regulator register values. */
837 for (i = 0; i < LDO_TEST_BANKS; i++) {
838 bank = REGULATOR_BANK_SEL(i);
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530839 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 if (rc)
841 goto bail;
842
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530843 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
844 &vreg->test_reg[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 if (rc)
846 goto bail;
847
848 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
849 }
850
851 /* Set pull down enable based on platform data. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530852 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 (vreg->pdata->pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
854 LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
855bail:
856 return rc;
857}
858
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530859static int pm8901_init_smps(struct pm8901_vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860{
861 int rc;
862
863 /* Store current regulator register values. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530864 rc = pm8xxx_readb(vreg->dev->parent, vreg->pfm_ctrl_addr,
865 &vreg->pfm_ctrl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 if (rc)
867 goto bail;
868
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530869 rc = pm8xxx_readb(vreg->dev->parent, vreg->pwr_cnfg_addr,
870 &vreg->pwr_cnfg_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 if (rc)
872 goto bail;
873
874 /* Set pull down enable based on platform data. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530875 rc = pm8901_vreg_write(vreg, vreg->pwr_cnfg_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 (vreg->pdata->pull_down_enable ? SMPS_PULL_DOWN_ENABLE : 0),
877 SMPS_PULL_DOWN_ENABLE_MASK, &vreg->pwr_cnfg_reg);
878
879bail:
880 return rc;
881}
882
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530883static int pm8901_init_vs(struct pm8901_vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884{
885 int rc = 0;
886
887 /* Set pull down enable based on platform data. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530888 rc = pm8901_vreg_write(vreg, vreg->ctrl_addr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 (vreg->pdata->pull_down_enable ? VS_PULL_DOWN_ENABLE : 0),
890 VS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
891
892 return rc;
893}
894
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530895static int pm8901_init_regulator(struct pm8901_vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896{
897 int rc;
898
899 /* Store current regulator register values. */
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530900 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
901 if (rc)
902 goto bail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530904 rc = pm8xxx_readb(vreg->dev->parent, vreg->pmr_addr, &vreg->pmr_reg);
905 if (rc)
906 goto bail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907
908 /* Set initial mode based on hardware state. */
909 if ((vreg->pmr_reg & VREG_PMR_STATE_MASK) == VREG_PMR_STATE_LPM)
910 vreg->optimum = REGULATOR_MODE_STANDBY;
911 else
912 vreg->optimum = REGULATOR_MODE_FAST;
913
914 vreg->mode_initialized = 0;
915
916 if (vreg->type == REGULATOR_TYPE_LDO)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530917 rc = pm8901_init_ldo(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 else if (vreg->type == REGULATOR_TYPE_SMPS)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530919 rc = pm8901_init_smps(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 else if (vreg->type == REGULATOR_TYPE_VS)
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530921 rc = pm8901_init_vs(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922bail:
923 if (rc)
924 pr_err("%s: pm8901_read/write failed; initial register states "
925 "unknown, rc=%d\n", __func__, rc);
926
927 return rc;
928}
929
930static int __devinit pm8901_vreg_probe(struct platform_device *pdev)
931{
932 struct regulator_desc *rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 struct pm8901_vreg *vreg;
934 const char *reg_name = NULL;
935 int rc = 0;
936
937 if (pdev == NULL)
938 return -EINVAL;
939
940 if (pdev->id >= 0 && pdev->id < PM8901_VREG_MAX) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 rdesc = &pm8901_vreg_descrip[pdev->id];
942 vreg = &pm8901_vreg[pdev->id];
943 vreg->pdata = pdev->dev.platform_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 reg_name = pm8901_vreg_descrip[pdev->id].name;
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530945 vreg->dev = &pdev->dev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946
Anirudh Ghayal9f77e962011-12-06 12:38:21 +0530947 rc = pm8901_init_regulator(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 if (rc)
949 goto bail;
950
951 /* Disallow idle and normal modes if pin control isn't set. */
952 if (vreg->pdata->pin_ctrl == 0)
953 vreg->pdata->init_data.constraints.valid_modes_mask
954 &= ~(REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE);
955
956 vreg->rdev = regulator_register(rdesc, &pdev->dev,
957 &vreg->pdata->init_data, vreg);
958 if (IS_ERR(vreg->rdev)) {
959 rc = PTR_ERR(vreg->rdev);
960 pr_err("%s: regulator_register failed for %s, rc=%d\n",
961 __func__, reg_name, rc);
962 }
963 } else {
964 rc = -ENODEV;
965 }
966
967bail:
968 if (rc)
969 pr_err("%s: error for %s, rc=%d\n", __func__, reg_name, rc);
970
971 return rc;
972}
973
974static int __devexit pm8901_vreg_remove(struct platform_device *pdev)
975{
976 regulator_unregister(pm8901_vreg[pdev->id].rdev);
977 return 0;
978}
979
980static struct platform_driver pm8901_vreg_driver = {
981 .probe = pm8901_vreg_probe,
982 .remove = __devexit_p(pm8901_vreg_remove),
983 .driver = {
984 .name = "pm8901-regulator",
985 .owner = THIS_MODULE,
986 },
987};
988
989static int __init pm8901_vreg_init(void)
990{
991 return platform_driver_register(&pm8901_vreg_driver);
992}
993
994static void __exit pm8901_vreg_exit(void)
995{
996 platform_driver_unregister(&pm8901_vreg_driver);
997}
998
999static void print_write_error(struct pm8901_vreg *vreg, int rc,
1000 const char *func)
1001{
1002 const char *reg_name = NULL;
1003 ptrdiff_t id = vreg - pm8901_vreg;
1004
1005 if (id >= 0 && id < PM8901_VREG_MAX)
1006 reg_name = pm8901_vreg_descrip[id].name;
1007 pr_err("%s: pm8901_vreg_write failed for %s, rc=%d\n",
1008 func, reg_name, rc);
1009}
1010
1011subsys_initcall(pm8901_vreg_init);
1012module_exit(pm8901_vreg_exit);
1013
1014MODULE_LICENSE("GPL v2");
1015MODULE_DESCRIPTION("PMIC8901 regulator driver");
1016MODULE_VERSION("1.0");
1017MODULE_ALIAS("platform:pm8901-regulator");