blob: d86a3dcd61e243a806724e16da6be4db44097a45 [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
58struct rk808_regulator_data {
59 struct gpio_desc *dvs_gpio[2];
60};
61
Doug Anderson8af25222014-09-16 10:22:54 -070062static const int rk808_buck_config_regs[] = {
63 RK808_BUCK1_CONFIG_REG,
64 RK808_BUCK2_CONFIG_REG,
65 RK808_BUCK3_CONFIG_REG,
66 RK808_BUCK4_CONFIG_REG,
67};
68
Chris Zhong2cd64ae2014-08-20 11:36:42 +080069static const struct regulator_linear_range rk808_buck_voltage_ranges[] = {
Chris Zhong1575e282014-09-30 17:44:54 +080070 REGULATOR_LINEAR_RANGE(712500, 0, 63, 12500),
Chris Zhong2cd64ae2014-08-20 11:36:42 +080071};
72
73static const struct regulator_linear_range rk808_buck4_voltage_ranges[] = {
74 REGULATOR_LINEAR_RANGE(1800000, 0, 15, 100000),
75};
76
77static const struct regulator_linear_range rk808_ldo_voltage_ranges[] = {
78 REGULATOR_LINEAR_RANGE(1800000, 0, 16, 100000),
79};
80
81static const struct regulator_linear_range rk808_ldo3_voltage_ranges[] = {
82 REGULATOR_LINEAR_RANGE(800000, 0, 13, 100000),
83 REGULATOR_LINEAR_RANGE(2500000, 15, 15, 0),
84};
85
86static const struct regulator_linear_range rk808_ldo6_voltage_ranges[] = {
87 REGULATOR_LINEAR_RANGE(800000, 0, 17, 100000),
88};
89
Chris Zhongbad47ad2015-07-20 17:23:53 +080090static int rk808_buck1_2_get_voltage_sel_regmap(struct regulator_dev *rdev)
91{
92 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
93 int id = rdev->desc->id - RK808_ID_DCDC1;
94 struct gpio_desc *gpio = pdata->dvs_gpio[id];
95 unsigned int val;
96 int ret;
97
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +020098 if (!gpio || gpiod_get_value(gpio) == 0)
Chris Zhongbad47ad2015-07-20 17:23:53 +080099 return regulator_get_voltage_sel_regmap(rdev);
100
101 ret = regmap_read(rdev->regmap,
102 rdev->desc->vsel_reg + RK808_DVS_REG_OFFSET,
103 &val);
104 if (ret != 0)
105 return ret;
106
107 val &= rdev->desc->vsel_mask;
108 val >>= ffs(rdev->desc->vsel_mask) - 1;
109
110 return val;
111}
112
113static int rk808_buck1_2_i2c_set_voltage_sel(struct regulator_dev *rdev,
114 unsigned sel)
115{
116 int ret, delta_sel;
117 unsigned int old_sel, tmp, val, mask = rdev->desc->vsel_mask;
118
119 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
120 if (ret != 0)
121 return ret;
122
123 tmp = val & ~mask;
124 old_sel = val & mask;
125 old_sel >>= ffs(mask) - 1;
126 delta_sel = sel - old_sel;
127
128 /*
129 * If directly modify the register to change the voltage, we will face
130 * the risk of overshoot. Put it into a multi-step, can effectively
131 * avoid this problem, a step is 100mv here.
132 */
133 while (delta_sel > MAX_STEPS_ONE_TIME) {
134 old_sel += MAX_STEPS_ONE_TIME;
135 val = old_sel << (ffs(mask) - 1);
136 val |= tmp;
137
138 /*
139 * i2c is 400kHz (2.5us per bit) and we must transmit _at least_
140 * 3 bytes (24 bits) plus start and stop so 26 bits. So we've
141 * got more than 65 us between each voltage change and thus
142 * won't ramp faster than ~1500 uV / us.
143 */
144 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
145 delta_sel = sel - old_sel;
146 }
147
148 sel <<= ffs(mask) - 1;
149 val = tmp | sel;
150 ret = regmap_write(rdev->regmap, rdev->desc->vsel_reg, val);
151
152 /*
153 * When we change the voltage register directly, the ramp rate is about
154 * 100000uv/us, wait 1us to make sure the target voltage to be stable,
155 * so we needn't wait extra time after that.
156 */
157 udelay(1);
158
159 return ret;
160}
161
162static int rk808_buck1_2_set_voltage_sel(struct regulator_dev *rdev,
163 unsigned sel)
164{
165 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
166 int id = rdev->desc->id - RK808_ID_DCDC1;
167 struct gpio_desc *gpio = pdata->dvs_gpio[id];
168 unsigned int reg = rdev->desc->vsel_reg;
169 unsigned old_sel;
170 int ret, gpio_level;
171
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200172 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800173 return rk808_buck1_2_i2c_set_voltage_sel(rdev, sel);
174
175 gpio_level = gpiod_get_value(gpio);
176 if (gpio_level == 0) {
177 reg += RK808_DVS_REG_OFFSET;
178 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &old_sel);
179 } else {
180 ret = regmap_read(rdev->regmap,
181 reg + RK808_DVS_REG_OFFSET,
182 &old_sel);
183 }
184
185 if (ret != 0)
186 return ret;
187
188 sel <<= ffs(rdev->desc->vsel_mask) - 1;
189 sel |= old_sel & ~rdev->desc->vsel_mask;
190
191 ret = regmap_write(rdev->regmap, reg, sel);
192 if (ret)
193 return ret;
194
195 gpiod_set_value(gpio, !gpio_level);
196
197 return ret;
198}
199
200static int rk808_buck1_2_set_voltage_time_sel(struct regulator_dev *rdev,
201 unsigned int old_selector,
202 unsigned int new_selector)
203{
204 struct rk808_regulator_data *pdata = rdev_get_drvdata(rdev);
205 int id = rdev->desc->id - RK808_ID_DCDC1;
206 struct gpio_desc *gpio = pdata->dvs_gpio[id];
207
208 /* if there is no dvs1/2 pin, we don't need wait extra time here. */
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200209 if (!gpio)
Chris Zhongbad47ad2015-07-20 17:23:53 +0800210 return 0;
211
212 return regulator_set_voltage_time_sel(rdev, old_selector, new_selector);
213}
214
Doug Anderson8af25222014-09-16 10:22:54 -0700215static int rk808_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
216{
217 unsigned int ramp_value = RK808_RAMP_RATE_10MV_PER_US;
218 unsigned int reg = rk808_buck_config_regs[rdev->desc->id -
219 RK808_ID_DCDC1];
220
221 switch (ramp_delay) {
222 case 1 ... 2000:
223 ramp_value = RK808_RAMP_RATE_2MV_PER_US;
224 break;
225 case 2001 ... 4000:
226 ramp_value = RK808_RAMP_RATE_4MV_PER_US;
227 break;
228 case 4001 ... 6000:
229 ramp_value = RK808_RAMP_RATE_6MV_PER_US;
230 break;
231 case 6001 ... 10000:
232 break;
233 default:
234 pr_warn("%s ramp_delay: %d not supported, setting 10000\n",
235 rdev->desc->name, ramp_delay);
236 }
237
238 return regmap_update_bits(rdev->regmap, reg,
239 RK808_RAMP_RATE_MASK, ramp_value);
240}
241
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800242static int rk808_set_suspend_voltage(struct regulator_dev *rdev, int uv)
Chris Zhong251ce3182014-10-10 15:35:06 -0700243{
244 unsigned int reg;
245 int sel = regulator_map_voltage_linear_range(rdev, uv, uv);
246
247 if (sel < 0)
248 return -EINVAL;
249
250 reg = rdev->desc->vsel_reg + RK808_SLP_REG_OFFSET;
251
252 return regmap_update_bits(rdev->regmap, reg,
253 rdev->desc->vsel_mask,
254 sel);
255}
256
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800257static int rk808_set_suspend_enable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700258{
259 unsigned int reg;
260
261 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
262
263 return regmap_update_bits(rdev->regmap, reg,
264 rdev->desc->enable_mask,
265 0);
266}
267
Wei Yongjun5cb2f032014-12-09 21:19:51 +0800268static int rk808_set_suspend_disable(struct regulator_dev *rdev)
Chris Zhong251ce3182014-10-10 15:35:06 -0700269{
270 unsigned int reg;
271
272 reg = rdev->desc->enable_reg + RK808_SLP_SET_OFF_REG_OFFSET;
273
274 return regmap_update_bits(rdev->regmap, reg,
275 rdev->desc->enable_mask,
276 rdev->desc->enable_mask);
277}
278
Doug Anderson8af25222014-09-16 10:22:54 -0700279static struct regulator_ops rk808_buck1_2_ops = {
280 .list_voltage = regulator_list_voltage_linear_range,
281 .map_voltage = regulator_map_voltage_linear_range,
Chris Zhongbad47ad2015-07-20 17:23:53 +0800282 .get_voltage_sel = rk808_buck1_2_get_voltage_sel_regmap,
283 .set_voltage_sel = rk808_buck1_2_set_voltage_sel,
284 .set_voltage_time_sel = rk808_buck1_2_set_voltage_time_sel,
Doug Anderson8af25222014-09-16 10:22:54 -0700285 .enable = regulator_enable_regmap,
286 .disable = regulator_disable_regmap,
287 .is_enabled = regulator_is_enabled_regmap,
288 .set_ramp_delay = rk808_set_ramp_delay,
Chris Zhong251ce3182014-10-10 15:35:06 -0700289 .set_suspend_voltage = rk808_set_suspend_voltage,
290 .set_suspend_enable = rk808_set_suspend_enable,
291 .set_suspend_disable = rk808_set_suspend_disable,
Doug Anderson8af25222014-09-16 10:22:54 -0700292};
293
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800294static struct regulator_ops rk808_reg_ops = {
295 .list_voltage = regulator_list_voltage_linear_range,
296 .map_voltage = regulator_map_voltage_linear_range,
297 .get_voltage_sel = regulator_get_voltage_sel_regmap,
298 .set_voltage_sel = regulator_set_voltage_sel_regmap,
299 .enable = regulator_enable_regmap,
300 .disable = regulator_disable_regmap,
301 .is_enabled = regulator_is_enabled_regmap,
Chris Zhong251ce3182014-10-10 15:35:06 -0700302 .set_suspend_voltage = rk808_set_suspend_voltage,
303 .set_suspend_enable = rk808_set_suspend_enable,
304 .set_suspend_disable = rk808_set_suspend_disable,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800305};
306
307static struct regulator_ops rk808_switch_ops = {
Chris Zhong251ce3182014-10-10 15:35:06 -0700308 .enable = regulator_enable_regmap,
309 .disable = regulator_disable_regmap,
310 .is_enabled = regulator_is_enabled_regmap,
311 .set_suspend_enable = rk808_set_suspend_enable,
312 .set_suspend_disable = rk808_set_suspend_disable,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800313};
314
315static const struct regulator_desc rk808_reg[] = {
316 {
317 .name = "DCDC_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700318 .supply_name = "vcc1",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800319 .id = RK808_ID_DCDC1,
Doug Anderson8af25222014-09-16 10:22:54 -0700320 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800321 .type = REGULATOR_VOLTAGE,
322 .n_voltages = 64,
323 .linear_ranges = rk808_buck_voltage_ranges,
324 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
325 .vsel_reg = RK808_BUCK1_ON_VSEL_REG,
326 .vsel_mask = RK808_BUCK_VSEL_MASK,
327 .enable_reg = RK808_DCDC_EN_REG,
328 .enable_mask = BIT(0),
329 .owner = THIS_MODULE,
330 }, {
331 .name = "DCDC_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700332 .supply_name = "vcc2",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800333 .id = RK808_ID_DCDC2,
Doug Anderson8af25222014-09-16 10:22:54 -0700334 .ops = &rk808_buck1_2_ops,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800335 .type = REGULATOR_VOLTAGE,
336 .n_voltages = 64,
337 .linear_ranges = rk808_buck_voltage_ranges,
338 .n_linear_ranges = ARRAY_SIZE(rk808_buck_voltage_ranges),
339 .vsel_reg = RK808_BUCK2_ON_VSEL_REG,
340 .vsel_mask = RK808_BUCK_VSEL_MASK,
341 .enable_reg = RK808_DCDC_EN_REG,
342 .enable_mask = BIT(1),
343 .owner = THIS_MODULE,
344 }, {
345 .name = "DCDC_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700346 .supply_name = "vcc3",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800347 .id = RK808_ID_DCDC3,
348 .ops = &rk808_switch_ops,
349 .type = REGULATOR_VOLTAGE,
350 .n_voltages = 1,
351 .enable_reg = RK808_DCDC_EN_REG,
352 .enable_mask = BIT(2),
353 .owner = THIS_MODULE,
354 }, {
355 .name = "DCDC_REG4",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700356 .supply_name = "vcc4",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800357 .id = RK808_ID_DCDC4,
358 .ops = &rk808_reg_ops,
359 .type = REGULATOR_VOLTAGE,
Axel Lin5a820672014-09-02 08:47:31 +0800360 .n_voltages = 16,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800361 .linear_ranges = rk808_buck4_voltage_ranges,
362 .n_linear_ranges = ARRAY_SIZE(rk808_buck4_voltage_ranges),
363 .vsel_reg = RK808_BUCK4_ON_VSEL_REG,
364 .vsel_mask = RK808_BUCK4_VSEL_MASK,
365 .enable_reg = RK808_DCDC_EN_REG,
366 .enable_mask = BIT(3),
367 .owner = THIS_MODULE,
368 }, {
369 .name = "LDO_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700370 .supply_name = "vcc6",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800371 .id = RK808_ID_LDO1,
372 .ops = &rk808_reg_ops,
373 .type = REGULATOR_VOLTAGE,
374 .n_voltages = 17,
375 .linear_ranges = rk808_ldo_voltage_ranges,
376 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
377 .vsel_reg = RK808_LDO1_ON_VSEL_REG,
378 .vsel_mask = RK808_LDO_VSEL_MASK,
379 .enable_reg = RK808_LDO_EN_REG,
380 .enable_mask = BIT(0),
Doug Anderson28249b02015-02-20 16:53:38 -0800381 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800382 .owner = THIS_MODULE,
383 }, {
384 .name = "LDO_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700385 .supply_name = "vcc6",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800386 .id = RK808_ID_LDO2,
387 .ops = &rk808_reg_ops,
388 .type = REGULATOR_VOLTAGE,
389 .n_voltages = 17,
390 .linear_ranges = rk808_ldo_voltage_ranges,
391 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
392 .vsel_reg = RK808_LDO2_ON_VSEL_REG,
393 .vsel_mask = RK808_LDO_VSEL_MASK,
394 .enable_reg = RK808_LDO_EN_REG,
395 .enable_mask = BIT(1),
Doug Anderson28249b02015-02-20 16:53:38 -0800396 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800397 .owner = THIS_MODULE,
398 }, {
399 .name = "LDO_REG3",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700400 .supply_name = "vcc7",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800401 .id = RK808_ID_LDO3,
402 .ops = &rk808_reg_ops,
403 .type = REGULATOR_VOLTAGE,
404 .n_voltages = 16,
405 .linear_ranges = rk808_ldo3_voltage_ranges,
406 .n_linear_ranges = ARRAY_SIZE(rk808_ldo3_voltage_ranges),
407 .vsel_reg = RK808_LDO3_ON_VSEL_REG,
408 .vsel_mask = RK808_BUCK4_VSEL_MASK,
409 .enable_reg = RK808_LDO_EN_REG,
410 .enable_mask = BIT(2),
Doug Anderson28249b02015-02-20 16:53:38 -0800411 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800412 .owner = THIS_MODULE,
413 }, {
414 .name = "LDO_REG4",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700415 .supply_name = "vcc9",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800416 .id = RK808_ID_LDO4,
417 .ops = &rk808_reg_ops,
418 .type = REGULATOR_VOLTAGE,
419 .n_voltages = 17,
420 .linear_ranges = rk808_ldo_voltage_ranges,
421 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
422 .vsel_reg = RK808_LDO4_ON_VSEL_REG,
423 .vsel_mask = RK808_LDO_VSEL_MASK,
424 .enable_reg = RK808_LDO_EN_REG,
425 .enable_mask = BIT(3),
Doug Anderson28249b02015-02-20 16:53:38 -0800426 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800427 .owner = THIS_MODULE,
428 }, {
429 .name = "LDO_REG5",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700430 .supply_name = "vcc9",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800431 .id = RK808_ID_LDO5,
432 .ops = &rk808_reg_ops,
433 .type = REGULATOR_VOLTAGE,
434 .n_voltages = 17,
435 .linear_ranges = rk808_ldo_voltage_ranges,
436 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
437 .vsel_reg = RK808_LDO5_ON_VSEL_REG,
438 .vsel_mask = RK808_LDO_VSEL_MASK,
439 .enable_reg = RK808_LDO_EN_REG,
440 .enable_mask = BIT(4),
Doug Anderson28249b02015-02-20 16:53:38 -0800441 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800442 .owner = THIS_MODULE,
443 }, {
444 .name = "LDO_REG6",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700445 .supply_name = "vcc10",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800446 .id = RK808_ID_LDO6,
447 .ops = &rk808_reg_ops,
448 .type = REGULATOR_VOLTAGE,
449 .n_voltages = 18,
450 .linear_ranges = rk808_ldo6_voltage_ranges,
451 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
452 .vsel_reg = RK808_LDO6_ON_VSEL_REG,
453 .vsel_mask = RK808_LDO_VSEL_MASK,
454 .enable_reg = RK808_LDO_EN_REG,
455 .enable_mask = BIT(5),
Doug Anderson28249b02015-02-20 16:53:38 -0800456 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800457 .owner = THIS_MODULE,
458 }, {
459 .name = "LDO_REG7",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700460 .supply_name = "vcc7",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800461 .id = RK808_ID_LDO7,
462 .ops = &rk808_reg_ops,
463 .type = REGULATOR_VOLTAGE,
464 .n_voltages = 18,
465 .linear_ranges = rk808_ldo6_voltage_ranges,
466 .n_linear_ranges = ARRAY_SIZE(rk808_ldo6_voltage_ranges),
467 .vsel_reg = RK808_LDO7_ON_VSEL_REG,
468 .vsel_mask = RK808_LDO_VSEL_MASK,
469 .enable_reg = RK808_LDO_EN_REG,
470 .enable_mask = BIT(6),
Doug Anderson28249b02015-02-20 16:53:38 -0800471 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800472 .owner = THIS_MODULE,
473 }, {
474 .name = "LDO_REG8",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700475 .supply_name = "vcc11",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800476 .id = RK808_ID_LDO8,
477 .ops = &rk808_reg_ops,
478 .type = REGULATOR_VOLTAGE,
479 .n_voltages = 17,
480 .linear_ranges = rk808_ldo_voltage_ranges,
481 .n_linear_ranges = ARRAY_SIZE(rk808_ldo_voltage_ranges),
482 .vsel_reg = RK808_LDO8_ON_VSEL_REG,
483 .vsel_mask = RK808_LDO_VSEL_MASK,
484 .enable_reg = RK808_LDO_EN_REG,
485 .enable_mask = BIT(7),
Doug Anderson28249b02015-02-20 16:53:38 -0800486 .enable_time = 400,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800487 .owner = THIS_MODULE,
488 }, {
489 .name = "SWITCH_REG1",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700490 .supply_name = "vcc8",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800491 .id = RK808_ID_SWITCH1,
492 .ops = &rk808_switch_ops,
493 .type = REGULATOR_VOLTAGE,
494 .enable_reg = RK808_DCDC_EN_REG,
495 .enable_mask = BIT(5),
496 .owner = THIS_MODULE,
497 }, {
498 .name = "SWITCH_REG2",
Doug Andersonb8074eb2014-09-02 09:14:28 -0700499 .supply_name = "vcc12",
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800500 .id = RK808_ID_SWITCH2,
501 .ops = &rk808_switch_ops,
502 .type = REGULATOR_VOLTAGE,
503 .enable_reg = RK808_DCDC_EN_REG,
504 .enable_mask = BIT(6),
505 .owner = THIS_MODULE,
506 },
507};
508
509static struct of_regulator_match rk808_reg_matches[] = {
510 [RK808_ID_DCDC1] = { .name = "DCDC_REG1" },
511 [RK808_ID_DCDC2] = { .name = "DCDC_REG2" },
512 [RK808_ID_DCDC3] = { .name = "DCDC_REG3" },
513 [RK808_ID_DCDC4] = { .name = "DCDC_REG4" },
514 [RK808_ID_LDO1] = { .name = "LDO_REG1" },
515 [RK808_ID_LDO2] = { .name = "LDO_REG2" },
516 [RK808_ID_LDO3] = { .name = "LDO_REG3" },
517 [RK808_ID_LDO4] = { .name = "LDO_REG4" },
518 [RK808_ID_LDO5] = { .name = "LDO_REG5" },
519 [RK808_ID_LDO6] = { .name = "LDO_REG6" },
520 [RK808_ID_LDO7] = { .name = "LDO_REG7" },
521 [RK808_ID_LDO8] = { .name = "LDO_REG8" },
522 [RK808_ID_SWITCH1] = { .name = "SWITCH_REG1" },
523 [RK808_ID_SWITCH2] = { .name = "SWITCH_REG2" },
524};
525
Chris Zhongbad47ad2015-07-20 17:23:53 +0800526static int rk808_regulator_dt_parse_pdata(struct device *dev,
527 struct device *client_dev,
528 struct regmap *map,
529 struct rk808_regulator_data *pdata)
530{
531 struct device_node *np;
532 int tmp, ret, i;
533
534 np = of_get_child_by_name(client_dev->of_node, "regulators");
535 if (!np)
536 return -ENXIO;
537
538 ret = of_regulator_match(dev, np, rk808_reg_matches,
539 RK808_NUM_REGULATORS);
540 if (ret < 0)
541 goto dt_parse_end;
542
543 for (i = 0; i < ARRAY_SIZE(pdata->dvs_gpio); i++) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200544 pdata->dvs_gpio[i] =
545 devm_gpiod_get_index_optional(client_dev, "dvs", i,
546 GPIOD_OUT_LOW);
Chris Zhongbad47ad2015-07-20 17:23:53 +0800547 if (IS_ERR(pdata->dvs_gpio[i])) {
Uwe Kleine-Königa13eaf02015-07-21 16:46:25 +0200548 ret = PTR_ERR(pdata->dvs_gpio[i]);
549 dev_err(dev, "failed to get dvs%d gpio (%d)\n", i, ret);
550 goto dt_parse_end;
551 }
552
553 if (!pdata->dvs_gpio[i]) {
Chris Zhongbad47ad2015-07-20 17:23:53 +0800554 dev_warn(dev, "there is no dvs%d gpio\n", i);
555 continue;
556 }
557
Chris Zhongbad47ad2015-07-20 17:23:53 +0800558 tmp = i ? RK808_DVS2_POL : RK808_DVS1_POL;
559 ret = regmap_update_bits(map, RK808_IO_POL_REG, tmp,
560 gpiod_is_active_low(pdata->dvs_gpio[i]) ?
561 0 : tmp);
562 }
563
564dt_parse_end:
565 of_node_put(np);
566 return ret;
567}
568
Chris Zhong571a4012014-09-10 09:18:06 +0800569static int rk808_regulator_probe(struct platform_device *pdev)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800570{
Chris Zhong571a4012014-09-10 09:18:06 +0800571 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
572 struct i2c_client *client = rk808->i2c;
Chris Zhong571a4012014-09-10 09:18:06 +0800573 struct regulator_config config = {};
574 struct regulator_dev *rk808_rdev;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800575 struct rk808_regulator_data *pdata;
Chris Zhong571a4012014-09-10 09:18:06 +0800576 int ret, i;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800577
Chris Zhongbad47ad2015-07-20 17:23:53 +0800578 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
579 if (!pdata)
580 return -ENOMEM;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800581
Chris Zhongbad47ad2015-07-20 17:23:53 +0800582 ret = rk808_regulator_dt_parse_pdata(&pdev->dev, &client->dev,
583 rk808->regmap, pdata);
Chris Zhong571a4012014-09-10 09:18:06 +0800584 if (ret < 0)
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800585 return ret;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800586
Chris Zhongbad47ad2015-07-20 17:23:53 +0800587 platform_set_drvdata(pdev, pdata);
588
Chris Zhong571a4012014-09-10 09:18:06 +0800589 /* Instantiate the regulators */
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800590 for (i = 0; i < RK808_NUM_REGULATORS; i++) {
591 if (!rk808_reg_matches[i].init_data ||
592 !rk808_reg_matches[i].of_node)
593 continue;
594
Chris Zhongd76c3332014-08-25 21:37:06 +0800595 config.dev = &client->dev;
Chris Zhongbad47ad2015-07-20 17:23:53 +0800596 config.driver_data = pdata;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800597 config.regmap = rk808->regmap;
Chris Zhong571a4012014-09-10 09:18:06 +0800598 config.of_node = rk808_reg_matches[i].of_node;
599 config.init_data = rk808_reg_matches[i].init_data;
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800600
601 rk808_rdev = devm_regulator_register(&pdev->dev,
602 &rk808_reg[i], &config);
603 if (IS_ERR(rk808_rdev)) {
Chris Zhongd76c3332014-08-25 21:37:06 +0800604 dev_err(&client->dev,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800605 "failed to register %d regulator\n", i);
606 return PTR_ERR(rk808_rdev);
607 }
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800608 }
Chris Zhong571a4012014-09-10 09:18:06 +0800609
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800610 return 0;
611}
612
613static struct platform_driver rk808_regulator_driver = {
614 .probe = rk808_regulator_probe,
615 .driver = {
616 .name = "rk808-regulator",
Chris Zhongbad47ad2015-07-20 17:23:53 +0800617 .owner = THIS_MODULE,
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800618 },
619};
620
621module_platform_driver(rk808_regulator_driver);
622
623MODULE_DESCRIPTION("regulator driver for the rk808 series PMICs");
624MODULE_AUTHOR("Chris Zhong<zyw@rock-chips.com>");
Chris Zhong571a4012014-09-10 09:18:06 +0800625MODULE_AUTHOR("Zhang Qing<zhangqing@rock-chips.com>");
Chris Zhong2cd64ae2014-08-20 11:36:42 +0800626MODULE_LICENSE("GPL");
627MODULE_ALIAS("platform:rk808-regulator");