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