1 /*
2  * Regulator driver for Rockchip RK808
3  *
4  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5  *
6  * Author: Chris Zhong <zyw@rock-chips.com>
7  * Author: Zhang Qing <zhangqing@rock-chips.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License,
11  * version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  */
18 
19 #include <linux/delay.h>
20 #include <linux/gpio.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
25 #include <linux/mfd/rk808.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/of_regulator.h>
28 #include <linux/gpio/consumer.h>
29 
30 /* Field Definitions */
31 #define RK808_BUCK_VSEL_MASK	0x3f
32 #define RK808_BUCK4_VSEL_MASK	0xf
33 #define RK808_LDO_VSEL_MASK	0x1f
34 
35 /* Ramp rate definitions for buck1 / buck2 only */
36 #define RK808_RAMP_RATE_OFFSET		3
37 #define RK808_RAMP_RATE_MASK		(3 << RK808_RAMP_RATE_OFFSET)
38 #define RK808_RAMP_RATE_2MV_PER_US	(0 << RK808_RAMP_RATE_OFFSET)
39 #define RK808_RAMP_RATE_4MV_PER_US	(1 << RK808_RAMP_RATE_OFFSET)
40 #define RK808_RAMP_RATE_6MV_PER_US	(2 << RK808_RAMP_RATE_OFFSET)
41 #define RK808_RAMP_RATE_10MV_PER_US	(3 << RK808_RAMP_RATE_OFFSET)
42 
43 #define RK808_DVS2_POL		BIT(2)
44 #define RK808_DVS1_POL		BIT(1)
45 
46 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
47 #define RK808_SLP_REG_OFFSET 1
48 
49 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
50 #define RK808_DVS_REG_OFFSET 2
51 
52 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
53 #define RK808_SLP_SET_OFF_REG_OFFSET 2
54 
55 /* max steps for increase voltage of Buck1/2, equal 100mv*/
56 #define MAX_STEPS_ONE_TIME 8
57 
58 struct rk808_regulator_data {
59 	struct gpio_desc *dvs_gpio[2];
60 };
61 
62 static const int rk808_buck_config_regs[] = {
63 	RK808_BUCK1_CONFIG_REG,
64 	RK808_BUCK2_CONFIG_REG,
65 	RK808_BUCK3_CONFIG_REG,
66 	RK808_BUCK4_CONFIG_REG,
67 };
68 
69 static const struct regulator_linear_range rk808_buck_voltage_ranges[] = {
70 	REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
71 };
72 
73 static const struct regulator_linear_range rk808_buck4_voltage_ranges[] = {
74 	REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
75 };
76 
77 static const struct regulator_linear_range rk808_ldo_voltage_ranges[] = {
78 	REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
79 };
80 
81 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
82 	REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
83 	REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
84 };
85 
86 static const struct regulator_linear_range rk808_ldo6_voltage_ranges[] = {
87 	REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
88 };
89 
90 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
91 {
92 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
93 	int id = rdev->desc->id - RK808_ID_DCDC1;
94 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
95 	unsigned int val;
96 	int ret;
97 
98 	if (!gpio || gpiod_get_value(gpio) == 0)
99 		return regulator_get_voltage_sel_regmap(rdev);
100 
101 	ret = regmap_read(rdev->regmap,
102 			  rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
103 			  &val);
104 	if (ret != 0)
105 		return ret;
106 
107 	val &= rdev->desc->vsel_mask;
108 	val >>= ffs(rdev->desc->vsel_mask) - 1;
109 
110 	return val;
111 }
112 
113 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
114 					     unsigned sel)
115 {
116 	int ret, delta_sel;
117 	unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
118 
119 	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
120 	if (ret != 0)
121 		return ret;
122 
123 	tmp = val & ~mask;
124 	old_sel = val & mask;
125 	old_sel >>= ffs(mask) - 1;
126 	delta_sel = sel - old_sel;
127 
128 	/*
129 	 * If directly modify the register to change the voltage, we will face
130 	 * the risk of overshoot. Put it into a multi-step, can effectively
131 	 * avoid this problem, a step is 100mv here.
132 	 */
133 	while (delta_sel > MAX_STEPS_ONE_TIME) {
134 		old_sel += MAX_STEPS_ONE_TIME;
135 		val = old_sel << (ffs(mask) - 1);
136 		val |= tmp;
137 
138 		/*
139 		 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
140 		 * 3 bytes (24 bits) plus start and stop so 26 bits.  So we've
141 		 * got more than 65 us between each voltage change and thus
142 		 * won't ramp faster than ~1500 uV / us.
143 		 */
144 		ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
145 		delta_sel = sel - old_sel;
146 	}
147 
148 	sel <<= ffs(mask) - 1;
149 	val = tmp | sel;
150 	ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
151 
152 	/*
153 	 * When we change the voltage register directly, the ramp rate is about
154 	 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
155 	 * so we needn't wait extra time after that.
156 	 */
157 	udelay(1);
158 
159 	return ret;
160 }
161 
162 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
163 					 unsigned sel)
164 {
165 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
166 	int id = rdev->desc->id - RK808_ID_DCDC1;
167 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
168 	unsigned int reg = rdev->desc->vsel_reg;
169 	unsigned old_sel;
170 	int ret, gpio_level;
171 
172 	if (!gpio)
173 		return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
174 
175 	gpio_level = gpiod_get_value(gpio);
176 	if (gpio_level == 0) {
177 		reg += RK808_DVS_REG_OFFSET;
178 		ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
179 	} else {
180 		ret = regmap_read(rdev->regmap,
181 				  reg + RK808_DVS_REG_OFFSET,
182 				  &old_sel);
183 	}
184 
185 	if (ret != 0)
186 		return ret;
187 
188 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
189 	sel |= old_sel & ~rdev->desc->vsel_mask;
190 
191 	ret = regmap_write(rdev->regmap, reg, sel);
192 	if (ret)
193 		return ret;
194 
195 	gpiod_set_value(gpio, !gpio_level);
196 
197 	return ret;
198 }
199 
200 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
201 				       unsigned int old_selector,
202 				       unsigned int new_selector)
203 {
204 	struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
205 	int id = rdev->desc->id - RK808_ID_DCDC1;
206 	struct gpio_desc *gpio = pdata->dvs_gpio[id];
207 
208 	/* if there is no dvs1/2 pin, we don't need wait extra time here. */
209 	if (!gpio)
210 		return 0;
211 
212 	return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
213 }
214 
215 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
216 {
217 	unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
218 	unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
219 						  RK808_ID_DCDC1];
220 
221 	switch (ramp_delay) {
222 	case 1 ... 2000:
223 		ramp_value = RK808_RAMP_RATE_2MV_PER_US;
224 		break;
225 	case 2001 ... 4000:
226 		ramp_value = RK808_RAMP_RATE_4MV_PER_US;
227 		break;
228 	case 4001 ... 6000:
229 		ramp_value = RK808_RAMP_RATE_6MV_PER_US;
230 		break;
231 	case 6001 ... 10000:
232 		break;
233 	default:
234 		pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
235 			rdev->desc->name, ramp_delay);
236 	}
237 
238 	return regmap_update_bits(rdev->regmap, reg,
239 				  RK808_RAMP_RATE_MASK, ramp_value);
240 }
241 
242 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
243 {
244 	unsigned int reg;
245 	int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
246 
247 	if (sel < 0)
248 		return -EINVAL;
249 
250 	reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
251 
252 	return regmap_update_bits(rdev->regmap, reg,
253 				  rdev->desc->vsel_mask,
254 				  sel);
255 }
256 
257 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
258 {
259 	unsigned int reg;
260 
261 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
262 
263 	return regmap_update_bits(rdev->regmap, reg,
264 				  rdev->desc->enable_mask,
265 				  0);
266 }
267 
268 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
269 {
270 	unsigned int reg;
271 
272 	reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
273 
274 	return regmap_update_bits(rdev->regmap, reg,
275 				  rdev->desc->enable_mask,
276 				  rdev->desc->enable_mask);
277 }
278 
279 static struct regulator_ops rk808_buck1_2_ops = {
280 	.list_voltage		= regulator_list_voltage_linear_range,
281 	.map_voltage		= regulator_map_voltage_linear_range,
282 	.get_voltage_sel	= rk808_buck1_2_get_voltage_sel_regmap,
283 	.set_voltage_sel	= rk808_buck1_2_set_voltage_sel,
284 	.set_voltage_time_sel	= rk808_buck1_2_set_voltage_time_sel,
285 	.enable			= regulator_enable_regmap,
286 	.disable		= regulator_disable_regmap,
287 	.is_enabled		= regulator_is_enabled_regmap,
288 	.set_ramp_delay		= rk808_set_ramp_delay,
289 	.set_suspend_voltage	= rk808_set_suspend_voltage,
290 	.set_suspend_enable	= rk808_set_suspend_enable,
291 	.set_suspend_disable	= rk808_set_suspend_disable,
292 };
293 
294 static struct regulator_ops rk808_reg_ops = {
295 	.list_voltage		= regulator_list_voltage_linear_range,
296 	.map_voltage		= regulator_map_voltage_linear_range,
297 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
298 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
299 	.enable			= regulator_enable_regmap,
300 	.disable		= regulator_disable_regmap,
301 	.is_enabled		= regulator_is_enabled_regmap,
302 	.set_suspend_voltage	= rk808_set_suspend_voltage,
303 	.set_suspend_enable	= rk808_set_suspend_enable,
304 	.set_suspend_disable	= rk808_set_suspend_disable,
305 };
306 
307 static struct regulator_ops rk808_switch_ops = {
308 	.enable			= regulator_enable_regmap,
309 	.disable		= regulator_disable_regmap,
310 	.is_enabled		= regulator_is_enabled_regmap,
311 	.set_suspend_enable	= rk808_set_suspend_enable,
312 	.set_suspend_disable	= rk808_set_suspend_disable,
313 };
314 
315 static const struct regulator_desc rk808_reg[] = {
316 	{
317 		.name = "DCDC_REG1",
318 		.supply_name = "vcc1",
319 		.id = RK808_ID_DCDC1,
320 		.ops = &rk808_buck1_2_ops,
321 		.type = REGULATOR_VOLTAGE,
322 		.n_voltages = 64,
323 		.linear_ranges = rk808_buck_voltage_ranges,
324 		.n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
325 		.vsel_reg = RK808_BUCK1_ON_VSEL_REG,
326 		.vsel_mask = RK808_BUCK_VSEL_MASK,
327 		.enable_reg = RK808_DCDC_EN_REG,
328 		.enable_mask = BIT(0),
329 		.owner = THIS_MODULE,
330 	}, {
331 		.name = "DCDC_REG2",
332 		.supply_name = "vcc2",
333 		.id = RK808_ID_DCDC2,
334 		.ops = &rk808_buck1_2_ops,
335 		.type = REGULATOR_VOLTAGE,
336 		.n_voltages = 64,
337 		.linear_ranges = rk808_buck_voltage_ranges,
338 		.n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
339 		.vsel_reg = RK808_BUCK2_ON_VSEL_REG,
340 		.vsel_mask = RK808_BUCK_VSEL_MASK,
341 		.enable_reg = RK808_DCDC_EN_REG,
342 		.enable_mask = BIT(1),
343 		.owner = THIS_MODULE,
344 	}, {
345 		.name = "DCDC_REG3",
346 		.supply_name = "vcc3",
347 		.id = RK808_ID_DCDC3,
348 		.ops = &rk808_switch_ops,
349 		.type = REGULATOR_VOLTAGE,
350 		.n_voltages = 1,
351 		.enable_reg = RK808_DCDC_EN_REG,
352 		.enable_mask = BIT(2),
353 		.owner = THIS_MODULE,
354 	}, {
355 		.name = "DCDC_REG4",
356 		.supply_name = "vcc4",
357 		.id = RK808_ID_DCDC4,
358 		.ops = &rk808_reg_ops,
359 		.type = REGULATOR_VOLTAGE,
360 		.n_voltages = 16,
361 		.linear_ranges = rk808_buck4_voltage_ranges,
362 		.n_linear_ranges = ARRAY_SIZE(rk808_buck4_voltage_ranges),
363 		.vsel_reg = RK808_BUCK4_ON_VSEL_REG,
364 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
365 		.enable_reg = RK808_DCDC_EN_REG,
366 		.enable_mask = BIT(3),
367 		.owner = THIS_MODULE,
368 	}, {
369 		.name = "LDO_REG1",
370 		.supply_name = "vcc6",
371 		.id = RK808_ID_LDO1,
372 		.ops = &rk808_reg_ops,
373 		.type = REGULATOR_VOLTAGE,
374 		.n_voltages = 17,
375 		.linear_ranges = rk808_ldo_voltage_ranges,
376 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
377 		.vsel_reg = RK808_LDO1_ON_VSEL_REG,
378 		.vsel_mask = RK808_LDO_VSEL_MASK,
379 		.enable_reg = RK808_LDO_EN_REG,
380 		.enable_mask = BIT(0),
381 		.enable_time = 400,
382 		.owner = THIS_MODULE,
383 	}, {
384 		.name = "LDO_REG2",
385 		.supply_name = "vcc6",
386 		.id = RK808_ID_LDO2,
387 		.ops = &rk808_reg_ops,
388 		.type = REGULATOR_VOLTAGE,
389 		.n_voltages = 17,
390 		.linear_ranges = rk808_ldo_voltage_ranges,
391 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
392 		.vsel_reg = RK808_LDO2_ON_VSEL_REG,
393 		.vsel_mask = RK808_LDO_VSEL_MASK,
394 		.enable_reg = RK808_LDO_EN_REG,
395 		.enable_mask = BIT(1),
396 		.enable_time = 400,
397 		.owner = THIS_MODULE,
398 	}, {
399 		.name = "LDO_REG3",
400 		.supply_name = "vcc7",
401 		.id = RK808_ID_LDO3,
402 		.ops = &rk808_reg_ops,
403 		.type = REGULATOR_VOLTAGE,
404 		.n_voltages = 16,
405 		.linear_ranges = rk808_ldo3_voltage_ranges,
406 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
407 		.vsel_reg = RK808_LDO3_ON_VSEL_REG,
408 		.vsel_mask = RK808_BUCK4_VSEL_MASK,
409 		.enable_reg = RK808_LDO_EN_REG,
410 		.enable_mask = BIT(2),
411 		.enable_time = 400,
412 		.owner = THIS_MODULE,
413 	}, {
414 		.name = "LDO_REG4",
415 		.supply_name = "vcc9",
416 		.id = RK808_ID_LDO4,
417 		.ops = &rk808_reg_ops,
418 		.type = REGULATOR_VOLTAGE,
419 		.n_voltages = 17,
420 		.linear_ranges = rk808_ldo_voltage_ranges,
421 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
422 		.vsel_reg = RK808_LDO4_ON_VSEL_REG,
423 		.vsel_mask = RK808_LDO_VSEL_MASK,
424 		.enable_reg = RK808_LDO_EN_REG,
425 		.enable_mask = BIT(3),
426 		.enable_time = 400,
427 		.owner = THIS_MODULE,
428 	}, {
429 		.name = "LDO_REG5",
430 		.supply_name = "vcc9",
431 		.id = RK808_ID_LDO5,
432 		.ops = &rk808_reg_ops,
433 		.type = REGULATOR_VOLTAGE,
434 		.n_voltages = 17,
435 		.linear_ranges = rk808_ldo_voltage_ranges,
436 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
437 		.vsel_reg = RK808_LDO5_ON_VSEL_REG,
438 		.vsel_mask = RK808_LDO_VSEL_MASK,
439 		.enable_reg = RK808_LDO_EN_REG,
440 		.enable_mask = BIT(4),
441 		.enable_time = 400,
442 		.owner = THIS_MODULE,
443 	}, {
444 		.name = "LDO_REG6",
445 		.supply_name = "vcc10",
446 		.id = RK808_ID_LDO6,
447 		.ops = &rk808_reg_ops,
448 		.type = REGULATOR_VOLTAGE,
449 		.n_voltages = 18,
450 		.linear_ranges = rk808_ldo6_voltage_ranges,
451 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
452 		.vsel_reg = RK808_LDO6_ON_VSEL_REG,
453 		.vsel_mask = RK808_LDO_VSEL_MASK,
454 		.enable_reg = RK808_LDO_EN_REG,
455 		.enable_mask = BIT(5),
456 		.enable_time = 400,
457 		.owner = THIS_MODULE,
458 	}, {
459 		.name = "LDO_REG7",
460 		.supply_name = "vcc7",
461 		.id = RK808_ID_LDO7,
462 		.ops = &rk808_reg_ops,
463 		.type = REGULATOR_VOLTAGE,
464 		.n_voltages = 18,
465 		.linear_ranges = rk808_ldo6_voltage_ranges,
466 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
467 		.vsel_reg = RK808_LDO7_ON_VSEL_REG,
468 		.vsel_mask = RK808_LDO_VSEL_MASK,
469 		.enable_reg = RK808_LDO_EN_REG,
470 		.enable_mask = BIT(6),
471 		.enable_time = 400,
472 		.owner = THIS_MODULE,
473 	}, {
474 		.name = "LDO_REG8",
475 		.supply_name = "vcc11",
476 		.id = RK808_ID_LDO8,
477 		.ops = &rk808_reg_ops,
478 		.type = REGULATOR_VOLTAGE,
479 		.n_voltages = 17,
480 		.linear_ranges = rk808_ldo_voltage_ranges,
481 		.n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
482 		.vsel_reg = RK808_LDO8_ON_VSEL_REG,
483 		.vsel_mask = RK808_LDO_VSEL_MASK,
484 		.enable_reg = RK808_LDO_EN_REG,
485 		.enable_mask = BIT(7),
486 		.enable_time = 400,
487 		.owner = THIS_MODULE,
488 	}, {
489 		.name = "SWITCH_REG1",
490 		.supply_name = "vcc8",
491 		.id = RK808_ID_SWITCH1,
492 		.ops = &rk808_switch_ops,
493 		.type = REGULATOR_VOLTAGE,
494 		.enable_reg = RK808_DCDC_EN_REG,
495 		.enable_mask = BIT(5),
496 		.owner = THIS_MODULE,
497 	}, {
498 		.name = "SWITCH_REG2",
499 		.supply_name = "vcc12",
500 		.id = RK808_ID_SWITCH2,
501 		.ops = &rk808_switch_ops,
502 		.type = REGULATOR_VOLTAGE,
503 		.enable_reg = RK808_DCDC_EN_REG,
504 		.enable_mask = BIT(6),
505 		.owner = THIS_MODULE,
506 	},
507 };
508 
509 static struct of_regulator_match rk808_reg_matches[] = {
510 	[RK808_ID_DCDC1]	= { .name = "DCDC_REG1" },
511 	[RK808_ID_DCDC2]	= { .name = "DCDC_REG2" },
512 	[RK808_ID_DCDC3]	= { .name = "DCDC_REG3" },
513 	[RK808_ID_DCDC4]	= { .name = "DCDC_REG4" },
514 	[RK808_ID_LDO1]		= { .name = "LDO_REG1" },
515 	[RK808_ID_LDO2]		= { .name = "LDO_REG2" },
516 	[RK808_ID_LDO3]		= { .name = "LDO_REG3" },
517 	[RK808_ID_LDO4]		= { .name = "LDO_REG4" },
518 	[RK808_ID_LDO5]		= { .name = "LDO_REG5" },
519 	[RK808_ID_LDO6]		= { .name = "LDO_REG6" },
520 	[RK808_ID_LDO7]		= { .name = "LDO_REG7" },
521 	[RK808_ID_LDO8]		= { .name = "LDO_REG8" },
522 	[RK808_ID_SWITCH1]	= { .name = "SWITCH_REG1" },
523 	[RK808_ID_SWITCH2]	= { .name = "SWITCH_REG2" },
524 };
525 
526 static int rk808_regulator_dt_parse_pdata(struct device *dev,
527 				   struct device *client_dev,
528 				   struct regmap *map,
529 				   struct rk808_regulator_data *pdata)
530 {
531 	struct device_node *np;
532 	int tmp, ret, i;
533 
534 	np = of_get_child_by_name(client_dev->of_node, "regulators");
535 	if (!np)
536 		return -ENXIO;
537 
538 	ret = of_regulator_match(dev, np, rk808_reg_matches,
539 				 RK808_NUM_REGULATORS);
540 	if (ret < 0)
541 		goto dt_parse_end;
542 
543 	for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
544 		pdata->dvs_gpio[i] =
545 			devm_gpiod_get_index_optional(client_dev, "dvs", i,
546 						      GPIOD_OUT_LOW);
547 		if (IS_ERR(pdata->dvs_gpio[i])) {
548 			ret = PTR_ERR(pdata->dvs_gpio[i]);
549 			dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
550 			goto dt_parse_end;
551 		}
552 
553 		if (!pdata->dvs_gpio[i]) {
554 			dev_warn(dev, "there is no dvs%d gpio\n", i);
555 			continue;
556 		}
557 
558 		tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
559 		ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
560 				gpiod_is_active_low(pdata->dvs_gpio[i]) ?
561 				0 : tmp);
562 	}
563 
564 dt_parse_end:
565 	of_node_put(np);
566 	return ret;
567 }
568 
569 static int rk808_regulator_probe(struct platform_device *pdev)
570 {
571 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
572 	struct i2c_client *client = rk808->i2c;
573 	struct regulator_config config = {};
574 	struct regulator_dev *rk808_rdev;
575 	struct rk808_regulator_data *pdata;
576 	int ret, i;
577 
578 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
579 	if (!pdata)
580 		return -ENOMEM;
581 
582 	ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
583 					     rk808->regmap, pdata);
584 	if (ret < 0)
585 		return ret;
586 
587 	platform_set_drvdata(pdev, pdata);
588 
589 	/* Instantiate the regulators */
590 	for (i = 0; i < RK808_NUM_REGULATORS; i++) {
591 		if (!rk808_reg_matches[i].init_data ||
592 		    !rk808_reg_matches[i].of_node)
593 			continue;
594 
595 		config.dev = &client->dev;
596 		config.driver_data = pdata;
597 		config.regmap = rk808->regmap;
598 		config.of_node = rk808_reg_matches[i].of_node;
599 		config.init_data = rk808_reg_matches[i].init_data;
600 
601 		rk808_rdev = devm_regulator_register(&pdev->dev,
602 						     &rk808_reg[i], &config);
603 		if (IS_ERR(rk808_rdev)) {
604 			dev_err(&client->dev,
605 				"failed to register %d regulator\n", i);
606 			return PTR_ERR(rk808_rdev);
607 		}
608 	}
609 
610 	return 0;
611 }
612 
613 static struct platform_driver rk808_regulator_driver = {
614 	.probe = rk808_regulator_probe,
615 	.driver = {
616 		.name = "rk808-regulator",
617 		.owner = THIS_MODULE,
618 	},
619 };
620 
621 module_platform_driver(rk808_regulator_driver);
622 
623 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
624 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
625 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
626 MODULE_LICENSE("GPL");
627 MODULE_ALIAS("platform:rk808-regulator");
628