blob: 4ca2db0590048e833ddb88e9eb2a88fee1e3c494 [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;
31
32 int ramp_delay;
33 bool buck2_ramp;
34 bool buck3_ramp;
35 bool buck4_ramp;
36
37 bool buck2_gpiodvs;
38 bool buck3_gpiodvs;
39 bool buck4_gpiodvs;
40 u8 buck2_vol[8];
41 u8 buck3_vol[8];
42 u8 buck4_vol[8];
43 int buck_gpios[3];
44 int buck_gpioindex;
45};
46
47struct s5m_voltage_desc {
48 int max;
49 int min;
50 int step;
51};
52
53static const struct s5m_voltage_desc buck_voltage_val1 = {
54 .max = 2225000,
55 .min = 650000,
56 .step = 6250,
57};
58
59static const struct s5m_voltage_desc buck_voltage_val2 = {
60 .max = 1600000,
61 .min = 600000,
62 .step = 6250,
63};
64
65static const struct s5m_voltage_desc buck_voltage_val3 = {
66 .max = 3000000,
67 .min = 750000,
68 .step = 12500,
69};
70
71static const struct s5m_voltage_desc ldo_voltage_val1 = {
72 .max = 3950000,
73 .min = 800000,
74 .step = 50000,
75};
76
77static const struct s5m_voltage_desc ldo_voltage_val2 = {
78 .max = 2375000,
79 .min = 800000,
80 .step = 25000,
81};
82
83static const struct s5m_voltage_desc *reg_voltage_map[] = {
84 [S5M8767_LDO1] = &ldo_voltage_val2,
85 [S5M8767_LDO2] = &ldo_voltage_val2,
86 [S5M8767_LDO3] = &ldo_voltage_val1,
87 [S5M8767_LDO4] = &ldo_voltage_val1,
88 [S5M8767_LDO5] = &ldo_voltage_val1,
89 [S5M8767_LDO6] = &ldo_voltage_val2,
90 [S5M8767_LDO7] = &ldo_voltage_val2,
91 [S5M8767_LDO8] = &ldo_voltage_val2,
92 [S5M8767_LDO9] = &ldo_voltage_val1,
93 [S5M8767_LDO10] = &ldo_voltage_val1,
94 [S5M8767_LDO11] = &ldo_voltage_val1,
95 [S5M8767_LDO12] = &ldo_voltage_val1,
96 [S5M8767_LDO13] = &ldo_voltage_val1,
97 [S5M8767_LDO14] = &ldo_voltage_val1,
98 [S5M8767_LDO15] = &ldo_voltage_val2,
99 [S5M8767_LDO16] = &ldo_voltage_val1,
100 [S5M8767_LDO17] = &ldo_voltage_val1,
101 [S5M8767_LDO18] = &ldo_voltage_val1,
102 [S5M8767_LDO19] = &ldo_voltage_val1,
103 [S5M8767_LDO20] = &ldo_voltage_val1,
104 [S5M8767_LDO21] = &ldo_voltage_val1,
105 [S5M8767_LDO22] = &ldo_voltage_val1,
106 [S5M8767_LDO23] = &ldo_voltage_val1,
107 [S5M8767_LDO24] = &ldo_voltage_val1,
108 [S5M8767_LDO25] = &ldo_voltage_val1,
109 [S5M8767_LDO26] = &ldo_voltage_val1,
110 [S5M8767_LDO27] = &ldo_voltage_val1,
111 [S5M8767_LDO28] = &ldo_voltage_val1,
112 [S5M8767_BUCK1] = &buck_voltage_val1,
113 [S5M8767_BUCK2] = &buck_voltage_val2,
114 [S5M8767_BUCK3] = &buck_voltage_val2,
115 [S5M8767_BUCK4] = &buck_voltage_val2,
116 [S5M8767_BUCK5] = &buck_voltage_val1,
117 [S5M8767_BUCK6] = &buck_voltage_val1,
118 [S5M8767_BUCK7] = NULL,
119 [S5M8767_BUCK8] = NULL,
120 [S5M8767_BUCK9] = &buck_voltage_val3,
121};
122
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900123static int s5m8767_list_voltage(struct regulator_dev *rdev,
124 unsigned int selector)
125{
126 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800127 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900128 int val;
129
130 if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0)
131 return -EINVAL;
132
133 desc = reg_voltage_map[reg_id];
134 if (desc == NULL)
135 return -EINVAL;
136
137 val = desc->min + desc->step * selector;
138 if (val > desc->max)
139 return -EINVAL;
140
141 return val;
142}
143
144static int s5m8767_get_register(struct regulator_dev *rdev, int *reg)
145{
Axel Lin20a14b82012-01-31 15:13:31 +0800146 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900147
148 switch (reg_id) {
149 case S5M8767_LDO1 ... S5M8767_LDO2:
150 *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
151 break;
152 case S5M8767_LDO3 ... S5M8767_LDO28:
153 *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
154 break;
155 case S5M8767_BUCK1:
156 *reg = S5M8767_REG_BUCK1CTRL1;
157 break;
158 case S5M8767_BUCK2 ... S5M8767_BUCK4:
159 *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
160 break;
161 case S5M8767_BUCK5:
162 *reg = S5M8767_REG_BUCK5CTRL1;
163 break;
164 case S5M8767_BUCK6 ... S5M8767_BUCK9:
165 *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
166 break;
167 default:
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
174static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
175{
176 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
177 int ret, reg;
178 int mask = 0xc0, pattern = 0xc0;
179 u8 val;
180
181 ret = s5m8767_get_register(rdev, &reg);
182 if (ret == -EINVAL)
183 return 1;
184 else if (ret)
185 return ret;
186
187 ret = s5m_reg_read(s5m8767->iodev, reg, &val);
188 if (ret)
189 return ret;
190
191 return (val & mask) == pattern;
192}
193
194static int s5m8767_reg_enable(struct regulator_dev *rdev)
195{
196 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
197 int ret, reg;
198 int mask = 0xc0, pattern = 0xc0;
199
200 ret = s5m8767_get_register(rdev, &reg);
201 if (ret)
202 return ret;
203
204 return s5m_reg_update(s5m8767->iodev, reg, pattern, mask);
205}
206
207static int s5m8767_reg_disable(struct regulator_dev *rdev)
208{
209 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
210 int ret, reg;
211 int mask = 0xc0, pattern = 0xc0;
212
213 ret = s5m8767_get_register(rdev, &reg);
214 if (ret)
215 return ret;
216
217 return s5m_reg_update(s5m8767->iodev, reg, ~pattern, mask);
218}
219
220static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
221{
Axel Lin0a416852012-03-10 10:59:43 +0800222 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin20a14b82012-01-31 15:13:31 +0800223 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900224 int reg;
225
226 switch (reg_id) {
227 case S5M8767_LDO1 ... S5M8767_LDO2:
228 reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
229 break;
230 case S5M8767_LDO3 ... S5M8767_LDO28:
231 reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
232 break;
233 case S5M8767_BUCK1:
234 reg = S5M8767_REG_BUCK1CTRL2;
235 break;
236 case S5M8767_BUCK2:
237 reg = S5M8767_REG_BUCK2DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800238 if (s5m8767->buck2_gpiodvs)
239 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900240 break;
241 case S5M8767_BUCK3:
242 reg = S5M8767_REG_BUCK3DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800243 if (s5m8767->buck3_gpiodvs)
244 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900245 break;
246 case S5M8767_BUCK4:
247 reg = S5M8767_REG_BUCK4DVS1;
Axel Lin0a416852012-03-10 10:59:43 +0800248 if (s5m8767->buck4_gpiodvs)
249 reg += s5m8767->buck_gpioindex;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900250 break;
251 case S5M8767_BUCK5:
252 reg = S5M8767_REG_BUCK5CTRL2;
253 break;
254 case S5M8767_BUCK6 ... S5M8767_BUCK9:
255 reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
256 break;
257 default:
258 return -EINVAL;
259 }
260
261 *_reg = reg;
262
263 return 0;
264}
265
266static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
267{
268 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin0a416852012-03-10 10:59:43 +0800269 int reg, mask, ret;
Axel Lin20a14b82012-01-31 15:13:31 +0800270 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900271 u8 val;
272
273 ret = s5m8767_get_voltage_register(rdev, &reg);
274 if (ret)
275 return ret;
276
Axel Lin0a416852012-03-10 10:59:43 +0800277 mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900278
279 ret = s5m_reg_read(s5m8767->iodev, reg, &val);
280 if (ret)
281 return ret;
282
283 val &= mask;
284
285 return val;
286}
287
Axel Lin5b5e9772012-03-09 11:31:08 +0800288static int s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900289 const struct s5m_voltage_desc *desc,
290 int min_vol, int max_vol)
291{
Axel Lin5b5e9772012-03-09 11:31:08 +0800292 int selector = 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900293
294 if (desc == NULL)
295 return -EINVAL;
296
297 if (max_vol < desc->min || min_vol > desc->max)
298 return -EINVAL;
299
Axel Lin5b5e9772012-03-09 11:31:08 +0800300 selector = (min_vol - desc->min) / desc->step;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900301
Axel Lin5b5e9772012-03-09 11:31:08 +0800302 if (desc->min + desc->step * selector > max_vol)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900303 return -EINVAL;
304
Axel Lin5b5e9772012-03-09 11:31:08 +0800305 return selector;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900306}
307
308static int s5m8767_set_voltage(struct regulator_dev *rdev,
309 int min_uV, int max_uV, unsigned *selector)
310{
311 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900312 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800313 int reg_id = rdev_get_id(rdev);
Axel Lin546e7842012-03-19 12:22:10 +0800314 int sel, reg, mask, ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900315 u8 val;
316
317 switch (reg_id) {
318 case S5M8767_LDO1 ... S5M8767_LDO28:
319 mask = 0x3f;
320 break;
321 case S5M8767_BUCK1 ... S5M8767_BUCK6:
322 mask = 0xff;
323 break;
324 case S5M8767_BUCK7 ... S5M8767_BUCK8:
325 return -EINVAL;
326 case S5M8767_BUCK9:
327 mask = 0xff;
328 break;
329 default:
330 return -EINVAL;
331 }
332
333 desc = reg_voltage_map[reg_id];
334
Axel Lin546e7842012-03-19 12:22:10 +0800335 sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
336 if (sel < 0)
337 return sel;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900338
339 ret = s5m8767_get_voltage_register(rdev, &reg);
340 if (ret)
341 return ret;
342
343 s5m_reg_read(s5m8767->iodev, reg, &val);
Axel Lin546e7842012-03-19 12:22:10 +0800344 val &= ~mask;
345 val |= sel;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900346
347 ret = s5m_reg_write(s5m8767->iodev, reg, val);
Axel Lin546e7842012-03-19 12:22:10 +0800348 *selector = sel;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900349
350 return ret;
351}
352
353static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
354{
355 int temp_index = s5m8767->buck_gpioindex;
356
357 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
358 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
359 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
360}
361
362static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
363{
364 int temp_index = s5m8767->buck_gpioindex;
365
366 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
367 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
368 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
369}
370
371static int s5m8767_set_voltage_buck(struct regulator_dev *rdev,
372 int min_uV, int max_uV, unsigned *selector)
373{
374 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Axel Lin20a14b82012-01-31 15:13:31 +0800375 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900376 const struct s5m_voltage_desc *desc;
377 int new_val, old_val, i = 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900378
379 if (reg_id < S5M8767_BUCK1 || reg_id > S5M8767_BUCK6)
380 return -EINVAL;
381
382 switch (reg_id) {
383 case S5M8767_BUCK1:
384 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
385 case S5M8767_BUCK2 ... S5M8767_BUCK4:
386 break;
387 case S5M8767_BUCK5 ... S5M8767_BUCK6:
388 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
389 case S5M8767_BUCK9:
390 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
391 }
392
393 desc = reg_voltage_map[reg_id];
Axel Lin5b5e9772012-03-09 11:31:08 +0800394 new_val = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900395 if (new_val < 0)
396 return new_val;
397
398 switch (reg_id) {
399 case S5M8767_BUCK2:
400 if (s5m8767->buck2_gpiodvs) {
401 while (s5m8767->buck2_vol[i] != new_val)
402 i++;
403 } else
404 return s5m8767_set_voltage(rdev, min_uV,
405 max_uV, selector);
406 break;
407 case S5M8767_BUCK3:
408 if (s5m8767->buck3_gpiodvs) {
409 while (s5m8767->buck3_vol[i] != new_val)
410 i++;
411 } else
412 return s5m8767_set_voltage(rdev, min_uV,
413 max_uV, selector);
414 break;
415 case S5M8767_BUCK4:
416 if (s5m8767->buck3_gpiodvs) {
417 while (s5m8767->buck4_vol[i] != new_val)
418 i++;
419 } else
420 return s5m8767_set_voltage(rdev, min_uV,
421 max_uV, selector);
422 break;
423 }
424
425 old_val = s5m8767->buck_gpioindex;
426 s5m8767->buck_gpioindex = i;
427
428 if (i > old_val)
429 s5m8767_set_high(s5m8767);
430 else
431 s5m8767_set_low(s5m8767);
432
433 *selector = new_val;
434 return 0;
435}
436
437static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
438 unsigned int old_sel,
439 unsigned int new_sel)
440{
441 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
442 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800443 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900444
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900445 desc = reg_voltage_map[reg_id];
446
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900447 if (old_sel < new_sel)
Axel Lin89e0f0e2012-02-24 14:52:45 +0800448 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
Sangbeom Kim0f8b9c72012-03-09 16:28:10 +0900449 s5m8767->ramp_delay * 1000);
Axel Lin89e0f0e2012-02-24 14:52:45 +0800450 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900451}
452
453static struct regulator_ops s5m8767_ldo_ops = {
454 .list_voltage = s5m8767_list_voltage,
455 .is_enabled = s5m8767_reg_is_enabled,
456 .enable = s5m8767_reg_enable,
457 .disable = s5m8767_reg_disable,
458 .get_voltage_sel = s5m8767_get_voltage_sel,
459 .set_voltage = s5m8767_set_voltage,
460 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
461};
462
463static struct regulator_ops s5m8767_buck_ops = {
464 .list_voltage = s5m8767_list_voltage,
465 .is_enabled = s5m8767_reg_is_enabled,
466 .enable = s5m8767_reg_enable,
467 .disable = s5m8767_reg_disable,
468 .get_voltage_sel = s5m8767_get_voltage_sel,
469 .set_voltage = s5m8767_set_voltage_buck,
470 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
471};
472
473#define regulator_desc_ldo(num) { \
474 .name = "LDO"#num, \
475 .id = S5M8767_LDO##num, \
476 .ops = &s5m8767_ldo_ops, \
477 .type = REGULATOR_VOLTAGE, \
478 .owner = THIS_MODULE, \
479}
480#define regulator_desc_buck(num) { \
481 .name = "BUCK"#num, \
482 .id = S5M8767_BUCK##num, \
483 .ops = &s5m8767_buck_ops, \
484 .type = REGULATOR_VOLTAGE, \
485 .owner = THIS_MODULE, \
486}
487
488static struct regulator_desc regulators[] = {
489 regulator_desc_ldo(1),
490 regulator_desc_ldo(2),
491 regulator_desc_ldo(3),
492 regulator_desc_ldo(4),
493 regulator_desc_ldo(5),
494 regulator_desc_ldo(6),
495 regulator_desc_ldo(7),
496 regulator_desc_ldo(8),
497 regulator_desc_ldo(9),
498 regulator_desc_ldo(10),
499 regulator_desc_ldo(11),
500 regulator_desc_ldo(12),
501 regulator_desc_ldo(13),
502 regulator_desc_ldo(14),
503 regulator_desc_ldo(15),
504 regulator_desc_ldo(16),
505 regulator_desc_ldo(17),
506 regulator_desc_ldo(18),
507 regulator_desc_ldo(19),
508 regulator_desc_ldo(20),
509 regulator_desc_ldo(21),
510 regulator_desc_ldo(22),
511 regulator_desc_ldo(23),
512 regulator_desc_ldo(24),
513 regulator_desc_ldo(25),
514 regulator_desc_ldo(26),
515 regulator_desc_ldo(27),
516 regulator_desc_ldo(28),
517 regulator_desc_buck(1),
518 regulator_desc_buck(2),
519 regulator_desc_buck(3),
520 regulator_desc_buck(4),
521 regulator_desc_buck(5),
522 regulator_desc_buck(6),
523 regulator_desc_buck(7),
524 regulator_desc_buck(8),
525 regulator_desc_buck(9),
526};
527
528static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
529{
530 struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
531 struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
532 struct regulator_dev **rdev;
533 struct s5m8767_info *s5m8767;
Mark Brown22cd2fef2012-02-06 11:34:36 +0000534 int i, ret, size;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900535
536 if (!pdata) {
537 dev_err(pdev->dev.parent, "Platform data not supplied\n");
538 return -ENODEV;
539 }
540
Axel Lin6c4efe22012-03-10 08:43:02 +0800541 if (pdata->buck2_gpiodvs) {
542 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
543 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
544 return -EINVAL;
545 }
546 }
547
548 if (pdata->buck3_gpiodvs) {
549 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
550 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
551 return -EINVAL;
552 }
553 }
554
555 if (pdata->buck4_gpiodvs) {
556 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
557 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
558 return -EINVAL;
559 }
560 }
561
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900562 s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
563 GFP_KERNEL);
564 if (!s5m8767)
565 return -ENOMEM;
566
567 size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
568 s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
569 if (!s5m8767->rdev)
570 return -ENOMEM;
571
572 rdev = s5m8767->rdev;
573 s5m8767->dev = &pdev->dev;
574 s5m8767->iodev = iodev;
575 s5m8767->num_regulators = S5M8767_REG_MAX - 2;
576 platform_set_drvdata(pdev, s5m8767);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900577
578 s5m8767->buck_gpioindex = pdata->buck_default_idx;
579 s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
580 s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
581 s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
582 s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
583 s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
584 s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
585 s5m8767->ramp_delay = pdata->buck_ramp_delay;
586 s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
587 s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
588 s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
589
590 for (i = 0; i < 8; i++) {
591 if (s5m8767->buck2_gpiodvs) {
592 s5m8767->buck2_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800593 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900594 &buck_voltage_val2,
595 pdata->buck2_voltage[i],
596 pdata->buck2_voltage[i] +
597 buck_voltage_val2.step);
598 }
599
600 if (s5m8767->buck3_gpiodvs) {
601 s5m8767->buck3_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800602 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900603 &buck_voltage_val2,
604 pdata->buck3_voltage[i],
605 pdata->buck3_voltage[i] +
606 buck_voltage_val2.step);
607 }
608
609 if (s5m8767->buck4_gpiodvs) {
610 s5m8767->buck4_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800611 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900612 &buck_voltage_val2,
613 pdata->buck4_voltage[i],
614 pdata->buck4_voltage[i] +
615 buck_voltage_val2.step);
616 }
617 }
618
619 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
620 pdata->buck4_gpiodvs) {
621 if (gpio_is_valid(pdata->buck_gpios[0]) &&
622 gpio_is_valid(pdata->buck_gpios[1]) &&
623 gpio_is_valid(pdata->buck_gpios[2])) {
624 ret = gpio_request(pdata->buck_gpios[0],
625 "S5M8767 SET1");
626 if (ret == -EBUSY)
627 dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
628
629 ret = gpio_request(pdata->buck_gpios[1],
630 "S5M8767 SET2");
631 if (ret == -EBUSY)
632 dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
633
634 ret = gpio_request(pdata->buck_gpios[2],
635 "S5M8767 SET3");
636 if (ret == -EBUSY)
637 dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
638 /* SET1 GPIO */
639 gpio_direction_output(pdata->buck_gpios[0],
640 (s5m8767->buck_gpioindex >> 2) & 0x1);
641 /* SET2 GPIO */
642 gpio_direction_output(pdata->buck_gpios[1],
643 (s5m8767->buck_gpioindex >> 1) & 0x1);
644 /* SET3 GPIO */
645 gpio_direction_output(pdata->buck_gpios[2],
646 (s5m8767->buck_gpioindex >> 0) & 0x1);
647 ret = 0;
648 } else {
649 dev_err(&pdev->dev, "GPIO NOT VALID\n");
650 ret = -EINVAL;
651 return ret;
652 }
653 }
654
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900655 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
656 (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
657 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
658 (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
659 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
660 (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
661
662 /* Initialize GPIO DVS registers */
663 for (i = 0; i < 8; i++) {
664 if (s5m8767->buck2_gpiodvs) {
665 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
666 s5m8767->buck2_vol[i]);
667 }
668
669 if (s5m8767->buck3_gpiodvs) {
670 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
671 s5m8767->buck3_vol[i]);
672 }
673
674 if (s5m8767->buck4_gpiodvs) {
675 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
676 s5m8767->buck4_vol[i]);
677 }
678 }
679 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
680 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
681 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
682
683 if (s5m8767->buck2_ramp)
684 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
685
686 if (s5m8767->buck3_ramp)
687 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
688
689 if (s5m8767->buck4_ramp)
690 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
691
692 if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
693 || s5m8767->buck4_ramp) {
694 switch (s5m8767->ramp_delay) {
695 case 15:
696 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
697 0xc0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800698 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900699 case 25:
700 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
701 0xd0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800702 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900703 case 50:
704 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
705 0xe0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800706 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900707 case 100:
708 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
709 0xf0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800710 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900711 default:
712 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
713 0x90, 0xf0);
714 }
715 }
716
717 for (i = 0; i < pdata->num_regulators; i++) {
718 const struct s5m_voltage_desc *desc;
719 int id = pdata->regulators[i].id;
720
721 desc = reg_voltage_map[id];
722 if (desc)
723 regulators[id].n_voltages =
724 (desc->max - desc->min) / desc->step + 1;
725
726 rdev[i] = regulator_register(&regulators[id], s5m8767->dev,
Mark Brown0ce69872012-01-17 11:25:45 +0000727 pdata->regulators[i].initdata, s5m8767, NULL);
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");