1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator driver for Rockchip RK805/RK808/RK818
4  *
5  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6  *
7  * Author: Chris Zhong <zyw@rock-chips.com>
8  * Author: Zhang Qing <zhangqing@rock-chips.com>
9  *
10  * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11  *
12  * Author: Wadim Egorov <w.egorov@phytec.de>
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/mfd/rk808.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/gpio/consumer.h>
25 
26 /* Field Definitions */
27 #define RK808_BUCK_VSEL_MASK	0x3f
28 #define RK808_BUCK4_VSEL_MASK	0xf
29 #define RK808_LDO_VSEL_MASK	0x1f
30 
31 #define RK809_BUCK5_VSEL_MASK		0x7
32 
33 #define RK817_LDO_VSEL_MASK		0x7f
34 #define RK817_BOOST_VSEL_MASK		0x7
35 #define RK817_BUCK_VSEL_MASK		0x7f
36 
37 #define RK818_BUCK_VSEL_MASK		0x3f
38 #define RK818_BUCK4_VSEL_MASK		0x1f
39 #define RK818_LDO_VSEL_MASK		0x1f
40 #define RK818_LDO3_ON_VSEL_MASK		0xf
41 #define RK818_BOOST_ON_VSEL_MASK	0xe0
42 
43 /* Ramp rate definitions for buck1 / buck2 only */
44 #define RK808_RAMP_RATE_OFFSET		3
45 #define RK808_RAMP_RATE_MASK		(3 << RK808_RAMP_RATE_OFFSET)
46 #define RK808_RAMP_RATE_2MV_PER_US	(0 << RK808_RAMP_RATE_OFFSET)
47 #define RK808_RAMP_RATE_4MV_PER_US	(1 << RK808_RAMP_RATE_OFFSET)
48 #define RK808_RAMP_RATE_6MV_PER_US	(2 << RK808_RAMP_RATE_OFFSET)
49 #define RK808_RAMP_RATE_10MV_PER_US	(3 << RK808_RAMP_RATE_OFFSET)
50 
51 #define RK808_DVS2_POL		BIT(2)
52 #define RK808_DVS1_POL		BIT(1)
53 
54 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
55 #define RK808_SLP_REG_OFFSET 1
56 
57 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
58 #define RK808_DVS_REG_OFFSET 2
59 
60 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
61 #define RK808_SLP_SET_OFF_REG_OFFSET 2
62 
63 /* max steps for increase voltage of Buck1/2, equal 100mv*/
64 #define MAX_STEPS_ONE_TIME 8
65 
66 #define ENABLE_MASK(id)			(BIT(id) | BIT(4 + (id)))
67 #define DISABLE_VAL(id)			(BIT(4 + (id)))
68 
69 #define RK817_BOOST_DESC(_id, _match, _supply, _min, _max, _step, _vreg,\
70 	_vmask, _ereg, _emask, _enval, _disval, _etime, m_drop)		\
71 	{							\
72 		.name		= (_match),				\
73 		.supply_name	= (_supply),				\
74 		.of_match	= of_match_ptr(_match),			\
75 		.regulators_node = of_match_ptr("regulators"),		\
76 		.type		= REGULATOR_VOLTAGE,			\
77 		.id		= (_id),				\
78 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
79 		.owner		= THIS_MODULE,				\
80 		.min_uV		= (_min) * 1000,			\
81 		.uV_step	= (_step) * 1000,			\
82 		.vsel_reg	= (_vreg),				\
83 		.vsel_mask	= (_vmask),				\
84 		.enable_reg	= (_ereg),				\
85 		.enable_mask	= (_emask),				\
86 		.enable_val     = (_enval),				\
87 		.disable_val     = (_disval),				\
88 		.enable_time	= (_etime),				\
89 		.min_dropout_uV = (m_drop) * 1000,			\
90 		.ops		= &rk817_boost_ops,			\
91 	}
92 
93 #define RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
94 	_vmask, _ereg, _emask, _enval, _disval, _etime, _ops)		\
95 	{								\
96 		.name		= (_match),				\
97 		.supply_name	= (_supply),				\
98 		.of_match	= of_match_ptr(_match),			\
99 		.regulators_node = of_match_ptr("regulators"),		\
100 		.type		= REGULATOR_VOLTAGE,			\
101 		.id		= (_id),				\
102 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
103 		.owner		= THIS_MODULE,				\
104 		.min_uV		= (_min) * 1000,			\
105 		.uV_step	= (_step) * 1000,			\
106 		.vsel_reg	= (_vreg),				\
107 		.vsel_mask	= (_vmask),				\
108 		.enable_reg	= (_ereg),				\
109 		.enable_mask	= (_emask),				\
110 		.enable_val     = (_enval),				\
111 		.disable_val     = (_disval),				\
112 		.enable_time	= (_etime),				\
113 		.ops		= _ops,			\
114 	}
115 
116 #define RK805_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
117 	_vmask, _ereg, _emask, _etime)					\
118 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
119 	_vmask, _ereg, _emask, 0, 0, _etime, &rk805_reg_ops)
120 
121 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
122 	_vmask, _ereg, _emask, _etime)					\
123 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
124 	_vmask, _ereg, _emask, 0, 0, _etime, &rk808_reg_ops)
125 
126 #define RK817_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
127 	_vmask, _ereg, _emask, _disval, _etime)				\
128 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
129 	_vmask, _ereg, _emask, _emask, _disval, _etime, &rk817_reg_ops)
130 
131 #define RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
132 	_enval, _disval, _ops)						\
133 	{								\
134 		.name		= (_match),				\
135 		.supply_name	= (_supply),				\
136 		.of_match	= of_match_ptr(_match),			\
137 		.regulators_node = of_match_ptr("regulators"),		\
138 		.type		= REGULATOR_VOLTAGE,			\
139 		.id		= (_id),				\
140 		.enable_reg	= (_ereg),				\
141 		.enable_mask	= (_emask),				\
142 		.enable_val     = (_enval),				\
143 		.disable_val     = (_disval),				\
144 		.owner		= THIS_MODULE,				\
145 		.ops		= _ops					\
146 	}
147 
148 #define RK817_DESC_SWITCH(_id, _match, _supply, _ereg, _emask,		\
149 	_disval)							\
150 	RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
151 	_emask, _disval, &rk817_switch_ops)
152 
153 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask)		\
154 	RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
155 	0, 0, &rk808_switch_ops)
156 
157 struct rk808_regulator_data {
158 	struct gpio_desc *dvs_gpio[2];
159 };
160 
161 static const int rk808_buck_config_regs[] = {
162 	RK808_BUCK1_CONFIG_REG,
163 	RK808_BUCK2_CONFIG_REG,
164 	RK808_BUCK3_CONFIG_REG,
165 	RK808_BUCK4_CONFIG_REG,
166 };
167 
168 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
169 	REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
170 	REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
171 };
172 
173 #define RK809_BUCK5_SEL_CNT		(8)
174 
175 static const struct regulator_linear_range rk809_buck5_voltage_ranges[] = {
176 	REGULATOR_LINEAR_RANGE(1500000, 0, 0, 0),
177 	REGULATOR_LINEAR_RANGE(1800000, 1, 3, 200000),
178 	REGULATOR_LINEAR_RANGE(2800000, 4, 5, 200000),
179 	REGULATOR_LINEAR_RANGE(3300000, 6, 7, 300000),
180 };
181 
182 #define RK817_BUCK1_MIN0 500000
183 #define RK817_BUCK1_MAX0 1500000
184 
185 #define RK817_BUCK1_MIN1 1600000
186 #define RK817_BUCK1_MAX1 2400000
187 
188 #define RK817_BUCK3_MAX1 3400000
189 
190 #define RK817_BUCK1_STP0 12500
191 #define RK817_BUCK1_STP1 100000
192 
193 #define RK817_BUCK1_SEL0 ((RK817_BUCK1_MAX0 - RK817_BUCK1_MIN0) /\
194 						  RK817_BUCK1_STP0)
195 #define RK817_BUCK1_SEL1 ((RK817_BUCK1_MAX1 - RK817_BUCK1_MIN1) /\
196 						  RK817_BUCK1_STP1)
197 
198 #define RK817_BUCK3_SEL1 ((RK817_BUCK3_MAX1 - RK817_BUCK1_MIN1) /\
199 						  RK817_BUCK1_STP1)
200 
201 #define RK817_BUCK1_SEL_CNT (RK817_BUCK1_SEL0 + RK817_BUCK1_SEL1 + 1)
202 #define RK817_BUCK3_SEL_CNT (RK817_BUCK1_SEL0 + RK817_BUCK3_SEL1 + 1)
203 
204 static const struct regulator_linear_range rk817_buck1_voltage_ranges[] = {
205 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN0, 0,
206 			       RK817_BUCK1_SEL0, RK817_BUCK1_STP0),
207 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN1, RK817_BUCK1_SEL0 + 1,
208 			       RK817_BUCK1_SEL_CNT, RK817_BUCK1_STP1),
209 };
210 
211 static const struct regulator_linear_range rk817_buck3_voltage_ranges[] = {
212 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN0, 0,
213 			       RK817_BUCK1_SEL0, RK817_BUCK1_STP0),
214 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN1, RK817_BUCK1_SEL0 + 1,
215 			       RK817_BUCK3_SEL_CNT, RK817_BUCK1_STP1),
216 };
217 
218 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
219 {
220 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
221 	int id = rdev_get_id(rdev);
222 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
223 	unsigned int val;
224 	int ret;
225 
226 	if (!gpio || gpiod_get_value(gpio) == 0)
227 		return regulator_get_voltage_sel_regmap(rdev);
228 
229 	ret = regmap_read(rdev->regmap,
230 			  rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
231 			  &val);
232 	if (ret != 0)
233 		return ret;
234 
235 	val &= rdev->desc->vsel_mask;
236 	val >>= ffs(rdev->desc->vsel_mask) - 1;
237 
238 	return val;
239 }
240 
241 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
242 					     unsigned sel)
243 {
244 	int ret, delta_sel;
245 	unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
246 
247 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
248 	if (ret != 0)
249 		return ret;
250 
251 	tmp = val & ~mask;
252 	old_sel = val & mask;
253 	old_sel >>= ffs(mask) - 1;
254 	delta_sel = sel - old_sel;
255 
256 	/*
257 	 * If directly modify the register to change the voltage, we will face
258 	 * the risk of overshoot. Put it into a multi-step, can effectively
259 	 * avoid this problem, a step is 100mv here.
260 	 */
261 	while (delta_sel > MAX_STEPS_ONE_TIME) {
262 		old_sel += MAX_STEPS_ONE_TIME;
263 		val = old_sel << (ffs(mask) - 1);
264 		val |= tmp;
265 
266 		/*
267 		 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
268 		 * 3 bytes (24 bits) plus start and stop so 26 bits.  So we've
269 		 * got more than 65 us between each voltage change and thus
270 		 * won't ramp faster than ~1500 uV / us.
271 		 */
272 		ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
273 		delta_sel = sel - old_sel;
274 	}
275 
276 	sel <<= ffs(mask) - 1;
277 	val = tmp | sel;
278 	ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
279 
280 	/*
281 	 * When we change the voltage register directly, the ramp rate is about
282 	 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
283 	 * so we needn't wait extra time after that.
284 	 */
285 	udelay(1);
286 
287 	return ret;
288 }
289 
290 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
291 					 unsigned sel)
292 {
293 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
294 	int id = rdev_get_id(rdev);
295 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
296 	unsigned int reg = rdev->desc->vsel_reg;
297 	unsigned old_sel;
298 	int ret, gpio_level;
299 
300 	if (!gpio)
301 		return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
302 
303 	gpio_level = gpiod_get_value(gpio);
304 	if (gpio_level == 0) {
305 		reg += RK808_DVS_REG_OFFSET;
306 		ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
307 	} else {
308 		ret = regmap_read(rdev->regmap,
309 				  reg + RK808_DVS_REG_OFFSET,
310 				  &old_sel);
311 	}
312 
313 	if (ret != 0)
314 		return ret;
315 
316 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
317 	sel |= old_sel & ~rdev->desc->vsel_mask;
318 
319 	ret = regmap_write(rdev->regmap, reg, sel);
320 	if (ret)
321 		return ret;
322 
323 	gpiod_set_value(gpio, !gpio_level);
324 
325 	return ret;
326 }
327 
328 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
329 				       unsigned int old_selector,
330 				       unsigned int new_selector)
331 {
332 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
333 	int id = rdev_get_id(rdev);
334 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
335 
336 	/* if there is no dvs1/2 pin, we don't need wait extra time here. */
337 	if (!gpio)
338 		return 0;
339 
340 	return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
341 }
342 
343 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
344 {
345 	unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
346 	unsigned int reg = rk808_buck_config_regs[rdev_get_id(rdev)];
347 
348 	switch (ramp_delay) {
349 	case 1 ... 2000:
350 		ramp_value = RK808_RAMP_RATE_2MV_PER_US;
351 		break;
352 	case 2001 ... 4000:
353 		ramp_value = RK808_RAMP_RATE_4MV_PER_US;
354 		break;
355 	case 4001 ... 6000:
356 		ramp_value = RK808_RAMP_RATE_6MV_PER_US;
357 		break;
358 	case 6001 ... 10000:
359 		break;
360 	default:
361 		pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
362 			rdev->desc->name, ramp_delay);
363 	}
364 
365 	return regmap_update_bits(rdev->regmap, reg,
366 				  RK808_RAMP_RATE_MASK, ramp_value);
367 }
368 
369 /*
370  * RK817 RK809
371  */
372 static int rk817_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
373 {
374 	unsigned int ramp_value = RK817_RAMP_RATE_25MV_PER_US;
375 	unsigned int reg = RK817_BUCK_CONFIG_REG(rdev_get_id(rdev));
376 
377 	switch (ramp_delay) {
378 	case 0 ... 3000:
379 		ramp_value = RK817_RAMP_RATE_3MV_PER_US;
380 		break;
381 	case 3001 ... 6300:
382 		ramp_value = RK817_RAMP_RATE_6_3MV_PER_US;
383 		break;
384 	case 6301 ... 12500:
385 		ramp_value = RK817_RAMP_RATE_12_5MV_PER_US;
386 		break;
387 	case 12501 ... 25000:
388 		break;
389 	default:
390 		dev_warn(&rdev->dev,
391 			 "%s ramp_delay: %d not supported, setting 10000\n",
392 			 rdev->desc->name, ramp_delay);
393 	}
394 
395 	return regmap_update_bits(rdev->regmap, reg,
396 				  RK817_RAMP_RATE_MASK, ramp_value);
397 }
398 
399 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
400 {
401 	unsigned int reg;
402 	int sel = regulator_map_voltage_linear(rdev, uv, uv);
403 
404 	if (sel < 0)
405 		return -EINVAL;
406 
407 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
408 
409 	return regmap_update_bits(rdev->regmap, reg,
410 				  rdev->desc->vsel_mask,
411 				  sel);
412 }
413 
414 static int rk817_set_suspend_voltage(struct regulator_dev *rdev, int uv)
415 {
416 	unsigned int reg;
417 	int sel = regulator_map_voltage_linear(rdev, uv, uv);
418 	/* only ldo1~ldo9 */
419 	if (sel < 0)
420 		return -EINVAL;
421 
422 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
423 
424 	return regmap_update_bits(rdev->regmap, reg,
425 				  rdev->desc->vsel_mask,
426 				  sel);
427 }
428 
429 static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
430 {
431 	unsigned int reg;
432 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
433 
434 	if (sel < 0)
435 		return -EINVAL;
436 
437 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
438 
439 	return regmap_update_bits(rdev->regmap, reg,
440 				  rdev->desc->vsel_mask,
441 				  sel);
442 }
443 
444 static int rk805_set_suspend_enable(struct regulator_dev *rdev)
445 {
446 	unsigned int reg;
447 
448 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
449 
450 	return regmap_update_bits(rdev->regmap, reg,
451 				  rdev->desc->enable_mask,
452 				  rdev->desc->enable_mask);
453 }
454 
455 static int rk805_set_suspend_disable(struct regulator_dev *rdev)
456 {
457 	unsigned int reg;
458 
459 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
460 
461 	return regmap_update_bits(rdev->regmap, reg,
462 				  rdev->desc->enable_mask,
463 				  0);
464 }
465 
466 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
467 {
468 	unsigned int reg;
469 
470 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
471 
472 	return regmap_update_bits(rdev->regmap, reg,
473 				  rdev->desc->enable_mask,
474 				  0);
475 }
476 
477 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
478 {
479 	unsigned int reg;
480 
481 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
482 
483 	return regmap_update_bits(rdev->regmap, reg,
484 				  rdev->desc->enable_mask,
485 				  rdev->desc->enable_mask);
486 }
487 
488 static int rk817_set_suspend_enable_ctrl(struct regulator_dev *rdev,
489 					 unsigned int en)
490 {
491 	unsigned int reg;
492 	int id = rdev_get_id(rdev);
493 	unsigned int id_slp, msk, val;
494 
495 	if (id >= RK817_ID_DCDC1 && id <= RK817_ID_DCDC4)
496 		id_slp = id;
497 	else if (id >= RK817_ID_LDO1 && id <= RK817_ID_LDO8)
498 		id_slp = 8 + (id - RK817_ID_LDO1);
499 	else if (id >= RK817_ID_LDO9 && id <= RK809_ID_SW2)
500 		id_slp = 4 + (id - RK817_ID_LDO9);
501 	else
502 		return -EINVAL;
503 
504 	reg = RK817_POWER_SLP_EN_REG(id_slp / 8);
505 
506 	msk = BIT(id_slp % 8);
507 	if (en)
508 		val = msk;
509 	else
510 		val = 0;
511 
512 	return regmap_update_bits(rdev->regmap, reg, msk, val);
513 }
514 
515 static int rk817_set_suspend_enable(struct regulator_dev *rdev)
516 {
517 	return rk817_set_suspend_enable_ctrl(rdev, 1);
518 }
519 
520 static int rk817_set_suspend_disable(struct regulator_dev *rdev)
521 {
522 	return rk817_set_suspend_enable_ctrl(rdev, 0);
523 }
524 
525 static int rk8xx_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
526 {
527 	unsigned int reg;
528 
529 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
530 
531 	switch (mode) {
532 	case REGULATOR_MODE_FAST:
533 		return regmap_update_bits(rdev->regmap, reg,
534 					  PWM_MODE_MSK, FPWM_MODE);
535 	case REGULATOR_MODE_NORMAL:
536 		return regmap_update_bits(rdev->regmap, reg,
537 					  PWM_MODE_MSK, AUTO_PWM_MODE);
538 	default:
539 		dev_err(&rdev->dev, "do not support this mode\n");
540 		return -EINVAL;
541 	}
542 
543 	return 0;
544 }
545 
546 static int rk8xx_set_mode(struct regulator_dev *rdev, unsigned int mode)
547 {
548 	switch (mode) {
549 	case REGULATOR_MODE_FAST:
550 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
551 					  PWM_MODE_MSK, FPWM_MODE);
552 	case REGULATOR_MODE_NORMAL:
553 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
554 					  PWM_MODE_MSK, AUTO_PWM_MODE);
555 	default:
556 		dev_err(&rdev->dev, "do not support this mode\n");
557 		return -EINVAL;
558 	}
559 
560 	return 0;
561 }
562 
563 static unsigned int rk8xx_get_mode(struct regulator_dev *rdev)
564 {
565 	unsigned int val;
566 	int err;
567 
568 	err = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
569 	if (err)
570 		return err;
571 
572 	if (val & FPWM_MODE)
573 		return REGULATOR_MODE_FAST;
574 	else
575 		return REGULATOR_MODE_NORMAL;
576 }
577 
578 static int rk8xx_is_enabled_wmsk_regmap(struct regulator_dev *rdev)
579 {
580 	unsigned int val;
581 	int ret;
582 
583 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
584 	if (ret != 0)
585 		return ret;
586 
587 	/* add write mask bit */
588 	val |= (rdev->desc->enable_mask & 0xf0);
589 	val &= rdev->desc->enable_mask;
590 
591 	if (rdev->desc->enable_is_inverted) {
592 		if (rdev->desc->enable_val)
593 			return val != rdev->desc->enable_val;
594 		return (val == 0);
595 	}
596 	if (rdev->desc->enable_val)
597 		return val == rdev->desc->enable_val;
598 	return val != 0;
599 }
600 
601 static unsigned int rk8xx_regulator_of_map_mode(unsigned int mode)
602 {
603 	switch (mode) {
604 	case 1:
605 		return REGULATOR_MODE_FAST;
606 	case 2:
607 		return REGULATOR_MODE_NORMAL;
608 	default:
609 		return -EINVAL;
610 	}
611 }
612 
613 static const struct regulator_ops rk805_reg_ops = {
614 	.list_voltage           = regulator_list_voltage_linear,
615 	.map_voltage            = regulator_map_voltage_linear,
616 	.get_voltage_sel        = regulator_get_voltage_sel_regmap,
617 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
618 	.enable                 = regulator_enable_regmap,
619 	.disable                = regulator_disable_regmap,
620 	.is_enabled             = regulator_is_enabled_regmap,
621 	.set_suspend_voltage    = rk808_set_suspend_voltage,
622 	.set_suspend_enable     = rk805_set_suspend_enable,
623 	.set_suspend_disable    = rk805_set_suspend_disable,
624 };
625 
626 static const struct regulator_ops rk805_switch_ops = {
627 	.enable                 = regulator_enable_regmap,
628 	.disable                = regulator_disable_regmap,
629 	.is_enabled             = regulator_is_enabled_regmap,
630 	.set_suspend_enable     = rk805_set_suspend_enable,
631 	.set_suspend_disable    = rk805_set_suspend_disable,
632 };
633 
634 static const struct regulator_ops rk808_buck1_2_ops = {
635 	.list_voltage		= regulator_list_voltage_linear,
636 	.map_voltage		= regulator_map_voltage_linear,
637 	.get_voltage_sel	= rk808_buck1_2_get_voltage_sel_regmap,
638 	.set_voltage_sel	= rk808_buck1_2_set_voltage_sel,
639 	.set_voltage_time_sel	= rk808_buck1_2_set_voltage_time_sel,
640 	.enable			= regulator_enable_regmap,
641 	.disable		= regulator_disable_regmap,
642 	.is_enabled		= regulator_is_enabled_regmap,
643 	.set_ramp_delay		= rk808_set_ramp_delay,
644 	.set_suspend_voltage	= rk808_set_suspend_voltage,
645 	.set_suspend_enable	= rk808_set_suspend_enable,
646 	.set_suspend_disable	= rk808_set_suspend_disable,
647 };
648 
649 static const struct regulator_ops rk808_reg_ops = {
650 	.list_voltage		= regulator_list_voltage_linear,
651 	.map_voltage		= regulator_map_voltage_linear,
652 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
653 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
654 	.enable			= regulator_enable_regmap,
655 	.disable		= regulator_disable_regmap,
656 	.is_enabled		= regulator_is_enabled_regmap,
657 	.set_suspend_voltage	= rk808_set_suspend_voltage,
658 	.set_suspend_enable	= rk808_set_suspend_enable,
659 	.set_suspend_disable	= rk808_set_suspend_disable,
660 };
661 
662 static const struct regulator_ops rk808_reg_ops_ranges = {
663 	.list_voltage		= regulator_list_voltage_linear_range,
664 	.map_voltage		= regulator_map_voltage_linear_range,
665 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
666 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
667 	.enable			= regulator_enable_regmap,
668 	.disable		= regulator_disable_regmap,
669 	.is_enabled		= regulator_is_enabled_regmap,
670 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
671 	.set_suspend_enable	= rk808_set_suspend_enable,
672 	.set_suspend_disable	= rk808_set_suspend_disable,
673 };
674 
675 static const struct regulator_ops rk808_switch_ops = {
676 	.enable			= regulator_enable_regmap,
677 	.disable		= regulator_disable_regmap,
678 	.is_enabled		= regulator_is_enabled_regmap,
679 	.set_suspend_enable	= rk808_set_suspend_enable,
680 	.set_suspend_disable	= rk808_set_suspend_disable,
681 };
682 
683 static const struct regulator_linear_range rk805_buck_1_2_voltage_ranges[] = {
684 	REGULATOR_LINEAR_RANGE(712500, 0, 59, 12500),
685 	REGULATOR_LINEAR_RANGE(1800000, 60, 62, 200000),
686 	REGULATOR_LINEAR_RANGE(2300000, 63, 63, 0),
687 };
688 
689 static struct regulator_ops rk809_buck5_ops_range = {
690 	.list_voltage		= regulator_list_voltage_linear_range,
691 	.map_voltage		= regulator_map_voltage_linear_range,
692 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
693 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
694 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
695 	.enable			= regulator_enable_regmap,
696 	.disable		= regulator_disable_regmap,
697 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
698 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
699 	.set_suspend_enable	= rk817_set_suspend_enable,
700 	.set_suspend_disable	= rk817_set_suspend_disable,
701 };
702 
703 static struct regulator_ops rk817_reg_ops = {
704 	.list_voltage		= regulator_list_voltage_linear,
705 	.map_voltage		= regulator_map_voltage_linear,
706 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
707 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
708 	.enable			= regulator_enable_regmap,
709 	.disable		= regulator_disable_regmap,
710 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
711 	.set_suspend_voltage	= rk817_set_suspend_voltage,
712 	.set_suspend_enable	= rk817_set_suspend_enable,
713 	.set_suspend_disable	= rk817_set_suspend_disable,
714 };
715 
716 static struct regulator_ops rk817_boost_ops = {
717 	.list_voltage		= regulator_list_voltage_linear,
718 	.map_voltage		= regulator_map_voltage_linear,
719 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
720 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
721 	.enable			= regulator_enable_regmap,
722 	.disable		= regulator_disable_regmap,
723 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
724 	.set_suspend_enable	= rk817_set_suspend_enable,
725 	.set_suspend_disable	= rk817_set_suspend_disable,
726 };
727 
728 static struct regulator_ops rk817_buck_ops_range = {
729 	.list_voltage		= regulator_list_voltage_linear_range,
730 	.map_voltage		= regulator_map_voltage_linear_range,
731 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
732 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
733 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
734 	.enable			= regulator_enable_regmap,
735 	.disable		= regulator_disable_regmap,
736 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
737 	.set_mode		= rk8xx_set_mode,
738 	.get_mode		= rk8xx_get_mode,
739 	.set_suspend_mode	= rk8xx_set_suspend_mode,
740 	.set_ramp_delay		= rk817_set_ramp_delay,
741 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
742 	.set_suspend_enable	= rk817_set_suspend_enable,
743 	.set_suspend_disable	= rk817_set_suspend_disable,
744 };
745 
746 static struct regulator_ops rk817_switch_ops = {
747 	.enable			= regulator_enable_regmap,
748 	.disable		= regulator_disable_regmap,
749 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
750 	.set_suspend_enable	= rk817_set_suspend_enable,
751 	.set_suspend_disable	= rk817_set_suspend_disable,
752 };
753 
754 static const struct regulator_desc rk805_reg[] = {
755 	{
756 		.name = "DCDC_REG1",
757 		.supply_name = "vcc1",
758 		.of_match = of_match_ptr("DCDC_REG1"),
759 		.regulators_node = of_match_ptr("regulators"),
760 		.id = RK805_ID_DCDC1,
761 		.ops = &rk808_reg_ops_ranges,
762 		.type = REGULATOR_VOLTAGE,
763 		.n_voltages = 64,
764 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
765 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
766 		.vsel_reg = RK805_BUCK1_ON_VSEL_REG,
767 		.vsel_mask = RK818_BUCK_VSEL_MASK,
768 		.enable_reg = RK805_DCDC_EN_REG,
769 		.enable_mask = BIT(0),
770 		.owner = THIS_MODULE,
771 	}, {
772 		.name = "DCDC_REG2",
773 		.supply_name = "vcc2",
774 		.of_match = of_match_ptr("DCDC_REG2"),
775 		.regulators_node = of_match_ptr("regulators"),
776 		.id = RK805_ID_DCDC2,
777 		.ops = &rk808_reg_ops_ranges,
778 		.type = REGULATOR_VOLTAGE,
779 		.n_voltages = 64,
780 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
781 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
782 		.vsel_reg = RK805_BUCK2_ON_VSEL_REG,
783 		.vsel_mask = RK818_BUCK_VSEL_MASK,
784 		.enable_reg = RK805_DCDC_EN_REG,
785 		.enable_mask = BIT(1),
786 		.owner = THIS_MODULE,
787 	}, {
788 		.name = "DCDC_REG3",
789 		.supply_name = "vcc3",
790 		.of_match = of_match_ptr("DCDC_REG3"),
791 		.regulators_node = of_match_ptr("regulators"),
792 		.id = RK805_ID_DCDC3,
793 		.ops = &rk805_switch_ops,
794 		.type = REGULATOR_VOLTAGE,
795 		.n_voltages = 1,
796 		.enable_reg = RK805_DCDC_EN_REG,
797 		.enable_mask = BIT(2),
798 		.owner = THIS_MODULE,
799 	},
800 
801 	RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
802 		RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
803 		RK805_DCDC_EN_REG, BIT(3), 0),
804 
805 	RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
806 		RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
807 		BIT(0), 400),
808 	RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
809 		RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
810 		BIT(1), 400),
811 	RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
812 		RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
813 		BIT(2), 400),
814 };
815 
816 static const struct regulator_desc rk808_reg[] = {
817 	{
818 		.name = "DCDC_REG1",
819 		.supply_name = "vcc1",
820 		.of_match = of_match_ptr("DCDC_REG1"),
821 		.regulators_node = of_match_ptr("regulators"),
822 		.id = RK808_ID_DCDC1,
823 		.ops = &rk808_buck1_2_ops,
824 		.type = REGULATOR_VOLTAGE,
825 		.min_uV = 712500,
826 		.uV_step = 12500,
827 		.n_voltages = 64,
828 		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
829 		.vsel_mask = RK808_BUCK_VSEL_MASK,
830 		.enable_reg = RK808_DCDC_EN_REG,
831 		.enable_mask = BIT(0),
832 		.owner = THIS_MODULE,
833 	}, {
834 		.name = "DCDC_REG2",
835 		.supply_name = "vcc2",
836 		.of_match = of_match_ptr("DCDC_REG2"),
837 		.regulators_node = of_match_ptr("regulators"),
838 		.id = RK808_ID_DCDC2,
839 		.ops = &rk808_buck1_2_ops,
840 		.type = REGULATOR_VOLTAGE,
841 		.min_uV = 712500,
842 		.uV_step = 12500,
843 		.n_voltages = 64,
844 		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
845 		.vsel_mask = RK808_BUCK_VSEL_MASK,
846 		.enable_reg = RK808_DCDC_EN_REG,
847 		.enable_mask = BIT(1),
848 		.owner = THIS_MODULE,
849 	}, {
850 		.name = "DCDC_REG3",
851 		.supply_name = "vcc3",
852 		.of_match = of_match_ptr("DCDC_REG3"),
853 		.regulators_node = of_match_ptr("regulators"),
854 		.id = RK808_ID_DCDC3,
855 		.ops = &rk808_switch_ops,
856 		.type = REGULATOR_VOLTAGE,
857 		.n_voltages = 1,
858 		.enable_reg = RK808_DCDC_EN_REG,
859 		.enable_mask = BIT(2),
860 		.owner = THIS_MODULE,
861 	},
862 	RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
863 		RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
864 		RK808_DCDC_EN_REG, BIT(3), 0),
865 	RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
866 		RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
867 		BIT(0), 400),
868 	RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
869 		RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
870 		BIT(1), 400),
871 	{
872 		.name = "LDO_REG3",
873 		.supply_name = "vcc7",
874 		.of_match = of_match_ptr("LDO_REG3"),
875 		.regulators_node = of_match_ptr("regulators"),
876 		.id = RK808_ID_LDO3,
877 		.ops = &rk808_reg_ops_ranges,
878 		.type = REGULATOR_VOLTAGE,
879 		.n_voltages = 16,
880 		.linear_ranges = rk808_ldo3_voltage_ranges,
881 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
882 		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
883 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
884 		.enable_reg = RK808_LDO_EN_REG,
885 		.enable_mask = BIT(2),
886 		.enable_time = 400,
887 		.owner = THIS_MODULE,
888 	},
889 	RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
890 		RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
891 		BIT(3), 400),
892 	RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
893 		RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
894 		BIT(4), 400),
895 	RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
896 		RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
897 		BIT(5), 400),
898 	RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
899 		RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
900 		BIT(6), 400),
901 	RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
902 		RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
903 		BIT(7), 400),
904 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
905 		RK808_DCDC_EN_REG, BIT(5)),
906 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
907 		RK808_DCDC_EN_REG, BIT(6)),
908 };
909 
910 static const struct regulator_desc rk809_reg[] = {
911 	{
912 		.name = "DCDC_REG1",
913 		.supply_name = "vcc1",
914 		.of_match = of_match_ptr("DCDC_REG1"),
915 		.regulators_node = of_match_ptr("regulators"),
916 		.id = RK817_ID_DCDC1,
917 		.ops = &rk817_buck_ops_range,
918 		.type = REGULATOR_VOLTAGE,
919 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
920 		.linear_ranges = rk817_buck1_voltage_ranges,
921 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
922 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
923 		.vsel_mask = RK817_BUCK_VSEL_MASK,
924 		.enable_reg = RK817_POWER_EN_REG(0),
925 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
926 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
927 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
928 		.of_map_mode = rk8xx_regulator_of_map_mode,
929 		.owner = THIS_MODULE,
930 	}, {
931 		.name = "DCDC_REG2",
932 		.supply_name = "vcc2",
933 		.of_match = of_match_ptr("DCDC_REG2"),
934 		.regulators_node = of_match_ptr("regulators"),
935 		.id = RK817_ID_DCDC2,
936 		.ops = &rk817_buck_ops_range,
937 		.type = REGULATOR_VOLTAGE,
938 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
939 		.linear_ranges = rk817_buck1_voltage_ranges,
940 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
941 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
942 		.vsel_mask = RK817_BUCK_VSEL_MASK,
943 		.enable_reg = RK817_POWER_EN_REG(0),
944 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
945 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
946 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
947 		.of_map_mode = rk8xx_regulator_of_map_mode,
948 		.owner = THIS_MODULE,
949 	}, {
950 		.name = "DCDC_REG3",
951 		.supply_name = "vcc3",
952 		.of_match = of_match_ptr("DCDC_REG3"),
953 		.regulators_node = of_match_ptr("regulators"),
954 		.id = RK817_ID_DCDC3,
955 		.ops = &rk817_buck_ops_range,
956 		.type = REGULATOR_VOLTAGE,
957 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
958 		.linear_ranges = rk817_buck1_voltage_ranges,
959 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
960 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
961 		.vsel_mask = RK817_BUCK_VSEL_MASK,
962 		.enable_reg = RK817_POWER_EN_REG(0),
963 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
964 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
965 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
966 		.of_map_mode = rk8xx_regulator_of_map_mode,
967 		.owner = THIS_MODULE,
968 	}, {
969 		.name = "DCDC_REG4",
970 		.supply_name = "vcc4",
971 		.of_match = of_match_ptr("DCDC_REG4"),
972 		.regulators_node = of_match_ptr("regulators"),
973 		.id = RK817_ID_DCDC4,
974 		.ops = &rk817_buck_ops_range,
975 		.type = REGULATOR_VOLTAGE,
976 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
977 		.linear_ranges = rk817_buck3_voltage_ranges,
978 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
979 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
980 		.vsel_mask = RK817_BUCK_VSEL_MASK,
981 		.enable_reg = RK817_POWER_EN_REG(0),
982 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
983 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
984 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
985 		.of_map_mode = rk8xx_regulator_of_map_mode,
986 		.owner = THIS_MODULE,
987 	},
988 	{
989 		.name = "DCDC_REG5",
990 		.supply_name = "vcc9",
991 		.of_match = of_match_ptr("DCDC_REG5"),
992 		.regulators_node = of_match_ptr("regulators"),
993 		.id = RK809_ID_DCDC5,
994 		.ops = &rk809_buck5_ops_range,
995 		.type = REGULATOR_VOLTAGE,
996 		.n_voltages = RK809_BUCK5_SEL_CNT,
997 		.linear_ranges = rk809_buck5_voltage_ranges,
998 		.n_linear_ranges = ARRAY_SIZE(rk809_buck5_voltage_ranges),
999 		.vsel_reg = RK809_BUCK5_CONFIG(0),
1000 		.vsel_mask = RK809_BUCK5_VSEL_MASK,
1001 		.enable_reg = RK817_POWER_EN_REG(3),
1002 		.enable_mask = ENABLE_MASK(1),
1003 		.enable_val = ENABLE_MASK(1),
1004 		.disable_val = DISABLE_VAL(1),
1005 		.of_map_mode = rk8xx_regulator_of_map_mode,
1006 		.owner = THIS_MODULE,
1007 	},
1008 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
1009 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
1010 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
1011 		   DISABLE_VAL(0), 400),
1012 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
1013 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
1014 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1015 		   DISABLE_VAL(1), 400),
1016 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1017 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1018 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1019 		   DISABLE_VAL(2), 400),
1020 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1021 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1022 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1023 		   DISABLE_VAL(3), 400),
1024 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1025 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1026 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1027 		   DISABLE_VAL(0), 400),
1028 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1029 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1030 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1031 		   DISABLE_VAL(1), 400),
1032 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1033 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1034 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1035 		   DISABLE_VAL(2), 400),
1036 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1037 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1038 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1039 		   DISABLE_VAL(3), 400),
1040 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1041 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1042 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1043 		   DISABLE_VAL(0), 400),
1044 	RK817_DESC_SWITCH(RK809_ID_SW1, "SWITCH_REG1", "vcc9",
1045 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1046 			  DISABLE_VAL(2)),
1047 	RK817_DESC_SWITCH(RK809_ID_SW2, "SWITCH_REG2", "vcc8",
1048 			  RK817_POWER_EN_REG(3), ENABLE_MASK(3),
1049 			  DISABLE_VAL(3)),
1050 };
1051 
1052 static const struct regulator_desc rk817_reg[] = {
1053 	{
1054 		.name = "DCDC_REG1",
1055 		.supply_name = "vcc1",
1056 		.of_match = of_match_ptr("DCDC_REG1"),
1057 		.regulators_node = of_match_ptr("regulators"),
1058 		.id = RK817_ID_DCDC1,
1059 		.ops = &rk817_buck_ops_range,
1060 		.type = REGULATOR_VOLTAGE,
1061 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1062 		.linear_ranges = rk817_buck1_voltage_ranges,
1063 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1064 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
1065 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1066 		.enable_reg = RK817_POWER_EN_REG(0),
1067 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
1068 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
1069 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
1070 		.of_map_mode = rk8xx_regulator_of_map_mode,
1071 		.owner = THIS_MODULE,
1072 	}, {
1073 		.name = "DCDC_REG2",
1074 		.supply_name = "vcc2",
1075 		.of_match = of_match_ptr("DCDC_REG2"),
1076 		.regulators_node = of_match_ptr("regulators"),
1077 		.id = RK817_ID_DCDC2,
1078 		.ops = &rk817_buck_ops_range,
1079 		.type = REGULATOR_VOLTAGE,
1080 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1081 		.linear_ranges = rk817_buck1_voltage_ranges,
1082 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1083 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
1084 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1085 		.enable_reg = RK817_POWER_EN_REG(0),
1086 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
1087 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
1088 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
1089 		.of_map_mode = rk8xx_regulator_of_map_mode,
1090 		.owner = THIS_MODULE,
1091 	}, {
1092 		.name = "DCDC_REG3",
1093 		.supply_name = "vcc3",
1094 		.of_match = of_match_ptr("DCDC_REG3"),
1095 		.regulators_node = of_match_ptr("regulators"),
1096 		.id = RK817_ID_DCDC3,
1097 		.ops = &rk817_buck_ops_range,
1098 		.type = REGULATOR_VOLTAGE,
1099 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1100 		.linear_ranges = rk817_buck1_voltage_ranges,
1101 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1102 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
1103 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1104 		.enable_reg = RK817_POWER_EN_REG(0),
1105 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
1106 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
1107 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
1108 		.of_map_mode = rk8xx_regulator_of_map_mode,
1109 		.owner = THIS_MODULE,
1110 	}, {
1111 		.name = "DCDC_REG4",
1112 		.supply_name = "vcc4",
1113 		.of_match = of_match_ptr("DCDC_REG4"),
1114 		.regulators_node = of_match_ptr("regulators"),
1115 		.id = RK817_ID_DCDC4,
1116 		.ops = &rk817_buck_ops_range,
1117 		.type = REGULATOR_VOLTAGE,
1118 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
1119 		.linear_ranges = rk817_buck3_voltage_ranges,
1120 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
1121 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
1122 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1123 		.enable_reg = RK817_POWER_EN_REG(0),
1124 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
1125 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
1126 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
1127 		.of_map_mode = rk8xx_regulator_of_map_mode,
1128 		.owner = THIS_MODULE,
1129 	},
1130 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
1131 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
1132 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
1133 		   DISABLE_VAL(0), 400),
1134 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
1135 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
1136 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1137 		   DISABLE_VAL(1), 400),
1138 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1139 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1140 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1141 		   DISABLE_VAL(2), 400),
1142 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1143 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1144 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1145 		   DISABLE_VAL(3), 400),
1146 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1147 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1148 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1149 		   DISABLE_VAL(0), 400),
1150 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1151 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1152 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1153 		   DISABLE_VAL(1), 400),
1154 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1155 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1156 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1157 		   DISABLE_VAL(2), 400),
1158 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1159 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1160 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1161 		   DISABLE_VAL(3), 400),
1162 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1163 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1164 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1165 		   DISABLE_VAL(0), 400),
1166 	RK817_BOOST_DESC(RK817_ID_BOOST, "BOOST", "vcc8", 4700, 5400, 100,
1167 			 RK817_BOOST_OTG_CFG, RK817_BOOST_VSEL_MASK,
1168 			 RK817_POWER_EN_REG(3), ENABLE_MASK(1), ENABLE_MASK(1),
1169 		   DISABLE_VAL(1), 400, 3500 - 5400),
1170 	RK817_DESC_SWITCH(RK817_ID_BOOST_OTG_SW, "OTG_SWITCH", "vcc9",
1171 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1172 			  DISABLE_VAL(2)),
1173 };
1174 
1175 static const struct regulator_desc rk818_reg[] = {
1176 	{
1177 		.name = "DCDC_REG1",
1178 		.supply_name = "vcc1",
1179 		.of_match = of_match_ptr("DCDC_REG1"),
1180 		.regulators_node = of_match_ptr("regulators"),
1181 		.id = RK818_ID_DCDC1,
1182 		.ops = &rk808_reg_ops,
1183 		.type = REGULATOR_VOLTAGE,
1184 		.min_uV = 712500,
1185 		.uV_step = 12500,
1186 		.n_voltages = 64,
1187 		.vsel_reg = RK818_BUCK1_ON_VSEL_REG,
1188 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1189 		.enable_reg = RK818_DCDC_EN_REG,
1190 		.enable_mask = BIT(0),
1191 		.owner = THIS_MODULE,
1192 	}, {
1193 		.name = "DCDC_REG2",
1194 		.supply_name = "vcc2",
1195 		.of_match = of_match_ptr("DCDC_REG2"),
1196 		.regulators_node = of_match_ptr("regulators"),
1197 		.id = RK818_ID_DCDC2,
1198 		.ops = &rk808_reg_ops,
1199 		.type = REGULATOR_VOLTAGE,
1200 		.min_uV = 712500,
1201 		.uV_step = 12500,
1202 		.n_voltages = 64,
1203 		.vsel_reg = RK818_BUCK2_ON_VSEL_REG,
1204 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1205 		.enable_reg = RK818_DCDC_EN_REG,
1206 		.enable_mask = BIT(1),
1207 		.owner = THIS_MODULE,
1208 	}, {
1209 		.name = "DCDC_REG3",
1210 		.supply_name = "vcc3",
1211 		.of_match = of_match_ptr("DCDC_REG3"),
1212 		.regulators_node = of_match_ptr("regulators"),
1213 		.id = RK818_ID_DCDC3,
1214 		.ops = &rk808_switch_ops,
1215 		.type = REGULATOR_VOLTAGE,
1216 		.n_voltages = 1,
1217 		.enable_reg = RK818_DCDC_EN_REG,
1218 		.enable_mask = BIT(2),
1219 		.owner = THIS_MODULE,
1220 	},
1221 	RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
1222 		RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
1223 		RK818_DCDC_EN_REG, BIT(3), 0),
1224 	RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
1225 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
1226 		RK818_DCDC_EN_REG, BIT(4), 0),
1227 	RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
1228 		RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1229 		BIT(0), 400),
1230 	RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
1231 		RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1232 		BIT(1), 400),
1233 	{
1234 		.name = "LDO_REG3",
1235 		.supply_name = "vcc7",
1236 		.of_match = of_match_ptr("LDO_REG3"),
1237 		.regulators_node = of_match_ptr("regulators"),
1238 		.id = RK818_ID_LDO3,
1239 		.ops = &rk808_reg_ops_ranges,
1240 		.type = REGULATOR_VOLTAGE,
1241 		.n_voltages = 16,
1242 		.linear_ranges = rk808_ldo3_voltage_ranges,
1243 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
1244 		.vsel_reg = RK818_LDO3_ON_VSEL_REG,
1245 		.vsel_mask = RK818_LDO3_ON_VSEL_MASK,
1246 		.enable_reg = RK818_LDO_EN_REG,
1247 		.enable_mask = BIT(2),
1248 		.enable_time = 400,
1249 		.owner = THIS_MODULE,
1250 	},
1251 	RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
1252 		RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1253 		BIT(3), 400),
1254 	RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
1255 		RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1256 		BIT(4), 400),
1257 	RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
1258 		RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1259 		BIT(5), 400),
1260 	RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
1261 		RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1262 		BIT(6), 400),
1263 	RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
1264 		RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1265 		BIT(7), 400),
1266 	RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
1267 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
1268 		RK818_DCDC_EN_REG, BIT(5), 400),
1269 	RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
1270 		RK818_DCDC_EN_REG, BIT(6)),
1271 	RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
1272 		RK818_H5V_EN_REG, BIT(0)),
1273 	RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
1274 		RK818_DCDC_EN_REG, BIT(7)),
1275 };
1276 
1277 static int rk808_regulator_dt_parse_pdata(struct device *dev,
1278 				   struct device *client_dev,
1279 				   struct regmap *map,
1280 				   struct rk808_regulator_data *pdata)
1281 {
1282 	struct device_node *np;
1283 	int tmp, ret = 0, i;
1284 
1285 	np = of_get_child_by_name(client_dev->of_node, "regulators");
1286 	if (!np)
1287 		return -ENXIO;
1288 
1289 	for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
1290 		pdata->dvs_gpio[i] =
1291 			devm_gpiod_get_index_optional(client_dev, "dvs", i,
1292 						      GPIOD_OUT_LOW);
1293 		if (IS_ERR(pdata->dvs_gpio[i])) {
1294 			ret = PTR_ERR(pdata->dvs_gpio[i]);
1295 			dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
1296 			goto dt_parse_end;
1297 		}
1298 
1299 		if (!pdata->dvs_gpio[i]) {
1300 			dev_warn(dev, "there is no dvs%d gpio\n", i);
1301 			continue;
1302 		}
1303 
1304 		tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
1305 		ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
1306 				gpiod_is_active_low(pdata->dvs_gpio[i]) ?
1307 				0 : tmp);
1308 	}
1309 
1310 dt_parse_end:
1311 	of_node_put(np);
1312 	return ret;
1313 }
1314 
1315 static int rk808_regulator_probe(struct platform_device *pdev)
1316 {
1317 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
1318 	struct i2c_client *client = rk808->i2c;
1319 	struct regulator_config config = {};
1320 	struct regulator_dev *rk808_rdev;
1321 	struct rk808_regulator_data *pdata;
1322 	const struct regulator_desc *regulators;
1323 	int ret, i, nregulators;
1324 
1325 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1326 	if (!pdata)
1327 		return -ENOMEM;
1328 
1329 	ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
1330 					     rk808->regmap, pdata);
1331 	if (ret < 0)
1332 		return ret;
1333 
1334 	platform_set_drvdata(pdev, pdata);
1335 
1336 	switch (rk808->variant) {
1337 	case RK805_ID:
1338 		regulators = rk805_reg;
1339 		nregulators = RK805_NUM_REGULATORS;
1340 		break;
1341 	case RK808_ID:
1342 		regulators = rk808_reg;
1343 		nregulators = RK808_NUM_REGULATORS;
1344 		break;
1345 	case RK809_ID:
1346 		regulators = rk809_reg;
1347 		nregulators = RK809_NUM_REGULATORS;
1348 		break;
1349 	case RK817_ID:
1350 		regulators = rk817_reg;
1351 		nregulators = RK817_NUM_REGULATORS;
1352 		break;
1353 	case RK818_ID:
1354 		regulators = rk818_reg;
1355 		nregulators = RK818_NUM_REGULATORS;
1356 		break;
1357 	default:
1358 		dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
1359 			rk808->variant);
1360 		return -EINVAL;
1361 	}
1362 
1363 	config.dev = &client->dev;
1364 	config.driver_data = pdata;
1365 	config.regmap = rk808->regmap;
1366 
1367 	/* Instantiate the regulators */
1368 	for (i = 0; i < nregulators; i++) {
1369 		rk808_rdev = devm_regulator_register(&pdev->dev,
1370 						     &regulators[i], &config);
1371 		if (IS_ERR(rk808_rdev)) {
1372 			dev_err(&client->dev,
1373 				"failed to register %d regulator\n", i);
1374 			return PTR_ERR(rk808_rdev);
1375 		}
1376 	}
1377 
1378 	return 0;
1379 }
1380 
1381 static struct platform_driver rk808_regulator_driver = {
1382 	.probe = rk808_regulator_probe,
1383 	.driver = {
1384 		.name = "rk808-regulator"
1385 	},
1386 };
1387 
1388 module_platform_driver(rk808_regulator_driver);
1389 
1390 MODULE_DESCRIPTION("regulator driver for the RK805/RK808/RK818 series PMICs");
1391 MODULE_AUTHOR("Tony xie <tony.xie@rock-chips.com>");
1392 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1393 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
1394 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
1395 MODULE_LICENSE("GPL");
1396 MODULE_ALIAS("platform:rk808-regulator");
1397