blob: 98ba1630dd4e0247ddfef17fbd96254cc9dd6c30 [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/bitops.h>
18#include <linux/mfd/pmic8058.h>
19#include <linux/regulator/driver.h>
20#include <linux/regulator/machine.h>
21#include <linux/regulator/pmic8058-regulator.h>
22
23/* Regulator types */
24#define REGULATOR_TYPE_LDO 0
25#define REGULATOR_TYPE_SMPS 1
26#define REGULATOR_TYPE_LVS 2
27#define REGULATOR_TYPE_NCP 3
28
29/* Common masks */
30#define REGULATOR_EN_MASK 0x80
31
32#define REGULATOR_BANK_MASK 0xF0
33#define REGULATOR_BANK_SEL(n) ((n) << 4)
34#define REGULATOR_BANK_WRITE 0x80
35
36#define LDO_TEST_BANKS 7
37#define SMPS_TEST_BANKS 8
38#define REGULATOR_TEST_BANKS_MAX SMPS_TEST_BANKS
39
40/* LDO programming */
41
42/* CTRL register */
43#define LDO_ENABLE_MASK 0x80
44#define LDO_ENABLE 0x80
45#define LDO_PULL_DOWN_ENABLE_MASK 0x40
46#define LDO_PULL_DOWN_ENABLE 0x40
47
48#define LDO_CTRL_PM_MASK 0x20
49#define LDO_CTRL_PM_HPM 0x00
50#define LDO_CTRL_PM_LPM 0x20
51
52#define LDO_CTRL_VPROG_MASK 0x1F
53
54/* TEST register bank 0 */
55#define LDO_TEST_LPM_MASK 0x40
56#define LDO_TEST_LPM_SEL_CTRL 0x00
57#define LDO_TEST_LPM_SEL_TCXO 0x40
58
59/* TEST register bank 2 */
60#define LDO_TEST_VPROG_UPDATE_MASK 0x08
61#define LDO_TEST_RANGE_SEL_MASK 0x04
62#define LDO_TEST_FINE_STEP_MASK 0x02
63#define LDO_TEST_FINE_STEP_SHIFT 1
64
65/* TEST register bank 4 */
66#define LDO_TEST_RANGE_EXT_MASK 0x01
67
68/* TEST register bank 5 */
69#define LDO_TEST_PIN_CTRL_MASK 0x0F
70#define LDO_TEST_PIN_CTRL_EN3 0x08
71#define LDO_TEST_PIN_CTRL_EN2 0x04
72#define LDO_TEST_PIN_CTRL_EN1 0x02
73#define LDO_TEST_PIN_CTRL_EN0 0x01
74
75/* TEST register bank 6 */
76#define LDO_TEST_PIN_CTRL_LPM_MASK 0x0F
77
78/* Allowable voltage ranges */
79#define PLDO_LOW_UV_MIN 750000
80#define PLDO_LOW_UV_MAX 1537500
81#define PLDO_LOW_FINE_STEP_UV 12500
82
83#define PLDO_NORM_UV_MIN 1500000
84#define PLDO_NORM_UV_MAX 3075000
85#define PLDO_NORM_FINE_STEP_UV 25000
86
87#define PLDO_HIGH_UV_MIN 1750000
88#define PLDO_HIGH_UV_MAX 4900000
89#define PLDO_HIGH_FINE_STEP_UV 50000
90
91#define NLDO_UV_MIN 750000
92#define NLDO_UV_MAX 1537500
93#define NLDO_FINE_STEP_UV 12500
94
95/* SMPS masks and values */
96
97/* CTRL register */
98
99/* Legacy mode */
100#define SMPS_LEGACY_ENABLE 0x80
101#define SMPS_LEGACY_PULL_DOWN_ENABLE 0x40
102#define SMPS_LEGACY_VREF_SEL_MASK 0x20
103#define SMPS_LEGACY_VPROG_MASK 0x1F
104
105/* Advanced mode */
106#define SMPS_ADVANCED_BAND_MASK 0xC0
107#define SMPS_ADVANCED_BAND_OFF 0x00
108#define SMPS_ADVANCED_BAND_1 0x40
109#define SMPS_ADVANCED_BAND_2 0x80
110#define SMPS_ADVANCED_BAND_3 0xC0
111#define SMPS_ADVANCED_VPROG_MASK 0x3F
112
113/* Legacy mode voltage ranges */
114#define SMPS_MODE1_UV_MIN 1500000
115#define SMPS_MODE1_UV_MAX 3050000
116#define SMPS_MODE1_UV_STEP 50000
117
118#define SMPS_MODE2_UV_MIN 750000
119#define SMPS_MODE2_UV_MAX 1525000
120#define SMPS_MODE2_UV_STEP 25000
121
122#define SMPS_MODE3_UV_MIN 375000
123#define SMPS_MODE3_UV_MAX 1150000
124#define SMPS_MODE3_UV_STEP 25000
125
126/* Advanced mode voltage ranges */
127#define SMPS_BAND3_UV_MIN 1500000
128#define SMPS_BAND3_UV_MAX 3075000
129#define SMPS_BAND3_UV_STEP 25000
130
131#define SMPS_BAND2_UV_MIN 750000
132#define SMPS_BAND2_UV_MAX 1537500
133#define SMPS_BAND2_UV_STEP 12500
134
135#define SMPS_BAND1_UV_MIN 375000
136#define SMPS_BAND1_UV_MAX 1162500
137#define SMPS_BAND1_UV_STEP 12500
138
139#define SMPS_UV_MIN SMPS_MODE3_UV_MIN
140#define SMPS_UV_MAX SMPS_MODE1_UV_MAX
141
142/* Test2 register bank 1 */
143#define SMPS_LEGACY_VLOW_SEL_MASK 0x01
144
145/* Test2 register bank 6 */
146#define SMPS_ADVANCED_PULL_DOWN_ENABLE 0x08
147
148/* Test2 register bank 7 */
149#define SMPS_ADVANCED_MODE_MASK 0x02
150#define SMPS_ADVANCED_MODE 0x02
151#define SMPS_LEGACY_MODE 0x00
152
153#define SMPS_IN_ADVANCED_MODE(vreg) \
154 ((vreg->test_reg[7] & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE)
155
156/* BUCK_SLEEP_CNTRL register */
157#define SMPS_PIN_CTRL_MASK 0xF0
158#define SMPS_PIN_CTRL_A1 0x80
159#define SMPS_PIN_CTRL_A0 0x40
160#define SMPS_PIN_CTRL_D1 0x20
161#define SMPS_PIN_CTRL_D0 0x10
162
163#define SMPS_PIN_CTRL_LPM_MASK 0x0F
164#define SMPS_PIN_CTRL_LPM_A1 0x08
165#define SMPS_PIN_CTRL_LPM_A0 0x04
166#define SMPS_PIN_CTRL_LPM_D1 0x02
167#define SMPS_PIN_CTRL_LPM_D0 0x01
168
169/* BUCK_CLOCK_CNTRL register */
170#define SMPS_CLK_DIVIDE2 0x40
171
172#define SMPS_CLK_CTRL_MASK 0x30
173#define SMPS_CLK_CTRL_FOLLOW_TCXO 0x00
174#define SMPS_CLK_CTRL_PWM 0x10
175#define SMPS_CLK_CTRL_PFM 0x20
176
177/* LVS masks and values */
178
179/* CTRL register */
180#define LVS_ENABLE_MASK 0x80
181#define LVS_ENABLE 0x80
182#define LVS_PULL_DOWN_ENABLE_MASK 0x40
183#define LVS_PULL_DOWN_ENABLE 0x00
184#define LVS_PULL_DOWN_DISABLE 0x40
185
186#define LVS_PIN_CTRL_MASK 0x0F
187#define LVS_PIN_CTRL_EN0 0x08
188#define LVS_PIN_CTRL_EN1 0x04
189#define LVS_PIN_CTRL_EN2 0x02
190#define LVS_PIN_CTRL_EN3 0x01
191
192/* NCP masks and values */
193
194/* CTRL register */
195#define NCP_VPROG_MASK 0x1F
196
197#define NCP_UV_MIN 1500000
198#define NCP_UV_MAX 3050000
199#define NCP_UV_STEP 50000
200
201#define GLOBAL_ENABLE_MAX (2)
202struct pm8058_enable {
203 u16 addr;
204 u8 reg;
205};
206
207struct pm8058_vreg {
208 struct pm8058_vreg_pdata *pdata;
209 struct regulator_dev *rdev;
210 struct pm8058_enable *global_enable[GLOBAL_ENABLE_MAX];
211 int hpm_min_load;
212 int save_uV;
213 unsigned pc_vote;
214 unsigned optimum;
215 unsigned mode_initialized;
216 u16 ctrl_addr;
217 u16 test_addr;
218 u16 clk_ctrl_addr;
219 u16 sleep_ctrl_addr;
220 u8 type;
221 u8 ctrl_reg;
222 u8 test_reg[REGULATOR_TEST_BANKS_MAX];
223 u8 clk_ctrl_reg;
224 u8 sleep_ctrl_reg;
225 u8 is_nmos;
226 u8 global_enable_mask[GLOBAL_ENABLE_MAX];
227};
228
229#define LDO_M2(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
230 _en0, _en0_mask, _en1, _en1_mask) \
231 [PM8058_VREG_ID_##_id] = { \
232 .ctrl_addr = _ctrl_addr, \
233 .test_addr = _test_addr, \
234 .type = REGULATOR_TYPE_LDO, \
235 .hpm_min_load = PM8058_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
236 .is_nmos = _is_nmos, \
237 .global_enable = { \
238 [0] = _en0, \
239 [1] = _en1, \
240 }, \
241 .global_enable_mask = { \
242 [0] = _en0_mask, \
243 [1] = _en1_mask, \
244 }, \
245 }
246
247#define LDO(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
248 _en0, _en0_mask) \
249 LDO_M2(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
250 _en0, _en0_mask, NULL, 0)
251
252#define SMPS(_id, _ctrl_addr, _test_addr, _clk_ctrl_addr, _sleep_ctrl_addr, \
253 _hpm_min_load, _en0, _en0_mask) \
254 [PM8058_VREG_ID_##_id] = { \
255 .ctrl_addr = _ctrl_addr, \
256 .test_addr = _test_addr, \
257 .clk_ctrl_addr = _clk_ctrl_addr, \
258 .sleep_ctrl_addr = _sleep_ctrl_addr, \
259 .type = REGULATOR_TYPE_SMPS, \
260 .hpm_min_load = PM8058_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
261 .global_enable = { \
262 [0] = _en0, \
263 [1] = NULL, \
264 }, \
265 .global_enable_mask = { \
266 [0] = _en0_mask, \
267 [1] = 0, \
268 }, \
269 }
270
271#define LVS(_id, _ctrl_addr, _en0, _en0_mask) \
272 [PM8058_VREG_ID_##_id] = { \
273 .ctrl_addr = _ctrl_addr, \
274 .type = REGULATOR_TYPE_LVS, \
275 .global_enable = { \
276 [0] = _en0, \
277 [1] = NULL, \
278 }, \
279 .global_enable_mask = { \
280 [0] = _en0_mask, \
281 [1] = 0, \
282 }, \
283 }
284
285#define NCP(_id, _ctrl_addr, _test1) \
286 [PM8058_VREG_ID_##_id] = { \
287 .ctrl_addr = _ctrl_addr, \
288 .type = REGULATOR_TYPE_NCP, \
289 .test_addr = _test1, \
290 .global_enable = { \
291 [0] = NULL, \
292 [1] = NULL, \
293 }, \
294 .global_enable_mask = { \
295 [0] = 0, \
296 [1] = 0, \
297 }, \
298 }
299
300#define MASTER_ENABLE_COUNT 6
301
302#define EN_MSM 0
303#define EN_PH 1
304#define EN_RF 2
305#define EN_GRP_5_4 3
306#define EN_GRP_3_2 4
307#define EN_GRP_1_0 5
308
309/* Master regulator control registers */
310static struct pm8058_enable m_en[MASTER_ENABLE_COUNT] = {
311 [EN_MSM] = {
312 .addr = 0x018, /* VREG_EN_MSM */
313 },
314 [EN_PH] = {
315 .addr = 0x019, /* VREG_EN_PH */
316 },
317 [EN_RF] = {
318 .addr = 0x01A, /* VREG_EN_RF */
319 },
320 [EN_GRP_5_4] = {
321 .addr = 0x1C8, /* VREG_EN_MSM_GRP_5-4 */
322 },
323 [EN_GRP_3_2] = {
324 .addr = 0x1C9, /* VREG_EN_MSM_GRP_3-2 */
325 },
326 [EN_GRP_1_0] = {
327 .addr = 0x1CA, /* VREG_EN_MSM_GRP_1-0 */
328 },
329};
330
331
332static struct pm8058_vreg pm8058_vreg[] = {
333 /* id ctrl test n/p hpm_min m_en m_en_mask */
334 LDO(L0, 0x009, 0x065, 1, LDO_150, &m_en[EN_GRP_5_4], BIT(3)),
335 LDO(L1, 0x00A, 0x066, 1, LDO_300, &m_en[EN_GRP_5_4], BIT(6) | BIT(2)),
336 LDO(L2, 0x00B, 0x067, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(2)),
337 LDO(L3, 0x00C, 0x068, 0, LDO_150, &m_en[EN_GRP_1_0], BIT(1)),
338 LDO(L4, 0x00D, 0x069, 0, LDO_50, &m_en[EN_MSM], 0),
339 LDO(L5, 0x00E, 0x06A, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(7)),
340 LDO(L6, 0x00F, 0x06B, 0, LDO_50, &m_en[EN_GRP_1_0], BIT(2)),
341 LDO(L7, 0x010, 0x06C, 0, LDO_50, &m_en[EN_GRP_3_2], BIT(3)),
342 LDO(L8, 0x011, 0x06D, 0, LDO_300, &m_en[EN_PH], BIT(7)),
343 LDO(L9, 0x012, 0x06E, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(3)),
344 LDO(L10, 0x013, 0x06F, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(4)),
345 LDO(L11, 0x014, 0x070, 0, LDO_150, &m_en[EN_PH], BIT(4)),
346 LDO(L12, 0x015, 0x071, 0, LDO_150, &m_en[EN_PH], BIT(3)),
347 LDO(L13, 0x016, 0x072, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(1)),
348 LDO(L14, 0x017, 0x073, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(5)),
349 LDO(L15, 0x089, 0x0E5, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(4)),
350 LDO(L16, 0x08A, 0x0E6, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(0)),
351 LDO(L17, 0x08B, 0x0E7, 0, LDO_150, &m_en[EN_RF], BIT(7)),
352 LDO(L18, 0x11D, 0x125, 0, LDO_150, &m_en[EN_RF], BIT(6)),
353 LDO(L19, 0x11E, 0x126, 0, LDO_150, &m_en[EN_RF], BIT(5)),
354 LDO(L20, 0x11F, 0x127, 0, LDO_150, &m_en[EN_RF], BIT(4)),
355 LDO_M2(L21, 0x120, 0x128, 1, LDO_150, &m_en[EN_GRP_5_4], BIT(1),
356 &m_en[EN_GRP_1_0], BIT(6)),
357 LDO(L22, 0x121, 0x129, 1, LDO_300, &m_en[EN_GRP_3_2], BIT(7)),
358 LDO(L23, 0x122, 0x12A, 1, LDO_300, &m_en[EN_GRP_5_4], BIT(0)),
359 LDO(L24, 0x123, 0x12B, 1, LDO_150, &m_en[EN_RF], BIT(3)),
360 LDO(L25, 0x124, 0x12C, 1, LDO_150, &m_en[EN_RF], BIT(2)),
361
362 /* id ctrl test2 clk sleep hpm_min m_en m_en_mask */
363 SMPS(S0, 0x004, 0x084, 0x1D1, 0x1D8, SMPS, &m_en[EN_MSM], BIT(7)),
364 SMPS(S1, 0x005, 0x085, 0x1D2, 0x1DB, SMPS, &m_en[EN_MSM], BIT(6)),
365 SMPS(S2, 0x110, 0x119, 0x1D3, 0x1DE, SMPS, &m_en[EN_GRP_5_4], BIT(5)),
366 SMPS(S3, 0x111, 0x11A, 0x1D4, 0x1E1, SMPS, &m_en[EN_GRP_5_4],
367 BIT(7) | BIT(4)),
368 SMPS(S4, 0x112, 0x11B, 0x1D5, 0x1E4, SMPS, &m_en[EN_GRP_3_2], BIT(5)),
369
370 /* id ctrl m_en m_en_mask */
371 LVS(LVS0, 0x12D, &m_en[EN_RF], BIT(1)),
372 LVS(LVS1, 0x12F, &m_en[EN_GRP_1_0], BIT(0)),
373
374 /* id ctrl test1 */
375 NCP(NCP, 0x090, 0x0EC),
376};
377
378static int pm8058_smps_set_voltage_advanced(struct pm8058_vreg *vreg,
379 struct pm8058_chip *chip, int uV,
380 int force_on);
381static int pm8058_smps_set_voltage_legacy(struct pm8058_vreg *vreg,
382 struct pm8058_chip *chip, int uV);
383static int _pm8058_vreg_is_enabled(struct pm8058_vreg *vreg);
384
385static unsigned int pm8058_vreg_get_mode(struct regulator_dev *dev);
386
387static void print_write_error(struct pm8058_vreg *vreg, int rc,
388 const char *func);
389
390static int pm8058_vreg_write(struct pm8058_chip *chip,
391 u16 addr, u8 val, u8 mask, u8 *reg_save)
392{
393 int rc = 0;
394 u8 reg;
395
396 reg = (*reg_save & ~mask) | (val & mask);
397 if (reg != *reg_save)
398 rc = pm8058_write(chip, addr, &reg, 1);
399 if (rc)
400 pr_err("%s: pm8058_write failed, rc=%d\n", __func__, rc);
401 else
402 *reg_save = reg;
403 return rc;
404}
405
406static int pm8058_vreg_is_global_enabled(struct pm8058_vreg *vreg)
407{
408 int ret = 0, i;
409
410 for (i = 0;
411 (i < GLOBAL_ENABLE_MAX) && !ret && vreg->global_enable[i]; i++)
412 ret = vreg->global_enable[i]->reg &
413 vreg->global_enable_mask[i];
414
415 return ret;
416}
417
418
419static int pm8058_vreg_set_global_enable(struct pm8058_vreg *vreg,
420 struct pm8058_chip *chip, int on)
421{
422 int rc = 0, i;
423
424 for (i = 0;
425 (i < GLOBAL_ENABLE_MAX) && !rc && vreg->global_enable[i]; i++)
426 rc = pm8058_vreg_write(chip, vreg->global_enable[i]->addr,
427 (on ? vreg->global_enable_mask[i] : 0),
428 vreg->global_enable_mask[i],
429 &vreg->global_enable[i]->reg);
430
431 return rc;
432}
433
434static int pm8058_vreg_using_pin_ctrl(struct pm8058_vreg *vreg)
435{
436 int ret = 0;
437
438 switch (vreg->type) {
439 case REGULATOR_TYPE_LDO:
440 ret = ((vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK) << 4)
441 | (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK);
442 break;
443 case REGULATOR_TYPE_SMPS:
444 ret = vreg->sleep_ctrl_reg
445 & (SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK);
446 break;
447 case REGULATOR_TYPE_LVS:
448 ret = vreg->ctrl_reg & LVS_PIN_CTRL_MASK;
449 break;
450 }
451
452 return ret;
453}
454
455static int pm8058_vreg_set_pin_ctrl(struct pm8058_vreg *vreg,
456 struct pm8058_chip *chip, int on)
457{
458 int rc = 0, bank;
459 u8 val = 0, mask;
460 unsigned pc = vreg->pdata->pin_ctrl;
461 unsigned pf = vreg->pdata->pin_fn;
462
463 switch (vreg->type) {
464 case REGULATOR_TYPE_LDO:
465 if (on) {
466 if (pc & PM8058_VREG_PIN_CTRL_D0)
467 val |= LDO_TEST_PIN_CTRL_EN0;
468 if (pc & PM8058_VREG_PIN_CTRL_D1)
469 val |= LDO_TEST_PIN_CTRL_EN1;
470 if (pc & PM8058_VREG_PIN_CTRL_A0)
471 val |= LDO_TEST_PIN_CTRL_EN2;
472 if (pc & PM8058_VREG_PIN_CTRL_A1)
473 val |= LDO_TEST_PIN_CTRL_EN3;
474
475 bank = (pf == PM8058_VREG_PIN_FN_ENABLE ? 5 : 6);
476 rc = pm8058_vreg_write(chip, vreg->test_addr,
477 val | REGULATOR_BANK_SEL(bank)
478 | REGULATOR_BANK_WRITE,
479 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
480 &vreg->test_reg[bank]);
481 if (rc)
482 goto bail;
483
484 val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
485 | REGULATOR_BANK_SEL(0);
486 mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
487 rc = pm8058_vreg_write(chip, vreg->test_addr, val, mask,
488 &vreg->test_reg[0]);
489 if (rc)
490 goto bail;
491
492 if (pf == PM8058_VREG_PIN_FN_ENABLE) {
493 /* Pin control ON/OFF */
494 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
495 LDO_CTRL_PM_HPM,
496 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK,
497 &vreg->ctrl_reg);
498 if (rc)
499 goto bail;
500 rc = pm8058_vreg_set_global_enable(vreg, chip,
501 0);
502 if (rc)
503 goto bail;
504 } else {
505 /* Pin control LPM/HPM */
506 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
507 LDO_ENABLE | LDO_CTRL_PM_LPM,
508 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK,
509 &vreg->ctrl_reg);
510 if (rc)
511 goto bail;
512 }
513 } else {
514 /* Pin control off */
515 rc = pm8058_vreg_write(chip, vreg->test_addr,
516 REGULATOR_BANK_SEL(5) | REGULATOR_BANK_WRITE,
517 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
518 &vreg->test_reg[5]);
519 if (rc)
520 goto bail;
521
522 rc = pm8058_vreg_write(chip, vreg->test_addr,
523 REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
524 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
525 &vreg->test_reg[6]);
526 if (rc)
527 goto bail;
528 }
529 break;
530
531 case REGULATOR_TYPE_SMPS:
532 if (on) {
533 if (pf == PM8058_VREG_PIN_FN_ENABLE) {
534 /* Pin control ON/OFF */
535 if (pc & PM8058_VREG_PIN_CTRL_D0)
536 val |= SMPS_PIN_CTRL_D0;
537 if (pc & PM8058_VREG_PIN_CTRL_D1)
538 val |= SMPS_PIN_CTRL_D1;
539 if (pc & PM8058_VREG_PIN_CTRL_A0)
540 val |= SMPS_PIN_CTRL_A0;
541 if (pc & PM8058_VREG_PIN_CTRL_A1)
542 val |= SMPS_PIN_CTRL_A1;
543 } else {
544 /* Pin control LPM/HPM */
545 if (pc & PM8058_VREG_PIN_CTRL_D0)
546 val |= SMPS_PIN_CTRL_LPM_D0;
547 if (pc & PM8058_VREG_PIN_CTRL_D1)
548 val |= SMPS_PIN_CTRL_LPM_D1;
549 if (pc & PM8058_VREG_PIN_CTRL_A0)
550 val |= SMPS_PIN_CTRL_LPM_A0;
551 if (pc & PM8058_VREG_PIN_CTRL_A1)
552 val |= SMPS_PIN_CTRL_LPM_A1;
553 }
554 rc = pm8058_vreg_set_global_enable(vreg, chip, 0);
555 if (rc)
556 goto bail;
557
558 rc = pm8058_smps_set_voltage_legacy(vreg, chip,
559 vreg->save_uV);
560 if (rc)
561 goto bail;
562
563 rc = pm8058_vreg_write(chip, vreg->sleep_ctrl_addr, val,
564 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
565 &vreg->sleep_ctrl_reg);
566 if (rc)
567 goto bail;
568
569 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
570 (pf == PM8058_VREG_PIN_FN_ENABLE
571 ? 0 : SMPS_LEGACY_ENABLE),
572 SMPS_LEGACY_ENABLE, &vreg->ctrl_reg);
573 if (rc)
574 goto bail;
575
576 rc = pm8058_vreg_write(chip, vreg->clk_ctrl_addr,
577 (pf == PM8058_VREG_PIN_FN_ENABLE
578 ? SMPS_CLK_CTRL_PWM : SMPS_CLK_CTRL_PFM),
579 SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
580 if (rc)
581 goto bail;
582 } else {
583 /* Pin control off */
584 if (!SMPS_IN_ADVANCED_MODE(vreg)) {
585 if (_pm8058_vreg_is_enabled(vreg))
586 val = SMPS_LEGACY_ENABLE;
587 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
588 val, SMPS_LEGACY_ENABLE,
589 &vreg->ctrl_reg);
590 if (rc)
591 goto bail;
592 }
593
594 rc = pm8058_vreg_write(chip, vreg->sleep_ctrl_addr, 0,
595 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
596 &vreg->sleep_ctrl_reg);
597 if (rc)
598 goto bail;
599
600 rc = pm8058_smps_set_voltage_advanced(vreg, chip,
601 vreg->save_uV, 0);
602 if (rc)
603 goto bail;
604 }
605 break;
606
607 case REGULATOR_TYPE_LVS:
608 if (on) {
609 if (pc & PM8058_VREG_PIN_CTRL_D0)
610 val |= LVS_PIN_CTRL_EN0;
611 if (pc & PM8058_VREG_PIN_CTRL_D1)
612 val |= LVS_PIN_CTRL_EN1;
613 if (pc & PM8058_VREG_PIN_CTRL_A0)
614 val |= LVS_PIN_CTRL_EN2;
615 if (pc & PM8058_VREG_PIN_CTRL_A1)
616 val |= LVS_PIN_CTRL_EN3;
617
618 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, val,
619 LVS_PIN_CTRL_MASK | LVS_ENABLE_MASK,
620 &vreg->ctrl_reg);
621 if (rc)
622 goto bail;
623
624 rc = pm8058_vreg_set_global_enable(vreg, chip, 0);
625 if (rc)
626 goto bail;
627 } else {
628 /* Pin control off */
629 if (_pm8058_vreg_is_enabled(vreg))
630 val = LVS_ENABLE;
631
632 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, val,
633 LVS_ENABLE_MASK | LVS_PIN_CTRL_MASK,
634 &vreg->ctrl_reg);
635 if (rc)
636 goto bail;
637
638 }
639 break;
640 }
641
642bail:
643 if (rc)
644 print_write_error(vreg, rc, __func__);
645
646 return rc;
647}
648
649static int pm8058_vreg_enable(struct regulator_dev *dev)
650{
651 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
652 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
653 int mode;
654 int rc = 0;
655
656 mode = pm8058_vreg_get_mode(dev);
657
658 if (mode == REGULATOR_MODE_IDLE) {
659 /* Turn on pin control. */
660 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 1);
661 if (rc)
662 goto bail;
663 return rc;
664 }
665 if (vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
666 rc = pm8058_smps_set_voltage_advanced(vreg, chip,
667 vreg->save_uV, 1);
668 else
669 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, REGULATOR_EN_MASK,
670 REGULATOR_EN_MASK, &vreg->ctrl_reg);
671bail:
672 if (rc)
673 print_write_error(vreg, rc, __func__);
674
675 return rc;
676}
677
678static int _pm8058_vreg_is_enabled(struct pm8058_vreg *vreg)
679{
680 /*
681 * All regulator types except advanced mode SMPS have enable bit in
682 * bit 7 of the control register. Global enable and pin control also
683 * do not work for advanced mode SMPS.
684 */
685 if (!(vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
686 && ((vreg->ctrl_reg & REGULATOR_EN_MASK)
687 || pm8058_vreg_is_global_enabled(vreg)
688 || pm8058_vreg_using_pin_ctrl(vreg)))
689 return 1;
690 else if (vreg->type == REGULATOR_TYPE_SMPS
691 && SMPS_IN_ADVANCED_MODE(vreg)
692 && ((vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK)
693 != SMPS_ADVANCED_BAND_OFF))
694 return 1;
695
696 return 0;
697}
698
699static int pm8058_vreg_is_enabled(struct regulator_dev *dev)
700{
701 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
702
703 return _pm8058_vreg_is_enabled(vreg);
704}
705
706static int pm8058_vreg_disable(struct regulator_dev *dev)
707{
708 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
709 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
710 int rc = 0;
711
712 /* Disable in global control register. */
713 rc = pm8058_vreg_set_global_enable(vreg, chip, 0);
714 if (rc)
715 goto bail;
716
717 /* Turn off pin control. */
718 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
719 if (rc)
720 goto bail;
721
722 /* Disable in local control register. */
723 if (vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
724 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
725 SMPS_ADVANCED_BAND_OFF, SMPS_ADVANCED_BAND_MASK,
726 &vreg->ctrl_reg);
727 else
728 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, 0,
729 REGULATOR_EN_MASK, &vreg->ctrl_reg);
730
731bail:
732 if (rc)
733 print_write_error(vreg, rc, __func__);
734
735 return rc;
736}
737
738static int pm8058_pldo_set_voltage(struct pm8058_chip *chip,
739 struct pm8058_vreg *vreg, int uV)
740{
741 int vmin, rc = 0;
742 unsigned vprog, fine_step;
743 u8 range_ext, range_sel, fine_step_reg;
744
745 if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX)
746 return -EINVAL;
747
748 if (uV < PLDO_LOW_UV_MAX + PLDO_LOW_FINE_STEP_UV) {
749 vmin = PLDO_LOW_UV_MIN;
750 fine_step = PLDO_LOW_FINE_STEP_UV;
751 range_ext = 0;
752 range_sel = LDO_TEST_RANGE_SEL_MASK;
753 } else if (uV < PLDO_NORM_UV_MAX + PLDO_NORM_FINE_STEP_UV) {
754 vmin = PLDO_NORM_UV_MIN;
755 fine_step = PLDO_NORM_FINE_STEP_UV;
756 range_ext = 0;
757 range_sel = 0;
758 } else {
759 vmin = PLDO_HIGH_UV_MIN;
760 fine_step = PLDO_HIGH_FINE_STEP_UV;
761 range_ext = LDO_TEST_RANGE_EXT_MASK;
762 range_sel = 0;
763 }
764
765 vprog = (uV - vmin) / fine_step;
766 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
767 vprog >>= 1;
768
769 /*
770 * Disable program voltage update if range extension, range select,
771 * or fine step have changed and the regulator is enabled.
772 */
773 if (_pm8058_vreg_is_enabled(vreg) &&
774 (((range_ext ^ vreg->test_reg[4]) & LDO_TEST_RANGE_EXT_MASK)
775 || ((range_sel ^ vreg->test_reg[2]) & LDO_TEST_RANGE_SEL_MASK)
776 || ((fine_step_reg ^ vreg->test_reg[2])
777 & LDO_TEST_FINE_STEP_MASK))) {
778 rc = pm8058_vreg_write(chip, vreg->test_addr,
779 REGULATOR_BANK_SEL(2) | REGULATOR_BANK_WRITE,
780 REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
781 &vreg->test_reg[2]);
782 if (rc)
783 goto bail;
784 }
785
786 /* Write new voltage. */
787 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, vprog,
788 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
789 if (rc)
790 goto bail;
791
792 /* Write range extension. */
793 rc = pm8058_vreg_write(chip, vreg->test_addr,
794 range_ext | REGULATOR_BANK_SEL(4)
795 | REGULATOR_BANK_WRITE,
796 LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
797 &vreg->test_reg[4]);
798 if (rc)
799 goto bail;
800
801 /* Write fine step, range select and program voltage update. */
802 rc = pm8058_vreg_write(chip, vreg->test_addr,
803 fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
804 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
805 LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
806 | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
807 &vreg->test_reg[2]);
808bail:
809 if (rc)
810 print_write_error(vreg, rc, __func__);
811
812 return rc;
813}
814
815static int pm8058_nldo_set_voltage(struct pm8058_chip *chip,
816 struct pm8058_vreg *vreg, int uV)
817{
818 unsigned vprog, fine_step_reg;
819 int rc;
820
821 if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX)
822 return -EINVAL;
823
824 vprog = (uV - NLDO_UV_MIN) / NLDO_FINE_STEP_UV;
825 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
826 vprog >>= 1;
827
828 /* Write new voltage. */
829 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, vprog,
830 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
831 if (rc)
832 goto bail;
833
834 /* Write fine step. */
835 rc = pm8058_vreg_write(chip, vreg->test_addr,
836 fine_step_reg | REGULATOR_BANK_SEL(2)
837 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
838 LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
839 | LDO_TEST_VPROG_UPDATE_MASK,
840 &vreg->test_reg[2]);
841bail:
842 if (rc)
843 print_write_error(vreg, rc, __func__);
844
845 return rc;
846}
847
848static int pm8058_ldo_set_voltage(struct regulator_dev *dev,
849 int min_uV, int max_uV, unsigned *selector)
850{
851 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
852 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
853
854 if (vreg->is_nmos)
855 return pm8058_nldo_set_voltage(chip, vreg, min_uV);
856 else
857 return pm8058_pldo_set_voltage(chip, vreg, min_uV);
858}
859
860static int pm8058_pldo_get_voltage(struct pm8058_vreg *vreg)
861{
862 int vmin, fine_step;
863 u8 range_ext, range_sel, vprog, fine_step_reg;
864
865 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
866 range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
867 range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
868 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
869
870 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
871
872 if (range_sel) {
873 /* low range mode */
874 fine_step = PLDO_LOW_FINE_STEP_UV;
875 vmin = PLDO_LOW_UV_MIN;
876 } else if (!range_ext) {
877 /* normal mode */
878 fine_step = PLDO_NORM_FINE_STEP_UV;
879 vmin = PLDO_NORM_UV_MIN;
880 } else {
881 /* high range mode */
882 fine_step = PLDO_HIGH_FINE_STEP_UV;
883 vmin = PLDO_HIGH_UV_MIN;
884 }
885
886 return fine_step * vprog + vmin;
887}
888
889static int pm8058_nldo_get_voltage(struct pm8058_vreg *vreg)
890{
891 u8 vprog, fine_step_reg;
892
893 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
894 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
895
896 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
897
898 return NLDO_FINE_STEP_UV * vprog + NLDO_UV_MIN;
899}
900
901static int pm8058_ldo_get_voltage(struct regulator_dev *dev)
902{
903 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
904
905 if (vreg->is_nmos)
906 return pm8058_nldo_get_voltage(vreg);
907 else
908 return pm8058_pldo_get_voltage(vreg);
909}
910
911static int pm8058_smps_get_voltage_advanced(struct pm8058_vreg *vreg)
912{
913 u8 vprog, band;
914 int uV = 0;
915
916 vprog = vreg->ctrl_reg & SMPS_ADVANCED_VPROG_MASK;
917 band = vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK;
918
919 if (band == SMPS_ADVANCED_BAND_1)
920 uV = vprog * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
921 else if (band == SMPS_ADVANCED_BAND_2)
922 uV = vprog * SMPS_BAND2_UV_STEP + SMPS_BAND2_UV_MIN;
923 else if (band == SMPS_ADVANCED_BAND_3)
924 uV = vprog * SMPS_BAND3_UV_STEP + SMPS_BAND3_UV_MIN;
925 else
926 uV = vreg->save_uV;
927
928 return uV;
929}
930
931static int pm8058_smps_get_voltage_legacy(struct pm8058_vreg *vreg)
932{
933 u8 vlow, vref, vprog;
934 int uV;
935
936 vlow = vreg->test_reg[1] & SMPS_LEGACY_VLOW_SEL_MASK;
937 vref = vreg->ctrl_reg & SMPS_LEGACY_VREF_SEL_MASK;
938 vprog = vreg->ctrl_reg & SMPS_LEGACY_VPROG_MASK;
939
940 if (vlow && vref) {
941 /* mode 3 */
942 uV = vprog * SMPS_MODE3_UV_STEP + SMPS_MODE3_UV_MIN;
943 } else if (vref) {
944 /* mode 2 */
945 uV = vprog * SMPS_MODE2_UV_STEP + SMPS_MODE2_UV_MIN;
946 } else {
947 /* mode 1 */
948 uV = vprog * SMPS_MODE1_UV_STEP + SMPS_MODE1_UV_MIN;
949 }
950
951 return uV;
952}
953
954static int _pm8058_smps_get_voltage(struct pm8058_vreg *vreg)
955{
956 if (SMPS_IN_ADVANCED_MODE(vreg))
957 return pm8058_smps_get_voltage_advanced(vreg);
958
959 return pm8058_smps_get_voltage_legacy(vreg);
960}
961
962static int pm8058_smps_get_voltage(struct regulator_dev *dev)
963{
964 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
965
966 return _pm8058_smps_get_voltage(vreg);
967}
968
969static int pm8058_smps_set_voltage_advanced(struct pm8058_vreg *vreg,
970 struct pm8058_chip *chip, int uV,
971 int force_on)
972{
973 u8 vprog, band;
974 int rc, new_uV;
975
976 if (uV < SMPS_BAND1_UV_MAX + SMPS_BAND1_UV_STEP) {
977 vprog = ((uV - SMPS_BAND1_UV_MIN) / SMPS_BAND1_UV_STEP);
978 band = SMPS_ADVANCED_BAND_1;
979 new_uV = SMPS_BAND1_UV_MIN + vprog * SMPS_BAND1_UV_STEP;
980 } else if (uV < SMPS_BAND2_UV_MAX + SMPS_BAND2_UV_STEP) {
981 vprog = ((uV - SMPS_BAND2_UV_MIN) / SMPS_BAND2_UV_STEP);
982 band = SMPS_ADVANCED_BAND_2;
983 new_uV = SMPS_BAND2_UV_MIN + vprog * SMPS_BAND2_UV_STEP;
984 } else {
985 vprog = ((uV - SMPS_BAND3_UV_MIN) / SMPS_BAND3_UV_STEP);
986 band = SMPS_ADVANCED_BAND_3;
987 new_uV = SMPS_BAND3_UV_MIN + vprog * SMPS_BAND3_UV_STEP;
988 }
989
990 /* Do not set band if regulator currently disabled. */
991 if (!_pm8058_vreg_is_enabled(vreg) && !force_on)
992 band = SMPS_ADVANCED_BAND_OFF;
993
994 /* Set advanced mode bit to 1. */
995 rc = pm8058_vreg_write(chip, vreg->test_addr, SMPS_ADVANCED_MODE
996 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
997 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
998 &vreg->test_reg[7]);
999 if (rc)
1000 goto bail;
1001
1002 /* Set voltage and voltage band. */
1003 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, band | vprog,
1004 SMPS_ADVANCED_BAND_MASK | SMPS_ADVANCED_VPROG_MASK,
1005 &vreg->ctrl_reg);
1006 if (rc)
1007 goto bail;
1008
1009 vreg->save_uV = new_uV;
1010
1011bail:
1012 return rc;
1013}
1014
1015static int pm8058_smps_set_voltage_legacy(struct pm8058_vreg *vreg,
1016 struct pm8058_chip *chip, int uV)
1017{
1018 u8 vlow, vref, vprog, pd, en;
1019 int rc;
1020
1021 if (uV < SMPS_MODE3_UV_MAX + SMPS_MODE3_UV_STEP) {
1022 vprog = ((uV - SMPS_MODE3_UV_MIN) / SMPS_MODE3_UV_STEP);
1023 vref = SMPS_LEGACY_VREF_SEL_MASK;
1024 vlow = SMPS_LEGACY_VLOW_SEL_MASK;
1025 } else if (uV < SMPS_MODE2_UV_MAX + SMPS_MODE2_UV_STEP) {
1026 vprog = ((uV - SMPS_MODE2_UV_MIN) / SMPS_MODE2_UV_STEP);
1027 vref = SMPS_LEGACY_VREF_SEL_MASK;
1028 vlow = 0;
1029 } else {
1030 vprog = ((uV - SMPS_MODE1_UV_MIN) / SMPS_MODE1_UV_STEP);
1031 vref = 0;
1032 vlow = 0;
1033 }
1034
1035 /* set vlow bit for ultra low voltage mode */
1036 rc = pm8058_vreg_write(chip, vreg->test_addr,
1037 vlow | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1),
1038 REGULATOR_BANK_MASK | SMPS_LEGACY_VLOW_SEL_MASK,
1039 &vreg->test_reg[1]);
1040 if (rc)
1041 goto bail;
1042
1043 /* Set advanced mode bit to 0. */
1044 rc = pm8058_vreg_write(chip, vreg->test_addr, SMPS_LEGACY_MODE
1045 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
1046 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
1047 &vreg->test_reg[7]);
1048 if (rc)
1049 goto bail;
1050
1051 en = (_pm8058_vreg_is_enabled(vreg) ? SMPS_LEGACY_ENABLE : 0);
1052 pd = (vreg->pdata->pull_down_enable ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0);
1053
1054 /* Set voltage (and the rest of the control register). */
1055 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, en | pd | vref | vprog,
1056 SMPS_LEGACY_ENABLE | SMPS_LEGACY_PULL_DOWN_ENABLE
1057 | SMPS_LEGACY_VREF_SEL_MASK | SMPS_LEGACY_VPROG_MASK,
1058 &vreg->ctrl_reg);
1059
1060 vreg->save_uV = pm8058_smps_get_voltage_legacy(vreg);
1061
1062bail:
1063 return rc;
1064}
1065
1066static int pm8058_smps_set_voltage(struct regulator_dev *dev,
1067 int min_uV, int max_uV, unsigned *selector)
1068{
1069 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1070 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
1071 int rc = 0;
1072
1073 if (min_uV < SMPS_UV_MIN || min_uV > SMPS_UV_MAX)
1074 return -EINVAL;
1075
1076 if (SMPS_IN_ADVANCED_MODE(vreg))
1077 rc = pm8058_smps_set_voltage_advanced(vreg, chip, min_uV, 0);
1078 else
1079 rc = pm8058_smps_set_voltage_legacy(vreg, chip, min_uV);
1080
1081 if (rc)
1082 print_write_error(vreg, rc, __func__);
1083
1084 return rc;
1085}
1086
1087static int pm8058_ncp_set_voltage(struct regulator_dev *dev,
1088 int min_uV, int max_uV, unsigned *selector)
1089{
1090 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1091 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
1092 int rc;
1093 u8 val;
1094
1095 if (min_uV < NCP_UV_MIN || min_uV > NCP_UV_MAX)
1096 return -EINVAL;
1097
1098 val = (min_uV - NCP_UV_MIN) / NCP_UV_STEP;
1099
1100 /* voltage setting */
1101 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, val, NCP_VPROG_MASK,
1102 &vreg->ctrl_reg);
1103 if (rc)
1104 print_write_error(vreg, rc, __func__);
1105
1106 return rc;
1107}
1108
1109static int pm8058_ncp_get_voltage(struct regulator_dev *dev)
1110{
1111 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1112 u8 vprog = vreg->ctrl_reg & NCP_VPROG_MASK;
1113 return NCP_UV_MIN + vprog * NCP_UV_STEP;
1114}
1115
1116static int pm8058_ldo_set_mode(struct pm8058_vreg *vreg,
1117 struct pm8058_chip *chip, unsigned int mode)
1118{
1119 int rc = 0;
1120 u8 mask, val;
1121
1122 switch (mode) {
1123 case REGULATOR_MODE_FAST:
1124 /* HPM */
1125 val = (_pm8058_vreg_is_enabled(vreg) ? LDO_ENABLE : 0)
1126 | LDO_CTRL_PM_HPM;
1127 mask = LDO_ENABLE_MASK | LDO_CTRL_PM_MASK;
1128 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, val, mask,
1129 &vreg->ctrl_reg);
1130 if (rc)
1131 goto bail;
1132
1133 if (pm8058_vreg_using_pin_ctrl(vreg))
1134 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
1135 if (rc)
1136 goto bail;
1137 break;
1138
1139 case REGULATOR_MODE_STANDBY:
1140 /* LPM */
1141 val = (_pm8058_vreg_is_enabled(vreg) ? LDO_ENABLE : 0)
1142 | LDO_CTRL_PM_LPM;
1143 mask = LDO_ENABLE_MASK | LDO_CTRL_PM_MASK;
1144 rc = pm8058_vreg_write(chip, vreg->ctrl_addr, val, mask,
1145 &vreg->ctrl_reg);
1146 if (rc)
1147 goto bail;
1148
1149 val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1150 | REGULATOR_BANK_SEL(0);
1151 mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
1152 rc = pm8058_vreg_write(chip, vreg->test_addr, val, mask,
1153 &vreg->test_reg[0]);
1154 if (rc)
1155 goto bail;
1156
1157 if (pm8058_vreg_using_pin_ctrl(vreg))
1158 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
1159 if (rc)
1160 goto bail;
1161 break;
1162
1163 case REGULATOR_MODE_IDLE:
1164 /* Pin Control */
1165 if (_pm8058_vreg_is_enabled(vreg))
1166 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 1);
1167 if (rc)
1168 goto bail;
1169 break;
1170
1171 default:
1172 pr_err("%s: invalid mode: %u\n", __func__, mode);
1173 return -EINVAL;
1174 }
1175
1176bail:
1177 if (rc)
1178 print_write_error(vreg, rc, __func__);
1179
1180 return rc;
1181}
1182
1183static int pm8058_smps_set_mode(struct pm8058_vreg *vreg,
1184 struct pm8058_chip *chip, unsigned int mode)
1185{
1186 int rc = 0;
1187 u8 mask, val;
1188
1189 switch (mode) {
1190 case REGULATOR_MODE_FAST:
1191 /* HPM */
1192 val = SMPS_CLK_CTRL_PWM;
1193 mask = SMPS_CLK_CTRL_MASK;
1194 rc = pm8058_vreg_write(chip, vreg->clk_ctrl_addr, val, mask,
1195 &vreg->clk_ctrl_reg);
1196 if (rc)
1197 goto bail;
1198
1199 if (pm8058_vreg_using_pin_ctrl(vreg))
1200 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
1201 if (rc)
1202 goto bail;
1203 break;
1204
1205 case REGULATOR_MODE_STANDBY:
1206 /* LPM */
1207 val = SMPS_CLK_CTRL_PFM;
1208 mask = SMPS_CLK_CTRL_MASK;
1209 rc = pm8058_vreg_write(chip, vreg->clk_ctrl_addr, val, mask,
1210 &vreg->clk_ctrl_reg);
1211 if (rc)
1212 goto bail;
1213
1214 if (pm8058_vreg_using_pin_ctrl(vreg))
1215 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
1216 if (rc)
1217 goto bail;
1218 break;
1219
1220 case REGULATOR_MODE_IDLE:
1221 /* Pin Control */
1222 if (_pm8058_vreg_is_enabled(vreg))
1223 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 1);
1224 if (rc)
1225 goto bail;
1226 break;
1227
1228 default:
1229 pr_err("%s: invalid mode: %u\n", __func__, mode);
1230 return -EINVAL;
1231 }
1232
1233bail:
1234 if (rc)
1235 print_write_error(vreg, rc, __func__);
1236
1237 return rc;
1238}
1239
1240static int pm8058_lvs_set_mode(struct pm8058_vreg *vreg,
1241 struct pm8058_chip *chip, unsigned int mode)
1242{
1243 int rc = 0;
1244
1245 if (mode == REGULATOR_MODE_IDLE) {
1246 /* Use pin control. */
1247 if (_pm8058_vreg_is_enabled(vreg))
1248 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 1);
1249 } else {
1250 /* Turn off pin control. */
1251 rc = pm8058_vreg_set_pin_ctrl(vreg, chip, 0);
1252 }
1253
1254 return rc;
1255}
1256
1257/*
1258 * Optimum mode programming:
1259 * REGULATOR_MODE_FAST: Go to HPM (highest priority)
1260 * REGULATOR_MODE_STANDBY: Go to pin ctrl mode if there are any pin ctrl
1261 * votes, else go to LPM
1262 *
1263 * Pin ctrl mode voting via regulator set_mode:
1264 * REGULATOR_MODE_IDLE: Go to pin ctrl mode if the optimum mode is LPM, else
1265 * go to HPM
1266 * REGULATOR_MODE_NORMAL: Go to LPM if it is the optimum mode, else go to HPM
1267 */
1268static int pm8058_vreg_set_mode(struct regulator_dev *dev, unsigned int mode)
1269{
1270 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1271 struct pm8058_chip *chip = dev_get_drvdata(dev->dev.parent);
1272 unsigned prev_optimum = vreg->optimum;
1273 unsigned prev_pc_vote = vreg->pc_vote;
1274 unsigned prev_mode_initialized = vreg->mode_initialized;
1275 int new_mode = REGULATOR_MODE_FAST;
1276 int rc = 0;
1277
1278 /* Determine new mode to go into. */
1279 switch (mode) {
1280 case REGULATOR_MODE_FAST:
1281 new_mode = REGULATOR_MODE_FAST;
1282 vreg->optimum = mode;
1283 vreg->mode_initialized = 1;
1284 break;
1285
1286 case REGULATOR_MODE_STANDBY:
1287 if (vreg->pc_vote)
1288 new_mode = REGULATOR_MODE_IDLE;
1289 else
1290 new_mode = REGULATOR_MODE_STANDBY;
1291 vreg->optimum = mode;
1292 vreg->mode_initialized = 1;
1293 break;
1294
1295 case REGULATOR_MODE_IDLE:
1296 if (vreg->pc_vote++)
1297 goto done; /* already taken care of */
1298
1299 if (vreg->mode_initialized
1300 && vreg->optimum == REGULATOR_MODE_FAST)
1301 new_mode = REGULATOR_MODE_FAST;
1302 else
1303 new_mode = REGULATOR_MODE_IDLE;
1304 break;
1305
1306 case REGULATOR_MODE_NORMAL:
1307 if (vreg->pc_vote && --(vreg->pc_vote))
1308 goto done; /* already taken care of */
1309
1310 if (vreg->optimum == REGULATOR_MODE_STANDBY)
1311 new_mode = REGULATOR_MODE_STANDBY;
1312 else
1313 new_mode = REGULATOR_MODE_FAST;
1314 break;
1315
1316 default:
1317 pr_err("%s: unknown mode, mode=%u\n", __func__, mode);
1318 return -EINVAL;
1319 }
1320
1321 switch (vreg->type) {
1322 case REGULATOR_TYPE_LDO:
1323 rc = pm8058_ldo_set_mode(vreg, chip, new_mode);
1324 break;
1325 case REGULATOR_TYPE_SMPS:
1326 rc = pm8058_smps_set_mode(vreg, chip, new_mode);
1327 break;
1328 case REGULATOR_TYPE_LVS:
1329 rc = pm8058_lvs_set_mode(vreg, chip, new_mode);
1330 break;
1331 }
1332
1333 if (rc) {
1334 print_write_error(vreg, rc, __func__);
1335 vreg->mode_initialized = prev_mode_initialized;
1336 vreg->optimum = prev_optimum;
1337 vreg->pc_vote = prev_pc_vote;
1338 return rc;
1339 }
1340
1341done:
1342 return 0;
1343}
1344
1345static unsigned int pm8058_vreg_get_mode(struct regulator_dev *dev)
1346{
1347 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1348
1349 if (!vreg->mode_initialized && vreg->pc_vote)
1350 return REGULATOR_MODE_IDLE;
1351
1352 /* Check physical pin control state. */
1353 switch (vreg->type) {
1354 case REGULATOR_TYPE_LDO:
1355 if (!(vreg->ctrl_reg & LDO_ENABLE_MASK)
1356 && !pm8058_vreg_is_global_enabled(vreg)
1357 && (vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK))
1358 return REGULATOR_MODE_IDLE;
1359 else if (((vreg->ctrl_reg & LDO_ENABLE_MASK)
1360 || pm8058_vreg_is_global_enabled(vreg))
1361 && (vreg->ctrl_reg & LDO_CTRL_PM_MASK)
1362 && (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK))
1363 return REGULATOR_MODE_IDLE;
1364 break;
1365 case REGULATOR_TYPE_SMPS:
1366 if (!SMPS_IN_ADVANCED_MODE(vreg)
1367 && !(vreg->ctrl_reg & REGULATOR_EN_MASK)
1368 && !pm8058_vreg_is_global_enabled(vreg)
1369 && (vreg->sleep_ctrl_reg & SMPS_PIN_CTRL_MASK))
1370 return REGULATOR_MODE_IDLE;
1371 else if (!SMPS_IN_ADVANCED_MODE(vreg)
1372 && ((vreg->ctrl_reg & REGULATOR_EN_MASK)
1373 || pm8058_vreg_is_global_enabled(vreg))
1374 && ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK)
1375 == SMPS_CLK_CTRL_PFM)
1376 && (vreg->sleep_ctrl_reg & SMPS_PIN_CTRL_LPM_MASK))
1377 return REGULATOR_MODE_IDLE;
1378 break;
1379 case REGULATOR_TYPE_LVS:
1380 if (!(vreg->ctrl_reg & LVS_ENABLE_MASK)
1381 && !pm8058_vreg_is_global_enabled(vreg)
1382 && (vreg->ctrl_reg & LVS_PIN_CTRL_MASK))
1383 return REGULATOR_MODE_IDLE;
1384 }
1385
1386 if (vreg->optimum == REGULATOR_MODE_FAST)
1387 return REGULATOR_MODE_FAST;
1388 else if (vreg->pc_vote)
1389 return REGULATOR_MODE_IDLE;
1390 else if (vreg->optimum == REGULATOR_MODE_STANDBY)
1391 return REGULATOR_MODE_STANDBY;
1392 return REGULATOR_MODE_FAST;
1393}
1394
1395unsigned int pm8058_vreg_get_optimum_mode(struct regulator_dev *dev,
1396 int input_uV, int output_uV, int load_uA)
1397{
1398 struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
1399
1400 if (load_uA <= 0) {
1401 /*
1402 * pm8058_vreg_get_optimum_mode is being called before consumers
1403 * have specified their load currents via
1404 * regulator_set_optimum_mode. Return whatever the existing mode
1405 * is.
1406 */
1407 return pm8058_vreg_get_mode(dev);
1408 }
1409
1410 if (load_uA >= vreg->hpm_min_load)
1411 return REGULATOR_MODE_FAST;
1412 return REGULATOR_MODE_STANDBY;
1413}
1414
1415static struct regulator_ops pm8058_ldo_ops = {
1416 .enable = pm8058_vreg_enable,
1417 .disable = pm8058_vreg_disable,
1418 .is_enabled = pm8058_vreg_is_enabled,
1419 .set_voltage = pm8058_ldo_set_voltage,
1420 .get_voltage = pm8058_ldo_get_voltage,
1421 .set_mode = pm8058_vreg_set_mode,
1422 .get_mode = pm8058_vreg_get_mode,
1423 .get_optimum_mode = pm8058_vreg_get_optimum_mode,
1424};
1425
1426static struct regulator_ops pm8058_smps_ops = {
1427 .enable = pm8058_vreg_enable,
1428 .disable = pm8058_vreg_disable,
1429 .is_enabled = pm8058_vreg_is_enabled,
1430 .set_voltage = pm8058_smps_set_voltage,
1431 .get_voltage = pm8058_smps_get_voltage,
1432 .set_mode = pm8058_vreg_set_mode,
1433 .get_mode = pm8058_vreg_get_mode,
1434 .get_optimum_mode = pm8058_vreg_get_optimum_mode,
1435};
1436
1437static struct regulator_ops pm8058_lvs_ops = {
1438 .enable = pm8058_vreg_enable,
1439 .disable = pm8058_vreg_disable,
1440 .is_enabled = pm8058_vreg_is_enabled,
1441 .set_mode = pm8058_vreg_set_mode,
1442 .get_mode = pm8058_vreg_get_mode,
1443};
1444
1445static struct regulator_ops pm8058_ncp_ops = {
1446 .enable = pm8058_vreg_enable,
1447 .disable = pm8058_vreg_disable,
1448 .is_enabled = pm8058_vreg_is_enabled,
1449 .set_voltage = pm8058_ncp_set_voltage,
1450 .get_voltage = pm8058_ncp_get_voltage,
1451};
1452
1453#define VREG_DESCRIP(_id, _name, _ops) \
1454 [_id] = { \
1455 .id = _id, \
1456 .name = _name, \
1457 .ops = _ops, \
1458 .type = REGULATOR_VOLTAGE, \
1459 .owner = THIS_MODULE, \
1460 }
1461
1462static struct regulator_desc pm8058_vreg_descrip[] = {
1463 VREG_DESCRIP(PM8058_VREG_ID_L0, "8058_l0", &pm8058_ldo_ops),
1464 VREG_DESCRIP(PM8058_VREG_ID_L1, "8058_l1", &pm8058_ldo_ops),
1465 VREG_DESCRIP(PM8058_VREG_ID_L2, "8058_l2", &pm8058_ldo_ops),
1466 VREG_DESCRIP(PM8058_VREG_ID_L3, "8058_l3", &pm8058_ldo_ops),
1467 VREG_DESCRIP(PM8058_VREG_ID_L4, "8058_l4", &pm8058_ldo_ops),
1468 VREG_DESCRIP(PM8058_VREG_ID_L5, "8058_l5", &pm8058_ldo_ops),
1469 VREG_DESCRIP(PM8058_VREG_ID_L6, "8058_l6", &pm8058_ldo_ops),
1470 VREG_DESCRIP(PM8058_VREG_ID_L7, "8058_l7", &pm8058_ldo_ops),
1471 VREG_DESCRIP(PM8058_VREG_ID_L8, "8058_l8", &pm8058_ldo_ops),
1472 VREG_DESCRIP(PM8058_VREG_ID_L9, "8058_l9", &pm8058_ldo_ops),
1473 VREG_DESCRIP(PM8058_VREG_ID_L10, "8058_l10", &pm8058_ldo_ops),
1474 VREG_DESCRIP(PM8058_VREG_ID_L11, "8058_l11", &pm8058_ldo_ops),
1475 VREG_DESCRIP(PM8058_VREG_ID_L12, "8058_l12", &pm8058_ldo_ops),
1476 VREG_DESCRIP(PM8058_VREG_ID_L13, "8058_l13", &pm8058_ldo_ops),
1477 VREG_DESCRIP(PM8058_VREG_ID_L14, "8058_l14", &pm8058_ldo_ops),
1478 VREG_DESCRIP(PM8058_VREG_ID_L15, "8058_l15", &pm8058_ldo_ops),
1479 VREG_DESCRIP(PM8058_VREG_ID_L16, "8058_l16", &pm8058_ldo_ops),
1480 VREG_DESCRIP(PM8058_VREG_ID_L17, "8058_l17", &pm8058_ldo_ops),
1481 VREG_DESCRIP(PM8058_VREG_ID_L18, "8058_l18", &pm8058_ldo_ops),
1482 VREG_DESCRIP(PM8058_VREG_ID_L19, "8058_l19", &pm8058_ldo_ops),
1483 VREG_DESCRIP(PM8058_VREG_ID_L20, "8058_l20", &pm8058_ldo_ops),
1484 VREG_DESCRIP(PM8058_VREG_ID_L21, "8058_l21", &pm8058_ldo_ops),
1485 VREG_DESCRIP(PM8058_VREG_ID_L22, "8058_l22", &pm8058_ldo_ops),
1486 VREG_DESCRIP(PM8058_VREG_ID_L23, "8058_l23", &pm8058_ldo_ops),
1487 VREG_DESCRIP(PM8058_VREG_ID_L24, "8058_l24", &pm8058_ldo_ops),
1488 VREG_DESCRIP(PM8058_VREG_ID_L25, "8058_l25", &pm8058_ldo_ops),
1489
1490 VREG_DESCRIP(PM8058_VREG_ID_S0, "8058_s0", &pm8058_smps_ops),
1491 VREG_DESCRIP(PM8058_VREG_ID_S1, "8058_s1", &pm8058_smps_ops),
1492 VREG_DESCRIP(PM8058_VREG_ID_S2, "8058_s2", &pm8058_smps_ops),
1493 VREG_DESCRIP(PM8058_VREG_ID_S3, "8058_s3", &pm8058_smps_ops),
1494 VREG_DESCRIP(PM8058_VREG_ID_S4, "8058_s4", &pm8058_smps_ops),
1495
1496 VREG_DESCRIP(PM8058_VREG_ID_LVS0, "8058_lvs0", &pm8058_lvs_ops),
1497 VREG_DESCRIP(PM8058_VREG_ID_LVS1, "8058_lvs1", &pm8058_lvs_ops),
1498
1499 VREG_DESCRIP(PM8058_VREG_ID_NCP, "8058_ncp", &pm8058_ncp_ops),
1500};
1501
1502static int pm8058_master_enable_init(struct pm8058_chip *chip)
1503{
1504 int rc = 0, i;
1505
1506 for (i = 0; i < MASTER_ENABLE_COUNT; i++) {
1507 rc = pm8058_read(chip, m_en[i].addr, &(m_en[i].reg), 1);
1508 if (rc)
1509 goto bail;
1510 }
1511
1512bail:
1513 if (rc)
1514 pr_err("%s: pm8058_read failed, rc=%d\n", __func__, rc);
1515
1516 return rc;
1517}
1518
1519static int pm8058_init_ldo(struct pm8058_chip *chip, struct pm8058_vreg *vreg)
1520{
1521 int rc = 0, i;
1522 u8 bank;
1523
1524 /* Save the current test register state. */
1525 for (i = 0; i < LDO_TEST_BANKS; i++) {
1526 bank = REGULATOR_BANK_SEL(i);
1527 rc = pm8058_write(chip, vreg->test_addr, &bank, 1);
1528 if (rc)
1529 goto bail;
1530
1531 rc = pm8058_read(chip, vreg->test_addr, &vreg->test_reg[i], 1);
1532 if (rc)
1533 goto bail;
1534 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
1535 }
1536
1537 if ((vreg->ctrl_reg & LDO_CTRL_PM_MASK) == LDO_CTRL_PM_LPM)
1538 vreg->optimum = REGULATOR_MODE_STANDBY;
1539 else
1540 vreg->optimum = REGULATOR_MODE_FAST;
1541
1542 /* Set pull down enable based on platform data. */
1543 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
1544 (vreg->pdata->pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
1545 LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
1546bail:
1547 return rc;
1548}
1549
1550static int pm8058_init_smps(struct pm8058_chip *chip, struct pm8058_vreg *vreg)
1551{
1552 int rc = 0, i;
1553 u8 bank;
1554
1555 /* Save the current test2 register state. */
1556 for (i = 0; i < SMPS_TEST_BANKS; i++) {
1557 bank = REGULATOR_BANK_SEL(i);
1558 rc = pm8058_write(chip, vreg->test_addr, &bank, 1);
1559 if (rc)
1560 goto bail;
1561
1562 rc = pm8058_read(chip, vreg->test_addr, &vreg->test_reg[i],
1563 1);
1564 if (rc)
1565 goto bail;
1566 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
1567 }
1568
1569 /* Save the current clock control register state. */
1570 rc = pm8058_read(chip, vreg->clk_ctrl_addr, &vreg->clk_ctrl_reg, 1);
1571 if (rc)
1572 goto bail;
1573
1574 /* Save the current sleep control register state. */
1575 rc = pm8058_read(chip, vreg->sleep_ctrl_addr, &vreg->sleep_ctrl_reg, 1);
1576 if (rc)
1577 goto bail;
1578
1579 vreg->save_uV = 1; /* This is not a no-op. */
1580 vreg->save_uV = _pm8058_smps_get_voltage(vreg);
1581
1582 if ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK) == SMPS_CLK_CTRL_PFM)
1583 vreg->optimum = REGULATOR_MODE_STANDBY;
1584 else
1585 vreg->optimum = REGULATOR_MODE_FAST;
1586
1587 /* Set advanced mode pull down enable based on platform data. */
1588 rc = pm8058_vreg_write(chip, vreg->test_addr,
1589 (vreg->pdata->pull_down_enable
1590 ? SMPS_ADVANCED_PULL_DOWN_ENABLE : 0)
1591 | REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
1592 REGULATOR_BANK_MASK | SMPS_ADVANCED_PULL_DOWN_ENABLE,
1593 &vreg->test_reg[6]);
1594 if (rc)
1595 goto bail;
1596
1597 if (!SMPS_IN_ADVANCED_MODE(vreg)) {
1598 /* Set legacy mode pull down enable based on platform data. */
1599 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
1600 (vreg->pdata->pull_down_enable
1601 ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0),
1602 SMPS_LEGACY_PULL_DOWN_ENABLE, &vreg->ctrl_reg);
1603 if (rc)
1604 goto bail;
1605 }
1606
1607bail:
1608 return rc;
1609}
1610
1611static int pm8058_init_lvs(struct pm8058_chip *chip, struct pm8058_vreg *vreg)
1612{
1613 int rc = 0;
1614
1615 vreg->optimum = REGULATOR_MODE_FAST;
1616
1617 /* Set pull down enable based on platform data. */
1618 rc = pm8058_vreg_write(chip, vreg->ctrl_addr,
1619 (vreg->pdata->pull_down_enable
1620 ? LVS_PULL_DOWN_ENABLE : LVS_PULL_DOWN_DISABLE),
1621 LVS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
1622 return rc;
1623}
1624
1625static int pm8058_init_ncp(struct pm8058_chip *chip, struct pm8058_vreg *vreg)
1626{
1627 int rc = 0;
1628
1629 /* Save the current test1 register state. */
1630 rc = pm8058_read(chip, vreg->test_addr, &vreg->test_reg[0], 1);
1631 if (rc)
1632 goto bail;
1633
1634 vreg->optimum = REGULATOR_MODE_FAST;
1635
1636bail:
1637 return rc;
1638}
1639
1640static int pm8058_init_regulator(struct pm8058_chip *chip,
1641 struct pm8058_vreg *vreg)
1642{
1643 static int master_enable_inited;
1644 int rc = 0;
1645
1646 vreg->mode_initialized = 0;
1647
1648 if (!master_enable_inited) {
1649 rc = pm8058_master_enable_init(chip);
1650 if (!rc)
1651 master_enable_inited = 1;
1652 }
1653
1654 /* save the current control register state */
1655 rc = pm8058_read(chip, vreg->ctrl_addr, &vreg->ctrl_reg, 1);
1656 if (rc)
1657 goto bail;
1658
1659 switch (vreg->type) {
1660 case REGULATOR_TYPE_LDO:
1661 rc = pm8058_init_ldo(chip, vreg);
1662 break;
1663 case REGULATOR_TYPE_SMPS:
1664 rc = pm8058_init_smps(chip, vreg);
1665 break;
1666 case REGULATOR_TYPE_LVS:
1667 rc = pm8058_init_lvs(chip, vreg);
1668 break;
1669 case REGULATOR_TYPE_NCP:
1670 rc = pm8058_init_ncp(chip, vreg);
1671 break;
1672 }
1673
1674bail:
1675 if (rc)
1676 pr_err("%s: pm8058_read/write failed; initial register states "
1677 "unknown, rc=%d\n", __func__, rc);
1678 return rc;
1679}
1680
1681static int __devinit pm8058_vreg_probe(struct platform_device *pdev)
1682{
1683 struct regulator_desc *rdesc;
1684 struct pm8058_chip *chip;
1685 struct pm8058_vreg *vreg;
1686 const char *reg_name = NULL;
1687 int rc = 0;
1688
1689 if (pdev == NULL)
1690 return -EINVAL;
1691
1692 if (pdev->id >= 0 && pdev->id < PM8058_VREG_MAX) {
1693 chip = platform_get_drvdata(pdev);
1694 rdesc = &pm8058_vreg_descrip[pdev->id];
1695 vreg = &pm8058_vreg[pdev->id];
1696 vreg->pdata = pdev->dev.platform_data;
1697 reg_name = pm8058_vreg_descrip[pdev->id].name;
1698
1699 rc = pm8058_init_regulator(chip, vreg);
1700 if (rc)
1701 goto bail;
1702
1703 /* Disallow idle and normal modes if pin control isn't set. */
1704 if (vreg->pdata->pin_ctrl == 0)
1705 vreg->pdata->init_data.constraints.valid_modes_mask
1706 &= ~(REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE);
1707
1708 vreg->rdev = regulator_register(rdesc, &pdev->dev,
1709 &vreg->pdata->init_data, vreg);
1710 if (IS_ERR(vreg->rdev)) {
1711 rc = PTR_ERR(vreg->rdev);
1712 pr_err("%s: regulator_register failed for %s, rc=%d\n",
1713 __func__, reg_name, rc);
1714 }
1715 } else {
1716 rc = -ENODEV;
1717 }
1718
1719bail:
1720 if (rc)
1721 pr_err("%s: error for %s, rc=%d\n", __func__, reg_name, rc);
1722
1723 return rc;
1724}
1725
1726static int __devexit pm8058_vreg_remove(struct platform_device *pdev)
1727{
1728 regulator_unregister(pm8058_vreg[pdev->id].rdev);
1729 return 0;
1730}
1731
1732static struct platform_driver pm8058_vreg_driver = {
1733 .probe = pm8058_vreg_probe,
1734 .remove = __devexit_p(pm8058_vreg_remove),
1735 .driver = {
1736 .name = "pm8058-regulator",
1737 .owner = THIS_MODULE,
1738 },
1739};
1740
1741static int __init pm8058_vreg_init(void)
1742{
1743 return platform_driver_register(&pm8058_vreg_driver);
1744}
1745
1746static void __exit pm8058_vreg_exit(void)
1747{
1748 platform_driver_unregister(&pm8058_vreg_driver);
1749}
1750
1751static void print_write_error(struct pm8058_vreg *vreg, int rc,
1752 const char *func)
1753{
1754 const char *reg_name = NULL;
1755 ptrdiff_t id = vreg - pm8058_vreg;
1756
1757 if (id >= 0 && id < PM8058_VREG_MAX)
1758 reg_name = pm8058_vreg_descrip[id].name;
1759 pr_err("%s: pm8058_vreg_write failed for %s, rc=%d\n",
1760 func, reg_name, rc);
1761}
1762
1763subsys_initcall(pm8058_vreg_init);
1764module_exit(pm8058_vreg_exit);
1765
1766MODULE_LICENSE("GPL v2");
1767MODULE_DESCRIPTION("PMIC8058 regulator driver");
1768MODULE_VERSION("1.0");
1769MODULE_ALIAS("platform:pm8058-regulator");