blob: d106b74fc821a604e52f501fc1bb3af4e4d292af [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>
Paul Gortmaker65602c32011-07-17 16:28:23 -040029#include <linux/module.h>
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090030#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>
35
36struct 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 */
42
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090043 bool buck1_gpiodvs;
44 bool buck2_gpiodvs;
45 bool buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090046 u8 buck1_vol[8];
47 u8 buck2_vol[8];
48 u8 buck5_vol[8];
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090049 int buck125_gpios[3];
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090050 int buck125_gpioindex;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090051 bool ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090052
53 u8 saved_states[MAX8997_REG_MAX];
54};
55
56static inline void max8997_set_gpio(struct max8997_data *max8997)
57{
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090058 int set3 = (max8997->buck125_gpioindex) & 0x1;
59 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
60 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
61
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090062 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);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090065}
66
67struct voltage_map_desc {
68 int min;
69 int max;
70 int step;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090071};
72
73/* Voltage maps in mV */
74static const struct voltage_map_desc ldo_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080075 .min = 800, .max = 3950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090076}; /* LDO1 ~ 18, 21 all */
77
78static const struct voltage_map_desc buck1245_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080079 .min = 650, .max = 2225, .step = 25,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090080}; /* Buck1, 2, 4, 5 */
81
82static const struct voltage_map_desc buck37_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080083 .min = 750, .max = 3900, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090084}; /* Buck3, 7 */
85
86/* current map in mA */
87static const struct voltage_map_desc charger_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080088 .min = 200, .max = 950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090089};
90
91static const struct voltage_map_desc topoff_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080092 .min = 50, .max = 200, .step = 10,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090093};
94
95static const struct voltage_map_desc *reg_voltage_map[] = {
96 [MAX8997_LDO1] = &ldo_voltage_map_desc,
97 [MAX8997_LDO2] = &ldo_voltage_map_desc,
98 [MAX8997_LDO3] = &ldo_voltage_map_desc,
99 [MAX8997_LDO4] = &ldo_voltage_map_desc,
100 [MAX8997_LDO5] = &ldo_voltage_map_desc,
101 [MAX8997_LDO6] = &ldo_voltage_map_desc,
102 [MAX8997_LDO7] = &ldo_voltage_map_desc,
103 [MAX8997_LDO8] = &ldo_voltage_map_desc,
104 [MAX8997_LDO9] = &ldo_voltage_map_desc,
105 [MAX8997_LDO10] = &ldo_voltage_map_desc,
106 [MAX8997_LDO11] = &ldo_voltage_map_desc,
107 [MAX8997_LDO12] = &ldo_voltage_map_desc,
108 [MAX8997_LDO13] = &ldo_voltage_map_desc,
109 [MAX8997_LDO14] = &ldo_voltage_map_desc,
110 [MAX8997_LDO15] = &ldo_voltage_map_desc,
111 [MAX8997_LDO16] = &ldo_voltage_map_desc,
112 [MAX8997_LDO17] = &ldo_voltage_map_desc,
113 [MAX8997_LDO18] = &ldo_voltage_map_desc,
114 [MAX8997_LDO21] = &ldo_voltage_map_desc,
115 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
116 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
117 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
118 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
119 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
120 [MAX8997_BUCK6] = NULL,
121 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
122 [MAX8997_EN32KHZ_AP] = NULL,
123 [MAX8997_EN32KHZ_CP] = NULL,
124 [MAX8997_ENVICHG] = NULL,
125 [MAX8997_ESAFEOUT1] = NULL,
126 [MAX8997_ESAFEOUT2] = NULL,
127 [MAX8997_CHARGER_CV] = NULL,
128 [MAX8997_CHARGER] = &charger_current_map_desc,
129 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
130};
131
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900132static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
133 unsigned int selector)
134{
Axel Linb3e13482012-03-01 09:26:27 +0800135 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900136
137 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
138 switch (selector) {
139 case 0:
140 return 4850000;
141 case 1:
142 return 4900000;
143 case 2:
144 return 4950000;
145 case 3:
146 return 3300000;
147 default:
148 return -EINVAL;
149 }
150 }
151
152 return -EINVAL;
153}
154
155static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
156 unsigned int selector)
157{
Axel Linb3e13482012-03-01 09:26:27 +0800158 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900159
160 if (rid != MAX8997_CHARGER_CV)
161 goto err;
162
163 switch (selector) {
164 case 0x00:
165 return 4200000;
166 case 0x01 ... 0x0E:
167 return 4000000 + 20000 * (selector - 0x01);
168 case 0x0F:
169 return 4350000;
170 default:
171 return -EINVAL;
172 }
173err:
174 return -EINVAL;
175}
176
177static int max8997_list_voltage(struct regulator_dev *rdev,
178 unsigned int selector)
179{
180 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800181 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900182 int val;
183
184 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
185 rid < 0)
186 return -EINVAL;
187
188 desc = reg_voltage_map[rid];
189 if (desc == NULL)
190 return -EINVAL;
191
192 val = desc->min + desc->step * selector;
193 if (val > desc->max)
194 return -EINVAL;
195
196 return val * 1000;
197}
198
199static int max8997_get_enable_register(struct regulator_dev *rdev,
200 int *reg, int *mask, int *pattern)
201{
Axel Linb3e13482012-03-01 09:26:27 +0800202 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900203
204 switch (rid) {
205 case MAX8997_LDO1 ... MAX8997_LDO21:
206 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
207 *mask = 0xC0;
208 *pattern = 0xC0;
209 break;
210 case MAX8997_BUCK1:
211 *reg = MAX8997_REG_BUCK1CTRL;
212 *mask = 0x01;
213 *pattern = 0x01;
214 break;
215 case MAX8997_BUCK2:
216 *reg = MAX8997_REG_BUCK2CTRL;
217 *mask = 0x01;
218 *pattern = 0x01;
219 break;
220 case MAX8997_BUCK3:
221 *reg = MAX8997_REG_BUCK3CTRL;
222 *mask = 0x01;
223 *pattern = 0x01;
224 break;
225 case MAX8997_BUCK4:
226 *reg = MAX8997_REG_BUCK4CTRL;
227 *mask = 0x01;
228 *pattern = 0x01;
229 break;
230 case MAX8997_BUCK5:
231 *reg = MAX8997_REG_BUCK5CTRL;
232 *mask = 0x01;
233 *pattern = 0x01;
234 break;
235 case MAX8997_BUCK6:
236 *reg = MAX8997_REG_BUCK6CTRL;
237 *mask = 0x01;
238 *pattern = 0x01;
239 break;
240 case MAX8997_BUCK7:
241 *reg = MAX8997_REG_BUCK7CTRL;
242 *mask = 0x01;
243 *pattern = 0x01;
244 break;
245 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
246 *reg = MAX8997_REG_MAINCON1;
247 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
248 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
249 break;
250 case MAX8997_ENVICHG:
251 *reg = MAX8997_REG_MBCCTRL1;
252 *mask = 0x80;
253 *pattern = 0x80;
254 break;
255 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
256 *reg = MAX8997_REG_SAFEOUTCTRL;
257 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
258 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
259 break;
260 case MAX8997_CHARGER:
261 *reg = MAX8997_REG_MBCCTRL2;
262 *mask = 0x40;
263 *pattern = 0x40;
264 break;
265 default:
266 /* Not controllable or not exists */
267 return -EINVAL;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900268 }
269
270 return 0;
271}
272
273static int max8997_reg_is_enabled(struct regulator_dev *rdev)
274{
275 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
276 struct i2c_client *i2c = max8997->iodev->i2c;
277 int ret, reg, mask, pattern;
278 u8 val;
279
280 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
Axel Linc245c082012-04-17 23:48:27 +0800281 if (ret)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900282 return ret;
283
284 ret = max8997_read_reg(i2c, reg, &val);
285 if (ret)
286 return ret;
287
288 return (val & mask) == pattern;
289}
290
291static int max8997_reg_enable(struct regulator_dev *rdev)
292{
293 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
294 struct i2c_client *i2c = max8997->iodev->i2c;
295 int ret, reg, mask, pattern;
296
297 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
298 if (ret)
299 return ret;
300
301 return max8997_update_reg(i2c, reg, pattern, mask);
302}
303
304static int max8997_reg_disable(struct regulator_dev *rdev)
305{
306 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
307 struct i2c_client *i2c = max8997->iodev->i2c;
308 int ret, reg, mask, pattern;
309
310 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
311 if (ret)
312 return ret;
313
314 return max8997_update_reg(i2c, reg, ~pattern, mask);
315}
316
317static int max8997_get_voltage_register(struct regulator_dev *rdev,
318 int *_reg, int *_shift, int *_mask)
319{
Axel Lin6f43c382012-03-13 11:22:41 +0800320 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800321 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900322 int reg, shift = 0, mask = 0x3f;
323
324 switch (rid) {
325 case MAX8997_LDO1 ... MAX8997_LDO21:
326 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
327 break;
328 case MAX8997_BUCK1:
329 reg = MAX8997_REG_BUCK1DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800330 if (max8997->buck1_gpiodvs)
331 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900332 break;
333 case MAX8997_BUCK2:
334 reg = MAX8997_REG_BUCK2DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800335 if (max8997->buck2_gpiodvs)
336 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900337 break;
338 case MAX8997_BUCK3:
339 reg = MAX8997_REG_BUCK3DVS;
340 break;
341 case MAX8997_BUCK4:
342 reg = MAX8997_REG_BUCK4DVS;
343 break;
344 case MAX8997_BUCK5:
345 reg = MAX8997_REG_BUCK5DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800346 if (max8997->buck5_gpiodvs)
347 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900348 break;
349 case MAX8997_BUCK7:
350 reg = MAX8997_REG_BUCK7DVS;
351 break;
352 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
353 reg = MAX8997_REG_SAFEOUTCTRL;
354 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
355 mask = 0x3;
356 break;
357 case MAX8997_CHARGER_CV:
358 reg = MAX8997_REG_MBCCTRL3;
359 shift = 0;
360 mask = 0xf;
361 break;
362 case MAX8997_CHARGER:
363 reg = MAX8997_REG_MBCCTRL4;
364 shift = 0;
365 mask = 0xf;
366 break;
367 case MAX8997_CHARGER_TOPOFF:
368 reg = MAX8997_REG_MBCCTRL5;
369 shift = 0;
370 mask = 0xf;
371 break;
372 default:
373 return -EINVAL;
374 }
375
376 *_reg = reg;
377 *_shift = shift;
378 *_mask = mask;
379
380 return 0;
381}
382
Axel Lin9e96b3a2012-04-11 22:56:11 +0800383static int max8997_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900384{
385 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900386 struct i2c_client *i2c = max8997->iodev->i2c;
387 int reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900388 u8 val;
389
390 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
391 if (ret)
392 return ret;
393
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900394 ret = max8997_read_reg(i2c, reg, &val);
395 if (ret)
396 return ret;
397
398 val >>= shift;
399 val &= mask;
400
Axel Lin9e96b3a2012-04-11 22:56:11 +0800401 return val;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900402}
403
404static inline int max8997_get_voltage_proper_val(
405 const struct voltage_map_desc *desc,
406 int min_vol, int max_vol)
407{
Axel Lin2358b772012-04-10 14:20:03 +0800408 int i;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900409
410 if (desc == NULL)
411 return -EINVAL;
412
413 if (max_vol < desc->min || min_vol > desc->max)
414 return -EINVAL;
415
Axel Lin2358b772012-04-10 14:20:03 +0800416 if (min_vol < desc->min)
417 min_vol = desc->min;
418
419 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900420
421 if (desc->min + desc->step * i > max_vol)
422 return -EINVAL;
423
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900424 return i;
425}
426
427static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
428 int min_uV, int max_uV, unsigned *selector)
429{
430 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
431 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800432 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900433 int lb, ub;
434 int reg, shift = 0, mask, ret = 0;
435 u8 val = 0x0;
436
437 if (rid != MAX8997_CHARGER_CV)
438 return -EINVAL;
439
440 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
441 if (ret)
442 return ret;
443
444 if (max_uV < 4000000 || min_uV > 4350000)
445 return -EINVAL;
446
447 if (min_uV <= 4000000) {
448 if (max_uV >= 4000000)
449 return -EINVAL;
450 else
451 val = 0x1;
452 } else if (min_uV <= 4200000 && max_uV >= 4200000)
453 val = 0x0;
454 else {
455 lb = (min_uV - 4000001) / 20000 + 2;
456 ub = (max_uV - 4000000) / 20000 + 1;
457
458 if (lb > ub)
459 return -EINVAL;
460
461 if (lb < 0xf)
462 val = lb;
463 else {
464 if (ub >= 0xf)
465 val = 0xf;
466 else
467 return -EINVAL;
468 }
469 }
470
471 *selector = val;
472
473 ret = max8997_update_reg(i2c, reg, val << shift, mask);
474
475 return ret;
476}
477
478/*
479 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
480 * BUCK1, 2, and 5 are available if they are not controlled by gpio
481 */
482static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
483 int min_uV, int max_uV, unsigned *selector)
484{
485 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
486 struct i2c_client *i2c = max8997->iodev->i2c;
487 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
488 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800489 int rid = rdev_get_id(rdev);
Axel Lin62bc4d42012-04-11 22:58:03 +0800490 int i, reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900491
492 switch (rid) {
493 case MAX8997_LDO1 ... MAX8997_LDO21:
494 break;
495 case MAX8997_BUCK1 ... MAX8997_BUCK5:
496 break;
497 case MAX8997_BUCK6:
498 return -EINVAL;
499 case MAX8997_BUCK7:
500 break;
501 case MAX8997_CHARGER:
502 break;
503 case MAX8997_CHARGER_TOPOFF:
504 break;
505 default:
506 return -EINVAL;
507 }
508
509 desc = reg_voltage_map[rid];
510
511 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
512 if (i < 0)
513 return i;
514
515 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
516 if (ret)
517 return ret;
518
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900519 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
520 *selector = i;
521
Axel Lin62bc4d42012-04-11 22:58:03 +0800522 return ret;
523}
524
525static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
526 unsigned int old_selector,
527 unsigned int new_selector)
528{
529 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
530 int rid = rdev_get_id(rdev);
531 const struct voltage_map_desc *desc = reg_voltage_map[rid];
532
533 /* Delay is required only if the voltage is increasing */
534 if (old_selector >= new_selector)
535 return 0;
536
537 /* No need to delay if gpio_dvs_mode */
538 switch (rid) {
539 case MAX8997_BUCK1:
540 if (max8997->buck1_gpiodvs)
541 return 0;
542 break;
543 case MAX8997_BUCK2:
544 if (max8997->buck2_gpiodvs)
545 return 0;
546 break;
547 case MAX8997_BUCK5:
548 if (max8997->buck5_gpiodvs)
549 return 0;
550 break;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900551 }
552
Axel Lin62bc4d42012-04-11 22:58:03 +0800553 switch (rid) {
554 case MAX8997_BUCK1:
555 case MAX8997_BUCK2:
556 case MAX8997_BUCK4:
557 case MAX8997_BUCK5:
558 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
559 max8997->ramp_delay);
560 }
561
562 return 0;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900563}
564
565/*
566 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
567 *
568 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
569 * of one of the bucks may affect that of another buck, which is the side
570 * effect of the change (set_voltage). This function examines the GPIO-DVS
571 * configurations and checks whether such side-effect exists.
572 */
573static int max8997_assess_side_effect(struct regulator_dev *rdev,
574 u8 new_val, int *best)
575{
576 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800577 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900578 u8 *buckx_val[3];
579 bool buckx_gpiodvs[3];
580 int side_effect[8];
581 int min_side_effect = INT_MAX;
582 int i;
583
584 *best = -1;
585
586 switch (rid) {
587 case MAX8997_BUCK1:
588 rid = 0;
589 break;
590 case MAX8997_BUCK2:
591 rid = 1;
592 break;
593 case MAX8997_BUCK5:
594 rid = 2;
595 break;
596 default:
597 return -EINVAL;
598 }
599
600 buckx_val[0] = max8997->buck1_vol;
601 buckx_val[1] = max8997->buck2_vol;
602 buckx_val[2] = max8997->buck5_vol;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900603 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
604 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
605 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900606
607 for (i = 0; i < 8; i++) {
608 int others;
609
610 if (new_val != (buckx_val[rid])[i]) {
611 side_effect[i] = -1;
612 continue;
613 }
614
615 side_effect[i] = 0;
616 for (others = 0; others < 3; others++) {
617 int diff;
618
619 if (others == rid)
620 continue;
621 if (buckx_gpiodvs[others] == false)
622 continue; /* Not affected */
623 diff = (buckx_val[others])[i] -
624 (buckx_val[others])[max8997->buck125_gpioindex];
625 if (diff > 0)
626 side_effect[i] += diff;
627 else if (diff < 0)
628 side_effect[i] -= diff;
629 }
630 if (side_effect[i] == 0) {
631 *best = i;
632 return 0; /* NO SIDE EFFECT! Use This! */
633 }
634 if (side_effect[i] < min_side_effect) {
635 min_side_effect = side_effect[i];
636 *best = i;
637 }
638 }
639
640 if (*best == -1)
641 return -EINVAL;
642
643 return side_effect[*best];
644}
645
646/*
647 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
648 * max8997_set_voltage_ldobuck to do the job.
649 */
650static int max8997_set_voltage_buck(struct regulator_dev *rdev,
651 int min_uV, int max_uV, unsigned *selector)
652{
653 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800654 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900655 const struct voltage_map_desc *desc;
656 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
657 bool gpio_dvs_mode = false;
658 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
659
660 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
661 return -EINVAL;
662
663 switch (rid) {
664 case MAX8997_BUCK1:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900665 if (max8997->buck1_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900666 gpio_dvs_mode = true;
667 break;
668 case MAX8997_BUCK2:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900669 if (max8997->buck2_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900670 gpio_dvs_mode = true;
671 break;
672 case MAX8997_BUCK5:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900673 if (max8997->buck5_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900674 gpio_dvs_mode = true;
675 break;
676 }
677
678 if (!gpio_dvs_mode)
679 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
680 selector);
681
682 desc = reg_voltage_map[rid];
683 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
684 if (new_val < 0)
685 return new_val;
686
687 tmp_dmg = INT_MAX;
688 tmp_idx = -1;
689 tmp_val = -1;
690 do {
691 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
692 if (damage == 0)
693 goto out;
694
695 if (tmp_dmg > damage) {
696 tmp_idx = new_idx;
697 tmp_val = new_val;
698 tmp_dmg = damage;
699 }
700
701 new_val++;
702 } while (desc->min + desc->step + new_val <= desc->max);
703
704 new_idx = tmp_idx;
705 new_val = tmp_val;
706
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900707 if (max8997->ignore_gpiodvs_side_effect == false)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900708 return -EINVAL;
709
710 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
711 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
712
713out:
714 if (new_idx < 0 || new_val < 0)
715 return -EINVAL;
716
717 max8997->buck125_gpioindex = new_idx;
718 max8997_set_gpio(max8997);
719 *selector = new_val;
720
721 return 0;
722}
723
724static const int safeoutvolt[] = {
725 3300000,
726 4850000,
727 4900000,
728 4950000,
729};
730
731/* For SAFEOUT1 and SAFEOUT2 */
732static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
733 int min_uV, int max_uV, unsigned *selector)
734{
735 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
736 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800737 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900738 int reg, shift = 0, mask, ret;
739 int i = 0;
740 u8 val;
741
742 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
743 return -EINVAL;
744
745 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
746 if (min_uV <= safeoutvolt[i] &&
747 max_uV >= safeoutvolt[i])
748 break;
749 }
750
751 if (i >= ARRAY_SIZE(safeoutvolt))
752 return -EINVAL;
753
754 if (i == 0)
755 val = 0x3;
756 else
757 val = i - 1;
758
759 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
760 if (ret)
761 return ret;
762
763 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
764 *selector = val;
765
766 return ret;
767}
768
769static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
770{
771 return 0;
772}
773
774static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
775{
776 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
777 struct i2c_client *i2c = max8997->iodev->i2c;
778 int ret, reg, mask, pattern;
Axel Linb3e13482012-03-01 09:26:27 +0800779 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900780
781 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
782 if (ret)
783 return ret;
784
785 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
786
787 if (rid == MAX8997_LDO1 ||
788 rid == MAX8997_LDO10 ||
789 rid == MAX8997_LDO21) {
790 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
791 rdev->desc->name);
792 return max8997_update_reg(i2c, reg, 0x40, mask);
793 }
794
795 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
796 rdev->desc->name, max8997->saved_states[rid] & mask,
797 (~pattern) & mask);
798 return max8997_update_reg(i2c, reg, ~pattern, mask);
799}
800
801static struct regulator_ops max8997_ldo_ops = {
802 .list_voltage = max8997_list_voltage,
803 .is_enabled = max8997_reg_is_enabled,
804 .enable = max8997_reg_enable,
805 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800806 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900807 .set_voltage = max8997_set_voltage_ldobuck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800808 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900809 .set_suspend_enable = max8997_reg_enable_suspend,
810 .set_suspend_disable = max8997_reg_disable_suspend,
811};
812
813static struct regulator_ops max8997_buck_ops = {
814 .list_voltage = max8997_list_voltage,
815 .is_enabled = max8997_reg_is_enabled,
816 .enable = max8997_reg_enable,
817 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800818 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900819 .set_voltage = max8997_set_voltage_buck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800820 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900821 .set_suspend_enable = max8997_reg_enable_suspend,
822 .set_suspend_disable = max8997_reg_disable_suspend,
823};
824
825static struct regulator_ops max8997_fixedvolt_ops = {
826 .list_voltage = max8997_list_voltage,
827 .is_enabled = max8997_reg_is_enabled,
828 .enable = max8997_reg_enable,
829 .disable = max8997_reg_disable,
830 .set_suspend_enable = max8997_reg_enable_suspend,
831 .set_suspend_disable = max8997_reg_disable_suspend,
832};
833
834static struct regulator_ops max8997_safeout_ops = {
835 .list_voltage = max8997_list_voltage_safeout,
836 .is_enabled = max8997_reg_is_enabled,
837 .enable = max8997_reg_enable,
838 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800839 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900840 .set_voltage = max8997_set_voltage_safeout,
841 .set_suspend_enable = max8997_reg_enable_suspend,
842 .set_suspend_disable = max8997_reg_disable_suspend,
843};
844
845static struct regulator_ops max8997_fixedstate_ops = {
846 .list_voltage = max8997_list_voltage_charger_cv,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800847 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900848 .set_voltage = max8997_set_voltage_charger_cv,
849};
850
Axel Lin9e96b3a2012-04-11 22:56:11 +0800851static int max8997_set_current_limit(struct regulator_dev *rdev,
852 int min_uA, int max_uA)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900853{
854 unsigned dummy;
Axel Lin9e96b3a2012-04-11 22:56:11 +0800855 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900856
Axel Lin9e96b3a2012-04-11 22:56:11 +0800857 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
858 return -EINVAL;
859
860 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
861 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900862}
863
Axel Lin9e96b3a2012-04-11 22:56:11 +0800864static int max8997_get_current_limit(struct regulator_dev *rdev)
865{
866 int sel, rid = rdev_get_id(rdev);
867
868 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
869 return -EINVAL;
870
871 sel = max8997_get_voltage_sel(rdev);
872 if (sel < 0)
873 return sel;
874
875 /* Reuse max8997_list_voltage to get current_limit. */
876 return max8997_list_voltage(rdev, sel);
877}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900878
879static struct regulator_ops max8997_charger_ops = {
880 .is_enabled = max8997_reg_is_enabled,
881 .enable = max8997_reg_enable,
882 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800883 .get_current_limit = max8997_get_current_limit,
884 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900885};
886
887static struct regulator_ops max8997_charger_fixedstate_ops = {
Axel Lin9e96b3a2012-04-11 22:56:11 +0800888 .get_current_limit = max8997_get_current_limit,
889 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900890};
891
Axel Lin4533f802012-03-13 14:53:58 +0800892#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
893 .name = #_name, \
894 .id = MAX8997_##_name, \
895 .ops = &_ops, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900896 .type = REGULATOR_VOLTAGE, \
897 .owner = THIS_MODULE, \
898}
899
Axel Lin4533f802012-03-13 14:53:58 +0800900#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
901 .name = #_name, \
902 .id = MAX8997_##_name, \
903 .ops = &_ops, \
904 .type = REGULATOR_CURRENT, \
905 .owner = THIS_MODULE, \
906}
907
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900908static struct regulator_desc regulators[] = {
Axel Lin4533f802012-03-13 14:53:58 +0800909 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
910 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
911 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
912 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
913 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
914 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
915 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
916 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
917 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
918 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
919 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
920 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
921 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
922 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
923 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
924 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
925 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
926 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
927 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
928 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
929 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
930 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
931 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
932 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
933 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
934 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
935 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
936 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
937 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
938 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
939 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
940 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
941 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
942 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
943 max8997_charger_fixedstate_ops),
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900944};
945
946static __devinit int max8997_pmic_probe(struct platform_device *pdev)
947{
948 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
949 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100950 struct regulator_config config = { };
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900951 struct regulator_dev **rdev;
952 struct max8997_data *max8997;
953 struct i2c_client *i2c;
954 int i, ret, size;
955 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
956
957 if (!pdata) {
958 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
959 return -ENODEV;
960 }
961
962 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
963 if (!max8997)
964 return -ENOMEM;
965
966 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
967 max8997->rdev = kzalloc(size, GFP_KERNEL);
968 if (!max8997->rdev) {
969 kfree(max8997);
970 return -ENOMEM;
971 }
972
973 rdev = max8997->rdev;
974 max8997->dev = &pdev->dev;
975 max8997->iodev = iodev;
976 max8997->num_regulators = pdata->num_regulators;
977 platform_set_drvdata(pdev, max8997);
978 i2c = max8997->iodev->i2c;
979
980 max8997->buck125_gpioindex = pdata->buck125_default_idx;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900981 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
982 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
983 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
984 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
985 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900986
987 for (i = 0; i < 8; i++) {
988 max8997->buck1_vol[i] = ret =
989 max8997_get_voltage_proper_val(
990 &buck1245_voltage_map_desc,
991 pdata->buck1_voltage[i] / 1000,
992 pdata->buck1_voltage[i] / 1000 +
993 buck1245_voltage_map_desc.step);
994 if (ret < 0)
995 goto err_alloc;
996
997 max8997->buck2_vol[i] = ret =
998 max8997_get_voltage_proper_val(
999 &buck1245_voltage_map_desc,
1000 pdata->buck2_voltage[i] / 1000,
1001 pdata->buck2_voltage[i] / 1000 +
1002 buck1245_voltage_map_desc.step);
1003 if (ret < 0)
1004 goto err_alloc;
1005
1006 max8997->buck5_vol[i] = ret =
1007 max8997_get_voltage_proper_val(
1008 &buck1245_voltage_map_desc,
1009 pdata->buck5_voltage[i] / 1000,
1010 pdata->buck5_voltage[i] / 1000 +
1011 buck1245_voltage_map_desc.step);
1012 if (ret < 0)
1013 goto err_alloc;
1014
1015 if (max_buck1 < max8997->buck1_vol[i])
1016 max_buck1 = max8997->buck1_vol[i];
1017 if (max_buck2 < max8997->buck2_vol[i])
1018 max_buck2 = max8997->buck2_vol[i];
1019 if (max_buck5 < max8997->buck5_vol[i])
1020 max_buck5 = max8997->buck5_vol[i];
1021 }
1022
1023 /* For the safety, set max voltage before setting up */
1024 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001025 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001026 max_buck1, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001027 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001028 max_buck2, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001029 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001030 max_buck5, 0x3f);
1031 }
1032
1033 /*
1034 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1035 * If at least one of them cares, set gpios.
1036 */
1037 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1038 pdata->buck5_gpiodvs) {
1039 bool gpio1set = false, gpio2set = false;
1040
1041 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1042 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1043 !gpio_is_valid(pdata->buck125_gpios[2])) {
1044 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1045 ret = -EINVAL;
1046 goto err_alloc;
1047 }
1048
1049 ret = gpio_request(pdata->buck125_gpios[0],
1050 "MAX8997 SET1");
1051 if (ret == -EBUSY)
1052 dev_warn(&pdev->dev, "Duplicated gpio request"
1053 " on SET1\n");
1054 else if (ret)
1055 goto err_alloc;
1056 else
1057 gpio1set = true;
1058
1059 ret = gpio_request(pdata->buck125_gpios[1],
1060 "MAX8997 SET2");
1061 if (ret == -EBUSY)
1062 dev_warn(&pdev->dev, "Duplicated gpio request"
1063 " on SET2\n");
1064 else if (ret) {
1065 if (gpio1set)
1066 gpio_free(pdata->buck125_gpios[0]);
1067 goto err_alloc;
1068 } else
1069 gpio2set = true;
1070
1071 ret = gpio_request(pdata->buck125_gpios[2],
1072 "MAX8997 SET3");
1073 if (ret == -EBUSY)
1074 dev_warn(&pdev->dev, "Duplicated gpio request"
1075 " on SET3\n");
1076 else if (ret) {
1077 if (gpio1set)
1078 gpio_free(pdata->buck125_gpios[0]);
1079 if (gpio2set)
1080 gpio_free(pdata->buck125_gpios[1]);
1081 goto err_alloc;
1082 }
1083
1084 gpio_direction_output(pdata->buck125_gpios[0],
1085 (max8997->buck125_gpioindex >> 2)
1086 & 0x1); /* SET1 */
1087 gpio_direction_output(pdata->buck125_gpios[1],
1088 (max8997->buck125_gpioindex >> 1)
1089 & 0x1); /* SET2 */
1090 gpio_direction_output(pdata->buck125_gpios[2],
1091 (max8997->buck125_gpioindex >> 0)
1092 & 0x1); /* SET3 */
1093 ret = 0;
1094 }
1095
1096 /* DVS-GPIO disabled */
1097 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1098 (1 << 1) : (0 << 1), 1 << 1);
1099 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1100 (1 << 1) : (0 << 1), 1 << 1);
1101 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1102 (1 << 1) : (0 << 1), 1 << 1);
1103
1104 /* Initialize all the DVS related BUCK registers */
1105 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001106 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001107 max8997->buck1_vol[i],
1108 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001109 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001110 max8997->buck2_vol[i],
1111 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001112 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001113 max8997->buck5_vol[i],
1114 0x3f);
1115 }
1116
Tushar Beheradbb48e72011-06-21 09:08:59 +05301117 /* Misc Settings */
1118 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1119 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1120
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001121 for (i = 0; i < pdata->num_regulators; i++) {
1122 const struct voltage_map_desc *desc;
1123 int id = pdata->regulators[i].id;
1124
1125 desc = reg_voltage_map[id];
1126 if (desc)
1127 regulators[id].n_voltages =
1128 (desc->max - desc->min) / desc->step + 1;
1129 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1130 regulators[id].n_voltages = 4;
1131 else if (id == MAX8997_CHARGER_CV)
1132 regulators[id].n_voltages = 16;
1133
Mark Brownc1727082012-04-04 00:50:22 +01001134 config.dev = max8997->dev;
1135 config.init_data = pdata->regulators[i].initdata;
1136 config.driver_data = max8997;
1137
1138 rdev[i] = regulator_register(&regulators[id], &config);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001139 if (IS_ERR(rdev[i])) {
1140 ret = PTR_ERR(rdev[i]);
1141 dev_err(max8997->dev, "regulator init failed for %d\n",
1142 id);
1143 rdev[i] = NULL;
1144 goto err;
1145 }
1146 }
1147
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001148 return 0;
1149err:
1150 for (i = 0; i < max8997->num_regulators; i++)
1151 if (rdev[i])
1152 regulator_unregister(rdev[i]);
1153err_alloc:
1154 kfree(max8997->rdev);
1155 kfree(max8997);
1156
1157 return ret;
1158}
1159
1160static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1161{
1162 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1163 struct regulator_dev **rdev = max8997->rdev;
1164 int i;
1165
1166 for (i = 0; i < max8997->num_regulators; i++)
1167 if (rdev[i])
1168 regulator_unregister(rdev[i]);
1169
1170 kfree(max8997->rdev);
1171 kfree(max8997);
1172
1173 return 0;
1174}
1175
1176static const struct platform_device_id max8997_pmic_id[] = {
1177 { "max8997-pmic", 0},
1178 { },
1179};
Axel Lina51b9072011-03-26 23:28:42 +08001180MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001181
1182static struct platform_driver max8997_pmic_driver = {
1183 .driver = {
1184 .name = "max8997-pmic",
1185 .owner = THIS_MODULE,
1186 },
1187 .probe = max8997_pmic_probe,
1188 .remove = __devexit_p(max8997_pmic_remove),
1189 .id_table = max8997_pmic_id,
1190};
1191
1192static int __init max8997_pmic_init(void)
1193{
1194 return platform_driver_register(&max8997_pmic_driver);
1195}
1196subsys_initcall(max8997_pmic_init);
1197
1198static void __exit max8997_pmic_cleanup(void)
1199{
1200 platform_driver_unregister(&max8997_pmic_driver);
1201}
1202module_exit(max8997_pmic_cleanup);
1203
1204MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1205MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1206MODULE_LICENSE("GPL");