blob: 14b06c8d378f1c0a93e080d4719ec20124efcf45 [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
14#include <linux/bug.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/gpio.h>
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
23#include <linux/mfd/s5m87xx/s5m-core.h>
24#include <linux/mfd/s5m87xx/s5m-pmic.h>
25
26struct s5m8767_info {
27 struct device *dev;
28 struct s5m87xx_dev *iodev;
29 int num_regulators;
30 struct regulator_dev **rdev;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +090031 struct s5m_opmode_data *opmode;
Sangbeom Kim9767ec72012-01-09 19:10:25 +090032
33 int ramp_delay;
34 bool buck2_ramp;
35 bool buck3_ramp;
36 bool buck4_ramp;
37
38 bool buck2_gpiodvs;
39 bool buck3_gpiodvs;
40 bool buck4_gpiodvs;
41 u8 buck2_vol[8];
42 u8 buck3_vol[8];
43 u8 buck4_vol[8];
44 int buck_gpios[3];
45 int buck_gpioindex;
46};
47
48struct s5m_voltage_desc {
49 int max;
50 int min;
51 int step;
52};
53
54static const struct s5m_voltage_desc buck_voltage_val1 = {
55 .max = 2225000,
56 .min = 650000,
57 .step = 6250,
58};
59
60static const struct s5m_voltage_desc buck_voltage_val2 = {
61 .max = 1600000,
62 .min = 600000,
63 .step = 6250,
64};
65
66static const struct s5m_voltage_desc buck_voltage_val3 = {
67 .max = 3000000,
68 .min = 750000,
69 .step = 12500,
70};
71
72static const struct s5m_voltage_desc ldo_voltage_val1 = {
73 .max = 3950000,
74 .min = 800000,
75 .step = 50000,
76};
77
78static const struct s5m_voltage_desc ldo_voltage_val2 = {
79 .max = 2375000,
80 .min = 800000,
81 .step = 25000,
82};
83
84static const struct s5m_voltage_desc *reg_voltage_map[] = {
85 [S5M8767_LDO1] = &ldo_voltage_val2,
86 [S5M8767_LDO2] = &ldo_voltage_val2,
87 [S5M8767_LDO3] = &ldo_voltage_val1,
88 [S5M8767_LDO4] = &ldo_voltage_val1,
89 [S5M8767_LDO5] = &ldo_voltage_val1,
90 [S5M8767_LDO6] = &ldo_voltage_val2,
91 [S5M8767_LDO7] = &ldo_voltage_val2,
92 [S5M8767_LDO8] = &ldo_voltage_val2,
93 [S5M8767_LDO9] = &ldo_voltage_val1,
94 [S5M8767_LDO10] = &ldo_voltage_val1,
95 [S5M8767_LDO11] = &ldo_voltage_val1,
96 [S5M8767_LDO12] = &ldo_voltage_val1,
97 [S5M8767_LDO13] = &ldo_voltage_val1,
98 [S5M8767_LDO14] = &ldo_voltage_val1,
99 [S5M8767_LDO15] = &ldo_voltage_val2,
100 [S5M8767_LDO16] = &ldo_voltage_val1,
101 [S5M8767_LDO17] = &ldo_voltage_val1,
102 [S5M8767_LDO18] = &ldo_voltage_val1,
103 [S5M8767_LDO19] = &ldo_voltage_val1,
104 [S5M8767_LDO20] = &ldo_voltage_val1,
105 [S5M8767_LDO21] = &ldo_voltage_val1,
106 [S5M8767_LDO22] = &ldo_voltage_val1,
107 [S5M8767_LDO23] = &ldo_voltage_val1,
108 [S5M8767_LDO24] = &ldo_voltage_val1,
109 [S5M8767_LDO25] = &ldo_voltage_val1,
110 [S5M8767_LDO26] = &ldo_voltage_val1,
111 [S5M8767_LDO27] = &ldo_voltage_val1,
112 [S5M8767_LDO28] = &ldo_voltage_val1,
113 [S5M8767_BUCK1] = &buck_voltage_val1,
114 [S5M8767_BUCK2] = &buck_voltage_val2,
115 [S5M8767_BUCK3] = &buck_voltage_val2,
116 [S5M8767_BUCK4] = &buck_voltage_val2,
117 [S5M8767_BUCK5] = &buck_voltage_val1,
118 [S5M8767_BUCK6] = &buck_voltage_val1,
119 [S5M8767_BUCK7] = NULL,
120 [S5M8767_BUCK8] = NULL,
121 [S5M8767_BUCK9] = &buck_voltage_val3,
122};
123
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900124static int s5m8767_list_voltage(struct regulator_dev *rdev,
125 unsigned int selector)
126{
127 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800128 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900129 int val;
130
131 if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0)
132 return -EINVAL;
133
134 desc = reg_voltage_map[reg_id];
135 if (desc == NULL)
136 return -EINVAL;
137
138 val = desc->min + desc->step * selector;
139 if (val > desc->max)
140 return -EINVAL;
141
142 return val;
143}
144
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900145unsigned int s5m8767_opmode_reg[][4] = {
146 /* {OFF, ON, LOWPOWER, SUSPEND} */
147 /* LDO1 ... LDO28 */
148 {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
149 {0x0, 0x3, 0x2, 0x1},
150 {0x0, 0x3, 0x2, 0x1},
151 {0x0, 0x0, 0x0, 0x0},
152 {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
153 {0x0, 0x3, 0x2, 0x1},
154 {0x0, 0x3, 0x2, 0x1},
155 {0x0, 0x3, 0x2, 0x1},
156 {0x0, 0x3, 0x2, 0x1},
157 {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
158 {0x0, 0x3, 0x2, 0x1},
159 {0x0, 0x3, 0x2, 0x1},
160 {0x0, 0x3, 0x2, 0x1},
161 {0x0, 0x3, 0x2, 0x1},
162 {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
163 {0x0, 0x3, 0x2, 0x1},
164 {0x0, 0x3, 0x2, 0x1},
165 {0x0, 0x0, 0x0, 0x0},
166 {0x0, 0x3, 0x2, 0x1},
167 {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
168 {0x0, 0x3, 0x2, 0x1},
169 {0x0, 0x3, 0x2, 0x1},
170 {0x0, 0x0, 0x0, 0x0},
171 {0x0, 0x3, 0x2, 0x1},
172 {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
173 {0x0, 0x3, 0x2, 0x1},
174 {0x0, 0x3, 0x2, 0x1},
175 {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
176
177 /* BUCK1 ... BUCK9 */
178 {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
179 {0x0, 0x3, 0x1, 0x1},
180 {0x0, 0x3, 0x1, 0x1},
181 {0x0, 0x3, 0x1, 0x1},
182 {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
183 {0x0, 0x3, 0x1, 0x1},
184 {0x0, 0x3, 0x1, 0x1},
185 {0x0, 0x3, 0x1, 0x1},
186 {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
187};
188
189static int s5m8767_get_register(struct regulator_dev *rdev, int *reg,
190 int *enable_ctrl)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900191{
Axel Lin20a14b82012-01-31 15:13:31 +0800192 int reg_id = rdev_get_id(rdev);
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900193 unsigned int mode;
194 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900195
196 switch (reg_id) {
197 case S5M8767_LDO1 ... S5M8767_LDO2:
198 *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
199 break;
200 case S5M8767_LDO3 ... S5M8767_LDO28:
201 *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
202 break;
203 case S5M8767_BUCK1:
204 *reg = S5M8767_REG_BUCK1CTRL1;
205 break;
206 case S5M8767_BUCK2 ... S5M8767_BUCK4:
207 *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
208 break;
209 case S5M8767_BUCK5:
210 *reg = S5M8767_REG_BUCK5CTRL1;
211 break;
212 case S5M8767_BUCK6 ... S5M8767_BUCK9:
213 *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
214 break;
215 default:
216 return -EINVAL;
217 }
218
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900219 mode = s5m8767->opmode[reg_id].mode;
220 *enable_ctrl = s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900221 return 0;
222}
223
224static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
225{
226 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
227 int ret, reg;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900228 int mask = 0xc0, enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900229 u8 val;
230
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900231 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900232 if (ret == -EINVAL)
233 return 1;
234 else if (ret)
235 return ret;
236
237 ret = s5m_reg_read(s5m8767->iodev, reg, &val);
238 if (ret)
239 return ret;
240
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900241 return (val & mask) == enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900242}
243
244static int s5m8767_reg_enable(struct regulator_dev *rdev)
245{
246 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
247 int ret, reg;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900248 int mask = 0xc0, enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900249
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900250 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900251 if (ret)
252 return ret;
253
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900254 return s5m_reg_update(s5m8767->iodev, reg, enable_ctrl, mask);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900255}
256
257static int s5m8767_reg_disable(struct regulator_dev *rdev)
258{
259 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
260 int ret, reg;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900261 int mask = 0xc0, enable_ctrl;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900262
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900263 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900264 if (ret)
265 return ret;
266
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900267 return s5m_reg_update(s5m8767->iodev, reg, ~mask, mask);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900268}
269
270static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
271{
Axel Lin0a416852012-03-10 10:59:43 +0800272 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin20a14b82012-01-31 15:13:31 +0800273 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900274 int reg;
275
276 switch (reg_id) {
277 case S5M8767_LDO1 ... S5M8767_LDO2:
278 reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
279 break;
280 case S5M8767_LDO3 ... S5M8767_LDO28:
281 reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
282 break;
283 case S5M8767_BUCK1:
284 reg = S5M8767_REG_BUCK1CTRL2;
285 break;
286 case S5M8767_BUCK2:
287 reg = S5M8767_REG_BUCK2DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800288 if (s5m8767->buck2_gpiodvs)
289 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900290 break;
291 case S5M8767_BUCK3:
292 reg = S5M8767_REG_BUCK3DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800293 if (s5m8767->buck3_gpiodvs)
294 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900295 break;
296 case S5M8767_BUCK4:
297 reg = S5M8767_REG_BUCK4DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800298 if (s5m8767->buck4_gpiodvs)
299 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900300 break;
301 case S5M8767_BUCK5:
302 reg = S5M8767_REG_BUCK5CTRL2;
303 break;
304 case S5M8767_BUCK6 ... S5M8767_BUCK9:
305 reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
306 break;
307 default:
308 return -EINVAL;
309 }
310
311 *_reg = reg;
312
313 return 0;
314}
315
316static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
317{
318 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin0a416852012-03-10 10:59:43 +0800319 int reg, mask, ret;
Axel Lin20a14b82012-01-31 15:13:31 +0800320 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900321 u8 val;
322
323 ret = s5m8767_get_voltage_register(rdev, &reg);
324 if (ret)
325 return ret;
326
Axel Lin0a416852012-03-10 10:59:43 +0800327 mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900328
329 ret = s5m_reg_read(s5m8767->iodev, reg, &val);
330 if (ret)
331 return ret;
332
333 val &= mask;
334
335 return val;
336}
337
Axel Lin5b5e9772012-03-09 11:31:08 +0800338static int s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900339 const struct s5m_voltage_desc *desc,
340 int min_vol, int max_vol)
341{
Axel Lin5b5e9772012-03-09 11:31:08 +0800342 int selector = 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900343
344 if (desc == NULL)
345 return -EINVAL;
346
347 if (max_vol < desc->min || min_vol > desc->max)
348 return -EINVAL;
349
Axel Lin5b5e9772012-03-09 11:31:08 +0800350 selector = (min_vol - desc->min) / desc->step;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900351
Axel Lin5b5e9772012-03-09 11:31:08 +0800352 if (desc->min + desc->step * selector > max_vol)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900353 return -EINVAL;
354
Axel Lin5b5e9772012-03-09 11:31:08 +0800355 return selector;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900356}
357
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900358static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
359{
360 int temp_index = s5m8767->buck_gpioindex;
361
362 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
363 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
364 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
365}
366
367static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
368{
369 int temp_index = s5m8767->buck_gpioindex;
370
371 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
372 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
373 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
374}
375
Axel Lin321d2ab2012-04-10 14:05:44 +0800376static int s5m8767_set_voltage(struct regulator_dev *rdev,
377 int min_uV, int max_uV, unsigned *selector)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900378{
379 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900380 const struct s5m_voltage_desc *desc;
Axel Lin321d2ab2012-04-10 14:05:44 +0800381 int reg_id = rdev_get_id(rdev);
382 int sel, reg, mask, ret = 0, old_index, index = 0;
383 u8 val;
384 u8 *buck234_vol = NULL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900385
386 switch (reg_id) {
Axel Lin321d2ab2012-04-10 14:05:44 +0800387 case S5M8767_LDO1 ... S5M8767_LDO28:
388 mask = 0x3f;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900389 break;
Axel Lin321d2ab2012-04-10 14:05:44 +0800390 case S5M8767_BUCK1 ... S5M8767_BUCK6:
391 mask = 0xff;
392 if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
393 buck234_vol = &s5m8767->buck2_vol[0];
394 else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
395 buck234_vol = &s5m8767->buck3_vol[0];
396 else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
397 buck234_vol = &s5m8767->buck4_vol[0];
398 break;
399 case S5M8767_BUCK7 ... S5M8767_BUCK8:
400 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900401 case S5M8767_BUCK9:
Axel Lin321d2ab2012-04-10 14:05:44 +0800402 mask = 0xff;
403 break;
404 default:
405 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900406 }
407
408 desc = reg_voltage_map[reg_id];
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900409
Axel Lin321d2ab2012-04-10 14:05:44 +0800410 sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
411 if (sel < 0)
412 return sel;
413
414 /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
415 if (buck234_vol) {
416 while (*buck234_vol != sel) {
417 buck234_vol++;
418 index++;
419 }
420 old_index = s5m8767->buck_gpioindex;
421 s5m8767->buck_gpioindex = index;
422
423 if (index > old_index)
424 s5m8767_set_high(s5m8767);
425 else
426 s5m8767_set_low(s5m8767);
427 } else {
428 ret = s5m8767_get_voltage_register(rdev, &reg);
429 if (ret)
430 return ret;
431
432 s5m_reg_read(s5m8767->iodev, reg, &val);
433 val = (val & ~mask) | sel;
434
435 ret = s5m_reg_write(s5m8767->iodev, reg, val);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900436 }
437
Axel Lin321d2ab2012-04-10 14:05:44 +0800438 *selector = sel;
439 return ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900440}
441
442static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
443 unsigned int old_sel,
444 unsigned int new_sel)
445{
446 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
447 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800448 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900449
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900450 desc = reg_voltage_map[reg_id];
451
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900452 if (old_sel < new_sel)
Axel Lin89e0f0e2012-02-24 14:52:45 +0800453 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
Sangbeom Kim0f8b9c72012-03-09 16:28:10 +0900454 s5m8767->ramp_delay * 1000);
Axel Lin89e0f0e2012-02-24 14:52:45 +0800455 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900456}
457
Axel Lind35aad02012-04-10 14:07:15 +0800458static struct regulator_ops s5m8767_ops = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900459 .list_voltage = s5m8767_list_voltage,
460 .is_enabled = s5m8767_reg_is_enabled,
461 .enable = s5m8767_reg_enable,
462 .disable = s5m8767_reg_disable,
463 .get_voltage_sel = s5m8767_get_voltage_sel,
Axel Lin321d2ab2012-04-10 14:05:44 +0800464 .set_voltage = s5m8767_set_voltage,
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900465 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
466};
467
468#define regulator_desc_ldo(num) { \
469 .name = "LDO"#num, \
470 .id = S5M8767_LDO##num, \
Axel Lind35aad02012-04-10 14:07:15 +0800471 .ops = &s5m8767_ops, \
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900472 .type = REGULATOR_VOLTAGE, \
473 .owner = THIS_MODULE, \
474}
475#define regulator_desc_buck(num) { \
476 .name = "BUCK"#num, \
477 .id = S5M8767_BUCK##num, \
Axel Lind35aad02012-04-10 14:07:15 +0800478 .ops = &s5m8767_ops, \
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900479 .type = REGULATOR_VOLTAGE, \
480 .owner = THIS_MODULE, \
481}
482
483static struct regulator_desc regulators[] = {
484 regulator_desc_ldo(1),
485 regulator_desc_ldo(2),
486 regulator_desc_ldo(3),
487 regulator_desc_ldo(4),
488 regulator_desc_ldo(5),
489 regulator_desc_ldo(6),
490 regulator_desc_ldo(7),
491 regulator_desc_ldo(8),
492 regulator_desc_ldo(9),
493 regulator_desc_ldo(10),
494 regulator_desc_ldo(11),
495 regulator_desc_ldo(12),
496 regulator_desc_ldo(13),
497 regulator_desc_ldo(14),
498 regulator_desc_ldo(15),
499 regulator_desc_ldo(16),
500 regulator_desc_ldo(17),
501 regulator_desc_ldo(18),
502 regulator_desc_ldo(19),
503 regulator_desc_ldo(20),
504 regulator_desc_ldo(21),
505 regulator_desc_ldo(22),
506 regulator_desc_ldo(23),
507 regulator_desc_ldo(24),
508 regulator_desc_ldo(25),
509 regulator_desc_ldo(26),
510 regulator_desc_ldo(27),
511 regulator_desc_ldo(28),
512 regulator_desc_buck(1),
513 regulator_desc_buck(2),
514 regulator_desc_buck(3),
515 regulator_desc_buck(4),
516 regulator_desc_buck(5),
517 regulator_desc_buck(6),
518 regulator_desc_buck(7),
519 regulator_desc_buck(8),
520 regulator_desc_buck(9),
521};
522
523static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
524{
525 struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
526 struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100527 struct regulator_config config = { };
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900528 struct regulator_dev **rdev;
529 struct s5m8767_info *s5m8767;
Mark Brown22cd2fef2012-02-06 11:34:36 +0000530 int i, ret, size;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900531
532 if (!pdata) {
533 dev_err(pdev->dev.parent, "Platform data not supplied\n");
534 return -ENODEV;
535 }
536
Axel Lin6c4efe22012-03-10 08:43:02 +0800537 if (pdata->buck2_gpiodvs) {
538 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
539 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
540 return -EINVAL;
541 }
542 }
543
544 if (pdata->buck3_gpiodvs) {
545 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
546 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
547 return -EINVAL;
548 }
549 }
550
551 if (pdata->buck4_gpiodvs) {
552 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
553 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
554 return -EINVAL;
555 }
556 }
557
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900558 s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
559 GFP_KERNEL);
560 if (!s5m8767)
561 return -ENOMEM;
562
563 size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
564 s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
565 if (!s5m8767->rdev)
566 return -ENOMEM;
567
568 rdev = s5m8767->rdev;
569 s5m8767->dev = &pdev->dev;
570 s5m8767->iodev = iodev;
571 s5m8767->num_regulators = S5M8767_REG_MAX - 2;
572 platform_set_drvdata(pdev, s5m8767);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900573
574 s5m8767->buck_gpioindex = pdata->buck_default_idx;
575 s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
576 s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
577 s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
578 s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
579 s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
580 s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
581 s5m8767->ramp_delay = pdata->buck_ramp_delay;
582 s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
583 s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
584 s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900585 s5m8767->opmode = pdata->opmode;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900586
587 for (i = 0; i < 8; i++) {
588 if (s5m8767->buck2_gpiodvs) {
589 s5m8767->buck2_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800590 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900591 &buck_voltage_val2,
592 pdata->buck2_voltage[i],
593 pdata->buck2_voltage[i] +
594 buck_voltage_val2.step);
595 }
596
597 if (s5m8767->buck3_gpiodvs) {
598 s5m8767->buck3_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800599 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900600 &buck_voltage_val2,
601 pdata->buck3_voltage[i],
602 pdata->buck3_voltage[i] +
603 buck_voltage_val2.step);
604 }
605
606 if (s5m8767->buck4_gpiodvs) {
607 s5m8767->buck4_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800608 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900609 &buck_voltage_val2,
610 pdata->buck4_voltage[i],
611 pdata->buck4_voltage[i] +
612 buck_voltage_val2.step);
613 }
614 }
615
616 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
617 pdata->buck4_gpiodvs) {
618 if (gpio_is_valid(pdata->buck_gpios[0]) &&
619 gpio_is_valid(pdata->buck_gpios[1]) &&
620 gpio_is_valid(pdata->buck_gpios[2])) {
621 ret = gpio_request(pdata->buck_gpios[0],
622 "S5M8767 SET1");
623 if (ret == -EBUSY)
624 dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
625
626 ret = gpio_request(pdata->buck_gpios[1],
627 "S5M8767 SET2");
628 if (ret == -EBUSY)
629 dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
630
631 ret = gpio_request(pdata->buck_gpios[2],
632 "S5M8767 SET3");
633 if (ret == -EBUSY)
634 dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
635 /* SET1 GPIO */
636 gpio_direction_output(pdata->buck_gpios[0],
637 (s5m8767->buck_gpioindex >> 2) & 0x1);
638 /* SET2 GPIO */
639 gpio_direction_output(pdata->buck_gpios[1],
640 (s5m8767->buck_gpioindex >> 1) & 0x1);
641 /* SET3 GPIO */
642 gpio_direction_output(pdata->buck_gpios[2],
643 (s5m8767->buck_gpioindex >> 0) & 0x1);
644 ret = 0;
645 } else {
646 dev_err(&pdev->dev, "GPIO NOT VALID\n");
647 ret = -EINVAL;
648 return ret;
649 }
650 }
651
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900652 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
653 (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
654 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
655 (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
656 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
657 (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
658
659 /* Initialize GPIO DVS registers */
660 for (i = 0; i < 8; i++) {
661 if (s5m8767->buck2_gpiodvs) {
662 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
663 s5m8767->buck2_vol[i]);
664 }
665
666 if (s5m8767->buck3_gpiodvs) {
667 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
668 s5m8767->buck3_vol[i]);
669 }
670
671 if (s5m8767->buck4_gpiodvs) {
672 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
673 s5m8767->buck4_vol[i]);
674 }
675 }
676 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
677 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
678 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
679
680 if (s5m8767->buck2_ramp)
681 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
682
683 if (s5m8767->buck3_ramp)
684 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
685
686 if (s5m8767->buck4_ramp)
687 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
688
689 if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
690 || s5m8767->buck4_ramp) {
691 switch (s5m8767->ramp_delay) {
692 case 15:
693 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
694 0xc0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800695 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900696 case 25:
697 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
698 0xd0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800699 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900700 case 50:
701 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
702 0xe0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800703 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900704 case 100:
705 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
706 0xf0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800707 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900708 default:
709 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
710 0x90, 0xf0);
711 }
712 }
713
714 for (i = 0; i < pdata->num_regulators; i++) {
715 const struct s5m_voltage_desc *desc;
716 int id = pdata->regulators[i].id;
717
718 desc = reg_voltage_map[id];
719 if (desc)
720 regulators[id].n_voltages =
721 (desc->max - desc->min) / desc->step + 1;
722
Mark Brownc172708d2012-04-04 00:50:22 +0100723 config.dev = s5m8767->dev;
724 config.init_data = pdata->regulators[i].initdata;
725 config.driver_data = s5m8767;
726
727 rdev[i] = regulator_register(&regulators[id], &config);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900728 if (IS_ERR(rdev[i])) {
729 ret = PTR_ERR(rdev[i]);
730 dev_err(s5m8767->dev, "regulator init failed for %d\n",
731 id);
732 rdev[i] = NULL;
733 goto err;
734 }
735 }
736
737 return 0;
738err:
739 for (i = 0; i < s5m8767->num_regulators; i++)
740 if (rdev[i])
741 regulator_unregister(rdev[i]);
742
743 return ret;
744}
745
746static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
747{
748 struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
749 struct regulator_dev **rdev = s5m8767->rdev;
750 int i;
751
752 for (i = 0; i < s5m8767->num_regulators; i++)
753 if (rdev[i])
754 regulator_unregister(rdev[i]);
755
756 return 0;
757}
758
759static const struct platform_device_id s5m8767_pmic_id[] = {
760 { "s5m8767-pmic", 0},
761 { },
762};
763MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
764
765static struct platform_driver s5m8767_pmic_driver = {
766 .driver = {
767 .name = "s5m8767-pmic",
768 .owner = THIS_MODULE,
769 },
770 .probe = s5m8767_pmic_probe,
771 .remove = __devexit_p(s5m8767_pmic_remove),
772 .id_table = s5m8767_pmic_id,
773};
774
775static int __init s5m8767_pmic_init(void)
776{
777 return platform_driver_register(&s5m8767_pmic_driver);
778}
779subsys_initcall(s5m8767_pmic_init);
780
781static void __exit s5m8767_pmic_exit(void)
782{
783 platform_driver_unregister(&s5m8767_pmic_driver);
784}
785module_exit(s5m8767_pmic_exit);
786
787/* Module information */
788MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
789MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
790MODULE_LICENSE("GPL");