Linux 3.4.102
[linux/fpc-iii.git] / drivers / regulator / max8997.c
blob8350f50bb062958b6f74442f71eeadba35d21951
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 uV */
75 static const struct voltage_map_desc ldo_voltage_map_desc = {
76 .min = 800000, .max = 3950000, .step = 50000, .n_bits = 6,
77 }; /* LDO1 ~ 18, 21 all */
79 static const struct voltage_map_desc buck1245_voltage_map_desc = {
80 .min = 650000, .max = 2225000, .step = 25000, .n_bits = 6,
81 }; /* Buck1, 2, 4, 5 */
83 static const struct voltage_map_desc buck37_voltage_map_desc = {
84 .min = 750000, .max = 3900000, .step = 50000, .n_bits = 6,
85 }; /* Buck3, 7 */
87 /* current map in uA */
88 static const struct voltage_map_desc charger_current_map_desc = {
89 .min = 200000, .max = 950000, .step = 50000, .n_bits = 4,
92 static const struct voltage_map_desc topoff_current_map_desc = {
93 .min = 50000, .max = 200000, .step = 10000, .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 int max8997_list_voltage_safeout(struct regulator_dev *rdev,
134 unsigned int selector)
136 int rid = rdev_get_id(rdev);
138 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
139 switch (selector) {
140 case 0:
141 return 4850000;
142 case 1:
143 return 4900000;
144 case 2:
145 return 4950000;
146 case 3:
147 return 3300000;
148 default:
149 return -EINVAL;
153 return -EINVAL;
156 static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
157 unsigned int selector)
159 int rid = rdev_get_id(rdev);
161 if (rid != MAX8997_CHARGER_CV)
162 goto err;
164 switch (selector) {
165 case 0x00:
166 return 4200000;
167 case 0x01 ... 0x0E:
168 return 4000000 + 20000 * (selector - 0x01);
169 case 0x0F:
170 return 4350000;
171 default:
172 return -EINVAL;
174 err:
175 return -EINVAL;
178 static int max8997_list_voltage(struct regulator_dev *rdev,
179 unsigned int selector)
181 const struct voltage_map_desc *desc;
182 int rid = rdev_get_id(rdev);
183 int val;
185 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
186 rid < 0)
187 return -EINVAL;
189 desc = reg_voltage_map[rid];
190 if (desc == NULL)
191 return -EINVAL;
193 val = desc->min + desc->step * selector;
194 if (val > desc->max)
195 return -EINVAL;
197 return val;
200 static int max8997_get_enable_register(struct regulator_dev *rdev,
201 int *reg, int *mask, int *pattern)
203 int rid = rdev_get_id(rdev);
205 switch (rid) {
206 case MAX8997_LDO1 ... MAX8997_LDO21:
207 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
208 *mask = 0xC0;
209 *pattern = 0xC0;
210 break;
211 case MAX8997_BUCK1:
212 *reg = MAX8997_REG_BUCK1CTRL;
213 *mask = 0x01;
214 *pattern = 0x01;
215 break;
216 case MAX8997_BUCK2:
217 *reg = MAX8997_REG_BUCK2CTRL;
218 *mask = 0x01;
219 *pattern = 0x01;
220 break;
221 case MAX8997_BUCK3:
222 *reg = MAX8997_REG_BUCK3CTRL;
223 *mask = 0x01;
224 *pattern = 0x01;
225 break;
226 case MAX8997_BUCK4:
227 *reg = MAX8997_REG_BUCK4CTRL;
228 *mask = 0x01;
229 *pattern = 0x01;
230 break;
231 case MAX8997_BUCK5:
232 *reg = MAX8997_REG_BUCK5CTRL;
233 *mask = 0x01;
234 *pattern = 0x01;
235 break;
236 case MAX8997_BUCK6:
237 *reg = MAX8997_REG_BUCK6CTRL;
238 *mask = 0x01;
239 *pattern = 0x01;
240 break;
241 case MAX8997_BUCK7:
242 *reg = MAX8997_REG_BUCK7CTRL;
243 *mask = 0x01;
244 *pattern = 0x01;
245 break;
246 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
247 *reg = MAX8997_REG_MAINCON1;
248 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
249 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
250 break;
251 case MAX8997_ENVICHG:
252 *reg = MAX8997_REG_MBCCTRL1;
253 *mask = 0x80;
254 *pattern = 0x80;
255 break;
256 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
257 *reg = MAX8997_REG_SAFEOUTCTRL;
258 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
259 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
260 break;
261 case MAX8997_CHARGER:
262 *reg = MAX8997_REG_MBCCTRL2;
263 *mask = 0x40;
264 *pattern = 0x40;
265 break;
266 default:
267 /* Not controllable or not exists */
268 return -EINVAL;
271 return 0;
274 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
276 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
277 struct i2c_client *i2c = max8997->iodev->i2c;
278 int ret, reg, mask, pattern;
279 u8 val;
281 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
282 if (ret == -EINVAL)
283 return 1; /* "not controllable" */
284 else if (ret)
285 return ret;
287 ret = max8997_read_reg(i2c, reg, &val);
288 if (ret)
289 return ret;
291 return (val & mask) == pattern;
294 static int max8997_reg_enable(struct regulator_dev *rdev)
296 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
297 struct i2c_client *i2c = max8997->iodev->i2c;
298 int ret, reg, mask, pattern;
300 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
301 if (ret)
302 return ret;
304 return max8997_update_reg(i2c, reg, pattern, mask);
307 static int max8997_reg_disable(struct regulator_dev *rdev)
309 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
310 struct i2c_client *i2c = max8997->iodev->i2c;
311 int ret, reg, mask, pattern;
313 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
314 if (ret)
315 return ret;
317 return max8997_update_reg(i2c, reg, ~pattern, mask);
320 static int max8997_get_voltage_register(struct regulator_dev *rdev,
321 int *_reg, int *_shift, int *_mask)
323 int rid = rdev_get_id(rdev);
324 int reg, shift = 0, mask = 0x3f;
326 switch (rid) {
327 case MAX8997_LDO1 ... MAX8997_LDO21:
328 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
329 break;
330 case MAX8997_BUCK1:
331 reg = MAX8997_REG_BUCK1DVS1;
332 break;
333 case MAX8997_BUCK2:
334 reg = MAX8997_REG_BUCK2DVS1;
335 break;
336 case MAX8997_BUCK3:
337 reg = MAX8997_REG_BUCK3DVS;
338 break;
339 case MAX8997_BUCK4:
340 reg = MAX8997_REG_BUCK4DVS;
341 break;
342 case MAX8997_BUCK5:
343 reg = MAX8997_REG_BUCK5DVS1;
344 break;
345 case MAX8997_BUCK7:
346 reg = MAX8997_REG_BUCK7DVS;
347 break;
348 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
349 reg = MAX8997_REG_SAFEOUTCTRL;
350 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
351 mask = 0x3;
352 break;
353 case MAX8997_CHARGER_CV:
354 reg = MAX8997_REG_MBCCTRL3;
355 shift = 0;
356 mask = 0xf;
357 break;
358 case MAX8997_CHARGER:
359 reg = MAX8997_REG_MBCCTRL4;
360 shift = 0;
361 mask = 0xf;
362 break;
363 case MAX8997_CHARGER_TOPOFF:
364 reg = MAX8997_REG_MBCCTRL5;
365 shift = 0;
366 mask = 0xf;
367 break;
368 default:
369 return -EINVAL;
372 *_reg = reg;
373 *_shift = shift;
374 *_mask = mask;
376 return 0;
379 static int max8997_get_voltage(struct regulator_dev *rdev)
381 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
382 struct i2c_client *i2c = max8997->iodev->i2c;
383 int reg, shift, mask, ret;
384 int rid = rdev_get_id(rdev);
385 u8 val;
387 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
388 if (ret)
389 return ret;
391 if ((rid == MAX8997_BUCK1 && max8997->buck1_gpiodvs) ||
392 (rid == MAX8997_BUCK2 && max8997->buck2_gpiodvs) ||
393 (rid == MAX8997_BUCK5 && max8997->buck5_gpiodvs))
394 reg += max8997->buck125_gpioindex;
396 ret = max8997_read_reg(i2c, reg, &val);
397 if (ret)
398 return ret;
400 val >>= shift;
401 val &= mask;
403 if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
404 return rdev->desc->ops->list_voltage(rdev, val);
407 * max8997_list_voltage returns value for any rdev with voltage_map,
408 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
409 * list_voltage ops (they are current regulators).
411 return max8997_list_voltage(rdev, val);
414 static inline int max8997_get_voltage_proper_val(
415 const struct voltage_map_desc *desc,
416 int min_vol, int max_vol)
418 int i = 0;
420 if (desc == NULL)
421 return -EINVAL;
423 if (max_vol < desc->min || min_vol > desc->max)
424 return -EINVAL;
426 while (desc->min + desc->step * i < min_vol &&
427 desc->min + desc->step * i < desc->max)
428 i++;
430 if (desc->min + desc->step * i > max_vol)
431 return -EINVAL;
433 if (i >= (1 << desc->n_bits))
434 return -EINVAL;
436 return i;
439 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
440 int min_uV, int max_uV, unsigned *selector)
442 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
443 struct i2c_client *i2c = max8997->iodev->i2c;
444 int rid = rdev_get_id(rdev);
445 int lb, ub;
446 int reg, shift = 0, mask, ret = 0;
447 u8 val = 0x0;
449 if (rid != MAX8997_CHARGER_CV)
450 return -EINVAL;
452 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
453 if (ret)
454 return ret;
456 if (max_uV < 4000000 || min_uV > 4350000)
457 return -EINVAL;
459 if (min_uV <= 4000000) {
460 if (max_uV >= 4000000)
461 return -EINVAL;
462 else
463 val = 0x1;
464 } else if (min_uV <= 4200000 && max_uV >= 4200000)
465 val = 0x0;
466 else {
467 lb = (min_uV - 4000001) / 20000 + 2;
468 ub = (max_uV - 4000000) / 20000 + 1;
470 if (lb > ub)
471 return -EINVAL;
473 if (lb < 0xf)
474 val = lb;
475 else {
476 if (ub >= 0xf)
477 val = 0xf;
478 else
479 return -EINVAL;
483 *selector = val;
485 ret = max8997_update_reg(i2c, reg, val << shift, mask);
487 return ret;
491 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
492 * BUCK1, 2, and 5 are available if they are not controlled by gpio
494 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
495 int min_uV, int max_uV, unsigned *selector)
497 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
498 struct i2c_client *i2c = max8997->iodev->i2c;
499 const struct voltage_map_desc *desc;
500 int rid = rdev_get_id(rdev);
501 int reg, shift = 0, mask, ret;
502 int i;
503 u8 org;
505 switch (rid) {
506 case MAX8997_LDO1 ... MAX8997_LDO21:
507 break;
508 case MAX8997_BUCK1 ... MAX8997_BUCK5:
509 break;
510 case MAX8997_BUCK6:
511 return -EINVAL;
512 case MAX8997_BUCK7:
513 break;
514 case MAX8997_CHARGER:
515 break;
516 case MAX8997_CHARGER_TOPOFF:
517 break;
518 default:
519 return -EINVAL;
522 desc = reg_voltage_map[rid];
524 i = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
525 if (i < 0)
526 return i;
528 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
529 if (ret)
530 return ret;
532 max8997_read_reg(i2c, reg, &org);
533 org = (org & mask) >> shift;
535 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
536 *selector = i;
538 if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 ||
539 rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) {
540 /* If the voltage is increasing */
541 if (org < i)
542 udelay(DIV_ROUND_UP(desc->step * (i - org),
543 max8997->ramp_delay * 1000));
546 return ret;
550 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
552 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
553 * of one of the bucks may affect that of another buck, which is the side
554 * effect of the change (set_voltage). This function examines the GPIO-DVS
555 * configurations and checks whether such side-effect exists.
557 static int max8997_assess_side_effect(struct regulator_dev *rdev,
558 u8 new_val, int *best)
560 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
561 int rid = rdev_get_id(rdev);
562 u8 *buckx_val[3];
563 bool buckx_gpiodvs[3];
564 int side_effect[8];
565 int min_side_effect = INT_MAX;
566 int i;
568 *best = -1;
570 switch (rid) {
571 case MAX8997_BUCK1:
572 rid = 0;
573 break;
574 case MAX8997_BUCK2:
575 rid = 1;
576 break;
577 case MAX8997_BUCK5:
578 rid = 2;
579 break;
580 default:
581 return -EINVAL;
584 buckx_val[0] = max8997->buck1_vol;
585 buckx_val[1] = max8997->buck2_vol;
586 buckx_val[2] = max8997->buck5_vol;
587 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
588 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
589 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
591 for (i = 0; i < 8; i++) {
592 int others;
594 if (new_val != (buckx_val[rid])[i]) {
595 side_effect[i] = -1;
596 continue;
599 side_effect[i] = 0;
600 for (others = 0; others < 3; others++) {
601 int diff;
603 if (others == rid)
604 continue;
605 if (buckx_gpiodvs[others] == false)
606 continue; /* Not affected */
607 diff = (buckx_val[others])[i] -
608 (buckx_val[others])[max8997->buck125_gpioindex];
609 if (diff > 0)
610 side_effect[i] += diff;
611 else if (diff < 0)
612 side_effect[i] -= diff;
614 if (side_effect[i] == 0) {
615 *best = i;
616 return 0; /* NO SIDE EFFECT! Use This! */
618 if (side_effect[i] < min_side_effect) {
619 min_side_effect = side_effect[i];
620 *best = i;
624 if (*best == -1)
625 return -EINVAL;
627 return side_effect[*best];
631 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
632 * max8997_set_voltage_ldobuck to do the job.
634 static int max8997_set_voltage_buck(struct regulator_dev *rdev,
635 int min_uV, int max_uV, unsigned *selector)
637 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
638 int rid = rdev_get_id(rdev);
639 const struct voltage_map_desc *desc;
640 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
641 bool gpio_dvs_mode = false;
643 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
644 return -EINVAL;
646 switch (rid) {
647 case MAX8997_BUCK1:
648 if (max8997->buck1_gpiodvs)
649 gpio_dvs_mode = true;
650 break;
651 case MAX8997_BUCK2:
652 if (max8997->buck2_gpiodvs)
653 gpio_dvs_mode = true;
654 break;
655 case MAX8997_BUCK5:
656 if (max8997->buck5_gpiodvs)
657 gpio_dvs_mode = true;
658 break;
661 if (!gpio_dvs_mode)
662 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
663 selector);
665 desc = reg_voltage_map[rid];
666 new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
667 if (new_val < 0)
668 return new_val;
670 tmp_dmg = INT_MAX;
671 tmp_idx = -1;
672 tmp_val = -1;
673 do {
674 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
675 if (damage == 0)
676 goto out;
678 if (tmp_dmg > damage) {
679 tmp_idx = new_idx;
680 tmp_val = new_val;
681 tmp_dmg = damage;
684 new_val++;
685 } while (desc->min + desc->step * new_val <= desc->max);
687 new_idx = tmp_idx;
688 new_val = tmp_val;
690 if (max8997->ignore_gpiodvs_side_effect == false)
691 return -EINVAL;
693 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
694 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
696 out:
697 if (new_idx < 0 || new_val < 0)
698 return -EINVAL;
700 max8997->buck125_gpioindex = new_idx;
701 max8997_set_gpio(max8997);
702 *selector = new_val;
704 return 0;
707 static const int safeoutvolt[] = {
708 3300000,
709 4850000,
710 4900000,
711 4950000,
714 /* For SAFEOUT1 and SAFEOUT2 */
715 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
716 int min_uV, int max_uV, unsigned *selector)
718 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
719 struct i2c_client *i2c = max8997->iodev->i2c;
720 int rid = rdev_get_id(rdev);
721 int reg, shift = 0, mask, ret;
722 int i = 0;
723 u8 val;
725 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
726 return -EINVAL;
728 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
729 if (min_uV <= safeoutvolt[i] &&
730 max_uV >= safeoutvolt[i])
731 break;
734 if (i >= ARRAY_SIZE(safeoutvolt))
735 return -EINVAL;
737 if (i == 0)
738 val = 0x3;
739 else
740 val = i - 1;
742 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
743 if (ret)
744 return ret;
746 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
747 *selector = val;
749 return ret;
752 static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
754 return 0;
757 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
759 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
760 struct i2c_client *i2c = max8997->iodev->i2c;
761 int ret, reg, mask, pattern;
762 int rid = rdev_get_id(rdev);
764 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
765 if (ret)
766 return ret;
768 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
770 if (rid == MAX8997_LDO1 ||
771 rid == MAX8997_LDO10 ||
772 rid == MAX8997_LDO21) {
773 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
774 rdev->desc->name);
775 return max8997_update_reg(i2c, reg, 0x40, mask);
778 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
779 rdev->desc->name, max8997->saved_states[rid] & mask,
780 (~pattern) & mask);
781 return max8997_update_reg(i2c, reg, ~pattern, mask);
784 static struct regulator_ops max8997_ldo_ops = {
785 .list_voltage = max8997_list_voltage,
786 .is_enabled = max8997_reg_is_enabled,
787 .enable = max8997_reg_enable,
788 .disable = max8997_reg_disable,
789 .get_voltage = max8997_get_voltage,
790 .set_voltage = max8997_set_voltage_ldobuck,
791 .set_suspend_enable = max8997_reg_enable_suspend,
792 .set_suspend_disable = max8997_reg_disable_suspend,
795 static struct regulator_ops max8997_buck_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 = max8997_get_voltage,
801 .set_voltage = max8997_set_voltage_buck,
802 .set_suspend_enable = max8997_reg_enable_suspend,
803 .set_suspend_disable = max8997_reg_disable_suspend,
806 static struct regulator_ops max8997_fixedvolt_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 .set_suspend_enable = max8997_reg_enable_suspend,
812 .set_suspend_disable = max8997_reg_disable_suspend,
815 static struct regulator_ops max8997_safeout_ops = {
816 .list_voltage = max8997_list_voltage_safeout,
817 .is_enabled = max8997_reg_is_enabled,
818 .enable = max8997_reg_enable,
819 .disable = max8997_reg_disable,
820 .get_voltage = max8997_get_voltage,
821 .set_voltage = max8997_set_voltage_safeout,
822 .set_suspend_enable = max8997_reg_enable_suspend,
823 .set_suspend_disable = max8997_reg_disable_suspend,
826 static struct regulator_ops max8997_fixedstate_ops = {
827 .list_voltage = max8997_list_voltage_charger_cv,
828 .get_voltage = max8997_get_voltage,
829 .set_voltage = max8997_set_voltage_charger_cv,
832 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev,
833 int min_uV, int max_uV)
835 unsigned dummy;
837 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy);
841 static struct regulator_ops max8997_charger_ops = {
842 .is_enabled = max8997_reg_is_enabled,
843 .enable = max8997_reg_enable,
844 .disable = max8997_reg_disable,
845 .get_current_limit = max8997_get_voltage,
846 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
849 static struct regulator_ops max8997_charger_fixedstate_ops = {
850 .is_enabled = max8997_reg_is_enabled,
851 .get_current_limit = max8997_get_voltage,
852 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
855 #define regulator_desc_ldo(num) { \
856 .name = "LDO"#num, \
857 .id = MAX8997_LDO##num, \
858 .ops = &max8997_ldo_ops, \
859 .type = REGULATOR_VOLTAGE, \
860 .owner = THIS_MODULE, \
862 #define regulator_desc_buck(num) { \
863 .name = "BUCK"#num, \
864 .id = MAX8997_BUCK##num, \
865 .ops = &max8997_buck_ops, \
866 .type = REGULATOR_VOLTAGE, \
867 .owner = THIS_MODULE, \
870 static struct regulator_desc regulators[] = {
871 regulator_desc_ldo(1),
872 regulator_desc_ldo(2),
873 regulator_desc_ldo(3),
874 regulator_desc_ldo(4),
875 regulator_desc_ldo(5),
876 regulator_desc_ldo(6),
877 regulator_desc_ldo(7),
878 regulator_desc_ldo(8),
879 regulator_desc_ldo(9),
880 regulator_desc_ldo(10),
881 regulator_desc_ldo(11),
882 regulator_desc_ldo(12),
883 regulator_desc_ldo(13),
884 regulator_desc_ldo(14),
885 regulator_desc_ldo(15),
886 regulator_desc_ldo(16),
887 regulator_desc_ldo(17),
888 regulator_desc_ldo(18),
889 regulator_desc_ldo(21),
890 regulator_desc_buck(1),
891 regulator_desc_buck(2),
892 regulator_desc_buck(3),
893 regulator_desc_buck(4),
894 regulator_desc_buck(5),
896 .name = "BUCK6",
897 .id = MAX8997_BUCK6,
898 .ops = &max8997_fixedvolt_ops,
899 .type = REGULATOR_VOLTAGE,
900 .owner = THIS_MODULE,
902 regulator_desc_buck(7),
904 .name = "EN32KHz_AP",
905 .id = MAX8997_EN32KHZ_AP,
906 .ops = &max8997_fixedvolt_ops,
907 .type = REGULATOR_VOLTAGE,
908 .owner = THIS_MODULE,
909 }, {
910 .name = "EN32KHz_CP",
911 .id = MAX8997_EN32KHZ_CP,
912 .ops = &max8997_fixedvolt_ops,
913 .type = REGULATOR_VOLTAGE,
914 .owner = THIS_MODULE,
915 }, {
916 .name = "ENVICHG",
917 .id = MAX8997_ENVICHG,
918 .ops = &max8997_fixedvolt_ops,
919 .type = REGULATOR_VOLTAGE,
920 .owner = THIS_MODULE,
921 }, {
922 .name = "ESAFEOUT1",
923 .id = MAX8997_ESAFEOUT1,
924 .ops = &max8997_safeout_ops,
925 .type = REGULATOR_VOLTAGE,
926 .owner = THIS_MODULE,
927 }, {
928 .name = "ESAFEOUT2",
929 .id = MAX8997_ESAFEOUT2,
930 .ops = &max8997_safeout_ops,
931 .type = REGULATOR_VOLTAGE,
932 .owner = THIS_MODULE,
933 }, {
934 .name = "CHARGER_CV",
935 .id = MAX8997_CHARGER_CV,
936 .ops = &max8997_fixedstate_ops,
937 .type = REGULATOR_VOLTAGE,
938 .owner = THIS_MODULE,
939 }, {
940 .name = "CHARGER",
941 .id = MAX8997_CHARGER,
942 .ops = &max8997_charger_ops,
943 .type = REGULATOR_CURRENT,
944 .owner = THIS_MODULE,
945 }, {
946 .name = "CHARGER_TOPOFF",
947 .id = MAX8997_CHARGER_TOPOFF,
948 .ops = &max8997_charger_fixedstate_ops,
949 .type = REGULATOR_CURRENT,
950 .owner = THIS_MODULE,
954 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
956 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
957 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
958 struct regulator_dev **rdev;
959 struct max8997_data *max8997;
960 struct i2c_client *i2c;
961 int i, ret, size;
962 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
964 if (!pdata) {
965 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
966 return -ENODEV;
969 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
970 if (!max8997)
971 return -ENOMEM;
973 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
974 max8997->rdev = kzalloc(size, GFP_KERNEL);
975 if (!max8997->rdev) {
976 kfree(max8997);
977 return -ENOMEM;
980 rdev = max8997->rdev;
981 max8997->dev = &pdev->dev;
982 max8997->iodev = iodev;
983 max8997->num_regulators = pdata->num_regulators;
984 platform_set_drvdata(pdev, max8997);
985 i2c = max8997->iodev->i2c;
987 max8997->buck125_gpioindex = pdata->buck125_default_idx;
988 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
989 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
990 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
991 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
992 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
994 for (i = 0; i < 8; i++) {
995 max8997->buck1_vol[i] = ret =
996 max8997_get_voltage_proper_val(
997 &buck1245_voltage_map_desc,
998 pdata->buck1_voltage[i],
999 pdata->buck1_voltage[i] +
1000 buck1245_voltage_map_desc.step);
1001 if (ret < 0)
1002 goto err_alloc;
1004 max8997->buck2_vol[i] = ret =
1005 max8997_get_voltage_proper_val(
1006 &buck1245_voltage_map_desc,
1007 pdata->buck2_voltage[i],
1008 pdata->buck2_voltage[i] +
1009 buck1245_voltage_map_desc.step);
1010 if (ret < 0)
1011 goto err_alloc;
1013 max8997->buck5_vol[i] = ret =
1014 max8997_get_voltage_proper_val(
1015 &buck1245_voltage_map_desc,
1016 pdata->buck5_voltage[i],
1017 pdata->buck5_voltage[i] +
1018 buck1245_voltage_map_desc.step);
1019 if (ret < 0)
1020 goto err_alloc;
1022 if (max_buck1 < max8997->buck1_vol[i])
1023 max_buck1 = max8997->buck1_vol[i];
1024 if (max_buck2 < max8997->buck2_vol[i])
1025 max_buck2 = max8997->buck2_vol[i];
1026 if (max_buck5 < max8997->buck5_vol[i])
1027 max_buck5 = max8997->buck5_vol[i];
1030 /* For the safety, set max voltage before setting up */
1031 for (i = 0; i < 8; i++) {
1032 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1033 max_buck1, 0x3f);
1034 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1035 max_buck2, 0x3f);
1036 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1037 max_buck5, 0x3f);
1041 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1042 * If at least one of them cares, set gpios.
1044 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1045 pdata->buck5_gpiodvs) {
1046 bool gpio1set = false, gpio2set = false;
1048 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1049 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1050 !gpio_is_valid(pdata->buck125_gpios[2])) {
1051 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1052 ret = -EINVAL;
1053 goto err_alloc;
1056 ret = gpio_request(pdata->buck125_gpios[0],
1057 "MAX8997 SET1");
1058 if (ret == -EBUSY)
1059 dev_warn(&pdev->dev, "Duplicated gpio request"
1060 " on SET1\n");
1061 else if (ret)
1062 goto err_alloc;
1063 else
1064 gpio1set = true;
1066 ret = gpio_request(pdata->buck125_gpios[1],
1067 "MAX8997 SET2");
1068 if (ret == -EBUSY)
1069 dev_warn(&pdev->dev, "Duplicated gpio request"
1070 " on SET2\n");
1071 else if (ret) {
1072 if (gpio1set)
1073 gpio_free(pdata->buck125_gpios[0]);
1074 goto err_alloc;
1075 } else
1076 gpio2set = true;
1078 ret = gpio_request(pdata->buck125_gpios[2],
1079 "MAX8997 SET3");
1080 if (ret == -EBUSY)
1081 dev_warn(&pdev->dev, "Duplicated gpio request"
1082 " on SET3\n");
1083 else if (ret) {
1084 if (gpio1set)
1085 gpio_free(pdata->buck125_gpios[0]);
1086 if (gpio2set)
1087 gpio_free(pdata->buck125_gpios[1]);
1088 goto err_alloc;
1091 gpio_direction_output(pdata->buck125_gpios[0],
1092 (max8997->buck125_gpioindex >> 2)
1093 & 0x1); /* SET1 */
1094 gpio_direction_output(pdata->buck125_gpios[1],
1095 (max8997->buck125_gpioindex >> 1)
1096 & 0x1); /* SET2 */
1097 gpio_direction_output(pdata->buck125_gpios[2],
1098 (max8997->buck125_gpioindex >> 0)
1099 & 0x1); /* SET3 */
1100 ret = 0;
1103 /* DVS-GPIO disabled */
1104 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1105 (1 << 1) : (0 << 1), 1 << 1);
1106 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1107 (1 << 1) : (0 << 1), 1 << 1);
1108 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1109 (1 << 1) : (0 << 1), 1 << 1);
1111 /* Initialize all the DVS related BUCK registers */
1112 for (i = 0; i < 8; i++) {
1113 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1114 max8997->buck1_vol[i],
1115 0x3f);
1116 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1117 max8997->buck2_vol[i],
1118 0x3f);
1119 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1120 max8997->buck5_vol[i],
1121 0x3f);
1124 /* Misc Settings */
1125 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1126 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1128 for (i = 0; i < pdata->num_regulators; i++) {
1129 const struct voltage_map_desc *desc;
1130 int id = pdata->regulators[i].id;
1132 desc = reg_voltage_map[id];
1133 if (desc)
1134 regulators[id].n_voltages =
1135 (desc->max - desc->min) / desc->step + 1;
1136 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1137 regulators[id].n_voltages = 4;
1138 else if (id == MAX8997_CHARGER_CV)
1139 regulators[id].n_voltages = 16;
1141 rdev[i] = regulator_register(&regulators[id], max8997->dev,
1142 pdata->regulators[i].initdata, max8997, NULL);
1143 if (IS_ERR(rdev[i])) {
1144 ret = PTR_ERR(rdev[i]);
1145 dev_err(max8997->dev, "regulator init failed for %d\n",
1146 id);
1147 rdev[i] = NULL;
1148 goto err;
1152 return 0;
1153 err:
1154 for (i = 0; i < max8997->num_regulators; i++)
1155 if (rdev[i])
1156 regulator_unregister(rdev[i]);
1157 err_alloc:
1158 kfree(max8997->rdev);
1159 kfree(max8997);
1161 return ret;
1164 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1166 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1167 struct regulator_dev **rdev = max8997->rdev;
1168 int i;
1170 for (i = 0; i < max8997->num_regulators; i++)
1171 if (rdev[i])
1172 regulator_unregister(rdev[i]);
1174 kfree(max8997->rdev);
1175 kfree(max8997);
1177 return 0;
1180 static const struct platform_device_id max8997_pmic_id[] = {
1181 { "max8997-pmic", 0},
1182 { },
1184 MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1186 static struct platform_driver max8997_pmic_driver = {
1187 .driver = {
1188 .name = "max8997-pmic",
1189 .owner = THIS_MODULE,
1191 .probe = max8997_pmic_probe,
1192 .remove = __devexit_p(max8997_pmic_remove),
1193 .id_table = max8997_pmic_id,
1196 static int __init max8997_pmic_init(void)
1198 return platform_driver_register(&max8997_pmic_driver);
1200 subsys_initcall(max8997_pmic_init);
1202 static void __exit max8997_pmic_cleanup(void)
1204 platform_driver_unregister(&max8997_pmic_driver);
1206 module_exit(max8997_pmic_cleanup);
1208 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1209 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1210 MODULE_LICENSE("GPL");