blob: ec99bd30e603ff839bb8fbbf4675df07ccdf7d39 [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);
281 if (ret == -EINVAL)
282 return 1; /* "not controllable" */
283 else if (ret)
284 return ret;
285
286 ret = max8997_read_reg(i2c, reg, &val);
287 if (ret)
288 return ret;
289
290 return (val & mask) == pattern;
291}
292
293static int max8997_reg_enable(struct regulator_dev *rdev)
294{
295 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
296 struct i2c_client *i2c = max8997->iodev->i2c;
297 int ret, reg, mask, pattern;
298
299 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
300 if (ret)
301 return ret;
302
303 return max8997_update_reg(i2c, reg, pattern, mask);
304}
305
306static int max8997_reg_disable(struct regulator_dev *rdev)
307{
308 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
309 struct i2c_client *i2c = max8997->iodev->i2c;
310 int ret, reg, mask, pattern;
311
312 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
313 if (ret)
314 return ret;
315
316 return max8997_update_reg(i2c, reg, ~pattern, mask);
317}
318
319static int max8997_get_voltage_register(struct regulator_dev *rdev,
320 int *_reg, int *_shift, int *_mask)
321{
Axel Lin6f43c382012-03-13 11:22:41 +0800322 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800323 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900324 int reg, shift = 0, mask = 0x3f;
325
326 switch (rid) {
327 case MAX8997_LDO1 ... MAX8997_LDO21:
328 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
329 break;
330 case MAX8997_BUCK1:
331 reg = MAX8997_REG_BUCK1DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800332 if (max8997->buck1_gpiodvs)
333 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900334 break;
335 case MAX8997_BUCK2:
336 reg = MAX8997_REG_BUCK2DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800337 if (max8997->buck2_gpiodvs)
338 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900339 break;
340 case MAX8997_BUCK3:
341 reg = MAX8997_REG_BUCK3DVS;
342 break;
343 case MAX8997_BUCK4:
344 reg = MAX8997_REG_BUCK4DVS;
345 break;
346 case MAX8997_BUCK5:
347 reg = MAX8997_REG_BUCK5DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800348 if (max8997->buck5_gpiodvs)
349 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900350 break;
351 case MAX8997_BUCK7:
352 reg = MAX8997_REG_BUCK7DVS;
353 break;
354 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
355 reg = MAX8997_REG_SAFEOUTCTRL;
356 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
357 mask = 0x3;
358 break;
359 case MAX8997_CHARGER_CV:
360 reg = MAX8997_REG_MBCCTRL3;
361 shift = 0;
362 mask = 0xf;
363 break;
364 case MAX8997_CHARGER:
365 reg = MAX8997_REG_MBCCTRL4;
366 shift = 0;
367 mask = 0xf;
368 break;
369 case MAX8997_CHARGER_TOPOFF:
370 reg = MAX8997_REG_MBCCTRL5;
371 shift = 0;
372 mask = 0xf;
373 break;
374 default:
375 return -EINVAL;
376 }
377
378 *_reg = reg;
379 *_shift = shift;
380 *_mask = mask;
381
382 return 0;
383}
384
Axel Lin9e96b3a2012-04-11 22:56:11 +0800385static int max8997_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900386{
387 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900388 struct i2c_client *i2c = max8997->iodev->i2c;
389 int reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900390 u8 val;
391
392 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
393 if (ret)
394 return ret;
395
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900396 ret = max8997_read_reg(i2c, reg, &val);
397 if (ret)
398 return ret;
399
400 val >>= shift;
401 val &= mask;
402
Axel Lin9e96b3a2012-04-11 22:56:11 +0800403 return val;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900404}
405
406static inline int max8997_get_voltage_proper_val(
407 const struct voltage_map_desc *desc,
408 int min_vol, int max_vol)
409{
Axel Lin2358b772012-04-10 14:20:03 +0800410 int i;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900411
412 if (desc == NULL)
413 return -EINVAL;
414
415 if (max_vol < desc->min || min_vol > desc->max)
416 return -EINVAL;
417
Axel Lin2358b772012-04-10 14:20:03 +0800418 if (min_vol < desc->min)
419 min_vol = desc->min;
420
421 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900422
423 if (desc->min + desc->step * i > max_vol)
424 return -EINVAL;
425
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900426 return i;
427}
428
429static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
430 int min_uV, int max_uV, unsigned *selector)
431{
432 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
433 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800434 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900435 int lb, ub;
436 int reg, shift = 0, mask, ret = 0;
437 u8 val = 0x0;
438
439 if (rid != MAX8997_CHARGER_CV)
440 return -EINVAL;
441
442 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
443 if (ret)
444 return ret;
445
446 if (max_uV < 4000000 || min_uV > 4350000)
447 return -EINVAL;
448
449 if (min_uV <= 4000000) {
450 if (max_uV >= 4000000)
451 return -EINVAL;
452 else
453 val = 0x1;
454 } else if (min_uV <= 4200000 && max_uV >= 4200000)
455 val = 0x0;
456 else {
457 lb = (min_uV - 4000001) / 20000 + 2;
458 ub = (max_uV - 4000000) / 20000 + 1;
459
460 if (lb > ub)
461 return -EINVAL;
462
463 if (lb < 0xf)
464 val = lb;
465 else {
466 if (ub >= 0xf)
467 val = 0xf;
468 else
469 return -EINVAL;
470 }
471 }
472
473 *selector = val;
474
475 ret = max8997_update_reg(i2c, reg, val << shift, mask);
476
477 return ret;
478}
479
480/*
481 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
482 * BUCK1, 2, and 5 are available if they are not controlled by gpio
483 */
484static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
485 int min_uV, int max_uV, unsigned *selector)
486{
487 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
488 struct i2c_client *i2c = max8997->iodev->i2c;
489 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
490 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800491 int rid = rdev_get_id(rdev);
Axel Lin62bc4d42012-04-11 22:58:03 +0800492 int i, reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900493
494 switch (rid) {
495 case MAX8997_LDO1 ... MAX8997_LDO21:
496 break;
497 case MAX8997_BUCK1 ... MAX8997_BUCK5:
498 break;
499 case MAX8997_BUCK6:
500 return -EINVAL;
501 case MAX8997_BUCK7:
502 break;
503 case MAX8997_CHARGER:
504 break;
505 case MAX8997_CHARGER_TOPOFF:
506 break;
507 default:
508 return -EINVAL;
509 }
510
511 desc = reg_voltage_map[rid];
512
513 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
514 if (i < 0)
515 return i;
516
517 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
518 if (ret)
519 return ret;
520
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900521 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
522 *selector = i;
523
Axel Lin62bc4d42012-04-11 22:58:03 +0800524 return ret;
525}
526
527static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
528 unsigned int old_selector,
529 unsigned int new_selector)
530{
531 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
532 int rid = rdev_get_id(rdev);
533 const struct voltage_map_desc *desc = reg_voltage_map[rid];
534
535 /* Delay is required only if the voltage is increasing */
536 if (old_selector >= new_selector)
537 return 0;
538
539 /* No need to delay if gpio_dvs_mode */
540 switch (rid) {
541 case MAX8997_BUCK1:
542 if (max8997->buck1_gpiodvs)
543 return 0;
544 break;
545 case MAX8997_BUCK2:
546 if (max8997->buck2_gpiodvs)
547 return 0;
548 break;
549 case MAX8997_BUCK5:
550 if (max8997->buck5_gpiodvs)
551 return 0;
552 break;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900553 }
554
Axel Lin62bc4d42012-04-11 22:58:03 +0800555 switch (rid) {
556 case MAX8997_BUCK1:
557 case MAX8997_BUCK2:
558 case MAX8997_BUCK4:
559 case MAX8997_BUCK5:
560 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
561 max8997->ramp_delay);
562 }
563
564 return 0;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900565}
566
567/*
568 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
569 *
570 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
571 * of one of the bucks may affect that of another buck, which is the side
572 * effect of the change (set_voltage). This function examines the GPIO-DVS
573 * configurations and checks whether such side-effect exists.
574 */
575static int max8997_assess_side_effect(struct regulator_dev *rdev,
576 u8 new_val, int *best)
577{
578 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800579 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900580 u8 *buckx_val[3];
581 bool buckx_gpiodvs[3];
582 int side_effect[8];
583 int min_side_effect = INT_MAX;
584 int i;
585
586 *best = -1;
587
588 switch (rid) {
589 case MAX8997_BUCK1:
590 rid = 0;
591 break;
592 case MAX8997_BUCK2:
593 rid = 1;
594 break;
595 case MAX8997_BUCK5:
596 rid = 2;
597 break;
598 default:
599 return -EINVAL;
600 }
601
602 buckx_val[0] = max8997->buck1_vol;
603 buckx_val[1] = max8997->buck2_vol;
604 buckx_val[2] = max8997->buck5_vol;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900605 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
606 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
607 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900608
609 for (i = 0; i < 8; i++) {
610 int others;
611
612 if (new_val != (buckx_val[rid])[i]) {
613 side_effect[i] = -1;
614 continue;
615 }
616
617 side_effect[i] = 0;
618 for (others = 0; others < 3; others++) {
619 int diff;
620
621 if (others == rid)
622 continue;
623 if (buckx_gpiodvs[others] == false)
624 continue; /* Not affected */
625 diff = (buckx_val[others])[i] -
626 (buckx_val[others])[max8997->buck125_gpioindex];
627 if (diff > 0)
628 side_effect[i] += diff;
629 else if (diff < 0)
630 side_effect[i] -= diff;
631 }
632 if (side_effect[i] == 0) {
633 *best = i;
634 return 0; /* NO SIDE EFFECT! Use This! */
635 }
636 if (side_effect[i] < min_side_effect) {
637 min_side_effect = side_effect[i];
638 *best = i;
639 }
640 }
641
642 if (*best == -1)
643 return -EINVAL;
644
645 return side_effect[*best];
646}
647
648/*
649 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
650 * max8997_set_voltage_ldobuck to do the job.
651 */
652static int max8997_set_voltage_buck(struct regulator_dev *rdev,
653 int min_uV, int max_uV, unsigned *selector)
654{
655 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800656 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900657 const struct voltage_map_desc *desc;
658 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
659 bool gpio_dvs_mode = false;
660 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
661
662 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
663 return -EINVAL;
664
665 switch (rid) {
666 case MAX8997_BUCK1:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900667 if (max8997->buck1_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900668 gpio_dvs_mode = true;
669 break;
670 case MAX8997_BUCK2:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900671 if (max8997->buck2_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900672 gpio_dvs_mode = true;
673 break;
674 case MAX8997_BUCK5:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900675 if (max8997->buck5_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900676 gpio_dvs_mode = true;
677 break;
678 }
679
680 if (!gpio_dvs_mode)
681 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
682 selector);
683
684 desc = reg_voltage_map[rid];
685 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
686 if (new_val < 0)
687 return new_val;
688
689 tmp_dmg = INT_MAX;
690 tmp_idx = -1;
691 tmp_val = -1;
692 do {
693 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
694 if (damage == 0)
695 goto out;
696
697 if (tmp_dmg > damage) {
698 tmp_idx = new_idx;
699 tmp_val = new_val;
700 tmp_dmg = damage;
701 }
702
703 new_val++;
704 } while (desc->min + desc->step + new_val <= desc->max);
705
706 new_idx = tmp_idx;
707 new_val = tmp_val;
708
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900709 if (max8997->ignore_gpiodvs_side_effect == false)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900710 return -EINVAL;
711
712 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
713 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
714
715out:
716 if (new_idx < 0 || new_val < 0)
717 return -EINVAL;
718
719 max8997->buck125_gpioindex = new_idx;
720 max8997_set_gpio(max8997);
721 *selector = new_val;
722
723 return 0;
724}
725
726static const int safeoutvolt[] = {
727 3300000,
728 4850000,
729 4900000,
730 4950000,
731};
732
733/* For SAFEOUT1 and SAFEOUT2 */
734static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
735 int min_uV, int max_uV, unsigned *selector)
736{
737 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
738 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800739 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900740 int reg, shift = 0, mask, ret;
741 int i = 0;
742 u8 val;
743
744 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
745 return -EINVAL;
746
747 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
748 if (min_uV <= safeoutvolt[i] &&
749 max_uV >= safeoutvolt[i])
750 break;
751 }
752
753 if (i >= ARRAY_SIZE(safeoutvolt))
754 return -EINVAL;
755
756 if (i == 0)
757 val = 0x3;
758 else
759 val = i - 1;
760
761 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
762 if (ret)
763 return ret;
764
765 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
766 *selector = val;
767
768 return ret;
769}
770
771static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
772{
773 return 0;
774}
775
776static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
777{
778 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
779 struct i2c_client *i2c = max8997->iodev->i2c;
780 int ret, reg, mask, pattern;
Axel Linb3e13482012-03-01 09:26:27 +0800781 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900782
783 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
784 if (ret)
785 return ret;
786
787 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
788
789 if (rid == MAX8997_LDO1 ||
790 rid == MAX8997_LDO10 ||
791 rid == MAX8997_LDO21) {
792 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
793 rdev->desc->name);
794 return max8997_update_reg(i2c, reg, 0x40, mask);
795 }
796
797 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
798 rdev->desc->name, max8997->saved_states[rid] & mask,
799 (~pattern) & mask);
800 return max8997_update_reg(i2c, reg, ~pattern, mask);
801}
802
803static struct regulator_ops max8997_ldo_ops = {
804 .list_voltage = max8997_list_voltage,
805 .is_enabled = max8997_reg_is_enabled,
806 .enable = max8997_reg_enable,
807 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800808 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900809 .set_voltage = max8997_set_voltage_ldobuck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800810 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900811 .set_suspend_enable = max8997_reg_enable_suspend,
812 .set_suspend_disable = max8997_reg_disable_suspend,
813};
814
815static struct regulator_ops max8997_buck_ops = {
816 .list_voltage = max8997_list_voltage,
817 .is_enabled = max8997_reg_is_enabled,
818 .enable = max8997_reg_enable,
819 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800820 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900821 .set_voltage = max8997_set_voltage_buck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800822 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900823 .set_suspend_enable = max8997_reg_enable_suspend,
824 .set_suspend_disable = max8997_reg_disable_suspend,
825};
826
827static struct regulator_ops max8997_fixedvolt_ops = {
828 .list_voltage = max8997_list_voltage,
829 .is_enabled = max8997_reg_is_enabled,
830 .enable = max8997_reg_enable,
831 .disable = max8997_reg_disable,
832 .set_suspend_enable = max8997_reg_enable_suspend,
833 .set_suspend_disable = max8997_reg_disable_suspend,
834};
835
836static struct regulator_ops max8997_safeout_ops = {
837 .list_voltage = max8997_list_voltage_safeout,
838 .is_enabled = max8997_reg_is_enabled,
839 .enable = max8997_reg_enable,
840 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800841 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900842 .set_voltage = max8997_set_voltage_safeout,
843 .set_suspend_enable = max8997_reg_enable_suspend,
844 .set_suspend_disable = max8997_reg_disable_suspend,
845};
846
847static struct regulator_ops max8997_fixedstate_ops = {
848 .list_voltage = max8997_list_voltage_charger_cv,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800849 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900850 .set_voltage = max8997_set_voltage_charger_cv,
851};
852
Axel Lin9e96b3a2012-04-11 22:56:11 +0800853static int max8997_set_current_limit(struct regulator_dev *rdev,
854 int min_uA, int max_uA)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900855{
856 unsigned dummy;
Axel Lin9e96b3a2012-04-11 22:56:11 +0800857 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900858
Axel Lin9e96b3a2012-04-11 22:56:11 +0800859 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
860 return -EINVAL;
861
862 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
863 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900864}
865
Axel Lin9e96b3a2012-04-11 22:56:11 +0800866static int max8997_get_current_limit(struct regulator_dev *rdev)
867{
868 int sel, rid = rdev_get_id(rdev);
869
870 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
871 return -EINVAL;
872
873 sel = max8997_get_voltage_sel(rdev);
874 if (sel < 0)
875 return sel;
876
877 /* Reuse max8997_list_voltage to get current_limit. */
878 return max8997_list_voltage(rdev, sel);
879}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900880
881static struct regulator_ops max8997_charger_ops = {
882 .is_enabled = max8997_reg_is_enabled,
883 .enable = max8997_reg_enable,
884 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800885 .get_current_limit = max8997_get_current_limit,
886 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900887};
888
889static struct regulator_ops max8997_charger_fixedstate_ops = {
890 .is_enabled = max8997_reg_is_enabled,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800891 .get_current_limit = max8997_get_current_limit,
892 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900893};
894
Axel Lin4533f802012-03-13 14:53:58 +0800895#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
896 .name = #_name, \
897 .id = MAX8997_##_name, \
898 .ops = &_ops, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900899 .type = REGULATOR_VOLTAGE, \
900 .owner = THIS_MODULE, \
901}
902
Axel Lin4533f802012-03-13 14:53:58 +0800903#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
904 .name = #_name, \
905 .id = MAX8997_##_name, \
906 .ops = &_ops, \
907 .type = REGULATOR_CURRENT, \
908 .owner = THIS_MODULE, \
909}
910
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900911static struct regulator_desc regulators[] = {
Axel Lin4533f802012-03-13 14:53:58 +0800912 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
913 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
914 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
915 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
916 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
917 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
918 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
919 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
920 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
921 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
922 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
923 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
924 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
925 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
926 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
927 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
928 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
929 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
930 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
931 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
932 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
933 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
934 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
935 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
936 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
937 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
938 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
939 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
940 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
941 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
942 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
943 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
944 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
945 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
946 max8997_charger_fixedstate_ops),
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900947};
948
949static __devinit int max8997_pmic_probe(struct platform_device *pdev)
950{
951 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
952 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100953 struct regulator_config config = { };
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900954 struct regulator_dev **rdev;
955 struct max8997_data *max8997;
956 struct i2c_client *i2c;
957 int i, ret, size;
958 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
959
960 if (!pdata) {
961 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
962 return -ENODEV;
963 }
964
965 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
966 if (!max8997)
967 return -ENOMEM;
968
969 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
970 max8997->rdev = kzalloc(size, GFP_KERNEL);
971 if (!max8997->rdev) {
972 kfree(max8997);
973 return -ENOMEM;
974 }
975
976 rdev = max8997->rdev;
977 max8997->dev = &pdev->dev;
978 max8997->iodev = iodev;
979 max8997->num_regulators = pdata->num_regulators;
980 platform_set_drvdata(pdev, max8997);
981 i2c = max8997->iodev->i2c;
982
983 max8997->buck125_gpioindex = pdata->buck125_default_idx;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900984 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
985 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
986 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
987 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
988 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900989
990 for (i = 0; i < 8; i++) {
991 max8997->buck1_vol[i] = ret =
992 max8997_get_voltage_proper_val(
993 &buck1245_voltage_map_desc,
994 pdata->buck1_voltage[i] / 1000,
995 pdata->buck1_voltage[i] / 1000 +
996 buck1245_voltage_map_desc.step);
997 if (ret < 0)
998 goto err_alloc;
999
1000 max8997->buck2_vol[i] = ret =
1001 max8997_get_voltage_proper_val(
1002 &buck1245_voltage_map_desc,
1003 pdata->buck2_voltage[i] / 1000,
1004 pdata->buck2_voltage[i] / 1000 +
1005 buck1245_voltage_map_desc.step);
1006 if (ret < 0)
1007 goto err_alloc;
1008
1009 max8997->buck5_vol[i] = ret =
1010 max8997_get_voltage_proper_val(
1011 &buck1245_voltage_map_desc,
1012 pdata->buck5_voltage[i] / 1000,
1013 pdata->buck5_voltage[i] / 1000 +
1014 buck1245_voltage_map_desc.step);
1015 if (ret < 0)
1016 goto err_alloc;
1017
1018 if (max_buck1 < max8997->buck1_vol[i])
1019 max_buck1 = max8997->buck1_vol[i];
1020 if (max_buck2 < max8997->buck2_vol[i])
1021 max_buck2 = max8997->buck2_vol[i];
1022 if (max_buck5 < max8997->buck5_vol[i])
1023 max_buck5 = max8997->buck5_vol[i];
1024 }
1025
1026 /* For the safety, set max voltage before setting up */
1027 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001028 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001029 max_buck1, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001030 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001031 max_buck2, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001032 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001033 max_buck5, 0x3f);
1034 }
1035
1036 /*
1037 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1038 * If at least one of them cares, set gpios.
1039 */
1040 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1041 pdata->buck5_gpiodvs) {
1042 bool gpio1set = false, gpio2set = false;
1043
1044 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1045 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1046 !gpio_is_valid(pdata->buck125_gpios[2])) {
1047 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1048 ret = -EINVAL;
1049 goto err_alloc;
1050 }
1051
1052 ret = gpio_request(pdata->buck125_gpios[0],
1053 "MAX8997 SET1");
1054 if (ret == -EBUSY)
1055 dev_warn(&pdev->dev, "Duplicated gpio request"
1056 " on SET1\n");
1057 else if (ret)
1058 goto err_alloc;
1059 else
1060 gpio1set = true;
1061
1062 ret = gpio_request(pdata->buck125_gpios[1],
1063 "MAX8997 SET2");
1064 if (ret == -EBUSY)
1065 dev_warn(&pdev->dev, "Duplicated gpio request"
1066 " on SET2\n");
1067 else if (ret) {
1068 if (gpio1set)
1069 gpio_free(pdata->buck125_gpios[0]);
1070 goto err_alloc;
1071 } else
1072 gpio2set = true;
1073
1074 ret = gpio_request(pdata->buck125_gpios[2],
1075 "MAX8997 SET3");
1076 if (ret == -EBUSY)
1077 dev_warn(&pdev->dev, "Duplicated gpio request"
1078 " on SET3\n");
1079 else if (ret) {
1080 if (gpio1set)
1081 gpio_free(pdata->buck125_gpios[0]);
1082 if (gpio2set)
1083 gpio_free(pdata->buck125_gpios[1]);
1084 goto err_alloc;
1085 }
1086
1087 gpio_direction_output(pdata->buck125_gpios[0],
1088 (max8997->buck125_gpioindex >> 2)
1089 & 0x1); /* SET1 */
1090 gpio_direction_output(pdata->buck125_gpios[1],
1091 (max8997->buck125_gpioindex >> 1)
1092 & 0x1); /* SET2 */
1093 gpio_direction_output(pdata->buck125_gpios[2],
1094 (max8997->buck125_gpioindex >> 0)
1095 & 0x1); /* SET3 */
1096 ret = 0;
1097 }
1098
1099 /* DVS-GPIO disabled */
1100 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1101 (1 << 1) : (0 << 1), 1 << 1);
1102 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1103 (1 << 1) : (0 << 1), 1 << 1);
1104 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1105 (1 << 1) : (0 << 1), 1 << 1);
1106
1107 /* Initialize all the DVS related BUCK registers */
1108 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001109 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001110 max8997->buck1_vol[i],
1111 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001112 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001113 max8997->buck2_vol[i],
1114 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001115 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001116 max8997->buck5_vol[i],
1117 0x3f);
1118 }
1119
Tushar Beheradbb48e72011-06-21 09:08:59 +05301120 /* Misc Settings */
1121 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1122 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1123
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001124 for (i = 0; i < pdata->num_regulators; i++) {
1125 const struct voltage_map_desc *desc;
1126 int id = pdata->regulators[i].id;
1127
1128 desc = reg_voltage_map[id];
1129 if (desc)
1130 regulators[id].n_voltages =
1131 (desc->max - desc->min) / desc->step + 1;
1132 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1133 regulators[id].n_voltages = 4;
1134 else if (id == MAX8997_CHARGER_CV)
1135 regulators[id].n_voltages = 16;
1136
Mark Brownc1727082012-04-04 00:50:22 +01001137 config.dev = max8997->dev;
1138 config.init_data = pdata->regulators[i].initdata;
1139 config.driver_data = max8997;
1140
1141 rdev[i] = regulator_register(&regulators[id], &config);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001142 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
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001151 return 0;
1152err:
1153 for (i = 0; i < max8997->num_regulators; i++)
1154 if (rdev[i])
1155 regulator_unregister(rdev[i]);
1156err_alloc:
1157 kfree(max8997->rdev);
1158 kfree(max8997);
1159
1160 return ret;
1161}
1162
1163static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1164{
1165 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1166 struct regulator_dev **rdev = max8997->rdev;
1167 int i;
1168
1169 for (i = 0; i < max8997->num_regulators; i++)
1170 if (rdev[i])
1171 regulator_unregister(rdev[i]);
1172
1173 kfree(max8997->rdev);
1174 kfree(max8997);
1175
1176 return 0;
1177}
1178
1179static const struct platform_device_id max8997_pmic_id[] = {
1180 { "max8997-pmic", 0},
1181 { },
1182};
Axel Lina51b9072011-03-26 23:28:42 +08001183MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001184
1185static struct platform_driver max8997_pmic_driver = {
1186 .driver = {
1187 .name = "max8997-pmic",
1188 .owner = THIS_MODULE,
1189 },
1190 .probe = max8997_pmic_probe,
1191 .remove = __devexit_p(max8997_pmic_remove),
1192 .id_table = max8997_pmic_id,
1193};
1194
1195static int __init max8997_pmic_init(void)
1196{
1197 return platform_driver_register(&max8997_pmic_driver);
1198}
1199subsys_initcall(max8997_pmic_init);
1200
1201static void __exit max8997_pmic_cleanup(void)
1202{
1203 platform_driver_unregister(&max8997_pmic_driver);
1204}
1205module_exit(max8997_pmic_cleanup);
1206
1207MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1208MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1209MODULE_LICENSE("GPL");