blob: ebe72d071b44b5354c9b19ee466cbdd3041af20a [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 Lin94e85a32012-04-13 09:30:05 +0800350 if (min_vol < desc->min)
351 min_vol = desc->min;
352
353 selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900354
Axel Lin5b5e9772012-03-09 11:31:08 +0800355 if (desc->min + desc->step * selector > max_vol)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900356 return -EINVAL;
357
Axel Lin5b5e9772012-03-09 11:31:08 +0800358 return selector;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900359}
360
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900361static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
362{
363 int temp_index = s5m8767->buck_gpioindex;
364
365 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
366 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
367 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
368}
369
370static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
371{
372 int temp_index = s5m8767->buck_gpioindex;
373
374 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
375 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
376 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
377}
378
Axel Lin321d2ab2012-04-10 14:05:44 +0800379static int s5m8767_set_voltage(struct regulator_dev *rdev,
380 int min_uV, int max_uV, unsigned *selector)
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900381{
382 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900383 const struct s5m_voltage_desc *desc;
Axel Lin321d2ab2012-04-10 14:05:44 +0800384 int reg_id = rdev_get_id(rdev);
385 int sel, reg, mask, ret = 0, old_index, index = 0;
386 u8 val;
387 u8 *buck234_vol = NULL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900388
389 switch (reg_id) {
Axel Lin321d2ab2012-04-10 14:05:44 +0800390 case S5M8767_LDO1 ... S5M8767_LDO28:
391 mask = 0x3f;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900392 break;
Axel Lin321d2ab2012-04-10 14:05:44 +0800393 case S5M8767_BUCK1 ... S5M8767_BUCK6:
394 mask = 0xff;
395 if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
396 buck234_vol = &s5m8767->buck2_vol[0];
397 else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
398 buck234_vol = &s5m8767->buck3_vol[0];
399 else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
400 buck234_vol = &s5m8767->buck4_vol[0];
401 break;
402 case S5M8767_BUCK7 ... S5M8767_BUCK8:
403 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900404 case S5M8767_BUCK9:
Axel Lin321d2ab2012-04-10 14:05:44 +0800405 mask = 0xff;
406 break;
407 default:
408 return -EINVAL;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900409 }
410
411 desc = reg_voltage_map[reg_id];
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900412
Axel Lin321d2ab2012-04-10 14:05:44 +0800413 sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
414 if (sel < 0)
415 return sel;
416
417 /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
418 if (buck234_vol) {
419 while (*buck234_vol != sel) {
420 buck234_vol++;
421 index++;
422 }
423 old_index = s5m8767->buck_gpioindex;
424 s5m8767->buck_gpioindex = index;
425
426 if (index > old_index)
427 s5m8767_set_high(s5m8767);
428 else
429 s5m8767_set_low(s5m8767);
430 } else {
431 ret = s5m8767_get_voltage_register(rdev, &reg);
432 if (ret)
433 return ret;
434
435 s5m_reg_read(s5m8767->iodev, reg, &val);
436 val = (val & ~mask) | sel;
437
438 ret = s5m_reg_write(s5m8767->iodev, reg, val);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900439 }
440
Axel Lin321d2ab2012-04-10 14:05:44 +0800441 *selector = sel;
442 return ret;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900443}
444
445static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
446 unsigned int old_sel,
447 unsigned int new_sel)
448{
449 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
450 const struct s5m_voltage_desc *desc;
Axel Lin20a14b82012-01-31 15:13:31 +0800451 int reg_id = rdev_get_id(rdev);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900452
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900453 desc = reg_voltage_map[reg_id];
454
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900455 if (old_sel < new_sel)
Axel Lin89e0f0e2012-02-24 14:52:45 +0800456 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
Sangbeom Kim0f8b9c72012-03-09 16:28:10 +0900457 s5m8767->ramp_delay * 1000);
Axel Lin89e0f0e2012-02-24 14:52:45 +0800458 return 0;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900459}
460
Axel Lind35aad02012-04-10 14:07:15 +0800461static struct regulator_ops s5m8767_ops = {
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900462 .list_voltage = s5m8767_list_voltage,
463 .is_enabled = s5m8767_reg_is_enabled,
464 .enable = s5m8767_reg_enable,
465 .disable = s5m8767_reg_disable,
466 .get_voltage_sel = s5m8767_get_voltage_sel,
Axel Lin321d2ab2012-04-10 14:05:44 +0800467 .set_voltage = s5m8767_set_voltage,
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900468 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
469};
470
Axel Lin65896e72012-04-10 14:08:19 +0800471#define s5m8767_regulator_desc(_name) { \
472 .name = #_name, \
473 .id = S5M8767_##_name, \
474 .ops = &s5m8767_ops, \
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900475 .type = REGULATOR_VOLTAGE, \
476 .owner = THIS_MODULE, \
477}
478
479static struct regulator_desc regulators[] = {
Axel Lin65896e72012-04-10 14:08:19 +0800480 s5m8767_regulator_desc(LDO1),
481 s5m8767_regulator_desc(LDO2),
482 s5m8767_regulator_desc(LDO3),
483 s5m8767_regulator_desc(LDO4),
484 s5m8767_regulator_desc(LDO5),
485 s5m8767_regulator_desc(LDO6),
486 s5m8767_regulator_desc(LDO7),
487 s5m8767_regulator_desc(LDO8),
488 s5m8767_regulator_desc(LDO9),
489 s5m8767_regulator_desc(LDO10),
490 s5m8767_regulator_desc(LDO11),
491 s5m8767_regulator_desc(LDO12),
492 s5m8767_regulator_desc(LDO13),
493 s5m8767_regulator_desc(LDO14),
494 s5m8767_regulator_desc(LDO15),
495 s5m8767_regulator_desc(LDO16),
496 s5m8767_regulator_desc(LDO17),
497 s5m8767_regulator_desc(LDO18),
498 s5m8767_regulator_desc(LDO19),
499 s5m8767_regulator_desc(LDO20),
500 s5m8767_regulator_desc(LDO21),
501 s5m8767_regulator_desc(LDO22),
502 s5m8767_regulator_desc(LDO23),
503 s5m8767_regulator_desc(LDO24),
504 s5m8767_regulator_desc(LDO25),
505 s5m8767_regulator_desc(LDO26),
506 s5m8767_regulator_desc(LDO27),
507 s5m8767_regulator_desc(LDO28),
508 s5m8767_regulator_desc(BUCK1),
509 s5m8767_regulator_desc(BUCK2),
510 s5m8767_regulator_desc(BUCK3),
511 s5m8767_regulator_desc(BUCK4),
512 s5m8767_regulator_desc(BUCK5),
513 s5m8767_regulator_desc(BUCK6),
514 s5m8767_regulator_desc(BUCK7),
515 s5m8767_regulator_desc(BUCK8),
516 s5m8767_regulator_desc(BUCK9),
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900517};
518
519static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
520{
521 struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
522 struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc172708d2012-04-04 00:50:22 +0100523 struct regulator_config config = { };
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900524 struct regulator_dev **rdev;
525 struct s5m8767_info *s5m8767;
Mark Brown22cd2fef2012-02-06 11:34:36 +0000526 int i, ret, size;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900527
528 if (!pdata) {
529 dev_err(pdev->dev.parent, "Platform data not supplied\n");
530 return -ENODEV;
531 }
532
Axel Lin6c4efe22012-03-10 08:43:02 +0800533 if (pdata->buck2_gpiodvs) {
534 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
535 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
536 return -EINVAL;
537 }
538 }
539
540 if (pdata->buck3_gpiodvs) {
541 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
542 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
543 return -EINVAL;
544 }
545 }
546
547 if (pdata->buck4_gpiodvs) {
548 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
549 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
550 return -EINVAL;
551 }
552 }
553
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900554 s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
555 GFP_KERNEL);
556 if (!s5m8767)
557 return -ENOMEM;
558
559 size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
560 s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
561 if (!s5m8767->rdev)
562 return -ENOMEM;
563
564 rdev = s5m8767->rdev;
565 s5m8767->dev = &pdev->dev;
566 s5m8767->iodev = iodev;
567 s5m8767->num_regulators = S5M8767_REG_MAX - 2;
568 platform_set_drvdata(pdev, s5m8767);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900569
570 s5m8767->buck_gpioindex = pdata->buck_default_idx;
571 s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
572 s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
573 s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
574 s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
575 s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
576 s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
577 s5m8767->ramp_delay = pdata->buck_ramp_delay;
578 s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
579 s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
580 s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
Sangbeom Kim7e44bb82012-03-09 17:55:34 +0900581 s5m8767->opmode = pdata->opmode;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900582
583 for (i = 0; i < 8; i++) {
584 if (s5m8767->buck2_gpiodvs) {
585 s5m8767->buck2_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800586 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900587 &buck_voltage_val2,
588 pdata->buck2_voltage[i],
589 pdata->buck2_voltage[i] +
590 buck_voltage_val2.step);
591 }
592
593 if (s5m8767->buck3_gpiodvs) {
594 s5m8767->buck3_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800595 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900596 &buck_voltage_val2,
597 pdata->buck3_voltage[i],
598 pdata->buck3_voltage[i] +
599 buck_voltage_val2.step);
600 }
601
602 if (s5m8767->buck4_gpiodvs) {
603 s5m8767->buck4_vol[i] =
Axel Lin5b5e9772012-03-09 11:31:08 +0800604 s5m8767_convert_voltage_to_sel(
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900605 &buck_voltage_val2,
606 pdata->buck4_voltage[i],
607 pdata->buck4_voltage[i] +
608 buck_voltage_val2.step);
609 }
610 }
611
612 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
613 pdata->buck4_gpiodvs) {
614 if (gpio_is_valid(pdata->buck_gpios[0]) &&
615 gpio_is_valid(pdata->buck_gpios[1]) &&
616 gpio_is_valid(pdata->buck_gpios[2])) {
617 ret = gpio_request(pdata->buck_gpios[0],
618 "S5M8767 SET1");
619 if (ret == -EBUSY)
620 dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
621
622 ret = gpio_request(pdata->buck_gpios[1],
623 "S5M8767 SET2");
624 if (ret == -EBUSY)
625 dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
626
627 ret = gpio_request(pdata->buck_gpios[2],
628 "S5M8767 SET3");
629 if (ret == -EBUSY)
630 dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
631 /* SET1 GPIO */
632 gpio_direction_output(pdata->buck_gpios[0],
633 (s5m8767->buck_gpioindex >> 2) & 0x1);
634 /* SET2 GPIO */
635 gpio_direction_output(pdata->buck_gpios[1],
636 (s5m8767->buck_gpioindex >> 1) & 0x1);
637 /* SET3 GPIO */
638 gpio_direction_output(pdata->buck_gpios[2],
639 (s5m8767->buck_gpioindex >> 0) & 0x1);
640 ret = 0;
641 } else {
642 dev_err(&pdev->dev, "GPIO NOT VALID\n");
643 ret = -EINVAL;
644 return ret;
645 }
646 }
647
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900648 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
649 (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
650 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
651 (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
652 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
653 (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
654
655 /* Initialize GPIO DVS registers */
656 for (i = 0; i < 8; i++) {
657 if (s5m8767->buck2_gpiodvs) {
658 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
659 s5m8767->buck2_vol[i]);
660 }
661
662 if (s5m8767->buck3_gpiodvs) {
663 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
664 s5m8767->buck3_vol[i]);
665 }
666
667 if (s5m8767->buck4_gpiodvs) {
668 s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
669 s5m8767->buck4_vol[i]);
670 }
671 }
672 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
673 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
674 s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
675
676 if (s5m8767->buck2_ramp)
677 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
678
679 if (s5m8767->buck3_ramp)
680 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
681
682 if (s5m8767->buck4_ramp)
683 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
684
685 if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
686 || s5m8767->buck4_ramp) {
687 switch (s5m8767->ramp_delay) {
688 case 15:
689 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
690 0xc0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800691 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900692 case 25:
693 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
694 0xd0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800695 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900696 case 50:
697 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
698 0xe0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800699 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900700 case 100:
701 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
702 0xf0, 0xf0);
Axel Lin047ec222012-01-12 14:57:09 +0800703 break;
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900704 default:
705 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
706 0x90, 0xf0);
707 }
708 }
709
710 for (i = 0; i < pdata->num_regulators; i++) {
711 const struct s5m_voltage_desc *desc;
712 int id = pdata->regulators[i].id;
713
714 desc = reg_voltage_map[id];
715 if (desc)
716 regulators[id].n_voltages =
717 (desc->max - desc->min) / desc->step + 1;
718
Mark Brownc172708d2012-04-04 00:50:22 +0100719 config.dev = s5m8767->dev;
720 config.init_data = pdata->regulators[i].initdata;
721 config.driver_data = s5m8767;
722
723 rdev[i] = regulator_register(&regulators[id], &config);
Sangbeom Kim9767ec72012-01-09 19:10:25 +0900724 if (IS_ERR(rdev[i])) {
725 ret = PTR_ERR(rdev[i]);
726 dev_err(s5m8767->dev, "regulator init failed for %d\n",
727 id);
728 rdev[i] = NULL;
729 goto err;
730 }
731 }
732
733 return 0;
734err:
735 for (i = 0; i < s5m8767->num_regulators; i++)
736 if (rdev[i])
737 regulator_unregister(rdev[i]);
738
739 return ret;
740}
741
742static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
743{
744 struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
745 struct regulator_dev **rdev = s5m8767->rdev;
746 int i;
747
748 for (i = 0; i < s5m8767->num_regulators; i++)
749 if (rdev[i])
750 regulator_unregister(rdev[i]);
751
752 return 0;
753}
754
755static const struct platform_device_id s5m8767_pmic_id[] = {
756 { "s5m8767-pmic", 0},
757 { },
758};
759MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
760
761static struct platform_driver s5m8767_pmic_driver = {
762 .driver = {
763 .name = "s5m8767-pmic",
764 .owner = THIS_MODULE,
765 },
766 .probe = s5m8767_pmic_probe,
767 .remove = __devexit_p(s5m8767_pmic_remove),
768 .id_table = s5m8767_pmic_id,
769};
770
771static int __init s5m8767_pmic_init(void)
772{
773 return platform_driver_register(&s5m8767_pmic_driver);
774}
775subsys_initcall(s5m8767_pmic_init);
776
777static void __exit s5m8767_pmic_exit(void)
778{
779 platform_driver_unregister(&s5m8767_pmic_driver);
780}
781module_exit(s5m8767_pmic_exit);
782
783/* Module information */
784MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
785MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
786MODULE_LICENSE("GPL");