ixgbevf: Use igb style interrupt masks instead of ixgbe style
[linux-2.6/cjktty.git] / drivers / regulator / max8997.c
blob704cd49ef3758c9ac00034238838af634e0c27f1
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/err.h>
26 #include <linux/gpio.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/mfd/max8997.h>
33 #include <linux/mfd/max8997-private.h>
35 struct max8997_data {
36 struct device *dev;
37 struct max8997_dev *iodev;
38 int num_regulators;
39 struct regulator_dev **rdev;
40 int ramp_delay; /* in mV/us */
42 bool buck1_gpiodvs;
43 bool buck2_gpiodvs;
44 bool buck5_gpiodvs;
45 u8 buck1_vol[8];
46 u8 buck2_vol[8];
47 u8 buck5_vol[8];
48 int buck125_gpios[3];
49 int buck125_gpioindex;
50 bool ignore_gpiodvs_side_effect;
52 u8 saved_states[MAX8997_REG_MAX];
55 static inline void max8997_set_gpio(struct max8997_data *max8997)
57 int set3 = (max8997->buck125_gpioindex) & 0x1;
58 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
59 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
61 gpio_set_value(max8997->buck125_gpios[0], set1);
62 gpio_set_value(max8997->buck125_gpios[1], set2);
63 gpio_set_value(max8997->buck125_gpios[2], set3);
66 struct voltage_map_desc {
67 int min;
68 int max;
69 int step;
72 /* Voltage maps in mV */
73 static const struct voltage_map_desc ldo_voltage_map_desc = {
74 .min = 800, .max = 3950, .step = 50,
75 }; /* LDO1 ~ 18, 21 all */
77 static const struct voltage_map_desc buck1245_voltage_map_desc = {
78 .min = 650, .max = 2225, .step = 25,
79 }; /* Buck1, 2, 4, 5 */
81 static const struct voltage_map_desc buck37_voltage_map_desc = {
82 .min = 750, .max = 3900, .step = 50,
83 }; /* Buck3, 7 */
85 /* current map in mA */
86 static const struct voltage_map_desc charger_current_map_desc = {
87 .min = 200, .max = 950, .step = 50,
90 static const struct voltage_map_desc topoff_current_map_desc = {
91 .min = 50, .max = 200, .step = 10,
94 static const struct voltage_map_desc *reg_voltage_map[] = {
95 [MAX8997_LDO1] = &ldo_voltage_map_desc,
96 [MAX8997_LDO2] = &ldo_voltage_map_desc,
97 [MAX8997_LDO3] = &ldo_voltage_map_desc,
98 [MAX8997_LDO4] = &ldo_voltage_map_desc,
99 [MAX8997_LDO5] = &ldo_voltage_map_desc,
100 [MAX8997_LDO6] = &ldo_voltage_map_desc,
101 [MAX8997_LDO7] = &ldo_voltage_map_desc,
102 [MAX8997_LDO8] = &ldo_voltage_map_desc,
103 [MAX8997_LDO9] = &ldo_voltage_map_desc,
104 [MAX8997_LDO10] = &ldo_voltage_map_desc,
105 [MAX8997_LDO11] = &ldo_voltage_map_desc,
106 [MAX8997_LDO12] = &ldo_voltage_map_desc,
107 [MAX8997_LDO13] = &ldo_voltage_map_desc,
108 [MAX8997_LDO14] = &ldo_voltage_map_desc,
109 [MAX8997_LDO15] = &ldo_voltage_map_desc,
110 [MAX8997_LDO16] = &ldo_voltage_map_desc,
111 [MAX8997_LDO17] = &ldo_voltage_map_desc,
112 [MAX8997_LDO18] = &ldo_voltage_map_desc,
113 [MAX8997_LDO21] = &ldo_voltage_map_desc,
114 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
115 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
116 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
117 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
118 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
119 [MAX8997_BUCK6] = NULL,
120 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
121 [MAX8997_EN32KHZ_AP] = NULL,
122 [MAX8997_EN32KHZ_CP] = NULL,
123 [MAX8997_ENVICHG] = NULL,
124 [MAX8997_ESAFEOUT1] = NULL,
125 [MAX8997_ESAFEOUT2] = NULL,
126 [MAX8997_CHARGER_CV] = NULL,
127 [MAX8997_CHARGER] = &charger_current_map_desc,
128 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
131 static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
132 unsigned int selector)
134 int rid = rdev_get_id(rdev);
136 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
137 switch (selector) {
138 case 0:
139 return 4850000;
140 case 1:
141 return 4900000;
142 case 2:
143 return 4950000;
144 case 3:
145 return 3300000;
146 default:
147 return -EINVAL;
151 return -EINVAL;
154 static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
155 unsigned int selector)
157 int rid = rdev_get_id(rdev);
159 if (rid != MAX8997_CHARGER_CV)
160 goto err;
162 switch (selector) {
163 case 0x00:
164 return 4200000;
165 case 0x01 ... 0x0E:
166 return 4000000 + 20000 * (selector - 0x01);
167 case 0x0F:
168 return 4350000;
169 default:
170 return -EINVAL;
172 err:
173 return -EINVAL;
176 static int max8997_list_voltage(struct regulator_dev *rdev,
177 unsigned int selector)
179 const struct voltage_map_desc *desc;
180 int rid = rdev_get_id(rdev);
181 int val;
183 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
184 rid < 0)
185 return -EINVAL;
187 desc = reg_voltage_map[rid];
188 if (desc == NULL)
189 return -EINVAL;
191 val = desc->min + desc->step * selector;
192 if (val > desc->max)
193 return -EINVAL;
195 return val * 1000;
198 static int max8997_get_enable_register(struct regulator_dev *rdev,
199 int *reg, int *mask, int *pattern)
201 int rid = rdev_get_id(rdev);
203 switch (rid) {
204 case MAX8997_LDO1 ... MAX8997_LDO21:
205 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
206 *mask = 0xC0;
207 *pattern = 0xC0;
208 break;
209 case MAX8997_BUCK1:
210 *reg = MAX8997_REG_BUCK1CTRL;
211 *mask = 0x01;
212 *pattern = 0x01;
213 break;
214 case MAX8997_BUCK2:
215 *reg = MAX8997_REG_BUCK2CTRL;
216 *mask = 0x01;
217 *pattern = 0x01;
218 break;
219 case MAX8997_BUCK3:
220 *reg = MAX8997_REG_BUCK3CTRL;
221 *mask = 0x01;
222 *pattern = 0x01;
223 break;
224 case MAX8997_BUCK4:
225 *reg = MAX8997_REG_BUCK4CTRL;
226 *mask = 0x01;
227 *pattern = 0x01;
228 break;
229 case MAX8997_BUCK5:
230 *reg = MAX8997_REG_BUCK5CTRL;
231 *mask = 0x01;
232 *pattern = 0x01;
233 break;
234 case MAX8997_BUCK6:
235 *reg = MAX8997_REG_BUCK6CTRL;
236 *mask = 0x01;
237 *pattern = 0x01;
238 break;
239 case MAX8997_BUCK7:
240 *reg = MAX8997_REG_BUCK7CTRL;
241 *mask = 0x01;
242 *pattern = 0x01;
243 break;
244 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
245 *reg = MAX8997_REG_MAINCON1;
246 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
247 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
248 break;
249 case MAX8997_ENVICHG:
250 *reg = MAX8997_REG_MBCCTRL1;
251 *mask = 0x80;
252 *pattern = 0x80;
253 break;
254 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
255 *reg = MAX8997_REG_SAFEOUTCTRL;
256 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
257 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
258 break;
259 case MAX8997_CHARGER:
260 *reg = MAX8997_REG_MBCCTRL2;
261 *mask = 0x40;
262 *pattern = 0x40;
263 break;
264 default:
265 /* Not controllable or not exists */
266 return -EINVAL;
269 return 0;
272 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
274 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
275 struct i2c_client *i2c = max8997->iodev->i2c;
276 int ret, reg, mask, pattern;
277 u8 val;
279 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
280 if (ret)
281 return ret;
283 ret = max8997_read_reg(i2c, reg, &val);
284 if (ret)
285 return ret;
287 return (val & mask) == pattern;
290 static int max8997_reg_enable(struct regulator_dev *rdev)
292 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
293 struct i2c_client *i2c = max8997->iodev->i2c;
294 int ret, reg, mask, pattern;
296 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
297 if (ret)
298 return ret;
300 return max8997_update_reg(i2c, reg, pattern, mask);
303 static int max8997_reg_disable(struct regulator_dev *rdev)
305 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
306 struct i2c_client *i2c = max8997->iodev->i2c;
307 int ret, reg, mask, pattern;
309 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
310 if (ret)
311 return ret;
313 return max8997_update_reg(i2c, reg, ~pattern, mask);
316 static int max8997_get_voltage_register(struct regulator_dev *rdev,
317 int *_reg, int *_shift, int *_mask)
319 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
320 int rid = rdev_get_id(rdev);
321 int reg, shift = 0, mask = 0x3f;
323 switch (rid) {
324 case MAX8997_LDO1 ... MAX8997_LDO21:
325 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
326 break;
327 case MAX8997_BUCK1:
328 reg = MAX8997_REG_BUCK1DVS1;
329 if (max8997->buck1_gpiodvs)
330 reg += max8997->buck125_gpioindex;
331 break;
332 case MAX8997_BUCK2:
333 reg = MAX8997_REG_BUCK2DVS1;
334 if (max8997->buck2_gpiodvs)
335 reg += max8997->buck125_gpioindex;
336 break;
337 case MAX8997_BUCK3:
338 reg = MAX8997_REG_BUCK3DVS;
339 break;
340 case MAX8997_BUCK4:
341 reg = MAX8997_REG_BUCK4DVS;
342 break;
343 case MAX8997_BUCK5:
344 reg = MAX8997_REG_BUCK5DVS1;
345 if (max8997->buck5_gpiodvs)
346 reg += max8997->buck125_gpioindex;
347 break;
348 case MAX8997_BUCK7:
349 reg = MAX8997_REG_BUCK7DVS;
350 break;
351 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
352 reg = MAX8997_REG_SAFEOUTCTRL;
353 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
354 mask = 0x3;
355 break;
356 case MAX8997_CHARGER_CV:
357 reg = MAX8997_REG_MBCCTRL3;
358 shift = 0;
359 mask = 0xf;
360 break;
361 case MAX8997_CHARGER:
362 reg = MAX8997_REG_MBCCTRL4;
363 shift = 0;
364 mask = 0xf;
365 break;
366 case MAX8997_CHARGER_TOPOFF:
367 reg = MAX8997_REG_MBCCTRL5;
368 shift = 0;
369 mask = 0xf;
370 break;
371 default:
372 return -EINVAL;
375 *_reg = reg;
376 *_shift = shift;
377 *_mask = mask;
379 return 0;
382 static int max8997_get_voltage_sel(struct regulator_dev *rdev)
384 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
385 struct i2c_client *i2c = max8997->iodev->i2c;
386 int reg, shift, mask, ret;
387 u8 val;
389 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
390 if (ret)
391 return ret;
393 ret = max8997_read_reg(i2c, reg, &val);
394 if (ret)
395 return ret;
397 val >>= shift;
398 val &= mask;
400 return val;
403 static inline int max8997_get_voltage_proper_val(
404 const struct voltage_map_desc *desc,
405 int min_vol, int max_vol)
407 int i;
409 if (desc == NULL)
410 return -EINVAL;
412 if (max_vol < desc->min || min_vol > desc->max)
413 return -EINVAL;
415 if (min_vol < desc->min)
416 min_vol = desc->min;
418 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
420 if (desc->min + desc->step * i > max_vol)
421 return -EINVAL;
423 return i;
426 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
427 int min_uV, int max_uV, unsigned *selector)
429 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
430 struct i2c_client *i2c = max8997->iodev->i2c;
431 int rid = rdev_get_id(rdev);
432 int lb, ub;
433 int reg, shift = 0, mask, ret = 0;
434 u8 val = 0x0;
436 if (rid != MAX8997_CHARGER_CV)
437 return -EINVAL;
439 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
440 if (ret)
441 return ret;
443 if (max_uV < 4000000 || min_uV > 4350000)
444 return -EINVAL;
446 if (min_uV <= 4000000) {
447 if (max_uV >= 4000000)
448 return -EINVAL;
449 else
450 val = 0x1;
451 } else if (min_uV <= 4200000 && max_uV >= 4200000)
452 val = 0x0;
453 else {
454 lb = (min_uV - 4000001) / 20000 + 2;
455 ub = (max_uV - 4000000) / 20000 + 1;
457 if (lb > ub)
458 return -EINVAL;
460 if (lb < 0xf)
461 val = lb;
462 else {
463 if (ub >= 0xf)
464 val = 0xf;
465 else
466 return -EINVAL;
470 *selector = val;
472 ret = max8997_update_reg(i2c, reg, val << shift, mask);
474 return ret;
478 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
479 * BUCK1, 2, and 5 are available if they are not controlled by gpio
481 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
482 int min_uV, int max_uV, unsigned *selector)
484 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
485 struct i2c_client *i2c = max8997->iodev->i2c;
486 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
487 const struct voltage_map_desc *desc;
488 int rid = rdev_get_id(rdev);
489 int i, reg, shift, mask, ret;
491 switch (rid) {
492 case MAX8997_LDO1 ... MAX8997_LDO21:
493 break;
494 case MAX8997_BUCK1 ... MAX8997_BUCK5:
495 break;
496 case MAX8997_BUCK6:
497 return -EINVAL;
498 case MAX8997_BUCK7:
499 break;
500 case MAX8997_CHARGER:
501 break;
502 case MAX8997_CHARGER_TOPOFF:
503 break;
504 default:
505 return -EINVAL;
508 desc = reg_voltage_map[rid];
510 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
511 if (i < 0)
512 return i;
514 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
515 if (ret)
516 return ret;
518 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
519 *selector = i;
521 return ret;
524 static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
525 unsigned int old_selector,
526 unsigned int new_selector)
528 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
529 int rid = rdev_get_id(rdev);
530 const struct voltage_map_desc *desc = reg_voltage_map[rid];
532 /* Delay is required only if the voltage is increasing */
533 if (old_selector >= new_selector)
534 return 0;
536 /* No need to delay if gpio_dvs_mode */
537 switch (rid) {
538 case MAX8997_BUCK1:
539 if (max8997->buck1_gpiodvs)
540 return 0;
541 break;
542 case MAX8997_BUCK2:
543 if (max8997->buck2_gpiodvs)
544 return 0;
545 break;
546 case MAX8997_BUCK5:
547 if (max8997->buck5_gpiodvs)
548 return 0;
549 break;
552 switch (rid) {
553 case MAX8997_BUCK1:
554 case MAX8997_BUCK2:
555 case MAX8997_BUCK4:
556 case MAX8997_BUCK5:
557 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
558 max8997->ramp_delay);
561 return 0;
565 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
567 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
568 * of one of the bucks may affect that of another buck, which is the side
569 * effect of the change (set_voltage). This function examines the GPIO-DVS
570 * configurations and checks whether such side-effect exists.
572 static int max8997_assess_side_effect(struct regulator_dev *rdev,
573 u8 new_val, int *best)
575 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
576 int rid = rdev_get_id(rdev);
577 u8 *buckx_val[3];
578 bool buckx_gpiodvs[3];
579 int side_effect[8];
580 int min_side_effect = INT_MAX;
581 int i;
583 *best = -1;
585 switch (rid) {
586 case MAX8997_BUCK1:
587 rid = 0;
588 break;
589 case MAX8997_BUCK2:
590 rid = 1;
591 break;
592 case MAX8997_BUCK5:
593 rid = 2;
594 break;
595 default:
596 return -EINVAL;
599 buckx_val[0] = max8997->buck1_vol;
600 buckx_val[1] = max8997->buck2_vol;
601 buckx_val[2] = max8997->buck5_vol;
602 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
603 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
604 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
606 for (i = 0; i < 8; i++) {
607 int others;
609 if (new_val != (buckx_val[rid])[i]) {
610 side_effect[i] = -1;
611 continue;
614 side_effect[i] = 0;
615 for (others = 0; others < 3; others++) {
616 int diff;
618 if (others == rid)
619 continue;
620 if (buckx_gpiodvs[others] == false)
621 continue; /* Not affected */
622 diff = (buckx_val[others])[i] -
623 (buckx_val[others])[max8997->buck125_gpioindex];
624 if (diff > 0)
625 side_effect[i] += diff;
626 else if (diff < 0)
627 side_effect[i] -= diff;
629 if (side_effect[i] == 0) {
630 *best = i;
631 return 0; /* NO SIDE EFFECT! Use This! */
633 if (side_effect[i] < min_side_effect) {
634 min_side_effect = side_effect[i];
635 *best = i;
639 if (*best == -1)
640 return -EINVAL;
642 return side_effect[*best];
646 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
647 * max8997_set_voltage_ldobuck to do the job.
649 static int max8997_set_voltage_buck(struct regulator_dev *rdev,
650 int min_uV, int max_uV, unsigned *selector)
652 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
653 int rid = rdev_get_id(rdev);
654 const struct voltage_map_desc *desc;
655 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
656 bool gpio_dvs_mode = false;
657 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
659 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
660 return -EINVAL;
662 switch (rid) {
663 case MAX8997_BUCK1:
664 if (max8997->buck1_gpiodvs)
665 gpio_dvs_mode = true;
666 break;
667 case MAX8997_BUCK2:
668 if (max8997->buck2_gpiodvs)
669 gpio_dvs_mode = true;
670 break;
671 case MAX8997_BUCK5:
672 if (max8997->buck5_gpiodvs)
673 gpio_dvs_mode = true;
674 break;
677 if (!gpio_dvs_mode)
678 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
679 selector);
681 desc = reg_voltage_map[rid];
682 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
683 if (new_val < 0)
684 return new_val;
686 tmp_dmg = INT_MAX;
687 tmp_idx = -1;
688 tmp_val = -1;
689 do {
690 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
691 if (damage == 0)
692 goto out;
694 if (tmp_dmg > damage) {
695 tmp_idx = new_idx;
696 tmp_val = new_val;
697 tmp_dmg = damage;
700 new_val++;
701 } while (desc->min + desc->step * new_val <= desc->max);
703 new_idx = tmp_idx;
704 new_val = tmp_val;
706 if (max8997->ignore_gpiodvs_side_effect == false)
707 return -EINVAL;
709 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
710 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
712 out:
713 if (new_idx < 0 || new_val < 0)
714 return -EINVAL;
716 max8997->buck125_gpioindex = new_idx;
717 max8997_set_gpio(max8997);
718 *selector = new_val;
720 return 0;
723 static const int safeoutvolt[] = {
724 3300000,
725 4850000,
726 4900000,
727 4950000,
730 /* For SAFEOUT1 and SAFEOUT2 */
731 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
732 int min_uV, int max_uV, unsigned *selector)
734 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
735 struct i2c_client *i2c = max8997->iodev->i2c;
736 int rid = rdev_get_id(rdev);
737 int reg, shift = 0, mask, ret;
738 int i = 0;
739 u8 val;
741 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
742 return -EINVAL;
744 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
745 if (min_uV <= safeoutvolt[i] &&
746 max_uV >= safeoutvolt[i])
747 break;
750 if (i >= ARRAY_SIZE(safeoutvolt))
751 return -EINVAL;
753 if (i == 0)
754 val = 0x3;
755 else
756 val = i - 1;
758 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
759 if (ret)
760 return ret;
762 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
763 *selector = val;
765 return ret;
768 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
770 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
771 struct i2c_client *i2c = max8997->iodev->i2c;
772 int ret, reg, mask, pattern;
773 int rid = rdev_get_id(rdev);
775 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
776 if (ret)
777 return ret;
779 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
781 if (rid == MAX8997_LDO1 ||
782 rid == MAX8997_LDO10 ||
783 rid == MAX8997_LDO21) {
784 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
785 rdev->desc->name);
786 return max8997_update_reg(i2c, reg, 0x40, mask);
789 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
790 rdev->desc->name, max8997->saved_states[rid] & mask,
791 (~pattern) & mask);
792 return max8997_update_reg(i2c, reg, ~pattern, mask);
795 static struct regulator_ops max8997_ldo_ops = {
796 .list_voltage = max8997_list_voltage,
797 .is_enabled = max8997_reg_is_enabled,
798 .enable = max8997_reg_enable,
799 .disable = max8997_reg_disable,
800 .get_voltage_sel = max8997_get_voltage_sel,
801 .set_voltage = max8997_set_voltage_ldobuck,
802 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
803 .set_suspend_disable = max8997_reg_disable_suspend,
806 static struct regulator_ops max8997_buck_ops = {
807 .list_voltage = max8997_list_voltage,
808 .is_enabled = max8997_reg_is_enabled,
809 .enable = max8997_reg_enable,
810 .disable = max8997_reg_disable,
811 .get_voltage_sel = max8997_get_voltage_sel,
812 .set_voltage = max8997_set_voltage_buck,
813 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
814 .set_suspend_disable = max8997_reg_disable_suspend,
817 static struct regulator_ops max8997_fixedvolt_ops = {
818 .list_voltage = max8997_list_voltage,
819 .is_enabled = max8997_reg_is_enabled,
820 .enable = max8997_reg_enable,
821 .disable = max8997_reg_disable,
822 .set_suspend_disable = max8997_reg_disable_suspend,
825 static struct regulator_ops max8997_safeout_ops = {
826 .list_voltage = max8997_list_voltage_safeout,
827 .is_enabled = max8997_reg_is_enabled,
828 .enable = max8997_reg_enable,
829 .disable = max8997_reg_disable,
830 .get_voltage_sel = max8997_get_voltage_sel,
831 .set_voltage = max8997_set_voltage_safeout,
832 .set_suspend_disable = max8997_reg_disable_suspend,
835 static struct regulator_ops max8997_fixedstate_ops = {
836 .list_voltage = max8997_list_voltage_charger_cv,
837 .get_voltage_sel = max8997_get_voltage_sel,
838 .set_voltage = max8997_set_voltage_charger_cv,
841 static int max8997_set_current_limit(struct regulator_dev *rdev,
842 int min_uA, int max_uA)
844 unsigned dummy;
845 int rid = rdev_get_id(rdev);
847 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
848 return -EINVAL;
850 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
851 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
854 static int max8997_get_current_limit(struct regulator_dev *rdev)
856 int sel, rid = rdev_get_id(rdev);
858 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
859 return -EINVAL;
861 sel = max8997_get_voltage_sel(rdev);
862 if (sel < 0)
863 return sel;
865 /* Reuse max8997_list_voltage to get current_limit. */
866 return max8997_list_voltage(rdev, sel);
869 static struct regulator_ops max8997_charger_ops = {
870 .is_enabled = max8997_reg_is_enabled,
871 .enable = max8997_reg_enable,
872 .disable = max8997_reg_disable,
873 .get_current_limit = max8997_get_current_limit,
874 .set_current_limit = max8997_set_current_limit,
877 static struct regulator_ops max8997_charger_fixedstate_ops = {
878 .get_current_limit = max8997_get_current_limit,
879 .set_current_limit = max8997_set_current_limit,
882 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
883 .name = #_name, \
884 .id = MAX8997_##_name, \
885 .ops = &_ops, \
886 .type = REGULATOR_VOLTAGE, \
887 .owner = THIS_MODULE, \
890 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
891 .name = #_name, \
892 .id = MAX8997_##_name, \
893 .ops = &_ops, \
894 .type = REGULATOR_CURRENT, \
895 .owner = THIS_MODULE, \
898 static struct regulator_desc regulators[] = {
899 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
900 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
901 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
902 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
903 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
904 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
905 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
906 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
907 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
908 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
909 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
910 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
911 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
912 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
913 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
914 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
915 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
916 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
917 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
918 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
919 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
920 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
921 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
922 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
923 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
924 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
925 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
926 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
927 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
928 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
929 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
930 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
931 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
932 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
933 max8997_charger_fixedstate_ops),
936 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
938 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
939 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
940 struct regulator_config config = { };
941 struct regulator_dev **rdev;
942 struct max8997_data *max8997;
943 struct i2c_client *i2c;
944 int i, ret, size;
945 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
947 if (!pdata) {
948 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
949 return -ENODEV;
952 max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
953 GFP_KERNEL);
954 if (!max8997)
955 return -ENOMEM;
957 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
958 max8997->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
959 if (!max8997->rdev)
960 return -ENOMEM;
962 rdev = max8997->rdev;
963 max8997->dev = &pdev->dev;
964 max8997->iodev = iodev;
965 max8997->num_regulators = pdata->num_regulators;
966 platform_set_drvdata(pdev, max8997);
967 i2c = max8997->iodev->i2c;
969 max8997->buck125_gpioindex = pdata->buck125_default_idx;
970 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
971 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
972 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
973 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
974 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
976 for (i = 0; i < 8; i++) {
977 max8997->buck1_vol[i] = ret =
978 max8997_get_voltage_proper_val(
979 &buck1245_voltage_map_desc,
980 pdata->buck1_voltage[i] / 1000,
981 pdata->buck1_voltage[i] / 1000 +
982 buck1245_voltage_map_desc.step);
983 if (ret < 0)
984 goto err_out;
986 max8997->buck2_vol[i] = ret =
987 max8997_get_voltage_proper_val(
988 &buck1245_voltage_map_desc,
989 pdata->buck2_voltage[i] / 1000,
990 pdata->buck2_voltage[i] / 1000 +
991 buck1245_voltage_map_desc.step);
992 if (ret < 0)
993 goto err_out;
995 max8997->buck5_vol[i] = ret =
996 max8997_get_voltage_proper_val(
997 &buck1245_voltage_map_desc,
998 pdata->buck5_voltage[i] / 1000,
999 pdata->buck5_voltage[i] / 1000 +
1000 buck1245_voltage_map_desc.step);
1001 if (ret < 0)
1002 goto err_out;
1004 if (max_buck1 < max8997->buck1_vol[i])
1005 max_buck1 = max8997->buck1_vol[i];
1006 if (max_buck2 < max8997->buck2_vol[i])
1007 max_buck2 = max8997->buck2_vol[i];
1008 if (max_buck5 < max8997->buck5_vol[i])
1009 max_buck5 = max8997->buck5_vol[i];
1012 /* For the safety, set max voltage before setting up */
1013 for (i = 0; i < 8; i++) {
1014 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1015 max_buck1, 0x3f);
1016 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1017 max_buck2, 0x3f);
1018 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1019 max_buck5, 0x3f);
1023 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1024 * If at least one of them cares, set gpios.
1026 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1027 pdata->buck5_gpiodvs) {
1028 bool gpio1set = false, gpio2set = false;
1030 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1031 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1032 !gpio_is_valid(pdata->buck125_gpios[2])) {
1033 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1034 ret = -EINVAL;
1035 goto err_out;
1038 ret = gpio_request(pdata->buck125_gpios[0],
1039 "MAX8997 SET1");
1040 if (ret == -EBUSY)
1041 dev_warn(&pdev->dev, "Duplicated gpio request"
1042 " on SET1\n");
1043 else if (ret)
1044 goto err_out;
1045 else
1046 gpio1set = true;
1048 ret = gpio_request(pdata->buck125_gpios[1],
1049 "MAX8997 SET2");
1050 if (ret == -EBUSY)
1051 dev_warn(&pdev->dev, "Duplicated gpio request"
1052 " on SET2\n");
1053 else if (ret) {
1054 if (gpio1set)
1055 gpio_free(pdata->buck125_gpios[0]);
1056 goto err_out;
1057 } else
1058 gpio2set = true;
1060 ret = gpio_request(pdata->buck125_gpios[2],
1061 "MAX8997 SET3");
1062 if (ret == -EBUSY)
1063 dev_warn(&pdev->dev, "Duplicated gpio request"
1064 " on SET3\n");
1065 else if (ret) {
1066 if (gpio1set)
1067 gpio_free(pdata->buck125_gpios[0]);
1068 if (gpio2set)
1069 gpio_free(pdata->buck125_gpios[1]);
1070 goto err_out;
1073 gpio_direction_output(pdata->buck125_gpios[0],
1074 (max8997->buck125_gpioindex >> 2)
1075 & 0x1); /* SET1 */
1076 gpio_direction_output(pdata->buck125_gpios[1],
1077 (max8997->buck125_gpioindex >> 1)
1078 & 0x1); /* SET2 */
1079 gpio_direction_output(pdata->buck125_gpios[2],
1080 (max8997->buck125_gpioindex >> 0)
1081 & 0x1); /* SET3 */
1082 ret = 0;
1085 /* DVS-GPIO disabled */
1086 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1087 (1 << 1) : (0 << 1), 1 << 1);
1088 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1089 (1 << 1) : (0 << 1), 1 << 1);
1090 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1091 (1 << 1) : (0 << 1), 1 << 1);
1093 /* Initialize all the DVS related BUCK registers */
1094 for (i = 0; i < 8; i++) {
1095 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1096 max8997->buck1_vol[i],
1097 0x3f);
1098 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1099 max8997->buck2_vol[i],
1100 0x3f);
1101 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1102 max8997->buck5_vol[i],
1103 0x3f);
1106 /* Misc Settings */
1107 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1108 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1110 for (i = 0; i < pdata->num_regulators; i++) {
1111 const struct voltage_map_desc *desc;
1112 int id = pdata->regulators[i].id;
1114 desc = reg_voltage_map[id];
1115 if (desc)
1116 regulators[id].n_voltages =
1117 (desc->max - desc->min) / desc->step + 1;
1118 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1119 regulators[id].n_voltages = 4;
1120 else if (id == MAX8997_CHARGER_CV)
1121 regulators[id].n_voltages = 16;
1123 config.dev = max8997->dev;
1124 config.init_data = pdata->regulators[i].initdata;
1125 config.driver_data = max8997;
1127 rdev[i] = regulator_register(&regulators[id], &config);
1128 if (IS_ERR(rdev[i])) {
1129 ret = PTR_ERR(rdev[i]);
1130 dev_err(max8997->dev, "regulator init failed for %d\n",
1131 id);
1132 rdev[i] = NULL;
1133 goto err;
1137 return 0;
1138 err:
1139 while (--i >= 0)
1140 regulator_unregister(rdev[i]);
1141 err_out:
1142 return ret;
1145 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1147 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1148 struct regulator_dev **rdev = max8997->rdev;
1149 int i;
1151 for (i = 0; i < max8997->num_regulators; i++)
1152 regulator_unregister(rdev[i]);
1153 return 0;
1156 static const struct platform_device_id max8997_pmic_id[] = {
1157 { "max8997-pmic", 0},
1158 { },
1160 MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1162 static struct platform_driver max8997_pmic_driver = {
1163 .driver = {
1164 .name = "max8997-pmic",
1165 .owner = THIS_MODULE,
1167 .probe = max8997_pmic_probe,
1168 .remove = __devexit_p(max8997_pmic_remove),
1169 .id_table = max8997_pmic_id,
1172 static int __init max8997_pmic_init(void)
1174 return platform_driver_register(&max8997_pmic_driver);
1176 subsys_initcall(max8997_pmic_init);
1178 static void __exit max8997_pmic_cleanup(void)
1180 platform_driver_unregister(&max8997_pmic_driver);
1182 module_exit(max8997_pmic_cleanup);
1184 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1185 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1186 MODULE_LICENSE("GPL");