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 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 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 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 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 25000\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 rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
415 {
416 	unsigned int reg;
417 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
418 
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 rk805_set_suspend_enable(struct regulator_dev *rdev)
430 {
431 	unsigned int reg;
432 
433 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
434 
435 	return regmap_update_bits(rdev->regmap, reg,
436 				  rdev->desc->enable_mask,
437 				  rdev->desc->enable_mask);
438 }
439 
440 static int rk805_set_suspend_disable(struct regulator_dev *rdev)
441 {
442 	unsigned int reg;
443 
444 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
445 
446 	return regmap_update_bits(rdev->regmap, reg,
447 				  rdev->desc->enable_mask,
448 				  0);
449 }
450 
451 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
452 {
453 	unsigned int reg;
454 
455 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
456 
457 	return regmap_update_bits(rdev->regmap, reg,
458 				  rdev->desc->enable_mask,
459 				  0);
460 }
461 
462 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
463 {
464 	unsigned int reg;
465 
466 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
467 
468 	return regmap_update_bits(rdev->regmap, reg,
469 				  rdev->desc->enable_mask,
470 				  rdev->desc->enable_mask);
471 }
472 
473 static int rk817_set_suspend_enable_ctrl(struct regulator_dev *rdev,
474 					 unsigned int en)
475 {
476 	unsigned int reg;
477 	int id = rdev_get_id(rdev);
478 	unsigned int id_slp, msk, val;
479 
480 	if (id >= RK817_ID_DCDC1 && id <= RK817_ID_DCDC4)
481 		id_slp = id;
482 	else if (id >= RK817_ID_LDO1 && id <= RK817_ID_LDO8)
483 		id_slp = 8 + (id - RK817_ID_LDO1);
484 	else if (id >= RK817_ID_LDO9 && id <= RK809_ID_SW2)
485 		id_slp = 4 + (id - RK817_ID_LDO9);
486 	else
487 		return -EINVAL;
488 
489 	reg = RK817_POWER_SLP_EN_REG(id_slp / 8);
490 
491 	msk = BIT(id_slp % 8);
492 	if (en)
493 		val = msk;
494 	else
495 		val = 0;
496 
497 	return regmap_update_bits(rdev->regmap, reg, msk, val);
498 }
499 
500 static int rk817_set_suspend_enable(struct regulator_dev *rdev)
501 {
502 	return rk817_set_suspend_enable_ctrl(rdev, 1);
503 }
504 
505 static int rk817_set_suspend_disable(struct regulator_dev *rdev)
506 {
507 	return rk817_set_suspend_enable_ctrl(rdev, 0);
508 }
509 
510 static int rk8xx_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
511 {
512 	unsigned int reg;
513 
514 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
515 
516 	switch (mode) {
517 	case REGULATOR_MODE_FAST:
518 		return regmap_update_bits(rdev->regmap, reg,
519 					  PWM_MODE_MSK, FPWM_MODE);
520 	case REGULATOR_MODE_NORMAL:
521 		return regmap_update_bits(rdev->regmap, reg,
522 					  PWM_MODE_MSK, AUTO_PWM_MODE);
523 	default:
524 		dev_err(&rdev->dev, "do not support this mode\n");
525 		return -EINVAL;
526 	}
527 
528 	return 0;
529 }
530 
531 static int rk8xx_set_mode(struct regulator_dev *rdev, unsigned int mode)
532 {
533 	switch (mode) {
534 	case REGULATOR_MODE_FAST:
535 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
536 					  PWM_MODE_MSK, FPWM_MODE);
537 	case REGULATOR_MODE_NORMAL:
538 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
539 					  PWM_MODE_MSK, AUTO_PWM_MODE);
540 	default:
541 		dev_err(&rdev->dev, "do not support this mode\n");
542 		return -EINVAL;
543 	}
544 
545 	return 0;
546 }
547 
548 static unsigned int rk8xx_get_mode(struct regulator_dev *rdev)
549 {
550 	unsigned int val;
551 	int err;
552 
553 	err = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
554 	if (err)
555 		return err;
556 
557 	if (val & FPWM_MODE)
558 		return REGULATOR_MODE_FAST;
559 	else
560 		return REGULATOR_MODE_NORMAL;
561 }
562 
563 static int rk8xx_is_enabled_wmsk_regmap(struct regulator_dev *rdev)
564 {
565 	unsigned int val;
566 	int ret;
567 
568 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
569 	if (ret != 0)
570 		return ret;
571 
572 	/* add write mask bit */
573 	val |= (rdev->desc->enable_mask & 0xf0);
574 	val &= rdev->desc->enable_mask;
575 
576 	if (rdev->desc->enable_is_inverted) {
577 		if (rdev->desc->enable_val)
578 			return val != rdev->desc->enable_val;
579 		return (val == 0);
580 	}
581 	if (rdev->desc->enable_val)
582 		return val == rdev->desc->enable_val;
583 	return val != 0;
584 }
585 
586 static unsigned int rk8xx_regulator_of_map_mode(unsigned int mode)
587 {
588 	switch (mode) {
589 	case 1:
590 		return REGULATOR_MODE_FAST;
591 	case 2:
592 		return REGULATOR_MODE_NORMAL;
593 	default:
594 		return REGULATOR_MODE_INVALID;
595 	}
596 }
597 
598 static const struct regulator_ops rk805_reg_ops = {
599 	.list_voltage           = regulator_list_voltage_linear,
600 	.map_voltage            = regulator_map_voltage_linear,
601 	.get_voltage_sel        = regulator_get_voltage_sel_regmap,
602 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
603 	.enable                 = regulator_enable_regmap,
604 	.disable                = regulator_disable_regmap,
605 	.is_enabled             = regulator_is_enabled_regmap,
606 	.set_suspend_voltage    = rk808_set_suspend_voltage,
607 	.set_suspend_enable     = rk805_set_suspend_enable,
608 	.set_suspend_disable    = rk805_set_suspend_disable,
609 };
610 
611 static const struct regulator_ops rk805_switch_ops = {
612 	.enable                 = regulator_enable_regmap,
613 	.disable                = regulator_disable_regmap,
614 	.is_enabled             = regulator_is_enabled_regmap,
615 	.set_suspend_enable     = rk805_set_suspend_enable,
616 	.set_suspend_disable    = rk805_set_suspend_disable,
617 };
618 
619 static const struct regulator_ops rk808_buck1_2_ops = {
620 	.list_voltage		= regulator_list_voltage_linear,
621 	.map_voltage		= regulator_map_voltage_linear,
622 	.get_voltage_sel	= rk808_buck1_2_get_voltage_sel_regmap,
623 	.set_voltage_sel	= rk808_buck1_2_set_voltage_sel,
624 	.set_voltage_time_sel	= rk808_buck1_2_set_voltage_time_sel,
625 	.enable			= regulator_enable_regmap,
626 	.disable		= regulator_disable_regmap,
627 	.is_enabled		= regulator_is_enabled_regmap,
628 	.set_ramp_delay		= rk808_set_ramp_delay,
629 	.set_suspend_voltage	= rk808_set_suspend_voltage,
630 	.set_suspend_enable	= rk808_set_suspend_enable,
631 	.set_suspend_disable	= rk808_set_suspend_disable,
632 };
633 
634 static const struct regulator_ops rk808_reg_ops = {
635 	.list_voltage		= regulator_list_voltage_linear,
636 	.map_voltage		= regulator_map_voltage_linear,
637 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
638 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
639 	.enable			= regulator_enable_regmap,
640 	.disable		= regulator_disable_regmap,
641 	.is_enabled		= regulator_is_enabled_regmap,
642 	.set_suspend_voltage	= rk808_set_suspend_voltage,
643 	.set_suspend_enable	= rk808_set_suspend_enable,
644 	.set_suspend_disable	= rk808_set_suspend_disable,
645 };
646 
647 static const struct regulator_ops rk808_reg_ops_ranges = {
648 	.list_voltage		= regulator_list_voltage_linear_range,
649 	.map_voltage		= regulator_map_voltage_linear_range,
650 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
651 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
652 	.enable			= regulator_enable_regmap,
653 	.disable		= regulator_disable_regmap,
654 	.is_enabled		= regulator_is_enabled_regmap,
655 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
656 	.set_suspend_enable	= rk808_set_suspend_enable,
657 	.set_suspend_disable	= rk808_set_suspend_disable,
658 };
659 
660 static const struct regulator_ops rk808_switch_ops = {
661 	.enable			= regulator_enable_regmap,
662 	.disable		= regulator_disable_regmap,
663 	.is_enabled		= regulator_is_enabled_regmap,
664 	.set_suspend_enable	= rk808_set_suspend_enable,
665 	.set_suspend_disable	= rk808_set_suspend_disable,
666 };
667 
668 static const struct linear_range rk805_buck_1_2_voltage_ranges[] = {
669 	REGULATOR_LINEAR_RANGE(712500, 0, 59, 12500),
670 	REGULATOR_LINEAR_RANGE(1800000, 60, 62, 200000),
671 	REGULATOR_LINEAR_RANGE(2300000, 63, 63, 0),
672 };
673 
674 static const struct regulator_ops rk809_buck5_ops_range = {
675 	.list_voltage		= regulator_list_voltage_linear_range,
676 	.map_voltage		= regulator_map_voltage_linear_range,
677 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
678 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
679 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
680 	.enable			= regulator_enable_regmap,
681 	.disable		= regulator_disable_regmap,
682 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
683 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
684 	.set_suspend_enable	= rk817_set_suspend_enable,
685 	.set_suspend_disable	= rk817_set_suspend_disable,
686 };
687 
688 static const struct regulator_ops rk817_reg_ops = {
689 	.list_voltage		= regulator_list_voltage_linear,
690 	.map_voltage		= regulator_map_voltage_linear,
691 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
692 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
693 	.enable			= regulator_enable_regmap,
694 	.disable		= regulator_disable_regmap,
695 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
696 	.set_suspend_voltage	= rk808_set_suspend_voltage,
697 	.set_suspend_enable	= rk817_set_suspend_enable,
698 	.set_suspend_disable	= rk817_set_suspend_disable,
699 };
700 
701 static const struct regulator_ops rk817_boost_ops = {
702 	.list_voltage		= regulator_list_voltage_linear,
703 	.map_voltage		= regulator_map_voltage_linear,
704 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
705 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
706 	.enable			= regulator_enable_regmap,
707 	.disable		= regulator_disable_regmap,
708 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
709 	.set_suspend_enable	= rk817_set_suspend_enable,
710 	.set_suspend_disable	= rk817_set_suspend_disable,
711 };
712 
713 static const struct regulator_ops rk817_buck_ops_range = {
714 	.list_voltage		= regulator_list_voltage_linear_range,
715 	.map_voltage		= regulator_map_voltage_linear_range,
716 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
717 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
718 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
719 	.enable			= regulator_enable_regmap,
720 	.disable		= regulator_disable_regmap,
721 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
722 	.set_mode		= rk8xx_set_mode,
723 	.get_mode		= rk8xx_get_mode,
724 	.set_suspend_mode	= rk8xx_set_suspend_mode,
725 	.set_ramp_delay		= rk817_set_ramp_delay,
726 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
727 	.set_suspend_enable	= rk817_set_suspend_enable,
728 	.set_suspend_disable	= rk817_set_suspend_disable,
729 };
730 
731 static const struct regulator_ops rk817_switch_ops = {
732 	.enable			= regulator_enable_regmap,
733 	.disable		= regulator_disable_regmap,
734 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
735 	.set_suspend_enable	= rk817_set_suspend_enable,
736 	.set_suspend_disable	= rk817_set_suspend_disable,
737 };
738 
739 static const struct regulator_desc rk805_reg[] = {
740 	{
741 		.name = "DCDC_REG1",
742 		.supply_name = "vcc1",
743 		.of_match = of_match_ptr("DCDC_REG1"),
744 		.regulators_node = of_match_ptr("regulators"),
745 		.id = RK805_ID_DCDC1,
746 		.ops = &rk808_reg_ops_ranges,
747 		.type = REGULATOR_VOLTAGE,
748 		.n_voltages = 64,
749 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
750 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
751 		.vsel_reg = RK805_BUCK1_ON_VSEL_REG,
752 		.vsel_mask = RK818_BUCK_VSEL_MASK,
753 		.enable_reg = RK805_DCDC_EN_REG,
754 		.enable_mask = BIT(0),
755 		.owner = THIS_MODULE,
756 	}, {
757 		.name = "DCDC_REG2",
758 		.supply_name = "vcc2",
759 		.of_match = of_match_ptr("DCDC_REG2"),
760 		.regulators_node = of_match_ptr("regulators"),
761 		.id = RK805_ID_DCDC2,
762 		.ops = &rk808_reg_ops_ranges,
763 		.type = REGULATOR_VOLTAGE,
764 		.n_voltages = 64,
765 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
766 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
767 		.vsel_reg = RK805_BUCK2_ON_VSEL_REG,
768 		.vsel_mask = RK818_BUCK_VSEL_MASK,
769 		.enable_reg = RK805_DCDC_EN_REG,
770 		.enable_mask = BIT(1),
771 		.owner = THIS_MODULE,
772 	}, {
773 		.name = "DCDC_REG3",
774 		.supply_name = "vcc3",
775 		.of_match = of_match_ptr("DCDC_REG3"),
776 		.regulators_node = of_match_ptr("regulators"),
777 		.id = RK805_ID_DCDC3,
778 		.ops = &rk805_switch_ops,
779 		.type = REGULATOR_VOLTAGE,
780 		.n_voltages = 1,
781 		.enable_reg = RK805_DCDC_EN_REG,
782 		.enable_mask = BIT(2),
783 		.owner = THIS_MODULE,
784 	},
785 
786 	RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
787 		RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
788 		RK805_DCDC_EN_REG, BIT(3), 0),
789 
790 	RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
791 		RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
792 		BIT(0), 400),
793 	RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
794 		RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
795 		BIT(1), 400),
796 	RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
797 		RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
798 		BIT(2), 400),
799 };
800 
801 static const struct regulator_desc rk808_reg[] = {
802 	{
803 		.name = "DCDC_REG1",
804 		.supply_name = "vcc1",
805 		.of_match = of_match_ptr("DCDC_REG1"),
806 		.regulators_node = of_match_ptr("regulators"),
807 		.id = RK808_ID_DCDC1,
808 		.ops = &rk808_buck1_2_ops,
809 		.type = REGULATOR_VOLTAGE,
810 		.min_uV = 712500,
811 		.uV_step = 12500,
812 		.n_voltages = 64,
813 		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
814 		.vsel_mask = RK808_BUCK_VSEL_MASK,
815 		.enable_reg = RK808_DCDC_EN_REG,
816 		.enable_mask = BIT(0),
817 		.owner = THIS_MODULE,
818 	}, {
819 		.name = "DCDC_REG2",
820 		.supply_name = "vcc2",
821 		.of_match = of_match_ptr("DCDC_REG2"),
822 		.regulators_node = of_match_ptr("regulators"),
823 		.id = RK808_ID_DCDC2,
824 		.ops = &rk808_buck1_2_ops,
825 		.type = REGULATOR_VOLTAGE,
826 		.min_uV = 712500,
827 		.uV_step = 12500,
828 		.n_voltages = 64,
829 		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
830 		.vsel_mask = RK808_BUCK_VSEL_MASK,
831 		.enable_reg = RK808_DCDC_EN_REG,
832 		.enable_mask = BIT(1),
833 		.owner = THIS_MODULE,
834 	}, {
835 		.name = "DCDC_REG3",
836 		.supply_name = "vcc3",
837 		.of_match = of_match_ptr("DCDC_REG3"),
838 		.regulators_node = of_match_ptr("regulators"),
839 		.id = RK808_ID_DCDC3,
840 		.ops = &rk808_switch_ops,
841 		.type = REGULATOR_VOLTAGE,
842 		.n_voltages = 1,
843 		.enable_reg = RK808_DCDC_EN_REG,
844 		.enable_mask = BIT(2),
845 		.owner = THIS_MODULE,
846 	},
847 	RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
848 		RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
849 		RK808_DCDC_EN_REG, BIT(3), 0),
850 	RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
851 		RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
852 		BIT(0), 400),
853 	RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
854 		RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
855 		BIT(1), 400),
856 	{
857 		.name = "LDO_REG3",
858 		.supply_name = "vcc7",
859 		.of_match = of_match_ptr("LDO_REG3"),
860 		.regulators_node = of_match_ptr("regulators"),
861 		.id = RK808_ID_LDO3,
862 		.ops = &rk808_reg_ops_ranges,
863 		.type = REGULATOR_VOLTAGE,
864 		.n_voltages = 16,
865 		.linear_ranges = rk808_ldo3_voltage_ranges,
866 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
867 		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
868 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
869 		.enable_reg = RK808_LDO_EN_REG,
870 		.enable_mask = BIT(2),
871 		.enable_time = 400,
872 		.owner = THIS_MODULE,
873 	},
874 	RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
875 		RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
876 		BIT(3), 400),
877 	RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
878 		RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
879 		BIT(4), 400),
880 	RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
881 		RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
882 		BIT(5), 400),
883 	RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
884 		RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
885 		BIT(6), 400),
886 	RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
887 		RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
888 		BIT(7), 400),
889 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
890 		RK808_DCDC_EN_REG, BIT(5)),
891 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
892 		RK808_DCDC_EN_REG, BIT(6)),
893 };
894 
895 static const struct regulator_desc rk809_reg[] = {
896 	{
897 		.name = "DCDC_REG1",
898 		.supply_name = "vcc1",
899 		.of_match = of_match_ptr("DCDC_REG1"),
900 		.regulators_node = of_match_ptr("regulators"),
901 		.id = RK817_ID_DCDC1,
902 		.ops = &rk817_buck_ops_range,
903 		.type = REGULATOR_VOLTAGE,
904 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
905 		.linear_ranges = rk817_buck1_voltage_ranges,
906 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
907 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
908 		.vsel_mask = RK817_BUCK_VSEL_MASK,
909 		.enable_reg = RK817_POWER_EN_REG(0),
910 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
911 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
912 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
913 		.of_map_mode = rk8xx_regulator_of_map_mode,
914 		.owner = THIS_MODULE,
915 	}, {
916 		.name = "DCDC_REG2",
917 		.supply_name = "vcc2",
918 		.of_match = of_match_ptr("DCDC_REG2"),
919 		.regulators_node = of_match_ptr("regulators"),
920 		.id = RK817_ID_DCDC2,
921 		.ops = &rk817_buck_ops_range,
922 		.type = REGULATOR_VOLTAGE,
923 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
924 		.linear_ranges = rk817_buck1_voltage_ranges,
925 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
926 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
927 		.vsel_mask = RK817_BUCK_VSEL_MASK,
928 		.enable_reg = RK817_POWER_EN_REG(0),
929 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
930 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
931 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
932 		.of_map_mode = rk8xx_regulator_of_map_mode,
933 		.owner = THIS_MODULE,
934 	}, {
935 		.name = "DCDC_REG3",
936 		.supply_name = "vcc3",
937 		.of_match = of_match_ptr("DCDC_REG3"),
938 		.regulators_node = of_match_ptr("regulators"),
939 		.id = RK817_ID_DCDC3,
940 		.ops = &rk817_buck_ops_range,
941 		.type = REGULATOR_VOLTAGE,
942 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
943 		.linear_ranges = rk817_buck1_voltage_ranges,
944 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
945 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
946 		.vsel_mask = RK817_BUCK_VSEL_MASK,
947 		.enable_reg = RK817_POWER_EN_REG(0),
948 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
949 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
950 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
951 		.of_map_mode = rk8xx_regulator_of_map_mode,
952 		.owner = THIS_MODULE,
953 	}, {
954 		.name = "DCDC_REG4",
955 		.supply_name = "vcc4",
956 		.of_match = of_match_ptr("DCDC_REG4"),
957 		.regulators_node = of_match_ptr("regulators"),
958 		.id = RK817_ID_DCDC4,
959 		.ops = &rk817_buck_ops_range,
960 		.type = REGULATOR_VOLTAGE,
961 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
962 		.linear_ranges = rk817_buck3_voltage_ranges,
963 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
964 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
965 		.vsel_mask = RK817_BUCK_VSEL_MASK,
966 		.enable_reg = RK817_POWER_EN_REG(0),
967 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
968 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
969 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
970 		.of_map_mode = rk8xx_regulator_of_map_mode,
971 		.owner = THIS_MODULE,
972 	},
973 	{
974 		.name = "DCDC_REG5",
975 		.supply_name = "vcc9",
976 		.of_match = of_match_ptr("DCDC_REG5"),
977 		.regulators_node = of_match_ptr("regulators"),
978 		.id = RK809_ID_DCDC5,
979 		.ops = &rk809_buck5_ops_range,
980 		.type = REGULATOR_VOLTAGE,
981 		.n_voltages = RK809_BUCK5_SEL_CNT,
982 		.linear_ranges = rk809_buck5_voltage_ranges,
983 		.n_linear_ranges = ARRAY_SIZE(rk809_buck5_voltage_ranges),
984 		.vsel_reg = RK809_BUCK5_CONFIG(0),
985 		.vsel_mask = RK809_BUCK5_VSEL_MASK,
986 		.enable_reg = RK817_POWER_EN_REG(3),
987 		.enable_mask = ENABLE_MASK(1),
988 		.enable_val = ENABLE_MASK(1),
989 		.disable_val = DISABLE_VAL(1),
990 		.of_map_mode = rk8xx_regulator_of_map_mode,
991 		.owner = THIS_MODULE,
992 	},
993 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
994 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
995 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
996 		   DISABLE_VAL(0), 400),
997 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
998 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
999 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1000 		   DISABLE_VAL(1), 400),
1001 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1002 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1003 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1004 		   DISABLE_VAL(2), 400),
1005 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1006 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1007 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1008 		   DISABLE_VAL(3), 400),
1009 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1010 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1011 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1012 		   DISABLE_VAL(0), 400),
1013 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1014 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1015 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1016 		   DISABLE_VAL(1), 400),
1017 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1018 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1019 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1020 		   DISABLE_VAL(2), 400),
1021 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1022 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1023 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1024 		   DISABLE_VAL(3), 400),
1025 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1026 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1027 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1028 		   DISABLE_VAL(0), 400),
1029 	RK817_DESC_SWITCH(RK809_ID_SW1, "SWITCH_REG1", "vcc9",
1030 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1031 			  DISABLE_VAL(2)),
1032 	RK817_DESC_SWITCH(RK809_ID_SW2, "SWITCH_REG2", "vcc8",
1033 			  RK817_POWER_EN_REG(3), ENABLE_MASK(3),
1034 			  DISABLE_VAL(3)),
1035 };
1036 
1037 static const struct regulator_desc rk817_reg[] = {
1038 	{
1039 		.name = "DCDC_REG1",
1040 		.supply_name = "vcc1",
1041 		.of_match = of_match_ptr("DCDC_REG1"),
1042 		.regulators_node = of_match_ptr("regulators"),
1043 		.id = RK817_ID_DCDC1,
1044 		.ops = &rk817_buck_ops_range,
1045 		.type = REGULATOR_VOLTAGE,
1046 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1047 		.linear_ranges = rk817_buck1_voltage_ranges,
1048 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1049 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
1050 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1051 		.enable_reg = RK817_POWER_EN_REG(0),
1052 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
1053 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
1054 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
1055 		.of_map_mode = rk8xx_regulator_of_map_mode,
1056 		.owner = THIS_MODULE,
1057 	}, {
1058 		.name = "DCDC_REG2",
1059 		.supply_name = "vcc2",
1060 		.of_match = of_match_ptr("DCDC_REG2"),
1061 		.regulators_node = of_match_ptr("regulators"),
1062 		.id = RK817_ID_DCDC2,
1063 		.ops = &rk817_buck_ops_range,
1064 		.type = REGULATOR_VOLTAGE,
1065 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1066 		.linear_ranges = rk817_buck1_voltage_ranges,
1067 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1068 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
1069 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1070 		.enable_reg = RK817_POWER_EN_REG(0),
1071 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
1072 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
1073 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
1074 		.of_map_mode = rk8xx_regulator_of_map_mode,
1075 		.owner = THIS_MODULE,
1076 	}, {
1077 		.name = "DCDC_REG3",
1078 		.supply_name = "vcc3",
1079 		.of_match = of_match_ptr("DCDC_REG3"),
1080 		.regulators_node = of_match_ptr("regulators"),
1081 		.id = RK817_ID_DCDC3,
1082 		.ops = &rk817_buck_ops_range,
1083 		.type = REGULATOR_VOLTAGE,
1084 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1085 		.linear_ranges = rk817_buck1_voltage_ranges,
1086 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1087 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
1088 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1089 		.enable_reg = RK817_POWER_EN_REG(0),
1090 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
1091 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
1092 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
1093 		.of_map_mode = rk8xx_regulator_of_map_mode,
1094 		.owner = THIS_MODULE,
1095 	}, {
1096 		.name = "DCDC_REG4",
1097 		.supply_name = "vcc4",
1098 		.of_match = of_match_ptr("DCDC_REG4"),
1099 		.regulators_node = of_match_ptr("regulators"),
1100 		.id = RK817_ID_DCDC4,
1101 		.ops = &rk817_buck_ops_range,
1102 		.type = REGULATOR_VOLTAGE,
1103 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
1104 		.linear_ranges = rk817_buck3_voltage_ranges,
1105 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
1106 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
1107 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1108 		.enable_reg = RK817_POWER_EN_REG(0),
1109 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
1110 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
1111 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
1112 		.of_map_mode = rk8xx_regulator_of_map_mode,
1113 		.owner = THIS_MODULE,
1114 	},
1115 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
1116 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
1117 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
1118 		   DISABLE_VAL(0), 400),
1119 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
1120 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
1121 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1122 		   DISABLE_VAL(1), 400),
1123 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1124 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1125 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1126 		   DISABLE_VAL(2), 400),
1127 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1128 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1129 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1130 		   DISABLE_VAL(3), 400),
1131 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1132 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1133 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1134 		   DISABLE_VAL(0), 400),
1135 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1136 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1137 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1138 		   DISABLE_VAL(1), 400),
1139 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1140 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1141 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1142 		   DISABLE_VAL(2), 400),
1143 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1144 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1145 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1146 		   DISABLE_VAL(3), 400),
1147 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1148 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1149 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1150 		   DISABLE_VAL(0), 400),
1151 	RK817_BOOST_DESC(RK817_ID_BOOST, "BOOST", "vcc8", 4700, 5400, 100,
1152 			 RK817_BOOST_OTG_CFG, RK817_BOOST_VSEL_MASK,
1153 			 RK817_POWER_EN_REG(3), ENABLE_MASK(1), ENABLE_MASK(1),
1154 		   DISABLE_VAL(1), 400, 3500 - 5400),
1155 	RK817_DESC_SWITCH(RK817_ID_BOOST_OTG_SW, "OTG_SWITCH", "vcc9",
1156 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1157 			  DISABLE_VAL(2)),
1158 };
1159 
1160 static const struct regulator_desc rk818_reg[] = {
1161 	{
1162 		.name = "DCDC_REG1",
1163 		.supply_name = "vcc1",
1164 		.of_match = of_match_ptr("DCDC_REG1"),
1165 		.regulators_node = of_match_ptr("regulators"),
1166 		.id = RK818_ID_DCDC1,
1167 		.ops = &rk808_reg_ops,
1168 		.type = REGULATOR_VOLTAGE,
1169 		.min_uV = 712500,
1170 		.uV_step = 12500,
1171 		.n_voltages = 64,
1172 		.vsel_reg = RK818_BUCK1_ON_VSEL_REG,
1173 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1174 		.enable_reg = RK818_DCDC_EN_REG,
1175 		.enable_mask = BIT(0),
1176 		.owner = THIS_MODULE,
1177 	}, {
1178 		.name = "DCDC_REG2",
1179 		.supply_name = "vcc2",
1180 		.of_match = of_match_ptr("DCDC_REG2"),
1181 		.regulators_node = of_match_ptr("regulators"),
1182 		.id = RK818_ID_DCDC2,
1183 		.ops = &rk808_reg_ops,
1184 		.type = REGULATOR_VOLTAGE,
1185 		.min_uV = 712500,
1186 		.uV_step = 12500,
1187 		.n_voltages = 64,
1188 		.vsel_reg = RK818_BUCK2_ON_VSEL_REG,
1189 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1190 		.enable_reg = RK818_DCDC_EN_REG,
1191 		.enable_mask = BIT(1),
1192 		.owner = THIS_MODULE,
1193 	}, {
1194 		.name = "DCDC_REG3",
1195 		.supply_name = "vcc3",
1196 		.of_match = of_match_ptr("DCDC_REG3"),
1197 		.regulators_node = of_match_ptr("regulators"),
1198 		.id = RK818_ID_DCDC3,
1199 		.ops = &rk808_switch_ops,
1200 		.type = REGULATOR_VOLTAGE,
1201 		.n_voltages = 1,
1202 		.enable_reg = RK818_DCDC_EN_REG,
1203 		.enable_mask = BIT(2),
1204 		.owner = THIS_MODULE,
1205 	},
1206 	RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
1207 		RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
1208 		RK818_DCDC_EN_REG, BIT(3), 0),
1209 	RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
1210 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
1211 		RK818_DCDC_EN_REG, BIT(4), 0),
1212 	RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
1213 		RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1214 		BIT(0), 400),
1215 	RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
1216 		RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1217 		BIT(1), 400),
1218 	{
1219 		.name = "LDO_REG3",
1220 		.supply_name = "vcc7",
1221 		.of_match = of_match_ptr("LDO_REG3"),
1222 		.regulators_node = of_match_ptr("regulators"),
1223 		.id = RK818_ID_LDO3,
1224 		.ops = &rk808_reg_ops_ranges,
1225 		.type = REGULATOR_VOLTAGE,
1226 		.n_voltages = 16,
1227 		.linear_ranges = rk808_ldo3_voltage_ranges,
1228 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
1229 		.vsel_reg = RK818_LDO3_ON_VSEL_REG,
1230 		.vsel_mask = RK818_LDO3_ON_VSEL_MASK,
1231 		.enable_reg = RK818_LDO_EN_REG,
1232 		.enable_mask = BIT(2),
1233 		.enable_time = 400,
1234 		.owner = THIS_MODULE,
1235 	},
1236 	RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
1237 		RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1238 		BIT(3), 400),
1239 	RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
1240 		RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1241 		BIT(4), 400),
1242 	RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
1243 		RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1244 		BIT(5), 400),
1245 	RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
1246 		RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1247 		BIT(6), 400),
1248 	RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
1249 		RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1250 		BIT(7), 400),
1251 	RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
1252 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
1253 		RK818_DCDC_EN_REG, BIT(5), 400),
1254 	RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
1255 		RK818_DCDC_EN_REG, BIT(6)),
1256 	RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
1257 		RK818_H5V_EN_REG, BIT(0)),
1258 	RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
1259 		RK818_DCDC_EN_REG, BIT(7)),
1260 };
1261 
1262 static int rk808_regulator_dt_parse_pdata(struct device *dev,
1263 				   struct device *client_dev,
1264 				   struct regmap *map,
1265 				   struct rk808_regulator_data *pdata)
1266 {
1267 	struct device_node *np;
1268 	int tmp, ret = 0, i;
1269 
1270 	np = of_get_child_by_name(client_dev->of_node, "regulators");
1271 	if (!np)
1272 		return -ENXIO;
1273 
1274 	for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
1275 		pdata->dvs_gpio[i] =
1276 			devm_gpiod_get_index_optional(client_dev, "dvs", i,
1277 						      GPIOD_OUT_LOW);
1278 		if (IS_ERR(pdata->dvs_gpio[i])) {
1279 			ret = PTR_ERR(pdata->dvs_gpio[i]);
1280 			dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
1281 			goto dt_parse_end;
1282 		}
1283 
1284 		if (!pdata->dvs_gpio[i]) {
1285 			dev_info(dev, "there is no dvs%d gpio\n", i);
1286 			continue;
1287 		}
1288 
1289 		tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
1290 		ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
1291 				gpiod_is_active_low(pdata->dvs_gpio[i]) ?
1292 				0 : tmp);
1293 	}
1294 
1295 dt_parse_end:
1296 	of_node_put(np);
1297 	return ret;
1298 }
1299 
1300 static int rk808_regulator_probe(struct platform_device *pdev)
1301 {
1302 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
1303 	struct i2c_client *client = rk808->i2c;
1304 	struct regulator_config config = {};
1305 	struct regulator_dev *rk808_rdev;
1306 	struct rk808_regulator_data *pdata;
1307 	const struct regulator_desc *regulators;
1308 	int ret, i, nregulators;
1309 
1310 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1311 	if (!pdata)
1312 		return -ENOMEM;
1313 
1314 	ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
1315 					     rk808->regmap, pdata);
1316 	if (ret < 0)
1317 		return ret;
1318 
1319 	platform_set_drvdata(pdev, pdata);
1320 
1321 	switch (rk808->variant) {
1322 	case RK805_ID:
1323 		regulators = rk805_reg;
1324 		nregulators = RK805_NUM_REGULATORS;
1325 		break;
1326 	case RK808_ID:
1327 		regulators = rk808_reg;
1328 		nregulators = RK808_NUM_REGULATORS;
1329 		break;
1330 	case RK809_ID:
1331 		regulators = rk809_reg;
1332 		nregulators = RK809_NUM_REGULATORS;
1333 		break;
1334 	case RK817_ID:
1335 		regulators = rk817_reg;
1336 		nregulators = RK817_NUM_REGULATORS;
1337 		break;
1338 	case RK818_ID:
1339 		regulators = rk818_reg;
1340 		nregulators = RK818_NUM_REGULATORS;
1341 		break;
1342 	default:
1343 		dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
1344 			rk808->variant);
1345 		return -EINVAL;
1346 	}
1347 
1348 	config.dev = &client->dev;
1349 	config.driver_data = pdata;
1350 	config.regmap = rk808->regmap;
1351 
1352 	/* Instantiate the regulators */
1353 	for (i = 0; i < nregulators; i++) {
1354 		rk808_rdev = devm_regulator_register(&pdev->dev,
1355 						     &regulators[i], &config);
1356 		if (IS_ERR(rk808_rdev)) {
1357 			dev_err(&client->dev,
1358 				"failed to register %d regulator\n", i);
1359 			return PTR_ERR(rk808_rdev);
1360 		}
1361 	}
1362 
1363 	return 0;
1364 }
1365 
1366 static struct platform_driver rk808_regulator_driver = {
1367 	.probe = rk808_regulator_probe,
1368 	.driver = {
1369 		.name = "rk808-regulator"
1370 	},
1371 };
1372 
1373 module_platform_driver(rk808_regulator_driver);
1374 
1375 MODULE_DESCRIPTION("regulator driver for the RK805/RK808/RK818 series PMICs");
1376 MODULE_AUTHOR("Tony xie <tony.xie@rock-chips.com>");
1377 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1378 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
1379 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
1380 MODULE_LICENSE("GPL");
1381 MODULE_ALIAS("platform:rk808-regulator");
1382