blob: ae3f0656feb00c406ca438642c051a6453ab9171 [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>
Tomasz Figaee999fb2013-06-25 16:08:10 +020031#include <linux/of.h>
32#include <linux/of_gpio.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020033#include <linux/platform_device.h>
34#include <linux/regulator/driver.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020035#include <linux/regulator/of_regulator.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020036#include <linux/mfd/max8998.h>
37#include <linux/mfd/max8998-private.h>
38
39struct max8998_data {
40 struct device *dev;
41 struct max8998_dev *iodev;
42 int num_regulators;
43 struct regulator_dev **rdev;
Lukasz Majewski889cd5a2010-09-27 14:32:25 +020044 u8 buck1_vol[4]; /* voltages for selection */
45 u8 buck2_vol[2];
46 unsigned int buck1_idx; /* index to last changed voltage */
47 /* value in a set */
48 unsigned int buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +020049};
50
51struct voltage_map_desc {
52 int min;
53 int max;
54 int step;
55};
56
Axel Linadf61782012-12-28 17:10:20 +080057/* Voltage maps in uV*/
Kyungmin Park156f2522010-06-16 09:04:16 +020058static const struct voltage_map_desc ldo23_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080059 .min = 800000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020060};
61static const struct voltage_map_desc ldo456711_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080062 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020063};
64static const struct voltage_map_desc ldo8_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080065 .min = 3000000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020066};
67static const struct voltage_map_desc ldo9_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080068 .min = 2800000, .step = 100000, .max = 3100000,
Kyungmin Park156f2522010-06-16 09:04:16 +020069};
70static const struct voltage_map_desc ldo10_voltage_map_desc = {
Marek Szyprowski36a6f8c2013-01-25 09:25:40 +010071 .min = 950000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020072};
73static const struct voltage_map_desc ldo1213_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080074 .min = 800000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020075};
76static const struct voltage_map_desc ldo1415_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080077 .min = 1200000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020078};
79static const struct voltage_map_desc ldo1617_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080080 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020081};
82static const struct voltage_map_desc buck12_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080083 .min = 750000, .step = 25000, .max = 1525000,
Kyungmin Park156f2522010-06-16 09:04:16 +020084};
85static const struct voltage_map_desc buck3_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080086 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020087};
88static const struct voltage_map_desc buck4_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080089 .min = 800000, .step = 100000, .max = 2300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020090};
91
92static const struct voltage_map_desc *ldo_voltage_map[] = {
93 NULL,
94 NULL,
95 &ldo23_voltage_map_desc, /* LDO2 */
96 &ldo23_voltage_map_desc, /* LDO3 */
97 &ldo456711_voltage_map_desc, /* LDO4 */
98 &ldo456711_voltage_map_desc, /* LDO5 */
99 &ldo456711_voltage_map_desc, /* LDO6 */
100 &ldo456711_voltage_map_desc, /* LDO7 */
101 &ldo8_voltage_map_desc, /* LDO8 */
102 &ldo9_voltage_map_desc, /* LDO9 */
103 &ldo10_voltage_map_desc, /* LDO10 */
104 &ldo456711_voltage_map_desc, /* LDO11 */
105 &ldo1213_voltage_map_desc, /* LDO12 */
106 &ldo1213_voltage_map_desc, /* LDO13 */
107 &ldo1415_voltage_map_desc, /* LDO14 */
108 &ldo1415_voltage_map_desc, /* LDO15 */
109 &ldo1617_voltage_map_desc, /* LDO16 */
110 &ldo1617_voltage_map_desc, /* LDO17 */
111 &buck12_voltage_map_desc, /* BUCK1 */
112 &buck12_voltage_map_desc, /* BUCK2 */
113 &buck3_voltage_map_desc, /* BUCK3 */
114 &buck4_voltage_map_desc, /* BUCK4 */
115};
116
Kyungmin Park156f2522010-06-16 09:04:16 +0200117static int max8998_get_enable_register(struct regulator_dev *rdev,
118 int *reg, int *shift)
119{
Axel Lin7b947912012-03-01 09:27:29 +0800120 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200121
122 switch (ldo) {
123 case MAX8998_LDO2 ... MAX8998_LDO5:
124 *reg = MAX8998_REG_ONOFF1;
125 *shift = 3 - (ldo - MAX8998_LDO2);
126 break;
127 case MAX8998_LDO6 ... MAX8998_LDO13:
128 *reg = MAX8998_REG_ONOFF2;
129 *shift = 7 - (ldo - MAX8998_LDO6);
130 break;
131 case MAX8998_LDO14 ... MAX8998_LDO17:
132 *reg = MAX8998_REG_ONOFF3;
133 *shift = 7 - (ldo - MAX8998_LDO14);
134 break;
135 case MAX8998_BUCK1 ... MAX8998_BUCK4:
136 *reg = MAX8998_REG_ONOFF1;
137 *shift = 7 - (ldo - MAX8998_BUCK1);
138 break;
139 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
140 *reg = MAX8998_REG_ONOFF4;
141 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
142 break;
143 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
144 *reg = MAX8998_REG_CHGR2;
145 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
146 break;
147 default:
148 return -EINVAL;
149 }
150
151 return 0;
152}
153
154static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
155{
156 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900157 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200158 int ret, reg, shift = 8;
159 u8 val;
160
161 ret = max8998_get_enable_register(rdev, &reg, &shift);
162 if (ret)
163 return ret;
164
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900165 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200166 if (ret)
167 return ret;
168
169 return val & (1 << shift);
170}
171
172static int max8998_ldo_enable(struct regulator_dev *rdev)
173{
174 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900175 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200176 int reg, shift = 8, ret;
177
178 ret = max8998_get_enable_register(rdev, &reg, &shift);
179 if (ret)
180 return ret;
181
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900182 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200183}
184
185static int max8998_ldo_disable(struct regulator_dev *rdev)
186{
187 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900188 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200189 int reg, shift = 8, ret;
190
191 ret = max8998_get_enable_register(rdev, &reg, &shift);
192 if (ret)
193 return ret;
194
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900195 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200196}
197
198static int max8998_get_voltage_register(struct regulator_dev *rdev,
199 int *_reg, int *_shift, int *_mask)
200{
Axel Lin7b947912012-03-01 09:27:29 +0800201 int ldo = rdev_get_id(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200202 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200203 int reg, shift = 0, mask = 0xff;
204
205 switch (ldo) {
206 case MAX8998_LDO2 ... MAX8998_LDO3:
207 reg = MAX8998_REG_LDO2_LDO3;
208 mask = 0xf;
209 if (ldo == MAX8998_LDO2)
210 shift = 4;
211 else
212 shift = 0;
213 break;
214 case MAX8998_LDO4 ... MAX8998_LDO7:
215 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
216 break;
217 case MAX8998_LDO8 ... MAX8998_LDO9:
218 reg = MAX8998_REG_LDO8_LDO9;
219 mask = 0xf;
220 if (ldo == MAX8998_LDO8)
221 shift = 4;
222 else
223 shift = 0;
224 break;
225 case MAX8998_LDO10 ... MAX8998_LDO11:
226 reg = MAX8998_REG_LDO10_LDO11;
227 if (ldo == MAX8998_LDO10) {
228 shift = 5;
229 mask = 0x7;
230 } else {
231 shift = 0;
232 mask = 0x1f;
233 }
234 break;
235 case MAX8998_LDO12 ... MAX8998_LDO17:
236 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
237 break;
238 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200239 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200240 break;
241 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200242 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200243 break;
244 case MAX8998_BUCK3:
245 reg = MAX8998_REG_BUCK3;
246 break;
247 case MAX8998_BUCK4:
248 reg = MAX8998_REG_BUCK4;
249 break;
250 default:
251 return -EINVAL;
252 }
253
254 *_reg = reg;
255 *_shift = shift;
256 *_mask = mask;
257
258 return 0;
259}
260
Axel Lin7b4354b2012-04-11 13:58:09 +0800261static int max8998_get_voltage_sel(struct regulator_dev *rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200262{
263 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900264 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200265 int reg, shift = 0, mask, ret;
266 u8 val;
267
268 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
269 if (ret)
270 return ret;
271
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900272 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200273 if (ret)
274 return ret;
275
276 val >>= shift;
277 val &= mask;
278
Axel Lin7b4354b2012-04-11 13:58:09 +0800279 return val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200280}
281
Axel Linbaae0192012-06-17 09:34:29 +0800282static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
283 unsigned selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200284{
285 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
286 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Linbaae0192012-06-17 09:34:29 +0800287 int reg, shift = 0, mask, ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000288
Lukasz Majewski9d924922010-09-27 14:32:23 +0200289 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
290 if (ret)
291 return ret;
292
Axel Linbaae0192012-06-17 09:34:29 +0800293 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
Lukasz Majewski9d924922010-09-27 14:32:23 +0200294
295 return ret;
296}
297
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200298static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
299{
300 gpio_set_value(gpio1, v & 0x1);
301 gpio_set_value(gpio2, (v >> 1) & 0x1);
302}
303
304static inline void buck2_gpio_set(int gpio, int v)
305{
306 gpio_set_value(gpio, v & 0x1);
307}
308
Axel Linbaae0192012-06-17 09:34:29 +0800309static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
310 unsigned selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200311{
312 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200313 struct max8998_platform_data *pdata =
314 dev_get_platdata(max8998->iodev->dev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900315 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Lin7b947912012-03-01 09:27:29 +0800316 int buck = rdev_get_id(rdev);
Axel Linc6163a72013-02-06 11:10:51 +0800317 int reg, shift = 0, mask, ret, j;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200318 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200319
Kyungmin Park156f2522010-06-16 09:04:16 +0200320 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
321 if (ret)
322 return ret;
323
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200324 switch (buck) {
325 case MAX8998_BUCK1:
326 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800327 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
Joe Perches85ee7a12011-04-23 20:38:19 -0700328 "buck1_vol3:%d, buck1_vol4:%d\n",
Axel Linbaae0192012-06-17 09:34:29 +0800329 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200330 max8998->buck1_vol[2], max8998->buck1_vol[3]);
331
332 if (gpio_is_valid(pdata->buck1_set1) &&
333 gpio_is_valid(pdata->buck1_set2)) {
334
335 /* check if requested voltage */
336 /* value is already defined */
337 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800338 if (max8998->buck1_vol[j] == selector) {
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200339 max8998->buck1_idx = j;
340 buck1_gpio_set(pdata->buck1_set1,
341 pdata->buck1_set2, j);
342 goto buck1_exit;
343 }
344 }
345
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100346 if (pdata->buck_voltage_lock)
347 return -EINVAL;
348
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200349 /* no predefine regulator found */
350 max8998->buck1_idx = (buck1_last_val % 2) + 2;
351 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
352 max8998->buck1_idx);
Axel Linbaae0192012-06-17 09:34:29 +0800353 max8998->buck1_vol[max8998->buck1_idx] = selector;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200354 ret = max8998_get_voltage_register(rdev, &reg,
355 &shift,
356 &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800357 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200358 buck1_gpio_set(pdata->buck1_set1,
359 pdata->buck1_set2, max8998->buck1_idx);
360 buck1_last_val++;
361buck1_exit:
362 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
363 i2c->name, gpio_get_value(pdata->buck1_set1),
364 gpio_get_value(pdata->buck1_set2));
365 break;
366 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800367 ret = max8998_write_reg(i2c, reg, selector);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200368 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200369 break;
370
371 case MAX8998_BUCK2:
372 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800373 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
374 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200375 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100376
377 /* check if requested voltage */
378 /* value is already defined */
379 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800380 if (max8998->buck2_vol[j] == selector) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100381 max8998->buck2_idx = j;
382 buck2_gpio_set(pdata->buck2_set3, j);
383 goto buck2_exit;
384 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200385 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100386
387 if (pdata->buck_voltage_lock)
388 return -EINVAL;
389
390 max8998_get_voltage_register(rdev,
391 &reg, &shift, &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800392 ret = max8998_write_reg(i2c, reg, selector);
393 max8998->buck2_vol[max8998->buck2_idx] = selector;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100394 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
395buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200396 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
397 gpio_get_value(pdata->buck2_set3));
398 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800399 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200400 }
401 break;
402
403 case MAX8998_BUCK3:
404 case MAX8998_BUCK4:
Axel Linbaae0192012-06-17 09:34:29 +0800405 ret = max8998_update_reg(i2c, reg, selector<<shift,
406 mask<<shift);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200407 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200408 }
409
Axel Lin276909d2012-04-11 13:59:14 +0800410 return ret;
411}
412
413static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
414 unsigned int old_selector,
415 unsigned int new_selector)
416{
417 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
418 struct i2c_client *i2c = max8998->iodev->i2c;
419 const struct voltage_map_desc *desc;
420 int buck = rdev_get_id(rdev);
421 u8 val = 0;
422 int difference, ret;
423
424 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
425 return -EINVAL;
426
427 desc = ldo_voltage_map[buck];
428
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200429 /* Voltage stabilization */
Axel Lin276909d2012-04-11 13:59:14 +0800430 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
431 if (ret)
432 return ret;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200433
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200434 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
435 /* MAX8998 has ENRAMP bit implemented, so test it*/
436 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
Axel Lin276909d2012-04-11 13:59:14 +0800437 return 0;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200438
Axel Linadf61782012-12-28 17:10:20 +0800439 difference = (new_selector - old_selector) * desc->step / 1000;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200440 if (difference > 0)
Axel Lin81d0a6a2013-01-09 19:34:57 +0800441 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200442
Axel Lin276909d2012-04-11 13:59:14 +0800443 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200444}
445
446static struct regulator_ops max8998_ldo_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800447 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800448 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200449 .is_enabled = max8998_ldo_is_enabled,
450 .enable = max8998_ldo_enable,
451 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800452 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800453 .set_voltage_sel = max8998_set_voltage_ldo_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200454};
455
456static struct regulator_ops max8998_buck_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800457 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800458 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200459 .is_enabled = max8998_ldo_is_enabled,
460 .enable = max8998_ldo_enable,
461 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800462 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800463 .set_voltage_sel = max8998_set_voltage_buck_sel,
Axel Lin276909d2012-04-11 13:59:14 +0800464 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200465};
466
467static struct regulator_ops max8998_others_ops = {
468 .is_enabled = max8998_ldo_is_enabled,
469 .enable = max8998_ldo_enable,
470 .disable = max8998_ldo_disable,
Kyungmin Park156f2522010-06-16 09:04:16 +0200471};
472
473static struct regulator_desc regulators[] = {
474 {
475 .name = "LDO2",
476 .id = MAX8998_LDO2,
477 .ops = &max8998_ldo_ops,
478 .type = REGULATOR_VOLTAGE,
479 .owner = THIS_MODULE,
480 }, {
481 .name = "LDO3",
482 .id = MAX8998_LDO3,
483 .ops = &max8998_ldo_ops,
484 .type = REGULATOR_VOLTAGE,
485 .owner = THIS_MODULE,
486 }, {
487 .name = "LDO4",
488 .id = MAX8998_LDO4,
489 .ops = &max8998_ldo_ops,
490 .type = REGULATOR_VOLTAGE,
491 .owner = THIS_MODULE,
492 }, {
493 .name = "LDO5",
494 .id = MAX8998_LDO5,
495 .ops = &max8998_ldo_ops,
496 .type = REGULATOR_VOLTAGE,
497 .owner = THIS_MODULE,
498 }, {
499 .name = "LDO6",
500 .id = MAX8998_LDO6,
501 .ops = &max8998_ldo_ops,
502 .type = REGULATOR_VOLTAGE,
503 .owner = THIS_MODULE,
504 }, {
505 .name = "LDO7",
506 .id = MAX8998_LDO7,
507 .ops = &max8998_ldo_ops,
508 .type = REGULATOR_VOLTAGE,
509 .owner = THIS_MODULE,
510 }, {
511 .name = "LDO8",
512 .id = MAX8998_LDO8,
513 .ops = &max8998_ldo_ops,
514 .type = REGULATOR_VOLTAGE,
515 .owner = THIS_MODULE,
516 }, {
517 .name = "LDO9",
518 .id = MAX8998_LDO9,
519 .ops = &max8998_ldo_ops,
520 .type = REGULATOR_VOLTAGE,
521 .owner = THIS_MODULE,
522 }, {
523 .name = "LDO10",
524 .id = MAX8998_LDO10,
525 .ops = &max8998_ldo_ops,
526 .type = REGULATOR_VOLTAGE,
527 .owner = THIS_MODULE,
528 }, {
529 .name = "LDO11",
530 .id = MAX8998_LDO11,
531 .ops = &max8998_ldo_ops,
532 .type = REGULATOR_VOLTAGE,
533 .owner = THIS_MODULE,
534 }, {
535 .name = "LDO12",
536 .id = MAX8998_LDO12,
537 .ops = &max8998_ldo_ops,
538 .type = REGULATOR_VOLTAGE,
539 .owner = THIS_MODULE,
540 }, {
541 .name = "LDO13",
542 .id = MAX8998_LDO13,
543 .ops = &max8998_ldo_ops,
544 .type = REGULATOR_VOLTAGE,
545 .owner = THIS_MODULE,
546 }, {
547 .name = "LDO14",
548 .id = MAX8998_LDO14,
549 .ops = &max8998_ldo_ops,
550 .type = REGULATOR_VOLTAGE,
551 .owner = THIS_MODULE,
552 }, {
553 .name = "LDO15",
554 .id = MAX8998_LDO15,
555 .ops = &max8998_ldo_ops,
556 .type = REGULATOR_VOLTAGE,
557 .owner = THIS_MODULE,
558 }, {
559 .name = "LDO16",
560 .id = MAX8998_LDO16,
561 .ops = &max8998_ldo_ops,
562 .type = REGULATOR_VOLTAGE,
563 .owner = THIS_MODULE,
564 }, {
565 .name = "LDO17",
566 .id = MAX8998_LDO17,
567 .ops = &max8998_ldo_ops,
568 .type = REGULATOR_VOLTAGE,
569 .owner = THIS_MODULE,
570 }, {
571 .name = "BUCK1",
572 .id = MAX8998_BUCK1,
573 .ops = &max8998_buck_ops,
574 .type = REGULATOR_VOLTAGE,
575 .owner = THIS_MODULE,
576 }, {
577 .name = "BUCK2",
578 .id = MAX8998_BUCK2,
579 .ops = &max8998_buck_ops,
580 .type = REGULATOR_VOLTAGE,
581 .owner = THIS_MODULE,
582 }, {
583 .name = "BUCK3",
584 .id = MAX8998_BUCK3,
585 .ops = &max8998_buck_ops,
586 .type = REGULATOR_VOLTAGE,
587 .owner = THIS_MODULE,
588 }, {
589 .name = "BUCK4",
590 .id = MAX8998_BUCK4,
591 .ops = &max8998_buck_ops,
592 .type = REGULATOR_VOLTAGE,
593 .owner = THIS_MODULE,
594 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200595 .name = "EN32KHz-AP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200596 .id = MAX8998_EN32KHZ_AP,
597 .ops = &max8998_others_ops,
598 .type = REGULATOR_VOLTAGE,
599 .owner = THIS_MODULE,
600 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200601 .name = "EN32KHz-CP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200602 .id = MAX8998_EN32KHZ_CP,
603 .ops = &max8998_others_ops,
604 .type = REGULATOR_VOLTAGE,
605 .owner = THIS_MODULE,
606 }, {
607 .name = "ENVICHG",
608 .id = MAX8998_ENVICHG,
609 .ops = &max8998_others_ops,
610 .type = REGULATOR_VOLTAGE,
611 .owner = THIS_MODULE,
612 }, {
613 .name = "ESAFEOUT1",
614 .id = MAX8998_ESAFEOUT1,
615 .ops = &max8998_others_ops,
616 .type = REGULATOR_VOLTAGE,
617 .owner = THIS_MODULE,
618 }, {
619 .name = "ESAFEOUT2",
620 .id = MAX8998_ESAFEOUT2,
621 .ops = &max8998_others_ops,
622 .type = REGULATOR_VOLTAGE,
623 .owner = THIS_MODULE,
624 }
625};
626
Tomasz Figaee999fb2013-06-25 16:08:10 +0200627static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
628 struct max8998_platform_data *pdata,
629 struct device_node *pmic_np)
630{
631 int gpio;
632
633 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
634 if (!gpio_is_valid(gpio)) {
635 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
636 return -EINVAL;
637 }
638 pdata->buck1_set1 = gpio;
639
640 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
641 if (!gpio_is_valid(gpio)) {
642 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
643 return -EINVAL;
644 }
645 pdata->buck1_set2 = gpio;
646
647 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
648 if (!gpio_is_valid(gpio)) {
649 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
650 return -EINVAL;
651 }
652 pdata->buck2_set3 = gpio;
653
654 return 0;
655}
656
657static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
658 struct max8998_platform_data *pdata)
659{
660 struct device_node *pmic_np = iodev->dev->of_node;
661 struct device_node *regulators_np, *reg_np;
662 struct max8998_regulator_data *rdata;
663 unsigned int i;
664 int ret;
665
666 regulators_np = of_get_child_by_name(pmic_np, "regulators");
667 if (!regulators_np) {
668 dev_err(iodev->dev, "could not find regulators sub-node\n");
669 return -EINVAL;
670 }
671
672 /* count the number of regulators to be supported in pmic */
673 pdata->num_regulators = of_get_child_count(regulators_np);
674
675 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
676 pdata->num_regulators, GFP_KERNEL);
677 if (!rdata)
678 return -ENOMEM;
679
680 pdata->regulators = rdata;
681 for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
682 reg_np = of_get_child_by_name(regulators_np,
683 regulators[i].name);
684 if (!reg_np)
685 continue;
686
687 rdata->id = regulators[i].id;
688 rdata->initdata = of_get_regulator_init_data(
689 iodev->dev, reg_np);
690 rdata->reg_node = reg_np;
691 ++rdata;
692 }
693 pdata->num_regulators = rdata - pdata->regulators;
694
695 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
696 if (ret)
697 return -EINVAL;
698
699 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
700 pdata->buck_voltage_lock = true;
701
702 ret = of_property_read_u32(pmic_np,
703 "max8998,pmic-buck1-default-dvs-idx",
704 &pdata->buck1_default_idx);
705 if (!ret && pdata->buck1_default_idx >= 4) {
706 pdata->buck1_default_idx = 0;
707 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
708 }
709
710 ret = of_property_read_u32(pmic_np,
711 "max8998,pmic-buck2-default-dvs-idx",
712 &pdata->buck2_default_idx);
713 if (!ret && pdata->buck2_default_idx >= 2) {
714 pdata->buck2_default_idx = 0;
715 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
716 }
717
718 ret = of_property_read_u32_array(pmic_np,
719 "max8998,pmic-buck1-dvs-voltage",
720 pdata->buck1_voltage,
721 ARRAY_SIZE(pdata->buck1_voltage));
722 if (ret) {
723 dev_err(iodev->dev, "buck1 voltages not specified\n");
724 return -EINVAL;
725 }
726
727 ret = of_property_read_u32_array(pmic_np,
728 "max8998,pmic-buck2-dvs-voltage",
729 pdata->buck2_voltage,
730 ARRAY_SIZE(pdata->buck2_voltage));
731 if (ret) {
732 dev_err(iodev->dev, "buck2 voltages not specified\n");
733 return -EINVAL;
734 }
735
736 return 0;
737}
738
Bill Pembertona5023572012-11-19 13:22:22 -0500739static int max8998_pmic_probe(struct platform_device *pdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200740{
741 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200742 struct max8998_platform_data *pdata = iodev->pdata;
Mark Brownc1727082012-04-04 00:50:22 +0100743 struct regulator_config config = { };
Kyungmin Park156f2522010-06-16 09:04:16 +0200744 struct regulator_dev **rdev;
745 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200746 struct i2c_client *i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200747 int i, ret, size;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200748 unsigned int v;
Kyungmin Park156f2522010-06-16 09:04:16 +0200749
750 if (!pdata) {
751 dev_err(pdev->dev.parent, "No platform init data supplied\n");
752 return -ENODEV;
753 }
754
Tomasz Figaee999fb2013-06-25 16:08:10 +0200755 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
756 ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
757 if (ret)
758 return ret;
759 }
760
Axel Lin0f80ea12012-04-19 09:47:37 +0800761 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
762 GFP_KERNEL);
Kyungmin Park156f2522010-06-16 09:04:16 +0200763 if (!max8998)
764 return -ENOMEM;
765
Axel Lin11fa0d12010-08-19 10:29:19 +0800766 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
Axel Lin0f80ea12012-04-19 09:47:37 +0800767 max8998->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
768 if (!max8998->rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200769 return -ENOMEM;
Kyungmin Park156f2522010-06-16 09:04:16 +0200770
771 rdev = max8998->rdev;
Axel Lin747cc852010-08-27 16:37:34 +0800772 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200773 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800774 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200775 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200776 i2c = max8998->iodev->i2c;
777
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100778 max8998->buck1_idx = pdata->buck1_default_idx;
779 max8998->buck2_idx = pdata->buck2_default_idx;
780
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200781 /* NOTE: */
782 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
783 /* will be displayed */
784
785 /* Check if MAX8998 voltage selection GPIOs are defined */
786 if (gpio_is_valid(pdata->buck1_set1) &&
787 gpio_is_valid(pdata->buck1_set2)) {
788 /* Check if SET1 is not equal to 0 */
789 if (!pdata->buck1_set1) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300790 dev_err(&pdev->dev,
791 "MAX8998 SET1 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200792 WARN_ON(!pdata->buck1_set1);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530793 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200794 }
795 /* Check if SET2 is not equal to 0 */
796 if (!pdata->buck1_set2) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300797 dev_err(&pdev->dev,
798 "MAX8998 SET2 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200799 WARN_ON(!pdata->buck1_set2);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530800 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200801 }
802
803 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
804 gpio_direction_output(pdata->buck1_set1,
805 max8998->buck1_idx & 0x1);
806
807
808 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
809 gpio_direction_output(pdata->buck1_set2,
810 (max8998->buck1_idx >> 1) & 0x1);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200811
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200812 /* Set predefined values for BUCK1 registers */
813 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
814 i = 0;
815 while (buck12_voltage_map_desc.min +
816 buck12_voltage_map_desc.step*i
817 < pdata->buck1_voltage[v])
818 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200819
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200820 max8998->buck1_vol[v] = i;
821 ret = max8998_write_reg(i2c,
822 MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
823 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530824 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200825 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200826 }
827
828 if (gpio_is_valid(pdata->buck2_set3)) {
829 /* Check if SET3 is not equal to 0 */
830 if (!pdata->buck2_set3) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300831 dev_err(&pdev->dev,
832 "MAX8998 SET3 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200833 WARN_ON(!pdata->buck2_set3);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530834 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200835 }
836 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
837 gpio_direction_output(pdata->buck2_set3,
838 max8998->buck2_idx & 0x1);
839
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200840 /* Set predefined values for BUCK2 registers */
841 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
842 i = 0;
843 while (buck12_voltage_map_desc.min +
844 buck12_voltage_map_desc.step*i
845 < pdata->buck2_voltage[v])
846 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200847
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200848 max8998->buck2_vol[v] = i;
849 ret = max8998_write_reg(i2c,
850 MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
851 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530852 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200853 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200854 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200855
856 for (i = 0; i < pdata->num_regulators; i++) {
857 const struct voltage_map_desc *desc;
858 int id = pdata->regulators[i].id;
859 int index = id - MAX8998_LDO2;
860
861 desc = ldo_voltage_map[id];
862 if (desc && regulators[index].ops != &max8998_others_ops) {
863 int count = (desc->max - desc->min) / desc->step + 1;
Axel Lin9a0fbb62012-06-17 09:33:07 +0800864
Kyungmin Park156f2522010-06-16 09:04:16 +0200865 regulators[index].n_voltages = count;
Axel Linadf61782012-12-28 17:10:20 +0800866 regulators[index].min_uV = desc->min;
867 regulators[index].uV_step = desc->step;
Kyungmin Park156f2522010-06-16 09:04:16 +0200868 }
Mark Brownc1727082012-04-04 00:50:22 +0100869
870 config.dev = max8998->dev;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200871 config.of_node = pdata->regulators[i].reg_node;
Mark Brownc1727082012-04-04 00:50:22 +0100872 config.init_data = pdata->regulators[i].initdata;
873 config.driver_data = max8998;
874
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530875 rdev[i] = devm_regulator_register(&pdev->dev,
876 &regulators[index], &config);
Kyungmin Park156f2522010-06-16 09:04:16 +0200877 if (IS_ERR(rdev[i])) {
878 ret = PTR_ERR(rdev[i]);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200879 dev_err(max8998->dev, "regulator %s init failed (%d)\n",
880 regulators[index].name, ret);
Kyungmin Park156f2522010-06-16 09:04:16 +0200881 rdev[i] = NULL;
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530882 return ret;
Kyungmin Park156f2522010-06-16 09:04:16 +0200883 }
884 }
885
886
887 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200888}
889
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900890static const struct platform_device_id max8998_pmic_id[] = {
891 { "max8998-pmic", TYPE_MAX8998 },
892 { "lp3974-pmic", TYPE_LP3974 },
893 { }
894};
Axel Lina51b9072011-03-26 23:28:42 +0800895MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900896
Kyungmin Park156f2522010-06-16 09:04:16 +0200897static struct platform_driver max8998_pmic_driver = {
898 .driver = {
899 .name = "max8998-pmic",
900 .owner = THIS_MODULE,
901 },
902 .probe = max8998_pmic_probe,
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900903 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200904};
905
906static int __init max8998_pmic_init(void)
907{
908 return platform_driver_register(&max8998_pmic_driver);
909}
910subsys_initcall(max8998_pmic_init);
911
912static void __exit max8998_pmic_cleanup(void)
913{
914 platform_driver_unregister(&max8998_pmic_driver);
915}
916module_exit(max8998_pmic_cleanup);
917
918MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
919MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
920MODULE_LICENSE("GPL");