blob: a6767494d39b192fd4e2915455249712a63c8bc4 [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
Chris Zhongbad47ad2015-07-20 17:23:53 +080019#include <linux/delay.h>
20#include <linux/gpio.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080021#include <linux/i2c.h>
Chris Zhongbad47ad2015-07-20 17:23:53 +080022#include <linux/module.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080023#include <linux/of_device.h>
Chris Zhongbad47ad2015-07-20 17:23:53 +080024#include <linux/of_gpio.h>
25#include <linux/mfd/rk808.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080026#include <linux/regulator/driver.h>
27#include <linux/regulator/of_regulator.h>
Uwe Kleine-König604d4992015-07-21 16:46:24 +020028#include <linux/gpio/consumer.h>
Chris Zhong571a4012014-09-10 09:18:06 +080029
30/* Field Definitions */
Chris Zhong2cd64ae2014-08-20 11:36:42 +080031#define RK808_BUCK_VSEL_MASK 0x3f
32#define RK808_BUCK4_VSEL_MASK 0xf
33#define RK808_LDO_VSEL_MASK 0x1f
34
Doug Anderson8af25222014-09-16 10:22:54 -070035/* 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)
42
Chris Zhongbad47ad2015-07-20 17:23:53 +080043#define RK808_DVS2_POL BIT(2)
44#define RK808_DVS1_POL BIT(1)
45
Chris Zhong251ce3182014-10-10 15:35:06 -070046/* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
47#define RK808_SLP_REG_OFFSET 1
48
Chris Zhongbad47ad2015-07-20 17:23:53 +080049/* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
50#define RK808_DVS_REG_OFFSET 2
51
Chris Zhong251ce3182014-10-10 15:35:06 -070052/* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
53#define RK808_SLP_SET_OFF_REG_OFFSET 2
54
Chris Zhongbad47ad2015-07-20 17:23:53 +080055/* max steps for increase voltage of Buck1/2, equal 100mv*/
56#define MAX_STEPS_ONE_TIME 8
57
Wadim Egorov9e9daa02016-05-10 15:18:55 +020058#define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
59 _vmask, _ereg, _emask, _etime) \
60 [_id] = { \
61 .name = (_match), \
62 .supply_name = (_supply), \
63 .of_match = of_match_ptr(_match), \
64 .regulators_node = of_match_ptr("regulators"), \
65 .type = REGULATOR_VOLTAGE, \
66 .id = (_id), \
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, \
77 }
78
79#define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \
80 [_id] = { \
81 .name = (_match), \
82 .supply_name = (_supply), \
83 .of_match = of_match_ptr(_match), \
84 .regulators_node = of_match_ptr("regulators"), \
85 .type = REGULATOR_VOLTAGE, \
86 .id = (_id), \
87 .enable_reg = (_ereg), \
88 .enable_mask = (_emask), \
89 .owner = THIS_MODULE, \
90 .ops = &rk808_switch_ops \
91 }
92
93
Chris Zhongbad47ad2015-07-20 17:23:53 +080094struct rk808_regulator_data {
95 struct gpio_desc *dvs_gpio[2];
96};
97
Doug Anderson8af25222014-09-16 10:22:54 -070098static 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,
103};
104
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800105static 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),
108};
109
Chris Zhongbad47ad2015-07-20 17:23:53 +0800110static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
111{
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];
115 unsigned int val;
116 int ret;
117
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200118 if (!gpio || gpiod_get_value(gpio) == 0)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800119 return regulator_get_voltage_sel_regmap(rdev);
120
121 ret = regmap_read(rdev->regmap,
122 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
123 &val);
124 if (ret != 0)
125 return ret;
126
127 val &= rdev->desc->vsel_mask;
128 val >>= ffs(rdev->desc->vsel_mask) - 1;
129
130 return val;
131}
132
133static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
134 unsigned sel)
135{
136 int ret, delta_sel;
137 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
138
139 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
140 if (ret != 0)
141 return ret;
142
143 tmp = val & ~mask;
144 old_sel = val & mask;
145 old_sel >>= ffs(mask) - 1;
146 delta_sel = sel - old_sel;
147
148 /*
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.
152 */
153 while (delta_sel > MAX_STEPS_ONE_TIME) {
154 old_sel += MAX_STEPS_ONE_TIME;
155 val = old_sel << (ffs(mask) - 1);
156 val |= tmp;
157
158 /*
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.
163 */
164 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
165 delta_sel = sel - old_sel;
166 }
167
168 sel <<= ffs(mask) - 1;
169 val = tmp | sel;
170 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
171
172 /*
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.
176 */
177 udelay(1);
178
179 return ret;
180}
181
182static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
183 unsigned sel)
184{
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;
189 unsigned old_sel;
190 int ret, gpio_level;
191
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200192 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800193 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
194
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);
199 } else {
200 ret = regmap_read(rdev->regmap,
201 reg + RK808_DVS_REG_OFFSET,
202 &old_sel);
203 }
204
205 if (ret != 0)
206 return ret;
207
208 sel <<= ffs(rdev->desc->vsel_mask) - 1;
209 sel |= old_sel & ~rdev->desc->vsel_mask;
210
211 ret = regmap_write(rdev->regmap, reg, sel);
212 if (ret)
213 return ret;
214
215 gpiod_set_value(gpio, !gpio_level);
216
217 return ret;
218}
219
220static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
221 unsigned int old_selector,
222 unsigned int new_selector)
223{
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];
227
228 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200229 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800230 return 0;
231
232 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
233}
234
Doug Anderson8af25222014-09-16 10:22:54 -0700235static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
236{
237 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
238 unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
239 RK808_ID_DCDC1];
240
241 switch (ramp_delay) {
242 case 1 ... 2000:
243 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
244 break;
245 case 2001 ... 4000:
246 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
247 break;
248 case 4001 ... 6000:
249 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
250 break;
251 case 6001 ... 10000:
252 break;
253 default:
254 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
255 rdev->desc->name, ramp_delay);
256 }
257
258 return regmap_update_bits(rdev->regmap, reg,
259 RK808_RAMP_RATE_MASK, ramp_value);
260}
261
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800262static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
Chris Zhong251ce3182014-10-10 15:35:06 -0700263{
264 unsigned int reg;
Wadim Egorovafcd6662016-04-25 15:20:43 +0200265 int sel = regulator_map_voltage_linear(rdev, uv, uv);
266
267 if (sel < 0)
268 return -EINVAL;
269
270 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
271
272 return regmap_update_bits(rdev->regmap, reg,
273 rdev->desc->vsel_mask,
274 sel);
275}
276
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200277static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
278{
279 unsigned int reg;
280 int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
281
282 if (sel < 0)
283 return -EINVAL;
284
285 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
286
287 return regmap_update_bits(rdev->regmap, reg,
288 rdev->desc->vsel_mask,
289 sel);
290}
291
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800292static int rk808_set_suspend_enable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700293{
294 unsigned int reg;
295
296 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
297
298 return regmap_update_bits(rdev->regmap, reg,
299 rdev->desc->enable_mask,
300 0);
301}
302
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800303static int rk808_set_suspend_disable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700304{
305 unsigned int reg;
306
307 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
308
309 return regmap_update_bits(rdev->regmap, reg,
310 rdev->desc->enable_mask,
311 rdev->desc->enable_mask);
312}
313
Doug Anderson8af25222014-09-16 10:22:54 -0700314static struct regulator_ops rk808_buck1_2_ops = {
Wadim Egorovafcd6662016-04-25 15:20:43 +0200315 .list_voltage = regulator_list_voltage_linear,
316 .map_voltage = regulator_map_voltage_linear,
Chris Zhongbad47ad2015-07-20 17:23:53 +0800317 .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,
Doug Anderson8af25222014-09-16 10:22:54 -0700320 .enable = regulator_enable_regmap,
321 .disable = regulator_disable_regmap,
322 .is_enabled = regulator_is_enabled_regmap,
323 .set_ramp_delay = rk808_set_ramp_delay,
Chris Zhong251ce3182014-10-10 15:35:06 -0700324 .set_suspend_voltage = rk808_set_suspend_voltage,
325 .set_suspend_enable = rk808_set_suspend_enable,
326 .set_suspend_disable = rk808_set_suspend_disable,
Doug Anderson8af25222014-09-16 10:22:54 -0700327};
328
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800329static struct regulator_ops rk808_reg_ops = {
Wadim Egorovafcd6662016-04-25 15:20:43 +0200330 .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,
340};
341
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200342static 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,
353};
354
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800355static struct regulator_ops rk808_switch_ops = {
Chris Zhong251ce3182014-10-10 15:35:06 -0700356 .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,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800361};
362
363static const struct regulator_desc rk808_reg[] = {
364 {
365 .name = "DCDC_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700366 .supply_name = "vcc1",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200367 .of_match = of_match_ptr("DCDC_REG1"),
368 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800369 .id = RK808_ID_DCDC1,
Doug Anderson8af25222014-09-16 10:22:54 -0700370 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800371 .type = REGULATOR_VOLTAGE,
Wadim Egorovafcd6662016-04-25 15:20:43 +0200372 .min_uV = 712500,
373 .uV_step = 12500,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800374 .n_voltages = 64,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800375 .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,
380 }, {
381 .name = "DCDC_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700382 .supply_name = "vcc2",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200383 .of_match = of_match_ptr("DCDC_REG2"),
384 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800385 .id = RK808_ID_DCDC2,
Doug Anderson8af25222014-09-16 10:22:54 -0700386 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800387 .type = REGULATOR_VOLTAGE,
Wadim Egorovafcd6662016-04-25 15:20:43 +0200388 .min_uV = 712500,
389 .uV_step = 12500,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800390 .n_voltages = 64,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800391 .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,
396 }, {
397 .name = "DCDC_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700398 .supply_name = "vcc3",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200399 .of_match = of_match_ptr("DCDC_REG3"),
400 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800401 .id = RK808_ID_DCDC3,
402 .ops = &rk808_switch_ops,
403 .type = REGULATOR_VOLTAGE,
404 .n_voltages = 1,
405 .enable_reg = RK808_DCDC_EN_REG,
406 .enable_mask = BIT(2),
407 .owner = THIS_MODULE,
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200408 },
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,
414 BIT(0), 400),
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,
417 BIT(1), 400),
418 {
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800419 .name = "LDO_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700420 .supply_name = "vcc7",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200421 .of_match = of_match_ptr("LDO_REG3"),
422 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800423 .id = RK808_ID_LDO3,
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200424 .ops = &rk808_reg_ops_ranges,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800425 .type = REGULATOR_VOLTAGE,
426 .n_voltages = 16,
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),
Doug Anderson28249b02015-02-20 16:53:38 -0800433 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800434 .owner = THIS_MODULE,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800435 },
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200436 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,
438 BIT(3), 400),
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,
441 BIT(4), 400),
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,
444 BIT(5), 400),
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,
447 BIT(6), 400),
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,
450 BIT(7), 400),
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)),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800455};
456
Chris Zhongbad47ad2015-07-20 17:23:53 +0800457static int rk808_regulator_dt_parse_pdata(struct device *dev,
458 struct device *client_dev,
459 struct regmap *map,
460 struct rk808_regulator_data *pdata)
461{
462 struct device_node *np;
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200463 int tmp, ret = 0, i;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800464
465 np = of_get_child_by_name(client_dev->of_node, "regulators");
466 if (!np)
467 return -ENXIO;
468
Chris Zhongbad47ad2015-07-20 17:23:53 +0800469 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200470 pdata->dvs_gpio[i] =
471 devm_gpiod_get_index_optional(client_dev, "dvs", i,
472 GPIOD_OUT_LOW);
Chris Zhongbad47ad2015-07-20 17:23:53 +0800473 if (IS_ERR(pdata->dvs_gpio[i])) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200474 ret = PTR_ERR(pdata->dvs_gpio[i]);
475 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
476 goto dt_parse_end;
477 }
478
479 if (!pdata->dvs_gpio[i]) {
Chris Zhongbad47ad2015-07-20 17:23:53 +0800480 dev_warn(dev, "there is no dvs%d gpio\n", i);
481 continue;
482 }
483
Chris Zhongbad47ad2015-07-20 17:23:53 +0800484 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]) ?
487 0 : tmp);
488 }
489
490dt_parse_end:
491 of_node_put(np);
492 return ret;
493}
494
Chris Zhong571a4012014-09-10 09:18:06 +0800495static int rk808_regulator_probe(struct platform_device *pdev)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800496{
Chris Zhong571a4012014-09-10 09:18:06 +0800497 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
498 struct i2c_client *client = rk808->i2c;
Chris Zhong571a4012014-09-10 09:18:06 +0800499 struct regulator_config config = {};
500 struct regulator_dev *rk808_rdev;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800501 struct rk808_regulator_data *pdata;
Chris Zhong571a4012014-09-10 09:18:06 +0800502 int ret, i;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800503
Chris Zhongbad47ad2015-07-20 17:23:53 +0800504 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
505 if (!pdata)
506 return -ENOMEM;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800507
Chris Zhongbad47ad2015-07-20 17:23:53 +0800508 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
509 rk808->regmap, pdata);
Chris Zhong571a4012014-09-10 09:18:06 +0800510 if (ret < 0)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800511 return ret;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800512
Chris Zhongbad47ad2015-07-20 17:23:53 +0800513 platform_set_drvdata(pdev, pdata);
514
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200515 config.dev = &client->dev;
516 config.driver_data = pdata;
517 config.regmap = rk808->regmap;
518
Chris Zhong571a4012014-09-10 09:18:06 +0800519 /* Instantiate the regulators */
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800520 for (i = 0; i < RK808_NUM_REGULATORS; i++) {
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800521 rk808_rdev = devm_regulator_register(&pdev->dev,
522 &rk808_reg[i], &config);
523 if (IS_ERR(rk808_rdev)) {
Chris Zhongd76c3332014-08-25 21:37:06 +0800524 dev_err(&client->dev,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800525 "failed to register %d regulator\n", i);
526 return PTR_ERR(rk808_rdev);
527 }
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800528 }
Chris Zhong571a4012014-09-10 09:18:06 +0800529
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800530 return 0;
531}
532
533static struct platform_driver rk808_regulator_driver = {
534 .probe = rk808_regulator_probe,
535 .driver = {
Markus Elfring556ae222016-08-15 10:30:31 +0200536 .name = "rk808-regulator"
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800537 },
538};
539
540module_platform_driver(rk808_regulator_driver);
541
542MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
543MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
Chris Zhong571a4012014-09-10 09:18:06 +0800544MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800545MODULE_LICENSE("GPL");
546MODULE_ALIAS("platform:rk808-regulator");