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/module.h>
20 #include <linux/i2c.h>
21 #include <linux/mfd/rk808.h>
22 #include <linux/of_device.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/of_regulator.h>
26 /* Field Definitions */
27 #define RK808_BUCK_VSEL_MASK 0x3f
28 #define RK808_BUCK4_VSEL_MASK 0xf
29 #define RK808_LDO_VSEL_MASK 0x1f
31 /* Ramp rate definitions for buck1 / buck2 only */
32 #define RK808_RAMP_RATE_OFFSET 3
33 #define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET)
34 #define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET)
35 #define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET)
36 #define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET)
37 #define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET)
39 /* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
40 #define RK808_SLP_REG_OFFSET 1
42 /* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
43 #define RK808_SLP_SET_OFF_REG_OFFSET 2
45 static const int rk808_buck_config_regs
[] = {
46 RK808_BUCK1_CONFIG_REG
,
47 RK808_BUCK2_CONFIG_REG
,
48 RK808_BUCK3_CONFIG_REG
,
49 RK808_BUCK4_CONFIG_REG
,
52 static const struct regulator_linear_range rk808_buck_voltage_ranges
[] = {
53 REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
56 static const struct regulator_linear_range rk808_buck4_voltage_ranges
[] = {
57 REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
60 static const struct regulator_linear_range rk808_ldo_voltage_ranges
[] = {
61 REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
64 static const struct regulator_linear_range rk808_ldo3_voltage_ranges
[] = {
65 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
66 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
69 static const struct regulator_linear_range rk808_ldo6_voltage_ranges
[] = {
70 REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
73 static int rk808_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
75 unsigned int ramp_value
= RK808_RAMP_RATE_10MV_PER_US
;
76 unsigned int reg
= rk808_buck_config_regs
[rdev
->desc
->id
-
81 ramp_value
= RK808_RAMP_RATE_2MV_PER_US
;
84 ramp_value
= RK808_RAMP_RATE_4MV_PER_US
;
87 ramp_value
= RK808_RAMP_RATE_6MV_PER_US
;
92 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
93 rdev
->desc
->name
, ramp_delay
);
96 return regmap_update_bits(rdev
->regmap
, reg
,
97 RK808_RAMP_RATE_MASK
, ramp_value
);
100 static int rk808_set_suspend_voltage(struct regulator_dev
*rdev
, int uv
)
103 int sel
= regulator_map_voltage_linear_range(rdev
, uv
, uv
);
108 reg
= rdev
->desc
->vsel_reg
+ RK808_SLP_REG_OFFSET
;
110 return regmap_update_bits(rdev
->regmap
, reg
,
111 rdev
->desc
->vsel_mask
,
115 static int rk808_set_suspend_enable(struct regulator_dev
*rdev
)
119 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
121 return regmap_update_bits(rdev
->regmap
, reg
,
122 rdev
->desc
->enable_mask
,
126 static int rk808_set_suspend_disable(struct regulator_dev
*rdev
)
130 reg
= rdev
->desc
->enable_reg
+ RK808_SLP_SET_OFF_REG_OFFSET
;
132 return regmap_update_bits(rdev
->regmap
, reg
,
133 rdev
->desc
->enable_mask
,
134 rdev
->desc
->enable_mask
);
137 static struct regulator_ops rk808_buck1_2_ops
= {
138 .list_voltage
= regulator_list_voltage_linear_range
,
139 .map_voltage
= regulator_map_voltage_linear_range
,
140 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
141 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
142 .enable
= regulator_enable_regmap
,
143 .disable
= regulator_disable_regmap
,
144 .is_enabled
= regulator_is_enabled_regmap
,
145 .set_ramp_delay
= rk808_set_ramp_delay
,
146 .set_suspend_voltage
= rk808_set_suspend_voltage
,
147 .set_suspend_enable
= rk808_set_suspend_enable
,
148 .set_suspend_disable
= rk808_set_suspend_disable
,
151 static struct regulator_ops rk808_reg_ops
= {
152 .list_voltage
= regulator_list_voltage_linear_range
,
153 .map_voltage
= regulator_map_voltage_linear_range
,
154 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
155 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
156 .enable
= regulator_enable_regmap
,
157 .disable
= regulator_disable_regmap
,
158 .is_enabled
= regulator_is_enabled_regmap
,
159 .set_suspend_voltage
= rk808_set_suspend_voltage
,
160 .set_suspend_enable
= rk808_set_suspend_enable
,
161 .set_suspend_disable
= rk808_set_suspend_disable
,
164 static struct regulator_ops rk808_switch_ops
= {
165 .enable
= regulator_enable_regmap
,
166 .disable
= regulator_disable_regmap
,
167 .is_enabled
= regulator_is_enabled_regmap
,
168 .set_suspend_enable
= rk808_set_suspend_enable
,
169 .set_suspend_disable
= rk808_set_suspend_disable
,
172 static const struct regulator_desc rk808_reg
[] = {
175 .supply_name
= "vcc1",
176 .id
= RK808_ID_DCDC1
,
177 .ops
= &rk808_buck1_2_ops
,
178 .type
= REGULATOR_VOLTAGE
,
180 .linear_ranges
= rk808_buck_voltage_ranges
,
181 .n_linear_ranges
= ARRAY_SIZE(rk808_buck_voltage_ranges
),
182 .vsel_reg
= RK808_BUCK1_ON_VSEL_REG
,
183 .vsel_mask
= RK808_BUCK_VSEL_MASK
,
184 .enable_reg
= RK808_DCDC_EN_REG
,
185 .enable_mask
= BIT(0),
186 .owner
= THIS_MODULE
,
189 .supply_name
= "vcc2",
190 .id
= RK808_ID_DCDC2
,
191 .ops
= &rk808_buck1_2_ops
,
192 .type
= REGULATOR_VOLTAGE
,
194 .linear_ranges
= rk808_buck_voltage_ranges
,
195 .n_linear_ranges
= ARRAY_SIZE(rk808_buck_voltage_ranges
),
196 .vsel_reg
= RK808_BUCK2_ON_VSEL_REG
,
197 .vsel_mask
= RK808_BUCK_VSEL_MASK
,
198 .enable_reg
= RK808_DCDC_EN_REG
,
199 .enable_mask
= BIT(1),
200 .owner
= THIS_MODULE
,
203 .supply_name
= "vcc3",
204 .id
= RK808_ID_DCDC3
,
205 .ops
= &rk808_switch_ops
,
206 .type
= REGULATOR_VOLTAGE
,
208 .enable_reg
= RK808_DCDC_EN_REG
,
209 .enable_mask
= BIT(2),
210 .owner
= THIS_MODULE
,
213 .supply_name
= "vcc4",
214 .id
= RK808_ID_DCDC4
,
215 .ops
= &rk808_reg_ops
,
216 .type
= REGULATOR_VOLTAGE
,
218 .linear_ranges
= rk808_buck4_voltage_ranges
,
219 .n_linear_ranges
= ARRAY_SIZE(rk808_buck4_voltage_ranges
),
220 .vsel_reg
= RK808_BUCK4_ON_VSEL_REG
,
221 .vsel_mask
= RK808_BUCK4_VSEL_MASK
,
222 .enable_reg
= RK808_DCDC_EN_REG
,
223 .enable_mask
= BIT(3),
224 .owner
= THIS_MODULE
,
227 .supply_name
= "vcc6",
229 .ops
= &rk808_reg_ops
,
230 .type
= REGULATOR_VOLTAGE
,
232 .linear_ranges
= rk808_ldo_voltage_ranges
,
233 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo_voltage_ranges
),
234 .vsel_reg
= RK808_LDO1_ON_VSEL_REG
,
235 .vsel_mask
= RK808_LDO_VSEL_MASK
,
236 .enable_reg
= RK808_LDO_EN_REG
,
237 .enable_mask
= BIT(0),
239 .owner
= THIS_MODULE
,
242 .supply_name
= "vcc6",
244 .ops
= &rk808_reg_ops
,
245 .type
= REGULATOR_VOLTAGE
,
247 .linear_ranges
= rk808_ldo_voltage_ranges
,
248 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo_voltage_ranges
),
249 .vsel_reg
= RK808_LDO2_ON_VSEL_REG
,
250 .vsel_mask
= RK808_LDO_VSEL_MASK
,
251 .enable_reg
= RK808_LDO_EN_REG
,
252 .enable_mask
= BIT(1),
254 .owner
= THIS_MODULE
,
257 .supply_name
= "vcc7",
259 .ops
= &rk808_reg_ops
,
260 .type
= REGULATOR_VOLTAGE
,
262 .linear_ranges
= rk808_ldo3_voltage_ranges
,
263 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo3_voltage_ranges
),
264 .vsel_reg
= RK808_LDO3_ON_VSEL_REG
,
265 .vsel_mask
= RK808_BUCK4_VSEL_MASK
,
266 .enable_reg
= RK808_LDO_EN_REG
,
267 .enable_mask
= BIT(2),
269 .owner
= THIS_MODULE
,
272 .supply_name
= "vcc9",
274 .ops
= &rk808_reg_ops
,
275 .type
= REGULATOR_VOLTAGE
,
277 .linear_ranges
= rk808_ldo_voltage_ranges
,
278 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo_voltage_ranges
),
279 .vsel_reg
= RK808_LDO4_ON_VSEL_REG
,
280 .vsel_mask
= RK808_LDO_VSEL_MASK
,
281 .enable_reg
= RK808_LDO_EN_REG
,
282 .enable_mask
= BIT(3),
284 .owner
= THIS_MODULE
,
287 .supply_name
= "vcc9",
289 .ops
= &rk808_reg_ops
,
290 .type
= REGULATOR_VOLTAGE
,
292 .linear_ranges
= rk808_ldo_voltage_ranges
,
293 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo_voltage_ranges
),
294 .vsel_reg
= RK808_LDO5_ON_VSEL_REG
,
295 .vsel_mask
= RK808_LDO_VSEL_MASK
,
296 .enable_reg
= RK808_LDO_EN_REG
,
297 .enable_mask
= BIT(4),
299 .owner
= THIS_MODULE
,
302 .supply_name
= "vcc10",
304 .ops
= &rk808_reg_ops
,
305 .type
= REGULATOR_VOLTAGE
,
307 .linear_ranges
= rk808_ldo6_voltage_ranges
,
308 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo6_voltage_ranges
),
309 .vsel_reg
= RK808_LDO6_ON_VSEL_REG
,
310 .vsel_mask
= RK808_LDO_VSEL_MASK
,
311 .enable_reg
= RK808_LDO_EN_REG
,
312 .enable_mask
= BIT(5),
314 .owner
= THIS_MODULE
,
317 .supply_name
= "vcc7",
319 .ops
= &rk808_reg_ops
,
320 .type
= REGULATOR_VOLTAGE
,
322 .linear_ranges
= rk808_ldo6_voltage_ranges
,
323 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo6_voltage_ranges
),
324 .vsel_reg
= RK808_LDO7_ON_VSEL_REG
,
325 .vsel_mask
= RK808_LDO_VSEL_MASK
,
326 .enable_reg
= RK808_LDO_EN_REG
,
327 .enable_mask
= BIT(6),
329 .owner
= THIS_MODULE
,
332 .supply_name
= "vcc11",
334 .ops
= &rk808_reg_ops
,
335 .type
= REGULATOR_VOLTAGE
,
337 .linear_ranges
= rk808_ldo_voltage_ranges
,
338 .n_linear_ranges
= ARRAY_SIZE(rk808_ldo_voltage_ranges
),
339 .vsel_reg
= RK808_LDO8_ON_VSEL_REG
,
340 .vsel_mask
= RK808_LDO_VSEL_MASK
,
341 .enable_reg
= RK808_LDO_EN_REG
,
342 .enable_mask
= BIT(7),
344 .owner
= THIS_MODULE
,
346 .name
= "SWITCH_REG1",
347 .supply_name
= "vcc8",
348 .id
= RK808_ID_SWITCH1
,
349 .ops
= &rk808_switch_ops
,
350 .type
= REGULATOR_VOLTAGE
,
351 .enable_reg
= RK808_DCDC_EN_REG
,
352 .enable_mask
= BIT(5),
353 .owner
= THIS_MODULE
,
355 .name
= "SWITCH_REG2",
356 .supply_name
= "vcc12",
357 .id
= RK808_ID_SWITCH2
,
358 .ops
= &rk808_switch_ops
,
359 .type
= REGULATOR_VOLTAGE
,
360 .enable_reg
= RK808_DCDC_EN_REG
,
361 .enable_mask
= BIT(6),
362 .owner
= THIS_MODULE
,
366 static struct of_regulator_match rk808_reg_matches
[] = {
367 [RK808_ID_DCDC1
] = { .name
= "DCDC_REG1" },
368 [RK808_ID_DCDC2
] = { .name
= "DCDC_REG2" },
369 [RK808_ID_DCDC3
] = { .name
= "DCDC_REG3" },
370 [RK808_ID_DCDC4
] = { .name
= "DCDC_REG4" },
371 [RK808_ID_LDO1
] = { .name
= "LDO_REG1" },
372 [RK808_ID_LDO2
] = { .name
= "LDO_REG2" },
373 [RK808_ID_LDO3
] = { .name
= "LDO_REG3" },
374 [RK808_ID_LDO4
] = { .name
= "LDO_REG4" },
375 [RK808_ID_LDO5
] = { .name
= "LDO_REG5" },
376 [RK808_ID_LDO6
] = { .name
= "LDO_REG6" },
377 [RK808_ID_LDO7
] = { .name
= "LDO_REG7" },
378 [RK808_ID_LDO8
] = { .name
= "LDO_REG8" },
379 [RK808_ID_SWITCH1
] = { .name
= "SWITCH_REG1" },
380 [RK808_ID_SWITCH2
] = { .name
= "SWITCH_REG2" },
383 static int rk808_regulator_probe(struct platform_device
*pdev
)
385 struct rk808
*rk808
= dev_get_drvdata(pdev
->dev
.parent
);
386 struct i2c_client
*client
= rk808
->i2c
;
387 struct device_node
*reg_np
;
388 struct regulator_config config
= {};
389 struct regulator_dev
*rk808_rdev
;
392 reg_np
= of_get_child_by_name(client
->dev
.of_node
, "regulators");
396 ret
= of_regulator_match(&pdev
->dev
, reg_np
, rk808_reg_matches
,
397 RK808_NUM_REGULATORS
);
402 /* Instantiate the regulators */
403 for (i
= 0; i
< RK808_NUM_REGULATORS
; i
++) {
404 if (!rk808_reg_matches
[i
].init_data
||
405 !rk808_reg_matches
[i
].of_node
)
408 config
.dev
= &client
->dev
;
409 config
.driver_data
= rk808
;
410 config
.regmap
= rk808
->regmap
;
411 config
.of_node
= rk808_reg_matches
[i
].of_node
;
412 config
.init_data
= rk808_reg_matches
[i
].init_data
;
414 rk808_rdev
= devm_regulator_register(&pdev
->dev
,
415 &rk808_reg
[i
], &config
);
416 if (IS_ERR(rk808_rdev
)) {
417 dev_err(&client
->dev
,
418 "failed to register %d regulator\n", i
);
419 return PTR_ERR(rk808_rdev
);
426 static struct platform_driver rk808_regulator_driver
= {
427 .probe
= rk808_regulator_probe
,
429 .name
= "rk808-regulator",
433 module_platform_driver(rk808_regulator_driver
);
435 MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
436 MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
437 MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
438 MODULE_LICENSE("GPL");
439 MODULE_ALIAS("platform:rk808-regulator");