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_buck_voltage_ranges
[] = {
70 REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
73 static const struct regulator_linear_range rk808_buck4_voltage_ranges
[] = {
74 REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
77 static const struct regulator_linear_range rk808_ldo_voltage_ranges
[] = {
78 REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
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),
86 static const struct regulator_linear_range rk808_ldo6_voltage_ranges
[] = {
87 REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
90 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
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
];
98 if (!gpio
|| gpiod_get_value(gpio
) == 0)
99 return regulator_get_voltage_sel_regmap(rdev
);
101 ret
= regmap_read(rdev
->regmap
,
102 rdev
->desc
->vsel_reg
+ RK808_DVS_REG_OFFSET
,
107 val
&= rdev
->desc
->vsel_mask
;
108 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
113 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev
*rdev
,
117 unsigned int old_sel
, tmp
, val
, mask
= rdev
->desc
->vsel_mask
;
119 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &val
);
124 old_sel
= val
& mask
;
125 old_sel
>>= ffs(mask
) - 1;
126 delta_sel
= sel
- old_sel
;
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.
133 while (delta_sel
> MAX_STEPS_ONE_TIME
) {
134 old_sel
+= MAX_STEPS_ONE_TIME
;
135 val
= old_sel
<< (ffs(mask
) - 1);
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.
144 ret
= regmap_write(rdev
->regmap
, rdev
->desc
->vsel_reg
, val
);
145 delta_sel
= sel
- old_sel
;
148 sel
<<= ffs(mask
) - 1;
150 ret
= regmap_write(rdev
->regmap
, rdev
->desc
->vsel_reg
, val
);
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.
162 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev
*rdev
,
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
;
173 return rk808_buck1_2_i2c_set_voltage_sel(rdev
, sel
);
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
);
180 ret
= regmap_read(rdev
->regmap
,
181 reg
+ RK808_DVS_REG_OFFSET
,
188 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
189 sel
|= old_sel
& ~rdev
->desc
->vsel_mask
;
191 ret
= regmap_write(rdev
->regmap
, reg
, sel
);
195 gpiod_set_value(gpio
, !gpio_level
);
200 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev
*rdev
,
201 unsigned int old_selector
,
202 unsigned int new_selector
)
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
];
208 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
212 return regulator_set_voltage_time_sel(rdev
, old_selector
, new_selector
);
215 static int rk808_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
217 unsigned int ramp_value
= RK808_RAMP_RATE_10MV_PER_US
;
218 unsigned int reg
= rk808_buck_config_regs
[rdev
->desc
->id
-
221 switch (ramp_delay
) {
223 ramp_value
= RK808_RAMP_RATE_2MV_PER_US
;
226 ramp_value
= RK808_RAMP_RATE_4MV_PER_US
;
229 ramp_value
= RK808_RAMP_RATE_6MV_PER_US
;
234 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
235 rdev
->desc
->name
, ramp_delay
);
238 return regmap_update_bits(rdev
->regmap
, reg
,
239 RK808_RAMP_RATE_MASK
, ramp_value
);
242 static int rk808_set_suspend_voltage(struct regulator_dev
*rdev
, int uv
)
245 int sel
= regulator_map_voltage_linear_range(rdev
, uv
, uv
);
250 reg
= rdev
->desc
->vsel_reg
+ RK808_SLP_REG_OFFSET
;
252 return regmap_update_bits(rdev
->regmap
, reg
,
253 rdev
->desc
->vsel_mask
,
257 static int rk808_set_suspend_enable(struct regulator_dev
*rdev
)
261 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
263 return regmap_update_bits(rdev
->regmap
, reg
,
264 rdev
->desc
->enable_mask
,
268 static int rk808_set_suspend_disable(struct regulator_dev
*rdev
)
272 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
274 return regmap_update_bits(rdev
->regmap
, reg
,
275 rdev
->desc
->enable_mask
,
276 rdev
->desc
->enable_mask
);
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
,
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
,
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
,
315 static const struct regulator_desc rk808_reg
[] = {
318 .supply_name
= "vcc1",
319 .id
= RK808_ID_DCDC1
,
320 .ops
= &rk808_buck1_2_ops
,
321 .type
= REGULATOR_VOLTAGE
,
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
,
332 .supply_name
= "vcc2",
333 .id
= RK808_ID_DCDC2
,
334 .ops
= &rk808_buck1_2_ops
,
335 .type
= REGULATOR_VOLTAGE
,
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
,
346 .supply_name
= "vcc3",
347 .id
= RK808_ID_DCDC3
,
348 .ops
= &rk808_switch_ops
,
349 .type
= REGULATOR_VOLTAGE
,
351 .enable_reg
= RK808_DCDC_EN_REG
,
352 .enable_mask
= BIT(2),
353 .owner
= THIS_MODULE
,
356 .supply_name
= "vcc4",
357 .id
= RK808_ID_DCDC4
,
358 .ops
= &rk808_reg_ops
,
359 .type
= REGULATOR_VOLTAGE
,
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
,
370 .supply_name
= "vcc6",
372 .ops
= &rk808_reg_ops
,
373 .type
= REGULATOR_VOLTAGE
,
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),
382 .owner
= THIS_MODULE
,
385 .supply_name
= "vcc6",
387 .ops
= &rk808_reg_ops
,
388 .type
= REGULATOR_VOLTAGE
,
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),
397 .owner
= THIS_MODULE
,
400 .supply_name
= "vcc7",
402 .ops
= &rk808_reg_ops
,
403 .type
= REGULATOR_VOLTAGE
,
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),
412 .owner
= THIS_MODULE
,
415 .supply_name
= "vcc9",
417 .ops
= &rk808_reg_ops
,
418 .type
= REGULATOR_VOLTAGE
,
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),
427 .owner
= THIS_MODULE
,
430 .supply_name
= "vcc9",
432 .ops
= &rk808_reg_ops
,
433 .type
= REGULATOR_VOLTAGE
,
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),
442 .owner
= THIS_MODULE
,
445 .supply_name
= "vcc10",
447 .ops
= &rk808_reg_ops
,
448 .type
= REGULATOR_VOLTAGE
,
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),
457 .owner
= THIS_MODULE
,
460 .supply_name
= "vcc7",
462 .ops
= &rk808_reg_ops
,
463 .type
= REGULATOR_VOLTAGE
,
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),
472 .owner
= THIS_MODULE
,
475 .supply_name
= "vcc11",
477 .ops
= &rk808_reg_ops
,
478 .type
= REGULATOR_VOLTAGE
,
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),
487 .owner
= THIS_MODULE
,
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
,
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
,
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" },
526 static int rk808_regulator_dt_parse_pdata(struct device
*dev
,
527 struct device
*client_dev
,
529 struct rk808_regulator_data
*pdata
)
531 struct device_node
*np
;
534 np
= of_get_child_by_name(client_dev
->of_node
, "regulators");
538 ret
= of_regulator_match(dev
, np
, rk808_reg_matches
,
539 RK808_NUM_REGULATORS
);
543 for (i
= 0; i
< ARRAY_SIZE(pdata
->dvs_gpio
); i
++) {
545 devm_gpiod_get_index_optional(client_dev
, "dvs", i
,
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
);
553 if (!pdata
->dvs_gpio
[i
]) {
554 dev_warn(dev
, "there is no dvs%d gpio\n", i
);
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
]) ?
569 static int rk808_regulator_probe(struct platform_device
*pdev
)
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
;
578 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
582 ret
= rk808_regulator_dt_parse_pdata(&pdev
->dev
, &client
->dev
,
583 rk808
->regmap
, pdata
);
587 platform_set_drvdata(pdev
, pdata
);
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
)
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
;
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
);
613 static struct platform_driver rk808_regulator_driver
= {
614 .probe
= rk808_regulator_probe
,
616 .name
= "rk808-regulator",
617 .owner
= THIS_MODULE
,
621 module_platform_driver(rk808_regulator_driver
);
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");