blob: 196a5c8838c4cae8f63fb4f894dfaae9b00836d3 [file] [log] [blame]
Chris Zhong2cd64ae2014-08-20 11:36:42 +08001/*
2 * Regulator driver for Rockchip RK808
3 *
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5 *
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
8 *
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.
12 *
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
16 * more details.
Chris Zhong2cd64ae2014-08-20 11:36:42 +080017 */
18
19#include <linux/module.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080020#include <linux/i2c.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080021#include <linux/mfd/rk808.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080022#include <linux/of_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/of_regulator.h>
Chris Zhong571a4012014-09-10 09:18:06 +080025
26/* Field Definitions */
Chris Zhong2cd64ae2014-08-20 11:36:42 +080027#define RK808_BUCK_VSEL_MASK 0x3f
28#define RK808_BUCK4_VSEL_MASK 0xf
29#define RK808_LDO_VSEL_MASK 0x1f
30
Doug Anderson8af25222014-09-16 10:22:54 -070031/* 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)
38
39static const int rk808_buck_config_regs[] = {
40 RK808_BUCK1_CONFIG_REG,
41 RK808_BUCK2_CONFIG_REG,
42 RK808_BUCK3_CONFIG_REG,
43 RK808_BUCK4_CONFIG_REG,
44};
45
Chris Zhong2cd64ae2014-08-20 11:36:42 +080046static const struct regulator_linear_range rk808_buck_voltage_ranges[] = {
Chris Zhong1575e282014-09-30 17:44:54 +080047 REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
Chris Zhong2cd64ae2014-08-20 11:36:42 +080048};
49
50static const struct regulator_linear_range rk808_buck4_voltage_ranges[] = {
51 REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
52};
53
54static const struct regulator_linear_range rk808_ldo_voltage_ranges[] = {
55 REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
56};
57
58static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
59 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
60 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
61};
62
63static const struct regulator_linear_range rk808_ldo6_voltage_ranges[] = {
64 REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
65};
66
Doug Anderson8af25222014-09-16 10:22:54 -070067static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
68{
69 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
70 unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
71 RK808_ID_DCDC1];
72
73 switch (ramp_delay) {
74 case 1 ... 2000:
75 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
76 break;
77 case 2001 ... 4000:
78 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
79 break;
80 case 4001 ... 6000:
81 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
82 break;
83 case 6001 ... 10000:
84 break;
85 default:
86 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
87 rdev->desc->name, ramp_delay);
88 }
89
90 return regmap_update_bits(rdev->regmap, reg,
91 RK808_RAMP_RATE_MASK, ramp_value);
92}
93
94static struct regulator_ops rk808_buck1_2_ops = {
95 .list_voltage = regulator_list_voltage_linear_range,
96 .map_voltage = regulator_map_voltage_linear_range,
97 .get_voltage_sel = regulator_get_voltage_sel_regmap,
98 .set_voltage_sel = regulator_set_voltage_sel_regmap,
99 .enable = regulator_enable_regmap,
100 .disable = regulator_disable_regmap,
101 .is_enabled = regulator_is_enabled_regmap,
102 .set_ramp_delay = rk808_set_ramp_delay,
103};
104
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800105static struct regulator_ops rk808_reg_ops = {
106 .list_voltage = regulator_list_voltage_linear_range,
107 .map_voltage = regulator_map_voltage_linear_range,
108 .get_voltage_sel = regulator_get_voltage_sel_regmap,
109 .set_voltage_sel = regulator_set_voltage_sel_regmap,
110 .enable = regulator_enable_regmap,
111 .disable = regulator_disable_regmap,
112 .is_enabled = regulator_is_enabled_regmap,
113};
114
115static struct regulator_ops rk808_switch_ops = {
116 .enable = regulator_enable_regmap,
117 .disable = regulator_disable_regmap,
118 .is_enabled = regulator_is_enabled_regmap,
119};
120
121static const struct regulator_desc rk808_reg[] = {
122 {
123 .name = "DCDC_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700124 .supply_name = "vcc1",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800125 .id = RK808_ID_DCDC1,
Doug Anderson8af25222014-09-16 10:22:54 -0700126 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800127 .type = REGULATOR_VOLTAGE,
128 .n_voltages = 64,
129 .linear_ranges = rk808_buck_voltage_ranges,
130 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
131 .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
132 .vsel_mask = RK808_BUCK_VSEL_MASK,
133 .enable_reg = RK808_DCDC_EN_REG,
134 .enable_mask = BIT(0),
135 .owner = THIS_MODULE,
136 }, {
137 .name = "DCDC_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700138 .supply_name = "vcc2",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800139 .id = RK808_ID_DCDC2,
Doug Anderson8af25222014-09-16 10:22:54 -0700140 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800141 .type = REGULATOR_VOLTAGE,
142 .n_voltages = 64,
143 .linear_ranges = rk808_buck_voltage_ranges,
144 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
145 .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
146 .vsel_mask = RK808_BUCK_VSEL_MASK,
147 .enable_reg = RK808_DCDC_EN_REG,
148 .enable_mask = BIT(1),
149 .owner = THIS_MODULE,
150 }, {
151 .name = "DCDC_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700152 .supply_name = "vcc3",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800153 .id = RK808_ID_DCDC3,
154 .ops = &rk808_switch_ops,
155 .type = REGULATOR_VOLTAGE,
156 .n_voltages = 1,
157 .enable_reg = RK808_DCDC_EN_REG,
158 .enable_mask = BIT(2),
159 .owner = THIS_MODULE,
160 }, {
161 .name = "DCDC_REG4",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700162 .supply_name = "vcc4",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800163 .id = RK808_ID_DCDC4,
164 .ops = &rk808_reg_ops,
165 .type = REGULATOR_VOLTAGE,
Axel Lin5a820672014-09-02 08:47:31 +0800166 .n_voltages = 16,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800167 .linear_ranges = rk808_buck4_voltage_ranges,
168 .n_linear_ranges = ARRAY_SIZE(rk808_buck4_voltage_ranges),
169 .vsel_reg = RK808_BUCK4_ON_VSEL_REG,
170 .vsel_mask = RK808_BUCK4_VSEL_MASK,
171 .enable_reg = RK808_DCDC_EN_REG,
172 .enable_mask = BIT(3),
173 .owner = THIS_MODULE,
174 }, {
175 .name = "LDO_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700176 .supply_name = "vcc6",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800177 .id = RK808_ID_LDO1,
178 .ops = &rk808_reg_ops,
179 .type = REGULATOR_VOLTAGE,
180 .n_voltages = 17,
181 .linear_ranges = rk808_ldo_voltage_ranges,
182 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
183 .vsel_reg = RK808_LDO1_ON_VSEL_REG,
184 .vsel_mask = RK808_LDO_VSEL_MASK,
185 .enable_reg = RK808_LDO_EN_REG,
186 .enable_mask = BIT(0),
187 .owner = THIS_MODULE,
188 }, {
189 .name = "LDO_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700190 .supply_name = "vcc6",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800191 .id = RK808_ID_LDO2,
192 .ops = &rk808_reg_ops,
193 .type = REGULATOR_VOLTAGE,
194 .n_voltages = 17,
195 .linear_ranges = rk808_ldo_voltage_ranges,
196 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
197 .vsel_reg = RK808_LDO2_ON_VSEL_REG,
198 .vsel_mask = RK808_LDO_VSEL_MASK,
199 .enable_reg = RK808_LDO_EN_REG,
200 .enable_mask = BIT(1),
201 .owner = THIS_MODULE,
202 }, {
203 .name = "LDO_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700204 .supply_name = "vcc7",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800205 .id = RK808_ID_LDO3,
206 .ops = &rk808_reg_ops,
207 .type = REGULATOR_VOLTAGE,
208 .n_voltages = 16,
209 .linear_ranges = rk808_ldo3_voltage_ranges,
210 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
211 .vsel_reg = RK808_LDO3_ON_VSEL_REG,
212 .vsel_mask = RK808_BUCK4_VSEL_MASK,
213 .enable_reg = RK808_LDO_EN_REG,
214 .enable_mask = BIT(2),
215 .owner = THIS_MODULE,
216 }, {
217 .name = "LDO_REG4",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700218 .supply_name = "vcc9",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800219 .id = RK808_ID_LDO4,
220 .ops = &rk808_reg_ops,
221 .type = REGULATOR_VOLTAGE,
222 .n_voltages = 17,
223 .linear_ranges = rk808_ldo_voltage_ranges,
224 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
225 .vsel_reg = RK808_LDO4_ON_VSEL_REG,
226 .vsel_mask = RK808_LDO_VSEL_MASK,
227 .enable_reg = RK808_LDO_EN_REG,
228 .enable_mask = BIT(3),
229 .owner = THIS_MODULE,
230 }, {
231 .name = "LDO_REG5",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700232 .supply_name = "vcc9",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800233 .id = RK808_ID_LDO5,
234 .ops = &rk808_reg_ops,
235 .type = REGULATOR_VOLTAGE,
236 .n_voltages = 17,
237 .linear_ranges = rk808_ldo_voltage_ranges,
238 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
239 .vsel_reg = RK808_LDO5_ON_VSEL_REG,
240 .vsel_mask = RK808_LDO_VSEL_MASK,
241 .enable_reg = RK808_LDO_EN_REG,
242 .enable_mask = BIT(4),
243 .owner = THIS_MODULE,
244 }, {
245 .name = "LDO_REG6",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700246 .supply_name = "vcc10",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800247 .id = RK808_ID_LDO6,
248 .ops = &rk808_reg_ops,
249 .type = REGULATOR_VOLTAGE,
250 .n_voltages = 18,
251 .linear_ranges = rk808_ldo6_voltage_ranges,
252 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
253 .vsel_reg = RK808_LDO6_ON_VSEL_REG,
254 .vsel_mask = RK808_LDO_VSEL_MASK,
255 .enable_reg = RK808_LDO_EN_REG,
256 .enable_mask = BIT(5),
257 .owner = THIS_MODULE,
258 }, {
259 .name = "LDO_REG7",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700260 .supply_name = "vcc7",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800261 .id = RK808_ID_LDO7,
262 .ops = &rk808_reg_ops,
263 .type = REGULATOR_VOLTAGE,
264 .n_voltages = 18,
265 .linear_ranges = rk808_ldo6_voltage_ranges,
266 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
267 .vsel_reg = RK808_LDO7_ON_VSEL_REG,
268 .vsel_mask = RK808_LDO_VSEL_MASK,
269 .enable_reg = RK808_LDO_EN_REG,
270 .enable_mask = BIT(6),
271 .owner = THIS_MODULE,
272 }, {
273 .name = "LDO_REG8",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700274 .supply_name = "vcc11",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800275 .id = RK808_ID_LDO8,
276 .ops = &rk808_reg_ops,
277 .type = REGULATOR_VOLTAGE,
278 .n_voltages = 17,
279 .linear_ranges = rk808_ldo_voltage_ranges,
280 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
281 .vsel_reg = RK808_LDO8_ON_VSEL_REG,
282 .vsel_mask = RK808_LDO_VSEL_MASK,
283 .enable_reg = RK808_LDO_EN_REG,
284 .enable_mask = BIT(7),
285 .owner = THIS_MODULE,
286 }, {
287 .name = "SWITCH_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700288 .supply_name = "vcc8",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800289 .id = RK808_ID_SWITCH1,
290 .ops = &rk808_switch_ops,
291 .type = REGULATOR_VOLTAGE,
292 .enable_reg = RK808_DCDC_EN_REG,
293 .enable_mask = BIT(5),
294 .owner = THIS_MODULE,
295 }, {
296 .name = "SWITCH_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700297 .supply_name = "vcc12",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800298 .id = RK808_ID_SWITCH2,
299 .ops = &rk808_switch_ops,
300 .type = REGULATOR_VOLTAGE,
301 .enable_reg = RK808_DCDC_EN_REG,
302 .enable_mask = BIT(6),
303 .owner = THIS_MODULE,
304 },
305};
306
307static struct of_regulator_match rk808_reg_matches[] = {
308 [RK808_ID_DCDC1] = { .name = "DCDC_REG1" },
309 [RK808_ID_DCDC2] = { .name = "DCDC_REG2" },
310 [RK808_ID_DCDC3] = { .name = "DCDC_REG3" },
311 [RK808_ID_DCDC4] = { .name = "DCDC_REG4" },
312 [RK808_ID_LDO1] = { .name = "LDO_REG1" },
313 [RK808_ID_LDO2] = { .name = "LDO_REG2" },
314 [RK808_ID_LDO3] = { .name = "LDO_REG3" },
315 [RK808_ID_LDO4] = { .name = "LDO_REG4" },
316 [RK808_ID_LDO5] = { .name = "LDO_REG5" },
317 [RK808_ID_LDO6] = { .name = "LDO_REG6" },
318 [RK808_ID_LDO7] = { .name = "LDO_REG7" },
319 [RK808_ID_LDO8] = { .name = "LDO_REG8" },
320 [RK808_ID_SWITCH1] = { .name = "SWITCH_REG1" },
321 [RK808_ID_SWITCH2] = { .name = "SWITCH_REG2" },
322};
323
Chris Zhong571a4012014-09-10 09:18:06 +0800324static int rk808_regulator_probe(struct platform_device *pdev)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800325{
Chris Zhong571a4012014-09-10 09:18:06 +0800326 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
327 struct i2c_client *client = rk808->i2c;
328 struct device_node *reg_np;
329 struct regulator_config config = {};
330 struct regulator_dev *rk808_rdev;
331 int ret, i;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800332
Chris Zhong571a4012014-09-10 09:18:06 +0800333 reg_np = of_get_child_by_name(client->dev.of_node, "regulators");
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800334 if (!reg_np)
335 return -ENXIO;
336
Axel Lin1ed3f8c2014-09-11 18:22:48 +0800337 ret = of_regulator_match(&pdev->dev, reg_np, rk808_reg_matches,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800338 RK808_NUM_REGULATORS);
Axel Lin1ed3f8c2014-09-11 18:22:48 +0800339 of_node_put(reg_np);
Chris Zhong571a4012014-09-10 09:18:06 +0800340 if (ret < 0)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800341 return ret;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800342
Chris Zhong571a4012014-09-10 09:18:06 +0800343 /* Instantiate the regulators */
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800344 for (i = 0; i < RK808_NUM_REGULATORS; i++) {
345 if (!rk808_reg_matches[i].init_data ||
346 !rk808_reg_matches[i].of_node)
347 continue;
348
Chris Zhongd76c3332014-08-25 21:37:06 +0800349 config.dev = &client->dev;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800350 config.driver_data = rk808;
351 config.regmap = rk808->regmap;
Chris Zhong571a4012014-09-10 09:18:06 +0800352 config.of_node = rk808_reg_matches[i].of_node;
353 config.init_data = rk808_reg_matches[i].init_data;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800354
355 rk808_rdev = devm_regulator_register(&pdev->dev,
356 &rk808_reg[i], &config);
357 if (IS_ERR(rk808_rdev)) {
Chris Zhongd76c3332014-08-25 21:37:06 +0800358 dev_err(&client->dev,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800359 "failed to register %d regulator\n", i);
360 return PTR_ERR(rk808_rdev);
361 }
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800362 }
Chris Zhong571a4012014-09-10 09:18:06 +0800363
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800364 return 0;
365}
366
367static struct platform_driver rk808_regulator_driver = {
368 .probe = rk808_regulator_probe,
369 .driver = {
370 .name = "rk808-regulator",
371 .owner = THIS_MODULE,
372 },
373};
374
375module_platform_driver(rk808_regulator_driver);
376
377MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
378MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
Chris Zhong571a4012014-09-10 09:18:06 +0800379MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800380MODULE_LICENSE("GPL");
381MODULE_ALIAS("platform:rk808-regulator");