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 #define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
59 _vmask, _ereg, _emask, _etime) \
62 .supply_name = (_supply), \
63 .of_match = of_match_ptr(_match), \
64 .regulators_node = of_match_ptr("regulators"), \
65 .type = REGULATOR_VOLTAGE, \
67 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
68 .owner = THIS_MODULE, \
69 .min_uV = (_min) * 1000, \
70 .uV_step = (_step) * 1000, \
71 .vsel_reg = (_vreg), \
72 .vsel_mask = (_vmask), \
73 .enable_reg = (_ereg), \
74 .enable_mask = (_emask), \
75 .enable_time = (_etime), \
76 .ops = &rk808_reg_ops, \
79 #define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \
82 .supply_name = (_supply), \
83 .of_match = of_match_ptr(_match), \
84 .regulators_node = of_match_ptr("regulators"), \
85 .type = REGULATOR_VOLTAGE, \
87 .enable_reg = (_ereg), \
88 .enable_mask = (_emask), \
89 .owner = THIS_MODULE, \
90 .ops = &rk808_switch_ops \
94 struct rk808_regulator_data
{
95 struct gpio_desc
*dvs_gpio
[2];
98 static const int rk808_buck_config_regs
[] = {
99 RK808_BUCK1_CONFIG_REG
,
100 RK808_BUCK2_CONFIG_REG
,
101 RK808_BUCK3_CONFIG_REG
,
102 RK808_BUCK4_CONFIG_REG
,
105 static const struct regulator_linear_range rk808_ldo3_voltage_ranges
[] = {
106 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
107 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
110 static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
112 struct rk808_regulator_data
*pdata
= rdev_get_drvdata(rdev
);
113 int id
= rdev
->desc
->id
- RK808_ID_DCDC1
;
114 struct gpio_desc
*gpio
= pdata
->dvs_gpio
[id
];
118 if (!gpio
|| gpiod_get_value(gpio
) == 0)
119 return regulator_get_voltage_sel_regmap(rdev
);
121 ret
= regmap_read(rdev
->regmap
,
122 rdev
->desc
->vsel_reg
+ RK808_DVS_REG_OFFSET
,
127 val
&= rdev
->desc
->vsel_mask
;
128 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
133 static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev
*rdev
,
137 unsigned int old_sel
, tmp
, val
, mask
= rdev
->desc
->vsel_mask
;
139 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &val
);
144 old_sel
= val
& mask
;
145 old_sel
>>= ffs(mask
) - 1;
146 delta_sel
= sel
- old_sel
;
149 * If directly modify the register to change the voltage, we will face
150 * the risk of overshoot. Put it into a multi-step, can effectively
151 * avoid this problem, a step is 100mv here.
153 while (delta_sel
> MAX_STEPS_ONE_TIME
) {
154 old_sel
+= MAX_STEPS_ONE_TIME
;
155 val
= old_sel
<< (ffs(mask
) - 1);
159 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
160 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
161 * got more than 65 us between each voltage change and thus
162 * won't ramp faster than ~1500 uV / us.
164 ret
= regmap_write(rdev
->regmap
, rdev
->desc
->vsel_reg
, val
);
165 delta_sel
= sel
- old_sel
;
168 sel
<<= ffs(mask
) - 1;
170 ret
= regmap_write(rdev
->regmap
, rdev
->desc
->vsel_reg
, val
);
173 * When we change the voltage register directly, the ramp rate is about
174 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
175 * so we needn't wait extra time after that.
182 static int rk808_buck1_2_set_voltage_sel(struct regulator_dev
*rdev
,
185 struct rk808_regulator_data
*pdata
= rdev_get_drvdata(rdev
);
186 int id
= rdev
->desc
->id
- RK808_ID_DCDC1
;
187 struct gpio_desc
*gpio
= pdata
->dvs_gpio
[id
];
188 unsigned int reg
= rdev
->desc
->vsel_reg
;
193 return rk808_buck1_2_i2c_set_voltage_sel(rdev
, sel
);
195 gpio_level
= gpiod_get_value(gpio
);
196 if (gpio_level
== 0) {
197 reg
+= RK808_DVS_REG_OFFSET
;
198 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &old_sel
);
200 ret
= regmap_read(rdev
->regmap
,
201 reg
+ RK808_DVS_REG_OFFSET
,
208 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
209 sel
|= old_sel
& ~rdev
->desc
->vsel_mask
;
211 ret
= regmap_write(rdev
->regmap
, reg
, sel
);
215 gpiod_set_value(gpio
, !gpio_level
);
220 static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev
*rdev
,
221 unsigned int old_selector
,
222 unsigned int new_selector
)
224 struct rk808_regulator_data
*pdata
= rdev_get_drvdata(rdev
);
225 int id
= rdev
->desc
->id
- RK808_ID_DCDC1
;
226 struct gpio_desc
*gpio
= pdata
->dvs_gpio
[id
];
228 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
232 return regulator_set_voltage_time_sel(rdev
, old_selector
, new_selector
);
235 static int rk808_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
237 unsigned int ramp_value
= RK808_RAMP_RATE_10MV_PER_US
;
238 unsigned int reg
= rk808_buck_config_regs
[rdev
->desc
->id
-
241 switch (ramp_delay
) {
243 ramp_value
= RK808_RAMP_RATE_2MV_PER_US
;
246 ramp_value
= RK808_RAMP_RATE_4MV_PER_US
;
249 ramp_value
= RK808_RAMP_RATE_6MV_PER_US
;
254 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
255 rdev
->desc
->name
, ramp_delay
);
258 return regmap_update_bits(rdev
->regmap
, reg
,
259 RK808_RAMP_RATE_MASK
, ramp_value
);
262 static int rk808_set_suspend_voltage(struct regulator_dev
*rdev
, int uv
)
265 int sel
= regulator_map_voltage_linear(rdev
, uv
, uv
);
270 reg
= rdev
->desc
->vsel_reg
+ RK808_SLP_REG_OFFSET
;
272 return regmap_update_bits(rdev
->regmap
, reg
,
273 rdev
->desc
->vsel_mask
,
277 static int rk808_set_suspend_voltage_range(struct regulator_dev
*rdev
, int uv
)
280 int sel
= regulator_map_voltage_linear_range(rdev
, uv
, uv
);
285 reg
= rdev
->desc
->vsel_reg
+ RK808_SLP_REG_OFFSET
;
287 return regmap_update_bits(rdev
->regmap
, reg
,
288 rdev
->desc
->vsel_mask
,
292 static int rk808_set_suspend_enable(struct regulator_dev
*rdev
)
296 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
298 return regmap_update_bits(rdev
->regmap
, reg
,
299 rdev
->desc
->enable_mask
,
303 static int rk808_set_suspend_disable(struct regulator_dev
*rdev
)
307 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
309 return regmap_update_bits(rdev
->regmap
, reg
,
310 rdev
->desc
->enable_mask
,
311 rdev
->desc
->enable_mask
);
314 static struct regulator_ops rk808_buck1_2_ops
= {
315 .list_voltage
= regulator_list_voltage_linear
,
316 .map_voltage
= regulator_map_voltage_linear
,
317 .get_voltage_sel
= rk808_buck1_2_get_voltage_sel_regmap
,
318 .set_voltage_sel
= rk808_buck1_2_set_voltage_sel
,
319 .set_voltage_time_sel
= rk808_buck1_2_set_voltage_time_sel
,
320 .enable
= regulator_enable_regmap
,
321 .disable
= regulator_disable_regmap
,
322 .is_enabled
= regulator_is_enabled_regmap
,
323 .set_ramp_delay
= rk808_set_ramp_delay
,
324 .set_suspend_voltage
= rk808_set_suspend_voltage
,
325 .set_suspend_enable
= rk808_set_suspend_enable
,
326 .set_suspend_disable
= rk808_set_suspend_disable
,
329 static struct regulator_ops rk808_reg_ops
= {
330 .list_voltage
= regulator_list_voltage_linear
,
331 .map_voltage
= regulator_map_voltage_linear
,
332 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
333 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
334 .enable
= regulator_enable_regmap
,
335 .disable
= regulator_disable_regmap
,
336 .is_enabled
= regulator_is_enabled_regmap
,
337 .set_suspend_voltage
= rk808_set_suspend_voltage
,
338 .set_suspend_enable
= rk808_set_suspend_enable
,
339 .set_suspend_disable
= rk808_set_suspend_disable
,
342 static struct regulator_ops rk808_reg_ops_ranges
= {
343 .list_voltage
= regulator_list_voltage_linear_range
,
344 .map_voltage
= regulator_map_voltage_linear_range
,
345 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
346 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
347 .enable
= regulator_enable_regmap
,
348 .disable
= regulator_disable_regmap
,
349 .is_enabled
= regulator_is_enabled_regmap
,
350 .set_suspend_voltage
= rk808_set_suspend_voltage_range
,
351 .set_suspend_enable
= rk808_set_suspend_enable
,
352 .set_suspend_disable
= rk808_set_suspend_disable
,
355 static struct regulator_ops rk808_switch_ops
= {
356 .enable
= regulator_enable_regmap
,
357 .disable
= regulator_disable_regmap
,
358 .is_enabled
= regulator_is_enabled_regmap
,
359 .set_suspend_enable
= rk808_set_suspend_enable
,
360 .set_suspend_disable
= rk808_set_suspend_disable
,
363 static const struct regulator_desc rk808_reg
[] = {
366 .supply_name
= "vcc1",
367 .of_match
= of_match_ptr("DCDC_REG1"),
368 .regulators_node
= of_match_ptr("regulators"),
369 .id
= RK808_ID_DCDC1
,
370 .ops
= &rk808_buck1_2_ops
,
371 .type
= REGULATOR_VOLTAGE
,
375 .vsel_reg
= RK808_BUCK1_ON_VSEL_REG
,
376 .vsel_mask
= RK808_BUCK_VSEL_MASK
,
377 .enable_reg
= RK808_DCDC_EN_REG
,
378 .enable_mask
= BIT(0),
379 .owner
= THIS_MODULE
,
382 .supply_name
= "vcc2",
383 .of_match
= of_match_ptr("DCDC_REG2"),
384 .regulators_node
= of_match_ptr("regulators"),
385 .id
= RK808_ID_DCDC2
,
386 .ops
= &rk808_buck1_2_ops
,
387 .type
= REGULATOR_VOLTAGE
,
391 .vsel_reg
= RK808_BUCK2_ON_VSEL_REG
,
392 .vsel_mask
= RK808_BUCK_VSEL_MASK
,
393 .enable_reg
= RK808_DCDC_EN_REG
,
394 .enable_mask
= BIT(1),
395 .owner
= THIS_MODULE
,
398 .supply_name
= "vcc3",
399 .of_match
= of_match_ptr("DCDC_REG3"),
400 .regulators_node
= of_match_ptr("regulators"),
401 .id
= RK808_ID_DCDC3
,
402 .ops
= &rk808_switch_ops
,
403 .type
= REGULATOR_VOLTAGE
,
405 .enable_reg
= RK808_DCDC_EN_REG
,
406 .enable_mask
= BIT(2),
407 .owner
= THIS_MODULE
,
409 RK8XX_DESC(RK808_ID_DCDC4
, "DCDC_REG4", "vcc4", 1800, 3300, 100,
410 RK808_BUCK4_ON_VSEL_REG
, RK808_BUCK4_VSEL_MASK
,
411 RK808_DCDC_EN_REG
, BIT(3), 0),
412 RK8XX_DESC(RK808_ID_LDO1
, "LDO_REG1", "vcc6", 1800, 3400, 100,
413 RK808_LDO1_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
415 RK8XX_DESC(RK808_ID_LDO2
, "LDO_REG2", "vcc6", 1800, 3400, 100,
416 RK808_LDO2_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
420 .supply_name
= "vcc7",
421 .of_match
= of_match_ptr("LDO_REG3"),
422 .regulators_node
= of_match_ptr("regulators"),
424 .ops
= &rk808_reg_ops_ranges
,
425 .type
= REGULATOR_VOLTAGE
,
427 .linear_ranges
= rk808_ldo3_voltage_ranges
,
428 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo3_voltage_ranges
),
429 .vsel_reg
= RK808_LDO3_ON_VSEL_REG
,
430 .vsel_mask
= RK808_BUCK4_VSEL_MASK
,
431 .enable_reg
= RK808_LDO_EN_REG
,
432 .enable_mask
= BIT(2),
434 .owner
= THIS_MODULE
,
436 RK8XX_DESC(RK808_ID_LDO4
, "LDO_REG4", "vcc9", 1800, 3400, 100,
437 RK808_LDO4_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
439 RK8XX_DESC(RK808_ID_LDO5
, "LDO_REG5", "vcc9", 1800, 3400, 100,
440 RK808_LDO5_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
442 RK8XX_DESC(RK808_ID_LDO6
, "LDO_REG6", "vcc10", 800, 2500, 100,
443 RK808_LDO6_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
445 RK8XX_DESC(RK808_ID_LDO7
, "LDO_REG7", "vcc7", 800, 2500, 100,
446 RK808_LDO7_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
448 RK8XX_DESC(RK808_ID_LDO8
, "LDO_REG8", "vcc11", 1800, 3400, 100,
449 RK808_LDO8_ON_VSEL_REG
, RK808_LDO_VSEL_MASK
, RK808_LDO_EN_REG
,
451 RK8XX_DESC_SWITCH(RK808_ID_SWITCH1
, "SWITCH_REG1", "vcc8",
452 RK808_DCDC_EN_REG
, BIT(5)),
453 RK8XX_DESC_SWITCH(RK808_ID_SWITCH2
, "SWITCH_REG2", "vcc12",
454 RK808_DCDC_EN_REG
, BIT(6)),
457 static int rk808_regulator_dt_parse_pdata(struct device
*dev
,
458 struct device
*client_dev
,
460 struct rk808_regulator_data
*pdata
)
462 struct device_node
*np
;
465 np
= of_get_child_by_name(client_dev
->of_node
, "regulators");
469 for (i
= 0; i
< ARRAY_SIZE(pdata
->dvs_gpio
); i
++) {
471 devm_gpiod_get_index_optional(client_dev
, "dvs", i
,
473 if (IS_ERR(pdata
->dvs_gpio
[i
])) {
474 ret
= PTR_ERR(pdata
->dvs_gpio
[i
]);
475 dev_err(dev
, "failed to get dvs%d gpio (%d)\n", i
, ret
);
479 if (!pdata
->dvs_gpio
[i
]) {
480 dev_warn(dev
, "there is no dvs%d gpio\n", i
);
484 tmp
= i
? RK808_DVS2_POL
: RK808_DVS1_POL
;
485 ret
= regmap_update_bits(map
, RK808_IO_POL_REG
, tmp
,
486 gpiod_is_active_low(pdata
->dvs_gpio
[i
]) ?
495 static int rk808_regulator_probe(struct platform_device
*pdev
)
497 struct rk808
*rk808
= dev_get_drvdata(pdev
->dev
.parent
);
498 struct i2c_client
*client
= rk808
->i2c
;
499 struct regulator_config config
= {};
500 struct regulator_dev
*rk808_rdev
;
501 struct rk808_regulator_data
*pdata
;
504 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
508 ret
= rk808_regulator_dt_parse_pdata(&pdev
->dev
, &client
->dev
,
509 rk808
->regmap
, pdata
);
513 platform_set_drvdata(pdev
, pdata
);
515 config
.dev
= &client
->dev
;
516 config
.driver_data
= pdata
;
517 config
.regmap
= rk808
->regmap
;
519 /* Instantiate the regulators */
520 for (i
= 0; i
< RK808_NUM_REGULATORS
; i
++) {
521 rk808_rdev
= devm_regulator_register(&pdev
->dev
,
522 &rk808_reg
[i
], &config
);
523 if (IS_ERR(rk808_rdev
)) {
524 dev_err(&client
->dev
,
525 "failed to register %d regulator\n", i
);
526 return PTR_ERR(rk808_rdev
);
533 static struct platform_driver rk808_regulator_driver
= {
534 .probe
= rk808_regulator_probe
,
536 .name
= "rk808-regulator",
537 .owner
= THIS_MODULE
,
541 module_platform_driver(rk808_regulator_driver
);
543 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
544 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
545 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
546 MODULE_LICENSE("GPL");
547 MODULE_ALIAS("platform:rk808-regulator");