blob: dfa8d50a5d741461ba24895a4d628bd6cc9b2c72 [file] [log] [blame]
Chris Zhong2cd64ae2014-08-20 11:36:42 +08001/*
Wadim Egorov11375292016-08-29 13:07:59 +02002 * Regulator driver for Rockchip RK808/RK818
Chris Zhong2cd64ae2014-08-20 11:36:42 +08003 *
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 *
Wadim Egorov11375292016-08-29 13:07:59 +02009 * Copyright (C) 2016 PHYTEC Messtechnik GmbH
10 *
11 * Author: Wadim Egorov <w.egorov@phytec.de>
12 *
Chris Zhong2cd64ae2014-08-20 11:36:42 +080013 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
Chris Zhong2cd64ae2014-08-20 11:36:42 +080021 */
22
Chris Zhongbad47ad2015-07-20 17:23:53 +080023#include <linux/delay.h>
24#include <linux/gpio.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080025#include <linux/i2c.h>
Chris Zhongbad47ad2015-07-20 17:23:53 +080026#include <linux/module.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080027#include <linux/of_device.h>
Chris Zhongbad47ad2015-07-20 17:23:53 +080028#include <linux/of_gpio.h>
29#include <linux/mfd/rk808.h>
Chris Zhong2cd64ae2014-08-20 11:36:42 +080030#include <linux/regulator/driver.h>
31#include <linux/regulator/of_regulator.h>
Uwe Kleine-König604d4992015-07-21 16:46:24 +020032#include <linux/gpio/consumer.h>
Chris Zhong571a4012014-09-10 09:18:06 +080033
34/* Field Definitions */
Chris Zhong2cd64ae2014-08-20 11:36:42 +080035#define RK808_BUCK_VSEL_MASK 0x3f
36#define RK808_BUCK4_VSEL_MASK 0xf
37#define RK808_LDO_VSEL_MASK 0x1f
38
Wadim Egorov11375292016-08-29 13:07:59 +020039#define RK818_BUCK_VSEL_MASK 0x3f
40#define RK818_BUCK4_VSEL_MASK 0x1f
41#define RK818_LDO_VSEL_MASK 0x1f
42#define RK818_LDO3_ON_VSEL_MASK 0xf
43#define RK818_BOOST_ON_VSEL_MASK 0xe0
44
Doug Anderson8af25222014-09-16 10:22:54 -070045/* Ramp rate definitions for buck1 / buck2 only */
46#define RK808_RAMP_RATE_OFFSET 3
47#define RK808_RAMP_RATE_MASK (3 << RK808_RAMP_RATE_OFFSET)
48#define RK808_RAMP_RATE_2MV_PER_US (0 << RK808_RAMP_RATE_OFFSET)
49#define RK808_RAMP_RATE_4MV_PER_US (1 << RK808_RAMP_RATE_OFFSET)
50#define RK808_RAMP_RATE_6MV_PER_US (2 << RK808_RAMP_RATE_OFFSET)
51#define RK808_RAMP_RATE_10MV_PER_US (3 << RK808_RAMP_RATE_OFFSET)
52
Chris Zhongbad47ad2015-07-20 17:23:53 +080053#define RK808_DVS2_POL BIT(2)
54#define RK808_DVS1_POL BIT(1)
55
Chris Zhong251ce3182014-10-10 15:35:06 -070056/* Offset from XXX_ON_VSEL to XXX_SLP_VSEL */
57#define RK808_SLP_REG_OFFSET 1
58
Chris Zhongbad47ad2015-07-20 17:23:53 +080059/* Offset from XXX_ON_VSEL to XXX_DVS_VSEL */
60#define RK808_DVS_REG_OFFSET 2
61
Chris Zhong251ce3182014-10-10 15:35:06 -070062/* Offset from XXX_EN_REG to SLEEP_SET_OFF_XXX */
63#define RK808_SLP_SET_OFF_REG_OFFSET 2
64
Chris Zhongbad47ad2015-07-20 17:23:53 +080065/* max steps for increase voltage of Buck1/2, equal 100mv*/
66#define MAX_STEPS_ONE_TIME 8
67
Wadim Egorov9e9daa02016-05-10 15:18:55 +020068#define RK8XX_DESC(_id, _match, _supply, _min, _max, _step, _vreg, \
69 _vmask, _ereg, _emask, _etime) \
70 [_id] = { \
71 .name = (_match), \
72 .supply_name = (_supply), \
73 .of_match = of_match_ptr(_match), \
74 .regulators_node = of_match_ptr("regulators"), \
75 .type = REGULATOR_VOLTAGE, \
76 .id = (_id), \
77 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
78 .owner = THIS_MODULE, \
79 .min_uV = (_min) * 1000, \
80 .uV_step = (_step) * 1000, \
81 .vsel_reg = (_vreg), \
82 .vsel_mask = (_vmask), \
83 .enable_reg = (_ereg), \
84 .enable_mask = (_emask), \
85 .enable_time = (_etime), \
86 .ops = &rk808_reg_ops, \
87 }
88
89#define RK8XX_DESC_SWITCH(_id, _match, _supply, _ereg, _emask) \
90 [_id] = { \
91 .name = (_match), \
92 .supply_name = (_supply), \
93 .of_match = of_match_ptr(_match), \
94 .regulators_node = of_match_ptr("regulators"), \
95 .type = REGULATOR_VOLTAGE, \
96 .id = (_id), \
97 .enable_reg = (_ereg), \
98 .enable_mask = (_emask), \
99 .owner = THIS_MODULE, \
100 .ops = &rk808_switch_ops \
101 }
102
103
Chris Zhongbad47ad2015-07-20 17:23:53 +0800104struct rk808_regulator_data {
105 struct gpio_desc *dvs_gpio[2];
106};
107
Doug Anderson8af25222014-09-16 10:22:54 -0700108static const int rk808_buck_config_regs[] = {
109 RK808_BUCK1_CONFIG_REG,
110 RK808_BUCK2_CONFIG_REG,
111 RK808_BUCK3_CONFIG_REG,
112 RK808_BUCK4_CONFIG_REG,
113};
114
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800115static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
116 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
117 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
118};
119
Chris Zhongbad47ad2015-07-20 17:23:53 +0800120static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
121{
122 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
123 int id = rdev->desc->id - RK808_ID_DCDC1;
124 struct gpio_desc *gpio = pdata->dvs_gpio[id];
125 unsigned int val;
126 int ret;
127
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200128 if (!gpio || gpiod_get_value(gpio) == 0)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800129 return regulator_get_voltage_sel_regmap(rdev);
130
131 ret = regmap_read(rdev->regmap,
132 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
133 &val);
134 if (ret != 0)
135 return ret;
136
137 val &= rdev->desc->vsel_mask;
138 val >>= ffs(rdev->desc->vsel_mask) - 1;
139
140 return val;
141}
142
143static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
144 unsigned sel)
145{
146 int ret, delta_sel;
147 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
148
149 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
150 if (ret != 0)
151 return ret;
152
153 tmp = val & ~mask;
154 old_sel = val & mask;
155 old_sel >>= ffs(mask) - 1;
156 delta_sel = sel - old_sel;
157
158 /*
159 * If directly modify the register to change the voltage, we will face
160 * the risk of overshoot. Put it into a multi-step, can effectively
161 * avoid this problem, a step is 100mv here.
162 */
163 while (delta_sel > MAX_STEPS_ONE_TIME) {
164 old_sel += MAX_STEPS_ONE_TIME;
165 val = old_sel << (ffs(mask) - 1);
166 val |= tmp;
167
168 /*
169 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
170 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
171 * got more than 65 us between each voltage change and thus
172 * won't ramp faster than ~1500 uV / us.
173 */
174 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
175 delta_sel = sel - old_sel;
176 }
177
178 sel <<= ffs(mask) - 1;
179 val = tmp | sel;
180 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
181
182 /*
183 * When we change the voltage register directly, the ramp rate is about
184 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
185 * so we needn't wait extra time after that.
186 */
187 udelay(1);
188
189 return ret;
190}
191
192static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
193 unsigned sel)
194{
195 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
196 int id = rdev->desc->id - RK808_ID_DCDC1;
197 struct gpio_desc *gpio = pdata->dvs_gpio[id];
198 unsigned int reg = rdev->desc->vsel_reg;
199 unsigned old_sel;
200 int ret, gpio_level;
201
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200202 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800203 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
204
205 gpio_level = gpiod_get_value(gpio);
206 if (gpio_level == 0) {
207 reg += RK808_DVS_REG_OFFSET;
208 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
209 } else {
210 ret = regmap_read(rdev->regmap,
211 reg + RK808_DVS_REG_OFFSET,
212 &old_sel);
213 }
214
215 if (ret != 0)
216 return ret;
217
218 sel <<= ffs(rdev->desc->vsel_mask) - 1;
219 sel |= old_sel & ~rdev->desc->vsel_mask;
220
221 ret = regmap_write(rdev->regmap, reg, sel);
222 if (ret)
223 return ret;
224
225 gpiod_set_value(gpio, !gpio_level);
226
227 return ret;
228}
229
230static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
231 unsigned int old_selector,
232 unsigned int new_selector)
233{
234 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
235 int id = rdev->desc->id - RK808_ID_DCDC1;
236 struct gpio_desc *gpio = pdata->dvs_gpio[id];
237
238 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200239 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800240 return 0;
241
242 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
243}
244
Doug Anderson8af25222014-09-16 10:22:54 -0700245static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
246{
247 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
248 unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
249 RK808_ID_DCDC1];
250
251 switch (ramp_delay) {
252 case 1 ... 2000:
253 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
254 break;
255 case 2001 ... 4000:
256 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
257 break;
258 case 4001 ... 6000:
259 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
260 break;
261 case 6001 ... 10000:
262 break;
263 default:
264 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
265 rdev->desc->name, ramp_delay);
266 }
267
268 return regmap_update_bits(rdev->regmap, reg,
269 RK808_RAMP_RATE_MASK, ramp_value);
270}
271
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800272static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
Chris Zhong251ce3182014-10-10 15:35:06 -0700273{
274 unsigned int reg;
Wadim Egorovafcd6662016-04-25 15:20:43 +0200275 int sel = regulator_map_voltage_linear(rdev, uv, uv);
276
277 if (sel < 0)
278 return -EINVAL;
279
280 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
281
282 return regmap_update_bits(rdev->regmap, reg,
283 rdev->desc->vsel_mask,
284 sel);
285}
286
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200287static int rk808_set_suspend_voltage_range(struct regulator_dev *rdev, int uv)
288{
289 unsigned int reg;
290 int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
291
292 if (sel < 0)
293 return -EINVAL;
294
295 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
296
297 return regmap_update_bits(rdev->regmap, reg,
298 rdev->desc->vsel_mask,
299 sel);
300}
301
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800302static int rk808_set_suspend_enable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700303{
304 unsigned int reg;
305
306 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
307
308 return regmap_update_bits(rdev->regmap, reg,
309 rdev->desc->enable_mask,
310 0);
311}
312
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800313static int rk808_set_suspend_disable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700314{
315 unsigned int reg;
316
317 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
318
319 return regmap_update_bits(rdev->regmap, reg,
320 rdev->desc->enable_mask,
321 rdev->desc->enable_mask);
322}
323
Doug Anderson8af25222014-09-16 10:22:54 -0700324static struct regulator_ops rk808_buck1_2_ops = {
Wadim Egorovafcd6662016-04-25 15:20:43 +0200325 .list_voltage = regulator_list_voltage_linear,
326 .map_voltage = regulator_map_voltage_linear,
Chris Zhongbad47ad2015-07-20 17:23:53 +0800327 .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap,
328 .set_voltage_sel = rk808_buck1_2_set_voltage_sel,
329 .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel,
Doug Anderson8af25222014-09-16 10:22:54 -0700330 .enable = regulator_enable_regmap,
331 .disable = regulator_disable_regmap,
332 .is_enabled = regulator_is_enabled_regmap,
333 .set_ramp_delay = rk808_set_ramp_delay,
Chris Zhong251ce3182014-10-10 15:35:06 -0700334 .set_suspend_voltage = rk808_set_suspend_voltage,
335 .set_suspend_enable = rk808_set_suspend_enable,
336 .set_suspend_disable = rk808_set_suspend_disable,
Doug Anderson8af25222014-09-16 10:22:54 -0700337};
338
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800339static struct regulator_ops rk808_reg_ops = {
Wadim Egorovafcd6662016-04-25 15:20:43 +0200340 .list_voltage = regulator_list_voltage_linear,
341 .map_voltage = regulator_map_voltage_linear,
342 .get_voltage_sel = regulator_get_voltage_sel_regmap,
343 .set_voltage_sel = regulator_set_voltage_sel_regmap,
344 .enable = regulator_enable_regmap,
345 .disable = regulator_disable_regmap,
346 .is_enabled = regulator_is_enabled_regmap,
347 .set_suspend_voltage = rk808_set_suspend_voltage,
348 .set_suspend_enable = rk808_set_suspend_enable,
349 .set_suspend_disable = rk808_set_suspend_disable,
350};
351
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200352static struct regulator_ops rk808_reg_ops_ranges = {
353 .list_voltage = regulator_list_voltage_linear_range,
354 .map_voltage = regulator_map_voltage_linear_range,
355 .get_voltage_sel = regulator_get_voltage_sel_regmap,
356 .set_voltage_sel = regulator_set_voltage_sel_regmap,
357 .enable = regulator_enable_regmap,
358 .disable = regulator_disable_regmap,
359 .is_enabled = regulator_is_enabled_regmap,
360 .set_suspend_voltage = rk808_set_suspend_voltage_range,
361 .set_suspend_enable = rk808_set_suspend_enable,
362 .set_suspend_disable = rk808_set_suspend_disable,
363};
364
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800365static struct regulator_ops rk808_switch_ops = {
Chris Zhong251ce3182014-10-10 15:35:06 -0700366 .enable = regulator_enable_regmap,
367 .disable = regulator_disable_regmap,
368 .is_enabled = regulator_is_enabled_regmap,
369 .set_suspend_enable = rk808_set_suspend_enable,
370 .set_suspend_disable = rk808_set_suspend_disable,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800371};
372
373static const struct regulator_desc rk808_reg[] = {
374 {
375 .name = "DCDC_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700376 .supply_name = "vcc1",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200377 .of_match = of_match_ptr("DCDC_REG1"),
378 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800379 .id = RK808_ID_DCDC1,
Doug Anderson8af25222014-09-16 10:22:54 -0700380 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800381 .type = REGULATOR_VOLTAGE,
Wadim Egorovafcd6662016-04-25 15:20:43 +0200382 .min_uV = 712500,
383 .uV_step = 12500,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800384 .n_voltages = 64,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800385 .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
386 .vsel_mask = RK808_BUCK_VSEL_MASK,
387 .enable_reg = RK808_DCDC_EN_REG,
388 .enable_mask = BIT(0),
389 .owner = THIS_MODULE,
390 }, {
391 .name = "DCDC_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700392 .supply_name = "vcc2",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200393 .of_match = of_match_ptr("DCDC_REG2"),
394 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800395 .id = RK808_ID_DCDC2,
Doug Anderson8af25222014-09-16 10:22:54 -0700396 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800397 .type = REGULATOR_VOLTAGE,
Wadim Egorovafcd6662016-04-25 15:20:43 +0200398 .min_uV = 712500,
399 .uV_step = 12500,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800400 .n_voltages = 64,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800401 .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
402 .vsel_mask = RK808_BUCK_VSEL_MASK,
403 .enable_reg = RK808_DCDC_EN_REG,
404 .enable_mask = BIT(1),
405 .owner = THIS_MODULE,
406 }, {
407 .name = "DCDC_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700408 .supply_name = "vcc3",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200409 .of_match = of_match_ptr("DCDC_REG3"),
410 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800411 .id = RK808_ID_DCDC3,
412 .ops = &rk808_switch_ops,
413 .type = REGULATOR_VOLTAGE,
414 .n_voltages = 1,
415 .enable_reg = RK808_DCDC_EN_REG,
416 .enable_mask = BIT(2),
417 .owner = THIS_MODULE,
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200418 },
419 RK8XX_DESC(RK808_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3300, 100,
420 RK808_BUCK4_ON_VSEL_REG, RK808_BUCK4_VSEL_MASK,
421 RK808_DCDC_EN_REG, BIT(3), 0),
422 RK8XX_DESC(RK808_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
423 RK808_LDO1_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
424 BIT(0), 400),
425 RK8XX_DESC(RK808_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
426 RK808_LDO2_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
427 BIT(1), 400),
428 {
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800429 .name = "LDO_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700430 .supply_name = "vcc7",
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200431 .of_match = of_match_ptr("LDO_REG3"),
432 .regulators_node = of_match_ptr("regulators"),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800433 .id = RK808_ID_LDO3,
Wadim Egorov129d7cf2016-04-26 16:54:04 +0200434 .ops = &rk808_reg_ops_ranges,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800435 .type = REGULATOR_VOLTAGE,
436 .n_voltages = 16,
437 .linear_ranges = rk808_ldo3_voltage_ranges,
438 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
439 .vsel_reg = RK808_LDO3_ON_VSEL_REG,
440 .vsel_mask = RK808_BUCK4_VSEL_MASK,
441 .enable_reg = RK808_LDO_EN_REG,
442 .enable_mask = BIT(2),
Doug Anderson28249b02015-02-20 16:53:38 -0800443 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800444 .owner = THIS_MODULE,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800445 },
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200446 RK8XX_DESC(RK808_ID_LDO4, "LDO_REG4", "vcc9", 1800, 3400, 100,
447 RK808_LDO4_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
448 BIT(3), 400),
449 RK8XX_DESC(RK808_ID_LDO5, "LDO_REG5", "vcc9", 1800, 3400, 100,
450 RK808_LDO5_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
451 BIT(4), 400),
452 RK8XX_DESC(RK808_ID_LDO6, "LDO_REG6", "vcc10", 800, 2500, 100,
453 RK808_LDO6_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
454 BIT(5), 400),
455 RK8XX_DESC(RK808_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
456 RK808_LDO7_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
457 BIT(6), 400),
458 RK8XX_DESC(RK808_ID_LDO8, "LDO_REG8", "vcc11", 1800, 3400, 100,
459 RK808_LDO8_ON_VSEL_REG, RK808_LDO_VSEL_MASK, RK808_LDO_EN_REG,
460 BIT(7), 400),
461 RK8XX_DESC_SWITCH(RK808_ID_SWITCH1, "SWITCH_REG1", "vcc8",
462 RK808_DCDC_EN_REG, BIT(5)),
463 RK8XX_DESC_SWITCH(RK808_ID_SWITCH2, "SWITCH_REG2", "vcc12",
464 RK808_DCDC_EN_REG, BIT(6)),
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800465};
466
Wadim Egorov11375292016-08-29 13:07:59 +0200467static const struct regulator_desc rk818_reg[] = {
468 {
469 .name = "DCDC_REG1",
470 .supply_name = "vcc1",
471 .of_match = of_match_ptr("DCDC_REG1"),
472 .regulators_node = of_match_ptr("regulators"),
473 .id = RK818_ID_DCDC1,
474 .ops = &rk808_reg_ops,
475 .type = REGULATOR_VOLTAGE,
476 .min_uV = 712500,
477 .uV_step = 12500,
478 .n_voltages = 64,
479 .vsel_reg = RK818_BUCK1_ON_VSEL_REG,
480 .vsel_mask = RK818_BUCK_VSEL_MASK,
481 .enable_reg = RK818_DCDC_EN_REG,
482 .enable_mask = BIT(0),
483 .owner = THIS_MODULE,
484 }, {
485 .name = "DCDC_REG2",
486 .supply_name = "vcc2",
487 .of_match = of_match_ptr("DCDC_REG2"),
488 .regulators_node = of_match_ptr("regulators"),
489 .id = RK818_ID_DCDC2,
490 .ops = &rk808_reg_ops,
491 .type = REGULATOR_VOLTAGE,
492 .min_uV = 712500,
493 .uV_step = 12500,
494 .n_voltages = 64,
495 .vsel_reg = RK818_BUCK2_ON_VSEL_REG,
496 .vsel_mask = RK818_BUCK_VSEL_MASK,
497 .enable_reg = RK818_DCDC_EN_REG,
498 .enable_mask = BIT(1),
499 .owner = THIS_MODULE,
500 }, {
501 .name = "DCDC_REG3",
502 .supply_name = "vcc3",
503 .of_match = of_match_ptr("DCDC_REG3"),
504 .regulators_node = of_match_ptr("regulators"),
505 .id = RK818_ID_DCDC3,
506 .ops = &rk808_switch_ops,
507 .type = REGULATOR_VOLTAGE,
508 .n_voltages = 1,
509 .enable_reg = RK818_DCDC_EN_REG,
510 .enable_mask = BIT(2),
511 .owner = THIS_MODULE,
512 },
513 RK8XX_DESC(RK818_ID_DCDC4, "DCDC_REG4", "vcc4", 1800, 3600, 100,
514 RK818_BUCK4_ON_VSEL_REG, RK818_BUCK4_VSEL_MASK,
515 RK818_DCDC_EN_REG, BIT(3), 0),
516 RK8XX_DESC(RK818_ID_BOOST, "DCDC_BOOST", "boost", 4700, 5400, 100,
517 RK818_BOOST_LDO9_ON_VSEL_REG, RK818_BOOST_ON_VSEL_MASK,
518 RK818_DCDC_EN_REG, BIT(4), 0),
519 RK8XX_DESC(RK818_ID_LDO1, "LDO_REG1", "vcc6", 1800, 3400, 100,
520 RK818_LDO1_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
521 BIT(0), 400),
522 RK8XX_DESC(RK818_ID_LDO2, "LDO_REG2", "vcc6", 1800, 3400, 100,
Wadim Egorov5b00d6c2017-03-22 16:50:50 +0100523 RK818_LDO2_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
Wadim Egorov11375292016-08-29 13:07:59 +0200524 BIT(1), 400),
525 {
526 .name = "LDO_REG3",
527 .supply_name = "vcc7",
528 .of_match = of_match_ptr("LDO_REG3"),
529 .regulators_node = of_match_ptr("regulators"),
530 .id = RK818_ID_LDO3,
531 .ops = &rk808_reg_ops_ranges,
532 .type = REGULATOR_VOLTAGE,
533 .n_voltages = 16,
534 .linear_ranges = rk808_ldo3_voltage_ranges,
535 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
536 .vsel_reg = RK818_LDO3_ON_VSEL_REG,
537 .vsel_mask = RK818_LDO3_ON_VSEL_MASK,
538 .enable_reg = RK818_LDO_EN_REG,
539 .enable_mask = BIT(2),
540 .enable_time = 400,
541 .owner = THIS_MODULE,
542 },
543 RK8XX_DESC(RK818_ID_LDO4, "LDO_REG4", "vcc8", 1800, 3400, 100,
544 RK818_LDO4_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
545 BIT(3), 400),
546 RK8XX_DESC(RK818_ID_LDO5, "LDO_REG5", "vcc7", 1800, 3400, 100,
547 RK818_LDO5_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
548 BIT(4), 400),
549 RK8XX_DESC(RK818_ID_LDO6, "LDO_REG6", "vcc8", 800, 2500, 100,
550 RK818_LDO6_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
551 BIT(5), 400),
552 RK8XX_DESC(RK818_ID_LDO7, "LDO_REG7", "vcc7", 800, 2500, 100,
553 RK818_LDO7_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
554 BIT(6), 400),
555 RK8XX_DESC(RK818_ID_LDO8, "LDO_REG8", "vcc8", 1800, 3400, 100,
556 RK818_LDO8_ON_VSEL_REG, RK818_LDO_VSEL_MASK, RK818_LDO_EN_REG,
557 BIT(7), 400),
558 RK8XX_DESC(RK818_ID_LDO9, "LDO_REG9", "vcc9", 1800, 3400, 100,
559 RK818_BOOST_LDO9_ON_VSEL_REG, RK818_LDO_VSEL_MASK,
560 RK818_DCDC_EN_REG, BIT(5), 400),
561 RK8XX_DESC_SWITCH(RK818_ID_SWITCH, "SWITCH_REG", "vcc9",
562 RK818_DCDC_EN_REG, BIT(6)),
563 RK8XX_DESC_SWITCH(RK818_ID_HDMI_SWITCH, "HDMI_SWITCH", "h_5v",
564 RK818_H5V_EN_REG, BIT(0)),
565 RK8XX_DESC_SWITCH(RK818_ID_OTG_SWITCH, "OTG_SWITCH", "usb",
566 RK818_DCDC_EN_REG, BIT(7)),
567};
568
Chris Zhongbad47ad2015-07-20 17:23:53 +0800569static int rk808_regulator_dt_parse_pdata(struct device *dev,
570 struct device *client_dev,
571 struct regmap *map,
572 struct rk808_regulator_data *pdata)
573{
574 struct device_node *np;
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200575 int tmp, ret = 0, i;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800576
577 np = of_get_child_by_name(client_dev->of_node, "regulators");
578 if (!np)
579 return -ENXIO;
580
Chris Zhongbad47ad2015-07-20 17:23:53 +0800581 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200582 pdata->dvs_gpio[i] =
583 devm_gpiod_get_index_optional(client_dev, "dvs", i,
584 GPIOD_OUT_LOW);
Chris Zhongbad47ad2015-07-20 17:23:53 +0800585 if (IS_ERR(pdata->dvs_gpio[i])) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200586 ret = PTR_ERR(pdata->dvs_gpio[i]);
587 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
588 goto dt_parse_end;
589 }
590
591 if (!pdata->dvs_gpio[i]) {
Chris Zhongbad47ad2015-07-20 17:23:53 +0800592 dev_warn(dev, "there is no dvs%d gpio\n", i);
593 continue;
594 }
595
Chris Zhongbad47ad2015-07-20 17:23:53 +0800596 tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
597 ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
598 gpiod_is_active_low(pdata->dvs_gpio[i]) ?
599 0 : tmp);
600 }
601
602dt_parse_end:
603 of_node_put(np);
604 return ret;
605}
606
Chris Zhong571a4012014-09-10 09:18:06 +0800607static int rk808_regulator_probe(struct platform_device *pdev)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800608{
Chris Zhong571a4012014-09-10 09:18:06 +0800609 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
610 struct i2c_client *client = rk808->i2c;
Chris Zhong571a4012014-09-10 09:18:06 +0800611 struct regulator_config config = {};
612 struct regulator_dev *rk808_rdev;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800613 struct rk808_regulator_data *pdata;
Wadim Egorov11375292016-08-29 13:07:59 +0200614 const struct regulator_desc *regulators;
615 int ret, i, nregulators;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800616
Chris Zhongbad47ad2015-07-20 17:23:53 +0800617 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
618 if (!pdata)
619 return -ENOMEM;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800620
Chris Zhongbad47ad2015-07-20 17:23:53 +0800621 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
622 rk808->regmap, pdata);
Chris Zhong571a4012014-09-10 09:18:06 +0800623 if (ret < 0)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800624 return ret;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800625
Chris Zhongbad47ad2015-07-20 17:23:53 +0800626 platform_set_drvdata(pdev, pdata);
627
Wadim Egorov11375292016-08-29 13:07:59 +0200628 switch (rk808->variant) {
629 case RK808_ID:
630 regulators = rk808_reg;
631 nregulators = RK808_NUM_REGULATORS;
632 break;
633 case RK818_ID:
634 regulators = rk818_reg;
635 nregulators = RK818_NUM_REGULATORS;
636 break;
637 default:
638 dev_err(&client->dev, "unsupported RK8XX ID %lu\n",
639 rk808->variant);
640 return -EINVAL;
641 }
642
Wadim Egorov9e9daa02016-05-10 15:18:55 +0200643 config.dev = &client->dev;
644 config.driver_data = pdata;
645 config.regmap = rk808->regmap;
646
Chris Zhong571a4012014-09-10 09:18:06 +0800647 /* Instantiate the regulators */
Wadim Egorov11375292016-08-29 13:07:59 +0200648 for (i = 0; i < nregulators; i++) {
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800649 rk808_rdev = devm_regulator_register(&pdev->dev,
Wadim Egorov11375292016-08-29 13:07:59 +0200650 &regulators[i], &config);
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800651 if (IS_ERR(rk808_rdev)) {
Chris Zhongd76c3332014-08-25 21:37:06 +0800652 dev_err(&client->dev,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800653 "failed to register %d regulator\n", i);
654 return PTR_ERR(rk808_rdev);
655 }
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800656 }
Chris Zhong571a4012014-09-10 09:18:06 +0800657
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800658 return 0;
659}
660
661static struct platform_driver rk808_regulator_driver = {
662 .probe = rk808_regulator_probe,
663 .driver = {
Markus Elfring556ae222016-08-15 10:30:31 +0200664 .name = "rk808-regulator"
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800665 },
666};
667
668module_platform_driver(rk808_regulator_driver);
669
Wadim Egorov11375292016-08-29 13:07:59 +0200670MODULE_DESCRIPTION("regulator driver for the RK808/RK818 series PMICs");
671MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
672MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
673MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800674MODULE_LICENSE("GPL");
675MODULE_ALIAS("platform:rk808-regulator");