blob: 3027e7ce100b9c3df4ad39b59d2f512eb55ad19e [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;
Lukasz Majewski889cd5a2010-09-27 14:32:25 +020043 u8 buck1_vol[4]; /* voltages for selection */
44 u8 buck2_vol[2];
45 unsigned int buck1_idx; /* index to last changed voltage */
46 /* value in a set */
47 unsigned int buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +020048};
49
50struct voltage_map_desc {
51 int min;
52 int max;
53 int step;
54};
55
Axel Linadf61782012-12-28 17:10:20 +080056/* Voltage maps in uV*/
Kyungmin Park156f2522010-06-16 09:04:16 +020057static const struct voltage_map_desc ldo23_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080058 .min = 800000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020059};
60static const struct voltage_map_desc ldo456711_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080061 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020062};
63static const struct voltage_map_desc ldo8_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080064 .min = 3000000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020065};
66static const struct voltage_map_desc ldo9_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080067 .min = 2800000, .step = 100000, .max = 3100000,
Kyungmin Park156f2522010-06-16 09:04:16 +020068};
69static const struct voltage_map_desc ldo10_voltage_map_desc = {
Marek Szyprowski36a6f8c2013-01-25 09:25:40 +010070 .min = 950000, .step = 50000, .max = 1300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020071};
72static const struct voltage_map_desc ldo1213_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080073 .min = 800000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020074};
75static const struct voltage_map_desc ldo1415_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080076 .min = 1200000, .step = 100000, .max = 3300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020077};
78static const struct voltage_map_desc ldo1617_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080079 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020080};
81static const struct voltage_map_desc buck12_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080082 .min = 750000, .step = 25000, .max = 1525000,
Kyungmin Park156f2522010-06-16 09:04:16 +020083};
84static const struct voltage_map_desc buck3_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080085 .min = 1600000, .step = 100000, .max = 3600000,
Kyungmin Park156f2522010-06-16 09:04:16 +020086};
87static const struct voltage_map_desc buck4_voltage_map_desc = {
Axel Linadf61782012-12-28 17:10:20 +080088 .min = 800000, .step = 100000, .max = 2300000,
Kyungmin Park156f2522010-06-16 09:04:16 +020089};
90
91static const struct voltage_map_desc *ldo_voltage_map[] = {
92 NULL,
93 NULL,
94 &ldo23_voltage_map_desc, /* LDO2 */
95 &ldo23_voltage_map_desc, /* LDO3 */
96 &ldo456711_voltage_map_desc, /* LDO4 */
97 &ldo456711_voltage_map_desc, /* LDO5 */
98 &ldo456711_voltage_map_desc, /* LDO6 */
99 &ldo456711_voltage_map_desc, /* LDO7 */
100 &ldo8_voltage_map_desc, /* LDO8 */
101 &ldo9_voltage_map_desc, /* LDO9 */
102 &ldo10_voltage_map_desc, /* LDO10 */
103 &ldo456711_voltage_map_desc, /* LDO11 */
104 &ldo1213_voltage_map_desc, /* LDO12 */
105 &ldo1213_voltage_map_desc, /* LDO13 */
106 &ldo1415_voltage_map_desc, /* LDO14 */
107 &ldo1415_voltage_map_desc, /* LDO15 */
108 &ldo1617_voltage_map_desc, /* LDO16 */
109 &ldo1617_voltage_map_desc, /* LDO17 */
110 &buck12_voltage_map_desc, /* BUCK1 */
111 &buck12_voltage_map_desc, /* BUCK2 */
112 &buck3_voltage_map_desc, /* BUCK3 */
113 &buck4_voltage_map_desc, /* BUCK4 */
114};
115
Kyungmin Park156f2522010-06-16 09:04:16 +0200116static int max8998_get_enable_register(struct regulator_dev *rdev,
117 int *reg, int *shift)
118{
Axel Lin7b947912012-03-01 09:27:29 +0800119 int ldo = rdev_get_id(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200120
121 switch (ldo) {
122 case MAX8998_LDO2 ... MAX8998_LDO5:
123 *reg = MAX8998_REG_ONOFF1;
124 *shift = 3 - (ldo - MAX8998_LDO2);
125 break;
126 case MAX8998_LDO6 ... MAX8998_LDO13:
127 *reg = MAX8998_REG_ONOFF2;
128 *shift = 7 - (ldo - MAX8998_LDO6);
129 break;
130 case MAX8998_LDO14 ... MAX8998_LDO17:
131 *reg = MAX8998_REG_ONOFF3;
132 *shift = 7 - (ldo - MAX8998_LDO14);
133 break;
134 case MAX8998_BUCK1 ... MAX8998_BUCK4:
135 *reg = MAX8998_REG_ONOFF1;
136 *shift = 7 - (ldo - MAX8998_BUCK1);
137 break;
138 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
139 *reg = MAX8998_REG_ONOFF4;
140 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
141 break;
142 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
143 *reg = MAX8998_REG_CHGR2;
144 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
145 break;
146 default:
147 return -EINVAL;
148 }
149
150 return 0;
151}
152
153static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
154{
155 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900156 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200157 int ret, reg, shift = 8;
158 u8 val;
159
160 ret = max8998_get_enable_register(rdev, &reg, &shift);
161 if (ret)
162 return ret;
163
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900164 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200165 if (ret)
166 return ret;
167
168 return val & (1 << shift);
169}
170
171static int max8998_ldo_enable(struct regulator_dev *rdev)
172{
173 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900174 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200175 int reg, shift = 8, ret;
176
177 ret = max8998_get_enable_register(rdev, &reg, &shift);
178 if (ret)
179 return ret;
180
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900181 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200182}
183
184static int max8998_ldo_disable(struct regulator_dev *rdev)
185{
186 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900187 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200188 int reg, shift = 8, ret;
189
190 ret = max8998_get_enable_register(rdev, &reg, &shift);
191 if (ret)
192 return ret;
193
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900194 return max8998_update_reg(i2c, reg, 0, 1<<shift);
Kyungmin Park156f2522010-06-16 09:04:16 +0200195}
196
197static int max8998_get_voltage_register(struct regulator_dev *rdev,
198 int *_reg, int *_shift, int *_mask)
199{
Axel Lin7b947912012-03-01 09:27:29 +0800200 int ldo = rdev_get_id(rdev);
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200201 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200202 int reg, shift = 0, mask = 0xff;
203
204 switch (ldo) {
205 case MAX8998_LDO2 ... MAX8998_LDO3:
206 reg = MAX8998_REG_LDO2_LDO3;
207 mask = 0xf;
208 if (ldo == MAX8998_LDO2)
209 shift = 4;
210 else
211 shift = 0;
212 break;
213 case MAX8998_LDO4 ... MAX8998_LDO7:
214 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
215 break;
216 case MAX8998_LDO8 ... MAX8998_LDO9:
217 reg = MAX8998_REG_LDO8_LDO9;
218 mask = 0xf;
219 if (ldo == MAX8998_LDO8)
220 shift = 4;
221 else
222 shift = 0;
223 break;
224 case MAX8998_LDO10 ... MAX8998_LDO11:
225 reg = MAX8998_REG_LDO10_LDO11;
226 if (ldo == MAX8998_LDO10) {
227 shift = 5;
228 mask = 0x7;
229 } else {
230 shift = 0;
231 mask = 0x1f;
232 }
233 break;
234 case MAX8998_LDO12 ... MAX8998_LDO17:
235 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
236 break;
237 case MAX8998_BUCK1:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200238 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200239 break;
240 case MAX8998_BUCK2:
Lukasz Majewski889cd5a2010-09-27 14:32:25 +0200241 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
Kyungmin Park156f2522010-06-16 09:04:16 +0200242 break;
243 case MAX8998_BUCK3:
244 reg = MAX8998_REG_BUCK3;
245 break;
246 case MAX8998_BUCK4:
247 reg = MAX8998_REG_BUCK4;
248 break;
249 default:
250 return -EINVAL;
251 }
252
253 *_reg = reg;
254 *_shift = shift;
255 *_mask = mask;
256
257 return 0;
258}
259
Axel Lin7b4354b2012-04-11 13:58:09 +0800260static int max8998_get_voltage_sel(struct regulator_dev *rdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200261{
262 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900263 struct i2c_client *i2c = max8998->iodev->i2c;
Kyungmin Park156f2522010-06-16 09:04:16 +0200264 int reg, shift = 0, mask, ret;
265 u8 val;
266
267 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
268 if (ret)
269 return ret;
270
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900271 ret = max8998_read_reg(i2c, reg, &val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200272 if (ret)
273 return ret;
274
275 val >>= shift;
276 val &= mask;
277
Axel Lin7b4354b2012-04-11 13:58:09 +0800278 return val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200279}
280
Axel Linbaae0192012-06-17 09:34:29 +0800281static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
282 unsigned selector)
Lukasz Majewski9d924922010-09-27 14:32:23 +0200283{
284 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
285 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Linbaae0192012-06-17 09:34:29 +0800286 int reg, shift = 0, mask, ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000287
Lukasz Majewski9d924922010-09-27 14:32:23 +0200288 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
289 if (ret)
290 return ret;
291
Axel Linbaae0192012-06-17 09:34:29 +0800292 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
Lukasz Majewski9d924922010-09-27 14:32:23 +0200293
294 return ret;
295}
296
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200297static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
298{
299 gpio_set_value(gpio1, v & 0x1);
300 gpio_set_value(gpio2, (v >> 1) & 0x1);
301}
302
303static inline void buck2_gpio_set(int gpio, int v)
304{
305 gpio_set_value(gpio, v & 0x1);
306}
307
Axel Linbaae0192012-06-17 09:34:29 +0800308static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
309 unsigned selector)
Kyungmin Park156f2522010-06-16 09:04:16 +0200310{
311 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200312 struct max8998_platform_data *pdata =
313 dev_get_platdata(max8998->iodev->dev);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900314 struct i2c_client *i2c = max8998->iodev->i2c;
Axel Lin7b947912012-03-01 09:27:29 +0800315 int buck = rdev_get_id(rdev);
Axel Linc6163a72013-02-06 11:10:51 +0800316 int reg, shift = 0, mask, ret, j;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200317 static u8 buck1_last_val;
Kyungmin Park156f2522010-06-16 09:04:16 +0200318
Kyungmin Park156f2522010-06-16 09:04:16 +0200319 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
320 if (ret)
321 return ret;
322
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200323 switch (buck) {
324 case MAX8998_BUCK1:
325 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800326 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
Joe Perches85ee7a12011-04-23 20:38:19 -0700327 "buck1_vol3:%d, buck1_vol4:%d\n",
Axel Linbaae0192012-06-17 09:34:29 +0800328 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200329 max8998->buck1_vol[2], max8998->buck1_vol[3]);
330
331 if (gpio_is_valid(pdata->buck1_set1) &&
332 gpio_is_valid(pdata->buck1_set2)) {
333
334 /* check if requested voltage */
335 /* value is already defined */
336 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800337 if (max8998->buck1_vol[j] == selector) {
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200338 max8998->buck1_idx = j;
339 buck1_gpio_set(pdata->buck1_set1,
340 pdata->buck1_set2, j);
341 goto buck1_exit;
342 }
343 }
344
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100345 if (pdata->buck_voltage_lock)
346 return -EINVAL;
347
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200348 /* no predefine regulator found */
349 max8998->buck1_idx = (buck1_last_val % 2) + 2;
350 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
351 max8998->buck1_idx);
Axel Linbaae0192012-06-17 09:34:29 +0800352 max8998->buck1_vol[max8998->buck1_idx] = selector;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200353 ret = max8998_get_voltage_register(rdev, &reg,
354 &shift,
355 &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800356 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200357 buck1_gpio_set(pdata->buck1_set1,
358 pdata->buck1_set2, max8998->buck1_idx);
359 buck1_last_val++;
360buck1_exit:
361 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
362 i2c->name, gpio_get_value(pdata->buck1_set1),
363 gpio_get_value(pdata->buck1_set2));
364 break;
365 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800366 ret = max8998_write_reg(i2c, reg, selector);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200367 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200368 break;
369
370 case MAX8998_BUCK2:
371 dev_dbg(max8998->dev,
Axel Linbaae0192012-06-17 09:34:29 +0800372 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
373 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200374 if (gpio_is_valid(pdata->buck2_set3)) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100375
376 /* check if requested voltage */
377 /* value is already defined */
378 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
Axel Linbaae0192012-06-17 09:34:29 +0800379 if (max8998->buck2_vol[j] == selector) {
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100380 max8998->buck2_idx = j;
381 buck2_gpio_set(pdata->buck2_set3, j);
382 goto buck2_exit;
383 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200384 }
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100385
386 if (pdata->buck_voltage_lock)
387 return -EINVAL;
388
389 max8998_get_voltage_register(rdev,
390 &reg, &shift, &mask);
Axel Linbaae0192012-06-17 09:34:29 +0800391 ret = max8998_write_reg(i2c, reg, selector);
392 max8998->buck2_vol[max8998->buck2_idx] = selector;
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100393 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
394buck2_exit:
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200395 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
396 gpio_get_value(pdata->buck2_set3));
397 } else {
Axel Linbaae0192012-06-17 09:34:29 +0800398 ret = max8998_write_reg(i2c, reg, selector);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200399 }
400 break;
401
402 case MAX8998_BUCK3:
403 case MAX8998_BUCK4:
Axel Linbaae0192012-06-17 09:34:29 +0800404 ret = max8998_update_reg(i2c, reg, selector<<shift,
405 mask<<shift);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200406 break;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200407 }
408
Axel Lin276909d2012-04-11 13:59:14 +0800409 return ret;
410}
411
412static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
413 unsigned int old_selector,
414 unsigned int new_selector)
415{
416 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
417 struct i2c_client *i2c = max8998->iodev->i2c;
418 const struct voltage_map_desc *desc;
419 int buck = rdev_get_id(rdev);
420 u8 val = 0;
421 int difference, ret;
422
423 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
424 return -EINVAL;
425
426 desc = ldo_voltage_map[buck];
427
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200428 /* Voltage stabilization */
Axel Lin276909d2012-04-11 13:59:14 +0800429 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
430 if (ret)
431 return ret;
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200432
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200433 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
434 /* MAX8998 has ENRAMP bit implemented, so test it*/
435 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
Axel Lin276909d2012-04-11 13:59:14 +0800436 return 0;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200437
Axel Linadf61782012-12-28 17:10:20 +0800438 difference = (new_selector - old_selector) * desc->step / 1000;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200439 if (difference > 0)
Axel Lin81d0a6a2013-01-09 19:34:57 +0800440 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
MyungJoo Hamc5a46552010-07-20 08:07:07 +0200441
Axel Lin276909d2012-04-11 13:59:14 +0800442 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200443}
444
445static struct regulator_ops max8998_ldo_ops = {
Axel Lin9a0fbb62012-06-17 09:33:07 +0800446 .list_voltage = regulator_list_voltage_linear,
Axel Linbaae0192012-06-17 09:34:29 +0800447 .map_voltage = regulator_map_voltage_linear,
Kyungmin Park156f2522010-06-16 09:04:16 +0200448 .is_enabled = max8998_ldo_is_enabled,
449 .enable = max8998_ldo_enable,
450 .disable = max8998_ldo_disable,
Axel Lin7b4354b2012-04-11 13:58:09 +0800451 .get_voltage_sel = max8998_get_voltage_sel,
Axel Linbaae0192012-06-17 09:34:29 +0800452 .set_voltage_sel = max8998_set_voltage_ldo_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200453};
454
455static struct regulator_ops max8998_buck_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_buck_sel,
Axel Lin276909d2012-04-11 13:59:14 +0800463 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
Kyungmin Park156f2522010-06-16 09:04:16 +0200464};
465
466static struct regulator_ops max8998_others_ops = {
467 .is_enabled = max8998_ldo_is_enabled,
468 .enable = max8998_ldo_enable,
469 .disable = max8998_ldo_disable,
Kyungmin Park156f2522010-06-16 09:04:16 +0200470};
471
472static struct regulator_desc regulators[] = {
473 {
474 .name = "LDO2",
475 .id = MAX8998_LDO2,
476 .ops = &max8998_ldo_ops,
477 .type = REGULATOR_VOLTAGE,
478 .owner = THIS_MODULE,
479 }, {
480 .name = "LDO3",
481 .id = MAX8998_LDO3,
482 .ops = &max8998_ldo_ops,
483 .type = REGULATOR_VOLTAGE,
484 .owner = THIS_MODULE,
485 }, {
486 .name = "LDO4",
487 .id = MAX8998_LDO4,
488 .ops = &max8998_ldo_ops,
489 .type = REGULATOR_VOLTAGE,
490 .owner = THIS_MODULE,
491 }, {
492 .name = "LDO5",
493 .id = MAX8998_LDO5,
494 .ops = &max8998_ldo_ops,
495 .type = REGULATOR_VOLTAGE,
496 .owner = THIS_MODULE,
497 }, {
498 .name = "LDO6",
499 .id = MAX8998_LDO6,
500 .ops = &max8998_ldo_ops,
501 .type = REGULATOR_VOLTAGE,
502 .owner = THIS_MODULE,
503 }, {
504 .name = "LDO7",
505 .id = MAX8998_LDO7,
506 .ops = &max8998_ldo_ops,
507 .type = REGULATOR_VOLTAGE,
508 .owner = THIS_MODULE,
509 }, {
510 .name = "LDO8",
511 .id = MAX8998_LDO8,
512 .ops = &max8998_ldo_ops,
513 .type = REGULATOR_VOLTAGE,
514 .owner = THIS_MODULE,
515 }, {
516 .name = "LDO9",
517 .id = MAX8998_LDO9,
518 .ops = &max8998_ldo_ops,
519 .type = REGULATOR_VOLTAGE,
520 .owner = THIS_MODULE,
521 }, {
522 .name = "LDO10",
523 .id = MAX8998_LDO10,
524 .ops = &max8998_ldo_ops,
525 .type = REGULATOR_VOLTAGE,
526 .owner = THIS_MODULE,
527 }, {
528 .name = "LDO11",
529 .id = MAX8998_LDO11,
530 .ops = &max8998_ldo_ops,
531 .type = REGULATOR_VOLTAGE,
532 .owner = THIS_MODULE,
533 }, {
534 .name = "LDO12",
535 .id = MAX8998_LDO12,
536 .ops = &max8998_ldo_ops,
537 .type = REGULATOR_VOLTAGE,
538 .owner = THIS_MODULE,
539 }, {
540 .name = "LDO13",
541 .id = MAX8998_LDO13,
542 .ops = &max8998_ldo_ops,
543 .type = REGULATOR_VOLTAGE,
544 .owner = THIS_MODULE,
545 }, {
546 .name = "LDO14",
547 .id = MAX8998_LDO14,
548 .ops = &max8998_ldo_ops,
549 .type = REGULATOR_VOLTAGE,
550 .owner = THIS_MODULE,
551 }, {
552 .name = "LDO15",
553 .id = MAX8998_LDO15,
554 .ops = &max8998_ldo_ops,
555 .type = REGULATOR_VOLTAGE,
556 .owner = THIS_MODULE,
557 }, {
558 .name = "LDO16",
559 .id = MAX8998_LDO16,
560 .ops = &max8998_ldo_ops,
561 .type = REGULATOR_VOLTAGE,
562 .owner = THIS_MODULE,
563 }, {
564 .name = "LDO17",
565 .id = MAX8998_LDO17,
566 .ops = &max8998_ldo_ops,
567 .type = REGULATOR_VOLTAGE,
568 .owner = THIS_MODULE,
569 }, {
570 .name = "BUCK1",
571 .id = MAX8998_BUCK1,
572 .ops = &max8998_buck_ops,
573 .type = REGULATOR_VOLTAGE,
574 .owner = THIS_MODULE,
575 }, {
576 .name = "BUCK2",
577 .id = MAX8998_BUCK2,
578 .ops = &max8998_buck_ops,
579 .type = REGULATOR_VOLTAGE,
580 .owner = THIS_MODULE,
581 }, {
582 .name = "BUCK3",
583 .id = MAX8998_BUCK3,
584 .ops = &max8998_buck_ops,
585 .type = REGULATOR_VOLTAGE,
586 .owner = THIS_MODULE,
587 }, {
588 .name = "BUCK4",
589 .id = MAX8998_BUCK4,
590 .ops = &max8998_buck_ops,
591 .type = REGULATOR_VOLTAGE,
592 .owner = THIS_MODULE,
593 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200594 .name = "EN32KHz-AP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200595 .id = MAX8998_EN32KHZ_AP,
596 .ops = &max8998_others_ops,
597 .type = REGULATOR_VOLTAGE,
598 .owner = THIS_MODULE,
599 }, {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200600 .name = "EN32KHz-CP",
Kyungmin Park156f2522010-06-16 09:04:16 +0200601 .id = MAX8998_EN32KHZ_CP,
602 .ops = &max8998_others_ops,
603 .type = REGULATOR_VOLTAGE,
604 .owner = THIS_MODULE,
605 }, {
606 .name = "ENVICHG",
607 .id = MAX8998_ENVICHG,
608 .ops = &max8998_others_ops,
609 .type = REGULATOR_VOLTAGE,
610 .owner = THIS_MODULE,
611 }, {
612 .name = "ESAFEOUT1",
613 .id = MAX8998_ESAFEOUT1,
614 .ops = &max8998_others_ops,
615 .type = REGULATOR_VOLTAGE,
616 .owner = THIS_MODULE,
617 }, {
618 .name = "ESAFEOUT2",
619 .id = MAX8998_ESAFEOUT2,
620 .ops = &max8998_others_ops,
621 .type = REGULATOR_VOLTAGE,
622 .owner = THIS_MODULE,
623 }
624};
625
Tomasz Figaee999fb2013-06-25 16:08:10 +0200626static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
627 struct max8998_platform_data *pdata,
628 struct device_node *pmic_np)
629{
630 int gpio;
631
632 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
633 if (!gpio_is_valid(gpio)) {
634 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
635 return -EINVAL;
636 }
637 pdata->buck1_set1 = gpio;
638
639 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
640 if (!gpio_is_valid(gpio)) {
641 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
642 return -EINVAL;
643 }
644 pdata->buck1_set2 = gpio;
645
646 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
647 if (!gpio_is_valid(gpio)) {
648 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
649 return -EINVAL;
650 }
651 pdata->buck2_set3 = gpio;
652
653 return 0;
654}
655
656static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
657 struct max8998_platform_data *pdata)
658{
659 struct device_node *pmic_np = iodev->dev->of_node;
660 struct device_node *regulators_np, *reg_np;
661 struct max8998_regulator_data *rdata;
662 unsigned int i;
663 int ret;
664
665 regulators_np = of_get_child_by_name(pmic_np, "regulators");
666 if (!regulators_np) {
667 dev_err(iodev->dev, "could not find regulators sub-node\n");
668 return -EINVAL;
669 }
670
671 /* count the number of regulators to be supported in pmic */
672 pdata->num_regulators = of_get_child_count(regulators_np);
673
674 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
675 pdata->num_regulators, GFP_KERNEL);
Sachin Kamat58c95372014-02-17 14:33:35 +0530676 if (!rdata) {
677 of_node_put(regulators_np);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200678 return -ENOMEM;
Sachin Kamat58c95372014-02-17 14:33:35 +0530679 }
Tomasz Figaee999fb2013-06-25 16:08:10 +0200680
681 pdata->regulators = rdata;
682 for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
683 reg_np = of_get_child_by_name(regulators_np,
684 regulators[i].name);
685 if (!reg_np)
686 continue;
687
688 rdata->id = regulators[i].id;
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100689 rdata->initdata = of_get_regulator_init_data(iodev->dev,
690 reg_np,
691 &regulators[i]);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200692 rdata->reg_node = reg_np;
693 ++rdata;
694 }
695 pdata->num_regulators = rdata - pdata->regulators;
696
Sachin Kamat58c95372014-02-17 14:33:35 +0530697 of_node_put(reg_np);
698 of_node_put(regulators_np);
699
Tomasz Figaee999fb2013-06-25 16:08:10 +0200700 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
701 if (ret)
702 return -EINVAL;
703
704 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
705 pdata->buck_voltage_lock = true;
706
707 ret = of_property_read_u32(pmic_np,
708 "max8998,pmic-buck1-default-dvs-idx",
709 &pdata->buck1_default_idx);
710 if (!ret && pdata->buck1_default_idx >= 4) {
711 pdata->buck1_default_idx = 0;
712 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
713 }
714
715 ret = of_property_read_u32(pmic_np,
716 "max8998,pmic-buck2-default-dvs-idx",
717 &pdata->buck2_default_idx);
718 if (!ret && pdata->buck2_default_idx >= 2) {
719 pdata->buck2_default_idx = 0;
720 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
721 }
722
723 ret = of_property_read_u32_array(pmic_np,
724 "max8998,pmic-buck1-dvs-voltage",
725 pdata->buck1_voltage,
726 ARRAY_SIZE(pdata->buck1_voltage));
727 if (ret) {
728 dev_err(iodev->dev, "buck1 voltages not specified\n");
729 return -EINVAL;
730 }
731
732 ret = of_property_read_u32_array(pmic_np,
733 "max8998,pmic-buck2-dvs-voltage",
734 pdata->buck2_voltage,
735 ARRAY_SIZE(pdata->buck2_voltage));
736 if (ret) {
737 dev_err(iodev->dev, "buck2 voltages not specified\n");
738 return -EINVAL;
739 }
740
741 return 0;
742}
743
Bill Pembertona5023572012-11-19 13:22:22 -0500744static int max8998_pmic_probe(struct platform_device *pdev)
Kyungmin Park156f2522010-06-16 09:04:16 +0200745{
746 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200747 struct max8998_platform_data *pdata = iodev->pdata;
Mark Brownc1727082012-04-04 00:50:22 +0100748 struct regulator_config config = { };
Axel Lin8a221df2014-03-09 15:32:19 +0800749 struct regulator_dev *rdev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200750 struct max8998_data *max8998;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200751 struct i2c_client *i2c;
Axel Lin8a221df2014-03-09 15:32:19 +0800752 int i, ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200753 unsigned int v;
Kyungmin Park156f2522010-06-16 09:04:16 +0200754
755 if (!pdata) {
756 dev_err(pdev->dev.parent, "No platform init data supplied\n");
757 return -ENODEV;
758 }
759
Tomasz Figaee999fb2013-06-25 16:08:10 +0200760 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
761 ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
762 if (ret)
763 return ret;
764 }
765
Axel Lin0f80ea12012-04-19 09:47:37 +0800766 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
767 GFP_KERNEL);
Kyungmin Park156f2522010-06-16 09:04:16 +0200768 if (!max8998)
769 return -ENOMEM;
770
Axel Lin747cc852010-08-27 16:37:34 +0800771 max8998->dev = &pdev->dev;
Kyungmin Park156f2522010-06-16 09:04:16 +0200772 max8998->iodev = iodev;
Axel Linc356cbc2010-08-22 15:26:49 +0800773 max8998->num_regulators = pdata->num_regulators;
Kyungmin Park156f2522010-06-16 09:04:16 +0200774 platform_set_drvdata(pdev, max8998);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200775 i2c = max8998->iodev->i2c;
776
MyungJoo Ham735a3d92011-01-11 12:20:05 +0100777 max8998->buck1_idx = pdata->buck1_default_idx;
778 max8998->buck2_idx = pdata->buck2_default_idx;
779
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200780 /* NOTE: */
781 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
782 /* will be displayed */
783
784 /* Check if MAX8998 voltage selection GPIOs are defined */
785 if (gpio_is_valid(pdata->buck1_set1) &&
786 gpio_is_valid(pdata->buck1_set2)) {
787 /* Check if SET1 is not equal to 0 */
788 if (!pdata->buck1_set1) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300789 dev_err(&pdev->dev,
790 "MAX8998 SET1 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200791 WARN_ON(!pdata->buck1_set1);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530792 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200793 }
794 /* Check if SET2 is not equal to 0 */
795 if (!pdata->buck1_set2) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300796 dev_err(&pdev->dev,
797 "MAX8998 SET2 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200798 WARN_ON(!pdata->buck1_set2);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530799 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200800 }
801
802 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
803 gpio_direction_output(pdata->buck1_set1,
804 max8998->buck1_idx & 0x1);
805
806
807 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
808 gpio_direction_output(pdata->buck1_set2,
809 (max8998->buck1_idx >> 1) & 0x1);
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200810
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200811 /* Set predefined values for BUCK1 registers */
812 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
813 i = 0;
814 while (buck12_voltage_map_desc.min +
815 buck12_voltage_map_desc.step*i
816 < pdata->buck1_voltage[v])
817 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200818
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200819 max8998->buck1_vol[v] = i;
820 ret = max8998_write_reg(i2c,
821 MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
822 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530823 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200824 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200825 }
826
827 if (gpio_is_valid(pdata->buck2_set3)) {
828 /* Check if SET3 is not equal to 0 */
829 if (!pdata->buck2_set3) {
Thiago Farina9df19a52013-02-23 00:52:35 -0300830 dev_err(&pdev->dev,
831 "MAX8998 SET3 GPIO defined as 0 !\n");
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200832 WARN_ON(!pdata->buck2_set3);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530833 return -EIO;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200834 }
835 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
836 gpio_direction_output(pdata->buck2_set3,
837 max8998->buck2_idx & 0x1);
838
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200839 /* Set predefined values for BUCK2 registers */
840 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
841 i = 0;
842 while (buck12_voltage_map_desc.min +
843 buck12_voltage_map_desc.step*i
844 < pdata->buck2_voltage[v])
845 i++;
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200846
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200847 max8998->buck2_vol[v] = i;
848 ret = max8998_write_reg(i2c,
849 MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
850 if (ret)
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530851 return ret;
Tomasz Figa4280e0b42013-06-24 14:39:53 +0200852 }
Lukasz Majewski50f19a42010-09-27 14:32:27 +0200853 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200854
855 for (i = 0; i < pdata->num_regulators; i++) {
856 const struct voltage_map_desc *desc;
857 int id = pdata->regulators[i].id;
858 int index = id - MAX8998_LDO2;
859
860 desc = ldo_voltage_map[id];
861 if (desc && regulators[index].ops != &max8998_others_ops) {
862 int count = (desc->max - desc->min) / desc->step + 1;
Axel Lin9a0fbb62012-06-17 09:33:07 +0800863
Kyungmin Park156f2522010-06-16 09:04:16 +0200864 regulators[index].n_voltages = count;
Axel Linadf61782012-12-28 17:10:20 +0800865 regulators[index].min_uV = desc->min;
866 regulators[index].uV_step = desc->step;
Kyungmin Park156f2522010-06-16 09:04:16 +0200867 }
Mark Brownc1727082012-04-04 00:50:22 +0100868
869 config.dev = max8998->dev;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200870 config.of_node = pdata->regulators[i].reg_node;
Mark Brownc1727082012-04-04 00:50:22 +0100871 config.init_data = pdata->regulators[i].initdata;
872 config.driver_data = max8998;
873
Axel Lin8a221df2014-03-09 15:32:19 +0800874 rdev = devm_regulator_register(&pdev->dev, &regulators[index],
875 &config);
876 if (IS_ERR(rdev)) {
877 ret = PTR_ERR(rdev);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200878 dev_err(max8998->dev, "regulator %s init failed (%d)\n",
879 regulators[index].name, ret);
Sachin Kamat8d491bf2013-09-04 11:08:02 +0530880 return ret;
Kyungmin Park156f2522010-06-16 09:04:16 +0200881 }
882 }
883
884
885 return 0;
Kyungmin Park156f2522010-06-16 09:04:16 +0200886}
887
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900888static const struct platform_device_id max8998_pmic_id[] = {
889 { "max8998-pmic", TYPE_MAX8998 },
890 { "lp3974-pmic", TYPE_LP3974 },
891 { }
892};
Axel Lina51b9072011-03-26 23:28:42 +0800893MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900894
Kyungmin Park156f2522010-06-16 09:04:16 +0200895static struct platform_driver max8998_pmic_driver = {
896 .driver = {
897 .name = "max8998-pmic",
Kyungmin Park156f2522010-06-16 09:04:16 +0200898 },
899 .probe = max8998_pmic_probe,
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900900 .id_table = max8998_pmic_id,
Kyungmin Park156f2522010-06-16 09:04:16 +0200901};
902
903static int __init max8998_pmic_init(void)
904{
905 return platform_driver_register(&max8998_pmic_driver);
906}
907subsys_initcall(max8998_pmic_init);
908
909static void __exit max8998_pmic_cleanup(void)
910{
911 platform_driver_unregister(&max8998_pmic_driver);
912}
913module_exit(max8998_pmic_cleanup);
914
915MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
916MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
917MODULE_LICENSE("GPL");