Merge branch 'topic/asoc' into for-linus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / max8997.c
blobad6628ca94f41378bdddbe11f1286d54a64f4fd0
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/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;
70 unsigned int n_bits;
73 /* Voltage maps in mV */
74 static const struct voltage_map_desc ldo_voltage_map_desc = {
75 .min = 800, .max = 3950, .step = 50, .n_bits = 6,
76 }; /* LDO1 ~ 18, 21 all */
78 static const struct voltage_map_desc buck1245_voltage_map_desc = {
79 .min = 650, .max = 2225, .step = 25, .n_bits = 6,
80 }; /* Buck1, 2, 4, 5 */
82 static const struct voltage_map_desc buck37_voltage_map_desc = {
83 .min = 750, .max = 3900, .step = 50, .n_bits = 6,
84 }; /* Buck3, 7 */
86 /* current map in mA */
87 static const struct voltage_map_desc charger_current_map_desc = {
88 .min = 200, .max = 950, .step = 50, .n_bits = 4,
91 static const struct voltage_map_desc topoff_current_map_desc = {
92 .min = 50, .max = 200, .step = 10, .n_bits = 4,
95 static const struct voltage_map_desc *reg_voltage_map[] = {
96 [MAX8997_LDO1] = &ldo_voltage_map_desc,
97 [MAX8997_LDO2] = &ldo_voltage_map_desc,
98 [MAX8997_LDO3] = &ldo_voltage_map_desc,
99 [MAX8997_LDO4] = &ldo_voltage_map_desc,
100 [MAX8997_LDO5] = &ldo_voltage_map_desc,
101 [MAX8997_LDO6] = &ldo_voltage_map_desc,
102 [MAX8997_LDO7] = &ldo_voltage_map_desc,
103 [MAX8997_LDO8] = &ldo_voltage_map_desc,
104 [MAX8997_LDO9] = &ldo_voltage_map_desc,
105 [MAX8997_LDO10] = &ldo_voltage_map_desc,
106 [MAX8997_LDO11] = &ldo_voltage_map_desc,
107 [MAX8997_LDO12] = &ldo_voltage_map_desc,
108 [MAX8997_LDO13] = &ldo_voltage_map_desc,
109 [MAX8997_LDO14] = &ldo_voltage_map_desc,
110 [MAX8997_LDO15] = &ldo_voltage_map_desc,
111 [MAX8997_LDO16] = &ldo_voltage_map_desc,
112 [MAX8997_LDO17] = &ldo_voltage_map_desc,
113 [MAX8997_LDO18] = &ldo_voltage_map_desc,
114 [MAX8997_LDO21] = &ldo_voltage_map_desc,
115 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
116 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
117 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
118 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
119 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
120 [MAX8997_BUCK6] = NULL,
121 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
122 [MAX8997_EN32KHZ_AP] = NULL,
123 [MAX8997_EN32KHZ_CP] = NULL,
124 [MAX8997_ENVICHG] = NULL,
125 [MAX8997_ESAFEOUT1] = NULL,
126 [MAX8997_ESAFEOUT2] = NULL,
127 [MAX8997_CHARGER_CV] = NULL,
128 [MAX8997_CHARGER] = &charger_current_map_desc,
129 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
132 static inline int max8997_get_rid(struct regulator_dev *rdev)
134 return rdev_get_id(rdev);
137 static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
138 unsigned int selector)
140 int rid = max8997_get_rid(rdev);
142 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
143 switch (selector) {
144 case 0:
145 return 4850000;
146 case 1:
147 return 4900000;
148 case 2:
149 return 4950000;
150 case 3:
151 return 3300000;
152 default:
153 return -EINVAL;
157 return -EINVAL;
160 static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
161 unsigned int selector)
163 int rid = max8997_get_rid(rdev);
165 if (rid != MAX8997_CHARGER_CV)
166 goto err;
168 switch (selector) {
169 case 0x00:
170 return 4200000;
171 case 0x01 ... 0x0E:
172 return 4000000 + 20000 * (selector - 0x01);
173 case 0x0F:
174 return 4350000;
175 default:
176 return -EINVAL;
178 err:
179 return -EINVAL;
182 static int max8997_list_voltage(struct regulator_dev *rdev,
183 unsigned int selector)
185 const struct voltage_map_desc *desc;
186 int rid = max8997_get_rid(rdev);
187 int val;
189 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
190 rid < 0)
191 return -EINVAL;
193 desc = reg_voltage_map[rid];
194 if (desc == NULL)
195 return -EINVAL;
197 val = desc->min + desc->step * selector;
198 if (val > desc->max)
199 return -EINVAL;
201 return val * 1000;
204 static int max8997_get_enable_register(struct regulator_dev *rdev,
205 int *reg, int *mask, int *pattern)
207 int rid = max8997_get_rid(rdev);
209 switch (rid) {
210 case MAX8997_LDO1 ... MAX8997_LDO21:
211 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
212 *mask = 0xC0;
213 *pattern = 0xC0;
214 break;
215 case MAX8997_BUCK1:
216 *reg = MAX8997_REG_BUCK1CTRL;
217 *mask = 0x01;
218 *pattern = 0x01;
219 break;
220 case MAX8997_BUCK2:
221 *reg = MAX8997_REG_BUCK2CTRL;
222 *mask = 0x01;
223 *pattern = 0x01;
224 break;
225 case MAX8997_BUCK3:
226 *reg = MAX8997_REG_BUCK3CTRL;
227 *mask = 0x01;
228 *pattern = 0x01;
229 break;
230 case MAX8997_BUCK4:
231 *reg = MAX8997_REG_BUCK4CTRL;
232 *mask = 0x01;
233 *pattern = 0x01;
234 break;
235 case MAX8997_BUCK5:
236 *reg = MAX8997_REG_BUCK5CTRL;
237 *mask = 0x01;
238 *pattern = 0x01;
239 break;
240 case MAX8997_BUCK6:
241 *reg = MAX8997_REG_BUCK6CTRL;
242 *mask = 0x01;
243 *pattern = 0x01;
244 break;
245 case MAX8997_BUCK7:
246 *reg = MAX8997_REG_BUCK7CTRL;
247 *mask = 0x01;
248 *pattern = 0x01;
249 break;
250 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
251 *reg = MAX8997_REG_MAINCON1;
252 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
253 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
254 break;
255 case MAX8997_ENVICHG:
256 *reg = MAX8997_REG_MBCCTRL1;
257 *mask = 0x80;
258 *pattern = 0x80;
259 break;
260 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
261 *reg = MAX8997_REG_SAFEOUTCTRL;
262 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
263 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
264 break;
265 case MAX8997_CHARGER:
266 *reg = MAX8997_REG_MBCCTRL2;
267 *mask = 0x40;
268 *pattern = 0x40;
269 break;
270 default:
271 /* Not controllable or not exists */
272 return -EINVAL;
275 return 0;
278 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
280 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
281 struct i2c_client *i2c = max8997->iodev->i2c;
282 int ret, reg, mask, pattern;
283 u8 val;
285 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
286 if (ret == -EINVAL)
287 return 1; /* "not controllable" */
288 else if (ret)
289 return ret;
291 ret = max8997_read_reg(i2c, reg, &val);
292 if (ret)
293 return ret;
295 return (val & mask) == pattern;
298 static int max8997_reg_enable(struct regulator_dev *rdev)
300 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
301 struct i2c_client *i2c = max8997->iodev->i2c;
302 int ret, reg, mask, pattern;
304 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
305 if (ret)
306 return ret;
308 return max8997_update_reg(i2c, reg, pattern, mask);
311 static int max8997_reg_disable(struct regulator_dev *rdev)
313 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
314 struct i2c_client *i2c = max8997->iodev->i2c;
315 int ret, reg, mask, pattern;
317 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
318 if (ret)
319 return ret;
321 return max8997_update_reg(i2c, reg, ~pattern, mask);
324 static int max8997_get_voltage_register(struct regulator_dev *rdev,
325 int *_reg, int *_shift, int *_mask)
327 int rid = max8997_get_rid(rdev);
328 int reg, shift = 0, mask = 0x3f;
330 switch (rid) {
331 case MAX8997_LDO1 ... MAX8997_LDO21:
332 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
333 break;
334 case MAX8997_BUCK1:
335 reg = MAX8997_REG_BUCK1DVS1;
336 break;
337 case MAX8997_BUCK2:
338 reg = MAX8997_REG_BUCK2DVS1;
339 break;
340 case MAX8997_BUCK3:
341 reg = MAX8997_REG_BUCK3DVS;
342 break;
343 case MAX8997_BUCK4:
344 reg = MAX8997_REG_BUCK4DVS;
345 break;
346 case MAX8997_BUCK5:
347 reg = MAX8997_REG_BUCK5DVS1;
348 break;
349 case MAX8997_BUCK7:
350 reg = MAX8997_REG_BUCK7DVS;
351 break;
352 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
353 reg = MAX8997_REG_SAFEOUTCTRL;
354 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
355 mask = 0x3;
356 break;
357 case MAX8997_CHARGER_CV:
358 reg = MAX8997_REG_MBCCTRL3;
359 shift = 0;
360 mask = 0xf;
361 break;
362 case MAX8997_CHARGER:
363 reg = MAX8997_REG_MBCCTRL4;
364 shift = 0;
365 mask = 0xf;
366 break;
367 case MAX8997_CHARGER_TOPOFF:
368 reg = MAX8997_REG_MBCCTRL5;
369 shift = 0;
370 mask = 0xf;
371 break;
372 default:
373 return -EINVAL;
376 *_reg = reg;
377 *_shift = shift;
378 *_mask = mask;
380 return 0;
383 static int max8997_get_voltage(struct regulator_dev *rdev)
385 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
386 struct i2c_client *i2c = max8997->iodev->i2c;
387 int reg, shift, mask, ret;
388 int rid = max8997_get_rid(rdev);
389 u8 val;
391 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
392 if (ret)
393 return ret;
395 if ((rid == MAX8997_BUCK1 && max8997->buck1_gpiodvs) ||
396 (rid == MAX8997_BUCK2 && max8997->buck2_gpiodvs) ||
397 (rid == MAX8997_BUCK5 && max8997->buck5_gpiodvs))
398 reg += max8997->buck125_gpioindex;
400 ret = max8997_read_reg(i2c, reg, &val);
401 if (ret)
402 return ret;
404 val >>= shift;
405 val &= mask;
407 if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
408 return rdev->desc->ops->list_voltage(rdev, val);
411 * max8997_list_voltage returns value for any rdev with voltage_map,
412 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
413 * list_voltage ops (they are current regulators).
415 return max8997_list_voltage(rdev, val);
418 static inline int max8997_get_voltage_proper_val(
419 const struct voltage_map_desc *desc,
420 int min_vol, int max_vol)
422 int i = 0;
424 if (desc == NULL)
425 return -EINVAL;
427 if (max_vol < desc->min || min_vol > desc->max)
428 return -EINVAL;
430 while (desc->min + desc->step * i < min_vol &&
431 desc->min + desc->step * i < desc->max)
432 i++;
434 if (desc->min + desc->step * i > max_vol)
435 return -EINVAL;
437 if (i >= (1 << desc->n_bits))
438 return -EINVAL;
440 return i;
443 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
444 int min_uV, int max_uV, unsigned *selector)
446 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
447 struct i2c_client *i2c = max8997->iodev->i2c;
448 int rid = max8997_get_rid(rdev);
449 int lb, ub;
450 int reg, shift = 0, mask, ret = 0;
451 u8 val = 0x0;
453 if (rid != MAX8997_CHARGER_CV)
454 return -EINVAL;
456 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
457 if (ret)
458 return ret;
460 if (max_uV < 4000000 || min_uV > 4350000)
461 return -EINVAL;
463 if (min_uV <= 4000000) {
464 if (max_uV >= 4000000)
465 return -EINVAL;
466 else
467 val = 0x1;
468 } else if (min_uV <= 4200000 && max_uV >= 4200000)
469 val = 0x0;
470 else {
471 lb = (min_uV - 4000001) / 20000 + 2;
472 ub = (max_uV - 4000000) / 20000 + 1;
474 if (lb > ub)
475 return -EINVAL;
477 if (lb < 0xf)
478 val = lb;
479 else {
480 if (ub >= 0xf)
481 val = 0xf;
482 else
483 return -EINVAL;
487 *selector = val;
489 ret = max8997_update_reg(i2c, reg, val << shift, mask);
491 return ret;
495 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
496 * BUCK1, 2, and 5 are available if they are not controlled by gpio
498 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
499 int min_uV, int max_uV, unsigned *selector)
501 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
502 struct i2c_client *i2c = max8997->iodev->i2c;
503 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
504 const struct voltage_map_desc *desc;
505 int rid = max8997_get_rid(rdev);
506 int reg, shift = 0, mask, ret;
507 int i;
508 u8 org;
510 switch (rid) {
511 case MAX8997_LDO1 ... MAX8997_LDO21:
512 break;
513 case MAX8997_BUCK1 ... MAX8997_BUCK5:
514 break;
515 case MAX8997_BUCK6:
516 return -EINVAL;
517 case MAX8997_BUCK7:
518 break;
519 case MAX8997_CHARGER:
520 break;
521 case MAX8997_CHARGER_TOPOFF:
522 break;
523 default:
524 return -EINVAL;
527 desc = reg_voltage_map[rid];
529 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
530 if (i < 0)
531 return i;
533 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
534 if (ret)
535 return ret;
537 max8997_read_reg(i2c, reg, &org);
538 org = (org & mask) >> shift;
540 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
541 *selector = i;
543 if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 ||
544 rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) {
545 /* If the voltage is increasing */
546 if (org < i)
547 udelay(DIV_ROUND_UP(desc->step * (i - org),
548 max8997->ramp_delay));
551 return ret;
555 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
557 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
558 * of one of the bucks may affect that of another buck, which is the side
559 * effect of the change (set_voltage). This function examines the GPIO-DVS
560 * configurations and checks whether such side-effect exists.
562 static int max8997_assess_side_effect(struct regulator_dev *rdev,
563 u8 new_val, int *best)
565 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
566 int rid = max8997_get_rid(rdev);
567 u8 *buckx_val[3];
568 bool buckx_gpiodvs[3];
569 int side_effect[8];
570 int min_side_effect = INT_MAX;
571 int i;
573 *best = -1;
575 switch (rid) {
576 case MAX8997_BUCK1:
577 rid = 0;
578 break;
579 case MAX8997_BUCK2:
580 rid = 1;
581 break;
582 case MAX8997_BUCK5:
583 rid = 2;
584 break;
585 default:
586 return -EINVAL;
589 buckx_val[0] = max8997->buck1_vol;
590 buckx_val[1] = max8997->buck2_vol;
591 buckx_val[2] = max8997->buck5_vol;
592 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
593 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
594 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
596 for (i = 0; i < 8; i++) {
597 int others;
599 if (new_val != (buckx_val[rid])[i]) {
600 side_effect[i] = -1;
601 continue;
604 side_effect[i] = 0;
605 for (others = 0; others < 3; others++) {
606 int diff;
608 if (others == rid)
609 continue;
610 if (buckx_gpiodvs[others] == false)
611 continue; /* Not affected */
612 diff = (buckx_val[others])[i] -
613 (buckx_val[others])[max8997->buck125_gpioindex];
614 if (diff > 0)
615 side_effect[i] += diff;
616 else if (diff < 0)
617 side_effect[i] -= diff;
619 if (side_effect[i] == 0) {
620 *best = i;
621 return 0; /* NO SIDE EFFECT! Use This! */
623 if (side_effect[i] < min_side_effect) {
624 min_side_effect = side_effect[i];
625 *best = i;
629 if (*best == -1)
630 return -EINVAL;
632 return side_effect[*best];
636 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
637 * max8997_set_voltage_ldobuck to do the job.
639 static int max8997_set_voltage_buck(struct regulator_dev *rdev,
640 int min_uV, int max_uV, unsigned *selector)
642 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
643 int rid = max8997_get_rid(rdev);
644 const struct voltage_map_desc *desc;
645 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
646 bool gpio_dvs_mode = false;
647 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
649 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
650 return -EINVAL;
652 switch (rid) {
653 case MAX8997_BUCK1:
654 if (max8997->buck1_gpiodvs)
655 gpio_dvs_mode = true;
656 break;
657 case MAX8997_BUCK2:
658 if (max8997->buck2_gpiodvs)
659 gpio_dvs_mode = true;
660 break;
661 case MAX8997_BUCK5:
662 if (max8997->buck5_gpiodvs)
663 gpio_dvs_mode = true;
664 break;
667 if (!gpio_dvs_mode)
668 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
669 selector);
671 desc = reg_voltage_map[rid];
672 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
673 if (new_val < 0)
674 return new_val;
676 tmp_dmg = INT_MAX;
677 tmp_idx = -1;
678 tmp_val = -1;
679 do {
680 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
681 if (damage == 0)
682 goto out;
684 if (tmp_dmg > damage) {
685 tmp_idx = new_idx;
686 tmp_val = new_val;
687 tmp_dmg = damage;
690 new_val++;
691 } while (desc->min + desc->step + new_val <= desc->max);
693 new_idx = tmp_idx;
694 new_val = tmp_val;
696 if (max8997->ignore_gpiodvs_side_effect == false)
697 return -EINVAL;
699 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
700 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
702 out:
703 if (new_idx < 0 || new_val < 0)
704 return -EINVAL;
706 max8997->buck125_gpioindex = new_idx;
707 max8997_set_gpio(max8997);
708 *selector = new_val;
710 return 0;
713 static const int safeoutvolt[] = {
714 3300000,
715 4850000,
716 4900000,
717 4950000,
720 /* For SAFEOUT1 and SAFEOUT2 */
721 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
722 int min_uV, int max_uV, unsigned *selector)
724 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
725 struct i2c_client *i2c = max8997->iodev->i2c;
726 int rid = max8997_get_rid(rdev);
727 int reg, shift = 0, mask, ret;
728 int i = 0;
729 u8 val;
731 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
732 return -EINVAL;
734 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
735 if (min_uV <= safeoutvolt[i] &&
736 max_uV >= safeoutvolt[i])
737 break;
740 if (i >= ARRAY_SIZE(safeoutvolt))
741 return -EINVAL;
743 if (i == 0)
744 val = 0x3;
745 else
746 val = i - 1;
748 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
749 if (ret)
750 return ret;
752 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
753 *selector = val;
755 return ret;
758 static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
760 return 0;
763 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
765 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
766 struct i2c_client *i2c = max8997->iodev->i2c;
767 int ret, reg, mask, pattern;
768 int rid = max8997_get_rid(rdev);
770 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
771 if (ret)
772 return ret;
774 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
776 if (rid == MAX8997_LDO1 ||
777 rid == MAX8997_LDO10 ||
778 rid == MAX8997_LDO21) {
779 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
780 rdev->desc->name);
781 return max8997_update_reg(i2c, reg, 0x40, mask);
784 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
785 rdev->desc->name, max8997->saved_states[rid] & mask,
786 (~pattern) & mask);
787 return max8997_update_reg(i2c, reg, ~pattern, mask);
790 static struct regulator_ops max8997_ldo_ops = {
791 .list_voltage = max8997_list_voltage,
792 .is_enabled = max8997_reg_is_enabled,
793 .enable = max8997_reg_enable,
794 .disable = max8997_reg_disable,
795 .get_voltage = max8997_get_voltage,
796 .set_voltage = max8997_set_voltage_ldobuck,
797 .set_suspend_enable = max8997_reg_enable_suspend,
798 .set_suspend_disable = max8997_reg_disable_suspend,
801 static struct regulator_ops max8997_buck_ops = {
802 .list_voltage = max8997_list_voltage,
803 .is_enabled = max8997_reg_is_enabled,
804 .enable = max8997_reg_enable,
805 .disable = max8997_reg_disable,
806 .get_voltage = max8997_get_voltage,
807 .set_voltage = max8997_set_voltage_buck,
808 .set_suspend_enable = max8997_reg_enable_suspend,
809 .set_suspend_disable = max8997_reg_disable_suspend,
812 static struct regulator_ops max8997_fixedvolt_ops = {
813 .list_voltage = max8997_list_voltage,
814 .is_enabled = max8997_reg_is_enabled,
815 .enable = max8997_reg_enable,
816 .disable = max8997_reg_disable,
817 .set_suspend_enable = max8997_reg_enable_suspend,
818 .set_suspend_disable = max8997_reg_disable_suspend,
821 static struct regulator_ops max8997_safeout_ops = {
822 .list_voltage = max8997_list_voltage_safeout,
823 .is_enabled = max8997_reg_is_enabled,
824 .enable = max8997_reg_enable,
825 .disable = max8997_reg_disable,
826 .get_voltage = max8997_get_voltage,
827 .set_voltage = max8997_set_voltage_safeout,
828 .set_suspend_enable = max8997_reg_enable_suspend,
829 .set_suspend_disable = max8997_reg_disable_suspend,
832 static struct regulator_ops max8997_fixedstate_ops = {
833 .list_voltage = max8997_list_voltage_charger_cv,
834 .get_voltage = max8997_get_voltage,
835 .set_voltage = max8997_set_voltage_charger_cv,
838 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev,
839 int min_uV, int max_uV)
841 unsigned dummy;
843 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy);
847 static struct regulator_ops max8997_charger_ops = {
848 .is_enabled = max8997_reg_is_enabled,
849 .enable = max8997_reg_enable,
850 .disable = max8997_reg_disable,
851 .get_current_limit = max8997_get_voltage,
852 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
855 static struct regulator_ops max8997_charger_fixedstate_ops = {
856 .is_enabled = max8997_reg_is_enabled,
857 .get_current_limit = max8997_get_voltage,
858 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
861 #define regulator_desc_ldo(num) { \
862 .name = "LDO"#num, \
863 .id = MAX8997_LDO##num, \
864 .ops = &max8997_ldo_ops, \
865 .type = REGULATOR_VOLTAGE, \
866 .owner = THIS_MODULE, \
868 #define regulator_desc_buck(num) { \
869 .name = "BUCK"#num, \
870 .id = MAX8997_BUCK##num, \
871 .ops = &max8997_buck_ops, \
872 .type = REGULATOR_VOLTAGE, \
873 .owner = THIS_MODULE, \
876 static struct regulator_desc regulators[] = {
877 regulator_desc_ldo(1),
878 regulator_desc_ldo(2),
879 regulator_desc_ldo(3),
880 regulator_desc_ldo(4),
881 regulator_desc_ldo(5),
882 regulator_desc_ldo(6),
883 regulator_desc_ldo(7),
884 regulator_desc_ldo(8),
885 regulator_desc_ldo(9),
886 regulator_desc_ldo(10),
887 regulator_desc_ldo(11),
888 regulator_desc_ldo(12),
889 regulator_desc_ldo(13),
890 regulator_desc_ldo(14),
891 regulator_desc_ldo(15),
892 regulator_desc_ldo(16),
893 regulator_desc_ldo(17),
894 regulator_desc_ldo(18),
895 regulator_desc_ldo(21),
896 regulator_desc_buck(1),
897 regulator_desc_buck(2),
898 regulator_desc_buck(3),
899 regulator_desc_buck(4),
900 regulator_desc_buck(5),
902 .name = "BUCK6",
903 .id = MAX8997_BUCK6,
904 .ops = &max8997_fixedvolt_ops,
905 .type = REGULATOR_VOLTAGE,
906 .owner = THIS_MODULE,
908 regulator_desc_buck(7),
910 .name = "EN32KHz AP",
911 .id = MAX8997_EN32KHZ_AP,
912 .ops = &max8997_fixedvolt_ops,
913 .type = REGULATOR_VOLTAGE,
914 .owner = THIS_MODULE,
915 }, {
916 .name = "EN32KHz CP",
917 .id = MAX8997_EN32KHZ_CP,
918 .ops = &max8997_fixedvolt_ops,
919 .type = REGULATOR_VOLTAGE,
920 .owner = THIS_MODULE,
921 }, {
922 .name = "ENVICHG",
923 .id = MAX8997_ENVICHG,
924 .ops = &max8997_fixedvolt_ops,
925 .type = REGULATOR_VOLTAGE,
926 .owner = THIS_MODULE,
927 }, {
928 .name = "ESAFEOUT1",
929 .id = MAX8997_ESAFEOUT1,
930 .ops = &max8997_safeout_ops,
931 .type = REGULATOR_VOLTAGE,
932 .owner = THIS_MODULE,
933 }, {
934 .name = "ESAFEOUT2",
935 .id = MAX8997_ESAFEOUT2,
936 .ops = &max8997_safeout_ops,
937 .type = REGULATOR_VOLTAGE,
938 .owner = THIS_MODULE,
939 }, {
940 .name = "CHARGER CV",
941 .id = MAX8997_CHARGER_CV,
942 .ops = &max8997_fixedstate_ops,
943 .type = REGULATOR_VOLTAGE,
944 .owner = THIS_MODULE,
945 }, {
946 .name = "CHARGER",
947 .id = MAX8997_CHARGER,
948 .ops = &max8997_charger_ops,
949 .type = REGULATOR_CURRENT,
950 .owner = THIS_MODULE,
951 }, {
952 .name = "CHARGER TOPOFF",
953 .id = MAX8997_CHARGER_TOPOFF,
954 .ops = &max8997_charger_fixedstate_ops,
955 .type = REGULATOR_CURRENT,
956 .owner = THIS_MODULE,
960 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
962 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
963 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
964 struct regulator_dev **rdev;
965 struct max8997_data *max8997;
966 struct i2c_client *i2c;
967 int i, ret, size;
968 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
970 if (!pdata) {
971 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
972 return -ENODEV;
975 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
976 if (!max8997)
977 return -ENOMEM;
979 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
980 max8997->rdev = kzalloc(size, GFP_KERNEL);
981 if (!max8997->rdev) {
982 kfree(max8997);
983 return -ENOMEM;
986 rdev = max8997->rdev;
987 max8997->dev = &pdev->dev;
988 max8997->iodev = iodev;
989 max8997->num_regulators = pdata->num_regulators;
990 platform_set_drvdata(pdev, max8997);
991 i2c = max8997->iodev->i2c;
993 max8997->buck125_gpioindex = pdata->buck125_default_idx;
994 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
995 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
996 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
997 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
998 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
1000 for (i = 0; i < 8; i++) {
1001 max8997->buck1_vol[i] = ret =
1002 max8997_get_voltage_proper_val(
1003 &buck1245_voltage_map_desc,
1004 pdata->buck1_voltage[i] / 1000,
1005 pdata->buck1_voltage[i] / 1000 +
1006 buck1245_voltage_map_desc.step);
1007 if (ret < 0)
1008 goto err_alloc;
1010 max8997->buck2_vol[i] = ret =
1011 max8997_get_voltage_proper_val(
1012 &buck1245_voltage_map_desc,
1013 pdata->buck2_voltage[i] / 1000,
1014 pdata->buck2_voltage[i] / 1000 +
1015 buck1245_voltage_map_desc.step);
1016 if (ret < 0)
1017 goto err_alloc;
1019 max8997->buck5_vol[i] = ret =
1020 max8997_get_voltage_proper_val(
1021 &buck1245_voltage_map_desc,
1022 pdata->buck5_voltage[i] / 1000,
1023 pdata->buck5_voltage[i] / 1000 +
1024 buck1245_voltage_map_desc.step);
1025 if (ret < 0)
1026 goto err_alloc;
1028 if (max_buck1 < max8997->buck1_vol[i])
1029 max_buck1 = max8997->buck1_vol[i];
1030 if (max_buck2 < max8997->buck2_vol[i])
1031 max_buck2 = max8997->buck2_vol[i];
1032 if (max_buck5 < max8997->buck5_vol[i])
1033 max_buck5 = max8997->buck5_vol[i];
1036 /* For the safety, set max voltage before setting up */
1037 for (i = 0; i < 8; i++) {
1038 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1039 max_buck1, 0x3f);
1040 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1041 max_buck2, 0x3f);
1042 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1043 max_buck5, 0x3f);
1047 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1048 * If at least one of them cares, set gpios.
1050 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1051 pdata->buck5_gpiodvs) {
1052 bool gpio1set = false, gpio2set = false;
1054 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1055 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1056 !gpio_is_valid(pdata->buck125_gpios[2])) {
1057 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1058 ret = -EINVAL;
1059 goto err_alloc;
1062 ret = gpio_request(pdata->buck125_gpios[0],
1063 "MAX8997 SET1");
1064 if (ret == -EBUSY)
1065 dev_warn(&pdev->dev, "Duplicated gpio request"
1066 " on SET1\n");
1067 else if (ret)
1068 goto err_alloc;
1069 else
1070 gpio1set = true;
1072 ret = gpio_request(pdata->buck125_gpios[1],
1073 "MAX8997 SET2");
1074 if (ret == -EBUSY)
1075 dev_warn(&pdev->dev, "Duplicated gpio request"
1076 " on SET2\n");
1077 else if (ret) {
1078 if (gpio1set)
1079 gpio_free(pdata->buck125_gpios[0]);
1080 goto err_alloc;
1081 } else
1082 gpio2set = true;
1084 ret = gpio_request(pdata->buck125_gpios[2],
1085 "MAX8997 SET3");
1086 if (ret == -EBUSY)
1087 dev_warn(&pdev->dev, "Duplicated gpio request"
1088 " on SET3\n");
1089 else if (ret) {
1090 if (gpio1set)
1091 gpio_free(pdata->buck125_gpios[0]);
1092 if (gpio2set)
1093 gpio_free(pdata->buck125_gpios[1]);
1094 goto err_alloc;
1097 gpio_direction_output(pdata->buck125_gpios[0],
1098 (max8997->buck125_gpioindex >> 2)
1099 & 0x1); /* SET1 */
1100 gpio_direction_output(pdata->buck125_gpios[1],
1101 (max8997->buck125_gpioindex >> 1)
1102 & 0x1); /* SET2 */
1103 gpio_direction_output(pdata->buck125_gpios[2],
1104 (max8997->buck125_gpioindex >> 0)
1105 & 0x1); /* SET3 */
1106 ret = 0;
1109 /* DVS-GPIO disabled */
1110 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1111 (1 << 1) : (0 << 1), 1 << 1);
1112 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1113 (1 << 1) : (0 << 1), 1 << 1);
1114 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1115 (1 << 1) : (0 << 1), 1 << 1);
1117 /* Initialize all the DVS related BUCK registers */
1118 for (i = 0; i < 8; i++) {
1119 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1120 max8997->buck1_vol[i],
1121 0x3f);
1122 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1123 max8997->buck2_vol[i],
1124 0x3f);
1125 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1126 max8997->buck5_vol[i],
1127 0x3f);
1130 /* Misc Settings */
1131 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1132 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1134 for (i = 0; i < pdata->num_regulators; i++) {
1135 const struct voltage_map_desc *desc;
1136 int id = pdata->regulators[i].id;
1138 desc = reg_voltage_map[id];
1139 if (desc)
1140 regulators[id].n_voltages =
1141 (desc->max - desc->min) / desc->step + 1;
1142 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1143 regulators[id].n_voltages = 4;
1144 else if (id == MAX8997_CHARGER_CV)
1145 regulators[id].n_voltages = 16;
1147 rdev[i] = regulator_register(&regulators[id], max8997->dev,
1148 pdata->regulators[i].initdata, max8997);
1149 if (IS_ERR(rdev[i])) {
1150 ret = PTR_ERR(rdev[i]);
1151 dev_err(max8997->dev, "regulator init failed for %d\n",
1152 id);
1153 rdev[i] = NULL;
1154 goto err;
1158 return 0;
1159 err:
1160 for (i = 0; i < max8997->num_regulators; i++)
1161 if (rdev[i])
1162 regulator_unregister(rdev[i]);
1163 err_alloc:
1164 kfree(max8997->rdev);
1165 kfree(max8997);
1167 return ret;
1170 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1172 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1173 struct regulator_dev **rdev = max8997->rdev;
1174 int i;
1176 for (i = 0; i < max8997->num_regulators; i++)
1177 if (rdev[i])
1178 regulator_unregister(rdev[i]);
1180 kfree(max8997->rdev);
1181 kfree(max8997);
1183 return 0;
1186 static const struct platform_device_id max8997_pmic_id[] = {
1187 { "max8997-pmic", 0},
1188 { },
1190 MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1192 static struct platform_driver max8997_pmic_driver = {
1193 .driver = {
1194 .name = "max8997-pmic",
1195 .owner = THIS_MODULE,
1197 .probe = max8997_pmic_probe,
1198 .remove = __devexit_p(max8997_pmic_remove),
1199 .id_table = max8997_pmic_id,
1202 static int __init max8997_pmic_init(void)
1204 return platform_driver_register(&max8997_pmic_driver);
1206 subsys_initcall(max8997_pmic_init);
1208 static void __exit max8997_pmic_cleanup(void)
1210 platform_driver_unregister(&max8997_pmic_driver);
1212 module_exit(max8997_pmic_cleanup);
1214 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1215 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1216 MODULE_LICENSE("GPL");