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  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
7  *
8  * Author: Chris Zhong <zyw@rock-chips.com>
9  * Author: Zhang Qing <zhangqing@rock-chips.com>
10  * Author: Xu Shengfei <xsf@rock-chips.com>
11  *
12  * Copyright (C) 2016 PHYTEC Messtechnik GmbH
13  *
14  * Author: Wadim Egorov <w.egorov@phytec.de>
15  */
16 
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_gpio.h>
22 #include <linux/mfd/rk808.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/of_regulator.h>
26 #include <linux/gpio/consumer.h>
27 
28 /* Field Definitions */
29 #define RK808_BUCK_VSEL_MASK	0x3f
30 #define RK808_BUCK4_VSEL_MASK	0xf
31 #define RK808_LDO_VSEL_MASK	0x1f
32 
33 #define RK809_BUCK5_VSEL_MASK		0x7
34 
35 #define RK817_LDO_VSEL_MASK		0x7f
36 #define RK817_BOOST_VSEL_MASK		0x7
37 #define RK817_BUCK_VSEL_MASK		0x7f
38 
39 #define RK818_BUCK_VSEL_MASK		0x3f
40 #define RK818_BUCK4_VSEL_MASK		0x1f
41 #define RK818_LDO_VSEL_MASK		0x1f
42 #define RK818_LDO3_ON_VSEL_MASK		0xf
43 #define RK818_BOOST_ON_VSEL_MASK	0xe0
44 
45 #define RK806_DCDC_SLP_REG_OFFSET	0x0A
46 #define RK806_NLDO_SLP_REG_OFFSET	0x05
47 #define RK806_PLDO_SLP_REG_OFFSET	0x06
48 
49 #define RK806_BUCK_SEL_CNT		0xff
50 #define RK806_LDO_SEL_CNT		0xff
51 
52 /* Ramp rate definitions for buck1 / buck2 only */
53 #define RK808_RAMP_RATE_OFFSET		3
54 #define RK808_RAMP_RATE_MASK		(3 << RK808_RAMP_RATE_OFFSET)
55 #define RK808_RAMP_RATE_2MV_PER_US	(0 << RK808_RAMP_RATE_OFFSET)
56 #define RK808_RAMP_RATE_4MV_PER_US	(1 << RK808_RAMP_RATE_OFFSET)
57 #define RK808_RAMP_RATE_6MV_PER_US	(2 << RK808_RAMP_RATE_OFFSET)
58 #define RK808_RAMP_RATE_10MV_PER_US	(3 << RK808_RAMP_RATE_OFFSET)
59 
60 #define RK808_DVS2_POL		BIT(2)
61 #define RK808_DVS1_POL		BIT(1)
62 
63 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
64 #define RK808_SLP_REG_OFFSET 1
65 
66 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
67 #define RK808_DVS_REG_OFFSET 2
68 
69 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
70 #define RK808_SLP_SET_OFF_REG_OFFSET 2
71 
72 /* max steps for increase voltage of Buck1/2, equal 100mv*/
73 #define MAX_STEPS_ONE_TIME 8
74 
75 #define ENABLE_MASK(id)			(BIT(id) | BIT(4 + (id)))
76 #define DISABLE_VAL(id)			(BIT(4 + (id)))
77 
78 #define RK817_BOOST_DESC(_id, _match, _supply, _min, _max, _step, _vreg,\
79 	_vmask, _ereg, _emask, _enval, _disval, _etime, m_drop)		\
80 	{							\
81 		.name		= (_match),				\
82 		.supply_name	= (_supply),				\
83 		.of_match	= of_match_ptr(_match),			\
84 		.regulators_node = of_match_ptr("regulators"),		\
85 		.type		= REGULATOR_VOLTAGE,			\
86 		.id		= (_id),				\
87 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
88 		.owner		= THIS_MODULE,				\
89 		.min_uV		= (_min) * 1000,			\
90 		.uV_step	= (_step) * 1000,			\
91 		.vsel_reg	= (_vreg),				\
92 		.vsel_mask	= (_vmask),				\
93 		.enable_reg	= (_ereg),				\
94 		.enable_mask	= (_emask),				\
95 		.enable_val     = (_enval),				\
96 		.disable_val     = (_disval),				\
97 		.enable_time	= (_etime),				\
98 		.min_dropout_uV = (m_drop) * 1000,			\
99 		.ops		= &rk817_boost_ops,			\
100 	}
101 
102 #define RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
103 	_vmask, _ereg, _emask, _enval, _disval, _etime, _ops)		\
104 	{								\
105 		.name		= (_match),				\
106 		.supply_name	= (_supply),				\
107 		.of_match	= of_match_ptr(_match),			\
108 		.regulators_node = of_match_ptr("regulators"),		\
109 		.type		= REGULATOR_VOLTAGE,			\
110 		.id		= (_id),				\
111 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
112 		.owner		= THIS_MODULE,				\
113 		.min_uV		= (_min) * 1000,			\
114 		.uV_step	= (_step) * 1000,			\
115 		.vsel_reg	= (_vreg),				\
116 		.vsel_mask	= (_vmask),				\
117 		.enable_reg	= (_ereg),				\
118 		.enable_mask	= (_emask),				\
119 		.enable_val     = (_enval),				\
120 		.disable_val     = (_disval),				\
121 		.enable_time	= (_etime),				\
122 		.ops		= _ops,			\
123 	}
124 
125 #define RK805_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
126 	_vmask, _ereg, _emask, _etime)					\
127 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
128 	_vmask, _ereg, _emask, 0, 0, _etime, &rk805_reg_ops)
129 
130 #define RK806_REGULATOR(_name, _supply_name, _id, _ops,\
131 			_n_voltages, _vr, _er, _lr, ctrl_bit,\
132 			_rr, _rm, _rt)\
133 [_id] = {\
134 		.name = _name,\
135 		.supply_name = _supply_name,\
136 		.of_match = of_match_ptr(_name),\
137 		.regulators_node = of_match_ptr("regulators"),\
138 		.id = _id,\
139 		.ops = &_ops,\
140 		.type = REGULATOR_VOLTAGE,\
141 		.n_voltages = _n_voltages,\
142 		.linear_ranges = _lr,\
143 		.n_linear_ranges = ARRAY_SIZE(_lr),\
144 		.vsel_reg = _vr,\
145 		.vsel_mask = 0xff,\
146 		.enable_reg = _er,\
147 		.enable_mask = ENABLE_MASK(ctrl_bit),\
148 		.enable_val = ENABLE_MASK(ctrl_bit),\
149 		.disable_val = DISABLE_VAL(ctrl_bit),\
150 		.of_map_mode = rk8xx_regulator_of_map_mode,\
151 		.ramp_reg = _rr,\
152 		.ramp_mask = _rm,\
153 		.ramp_delay_table = _rt, \
154 		.n_ramp_values = ARRAY_SIZE(_rt), \
155 		.owner = THIS_MODULE,\
156 	}
157 
158 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
159 	_vmask, _ereg, _emask, _etime)					\
160 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
161 	_vmask, _ereg, _emask, 0, 0, _etime, &rk808_reg_ops)
162 
163 #define RK817_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
164 	_vmask, _ereg, _emask, _disval, _etime)				\
165 	RK8XX_DESC_COM(_id, _match, _supply, _min, _max, _step, _vreg,	\
166 	_vmask, _ereg, _emask, _emask, _disval, _etime, &rk817_reg_ops)
167 
168 #define RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
169 	_enval, _disval, _ops)						\
170 	{								\
171 		.name		= (_match),				\
172 		.supply_name	= (_supply),				\
173 		.of_match	= of_match_ptr(_match),			\
174 		.regulators_node = of_match_ptr("regulators"),		\
175 		.type		= REGULATOR_VOLTAGE,			\
176 		.id		= (_id),				\
177 		.enable_reg	= (_ereg),				\
178 		.enable_mask	= (_emask),				\
179 		.enable_val     = (_enval),				\
180 		.disable_val     = (_disval),				\
181 		.owner		= THIS_MODULE,				\
182 		.ops		= _ops					\
183 	}
184 
185 #define RK817_DESC_SWITCH(_id, _match, _supply, _ereg, _emask,		\
186 	_disval)							\
187 	RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
188 	_emask, _disval, &rk817_switch_ops)
189 
190 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask)		\
191 	RKXX_DESC_SWITCH_COM(_id, _match, _supply, _ereg, _emask,	\
192 	0, 0, &rk808_switch_ops)
193 
194 struct rk8xx_register_bit {
195 	u8 reg;
196 	u8 bit;
197 };
198 
199 #define RK8XX_REG_BIT(_reg, _bit)					\
200 	{								\
201 		.reg = _reg,						\
202 		.bit = BIT(_bit),						\
203 	}
204 
205 struct rk808_regulator_data {
206 	struct gpio_desc *dvs_gpio[2];
207 };
208 
209 static const struct linear_range rk808_ldo3_voltage_ranges[] = {
210 	REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
211 	REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
212 };
213 
214 #define RK809_BUCK5_SEL_CNT		(8)
215 
216 static const struct linear_range rk809_buck5_voltage_ranges[] = {
217 	REGULATOR_LINEAR_RANGE(1500000, 0, 0, 0),
218 	REGULATOR_LINEAR_RANGE(1800000, 1, 3, 200000),
219 	REGULATOR_LINEAR_RANGE(2800000, 4, 5, 200000),
220 	REGULATOR_LINEAR_RANGE(3300000, 6, 7, 300000),
221 };
222 
223 #define RK817_BUCK1_MIN0 500000
224 #define RK817_BUCK1_MAX0 1500000
225 
226 #define RK817_BUCK1_MIN1 1600000
227 #define RK817_BUCK1_MAX1 2400000
228 
229 #define RK817_BUCK3_MAX1 3400000
230 
231 #define RK817_BUCK1_STP0 12500
232 #define RK817_BUCK1_STP1 100000
233 
234 #define RK817_BUCK1_SEL0 ((RK817_BUCK1_MAX0 - RK817_BUCK1_MIN0) /\
235 						  RK817_BUCK1_STP0)
236 #define RK817_BUCK1_SEL1 ((RK817_BUCK1_MAX1 - RK817_BUCK1_MIN1) /\
237 						  RK817_BUCK1_STP1)
238 
239 #define RK817_BUCK3_SEL1 ((RK817_BUCK3_MAX1 - RK817_BUCK1_MIN1) /\
240 						  RK817_BUCK1_STP1)
241 
242 #define RK817_BUCK1_SEL_CNT (RK817_BUCK1_SEL0 + RK817_BUCK1_SEL1 + 1)
243 #define RK817_BUCK3_SEL_CNT (RK817_BUCK1_SEL0 + RK817_BUCK3_SEL1 + 1)
244 
245 static const struct linear_range rk817_buck1_voltage_ranges[] = {
246 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN0, 0,
247 			       RK817_BUCK1_SEL0, RK817_BUCK1_STP0),
248 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN1, RK817_BUCK1_SEL0 + 1,
249 			       RK817_BUCK1_SEL_CNT, RK817_BUCK1_STP1),
250 };
251 
252 static const struct linear_range rk817_buck3_voltage_ranges[] = {
253 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN0, 0,
254 			       RK817_BUCK1_SEL0, RK817_BUCK1_STP0),
255 	REGULATOR_LINEAR_RANGE(RK817_BUCK1_MIN1, RK817_BUCK1_SEL0 + 1,
256 			       RK817_BUCK3_SEL_CNT, RK817_BUCK1_STP1),
257 };
258 
259 static const unsigned int rk808_buck1_2_ramp_table[] = {
260 	2000, 4000, 6000, 10000
261 };
262 
263 /* RK817 RK809 */
264 static const unsigned int rk817_buck1_4_ramp_table[] = {
265 	3000, 6300, 12500, 25000
266 };
267 
268 static int rk806_set_mode_dcdc(struct regulator_dev *rdev, unsigned int mode)
269 {
270 	int rid = rdev_get_id(rdev);
271 	int ctr_bit, reg;
272 
273 	reg = RK806_POWER_FPWM_EN0 + rid / 8;
274 	ctr_bit = rid % 8;
275 
276 	switch (mode) {
277 	case REGULATOR_MODE_FAST:
278 		return regmap_update_bits(rdev->regmap, reg,
279 					  PWM_MODE_MSK << ctr_bit,
280 					  FPWM_MODE << ctr_bit);
281 	case REGULATOR_MODE_NORMAL:
282 		return regmap_update_bits(rdev->regmap, reg,
283 					  PWM_MODE_MSK << ctr_bit,
284 					  AUTO_PWM_MODE << ctr_bit);
285 	default:
286 		dev_err(rdev_get_dev(rdev), "mode unsupported: %u\n", mode);
287 		return -EINVAL;
288 	}
289 
290 	return 0;
291 }
292 
293 static unsigned int rk806_get_mode_dcdc(struct regulator_dev *rdev)
294 {
295 	int rid = rdev_get_id(rdev);
296 	int ctr_bit, reg;
297 	unsigned int val;
298 	int err;
299 
300 	reg = RK806_POWER_FPWM_EN0 + rid / 8;
301 	ctr_bit = rid % 8;
302 
303 	err = regmap_read(rdev->regmap, reg, &val);
304 	if (err)
305 		return err;
306 
307 	if ((val >> ctr_bit) & FPWM_MODE)
308 		return REGULATOR_MODE_FAST;
309 	else
310 		return REGULATOR_MODE_NORMAL;
311 }
312 
313 static const struct rk8xx_register_bit rk806_dcdc_rate2[] = {
314 	RK8XX_REG_BIT(0xEB, 0),
315 	RK8XX_REG_BIT(0xEB, 1),
316 	RK8XX_REG_BIT(0xEB, 2),
317 	RK8XX_REG_BIT(0xEB, 3),
318 	RK8XX_REG_BIT(0xEB, 4),
319 	RK8XX_REG_BIT(0xEB, 5),
320 	RK8XX_REG_BIT(0xEB, 6),
321 	RK8XX_REG_BIT(0xEB, 7),
322 	RK8XX_REG_BIT(0xEA, 0),
323 	RK8XX_REG_BIT(0xEA, 1),
324 };
325 
326 static const unsigned int rk806_ramp_delay_table_dcdc[] = {
327 	50000, 25000, 12500, 6250, 3125, 1560, 961, 390
328 };
329 
330 static int rk806_set_ramp_delay_dcdc(struct regulator_dev *rdev, int ramp_delay)
331 {
332 	int rid = rdev_get_id(rdev);
333 	int regval, ramp_value, ret;
334 
335 	ret = regulator_find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
336 					    rdev->desc->n_ramp_values, &ramp_value);
337 	if (ret) {
338 		dev_warn(rdev_get_dev(rdev),
339 			 "Can't set ramp-delay %u, setting %u\n", ramp_delay,
340 			 rdev->desc->ramp_delay_table[ramp_value]);
341 	}
342 
343 	regval = ramp_value << (ffs(rdev->desc->ramp_mask) - 1);
344 
345 	ret = regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
346 				 rdev->desc->ramp_mask, regval);
347 	if (ret)
348 		return ret;
349 
350 	/*
351 	 * The above is effectively a copy of regulator_set_ramp_delay_regmap(),
352 	 * but that only stores the lower 2 bits for rk806 DCDC ramp. The MSB must
353 	 * be stored in a separate register, so this open codes the implementation
354 	 * to have access to the ramp_value.
355 	 */
356 
357 	regval = (ramp_value >> 2) & 0x1 ? rk806_dcdc_rate2[rid].bit : 0;
358 	return regmap_update_bits(rdev->regmap, rk806_dcdc_rate2[rid].reg,
359 				  rk806_dcdc_rate2[rid].bit,
360 				  regval);
361 }
362 
363 static const unsigned int rk806_ramp_delay_table_ldo[] = {
364 	100000, 50000, 25000, 12500, 6280, 3120, 1900, 780
365 };
366 
367 static int rk806_set_suspend_voltage_range(struct regulator_dev *rdev, int reg_offset, int uv)
368 {
369 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
370 	unsigned int reg;
371 
372 	if (sel < 0)
373 		return -EINVAL;
374 
375 	reg = rdev->desc->vsel_reg + reg_offset;
376 
377 	return regmap_update_bits(rdev->regmap, reg, rdev->desc->vsel_mask, sel);
378 }
379 
380 static int rk806_set_suspend_voltage_range_dcdc(struct regulator_dev *rdev, int uv)
381 {
382 	return rk806_set_suspend_voltage_range(rdev, RK806_DCDC_SLP_REG_OFFSET, uv);
383 }
384 
385 static int rk806_set_suspend_voltage_range_nldo(struct regulator_dev *rdev, int uv)
386 {
387 	return rk806_set_suspend_voltage_range(rdev, RK806_NLDO_SLP_REG_OFFSET, uv);
388 }
389 
390 static int rk806_set_suspend_voltage_range_pldo(struct regulator_dev *rdev, int uv)
391 {
392 	return rk806_set_suspend_voltage_range(rdev, RK806_PLDO_SLP_REG_OFFSET, uv);
393 }
394 
395 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
396 {
397 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
398 	int id = rdev_get_id(rdev);
399 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
400 	unsigned int val;
401 	int ret;
402 
403 	if (!gpio || gpiod_get_value(gpio) == 0)
404 		return regulator_get_voltage_sel_regmap(rdev);
405 
406 	ret = regmap_read(rdev->regmap,
407 			  rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
408 			  &val);
409 	if (ret != 0)
410 		return ret;
411 
412 	val &= rdev->desc->vsel_mask;
413 	val >>= ffs(rdev->desc->vsel_mask) - 1;
414 
415 	return val;
416 }
417 
418 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
419 					     unsigned sel)
420 {
421 	int ret, delta_sel;
422 	unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
423 
424 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
425 	if (ret != 0)
426 		return ret;
427 
428 	tmp = val & ~mask;
429 	old_sel = val & mask;
430 	old_sel >>= ffs(mask) - 1;
431 	delta_sel = sel - old_sel;
432 
433 	/*
434 	 * If directly modify the register to change the voltage, we will face
435 	 * the risk of overshoot. Put it into a multi-step, can effectively
436 	 * avoid this problem, a step is 100mv here.
437 	 */
438 	while (delta_sel > MAX_STEPS_ONE_TIME) {
439 		old_sel += MAX_STEPS_ONE_TIME;
440 		val = old_sel << (ffs(mask) - 1);
441 		val |= tmp;
442 
443 		/*
444 		 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
445 		 * 3 bytes (24 bits) plus start and stop so 26 bits.  So we've
446 		 * got more than 65 us between each voltage change and thus
447 		 * won't ramp faster than ~1500 uV / us.
448 		 */
449 		ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
450 		delta_sel = sel - old_sel;
451 	}
452 
453 	sel <<= ffs(mask) - 1;
454 	val = tmp | sel;
455 	ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
456 
457 	/*
458 	 * When we change the voltage register directly, the ramp rate is about
459 	 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
460 	 * so we needn't wait extra time after that.
461 	 */
462 	udelay(1);
463 
464 	return ret;
465 }
466 
467 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
468 					 unsigned sel)
469 {
470 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
471 	int id = rdev_get_id(rdev);
472 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
473 	unsigned int reg = rdev->desc->vsel_reg;
474 	unsigned old_sel;
475 	int ret, gpio_level;
476 
477 	if (!gpio)
478 		return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
479 
480 	gpio_level = gpiod_get_value(gpio);
481 	if (gpio_level == 0) {
482 		reg += RK808_DVS_REG_OFFSET;
483 		ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
484 	} else {
485 		ret = regmap_read(rdev->regmap,
486 				  reg + RK808_DVS_REG_OFFSET,
487 				  &old_sel);
488 	}
489 
490 	if (ret != 0)
491 		return ret;
492 
493 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
494 	sel |= old_sel & ~rdev->desc->vsel_mask;
495 
496 	ret = regmap_write(rdev->regmap, reg, sel);
497 	if (ret)
498 		return ret;
499 
500 	gpiod_set_value(gpio, !gpio_level);
501 
502 	return ret;
503 }
504 
505 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
506 				       unsigned int old_selector,
507 				       unsigned int new_selector)
508 {
509 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
510 	int id = rdev_get_id(rdev);
511 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
512 
513 	/* if there is no dvs1/2 pin, we don't need wait extra time here. */
514 	if (!gpio)
515 		return 0;
516 
517 	return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
518 }
519 
520 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
521 {
522 	unsigned int reg;
523 	int sel = regulator_map_voltage_linear(rdev, uv, uv);
524 
525 	if (sel < 0)
526 		return -EINVAL;
527 
528 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
529 
530 	return regmap_update_bits(rdev->regmap, reg,
531 				  rdev->desc->vsel_mask,
532 				  sel);
533 }
534 
535 static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
536 {
537 	unsigned int reg;
538 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
539 
540 	if (sel < 0)
541 		return -EINVAL;
542 
543 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
544 
545 	return regmap_update_bits(rdev->regmap, reg,
546 				  rdev->desc->vsel_mask,
547 				  sel);
548 }
549 
550 static int rk805_set_suspend_enable(struct regulator_dev *rdev)
551 {
552 	unsigned int reg;
553 
554 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
555 
556 	return regmap_update_bits(rdev->regmap, reg,
557 				  rdev->desc->enable_mask,
558 				  rdev->desc->enable_mask);
559 }
560 
561 static int rk805_set_suspend_disable(struct regulator_dev *rdev)
562 {
563 	unsigned int reg;
564 
565 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
566 
567 	return regmap_update_bits(rdev->regmap, reg,
568 				  rdev->desc->enable_mask,
569 				  0);
570 }
571 
572 static const struct rk8xx_register_bit rk806_suspend_bits[] = {
573 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 0),
574 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 1),
575 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 2),
576 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 3),
577 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 4),
578 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 5),
579 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 6),
580 	RK8XX_REG_BIT(RK806_POWER_SLP_EN0, 7),
581 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 6),
582 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 7),
583 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 0),
584 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 1),
585 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 2),
586 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 3),
587 	RK8XX_REG_BIT(RK806_POWER_SLP_EN1, 4),
588 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 1),
589 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 2),
590 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 3),
591 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 4),
592 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 5),
593 	RK8XX_REG_BIT(RK806_POWER_SLP_EN2, 0),
594 };
595 
596 static int rk806_set_suspend_enable(struct regulator_dev *rdev)
597 {
598 	int rid = rdev_get_id(rdev);
599 
600 	return regmap_update_bits(rdev->regmap, rk806_suspend_bits[rid].reg,
601 				  rk806_suspend_bits[rid].bit,
602 				  rk806_suspend_bits[rid].bit);
603 }
604 
605 static int rk806_set_suspend_disable(struct regulator_dev *rdev)
606 {
607 	int rid = rdev_get_id(rdev);
608 
609 	return regmap_update_bits(rdev->regmap, rk806_suspend_bits[rid].reg,
610 				  rk806_suspend_bits[rid].bit, 0);
611 }
612 
613 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
614 {
615 	unsigned int reg;
616 
617 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
618 
619 	return regmap_update_bits(rdev->regmap, reg,
620 				  rdev->desc->enable_mask,
621 				  0);
622 }
623 
624 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
625 {
626 	unsigned int reg;
627 
628 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
629 
630 	return regmap_update_bits(rdev->regmap, reg,
631 				  rdev->desc->enable_mask,
632 				  rdev->desc->enable_mask);
633 }
634 
635 static int rk817_set_suspend_enable_ctrl(struct regulator_dev *rdev,
636 					 unsigned int en)
637 {
638 	unsigned int reg;
639 	int id = rdev_get_id(rdev);
640 	unsigned int id_slp, msk, val;
641 
642 	if (id >= RK817_ID_DCDC1 && id <= RK817_ID_DCDC4)
643 		id_slp = id;
644 	else if (id >= RK817_ID_LDO1 && id <= RK817_ID_LDO8)
645 		id_slp = 8 + (id - RK817_ID_LDO1);
646 	else if (id >= RK817_ID_LDO9 && id <= RK809_ID_SW2)
647 		id_slp = 4 + (id - RK817_ID_LDO9);
648 	else
649 		return -EINVAL;
650 
651 	reg = RK817_POWER_SLP_EN_REG(id_slp / 8);
652 
653 	msk = BIT(id_slp % 8);
654 	if (en)
655 		val = msk;
656 	else
657 		val = 0;
658 
659 	return regmap_update_bits(rdev->regmap, reg, msk, val);
660 }
661 
662 static int rk817_set_suspend_enable(struct regulator_dev *rdev)
663 {
664 	return rk817_set_suspend_enable_ctrl(rdev, 1);
665 }
666 
667 static int rk817_set_suspend_disable(struct regulator_dev *rdev)
668 {
669 	return rk817_set_suspend_enable_ctrl(rdev, 0);
670 }
671 
672 static int rk8xx_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
673 {
674 	unsigned int reg;
675 
676 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
677 
678 	switch (mode) {
679 	case REGULATOR_MODE_FAST:
680 		return regmap_update_bits(rdev->regmap, reg,
681 					  PWM_MODE_MSK, FPWM_MODE);
682 	case REGULATOR_MODE_NORMAL:
683 		return regmap_update_bits(rdev->regmap, reg,
684 					  PWM_MODE_MSK, AUTO_PWM_MODE);
685 	default:
686 		dev_err(&rdev->dev, "do not support this mode\n");
687 		return -EINVAL;
688 	}
689 
690 	return 0;
691 }
692 
693 static int rk8xx_set_mode(struct regulator_dev *rdev, unsigned int mode)
694 {
695 	switch (mode) {
696 	case REGULATOR_MODE_FAST:
697 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
698 					  PWM_MODE_MSK, FPWM_MODE);
699 	case REGULATOR_MODE_NORMAL:
700 		return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
701 					  PWM_MODE_MSK, AUTO_PWM_MODE);
702 	default:
703 		dev_err(&rdev->dev, "do not support this mode\n");
704 		return -EINVAL;
705 	}
706 
707 	return 0;
708 }
709 
710 static unsigned int rk8xx_get_mode(struct regulator_dev *rdev)
711 {
712 	unsigned int val;
713 	int err;
714 
715 	err = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
716 	if (err)
717 		return err;
718 
719 	if (val & FPWM_MODE)
720 		return REGULATOR_MODE_FAST;
721 	else
722 		return REGULATOR_MODE_NORMAL;
723 }
724 
725 static int rk8xx_is_enabled_wmsk_regmap(struct regulator_dev *rdev)
726 {
727 	unsigned int val;
728 	int ret;
729 
730 	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
731 	if (ret != 0)
732 		return ret;
733 
734 	/* add write mask bit */
735 	val |= (rdev->desc->enable_mask & 0xf0);
736 	val &= rdev->desc->enable_mask;
737 
738 	if (rdev->desc->enable_is_inverted) {
739 		if (rdev->desc->enable_val)
740 			return val != rdev->desc->enable_val;
741 		return (val == 0);
742 	}
743 	if (rdev->desc->enable_val)
744 		return val == rdev->desc->enable_val;
745 	return val != 0;
746 }
747 
748 static unsigned int rk8xx_regulator_of_map_mode(unsigned int mode)
749 {
750 	switch (mode) {
751 	case 1:
752 		return REGULATOR_MODE_FAST;
753 	case 2:
754 		return REGULATOR_MODE_NORMAL;
755 	default:
756 		return REGULATOR_MODE_INVALID;
757 	}
758 }
759 
760 static const struct regulator_ops rk805_reg_ops = {
761 	.list_voltage           = regulator_list_voltage_linear,
762 	.map_voltage            = regulator_map_voltage_linear,
763 	.get_voltage_sel        = regulator_get_voltage_sel_regmap,
764 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
765 	.enable                 = regulator_enable_regmap,
766 	.disable                = regulator_disable_regmap,
767 	.is_enabled             = regulator_is_enabled_regmap,
768 	.set_suspend_voltage    = rk808_set_suspend_voltage,
769 	.set_suspend_enable     = rk805_set_suspend_enable,
770 	.set_suspend_disable    = rk805_set_suspend_disable,
771 };
772 
773 static const struct regulator_ops rk805_switch_ops = {
774 	.enable                 = regulator_enable_regmap,
775 	.disable                = regulator_disable_regmap,
776 	.is_enabled             = regulator_is_enabled_regmap,
777 	.set_suspend_enable     = rk805_set_suspend_enable,
778 	.set_suspend_disable    = rk805_set_suspend_disable,
779 };
780 
781 static const struct regulator_ops rk806_ops_dcdc = {
782 	.list_voltage		= regulator_list_voltage_linear_range,
783 	.map_voltage		= regulator_map_voltage_linear_range,
784 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
785 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
786 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
787 	.set_mode		= rk806_set_mode_dcdc,
788 	.get_mode		= rk806_get_mode_dcdc,
789 
790 	.enable			= regulator_enable_regmap,
791 	.disable		= regulator_disable_regmap,
792 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
793 
794 	.set_suspend_mode	= rk806_set_mode_dcdc,
795 	.set_ramp_delay		= rk806_set_ramp_delay_dcdc,
796 
797 	.set_suspend_voltage	= rk806_set_suspend_voltage_range_dcdc,
798 	.set_suspend_enable	= rk806_set_suspend_enable,
799 	.set_suspend_disable	= rk806_set_suspend_disable,
800 };
801 
802 static const struct regulator_ops rk806_ops_nldo = {
803 	.list_voltage		= regulator_list_voltage_linear_range,
804 	.map_voltage		= regulator_map_voltage_linear_range,
805 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
806 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
807 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
808 
809 	.enable			= regulator_enable_regmap,
810 	.disable		= regulator_disable_regmap,
811 	.is_enabled		= regulator_is_enabled_regmap,
812 
813 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
814 
815 	.set_suspend_voltage	= rk806_set_suspend_voltage_range_nldo,
816 	.set_suspend_enable	= rk806_set_suspend_enable,
817 	.set_suspend_disable	= rk806_set_suspend_disable,
818 };
819 
820 static const struct regulator_ops rk806_ops_pldo = {
821 	.list_voltage		= regulator_list_voltage_linear_range,
822 	.map_voltage		= regulator_map_voltage_linear_range,
823 
824 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
825 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
826 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
827 
828 	.enable			= regulator_enable_regmap,
829 	.disable		= regulator_disable_regmap,
830 	.is_enabled		= regulator_is_enabled_regmap,
831 
832 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
833 
834 	.set_suspend_voltage	= rk806_set_suspend_voltage_range_pldo,
835 	.set_suspend_enable	= rk806_set_suspend_enable,
836 	.set_suspend_disable	= rk806_set_suspend_disable,
837 };
838 
839 static const struct regulator_ops rk808_buck1_2_ops = {
840 	.list_voltage		= regulator_list_voltage_linear,
841 	.map_voltage		= regulator_map_voltage_linear,
842 	.get_voltage_sel	= rk808_buck1_2_get_voltage_sel_regmap,
843 	.set_voltage_sel	= rk808_buck1_2_set_voltage_sel,
844 	.set_voltage_time_sel	= rk808_buck1_2_set_voltage_time_sel,
845 	.enable			= regulator_enable_regmap,
846 	.disable		= regulator_disable_regmap,
847 	.is_enabled		= regulator_is_enabled_regmap,
848 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
849 	.set_suspend_voltage	= rk808_set_suspend_voltage,
850 	.set_suspend_enable	= rk808_set_suspend_enable,
851 	.set_suspend_disable	= rk808_set_suspend_disable,
852 };
853 
854 static const struct regulator_ops rk808_reg_ops = {
855 	.list_voltage		= regulator_list_voltage_linear,
856 	.map_voltage		= regulator_map_voltage_linear,
857 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
858 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
859 	.enable			= regulator_enable_regmap,
860 	.disable		= regulator_disable_regmap,
861 	.is_enabled		= regulator_is_enabled_regmap,
862 	.set_suspend_voltage	= rk808_set_suspend_voltage,
863 	.set_suspend_enable	= rk808_set_suspend_enable,
864 	.set_suspend_disable	= rk808_set_suspend_disable,
865 };
866 
867 static const struct regulator_ops rk808_reg_ops_ranges = {
868 	.list_voltage		= regulator_list_voltage_linear_range,
869 	.map_voltage		= regulator_map_voltage_linear_range,
870 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
871 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
872 	.enable			= regulator_enable_regmap,
873 	.disable		= regulator_disable_regmap,
874 	.is_enabled		= regulator_is_enabled_regmap,
875 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
876 	.set_suspend_enable	= rk808_set_suspend_enable,
877 	.set_suspend_disable	= rk808_set_suspend_disable,
878 };
879 
880 static const struct regulator_ops rk808_switch_ops = {
881 	.enable			= regulator_enable_regmap,
882 	.disable		= regulator_disable_regmap,
883 	.is_enabled		= regulator_is_enabled_regmap,
884 	.set_suspend_enable	= rk808_set_suspend_enable,
885 	.set_suspend_disable	= rk808_set_suspend_disable,
886 };
887 
888 static const struct linear_range rk805_buck_1_2_voltage_ranges[] = {
889 	REGULATOR_LINEAR_RANGE(712500, 0, 59, 12500),
890 	REGULATOR_LINEAR_RANGE(1800000, 60, 62, 200000),
891 	REGULATOR_LINEAR_RANGE(2300000, 63, 63, 0),
892 };
893 
894 static const struct regulator_ops rk809_buck5_ops_range = {
895 	.list_voltage		= regulator_list_voltage_linear_range,
896 	.map_voltage		= regulator_map_voltage_linear_range,
897 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
898 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
899 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
900 	.enable			= regulator_enable_regmap,
901 	.disable		= regulator_disable_regmap,
902 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
903 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
904 	.set_suspend_enable	= rk817_set_suspend_enable,
905 	.set_suspend_disable	= rk817_set_suspend_disable,
906 };
907 
908 static const struct regulator_ops rk817_reg_ops = {
909 	.list_voltage		= regulator_list_voltage_linear,
910 	.map_voltage		= regulator_map_voltage_linear,
911 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
912 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
913 	.enable			= regulator_enable_regmap,
914 	.disable		= regulator_disable_regmap,
915 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
916 	.set_suspend_voltage	= rk808_set_suspend_voltage,
917 	.set_suspend_enable	= rk817_set_suspend_enable,
918 	.set_suspend_disable	= rk817_set_suspend_disable,
919 };
920 
921 static const struct regulator_ops rk817_boost_ops = {
922 	.list_voltage		= regulator_list_voltage_linear,
923 	.map_voltage		= regulator_map_voltage_linear,
924 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
925 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
926 	.enable			= regulator_enable_regmap,
927 	.disable		= regulator_disable_regmap,
928 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
929 	.set_suspend_enable	= rk817_set_suspend_enable,
930 	.set_suspend_disable	= rk817_set_suspend_disable,
931 };
932 
933 static const struct regulator_ops rk817_buck_ops_range = {
934 	.list_voltage		= regulator_list_voltage_linear_range,
935 	.map_voltage		= regulator_map_voltage_linear_range,
936 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
937 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
938 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
939 	.enable			= regulator_enable_regmap,
940 	.disable		= regulator_disable_regmap,
941 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
942 	.set_mode		= rk8xx_set_mode,
943 	.get_mode		= rk8xx_get_mode,
944 	.set_suspend_mode	= rk8xx_set_suspend_mode,
945 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
946 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
947 	.set_suspend_enable	= rk817_set_suspend_enable,
948 	.set_suspend_disable	= rk817_set_suspend_disable,
949 };
950 
951 static const struct regulator_ops rk817_switch_ops = {
952 	.enable			= regulator_enable_regmap,
953 	.disable		= regulator_disable_regmap,
954 	.is_enabled		= rk8xx_is_enabled_wmsk_regmap,
955 	.set_suspend_enable	= rk817_set_suspend_enable,
956 	.set_suspend_disable	= rk817_set_suspend_disable,
957 };
958 
959 static const struct regulator_desc rk805_reg[] = {
960 	{
961 		.name = "DCDC_REG1",
962 		.supply_name = "vcc1",
963 		.of_match = of_match_ptr("DCDC_REG1"),
964 		.regulators_node = of_match_ptr("regulators"),
965 		.id = RK805_ID_DCDC1,
966 		.ops = &rk808_reg_ops_ranges,
967 		.type = REGULATOR_VOLTAGE,
968 		.n_voltages = 64,
969 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
970 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
971 		.vsel_reg = RK805_BUCK1_ON_VSEL_REG,
972 		.vsel_mask = RK818_BUCK_VSEL_MASK,
973 		.enable_reg = RK805_DCDC_EN_REG,
974 		.enable_mask = BIT(0),
975 		.owner = THIS_MODULE,
976 	}, {
977 		.name = "DCDC_REG2",
978 		.supply_name = "vcc2",
979 		.of_match = of_match_ptr("DCDC_REG2"),
980 		.regulators_node = of_match_ptr("regulators"),
981 		.id = RK805_ID_DCDC2,
982 		.ops = &rk808_reg_ops_ranges,
983 		.type = REGULATOR_VOLTAGE,
984 		.n_voltages = 64,
985 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
986 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
987 		.vsel_reg = RK805_BUCK2_ON_VSEL_REG,
988 		.vsel_mask = RK818_BUCK_VSEL_MASK,
989 		.enable_reg = RK805_DCDC_EN_REG,
990 		.enable_mask = BIT(1),
991 		.owner = THIS_MODULE,
992 	}, {
993 		.name = "DCDC_REG3",
994 		.supply_name = "vcc3",
995 		.of_match = of_match_ptr("DCDC_REG3"),
996 		.regulators_node = of_match_ptr("regulators"),
997 		.id = RK805_ID_DCDC3,
998 		.ops = &rk805_switch_ops,
999 		.type = REGULATOR_VOLTAGE,
1000 		.n_voltages = 1,
1001 		.enable_reg = RK805_DCDC_EN_REG,
1002 		.enable_mask = BIT(2),
1003 		.owner = THIS_MODULE,
1004 	},
1005 
1006 	RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
1007 		RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
1008 		RK805_DCDC_EN_REG, BIT(3), 0),
1009 
1010 	RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
1011 		RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
1012 		BIT(0), 400),
1013 	RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
1014 		RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
1015 		BIT(1), 400),
1016 	RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
1017 		RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
1018 		BIT(2), 400),
1019 };
1020 
1021 static const struct linear_range rk806_buck_voltage_ranges[] = {
1022 	REGULATOR_LINEAR_RANGE(500000, 0, 160, 6250), /* 500mV ~ 1500mV */
1023 	REGULATOR_LINEAR_RANGE(1500000, 161, 237, 25000), /* 1500mV ~ 3400mV */
1024 	REGULATOR_LINEAR_RANGE(3400000, 238, 255, 0),
1025 };
1026 
1027 static const struct linear_range rk806_ldo_voltage_ranges[] = {
1028 	REGULATOR_LINEAR_RANGE(500000, 0, 232, 12500), /* 500mV ~ 3400mV */
1029 	REGULATOR_LINEAR_RANGE(3400000, 233, 255, 0), /* 500mV ~ 3400mV */
1030 };
1031 
1032 static const struct regulator_desc rk806_reg[] = {
1033 	RK806_REGULATOR("dcdc-reg1", "vcc1", RK806_ID_DCDC1, rk806_ops_dcdc,
1034 			RK806_BUCK_SEL_CNT, RK806_BUCK1_ON_VSEL,
1035 			RK806_POWER_EN0, rk806_buck_voltage_ranges, 0,
1036 			RK806_BUCK1_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1037 	RK806_REGULATOR("dcdc-reg2", "vcc2", RK806_ID_DCDC2, rk806_ops_dcdc,
1038 			RK806_BUCK_SEL_CNT, RK806_BUCK2_ON_VSEL,
1039 			RK806_POWER_EN0, rk806_buck_voltage_ranges, 1,
1040 			RK806_BUCK2_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1041 	RK806_REGULATOR("dcdc-reg3", "vcc3", RK806_ID_DCDC3, rk806_ops_dcdc,
1042 			RK806_BUCK_SEL_CNT, RK806_BUCK3_ON_VSEL,
1043 			RK806_POWER_EN0, rk806_buck_voltage_ranges, 2,
1044 			RK806_BUCK3_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1045 	RK806_REGULATOR("dcdc-reg4", "vcc4", RK806_ID_DCDC4, rk806_ops_dcdc,
1046 			RK806_BUCK_SEL_CNT, RK806_BUCK4_ON_VSEL,
1047 			RK806_POWER_EN0, rk806_buck_voltage_ranges, 3,
1048 			RK806_BUCK4_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1049 
1050 	RK806_REGULATOR("dcdc-reg5", "vcc5", RK806_ID_DCDC5, rk806_ops_dcdc,
1051 			RK806_BUCK_SEL_CNT, RK806_BUCK5_ON_VSEL,
1052 			RK806_POWER_EN1, rk806_buck_voltage_ranges, 0,
1053 			RK806_BUCK5_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1054 	RK806_REGULATOR("dcdc-reg6", "vcc6", RK806_ID_DCDC6, rk806_ops_dcdc,
1055 			RK806_BUCK_SEL_CNT, RK806_BUCK6_ON_VSEL,
1056 			RK806_POWER_EN1, rk806_buck_voltage_ranges, 1,
1057 			RK806_BUCK6_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1058 	RK806_REGULATOR("dcdc-reg7", "vcc7", RK806_ID_DCDC7, rk806_ops_dcdc,
1059 			RK806_BUCK_SEL_CNT, RK806_BUCK7_ON_VSEL,
1060 			RK806_POWER_EN1, rk806_buck_voltage_ranges, 2,
1061 			RK806_BUCK7_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1062 	RK806_REGULATOR("dcdc-reg8", "vcc8", RK806_ID_DCDC8, rk806_ops_dcdc,
1063 			RK806_BUCK_SEL_CNT, RK806_BUCK8_ON_VSEL,
1064 			RK806_POWER_EN1, rk806_buck_voltage_ranges, 3,
1065 			RK806_BUCK8_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1066 
1067 	RK806_REGULATOR("dcdc-reg9", "vcc9", RK806_ID_DCDC9, rk806_ops_dcdc,
1068 			RK806_BUCK_SEL_CNT, RK806_BUCK9_ON_VSEL,
1069 			RK806_POWER_EN2, rk806_buck_voltage_ranges, 0,
1070 			RK806_BUCK9_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1071 	RK806_REGULATOR("dcdc-reg10", "vcc10", RK806_ID_DCDC10, rk806_ops_dcdc,
1072 			RK806_BUCK_SEL_CNT, RK806_BUCK10_ON_VSEL,
1073 			RK806_POWER_EN2, rk806_buck_voltage_ranges, 1,
1074 			RK806_BUCK10_CONFIG, 0xc0, rk806_ramp_delay_table_dcdc),
1075 
1076 	RK806_REGULATOR("nldo-reg1", "vcc13", RK806_ID_NLDO1, rk806_ops_nldo,
1077 			RK806_LDO_SEL_CNT, RK806_NLDO1_ON_VSEL,
1078 			RK806_POWER_EN3, rk806_ldo_voltage_ranges, 0,
1079 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1080 	RK806_REGULATOR("nldo-reg2", "vcc13", RK806_ID_NLDO2, rk806_ops_nldo,
1081 			RK806_LDO_SEL_CNT, RK806_NLDO2_ON_VSEL,
1082 			RK806_POWER_EN3, rk806_ldo_voltage_ranges, 1,
1083 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1084 	RK806_REGULATOR("nldo-reg3", "vcc13", RK806_ID_NLDO3, rk806_ops_nldo,
1085 			RK806_LDO_SEL_CNT, RK806_NLDO3_ON_VSEL,
1086 			RK806_POWER_EN3, rk806_ldo_voltage_ranges, 2,
1087 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1088 	RK806_REGULATOR("nldo-reg4", "vcc14", RK806_ID_NLDO4, rk806_ops_nldo,
1089 			RK806_LDO_SEL_CNT, RK806_NLDO4_ON_VSEL,
1090 			RK806_POWER_EN3, rk806_ldo_voltage_ranges, 3,
1091 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1092 
1093 	RK806_REGULATOR("nldo-reg5", "vcc14", RK806_ID_NLDO5, rk806_ops_nldo,
1094 			RK806_LDO_SEL_CNT, RK806_NLDO5_ON_VSEL,
1095 			RK806_POWER_EN5, rk806_ldo_voltage_ranges, 2,
1096 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1097 
1098 	RK806_REGULATOR("pldo-reg1", "vcc11", RK806_ID_PLDO1, rk806_ops_pldo,
1099 			RK806_LDO_SEL_CNT, RK806_PLDO1_ON_VSEL,
1100 			RK806_POWER_EN4, rk806_ldo_voltage_ranges, 1,
1101 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1102 	RK806_REGULATOR("pldo-reg2", "vcc11", RK806_ID_PLDO2, rk806_ops_pldo,
1103 			RK806_LDO_SEL_CNT, RK806_PLDO2_ON_VSEL,
1104 			RK806_POWER_EN4, rk806_ldo_voltage_ranges, 2,
1105 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1106 	RK806_REGULATOR("pldo-reg3", "vcc11", RK806_ID_PLDO3, rk806_ops_pldo,
1107 			RK806_LDO_SEL_CNT, RK806_PLDO3_ON_VSEL,
1108 			RK806_POWER_EN4, rk806_ldo_voltage_ranges, 3,
1109 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1110 
1111 	RK806_REGULATOR("pldo-reg4", "vcc12", RK806_ID_PLDO4, rk806_ops_pldo,
1112 			RK806_LDO_SEL_CNT, RK806_PLDO4_ON_VSEL,
1113 			RK806_POWER_EN5, rk806_ldo_voltage_ranges, 0,
1114 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1115 	RK806_REGULATOR("pldo-reg5", "vcc12", RK806_ID_PLDO5, rk806_ops_pldo,
1116 			RK806_LDO_SEL_CNT, RK806_PLDO5_ON_VSEL,
1117 			RK806_POWER_EN5, rk806_ldo_voltage_ranges, 1,
1118 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1119 
1120 	RK806_REGULATOR("pldo-reg6", "vcca", RK806_ID_PLDO6, rk806_ops_pldo,
1121 			RK806_LDO_SEL_CNT, RK806_PLDO6_ON_VSEL,
1122 			RK806_POWER_EN4, rk806_ldo_voltage_ranges, 0,
1123 			0xEA, 0x38, rk806_ramp_delay_table_ldo),
1124 };
1125 
1126 
1127 static const struct regulator_desc rk808_reg[] = {
1128 	{
1129 		.name = "DCDC_REG1",
1130 		.supply_name = "vcc1",
1131 		.of_match = of_match_ptr("DCDC_REG1"),
1132 		.regulators_node = of_match_ptr("regulators"),
1133 		.id = RK808_ID_DCDC1,
1134 		.ops = &rk808_buck1_2_ops,
1135 		.type = REGULATOR_VOLTAGE,
1136 		.min_uV = 712500,
1137 		.uV_step = 12500,
1138 		.n_voltages = 64,
1139 		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
1140 		.vsel_mask = RK808_BUCK_VSEL_MASK,
1141 		.enable_reg = RK808_DCDC_EN_REG,
1142 		.enable_mask = BIT(0),
1143 		.ramp_reg = RK808_BUCK1_CONFIG_REG,
1144 		.ramp_mask = RK808_RAMP_RATE_MASK,
1145 		.ramp_delay_table = rk808_buck1_2_ramp_table,
1146 		.n_ramp_values = ARRAY_SIZE(rk808_buck1_2_ramp_table),
1147 		.owner = THIS_MODULE,
1148 	}, {
1149 		.name = "DCDC_REG2",
1150 		.supply_name = "vcc2",
1151 		.of_match = of_match_ptr("DCDC_REG2"),
1152 		.regulators_node = of_match_ptr("regulators"),
1153 		.id = RK808_ID_DCDC2,
1154 		.ops = &rk808_buck1_2_ops,
1155 		.type = REGULATOR_VOLTAGE,
1156 		.min_uV = 712500,
1157 		.uV_step = 12500,
1158 		.n_voltages = 64,
1159 		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
1160 		.vsel_mask = RK808_BUCK_VSEL_MASK,
1161 		.enable_reg = RK808_DCDC_EN_REG,
1162 		.enable_mask = BIT(1),
1163 		.ramp_reg = RK808_BUCK2_CONFIG_REG,
1164 		.ramp_mask = RK808_RAMP_RATE_MASK,
1165 		.ramp_delay_table = rk808_buck1_2_ramp_table,
1166 		.n_ramp_values = ARRAY_SIZE(rk808_buck1_2_ramp_table),
1167 		.owner = THIS_MODULE,
1168 	}, {
1169 		.name = "DCDC_REG3",
1170 		.supply_name = "vcc3",
1171 		.of_match = of_match_ptr("DCDC_REG3"),
1172 		.regulators_node = of_match_ptr("regulators"),
1173 		.id = RK808_ID_DCDC3,
1174 		.ops = &rk808_switch_ops,
1175 		.type = REGULATOR_VOLTAGE,
1176 		.n_voltages = 1,
1177 		.enable_reg = RK808_DCDC_EN_REG,
1178 		.enable_mask = BIT(2),
1179 		.owner = THIS_MODULE,
1180 	},
1181 	RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
1182 		RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
1183 		RK808_DCDC_EN_REG, BIT(3), 0),
1184 	RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
1185 		RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1186 		BIT(0), 400),
1187 	RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
1188 		RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1189 		BIT(1), 400),
1190 	{
1191 		.name = "LDO_REG3",
1192 		.supply_name = "vcc7",
1193 		.of_match = of_match_ptr("LDO_REG3"),
1194 		.regulators_node = of_match_ptr("regulators"),
1195 		.id = RK808_ID_LDO3,
1196 		.ops = &rk808_reg_ops_ranges,
1197 		.type = REGULATOR_VOLTAGE,
1198 		.n_voltages = 16,
1199 		.linear_ranges = rk808_ldo3_voltage_ranges,
1200 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
1201 		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
1202 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
1203 		.enable_reg = RK808_LDO_EN_REG,
1204 		.enable_mask = BIT(2),
1205 		.enable_time = 400,
1206 		.owner = THIS_MODULE,
1207 	},
1208 	RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
1209 		RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1210 		BIT(3), 400),
1211 	RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
1212 		RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1213 		BIT(4), 400),
1214 	RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
1215 		RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1216 		BIT(5), 400),
1217 	RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
1218 		RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1219 		BIT(6), 400),
1220 	RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
1221 		RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
1222 		BIT(7), 400),
1223 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
1224 		RK808_DCDC_EN_REG, BIT(5)),
1225 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
1226 		RK808_DCDC_EN_REG, BIT(6)),
1227 };
1228 
1229 static const struct regulator_desc rk809_reg[] = {
1230 	{
1231 		.name = "DCDC_REG1",
1232 		.supply_name = "vcc1",
1233 		.of_match = of_match_ptr("DCDC_REG1"),
1234 		.regulators_node = of_match_ptr("regulators"),
1235 		.id = RK817_ID_DCDC1,
1236 		.ops = &rk817_buck_ops_range,
1237 		.type = REGULATOR_VOLTAGE,
1238 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1239 		.linear_ranges = rk817_buck1_voltage_ranges,
1240 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1241 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
1242 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1243 		.enable_reg = RK817_POWER_EN_REG(0),
1244 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
1245 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
1246 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
1247 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC1),
1248 		.ramp_mask = RK817_RAMP_RATE_MASK,
1249 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1250 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1251 		.of_map_mode = rk8xx_regulator_of_map_mode,
1252 		.owner = THIS_MODULE,
1253 	}, {
1254 		.name = "DCDC_REG2",
1255 		.supply_name = "vcc2",
1256 		.of_match = of_match_ptr("DCDC_REG2"),
1257 		.regulators_node = of_match_ptr("regulators"),
1258 		.id = RK817_ID_DCDC2,
1259 		.ops = &rk817_buck_ops_range,
1260 		.type = REGULATOR_VOLTAGE,
1261 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1262 		.linear_ranges = rk817_buck1_voltage_ranges,
1263 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1264 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
1265 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1266 		.enable_reg = RK817_POWER_EN_REG(0),
1267 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
1268 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
1269 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
1270 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC2),
1271 		.ramp_mask = RK817_RAMP_RATE_MASK,
1272 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1273 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1274 		.of_map_mode = rk8xx_regulator_of_map_mode,
1275 		.owner = THIS_MODULE,
1276 	}, {
1277 		.name = "DCDC_REG3",
1278 		.supply_name = "vcc3",
1279 		.of_match = of_match_ptr("DCDC_REG3"),
1280 		.regulators_node = of_match_ptr("regulators"),
1281 		.id = RK817_ID_DCDC3,
1282 		.ops = &rk817_buck_ops_range,
1283 		.type = REGULATOR_VOLTAGE,
1284 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1285 		.linear_ranges = rk817_buck1_voltage_ranges,
1286 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1287 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
1288 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1289 		.apply_reg = RK817_POWER_CONFIG,
1290 		.apply_bit = RK817_BUCK3_FB_RES_INTER,
1291 		.enable_reg = RK817_POWER_EN_REG(0),
1292 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
1293 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
1294 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
1295 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC3),
1296 		.ramp_mask = RK817_RAMP_RATE_MASK,
1297 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1298 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1299 		.of_map_mode = rk8xx_regulator_of_map_mode,
1300 		.owner = THIS_MODULE,
1301 	}, {
1302 		.name = "DCDC_REG4",
1303 		.supply_name = "vcc4",
1304 		.of_match = of_match_ptr("DCDC_REG4"),
1305 		.regulators_node = of_match_ptr("regulators"),
1306 		.id = RK817_ID_DCDC4,
1307 		.ops = &rk817_buck_ops_range,
1308 		.type = REGULATOR_VOLTAGE,
1309 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
1310 		.linear_ranges = rk817_buck3_voltage_ranges,
1311 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
1312 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
1313 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1314 		.enable_reg = RK817_POWER_EN_REG(0),
1315 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
1316 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
1317 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
1318 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC4),
1319 		.ramp_mask = RK817_RAMP_RATE_MASK,
1320 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1321 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1322 		.of_map_mode = rk8xx_regulator_of_map_mode,
1323 		.owner = THIS_MODULE,
1324 	},
1325 	{
1326 		.name = "DCDC_REG5",
1327 		.supply_name = "vcc9",
1328 		.of_match = of_match_ptr("DCDC_REG5"),
1329 		.regulators_node = of_match_ptr("regulators"),
1330 		.id = RK809_ID_DCDC5,
1331 		.ops = &rk809_buck5_ops_range,
1332 		.type = REGULATOR_VOLTAGE,
1333 		.n_voltages = RK809_BUCK5_SEL_CNT,
1334 		.linear_ranges = rk809_buck5_voltage_ranges,
1335 		.n_linear_ranges = ARRAY_SIZE(rk809_buck5_voltage_ranges),
1336 		.vsel_reg = RK809_BUCK5_CONFIG(0),
1337 		.vsel_mask = RK809_BUCK5_VSEL_MASK,
1338 		.enable_reg = RK817_POWER_EN_REG(3),
1339 		.enable_mask = ENABLE_MASK(1),
1340 		.enable_val = ENABLE_MASK(1),
1341 		.disable_val = DISABLE_VAL(1),
1342 		.of_map_mode = rk8xx_regulator_of_map_mode,
1343 		.owner = THIS_MODULE,
1344 	},
1345 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
1346 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
1347 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
1348 		   DISABLE_VAL(0), 400),
1349 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
1350 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
1351 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1352 		   DISABLE_VAL(1), 400),
1353 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1354 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1355 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1356 		   DISABLE_VAL(2), 400),
1357 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1358 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1359 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1360 		   DISABLE_VAL(3), 400),
1361 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1362 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1363 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1364 		   DISABLE_VAL(0), 400),
1365 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1366 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1367 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1368 		   DISABLE_VAL(1), 400),
1369 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1370 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1371 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1372 		   DISABLE_VAL(2), 400),
1373 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1374 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1375 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1376 		   DISABLE_VAL(3), 400),
1377 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1378 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1379 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1380 		   DISABLE_VAL(0), 400),
1381 	RK817_DESC_SWITCH(RK809_ID_SW1, "SWITCH_REG1", "vcc9",
1382 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1383 			  DISABLE_VAL(2)),
1384 	RK817_DESC_SWITCH(RK809_ID_SW2, "SWITCH_REG2", "vcc8",
1385 			  RK817_POWER_EN_REG(3), ENABLE_MASK(3),
1386 			  DISABLE_VAL(3)),
1387 };
1388 
1389 static const struct regulator_desc rk817_reg[] = {
1390 	{
1391 		.name = "DCDC_REG1",
1392 		.supply_name = "vcc1",
1393 		.of_match = of_match_ptr("DCDC_REG1"),
1394 		.regulators_node = of_match_ptr("regulators"),
1395 		.id = RK817_ID_DCDC1,
1396 		.ops = &rk817_buck_ops_range,
1397 		.type = REGULATOR_VOLTAGE,
1398 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1399 		.linear_ranges = rk817_buck1_voltage_ranges,
1400 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1401 		.vsel_reg = RK817_BUCK1_ON_VSEL_REG,
1402 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1403 		.enable_reg = RK817_POWER_EN_REG(0),
1404 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC1),
1405 		.enable_val = ENABLE_MASK(RK817_ID_DCDC1),
1406 		.disable_val = DISABLE_VAL(RK817_ID_DCDC1),
1407 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC1),
1408 		.ramp_mask = RK817_RAMP_RATE_MASK,
1409 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1410 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1411 		.of_map_mode = rk8xx_regulator_of_map_mode,
1412 		.owner = THIS_MODULE,
1413 	}, {
1414 		.name = "DCDC_REG2",
1415 		.supply_name = "vcc2",
1416 		.of_match = of_match_ptr("DCDC_REG2"),
1417 		.regulators_node = of_match_ptr("regulators"),
1418 		.id = RK817_ID_DCDC2,
1419 		.ops = &rk817_buck_ops_range,
1420 		.type = REGULATOR_VOLTAGE,
1421 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1422 		.linear_ranges = rk817_buck1_voltage_ranges,
1423 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1424 		.vsel_reg = RK817_BUCK2_ON_VSEL_REG,
1425 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1426 		.enable_reg = RK817_POWER_EN_REG(0),
1427 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC2),
1428 		.enable_val = ENABLE_MASK(RK817_ID_DCDC2),
1429 		.disable_val = DISABLE_VAL(RK817_ID_DCDC2),
1430 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC2),
1431 		.ramp_mask = RK817_RAMP_RATE_MASK,
1432 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1433 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1434 		.of_map_mode = rk8xx_regulator_of_map_mode,
1435 		.owner = THIS_MODULE,
1436 	}, {
1437 		.name = "DCDC_REG3",
1438 		.supply_name = "vcc3",
1439 		.of_match = of_match_ptr("DCDC_REG3"),
1440 		.regulators_node = of_match_ptr("regulators"),
1441 		.id = RK817_ID_DCDC3,
1442 		.ops = &rk817_buck_ops_range,
1443 		.type = REGULATOR_VOLTAGE,
1444 		.n_voltages = RK817_BUCK1_SEL_CNT + 1,
1445 		.linear_ranges = rk817_buck1_voltage_ranges,
1446 		.n_linear_ranges = ARRAY_SIZE(rk817_buck1_voltage_ranges),
1447 		.vsel_reg = RK817_BUCK3_ON_VSEL_REG,
1448 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1449 		.enable_reg = RK817_POWER_EN_REG(0),
1450 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC3),
1451 		.enable_val = ENABLE_MASK(RK817_ID_DCDC3),
1452 		.disable_val = DISABLE_VAL(RK817_ID_DCDC3),
1453 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC3),
1454 		.ramp_mask = RK817_RAMP_RATE_MASK,
1455 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1456 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1457 		.of_map_mode = rk8xx_regulator_of_map_mode,
1458 		.owner = THIS_MODULE,
1459 	}, {
1460 		.name = "DCDC_REG4",
1461 		.supply_name = "vcc4",
1462 		.of_match = of_match_ptr("DCDC_REG4"),
1463 		.regulators_node = of_match_ptr("regulators"),
1464 		.id = RK817_ID_DCDC4,
1465 		.ops = &rk817_buck_ops_range,
1466 		.type = REGULATOR_VOLTAGE,
1467 		.n_voltages = RK817_BUCK3_SEL_CNT + 1,
1468 		.linear_ranges = rk817_buck3_voltage_ranges,
1469 		.n_linear_ranges = ARRAY_SIZE(rk817_buck3_voltage_ranges),
1470 		.vsel_reg = RK817_BUCK4_ON_VSEL_REG,
1471 		.vsel_mask = RK817_BUCK_VSEL_MASK,
1472 		.enable_reg = RK817_POWER_EN_REG(0),
1473 		.enable_mask = ENABLE_MASK(RK817_ID_DCDC4),
1474 		.enable_val = ENABLE_MASK(RK817_ID_DCDC4),
1475 		.disable_val = DISABLE_VAL(RK817_ID_DCDC4),
1476 		.ramp_reg = RK817_BUCK_CONFIG_REG(RK817_ID_DCDC4),
1477 		.ramp_mask = RK817_RAMP_RATE_MASK,
1478 		.ramp_delay_table = rk817_buck1_4_ramp_table,
1479 		.n_ramp_values = ARRAY_SIZE(rk817_buck1_4_ramp_table),
1480 		.of_map_mode = rk8xx_regulator_of_map_mode,
1481 		.owner = THIS_MODULE,
1482 	},
1483 	RK817_DESC(RK817_ID_LDO1, "LDO_REG1", "vcc5", 600, 3400, 25,
1484 		   RK817_LDO_ON_VSEL_REG(0), RK817_LDO_VSEL_MASK,
1485 		   RK817_POWER_EN_REG(1), ENABLE_MASK(0),
1486 		   DISABLE_VAL(0), 400),
1487 	RK817_DESC(RK817_ID_LDO2, "LDO_REG2", "vcc5", 600, 3400, 25,
1488 		   RK817_LDO_ON_VSEL_REG(1), RK817_LDO_VSEL_MASK,
1489 		   RK817_POWER_EN_REG(1), ENABLE_MASK(1),
1490 		   DISABLE_VAL(1), 400),
1491 	RK817_DESC(RK817_ID_LDO3, "LDO_REG3", "vcc5", 600, 3400, 25,
1492 		   RK817_LDO_ON_VSEL_REG(2), RK817_LDO_VSEL_MASK,
1493 		   RK817_POWER_EN_REG(1), ENABLE_MASK(2),
1494 		   DISABLE_VAL(2), 400),
1495 	RK817_DESC(RK817_ID_LDO4, "LDO_REG4", "vcc6", 600, 3400, 25,
1496 		   RK817_LDO_ON_VSEL_REG(3), RK817_LDO_VSEL_MASK,
1497 		   RK817_POWER_EN_REG(1), ENABLE_MASK(3),
1498 		   DISABLE_VAL(3), 400),
1499 	RK817_DESC(RK817_ID_LDO5, "LDO_REG5", "vcc6", 600, 3400, 25,
1500 		   RK817_LDO_ON_VSEL_REG(4), RK817_LDO_VSEL_MASK,
1501 		   RK817_POWER_EN_REG(2), ENABLE_MASK(0),
1502 		   DISABLE_VAL(0), 400),
1503 	RK817_DESC(RK817_ID_LDO6, "LDO_REG6", "vcc6", 600, 3400, 25,
1504 		   RK817_LDO_ON_VSEL_REG(5), RK817_LDO_VSEL_MASK,
1505 		   RK817_POWER_EN_REG(2), ENABLE_MASK(1),
1506 		   DISABLE_VAL(1), 400),
1507 	RK817_DESC(RK817_ID_LDO7, "LDO_REG7", "vcc7", 600, 3400, 25,
1508 		   RK817_LDO_ON_VSEL_REG(6), RK817_LDO_VSEL_MASK,
1509 		   RK817_POWER_EN_REG(2), ENABLE_MASK(2),
1510 		   DISABLE_VAL(2), 400),
1511 	RK817_DESC(RK817_ID_LDO8, "LDO_REG8", "vcc7", 600, 3400, 25,
1512 		   RK817_LDO_ON_VSEL_REG(7), RK817_LDO_VSEL_MASK,
1513 		   RK817_POWER_EN_REG(2), ENABLE_MASK(3),
1514 		   DISABLE_VAL(3), 400),
1515 	RK817_DESC(RK817_ID_LDO9, "LDO_REG9", "vcc7", 600, 3400, 25,
1516 		   RK817_LDO_ON_VSEL_REG(8), RK817_LDO_VSEL_MASK,
1517 		   RK817_POWER_EN_REG(3), ENABLE_MASK(0),
1518 		   DISABLE_VAL(0), 400),
1519 	RK817_BOOST_DESC(RK817_ID_BOOST, "BOOST", "vcc8", 4700, 5400, 100,
1520 			 RK817_BOOST_OTG_CFG, RK817_BOOST_VSEL_MASK,
1521 			 RK817_POWER_EN_REG(3), ENABLE_MASK(1), ENABLE_MASK(1),
1522 		   DISABLE_VAL(1), 400, 3500 - 5400),
1523 	RK817_DESC_SWITCH(RK817_ID_BOOST_OTG_SW, "OTG_SWITCH", "vcc9",
1524 			  RK817_POWER_EN_REG(3), ENABLE_MASK(2),
1525 			  DISABLE_VAL(2)),
1526 };
1527 
1528 static const struct regulator_desc rk818_reg[] = {
1529 	{
1530 		.name = "DCDC_REG1",
1531 		.supply_name = "vcc1",
1532 		.of_match = of_match_ptr("DCDC_REG1"),
1533 		.regulators_node = of_match_ptr("regulators"),
1534 		.id = RK818_ID_DCDC1,
1535 		.ops = &rk808_reg_ops,
1536 		.type = REGULATOR_VOLTAGE,
1537 		.min_uV = 712500,
1538 		.uV_step = 12500,
1539 		.n_voltages = 64,
1540 		.vsel_reg = RK818_BUCK1_ON_VSEL_REG,
1541 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1542 		.enable_reg = RK818_DCDC_EN_REG,
1543 		.enable_mask = BIT(0),
1544 		.owner = THIS_MODULE,
1545 	}, {
1546 		.name = "DCDC_REG2",
1547 		.supply_name = "vcc2",
1548 		.of_match = of_match_ptr("DCDC_REG2"),
1549 		.regulators_node = of_match_ptr("regulators"),
1550 		.id = RK818_ID_DCDC2,
1551 		.ops = &rk808_reg_ops,
1552 		.type = REGULATOR_VOLTAGE,
1553 		.min_uV = 712500,
1554 		.uV_step = 12500,
1555 		.n_voltages = 64,
1556 		.vsel_reg = RK818_BUCK2_ON_VSEL_REG,
1557 		.vsel_mask = RK818_BUCK_VSEL_MASK,
1558 		.enable_reg = RK818_DCDC_EN_REG,
1559 		.enable_mask = BIT(1),
1560 		.owner = THIS_MODULE,
1561 	}, {
1562 		.name = "DCDC_REG3",
1563 		.supply_name = "vcc3",
1564 		.of_match = of_match_ptr("DCDC_REG3"),
1565 		.regulators_node = of_match_ptr("regulators"),
1566 		.id = RK818_ID_DCDC3,
1567 		.ops = &rk808_switch_ops,
1568 		.type = REGULATOR_VOLTAGE,
1569 		.n_voltages = 1,
1570 		.enable_reg = RK818_DCDC_EN_REG,
1571 		.enable_mask = BIT(2),
1572 		.owner = THIS_MODULE,
1573 	},
1574 	RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
1575 		RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
1576 		RK818_DCDC_EN_REG, BIT(3), 0),
1577 	RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
1578 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
1579 		RK818_DCDC_EN_REG, BIT(4), 0),
1580 	RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
1581 		RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1582 		BIT(0), 400),
1583 	RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
1584 		RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1585 		BIT(1), 400),
1586 	{
1587 		.name = "LDO_REG3",
1588 		.supply_name = "vcc7",
1589 		.of_match = of_match_ptr("LDO_REG3"),
1590 		.regulators_node = of_match_ptr("regulators"),
1591 		.id = RK818_ID_LDO3,
1592 		.ops = &rk808_reg_ops_ranges,
1593 		.type = REGULATOR_VOLTAGE,
1594 		.n_voltages = 16,
1595 		.linear_ranges = rk808_ldo3_voltage_ranges,
1596 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
1597 		.vsel_reg = RK818_LDO3_ON_VSEL_REG,
1598 		.vsel_mask = RK818_LDO3_ON_VSEL_MASK,
1599 		.enable_reg = RK818_LDO_EN_REG,
1600 		.enable_mask = BIT(2),
1601 		.enable_time = 400,
1602 		.owner = THIS_MODULE,
1603 	},
1604 	RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
1605 		RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1606 		BIT(3), 400),
1607 	RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
1608 		RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1609 		BIT(4), 400),
1610 	RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
1611 		RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1612 		BIT(5), 400),
1613 	RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
1614 		RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1615 		BIT(6), 400),
1616 	RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
1617 		RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
1618 		BIT(7), 400),
1619 	RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
1620 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
1621 		RK818_DCDC_EN_REG, BIT(5), 400),
1622 	RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
1623 		RK818_DCDC_EN_REG, BIT(6)),
1624 	RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
1625 		RK818_H5V_EN_REG, BIT(0)),
1626 	RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
1627 		RK818_DCDC_EN_REG, BIT(7)),
1628 };
1629 
1630 static int rk808_regulator_dt_parse_pdata(struct device *dev,
1631 				   struct regmap *map,
1632 				   struct rk808_regulator_data *pdata)
1633 {
1634 	struct device_node *np;
1635 	int tmp, ret = 0, i;
1636 
1637 	np = of_get_child_by_name(dev->of_node, "regulators");
1638 	if (!np)
1639 		return -ENXIO;
1640 
1641 	for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
1642 		pdata->dvs_gpio[i] =
1643 			devm_gpiod_get_index_optional(dev, "dvs", i,
1644 						      GPIOD_OUT_LOW);
1645 		if (IS_ERR(pdata->dvs_gpio[i])) {
1646 			ret = PTR_ERR(pdata->dvs_gpio[i]);
1647 			dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
1648 			goto dt_parse_end;
1649 		}
1650 
1651 		if (!pdata->dvs_gpio[i]) {
1652 			dev_dbg(dev, "there is no dvs%d gpio\n", i);
1653 			continue;
1654 		}
1655 
1656 		tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
1657 		ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
1658 				gpiod_is_active_low(pdata->dvs_gpio[i]) ?
1659 				0 : tmp);
1660 	}
1661 
1662 dt_parse_end:
1663 	of_node_put(np);
1664 	return ret;
1665 }
1666 
1667 static int rk808_regulator_probe(struct platform_device *pdev)
1668 {
1669 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
1670 	struct regulator_config config = {};
1671 	struct regulator_dev *rk808_rdev;
1672 	struct rk808_regulator_data *pdata;
1673 	const struct regulator_desc *regulators;
1674 	struct regmap *regmap;
1675 	int ret, i, nregulators;
1676 
1677 	pdev->dev.of_node = pdev->dev.parent->of_node;
1678 	pdev->dev.of_node_reused = true;
1679 
1680 	regmap = dev_get_regmap(pdev->dev.parent, NULL);
1681 	if (!regmap)
1682 		return -ENODEV;
1683 
1684 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1685 	if (!pdata)
1686 		return -ENOMEM;
1687 
1688 	switch (rk808->variant) {
1689 	case RK805_ID:
1690 		regulators = rk805_reg;
1691 		nregulators = RK805_NUM_REGULATORS;
1692 		break;
1693 	case RK806_ID:
1694 		regulators = rk806_reg;
1695 		nregulators = ARRAY_SIZE(rk806_reg);
1696 		break;
1697 	case RK808_ID:
1698 		/* DVS0/1 GPIOs are supported on the RK808 only */
1699 		ret = rk808_regulator_dt_parse_pdata(&pdev->dev, regmap, pdata);
1700 		if (ret < 0)
1701 			return ret;
1702 
1703 		regulators = rk808_reg;
1704 		nregulators = RK808_NUM_REGULATORS;
1705 		break;
1706 	case RK809_ID:
1707 		regulators = rk809_reg;
1708 		nregulators = RK809_NUM_REGULATORS;
1709 		break;
1710 	case RK817_ID:
1711 		regulators = rk817_reg;
1712 		nregulators = RK817_NUM_REGULATORS;
1713 		break;
1714 	case RK818_ID:
1715 		regulators = rk818_reg;
1716 		nregulators = RK818_NUM_REGULATORS;
1717 		break;
1718 	default:
1719 		dev_err(&pdev->dev, "unsupported RK8XX ID %lu\n",
1720 			rk808->variant);
1721 		return -EINVAL;
1722 	}
1723 
1724 	platform_set_drvdata(pdev, pdata);
1725 
1726 	config.dev = &pdev->dev;
1727 	config.driver_data = pdata;
1728 	config.regmap = regmap;
1729 
1730 	/* Instantiate the regulators */
1731 	for (i = 0; i < nregulators; i++) {
1732 		rk808_rdev = devm_regulator_register(&pdev->dev,
1733 						     &regulators[i], &config);
1734 		if (IS_ERR(rk808_rdev))
1735 			return dev_err_probe(&pdev->dev, PTR_ERR(rk808_rdev),
1736 					     "failed to register %d regulator\n", i);
1737 	}
1738 
1739 	return 0;
1740 }
1741 
1742 static struct platform_driver rk808_regulator_driver = {
1743 	.probe = rk808_regulator_probe,
1744 	.driver = {
1745 		.name = "rk808-regulator",
1746 		.probe_type = PROBE_FORCE_SYNCHRONOUS,
1747 	},
1748 };
1749 
1750 module_platform_driver(rk808_regulator_driver);
1751 
1752 MODULE_DESCRIPTION("regulator driver for the RK805/RK808/RK818 series PMICs");
1753 MODULE_AUTHOR("Tony xie <tony.xie@rock-chips.com>");
1754 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1755 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
1756 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
1757 MODULE_AUTHOR("Xu Shengfei <xsf@rock-chips.com>");
1758 MODULE_LICENSE("GPL");
1759 MODULE_ALIAS("platform:rk808-regulator");
1760