2 * Regulator driver for Rockchip RK808/RK818
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
9 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
11 * Author: Wadim Egorov <w.egorov@phytec.de>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 #include <linux/delay.h>
24 #include <linux/gpio.h>
25 #include <linux/i2c.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/mfd/rk808.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/of_regulator.h>
32 #include <linux/gpio/consumer.h>
34 /* Field Definitions */
35 #define RK808_BUCK_VSEL_MASK 0x3f
36 #define RK808_BUCK4_VSEL_MASK 0xf
37 #define RK808_LDO_VSEL_MASK 0x1f
39 #define RK818_BUCK_VSEL_MASK 0x3f
40 #define RK818_BUCK4_VSEL_MASK 0x1f
41 #define RK818_LDO_VSEL_MASK 0x1f
42 #define RK818_LDO3_ON_VSEL_MASK 0xf
43 #define RK818_BOOST_ON_VSEL_MASK 0xe0
45 /* Ramp rate definitions for buck1 / buck2 only */
46 #define RK808_RAMP_RATE_OFFSET 3
47 #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET)
48 #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET)
49 #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET)
50 #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET)
51 #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET)
53 #define RK808_DVS2_POL BIT(2)
54 #define RK808_DVS1_POL BIT(1)
56 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
57 #define RK808_SLP_REG_OFFSET 1
59 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
60 #define RK808_DVS_REG_OFFSET 2
62 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
63 #define RK808_SLP_SET_OFF_REG_OFFSET 2
65 /* max steps for increase voltage of Buck1/2, equal 100mv*/
66 #define MAX_STEPS_ONE_TIME 8
68 #define RK805_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
69 _vmask, _ereg, _emask, _etime) \
72 .supply_name = (_supply), \
73 .of_match = of_match_ptr(_match), \
74 .regulators_node = of_match_ptr("regulators"), \
75 .type = REGULATOR_VOLTAGE, \
77 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
78 .owner = THIS_MODULE, \
79 .min_uV = (_min) * 1000, \
80 .uV_step = (_step) * 1000, \
81 .vsel_reg = (_vreg), \
82 .vsel_mask = (_vmask), \
83 .enable_reg = (_ereg), \
84 .enable_mask = (_emask), \
85 .enable_time = (_etime), \
86 .ops = &rk805_reg_ops, \
89 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
90 _vmask, _ereg, _emask, _etime) \
93 .supply_name = (_supply), \
94 .of_match = of_match_ptr(_match), \
95 .regulators_node = of_match_ptr("regulators"), \
96 .type = REGULATOR_VOLTAGE, \
98 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
99 .owner = THIS_MODULE, \
100 .min_uV = (_min) * 1000, \
101 .uV_step = (_step) * 1000, \
102 .vsel_reg = (_vreg), \
103 .vsel_mask = (_vmask), \
104 .enable_reg = (_ereg), \
105 .enable_mask = (_emask), \
106 .enable_time = (_etime), \
107 .ops = &rk808_reg_ops, \
110 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \
113 .supply_name = (_supply), \
114 .of_match = of_match_ptr(_match), \
115 .regulators_node = of_match_ptr("regulators"), \
116 .type = REGULATOR_VOLTAGE, \
118 .enable_reg = (_ereg), \
119 .enable_mask = (_emask), \
120 .owner = THIS_MODULE, \
121 .ops = &rk808_switch_ops \
125 struct rk808_regulator_data {
126 struct gpio_desc *dvs_gpio[2];
129 static const int rk808_buck_config_regs[] = {
130 RK808_BUCK1_CONFIG_REG,
131 RK808_BUCK2_CONFIG_REG,
132 RK808_BUCK3_CONFIG_REG,
133 RK808_BUCK4_CONFIG_REG,
136 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
137 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
138 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
141 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
143 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
144 int id = rdev_get_id(rdev);
145 struct gpio_desc *gpio = pdata->dvs_gpio[id];
149 if (!gpio || gpiod_get_value(gpio) == 0)
150 return regulator_get_voltage_sel_regmap(rdev);
152 ret = regmap_read(rdev->regmap,
153 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
158 val &= rdev->desc->vsel_mask;
159 val >>= ffs(rdev->desc->vsel_mask) - 1;
164 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
168 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
170 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
175 old_sel = val & mask;
176 old_sel >>= ffs(mask) - 1;
177 delta_sel = sel - old_sel;
180 * If directly modify the register to change the voltage, we will face
181 * the risk of overshoot. Put it into a multi-step, can effectively
182 * avoid this problem, a step is 100mv here.
184 while (delta_sel > MAX_STEPS_ONE_TIME) {
185 old_sel += MAX_STEPS_ONE_TIME;
186 val = old_sel << (ffs(mask) - 1);
190 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
191 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
192 * got more than 65 us between each voltage change and thus
193 * won't ramp faster than ~1500 uV / us.
195 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
196 delta_sel = sel - old_sel;
199 sel <<= ffs(mask) - 1;
201 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
204 * When we change the voltage register directly, the ramp rate is about
205 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
206 * so we needn't wait extra time after that.
213 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
216 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
217 int id = rdev_get_id(rdev);
218 struct gpio_desc *gpio = pdata->dvs_gpio[id];
219 unsigned int reg = rdev->desc->vsel_reg;
224 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
226 gpio_level = gpiod_get_value(gpio);
227 if (gpio_level == 0) {
228 reg += RK808_DVS_REG_OFFSET;
229 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
231 ret = regmap_read(rdev->regmap,
232 reg + RK808_DVS_REG_OFFSET,
239 sel <<= ffs(rdev->desc->vsel_mask) - 1;
240 sel |= old_sel & ~rdev->desc->vsel_mask;
242 ret = regmap_write(rdev->regmap, reg, sel);
246 gpiod_set_value(gpio, !gpio_level);
251 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
252 unsigned int old_selector,
253 unsigned int new_selector)
255 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
256 int id = rdev_get_id(rdev);
257 struct gpio_desc *gpio = pdata->dvs_gpio[id];
259 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
263 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
266 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
268 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
269 unsigned int reg = rk808_buck_config_regs[rdev_get_id(rdev)];
271 switch (ramp_delay) {
273 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
276 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
279 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
284 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
285 rdev->desc->name, ramp_delay);
288 return regmap_update_bits(rdev->regmap, reg,
289 RK808_RAMP_RATE_MASK, ramp_value);
292 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
295 int sel = regulator_map_voltage_linear(rdev, uv, uv);
300 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
302 return regmap_update_bits(rdev->regmap, reg,
303 rdev->desc->vsel_mask,
307 static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
310 int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
315 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
317 return regmap_update_bits(rdev->regmap, reg,
318 rdev->desc->vsel_mask,
322 static int rk805_set_suspend_enable(struct regulator_dev *rdev)
326 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
328 return regmap_update_bits(rdev->regmap, reg,
329 rdev->desc->enable_mask,
330 rdev->desc->enable_mask);
333 static int rk805_set_suspend_disable(struct regulator_dev *rdev)
337 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
339 return regmap_update_bits(rdev->regmap, reg,
340 rdev->desc->enable_mask,
344 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
348 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
350 return regmap_update_bits(rdev->regmap, reg,
351 rdev->desc->enable_mask,
355 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
359 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
361 return regmap_update_bits(rdev->regmap, reg,
362 rdev->desc->enable_mask,
363 rdev->desc->enable_mask);
366 static struct regulator_ops rk805_reg_ops = {
367 .list_voltage = regulator_list_voltage_linear,
368 .map_voltage = regulator_map_voltage_linear,
369 .get_voltage_sel = regulator_get_voltage_sel_regmap,
370 .set_voltage_sel = regulator_set_voltage_sel_regmap,
371 .enable = regulator_enable_regmap,
372 .disable = regulator_disable_regmap,
373 .is_enabled = regulator_is_enabled_regmap,
374 .set_suspend_voltage = rk808_set_suspend_voltage,
375 .set_suspend_enable = rk805_set_suspend_enable,
376 .set_suspend_disable = rk805_set_suspend_disable,
379 static struct regulator_ops rk805_switch_ops = {
380 .enable = regulator_enable_regmap,
381 .disable = regulator_disable_regmap,
382 .is_enabled = regulator_is_enabled_regmap,
383 .set_suspend_enable = rk805_set_suspend_enable,
384 .set_suspend_disable = rk805_set_suspend_disable,
387 static struct regulator_ops rk808_buck1_2_ops = {
388 .list_voltage = regulator_list_voltage_linear,
389 .map_voltage = regulator_map_voltage_linear,
390 .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap,
391 .set_voltage_sel = rk808_buck1_2_set_voltage_sel,
392 .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel,
393 .enable = regulator_enable_regmap,
394 .disable = regulator_disable_regmap,
395 .is_enabled = regulator_is_enabled_regmap,
396 .set_ramp_delay = rk808_set_ramp_delay,
397 .set_suspend_voltage = rk808_set_suspend_voltage,
398 .set_suspend_enable = rk808_set_suspend_enable,
399 .set_suspend_disable = rk808_set_suspend_disable,
402 static struct regulator_ops rk808_reg_ops = {
403 .list_voltage = regulator_list_voltage_linear,
404 .map_voltage = regulator_map_voltage_linear,
405 .get_voltage_sel = regulator_get_voltage_sel_regmap,
406 .set_voltage_sel = regulator_set_voltage_sel_regmap,
407 .enable = regulator_enable_regmap,
408 .disable = regulator_disable_regmap,
409 .is_enabled = regulator_is_enabled_regmap,
410 .set_suspend_voltage = rk808_set_suspend_voltage,
411 .set_suspend_enable = rk808_set_suspend_enable,
412 .set_suspend_disable = rk808_set_suspend_disable,
415 static struct regulator_ops rk808_reg_ops_ranges = {
416 .list_voltage = regulator_list_voltage_linear_range,
417 .map_voltage = regulator_map_voltage_linear_range,
418 .get_voltage_sel = regulator_get_voltage_sel_regmap,
419 .set_voltage_sel = regulator_set_voltage_sel_regmap,
420 .enable = regulator_enable_regmap,
421 .disable = regulator_disable_regmap,
422 .is_enabled = regulator_is_enabled_regmap,
423 .set_suspend_voltage = rk808_set_suspend_voltage_range,
424 .set_suspend_enable = rk808_set_suspend_enable,
425 .set_suspend_disable = rk808_set_suspend_disable,
428 static struct regulator_ops rk808_switch_ops = {
429 .enable = regulator_enable_regmap,
430 .disable = regulator_disable_regmap,
431 .is_enabled = regulator_is_enabled_regmap,
432 .set_suspend_enable = rk808_set_suspend_enable,
433 .set_suspend_disable = rk808_set_suspend_disable,
436 static const struct regulator_desc rk805_reg[] = {
439 .supply_name = "vcc1",
440 .of_match = of_match_ptr("DCDC_REG1"),
441 .regulators_node = of_match_ptr("regulators"),
442 .id = RK805_ID_DCDC1,
443 .ops = &rk805_reg_ops,
444 .type = REGULATOR_VOLTAGE,
448 .vsel_reg = RK805_BUCK1_ON_VSEL_REG,
449 .vsel_mask = RK818_BUCK_VSEL_MASK,
450 .enable_reg = RK805_DCDC_EN_REG,
451 .enable_mask = BIT(0),
452 .owner = THIS_MODULE,
455 .supply_name = "vcc2",
456 .of_match = of_match_ptr("DCDC_REG2"),
457 .regulators_node = of_match_ptr("regulators"),
458 .id = RK805_ID_DCDC2,
459 .ops = &rk805_reg_ops,
460 .type = REGULATOR_VOLTAGE,
464 .vsel_reg = RK805_BUCK2_ON_VSEL_REG,
465 .vsel_mask = RK818_BUCK_VSEL_MASK,
466 .enable_reg = RK805_DCDC_EN_REG,
467 .enable_mask = BIT(1),
468 .owner = THIS_MODULE,
471 .supply_name = "vcc3",
472 .of_match = of_match_ptr("DCDC_REG3"),
473 .regulators_node = of_match_ptr("regulators"),
474 .id = RK805_ID_DCDC3,
475 .ops = &rk805_switch_ops,
476 .type = REGULATOR_VOLTAGE,
478 .enable_reg = RK805_DCDC_EN_REG,
479 .enable_mask = BIT(2),
480 .owner = THIS_MODULE,
483 RK805_DESC(RK805_ID_DCDC4, "DCDC_REG4", "vcc4", 800, 3400, 100,
484 RK805_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
485 RK805_DCDC_EN_REG, BIT(3), 0),
487 RK805_DESC(RK805_ID_LDO1, "LDO_REG1", "vcc5", 800, 3400, 100,
488 RK805_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
490 RK805_DESC(RK805_ID_LDO2, "LDO_REG2", "vcc5", 800, 3400, 100,
491 RK805_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
493 RK805_DESC(RK805_ID_LDO3, "LDO_REG3", "vcc6", 800, 3400, 100,
494 RK805_LDO3_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK805_LDO_EN_REG,
498 static const struct regulator_desc rk808_reg[] = {
501 .supply_name = "vcc1",
502 .of_match = of_match_ptr("DCDC_REG1"),
503 .regulators_node = of_match_ptr("regulators"),
504 .id = RK808_ID_DCDC1,
505 .ops = &rk808_buck1_2_ops,
506 .type = REGULATOR_VOLTAGE,
510 .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
511 .vsel_mask = RK808_BUCK_VSEL_MASK,
512 .enable_reg = RK808_DCDC_EN_REG,
513 .enable_mask = BIT(0),
514 .owner = THIS_MODULE,
517 .supply_name = "vcc2",
518 .of_match = of_match_ptr("DCDC_REG2"),
519 .regulators_node = of_match_ptr("regulators"),
520 .id = RK808_ID_DCDC2,
521 .ops = &rk808_buck1_2_ops,
522 .type = REGULATOR_VOLTAGE,
526 .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
527 .vsel_mask = RK808_BUCK_VSEL_MASK,
528 .enable_reg = RK808_DCDC_EN_REG,
529 .enable_mask = BIT(1),
530 .owner = THIS_MODULE,
533 .supply_name = "vcc3",
534 .of_match = of_match_ptr("DCDC_REG3"),
535 .regulators_node = of_match_ptr("regulators"),
536 .id = RK808_ID_DCDC3,
537 .ops = &rk808_switch_ops,
538 .type = REGULATOR_VOLTAGE,
540 .enable_reg = RK808_DCDC_EN_REG,
541 .enable_mask = BIT(2),
542 .owner = THIS_MODULE,
544 RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
545 RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
546 RK808_DCDC_EN_REG, BIT(3), 0),
547 RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
548 RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
550 RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
551 RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
555 .supply_name = "vcc7",
556 .of_match = of_match_ptr("LDO_REG3"),
557 .regulators_node = of_match_ptr("regulators"),
559 .ops = &rk808_reg_ops_ranges,
560 .type = REGULATOR_VOLTAGE,
562 .linear_ranges = rk808_ldo3_voltage_ranges,
563 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
564 .vsel_reg = RK808_LDO3_ON_VSEL_REG,
565 .vsel_mask = RK808_BUCK4_VSEL_MASK,
566 .enable_reg = RK808_LDO_EN_REG,
567 .enable_mask = BIT(2),
569 .owner = THIS_MODULE,
571 RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
572 RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
574 RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
575 RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
577 RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
578 RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
580 RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
581 RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
583 RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
584 RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
586 RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
587 RK808_DCDC_EN_REG, BIT(5)),
588 RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
589 RK808_DCDC_EN_REG, BIT(6)),
592 static const struct regulator_desc rk818_reg[] = {
595 .supply_name = "vcc1",
596 .of_match = of_match_ptr("DCDC_REG1"),
597 .regulators_node = of_match_ptr("regulators"),
598 .id = RK818_ID_DCDC1,
599 .ops = &rk808_reg_ops,
600 .type = REGULATOR_VOLTAGE,
604 .vsel_reg = RK818_BUCK1_ON_VSEL_REG,
605 .vsel_mask = RK818_BUCK_VSEL_MASK,
606 .enable_reg = RK818_DCDC_EN_REG,
607 .enable_mask = BIT(0),
608 .owner = THIS_MODULE,
611 .supply_name = "vcc2",
612 .of_match = of_match_ptr("DCDC_REG2"),
613 .regulators_node = of_match_ptr("regulators"),
614 .id = RK818_ID_DCDC2,
615 .ops = &rk808_reg_ops,
616 .type = REGULATOR_VOLTAGE,
620 .vsel_reg = RK818_BUCK2_ON_VSEL_REG,
621 .vsel_mask = RK818_BUCK_VSEL_MASK,
622 .enable_reg = RK818_DCDC_EN_REG,
623 .enable_mask = BIT(1),
624 .owner = THIS_MODULE,
627 .supply_name = "vcc3",
628 .of_match = of_match_ptr("DCDC_REG3"),
629 .regulators_node = of_match_ptr("regulators"),
630 .id = RK818_ID_DCDC3,
631 .ops = &rk808_switch_ops,
632 .type = REGULATOR_VOLTAGE,
634 .enable_reg = RK818_DCDC_EN_REG,
635 .enable_mask = BIT(2),
636 .owner = THIS_MODULE,
638 RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
639 RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
640 RK818_DCDC_EN_REG, BIT(3), 0),
641 RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
642 RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
643 RK818_DCDC_EN_REG, BIT(4), 0),
644 RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
645 RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
647 RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
648 RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
652 .supply_name = "vcc7",
653 .of_match = of_match_ptr("LDO_REG3"),
654 .regulators_node = of_match_ptr("regulators"),
656 .ops = &rk808_reg_ops_ranges,
657 .type = REGULATOR_VOLTAGE,
659 .linear_ranges = rk808_ldo3_voltage_ranges,
660 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
661 .vsel_reg = RK818_LDO3_ON_VSEL_REG,
662 .vsel_mask = RK818_LDO3_ON_VSEL_MASK,
663 .enable_reg = RK818_LDO_EN_REG,
664 .enable_mask = BIT(2),
666 .owner = THIS_MODULE,
668 RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
669 RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
671 RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
672 RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
674 RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
675 RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
677 RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
678 RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
680 RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
681 RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
683 RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
684 RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
685 RK818_DCDC_EN_REG, BIT(5), 400),
686 RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
687 RK818_DCDC_EN_REG, BIT(6)),
688 RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
689 RK818_H5V_EN_REG, BIT(0)),
690 RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
691 RK818_DCDC_EN_REG, BIT(7)),
694 static int rk808_regulator_dt_parse_pdata(struct device *dev,
695 struct device *client_dev,
697 struct rk808_regulator_data *pdata)
699 struct device_node *np;
702 np = of_get_child_by_name(client_dev->of_node, "regulators");
706 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
708 devm_gpiod_get_index_optional(client_dev, "dvs", i,
710 if (IS_ERR(pdata->dvs_gpio[i])) {
711 ret = PTR_ERR(pdata->dvs_gpio[i]);
712 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
716 if (!pdata->dvs_gpio[i]) {
717 dev_warn(dev, "there is no dvs%d gpio\n", i);
721 tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
722 ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
723 gpiod_is_active_low(pdata->dvs_gpio[i]) ?
732 static int rk808_regulator_probe(struct platform_device *pdev)
734 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
735 struct i2c_client *client = rk808->i2c;
736 struct regulator_config config = {};
737 struct regulator_dev *rk808_rdev;
738 struct rk808_regulator_data *pdata;
739 const struct regulator_desc *regulators;
740 int ret, i, nregulators;
742 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
746 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
747 rk808->regmap, pdata);
751 platform_set_drvdata(pdev, pdata);
753 switch (rk808->variant) {
755 regulators = rk805_reg;
756 nregulators = RK805_NUM_REGULATORS;
759 regulators = rk808_reg;
760 nregulators = RK808_NUM_REGULATORS;
763 regulators = rk818_reg;
764 nregulators = RK818_NUM_REGULATORS;
767 dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
772 config.dev = &client->dev;
773 config.driver_data = pdata;
774 config.regmap = rk808->regmap;
776 /* Instantiate the regulators */
777 for (i = 0; i < nregulators; i++) {
778 rk808_rdev = devm_regulator_register(&pdev->dev,
779 ®ulators[i], &config);
780 if (IS_ERR(rk808_rdev)) {
781 dev_err(&client->dev,
782 "failed to register %d regulator\n", i);
783 return PTR_ERR(rk808_rdev);
790 static struct platform_driver rk808_regulator_driver = {
791 .probe = rk808_regulator_probe,
793 .name = "rk808-regulator"
797 module_platform_driver(rk808_regulator_driver);
799 MODULE_DESCRIPTION("regulator driver for the RK808/RK818 series PMICs");
800 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
801 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
802 MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
803 MODULE_LICENSE("GPL");
804 MODULE_ALIAS("platform:rk808-regulator");