blob: 5dfa920ff0c8873c91e8c02176af4d722f3c3659 [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>
31#include <linux/platform_device.h>
32#include <linux/regulator/driver.h>
33#include <linux/mfd/max8998.h>
34#include <linux/mfd/max8998-private.h>
35
36struct max8998_data {
37 struct device *dev;
38 struct max8998_dev *iodev;
39 int num_regulators;
40 struct regulator_dev **rdev;
Lukasz Majewski889cd5a2010-09-27 14:32:25 +020041 u8 buck1_vol[4]; /* voltages for selection */
42 u8 buck2_vol[2];
43 unsigned int buck1_idx; /* index to last changed voltage */
44 /* value in a set */
45 unsigned int buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +020046};
47
48struct voltage_map_desc {
49 int min;
50 int max;
51 int step;
52};
53
54/* Voltage maps */
55static const struct voltage_map_desc ldo23_voltage_map_desc = {
56 .min = 800, .step = 50, .max = 1300,
57};
58static const struct voltage_map_desc ldo456711_voltage_map_desc = {
59 .min = 1600, .step = 100, .max = 3600,
60};
61static const struct voltage_map_desc ldo8_voltage_map_desc = {
62 .min = 3000, .step = 100, .max = 3600,
63};
64static const struct voltage_map_desc ldo9_voltage_map_desc = {
65 .min = 2800, .step = 100, .max = 3100,
66};
67static const struct voltage_map_desc ldo10_voltage_map_desc = {
68 .min = 950, .step = 50, .max = 1300,
69};
70static const struct voltage_map_desc ldo1213_voltage_map_desc = {
71 .min = 800, .step = 100, .max = 3300,
72};
73static const struct voltage_map_desc ldo1415_voltage_map_desc = {
74 .min = 1200, .step = 100, .max = 3300,
75};
76static const struct voltage_map_desc ldo1617_voltage_map_desc = {
77 .min = 1600, .step = 100, .max = 3600,
78};
79static const struct voltage_map_desc buck12_voltage_map_desc = {
80 .min = 750, .step = 25, .max = 1525,
81};
82static const struct voltage_map_desc buck3_voltage_map_desc = {
83 .min = 1600, .step = 100, .max = 3600,
84};
85static const struct voltage_map_desc buck4_voltage_map_desc = {
86 .min = 800, .step = 100, .max = 2300,
87};
88
89static const struct voltage_map_desc *ldo_voltage_map[] = {
90 NULL,
91 NULL,
92 &ldo23_voltage_map_desc, /* LDO2 */
93 &ldo23_voltage_map_desc, /* LDO3 */
94 &ldo456711_voltage_map_desc, /* LDO4 */
95 &ldo456711_voltage_map_desc, /* LDO5 */
96 &ldo456711_voltage_map_desc, /* LDO6 */
97 &ldo456711_voltage_map_desc, /* LDO7 */
98 &ldo8_voltage_map_desc, /* LDO8 */
99 &ldo9_voltage_map_desc, /* LDO9 */
100 &ldo10_voltage_map_desc, /* LDO10 */
101 &ldo456711_voltage_map_desc, /* LDO11 */
102 &ldo1213_voltage_map_desc, /* LDO12 */
103 &ldo1213_voltage_map_desc, /* LDO13 */
104 &ldo1415_voltage_map_desc, /* LDO14 */
105 &ldo1415_voltage_map_desc, /* LDO15 */
106 &ldo1617_voltage_map_desc, /* LDO16 */
107 &ldo1617_voltage_map_desc, /* LDO17 */
108 &buck12_voltage_map_desc, /* BUCK1 */
109 &buck12_voltage_map_desc, /* BUCK2 */
110 &buck3_voltage_map_desc, /* BUCK3 */
111 &buck4_voltage_map_desc, /* BUCK4 */
112};
113
Kyungmin Park156f2522010-06-16 09:04:16 +0200114static int max8998_get_enable_register(struct regulator_dev *rdev,
115 int *reg, int *shift)
116{
Axel Lin7b947912012-03-01 09:27:29 +0800117 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200118
119 switch (ldo) {
120 case MAX8998_LDO2 ... MAX8998_LDO5:
121 *reg = MAX8998_REG_ONOFF1;
122 *shift = 3 - (ldo - MAX8998_LDO2);
123 break;
124 case MAX8998_LDO6 ... MAX8998_LDO13:
125 *reg = MAX8998_REG_ONOFF2;
126 *shift = 7 - (ldo - MAX8998_LDO6);
127 break;
128 case MAX8998_LDO14 ... MAX8998_LDO17:
129 *reg = MAX8998_REG_ONOFF3;
130 *shift = 7 - (ldo - MAX8998_LDO14);
131 break;
132 case MAX8998_BUCK1 ... MAX8998_BUCK4:
133 *reg = MAX8998_REG_ONOFF1;
134 *shift = 7 - (ldo - MAX8998_BUCK1);
135 break;
136 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
137 *reg = MAX8998_REG_ONOFF4;
138 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
139 break;
140 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
141 *reg = MAX8998_REG_CHGR2;
142 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
143 break;
144 default:
145 return -EINVAL;
146 }
147
148 return 0;
149}
150
151static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
152{
153 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900154 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200155 int ret, reg, shift = 8;
156 u8 val;
157
158 ret = max8998_get_enable_register(rdev, &reg, &shift);
159 if (ret)
160 return ret;
161
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900162 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200163 if (ret)
164 return ret;
165
166 return val & (1 << shift);
167}
168
169static int max8998_ldo_enable(struct regulator_dev *rdev)
170{
171 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900172 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200173 int reg, shift = 8, ret;
174
175 ret = max8998_get_enable_register(rdev, &reg, &shift);
176 if (ret)
177 return ret;
178
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900179 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200180}
181
182static int max8998_ldo_disable(struct regulator_dev *rdev)
183{
184 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900185 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200186 int reg, shift = 8, ret;
187
188 ret = max8998_get_enable_register(rdev, &reg, &shift);
189 if (ret)
190 return ret;
191
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900192 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200193}
194
195static int max8998_get_voltage_register(struct regulator_dev *rdev,
196 int *_reg, int *_shift, int *_mask)
197{
Axel Lin7b947912012-03-01 09:27:29 +0800198 int ldo = rdev_get_id(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200199 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200200 int reg, shift = 0, mask = 0xff;
201
202 switch (ldo) {
203 case MAX8998_LDO2 ... MAX8998_LDO3:
204 reg = MAX8998_REG_LDO2_LDO3;
205 mask = 0xf;
206 if (ldo == MAX8998_LDO2)
207 shift = 4;
208 else
209 shift = 0;
210 break;
211 case MAX8998_LDO4 ... MAX8998_LDO7:
212 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
213 break;
214 case MAX8998_LDO8 ... MAX8998_LDO9:
215 reg = MAX8998_REG_LDO8_LDO9;
216 mask = 0xf;
217 if (ldo == MAX8998_LDO8)
218 shift = 4;
219 else
220 shift = 0;
221 break;
222 case MAX8998_LDO10 ... MAX8998_LDO11:
223 reg = MAX8998_REG_LDO10_LDO11;
224 if (ldo == MAX8998_LDO10) {
225 shift = 5;
226 mask = 0x7;
227 } else {
228 shift = 0;
229 mask = 0x1f;
230 }
231 break;
232 case MAX8998_LDO12 ... MAX8998_LDO17:
233 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
234 break;
235 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200236 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200237 break;
238 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200239 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200240 break;
241 case MAX8998_BUCK3:
242 reg = MAX8998_REG_BUCK3;
243 break;
244 case MAX8998_BUCK4:
245 reg = MAX8998_REG_BUCK4;
246 break;
247 default:
248 return -EINVAL;
249 }
250
251 *_reg = reg;
252 *_shift = shift;
253 *_mask = mask;
254
255 return 0;
256}
257
Axel Lin7b4354b2012-04-11 13:58:09 +0800258static int max8998_get_voltage_sel(struct regulator_dev *rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200259{
260 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900261 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200262 int reg, shift = 0, mask, ret;
263 u8 val;
264
265 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
266 if (ret)
267 return ret;
268
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900269 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200270 if (ret)
271 return ret;
272
273 val >>= shift;
274 val &= mask;
275
Axel Lin7b4354b2012-04-11 13:58:09 +0800276 return val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200277}
278
Axel Linbaae0192012-06-17 09:34:29 +0800279static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
280 unsigned selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200281{
282 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
283 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Linbaae0192012-06-17 09:34:29 +0800284 int reg, shift = 0, mask, ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000285
Lukasz Majewski9d924922010-09-27 14:32:23 +0200286 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
287 if (ret)
288 return ret;
289
Axel Linbaae0192012-06-17 09:34:29 +0800290 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
Lukasz Majewski9d924922010-09-27 14:32:23 +0200291
292 return ret;
293}
294
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200295static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
296{
297 gpio_set_value(gpio1, v & 0x1);
298 gpio_set_value(gpio2, (v >> 1) & 0x1);
299}
300
301static inline void buck2_gpio_set(int gpio, int v)
302{
303 gpio_set_value(gpio, v & 0x1);
304}
305
Axel Linbaae0192012-06-17 09:34:29 +0800306static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
307 unsigned selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200308{
309 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200310 struct max8998_platform_data *pdata =
311 dev_get_platdata(max8998->iodev->dev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900312 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Lin7b947912012-03-01 09:27:29 +0800313 int buck = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200314 int reg, shift = 0, mask, ret;
Axel Linbaae0192012-06-17 09:34:29 +0800315 int j, previous_sel;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200316 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200317
Kyungmin Park156f2522010-06-16 09:04:16 +0200318 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
319 if (ret)
320 return ret;
321
Axel Lin7b4354b2012-04-11 13:58:09 +0800322 previous_sel = max8998_get_voltage_sel(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200323
324 /* Check if voltage needs to be changed */
325 /* if previous_voltage equal new voltage, return */
Axel Linbaae0192012-06-17 09:34:29 +0800326 if (previous_sel == selector) {
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200327 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
Axel Lin9a0fbb62012-06-17 09:33:07 +0800328 regulator_list_voltage_linear(rdev, previous_sel),
Axel Linbaae0192012-06-17 09:34:29 +0800329 regulator_list_voltage_linear(rdev, selector));
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200330 return ret;
331 }
332
333 switch (buck) {
334 case MAX8998_BUCK1:
335 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800336 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
Joe Perches85ee7a12011-04-23 20:38:19 -0700337 "buck1_vol3:%d, buck1_vol4:%d\n",
Axel Linbaae0192012-06-17 09:34:29 +0800338 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200339 max8998->buck1_vol[2], max8998->buck1_vol[3]);
340
341 if (gpio_is_valid(pdata->buck1_set1) &&
342 gpio_is_valid(pdata->buck1_set2)) {
343
344 /* check if requested voltage */
345 /* value is already defined */
346 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800347 if (max8998->buck1_vol[j] == selector) {
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200348 max8998->buck1_idx = j;
349 buck1_gpio_set(pdata->buck1_set1,
350 pdata->buck1_set2, j);
351 goto buck1_exit;
352 }
353 }
354
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100355 if (pdata->buck_voltage_lock)
356 return -EINVAL;
357
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200358 /* no predefine regulator found */
359 max8998->buck1_idx = (buck1_last_val % 2) + 2;
360 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
361 max8998->buck1_idx);
Axel Linbaae0192012-06-17 09:34:29 +0800362 max8998->buck1_vol[max8998->buck1_idx] = selector;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200363 ret = max8998_get_voltage_register(rdev, &reg,
364 &shift,
365 &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800366 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200367 buck1_gpio_set(pdata->buck1_set1,
368 pdata->buck1_set2, max8998->buck1_idx);
369 buck1_last_val++;
370buck1_exit:
371 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
372 i2c->name, gpio_get_value(pdata->buck1_set1),
373 gpio_get_value(pdata->buck1_set2));
374 break;
375 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800376 ret = max8998_write_reg(i2c, reg, selector);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200377 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200378 break;
379
380 case MAX8998_BUCK2:
381 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800382 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
383 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200384 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100385
386 /* check if requested voltage */
387 /* value is already defined */
388 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800389 if (max8998->buck2_vol[j] == selector) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100390 max8998->buck2_idx = j;
391 buck2_gpio_set(pdata->buck2_set3, j);
392 goto buck2_exit;
393 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200394 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100395
396 if (pdata->buck_voltage_lock)
397 return -EINVAL;
398
399 max8998_get_voltage_register(rdev,
400 &reg, &shift, &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800401 ret = max8998_write_reg(i2c, reg, selector);
402 max8998->buck2_vol[max8998->buck2_idx] = selector;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100403 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
404buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200405 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
406 gpio_get_value(pdata->buck2_set3));
407 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800408 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200409 }
410 break;
411
412 case MAX8998_BUCK3:
413 case MAX8998_BUCK4:
Axel Linbaae0192012-06-17 09:34:29 +0800414 ret = max8998_update_reg(i2c, reg, selector<<shift,
415 mask<<shift);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200416 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200417 }
418
Axel Lin276909d2012-04-11 13:59:14 +0800419 return ret;
420}
421
422static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
423 unsigned int old_selector,
424 unsigned int new_selector)
425{
426 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
427 struct i2c_client *i2c = max8998->iodev->i2c;
428 const struct voltage_map_desc *desc;
429 int buck = rdev_get_id(rdev);
430 u8 val = 0;
431 int difference, ret;
432
433 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
434 return -EINVAL;
435
436 desc = ldo_voltage_map[buck];
437
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200438 /* Voltage stabilization */
Axel Lin276909d2012-04-11 13:59:14 +0800439 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
440 if (ret)
441 return ret;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200442
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200443 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
444 /* MAX8998 has ENRAMP bit implemented, so test it*/
445 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
Axel Lin276909d2012-04-11 13:59:14 +0800446 return 0;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200447
Axel Lin276909d2012-04-11 13:59:14 +0800448 difference = (new_selector - old_selector) * desc->step;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200449 if (difference > 0)
Axel Lin276909d2012-04-11 13:59:14 +0800450 return difference / ((val & 0x0f) + 1);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200451
Axel Lin276909d2012-04-11 13:59:14 +0800452 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200453}
454
455static struct regulator_ops max8998_ldo_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800456 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800457 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200458 .is_enabled = max8998_ldo_is_enabled,
459 .enable = max8998_ldo_enable,
460 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800461 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800462 .set_voltage_sel = max8998_set_voltage_ldo_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200463};
464
465static struct regulator_ops max8998_buck_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800466 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800467 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200468 .is_enabled = max8998_ldo_is_enabled,
469 .enable = max8998_ldo_enable,
470 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800471 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800472 .set_voltage_sel = max8998_set_voltage_buck_sel,
Axel Lin276909d2012-04-11 13:59:14 +0800473 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200474};
475
476static struct regulator_ops max8998_others_ops = {
477 .is_enabled = max8998_ldo_is_enabled,
478 .enable = max8998_ldo_enable,
479 .disable = max8998_ldo_disable,
Kyungmin Park156f2522010-06-16 09:04:16 +0200480};
481
482static struct regulator_desc regulators[] = {
483 {
484 .name = "LDO2",
485 .id = MAX8998_LDO2,
486 .ops = &max8998_ldo_ops,
487 .type = REGULATOR_VOLTAGE,
488 .owner = THIS_MODULE,
489 }, {
490 .name = "LDO3",
491 .id = MAX8998_LDO3,
492 .ops = &max8998_ldo_ops,
493 .type = REGULATOR_VOLTAGE,
494 .owner = THIS_MODULE,
495 }, {
496 .name = "LDO4",
497 .id = MAX8998_LDO4,
498 .ops = &max8998_ldo_ops,
499 .type = REGULATOR_VOLTAGE,
500 .owner = THIS_MODULE,
501 }, {
502 .name = "LDO5",
503 .id = MAX8998_LDO5,
504 .ops = &max8998_ldo_ops,
505 .type = REGULATOR_VOLTAGE,
506 .owner = THIS_MODULE,
507 }, {
508 .name = "LDO6",
509 .id = MAX8998_LDO6,
510 .ops = &max8998_ldo_ops,
511 .type = REGULATOR_VOLTAGE,
512 .owner = THIS_MODULE,
513 }, {
514 .name = "LDO7",
515 .id = MAX8998_LDO7,
516 .ops = &max8998_ldo_ops,
517 .type = REGULATOR_VOLTAGE,
518 .owner = THIS_MODULE,
519 }, {
520 .name = "LDO8",
521 .id = MAX8998_LDO8,
522 .ops = &max8998_ldo_ops,
523 .type = REGULATOR_VOLTAGE,
524 .owner = THIS_MODULE,
525 }, {
526 .name = "LDO9",
527 .id = MAX8998_LDO9,
528 .ops = &max8998_ldo_ops,
529 .type = REGULATOR_VOLTAGE,
530 .owner = THIS_MODULE,
531 }, {
532 .name = "LDO10",
533 .id = MAX8998_LDO10,
534 .ops = &max8998_ldo_ops,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 }, {
538 .name = "LDO11",
539 .id = MAX8998_LDO11,
540 .ops = &max8998_ldo_ops,
541 .type = REGULATOR_VOLTAGE,
542 .owner = THIS_MODULE,
543 }, {
544 .name = "LDO12",
545 .id = MAX8998_LDO12,
546 .ops = &max8998_ldo_ops,
547 .type = REGULATOR_VOLTAGE,
548 .owner = THIS_MODULE,
549 }, {
550 .name = "LDO13",
551 .id = MAX8998_LDO13,
552 .ops = &max8998_ldo_ops,
553 .type = REGULATOR_VOLTAGE,
554 .owner = THIS_MODULE,
555 }, {
556 .name = "LDO14",
557 .id = MAX8998_LDO14,
558 .ops = &max8998_ldo_ops,
559 .type = REGULATOR_VOLTAGE,
560 .owner = THIS_MODULE,
561 }, {
562 .name = "LDO15",
563 .id = MAX8998_LDO15,
564 .ops = &max8998_ldo_ops,
565 .type = REGULATOR_VOLTAGE,
566 .owner = THIS_MODULE,
567 }, {
568 .name = "LDO16",
569 .id = MAX8998_LDO16,
570 .ops = &max8998_ldo_ops,
571 .type = REGULATOR_VOLTAGE,
572 .owner = THIS_MODULE,
573 }, {
574 .name = "LDO17",
575 .id = MAX8998_LDO17,
576 .ops = &max8998_ldo_ops,
577 .type = REGULATOR_VOLTAGE,
578 .owner = THIS_MODULE,
579 }, {
580 .name = "BUCK1",
581 .id = MAX8998_BUCK1,
582 .ops = &max8998_buck_ops,
583 .type = REGULATOR_VOLTAGE,
584 .owner = THIS_MODULE,
585 }, {
586 .name = "BUCK2",
587 .id = MAX8998_BUCK2,
588 .ops = &max8998_buck_ops,
589 .type = REGULATOR_VOLTAGE,
590 .owner = THIS_MODULE,
591 }, {
592 .name = "BUCK3",
593 .id = MAX8998_BUCK3,
594 .ops = &max8998_buck_ops,
595 .type = REGULATOR_VOLTAGE,
596 .owner = THIS_MODULE,
597 }, {
598 .name = "BUCK4",
599 .id = MAX8998_BUCK4,
600 .ops = &max8998_buck_ops,
601 .type = REGULATOR_VOLTAGE,
602 .owner = THIS_MODULE,
603 }, {
604 .name = "EN32KHz AP",
605 .id = MAX8998_EN32KHZ_AP,
606 .ops = &max8998_others_ops,
607 .type = REGULATOR_VOLTAGE,
608 .owner = THIS_MODULE,
609 }, {
610 .name = "EN32KHz CP",
611 .id = MAX8998_EN32KHZ_CP,
612 .ops = &max8998_others_ops,
613 .type = REGULATOR_VOLTAGE,
614 .owner = THIS_MODULE,
615 }, {
616 .name = "ENVICHG",
617 .id = MAX8998_ENVICHG,
618 .ops = &max8998_others_ops,
619 .type = REGULATOR_VOLTAGE,
620 .owner = THIS_MODULE,
621 }, {
622 .name = "ESAFEOUT1",
623 .id = MAX8998_ESAFEOUT1,
624 .ops = &max8998_others_ops,
625 .type = REGULATOR_VOLTAGE,
626 .owner = THIS_MODULE,
627 }, {
628 .name = "ESAFEOUT2",
629 .id = MAX8998_ESAFEOUT2,
630 .ops = &max8998_others_ops,
631 .type = REGULATOR_VOLTAGE,
632 .owner = THIS_MODULE,
633 }
634};
635
636static __devinit int max8998_pmic_probe(struct platform_device *pdev)
637{
638 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
639 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
Mark Brownc1727082012-04-04 00:50:22 +0100640 struct regulator_config config = { };
Kyungmin Park156f2522010-06-16 09:04:16 +0200641 struct regulator_dev **rdev;
642 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200643 struct i2c_client *i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200644 int i, ret, size;
645
646 if (!pdata) {
647 dev_err(pdev->dev.parent, "No platform init data supplied\n");
648 return -ENODEV;
649 }
650
Axel Lin0f80ea12012-04-19 09:47:37 +0800651 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
652 GFP_KERNEL);
Kyungmin Park156f2522010-06-16 09:04:16 +0200653 if (!max8998)
654 return -ENOMEM;
655
Axel Lin11fa0d12010-08-19 10:29:19 +0800656 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Axel Lin0f80ea12012-04-19 09:47:37 +0800657 max8998->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
658 if (!max8998->rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200659 return -ENOMEM;
Kyungmin Park156f2522010-06-16 09:04:16 +0200660
661 rdev = max8998->rdev;
Axel Lin747cc852010-08-27 16:37:34 +0800662 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200663 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800664 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200665 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200666 i2c = max8998->iodev->i2c;
667
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100668 max8998->buck1_idx = pdata->buck1_default_idx;
669 max8998->buck2_idx = pdata->buck2_default_idx;
670
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200671 /* NOTE: */
672 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
673 /* will be displayed */
674
675 /* Check if MAX8998 voltage selection GPIOs are defined */
676 if (gpio_is_valid(pdata->buck1_set1) &&
677 gpio_is_valid(pdata->buck1_set2)) {
678 /* Check if SET1 is not equal to 0 */
679 if (!pdata->buck1_set1) {
680 printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
681 WARN_ON(!pdata->buck1_set1);
Axel Lin6be6e962011-05-13 14:54:06 +0800682 ret = -EIO;
Axel Lin0f80ea12012-04-19 09:47:37 +0800683 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200684 }
685 /* Check if SET2 is not equal to 0 */
686 if (!pdata->buck1_set2) {
687 printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
688 WARN_ON(!pdata->buck1_set2);
Axel Lin6be6e962011-05-13 14:54:06 +0800689 ret = -EIO;
Axel Lin0f80ea12012-04-19 09:47:37 +0800690 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200691 }
692
693 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
694 gpio_direction_output(pdata->buck1_set1,
695 max8998->buck1_idx & 0x1);
696
697
698 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
699 gpio_direction_output(pdata->buck1_set2,
700 (max8998->buck1_idx >> 1) & 0x1);
701 /* Set predefined value for BUCK1 register 1 */
702 i = 0;
703 while (buck12_voltage_map_desc.min +
704 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100705 < (pdata->buck1_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200706 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200707 max8998->buck1_vol[0] = i;
708 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100709 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800710 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200711
712 /* Set predefined value for BUCK1 register 2 */
713 i = 0;
714 while (buck12_voltage_map_desc.min +
715 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100716 < (pdata->buck1_voltage2 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200717 i++;
718
719 max8998->buck1_vol[1] = i;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100720 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
721 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800722 goto err_out;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100723
724 /* Set predefined value for BUCK1 register 3 */
725 i = 0;
726 while (buck12_voltage_map_desc.min +
727 buck12_voltage_map_desc.step*i
728 < (pdata->buck1_voltage3 / 1000))
729 i++;
730
731 max8998->buck1_vol[2] = i;
732 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
733 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800734 goto err_out;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100735
736 /* Set predefined value for BUCK1 register 4 */
737 i = 0;
738 while (buck12_voltage_map_desc.min +
739 buck12_voltage_map_desc.step*i
740 < (pdata->buck1_voltage4 / 1000))
741 i++;
742
743 max8998->buck1_vol[3] = i;
744 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200745 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800746 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200747
748 }
749
750 if (gpio_is_valid(pdata->buck2_set3)) {
751 /* Check if SET3 is not equal to 0 */
752 if (!pdata->buck2_set3) {
753 printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
754 WARN_ON(!pdata->buck2_set3);
Axel Lin6be6e962011-05-13 14:54:06 +0800755 ret = -EIO;
Axel Lin0f80ea12012-04-19 09:47:37 +0800756 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200757 }
758 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
759 gpio_direction_output(pdata->buck2_set3,
760 max8998->buck2_idx & 0x1);
761
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100762 /* BUCK2 register 1 */
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200763 i = 0;
764 while (buck12_voltage_map_desc.min +
765 buck12_voltage_map_desc.step*i
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100766 < (pdata->buck2_voltage1 / 1000))
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200767 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200768 max8998->buck2_vol[0] = i;
769 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
770 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800771 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200772
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100773 /* BUCK2 register 2 */
774 i = 0;
775 while (buck12_voltage_map_desc.min +
776 buck12_voltage_map_desc.step*i
777 < (pdata->buck2_voltage2 / 1000))
778 i++;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100779 max8998->buck2_vol[1] = i;
780 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
781 if (ret)
Axel Lin0f80ea12012-04-19 09:47:37 +0800782 goto err_out;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200783 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200784
785 for (i = 0; i < pdata->num_regulators; i++) {
786 const struct voltage_map_desc *desc;
787 int id = pdata->regulators[i].id;
788 int index = id - MAX8998_LDO2;
789
790 desc = ldo_voltage_map[id];
791 if (desc && regulators[index].ops != &max8998_others_ops) {
792 int count = (desc->max - desc->min) / desc->step + 1;
Axel Lin9a0fbb62012-06-17 09:33:07 +0800793
Kyungmin Park156f2522010-06-16 09:04:16 +0200794 regulators[index].n_voltages = count;
Axel Lin9a0fbb62012-06-17 09:33:07 +0800795 regulators[index].min_uV = desc->min * 1000;
796 regulators[index].uV_step = desc->step * 1000;
Kyungmin Park156f2522010-06-16 09:04:16 +0200797 }
Mark Brownc1727082012-04-04 00:50:22 +0100798
799 config.dev = max8998->dev;
800 config.init_data = pdata->regulators[i].initdata;
801 config.driver_data = max8998;
802
803 rdev[i] = regulator_register(&regulators[index], &config);
Kyungmin Park156f2522010-06-16 09:04:16 +0200804 if (IS_ERR(rdev[i])) {
805 ret = PTR_ERR(rdev[i]);
806 dev_err(max8998->dev, "regulator init failed\n");
807 rdev[i] = NULL;
808 goto err;
809 }
810 }
811
812
813 return 0;
814err:
Axel Lin0f80ea12012-04-19 09:47:37 +0800815 while (--i >= 0)
816 regulator_unregister(rdev[i]);
817err_out:
Kyungmin Park156f2522010-06-16 09:04:16 +0200818 return ret;
819}
820
821static int __devexit max8998_pmic_remove(struct platform_device *pdev)
822{
823 struct max8998_data *max8998 = platform_get_drvdata(pdev);
824 struct regulator_dev **rdev = max8998->rdev;
825 int i;
826
Axel Lin11fa0d12010-08-19 10:29:19 +0800827 for (i = 0; i < max8998->num_regulators; i++)
Axel Lin0f80ea12012-04-19 09:47:37 +0800828 regulator_unregister(rdev[i]);
Kyungmin Park156f2522010-06-16 09:04:16 +0200829 return 0;
830}
831
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900832static const struct platform_device_id max8998_pmic_id[] = {
833 { "max8998-pmic", TYPE_MAX8998 },
834 { "lp3974-pmic", TYPE_LP3974 },
835 { }
836};
Axel Lina51b9072011-03-26 23:28:42 +0800837MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900838
Kyungmin Park156f2522010-06-16 09:04:16 +0200839static struct platform_driver max8998_pmic_driver = {
840 .driver = {
841 .name = "max8998-pmic",
842 .owner = THIS_MODULE,
843 },
844 .probe = max8998_pmic_probe,
845 .remove = __devexit_p(max8998_pmic_remove),
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900846 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200847};
848
849static int __init max8998_pmic_init(void)
850{
851 return platform_driver_register(&max8998_pmic_driver);
852}
853subsys_initcall(max8998_pmic_init);
854
855static void __exit max8998_pmic_cleanup(void)
856{
857 platform_driver_unregister(&max8998_pmic_driver);
858}
859module_exit(max8998_pmic_cleanup);
860
861MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
862MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
863MODULE_LICENSE("GPL");