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