blob: 5890265eeacc092505f66506db187241b52065c3 [file] [log] [blame]
Kyungmin Park156f2522010-06-16 09:04:16 +02001/*
2 * max8998.c - Voltage regulator driver for the Maxim 8998
3 *
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 * Marek Szyprowski <m.szyprowski@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/i2c.h>
26#include <linux/err.h>
27#include <linux/gpio.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/mutex.h>
MyungJoo Hamc5a46552010-07-20 08:07:07 +020031#include <linux/delay.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020032#include <linux/platform_device.h>
33#include <linux/regulator/driver.h>
34#include <linux/mfd/max8998.h>
35#include <linux/mfd/max8998-private.h>
36
37struct max8998_data {
38 struct device *dev;
39 struct max8998_dev *iodev;
40 int num_regulators;
41 struct regulator_dev **rdev;
Lukasz Majewski889cd5a2010-09-27 14:32:25 +020042 u8 buck1_vol[4]; /* voltages for selection */
43 u8 buck2_vol[2];
44 unsigned int buck1_idx; /* index to last changed voltage */
45 /* value in a set */
46 unsigned int buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +020047};
48
49struct voltage_map_desc {
50 int min;
51 int max;
52 int step;
53};
54
55/* Voltage maps */
56static const struct voltage_map_desc ldo23_voltage_map_desc = {
57 .min = 800, .step = 50, .max = 1300,
58};
59static const struct voltage_map_desc ldo456711_voltage_map_desc = {
60 .min = 1600, .step = 100, .max = 3600,
61};
62static const struct voltage_map_desc ldo8_voltage_map_desc = {
63 .min = 3000, .step = 100, .max = 3600,
64};
65static const struct voltage_map_desc ldo9_voltage_map_desc = {
66 .min = 2800, .step = 100, .max = 3100,
67};
68static const struct voltage_map_desc ldo10_voltage_map_desc = {
69 .min = 950, .step = 50, .max = 1300,
70};
71static const struct voltage_map_desc ldo1213_voltage_map_desc = {
72 .min = 800, .step = 100, .max = 3300,
73};
74static const struct voltage_map_desc ldo1415_voltage_map_desc = {
75 .min = 1200, .step = 100, .max = 3300,
76};
77static const struct voltage_map_desc ldo1617_voltage_map_desc = {
78 .min = 1600, .step = 100, .max = 3600,
79};
80static const struct voltage_map_desc buck12_voltage_map_desc = {
81 .min = 750, .step = 25, .max = 1525,
82};
83static const struct voltage_map_desc buck3_voltage_map_desc = {
84 .min = 1600, .step = 100, .max = 3600,
85};
86static const struct voltage_map_desc buck4_voltage_map_desc = {
87 .min = 800, .step = 100, .max = 2300,
88};
89
90static const struct voltage_map_desc *ldo_voltage_map[] = {
91 NULL,
92 NULL,
93 &ldo23_voltage_map_desc, /* LDO2 */
94 &ldo23_voltage_map_desc, /* LDO3 */
95 &ldo456711_voltage_map_desc, /* LDO4 */
96 &ldo456711_voltage_map_desc, /* LDO5 */
97 &ldo456711_voltage_map_desc, /* LDO6 */
98 &ldo456711_voltage_map_desc, /* LDO7 */
99 &ldo8_voltage_map_desc, /* LDO8 */
100 &ldo9_voltage_map_desc, /* LDO9 */
101 &ldo10_voltage_map_desc, /* LDO10 */
102 &ldo456711_voltage_map_desc, /* LDO11 */
103 &ldo1213_voltage_map_desc, /* LDO12 */
104 &ldo1213_voltage_map_desc, /* LDO13 */
105 &ldo1415_voltage_map_desc, /* LDO14 */
106 &ldo1415_voltage_map_desc, /* LDO15 */
107 &ldo1617_voltage_map_desc, /* LDO16 */
108 &ldo1617_voltage_map_desc, /* LDO17 */
109 &buck12_voltage_map_desc, /* BUCK1 */
110 &buck12_voltage_map_desc, /* BUCK2 */
111 &buck3_voltage_map_desc, /* BUCK3 */
112 &buck4_voltage_map_desc, /* BUCK4 */
113};
114
Kyungmin Park156f2522010-06-16 09:04:16 +0200115static int max8998_list_voltage(struct regulator_dev *rdev,
116 unsigned int selector)
117{
118 const struct voltage_map_desc *desc;
Axel Lin7b947912012-03-01 09:27:29 +0800119 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200120 int val;
121
Axel Lin1b69d8e2010-08-04 14:34:10 +0800122 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
Kyungmin Park156f2522010-06-16 09:04:16 +0200123 return -EINVAL;
124
125 desc = ldo_voltage_map[ldo];
126 if (desc == NULL)
127 return -EINVAL;
128
129 val = desc->min + desc->step * selector;
130 if (val > desc->max)
131 return -EINVAL;
132
133 return val * 1000;
134}
135
136static int max8998_get_enable_register(struct regulator_dev *rdev,
137 int *reg, int *shift)
138{
Axel Lin7b947912012-03-01 09:27:29 +0800139 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200140
141 switch (ldo) {
142 case MAX8998_LDO2 ... MAX8998_LDO5:
143 *reg = MAX8998_REG_ONOFF1;
144 *shift = 3 - (ldo - MAX8998_LDO2);
145 break;
146 case MAX8998_LDO6 ... MAX8998_LDO13:
147 *reg = MAX8998_REG_ONOFF2;
148 *shift = 7 - (ldo - MAX8998_LDO6);
149 break;
150 case MAX8998_LDO14 ... MAX8998_LDO17:
151 *reg = MAX8998_REG_ONOFF3;
152 *shift = 7 - (ldo - MAX8998_LDO14);
153 break;
154 case MAX8998_BUCK1 ... MAX8998_BUCK4:
155 *reg = MAX8998_REG_ONOFF1;
156 *shift = 7 - (ldo - MAX8998_BUCK1);
157 break;
158 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
159 *reg = MAX8998_REG_ONOFF4;
160 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
161 break;
162 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
163 *reg = MAX8998_REG_CHGR2;
164 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
165 break;
166 default:
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
173static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
174{
175 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900176 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200177 int ret, reg, shift = 8;
178 u8 val;
179
180 ret = max8998_get_enable_register(rdev, &reg, &shift);
181 if (ret)
182 return ret;
183
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900184 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200185 if (ret)
186 return ret;
187
188 return val & (1 << shift);
189}
190
191static int max8998_ldo_enable(struct regulator_dev *rdev)
192{
193 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900194 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200195 int reg, shift = 8, ret;
196
197 ret = max8998_get_enable_register(rdev, &reg, &shift);
198 if (ret)
199 return ret;
200
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900201 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200202}
203
204static int max8998_ldo_disable(struct regulator_dev *rdev)
205{
206 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900207 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200208 int reg, shift = 8, ret;
209
210 ret = max8998_get_enable_register(rdev, &reg, &shift);
211 if (ret)
212 return ret;
213
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900214 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200215}
216
217static int max8998_get_voltage_register(struct regulator_dev *rdev,
218 int *_reg, int *_shift, int *_mask)
219{
Axel Lin7b947912012-03-01 09:27:29 +0800220 int ldo = rdev_get_id(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200221 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200222 int reg, shift = 0, mask = 0xff;
223
224 switch (ldo) {
225 case MAX8998_LDO2 ... MAX8998_LDO3:
226 reg = MAX8998_REG_LDO2_LDO3;
227 mask = 0xf;
228 if (ldo == MAX8998_LDO2)
229 shift = 4;
230 else
231 shift = 0;
232 break;
233 case MAX8998_LDO4 ... MAX8998_LDO7:
234 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
235 break;
236 case MAX8998_LDO8 ... MAX8998_LDO9:
237 reg = MAX8998_REG_LDO8_LDO9;
238 mask = 0xf;
239 if (ldo == MAX8998_LDO8)
240 shift = 4;
241 else
242 shift = 0;
243 break;
244 case MAX8998_LDO10 ... MAX8998_LDO11:
245 reg = MAX8998_REG_LDO10_LDO11;
246 if (ldo == MAX8998_LDO10) {
247 shift = 5;
248 mask = 0x7;
249 } else {
250 shift = 0;
251 mask = 0x1f;
252 }
253 break;
254 case MAX8998_LDO12 ... MAX8998_LDO17:
255 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
256 break;
257 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200258 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200259 break;
260 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200261 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200262 break;
263 case MAX8998_BUCK3:
264 reg = MAX8998_REG_BUCK3;
265 break;
266 case MAX8998_BUCK4:
267 reg = MAX8998_REG_BUCK4;
268 break;
269 default:
270 return -EINVAL;
271 }
272
273 *_reg = reg;
274 *_shift = shift;
275 *_mask = mask;
276
277 return 0;
278}
279
280static int max8998_get_voltage(struct regulator_dev *rdev)
281{
282 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900283 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200284 int reg, shift = 0, mask, ret;
285 u8 val;
286
287 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
288 if (ret)
289 return ret;
290
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900291 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200292 if (ret)
293 return ret;
294
295 val >>= shift;
296 val &= mask;
297
298 return max8998_list_voltage(rdev, val);
299}
300
Lukasz Majewski9d924922010-09-27 14:32:23 +0200301static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000302 int min_uV, int max_uV, unsigned *selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200303{
304 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
305 struct i2c_client *i2c = max8998->iodev->i2c;
306 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
307 const struct voltage_map_desc *desc;
Axel Lin7b947912012-03-01 09:27:29 +0800308 int ldo = rdev_get_id(rdev);
Lukasz Majewski9d924922010-09-27 14:32:23 +0200309 int reg, shift = 0, mask, ret;
310 int i = 0;
311
312 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
313 return -EINVAL;
314
315 desc = ldo_voltage_map[ldo];
316 if (desc == NULL)
317 return -EINVAL;
318
319 if (max_vol < desc->min || min_vol > desc->max)
320 return -EINVAL;
321
322 while (desc->min + desc->step*i < min_vol &&
323 desc->min + desc->step*i < desc->max)
324 i++;
325
326 if (desc->min + desc->step*i > max_vol)
327 return -EINVAL;
328
Mark Brown3a93f2a2010-11-10 14:38:29 +0000329 *selector = i;
330
Lukasz Majewski9d924922010-09-27 14:32:23 +0200331 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
332 if (ret)
333 return ret;
334
335 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
336
337 return ret;
338}
339
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200340static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
341{
342 gpio_set_value(gpio1, v & 0x1);
343 gpio_set_value(gpio2, (v >> 1) & 0x1);
344}
345
346static inline void buck2_gpio_set(int gpio, int v)
347{
348 gpio_set_value(gpio, v & 0x1);
349}
350
Lukasz Majewski9d924922010-09-27 14:32:23 +0200351static int max8998_set_voltage_buck(struct regulator_dev *rdev,
Mark Brownf4d6adf2010-11-19 12:07:44 +0000352 int min_uV, int max_uV, unsigned *selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200353{
354 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200355 struct max8998_platform_data *pdata =
356 dev_get_platdata(max8998->iodev->dev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900357 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200358 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
359 const struct voltage_map_desc *desc;
Axel Lin7b947912012-03-01 09:27:29 +0800360 int buck = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200361 int reg, shift = 0, mask, ret;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200362 int difference = 0, i = 0, j = 0, previous_vol = 0;
363 u8 val = 0;
364 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200365
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200366 if (buck >= ARRAY_SIZE(ldo_voltage_map))
Kyungmin Park156f2522010-06-16 09:04:16 +0200367 return -EINVAL;
368
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200369 desc = ldo_voltage_map[buck];
370
Kyungmin Park156f2522010-06-16 09:04:16 +0200371 if (desc == NULL)
372 return -EINVAL;
373
374 if (max_vol < desc->min || min_vol > desc->max)
375 return -EINVAL;
376
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200377 while (desc->min + desc->step*i < min_vol &&
Kyungmin Park156f2522010-06-16 09:04:16 +0200378 desc->min + desc->step*i < desc->max)
379 i++;
380
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200381 if (desc->min + desc->step*i > max_vol)
382 return -EINVAL;
383
Mark Brown3a93f2a2010-11-10 14:38:29 +0000384 *selector = i;
385
Kyungmin Park156f2522010-06-16 09:04:16 +0200386 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
387 if (ret)
388 return ret;
389
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200390 previous_vol = max8998_get_voltage(rdev);
391
392 /* Check if voltage needs to be changed */
393 /* if previous_voltage equal new voltage, return */
394 if (previous_vol == max8998_list_voltage(rdev, i)) {
395 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
396 previous_vol, max8998_list_voltage(rdev, i));
397 return ret;
398 }
399
400 switch (buck) {
401 case MAX8998_BUCK1:
402 dev_dbg(max8998->dev,
Joe Perches85ee7a12011-04-23 20:38:19 -0700403 "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n"
404 "buck1_vol3:%d, buck1_vol4:%d\n",
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200405 i, max8998->buck1_vol[0], max8998->buck1_vol[1],
406 max8998->buck1_vol[2], max8998->buck1_vol[3]);
407
408 if (gpio_is_valid(pdata->buck1_set1) &&
409 gpio_is_valid(pdata->buck1_set2)) {
410
411 /* check if requested voltage */
412 /* value is already defined */
413 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
414 if (max8998->buck1_vol[j] == i) {
415 max8998->buck1_idx = j;
416 buck1_gpio_set(pdata->buck1_set1,
417 pdata->buck1_set2, j);
418 goto buck1_exit;
419 }
420 }
421
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100422 if (pdata->buck_voltage_lock)
423 return -EINVAL;
424
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200425 /* no predefine regulator found */
426 max8998->buck1_idx = (buck1_last_val % 2) + 2;
427 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
428 max8998->buck1_idx);
429 max8998->buck1_vol[max8998->buck1_idx] = i;
430 ret = max8998_get_voltage_register(rdev, &reg,
431 &shift,
432 &mask);
433 ret = max8998_write_reg(i2c, reg, i);
434 buck1_gpio_set(pdata->buck1_set1,
435 pdata->buck1_set2, max8998->buck1_idx);
436 buck1_last_val++;
437buck1_exit:
438 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
439 i2c->name, gpio_get_value(pdata->buck1_set1),
440 gpio_get_value(pdata->buck1_set2));
441 break;
442 } else {
443 ret = max8998_write_reg(i2c, reg, i);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200444 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200445 break;
446
447 case MAX8998_BUCK2:
448 dev_dbg(max8998->dev,
449 "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
450 , i, max8998->buck2_vol[0], max8998->buck2_vol[1]);
451 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100452
453 /* check if requested voltage */
454 /* value is already defined */
455 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
456 if (max8998->buck2_vol[j] == i) {
457 max8998->buck2_idx = j;
458 buck2_gpio_set(pdata->buck2_set3, j);
459 goto buck2_exit;
460 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200461 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100462
463 if (pdata->buck_voltage_lock)
464 return -EINVAL;
465
466 max8998_get_voltage_register(rdev,
467 &reg, &shift, &mask);
468 ret = max8998_write_reg(i2c, reg, i);
469 max8998->buck2_vol[max8998->buck2_idx] = i;
470 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
471buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200472 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
473 gpio_get_value(pdata->buck2_set3));
474 } else {
475 ret = max8998_write_reg(i2c, reg, i);
476 }
477 break;
478
479 case MAX8998_BUCK3:
480 case MAX8998_BUCK4:
481 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
482 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200483 }
484
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200485 /* Voltage stabilization */
486 max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200487
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200488 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
489 /* MAX8998 has ENRAMP bit implemented, so test it*/
490 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
491 return ret;
492
493 difference = desc->min + desc->step*i - previous_vol/1000;
494 if (difference > 0)
495 udelay(difference / ((val & 0x0f) + 1));
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200496
497 return ret;
Kyungmin Park156f2522010-06-16 09:04:16 +0200498}
499
500static struct regulator_ops max8998_ldo_ops = {
501 .list_voltage = max8998_list_voltage,
502 .is_enabled = max8998_ldo_is_enabled,
503 .enable = max8998_ldo_enable,
504 .disable = max8998_ldo_disable,
505 .get_voltage = max8998_get_voltage,
Lukasz Majewski9d924922010-09-27 14:32:23 +0200506 .set_voltage = max8998_set_voltage_ldo,
Kyungmin Park156f2522010-06-16 09:04:16 +0200507 .set_suspend_enable = max8998_ldo_enable,
508 .set_suspend_disable = max8998_ldo_disable,
509};
510
511static struct regulator_ops max8998_buck_ops = {
512 .list_voltage = max8998_list_voltage,
513 .is_enabled = max8998_ldo_is_enabled,
514 .enable = max8998_ldo_enable,
515 .disable = max8998_ldo_disable,
516 .get_voltage = max8998_get_voltage,
Lukasz Majewski9d924922010-09-27 14:32:23 +0200517 .set_voltage = max8998_set_voltage_buck,
Kyungmin Park156f2522010-06-16 09:04:16 +0200518 .set_suspend_enable = max8998_ldo_enable,
519 .set_suspend_disable = max8998_ldo_disable,
520};
521
522static struct regulator_ops max8998_others_ops = {
523 .is_enabled = max8998_ldo_is_enabled,
524 .enable = max8998_ldo_enable,
525 .disable = max8998_ldo_disable,
526 .set_suspend_enable = max8998_ldo_enable,
527 .set_suspend_disable = max8998_ldo_disable,
528};
529
530static struct regulator_desc regulators[] = {
531 {
532 .name = "LDO2",
533 .id = MAX8998_LDO2,
534 .ops = &max8998_ldo_ops,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 }, {
538 .name = "LDO3",
539 .id = MAX8998_LDO3,
540 .ops = &max8998_ldo_ops,
541 .type = REGULATOR_VOLTAGE,
542 .owner = THIS_MODULE,
543 }, {
544 .name = "LDO4",
545 .id = MAX8998_LDO4,
546 .ops = &max8998_ldo_ops,
547 .type = REGULATOR_VOLTAGE,
548 .owner = THIS_MODULE,
549 }, {
550 .name = "LDO5",
551 .id = MAX8998_LDO5,
552 .ops = &max8998_ldo_ops,
553 .type = REGULATOR_VOLTAGE,
554 .owner = THIS_MODULE,
555 }, {
556 .name = "LDO6",
557 .id = MAX8998_LDO6,
558 .ops = &max8998_ldo_ops,
559 .type = REGULATOR_VOLTAGE,
560 .owner = THIS_MODULE,
561 }, {
562 .name = "LDO7",
563 .id = MAX8998_LDO7,
564 .ops = &max8998_ldo_ops,
565 .type = REGULATOR_VOLTAGE,
566 .owner = THIS_MODULE,
567 }, {
568 .name = "LDO8",
569 .id = MAX8998_LDO8,
570 .ops = &max8998_ldo_ops,
571 .type = REGULATOR_VOLTAGE,
572 .owner = THIS_MODULE,
573 }, {
574 .name = "LDO9",
575 .id = MAX8998_LDO9,
576 .ops = &max8998_ldo_ops,
577 .type = REGULATOR_VOLTAGE,
578 .owner = THIS_MODULE,
579 }, {
580 .name = "LDO10",
581 .id = MAX8998_LDO10,
582 .ops = &max8998_ldo_ops,
583 .type = REGULATOR_VOLTAGE,
584 .owner = THIS_MODULE,
585 }, {
586 .name = "LDO11",
587 .id = MAX8998_LDO11,
588 .ops = &max8998_ldo_ops,
589 .type = REGULATOR_VOLTAGE,
590 .owner = THIS_MODULE,
591 }, {
592 .name = "LDO12",
593 .id = MAX8998_LDO12,
594 .ops = &max8998_ldo_ops,
595 .type = REGULATOR_VOLTAGE,
596 .owner = THIS_MODULE,
597 }, {
598 .name = "LDO13",
599 .id = MAX8998_LDO13,
600 .ops = &max8998_ldo_ops,
601 .type = REGULATOR_VOLTAGE,
602 .owner = THIS_MODULE,
603 }, {
604 .name = "LDO14",
605 .id = MAX8998_LDO14,
606 .ops = &max8998_ldo_ops,
607 .type = REGULATOR_VOLTAGE,
608 .owner = THIS_MODULE,
609 }, {
610 .name = "LDO15",
611 .id = MAX8998_LDO15,
612 .ops = &max8998_ldo_ops,
613 .type = REGULATOR_VOLTAGE,
614 .owner = THIS_MODULE,
615 }, {
616 .name = "LDO16",
617 .id = MAX8998_LDO16,
618 .ops = &max8998_ldo_ops,
619 .type = REGULATOR_VOLTAGE,
620 .owner = THIS_MODULE,
621 }, {
622 .name = "LDO17",
623 .id = MAX8998_LDO17,
624 .ops = &max8998_ldo_ops,
625 .type = REGULATOR_VOLTAGE,
626 .owner = THIS_MODULE,
627 }, {
628 .name = "BUCK1",
629 .id = MAX8998_BUCK1,
630 .ops = &max8998_buck_ops,
631 .type = REGULATOR_VOLTAGE,
632 .owner = THIS_MODULE,
633 }, {
634 .name = "BUCK2",
635 .id = MAX8998_BUCK2,
636 .ops = &max8998_buck_ops,
637 .type = REGULATOR_VOLTAGE,
638 .owner = THIS_MODULE,
639 }, {
640 .name = "BUCK3",
641 .id = MAX8998_BUCK3,
642 .ops = &max8998_buck_ops,
643 .type = REGULATOR_VOLTAGE,
644 .owner = THIS_MODULE,
645 }, {
646 .name = "BUCK4",
647 .id = MAX8998_BUCK4,
648 .ops = &max8998_buck_ops,
649 .type = REGULATOR_VOLTAGE,
650 .owner = THIS_MODULE,
651 }, {
652 .name = "EN32KHz AP",
653 .id = MAX8998_EN32KHZ_AP,
654 .ops = &max8998_others_ops,
655 .type = REGULATOR_VOLTAGE,
656 .owner = THIS_MODULE,
657 }, {
658 .name = "EN32KHz CP",
659 .id = MAX8998_EN32KHZ_CP,
660 .ops = &max8998_others_ops,
661 .type = REGULATOR_VOLTAGE,
662 .owner = THIS_MODULE,
663 }, {
664 .name = "ENVICHG",
665 .id = MAX8998_ENVICHG,
666 .ops = &max8998_others_ops,
667 .type = REGULATOR_VOLTAGE,
668 .owner = THIS_MODULE,
669 }, {
670 .name = "ESAFEOUT1",
671 .id = MAX8998_ESAFEOUT1,
672 .ops = &max8998_others_ops,
673 .type = REGULATOR_VOLTAGE,
674 .owner = THIS_MODULE,
675 }, {
676 .name = "ESAFEOUT2",
677 .id = MAX8998_ESAFEOUT2,
678 .ops = &max8998_others_ops,
679 .type = REGULATOR_VOLTAGE,
680 .owner = THIS_MODULE,
681 }
682};
683
684static __devinit int max8998_pmic_probe(struct platform_device *pdev)
685{
686 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
687 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
688 struct regulator_dev **rdev;
689 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200690 struct i2c_client *i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200691 int i, ret, size;
692
693 if (!pdata) {
694 dev_err(pdev->dev.parent, "No platform init data supplied\n");
695 return -ENODEV;
696 }
697
698 max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
699 if (!max8998)
700 return -ENOMEM;
701
Axel Lin11fa0d12010-08-19 10:29:19 +0800702 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200703 max8998->rdev = kzalloc(size, GFP_KERNEL);
704 if (!max8998->rdev) {
705 kfree(max8998);
706 return -ENOMEM;
707 }
708
709 rdev = max8998->rdev;
Axel Lin747cc852010-08-27 16:37:34 +0800710 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200711 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800712 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200713 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200714 i2c = max8998->iodev->i2c;
715
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100716 max8998->buck1_idx = pdata->buck1_default_idx;
717 max8998->buck2_idx = pdata->buck2_default_idx;
718
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200719 /* NOTE: */
720 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
721 /* will be displayed */
722
723 /* Check if MAX8998 voltage selection GPIOs are defined */
724 if (gpio_is_valid(pdata->buck1_set1) &&
725 gpio_is_valid(pdata->buck1_set2)) {
726 /* Check if SET1 is not equal to 0 */
727 if (!pdata->buck1_set1) {
728 printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
729 WARN_ON(!pdata->buck1_set1);
Axel Lin6be6e962011-05-13 14:54:06 +0800730 ret = -EIO;
731 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200732 }
733 /* Check if SET2 is not equal to 0 */
734 if (!pdata->buck1_set2) {
735 printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
736 WARN_ON(!pdata->buck1_set2);
Axel Lin6be6e962011-05-13 14:54:06 +0800737 ret = -EIO;
738 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200739 }
740
741 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
742 gpio_direction_output(pdata->buck1_set1,
743 max8998->buck1_idx & 0x1);
744
745
746 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
747 gpio_direction_output(pdata->buck1_set2,
748 (max8998->buck1_idx >> 1) & 0x1);
749 /* Set predefined value for BUCK1 register 1 */
750 i = 0;
751 while (buck12_voltage_map_desc.min +
752 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100753 < (pdata->buck1_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200754 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200755 max8998->buck1_vol[0] = i;
756 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100757 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800758 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200759
760 /* Set predefined value for BUCK1 register 2 */
761 i = 0;
762 while (buck12_voltage_map_desc.min +
763 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100764 < (pdata->buck1_voltage2 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200765 i++;
766
767 max8998->buck1_vol[1] = i;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100768 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
769 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800770 goto err_free_mem;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100771
772 /* Set predefined value for BUCK1 register 3 */
773 i = 0;
774 while (buck12_voltage_map_desc.min +
775 buck12_voltage_map_desc.step*i
776 < (pdata->buck1_voltage3 / 1000))
777 i++;
778
779 max8998->buck1_vol[2] = i;
780 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
781 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800782 goto err_free_mem;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100783
784 /* Set predefined value for BUCK1 register 4 */
785 i = 0;
786 while (buck12_voltage_map_desc.min +
787 buck12_voltage_map_desc.step*i
788 < (pdata->buck1_voltage4 / 1000))
789 i++;
790
791 max8998->buck1_vol[3] = i;
792 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200793 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800794 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200795
796 }
797
798 if (gpio_is_valid(pdata->buck2_set3)) {
799 /* Check if SET3 is not equal to 0 */
800 if (!pdata->buck2_set3) {
801 printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
802 WARN_ON(!pdata->buck2_set3);
Axel Lin6be6e962011-05-13 14:54:06 +0800803 ret = -EIO;
804 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200805 }
806 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
807 gpio_direction_output(pdata->buck2_set3,
808 max8998->buck2_idx & 0x1);
809
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100810 /* BUCK2 register 1 */
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200811 i = 0;
812 while (buck12_voltage_map_desc.min +
813 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100814 < (pdata->buck2_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200815 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200816 max8998->buck2_vol[0] = i;
817 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
818 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800819 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200820
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100821 /* BUCK2 register 2 */
822 i = 0;
823 while (buck12_voltage_map_desc.min +
824 buck12_voltage_map_desc.step*i
825 < (pdata->buck2_voltage2 / 1000))
826 i++;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100827 max8998->buck2_vol[1] = i;
828 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
829 if (ret)
Axel Lin6be6e962011-05-13 14:54:06 +0800830 goto err_free_mem;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200831 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200832
833 for (i = 0; i < pdata->num_regulators; i++) {
834 const struct voltage_map_desc *desc;
835 int id = pdata->regulators[i].id;
836 int index = id - MAX8998_LDO2;
837
838 desc = ldo_voltage_map[id];
839 if (desc && regulators[index].ops != &max8998_others_ops) {
840 int count = (desc->max - desc->min) / desc->step + 1;
841 regulators[index].n_voltages = count;
842 }
843 rdev[i] = regulator_register(&regulators[index], max8998->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530844 pdata->regulators[i].initdata, max8998, NULL);
Kyungmin Park156f2522010-06-16 09:04:16 +0200845 if (IS_ERR(rdev[i])) {
846 ret = PTR_ERR(rdev[i]);
847 dev_err(max8998->dev, "regulator init failed\n");
848 rdev[i] = NULL;
849 goto err;
850 }
851 }
852
853
854 return 0;
855err:
Axel Lin11fa0d12010-08-19 10:29:19 +0800856 for (i = 0; i < max8998->num_regulators; i++)
Kyungmin Park156f2522010-06-16 09:04:16 +0200857 if (rdev[i])
858 regulator_unregister(rdev[i]);
859
Axel Lin6be6e962011-05-13 14:54:06 +0800860err_free_mem:
Kyungmin Park156f2522010-06-16 09:04:16 +0200861 kfree(max8998->rdev);
862 kfree(max8998);
863
864 return ret;
865}
866
867static int __devexit max8998_pmic_remove(struct platform_device *pdev)
868{
869 struct max8998_data *max8998 = platform_get_drvdata(pdev);
870 struct regulator_dev **rdev = max8998->rdev;
871 int i;
872
Axel Lin11fa0d12010-08-19 10:29:19 +0800873 for (i = 0; i < max8998->num_regulators; i++)
Kyungmin Park156f2522010-06-16 09:04:16 +0200874 if (rdev[i])
875 regulator_unregister(rdev[i]);
876
877 kfree(max8998->rdev);
878 kfree(max8998);
879
880 return 0;
881}
882
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900883static const struct platform_device_id max8998_pmic_id[] = {
884 { "max8998-pmic", TYPE_MAX8998 },
885 { "lp3974-pmic", TYPE_LP3974 },
886 { }
887};
Axel Lina51b9072011-03-26 23:28:42 +0800888MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900889
Kyungmin Park156f2522010-06-16 09:04:16 +0200890static struct platform_driver max8998_pmic_driver = {
891 .driver = {
892 .name = "max8998-pmic",
893 .owner = THIS_MODULE,
894 },
895 .probe = max8998_pmic_probe,
896 .remove = __devexit_p(max8998_pmic_remove),
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900897 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200898};
899
900static int __init max8998_pmic_init(void)
901{
902 return platform_driver_register(&max8998_pmic_driver);
903}
904subsys_initcall(max8998_pmic_init);
905
906static void __exit max8998_pmic_cleanup(void)
907{
908 platform_driver_unregister(&max8998_pmic_driver);
909}
910module_exit(max8998_pmic_cleanup);
911
912MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
913MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
914MODULE_LICENSE("GPL");