1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
6 #include <linux/module.h>
7 #include <linux/delay.h>
9 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/bitops.h>
12 #include <linux/slab.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/ktime.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regmap.h>
19 #include <linux/list.h>
20 #include <linux/mfd/syscon.h>
23 /* Pin control enable input pins. */
24 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE 0x00
25 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0 0x01
26 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1 0x02
27 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2 0x04
28 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3 0x08
29 #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT 0x10
31 /* Pin control high power mode input pins. */
32 #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE 0x00
33 #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0 0x01
34 #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1 0x02
35 #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2 0x04
36 #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3 0x08
37 #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B 0x10
38 #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT 0x20
41 * Used with enable parameters to specify that hardware default register values
42 * should be left unaltered.
44 #define SPMI_REGULATOR_USE_HW_DEFAULT 2
46 /* Soft start strength of a voltage switch type regulator */
47 enum spmi_vs_soft_start_str
{
48 SPMI_VS_SOFT_START_STR_0P05_UA
= 0,
49 SPMI_VS_SOFT_START_STR_0P25_UA
,
50 SPMI_VS_SOFT_START_STR_0P55_UA
,
51 SPMI_VS_SOFT_START_STR_0P75_UA
,
52 SPMI_VS_SOFT_START_STR_HW_DEFAULT
,
56 * struct spmi_regulator_init_data - spmi-regulator initialization data
57 * @pin_ctrl_enable: Bit mask specifying which hardware pins should be
58 * used to enable the regulator, if any
59 * Value should be an ORing of
60 * SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants. If
61 * the bit specified by
62 * SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
63 * set, then pin control enable hardware registers
64 * will not be modified.
65 * @pin_ctrl_hpm: Bit mask specifying which hardware pins should be
66 * used to force the regulator into high power
68 * Value should be an ORing of
69 * SPMI_REGULATOR_PIN_CTRL_HPM_* constants. If
70 * the bit specified by
71 * SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
72 * set, then pin control mode hardware registers
73 * will not be modified.
74 * @vs_soft_start_strength: This parameter sets the soft start strength for
75 * voltage switch type regulators. Its value
76 * should be one of SPMI_VS_SOFT_START_STR_*. If
77 * its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
78 * then the soft start strength will be left at its
79 * default hardware value.
81 struct spmi_regulator_init_data
{
82 unsigned pin_ctrl_enable
;
83 unsigned pin_ctrl_hpm
;
84 enum spmi_vs_soft_start_str vs_soft_start_strength
;
87 /* These types correspond to unique register layouts. */
88 enum spmi_regulator_logical_type
{
89 SPMI_REGULATOR_LOGICAL_TYPE_SMPS
,
90 SPMI_REGULATOR_LOGICAL_TYPE_LDO
,
91 SPMI_REGULATOR_LOGICAL_TYPE_VS
,
92 SPMI_REGULATOR_LOGICAL_TYPE_BOOST
,
93 SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS
,
94 SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP
,
95 SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO
,
96 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
,
97 SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
,
98 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO
,
99 SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426
,
100 SPMI_REGULATOR_LOGICAL_TYPE_HFS430
,
103 enum spmi_regulator_type
{
104 SPMI_REGULATOR_TYPE_BUCK
= 0x03,
105 SPMI_REGULATOR_TYPE_LDO
= 0x04,
106 SPMI_REGULATOR_TYPE_VS
= 0x05,
107 SPMI_REGULATOR_TYPE_BOOST
= 0x1b,
108 SPMI_REGULATOR_TYPE_FTS
= 0x1c,
109 SPMI_REGULATOR_TYPE_BOOST_BYP
= 0x1f,
110 SPMI_REGULATOR_TYPE_ULT_LDO
= 0x21,
111 SPMI_REGULATOR_TYPE_ULT_BUCK
= 0x22,
114 enum spmi_regulator_subtype
{
115 SPMI_REGULATOR_SUBTYPE_GP_CTL
= 0x08,
116 SPMI_REGULATOR_SUBTYPE_RF_CTL
= 0x09,
117 SPMI_REGULATOR_SUBTYPE_N50
= 0x01,
118 SPMI_REGULATOR_SUBTYPE_N150
= 0x02,
119 SPMI_REGULATOR_SUBTYPE_N300
= 0x03,
120 SPMI_REGULATOR_SUBTYPE_N600
= 0x04,
121 SPMI_REGULATOR_SUBTYPE_N1200
= 0x05,
122 SPMI_REGULATOR_SUBTYPE_N600_ST
= 0x06,
123 SPMI_REGULATOR_SUBTYPE_N1200_ST
= 0x07,
124 SPMI_REGULATOR_SUBTYPE_N900_ST
= 0x14,
125 SPMI_REGULATOR_SUBTYPE_N300_ST
= 0x15,
126 SPMI_REGULATOR_SUBTYPE_P50
= 0x08,
127 SPMI_REGULATOR_SUBTYPE_P150
= 0x09,
128 SPMI_REGULATOR_SUBTYPE_P300
= 0x0a,
129 SPMI_REGULATOR_SUBTYPE_P600
= 0x0b,
130 SPMI_REGULATOR_SUBTYPE_P1200
= 0x0c,
131 SPMI_REGULATOR_SUBTYPE_LN
= 0x10,
132 SPMI_REGULATOR_SUBTYPE_LV_P50
= 0x28,
133 SPMI_REGULATOR_SUBTYPE_LV_P150
= 0x29,
134 SPMI_REGULATOR_SUBTYPE_LV_P300
= 0x2a,
135 SPMI_REGULATOR_SUBTYPE_LV_P600
= 0x2b,
136 SPMI_REGULATOR_SUBTYPE_LV_P1200
= 0x2c,
137 SPMI_REGULATOR_SUBTYPE_LV_P450
= 0x2d,
138 SPMI_REGULATOR_SUBTYPE_LV100
= 0x01,
139 SPMI_REGULATOR_SUBTYPE_LV300
= 0x02,
140 SPMI_REGULATOR_SUBTYPE_MV300
= 0x08,
141 SPMI_REGULATOR_SUBTYPE_MV500
= 0x09,
142 SPMI_REGULATOR_SUBTYPE_HDMI
= 0x10,
143 SPMI_REGULATOR_SUBTYPE_OTG
= 0x11,
144 SPMI_REGULATOR_SUBTYPE_5V_BOOST
= 0x01,
145 SPMI_REGULATOR_SUBTYPE_FTS_CTL
= 0x08,
146 SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL
= 0x09,
147 SPMI_REGULATOR_SUBTYPE_FTS426_CTL
= 0x0a,
148 SPMI_REGULATOR_SUBTYPE_BB_2A
= 0x01,
149 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1
= 0x0d,
150 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2
= 0x0e,
151 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3
= 0x0f,
152 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4
= 0x10,
153 SPMI_REGULATOR_SUBTYPE_HFS430
= 0x0a,
156 enum spmi_common_regulator_registers
{
157 SPMI_COMMON_REG_DIG_MAJOR_REV
= 0x01,
158 SPMI_COMMON_REG_TYPE
= 0x04,
159 SPMI_COMMON_REG_SUBTYPE
= 0x05,
160 SPMI_COMMON_REG_VOLTAGE_RANGE
= 0x40,
161 SPMI_COMMON_REG_VOLTAGE_SET
= 0x41,
162 SPMI_COMMON_REG_MODE
= 0x45,
163 SPMI_COMMON_REG_ENABLE
= 0x46,
164 SPMI_COMMON_REG_PULL_DOWN
= 0x48,
165 SPMI_COMMON_REG_SOFT_START
= 0x4c,
166 SPMI_COMMON_REG_STEP_CTRL
= 0x61,
170 * Second common register layout used by newer devices starting with ftsmps426
171 * Note that some of the registers from the first common layout remain
172 * unchanged and their definition is not duplicated.
174 enum spmi_ftsmps426_regulator_registers
{
175 SPMI_FTSMPS426_REG_VOLTAGE_LSB
= 0x40,
176 SPMI_FTSMPS426_REG_VOLTAGE_MSB
= 0x41,
177 SPMI_FTSMPS426_REG_VOLTAGE_ULS_LSB
= 0x68,
178 SPMI_FTSMPS426_REG_VOLTAGE_ULS_MSB
= 0x69,
181 enum spmi_vs_registers
{
182 SPMI_VS_REG_OCP
= 0x4a,
183 SPMI_VS_REG_SOFT_START
= 0x4c,
186 enum spmi_boost_registers
{
187 SPMI_BOOST_REG_CURRENT_LIMIT
= 0x4a,
190 enum spmi_boost_byp_registers
{
191 SPMI_BOOST_BYP_REG_CURRENT_LIMIT
= 0x4b,
194 enum spmi_saw3_registers
{
199 SAW3_PMIC_STS
= 0x14,
203 SAW3_AVS_LIMIT
= 0x24,
205 SAW3_AVS_HYSTERESIS
= 0x2C,
206 SAW3_SPM_STS2
= 0x38,
207 SAW3_SPM_PMIC_DATA_3
= 0x4C,
208 SAW3_VERSION
= 0xFD0,
211 /* Used for indexing into ctrl_reg. These are offets from 0x40 */
212 enum spmi_common_control_register_index
{
213 SPMI_COMMON_IDX_VOLTAGE_RANGE
= 0,
214 SPMI_COMMON_IDX_VOLTAGE_SET
= 1,
215 SPMI_COMMON_IDX_MODE
= 5,
216 SPMI_COMMON_IDX_ENABLE
= 6,
219 /* Common regulator control register layout */
220 #define SPMI_COMMON_ENABLE_MASK 0x80
221 #define SPMI_COMMON_ENABLE 0x80
222 #define SPMI_COMMON_DISABLE 0x00
223 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
224 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
225 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
226 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
227 #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f
229 /* Common regulator mode register layout */
230 #define SPMI_COMMON_MODE_HPM_MASK 0x80
231 #define SPMI_COMMON_MODE_AUTO_MASK 0x40
232 #define SPMI_COMMON_MODE_BYPASS_MASK 0x20
233 #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
234 #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
235 #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
236 #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
237 #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
238 #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f
240 #define SPMI_FTSMPS426_MODE_BYPASS_MASK 3
241 #define SPMI_FTSMPS426_MODE_RETENTION_MASK 4
242 #define SPMI_FTSMPS426_MODE_LPM_MASK 5
243 #define SPMI_FTSMPS426_MODE_AUTO_MASK 6
244 #define SPMI_FTSMPS426_MODE_HPM_MASK 7
246 #define SPMI_FTSMPS426_MODE_MASK 0x07
248 /* Common regulator pull down control register layout */
249 #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80
251 /* LDO regulator current limit control register layout */
252 #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
254 /* LDO regulator soft start control register layout */
255 #define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80
257 /* VS regulator over current protection control register layout */
258 #define SPMI_VS_OCP_OVERRIDE 0x01
259 #define SPMI_VS_OCP_NO_OVERRIDE 0x00
261 /* VS regulator soft start control register layout */
262 #define SPMI_VS_SOFT_START_ENABLE_MASK 0x80
263 #define SPMI_VS_SOFT_START_SEL_MASK 0x03
265 /* Boost regulator current limit control register layout */
266 #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
267 #define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07
269 #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10
270 #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
271 #define SPMI_VS_OCP_FALL_DELAY_US 90
272 #define SPMI_VS_OCP_FAULT_DELAY_US 20000
274 #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18
275 #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3
276 #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07
277 #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0
279 /* Clock rate in kHz of the FTSMPS regulator reference clock. */
280 #define SPMI_FTSMPS_CLOCK_RATE 19200
282 /* Minimum voltage stepper delay for each step. */
283 #define SPMI_FTSMPS_STEP_DELAY 8
284 #define SPMI_DEFAULT_STEP_DELAY 20
287 * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
288 * adjust the step rate in order to account for oscillator variance.
290 #define SPMI_FTSMPS_STEP_MARGIN_NUM 4
291 #define SPMI_FTSMPS_STEP_MARGIN_DEN 5
293 #define SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK 0x03
294 #define SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT 0
296 /* Clock rate in kHz of the FTSMPS426 regulator reference clock. */
297 #define SPMI_FTSMPS426_CLOCK_RATE 4800
299 #define SPMI_HFS430_CLOCK_RATE 1600
301 /* Minimum voltage stepper delay for each step. */
302 #define SPMI_FTSMPS426_STEP_DELAY 2
305 * The ratio SPMI_FTSMPS426_STEP_MARGIN_NUM/SPMI_FTSMPS426_STEP_MARGIN_DEN is
306 * used to adjust the step rate in order to account for oscillator variance.
308 #define SPMI_FTSMPS426_STEP_MARGIN_NUM 10
309 #define SPMI_FTSMPS426_STEP_MARGIN_DEN 11
312 /* VSET value to decide the range of ULT SMPS */
313 #define ULT_SMPS_RANGE_SPLIT 0x60
316 * struct spmi_voltage_range - regulator set point voltage mapping description
317 * @min_uV: Minimum programmable output voltage resulting from
318 * set point register value 0x00
319 * @max_uV: Maximum programmable output voltage
320 * @step_uV: Output voltage increase resulting from the set point
321 * register value increasing by 1
322 * @set_point_min_uV: Minimum allowed voltage
323 * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order
324 * to pick which range should be used in the case of
325 * overlapping set points.
326 * @n_voltages: Number of preferred voltage set points present in this
328 * @range_sel: Voltage range register value corresponding to this range
330 * The following relationships must be true for the values used in this struct:
331 * (max_uV - min_uV) % step_uV == 0
332 * (set_point_min_uV - min_uV) % step_uV == 0*
333 * (set_point_max_uV - min_uV) % step_uV == 0*
334 * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
336 * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
337 * specify that the voltage range has meaning, but is not preferred.
339 struct spmi_voltage_range
{
343 int set_point_min_uV
;
344 int set_point_max_uV
;
350 * The ranges specified in the spmi_voltage_set_points struct must be listed
351 * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
353 struct spmi_voltage_set_points
{
354 struct spmi_voltage_range
*range
;
359 struct spmi_regulator
{
360 struct regulator_desc desc
;
362 struct delayed_work ocp_work
;
363 struct regmap
*regmap
;
364 struct spmi_voltage_set_points
*set_points
;
365 enum spmi_regulator_logical_type logical_type
;
369 int ocp_retry_delay_ms
;
372 ktime_t vs_enable_time
;
374 struct list_head node
;
377 struct spmi_regulator_mapping
{
378 enum spmi_regulator_type type
;
379 enum spmi_regulator_subtype subtype
;
380 enum spmi_regulator_logical_type logical_type
;
383 struct regulator_ops
*ops
;
384 struct spmi_voltage_set_points
*set_points
;
388 struct spmi_regulator_data
{
396 #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
397 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
399 .type = SPMI_REGULATOR_TYPE_##_type, \
400 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
401 .revision_min = _dig_major_min, \
402 .revision_max = _dig_major_max, \
403 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
404 .ops = &spmi_##_ops_val##_ops, \
405 .set_points = &_set_points_val##_set_points, \
406 .hpm_min_load = _hpm_min_load, \
409 #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
411 .type = SPMI_REGULATOR_TYPE_VS, \
412 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
413 .revision_min = _dig_major_min, \
414 .revision_max = _dig_major_max, \
415 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \
416 .ops = &spmi_vs_ops, \
419 #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
420 _set_point_max_uV, _max_uV, _step_uV) \
424 .set_point_min_uV = _set_point_min_uV, \
425 .set_point_max_uV = _set_point_max_uV, \
426 .step_uV = _step_uV, \
427 .range_sel = _range_sel, \
430 #define DEFINE_SPMI_SET_POINTS(name) \
431 struct spmi_voltage_set_points name##_set_points = { \
432 .range = name##_ranges, \
433 .count = ARRAY_SIZE(name##_ranges), \
437 * These tables contain the physically available PMIC regulator voltage setpoint
438 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
439 * to ensure that the setpoints available to software are monotonically
440 * increasing and unique. The set_voltage callback functions expect these
441 * properties to hold.
443 static struct spmi_voltage_range pldo_ranges
[] = {
444 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
445 SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
446 SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
449 static struct spmi_voltage_range nldo1_ranges
[] = {
450 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
453 static struct spmi_voltage_range nldo2_ranges
[] = {
454 SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500),
455 SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250),
456 SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500),
459 static struct spmi_voltage_range nldo3_ranges
[] = {
460 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
461 SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500),
462 SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500),
465 static struct spmi_voltage_range ln_ldo_ranges
[] = {
466 SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000),
467 SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
470 static struct spmi_voltage_range smps_ranges
[] = {
471 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
472 SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
475 static struct spmi_voltage_range ftsmps_ranges
[] = {
476 SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000),
477 SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000),
480 static struct spmi_voltage_range ftsmps2p5_ranges
[] = {
481 SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000),
482 SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000),
485 static struct spmi_voltage_range ftsmps426_ranges
[] = {
486 SPMI_VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000),
489 static struct spmi_voltage_range boost_ranges
[] = {
490 SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
493 static struct spmi_voltage_range boost_byp_ranges
[] = {
494 SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
497 static struct spmi_voltage_range ult_lo_smps_ranges
[] = {
498 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
499 SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000),
502 static struct spmi_voltage_range ult_ho_smps_ranges
[] = {
503 SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
506 static struct spmi_voltage_range ult_nldo_ranges
[] = {
507 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
510 static struct spmi_voltage_range ult_pldo_ranges
[] = {
511 SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
514 static struct spmi_voltage_range hfs430_ranges
[] = {
515 SPMI_VOLTAGE_RANGE(0, 320000, 320000, 2040000, 2040000, 8000),
518 static DEFINE_SPMI_SET_POINTS(pldo
);
519 static DEFINE_SPMI_SET_POINTS(nldo1
);
520 static DEFINE_SPMI_SET_POINTS(nldo2
);
521 static DEFINE_SPMI_SET_POINTS(nldo3
);
522 static DEFINE_SPMI_SET_POINTS(ln_ldo
);
523 static DEFINE_SPMI_SET_POINTS(smps
);
524 static DEFINE_SPMI_SET_POINTS(ftsmps
);
525 static DEFINE_SPMI_SET_POINTS(ftsmps2p5
);
526 static DEFINE_SPMI_SET_POINTS(ftsmps426
);
527 static DEFINE_SPMI_SET_POINTS(boost
);
528 static DEFINE_SPMI_SET_POINTS(boost_byp
);
529 static DEFINE_SPMI_SET_POINTS(ult_lo_smps
);
530 static DEFINE_SPMI_SET_POINTS(ult_ho_smps
);
531 static DEFINE_SPMI_SET_POINTS(ult_nldo
);
532 static DEFINE_SPMI_SET_POINTS(ult_pldo
);
533 static DEFINE_SPMI_SET_POINTS(hfs430
);
535 static inline int spmi_vreg_read(struct spmi_regulator
*vreg
, u16 addr
, u8
*buf
,
538 return regmap_bulk_read(vreg
->regmap
, vreg
->base
+ addr
, buf
, len
);
541 static inline int spmi_vreg_write(struct spmi_regulator
*vreg
, u16 addr
,
544 return regmap_bulk_write(vreg
->regmap
, vreg
->base
+ addr
, buf
, len
);
547 static int spmi_vreg_update_bits(struct spmi_regulator
*vreg
, u16 addr
, u8 val
,
550 return regmap_update_bits(vreg
->regmap
, vreg
->base
+ addr
, mask
, val
);
553 static int spmi_regulator_vs_enable(struct regulator_dev
*rdev
)
555 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
559 vreg
->vs_enable_time
= ktime_get();
562 return regulator_enable_regmap(rdev
);
565 static int spmi_regulator_vs_ocp(struct regulator_dev
*rdev
)
567 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
568 u8 reg
= SPMI_VS_OCP_OVERRIDE
;
570 return spmi_vreg_write(vreg
, SPMI_VS_REG_OCP
, ®
, 1);
573 static int spmi_regulator_select_voltage(struct spmi_regulator
*vreg
,
574 int min_uV
, int max_uV
)
576 const struct spmi_voltage_range
*range
;
578 int lim_min_uV
, lim_max_uV
, i
, range_id
, range_max_uV
;
579 int selector
, voltage_sel
;
581 /* Check if request voltage is outside of physically settable range. */
582 lim_min_uV
= vreg
->set_points
->range
[0].set_point_min_uV
;
584 vreg
->set_points
->range
[vreg
->set_points
->count
- 1].set_point_max_uV
;
586 if (uV
< lim_min_uV
&& max_uV
>= lim_min_uV
)
589 if (uV
< lim_min_uV
|| uV
> lim_max_uV
) {
591 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
592 min_uV
, max_uV
, lim_min_uV
, lim_max_uV
);
596 /* Find the range which uV is inside of. */
597 for (i
= vreg
->set_points
->count
- 1; i
> 0; i
--) {
598 range_max_uV
= vreg
->set_points
->range
[i
- 1].set_point_max_uV
;
599 if (uV
> range_max_uV
&& range_max_uV
> 0)
604 range
= &vreg
->set_points
->range
[range_id
];
607 * Force uV to be an allowed set point by applying a ceiling function to
610 voltage_sel
= DIV_ROUND_UP(uV
- range
->min_uV
, range
->step_uV
);
611 uV
= voltage_sel
* range
->step_uV
+ range
->min_uV
;
615 "request v=[%d, %d] cannot be met by any set point; "
616 "next set point: %d\n",
622 for (i
= 0; i
< range_id
; i
++)
623 selector
+= vreg
->set_points
->range
[i
].n_voltages
;
624 selector
+= (uV
- range
->set_point_min_uV
) / range
->step_uV
;
629 static int spmi_sw_selector_to_hw(struct spmi_regulator
*vreg
,
630 unsigned selector
, u8
*range_sel
,
633 const struct spmi_voltage_range
*range
, *end
;
636 range
= vreg
->set_points
->range
;
637 end
= range
+ vreg
->set_points
->count
;
639 for (; range
< end
; range
++) {
640 if (selector
< range
->n_voltages
) {
642 * hardware selectors between set point min and real
643 * min are invalid so we ignore them
645 offset
= range
->set_point_min_uV
- range
->min_uV
;
646 offset
/= range
->step_uV
;
647 *voltage_sel
= selector
+ offset
;
648 *range_sel
= range
->range_sel
;
652 selector
-= range
->n_voltages
;
658 static int spmi_hw_selector_to_sw(struct spmi_regulator
*vreg
, u8 hw_sel
,
659 const struct spmi_voltage_range
*range
)
662 unsigned offset
, max_hw_sel
;
663 const struct spmi_voltage_range
*r
= vreg
->set_points
->range
;
664 const struct spmi_voltage_range
*end
= r
+ vreg
->set_points
->count
;
666 for (; r
< end
; r
++) {
667 if (r
== range
&& range
->n_voltages
) {
669 * hardware selectors between set point min and real
670 * min and between set point max and real max are
671 * invalid so we return an error if they're
672 * programmed into the hardware
674 offset
= range
->set_point_min_uV
- range
->min_uV
;
675 offset
/= range
->step_uV
;
679 max_hw_sel
= range
->set_point_max_uV
- range
->min_uV
;
680 max_hw_sel
/= range
->step_uV
;
681 if (hw_sel
> max_hw_sel
)
684 return sw_sel
+ hw_sel
- offset
;
686 sw_sel
+= r
->n_voltages
;
692 static const struct spmi_voltage_range
*
693 spmi_regulator_find_range(struct spmi_regulator
*vreg
)
696 const struct spmi_voltage_range
*range
, *end
;
698 range
= vreg
->set_points
->range
;
699 end
= range
+ vreg
->set_points
->count
;
701 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, &range_sel
, 1);
703 for (; range
< end
; range
++)
704 if (range
->range_sel
== range_sel
)
710 static int spmi_regulator_select_voltage_same_range(struct spmi_regulator
*vreg
,
711 int min_uV
, int max_uV
)
713 const struct spmi_voltage_range
*range
;
717 range
= spmi_regulator_find_range(vreg
);
719 goto different_range
;
721 if (uV
< range
->min_uV
&& max_uV
>= range
->min_uV
)
724 if (uV
< range
->min_uV
|| uV
> range
->max_uV
) {
725 /* Current range doesn't support the requested voltage. */
726 goto different_range
;
730 * Force uV to be an allowed set point by applying a ceiling function to
733 uV
= DIV_ROUND_UP(uV
- range
->min_uV
, range
->step_uV
);
734 uV
= uV
* range
->step_uV
+ range
->min_uV
;
738 * No set point in the current voltage range is within the
739 * requested min_uV to max_uV range.
741 goto different_range
;
745 for (i
= 0; i
< vreg
->set_points
->count
; i
++) {
746 if (uV
>= vreg
->set_points
->range
[i
].set_point_min_uV
747 && uV
<= vreg
->set_points
->range
[i
].set_point_max_uV
) {
749 (uV
- vreg
->set_points
->range
[i
].set_point_min_uV
)
750 / vreg
->set_points
->range
[i
].step_uV
;
754 selector
+= vreg
->set_points
->range
[i
].n_voltages
;
757 if (selector
>= vreg
->set_points
->n_voltages
)
758 goto different_range
;
763 return spmi_regulator_select_voltage(vreg
, min_uV
, max_uV
);
766 static int spmi_regulator_common_map_voltage(struct regulator_dev
*rdev
,
767 int min_uV
, int max_uV
)
769 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
772 * Favor staying in the current voltage range if possible. This avoids
773 * voltage spikes that occur when changing the voltage range.
775 return spmi_regulator_select_voltage_same_range(vreg
, min_uV
, max_uV
);
779 spmi_regulator_common_set_voltage(struct regulator_dev
*rdev
, unsigned selector
)
781 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
784 u8 range_sel
, voltage_sel
;
786 ret
= spmi_sw_selector_to_hw(vreg
, selector
, &range_sel
, &voltage_sel
);
791 buf
[1] = voltage_sel
;
792 return spmi_vreg_write(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, buf
, 2);
795 static int spmi_regulator_common_list_voltage(struct regulator_dev
*rdev
,
798 static int spmi_regulator_ftsmps426_set_voltage(struct regulator_dev
*rdev
,
801 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
805 mV
= spmi_regulator_common_list_voltage(rdev
, selector
) / 1000;
809 return spmi_vreg_write(vreg
, SPMI_FTSMPS426_REG_VOLTAGE_LSB
, buf
, 2);
812 static int spmi_regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
813 unsigned int old_selector
, unsigned int new_selector
)
815 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
818 diff_uV
= abs(spmi_regulator_common_list_voltage(rdev
, new_selector
) -
819 spmi_regulator_common_list_voltage(rdev
, old_selector
));
821 return DIV_ROUND_UP(diff_uV
, vreg
->slew_rate
);
824 static int spmi_regulator_common_get_voltage(struct regulator_dev
*rdev
)
826 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
827 const struct spmi_voltage_range
*range
;
830 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &voltage_sel
, 1);
832 range
= spmi_regulator_find_range(vreg
);
836 return spmi_hw_selector_to_sw(vreg
, voltage_sel
, range
);
839 static int spmi_regulator_ftsmps426_get_voltage(struct regulator_dev
*rdev
)
841 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
842 const struct spmi_voltage_range
*range
;
846 spmi_vreg_read(vreg
, SPMI_FTSMPS426_REG_VOLTAGE_LSB
, buf
, 2);
848 uV
= (((unsigned int)buf
[1] << 8) | (unsigned int)buf
[0]) * 1000;
849 range
= vreg
->set_points
->range
;
851 return (uV
- range
->set_point_min_uV
) / range
->step_uV
;
854 static int spmi_regulator_single_map_voltage(struct regulator_dev
*rdev
,
855 int min_uV
, int max_uV
)
857 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
859 return spmi_regulator_select_voltage(vreg
, min_uV
, max_uV
);
862 static int spmi_regulator_single_range_set_voltage(struct regulator_dev
*rdev
,
865 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
869 * Certain types of regulators do not have a range select register so
870 * only voltage set register needs to be written.
872 return spmi_vreg_write(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &sel
, 1);
875 static int spmi_regulator_single_range_get_voltage(struct regulator_dev
*rdev
)
877 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
881 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &selector
, 1);
888 static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev
*rdev
,
891 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
893 u8 range_sel
, voltage_sel
;
895 ret
= spmi_sw_selector_to_hw(vreg
, selector
, &range_sel
, &voltage_sel
);
900 * Calculate VSET based on range
901 * In case of range 0: voltage_sel is a 7 bit value, can be written
902 * witout any modification.
903 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
907 voltage_sel
|= ULT_SMPS_RANGE_SPLIT
;
909 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
,
913 static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev
*rdev
)
915 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
916 const struct spmi_voltage_range
*range
;
919 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &voltage_sel
, 1);
921 range
= spmi_regulator_find_range(vreg
);
925 if (range
->range_sel
== 1)
926 voltage_sel
&= ~ULT_SMPS_RANGE_SPLIT
;
928 return spmi_hw_selector_to_sw(vreg
, voltage_sel
, range
);
931 static int spmi_regulator_common_list_voltage(struct regulator_dev
*rdev
,
934 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
938 if (selector
>= vreg
->set_points
->n_voltages
)
941 for (i
= 0; i
< vreg
->set_points
->count
; i
++) {
942 if (selector
< vreg
->set_points
->range
[i
].n_voltages
) {
943 uV
= selector
* vreg
->set_points
->range
[i
].step_uV
944 + vreg
->set_points
->range
[i
].set_point_min_uV
;
948 selector
-= vreg
->set_points
->range
[i
].n_voltages
;
955 spmi_regulator_common_set_bypass(struct regulator_dev
*rdev
, bool enable
)
957 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
958 u8 mask
= SPMI_COMMON_MODE_BYPASS_MASK
;
964 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_MODE
, val
, mask
);
968 spmi_regulator_common_get_bypass(struct regulator_dev
*rdev
, bool *enable
)
970 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
974 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_MODE
, &val
, 1);
975 *enable
= val
& SPMI_COMMON_MODE_BYPASS_MASK
;
980 static unsigned int spmi_regulator_common_get_mode(struct regulator_dev
*rdev
)
982 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
985 spmi_vreg_read(vreg
, SPMI_COMMON_REG_MODE
, ®
, 1);
987 reg
&= SPMI_COMMON_MODE_HPM_MASK
| SPMI_COMMON_MODE_AUTO_MASK
;
990 case SPMI_COMMON_MODE_HPM_MASK
:
991 return REGULATOR_MODE_NORMAL
;
992 case SPMI_COMMON_MODE_AUTO_MASK
:
993 return REGULATOR_MODE_FAST
;
995 return REGULATOR_MODE_IDLE
;
999 static unsigned int spmi_regulator_ftsmps426_get_mode(struct regulator_dev
*rdev
)
1001 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1004 spmi_vreg_read(vreg
, SPMI_COMMON_REG_MODE
, ®
, 1);
1007 case SPMI_FTSMPS426_MODE_HPM_MASK
:
1008 return REGULATOR_MODE_NORMAL
;
1009 case SPMI_FTSMPS426_MODE_AUTO_MASK
:
1010 return REGULATOR_MODE_FAST
;
1012 return REGULATOR_MODE_IDLE
;
1017 spmi_regulator_common_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
1019 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1020 u8 mask
= SPMI_COMMON_MODE_HPM_MASK
| SPMI_COMMON_MODE_AUTO_MASK
;
1024 case REGULATOR_MODE_NORMAL
:
1025 val
= SPMI_COMMON_MODE_HPM_MASK
;
1027 case REGULATOR_MODE_FAST
:
1028 val
= SPMI_COMMON_MODE_AUTO_MASK
;
1035 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_MODE
, val
, mask
);
1039 spmi_regulator_ftsmps426_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
1041 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1042 u8 mask
= SPMI_FTSMPS426_MODE_MASK
;
1046 case REGULATOR_MODE_NORMAL
:
1047 val
= SPMI_FTSMPS426_MODE_HPM_MASK
;
1049 case REGULATOR_MODE_FAST
:
1050 val
= SPMI_FTSMPS426_MODE_AUTO_MASK
;
1052 case REGULATOR_MODE_IDLE
:
1053 val
= SPMI_FTSMPS426_MODE_LPM_MASK
;
1059 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_MODE
, val
, mask
);
1063 spmi_regulator_common_set_load(struct regulator_dev
*rdev
, int load_uA
)
1065 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1068 if (load_uA
>= vreg
->hpm_min_load
)
1069 mode
= REGULATOR_MODE_NORMAL
;
1071 mode
= REGULATOR_MODE_IDLE
;
1073 return spmi_regulator_common_set_mode(rdev
, mode
);
1076 static int spmi_regulator_common_set_pull_down(struct regulator_dev
*rdev
)
1078 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1079 unsigned int mask
= SPMI_COMMON_PULL_DOWN_ENABLE_MASK
;
1081 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_PULL_DOWN
,
1085 static int spmi_regulator_common_set_soft_start(struct regulator_dev
*rdev
)
1087 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1088 unsigned int mask
= SPMI_LDO_SOFT_START_ENABLE_MASK
;
1090 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_SOFT_START
,
1094 static int spmi_regulator_set_ilim(struct regulator_dev
*rdev
, int ilim_uA
)
1096 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1097 enum spmi_regulator_logical_type type
= vreg
->logical_type
;
1098 unsigned int current_reg
;
1100 u8 mask
= SPMI_BOOST_CURRENT_LIMIT_MASK
|
1101 SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK
;
1102 int max
= (SPMI_BOOST_CURRENT_LIMIT_MASK
+ 1) * 500;
1104 if (type
== SPMI_REGULATOR_LOGICAL_TYPE_BOOST
)
1105 current_reg
= SPMI_BOOST_REG_CURRENT_LIMIT
;
1107 current_reg
= SPMI_BOOST_BYP_REG_CURRENT_LIMIT
;
1109 if (ilim_uA
> max
|| ilim_uA
<= 0)
1112 reg
= (ilim_uA
- 1) / 500;
1113 reg
|= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK
;
1115 return spmi_vreg_update_bits(vreg
, current_reg
, reg
, mask
);
1118 static int spmi_regulator_vs_clear_ocp(struct spmi_regulator
*vreg
)
1122 ret
= spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
1123 SPMI_COMMON_DISABLE
, SPMI_COMMON_ENABLE_MASK
);
1125 vreg
->vs_enable_time
= ktime_get();
1127 ret
= spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
1128 SPMI_COMMON_ENABLE
, SPMI_COMMON_ENABLE_MASK
);
1133 static void spmi_regulator_vs_ocp_work(struct work_struct
*work
)
1135 struct delayed_work
*dwork
= to_delayed_work(work
);
1136 struct spmi_regulator
*vreg
1137 = container_of(dwork
, struct spmi_regulator
, ocp_work
);
1139 spmi_regulator_vs_clear_ocp(vreg
);
1142 static irqreturn_t
spmi_regulator_vs_ocp_isr(int irq
, void *data
)
1144 struct spmi_regulator
*vreg
= data
;
1145 ktime_t ocp_irq_time
;
1146 s64 ocp_trigger_delay_us
;
1148 ocp_irq_time
= ktime_get();
1149 ocp_trigger_delay_us
= ktime_us_delta(ocp_irq_time
,
1150 vreg
->vs_enable_time
);
1153 * Reset the OCP count if there is a large delay between switch enable
1154 * and when OCP triggers. This is indicative of a hotplug event as
1155 * opposed to a fault.
1157 if (ocp_trigger_delay_us
> SPMI_VS_OCP_FAULT_DELAY_US
)
1158 vreg
->ocp_count
= 0;
1160 /* Wait for switch output to settle back to 0 V after OCP triggered. */
1161 udelay(SPMI_VS_OCP_FALL_DELAY_US
);
1165 if (vreg
->ocp_count
== 1) {
1166 /* Immediately clear the over current condition. */
1167 spmi_regulator_vs_clear_ocp(vreg
);
1168 } else if (vreg
->ocp_count
<= vreg
->ocp_max_retries
) {
1169 /* Schedule the over current clear task to run later. */
1170 schedule_delayed_work(&vreg
->ocp_work
,
1171 msecs_to_jiffies(vreg
->ocp_retry_delay_ms
) + 1);
1174 "OCP triggered %d times; no further retries\n",
1181 #define SAW3_VCTL_DATA_MASK 0xFF
1182 #define SAW3_VCTL_CLEAR_MASK 0x700FF
1183 #define SAW3_AVS_CTL_EN_MASK 0x1
1184 #define SAW3_AVS_CTL_TGGL_MASK 0x8000000
1185 #define SAW3_AVS_CTL_CLEAR_MASK 0x7efc00
1187 static struct regmap
*saw_regmap
;
1189 static void spmi_saw_set_vdd(void *data
)
1191 u32 vctl
, data3
, avs_ctl
, pmic_sts
;
1192 bool avs_enabled
= false;
1193 unsigned long timeout
;
1194 u8 voltage_sel
= *(u8
*)data
;
1196 regmap_read(saw_regmap
, SAW3_AVS_CTL
, &avs_ctl
);
1197 regmap_read(saw_regmap
, SAW3_VCTL
, &vctl
);
1198 regmap_read(saw_regmap
, SAW3_SPM_PMIC_DATA_3
, &data3
);
1200 /* select the band */
1201 vctl
&= ~SAW3_VCTL_CLEAR_MASK
;
1202 vctl
|= (u32
)voltage_sel
;
1204 data3
&= ~SAW3_VCTL_CLEAR_MASK
;
1205 data3
|= (u32
)voltage_sel
;
1207 /* If AVS is enabled, switch it off during the voltage change */
1208 avs_enabled
= SAW3_AVS_CTL_EN_MASK
& avs_ctl
;
1210 avs_ctl
&= ~SAW3_AVS_CTL_TGGL_MASK
;
1211 regmap_write(saw_regmap
, SAW3_AVS_CTL
, avs_ctl
);
1214 regmap_write(saw_regmap
, SAW3_RST
, 1);
1215 regmap_write(saw_regmap
, SAW3_VCTL
, vctl
);
1216 regmap_write(saw_regmap
, SAW3_SPM_PMIC_DATA_3
, data3
);
1218 timeout
= jiffies
+ usecs_to_jiffies(100);
1220 regmap_read(saw_regmap
, SAW3_PMIC_STS
, &pmic_sts
);
1221 pmic_sts
&= SAW3_VCTL_DATA_MASK
;
1222 if (pmic_sts
== (u32
)voltage_sel
)
1227 } while (time_before(jiffies
, timeout
));
1229 /* After successful voltage change, switch the AVS back on */
1232 avs_ctl
&= ~SAW3_AVS_CTL_CLEAR_MASK
;
1233 avs_ctl
|= ((pmic_sts
- 4) << 10);
1234 avs_ctl
|= (pmic_sts
<< 17);
1235 avs_ctl
|= SAW3_AVS_CTL_TGGL_MASK
;
1236 regmap_write(saw_regmap
, SAW3_AVS_CTL
, avs_ctl
);
1241 spmi_regulator_saw_set_voltage(struct regulator_dev
*rdev
, unsigned selector
)
1243 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
1245 u8 range_sel
, voltage_sel
;
1247 ret
= spmi_sw_selector_to_hw(vreg
, selector
, &range_sel
, &voltage_sel
);
1251 if (0 != range_sel
) {
1252 dev_dbg(&rdev
->dev
, "range_sel = %02X voltage_sel = %02X", \
1253 range_sel
, voltage_sel
);
1257 /* Always do the SAW register writes on the first CPU */
1258 return smp_call_function_single(0, spmi_saw_set_vdd
, \
1259 &voltage_sel
, true);
1262 static struct regulator_ops spmi_saw_ops
= {};
1264 static struct regulator_ops spmi_smps_ops
= {
1265 .enable
= regulator_enable_regmap
,
1266 .disable
= regulator_disable_regmap
,
1267 .is_enabled
= regulator_is_enabled_regmap
,
1268 .set_voltage_sel
= spmi_regulator_common_set_voltage
,
1269 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1270 .get_voltage_sel
= spmi_regulator_common_get_voltage
,
1271 .map_voltage
= spmi_regulator_common_map_voltage
,
1272 .list_voltage
= spmi_regulator_common_list_voltage
,
1273 .set_mode
= spmi_regulator_common_set_mode
,
1274 .get_mode
= spmi_regulator_common_get_mode
,
1275 .set_load
= spmi_regulator_common_set_load
,
1276 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1279 static struct regulator_ops spmi_ldo_ops
= {
1280 .enable
= regulator_enable_regmap
,
1281 .disable
= regulator_disable_regmap
,
1282 .is_enabled
= regulator_is_enabled_regmap
,
1283 .set_voltage_sel
= spmi_regulator_common_set_voltage
,
1284 .get_voltage_sel
= spmi_regulator_common_get_voltage
,
1285 .map_voltage
= spmi_regulator_common_map_voltage
,
1286 .list_voltage
= spmi_regulator_common_list_voltage
,
1287 .set_mode
= spmi_regulator_common_set_mode
,
1288 .get_mode
= spmi_regulator_common_get_mode
,
1289 .set_load
= spmi_regulator_common_set_load
,
1290 .set_bypass
= spmi_regulator_common_set_bypass
,
1291 .get_bypass
= spmi_regulator_common_get_bypass
,
1292 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1293 .set_soft_start
= spmi_regulator_common_set_soft_start
,
1296 static struct regulator_ops spmi_ln_ldo_ops
= {
1297 .enable
= regulator_enable_regmap
,
1298 .disable
= regulator_disable_regmap
,
1299 .is_enabled
= regulator_is_enabled_regmap
,
1300 .set_voltage_sel
= spmi_regulator_common_set_voltage
,
1301 .get_voltage_sel
= spmi_regulator_common_get_voltage
,
1302 .map_voltage
= spmi_regulator_common_map_voltage
,
1303 .list_voltage
= spmi_regulator_common_list_voltage
,
1304 .set_bypass
= spmi_regulator_common_set_bypass
,
1305 .get_bypass
= spmi_regulator_common_get_bypass
,
1308 static struct regulator_ops spmi_vs_ops
= {
1309 .enable
= spmi_regulator_vs_enable
,
1310 .disable
= regulator_disable_regmap
,
1311 .is_enabled
= regulator_is_enabled_regmap
,
1312 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1313 .set_soft_start
= spmi_regulator_common_set_soft_start
,
1314 .set_over_current_protection
= spmi_regulator_vs_ocp
,
1315 .set_mode
= spmi_regulator_common_set_mode
,
1316 .get_mode
= spmi_regulator_common_get_mode
,
1319 static struct regulator_ops spmi_boost_ops
= {
1320 .enable
= regulator_enable_regmap
,
1321 .disable
= regulator_disable_regmap
,
1322 .is_enabled
= regulator_is_enabled_regmap
,
1323 .set_voltage_sel
= spmi_regulator_single_range_set_voltage
,
1324 .get_voltage_sel
= spmi_regulator_single_range_get_voltage
,
1325 .map_voltage
= spmi_regulator_single_map_voltage
,
1326 .list_voltage
= spmi_regulator_common_list_voltage
,
1327 .set_input_current_limit
= spmi_regulator_set_ilim
,
1330 static struct regulator_ops spmi_ftsmps_ops
= {
1331 .enable
= regulator_enable_regmap
,
1332 .disable
= regulator_disable_regmap
,
1333 .is_enabled
= regulator_is_enabled_regmap
,
1334 .set_voltage_sel
= spmi_regulator_common_set_voltage
,
1335 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1336 .get_voltage_sel
= spmi_regulator_common_get_voltage
,
1337 .map_voltage
= spmi_regulator_common_map_voltage
,
1338 .list_voltage
= spmi_regulator_common_list_voltage
,
1339 .set_mode
= spmi_regulator_common_set_mode
,
1340 .get_mode
= spmi_regulator_common_get_mode
,
1341 .set_load
= spmi_regulator_common_set_load
,
1342 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1345 static struct regulator_ops spmi_ult_lo_smps_ops
= {
1346 .enable
= regulator_enable_regmap
,
1347 .disable
= regulator_disable_regmap
,
1348 .is_enabled
= regulator_is_enabled_regmap
,
1349 .set_voltage_sel
= spmi_regulator_ult_lo_smps_set_voltage
,
1350 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1351 .get_voltage_sel
= spmi_regulator_ult_lo_smps_get_voltage
,
1352 .list_voltage
= spmi_regulator_common_list_voltage
,
1353 .set_mode
= spmi_regulator_common_set_mode
,
1354 .get_mode
= spmi_regulator_common_get_mode
,
1355 .set_load
= spmi_regulator_common_set_load
,
1356 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1359 static struct regulator_ops spmi_ult_ho_smps_ops
= {
1360 .enable
= regulator_enable_regmap
,
1361 .disable
= regulator_disable_regmap
,
1362 .is_enabled
= regulator_is_enabled_regmap
,
1363 .set_voltage_sel
= spmi_regulator_single_range_set_voltage
,
1364 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1365 .get_voltage_sel
= spmi_regulator_single_range_get_voltage
,
1366 .map_voltage
= spmi_regulator_single_map_voltage
,
1367 .list_voltage
= spmi_regulator_common_list_voltage
,
1368 .set_mode
= spmi_regulator_common_set_mode
,
1369 .get_mode
= spmi_regulator_common_get_mode
,
1370 .set_load
= spmi_regulator_common_set_load
,
1371 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1374 static struct regulator_ops spmi_ult_ldo_ops
= {
1375 .enable
= regulator_enable_regmap
,
1376 .disable
= regulator_disable_regmap
,
1377 .is_enabled
= regulator_is_enabled_regmap
,
1378 .set_voltage_sel
= spmi_regulator_single_range_set_voltage
,
1379 .get_voltage_sel
= spmi_regulator_single_range_get_voltage
,
1380 .map_voltage
= spmi_regulator_single_map_voltage
,
1381 .list_voltage
= spmi_regulator_common_list_voltage
,
1382 .set_mode
= spmi_regulator_common_set_mode
,
1383 .get_mode
= spmi_regulator_common_get_mode
,
1384 .set_load
= spmi_regulator_common_set_load
,
1385 .set_bypass
= spmi_regulator_common_set_bypass
,
1386 .get_bypass
= spmi_regulator_common_get_bypass
,
1387 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1388 .set_soft_start
= spmi_regulator_common_set_soft_start
,
1391 static struct regulator_ops spmi_ftsmps426_ops
= {
1392 .enable
= regulator_enable_regmap
,
1393 .disable
= regulator_disable_regmap
,
1394 .is_enabled
= regulator_is_enabled_regmap
,
1395 .set_voltage_sel
= spmi_regulator_ftsmps426_set_voltage
,
1396 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1397 .get_voltage_sel
= spmi_regulator_ftsmps426_get_voltage
,
1398 .map_voltage
= spmi_regulator_single_map_voltage
,
1399 .list_voltage
= spmi_regulator_common_list_voltage
,
1400 .set_mode
= spmi_regulator_ftsmps426_set_mode
,
1401 .get_mode
= spmi_regulator_ftsmps426_get_mode
,
1402 .set_load
= spmi_regulator_common_set_load
,
1403 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1406 static struct regulator_ops spmi_hfs430_ops
= {
1407 .enable
= regulator_enable_regmap
,
1408 .disable
= regulator_disable_regmap
,
1409 .is_enabled
= regulator_is_enabled_regmap
,
1410 .set_voltage_sel
= spmi_regulator_ftsmps426_set_voltage
,
1411 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
1412 .get_voltage_sel
= spmi_regulator_ftsmps426_get_voltage
,
1413 .map_voltage
= spmi_regulator_single_map_voltage
,
1414 .list_voltage
= spmi_regulator_common_list_voltage
,
1415 .set_mode
= spmi_regulator_ftsmps426_set_mode
,
1416 .get_mode
= spmi_regulator_ftsmps426_get_mode
,
1419 /* Maximum possible digital major revision value */
1422 static const struct spmi_regulator_mapping supported_regulators
[] = {
1423 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
1424 SPMI_VREG(BUCK
, GP_CTL
, 0, INF
, SMPS
, smps
, smps
, 100000),
1425 SPMI_VREG(BUCK
, HFS430
, 0, INF
, HFS430
, hfs430
, hfs430
, 10000),
1426 SPMI_VREG(LDO
, N300
, 0, INF
, LDO
, ldo
, nldo1
, 10000),
1427 SPMI_VREG(LDO
, N600
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1428 SPMI_VREG(LDO
, N1200
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1429 SPMI_VREG(LDO
, N600
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1430 SPMI_VREG(LDO
, N1200
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1431 SPMI_VREG(LDO
, N600_ST
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1432 SPMI_VREG(LDO
, N1200_ST
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1433 SPMI_VREG(LDO
, N600_ST
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1434 SPMI_VREG(LDO
, N1200_ST
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1435 SPMI_VREG(LDO
, P50
, 0, INF
, LDO
, ldo
, pldo
, 5000),
1436 SPMI_VREG(LDO
, P150
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1437 SPMI_VREG(LDO
, P300
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1438 SPMI_VREG(LDO
, P600
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1439 SPMI_VREG(LDO
, P1200
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1440 SPMI_VREG(LDO
, LN
, 0, INF
, LN_LDO
, ln_ldo
, ln_ldo
, 0),
1441 SPMI_VREG(LDO
, LV_P50
, 0, INF
, LDO
, ldo
, pldo
, 5000),
1442 SPMI_VREG(LDO
, LV_P150
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1443 SPMI_VREG(LDO
, LV_P300
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1444 SPMI_VREG(LDO
, LV_P600
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1445 SPMI_VREG(LDO
, LV_P1200
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1446 SPMI_VREG_VS(LV100
, 0, INF
),
1447 SPMI_VREG_VS(LV300
, 0, INF
),
1448 SPMI_VREG_VS(MV300
, 0, INF
),
1449 SPMI_VREG_VS(MV500
, 0, INF
),
1450 SPMI_VREG_VS(HDMI
, 0, INF
),
1451 SPMI_VREG_VS(OTG
, 0, INF
),
1452 SPMI_VREG(BOOST
, 5V_BOOST
, 0, INF
, BOOST
, boost
, boost
, 0),
1453 SPMI_VREG(FTS
, FTS_CTL
, 0, INF
, FTSMPS
, ftsmps
, ftsmps
, 100000),
1454 SPMI_VREG(FTS
, FTS2p5_CTL
, 0, INF
, FTSMPS
, ftsmps
, ftsmps2p5
, 100000),
1455 SPMI_VREG(FTS
, FTS426_CTL
, 0, INF
, FTSMPS426
, ftsmps426
, ftsmps426
, 100000),
1456 SPMI_VREG(BOOST_BYP
, BB_2A
, 0, INF
, BOOST_BYP
, boost
, boost_byp
, 0),
1457 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL1
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1458 ult_lo_smps
, 100000),
1459 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL2
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1460 ult_lo_smps
, 100000),
1461 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL3
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1462 ult_lo_smps
, 100000),
1463 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL4
, 0, INF
, ULT_HO_SMPS
, ult_ho_smps
,
1464 ult_ho_smps
, 100000),
1465 SPMI_VREG(ULT_LDO
, N300_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1466 SPMI_VREG(ULT_LDO
, N600_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1467 SPMI_VREG(ULT_LDO
, N900_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1468 SPMI_VREG(ULT_LDO
, N1200_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1469 SPMI_VREG(ULT_LDO
, LV_P150
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1470 SPMI_VREG(ULT_LDO
, LV_P300
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1471 SPMI_VREG(ULT_LDO
, LV_P450
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1472 SPMI_VREG(ULT_LDO
, P600
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1473 SPMI_VREG(ULT_LDO
, P150
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1474 SPMI_VREG(ULT_LDO
, P50
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 5000),
1477 static void spmi_calculate_num_voltages(struct spmi_voltage_set_points
*points
)
1480 struct spmi_voltage_range
*range
= points
->range
;
1482 for (; range
< points
->range
+ points
->count
; range
++) {
1484 if (range
->set_point_max_uV
) {
1485 n
= range
->set_point_max_uV
- range
->set_point_min_uV
;
1486 n
= (n
/ range
->step_uV
) + 1;
1488 range
->n_voltages
= n
;
1489 points
->n_voltages
+= n
;
1493 static int spmi_regulator_match(struct spmi_regulator
*vreg
, u16 force_type
)
1495 const struct spmi_regulator_mapping
*mapping
;
1498 u8 version
[SPMI_COMMON_REG_SUBTYPE
- SPMI_COMMON_REG_DIG_MAJOR_REV
+ 1];
1501 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_DIG_MAJOR_REV
, version
,
1502 ARRAY_SIZE(version
));
1504 dev_dbg(vreg
->dev
, "could not read version registers\n");
1507 dig_major_rev
= version
[SPMI_COMMON_REG_DIG_MAJOR_REV
1508 - SPMI_COMMON_REG_DIG_MAJOR_REV
];
1511 type
= version
[SPMI_COMMON_REG_TYPE
-
1512 SPMI_COMMON_REG_DIG_MAJOR_REV
];
1513 subtype
= version
[SPMI_COMMON_REG_SUBTYPE
-
1514 SPMI_COMMON_REG_DIG_MAJOR_REV
];
1516 type
= force_type
>> 8;
1517 subtype
= force_type
;
1520 for (i
= 0; i
< ARRAY_SIZE(supported_regulators
); i
++) {
1521 mapping
= &supported_regulators
[i
];
1522 if (mapping
->type
== type
&& mapping
->subtype
== subtype
1523 && mapping
->revision_min
<= dig_major_rev
1524 && mapping
->revision_max
>= dig_major_rev
)
1529 "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1530 vreg
->desc
.name
, type
, subtype
, dig_major_rev
);
1535 vreg
->logical_type
= mapping
->logical_type
;
1536 vreg
->set_points
= mapping
->set_points
;
1537 vreg
->hpm_min_load
= mapping
->hpm_min_load
;
1538 vreg
->desc
.ops
= mapping
->ops
;
1540 if (mapping
->set_points
) {
1541 if (!mapping
->set_points
->n_voltages
)
1542 spmi_calculate_num_voltages(mapping
->set_points
);
1543 vreg
->desc
.n_voltages
= mapping
->set_points
->n_voltages
;
1549 static int spmi_regulator_init_slew_rate(struct spmi_regulator
*vreg
)
1553 int step
, delay
, slew_rate
, step_delay
;
1554 const struct spmi_voltage_range
*range
;
1556 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_STEP_CTRL
, ®
, 1);
1558 dev_err(vreg
->dev
, "spmi read failed, ret=%d\n", ret
);
1562 range
= spmi_regulator_find_range(vreg
);
1566 switch (vreg
->logical_type
) {
1567 case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS
:
1568 step_delay
= SPMI_FTSMPS_STEP_DELAY
;
1571 step_delay
= SPMI_DEFAULT_STEP_DELAY
;
1575 step
= reg
& SPMI_FTSMPS_STEP_CTRL_STEP_MASK
;
1576 step
>>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT
;
1578 delay
= reg
& SPMI_FTSMPS_STEP_CTRL_DELAY_MASK
;
1579 delay
>>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT
;
1581 /* slew_rate has units of uV/us */
1582 slew_rate
= SPMI_FTSMPS_CLOCK_RATE
* range
->step_uV
* (1 << step
);
1583 slew_rate
/= 1000 * (step_delay
<< delay
);
1584 slew_rate
*= SPMI_FTSMPS_STEP_MARGIN_NUM
;
1585 slew_rate
/= SPMI_FTSMPS_STEP_MARGIN_DEN
;
1587 /* Ensure that the slew rate is greater than 0 */
1588 vreg
->slew_rate
= max(slew_rate
, 1);
1593 static int spmi_regulator_init_slew_rate_ftsmps426(struct spmi_regulator
*vreg
,
1598 int delay
, slew_rate
;
1599 const struct spmi_voltage_range
*range
= &vreg
->set_points
->range
[0];
1601 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_STEP_CTRL
, ®
, 1);
1603 dev_err(vreg
->dev
, "spmi read failed, ret=%d\n", ret
);
1607 delay
= reg
& SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK
;
1608 delay
>>= SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT
;
1610 /* slew_rate has units of uV/us */
1611 slew_rate
= clock_rate
* range
->step_uV
;
1612 slew_rate
/= 1000 * (SPMI_FTSMPS426_STEP_DELAY
<< delay
);
1613 slew_rate
*= SPMI_FTSMPS426_STEP_MARGIN_NUM
;
1614 slew_rate
/= SPMI_FTSMPS426_STEP_MARGIN_DEN
;
1616 /* Ensure that the slew rate is greater than 0 */
1617 vreg
->slew_rate
= max(slew_rate
, 1);
1622 static int spmi_regulator_init_registers(struct spmi_regulator
*vreg
,
1623 const struct spmi_regulator_init_data
*data
)
1626 enum spmi_regulator_logical_type type
;
1627 u8 ctrl_reg
[8], reg
, mask
;
1629 type
= vreg
->logical_type
;
1631 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, ctrl_reg
, 8);
1635 /* Set up enable pin control. */
1636 if ((type
== SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1637 || type
== SPMI_REGULATOR_LOGICAL_TYPE_LDO
1638 || type
== SPMI_REGULATOR_LOGICAL_TYPE_VS
)
1639 && !(data
->pin_ctrl_enable
1640 & SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT
)) {
1641 ctrl_reg
[SPMI_COMMON_IDX_ENABLE
] &=
1642 ~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK
;
1643 ctrl_reg
[SPMI_COMMON_IDX_ENABLE
] |=
1644 data
->pin_ctrl_enable
& SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK
;
1647 /* Set up mode pin control. */
1648 if ((type
== SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1649 || type
== SPMI_REGULATOR_LOGICAL_TYPE_LDO
)
1650 && !(data
->pin_ctrl_hpm
1651 & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT
)) {
1652 ctrl_reg
[SPMI_COMMON_IDX_MODE
] &=
1653 ~SPMI_COMMON_MODE_FOLLOW_ALL_MASK
;
1654 ctrl_reg
[SPMI_COMMON_IDX_MODE
] |=
1655 data
->pin_ctrl_hpm
& SPMI_COMMON_MODE_FOLLOW_ALL_MASK
;
1658 if (type
== SPMI_REGULATOR_LOGICAL_TYPE_VS
1659 && !(data
->pin_ctrl_hpm
& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT
)) {
1660 ctrl_reg
[SPMI_COMMON_IDX_MODE
] &=
1661 ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK
;
1662 ctrl_reg
[SPMI_COMMON_IDX_MODE
] |=
1663 data
->pin_ctrl_hpm
& SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK
;
1666 if ((type
== SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
1667 || type
== SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
1668 || type
== SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO
)
1669 && !(data
->pin_ctrl_hpm
1670 & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT
)) {
1671 ctrl_reg
[SPMI_COMMON_IDX_MODE
] &=
1672 ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK
;
1673 ctrl_reg
[SPMI_COMMON_IDX_MODE
] |=
1674 data
->pin_ctrl_hpm
& SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK
;
1677 /* Write back any control register values that were modified. */
1678 ret
= spmi_vreg_write(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, ctrl_reg
, 8);
1682 /* Set soft start strength and over current protection for VS. */
1683 if (type
== SPMI_REGULATOR_LOGICAL_TYPE_VS
) {
1684 if (data
->vs_soft_start_strength
1685 != SPMI_VS_SOFT_START_STR_HW_DEFAULT
) {
1686 reg
= data
->vs_soft_start_strength
1687 & SPMI_VS_SOFT_START_SEL_MASK
;
1688 mask
= SPMI_VS_SOFT_START_SEL_MASK
;
1689 return spmi_vreg_update_bits(vreg
,
1690 SPMI_VS_REG_SOFT_START
,
1698 static void spmi_regulator_get_dt_config(struct spmi_regulator
*vreg
,
1699 struct device_node
*node
, struct spmi_regulator_init_data
*data
)
1702 * Initialize configuration parameters to use hardware default in case
1703 * no value is specified via device tree.
1705 data
->pin_ctrl_enable
= SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT
;
1706 data
->pin_ctrl_hpm
= SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT
;
1707 data
->vs_soft_start_strength
= SPMI_VS_SOFT_START_STR_HW_DEFAULT
;
1709 /* These bindings are optional, so it is okay if they aren't found. */
1710 of_property_read_u32(node
, "qcom,ocp-max-retries",
1711 &vreg
->ocp_max_retries
);
1712 of_property_read_u32(node
, "qcom,ocp-retry-delay",
1713 &vreg
->ocp_retry_delay_ms
);
1714 of_property_read_u32(node
, "qcom,pin-ctrl-enable",
1715 &data
->pin_ctrl_enable
);
1716 of_property_read_u32(node
, "qcom,pin-ctrl-hpm", &data
->pin_ctrl_hpm
);
1717 of_property_read_u32(node
, "qcom,vs-soft-start-strength",
1718 &data
->vs_soft_start_strength
);
1721 static unsigned int spmi_regulator_of_map_mode(unsigned int mode
)
1724 return REGULATOR_MODE_NORMAL
;
1726 return REGULATOR_MODE_FAST
;
1728 return REGULATOR_MODE_IDLE
;
1731 static int spmi_regulator_of_parse(struct device_node
*node
,
1732 const struct regulator_desc
*desc
,
1733 struct regulator_config
*config
)
1735 struct spmi_regulator_init_data data
= { };
1736 struct spmi_regulator
*vreg
= config
->driver_data
;
1737 struct device
*dev
= config
->dev
;
1740 spmi_regulator_get_dt_config(vreg
, node
, &data
);
1742 if (!vreg
->ocp_max_retries
)
1743 vreg
->ocp_max_retries
= SPMI_VS_OCP_DEFAULT_MAX_RETRIES
;
1744 if (!vreg
->ocp_retry_delay_ms
)
1745 vreg
->ocp_retry_delay_ms
= SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS
;
1747 ret
= spmi_regulator_init_registers(vreg
, &data
);
1749 dev_err(dev
, "common initialization failed, ret=%d\n", ret
);
1753 switch (vreg
->logical_type
) {
1754 case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS
:
1755 case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
:
1756 case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
:
1757 case SPMI_REGULATOR_LOGICAL_TYPE_SMPS
:
1758 ret
= spmi_regulator_init_slew_rate(vreg
);
1762 case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426
:
1763 ret
= spmi_regulator_init_slew_rate_ftsmps426(vreg
,
1764 SPMI_FTSMPS426_CLOCK_RATE
);
1768 case SPMI_REGULATOR_LOGICAL_TYPE_HFS430
:
1769 ret
= spmi_regulator_init_slew_rate_ftsmps426(vreg
,
1770 SPMI_HFS430_CLOCK_RATE
);
1778 if (vreg
->logical_type
!= SPMI_REGULATOR_LOGICAL_TYPE_VS
)
1781 if (vreg
->ocp_irq
) {
1782 ret
= devm_request_irq(dev
, vreg
->ocp_irq
,
1783 spmi_regulator_vs_ocp_isr
, IRQF_TRIGGER_RISING
, "ocp",
1786 dev_err(dev
, "failed to request irq %d, ret=%d\n",
1787 vreg
->ocp_irq
, ret
);
1791 INIT_DELAYED_WORK(&vreg
->ocp_work
, spmi_regulator_vs_ocp_work
);
1797 static const struct spmi_regulator_data pm8941_regulators
[] = {
1798 { "s1", 0x1400, "vdd_s1", },
1799 { "s2", 0x1700, "vdd_s2", },
1800 { "s3", 0x1a00, "vdd_s3", },
1802 { "l1", 0x4000, "vdd_l1_l3", },
1803 { "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1804 { "l3", 0x4200, "vdd_l1_l3", },
1805 { "l4", 0x4300, "vdd_l4_l11", },
1806 { "l5", 0x4400, "vdd_l5_l7", NULL
, 0x0410 },
1807 { "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1808 { "l7", 0x4600, "vdd_l5_l7", NULL
, 0x0410 },
1809 { "l8", 0x4700, "vdd_l8_l16_l18_19", },
1810 { "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1811 { "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1812 { "l11", 0x4a00, "vdd_l4_l11", },
1813 { "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1814 { "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1815 { "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1816 { "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1817 { "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1818 { "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1819 { "l18", 0x5100, "vdd_l8_l16_l18_19", },
1820 { "l19", 0x5200, "vdd_l8_l16_l18_19", },
1821 { "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1822 { "l21", 0x5400, "vdd_l21", },
1823 { "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1824 { "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1825 { "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1826 { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1827 { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1828 { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
1829 { "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", },
1830 { "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", },
1834 static const struct spmi_regulator_data pm8841_regulators
[] = {
1835 { "s1", 0x1400, "vdd_s1", },
1836 { "s2", 0x1700, "vdd_s2", NULL
, 0x1c08 },
1837 { "s3", 0x1a00, "vdd_s3", },
1838 { "s4", 0x1d00, "vdd_s4", NULL
, 0x1c08 },
1839 { "s5", 0x2000, "vdd_s5", NULL
, 0x1c08 },
1840 { "s6", 0x2300, "vdd_s6", NULL
, 0x1c08 },
1841 { "s7", 0x2600, "vdd_s7", NULL
, 0x1c08 },
1842 { "s8", 0x2900, "vdd_s8", NULL
, 0x1c08 },
1846 static const struct spmi_regulator_data pm8916_regulators
[] = {
1847 { "s1", 0x1400, "vdd_s1", },
1848 { "s2", 0x1700, "vdd_s2", },
1849 { "s3", 0x1a00, "vdd_s3", },
1850 { "s4", 0x1d00, "vdd_s4", },
1851 { "l1", 0x4000, "vdd_l1_l3", },
1852 { "l2", 0x4100, "vdd_l2", },
1853 { "l3", 0x4200, "vdd_l1_l3", },
1854 { "l4", 0x4300, "vdd_l4_l5_l6", },
1855 { "l5", 0x4400, "vdd_l4_l5_l6", },
1856 { "l6", 0x4500, "vdd_l4_l5_l6", },
1857 { "l7", 0x4600, "vdd_l7", },
1858 { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1859 { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1860 { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1861 { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1862 { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1863 { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1864 { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1865 { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1866 { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1867 { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1868 { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1872 static const struct spmi_regulator_data pm8950_regulators
[] = {
1873 { "s1", 0x1400, "vdd_s1", },
1874 { "s2", 0x1700, "vdd_s2", },
1875 { "s3", 0x1a00, "vdd_s3", },
1876 { "s4", 0x1d00, "vdd_s4", },
1877 { "s5", 0x2000, "vdd_s5", },
1878 { "s6", 0x2300, "vdd_s6", },
1879 { "l1", 0x4000, "vdd_l1_l19", },
1880 { "l2", 0x4100, "vdd_l2_l23", },
1881 { "l3", 0x4200, "vdd_l3", },
1882 { "l4", 0x4300, "vdd_l4_l5_l6_l7_l16", },
1883 { "l5", 0x4400, "vdd_l4_l5_l6_l7_l16", },
1884 { "l6", 0x4500, "vdd_l4_l5_l6_l7_l16", },
1885 { "l7", 0x4600, "vdd_l4_l5_l6_l7_l16", },
1886 { "l8", 0x4700, "vdd_l8_l11_l12_l17_l22", },
1887 { "l9", 0x4800, "vdd_l9_l10_l13_l14_l15_l18", },
1888 { "l10", 0x4900, "vdd_l9_l10_l13_l14_l15_l18", },
1889 { "l11", 0x4a00, "vdd_l8_l11_l12_l17_l22", },
1890 { "l12", 0x4b00, "vdd_l8_l11_l12_l17_l22", },
1891 { "l13", 0x4c00, "vdd_l9_l10_l13_l14_l15_l18", },
1892 { "l14", 0x4d00, "vdd_l9_l10_l13_l14_l15_l18", },
1893 { "l15", 0x4e00, "vdd_l9_l10_l13_l14_l15_l18", },
1894 { "l16", 0x4f00, "vdd_l4_l5_l6_l7_l16", },
1895 { "l17", 0x5000, "vdd_l8_l11_l12_l17_l22", },
1896 { "l18", 0x5100, "vdd_l9_l10_l13_l14_l15_l18", },
1897 { "l19", 0x5200, "vdd_l1_l19", },
1898 { "l20", 0x5300, "vdd_l20", },
1899 { "l21", 0x5400, "vdd_l21", },
1900 { "l22", 0x5500, "vdd_l8_l11_l12_l17_l22", },
1901 { "l23", 0x5600, "vdd_l2_l23", },
1905 static const struct spmi_regulator_data pm8994_regulators
[] = {
1906 { "s1", 0x1400, "vdd_s1", },
1907 { "s2", 0x1700, "vdd_s2", },
1908 { "s3", 0x1a00, "vdd_s3", },
1909 { "s4", 0x1d00, "vdd_s4", },
1910 { "s5", 0x2000, "vdd_s5", },
1911 { "s6", 0x2300, "vdd_s6", },
1912 { "s7", 0x2600, "vdd_s7", },
1913 { "s8", 0x2900, "vdd_s8", },
1914 { "s9", 0x2c00, "vdd_s9", },
1915 { "s10", 0x2f00, "vdd_s10", },
1916 { "s11", 0x3200, "vdd_s11", },
1917 { "s12", 0x3500, "vdd_s12", },
1918 { "l1", 0x4000, "vdd_l1", },
1919 { "l2", 0x4100, "vdd_l2_l26_l28", },
1920 { "l3", 0x4200, "vdd_l3_l11", },
1921 { "l4", 0x4300, "vdd_l4_l27_l31", },
1922 { "l5", 0x4400, "vdd_l5_l7", },
1923 { "l6", 0x4500, "vdd_l6_l12_l32", },
1924 { "l7", 0x4600, "vdd_l5_l7", },
1925 { "l8", 0x4700, "vdd_l8_l16_l30", },
1926 { "l9", 0x4800, "vdd_l9_l10_l18_l22", },
1927 { "l10", 0x4900, "vdd_l9_l10_l18_l22", },
1928 { "l11", 0x4a00, "vdd_l3_l11", },
1929 { "l12", 0x4b00, "vdd_l6_l12_l32", },
1930 { "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
1931 { "l14", 0x4d00, "vdd_l14_l15", },
1932 { "l15", 0x4e00, "vdd_l14_l15", },
1933 { "l16", 0x4f00, "vdd_l8_l16_l30", },
1934 { "l17", 0x5000, "vdd_l17_l29", },
1935 { "l18", 0x5100, "vdd_l9_l10_l18_l22", },
1936 { "l19", 0x5200, "vdd_l13_l19_l23_l24", },
1937 { "l20", 0x5300, "vdd_l20_l21", },
1938 { "l21", 0x5400, "vdd_l20_l21", },
1939 { "l22", 0x5500, "vdd_l9_l10_l18_l22", },
1940 { "l23", 0x5600, "vdd_l13_l19_l23_l24", },
1941 { "l24", 0x5700, "vdd_l13_l19_l23_l24", },
1942 { "l25", 0x5800, "vdd_l25", },
1943 { "l26", 0x5900, "vdd_l2_l26_l28", },
1944 { "l27", 0x5a00, "vdd_l4_l27_l31", },
1945 { "l28", 0x5b00, "vdd_l2_l26_l28", },
1946 { "l29", 0x5c00, "vdd_l17_l29", },
1947 { "l30", 0x5d00, "vdd_l8_l16_l30", },
1948 { "l31", 0x5e00, "vdd_l4_l27_l31", },
1949 { "l32", 0x5f00, "vdd_l6_l12_l32", },
1950 { "lvs1", 0x8000, "vdd_lvs_1_2", },
1951 { "lvs2", 0x8100, "vdd_lvs_1_2", },
1955 static const struct spmi_regulator_data pmi8994_regulators
[] = {
1956 { "s1", 0x1400, "vdd_s1", },
1957 { "s2", 0x1700, "vdd_s2", },
1958 { "s3", 0x1a00, "vdd_s3", },
1959 { "l1", 0x4000, "vdd_l1", },
1963 static const struct spmi_regulator_data pm8004_regulators
[] = {
1964 { "s2", 0x1700, "vdd_s2", },
1965 { "s5", 0x2000, "vdd_s5", },
1969 static const struct spmi_regulator_data pm8005_regulators
[] = {
1970 { "s1", 0x1400, "vdd_s1", },
1971 { "s2", 0x1700, "vdd_s2", },
1972 { "s3", 0x1a00, "vdd_s3", },
1973 { "s4", 0x1d00, "vdd_s4", },
1977 static const struct spmi_regulator_data pms405_regulators
[] = {
1978 { "s3", 0x1a00, "vdd_s3"},
1982 static const struct of_device_id qcom_spmi_regulator_match
[] = {
1983 { .compatible
= "qcom,pm8004-regulators", .data
= &pm8004_regulators
},
1984 { .compatible
= "qcom,pm8005-regulators", .data
= &pm8005_regulators
},
1985 { .compatible
= "qcom,pm8841-regulators", .data
= &pm8841_regulators
},
1986 { .compatible
= "qcom,pm8916-regulators", .data
= &pm8916_regulators
},
1987 { .compatible
= "qcom,pm8941-regulators", .data
= &pm8941_regulators
},
1988 { .compatible
= "qcom,pm8950-regulators", .data
= &pm8950_regulators
},
1989 { .compatible
= "qcom,pm8994-regulators", .data
= &pm8994_regulators
},
1990 { .compatible
= "qcom,pmi8994-regulators", .data
= &pmi8994_regulators
},
1991 { .compatible
= "qcom,pms405-regulators", .data
= &pms405_regulators
},
1994 MODULE_DEVICE_TABLE(of
, qcom_spmi_regulator_match
);
1996 static int qcom_spmi_regulator_probe(struct platform_device
*pdev
)
1998 const struct spmi_regulator_data
*reg
;
1999 const struct spmi_voltage_range
*range
;
2000 const struct of_device_id
*match
;
2001 struct regulator_config config
= { };
2002 struct regulator_dev
*rdev
;
2003 struct spmi_regulator
*vreg
;
2004 struct regmap
*regmap
;
2006 struct device
*dev
= &pdev
->dev
;
2007 struct device_node
*node
= pdev
->dev
.of_node
;
2008 struct device_node
*syscon
, *reg_node
;
2009 struct property
*reg_prop
;
2011 struct list_head
*vreg_list
;
2013 vreg_list
= devm_kzalloc(dev
, sizeof(*vreg_list
), GFP_KERNEL
);
2016 INIT_LIST_HEAD(vreg_list
);
2017 platform_set_drvdata(pdev
, vreg_list
);
2019 regmap
= dev_get_regmap(dev
->parent
, NULL
);
2023 match
= of_match_device(qcom_spmi_regulator_match
, &pdev
->dev
);
2027 if (of_find_property(node
, "qcom,saw-reg", &lenp
)) {
2028 syscon
= of_parse_phandle(node
, "qcom,saw-reg", 0);
2029 saw_regmap
= syscon_node_to_regmap(syscon
);
2030 of_node_put(syscon
);
2031 if (IS_ERR(saw_regmap
))
2032 dev_err(dev
, "ERROR reading SAW regmap\n");
2035 for (reg
= match
->data
; reg
->name
; reg
++) {
2038 reg_node
= of_get_child_by_name(node
, reg
->name
);
2039 reg_prop
= of_find_property(reg_node
, "qcom,saw-slave",
2041 of_node_put(reg_node
);
2046 vreg
= devm_kzalloc(dev
, sizeof(*vreg
), GFP_KERNEL
);
2051 vreg
->base
= reg
->base
;
2052 vreg
->regmap
= regmap
;
2054 vreg
->ocp_irq
= platform_get_irq_byname(pdev
, reg
->ocp
);
2055 if (vreg
->ocp_irq
< 0) {
2056 ret
= vreg
->ocp_irq
;
2061 vreg
->desc
.owner
= THIS_MODULE
;
2062 vreg
->desc
.type
= REGULATOR_VOLTAGE
;
2063 vreg
->desc
.enable_reg
= reg
->base
+ SPMI_COMMON_REG_ENABLE
;
2064 vreg
->desc
.enable_mask
= SPMI_COMMON_ENABLE_MASK
;
2065 vreg
->desc
.enable_val
= SPMI_COMMON_ENABLE
;
2066 vreg
->desc
.name
= name
= reg
->name
;
2067 vreg
->desc
.supply_name
= reg
->supply
;
2068 vreg
->desc
.of_match
= reg
->name
;
2069 vreg
->desc
.of_parse_cb
= spmi_regulator_of_parse
;
2070 vreg
->desc
.of_map_mode
= spmi_regulator_of_map_mode
;
2072 ret
= spmi_regulator_match(vreg
, reg
->force_type
);
2077 reg_node
= of_get_child_by_name(node
, reg
->name
);
2078 reg_prop
= of_find_property(reg_node
, "qcom,saw-leader",
2080 of_node_put(reg_node
);
2082 spmi_saw_ops
= *(vreg
->desc
.ops
);
2083 spmi_saw_ops
.set_voltage_sel
=
2084 spmi_regulator_saw_set_voltage
;
2085 vreg
->desc
.ops
= &spmi_saw_ops
;
2089 if (vreg
->set_points
&& vreg
->set_points
->count
== 1) {
2090 /* since there is only one range */
2091 range
= vreg
->set_points
->range
;
2092 vreg
->desc
.uV_step
= range
->step_uV
;
2096 config
.driver_data
= vreg
;
2097 config
.regmap
= regmap
;
2098 rdev
= devm_regulator_register(dev
, &vreg
->desc
, &config
);
2100 dev_err(dev
, "failed to register %s\n", name
);
2101 ret
= PTR_ERR(rdev
);
2105 INIT_LIST_HEAD(&vreg
->node
);
2106 list_add(&vreg
->node
, vreg_list
);
2112 list_for_each_entry(vreg
, vreg_list
, node
)
2114 cancel_delayed_work_sync(&vreg
->ocp_work
);
2118 static int qcom_spmi_regulator_remove(struct platform_device
*pdev
)
2120 struct spmi_regulator
*vreg
;
2121 struct list_head
*vreg_list
= platform_get_drvdata(pdev
);
2123 list_for_each_entry(vreg
, vreg_list
, node
)
2125 cancel_delayed_work_sync(&vreg
->ocp_work
);
2130 static struct platform_driver qcom_spmi_regulator_driver
= {
2132 .name
= "qcom-spmi-regulator",
2133 .of_match_table
= qcom_spmi_regulator_match
,
2135 .probe
= qcom_spmi_regulator_probe
,
2136 .remove
= qcom_spmi_regulator_remove
,
2138 module_platform_driver(qcom_spmi_regulator_driver
);
2140 MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
2141 MODULE_LICENSE("GPL v2");
2142 MODULE_ALIAS("platform:qcom-spmi-regulator");