USB: workaround for bug in old version of GCC
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / max8997.c
blob6176129a27e531e20c8921537798648c723f78ec
1 /*
2 * max8997.c - Regulator driver for the Maxim 8997/8966
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@smasung.com>
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.
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.
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
21 * This driver is based on max8998.c
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>
29 #include <linux/module.h>
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>
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 */
43 bool buck1_gpiodvs;
44 bool buck2_gpiodvs;
45 bool buck5_gpiodvs;
46 u8 buck1_vol[8];
47 u8 buck2_vol[8];
48 u8 buck5_vol[8];
49 int buck125_gpios[3];
50 int buck125_gpioindex;
51 bool ignore_gpiodvs_side_effect;
53 u8 saved_states[MAX8997_REG_MAX];
56 static inline void max8997_set_gpio(struct max8997_data *max8997)
58 int set3 = (max8997->buck125_gpioindex) & 0x1;
59 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
60 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
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);
67 struct voltage_map_desc {
68 int min;
69 int max;
70 int step;
71 unsigned int n_bits;
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 */
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 */
83 static const struct voltage_map_desc buck37_voltage_map_desc = {
84 .min = 750, .max = 3900, .step = 50, .n_bits = 6,
85 }; /* Buck3, 7 */
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,
92 static const struct voltage_map_desc topoff_current_map_desc = {
93 .min = 50, .max = 200, .step = 10, .n_bits = 4,
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,
133 static inline int max8997_get_rid(struct regulator_dev *rdev)
135 return rdev_get_id(rdev);
138 static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
139 unsigned int selector)
141 int rid = max8997_get_rid(rdev);
143 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
144 switch (selector) {
145 case 0:
146 return 4850000;
147 case 1:
148 return 4900000;
149 case 2:
150 return 4950000;
151 case 3:
152 return 3300000;
153 default:
154 return -EINVAL;
158 return -EINVAL;
161 static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
162 unsigned int selector)
164 int rid = max8997_get_rid(rdev);
166 if (rid != MAX8997_CHARGER_CV)
167 goto err;
169 switch (selector) {
170 case 0x00:
171 return 4200000;
172 case 0x01 ... 0x0E:
173 return 4000000 + 20000 * (selector - 0x01);
174 case 0x0F:
175 return 4350000;
176 default:
177 return -EINVAL;
179 err:
180 return -EINVAL;
183 static int max8997_list_voltage(struct regulator_dev *rdev,
184 unsigned int selector)
186 const struct voltage_map_desc *desc;
187 int rid = max8997_get_rid(rdev);
188 int val;
190 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
191 rid < 0)
192 return -EINVAL;
194 desc = reg_voltage_map[rid];
195 if (desc == NULL)
196 return -EINVAL;
198 val = desc->min + desc->step * selector;
199 if (val > desc->max)
200 return -EINVAL;
202 return val * 1000;
205 static int max8997_get_enable_register(struct regulator_dev *rdev,
206 int *reg, int *mask, int *pattern)
208 int rid = max8997_get_rid(rdev);
210 switch (rid) {
211 case MAX8997_LDO1 ... MAX8997_LDO21:
212 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
213 *mask = 0xC0;
214 *pattern = 0xC0;
215 break;
216 case MAX8997_BUCK1:
217 *reg = MAX8997_REG_BUCK1CTRL;
218 *mask = 0x01;
219 *pattern = 0x01;
220 break;
221 case MAX8997_BUCK2:
222 *reg = MAX8997_REG_BUCK2CTRL;
223 *mask = 0x01;
224 *pattern = 0x01;
225 break;
226 case MAX8997_BUCK3:
227 *reg = MAX8997_REG_BUCK3CTRL;
228 *mask = 0x01;
229 *pattern = 0x01;
230 break;
231 case MAX8997_BUCK4:
232 *reg = MAX8997_REG_BUCK4CTRL;
233 *mask = 0x01;
234 *pattern = 0x01;
235 break;
236 case MAX8997_BUCK5:
237 *reg = MAX8997_REG_BUCK5CTRL;
238 *mask = 0x01;
239 *pattern = 0x01;
240 break;
241 case MAX8997_BUCK6:
242 *reg = MAX8997_REG_BUCK6CTRL;
243 *mask = 0x01;
244 *pattern = 0x01;
245 break;
246 case MAX8997_BUCK7:
247 *reg = MAX8997_REG_BUCK7CTRL;
248 *mask = 0x01;
249 *pattern = 0x01;
250 break;
251 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
252 *reg = MAX8997_REG_MAINCON1;
253 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
254 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
255 break;
256 case MAX8997_ENVICHG:
257 *reg = MAX8997_REG_MBCCTRL1;
258 *mask = 0x80;
259 *pattern = 0x80;
260 break;
261 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
262 *reg = MAX8997_REG_SAFEOUTCTRL;
263 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
264 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
265 break;
266 case MAX8997_CHARGER:
267 *reg = MAX8997_REG_MBCCTRL2;
268 *mask = 0x40;
269 *pattern = 0x40;
270 break;
271 default:
272 /* Not controllable or not exists */
273 return -EINVAL;
276 return 0;
279 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
281 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
282 struct i2c_client *i2c = max8997->iodev->i2c;
283 int ret, reg, mask, pattern;
284 u8 val;
286 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
287 if (ret == -EINVAL)
288 return 1; /* "not controllable" */
289 else if (ret)
290 return ret;
292 ret = max8997_read_reg(i2c, reg, &val);
293 if (ret)
294 return ret;
296 return (val & mask) == pattern;
299 static int max8997_reg_enable(struct regulator_dev *rdev)
301 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
302 struct i2c_client *i2c = max8997->iodev->i2c;
303 int ret, reg, mask, pattern;
305 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
306 if (ret)
307 return ret;
309 return max8997_update_reg(i2c, reg, pattern, mask);
312 static int max8997_reg_disable(struct regulator_dev *rdev)
314 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
315 struct i2c_client *i2c = max8997->iodev->i2c;
316 int ret, reg, mask, pattern;
318 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
319 if (ret)
320 return ret;
322 return max8997_update_reg(i2c, reg, ~pattern, mask);
325 static int max8997_get_voltage_register(struct regulator_dev *rdev,
326 int *_reg, int *_shift, int *_mask)
328 int rid = max8997_get_rid(rdev);
329 int reg, shift = 0, mask = 0x3f;
331 switch (rid) {
332 case MAX8997_LDO1 ... MAX8997_LDO21:
333 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
334 break;
335 case MAX8997_BUCK1:
336 reg = MAX8997_REG_BUCK1DVS1;
337 break;
338 case MAX8997_BUCK2:
339 reg = MAX8997_REG_BUCK2DVS1;
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;
349 break;
350 case MAX8997_BUCK7:
351 reg = MAX8997_REG_BUCK7DVS;
352 break;
353 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
354 reg = MAX8997_REG_SAFEOUTCTRL;
355 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
356 mask = 0x3;
357 break;
358 case MAX8997_CHARGER_CV:
359 reg = MAX8997_REG_MBCCTRL3;
360 shift = 0;
361 mask = 0xf;
362 break;
363 case MAX8997_CHARGER:
364 reg = MAX8997_REG_MBCCTRL4;
365 shift = 0;
366 mask = 0xf;
367 break;
368 case MAX8997_CHARGER_TOPOFF:
369 reg = MAX8997_REG_MBCCTRL5;
370 shift = 0;
371 mask = 0xf;
372 break;
373 default:
374 return -EINVAL;
377 *_reg = reg;
378 *_shift = shift;
379 *_mask = mask;
381 return 0;
384 static int max8997_get_voltage(struct regulator_dev *rdev)
386 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
387 struct i2c_client *i2c = max8997->iodev->i2c;
388 int reg, shift, mask, ret;
389 int rid = max8997_get_rid(rdev);
390 u8 val;
392 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
393 if (ret)
394 return ret;
396 if ((rid == MAX8997_BUCK1 && max8997->buck1_gpiodvs) ||
397 (rid == MAX8997_BUCK2 && max8997->buck2_gpiodvs) ||
398 (rid == MAX8997_BUCK5 && max8997->buck5_gpiodvs))
399 reg += max8997->buck125_gpioindex;
401 ret = max8997_read_reg(i2c, reg, &val);
402 if (ret)
403 return ret;
405 val >>= shift;
406 val &= mask;
408 if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
409 return rdev->desc->ops->list_voltage(rdev, val);
412 * max8997_list_voltage returns value for any rdev with voltage_map,
413 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
414 * list_voltage ops (they are current regulators).
416 return max8997_list_voltage(rdev, val);
419 static inline int max8997_get_voltage_proper_val(
420 const struct voltage_map_desc *desc,
421 int min_vol, int max_vol)
423 int i = 0;
425 if (desc == NULL)
426 return -EINVAL;
428 if (max_vol < desc->min || min_vol > desc->max)
429 return -EINVAL;
431 while (desc->min + desc->step * i < min_vol &&
432 desc->min + desc->step * i < desc->max)
433 i++;
435 if (desc->min + desc->step * i > max_vol)
436 return -EINVAL;
438 if (i >= (1 << desc->n_bits))
439 return -EINVAL;
441 return i;
444 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
445 int min_uV, int max_uV, unsigned *selector)
447 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
448 struct i2c_client *i2c = max8997->iodev->i2c;
449 int rid = max8997_get_rid(rdev);
450 int lb, ub;
451 int reg, shift = 0, mask, ret = 0;
452 u8 val = 0x0;
454 if (rid != MAX8997_CHARGER_CV)
455 return -EINVAL;
457 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
458 if (ret)
459 return ret;
461 if (max_uV < 4000000 || min_uV > 4350000)
462 return -EINVAL;
464 if (min_uV <= 4000000) {
465 if (max_uV >= 4000000)
466 return -EINVAL;
467 else
468 val = 0x1;
469 } else if (min_uV <= 4200000 && max_uV >= 4200000)
470 val = 0x0;
471 else {
472 lb = (min_uV - 4000001) / 20000 + 2;
473 ub = (max_uV - 4000000) / 20000 + 1;
475 if (lb > ub)
476 return -EINVAL;
478 if (lb < 0xf)
479 val = lb;
480 else {
481 if (ub >= 0xf)
482 val = 0xf;
483 else
484 return -EINVAL;
488 *selector = val;
490 ret = max8997_update_reg(i2c, reg, val << shift, mask);
492 return ret;
496 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
497 * BUCK1, 2, and 5 are available if they are not controlled by gpio
499 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
500 int min_uV, int max_uV, unsigned *selector)
502 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
503 struct i2c_client *i2c = max8997->iodev->i2c;
504 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
505 const struct voltage_map_desc *desc;
506 int rid = max8997_get_rid(rdev);
507 int reg, shift = 0, mask, ret;
508 int i;
509 u8 org;
511 switch (rid) {
512 case MAX8997_LDO1 ... MAX8997_LDO21:
513 break;
514 case MAX8997_BUCK1 ... MAX8997_BUCK5:
515 break;
516 case MAX8997_BUCK6:
517 return -EINVAL;
518 case MAX8997_BUCK7:
519 break;
520 case MAX8997_CHARGER:
521 break;
522 case MAX8997_CHARGER_TOPOFF:
523 break;
524 default:
525 return -EINVAL;
528 desc = reg_voltage_map[rid];
530 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
531 if (i < 0)
532 return i;
534 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
535 if (ret)
536 return ret;
538 max8997_read_reg(i2c, reg, &org);
539 org = (org & mask) >> shift;
541 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
542 *selector = i;
544 if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 ||
545 rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) {
546 /* If the voltage is increasing */
547 if (org < i)
548 udelay(DIV_ROUND_UP(desc->step * (i - org),
549 max8997->ramp_delay));
552 return ret;
556 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
558 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
559 * of one of the bucks may affect that of another buck, which is the side
560 * effect of the change (set_voltage). This function examines the GPIO-DVS
561 * configurations and checks whether such side-effect exists.
563 static int max8997_assess_side_effect(struct regulator_dev *rdev,
564 u8 new_val, int *best)
566 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
567 int rid = max8997_get_rid(rdev);
568 u8 *buckx_val[3];
569 bool buckx_gpiodvs[3];
570 int side_effect[8];
571 int min_side_effect = INT_MAX;
572 int i;
574 *best = -1;
576 switch (rid) {
577 case MAX8997_BUCK1:
578 rid = 0;
579 break;
580 case MAX8997_BUCK2:
581 rid = 1;
582 break;
583 case MAX8997_BUCK5:
584 rid = 2;
585 break;
586 default:
587 return -EINVAL;
590 buckx_val[0] = max8997->buck1_vol;
591 buckx_val[1] = max8997->buck2_vol;
592 buckx_val[2] = max8997->buck5_vol;
593 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
594 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
595 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
597 for (i = 0; i < 8; i++) {
598 int others;
600 if (new_val != (buckx_val[rid])[i]) {
601 side_effect[i] = -1;
602 continue;
605 side_effect[i] = 0;
606 for (others = 0; others < 3; others++) {
607 int diff;
609 if (others == rid)
610 continue;
611 if (buckx_gpiodvs[others] == false)
612 continue; /* Not affected */
613 diff = (buckx_val[others])[i] -
614 (buckx_val[others])[max8997->buck125_gpioindex];
615 if (diff > 0)
616 side_effect[i] += diff;
617 else if (diff < 0)
618 side_effect[i] -= diff;
620 if (side_effect[i] == 0) {
621 *best = i;
622 return 0; /* NO SIDE EFFECT! Use This! */
624 if (side_effect[i] < min_side_effect) {
625 min_side_effect = side_effect[i];
626 *best = i;
630 if (*best == -1)
631 return -EINVAL;
633 return side_effect[*best];
637 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
638 * max8997_set_voltage_ldobuck to do the job.
640 static int max8997_set_voltage_buck(struct regulator_dev *rdev,
641 int min_uV, int max_uV, unsigned *selector)
643 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
644 int rid = max8997_get_rid(rdev);
645 const struct voltage_map_desc *desc;
646 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
647 bool gpio_dvs_mode = false;
648 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
650 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
651 return -EINVAL;
653 switch (rid) {
654 case MAX8997_BUCK1:
655 if (max8997->buck1_gpiodvs)
656 gpio_dvs_mode = true;
657 break;
658 case MAX8997_BUCK2:
659 if (max8997->buck2_gpiodvs)
660 gpio_dvs_mode = true;
661 break;
662 case MAX8997_BUCK5:
663 if (max8997->buck5_gpiodvs)
664 gpio_dvs_mode = true;
665 break;
668 if (!gpio_dvs_mode)
669 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
670 selector);
672 desc = reg_voltage_map[rid];
673 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
674 if (new_val < 0)
675 return new_val;
677 tmp_dmg = INT_MAX;
678 tmp_idx = -1;
679 tmp_val = -1;
680 do {
681 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
682 if (damage == 0)
683 goto out;
685 if (tmp_dmg > damage) {
686 tmp_idx = new_idx;
687 tmp_val = new_val;
688 tmp_dmg = damage;
691 new_val++;
692 } while (desc->min + desc->step + new_val <= desc->max);
694 new_idx = tmp_idx;
695 new_val = tmp_val;
697 if (max8997->ignore_gpiodvs_side_effect == false)
698 return -EINVAL;
700 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
701 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
703 out:
704 if (new_idx < 0 || new_val < 0)
705 return -EINVAL;
707 max8997->buck125_gpioindex = new_idx;
708 max8997_set_gpio(max8997);
709 *selector = new_val;
711 return 0;
714 static const int safeoutvolt[] = {
715 3300000,
716 4850000,
717 4900000,
718 4950000,
721 /* For SAFEOUT1 and SAFEOUT2 */
722 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
723 int min_uV, int max_uV, unsigned *selector)
725 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
726 struct i2c_client *i2c = max8997->iodev->i2c;
727 int rid = max8997_get_rid(rdev);
728 int reg, shift = 0, mask, ret;
729 int i = 0;
730 u8 val;
732 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
733 return -EINVAL;
735 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
736 if (min_uV <= safeoutvolt[i] &&
737 max_uV >= safeoutvolt[i])
738 break;
741 if (i >= ARRAY_SIZE(safeoutvolt))
742 return -EINVAL;
744 if (i == 0)
745 val = 0x3;
746 else
747 val = i - 1;
749 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
750 if (ret)
751 return ret;
753 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
754 *selector = val;
756 return ret;
759 static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
761 return 0;
764 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
766 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
767 struct i2c_client *i2c = max8997->iodev->i2c;
768 int ret, reg, mask, pattern;
769 int rid = max8997_get_rid(rdev);
771 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
772 if (ret)
773 return ret;
775 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
777 if (rid == MAX8997_LDO1 ||
778 rid == MAX8997_LDO10 ||
779 rid == MAX8997_LDO21) {
780 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
781 rdev->desc->name);
782 return max8997_update_reg(i2c, reg, 0x40, mask);
785 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
786 rdev->desc->name, max8997->saved_states[rid] & mask,
787 (~pattern) & mask);
788 return max8997_update_reg(i2c, reg, ~pattern, mask);
791 static struct regulator_ops max8997_ldo_ops = {
792 .list_voltage = max8997_list_voltage,
793 .is_enabled = max8997_reg_is_enabled,
794 .enable = max8997_reg_enable,
795 .disable = max8997_reg_disable,
796 .get_voltage = max8997_get_voltage,
797 .set_voltage = max8997_set_voltage_ldobuck,
798 .set_suspend_enable = max8997_reg_enable_suspend,
799 .set_suspend_disable = max8997_reg_disable_suspend,
802 static struct regulator_ops max8997_buck_ops = {
803 .list_voltage = max8997_list_voltage,
804 .is_enabled = max8997_reg_is_enabled,
805 .enable = max8997_reg_enable,
806 .disable = max8997_reg_disable,
807 .get_voltage = max8997_get_voltage,
808 .set_voltage = max8997_set_voltage_buck,
809 .set_suspend_enable = max8997_reg_enable_suspend,
810 .set_suspend_disable = max8997_reg_disable_suspend,
813 static struct regulator_ops max8997_fixedvolt_ops = {
814 .list_voltage = max8997_list_voltage,
815 .is_enabled = max8997_reg_is_enabled,
816 .enable = max8997_reg_enable,
817 .disable = max8997_reg_disable,
818 .set_suspend_enable = max8997_reg_enable_suspend,
819 .set_suspend_disable = max8997_reg_disable_suspend,
822 static struct regulator_ops max8997_safeout_ops = {
823 .list_voltage = max8997_list_voltage_safeout,
824 .is_enabled = max8997_reg_is_enabled,
825 .enable = max8997_reg_enable,
826 .disable = max8997_reg_disable,
827 .get_voltage = max8997_get_voltage,
828 .set_voltage = max8997_set_voltage_safeout,
829 .set_suspend_enable = max8997_reg_enable_suspend,
830 .set_suspend_disable = max8997_reg_disable_suspend,
833 static struct regulator_ops max8997_fixedstate_ops = {
834 .list_voltage = max8997_list_voltage_charger_cv,
835 .get_voltage = max8997_get_voltage,
836 .set_voltage = max8997_set_voltage_charger_cv,
839 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev,
840 int min_uV, int max_uV)
842 unsigned dummy;
844 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy);
848 static struct regulator_ops max8997_charger_ops = {
849 .is_enabled = max8997_reg_is_enabled,
850 .enable = max8997_reg_enable,
851 .disable = max8997_reg_disable,
852 .get_current_limit = max8997_get_voltage,
853 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
856 static struct regulator_ops max8997_charger_fixedstate_ops = {
857 .is_enabled = max8997_reg_is_enabled,
858 .get_current_limit = max8997_get_voltage,
859 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
862 #define regulator_desc_ldo(num) { \
863 .name = "LDO"#num, \
864 .id = MAX8997_LDO##num, \
865 .ops = &max8997_ldo_ops, \
866 .type = REGULATOR_VOLTAGE, \
867 .owner = THIS_MODULE, \
869 #define regulator_desc_buck(num) { \
870 .name = "BUCK"#num, \
871 .id = MAX8997_BUCK##num, \
872 .ops = &max8997_buck_ops, \
873 .type = REGULATOR_VOLTAGE, \
874 .owner = THIS_MODULE, \
877 static struct regulator_desc regulators[] = {
878 regulator_desc_ldo(1),
879 regulator_desc_ldo(2),
880 regulator_desc_ldo(3),
881 regulator_desc_ldo(4),
882 regulator_desc_ldo(5),
883 regulator_desc_ldo(6),
884 regulator_desc_ldo(7),
885 regulator_desc_ldo(8),
886 regulator_desc_ldo(9),
887 regulator_desc_ldo(10),
888 regulator_desc_ldo(11),
889 regulator_desc_ldo(12),
890 regulator_desc_ldo(13),
891 regulator_desc_ldo(14),
892 regulator_desc_ldo(15),
893 regulator_desc_ldo(16),
894 regulator_desc_ldo(17),
895 regulator_desc_ldo(18),
896 regulator_desc_ldo(21),
897 regulator_desc_buck(1),
898 regulator_desc_buck(2),
899 regulator_desc_buck(3),
900 regulator_desc_buck(4),
901 regulator_desc_buck(5),
903 .name = "BUCK6",
904 .id = MAX8997_BUCK6,
905 .ops = &max8997_fixedvolt_ops,
906 .type = REGULATOR_VOLTAGE,
907 .owner = THIS_MODULE,
909 regulator_desc_buck(7),
911 .name = "EN32KHz AP",
912 .id = MAX8997_EN32KHZ_AP,
913 .ops = &max8997_fixedvolt_ops,
914 .type = REGULATOR_VOLTAGE,
915 .owner = THIS_MODULE,
916 }, {
917 .name = "EN32KHz CP",
918 .id = MAX8997_EN32KHZ_CP,
919 .ops = &max8997_fixedvolt_ops,
920 .type = REGULATOR_VOLTAGE,
921 .owner = THIS_MODULE,
922 }, {
923 .name = "ENVICHG",
924 .id = MAX8997_ENVICHG,
925 .ops = &max8997_fixedvolt_ops,
926 .type = REGULATOR_VOLTAGE,
927 .owner = THIS_MODULE,
928 }, {
929 .name = "ESAFEOUT1",
930 .id = MAX8997_ESAFEOUT1,
931 .ops = &max8997_safeout_ops,
932 .type = REGULATOR_VOLTAGE,
933 .owner = THIS_MODULE,
934 }, {
935 .name = "ESAFEOUT2",
936 .id = MAX8997_ESAFEOUT2,
937 .ops = &max8997_safeout_ops,
938 .type = REGULATOR_VOLTAGE,
939 .owner = THIS_MODULE,
940 }, {
941 .name = "CHARGER CV",
942 .id = MAX8997_CHARGER_CV,
943 .ops = &max8997_fixedstate_ops,
944 .type = REGULATOR_VOLTAGE,
945 .owner = THIS_MODULE,
946 }, {
947 .name = "CHARGER",
948 .id = MAX8997_CHARGER,
949 .ops = &max8997_charger_ops,
950 .type = REGULATOR_CURRENT,
951 .owner = THIS_MODULE,
952 }, {
953 .name = "CHARGER TOPOFF",
954 .id = MAX8997_CHARGER_TOPOFF,
955 .ops = &max8997_charger_fixedstate_ops,
956 .type = REGULATOR_CURRENT,
957 .owner = THIS_MODULE,
961 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
963 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
964 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
965 struct regulator_dev **rdev;
966 struct max8997_data *max8997;
967 struct i2c_client *i2c;
968 int i, ret, size;
969 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
971 if (!pdata) {
972 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
973 return -ENODEV;
976 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
977 if (!max8997)
978 return -ENOMEM;
980 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
981 max8997->rdev = kzalloc(size, GFP_KERNEL);
982 if (!max8997->rdev) {
983 kfree(max8997);
984 return -ENOMEM;
987 rdev = max8997->rdev;
988 max8997->dev = &pdev->dev;
989 max8997->iodev = iodev;
990 max8997->num_regulators = pdata->num_regulators;
991 platform_set_drvdata(pdev, max8997);
992 i2c = max8997->iodev->i2c;
994 max8997->buck125_gpioindex = pdata->buck125_default_idx;
995 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
996 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
997 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
998 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
999 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
1001 for (i = 0; i < 8; i++) {
1002 max8997->buck1_vol[i] = ret =
1003 max8997_get_voltage_proper_val(
1004 &buck1245_voltage_map_desc,
1005 pdata->buck1_voltage[i] / 1000,
1006 pdata->buck1_voltage[i] / 1000 +
1007 buck1245_voltage_map_desc.step);
1008 if (ret < 0)
1009 goto err_alloc;
1011 max8997->buck2_vol[i] = ret =
1012 max8997_get_voltage_proper_val(
1013 &buck1245_voltage_map_desc,
1014 pdata->buck2_voltage[i] / 1000,
1015 pdata->buck2_voltage[i] / 1000 +
1016 buck1245_voltage_map_desc.step);
1017 if (ret < 0)
1018 goto err_alloc;
1020 max8997->buck5_vol[i] = ret =
1021 max8997_get_voltage_proper_val(
1022 &buck1245_voltage_map_desc,
1023 pdata->buck5_voltage[i] / 1000,
1024 pdata->buck5_voltage[i] / 1000 +
1025 buck1245_voltage_map_desc.step);
1026 if (ret < 0)
1027 goto err_alloc;
1029 if (max_buck1 < max8997->buck1_vol[i])
1030 max_buck1 = max8997->buck1_vol[i];
1031 if (max_buck2 < max8997->buck2_vol[i])
1032 max_buck2 = max8997->buck2_vol[i];
1033 if (max_buck5 < max8997->buck5_vol[i])
1034 max_buck5 = max8997->buck5_vol[i];
1037 /* For the safety, set max voltage before setting up */
1038 for (i = 0; i < 8; i++) {
1039 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1040 max_buck1, 0x3f);
1041 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1042 max_buck2, 0x3f);
1043 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1044 max_buck5, 0x3f);
1048 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1049 * If at least one of them cares, set gpios.
1051 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1052 pdata->buck5_gpiodvs) {
1053 bool gpio1set = false, gpio2set = false;
1055 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1056 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1057 !gpio_is_valid(pdata->buck125_gpios[2])) {
1058 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1059 ret = -EINVAL;
1060 goto err_alloc;
1063 ret = gpio_request(pdata->buck125_gpios[0],
1064 "MAX8997 SET1");
1065 if (ret == -EBUSY)
1066 dev_warn(&pdev->dev, "Duplicated gpio request"
1067 " on SET1\n");
1068 else if (ret)
1069 goto err_alloc;
1070 else
1071 gpio1set = true;
1073 ret = gpio_request(pdata->buck125_gpios[1],
1074 "MAX8997 SET2");
1075 if (ret == -EBUSY)
1076 dev_warn(&pdev->dev, "Duplicated gpio request"
1077 " on SET2\n");
1078 else if (ret) {
1079 if (gpio1set)
1080 gpio_free(pdata->buck125_gpios[0]);
1081 goto err_alloc;
1082 } else
1083 gpio2set = true;
1085 ret = gpio_request(pdata->buck125_gpios[2],
1086 "MAX8997 SET3");
1087 if (ret == -EBUSY)
1088 dev_warn(&pdev->dev, "Duplicated gpio request"
1089 " on SET3\n");
1090 else if (ret) {
1091 if (gpio1set)
1092 gpio_free(pdata->buck125_gpios[0]);
1093 if (gpio2set)
1094 gpio_free(pdata->buck125_gpios[1]);
1095 goto err_alloc;
1098 gpio_direction_output(pdata->buck125_gpios[0],
1099 (max8997->buck125_gpioindex >> 2)
1100 & 0x1); /* SET1 */
1101 gpio_direction_output(pdata->buck125_gpios[1],
1102 (max8997->buck125_gpioindex >> 1)
1103 & 0x1); /* SET2 */
1104 gpio_direction_output(pdata->buck125_gpios[2],
1105 (max8997->buck125_gpioindex >> 0)
1106 & 0x1); /* SET3 */
1107 ret = 0;
1110 /* DVS-GPIO disabled */
1111 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1112 (1 << 1) : (0 << 1), 1 << 1);
1113 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1114 (1 << 1) : (0 << 1), 1 << 1);
1115 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1116 (1 << 1) : (0 << 1), 1 << 1);
1118 /* Initialize all the DVS related BUCK registers */
1119 for (i = 0; i < 8; i++) {
1120 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1121 max8997->buck1_vol[i],
1122 0x3f);
1123 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1124 max8997->buck2_vol[i],
1125 0x3f);
1126 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1127 max8997->buck5_vol[i],
1128 0x3f);
1131 /* Misc Settings */
1132 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1133 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1135 for (i = 0; i < pdata->num_regulators; i++) {
1136 const struct voltage_map_desc *desc;
1137 int id = pdata->regulators[i].id;
1139 desc = reg_voltage_map[id];
1140 if (desc)
1141 regulators[id].n_voltages =
1142 (desc->max - desc->min) / desc->step + 1;
1143 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1144 regulators[id].n_voltages = 4;
1145 else if (id == MAX8997_CHARGER_CV)
1146 regulators[id].n_voltages = 16;
1148 rdev[i] = regulator_register(&regulators[id], max8997->dev,
1149 pdata->regulators[i].initdata, max8997);
1150 if (IS_ERR(rdev[i])) {
1151 ret = PTR_ERR(rdev[i]);
1152 dev_err(max8997->dev, "regulator init failed for %d\n",
1153 id);
1154 rdev[i] = NULL;
1155 goto err;
1159 return 0;
1160 err:
1161 for (i = 0; i < max8997->num_regulators; i++)
1162 if (rdev[i])
1163 regulator_unregister(rdev[i]);
1164 err_alloc:
1165 kfree(max8997->rdev);
1166 kfree(max8997);
1168 return ret;
1171 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1173 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1174 struct regulator_dev **rdev = max8997->rdev;
1175 int i;
1177 for (i = 0; i < max8997->num_regulators; i++)
1178 if (rdev[i])
1179 regulator_unregister(rdev[i]);
1181 kfree(max8997->rdev);
1182 kfree(max8997);
1184 return 0;
1187 static const struct platform_device_id max8997_pmic_id[] = {
1188 { "max8997-pmic", 0},
1189 { },
1191 MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1193 static struct platform_driver max8997_pmic_driver = {
1194 .driver = {
1195 .name = "max8997-pmic",
1196 .owner = THIS_MODULE,
1198 .probe = max8997_pmic_probe,
1199 .remove = __devexit_p(max8997_pmic_remove),
1200 .id_table = max8997_pmic_id,
1203 static int __init max8997_pmic_init(void)
1205 return platform_driver_register(&max8997_pmic_driver);
1207 subsys_initcall(max8997_pmic_init);
1209 static void __exit max8997_pmic_cleanup(void)
1211 platform_driver_unregister(&max8997_pmic_driver);
1213 module_exit(max8997_pmic_cleanup);
1215 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1216 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1217 MODULE_LICENSE("GPL");