1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright 2014 Embest Technology Co. Ltd. Inc.
4 // bd71815-regulator.c ROHM BD71815 regulator driver
5 //
6 // Author: Tony Luo <luofc@embedinfo.com>
7 //
8 // Partially rewritten at 2021 by
9 // Matti Vaittinen <matti.vaitinen@fi.rohmeurope.com>
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/gpio.h>
20 #include <linux/mfd/rohm-generic.h>
21 #include <linux/mfd/rohm-bd71815.h>
22 #include <linux/regulator/of_regulator.h>
23 
24 struct bd71815_regulator {
25 	struct regulator_desc desc;
26 	const struct rohm_dvs_config *dvs;
27 };
28 
29 struct bd71815_pmic {
30 	struct bd71815_regulator descs[BD71815_REGULATOR_CNT];
31 	struct regmap *regmap;
32 	struct device *dev;
33 	struct gpio_descs *gps;
34 	struct regulator_dev *rdev[BD71815_REGULATOR_CNT];
35 };
36 
37 static const int bd7181x_wled_currents[] = {
38 	10, 20, 30, 50, 70, 100, 200, 300, 500, 700, 1000, 2000, 3000, 4000,
39 	5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000,
40 	16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000,
41 };
42 
43 static const struct rohm_dvs_config buck1_dvs = {
44 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
45 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
46 	.run_reg		= BD71815_REG_BUCK1_VOLT_H,
47 	.run_mask		= BD71815_VOLT_MASK,
48 	.run_on_mask		= BD71815_BUCK_RUN_ON,
49 	.snvs_on_mask		= BD71815_BUCK_SNVS_ON,
50 	.suspend_reg		= BD71815_REG_BUCK1_VOLT_L,
51 	.suspend_mask		= BD71815_VOLT_MASK,
52 	.suspend_on_mask	= BD71815_BUCK_SUSP_ON,
53 	.lpsr_reg		= BD71815_REG_BUCK1_VOLT_L,
54 	.lpsr_mask		= BD71815_VOLT_MASK,
55 	.lpsr_on_mask		= BD71815_BUCK_LPSR_ON,
56 };
57 
58 static const struct rohm_dvs_config buck2_dvs = {
59 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
60 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
61 	.run_reg		= BD71815_REG_BUCK2_VOLT_H,
62 	.run_mask		= BD71815_VOLT_MASK,
63 	.run_on_mask		= BD71815_BUCK_RUN_ON,
64 	.snvs_on_mask		= BD71815_BUCK_SNVS_ON,
65 	.suspend_reg		= BD71815_REG_BUCK2_VOLT_L,
66 	.suspend_mask		= BD71815_VOLT_MASK,
67 	.suspend_on_mask	= BD71815_BUCK_SUSP_ON,
68 	.lpsr_reg		= BD71815_REG_BUCK2_VOLT_L,
69 	.lpsr_mask		= BD71815_VOLT_MASK,
70 	.lpsr_on_mask		= BD71815_BUCK_LPSR_ON,
71 };
72 
73 static const struct rohm_dvs_config buck3_dvs = {
74 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
75 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
76 	.run_reg		= BD71815_REG_BUCK3_VOLT,
77 	.run_mask		= BD71815_VOLT_MASK,
78 	.run_on_mask		= BD71815_BUCK_RUN_ON,
79 	.snvs_on_mask		= BD71815_BUCK_SNVS_ON,
80 	.suspend_on_mask	= BD71815_BUCK_SUSP_ON,
81 	.lpsr_on_mask		= BD71815_BUCK_LPSR_ON,
82 };
83 
84 static const struct rohm_dvs_config buck4_dvs = {
85 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
86 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
87 	.run_reg		= BD71815_REG_BUCK4_VOLT,
88 	.run_mask		= BD71815_VOLT_MASK,
89 	.run_on_mask		= BD71815_BUCK_RUN_ON,
90 	.snvs_on_mask		= BD71815_BUCK_SNVS_ON,
91 	.suspend_on_mask	= BD71815_BUCK_SUSP_ON,
92 	.lpsr_on_mask		= BD71815_BUCK_LPSR_ON,
93 };
94 
95 static const struct rohm_dvs_config ldo1_dvs = {
96 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
97 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
98 	.run_reg		= BD71815_REG_LDO_MODE1,
99 	.run_mask		= BD71815_VOLT_MASK,
100 	.run_on_mask		= LDO1_RUN_ON,
101 	.snvs_on_mask		= LDO1_SNVS_ON,
102 	.suspend_on_mask	= LDO1_SUSP_ON,
103 	.lpsr_on_mask		= LDO1_LPSR_ON,
104 };
105 
106 static const struct rohm_dvs_config ldo2_dvs = {
107 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
108 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
109 	.run_reg		= BD71815_REG_LDO_MODE2,
110 	.run_mask		= BD71815_VOLT_MASK,
111 	.run_on_mask		= LDO2_RUN_ON,
112 	.snvs_on_mask		= LDO2_SNVS_ON,
113 	.suspend_on_mask	= LDO2_SUSP_ON,
114 	.lpsr_on_mask		= LDO2_LPSR_ON,
115 };
116 
117 static const struct rohm_dvs_config ldo3_dvs = {
118 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
119 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
120 	.run_reg		= BD71815_REG_LDO_MODE2,
121 	.run_mask		= BD71815_VOLT_MASK,
122 	.run_on_mask		= LDO3_RUN_ON,
123 	.snvs_on_mask		= LDO3_SNVS_ON,
124 	.suspend_on_mask	= LDO3_SUSP_ON,
125 	.lpsr_on_mask		= LDO3_LPSR_ON,
126 };
127 
128 static const struct rohm_dvs_config ldo4_dvs = {
129 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
130 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
131 	.run_reg		= BD71815_REG_LDO_MODE3,
132 	.run_mask		= BD71815_VOLT_MASK,
133 	.run_on_mask		= LDO4_RUN_ON,
134 	.snvs_on_mask		= LDO4_SNVS_ON,
135 	.suspend_on_mask	= LDO4_SUSP_ON,
136 	.lpsr_on_mask		= LDO4_LPSR_ON,
137 };
138 
139 static const struct rohm_dvs_config ldo5_dvs = {
140 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
141 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
142 	.run_reg		= BD71815_REG_LDO_MODE3,
143 	.run_mask		= BD71815_VOLT_MASK,
144 	.run_on_mask		= LDO5_RUN_ON,
145 	.snvs_on_mask		= LDO5_SNVS_ON,
146 	.suspend_on_mask	= LDO5_SUSP_ON,
147 	.lpsr_on_mask		= LDO5_LPSR_ON,
148 };
149 
150 static const struct rohm_dvs_config dvref_dvs = {
151 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
152 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
153 	.run_on_mask		= DVREF_RUN_ON,
154 	.snvs_on_mask		= DVREF_SNVS_ON,
155 	.suspend_on_mask	= DVREF_SUSP_ON,
156 	.lpsr_on_mask		= DVREF_LPSR_ON,
157 };
158 
159 static const struct rohm_dvs_config ldolpsr_dvs = {
160 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
161 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
162 	.run_on_mask		= DVREF_RUN_ON,
163 	.snvs_on_mask		= DVREF_SNVS_ON,
164 	.suspend_on_mask	= DVREF_SUSP_ON,
165 	.lpsr_on_mask		= DVREF_LPSR_ON,
166 };
167 
168 static const struct rohm_dvs_config buck5_dvs = {
169 	.level_map		= ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_SNVS |
170 				  ROHM_DVS_LEVEL_SUSPEND | ROHM_DVS_LEVEL_LPSR,
171 	.run_reg		= BD71815_REG_BUCK5_VOLT,
172 	.run_mask		= BD71815_VOLT_MASK,
173 	.run_on_mask		= BD71815_BUCK_RUN_ON,
174 	.snvs_on_mask		= BD71815_BUCK_SNVS_ON,
175 	.suspend_on_mask	= BD71815_BUCK_SUSP_ON,
176 	.lpsr_on_mask		= BD71815_BUCK_LPSR_ON,
177 };
178 
179 static int set_hw_dvs_levels(struct device_node *np,
180 			     const struct regulator_desc *desc,
181 			     struct regulator_config *cfg)
182 {
183 	struct bd71815_regulator *data;
184 
185 	data = container_of(desc, struct bd71815_regulator, desc);
186 	return rohm_regulator_set_dvs_levels(data->dvs, np, desc, cfg->regmap);
187 }
188 
189 /*
190  * Bucks 1 and 2 have two voltage selection registers where selected
191  * voltage can be set. Which of the registers is used can be either controlled
192  * by a control bit in register - or by HW state. If HW state specific voltages
193  * are given - then we assume HW state based control should be used.
194  *
195  * If volatge value is updated to currently selected register - then output
196  * voltage is immediately changed no matter what is set as ramp rate. Thus we
197  * default changing voltage by writing new value to inactive register and
198  * then updating the 'register selection' bit. This naturally only works when
199  * HW state machine is not used to select the voltage.
200  */
201 static int buck12_set_hw_dvs_levels(struct device_node *np,
202 				    const struct regulator_desc *desc,
203 				    struct regulator_config *cfg)
204 {
205 	struct bd71815_regulator *data;
206 	int ret = 0, val;
207 
208 	data = container_of(desc, struct bd71815_regulator, desc);
209 
210 	if (of_find_property(np, "rohm,dvs-run-voltage", NULL) ||
211 	    of_find_property(np, "rohm,dvs-suspend-voltage", NULL) ||
212 	    of_find_property(np, "rohm,dvs-lpsr-voltage", NULL) ||
213 	    of_find_property(np, "rohm,dvs-snvs-voltage", NULL)) {
214 		ret = regmap_read(cfg->regmap, desc->vsel_reg, &val);
215 		if (ret)
216 			return ret;
217 
218 		if (!(BD71815_BUCK_STBY_DVS & val) &&
219 		    !(BD71815_BUCK_DVSSEL & val)) {
220 			int val2;
221 
222 			/*
223 			 * We are currently using voltage from _L.
224 			 * We'd better copy it to _H and switch to it to
225 			 * avoid shutting us down if LPSR or SUSPEND is set to
226 			 * disabled. _L value is at reg _H + 1
227 			 */
228 			ret = regmap_read(cfg->regmap, desc->vsel_reg + 1,
229 					  &val2);
230 			if (ret)
231 				return ret;
232 
233 			ret = regmap_update_bits(cfg->regmap, desc->vsel_reg,
234 						 BD71815_VOLT_MASK |
235 						 BD71815_BUCK_DVSSEL,
236 						 val2 | BD71815_BUCK_DVSSEL);
237 			if (ret)
238 				return ret;
239 		}
240 		ret = rohm_regulator_set_dvs_levels(data->dvs, np, desc,
241 						    cfg->regmap);
242 		if (ret)
243 			return ret;
244 		/*
245 		 * DVS levels were given => use HW-state machine for voltage
246 		 * controls. NOTE: AFAIK, This means that if voltage is changed
247 		 * by SW the ramp-rate is not respected. Should we disable
248 		 * SW voltage control when the HW state machine is used?
249 		 */
250 		ret = regmap_update_bits(cfg->regmap, desc->vsel_reg,
251 					 BD71815_BUCK_STBY_DVS,
252 					 BD71815_BUCK_STBY_DVS);
253 	}
254 
255 	return ret;
256 }
257 
258 /*
259  * BUCK1/2
260  * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
261  * 00: 10.00mV/usec	10mV 1uS
262  * 01: 5.00mV/usec	10mV 2uS
263  * 10: 2.50mV/usec	10mV 4uS
264  * 11: 1.25mV/usec	10mV 8uS
265  */
266 static const unsigned int bd7181x_ramp_table[] = { 1250, 2500, 5000, 10000 };
267 
268 static int bd7181x_led_set_current_limit(struct regulator_dev *rdev,
269 					int min_uA, int max_uA)
270 {
271 	int ret;
272 	int onstatus;
273 
274 	onstatus = regulator_is_enabled_regmap(rdev);
275 
276 	ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
277 	if (!ret) {
278 		int newstatus;
279 
280 		newstatus = regulator_is_enabled_regmap(rdev);
281 		if (onstatus != newstatus) {
282 			/*
283 			 * HW FIX: spurious led status change detected. Toggle
284 			 * state as a workaround
285 			 */
286 			if (onstatus)
287 				ret = regulator_enable_regmap(rdev);
288 			else
289 				ret = regulator_disable_regmap(rdev);
290 
291 			if (ret)
292 				dev_err(rdev_get_dev(rdev),
293 					"failed to revert the LED state (%d)\n",
294 					ret);
295 		}
296 	}
297 
298 	return ret;
299 }
300 
301 static int bd7181x_buck12_get_voltage_sel(struct regulator_dev *rdev)
302 {
303 	struct bd71815_pmic *pmic = rdev_get_drvdata(rdev);
304 	int rid = rdev_get_id(rdev);
305 	int ret, regh, regl, val;
306 
307 	regh = BD71815_REG_BUCK1_VOLT_H + rid * 0x2;
308 	regl = BD71815_REG_BUCK1_VOLT_L + rid * 0x2;
309 
310 	ret = regmap_read(pmic->regmap, regh, &val);
311 	if (ret)
312 		return ret;
313 
314 	/*
315 	 * If we use HW state machine based voltage reg selection - then we
316 	 * return BD71815_REG_BUCK1_VOLT_H which is used at RUN.
317 	 * Else we do return the BD71815_REG_BUCK1_VOLT_H or
318 	 * BD71815_REG_BUCK1_VOLT_L depending on which is selected to be used
319 	 * by BD71815_BUCK_DVSSEL bit
320 	 */
321 	if ((!(val & BD71815_BUCK_STBY_DVS)) && (!(val & BD71815_BUCK_DVSSEL)))
322 		ret = regmap_read(pmic->regmap, regl, &val);
323 
324 	if (ret)
325 		return ret;
326 
327 	return val & BD71815_VOLT_MASK;
328 }
329 
330 /*
331  * For Buck 1/2.
332  */
333 static int bd7181x_buck12_set_voltage_sel(struct regulator_dev *rdev,
334 					  unsigned int sel)
335 {
336 	struct bd71815_pmic *pmic = rdev_get_drvdata(rdev);
337 	int rid = rdev_get_id(rdev);
338 	int ret, val, reg, regh, regl;
339 
340 	regh = BD71815_REG_BUCK1_VOLT_H + rid*0x2;
341 	regl = BD71815_REG_BUCK1_VOLT_L + rid*0x2;
342 
343 	ret = regmap_read(pmic->regmap, regh, &val);
344 	if (ret)
345 		return ret;
346 
347 	/*
348 	 * If bucks 1 & 2 are controlled by state machine - then the RUN state
349 	 * voltage is set to BD71815_REG_BUCK1_VOLT_H. Changing SUSPEND/LPSR
350 	 * voltages at runtime is not supported by this driver.
351 	 */
352 	if (((val & BD71815_BUCK_STBY_DVS))) {
353 		return regmap_update_bits(pmic->regmap, regh, BD71815_VOLT_MASK,
354 					  sel);
355 	}
356 	/* Update new voltage to the register which is not selected now */
357 	if (val & BD71815_BUCK_DVSSEL)
358 		reg = regl;
359 	else
360 		reg = regh;
361 
362 	ret = regmap_update_bits(pmic->regmap, reg, BD71815_VOLT_MASK, sel);
363 	if (ret)
364 		return ret;
365 
366 	/* Select the other DVS register to be used */
367 	return regmap_update_bits(pmic->regmap, regh, BD71815_BUCK_DVSSEL, ~val);
368 }
369 
370 static const struct regulator_ops bd7181x_ldo_regulator_ops = {
371 	.enable = regulator_enable_regmap,
372 	.disable = regulator_disable_regmap,
373 	.is_enabled = regulator_is_enabled_regmap,
374 	.list_voltage = regulator_list_voltage_linear,
375 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
376 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
377 };
378 
379 static const struct regulator_ops bd7181x_fixed_regulator_ops = {
380 	.enable = regulator_enable_regmap,
381 	.disable = regulator_disable_regmap,
382 	.is_enabled = regulator_is_enabled_regmap,
383 	.list_voltage = regulator_list_voltage_linear,
384 };
385 
386 static const struct regulator_ops bd7181x_buck_regulator_ops = {
387 	.enable = regulator_enable_regmap,
388 	.disable = regulator_disable_regmap,
389 	.is_enabled = regulator_is_enabled_regmap,
390 	.list_voltage = regulator_list_voltage_linear,
391 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
392 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
393 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
394 };
395 
396 static const struct regulator_ops bd7181x_buck12_regulator_ops = {
397 	.enable = regulator_enable_regmap,
398 	.disable = regulator_disable_regmap,
399 	.is_enabled = regulator_is_enabled_regmap,
400 	.list_voltage = regulator_list_voltage_linear,
401 	.set_voltage_sel = bd7181x_buck12_set_voltage_sel,
402 	.get_voltage_sel = bd7181x_buck12_get_voltage_sel,
403 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
404 	.set_ramp_delay = regulator_set_ramp_delay_regmap,
405 };
406 
407 static const struct regulator_ops bd7181x_led_regulator_ops = {
408 	.enable = regulator_enable_regmap,
409 	.disable = regulator_disable_regmap,
410 	.is_enabled = regulator_is_enabled_regmap,
411 	.set_current_limit = bd7181x_led_set_current_limit,
412 	.get_current_limit = regulator_get_current_limit_regmap,
413 };
414 
415 #define BD71815_FIXED_REG(_name, _id, ereg, emsk, voltage, _dvs)	\
416 	[(_id)] = {							\
417 		.desc = {						\
418 			.name = #_name,					\
419 			.of_match = of_match_ptr(#_name),		\
420 			.regulators_node = of_match_ptr("regulators"),	\
421 			.n_voltages = 1,				\
422 			.ops = &bd7181x_fixed_regulator_ops,		\
423 			.type = REGULATOR_VOLTAGE,			\
424 			.id = (_id),					\
425 			.owner = THIS_MODULE,				\
426 			.min_uV = (voltage),				\
427 			.enable_reg = (ereg),				\
428 			.enable_mask = (emsk),				\
429 			.of_parse_cb = set_hw_dvs_levels,		\
430 		},							\
431 		.dvs = (_dvs),						\
432 	}
433 
434 #define BD71815_BUCK_REG(_name, _id, vsel, ereg, min, max, step, _dvs)	\
435 	[(_id)] = {							\
436 		.desc = {						\
437 			.name = #_name,					\
438 			.of_match = of_match_ptr(#_name),		\
439 			.regulators_node = of_match_ptr("regulators"),	\
440 			.n_voltages = ((max) - (min)) / (step) + 1,	\
441 			.ops = &bd7181x_buck_regulator_ops,		\
442 			.type = REGULATOR_VOLTAGE,			\
443 			.id = (_id),					\
444 			.owner = THIS_MODULE,				\
445 			.min_uV = (min),				\
446 			.uV_step = (step),				\
447 			.vsel_reg = (vsel),				\
448 			.vsel_mask = BD71815_VOLT_MASK,			\
449 			.enable_reg = (ereg),				\
450 			.enable_mask = BD71815_BUCK_RUN_ON,		\
451 			.of_parse_cb = set_hw_dvs_levels,		\
452 		},							\
453 		.dvs = (_dvs),						\
454 	}
455 
456 #define BD71815_BUCK12_REG(_name, _id, vsel, ereg, min, max, step,	\
457 			   _dvs)					\
458 	[(_id)] = {							\
459 		.desc = {						\
460 			.name = #_name,					\
461 			.of_match = of_match_ptr(#_name),		\
462 			.regulators_node = of_match_ptr("regulators"),	\
463 			.n_voltages = ((max) - (min)) / (step) + 1,	\
464 			.ops = &bd7181x_buck12_regulator_ops,		\
465 			.type = REGULATOR_VOLTAGE,			\
466 			.id = (_id),					\
467 			.owner = THIS_MODULE,				\
468 			.min_uV = (min),				\
469 			.uV_step = (step),				\
470 			.vsel_reg = (vsel),				\
471 			.vsel_mask = 0x3f,				\
472 			.enable_reg = (ereg),				\
473 			.enable_mask = 0x04,				\
474 			.ramp_reg = (ereg),				\
475 			.ramp_mask = BD71815_BUCK_RAMPRATE_MASK,	\
476 			.ramp_delay_table = bd7181x_ramp_table,		\
477 			.n_ramp_values = ARRAY_SIZE(bd7181x_ramp_table),\
478 			.of_parse_cb = buck12_set_hw_dvs_levels,	\
479 		},							\
480 		.dvs = (_dvs),						\
481 	}
482 
483 #define BD71815_LED_REG(_name, _id, csel, mask, ereg, emsk, currents)	\
484 	[(_id)] = {							\
485 		.desc = {						\
486 			.name = #_name,					\
487 			.of_match = of_match_ptr(#_name),		\
488 			.regulators_node = of_match_ptr("regulators"),	\
489 			.n_current_limits = ARRAY_SIZE(currents),	\
490 			.ops = &bd7181x_led_regulator_ops,		\
491 			.type = REGULATOR_CURRENT,			\
492 			.id = (_id),					\
493 			.owner = THIS_MODULE,				\
494 			.curr_table = currents,				\
495 			.csel_reg = (csel),				\
496 			.csel_mask = (mask),				\
497 			.enable_reg = (ereg),				\
498 			.enable_mask = (emsk),				\
499 		},							\
500 	}
501 
502 #define BD71815_LDO_REG(_name, _id, vsel, ereg, emsk, min, max, step,	\
503 			_dvs)						\
504 	[(_id)] = {							\
505 		.desc = {						\
506 			.name = #_name,					\
507 			.of_match = of_match_ptr(#_name),		\
508 			.regulators_node = of_match_ptr("regulators"),	\
509 			.n_voltages = ((max) - (min)) / (step) + 1,	\
510 			.ops = &bd7181x_ldo_regulator_ops,		\
511 			.type = REGULATOR_VOLTAGE,			\
512 			.id = (_id),					\
513 			.owner = THIS_MODULE,				\
514 			.min_uV = (min),				\
515 			.uV_step = (step),				\
516 			.vsel_reg = (vsel),				\
517 			.vsel_mask = BD71815_VOLT_MASK,			\
518 			.enable_reg = (ereg),				\
519 			.enable_mask = (emsk),				\
520 			.of_parse_cb = set_hw_dvs_levels,		\
521 		},							\
522 		.dvs = (_dvs),						\
523 	}
524 
525 static struct bd71815_regulator bd71815_regulators[] = {
526 	BD71815_BUCK12_REG(buck1, BD71815_BUCK1, BD71815_REG_BUCK1_VOLT_H,
527 			   BD71815_REG_BUCK1_MODE, 800000, 2000000, 25000,
528 			   &buck1_dvs),
529 	BD71815_BUCK12_REG(buck2, BD71815_BUCK2, BD71815_REG_BUCK2_VOLT_H,
530 			   BD71815_REG_BUCK2_MODE, 800000, 2000000, 25000,
531 			   &buck2_dvs),
532 	BD71815_BUCK_REG(buck3, BD71815_BUCK3, BD71815_REG_BUCK3_VOLT,
533 			 BD71815_REG_BUCK3_MODE,  1200000, 2700000, 50000,
534 			 &buck3_dvs),
535 	BD71815_BUCK_REG(buck4, BD71815_BUCK4, BD71815_REG_BUCK4_VOLT,
536 			 BD71815_REG_BUCK4_MODE,  1100000, 1850000, 25000,
537 			 &buck4_dvs),
538 	BD71815_BUCK_REG(buck5, BD71815_BUCK5, BD71815_REG_BUCK5_VOLT,
539 			 BD71815_REG_BUCK5_MODE,  1800000, 3300000, 50000,
540 			 &buck5_dvs),
541 	BD71815_LDO_REG(ldo1, BD71815_LDO1, BD71815_REG_LDO1_VOLT,
542 			BD71815_REG_LDO_MODE1, LDO1_RUN_ON, 800000, 3300000,
543 			50000, &ldo1_dvs),
544 	BD71815_LDO_REG(ldo2, BD71815_LDO2, BD71815_REG_LDO2_VOLT,
545 			BD71815_REG_LDO_MODE2, LDO2_RUN_ON, 800000, 3300000,
546 			50000, &ldo2_dvs),
547 	/*
548 	 * Let's default LDO3 to be enabled by SW. We can override ops if DT
549 	 * says LDO3 should be enabled by HW when DCIN is connected.
550 	 */
551 	BD71815_LDO_REG(ldo3, BD71815_LDO3, BD71815_REG_LDO3_VOLT,
552 			BD71815_REG_LDO_MODE2, LDO3_RUN_ON, 800000, 3300000,
553 			50000, &ldo3_dvs),
554 	BD71815_LDO_REG(ldo4, BD71815_LDO4, BD71815_REG_LDO4_VOLT,
555 			BD71815_REG_LDO_MODE3, LDO4_RUN_ON, 800000, 3300000,
556 			50000, &ldo4_dvs),
557 	BD71815_LDO_REG(ldo5, BD71815_LDO5, BD71815_REG_LDO5_VOLT_H,
558 			BD71815_REG_LDO_MODE3, LDO5_RUN_ON, 800000, 3300000,
559 			50000, &ldo5_dvs),
560 	BD71815_FIXED_REG(ldodvref, BD71815_LDODVREF, BD71815_REG_LDO_MODE4,
561 			  DVREF_RUN_ON, 3000000, &dvref_dvs),
562 	BD71815_FIXED_REG(ldolpsr, BD71815_LDOLPSR, BD71815_REG_LDO_MODE4,
563 			  LDO_LPSR_RUN_ON, 1800000, &ldolpsr_dvs),
564 	BD71815_LED_REG(wled, BD71815_WLED, BD71815_REG_LED_DIMM, LED_DIMM_MASK,
565 			BD71815_REG_LED_CTRL, LED_RUN_ON,
566 			bd7181x_wled_currents),
567 };
568 
569 static int bd7181x_probe(struct platform_device *pdev)
570 {
571 	struct bd71815_pmic *pmic;
572 	struct regulator_config config = {};
573 	int i, ret;
574 	struct gpio_desc *ldo4_en;
575 
576 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
577 	if (!pmic)
578 		return -ENOMEM;
579 
580 	memcpy(pmic->descs, bd71815_regulators,	sizeof(pmic->descs));
581 
582 	pmic->dev = &pdev->dev;
583 	pmic->regmap = dev_get_regmap(pdev->dev.parent, NULL);
584 	if (!pmic->regmap) {
585 		dev_err(pmic->dev, "No parent regmap\n");
586 		return -ENODEV;
587 	}
588 	platform_set_drvdata(pdev, pmic);
589 	ldo4_en = devm_gpiod_get_from_of_node(&pdev->dev,
590 					      pdev->dev.parent->of_node,
591 						 "rohm,vsel-gpios", 0,
592 						 GPIOD_ASIS, "ldo4-en");
593 
594 	if (IS_ERR(ldo4_en)) {
595 		ret = PTR_ERR(ldo4_en);
596 		if (ret != -ENOENT)
597 			return ret;
598 		ldo4_en = NULL;
599 	}
600 
601 	/* Disable to go to ship-mode */
602 	ret = regmap_update_bits(pmic->regmap, BD71815_REG_PWRCTRL,
603 				 RESTARTEN, 0);
604 	if (ret)
605 		return ret;
606 
607 	config.dev = pdev->dev.parent;
608 	config.regmap = pmic->regmap;
609 
610 	for (i = 0; i < BD71815_REGULATOR_CNT; i++) {
611 		struct regulator_desc *desc;
612 		struct regulator_dev *rdev;
613 
614 		desc = &pmic->descs[i].desc;
615 		if (i == BD71815_LDO4)
616 			config.ena_gpiod = ldo4_en;
617 
618 		config.driver_data = pmic;
619 
620 		rdev = devm_regulator_register(&pdev->dev, desc, &config);
621 		if (IS_ERR(rdev)) {
622 			dev_err(&pdev->dev,
623 				"failed to register %s regulator\n",
624 				desc->name);
625 			return PTR_ERR(rdev);
626 		}
627 		config.ena_gpiod = NULL;
628 		pmic->rdev[i] = rdev;
629 	}
630 	return 0;
631 }
632 
633 static const struct platform_device_id bd7181x_pmic_id[] = {
634 	{ "bd71815-pmic", ROHM_CHIP_TYPE_BD71815 },
635 	{ },
636 };
637 MODULE_DEVICE_TABLE(platform, bd7181x_pmic_id);
638 
639 static struct platform_driver bd7181x_regulator = {
640 	.driver = {
641 		.name = "bd7181x-pmic",
642 		.owner = THIS_MODULE,
643 	},
644 	.probe = bd7181x_probe,
645 	.id_table = bd7181x_pmic_id,
646 };
647 module_platform_driver(bd7181x_regulator);
648 
649 MODULE_AUTHOR("Tony Luo <luofc@embedinfo.com>");
650 MODULE_DESCRIPTION("BD71815 voltage regulator driver");
651 MODULE_LICENSE("GPL v2");
652 MODULE_ALIAS("platform:bd7181x-pmic");
653