2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
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.
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.
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/bitops.h>
20 #include <linux/slab.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/ktime.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regmap.h>
27 #include <linux/list.h>
29 /* These types correspond to unique register layouts. */
30 enum spmi_regulator_logical_type
{
31 SPMI_REGULATOR_LOGICAL_TYPE_SMPS
,
32 SPMI_REGULATOR_LOGICAL_TYPE_LDO
,
33 SPMI_REGULATOR_LOGICAL_TYPE_VS
,
34 SPMI_REGULATOR_LOGICAL_TYPE_BOOST
,
35 SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS
,
36 SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP
,
37 SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO
,
38 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
,
39 SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
,
40 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO
,
43 enum spmi_regulator_type
{
44 SPMI_REGULATOR_TYPE_BUCK
= 0x03,
45 SPMI_REGULATOR_TYPE_LDO
= 0x04,
46 SPMI_REGULATOR_TYPE_VS
= 0x05,
47 SPMI_REGULATOR_TYPE_BOOST
= 0x1b,
48 SPMI_REGULATOR_TYPE_FTS
= 0x1c,
49 SPMI_REGULATOR_TYPE_BOOST_BYP
= 0x1f,
50 SPMI_REGULATOR_TYPE_ULT_LDO
= 0x21,
51 SPMI_REGULATOR_TYPE_ULT_BUCK
= 0x22,
54 enum spmi_regulator_subtype
{
55 SPMI_REGULATOR_SUBTYPE_GP_CTL
= 0x08,
56 SPMI_REGULATOR_SUBTYPE_RF_CTL
= 0x09,
57 SPMI_REGULATOR_SUBTYPE_N50
= 0x01,
58 SPMI_REGULATOR_SUBTYPE_N150
= 0x02,
59 SPMI_REGULATOR_SUBTYPE_N300
= 0x03,
60 SPMI_REGULATOR_SUBTYPE_N600
= 0x04,
61 SPMI_REGULATOR_SUBTYPE_N1200
= 0x05,
62 SPMI_REGULATOR_SUBTYPE_N600_ST
= 0x06,
63 SPMI_REGULATOR_SUBTYPE_N1200_ST
= 0x07,
64 SPMI_REGULATOR_SUBTYPE_N900_ST
= 0x14,
65 SPMI_REGULATOR_SUBTYPE_N300_ST
= 0x15,
66 SPMI_REGULATOR_SUBTYPE_P50
= 0x08,
67 SPMI_REGULATOR_SUBTYPE_P150
= 0x09,
68 SPMI_REGULATOR_SUBTYPE_P300
= 0x0a,
69 SPMI_REGULATOR_SUBTYPE_P600
= 0x0b,
70 SPMI_REGULATOR_SUBTYPE_P1200
= 0x0c,
71 SPMI_REGULATOR_SUBTYPE_LN
= 0x10,
72 SPMI_REGULATOR_SUBTYPE_LV_P50
= 0x28,
73 SPMI_REGULATOR_SUBTYPE_LV_P150
= 0x29,
74 SPMI_REGULATOR_SUBTYPE_LV_P300
= 0x2a,
75 SPMI_REGULATOR_SUBTYPE_LV_P600
= 0x2b,
76 SPMI_REGULATOR_SUBTYPE_LV_P1200
= 0x2c,
77 SPMI_REGULATOR_SUBTYPE_LV_P450
= 0x2d,
78 SPMI_REGULATOR_SUBTYPE_LV100
= 0x01,
79 SPMI_REGULATOR_SUBTYPE_LV300
= 0x02,
80 SPMI_REGULATOR_SUBTYPE_MV300
= 0x08,
81 SPMI_REGULATOR_SUBTYPE_MV500
= 0x09,
82 SPMI_REGULATOR_SUBTYPE_HDMI
= 0x10,
83 SPMI_REGULATOR_SUBTYPE_OTG
= 0x11,
84 SPMI_REGULATOR_SUBTYPE_5V_BOOST
= 0x01,
85 SPMI_REGULATOR_SUBTYPE_FTS_CTL
= 0x08,
86 SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL
= 0x09,
87 SPMI_REGULATOR_SUBTYPE_BB_2A
= 0x01,
88 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1
= 0x0d,
89 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2
= 0x0e,
90 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3
= 0x0f,
91 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4
= 0x10,
94 enum spmi_common_regulator_registers
{
95 SPMI_COMMON_REG_DIG_MAJOR_REV
= 0x01,
96 SPMI_COMMON_REG_TYPE
= 0x04,
97 SPMI_COMMON_REG_SUBTYPE
= 0x05,
98 SPMI_COMMON_REG_VOLTAGE_RANGE
= 0x40,
99 SPMI_COMMON_REG_VOLTAGE_SET
= 0x41,
100 SPMI_COMMON_REG_MODE
= 0x45,
101 SPMI_COMMON_REG_ENABLE
= 0x46,
102 SPMI_COMMON_REG_PULL_DOWN
= 0x48,
103 SPMI_COMMON_REG_SOFT_START
= 0x4c,
104 SPMI_COMMON_REG_STEP_CTRL
= 0x61,
107 enum spmi_vs_registers
{
108 SPMI_VS_REG_OCP
= 0x4a,
109 SPMI_VS_REG_SOFT_START
= 0x4c,
112 enum spmi_boost_registers
{
113 SPMI_BOOST_REG_CURRENT_LIMIT
= 0x4a,
116 enum spmi_boost_byp_registers
{
117 SPMI_BOOST_BYP_REG_CURRENT_LIMIT
= 0x4b,
120 /* Used for indexing into ctrl_reg. These are offets from 0x40 */
121 enum spmi_common_control_register_index
{
122 SPMI_COMMON_IDX_VOLTAGE_RANGE
= 0,
123 SPMI_COMMON_IDX_VOLTAGE_SET
= 1,
124 SPMI_COMMON_IDX_MODE
= 5,
125 SPMI_COMMON_IDX_ENABLE
= 6,
128 /* Common regulator control register layout */
129 #define SPMI_COMMON_ENABLE_MASK 0x80
130 #define SPMI_COMMON_ENABLE 0x80
131 #define SPMI_COMMON_DISABLE 0x00
132 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
133 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
134 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
135 #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
136 #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f
138 /* Common regulator mode register layout */
139 #define SPMI_COMMON_MODE_HPM_MASK 0x80
140 #define SPMI_COMMON_MODE_AUTO_MASK 0x40
141 #define SPMI_COMMON_MODE_BYPASS_MASK 0x20
142 #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
143 #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
144 #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
145 #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
146 #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
147 #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f
149 /* Common regulator pull down control register layout */
150 #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80
152 /* LDO regulator current limit control register layout */
153 #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
155 /* LDO regulator soft start control register layout */
156 #define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80
158 /* VS regulator over current protection control register layout */
159 #define SPMI_VS_OCP_OVERRIDE 0x01
160 #define SPMI_VS_OCP_NO_OVERRIDE 0x00
162 /* VS regulator soft start control register layout */
163 #define SPMI_VS_SOFT_START_ENABLE_MASK 0x80
164 #define SPMI_VS_SOFT_START_SEL_MASK 0x03
166 /* Boost regulator current limit control register layout */
167 #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
168 #define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07
170 #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10
171 #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
172 #define SPMI_VS_OCP_FALL_DELAY_US 90
173 #define SPMI_VS_OCP_FAULT_DELAY_US 20000
175 #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18
176 #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3
177 #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07
178 #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0
180 /* Clock rate in kHz of the FTSMPS regulator reference clock. */
181 #define SPMI_FTSMPS_CLOCK_RATE 19200
183 /* Minimum voltage stepper delay for each step. */
184 #define SPMI_FTSMPS_STEP_DELAY 8
187 * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
188 * adjust the step rate in order to account for oscillator variance.
190 #define SPMI_FTSMPS_STEP_MARGIN_NUM 4
191 #define SPMI_FTSMPS_STEP_MARGIN_DEN 5
194 * This voltage in uV is returned by get_voltage functions when there is no way
195 * to determine the current voltage level. It is needed because the regulator
196 * framework treats a 0 uV voltage as an error.
198 #define VOLTAGE_UNKNOWN 1
200 /* VSET value to decide the range of ULT SMPS */
201 #define ULT_SMPS_RANGE_SPLIT 0x60
204 * struct spmi_voltage_range - regulator set point voltage mapping description
205 * @min_uV: Minimum programmable output voltage resulting from
206 * set point register value 0x00
207 * @max_uV: Maximum programmable output voltage
208 * @step_uV: Output voltage increase resulting from the set point
209 * register value increasing by 1
210 * @set_point_min_uV: Minimum allowed voltage
211 * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order
212 * to pick which range should be used in the case of
213 * overlapping set points.
214 * @n_voltages: Number of preferred voltage set points present in this
216 * @range_sel: Voltage range register value corresponding to this range
218 * The following relationships must be true for the values used in this struct:
219 * (max_uV - min_uV) % step_uV == 0
220 * (set_point_min_uV - min_uV) % step_uV == 0*
221 * (set_point_max_uV - min_uV) % step_uV == 0*
222 * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
224 * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
225 * specify that the voltage range has meaning, but is not preferred.
227 struct spmi_voltage_range
{
231 int set_point_min_uV
;
232 int set_point_max_uV
;
238 * The ranges specified in the spmi_voltage_set_points struct must be listed
239 * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
241 struct spmi_voltage_set_points
{
242 struct spmi_voltage_range
*range
;
247 struct spmi_regulator
{
248 struct regulator_desc desc
;
250 struct delayed_work ocp_work
;
251 struct regmap
*regmap
;
252 struct spmi_voltage_set_points
*set_points
;
253 enum spmi_regulator_logical_type logical_type
;
257 int ocp_retry_delay_ms
;
260 ktime_t vs_enable_time
;
262 struct list_head node
;
265 struct spmi_regulator_mapping
{
266 enum spmi_regulator_type type
;
267 enum spmi_regulator_subtype subtype
;
268 enum spmi_regulator_logical_type logical_type
;
271 struct regulator_ops
*ops
;
272 struct spmi_voltage_set_points
*set_points
;
276 struct spmi_regulator_data
{
284 #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
285 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
287 .type = SPMI_REGULATOR_TYPE_##_type, \
288 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
289 .revision_min = _dig_major_min, \
290 .revision_max = _dig_major_max, \
291 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
292 .ops = &spmi_##_ops_val##_ops, \
293 .set_points = &_set_points_val##_set_points, \
294 .hpm_min_load = _hpm_min_load, \
297 #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
299 .type = SPMI_REGULATOR_TYPE_VS, \
300 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
301 .revision_min = _dig_major_min, \
302 .revision_max = _dig_major_max, \
303 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \
304 .ops = &spmi_vs_ops, \
307 #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
308 _set_point_max_uV, _max_uV, _step_uV) \
312 .set_point_min_uV = _set_point_min_uV, \
313 .set_point_max_uV = _set_point_max_uV, \
314 .step_uV = _step_uV, \
315 .range_sel = _range_sel, \
318 #define DEFINE_SPMI_SET_POINTS(name) \
319 struct spmi_voltage_set_points name##_set_points = { \
320 .range = name##_ranges, \
321 .count = ARRAY_SIZE(name##_ranges), \
325 * These tables contain the physically available PMIC regulator voltage setpoint
326 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
327 * to ensure that the setpoints available to software are monotonically
328 * increasing and unique. The set_voltage callback functions expect these
329 * properties to hold.
331 static struct spmi_voltage_range pldo_ranges
[] = {
332 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
333 SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
334 SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
337 static struct spmi_voltage_range nldo1_ranges
[] = {
338 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
341 static struct spmi_voltage_range nldo2_ranges
[] = {
342 SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500),
343 SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250),
344 SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500),
347 static struct spmi_voltage_range nldo3_ranges
[] = {
348 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
349 SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500),
350 SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500),
353 static struct spmi_voltage_range ln_ldo_ranges
[] = {
354 SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000),
355 SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
358 static struct spmi_voltage_range smps_ranges
[] = {
359 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
360 SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
363 static struct spmi_voltage_range ftsmps_ranges
[] = {
364 SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000),
365 SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000),
368 static struct spmi_voltage_range ftsmps2p5_ranges
[] = {
369 SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000),
370 SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000),
373 static struct spmi_voltage_range boost_ranges
[] = {
374 SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
377 static struct spmi_voltage_range boost_byp_ranges
[] = {
378 SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
381 static struct spmi_voltage_range ult_lo_smps_ranges
[] = {
382 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
383 SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000),
386 static struct spmi_voltage_range ult_ho_smps_ranges
[] = {
387 SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
390 static struct spmi_voltage_range ult_nldo_ranges
[] = {
391 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
394 static struct spmi_voltage_range ult_pldo_ranges
[] = {
395 SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
398 static DEFINE_SPMI_SET_POINTS(pldo
);
399 static DEFINE_SPMI_SET_POINTS(nldo1
);
400 static DEFINE_SPMI_SET_POINTS(nldo2
);
401 static DEFINE_SPMI_SET_POINTS(nldo3
);
402 static DEFINE_SPMI_SET_POINTS(ln_ldo
);
403 static DEFINE_SPMI_SET_POINTS(smps
);
404 static DEFINE_SPMI_SET_POINTS(ftsmps
);
405 static DEFINE_SPMI_SET_POINTS(ftsmps2p5
);
406 static DEFINE_SPMI_SET_POINTS(boost
);
407 static DEFINE_SPMI_SET_POINTS(boost_byp
);
408 static DEFINE_SPMI_SET_POINTS(ult_lo_smps
);
409 static DEFINE_SPMI_SET_POINTS(ult_ho_smps
);
410 static DEFINE_SPMI_SET_POINTS(ult_nldo
);
411 static DEFINE_SPMI_SET_POINTS(ult_pldo
);
413 static inline int spmi_vreg_read(struct spmi_regulator
*vreg
, u16 addr
, u8
*buf
,
416 return regmap_bulk_read(vreg
->regmap
, vreg
->base
+ addr
, buf
, len
);
419 static inline int spmi_vreg_write(struct spmi_regulator
*vreg
, u16 addr
,
422 return regmap_bulk_write(vreg
->regmap
, vreg
->base
+ addr
, buf
, len
);
425 static int spmi_vreg_update_bits(struct spmi_regulator
*vreg
, u16 addr
, u8 val
,
428 return regmap_update_bits(vreg
->regmap
, vreg
->base
+ addr
, mask
, val
);
431 static int spmi_regulator_common_is_enabled(struct regulator_dev
*rdev
)
433 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
436 spmi_vreg_read(vreg
, SPMI_COMMON_REG_ENABLE
, ®
, 1);
438 return (reg
& SPMI_COMMON_ENABLE_MASK
) == SPMI_COMMON_ENABLE
;
441 static int spmi_regulator_common_enable(struct regulator_dev
*rdev
)
443 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
445 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
446 SPMI_COMMON_ENABLE
, SPMI_COMMON_ENABLE_MASK
);
449 static int spmi_regulator_vs_enable(struct regulator_dev
*rdev
)
451 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
455 vreg
->vs_enable_time
= ktime_get();
458 return spmi_regulator_common_enable(rdev
);
461 static int spmi_regulator_common_disable(struct regulator_dev
*rdev
)
463 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
465 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
466 SPMI_COMMON_DISABLE
, SPMI_COMMON_ENABLE_MASK
);
469 static int spmi_regulator_select_voltage(struct spmi_regulator
*vreg
,
470 int min_uV
, int max_uV
, u8
*range_sel
, u8
*voltage_sel
,
473 const struct spmi_voltage_range
*range
;
475 int lim_min_uV
, lim_max_uV
, i
, range_id
, range_max_uV
;
477 /* Check if request voltage is outside of physically settable range. */
478 lim_min_uV
= vreg
->set_points
->range
[0].set_point_min_uV
;
480 vreg
->set_points
->range
[vreg
->set_points
->count
- 1].set_point_max_uV
;
482 if (uV
< lim_min_uV
&& max_uV
>= lim_min_uV
)
485 if (uV
< lim_min_uV
|| uV
> lim_max_uV
) {
487 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
488 min_uV
, max_uV
, lim_min_uV
, lim_max_uV
);
492 /* Find the range which uV is inside of. */
493 for (i
= vreg
->set_points
->count
- 1; i
> 0; i
--) {
494 range_max_uV
= vreg
->set_points
->range
[i
- 1].set_point_max_uV
;
495 if (uV
> range_max_uV
&& range_max_uV
> 0)
500 range
= &vreg
->set_points
->range
[range_id
];
501 *range_sel
= range
->range_sel
;
504 * Force uV to be an allowed set point by applying a ceiling function to
507 *voltage_sel
= (uV
- range
->min_uV
+ range
->step_uV
- 1)
509 uV
= *voltage_sel
* range
->step_uV
+ range
->min_uV
;
513 "request v=[%d, %d] cannot be met by any set point; "
514 "next set point: %d\n",
520 for (i
= 0; i
< range_id
; i
++)
521 *selector
+= vreg
->set_points
->range
[i
].n_voltages
;
522 *selector
+= (uV
- range
->set_point_min_uV
) / range
->step_uV
;
527 static const struct spmi_voltage_range
*
528 spmi_regulator_find_range(struct spmi_regulator
*vreg
)
531 const struct spmi_voltage_range
*range
, *end
;
533 range
= vreg
->set_points
->range
;
534 end
= range
+ vreg
->set_points
->count
;
536 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, &range_sel
, 1);
538 for (; range
< end
; range
++)
539 if (range
->range_sel
== range_sel
)
545 static int spmi_regulator_select_voltage_same_range(struct spmi_regulator
*vreg
,
546 int min_uV
, int max_uV
, u8
*range_sel
, u8
*voltage_sel
,
549 const struct spmi_voltage_range
*range
;
553 range
= spmi_regulator_find_range(vreg
);
555 goto different_range
;
557 if (uV
< range
->min_uV
&& max_uV
>= range
->min_uV
)
560 if (uV
< range
->min_uV
|| uV
> range
->max_uV
) {
561 /* Current range doesn't support the requested voltage. */
562 goto different_range
;
566 * Force uV to be an allowed set point by applying a ceiling function to
569 *voltage_sel
= DIV_ROUND_UP(uV
- range
->min_uV
, range
->step_uV
);
570 uV
= *voltage_sel
* range
->step_uV
+ range
->min_uV
;
574 * No set point in the current voltage range is within the
575 * requested min_uV to max_uV range.
577 goto different_range
;
581 for (i
= 0; i
< vreg
->set_points
->count
; i
++) {
582 if (uV
>= vreg
->set_points
->range
[i
].set_point_min_uV
583 && uV
<= vreg
->set_points
->range
[i
].set_point_max_uV
) {
585 (uV
- vreg
->set_points
->range
[i
].set_point_min_uV
)
586 / vreg
->set_points
->range
[i
].step_uV
;
590 *selector
+= vreg
->set_points
->range
[i
].n_voltages
;
593 if (*selector
>= vreg
->set_points
->n_voltages
)
594 goto different_range
;
599 return spmi_regulator_select_voltage(vreg
, min_uV
, max_uV
,
600 range_sel
, voltage_sel
, selector
);
603 static int spmi_regulator_common_set_voltage(struct regulator_dev
*rdev
,
604 int min_uV
, int max_uV
, unsigned *selector
)
606 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
609 u8 range_sel
, voltage_sel
;
612 * Favor staying in the current voltage range if possible. This avoids
613 * voltage spikes that occur when changing the voltage range.
615 ret
= spmi_regulator_select_voltage_same_range(vreg
, min_uV
, max_uV
,
616 &range_sel
, &voltage_sel
, selector
);
621 buf
[1] = voltage_sel
;
622 return spmi_vreg_write(vreg
, SPMI_COMMON_REG_VOLTAGE_RANGE
, buf
, 2);
625 static int spmi_regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
626 unsigned int old_selector
, unsigned int new_selector
)
628 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
629 const struct spmi_voltage_range
*range
;
632 range
= spmi_regulator_find_range(vreg
);
636 diff_uV
= abs(new_selector
- old_selector
) * range
->step_uV
;
638 return DIV_ROUND_UP(diff_uV
, vreg
->slew_rate
);
641 static int spmi_regulator_common_get_voltage(struct regulator_dev
*rdev
)
643 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
644 const struct spmi_voltage_range
*range
;
647 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &voltage_sel
, 1);
649 range
= spmi_regulator_find_range(vreg
);
651 return VOLTAGE_UNKNOWN
;
653 return range
->step_uV
* voltage_sel
+ range
->min_uV
;
656 static int spmi_regulator_single_range_set_voltage(struct regulator_dev
*rdev
,
657 int min_uV
, int max_uV
, unsigned *selector
)
659 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
663 ret
= spmi_regulator_select_voltage(vreg
, min_uV
, max_uV
, &range_sel
,
666 dev_err(vreg
->dev
, "could not set voltage, ret=%d\n", ret
);
671 * Certain types of regulators do not have a range select register so
672 * only voltage set register needs to be written.
674 return spmi_vreg_write(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &sel
, 1);
677 static int spmi_regulator_single_range_get_voltage(struct regulator_dev
*rdev
)
679 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
680 const struct spmi_voltage_range
*range
= vreg
->set_points
->range
;
683 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &voltage_sel
, 1);
685 return range
->step_uV
* voltage_sel
+ range
->min_uV
;
688 static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev
*rdev
,
689 int min_uV
, int max_uV
, unsigned *selector
)
691 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
693 u8 range_sel
, voltage_sel
;
696 * Favor staying in the current voltage range if possible. This avoids
697 * voltage spikes that occur when changing the voltage range.
699 ret
= spmi_regulator_select_voltage_same_range(vreg
, min_uV
, max_uV
,
700 &range_sel
, &voltage_sel
, selector
);
705 * Calculate VSET based on range
706 * In case of range 0: voltage_sel is a 7 bit value, can be written
707 * witout any modification.
708 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
712 voltage_sel
|= ULT_SMPS_RANGE_SPLIT
;
714 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
,
718 static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev
*rdev
)
720 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
721 const struct spmi_voltage_range
*range
;
724 spmi_vreg_read(vreg
, SPMI_COMMON_REG_VOLTAGE_SET
, &voltage_sel
, 1);
726 range
= spmi_regulator_find_range(vreg
);
728 return VOLTAGE_UNKNOWN
;
730 if (range
->range_sel
== 1)
731 voltage_sel
&= ~ULT_SMPS_RANGE_SPLIT
;
733 return range
->step_uV
* voltage_sel
+ range
->min_uV
;
736 static int spmi_regulator_common_list_voltage(struct regulator_dev
*rdev
,
739 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
743 if (selector
>= vreg
->set_points
->n_voltages
)
746 for (i
= 0; i
< vreg
->set_points
->count
; i
++) {
747 if (selector
< vreg
->set_points
->range
[i
].n_voltages
) {
748 uV
= selector
* vreg
->set_points
->range
[i
].step_uV
749 + vreg
->set_points
->range
[i
].set_point_min_uV
;
753 selector
-= vreg
->set_points
->range
[i
].n_voltages
;
760 spmi_regulator_common_set_bypass(struct regulator_dev
*rdev
, bool enable
)
762 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
763 u8 mask
= SPMI_COMMON_MODE_BYPASS_MASK
;
769 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_MODE
, val
, mask
);
773 spmi_regulator_common_get_bypass(struct regulator_dev
*rdev
, bool *enable
)
775 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
779 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_MODE
, &val
, 1);
780 *enable
= val
& SPMI_COMMON_MODE_BYPASS_MASK
;
785 static unsigned int spmi_regulator_common_get_mode(struct regulator_dev
*rdev
)
787 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
790 spmi_vreg_read(vreg
, SPMI_COMMON_REG_MODE
, ®
, 1);
792 if (reg
& SPMI_COMMON_MODE_HPM_MASK
)
793 return REGULATOR_MODE_NORMAL
;
795 return REGULATOR_MODE_IDLE
;
799 spmi_regulator_common_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
801 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
802 u8 mask
= SPMI_COMMON_MODE_HPM_MASK
;
805 if (mode
== REGULATOR_MODE_NORMAL
)
808 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_MODE
, val
, mask
);
812 spmi_regulator_common_set_load(struct regulator_dev
*rdev
, int load_uA
)
814 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
817 if (load_uA
>= vreg
->hpm_min_load
)
818 mode
= REGULATOR_MODE_NORMAL
;
820 mode
= REGULATOR_MODE_IDLE
;
822 return spmi_regulator_common_set_mode(rdev
, mode
);
825 static int spmi_regulator_common_set_pull_down(struct regulator_dev
*rdev
)
827 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
828 unsigned int mask
= SPMI_COMMON_PULL_DOWN_ENABLE_MASK
;
830 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_PULL_DOWN
,
834 static int spmi_regulator_common_set_soft_start(struct regulator_dev
*rdev
)
836 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
837 unsigned int mask
= SPMI_LDO_SOFT_START_ENABLE_MASK
;
839 return spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_SOFT_START
,
843 static int spmi_regulator_set_ilim(struct regulator_dev
*rdev
, int ilim_uA
)
845 struct spmi_regulator
*vreg
= rdev_get_drvdata(rdev
);
846 enum spmi_regulator_logical_type type
= vreg
->logical_type
;
847 unsigned int current_reg
;
849 u8 mask
= SPMI_BOOST_CURRENT_LIMIT_MASK
|
850 SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK
;
851 int max
= (SPMI_BOOST_CURRENT_LIMIT_MASK
+ 1) * 500;
853 if (type
== SPMI_REGULATOR_LOGICAL_TYPE_BOOST
)
854 current_reg
= SPMI_BOOST_REG_CURRENT_LIMIT
;
856 current_reg
= SPMI_BOOST_BYP_REG_CURRENT_LIMIT
;
858 if (ilim_uA
> max
|| ilim_uA
<= 0)
861 reg
= (ilim_uA
- 1) / 500;
862 reg
|= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK
;
864 return spmi_vreg_update_bits(vreg
, current_reg
, reg
, mask
);
867 static int spmi_regulator_vs_clear_ocp(struct spmi_regulator
*vreg
)
871 ret
= spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
872 SPMI_COMMON_DISABLE
, SPMI_COMMON_ENABLE_MASK
);
874 vreg
->vs_enable_time
= ktime_get();
876 ret
= spmi_vreg_update_bits(vreg
, SPMI_COMMON_REG_ENABLE
,
877 SPMI_COMMON_ENABLE
, SPMI_COMMON_ENABLE_MASK
);
882 static void spmi_regulator_vs_ocp_work(struct work_struct
*work
)
884 struct delayed_work
*dwork
= to_delayed_work(work
);
885 struct spmi_regulator
*vreg
886 = container_of(dwork
, struct spmi_regulator
, ocp_work
);
888 spmi_regulator_vs_clear_ocp(vreg
);
891 static irqreturn_t
spmi_regulator_vs_ocp_isr(int irq
, void *data
)
893 struct spmi_regulator
*vreg
= data
;
894 ktime_t ocp_irq_time
;
895 s64 ocp_trigger_delay_us
;
897 ocp_irq_time
= ktime_get();
898 ocp_trigger_delay_us
= ktime_us_delta(ocp_irq_time
,
899 vreg
->vs_enable_time
);
902 * Reset the OCP count if there is a large delay between switch enable
903 * and when OCP triggers. This is indicative of a hotplug event as
904 * opposed to a fault.
906 if (ocp_trigger_delay_us
> SPMI_VS_OCP_FAULT_DELAY_US
)
909 /* Wait for switch output to settle back to 0 V after OCP triggered. */
910 udelay(SPMI_VS_OCP_FALL_DELAY_US
);
914 if (vreg
->ocp_count
== 1) {
915 /* Immediately clear the over current condition. */
916 spmi_regulator_vs_clear_ocp(vreg
);
917 } else if (vreg
->ocp_count
<= vreg
->ocp_max_retries
) {
918 /* Schedule the over current clear task to run later. */
919 schedule_delayed_work(&vreg
->ocp_work
,
920 msecs_to_jiffies(vreg
->ocp_retry_delay_ms
) + 1);
923 "OCP triggered %d times; no further retries\n",
930 static struct regulator_ops spmi_smps_ops
= {
931 .enable
= spmi_regulator_common_enable
,
932 .disable
= spmi_regulator_common_disable
,
933 .is_enabled
= spmi_regulator_common_is_enabled
,
934 .set_voltage
= spmi_regulator_common_set_voltage
,
935 .get_voltage
= spmi_regulator_common_get_voltage
,
936 .list_voltage
= spmi_regulator_common_list_voltage
,
937 .set_mode
= spmi_regulator_common_set_mode
,
938 .get_mode
= spmi_regulator_common_get_mode
,
939 .set_load
= spmi_regulator_common_set_load
,
940 .set_pull_down
= spmi_regulator_common_set_pull_down
,
943 static struct regulator_ops spmi_ldo_ops
= {
944 .enable
= spmi_regulator_common_enable
,
945 .disable
= spmi_regulator_common_disable
,
946 .is_enabled
= spmi_regulator_common_is_enabled
,
947 .set_voltage
= spmi_regulator_common_set_voltage
,
948 .get_voltage
= spmi_regulator_common_get_voltage
,
949 .list_voltage
= spmi_regulator_common_list_voltage
,
950 .set_mode
= spmi_regulator_common_set_mode
,
951 .get_mode
= spmi_regulator_common_get_mode
,
952 .set_load
= spmi_regulator_common_set_load
,
953 .set_bypass
= spmi_regulator_common_set_bypass
,
954 .get_bypass
= spmi_regulator_common_get_bypass
,
955 .set_pull_down
= spmi_regulator_common_set_pull_down
,
956 .set_soft_start
= spmi_regulator_common_set_soft_start
,
959 static struct regulator_ops spmi_ln_ldo_ops
= {
960 .enable
= spmi_regulator_common_enable
,
961 .disable
= spmi_regulator_common_disable
,
962 .is_enabled
= spmi_regulator_common_is_enabled
,
963 .set_voltage
= spmi_regulator_common_set_voltage
,
964 .get_voltage
= spmi_regulator_common_get_voltage
,
965 .list_voltage
= spmi_regulator_common_list_voltage
,
966 .set_bypass
= spmi_regulator_common_set_bypass
,
967 .get_bypass
= spmi_regulator_common_get_bypass
,
970 static struct regulator_ops spmi_vs_ops
= {
971 .enable
= spmi_regulator_vs_enable
,
972 .disable
= spmi_regulator_common_disable
,
973 .is_enabled
= spmi_regulator_common_is_enabled
,
974 .set_pull_down
= spmi_regulator_common_set_pull_down
,
975 .set_soft_start
= spmi_regulator_common_set_soft_start
,
978 static struct regulator_ops spmi_boost_ops
= {
979 .enable
= spmi_regulator_common_enable
,
980 .disable
= spmi_regulator_common_disable
,
981 .is_enabled
= spmi_regulator_common_is_enabled
,
982 .set_voltage
= spmi_regulator_single_range_set_voltage
,
983 .get_voltage
= spmi_regulator_single_range_get_voltage
,
984 .list_voltage
= spmi_regulator_common_list_voltage
,
985 .set_input_current_limit
= spmi_regulator_set_ilim
,
988 static struct regulator_ops spmi_ftsmps_ops
= {
989 .enable
= spmi_regulator_common_enable
,
990 .disable
= spmi_regulator_common_disable
,
991 .is_enabled
= spmi_regulator_common_is_enabled
,
992 .set_voltage
= spmi_regulator_common_set_voltage
,
993 .set_voltage_time_sel
= spmi_regulator_set_voltage_time_sel
,
994 .get_voltage
= spmi_regulator_common_get_voltage
,
995 .list_voltage
= spmi_regulator_common_list_voltage
,
996 .set_mode
= spmi_regulator_common_set_mode
,
997 .get_mode
= spmi_regulator_common_get_mode
,
998 .set_load
= spmi_regulator_common_set_load
,
999 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1002 static struct regulator_ops spmi_ult_lo_smps_ops
= {
1003 .enable
= spmi_regulator_common_enable
,
1004 .disable
= spmi_regulator_common_disable
,
1005 .is_enabled
= spmi_regulator_common_is_enabled
,
1006 .set_voltage
= spmi_regulator_ult_lo_smps_set_voltage
,
1007 .get_voltage
= spmi_regulator_ult_lo_smps_get_voltage
,
1008 .list_voltage
= spmi_regulator_common_list_voltage
,
1009 .set_mode
= spmi_regulator_common_set_mode
,
1010 .get_mode
= spmi_regulator_common_get_mode
,
1011 .set_load
= spmi_regulator_common_set_load
,
1012 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1015 static struct regulator_ops spmi_ult_ho_smps_ops
= {
1016 .enable
= spmi_regulator_common_enable
,
1017 .disable
= spmi_regulator_common_disable
,
1018 .is_enabled
= spmi_regulator_common_is_enabled
,
1019 .set_voltage
= spmi_regulator_single_range_set_voltage
,
1020 .get_voltage
= spmi_regulator_single_range_get_voltage
,
1021 .list_voltage
= spmi_regulator_common_list_voltage
,
1022 .set_mode
= spmi_regulator_common_set_mode
,
1023 .get_mode
= spmi_regulator_common_get_mode
,
1024 .set_load
= spmi_regulator_common_set_load
,
1025 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1028 static struct regulator_ops spmi_ult_ldo_ops
= {
1029 .enable
= spmi_regulator_common_enable
,
1030 .disable
= spmi_regulator_common_disable
,
1031 .is_enabled
= spmi_regulator_common_is_enabled
,
1032 .set_voltage
= spmi_regulator_single_range_set_voltage
,
1033 .get_voltage
= spmi_regulator_single_range_get_voltage
,
1034 .list_voltage
= spmi_regulator_common_list_voltage
,
1035 .set_mode
= spmi_regulator_common_set_mode
,
1036 .get_mode
= spmi_regulator_common_get_mode
,
1037 .set_load
= spmi_regulator_common_set_load
,
1038 .set_bypass
= spmi_regulator_common_set_bypass
,
1039 .get_bypass
= spmi_regulator_common_get_bypass
,
1040 .set_pull_down
= spmi_regulator_common_set_pull_down
,
1041 .set_soft_start
= spmi_regulator_common_set_soft_start
,
1044 /* Maximum possible digital major revision value */
1047 static const struct spmi_regulator_mapping supported_regulators
[] = {
1048 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
1049 SPMI_VREG(BUCK
, GP_CTL
, 0, INF
, SMPS
, smps
, smps
, 100000),
1050 SPMI_VREG(LDO
, N300
, 0, INF
, LDO
, ldo
, nldo1
, 10000),
1051 SPMI_VREG(LDO
, N600
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1052 SPMI_VREG(LDO
, N1200
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1053 SPMI_VREG(LDO
, N600
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1054 SPMI_VREG(LDO
, N1200
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1055 SPMI_VREG(LDO
, N600_ST
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1056 SPMI_VREG(LDO
, N1200_ST
, 0, 0, LDO
, ldo
, nldo2
, 10000),
1057 SPMI_VREG(LDO
, N600_ST
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1058 SPMI_VREG(LDO
, N1200_ST
, 1, INF
, LDO
, ldo
, nldo3
, 10000),
1059 SPMI_VREG(LDO
, P50
, 0, INF
, LDO
, ldo
, pldo
, 5000),
1060 SPMI_VREG(LDO
, P150
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1061 SPMI_VREG(LDO
, P300
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1062 SPMI_VREG(LDO
, P600
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1063 SPMI_VREG(LDO
, P1200
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1064 SPMI_VREG(LDO
, LN
, 0, INF
, LN_LDO
, ln_ldo
, ln_ldo
, 0),
1065 SPMI_VREG(LDO
, LV_P50
, 0, INF
, LDO
, ldo
, pldo
, 5000),
1066 SPMI_VREG(LDO
, LV_P150
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1067 SPMI_VREG(LDO
, LV_P300
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1068 SPMI_VREG(LDO
, LV_P600
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1069 SPMI_VREG(LDO
, LV_P1200
, 0, INF
, LDO
, ldo
, pldo
, 10000),
1070 SPMI_VREG_VS(LV100
, 0, INF
),
1071 SPMI_VREG_VS(LV300
, 0, INF
),
1072 SPMI_VREG_VS(MV300
, 0, INF
),
1073 SPMI_VREG_VS(MV500
, 0, INF
),
1074 SPMI_VREG_VS(HDMI
, 0, INF
),
1075 SPMI_VREG_VS(OTG
, 0, INF
),
1076 SPMI_VREG(BOOST
, 5V_BOOST
, 0, INF
, BOOST
, boost
, boost
, 0),
1077 SPMI_VREG(FTS
, FTS_CTL
, 0, INF
, FTSMPS
, ftsmps
, ftsmps
, 100000),
1078 SPMI_VREG(FTS
, FTS2p5_CTL
, 0, INF
, FTSMPS
, ftsmps
, ftsmps2p5
, 100000),
1079 SPMI_VREG(BOOST_BYP
, BB_2A
, 0, INF
, BOOST_BYP
, boost
, boost_byp
, 0),
1080 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL1
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1081 ult_lo_smps
, 100000),
1082 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL2
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1083 ult_lo_smps
, 100000),
1084 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL3
, 0, INF
, ULT_LO_SMPS
, ult_lo_smps
,
1085 ult_lo_smps
, 100000),
1086 SPMI_VREG(ULT_BUCK
, ULT_HF_CTL4
, 0, INF
, ULT_HO_SMPS
, ult_ho_smps
,
1087 ult_ho_smps
, 100000),
1088 SPMI_VREG(ULT_LDO
, N300_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1089 SPMI_VREG(ULT_LDO
, N600_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1090 SPMI_VREG(ULT_LDO
, N900_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1091 SPMI_VREG(ULT_LDO
, N1200_ST
, 0, INF
, ULT_LDO
, ult_ldo
, ult_nldo
, 10000),
1092 SPMI_VREG(ULT_LDO
, LV_P150
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1093 SPMI_VREG(ULT_LDO
, LV_P300
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1094 SPMI_VREG(ULT_LDO
, LV_P450
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1095 SPMI_VREG(ULT_LDO
, P600
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1096 SPMI_VREG(ULT_LDO
, P150
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 10000),
1097 SPMI_VREG(ULT_LDO
, P50
, 0, INF
, ULT_LDO
, ult_ldo
, ult_pldo
, 5000),
1100 static void spmi_calculate_num_voltages(struct spmi_voltage_set_points
*points
)
1103 struct spmi_voltage_range
*range
= points
->range
;
1105 for (; range
< points
->range
+ points
->count
; range
++) {
1107 if (range
->set_point_max_uV
) {
1108 n
= range
->set_point_max_uV
- range
->set_point_min_uV
;
1109 n
= (n
/ range
->step_uV
) + 1;
1111 range
->n_voltages
= n
;
1112 points
->n_voltages
+= n
;
1116 static int spmi_regulator_match(struct spmi_regulator
*vreg
, u16 force_type
)
1118 const struct spmi_regulator_mapping
*mapping
;
1121 u8 version
[SPMI_COMMON_REG_SUBTYPE
- SPMI_COMMON_REG_DIG_MAJOR_REV
+ 1];
1124 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_DIG_MAJOR_REV
, version
,
1125 ARRAY_SIZE(version
));
1127 dev_err(vreg
->dev
, "could not read version registers\n");
1130 dig_major_rev
= version
[SPMI_COMMON_REG_DIG_MAJOR_REV
1131 - SPMI_COMMON_REG_DIG_MAJOR_REV
];
1133 type
= version
[SPMI_COMMON_REG_TYPE
-
1134 SPMI_COMMON_REG_DIG_MAJOR_REV
];
1135 subtype
= version
[SPMI_COMMON_REG_SUBTYPE
-
1136 SPMI_COMMON_REG_DIG_MAJOR_REV
];
1138 type
= force_type
>> 8;
1139 subtype
= force_type
;
1142 for (i
= 0; i
< ARRAY_SIZE(supported_regulators
); i
++) {
1143 mapping
= &supported_regulators
[i
];
1144 if (mapping
->type
== type
&& mapping
->subtype
== subtype
1145 && mapping
->revision_min
<= dig_major_rev
1146 && mapping
->revision_max
>= dig_major_rev
)
1151 "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1152 vreg
->desc
.name
, type
, subtype
, dig_major_rev
);
1157 vreg
->logical_type
= mapping
->logical_type
;
1158 vreg
->set_points
= mapping
->set_points
;
1159 vreg
->hpm_min_load
= mapping
->hpm_min_load
;
1160 vreg
->desc
.ops
= mapping
->ops
;
1162 if (mapping
->set_points
) {
1163 if (!mapping
->set_points
->n_voltages
)
1164 spmi_calculate_num_voltages(mapping
->set_points
);
1165 vreg
->desc
.n_voltages
= mapping
->set_points
->n_voltages
;
1171 static int spmi_regulator_ftsmps_init_slew_rate(struct spmi_regulator
*vreg
)
1175 int step
, delay
, slew_rate
;
1176 const struct spmi_voltage_range
*range
;
1178 ret
= spmi_vreg_read(vreg
, SPMI_COMMON_REG_STEP_CTRL
, ®
, 1);
1180 dev_err(vreg
->dev
, "spmi read failed, ret=%d\n", ret
);
1184 range
= spmi_regulator_find_range(vreg
);
1188 step
= reg
& SPMI_FTSMPS_STEP_CTRL_STEP_MASK
;
1189 step
>>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT
;
1191 delay
= reg
& SPMI_FTSMPS_STEP_CTRL_DELAY_MASK
;
1192 delay
>>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT
;
1194 /* slew_rate has units of uV/us */
1195 slew_rate
= SPMI_FTSMPS_CLOCK_RATE
* range
->step_uV
* (1 << step
);
1196 slew_rate
/= 1000 * (SPMI_FTSMPS_STEP_DELAY
<< delay
);
1197 slew_rate
*= SPMI_FTSMPS_STEP_MARGIN_NUM
;
1198 slew_rate
/= SPMI_FTSMPS_STEP_MARGIN_DEN
;
1200 /* Ensure that the slew rate is greater than 0 */
1201 vreg
->slew_rate
= max(slew_rate
, 1);
1206 static unsigned int spmi_regulator_of_map_mode(unsigned int mode
)
1209 return REGULATOR_MODE_NORMAL
;
1211 return REGULATOR_MODE_IDLE
;
1214 static int spmi_regulator_of_parse(struct device_node
*node
,
1215 const struct regulator_desc
*desc
,
1216 struct regulator_config
*config
)
1218 struct spmi_regulator
*vreg
= config
->driver_data
;
1219 struct device
*dev
= config
->dev
;
1222 vreg
->ocp_max_retries
= SPMI_VS_OCP_DEFAULT_MAX_RETRIES
;
1223 vreg
->ocp_retry_delay_ms
= SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS
;
1225 if (vreg
->logical_type
== SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS
) {
1226 ret
= spmi_regulator_ftsmps_init_slew_rate(vreg
);
1231 if (vreg
->logical_type
!= SPMI_REGULATOR_LOGICAL_TYPE_VS
)
1234 if (vreg
->ocp_irq
) {
1235 ret
= devm_request_irq(dev
, vreg
->ocp_irq
,
1236 spmi_regulator_vs_ocp_isr
, IRQF_TRIGGER_RISING
, "ocp",
1239 dev_err(dev
, "failed to request irq %d, ret=%d\n",
1240 vreg
->ocp_irq
, ret
);
1244 INIT_DELAYED_WORK(&vreg
->ocp_work
, spmi_regulator_vs_ocp_work
);
1250 static const struct spmi_regulator_data pm8941_regulators
[] = {
1251 { "s1", 0x1400, "vdd_s1", },
1252 { "s2", 0x1700, "vdd_s2", },
1253 { "s3", 0x1a00, "vdd_s3", },
1254 { "l1", 0x4000, "vdd_l1_l3", },
1255 { "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1256 { "l3", 0x4200, "vdd_l1_l3", },
1257 { "l4", 0x4300, "vdd_l4_l11", },
1258 { "l5", 0x4400, "vdd_l5_l7", NULL
, 0x0410 },
1259 { "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1260 { "l7", 0x4600, "vdd_l5_l7", NULL
, 0x0410 },
1261 { "l8", 0x4700, "vdd_l8_l16_l18_19", },
1262 { "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1263 { "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1264 { "l11", 0x4a00, "vdd_l4_l11", },
1265 { "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1266 { "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1267 { "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1268 { "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1269 { "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1270 { "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1271 { "l18", 0x5100, "vdd_l8_l16_l18_19", },
1272 { "l19", 0x5200, "vdd_l8_l16_l18_19", },
1273 { "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1274 { "l21", 0x5400, "vdd_l21", },
1275 { "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1276 { "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1277 { "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1278 { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1279 { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1280 { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
1281 { "mvs1", 0x8300, "vin_5vs", },
1282 { "mvs2", 0x8400, "vin_5vs", },
1286 static const struct spmi_regulator_data pm8841_regulators
[] = {
1287 { "s1", 0x1400, "vdd_s1", },
1288 { "s2", 0x1700, "vdd_s2", NULL
, 0x1c08 },
1289 { "s3", 0x1a00, "vdd_s3", },
1290 { "s4", 0x1d00, "vdd_s4", NULL
, 0x1c08 },
1291 { "s5", 0x2000, "vdd_s5", NULL
, 0x1c08 },
1292 { "s6", 0x2300, "vdd_s6", NULL
, 0x1c08 },
1293 { "s7", 0x2600, "vdd_s7", NULL
, 0x1c08 },
1294 { "s8", 0x2900, "vdd_s8", NULL
, 0x1c08 },
1298 static const struct spmi_regulator_data pm8916_regulators
[] = {
1299 { "s1", 0x1400, "vdd_s1", },
1300 { "s2", 0x1700, "vdd_s2", },
1301 { "s3", 0x1a00, "vdd_s3", },
1302 { "s4", 0x1d00, "vdd_s4", },
1303 { "l1", 0x4000, "vdd_l1_l3", },
1304 { "l2", 0x4100, "vdd_l2", },
1305 { "l3", 0x4200, "vdd_l1_l3", },
1306 { "l4", 0x4300, "vdd_l4_l5_l6", },
1307 { "l5", 0x4400, "vdd_l4_l5_l6", },
1308 { "l6", 0x4500, "vdd_l4_l5_l6", },
1309 { "l7", 0x4600, "vdd_l7", },
1310 { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1311 { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1312 { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1313 { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1314 { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1315 { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1316 { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1317 { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1318 { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1319 { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1320 { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1324 static const struct of_device_id qcom_spmi_regulator_match
[] = {
1325 { .compatible
= "qcom,pm8841-regulators", .data
= &pm8841_regulators
},
1326 { .compatible
= "qcom,pm8916-regulators", .data
= &pm8916_regulators
},
1327 { .compatible
= "qcom,pm8941-regulators", .data
= &pm8941_regulators
},
1330 MODULE_DEVICE_TABLE(of
, qcom_spmi_regulator_match
);
1332 static int qcom_spmi_regulator_probe(struct platform_device
*pdev
)
1334 const struct spmi_regulator_data
*reg
;
1335 const struct of_device_id
*match
;
1336 struct regulator_config config
= { };
1337 struct regulator_dev
*rdev
;
1338 struct spmi_regulator
*vreg
;
1339 struct regmap
*regmap
;
1341 struct device
*dev
= &pdev
->dev
;
1343 struct list_head
*vreg_list
;
1345 vreg_list
= devm_kzalloc(dev
, sizeof(*vreg_list
), GFP_KERNEL
);
1348 INIT_LIST_HEAD(vreg_list
);
1349 platform_set_drvdata(pdev
, vreg_list
);
1351 regmap
= dev_get_regmap(dev
->parent
, NULL
);
1355 match
= of_match_device(qcom_spmi_regulator_match
, &pdev
->dev
);
1359 for (reg
= match
->data
; reg
->name
; reg
++) {
1360 vreg
= devm_kzalloc(dev
, sizeof(*vreg
), GFP_KERNEL
);
1365 vreg
->base
= reg
->base
;
1366 vreg
->regmap
= regmap
;
1369 vreg
->ocp_irq
= platform_get_irq_byname(pdev
, reg
->ocp
);
1370 if (vreg
->ocp_irq
< 0) {
1371 ret
= vreg
->ocp_irq
;
1377 vreg
->desc
.owner
= THIS_MODULE
;
1378 vreg
->desc
.type
= REGULATOR_VOLTAGE
;
1379 vreg
->desc
.name
= name
= reg
->name
;
1380 vreg
->desc
.supply_name
= reg
->supply
;
1381 vreg
->desc
.of_match
= reg
->name
;
1382 vreg
->desc
.of_parse_cb
= spmi_regulator_of_parse
;
1383 vreg
->desc
.of_map_mode
= spmi_regulator_of_map_mode
;
1385 ret
= spmi_regulator_match(vreg
, reg
->force_type
);
1390 config
.driver_data
= vreg
;
1391 rdev
= devm_regulator_register(dev
, &vreg
->desc
, &config
);
1393 dev_err(dev
, "failed to register %s\n", name
);
1394 ret
= PTR_ERR(rdev
);
1398 INIT_LIST_HEAD(&vreg
->node
);
1399 list_add(&vreg
->node
, vreg_list
);
1405 list_for_each_entry(vreg
, vreg_list
, node
)
1407 cancel_delayed_work_sync(&vreg
->ocp_work
);
1411 static int qcom_spmi_regulator_remove(struct platform_device
*pdev
)
1413 struct spmi_regulator
*vreg
;
1414 struct list_head
*vreg_list
= platform_get_drvdata(pdev
);
1416 list_for_each_entry(vreg
, vreg_list
, node
)
1418 cancel_delayed_work_sync(&vreg
->ocp_work
);
1423 static struct platform_driver qcom_spmi_regulator_driver
= {
1425 .name
= "qcom-spmi-regulator",
1426 .of_match_table
= qcom_spmi_regulator_match
,
1428 .probe
= qcom_spmi_regulator_probe
,
1429 .remove
= qcom_spmi_regulator_remove
,
1431 module_platform_driver(qcom_spmi_regulator_driver
);
1433 MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
1434 MODULE_LICENSE("GPL v2");
1435 MODULE_ALIAS("platform:qcom-spmi-regulator");