blob: 6884eb880dc9f33b56ef72d8eabd22cada499a3f [file] [log] [blame]
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001/*
2 * s5m8767.c
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd
5 * http://www.samsung.com
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
Sangbeom Kim9767ec72012-01-09 19:10:25 +090014#include <linux/err.h>
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080015#include <linux/of_gpio.h>
Sangbeom Kim9767ec72012-01-09 19:10:25 +090016#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/regulator/driver.h>
19#include <linux/regulator/machine.h>
Sangbeom Kim54227bc2012-07-11 21:07:16 +090020#include <linux/mfd/samsung/core.h>
21#include <linux/mfd/samsung/s5m8767.h>
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080022#include <linux/regulator/of_regulator.h>
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +010023#include <linux/regmap.h>
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -080024
25#define S5M8767_OPMODE_NORMAL_MODE 0x1
Sangbeom Kim9767ec72012-01-09 19:10:25 +090026
27struct s5m8767_info {
28 struct device *dev;
Sangbeom Kim63063bf2012-07-11 21:06:55 +090029 struct sec_pmic_dev *iodev;
Sangbeom Kim9767ec72012-01-09 19:10:25 +090030 int num_regulators;
31 struct regulator_dev **rdev;
Sangbeom Kim63063bf2012-07-11 21:06:55 +090032 struct sec_opmode_data *opmode;
Sangbeom Kim9767ec72012-01-09 19:10:25 +090033
34 int ramp_delay;
35 bool buck2_ramp;
36 bool buck3_ramp;
37 bool buck4_ramp;
38
39 bool buck2_gpiodvs;
40 bool buck3_gpiodvs;
41 bool buck4_gpiodvs;
42 u8 buck2_vol[8];
43 u8 buck3_vol[8];
44 u8 buck4_vol[8];
45 int buck_gpios[3];
Sangbeom Kimc848bc82012-06-18 09:49:20 +090046 int buck_ds[3];
Sangbeom Kim9767ec72012-01-09 19:10:25 +090047 int buck_gpioindex;
48};
49
Sangbeom Kim63063bf2012-07-11 21:06:55 +090050struct sec_voltage_desc {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090051 int max;
52 int min;
53 int step;
54};
55
Sangbeom Kim63063bf2012-07-11 21:06:55 +090056static const struct sec_voltage_desc buck_voltage_val1 = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090057 .max = 2225000,
58 .min = 650000,
59 .step = 6250,
60};
61
Sangbeom Kim63063bf2012-07-11 21:06:55 +090062static const struct sec_voltage_desc buck_voltage_val2 = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090063 .max = 1600000,
64 .min = 600000,
65 .step = 6250,
66};
67
Sangbeom Kim63063bf2012-07-11 21:06:55 +090068static const struct sec_voltage_desc buck_voltage_val3 = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090069 .max = 3000000,
70 .min = 750000,
71 .step = 12500,
72};
73
Sangbeom Kim63063bf2012-07-11 21:06:55 +090074static const struct sec_voltage_desc ldo_voltage_val1 = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090075 .max = 3950000,
76 .min = 800000,
77 .step = 50000,
78};
79
Sangbeom Kim63063bf2012-07-11 21:06:55 +090080static const struct sec_voltage_desc ldo_voltage_val2 = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090081 .max = 2375000,
82 .min = 800000,
83 .step = 25000,
84};
85
Sangbeom Kim63063bf2012-07-11 21:06:55 +090086static const struct sec_voltage_desc *reg_voltage_map[] = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +090087 [S5M8767_LDO1] = &ldo_voltage_val2,
88 [S5M8767_LDO2] = &ldo_voltage_val2,
89 [S5M8767_LDO3] = &ldo_voltage_val1,
90 [S5M8767_LDO4] = &ldo_voltage_val1,
91 [S5M8767_LDO5] = &ldo_voltage_val1,
92 [S5M8767_LDO6] = &ldo_voltage_val2,
93 [S5M8767_LDO7] = &ldo_voltage_val2,
94 [S5M8767_LDO8] = &ldo_voltage_val2,
95 [S5M8767_LDO9] = &ldo_voltage_val1,
96 [S5M8767_LDO10] = &ldo_voltage_val1,
97 [S5M8767_LDO11] = &ldo_voltage_val1,
98 [S5M8767_LDO12] = &ldo_voltage_val1,
99 [S5M8767_LDO13] = &ldo_voltage_val1,
100 [S5M8767_LDO14] = &ldo_voltage_val1,
101 [S5M8767_LDO15] = &ldo_voltage_val2,
102 [S5M8767_LDO16] = &ldo_voltage_val1,
103 [S5M8767_LDO17] = &ldo_voltage_val1,
104 [S5M8767_LDO18] = &ldo_voltage_val1,
105 [S5M8767_LDO19] = &ldo_voltage_val1,
106 [S5M8767_LDO20] = &ldo_voltage_val1,
107 [S5M8767_LDO21] = &ldo_voltage_val1,
108 [S5M8767_LDO22] = &ldo_voltage_val1,
109 [S5M8767_LDO23] = &ldo_voltage_val1,
110 [S5M8767_LDO24] = &ldo_voltage_val1,
111 [S5M8767_LDO25] = &ldo_voltage_val1,
112 [S5M8767_LDO26] = &ldo_voltage_val1,
113 [S5M8767_LDO27] = &ldo_voltage_val1,
114 [S5M8767_LDO28] = &ldo_voltage_val1,
115 [S5M8767_BUCK1] = &buck_voltage_val1,
116 [S5M8767_BUCK2] = &buck_voltage_val2,
117 [S5M8767_BUCK3] = &buck_voltage_val2,
118 [S5M8767_BUCK4] = &buck_voltage_val2,
119 [S5M8767_BUCK5] = &buck_voltage_val1,
120 [S5M8767_BUCK6] = &buck_voltage_val1,
Krzysztof Kozlowski463616e2013-12-10 10:09:43 +0100121 [S5M8767_BUCK7] = &buck_voltage_val3,
122 [S5M8767_BUCK8] = &buck_voltage_val3,
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900123 [S5M8767_BUCK9] = &buck_voltage_val3,
124};
125
Mark Brown5ceba7b2012-05-09 20:37:51 +0100126static unsigned int s5m8767_opmode_reg[][4] = {
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900127 /* {OFF, ON, LOWPOWER, SUSPEND} */
128 /* LDO1 ... LDO28 */
129 {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
130 {0x0, 0x3, 0x2, 0x1},
131 {0x0, 0x3, 0x2, 0x1},
132 {0x0, 0x0, 0x0, 0x0},
133 {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
134 {0x0, 0x3, 0x2, 0x1},
135 {0x0, 0x3, 0x2, 0x1},
136 {0x0, 0x3, 0x2, 0x1},
137 {0x0, 0x3, 0x2, 0x1},
138 {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
139 {0x0, 0x3, 0x2, 0x1},
140 {0x0, 0x3, 0x2, 0x1},
141 {0x0, 0x3, 0x2, 0x1},
142 {0x0, 0x3, 0x2, 0x1},
143 {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
144 {0x0, 0x3, 0x2, 0x1},
145 {0x0, 0x3, 0x2, 0x1},
146 {0x0, 0x0, 0x0, 0x0},
147 {0x0, 0x3, 0x2, 0x1},
148 {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
149 {0x0, 0x3, 0x2, 0x1},
150 {0x0, 0x3, 0x2, 0x1},
151 {0x0, 0x0, 0x0, 0x0},
152 {0x0, 0x3, 0x2, 0x1},
153 {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
154 {0x0, 0x3, 0x2, 0x1},
155 {0x0, 0x3, 0x2, 0x1},
156 {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
157
158 /* BUCK1 ... BUCK9 */
159 {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
160 {0x0, 0x3, 0x1, 0x1},
161 {0x0, 0x3, 0x1, 0x1},
162 {0x0, 0x3, 0x1, 0x1},
163 {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
164 {0x0, 0x3, 0x1, 0x1},
165 {0x0, 0x3, 0x1, 0x1},
166 {0x0, 0x3, 0x1, 0x1},
167 {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
168};
169
170static int s5m8767_get_register(struct regulator_dev *rdev, int *reg,
171 int *enable_ctrl)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900172{
Amit Daniel Kachhap9bb096f2012-12-10 18:19:39 +0530173 int i, reg_id = rdev_get_id(rdev);
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900174 unsigned int mode;
175 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900176
177 switch (reg_id) {
178 case S5M8767_LDO1 ... S5M8767_LDO2:
179 *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
180 break;
181 case S5M8767_LDO3 ... S5M8767_LDO28:
182 *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
183 break;
184 case S5M8767_BUCK1:
185 *reg = S5M8767_REG_BUCK1CTRL1;
186 break;
187 case S5M8767_BUCK2 ... S5M8767_BUCK4:
188 *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
189 break;
190 case S5M8767_BUCK5:
191 *reg = S5M8767_REG_BUCK5CTRL1;
192 break;
193 case S5M8767_BUCK6 ... S5M8767_BUCK9:
194 *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
195 break;
196 default:
197 return -EINVAL;
198 }
199
Amit Daniel Kachhap9bb096f2012-12-10 18:19:39 +0530200 for (i = 0; i < s5m8767->num_regulators; i++) {
201 if (s5m8767->opmode[i].id == reg_id) {
202 mode = s5m8767->opmode[i].mode;
203 break;
204 }
205 }
206
207 if (i < s5m8767->num_regulators)
208 *enable_ctrl =
209 s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;
210
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900211 return 0;
212}
213
214static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
215{
216 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
217 int ret, reg;
Krzysztof Kozlowski79b53d12013-12-10 10:09:42 +0100218 int enable_ctrl;
Inderpal Singh3ef30392012-12-12 08:57:00 +0530219 unsigned int val;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900220
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900221 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900222 if (ret == -EINVAL)
223 return 1;
224 else if (ret)
225 return ret;
226
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100227 ret = regmap_read(s5m8767->iodev->regmap_pmic, reg, &val);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900228 if (ret)
229 return ret;
230
Krzysztof Kozlowski79b53d12013-12-10 10:09:42 +0100231 return (val & S5M8767_ENCTRL_MASK) == enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900232}
233
234static int s5m8767_reg_enable(struct regulator_dev *rdev)
235{
236 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
237 int ret, reg;
Krzysztof Kozlowski79b53d12013-12-10 10:09:42 +0100238 int enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900239
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900240 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900241 if (ret)
242 return ret;
243
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100244 return regmap_update_bits(s5m8767->iodev->regmap_pmic, reg,
245 S5M8767_ENCTRL_MASK, enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900246}
247
248static int s5m8767_reg_disable(struct regulator_dev *rdev)
249{
250 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100251 int ret, reg, enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900252
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900253 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900254 if (ret)
255 return ret;
256
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100257 return regmap_update_bits(s5m8767->iodev->regmap_pmic, reg,
258 S5M8767_ENCTRL_MASK, ~S5M8767_ENCTRL_MASK);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900259}
260
Axel Lin31a932e2013-01-18 09:55:06 +0800261static int s5m8767_get_vsel_reg(int reg_id, struct s5m8767_info *s5m8767)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900262{
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900263 int reg;
264
265 switch (reg_id) {
266 case S5M8767_LDO1 ... S5M8767_LDO2:
267 reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
268 break;
269 case S5M8767_LDO3 ... S5M8767_LDO28:
270 reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
271 break;
272 case S5M8767_BUCK1:
273 reg = S5M8767_REG_BUCK1CTRL2;
274 break;
275 case S5M8767_BUCK2:
Amit Daniel Kachhapda130ab2012-12-10 18:19:40 +0530276 reg = S5M8767_REG_BUCK2DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800277 if (s5m8767->buck2_gpiodvs)
278 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900279 break;
280 case S5M8767_BUCK3:
Amit Daniel Kachhapda130ab2012-12-10 18:19:40 +0530281 reg = S5M8767_REG_BUCK3DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800282 if (s5m8767->buck3_gpiodvs)
283 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900284 break;
285 case S5M8767_BUCK4:
Amit Daniel Kachhapda130ab2012-12-10 18:19:40 +0530286 reg = S5M8767_REG_BUCK4DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800287 if (s5m8767->buck4_gpiodvs)
288 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900289 break;
290 case S5M8767_BUCK5:
291 reg = S5M8767_REG_BUCK5CTRL2;
292 break;
293 case S5M8767_BUCK6 ... S5M8767_BUCK9:
294 reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
295 break;
296 default:
297 return -EINVAL;
298 }
299
Axel Lin31a932e2013-01-18 09:55:06 +0800300 return reg;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900301}
302
Axel Lin854f73e2012-12-29 09:38:50 +0800303static int s5m8767_convert_voltage_to_sel(const struct sec_voltage_desc *desc,
304 int min_vol)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900305{
Axel Lin5b5e9772012-03-09 11:31:08 +0800306 int selector = 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900307
308 if (desc == NULL)
309 return -EINVAL;
310
Axel Lin854f73e2012-12-29 09:38:50 +0800311 if (min_vol > desc->max)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900312 return -EINVAL;
313
Axel Lin94e85a32012-04-13 09:30:05 +0800314 if (min_vol < desc->min)
315 min_vol = desc->min;
316
317 selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900318
Axel Lin854f73e2012-12-29 09:38:50 +0800319 if (desc->min + desc->step * selector > desc->max)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900320 return -EINVAL;
321
Axel Lin5b5e9772012-03-09 11:31:08 +0800322 return selector;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900323}
324
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900325static inline int s5m8767_set_high(struct s5m8767_info *s5m8767)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900326{
327 int temp_index = s5m8767->buck_gpioindex;
328
329 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
330 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
331 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900332
333 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900334}
335
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900336static inline int s5m8767_set_low(struct s5m8767_info *s5m8767)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900337{
338 int temp_index = s5m8767->buck_gpioindex;
339
340 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
341 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
342 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900343
344 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900345}
346
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900347static int s5m8767_set_voltage_sel(struct regulator_dev *rdev,
348 unsigned selector)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900349{
350 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin321d2ab2012-04-10 14:05:44 +0800351 int reg_id = rdev_get_id(rdev);
Axel Lin31a932e2013-01-18 09:55:06 +0800352 int old_index, index = 0;
Axel Lin321d2ab2012-04-10 14:05:44 +0800353 u8 *buck234_vol = NULL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900354
355 switch (reg_id) {
Axel Lin321d2ab2012-04-10 14:05:44 +0800356 case S5M8767_LDO1 ... S5M8767_LDO28:
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900357 break;
Axel Lin321d2ab2012-04-10 14:05:44 +0800358 case S5M8767_BUCK1 ... S5M8767_BUCK6:
Axel Lin321d2ab2012-04-10 14:05:44 +0800359 if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
360 buck234_vol = &s5m8767->buck2_vol[0];
361 else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
362 buck234_vol = &s5m8767->buck3_vol[0];
363 else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
364 buck234_vol = &s5m8767->buck4_vol[0];
365 break;
366 case S5M8767_BUCK7 ... S5M8767_BUCK8:
367 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900368 case S5M8767_BUCK9:
Axel Lin321d2ab2012-04-10 14:05:44 +0800369 break;
370 default:
371 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900372 }
373
Axel Lin321d2ab2012-04-10 14:05:44 +0800374 /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
375 if (buck234_vol) {
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900376 while (*buck234_vol != selector) {
Axel Lin321d2ab2012-04-10 14:05:44 +0800377 buck234_vol++;
378 index++;
379 }
380 old_index = s5m8767->buck_gpioindex;
381 s5m8767->buck_gpioindex = index;
382
383 if (index > old_index)
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900384 return s5m8767_set_high(s5m8767);
Axel Lin321d2ab2012-04-10 14:05:44 +0800385 else
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900386 return s5m8767_set_low(s5m8767);
Axel Lin321d2ab2012-04-10 14:05:44 +0800387 } else {
Axel Lin31a932e2013-01-18 09:55:06 +0800388 return regulator_set_voltage_sel_regmap(rdev, selector);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900389 }
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900390}
391
392static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
393 unsigned int old_sel,
394 unsigned int new_sel)
395{
396 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900397 const struct sec_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800398 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900399
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900400 desc = reg_voltage_map[reg_id];
401
Sangbeom Kim9d88fc02012-06-13 17:27:16 +0900402 if ((old_sel < new_sel) && s5m8767->ramp_delay)
Axel Lin89e0f0e2012-02-24 14:52:45 +0800403 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
Sangbeom Kim0f8b9c72012-03-09 16:28:10 +0900404 s5m8767->ramp_delay * 1000);
Axel Lin89e0f0e2012-02-24 14:52:45 +0800405 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900406}
407
Axel Lind35aad02012-04-10 14:07:15 +0800408static struct regulator_ops s5m8767_ops = {
Axel Line2eb1692012-06-18 14:25:27 +0800409 .list_voltage = regulator_list_voltage_linear,
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900410 .is_enabled = s5m8767_reg_is_enabled,
411 .enable = s5m8767_reg_enable,
412 .disable = s5m8767_reg_disable,
Axel Lin31a932e2013-01-18 09:55:06 +0800413 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Sangbeom Kimdf2643c2012-06-13 17:28:18 +0900414 .set_voltage_sel = s5m8767_set_voltage_sel,
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900415 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
416};
417
Axel Line2eb1692012-06-18 14:25:27 +0800418static struct regulator_ops s5m8767_buck78_ops = {
Krzysztof Kozlowski463616e2013-12-10 10:09:43 +0100419 .list_voltage = regulator_list_voltage_linear,
Axel Line2eb1692012-06-18 14:25:27 +0800420 .is_enabled = s5m8767_reg_is_enabled,
421 .enable = s5m8767_reg_enable,
422 .disable = s5m8767_reg_disable,
Krzysztof Kozlowski463616e2013-12-10 10:09:43 +0100423 .get_voltage_sel = regulator_get_voltage_sel_regmap,
424 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Axel Line2eb1692012-06-18 14:25:27 +0800425};
426
Axel Lin65896e72012-04-10 14:08:19 +0800427#define s5m8767_regulator_desc(_name) { \
428 .name = #_name, \
429 .id = S5M8767_##_name, \
430 .ops = &s5m8767_ops, \
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900431 .type = REGULATOR_VOLTAGE, \
432 .owner = THIS_MODULE, \
433}
434
Axel Line2eb1692012-06-18 14:25:27 +0800435#define s5m8767_regulator_buck78_desc(_name) { \
436 .name = #_name, \
437 .id = S5M8767_##_name, \
438 .ops = &s5m8767_buck78_ops, \
439 .type = REGULATOR_VOLTAGE, \
440 .owner = THIS_MODULE, \
441}
442
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900443static struct regulator_desc regulators[] = {
Axel Lin65896e72012-04-10 14:08:19 +0800444 s5m8767_regulator_desc(LDO1),
445 s5m8767_regulator_desc(LDO2),
446 s5m8767_regulator_desc(LDO3),
447 s5m8767_regulator_desc(LDO4),
448 s5m8767_regulator_desc(LDO5),
449 s5m8767_regulator_desc(LDO6),
450 s5m8767_regulator_desc(LDO7),
451 s5m8767_regulator_desc(LDO8),
452 s5m8767_regulator_desc(LDO9),
453 s5m8767_regulator_desc(LDO10),
454 s5m8767_regulator_desc(LDO11),
455 s5m8767_regulator_desc(LDO12),
456 s5m8767_regulator_desc(LDO13),
457 s5m8767_regulator_desc(LDO14),
458 s5m8767_regulator_desc(LDO15),
459 s5m8767_regulator_desc(LDO16),
460 s5m8767_regulator_desc(LDO17),
461 s5m8767_regulator_desc(LDO18),
462 s5m8767_regulator_desc(LDO19),
463 s5m8767_regulator_desc(LDO20),
464 s5m8767_regulator_desc(LDO21),
465 s5m8767_regulator_desc(LDO22),
466 s5m8767_regulator_desc(LDO23),
467 s5m8767_regulator_desc(LDO24),
468 s5m8767_regulator_desc(LDO25),
469 s5m8767_regulator_desc(LDO26),
470 s5m8767_regulator_desc(LDO27),
471 s5m8767_regulator_desc(LDO28),
472 s5m8767_regulator_desc(BUCK1),
473 s5m8767_regulator_desc(BUCK2),
474 s5m8767_regulator_desc(BUCK3),
475 s5m8767_regulator_desc(BUCK4),
476 s5m8767_regulator_desc(BUCK5),
477 s5m8767_regulator_desc(BUCK6),
Axel Line2eb1692012-06-18 14:25:27 +0800478 s5m8767_regulator_buck78_desc(BUCK7),
479 s5m8767_regulator_buck78_desc(BUCK8),
Axel Lin65896e72012-04-10 14:08:19 +0800480 s5m8767_regulator_desc(BUCK9),
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900481};
482
Krzysztof Kozlowskiee1e0992014-01-24 14:37:57 +0100483/*
484 * Enable GPIO control over BUCK9 in regulator_config for that regulator.
485 */
486static void s5m8767_regulator_config_ext_control(struct s5m8767_info *s5m8767,
487 struct sec_regulator_data *rdata,
488 struct regulator_config *config)
489{
490 int i, mode = 0;
491
492 if (rdata->id != S5M8767_BUCK9)
493 return;
494
495 /* Check if opmode for regulator matches S5M8767_ENCTRL_USE_GPIO */
496 for (i = 0; i < s5m8767->num_regulators; i++) {
497 const struct sec_opmode_data *opmode = &s5m8767->opmode[i];
498 if (opmode->id == rdata->id) {
499 mode = s5m8767_opmode_reg[rdata->id][opmode->mode];
500 break;
501 }
502 }
503 if (mode != S5M8767_ENCTRL_USE_GPIO) {
504 dev_warn(s5m8767->dev,
505 "ext-control for %s: mismatched op_mode (%x), ignoring\n",
506 rdata->reg_node->name, mode);
507 return;
508 }
509
510 if (!gpio_is_valid(rdata->ext_control_gpio)) {
511 dev_warn(s5m8767->dev,
512 "ext-control for %s: GPIO not valid, ignoring\n",
513 rdata->reg_node->name);
514 return;
515 }
516
517 config->ena_gpio = rdata->ext_control_gpio;
518 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
519}
520
521/*
522 * Turn on GPIO control over BUCK9.
523 */
524static int s5m8767_enable_ext_control(struct s5m8767_info *s5m8767,
525 struct regulator_dev *rdev)
526{
527 int ret, reg, enable_ctrl;
528
529 if (rdev_get_id(rdev) != S5M8767_BUCK9)
530 return -EINVAL;
531
532 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
533 if (ret)
534 return ret;
535
536 return regmap_update_bits(s5m8767->iodev->regmap_pmic,
537 reg, S5M8767_ENCTRL_MASK,
538 S5M8767_ENCTRL_USE_GPIO << S5M8767_ENCTRL_SHIFT);
539}
540
541
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800542#ifdef CONFIG_OF
543static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev *iodev,
544 struct sec_platform_data *pdata,
545 struct device_node *pmic_np)
546{
547 int i, gpio;
548
549 for (i = 0; i < 3; i++) {
550 gpio = of_get_named_gpio(pmic_np,
551 "s5m8767,pmic-buck-dvs-gpios", i);
552 if (!gpio_is_valid(gpio)) {
553 dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
554 return -EINVAL;
555 }
556 pdata->buck_gpios[i] = gpio;
557 }
558 return 0;
559}
560
561static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev *iodev,
562 struct sec_platform_data *pdata,
563 struct device_node *pmic_np)
564{
565 int i, gpio;
566
567 for (i = 0; i < 3; i++) {
568 gpio = of_get_named_gpio(pmic_np,
569 "s5m8767,pmic-buck-ds-gpios", i);
570 if (!gpio_is_valid(gpio)) {
571 dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
572 return -EINVAL;
573 }
574 pdata->buck_ds[i] = gpio;
575 }
576 return 0;
577}
578
Krzysztof Kozlowskiee1e0992014-01-24 14:37:57 +0100579static void s5m8767_pmic_dt_parse_ext_control_gpio(struct sec_pmic_dev *iodev,
580 struct sec_regulator_data *rdata,
581 struct device_node *reg_np)
582{
583 rdata->ext_control_gpio = of_get_named_gpio(reg_np,
584 "s5m8767,pmic-ext-control-gpios", 0);
585 if (!gpio_is_valid(rdata->ext_control_gpio))
586 rdata->ext_control_gpio = 0;
587}
588
Axel Lincbb0ed42013-02-13 09:29:45 +0800589static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800590 struct sec_platform_data *pdata)
591{
Axel Lincbb0ed42013-02-13 09:29:45 +0800592 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800593 struct device_node *pmic_np, *regulators_np, *reg_np;
594 struct sec_regulator_data *rdata;
595 struct sec_opmode_data *rmode;
Chanwoo Choi04f9f062013-10-10 10:41:36 +0900596 unsigned int i, dvs_voltage_nr = 8, ret;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800597
598 pmic_np = iodev->dev->of_node;
599 if (!pmic_np) {
600 dev_err(iodev->dev, "could not find pmic sub-node\n");
601 return -ENODEV;
602 }
603
604 regulators_np = of_find_node_by_name(pmic_np, "regulators");
605 if (!regulators_np) {
606 dev_err(iodev->dev, "could not find regulators sub-node\n");
607 return -EINVAL;
608 }
609
610 /* count the number of regulators to be supported in pmic */
Axel Lin1f91b6f2013-02-06 10:55:05 +0800611 pdata->num_regulators = of_get_child_count(regulators_np);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800612
Axel Lincbb0ed42013-02-13 09:29:45 +0800613 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800614 pdata->num_regulators, GFP_KERNEL);
Sachin Kamat4754b422014-02-20 14:23:11 +0530615 if (!rdata)
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800616 return -ENOMEM;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800617
Axel Lincbb0ed42013-02-13 09:29:45 +0800618 rmode = devm_kzalloc(&pdev->dev, sizeof(*rmode) *
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800619 pdata->num_regulators, GFP_KERNEL);
Sachin Kamat4754b422014-02-20 14:23:11 +0530620 if (!rmode)
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800621 return -ENOMEM;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800622
623 pdata->regulators = rdata;
624 pdata->opmode = rmode;
625 for_each_child_of_node(regulators_np, reg_np) {
626 for (i = 0; i < ARRAY_SIZE(regulators); i++)
627 if (!of_node_cmp(reg_np->name, regulators[i].name))
628 break;
629
630 if (i == ARRAY_SIZE(regulators)) {
631 dev_warn(iodev->dev,
632 "don't know how to configure regulator %s\n",
633 reg_np->name);
634 continue;
635 }
636
Krzysztof Kozlowskiee1e0992014-01-24 14:37:57 +0100637 s5m8767_pmic_dt_parse_ext_control_gpio(iodev, rdata, reg_np);
638
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800639 rdata->id = i;
640 rdata->initdata = of_get_regulator_init_data(
Axel Lincbb0ed42013-02-13 09:29:45 +0800641 &pdev->dev, reg_np);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800642 rdata->reg_node = reg_np;
643 rdata++;
644 rmode->id = i;
645 if (of_property_read_u32(reg_np, "op_mode",
646 &rmode->mode)) {
647 dev_warn(iodev->dev,
648 "no op_mode property property at %s\n",
649 reg_np->full_name);
650
651 rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
652 }
653 rmode++;
654 }
655
Chanwoo Choi04f9f062013-10-10 10:41:36 +0900656 if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800657 pdata->buck2_gpiodvs = true;
658
Chanwoo Choi04f9f062013-10-10 10:41:36 +0900659 if (of_property_read_u32_array(pmic_np,
660 "s5m8767,pmic-buck2-dvs-voltage",
661 pdata->buck2_voltage, dvs_voltage_nr)) {
662 dev_err(iodev->dev, "buck2 voltages not specified\n");
663 return -EINVAL;
664 }
665 }
666
667 if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800668 pdata->buck3_gpiodvs = true;
669
Chanwoo Choi04f9f062013-10-10 10:41:36 +0900670 if (of_property_read_u32_array(pmic_np,
671 "s5m8767,pmic-buck3-dvs-voltage",
672 pdata->buck3_voltage, dvs_voltage_nr)) {
673 dev_err(iodev->dev, "buck3 voltages not specified\n");
674 return -EINVAL;
675 }
676 }
677
678 if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800679 pdata->buck4_gpiodvs = true;
680
Chanwoo Choi04f9f062013-10-10 10:41:36 +0900681 if (of_property_read_u32_array(pmic_np,
682 "s5m8767,pmic-buck4-dvs-voltage",
683 pdata->buck4_voltage, dvs_voltage_nr)) {
684 dev_err(iodev->dev, "buck4 voltages not specified\n");
685 return -EINVAL;
686 }
687 }
688
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800689 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
690 pdata->buck4_gpiodvs) {
691 ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
692 if (ret)
693 return -EINVAL;
694
695 if (of_property_read_u32(pmic_np,
696 "s5m8767,pmic-buck-default-dvs-idx",
697 &pdata->buck_default_idx)) {
698 pdata->buck_default_idx = 0;
699 } else {
700 if (pdata->buck_default_idx >= 8) {
701 pdata->buck_default_idx = 0;
702 dev_info(iodev->dev,
703 "invalid value for default dvs index, use 0\n");
704 }
705 }
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800706 }
707
708 ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
709 if (ret)
710 return -EINVAL;
711
Chanwoo Choi033054e2013-10-10 10:41:35 +0900712 if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
713 pdata->buck2_ramp_enable = true;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800714
Chanwoo Choi033054e2013-10-10 10:41:35 +0900715 if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
716 pdata->buck3_ramp_enable = true;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800717
Chanwoo Choi033054e2013-10-10 10:41:35 +0900718 if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
719 pdata->buck4_ramp_enable = true;
720
721 if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
722 || pdata->buck4_ramp_enable) {
723 if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
724 &pdata->buck_ramp_delay))
725 pdata->buck_ramp_delay = 0;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800726 }
727
728 return 0;
729}
730#else
Axel Lincbb0ed42013-02-13 09:29:45 +0800731static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800732 struct sec_platform_data *pdata)
733{
734 return 0;
735}
736#endif /* CONFIG_OF */
737
Bill Pembertona5023572012-11-19 13:22:22 -0500738static int s5m8767_pmic_probe(struct platform_device *pdev)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900739{
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900740 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800741 struct sec_platform_data *pdata = iodev->pdata;
Mark Brownc1727082012-04-04 00:50:22 +0100742 struct regulator_config config = { };
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900743 struct regulator_dev **rdev;
744 struct s5m8767_info *s5m8767;
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900745 int i, ret, size, buck_init;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900746
Axel Line81d7bc2013-02-13 09:31:31 +0800747 if (!pdata) {
748 dev_err(pdev->dev.parent, "Platform data not supplied\n");
749 return -ENODEV;
750 }
751
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800752 if (iodev->dev->of_node) {
Axel Lincbb0ed42013-02-13 09:29:45 +0800753 ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -0800754 if (ret)
755 return ret;
756 }
757
Axel Lin6c4efe22012-03-10 08:43:02 +0800758 if (pdata->buck2_gpiodvs) {
759 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
760 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
761 return -EINVAL;
762 }
763 }
764
765 if (pdata->buck3_gpiodvs) {
766 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
767 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
768 return -EINVAL;
769 }
770 }
771
772 if (pdata->buck4_gpiodvs) {
773 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
774 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
775 return -EINVAL;
776 }
777 }
778
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900779 s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
780 GFP_KERNEL);
781 if (!s5m8767)
782 return -ENOMEM;
783
784 size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
785 s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
786 if (!s5m8767->rdev)
787 return -ENOMEM;
788
789 rdev = s5m8767->rdev;
790 s5m8767->dev = &pdev->dev;
791 s5m8767->iodev = iodev;
Amit Daniel Kachhap9bb096f2012-12-10 18:19:39 +0530792 s5m8767->num_regulators = pdata->num_regulators;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900793 platform_set_drvdata(pdev, s5m8767);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900794
795 s5m8767->buck_gpioindex = pdata->buck_default_idx;
796 s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
797 s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
798 s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
799 s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
800 s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
801 s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900802 s5m8767->buck_ds[0] = pdata->buck_ds[0];
803 s5m8767->buck_ds[1] = pdata->buck_ds[1];
804 s5m8767->buck_ds[2] = pdata->buck_ds[2];
805
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900806 s5m8767->ramp_delay = pdata->buck_ramp_delay;
807 s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
808 s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
809 s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900810 s5m8767->opmode = pdata->opmode;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900811
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900812 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800813 pdata->buck2_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900814
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100815 regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK2DVS2,
816 buck_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900817
818 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800819 pdata->buck3_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900820
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100821 regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK3DVS2,
822 buck_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900823
824 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800825 pdata->buck4_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900826
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100827 regmap_write(s5m8767->iodev->regmap_pmic, S5M8767_REG_BUCK4DVS2,
828 buck_init);
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900829
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900830 for (i = 0; i < 8; i++) {
831 if (s5m8767->buck2_gpiodvs) {
832 s5m8767->buck2_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800833 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900834 &buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800835 pdata->buck2_voltage[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900836 }
837
838 if (s5m8767->buck3_gpiodvs) {
839 s5m8767->buck3_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800840 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900841 &buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800842 pdata->buck3_voltage[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900843 }
844
845 if (s5m8767->buck4_gpiodvs) {
846 s5m8767->buck4_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800847 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900848 &buck_voltage_val2,
Axel Lin854f73e2012-12-29 09:38:50 +0800849 pdata->buck4_voltage[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900850 }
851 }
852
Amit Daniel Kachhap76c854d2012-12-10 18:19:41 +0530853 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
854 pdata->buck4_gpiodvs) {
855
856 if (!gpio_is_valid(pdata->buck_gpios[0]) ||
857 !gpio_is_valid(pdata->buck_gpios[1]) ||
858 !gpio_is_valid(pdata->buck_gpios[2])) {
859 dev_err(&pdev->dev, "GPIO NOT VALID\n");
860 return -EINVAL;
861 }
862
Axel Lin5febb3c2012-07-05 23:12:49 +0800863 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
864 "S5M8767 SET1");
865 if (ret)
866 return ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900867
Axel Lin5febb3c2012-07-05 23:12:49 +0800868 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
869 "S5M8767 SET2");
870 if (ret)
871 return ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900872
Axel Lin5febb3c2012-07-05 23:12:49 +0800873 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
874 "S5M8767 SET3");
875 if (ret)
876 return ret;
877
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900878 /* SET1 GPIO */
879 gpio_direction_output(pdata->buck_gpios[0],
880 (s5m8767->buck_gpioindex >> 2) & 0x1);
881 /* SET2 GPIO */
882 gpio_direction_output(pdata->buck_gpios[1],
883 (s5m8767->buck_gpioindex >> 1) & 0x1);
884 /* SET3 GPIO */
885 gpio_direction_output(pdata->buck_gpios[2],
886 (s5m8767->buck_gpioindex >> 0) & 0x1);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900887 }
888
Axel Lin5febb3c2012-07-05 23:12:49 +0800889 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
890 if (ret)
891 return ret;
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900892
Axel Lin5febb3c2012-07-05 23:12:49 +0800893 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
894 if (ret)
895 return ret;
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900896
Axel Lin5febb3c2012-07-05 23:12:49 +0800897 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
898 if (ret)
899 return ret;
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900900
901 /* DS2 GPIO */
902 gpio_direction_output(pdata->buck_ds[0], 0x0);
903 /* DS3 GPIO */
904 gpio_direction_output(pdata->buck_ds[1], 0x0);
905 /* DS4 GPIO */
906 gpio_direction_output(pdata->buck_ds[2], 0x0);
907
908 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
909 pdata->buck4_gpiodvs) {
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100910 regmap_update_bits(s5m8767->iodev->regmap_pmic,
911 S5M8767_REG_BUCK2CTRL, 1 << 1,
912 (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1));
913 regmap_update_bits(s5m8767->iodev->regmap_pmic,
914 S5M8767_REG_BUCK3CTRL, 1 << 1,
915 (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1));
916 regmap_update_bits(s5m8767->iodev->regmap_pmic,
917 S5M8767_REG_BUCK4CTRL, 1 << 1,
918 (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1));
Sangbeom Kimc848bc82012-06-18 09:49:20 +0900919 }
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900920
921 /* Initialize GPIO DVS registers */
922 for (i = 0; i < 8; i++) {
923 if (s5m8767->buck2_gpiodvs) {
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100924 regmap_write(s5m8767->iodev->regmap_pmic,
925 S5M8767_REG_BUCK2DVS1 + i,
926 s5m8767->buck2_vol[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900927 }
928
929 if (s5m8767->buck3_gpiodvs) {
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100930 regmap_write(s5m8767->iodev->regmap_pmic,
931 S5M8767_REG_BUCK3DVS1 + i,
932 s5m8767->buck3_vol[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900933 }
934
935 if (s5m8767->buck4_gpiodvs) {
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100936 regmap_write(s5m8767->iodev->regmap_pmic,
937 S5M8767_REG_BUCK4DVS1 + i,
938 s5m8767->buck4_vol[i]);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900939 }
940 }
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900941
942 if (s5m8767->buck2_ramp)
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100943 regmap_update_bits(s5m8767->iodev->regmap_pmic,
944 S5M8767_REG_DVSRAMP, 0x08, 0x08);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900945
946 if (s5m8767->buck3_ramp)
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100947 regmap_update_bits(s5m8767->iodev->regmap_pmic,
948 S5M8767_REG_DVSRAMP, 0x04, 0x04);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900949
950 if (s5m8767->buck4_ramp)
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100951 regmap_update_bits(s5m8767->iodev->regmap_pmic,
952 S5M8767_REG_DVSRAMP, 0x02, 0x02);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900953
954 if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
955 || s5m8767->buck4_ramp) {
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100956 unsigned int val;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900957 switch (s5m8767->ramp_delay) {
Sangbeom Kim1af142c2012-06-13 17:27:54 +0900958 case 5:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100959 val = S5M8767_DVS_BUCK_RAMP_5;
Sangbeom Kim1af142c2012-06-13 17:27:54 +0900960 break;
961 case 10:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100962 val = S5M8767_DVS_BUCK_RAMP_10;
Axel Lin047ec222012-01-12 14:57:09 +0800963 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900964 case 25:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100965 val = S5M8767_DVS_BUCK_RAMP_25;
Axel Lin047ec222012-01-12 14:57:09 +0800966 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900967 case 50:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100968 val = S5M8767_DVS_BUCK_RAMP_50;
Axel Lin047ec222012-01-12 14:57:09 +0800969 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900970 case 100:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100971 val = S5M8767_DVS_BUCK_RAMP_100;
Axel Lin047ec222012-01-12 14:57:09 +0800972 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900973 default:
Krzysztof Kozlowskif37ff6b2013-12-17 14:23:17 +0100974 val = S5M8767_DVS_BUCK_RAMP_10;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900975 }
Krzysztof Kozlowskid13733f2013-12-17 14:23:18 +0100976 regmap_update_bits(s5m8767->iodev->regmap_pmic,
977 S5M8767_REG_DVSRAMP,
978 S5M8767_DVS_BUCK_RAMP_MASK,
979 val << S5M8767_DVS_BUCK_RAMP_SHIFT);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900980 }
981
982 for (i = 0; i < pdata->num_regulators; i++) {
Sangbeom Kim63063bf2012-07-11 21:06:55 +0900983 const struct sec_voltage_desc *desc;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900984 int id = pdata->regulators[i].id;
985
986 desc = reg_voltage_map[id];
Axel Line2eb1692012-06-18 14:25:27 +0800987 if (desc) {
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900988 regulators[id].n_voltages =
989 (desc->max - desc->min) / desc->step + 1;
Axel Line2eb1692012-06-18 14:25:27 +0800990 regulators[id].min_uV = desc->min;
991 regulators[id].uV_step = desc->step;
Axel Lin31a932e2013-01-18 09:55:06 +0800992 regulators[id].vsel_reg =
993 s5m8767_get_vsel_reg(id, s5m8767);
994 if (id < S5M8767_BUCK1)
995 regulators[id].vsel_mask = 0x3f;
996 else
997 regulators[id].vsel_mask = 0xff;
Axel Line2eb1692012-06-18 14:25:27 +0800998 }
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900999
Mark Brownc1727082012-04-04 00:50:22 +01001000 config.dev = s5m8767->dev;
1001 config.init_data = pdata->regulators[i].initdata;
1002 config.driver_data = s5m8767;
Krzysztof Kozlowski3e1e4a52013-12-12 17:12:31 -08001003 config.regmap = iodev->regmap_pmic;
Amit Daniel Kachhap26aec0092013-02-03 15:49:47 -08001004 config.of_node = pdata->regulators[i].reg_node;
Krzysztof Kozlowskiee1e0992014-01-24 14:37:57 +01001005 if (pdata->regulators[i].ext_control_gpio)
1006 s5m8767_regulator_config_ext_control(s5m8767,
1007 &pdata->regulators[i], &config);
Mark Brownc1727082012-04-04 00:50:22 +01001008
Mark Brownf0db4752013-08-31 11:57:34 +01001009 rdev[i] = devm_regulator_register(&pdev->dev, &regulators[id],
1010 &config);
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001011 if (IS_ERR(rdev[i])) {
1012 ret = PTR_ERR(rdev[i]);
1013 dev_err(s5m8767->dev, "regulator init failed for %d\n",
1014 id);
Mark Brownf0db4752013-08-31 11:57:34 +01001015 return ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001016 }
Krzysztof Kozlowskiee1e0992014-01-24 14:37:57 +01001017
1018 if (pdata->regulators[i].ext_control_gpio) {
1019 ret = s5m8767_enable_ext_control(s5m8767, rdev[i]);
1020 if (ret < 0) {
1021 dev_err(s5m8767->dev,
1022 "failed to enable gpio control over %s: %d\n",
1023 rdev[i]->desc->name, ret);
1024 return ret;
1025 }
1026 }
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001027 }
1028
1029 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001030}
1031
1032static const struct platform_device_id s5m8767_pmic_id[] = {
1033 { "s5m8767-pmic", 0},
1034 { },
1035};
1036MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
1037
1038static struct platform_driver s5m8767_pmic_driver = {
1039 .driver = {
1040 .name = "s5m8767-pmic",
1041 .owner = THIS_MODULE,
1042 },
1043 .probe = s5m8767_pmic_probe,
Sangbeom Kim9767ec72012-01-09 19:10:25 +09001044 .id_table = s5m8767_pmic_id,
1045};
1046
1047static int __init s5m8767_pmic_init(void)
1048{
1049 return platform_driver_register(&s5m8767_pmic_driver);
1050}
1051subsys_initcall(s5m8767_pmic_init);
1052
1053static void __exit s5m8767_pmic_exit(void)
1054{
1055 platform_driver_unregister(&s5m8767_pmic_driver);
1056}
1057module_exit(s5m8767_pmic_exit);
1058
1059/* Module information */
1060MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1061MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
1062MODULE_LICENSE("GPL");