xref: /linux/drivers/regulator/qcom_spmi-regulator.c (revision 97fb5e8d9b57f10f294303c9a5d1bd033eded6bf)
1*97fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e92a4047SStephen Boyd /*
3e92a4047SStephen Boyd  * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
4e92a4047SStephen Boyd  */
5e92a4047SStephen Boyd 
6e92a4047SStephen Boyd #include <linux/module.h>
7e92a4047SStephen Boyd #include <linux/delay.h>
8e92a4047SStephen Boyd #include <linux/err.h>
9e92a4047SStephen Boyd #include <linux/kernel.h>
10e92a4047SStephen Boyd #include <linux/interrupt.h>
11e92a4047SStephen Boyd #include <linux/bitops.h>
12e92a4047SStephen Boyd #include <linux/slab.h>
13e92a4047SStephen Boyd #include <linux/of.h>
14e92a4047SStephen Boyd #include <linux/of_device.h>
15e92a4047SStephen Boyd #include <linux/platform_device.h>
16e92a4047SStephen Boyd #include <linux/ktime.h>
17e92a4047SStephen Boyd #include <linux/regulator/driver.h>
18e92a4047SStephen Boyd #include <linux/regmap.h>
19e92a4047SStephen Boyd #include <linux/list.h>
200caecaa8SIlia Lin #include <linux/mfd/syscon.h>
210caecaa8SIlia Lin #include <linux/io.h>
22e92a4047SStephen Boyd 
23e2adfacdSStephen Boyd /* Pin control enable input pins. */
24e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE		0x00
25e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0		0x01
26e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1		0x02
27e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2		0x04
28e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3		0x08
29e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT	0x10
30e2adfacdSStephen Boyd 
31e2adfacdSStephen Boyd /* Pin control high power mode input pins. */
32e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE		0x00
33e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0			0x01
34e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1			0x02
35e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2			0x04
36e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3			0x08
37e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B		0x10
38e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT		0x20
39e2adfacdSStephen Boyd 
40e2adfacdSStephen Boyd /*
41e2adfacdSStephen Boyd  * Used with enable parameters to specify that hardware default register values
42e2adfacdSStephen Boyd  * should be left unaltered.
43e2adfacdSStephen Boyd  */
44e2adfacdSStephen Boyd #define SPMI_REGULATOR_USE_HW_DEFAULT			2
45e2adfacdSStephen Boyd 
46e2adfacdSStephen Boyd /* Soft start strength of a voltage switch type regulator */
47e2adfacdSStephen Boyd enum spmi_vs_soft_start_str {
48e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P05_UA = 0,
49e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P25_UA,
50e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P55_UA,
51e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P75_UA,
52e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_HW_DEFAULT,
53e2adfacdSStephen Boyd };
54e2adfacdSStephen Boyd 
55e2adfacdSStephen Boyd /**
56e2adfacdSStephen Boyd  * struct spmi_regulator_init_data - spmi-regulator initialization data
57e2adfacdSStephen Boyd  * @pin_ctrl_enable:        Bit mask specifying which hardware pins should be
58e2adfacdSStephen Boyd  *				used to enable the regulator, if any
59e2adfacdSStephen Boyd  *			    Value should be an ORing of
60e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants.  If
61e2adfacdSStephen Boyd  *				the bit specified by
62e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
63e2adfacdSStephen Boyd  *				set, then pin control enable hardware registers
64e2adfacdSStephen Boyd  *				will not be modified.
65e2adfacdSStephen Boyd  * @pin_ctrl_hpm:           Bit mask specifying which hardware pins should be
66e2adfacdSStephen Boyd  *				used to force the regulator into high power
67e2adfacdSStephen Boyd  *				mode, if any
68e2adfacdSStephen Boyd  *			    Value should be an ORing of
69e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_* constants.  If
70e2adfacdSStephen Boyd  *				the bit specified by
71e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
72e2adfacdSStephen Boyd  *				set, then pin control mode hardware registers
73e2adfacdSStephen Boyd  *				will not be modified.
74e2adfacdSStephen Boyd  * @vs_soft_start_strength: This parameter sets the soft start strength for
75e2adfacdSStephen Boyd  *				voltage switch type regulators.  Its value
76e2adfacdSStephen Boyd  *				should be one of SPMI_VS_SOFT_START_STR_*.  If
77e2adfacdSStephen Boyd  *				its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
78e2adfacdSStephen Boyd  *				then the soft start strength will be left at its
79e2adfacdSStephen Boyd  *				default hardware value.
80e2adfacdSStephen Boyd  */
81e2adfacdSStephen Boyd struct spmi_regulator_init_data {
82e2adfacdSStephen Boyd 	unsigned				pin_ctrl_enable;
83e2adfacdSStephen Boyd 	unsigned				pin_ctrl_hpm;
84e2adfacdSStephen Boyd 	enum spmi_vs_soft_start_str		vs_soft_start_strength;
85e2adfacdSStephen Boyd };
86e2adfacdSStephen Boyd 
87e92a4047SStephen Boyd /* These types correspond to unique register layouts. */
88e92a4047SStephen Boyd enum spmi_regulator_logical_type {
89e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_SMPS,
90e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LDO,
91e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_VS,
92e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST,
93e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS,
94e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
95e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO,
96e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
97e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
98e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO,
99e92a4047SStephen Boyd };
100e92a4047SStephen Boyd 
101e92a4047SStephen Boyd enum spmi_regulator_type {
102e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BUCK		= 0x03,
103e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_LDO			= 0x04,
104e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_VS			= 0x05,
105e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST		= 0x1b,
106e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_FTS			= 0x1c,
107e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST_BYP		= 0x1f,
108e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_LDO		= 0x21,
109e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_BUCK		= 0x22,
110e92a4047SStephen Boyd };
111e92a4047SStephen Boyd 
112e92a4047SStephen Boyd enum spmi_regulator_subtype {
113e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_GP_CTL		= 0x08,
114e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_RF_CTL		= 0x09,
115e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N50		= 0x01,
116e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N150		= 0x02,
117e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300		= 0x03,
118e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600		= 0x04,
119e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200		= 0x05,
120e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600_ST		= 0x06,
121e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200_ST		= 0x07,
122e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N900_ST		= 0x14,
123e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300_ST		= 0x15,
124e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P50		= 0x08,
125e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P150		= 0x09,
126e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P300		= 0x0a,
127e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P600		= 0x0b,
128e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P1200		= 0x0c,
129e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LN		= 0x10,
130e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P50		= 0x28,
131e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P150		= 0x29,
132e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P300		= 0x2a,
133e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P600		= 0x2b,
134e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P1200		= 0x2c,
135e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P450		= 0x2d,
136e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV100		= 0x01,
137e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV300		= 0x02,
138e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV300		= 0x08,
139e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV500		= 0x09,
140e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_HDMI		= 0x10,
141e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_OTG		= 0x11,
142e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_5V_BOOST		= 0x01,
143e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS_CTL		= 0x08,
144e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL	= 0x09,
145e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_BB_2A		= 0x01,
146e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1	= 0x0d,
147e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2	= 0x0e,
148e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3	= 0x0f,
149e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4	= 0x10,
150e92a4047SStephen Boyd };
151e92a4047SStephen Boyd 
152e92a4047SStephen Boyd enum spmi_common_regulator_registers {
153e92a4047SStephen Boyd 	SPMI_COMMON_REG_DIG_MAJOR_REV		= 0x01,
154e92a4047SStephen Boyd 	SPMI_COMMON_REG_TYPE			= 0x04,
155e92a4047SStephen Boyd 	SPMI_COMMON_REG_SUBTYPE			= 0x05,
156e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_RANGE		= 0x40,
157e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_SET		= 0x41,
158e92a4047SStephen Boyd 	SPMI_COMMON_REG_MODE			= 0x45,
159e92a4047SStephen Boyd 	SPMI_COMMON_REG_ENABLE			= 0x46,
160e92a4047SStephen Boyd 	SPMI_COMMON_REG_PULL_DOWN		= 0x48,
161e92a4047SStephen Boyd 	SPMI_COMMON_REG_SOFT_START		= 0x4c,
162e92a4047SStephen Boyd 	SPMI_COMMON_REG_STEP_CTRL		= 0x61,
163e92a4047SStephen Boyd };
164e92a4047SStephen Boyd 
165e92a4047SStephen Boyd enum spmi_vs_registers {
166e92a4047SStephen Boyd 	SPMI_VS_REG_OCP				= 0x4a,
167e92a4047SStephen Boyd 	SPMI_VS_REG_SOFT_START			= 0x4c,
168e92a4047SStephen Boyd };
169e92a4047SStephen Boyd 
170e92a4047SStephen Boyd enum spmi_boost_registers {
171e92a4047SStephen Boyd 	SPMI_BOOST_REG_CURRENT_LIMIT		= 0x4a,
172e92a4047SStephen Boyd };
173e92a4047SStephen Boyd 
174e92a4047SStephen Boyd enum spmi_boost_byp_registers {
175e92a4047SStephen Boyd 	SPMI_BOOST_BYP_REG_CURRENT_LIMIT	= 0x4b,
176e92a4047SStephen Boyd };
177e92a4047SStephen Boyd 
1780caecaa8SIlia Lin enum spmi_saw3_registers {
1790caecaa8SIlia Lin 	SAW3_SECURE				= 0x00,
1800caecaa8SIlia Lin 	SAW3_ID					= 0x04,
1810caecaa8SIlia Lin 	SAW3_SPM_STS				= 0x0C,
1820caecaa8SIlia Lin 	SAW3_AVS_STS				= 0x10,
1830caecaa8SIlia Lin 	SAW3_PMIC_STS				= 0x14,
1840caecaa8SIlia Lin 	SAW3_RST				= 0x18,
1850caecaa8SIlia Lin 	SAW3_VCTL				= 0x1C,
1860caecaa8SIlia Lin 	SAW3_AVS_CTL				= 0x20,
1870caecaa8SIlia Lin 	SAW3_AVS_LIMIT				= 0x24,
1880caecaa8SIlia Lin 	SAW3_AVS_DLY				= 0x28,
1890caecaa8SIlia Lin 	SAW3_AVS_HYSTERESIS			= 0x2C,
1900caecaa8SIlia Lin 	SAW3_SPM_STS2				= 0x38,
1910caecaa8SIlia Lin 	SAW3_SPM_PMIC_DATA_3			= 0x4C,
1920caecaa8SIlia Lin 	SAW3_VERSION				= 0xFD0,
1930caecaa8SIlia Lin };
1940caecaa8SIlia Lin 
195e92a4047SStephen Boyd /* Used for indexing into ctrl_reg.  These are offets from 0x40 */
196e92a4047SStephen Boyd enum spmi_common_control_register_index {
197e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_RANGE		= 0,
198e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_SET		= 1,
199e92a4047SStephen Boyd 	SPMI_COMMON_IDX_MODE			= 5,
200e92a4047SStephen Boyd 	SPMI_COMMON_IDX_ENABLE			= 6,
201e92a4047SStephen Boyd };
202e92a4047SStephen Boyd 
203e92a4047SStephen Boyd /* Common regulator control register layout */
204e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_MASK			0x80
205e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE			0x80
206e92a4047SStephen Boyd #define SPMI_COMMON_DISABLE			0x00
207e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK	0x08
208e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK	0x04
209e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK	0x02
210e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK	0x01
211e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK	0x0f
212e92a4047SStephen Boyd 
213e92a4047SStephen Boyd /* Common regulator mode register layout */
214e92a4047SStephen Boyd #define SPMI_COMMON_MODE_HPM_MASK		0x80
215e92a4047SStephen Boyd #define SPMI_COMMON_MODE_AUTO_MASK		0x40
216e92a4047SStephen Boyd #define SPMI_COMMON_MODE_BYPASS_MASK		0x20
217e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK	0x10
218e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK	0x08
219e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK	0x04
220e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK	0x02
221e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK	0x01
222e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK	0x1f
223e92a4047SStephen Boyd 
224e92a4047SStephen Boyd /* Common regulator pull down control register layout */
225e92a4047SStephen Boyd #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK	0x80
226e92a4047SStephen Boyd 
227e92a4047SStephen Boyd /* LDO regulator current limit control register layout */
228e92a4047SStephen Boyd #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK	0x80
229e92a4047SStephen Boyd 
230e92a4047SStephen Boyd /* LDO regulator soft start control register layout */
231e92a4047SStephen Boyd #define SPMI_LDO_SOFT_START_ENABLE_MASK		0x80
232e92a4047SStephen Boyd 
233e92a4047SStephen Boyd /* VS regulator over current protection control register layout */
234e92a4047SStephen Boyd #define SPMI_VS_OCP_OVERRIDE			0x01
235e92a4047SStephen Boyd #define SPMI_VS_OCP_NO_OVERRIDE			0x00
236e92a4047SStephen Boyd 
237e92a4047SStephen Boyd /* VS regulator soft start control register layout */
238e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_ENABLE_MASK		0x80
239e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_SEL_MASK		0x03
240e92a4047SStephen Boyd 
241e92a4047SStephen Boyd /* Boost regulator current limit control register layout */
242e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK	0x80
243e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_MASK		0x07
244e92a4047SStephen Boyd 
245e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES		10
246e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS	30
247e92a4047SStephen Boyd #define SPMI_VS_OCP_FALL_DELAY_US		90
248e92a4047SStephen Boyd #define SPMI_VS_OCP_FAULT_DELAY_US		20000
249e92a4047SStephen Boyd 
250e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK		0x18
251e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT	3
252e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK	0x07
253e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT	0
254e92a4047SStephen Boyd 
255e92a4047SStephen Boyd /* Clock rate in kHz of the FTSMPS regulator reference clock. */
256e92a4047SStephen Boyd #define SPMI_FTSMPS_CLOCK_RATE		19200
257e92a4047SStephen Boyd 
258e92a4047SStephen Boyd /* Minimum voltage stepper delay for each step. */
259e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_DELAY		8
2602cf7b99cSStephen Boyd #define SPMI_DEFAULT_STEP_DELAY		20
261e92a4047SStephen Boyd 
262e92a4047SStephen Boyd /*
263e92a4047SStephen Boyd  * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
264e92a4047SStephen Boyd  * adjust the step rate in order to account for oscillator variance.
265e92a4047SStephen Boyd  */
266e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_NUM	4
267e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_DEN	5
268e92a4047SStephen Boyd 
269e92a4047SStephen Boyd /* VSET value to decide the range of ULT SMPS */
270e92a4047SStephen Boyd #define ULT_SMPS_RANGE_SPLIT 0x60
271e92a4047SStephen Boyd 
272e92a4047SStephen Boyd /**
273e92a4047SStephen Boyd  * struct spmi_voltage_range - regulator set point voltage mapping description
274e92a4047SStephen Boyd  * @min_uV:		Minimum programmable output voltage resulting from
275e92a4047SStephen Boyd  *			set point register value 0x00
276e92a4047SStephen Boyd  * @max_uV:		Maximum programmable output voltage
277e92a4047SStephen Boyd  * @step_uV:		Output voltage increase resulting from the set point
278e92a4047SStephen Boyd  *			register value increasing by 1
279e92a4047SStephen Boyd  * @set_point_min_uV:	Minimum allowed voltage
280e92a4047SStephen Boyd  * @set_point_max_uV:	Maximum allowed voltage.  This may be tweaked in order
281e92a4047SStephen Boyd  *			to pick which range should be used in the case of
282e92a4047SStephen Boyd  *			overlapping set points.
283e92a4047SStephen Boyd  * @n_voltages:		Number of preferred voltage set points present in this
284e92a4047SStephen Boyd  *			range
285e92a4047SStephen Boyd  * @range_sel:		Voltage range register value corresponding to this range
286e92a4047SStephen Boyd  *
287e92a4047SStephen Boyd  * The following relationships must be true for the values used in this struct:
288e92a4047SStephen Boyd  * (max_uV - min_uV) % step_uV == 0
289e92a4047SStephen Boyd  * (set_point_min_uV - min_uV) % step_uV == 0*
290e92a4047SStephen Boyd  * (set_point_max_uV - min_uV) % step_uV == 0*
291e92a4047SStephen Boyd  * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
292e92a4047SStephen Boyd  *
293e92a4047SStephen Boyd  * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
294e92a4047SStephen Boyd  * specify that the voltage range has meaning, but is not preferred.
295e92a4047SStephen Boyd  */
296e92a4047SStephen Boyd struct spmi_voltage_range {
297e92a4047SStephen Boyd 	int					min_uV;
298e92a4047SStephen Boyd 	int					max_uV;
299e92a4047SStephen Boyd 	int					step_uV;
300e92a4047SStephen Boyd 	int					set_point_min_uV;
301e92a4047SStephen Boyd 	int					set_point_max_uV;
302e92a4047SStephen Boyd 	unsigned				n_voltages;
303e92a4047SStephen Boyd 	u8					range_sel;
304e92a4047SStephen Boyd };
305e92a4047SStephen Boyd 
306e92a4047SStephen Boyd /*
307e92a4047SStephen Boyd  * The ranges specified in the spmi_voltage_set_points struct must be listed
308e92a4047SStephen Boyd  * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
309e92a4047SStephen Boyd  */
310e92a4047SStephen Boyd struct spmi_voltage_set_points {
311e92a4047SStephen Boyd 	struct spmi_voltage_range		*range;
312e92a4047SStephen Boyd 	int					count;
313e92a4047SStephen Boyd 	unsigned				n_voltages;
314e92a4047SStephen Boyd };
315e92a4047SStephen Boyd 
316e92a4047SStephen Boyd struct spmi_regulator {
317e92a4047SStephen Boyd 	struct regulator_desc			desc;
318e92a4047SStephen Boyd 	struct device				*dev;
319e92a4047SStephen Boyd 	struct delayed_work			ocp_work;
320e92a4047SStephen Boyd 	struct regmap				*regmap;
321e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
322e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
323e92a4047SStephen Boyd 	int					ocp_irq;
324e92a4047SStephen Boyd 	int					ocp_count;
325e92a4047SStephen Boyd 	int					ocp_max_retries;
326e92a4047SStephen Boyd 	int					ocp_retry_delay_ms;
327e92a4047SStephen Boyd 	int					hpm_min_load;
328e92a4047SStephen Boyd 	int					slew_rate;
329e92a4047SStephen Boyd 	ktime_t					vs_enable_time;
330e92a4047SStephen Boyd 	u16					base;
331e92a4047SStephen Boyd 	struct list_head			node;
332e92a4047SStephen Boyd };
333e92a4047SStephen Boyd 
334e92a4047SStephen Boyd struct spmi_regulator_mapping {
335e92a4047SStephen Boyd 	enum spmi_regulator_type		type;
336e92a4047SStephen Boyd 	enum spmi_regulator_subtype		subtype;
337e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
338e92a4047SStephen Boyd 	u32					revision_min;
339e92a4047SStephen Boyd 	u32					revision_max;
340e92a4047SStephen Boyd 	struct regulator_ops			*ops;
341e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
342e92a4047SStephen Boyd 	int					hpm_min_load;
343e92a4047SStephen Boyd };
344e92a4047SStephen Boyd 
345e92a4047SStephen Boyd struct spmi_regulator_data {
346e92a4047SStephen Boyd 	const char			*name;
347e92a4047SStephen Boyd 	u16				base;
348e92a4047SStephen Boyd 	const char			*supply;
349e92a4047SStephen Boyd 	const char			*ocp;
350e92a4047SStephen Boyd 	u16				force_type;
351e92a4047SStephen Boyd };
352e92a4047SStephen Boyd 
353e92a4047SStephen Boyd #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
354e92a4047SStephen Boyd 		      _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
355e92a4047SStephen Boyd 	{ \
356e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_##_type, \
357e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
358e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
359e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
360e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
361e92a4047SStephen Boyd 		.ops		= &spmi_##_ops_val##_ops, \
362e92a4047SStephen Boyd 		.set_points	= &_set_points_val##_set_points, \
363e92a4047SStephen Boyd 		.hpm_min_load	= _hpm_min_load, \
364e92a4047SStephen Boyd 	}
365e92a4047SStephen Boyd 
366e92a4047SStephen Boyd #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
367e92a4047SStephen Boyd 	{ \
368e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_VS, \
369e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
370e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
371e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
372e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_VS, \
373e92a4047SStephen Boyd 		.ops		= &spmi_vs_ops, \
374e92a4047SStephen Boyd 	}
375e92a4047SStephen Boyd 
376e92a4047SStephen Boyd #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
377e92a4047SStephen Boyd 			_set_point_max_uV, _max_uV, _step_uV) \
378e92a4047SStephen Boyd 	{ \
379e92a4047SStephen Boyd 		.min_uV			= _min_uV, \
380e92a4047SStephen Boyd 		.max_uV			= _max_uV, \
381e92a4047SStephen Boyd 		.set_point_min_uV	= _set_point_min_uV, \
382e92a4047SStephen Boyd 		.set_point_max_uV	= _set_point_max_uV, \
383e92a4047SStephen Boyd 		.step_uV		= _step_uV, \
384e92a4047SStephen Boyd 		.range_sel		= _range_sel, \
385e92a4047SStephen Boyd 	}
386e92a4047SStephen Boyd 
387e92a4047SStephen Boyd #define DEFINE_SPMI_SET_POINTS(name) \
388e92a4047SStephen Boyd struct spmi_voltage_set_points name##_set_points = { \
389e92a4047SStephen Boyd 	.range	= name##_ranges, \
390e92a4047SStephen Boyd 	.count	= ARRAY_SIZE(name##_ranges), \
391e92a4047SStephen Boyd }
392e92a4047SStephen Boyd 
393e92a4047SStephen Boyd /*
394e92a4047SStephen Boyd  * These tables contain the physically available PMIC regulator voltage setpoint
395e92a4047SStephen Boyd  * ranges.  Where two ranges overlap in hardware, one of the ranges is trimmed
396e92a4047SStephen Boyd  * to ensure that the setpoints available to software are monotonically
397e92a4047SStephen Boyd  * increasing and unique.  The set_voltage callback functions expect these
398e92a4047SStephen Boyd  * properties to hold.
399e92a4047SStephen Boyd  */
400e92a4047SStephen Boyd static struct spmi_voltage_range pldo_ranges[] = {
401e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
402e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
403e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
404e92a4047SStephen Boyd };
405e92a4047SStephen Boyd 
406e92a4047SStephen Boyd static struct spmi_voltage_range nldo1_ranges[] = {
407e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
408e92a4047SStephen Boyd };
409e92a4047SStephen Boyd 
410e92a4047SStephen Boyd static struct spmi_voltage_range nldo2_ranges[] = {
411e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,       0,       0, 1537500, 12500),
412e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,  375000,  768750,  768750,  6250),
413e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  775000, 1537500, 1537500, 12500),
414e92a4047SStephen Boyd };
415e92a4047SStephen Boyd 
416e92a4047SStephen Boyd static struct spmi_voltage_range nldo3_ranges[] = {
417e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
418e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,       0,       0, 1537500, 12500),
419e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,       0,       0, 1537500, 12500),
420e92a4047SStephen Boyd };
421e92a4047SStephen Boyd 
422e92a4047SStephen Boyd static struct spmi_voltage_range ln_ldo_ranges[] = {
423e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  690000,  690000, 1110000, 1110000, 60000),
424e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
425e92a4047SStephen Boyd };
426e92a4047SStephen Boyd 
427e92a4047SStephen Boyd static struct spmi_voltage_range smps_ranges[] = {
428e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
429e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
430e92a4047SStephen Boyd };
431e92a4047SStephen Boyd 
432e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps_ranges[] = {
433e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,       0,  350000, 1275000, 1275000,  5000),
434e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,       0, 1280000, 2040000, 2040000, 10000),
435e92a4047SStephen Boyd };
436e92a4047SStephen Boyd 
437e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps2p5_ranges[] = {
438e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,   80000,  350000, 1355000, 1355000,  5000),
439e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  160000, 1360000, 2200000, 2200000, 10000),
440e92a4047SStephen Boyd };
441e92a4047SStephen Boyd 
442e92a4047SStephen Boyd static struct spmi_voltage_range boost_ranges[] = {
443e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
444e92a4047SStephen Boyd };
445e92a4047SStephen Boyd 
446e92a4047SStephen Boyd static struct spmi_voltage_range boost_byp_ranges[] = {
447e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
448e92a4047SStephen Boyd };
449e92a4047SStephen Boyd 
450e92a4047SStephen Boyd static struct spmi_voltage_range ult_lo_smps_ranges[] = {
451e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
452e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  750000,       0,       0, 1525000, 25000),
453e92a4047SStephen Boyd };
454e92a4047SStephen Boyd 
455e92a4047SStephen Boyd static struct spmi_voltage_range ult_ho_smps_ranges[] = {
456e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
457e92a4047SStephen Boyd };
458e92a4047SStephen Boyd 
459e92a4047SStephen Boyd static struct spmi_voltage_range ult_nldo_ranges[] = {
460e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
461e92a4047SStephen Boyd };
462e92a4047SStephen Boyd 
463e92a4047SStephen Boyd static struct spmi_voltage_range ult_pldo_ranges[] = {
464e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
465e92a4047SStephen Boyd };
466e92a4047SStephen Boyd 
467e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(pldo);
468e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo1);
469e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo2);
470e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo3);
471e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ln_ldo);
472e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(smps);
473e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps);
474e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps2p5);
475e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost);
476e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost_byp);
477e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_lo_smps);
478e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_ho_smps);
479e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_nldo);
480e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_pldo);
481e92a4047SStephen Boyd 
482e92a4047SStephen Boyd static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf,
483e92a4047SStephen Boyd 				 int len)
484e92a4047SStephen Boyd {
485e92a4047SStephen Boyd 	return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len);
486e92a4047SStephen Boyd }
487e92a4047SStephen Boyd 
488e92a4047SStephen Boyd static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr,
489e92a4047SStephen Boyd 				u8 *buf, int len)
490e92a4047SStephen Boyd {
491e92a4047SStephen Boyd 	return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len);
492e92a4047SStephen Boyd }
493e92a4047SStephen Boyd 
494e92a4047SStephen Boyd static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val,
495e92a4047SStephen Boyd 		u8 mask)
496e92a4047SStephen Boyd {
497e92a4047SStephen Boyd 	return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val);
498e92a4047SStephen Boyd }
499e92a4047SStephen Boyd 
500e92a4047SStephen Boyd static int spmi_regulator_vs_enable(struct regulator_dev *rdev)
501e92a4047SStephen Boyd {
502e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
503e92a4047SStephen Boyd 
504e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
505e92a4047SStephen Boyd 		vreg->ocp_count = 0;
506e92a4047SStephen Boyd 		vreg->vs_enable_time = ktime_get();
507e92a4047SStephen Boyd 	}
508e92a4047SStephen Boyd 
5099d485332SAxel Lin 	return regulator_enable_regmap(rdev);
510e92a4047SStephen Boyd }
511e92a4047SStephen Boyd 
512e2adfacdSStephen Boyd static int spmi_regulator_vs_ocp(struct regulator_dev *rdev)
513e2adfacdSStephen Boyd {
514e2adfacdSStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
515e2adfacdSStephen Boyd 	u8 reg = SPMI_VS_OCP_OVERRIDE;
516e2adfacdSStephen Boyd 
517e2adfacdSStephen Boyd 	return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, &reg, 1);
518e2adfacdSStephen Boyd }
519e2adfacdSStephen Boyd 
520e92a4047SStephen Boyd static int spmi_regulator_select_voltage(struct spmi_regulator *vreg,
5211b5b1968SStephen Boyd 					 int min_uV, int max_uV)
522e92a4047SStephen Boyd {
523e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
524e92a4047SStephen Boyd 	int uV = min_uV;
525e92a4047SStephen Boyd 	int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
5261b5b1968SStephen Boyd 	int selector, voltage_sel;
527e92a4047SStephen Boyd 
528e92a4047SStephen Boyd 	/* Check if request voltage is outside of physically settable range. */
529e92a4047SStephen Boyd 	lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
530e92a4047SStephen Boyd 	lim_max_uV =
531e92a4047SStephen Boyd 	  vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
532e92a4047SStephen Boyd 
533e92a4047SStephen Boyd 	if (uV < lim_min_uV && max_uV >= lim_min_uV)
534e92a4047SStephen Boyd 		uV = lim_min_uV;
535e92a4047SStephen Boyd 
536e92a4047SStephen Boyd 	if (uV < lim_min_uV || uV > lim_max_uV) {
537e92a4047SStephen Boyd 		dev_err(vreg->dev,
538e92a4047SStephen Boyd 			"request v=[%d, %d] is outside possible v=[%d, %d]\n",
539e92a4047SStephen Boyd 			 min_uV, max_uV, lim_min_uV, lim_max_uV);
540e92a4047SStephen Boyd 		return -EINVAL;
541e92a4047SStephen Boyd 	}
542e92a4047SStephen Boyd 
543e92a4047SStephen Boyd 	/* Find the range which uV is inside of. */
544e92a4047SStephen Boyd 	for (i = vreg->set_points->count - 1; i > 0; i--) {
545e92a4047SStephen Boyd 		range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
546e92a4047SStephen Boyd 		if (uV > range_max_uV && range_max_uV > 0)
547e92a4047SStephen Boyd 			break;
548e92a4047SStephen Boyd 	}
549e92a4047SStephen Boyd 
550e92a4047SStephen Boyd 	range_id = i;
551e92a4047SStephen Boyd 	range = &vreg->set_points->range[range_id];
552e92a4047SStephen Boyd 
553e92a4047SStephen Boyd 	/*
554e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
555e92a4047SStephen Boyd 	 * the uV value.
556e92a4047SStephen Boyd 	 */
5571b5b1968SStephen Boyd 	voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
5581b5b1968SStephen Boyd 	uV = voltage_sel * range->step_uV + range->min_uV;
559e92a4047SStephen Boyd 
560e92a4047SStephen Boyd 	if (uV > max_uV) {
561e92a4047SStephen Boyd 		dev_err(vreg->dev,
562e92a4047SStephen Boyd 			"request v=[%d, %d] cannot be met by any set point; "
563e92a4047SStephen Boyd 			"next set point: %d\n",
564e92a4047SStephen Boyd 			min_uV, max_uV, uV);
565e92a4047SStephen Boyd 		return -EINVAL;
566e92a4047SStephen Boyd 	}
567e92a4047SStephen Boyd 
5681b5b1968SStephen Boyd 	selector = 0;
569e92a4047SStephen Boyd 	for (i = 0; i < range_id; i++)
5701b5b1968SStephen Boyd 		selector += vreg->set_points->range[i].n_voltages;
5711b5b1968SStephen Boyd 	selector += (uV - range->set_point_min_uV) / range->step_uV;
572e92a4047SStephen Boyd 
5731b5b1968SStephen Boyd 	return selector;
5741b5b1968SStephen Boyd }
5751b5b1968SStephen Boyd 
5761b5b1968SStephen Boyd static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg,
5771b5b1968SStephen Boyd 				  unsigned selector, u8 *range_sel,
5781b5b1968SStephen Boyd 				  u8 *voltage_sel)
5791b5b1968SStephen Boyd {
5801b5b1968SStephen Boyd 	const struct spmi_voltage_range *range, *end;
581ab953b9dSStephen Boyd 	unsigned offset;
5821b5b1968SStephen Boyd 
5831b5b1968SStephen Boyd 	range = vreg->set_points->range;
5841b5b1968SStephen Boyd 	end = range + vreg->set_points->count;
5851b5b1968SStephen Boyd 
5861b5b1968SStephen Boyd 	for (; range < end; range++) {
5871b5b1968SStephen Boyd 		if (selector < range->n_voltages) {
588ab953b9dSStephen Boyd 			/*
589ab953b9dSStephen Boyd 			 * hardware selectors between set point min and real
590ab953b9dSStephen Boyd 			 * min are invalid so we ignore them
591ab953b9dSStephen Boyd 			 */
592ab953b9dSStephen Boyd 			offset = range->set_point_min_uV - range->min_uV;
593ab953b9dSStephen Boyd 			offset /= range->step_uV;
594ab953b9dSStephen Boyd 			*voltage_sel = selector + offset;
5951b5b1968SStephen Boyd 			*range_sel = range->range_sel;
596e92a4047SStephen Boyd 			return 0;
597e92a4047SStephen Boyd 		}
598e92a4047SStephen Boyd 
5991b5b1968SStephen Boyd 		selector -= range->n_voltages;
6001b5b1968SStephen Boyd 	}
6011b5b1968SStephen Boyd 
6021b5b1968SStephen Boyd 	return -EINVAL;
6031b5b1968SStephen Boyd }
6041b5b1968SStephen Boyd 
6051b5b1968SStephen Boyd static int spmi_hw_selector_to_sw(struct spmi_regulator *vreg, u8 hw_sel,
6061b5b1968SStephen Boyd 				  const struct spmi_voltage_range *range)
6071b5b1968SStephen Boyd {
608ab953b9dSStephen Boyd 	unsigned sw_sel = 0;
609ab953b9dSStephen Boyd 	unsigned offset, max_hw_sel;
6101b5b1968SStephen Boyd 	const struct spmi_voltage_range *r = vreg->set_points->range;
611ab953b9dSStephen Boyd 	const struct spmi_voltage_range *end = r + vreg->set_points->count;
6121b5b1968SStephen Boyd 
613ab953b9dSStephen Boyd 	for (; r < end; r++) {
614ab953b9dSStephen Boyd 		if (r == range && range->n_voltages) {
615ab953b9dSStephen Boyd 			/*
616ab953b9dSStephen Boyd 			 * hardware selectors between set point min and real
617ab953b9dSStephen Boyd 			 * min and between set point max and real max are
618ab953b9dSStephen Boyd 			 * invalid so we return an error if they're
619ab953b9dSStephen Boyd 			 * programmed into the hardware
620ab953b9dSStephen Boyd 			 */
621ab953b9dSStephen Boyd 			offset = range->set_point_min_uV - range->min_uV;
622ab953b9dSStephen Boyd 			offset /= range->step_uV;
623ab953b9dSStephen Boyd 			if (hw_sel < offset)
624ab953b9dSStephen Boyd 				return -EINVAL;
625ab953b9dSStephen Boyd 
626ab953b9dSStephen Boyd 			max_hw_sel = range->set_point_max_uV - range->min_uV;
627ab953b9dSStephen Boyd 			max_hw_sel /= range->step_uV;
628ab953b9dSStephen Boyd 			if (hw_sel > max_hw_sel)
629ab953b9dSStephen Boyd 				return -EINVAL;
630ab953b9dSStephen Boyd 
631ab953b9dSStephen Boyd 			return sw_sel + hw_sel - offset;
632ab953b9dSStephen Boyd 		}
6331b5b1968SStephen Boyd 		sw_sel += r->n_voltages;
6341b5b1968SStephen Boyd 	}
6351b5b1968SStephen Boyd 
636ab953b9dSStephen Boyd 	return -EINVAL;
6371b5b1968SStephen Boyd }
6381b5b1968SStephen Boyd 
639e92a4047SStephen Boyd static const struct spmi_voltage_range *
640e92a4047SStephen Boyd spmi_regulator_find_range(struct spmi_regulator *vreg)
641e92a4047SStephen Boyd {
642e92a4047SStephen Boyd 	u8 range_sel;
643e92a4047SStephen Boyd 	const struct spmi_voltage_range *range, *end;
644e92a4047SStephen Boyd 
645e92a4047SStephen Boyd 	range = vreg->set_points->range;
646e92a4047SStephen Boyd 	end = range + vreg->set_points->count;
647e92a4047SStephen Boyd 
648e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1);
649e92a4047SStephen Boyd 
650e92a4047SStephen Boyd 	for (; range < end; range++)
651e92a4047SStephen Boyd 		if (range->range_sel == range_sel)
652e92a4047SStephen Boyd 			return range;
653e92a4047SStephen Boyd 
654e92a4047SStephen Boyd 	return NULL;
655e92a4047SStephen Boyd }
656e92a4047SStephen Boyd 
657e92a4047SStephen Boyd static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg,
6581b5b1968SStephen Boyd 		int min_uV, int max_uV)
659e92a4047SStephen Boyd {
660e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
661e92a4047SStephen Boyd 	int uV = min_uV;
6621b5b1968SStephen Boyd 	int i, selector;
663e92a4047SStephen Boyd 
664e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
665e92a4047SStephen Boyd 	if (!range)
666e92a4047SStephen Boyd 		goto different_range;
667e92a4047SStephen Boyd 
668e92a4047SStephen Boyd 	if (uV < range->min_uV && max_uV >= range->min_uV)
669e92a4047SStephen Boyd 		uV = range->min_uV;
670e92a4047SStephen Boyd 
671e92a4047SStephen Boyd 	if (uV < range->min_uV || uV > range->max_uV) {
672e92a4047SStephen Boyd 		/* Current range doesn't support the requested voltage. */
673e92a4047SStephen Boyd 		goto different_range;
674e92a4047SStephen Boyd 	}
675e92a4047SStephen Boyd 
676e92a4047SStephen Boyd 	/*
677e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
678e92a4047SStephen Boyd 	 * the uV value.
679e92a4047SStephen Boyd 	 */
6801b5b1968SStephen Boyd 	uV = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
6811b5b1968SStephen Boyd 	uV = uV * range->step_uV + range->min_uV;
682e92a4047SStephen Boyd 
683e92a4047SStephen Boyd 	if (uV > max_uV) {
684e92a4047SStephen Boyd 		/*
685e92a4047SStephen Boyd 		 * No set point in the current voltage range is within the
686e92a4047SStephen Boyd 		 * requested min_uV to max_uV range.
687e92a4047SStephen Boyd 		 */
688e92a4047SStephen Boyd 		goto different_range;
689e92a4047SStephen Boyd 	}
690e92a4047SStephen Boyd 
6911b5b1968SStephen Boyd 	selector = 0;
692e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
693e92a4047SStephen Boyd 		if (uV >= vreg->set_points->range[i].set_point_min_uV
6949b2dfee3SStephen Boyd 		    && uV <= vreg->set_points->range[i].set_point_max_uV) {
6951b5b1968SStephen Boyd 			selector +=
696e92a4047SStephen Boyd 			    (uV - vreg->set_points->range[i].set_point_min_uV)
697e92a4047SStephen Boyd 				/ vreg->set_points->range[i].step_uV;
698e92a4047SStephen Boyd 			break;
6999b2dfee3SStephen Boyd 		}
700e92a4047SStephen Boyd 
7011b5b1968SStephen Boyd 		selector += vreg->set_points->range[i].n_voltages;
702e92a4047SStephen Boyd 	}
703e92a4047SStephen Boyd 
7041b5b1968SStephen Boyd 	if (selector >= vreg->set_points->n_voltages)
705e92a4047SStephen Boyd 		goto different_range;
706e92a4047SStephen Boyd 
707b1d21a24SStephen Boyd 	return selector;
708e92a4047SStephen Boyd 
709e92a4047SStephen Boyd different_range:
7101b5b1968SStephen Boyd 	return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
711e92a4047SStephen Boyd }
712e92a4047SStephen Boyd 
7131b5b1968SStephen Boyd static int spmi_regulator_common_map_voltage(struct regulator_dev *rdev,
7141b5b1968SStephen Boyd 					     int min_uV, int max_uV)
7151b5b1968SStephen Boyd {
7161b5b1968SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7171b5b1968SStephen Boyd 
7181b5b1968SStephen Boyd 	/*
7191b5b1968SStephen Boyd 	 * Favor staying in the current voltage range if possible.  This avoids
7201b5b1968SStephen Boyd 	 * voltage spikes that occur when changing the voltage range.
7211b5b1968SStephen Boyd 	 */
7221b5b1968SStephen Boyd 	return spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV);
7231b5b1968SStephen Boyd }
7241b5b1968SStephen Boyd 
7251b5b1968SStephen Boyd static int
7261b5b1968SStephen Boyd spmi_regulator_common_set_voltage(struct regulator_dev *rdev, unsigned selector)
727e92a4047SStephen Boyd {
728e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
729e92a4047SStephen Boyd 	int ret;
730e92a4047SStephen Boyd 	u8 buf[2];
731e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
732e92a4047SStephen Boyd 
7331b5b1968SStephen Boyd 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
734e92a4047SStephen Boyd 	if (ret)
735e92a4047SStephen Boyd 		return ret;
736e92a4047SStephen Boyd 
737e92a4047SStephen Boyd 	buf[0] = range_sel;
738e92a4047SStephen Boyd 	buf[1] = voltage_sel;
739e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2);
740e92a4047SStephen Boyd }
741e92a4047SStephen Boyd 
742e92a4047SStephen Boyd static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
743e92a4047SStephen Boyd 		unsigned int old_selector, unsigned int new_selector)
744e92a4047SStephen Boyd {
745e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
746e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
747e92a4047SStephen Boyd 	int diff_uV;
748e92a4047SStephen Boyd 
749e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
750e92a4047SStephen Boyd 	if (!range)
751e92a4047SStephen Boyd 		return -EINVAL;
752e92a4047SStephen Boyd 
753e92a4047SStephen Boyd 	diff_uV = abs(new_selector - old_selector) * range->step_uV;
754e92a4047SStephen Boyd 
755e92a4047SStephen Boyd 	return DIV_ROUND_UP(diff_uV, vreg->slew_rate);
756e92a4047SStephen Boyd }
757e92a4047SStephen Boyd 
758e92a4047SStephen Boyd static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev)
759e92a4047SStephen Boyd {
760e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
761e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
762e92a4047SStephen Boyd 	u8 voltage_sel;
763e92a4047SStephen Boyd 
764e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
765e92a4047SStephen Boyd 
766e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
767e92a4047SStephen Boyd 	if (!range)
7681b5b1968SStephen Boyd 		return -EINVAL;
769e92a4047SStephen Boyd 
7701b5b1968SStephen Boyd 	return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
7711b5b1968SStephen Boyd }
7721b5b1968SStephen Boyd 
7731b5b1968SStephen Boyd static int spmi_regulator_single_map_voltage(struct regulator_dev *rdev,
7741b5b1968SStephen Boyd 		int min_uV, int max_uV)
7751b5b1968SStephen Boyd {
7761b5b1968SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7771b5b1968SStephen Boyd 
7781b5b1968SStephen Boyd 	return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
779e92a4047SStephen Boyd }
780e92a4047SStephen Boyd 
781e92a4047SStephen Boyd static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev,
7821b5b1968SStephen Boyd 						   unsigned selector)
783e92a4047SStephen Boyd {
784e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7851b5b1968SStephen Boyd 	u8 sel = selector;
786e92a4047SStephen Boyd 
787e92a4047SStephen Boyd 	/*
788e92a4047SStephen Boyd 	 * Certain types of regulators do not have a range select register so
789e92a4047SStephen Boyd 	 * only voltage set register needs to be written.
790e92a4047SStephen Boyd 	 */
791e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1);
792e92a4047SStephen Boyd }
793e92a4047SStephen Boyd 
794e92a4047SStephen Boyd static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev)
795e92a4047SStephen Boyd {
796e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7971b5b1968SStephen Boyd 	u8 selector;
7981b5b1968SStephen Boyd 	int ret;
799e92a4047SStephen Boyd 
8001b5b1968SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &selector, 1);
8011b5b1968SStephen Boyd 	if (ret)
8021b5b1968SStephen Boyd 		return ret;
803e92a4047SStephen Boyd 
8041b5b1968SStephen Boyd 	return selector;
805e92a4047SStephen Boyd }
806e92a4047SStephen Boyd 
807e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
8081b5b1968SStephen Boyd 						  unsigned selector)
809e92a4047SStephen Boyd {
810e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
811e92a4047SStephen Boyd 	int ret;
812e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
813e92a4047SStephen Boyd 
8141b5b1968SStephen Boyd 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
815e92a4047SStephen Boyd 	if (ret)
816e92a4047SStephen Boyd 		return ret;
817e92a4047SStephen Boyd 
818e92a4047SStephen Boyd 	/*
819e92a4047SStephen Boyd 	 * Calculate VSET based on range
820e92a4047SStephen Boyd 	 * In case of range 0: voltage_sel is a 7 bit value, can be written
821e92a4047SStephen Boyd 	 *			witout any modification.
822e92a4047SStephen Boyd 	 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
823e92a4047SStephen Boyd 	 *			[011].
824e92a4047SStephen Boyd 	 */
825e92a4047SStephen Boyd 	if (range_sel == 1)
826e92a4047SStephen Boyd 		voltage_sel |= ULT_SMPS_RANGE_SPLIT;
827e92a4047SStephen Boyd 
8280f94bffaSJulia Lawall 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET,
829e92a4047SStephen Boyd 				     voltage_sel, 0xff);
830e92a4047SStephen Boyd }
831e92a4047SStephen Boyd 
832e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
833e92a4047SStephen Boyd {
834e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
835e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
836e92a4047SStephen Boyd 	u8 voltage_sel;
837e92a4047SStephen Boyd 
838e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
839e92a4047SStephen Boyd 
840e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
841e92a4047SStephen Boyd 	if (!range)
8421b5b1968SStephen Boyd 		return -EINVAL;
843e92a4047SStephen Boyd 
844e92a4047SStephen Boyd 	if (range->range_sel == 1)
845e92a4047SStephen Boyd 		voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
846e92a4047SStephen Boyd 
8471b5b1968SStephen Boyd 	return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
848e92a4047SStephen Boyd }
849e92a4047SStephen Boyd 
850e92a4047SStephen Boyd static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
851e92a4047SStephen Boyd 			unsigned selector)
852e92a4047SStephen Boyd {
853e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
854e92a4047SStephen Boyd 	int uV = 0;
855e92a4047SStephen Boyd 	int i;
856e92a4047SStephen Boyd 
857e92a4047SStephen Boyd 	if (selector >= vreg->set_points->n_voltages)
858e92a4047SStephen Boyd 		return 0;
859e92a4047SStephen Boyd 
860e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
8619b2dfee3SStephen Boyd 		if (selector < vreg->set_points->range[i].n_voltages) {
862e92a4047SStephen Boyd 			uV = selector * vreg->set_points->range[i].step_uV
863e92a4047SStephen Boyd 				+ vreg->set_points->range[i].set_point_min_uV;
864e92a4047SStephen Boyd 			break;
8659b2dfee3SStephen Boyd 		}
866e92a4047SStephen Boyd 
867e92a4047SStephen Boyd 		selector -= vreg->set_points->range[i].n_voltages;
868e92a4047SStephen Boyd 	}
869e92a4047SStephen Boyd 
870e92a4047SStephen Boyd 	return uV;
871e92a4047SStephen Boyd }
872e92a4047SStephen Boyd 
873e92a4047SStephen Boyd static int
874e92a4047SStephen Boyd spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable)
875e92a4047SStephen Boyd {
876e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
877e92a4047SStephen Boyd 	u8 mask = SPMI_COMMON_MODE_BYPASS_MASK;
878e92a4047SStephen Boyd 	u8 val = 0;
879e92a4047SStephen Boyd 
880e92a4047SStephen Boyd 	if (enable)
881e92a4047SStephen Boyd 		val = mask;
882e92a4047SStephen Boyd 
883e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
884e92a4047SStephen Boyd }
885e92a4047SStephen Boyd 
886e92a4047SStephen Boyd static int
887e92a4047SStephen Boyd spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable)
888e92a4047SStephen Boyd {
889e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
890e92a4047SStephen Boyd 	u8 val;
891e92a4047SStephen Boyd 	int ret;
892e92a4047SStephen Boyd 
893e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1);
894e92a4047SStephen Boyd 	*enable = val & SPMI_COMMON_MODE_BYPASS_MASK;
895e92a4047SStephen Boyd 
896e92a4047SStephen Boyd 	return ret;
897e92a4047SStephen Boyd }
898e92a4047SStephen Boyd 
899e92a4047SStephen Boyd static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev)
900e92a4047SStephen Boyd {
901e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
902e92a4047SStephen Boyd 	u8 reg;
903e92a4047SStephen Boyd 
904e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
905e92a4047SStephen Boyd 
906e92a4047SStephen Boyd 	if (reg & SPMI_COMMON_MODE_HPM_MASK)
907e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
908e92a4047SStephen Boyd 
909e2adfacdSStephen Boyd 	if (reg & SPMI_COMMON_MODE_AUTO_MASK)
910e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
911e2adfacdSStephen Boyd 
912e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
913e92a4047SStephen Boyd }
914e92a4047SStephen Boyd 
915e92a4047SStephen Boyd static int
916e92a4047SStephen Boyd spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode)
917e92a4047SStephen Boyd {
918e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
919e2adfacdSStephen Boyd 	u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
920e92a4047SStephen Boyd 	u8 val = 0;
921e92a4047SStephen Boyd 
922e92a4047SStephen Boyd 	if (mode == REGULATOR_MODE_NORMAL)
923e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_HPM_MASK;
924e2adfacdSStephen Boyd 	else if (mode == REGULATOR_MODE_FAST)
925e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_AUTO_MASK;
926e92a4047SStephen Boyd 
927e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
928e92a4047SStephen Boyd }
929e92a4047SStephen Boyd 
930e92a4047SStephen Boyd static int
931e92a4047SStephen Boyd spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA)
932e92a4047SStephen Boyd {
933e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
934e92a4047SStephen Boyd 	unsigned int mode;
935e92a4047SStephen Boyd 
936e92a4047SStephen Boyd 	if (load_uA >= vreg->hpm_min_load)
937e92a4047SStephen Boyd 		mode = REGULATOR_MODE_NORMAL;
938e92a4047SStephen Boyd 	else
939e92a4047SStephen Boyd 		mode = REGULATOR_MODE_IDLE;
940e92a4047SStephen Boyd 
941e92a4047SStephen Boyd 	return spmi_regulator_common_set_mode(rdev, mode);
942e92a4047SStephen Boyd }
943e92a4047SStephen Boyd 
944e92a4047SStephen Boyd static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev)
945e92a4047SStephen Boyd {
946e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
947e92a4047SStephen Boyd 	unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK;
948e92a4047SStephen Boyd 
949e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN,
950e92a4047SStephen Boyd 				     mask, mask);
951e92a4047SStephen Boyd }
952e92a4047SStephen Boyd 
953e92a4047SStephen Boyd static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev)
954e92a4047SStephen Boyd {
955e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
956e92a4047SStephen Boyd 	unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK;
957e92a4047SStephen Boyd 
958e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START,
959e92a4047SStephen Boyd 				     mask, mask);
960e92a4047SStephen Boyd }
961e92a4047SStephen Boyd 
962e92a4047SStephen Boyd static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA)
963e92a4047SStephen Boyd {
964e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
965e92a4047SStephen Boyd 	enum spmi_regulator_logical_type type = vreg->logical_type;
966e92a4047SStephen Boyd 	unsigned int current_reg;
967e92a4047SStephen Boyd 	u8 reg;
968e92a4047SStephen Boyd 	u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK |
969e92a4047SStephen Boyd 		  SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
970e92a4047SStephen Boyd 	int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500;
971e92a4047SStephen Boyd 
972e92a4047SStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST)
973e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_REG_CURRENT_LIMIT;
974e92a4047SStephen Boyd 	else
975e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT;
976e92a4047SStephen Boyd 
977e92a4047SStephen Boyd 	if (ilim_uA > max || ilim_uA <= 0)
978e92a4047SStephen Boyd 		return -EINVAL;
979e92a4047SStephen Boyd 
980e92a4047SStephen Boyd 	reg = (ilim_uA - 1) / 500;
981e92a4047SStephen Boyd 	reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
982e92a4047SStephen Boyd 
983e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, current_reg, reg, mask);
984e92a4047SStephen Boyd }
985e92a4047SStephen Boyd 
986e92a4047SStephen Boyd static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg)
987e92a4047SStephen Boyd {
988e92a4047SStephen Boyd 	int ret;
989e92a4047SStephen Boyd 
990e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
991e92a4047SStephen Boyd 		SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
992e92a4047SStephen Boyd 
993e92a4047SStephen Boyd 	vreg->vs_enable_time = ktime_get();
994e92a4047SStephen Boyd 
995e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
996e92a4047SStephen Boyd 		SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
997e92a4047SStephen Boyd 
998e92a4047SStephen Boyd 	return ret;
999e92a4047SStephen Boyd }
1000e92a4047SStephen Boyd 
1001e92a4047SStephen Boyd static void spmi_regulator_vs_ocp_work(struct work_struct *work)
1002e92a4047SStephen Boyd {
1003e92a4047SStephen Boyd 	struct delayed_work *dwork = to_delayed_work(work);
1004e92a4047SStephen Boyd 	struct spmi_regulator *vreg
1005e92a4047SStephen Boyd 		= container_of(dwork, struct spmi_regulator, ocp_work);
1006e92a4047SStephen Boyd 
1007e92a4047SStephen Boyd 	spmi_regulator_vs_clear_ocp(vreg);
1008e92a4047SStephen Boyd }
1009e92a4047SStephen Boyd 
1010e92a4047SStephen Boyd static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data)
1011e92a4047SStephen Boyd {
1012e92a4047SStephen Boyd 	struct spmi_regulator *vreg = data;
1013e92a4047SStephen Boyd 	ktime_t ocp_irq_time;
1014e92a4047SStephen Boyd 	s64 ocp_trigger_delay_us;
1015e92a4047SStephen Boyd 
1016e92a4047SStephen Boyd 	ocp_irq_time = ktime_get();
1017e92a4047SStephen Boyd 	ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
1018e92a4047SStephen Boyd 						vreg->vs_enable_time);
1019e92a4047SStephen Boyd 
1020e92a4047SStephen Boyd 	/*
1021e92a4047SStephen Boyd 	 * Reset the OCP count if there is a large delay between switch enable
1022e92a4047SStephen Boyd 	 * and when OCP triggers.  This is indicative of a hotplug event as
1023e92a4047SStephen Boyd 	 * opposed to a fault.
1024e92a4047SStephen Boyd 	 */
1025e92a4047SStephen Boyd 	if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US)
1026e92a4047SStephen Boyd 		vreg->ocp_count = 0;
1027e92a4047SStephen Boyd 
1028e92a4047SStephen Boyd 	/* Wait for switch output to settle back to 0 V after OCP triggered. */
1029e92a4047SStephen Boyd 	udelay(SPMI_VS_OCP_FALL_DELAY_US);
1030e92a4047SStephen Boyd 
1031e92a4047SStephen Boyd 	vreg->ocp_count++;
1032e92a4047SStephen Boyd 
1033e92a4047SStephen Boyd 	if (vreg->ocp_count == 1) {
1034e92a4047SStephen Boyd 		/* Immediately clear the over current condition. */
1035e92a4047SStephen Boyd 		spmi_regulator_vs_clear_ocp(vreg);
1036e92a4047SStephen Boyd 	} else if (vreg->ocp_count <= vreg->ocp_max_retries) {
1037e92a4047SStephen Boyd 		/* Schedule the over current clear task to run later. */
1038e92a4047SStephen Boyd 		schedule_delayed_work(&vreg->ocp_work,
1039e92a4047SStephen Boyd 			msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
1040e92a4047SStephen Boyd 	} else {
1041e92a4047SStephen Boyd 		dev_err(vreg->dev,
1042e92a4047SStephen Boyd 			"OCP triggered %d times; no further retries\n",
1043e92a4047SStephen Boyd 			vreg->ocp_count);
1044e92a4047SStephen Boyd 	}
1045e92a4047SStephen Boyd 
1046e92a4047SStephen Boyd 	return IRQ_HANDLED;
1047e92a4047SStephen Boyd }
1048e92a4047SStephen Boyd 
10490caecaa8SIlia Lin #define SAW3_VCTL_DATA_MASK	0xFF
10500caecaa8SIlia Lin #define SAW3_VCTL_CLEAR_MASK	0x700FF
10510caecaa8SIlia Lin #define SAW3_AVS_CTL_EN_MASK	0x1
10520caecaa8SIlia Lin #define SAW3_AVS_CTL_TGGL_MASK	0x8000000
10530caecaa8SIlia Lin #define SAW3_AVS_CTL_CLEAR_MASK	0x7efc00
10540caecaa8SIlia Lin 
10559689ca0aSNiklas Cassel static struct regmap *saw_regmap;
10560caecaa8SIlia Lin 
10570caecaa8SIlia Lin static void spmi_saw_set_vdd(void *data)
10580caecaa8SIlia Lin {
10590caecaa8SIlia Lin 	u32 vctl, data3, avs_ctl, pmic_sts;
10600caecaa8SIlia Lin 	bool avs_enabled = false;
10610caecaa8SIlia Lin 	unsigned long timeout;
10620caecaa8SIlia Lin 	u8 voltage_sel = *(u8 *)data;
10630caecaa8SIlia Lin 
10640caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_AVS_CTL, &avs_ctl);
10650caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_VCTL, &vctl);
10660caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_SPM_PMIC_DATA_3, &data3);
10670caecaa8SIlia Lin 
10680caecaa8SIlia Lin 	/* select the band */
10690caecaa8SIlia Lin 	vctl &= ~SAW3_VCTL_CLEAR_MASK;
10700caecaa8SIlia Lin 	vctl |= (u32)voltage_sel;
10710caecaa8SIlia Lin 
10720caecaa8SIlia Lin 	data3 &= ~SAW3_VCTL_CLEAR_MASK;
10730caecaa8SIlia Lin 	data3 |= (u32)voltage_sel;
10740caecaa8SIlia Lin 
10750caecaa8SIlia Lin 	/* If AVS is enabled, switch it off during the voltage change */
10760caecaa8SIlia Lin 	avs_enabled = SAW3_AVS_CTL_EN_MASK & avs_ctl;
10770caecaa8SIlia Lin 	if (avs_enabled) {
10780caecaa8SIlia Lin 		avs_ctl &= ~SAW3_AVS_CTL_TGGL_MASK;
10790caecaa8SIlia Lin 		regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
10800caecaa8SIlia Lin 	}
10810caecaa8SIlia Lin 
10820caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_RST, 1);
10830caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_VCTL, vctl);
10840caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_SPM_PMIC_DATA_3, data3);
10850caecaa8SIlia Lin 
10860caecaa8SIlia Lin 	timeout = jiffies + usecs_to_jiffies(100);
10870caecaa8SIlia Lin 	do {
10880caecaa8SIlia Lin 		regmap_read(saw_regmap, SAW3_PMIC_STS, &pmic_sts);
10890caecaa8SIlia Lin 		pmic_sts &= SAW3_VCTL_DATA_MASK;
10900caecaa8SIlia Lin 		if (pmic_sts == (u32)voltage_sel)
10910caecaa8SIlia Lin 			break;
10920caecaa8SIlia Lin 
10930caecaa8SIlia Lin 		cpu_relax();
10940caecaa8SIlia Lin 
10950caecaa8SIlia Lin 	} while (time_before(jiffies, timeout));
10960caecaa8SIlia Lin 
10970caecaa8SIlia Lin 	/* After successful voltage change, switch the AVS back on */
10980caecaa8SIlia Lin 	if (avs_enabled) {
10990caecaa8SIlia Lin 		pmic_sts &= 0x3f;
11000caecaa8SIlia Lin 		avs_ctl &= ~SAW3_AVS_CTL_CLEAR_MASK;
11010caecaa8SIlia Lin 		avs_ctl |= ((pmic_sts - 4) << 10);
11020caecaa8SIlia Lin 		avs_ctl |= (pmic_sts << 17);
11030caecaa8SIlia Lin 		avs_ctl |= SAW3_AVS_CTL_TGGL_MASK;
11040caecaa8SIlia Lin 		regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
11050caecaa8SIlia Lin 	}
11060caecaa8SIlia Lin }
11070caecaa8SIlia Lin 
11080caecaa8SIlia Lin static int
11090caecaa8SIlia Lin spmi_regulator_saw_set_voltage(struct regulator_dev *rdev, unsigned selector)
11100caecaa8SIlia Lin {
11110caecaa8SIlia Lin 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
11120caecaa8SIlia Lin 	int ret;
11130caecaa8SIlia Lin 	u8 range_sel, voltage_sel;
11140caecaa8SIlia Lin 
11150caecaa8SIlia Lin 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
11160caecaa8SIlia Lin 	if (ret)
11170caecaa8SIlia Lin 		return ret;
11180caecaa8SIlia Lin 
11190caecaa8SIlia Lin 	if (0 != range_sel) {
11200caecaa8SIlia Lin 		dev_dbg(&rdev->dev, "range_sel = %02X voltage_sel = %02X", \
11210caecaa8SIlia Lin 			range_sel, voltage_sel);
11220caecaa8SIlia Lin 		return -EINVAL;
11230caecaa8SIlia Lin 	}
11240caecaa8SIlia Lin 
11250caecaa8SIlia Lin 	/* Always do the SAW register writes on the first CPU */
11260caecaa8SIlia Lin 	return smp_call_function_single(0, spmi_saw_set_vdd, \
11270caecaa8SIlia Lin 					&voltage_sel, true);
11280caecaa8SIlia Lin }
11290caecaa8SIlia Lin 
11300caecaa8SIlia Lin static struct regulator_ops spmi_saw_ops = {};
11310caecaa8SIlia Lin 
1132e92a4047SStephen Boyd static struct regulator_ops spmi_smps_ops = {
11339d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11349d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11359d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11361b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11372cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
11381b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11391b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1140e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1141e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1142e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1143e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1144e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1145e92a4047SStephen Boyd };
1146e92a4047SStephen Boyd 
1147e92a4047SStephen Boyd static struct regulator_ops spmi_ldo_ops = {
11489d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11499d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11509d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11511b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11521b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11531b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1154e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1155e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1156e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1157e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1158e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1159e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1160e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1161e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1162e92a4047SStephen Boyd };
1163e92a4047SStephen Boyd 
1164e92a4047SStephen Boyd static struct regulator_ops spmi_ln_ldo_ops = {
11659d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11669d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11679d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11681b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11691b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11701b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1171e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1172e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1173e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1174e92a4047SStephen Boyd };
1175e92a4047SStephen Boyd 
1176e92a4047SStephen Boyd static struct regulator_ops spmi_vs_ops = {
1177e92a4047SStephen Boyd 	.enable			= spmi_regulator_vs_enable,
11789d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11799d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
1180e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1181e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1182e2adfacdSStephen Boyd 	.set_over_current_protection = spmi_regulator_vs_ocp,
1183919163f6SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1184919163f6SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1185e92a4047SStephen Boyd };
1186e92a4047SStephen Boyd 
1187e92a4047SStephen Boyd static struct regulator_ops spmi_boost_ops = {
11889d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11899d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11909d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11911b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
11921b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
11931b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1194e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1195e92a4047SStephen Boyd 	.set_input_current_limit = spmi_regulator_set_ilim,
1196e92a4047SStephen Boyd };
1197e92a4047SStephen Boyd 
1198e92a4047SStephen Boyd static struct regulator_ops spmi_ftsmps_ops = {
11999d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12009d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12019d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12021b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
1203e92a4047SStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12041b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
12051b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1206e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1207e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1208e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1209e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1210e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1211e92a4047SStephen Boyd };
1212e92a4047SStephen Boyd 
1213e92a4047SStephen Boyd static struct regulator_ops spmi_ult_lo_smps_ops = {
12149d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12159d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12169d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12171b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_ult_lo_smps_set_voltage,
12182cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12191b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_ult_lo_smps_get_voltage,
1220e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1221e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1222e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1223e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1224e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1225e92a4047SStephen Boyd };
1226e92a4047SStephen Boyd 
1227e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ho_smps_ops = {
12289d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12299d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12309d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12311b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
12322cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12331b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
12341b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1235e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1236e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1237e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1238e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1239e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1240e92a4047SStephen Boyd };
1241e92a4047SStephen Boyd 
1242e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ldo_ops = {
12439d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12449d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12459d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12461b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
12471b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
12481b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1249e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1250e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1251e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1252e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1253e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1254e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1255e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1256e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1257e92a4047SStephen Boyd };
1258e92a4047SStephen Boyd 
1259e92a4047SStephen Boyd /* Maximum possible digital major revision value */
1260e92a4047SStephen Boyd #define INF 0xFF
1261e92a4047SStephen Boyd 
1262e92a4047SStephen Boyd static const struct spmi_regulator_mapping supported_regulators[] = {
1263e92a4047SStephen Boyd 	/*           type subtype dig_min dig_max ltype ops setpoints hpm_min */
1264e92a4047SStephen Boyd 	SPMI_VREG(BUCK,  GP_CTL,   0, INF, SMPS,   smps,   smps,   100000),
1265e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N300,     0, INF, LDO,    ldo,    nldo1,   10000),
1266e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     0,   0, LDO,    ldo,    nldo2,   10000),
1267e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    0,   0, LDO,    ldo,    nldo2,   10000),
1268e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     1, INF, LDO,    ldo,    nldo3,   10000),
1269e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    1, INF, LDO,    ldo,    nldo3,   10000),
1270e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  0,   0, LDO,    ldo,    nldo2,   10000),
1271e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 0,   0, LDO,    ldo,    nldo2,   10000),
1272e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  1, INF, LDO,    ldo,    nldo3,   10000),
1273e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 1, INF, LDO,    ldo,    nldo3,   10000),
1274e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P50,      0, INF, LDO,    ldo,    pldo,     5000),
1275e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P150,     0, INF, LDO,    ldo,    pldo,    10000),
1276e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P300,     0, INF, LDO,    ldo,    pldo,    10000),
1277e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P600,     0, INF, LDO,    ldo,    pldo,    10000),
1278e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P1200,    0, INF, LDO,    ldo,    pldo,    10000),
1279e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LN,       0, INF, LN_LDO, ln_ldo, ln_ldo,      0),
1280e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P50,   0, INF, LDO,    ldo,    pldo,     5000),
1281e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P150,  0, INF, LDO,    ldo,    pldo,    10000),
1282e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P300,  0, INF, LDO,    ldo,    pldo,    10000),
1283e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P600,  0, INF, LDO,    ldo,    pldo,    10000),
1284e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P1200, 0, INF, LDO,    ldo,    pldo,    10000),
1285e92a4047SStephen Boyd 	SPMI_VREG_VS(LV100,        0, INF),
1286e92a4047SStephen Boyd 	SPMI_VREG_VS(LV300,        0, INF),
1287e92a4047SStephen Boyd 	SPMI_VREG_VS(MV300,        0, INF),
1288e92a4047SStephen Boyd 	SPMI_VREG_VS(MV500,        0, INF),
1289e92a4047SStephen Boyd 	SPMI_VREG_VS(HDMI,         0, INF),
1290e92a4047SStephen Boyd 	SPMI_VREG_VS(OTG,          0, INF),
1291e92a4047SStephen Boyd 	SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST,  boost,  boost,       0),
1292e92a4047SStephen Boyd 	SPMI_VREG(FTS,   FTS_CTL,  0, INF, FTSMPS, ftsmps, ftsmps, 100000),
1293e92a4047SStephen Boyd 	SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000),
1294e92a4047SStephen Boyd 	SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
1295e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1296e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1297e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1298e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1299e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1300e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1301e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
1302e92a4047SStephen Boyd 						ult_ho_smps,   100000),
1303e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1304e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1305e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1306e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1307e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P150,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1308e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P300,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1309e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P450,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1310e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P600,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1311e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P150,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1312e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P50,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000),
1313e92a4047SStephen Boyd };
1314e92a4047SStephen Boyd 
1315e92a4047SStephen Boyd static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points)
1316e92a4047SStephen Boyd {
1317e92a4047SStephen Boyd 	unsigned int n;
1318e92a4047SStephen Boyd 	struct spmi_voltage_range *range = points->range;
1319e92a4047SStephen Boyd 
1320e92a4047SStephen Boyd 	for (; range < points->range + points->count; range++) {
1321e92a4047SStephen Boyd 		n = 0;
1322e92a4047SStephen Boyd 		if (range->set_point_max_uV) {
1323e92a4047SStephen Boyd 			n = range->set_point_max_uV - range->set_point_min_uV;
1324419d06a1SAxel Lin 			n = (n / range->step_uV) + 1;
1325e92a4047SStephen Boyd 		}
1326e92a4047SStephen Boyd 		range->n_voltages = n;
1327e92a4047SStephen Boyd 		points->n_voltages += n;
1328e92a4047SStephen Boyd 	}
1329e92a4047SStephen Boyd }
1330e92a4047SStephen Boyd 
1331e92a4047SStephen Boyd static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)
1332e92a4047SStephen Boyd {
1333e92a4047SStephen Boyd 	const struct spmi_regulator_mapping *mapping;
1334e92a4047SStephen Boyd 	int ret, i;
1335e92a4047SStephen Boyd 	u32 dig_major_rev;
1336e92a4047SStephen Boyd 	u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1];
1337e92a4047SStephen Boyd 	u8 type, subtype;
1338e92a4047SStephen Boyd 
1339e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version,
1340e92a4047SStephen Boyd 		ARRAY_SIZE(version));
1341e92a4047SStephen Boyd 	if (ret) {
13426ee5c044SStephen Boyd 		dev_dbg(vreg->dev, "could not read version registers\n");
1343e92a4047SStephen Boyd 		return ret;
1344e92a4047SStephen Boyd 	}
1345e92a4047SStephen Boyd 	dig_major_rev	= version[SPMI_COMMON_REG_DIG_MAJOR_REV
1346e92a4047SStephen Boyd 					- SPMI_COMMON_REG_DIG_MAJOR_REV];
13470caecaa8SIlia Lin 
1348e92a4047SStephen Boyd 	if (!force_type) {
1349e92a4047SStephen Boyd 		type		= version[SPMI_COMMON_REG_TYPE -
1350e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1351e92a4047SStephen Boyd 		subtype		= version[SPMI_COMMON_REG_SUBTYPE -
1352e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1353e92a4047SStephen Boyd 	} else {
1354e92a4047SStephen Boyd 		type = force_type >> 8;
1355e92a4047SStephen Boyd 		subtype = force_type;
1356e92a4047SStephen Boyd 	}
1357e92a4047SStephen Boyd 
1358e92a4047SStephen Boyd 	for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1359e92a4047SStephen Boyd 		mapping = &supported_regulators[i];
1360e92a4047SStephen Boyd 		if (mapping->type == type && mapping->subtype == subtype
1361e92a4047SStephen Boyd 		    && mapping->revision_min <= dig_major_rev
1362e92a4047SStephen Boyd 		    && mapping->revision_max >= dig_major_rev)
1363e92a4047SStephen Boyd 			goto found;
1364e92a4047SStephen Boyd 	}
1365e92a4047SStephen Boyd 
1366e92a4047SStephen Boyd 	dev_err(vreg->dev,
1367e92a4047SStephen Boyd 		"unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1368e92a4047SStephen Boyd 		vreg->desc.name, type, subtype, dig_major_rev);
1369e92a4047SStephen Boyd 
1370e92a4047SStephen Boyd 	return -ENODEV;
1371e92a4047SStephen Boyd 
1372e92a4047SStephen Boyd found:
1373e92a4047SStephen Boyd 	vreg->logical_type	= mapping->logical_type;
1374e92a4047SStephen Boyd 	vreg->set_points	= mapping->set_points;
1375e92a4047SStephen Boyd 	vreg->hpm_min_load	= mapping->hpm_min_load;
1376e92a4047SStephen Boyd 	vreg->desc.ops		= mapping->ops;
1377e92a4047SStephen Boyd 
1378e92a4047SStephen Boyd 	if (mapping->set_points) {
1379e92a4047SStephen Boyd 		if (!mapping->set_points->n_voltages)
1380e92a4047SStephen Boyd 			spmi_calculate_num_voltages(mapping->set_points);
1381e92a4047SStephen Boyd 		vreg->desc.n_voltages = mapping->set_points->n_voltages;
1382e92a4047SStephen Boyd 	}
1383e92a4047SStephen Boyd 
1384e92a4047SStephen Boyd 	return 0;
1385e92a4047SStephen Boyd }
1386e92a4047SStephen Boyd 
13872cf7b99cSStephen Boyd static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg)
1388e92a4047SStephen Boyd {
1389e92a4047SStephen Boyd 	int ret;
1390e92a4047SStephen Boyd 	u8 reg = 0;
13912cf7b99cSStephen Boyd 	int step, delay, slew_rate, step_delay;
1392e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
1393e92a4047SStephen Boyd 
1394e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
1395e92a4047SStephen Boyd 	if (ret) {
1396e92a4047SStephen Boyd 		dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
1397e92a4047SStephen Boyd 		return ret;
1398e92a4047SStephen Boyd 	}
1399e92a4047SStephen Boyd 
1400e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
1401e92a4047SStephen Boyd 	if (!range)
1402e92a4047SStephen Boyd 		return -EINVAL;
1403e92a4047SStephen Boyd 
14042cf7b99cSStephen Boyd 	switch (vreg->logical_type) {
14052cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
14062cf7b99cSStephen Boyd 		step_delay = SPMI_FTSMPS_STEP_DELAY;
14072cf7b99cSStephen Boyd 		break;
14082cf7b99cSStephen Boyd 	default:
14092cf7b99cSStephen Boyd 		step_delay = SPMI_DEFAULT_STEP_DELAY;
14102cf7b99cSStephen Boyd 		break;
14112cf7b99cSStephen Boyd 	}
14122cf7b99cSStephen Boyd 
1413e92a4047SStephen Boyd 	step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK;
1414e92a4047SStephen Boyd 	step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT;
1415e92a4047SStephen Boyd 
1416e92a4047SStephen Boyd 	delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK;
1417e92a4047SStephen Boyd 	delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT;
1418e92a4047SStephen Boyd 
1419e92a4047SStephen Boyd 	/* slew_rate has units of uV/us */
1420e92a4047SStephen Boyd 	slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
14212cf7b99cSStephen Boyd 	slew_rate /= 1000 * (step_delay << delay);
1422e92a4047SStephen Boyd 	slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM;
1423e92a4047SStephen Boyd 	slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN;
1424e92a4047SStephen Boyd 
1425e92a4047SStephen Boyd 	/* Ensure that the slew rate is greater than 0 */
1426e92a4047SStephen Boyd 	vreg->slew_rate = max(slew_rate, 1);
1427e92a4047SStephen Boyd 
1428e92a4047SStephen Boyd 	return ret;
1429e92a4047SStephen Boyd }
1430e92a4047SStephen Boyd 
1431e2adfacdSStephen Boyd static int spmi_regulator_init_registers(struct spmi_regulator *vreg,
1432e2adfacdSStephen Boyd 				const struct spmi_regulator_init_data *data)
1433e2adfacdSStephen Boyd {
1434e2adfacdSStephen Boyd 	int ret;
1435e2adfacdSStephen Boyd 	enum spmi_regulator_logical_type type;
1436e2adfacdSStephen Boyd 	u8 ctrl_reg[8], reg, mask;
1437e2adfacdSStephen Boyd 
1438e2adfacdSStephen Boyd 	type = vreg->logical_type;
1439e2adfacdSStephen Boyd 
1440e2adfacdSStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1441e2adfacdSStephen Boyd 	if (ret)
1442e2adfacdSStephen Boyd 		return ret;
1443e2adfacdSStephen Boyd 
1444e2adfacdSStephen Boyd 	/* Set up enable pin control. */
1445e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1446e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO
1447e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_VS)
1448e2adfacdSStephen Boyd 	    && !(data->pin_ctrl_enable
1449e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1450e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] &=
1451e2adfacdSStephen Boyd 			~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1452e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] |=
1453e2adfacdSStephen Boyd 		    data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1454e2adfacdSStephen Boyd 	}
1455e2adfacdSStephen Boyd 
1456e2adfacdSStephen Boyd 	/* Set up mode pin control. */
1457e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1458e2adfacdSStephen Boyd 	    || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO)
1459e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1460e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1461e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1462e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1463e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1464e2adfacdSStephen Boyd 			data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1465e2adfacdSStephen Boyd 	}
1466e2adfacdSStephen Boyd 
1467e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS
1468e2adfacdSStephen Boyd 	   && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1469e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1470e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1471e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1472e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1473e2adfacdSStephen Boyd 	}
1474e2adfacdSStephen Boyd 
1475e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
1476e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
1477e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO)
1478e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1479e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1480e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1481e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1482e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1483e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1484e2adfacdSStephen Boyd 	}
1485e2adfacdSStephen Boyd 
1486e2adfacdSStephen Boyd 	/* Write back any control register values that were modified. */
1487e2adfacdSStephen Boyd 	ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1488e2adfacdSStephen Boyd 	if (ret)
1489e2adfacdSStephen Boyd 		return ret;
1490e2adfacdSStephen Boyd 
1491e2adfacdSStephen Boyd 	/* Set soft start strength and over current protection for VS. */
1492e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) {
1493e2adfacdSStephen Boyd 		if (data->vs_soft_start_strength
1494e2adfacdSStephen Boyd 				!= SPMI_VS_SOFT_START_STR_HW_DEFAULT) {
1495e2adfacdSStephen Boyd 			reg = data->vs_soft_start_strength
1496e2adfacdSStephen Boyd 				& SPMI_VS_SOFT_START_SEL_MASK;
1497e2adfacdSStephen Boyd 			mask = SPMI_VS_SOFT_START_SEL_MASK;
1498e2adfacdSStephen Boyd 			return spmi_vreg_update_bits(vreg,
1499e2adfacdSStephen Boyd 						     SPMI_VS_REG_SOFT_START,
1500e2adfacdSStephen Boyd 						     reg, mask);
1501e2adfacdSStephen Boyd 		}
1502e2adfacdSStephen Boyd 	}
1503e2adfacdSStephen Boyd 
1504e2adfacdSStephen Boyd 	return 0;
1505e2adfacdSStephen Boyd }
1506e2adfacdSStephen Boyd 
1507e2adfacdSStephen Boyd static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg,
1508e2adfacdSStephen Boyd 		struct device_node *node, struct spmi_regulator_init_data *data)
1509e2adfacdSStephen Boyd {
1510e2adfacdSStephen Boyd 	/*
1511e2adfacdSStephen Boyd 	 * Initialize configuration parameters to use hardware default in case
1512e2adfacdSStephen Boyd 	 * no value is specified via device tree.
1513e2adfacdSStephen Boyd 	 */
1514e2adfacdSStephen Boyd 	data->pin_ctrl_enable	    = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1515e2adfacdSStephen Boyd 	data->pin_ctrl_hpm	    = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1516e2adfacdSStephen Boyd 	data->vs_soft_start_strength	= SPMI_VS_SOFT_START_STR_HW_DEFAULT;
1517e2adfacdSStephen Boyd 
1518e2adfacdSStephen Boyd 	/* These bindings are optional, so it is okay if they aren't found. */
1519e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-max-retries",
1520e2adfacdSStephen Boyd 		&vreg->ocp_max_retries);
1521e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-retry-delay",
1522e2adfacdSStephen Boyd 		&vreg->ocp_retry_delay_ms);
1523e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-enable",
1524e2adfacdSStephen Boyd 		&data->pin_ctrl_enable);
1525e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm);
1526e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,vs-soft-start-strength",
1527e2adfacdSStephen Boyd 		&data->vs_soft_start_strength);
1528e2adfacdSStephen Boyd }
1529e2adfacdSStephen Boyd 
1530e92a4047SStephen Boyd static unsigned int spmi_regulator_of_map_mode(unsigned int mode)
1531e92a4047SStephen Boyd {
1532e2adfacdSStephen Boyd 	if (mode == 1)
1533e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
1534e2adfacdSStephen Boyd 	if (mode == 2)
1535e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
1536e92a4047SStephen Boyd 
1537e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
1538e92a4047SStephen Boyd }
1539e92a4047SStephen Boyd 
1540e92a4047SStephen Boyd static int spmi_regulator_of_parse(struct device_node *node,
1541e92a4047SStephen Boyd 			    const struct regulator_desc *desc,
1542e92a4047SStephen Boyd 			    struct regulator_config *config)
1543e92a4047SStephen Boyd {
1544e2adfacdSStephen Boyd 	struct spmi_regulator_init_data data = { };
1545e92a4047SStephen Boyd 	struct spmi_regulator *vreg = config->driver_data;
1546e92a4047SStephen Boyd 	struct device *dev = config->dev;
1547e92a4047SStephen Boyd 	int ret;
1548e92a4047SStephen Boyd 
1549e2adfacdSStephen Boyd 	spmi_regulator_get_dt_config(vreg, node, &data);
1550e2adfacdSStephen Boyd 
1551e2adfacdSStephen Boyd 	if (!vreg->ocp_max_retries)
1552e92a4047SStephen Boyd 		vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES;
1553e2adfacdSStephen Boyd 	if (!vreg->ocp_retry_delay_ms)
1554e92a4047SStephen Boyd 		vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS;
1555e92a4047SStephen Boyd 
1556e2adfacdSStephen Boyd 	ret = spmi_regulator_init_registers(vreg, &data);
1557e2adfacdSStephen Boyd 	if (ret) {
1558e2adfacdSStephen Boyd 		dev_err(dev, "common initialization failed, ret=%d\n", ret);
1559e2adfacdSStephen Boyd 		return ret;
1560e2adfacdSStephen Boyd 	}
1561e2adfacdSStephen Boyd 
15622cf7b99cSStephen Boyd 	switch (vreg->logical_type) {
15632cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
15642cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS:
15652cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS:
15662cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_SMPS:
15672cf7b99cSStephen Boyd 		ret = spmi_regulator_init_slew_rate(vreg);
1568e92a4047SStephen Boyd 		if (ret)
1569e92a4047SStephen Boyd 			return ret;
15702cf7b99cSStephen Boyd 	default:
15712cf7b99cSStephen Boyd 		break;
1572e92a4047SStephen Boyd 	}
1573e92a4047SStephen Boyd 
1574e92a4047SStephen Boyd 	if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS)
1575e92a4047SStephen Boyd 		vreg->ocp_irq = 0;
1576e92a4047SStephen Boyd 
1577e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
1578e92a4047SStephen Boyd 		ret = devm_request_irq(dev, vreg->ocp_irq,
1579e92a4047SStephen Boyd 			spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1580e92a4047SStephen Boyd 			vreg);
1581e92a4047SStephen Boyd 		if (ret < 0) {
1582e92a4047SStephen Boyd 			dev_err(dev, "failed to request irq %d, ret=%d\n",
1583e92a4047SStephen Boyd 				vreg->ocp_irq, ret);
1584e92a4047SStephen Boyd 			return ret;
1585e92a4047SStephen Boyd 		}
1586e92a4047SStephen Boyd 
1587e92a4047SStephen Boyd 		INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work);
1588e92a4047SStephen Boyd 	}
1589e92a4047SStephen Boyd 
1590e92a4047SStephen Boyd 	return 0;
1591e92a4047SStephen Boyd }
1592e92a4047SStephen Boyd 
1593e92a4047SStephen Boyd static const struct spmi_regulator_data pm8941_regulators[] = {
1594e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1595e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1596e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1597c333dfe8SStephen Boyd 	{ "s4", 0xa000, },
1598e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1599e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1600e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1601e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l11", },
1602e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 },
1603e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1604e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 },
1605e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l18_19", },
1606e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1607e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1608e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l4_l11", },
1609e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1610e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1611e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1612e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1613e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1614e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1615e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l8_l16_l18_19", },
1616e92a4047SStephen Boyd 	{ "l19", 0x5200, "vdd_l8_l16_l18_19", },
1617e92a4047SStephen Boyd 	{ "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1618e92a4047SStephen Boyd 	{ "l21", 0x5400, "vdd_l21", },
1619e92a4047SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1620e92a4047SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1621e92a4047SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1622e92a4047SStephen Boyd 	{ "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1623e92a4047SStephen Boyd 	{ "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1624e92a4047SStephen Boyd 	{ "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
162593bfe79bSStephen Boyd 	{ "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", },
162693bfe79bSStephen Boyd 	{ "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", },
1627e92a4047SStephen Boyd 	{ }
1628e92a4047SStephen Boyd };
1629e92a4047SStephen Boyd 
1630e92a4047SStephen Boyd static const struct spmi_regulator_data pm8841_regulators[] = {
1631e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1632e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", NULL, 0x1c08 },
1633e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1634e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 },
1635e92a4047SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", NULL, 0x1c08 },
1636e92a4047SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", NULL, 0x1c08 },
1637e92a4047SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", NULL, 0x1c08 },
1638e92a4047SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", NULL, 0x1c08 },
1639e92a4047SStephen Boyd 	{ }
1640e92a4047SStephen Boyd };
1641e92a4047SStephen Boyd 
1642e92a4047SStephen Boyd static const struct spmi_regulator_data pm8916_regulators[] = {
1643e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1644e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1645e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1646e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
1647e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1648e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2", },
1649e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1650e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l5_l6", },
1651e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l4_l5_l6", },
1652e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l4_l5_l6", },
1653e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l7", },
1654e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1655e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1656e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1657e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1658e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1659e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1660e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1661e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1662e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1663e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1664e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1665e92a4047SStephen Boyd 	{ }
1666e92a4047SStephen Boyd };
1667e92a4047SStephen Boyd 
166850314e55SStephen Boyd static const struct spmi_regulator_data pm8994_regulators[] = {
166950314e55SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
167050314e55SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
167150314e55SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
167250314e55SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
167350314e55SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", },
167450314e55SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", },
167550314e55SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", },
167650314e55SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", },
167750314e55SStephen Boyd 	{ "s9", 0x2c00, "vdd_s9", },
167850314e55SStephen Boyd 	{ "s10", 0x2f00, "vdd_s10", },
167950314e55SStephen Boyd 	{ "s11", 0x3200, "vdd_s11", },
168050314e55SStephen Boyd 	{ "s12", 0x3500, "vdd_s12", },
168150314e55SStephen Boyd 	{ "l1", 0x4000, "vdd_l1", },
168250314e55SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_l26_l28", },
168350314e55SStephen Boyd 	{ "l3", 0x4200, "vdd_l3_l11", },
168450314e55SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l27_l31", },
168550314e55SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", },
168650314e55SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l32", },
168750314e55SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", },
168850314e55SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l30", },
168950314e55SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l18_l22", },
169050314e55SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l18_l22", },
169150314e55SStephen Boyd 	{ "l11", 0x4a00, "vdd_l3_l11", },
169250314e55SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l32", },
169350314e55SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
169450314e55SStephen Boyd 	{ "l14", 0x4d00, "vdd_l14_l15", },
169550314e55SStephen Boyd 	{ "l15", 0x4e00, "vdd_l14_l15", },
169650314e55SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l30", },
169750314e55SStephen Boyd 	{ "l17", 0x5000, "vdd_l17_l29", },
169850314e55SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l18_l22", },
169950314e55SStephen Boyd 	{ "l19", 0x5200, "vdd_l13_l19_l23_l24", },
170050314e55SStephen Boyd 	{ "l20", 0x5300, "vdd_l20_l21", },
170150314e55SStephen Boyd 	{ "l21", 0x5400, "vdd_l20_l21", },
170250314e55SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l18_l22", },
170350314e55SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l19_l23_l24", },
170450314e55SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l19_l23_l24", },
170550314e55SStephen Boyd 	{ "l25", 0x5800, "vdd_l25", },
170650314e55SStephen Boyd 	{ "l26", 0x5900, "vdd_l2_l26_l28", },
170750314e55SStephen Boyd 	{ "l27", 0x5a00, "vdd_l4_l27_l31", },
170850314e55SStephen Boyd 	{ "l28", 0x5b00, "vdd_l2_l26_l28", },
170950314e55SStephen Boyd 	{ "l29", 0x5c00, "vdd_l17_l29", },
171050314e55SStephen Boyd 	{ "l30", 0x5d00, "vdd_l8_l16_l30", },
171150314e55SStephen Boyd 	{ "l31", 0x5e00, "vdd_l4_l27_l31", },
171250314e55SStephen Boyd 	{ "l32", 0x5f00, "vdd_l6_l12_l32", },
171350314e55SStephen Boyd 	{ "lvs1", 0x8000, "vdd_lvs_1_2", },
171450314e55SStephen Boyd 	{ "lvs2", 0x8100, "vdd_lvs_1_2", },
171550314e55SStephen Boyd 	{ }
171650314e55SStephen Boyd };
171750314e55SStephen Boyd 
1718ca5cd8c9SRajendra Nayak static const struct spmi_regulator_data pmi8994_regulators[] = {
1719ca5cd8c9SRajendra Nayak 	{ "s1", 0x1400, "vdd_s1", },
1720ca5cd8c9SRajendra Nayak 	{ "s2", 0x1700, "vdd_s2", },
1721ca5cd8c9SRajendra Nayak 	{ "s3", 0x1a00, "vdd_s3", },
1722ca5cd8c9SRajendra Nayak 	{ "l1", 0x4000, "vdd_l1", },
1723ca5cd8c9SRajendra Nayak 	{ }
1724ca5cd8c9SRajendra Nayak };
1725ca5cd8c9SRajendra Nayak 
1726e92a4047SStephen Boyd static const struct of_device_id qcom_spmi_regulator_match[] = {
1727e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators },
1728e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators },
1729e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators },
173050314e55SStephen Boyd 	{ .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators },
1731ca5cd8c9SRajendra Nayak 	{ .compatible = "qcom,pmi8994-regulators", .data = &pmi8994_regulators },
1732e92a4047SStephen Boyd 	{ }
1733e92a4047SStephen Boyd };
1734e92a4047SStephen Boyd MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match);
1735e92a4047SStephen Boyd 
1736e92a4047SStephen Boyd static int qcom_spmi_regulator_probe(struct platform_device *pdev)
1737e92a4047SStephen Boyd {
1738e92a4047SStephen Boyd 	const struct spmi_regulator_data *reg;
1739e92a4047SStephen Boyd 	const struct of_device_id *match;
1740e92a4047SStephen Boyd 	struct regulator_config config = { };
1741e92a4047SStephen Boyd 	struct regulator_dev *rdev;
1742e92a4047SStephen Boyd 	struct spmi_regulator *vreg;
1743e92a4047SStephen Boyd 	struct regmap *regmap;
1744e92a4047SStephen Boyd 	const char *name;
1745e92a4047SStephen Boyd 	struct device *dev = &pdev->dev;
17460caecaa8SIlia Lin 	struct device_node *node = pdev->dev.of_node;
1747fffe7f52SNiklas Cassel 	struct device_node *syscon, *reg_node;
1748fffe7f52SNiklas Cassel 	struct property *reg_prop;
17490caecaa8SIlia Lin 	int ret, lenp;
1750e92a4047SStephen Boyd 	struct list_head *vreg_list;
1751e92a4047SStephen Boyd 
1752e92a4047SStephen Boyd 	vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL);
1753e92a4047SStephen Boyd 	if (!vreg_list)
1754e92a4047SStephen Boyd 		return -ENOMEM;
1755e92a4047SStephen Boyd 	INIT_LIST_HEAD(vreg_list);
1756e92a4047SStephen Boyd 	platform_set_drvdata(pdev, vreg_list);
1757e92a4047SStephen Boyd 
1758e92a4047SStephen Boyd 	regmap = dev_get_regmap(dev->parent, NULL);
1759e92a4047SStephen Boyd 	if (!regmap)
1760e92a4047SStephen Boyd 		return -ENODEV;
1761e92a4047SStephen Boyd 
1762e92a4047SStephen Boyd 	match = of_match_device(qcom_spmi_regulator_match, &pdev->dev);
1763e92a4047SStephen Boyd 	if (!match)
1764e92a4047SStephen Boyd 		return -ENODEV;
1765e92a4047SStephen Boyd 
17660caecaa8SIlia Lin 	if (of_find_property(node, "qcom,saw-reg", &lenp)) {
17670caecaa8SIlia Lin 		syscon = of_parse_phandle(node, "qcom,saw-reg", 0);
17680caecaa8SIlia Lin 		saw_regmap = syscon_node_to_regmap(syscon);
17690caecaa8SIlia Lin 		of_node_put(syscon);
177085046a15SNiklas Cassel 		if (IS_ERR(saw_regmap))
17710caecaa8SIlia Lin 			dev_err(dev, "ERROR reading SAW regmap\n");
17720caecaa8SIlia Lin 	}
17730caecaa8SIlia Lin 
1774e92a4047SStephen Boyd 	for (reg = match->data; reg->name; reg++) {
17750caecaa8SIlia Lin 
1776fffe7f52SNiklas Cassel 		if (saw_regmap) {
1777fffe7f52SNiklas Cassel 			reg_node = of_get_child_by_name(node, reg->name);
1778fffe7f52SNiklas Cassel 			reg_prop = of_find_property(reg_node, "qcom,saw-slave",
1779fffe7f52SNiklas Cassel 						    &lenp);
1780fffe7f52SNiklas Cassel 			of_node_put(reg_node);
1781fffe7f52SNiklas Cassel 			if (reg_prop)
17820caecaa8SIlia Lin 				continue;
17830caecaa8SIlia Lin 		}
17840caecaa8SIlia Lin 
1785e92a4047SStephen Boyd 		vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1786e92a4047SStephen Boyd 		if (!vreg)
1787e92a4047SStephen Boyd 			return -ENOMEM;
1788e92a4047SStephen Boyd 
1789e92a4047SStephen Boyd 		vreg->dev = dev;
1790e92a4047SStephen Boyd 		vreg->base = reg->base;
1791e92a4047SStephen Boyd 		vreg->regmap = regmap;
1792e92a4047SStephen Boyd 		if (reg->ocp) {
1793e92a4047SStephen Boyd 			vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp);
1794e92a4047SStephen Boyd 			if (vreg->ocp_irq < 0) {
1795e92a4047SStephen Boyd 				ret = vreg->ocp_irq;
1796e92a4047SStephen Boyd 				goto err;
1797e92a4047SStephen Boyd 			}
1798e92a4047SStephen Boyd 		}
1799e92a4047SStephen Boyd 		vreg->desc.id = -1;
1800e92a4047SStephen Boyd 		vreg->desc.owner = THIS_MODULE;
1801e92a4047SStephen Boyd 		vreg->desc.type = REGULATOR_VOLTAGE;
18029d485332SAxel Lin 		vreg->desc.enable_reg = reg->base + SPMI_COMMON_REG_ENABLE;
18039d485332SAxel Lin 		vreg->desc.enable_mask = SPMI_COMMON_ENABLE_MASK;
18049d485332SAxel Lin 		vreg->desc.enable_val = SPMI_COMMON_ENABLE;
1805e92a4047SStephen Boyd 		vreg->desc.name = name = reg->name;
1806e92a4047SStephen Boyd 		vreg->desc.supply_name = reg->supply;
1807e92a4047SStephen Boyd 		vreg->desc.of_match = reg->name;
1808e92a4047SStephen Boyd 		vreg->desc.of_parse_cb = spmi_regulator_of_parse;
1809e92a4047SStephen Boyd 		vreg->desc.of_map_mode = spmi_regulator_of_map_mode;
1810e92a4047SStephen Boyd 
1811e92a4047SStephen Boyd 		ret = spmi_regulator_match(vreg, reg->force_type);
1812e92a4047SStephen Boyd 		if (ret)
18136ee5c044SStephen Boyd 			continue;
1814e92a4047SStephen Boyd 
1815fffe7f52SNiklas Cassel 		if (saw_regmap) {
1816fffe7f52SNiklas Cassel 			reg_node = of_get_child_by_name(node, reg->name);
1817fffe7f52SNiklas Cassel 			reg_prop = of_find_property(reg_node, "qcom,saw-leader",
1818fffe7f52SNiklas Cassel 						    &lenp);
1819fffe7f52SNiklas Cassel 			of_node_put(reg_node);
1820fffe7f52SNiklas Cassel 			if (reg_prop) {
18210caecaa8SIlia Lin 				spmi_saw_ops = *(vreg->desc.ops);
1822fffe7f52SNiklas Cassel 				spmi_saw_ops.set_voltage_sel =
18230caecaa8SIlia Lin 					spmi_regulator_saw_set_voltage;
18240caecaa8SIlia Lin 				vreg->desc.ops = &spmi_saw_ops;
18250caecaa8SIlia Lin 			}
1826fffe7f52SNiklas Cassel 		}
18270caecaa8SIlia Lin 
1828e92a4047SStephen Boyd 		config.dev = dev;
1829e92a4047SStephen Boyd 		config.driver_data = vreg;
18309d485332SAxel Lin 		config.regmap = regmap;
1831e92a4047SStephen Boyd 		rdev = devm_regulator_register(dev, &vreg->desc, &config);
1832e92a4047SStephen Boyd 		if (IS_ERR(rdev)) {
1833e92a4047SStephen Boyd 			dev_err(dev, "failed to register %s\n", name);
1834e92a4047SStephen Boyd 			ret = PTR_ERR(rdev);
1835e92a4047SStephen Boyd 			goto err;
1836e92a4047SStephen Boyd 		}
1837e92a4047SStephen Boyd 
1838e92a4047SStephen Boyd 		INIT_LIST_HEAD(&vreg->node);
1839e92a4047SStephen Boyd 		list_add(&vreg->node, vreg_list);
1840e92a4047SStephen Boyd 	}
1841e92a4047SStephen Boyd 
1842e92a4047SStephen Boyd 	return 0;
1843e92a4047SStephen Boyd 
1844e92a4047SStephen Boyd err:
1845e92a4047SStephen Boyd 	list_for_each_entry(vreg, vreg_list, node)
1846e92a4047SStephen Boyd 		if (vreg->ocp_irq)
1847e92a4047SStephen Boyd 			cancel_delayed_work_sync(&vreg->ocp_work);
1848e92a4047SStephen Boyd 	return ret;
1849e92a4047SStephen Boyd }
1850e92a4047SStephen Boyd 
1851e92a4047SStephen Boyd static int qcom_spmi_regulator_remove(struct platform_device *pdev)
1852e92a4047SStephen Boyd {
1853e92a4047SStephen Boyd 	struct spmi_regulator *vreg;
1854e92a4047SStephen Boyd 	struct list_head *vreg_list = platform_get_drvdata(pdev);
1855e92a4047SStephen Boyd 
1856e92a4047SStephen Boyd 	list_for_each_entry(vreg, vreg_list, node)
1857e92a4047SStephen Boyd 		if (vreg->ocp_irq)
1858e92a4047SStephen Boyd 			cancel_delayed_work_sync(&vreg->ocp_work);
1859e92a4047SStephen Boyd 
1860e92a4047SStephen Boyd 	return 0;
1861e92a4047SStephen Boyd }
1862e92a4047SStephen Boyd 
1863e92a4047SStephen Boyd static struct platform_driver qcom_spmi_regulator_driver = {
1864e92a4047SStephen Boyd 	.driver		= {
1865e92a4047SStephen Boyd 		.name	= "qcom-spmi-regulator",
1866e92a4047SStephen Boyd 		.of_match_table = qcom_spmi_regulator_match,
1867e92a4047SStephen Boyd 	},
1868e92a4047SStephen Boyd 	.probe		= qcom_spmi_regulator_probe,
1869e92a4047SStephen Boyd 	.remove		= qcom_spmi_regulator_remove,
1870e92a4047SStephen Boyd };
1871e92a4047SStephen Boyd module_platform_driver(qcom_spmi_regulator_driver);
1872e92a4047SStephen Boyd 
1873e92a4047SStephen Boyd MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
1874e92a4047SStephen Boyd MODULE_LICENSE("GPL v2");
1875e92a4047SStephen Boyd MODULE_ALIAS("platform:qcom-spmi-regulator");
1876