blob: f57e9c42fdb4e2957ee767d68897ca36e57ae219 [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
115static inline int max8998_get_ldo(struct regulator_dev *rdev)
116{
117 return rdev_get_id(rdev);
118}
119
120static int max8998_list_voltage(struct regulator_dev *rdev,
121 unsigned int selector)
122{
123 const struct voltage_map_desc *desc;
124 int ldo = max8998_get_ldo(rdev);
125 int val;
126
Axel Lin1b69d8e2010-08-04 14:34:10 +0800127 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
Kyungmin Park156f2522010-06-16 09:04:16 +0200128 return -EINVAL;
129
130 desc = ldo_voltage_map[ldo];
131 if (desc == NULL)
132 return -EINVAL;
133
134 val = desc->min + desc->step * selector;
135 if (val > desc->max)
136 return -EINVAL;
137
138 return val * 1000;
139}
140
141static int max8998_get_enable_register(struct regulator_dev *rdev,
142 int *reg, int *shift)
143{
144 int ldo = max8998_get_ldo(rdev);
145
146 switch (ldo) {
147 case MAX8998_LDO2 ... MAX8998_LDO5:
148 *reg = MAX8998_REG_ONOFF1;
149 *shift = 3 - (ldo - MAX8998_LDO2);
150 break;
151 case MAX8998_LDO6 ... MAX8998_LDO13:
152 *reg = MAX8998_REG_ONOFF2;
153 *shift = 7 - (ldo - MAX8998_LDO6);
154 break;
155 case MAX8998_LDO14 ... MAX8998_LDO17:
156 *reg = MAX8998_REG_ONOFF3;
157 *shift = 7 - (ldo - MAX8998_LDO14);
158 break;
159 case MAX8998_BUCK1 ... MAX8998_BUCK4:
160 *reg = MAX8998_REG_ONOFF1;
161 *shift = 7 - (ldo - MAX8998_BUCK1);
162 break;
163 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
164 *reg = MAX8998_REG_ONOFF4;
165 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
166 break;
167 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
168 *reg = MAX8998_REG_CHGR2;
169 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
170 break;
171 default:
172 return -EINVAL;
173 }
174
175 return 0;
176}
177
178static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
179{
180 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900181 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200182 int ret, reg, shift = 8;
183 u8 val;
184
185 ret = max8998_get_enable_register(rdev, &reg, &shift);
186 if (ret)
187 return ret;
188
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900189 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200190 if (ret)
191 return ret;
192
193 return val & (1 << shift);
194}
195
196static int max8998_ldo_enable(struct regulator_dev *rdev)
197{
198 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900199 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200200 int reg, shift = 8, ret;
201
202 ret = max8998_get_enable_register(rdev, &reg, &shift);
203 if (ret)
204 return ret;
205
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900206 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200207}
208
209static int max8998_ldo_disable(struct regulator_dev *rdev)
210{
211 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900212 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200213 int reg, shift = 8, ret;
214
215 ret = max8998_get_enable_register(rdev, &reg, &shift);
216 if (ret)
217 return ret;
218
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900219 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200220}
221
222static int max8998_get_voltage_register(struct regulator_dev *rdev,
223 int *_reg, int *_shift, int *_mask)
224{
225 int ldo = max8998_get_ldo(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200226 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200227 int reg, shift = 0, mask = 0xff;
228
229 switch (ldo) {
230 case MAX8998_LDO2 ... MAX8998_LDO3:
231 reg = MAX8998_REG_LDO2_LDO3;
232 mask = 0xf;
233 if (ldo == MAX8998_LDO2)
234 shift = 4;
235 else
236 shift = 0;
237 break;
238 case MAX8998_LDO4 ... MAX8998_LDO7:
239 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
240 break;
241 case MAX8998_LDO8 ... MAX8998_LDO9:
242 reg = MAX8998_REG_LDO8_LDO9;
243 mask = 0xf;
244 if (ldo == MAX8998_LDO8)
245 shift = 4;
246 else
247 shift = 0;
248 break;
249 case MAX8998_LDO10 ... MAX8998_LDO11:
250 reg = MAX8998_REG_LDO10_LDO11;
251 if (ldo == MAX8998_LDO10) {
252 shift = 5;
253 mask = 0x7;
254 } else {
255 shift = 0;
256 mask = 0x1f;
257 }
258 break;
259 case MAX8998_LDO12 ... MAX8998_LDO17:
260 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
261 break;
262 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200263 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200264 break;
265 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200266 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200267 break;
268 case MAX8998_BUCK3:
269 reg = MAX8998_REG_BUCK3;
270 break;
271 case MAX8998_BUCK4:
272 reg = MAX8998_REG_BUCK4;
273 break;
274 default:
275 return -EINVAL;
276 }
277
278 *_reg = reg;
279 *_shift = shift;
280 *_mask = mask;
281
282 return 0;
283}
284
285static int max8998_get_voltage(struct regulator_dev *rdev)
286{
287 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900288 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200289 int reg, shift = 0, mask, ret;
290 u8 val;
291
292 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
293 if (ret)
294 return ret;
295
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900296 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200297 if (ret)
298 return ret;
299
300 val >>= shift;
301 val &= mask;
302
303 return max8998_list_voltage(rdev, val);
304}
305
Lukasz Majewski9d924922010-09-27 14:32:23 +0200306static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000307 int min_uV, int max_uV, unsigned *selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200308{
309 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
310 struct i2c_client *i2c = max8998->iodev->i2c;
311 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
312 const struct voltage_map_desc *desc;
313 int ldo = max8998_get_ldo(rdev);
314 int reg, shift = 0, mask, ret;
315 int i = 0;
316
317 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
318 return -EINVAL;
319
320 desc = ldo_voltage_map[ldo];
321 if (desc == NULL)
322 return -EINVAL;
323
324 if (max_vol < desc->min || min_vol > desc->max)
325 return -EINVAL;
326
327 while (desc->min + desc->step*i < min_vol &&
328 desc->min + desc->step*i < desc->max)
329 i++;
330
331 if (desc->min + desc->step*i > max_vol)
332 return -EINVAL;
333
Mark Brown3a93f2a2010-11-10 14:38:29 +0000334 *selector = i;
335
Lukasz Majewski9d924922010-09-27 14:32:23 +0200336 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
337 if (ret)
338 return ret;
339
340 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
341
342 return ret;
343}
344
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200345static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
346{
347 gpio_set_value(gpio1, v & 0x1);
348 gpio_set_value(gpio2, (v >> 1) & 0x1);
349}
350
351static inline void buck2_gpio_set(int gpio, int v)
352{
353 gpio_set_value(gpio, v & 0x1);
354}
355
Lukasz Majewski9d924922010-09-27 14:32:23 +0200356static int max8998_set_voltage_buck(struct regulator_dev *rdev,
Mark Brownf4d6adf2010-11-19 12:07:44 +0000357 int min_uV, int max_uV, unsigned *selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200358{
359 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200360 struct max8998_platform_data *pdata =
361 dev_get_platdata(max8998->iodev->dev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900362 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200363 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
364 const struct voltage_map_desc *desc;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200365 int buck = max8998_get_ldo(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200366 int reg, shift = 0, mask, ret;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200367 int difference = 0, i = 0, j = 0, previous_vol = 0;
368 u8 val = 0;
369 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200370
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200371 if (buck >= ARRAY_SIZE(ldo_voltage_map))
Kyungmin Park156f2522010-06-16 09:04:16 +0200372 return -EINVAL;
373
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200374 desc = ldo_voltage_map[buck];
375
Kyungmin Park156f2522010-06-16 09:04:16 +0200376 if (desc == NULL)
377 return -EINVAL;
378
379 if (max_vol < desc->min || min_vol > desc->max)
380 return -EINVAL;
381
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200382 while (desc->min + desc->step*i < min_vol &&
Kyungmin Park156f2522010-06-16 09:04:16 +0200383 desc->min + desc->step*i < desc->max)
384 i++;
385
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200386 if (desc->min + desc->step*i > max_vol)
387 return -EINVAL;
388
Mark Brown3a93f2a2010-11-10 14:38:29 +0000389 *selector = i;
390
Kyungmin Park156f2522010-06-16 09:04:16 +0200391 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
392 if (ret)
393 return ret;
394
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200395 previous_vol = max8998_get_voltage(rdev);
396
397 /* Check if voltage needs to be changed */
398 /* if previous_voltage equal new voltage, return */
399 if (previous_vol == max8998_list_voltage(rdev, i)) {
400 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
401 previous_vol, max8998_list_voltage(rdev, i));
402 return ret;
403 }
404
405 switch (buck) {
406 case MAX8998_BUCK1:
407 dev_dbg(max8998->dev,
Joe Perches85ee7a12011-04-23 20:38:19 -0700408 "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n"
409 "buck1_vol3:%d, buck1_vol4:%d\n",
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200410 i, max8998->buck1_vol[0], max8998->buck1_vol[1],
411 max8998->buck1_vol[2], max8998->buck1_vol[3]);
412
413 if (gpio_is_valid(pdata->buck1_set1) &&
414 gpio_is_valid(pdata->buck1_set2)) {
415
416 /* check if requested voltage */
417 /* value is already defined */
418 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
419 if (max8998->buck1_vol[j] == i) {
420 max8998->buck1_idx = j;
421 buck1_gpio_set(pdata->buck1_set1,
422 pdata->buck1_set2, j);
423 goto buck1_exit;
424 }
425 }
426
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100427 if (pdata->buck_voltage_lock)
428 return -EINVAL;
429
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200430 /* no predefine regulator found */
431 max8998->buck1_idx = (buck1_last_val % 2) + 2;
432 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
433 max8998->buck1_idx);
434 max8998->buck1_vol[max8998->buck1_idx] = i;
435 ret = max8998_get_voltage_register(rdev, &reg,
436 &shift,
437 &mask);
438 ret = max8998_write_reg(i2c, reg, i);
439 buck1_gpio_set(pdata->buck1_set1,
440 pdata->buck1_set2, max8998->buck1_idx);
441 buck1_last_val++;
442buck1_exit:
443 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
444 i2c->name, gpio_get_value(pdata->buck1_set1),
445 gpio_get_value(pdata->buck1_set2));
446 break;
447 } else {
448 ret = max8998_write_reg(i2c, reg, i);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200449 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200450 break;
451
452 case MAX8998_BUCK2:
453 dev_dbg(max8998->dev,
454 "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
455 , i, max8998->buck2_vol[0], max8998->buck2_vol[1]);
456 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100457
458 /* check if requested voltage */
459 /* value is already defined */
460 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
461 if (max8998->buck2_vol[j] == i) {
462 max8998->buck2_idx = j;
463 buck2_gpio_set(pdata->buck2_set3, j);
464 goto buck2_exit;
465 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200466 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100467
468 if (pdata->buck_voltage_lock)
469 return -EINVAL;
470
471 max8998_get_voltage_register(rdev,
472 &reg, &shift, &mask);
473 ret = max8998_write_reg(i2c, reg, i);
474 max8998->buck2_vol[max8998->buck2_idx] = i;
475 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
476buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200477 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
478 gpio_get_value(pdata->buck2_set3));
479 } else {
480 ret = max8998_write_reg(i2c, reg, i);
481 }
482 break;
483
484 case MAX8998_BUCK3:
485 case MAX8998_BUCK4:
486 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
487 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200488 }
489
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200490 /* Voltage stabilization */
491 max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200492
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200493 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
494 /* MAX8998 has ENRAMP bit implemented, so test it*/
495 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
496 return ret;
497
498 difference = desc->min + desc->step*i - previous_vol/1000;
499 if (difference > 0)
500 udelay(difference / ((val & 0x0f) + 1));
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200501
502 return ret;
Kyungmin Park156f2522010-06-16 09:04:16 +0200503}
504
505static struct regulator_ops max8998_ldo_ops = {
506 .list_voltage = max8998_list_voltage,
507 .is_enabled = max8998_ldo_is_enabled,
508 .enable = max8998_ldo_enable,
509 .disable = max8998_ldo_disable,
510 .get_voltage = max8998_get_voltage,
Lukasz Majewski9d924922010-09-27 14:32:23 +0200511 .set_voltage = max8998_set_voltage_ldo,
Kyungmin Park156f2522010-06-16 09:04:16 +0200512 .set_suspend_enable = max8998_ldo_enable,
513 .set_suspend_disable = max8998_ldo_disable,
514};
515
516static struct regulator_ops max8998_buck_ops = {
517 .list_voltage = max8998_list_voltage,
518 .is_enabled = max8998_ldo_is_enabled,
519 .enable = max8998_ldo_enable,
520 .disable = max8998_ldo_disable,
521 .get_voltage = max8998_get_voltage,
Lukasz Majewski9d924922010-09-27 14:32:23 +0200522 .set_voltage = max8998_set_voltage_buck,
Kyungmin Park156f2522010-06-16 09:04:16 +0200523 .set_suspend_enable = max8998_ldo_enable,
524 .set_suspend_disable = max8998_ldo_disable,
525};
526
527static struct regulator_ops max8998_others_ops = {
528 .is_enabled = max8998_ldo_is_enabled,
529 .enable = max8998_ldo_enable,
530 .disable = max8998_ldo_disable,
531 .set_suspend_enable = max8998_ldo_enable,
532 .set_suspend_disable = max8998_ldo_disable,
533};
534
535static struct regulator_desc regulators[] = {
536 {
537 .name = "LDO2",
538 .id = MAX8998_LDO2,
539 .ops = &max8998_ldo_ops,
540 .type = REGULATOR_VOLTAGE,
541 .owner = THIS_MODULE,
542 }, {
543 .name = "LDO3",
544 .id = MAX8998_LDO3,
545 .ops = &max8998_ldo_ops,
546 .type = REGULATOR_VOLTAGE,
547 .owner = THIS_MODULE,
548 }, {
549 .name = "LDO4",
550 .id = MAX8998_LDO4,
551 .ops = &max8998_ldo_ops,
552 .type = REGULATOR_VOLTAGE,
553 .owner = THIS_MODULE,
554 }, {
555 .name = "LDO5",
556 .id = MAX8998_LDO5,
557 .ops = &max8998_ldo_ops,
558 .type = REGULATOR_VOLTAGE,
559 .owner = THIS_MODULE,
560 }, {
561 .name = "LDO6",
562 .id = MAX8998_LDO6,
563 .ops = &max8998_ldo_ops,
564 .type = REGULATOR_VOLTAGE,
565 .owner = THIS_MODULE,
566 }, {
567 .name = "LDO7",
568 .id = MAX8998_LDO7,
569 .ops = &max8998_ldo_ops,
570 .type = REGULATOR_VOLTAGE,
571 .owner = THIS_MODULE,
572 }, {
573 .name = "LDO8",
574 .id = MAX8998_LDO8,
575 .ops = &max8998_ldo_ops,
576 .type = REGULATOR_VOLTAGE,
577 .owner = THIS_MODULE,
578 }, {
579 .name = "LDO9",
580 .id = MAX8998_LDO9,
581 .ops = &max8998_ldo_ops,
582 .type = REGULATOR_VOLTAGE,
583 .owner = THIS_MODULE,
584 }, {
585 .name = "LDO10",
586 .id = MAX8998_LDO10,
587 .ops = &max8998_ldo_ops,
588 .type = REGULATOR_VOLTAGE,
589 .owner = THIS_MODULE,
590 }, {
591 .name = "LDO11",
592 .id = MAX8998_LDO11,
593 .ops = &max8998_ldo_ops,
594 .type = REGULATOR_VOLTAGE,
595 .owner = THIS_MODULE,
596 }, {
597 .name = "LDO12",
598 .id = MAX8998_LDO12,
599 .ops = &max8998_ldo_ops,
600 .type = REGULATOR_VOLTAGE,
601 .owner = THIS_MODULE,
602 }, {
603 .name = "LDO13",
604 .id = MAX8998_LDO13,
605 .ops = &max8998_ldo_ops,
606 .type = REGULATOR_VOLTAGE,
607 .owner = THIS_MODULE,
608 }, {
609 .name = "LDO14",
610 .id = MAX8998_LDO14,
611 .ops = &max8998_ldo_ops,
612 .type = REGULATOR_VOLTAGE,
613 .owner = THIS_MODULE,
614 }, {
615 .name = "LDO15",
616 .id = MAX8998_LDO15,
617 .ops = &max8998_ldo_ops,
618 .type = REGULATOR_VOLTAGE,
619 .owner = THIS_MODULE,
620 }, {
621 .name = "LDO16",
622 .id = MAX8998_LDO16,
623 .ops = &max8998_ldo_ops,
624 .type = REGULATOR_VOLTAGE,
625 .owner = THIS_MODULE,
626 }, {
627 .name = "LDO17",
628 .id = MAX8998_LDO17,
629 .ops = &max8998_ldo_ops,
630 .type = REGULATOR_VOLTAGE,
631 .owner = THIS_MODULE,
632 }, {
633 .name = "BUCK1",
634 .id = MAX8998_BUCK1,
635 .ops = &max8998_buck_ops,
636 .type = REGULATOR_VOLTAGE,
637 .owner = THIS_MODULE,
638 }, {
639 .name = "BUCK2",
640 .id = MAX8998_BUCK2,
641 .ops = &max8998_buck_ops,
642 .type = REGULATOR_VOLTAGE,
643 .owner = THIS_MODULE,
644 }, {
645 .name = "BUCK3",
646 .id = MAX8998_BUCK3,
647 .ops = &max8998_buck_ops,
648 .type = REGULATOR_VOLTAGE,
649 .owner = THIS_MODULE,
650 }, {
651 .name = "BUCK4",
652 .id = MAX8998_BUCK4,
653 .ops = &max8998_buck_ops,
654 .type = REGULATOR_VOLTAGE,
655 .owner = THIS_MODULE,
656 }, {
657 .name = "EN32KHz AP",
658 .id = MAX8998_EN32KHZ_AP,
659 .ops = &max8998_others_ops,
660 .type = REGULATOR_VOLTAGE,
661 .owner = THIS_MODULE,
662 }, {
663 .name = "EN32KHz CP",
664 .id = MAX8998_EN32KHZ_CP,
665 .ops = &max8998_others_ops,
666 .type = REGULATOR_VOLTAGE,
667 .owner = THIS_MODULE,
668 }, {
669 .name = "ENVICHG",
670 .id = MAX8998_ENVICHG,
671 .ops = &max8998_others_ops,
672 .type = REGULATOR_VOLTAGE,
673 .owner = THIS_MODULE,
674 }, {
675 .name = "ESAFEOUT1",
676 .id = MAX8998_ESAFEOUT1,
677 .ops = &max8998_others_ops,
678 .type = REGULATOR_VOLTAGE,
679 .owner = THIS_MODULE,
680 }, {
681 .name = "ESAFEOUT2",
682 .id = MAX8998_ESAFEOUT2,
683 .ops = &max8998_others_ops,
684 .type = REGULATOR_VOLTAGE,
685 .owner = THIS_MODULE,
686 }
687};
688
689static __devinit int max8998_pmic_probe(struct platform_device *pdev)
690{
691 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
692 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
693 struct regulator_dev **rdev;
694 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200695 struct i2c_client *i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200696 int i, ret, size;
697
698 if (!pdata) {
699 dev_err(pdev->dev.parent, "No platform init data supplied\n");
700 return -ENODEV;
701 }
702
703 max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
704 if (!max8998)
705 return -ENOMEM;
706
Axel Lin11fa0d12010-08-19 10:29:19 +0800707 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200708 max8998->rdev = kzalloc(size, GFP_KERNEL);
709 if (!max8998->rdev) {
710 kfree(max8998);
711 return -ENOMEM;
712 }
713
714 rdev = max8998->rdev;
Axel Lin747cc852010-08-27 16:37:34 +0800715 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200716 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800717 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200718 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200719 i2c = max8998->iodev->i2c;
720
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100721 max8998->buck1_idx = pdata->buck1_default_idx;
722 max8998->buck2_idx = pdata->buck2_default_idx;
723
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200724 /* NOTE: */
725 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
726 /* will be displayed */
727
728 /* Check if MAX8998 voltage selection GPIOs are defined */
729 if (gpio_is_valid(pdata->buck1_set1) &&
730 gpio_is_valid(pdata->buck1_set2)) {
731 /* Check if SET1 is not equal to 0 */
732 if (!pdata->buck1_set1) {
733 printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
734 WARN_ON(!pdata->buck1_set1);
735 return -EIO;
736 }
737 /* Check if SET2 is not equal to 0 */
738 if (!pdata->buck1_set2) {
739 printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
740 WARN_ON(!pdata->buck1_set2);
741 return -EIO;
742 }
743
744 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
745 gpio_direction_output(pdata->buck1_set1,
746 max8998->buck1_idx & 0x1);
747
748
749 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
750 gpio_direction_output(pdata->buck1_set2,
751 (max8998->buck1_idx >> 1) & 0x1);
752 /* Set predefined value for BUCK1 register 1 */
753 i = 0;
754 while (buck12_voltage_map_desc.min +
755 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100756 < (pdata->buck1_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200757 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200758 max8998->buck1_vol[0] = i;
759 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100760 if (ret)
761 return ret;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200762
763 /* Set predefined value for BUCK1 register 2 */
764 i = 0;
765 while (buck12_voltage_map_desc.min +
766 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100767 < (pdata->buck1_voltage2 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200768 i++;
769
770 max8998->buck1_vol[1] = i;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100771 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
772 if (ret)
773 return ret;
774
775 /* Set predefined value for BUCK1 register 3 */
776 i = 0;
777 while (buck12_voltage_map_desc.min +
778 buck12_voltage_map_desc.step*i
779 < (pdata->buck1_voltage3 / 1000))
780 i++;
781
782 max8998->buck1_vol[2] = i;
783 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
784 if (ret)
785 return ret;
786
787 /* Set predefined value for BUCK1 register 4 */
788 i = 0;
789 while (buck12_voltage_map_desc.min +
790 buck12_voltage_map_desc.step*i
791 < (pdata->buck1_voltage4 / 1000))
792 i++;
793
794 max8998->buck1_vol[3] = i;
795 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200796 if (ret)
797 return ret;
798
799 }
800
801 if (gpio_is_valid(pdata->buck2_set3)) {
802 /* Check if SET3 is not equal to 0 */
803 if (!pdata->buck2_set3) {
804 printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
805 WARN_ON(!pdata->buck2_set3);
806 return -EIO;
807 }
808 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
809 gpio_direction_output(pdata->buck2_set3,
810 max8998->buck2_idx & 0x1);
811
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100812 /* BUCK2 register 1 */
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200813 i = 0;
814 while (buck12_voltage_map_desc.min +
815 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100816 < (pdata->buck2_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200817 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200818 max8998->buck2_vol[0] = i;
819 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
820 if (ret)
821 return ret;
822
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100823 /* BUCK2 register 2 */
824 i = 0;
825 while (buck12_voltage_map_desc.min +
826 buck12_voltage_map_desc.step*i
827 < (pdata->buck2_voltage2 / 1000))
828 i++;
829 printk(KERN_ERR "i2:%d, buck2_idx:%d\n", i, max8998->buck2_idx);
830 max8998->buck2_vol[1] = i;
831 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
832 if (ret)
833 return ret;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200834 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200835
836 for (i = 0; i < pdata->num_regulators; i++) {
837 const struct voltage_map_desc *desc;
838 int id = pdata->regulators[i].id;
839 int index = id - MAX8998_LDO2;
840
841 desc = ldo_voltage_map[id];
842 if (desc && regulators[index].ops != &max8998_others_ops) {
843 int count = (desc->max - desc->min) / desc->step + 1;
844 regulators[index].n_voltages = count;
845 }
846 rdev[i] = regulator_register(&regulators[index], max8998->dev,
847 pdata->regulators[i].initdata, max8998);
848 if (IS_ERR(rdev[i])) {
849 ret = PTR_ERR(rdev[i]);
850 dev_err(max8998->dev, "regulator init failed\n");
851 rdev[i] = NULL;
852 goto err;
853 }
854 }
855
856
857 return 0;
858err:
Axel Lin11fa0d12010-08-19 10:29:19 +0800859 for (i = 0; i < max8998->num_regulators; i++)
Kyungmin Park156f2522010-06-16 09:04:16 +0200860 if (rdev[i])
861 regulator_unregister(rdev[i]);
862
863 kfree(max8998->rdev);
864 kfree(max8998);
865
866 return ret;
867}
868
869static int __devexit max8998_pmic_remove(struct platform_device *pdev)
870{
871 struct max8998_data *max8998 = platform_get_drvdata(pdev);
872 struct regulator_dev **rdev = max8998->rdev;
873 int i;
874
Axel Lin11fa0d12010-08-19 10:29:19 +0800875 for (i = 0; i < max8998->num_regulators; i++)
Kyungmin Park156f2522010-06-16 09:04:16 +0200876 if (rdev[i])
877 regulator_unregister(rdev[i]);
878
879 kfree(max8998->rdev);
880 kfree(max8998);
881
882 return 0;
883}
884
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900885static const struct platform_device_id max8998_pmic_id[] = {
886 { "max8998-pmic", TYPE_MAX8998 },
887 { "lp3974-pmic", TYPE_LP3974 },
888 { }
889};
Axel Lina51b9072011-03-26 23:28:42 +0800890MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900891
Kyungmin Park156f2522010-06-16 09:04:16 +0200892static struct platform_driver max8998_pmic_driver = {
893 .driver = {
894 .name = "max8998-pmic",
895 .owner = THIS_MODULE,
896 },
897 .probe = max8998_pmic_probe,
898 .remove = __devexit_p(max8998_pmic_remove),
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900899 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200900};
901
902static int __init max8998_pmic_init(void)
903{
904 return platform_driver_register(&max8998_pmic_driver);
905}
906subsys_initcall(max8998_pmic_init);
907
908static void __exit max8998_pmic_cleanup(void)
909{
910 platform_driver_unregister(&max8998_pmic_driver);
911}
912module_exit(max8998_pmic_cleanup);
913
914MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
915MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
916MODULE_LICENSE("GPL");