xref: /linux/drivers/regulator/qcom_spmi-regulator.c (revision 50314e55a140a0bc898d5f34b591e4e4ecedc75f)
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>
28e92a4047SStephen Boyd 
29e2adfacdSStephen Boyd /* Pin control enable input pins. */
30e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE		0x00
31e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0		0x01
32e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1		0x02
33e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2		0x04
34e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3		0x08
35e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT	0x10
36e2adfacdSStephen Boyd 
37e2adfacdSStephen Boyd /* Pin control high power mode input pins. */
38e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE		0x00
39e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0			0x01
40e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1			0x02
41e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2			0x04
42e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3			0x08
43e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B		0x10
44e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT		0x20
45e2adfacdSStephen Boyd 
46e2adfacdSStephen Boyd /*
47e2adfacdSStephen Boyd  * Used with enable parameters to specify that hardware default register values
48e2adfacdSStephen Boyd  * should be left unaltered.
49e2adfacdSStephen Boyd  */
50e2adfacdSStephen Boyd #define SPMI_REGULATOR_USE_HW_DEFAULT			2
51e2adfacdSStephen Boyd 
52e2adfacdSStephen Boyd /* Soft start strength of a voltage switch type regulator */
53e2adfacdSStephen Boyd enum spmi_vs_soft_start_str {
54e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P05_UA = 0,
55e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P25_UA,
56e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P55_UA,
57e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_0P75_UA,
58e2adfacdSStephen Boyd 	SPMI_VS_SOFT_START_STR_HW_DEFAULT,
59e2adfacdSStephen Boyd };
60e2adfacdSStephen Boyd 
61e2adfacdSStephen Boyd /**
62e2adfacdSStephen Boyd  * struct spmi_regulator_init_data - spmi-regulator initialization data
63e2adfacdSStephen Boyd  * @pin_ctrl_enable:        Bit mask specifying which hardware pins should be
64e2adfacdSStephen Boyd  *				used to enable the regulator, if any
65e2adfacdSStephen Boyd  *			    Value should be an ORing of
66e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants.  If
67e2adfacdSStephen Boyd  *				the bit specified by
68e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
69e2adfacdSStephen Boyd  *				set, then pin control enable hardware registers
70e2adfacdSStephen Boyd  *				will not be modified.
71e2adfacdSStephen Boyd  * @pin_ctrl_hpm:           Bit mask specifying which hardware pins should be
72e2adfacdSStephen Boyd  *				used to force the regulator into high power
73e2adfacdSStephen Boyd  *				mode, if any
74e2adfacdSStephen Boyd  *			    Value should be an ORing of
75e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_* constants.  If
76e2adfacdSStephen Boyd  *				the bit specified by
77e2adfacdSStephen Boyd  *				SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
78e2adfacdSStephen Boyd  *				set, then pin control mode hardware registers
79e2adfacdSStephen Boyd  *				will not be modified.
80e2adfacdSStephen Boyd  * @vs_soft_start_strength: This parameter sets the soft start strength for
81e2adfacdSStephen Boyd  *				voltage switch type regulators.  Its value
82e2adfacdSStephen Boyd  *				should be one of SPMI_VS_SOFT_START_STR_*.  If
83e2adfacdSStephen Boyd  *				its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
84e2adfacdSStephen Boyd  *				then the soft start strength will be left at its
85e2adfacdSStephen Boyd  *				default hardware value.
86e2adfacdSStephen Boyd  */
87e2adfacdSStephen Boyd struct spmi_regulator_init_data {
88e2adfacdSStephen Boyd 	unsigned				pin_ctrl_enable;
89e2adfacdSStephen Boyd 	unsigned				pin_ctrl_hpm;
90e2adfacdSStephen Boyd 	enum spmi_vs_soft_start_str		vs_soft_start_strength;
91e2adfacdSStephen Boyd };
92e2adfacdSStephen Boyd 
93e92a4047SStephen Boyd /* These types correspond to unique register layouts. */
94e92a4047SStephen Boyd enum spmi_regulator_logical_type {
95e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_SMPS,
96e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LDO,
97e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_VS,
98e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST,
99e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS,
100e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
101e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO,
102e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
103e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
104e92a4047SStephen Boyd 	SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO,
105e92a4047SStephen Boyd };
106e92a4047SStephen Boyd 
107e92a4047SStephen Boyd enum spmi_regulator_type {
108e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BUCK		= 0x03,
109e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_LDO			= 0x04,
110e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_VS			= 0x05,
111e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST		= 0x1b,
112e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_FTS			= 0x1c,
113e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_BOOST_BYP		= 0x1f,
114e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_LDO		= 0x21,
115e92a4047SStephen Boyd 	SPMI_REGULATOR_TYPE_ULT_BUCK		= 0x22,
116e92a4047SStephen Boyd };
117e92a4047SStephen Boyd 
118e92a4047SStephen Boyd enum spmi_regulator_subtype {
119e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_GP_CTL		= 0x08,
120e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_RF_CTL		= 0x09,
121e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N50		= 0x01,
122e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N150		= 0x02,
123e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300		= 0x03,
124e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600		= 0x04,
125e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200		= 0x05,
126e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N600_ST		= 0x06,
127e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N1200_ST		= 0x07,
128e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N900_ST		= 0x14,
129e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_N300_ST		= 0x15,
130e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P50		= 0x08,
131e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P150		= 0x09,
132e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P300		= 0x0a,
133e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P600		= 0x0b,
134e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_P1200		= 0x0c,
135e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LN		= 0x10,
136e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P50		= 0x28,
137e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P150		= 0x29,
138e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P300		= 0x2a,
139e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P600		= 0x2b,
140e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P1200		= 0x2c,
141e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV_P450		= 0x2d,
142e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV100		= 0x01,
143e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_LV300		= 0x02,
144e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV300		= 0x08,
145e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_MV500		= 0x09,
146e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_HDMI		= 0x10,
147e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_OTG		= 0x11,
148e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_5V_BOOST		= 0x01,
149e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS_CTL		= 0x08,
150e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL	= 0x09,
151e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_BB_2A		= 0x01,
152e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1	= 0x0d,
153e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2	= 0x0e,
154e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3	= 0x0f,
155e92a4047SStephen Boyd 	SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4	= 0x10,
156e92a4047SStephen Boyd };
157e92a4047SStephen Boyd 
158e92a4047SStephen Boyd enum spmi_common_regulator_registers {
159e92a4047SStephen Boyd 	SPMI_COMMON_REG_DIG_MAJOR_REV		= 0x01,
160e92a4047SStephen Boyd 	SPMI_COMMON_REG_TYPE			= 0x04,
161e92a4047SStephen Boyd 	SPMI_COMMON_REG_SUBTYPE			= 0x05,
162e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_RANGE		= 0x40,
163e92a4047SStephen Boyd 	SPMI_COMMON_REG_VOLTAGE_SET		= 0x41,
164e92a4047SStephen Boyd 	SPMI_COMMON_REG_MODE			= 0x45,
165e92a4047SStephen Boyd 	SPMI_COMMON_REG_ENABLE			= 0x46,
166e92a4047SStephen Boyd 	SPMI_COMMON_REG_PULL_DOWN		= 0x48,
167e92a4047SStephen Boyd 	SPMI_COMMON_REG_SOFT_START		= 0x4c,
168e92a4047SStephen Boyd 	SPMI_COMMON_REG_STEP_CTRL		= 0x61,
169e92a4047SStephen Boyd };
170e92a4047SStephen Boyd 
171e92a4047SStephen Boyd enum spmi_vs_registers {
172e92a4047SStephen Boyd 	SPMI_VS_REG_OCP				= 0x4a,
173e92a4047SStephen Boyd 	SPMI_VS_REG_SOFT_START			= 0x4c,
174e92a4047SStephen Boyd };
175e92a4047SStephen Boyd 
176e92a4047SStephen Boyd enum spmi_boost_registers {
177e92a4047SStephen Boyd 	SPMI_BOOST_REG_CURRENT_LIMIT		= 0x4a,
178e92a4047SStephen Boyd };
179e92a4047SStephen Boyd 
180e92a4047SStephen Boyd enum spmi_boost_byp_registers {
181e92a4047SStephen Boyd 	SPMI_BOOST_BYP_REG_CURRENT_LIMIT	= 0x4b,
182e92a4047SStephen Boyd };
183e92a4047SStephen Boyd 
184e92a4047SStephen Boyd /* Used for indexing into ctrl_reg.  These are offets from 0x40 */
185e92a4047SStephen Boyd enum spmi_common_control_register_index {
186e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_RANGE		= 0,
187e92a4047SStephen Boyd 	SPMI_COMMON_IDX_VOLTAGE_SET		= 1,
188e92a4047SStephen Boyd 	SPMI_COMMON_IDX_MODE			= 5,
189e92a4047SStephen Boyd 	SPMI_COMMON_IDX_ENABLE			= 6,
190e92a4047SStephen Boyd };
191e92a4047SStephen Boyd 
192e92a4047SStephen Boyd /* Common regulator control register layout */
193e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_MASK			0x80
194e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE			0x80
195e92a4047SStephen Boyd #define SPMI_COMMON_DISABLE			0x00
196e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK	0x08
197e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK	0x04
198e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK	0x02
199e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK	0x01
200e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK	0x0f
201e92a4047SStephen Boyd 
202e92a4047SStephen Boyd /* Common regulator mode register layout */
203e92a4047SStephen Boyd #define SPMI_COMMON_MODE_HPM_MASK		0x80
204e92a4047SStephen Boyd #define SPMI_COMMON_MODE_AUTO_MASK		0x40
205e92a4047SStephen Boyd #define SPMI_COMMON_MODE_BYPASS_MASK		0x20
206e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK	0x10
207e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK	0x08
208e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK	0x04
209e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK	0x02
210e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK	0x01
211e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK	0x1f
212e92a4047SStephen Boyd 
213e92a4047SStephen Boyd /* Common regulator pull down control register layout */
214e92a4047SStephen Boyd #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK	0x80
215e92a4047SStephen Boyd 
216e92a4047SStephen Boyd /* LDO regulator current limit control register layout */
217e92a4047SStephen Boyd #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK	0x80
218e92a4047SStephen Boyd 
219e92a4047SStephen Boyd /* LDO regulator soft start control register layout */
220e92a4047SStephen Boyd #define SPMI_LDO_SOFT_START_ENABLE_MASK		0x80
221e92a4047SStephen Boyd 
222e92a4047SStephen Boyd /* VS regulator over current protection control register layout */
223e92a4047SStephen Boyd #define SPMI_VS_OCP_OVERRIDE			0x01
224e92a4047SStephen Boyd #define SPMI_VS_OCP_NO_OVERRIDE			0x00
225e92a4047SStephen Boyd 
226e92a4047SStephen Boyd /* VS regulator soft start control register layout */
227e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_ENABLE_MASK		0x80
228e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_SEL_MASK		0x03
229e92a4047SStephen Boyd 
230e92a4047SStephen Boyd /* Boost regulator current limit control register layout */
231e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK	0x80
232e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_MASK		0x07
233e92a4047SStephen Boyd 
234e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES		10
235e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS	30
236e92a4047SStephen Boyd #define SPMI_VS_OCP_FALL_DELAY_US		90
237e92a4047SStephen Boyd #define SPMI_VS_OCP_FAULT_DELAY_US		20000
238e92a4047SStephen Boyd 
239e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK		0x18
240e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT	3
241e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK	0x07
242e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT	0
243e92a4047SStephen Boyd 
244e92a4047SStephen Boyd /* Clock rate in kHz of the FTSMPS regulator reference clock. */
245e92a4047SStephen Boyd #define SPMI_FTSMPS_CLOCK_RATE		19200
246e92a4047SStephen Boyd 
247e92a4047SStephen Boyd /* Minimum voltage stepper delay for each step. */
248e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_DELAY		8
249e92a4047SStephen Boyd 
250e92a4047SStephen Boyd /*
251e92a4047SStephen Boyd  * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
252e92a4047SStephen Boyd  * adjust the step rate in order to account for oscillator variance.
253e92a4047SStephen Boyd  */
254e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_NUM	4
255e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_DEN	5
256e92a4047SStephen Boyd 
257e92a4047SStephen Boyd /*
258e92a4047SStephen Boyd  * This voltage in uV is returned by get_voltage functions when there is no way
259e92a4047SStephen Boyd  * to determine the current voltage level.  It is needed because the regulator
260e92a4047SStephen Boyd  * framework treats a 0 uV voltage as an error.
261e92a4047SStephen Boyd  */
262e92a4047SStephen Boyd #define VOLTAGE_UNKNOWN 1
263e92a4047SStephen Boyd 
264e92a4047SStephen Boyd /* VSET value to decide the range of ULT SMPS */
265e92a4047SStephen Boyd #define ULT_SMPS_RANGE_SPLIT 0x60
266e92a4047SStephen Boyd 
267e92a4047SStephen Boyd /**
268e92a4047SStephen Boyd  * struct spmi_voltage_range - regulator set point voltage mapping description
269e92a4047SStephen Boyd  * @min_uV:		Minimum programmable output voltage resulting from
270e92a4047SStephen Boyd  *			set point register value 0x00
271e92a4047SStephen Boyd  * @max_uV:		Maximum programmable output voltage
272e92a4047SStephen Boyd  * @step_uV:		Output voltage increase resulting from the set point
273e92a4047SStephen Boyd  *			register value increasing by 1
274e92a4047SStephen Boyd  * @set_point_min_uV:	Minimum allowed voltage
275e92a4047SStephen Boyd  * @set_point_max_uV:	Maximum allowed voltage.  This may be tweaked in order
276e92a4047SStephen Boyd  *			to pick which range should be used in the case of
277e92a4047SStephen Boyd  *			overlapping set points.
278e92a4047SStephen Boyd  * @n_voltages:		Number of preferred voltage set points present in this
279e92a4047SStephen Boyd  *			range
280e92a4047SStephen Boyd  * @range_sel:		Voltage range register value corresponding to this range
281e92a4047SStephen Boyd  *
282e92a4047SStephen Boyd  * The following relationships must be true for the values used in this struct:
283e92a4047SStephen Boyd  * (max_uV - min_uV) % step_uV == 0
284e92a4047SStephen Boyd  * (set_point_min_uV - min_uV) % step_uV == 0*
285e92a4047SStephen Boyd  * (set_point_max_uV - min_uV) % step_uV == 0*
286e92a4047SStephen Boyd  * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
287e92a4047SStephen Boyd  *
288e92a4047SStephen Boyd  * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
289e92a4047SStephen Boyd  * specify that the voltage range has meaning, but is not preferred.
290e92a4047SStephen Boyd  */
291e92a4047SStephen Boyd struct spmi_voltage_range {
292e92a4047SStephen Boyd 	int					min_uV;
293e92a4047SStephen Boyd 	int					max_uV;
294e92a4047SStephen Boyd 	int					step_uV;
295e92a4047SStephen Boyd 	int					set_point_min_uV;
296e92a4047SStephen Boyd 	int					set_point_max_uV;
297e92a4047SStephen Boyd 	unsigned				n_voltages;
298e92a4047SStephen Boyd 	u8					range_sel;
299e92a4047SStephen Boyd };
300e92a4047SStephen Boyd 
301e92a4047SStephen Boyd /*
302e92a4047SStephen Boyd  * The ranges specified in the spmi_voltage_set_points struct must be listed
303e92a4047SStephen Boyd  * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
304e92a4047SStephen Boyd  */
305e92a4047SStephen Boyd struct spmi_voltage_set_points {
306e92a4047SStephen Boyd 	struct spmi_voltage_range		*range;
307e92a4047SStephen Boyd 	int					count;
308e92a4047SStephen Boyd 	unsigned				n_voltages;
309e92a4047SStephen Boyd };
310e92a4047SStephen Boyd 
311e92a4047SStephen Boyd struct spmi_regulator {
312e92a4047SStephen Boyd 	struct regulator_desc			desc;
313e92a4047SStephen Boyd 	struct device				*dev;
314e92a4047SStephen Boyd 	struct delayed_work			ocp_work;
315e92a4047SStephen Boyd 	struct regmap				*regmap;
316e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
317e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
318e92a4047SStephen Boyd 	int					ocp_irq;
319e92a4047SStephen Boyd 	int					ocp_count;
320e92a4047SStephen Boyd 	int					ocp_max_retries;
321e92a4047SStephen Boyd 	int					ocp_retry_delay_ms;
322e92a4047SStephen Boyd 	int					hpm_min_load;
323e92a4047SStephen Boyd 	int					slew_rate;
324e92a4047SStephen Boyd 	ktime_t					vs_enable_time;
325e92a4047SStephen Boyd 	u16					base;
326e92a4047SStephen Boyd 	struct list_head			node;
327e92a4047SStephen Boyd };
328e92a4047SStephen Boyd 
329e92a4047SStephen Boyd struct spmi_regulator_mapping {
330e92a4047SStephen Boyd 	enum spmi_regulator_type		type;
331e92a4047SStephen Boyd 	enum spmi_regulator_subtype		subtype;
332e92a4047SStephen Boyd 	enum spmi_regulator_logical_type	logical_type;
333e92a4047SStephen Boyd 	u32					revision_min;
334e92a4047SStephen Boyd 	u32					revision_max;
335e92a4047SStephen Boyd 	struct regulator_ops			*ops;
336e92a4047SStephen Boyd 	struct spmi_voltage_set_points		*set_points;
337e92a4047SStephen Boyd 	int					hpm_min_load;
338e92a4047SStephen Boyd };
339e92a4047SStephen Boyd 
340e92a4047SStephen Boyd struct spmi_regulator_data {
341e92a4047SStephen Boyd 	const char			*name;
342e92a4047SStephen Boyd 	u16				base;
343e92a4047SStephen Boyd 	const char			*supply;
344e92a4047SStephen Boyd 	const char			*ocp;
345e92a4047SStephen Boyd 	u16				force_type;
346e92a4047SStephen Boyd };
347e92a4047SStephen Boyd 
348e92a4047SStephen Boyd #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
349e92a4047SStephen Boyd 		      _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
350e92a4047SStephen Boyd 	{ \
351e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_##_type, \
352e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
353e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
354e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
355e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
356e92a4047SStephen Boyd 		.ops		= &spmi_##_ops_val##_ops, \
357e92a4047SStephen Boyd 		.set_points	= &_set_points_val##_set_points, \
358e92a4047SStephen Boyd 		.hpm_min_load	= _hpm_min_load, \
359e92a4047SStephen Boyd 	}
360e92a4047SStephen Boyd 
361e92a4047SStephen Boyd #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
362e92a4047SStephen Boyd 	{ \
363e92a4047SStephen Boyd 		.type		= SPMI_REGULATOR_TYPE_VS, \
364e92a4047SStephen Boyd 		.subtype	= SPMI_REGULATOR_SUBTYPE_##_subtype, \
365e92a4047SStephen Boyd 		.revision_min	= _dig_major_min, \
366e92a4047SStephen Boyd 		.revision_max	= _dig_major_max, \
367e92a4047SStephen Boyd 		.logical_type	= SPMI_REGULATOR_LOGICAL_TYPE_VS, \
368e92a4047SStephen Boyd 		.ops		= &spmi_vs_ops, \
369e92a4047SStephen Boyd 	}
370e92a4047SStephen Boyd 
371e92a4047SStephen Boyd #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
372e92a4047SStephen Boyd 			_set_point_max_uV, _max_uV, _step_uV) \
373e92a4047SStephen Boyd 	{ \
374e92a4047SStephen Boyd 		.min_uV			= _min_uV, \
375e92a4047SStephen Boyd 		.max_uV			= _max_uV, \
376e92a4047SStephen Boyd 		.set_point_min_uV	= _set_point_min_uV, \
377e92a4047SStephen Boyd 		.set_point_max_uV	= _set_point_max_uV, \
378e92a4047SStephen Boyd 		.step_uV		= _step_uV, \
379e92a4047SStephen Boyd 		.range_sel		= _range_sel, \
380e92a4047SStephen Boyd 	}
381e92a4047SStephen Boyd 
382e92a4047SStephen Boyd #define DEFINE_SPMI_SET_POINTS(name) \
383e92a4047SStephen Boyd struct spmi_voltage_set_points name##_set_points = { \
384e92a4047SStephen Boyd 	.range	= name##_ranges, \
385e92a4047SStephen Boyd 	.count	= ARRAY_SIZE(name##_ranges), \
386e92a4047SStephen Boyd }
387e92a4047SStephen Boyd 
388e92a4047SStephen Boyd /*
389e92a4047SStephen Boyd  * These tables contain the physically available PMIC regulator voltage setpoint
390e92a4047SStephen Boyd  * ranges.  Where two ranges overlap in hardware, one of the ranges is trimmed
391e92a4047SStephen Boyd  * to ensure that the setpoints available to software are monotonically
392e92a4047SStephen Boyd  * increasing and unique.  The set_voltage callback functions expect these
393e92a4047SStephen Boyd  * properties to hold.
394e92a4047SStephen Boyd  */
395e92a4047SStephen Boyd static struct spmi_voltage_range pldo_ranges[] = {
396e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
397e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
398e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
399e92a4047SStephen Boyd };
400e92a4047SStephen Boyd 
401e92a4047SStephen Boyd static struct spmi_voltage_range nldo1_ranges[] = {
402e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  750000, 1537500, 1537500, 12500),
403e92a4047SStephen Boyd };
404e92a4047SStephen Boyd 
405e92a4047SStephen Boyd static struct spmi_voltage_range nldo2_ranges[] = {
406e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,       0,       0, 1537500, 12500),
407e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,  375000,  768750,  768750,  6250),
408e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,  775000, 1537500, 1537500, 12500),
409e92a4047SStephen Boyd };
410e92a4047SStephen Boyd 
411e92a4047SStephen Boyd static struct spmi_voltage_range nldo3_ranges[] = {
412e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
413e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  375000,       0,       0, 1537500, 12500),
414e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(2,  750000,       0,       0, 1537500, 12500),
415e92a4047SStephen Boyd };
416e92a4047SStephen Boyd 
417e92a4047SStephen Boyd static struct spmi_voltage_range ln_ldo_ranges[] = {
418e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  690000,  690000, 1110000, 1110000, 60000),
419e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
420e92a4047SStephen Boyd };
421e92a4047SStephen Boyd 
422e92a4047SStephen Boyd static struct spmi_voltage_range smps_ranges[] = {
423e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
424e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
425e92a4047SStephen Boyd };
426e92a4047SStephen Boyd 
427e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps_ranges[] = {
428e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,       0,  350000, 1275000, 1275000,  5000),
429e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,       0, 1280000, 2040000, 2040000, 10000),
430e92a4047SStephen Boyd };
431e92a4047SStephen Boyd 
432e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps2p5_ranges[] = {
433e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,   80000,  350000, 1355000, 1355000,  5000),
434e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  160000, 1360000, 2200000, 2200000, 10000),
435e92a4047SStephen Boyd };
436e92a4047SStephen Boyd 
437e92a4047SStephen Boyd static struct spmi_voltage_range boost_ranges[] = {
438e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
439e92a4047SStephen Boyd };
440e92a4047SStephen Boyd 
441e92a4047SStephen Boyd static struct spmi_voltage_range boost_byp_ranges[] = {
442e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
443e92a4047SStephen Boyd };
444e92a4047SStephen Boyd 
445e92a4047SStephen Boyd static struct spmi_voltage_range ult_lo_smps_ranges[] = {
446e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1562500, 1562500, 12500),
447e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(1,  750000,       0,       0, 1525000, 25000),
448e92a4047SStephen Boyd };
449e92a4047SStephen Boyd 
450e92a4047SStephen Boyd static struct spmi_voltage_range ult_ho_smps_ranges[] = {
451e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
452e92a4047SStephen Boyd };
453e92a4047SStephen Boyd 
454e92a4047SStephen Boyd static struct spmi_voltage_range ult_nldo_ranges[] = {
455e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0,  375000,  375000, 1537500, 1537500, 12500),
456e92a4047SStephen Boyd };
457e92a4047SStephen Boyd 
458e92a4047SStephen Boyd static struct spmi_voltage_range ult_pldo_ranges[] = {
459e92a4047SStephen Boyd 	SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
460e92a4047SStephen Boyd };
461e92a4047SStephen Boyd 
462e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(pldo);
463e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo1);
464e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo2);
465e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo3);
466e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ln_ldo);
467e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(smps);
468e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps);
469e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps2p5);
470e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost);
471e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost_byp);
472e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_lo_smps);
473e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_ho_smps);
474e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_nldo);
475e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_pldo);
476e92a4047SStephen Boyd 
477e92a4047SStephen Boyd static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf,
478e92a4047SStephen Boyd 				 int len)
479e92a4047SStephen Boyd {
480e92a4047SStephen Boyd 	return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len);
481e92a4047SStephen Boyd }
482e92a4047SStephen Boyd 
483e92a4047SStephen Boyd static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr,
484e92a4047SStephen Boyd 				u8 *buf, int len)
485e92a4047SStephen Boyd {
486e92a4047SStephen Boyd 	return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len);
487e92a4047SStephen Boyd }
488e92a4047SStephen Boyd 
489e92a4047SStephen Boyd static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val,
490e92a4047SStephen Boyd 		u8 mask)
491e92a4047SStephen Boyd {
492e92a4047SStephen Boyd 	return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val);
493e92a4047SStephen Boyd }
494e92a4047SStephen Boyd 
495e92a4047SStephen Boyd static int spmi_regulator_common_is_enabled(struct regulator_dev *rdev)
496e92a4047SStephen Boyd {
497e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
498e92a4047SStephen Boyd 	u8 reg;
499e92a4047SStephen Boyd 
500e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_ENABLE, &reg, 1);
501e92a4047SStephen Boyd 
502e92a4047SStephen Boyd 	return (reg & SPMI_COMMON_ENABLE_MASK) == SPMI_COMMON_ENABLE;
503e92a4047SStephen Boyd }
504e92a4047SStephen Boyd 
505e92a4047SStephen Boyd static int spmi_regulator_common_enable(struct regulator_dev *rdev)
506e92a4047SStephen Boyd {
507e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
508e92a4047SStephen Boyd 
509e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
510e92a4047SStephen Boyd 		SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
511e92a4047SStephen Boyd }
512e92a4047SStephen Boyd 
513e92a4047SStephen Boyd static int spmi_regulator_vs_enable(struct regulator_dev *rdev)
514e92a4047SStephen Boyd {
515e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
516e92a4047SStephen Boyd 
517e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
518e92a4047SStephen Boyd 		vreg->ocp_count = 0;
519e92a4047SStephen Boyd 		vreg->vs_enable_time = ktime_get();
520e92a4047SStephen Boyd 	}
521e92a4047SStephen Boyd 
522e92a4047SStephen Boyd 	return spmi_regulator_common_enable(rdev);
523e92a4047SStephen Boyd }
524e92a4047SStephen Boyd 
525e2adfacdSStephen Boyd static int spmi_regulator_vs_ocp(struct regulator_dev *rdev)
526e2adfacdSStephen Boyd {
527e2adfacdSStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
528e2adfacdSStephen Boyd 	u8 reg = SPMI_VS_OCP_OVERRIDE;
529e2adfacdSStephen Boyd 
530e2adfacdSStephen Boyd 	return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, &reg, 1);
531e2adfacdSStephen Boyd }
532e2adfacdSStephen Boyd 
533e92a4047SStephen Boyd static int spmi_regulator_common_disable(struct regulator_dev *rdev)
534e92a4047SStephen Boyd {
535e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
536e92a4047SStephen Boyd 
537e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
538e92a4047SStephen Boyd 		SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
539e92a4047SStephen Boyd }
540e92a4047SStephen Boyd 
541e92a4047SStephen Boyd static int spmi_regulator_select_voltage(struct spmi_regulator *vreg,
542e92a4047SStephen Boyd 		int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel,
543e92a4047SStephen Boyd 		unsigned *selector)
544e92a4047SStephen Boyd {
545e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
546e92a4047SStephen Boyd 	int uV = min_uV;
547e92a4047SStephen Boyd 	int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
548e92a4047SStephen Boyd 
549e92a4047SStephen Boyd 	/* Check if request voltage is outside of physically settable range. */
550e92a4047SStephen Boyd 	lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
551e92a4047SStephen Boyd 	lim_max_uV =
552e92a4047SStephen Boyd 	  vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
553e92a4047SStephen Boyd 
554e92a4047SStephen Boyd 	if (uV < lim_min_uV && max_uV >= lim_min_uV)
555e92a4047SStephen Boyd 		uV = lim_min_uV;
556e92a4047SStephen Boyd 
557e92a4047SStephen Boyd 	if (uV < lim_min_uV || uV > lim_max_uV) {
558e92a4047SStephen Boyd 		dev_err(vreg->dev,
559e92a4047SStephen Boyd 			"request v=[%d, %d] is outside possible v=[%d, %d]\n",
560e92a4047SStephen Boyd 			 min_uV, max_uV, lim_min_uV, lim_max_uV);
561e92a4047SStephen Boyd 		return -EINVAL;
562e92a4047SStephen Boyd 	}
563e92a4047SStephen Boyd 
564e92a4047SStephen Boyd 	/* Find the range which uV is inside of. */
565e92a4047SStephen Boyd 	for (i = vreg->set_points->count - 1; i > 0; i--) {
566e92a4047SStephen Boyd 		range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
567e92a4047SStephen Boyd 		if (uV > range_max_uV && range_max_uV > 0)
568e92a4047SStephen Boyd 			break;
569e92a4047SStephen Boyd 	}
570e92a4047SStephen Boyd 
571e92a4047SStephen Boyd 	range_id = i;
572e92a4047SStephen Boyd 	range = &vreg->set_points->range[range_id];
573e92a4047SStephen Boyd 	*range_sel = range->range_sel;
574e92a4047SStephen Boyd 
575e92a4047SStephen Boyd 	/*
576e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
577e92a4047SStephen Boyd 	 * the uV value.
578e92a4047SStephen Boyd 	 */
5795d506a5aSAxel Lin 	*voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
580e92a4047SStephen Boyd 	uV = *voltage_sel * range->step_uV + range->min_uV;
581e92a4047SStephen Boyd 
582e92a4047SStephen Boyd 	if (uV > max_uV) {
583e92a4047SStephen Boyd 		dev_err(vreg->dev,
584e92a4047SStephen Boyd 			"request v=[%d, %d] cannot be met by any set point; "
585e92a4047SStephen Boyd 			"next set point: %d\n",
586e92a4047SStephen Boyd 			min_uV, max_uV, uV);
587e92a4047SStephen Boyd 		return -EINVAL;
588e92a4047SStephen Boyd 	}
589e92a4047SStephen Boyd 
590e92a4047SStephen Boyd 	*selector = 0;
591e92a4047SStephen Boyd 	for (i = 0; i < range_id; i++)
592e92a4047SStephen Boyd 		*selector += vreg->set_points->range[i].n_voltages;
593e92a4047SStephen Boyd 	*selector += (uV - range->set_point_min_uV) / range->step_uV;
594e92a4047SStephen Boyd 
595e92a4047SStephen Boyd 	return 0;
596e92a4047SStephen Boyd }
597e92a4047SStephen Boyd 
598e92a4047SStephen Boyd static const struct spmi_voltage_range *
599e92a4047SStephen Boyd spmi_regulator_find_range(struct spmi_regulator *vreg)
600e92a4047SStephen Boyd {
601e92a4047SStephen Boyd 	u8 range_sel;
602e92a4047SStephen Boyd 	const struct spmi_voltage_range *range, *end;
603e92a4047SStephen Boyd 
604e92a4047SStephen Boyd 	range = vreg->set_points->range;
605e92a4047SStephen Boyd 	end = range + vreg->set_points->count;
606e92a4047SStephen Boyd 
607e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1);
608e92a4047SStephen Boyd 
609e92a4047SStephen Boyd 	for (; range < end; range++)
610e92a4047SStephen Boyd 		if (range->range_sel == range_sel)
611e92a4047SStephen Boyd 			return range;
612e92a4047SStephen Boyd 
613e92a4047SStephen Boyd 	return NULL;
614e92a4047SStephen Boyd }
615e92a4047SStephen Boyd 
616e92a4047SStephen Boyd static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg,
617e92a4047SStephen Boyd 		int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel,
618e92a4047SStephen Boyd 		unsigned *selector)
619e92a4047SStephen Boyd {
620e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
621e92a4047SStephen Boyd 	int uV = min_uV;
622e92a4047SStephen Boyd 	int i;
623e92a4047SStephen Boyd 
624e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
625e92a4047SStephen Boyd 	if (!range)
626e92a4047SStephen Boyd 		goto different_range;
627e92a4047SStephen Boyd 
628e92a4047SStephen Boyd 	if (uV < range->min_uV && max_uV >= range->min_uV)
629e92a4047SStephen Boyd 		uV = range->min_uV;
630e92a4047SStephen Boyd 
631e92a4047SStephen Boyd 	if (uV < range->min_uV || uV > range->max_uV) {
632e92a4047SStephen Boyd 		/* Current range doesn't support the requested voltage. */
633e92a4047SStephen Boyd 		goto different_range;
634e92a4047SStephen Boyd 	}
635e92a4047SStephen Boyd 
636e92a4047SStephen Boyd 	/*
637e92a4047SStephen Boyd 	 * Force uV to be an allowed set point by applying a ceiling function to
638e92a4047SStephen Boyd 	 * the uV value.
639e92a4047SStephen Boyd 	 */
640e92a4047SStephen Boyd 	*voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
641e92a4047SStephen Boyd 	uV = *voltage_sel * range->step_uV + range->min_uV;
642e92a4047SStephen Boyd 
643e92a4047SStephen Boyd 	if (uV > max_uV) {
644e92a4047SStephen Boyd 		/*
645e92a4047SStephen Boyd 		 * No set point in the current voltage range is within the
646e92a4047SStephen Boyd 		 * requested min_uV to max_uV range.
647e92a4047SStephen Boyd 		 */
648e92a4047SStephen Boyd 		goto different_range;
649e92a4047SStephen Boyd 	}
650e92a4047SStephen Boyd 
651e92a4047SStephen Boyd 	*selector = 0;
652e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
653e92a4047SStephen Boyd 		if (uV >= vreg->set_points->range[i].set_point_min_uV
6549b2dfee3SStephen Boyd 		    && uV <= vreg->set_points->range[i].set_point_max_uV) {
655e92a4047SStephen Boyd 			*selector +=
656e92a4047SStephen Boyd 			    (uV - vreg->set_points->range[i].set_point_min_uV)
657e92a4047SStephen Boyd 				/ vreg->set_points->range[i].step_uV;
658e92a4047SStephen Boyd 			break;
6599b2dfee3SStephen Boyd 		}
660e92a4047SStephen Boyd 
661e92a4047SStephen Boyd 		*selector += vreg->set_points->range[i].n_voltages;
662e92a4047SStephen Boyd 	}
663e92a4047SStephen Boyd 
664e92a4047SStephen Boyd 	if (*selector >= vreg->set_points->n_voltages)
665e92a4047SStephen Boyd 		goto different_range;
666e92a4047SStephen Boyd 
667e92a4047SStephen Boyd 	return 0;
668e92a4047SStephen Boyd 
669e92a4047SStephen Boyd different_range:
670e92a4047SStephen Boyd 	return spmi_regulator_select_voltage(vreg, min_uV, max_uV,
671e92a4047SStephen Boyd 			range_sel, voltage_sel, selector);
672e92a4047SStephen Boyd }
673e92a4047SStephen Boyd 
674e92a4047SStephen Boyd static int spmi_regulator_common_set_voltage(struct regulator_dev *rdev,
675e92a4047SStephen Boyd 		int min_uV, int max_uV, unsigned *selector)
676e92a4047SStephen Boyd {
677e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
678e92a4047SStephen Boyd 	int ret;
679e92a4047SStephen Boyd 	u8 buf[2];
680e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
681e92a4047SStephen Boyd 
682e92a4047SStephen Boyd 	/*
683e92a4047SStephen Boyd 	 * Favor staying in the current voltage range if possible.  This avoids
684e92a4047SStephen Boyd 	 * voltage spikes that occur when changing the voltage range.
685e92a4047SStephen Boyd 	 */
686e92a4047SStephen Boyd 	ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
687e92a4047SStephen Boyd 		&range_sel, &voltage_sel, selector);
688e92a4047SStephen Boyd 	if (ret)
689e92a4047SStephen Boyd 		return ret;
690e92a4047SStephen Boyd 
691e92a4047SStephen Boyd 	buf[0] = range_sel;
692e92a4047SStephen Boyd 	buf[1] = voltage_sel;
693e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2);
694e92a4047SStephen Boyd }
695e92a4047SStephen Boyd 
696e92a4047SStephen Boyd static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
697e92a4047SStephen Boyd 		unsigned int old_selector, unsigned int new_selector)
698e92a4047SStephen Boyd {
699e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
700e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
701e92a4047SStephen Boyd 	int diff_uV;
702e92a4047SStephen Boyd 
703e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
704e92a4047SStephen Boyd 	if (!range)
705e92a4047SStephen Boyd 		return -EINVAL;
706e92a4047SStephen Boyd 
707e92a4047SStephen Boyd 	diff_uV = abs(new_selector - old_selector) * range->step_uV;
708e92a4047SStephen Boyd 
709e92a4047SStephen Boyd 	return DIV_ROUND_UP(diff_uV, vreg->slew_rate);
710e92a4047SStephen Boyd }
711e92a4047SStephen Boyd 
712e92a4047SStephen Boyd static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev)
713e92a4047SStephen Boyd {
714e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
715e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
716e92a4047SStephen Boyd 	u8 voltage_sel;
717e92a4047SStephen Boyd 
718e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
719e92a4047SStephen Boyd 
720e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
721e92a4047SStephen Boyd 	if (!range)
722e92a4047SStephen Boyd 		return VOLTAGE_UNKNOWN;
723e92a4047SStephen Boyd 
724e92a4047SStephen Boyd 	return range->step_uV * voltage_sel + range->min_uV;
725e92a4047SStephen Boyd }
726e92a4047SStephen Boyd 
727e92a4047SStephen Boyd static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev,
728e92a4047SStephen Boyd 		int min_uV, int max_uV, unsigned *selector)
729e92a4047SStephen Boyd {
730e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
731e92a4047SStephen Boyd 	int ret;
732e92a4047SStephen Boyd 	u8 range_sel, sel;
733e92a4047SStephen Boyd 
734e92a4047SStephen Boyd 	ret = spmi_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
735e92a4047SStephen Boyd 		&sel, selector);
736e92a4047SStephen Boyd 	if (ret) {
737e92a4047SStephen Boyd 		dev_err(vreg->dev, "could not set voltage, ret=%d\n", ret);
738e92a4047SStephen Boyd 		return ret;
739e92a4047SStephen Boyd 	}
740e92a4047SStephen Boyd 
741e92a4047SStephen Boyd 	/*
742e92a4047SStephen Boyd 	 * Certain types of regulators do not have a range select register so
743e92a4047SStephen Boyd 	 * only voltage set register needs to be written.
744e92a4047SStephen Boyd 	 */
745e92a4047SStephen Boyd 	return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1);
746e92a4047SStephen Boyd }
747e92a4047SStephen Boyd 
748e92a4047SStephen Boyd static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev)
749e92a4047SStephen Boyd {
750e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
751e92a4047SStephen Boyd 	const struct spmi_voltage_range *range = vreg->set_points->range;
752e92a4047SStephen Boyd 	u8 voltage_sel;
753e92a4047SStephen Boyd 
754e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
755e92a4047SStephen Boyd 
756e92a4047SStephen Boyd 	return range->step_uV * voltage_sel + range->min_uV;
757e92a4047SStephen Boyd }
758e92a4047SStephen Boyd 
759e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
760e92a4047SStephen Boyd 		int min_uV, int max_uV, unsigned *selector)
761e92a4047SStephen Boyd {
762e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
763e92a4047SStephen Boyd 	int ret;
764e92a4047SStephen Boyd 	u8 range_sel, voltage_sel;
765e92a4047SStephen Boyd 
766e92a4047SStephen Boyd 	/*
767e92a4047SStephen Boyd 	 * Favor staying in the current voltage range if possible. This avoids
768e92a4047SStephen Boyd 	 * voltage spikes that occur when changing the voltage range.
769e92a4047SStephen Boyd 	 */
770e92a4047SStephen Boyd 	ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
771e92a4047SStephen Boyd 		&range_sel, &voltage_sel, selector);
772e92a4047SStephen Boyd 	if (ret)
773e92a4047SStephen Boyd 		return ret;
774e92a4047SStephen Boyd 
775e92a4047SStephen Boyd 	/*
776e92a4047SStephen Boyd 	 * Calculate VSET based on range
777e92a4047SStephen Boyd 	 * In case of range 0: voltage_sel is a 7 bit value, can be written
778e92a4047SStephen Boyd 	 *			witout any modification.
779e92a4047SStephen Boyd 	 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
780e92a4047SStephen Boyd 	 *			[011].
781e92a4047SStephen Boyd 	 */
782e92a4047SStephen Boyd 	if (range_sel == 1)
783e92a4047SStephen Boyd 		voltage_sel |= ULT_SMPS_RANGE_SPLIT;
784e92a4047SStephen Boyd 
7850f94bffaSJulia Lawall 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET,
786e92a4047SStephen Boyd 	       voltage_sel, 0xff);
787e92a4047SStephen Boyd }
788e92a4047SStephen Boyd 
789e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
790e92a4047SStephen Boyd {
791e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
792e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
793e92a4047SStephen Boyd 	u8 voltage_sel;
794e92a4047SStephen Boyd 
795e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
796e92a4047SStephen Boyd 
797e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
798e92a4047SStephen Boyd 	if (!range)
799e92a4047SStephen Boyd 		return VOLTAGE_UNKNOWN;
800e92a4047SStephen Boyd 
801e92a4047SStephen Boyd 	if (range->range_sel == 1)
802e92a4047SStephen Boyd 		voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
803e92a4047SStephen Boyd 
804e92a4047SStephen Boyd 	return range->step_uV * voltage_sel + range->min_uV;
805e92a4047SStephen Boyd }
806e92a4047SStephen Boyd 
807e92a4047SStephen Boyd static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
808e92a4047SStephen Boyd 			unsigned selector)
809e92a4047SStephen Boyd {
810e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
811e92a4047SStephen Boyd 	int uV = 0;
812e92a4047SStephen Boyd 	int i;
813e92a4047SStephen Boyd 
814e92a4047SStephen Boyd 	if (selector >= vreg->set_points->n_voltages)
815e92a4047SStephen Boyd 		return 0;
816e92a4047SStephen Boyd 
817e92a4047SStephen Boyd 	for (i = 0; i < vreg->set_points->count; i++) {
8189b2dfee3SStephen Boyd 		if (selector < vreg->set_points->range[i].n_voltages) {
819e92a4047SStephen Boyd 			uV = selector * vreg->set_points->range[i].step_uV
820e92a4047SStephen Boyd 				+ vreg->set_points->range[i].set_point_min_uV;
821e92a4047SStephen Boyd 			break;
8229b2dfee3SStephen Boyd 		}
823e92a4047SStephen Boyd 
824e92a4047SStephen Boyd 		selector -= vreg->set_points->range[i].n_voltages;
825e92a4047SStephen Boyd 	}
826e92a4047SStephen Boyd 
827e92a4047SStephen Boyd 	return uV;
828e92a4047SStephen Boyd }
829e92a4047SStephen Boyd 
830e92a4047SStephen Boyd static int
831e92a4047SStephen Boyd spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable)
832e92a4047SStephen Boyd {
833e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
834e92a4047SStephen Boyd 	u8 mask = SPMI_COMMON_MODE_BYPASS_MASK;
835e92a4047SStephen Boyd 	u8 val = 0;
836e92a4047SStephen Boyd 
837e92a4047SStephen Boyd 	if (enable)
838e92a4047SStephen Boyd 		val = mask;
839e92a4047SStephen Boyd 
840e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
841e92a4047SStephen Boyd }
842e92a4047SStephen Boyd 
843e92a4047SStephen Boyd static int
844e92a4047SStephen Boyd spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable)
845e92a4047SStephen Boyd {
846e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
847e92a4047SStephen Boyd 	u8 val;
848e92a4047SStephen Boyd 	int ret;
849e92a4047SStephen Boyd 
850e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1);
851e92a4047SStephen Boyd 	*enable = val & SPMI_COMMON_MODE_BYPASS_MASK;
852e92a4047SStephen Boyd 
853e92a4047SStephen Boyd 	return ret;
854e92a4047SStephen Boyd }
855e92a4047SStephen Boyd 
856e92a4047SStephen Boyd static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev)
857e92a4047SStephen Boyd {
858e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
859e92a4047SStephen Boyd 	u8 reg;
860e92a4047SStephen Boyd 
861e92a4047SStephen Boyd 	spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
862e92a4047SStephen Boyd 
863e92a4047SStephen Boyd 	if (reg & SPMI_COMMON_MODE_HPM_MASK)
864e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
865e92a4047SStephen Boyd 
866e2adfacdSStephen Boyd 	if (reg & SPMI_COMMON_MODE_AUTO_MASK)
867e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
868e2adfacdSStephen Boyd 
869e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
870e92a4047SStephen Boyd }
871e92a4047SStephen Boyd 
872e92a4047SStephen Boyd static int
873e92a4047SStephen Boyd spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode)
874e92a4047SStephen Boyd {
875e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
876e2adfacdSStephen Boyd 	u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
877e92a4047SStephen Boyd 	u8 val = 0;
878e92a4047SStephen Boyd 
879e92a4047SStephen Boyd 	if (mode == REGULATOR_MODE_NORMAL)
880e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_HPM_MASK;
881e2adfacdSStephen Boyd 	else if (mode == REGULATOR_MODE_FAST)
882e2adfacdSStephen Boyd 		val = SPMI_COMMON_MODE_AUTO_MASK;
883e92a4047SStephen Boyd 
884e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
885e92a4047SStephen Boyd }
886e92a4047SStephen Boyd 
887e92a4047SStephen Boyd static int
888e92a4047SStephen Boyd spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA)
889e92a4047SStephen Boyd {
890e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
891e92a4047SStephen Boyd 	unsigned int mode;
892e92a4047SStephen Boyd 
893e92a4047SStephen Boyd 	if (load_uA >= vreg->hpm_min_load)
894e92a4047SStephen Boyd 		mode = REGULATOR_MODE_NORMAL;
895e92a4047SStephen Boyd 	else
896e92a4047SStephen Boyd 		mode = REGULATOR_MODE_IDLE;
897e92a4047SStephen Boyd 
898e92a4047SStephen Boyd 	return spmi_regulator_common_set_mode(rdev, mode);
899e92a4047SStephen Boyd }
900e92a4047SStephen Boyd 
901e92a4047SStephen Boyd static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev)
902e92a4047SStephen Boyd {
903e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
904e92a4047SStephen Boyd 	unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK;
905e92a4047SStephen Boyd 
906e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN,
907e92a4047SStephen Boyd 				     mask, mask);
908e92a4047SStephen Boyd }
909e92a4047SStephen Boyd 
910e92a4047SStephen Boyd static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev)
911e92a4047SStephen Boyd {
912e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
913e92a4047SStephen Boyd 	unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK;
914e92a4047SStephen Boyd 
915e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START,
916e92a4047SStephen Boyd 				     mask, mask);
917e92a4047SStephen Boyd }
918e92a4047SStephen Boyd 
919e92a4047SStephen Boyd static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA)
920e92a4047SStephen Boyd {
921e92a4047SStephen Boyd 	struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
922e92a4047SStephen Boyd 	enum spmi_regulator_logical_type type = vreg->logical_type;
923e92a4047SStephen Boyd 	unsigned int current_reg;
924e92a4047SStephen Boyd 	u8 reg;
925e92a4047SStephen Boyd 	u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK |
926e92a4047SStephen Boyd 		  SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
927e92a4047SStephen Boyd 	int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500;
928e92a4047SStephen Boyd 
929e92a4047SStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST)
930e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_REG_CURRENT_LIMIT;
931e92a4047SStephen Boyd 	else
932e92a4047SStephen Boyd 		current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT;
933e92a4047SStephen Boyd 
934e92a4047SStephen Boyd 	if (ilim_uA > max || ilim_uA <= 0)
935e92a4047SStephen Boyd 		return -EINVAL;
936e92a4047SStephen Boyd 
937e92a4047SStephen Boyd 	reg = (ilim_uA - 1) / 500;
938e92a4047SStephen Boyd 	reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
939e92a4047SStephen Boyd 
940e92a4047SStephen Boyd 	return spmi_vreg_update_bits(vreg, current_reg, reg, mask);
941e92a4047SStephen Boyd }
942e92a4047SStephen Boyd 
943e92a4047SStephen Boyd static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg)
944e92a4047SStephen Boyd {
945e92a4047SStephen Boyd 	int ret;
946e92a4047SStephen Boyd 
947e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
948e92a4047SStephen Boyd 		SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
949e92a4047SStephen Boyd 
950e92a4047SStephen Boyd 	vreg->vs_enable_time = ktime_get();
951e92a4047SStephen Boyd 
952e92a4047SStephen Boyd 	ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
953e92a4047SStephen Boyd 		SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
954e92a4047SStephen Boyd 
955e92a4047SStephen Boyd 	return ret;
956e92a4047SStephen Boyd }
957e92a4047SStephen Boyd 
958e92a4047SStephen Boyd static void spmi_regulator_vs_ocp_work(struct work_struct *work)
959e92a4047SStephen Boyd {
960e92a4047SStephen Boyd 	struct delayed_work *dwork = to_delayed_work(work);
961e92a4047SStephen Boyd 	struct spmi_regulator *vreg
962e92a4047SStephen Boyd 		= container_of(dwork, struct spmi_regulator, ocp_work);
963e92a4047SStephen Boyd 
964e92a4047SStephen Boyd 	spmi_regulator_vs_clear_ocp(vreg);
965e92a4047SStephen Boyd }
966e92a4047SStephen Boyd 
967e92a4047SStephen Boyd static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data)
968e92a4047SStephen Boyd {
969e92a4047SStephen Boyd 	struct spmi_regulator *vreg = data;
970e92a4047SStephen Boyd 	ktime_t ocp_irq_time;
971e92a4047SStephen Boyd 	s64 ocp_trigger_delay_us;
972e92a4047SStephen Boyd 
973e92a4047SStephen Boyd 	ocp_irq_time = ktime_get();
974e92a4047SStephen Boyd 	ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
975e92a4047SStephen Boyd 						vreg->vs_enable_time);
976e92a4047SStephen Boyd 
977e92a4047SStephen Boyd 	/*
978e92a4047SStephen Boyd 	 * Reset the OCP count if there is a large delay between switch enable
979e92a4047SStephen Boyd 	 * and when OCP triggers.  This is indicative of a hotplug event as
980e92a4047SStephen Boyd 	 * opposed to a fault.
981e92a4047SStephen Boyd 	 */
982e92a4047SStephen Boyd 	if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US)
983e92a4047SStephen Boyd 		vreg->ocp_count = 0;
984e92a4047SStephen Boyd 
985e92a4047SStephen Boyd 	/* Wait for switch output to settle back to 0 V after OCP triggered. */
986e92a4047SStephen Boyd 	udelay(SPMI_VS_OCP_FALL_DELAY_US);
987e92a4047SStephen Boyd 
988e92a4047SStephen Boyd 	vreg->ocp_count++;
989e92a4047SStephen Boyd 
990e92a4047SStephen Boyd 	if (vreg->ocp_count == 1) {
991e92a4047SStephen Boyd 		/* Immediately clear the over current condition. */
992e92a4047SStephen Boyd 		spmi_regulator_vs_clear_ocp(vreg);
993e92a4047SStephen Boyd 	} else if (vreg->ocp_count <= vreg->ocp_max_retries) {
994e92a4047SStephen Boyd 		/* Schedule the over current clear task to run later. */
995e92a4047SStephen Boyd 		schedule_delayed_work(&vreg->ocp_work,
996e92a4047SStephen Boyd 			msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
997e92a4047SStephen Boyd 	} else {
998e92a4047SStephen Boyd 		dev_err(vreg->dev,
999e92a4047SStephen Boyd 			"OCP triggered %d times; no further retries\n",
1000e92a4047SStephen Boyd 			vreg->ocp_count);
1001e92a4047SStephen Boyd 	}
1002e92a4047SStephen Boyd 
1003e92a4047SStephen Boyd 	return IRQ_HANDLED;
1004e92a4047SStephen Boyd }
1005e92a4047SStephen Boyd 
1006e92a4047SStephen Boyd static struct regulator_ops spmi_smps_ops = {
1007e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1008e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1009e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1010e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_common_set_voltage,
1011e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_common_get_voltage,
1012e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1013e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1014e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1015e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1016e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1017e92a4047SStephen Boyd };
1018e92a4047SStephen Boyd 
1019e92a4047SStephen Boyd static struct regulator_ops spmi_ldo_ops = {
1020e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1021e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1022e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1023e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_common_set_voltage,
1024e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_common_get_voltage,
1025e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1026e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1027e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1028e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1029e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1030e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1031e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1032e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1033e92a4047SStephen Boyd };
1034e92a4047SStephen Boyd 
1035e92a4047SStephen Boyd static struct regulator_ops spmi_ln_ldo_ops = {
1036e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1037e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1038e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1039e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_common_set_voltage,
1040e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_common_get_voltage,
1041e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1042e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1043e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1044e92a4047SStephen Boyd };
1045e92a4047SStephen Boyd 
1046e92a4047SStephen Boyd static struct regulator_ops spmi_vs_ops = {
1047e92a4047SStephen Boyd 	.enable			= spmi_regulator_vs_enable,
1048e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1049e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1050e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1051e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1052e2adfacdSStephen Boyd 	.set_over_current_protection = spmi_regulator_vs_ocp,
1053e92a4047SStephen Boyd };
1054e92a4047SStephen Boyd 
1055e92a4047SStephen Boyd static struct regulator_ops spmi_boost_ops = {
1056e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1057e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1058e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1059e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_single_range_set_voltage,
1060e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_single_range_get_voltage,
1061e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1062e92a4047SStephen Boyd 	.set_input_current_limit = spmi_regulator_set_ilim,
1063e92a4047SStephen Boyd };
1064e92a4047SStephen Boyd 
1065e92a4047SStephen Boyd static struct regulator_ops spmi_ftsmps_ops = {
1066e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1067e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1068e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1069e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_common_set_voltage,
1070e92a4047SStephen Boyd 	.set_voltage_time_sel	= spmi_regulator_set_voltage_time_sel,
1071e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_common_get_voltage,
1072e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1073e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1074e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1075e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1076e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1077e92a4047SStephen Boyd };
1078e92a4047SStephen Boyd 
1079e92a4047SStephen Boyd static struct regulator_ops spmi_ult_lo_smps_ops = {
1080e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1081e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1082e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1083e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_ult_lo_smps_set_voltage,
1084e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_ult_lo_smps_get_voltage,
1085e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1086e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1087e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1088e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1089e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1090e92a4047SStephen Boyd };
1091e92a4047SStephen Boyd 
1092e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ho_smps_ops = {
1093e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1094e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1095e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1096e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_single_range_set_voltage,
1097e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_single_range_get_voltage,
1098e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1099e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1100e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1101e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1102e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1103e92a4047SStephen Boyd };
1104e92a4047SStephen Boyd 
1105e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ldo_ops = {
1106e92a4047SStephen Boyd 	.enable			= spmi_regulator_common_enable,
1107e92a4047SStephen Boyd 	.disable		= spmi_regulator_common_disable,
1108e92a4047SStephen Boyd 	.is_enabled		= spmi_regulator_common_is_enabled,
1109e92a4047SStephen Boyd 	.set_voltage		= spmi_regulator_single_range_set_voltage,
1110e92a4047SStephen Boyd 	.get_voltage		= spmi_regulator_single_range_get_voltage,
1111e92a4047SStephen Boyd 	.list_voltage		= spmi_regulator_common_list_voltage,
1112e92a4047SStephen Boyd 	.set_mode		= spmi_regulator_common_set_mode,
1113e92a4047SStephen Boyd 	.get_mode		= spmi_regulator_common_get_mode,
1114e92a4047SStephen Boyd 	.set_load		= spmi_regulator_common_set_load,
1115e92a4047SStephen Boyd 	.set_bypass		= spmi_regulator_common_set_bypass,
1116e92a4047SStephen Boyd 	.get_bypass		= spmi_regulator_common_get_bypass,
1117e92a4047SStephen Boyd 	.set_pull_down		= spmi_regulator_common_set_pull_down,
1118e92a4047SStephen Boyd 	.set_soft_start		= spmi_regulator_common_set_soft_start,
1119e92a4047SStephen Boyd };
1120e92a4047SStephen Boyd 
1121e92a4047SStephen Boyd /* Maximum possible digital major revision value */
1122e92a4047SStephen Boyd #define INF 0xFF
1123e92a4047SStephen Boyd 
1124e92a4047SStephen Boyd static const struct spmi_regulator_mapping supported_regulators[] = {
1125e92a4047SStephen Boyd 	/*           type subtype dig_min dig_max ltype ops setpoints hpm_min */
1126e92a4047SStephen Boyd 	SPMI_VREG(BUCK,  GP_CTL,   0, INF, SMPS,   smps,   smps,   100000),
1127e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N300,     0, INF, LDO,    ldo,    nldo1,   10000),
1128e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     0,   0, LDO,    ldo,    nldo2,   10000),
1129e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    0,   0, LDO,    ldo,    nldo2,   10000),
1130e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600,     1, INF, LDO,    ldo,    nldo3,   10000),
1131e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200,    1, INF, LDO,    ldo,    nldo3,   10000),
1132e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  0,   0, LDO,    ldo,    nldo2,   10000),
1133e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 0,   0, LDO,    ldo,    nldo2,   10000),
1134e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N600_ST,  1, INF, LDO,    ldo,    nldo3,   10000),
1135e92a4047SStephen Boyd 	SPMI_VREG(LDO,   N1200_ST, 1, INF, LDO,    ldo,    nldo3,   10000),
1136e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P50,      0, INF, LDO,    ldo,    pldo,     5000),
1137e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P150,     0, INF, LDO,    ldo,    pldo,    10000),
1138e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P300,     0, INF, LDO,    ldo,    pldo,    10000),
1139e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P600,     0, INF, LDO,    ldo,    pldo,    10000),
1140e92a4047SStephen Boyd 	SPMI_VREG(LDO,   P1200,    0, INF, LDO,    ldo,    pldo,    10000),
1141e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LN,       0, INF, LN_LDO, ln_ldo, ln_ldo,      0),
1142e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P50,   0, INF, LDO,    ldo,    pldo,     5000),
1143e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P150,  0, INF, LDO,    ldo,    pldo,    10000),
1144e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P300,  0, INF, LDO,    ldo,    pldo,    10000),
1145e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P600,  0, INF, LDO,    ldo,    pldo,    10000),
1146e92a4047SStephen Boyd 	SPMI_VREG(LDO,   LV_P1200, 0, INF, LDO,    ldo,    pldo,    10000),
1147e92a4047SStephen Boyd 	SPMI_VREG_VS(LV100,        0, INF),
1148e92a4047SStephen Boyd 	SPMI_VREG_VS(LV300,        0, INF),
1149e92a4047SStephen Boyd 	SPMI_VREG_VS(MV300,        0, INF),
1150e92a4047SStephen Boyd 	SPMI_VREG_VS(MV500,        0, INF),
1151e92a4047SStephen Boyd 	SPMI_VREG_VS(HDMI,         0, INF),
1152e92a4047SStephen Boyd 	SPMI_VREG_VS(OTG,          0, INF),
1153e92a4047SStephen Boyd 	SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST,  boost,  boost,       0),
1154e92a4047SStephen Boyd 	SPMI_VREG(FTS,   FTS_CTL,  0, INF, FTSMPS, ftsmps, ftsmps, 100000),
1155e92a4047SStephen Boyd 	SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000),
1156e92a4047SStephen Boyd 	SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
1157e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1158e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1159e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1160e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1161e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1162e92a4047SStephen Boyd 						ult_lo_smps,   100000),
1163e92a4047SStephen Boyd 	SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
1164e92a4047SStephen Boyd 						ult_ho_smps,   100000),
1165e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1166e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1167e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1168e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1169e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P150,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1170e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P300,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1171e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, LV_P450,  0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1172e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P600,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1173e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P150,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1174e92a4047SStephen Boyd 	SPMI_VREG(ULT_LDO, P50,     0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000),
1175e92a4047SStephen Boyd };
1176e92a4047SStephen Boyd 
1177e92a4047SStephen Boyd static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points)
1178e92a4047SStephen Boyd {
1179e92a4047SStephen Boyd 	unsigned int n;
1180e92a4047SStephen Boyd 	struct spmi_voltage_range *range = points->range;
1181e92a4047SStephen Boyd 
1182e92a4047SStephen Boyd 	for (; range < points->range + points->count; range++) {
1183e92a4047SStephen Boyd 		n = 0;
1184e92a4047SStephen Boyd 		if (range->set_point_max_uV) {
1185e92a4047SStephen Boyd 			n = range->set_point_max_uV - range->set_point_min_uV;
1186419d06a1SAxel Lin 			n = (n / range->step_uV) + 1;
1187e92a4047SStephen Boyd 		}
1188e92a4047SStephen Boyd 		range->n_voltages = n;
1189e92a4047SStephen Boyd 		points->n_voltages += n;
1190e92a4047SStephen Boyd 	}
1191e92a4047SStephen Boyd }
1192e92a4047SStephen Boyd 
1193e92a4047SStephen Boyd static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)
1194e92a4047SStephen Boyd {
1195e92a4047SStephen Boyd 	const struct spmi_regulator_mapping *mapping;
1196e92a4047SStephen Boyd 	int ret, i;
1197e92a4047SStephen Boyd 	u32 dig_major_rev;
1198e92a4047SStephen Boyd 	u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1];
1199e92a4047SStephen Boyd 	u8 type, subtype;
1200e92a4047SStephen Boyd 
1201e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version,
1202e92a4047SStephen Boyd 		ARRAY_SIZE(version));
1203e92a4047SStephen Boyd 	if (ret) {
1204e92a4047SStephen Boyd 		dev_err(vreg->dev, "could not read version registers\n");
1205e92a4047SStephen Boyd 		return ret;
1206e92a4047SStephen Boyd 	}
1207e92a4047SStephen Boyd 	dig_major_rev	= version[SPMI_COMMON_REG_DIG_MAJOR_REV
1208e92a4047SStephen Boyd 					- SPMI_COMMON_REG_DIG_MAJOR_REV];
1209e92a4047SStephen Boyd 	if (!force_type) {
1210e92a4047SStephen Boyd 		type		= version[SPMI_COMMON_REG_TYPE -
1211e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1212e92a4047SStephen Boyd 		subtype		= version[SPMI_COMMON_REG_SUBTYPE -
1213e92a4047SStephen Boyd 					  SPMI_COMMON_REG_DIG_MAJOR_REV];
1214e92a4047SStephen Boyd 	} else {
1215e92a4047SStephen Boyd 		type = force_type >> 8;
1216e92a4047SStephen Boyd 		subtype = force_type;
1217e92a4047SStephen Boyd 	}
1218e92a4047SStephen Boyd 
1219e92a4047SStephen Boyd 	for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1220e92a4047SStephen Boyd 		mapping = &supported_regulators[i];
1221e92a4047SStephen Boyd 		if (mapping->type == type && mapping->subtype == subtype
1222e92a4047SStephen Boyd 		    && mapping->revision_min <= dig_major_rev
1223e92a4047SStephen Boyd 		    && mapping->revision_max >= dig_major_rev)
1224e92a4047SStephen Boyd 			goto found;
1225e92a4047SStephen Boyd 	}
1226e92a4047SStephen Boyd 
1227e92a4047SStephen Boyd 	dev_err(vreg->dev,
1228e92a4047SStephen Boyd 		"unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1229e92a4047SStephen Boyd 		vreg->desc.name, type, subtype, dig_major_rev);
1230e92a4047SStephen Boyd 
1231e92a4047SStephen Boyd 	return -ENODEV;
1232e92a4047SStephen Boyd 
1233e92a4047SStephen Boyd found:
1234e92a4047SStephen Boyd 	vreg->logical_type	= mapping->logical_type;
1235e92a4047SStephen Boyd 	vreg->set_points	= mapping->set_points;
1236e92a4047SStephen Boyd 	vreg->hpm_min_load	= mapping->hpm_min_load;
1237e92a4047SStephen Boyd 	vreg->desc.ops		= mapping->ops;
1238e92a4047SStephen Boyd 
1239e92a4047SStephen Boyd 	if (mapping->set_points) {
1240e92a4047SStephen Boyd 		if (!mapping->set_points->n_voltages)
1241e92a4047SStephen Boyd 			spmi_calculate_num_voltages(mapping->set_points);
1242e92a4047SStephen Boyd 		vreg->desc.n_voltages = mapping->set_points->n_voltages;
1243e92a4047SStephen Boyd 	}
1244e92a4047SStephen Boyd 
1245e92a4047SStephen Boyd 	return 0;
1246e92a4047SStephen Boyd }
1247e92a4047SStephen Boyd 
1248e92a4047SStephen Boyd static int spmi_regulator_ftsmps_init_slew_rate(struct spmi_regulator *vreg)
1249e92a4047SStephen Boyd {
1250e92a4047SStephen Boyd 	int ret;
1251e92a4047SStephen Boyd 	u8 reg = 0;
1252e92a4047SStephen Boyd 	int step, delay, slew_rate;
1253e92a4047SStephen Boyd 	const struct spmi_voltage_range *range;
1254e92a4047SStephen Boyd 
1255e92a4047SStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
1256e92a4047SStephen Boyd 	if (ret) {
1257e92a4047SStephen Boyd 		dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
1258e92a4047SStephen Boyd 		return ret;
1259e92a4047SStephen Boyd 	}
1260e92a4047SStephen Boyd 
1261e92a4047SStephen Boyd 	range = spmi_regulator_find_range(vreg);
1262e92a4047SStephen Boyd 	if (!range)
1263e92a4047SStephen Boyd 		return -EINVAL;
1264e92a4047SStephen Boyd 
1265e92a4047SStephen Boyd 	step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK;
1266e92a4047SStephen Boyd 	step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT;
1267e92a4047SStephen Boyd 
1268e92a4047SStephen Boyd 	delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK;
1269e92a4047SStephen Boyd 	delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT;
1270e92a4047SStephen Boyd 
1271e92a4047SStephen Boyd 	/* slew_rate has units of uV/us */
1272e92a4047SStephen Boyd 	slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
1273e92a4047SStephen Boyd 	slew_rate /= 1000 * (SPMI_FTSMPS_STEP_DELAY << delay);
1274e92a4047SStephen Boyd 	slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM;
1275e92a4047SStephen Boyd 	slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN;
1276e92a4047SStephen Boyd 
1277e92a4047SStephen Boyd 	/* Ensure that the slew rate is greater than 0 */
1278e92a4047SStephen Boyd 	vreg->slew_rate = max(slew_rate, 1);
1279e92a4047SStephen Boyd 
1280e92a4047SStephen Boyd 	return ret;
1281e92a4047SStephen Boyd }
1282e92a4047SStephen Boyd 
1283e2adfacdSStephen Boyd static int spmi_regulator_init_registers(struct spmi_regulator *vreg,
1284e2adfacdSStephen Boyd 				const struct spmi_regulator_init_data *data)
1285e2adfacdSStephen Boyd {
1286e2adfacdSStephen Boyd 	int ret;
1287e2adfacdSStephen Boyd 	enum spmi_regulator_logical_type type;
1288e2adfacdSStephen Boyd 	u8 ctrl_reg[8], reg, mask;
1289e2adfacdSStephen Boyd 
1290e2adfacdSStephen Boyd 	type = vreg->logical_type;
1291e2adfacdSStephen Boyd 
1292e2adfacdSStephen Boyd 	ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1293e2adfacdSStephen Boyd 	if (ret)
1294e2adfacdSStephen Boyd 		return ret;
1295e2adfacdSStephen Boyd 
1296e2adfacdSStephen Boyd 	/* Set up enable pin control. */
1297e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1298e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO
1299e2adfacdSStephen Boyd 	     || type == SPMI_REGULATOR_LOGICAL_TYPE_VS)
1300e2adfacdSStephen Boyd 	    && !(data->pin_ctrl_enable
1301e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1302e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] &=
1303e2adfacdSStephen Boyd 			~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1304e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_ENABLE] |=
1305e2adfacdSStephen Boyd 		    data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1306e2adfacdSStephen Boyd 	}
1307e2adfacdSStephen Boyd 
1308e2adfacdSStephen Boyd 	/* Set up mode pin control. */
1309e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1310e2adfacdSStephen Boyd 	    || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO)
1311e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1312e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1313e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1314e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1315e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1316e2adfacdSStephen Boyd 			data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1317e2adfacdSStephen Boyd 	}
1318e2adfacdSStephen Boyd 
1319e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS
1320e2adfacdSStephen Boyd 	   && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1321e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1322e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1323e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1324e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1325e2adfacdSStephen Boyd 	}
1326e2adfacdSStephen Boyd 
1327e2adfacdSStephen Boyd 	if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
1328e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
1329e2adfacdSStephen Boyd 		|| type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO)
1330e2adfacdSStephen Boyd 		&& !(data->pin_ctrl_hpm
1331e2adfacdSStephen Boyd 			& SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1332e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1333e2adfacdSStephen Boyd 			~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1334e2adfacdSStephen Boyd 		ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1335e2adfacdSStephen Boyd 		       data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1336e2adfacdSStephen Boyd 	}
1337e2adfacdSStephen Boyd 
1338e2adfacdSStephen Boyd 	/* Write back any control register values that were modified. */
1339e2adfacdSStephen Boyd 	ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1340e2adfacdSStephen Boyd 	if (ret)
1341e2adfacdSStephen Boyd 		return ret;
1342e2adfacdSStephen Boyd 
1343e2adfacdSStephen Boyd 	/* Set soft start strength and over current protection for VS. */
1344e2adfacdSStephen Boyd 	if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) {
1345e2adfacdSStephen Boyd 		if (data->vs_soft_start_strength
1346e2adfacdSStephen Boyd 				!= SPMI_VS_SOFT_START_STR_HW_DEFAULT) {
1347e2adfacdSStephen Boyd 			reg = data->vs_soft_start_strength
1348e2adfacdSStephen Boyd 				& SPMI_VS_SOFT_START_SEL_MASK;
1349e2adfacdSStephen Boyd 			mask = SPMI_VS_SOFT_START_SEL_MASK;
1350e2adfacdSStephen Boyd 			return spmi_vreg_update_bits(vreg,
1351e2adfacdSStephen Boyd 						     SPMI_VS_REG_SOFT_START,
1352e2adfacdSStephen Boyd 						     reg, mask);
1353e2adfacdSStephen Boyd 		}
1354e2adfacdSStephen Boyd 	}
1355e2adfacdSStephen Boyd 
1356e2adfacdSStephen Boyd 	return 0;
1357e2adfacdSStephen Boyd }
1358e2adfacdSStephen Boyd 
1359e2adfacdSStephen Boyd static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg,
1360e2adfacdSStephen Boyd 		struct device_node *node, struct spmi_regulator_init_data *data)
1361e2adfacdSStephen Boyd {
1362e2adfacdSStephen Boyd 	/*
1363e2adfacdSStephen Boyd 	 * Initialize configuration parameters to use hardware default in case
1364e2adfacdSStephen Boyd 	 * no value is specified via device tree.
1365e2adfacdSStephen Boyd 	 */
1366e2adfacdSStephen Boyd 	data->pin_ctrl_enable	    = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1367e2adfacdSStephen Boyd 	data->pin_ctrl_hpm	    = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1368e2adfacdSStephen Boyd 	data->vs_soft_start_strength	= SPMI_VS_SOFT_START_STR_HW_DEFAULT;
1369e2adfacdSStephen Boyd 
1370e2adfacdSStephen Boyd 	/* These bindings are optional, so it is okay if they aren't found. */
1371e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-max-retries",
1372e2adfacdSStephen Boyd 		&vreg->ocp_max_retries);
1373e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,ocp-retry-delay",
1374e2adfacdSStephen Boyd 		&vreg->ocp_retry_delay_ms);
1375e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-enable",
1376e2adfacdSStephen Boyd 		&data->pin_ctrl_enable);
1377e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm);
1378e2adfacdSStephen Boyd 	of_property_read_u32(node, "qcom,vs-soft-start-strength",
1379e2adfacdSStephen Boyd 		&data->vs_soft_start_strength);
1380e2adfacdSStephen Boyd }
1381e2adfacdSStephen Boyd 
1382e92a4047SStephen Boyd static unsigned int spmi_regulator_of_map_mode(unsigned int mode)
1383e92a4047SStephen Boyd {
1384e2adfacdSStephen Boyd 	if (mode == 1)
1385e92a4047SStephen Boyd 		return REGULATOR_MODE_NORMAL;
1386e2adfacdSStephen Boyd 	if (mode == 2)
1387e2adfacdSStephen Boyd 		return REGULATOR_MODE_FAST;
1388e92a4047SStephen Boyd 
1389e92a4047SStephen Boyd 	return REGULATOR_MODE_IDLE;
1390e92a4047SStephen Boyd }
1391e92a4047SStephen Boyd 
1392e92a4047SStephen Boyd static int spmi_regulator_of_parse(struct device_node *node,
1393e92a4047SStephen Boyd 			    const struct regulator_desc *desc,
1394e92a4047SStephen Boyd 			    struct regulator_config *config)
1395e92a4047SStephen Boyd {
1396e2adfacdSStephen Boyd 	struct spmi_regulator_init_data data = { };
1397e92a4047SStephen Boyd 	struct spmi_regulator *vreg = config->driver_data;
1398e92a4047SStephen Boyd 	struct device *dev = config->dev;
1399e92a4047SStephen Boyd 	int ret;
1400e92a4047SStephen Boyd 
1401e2adfacdSStephen Boyd 	spmi_regulator_get_dt_config(vreg, node, &data);
1402e2adfacdSStephen Boyd 
1403e2adfacdSStephen Boyd 	if (!vreg->ocp_max_retries)
1404e92a4047SStephen Boyd 		vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES;
1405e2adfacdSStephen Boyd 	if (!vreg->ocp_retry_delay_ms)
1406e92a4047SStephen Boyd 		vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS;
1407e92a4047SStephen Boyd 
1408e2adfacdSStephen Boyd 	ret = spmi_regulator_init_registers(vreg, &data);
1409e2adfacdSStephen Boyd 	if (ret) {
1410e2adfacdSStephen Boyd 		dev_err(dev, "common initialization failed, ret=%d\n", ret);
1411e2adfacdSStephen Boyd 		return ret;
1412e2adfacdSStephen Boyd 	}
1413e2adfacdSStephen Boyd 
1414e92a4047SStephen Boyd 	if (vreg->logical_type == SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS) {
1415e92a4047SStephen Boyd 		ret = spmi_regulator_ftsmps_init_slew_rate(vreg);
1416e92a4047SStephen Boyd 		if (ret)
1417e92a4047SStephen Boyd 			return ret;
1418e92a4047SStephen Boyd 	}
1419e92a4047SStephen Boyd 
1420e92a4047SStephen Boyd 	if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS)
1421e92a4047SStephen Boyd 		vreg->ocp_irq = 0;
1422e92a4047SStephen Boyd 
1423e92a4047SStephen Boyd 	if (vreg->ocp_irq) {
1424e92a4047SStephen Boyd 		ret = devm_request_irq(dev, vreg->ocp_irq,
1425e92a4047SStephen Boyd 			spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1426e92a4047SStephen Boyd 			vreg);
1427e92a4047SStephen Boyd 		if (ret < 0) {
1428e92a4047SStephen Boyd 			dev_err(dev, "failed to request irq %d, ret=%d\n",
1429e92a4047SStephen Boyd 				vreg->ocp_irq, ret);
1430e92a4047SStephen Boyd 			return ret;
1431e92a4047SStephen Boyd 		}
1432e92a4047SStephen Boyd 
1433e92a4047SStephen Boyd 		INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work);
1434e92a4047SStephen Boyd 	}
1435e92a4047SStephen Boyd 
1436e92a4047SStephen Boyd 	return 0;
1437e92a4047SStephen Boyd }
1438e92a4047SStephen Boyd 
1439e92a4047SStephen Boyd static const struct spmi_regulator_data pm8941_regulators[] = {
1440e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1441e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1442e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1443e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1444e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1445e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1446e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l11", },
1447e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 },
1448e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1449e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 },
1450e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l18_19", },
1451e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1452e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1453e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l4_l11", },
1454e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1455e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1456e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1457e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1458e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1459e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1460e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l8_l16_l18_19", },
1461e92a4047SStephen Boyd 	{ "l19", 0x5200, "vdd_l8_l16_l18_19", },
1462e92a4047SStephen Boyd 	{ "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1463e92a4047SStephen Boyd 	{ "l21", 0x5400, "vdd_l21", },
1464e92a4047SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1465e92a4047SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1466e92a4047SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1467e92a4047SStephen Boyd 	{ "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1468e92a4047SStephen Boyd 	{ "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1469e92a4047SStephen Boyd 	{ "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
1470e92a4047SStephen Boyd 	{ "mvs1", 0x8300, "vin_5vs", },
1471e92a4047SStephen Boyd 	{ "mvs2", 0x8400, "vin_5vs", },
1472e92a4047SStephen Boyd 	{ }
1473e92a4047SStephen Boyd };
1474e92a4047SStephen Boyd 
1475e92a4047SStephen Boyd static const struct spmi_regulator_data pm8841_regulators[] = {
1476e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1477e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", NULL, 0x1c08 },
1478e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1479e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 },
1480e92a4047SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", NULL, 0x1c08 },
1481e92a4047SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", NULL, 0x1c08 },
1482e92a4047SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", NULL, 0x1c08 },
1483e92a4047SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", NULL, 0x1c08 },
1484e92a4047SStephen Boyd 	{ }
1485e92a4047SStephen Boyd };
1486e92a4047SStephen Boyd 
1487e92a4047SStephen Boyd static const struct spmi_regulator_data pm8916_regulators[] = {
1488e92a4047SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1489e92a4047SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1490e92a4047SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1491e92a4047SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
1492e92a4047SStephen Boyd 	{ "l1", 0x4000, "vdd_l1_l3", },
1493e92a4047SStephen Boyd 	{ "l2", 0x4100, "vdd_l2", },
1494e92a4047SStephen Boyd 	{ "l3", 0x4200, "vdd_l1_l3", },
1495e92a4047SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l5_l6", },
1496e92a4047SStephen Boyd 	{ "l5", 0x4400, "vdd_l4_l5_l6", },
1497e92a4047SStephen Boyd 	{ "l6", 0x4500, "vdd_l4_l5_l6", },
1498e92a4047SStephen Boyd 	{ "l7", 0x4600, "vdd_l7", },
1499e92a4047SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1500e92a4047SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1501e92a4047SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1502e92a4047SStephen Boyd 	{ "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1503e92a4047SStephen Boyd 	{ "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1504e92a4047SStephen Boyd 	{ "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1505e92a4047SStephen Boyd 	{ "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1506e92a4047SStephen Boyd 	{ "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1507e92a4047SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1508e92a4047SStephen Boyd 	{ "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1509e92a4047SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1510e92a4047SStephen Boyd 	{ }
1511e92a4047SStephen Boyd };
1512e92a4047SStephen Boyd 
1513*50314e55SStephen Boyd static const struct spmi_regulator_data pm8994_regulators[] = {
1514*50314e55SStephen Boyd 	{ "s1", 0x1400, "vdd_s1", },
1515*50314e55SStephen Boyd 	{ "s2", 0x1700, "vdd_s2", },
1516*50314e55SStephen Boyd 	{ "s3", 0x1a00, "vdd_s3", },
1517*50314e55SStephen Boyd 	{ "s4", 0x1d00, "vdd_s4", },
1518*50314e55SStephen Boyd 	{ "s5", 0x2000, "vdd_s5", },
1519*50314e55SStephen Boyd 	{ "s6", 0x2300, "vdd_s6", },
1520*50314e55SStephen Boyd 	{ "s7", 0x2600, "vdd_s7", },
1521*50314e55SStephen Boyd 	{ "s8", 0x2900, "vdd_s8", },
1522*50314e55SStephen Boyd 	{ "s9", 0x2c00, "vdd_s9", },
1523*50314e55SStephen Boyd 	{ "s10", 0x2f00, "vdd_s10", },
1524*50314e55SStephen Boyd 	{ "s11", 0x3200, "vdd_s11", },
1525*50314e55SStephen Boyd 	{ "s12", 0x3500, "vdd_s12", },
1526*50314e55SStephen Boyd 	{ "l1", 0x4000, "vdd_l1", },
1527*50314e55SStephen Boyd 	{ "l2", 0x4100, "vdd_l2_l26_l28", },
1528*50314e55SStephen Boyd 	{ "l3", 0x4200, "vdd_l3_l11", },
1529*50314e55SStephen Boyd 	{ "l4", 0x4300, "vdd_l4_l27_l31", },
1530*50314e55SStephen Boyd 	{ "l5", 0x4400, "vdd_l5_l7", },
1531*50314e55SStephen Boyd 	{ "l6", 0x4500, "vdd_l6_l12_l32", },
1532*50314e55SStephen Boyd 	{ "l7", 0x4600, "vdd_l5_l7", },
1533*50314e55SStephen Boyd 	{ "l8", 0x4700, "vdd_l8_l16_l30", },
1534*50314e55SStephen Boyd 	{ "l9", 0x4800, "vdd_l9_l10_l18_l22", },
1535*50314e55SStephen Boyd 	{ "l10", 0x4900, "vdd_l9_l10_l18_l22", },
1536*50314e55SStephen Boyd 	{ "l11", 0x4a00, "vdd_l3_l11", },
1537*50314e55SStephen Boyd 	{ "l12", 0x4b00, "vdd_l6_l12_l32", },
1538*50314e55SStephen Boyd 	{ "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
1539*50314e55SStephen Boyd 	{ "l14", 0x4d00, "vdd_l14_l15", },
1540*50314e55SStephen Boyd 	{ "l15", 0x4e00, "vdd_l14_l15", },
1541*50314e55SStephen Boyd 	{ "l16", 0x4f00, "vdd_l8_l16_l30", },
1542*50314e55SStephen Boyd 	{ "l17", 0x5000, "vdd_l17_l29", },
1543*50314e55SStephen Boyd 	{ "l18", 0x5100, "vdd_l9_l10_l18_l22", },
1544*50314e55SStephen Boyd 	{ "l19", 0x5200, "vdd_l13_l19_l23_l24", },
1545*50314e55SStephen Boyd 	{ "l20", 0x5300, "vdd_l20_l21", },
1546*50314e55SStephen Boyd 	{ "l21", 0x5400, "vdd_l20_l21", },
1547*50314e55SStephen Boyd 	{ "l22", 0x5500, "vdd_l9_l10_l18_l22", },
1548*50314e55SStephen Boyd 	{ "l23", 0x5600, "vdd_l13_l19_l23_l24", },
1549*50314e55SStephen Boyd 	{ "l24", 0x5700, "vdd_l13_l19_l23_l24", },
1550*50314e55SStephen Boyd 	{ "l25", 0x5800, "vdd_l25", },
1551*50314e55SStephen Boyd 	{ "l26", 0x5900, "vdd_l2_l26_l28", },
1552*50314e55SStephen Boyd 	{ "l27", 0x5a00, "vdd_l4_l27_l31", },
1553*50314e55SStephen Boyd 	{ "l28", 0x5b00, "vdd_l2_l26_l28", },
1554*50314e55SStephen Boyd 	{ "l29", 0x5c00, "vdd_l17_l29", },
1555*50314e55SStephen Boyd 	{ "l30", 0x5d00, "vdd_l8_l16_l30", },
1556*50314e55SStephen Boyd 	{ "l31", 0x5e00, "vdd_l4_l27_l31", },
1557*50314e55SStephen Boyd 	{ "l32", 0x5f00, "vdd_l6_l12_l32", },
1558*50314e55SStephen Boyd 	{ "lvs1", 0x8000, "vdd_lvs_1_2", },
1559*50314e55SStephen Boyd 	{ "lvs2", 0x8100, "vdd_lvs_1_2", },
1560*50314e55SStephen Boyd 	{ }
1561*50314e55SStephen Boyd };
1562*50314e55SStephen Boyd 
1563e92a4047SStephen Boyd static const struct of_device_id qcom_spmi_regulator_match[] = {
1564e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators },
1565e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators },
1566e92a4047SStephen Boyd 	{ .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators },
1567*50314e55SStephen Boyd 	{ .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators },
1568e92a4047SStephen Boyd 	{ }
1569e92a4047SStephen Boyd };
1570e92a4047SStephen Boyd MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match);
1571e92a4047SStephen Boyd 
1572e92a4047SStephen Boyd static int qcom_spmi_regulator_probe(struct platform_device *pdev)
1573e92a4047SStephen Boyd {
1574e92a4047SStephen Boyd 	const struct spmi_regulator_data *reg;
1575e92a4047SStephen Boyd 	const struct of_device_id *match;
1576e92a4047SStephen Boyd 	struct regulator_config config = { };
1577e92a4047SStephen Boyd 	struct regulator_dev *rdev;
1578e92a4047SStephen Boyd 	struct spmi_regulator *vreg;
1579e92a4047SStephen Boyd 	struct regmap *regmap;
1580e92a4047SStephen Boyd 	const char *name;
1581e92a4047SStephen Boyd 	struct device *dev = &pdev->dev;
1582e92a4047SStephen Boyd 	int ret;
1583e92a4047SStephen Boyd 	struct list_head *vreg_list;
1584e92a4047SStephen Boyd 
1585e92a4047SStephen Boyd 	vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL);
1586e92a4047SStephen Boyd 	if (!vreg_list)
1587e92a4047SStephen Boyd 		return -ENOMEM;
1588e92a4047SStephen Boyd 	INIT_LIST_HEAD(vreg_list);
1589e92a4047SStephen Boyd 	platform_set_drvdata(pdev, vreg_list);
1590e92a4047SStephen Boyd 
1591e92a4047SStephen Boyd 	regmap = dev_get_regmap(dev->parent, NULL);
1592e92a4047SStephen Boyd 	if (!regmap)
1593e92a4047SStephen Boyd 		return -ENODEV;
1594e92a4047SStephen Boyd 
1595e92a4047SStephen Boyd 	match = of_match_device(qcom_spmi_regulator_match, &pdev->dev);
1596e92a4047SStephen Boyd 	if (!match)
1597e92a4047SStephen Boyd 		return -ENODEV;
1598e92a4047SStephen Boyd 
1599e92a4047SStephen Boyd 	for (reg = match->data; reg->name; reg++) {
1600e92a4047SStephen Boyd 		vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1601e92a4047SStephen Boyd 		if (!vreg)
1602e92a4047SStephen Boyd 			return -ENOMEM;
1603e92a4047SStephen Boyd 
1604e92a4047SStephen Boyd 		vreg->dev = dev;
1605e92a4047SStephen Boyd 		vreg->base = reg->base;
1606e92a4047SStephen Boyd 		vreg->regmap = regmap;
1607e92a4047SStephen Boyd 
1608e92a4047SStephen Boyd 		if (reg->ocp) {
1609e92a4047SStephen Boyd 			vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp);
1610e92a4047SStephen Boyd 			if (vreg->ocp_irq < 0) {
1611e92a4047SStephen Boyd 				ret = vreg->ocp_irq;
1612e92a4047SStephen Boyd 				goto err;
1613e92a4047SStephen Boyd 			}
1614e92a4047SStephen Boyd 		}
1615e92a4047SStephen Boyd 
1616e92a4047SStephen Boyd 		vreg->desc.id = -1;
1617e92a4047SStephen Boyd 		vreg->desc.owner = THIS_MODULE;
1618e92a4047SStephen Boyd 		vreg->desc.type = REGULATOR_VOLTAGE;
1619e92a4047SStephen Boyd 		vreg->desc.name = name = reg->name;
1620e92a4047SStephen Boyd 		vreg->desc.supply_name = reg->supply;
1621e92a4047SStephen Boyd 		vreg->desc.of_match = reg->name;
1622e92a4047SStephen Boyd 		vreg->desc.of_parse_cb = spmi_regulator_of_parse;
1623e92a4047SStephen Boyd 		vreg->desc.of_map_mode = spmi_regulator_of_map_mode;
1624e92a4047SStephen Boyd 
1625e92a4047SStephen Boyd 		ret = spmi_regulator_match(vreg, reg->force_type);
1626e92a4047SStephen Boyd 		if (ret)
1627e92a4047SStephen Boyd 			goto err;
1628e92a4047SStephen Boyd 
1629e92a4047SStephen Boyd 		config.dev = dev;
1630e92a4047SStephen Boyd 		config.driver_data = vreg;
1631e92a4047SStephen Boyd 		rdev = devm_regulator_register(dev, &vreg->desc, &config);
1632e92a4047SStephen Boyd 		if (IS_ERR(rdev)) {
1633e92a4047SStephen Boyd 			dev_err(dev, "failed to register %s\n", name);
1634e92a4047SStephen Boyd 			ret = PTR_ERR(rdev);
1635e92a4047SStephen Boyd 			goto err;
1636e92a4047SStephen Boyd 		}
1637e92a4047SStephen Boyd 
1638e92a4047SStephen Boyd 		INIT_LIST_HEAD(&vreg->node);
1639e92a4047SStephen Boyd 		list_add(&vreg->node, vreg_list);
1640e92a4047SStephen Boyd 	}
1641e92a4047SStephen Boyd 
1642e92a4047SStephen Boyd 	return 0;
1643e92a4047SStephen Boyd 
1644e92a4047SStephen Boyd err:
1645e92a4047SStephen Boyd 	list_for_each_entry(vreg, vreg_list, node)
1646e92a4047SStephen Boyd 		if (vreg->ocp_irq)
1647e92a4047SStephen Boyd 			cancel_delayed_work_sync(&vreg->ocp_work);
1648e92a4047SStephen Boyd 	return ret;
1649e92a4047SStephen Boyd }
1650e92a4047SStephen Boyd 
1651e92a4047SStephen Boyd static int qcom_spmi_regulator_remove(struct platform_device *pdev)
1652e92a4047SStephen Boyd {
1653e92a4047SStephen Boyd 	struct spmi_regulator *vreg;
1654e92a4047SStephen Boyd 	struct list_head *vreg_list = platform_get_drvdata(pdev);
1655e92a4047SStephen Boyd 
1656e92a4047SStephen Boyd 	list_for_each_entry(vreg, vreg_list, node)
1657e92a4047SStephen Boyd 		if (vreg->ocp_irq)
1658e92a4047SStephen Boyd 			cancel_delayed_work_sync(&vreg->ocp_work);
1659e92a4047SStephen Boyd 
1660e92a4047SStephen Boyd 	return 0;
1661e92a4047SStephen Boyd }
1662e92a4047SStephen Boyd 
1663e92a4047SStephen Boyd static struct platform_driver qcom_spmi_regulator_driver = {
1664e92a4047SStephen Boyd 	.driver		= {
1665e92a4047SStephen Boyd 		.name	= "qcom-spmi-regulator",
1666e92a4047SStephen Boyd 		.of_match_table = qcom_spmi_regulator_match,
1667e92a4047SStephen Boyd 	},
1668e92a4047SStephen Boyd 	.probe		= qcom_spmi_regulator_probe,
1669e92a4047SStephen Boyd 	.remove		= qcom_spmi_regulator_remove,
1670e92a4047SStephen Boyd };
1671e92a4047SStephen Boyd module_platform_driver(qcom_spmi_regulator_driver);
1672e92a4047SStephen Boyd 
1673e92a4047SStephen Boyd MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
1674e92a4047SStephen Boyd MODULE_LICENSE("GPL v2");
1675e92a4047SStephen Boyd MODULE_ALIAS("platform:qcom-spmi-regulator");
1676