2 * Regulator driver for Rockchip RK808
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 * 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.
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
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>
30 /* Field Definitions */
31 #define RK808_BUCK_VSEL_MASK 0x3f
32 #define RK808_BUCK4_VSEL_MASK 0xf
33 #define RK808_LDO_VSEL_MASK 0x1f
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)
43 #define RK808_DVS2_POL BIT(2)
44 #define RK808_DVS1_POL BIT(1)
46 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
47 #define RK808_SLP_REG_OFFSET 1
49 /* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
50 #define RK808_DVS_REG_OFFSET 2
52 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
53 #define RK808_SLP_SET_OFF_REG_OFFSET 2
55 /* max steps for increase voltage of Buck1/2, equal 100mv*/
56 #define MAX_STEPS_ONE_TIME 8
58 struct rk808_regulator_data {
59 struct gpio_desc *dvs_gpio[2];
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,
69 static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
70 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
71 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
74 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
76 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
77 int id = rdev->desc->id - RK808_ID_DCDC1;
78 struct gpio_desc *gpio = pdata->dvs_gpio[id];
82 if (!gpio || gpiod_get_value(gpio) == 0)
83 return regulator_get_voltage_sel_regmap(rdev);
85 ret = regmap_read(rdev->regmap,
86 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
91 val &= rdev->desc->vsel_mask;
92 val >>= ffs(rdev->desc->vsel_mask) - 1;
97 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
101 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
103 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
108 old_sel = val & mask;
109 old_sel >>= ffs(mask) - 1;
110 delta_sel = sel - old_sel;
113 * If directly modify the register to change the voltage, we will face
114 * the risk of overshoot. Put it into a multi-step, can effectively
115 * avoid this problem, a step is 100mv here.
117 while (delta_sel > MAX_STEPS_ONE_TIME) {
118 old_sel += MAX_STEPS_ONE_TIME;
119 val = old_sel << (ffs(mask) - 1);
123 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
124 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
125 * got more than 65 us between each voltage change and thus
126 * won't ramp faster than ~1500 uV / us.
128 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
129 delta_sel = sel - old_sel;
132 sel <<= ffs(mask) - 1;
134 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
137 * When we change the voltage register directly, the ramp rate is about
138 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
139 * so we needn't wait extra time after that.
146 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
149 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
150 int id = rdev->desc->id - RK808_ID_DCDC1;
151 struct gpio_desc *gpio = pdata->dvs_gpio[id];
152 unsigned int reg = rdev->desc->vsel_reg;
157 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
159 gpio_level = gpiod_get_value(gpio);
160 if (gpio_level == 0) {
161 reg += RK808_DVS_REG_OFFSET;
162 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
164 ret = regmap_read(rdev->regmap,
165 reg + RK808_DVS_REG_OFFSET,
172 sel <<= ffs(rdev->desc->vsel_mask) - 1;
173 sel |= old_sel & ~rdev->desc->vsel_mask;
175 ret = regmap_write(rdev->regmap, reg, sel);
179 gpiod_set_value(gpio, !gpio_level);
184 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
185 unsigned int old_selector,
186 unsigned int new_selector)
188 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
189 int id = rdev->desc->id - RK808_ID_DCDC1;
190 struct gpio_desc *gpio = pdata->dvs_gpio[id];
192 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
196 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
199 static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
201 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
202 unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
205 switch (ramp_delay) {
207 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
210 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
213 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
218 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
219 rdev->desc->name, ramp_delay);
222 return regmap_update_bits(rdev->regmap, reg,
223 RK808_RAMP_RATE_MASK, ramp_value);
226 static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
229 int sel = regulator_map_voltage_linear(rdev, uv, uv);
234 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
236 return regmap_update_bits(rdev->regmap, reg,
237 rdev->desc->vsel_mask,
241 static int rk808_set_suspend_enable(struct regulator_dev *rdev)
245 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
247 return regmap_update_bits(rdev->regmap, reg,
248 rdev->desc->enable_mask,
252 static int rk808_set_suspend_disable(struct regulator_dev *rdev)
256 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
258 return regmap_update_bits(rdev->regmap, reg,
259 rdev->desc->enable_mask,
260 rdev->desc->enable_mask);
263 static struct regulator_ops rk808_buck1_2_ops = {
264 .list_voltage = regulator_list_voltage_linear,
265 .map_voltage = regulator_map_voltage_linear,
266 .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap,
267 .set_voltage_sel = rk808_buck1_2_set_voltage_sel,
268 .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel,
269 .enable = regulator_enable_regmap,
270 .disable = regulator_disable_regmap,
271 .is_enabled = regulator_is_enabled_regmap,
272 .set_ramp_delay = rk808_set_ramp_delay,
273 .set_suspend_voltage = rk808_set_suspend_voltage,
274 .set_suspend_enable = rk808_set_suspend_enable,
275 .set_suspend_disable = rk808_set_suspend_disable,
278 static struct regulator_ops rk808_reg_ops = {
279 .list_voltage = regulator_list_voltage_linear,
280 .map_voltage = regulator_map_voltage_linear,
281 .get_voltage_sel = regulator_get_voltage_sel_regmap,
282 .set_voltage_sel = regulator_set_voltage_sel_regmap,
283 .enable = regulator_enable_regmap,
284 .disable = regulator_disable_regmap,
285 .is_enabled = regulator_is_enabled_regmap,
286 .set_suspend_voltage = rk808_set_suspend_voltage,
287 .set_suspend_enable = rk808_set_suspend_enable,
288 .set_suspend_disable = rk808_set_suspend_disable,
291 static struct regulator_ops rk808_switch_ops = {
292 .enable = regulator_enable_regmap,
293 .disable = regulator_disable_regmap,
294 .is_enabled = regulator_is_enabled_regmap,
295 .set_suspend_enable = rk808_set_suspend_enable,
296 .set_suspend_disable = rk808_set_suspend_disable,
299 static const struct regulator_desc rk808_reg[] = {
302 .supply_name = "vcc1",
303 .id = RK808_ID_DCDC1,
304 .ops = &rk808_buck1_2_ops,
305 .type = REGULATOR_VOLTAGE,
309 .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
310 .vsel_mask = RK808_BUCK_VSEL_MASK,
311 .enable_reg = RK808_DCDC_EN_REG,
312 .enable_mask = BIT(0),
313 .owner = THIS_MODULE,
316 .supply_name = "vcc2",
317 .id = RK808_ID_DCDC2,
318 .ops = &rk808_buck1_2_ops,
319 .type = REGULATOR_VOLTAGE,
323 .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
324 .vsel_mask = RK808_BUCK_VSEL_MASK,
325 .enable_reg = RK808_DCDC_EN_REG,
326 .enable_mask = BIT(1),
327 .owner = THIS_MODULE,
330 .supply_name = "vcc3",
331 .id = RK808_ID_DCDC3,
332 .ops = &rk808_switch_ops,
333 .type = REGULATOR_VOLTAGE,
335 .enable_reg = RK808_DCDC_EN_REG,
336 .enable_mask = BIT(2),
337 .owner = THIS_MODULE,
340 .supply_name = "vcc4",
341 .id = RK808_ID_DCDC4,
342 .ops = &rk808_reg_ops,
343 .type = REGULATOR_VOLTAGE,
347 .vsel_reg = RK808_BUCK4_ON_VSEL_REG,
348 .vsel_mask = RK808_BUCK4_VSEL_MASK,
349 .enable_reg = RK808_DCDC_EN_REG,
350 .enable_mask = BIT(3),
351 .owner = THIS_MODULE,
354 .supply_name = "vcc6",
356 .ops = &rk808_reg_ops,
357 .type = REGULATOR_VOLTAGE,
361 .vsel_reg = RK808_LDO1_ON_VSEL_REG,
362 .vsel_mask = RK808_LDO_VSEL_MASK,
363 .enable_reg = RK808_LDO_EN_REG,
364 .enable_mask = BIT(0),
366 .owner = THIS_MODULE,
369 .supply_name = "vcc6",
371 .ops = &rk808_reg_ops,
372 .type = REGULATOR_VOLTAGE,
376 .vsel_reg = RK808_LDO2_ON_VSEL_REG,
377 .vsel_mask = RK808_LDO_VSEL_MASK,
378 .enable_reg = RK808_LDO_EN_REG,
379 .enable_mask = BIT(1),
381 .owner = THIS_MODULE,
384 .supply_name = "vcc7",
386 .ops = &rk808_reg_ops,
387 .type = REGULATOR_VOLTAGE,
389 .linear_ranges = rk808_ldo3_voltage_ranges,
390 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
391 .vsel_reg = RK808_LDO3_ON_VSEL_REG,
392 .vsel_mask = RK808_BUCK4_VSEL_MASK,
393 .enable_reg = RK808_LDO_EN_REG,
394 .enable_mask = BIT(2),
396 .owner = THIS_MODULE,
399 .supply_name = "vcc9",
401 .ops = &rk808_reg_ops,
402 .type = REGULATOR_VOLTAGE,
406 .vsel_reg = RK808_LDO4_ON_VSEL_REG,
407 .vsel_mask = RK808_LDO_VSEL_MASK,
408 .enable_reg = RK808_LDO_EN_REG,
409 .enable_mask = BIT(3),
411 .owner = THIS_MODULE,
414 .supply_name = "vcc9",
416 .ops = &rk808_reg_ops,
417 .type = REGULATOR_VOLTAGE,
421 .vsel_reg = RK808_LDO5_ON_VSEL_REG,
422 .vsel_mask = RK808_LDO_VSEL_MASK,
423 .enable_reg = RK808_LDO_EN_REG,
424 .enable_mask = BIT(4),
426 .owner = THIS_MODULE,
429 .supply_name = "vcc10",
431 .ops = &rk808_reg_ops,
432 .type = REGULATOR_VOLTAGE,
436 .vsel_reg = RK808_LDO6_ON_VSEL_REG,
437 .vsel_mask = RK808_LDO_VSEL_MASK,
438 .enable_reg = RK808_LDO_EN_REG,
439 .enable_mask = BIT(5),
441 .owner = THIS_MODULE,
444 .supply_name = "vcc7",
446 .ops = &rk808_reg_ops,
447 .type = REGULATOR_VOLTAGE,
451 .vsel_reg = RK808_LDO7_ON_VSEL_REG,
452 .vsel_mask = RK808_LDO_VSEL_MASK,
453 .enable_reg = RK808_LDO_EN_REG,
454 .enable_mask = BIT(6),
456 .owner = THIS_MODULE,
459 .supply_name = "vcc11",
461 .ops = &rk808_reg_ops,
462 .type = REGULATOR_VOLTAGE,
466 .vsel_reg = RK808_LDO8_ON_VSEL_REG,
467 .vsel_mask = RK808_LDO_VSEL_MASK,
468 .enable_reg = RK808_LDO_EN_REG,
469 .enable_mask = BIT(7),
471 .owner = THIS_MODULE,
473 .name = "SWITCH_REG1",
474 .supply_name = "vcc8",
475 .id = RK808_ID_SWITCH1,
476 .ops = &rk808_switch_ops,
477 .type = REGULATOR_VOLTAGE,
478 .enable_reg = RK808_DCDC_EN_REG,
479 .enable_mask = BIT(5),
480 .owner = THIS_MODULE,
482 .name = "SWITCH_REG2",
483 .supply_name = "vcc12",
484 .id = RK808_ID_SWITCH2,
485 .ops = &rk808_switch_ops,
486 .type = REGULATOR_VOLTAGE,
487 .enable_reg = RK808_DCDC_EN_REG,
488 .enable_mask = BIT(6),
489 .owner = THIS_MODULE,
493 static struct of_regulator_match rk808_reg_matches[] = {
494 [RK808_ID_DCDC1] = { .name = "DCDC_REG1" },
495 [RK808_ID_DCDC2] = { .name = "DCDC_REG2" },
496 [RK808_ID_DCDC3] = { .name = "DCDC_REG3" },
497 [RK808_ID_DCDC4] = { .name = "DCDC_REG4" },
498 [RK808_ID_LDO1] = { .name = "LDO_REG1" },
499 [RK808_ID_LDO2] = { .name = "LDO_REG2" },
500 [RK808_ID_LDO3] = { .name = "LDO_REG3" },
501 [RK808_ID_LDO4] = { .name = "LDO_REG4" },
502 [RK808_ID_LDO5] = { .name = "LDO_REG5" },
503 [RK808_ID_LDO6] = { .name = "LDO_REG6" },
504 [RK808_ID_LDO7] = { .name = "LDO_REG7" },
505 [RK808_ID_LDO8] = { .name = "LDO_REG8" },
506 [RK808_ID_SWITCH1] = { .name = "SWITCH_REG1" },
507 [RK808_ID_SWITCH2] = { .name = "SWITCH_REG2" },
510 static int rk808_regulator_dt_parse_pdata(struct device *dev,
511 struct device *client_dev,
513 struct rk808_regulator_data *pdata)
515 struct device_node *np;
518 np = of_get_child_by_name(client_dev->of_node, "regulators");
522 ret = of_regulator_match(dev, np, rk808_reg_matches,
523 RK808_NUM_REGULATORS);
527 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
529 devm_gpiod_get_index_optional(client_dev, "dvs", i,
531 if (IS_ERR(pdata->dvs_gpio[i])) {
532 ret = PTR_ERR(pdata->dvs_gpio[i]);
533 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
537 if (!pdata->dvs_gpio[i]) {
538 dev_warn(dev, "there is no dvs%d gpio\n", i);
542 tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
543 ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
544 gpiod_is_active_low(pdata->dvs_gpio[i]) ?
553 static int rk808_regulator_probe(struct platform_device *pdev)
555 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
556 struct i2c_client *client = rk808->i2c;
557 struct regulator_config config = {};
558 struct regulator_dev *rk808_rdev;
559 struct rk808_regulator_data *pdata;
562 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
566 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
567 rk808->regmap, pdata);
571 platform_set_drvdata(pdev, pdata);
573 /* Instantiate the regulators */
574 for (i = 0; i < RK808_NUM_REGULATORS; i++) {
575 if (!rk808_reg_matches[i].init_data ||
576 !rk808_reg_matches[i].of_node)
579 config.dev = &client->dev;
580 config.driver_data = pdata;
581 config.regmap = rk808->regmap;
582 config.of_node = rk808_reg_matches[i].of_node;
583 config.init_data = rk808_reg_matches[i].init_data;
585 rk808_rdev = devm_regulator_register(&pdev->dev,
586 &rk808_reg[i], &config);
587 if (IS_ERR(rk808_rdev)) {
588 dev_err(&client->dev,
589 "failed to register %d regulator\n", i);
590 return PTR_ERR(rk808_rdev);
597 static struct platform_driver rk808_regulator_driver = {
598 .probe = rk808_regulator_probe,
600 .name = "rk808-regulator",
601 .owner = THIS_MODULE,
605 module_platform_driver(rk808_regulator_driver);
607 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
608 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
609 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
610 MODULE_LICENSE("GPL");
611 MODULE_ALIAS("platform:rk808-regulator");