xref: /linux/drivers/regulator/qcom_spmi-regulator.c (revision 0caecaa87202b667591d57e8ca233ee1b548ba13)
1e92a4047SStephen Boyd /*
2e92a4047SStephen Boyd  * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
3e92a4047SStephen Boyd  *
4e92a4047SStephen Boyd  * This program is free software; you can redistribute it and/or modify
5e92a4047SStephen Boyd  * it under the terms of the GNU General Public License version 2 and
6e92a4047SStephen Boyd  * only version 2 as published by the Free Software Foundation.
7e92a4047SStephen Boyd  *
8e92a4047SStephen Boyd  * This program is distributed in the hope that it will be useful,
9e92a4047SStephen Boyd  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10e92a4047SStephen Boyd  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11e92a4047SStephen Boyd  * GNU General Public License for more details.
12e92a4047SStephen Boyd  */
13e92a4047SStephen Boyd 
14e92a4047SStephen Boyd #include <linux/module.h>
15e92a4047SStephen Boyd #include <linux/delay.h>
16e92a4047SStephen Boyd #include <linux/err.h>
17e92a4047SStephen Boyd #include <linux/kernel.h>
18e92a4047SStephen Boyd #include <linux/interrupt.h>
19e92a4047SStephen Boyd #include <linux/bitops.h>
20e92a4047SStephen Boyd #include <linux/slab.h>
21e92a4047SStephen Boyd #include <linux/of.h>
22e92a4047SStephen Boyd #include <linux/of_device.h>
23e92a4047SStephen Boyd #include <linux/platform_device.h>
24e92a4047SStephen Boyd #include <linux/ktime.h>
25e92a4047SStephen Boyd #include <linux/regulator/driver.h>
26e92a4047SStephen Boyd #include <linux/regmap.h>
27e92a4047SStephen Boyd #include <linux/list.h>
28*0caecaa8SIlia Lin #include <linux/mfd/syscon.h>
29*0caecaa8SIlia Lin #include <linux/io.h>
30e92a4047SStephen Boyd 
31e2adfacdSStephen Boyd /* Pin control enable input pins. */
32e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE		0x00
33e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0		0x01
34e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1		0x02
35e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2		0x04
36e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3		0x08
37e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT	0x10
38e2adfacdSStephen Boyd 
39e2adfacdSStephen Boyd /* Pin control high power mode input pins. */
40e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE		0x00
41e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0			0x01
42e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1			0x02
43e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2			0x04
44e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3			0x08
45e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B		0x10
46e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT		0x20
47e2adfacdSStephen Boyd 
48e2adfacdSStephen Boyd /*
49e2adfacdSStephen Boyd  * Used with enable parameters to specify that hardware default register values
50e2adfacdSStephen Boyd  * should be left unaltered.
51e2adfacdSStephen Boyd  */
52e2adfacdSStephen Boyd #define SPMI_REGULATOR_USE_HW_DEFAULT			2
53e2adfacdSStephen Boyd 
54e2adfacdSStephen Boyd /* Soft start strength of a voltage switch type regulator */
55e2adfacdSStephen Boyd enum spmi_vs_soft_start_str {
56e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P05_UA = 0,
57e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P25_UA,
58e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P55_UA,
59e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P75_UA,
60e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_HW_DEFAULT,
61e2adfacdSStephen Boyd };
62e2adfacdSStephen Boyd 
63e2adfacdSStephen Boyd /**
64e2adfacdSStephen Boyd  * struct spmi_regulator_init_data - spmi-regulator initialization data
65e2adfacdSStephen Boyd  * @pin_ctrl_enable:        Bit mask specifying which hardware pins should be
66e2adfacdSStephen Boyd  *				used to enable the regulator, if any
67e2adfacdSStephen Boyd  *			    Value should be an ORing of
68e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants.  If
69e2adfacdSStephen Boyd  *				the bit specified by
70e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
71e2adfacdSStephen Boyd  *				set, then pin control enable hardware registers
72e2adfacdSStephen Boyd  *				will not be modified.
73e2adfacdSStephen Boyd  * @pin_ctrl_hpm:           Bit mask specifying which hardware pins should be
74e2adfacdSStephen Boyd  *				used to force the regulator into high power
75e2adfacdSStephen Boyd  *				mode, if any
76e2adfacdSStephen Boyd  *			    Value should be an ORing of
77e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_* constants.  If
78e2adfacdSStephen Boyd  *				the bit specified by
79e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
80e2adfacdSStephen Boyd  *				set, then pin control mode hardware registers
81e2adfacdSStephen Boyd  *				will not be modified.
82e2adfacdSStephen Boyd  * @vs_soft_start_strength: This parameter sets the soft start strength for
83e2adfacdSStephen Boyd  *				voltage switch type regulators.  Its value
84e2adfacdSStephen Boyd  *				should be one of SPMI_VS_SOFT_START_STR_*.  If
85e2adfacdSStephen Boyd  *				its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
86e2adfacdSStephen Boyd  *				then the soft start strength will be left at its
87e2adfacdSStephen Boyd  *				default hardware value.
88e2adfacdSStephen Boyd  */
89e2adfacdSStephen Boyd struct spmi_regulator_init_data {
90e2adfacdSStephen Boyd 	unsigned				pin_ctrl_enable;
91e2adfacdSStephen Boyd 	unsigned				pin_ctrl_hpm;
92e2adfacdSStephen Boyd 	enum spmi_vs_soft_start_str		vs_soft_start_strength;
93e2adfacdSStephen Boyd };
94e2adfacdSStephen Boyd 
95e92a4047SStephen Boyd /* These types correspond to unique register layouts. */
96e92a4047SStephen Boyd enum spmi_regulator_logical_type {
97e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_SMPS,
98e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LDO,
99e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_VS,
100e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST,
101e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS,
102e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
103e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO,
104e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
105e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
106e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO,
107e92a4047SStephen Boyd };
108e92a4047SStephen Boyd 
109e92a4047SStephen Boyd enum spmi_regulator_type {
110e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BUCK		= 0x03,
111e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_LDO			= 0x04,
112e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_VS			= 0x05,
113e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST		= 0x1b,
114e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_FTS			= 0x1c,
115e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST_BYP		= 0x1f,
116e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_LDO		= 0x21,
117e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_BUCK		= 0x22,
118e92a4047SStephen Boyd };
119e92a4047SStephen Boyd 
120e92a4047SStephen Boyd enum spmi_regulator_subtype {
121e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_GP_CTL		= 0x08,
122e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_RF_CTL		= 0x09,
123e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N50		= 0x01,
124e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N150		= 0x02,
125e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300		= 0x03,
126e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600		= 0x04,
127e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200		= 0x05,
128e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600_ST		= 0x06,
129e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200_ST		= 0x07,
130e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N900_ST		= 0x14,
131e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300_ST		= 0x15,
132e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P50		= 0x08,
133e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P150		= 0x09,
134e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P300		= 0x0a,
135e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P600		= 0x0b,
136e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P1200		= 0x0c,
137e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LN		= 0x10,
138e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P50		= 0x28,
139e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P150		= 0x29,
140e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P300		= 0x2a,
141e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P600		= 0x2b,
142e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P1200		= 0x2c,
143e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P450		= 0x2d,
144e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV100		= 0x01,
145e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV300		= 0x02,
146e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV300		= 0x08,
147e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV500		= 0x09,
148e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_HDMI		= 0x10,
149e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_OTG		= 0x11,
150e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_5V_BOOST		= 0x01,
151e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS_CTL		= 0x08,
152e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL	= 0x09,
153e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_BB_2A		= 0x01,
154e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1	= 0x0d,
155e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2	= 0x0e,
156e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3	= 0x0f,
157e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4	= 0x10,
158e92a4047SStephen Boyd };
159e92a4047SStephen Boyd 
160e92a4047SStephen Boyd enum spmi_common_regulator_registers {
161e92a4047SStephen Boyd 	SPMI_COMMON_REG_DIG_MAJOR_REV		= 0x01,
162e92a4047SStephen Boyd 	SPMI_COMMON_REG_TYPE			= 0x04,
163e92a4047SStephen Boyd 	SPMI_COMMON_REG_SUBTYPE			= 0x05,
164e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_RANGE		= 0x40,
165e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_SET		= 0x41,
166e92a4047SStephen Boyd 	SPMI_COMMON_REG_MODE			= 0x45,
167e92a4047SStephen Boyd 	SPMI_COMMON_REG_ENABLE			= 0x46,
168e92a4047SStephen Boyd 	SPMI_COMMON_REG_PULL_DOWN		= 0x48,
169e92a4047SStephen Boyd 	SPMI_COMMON_REG_SOFT_START		= 0x4c,
170e92a4047SStephen Boyd 	SPMI_COMMON_REG_STEP_CTRL		= 0x61,
171e92a4047SStephen Boyd };
172e92a4047SStephen Boyd 
173e92a4047SStephen Boyd enum spmi_vs_registers {
174e92a4047SStephen Boyd 	SPMI_VS_REG_OCP				= 0x4a,
175e92a4047SStephen Boyd 	SPMI_VS_REG_SOFT_START			= 0x4c,
176e92a4047SStephen Boyd };
177e92a4047SStephen Boyd 
178e92a4047SStephen Boyd enum spmi_boost_registers {
179e92a4047SStephen Boyd 	SPMI_BOOST_REG_CURRENT_LIMIT		= 0x4a,
180e92a4047SStephen Boyd };
181e92a4047SStephen Boyd 
182e92a4047SStephen Boyd enum spmi_boost_byp_registers {
183e92a4047SStephen Boyd 	SPMI_BOOST_BYP_REG_CURRENT_LIMIT	= 0x4b,
184e92a4047SStephen Boyd };
185e92a4047SStephen Boyd 
186*0caecaa8SIlia Lin enum spmi_saw3_registers {
187*0caecaa8SIlia Lin 	SAW3_SECURE				= 0x00,
188*0caecaa8SIlia Lin 	SAW3_ID					= 0x04,
189*0caecaa8SIlia Lin 	SAW3_SPM_STS				= 0x0C,
190*0caecaa8SIlia Lin 	SAW3_AVS_STS				= 0x10,
191*0caecaa8SIlia Lin 	SAW3_PMIC_STS				= 0x14,
192*0caecaa8SIlia Lin 	SAW3_RST				= 0x18,
193*0caecaa8SIlia Lin 	SAW3_VCTL				= 0x1C,
194*0caecaa8SIlia Lin 	SAW3_AVS_CTL				= 0x20,
195*0caecaa8SIlia Lin 	SAW3_AVS_LIMIT				= 0x24,
196*0caecaa8SIlia Lin 	SAW3_AVS_DLY				= 0x28,
197*0caecaa8SIlia Lin 	SAW3_AVS_HYSTERESIS			= 0x2C,
198*0caecaa8SIlia Lin 	SAW3_SPM_STS2				= 0x38,
199*0caecaa8SIlia Lin 	SAW3_SPM_PMIC_DATA_3			= 0x4C,
200*0caecaa8SIlia Lin 	SAW3_VERSION				= 0xFD0,
201*0caecaa8SIlia Lin };
202*0caecaa8SIlia Lin 
203e92a4047SStephen Boyd /* Used for indexing into ctrl_reg.  These are offets from 0x40 */
204e92a4047SStephen Boyd enum spmi_common_control_register_index {
205e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_RANGE		= 0,
206e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_SET		= 1,
207e92a4047SStephen Boyd 	SPMI_COMMON_IDX_MODE			= 5,
208e92a4047SStephen Boyd 	SPMI_COMMON_IDX_ENABLE			= 6,
209e92a4047SStephen Boyd };
210e92a4047SStephen Boyd 
211e92a4047SStephen Boyd /* Common regulator control register layout */
212e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_MASK			0x80
213e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE			0x80
214e92a4047SStephen Boyd #define SPMI_COMMON_DISABLE			0x00
215e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK	0x08
216e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK	0x04
217e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK	0x02
218e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK	0x01
219e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK	0x0f
220e92a4047SStephen Boyd 
221e92a4047SStephen Boyd /* Common regulator mode register layout */
222e92a4047SStephen Boyd #define SPMI_COMMON_MODE_HPM_MASK		0x80
223e92a4047SStephen Boyd #define SPMI_COMMON_MODE_AUTO_MASK		0x40
224e92a4047SStephen Boyd #define SPMI_COMMON_MODE_BYPASS_MASK		0x20
225e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK	0x10
226e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK	0x08
227e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK	0x04
228e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK	0x02
229e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK	0x01
230e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK	0x1f
231e92a4047SStephen Boyd 
232e92a4047SStephen Boyd /* Common regulator pull down control register layout */
233e92a4047SStephen Boyd #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK	0x80
234e92a4047SStephen Boyd 
235e92a4047SStephen Boyd /* LDO regulator current limit control register layout */
236e92a4047SStephen Boyd #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK	0x80
237e92a4047SStephen Boyd 
238e92a4047SStephen Boyd /* LDO regulator soft start control register layout */
239e92a4047SStephen Boyd #define SPMI_LDO_SOFT_START_ENABLE_MASK		0x80
240e92a4047SStephen Boyd 
241e92a4047SStephen Boyd /* VS regulator over current protection control register layout */
242e92a4047SStephen Boyd #define SPMI_VS_OCP_OVERRIDE			0x01
243e92a4047SStephen Boyd #define SPMI_VS_OCP_NO_OVERRIDE			0x00
244e92a4047SStephen Boyd 
245e92a4047SStephen Boyd /* VS regulator soft start control register layout */
246e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_ENABLE_MASK		0x80
247e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_SEL_MASK		0x03
248e92a4047SStephen Boyd 
249e92a4047SStephen Boyd /* Boost regulator current limit control register layout */
250e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK	0x80
251e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_MASK		0x07
252e92a4047SStephen Boyd 
253e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES		10
254e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS	30
255e92a4047SStephen Boyd #define SPMI_VS_OCP_FALL_DELAY_US		90
256e92a4047SStephen Boyd #define SPMI_VS_OCP_FAULT_DELAY_US		20000
257e92a4047SStephen Boyd 
258e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK		0x18
259e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT	3
260e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK	0x07
261e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT	0
262e92a4047SStephen Boyd 
263e92a4047SStephen Boyd /* Clock rate in kHz of the FTSMPS regulator reference clock. */
264e92a4047SStephen Boyd #define SPMI_FTSMPS_CLOCK_RATE		19200
265e92a4047SStephen Boyd 
266e92a4047SStephen Boyd /* Minimum voltage stepper delay for each step. */
267e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_DELAY		8
2682cf7b99cSStephen Boyd #define SPMI_DEFAULT_STEP_DELAY		20
269e92a4047SStephen Boyd 
270e92a4047SStephen Boyd /*
271e92a4047SStephen Boyd  * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
272e92a4047SStephen Boyd  * adjust the step rate in order to account for oscillator variance.
273e92a4047SStephen Boyd  */
274e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_NUM	4
275e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_DEN	5
276e92a4047SStephen Boyd 
277e92a4047SStephen Boyd /* VSET value to decide the range of ULT SMPS */
278e92a4047SStephen Boyd #define ULT_SMPS_RANGE_SPLIT 0x60
279e92a4047SStephen Boyd 
280e92a4047SStephen Boyd /**
281e92a4047SStephen Boyd  * struct spmi_voltage_range - regulator set point voltage mapping description
282e92a4047SStephen Boyd  * @min_uV:		Minimum programmable output voltage resulting from
283e92a4047SStephen Boyd  *			set point register value 0x00
284e92a4047SStephen Boyd  * @max_uV:		Maximum programmable output voltage
285e92a4047SStephen Boyd  * @step_uV:		Output voltage increase resulting from the set point
286e92a4047SStephen Boyd  *			register value increasing by 1
287e92a4047SStephen Boyd  * @set_point_min_uV:	Minimum allowed voltage
288e92a4047SStephen Boyd  * @set_point_max_uV:	Maximum allowed voltage.  This may be tweaked in order
289e92a4047SStephen Boyd  *			to pick which range should be used in the case of
290e92a4047SStephen Boyd  *			overlapping set points.
291e92a4047SStephen Boyd  * @n_voltages:		Number of preferred voltage set points present in this
292e92a4047SStephen Boyd  *			range
293e92a4047SStephen Boyd  * @range_sel:		Voltage range register value corresponding to this range
294e92a4047SStephen Boyd  *
295e92a4047SStephen Boyd  * The following relationships must be true for the values used in this struct:
296e92a4047SStephen Boyd  * (max_uV - min_uV) % step_uV == 0
297e92a4047SStephen Boyd  * (set_point_min_uV - min_uV) % step_uV == 0*
298e92a4047SStephen Boyd  * (set_point_max_uV - min_uV) % step_uV == 0*
299e92a4047SStephen Boyd  * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
300e92a4047SStephen Boyd  *
301e92a4047SStephen Boyd  * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
302e92a4047SStephen Boyd  * specify that the voltage range has meaning, but is not preferred.
303e92a4047SStephen Boyd  */
304e92a4047SStephen Boyd struct spmi_voltage_range {
305e92a4047SStephen Boyd 	int					min_uV;
306e92a4047SStephen Boyd 	int					max_uV;
307e92a4047SStephen Boyd 	int					step_uV;
308e92a4047SStephen Boyd 	int					set_point_min_uV;
309e92a4047SStephen Boyd 	int					set_point_max_uV;
310e92a4047SStephen Boyd 	unsigned				n_voltages;
311e92a4047SStephen Boyd 	u8					range_sel;
312e92a4047SStephen Boyd };
313e92a4047SStephen Boyd 
314e92a4047SStephen Boyd /*
315e92a4047SStephen Boyd  * The ranges specified in the spmi_voltage_set_points struct must be listed
316e92a4047SStephen Boyd  * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
317e92a4047SStephen Boyd  */
318e92a4047SStephen Boyd struct spmi_voltage_set_points {
319e92a4047SStephen Boyd 	struct spmi_voltage_range		*range;
320e92a4047SStephen Boyd 	int					count;
321e92a4047SStephen Boyd 	unsigned				n_voltages;
322e92a4047SStephen Boyd };
323e92a4047SStephen Boyd 
324e92a4047SStephen Boyd struct spmi_regulator {
325e92a4047SStephen Boyd 	struct regulator_desc			desc;
326e92a4047SStephen Boyd 	struct device				*dev;
327e92a4047SStephen Boyd 	struct delayed_work			ocp_work;
328e92a4047SStephen Boyd 	struct regmap				*regmap;
329e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
330e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
331e92a4047SStephen Boyd 	int					ocp_irq;
332e92a4047SStephen Boyd 	int					ocp_count;
333e92a4047SStephen Boyd 	int					ocp_max_retries;
334e92a4047SStephen Boyd 	int					ocp_retry_delay_ms;
335e92a4047SStephen Boyd 	int					hpm_min_load;
336e92a4047SStephen Boyd 	int					slew_rate;
337e92a4047SStephen Boyd 	ktime_t					vs_enable_time;
338e92a4047SStephen Boyd 	u16					base;
339e92a4047SStephen Boyd 	struct list_head			node;
340e92a4047SStephen Boyd };
341e92a4047SStephen Boyd 
342e92a4047SStephen Boyd struct spmi_regulator_mapping {
343e92a4047SStephen Boyd 	enum spmi_regulator_type		type;
344e92a4047SStephen Boyd 	enum spmi_regulator_subtype		subtype;
345e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
346e92a4047SStephen Boyd 	u32					revision_min;
347e92a4047SStephen Boyd 	u32					revision_max;
348e92a4047SStephen Boyd 	struct regulator_ops			*ops;
349e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
350e92a4047SStephen Boyd 	int					hpm_min_load;
351e92a4047SStephen Boyd };
352e92a4047SStephen Boyd 
353e92a4047SStephen Boyd struct spmi_regulator_data {
354e92a4047SStephen Boyd 	const char			*name;
355e92a4047SStephen Boyd 	u16				base;
356e92a4047SStephen Boyd 	const char			*supply;
357e92a4047SStephen Boyd 	const char			*ocp;
358e92a4047SStephen Boyd 	u16				force_type;
359e92a4047SStephen Boyd };
360e92a4047SStephen Boyd 
361e92a4047SStephen Boyd #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
362e92a4047SStephen Boyd 		      _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
363e92a4047SStephen Boyd 	{ \
364e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_##_type, \
365e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
366e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
367e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
368e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
369e92a4047SStephen Boyd 		.ops		= &spmi_##_ops_val##_ops, \
370e92a4047SStephen Boyd 		.set_points	= &_set_points_val##_set_points, \
371e92a4047SStephen Boyd 		.hpm_min_load	= _hpm_min_load, \
372e92a4047SStephen Boyd 	}
373e92a4047SStephen Boyd 
374e92a4047SStephen Boyd #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
375e92a4047SStephen Boyd 	{ \
376e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_VS, \
377e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
378e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
379e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
380e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_VS, \
381e92a4047SStephen Boyd 		.ops		= &spmi_vs_ops, \
382e92a4047SStephen Boyd 	}
383e92a4047SStephen Boyd 
384e92a4047SStephen Boyd #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
385e92a4047SStephen Boyd 			_set_point_max_uV, _max_uV, _step_uV) \
386e92a4047SStephen Boyd 	{ \
387e92a4047SStephen Boyd 		.min_uV			= _min_uV, \
388e92a4047SStephen Boyd 		.max_uV			= _max_uV, \
389e92a4047SStephen Boyd 		.set_point_min_uV	= _set_point_min_uV, \
390e92a4047SStephen Boyd 		.set_point_max_uV	= _set_point_max_uV, \
391e92a4047SStephen Boyd 		.step_uV		= _step_uV, \
392e92a4047SStephen Boyd 		.range_sel		= _range_sel, \
393e92a4047SStephen Boyd 	}
394e92a4047SStephen Boyd 
395e92a4047SStephen Boyd #define DEFINE_SPMI_SET_POINTS(name) \
396e92a4047SStephen Boyd struct spmi_voltage_set_points name##_set_points = { \
397e92a4047SStephen Boyd 	.range	= name##_ranges, \
398e92a4047SStephen Boyd 	.count	= ARRAY_SIZE(name##_ranges), \
399e92a4047SStephen Boyd }
400e92a4047SStephen Boyd 
401e92a4047SStephen Boyd /*
402e92a4047SStephen Boyd  * These tables contain the physically available PMIC regulator voltage setpoint
403e92a4047SStephen Boyd  * ranges.  Where two ranges overlap in hardware, one of the ranges is trimmed
404e92a4047SStephen Boyd  * to ensure that the setpoints available to software are monotonically
405e92a4047SStephen Boyd  * increasing and unique.  The set_voltage callback functions expect these
406e92a4047SStephen Boyd  * properties to hold.
407e92a4047SStephen Boyd  */
408e92a4047SStephen Boyd static struct spmi_voltage_range pldo_ranges[] = {
409e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
410e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
411e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
412e92a4047SStephen Boyd };
413e92a4047SStephen Boyd 
414e92a4047SStephen Boyd static struct spmi_voltage_range nldo1_ranges[] = {
415e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
416e92a4047SStephen Boyd };
417e92a4047SStephen Boyd 
418e92a4047SStephen Boyd static struct spmi_voltage_range nldo2_ranges[] = {
419e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,       0,       0, 1537500, 12500),
420e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,  375000,  768750,  768750,  6250),
421e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  775000, 1537500, 1537500, 12500),
422e92a4047SStephen Boyd };
423e92a4047SStephen Boyd 
424e92a4047SStephen Boyd static struct spmi_voltage_range nldo3_ranges[] = {
425e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
426e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,       0,       0, 1537500, 12500),
427e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,       0,       0, 1537500, 12500),
428e92a4047SStephen Boyd };
429e92a4047SStephen Boyd 
430e92a4047SStephen Boyd static struct spmi_voltage_range ln_ldo_ranges[] = {
431e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  690000,  690000, 1110000, 1110000, 60000),
432e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
433e92a4047SStephen Boyd };
434e92a4047SStephen Boyd 
435e92a4047SStephen Boyd static struct spmi_voltage_range smps_ranges[] = {
436e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
437e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
438e92a4047SStephen Boyd };
439e92a4047SStephen Boyd 
440e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps_ranges[] = {
441e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,       0,  350000, 1275000, 1275000,  5000),
442e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,       0, 1280000, 2040000, 2040000, 10000),
443e92a4047SStephen Boyd };
444e92a4047SStephen Boyd 
445e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps2p5_ranges[] = {
446e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,   80000,  350000, 1355000, 1355000,  5000),
447e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  160000, 1360000, 2200000, 2200000, 10000),
448e92a4047SStephen Boyd };
449e92a4047SStephen Boyd 
450e92a4047SStephen Boyd static struct spmi_voltage_range boost_ranges[] = {
451e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
452e92a4047SStephen Boyd };
453e92a4047SStephen Boyd 
454e92a4047SStephen Boyd static struct spmi_voltage_range boost_byp_ranges[] = {
455e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
456e92a4047SStephen Boyd };
457e92a4047SStephen Boyd 
458e92a4047SStephen Boyd static struct spmi_voltage_range ult_lo_smps_ranges[] = {
459e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
460e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  750000,       0,       0, 1525000, 25000),
461e92a4047SStephen Boyd };
462e92a4047SStephen Boyd 
463e92a4047SStephen Boyd static struct spmi_voltage_range ult_ho_smps_ranges[] = {
464e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
465e92a4047SStephen Boyd };
466e92a4047SStephen Boyd 
467e92a4047SStephen Boyd static struct spmi_voltage_range ult_nldo_ranges[] = {
468e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
469e92a4047SStephen Boyd };
470e92a4047SStephen Boyd 
471e92a4047SStephen Boyd static struct spmi_voltage_range ult_pldo_ranges[] = {
472e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
473e92a4047SStephen Boyd };
474e92a4047SStephen Boyd 
475e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(pldo);
476e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo1);
477e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo2);
478e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo3);
479e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ln_ldo);
480e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(smps);
481e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps);
482e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps2p5);
483e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost);
484e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost_byp);
485e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_lo_smps);
486e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_ho_smps);
487e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_nldo);
488e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_pldo);
489e92a4047SStephen Boyd 
490e92a4047SStephen Boyd static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf,
491e92a4047SStephen Boyd 				 int len)
492e92a4047SStephen Boyd {
493e92a4047SStephen Boyd 	return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len);
494e92a4047SStephen Boyd }
495e92a4047SStephen Boyd 
496e92a4047SStephen Boyd static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr,
497e92a4047SStephen Boyd 				u8 *buf, int len)
498e92a4047SStephen Boyd {
499e92a4047SStephen Boyd 	return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len);
500e92a4047SStephen Boyd }
501e92a4047SStephen Boyd 
502e92a4047SStephen Boyd static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val,
503e92a4047SStephen Boyd 		u8 mask)
504e92a4047SStephen Boyd {
505e92a4047SStephen Boyd 	return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val);
506e92a4047SStephen Boyd }
507e92a4047SStephen Boyd 
508e92a4047SStephen Boyd static int spmi_regulator_vs_enable(struct regulator_dev *rdev)
509e92a4047SStephen Boyd {
510e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
511e92a4047SStephen Boyd 
512e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
513e92a4047SStephen Boyd 		vreg->ocp_count = 0;
514e92a4047SStephen Boyd 		vreg->vs_enable_time = ktime_get();
515e92a4047SStephen Boyd 	}
516e92a4047SStephen Boyd 
5179d485332SAxel Lin 	return regulator_enable_regmap(rdev);
518e92a4047SStephen Boyd }
519e92a4047SStephen Boyd 
520e2adfacdSStephen Boyd static int spmi_regulator_vs_ocp(struct regulator_dev *rdev)
521e2adfacdSStephen Boyd {
522e2adfacdSStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
523e2adfacdSStephen Boyd 	u8 reg = SPMI_VS_OCP_OVERRIDE;
524e2adfacdSStephen Boyd 
525e2adfacdSStephen Boyd 	return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, &reg, 1);
526e2adfacdSStephen Boyd }
527e2adfacdSStephen Boyd 
528e92a4047SStephen Boyd static int spmi_regulator_select_voltage(struct spmi_regulator *vreg,
5291b5b1968SStephen Boyd 					 int min_uV, int max_uV)
530e92a4047SStephen Boyd {
531e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
532e92a4047SStephen Boyd 	int uV = min_uV;
533e92a4047SStephen Boyd 	int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
5341b5b1968SStephen Boyd 	int selector, voltage_sel;
535e92a4047SStephen Boyd 
536e92a4047SStephen Boyd 	/* Check if request voltage is outside of physically settable range. */
537e92a4047SStephen Boyd 	lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
538e92a4047SStephen Boyd 	lim_max_uV =
539e92a4047SStephen Boyd 	  vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
540e92a4047SStephen Boyd 
541e92a4047SStephen Boyd 	if (uV < lim_min_uV && max_uV >= lim_min_uV)
542e92a4047SStephen Boyd 		uV = lim_min_uV;
543e92a4047SStephen Boyd 
544e92a4047SStephen Boyd 	if (uV < lim_min_uV || uV > lim_max_uV) {
545e92a4047SStephen Boyd 		dev_err(vreg->dev,
546e92a4047SStephen Boyd 			"request v=[%d, %d] is outside possible v=[%d, %d]\n",
547e92a4047SStephen Boyd 			 min_uV, max_uV, lim_min_uV, lim_max_uV);
548e92a4047SStephen Boyd 		return -EINVAL;
549e92a4047SStephen Boyd 	}
550e92a4047SStephen Boyd 
551e92a4047SStephen Boyd 	/* Find the range which uV is inside of. */
552e92a4047SStephen Boyd 	for (i = vreg->set_points->count - 1; i > 0; i--) {
553e92a4047SStephen Boyd 		range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
554e92a4047SStephen Boyd 		if (uV > range_max_uV && range_max_uV > 0)
555e92a4047SStephen Boyd 			break;
556e92a4047SStephen Boyd 	}
557e92a4047SStephen Boyd 
558e92a4047SStephen Boyd 	range_id = i;
559e92a4047SStephen Boyd 	range = &vreg->set_points->range[range_id];
560e92a4047SStephen Boyd 
561e92a4047SStephen Boyd 	/*
562e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
563e92a4047SStephen Boyd 	 * the uV value.
564e92a4047SStephen Boyd 	 */
5651b5b1968SStephen Boyd 	voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
5661b5b1968SStephen Boyd 	uV = voltage_sel * range->step_uV + range->min_uV;
567e92a4047SStephen Boyd 
568e92a4047SStephen Boyd 	if (uV > max_uV) {
569e92a4047SStephen Boyd 		dev_err(vreg->dev,
570e92a4047SStephen Boyd 			"request v=[%d, %d] cannot be met by any set point; "
571e92a4047SStephen Boyd 			"next set point: %d\n",
572e92a4047SStephen Boyd 			min_uV, max_uV, uV);
573e92a4047SStephen Boyd 		return -EINVAL;
574e92a4047SStephen Boyd 	}
575e92a4047SStephen Boyd 
5761b5b1968SStephen Boyd 	selector = 0;
577e92a4047SStephen Boyd 	for (i = 0; i < range_id; i++)
5781b5b1968SStephen Boyd 		selector += vreg->set_points->range[i].n_voltages;
5791b5b1968SStephen Boyd 	selector += (uV - range->set_point_min_uV) / range->step_uV;
580e92a4047SStephen Boyd 
5811b5b1968SStephen Boyd 	return selector;
5821b5b1968SStephen Boyd }
5831b5b1968SStephen Boyd 
5841b5b1968SStephen Boyd static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg,
5851b5b1968SStephen Boyd 				  unsigned selector, u8 *range_sel,
5861b5b1968SStephen Boyd 				  u8 *voltage_sel)
5871b5b1968SStephen Boyd {
5881b5b1968SStephen Boyd 	const struct spmi_voltage_range *range, *end;
589ab953b9dSStephen Boyd 	unsigned offset;
5901b5b1968SStephen Boyd 
5911b5b1968SStephen Boyd 	range = vreg->set_points->range;
5921b5b1968SStephen Boyd 	end = range + vreg->set_points->count;
5931b5b1968SStephen Boyd 
5941b5b1968SStephen Boyd 	for (; range < end; range++) {
5951b5b1968SStephen Boyd 		if (selector < range->n_voltages) {
596ab953b9dSStephen Boyd 			/*
597ab953b9dSStephen Boyd 			 * hardware selectors between set point min and real
598ab953b9dSStephen Boyd 			 * min are invalid so we ignore them
599ab953b9dSStephen Boyd 			 */
600ab953b9dSStephen Boyd 			offset = range->set_point_min_uV - range->min_uV;
601ab953b9dSStephen Boyd 			offset /= range->step_uV;
602ab953b9dSStephen Boyd 			*voltage_sel = selector + offset;
6031b5b1968SStephen Boyd 			*range_sel = range->range_sel;
604e92a4047SStephen Boyd 			return 0;
605e92a4047SStephen Boyd 		}
606e92a4047SStephen Boyd 
6071b5b1968SStephen Boyd 		selector -= range->n_voltages;
6081b5b1968SStephen Boyd 	}
6091b5b1968SStephen Boyd 
6101b5b1968SStephen Boyd 	return -EINVAL;
6111b5b1968SStephen Boyd }
6121b5b1968SStephen Boyd 
6131b5b1968SStephen Boyd static int spmi_hw_selector_to_sw(struct spmi_regulator *vreg, u8 hw_sel,
6141b5b1968SStephen Boyd 				  const struct spmi_voltage_range *range)
6151b5b1968SStephen Boyd {
616ab953b9dSStephen Boyd 	unsigned sw_sel = 0;
617ab953b9dSStephen Boyd 	unsigned offset, max_hw_sel;
6181b5b1968SStephen Boyd 	const struct spmi_voltage_range *r = vreg->set_points->range;
619ab953b9dSStephen Boyd 	const struct spmi_voltage_range *end = r + vreg->set_points->count;
6201b5b1968SStephen Boyd 
621ab953b9dSStephen Boyd 	for (; r < end; r++) {
622ab953b9dSStephen Boyd 		if (r == range && range->n_voltages) {
623ab953b9dSStephen Boyd 			/*
624ab953b9dSStephen Boyd 			 * hardware selectors between set point min and real
625ab953b9dSStephen Boyd 			 * min and between set point max and real max are
626ab953b9dSStephen Boyd 			 * invalid so we return an error if they're
627ab953b9dSStephen Boyd 			 * programmed into the hardware
628ab953b9dSStephen Boyd 			 */
629ab953b9dSStephen Boyd 			offset = range->set_point_min_uV - range->min_uV;
630ab953b9dSStephen Boyd 			offset /= range->step_uV;
631ab953b9dSStephen Boyd 			if (hw_sel < offset)
632ab953b9dSStephen Boyd 				return -EINVAL;
633ab953b9dSStephen Boyd 
634ab953b9dSStephen Boyd 			max_hw_sel = range->set_point_max_uV - range->min_uV;
635ab953b9dSStephen Boyd 			max_hw_sel /= range->step_uV;
636ab953b9dSStephen Boyd 			if (hw_sel > max_hw_sel)
637ab953b9dSStephen Boyd 				return -EINVAL;
638ab953b9dSStephen Boyd 
639ab953b9dSStephen Boyd 			return sw_sel + hw_sel - offset;
640ab953b9dSStephen Boyd 		}
6411b5b1968SStephen Boyd 		sw_sel += r->n_voltages;
6421b5b1968SStephen Boyd 	}
6431b5b1968SStephen Boyd 
644ab953b9dSStephen Boyd 	return -EINVAL;
6451b5b1968SStephen Boyd }
6461b5b1968SStephen Boyd 
647e92a4047SStephen Boyd static const struct spmi_voltage_range *
648e92a4047SStephen Boyd spmi_regulator_find_range(struct spmi_regulator *vreg)
649e92a4047SStephen Boyd {
650e92a4047SStephen Boyd 	u8 range_sel;
651e92a4047SStephen Boyd 	const struct spmi_voltage_range *range, *end;
652e92a4047SStephen Boyd 
653e92a4047SStephen Boyd 	range = vreg->set_points->range;
654e92a4047SStephen Boyd 	end = range + vreg->set_points->count;
655e92a4047SStephen Boyd 
656e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1);
657e92a4047SStephen Boyd 
658e92a4047SStephen Boyd 	for (; range < end; range++)
659e92a4047SStephen Boyd 		if (range->range_sel == range_sel)
660e92a4047SStephen Boyd 			return range;
661e92a4047SStephen Boyd 
662e92a4047SStephen Boyd 	return NULL;
663e92a4047SStephen Boyd }
664e92a4047SStephen Boyd 
665e92a4047SStephen Boyd static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg,
6661b5b1968SStephen Boyd 		int min_uV, int max_uV)
667e92a4047SStephen Boyd {
668e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
669e92a4047SStephen Boyd 	int uV = min_uV;
6701b5b1968SStephen Boyd 	int i, selector;
671e92a4047SStephen Boyd 
672e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
673e92a4047SStephen Boyd 	if (!range)
674e92a4047SStephen Boyd 		goto different_range;
675e92a4047SStephen Boyd 
676e92a4047SStephen Boyd 	if (uV < range->min_uV && max_uV >= range->min_uV)
677e92a4047SStephen Boyd 		uV = range->min_uV;
678e92a4047SStephen Boyd 
679e92a4047SStephen Boyd 	if (uV < range->min_uV || uV > range->max_uV) {
680e92a4047SStephen Boyd 		/* Current range doesn't support the requested voltage. */
681e92a4047SStephen Boyd 		goto different_range;
682e92a4047SStephen Boyd 	}
683e92a4047SStephen Boyd 
684e92a4047SStephen Boyd 	/*
685e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
686e92a4047SStephen Boyd 	 * the uV value.
687e92a4047SStephen Boyd 	 */
6881b5b1968SStephen Boyd 	uV = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
6891b5b1968SStephen Boyd 	uV = uV * range->step_uV + range->min_uV;
690e92a4047SStephen Boyd 
691e92a4047SStephen Boyd 	if (uV > max_uV) {
692e92a4047SStephen Boyd 		/*
693e92a4047SStephen Boyd 		 * No set point in the current voltage range is within the
694e92a4047SStephen Boyd 		 * requested min_uV to max_uV range.
695e92a4047SStephen Boyd 		 */
696e92a4047SStephen Boyd 		goto different_range;
697e92a4047SStephen Boyd 	}
698e92a4047SStephen Boyd 
6991b5b1968SStephen Boyd 	selector = 0;
700e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
701e92a4047SStephen Boyd 		if (uV >= vreg->set_points->range[i].set_point_min_uV
7029b2dfee3SStephen Boyd 		    && uV <= vreg->set_points->range[i].set_point_max_uV) {
7031b5b1968SStephen Boyd 			selector +=
704e92a4047SStephen Boyd 			    (uV - vreg->set_points->range[i].set_point_min_uV)
705e92a4047SStephen Boyd 				/ vreg->set_points->range[i].step_uV;
706e92a4047SStephen Boyd 			break;
7079b2dfee3SStephen Boyd 		}
708e92a4047SStephen Boyd 
7091b5b1968SStephen Boyd 		selector += vreg->set_points->range[i].n_voltages;
710e92a4047SStephen Boyd 	}
711e92a4047SStephen Boyd 
7121b5b1968SStephen Boyd 	if (selector >= vreg->set_points->n_voltages)
713e92a4047SStephen Boyd 		goto different_range;
714e92a4047SStephen Boyd 
715b1d21a24SStephen Boyd 	return selector;
716e92a4047SStephen Boyd 
717e92a4047SStephen Boyd different_range:
7181b5b1968SStephen Boyd 	return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
719e92a4047SStephen Boyd }
720e92a4047SStephen Boyd 
7211b5b1968SStephen Boyd static int spmi_regulator_common_map_voltage(struct regulator_dev *rdev,
7221b5b1968SStephen Boyd 					     int min_uV, int max_uV)
7231b5b1968SStephen Boyd {
7241b5b1968SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7251b5b1968SStephen Boyd 
7261b5b1968SStephen Boyd 	/*
7271b5b1968SStephen Boyd 	 * Favor staying in the current voltage range if possible.  This avoids
7281b5b1968SStephen Boyd 	 * voltage spikes that occur when changing the voltage range.
7291b5b1968SStephen Boyd 	 */
7301b5b1968SStephen Boyd 	return spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV);
7311b5b1968SStephen Boyd }
7321b5b1968SStephen Boyd 
7331b5b1968SStephen Boyd static int
7341b5b1968SStephen Boyd spmi_regulator_common_set_voltage(struct regulator_dev *rdev, unsigned selector)
735e92a4047SStephen Boyd {
736e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
737e92a4047SStephen Boyd 	int ret;
738e92a4047SStephen Boyd 	u8 buf[2];
739e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
740e92a4047SStephen Boyd 
7411b5b1968SStephen Boyd 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
742e92a4047SStephen Boyd 	if (ret)
743e92a4047SStephen Boyd 		return ret;
744e92a4047SStephen Boyd 
745e92a4047SStephen Boyd 	buf[0] = range_sel;
746e92a4047SStephen Boyd 	buf[1] = voltage_sel;
747e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2);
748e92a4047SStephen Boyd }
749e92a4047SStephen Boyd 
750e92a4047SStephen Boyd static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
751e92a4047SStephen Boyd 		unsigned int old_selector, unsigned int new_selector)
752e92a4047SStephen Boyd {
753e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
754e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
755e92a4047SStephen Boyd 	int diff_uV;
756e92a4047SStephen Boyd 
757e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
758e92a4047SStephen Boyd 	if (!range)
759e92a4047SStephen Boyd 		return -EINVAL;
760e92a4047SStephen Boyd 
761e92a4047SStephen Boyd 	diff_uV = abs(new_selector - old_selector) * range->step_uV;
762e92a4047SStephen Boyd 
763e92a4047SStephen Boyd 	return DIV_ROUND_UP(diff_uV, vreg->slew_rate);
764e92a4047SStephen Boyd }
765e92a4047SStephen Boyd 
766e92a4047SStephen Boyd static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev)
767e92a4047SStephen Boyd {
768e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
769e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
770e92a4047SStephen Boyd 	u8 voltage_sel;
771e92a4047SStephen Boyd 
772e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
773e92a4047SStephen Boyd 
774e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
775e92a4047SStephen Boyd 	if (!range)
7761b5b1968SStephen Boyd 		return -EINVAL;
777e92a4047SStephen Boyd 
7781b5b1968SStephen Boyd 	return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
7791b5b1968SStephen Boyd }
7801b5b1968SStephen Boyd 
7811b5b1968SStephen Boyd static int spmi_regulator_single_map_voltage(struct regulator_dev *rdev,
7821b5b1968SStephen Boyd 		int min_uV, int max_uV)
7831b5b1968SStephen Boyd {
7841b5b1968SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7851b5b1968SStephen Boyd 
7861b5b1968SStephen Boyd 	return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
787e92a4047SStephen Boyd }
788e92a4047SStephen Boyd 
789e92a4047SStephen Boyd static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev,
7901b5b1968SStephen Boyd 						   unsigned selector)
791e92a4047SStephen Boyd {
792e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
7931b5b1968SStephen Boyd 	u8 sel = selector;
794e92a4047SStephen Boyd 
795e92a4047SStephen Boyd 	/*
796e92a4047SStephen Boyd 	 * Certain types of regulators do not have a range select register so
797e92a4047SStephen Boyd 	 * only voltage set register needs to be written.
798e92a4047SStephen Boyd 	 */
799e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1);
800e92a4047SStephen Boyd }
801e92a4047SStephen Boyd 
802e92a4047SStephen Boyd static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev)
803e92a4047SStephen Boyd {
804e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
8051b5b1968SStephen Boyd 	u8 selector;
8061b5b1968SStephen Boyd 	int ret;
807e92a4047SStephen Boyd 
8081b5b1968SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &selector, 1);
8091b5b1968SStephen Boyd 	if (ret)
8101b5b1968SStephen Boyd 		return ret;
811e92a4047SStephen Boyd 
8121b5b1968SStephen Boyd 	return selector;
813e92a4047SStephen Boyd }
814e92a4047SStephen Boyd 
815e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
8161b5b1968SStephen Boyd 						  unsigned selector)
817e92a4047SStephen Boyd {
818e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
819e92a4047SStephen Boyd 	int ret;
820e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
821e92a4047SStephen Boyd 
8221b5b1968SStephen Boyd 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
823e92a4047SStephen Boyd 	if (ret)
824e92a4047SStephen Boyd 		return ret;
825e92a4047SStephen Boyd 
826e92a4047SStephen Boyd 	/*
827e92a4047SStephen Boyd 	 * Calculate VSET based on range
828e92a4047SStephen Boyd 	 * In case of range 0: voltage_sel is a 7 bit value, can be written
829e92a4047SStephen Boyd 	 *			witout any modification.
830e92a4047SStephen Boyd 	 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
831e92a4047SStephen Boyd 	 *			[011].
832e92a4047SStephen Boyd 	 */
833e92a4047SStephen Boyd 	if (range_sel == 1)
834e92a4047SStephen Boyd 		voltage_sel |= ULT_SMPS_RANGE_SPLIT;
835e92a4047SStephen Boyd 
8360f94bffaSJulia Lawall 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET,
837e92a4047SStephen Boyd 				     voltage_sel, 0xff);
838e92a4047SStephen Boyd }
839e92a4047SStephen Boyd 
840e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
841e92a4047SStephen Boyd {
842e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
843e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
844e92a4047SStephen Boyd 	u8 voltage_sel;
845e92a4047SStephen Boyd 
846e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
847e92a4047SStephen Boyd 
848e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
849e92a4047SStephen Boyd 	if (!range)
8501b5b1968SStephen Boyd 		return -EINVAL;
851e92a4047SStephen Boyd 
852e92a4047SStephen Boyd 	if (range->range_sel == 1)
853e92a4047SStephen Boyd 		voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
854e92a4047SStephen Boyd 
8551b5b1968SStephen Boyd 	return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
856e92a4047SStephen Boyd }
857e92a4047SStephen Boyd 
858e92a4047SStephen Boyd static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
859e92a4047SStephen Boyd 			unsigned selector)
860e92a4047SStephen Boyd {
861e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
862e92a4047SStephen Boyd 	int uV = 0;
863e92a4047SStephen Boyd 	int i;
864e92a4047SStephen Boyd 
865e92a4047SStephen Boyd 	if (selector >= vreg->set_points->n_voltages)
866e92a4047SStephen Boyd 		return 0;
867e92a4047SStephen Boyd 
868e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
8699b2dfee3SStephen Boyd 		if (selector < vreg->set_points->range[i].n_voltages) {
870e92a4047SStephen Boyd 			uV = selector * vreg->set_points->range[i].step_uV
871e92a4047SStephen Boyd 				+ vreg->set_points->range[i].set_point_min_uV;
872e92a4047SStephen Boyd 			break;
8739b2dfee3SStephen Boyd 		}
874e92a4047SStephen Boyd 
875e92a4047SStephen Boyd 		selector -= vreg->set_points->range[i].n_voltages;
876e92a4047SStephen Boyd 	}
877e92a4047SStephen Boyd 
878e92a4047SStephen Boyd 	return uV;
879e92a4047SStephen Boyd }
880e92a4047SStephen Boyd 
881e92a4047SStephen Boyd static int
882e92a4047SStephen Boyd spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable)
883e92a4047SStephen Boyd {
884e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
885e92a4047SStephen Boyd 	u8 mask = SPMI_COMMON_MODE_BYPASS_MASK;
886e92a4047SStephen Boyd 	u8 val = 0;
887e92a4047SStephen Boyd 
888e92a4047SStephen Boyd 	if (enable)
889e92a4047SStephen Boyd 		val = mask;
890e92a4047SStephen Boyd 
891e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
892e92a4047SStephen Boyd }
893e92a4047SStephen Boyd 
894e92a4047SStephen Boyd static int
895e92a4047SStephen Boyd spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable)
896e92a4047SStephen Boyd {
897e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
898e92a4047SStephen Boyd 	u8 val;
899e92a4047SStephen Boyd 	int ret;
900e92a4047SStephen Boyd 
901e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1);
902e92a4047SStephen Boyd 	*enable = val & SPMI_COMMON_MODE_BYPASS_MASK;
903e92a4047SStephen Boyd 
904e92a4047SStephen Boyd 	return ret;
905e92a4047SStephen Boyd }
906e92a4047SStephen Boyd 
907e92a4047SStephen Boyd static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev)
908e92a4047SStephen Boyd {
909e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
910e92a4047SStephen Boyd 	u8 reg;
911e92a4047SStephen Boyd 
912e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
913e92a4047SStephen Boyd 
914e92a4047SStephen Boyd 	if (reg & SPMI_COMMON_MODE_HPM_MASK)
915e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
916e92a4047SStephen Boyd 
917e2adfacdSStephen Boyd 	if (reg & SPMI_COMMON_MODE_AUTO_MASK)
918e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
919e2adfacdSStephen Boyd 
920e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
921e92a4047SStephen Boyd }
922e92a4047SStephen Boyd 
923e92a4047SStephen Boyd static int
924e92a4047SStephen Boyd spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode)
925e92a4047SStephen Boyd {
926e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
927e2adfacdSStephen Boyd 	u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
928e92a4047SStephen Boyd 	u8 val = 0;
929e92a4047SStephen Boyd 
930e92a4047SStephen Boyd 	if (mode == REGULATOR_MODE_NORMAL)
931e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_HPM_MASK;
932e2adfacdSStephen Boyd 	else if (mode == REGULATOR_MODE_FAST)
933e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_AUTO_MASK;
934e92a4047SStephen Boyd 
935e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
936e92a4047SStephen Boyd }
937e92a4047SStephen Boyd 
938e92a4047SStephen Boyd static int
939e92a4047SStephen Boyd spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA)
940e92a4047SStephen Boyd {
941e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
942e92a4047SStephen Boyd 	unsigned int mode;
943e92a4047SStephen Boyd 
944e92a4047SStephen Boyd 	if (load_uA >= vreg->hpm_min_load)
945e92a4047SStephen Boyd 		mode = REGULATOR_MODE_NORMAL;
946e92a4047SStephen Boyd 	else
947e92a4047SStephen Boyd 		mode = REGULATOR_MODE_IDLE;
948e92a4047SStephen Boyd 
949e92a4047SStephen Boyd 	return spmi_regulator_common_set_mode(rdev, mode);
950e92a4047SStephen Boyd }
951e92a4047SStephen Boyd 
952e92a4047SStephen Boyd static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev)
953e92a4047SStephen Boyd {
954e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
955e92a4047SStephen Boyd 	unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK;
956e92a4047SStephen Boyd 
957e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN,
958e92a4047SStephen Boyd 				     mask, mask);
959e92a4047SStephen Boyd }
960e92a4047SStephen Boyd 
961e92a4047SStephen Boyd static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev)
962e92a4047SStephen Boyd {
963e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
964e92a4047SStephen Boyd 	unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK;
965e92a4047SStephen Boyd 
966e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START,
967e92a4047SStephen Boyd 				     mask, mask);
968e92a4047SStephen Boyd }
969e92a4047SStephen Boyd 
970e92a4047SStephen Boyd static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA)
971e92a4047SStephen Boyd {
972e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
973e92a4047SStephen Boyd 	enum spmi_regulator_logical_type type = vreg->logical_type;
974e92a4047SStephen Boyd 	unsigned int current_reg;
975e92a4047SStephen Boyd 	u8 reg;
976e92a4047SStephen Boyd 	u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK |
977e92a4047SStephen Boyd 		  SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
978e92a4047SStephen Boyd 	int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500;
979e92a4047SStephen Boyd 
980e92a4047SStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST)
981e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_REG_CURRENT_LIMIT;
982e92a4047SStephen Boyd 	else
983e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT;
984e92a4047SStephen Boyd 
985e92a4047SStephen Boyd 	if (ilim_uA > max || ilim_uA <= 0)
986e92a4047SStephen Boyd 		return -EINVAL;
987e92a4047SStephen Boyd 
988e92a4047SStephen Boyd 	reg = (ilim_uA - 1) / 500;
989e92a4047SStephen Boyd 	reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
990e92a4047SStephen Boyd 
991e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, current_reg, reg, mask);
992e92a4047SStephen Boyd }
993e92a4047SStephen Boyd 
994e92a4047SStephen Boyd static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg)
995e92a4047SStephen Boyd {
996e92a4047SStephen Boyd 	int ret;
997e92a4047SStephen Boyd 
998e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
999e92a4047SStephen Boyd 		SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
1000e92a4047SStephen Boyd 
1001e92a4047SStephen Boyd 	vreg->vs_enable_time = ktime_get();
1002e92a4047SStephen Boyd 
1003e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
1004e92a4047SStephen Boyd 		SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
1005e92a4047SStephen Boyd 
1006e92a4047SStephen Boyd 	return ret;
1007e92a4047SStephen Boyd }
1008e92a4047SStephen Boyd 
1009e92a4047SStephen Boyd static void spmi_regulator_vs_ocp_work(struct work_struct *work)
1010e92a4047SStephen Boyd {
1011e92a4047SStephen Boyd 	struct delayed_work *dwork = to_delayed_work(work);
1012e92a4047SStephen Boyd 	struct spmi_regulator *vreg
1013e92a4047SStephen Boyd 		= container_of(dwork, struct spmi_regulator, ocp_work);
1014e92a4047SStephen Boyd 
1015e92a4047SStephen Boyd 	spmi_regulator_vs_clear_ocp(vreg);
1016e92a4047SStephen Boyd }
1017e92a4047SStephen Boyd 
1018e92a4047SStephen Boyd static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data)
1019e92a4047SStephen Boyd {
1020e92a4047SStephen Boyd 	struct spmi_regulator *vreg = data;
1021e92a4047SStephen Boyd 	ktime_t ocp_irq_time;
1022e92a4047SStephen Boyd 	s64 ocp_trigger_delay_us;
1023e92a4047SStephen Boyd 
1024e92a4047SStephen Boyd 	ocp_irq_time = ktime_get();
1025e92a4047SStephen Boyd 	ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
1026e92a4047SStephen Boyd 						vreg->vs_enable_time);
1027e92a4047SStephen Boyd 
1028e92a4047SStephen Boyd 	/*
1029e92a4047SStephen Boyd 	 * Reset the OCP count if there is a large delay between switch enable
1030e92a4047SStephen Boyd 	 * and when OCP triggers.  This is indicative of a hotplug event as
1031e92a4047SStephen Boyd 	 * opposed to a fault.
1032e92a4047SStephen Boyd 	 */
1033e92a4047SStephen Boyd 	if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US)
1034e92a4047SStephen Boyd 		vreg->ocp_count = 0;
1035e92a4047SStephen Boyd 
1036e92a4047SStephen Boyd 	/* Wait for switch output to settle back to 0 V after OCP triggered. */
1037e92a4047SStephen Boyd 	udelay(SPMI_VS_OCP_FALL_DELAY_US);
1038e92a4047SStephen Boyd 
1039e92a4047SStephen Boyd 	vreg->ocp_count++;
1040e92a4047SStephen Boyd 
1041e92a4047SStephen Boyd 	if (vreg->ocp_count == 1) {
1042e92a4047SStephen Boyd 		/* Immediately clear the over current condition. */
1043e92a4047SStephen Boyd 		spmi_regulator_vs_clear_ocp(vreg);
1044e92a4047SStephen Boyd 	} else if (vreg->ocp_count <= vreg->ocp_max_retries) {
1045e92a4047SStephen Boyd 		/* Schedule the over current clear task to run later. */
1046e92a4047SStephen Boyd 		schedule_delayed_work(&vreg->ocp_work,
1047e92a4047SStephen Boyd 			msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
1048e92a4047SStephen Boyd 	} else {
1049e92a4047SStephen Boyd 		dev_err(vreg->dev,
1050e92a4047SStephen Boyd 			"OCP triggered %d times; no further retries\n",
1051e92a4047SStephen Boyd 			vreg->ocp_count);
1052e92a4047SStephen Boyd 	}
1053e92a4047SStephen Boyd 
1054e92a4047SStephen Boyd 	return IRQ_HANDLED;
1055e92a4047SStephen Boyd }
1056e92a4047SStephen Boyd 
1057*0caecaa8SIlia Lin #define SAW3_VCTL_DATA_MASK	0xFF
1058*0caecaa8SIlia Lin #define SAW3_VCTL_CLEAR_MASK	0x700FF
1059*0caecaa8SIlia Lin #define SAW3_AVS_CTL_EN_MASK	0x1
1060*0caecaa8SIlia Lin #define SAW3_AVS_CTL_TGGL_MASK	0x8000000
1061*0caecaa8SIlia Lin #define SAW3_AVS_CTL_CLEAR_MASK	0x7efc00
1062*0caecaa8SIlia Lin 
1063*0caecaa8SIlia Lin static struct regmap *saw_regmap = NULL;
1064*0caecaa8SIlia Lin 
1065*0caecaa8SIlia Lin static void spmi_saw_set_vdd(void *data)
1066*0caecaa8SIlia Lin {
1067*0caecaa8SIlia Lin 	u32 vctl, data3, avs_ctl, pmic_sts;
1068*0caecaa8SIlia Lin 	bool avs_enabled = false;
1069*0caecaa8SIlia Lin 	unsigned long timeout;
1070*0caecaa8SIlia Lin 	u8 voltage_sel = *(u8 *)data;
1071*0caecaa8SIlia Lin 
1072*0caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_AVS_CTL, &avs_ctl);
1073*0caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_VCTL, &vctl);
1074*0caecaa8SIlia Lin 	regmap_read(saw_regmap, SAW3_SPM_PMIC_DATA_3, &data3);
1075*0caecaa8SIlia Lin 
1076*0caecaa8SIlia Lin 	/* select the band */
1077*0caecaa8SIlia Lin 	vctl &= ~SAW3_VCTL_CLEAR_MASK;
1078*0caecaa8SIlia Lin 	vctl |= (u32)voltage_sel;
1079*0caecaa8SIlia Lin 
1080*0caecaa8SIlia Lin 	data3 &= ~SAW3_VCTL_CLEAR_MASK;
1081*0caecaa8SIlia Lin 	data3 |= (u32)voltage_sel;
1082*0caecaa8SIlia Lin 
1083*0caecaa8SIlia Lin 	/* If AVS is enabled, switch it off during the voltage change */
1084*0caecaa8SIlia Lin 	avs_enabled = SAW3_AVS_CTL_EN_MASK & avs_ctl;
1085*0caecaa8SIlia Lin 	if (avs_enabled) {
1086*0caecaa8SIlia Lin 		avs_ctl &= ~SAW3_AVS_CTL_TGGL_MASK;
1087*0caecaa8SIlia Lin 		regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
1088*0caecaa8SIlia Lin 	}
1089*0caecaa8SIlia Lin 
1090*0caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_RST, 1);
1091*0caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_VCTL, vctl);
1092*0caecaa8SIlia Lin 	regmap_write(saw_regmap, SAW3_SPM_PMIC_DATA_3, data3);
1093*0caecaa8SIlia Lin 
1094*0caecaa8SIlia Lin 	timeout = jiffies + usecs_to_jiffies(100);
1095*0caecaa8SIlia Lin 	do {
1096*0caecaa8SIlia Lin 		regmap_read(saw_regmap, SAW3_PMIC_STS, &pmic_sts);
1097*0caecaa8SIlia Lin 		pmic_sts &= SAW3_VCTL_DATA_MASK;
1098*0caecaa8SIlia Lin 		if (pmic_sts == (u32)voltage_sel)
1099*0caecaa8SIlia Lin 			break;
1100*0caecaa8SIlia Lin 
1101*0caecaa8SIlia Lin 		cpu_relax();
1102*0caecaa8SIlia Lin 
1103*0caecaa8SIlia Lin 	} while (time_before(jiffies, timeout));
1104*0caecaa8SIlia Lin 
1105*0caecaa8SIlia Lin 	/* After successful voltage change, switch the AVS back on */
1106*0caecaa8SIlia Lin 	if (avs_enabled) {
1107*0caecaa8SIlia Lin 		pmic_sts &= 0x3f;
1108*0caecaa8SIlia Lin 		avs_ctl &= ~SAW3_AVS_CTL_CLEAR_MASK;
1109*0caecaa8SIlia Lin 		avs_ctl |= ((pmic_sts - 4) << 10);
1110*0caecaa8SIlia Lin 		avs_ctl |= (pmic_sts << 17);
1111*0caecaa8SIlia Lin 		avs_ctl |= SAW3_AVS_CTL_TGGL_MASK;
1112*0caecaa8SIlia Lin 		regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
1113*0caecaa8SIlia Lin 	}
1114*0caecaa8SIlia Lin }
1115*0caecaa8SIlia Lin 
1116*0caecaa8SIlia Lin static int
1117*0caecaa8SIlia Lin spmi_regulator_saw_set_voltage(struct regulator_dev *rdev, unsigned selector)
1118*0caecaa8SIlia Lin {
1119*0caecaa8SIlia Lin 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
1120*0caecaa8SIlia Lin 	int ret;
1121*0caecaa8SIlia Lin 	u8 range_sel, voltage_sel;
1122*0caecaa8SIlia Lin 
1123*0caecaa8SIlia Lin 	ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
1124*0caecaa8SIlia Lin 	if (ret)
1125*0caecaa8SIlia Lin 		return ret;
1126*0caecaa8SIlia Lin 
1127*0caecaa8SIlia Lin 	if (0 != range_sel) {
1128*0caecaa8SIlia Lin 		dev_dbg(&rdev->dev, "range_sel = %02X voltage_sel = %02X", \
1129*0caecaa8SIlia Lin 			range_sel, voltage_sel);
1130*0caecaa8SIlia Lin 		return -EINVAL;
1131*0caecaa8SIlia Lin 	}
1132*0caecaa8SIlia Lin 
1133*0caecaa8SIlia Lin 	/* Always do the SAW register writes on the first CPU */
1134*0caecaa8SIlia Lin 	return smp_call_function_single(0, spmi_saw_set_vdd, \
1135*0caecaa8SIlia Lin 					&voltage_sel, true);
1136*0caecaa8SIlia Lin }
1137*0caecaa8SIlia Lin 
1138*0caecaa8SIlia Lin static struct regulator_ops spmi_saw_ops = {};
1139*0caecaa8SIlia Lin 
1140e92a4047SStephen Boyd static struct regulator_ops spmi_smps_ops = {
11419d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11429d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11439d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11441b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11452cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
11461b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11471b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1148e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1149e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1150e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1151e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1152e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1153e92a4047SStephen Boyd };
1154e92a4047SStephen Boyd 
1155e92a4047SStephen Boyd static struct regulator_ops spmi_ldo_ops = {
11569d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11579d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11589d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11591b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11601b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11611b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1162e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1163e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1164e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1165e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1166e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1167e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1168e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1169e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1170e92a4047SStephen Boyd };
1171e92a4047SStephen Boyd 
1172e92a4047SStephen Boyd static struct regulator_ops spmi_ln_ldo_ops = {
11739d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11749d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11759d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11761b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
11771b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
11781b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1179e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1180e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1181e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1182e92a4047SStephen Boyd };
1183e92a4047SStephen Boyd 
1184e92a4047SStephen Boyd static struct regulator_ops spmi_vs_ops = {
1185e92a4047SStephen Boyd 	.enable			= spmi_regulator_vs_enable,
11869d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11879d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
1188e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1189e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1190e2adfacdSStephen Boyd 	.set_over_current_protection = spmi_regulator_vs_ocp,
1191919163f6SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1192919163f6SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1193e92a4047SStephen Boyd };
1194e92a4047SStephen Boyd 
1195e92a4047SStephen Boyd static struct regulator_ops spmi_boost_ops = {
11969d485332SAxel Lin 	.enable			= regulator_enable_regmap,
11979d485332SAxel Lin 	.disable		= regulator_disable_regmap,
11989d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
11991b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
12001b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
12011b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1202e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1203e92a4047SStephen Boyd 	.set_input_current_limit = spmi_regulator_set_ilim,
1204e92a4047SStephen Boyd };
1205e92a4047SStephen Boyd 
1206e92a4047SStephen Boyd static struct regulator_ops spmi_ftsmps_ops = {
12079d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12089d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12099d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12101b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_common_set_voltage,
1211e92a4047SStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12121b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_common_get_voltage,
12131b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_common_map_voltage,
1214e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1215e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1216e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1217e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1218e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1219e92a4047SStephen Boyd };
1220e92a4047SStephen Boyd 
1221e92a4047SStephen Boyd static struct regulator_ops spmi_ult_lo_smps_ops = {
12229d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12239d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12249d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12251b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_ult_lo_smps_set_voltage,
12262cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12271b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_ult_lo_smps_get_voltage,
1228e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1229e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1230e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1231e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1232e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1233e92a4047SStephen Boyd };
1234e92a4047SStephen Boyd 
1235e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ho_smps_ops = {
12369d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12379d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12389d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12391b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
12402cf7b99cSStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
12411b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
12421b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1243e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1244e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1245e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1246e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1247e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1248e92a4047SStephen Boyd };
1249e92a4047SStephen Boyd 
1250e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ldo_ops = {
12519d485332SAxel Lin 	.enable			= regulator_enable_regmap,
12529d485332SAxel Lin 	.disable		= regulator_disable_regmap,
12539d485332SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
12541b5b1968SStephen Boyd 	.set_voltage_sel	= spmi_regulator_single_range_set_voltage,
12551b5b1968SStephen Boyd 	.get_voltage_sel	= spmi_regulator_single_range_get_voltage,
12561b5b1968SStephen Boyd 	.map_voltage		= spmi_regulator_single_map_voltage,
1257e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1258e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1259e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1260e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1261e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1262e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1263e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1264e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1265e92a4047SStephen Boyd };
1266e92a4047SStephen Boyd 
1267e92a4047SStephen Boyd /* Maximum possible digital major revision value */
1268e92a4047SStephen Boyd #define INF 0xFF
1269e92a4047SStephen Boyd 
1270e92a4047SStephen Boyd static const struct spmi_regulator_mapping supported_regulators[] = {
1271e92a4047SStephen Boyd 	/*           type subtype dig_min dig_max ltype ops setpoints hpm_min */
1272e92a4047SStephen Boyd 	SPMI_VREG(BUCK,  GP_CTL,   0, INF, SMPS,   smps,   smps,   100000),
1273e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N300,     0, INF, LDO,    ldo,    nldo1,   10000),
1274e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     0,   0, LDO,    ldo,    nldo2,   10000),
1275e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    0,   0, LDO,    ldo,    nldo2,   10000),
1276e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     1, INF, LDO,    ldo,    nldo3,   10000),
1277e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    1, INF, LDO,    ldo,    nldo3,   10000),
1278e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  0,   0, LDO,    ldo,    nldo2,   10000),
1279e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 0,   0, LDO,    ldo,    nldo2,   10000),
1280e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  1, INF, LDO,    ldo,    nldo3,   10000),
1281e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 1, INF, LDO,    ldo,    nldo3,   10000),
1282e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P50,      0, INF, LDO,    ldo,    pldo,     5000),
1283e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P150,     0, INF, LDO,    ldo,    pldo,    10000),
1284e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P300,     0, INF, LDO,    ldo,    pldo,    10000),
1285e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P600,     0, INF, LDO,    ldo,    pldo,    10000),
1286e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P1200,    0, INF, LDO,    ldo,    pldo,    10000),
1287e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LN,       0, INF, LN_LDO, ln_ldo, ln_ldo,      0),
1288e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P50,   0, INF, LDO,    ldo,    pldo,     5000),
1289e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P150,  0, INF, LDO,    ldo,    pldo,    10000),
1290e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P300,  0, INF, LDO,    ldo,    pldo,    10000),
1291e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P600,  0, INF, LDO,    ldo,    pldo,    10000),
1292e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P1200, 0, INF, LDO,    ldo,    pldo,    10000),
1293e92a4047SStephen Boyd 	SPMI_VREG_VS(LV100,        0, INF),
1294e92a4047SStephen Boyd 	SPMI_VREG_VS(LV300,        0, INF),
1295e92a4047SStephen Boyd 	SPMI_VREG_VS(MV300,        0, INF),
1296e92a4047SStephen Boyd 	SPMI_VREG_VS(MV500,        0, INF),
1297e92a4047SStephen Boyd 	SPMI_VREG_VS(HDMI,         0, INF),
1298e92a4047SStephen Boyd 	SPMI_VREG_VS(OTG,          0, INF),
1299e92a4047SStephen Boyd 	SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST,  boost,  boost,       0),
1300e92a4047SStephen Boyd 	SPMI_VREG(FTS,   FTS_CTL,  0, INF, FTSMPS, ftsmps, ftsmps, 100000),
1301e92a4047SStephen Boyd 	SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000),
1302e92a4047SStephen Boyd 	SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
1303e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1304e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1305e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1306e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1307e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1308e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1309e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
1310e92a4047SStephen Boyd 						ult_ho_smps,   100000),
1311e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1312e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1313e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1314e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1315e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P150,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1316e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P300,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1317e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P450,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1318e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P600,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1319e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P150,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1320e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P50,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000),
1321e92a4047SStephen Boyd };
1322e92a4047SStephen Boyd 
1323e92a4047SStephen Boyd static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points)
1324e92a4047SStephen Boyd {
1325e92a4047SStephen Boyd 	unsigned int n;
1326e92a4047SStephen Boyd 	struct spmi_voltage_range *range = points->range;
1327e92a4047SStephen Boyd 
1328e92a4047SStephen Boyd 	for (; range < points->range + points->count; range++) {
1329e92a4047SStephen Boyd 		n = 0;
1330e92a4047SStephen Boyd 		if (range->set_point_max_uV) {
1331e92a4047SStephen Boyd 			n = range->set_point_max_uV - range->set_point_min_uV;
1332419d06a1SAxel Lin 			n = (n / range->step_uV) + 1;
1333e92a4047SStephen Boyd 		}
1334e92a4047SStephen Boyd 		range->n_voltages = n;
1335e92a4047SStephen Boyd 		points->n_voltages += n;
1336e92a4047SStephen Boyd 	}
1337e92a4047SStephen Boyd }
1338e92a4047SStephen Boyd 
1339e92a4047SStephen Boyd static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)
1340e92a4047SStephen Boyd {
1341e92a4047SStephen Boyd 	const struct spmi_regulator_mapping *mapping;
1342e92a4047SStephen Boyd 	int ret, i;
1343e92a4047SStephen Boyd 	u32 dig_major_rev;
1344e92a4047SStephen Boyd 	u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1];
1345e92a4047SStephen Boyd 	u8 type, subtype;
1346e92a4047SStephen Boyd 
1347e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version,
1348e92a4047SStephen Boyd 		ARRAY_SIZE(version));
1349e92a4047SStephen Boyd 	if (ret) {
13506ee5c044SStephen Boyd 		dev_dbg(vreg->dev, "could not read version registers\n");
1351e92a4047SStephen Boyd 		return ret;
1352e92a4047SStephen Boyd 	}
1353e92a4047SStephen Boyd 	dig_major_rev	= version[SPMI_COMMON_REG_DIG_MAJOR_REV
1354e92a4047SStephen Boyd 					- SPMI_COMMON_REG_DIG_MAJOR_REV];
1355*0caecaa8SIlia Lin 
1356e92a4047SStephen Boyd 	if (!force_type) {
1357e92a4047SStephen Boyd 		type		= version[SPMI_COMMON_REG_TYPE -
1358e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1359e92a4047SStephen Boyd 		subtype		= version[SPMI_COMMON_REG_SUBTYPE -
1360e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1361e92a4047SStephen Boyd 	} else {
1362e92a4047SStephen Boyd 		type = force_type >> 8;
1363e92a4047SStephen Boyd 		subtype = force_type;
1364e92a4047SStephen Boyd 	}
1365e92a4047SStephen Boyd 
1366e92a4047SStephen Boyd 	for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1367e92a4047SStephen Boyd 		mapping = &supported_regulators[i];
1368e92a4047SStephen Boyd 		if (mapping->type == type && mapping->subtype == subtype
1369e92a4047SStephen Boyd 		    && mapping->revision_min <= dig_major_rev
1370e92a4047SStephen Boyd 		    && mapping->revision_max >= dig_major_rev)
1371e92a4047SStephen Boyd 			goto found;
1372e92a4047SStephen Boyd 	}
1373e92a4047SStephen Boyd 
1374e92a4047SStephen Boyd 	dev_err(vreg->dev,
1375e92a4047SStephen Boyd 		"unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1376e92a4047SStephen Boyd 		vreg->desc.name, type, subtype, dig_major_rev);
1377e92a4047SStephen Boyd 
1378e92a4047SStephen Boyd 	return -ENODEV;
1379e92a4047SStephen Boyd 
1380e92a4047SStephen Boyd found:
1381e92a4047SStephen Boyd 	vreg->logical_type	= mapping->logical_type;
1382e92a4047SStephen Boyd 	vreg->set_points	= mapping->set_points;
1383e92a4047SStephen Boyd 	vreg->hpm_min_load	= mapping->hpm_min_load;
1384e92a4047SStephen Boyd 	vreg->desc.ops		= mapping->ops;
1385e92a4047SStephen Boyd 
1386e92a4047SStephen Boyd 	if (mapping->set_points) {
1387e92a4047SStephen Boyd 		if (!mapping->set_points->n_voltages)
1388e92a4047SStephen Boyd 			spmi_calculate_num_voltages(mapping->set_points);
1389e92a4047SStephen Boyd 		vreg->desc.n_voltages = mapping->set_points->n_voltages;
1390e92a4047SStephen Boyd 	}
1391e92a4047SStephen Boyd 
1392e92a4047SStephen Boyd 	return 0;
1393e92a4047SStephen Boyd }
1394e92a4047SStephen Boyd 
13952cf7b99cSStephen Boyd static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg)
1396e92a4047SStephen Boyd {
1397e92a4047SStephen Boyd 	int ret;
1398e92a4047SStephen Boyd 	u8 reg = 0;
13992cf7b99cSStephen Boyd 	int step, delay, slew_rate, step_delay;
1400e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
1401e92a4047SStephen Boyd 
1402e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
1403e92a4047SStephen Boyd 	if (ret) {
1404e92a4047SStephen Boyd 		dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
1405e92a4047SStephen Boyd 		return ret;
1406e92a4047SStephen Boyd 	}
1407e92a4047SStephen Boyd 
1408e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
1409e92a4047SStephen Boyd 	if (!range)
1410e92a4047SStephen Boyd 		return -EINVAL;
1411e92a4047SStephen Boyd 
14122cf7b99cSStephen Boyd 	switch (vreg->logical_type) {
14132cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
14142cf7b99cSStephen Boyd 		step_delay = SPMI_FTSMPS_STEP_DELAY;
14152cf7b99cSStephen Boyd 		break;
14162cf7b99cSStephen Boyd 	default:
14172cf7b99cSStephen Boyd 		step_delay = SPMI_DEFAULT_STEP_DELAY;
14182cf7b99cSStephen Boyd 		break;
14192cf7b99cSStephen Boyd 	}
14202cf7b99cSStephen Boyd 
1421e92a4047SStephen Boyd 	step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK;
1422e92a4047SStephen Boyd 	step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT;
1423e92a4047SStephen Boyd 
1424e92a4047SStephen Boyd 	delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK;
1425e92a4047SStephen Boyd 	delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT;
1426e92a4047SStephen Boyd 
1427e92a4047SStephen Boyd 	/* slew_rate has units of uV/us */
1428e92a4047SStephen Boyd 	slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
14292cf7b99cSStephen Boyd 	slew_rate /= 1000 * (step_delay << delay);
1430e92a4047SStephen Boyd 	slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM;
1431e92a4047SStephen Boyd 	slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN;
1432e92a4047SStephen Boyd 
1433e92a4047SStephen Boyd 	/* Ensure that the slew rate is greater than 0 */
1434e92a4047SStephen Boyd 	vreg->slew_rate = max(slew_rate, 1);
1435e92a4047SStephen Boyd 
1436e92a4047SStephen Boyd 	return ret;
1437e92a4047SStephen Boyd }
1438e92a4047SStephen Boyd 
1439e2adfacdSStephen Boyd static int spmi_regulator_init_registers(struct spmi_regulator *vreg,
1440e2adfacdSStephen Boyd 				const struct spmi_regulator_init_data *data)
1441e2adfacdSStephen Boyd {
1442e2adfacdSStephen Boyd 	int ret;
1443e2adfacdSStephen Boyd 	enum spmi_regulator_logical_type type;
1444e2adfacdSStephen Boyd 	u8 ctrl_reg[8], reg, mask;
1445e2adfacdSStephen Boyd 
1446e2adfacdSStephen Boyd 	type = vreg->logical_type;
1447e2adfacdSStephen Boyd 
1448e2adfacdSStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1449e2adfacdSStephen Boyd 	if (ret)
1450e2adfacdSStephen Boyd 		return ret;
1451e2adfacdSStephen Boyd 
1452e2adfacdSStephen Boyd 	/* Set up enable pin control. */
1453e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1454e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO
1455e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_VS)
1456e2adfacdSStephen Boyd 	    && !(data->pin_ctrl_enable
1457e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1458e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] &=
1459e2adfacdSStephen Boyd 			~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1460e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] |=
1461e2adfacdSStephen Boyd 		    data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1462e2adfacdSStephen Boyd 	}
1463e2adfacdSStephen Boyd 
1464e2adfacdSStephen Boyd 	/* Set up mode pin control. */
1465e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1466e2adfacdSStephen Boyd 	    || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO)
1467e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1468e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1469e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1470e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1471e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1472e2adfacdSStephen Boyd 			data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1473e2adfacdSStephen Boyd 	}
1474e2adfacdSStephen Boyd 
1475e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS
1476e2adfacdSStephen Boyd 	   && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1477e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1478e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1479e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1480e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1481e2adfacdSStephen Boyd 	}
1482e2adfacdSStephen Boyd 
1483e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
1484e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
1485e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO)
1486e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1487e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1488e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1489e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1490e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1491e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1492e2adfacdSStephen Boyd 	}
1493e2adfacdSStephen Boyd 
1494e2adfacdSStephen Boyd 	/* Write back any control register values that were modified. */
1495e2adfacdSStephen Boyd 	ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1496e2adfacdSStephen Boyd 	if (ret)
1497e2adfacdSStephen Boyd 		return ret;
1498e2adfacdSStephen Boyd 
1499e2adfacdSStephen Boyd 	/* Set soft start strength and over current protection for VS. */
1500e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) {
1501e2adfacdSStephen Boyd 		if (data->vs_soft_start_strength
1502e2adfacdSStephen Boyd 				!= SPMI_VS_SOFT_START_STR_HW_DEFAULT) {
1503e2adfacdSStephen Boyd 			reg = data->vs_soft_start_strength
1504e2adfacdSStephen Boyd 				& SPMI_VS_SOFT_START_SEL_MASK;
1505e2adfacdSStephen Boyd 			mask = SPMI_VS_SOFT_START_SEL_MASK;
1506e2adfacdSStephen Boyd 			return spmi_vreg_update_bits(vreg,
1507e2adfacdSStephen Boyd 						     SPMI_VS_REG_SOFT_START,
1508e2adfacdSStephen Boyd 						     reg, mask);
1509e2adfacdSStephen Boyd 		}
1510e2adfacdSStephen Boyd 	}
1511e2adfacdSStephen Boyd 
1512e2adfacdSStephen Boyd 	return 0;
1513e2adfacdSStephen Boyd }
1514e2adfacdSStephen Boyd 
1515e2adfacdSStephen Boyd static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg,
1516e2adfacdSStephen Boyd 		struct device_node *node, struct spmi_regulator_init_data *data)
1517e2adfacdSStephen Boyd {
1518e2adfacdSStephen Boyd 	/*
1519e2adfacdSStephen Boyd 	 * Initialize configuration parameters to use hardware default in case
1520e2adfacdSStephen Boyd 	 * no value is specified via device tree.
1521e2adfacdSStephen Boyd 	 */
1522e2adfacdSStephen Boyd 	data->pin_ctrl_enable	    = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1523e2adfacdSStephen Boyd 	data->pin_ctrl_hpm	    = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1524e2adfacdSStephen Boyd 	data->vs_soft_start_strength	= SPMI_VS_SOFT_START_STR_HW_DEFAULT;
1525e2adfacdSStephen Boyd 
1526e2adfacdSStephen Boyd 	/* These bindings are optional, so it is okay if they aren't found. */
1527e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-max-retries",
1528e2adfacdSStephen Boyd 		&vreg->ocp_max_retries);
1529e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-retry-delay",
1530e2adfacdSStephen Boyd 		&vreg->ocp_retry_delay_ms);
1531e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-enable",
1532e2adfacdSStephen Boyd 		&data->pin_ctrl_enable);
1533e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm);
1534e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,vs-soft-start-strength",
1535e2adfacdSStephen Boyd 		&data->vs_soft_start_strength);
1536e2adfacdSStephen Boyd }
1537e2adfacdSStephen Boyd 
1538e92a4047SStephen Boyd static unsigned int spmi_regulator_of_map_mode(unsigned int mode)
1539e92a4047SStephen Boyd {
1540e2adfacdSStephen Boyd 	if (mode == 1)
1541e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
1542e2adfacdSStephen Boyd 	if (mode == 2)
1543e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
1544e92a4047SStephen Boyd 
1545e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
1546e92a4047SStephen Boyd }
1547e92a4047SStephen Boyd 
1548e92a4047SStephen Boyd static int spmi_regulator_of_parse(struct device_node *node,
1549e92a4047SStephen Boyd 			    const struct regulator_desc *desc,
1550e92a4047SStephen Boyd 			    struct regulator_config *config)
1551e92a4047SStephen Boyd {
1552e2adfacdSStephen Boyd 	struct spmi_regulator_init_data data = { };
1553e92a4047SStephen Boyd 	struct spmi_regulator *vreg = config->driver_data;
1554e92a4047SStephen Boyd 	struct device *dev = config->dev;
1555e92a4047SStephen Boyd 	int ret;
1556e92a4047SStephen Boyd 
1557e2adfacdSStephen Boyd 	spmi_regulator_get_dt_config(vreg, node, &data);
1558e2adfacdSStephen Boyd 
1559e2adfacdSStephen Boyd 	if (!vreg->ocp_max_retries)
1560e92a4047SStephen Boyd 		vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES;
1561e2adfacdSStephen Boyd 	if (!vreg->ocp_retry_delay_ms)
1562e92a4047SStephen Boyd 		vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS;
1563e92a4047SStephen Boyd 
1564e2adfacdSStephen Boyd 	ret = spmi_regulator_init_registers(vreg, &data);
1565e2adfacdSStephen Boyd 	if (ret) {
1566e2adfacdSStephen Boyd 		dev_err(dev, "common initialization failed, ret=%d\n", ret);
1567e2adfacdSStephen Boyd 		return ret;
1568e2adfacdSStephen Boyd 	}
1569e2adfacdSStephen Boyd 
15702cf7b99cSStephen Boyd 	switch (vreg->logical_type) {
15712cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
15722cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS:
15732cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS:
15742cf7b99cSStephen Boyd 	case SPMI_REGULATOR_LOGICAL_TYPE_SMPS:
15752cf7b99cSStephen Boyd 		ret = spmi_regulator_init_slew_rate(vreg);
1576e92a4047SStephen Boyd 		if (ret)
1577e92a4047SStephen Boyd 			return ret;
15782cf7b99cSStephen Boyd 	default:
15792cf7b99cSStephen Boyd 		break;
1580e92a4047SStephen Boyd 	}
1581e92a4047SStephen Boyd 
1582e92a4047SStephen Boyd 	if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS)
1583e92a4047SStephen Boyd 		vreg->ocp_irq = 0;
1584e92a4047SStephen Boyd 
1585e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
1586e92a4047SStephen Boyd 		ret = devm_request_irq(dev, vreg->ocp_irq,
1587e92a4047SStephen Boyd 			spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1588e92a4047SStephen Boyd 			vreg);
1589e92a4047SStephen Boyd 		if (ret < 0) {
1590e92a4047SStephen Boyd 			dev_err(dev, "failed to request irq %d, ret=%d\n",
1591e92a4047SStephen Boyd 				vreg->ocp_irq, ret);
1592e92a4047SStephen Boyd 			return ret;
1593e92a4047SStephen Boyd 		}
1594e92a4047SStephen Boyd 
1595e92a4047SStephen Boyd 		INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work);
1596e92a4047SStephen Boyd 	}
1597e92a4047SStephen Boyd 
1598e92a4047SStephen Boyd 	return 0;
1599e92a4047SStephen Boyd }
1600e92a4047SStephen Boyd 
1601e92a4047SStephen Boyd static const struct spmi_regulator_data pm8941_regulators[] = {
1602e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1603e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1604e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1605c333dfe8SStephen Boyd 	{ "s4", 0xa000, },
1606e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1607e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1608e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1609e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l11", },
1610e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 },
1611e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1612e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 },
1613e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l18_19", },
1614e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1615e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1616e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l4_l11", },
1617e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1618e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1619e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1620e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1621e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1622e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1623e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l8_l16_l18_19", },
1624e92a4047SStephen Boyd 	{ "l19", 0x5200, "vdd_l8_l16_l18_19", },
1625e92a4047SStephen Boyd 	{ "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1626e92a4047SStephen Boyd 	{ "l21", 0x5400, "vdd_l21", },
1627e92a4047SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1628e92a4047SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1629e92a4047SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1630e92a4047SStephen Boyd 	{ "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1631e92a4047SStephen Boyd 	{ "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1632e92a4047SStephen Boyd 	{ "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
163393bfe79bSStephen Boyd 	{ "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", },
163493bfe79bSStephen Boyd 	{ "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", },
1635e92a4047SStephen Boyd 	{ }
1636e92a4047SStephen Boyd };
1637e92a4047SStephen Boyd 
1638e92a4047SStephen Boyd static const struct spmi_regulator_data pm8841_regulators[] = {
1639e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1640e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", NULL, 0x1c08 },
1641e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1642e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 },
1643e92a4047SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", NULL, 0x1c08 },
1644e92a4047SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", NULL, 0x1c08 },
1645e92a4047SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", NULL, 0x1c08 },
1646e92a4047SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", NULL, 0x1c08 },
1647e92a4047SStephen Boyd 	{ }
1648e92a4047SStephen Boyd };
1649e92a4047SStephen Boyd 
1650e92a4047SStephen Boyd static const struct spmi_regulator_data pm8916_regulators[] = {
1651e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1652e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1653e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1654e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
1655e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1656e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2", },
1657e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1658e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l5_l6", },
1659e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l4_l5_l6", },
1660e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l4_l5_l6", },
1661e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l7", },
1662e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1663e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1664e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1665e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1666e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1667e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1668e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1669e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1670e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1671e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1672e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1673e92a4047SStephen Boyd 	{ }
1674e92a4047SStephen Boyd };
1675e92a4047SStephen Boyd 
167650314e55SStephen Boyd static const struct spmi_regulator_data pm8994_regulators[] = {
167750314e55SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
167850314e55SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
167950314e55SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
168050314e55SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
168150314e55SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", },
168250314e55SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", },
168350314e55SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", },
168450314e55SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", },
168550314e55SStephen Boyd 	{ "s9", 0x2c00, "vdd_s9", },
168650314e55SStephen Boyd 	{ "s10", 0x2f00, "vdd_s10", },
168750314e55SStephen Boyd 	{ "s11", 0x3200, "vdd_s11", },
168850314e55SStephen Boyd 	{ "s12", 0x3500, "vdd_s12", },
168950314e55SStephen Boyd 	{ "l1", 0x4000, "vdd_l1", },
169050314e55SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_l26_l28", },
169150314e55SStephen Boyd 	{ "l3", 0x4200, "vdd_l3_l11", },
169250314e55SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l27_l31", },
169350314e55SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", },
169450314e55SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l32", },
169550314e55SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", },
169650314e55SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l30", },
169750314e55SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l18_l22", },
169850314e55SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l18_l22", },
169950314e55SStephen Boyd 	{ "l11", 0x4a00, "vdd_l3_l11", },
170050314e55SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l32", },
170150314e55SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
170250314e55SStephen Boyd 	{ "l14", 0x4d00, "vdd_l14_l15", },
170350314e55SStephen Boyd 	{ "l15", 0x4e00, "vdd_l14_l15", },
170450314e55SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l30", },
170550314e55SStephen Boyd 	{ "l17", 0x5000, "vdd_l17_l29", },
170650314e55SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l18_l22", },
170750314e55SStephen Boyd 	{ "l19", 0x5200, "vdd_l13_l19_l23_l24", },
170850314e55SStephen Boyd 	{ "l20", 0x5300, "vdd_l20_l21", },
170950314e55SStephen Boyd 	{ "l21", 0x5400, "vdd_l20_l21", },
171050314e55SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l18_l22", },
171150314e55SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l19_l23_l24", },
171250314e55SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l19_l23_l24", },
171350314e55SStephen Boyd 	{ "l25", 0x5800, "vdd_l25", },
171450314e55SStephen Boyd 	{ "l26", 0x5900, "vdd_l2_l26_l28", },
171550314e55SStephen Boyd 	{ "l27", 0x5a00, "vdd_l4_l27_l31", },
171650314e55SStephen Boyd 	{ "l28", 0x5b00, "vdd_l2_l26_l28", },
171750314e55SStephen Boyd 	{ "l29", 0x5c00, "vdd_l17_l29", },
171850314e55SStephen Boyd 	{ "l30", 0x5d00, "vdd_l8_l16_l30", },
171950314e55SStephen Boyd 	{ "l31", 0x5e00, "vdd_l4_l27_l31", },
172050314e55SStephen Boyd 	{ "l32", 0x5f00, "vdd_l6_l12_l32", },
172150314e55SStephen Boyd 	{ "lvs1", 0x8000, "vdd_lvs_1_2", },
172250314e55SStephen Boyd 	{ "lvs2", 0x8100, "vdd_lvs_1_2", },
172350314e55SStephen Boyd 	{ }
172450314e55SStephen Boyd };
172550314e55SStephen Boyd 
1726ca5cd8c9SRajendra Nayak static const struct spmi_regulator_data pmi8994_regulators[] = {
1727ca5cd8c9SRajendra Nayak 	{ "s1", 0x1400, "vdd_s1", },
1728ca5cd8c9SRajendra Nayak 	{ "s2", 0x1700, "vdd_s2", },
1729ca5cd8c9SRajendra Nayak 	{ "s3", 0x1a00, "vdd_s3", },
1730ca5cd8c9SRajendra Nayak 	{ "l1", 0x4000, "vdd_l1", },
1731ca5cd8c9SRajendra Nayak         { }
1732ca5cd8c9SRajendra Nayak };
1733ca5cd8c9SRajendra Nayak 
1734e92a4047SStephen Boyd static const struct of_device_id qcom_spmi_regulator_match[] = {
1735e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators },
1736e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators },
1737e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators },
173850314e55SStephen Boyd 	{ .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators },
1739ca5cd8c9SRajendra Nayak 	{ .compatible = "qcom,pmi8994-regulators", .data = &pmi8994_regulators },
1740e92a4047SStephen Boyd 	{ }
1741e92a4047SStephen Boyd };
1742e92a4047SStephen Boyd MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match);
1743e92a4047SStephen Boyd 
1744e92a4047SStephen Boyd static int qcom_spmi_regulator_probe(struct platform_device *pdev)
1745e92a4047SStephen Boyd {
1746e92a4047SStephen Boyd 	const struct spmi_regulator_data *reg;
1747e92a4047SStephen Boyd 	const struct of_device_id *match;
1748e92a4047SStephen Boyd 	struct regulator_config config = { };
1749e92a4047SStephen Boyd 	struct regulator_dev *rdev;
1750e92a4047SStephen Boyd 	struct spmi_regulator *vreg;
1751e92a4047SStephen Boyd 	struct regmap *regmap;
1752e92a4047SStephen Boyd 	const char *name;
1753e92a4047SStephen Boyd 	struct device *dev = &pdev->dev;
1754*0caecaa8SIlia Lin 	struct device_node *node = pdev->dev.of_node;
1755*0caecaa8SIlia Lin 	struct device_node *syscon;
1756*0caecaa8SIlia Lin 	int ret, lenp;
1757e92a4047SStephen Boyd 	struct list_head *vreg_list;
1758e92a4047SStephen Boyd 
1759e92a4047SStephen Boyd 	vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL);
1760e92a4047SStephen Boyd 	if (!vreg_list)
1761e92a4047SStephen Boyd 		return -ENOMEM;
1762e92a4047SStephen Boyd 	INIT_LIST_HEAD(vreg_list);
1763e92a4047SStephen Boyd 	platform_set_drvdata(pdev, vreg_list);
1764e92a4047SStephen Boyd 
1765e92a4047SStephen Boyd 	regmap = dev_get_regmap(dev->parent, NULL);
1766e92a4047SStephen Boyd 	if (!regmap)
1767e92a4047SStephen Boyd 		return -ENODEV;
1768e92a4047SStephen Boyd 
1769e92a4047SStephen Boyd 	match = of_match_device(qcom_spmi_regulator_match, &pdev->dev);
1770e92a4047SStephen Boyd 	if (!match)
1771e92a4047SStephen Boyd 		return -ENODEV;
1772e92a4047SStephen Boyd 
1773*0caecaa8SIlia Lin 	if (of_find_property(node, "qcom,saw-reg", &lenp)) {
1774*0caecaa8SIlia Lin 		syscon = of_parse_phandle(node, "qcom,saw-reg", 0);
1775*0caecaa8SIlia Lin 		saw_regmap = syscon_node_to_regmap(syscon);
1776*0caecaa8SIlia Lin 		of_node_put(syscon);
1777*0caecaa8SIlia Lin 		if (IS_ERR(regmap))
1778*0caecaa8SIlia Lin 			dev_err(dev, "ERROR reading SAW regmap\n");
1779*0caecaa8SIlia Lin 	}
1780*0caecaa8SIlia Lin 
1781e92a4047SStephen Boyd 	for (reg = match->data; reg->name; reg++) {
1782*0caecaa8SIlia Lin 
1783*0caecaa8SIlia Lin 		if (saw_regmap && \
1784*0caecaa8SIlia Lin 		    of_find_property(of_find_node_by_name(node, reg->name), \
1785*0caecaa8SIlia Lin 				     "qcom,saw-slave", &lenp)) {
1786*0caecaa8SIlia Lin 			continue;
1787*0caecaa8SIlia Lin 		}
1788*0caecaa8SIlia Lin 
1789e92a4047SStephen Boyd 		vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1790e92a4047SStephen Boyd 		if (!vreg)
1791e92a4047SStephen Boyd 			return -ENOMEM;
1792e92a4047SStephen Boyd 
1793e92a4047SStephen Boyd 		vreg->dev = dev;
1794e92a4047SStephen Boyd 		vreg->base = reg->base;
1795e92a4047SStephen Boyd 		vreg->regmap = regmap;
1796e92a4047SStephen Boyd 		if (reg->ocp) {
1797e92a4047SStephen Boyd 			vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp);
1798e92a4047SStephen Boyd 			if (vreg->ocp_irq < 0) {
1799e92a4047SStephen Boyd 				ret = vreg->ocp_irq;
1800e92a4047SStephen Boyd 				goto err;
1801e92a4047SStephen Boyd 			}
1802e92a4047SStephen Boyd 		}
1803e92a4047SStephen Boyd 		vreg->desc.id = -1;
1804e92a4047SStephen Boyd 		vreg->desc.owner = THIS_MODULE;
1805e92a4047SStephen Boyd 		vreg->desc.type = REGULATOR_VOLTAGE;
18069d485332SAxel Lin 		vreg->desc.enable_reg = reg->base + SPMI_COMMON_REG_ENABLE;
18079d485332SAxel Lin 		vreg->desc.enable_mask = SPMI_COMMON_ENABLE_MASK;
18089d485332SAxel Lin 		vreg->desc.enable_val = SPMI_COMMON_ENABLE;
1809e92a4047SStephen Boyd 		vreg->desc.name = name = reg->name;
1810e92a4047SStephen Boyd 		vreg->desc.supply_name = reg->supply;
1811e92a4047SStephen Boyd 		vreg->desc.of_match = reg->name;
1812e92a4047SStephen Boyd 		vreg->desc.of_parse_cb = spmi_regulator_of_parse;
1813e92a4047SStephen Boyd 		vreg->desc.of_map_mode = spmi_regulator_of_map_mode;
1814e92a4047SStephen Boyd 
1815e92a4047SStephen Boyd 		ret = spmi_regulator_match(vreg, reg->force_type);
1816e92a4047SStephen Boyd 		if (ret)
18176ee5c044SStephen Boyd 			continue;
1818e92a4047SStephen Boyd 
1819*0caecaa8SIlia Lin 		if (saw_regmap && \
1820*0caecaa8SIlia Lin 		    of_find_property(of_find_node_by_name(node, reg->name), \
1821*0caecaa8SIlia Lin 				     "qcom,saw-leader", &lenp)) {
1822*0caecaa8SIlia Lin 			spmi_saw_ops = *(vreg->desc.ops);
1823*0caecaa8SIlia Lin 			spmi_saw_ops.set_voltage_sel = \
1824*0caecaa8SIlia Lin 				spmi_regulator_saw_set_voltage;
1825*0caecaa8SIlia Lin 			vreg->desc.ops = &spmi_saw_ops;
1826*0caecaa8SIlia Lin 		}
1827*0caecaa8SIlia 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