blob: b56c4326853d0e731029718b812131dd69bfa775 [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>
Thomas Abraham77b71b32012-11-27 14:04:32 +053027#include <linux/of_gpio.h>
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090028#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>
Thomas Abraham77b71b32012-11-27 14:04:32 +053035#include <linux/regulator/of_regulator.h>
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090036
37struct max8997_data {
38 struct device *dev;
39 struct max8997_dev *iodev;
40 int num_regulators;
41 struct regulator_dev **rdev;
42 int ramp_delay; /* in mV/us */
43
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090044 bool buck1_gpiodvs;
45 bool buck2_gpiodvs;
46 bool buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090047 u8 buck1_vol[8];
48 u8 buck2_vol[8];
49 u8 buck5_vol[8];
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090050 int buck125_gpios[3];
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090051 int buck125_gpioindex;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090052 bool ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090053
54 u8 saved_states[MAX8997_REG_MAX];
55};
56
57static inline void max8997_set_gpio(struct max8997_data *max8997)
58{
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090059 int set3 = (max8997->buck125_gpioindex) & 0x1;
60 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
61 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
62
MyungJoo Ham6e0414a2011-06-20 17:30:17 +090063 gpio_set_value(max8997->buck125_gpios[0], set1);
64 gpio_set_value(max8997->buck125_gpios[1], set2);
65 gpio_set_value(max8997->buck125_gpios[2], set3);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090066}
67
68struct voltage_map_desc {
69 int min;
70 int max;
71 int step;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090072};
73
74/* Voltage maps in mV */
75static const struct voltage_map_desc ldo_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080076 .min = 800, .max = 3950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090077}; /* LDO1 ~ 18, 21 all */
78
79static const struct voltage_map_desc buck1245_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080080 .min = 650, .max = 2225, .step = 25,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090081}; /* Buck1, 2, 4, 5 */
82
83static const struct voltage_map_desc buck37_voltage_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080084 .min = 750, .max = 3900, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090085}; /* Buck3, 7 */
86
87/* current map in mA */
88static const struct voltage_map_desc charger_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080089 .min = 200, .max = 950, .step = 50,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090090};
91
92static const struct voltage_map_desc topoff_current_map_desc = {
Axel Lin0fa310c2012-04-10 14:22:38 +080093 .min = 50, .max = 200, .step = 10,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +090094};
95
96static const struct voltage_map_desc *reg_voltage_map[] = {
97 [MAX8997_LDO1] = &ldo_voltage_map_desc,
98 [MAX8997_LDO2] = &ldo_voltage_map_desc,
99 [MAX8997_LDO3] = &ldo_voltage_map_desc,
100 [MAX8997_LDO4] = &ldo_voltage_map_desc,
101 [MAX8997_LDO5] = &ldo_voltage_map_desc,
102 [MAX8997_LDO6] = &ldo_voltage_map_desc,
103 [MAX8997_LDO7] = &ldo_voltage_map_desc,
104 [MAX8997_LDO8] = &ldo_voltage_map_desc,
105 [MAX8997_LDO9] = &ldo_voltage_map_desc,
106 [MAX8997_LDO10] = &ldo_voltage_map_desc,
107 [MAX8997_LDO11] = &ldo_voltage_map_desc,
108 [MAX8997_LDO12] = &ldo_voltage_map_desc,
109 [MAX8997_LDO13] = &ldo_voltage_map_desc,
110 [MAX8997_LDO14] = &ldo_voltage_map_desc,
111 [MAX8997_LDO15] = &ldo_voltage_map_desc,
112 [MAX8997_LDO16] = &ldo_voltage_map_desc,
113 [MAX8997_LDO17] = &ldo_voltage_map_desc,
114 [MAX8997_LDO18] = &ldo_voltage_map_desc,
115 [MAX8997_LDO21] = &ldo_voltage_map_desc,
116 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
117 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
118 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
119 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
120 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
121 [MAX8997_BUCK6] = NULL,
122 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
123 [MAX8997_EN32KHZ_AP] = NULL,
124 [MAX8997_EN32KHZ_CP] = NULL,
125 [MAX8997_ENVICHG] = NULL,
126 [MAX8997_ESAFEOUT1] = NULL,
127 [MAX8997_ESAFEOUT2] = NULL,
128 [MAX8997_CHARGER_CV] = NULL,
129 [MAX8997_CHARGER] = &charger_current_map_desc,
130 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
131};
132
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900133static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
134 unsigned int selector)
135{
Axel Linb3e13482012-03-01 09:26:27 +0800136 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900137
138 if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
139 switch (selector) {
140 case 0:
141 return 4850000;
142 case 1:
143 return 4900000;
144 case 2:
145 return 4950000;
146 case 3:
147 return 3300000;
148 default:
149 return -EINVAL;
150 }
151 }
152
153 return -EINVAL;
154}
155
156static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
157 unsigned int selector)
158{
Axel Linb3e13482012-03-01 09:26:27 +0800159 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900160
161 if (rid != MAX8997_CHARGER_CV)
162 goto err;
163
164 switch (selector) {
165 case 0x00:
166 return 4200000;
167 case 0x01 ... 0x0E:
168 return 4000000 + 20000 * (selector - 0x01);
169 case 0x0F:
170 return 4350000;
171 default:
172 return -EINVAL;
173 }
174err:
175 return -EINVAL;
176}
177
178static int max8997_list_voltage(struct regulator_dev *rdev,
179 unsigned int selector)
180{
181 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800182 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900183 int val;
184
185 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
186 rid < 0)
187 return -EINVAL;
188
189 desc = reg_voltage_map[rid];
190 if (desc == NULL)
191 return -EINVAL;
192
193 val = desc->min + desc->step * selector;
194 if (val > desc->max)
195 return -EINVAL;
196
197 return val * 1000;
198}
199
200static int max8997_get_enable_register(struct regulator_dev *rdev,
201 int *reg, int *mask, int *pattern)
202{
Axel Linb3e13482012-03-01 09:26:27 +0800203 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900204
205 switch (rid) {
206 case MAX8997_LDO1 ... MAX8997_LDO21:
207 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
208 *mask = 0xC0;
209 *pattern = 0xC0;
210 break;
211 case MAX8997_BUCK1:
212 *reg = MAX8997_REG_BUCK1CTRL;
213 *mask = 0x01;
214 *pattern = 0x01;
215 break;
216 case MAX8997_BUCK2:
217 *reg = MAX8997_REG_BUCK2CTRL;
218 *mask = 0x01;
219 *pattern = 0x01;
220 break;
221 case MAX8997_BUCK3:
222 *reg = MAX8997_REG_BUCK3CTRL;
223 *mask = 0x01;
224 *pattern = 0x01;
225 break;
226 case MAX8997_BUCK4:
227 *reg = MAX8997_REG_BUCK4CTRL;
228 *mask = 0x01;
229 *pattern = 0x01;
230 break;
231 case MAX8997_BUCK5:
232 *reg = MAX8997_REG_BUCK5CTRL;
233 *mask = 0x01;
234 *pattern = 0x01;
235 break;
236 case MAX8997_BUCK6:
237 *reg = MAX8997_REG_BUCK6CTRL;
238 *mask = 0x01;
239 *pattern = 0x01;
240 break;
241 case MAX8997_BUCK7:
242 *reg = MAX8997_REG_BUCK7CTRL;
243 *mask = 0x01;
244 *pattern = 0x01;
245 break;
246 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
247 *reg = MAX8997_REG_MAINCON1;
248 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
249 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
250 break;
251 case MAX8997_ENVICHG:
252 *reg = MAX8997_REG_MBCCTRL1;
253 *mask = 0x80;
254 *pattern = 0x80;
255 break;
256 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
257 *reg = MAX8997_REG_SAFEOUTCTRL;
258 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
259 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
260 break;
261 case MAX8997_CHARGER:
262 *reg = MAX8997_REG_MBCCTRL2;
263 *mask = 0x40;
264 *pattern = 0x40;
265 break;
266 default:
267 /* Not controllable or not exists */
268 return -EINVAL;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900269 }
270
271 return 0;
272}
273
274static int max8997_reg_is_enabled(struct regulator_dev *rdev)
275{
276 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
277 struct i2c_client *i2c = max8997->iodev->i2c;
278 int ret, reg, mask, pattern;
279 u8 val;
280
281 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
Axel Linc245c082012-04-17 23:48:27 +0800282 if (ret)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900283 return ret;
284
285 ret = max8997_read_reg(i2c, reg, &val);
286 if (ret)
287 return ret;
288
289 return (val & mask) == pattern;
290}
291
292static int max8997_reg_enable(struct regulator_dev *rdev)
293{
294 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
295 struct i2c_client *i2c = max8997->iodev->i2c;
296 int ret, reg, mask, pattern;
297
298 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
299 if (ret)
300 return ret;
301
302 return max8997_update_reg(i2c, reg, pattern, mask);
303}
304
305static int max8997_reg_disable(struct regulator_dev *rdev)
306{
307 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
308 struct i2c_client *i2c = max8997->iodev->i2c;
309 int ret, reg, mask, pattern;
310
311 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
312 if (ret)
313 return ret;
314
315 return max8997_update_reg(i2c, reg, ~pattern, mask);
316}
317
318static int max8997_get_voltage_register(struct regulator_dev *rdev,
319 int *_reg, int *_shift, int *_mask)
320{
Axel Lin6f43c382012-03-13 11:22:41 +0800321 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800322 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900323 int reg, shift = 0, mask = 0x3f;
324
325 switch (rid) {
326 case MAX8997_LDO1 ... MAX8997_LDO21:
327 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
328 break;
329 case MAX8997_BUCK1:
330 reg = MAX8997_REG_BUCK1DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800331 if (max8997->buck1_gpiodvs)
332 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900333 break;
334 case MAX8997_BUCK2:
335 reg = MAX8997_REG_BUCK2DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800336 if (max8997->buck2_gpiodvs)
337 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900338 break;
339 case MAX8997_BUCK3:
340 reg = MAX8997_REG_BUCK3DVS;
341 break;
342 case MAX8997_BUCK4:
343 reg = MAX8997_REG_BUCK4DVS;
344 break;
345 case MAX8997_BUCK5:
346 reg = MAX8997_REG_BUCK5DVS1;
Axel Lin6f43c382012-03-13 11:22:41 +0800347 if (max8997->buck5_gpiodvs)
348 reg += max8997->buck125_gpioindex;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900349 break;
350 case MAX8997_BUCK7:
351 reg = MAX8997_REG_BUCK7DVS;
352 break;
353 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
354 reg = MAX8997_REG_SAFEOUTCTRL;
355 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
356 mask = 0x3;
357 break;
358 case MAX8997_CHARGER_CV:
359 reg = MAX8997_REG_MBCCTRL3;
360 shift = 0;
361 mask = 0xf;
362 break;
363 case MAX8997_CHARGER:
364 reg = MAX8997_REG_MBCCTRL4;
365 shift = 0;
366 mask = 0xf;
367 break;
368 case MAX8997_CHARGER_TOPOFF:
369 reg = MAX8997_REG_MBCCTRL5;
370 shift = 0;
371 mask = 0xf;
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 *_reg = reg;
378 *_shift = shift;
379 *_mask = mask;
380
381 return 0;
382}
383
Axel Lin9e96b3a2012-04-11 22:56:11 +0800384static int max8997_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900385{
386 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900387 struct i2c_client *i2c = max8997->iodev->i2c;
388 int reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900389 u8 val;
390
391 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
392 if (ret)
393 return ret;
394
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900395 ret = max8997_read_reg(i2c, reg, &val);
396 if (ret)
397 return ret;
398
399 val >>= shift;
400 val &= mask;
401
Axel Lin9e96b3a2012-04-11 22:56:11 +0800402 return val;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900403}
404
405static inline int max8997_get_voltage_proper_val(
406 const struct voltage_map_desc *desc,
407 int min_vol, int max_vol)
408{
Axel Lin2358b772012-04-10 14:20:03 +0800409 int i;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900410
411 if (desc == NULL)
412 return -EINVAL;
413
414 if (max_vol < desc->min || min_vol > desc->max)
415 return -EINVAL;
416
Axel Lin2358b772012-04-10 14:20:03 +0800417 if (min_vol < desc->min)
418 min_vol = desc->min;
419
420 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900421
422 if (desc->min + desc->step * i > max_vol)
423 return -EINVAL;
424
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900425 return i;
426}
427
428static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
429 int min_uV, int max_uV, unsigned *selector)
430{
431 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
432 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800433 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900434 int lb, ub;
435 int reg, shift = 0, mask, ret = 0;
436 u8 val = 0x0;
437
438 if (rid != MAX8997_CHARGER_CV)
439 return -EINVAL;
440
441 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
442 if (ret)
443 return ret;
444
445 if (max_uV < 4000000 || min_uV > 4350000)
446 return -EINVAL;
447
448 if (min_uV <= 4000000) {
449 if (max_uV >= 4000000)
450 return -EINVAL;
451 else
452 val = 0x1;
453 } else if (min_uV <= 4200000 && max_uV >= 4200000)
454 val = 0x0;
455 else {
456 lb = (min_uV - 4000001) / 20000 + 2;
457 ub = (max_uV - 4000000) / 20000 + 1;
458
459 if (lb > ub)
460 return -EINVAL;
461
462 if (lb < 0xf)
463 val = lb;
464 else {
465 if (ub >= 0xf)
466 val = 0xf;
467 else
468 return -EINVAL;
469 }
470 }
471
472 *selector = val;
473
474 ret = max8997_update_reg(i2c, reg, val << shift, mask);
475
476 return ret;
477}
478
479/*
480 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
481 * BUCK1, 2, and 5 are available if they are not controlled by gpio
482 */
483static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
484 int min_uV, int max_uV, unsigned *selector)
485{
486 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
487 struct i2c_client *i2c = max8997->iodev->i2c;
488 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
489 const struct voltage_map_desc *desc;
Axel Linb3e13482012-03-01 09:26:27 +0800490 int rid = rdev_get_id(rdev);
Axel Lin62bc4d42012-04-11 22:58:03 +0800491 int i, reg, shift, mask, ret;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900492
493 switch (rid) {
494 case MAX8997_LDO1 ... MAX8997_LDO21:
495 break;
496 case MAX8997_BUCK1 ... MAX8997_BUCK5:
497 break;
498 case MAX8997_BUCK6:
499 return -EINVAL;
500 case MAX8997_BUCK7:
501 break;
502 case MAX8997_CHARGER:
503 break;
504 case MAX8997_CHARGER_TOPOFF:
505 break;
506 default:
507 return -EINVAL;
508 }
509
510 desc = reg_voltage_map[rid];
511
512 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
513 if (i < 0)
514 return i;
515
516 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
517 if (ret)
518 return ret;
519
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900520 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
521 *selector = i;
522
Axel Lin62bc4d42012-04-11 22:58:03 +0800523 return ret;
524}
525
526static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
527 unsigned int old_selector,
528 unsigned int new_selector)
529{
530 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
531 int rid = rdev_get_id(rdev);
532 const struct voltage_map_desc *desc = reg_voltage_map[rid];
533
534 /* Delay is required only if the voltage is increasing */
535 if (old_selector >= new_selector)
536 return 0;
537
538 /* No need to delay if gpio_dvs_mode */
539 switch (rid) {
540 case MAX8997_BUCK1:
541 if (max8997->buck1_gpiodvs)
542 return 0;
543 break;
544 case MAX8997_BUCK2:
545 if (max8997->buck2_gpiodvs)
546 return 0;
547 break;
548 case MAX8997_BUCK5:
549 if (max8997->buck5_gpiodvs)
550 return 0;
551 break;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900552 }
553
Axel Lin62bc4d42012-04-11 22:58:03 +0800554 switch (rid) {
555 case MAX8997_BUCK1:
556 case MAX8997_BUCK2:
557 case MAX8997_BUCK4:
558 case MAX8997_BUCK5:
559 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
560 max8997->ramp_delay);
561 }
562
563 return 0;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900564}
565
566/*
567 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
568 *
569 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
570 * of one of the bucks may affect that of another buck, which is the side
571 * effect of the change (set_voltage). This function examines the GPIO-DVS
572 * configurations and checks whether such side-effect exists.
573 */
574static int max8997_assess_side_effect(struct regulator_dev *rdev,
575 u8 new_val, int *best)
576{
577 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800578 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900579 u8 *buckx_val[3];
580 bool buckx_gpiodvs[3];
581 int side_effect[8];
582 int min_side_effect = INT_MAX;
583 int i;
584
585 *best = -1;
586
587 switch (rid) {
588 case MAX8997_BUCK1:
589 rid = 0;
590 break;
591 case MAX8997_BUCK2:
592 rid = 1;
593 break;
594 case MAX8997_BUCK5:
595 rid = 2;
596 break;
597 default:
598 return -EINVAL;
599 }
600
601 buckx_val[0] = max8997->buck1_vol;
602 buckx_val[1] = max8997->buck2_vol;
603 buckx_val[2] = max8997->buck5_vol;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900604 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
605 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
606 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900607
608 for (i = 0; i < 8; i++) {
609 int others;
610
611 if (new_val != (buckx_val[rid])[i]) {
612 side_effect[i] = -1;
613 continue;
614 }
615
616 side_effect[i] = 0;
617 for (others = 0; others < 3; others++) {
618 int diff;
619
620 if (others == rid)
621 continue;
622 if (buckx_gpiodvs[others] == false)
623 continue; /* Not affected */
624 diff = (buckx_val[others])[i] -
625 (buckx_val[others])[max8997->buck125_gpioindex];
626 if (diff > 0)
627 side_effect[i] += diff;
628 else if (diff < 0)
629 side_effect[i] -= diff;
630 }
631 if (side_effect[i] == 0) {
632 *best = i;
633 return 0; /* NO SIDE EFFECT! Use This! */
634 }
635 if (side_effect[i] < min_side_effect) {
636 min_side_effect = side_effect[i];
637 *best = i;
638 }
639 }
640
641 if (*best == -1)
642 return -EINVAL;
643
644 return side_effect[*best];
645}
646
647/*
648 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
649 * max8997_set_voltage_ldobuck to do the job.
650 */
651static int max8997_set_voltage_buck(struct regulator_dev *rdev,
652 int min_uV, int max_uV, unsigned *selector)
653{
654 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
Axel Linb3e13482012-03-01 09:26:27 +0800655 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900656 const struct voltage_map_desc *desc;
657 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
658 bool gpio_dvs_mode = false;
659 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
660
661 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
662 return -EINVAL;
663
664 switch (rid) {
665 case MAX8997_BUCK1:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900666 if (max8997->buck1_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900667 gpio_dvs_mode = true;
668 break;
669 case MAX8997_BUCK2:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900670 if (max8997->buck2_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900671 gpio_dvs_mode = true;
672 break;
673 case MAX8997_BUCK5:
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900674 if (max8997->buck5_gpiodvs)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900675 gpio_dvs_mode = true;
676 break;
677 }
678
679 if (!gpio_dvs_mode)
680 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
681 selector);
682
683 desc = reg_voltage_map[rid];
684 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
685 if (new_val < 0)
686 return new_val;
687
688 tmp_dmg = INT_MAX;
689 tmp_idx = -1;
690 tmp_val = -1;
691 do {
692 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
693 if (damage == 0)
694 goto out;
695
696 if (tmp_dmg > damage) {
697 tmp_idx = new_idx;
698 tmp_val = new_val;
699 tmp_dmg = damage;
700 }
701
702 new_val++;
Axel Linf55205f2012-04-11 20:53:58 +0800703 } while (desc->min + desc->step * new_val <= desc->max);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900704
705 new_idx = tmp_idx;
706 new_val = tmp_val;
707
MyungJoo Ham6e0414a2011-06-20 17:30:17 +0900708 if (max8997->ignore_gpiodvs_side_effect == false)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900709 return -EINVAL;
710
711 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
712 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
713
714out:
715 if (new_idx < 0 || new_val < 0)
716 return -EINVAL;
717
718 max8997->buck125_gpioindex = new_idx;
719 max8997_set_gpio(max8997);
720 *selector = new_val;
721
722 return 0;
723}
724
725static const int safeoutvolt[] = {
726 3300000,
727 4850000,
728 4900000,
729 4950000,
730};
731
732/* For SAFEOUT1 and SAFEOUT2 */
733static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
734 int min_uV, int max_uV, unsigned *selector)
735{
736 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
737 struct i2c_client *i2c = max8997->iodev->i2c;
Axel Linb3e13482012-03-01 09:26:27 +0800738 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900739 int reg, shift = 0, mask, ret;
740 int i = 0;
741 u8 val;
742
743 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
744 return -EINVAL;
745
746 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
747 if (min_uV <= safeoutvolt[i] &&
748 max_uV >= safeoutvolt[i])
749 break;
750 }
751
752 if (i >= ARRAY_SIZE(safeoutvolt))
753 return -EINVAL;
754
755 if (i == 0)
756 val = 0x3;
757 else
758 val = i - 1;
759
760 ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
761 if (ret)
762 return ret;
763
764 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
765 *selector = val;
766
767 return ret;
768}
769
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900770static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
771{
772 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
773 struct i2c_client *i2c = max8997->iodev->i2c;
774 int ret, reg, mask, pattern;
Axel Linb3e13482012-03-01 09:26:27 +0800775 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900776
777 ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
778 if (ret)
779 return ret;
780
781 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
782
783 if (rid == MAX8997_LDO1 ||
784 rid == MAX8997_LDO10 ||
785 rid == MAX8997_LDO21) {
786 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
787 rdev->desc->name);
788 return max8997_update_reg(i2c, reg, 0x40, mask);
789 }
790
791 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
792 rdev->desc->name, max8997->saved_states[rid] & mask,
793 (~pattern) & mask);
794 return max8997_update_reg(i2c, reg, ~pattern, mask);
795}
796
797static struct regulator_ops max8997_ldo_ops = {
798 .list_voltage = max8997_list_voltage,
799 .is_enabled = max8997_reg_is_enabled,
800 .enable = max8997_reg_enable,
801 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800802 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900803 .set_voltage = max8997_set_voltage_ldobuck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800804 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900805 .set_suspend_disable = max8997_reg_disable_suspend,
806};
807
808static struct regulator_ops max8997_buck_ops = {
809 .list_voltage = max8997_list_voltage,
810 .is_enabled = max8997_reg_is_enabled,
811 .enable = max8997_reg_enable,
812 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800813 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900814 .set_voltage = max8997_set_voltage_buck,
Axel Lin62bc4d42012-04-11 22:58:03 +0800815 .set_voltage_time_sel = max8997_set_voltage_ldobuck_time_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900816 .set_suspend_disable = max8997_reg_disable_suspend,
817};
818
819static struct regulator_ops max8997_fixedvolt_ops = {
820 .list_voltage = max8997_list_voltage,
821 .is_enabled = max8997_reg_is_enabled,
822 .enable = max8997_reg_enable,
823 .disable = max8997_reg_disable,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900824 .set_suspend_disable = max8997_reg_disable_suspend,
825};
826
827static struct regulator_ops max8997_safeout_ops = {
828 .list_voltage = max8997_list_voltage_safeout,
829 .is_enabled = max8997_reg_is_enabled,
830 .enable = max8997_reg_enable,
831 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800832 .get_voltage_sel = max8997_get_voltage_sel,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900833 .set_voltage = max8997_set_voltage_safeout,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900834 .set_suspend_disable = max8997_reg_disable_suspend,
835};
836
837static struct regulator_ops max8997_fixedstate_ops = {
838 .list_voltage = max8997_list_voltage_charger_cv,
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_charger_cv,
841};
842
Axel Lin9e96b3a2012-04-11 22:56:11 +0800843static int max8997_set_current_limit(struct regulator_dev *rdev,
844 int min_uA, int max_uA)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900845{
846 unsigned dummy;
Axel Lin9e96b3a2012-04-11 22:56:11 +0800847 int rid = rdev_get_id(rdev);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900848
Axel Lin9e96b3a2012-04-11 22:56:11 +0800849 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
850 return -EINVAL;
851
852 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
853 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900854}
855
Axel Lin9e96b3a2012-04-11 22:56:11 +0800856static int max8997_get_current_limit(struct regulator_dev *rdev)
857{
858 int sel, rid = rdev_get_id(rdev);
859
860 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
861 return -EINVAL;
862
863 sel = max8997_get_voltage_sel(rdev);
864 if (sel < 0)
865 return sel;
866
867 /* Reuse max8997_list_voltage to get current_limit. */
868 return max8997_list_voltage(rdev, sel);
869}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900870
871static struct regulator_ops max8997_charger_ops = {
872 .is_enabled = max8997_reg_is_enabled,
873 .enable = max8997_reg_enable,
874 .disable = max8997_reg_disable,
Axel Lin9e96b3a2012-04-11 22:56:11 +0800875 .get_current_limit = max8997_get_current_limit,
876 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900877};
878
879static struct regulator_ops max8997_charger_fixedstate_ops = {
Axel Lin9e96b3a2012-04-11 22:56:11 +0800880 .get_current_limit = max8997_get_current_limit,
881 .set_current_limit = max8997_set_current_limit,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900882};
883
Axel Lin4533f802012-03-13 14:53:58 +0800884#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
885 .name = #_name, \
886 .id = MAX8997_##_name, \
887 .ops = &_ops, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900888 .type = REGULATOR_VOLTAGE, \
889 .owner = THIS_MODULE, \
890}
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900891
Axel Lin4533f802012-03-13 14:53:58 +0800892#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
893 .name = #_name, \
894 .id = MAX8997_##_name, \
895 .ops = &_ops, \
896 .type = REGULATOR_CURRENT, \
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900897 .owner = THIS_MODULE, \
898}
899
900static struct regulator_desc regulators[] = {
Axel Lin4533f802012-03-13 14:53:58 +0800901 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
902 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
903 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
904 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
905 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
906 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
907 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
908 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
909 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
910 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
911 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
912 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
913 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
914 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
915 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
916 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
917 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
918 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
919 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
920 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
921 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
922 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
923 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
924 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
925 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
926 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
927 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
928 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
929 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
930 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
931 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
932 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
933 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
934 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
935 max8997_charger_fixedstate_ops),
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +0900936};
937
Thomas Abraham77b71b32012-11-27 14:04:32 +0530938#ifdef CONFIG_OF
939static int max8997_pmic_dt_parse_dvs_gpio(struct max8997_dev *iodev,
940 struct max8997_platform_data *pdata,
941 struct device_node *pmic_np)
942{
943 int i, gpio;
944
945 for (i = 0; i < 3; i++) {
946 gpio = of_get_named_gpio(pmic_np,
947 "max8997,pmic-buck125-dvs-gpios", i);
948 if (!gpio_is_valid(gpio)) {
949 dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
950 return -EINVAL;
951 }
952 pdata->buck125_gpios[i] = gpio;
953 }
954 return 0;
955}
956
957static int max8997_pmic_dt_parse_pdata(struct max8997_dev *iodev,
958 struct max8997_platform_data *pdata)
959{
960 struct device_node *pmic_np, *regulators_np, *reg_np;
961 struct max8997_regulator_data *rdata;
962 unsigned int i, dvs_voltage_nr = 1, ret;
963
964 pmic_np = iodev->dev->of_node;
965 if (!pmic_np) {
966 dev_err(iodev->dev, "could not find pmic sub-node\n");
967 return -ENODEV;
968 }
969
970 regulators_np = of_find_node_by_name(pmic_np, "regulators");
971 if (!regulators_np) {
972 dev_err(iodev->dev, "could not find regulators sub-node\n");
973 return -EINVAL;
974 }
975
976 /* count the number of regulators to be supported in pmic */
977 pdata->num_regulators = 0;
978 for_each_child_of_node(regulators_np, reg_np)
979 pdata->num_regulators++;
980
981 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
982 pdata->num_regulators, GFP_KERNEL);
983 if (!rdata) {
984 dev_err(iodev->dev, "could not allocate memory for "
985 "regulator data\n");
986 return -ENOMEM;
987 }
988
989 pdata->regulators = rdata;
990 for_each_child_of_node(regulators_np, reg_np) {
991 for (i = 0; i < ARRAY_SIZE(regulators); i++)
992 if (!of_node_cmp(reg_np->name, regulators[i].name))
993 break;
994
995 if (i == ARRAY_SIZE(regulators)) {
996 dev_warn(iodev->dev, "don't know how to configure "
997 "regulator %s\n", reg_np->name);
998 continue;
999 }
1000
1001 rdata->id = i;
1002 rdata->initdata = of_get_regulator_init_data(
1003 iodev->dev, reg_np);
1004 rdata->reg_node = reg_np;
1005 rdata++;
1006 }
1007
1008 if (of_get_property(pmic_np, "max8997,pmic-buck1-uses-gpio-dvs", NULL))
1009 pdata->buck1_gpiodvs = true;
1010
1011 if (of_get_property(pmic_np, "max8997,pmic-buck2-uses-gpio-dvs", NULL))
1012 pdata->buck2_gpiodvs = true;
1013
1014 if (of_get_property(pmic_np, "max8997,pmic-buck5-uses-gpio-dvs", NULL))
1015 pdata->buck5_gpiodvs = true;
1016
1017 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1018 pdata->buck5_gpiodvs) {
1019 ret = max8997_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
1020 if (ret)
1021 return -EINVAL;
1022
1023 if (of_property_read_u32(pmic_np,
1024 "max8997,pmic-buck125-default-dvs-idx",
1025 &pdata->buck125_default_idx)) {
1026 pdata->buck125_default_idx = 0;
1027 } else {
1028 if (pdata->buck125_default_idx >= 8) {
1029 pdata->buck125_default_idx = 0;
1030 dev_info(iodev->dev, "invalid value for "
1031 "default dvs index, using 0 instead\n");
1032 }
1033 }
1034
1035 if (of_get_property(pmic_np,
1036 "max8997,pmic-ignore-gpiodvs-side-effect", NULL))
1037 pdata->ignore_gpiodvs_side_effect = true;
1038
1039 dvs_voltage_nr = 8;
1040 }
1041
1042 if (of_property_read_u32_array(pmic_np,
1043 "max8997,pmic-buck1-dvs-voltage",
1044 pdata->buck1_voltage, dvs_voltage_nr)) {
1045 dev_err(iodev->dev, "buck1 voltages not specified\n");
1046 return -EINVAL;
1047 }
1048
1049 if (of_property_read_u32_array(pmic_np,
1050 "max8997,pmic-buck2-dvs-voltage",
1051 pdata->buck2_voltage, dvs_voltage_nr)) {
1052 dev_err(iodev->dev, "buck2 voltages not specified\n");
1053 return -EINVAL;
1054 }
1055
1056 if (of_property_read_u32_array(pmic_np,
1057 "max8997,pmic-buck5-dvs-voltage",
1058 pdata->buck5_voltage, dvs_voltage_nr)) {
1059 dev_err(iodev->dev, "buck5 voltages not specified\n");
1060 return -EINVAL;
1061 }
1062
1063 return 0;
1064}
1065#else
1066static int max8997_pmic_dt_parse_pdata(struct max8997_dev *iodev,
1067 struct max8997_platform_data *pdata)
1068{
1069 return 0;
1070}
1071#endif /* CONFIG_OF */
1072
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001073static __devinit int max8997_pmic_probe(struct platform_device *pdev)
1074{
1075 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Thomas Abraham77b71b32012-11-27 14:04:32 +05301076 struct max8997_platform_data *pdata = iodev->pdata;
Mark Brownc172708d2012-04-04 00:50:22 +01001077 struct regulator_config config = { };
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001078 struct regulator_dev **rdev;
1079 struct max8997_data *max8997;
1080 struct i2c_client *i2c;
Thomas Abraham068a8c82012-11-23 13:33:14 +05301081 int i, ret, size, nr_dvs;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001082 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
1083
Thomas Abraham77b71b32012-11-27 14:04:32 +05301084 if (IS_ERR_OR_NULL(pdata)) {
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001085 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
1086 return -ENODEV;
1087 }
1088
Thomas Abraham77b71b32012-11-27 14:04:32 +05301089 if (iodev->dev->of_node) {
1090 ret = max8997_pmic_dt_parse_pdata(iodev, pdata);
1091 if (ret)
1092 return ret;
1093 }
1094
Axel Lin8ae57672012-04-19 09:44:13 +08001095 max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
1096 GFP_KERNEL);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001097 if (!max8997)
1098 return -ENOMEM;
1099
1100 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Axel Lin8ae57672012-04-19 09:44:13 +08001101 max8997->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
1102 if (!max8997->rdev)
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001103 return -ENOMEM;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001104
1105 rdev = max8997->rdev;
1106 max8997->dev = &pdev->dev;
1107 max8997->iodev = iodev;
1108 max8997->num_regulators = pdata->num_regulators;
1109 platform_set_drvdata(pdev, max8997);
1110 i2c = max8997->iodev->i2c;
1111
1112 max8997->buck125_gpioindex = pdata->buck125_default_idx;
MyungJoo Ham6e0414a2011-06-20 17:30:17 +09001113 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
1114 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
1115 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
1116 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
1117 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001118
Thomas Abraham068a8c82012-11-23 13:33:14 +05301119 nr_dvs = (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1120 pdata->buck5_gpiodvs) ? 8 : 1;
1121
1122 for (i = 0; i < nr_dvs; i++) {
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001123 max8997->buck1_vol[i] = ret =
1124 max8997_get_voltage_proper_val(
1125 &buck1245_voltage_map_desc,
1126 pdata->buck1_voltage[i] / 1000,
1127 pdata->buck1_voltage[i] / 1000 +
1128 buck1245_voltage_map_desc.step);
1129 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +08001130 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001131
1132 max8997->buck2_vol[i] = ret =
1133 max8997_get_voltage_proper_val(
1134 &buck1245_voltage_map_desc,
1135 pdata->buck2_voltage[i] / 1000,
1136 pdata->buck2_voltage[i] / 1000 +
1137 buck1245_voltage_map_desc.step);
1138 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +08001139 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001140
1141 max8997->buck5_vol[i] = ret =
1142 max8997_get_voltage_proper_val(
1143 &buck1245_voltage_map_desc,
1144 pdata->buck5_voltage[i] / 1000,
1145 pdata->buck5_voltage[i] / 1000 +
1146 buck1245_voltage_map_desc.step);
1147 if (ret < 0)
Axel Lin8ae57672012-04-19 09:44:13 +08001148 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001149
1150 if (max_buck1 < max8997->buck1_vol[i])
1151 max_buck1 = max8997->buck1_vol[i];
1152 if (max_buck2 < max8997->buck2_vol[i])
1153 max_buck2 = max8997->buck2_vol[i];
1154 if (max_buck5 < max8997->buck5_vol[i])
1155 max_buck5 = max8997->buck5_vol[i];
1156 }
1157
1158 /* For the safety, set max voltage before setting up */
1159 for (i = 0; i < 8; i++) {
Axel Linecb9c4f2011-05-16 18:20:34 +08001160 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001161 max_buck1, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001162 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001163 max_buck2, 0x3f);
Axel Linecb9c4f2011-05-16 18:20:34 +08001164 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001165 max_buck5, 0x3f);
1166 }
1167
Thomas Abraham11ec7bf02012-11-23 13:33:13 +05301168 /* Initialize all the DVS related BUCK registers */
Thomas Abraham068a8c82012-11-23 13:33:14 +05301169 for (i = 0; i < nr_dvs; i++) {
Thomas Abraham11ec7bf02012-11-23 13:33:13 +05301170 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1171 max8997->buck1_vol[i],
1172 0x3f);
1173 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1174 max8997->buck2_vol[i],
1175 0x3f);
1176 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1177 max8997->buck5_vol[i],
1178 0x3f);
1179 }
1180
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001181 /*
1182 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1183 * If at least one of them cares, set gpios.
1184 */
1185 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1186 pdata->buck5_gpiodvs) {
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001187
1188 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1189 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1190 !gpio_is_valid(pdata->buck125_gpios[2])) {
1191 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1192 ret = -EINVAL;
Axel Lin8ae57672012-04-19 09:44:13 +08001193 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001194 }
1195
Axel Lin8fa25ed2012-07-05 23:06:57 +08001196 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
1197 "MAX8997 SET1");
1198 if (ret)
Axel Lin8ae57672012-04-19 09:44:13 +08001199 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001200
Axel Lin8fa25ed2012-07-05 23:06:57 +08001201 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
1202 "MAX8997 SET2");
1203 if (ret)
Axel Lin8ae57672012-04-19 09:44:13 +08001204 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001205
Axel Lin8fa25ed2012-07-05 23:06:57 +08001206 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001207 "MAX8997 SET3");
Axel Lin8fa25ed2012-07-05 23:06:57 +08001208 if (ret)
Axel Lin8ae57672012-04-19 09:44:13 +08001209 goto err_out;
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001210
1211 gpio_direction_output(pdata->buck125_gpios[0],
1212 (max8997->buck125_gpioindex >> 2)
1213 & 0x1); /* SET1 */
1214 gpio_direction_output(pdata->buck125_gpios[1],
1215 (max8997->buck125_gpioindex >> 1)
1216 & 0x1); /* SET2 */
1217 gpio_direction_output(pdata->buck125_gpios[2],
1218 (max8997->buck125_gpioindex >> 0)
1219 & 0x1); /* SET3 */
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001220 }
1221
1222 /* DVS-GPIO disabled */
1223 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1224 (1 << 1) : (0 << 1), 1 << 1);
1225 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1226 (1 << 1) : (0 << 1), 1 << 1);
1227 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1228 (1 << 1) : (0 << 1), 1 << 1);
1229
Tushar Beheradbb48e72011-06-21 09:08:59 +05301230 /* Misc Settings */
1231 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1232 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1233
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001234 for (i = 0; i < pdata->num_regulators; i++) {
1235 const struct voltage_map_desc *desc;
1236 int id = pdata->regulators[i].id;
1237
1238 desc = reg_voltage_map[id];
1239 if (desc)
1240 regulators[id].n_voltages =
1241 (desc->max - desc->min) / desc->step + 1;
1242 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1243 regulators[id].n_voltages = 4;
1244 else if (id == MAX8997_CHARGER_CV)
1245 regulators[id].n_voltages = 16;
1246
Mark Brownc172708d2012-04-04 00:50:22 +01001247 config.dev = max8997->dev;
1248 config.init_data = pdata->regulators[i].initdata;
1249 config.driver_data = max8997;
Thomas Abraham77b71b32012-11-27 14:04:32 +05301250 config.of_node = pdata->regulators[i].reg_node;
Mark Brownc172708d2012-04-04 00:50:22 +01001251
1252 rdev[i] = regulator_register(&regulators[id], &config);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001253 if (IS_ERR(rdev[i])) {
1254 ret = PTR_ERR(rdev[i]);
1255 dev_err(max8997->dev, "regulator init failed for %d\n",
1256 id);
1257 rdev[i] = NULL;
1258 goto err;
1259 }
1260 }
1261
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001262 return 0;
1263err:
Axel Lin8ae57672012-04-19 09:44:13 +08001264 while (--i >= 0)
1265 regulator_unregister(rdev[i]);
1266err_out:
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001267 return ret;
1268}
1269
1270static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1271{
1272 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1273 struct regulator_dev **rdev = max8997->rdev;
1274 int i;
1275
1276 for (i = 0; i < max8997->num_regulators; i++)
Axel Lin8ae57672012-04-19 09:44:13 +08001277 regulator_unregister(rdev[i]);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001278 return 0;
1279}
1280
1281static const struct platform_device_id max8997_pmic_id[] = {
1282 { "max8997-pmic", 0},
1283 { },
1284};
Axel Lina51b9072011-03-26 23:28:42 +08001285MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
MyungJoo Hambd6ca2c2011-03-11 11:34:44 +09001286
1287static struct platform_driver max8997_pmic_driver = {
1288 .driver = {
1289 .name = "max8997-pmic",
1290 .owner = THIS_MODULE,
1291 },
1292 .probe = max8997_pmic_probe,
1293 .remove = __devexit_p(max8997_pmic_remove),
1294 .id_table = max8997_pmic_id,
1295};
1296
1297static int __init max8997_pmic_init(void)
1298{
1299 return platform_driver_register(&max8997_pmic_driver);
1300}
1301subsys_initcall(max8997_pmic_init);
1302
1303static void __exit max8997_pmic_cleanup(void)
1304{
1305 platform_driver_unregister(&max8997_pmic_driver);
1306}
1307module_exit(max8997_pmic_cleanup);
1308
1309MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1310MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1311MODULE_LICENSE("GPL");