blob: 704cd49ef3758c9ac00034238838af634e0c27f1 [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>
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090025#include <linux/err.h>
26#include <linux/gpio.h>
27#include <linux/slab.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040028#include <linux/module.h>
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090029#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
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090042 bool buck1_gpiodvs;
43 bool buck2_gpiodvs;
44 bool buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090045 u8 buck1_vol[8];
46 u8 buck2_vol[8];
47 u8 buck5_vol[8];
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090048 int buck125_gpios[3];
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090049 int buck125_gpioindex;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090050 bool ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090051
52 u8 saved_states[MAX8997_REG_MAX];
53};
54
55static inline void max8997_set_gpio(struct max8997_data *max8997)
56{
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090057 int set3 = (max8997->buck125_gpioindex) & 0x1;
58 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
59 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
60
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090061 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);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090064}
65
66struct voltage_map_desc {
67 int min;
68 int max;
69 int step;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090070};
71
72/* Voltage maps in mV */
73static const struct voltage_map_desc ldo_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080074 .min = 800, .max = 3950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090075}; /* LDO1 ~ 18, 21 all */
76
77static const struct voltage_map_desc buck1245_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080078 .min = 650, .max = 2225, .step = 25,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090079}; /* Buck1, 2, 4, 5 */
80
81static const struct voltage_map_desc buck37_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080082 .min = 750, .max = 3900, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090083}; /* Buck3, 7 */
84
85/* current map in mA */
86static const struct voltage_map_desc charger_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080087 .min = 200, .max = 950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090088};
89
90static const struct voltage_map_desc topoff_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080091 .min = 50, .max = 200, .step = 10,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090092};
93
94static const struct voltage_map_desc *reg_voltage_map[] = {
95 [MAX8997_LDO1] = &ldo_voltage_map_desc,
96 [MAX8997_LDO2] = &ldo_voltage_map_desc,
97 [MAX8997_LDO3] = &ldo_voltage_map_desc,
98 [MAX8997_LDO4] = &ldo_voltage_map_desc,
99 [MAX8997_LDO5] = &ldo_voltage_map_desc,
100 [MAX8997_LDO6] = &ldo_voltage_map_desc,
101 [MAX8997_LDO7] = &ldo_voltage_map_desc,
102 [MAX8997_LDO8] = &ldo_voltage_map_desc,
103 [MAX8997_LDO9] = &ldo_voltage_map_desc,
104 [MAX8997_LDO10] = &ldo_voltage_map_desc,
105 [MAX8997_LDO11] = &ldo_voltage_map_desc,
106 [MAX8997_LDO12] = &ldo_voltage_map_desc,
107 [MAX8997_LDO13] = &ldo_voltage_map_desc,
108 [MAX8997_LDO14] = &ldo_voltage_map_desc,
109 [MAX8997_LDO15] = &ldo_voltage_map_desc,
110 [MAX8997_LDO16] = &ldo_voltage_map_desc,
111 [MAX8997_LDO17] = &ldo_voltage_map_desc,
112 [MAX8997_LDO18] = &ldo_voltage_map_desc,
113 [MAX8997_LDO21] = &ldo_voltage_map_desc,
114 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
115 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
116 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
117 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
118 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
119 [MAX8997_BUCK6] = NULL,
120 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
121 [MAX8997_EN32KHZ_AP] = NULL,
122 [MAX8997_EN32KHZ_CP] = NULL,
123 [MAX8997_ENVICHG] = NULL,
124 [MAX8997_ESAFEOUT1] = NULL,
125 [MAX8997_ESAFEOUT2] = NULL,
126 [MAX8997_CHARGER_CV] = NULL,
127 [MAX8997_CHARGER] = &charger_current_map_desc,
128 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
129};
130
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900131static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
132 unsigned int selector)
133{
Axel Linb3e13482012-03-01 09:26:27 +0800134 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900135
136 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
137 switch (selector) {
138 case 0:
139 return 4850000;
140 case 1:
141 return 4900000;
142 case 2:
143 return 4950000;
144 case 3:
145 return 3300000;
146 default:
147 return -EINVAL;
148 }
149 }
150
151 return -EINVAL;
152}
153
154static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
155 unsigned int selector)
156{
Axel Linb3e13482012-03-01 09:26:27 +0800157 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900158
159 if (rid != MAX8997_CHARGER_CV)
160 goto err;
161
162 switch (selector) {
163 case 0x00:
164 return 4200000;
165 case 0x01 ... 0x0E:
166 return 4000000 + 20000 * (selector - 0x01);
167 case 0x0F:
168 return 4350000;
169 default:
170 return -EINVAL;
171 }
172err:
173 return -EINVAL;
174}
175
176static int max8997_list_voltage(struct regulator_dev *rdev,
177 unsigned int selector)
178{
179 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800180 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900181 int val;
182
183 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
184 rid < 0)
185 return -EINVAL;
186
187 desc = reg_voltage_map[rid];
188 if (desc == NULL)
189 return -EINVAL;
190
191 val = desc->min + desc->step * selector;
192 if (val > desc->max)
193 return -EINVAL;
194
195 return val * 1000;
196}
197
198static int max8997_get_enable_register(struct regulator_dev *rdev,
199 int *reg, int *mask, int *pattern)
200{
Axel Linb3e13482012-03-01 09:26:27 +0800201 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900202
203 switch (rid) {
204 case MAX8997_LDO1 ... MAX8997_LDO21:
205 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
206 *mask = 0xC0;
207 *pattern = 0xC0;
208 break;
209 case MAX8997_BUCK1:
210 *reg = MAX8997_REG_BUCK1CTRL;
211 *mask = 0x01;
212 *pattern = 0x01;
213 break;
214 case MAX8997_BUCK2:
215 *reg = MAX8997_REG_BUCK2CTRL;
216 *mask = 0x01;
217 *pattern = 0x01;
218 break;
219 case MAX8997_BUCK3:
220 *reg = MAX8997_REG_BUCK3CTRL;
221 *mask = 0x01;
222 *pattern = 0x01;
223 break;
224 case MAX8997_BUCK4:
225 *reg = MAX8997_REG_BUCK4CTRL;
226 *mask = 0x01;
227 *pattern = 0x01;
228 break;
229 case MAX8997_BUCK5:
230 *reg = MAX8997_REG_BUCK5CTRL;
231 *mask = 0x01;
232 *pattern = 0x01;
233 break;
234 case MAX8997_BUCK6:
235 *reg = MAX8997_REG_BUCK6CTRL;
236 *mask = 0x01;
237 *pattern = 0x01;
238 break;
239 case MAX8997_BUCK7:
240 *reg = MAX8997_REG_BUCK7CTRL;
241 *mask = 0x01;
242 *pattern = 0x01;
243 break;
244 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
245 *reg = MAX8997_REG_MAINCON1;
246 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
247 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
248 break;
249 case MAX8997_ENVICHG:
250 *reg = MAX8997_REG_MBCCTRL1;
251 *mask = 0x80;
252 *pattern = 0x80;
253 break;
254 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
255 *reg = MAX8997_REG_SAFEOUTCTRL;
256 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
257 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
258 break;
259 case MAX8997_CHARGER:
260 *reg = MAX8997_REG_MBCCTRL2;
261 *mask = 0x40;
262 *pattern = 0x40;
263 break;
264 default:
265 /* Not controllable or not exists */
266 return -EINVAL;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900267 }
268
269 return 0;
270}
271
272static int max8997_reg_is_enabled(struct regulator_dev *rdev)
273{
274 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
275 struct i2c_client *i2c = max8997->iodev->i2c;
276 int ret, reg, mask, pattern;
277 u8 val;
278
279 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
Axel Linc245c082012-04-17 23:48:27 +0800280 if (ret)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900281 return ret;
282
283 ret = max8997_read_reg(i2c, reg, &val);
284 if (ret)
285 return ret;
286
287 return (val & mask) == pattern;
288}
289
290static int max8997_reg_enable(struct regulator_dev *rdev)
291{
292 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
293 struct i2c_client *i2c = max8997->iodev->i2c;
294 int ret, reg, mask, pattern;
295
296 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
297 if (ret)
298 return ret;
299
300 return max8997_update_reg(i2c, reg, pattern, mask);
301}
302
303static int max8997_reg_disable(struct regulator_dev *rdev)
304{
305 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
306 struct i2c_client *i2c = max8997->iodev->i2c;
307 int ret, reg, mask, pattern;
308
309 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
310 if (ret)
311 return ret;
312
313 return max8997_update_reg(i2c, reg, ~pattern, mask);
314}
315
316static int max8997_get_voltage_register(struct regulator_dev *rdev,
317 int *_reg, int *_shift, int *_mask)
318{
Axel Lin6f43c382012-03-13 11:22:41 +0800319 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800320 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900321 int reg, shift = 0, mask = 0x3f;
322
323 switch (rid) {
324 case MAX8997_LDO1 ... MAX8997_LDO21:
325 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
326 break;
327 case MAX8997_BUCK1:
328 reg = MAX8997_REG_BUCK1DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800329 if (max8997->buck1_gpiodvs)
330 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900331 break;
332 case MAX8997_BUCK2:
333 reg = MAX8997_REG_BUCK2DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800334 if (max8997->buck2_gpiodvs)
335 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900336 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;
Axel Lin6f43c382012-03-13 11:22:41 +0800345 if (max8997->buck5_gpiodvs)
346 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900347 break;
348 case MAX8997_BUCK7:
349 reg = MAX8997_REG_BUCK7DVS;
350 break;
351 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
352 reg = MAX8997_REG_SAFEOUTCTRL;
353 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
354 mask = 0x3;
355 break;
356 case MAX8997_CHARGER_CV:
357 reg = MAX8997_REG_MBCCTRL3;
358 shift = 0;
359 mask = 0xf;
360 break;
361 case MAX8997_CHARGER:
362 reg = MAX8997_REG_MBCCTRL4;
363 shift = 0;
364 mask = 0xf;
365 break;
366 case MAX8997_CHARGER_TOPOFF:
367 reg = MAX8997_REG_MBCCTRL5;
368 shift = 0;
369 mask = 0xf;
370 break;
371 default:
372 return -EINVAL;
373 }
374
375 *_reg = reg;
376 *_shift = shift;
377 *_mask = mask;
378
379 return 0;
380}
381
Axel Lin9e96b3a2012-04-11 22:56:11 +0800382static int max8997_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900383{
384 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900385 struct i2c_client *i2c = max8997->iodev->i2c;
386 int reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900387 u8 val;
388
389 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
390 if (ret)
391 return ret;
392
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900393 ret = max8997_read_reg(i2c, reg, &val);
394 if (ret)
395 return ret;
396
397 val >>= shift;
398 val &= mask;
399
Axel Lin9e96b3a2012-04-11 22:56:11 +0800400 return val;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900401}
402
403static inline int max8997_get_voltage_proper_val(
404 const struct voltage_map_desc *desc,
405 int min_vol, int max_vol)
406{
Axel Lin2358b772012-04-10 14:20:03 +0800407 int i;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900408
409 if (desc == NULL)
410 return -EINVAL;
411
412 if (max_vol < desc->min || min_vol > desc->max)
413 return -EINVAL;
414
Axel Lin2358b772012-04-10 14:20:03 +0800415 if (min_vol < desc->min)
416 min_vol = desc->min;
417
418 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900419
420 if (desc->min + desc->step * i > max_vol)
421 return -EINVAL;
422
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900423 return i;
424}
425
426static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
427 int min_uV, int max_uV, unsigned *selector)
428{
429 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
430 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800431 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900432 int lb, ub;
433 int reg, shift = 0, mask, ret = 0;
434 u8 val = 0x0;
435
436 if (rid != MAX8997_CHARGER_CV)
437 return -EINVAL;
438
439 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
440 if (ret)
441 return ret;
442
443 if (max_uV < 4000000 || min_uV > 4350000)
444 return -EINVAL;
445
446 if (min_uV <= 4000000) {
447 if (max_uV >= 4000000)
448 return -EINVAL;
449 else
450 val = 0x1;
451 } else if (min_uV <= 4200000 && max_uV >= 4200000)
452 val = 0x0;
453 else {
454 lb = (min_uV - 4000001) / 20000 + 2;
455 ub = (max_uV - 4000000) / 20000 + 1;
456
457 if (lb > ub)
458 return -EINVAL;
459
460 if (lb < 0xf)
461 val = lb;
462 else {
463 if (ub >= 0xf)
464 val = 0xf;
465 else
466 return -EINVAL;
467 }
468 }
469
470 *selector = val;
471
472 ret = max8997_update_reg(i2c, reg, val << shift, mask);
473
474 return ret;
475}
476
477/*
478 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
479 * BUCK1, 2, and 5 are available if they are not controlled by gpio
480 */
481static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
482 int min_uV, int max_uV, unsigned *selector)
483{
484 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
485 struct i2c_client *i2c = max8997->iodev->i2c;
486 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
487 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800488 int rid = rdev_get_id(rdev);
Axel Lin62bc4d42012-04-11 22:58:03 +0800489 int i, reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900490
491 switch (rid) {
492 case MAX8997_LDO1 ... MAX8997_LDO21:
493 break;
494 case MAX8997_BUCK1 ... MAX8997_BUCK5:
495 break;
496 case MAX8997_BUCK6:
497 return -EINVAL;
498 case MAX8997_BUCK7:
499 break;
500 case MAX8997_CHARGER:
501 break;
502 case MAX8997_CHARGER_TOPOFF:
503 break;
504 default:
505 return -EINVAL;
506 }
507
508 desc = reg_voltage_map[rid];
509
510 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
511 if (i < 0)
512 return i;
513
514 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
515 if (ret)
516 return ret;
517
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900518 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
519 *selector = i;
520
Axel Lin62bc4d42012-04-11 22:58:03 +0800521 return ret;
522}
523
524static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
525 unsigned int old_selector,
526 unsigned int new_selector)
527{
528 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
529 int rid = rdev_get_id(rdev);
530 const struct voltage_map_desc *desc = reg_voltage_map[rid];
531
532 /* Delay is required only if the voltage is increasing */
533 if (old_selector >= new_selector)
534 return 0;
535
536 /* No need to delay if gpio_dvs_mode */
537 switch (rid) {
538 case MAX8997_BUCK1:
539 if (max8997->buck1_gpiodvs)
540 return 0;
541 break;
542 case MAX8997_BUCK2:
543 if (max8997->buck2_gpiodvs)
544 return 0;
545 break;
546 case MAX8997_BUCK5:
547 if (max8997->buck5_gpiodvs)
548 return 0;
549 break;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900550 }
551
Axel Lin62bc4d42012-04-11 22:58:03 +0800552 switch (rid) {
553 case MAX8997_BUCK1:
554 case MAX8997_BUCK2:
555 case MAX8997_BUCK4:
556 case MAX8997_BUCK5:
557 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
558 max8997->ramp_delay);
559 }
560
561 return 0;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900562}
563
564/*
565 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
566 *
567 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
568 * of one of the bucks may affect that of another buck, which is the side
569 * effect of the change (set_voltage). This function examines the GPIO-DVS
570 * configurations and checks whether such side-effect exists.
571 */
572static int max8997_assess_side_effect(struct regulator_dev *rdev,
573 u8 new_val, int *best)
574{
575 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800576 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900577 u8 *buckx_val[3];
578 bool buckx_gpiodvs[3];
579 int side_effect[8];
580 int min_side_effect = INT_MAX;
581 int i;
582
583 *best = -1;
584
585 switch (rid) {
586 case MAX8997_BUCK1:
587 rid = 0;
588 break;
589 case MAX8997_BUCK2:
590 rid = 1;
591 break;
592 case MAX8997_BUCK5:
593 rid = 2;
594 break;
595 default:
596 return -EINVAL;
597 }
598
599 buckx_val[0] = max8997->buck1_vol;
600 buckx_val[1] = max8997->buck2_vol;
601 buckx_val[2] = max8997->buck5_vol;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900602 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
603 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
604 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900605
606 for (i = 0; i < 8; i++) {
607 int others;
608
609 if (new_val != (buckx_val[rid])[i]) {
610 side_effect[i] = -1;
611 continue;
612 }
613
614 side_effect[i] = 0;
615 for (others = 0; others < 3; others++) {
616 int diff;
617
618 if (others == rid)
619 continue;
620 if (buckx_gpiodvs[others] == false)
621 continue; /* Not affected */
622 diff = (buckx_val[others])[i] -
623 (buckx_val[others])[max8997->buck125_gpioindex];
624 if (diff > 0)
625 side_effect[i] += diff;
626 else if (diff < 0)
627 side_effect[i] -= diff;
628 }
629 if (side_effect[i] == 0) {
630 *best = i;
631 return 0; /* NO SIDE EFFECT! Use This! */
632 }
633 if (side_effect[i] < min_side_effect) {
634 min_side_effect = side_effect[i];
635 *best = i;
636 }
637 }
638
639 if (*best == -1)
640 return -EINVAL;
641
642 return side_effect[*best];
643}
644
645/*
646 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
647 * max8997_set_voltage_ldobuck to do the job.
648 */
649static int max8997_set_voltage_buck(struct regulator_dev *rdev,
650 int min_uV, int max_uV, unsigned *selector)
651{
652 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800653 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900654 const struct voltage_map_desc *desc;
655 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
656 bool gpio_dvs_mode = false;
657 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
658
659 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
660 return -EINVAL;
661
662 switch (rid) {
663 case MAX8997_BUCK1:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900664 if (max8997->buck1_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900665 gpio_dvs_mode = true;
666 break;
667 case MAX8997_BUCK2:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900668 if (max8997->buck2_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900669 gpio_dvs_mode = true;
670 break;
671 case MAX8997_BUCK5:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900672 if (max8997->buck5_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900673 gpio_dvs_mode = true;
674 break;
675 }
676
677 if (!gpio_dvs_mode)
678 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
679 selector);
680
681 desc = reg_voltage_map[rid];
682 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
683 if (new_val < 0)
684 return new_val;
685
686 tmp_dmg = INT_MAX;
687 tmp_idx = -1;
688 tmp_val = -1;
689 do {
690 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
691 if (damage == 0)
692 goto out;
693
694 if (tmp_dmg > damage) {
695 tmp_idx = new_idx;
696 tmp_val = new_val;
697 tmp_dmg = damage;
698 }
699
700 new_val++;
Axel Linf55205f2012-04-11 20:53:58 +0800701 } while (desc->min + desc->step * new_val <= desc->max);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900702
703 new_idx = tmp_idx;
704 new_val = tmp_val;
705
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900706 if (max8997->ignore_gpiodvs_side_effect == false)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900707 return -EINVAL;
708
709 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
710 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
711
712out:
713 if (new_idx < 0 || new_val < 0)
714 return -EINVAL;
715
716 max8997->buck125_gpioindex = new_idx;
717 max8997_set_gpio(max8997);
718 *selector = new_val;
719
720 return 0;
721}
722
723static const int safeoutvolt[] = {
724 3300000,
725 4850000,
726 4900000,
727 4950000,
728};
729
730/* For SAFEOUT1 and SAFEOUT2 */
731static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
732 int min_uV, int max_uV, unsigned *selector)
733{
734 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
735 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800736 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900737 int reg, shift = 0, mask, ret;
738 int i = 0;
739 u8 val;
740
741 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
742 return -EINVAL;
743
744 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
745 if (min_uV <= safeoutvolt[i] &&
746 max_uV >= safeoutvolt[i])
747 break;
748 }
749
750 if (i >= ARRAY_SIZE(safeoutvolt))
751 return -EINVAL;
752
753 if (i == 0)
754 val = 0x3;
755 else
756 val = i - 1;
757
758 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
759 if (ret)
760 return ret;
761
762 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
763 *selector = val;
764
765 return ret;
766}
767
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900768static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
769{
770 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
771 struct i2c_client *i2c = max8997->iodev->i2c;
772 int ret, reg, mask, pattern;
Axel Linb3e13482012-03-01 09:26:27 +0800773 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900774
775 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
776 if (ret)
777 return ret;
778
779 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
780
781 if (rid == MAX8997_LDO1 ||
782 rid == MAX8997_LDO10 ||
783 rid == MAX8997_LDO21) {
784 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
785 rdev->desc->name);
786 return max8997_update_reg(i2c, reg, 0x40, mask);
787 }
788
789 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
790 rdev->desc->name, max8997->saved_states[rid] & mask,
791 (~pattern) & mask);
792 return max8997_update_reg(i2c, reg, ~pattern, mask);
793}
794
795static struct regulator_ops max8997_ldo_ops = {
796 .list_voltage = max8997_list_voltage,
797 .is_enabled = max8997_reg_is_enabled,
798 .enable = max8997_reg_enable,
799 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800800 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900801 .set_voltage = max8997_set_voltage_ldobuck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800802 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900803 .set_suspend_disable = max8997_reg_disable_suspend,
804};
805
806static struct regulator_ops max8997_buck_ops = {
807 .list_voltage = max8997_list_voltage,
808 .is_enabled = max8997_reg_is_enabled,
809 .enable = max8997_reg_enable,
810 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800811 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900812 .set_voltage = max8997_set_voltage_buck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800813 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900814 .set_suspend_disable = max8997_reg_disable_suspend,
815};
816
817static struct regulator_ops max8997_fixedvolt_ops = {
818 .list_voltage = max8997_list_voltage,
819 .is_enabled = max8997_reg_is_enabled,
820 .enable = max8997_reg_enable,
821 .disable = max8997_reg_disable,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900822 .set_suspend_disable = max8997_reg_disable_suspend,
823};
824
825static struct regulator_ops max8997_safeout_ops = {
826 .list_voltage = max8997_list_voltage_safeout,
827 .is_enabled = max8997_reg_is_enabled,
828 .enable = max8997_reg_enable,
829 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800830 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900831 .set_voltage = max8997_set_voltage_safeout,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900832 .set_suspend_disable = max8997_reg_disable_suspend,
833};
834
835static struct regulator_ops max8997_fixedstate_ops = {
836 .list_voltage = max8997_list_voltage_charger_cv,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800837 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900838 .set_voltage = max8997_set_voltage_charger_cv,
839};
840
Axel Lin9e96b3a2012-04-11 22:56:11 +0800841static int max8997_set_current_limit(struct regulator_dev *rdev,
842 int min_uA, int max_uA)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900843{
844 unsigned dummy;
Axel Lin9e96b3a2012-04-11 22:56:11 +0800845 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900846
Axel Lin9e96b3a2012-04-11 22:56:11 +0800847 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
848 return -EINVAL;
849
850 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
851 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900852}
853
Axel Lin9e96b3a2012-04-11 22:56:11 +0800854static int max8997_get_current_limit(struct regulator_dev *rdev)
855{
856 int sel, rid = rdev_get_id(rdev);
857
858 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
859 return -EINVAL;
860
861 sel = max8997_get_voltage_sel(rdev);
862 if (sel < 0)
863 return sel;
864
865 /* Reuse max8997_list_voltage to get current_limit. */
866 return max8997_list_voltage(rdev, sel);
867}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900868
869static struct regulator_ops max8997_charger_ops = {
870 .is_enabled = max8997_reg_is_enabled,
871 .enable = max8997_reg_enable,
872 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800873 .get_current_limit = max8997_get_current_limit,
874 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900875};
876
877static struct regulator_ops max8997_charger_fixedstate_ops = {
Axel Lin9e96b3a2012-04-11 22:56:11 +0800878 .get_current_limit = max8997_get_current_limit,
879 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900880};
881
Axel Lin4533f802012-03-13 14:53:58 +0800882#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
883 .name = #_name, \
884 .id = MAX8997_##_name, \
885 .ops = &_ops, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900886 .type = REGULATOR_VOLTAGE, \
887 .owner = THIS_MODULE, \
888}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900889
Axel Lin4533f802012-03-13 14:53:58 +0800890#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
891 .name = #_name, \
892 .id = MAX8997_##_name, \
893 .ops = &_ops, \
894 .type = REGULATOR_CURRENT, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900895 .owner = THIS_MODULE, \
896}
897
898static struct regulator_desc regulators[] = {
Axel Lin4533f802012-03-13 14:53:58 +0800899 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
900 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
901 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
902 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
903 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
904 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
905 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
906 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
907 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
908 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
909 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
910 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
911 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
912 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
913 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
914 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
915 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
916 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
917 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
918 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
919 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
920 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
921 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
922 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
923 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
924 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
925 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
926 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
927 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
928 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
929 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
930 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
931 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
932 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
933 max8997_charger_fixedstate_ops),
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900934};
935
936static __devinit int max8997_pmic_probe(struct platform_device *pdev)
937{
938 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
939 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100940 struct regulator_config config = { };
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900941 struct regulator_dev **rdev;
942 struct max8997_data *max8997;
943 struct i2c_client *i2c;
944 int i, ret, size;
945 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
946
947 if (!pdata) {
948 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
949 return -ENODEV;
950 }
951
Axel Lin8ae57672012-04-19 09:44:13 +0800952 max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
953 GFP_KERNEL);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900954 if (!max8997)
955 return -ENOMEM;
956
957 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Axel Lin8ae57672012-04-19 09:44:13 +0800958 max8997->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
959 if (!max8997->rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900960 return -ENOMEM;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900961
962 rdev = max8997->rdev;
963 max8997->dev = &pdev->dev;
964 max8997->iodev = iodev;
965 max8997->num_regulators = pdata->num_regulators;
966 platform_set_drvdata(pdev, max8997);
967 i2c = max8997->iodev->i2c;
968
969 max8997->buck125_gpioindex = pdata->buck125_default_idx;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900970 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
971 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
972 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
973 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
974 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900975
976 for (i = 0; i < 8; i++) {
977 max8997->buck1_vol[i] = ret =
978 max8997_get_voltage_proper_val(
979 &buck1245_voltage_map_desc,
980 pdata->buck1_voltage[i] / 1000,
981 pdata->buck1_voltage[i] / 1000 +
982 buck1245_voltage_map_desc.step);
983 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +0800984 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900985
986 max8997->buck2_vol[i] = ret =
987 max8997_get_voltage_proper_val(
988 &buck1245_voltage_map_desc,
989 pdata->buck2_voltage[i] / 1000,
990 pdata->buck2_voltage[i] / 1000 +
991 buck1245_voltage_map_desc.step);
992 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +0800993 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900994
995 max8997->buck5_vol[i] = ret =
996 max8997_get_voltage_proper_val(
997 &buck1245_voltage_map_desc,
998 pdata->buck5_voltage[i] / 1000,
999 pdata->buck5_voltage[i] / 1000 +
1000 buck1245_voltage_map_desc.step);
1001 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +08001002 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001003
1004 if (max_buck1 < max8997->buck1_vol[i])
1005 max_buck1 = max8997->buck1_vol[i];
1006 if (max_buck2 < max8997->buck2_vol[i])
1007 max_buck2 = max8997->buck2_vol[i];
1008 if (max_buck5 < max8997->buck5_vol[i])
1009 max_buck5 = max8997->buck5_vol[i];
1010 }
1011
1012 /* For the safety, set max voltage before setting up */
1013 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001014 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001015 max_buck1, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001016 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001017 max_buck2, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001018 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001019 max_buck5, 0x3f);
1020 }
1021
1022 /*
1023 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1024 * If at least one of them cares, set gpios.
1025 */
1026 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1027 pdata->buck5_gpiodvs) {
1028 bool gpio1set = false, gpio2set = false;
1029
1030 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1031 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1032 !gpio_is_valid(pdata->buck125_gpios[2])) {
1033 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1034 ret = -EINVAL;
Axel Lin8ae57672012-04-19 09:44:13 +08001035 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001036 }
1037
1038 ret = gpio_request(pdata->buck125_gpios[0],
1039 "MAX8997 SET1");
1040 if (ret == -EBUSY)
1041 dev_warn(&pdev->dev, "Duplicated gpio request"
1042 " on SET1\n");
1043 else if (ret)
Axel Lin8ae57672012-04-19 09:44:13 +08001044 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001045 else
1046 gpio1set = true;
1047
1048 ret = gpio_request(pdata->buck125_gpios[1],
1049 "MAX8997 SET2");
1050 if (ret == -EBUSY)
1051 dev_warn(&pdev->dev, "Duplicated gpio request"
1052 " on SET2\n");
1053 else if (ret) {
1054 if (gpio1set)
1055 gpio_free(pdata->buck125_gpios[0]);
Axel Lin8ae57672012-04-19 09:44:13 +08001056 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001057 } else
1058 gpio2set = true;
1059
1060 ret = gpio_request(pdata->buck125_gpios[2],
1061 "MAX8997 SET3");
1062 if (ret == -EBUSY)
1063 dev_warn(&pdev->dev, "Duplicated gpio request"
1064 " on SET3\n");
1065 else if (ret) {
1066 if (gpio1set)
1067 gpio_free(pdata->buck125_gpios[0]);
1068 if (gpio2set)
1069 gpio_free(pdata->buck125_gpios[1]);
Axel Lin8ae57672012-04-19 09:44:13 +08001070 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001071 }
1072
1073 gpio_direction_output(pdata->buck125_gpios[0],
1074 (max8997->buck125_gpioindex >> 2)
1075 & 0x1); /* SET1 */
1076 gpio_direction_output(pdata->buck125_gpios[1],
1077 (max8997->buck125_gpioindex >> 1)
1078 & 0x1); /* SET2 */
1079 gpio_direction_output(pdata->buck125_gpios[2],
1080 (max8997->buck125_gpioindex >> 0)
1081 & 0x1); /* SET3 */
1082 ret = 0;
1083 }
1084
1085 /* DVS-GPIO disabled */
1086 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1087 (1 << 1) : (0 << 1), 1 << 1);
1088 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1089 (1 << 1) : (0 << 1), 1 << 1);
1090 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1091 (1 << 1) : (0 << 1), 1 << 1);
1092
1093 /* Initialize all the DVS related BUCK registers */
1094 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001095 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001096 max8997->buck1_vol[i],
1097 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001098 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001099 max8997->buck2_vol[i],
1100 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001101 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001102 max8997->buck5_vol[i],
1103 0x3f);
1104 }
1105
Tushar Beheradbb48e72011-06-21 09:08:59 +05301106 /* Misc Settings */
1107 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1108 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1109
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001110 for (i = 0; i < pdata->num_regulators; i++) {
1111 const struct voltage_map_desc *desc;
1112 int id = pdata->regulators[i].id;
1113
1114 desc = reg_voltage_map[id];
1115 if (desc)
1116 regulators[id].n_voltages =
1117 (desc->max - desc->min) / desc->step + 1;
1118 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1119 regulators[id].n_voltages = 4;
1120 else if (id == MAX8997_CHARGER_CV)
1121 regulators[id].n_voltages = 16;
1122
Mark Brownc1727082012-04-04 00:50:22 +01001123 config.dev = max8997->dev;
1124 config.init_data = pdata->regulators[i].initdata;
1125 config.driver_data = max8997;
1126
1127 rdev[i] = regulator_register(&regulators[id], &config);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001128 if (IS_ERR(rdev[i])) {
1129 ret = PTR_ERR(rdev[i]);
1130 dev_err(max8997->dev, "regulator init failed for %d\n",
1131 id);
1132 rdev[i] = NULL;
1133 goto err;
1134 }
1135 }
1136
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001137 return 0;
1138err:
Axel Lin8ae57672012-04-19 09:44:13 +08001139 while (--i >= 0)
1140 regulator_unregister(rdev[i]);
1141err_out:
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001142 return ret;
1143}
1144
1145static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1146{
1147 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1148 struct regulator_dev **rdev = max8997->rdev;
1149 int i;
1150
1151 for (i = 0; i < max8997->num_regulators; i++)
Axel Lin8ae57672012-04-19 09:44:13 +08001152 regulator_unregister(rdev[i]);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001153 return 0;
1154}
1155
1156static const struct platform_device_id max8997_pmic_id[] = {
1157 { "max8997-pmic", 0},
1158 { },
1159};
Axel Lina51b9072011-03-26 23:28:42 +08001160MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001161
1162static struct platform_driver max8997_pmic_driver = {
1163 .driver = {
1164 .name = "max8997-pmic",
1165 .owner = THIS_MODULE,
1166 },
1167 .probe = max8997_pmic_probe,
1168 .remove = __devexit_p(max8997_pmic_remove),
1169 .id_table = max8997_pmic_id,
1170};
1171
1172static int __init max8997_pmic_init(void)
1173{
1174 return platform_driver_register(&max8997_pmic_driver);
1175}
1176subsys_initcall(max8997_pmic_init);
1177
1178static void __exit max8997_pmic_cleanup(void)
1179{
1180 platform_driver_unregister(&max8997_pmic_driver);
1181}
1182module_exit(max8997_pmic_cleanup);
1183
1184MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1185MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1186MODULE_LICENSE("GPL");