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