xref: /linux/drivers/regulator/tps6594-regulator.c (revision bf977a9ad33d204c8ca646cef83184eb364820ff)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Regulator driver for tps6594 PMIC
4 //
5 // Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
6 
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
18 
19 #include <linux/mfd/tps6594.h>
20 
21 #define BUCK_NB			5
22 #define LDO_NB			4
23 #define MULTI_PHASE_NB		4
24 
25 enum tps6594_regulator_id {
26 	/* DCDC's */
27 	TPS6594_BUCK_1,
28 	TPS6594_BUCK_2,
29 	TPS6594_BUCK_3,
30 	TPS6594_BUCK_4,
31 	TPS6594_BUCK_5,
32 
33 	/* LDOs */
34 	TPS6594_LDO_1,
35 	TPS6594_LDO_2,
36 	TPS6594_LDO_3,
37 	TPS6594_LDO_4,
38 };
39 
40 enum tps6594_multi_regulator_id {
41 	/* Multi-phase DCDC's */
42 	TPS6594_BUCK_12,
43 	TPS6594_BUCK_34,
44 	TPS6594_BUCK_123,
45 	TPS6594_BUCK_1234,
46 };
47 
48 struct tps6594_regulator_irq_type {
49 	const char *irq_name;
50 	const char *regulator_name;
51 	const char *event_name;
52 	unsigned long event;
53 };
54 
55 static const struct tps6594_regulator_irq_type tps6594_ext_regulator_irq_types[] = {
56 	{ TPS6594_IRQ_NAME_VCCA_OV, "VCCA", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
57 	{ TPS6594_IRQ_NAME_VCCA_UV, "VCCA", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
58 	{ TPS6594_IRQ_NAME_VMON1_OV, "VMON1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
59 	{ TPS6594_IRQ_NAME_VMON1_UV, "VMON1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
60 	{ TPS6594_IRQ_NAME_VMON1_RV, "VMON1", "residual voltage",
61 	  REGULATOR_EVENT_OVER_VOLTAGE_WARN },
62 	{ TPS6594_IRQ_NAME_VMON2_OV, "VMON2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
63 	{ TPS6594_IRQ_NAME_VMON2_UV, "VMON2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
64 	{ TPS6594_IRQ_NAME_VMON2_RV, "VMON2", "residual voltage",
65 	  REGULATOR_EVENT_OVER_VOLTAGE_WARN },
66 };
67 
68 static const struct tps6594_regulator_irq_type tps65224_ext_regulator_irq_types[] = {
69 	{ TPS65224_IRQ_NAME_VCCA_UVOV, "VCCA", "voltage out of range",
70 	  REGULATOR_EVENT_REGULATION_OUT },
71 	{ TPS65224_IRQ_NAME_VMON1_UVOV, "VMON1", "voltage out of range",
72 	  REGULATOR_EVENT_REGULATION_OUT },
73 	{ TPS65224_IRQ_NAME_VMON2_UVOV, "VMON2", "voltage out of range",
74 	  REGULATOR_EVENT_REGULATION_OUT },
75 };
76 
77 struct tps6594_regulator_irq_data {
78 	struct device *dev;
79 	const struct tps6594_regulator_irq_type *type;
80 	struct regulator_dev *rdev;
81 };
82 
83 struct tps6594_ext_regulator_irq_data {
84 	struct device *dev;
85 	const struct tps6594_regulator_irq_type *type;
86 };
87 
88 #define TPS6594_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
89 			   _em, _cr, _cm, _lr, _nlr, _delay, _fuv, \
90 			   _ct, _ncl, _bpm) \
91 	{								\
92 		.name			= _name,			\
93 		.of_match		= _of,				\
94 		.regulators_node	= of_match_ptr("regulators"),	\
95 		.supply_name		= _of,				\
96 		.id			= _id,				\
97 		.ops			= &(_ops),			\
98 		.n_voltages		= _n,				\
99 		.type			= _type,			\
100 		.owner			= THIS_MODULE,			\
101 		.vsel_reg		= _vr,				\
102 		.vsel_mask		= _vm,				\
103 		.csel_reg		= _cr,				\
104 		.csel_mask		= _cm,				\
105 		.curr_table		= _ct,				\
106 		.n_current_limits	= _ncl,				\
107 		.enable_reg		= _er,				\
108 		.enable_mask		= _em,				\
109 		.volt_table		= NULL,				\
110 		.linear_ranges		= _lr,				\
111 		.n_linear_ranges	= _nlr,				\
112 		.ramp_delay		= _delay,			\
113 		.fixed_uV		= _fuv,				\
114 		.bypass_reg		= _vr,				\
115 		.bypass_mask		= _bpm,				\
116 	}								\
117 
118 static const struct linear_range bucks_ranges[] = {
119 	REGULATOR_LINEAR_RANGE(300000, 0x0, 0xe, 20000),
120 	REGULATOR_LINEAR_RANGE(600000, 0xf, 0x72, 5000),
121 	REGULATOR_LINEAR_RANGE(1100000, 0x73, 0xaa, 10000),
122 	REGULATOR_LINEAR_RANGE(1660000, 0xab, 0xff, 20000),
123 };
124 
125 static const struct linear_range ldos_1_2_3_ranges[] = {
126 	REGULATOR_LINEAR_RANGE(600000, 0x4, 0x3a, 50000),
127 };
128 
129 static const struct linear_range ldos_4_ranges[] = {
130 	REGULATOR_LINEAR_RANGE(1200000, 0x20, 0x74, 25000),
131 };
132 
133 /* Voltage range for TPS65224 Bucks and LDOs */
134 static const struct linear_range tps65224_bucks_1_ranges[] = {
135 	REGULATOR_LINEAR_RANGE(500000, 0x0a, 0x0e, 20000),
136 	REGULATOR_LINEAR_RANGE(600000, 0x0f, 0x72, 5000),
137 	REGULATOR_LINEAR_RANGE(1100000, 0x73, 0xaa, 10000),
138 	REGULATOR_LINEAR_RANGE(1660000, 0xab, 0xfd, 20000),
139 };
140 
141 static const struct linear_range tps65224_bucks_2_3_4_ranges[] = {
142 	REGULATOR_LINEAR_RANGE(500000, 0x0, 0x1a, 25000),
143 	REGULATOR_LINEAR_RANGE(1200000, 0x1b, 0x45, 50000),
144 };
145 
146 static const struct linear_range tps65224_ldos_1_ranges[] = {
147 	REGULATOR_LINEAR_RANGE(1200000, 0xC, 0x36, 50000),
148 };
149 
150 static const struct linear_range tps65224_ldos_2_3_ranges[] = {
151 	REGULATOR_LINEAR_RANGE(600000, 0x0, 0x38, 50000),
152 };
153 
154 /* Operations permitted on BUCK1/2/3/4/5 */
155 static const struct regulator_ops tps6594_bucks_ops = {
156 	.is_enabled		= regulator_is_enabled_regmap,
157 	.enable			= regulator_enable_regmap,
158 	.disable		= regulator_disable_regmap,
159 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
160 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
161 	.list_voltage		= regulator_list_voltage_linear_range,
162 	.map_voltage		= regulator_map_voltage_linear_range,
163 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
164 
165 };
166 
167 /* Operations permitted on LDO1/2/3 */
168 static const struct regulator_ops tps6594_ldos_1_2_3_ops = {
169 	.is_enabled		= regulator_is_enabled_regmap,
170 	.enable			= regulator_enable_regmap,
171 	.disable		= regulator_disable_regmap,
172 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
173 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
174 	.list_voltage		= regulator_list_voltage_linear_range,
175 	.map_voltage		= regulator_map_voltage_linear_range,
176 	.set_bypass		= regulator_set_bypass_regmap,
177 	.get_bypass		= regulator_get_bypass_regmap,
178 };
179 
180 /* Operations permitted on LDO4 */
181 static const struct regulator_ops tps6594_ldos_4_ops = {
182 	.is_enabled		= regulator_is_enabled_regmap,
183 	.enable			= regulator_enable_regmap,
184 	.disable		= regulator_disable_regmap,
185 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
186 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
187 	.list_voltage		= regulator_list_voltage_linear_range,
188 	.map_voltage		= regulator_map_voltage_linear_range,
189 };
190 
191 static const struct regulator_desc tps6594_buck_regs[] = {
192 	TPS6594_REGULATOR("BUCK1", "buck1", TPS6594_BUCK_1,
193 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
194 			  TPS6594_REG_BUCKX_VOUT_1(0),
195 			  TPS6594_MASK_BUCKS_VSET,
196 			  TPS6594_REG_BUCKX_CTRL(0),
197 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
198 			  4, 0, 0, NULL, 0, 0),
199 	TPS6594_REGULATOR("BUCK2", "buck2", TPS6594_BUCK_2,
200 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
201 			  TPS6594_REG_BUCKX_VOUT_1(1),
202 			  TPS6594_MASK_BUCKS_VSET,
203 			  TPS6594_REG_BUCKX_CTRL(1),
204 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
205 			  4, 0, 0, NULL, 0, 0),
206 	TPS6594_REGULATOR("BUCK3", "buck3", TPS6594_BUCK_3,
207 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
208 			  TPS6594_REG_BUCKX_VOUT_1(2),
209 			  TPS6594_MASK_BUCKS_VSET,
210 			  TPS6594_REG_BUCKX_CTRL(2),
211 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
212 			  4, 0, 0, NULL, 0, 0),
213 	TPS6594_REGULATOR("BUCK4", "buck4", TPS6594_BUCK_4,
214 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
215 			  TPS6594_REG_BUCKX_VOUT_1(3),
216 			  TPS6594_MASK_BUCKS_VSET,
217 			  TPS6594_REG_BUCKX_CTRL(3),
218 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
219 			  4, 0, 0, NULL, 0, 0),
220 	TPS6594_REGULATOR("BUCK5", "buck5", TPS6594_BUCK_5,
221 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
222 			  TPS6594_REG_BUCKX_VOUT_1(4),
223 			  TPS6594_MASK_BUCKS_VSET,
224 			  TPS6594_REG_BUCKX_CTRL(4),
225 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
226 			  4, 0, 0, NULL, 0, 0),
227 };
228 
229 /* Buck configuration for TPS65224 */
230 static const struct regulator_desc tps65224_buck_regs[] = {
231 	TPS6594_REGULATOR("BUCK1", "buck1", TPS6594_BUCK_1,
232 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCK1_VSET,
233 			  TPS6594_REG_BUCKX_VOUT_1(0),
234 			  TPS65224_MASK_BUCK1_VSET,
235 			  TPS6594_REG_BUCKX_CTRL(0),
236 			  TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_1_ranges,
237 			  4, 0, 0, NULL, 0, 0),
238 	TPS6594_REGULATOR("BUCK2", "buck2", TPS6594_BUCK_2,
239 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
240 			  TPS6594_REG_BUCKX_VOUT_1(1),
241 			  TPS65224_MASK_BUCKS_VSET,
242 			  TPS6594_REG_BUCKX_CTRL(1),
243 			  TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
244 			  4, 0, 0, NULL, 0, 0),
245 	TPS6594_REGULATOR("BUCK3", "buck3", TPS6594_BUCK_3,
246 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
247 			  TPS6594_REG_BUCKX_VOUT_1(2),
248 			  TPS65224_MASK_BUCKS_VSET,
249 			  TPS6594_REG_BUCKX_CTRL(2),
250 			  TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
251 			  4, 0, 0, NULL, 0, 0),
252 	TPS6594_REGULATOR("BUCK4", "buck4", TPS6594_BUCK_4,
253 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCKS_VSET,
254 			  TPS6594_REG_BUCKX_VOUT_1(3),
255 			  TPS65224_MASK_BUCKS_VSET,
256 			  TPS6594_REG_BUCKX_CTRL(3),
257 			  TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_2_3_4_ranges,
258 			  4, 0, 0, NULL, 0, 0),
259 };
260 
261 static const struct tps6594_regulator_irq_type tps6594_buck1_irq_types[] = {
262 	{ TPS6594_IRQ_NAME_BUCK1_OV, "BUCK1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
263 	{ TPS6594_IRQ_NAME_BUCK1_UV, "BUCK1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
264 	{ TPS6594_IRQ_NAME_BUCK1_SC, "BUCK1", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
265 	{ TPS6594_IRQ_NAME_BUCK1_ILIM, "BUCK1", "reach ilim, overcurrent",
266 	  REGULATOR_EVENT_OVER_CURRENT },
267 };
268 
269 static const struct tps6594_regulator_irq_type tps6594_buck2_irq_types[] = {
270 	{ TPS6594_IRQ_NAME_BUCK2_OV, "BUCK2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
271 	{ TPS6594_IRQ_NAME_BUCK2_UV, "BUCK2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
272 	{ TPS6594_IRQ_NAME_BUCK2_SC, "BUCK2", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
273 	{ TPS6594_IRQ_NAME_BUCK2_ILIM, "BUCK2", "reach ilim, overcurrent",
274 	  REGULATOR_EVENT_OVER_CURRENT },
275 };
276 
277 static const struct tps6594_regulator_irq_type tps6594_buck3_irq_types[] = {
278 	{ TPS6594_IRQ_NAME_BUCK3_OV, "BUCK3", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
279 	{ TPS6594_IRQ_NAME_BUCK3_UV, "BUCK3", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
280 	{ TPS6594_IRQ_NAME_BUCK3_SC, "BUCK3", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
281 	{ TPS6594_IRQ_NAME_BUCK3_ILIM, "BUCK3", "reach ilim, overcurrent",
282 	  REGULATOR_EVENT_OVER_CURRENT },
283 };
284 
285 static const struct tps6594_regulator_irq_type tps6594_buck4_irq_types[] = {
286 	{ TPS6594_IRQ_NAME_BUCK4_OV, "BUCK4", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
287 	{ TPS6594_IRQ_NAME_BUCK4_UV, "BUCK4", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
288 	{ TPS6594_IRQ_NAME_BUCK4_SC, "BUCK4", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
289 	{ TPS6594_IRQ_NAME_BUCK4_ILIM, "BUCK4", "reach ilim, overcurrent",
290 	  REGULATOR_EVENT_OVER_CURRENT },
291 };
292 
293 static const struct tps6594_regulator_irq_type tps6594_buck5_irq_types[] = {
294 	{ TPS6594_IRQ_NAME_BUCK5_OV, "BUCK5", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
295 	{ TPS6594_IRQ_NAME_BUCK5_UV, "BUCK5", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
296 	{ TPS6594_IRQ_NAME_BUCK5_SC, "BUCK5", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
297 	{ TPS6594_IRQ_NAME_BUCK5_ILIM, "BUCK5", "reach ilim, overcurrent",
298 	  REGULATOR_EVENT_OVER_CURRENT },
299 };
300 
301 static const struct tps6594_regulator_irq_type tps6594_ldo1_irq_types[] = {
302 	{ TPS6594_IRQ_NAME_LDO1_OV, "LDO1", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
303 	{ TPS6594_IRQ_NAME_LDO1_UV, "LDO1", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
304 	{ TPS6594_IRQ_NAME_LDO1_SC, "LDO1", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
305 	{ TPS6594_IRQ_NAME_LDO1_ILIM, "LDO1", "reach ilim, overcurrent",
306 	  REGULATOR_EVENT_OVER_CURRENT },
307 };
308 
309 static const struct tps6594_regulator_irq_type tps6594_ldo2_irq_types[] = {
310 	{ TPS6594_IRQ_NAME_LDO2_OV, "LDO2", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
311 	{ TPS6594_IRQ_NAME_LDO2_UV, "LDO2", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
312 	{ TPS6594_IRQ_NAME_LDO2_SC, "LDO2", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
313 	{ TPS6594_IRQ_NAME_LDO2_ILIM, "LDO2", "reach ilim, overcurrent",
314 	  REGULATOR_EVENT_OVER_CURRENT },
315 };
316 
317 static const struct tps6594_regulator_irq_type tps6594_ldo3_irq_types[] = {
318 	{ TPS6594_IRQ_NAME_LDO3_OV, "LDO3", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
319 	{ TPS6594_IRQ_NAME_LDO3_UV, "LDO3", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
320 	{ TPS6594_IRQ_NAME_LDO3_SC, "LDO3", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
321 	{ TPS6594_IRQ_NAME_LDO3_ILIM, "LDO3", "reach ilim, overcurrent",
322 	  REGULATOR_EVENT_OVER_CURRENT },
323 };
324 
325 static const struct tps6594_regulator_irq_type tps6594_ldo4_irq_types[] = {
326 	{ TPS6594_IRQ_NAME_LDO4_OV, "LDO4", "overvoltage", REGULATOR_EVENT_OVER_VOLTAGE_WARN },
327 	{ TPS6594_IRQ_NAME_LDO4_UV, "LDO4", "undervoltage", REGULATOR_EVENT_UNDER_VOLTAGE },
328 	{ TPS6594_IRQ_NAME_LDO4_SC, "LDO4", "short circuit", REGULATOR_EVENT_REGULATION_OUT },
329 	{ TPS6594_IRQ_NAME_LDO4_ILIM, "LDO4", "reach ilim, overcurrent",
330 	  REGULATOR_EVENT_OVER_CURRENT },
331 };
332 
333 static const struct tps6594_regulator_irq_type tps65224_buck1_irq_types[] = {
334 	{ TPS65224_IRQ_NAME_BUCK1_UVOV, "BUCK1", "voltage out of range",
335 	  REGULATOR_EVENT_REGULATION_OUT },
336 };
337 
338 static const struct tps6594_regulator_irq_type tps65224_buck2_irq_types[] = {
339 	{ TPS65224_IRQ_NAME_BUCK2_UVOV, "BUCK2", "voltage out of range",
340 	  REGULATOR_EVENT_REGULATION_OUT },
341 };
342 
343 static const struct tps6594_regulator_irq_type tps65224_buck3_irq_types[] = {
344 	{ TPS65224_IRQ_NAME_BUCK3_UVOV, "BUCK3", "voltage out of range",
345 	  REGULATOR_EVENT_REGULATION_OUT },
346 };
347 
348 static const struct tps6594_regulator_irq_type tps65224_buck4_irq_types[] = {
349 	{ TPS65224_IRQ_NAME_BUCK4_UVOV, "BUCK4", "voltage out of range",
350 	  REGULATOR_EVENT_REGULATION_OUT },
351 };
352 
353 static const struct tps6594_regulator_irq_type tps65224_ldo1_irq_types[] = {
354 	{ TPS65224_IRQ_NAME_LDO1_UVOV, "LDO1", "voltage out of range",
355 	  REGULATOR_EVENT_REGULATION_OUT },
356 };
357 
358 static const struct tps6594_regulator_irq_type tps65224_ldo2_irq_types[] = {
359 	{ TPS65224_IRQ_NAME_LDO2_UVOV, "LDO2", "voltage out of range",
360 	  REGULATOR_EVENT_REGULATION_OUT },
361 };
362 
363 static const struct tps6594_regulator_irq_type tps65224_ldo3_irq_types[] = {
364 	{ TPS65224_IRQ_NAME_LDO3_UVOV, "LDO3", "voltage out of range",
365 	  REGULATOR_EVENT_REGULATION_OUT },
366 };
367 
368 static const struct tps6594_regulator_irq_type *tps6594_bucks_irq_types[] = {
369 	tps6594_buck1_irq_types,
370 	tps6594_buck2_irq_types,
371 	tps6594_buck3_irq_types,
372 	tps6594_buck4_irq_types,
373 	tps6594_buck5_irq_types,
374 };
375 
376 static const struct tps6594_regulator_irq_type *tps6594_ldos_irq_types[] = {
377 	tps6594_ldo1_irq_types,
378 	tps6594_ldo2_irq_types,
379 	tps6594_ldo3_irq_types,
380 	tps6594_ldo4_irq_types,
381 };
382 
383 static const struct tps6594_regulator_irq_type *tps65224_bucks_irq_types[] = {
384 	tps65224_buck1_irq_types,
385 	tps65224_buck2_irq_types,
386 	tps65224_buck3_irq_types,
387 	tps65224_buck4_irq_types,
388 };
389 
390 static const struct tps6594_regulator_irq_type *tps65224_ldos_irq_types[] = {
391 	tps65224_ldo1_irq_types,
392 	tps65224_ldo2_irq_types,
393 	tps65224_ldo3_irq_types,
394 };
395 
396 static const struct regulator_desc tps6594_multi_regs[] = {
397 	TPS6594_REGULATOR("BUCK12", "buck12", TPS6594_BUCK_1,
398 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
399 			  TPS6594_REG_BUCKX_VOUT_1(0),
400 			  TPS6594_MASK_BUCKS_VSET,
401 			  TPS6594_REG_BUCKX_CTRL(0),
402 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
403 			  4, 4000, 0, NULL, 0, 0),
404 	TPS6594_REGULATOR("BUCK34", "buck34", TPS6594_BUCK_3,
405 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
406 			  TPS6594_REG_BUCKX_VOUT_1(2),
407 			  TPS6594_MASK_BUCKS_VSET,
408 			  TPS6594_REG_BUCKX_CTRL(2),
409 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
410 			  4, 0, 0, NULL, 0, 0),
411 	TPS6594_REGULATOR("BUCK123", "buck123", TPS6594_BUCK_1,
412 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
413 			  TPS6594_REG_BUCKX_VOUT_1(0),
414 			  TPS6594_MASK_BUCKS_VSET,
415 			  TPS6594_REG_BUCKX_CTRL(0),
416 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
417 			  4, 4000, 0, NULL, 0, 0),
418 	TPS6594_REGULATOR("BUCK1234", "buck1234", TPS6594_BUCK_1,
419 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS6594_MASK_BUCKS_VSET,
420 			  TPS6594_REG_BUCKX_VOUT_1(0),
421 			  TPS6594_MASK_BUCKS_VSET,
422 			  TPS6594_REG_BUCKX_CTRL(0),
423 			  TPS6594_BIT_BUCK_EN, 0, 0, bucks_ranges,
424 			  4, 4000, 0, NULL, 0, 0),
425 };
426 
427 static const struct regulator_desc tps65224_multi_regs[] = {
428 	TPS6594_REGULATOR("BUCK12", "buck12", TPS6594_BUCK_1,
429 			  REGULATOR_VOLTAGE, tps6594_bucks_ops, TPS65224_MASK_BUCK1_VSET,
430 			  TPS6594_REG_BUCKX_VOUT_1(0),
431 			  TPS65224_MASK_BUCK1_VSET,
432 			  TPS6594_REG_BUCKX_CTRL(0),
433 			  TPS6594_BIT_BUCK_EN, 0, 0, tps65224_bucks_1_ranges,
434 			  4, 4000, 0, NULL, 0, 0),
435 };
436 
437 static const struct regulator_desc tps6594_ldo_regs[] = {
438 	TPS6594_REGULATOR("LDO1", "ldo1", TPS6594_LDO_1,
439 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
440 			  TPS6594_REG_LDOX_VOUT(0),
441 			  TPS6594_MASK_LDO123_VSET,
442 			  TPS6594_REG_LDOX_CTRL(0),
443 			  TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
444 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
445 	TPS6594_REGULATOR("LDO2", "ldo2", TPS6594_LDO_2,
446 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
447 			  TPS6594_REG_LDOX_VOUT(1),
448 			  TPS6594_MASK_LDO123_VSET,
449 			  TPS6594_REG_LDOX_CTRL(1),
450 			  TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
451 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
452 	TPS6594_REGULATOR("LDO3", "ldo3", TPS6594_LDO_3,
453 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
454 			  TPS6594_REG_LDOX_VOUT(2),
455 			  TPS6594_MASK_LDO123_VSET,
456 			  TPS6594_REG_LDOX_CTRL(2),
457 			  TPS6594_BIT_LDO_EN, 0, 0, ldos_1_2_3_ranges,
458 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
459 	TPS6594_REGULATOR("LDO4", "ldo4", TPS6594_LDO_4,
460 			  REGULATOR_VOLTAGE, tps6594_ldos_4_ops, TPS6594_MASK_LDO4_VSET >> 1,
461 			  TPS6594_REG_LDOX_VOUT(3),
462 			  TPS6594_MASK_LDO4_VSET,
463 			  TPS6594_REG_LDOX_CTRL(3),
464 			  TPS6594_BIT_LDO_EN, 0, 0, ldos_4_ranges,
465 			  1, 0, 0, NULL, 0, 0),
466 };
467 
468 static const struct regulator_desc tps65224_ldo_regs[] = {
469 	TPS6594_REGULATOR("LDO1", "ldo1", TPS6594_LDO_1,
470 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
471 			  TPS6594_REG_LDOX_VOUT(0),
472 			  TPS6594_MASK_LDO123_VSET,
473 			  TPS6594_REG_LDOX_CTRL(0),
474 			  TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_1_ranges,
475 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
476 	TPS6594_REGULATOR("LDO2", "ldo2", TPS6594_LDO_2,
477 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
478 			  TPS6594_REG_LDOX_VOUT(1),
479 			  TPS6594_MASK_LDO123_VSET,
480 			  TPS6594_REG_LDOX_CTRL(1),
481 			  TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_2_3_ranges,
482 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
483 	TPS6594_REGULATOR("LDO3", "ldo3", TPS6594_LDO_3,
484 			  REGULATOR_VOLTAGE, tps6594_ldos_1_2_3_ops, TPS6594_MASK_LDO123_VSET,
485 			  TPS6594_REG_LDOX_VOUT(2),
486 			  TPS6594_MASK_LDO123_VSET,
487 			  TPS6594_REG_LDOX_CTRL(2),
488 			  TPS6594_BIT_LDO_EN, 0, 0, tps65224_ldos_2_3_ranges,
489 			  1, 0, 0, NULL, 0, TPS6594_BIT_LDO_BYPASS),
490 };
491 
tps6594_regulator_irq_handler(int irq,void * data)492 static irqreturn_t tps6594_regulator_irq_handler(int irq, void *data)
493 {
494 	struct tps6594_regulator_irq_data *irq_data = data;
495 
496 	if (irq_data->type->event_name[0] == '\0') {
497 		/* This is the timeout interrupt no specific regulator */
498 		dev_err(irq_data->dev,
499 			"System was put in shutdown due to timeout during an active or standby transition.\n");
500 		return IRQ_HANDLED;
501 	}
502 
503 	dev_err(irq_data->dev, "Error IRQ trap %s for %s\n",
504 		irq_data->type->event_name, irq_data->type->regulator_name);
505 
506 	regulator_notifier_call_chain(irq_data->rdev,
507 				      irq_data->type->event, NULL);
508 
509 	return IRQ_HANDLED;
510 }
511 
tps6594_request_reg_irqs(struct platform_device * pdev,struct regulator_dev * rdev,struct tps6594_regulator_irq_data * irq_data,const struct tps6594_regulator_irq_type * regs_irq_types,size_t interrupt_cnt,int * irq_idx)512 static int tps6594_request_reg_irqs(struct platform_device *pdev,
513 				    struct regulator_dev *rdev,
514 				    struct tps6594_regulator_irq_data *irq_data,
515 				    const struct tps6594_regulator_irq_type *regs_irq_types,
516 				    size_t interrupt_cnt,
517 				    int *irq_idx)
518 {
519 	const struct tps6594_regulator_irq_type *irq_type;
520 	struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
521 	size_t j;
522 	int irq;
523 	int error;
524 
525 	for (j = 0; j < interrupt_cnt; j++) {
526 		irq_type = &regs_irq_types[j];
527 		irq = platform_get_irq_byname(pdev, irq_type->irq_name);
528 		if (irq < 0)
529 			return -EINVAL;
530 
531 		irq_data[*irq_idx].dev = tps->dev;
532 		irq_data[*irq_idx].type = irq_type;
533 		irq_data[*irq_idx].rdev = rdev;
534 
535 		error = devm_request_threaded_irq(tps->dev, irq, NULL,
536 						  tps6594_regulator_irq_handler, IRQF_ONESHOT,
537 						  irq_type->irq_name, &irq_data[*irq_idx]);
538 		if (error) {
539 			dev_err(tps->dev, "tps6594 failed to request %s IRQ %d: %d\n",
540 				irq_type->irq_name, irq, error);
541 			return error;
542 		}
543 		(*irq_idx)++;
544 	}
545 	return 0;
546 }
547 
548 struct tps6594_regulator_desc {
549 	const struct regulator_desc *multi_phase_regs;
550 	unsigned int num_multi_phase_regs;
551 
552 	const struct regulator_desc *buck_regs;
553 	int num_buck_regs;
554 
555 	const struct regulator_desc *ldo_regs;
556 	int num_ldo_regs;
557 
558 	const struct tps6594_regulator_irq_type **bucks_irq_types;
559 	const struct tps6594_regulator_irq_type **ldos_irq_types;
560 	int num_irq_types;
561 
562 	const struct tps6594_regulator_irq_type *ext_irq_types;
563 	int num_ext_irqs;
564 };
565 
566 static const struct tps6594_regulator_desc tps65224_reg_desc = {
567 	.multi_phase_regs = tps65224_multi_regs,
568 	.num_multi_phase_regs = ARRAY_SIZE(tps65224_multi_regs),
569 	.buck_regs = tps65224_buck_regs,
570 	.num_buck_regs = ARRAY_SIZE(tps65224_buck_regs),
571 	.ldo_regs = tps65224_ldo_regs,
572 	.num_ldo_regs = ARRAY_SIZE(tps65224_ldo_regs),
573 	.bucks_irq_types = tps65224_bucks_irq_types,
574 	.ldos_irq_types = tps65224_ldos_irq_types,
575 	.num_irq_types = 1, /* OV or UV */
576 	.ext_irq_types = tps65224_ext_regulator_irq_types,
577 	.num_ext_irqs = ARRAY_SIZE(tps65224_ext_regulator_irq_types),
578 };
579 
580 static const struct tps6594_regulator_desc tps652g1_reg_desc = {
581 	.ldo_regs = tps65224_ldo_regs,
582 	.num_ldo_regs = ARRAY_SIZE(tps65224_ldo_regs),
583 	.buck_regs = tps65224_buck_regs,
584 	.num_buck_regs = ARRAY_SIZE(tps65224_buck_regs),
585 };
586 
587 static const struct tps6594_regulator_desc tps6594_reg_desc = {
588 	.multi_phase_regs = tps6594_multi_regs,
589 	.num_multi_phase_regs = ARRAY_SIZE(tps6594_multi_regs),
590 	.buck_regs = tps6594_buck_regs,
591 	.num_buck_regs = ARRAY_SIZE(tps6594_buck_regs),
592 	.ldo_regs = tps6594_ldo_regs,
593 	.num_ldo_regs = ARRAY_SIZE(tps6594_ldo_regs),
594 	.bucks_irq_types = tps6594_bucks_irq_types,
595 	.ldos_irq_types = tps6594_ldos_irq_types,
596 	.num_irq_types = 4, /* OV, UV, SC and ILIM */
597 	.ext_irq_types = tps6594_ext_regulator_irq_types,
598 	.num_ext_irqs = 2, /* only VCCA OV and UV */
599 };
600 
601 static const struct tps6594_regulator_desc lp8764_reg_desc = {
602 	.multi_phase_regs = tps6594_multi_regs,
603 	.num_multi_phase_regs = ARRAY_SIZE(tps6594_multi_regs),
604 	.buck_regs = tps6594_buck_regs,
605 	.num_buck_regs = ARRAY_SIZE(tps6594_buck_regs),
606 	.bucks_irq_types = tps6594_bucks_irq_types,
607 	.num_irq_types = 4, /* OV, UV, SC and ILIM */
608 	.ext_irq_types = tps6594_ext_regulator_irq_types,
609 	.num_ext_irqs = ARRAY_SIZE(tps6594_ext_regulator_irq_types),
610 };
611 
tps6594_regulator_probe(struct platform_device * pdev)612 static int tps6594_regulator_probe(struct platform_device *pdev)
613 {
614 	struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
615 	struct regulator_dev *rdev;
616 	struct device_node *np = NULL;
617 	struct device_node *np_pmic_parent = NULL;
618 	struct regulator_config config = {};
619 	struct tps6594_regulator_irq_data *irq_data;
620 	struct tps6594_ext_regulator_irq_data *irq_ext_reg_data;
621 	const struct tps6594_regulator_irq_type *irq_type;
622 	bool buck_configured[BUCK_NB] = { false };
623 	bool buck_multi[MULTI_PHASE_NB] = { false };
624 	const struct tps6594_regulator_desc *desc;
625 	const struct regulator_desc *multi_regs;
626 
627 	const char *npname;
628 	int error, i, irq, multi;
629 	int irq_idx = 0;
630 	int buck_idx = 0;
631 	size_t reg_irq_nb;
632 
633 	switch (tps->chip_id) {
634 	case TPS65224:
635 		desc = &tps65224_reg_desc;
636 		break;
637 	case TPS652G1:
638 		desc = &tps652g1_reg_desc;
639 		break;
640 	case TPS6594:
641 	case TPS6593:
642 		desc = &tps6594_reg_desc;
643 		break;
644 	case LP8764:
645 		desc = &lp8764_reg_desc;
646 		break;
647 	default:
648 		dev_err(tps->dev, "unknown chip_id %lu\n", tps->chip_id);
649 		return -EINVAL;
650 	};
651 
652 	enum {
653 		MULTI_BUCK12,
654 		MULTI_BUCK12_34,
655 		MULTI_BUCK123,
656 		MULTI_BUCK1234,
657 	};
658 
659 	config.dev = tps->dev;
660 	config.driver_data = tps;
661 	config.regmap = tps->regmap;
662 
663 	/*
664 	 * Switch case defines different possible multi phase config
665 	 * This is based on dts buck node name.
666 	 * Buck node name must be chosen accordingly.
667 	 * Default case is no Multiphase buck.
668 	 * In case of Multiphase configuration, value should be defined for
669 	 * buck_configured to avoid creating bucks for every buck in multiphase
670 	 */
671 	for (multi = 0; multi < desc->num_multi_phase_regs; multi++) {
672 		multi_regs = &desc->multi_phase_regs[multi];
673 		np = of_find_node_by_name(tps->dev->of_node, multi_regs->supply_name);
674 		npname = of_node_full_name(np);
675 		np_pmic_parent = of_get_parent(of_get_parent(np));
676 		if (of_node_cmp(of_node_full_name(np_pmic_parent), tps->dev->of_node->full_name))
677 			continue;
678 		if (strcmp(npname, multi_regs->supply_name) == 0) {
679 			switch (multi) {
680 			case MULTI_BUCK12:
681 				buck_multi[0] = true;
682 				buck_configured[0] = true;
683 				buck_configured[1] = true;
684 				break;
685 			/* multiphase buck34 is supported only with buck12 */
686 			case MULTI_BUCK12_34:
687 				buck_multi[0] = true;
688 				buck_multi[1] = true;
689 				buck_configured[0] = true;
690 				buck_configured[1] = true;
691 				buck_configured[2] = true;
692 				buck_configured[3] = true;
693 				break;
694 			case MULTI_BUCK123:
695 				buck_multi[2] = true;
696 				buck_configured[0] = true;
697 				buck_configured[1] = true;
698 				buck_configured[2] = true;
699 				break;
700 			case MULTI_BUCK1234:
701 				buck_multi[3] = true;
702 				buck_configured[0] = true;
703 				buck_configured[1] = true;
704 				buck_configured[2] = true;
705 				buck_configured[3] = true;
706 				break;
707 			}
708 		}
709 	}
710 
711 	reg_irq_nb = desc->num_irq_types * (desc->num_buck_regs + desc->num_ldo_regs);
712 
713 	irq_data = devm_kmalloc_array(tps->dev, reg_irq_nb,
714 				      sizeof(struct tps6594_regulator_irq_data), GFP_KERNEL);
715 	if (!irq_data)
716 		return -ENOMEM;
717 
718 	for (i = 0; i < desc->num_multi_phase_regs; i++) {
719 		if (!buck_multi[i])
720 			continue;
721 
722 		rdev = devm_regulator_register(&pdev->dev, &desc->multi_phase_regs[i],
723 					       &config);
724 		if (IS_ERR(rdev))
725 			return dev_err_probe(tps->dev, PTR_ERR(rdev),
726 					     "failed to register %s regulator\n",
727 					     pdev->name);
728 
729 		if (!desc->num_irq_types)
730 			continue;
731 
732 		/* config multiphase buck12+buck34 */
733 		if (i == MULTI_BUCK12_34)
734 			buck_idx = 2;
735 
736 		error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
737 						 desc->bucks_irq_types[buck_idx],
738 						 desc->num_irq_types, &irq_idx);
739 		if (error)
740 			return error;
741 
742 		error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
743 						 desc->bucks_irq_types[buck_idx + 1],
744 						 desc->num_irq_types, &irq_idx);
745 		if (error)
746 			return error;
747 
748 		if (i == MULTI_BUCK123 || i == MULTI_BUCK1234) {
749 			error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
750 							 desc->bucks_irq_types[buck_idx + 2],
751 							 desc->num_irq_types,
752 							 &irq_idx);
753 			if (error)
754 				return error;
755 		}
756 		if (i == MULTI_BUCK1234) {
757 			error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
758 							 desc->bucks_irq_types[buck_idx + 3],
759 							 desc->num_irq_types,
760 							 &irq_idx);
761 			if (error)
762 				return error;
763 		}
764 	}
765 
766 	for (i = 0; i < desc->num_buck_regs; i++) {
767 		if (buck_configured[i])
768 			continue;
769 
770 		rdev = devm_regulator_register(&pdev->dev, &desc->buck_regs[i], &config);
771 		if (IS_ERR(rdev))
772 			return dev_err_probe(tps->dev, PTR_ERR(rdev),
773 					     "failed to register %s regulator\n", pdev->name);
774 
775 		if (!desc->num_irq_types)
776 			continue;
777 
778 		error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
779 						 desc->bucks_irq_types[i],
780 						 desc->num_irq_types, &irq_idx);
781 		if (error)
782 			return error;
783 	}
784 
785 	for (i = 0; i < desc->num_ldo_regs; i++) {
786 		rdev = devm_regulator_register(&pdev->dev, &desc->ldo_regs[i], &config);
787 		if (IS_ERR(rdev))
788 			return dev_err_probe(tps->dev, PTR_ERR(rdev),
789 					     "failed to register %s regulator\n",
790 					     pdev->name);
791 
792 		if (!desc->num_irq_types)
793 			continue;
794 
795 		error = tps6594_request_reg_irqs(pdev, rdev, irq_data,
796 						 desc->ldos_irq_types[i],
797 						 desc->num_irq_types, &irq_idx);
798 		if (error)
799 			return error;
800 	}
801 
802 	irq_ext_reg_data = devm_kmalloc_array(tps->dev,
803 					      desc->num_ext_irqs,
804 					      sizeof(struct tps6594_ext_regulator_irq_data),
805 					      GFP_KERNEL);
806 	if (!irq_ext_reg_data)
807 		return -ENOMEM;
808 
809 	for (i = 0; i < desc->num_ext_irqs; ++i) {
810 		irq_type = &desc->ext_irq_types[i];
811 		irq = platform_get_irq_byname(pdev, irq_type->irq_name);
812 		if (irq < 0)
813 			return -EINVAL;
814 
815 		irq_ext_reg_data[i].dev = tps->dev;
816 		irq_ext_reg_data[i].type = irq_type;
817 
818 		error = devm_request_threaded_irq(tps->dev, irq, NULL,
819 						  tps6594_regulator_irq_handler,
820 						  IRQF_ONESHOT,
821 						  irq_type->irq_name,
822 						  &irq_ext_reg_data[i]);
823 		if (error)
824 			return dev_err_probe(tps->dev, error,
825 					     "failed to request %s IRQ %d\n",
826 					     irq_type->irq_name, irq);
827 	}
828 
829 	return 0;
830 }
831 
832 static struct platform_driver tps6594_regulator_driver = {
833 	.driver = {
834 		.name = "tps6594-regulator",
835 	},
836 	.probe = tps6594_regulator_probe,
837 };
838 
839 module_platform_driver(tps6594_regulator_driver);
840 
841 MODULE_ALIAS("platform:tps6594-regulator");
842 MODULE_AUTHOR("Jerome Neanne <jneanne@baylibre.com>");
843 MODULE_AUTHOR("Nirmala Devi Mal Nadar <m.nirmaladevi@ltts.com>");
844 MODULE_DESCRIPTION("TPS6594 voltage regulator driver");
845 MODULE_LICENSE("GPL");
846