blob: 2f31cbb9606818f2266beb7b4b0a4f35d0f2a935 [file] [log] [blame]
David Collins34dd32c2011-08-29 09:22:19 -07001/*
2 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
3 *
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>
17#include <linux/err.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/mutex.h>
21#include <linux/init.h>
22#include <linux/bitops.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26#include <linux/regulator/pm8018-regulator.h>
27#include <linux/mfd/pm8xxx/core.h>
28
29/* Debug Flag Definitions */
30enum {
31 PM8018_VREG_DEBUG_REQUEST = BIT(0),
32 PM8018_VREG_DEBUG_DUPLICATE = BIT(1),
33 PM8018_VREG_DEBUG_INIT = BIT(2),
34 PM8018_VREG_DEBUG_WRITES = BIT(3), /* SSBI writes */
35};
36
37static int pm8018_vreg_debug_mask;
38module_param_named(
39 debug_mask, pm8018_vreg_debug_mask, int, S_IRUSR | S_IWUSR
40);
41
42#define REGULATOR_TYPE_PLDO 0
43#define REGULATOR_TYPE_NLDO 1
44#define REGULATOR_TYPE_NLDO1200 2
45#define REGULATOR_TYPE_SMPS 3
46#define REGULATOR_TYPE_VS 4
47
48/* Common Masks */
49#define REGULATOR_ENABLE_MASK 0x80
50#define REGULATOR_ENABLE 0x80
51#define REGULATOR_DISABLE 0x00
52
53#define REGULATOR_BANK_MASK 0xF0
54#define REGULATOR_BANK_SEL(n) ((n) << 4)
55#define REGULATOR_BANK_WRITE 0x80
56
57#define LDO_TEST_BANKS 7
58#define NLDO1200_TEST_BANKS 5
59#define SMPS_TEST_BANKS 8
60#define REGULATOR_TEST_BANKS_MAX SMPS_TEST_BANKS
61
62/*
63 * This voltage in uV is returned by get_voltage functions when there is no way
64 * to determine the current voltage level. It is needed because the regulator
65 * framework treats a 0 uV voltage as an error.
66 */
67#define VOLTAGE_UNKNOWN 1
68
69/* LDO masks and values */
70
71/* CTRL register */
72#define LDO_ENABLE_MASK 0x80
73#define LDO_DISABLE 0x00
74#define LDO_ENABLE 0x80
75#define LDO_PULL_DOWN_ENABLE_MASK 0x40
76#define LDO_PULL_DOWN_ENABLE 0x40
77
78#define LDO_CTRL_PM_MASK 0x20
79#define LDO_CTRL_PM_HPM 0x00
80#define LDO_CTRL_PM_LPM 0x20
81
82#define LDO_CTRL_VPROG_MASK 0x1F
83
84/* TEST register bank 0 */
85#define LDO_TEST_LPM_MASK 0x40
86#define LDO_TEST_LPM_SEL_CTRL 0x00
87#define LDO_TEST_LPM_SEL_TCXO 0x40
88
89/* TEST register bank 2 */
90#define LDO_TEST_VPROG_UPDATE_MASK 0x08
91#define LDO_TEST_RANGE_SEL_MASK 0x04
92#define LDO_TEST_FINE_STEP_MASK 0x02
93#define LDO_TEST_FINE_STEP_SHIFT 1
94
95/* TEST register bank 4 */
96#define LDO_TEST_RANGE_EXT_MASK 0x01
97
98/* TEST register bank 5 */
99#define LDO_TEST_PIN_CTRL_MASK 0x0F
100#define LDO_TEST_PIN_CTRL_EN3 0x08
101#define LDO_TEST_PIN_CTRL_EN2 0x04
102#define LDO_TEST_PIN_CTRL_EN1 0x02
103#define LDO_TEST_PIN_CTRL_EN0 0x01
104
105/* TEST register bank 6 */
106#define LDO_TEST_PIN_CTRL_LPM_MASK 0x0F
107
108/*
109 * If a given voltage could be output by two ranges, then the preferred one must
110 * be determined by the range limits. Specified voltage ranges should must
111 * not overlap.
112 *
113 * Allowable voltage ranges:
114 */
115#define PLDO_LOW_UV_MIN 750000
116#define PLDO_LOW_UV_MAX 1487500
117#define PLDO_LOW_UV_FINE_STEP 12500
118
119#define PLDO_NORM_UV_MIN 1500000
120#define PLDO_NORM_UV_MAX 3075000
121#define PLDO_NORM_UV_FINE_STEP 25000
122
123#define PLDO_HIGH_UV_MIN 1750000
124#define PLDO_HIGH_UV_SET_POINT_MIN 3100000
125#define PLDO_HIGH_UV_MAX 4900000
126#define PLDO_HIGH_UV_FINE_STEP 50000
127
128#define PLDO_LOW_SET_POINTS ((PLDO_LOW_UV_MAX - PLDO_LOW_UV_MIN) \
129 / PLDO_LOW_UV_FINE_STEP + 1)
130#define PLDO_NORM_SET_POINTS ((PLDO_NORM_UV_MAX - PLDO_NORM_UV_MIN) \
131 / PLDO_NORM_UV_FINE_STEP + 1)
132#define PLDO_HIGH_SET_POINTS ((PLDO_HIGH_UV_MAX \
133 - PLDO_HIGH_UV_SET_POINT_MIN) \
134 / PLDO_HIGH_UV_FINE_STEP + 1)
135#define PLDO_SET_POINTS (PLDO_LOW_SET_POINTS \
136 + PLDO_NORM_SET_POINTS \
137 + PLDO_HIGH_SET_POINTS)
138
139#define NLDO_UV_MIN 750000
140#define NLDO_UV_MAX 1537500
141#define NLDO_UV_FINE_STEP 12500
142
143#define NLDO_SET_POINTS ((NLDO_UV_MAX - NLDO_UV_MIN) \
144 / NLDO_UV_FINE_STEP + 1)
145
146/* NLDO1200 masks and values */
147
148/* CTRL register */
149#define NLDO1200_ENABLE_MASK 0x80
150#define NLDO1200_DISABLE 0x00
151#define NLDO1200_ENABLE 0x80
152
153/* Legacy mode */
154#define NLDO1200_LEGACY_PM_MASK 0x20
155#define NLDO1200_LEGACY_PM_HPM 0x00
156#define NLDO1200_LEGACY_PM_LPM 0x20
157
158/* Advanced mode */
159#define NLDO1200_CTRL_RANGE_MASK 0x40
160#define NLDO1200_CTRL_RANGE_HIGH 0x00
161#define NLDO1200_CTRL_RANGE_LOW 0x40
162#define NLDO1200_CTRL_VPROG_MASK 0x3F
163
164#define NLDO1200_LOW_UV_MIN 375000
165#define NLDO1200_LOW_UV_MAX 743750
166#define NLDO1200_LOW_UV_STEP 6250
167
168#define NLDO1200_HIGH_UV_MIN 750000
169#define NLDO1200_HIGH_UV_MAX 1537500
170#define NLDO1200_HIGH_UV_STEP 12500
171
172#define NLDO1200_LOW_SET_POINTS ((NLDO1200_LOW_UV_MAX \
173 - NLDO1200_LOW_UV_MIN) \
174 / NLDO1200_LOW_UV_STEP + 1)
175#define NLDO1200_HIGH_SET_POINTS ((NLDO1200_HIGH_UV_MAX \
176 - NLDO1200_HIGH_UV_MIN) \
177 / NLDO1200_HIGH_UV_STEP + 1)
178#define NLDO1200_SET_POINTS (NLDO1200_LOW_SET_POINTS \
179 + NLDO1200_HIGH_SET_POINTS)
180
181/* TEST register bank 0 */
182#define NLDO1200_TEST_LPM_MASK 0x04
183#define NLDO1200_TEST_LPM_SEL_CTRL 0x00
184#define NLDO1200_TEST_LPM_SEL_TCXO 0x04
185
186/* TEST register bank 1 */
187#define NLDO1200_PULL_DOWN_ENABLE_MASK 0x02
188#define NLDO1200_PULL_DOWN_ENABLE 0x02
189
190/* TEST register bank 2 */
191#define NLDO1200_ADVANCED_MODE_MASK 0x08
192#define NLDO1200_ADVANCED_MODE 0x00
193#define NLDO1200_LEGACY_MODE 0x08
194
195/* Advanced mode power mode control */
196#define NLDO1200_ADVANCED_PM_MASK 0x02
197#define NLDO1200_ADVANCED_PM_HPM 0x00
198#define NLDO1200_ADVANCED_PM_LPM 0x02
199
200#define NLDO1200_IN_ADVANCED_MODE(vreg) \
201 ((vreg->test_reg[2] & NLDO1200_ADVANCED_MODE_MASK) \
202 == NLDO1200_ADVANCED_MODE)
203
204/* SMPS masks and values */
205
206/* CTRL register */
207
208/* Legacy mode */
209#define SMPS_LEGACY_ENABLE_MASK 0x80
210#define SMPS_LEGACY_DISABLE 0x00
211#define SMPS_LEGACY_ENABLE 0x80
212#define SMPS_LEGACY_PULL_DOWN_ENABLE 0x40
213#define SMPS_LEGACY_VREF_SEL_MASK 0x20
214#define SMPS_LEGACY_VPROG_MASK 0x1F
215
216/* Advanced mode */
217#define SMPS_ADVANCED_BAND_MASK 0xC0
218#define SMPS_ADVANCED_BAND_OFF 0x00
219#define SMPS_ADVANCED_BAND_1 0x40
220#define SMPS_ADVANCED_BAND_2 0x80
221#define SMPS_ADVANCED_BAND_3 0xC0
222#define SMPS_ADVANCED_VPROG_MASK 0x3F
223
224/* Legacy mode voltage ranges */
225#define SMPS_MODE3_UV_MIN 375000
226#define SMPS_MODE3_UV_MAX 725000
227#define SMPS_MODE3_UV_STEP 25000
228
229#define SMPS_MODE2_UV_MIN 750000
230#define SMPS_MODE2_UV_MAX 1475000
231#define SMPS_MODE2_UV_STEP 25000
232
233#define SMPS_MODE1_UV_MIN 1500000
234#define SMPS_MODE1_UV_MAX 3050000
235#define SMPS_MODE1_UV_STEP 50000
236
237#define SMPS_MODE3_SET_POINTS ((SMPS_MODE3_UV_MAX \
238 - SMPS_MODE3_UV_MIN) \
239 / SMPS_MODE3_UV_STEP + 1)
240#define SMPS_MODE2_SET_POINTS ((SMPS_MODE2_UV_MAX \
241 - SMPS_MODE2_UV_MIN) \
242 / SMPS_MODE2_UV_STEP + 1)
243#define SMPS_MODE1_SET_POINTS ((SMPS_MODE1_UV_MAX \
244 - SMPS_MODE1_UV_MIN) \
245 / SMPS_MODE1_UV_STEP + 1)
246#define SMPS_LEGACY_SET_POINTS (SMPS_MODE3_SET_POINTS \
247 + SMPS_MODE2_SET_POINTS \
248 + SMPS_MODE1_SET_POINTS)
249
250/* Advanced mode voltage ranges */
251#define SMPS_BAND1_UV_MIN 375000
252#define SMPS_BAND1_UV_MAX 737500
253#define SMPS_BAND1_UV_STEP 12500
254
255#define SMPS_BAND2_UV_MIN 750000
256#define SMPS_BAND2_UV_MAX 1487500
257#define SMPS_BAND2_UV_STEP 12500
258
259#define SMPS_BAND3_UV_MIN 1500000
260#define SMPS_BAND3_UV_MAX 3075000
261#define SMPS_BAND3_UV_STEP 25000
262
263#define SMPS_BAND1_SET_POINTS ((SMPS_BAND1_UV_MAX \
264 - SMPS_BAND1_UV_MIN) \
265 / SMPS_BAND1_UV_STEP + 1)
266#define SMPS_BAND2_SET_POINTS ((SMPS_BAND2_UV_MAX \
267 - SMPS_BAND2_UV_MIN) \
268 / SMPS_BAND2_UV_STEP + 1)
269#define SMPS_BAND3_SET_POINTS ((SMPS_BAND3_UV_MAX \
270 - SMPS_BAND3_UV_MIN) \
271 / SMPS_BAND3_UV_STEP + 1)
272#define SMPS_ADVANCED_SET_POINTS (SMPS_BAND1_SET_POINTS \
273 + SMPS_BAND2_SET_POINTS \
274 + SMPS_BAND3_SET_POINTS)
275
276/* Test2 register bank 1 */
277#define SMPS_LEGACY_VLOW_SEL_MASK 0x01
278
279/* Test2 register bank 6 */
280#define SMPS_ADVANCED_PULL_DOWN_ENABLE 0x08
281
282/* Test2 register bank 7 */
283#define SMPS_ADVANCED_MODE_MASK 0x02
284#define SMPS_ADVANCED_MODE 0x02
285#define SMPS_LEGACY_MODE 0x00
286
287#define SMPS_IN_ADVANCED_MODE(vreg) \
288 ((vreg->test_reg[7] & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE)
289
290/* BUCK_SLEEP_CNTRL register */
291#define SMPS_PIN_CTRL_MASK 0xF0
292#define SMPS_PIN_CTRL_EN3 0x80
293#define SMPS_PIN_CTRL_EN2 0x40
294#define SMPS_PIN_CTRL_EN1 0x20
295#define SMPS_PIN_CTRL_EN0 0x10
296
297#define SMPS_PIN_CTRL_LPM_MASK 0x0F
298#define SMPS_PIN_CTRL_LPM_EN3 0x08
299#define SMPS_PIN_CTRL_LPM_EN2 0x04
300#define SMPS_PIN_CTRL_LPM_EN1 0x02
301#define SMPS_PIN_CTRL_LPM_EN0 0x01
302
303/* BUCK_CLOCK_CNTRL register */
304#define SMPS_CLK_DIVIDE2 0x40
305
306#define SMPS_CLK_CTRL_MASK 0x30
307#define SMPS_CLK_CTRL_FOLLOW_TCXO 0x00
308#define SMPS_CLK_CTRL_PWM 0x10
309#define SMPS_CLK_CTRL_PFM 0x20
310
311/* VS masks and values */
312
313/* CTRL register */
314#define VS_ENABLE_MASK 0x80
315#define VS_DISABLE 0x00
316#define VS_ENABLE 0x80
317#define VS_PULL_DOWN_ENABLE_MASK 0x40
318#define VS_PULL_DOWN_ENABLE 0x40
319
320#define VS_PIN_CTRL_MASK 0x0F
321#define VS_PIN_CTRL_EN0 0x08
322#define VS_PIN_CTRL_EN1 0x04
323#define VS_PIN_CTRL_EN2 0x02
324#define VS_PIN_CTRL_EN3 0x01
325
326#define IS_REAL_REGULATOR(id) ((id) >= 0 && \
327 (id) < PM8018_VREG_ID_L2_PC)
328
329struct pm8018_vreg {
330 /* Configuration data */
331 struct regulator_dev *rdev;
332 struct regulator_dev *rdev_pc;
333 struct device *dev;
334 struct device *dev_pc;
335 const char *name;
336 struct pm8018_regulator_platform_data pdata;
337 const int hpm_min_load;
338 const u16 ctrl_addr;
339 const u16 test_addr;
340 const u16 clk_ctrl_addr;
341 const u16 sleep_ctrl_addr;
342 const u8 type;
343 /* State data */
344 struct mutex pc_lock;
345 int save_uV;
346 int mode;
347 u32 write_count;
348 u32 prev_write_count;
349 bool is_enabled;
350 bool is_enabled_pc;
351 u8 test_reg[REGULATOR_TEST_BANKS_MAX];
352 u8 ctrl_reg;
353 u8 clk_ctrl_reg;
354 u8 sleep_ctrl_reg;
355};
356
357#define vreg_err(vreg, fmt, ...) \
358 pr_err("%s: " fmt, vreg->name, ##__VA_ARGS__)
359
360#define PLDO(_id, _ctrl_addr, _test_addr, _hpm_min_load) \
361 [PM8018_VREG_ID_##_id] = { \
362 .type = REGULATOR_TYPE_PLDO, \
363 .ctrl_addr = _ctrl_addr, \
364 .test_addr = _test_addr, \
365 .hpm_min_load = PM8018_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
366 }
367
368#define NLDO(_id, _ctrl_addr, _test_addr, _hpm_min_load) \
369 [PM8018_VREG_ID_##_id] = { \
370 .type = REGULATOR_TYPE_NLDO, \
371 .ctrl_addr = _ctrl_addr, \
372 .test_addr = _test_addr, \
373 .hpm_min_load = PM8018_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
374 }
375
376#define NLDO1200(_id, _ctrl_addr, _test_addr, _hpm_min_load) \
377 [PM8018_VREG_ID_##_id] = { \
378 .type = REGULATOR_TYPE_NLDO1200, \
379 .ctrl_addr = _ctrl_addr, \
380 .test_addr = _test_addr, \
381 .hpm_min_load = PM8018_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
382 }
383
384#define SMPS(_id, _ctrl_addr, _test_addr, _clk_ctrl_addr, _sleep_ctrl_addr, \
385 _hpm_min_load) \
386 [PM8018_VREG_ID_##_id] = { \
387 .type = REGULATOR_TYPE_SMPS, \
388 .ctrl_addr = _ctrl_addr, \
389 .test_addr = _test_addr, \
390 .clk_ctrl_addr = _clk_ctrl_addr, \
391 .sleep_ctrl_addr = _sleep_ctrl_addr, \
392 .hpm_min_load = PM8018_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
393 }
394
395#define VS(_id, _ctrl_addr) \
396 [PM8018_VREG_ID_##_id] = { \
397 .type = REGULATOR_TYPE_VS, \
398 .ctrl_addr = _ctrl_addr, \
399 }
400
401static struct pm8018_vreg pm8018_vreg[] = {
402 /* id ctrl test hpm_min */
403 PLDO(L2, 0x0B0, 0x0B1, LDO_50),
404 PLDO(L3, 0x0B2, 0x0B3, LDO_50),
405 PLDO(L4, 0x0B4, 0x0B5, LDO_300),
406 PLDO(L5, 0x0B6, 0x0B7, LDO_150),
407 PLDO(L6, 0x0B8, 0x0B9, LDO_150),
408 PLDO(L7, 0x0BA, 0x0BB, LDO_300),
409 NLDO(L8, 0x0BC, 0x0BD, LDO_150),
410 NLDO1200(L9, 0x0BE, 0x0BF, LDO_1200),
411 NLDO1200(L10, 0x0C0, 0x0C1, LDO_1200),
412 NLDO1200(L11, 0x0C2, 0x0C3, LDO_1200),
413 NLDO1200(L12, 0x0C4, 0x0C5, LDO_1200),
414 PLDO(L13, 0x0C8, 0x0C9, LDO_50),
415 PLDO(L14, 0x0CA, 0x0CB, LDO_50),
416
417 /* id ctrl test2 clk sleep hpm_min */
418 SMPS(S1, 0x1D0, 0x1D5, 0x009, 0x1D2, SMPS_1500),
419 SMPS(S2, 0x1D8, 0x1DD, 0x00A, 0x1DA, SMPS_1500),
420 SMPS(S3, 0x1E0, 0x1E5, 0x00B, 0x1E2, SMPS_1500),
421 SMPS(S4, 0x1E8, 0x1ED, 0x00C, 0x1EA, SMPS_1500),
422 SMPS(S5, 0x1F0, 0x1F5, 0x00D, 0x1F2, SMPS_1500),
423
424 /* id ctrl */
425 VS(LVS1, 0x060),
426};
427
428/* Determines which label to add to the print. */
429enum pm8018_regulator_action {
430 PM8018_REGULATOR_ACTION_INIT,
431 PM8018_REGULATOR_ACTION_ENABLE,
432 PM8018_REGULATOR_ACTION_DISABLE,
433 PM8018_REGULATOR_ACTION_VOLTAGE,
434 PM8018_REGULATOR_ACTION_MODE,
435 PM8018_REGULATOR_ACTION_PIN_CTRL,
436};
437
438/* Debug state printing */
439static void pm8018_vreg_show_state(struct regulator_dev *rdev,
440 enum pm8018_regulator_action action);
441
442/*
443 * Perform a masked write to a PMIC register only if the new value differs
444 * from the last value written to the register. This removes redundant
445 * register writing.
446 *
447 * No locking is required because registers are not shared between regulators.
448 */
449static int pm8018_vreg_masked_write(struct pm8018_vreg *vreg, u16 addr, u8 val,
450 u8 mask, u8 *reg_save)
451{
452 int rc = 0;
453 u8 reg;
454
455 reg = (*reg_save & ~mask) | (val & mask);
456 if (reg != *reg_save) {
457 rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
458
459 if (rc) {
460 pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
461 vreg->name, addr, rc);
462 } else {
463 *reg_save = reg;
464 vreg->write_count++;
465 if (pm8018_vreg_debug_mask & PM8018_VREG_DEBUG_WRITES)
466 pr_info("%s: write(0x%03X)=0x%02X", vreg->name,
467 addr, reg);
468 }
469 }
470
471 return rc;
472}
473
474/*
475 * Perform a masked write to a PMIC register without checking the previously
476 * written value. This is needed for registers that must be rewritten even if
477 * the value hasn't changed in order for changes in other registers to take
478 * effect.
479 */
480static int pm8018_vreg_masked_write_forced(struct pm8018_vreg *vreg, u16 addr,
481 u8 val, u8 mask, u8 *reg_save)
482{
483 int rc = 0;
484 u8 reg;
485
486 reg = (*reg_save & ~mask) | (val & mask);
487 rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
488
489 if (rc) {
490 pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
491 vreg->name, addr, rc);
492 } else {
493 *reg_save = reg;
494 vreg->write_count++;
495 if (pm8018_vreg_debug_mask & PM8018_VREG_DEBUG_WRITES)
496 pr_info("%s: write(0x%03X)=0x%02X", vreg->name,
497 addr, reg);
498 }
499
500 return rc;
501}
502
503static int pm8018_vreg_is_pin_controlled(struct pm8018_vreg *vreg)
504{
505 int ret = 0;
506
507 switch (vreg->type) {
508 case REGULATOR_TYPE_PLDO:
509 case REGULATOR_TYPE_NLDO:
510 ret = ((vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK) << 4)
511 | (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK);
512 break;
513 case REGULATOR_TYPE_SMPS:
514 ret = vreg->sleep_ctrl_reg
515 & (SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK);
516 break;
517 case REGULATOR_TYPE_VS:
518 ret = vreg->ctrl_reg & VS_PIN_CTRL_MASK;
519 break;
520 }
521
522 return ret;
523}
524
525/*
526 * Returns the logical pin control enable state because the pin control options
527 * present in the hardware out of restart could be different from those desired
528 * by the consumer.
529 */
530static int pm8018_vreg_pin_control_is_enabled(struct regulator_dev *rdev)
531{
532 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
533 int enabled;
534
535 mutex_lock(&vreg->pc_lock);
536 enabled = vreg->is_enabled_pc;
537 mutex_unlock(&vreg->pc_lock);
538
539 return enabled;
540}
541
542/* Returns the physical enable state of the regulator. */
543static int _pm8018_vreg_is_enabled(struct pm8018_vreg *vreg)
544{
545 int rc = 0;
546
547 /*
548 * All regulator types except advanced mode SMPS have enable bit in
549 * bit 7 of the control register.
550 */
551
552 if (vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg)) {
553 if ((vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK)
554 != SMPS_ADVANCED_BAND_OFF)
555 rc = 1;
556 } else {
557 if ((vreg->ctrl_reg & REGULATOR_ENABLE_MASK)
558 == REGULATOR_ENABLE)
559 rc = 1;
560 }
561
562 return rc;
563}
564
565/*
566 * Returns the logical enable state of the regulator which may be different from
567 * the physical enable state thanks to HPM/LPM pin control.
568 */
569static int pm8018_vreg_is_enabled(struct regulator_dev *rdev)
570{
571 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
572 int enabled;
573
574 if (vreg->type == REGULATOR_TYPE_PLDO
575 || vreg->type == REGULATOR_TYPE_NLDO
576 || vreg->type == REGULATOR_TYPE_SMPS
577 || vreg->type == REGULATOR_TYPE_VS) {
578 /* Pin controllable */
579 mutex_lock(&vreg->pc_lock);
580 enabled = vreg->is_enabled;
581 mutex_unlock(&vreg->pc_lock);
582 } else {
583 /* Not pin controlable */
584 enabled = _pm8018_vreg_is_enabled(vreg);
585 }
586
587 return enabled;
588}
589
590static int pm8018_pldo_get_voltage(struct regulator_dev *rdev)
591{
592 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
593 int vmin, fine_step;
594 u8 range_ext, range_sel, vprog, fine_step_reg;
595
596 mutex_lock(&vreg->pc_lock);
597
598 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
599 range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
600 range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
601 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
602
603 mutex_unlock(&vreg->pc_lock);
604
605 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
606
607 if (range_sel) {
608 /* low range mode */
609 fine_step = PLDO_LOW_UV_FINE_STEP;
610 vmin = PLDO_LOW_UV_MIN;
611 } else if (!range_ext) {
612 /* normal mode */
613 fine_step = PLDO_NORM_UV_FINE_STEP;
614 vmin = PLDO_NORM_UV_MIN;
615 } else {
616 /* high range mode */
617 fine_step = PLDO_HIGH_UV_FINE_STEP;
618 vmin = PLDO_HIGH_UV_MIN;
619 }
620
621 return fine_step * vprog + vmin;
622}
623
624static int pm8018_pldo_list_voltage(struct regulator_dev *rdev,
625 unsigned selector)
626{
627 int uV;
628
629 if (selector >= PLDO_SET_POINTS)
630 return 0;
631
632 if (selector < PLDO_LOW_SET_POINTS)
633 uV = selector * PLDO_LOW_UV_FINE_STEP + PLDO_LOW_UV_MIN;
634 else if (selector < (PLDO_LOW_SET_POINTS + PLDO_NORM_SET_POINTS))
635 uV = (selector - PLDO_LOW_SET_POINTS) * PLDO_NORM_UV_FINE_STEP
636 + PLDO_NORM_UV_MIN;
637 else
638 uV = (selector - PLDO_LOW_SET_POINTS - PLDO_NORM_SET_POINTS)
639 * PLDO_HIGH_UV_FINE_STEP
640 + PLDO_HIGH_UV_SET_POINT_MIN;
641
642 return uV;
643}
644
645static int pm8018_pldo_set_voltage(struct regulator_dev *rdev, int min_uV,
646 int max_uV, unsigned *selector)
647{
648 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
649 int rc = 0, uV = min_uV;
650 int vmin;
651 unsigned vprog, fine_step;
652 u8 range_ext, range_sel, fine_step_reg, prev_reg;
653 bool reg_changed = false;
654
655 if (uV < PLDO_LOW_UV_MIN && max_uV >= PLDO_LOW_UV_MIN)
656 uV = PLDO_LOW_UV_MIN;
657
658 if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX) {
659 vreg_err(vreg,
660 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
661 min_uV, max_uV, PLDO_LOW_UV_MIN, PLDO_HIGH_UV_MAX);
662 return -EINVAL;
663 }
664
665 if (uV > PLDO_NORM_UV_MAX) {
666 vmin = PLDO_HIGH_UV_MIN;
667 fine_step = PLDO_HIGH_UV_FINE_STEP;
668 range_ext = LDO_TEST_RANGE_EXT_MASK;
669 range_sel = 0;
670 } else if (uV > PLDO_LOW_UV_MAX) {
671 vmin = PLDO_NORM_UV_MIN;
672 fine_step = PLDO_NORM_UV_FINE_STEP;
673 range_ext = 0;
674 range_sel = 0;
675 } else {
676 vmin = PLDO_LOW_UV_MIN;
677 fine_step = PLDO_LOW_UV_FINE_STEP;
678 range_ext = 0;
679 range_sel = LDO_TEST_RANGE_SEL_MASK;
680 }
681
682 vprog = (uV - vmin + fine_step - 1) / fine_step;
683 uV = vprog * fine_step + vmin;
684 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
685 vprog >>= 1;
686
687 if (uV > max_uV) {
688 vreg_err(vreg,
689 "request v=[%d, %d] cannot be met by any set point\n",
690 min_uV, max_uV);
691 return -EINVAL;
692 }
693
694 mutex_lock(&vreg->pc_lock);
695
696 /* Write fine step, range select and program voltage update. */
697 prev_reg = vreg->test_reg[2];
698 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
699 fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
700 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
701 LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
702 | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
703 &vreg->test_reg[2]);
704 if (rc)
705 goto bail;
706 if (prev_reg != vreg->test_reg[2])
707 reg_changed = true;
708
709 /* Write range extension. */
710 prev_reg = vreg->test_reg[4];
711 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
712 range_ext | REGULATOR_BANK_SEL(4)
713 | REGULATOR_BANK_WRITE,
714 LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
715 &vreg->test_reg[4]);
716 if (rc)
717 goto bail;
718 if (prev_reg != vreg->test_reg[4])
719 reg_changed = true;
720
721 /* Write new voltage. */
722 if (reg_changed) {
723 /*
724 * Force a CTRL register write even if the value hasn't changed.
725 * This is neccessary because range select, range extension, and
726 * fine step will not update until a value is written into the
727 * control register.
728 */
729 rc = pm8018_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
730 vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
731 } else {
732 /* Only write to control register if new value is different. */
733 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
734 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
735 }
736bail:
737 mutex_unlock(&vreg->pc_lock);
738
739 if (rc)
740 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
741 else
742 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_VOLTAGE);
743
744 return rc;
745}
746
747static int pm8018_nldo_get_voltage(struct regulator_dev *rdev)
748{
749 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
750 u8 vprog, fine_step_reg;
751
752 mutex_lock(&vreg->pc_lock);
753
754 fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
755 vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
756
757 mutex_unlock(&vreg->pc_lock);
758
759 vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
760
761 return NLDO_UV_FINE_STEP * vprog + NLDO_UV_MIN;
762}
763
764static int pm8018_nldo_list_voltage(struct regulator_dev *rdev,
765 unsigned selector)
766{
767 if (selector >= NLDO_SET_POINTS)
768 return 0;
769
770 return selector * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
771}
772
773static int pm8018_nldo_set_voltage(struct regulator_dev *rdev, int min_uV,
774 int max_uV, unsigned *selector)
775{
776 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
777 unsigned vprog, fine_step_reg, prev_reg;
778 int rc;
779 int uV = min_uV;
780
781 if (uV < NLDO_UV_MIN && max_uV >= NLDO_UV_MIN)
782 uV = NLDO_UV_MIN;
783
784 if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX) {
785 vreg_err(vreg,
786 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
787 min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
788 return -EINVAL;
789 }
790
791 vprog = (uV - NLDO_UV_MIN + NLDO_UV_FINE_STEP - 1) / NLDO_UV_FINE_STEP;
792 uV = vprog * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
793 fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
794 vprog >>= 1;
795
796 if (uV > max_uV) {
797 vreg_err(vreg,
798 "request v=[%d, %d] cannot be met by any set point\n",
799 min_uV, max_uV);
800 return -EINVAL;
801 }
802
803 mutex_lock(&vreg->pc_lock);
804
805 /* Write fine step. */
806 prev_reg = vreg->test_reg[2];
807 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
808 fine_step_reg | REGULATOR_BANK_SEL(2)
809 | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
810 LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
811 | LDO_TEST_VPROG_UPDATE_MASK,
812 &vreg->test_reg[2]);
813 if (rc)
814 goto bail;
815
816 /* Write new voltage. */
817 if (prev_reg != vreg->test_reg[2]) {
818 /*
819 * Force a CTRL register write even if the value hasn't changed.
820 * This is neccessary because fine step will not update until a
821 * value is written into the control register.
822 */
823 rc = pm8018_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
824 vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
825 } else {
826 /* Only write to control register if new value is different. */
827 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
828 LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
829 }
830bail:
831 mutex_unlock(&vreg->pc_lock);
832
833 if (rc)
834 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
835 else
836 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_VOLTAGE);
837
838 return rc;
839}
840
841static int _pm8018_nldo1200_get_voltage(struct pm8018_vreg *vreg)
842{
843 int uV = 0;
844 int vprog;
845
846 if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
847 pr_warn("%s: currently in legacy mode; voltage unknown.\n",
848 vreg->name);
849 return vreg->save_uV;
850 }
851
852 vprog = vreg->ctrl_reg & NLDO1200_CTRL_VPROG_MASK;
853
854 if ((vreg->ctrl_reg & NLDO1200_CTRL_RANGE_MASK)
855 == NLDO1200_CTRL_RANGE_LOW)
856 uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
857 else
858 uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
859
860 return uV;
861}
862
863static int pm8018_nldo1200_get_voltage(struct regulator_dev *rdev)
864{
865 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
866
867 return _pm8018_nldo1200_get_voltage(vreg);
868}
869
870static int pm8018_nldo1200_list_voltage(struct regulator_dev *rdev,
871 unsigned selector)
872{
873 int uV;
874
875 if (selector >= NLDO1200_SET_POINTS)
876 return 0;
877
878 if (selector < NLDO1200_LOW_SET_POINTS)
879 uV = selector * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
880 else
881 uV = (selector - NLDO1200_LOW_SET_POINTS)
882 * NLDO1200_HIGH_UV_STEP
883 + NLDO1200_HIGH_UV_MIN;
884
885 return uV;
886}
887
888static int _pm8018_nldo1200_set_voltage(struct pm8018_vreg *vreg, int min_uV,
889 int max_uV)
890{
891 u8 vprog, range;
892 int rc;
893 int uV = min_uV;
894
895 if (uV < NLDO1200_LOW_UV_MIN && max_uV >= NLDO1200_LOW_UV_MIN)
896 uV = NLDO1200_LOW_UV_MIN;
897
898 if (uV < NLDO1200_LOW_UV_MIN || uV > NLDO1200_HIGH_UV_MAX) {
899 vreg_err(vreg,
900 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
901 min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
902 return -EINVAL;
903 }
904
905 if (uV > NLDO1200_LOW_UV_MAX) {
906 vprog = (uV - NLDO1200_HIGH_UV_MIN + NLDO1200_HIGH_UV_STEP - 1)
907 / NLDO1200_HIGH_UV_STEP;
908 uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
909 vprog &= NLDO1200_CTRL_VPROG_MASK;
910 range = NLDO1200_CTRL_RANGE_HIGH;
911 } else {
912 vprog = (uV - NLDO1200_LOW_UV_MIN + NLDO1200_LOW_UV_STEP - 1)
913 / NLDO1200_LOW_UV_STEP;
914 uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
915 vprog &= NLDO1200_CTRL_VPROG_MASK;
916 range = NLDO1200_CTRL_RANGE_LOW;
917 }
918
919 if (uV > max_uV) {
920 vreg_err(vreg,
921 "request v=[%d, %d] cannot be met by any set point\n",
922 min_uV, max_uV);
923 return -EINVAL;
924 }
925
926 /* Set to advanced mode */
927 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
928 NLDO1200_ADVANCED_MODE | REGULATOR_BANK_SEL(2)
929 | REGULATOR_BANK_WRITE, NLDO1200_ADVANCED_MODE_MASK
930 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
931 if (rc)
932 goto bail;
933
934 /* Set voltage and range selection. */
935 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, vprog | range,
936 NLDO1200_CTRL_VPROG_MASK | NLDO1200_CTRL_RANGE_MASK,
937 &vreg->ctrl_reg);
938 if (rc)
939 goto bail;
940
941 vreg->save_uV = uV;
942
943bail:
944 if (rc)
945 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
946
947 return rc;
948}
949
950static int pm8018_nldo1200_set_voltage(struct regulator_dev *rdev, int min_uV,
951 int max_uV, unsigned *selector)
952{
953 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
954 int rc;
955
956 rc = _pm8018_nldo1200_set_voltage(vreg, min_uV, max_uV);
957
958 if (!rc)
959 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_VOLTAGE);
960
961 return rc;
962}
963
964static int pm8018_smps_get_voltage_advanced(struct pm8018_vreg *vreg)
965{
966 u8 vprog, band;
967 int uV = 0;
968
969 vprog = vreg->ctrl_reg & SMPS_ADVANCED_VPROG_MASK;
970 band = vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK;
971
972 if (band == SMPS_ADVANCED_BAND_1)
973 uV = vprog * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
974 else if (band == SMPS_ADVANCED_BAND_2)
975 uV = vprog * SMPS_BAND2_UV_STEP + SMPS_BAND2_UV_MIN;
976 else if (band == SMPS_ADVANCED_BAND_3)
977 uV = vprog * SMPS_BAND3_UV_STEP + SMPS_BAND3_UV_MIN;
978 else if (vreg->save_uV > 0)
979 uV = vreg->save_uV;
980 else
981 uV = VOLTAGE_UNKNOWN;
982
983 return uV;
984}
985
986static int pm8018_smps_get_voltage_legacy(struct pm8018_vreg *vreg)
987{
988 u8 vlow, vref, vprog;
989 int uV;
990
991 vlow = vreg->test_reg[1] & SMPS_LEGACY_VLOW_SEL_MASK;
992 vref = vreg->ctrl_reg & SMPS_LEGACY_VREF_SEL_MASK;
993 vprog = vreg->ctrl_reg & SMPS_LEGACY_VPROG_MASK;
994
995 if (vlow && vref) {
996 /* mode 3 */
997 uV = vprog * SMPS_MODE3_UV_STEP + SMPS_MODE3_UV_MIN;
998 } else if (vref) {
999 /* mode 2 */
1000 uV = vprog * SMPS_MODE2_UV_STEP + SMPS_MODE2_UV_MIN;
1001 } else {
1002 /* mode 1 */
1003 uV = vprog * SMPS_MODE1_UV_STEP + SMPS_MODE1_UV_MIN;
1004 }
1005
1006 return uV;
1007}
1008
1009static int _pm8018_smps_get_voltage(struct pm8018_vreg *vreg)
1010{
1011 if (SMPS_IN_ADVANCED_MODE(vreg))
1012 return pm8018_smps_get_voltage_advanced(vreg);
1013
1014 return pm8018_smps_get_voltage_legacy(vreg);
1015}
1016
1017static int pm8018_smps_list_voltage(struct regulator_dev *rdev,
1018 unsigned selector)
1019{
1020 int uV;
1021
1022 if (selector >= SMPS_ADVANCED_SET_POINTS)
1023 return 0;
1024
1025 if (selector < SMPS_BAND1_SET_POINTS)
1026 uV = selector * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
1027 else if (selector < (SMPS_BAND1_SET_POINTS + SMPS_BAND2_SET_POINTS))
1028 uV = (selector - SMPS_BAND1_SET_POINTS) * SMPS_BAND2_UV_STEP
1029 + SMPS_BAND2_UV_MIN;
1030 else
1031 uV = (selector - SMPS_BAND1_SET_POINTS - SMPS_BAND2_SET_POINTS)
1032 * SMPS_BAND3_UV_STEP
1033 + SMPS_BAND3_UV_MIN;
1034
1035 return uV;
1036}
1037
1038static int pm8018_smps_get_voltage(struct regulator_dev *rdev)
1039{
1040 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1041 int uV;
1042
1043 mutex_lock(&vreg->pc_lock);
1044 uV = _pm8018_smps_get_voltage(vreg);
1045 mutex_unlock(&vreg->pc_lock);
1046
1047 return uV;
1048}
1049
1050static int pm8018_smps_set_voltage_advanced(struct pm8018_vreg *vreg,
1051 int min_uV, int max_uV, int force_on)
1052{
1053 u8 vprog, band;
1054 int rc;
1055 int uV = min_uV;
1056
1057 if (uV < SMPS_BAND1_UV_MIN && max_uV >= SMPS_BAND1_UV_MIN)
1058 uV = SMPS_BAND1_UV_MIN;
1059
1060 if (uV < SMPS_BAND1_UV_MIN || uV > SMPS_BAND3_UV_MAX) {
1061 vreg_err(vreg,
1062 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1063 min_uV, max_uV, SMPS_BAND1_UV_MIN, SMPS_BAND3_UV_MAX);
1064 return -EINVAL;
1065 }
1066
1067 if (uV > SMPS_BAND2_UV_MAX) {
1068 vprog = (uV - SMPS_BAND3_UV_MIN + SMPS_BAND3_UV_STEP - 1)
1069 / SMPS_BAND3_UV_STEP;
1070 band = SMPS_ADVANCED_BAND_3;
1071 uV = SMPS_BAND3_UV_MIN + vprog * SMPS_BAND3_UV_STEP;
1072 } else if (uV > SMPS_BAND1_UV_MAX) {
1073 vprog = (uV - SMPS_BAND2_UV_MIN + SMPS_BAND2_UV_STEP - 1)
1074 / SMPS_BAND2_UV_STEP;
1075 band = SMPS_ADVANCED_BAND_2;
1076 uV = SMPS_BAND2_UV_MIN + vprog * SMPS_BAND2_UV_STEP;
1077 } else {
1078 vprog = (uV - SMPS_BAND1_UV_MIN + SMPS_BAND1_UV_STEP - 1)
1079 / SMPS_BAND1_UV_STEP;
1080 band = SMPS_ADVANCED_BAND_1;
1081 uV = SMPS_BAND1_UV_MIN + vprog * SMPS_BAND1_UV_STEP;
1082 }
1083
1084 if (uV > max_uV) {
1085 vreg_err(vreg,
1086 "request v=[%d, %d] cannot be met by any set point\n",
1087 min_uV, max_uV);
1088 return -EINVAL;
1089 }
1090
1091 /* Do not set band if regulator currently disabled. */
1092 if (!_pm8018_vreg_is_enabled(vreg) && !force_on)
1093 band = SMPS_ADVANCED_BAND_OFF;
1094
1095 /* Set advanced mode bit to 1. */
1096 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr, SMPS_ADVANCED_MODE
1097 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
1098 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
1099 &vreg->test_reg[7]);
1100 if (rc)
1101 goto bail;
1102
1103 /* Set voltage and voltage band. */
1104 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, band | vprog,
1105 SMPS_ADVANCED_BAND_MASK | SMPS_ADVANCED_VPROG_MASK,
1106 &vreg->ctrl_reg);
1107 if (rc)
1108 goto bail;
1109
1110 vreg->save_uV = uV;
1111
1112bail:
1113 if (rc)
1114 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1115
1116 return rc;
1117}
1118
1119static int pm8018_smps_set_voltage_legacy(struct pm8018_vreg *vreg, int min_uV,
1120 int max_uV)
1121{
1122 u8 vlow, vref, vprog, pd, en;
1123 int rc;
1124 int uV = min_uV;
1125
1126
1127 if (uV < SMPS_MODE3_UV_MIN && max_uV >= SMPS_MODE3_UV_MIN)
1128 uV = SMPS_MODE3_UV_MIN;
1129
1130 if (uV < SMPS_MODE3_UV_MIN || uV > SMPS_MODE1_UV_MAX) {
1131 vreg_err(vreg,
1132 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1133 min_uV, max_uV, SMPS_MODE3_UV_MIN, SMPS_MODE1_UV_MAX);
1134 return -EINVAL;
1135 }
1136
1137 if (uV > SMPS_MODE2_UV_MAX) {
1138 vprog = (uV - SMPS_MODE1_UV_MIN + SMPS_MODE1_UV_STEP - 1)
1139 / SMPS_MODE1_UV_STEP;
1140 vref = 0;
1141 vlow = 0;
1142 uV = SMPS_MODE1_UV_MIN + vprog * SMPS_MODE1_UV_STEP;
1143 } else if (uV > SMPS_MODE3_UV_MAX) {
1144 vprog = (uV - SMPS_MODE2_UV_MIN + SMPS_MODE2_UV_STEP - 1)
1145 / SMPS_MODE2_UV_STEP;
1146 vref = SMPS_LEGACY_VREF_SEL_MASK;
1147 vlow = 0;
1148 uV = SMPS_MODE2_UV_MIN + vprog * SMPS_MODE2_UV_STEP;
1149 } else {
1150 vprog = (uV - SMPS_MODE3_UV_MIN + SMPS_MODE3_UV_STEP - 1)
1151 / SMPS_MODE3_UV_STEP;
1152 vref = SMPS_LEGACY_VREF_SEL_MASK;
1153 vlow = SMPS_LEGACY_VLOW_SEL_MASK;
1154 uV = SMPS_MODE3_UV_MIN + vprog * SMPS_MODE3_UV_STEP;
1155 }
1156
1157 if (uV > max_uV) {
1158 vreg_err(vreg,
1159 "request v=[%d, %d] cannot be met by any set point\n",
1160 min_uV, max_uV);
1161 return -EINVAL;
1162 }
1163
1164 /* set vlow bit for ultra low voltage mode */
1165 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1166 vlow | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1),
1167 REGULATOR_BANK_MASK | SMPS_LEGACY_VLOW_SEL_MASK,
1168 &vreg->test_reg[1]);
1169 if (rc)
1170 goto bail;
1171
1172 /* Set advanced mode bit to 0. */
1173 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr, SMPS_LEGACY_MODE
1174 | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
1175 SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
1176 &vreg->test_reg[7]);
1177 if (rc)
1178 goto bail;
1179
1180 en = (_pm8018_vreg_is_enabled(vreg) ? SMPS_LEGACY_ENABLE : 0);
1181 pd = (vreg->pdata.pull_down_enable ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0);
1182
1183 /* Set voltage (and the rest of the control register). */
1184 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1185 en | pd | vref | vprog,
1186 SMPS_LEGACY_ENABLE_MASK | SMPS_LEGACY_PULL_DOWN_ENABLE
1187 | SMPS_LEGACY_VREF_SEL_MASK | SMPS_LEGACY_VPROG_MASK,
1188 &vreg->ctrl_reg);
1189
1190 vreg->save_uV = uV;
1191
1192bail:
1193 if (rc)
1194 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1195
1196 return rc;
1197}
1198
1199static int pm8018_smps_set_voltage(struct regulator_dev *rdev, int min_uV,
1200 int max_uV, unsigned *selector)
1201{
1202 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1203 int rc = 0;
1204
1205 mutex_lock(&vreg->pc_lock);
1206
1207 if (SMPS_IN_ADVANCED_MODE(vreg) || !pm8018_vreg_is_pin_controlled(vreg))
1208 rc = pm8018_smps_set_voltage_advanced(vreg, min_uV, max_uV, 0);
1209 else
1210 rc = pm8018_smps_set_voltage_legacy(vreg, min_uV, max_uV);
1211
1212 mutex_unlock(&vreg->pc_lock);
1213
1214 if (!rc)
1215 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_VOLTAGE);
1216
1217 return rc;
1218}
1219
1220static unsigned int pm8018_ldo_get_mode(struct regulator_dev *rdev)
1221{
1222 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1223 unsigned int mode = 0;
1224
1225 mutex_lock(&vreg->pc_lock);
1226 mode = vreg->mode;
1227 mutex_unlock(&vreg->pc_lock);
1228
1229 return mode;
1230}
1231
1232static int pm8018_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
1233{
1234 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1235 int rc = 0;
1236
1237 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1238 vreg_err(vreg, "invalid mode: %u\n", mode);
1239 return -EINVAL;
1240 }
1241
1242 mutex_lock(&vreg->pc_lock);
1243
1244 if (mode == REGULATOR_MODE_NORMAL
1245 || (vreg->is_enabled_pc
1246 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE)) {
1247 /* HPM */
1248 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1249 LDO_CTRL_PM_HPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1250 } else {
1251 /* LPM */
1252 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1253 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1254 if (rc)
1255 goto bail;
1256
1257 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1258 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1259 | REGULATOR_BANK_SEL(0),
1260 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
1261 &vreg->test_reg[0]);
1262 }
1263
1264bail:
1265 if (!rc)
1266 vreg->mode = mode;
1267
1268 mutex_unlock(&vreg->pc_lock);
1269
1270 if (rc)
1271 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1272 else
1273 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_MODE);
1274
1275 return rc;
1276}
1277
1278static unsigned int pm8018_nldo1200_get_mode(struct regulator_dev *rdev)
1279{
1280 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1281 unsigned int mode = 0;
1282
1283 if (NLDO1200_IN_ADVANCED_MODE(vreg)) {
1284 /* Advanced mode */
1285 if ((vreg->test_reg[2] & NLDO1200_ADVANCED_PM_MASK)
1286 == NLDO1200_ADVANCED_PM_LPM)
1287 mode = REGULATOR_MODE_IDLE;
1288 else
1289 mode = REGULATOR_MODE_NORMAL;
1290 } else {
1291 /* Legacy mode */
1292 if ((vreg->ctrl_reg & NLDO1200_LEGACY_PM_MASK)
1293 == NLDO1200_LEGACY_PM_LPM)
1294 mode = REGULATOR_MODE_IDLE;
1295 else
1296 mode = REGULATOR_MODE_NORMAL;
1297 }
1298
1299 return mode;
1300}
1301
1302static int pm8018_nldo1200_set_mode(struct regulator_dev *rdev,
1303 unsigned int mode)
1304{
1305 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1306 int rc = 0;
1307
1308 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1309 vreg_err(vreg, "invalid mode: %u\n", mode);
1310 return -EINVAL;
1311 }
1312
1313 /*
1314 * Make sure that advanced mode is in use. If it isn't, then set it
1315 * and update the voltage accordingly.
1316 */
1317 if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
1318 rc = _pm8018_nldo1200_set_voltage(vreg, vreg->save_uV,
1319 vreg->save_uV);
1320 if (rc)
1321 goto bail;
1322 }
1323
1324 if (mode == REGULATOR_MODE_NORMAL) {
1325 /* HPM */
1326 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1327 NLDO1200_ADVANCED_PM_HPM | REGULATOR_BANK_WRITE
1328 | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
1329 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
1330 } else {
1331 /* LPM */
1332 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1333 NLDO1200_ADVANCED_PM_LPM | REGULATOR_BANK_WRITE
1334 | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
1335 | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
1336 }
1337
1338bail:
1339 if (rc)
1340 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1341 else
1342 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_MODE);
1343
1344 return rc;
1345}
1346
1347static unsigned int pm8018_smps_get_mode(struct regulator_dev *rdev)
1348{
1349 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1350 unsigned int mode = 0;
1351
1352 mutex_lock(&vreg->pc_lock);
1353 mode = vreg->mode;
1354 mutex_unlock(&vreg->pc_lock);
1355
1356 return mode;
1357}
1358
1359static int pm8018_smps_set_mode(struct regulator_dev *rdev, unsigned int mode)
1360{
1361 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1362 int rc = 0;
1363
1364 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
1365 vreg_err(vreg, "invalid mode: %u\n", mode);
1366 return -EINVAL;
1367 }
1368
1369 mutex_lock(&vreg->pc_lock);
1370
1371 if (mode == REGULATOR_MODE_NORMAL
1372 || (vreg->is_enabled_pc
1373 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE)) {
1374 /* HPM */
1375 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1376 SMPS_CLK_CTRL_PWM, SMPS_CLK_CTRL_MASK,
1377 &vreg->clk_ctrl_reg);
1378 } else {
1379 /* LPM */
1380 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1381 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
1382 &vreg->clk_ctrl_reg);
1383 }
1384
1385 if (!rc)
1386 vreg->mode = mode;
1387
1388 mutex_unlock(&vreg->pc_lock);
1389
1390 if (rc)
1391 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1392 else
1393 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_MODE);
1394
1395 return rc;
1396}
1397
1398static unsigned int pm8018_vreg_get_optimum_mode(struct regulator_dev *rdev,
1399 int input_uV, int output_uV, int load_uA)
1400{
1401 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1402 unsigned int mode;
1403
1404 if (load_uA + vreg->pdata.system_uA >= vreg->hpm_min_load)
1405 mode = REGULATOR_MODE_NORMAL;
1406 else
1407 mode = REGULATOR_MODE_IDLE;
1408
1409 return mode;
1410}
1411
1412static int pm8018_ldo_enable(struct regulator_dev *rdev)
1413{
1414 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1415 int rc, val;
1416
1417 mutex_lock(&vreg->pc_lock);
1418
1419 /*
1420 * Choose HPM if previously set to HPM or if pin control is enabled in
1421 * on/off mode.
1422 */
1423 val = LDO_CTRL_PM_LPM;
1424 if (vreg->mode == REGULATOR_MODE_NORMAL
1425 || (vreg->is_enabled_pc
1426 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE))
1427 val = LDO_CTRL_PM_HPM;
1428
1429 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, val | LDO_ENABLE,
1430 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1431
1432 if (!rc)
1433 vreg->is_enabled = true;
1434
1435 mutex_unlock(&vreg->pc_lock);
1436
1437 if (rc)
1438 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1439 else
1440 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_ENABLE);
1441
1442 return rc;
1443}
1444
1445static int pm8018_ldo_disable(struct regulator_dev *rdev)
1446{
1447 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1448 int rc;
1449
1450 mutex_lock(&vreg->pc_lock);
1451
1452 /*
1453 * Only disable the regulator if it isn't still required for HPM/LPM
1454 * pin control.
1455 */
1456 if (!vreg->is_enabled_pc
1457 || vreg->pdata.pin_fn != PM8018_VREG_PIN_FN_MODE) {
1458 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1459 LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
1460 if (rc)
1461 goto bail;
1462 }
1463
1464 /* Change to LPM if HPM/LPM pin control is enabled. */
1465 if (vreg->is_enabled_pc
1466 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_MODE) {
1467 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1468 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1469 if (rc)
1470 goto bail;
1471
1472 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1473 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1474 | REGULATOR_BANK_SEL(0),
1475 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
1476 &vreg->test_reg[0]);
1477 }
1478
1479 if (!rc)
1480 vreg->is_enabled = false;
1481bail:
1482 mutex_unlock(&vreg->pc_lock);
1483
1484 if (rc)
1485 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1486 else
1487 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_DISABLE);
1488
1489 return rc;
1490}
1491
1492static int pm8018_nldo1200_enable(struct regulator_dev *rdev)
1493{
1494 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1495 int rc;
1496
1497 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_ENABLE,
1498 NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
1499
1500 if (rc)
1501 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1502 else
1503 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_ENABLE);
1504
1505 return rc;
1506}
1507
1508static int pm8018_nldo1200_disable(struct regulator_dev *rdev)
1509{
1510 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1511 int rc;
1512
1513 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_DISABLE,
1514 NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
1515
1516 if (rc)
1517 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1518 else
1519 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_DISABLE);
1520
1521 return rc;
1522}
1523
1524static int pm8018_smps_enable(struct regulator_dev *rdev)
1525{
1526 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1527 int rc = 0;
1528 int val;
1529
1530 mutex_lock(&vreg->pc_lock);
1531
1532 if (SMPS_IN_ADVANCED_MODE(vreg)
1533 || !pm8018_vreg_is_pin_controlled(vreg)) {
1534 /* Enable in advanced mode if not using pin control. */
1535 rc = pm8018_smps_set_voltage_advanced(vreg, vreg->save_uV,
1536 vreg->save_uV, 1);
1537 } else {
1538 rc = pm8018_smps_set_voltage_legacy(vreg, vreg->save_uV,
1539 vreg->save_uV);
1540 if (rc)
1541 goto bail;
1542
1543 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1544 SMPS_LEGACY_ENABLE, SMPS_LEGACY_ENABLE_MASK,
1545 &vreg->ctrl_reg);
1546 }
1547
1548 /*
1549 * Choose HPM if previously set to HPM or if pin control is enabled in
1550 * on/off mode.
1551 */
1552 val = SMPS_CLK_CTRL_PFM;
1553 if (vreg->mode == REGULATOR_MODE_NORMAL
1554 || (vreg->is_enabled_pc
1555 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE))
1556 val = SMPS_CLK_CTRL_PWM;
1557
1558 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr, val,
1559 SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
1560
1561 if (!rc)
1562 vreg->is_enabled = true;
1563bail:
1564 mutex_unlock(&vreg->pc_lock);
1565
1566 if (rc)
1567 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1568 else
1569 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_ENABLE);
1570
1571 return rc;
1572}
1573
1574static int pm8018_smps_disable(struct regulator_dev *rdev)
1575{
1576 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1577 int rc;
1578
1579 mutex_lock(&vreg->pc_lock);
1580
1581 if (SMPS_IN_ADVANCED_MODE(vreg)) {
1582 /* Change SMPS to legacy mode before disabling. */
1583 rc = pm8018_smps_set_voltage_legacy(vreg, vreg->save_uV,
1584 vreg->save_uV);
1585 if (rc)
1586 goto bail;
1587 }
1588
1589 /*
1590 * Only disable the regulator if it isn't still required for HPM/LPM
1591 * pin control.
1592 */
1593 if (!vreg->is_enabled_pc
1594 || vreg->pdata.pin_fn != PM8018_VREG_PIN_FN_MODE) {
1595 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1596 SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
1597 &vreg->ctrl_reg);
1598 if (rc)
1599 goto bail;
1600 }
1601
1602 /* Change to LPM if HPM/LPM pin control is enabled. */
1603 if (vreg->is_enabled_pc
1604 && vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_MODE)
1605 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1606 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
1607 &vreg->clk_ctrl_reg);
1608
1609 if (!rc)
1610 vreg->is_enabled = false;
1611
1612bail:
1613 mutex_unlock(&vreg->pc_lock);
1614
1615 if (rc)
1616 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1617 else
1618 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_DISABLE);
1619
1620 return rc;
1621}
1622
1623static int pm8018_vs_enable(struct regulator_dev *rdev)
1624{
1625 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1626 int rc;
1627
1628 mutex_lock(&vreg->pc_lock);
1629
1630 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, VS_ENABLE,
1631 VS_ENABLE_MASK, &vreg->ctrl_reg);
1632
1633 if (!rc)
1634 vreg->is_enabled = true;
1635
1636 mutex_unlock(&vreg->pc_lock);
1637
1638 if (rc)
1639 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1640 else
1641 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_ENABLE);
1642
1643 return rc;
1644}
1645
1646static int pm8018_vs_disable(struct regulator_dev *rdev)
1647{
1648 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1649 int rc;
1650
1651 mutex_lock(&vreg->pc_lock);
1652
1653 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, VS_DISABLE,
1654 VS_ENABLE_MASK, &vreg->ctrl_reg);
1655
1656 if (!rc)
1657 vreg->is_enabled = false;
1658
1659 mutex_unlock(&vreg->pc_lock);
1660
1661 if (rc)
1662 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1663 else
1664 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_DISABLE);
1665
1666 return rc;
1667}
1668
1669static int pm8018_ldo_pin_control_enable(struct regulator_dev *rdev)
1670{
1671 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1672 int rc = 0;
1673 int bank;
1674 u8 val = 0;
1675 u8 mask;
1676
1677 mutex_lock(&vreg->pc_lock);
1678
1679 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_D1)
1680 val |= LDO_TEST_PIN_CTRL_EN0;
1681 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A0)
1682 val |= LDO_TEST_PIN_CTRL_EN1;
1683 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A1)
1684 val |= LDO_TEST_PIN_CTRL_EN2;
1685 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A2)
1686 val |= LDO_TEST_PIN_CTRL_EN3;
1687
1688 bank = (vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE ? 5 : 6);
1689 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1690 val | REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
1691 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
1692 &vreg->test_reg[bank]);
1693 if (rc)
1694 goto bail;
1695
1696 /* Unset pin control bits in unused bank. */
1697 bank = (bank == 5 ? 6 : 5);
1698 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1699 REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
1700 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
1701 &vreg->test_reg[bank]);
1702 if (rc)
1703 goto bail;
1704
1705 val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1706 | REGULATOR_BANK_SEL(0);
1707 mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
1708 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr, val, mask,
1709 &vreg->test_reg[0]);
1710 if (rc)
1711 goto bail;
1712
1713 if (vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE) {
1714 /* Pin control ON/OFF */
1715 val = LDO_CTRL_PM_HPM;
1716 /* Leave physically enabled if already enabled. */
1717 val |= (vreg->is_enabled ? LDO_ENABLE : LDO_DISABLE);
1718 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, val,
1719 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1720 if (rc)
1721 goto bail;
1722 } else {
1723 /* Pin control LPM/HPM */
1724 val = LDO_ENABLE;
1725 /* Leave in HPM if already enabled in HPM. */
1726 val |= (vreg->is_enabled && vreg->mode == REGULATOR_MODE_NORMAL
1727 ? LDO_CTRL_PM_HPM : LDO_CTRL_PM_LPM);
1728 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, val,
1729 LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1730 if (rc)
1731 goto bail;
1732 }
1733
1734bail:
1735 if (!rc)
1736 vreg->is_enabled_pc = true;
1737
1738 mutex_unlock(&vreg->pc_lock);
1739
1740 if (rc)
1741 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1742 else
1743 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1744
1745 return rc;
1746}
1747
1748static int pm8018_ldo_pin_control_disable(struct regulator_dev *rdev)
1749{
1750 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1751 int rc;
1752
1753 mutex_lock(&vreg->pc_lock);
1754
1755 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1756 REGULATOR_BANK_SEL(5) | REGULATOR_BANK_WRITE,
1757 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
1758 &vreg->test_reg[5]);
1759 if (rc)
1760 goto bail;
1761
1762 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1763 REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
1764 LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
1765 &vreg->test_reg[6]);
1766
1767 /*
1768 * Physically disable the regulator if it was enabled in HPM/LPM pin
1769 * control mode previously and it logically should not be enabled.
1770 */
1771 if ((vreg->ctrl_reg & LDO_ENABLE_MASK) == LDO_ENABLE
1772 && !vreg->is_enabled) {
1773 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1774 LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
1775 if (rc)
1776 goto bail;
1777 }
1778
1779 /* Change to LPM if LPM was enabled. */
1780 if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
1781 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1782 LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
1783 if (rc)
1784 goto bail;
1785
1786 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
1787 LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
1788 | REGULATOR_BANK_SEL(0),
1789 LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
1790 &vreg->test_reg[0]);
1791 if (rc)
1792 goto bail;
1793 }
1794
1795bail:
1796 if (!rc)
1797 vreg->is_enabled_pc = false;
1798
1799 mutex_unlock(&vreg->pc_lock);
1800
1801 if (rc)
1802 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1803 else
1804 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1805
1806 return rc;
1807}
1808
1809static int pm8018_smps_pin_control_enable(struct regulator_dev *rdev)
1810{
1811 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1812 int rc = 0;
1813 u8 val = 0;
1814
1815 mutex_lock(&vreg->pc_lock);
1816
1817 if (vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE) {
1818 /* Pin control ON/OFF */
1819 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_D1)
1820 val |= SMPS_PIN_CTRL_EN0;
1821 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A0)
1822 val |= SMPS_PIN_CTRL_EN1;
1823 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A1)
1824 val |= SMPS_PIN_CTRL_EN2;
1825 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A2)
1826 val |= SMPS_PIN_CTRL_EN3;
1827 } else {
1828 /* Pin control LPM/HPM */
1829 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_D1)
1830 val |= SMPS_PIN_CTRL_LPM_EN0;
1831 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A0)
1832 val |= SMPS_PIN_CTRL_LPM_EN1;
1833 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A1)
1834 val |= SMPS_PIN_CTRL_LPM_EN2;
1835 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A2)
1836 val |= SMPS_PIN_CTRL_LPM_EN3;
1837 }
1838
1839 rc = pm8018_smps_set_voltage_legacy(vreg, vreg->save_uV, vreg->save_uV);
1840 if (rc)
1841 goto bail;
1842
1843 rc = pm8018_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, val,
1844 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
1845 &vreg->sleep_ctrl_reg);
1846 if (rc)
1847 goto bail;
1848
1849 /*
1850 * Physically enable the regulator if using HPM/LPM pin control mode or
1851 * if the regulator should be logically left on.
1852 */
1853 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1854 ((vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_MODE
1855 || vreg->is_enabled) ?
1856 SMPS_LEGACY_ENABLE : SMPS_LEGACY_DISABLE),
1857 SMPS_LEGACY_ENABLE_MASK, &vreg->ctrl_reg);
1858 if (rc)
1859 goto bail;
1860
1861 /*
1862 * Set regulator to HPM if using on/off pin control or if the regulator
1863 * is already enabled in HPM. Otherwise, set it to LPM.
1864 */
1865 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1866 (vreg->pdata.pin_fn == PM8018_VREG_PIN_FN_ENABLE
1867 || (vreg->is_enabled
1868 && vreg->mode == REGULATOR_MODE_NORMAL)
1869 ? SMPS_CLK_CTRL_PWM : SMPS_CLK_CTRL_PFM),
1870 SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
1871
1872bail:
1873 if (!rc)
1874 vreg->is_enabled_pc = true;
1875
1876 mutex_unlock(&vreg->pc_lock);
1877
1878 if (rc)
1879 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1880 else
1881 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1882
1883 return rc;
1884}
1885
1886static int pm8018_smps_pin_control_disable(struct regulator_dev *rdev)
1887{
1888 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1889 int rc;
1890
1891 mutex_lock(&vreg->pc_lock);
1892
1893 rc = pm8018_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, 0,
1894 SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
1895 &vreg->sleep_ctrl_reg);
1896 if (rc)
1897 goto bail;
1898
1899 /*
1900 * Physically disable the regulator if it was enabled in HPM/LPM pin
1901 * control mode previously and it logically should not be enabled.
1902 */
1903 if ((vreg->ctrl_reg & SMPS_LEGACY_ENABLE_MASK) == SMPS_LEGACY_ENABLE
1904 && vreg->is_enabled == false) {
1905 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
1906 SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
1907 &vreg->ctrl_reg);
1908 if (rc)
1909 goto bail;
1910 }
1911
1912 /* Change to LPM if LPM was enabled. */
1913 if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
1914 rc = pm8018_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
1915 SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
1916 &vreg->clk_ctrl_reg);
1917 if (rc)
1918 goto bail;
1919 }
1920
1921 rc = pm8018_smps_set_voltage_advanced(vreg, vreg->save_uV,
1922 vreg->save_uV, 0);
1923
1924bail:
1925 if (!rc)
1926 vreg->is_enabled_pc = false;
1927
1928 mutex_unlock(&vreg->pc_lock);
1929
1930 if (rc)
1931 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1932 else
1933 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1934
1935 return rc;
1936}
1937
1938static int pm8018_vs_pin_control_enable(struct regulator_dev *rdev)
1939{
1940 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1941 int rc;
1942 u8 val = 0;
1943
1944 mutex_lock(&vreg->pc_lock);
1945
1946 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_D1)
1947 val |= VS_PIN_CTRL_EN0;
1948 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A0)
1949 val |= VS_PIN_CTRL_EN1;
1950 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A1)
1951 val |= VS_PIN_CTRL_EN2;
1952 if (vreg->pdata.pin_ctrl & PM8018_VREG_PIN_CTRL_A2)
1953 val |= VS_PIN_CTRL_EN3;
1954
1955 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, val,
1956 VS_PIN_CTRL_MASK | VS_ENABLE_MASK, &vreg->ctrl_reg);
1957
1958 if (!rc)
1959 vreg->is_enabled_pc = true;
1960
1961 mutex_unlock(&vreg->pc_lock);
1962
1963 if (rc)
1964 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1965 else
1966 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1967
1968 return rc;
1969}
1970
1971static int pm8018_vs_pin_control_disable(struct regulator_dev *rdev)
1972{
1973 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1974 int rc;
1975
1976 mutex_lock(&vreg->pc_lock);
1977
1978 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr, 0,
1979 VS_PIN_CTRL_MASK, &vreg->ctrl_reg);
1980
1981 if (!rc)
1982 vreg->is_enabled_pc = false;
1983
1984 mutex_unlock(&vreg->pc_lock);
1985
1986 if (rc)
1987 vreg_err(vreg, "pm8018_vreg_masked_write failed, rc=%d\n", rc);
1988 else
1989 pm8018_vreg_show_state(rdev, PM8018_REGULATOR_ACTION_PIN_CTRL);
1990
1991 return rc;
1992}
1993
1994static int pm8018_enable_time(struct regulator_dev *rdev)
1995{
1996 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
1997
1998 return vreg->pdata.enable_time;
1999}
2000
2001static const char const *pm8018_print_actions[] = {
2002 [PM8018_REGULATOR_ACTION_INIT] = "initial ",
2003 [PM8018_REGULATOR_ACTION_ENABLE] = "enable ",
2004 [PM8018_REGULATOR_ACTION_DISABLE] = "disable ",
2005 [PM8018_REGULATOR_ACTION_VOLTAGE] = "set voltage",
2006 [PM8018_REGULATOR_ACTION_MODE] = "set mode ",
2007 [PM8018_REGULATOR_ACTION_PIN_CTRL] = "pin control",
2008};
2009
2010static void pm8018_vreg_show_state(struct regulator_dev *rdev,
2011 enum pm8018_regulator_action action)
2012{
2013 struct pm8018_vreg *vreg = rdev_get_drvdata(rdev);
2014 int uV, pc;
2015 unsigned int mode;
2016 const char *pc_en0 = "", *pc_en1 = "", *pc_en2 = "", *pc_en3 = "";
2017 const char *pc_total = "";
2018 const char *action_label = pm8018_print_actions[action];
2019 const char *enable_label;
2020
2021 mutex_lock(&vreg->pc_lock);
2022
2023 /*
2024 * Do not print unless REQUEST is specified and SSBI writes have taken
2025 * place, or DUPLICATE is specified.
2026 */
2027 if (!((pm8018_vreg_debug_mask & PM8018_VREG_DEBUG_DUPLICATE)
2028 || ((pm8018_vreg_debug_mask & PM8018_VREG_DEBUG_REQUEST)
2029 && (vreg->write_count != vreg->prev_write_count)))) {
2030 mutex_unlock(&vreg->pc_lock);
2031 return;
2032 }
2033
2034 vreg->prev_write_count = vreg->write_count;
2035
2036 pc = vreg->pdata.pin_ctrl;
2037 if (vreg->is_enabled_pc) {
2038 if (pc & PM8018_VREG_PIN_CTRL_D1)
2039 pc_en0 = " D1";
2040 if (pc & PM8018_VREG_PIN_CTRL_A0)
2041 pc_en1 = " A0";
2042 if (pc & PM8018_VREG_PIN_CTRL_A1)
2043 pc_en2 = " A1";
2044 if (pc & PM8018_VREG_PIN_CTRL_A2)
2045 pc_en3 = " A2";
2046 if (pc == PM8018_VREG_PIN_CTRL_NONE)
2047 pc_total = " none";
2048 } else {
2049 pc_total = " none";
2050 }
2051
2052 mutex_unlock(&vreg->pc_lock);
2053
2054 enable_label = pm8018_vreg_is_enabled(rdev) ? "on " : "off";
2055
2056 switch (vreg->type) {
2057 case REGULATOR_TYPE_PLDO:
2058 uV = pm8018_pldo_get_voltage(rdev);
2059 mode = pm8018_ldo_get_mode(rdev);
2060 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2061 action_label, vreg->name, enable_label, uV,
2062 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2063 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2064 break;
2065 case REGULATOR_TYPE_NLDO:
2066 uV = pm8018_nldo_get_voltage(rdev);
2067 mode = pm8018_ldo_get_mode(rdev);
2068 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2069 action_label, vreg->name, enable_label, uV,
2070 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2071 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2072 break;
2073 case REGULATOR_TYPE_NLDO1200:
2074 uV = pm8018_nldo1200_get_voltage(rdev);
2075 mode = pm8018_nldo1200_get_mode(rdev);
2076 pr_info("%s %-9s: %s, v=%7d uV, mode=%s\n",
2077 action_label, vreg->name, enable_label, uV,
2078 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"));
2079 break;
2080 case REGULATOR_TYPE_SMPS:
2081 uV = pm8018_smps_get_voltage(rdev);
2082 mode = pm8018_smps_get_mode(rdev);
2083 pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
2084 action_label, vreg->name, enable_label, uV,
2085 (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
2086 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2087 break;
2088 case REGULATOR_TYPE_VS:
2089 pr_info("%s %-9s: %s, pc=%s%s%s%s%s\n",
2090 action_label, vreg->name, enable_label,
2091 pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
2092 break;
2093 default:
2094 break;
2095 }
2096}
2097
2098/* Real regulator operations. */
2099static struct regulator_ops pm8018_pldo_ops = {
2100 .enable = pm8018_ldo_enable,
2101 .disable = pm8018_ldo_disable,
2102 .is_enabled = pm8018_vreg_is_enabled,
2103 .set_voltage = pm8018_pldo_set_voltage,
2104 .get_voltage = pm8018_pldo_get_voltage,
2105 .list_voltage = pm8018_pldo_list_voltage,
2106 .set_mode = pm8018_ldo_set_mode,
2107 .get_mode = pm8018_ldo_get_mode,
2108 .get_optimum_mode = pm8018_vreg_get_optimum_mode,
2109 .enable_time = pm8018_enable_time,
2110};
2111
2112static struct regulator_ops pm8018_nldo_ops = {
2113 .enable = pm8018_ldo_enable,
2114 .disable = pm8018_ldo_disable,
2115 .is_enabled = pm8018_vreg_is_enabled,
2116 .set_voltage = pm8018_nldo_set_voltage,
2117 .get_voltage = pm8018_nldo_get_voltage,
2118 .list_voltage = pm8018_nldo_list_voltage,
2119 .set_mode = pm8018_ldo_set_mode,
2120 .get_mode = pm8018_ldo_get_mode,
2121 .get_optimum_mode = pm8018_vreg_get_optimum_mode,
2122 .enable_time = pm8018_enable_time,
2123};
2124
2125static struct regulator_ops pm8018_nldo1200_ops = {
2126 .enable = pm8018_nldo1200_enable,
2127 .disable = pm8018_nldo1200_disable,
2128 .is_enabled = pm8018_vreg_is_enabled,
2129 .set_voltage = pm8018_nldo1200_set_voltage,
2130 .get_voltage = pm8018_nldo1200_get_voltage,
2131 .list_voltage = pm8018_nldo1200_list_voltage,
2132 .set_mode = pm8018_nldo1200_set_mode,
2133 .get_mode = pm8018_nldo1200_get_mode,
2134 .get_optimum_mode = pm8018_vreg_get_optimum_mode,
2135 .enable_time = pm8018_enable_time,
2136};
2137
2138static struct regulator_ops pm8018_smps_ops = {
2139 .enable = pm8018_smps_enable,
2140 .disable = pm8018_smps_disable,
2141 .is_enabled = pm8018_vreg_is_enabled,
2142 .set_voltage = pm8018_smps_set_voltage,
2143 .get_voltage = pm8018_smps_get_voltage,
2144 .list_voltage = pm8018_smps_list_voltage,
2145 .set_mode = pm8018_smps_set_mode,
2146 .get_mode = pm8018_smps_get_mode,
2147 .get_optimum_mode = pm8018_vreg_get_optimum_mode,
2148 .enable_time = pm8018_enable_time,
2149};
2150
2151static struct regulator_ops pm8018_vs_ops = {
2152 .enable = pm8018_vs_enable,
2153 .disable = pm8018_vs_disable,
2154 .is_enabled = pm8018_vreg_is_enabled,
2155 .enable_time = pm8018_enable_time,
2156};
2157
2158/* Pin control regulator operations. */
2159static struct regulator_ops pm8018_ldo_pc_ops = {
2160 .enable = pm8018_ldo_pin_control_enable,
2161 .disable = pm8018_ldo_pin_control_disable,
2162 .is_enabled = pm8018_vreg_pin_control_is_enabled,
2163};
2164
2165static struct regulator_ops pm8018_smps_pc_ops = {
2166 .enable = pm8018_smps_pin_control_enable,
2167 .disable = pm8018_smps_pin_control_disable,
2168 .is_enabled = pm8018_vreg_pin_control_is_enabled,
2169};
2170
2171static struct regulator_ops pm8018_vs_pc_ops = {
2172 .enable = pm8018_vs_pin_control_enable,
2173 .disable = pm8018_vs_pin_control_disable,
2174 .is_enabled = pm8018_vreg_pin_control_is_enabled,
2175};
2176
2177#define VREG_DESC(_id, _name, _ops, _n_voltages) \
2178 [PM8018_VREG_ID_##_id] = { \
2179 .id = PM8018_VREG_ID_##_id, \
2180 .name = _name, \
2181 .n_voltages = _n_voltages, \
2182 .ops = _ops, \
2183 .type = REGULATOR_VOLTAGE, \
2184 .owner = THIS_MODULE, \
2185 }
2186
2187static struct regulator_desc pm8018_vreg_description[] = {
2188 VREG_DESC(L2, "8018_l2", &pm8018_pldo_ops, PLDO_SET_POINTS),
2189 VREG_DESC(L3, "8018_l3", &pm8018_pldo_ops, PLDO_SET_POINTS),
2190 VREG_DESC(L4, "8018_l4", &pm8018_pldo_ops, PLDO_SET_POINTS),
2191 VREG_DESC(L5, "8018_l5", &pm8018_pldo_ops, PLDO_SET_POINTS),
2192 VREG_DESC(L6, "8018_l6", &pm8018_pldo_ops, PLDO_SET_POINTS),
2193 VREG_DESC(L7, "8018_l7", &pm8018_pldo_ops, PLDO_SET_POINTS),
2194 VREG_DESC(L8, "8018_l8", &pm8018_nldo_ops, NLDO_SET_POINTS),
2195 VREG_DESC(L9, "8018_l9", &pm8018_nldo1200_ops, NLDO1200_SET_POINTS),
2196 VREG_DESC(L10, "8018_l10", &pm8018_nldo1200_ops, NLDO1200_SET_POINTS),
2197 VREG_DESC(L11, "8018_l11", &pm8018_nldo1200_ops, NLDO1200_SET_POINTS),
2198 VREG_DESC(L12, "8018_l12", &pm8018_nldo1200_ops, NLDO1200_SET_POINTS),
2199 VREG_DESC(L13, "8018_l13", &pm8018_pldo_ops, PLDO_SET_POINTS),
2200 VREG_DESC(L14, "8018_l14", &pm8018_pldo_ops, PLDO_SET_POINTS),
2201
2202 VREG_DESC(S1, "8018_s1", &pm8018_smps_ops, SMPS_ADVANCED_SET_POINTS),
2203 VREG_DESC(S2, "8018_s2", &pm8018_smps_ops, SMPS_ADVANCED_SET_POINTS),
2204 VREG_DESC(S3, "8018_s3", &pm8018_smps_ops, SMPS_ADVANCED_SET_POINTS),
2205 VREG_DESC(S4, "8018_s4", &pm8018_smps_ops, SMPS_ADVANCED_SET_POINTS),
2206 VREG_DESC(S5, "8018_s5", &pm8018_smps_ops, SMPS_ADVANCED_SET_POINTS),
2207
2208 VREG_DESC(LVS1, "8018_lvs1", &pm8018_vs_ops, 0),
2209
2210 VREG_DESC(L2_PC, "8018_l2_pc", &pm8018_ldo_pc_ops, 0),
2211 VREG_DESC(L3_PC, "8018_l3_pc", &pm8018_ldo_pc_ops, 0),
2212 VREG_DESC(L4_PC, "8018_l4_pc", &pm8018_ldo_pc_ops, 0),
2213 VREG_DESC(L5_PC, "8018_l5_pc", &pm8018_ldo_pc_ops, 0),
2214 VREG_DESC(L6_PC, "8018_l6_pc", &pm8018_ldo_pc_ops, 0),
2215 VREG_DESC(L7_PC, "8018_l7_pc", &pm8018_ldo_pc_ops, 0),
2216 VREG_DESC(L8_PC, "8018_l8_pc", &pm8018_ldo_pc_ops, 0),
2217
2218 VREG_DESC(L13_PC, "8018_l13_pc", &pm8018_ldo_pc_ops, 0),
2219 VREG_DESC(L14_PC, "8018_l14_pc", &pm8018_ldo_pc_ops, 0),
2220
2221 VREG_DESC(S1_PC, "8018_s1_pc", &pm8018_smps_pc_ops, 0),
2222 VREG_DESC(S2_PC, "8018_s2_pc", &pm8018_smps_pc_ops, 0),
2223 VREG_DESC(S3_PC, "8018_s3_pc", &pm8018_smps_pc_ops, 0),
2224 VREG_DESC(S4_PC, "8018_s4_pc", &pm8018_smps_pc_ops, 0),
2225 VREG_DESC(S5_PC, "8018_s5_pc", &pm8018_smps_pc_ops, 0),
2226
2227 VREG_DESC(LVS1_PC, "8018_lvs1_pc", &pm8018_vs_pc_ops, 0),
2228};
2229
2230static int pm8018_init_ldo(struct pm8018_vreg *vreg, bool is_real)
2231{
2232 int rc = 0;
2233 int i;
2234 u8 bank;
2235
2236 /* Save the current control register state. */
2237 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2238 if (rc)
2239 goto bail;
2240
2241 /* Save the current test register state. */
2242 for (i = 0; i < LDO_TEST_BANKS; i++) {
2243 bank = REGULATOR_BANK_SEL(i);
2244 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2245 if (rc)
2246 goto bail;
2247
2248 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2249 &vreg->test_reg[i]);
2250 if (rc)
2251 goto bail;
2252 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2253 }
2254
2255 if (is_real) {
2256 /* Set pull down enable based on platform data. */
2257 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
2258 (vreg->pdata.pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
2259 LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
2260
2261 vreg->is_enabled = !!_pm8018_vreg_is_enabled(vreg);
2262
2263 vreg->mode = ((vreg->ctrl_reg & LDO_CTRL_PM_MASK)
2264 == LDO_CTRL_PM_LPM ?
2265 REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
2266 }
2267bail:
2268 if (rc)
2269 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2270
2271 return rc;
2272}
2273
2274static int pm8018_init_nldo1200(struct pm8018_vreg *vreg)
2275{
2276 int rc = 0;
2277 int i;
2278 u8 bank;
2279
2280 /* Save the current control register state. */
2281 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2282 if (rc)
2283 goto bail;
2284
2285 /* Save the current test register state. */
2286 for (i = 0; i < LDO_TEST_BANKS; i++) {
2287 bank = REGULATOR_BANK_SEL(i);
2288 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2289 if (rc)
2290 goto bail;
2291
2292 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2293 &vreg->test_reg[i]);
2294 if (rc)
2295 goto bail;
2296 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2297 }
2298
2299 vreg->save_uV = _pm8018_nldo1200_get_voltage(vreg);
2300
2301 /* Set pull down enable based on platform data. */
2302 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
2303 (vreg->pdata.pull_down_enable ? NLDO1200_PULL_DOWN_ENABLE : 0)
2304 | REGULATOR_BANK_SEL(1) | REGULATOR_BANK_WRITE,
2305 NLDO1200_PULL_DOWN_ENABLE_MASK | REGULATOR_BANK_MASK,
2306 &vreg->test_reg[1]);
2307
2308bail:
2309 if (rc)
2310 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2311
2312 return rc;
2313}
2314
2315static int pm8018_init_smps(struct pm8018_vreg *vreg, bool is_real)
2316{
2317 int rc = 0;
2318 int i;
2319 u8 bank;
2320
2321 /* Save the current control register state. */
2322 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2323 if (rc)
2324 goto bail;
2325
2326 /* Save the current test2 register state. */
2327 for (i = 0; i < SMPS_TEST_BANKS; i++) {
2328 bank = REGULATOR_BANK_SEL(i);
2329 rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
2330 if (rc)
2331 goto bail;
2332
2333 rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
2334 &vreg->test_reg[i]);
2335 if (rc)
2336 goto bail;
2337 vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
2338 }
2339
2340 /* Save the current clock control register state. */
2341 rc = pm8xxx_readb(vreg->dev->parent, vreg->clk_ctrl_addr,
2342 &vreg->clk_ctrl_reg);
2343 if (rc)
2344 goto bail;
2345
2346 /* Save the current sleep control register state. */
2347 rc = pm8xxx_readb(vreg->dev->parent, vreg->sleep_ctrl_addr,
2348 &vreg->sleep_ctrl_reg);
2349 if (rc)
2350 goto bail;
2351
2352 vreg->save_uV = _pm8018_smps_get_voltage(vreg);
2353
2354 if (is_real) {
2355 /* Set advanced mode pull down enable based on platform data. */
2356 rc = pm8018_vreg_masked_write(vreg, vreg->test_addr,
2357 (vreg->pdata.pull_down_enable
2358 ? SMPS_ADVANCED_PULL_DOWN_ENABLE : 0)
2359 | REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
2360 REGULATOR_BANK_MASK | SMPS_ADVANCED_PULL_DOWN_ENABLE,
2361 &vreg->test_reg[6]);
2362 if (rc)
2363 goto bail;
2364
2365 vreg->is_enabled = !!_pm8018_vreg_is_enabled(vreg);
2366
2367 vreg->mode = ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK)
2368 == SMPS_CLK_CTRL_PFM ?
2369 REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
2370 }
2371
2372 if (!SMPS_IN_ADVANCED_MODE(vreg) && is_real) {
2373 /* Set legacy mode pull down enable based on platform data. */
2374 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
2375 (vreg->pdata.pull_down_enable
2376 ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0),
2377 SMPS_LEGACY_PULL_DOWN_ENABLE, &vreg->ctrl_reg);
2378 if (rc)
2379 goto bail;
2380 }
2381
2382bail:
2383 if (rc)
2384 vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
2385
2386 return rc;
2387}
2388
2389static int pm8018_init_vs(struct pm8018_vreg *vreg, bool is_real)
2390{
2391 int rc = 0;
2392
2393 /* Save the current control register state. */
2394 rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
2395 if (rc) {
2396 vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
2397 return rc;
2398 }
2399
2400 if (is_real) {
2401 /* Set pull down enable based on platform data. */
2402 rc = pm8018_vreg_masked_write(vreg, vreg->ctrl_addr,
2403 (vreg->pdata.pull_down_enable ? VS_PULL_DOWN_ENABLE : 0),
2404 VS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
2405
2406 if (rc)
2407 vreg_err(vreg,
2408 "pm8018_vreg_masked_write failed, rc=%d\n", rc);
2409
2410 vreg->is_enabled = !!_pm8018_vreg_is_enabled(vreg);
2411 }
2412
2413 return rc;
2414}
2415
2416int pc_id_to_real_id(int id)
2417{
2418 int real_id;
2419
2420 if (id >= PM8018_VREG_ID_L2_PC && id <= PM8018_VREG_ID_L8_PC)
2421 real_id = id - PM8018_VREG_ID_L2_PC + PM8018_VREG_ID_L2;
2422 else
2423 real_id = id - PM8018_VREG_ID_L13_PC + PM8018_VREG_ID_L13;
2424
2425 return real_id;
2426}
2427
2428static int __devinit pm8018_vreg_probe(struct platform_device *pdev)
2429{
2430 const struct pm8018_regulator_platform_data *pdata;
2431 enum pm8018_vreg_pin_function pin_fn;
2432 struct regulator_desc *rdesc;
2433 struct pm8018_vreg *vreg;
2434 const char *reg_name = "";
2435 unsigned pin_ctrl;
2436 int rc = 0, id = pdev->id;
2437
2438 if (pdev == NULL)
2439 return -EINVAL;
2440
2441 if (pdev->id >= 0 && pdev->id < PM8018_VREG_ID_MAX) {
2442 pdata = pdev->dev.platform_data;
2443 rdesc = &pm8018_vreg_description[pdev->id];
2444 if (!IS_REAL_REGULATOR(pdev->id))
2445 id = pc_id_to_real_id(pdev->id);
2446 vreg = &pm8018_vreg[id];
2447 reg_name = pm8018_vreg_description[pdev->id].name;
2448 if (!pdata) {
2449 pr_err("%s requires platform data\n", reg_name);
2450 return -EINVAL;
2451 }
2452
2453 mutex_lock(&vreg->pc_lock);
2454
2455 if (IS_REAL_REGULATOR(pdev->id)) {
2456 /* Do not modify pin control and pin function values. */
2457 pin_ctrl = vreg->pdata.pin_ctrl;
2458 pin_fn = vreg->pdata.pin_fn;
2459 memcpy(&(vreg->pdata), pdata,
2460 sizeof(struct pm8018_regulator_platform_data));
2461 vreg->pdata.pin_ctrl = pin_ctrl;
2462 vreg->pdata.pin_fn = pin_fn;
2463 vreg->dev = &pdev->dev;
2464 vreg->name = reg_name;
2465 } else {
2466 /* Pin control regulator */
2467 if ((pdata->pin_ctrl &
2468 (PM8018_VREG_PIN_CTRL_D1 | PM8018_VREG_PIN_CTRL_A0
2469 | PM8018_VREG_PIN_CTRL_A1 | PM8018_VREG_PIN_CTRL_A2))
2470 == PM8018_VREG_PIN_CTRL_NONE) {
2471 pr_err("%s: no pin control input specified\n",
2472 reg_name);
2473 mutex_unlock(&vreg->pc_lock);
2474 return -EINVAL;
2475 }
2476 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
2477 vreg->pdata.pin_fn = pdata->pin_fn;
2478 vreg->dev_pc = &pdev->dev;
2479 if (!vreg->dev)
2480 vreg->dev = &pdev->dev;
2481 if (!vreg->name)
2482 vreg->name = reg_name;
2483 }
2484
2485 /* Initialize register values. */
2486 switch (vreg->type) {
2487 case REGULATOR_TYPE_PLDO:
2488 case REGULATOR_TYPE_NLDO:
2489 rc = pm8018_init_ldo(vreg, IS_REAL_REGULATOR(pdev->id));
2490 break;
2491 case REGULATOR_TYPE_NLDO1200:
2492 rc = pm8018_init_nldo1200(vreg);
2493 break;
2494 case REGULATOR_TYPE_SMPS:
2495 rc = pm8018_init_smps(vreg,
2496 IS_REAL_REGULATOR(pdev->id));
2497 break;
2498 case REGULATOR_TYPE_VS:
2499 rc = pm8018_init_vs(vreg, IS_REAL_REGULATOR(pdev->id));
2500 break;
2501 }
2502
2503 mutex_unlock(&vreg->pc_lock);
2504
2505 if (rc)
2506 goto bail;
2507
2508 if (IS_REAL_REGULATOR(pdev->id)) {
2509 vreg->rdev = regulator_register(rdesc, &pdev->dev,
2510 &(pdata->init_data), vreg);
2511 if (IS_ERR(vreg->rdev)) {
2512 rc = PTR_ERR(vreg->rdev);
2513 vreg->rdev = NULL;
2514 pr_err("regulator_register failed: %s, rc=%d\n",
2515 reg_name, rc);
2516 }
2517 } else {
2518 vreg->rdev_pc = regulator_register(rdesc, &pdev->dev,
2519 &(pdata->init_data), vreg);
2520 if (IS_ERR(vreg->rdev_pc)) {
2521 rc = PTR_ERR(vreg->rdev_pc);
2522 vreg->rdev_pc = NULL;
2523 pr_err("regulator_register failed: %s, rc=%d\n",
2524 reg_name, rc);
2525 }
2526 }
2527 if ((pm8018_vreg_debug_mask & PM8018_VREG_DEBUG_INIT) && !rc
2528 && vreg->rdev)
2529 pm8018_vreg_show_state(vreg->rdev,
2530 PM8018_REGULATOR_ACTION_INIT);
2531 } else {
2532 rc = -ENODEV;
2533 }
2534
2535bail:
2536 if (rc)
2537 pr_err("error for %s, rc=%d\n", reg_name, rc);
2538
2539 return rc;
2540}
2541
2542static int __devexit pm8018_vreg_remove(struct platform_device *pdev)
2543{
2544 if (IS_REAL_REGULATOR(pdev->id))
2545 regulator_unregister(pm8018_vreg[pdev->id].rdev);
2546 else
2547 regulator_unregister(
2548 pm8018_vreg[pc_id_to_real_id(pdev->id)].rdev_pc);
2549
2550 return 0;
2551}
2552
2553static struct platform_driver pm8018_vreg_driver = {
2554 .probe = pm8018_vreg_probe,
2555 .remove = __devexit_p(pm8018_vreg_remove),
2556 .driver = {
2557 .name = PM8018_REGULATOR_DEV_NAME,
2558 .owner = THIS_MODULE,
2559 },
2560};
2561
2562static int __init pm8018_vreg_init(void)
2563{
2564 int i;
2565
2566 for (i = 0; i < ARRAY_SIZE(pm8018_vreg); i++) {
2567 mutex_init(&pm8018_vreg[i].pc_lock);
2568 pm8018_vreg[i].write_count = 0;
2569 pm8018_vreg[i].prev_write_count = -1;
2570 }
2571
2572 return platform_driver_register(&pm8018_vreg_driver);
2573}
2574postcore_initcall(pm8018_vreg_init);
2575
2576static void __exit pm8018_vreg_exit(void)
2577{
2578 int i;
2579
2580 platform_driver_unregister(&pm8018_vreg_driver);
2581
2582 for (i = 0; i < ARRAY_SIZE(pm8018_vreg); i++)
2583 mutex_destroy(&pm8018_vreg[i].pc_lock);
2584}
2585module_exit(pm8018_vreg_exit);
2586
2587MODULE_LICENSE("GPL v2");
2588MODULE_DESCRIPTION("PMIC8018 regulator driver");
2589MODULE_VERSION("1.0");
2590MODULE_ALIAS("platform:" PM8018_REGULATOR_DEV_NAME);