xref: /openbmc/linux/drivers/regulator/rk808-regulator.c (revision 2f0f2441b4a10948e2ec042b48fef13680387f7c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Regulator driver for Rockchip RK805/RK808/RK818
4  *
5  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6  *
7  * Author: Chris Zhong <zyw@rock-chips.com>
8  * Author: Zhang Qing <zhangqing@rock-chips.com>
9  *
10  * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11  *
12  * Author: Wadim Egorov <w.egorov@phytec.de>
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/mfd/rk808.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/gpio/consumer.h>
25 
26 /* Field Definitions */
27 #define RK808_BUCK_VSEL_MASK	0x3f
28 #define RK808_BUCK4_VSEL_MASK	0xf
29 #define RK808_LDO_VSEL_MASK	0x1f
30 
31 #define RK818_BUCK_VSEL_MASK		0x3f
32 #define RK818_BUCK4_VSEL_MASK		0x1f
33 #define RK818_LDO_VSEL_MASK		0x1f
34 #define RK818_LDO3_ON_VSEL_MASK		0xf
35 #define RK818_BOOST_ON_VSEL_MASK	0xe0
36 
37 /* Ramp rate definitions for buck1 / buck2 only */
38 #define RK808_RAMP_RATE_OFFSET		3
39 #define RK808_RAMP_RATE_MASK		(3 << RK808_RAMP_RATE_OFFSET)
40 #define RK808_RAMP_RATE_2MV_PER_US	(0 << RK808_RAMP_RATE_OFFSET)
41 #define RK808_RAMP_RATE_4MV_PER_US	(1 << RK808_RAMP_RATE_OFFSET)
42 #define RK808_RAMP_RATE_6MV_PER_US	(2 << RK808_RAMP_RATE_OFFSET)
43 #define RK808_RAMP_RATE_10MV_PER_US	(3 << RK808_RAMP_RATE_OFFSET)
44 
45 #define RK808_DVS2_POL		BIT(2)
46 #define RK808_DVS1_POL		BIT(1)
47 
48 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
49 #define RK808_SLP_REG_OFFSET 1
50 
51 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
52 #define RK808_DVS_REG_OFFSET 2
53 
54 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
55 #define RK808_SLP_SET_OFF_REG_OFFSET 2
56 
57 /* max steps for increase voltage of Buck1/2, equal 100mv*/
58 #define MAX_STEPS_ONE_TIME 8
59 
60 #define RK805_DESC(_id, _match, _supply, _min, _max, _step, _vreg,      \
61 	_vmask, _ereg, _emask, _etime)                                  \
62 	[_id] = {                                                       \
63 		.name           = (_match),                             \
64 		.supply_name    = (_supply),                            \
65 		.of_match       = of_match_ptr(_match),                 \
66 		.regulators_node = of_match_ptr("regulators"),          \
67 		.type           = REGULATOR_VOLTAGE,                    \
68 		.id             = (_id),                                \
69 		.n_voltages     = (((_max) - (_min)) / (_step) + 1),    \
70 		.owner          = THIS_MODULE,                          \
71 		.min_uV         = (_min) * 1000,                        \
72 		.uV_step        = (_step) * 1000,                       \
73 		.vsel_reg       = (_vreg),                              \
74 		.vsel_mask      = (_vmask),                             \
75 		.enable_reg     = (_ereg),                              \
76 		.enable_mask    = (_emask),                             \
77 		.enable_time    = (_etime),                             \
78 		.ops            = &rk805_reg_ops,                       \
79 	}
80 
81 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
82 	_vmask, _ereg, _emask, _etime)					\
83 	[_id] = {							\
84 		.name		= (_match),				\
85 		.supply_name	= (_supply),				\
86 		.of_match	= of_match_ptr(_match),			\
87 		.regulators_node = of_match_ptr("regulators"),		\
88 		.type		= REGULATOR_VOLTAGE,			\
89 		.id		= (_id),				\
90 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
91 		.owner		= THIS_MODULE,				\
92 		.min_uV		= (_min) * 1000,			\
93 		.uV_step	= (_step) * 1000,			\
94 		.vsel_reg	= (_vreg),				\
95 		.vsel_mask	= (_vmask),				\
96 		.enable_reg	= (_ereg),				\
97 		.enable_mask	= (_emask),				\
98 		.enable_time	= (_etime),				\
99 		.ops		= &rk808_reg_ops,			\
100 	}
101 
102 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask)		\
103 	[_id] = {							\
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 		.enable_reg	= (_ereg),				\
111 		.enable_mask	= (_emask),				\
112 		.owner		= THIS_MODULE,				\
113 		.ops		= &rk808_switch_ops			\
114 	}
115 
116 
117 struct rk808_regulator_data {
118 	struct gpio_desc *dvs_gpio[2];
119 };
120 
121 static const int rk808_buck_config_regs[] = {
122 	RK808_BUCK1_CONFIG_REG,
123 	RK808_BUCK2_CONFIG_REG,
124 	RK808_BUCK3_CONFIG_REG,
125 	RK808_BUCK4_CONFIG_REG,
126 };
127 
128 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
129 	REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
130 	REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
131 };
132 
133 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
134 {
135 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
136 	int id = rdev_get_id(rdev);
137 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
138 	unsigned int val;
139 	int ret;
140 
141 	if (!gpio || gpiod_get_value(gpio) == 0)
142 		return regulator_get_voltage_sel_regmap(rdev);
143 
144 	ret = regmap_read(rdev->regmap,
145 			  rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
146 			  &val);
147 	if (ret != 0)
148 		return ret;
149 
150 	val &= rdev->desc->vsel_mask;
151 	val >>= ffs(rdev->desc->vsel_mask) - 1;
152 
153 	return val;
154 }
155 
156 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
157 					     unsigned sel)
158 {
159 	int ret, delta_sel;
160 	unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
161 
162 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
163 	if (ret != 0)
164 		return ret;
165 
166 	tmp = val & ~mask;
167 	old_sel = val & mask;
168 	old_sel >>= ffs(mask) - 1;
169 	delta_sel = sel - old_sel;
170 
171 	/*
172 	 * If directly modify the register to change the voltage, we will face
173 	 * the risk of overshoot. Put it into a multi-step, can effectively
174 	 * avoid this problem, a step is 100mv here.
175 	 */
176 	while (delta_sel > MAX_STEPS_ONE_TIME) {
177 		old_sel += MAX_STEPS_ONE_TIME;
178 		val = old_sel << (ffs(mask) - 1);
179 		val |= tmp;
180 
181 		/*
182 		 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
183 		 * 3 bytes (24 bits) plus start and stop so 26 bits.  So we've
184 		 * got more than 65 us between each voltage change and thus
185 		 * won't ramp faster than ~1500 uV / us.
186 		 */
187 		ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
188 		delta_sel = sel - old_sel;
189 	}
190 
191 	sel <<= ffs(mask) - 1;
192 	val = tmp | sel;
193 	ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
194 
195 	/*
196 	 * When we change the voltage register directly, the ramp rate is about
197 	 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
198 	 * so we needn't wait extra time after that.
199 	 */
200 	udelay(1);
201 
202 	return ret;
203 }
204 
205 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
206 					 unsigned sel)
207 {
208 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
209 	int id = rdev_get_id(rdev);
210 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
211 	unsigned int reg = rdev->desc->vsel_reg;
212 	unsigned old_sel;
213 	int ret, gpio_level;
214 
215 	if (!gpio)
216 		return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
217 
218 	gpio_level = gpiod_get_value(gpio);
219 	if (gpio_level == 0) {
220 		reg += RK808_DVS_REG_OFFSET;
221 		ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
222 	} else {
223 		ret = regmap_read(rdev->regmap,
224 				  reg + RK808_DVS_REG_OFFSET,
225 				  &old_sel);
226 	}
227 
228 	if (ret != 0)
229 		return ret;
230 
231 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
232 	sel |= old_sel & ~rdev->desc->vsel_mask;
233 
234 	ret = regmap_write(rdev->regmap, reg, sel);
235 	if (ret)
236 		return ret;
237 
238 	gpiod_set_value(gpio, !gpio_level);
239 
240 	return ret;
241 }
242 
243 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
244 				       unsigned int old_selector,
245 				       unsigned int new_selector)
246 {
247 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
248 	int id = rdev_get_id(rdev);
249 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
250 
251 	/* if there is no dvs1/2 pin, we don't need wait extra time here. */
252 	if (!gpio)
253 		return 0;
254 
255 	return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
256 }
257 
258 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
259 {
260 	unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
261 	unsigned int reg = rk808_buck_config_regs[rdev_get_id(rdev)];
262 
263 	switch (ramp_delay) {
264 	case 1 ... 2000:
265 		ramp_value = RK808_RAMP_RATE_2MV_PER_US;
266 		break;
267 	case 2001 ... 4000:
268 		ramp_value = RK808_RAMP_RATE_4MV_PER_US;
269 		break;
270 	case 4001 ... 6000:
271 		ramp_value = RK808_RAMP_RATE_6MV_PER_US;
272 		break;
273 	case 6001 ... 10000:
274 		break;
275 	default:
276 		pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
277 			rdev->desc->name, ramp_delay);
278 	}
279 
280 	return regmap_update_bits(rdev->regmap, reg,
281 				  RK808_RAMP_RATE_MASK, ramp_value);
282 }
283 
284 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
285 {
286 	unsigned int reg;
287 	int sel = regulator_map_voltage_linear(rdev, uv, uv);
288 
289 	if (sel < 0)
290 		return -EINVAL;
291 
292 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
293 
294 	return regmap_update_bits(rdev->regmap, reg,
295 				  rdev->desc->vsel_mask,
296 				  sel);
297 }
298 
299 static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
300 {
301 	unsigned int reg;
302 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
303 
304 	if (sel < 0)
305 		return -EINVAL;
306 
307 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
308 
309 	return regmap_update_bits(rdev->regmap, reg,
310 				  rdev->desc->vsel_mask,
311 				  sel);
312 }
313 
314 static int rk805_set_suspend_enable(struct regulator_dev *rdev)
315 {
316 	unsigned int reg;
317 
318 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
319 
320 	return regmap_update_bits(rdev->regmap, reg,
321 				  rdev->desc->enable_mask,
322 				  rdev->desc->enable_mask);
323 }
324 
325 static int rk805_set_suspend_disable(struct regulator_dev *rdev)
326 {
327 	unsigned int reg;
328 
329 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
330 
331 	return regmap_update_bits(rdev->regmap, reg,
332 				  rdev->desc->enable_mask,
333 				  0);
334 }
335 
336 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
337 {
338 	unsigned int reg;
339 
340 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
341 
342 	return regmap_update_bits(rdev->regmap, reg,
343 				  rdev->desc->enable_mask,
344 				  0);
345 }
346 
347 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
348 {
349 	unsigned int reg;
350 
351 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
352 
353 	return regmap_update_bits(rdev->regmap, reg,
354 				  rdev->desc->enable_mask,
355 				  rdev->desc->enable_mask);
356 }
357 
358 static const struct regulator_ops rk805_reg_ops = {
359 	.list_voltage           = regulator_list_voltage_linear,
360 	.map_voltage            = regulator_map_voltage_linear,
361 	.get_voltage_sel        = regulator_get_voltage_sel_regmap,
362 	.set_voltage_sel        = regulator_set_voltage_sel_regmap,
363 	.enable                 = regulator_enable_regmap,
364 	.disable                = regulator_disable_regmap,
365 	.is_enabled             = regulator_is_enabled_regmap,
366 	.set_suspend_voltage    = rk808_set_suspend_voltage,
367 	.set_suspend_enable     = rk805_set_suspend_enable,
368 	.set_suspend_disable    = rk805_set_suspend_disable,
369 };
370 
371 static const struct regulator_ops rk805_switch_ops = {
372 	.enable                 = regulator_enable_regmap,
373 	.disable                = regulator_disable_regmap,
374 	.is_enabled             = regulator_is_enabled_regmap,
375 	.set_suspend_enable     = rk805_set_suspend_enable,
376 	.set_suspend_disable    = rk805_set_suspend_disable,
377 };
378 
379 static const struct regulator_ops rk808_buck1_2_ops = {
380 	.list_voltage		= regulator_list_voltage_linear,
381 	.map_voltage		= regulator_map_voltage_linear,
382 	.get_voltage_sel	= rk808_buck1_2_get_voltage_sel_regmap,
383 	.set_voltage_sel	= rk808_buck1_2_set_voltage_sel,
384 	.set_voltage_time_sel	= rk808_buck1_2_set_voltage_time_sel,
385 	.enable			= regulator_enable_regmap,
386 	.disable		= regulator_disable_regmap,
387 	.is_enabled		= regulator_is_enabled_regmap,
388 	.set_ramp_delay		= rk808_set_ramp_delay,
389 	.set_suspend_voltage	= rk808_set_suspend_voltage,
390 	.set_suspend_enable	= rk808_set_suspend_enable,
391 	.set_suspend_disable	= rk808_set_suspend_disable,
392 };
393 
394 static const struct regulator_ops rk808_reg_ops = {
395 	.list_voltage		= regulator_list_voltage_linear,
396 	.map_voltage		= regulator_map_voltage_linear,
397 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
398 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
399 	.enable			= regulator_enable_regmap,
400 	.disable		= regulator_disable_regmap,
401 	.is_enabled		= regulator_is_enabled_regmap,
402 	.set_suspend_voltage	= rk808_set_suspend_voltage,
403 	.set_suspend_enable	= rk808_set_suspend_enable,
404 	.set_suspend_disable	= rk808_set_suspend_disable,
405 };
406 
407 static const struct regulator_ops rk808_reg_ops_ranges = {
408 	.list_voltage		= regulator_list_voltage_linear_range,
409 	.map_voltage		= regulator_map_voltage_linear_range,
410 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
411 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
412 	.enable			= regulator_enable_regmap,
413 	.disable		= regulator_disable_regmap,
414 	.is_enabled		= regulator_is_enabled_regmap,
415 	.set_suspend_voltage	= rk808_set_suspend_voltage_range,
416 	.set_suspend_enable	= rk808_set_suspend_enable,
417 	.set_suspend_disable	= rk808_set_suspend_disable,
418 };
419 
420 static const struct regulator_ops rk808_switch_ops = {
421 	.enable			= regulator_enable_regmap,
422 	.disable		= regulator_disable_regmap,
423 	.is_enabled		= regulator_is_enabled_regmap,
424 	.set_suspend_enable	= rk808_set_suspend_enable,
425 	.set_suspend_disable	= rk808_set_suspend_disable,
426 };
427 
428 static const struct regulator_linear_range rk805_buck_1_2_voltage_ranges[] = {
429 	REGULATOR_LINEAR_RANGE(712500, 0, 59, 12500),
430 	REGULATOR_LINEAR_RANGE(1800000, 60, 62, 200000),
431 	REGULATOR_LINEAR_RANGE(2300000, 63, 63, 0),
432 };
433 
434 static const struct regulator_desc rk805_reg[] = {
435 	{
436 		.name = "DCDC_REG1",
437 		.supply_name = "vcc1",
438 		.of_match = of_match_ptr("DCDC_REG1"),
439 		.regulators_node = of_match_ptr("regulators"),
440 		.id = RK805_ID_DCDC1,
441 		.ops = &rk808_reg_ops_ranges,
442 		.type = REGULATOR_VOLTAGE,
443 		.n_voltages = 64,
444 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
445 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
446 		.vsel_reg = RK805_BUCK1_ON_VSEL_REG,
447 		.vsel_mask = RK818_BUCK_VSEL_MASK,
448 		.enable_reg = RK805_DCDC_EN_REG,
449 		.enable_mask = BIT(0),
450 		.owner = THIS_MODULE,
451 	}, {
452 		.name = "DCDC_REG2",
453 		.supply_name = "vcc2",
454 		.of_match = of_match_ptr("DCDC_REG2"),
455 		.regulators_node = of_match_ptr("regulators"),
456 		.id = RK805_ID_DCDC2,
457 		.ops = &rk808_reg_ops_ranges,
458 		.type = REGULATOR_VOLTAGE,
459 		.n_voltages = 64,
460 		.linear_ranges = rk805_buck_1_2_voltage_ranges,
461 		.n_linear_ranges = ARRAY_SIZE(rk805_buck_1_2_voltage_ranges),
462 		.vsel_reg = RK805_BUCK2_ON_VSEL_REG,
463 		.vsel_mask = RK818_BUCK_VSEL_MASK,
464 		.enable_reg = RK805_DCDC_EN_REG,
465 		.enable_mask = BIT(1),
466 		.owner = THIS_MODULE,
467 	}, {
468 		.name = "DCDC_REG3",
469 		.supply_name = "vcc3",
470 		.of_match = of_match_ptr("DCDC_REG3"),
471 		.regulators_node = of_match_ptr("regulators"),
472 		.id = RK805_ID_DCDC3,
473 		.ops = &rk805_switch_ops,
474 		.type = REGULATOR_VOLTAGE,
475 		.n_voltages = 1,
476 		.enable_reg = RK805_DCDC_EN_REG,
477 		.enable_mask = BIT(2),
478 		.owner = THIS_MODULE,
479 	},
480 
481 	RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
482 		RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
483 		RK805_DCDC_EN_REG, BIT(3), 0),
484 
485 	RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
486 		RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
487 		BIT(0), 400),
488 	RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
489 		RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
490 		BIT(1), 400),
491 	RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
492 		RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
493 		BIT(2), 400),
494 };
495 
496 static const struct regulator_desc rk808_reg[] = {
497 	{
498 		.name = "DCDC_REG1",
499 		.supply_name = "vcc1",
500 		.of_match = of_match_ptr("DCDC_REG1"),
501 		.regulators_node = of_match_ptr("regulators"),
502 		.id = RK808_ID_DCDC1,
503 		.ops = &rk808_buck1_2_ops,
504 		.type = REGULATOR_VOLTAGE,
505 		.min_uV = 712500,
506 		.uV_step = 12500,
507 		.n_voltages = 64,
508 		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
509 		.vsel_mask = RK808_BUCK_VSEL_MASK,
510 		.enable_reg = RK808_DCDC_EN_REG,
511 		.enable_mask = BIT(0),
512 		.owner = THIS_MODULE,
513 	}, {
514 		.name = "DCDC_REG2",
515 		.supply_name = "vcc2",
516 		.of_match = of_match_ptr("DCDC_REG2"),
517 		.regulators_node = of_match_ptr("regulators"),
518 		.id = RK808_ID_DCDC2,
519 		.ops = &rk808_buck1_2_ops,
520 		.type = REGULATOR_VOLTAGE,
521 		.min_uV = 712500,
522 		.uV_step = 12500,
523 		.n_voltages = 64,
524 		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
525 		.vsel_mask = RK808_BUCK_VSEL_MASK,
526 		.enable_reg = RK808_DCDC_EN_REG,
527 		.enable_mask = BIT(1),
528 		.owner = THIS_MODULE,
529 	}, {
530 		.name = "DCDC_REG3",
531 		.supply_name = "vcc3",
532 		.of_match = of_match_ptr("DCDC_REG3"),
533 		.regulators_node = of_match_ptr("regulators"),
534 		.id = RK808_ID_DCDC3,
535 		.ops = &rk808_switch_ops,
536 		.type = REGULATOR_VOLTAGE,
537 		.n_voltages = 1,
538 		.enable_reg = RK808_DCDC_EN_REG,
539 		.enable_mask = BIT(2),
540 		.owner = THIS_MODULE,
541 	},
542 	RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
543 		RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
544 		RK808_DCDC_EN_REG, BIT(3), 0),
545 	RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
546 		RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
547 		BIT(0), 400),
548 	RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
549 		RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
550 		BIT(1), 400),
551 	{
552 		.name = "LDO_REG3",
553 		.supply_name = "vcc7",
554 		.of_match = of_match_ptr("LDO_REG3"),
555 		.regulators_node = of_match_ptr("regulators"),
556 		.id = RK808_ID_LDO3,
557 		.ops = &rk808_reg_ops_ranges,
558 		.type = REGULATOR_VOLTAGE,
559 		.n_voltages = 16,
560 		.linear_ranges = rk808_ldo3_voltage_ranges,
561 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
562 		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
563 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
564 		.enable_reg = RK808_LDO_EN_REG,
565 		.enable_mask = BIT(2),
566 		.enable_time = 400,
567 		.owner = THIS_MODULE,
568 	},
569 	RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
570 		RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
571 		BIT(3), 400),
572 	RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
573 		RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
574 		BIT(4), 400),
575 	RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
576 		RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
577 		BIT(5), 400),
578 	RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
579 		RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
580 		BIT(6), 400),
581 	RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
582 		RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
583 		BIT(7), 400),
584 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
585 		RK808_DCDC_EN_REG, BIT(5)),
586 	RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
587 		RK808_DCDC_EN_REG, BIT(6)),
588 };
589 
590 static const struct regulator_desc rk818_reg[] = {
591 	{
592 		.name = "DCDC_REG1",
593 		.supply_name = "vcc1",
594 		.of_match = of_match_ptr("DCDC_REG1"),
595 		.regulators_node = of_match_ptr("regulators"),
596 		.id = RK818_ID_DCDC1,
597 		.ops = &rk808_reg_ops,
598 		.type = REGULATOR_VOLTAGE,
599 		.min_uV = 712500,
600 		.uV_step = 12500,
601 		.n_voltages = 64,
602 		.vsel_reg = RK818_BUCK1_ON_VSEL_REG,
603 		.vsel_mask = RK818_BUCK_VSEL_MASK,
604 		.enable_reg = RK818_DCDC_EN_REG,
605 		.enable_mask = BIT(0),
606 		.owner = THIS_MODULE,
607 	}, {
608 		.name = "DCDC_REG2",
609 		.supply_name = "vcc2",
610 		.of_match = of_match_ptr("DCDC_REG2"),
611 		.regulators_node = of_match_ptr("regulators"),
612 		.id = RK818_ID_DCDC2,
613 		.ops = &rk808_reg_ops,
614 		.type = REGULATOR_VOLTAGE,
615 		.min_uV = 712500,
616 		.uV_step = 12500,
617 		.n_voltages = 64,
618 		.vsel_reg = RK818_BUCK2_ON_VSEL_REG,
619 		.vsel_mask = RK818_BUCK_VSEL_MASK,
620 		.enable_reg = RK818_DCDC_EN_REG,
621 		.enable_mask = BIT(1),
622 		.owner = THIS_MODULE,
623 	}, {
624 		.name = "DCDC_REG3",
625 		.supply_name = "vcc3",
626 		.of_match = of_match_ptr("DCDC_REG3"),
627 		.regulators_node = of_match_ptr("regulators"),
628 		.id = RK818_ID_DCDC3,
629 		.ops = &rk808_switch_ops,
630 		.type = REGULATOR_VOLTAGE,
631 		.n_voltages = 1,
632 		.enable_reg = RK818_DCDC_EN_REG,
633 		.enable_mask = BIT(2),
634 		.owner = THIS_MODULE,
635 	},
636 	RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
637 		RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
638 		RK818_DCDC_EN_REG, BIT(3), 0),
639 	RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
640 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
641 		RK818_DCDC_EN_REG, BIT(4), 0),
642 	RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
643 		RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
644 		BIT(0), 400),
645 	RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
646 		RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
647 		BIT(1), 400),
648 	{
649 		.name = "LDO_REG3",
650 		.supply_name = "vcc7",
651 		.of_match = of_match_ptr("LDO_REG3"),
652 		.regulators_node = of_match_ptr("regulators"),
653 		.id = RK818_ID_LDO3,
654 		.ops = &rk808_reg_ops_ranges,
655 		.type = REGULATOR_VOLTAGE,
656 		.n_voltages = 16,
657 		.linear_ranges = rk808_ldo3_voltage_ranges,
658 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
659 		.vsel_reg = RK818_LDO3_ON_VSEL_REG,
660 		.vsel_mask = RK818_LDO3_ON_VSEL_MASK,
661 		.enable_reg = RK818_LDO_EN_REG,
662 		.enable_mask = BIT(2),
663 		.enable_time = 400,
664 		.owner = THIS_MODULE,
665 	},
666 	RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
667 		RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
668 		BIT(3), 400),
669 	RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
670 		RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
671 		BIT(4), 400),
672 	RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
673 		RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
674 		BIT(5), 400),
675 	RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
676 		RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
677 		BIT(6), 400),
678 	RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
679 		RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
680 		BIT(7), 400),
681 	RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
682 		RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
683 		RK818_DCDC_EN_REG, BIT(5), 400),
684 	RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
685 		RK818_DCDC_EN_REG, BIT(6)),
686 	RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
687 		RK818_H5V_EN_REG, BIT(0)),
688 	RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
689 		RK818_DCDC_EN_REG, BIT(7)),
690 };
691 
692 static int rk808_regulator_dt_parse_pdata(struct device *dev,
693 				   struct device *client_dev,
694 				   struct regmap *map,
695 				   struct rk808_regulator_data *pdata)
696 {
697 	struct device_node *np;
698 	int tmp, ret = 0, i;
699 
700 	np = of_get_child_by_name(client_dev->of_node, "regulators");
701 	if (!np)
702 		return -ENXIO;
703 
704 	for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
705 		pdata->dvs_gpio[i] =
706 			devm_gpiod_get_index_optional(client_dev, "dvs", i,
707 						      GPIOD_OUT_LOW);
708 		if (IS_ERR(pdata->dvs_gpio[i])) {
709 			ret = PTR_ERR(pdata->dvs_gpio[i]);
710 			dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
711 			goto dt_parse_end;
712 		}
713 
714 		if (!pdata->dvs_gpio[i]) {
715 			dev_warn(dev, "there is no dvs%d gpio\n", i);
716 			continue;
717 		}
718 
719 		tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
720 		ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
721 				gpiod_is_active_low(pdata->dvs_gpio[i]) ?
722 				0 : tmp);
723 	}
724 
725 dt_parse_end:
726 	of_node_put(np);
727 	return ret;
728 }
729 
730 static int rk808_regulator_probe(struct platform_device *pdev)
731 {
732 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
733 	struct i2c_client *client = rk808->i2c;
734 	struct regulator_config config = {};
735 	struct regulator_dev *rk808_rdev;
736 	struct rk808_regulator_data *pdata;
737 	const struct regulator_desc *regulators;
738 	int ret, i, nregulators;
739 
740 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
741 	if (!pdata)
742 		return -ENOMEM;
743 
744 	ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
745 					     rk808->regmap, pdata);
746 	if (ret < 0)
747 		return ret;
748 
749 	platform_set_drvdata(pdev, pdata);
750 
751 	switch (rk808->variant) {
752 	case RK805_ID:
753 		regulators = rk805_reg;
754 		nregulators = RK805_NUM_REGULATORS;
755 		break;
756 	case RK808_ID:
757 		regulators = rk808_reg;
758 		nregulators = RK808_NUM_REGULATORS;
759 		break;
760 	case RK818_ID:
761 		regulators = rk818_reg;
762 		nregulators = RK818_NUM_REGULATORS;
763 		break;
764 	default:
765 		dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
766 			rk808->variant);
767 		return -EINVAL;
768 	}
769 
770 	config.dev = &client->dev;
771 	config.driver_data = pdata;
772 	config.regmap = rk808->regmap;
773 
774 	/* Instantiate the regulators */
775 	for (i = 0; i < nregulators; i++) {
776 		rk808_rdev = devm_regulator_register(&pdev->dev,
777 						     &regulators[i], &config);
778 		if (IS_ERR(rk808_rdev)) {
779 			dev_err(&client->dev,
780 				"failed to register %d regulator\n", i);
781 			return PTR_ERR(rk808_rdev);
782 		}
783 	}
784 
785 	return 0;
786 }
787 
788 static struct platform_driver rk808_regulator_driver = {
789 	.probe = rk808_regulator_probe,
790 	.driver = {
791 		.name = "rk808-regulator"
792 	},
793 };
794 
795 module_platform_driver(rk808_regulator_driver);
796 
797 MODULE_DESCRIPTION("regulator driver for the RK805/RK808/RK818 series PMICs");
798 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
799 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
800 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
801 MODULE_LICENSE("GPL");
802 MODULE_ALIAS("platform:rk808-regulator");
803