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