]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/regulator/rk808-regulator.c
Merge tag 'v3.18-rc1' into x86/urgent
[mirror_ubuntu-bionic-kernel.git] / drivers / regulator / rk808-regulator.c
CommitLineData
2cd64ae3
CZ
1/*
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.
2cd64ae3
CZ
17 */
18
19#include <linux/module.h>
2cd64ae3 20#include <linux/i2c.h>
2cd64ae3 21#include <linux/mfd/rk808.h>
2cd64ae3
CZ
22#include <linux/of_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/of_regulator.h>
571a4010
CZ
25
26/* Field Definitions */
2cd64ae3
CZ
27#define RK808_BUCK_VSEL_MASK 0x3f
28#define RK808_BUCK4_VSEL_MASK 0xf
29#define RK808_LDO_VSEL_MASK 0x1f
30
8af25227
DA
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)
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
2cd64ae3
CZ
46static const struct regulator_linear_range rk808_buck_voltage_ranges[] = {
47 REGULATOR_LINEAR_RANGE(700000, 0, 63, 12500),
48};
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
8af25227
DA
67static 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
2cd64ae3
CZ
105static 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",
b8074eba 124 .supply_name = "vcc1",
2cd64ae3 125 .id = RK808_ID_DCDC1,
8af25227 126 .ops = &rk808_buck1_2_ops,
2cd64ae3
CZ
127 .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",
b8074eba 138 .supply_name = "vcc2",
2cd64ae3 139 .id = RK808_ID_DCDC2,
8af25227 140 .ops = &rk808_buck1_2_ops,
2cd64ae3
CZ
141 .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",
b8074eba 152 .supply_name = "vcc3",
2cd64ae3
CZ
153 .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",
b8074eba 162 .supply_name = "vcc4",
2cd64ae3
CZ
163 .id = RK808_ID_DCDC4,
164 .ops = &rk808_reg_ops,
165 .type = REGULATOR_VOLTAGE,
5a82067f 166 .n_voltages = 16,
2cd64ae3
CZ
167 .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",
b8074eba 176 .supply_name = "vcc6",
2cd64ae3
CZ
177 .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",
b8074eba 190 .supply_name = "vcc6",
2cd64ae3
CZ
191 .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",
b8074eba 204 .supply_name = "vcc7",
2cd64ae3
CZ
205 .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",
b8074eba 218 .supply_name = "vcc9",
2cd64ae3
CZ
219 .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",
b8074eba 232 .supply_name = "vcc9",
2cd64ae3
CZ
233 .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",
b8074eba 246 .supply_name = "vcc10",
2cd64ae3
CZ
247 .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",
b8074eba 260 .supply_name = "vcc7",
2cd64ae3
CZ
261 .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",
b8074eba 274 .supply_name = "vcc11",
2cd64ae3
CZ
275 .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",
b8074eba 288 .supply_name = "vcc8",
2cd64ae3
CZ
289 .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",
b8074eba 297 .supply_name = "vcc12",
2cd64ae3
CZ
298 .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
2cd64ae3
CZ
324static int rk808_regulator_probe(struct platform_device *pdev)
325{
326 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
d76c333e 327 struct i2c_client *client = rk808->i2c;
571a4010 328 struct device_node *reg_np;
462004f1 329 struct regulator_config config = {};
2cd64ae3 330 struct regulator_dev *rk808_rdev;
571a4010 331 int ret, i;
2cd64ae3 332
571a4010
CZ
333 reg_np = of_get_child_by_name(client->dev.of_node, "regulators");
334 if (!reg_np)
335 return -ENXIO;
2cd64ae3 336
1ed3f8ce 337 ret = of_regulator_match(&pdev->dev, reg_np, rk808_reg_matches,
571a4010 338 RK808_NUM_REGULATORS);
1ed3f8ce 339 of_node_put(reg_np);
571a4010 340 if (ret < 0)
2cd64ae3
CZ
341 return ret;
342
2cd64ae3
CZ
343 /* Instantiate the regulators */
344 for (i = 0; i < RK808_NUM_REGULATORS; i++) {
571a4010
CZ
345 if (!rk808_reg_matches[i].init_data ||
346 !rk808_reg_matches[i].of_node)
2cd64ae3
CZ
347 continue;
348
d76c333e 349 config.dev = &client->dev;
2cd64ae3
CZ
350 config.driver_data = rk808;
351 config.regmap = rk808->regmap;
571a4010
CZ
352 config.of_node = rk808_reg_matches[i].of_node;
353 config.init_data = rk808_reg_matches[i].init_data;
2cd64ae3
CZ
354
355 rk808_rdev = devm_regulator_register(&pdev->dev,
356 &rk808_reg[i], &config);
357 if (IS_ERR(rk808_rdev)) {
d76c333e 358 dev_err(&client->dev,
2cd64ae3
CZ
359 "failed to register %d regulator\n", i);
360 return PTR_ERR(rk808_rdev);
361 }
2cd64ae3 362 }
571a4010 363
2cd64ae3
CZ
364 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>");
571a4010 379MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
2cd64ae3
CZ
380MODULE_LICENSE("GPL");
381MODULE_ALIAS("platform:rk808-regulator");