blob: fa17449bbc4d67bdf41c0814225576764cb176e6 [file] [log] [blame]
David Collins5c4a5ab2011-12-02 15:01:26 -08001/*
David Collins0c19fa82012-01-30 11:23:38 -08002 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
David Collins5c4a5ab2011-12-02 15:01:26 -08003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/module.h>
David Collins0c19fa82012-01-30 11:23:38 -080017#include <linux/delay.h>
David Collins5c4a5ab2011-12-02 15:01:26 -080018#include <linux/err.h>
19#include <linux/string.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/bitops.h>
23#include <linux/mfd/pm8xxx/core.h>
24#include <linux/mfd/pm8xxx/regulator.h>
25
26/* Debug Flag Definitions */
27enum {
28 PM8XXX_VREG_DEBUG_REQUEST = BIT(0),
29 PM8XXX_VREG_DEBUG_DUPLICATE = BIT(1),
30 PM8XXX_VREG_DEBUG_INIT = BIT(2),
31 PM8XXX_VREG_DEBUG_WRITES = BIT(3), /* SSBI writes */
32};
33
34static int pm8xxx_vreg_debug_mask;
35module_param_named(
36 debug_mask, pm8xxx_vreg_debug_mask, int, S_IRUSR | S_IWUSR
37);
38
39/* Common Masks */
40#define REGULATOR_ENABLE_MASK 0x80
41#define REGULATOR_ENABLE 0x80
42#define REGULATOR_DISABLE 0x00
43
44#define REGULATOR_BANK_MASK 0xF0
45#define REGULATOR_BANK_SEL(n) ((n) << 4)
46#define REGULATOR_BANK_WRITE 0x80
47
48#define LDO_TEST_BANKS 7
49#define NLDO1200_TEST_BANKS 5
50#define SMPS_TEST_BANKS 8
51
52/*
53 * This voltage in uV is returned by get_voltage functions when there is no way
54 * to determine the current voltage level. It is needed because the regulator
55 * framework treats a 0 uV voltage as an error.
56 */
57#define VOLTAGE_UNKNOWN 1
58
59/* LDO masks and values */
60
61/* CTRL register */
62#define LDO_ENABLE_MASK 0x80
63#define LDO_DISABLE 0x00
64#define LDO_ENABLE 0x80
65#define LDO_PULL_DOWN_ENABLE_MASK 0x40
66#define LDO_PULL_DOWN_ENABLE 0x40
67
68#define LDO_CTRL_PM_MASK 0x20
69#define LDO_CTRL_PM_HPM 0x00
70#define LDO_CTRL_PM_LPM 0x20
71
72#define LDO_CTRL_VPROG_MASK 0x1F
73
74/* TEST register bank 0 */
75#define LDO_TEST_LPM_MASK 0x04
76#define LDO_TEST_LPM_SEL_CTRL 0x00
77#define LDO_TEST_LPM_SEL_TCXO 0x04
78
79/* TEST register bank 2 */
80#define LDO_TEST_VPROG_UPDATE_MASK 0x08
81#define LDO_TEST_RANGE_SEL_MASK 0x04
82#define LDO_TEST_FINE_STEP_MASK 0x02
83#define LDO_TEST_FINE_STEP_SHIFT 1
84
85/* TEST register bank 4 */
86#define LDO_TEST_RANGE_EXT_MASK 0x01
87
88/* TEST register bank 5 */
89#define LDO_TEST_PIN_CTRL_MASK 0x0F
90#define LDO_TEST_PIN_CTRL_EN3 0x08
91#define LDO_TEST_PIN_CTRL_EN2 0x04
92#define LDO_TEST_PIN_CTRL_EN1 0x02
93#define LDO_TEST_PIN_CTRL_EN0 0x01
94
95/* TEST register bank 6 */
96#define LDO_TEST_PIN_CTRL_LPM_MASK 0x0F
97
98/*
99 * If a given voltage could be output by two ranges, then the preferred one must
100 * be determined by the range limits. Specified voltage ranges should must
101 * not overlap.
102 *
103 * Allowable voltage ranges:
104 */
105#define PLDO_LOW_UV_MIN 750000
106#define PLDO_LOW_UV_MAX 1487500
107#define PLDO_LOW_UV_FINE_STEP 12500
108
109#define PLDO_NORM_UV_MIN 1500000
110#define PLDO_NORM_UV_MAX 3075000
111#define PLDO_NORM_UV_FINE_STEP 25000
112
113#define PLDO_HIGH_UV_MIN 1750000
114#define PLDO_HIGH_UV_SET_POINT_MIN 3100000
115#define PLDO_HIGH_UV_MAX 4900000
116#define PLDO_HIGH_UV_FINE_STEP 50000
117
118#define PLDO_LOW_SET_POINTS ((PLDO_LOW_UV_MAX - PLDO_LOW_UV_MIN) \
119 / PLDO_LOW_UV_FINE_STEP + 1)
120#define PLDO_NORM_SET_POINTS ((PLDO_NORM_UV_MAX - PLDO_NORM_UV_MIN) \
121 / PLDO_NORM_UV_FINE_STEP + 1)
122#define PLDO_HIGH_SET_POINTS ((PLDO_HIGH_UV_MAX \
123 - PLDO_HIGH_UV_SET_POINT_MIN) \
124 / PLDO_HIGH_UV_FINE_STEP + 1)
125#define PLDO_SET_POINTS (PLDO_LOW_SET_POINTS \
126 + PLDO_NORM_SET_POINTS \
127 + PLDO_HIGH_SET_POINTS)
128
129#define NLDO_UV_MIN 750000
130#define NLDO_UV_MAX 1537500
131#define NLDO_UV_FINE_STEP 12500
132
133#define NLDO_SET_POINTS ((NLDO_UV_MAX - NLDO_UV_MIN) \
134 / NLDO_UV_FINE_STEP + 1)
135
136/* NLDO1200 masks and values */
137
138/* CTRL register */
139#define NLDO1200_ENABLE_MASK 0x80
140#define NLDO1200_DISABLE 0x00
141#define NLDO1200_ENABLE 0x80
142
143/* Legacy mode */
144#define NLDO1200_LEGACY_PM_MASK 0x20
145#define NLDO1200_LEGACY_PM_HPM 0x00
146#define NLDO1200_LEGACY_PM_LPM 0x20
147
148/* Advanced mode */
149#define NLDO1200_CTRL_RANGE_MASK 0x40
150#define NLDO1200_CTRL_RANGE_HIGH 0x00
151#define NLDO1200_CTRL_RANGE_LOW 0x40
152#define NLDO1200_CTRL_VPROG_MASK 0x3F
153
154#define NLDO1200_LOW_UV_MIN 375000
155#define NLDO1200_LOW_UV_MAX 743750
156#define NLDO1200_LOW_UV_STEP 6250
157
158#define NLDO1200_HIGH_UV_MIN 750000
159#define NLDO1200_HIGH_UV_MAX 1537500
160#define NLDO1200_HIGH_UV_STEP 12500
161
162#define NLDO1200_LOW_SET_POINTS ((NLDO1200_LOW_UV_MAX \
163 - NLDO1200_LOW_UV_MIN) \
164 / NLDO1200_LOW_UV_STEP + 1)
165#define NLDO1200_HIGH_SET_POINTS ((NLDO1200_HIGH_UV_MAX \
166 - NLDO1200_HIGH_UV_MIN) \
167 / NLDO1200_HIGH_UV_STEP + 1)
168#define NLDO1200_SET_POINTS (NLDO1200_LOW_SET_POINTS \
169 + NLDO1200_HIGH_SET_POINTS)
170
171/* TEST register bank 0 */
172#define NLDO1200_TEST_LPM_MASK 0x04
173#define NLDO1200_TEST_LPM_SEL_CTRL 0x00
174#define NLDO1200_TEST_LPM_SEL_TCXO 0x04
175
176/* TEST register bank 1 */
177#define NLDO1200_PULL_DOWN_ENABLE_MASK 0x02
178#define NLDO1200_PULL_DOWN_ENABLE 0x02
179
180/* TEST register bank 2 */
181#define NLDO1200_ADVANCED_MODE_MASK 0x08
182#define NLDO1200_ADVANCED_MODE 0x00
183#define NLDO1200_LEGACY_MODE 0x08
184
185/* Advanced mode power mode control */
186#define NLDO1200_ADVANCED_PM_MASK 0x02
187#define NLDO1200_ADVANCED_PM_HPM 0x00
188#define NLDO1200_ADVANCED_PM_LPM 0x02
189
190#define NLDO1200_IN_ADVANCED_MODE(vreg) \
191 ((vreg->test_reg[2] & NLDO1200_ADVANCED_MODE_MASK) \
192 == NLDO1200_ADVANCED_MODE)
193
194/* SMPS masks and values */
195
196/* CTRL register */
197
198/* Legacy mode */
199#define SMPS_LEGACY_ENABLE_MASK 0x80
200#define SMPS_LEGACY_DISABLE 0x00
201#define SMPS_LEGACY_ENABLE 0x80
202#define SMPS_LEGACY_PULL_DOWN_ENABLE 0x40
203#define SMPS_LEGACY_VREF_SEL_MASK 0x20
204#define SMPS_LEGACY_VPROG_MASK 0x1F
205
206/* Advanced mode */
207#define SMPS_ADVANCED_BAND_MASK 0xC0
208#define SMPS_ADVANCED_BAND_OFF 0x00
209#define SMPS_ADVANCED_BAND_1 0x40
210#define SMPS_ADVANCED_BAND_2 0x80
211#define SMPS_ADVANCED_BAND_3 0xC0
212#define SMPS_ADVANCED_VPROG_MASK 0x3F
213
214/* Legacy mode voltage ranges */
215#define SMPS_MODE3_UV_MIN 375000
216#define SMPS_MODE3_UV_MAX 725000
217#define SMPS_MODE3_UV_STEP 25000
218
219#define SMPS_MODE2_UV_MIN 750000
220#define SMPS_MODE2_UV_MAX 1475000
221#define SMPS_MODE2_UV_STEP 25000
222
223#define SMPS_MODE1_UV_MIN 1500000
224#define SMPS_MODE1_UV_MAX 3050000
225#define SMPS_MODE1_UV_STEP 50000
226
227#define SMPS_MODE3_SET_POINTS ((SMPS_MODE3_UV_MAX \
228 - SMPS_MODE3_UV_MIN) \
229 / SMPS_MODE3_UV_STEP + 1)
230#define SMPS_MODE2_SET_POINTS ((SMPS_MODE2_UV_MAX \
231 - SMPS_MODE2_UV_MIN) \
232 / SMPS_MODE2_UV_STEP + 1)
233#define SMPS_MODE1_SET_POINTS ((SMPS_MODE1_UV_MAX \
234 - SMPS_MODE1_UV_MIN) \
235 / SMPS_MODE1_UV_STEP + 1)
236#define SMPS_LEGACY_SET_POINTS (SMPS_MODE3_SET_POINTS \
237 + SMPS_MODE2_SET_POINTS \
238 + SMPS_MODE1_SET_POINTS)
239
240/* Advanced mode voltage ranges */
241#define SMPS_BAND1_UV_MIN 375000
242#define SMPS_BAND1_UV_MAX 737500
243#define SMPS_BAND1_UV_STEP 12500
244
245#define SMPS_BAND2_UV_MIN 750000
246#define SMPS_BAND2_UV_MAX 1487500
247#define SMPS_BAND2_UV_STEP 12500
248
249#define SMPS_BAND3_UV_MIN 1500000
250#define SMPS_BAND3_UV_MAX 3075000
251#define SMPS_BAND3_UV_STEP 25000
252
253#define SMPS_BAND1_SET_POINTS ((SMPS_BAND1_UV_MAX \
254 - SMPS_BAND1_UV_MIN) \
255 / SMPS_BAND1_UV_STEP + 1)
256#define SMPS_BAND2_SET_POINTS ((SMPS_BAND2_UV_MAX \
257 - SMPS_BAND2_UV_MIN) \
258 / SMPS_BAND2_UV_STEP + 1)
259#define SMPS_BAND3_SET_POINTS ((SMPS_BAND3_UV_MAX \
260 - SMPS_BAND3_UV_MIN) \
261 / SMPS_BAND3_UV_STEP + 1)
262#define SMPS_ADVANCED_SET_POINTS (SMPS_BAND1_SET_POINTS \
263 + SMPS_BAND2_SET_POINTS \
264 + SMPS_BAND3_SET_POINTS)
265
266/* Test2 register bank 1 */
267#define SMPS_LEGACY_VLOW_SEL_MASK 0x01
268
269/* Test2 register bank 6 */
270#define SMPS_ADVANCED_PULL_DOWN_ENABLE 0x08
271
272/* Test2 register bank 7 */
273#define SMPS_ADVANCED_MODE_MASK 0x02
274#define SMPS_ADVANCED_MODE 0x02
275#define SMPS_LEGACY_MODE 0x00
276
277#define SMPS_IN_ADVANCED_MODE(vreg) \
278 ((vreg->test_reg[7] & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE)
279
280/* BUCK_SLEEP_CNTRL register */
281#define SMPS_PIN_CTRL_MASK 0xF0
282#define SMPS_PIN_CTRL_EN3 0x80
283#define SMPS_PIN_CTRL_EN2 0x40
284#define SMPS_PIN_CTRL_EN1 0x20
285#define SMPS_PIN_CTRL_EN0 0x10
286
287#define SMPS_PIN_CTRL_LPM_MASK 0x0F
288#define SMPS_PIN_CTRL_LPM_EN3 0x08
289#define SMPS_PIN_CTRL_LPM_EN2 0x04
290#define SMPS_PIN_CTRL_LPM_EN1 0x02
291#define SMPS_PIN_CTRL_LPM_EN0 0x01
292
293/* BUCK_CLOCK_CNTRL register */
294#define SMPS_CLK_DIVIDE2 0x40
295
296#define SMPS_CLK_CTRL_MASK 0x30
297#define SMPS_CLK_CTRL_FOLLOW_TCXO 0x00
298#define SMPS_CLK_CTRL_PWM 0x10
299#define SMPS_CLK_CTRL_PFM 0x20
300
301/* FTSMPS masks and values */
302
303/* CTRL register */
304#define FTSMPS_VCTRL_BAND_MASK 0xC0
305#define FTSMPS_VCTRL_BAND_OFF 0x00
306#define FTSMPS_VCTRL_BAND_1 0x40
307#define FTSMPS_VCTRL_BAND_2 0x80
308#define FTSMPS_VCTRL_BAND_3 0xC0
309#define FTSMPS_VCTRL_VPROG_MASK 0x3F
310
311#define FTSMPS_BAND1_UV_MIN 350000
312#define FTSMPS_BAND1_UV_MAX 650000
313/* 3 LSB's of program voltage must be 0 in band 1. */
314/* Logical step size */
315#define FTSMPS_BAND1_UV_LOG_STEP 50000
316/* Physical step size */
317#define FTSMPS_BAND1_UV_PHYS_STEP 6250
318
319#define FTSMPS_BAND2_UV_MIN 700000
320#define FTSMPS_BAND2_UV_MAX 1400000
321#define FTSMPS_BAND2_UV_STEP 12500
322
323#define FTSMPS_BAND3_UV_MIN 1400000
324#define FTSMPS_BAND3_UV_SET_POINT_MIN 1500000
325#define FTSMPS_BAND3_UV_MAX 3300000
326#define FTSMPS_BAND3_UV_STEP 50000
327
328#define FTSMPS_BAND1_SET_POINTS ((FTSMPS_BAND1_UV_MAX \
329 - FTSMPS_BAND1_UV_MIN) \
330 / FTSMPS_BAND1_UV_LOG_STEP + 1)
331#define FTSMPS_BAND2_SET_POINTS ((FTSMPS_BAND2_UV_MAX \
332 - FTSMPS_BAND2_UV_MIN) \
333 / FTSMPS_BAND2_UV_STEP + 1)
334#define FTSMPS_BAND3_SET_POINTS ((FTSMPS_BAND3_UV_MAX \
335 - FTSMPS_BAND3_UV_SET_POINT_MIN) \
336 / FTSMPS_BAND3_UV_STEP + 1)
337#define FTSMPS_SET_POINTS (FTSMPS_BAND1_SET_POINTS \
338 + FTSMPS_BAND2_SET_POINTS \
339 + FTSMPS_BAND3_SET_POINTS)
340
341/* FTS_CNFG1 register bank 0 */
342#define FTSMPS_CNFG1_PM_MASK 0x0C
343#define FTSMPS_CNFG1_PM_PWM 0x00
344#define FTSMPS_CNFG1_PM_PFM 0x08
345
346/* PWR_CNFG register */
347#define FTSMPS_PULL_DOWN_ENABLE_MASK 0x40
348#define FTSMPS_PULL_DOWN_ENABLE 0x40
349
350/* VS masks and values */
351
352/* CTRL register */
353#define VS_ENABLE_MASK 0x80
354#define VS_DISABLE 0x00
355#define VS_ENABLE 0x80
356#define VS_PULL_DOWN_ENABLE_MASK 0x40
357#define VS_PULL_DOWN_DISABLE 0x40
358#define VS_PULL_DOWN_ENABLE 0x00
359
David Collins0c19fa82012-01-30 11:23:38 -0800360#define VS_MODE_MASK 0x30
361#define VS_MODE_NORMAL 0x10
362#define VS_MODE_LPM 0x20
363
David Collins5c4a5ab2011-12-02 15:01:26 -0800364#define VS_PIN_CTRL_MASK 0x0F
365#define VS_PIN_CTRL_EN0 0x08
366#define VS_PIN_CTRL_EN1 0x04
367#define VS_PIN_CTRL_EN2 0x02
368#define VS_PIN_CTRL_EN3 0x01
369
David Collins0c19fa82012-01-30 11:23:38 -0800370/* TEST register */
371#define VS_OCP_MASK 0x10
372#define VS_OCP_ENABLE 0x00
373#define VS_OCP_DISABLE 0x10
374
David Collins5c4a5ab2011-12-02 15:01:26 -0800375/* VS300 masks and values */
376
377/* CTRL register */
378#define VS300_CTRL_ENABLE_MASK 0xC0
379#define VS300_CTRL_DISABLE 0x00
380#define VS300_CTRL_ENABLE 0x40
381
382#define VS300_PULL_DOWN_ENABLE_MASK 0x20
383#define VS300_PULL_DOWN_ENABLE 0x20
384
David Collins0c19fa82012-01-30 11:23:38 -0800385#define VS300_MODE_MASK 0x18
386#define VS300_MODE_NORMAL 0x00
387#define VS300_MODE_LPM 0x08
388
David Collins5c4a5ab2011-12-02 15:01:26 -0800389/* NCP masks and values */
390
391/* CTRL register */
392#define NCP_ENABLE_MASK 0x80
393#define NCP_DISABLE 0x00
394#define NCP_ENABLE 0x80
395#define NCP_VPROG_MASK 0x1F
396
397#define NCP_UV_MIN 1500000
398#define NCP_UV_MAX 3050000
399#define NCP_UV_STEP 50000
400
401#define NCP_SET_POINTS ((NCP_UV_MAX - NCP_UV_MIN) \
402 / NCP_UV_STEP + 1)
403
David Collins525da182012-02-13 10:36:37 -0800404/* Boost masks and values */
405#define BOOST_ENABLE_MASK 0x80
406#define BOOST_DISABLE 0x00
407#define BOOST_ENABLE 0x80
408#define BOOST_VPROG_MASK 0x1F
409
410#define BOOST_UV_MIN 4000000
411#define BOOST_UV_MAX 5550000
412#define BOOST_UV_STEP 50000
413
414#define BOOST_SET_POINTS ((BOOST_UV_MAX - BOOST_UV_MIN) \
415 / BOOST_UV_STEP + 1)
416
David Collins5c4a5ab2011-12-02 15:01:26 -0800417#define vreg_err(vreg, fmt, ...) \
418 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
419
420/* Determines which label to add to the print. */
421enum pm8xxx_regulator_action {
422 PM8XXX_REGULATOR_ACTION_INIT,
423 PM8XXX_REGULATOR_ACTION_ENABLE,
424 PM8XXX_REGULATOR_ACTION_DISABLE,
425 PM8XXX_REGULATOR_ACTION_VOLTAGE,
426 PM8XXX_REGULATOR_ACTION_MODE,
427 PM8XXX_REGULATOR_ACTION_PIN_CTRL,
428};
429
430/* Debug state printing */
431static void pm8xxx_vreg_show_state(struct regulator_dev *rdev,
432 enum pm8xxx_regulator_action action);
433
434/*
435 * Perform a masked write to a PMIC register only if the new value differs
436 * from the last value written to the register. This removes redundant
437 * register writing.
438 *
439 * No locking is required because registers are not shared between regulators.
440 */
441static int pm8xxx_vreg_masked_write(struct pm8xxx_vreg *vreg, u16 addr, u8 val,
442 u8 mask, u8 *reg_save)
443{
444 int rc = 0;
445 u8 reg;
446
447 reg = (*reg_save & ~mask) | (val & mask);
448 if (reg != *reg_save) {
449 rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
450
451 if (rc) {
452 pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
453 vreg->rdesc.name, addr, rc);
454 } else {
455 *reg_save = reg;
456 vreg->write_count++;
457 if (pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_WRITES)
458 pr_info("%s: write(0x%03X)=0x%02X\n",
459 vreg->rdesc.name, addr, reg);
460 }
461 }
462
463 return rc;
464}
465
466/*
467 * Perform a masked write to a PMIC register without checking the previously
468 * written value. This is needed for registers that must be rewritten even if
469 * the value hasn't changed in order for changes in other registers to take
470 * effect.
471 */
472static int pm8xxx_vreg_masked_write_forced(struct pm8xxx_vreg *vreg, u16 addr,
473 u8 val, u8 mask, u8 *reg_save)
474{
475 int rc = 0;
476 u8 reg;
477
478 reg = (*reg_save & ~mask) | (val & mask);
479 rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
480
481 if (rc) {
482 pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
483 vreg->rdesc.name, addr, rc);
484 } else {
485 *reg_save = reg;
486 vreg->write_count++;
487 if (pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_WRITES)
488 pr_info("%s: write(0x%03X)=0x%02X\n", vreg->rdesc.name,
489 addr, reg);
490 }
491
492 return rc;
493}
494
495static int pm8xxx_vreg_is_pin_controlled(struct pm8xxx_vreg *vreg)
496{
497 int ret = 0;
498
499 switch (vreg->type) {
500 case PM8XXX_REGULATOR_TYPE_PLDO:
501 case PM8XXX_REGULATOR_TYPE_NLDO:
502 ret = ((vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK) << 4)
503 | (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK);
504 break;
505 case PM8XXX_REGULATOR_TYPE_SMPS:
506 ret = vreg->sleep_ctrl_reg
507 & (SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK);
508 break;
509 case PM8XXX_REGULATOR_TYPE_VS:
510 ret = vreg->ctrl_reg & VS_PIN_CTRL_MASK;
511 break;
512 default:
513 break;
514 }
515
516 return ret;
517}
518
519/*
520 * Returns the logical pin control enable state because the pin control options
521 * present in the hardware out of restart could be different from those desired
522 * by the consumer.
523 */
524static int pm8xxx_vreg_pin_control_is_enabled(struct regulator_dev *rdev)
525{
526 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
527 int enabled;
528
529 mutex_lock(&vreg->pc_lock);
530 enabled = vreg->is_enabled_pc;
531 mutex_unlock(&vreg->pc_lock);
532
533 return enabled;
534}
535
536/* Returns the physical enable state of the regulator. */
537static int _pm8xxx_vreg_is_enabled(struct pm8xxx_vreg *vreg)
538{
539 int rc = 0;
540
541 /*
542 * All regulator types except advanced mode SMPS, FTSMPS, and VS300 have
543 * enable bit in bit 7 of the control register.
544 */
545 switch (vreg->type) {
546 case PM8XXX_REGULATOR_TYPE_FTSMPS:
547 if ((vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK)
548 != FTSMPS_VCTRL_BAND_OFF)
549 rc = 1;
550 break;
551 case PM8XXX_REGULATOR_TYPE_VS300:
552 if ((vreg->ctrl_reg & VS300_CTRL_ENABLE_MASK)
553 != VS300_CTRL_DISABLE)
554 rc = 1;
555 break;
556 case PM8XXX_REGULATOR_TYPE_SMPS:
557 if (SMPS_IN_ADVANCED_MODE(vreg)) {
558 if ((vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK)
559 != SMPS_ADVANCED_BAND_OFF)
560 rc = 1;
561 break;
562 }
563 /* Fall through for legacy mode SMPS. */
564 default:
565 if ((vreg->ctrl_reg & REGULATOR_ENABLE_MASK)
566 == REGULATOR_ENABLE)
567 rc = 1;
568 }
569
570 return rc;
571}
572
573/*
574 * Returns the logical enable state of the regulator which may be different from
575 * the physical enable state thanks to HPM/LPM pin control.
576 */
577static int pm8xxx_vreg_is_enabled(struct regulator_dev *rdev)
578{
579 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
580 int enabled;
581
582 if (vreg->type == PM8XXX_REGULATOR_TYPE_PLDO
583 || vreg->type == PM8XXX_REGULATOR_TYPE_NLDO
584 || vreg->type == PM8XXX_REGULATOR_TYPE_SMPS
585 || vreg->type == PM8XXX_REGULATOR_TYPE_VS) {
586 /* Pin controllable */
587 mutex_lock(&vreg->pc_lock);
588 enabled = vreg->is_enabled;
589 mutex_unlock(&vreg->pc_lock);
590 } else {
591 /* Not pin controlable */
592 enabled = _pm8xxx_vreg_is_enabled(vreg);
593 }
594
595 return enabled;
596}
597
598static int pm8xxx_pldo_get_voltage(struct regulator_dev *rdev)
599{
600 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
601 int vmin, fine_step;
602 u8 range_ext, range_sel, vprog, fine_step_reg;
603
604 mutex_lock(&vreg->pc_lock);
605
606 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
607 range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
608 range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
609 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
610
611 mutex_unlock(&vreg->pc_lock);
612
613 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
614
615 if (range_sel) {
616 /* low range mode */
617 fine_step = PLDO_LOW_UV_FINE_STEP;
618 vmin = PLDO_LOW_UV_MIN;
619 } else if (!range_ext) {
620 /* normal mode */
621 fine_step = PLDO_NORM_UV_FINE_STEP;
622 vmin = PLDO_NORM_UV_MIN;
623 } else {
624 /* high range mode */
625 fine_step = PLDO_HIGH_UV_FINE_STEP;
626 vmin = PLDO_HIGH_UV_MIN;
627 }
628
629 return fine_step * vprog + vmin;
630}
631
632static int pm8xxx_pldo_list_voltage(struct regulator_dev *rdev,
633 unsigned selector)
634{
635 int uV;
636
637 if (selector >= PLDO_SET_POINTS)
638 return 0;
639
640 if (selector < PLDO_LOW_SET_POINTS)
641 uV = selector * PLDO_LOW_UV_FINE_STEP + PLDO_LOW_UV_MIN;
642 else if (selector < (PLDO_LOW_SET_POINTS + PLDO_NORM_SET_POINTS))
643 uV = (selector - PLDO_LOW_SET_POINTS) * PLDO_NORM_UV_FINE_STEP
644 + PLDO_NORM_UV_MIN;
645 else
646 uV = (selector - PLDO_LOW_SET_POINTS - PLDO_NORM_SET_POINTS)
647 * PLDO_HIGH_UV_FINE_STEP
648 + PLDO_HIGH_UV_SET_POINT_MIN;
649
650 return uV;
651}
652
653static int pm8xxx_pldo_set_voltage(struct regulator_dev *rdev, int min_uV,
654 int max_uV, unsigned *selector)
655{
656 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
657 int rc = 0, uV = min_uV;
658 int vmin;
659 unsigned vprog, fine_step;
660 u8 range_ext, range_sel, fine_step_reg, prev_reg;
661 bool reg_changed = false;
662
663 if (uV < PLDO_LOW_UV_MIN && max_uV >= PLDO_LOW_UV_MIN)
664 uV = PLDO_LOW_UV_MIN;
665
666 if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX) {
667 vreg_err(vreg,
668 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
669 min_uV, max_uV, PLDO_LOW_UV_MIN, PLDO_HIGH_UV_MAX);
670 return -EINVAL;
671 }
672
673 if (uV > PLDO_NORM_UV_MAX) {
674 vmin = PLDO_HIGH_UV_MIN;
675 fine_step = PLDO_HIGH_UV_FINE_STEP;
676 range_ext = LDO_TEST_RANGE_EXT_MASK;
677 range_sel = 0;
678 } else if (uV > PLDO_LOW_UV_MAX) {
679 vmin = PLDO_NORM_UV_MIN;
680 fine_step = PLDO_NORM_UV_FINE_STEP;
681 range_ext = 0;
682 range_sel = 0;
683 } else {
684 vmin = PLDO_LOW_UV_MIN;
685 fine_step = PLDO_LOW_UV_FINE_STEP;
686 range_ext = 0;
687 range_sel = LDO_TEST_RANGE_SEL_MASK;
688 }
689
690 vprog = (uV - vmin + fine_step - 1) / fine_step;
691 uV = vprog * fine_step + vmin;
692 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
693 vprog >>= 1;
694
695 if (uV > max_uV) {
696 vreg_err(vreg,
697 "request v=[%d, %d] cannot be met by any set point\n",
698 min_uV, max_uV);
699 return -EINVAL;
700 }
701
702 mutex_lock(&vreg->pc_lock);
703
704 /* Write fine step, range select and program voltage update. */
705 prev_reg = vreg->test_reg[2];
706 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
707 fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
708 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
709 LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
710 | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
711 &vreg->test_reg[2]);
712 if (rc)
713 goto bail;
714 if (prev_reg != vreg->test_reg[2])
715 reg_changed = true;
716
717 /* Write range extension. */
718 prev_reg = vreg->test_reg[4];
719 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
720 range_ext | REGULATOR_BANK_SEL(4)
721 | REGULATOR_BANK_WRITE,
722 LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
723 &vreg->test_reg[4]);
724 if (rc)
725 goto bail;
726 if (prev_reg != vreg->test_reg[4])
727 reg_changed = true;
728
729 /* Write new voltage. */
730 if (reg_changed) {
731 /*
732 * Force a CTRL register write even if the value hasn't changed.
733 * This is neccessary because range select, range extension, and
734 * fine step will not update until a value is written into the
735 * control register.
736 */
737 rc = pm8xxx_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
738 vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
739 } else {
740 /* Only write to control register if new value is different. */
741 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
742 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
743 }
744bail:
745 mutex_unlock(&vreg->pc_lock);
746
747 if (rc)
748 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
749 else
750 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
751
752 return rc;
753}
754
755static int pm8xxx_nldo_get_voltage(struct regulator_dev *rdev)
756{
757 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
758 u8 vprog, fine_step_reg;
759
760 mutex_lock(&vreg->pc_lock);
761
762 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
763 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
764
765 mutex_unlock(&vreg->pc_lock);
766
767 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
768
769 return NLDO_UV_FINE_STEP * vprog + NLDO_UV_MIN;
770}
771
772static int pm8xxx_nldo_list_voltage(struct regulator_dev *rdev,
773 unsigned selector)
774{
775 if (selector >= NLDO_SET_POINTS)
776 return 0;
777
778 return selector * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
779}
780
781static int pm8xxx_nldo_set_voltage(struct regulator_dev *rdev, int min_uV,
782 int max_uV, unsigned *selector)
783{
784 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
785 unsigned vprog, fine_step_reg, prev_reg;
786 int rc;
787 int uV = min_uV;
788
789 if (uV < NLDO_UV_MIN && max_uV >= NLDO_UV_MIN)
790 uV = NLDO_UV_MIN;
791
792 if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX) {
793 vreg_err(vreg,
794 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
795 min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
796 return -EINVAL;
797 }
798
799 vprog = (uV - NLDO_UV_MIN + NLDO_UV_FINE_STEP - 1) / NLDO_UV_FINE_STEP;
800 uV = vprog * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
801 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
802 vprog >>= 1;
803
804 if (uV > max_uV) {
805 vreg_err(vreg,
806 "request v=[%d, %d] cannot be met by any set point\n",
807 min_uV, max_uV);
808 return -EINVAL;
809 }
810
811 mutex_lock(&vreg->pc_lock);
812
813 /* Write fine step. */
814 prev_reg = vreg->test_reg[2];
815 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
816 fine_step_reg | REGULATOR_BANK_SEL(2)
817 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
818 LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
819 | LDO_TEST_VPROG_UPDATE_MASK,
820 &vreg->test_reg[2]);
821 if (rc)
822 goto bail;
823
824 /* Write new voltage. */
825 if (prev_reg != vreg->test_reg[2]) {
826 /*
827 * Force a CTRL register write even if the value hasn't changed.
828 * This is neccessary because fine step will not update until a
829 * value is written into the control register.
830 */
831 rc = pm8xxx_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
832 vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
833 } else {
834 /* Only write to control register if new value is different. */
835 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
836 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
837 }
838bail:
839 mutex_unlock(&vreg->pc_lock);
840
841 if (rc)
842 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
843 else
844 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
845
846 return rc;
847}
848
849static int _pm8xxx_nldo1200_get_voltage(struct pm8xxx_vreg *vreg)
850{
851 int uV = 0;
852 int vprog;
853
854 if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
855 pr_warn("%s: currently in legacy mode; voltage unknown.\n",
856 vreg->rdesc.name);
857 return vreg->save_uV;
858 }
859
860 vprog = vreg->ctrl_reg & NLDO1200_CTRL_VPROG_MASK;
861
862 if ((vreg->ctrl_reg & NLDO1200_CTRL_RANGE_MASK)
863 == NLDO1200_CTRL_RANGE_LOW)
864 uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
865 else
866 uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
867
868 return uV;
869}
870
871static int pm8xxx_nldo1200_get_voltage(struct regulator_dev *rdev)
872{
873 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
874
875 return _pm8xxx_nldo1200_get_voltage(vreg);
876}
877
878static int pm8xxx_nldo1200_list_voltage(struct regulator_dev *rdev,
879 unsigned selector)
880{
881 int uV;
882
883 if (selector >= NLDO1200_SET_POINTS)
884 return 0;
885
886 if (selector < NLDO1200_LOW_SET_POINTS)
887 uV = selector * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
888 else
889 uV = (selector - NLDO1200_LOW_SET_POINTS)
890 * NLDO1200_HIGH_UV_STEP
891 + NLDO1200_HIGH_UV_MIN;
892
893 return uV;
894}
895
896static int _pm8xxx_nldo1200_set_voltage(struct pm8xxx_vreg *vreg, int min_uV,
897 int max_uV)
898{
899 u8 vprog, range;
900 int rc;
901 int uV = min_uV;
902
903 if (uV < NLDO1200_LOW_UV_MIN && max_uV >= NLDO1200_LOW_UV_MIN)
904 uV = NLDO1200_LOW_UV_MIN;
905
906 if (uV < NLDO1200_LOW_UV_MIN || uV > NLDO1200_HIGH_UV_MAX) {
907 vreg_err(vreg,
908 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
909 min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
910 return -EINVAL;
911 }
912
913 if (uV > NLDO1200_LOW_UV_MAX) {
914 vprog = (uV - NLDO1200_HIGH_UV_MIN + NLDO1200_HIGH_UV_STEP - 1)
915 / NLDO1200_HIGH_UV_STEP;
916 uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
917 vprog &= NLDO1200_CTRL_VPROG_MASK;
918 range = NLDO1200_CTRL_RANGE_HIGH;
919 } else {
920 vprog = (uV - NLDO1200_LOW_UV_MIN + NLDO1200_LOW_UV_STEP - 1)
921 / NLDO1200_LOW_UV_STEP;
922 uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
923 vprog &= NLDO1200_CTRL_VPROG_MASK;
924 range = NLDO1200_CTRL_RANGE_LOW;
925 }
926
927 if (uV > max_uV) {
928 vreg_err(vreg,
929 "request v=[%d, %d] cannot be met by any set point\n",
930 min_uV, max_uV);
931 return -EINVAL;
932 }
933
934 /* Set to advanced mode */
935 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
936 NLDO1200_ADVANCED_MODE | REGULATOR_BANK_SEL(2)
937 | REGULATOR_BANK_WRITE, NLDO1200_ADVANCED_MODE_MASK
938 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
939 if (rc)
940 goto bail;
941
942 /* Set voltage and range selection. */
943 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog | range,
944 NLDO1200_CTRL_VPROG_MASK | NLDO1200_CTRL_RANGE_MASK,
945 &vreg->ctrl_reg);
946 if (rc)
947 goto bail;
948
949 vreg->save_uV = uV;
950
951bail:
952 if (rc)
953 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
954
955 return rc;
956}
957
958static int pm8xxx_nldo1200_set_voltage(struct regulator_dev *rdev, int min_uV,
959 int max_uV, unsigned *selector)
960{
961 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
962 int rc;
963
964 rc = _pm8xxx_nldo1200_set_voltage(vreg, min_uV, max_uV);
965
966 if (!rc)
967 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
968
969 return rc;
970}
971
972static int pm8xxx_smps_get_voltage_advanced(struct pm8xxx_vreg *vreg)
973{
974 u8 vprog, band;
975 int uV = 0;
976
977 vprog = vreg->ctrl_reg & SMPS_ADVANCED_VPROG_MASK;
978 band = vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK;
979
980 if (band == SMPS_ADVANCED_BAND_1)
981 uV = vprog * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
982 else if (band == SMPS_ADVANCED_BAND_2)
983 uV = vprog * SMPS_BAND2_UV_STEP + SMPS_BAND2_UV_MIN;
984 else if (band == SMPS_ADVANCED_BAND_3)
985 uV = vprog * SMPS_BAND3_UV_STEP + SMPS_BAND3_UV_MIN;
986 else if (vreg->save_uV > 0)
987 uV = vreg->save_uV;
988 else
989 uV = VOLTAGE_UNKNOWN;
990
991 return uV;
992}
993
994static int pm8xxx_smps_get_voltage_legacy(struct pm8xxx_vreg *vreg)
995{
996 u8 vlow, vref, vprog;
997 int uV;
998
999 vlow = vreg->test_reg[1] & SMPS_LEGACY_VLOW_SEL_MASK;
1000 vref = vreg->ctrl_reg & SMPS_LEGACY_VREF_SEL_MASK;
1001 vprog = vreg->ctrl_reg & SMPS_LEGACY_VPROG_MASK;
1002
1003 if (vlow && vref) {
1004 /* mode 3 */
1005 uV = vprog * SMPS_MODE3_UV_STEP + SMPS_MODE3_UV_MIN;
1006 } else if (vref) {
1007 /* mode 2 */
1008 uV = vprog * SMPS_MODE2_UV_STEP + SMPS_MODE2_UV_MIN;
1009 } else {
1010 /* mode 1 */
1011 uV = vprog * SMPS_MODE1_UV_STEP + SMPS_MODE1_UV_MIN;
1012 }
1013
1014 return uV;
1015}
1016
1017static int _pm8xxx_smps_get_voltage(struct pm8xxx_vreg *vreg)
1018{
1019 if (SMPS_IN_ADVANCED_MODE(vreg))
1020 return pm8xxx_smps_get_voltage_advanced(vreg);
1021
1022 return pm8xxx_smps_get_voltage_legacy(vreg);
1023}
1024
1025static int pm8xxx_smps_list_voltage(struct regulator_dev *rdev,
1026 unsigned selector)
1027{
1028 int uV;
1029
1030 if (selector >= SMPS_ADVANCED_SET_POINTS)
1031 return 0;
1032
1033 if (selector < SMPS_BAND1_SET_POINTS)
1034 uV = selector * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
1035 else if (selector < (SMPS_BAND1_SET_POINTS + SMPS_BAND2_SET_POINTS))
1036 uV = (selector - SMPS_BAND1_SET_POINTS) * SMPS_BAND2_UV_STEP
1037 + SMPS_BAND2_UV_MIN;
1038 else
1039 uV = (selector - SMPS_BAND1_SET_POINTS - SMPS_BAND2_SET_POINTS)
1040 * SMPS_BAND3_UV_STEP
1041 + SMPS_BAND3_UV_MIN;
1042
1043 return uV;
1044}
1045
1046static int pm8xxx_smps_get_voltage(struct regulator_dev *rdev)
1047{
1048 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1049 int uV;
1050
1051 mutex_lock(&vreg->pc_lock);
1052 uV = _pm8xxx_smps_get_voltage(vreg);
1053 mutex_unlock(&vreg->pc_lock);
1054
1055 return uV;
1056}
1057
1058static int pm8xxx_smps_set_voltage_advanced(struct pm8xxx_vreg *vreg,
1059 int min_uV, int max_uV, int force_on)
1060{
1061 u8 vprog, band;
1062 int rc;
1063 int uV = min_uV;
1064
1065 if (uV < SMPS_BAND1_UV_MIN && max_uV >= SMPS_BAND1_UV_MIN)
1066 uV = SMPS_BAND1_UV_MIN;
1067
1068 if (uV < SMPS_BAND1_UV_MIN || uV > SMPS_BAND3_UV_MAX) {
1069 vreg_err(vreg,
1070 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1071 min_uV, max_uV, SMPS_BAND1_UV_MIN, SMPS_BAND3_UV_MAX);
1072 return -EINVAL;
1073 }
1074
1075 if (uV > SMPS_BAND2_UV_MAX) {
1076 vprog = (uV - SMPS_BAND3_UV_MIN + SMPS_BAND3_UV_STEP - 1)
1077 / SMPS_BAND3_UV_STEP;
1078 band = SMPS_ADVANCED_BAND_3;
1079 uV = SMPS_BAND3_UV_MIN + vprog * SMPS_BAND3_UV_STEP;
1080 } else if (uV > SMPS_BAND1_UV_MAX) {
1081 vprog = (uV - SMPS_BAND2_UV_MIN + SMPS_BAND2_UV_STEP - 1)
1082 / SMPS_BAND2_UV_STEP;
1083 band = SMPS_ADVANCED_BAND_2;
1084 uV = SMPS_BAND2_UV_MIN + vprog * SMPS_BAND2_UV_STEP;
1085 } else {
1086 vprog = (uV - SMPS_BAND1_UV_MIN + SMPS_BAND1_UV_STEP - 1)
1087 / SMPS_BAND1_UV_STEP;
1088 band = SMPS_ADVANCED_BAND_1;
1089 uV = SMPS_BAND1_UV_MIN + vprog * SMPS_BAND1_UV_STEP;
1090 }
1091
1092 if (uV > max_uV) {
1093 vreg_err(vreg,
1094 "request v=[%d, %d] cannot be met by any set point\n",
1095 min_uV, max_uV);
1096 return -EINVAL;
1097 }
1098
1099 /* Do not set band if regulator currently disabled. */
1100 if (!_pm8xxx_vreg_is_enabled(vreg) && !force_on)
1101 band = SMPS_ADVANCED_BAND_OFF;
1102
1103 /* Set advanced mode bit to 1. */
1104 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, SMPS_ADVANCED_MODE
1105 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
1106 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
1107 &vreg->test_reg[7]);
1108 if (rc)
1109 goto bail;
1110
1111 /* Set voltage and voltage band. */
1112 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, band | vprog,
1113 SMPS_ADVANCED_BAND_MASK | SMPS_ADVANCED_VPROG_MASK,
1114 &vreg->ctrl_reg);
1115 if (rc)
1116 goto bail;
1117
1118 vreg->save_uV = uV;
1119
1120bail:
1121 if (rc)
1122 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1123
1124 return rc;
1125}
1126
1127static int pm8xxx_smps_set_voltage_legacy(struct pm8xxx_vreg *vreg, int min_uV,
1128 int max_uV)
1129{
1130 u8 vlow, vref, vprog, pd, en;
1131 int rc;
1132 int uV = min_uV;
1133
1134 if (uV < SMPS_MODE3_UV_MIN && max_uV >= SMPS_MODE3_UV_MIN)
1135 uV = SMPS_MODE3_UV_MIN;
1136
1137 if (uV < SMPS_MODE3_UV_MIN || uV > SMPS_MODE1_UV_MAX) {
1138 vreg_err(vreg,
1139 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1140 min_uV, max_uV, SMPS_MODE3_UV_MIN, SMPS_MODE1_UV_MAX);
1141 return -EINVAL;
1142 }
1143
1144 if (uV > SMPS_MODE2_UV_MAX) {
1145 vprog = (uV - SMPS_MODE1_UV_MIN + SMPS_MODE1_UV_STEP - 1)
1146 / SMPS_MODE1_UV_STEP;
1147 vref = 0;
1148 vlow = 0;
1149 uV = SMPS_MODE1_UV_MIN + vprog * SMPS_MODE1_UV_STEP;
1150 } else if (uV > SMPS_MODE3_UV_MAX) {
1151 vprog = (uV - SMPS_MODE2_UV_MIN + SMPS_MODE2_UV_STEP - 1)
1152 / SMPS_MODE2_UV_STEP;
1153 vref = SMPS_LEGACY_VREF_SEL_MASK;
1154 vlow = 0;
1155 uV = SMPS_MODE2_UV_MIN + vprog * SMPS_MODE2_UV_STEP;
1156 } else {
1157 vprog = (uV - SMPS_MODE3_UV_MIN + SMPS_MODE3_UV_STEP - 1)
1158 / SMPS_MODE3_UV_STEP;
1159 vref = SMPS_LEGACY_VREF_SEL_MASK;
1160 vlow = SMPS_LEGACY_VLOW_SEL_MASK;
1161 uV = SMPS_MODE3_UV_MIN + vprog * SMPS_MODE3_UV_STEP;
1162 }
1163
1164 if (uV > max_uV) {
1165 vreg_err(vreg,
1166 "request v=[%d, %d] cannot be met by any set point\n",
1167 min_uV, max_uV);
1168 return -EINVAL;
1169 }
1170
1171 /* set vlow bit for ultra low voltage mode */
1172 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1173 vlow | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1),
1174 REGULATOR_BANK_MASK | SMPS_LEGACY_VLOW_SEL_MASK,
1175 &vreg->test_reg[1]);
1176 if (rc)
1177 goto bail;
1178
1179 /* Set advanced mode bit to 0. */
1180 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, SMPS_LEGACY_MODE
1181 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
1182 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
1183 &vreg->test_reg[7]);
1184 if (rc)
1185 goto bail;
1186
1187 en = (_pm8xxx_vreg_is_enabled(vreg) ? SMPS_LEGACY_ENABLE : 0);
1188 pd = (vreg->pdata.pull_down_enable ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0);
1189
1190 /* Set voltage (and the rest of the control register). */
1191 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1192 en | pd | vref | vprog,
1193 SMPS_LEGACY_ENABLE_MASK | SMPS_LEGACY_PULL_DOWN_ENABLE
1194 | SMPS_LEGACY_VREF_SEL_MASK | SMPS_LEGACY_VPROG_MASK,
1195 &vreg->ctrl_reg);
1196
1197 vreg->save_uV = uV;
1198
1199bail:
1200 if (rc)
1201 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1202
1203 return rc;
1204}
1205
1206static int pm8xxx_smps_set_voltage(struct regulator_dev *rdev, int min_uV,
1207 int max_uV, unsigned *selector)
1208{
1209 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1210 int rc = 0;
1211
1212 mutex_lock(&vreg->pc_lock);
1213
1214 if (SMPS_IN_ADVANCED_MODE(vreg) || !pm8xxx_vreg_is_pin_controlled(vreg))
1215 rc = pm8xxx_smps_set_voltage_advanced(vreg, min_uV, max_uV, 0);
1216 else
1217 rc = pm8xxx_smps_set_voltage_legacy(vreg, min_uV, max_uV);
1218
1219 mutex_unlock(&vreg->pc_lock);
1220
1221 if (!rc)
1222 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
1223
1224 return rc;
1225}
1226
1227static int _pm8xxx_ftsmps_get_voltage(struct pm8xxx_vreg *vreg)
1228{
1229 u8 vprog, band;
1230 int uV = 0;
1231
1232 if ((vreg->test_reg[0] & FTSMPS_CNFG1_PM_MASK) == FTSMPS_CNFG1_PM_PFM) {
1233 vprog = vreg->pfm_ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
1234 band = vreg->pfm_ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
1235 if (band == FTSMPS_VCTRL_BAND_OFF && vprog == 0) {
1236 /* PWM_VCTRL overrides PFM_VCTRL */
1237 vprog = vreg->ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
1238 band = vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
1239 }
1240 } else {
1241 vprog = vreg->ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
1242 band = vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
1243 }
1244
1245 if (band == FTSMPS_VCTRL_BAND_1)
1246 uV = vprog * FTSMPS_BAND1_UV_PHYS_STEP + FTSMPS_BAND1_UV_MIN;
1247 else if (band == FTSMPS_VCTRL_BAND_2)
1248 uV = vprog * FTSMPS_BAND2_UV_STEP + FTSMPS_BAND2_UV_MIN;
1249 else if (band == FTSMPS_VCTRL_BAND_3)
1250 uV = vprog * FTSMPS_BAND3_UV_STEP + FTSMPS_BAND3_UV_MIN;
1251 else if (vreg->save_uV > 0)
1252 uV = vreg->save_uV;
1253 else
1254 uV = VOLTAGE_UNKNOWN;
1255
1256 return uV;
1257}
1258
1259static int pm8xxx_ftsmps_get_voltage(struct regulator_dev *rdev)
1260{
1261 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1262
1263 return _pm8xxx_ftsmps_get_voltage(vreg);
1264}
1265
1266static int pm8xxx_ftsmps_list_voltage(struct regulator_dev *rdev,
1267 unsigned selector)
1268{
1269 int uV;
1270
1271 if (selector >= FTSMPS_SET_POINTS)
1272 return 0;
1273
1274 if (selector < FTSMPS_BAND1_SET_POINTS)
1275 uV = selector * FTSMPS_BAND1_UV_LOG_STEP + FTSMPS_BAND1_UV_MIN;
1276 else if (selector < (FTSMPS_BAND1_SET_POINTS + FTSMPS_BAND2_SET_POINTS))
1277 uV = (selector - FTSMPS_BAND1_SET_POINTS) * FTSMPS_BAND2_UV_STEP
1278 + FTSMPS_BAND2_UV_MIN;
1279 else
1280 uV = (selector - FTSMPS_BAND1_SET_POINTS
1281 - FTSMPS_BAND2_SET_POINTS)
1282 * FTSMPS_BAND3_UV_STEP
1283 + FTSMPS_BAND3_UV_SET_POINT_MIN;
1284
1285 return uV;
1286}
1287
1288static int _pm8xxx_ftsmps_set_voltage(struct pm8xxx_vreg *vreg, int min_uV,
1289 int max_uV, int force_on)
1290{
1291 int rc = 0;
1292 u8 vprog, band;
1293 int uV = min_uV;
1294
1295 if (uV < FTSMPS_BAND1_UV_MIN && max_uV >= FTSMPS_BAND1_UV_MIN)
1296 uV = FTSMPS_BAND1_UV_MIN;
1297
1298 if (uV < FTSMPS_BAND1_UV_MIN || uV > FTSMPS_BAND3_UV_MAX) {
1299 vreg_err(vreg,
1300 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1301 min_uV, max_uV, FTSMPS_BAND1_UV_MIN,
1302 FTSMPS_BAND3_UV_MAX);
1303 return -EINVAL;
1304 }
1305
1306 /* Round up for set points in the gaps between bands. */
1307 if (uV > FTSMPS_BAND1_UV_MAX && uV < FTSMPS_BAND2_UV_MIN)
1308 uV = FTSMPS_BAND2_UV_MIN;
1309 else if (uV > FTSMPS_BAND2_UV_MAX
1310 && uV < FTSMPS_BAND3_UV_SET_POINT_MIN)
1311 uV = FTSMPS_BAND3_UV_SET_POINT_MIN;
1312
1313 if (uV > FTSMPS_BAND2_UV_MAX) {
1314 vprog = (uV - FTSMPS_BAND3_UV_MIN + FTSMPS_BAND3_UV_STEP - 1)
1315 / FTSMPS_BAND3_UV_STEP;
1316 band = FTSMPS_VCTRL_BAND_3;
1317 uV = FTSMPS_BAND3_UV_MIN + vprog * FTSMPS_BAND3_UV_STEP;
1318 } else if (uV > FTSMPS_BAND1_UV_MAX) {
1319 vprog = (uV - FTSMPS_BAND2_UV_MIN + FTSMPS_BAND2_UV_STEP - 1)
1320 / FTSMPS_BAND2_UV_STEP;
1321 band = FTSMPS_VCTRL_BAND_2;
1322 uV = FTSMPS_BAND2_UV_MIN + vprog * FTSMPS_BAND2_UV_STEP;
1323 } else {
1324 vprog = (uV - FTSMPS_BAND1_UV_MIN
1325 + FTSMPS_BAND1_UV_LOG_STEP - 1)
1326 / FTSMPS_BAND1_UV_LOG_STEP;
1327 uV = FTSMPS_BAND1_UV_MIN + vprog * FTSMPS_BAND1_UV_LOG_STEP;
1328 vprog *= FTSMPS_BAND1_UV_LOG_STEP / FTSMPS_BAND1_UV_PHYS_STEP;
1329 band = FTSMPS_VCTRL_BAND_1;
1330 }
1331
1332 if (uV > max_uV) {
1333 vreg_err(vreg,
1334 "request v=[%d, %d] cannot be met by any set point\n",
1335 min_uV, max_uV);
1336 return -EINVAL;
1337 }
1338
1339 /*
1340 * Do not set voltage if regulator is currently disabled because doing
1341 * so will enable it.
1342 */
1343 if (_pm8xxx_vreg_is_enabled(vreg) || force_on) {
1344 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1345 band | vprog,
1346 FTSMPS_VCTRL_BAND_MASK | FTSMPS_VCTRL_VPROG_MASK,
1347 &vreg->ctrl_reg);
1348 if (rc)
1349 goto bail;
1350
1351 /* Program PFM_VCTRL as 0x00 so that PWM_VCTRL overrides it. */
1352 rc = pm8xxx_vreg_masked_write(vreg, vreg->pfm_ctrl_addr, 0x00,
1353 FTSMPS_VCTRL_BAND_MASK | FTSMPS_VCTRL_VPROG_MASK,
1354 &vreg->pfm_ctrl_reg);
1355 if (rc)
1356 goto bail;
1357 }
1358
1359 vreg->save_uV = uV;
1360
1361bail:
1362 if (rc)
1363 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1364
1365 return rc;
1366}
1367
1368static int pm8xxx_ftsmps_set_voltage(struct regulator_dev *rdev, int min_uV,
1369 int max_uV, unsigned *selector)
1370{
1371 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1372 int rc;
1373
1374 rc = _pm8xxx_ftsmps_set_voltage(vreg, min_uV, max_uV, 0);
1375
1376 if (!rc)
1377 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
1378
1379 return rc;
1380}
1381
1382static int pm8xxx_ncp_get_voltage(struct regulator_dev *rdev)
1383{
1384 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1385 u8 vprog;
1386
1387 vprog = vreg->ctrl_reg & NCP_VPROG_MASK;
1388
1389 return NCP_UV_MIN + vprog * NCP_UV_STEP;
1390}
1391
1392static int pm8xxx_ncp_list_voltage(struct regulator_dev *rdev,
1393 unsigned selector)
1394{
1395 if (selector >= NCP_SET_POINTS)
1396 return 0;
1397
1398 return selector * NCP_UV_STEP + NCP_UV_MIN;
1399}
1400
1401static int pm8xxx_ncp_set_voltage(struct regulator_dev *rdev, int min_uV,
1402 int max_uV, unsigned *selector)
1403{
1404 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1405 int rc;
1406 int uV = min_uV;
1407 u8 val;
1408
1409 if (uV < NCP_UV_MIN && max_uV >= NCP_UV_MIN)
1410 uV = NCP_UV_MIN;
1411
1412 if (uV < NCP_UV_MIN || uV > NCP_UV_MAX) {
1413 vreg_err(vreg,
1414 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1415 min_uV, max_uV, NCP_UV_MIN, NCP_UV_MAX);
1416 return -EINVAL;
1417 }
1418
1419 val = (uV - NCP_UV_MIN + NCP_UV_STEP - 1) / NCP_UV_STEP;
1420 uV = val * NCP_UV_STEP + NCP_UV_MIN;
1421
1422 if (uV > max_uV) {
1423 vreg_err(vreg,
1424 "request v=[%d, %d] cannot be met by any set point\n",
1425 min_uV, max_uV);
1426 return -EINVAL;
1427 }
1428
1429 /* voltage setting */
1430 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
1431 NCP_VPROG_MASK, &vreg->ctrl_reg);
1432 if (rc)
1433 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1434 else
1435 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
1436
1437 return rc;
1438}
1439
David Collins525da182012-02-13 10:36:37 -08001440static int pm8xxx_boost_get_voltage(struct regulator_dev *rdev)
1441{
1442 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1443 u8 vprog;
1444
1445 vprog = vreg->ctrl_reg & BOOST_VPROG_MASK;
1446
1447 return BOOST_UV_STEP * vprog + BOOST_UV_MIN;
1448}
1449
1450static int pm8xxx_boost_list_voltage(struct regulator_dev *rdev,
1451 unsigned selector)
1452{
1453 if (selector >= BOOST_SET_POINTS)
1454 return 0;
1455
1456 return selector * BOOST_UV_STEP + BOOST_UV_MIN;
1457}
1458
1459static int pm8xxx_boost_set_voltage(struct regulator_dev *rdev, int min_uV,
1460 int max_uV, unsigned *selector)
1461{
1462 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1463 int rc;
1464 int uV = min_uV;
1465 u8 val;
1466
1467 if (uV < BOOST_UV_MIN && max_uV >= BOOST_UV_MIN)
1468 uV = BOOST_UV_MIN;
1469
1470 if (uV < BOOST_UV_MIN || uV > BOOST_UV_MAX) {
1471 vreg_err(vreg,
1472 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1473 min_uV, max_uV, BOOST_UV_MIN, BOOST_UV_MAX);
1474 return -EINVAL;
1475 }
1476
1477 val = (uV - BOOST_UV_MIN + BOOST_UV_STEP - 1) / BOOST_UV_STEP;
1478 uV = val * BOOST_UV_STEP + BOOST_UV_MIN;
1479
1480 if (uV > max_uV) {
1481 vreg_err(vreg,
1482 "request v=[%d, %d] cannot be met by any set point\n",
1483 min_uV, max_uV);
1484 return -EINVAL;
1485 }
1486
1487 /* voltage setting */
1488 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
1489 BOOST_VPROG_MASK, &vreg->ctrl_reg);
1490 if (rc)
1491 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1492 else
1493 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
1494
1495 return rc;
1496}
1497
David Collins5c4a5ab2011-12-02 15:01:26 -08001498static unsigned int pm8xxx_ldo_get_mode(struct regulator_dev *rdev)
1499{
1500 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1501 unsigned int mode = 0;
1502
1503 mutex_lock(&vreg->pc_lock);
1504 mode = vreg->mode;
1505 mutex_unlock(&vreg->pc_lock);
1506
1507 return mode;
1508}
1509
1510static int pm8xxx_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
1511{
1512 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1513 int rc = 0;
1514
1515 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1516 vreg_err(vreg, "invalid mode: %u\n", mode);
1517 return -EINVAL;
1518 }
1519
1520 mutex_lock(&vreg->pc_lock);
1521
1522 if (mode == REGULATOR_MODE_NORMAL
1523 || (vreg->is_enabled_pc
1524 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE)) {
1525 /* HPM */
1526 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1527 LDO_CTRL_PM_HPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1528 } else {
1529 /* LPM */
1530 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1531 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1532 if (rc)
1533 goto bail;
1534
1535 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1536 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1537 | REGULATOR_BANK_SEL(0),
1538 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
1539 &vreg->test_reg[0]);
1540 }
1541
1542bail:
1543 if (!rc)
1544 vreg->mode = mode;
1545
1546 mutex_unlock(&vreg->pc_lock);
1547
1548 if (rc)
1549 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1550 else
1551 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
1552
1553 return rc;
1554}
1555
1556static unsigned int pm8xxx_nldo1200_get_mode(struct regulator_dev *rdev)
1557{
1558 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1559 unsigned int mode = 0;
1560
1561 if (NLDO1200_IN_ADVANCED_MODE(vreg)) {
1562 /* Advanced mode */
1563 if ((vreg->test_reg[2] & NLDO1200_ADVANCED_PM_MASK)
1564 == NLDO1200_ADVANCED_PM_LPM)
1565 mode = REGULATOR_MODE_IDLE;
1566 else
1567 mode = REGULATOR_MODE_NORMAL;
1568 } else {
1569 /* Legacy mode */
1570 if ((vreg->ctrl_reg & NLDO1200_LEGACY_PM_MASK)
1571 == NLDO1200_LEGACY_PM_LPM)
1572 mode = REGULATOR_MODE_IDLE;
1573 else
1574 mode = REGULATOR_MODE_NORMAL;
1575 }
1576
1577 return mode;
1578}
1579
1580static int pm8xxx_nldo1200_set_mode(struct regulator_dev *rdev,
1581 unsigned int mode)
1582{
1583 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1584 int rc = 0;
1585
1586 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1587 vreg_err(vreg, "invalid mode: %u\n", mode);
1588 return -EINVAL;
1589 }
1590
1591 /*
1592 * Make sure that advanced mode is in use. If it isn't, then set it
1593 * and update the voltage accordingly.
1594 */
1595 if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
1596 rc = _pm8xxx_nldo1200_set_voltage(vreg, vreg->save_uV,
1597 vreg->save_uV);
1598 if (rc)
1599 goto bail;
1600 }
1601
1602 if (mode == REGULATOR_MODE_NORMAL) {
1603 /* HPM */
1604 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1605 NLDO1200_ADVANCED_PM_HPM | REGULATOR_BANK_WRITE
1606 | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
1607 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
1608 } else {
1609 /* LPM */
1610 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1611 NLDO1200_ADVANCED_PM_LPM | REGULATOR_BANK_WRITE
1612 | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
1613 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
1614 }
1615
1616bail:
1617 if (rc)
1618 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1619 else
1620 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
1621
1622 return rc;
1623}
1624
1625static unsigned int pm8xxx_smps_get_mode(struct regulator_dev *rdev)
1626{
1627 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1628 unsigned int mode = 0;
1629
1630 mutex_lock(&vreg->pc_lock);
1631 mode = vreg->mode;
1632 mutex_unlock(&vreg->pc_lock);
1633
1634 return mode;
1635}
1636
1637static int pm8xxx_smps_set_mode(struct regulator_dev *rdev, unsigned int mode)
1638{
1639 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1640 int rc = 0;
1641
1642 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1643 vreg_err(vreg, "invalid mode: %u\n", mode);
1644 return -EINVAL;
1645 }
1646
1647 mutex_lock(&vreg->pc_lock);
1648
1649 if (mode == REGULATOR_MODE_NORMAL
1650 || (vreg->is_enabled_pc
1651 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE)) {
1652 /* HPM */
1653 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1654 SMPS_CLK_CTRL_PWM, SMPS_CLK_CTRL_MASK,
1655 &vreg->clk_ctrl_reg);
1656 } else {
1657 /* LPM */
1658 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1659 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
1660 &vreg->clk_ctrl_reg);
1661 }
1662
1663 if (!rc)
1664 vreg->mode = mode;
1665
1666 mutex_unlock(&vreg->pc_lock);
1667
1668 if (rc)
1669 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1670 else
1671 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
1672
1673 return rc;
1674}
1675
1676static unsigned int pm8xxx_ftsmps_get_mode(struct regulator_dev *rdev)
1677{
1678 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1679 unsigned int mode = 0;
1680
1681 if ((vreg->test_reg[0] & FTSMPS_CNFG1_PM_MASK) == FTSMPS_CNFG1_PM_PFM)
1682 mode = REGULATOR_MODE_IDLE;
1683 else
1684 mode = REGULATOR_MODE_NORMAL;
1685
1686 return mode;
1687}
1688
1689static int pm8xxx_ftsmps_set_mode(struct regulator_dev *rdev, unsigned int mode)
1690{
1691 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1692 int rc = 0;
1693
1694 if (mode == REGULATOR_MODE_NORMAL) {
1695 /* HPM */
1696 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1697 FTSMPS_CNFG1_PM_PWM | REGULATOR_BANK_WRITE
1698 | REGULATOR_BANK_SEL(0), FTSMPS_CNFG1_PM_MASK
1699 | REGULATOR_BANK_MASK, &vreg->test_reg[0]);
1700 } else if (mode == REGULATOR_MODE_IDLE) {
1701 /* LPM */
1702 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1703 FTSMPS_CNFG1_PM_PFM | REGULATOR_BANK_WRITE
1704 | REGULATOR_BANK_SEL(0), FTSMPS_CNFG1_PM_MASK
1705 | REGULATOR_BANK_MASK, &vreg->test_reg[0]);
1706 } else {
1707 vreg_err(vreg, "invalid mode: %u\n", mode);
1708 return -EINVAL;
1709 }
1710
1711 if (rc)
1712 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1713 else
1714 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
1715
1716 return rc;
1717}
1718
1719static unsigned int pm8xxx_vreg_get_optimum_mode(struct regulator_dev *rdev,
1720 int input_uV, int output_uV, int load_uA)
1721{
1722 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1723 unsigned int mode;
1724
1725 if (load_uA + vreg->pdata.system_uA >= vreg->hpm_min_load)
1726 mode = REGULATOR_MODE_NORMAL;
1727 else
1728 mode = REGULATOR_MODE_IDLE;
1729
1730 return mode;
1731}
1732
1733static int pm8xxx_ldo_enable(struct regulator_dev *rdev)
1734{
1735 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1736 int rc, val;
1737
1738 mutex_lock(&vreg->pc_lock);
1739
1740 /*
1741 * Choose HPM if previously set to HPM or if pin control is enabled in
1742 * on/off mode.
1743 */
1744 val = LDO_CTRL_PM_LPM;
1745 if (vreg->mode == REGULATOR_MODE_NORMAL
1746 || (vreg->is_enabled_pc
1747 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE))
1748 val = LDO_CTRL_PM_HPM;
1749
1750 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val | LDO_ENABLE,
1751 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1752
1753 if (!rc)
1754 vreg->is_enabled = true;
1755
1756 mutex_unlock(&vreg->pc_lock);
1757
1758 if (rc)
1759 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1760 else
1761 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
1762
1763 return rc;
1764}
1765
1766static int pm8xxx_ldo_disable(struct regulator_dev *rdev)
1767{
1768 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1769 int rc;
1770
1771 mutex_lock(&vreg->pc_lock);
1772
1773 /*
1774 * Only disable the regulator if it isn't still required for HPM/LPM
1775 * pin control.
1776 */
1777 if (!vreg->is_enabled_pc
1778 || vreg->pdata.pin_fn != PM8XXX_VREG_PIN_FN_MODE) {
1779 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1780 LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
1781 if (rc)
1782 goto bail;
1783 }
1784
1785 /* Change to LPM if HPM/LPM pin control is enabled. */
1786 if (vreg->is_enabled_pc
1787 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE) {
1788 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1789 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1790 if (rc)
1791 goto bail;
1792
1793 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1794 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1795 | REGULATOR_BANK_SEL(0),
1796 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
1797 &vreg->test_reg[0]);
1798 }
1799
1800 if (!rc)
1801 vreg->is_enabled = false;
1802bail:
1803 mutex_unlock(&vreg->pc_lock);
1804
1805 if (rc)
1806 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1807 else
1808 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
1809
1810 return rc;
1811}
1812
1813static int pm8xxx_nldo1200_enable(struct regulator_dev *rdev)
1814{
1815 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1816 int rc;
1817
1818 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_ENABLE,
1819 NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
1820
1821 if (rc)
1822 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1823 else
1824 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
1825
1826 return rc;
1827}
1828
1829static int pm8xxx_nldo1200_disable(struct regulator_dev *rdev)
1830{
1831 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1832 int rc;
1833
1834 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_DISABLE,
1835 NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
1836
1837 if (rc)
1838 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1839 else
1840 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
1841
1842 return rc;
1843}
1844
1845static int pm8xxx_smps_enable(struct regulator_dev *rdev)
1846{
1847 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1848 int rc = 0;
1849 int val;
1850
1851 mutex_lock(&vreg->pc_lock);
1852
1853 if (SMPS_IN_ADVANCED_MODE(vreg)
1854 || !pm8xxx_vreg_is_pin_controlled(vreg)) {
1855 /* Enable in advanced mode if not using pin control. */
1856 rc = pm8xxx_smps_set_voltage_advanced(vreg, vreg->save_uV,
1857 vreg->save_uV, 1);
1858 } else {
1859 rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV,
1860 vreg->save_uV);
1861 if (rc)
1862 goto bail;
1863
1864 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1865 SMPS_LEGACY_ENABLE, SMPS_LEGACY_ENABLE_MASK,
1866 &vreg->ctrl_reg);
1867 }
1868
1869 /*
1870 * Choose HPM if previously set to HPM or if pin control is enabled in
1871 * on/off mode.
1872 */
1873 val = SMPS_CLK_CTRL_PFM;
1874 if (vreg->mode == REGULATOR_MODE_NORMAL
1875 || (vreg->is_enabled_pc
1876 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE))
1877 val = SMPS_CLK_CTRL_PWM;
1878
1879 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr, val,
1880 SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
1881
1882 if (!rc)
1883 vreg->is_enabled = true;
1884bail:
1885 mutex_unlock(&vreg->pc_lock);
1886
1887 if (rc)
1888 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1889 else
1890 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
1891
1892 return rc;
1893}
1894
1895static int pm8xxx_smps_disable(struct regulator_dev *rdev)
1896{
1897 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1898 int rc;
1899
1900 mutex_lock(&vreg->pc_lock);
1901
1902 if (SMPS_IN_ADVANCED_MODE(vreg)) {
1903 /* Change SMPS to legacy mode before disabling. */
1904 rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV,
1905 vreg->save_uV);
1906 if (rc)
1907 goto bail;
1908 }
1909
1910 /*
1911 * Only disable the regulator if it isn't still required for HPM/LPM
1912 * pin control.
1913 */
1914 if (!vreg->is_enabled_pc
1915 || vreg->pdata.pin_fn != PM8XXX_VREG_PIN_FN_MODE) {
1916 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1917 SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
1918 &vreg->ctrl_reg);
1919 if (rc)
1920 goto bail;
1921 }
1922
1923 /* Change to LPM if HPM/LPM pin control is enabled. */
1924 if (vreg->is_enabled_pc
1925 && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE)
1926 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1927 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
1928 &vreg->clk_ctrl_reg);
1929
1930 if (!rc)
1931 vreg->is_enabled = false;
1932
1933bail:
1934 mutex_unlock(&vreg->pc_lock);
1935
1936 if (rc)
1937 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1938 else
1939 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
1940
1941 return rc;
1942}
1943
1944static int pm8xxx_ftsmps_enable(struct regulator_dev *rdev)
1945{
1946 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1947 int rc;
1948
1949 rc = _pm8xxx_ftsmps_set_voltage(vreg, vreg->save_uV, vreg->save_uV, 1);
1950
1951 if (rc)
1952 vreg_err(vreg, "set voltage failed, rc=%d\n", rc);
1953 else
1954 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
1955
1956 return rc;
1957}
1958
1959static int pm8xxx_ftsmps_disable(struct regulator_dev *rdev)
1960{
1961 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1962 int rc;
1963
1964 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1965 FTSMPS_VCTRL_BAND_OFF, FTSMPS_VCTRL_BAND_MASK, &vreg->ctrl_reg);
1966 if (rc)
1967 goto bail;
1968
1969 rc = pm8xxx_vreg_masked_write(vreg, vreg->pfm_ctrl_addr,
1970 FTSMPS_VCTRL_BAND_OFF, FTSMPS_VCTRL_BAND_MASK,
1971 &vreg->pfm_ctrl_reg);
1972bail:
1973 if (rc)
1974 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
1975 else
1976 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
1977
1978 return rc;
1979}
1980
1981static int pm8xxx_vs_enable(struct regulator_dev *rdev)
1982{
1983 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
1984 int rc;
1985
1986 mutex_lock(&vreg->pc_lock);
1987
David Collins0c19fa82012-01-30 11:23:38 -08001988 if (vreg->pdata.ocp_enable) {
1989 /* Disable OCP. */
1990 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
1991 VS_OCP_DISABLE, VS_OCP_MASK, &vreg->test_reg[0]);
1992 if (rc)
1993 goto done;
David Collins5c4a5ab2011-12-02 15:01:26 -08001994
David Collins0c19fa82012-01-30 11:23:38 -08001995 /* Enable the switch while OCP is disabled. */
1996 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
1997 VS_ENABLE | VS_MODE_NORMAL,
1998 VS_ENABLE_MASK | VS_MODE_MASK,
1999 &vreg->ctrl_reg);
2000 if (rc)
2001 goto done;
2002
2003 /* Wait for inrush current to subside, then enable OCP. */
2004 udelay(vreg->pdata.ocp_enable_time);
2005 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2006 VS_OCP_ENABLE, VS_OCP_MASK, &vreg->test_reg[0]);
2007 } else {
2008 /* Enable the switch without touching OCP. */
2009 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS_ENABLE,
2010 VS_ENABLE_MASK, &vreg->ctrl_reg);
2011 }
2012
2013done:
David Collins5c4a5ab2011-12-02 15:01:26 -08002014 if (!rc)
2015 vreg->is_enabled = true;
2016
2017 mutex_unlock(&vreg->pc_lock);
2018
2019 if (rc)
2020 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2021 else
2022 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
2023
2024 return rc;
2025}
2026
2027static int pm8xxx_vs_disable(struct regulator_dev *rdev)
2028{
2029 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2030 int rc;
2031
2032 mutex_lock(&vreg->pc_lock);
2033
2034 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS_DISABLE,
2035 VS_ENABLE_MASK, &vreg->ctrl_reg);
2036
2037 if (!rc)
2038 vreg->is_enabled = false;
2039
2040 mutex_unlock(&vreg->pc_lock);
2041
2042 if (rc)
2043 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2044 else
2045 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
2046
2047 return rc;
2048}
2049
2050static int pm8xxx_vs300_enable(struct regulator_dev *rdev)
2051{
2052 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2053 int rc;
2054
David Collins0c19fa82012-01-30 11:23:38 -08002055 if (vreg->pdata.ocp_enable) {
2056 /* Disable OCP. */
2057 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2058 VS_OCP_DISABLE, VS_OCP_MASK, &vreg->test_reg[0]);
2059 if (rc)
2060 goto done;
David Collins5c4a5ab2011-12-02 15:01:26 -08002061
David Collins0c19fa82012-01-30 11:23:38 -08002062 /* Enable the switch while OCP is disabled. */
2063 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2064 VS300_CTRL_ENABLE | VS300_MODE_NORMAL,
2065 VS300_CTRL_ENABLE_MASK | VS300_MODE_MASK,
2066 &vreg->ctrl_reg);
2067 if (rc)
2068 goto done;
2069
2070 /* Wait for inrush current to subside, then enable OCP. */
2071 udelay(vreg->pdata.ocp_enable_time);
2072 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2073 VS_OCP_ENABLE, VS_OCP_MASK, &vreg->test_reg[0]);
2074 } else {
2075 /* Enable the regulator without touching OCP. */
2076 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2077 VS300_CTRL_ENABLE, VS300_CTRL_ENABLE_MASK,
2078 &vreg->ctrl_reg);
2079 }
2080
2081done:
2082 if (rc) {
David Collins5c4a5ab2011-12-02 15:01:26 -08002083 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
David Collins0c19fa82012-01-30 11:23:38 -08002084 } else {
2085 vreg->is_enabled = true;
David Collins5c4a5ab2011-12-02 15:01:26 -08002086 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
David Collins0c19fa82012-01-30 11:23:38 -08002087 }
David Collins5c4a5ab2011-12-02 15:01:26 -08002088
2089 return rc;
2090}
2091
2092static int pm8xxx_vs300_disable(struct regulator_dev *rdev)
2093{
2094 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2095 int rc;
2096
2097 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS300_CTRL_DISABLE,
2098 VS300_CTRL_ENABLE_MASK, &vreg->ctrl_reg);
2099
2100 if (rc)
2101 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2102 else
2103 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
2104
2105 return rc;
2106}
2107
2108static int pm8xxx_ncp_enable(struct regulator_dev *rdev)
2109{
2110 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2111 int rc;
2112
2113 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NCP_ENABLE,
2114 NCP_ENABLE_MASK, &vreg->ctrl_reg);
2115
2116 if (rc)
2117 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2118 else
2119 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
2120
2121 return rc;
2122}
2123
2124static int pm8xxx_ncp_disable(struct regulator_dev *rdev)
2125{
2126 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2127 int rc;
2128
2129 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NCP_DISABLE,
2130 NCP_ENABLE_MASK, &vreg->ctrl_reg);
2131
2132 if (rc)
2133 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2134 else
2135 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
2136
2137 return rc;
2138}
2139
David Collins525da182012-02-13 10:36:37 -08002140static int pm8xxx_boost_enable(struct regulator_dev *rdev)
2141{
2142 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2143 int rc;
2144
2145 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, BOOST_ENABLE,
2146 BOOST_ENABLE_MASK, &vreg->ctrl_reg);
2147
2148 if (rc)
2149 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2150 else
2151 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
2152
2153 return rc;
2154}
2155
2156static int pm8xxx_boost_disable(struct regulator_dev *rdev)
2157{
2158 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2159 int rc;
2160
2161 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, BOOST_DISABLE,
2162 BOOST_ENABLE_MASK, &vreg->ctrl_reg);
2163
2164 if (rc)
2165 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2166 else
2167 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
2168
2169 return rc;
2170}
2171
David Collins5c4a5ab2011-12-02 15:01:26 -08002172static int pm8xxx_ldo_pin_control_enable(struct regulator_dev *rdev)
2173{
2174 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2175 int rc = 0;
2176 int bank;
2177 u8 val = 0;
2178 u8 mask;
2179
2180 mutex_lock(&vreg->pc_lock);
2181
2182 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
2183 val |= LDO_TEST_PIN_CTRL_EN0;
2184 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
2185 val |= LDO_TEST_PIN_CTRL_EN1;
2186 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
2187 val |= LDO_TEST_PIN_CTRL_EN2;
2188 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
2189 val |= LDO_TEST_PIN_CTRL_EN3;
2190
2191 bank = (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE ? 5 : 6);
2192 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2193 val | REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
2194 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
2195 &vreg->test_reg[bank]);
2196 if (rc)
2197 goto bail;
2198
2199 /* Unset pin control bits in unused bank. */
2200 bank = (bank == 5 ? 6 : 5);
2201 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2202 REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
2203 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
2204 &vreg->test_reg[bank]);
2205 if (rc)
2206 goto bail;
2207
2208 val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
2209 | REGULATOR_BANK_SEL(0);
2210 mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
2211 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, val, mask,
2212 &vreg->test_reg[0]);
2213 if (rc)
2214 goto bail;
2215
2216 if (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE) {
2217 /* Pin control ON/OFF */
2218 val = LDO_CTRL_PM_HPM;
2219 /* Leave physically enabled if already enabled. */
2220 val |= (vreg->is_enabled ? LDO_ENABLE : LDO_DISABLE);
2221 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
2222 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
2223 if (rc)
2224 goto bail;
2225 } else {
2226 /* Pin control LPM/HPM */
2227 val = LDO_ENABLE;
2228 /* Leave in HPM if already enabled in HPM. */
2229 val |= (vreg->is_enabled && vreg->mode == REGULATOR_MODE_NORMAL
2230 ? LDO_CTRL_PM_HPM : LDO_CTRL_PM_LPM);
2231 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
2232 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
2233 if (rc)
2234 goto bail;
2235 }
2236
2237bail:
2238 if (!rc)
2239 vreg->is_enabled_pc = true;
2240
2241 mutex_unlock(&vreg->pc_lock);
2242
2243 if (rc)
2244 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2245 else
2246 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2247
2248 return rc;
2249}
2250
2251static int pm8xxx_ldo_pin_control_disable(struct regulator_dev *rdev)
2252{
2253 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2254 int rc;
2255
2256 mutex_lock(&vreg->pc_lock);
2257
2258 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2259 REGULATOR_BANK_SEL(5) | REGULATOR_BANK_WRITE,
2260 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
2261 &vreg->test_reg[5]);
2262 if (rc)
2263 goto bail;
2264
2265 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2266 REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
2267 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
2268 &vreg->test_reg[6]);
2269
2270 /*
2271 * Physically disable the regulator if it was enabled in HPM/LPM pin
2272 * control mode previously and it logically should not be enabled.
2273 */
2274 if ((vreg->ctrl_reg & LDO_ENABLE_MASK) == LDO_ENABLE
2275 && !vreg->is_enabled) {
2276 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2277 LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
2278 if (rc)
2279 goto bail;
2280 }
2281
2282 /* Change to LPM if LPM was enabled. */
2283 if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
2284 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2285 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
2286 if (rc)
2287 goto bail;
2288
2289 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2290 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
2291 | REGULATOR_BANK_SEL(0),
2292 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
2293 &vreg->test_reg[0]);
2294 if (rc)
2295 goto bail;
2296 }
2297
2298bail:
2299 if (!rc)
2300 vreg->is_enabled_pc = false;
2301
2302 mutex_unlock(&vreg->pc_lock);
2303
2304 if (rc)
2305 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2306 else
2307 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2308
2309 return rc;
2310}
2311
2312static int pm8xxx_smps_pin_control_enable(struct regulator_dev *rdev)
2313{
2314 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2315 int rc = 0;
2316 u8 val = 0;
2317
2318 mutex_lock(&vreg->pc_lock);
2319
2320 if (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE) {
2321 /* Pin control ON/OFF */
2322 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
2323 val |= SMPS_PIN_CTRL_EN0;
2324 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
2325 val |= SMPS_PIN_CTRL_EN1;
2326 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
2327 val |= SMPS_PIN_CTRL_EN2;
2328 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
2329 val |= SMPS_PIN_CTRL_EN3;
2330 } else {
2331 /* Pin control LPM/HPM */
2332 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
2333 val |= SMPS_PIN_CTRL_LPM_EN0;
2334 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
2335 val |= SMPS_PIN_CTRL_LPM_EN1;
2336 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
2337 val |= SMPS_PIN_CTRL_LPM_EN2;
2338 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
2339 val |= SMPS_PIN_CTRL_LPM_EN3;
2340 }
2341
2342 rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV, vreg->save_uV);
2343 if (rc)
2344 goto bail;
2345
2346 rc = pm8xxx_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, val,
2347 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
2348 &vreg->sleep_ctrl_reg);
2349 if (rc)
2350 goto bail;
2351
2352 /*
2353 * Physically enable the regulator if using HPM/LPM pin control mode or
2354 * if the regulator should be logically left on.
2355 */
2356 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2357 ((vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE
2358 || vreg->is_enabled) ?
2359 SMPS_LEGACY_ENABLE : SMPS_LEGACY_DISABLE),
2360 SMPS_LEGACY_ENABLE_MASK, &vreg->ctrl_reg);
2361 if (rc)
2362 goto bail;
2363
2364 /*
2365 * Set regulator to HPM if using on/off pin control or if the regulator
2366 * is already enabled in HPM. Otherwise, set it to LPM.
2367 */
2368 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
2369 (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE
2370 || (vreg->is_enabled
2371 && vreg->mode == REGULATOR_MODE_NORMAL)
2372 ? SMPS_CLK_CTRL_PWM : SMPS_CLK_CTRL_PFM),
2373 SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
2374
2375bail:
2376 if (!rc)
2377 vreg->is_enabled_pc = true;
2378
2379 mutex_unlock(&vreg->pc_lock);
2380
2381 if (rc)
2382 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2383 else
2384 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2385
2386 return rc;
2387}
2388
2389static int pm8xxx_smps_pin_control_disable(struct regulator_dev *rdev)
2390{
2391 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2392 int rc;
2393
2394 mutex_lock(&vreg->pc_lock);
2395
2396 rc = pm8xxx_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, 0,
2397 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
2398 &vreg->sleep_ctrl_reg);
2399 if (rc)
2400 goto bail;
2401
2402 /*
2403 * Physically disable the regulator if it was enabled in HPM/LPM pin
2404 * control mode previously and it logically should not be enabled.
2405 */
2406 if ((vreg->ctrl_reg & SMPS_LEGACY_ENABLE_MASK) == SMPS_LEGACY_ENABLE
2407 && vreg->is_enabled == false) {
2408 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2409 SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
2410 &vreg->ctrl_reg);
2411 if (rc)
2412 goto bail;
2413 }
2414
2415 /* Change to LPM if LPM was enabled. */
2416 if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
2417 rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
2418 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
2419 &vreg->clk_ctrl_reg);
2420 if (rc)
2421 goto bail;
2422 }
2423
2424 rc = pm8xxx_smps_set_voltage_advanced(vreg, vreg->save_uV,
2425 vreg->save_uV, 0);
2426
2427bail:
2428 if (!rc)
2429 vreg->is_enabled_pc = false;
2430
2431 mutex_unlock(&vreg->pc_lock);
2432
2433 if (rc)
2434 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2435 else
2436 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2437
2438 return rc;
2439}
2440
2441static int pm8xxx_vs_pin_control_enable(struct regulator_dev *rdev)
2442{
2443 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2444 int rc;
2445 u8 val = 0;
2446
2447 mutex_lock(&vreg->pc_lock);
2448
2449 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
2450 val |= VS_PIN_CTRL_EN0;
2451 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
2452 val |= VS_PIN_CTRL_EN1;
2453 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
2454 val |= VS_PIN_CTRL_EN2;
2455 if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
2456 val |= VS_PIN_CTRL_EN3;
2457
2458 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
2459 VS_PIN_CTRL_MASK | VS_ENABLE_MASK, &vreg->ctrl_reg);
2460
2461 if (!rc)
2462 vreg->is_enabled_pc = true;
2463
2464 mutex_unlock(&vreg->pc_lock);
2465
2466 if (rc)
2467 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2468 else
2469 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2470
2471 return rc;
2472}
2473
2474static int pm8xxx_vs_pin_control_disable(struct regulator_dev *rdev)
2475{
2476 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2477 int rc;
2478
2479 mutex_lock(&vreg->pc_lock);
2480
2481 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, 0,
2482 VS_PIN_CTRL_MASK, &vreg->ctrl_reg);
2483
2484 if (!rc)
2485 vreg->is_enabled_pc = false;
2486
2487 mutex_unlock(&vreg->pc_lock);
2488
2489 if (rc)
2490 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
2491 else
2492 pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
2493
2494 return rc;
2495}
2496
2497static int pm8xxx_enable_time(struct regulator_dev *rdev)
2498{
2499 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2500
2501 return vreg->pdata.enable_time;
2502}
2503
2504static const char const *pm8xxx_print_actions[] = {
2505 [PM8XXX_REGULATOR_ACTION_INIT] = "initial ",
2506 [PM8XXX_REGULATOR_ACTION_ENABLE] = "enable ",
2507 [PM8XXX_REGULATOR_ACTION_DISABLE] = "disable ",
2508 [PM8XXX_REGULATOR_ACTION_VOLTAGE] = "set voltage",
2509 [PM8XXX_REGULATOR_ACTION_MODE] = "set mode ",
2510 [PM8XXX_REGULATOR_ACTION_PIN_CTRL] = "pin control",
2511};
2512
2513static void pm8xxx_vreg_show_state(struct regulator_dev *rdev,
2514 enum pm8xxx_regulator_action action)
2515{
2516 struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
2517 int uV, pc;
2518 unsigned int mode;
2519 const char *pc_en0 = "", *pc_en1 = "", *pc_en2 = "", *pc_en3 = "";
2520 const char *pc_total = "";
2521 const char *action_label = pm8xxx_print_actions[action];
2522 const char *enable_label;
2523
2524 mutex_lock(&vreg->pc_lock);
2525
2526 /*
2527 * Do not print unless REQUEST is specified and SSBI writes have taken
2528 * place, or DUPLICATE is specified.
2529 */
2530 if (!((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_DUPLICATE)
2531 || ((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_REQUEST)
2532 && (vreg->write_count != vreg->prev_write_count)))) {
2533 mutex_unlock(&vreg->pc_lock);
2534 return;
2535 }
2536
2537 vreg->prev_write_count = vreg->write_count;
2538
2539 pc = vreg->pdata.pin_ctrl;
2540 if (vreg->is_enabled_pc) {
2541 if (pc & PM8XXX_VREG_PIN_CTRL_EN0)
2542 pc_en0 = " EN0";
2543 if (pc & PM8XXX_VREG_PIN_CTRL_EN1)
2544 pc_en1 = " EN1";
2545 if (pc & PM8XXX_VREG_PIN_CTRL_EN2)
2546 pc_en2 = " EN2";
2547 if (pc & PM8XXX_VREG_PIN_CTRL_EN3)
2548 pc_en3 = " EN3";
2549 if (pc == PM8XXX_VREG_PIN_CTRL_NONE)
2550 pc_total = " none";
2551 } else {
2552 pc_total = " none";
2553 }
2554
2555 mutex_unlock(&vreg->pc_lock);
2556
2557 enable_label = pm8xxx_vreg_is_enabled(rdev) ? "on " : "off";
2558
2559 switch (vreg->type) {
2560 case PM8XXX_REGULATOR_TYPE_PLDO:
2561 uV = pm8xxx_pldo_get_voltage(rdev);
2562 mode = pm8xxx_ldo_get_mode(rdev);
2563 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2564 action_label, vreg->rdesc.name, enable_label, uV,
2565 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2566 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2567 break;
2568 case PM8XXX_REGULATOR_TYPE_NLDO:
2569 uV = pm8xxx_nldo_get_voltage(rdev);
2570 mode = pm8xxx_ldo_get_mode(rdev);
2571 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2572 action_label, vreg->rdesc.name, enable_label, uV,
2573 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2574 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2575 break;
2576 case PM8XXX_REGULATOR_TYPE_NLDO1200:
2577 uV = pm8xxx_nldo1200_get_voltage(rdev);
2578 mode = pm8xxx_nldo1200_get_mode(rdev);
2579 pr_info("%s %-9s: %s, v=%7d uV, mode=%s\n",
2580 action_label, vreg->rdesc.name, enable_label, uV,
2581 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"));
2582 break;
2583 case PM8XXX_REGULATOR_TYPE_SMPS:
2584 uV = pm8xxx_smps_get_voltage(rdev);
2585 mode = pm8xxx_smps_get_mode(rdev);
2586 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2587 action_label, vreg->rdesc.name, enable_label, uV,
2588 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2589 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2590 break;
2591 case PM8XXX_REGULATOR_TYPE_FTSMPS:
2592 uV = pm8xxx_ftsmps_get_voltage(rdev);
2593 mode = pm8xxx_ftsmps_get_mode(rdev);
2594 pr_info("%s %-9s: %s, v=%7d uV, mode=%s\n",
2595 action_label, vreg->rdesc.name, enable_label, uV,
2596 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"));
2597 break;
2598 case PM8XXX_REGULATOR_TYPE_VS:
2599 pr_info("%s %-9s: %s, pc=%s%s%s%s%s\n",
2600 action_label, vreg->rdesc.name, enable_label,
2601 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2602 break;
2603 case PM8XXX_REGULATOR_TYPE_VS300:
2604 pr_info("%s %-9s: %s\n",
2605 action_label, vreg->rdesc.name, enable_label);
2606 break;
2607 case PM8XXX_REGULATOR_TYPE_NCP:
2608 uV = pm8xxx_ncp_get_voltage(rdev);
2609 pr_info("%s %-9s: %s, v=%7d uV\n",
2610 action_label, vreg->rdesc.name, enable_label, uV);
2611 break;
David Collins525da182012-02-13 10:36:37 -08002612 case PM8XXX_REGULATOR_TYPE_BOOST:
2613 uV = pm8xxx_boost_get_voltage(rdev);
2614 pr_info("%s %-9s: %s, v=%7d uV\n",
2615 action_label, vreg->rdesc.name, enable_label, uV);
2616 break;
David Collins5c4a5ab2011-12-02 15:01:26 -08002617 default:
2618 break;
2619 }
2620}
2621
2622/* Real regulator operations. */
2623static struct regulator_ops pm8xxx_pldo_ops = {
2624 .enable = pm8xxx_ldo_enable,
2625 .disable = pm8xxx_ldo_disable,
2626 .is_enabled = pm8xxx_vreg_is_enabled,
2627 .set_voltage = pm8xxx_pldo_set_voltage,
2628 .get_voltage = pm8xxx_pldo_get_voltage,
2629 .list_voltage = pm8xxx_pldo_list_voltage,
2630 .set_mode = pm8xxx_ldo_set_mode,
2631 .get_mode = pm8xxx_ldo_get_mode,
2632 .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
2633 .enable_time = pm8xxx_enable_time,
2634};
2635
2636static struct regulator_ops pm8xxx_nldo_ops = {
2637 .enable = pm8xxx_ldo_enable,
2638 .disable = pm8xxx_ldo_disable,
2639 .is_enabled = pm8xxx_vreg_is_enabled,
2640 .set_voltage = pm8xxx_nldo_set_voltage,
2641 .get_voltage = pm8xxx_nldo_get_voltage,
2642 .list_voltage = pm8xxx_nldo_list_voltage,
2643 .set_mode = pm8xxx_ldo_set_mode,
2644 .get_mode = pm8xxx_ldo_get_mode,
2645 .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
2646 .enable_time = pm8xxx_enable_time,
2647};
2648
2649static struct regulator_ops pm8xxx_nldo1200_ops = {
2650 .enable = pm8xxx_nldo1200_enable,
2651 .disable = pm8xxx_nldo1200_disable,
2652 .is_enabled = pm8xxx_vreg_is_enabled,
2653 .set_voltage = pm8xxx_nldo1200_set_voltage,
2654 .get_voltage = pm8xxx_nldo1200_get_voltage,
2655 .list_voltage = pm8xxx_nldo1200_list_voltage,
2656 .set_mode = pm8xxx_nldo1200_set_mode,
2657 .get_mode = pm8xxx_nldo1200_get_mode,
2658 .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
2659 .enable_time = pm8xxx_enable_time,
2660};
2661
2662static struct regulator_ops pm8xxx_smps_ops = {
2663 .enable = pm8xxx_smps_enable,
2664 .disable = pm8xxx_smps_disable,
2665 .is_enabled = pm8xxx_vreg_is_enabled,
2666 .set_voltage = pm8xxx_smps_set_voltage,
2667 .get_voltage = pm8xxx_smps_get_voltage,
2668 .list_voltage = pm8xxx_smps_list_voltage,
2669 .set_mode = pm8xxx_smps_set_mode,
2670 .get_mode = pm8xxx_smps_get_mode,
2671 .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
2672 .enable_time = pm8xxx_enable_time,
2673};
2674
2675static struct regulator_ops pm8xxx_ftsmps_ops = {
2676 .enable = pm8xxx_ftsmps_enable,
2677 .disable = pm8xxx_ftsmps_disable,
2678 .is_enabled = pm8xxx_vreg_is_enabled,
2679 .set_voltage = pm8xxx_ftsmps_set_voltage,
2680 .get_voltage = pm8xxx_ftsmps_get_voltage,
2681 .list_voltage = pm8xxx_ftsmps_list_voltage,
2682 .set_mode = pm8xxx_ftsmps_set_mode,
2683 .get_mode = pm8xxx_ftsmps_get_mode,
2684 .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
2685 .enable_time = pm8xxx_enable_time,
2686};
2687
2688static struct regulator_ops pm8xxx_vs_ops = {
2689 .enable = pm8xxx_vs_enable,
2690 .disable = pm8xxx_vs_disable,
2691 .is_enabled = pm8xxx_vreg_is_enabled,
2692 .enable_time = pm8xxx_enable_time,
2693};
2694
2695static struct regulator_ops pm8xxx_vs300_ops = {
2696 .enable = pm8xxx_vs300_enable,
2697 .disable = pm8xxx_vs300_disable,
2698 .is_enabled = pm8xxx_vreg_is_enabled,
2699 .enable_time = pm8xxx_enable_time,
2700};
2701
2702static struct regulator_ops pm8xxx_ncp_ops = {
2703 .enable = pm8xxx_ncp_enable,
2704 .disable = pm8xxx_ncp_disable,
2705 .is_enabled = pm8xxx_vreg_is_enabled,
2706 .set_voltage = pm8xxx_ncp_set_voltage,
2707 .get_voltage = pm8xxx_ncp_get_voltage,
2708 .list_voltage = pm8xxx_ncp_list_voltage,
2709 .enable_time = pm8xxx_enable_time,
2710};
2711
David Collins525da182012-02-13 10:36:37 -08002712static struct regulator_ops pm8xxx_boost_ops = {
2713 .enable = pm8xxx_boost_enable,
2714 .disable = pm8xxx_boost_disable,
2715 .is_enabled = pm8xxx_vreg_is_enabled,
2716 .set_voltage = pm8xxx_boost_set_voltage,
2717 .get_voltage = pm8xxx_boost_get_voltage,
2718 .list_voltage = pm8xxx_boost_list_voltage,
2719 .enable_time = pm8xxx_enable_time,
2720};
2721
David Collins5c4a5ab2011-12-02 15:01:26 -08002722/* Pin control regulator operations. */
2723static struct regulator_ops pm8xxx_ldo_pc_ops = {
2724 .enable = pm8xxx_ldo_pin_control_enable,
2725 .disable = pm8xxx_ldo_pin_control_disable,
2726 .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
2727};
2728
2729static struct regulator_ops pm8xxx_smps_pc_ops = {
2730 .enable = pm8xxx_smps_pin_control_enable,
2731 .disable = pm8xxx_smps_pin_control_disable,
2732 .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
2733};
2734
2735static struct regulator_ops pm8xxx_vs_pc_ops = {
2736 .enable = pm8xxx_vs_pin_control_enable,
2737 .disable = pm8xxx_vs_pin_control_disable,
2738 .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
2739};
2740
2741static struct regulator_ops *pm8xxx_reg_ops[PM8XXX_REGULATOR_TYPE_MAX] = {
2742 [PM8XXX_REGULATOR_TYPE_PLDO] = &pm8xxx_pldo_ops,
2743 [PM8XXX_REGULATOR_TYPE_NLDO] = &pm8xxx_nldo_ops,
2744 [PM8XXX_REGULATOR_TYPE_NLDO1200] = &pm8xxx_nldo1200_ops,
2745 [PM8XXX_REGULATOR_TYPE_SMPS] = &pm8xxx_smps_ops,
2746 [PM8XXX_REGULATOR_TYPE_FTSMPS] = &pm8xxx_ftsmps_ops,
2747 [PM8XXX_REGULATOR_TYPE_VS] = &pm8xxx_vs_ops,
2748 [PM8XXX_REGULATOR_TYPE_VS300] = &pm8xxx_vs300_ops,
2749 [PM8XXX_REGULATOR_TYPE_NCP] = &pm8xxx_ncp_ops,
David Collins525da182012-02-13 10:36:37 -08002750 [PM8XXX_REGULATOR_TYPE_BOOST] = &pm8xxx_boost_ops,
David Collins5c4a5ab2011-12-02 15:01:26 -08002751};
2752
2753static struct regulator_ops *pm8xxx_reg_pc_ops[PM8XXX_REGULATOR_TYPE_MAX] = {
2754 [PM8XXX_REGULATOR_TYPE_PLDO] = &pm8xxx_ldo_pc_ops,
2755 [PM8XXX_REGULATOR_TYPE_NLDO] = &pm8xxx_ldo_pc_ops,
2756 [PM8XXX_REGULATOR_TYPE_SMPS] = &pm8xxx_smps_pc_ops,
2757 [PM8XXX_REGULATOR_TYPE_VS] = &pm8xxx_vs_pc_ops,
2758};
2759
2760static unsigned pm8xxx_n_voltages[PM8XXX_REGULATOR_TYPE_MAX] = {
2761 [PM8XXX_REGULATOR_TYPE_PLDO] = PLDO_SET_POINTS,
2762 [PM8XXX_REGULATOR_TYPE_NLDO] = NLDO_SET_POINTS,
2763 [PM8XXX_REGULATOR_TYPE_NLDO1200] = NLDO1200_SET_POINTS,
2764 [PM8XXX_REGULATOR_TYPE_SMPS] = SMPS_ADVANCED_SET_POINTS,
2765 [PM8XXX_REGULATOR_TYPE_FTSMPS] = FTSMPS_SET_POINTS,
2766 [PM8XXX_REGULATOR_TYPE_VS] = 0,
2767 [PM8XXX_REGULATOR_TYPE_VS300] = 0,
2768 [PM8XXX_REGULATOR_TYPE_NCP] = NCP_SET_POINTS,
David Collins525da182012-02-13 10:36:37 -08002769 [PM8XXX_REGULATOR_TYPE_BOOST] = BOOST_SET_POINTS,
David Collins5c4a5ab2011-12-02 15:01:26 -08002770};
2771
2772static int pm8xxx_init_ldo(struct pm8xxx_vreg *vreg, bool is_real)
2773{
2774 int rc = 0;
2775 int i;
2776 u8 bank;
2777
2778 /* Save the current control register state. */
2779 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2780 if (rc)
2781 goto bail;
2782
2783 /* Save the current test register state. */
2784 for (i = 0; i < LDO_TEST_BANKS; i++) {
2785 bank = REGULATOR_BANK_SEL(i);
2786 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2787 if (rc)
2788 goto bail;
2789
2790 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2791 &vreg->test_reg[i]);
2792 if (rc)
2793 goto bail;
2794 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2795 }
2796
2797 if (is_real) {
2798 /* Set pull down enable based on platform data. */
2799 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2800 (vreg->pdata.pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
2801 LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
2802
2803 vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
2804
2805 vreg->mode = ((vreg->ctrl_reg & LDO_CTRL_PM_MASK)
2806 == LDO_CTRL_PM_LPM ?
2807 REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
2808 }
2809bail:
2810 if (rc)
2811 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2812
2813 return rc;
2814}
2815
2816static int pm8xxx_init_nldo1200(struct pm8xxx_vreg *vreg)
2817{
2818 int rc = 0;
2819 int i;
2820 u8 bank;
2821
2822 /* Save the current control register state. */
2823 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2824 if (rc)
2825 goto bail;
2826
2827 /* Save the current test register state. */
2828 for (i = 0; i < LDO_TEST_BANKS; i++) {
2829 bank = REGULATOR_BANK_SEL(i);
2830 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2831 if (rc)
2832 goto bail;
2833
2834 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2835 &vreg->test_reg[i]);
2836 if (rc)
2837 goto bail;
2838 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2839 }
2840
2841 vreg->save_uV = _pm8xxx_nldo1200_get_voltage(vreg);
2842
2843 /* Set pull down enable based on platform data. */
2844 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2845 (vreg->pdata.pull_down_enable ? NLDO1200_PULL_DOWN_ENABLE : 0)
2846 | REGULATOR_BANK_SEL(1) | REGULATOR_BANK_WRITE,
2847 NLDO1200_PULL_DOWN_ENABLE_MASK | REGULATOR_BANK_MASK,
2848 &vreg->test_reg[1]);
2849
2850bail:
2851 if (rc)
2852 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2853
2854 return rc;
2855}
2856
2857static int pm8xxx_init_smps(struct pm8xxx_vreg *vreg, bool is_real)
2858{
2859 int rc = 0;
2860 int i;
2861 u8 bank;
2862
2863 /* Save the current control register state. */
2864 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2865 if (rc)
2866 goto bail;
2867
2868 /* Save the current test2 register state. */
2869 for (i = 0; i < SMPS_TEST_BANKS; i++) {
2870 bank = REGULATOR_BANK_SEL(i);
2871 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2872 if (rc)
2873 goto bail;
2874
2875 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2876 &vreg->test_reg[i]);
2877 if (rc)
2878 goto bail;
2879 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2880 }
2881
2882 /* Save the current clock control register state. */
2883 rc = pm8xxx_readb(vreg->dev->parent, vreg->clk_ctrl_addr,
2884 &vreg->clk_ctrl_reg);
2885 if (rc)
2886 goto bail;
2887
2888 /* Save the current sleep control register state. */
2889 rc = pm8xxx_readb(vreg->dev->parent, vreg->sleep_ctrl_addr,
2890 &vreg->sleep_ctrl_reg);
2891 if (rc)
2892 goto bail;
2893
2894 vreg->save_uV = _pm8xxx_smps_get_voltage(vreg);
2895
2896 if (is_real) {
2897 /* Set advanced mode pull down enable based on platform data. */
2898 rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
2899 (vreg->pdata.pull_down_enable
2900 ? SMPS_ADVANCED_PULL_DOWN_ENABLE : 0)
2901 | REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
2902 REGULATOR_BANK_MASK | SMPS_ADVANCED_PULL_DOWN_ENABLE,
2903 &vreg->test_reg[6]);
2904 if (rc)
2905 goto bail;
2906
2907 vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
2908
2909 vreg->mode = ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK)
2910 == SMPS_CLK_CTRL_PFM ?
2911 REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
2912 }
2913
2914 if (!SMPS_IN_ADVANCED_MODE(vreg) && is_real) {
2915 /* Set legacy mode pull down enable based on platform data. */
2916 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
2917 (vreg->pdata.pull_down_enable
2918 ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0),
2919 SMPS_LEGACY_PULL_DOWN_ENABLE, &vreg->ctrl_reg);
2920 if (rc)
2921 goto bail;
2922 }
2923
2924bail:
2925 if (rc)
2926 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2927
2928 return rc;
2929}
2930
2931static int pm8xxx_init_ftsmps(struct pm8xxx_vreg *vreg)
2932{
2933 int rc, i;
2934 u8 bank;
2935
2936 /* Save the current control register state. */
2937 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2938 if (rc)
2939 goto bail;
2940
2941 /* Store current regulator register values. */
2942 rc = pm8xxx_readb(vreg->dev->parent, vreg->pfm_ctrl_addr,
2943 &vreg->pfm_ctrl_reg);
2944 if (rc)
2945 goto bail;
2946
2947 rc = pm8xxx_readb(vreg->dev->parent, vreg->pwr_cnfg_addr,
2948 &vreg->pwr_cnfg_reg);
2949 if (rc)
2950 goto bail;
2951
2952 /* Save the current fts_cnfg1 register state (uses 'test' member). */
2953 for (i = 0; i < SMPS_TEST_BANKS; i++) {
2954 bank = REGULATOR_BANK_SEL(i);
2955 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2956 if (rc)
2957 goto bail;
2958
2959 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2960 &vreg->test_reg[i]);
2961 if (rc)
2962 goto bail;
2963 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2964 }
2965
2966 vreg->save_uV = _pm8xxx_ftsmps_get_voltage(vreg);
2967
2968 /* Set pull down enable based on platform data. */
2969 rc = pm8xxx_vreg_masked_write(vreg, vreg->pwr_cnfg_addr,
2970 (vreg->pdata.pull_down_enable ? FTSMPS_PULL_DOWN_ENABLE : 0),
2971 FTSMPS_PULL_DOWN_ENABLE_MASK, &vreg->pwr_cnfg_reg);
2972
2973bail:
2974 if (rc)
2975 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2976
2977 return rc;
2978}
2979
2980static int pm8xxx_init_vs(struct pm8xxx_vreg *vreg, bool is_real)
2981{
2982 int rc = 0;
2983
2984 /* Save the current control register state. */
2985 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2986 if (rc) {
2987 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
2988 return rc;
2989 }
2990
David Collins0c19fa82012-01-30 11:23:38 -08002991 /* Save the current test register state. */
2992 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2993 &vreg->test_reg[0]);
2994 if (rc) {
2995 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
2996 return rc;
2997 }
2998
David Collins5c4a5ab2011-12-02 15:01:26 -08002999 if (is_real) {
3000 /* Set pull down enable based on platform data. */
3001 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
3002 (vreg->pdata.pull_down_enable ? VS_PULL_DOWN_ENABLE
3003 : VS_PULL_DOWN_DISABLE),
3004 VS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
3005
3006 if (rc)
3007 vreg_err(vreg,
3008 "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
3009
3010 vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
3011 }
3012
3013 return rc;
3014}
3015
3016static int pm8xxx_init_vs300(struct pm8xxx_vreg *vreg)
3017{
3018 int rc;
3019
3020 /* Save the current control register state. */
3021 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
3022 if (rc) {
3023 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
3024 return rc;
3025 }
3026
David Collins0c19fa82012-01-30 11:23:38 -08003027 /* Save the current test register state. */
3028 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
3029 &vreg->test_reg[0]);
3030 if (rc) {
3031 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
3032 return rc;
3033 }
3034
David Collins5c4a5ab2011-12-02 15:01:26 -08003035 /* Set pull down enable based on platform data. */
3036 rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
3037 (vreg->pdata.pull_down_enable ? VS300_PULL_DOWN_ENABLE : 0),
3038 VS300_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
3039
3040 if (rc)
3041 vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
3042
3043 return rc;
3044}
3045
3046static int pm8xxx_init_ncp(struct pm8xxx_vreg *vreg)
3047{
3048 int rc;
3049
3050 /* Save the current control register state. */
3051 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
3052 if (rc) {
3053 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
3054 return rc;
3055 }
3056
3057 return rc;
3058}
3059
David Collins525da182012-02-13 10:36:37 -08003060static int pm8xxx_init_boost(struct pm8xxx_vreg *vreg)
3061{
3062 int rc;
3063
3064 /* Save the current control register state. */
3065 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
3066 if (rc) {
3067 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
3068 return rc;
3069 }
3070
3071 return rc;
3072}
3073
David Collins5c4a5ab2011-12-02 15:01:26 -08003074static int __devinit pm8xxx_vreg_probe(struct platform_device *pdev)
3075{
3076 struct pm8xxx_regulator_core_platform_data *core_data;
3077 const struct pm8xxx_regulator_platform_data *pdata;
3078 enum pm8xxx_vreg_pin_function pin_fn;
3079 struct regulator_desc *rdesc;
3080 struct pm8xxx_vreg *vreg;
3081 unsigned pin_ctrl;
3082 int rc = 0;
3083
3084 if (pdev == NULL) {
3085 pr_err("no platform device specified\n");
3086 return -EINVAL;
3087 }
3088
3089 core_data = pdev->dev.platform_data;
3090 if (core_data == NULL) {
3091 pr_err("no core data specified\n");
3092 return -EINVAL;
3093 }
3094
3095 pdata = core_data->pdata;
3096 vreg = core_data->vreg;
3097 if (pdata == NULL) {
3098 pr_err("no pdata specified\n");
3099 return -EINVAL;
3100 } else if (vreg == NULL) {
3101 pr_err("no vreg specified\n");
3102 return -EINVAL;
3103 }
3104
3105 if (vreg->rdesc.name == NULL) {
3106 pr_err("regulator name missing\n");
3107 return -EINVAL;
3108 } else if (vreg->type < 0 || vreg->type > PM8XXX_REGULATOR_TYPE_MAX) {
3109 pr_err("%s: regulator type=%d is invalid\n", vreg->rdesc.name,
3110 vreg->type);
3111 return -EINVAL;
3112 } else if (core_data->is_pin_controlled
3113 && pm8xxx_reg_pc_ops[vreg->type] == NULL) {
3114 pr_err("%s: regulator type=%d does not support pin control\n",
3115 vreg->rdesc.name, vreg->type);
3116 return -EINVAL;
3117 } else if (core_data->is_pin_controlled
3118 && vreg->rdesc_pc.name == NULL) {
3119 pr_err("%s: regulator pin control name missing\n",
3120 vreg->rdesc.name);
3121 return -EINVAL;
3122 }
3123
3124 if (core_data->is_pin_controlled)
3125 rdesc = &vreg->rdesc_pc;
3126 else
3127 rdesc = &vreg->rdesc;
3128 if (!pdata) {
3129 pr_err("%s requires platform data\n", vreg->rdesc.name);
3130 return -EINVAL;
3131 }
3132
3133 rdesc->id = pdev->id;
3134 rdesc->owner = THIS_MODULE;
3135 rdesc->type = REGULATOR_VOLTAGE;
3136 if (core_data->is_pin_controlled) {
3137 rdesc->ops = pm8xxx_reg_pc_ops[vreg->type];
3138 rdesc->n_voltages = 0;
3139 } else {
3140 rdesc->ops = pm8xxx_reg_ops[vreg->type];
3141 rdesc->n_voltages = pm8xxx_n_voltages[vreg->type];
3142 }
3143
3144 mutex_lock(&vreg->pc_lock);
3145
3146 if (!core_data->is_pin_controlled) {
3147 /* Do not modify pin control and pin function values. */
3148 pin_ctrl = vreg->pdata.pin_ctrl;
3149 pin_fn = vreg->pdata.pin_fn;
3150 memcpy(&(vreg->pdata), pdata,
3151 sizeof(struct pm8xxx_regulator_platform_data));
3152 vreg->pdata.pin_ctrl = pin_ctrl;
3153 vreg->pdata.pin_fn = pin_fn;
3154 vreg->dev = &pdev->dev;
3155 } else {
3156 /* Pin control regulator */
3157 if ((pdata->pin_ctrl & PM8XXX_VREG_PIN_CTRL_ALL)
3158 == PM8XXX_VREG_PIN_CTRL_NONE) {
3159 pr_err("%s: no pin control input specified\n",
3160 vreg->rdesc.name);
3161 mutex_unlock(&vreg->pc_lock);
3162 return -EINVAL;
3163 }
3164 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
3165 vreg->pdata.pin_fn = pdata->pin_fn;
3166 vreg->dev_pc = &pdev->dev;
3167 if (!vreg->dev)
3168 vreg->dev = &pdev->dev;
3169 }
3170
3171 /* Initialize register values. */
3172 switch (vreg->type) {
3173 case PM8XXX_REGULATOR_TYPE_PLDO:
3174 case PM8XXX_REGULATOR_TYPE_NLDO:
3175 rc = pm8xxx_init_ldo(vreg, !core_data->is_pin_controlled);
3176 break;
3177 case PM8XXX_REGULATOR_TYPE_NLDO1200:
3178 rc = pm8xxx_init_nldo1200(vreg);
3179 break;
3180 case PM8XXX_REGULATOR_TYPE_SMPS:
3181 rc = pm8xxx_init_smps(vreg, !core_data->is_pin_controlled);
3182 break;
3183 case PM8XXX_REGULATOR_TYPE_FTSMPS:
3184 rc = pm8xxx_init_ftsmps(vreg);
3185 break;
3186 case PM8XXX_REGULATOR_TYPE_VS:
3187 rc = pm8xxx_init_vs(vreg, !core_data->is_pin_controlled);
3188 break;
3189 case PM8XXX_REGULATOR_TYPE_VS300:
3190 rc = pm8xxx_init_vs300(vreg);
3191 break;
3192 case PM8XXX_REGULATOR_TYPE_NCP:
3193 rc = pm8xxx_init_ncp(vreg);
3194 break;
David Collins525da182012-02-13 10:36:37 -08003195 case PM8XXX_REGULATOR_TYPE_BOOST:
3196 rc = pm8xxx_init_boost(vreg);
3197 break;
David Collins5c4a5ab2011-12-02 15:01:26 -08003198 default:
3199 break;
3200 }
3201
3202 mutex_unlock(&vreg->pc_lock);
3203
3204 if (rc)
3205 goto bail;
3206
3207 if (!core_data->is_pin_controlled) {
3208 vreg->rdev = regulator_register(rdesc, &pdev->dev,
3209 &(pdata->init_data), vreg);
3210 if (IS_ERR(vreg->rdev)) {
3211 rc = PTR_ERR(vreg->rdev);
3212 vreg->rdev = NULL;
3213 pr_err("regulator_register failed: %s, rc=%d\n",
3214 vreg->rdesc.name, rc);
3215 }
3216 } else {
3217 vreg->rdev_pc = regulator_register(rdesc, &pdev->dev,
3218 &(pdata->init_data), vreg);
3219 if (IS_ERR(vreg->rdev_pc)) {
3220 rc = PTR_ERR(vreg->rdev_pc);
3221 vreg->rdev_pc = NULL;
3222 pr_err("regulator_register failed: %s, rc=%d\n",
3223 vreg->rdesc.name, rc);
3224 }
3225 }
3226 if ((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_INIT) && !rc
3227 && vreg->rdev)
3228 pm8xxx_vreg_show_state(vreg->rdev,
3229 PM8XXX_REGULATOR_ACTION_INIT);
3230
3231 platform_set_drvdata(pdev, core_data);
3232
3233bail:
3234 if (rc)
3235 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
3236
3237 return rc;
3238}
3239
3240static int __devexit pm8xxx_vreg_remove(struct platform_device *pdev)
3241{
3242 struct pm8xxx_regulator_core_platform_data *core_data;
3243
3244 core_data = platform_get_drvdata(pdev);
3245 platform_set_drvdata(pdev, NULL);
3246
3247 if (core_data) {
3248 if (core_data->is_pin_controlled)
3249 regulator_unregister(core_data->vreg->rdev_pc);
3250 else
3251 regulator_unregister(core_data->vreg->rdev);
3252 }
3253
3254 return 0;
3255}
3256
3257static struct platform_driver pm8xxx_vreg_driver = {
3258 .probe = pm8xxx_vreg_probe,
3259 .remove = __devexit_p(pm8xxx_vreg_remove),
3260 .driver = {
3261 .name = PM8XXX_REGULATOR_DEV_NAME,
3262 .owner = THIS_MODULE,
3263 },
3264};
3265
3266static int __init pm8xxx_vreg_init(void)
3267{
3268 return platform_driver_register(&pm8xxx_vreg_driver);
3269}
3270postcore_initcall(pm8xxx_vreg_init);
3271
3272static void __exit pm8xxx_vreg_exit(void)
3273{
3274 platform_driver_unregister(&pm8xxx_vreg_driver);
3275}
3276module_exit(pm8xxx_vreg_exit);
3277
3278MODULE_LICENSE("GPL v2");
3279MODULE_DESCRIPTION("PMIC PM8XXX regulator driver");
3280MODULE_VERSION("1.0");
3281MODULE_ALIAS("platform:" PM8XXX_REGULATOR_DEV_NAME);