]>
Commit | Line | Data |
---|---|---|
bd6ca2cf MH |
1 | /* |
2 | * max8997.c - Regulator driver for the Maxim 8997/8966 | |
3 | * | |
4 | * Copyright (C) 2011 Samsung Electronics | |
5 | * MyungJoo Ham <myungjoo.ham@smasung.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * This driver is based on max8998.c | |
22 | */ | |
23 | ||
24 | #include <linux/bug.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/err.h> | |
27 | #include <linux/gpio.h> | |
28 | #include <linux/slab.h> | |
65602c32 | 29 | #include <linux/module.h> |
bd6ca2cf MH |
30 | #include <linux/platform_device.h> |
31 | #include <linux/regulator/driver.h> | |
32 | #include <linux/regulator/machine.h> | |
33 | #include <linux/mfd/max8997.h> | |
34 | #include <linux/mfd/max8997-private.h> | |
35 | ||
36 | struct max8997_data { | |
37 | struct device *dev; | |
38 | struct max8997_dev *iodev; | |
39 | int num_regulators; | |
40 | struct regulator_dev **rdev; | |
41 | int ramp_delay; /* in mV/us */ | |
42 | ||
6e0414a5 MH |
43 | bool buck1_gpiodvs; |
44 | bool buck2_gpiodvs; | |
45 | bool buck5_gpiodvs; | |
bd6ca2cf MH |
46 | u8 buck1_vol[8]; |
47 | u8 buck2_vol[8]; | |
48 | u8 buck5_vol[8]; | |
6e0414a5 | 49 | int buck125_gpios[3]; |
bd6ca2cf | 50 | int buck125_gpioindex; |
6e0414a5 | 51 | bool ignore_gpiodvs_side_effect; |
bd6ca2cf MH |
52 | |
53 | u8 saved_states[MAX8997_REG_MAX]; | |
54 | }; | |
55 | ||
56 | static inline void max8997_set_gpio(struct max8997_data *max8997) | |
57 | { | |
bd6ca2cf MH |
58 | int set3 = (max8997->buck125_gpioindex) & 0x1; |
59 | int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1; | |
60 | int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1; | |
61 | ||
6e0414a5 MH |
62 | gpio_set_value(max8997->buck125_gpios[0], set1); |
63 | gpio_set_value(max8997->buck125_gpios[1], set2); | |
64 | gpio_set_value(max8997->buck125_gpios[2], set3); | |
bd6ca2cf MH |
65 | } |
66 | ||
67 | struct voltage_map_desc { | |
68 | int min; | |
69 | int max; | |
70 | int step; | |
71 | unsigned int n_bits; | |
72 | }; | |
73 | ||
74 | /* Voltage maps in mV */ | |
75 | static const struct voltage_map_desc ldo_voltage_map_desc = { | |
76 | .min = 800, .max = 3950, .step = 50, .n_bits = 6, | |
77 | }; /* LDO1 ~ 18, 21 all */ | |
78 | ||
79 | static const struct voltage_map_desc buck1245_voltage_map_desc = { | |
80 | .min = 650, .max = 2225, .step = 25, .n_bits = 6, | |
81 | }; /* Buck1, 2, 4, 5 */ | |
82 | ||
83 | static const struct voltage_map_desc buck37_voltage_map_desc = { | |
84 | .min = 750, .max = 3900, .step = 50, .n_bits = 6, | |
85 | }; /* Buck3, 7 */ | |
86 | ||
87 | /* current map in mA */ | |
88 | static const struct voltage_map_desc charger_current_map_desc = { | |
89 | .min = 200, .max = 950, .step = 50, .n_bits = 4, | |
90 | }; | |
91 | ||
92 | static const struct voltage_map_desc topoff_current_map_desc = { | |
93 | .min = 50, .max = 200, .step = 10, .n_bits = 4, | |
94 | }; | |
95 | ||
96 | static const struct voltage_map_desc *reg_voltage_map[] = { | |
97 | [MAX8997_LDO1] = &ldo_voltage_map_desc, | |
98 | [MAX8997_LDO2] = &ldo_voltage_map_desc, | |
99 | [MAX8997_LDO3] = &ldo_voltage_map_desc, | |
100 | [MAX8997_LDO4] = &ldo_voltage_map_desc, | |
101 | [MAX8997_LDO5] = &ldo_voltage_map_desc, | |
102 | [MAX8997_LDO6] = &ldo_voltage_map_desc, | |
103 | [MAX8997_LDO7] = &ldo_voltage_map_desc, | |
104 | [MAX8997_LDO8] = &ldo_voltage_map_desc, | |
105 | [MAX8997_LDO9] = &ldo_voltage_map_desc, | |
106 | [MAX8997_LDO10] = &ldo_voltage_map_desc, | |
107 | [MAX8997_LDO11] = &ldo_voltage_map_desc, | |
108 | [MAX8997_LDO12] = &ldo_voltage_map_desc, | |
109 | [MAX8997_LDO13] = &ldo_voltage_map_desc, | |
110 | [MAX8997_LDO14] = &ldo_voltage_map_desc, | |
111 | [MAX8997_LDO15] = &ldo_voltage_map_desc, | |
112 | [MAX8997_LDO16] = &ldo_voltage_map_desc, | |
113 | [MAX8997_LDO17] = &ldo_voltage_map_desc, | |
114 | [MAX8997_LDO18] = &ldo_voltage_map_desc, | |
115 | [MAX8997_LDO21] = &ldo_voltage_map_desc, | |
116 | [MAX8997_BUCK1] = &buck1245_voltage_map_desc, | |
117 | [MAX8997_BUCK2] = &buck1245_voltage_map_desc, | |
118 | [MAX8997_BUCK3] = &buck37_voltage_map_desc, | |
119 | [MAX8997_BUCK4] = &buck1245_voltage_map_desc, | |
120 | [MAX8997_BUCK5] = &buck1245_voltage_map_desc, | |
121 | [MAX8997_BUCK6] = NULL, | |
122 | [MAX8997_BUCK7] = &buck37_voltage_map_desc, | |
123 | [MAX8997_EN32KHZ_AP] = NULL, | |
124 | [MAX8997_EN32KHZ_CP] = NULL, | |
125 | [MAX8997_ENVICHG] = NULL, | |
126 | [MAX8997_ESAFEOUT1] = NULL, | |
127 | [MAX8997_ESAFEOUT2] = NULL, | |
128 | [MAX8997_CHARGER_CV] = NULL, | |
129 | [MAX8997_CHARGER] = &charger_current_map_desc, | |
130 | [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc, | |
131 | }; | |
132 | ||
bd6ca2cf MH |
133 | static int max8997_list_voltage_safeout(struct regulator_dev *rdev, |
134 | unsigned int selector) | |
135 | { | |
b3e1348e | 136 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
137 | |
138 | if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) { | |
139 | switch (selector) { | |
140 | case 0: | |
141 | return 4850000; | |
142 | case 1: | |
143 | return 4900000; | |
144 | case 2: | |
145 | return 4950000; | |
146 | case 3: | |
147 | return 3300000; | |
148 | default: | |
149 | return -EINVAL; | |
150 | } | |
151 | } | |
152 | ||
153 | return -EINVAL; | |
154 | } | |
155 | ||
156 | static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev, | |
157 | unsigned int selector) | |
158 | { | |
b3e1348e | 159 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
160 | |
161 | if (rid != MAX8997_CHARGER_CV) | |
162 | goto err; | |
163 | ||
164 | switch (selector) { | |
165 | case 0x00: | |
166 | return 4200000; | |
167 | case 0x01 ... 0x0E: | |
168 | return 4000000 + 20000 * (selector - 0x01); | |
169 | case 0x0F: | |
170 | return 4350000; | |
171 | default: | |
172 | return -EINVAL; | |
173 | } | |
174 | err: | |
175 | return -EINVAL; | |
176 | } | |
177 | ||
178 | static int max8997_list_voltage(struct regulator_dev *rdev, | |
179 | unsigned int selector) | |
180 | { | |
181 | const struct voltage_map_desc *desc; | |
b3e1348e | 182 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
183 | int val; |
184 | ||
185 | if (rid >= ARRAY_SIZE(reg_voltage_map) || | |
186 | rid < 0) | |
187 | return -EINVAL; | |
188 | ||
189 | desc = reg_voltage_map[rid]; | |
190 | if (desc == NULL) | |
191 | return -EINVAL; | |
192 | ||
193 | val = desc->min + desc->step * selector; | |
194 | if (val > desc->max) | |
195 | return -EINVAL; | |
196 | ||
197 | return val * 1000; | |
198 | } | |
199 | ||
200 | static int max8997_get_enable_register(struct regulator_dev *rdev, | |
201 | int *reg, int *mask, int *pattern) | |
202 | { | |
b3e1348e | 203 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
204 | |
205 | switch (rid) { | |
206 | case MAX8997_LDO1 ... MAX8997_LDO21: | |
207 | *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1); | |
208 | *mask = 0xC0; | |
209 | *pattern = 0xC0; | |
210 | break; | |
211 | case MAX8997_BUCK1: | |
212 | *reg = MAX8997_REG_BUCK1CTRL; | |
213 | *mask = 0x01; | |
214 | *pattern = 0x01; | |
215 | break; | |
216 | case MAX8997_BUCK2: | |
217 | *reg = MAX8997_REG_BUCK2CTRL; | |
218 | *mask = 0x01; | |
219 | *pattern = 0x01; | |
220 | break; | |
221 | case MAX8997_BUCK3: | |
222 | *reg = MAX8997_REG_BUCK3CTRL; | |
223 | *mask = 0x01; | |
224 | *pattern = 0x01; | |
225 | break; | |
226 | case MAX8997_BUCK4: | |
227 | *reg = MAX8997_REG_BUCK4CTRL; | |
228 | *mask = 0x01; | |
229 | *pattern = 0x01; | |
230 | break; | |
231 | case MAX8997_BUCK5: | |
232 | *reg = MAX8997_REG_BUCK5CTRL; | |
233 | *mask = 0x01; | |
234 | *pattern = 0x01; | |
235 | break; | |
236 | case MAX8997_BUCK6: | |
237 | *reg = MAX8997_REG_BUCK6CTRL; | |
238 | *mask = 0x01; | |
239 | *pattern = 0x01; | |
240 | break; | |
241 | case MAX8997_BUCK7: | |
242 | *reg = MAX8997_REG_BUCK7CTRL; | |
243 | *mask = 0x01; | |
244 | *pattern = 0x01; | |
245 | break; | |
246 | case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP: | |
247 | *reg = MAX8997_REG_MAINCON1; | |
248 | *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP); | |
249 | *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP); | |
250 | break; | |
251 | case MAX8997_ENVICHG: | |
252 | *reg = MAX8997_REG_MBCCTRL1; | |
253 | *mask = 0x80; | |
254 | *pattern = 0x80; | |
255 | break; | |
256 | case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2: | |
257 | *reg = MAX8997_REG_SAFEOUTCTRL; | |
258 | *mask = 0x40 << (rid - MAX8997_ESAFEOUT1); | |
259 | *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1); | |
260 | break; | |
261 | case MAX8997_CHARGER: | |
262 | *reg = MAX8997_REG_MBCCTRL2; | |
263 | *mask = 0x40; | |
264 | *pattern = 0x40; | |
265 | break; | |
266 | default: | |
267 | /* Not controllable or not exists */ | |
268 | return -EINVAL; | |
bd6ca2cf MH |
269 | } |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | static int max8997_reg_is_enabled(struct regulator_dev *rdev) | |
275 | { | |
276 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
277 | struct i2c_client *i2c = max8997->iodev->i2c; | |
278 | int ret, reg, mask, pattern; | |
279 | u8 val; | |
280 | ||
281 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); | |
282 | if (ret == -EINVAL) | |
283 | return 1; /* "not controllable" */ | |
284 | else if (ret) | |
285 | return ret; | |
286 | ||
287 | ret = max8997_read_reg(i2c, reg, &val); | |
288 | if (ret) | |
289 | return ret; | |
290 | ||
291 | return (val & mask) == pattern; | |
292 | } | |
293 | ||
294 | static int max8997_reg_enable(struct regulator_dev *rdev) | |
295 | { | |
296 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
297 | struct i2c_client *i2c = max8997->iodev->i2c; | |
298 | int ret, reg, mask, pattern; | |
299 | ||
300 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); | |
301 | if (ret) | |
302 | return ret; | |
303 | ||
304 | return max8997_update_reg(i2c, reg, pattern, mask); | |
305 | } | |
306 | ||
307 | static int max8997_reg_disable(struct regulator_dev *rdev) | |
308 | { | |
309 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
310 | struct i2c_client *i2c = max8997->iodev->i2c; | |
311 | int ret, reg, mask, pattern; | |
312 | ||
313 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); | |
314 | if (ret) | |
315 | return ret; | |
316 | ||
317 | return max8997_update_reg(i2c, reg, ~pattern, mask); | |
318 | } | |
319 | ||
320 | static int max8997_get_voltage_register(struct regulator_dev *rdev, | |
321 | int *_reg, int *_shift, int *_mask) | |
322 | { | |
6f43c380 | 323 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
b3e1348e | 324 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
325 | int reg, shift = 0, mask = 0x3f; |
326 | ||
327 | switch (rid) { | |
328 | case MAX8997_LDO1 ... MAX8997_LDO21: | |
329 | reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1); | |
330 | break; | |
331 | case MAX8997_BUCK1: | |
332 | reg = MAX8997_REG_BUCK1DVS1; | |
6f43c380 AL |
333 | if (max8997->buck1_gpiodvs) |
334 | reg += max8997->buck125_gpioindex; | |
bd6ca2cf MH |
335 | break; |
336 | case MAX8997_BUCK2: | |
337 | reg = MAX8997_REG_BUCK2DVS1; | |
6f43c380 AL |
338 | if (max8997->buck2_gpiodvs) |
339 | reg += max8997->buck125_gpioindex; | |
bd6ca2cf MH |
340 | break; |
341 | case MAX8997_BUCK3: | |
342 | reg = MAX8997_REG_BUCK3DVS; | |
343 | break; | |
344 | case MAX8997_BUCK4: | |
345 | reg = MAX8997_REG_BUCK4DVS; | |
346 | break; | |
347 | case MAX8997_BUCK5: | |
348 | reg = MAX8997_REG_BUCK5DVS1; | |
6f43c380 AL |
349 | if (max8997->buck5_gpiodvs) |
350 | reg += max8997->buck125_gpioindex; | |
bd6ca2cf MH |
351 | break; |
352 | case MAX8997_BUCK7: | |
353 | reg = MAX8997_REG_BUCK7DVS; | |
354 | break; | |
355 | case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2: | |
356 | reg = MAX8997_REG_SAFEOUTCTRL; | |
357 | shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0; | |
358 | mask = 0x3; | |
359 | break; | |
360 | case MAX8997_CHARGER_CV: | |
361 | reg = MAX8997_REG_MBCCTRL3; | |
362 | shift = 0; | |
363 | mask = 0xf; | |
364 | break; | |
365 | case MAX8997_CHARGER: | |
366 | reg = MAX8997_REG_MBCCTRL4; | |
367 | shift = 0; | |
368 | mask = 0xf; | |
369 | break; | |
370 | case MAX8997_CHARGER_TOPOFF: | |
371 | reg = MAX8997_REG_MBCCTRL5; | |
372 | shift = 0; | |
373 | mask = 0xf; | |
374 | break; | |
375 | default: | |
376 | return -EINVAL; | |
377 | } | |
378 | ||
379 | *_reg = reg; | |
380 | *_shift = shift; | |
381 | *_mask = mask; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
386 | static int max8997_get_voltage(struct regulator_dev *rdev) | |
387 | { | |
388 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
bd6ca2cf MH |
389 | struct i2c_client *i2c = max8997->iodev->i2c; |
390 | int reg, shift, mask, ret; | |
bd6ca2cf MH |
391 | u8 val; |
392 | ||
393 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); | |
394 | if (ret) | |
395 | return ret; | |
396 | ||
bd6ca2cf MH |
397 | ret = max8997_read_reg(i2c, reg, &val); |
398 | if (ret) | |
399 | return ret; | |
400 | ||
401 | val >>= shift; | |
402 | val &= mask; | |
403 | ||
404 | if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage) | |
405 | return rdev->desc->ops->list_voltage(rdev, val); | |
406 | ||
407 | /* | |
408 | * max8997_list_voltage returns value for any rdev with voltage_map, | |
409 | * which works for "CHARGER" and "CHARGER TOPOFF" that do not have | |
410 | * list_voltage ops (they are current regulators). | |
411 | */ | |
412 | return max8997_list_voltage(rdev, val); | |
413 | } | |
414 | ||
415 | static inline int max8997_get_voltage_proper_val( | |
416 | const struct voltage_map_desc *desc, | |
417 | int min_vol, int max_vol) | |
418 | { | |
419 | int i = 0; | |
420 | ||
421 | if (desc == NULL) | |
422 | return -EINVAL; | |
423 | ||
424 | if (max_vol < desc->min || min_vol > desc->max) | |
425 | return -EINVAL; | |
426 | ||
427 | while (desc->min + desc->step * i < min_vol && | |
428 | desc->min + desc->step * i < desc->max) | |
429 | i++; | |
430 | ||
431 | if (desc->min + desc->step * i > max_vol) | |
432 | return -EINVAL; | |
433 | ||
434 | if (i >= (1 << desc->n_bits)) | |
435 | return -EINVAL; | |
436 | ||
437 | return i; | |
438 | } | |
439 | ||
440 | static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev, | |
441 | int min_uV, int max_uV, unsigned *selector) | |
442 | { | |
443 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
444 | struct i2c_client *i2c = max8997->iodev->i2c; | |
b3e1348e | 445 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
446 | int lb, ub; |
447 | int reg, shift = 0, mask, ret = 0; | |
448 | u8 val = 0x0; | |
449 | ||
450 | if (rid != MAX8997_CHARGER_CV) | |
451 | return -EINVAL; | |
452 | ||
453 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); | |
454 | if (ret) | |
455 | return ret; | |
456 | ||
457 | if (max_uV < 4000000 || min_uV > 4350000) | |
458 | return -EINVAL; | |
459 | ||
460 | if (min_uV <= 4000000) { | |
461 | if (max_uV >= 4000000) | |
462 | return -EINVAL; | |
463 | else | |
464 | val = 0x1; | |
465 | } else if (min_uV <= 4200000 && max_uV >= 4200000) | |
466 | val = 0x0; | |
467 | else { | |
468 | lb = (min_uV - 4000001) / 20000 + 2; | |
469 | ub = (max_uV - 4000000) / 20000 + 1; | |
470 | ||
471 | if (lb > ub) | |
472 | return -EINVAL; | |
473 | ||
474 | if (lb < 0xf) | |
475 | val = lb; | |
476 | else { | |
477 | if (ub >= 0xf) | |
478 | val = 0xf; | |
479 | else | |
480 | return -EINVAL; | |
481 | } | |
482 | } | |
483 | ||
484 | *selector = val; | |
485 | ||
486 | ret = max8997_update_reg(i2c, reg, val << shift, mask); | |
487 | ||
488 | return ret; | |
489 | } | |
490 | ||
491 | /* | |
492 | * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF | |
493 | * BUCK1, 2, and 5 are available if they are not controlled by gpio | |
494 | */ | |
495 | static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev, | |
496 | int min_uV, int max_uV, unsigned *selector) | |
497 | { | |
498 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
499 | struct i2c_client *i2c = max8997->iodev->i2c; | |
500 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; | |
501 | const struct voltage_map_desc *desc; | |
b3e1348e | 502 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
503 | int reg, shift = 0, mask, ret; |
504 | int i; | |
505 | u8 org; | |
506 | ||
507 | switch (rid) { | |
508 | case MAX8997_LDO1 ... MAX8997_LDO21: | |
509 | break; | |
510 | case MAX8997_BUCK1 ... MAX8997_BUCK5: | |
511 | break; | |
512 | case MAX8997_BUCK6: | |
513 | return -EINVAL; | |
514 | case MAX8997_BUCK7: | |
515 | break; | |
516 | case MAX8997_CHARGER: | |
517 | break; | |
518 | case MAX8997_CHARGER_TOPOFF: | |
519 | break; | |
520 | default: | |
521 | return -EINVAL; | |
522 | } | |
523 | ||
524 | desc = reg_voltage_map[rid]; | |
525 | ||
526 | i = max8997_get_voltage_proper_val(desc, min_vol, max_vol); | |
527 | if (i < 0) | |
528 | return i; | |
529 | ||
530 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); | |
531 | if (ret) | |
532 | return ret; | |
533 | ||
534 | max8997_read_reg(i2c, reg, &org); | |
535 | org = (org & mask) >> shift; | |
536 | ||
537 | ret = max8997_update_reg(i2c, reg, i << shift, mask << shift); | |
538 | *selector = i; | |
539 | ||
540 | if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 || | |
541 | rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) { | |
542 | /* If the voltage is increasing */ | |
543 | if (org < i) | |
6ff325a2 DK |
544 | udelay(DIV_ROUND_UP(desc->step * (i - org), |
545 | max8997->ramp_delay)); | |
bd6ca2cf MH |
546 | } |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Assess the damage on the voltage setting of BUCK1,2,5 by the change. | |
553 | * | |
554 | * When GPIO-DVS mode is used for multiple bucks, changing the voltage value | |
555 | * of one of the bucks may affect that of another buck, which is the side | |
556 | * effect of the change (set_voltage). This function examines the GPIO-DVS | |
557 | * configurations and checks whether such side-effect exists. | |
558 | */ | |
559 | static int max8997_assess_side_effect(struct regulator_dev *rdev, | |
560 | u8 new_val, int *best) | |
561 | { | |
562 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
b3e1348e | 563 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
564 | u8 *buckx_val[3]; |
565 | bool buckx_gpiodvs[3]; | |
566 | int side_effect[8]; | |
567 | int min_side_effect = INT_MAX; | |
568 | int i; | |
569 | ||
570 | *best = -1; | |
571 | ||
572 | switch (rid) { | |
573 | case MAX8997_BUCK1: | |
574 | rid = 0; | |
575 | break; | |
576 | case MAX8997_BUCK2: | |
577 | rid = 1; | |
578 | break; | |
579 | case MAX8997_BUCK5: | |
580 | rid = 2; | |
581 | break; | |
582 | default: | |
583 | return -EINVAL; | |
584 | } | |
585 | ||
586 | buckx_val[0] = max8997->buck1_vol; | |
587 | buckx_val[1] = max8997->buck2_vol; | |
588 | buckx_val[2] = max8997->buck5_vol; | |
6e0414a5 MH |
589 | buckx_gpiodvs[0] = max8997->buck1_gpiodvs; |
590 | buckx_gpiodvs[1] = max8997->buck2_gpiodvs; | |
591 | buckx_gpiodvs[2] = max8997->buck5_gpiodvs; | |
bd6ca2cf MH |
592 | |
593 | for (i = 0; i < 8; i++) { | |
594 | int others; | |
595 | ||
596 | if (new_val != (buckx_val[rid])[i]) { | |
597 | side_effect[i] = -1; | |
598 | continue; | |
599 | } | |
600 | ||
601 | side_effect[i] = 0; | |
602 | for (others = 0; others < 3; others++) { | |
603 | int diff; | |
604 | ||
605 | if (others == rid) | |
606 | continue; | |
607 | if (buckx_gpiodvs[others] == false) | |
608 | continue; /* Not affected */ | |
609 | diff = (buckx_val[others])[i] - | |
610 | (buckx_val[others])[max8997->buck125_gpioindex]; | |
611 | if (diff > 0) | |
612 | side_effect[i] += diff; | |
613 | else if (diff < 0) | |
614 | side_effect[i] -= diff; | |
615 | } | |
616 | if (side_effect[i] == 0) { | |
617 | *best = i; | |
618 | return 0; /* NO SIDE EFFECT! Use This! */ | |
619 | } | |
620 | if (side_effect[i] < min_side_effect) { | |
621 | min_side_effect = side_effect[i]; | |
622 | *best = i; | |
623 | } | |
624 | } | |
625 | ||
626 | if (*best == -1) | |
627 | return -EINVAL; | |
628 | ||
629 | return side_effect[*best]; | |
630 | } | |
631 | ||
632 | /* | |
633 | * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls | |
634 | * max8997_set_voltage_ldobuck to do the job. | |
635 | */ | |
636 | static int max8997_set_voltage_buck(struct regulator_dev *rdev, | |
637 | int min_uV, int max_uV, unsigned *selector) | |
638 | { | |
639 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
b3e1348e | 640 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
641 | const struct voltage_map_desc *desc; |
642 | int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg; | |
643 | bool gpio_dvs_mode = false; | |
644 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; | |
645 | ||
646 | if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7) | |
647 | return -EINVAL; | |
648 | ||
649 | switch (rid) { | |
650 | case MAX8997_BUCK1: | |
6e0414a5 | 651 | if (max8997->buck1_gpiodvs) |
bd6ca2cf MH |
652 | gpio_dvs_mode = true; |
653 | break; | |
654 | case MAX8997_BUCK2: | |
6e0414a5 | 655 | if (max8997->buck2_gpiodvs) |
bd6ca2cf MH |
656 | gpio_dvs_mode = true; |
657 | break; | |
658 | case MAX8997_BUCK5: | |
6e0414a5 | 659 | if (max8997->buck5_gpiodvs) |
bd6ca2cf MH |
660 | gpio_dvs_mode = true; |
661 | break; | |
662 | } | |
663 | ||
664 | if (!gpio_dvs_mode) | |
665 | return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, | |
666 | selector); | |
667 | ||
668 | desc = reg_voltage_map[rid]; | |
669 | new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol); | |
670 | if (new_val < 0) | |
671 | return new_val; | |
672 | ||
673 | tmp_dmg = INT_MAX; | |
674 | tmp_idx = -1; | |
675 | tmp_val = -1; | |
676 | do { | |
677 | damage = max8997_assess_side_effect(rdev, new_val, &new_idx); | |
678 | if (damage == 0) | |
679 | goto out; | |
680 | ||
681 | if (tmp_dmg > damage) { | |
682 | tmp_idx = new_idx; | |
683 | tmp_val = new_val; | |
684 | tmp_dmg = damage; | |
685 | } | |
686 | ||
687 | new_val++; | |
688 | } while (desc->min + desc->step + new_val <= desc->max); | |
689 | ||
690 | new_idx = tmp_idx; | |
691 | new_val = tmp_val; | |
692 | ||
6e0414a5 | 693 | if (max8997->ignore_gpiodvs_side_effect == false) |
bd6ca2cf MH |
694 | return -EINVAL; |
695 | ||
696 | dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:" | |
697 | " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx); | |
698 | ||
699 | out: | |
700 | if (new_idx < 0 || new_val < 0) | |
701 | return -EINVAL; | |
702 | ||
703 | max8997->buck125_gpioindex = new_idx; | |
704 | max8997_set_gpio(max8997); | |
705 | *selector = new_val; | |
706 | ||
707 | return 0; | |
708 | } | |
709 | ||
710 | static const int safeoutvolt[] = { | |
711 | 3300000, | |
712 | 4850000, | |
713 | 4900000, | |
714 | 4950000, | |
715 | }; | |
716 | ||
717 | /* For SAFEOUT1 and SAFEOUT2 */ | |
718 | static int max8997_set_voltage_safeout(struct regulator_dev *rdev, | |
719 | int min_uV, int max_uV, unsigned *selector) | |
720 | { | |
721 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
722 | struct i2c_client *i2c = max8997->iodev->i2c; | |
b3e1348e | 723 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
724 | int reg, shift = 0, mask, ret; |
725 | int i = 0; | |
726 | u8 val; | |
727 | ||
728 | if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2) | |
729 | return -EINVAL; | |
730 | ||
731 | for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) { | |
732 | if (min_uV <= safeoutvolt[i] && | |
733 | max_uV >= safeoutvolt[i]) | |
734 | break; | |
735 | } | |
736 | ||
737 | if (i >= ARRAY_SIZE(safeoutvolt)) | |
738 | return -EINVAL; | |
739 | ||
740 | if (i == 0) | |
741 | val = 0x3; | |
742 | else | |
743 | val = i - 1; | |
744 | ||
745 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); | |
746 | if (ret) | |
747 | return ret; | |
748 | ||
749 | ret = max8997_update_reg(i2c, reg, val << shift, mask << shift); | |
750 | *selector = val; | |
751 | ||
752 | return ret; | |
753 | } | |
754 | ||
755 | static int max8997_reg_enable_suspend(struct regulator_dev *rdev) | |
756 | { | |
757 | return 0; | |
758 | } | |
759 | ||
760 | static int max8997_reg_disable_suspend(struct regulator_dev *rdev) | |
761 | { | |
762 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); | |
763 | struct i2c_client *i2c = max8997->iodev->i2c; | |
764 | int ret, reg, mask, pattern; | |
b3e1348e | 765 | int rid = rdev_get_id(rdev); |
bd6ca2cf MH |
766 | |
767 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); | |
768 | if (ret) | |
769 | return ret; | |
770 | ||
771 | max8997_read_reg(i2c, reg, &max8997->saved_states[rid]); | |
772 | ||
773 | if (rid == MAX8997_LDO1 || | |
774 | rid == MAX8997_LDO10 || | |
775 | rid == MAX8997_LDO21) { | |
776 | dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n", | |
777 | rdev->desc->name); | |
778 | return max8997_update_reg(i2c, reg, 0x40, mask); | |
779 | } | |
780 | ||
781 | dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n", | |
782 | rdev->desc->name, max8997->saved_states[rid] & mask, | |
783 | (~pattern) & mask); | |
784 | return max8997_update_reg(i2c, reg, ~pattern, mask); | |
785 | } | |
786 | ||
787 | static struct regulator_ops max8997_ldo_ops = { | |
788 | .list_voltage = max8997_list_voltage, | |
789 | .is_enabled = max8997_reg_is_enabled, | |
790 | .enable = max8997_reg_enable, | |
791 | .disable = max8997_reg_disable, | |
792 | .get_voltage = max8997_get_voltage, | |
793 | .set_voltage = max8997_set_voltage_ldobuck, | |
794 | .set_suspend_enable = max8997_reg_enable_suspend, | |
795 | .set_suspend_disable = max8997_reg_disable_suspend, | |
796 | }; | |
797 | ||
798 | static struct regulator_ops max8997_buck_ops = { | |
799 | .list_voltage = max8997_list_voltage, | |
800 | .is_enabled = max8997_reg_is_enabled, | |
801 | .enable = max8997_reg_enable, | |
802 | .disable = max8997_reg_disable, | |
803 | .get_voltage = max8997_get_voltage, | |
804 | .set_voltage = max8997_set_voltage_buck, | |
805 | .set_suspend_enable = max8997_reg_enable_suspend, | |
806 | .set_suspend_disable = max8997_reg_disable_suspend, | |
807 | }; | |
808 | ||
809 | static struct regulator_ops max8997_fixedvolt_ops = { | |
810 | .list_voltage = max8997_list_voltage, | |
811 | .is_enabled = max8997_reg_is_enabled, | |
812 | .enable = max8997_reg_enable, | |
813 | .disable = max8997_reg_disable, | |
814 | .set_suspend_enable = max8997_reg_enable_suspend, | |
815 | .set_suspend_disable = max8997_reg_disable_suspend, | |
816 | }; | |
817 | ||
818 | static struct regulator_ops max8997_safeout_ops = { | |
819 | .list_voltage = max8997_list_voltage_safeout, | |
820 | .is_enabled = max8997_reg_is_enabled, | |
821 | .enable = max8997_reg_enable, | |
822 | .disable = max8997_reg_disable, | |
823 | .get_voltage = max8997_get_voltage, | |
824 | .set_voltage = max8997_set_voltage_safeout, | |
825 | .set_suspend_enable = max8997_reg_enable_suspend, | |
826 | .set_suspend_disable = max8997_reg_disable_suspend, | |
827 | }; | |
828 | ||
829 | static struct regulator_ops max8997_fixedstate_ops = { | |
830 | .list_voltage = max8997_list_voltage_charger_cv, | |
831 | .get_voltage = max8997_get_voltage, | |
832 | .set_voltage = max8997_set_voltage_charger_cv, | |
833 | }; | |
834 | ||
835 | static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev, | |
836 | int min_uV, int max_uV) | |
837 | { | |
838 | unsigned dummy; | |
839 | ||
840 | return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy); | |
841 | } | |
842 | ||
843 | ||
844 | static struct regulator_ops max8997_charger_ops = { | |
845 | .is_enabled = max8997_reg_is_enabled, | |
846 | .enable = max8997_reg_enable, | |
847 | .disable = max8997_reg_disable, | |
848 | .get_current_limit = max8997_get_voltage, | |
849 | .set_current_limit = max8997_set_voltage_ldobuck_wrap, | |
850 | }; | |
851 | ||
852 | static struct regulator_ops max8997_charger_fixedstate_ops = { | |
853 | .is_enabled = max8997_reg_is_enabled, | |
854 | .get_current_limit = max8997_get_voltage, | |
855 | .set_current_limit = max8997_set_voltage_ldobuck_wrap, | |
856 | }; | |
857 | ||
4533f80e AL |
858 | #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\ |
859 | .name = #_name, \ | |
860 | .id = MAX8997_##_name, \ | |
861 | .ops = &_ops, \ | |
bd6ca2cf MH |
862 | .type = REGULATOR_VOLTAGE, \ |
863 | .owner = THIS_MODULE, \ | |
864 | } | |
4533f80e AL |
865 | |
866 | #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\ | |
867 | .name = #_name, \ | |
868 | .id = MAX8997_##_name, \ | |
869 | .ops = &_ops, \ | |
870 | .type = REGULATOR_CURRENT, \ | |
bd6ca2cf MH |
871 | .owner = THIS_MODULE, \ |
872 | } | |
873 | ||
874 | static struct regulator_desc regulators[] = { | |
4533f80e AL |
875 | MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops), |
876 | MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops), | |
877 | MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops), | |
878 | MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops), | |
879 | MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops), | |
880 | MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops), | |
881 | MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops), | |
882 | MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops), | |
883 | MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops), | |
884 | MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops), | |
885 | MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops), | |
886 | MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops), | |
887 | MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops), | |
888 | MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops), | |
889 | MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops), | |
890 | MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops), | |
891 | MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops), | |
892 | MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops), | |
893 | MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops), | |
894 | MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops), | |
895 | MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops), | |
896 | MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops), | |
897 | MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops), | |
898 | MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops), | |
899 | MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops), | |
900 | MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops), | |
901 | MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops), | |
902 | MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops), | |
903 | MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops), | |
904 | MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops), | |
905 | MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops), | |
906 | MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops), | |
907 | MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops), | |
908 | MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF, | |
909 | max8997_charger_fixedstate_ops), | |
bd6ca2cf MH |
910 | }; |
911 | ||
912 | static __devinit int max8997_pmic_probe(struct platform_device *pdev) | |
913 | { | |
914 | struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
915 | struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev); | |
916 | struct regulator_dev **rdev; | |
917 | struct max8997_data *max8997; | |
918 | struct i2c_client *i2c; | |
919 | int i, ret, size; | |
920 | u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0; | |
921 | ||
922 | if (!pdata) { | |
923 | dev_err(pdev->dev.parent, "No platform init data supplied.\n"); | |
924 | return -ENODEV; | |
925 | } | |
926 | ||
927 | max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL); | |
928 | if (!max8997) | |
929 | return -ENOMEM; | |
930 | ||
931 | size = sizeof(struct regulator_dev *) * pdata->num_regulators; | |
932 | max8997->rdev = kzalloc(size, GFP_KERNEL); | |
933 | if (!max8997->rdev) { | |
934 | kfree(max8997); | |
935 | return -ENOMEM; | |
936 | } | |
937 | ||
938 | rdev = max8997->rdev; | |
939 | max8997->dev = &pdev->dev; | |
940 | max8997->iodev = iodev; | |
941 | max8997->num_regulators = pdata->num_regulators; | |
942 | platform_set_drvdata(pdev, max8997); | |
943 | i2c = max8997->iodev->i2c; | |
944 | ||
945 | max8997->buck125_gpioindex = pdata->buck125_default_idx; | |
6e0414a5 MH |
946 | max8997->buck1_gpiodvs = pdata->buck1_gpiodvs; |
947 | max8997->buck2_gpiodvs = pdata->buck2_gpiodvs; | |
948 | max8997->buck5_gpiodvs = pdata->buck5_gpiodvs; | |
949 | memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3); | |
950 | max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect; | |
bd6ca2cf MH |
951 | |
952 | for (i = 0; i < 8; i++) { | |
953 | max8997->buck1_vol[i] = ret = | |
954 | max8997_get_voltage_proper_val( | |
955 | &buck1245_voltage_map_desc, | |
956 | pdata->buck1_voltage[i] / 1000, | |
957 | pdata->buck1_voltage[i] / 1000 + | |
958 | buck1245_voltage_map_desc.step); | |
959 | if (ret < 0) | |
960 | goto err_alloc; | |
961 | ||
962 | max8997->buck2_vol[i] = ret = | |
963 | max8997_get_voltage_proper_val( | |
964 | &buck1245_voltage_map_desc, | |
965 | pdata->buck2_voltage[i] / 1000, | |
966 | pdata->buck2_voltage[i] / 1000 + | |
967 | buck1245_voltage_map_desc.step); | |
968 | if (ret < 0) | |
969 | goto err_alloc; | |
970 | ||
971 | max8997->buck5_vol[i] = ret = | |
972 | max8997_get_voltage_proper_val( | |
973 | &buck1245_voltage_map_desc, | |
974 | pdata->buck5_voltage[i] / 1000, | |
975 | pdata->buck5_voltage[i] / 1000 + | |
976 | buck1245_voltage_map_desc.step); | |
977 | if (ret < 0) | |
978 | goto err_alloc; | |
979 | ||
980 | if (max_buck1 < max8997->buck1_vol[i]) | |
981 | max_buck1 = max8997->buck1_vol[i]; | |
982 | if (max_buck2 < max8997->buck2_vol[i]) | |
983 | max_buck2 = max8997->buck2_vol[i]; | |
984 | if (max_buck5 < max8997->buck5_vol[i]) | |
985 | max_buck5 = max8997->buck5_vol[i]; | |
986 | } | |
987 | ||
988 | /* For the safety, set max voltage before setting up */ | |
989 | for (i = 0; i < 8; i++) { | |
ecb9c4f5 | 990 | max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i, |
bd6ca2cf | 991 | max_buck1, 0x3f); |
ecb9c4f5 | 992 | max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i, |
bd6ca2cf | 993 | max_buck2, 0x3f); |
ecb9c4f5 | 994 | max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i, |
bd6ca2cf MH |
995 | max_buck5, 0x3f); |
996 | } | |
997 | ||
998 | /* | |
999 | * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them. | |
1000 | * If at least one of them cares, set gpios. | |
1001 | */ | |
1002 | if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs || | |
1003 | pdata->buck5_gpiodvs) { | |
1004 | bool gpio1set = false, gpio2set = false; | |
1005 | ||
1006 | if (!gpio_is_valid(pdata->buck125_gpios[0]) || | |
1007 | !gpio_is_valid(pdata->buck125_gpios[1]) || | |
1008 | !gpio_is_valid(pdata->buck125_gpios[2])) { | |
1009 | dev_err(&pdev->dev, "GPIO NOT VALID\n"); | |
1010 | ret = -EINVAL; | |
1011 | goto err_alloc; | |
1012 | } | |
1013 | ||
1014 | ret = gpio_request(pdata->buck125_gpios[0], | |
1015 | "MAX8997 SET1"); | |
1016 | if (ret == -EBUSY) | |
1017 | dev_warn(&pdev->dev, "Duplicated gpio request" | |
1018 | " on SET1\n"); | |
1019 | else if (ret) | |
1020 | goto err_alloc; | |
1021 | else | |
1022 | gpio1set = true; | |
1023 | ||
1024 | ret = gpio_request(pdata->buck125_gpios[1], | |
1025 | "MAX8997 SET2"); | |
1026 | if (ret == -EBUSY) | |
1027 | dev_warn(&pdev->dev, "Duplicated gpio request" | |
1028 | " on SET2\n"); | |
1029 | else if (ret) { | |
1030 | if (gpio1set) | |
1031 | gpio_free(pdata->buck125_gpios[0]); | |
1032 | goto err_alloc; | |
1033 | } else | |
1034 | gpio2set = true; | |
1035 | ||
1036 | ret = gpio_request(pdata->buck125_gpios[2], | |
1037 | "MAX8997 SET3"); | |
1038 | if (ret == -EBUSY) | |
1039 | dev_warn(&pdev->dev, "Duplicated gpio request" | |
1040 | " on SET3\n"); | |
1041 | else if (ret) { | |
1042 | if (gpio1set) | |
1043 | gpio_free(pdata->buck125_gpios[0]); | |
1044 | if (gpio2set) | |
1045 | gpio_free(pdata->buck125_gpios[1]); | |
1046 | goto err_alloc; | |
1047 | } | |
1048 | ||
1049 | gpio_direction_output(pdata->buck125_gpios[0], | |
1050 | (max8997->buck125_gpioindex >> 2) | |
1051 | & 0x1); /* SET1 */ | |
1052 | gpio_direction_output(pdata->buck125_gpios[1], | |
1053 | (max8997->buck125_gpioindex >> 1) | |
1054 | & 0x1); /* SET2 */ | |
1055 | gpio_direction_output(pdata->buck125_gpios[2], | |
1056 | (max8997->buck125_gpioindex >> 0) | |
1057 | & 0x1); /* SET3 */ | |
1058 | ret = 0; | |
1059 | } | |
1060 | ||
1061 | /* DVS-GPIO disabled */ | |
1062 | max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ? | |
1063 | (1 << 1) : (0 << 1), 1 << 1); | |
1064 | max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ? | |
1065 | (1 << 1) : (0 << 1), 1 << 1); | |
1066 | max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ? | |
1067 | (1 << 1) : (0 << 1), 1 << 1); | |
1068 | ||
1069 | /* Initialize all the DVS related BUCK registers */ | |
1070 | for (i = 0; i < 8; i++) { | |
ecb9c4f5 | 1071 | max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i, |
bd6ca2cf MH |
1072 | max8997->buck1_vol[i], |
1073 | 0x3f); | |
ecb9c4f5 | 1074 | max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i, |
bd6ca2cf MH |
1075 | max8997->buck2_vol[i], |
1076 | 0x3f); | |
ecb9c4f5 | 1077 | max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i, |
bd6ca2cf MH |
1078 | max8997->buck5_vol[i], |
1079 | 0x3f); | |
1080 | } | |
1081 | ||
dbb48e7c TB |
1082 | /* Misc Settings */ |
1083 | max8997->ramp_delay = 10; /* set 10mV/us, which is the default */ | |
1084 | max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9); | |
1085 | ||
bd6ca2cf MH |
1086 | for (i = 0; i < pdata->num_regulators; i++) { |
1087 | const struct voltage_map_desc *desc; | |
1088 | int id = pdata->regulators[i].id; | |
1089 | ||
1090 | desc = reg_voltage_map[id]; | |
1091 | if (desc) | |
1092 | regulators[id].n_voltages = | |
1093 | (desc->max - desc->min) / desc->step + 1; | |
1094 | else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2) | |
1095 | regulators[id].n_voltages = 4; | |
1096 | else if (id == MAX8997_CHARGER_CV) | |
1097 | regulators[id].n_voltages = 16; | |
1098 | ||
1099 | rdev[i] = regulator_register(®ulators[id], max8997->dev, | |
2c043bcb | 1100 | pdata->regulators[i].initdata, max8997, NULL); |
bd6ca2cf MH |
1101 | if (IS_ERR(rdev[i])) { |
1102 | ret = PTR_ERR(rdev[i]); | |
1103 | dev_err(max8997->dev, "regulator init failed for %d\n", | |
1104 | id); | |
1105 | rdev[i] = NULL; | |
1106 | goto err; | |
1107 | } | |
1108 | } | |
1109 | ||
bd6ca2cf MH |
1110 | return 0; |
1111 | err: | |
1112 | for (i = 0; i < max8997->num_regulators; i++) | |
1113 | if (rdev[i]) | |
1114 | regulator_unregister(rdev[i]); | |
1115 | err_alloc: | |
1116 | kfree(max8997->rdev); | |
1117 | kfree(max8997); | |
1118 | ||
1119 | return ret; | |
1120 | } | |
1121 | ||
1122 | static int __devexit max8997_pmic_remove(struct platform_device *pdev) | |
1123 | { | |
1124 | struct max8997_data *max8997 = platform_get_drvdata(pdev); | |
1125 | struct regulator_dev **rdev = max8997->rdev; | |
1126 | int i; | |
1127 | ||
1128 | for (i = 0; i < max8997->num_regulators; i++) | |
1129 | if (rdev[i]) | |
1130 | regulator_unregister(rdev[i]); | |
1131 | ||
1132 | kfree(max8997->rdev); | |
1133 | kfree(max8997); | |
1134 | ||
1135 | return 0; | |
1136 | } | |
1137 | ||
1138 | static const struct platform_device_id max8997_pmic_id[] = { | |
1139 | { "max8997-pmic", 0}, | |
1140 | { }, | |
1141 | }; | |
a51b907b | 1142 | MODULE_DEVICE_TABLE(platform, max8997_pmic_id); |
bd6ca2cf MH |
1143 | |
1144 | static struct platform_driver max8997_pmic_driver = { | |
1145 | .driver = { | |
1146 | .name = "max8997-pmic", | |
1147 | .owner = THIS_MODULE, | |
1148 | }, | |
1149 | .probe = max8997_pmic_probe, | |
1150 | .remove = __devexit_p(max8997_pmic_remove), | |
1151 | .id_table = max8997_pmic_id, | |
1152 | }; | |
1153 | ||
1154 | static int __init max8997_pmic_init(void) | |
1155 | { | |
1156 | return platform_driver_register(&max8997_pmic_driver); | |
1157 | } | |
1158 | subsys_initcall(max8997_pmic_init); | |
1159 | ||
1160 | static void __exit max8997_pmic_cleanup(void) | |
1161 | { | |
1162 | platform_driver_unregister(&max8997_pmic_driver); | |
1163 | } | |
1164 | module_exit(max8997_pmic_cleanup); | |
1165 | ||
1166 | MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver"); | |
1167 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
1168 | MODULE_LICENSE("GPL"); |