xref: /linux/drivers/regulator/max8998.c (revision 827634added7f38b7d724cab1dccdb2b004c13c3)
1 /*
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/of.h>
32 #include <linux/of_gpio.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/of_regulator.h>
36 #include <linux/mfd/max8998.h>
37 #include <linux/mfd/max8998-private.h>
38 
39 struct max8998_data {
40 	struct device		*dev;
41 	struct max8998_dev	*iodev;
42 	int			num_regulators;
43 	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;
48 };
49 
50 struct voltage_map_desc {
51 	int min;
52 	int max;
53 	int step;
54 };
55 
56 /* Voltage maps in uV*/
57 static const struct voltage_map_desc ldo23_voltage_map_desc = {
58 	.min = 800000,	.step = 50000,	.max = 1300000,
59 };
60 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
61 	.min = 1600000,	.step = 100000,	.max = 3600000,
62 };
63 static const struct voltage_map_desc ldo8_voltage_map_desc = {
64 	.min = 3000000,	.step = 100000,	.max = 3600000,
65 };
66 static const struct voltage_map_desc ldo9_voltage_map_desc = {
67 	.min = 2800000,	.step = 100000,	.max = 3100000,
68 };
69 static const struct voltage_map_desc ldo10_voltage_map_desc = {
70 	.min = 950000,	.step = 50000,	.max = 1300000,
71 };
72 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
73 	.min = 800000,	.step = 100000,	.max = 3300000,
74 };
75 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
76 	.min = 1200000,	.step = 100000,	.max = 3300000,
77 };
78 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
79 	.min = 1600000,	.step = 100000,	.max = 3600000,
80 };
81 static const struct voltage_map_desc buck12_voltage_map_desc = {
82 	.min = 750000,	.step = 25000,	.max = 1525000,
83 };
84 static const struct voltage_map_desc buck3_voltage_map_desc = {
85 	.min = 1600000,	.step = 100000,	.max = 3600000,
86 };
87 static const struct voltage_map_desc buck4_voltage_map_desc = {
88 	.min = 800000,	.step = 100000,	.max = 2300000,
89 };
90 
91 static 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 
116 static int max8998_get_enable_register(struct regulator_dev *rdev,
117 					int *reg, int *shift)
118 {
119 	int ldo = rdev_get_id(rdev);
120 
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 
153 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
154 {
155 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
156 	struct i2c_client *i2c = max8998->iodev->i2c;
157 	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 
164 	ret = max8998_read_reg(i2c, reg, &val);
165 	if (ret)
166 		return ret;
167 
168 	return val & (1 << shift);
169 }
170 
171 static int max8998_ldo_enable(struct regulator_dev *rdev)
172 {
173 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
174 	struct i2c_client *i2c = max8998->iodev->i2c;
175 	int reg, shift = 8, ret;
176 
177 	ret = max8998_get_enable_register(rdev, &reg, &shift);
178 	if (ret)
179 		return ret;
180 
181 	return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
182 }
183 
184 static int max8998_ldo_disable(struct regulator_dev *rdev)
185 {
186 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
187 	struct i2c_client *i2c = max8998->iodev->i2c;
188 	int reg, shift = 8, ret;
189 
190 	ret = max8998_get_enable_register(rdev, &reg, &shift);
191 	if (ret)
192 		return ret;
193 
194 	return max8998_update_reg(i2c, reg, 0, 1<<shift);
195 }
196 
197 static int max8998_get_voltage_register(struct regulator_dev *rdev,
198 				int *_reg, int *_shift, int *_mask)
199 {
200 	int ldo = rdev_get_id(rdev);
201 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
202 	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:
238 		reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
239 		break;
240 	case MAX8998_BUCK2:
241 		reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
242 		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 
260 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
261 {
262 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
263 	struct i2c_client *i2c = max8998->iodev->i2c;
264 	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 
271 	ret = max8998_read_reg(i2c, reg, &val);
272 	if (ret)
273 		return ret;
274 
275 	val >>= shift;
276 	val &= mask;
277 
278 	return val;
279 }
280 
281 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
282 				       unsigned selector)
283 {
284 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
285 	struct i2c_client *i2c = max8998->iodev->i2c;
286 	int reg, shift = 0, mask, ret;
287 
288 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
289 	if (ret)
290 		return ret;
291 
292 	ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
293 
294 	return ret;
295 }
296 
297 static 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 
303 static inline void buck2_gpio_set(int gpio, int v)
304 {
305 	gpio_set_value(gpio, v & 0x1);
306 }
307 
308 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
309 					unsigned selector)
310 {
311 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
312 	struct max8998_platform_data *pdata =
313 		dev_get_platdata(max8998->iodev->dev);
314 	struct i2c_client *i2c = max8998->iodev->i2c;
315 	int buck = rdev_get_id(rdev);
316 	int reg, shift = 0, mask, ret, j;
317 	static u8 buck1_last_val;
318 
319 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
320 	if (ret)
321 		return ret;
322 
323 	switch (buck) {
324 	case MAX8998_BUCK1:
325 		dev_dbg(max8998->dev,
326 			"BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
327 			"buck1_vol3:%d, buck1_vol4:%d\n",
328 			selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
329 			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++) {
337 				if (max8998->buck1_vol[j] == selector) {
338 					max8998->buck1_idx = j;
339 					buck1_gpio_set(pdata->buck1_set1,
340 						       pdata->buck1_set2, j);
341 					goto buck1_exit;
342 				}
343 			}
344 
345 			if (pdata->buck_voltage_lock)
346 				return -EINVAL;
347 
348 			/* 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);
352 			max8998->buck1_vol[max8998->buck1_idx] = selector;
353 			ret = max8998_get_voltage_register(rdev, &reg,
354 							   &shift,
355 							   &mask);
356 			ret = max8998_write_reg(i2c, reg, selector);
357 			buck1_gpio_set(pdata->buck1_set1,
358 				       pdata->buck1_set2, max8998->buck1_idx);
359 			buck1_last_val++;
360 buck1_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 {
366 			ret = max8998_write_reg(i2c, reg, selector);
367 		}
368 		break;
369 
370 	case MAX8998_BUCK2:
371 		dev_dbg(max8998->dev,
372 			"BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
373 			selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
374 		if (gpio_is_valid(pdata->buck2_set3)) {
375 
376 			/* check if requested voltage */
377 			/* value is already defined */
378 			for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
379 				if (max8998->buck2_vol[j] == selector) {
380 					max8998->buck2_idx = j;
381 					buck2_gpio_set(pdata->buck2_set3, j);
382 					goto buck2_exit;
383 				}
384 			}
385 
386 			if (pdata->buck_voltage_lock)
387 				return -EINVAL;
388 
389 			max8998_get_voltage_register(rdev,
390 					&reg, &shift, &mask);
391 			ret = max8998_write_reg(i2c, reg, selector);
392 			max8998->buck2_vol[max8998->buck2_idx] = selector;
393 			buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
394 buck2_exit:
395 			dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
396 				gpio_get_value(pdata->buck2_set3));
397 		} else {
398 			ret = max8998_write_reg(i2c, reg, selector);
399 		}
400 		break;
401 
402 	case MAX8998_BUCK3:
403 	case MAX8998_BUCK4:
404 		ret = max8998_update_reg(i2c, reg, selector<<shift,
405 					 mask<<shift);
406 		break;
407 	}
408 
409 	return ret;
410 }
411 
412 static 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 
428 	/* Voltage stabilization */
429 	ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
430 	if (ret)
431 		return ret;
432 
433 	/* 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))
436 		return 0;
437 
438 	difference = (new_selector - old_selector) * desc->step / 1000;
439 	if (difference > 0)
440 		return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
441 
442 	return 0;
443 }
444 
445 static struct regulator_ops max8998_ldo_ops = {
446 	.list_voltage		= regulator_list_voltage_linear,
447 	.map_voltage		= regulator_map_voltage_linear,
448 	.is_enabled		= max8998_ldo_is_enabled,
449 	.enable			= max8998_ldo_enable,
450 	.disable		= max8998_ldo_disable,
451 	.get_voltage_sel	= max8998_get_voltage_sel,
452 	.set_voltage_sel	= max8998_set_voltage_ldo_sel,
453 };
454 
455 static struct regulator_ops max8998_buck_ops = {
456 	.list_voltage		= regulator_list_voltage_linear,
457 	.map_voltage		= regulator_map_voltage_linear,
458 	.is_enabled		= max8998_ldo_is_enabled,
459 	.enable			= max8998_ldo_enable,
460 	.disable		= max8998_ldo_disable,
461 	.get_voltage_sel	= max8998_get_voltage_sel,
462 	.set_voltage_sel	= max8998_set_voltage_buck_sel,
463 	.set_voltage_time_sel	= max8998_set_voltage_buck_time_sel,
464 };
465 
466 static struct regulator_ops max8998_others_ops = {
467 	.is_enabled		= max8998_ldo_is_enabled,
468 	.enable			= max8998_ldo_enable,
469 	.disable		= max8998_ldo_disable,
470 };
471 
472 static 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 	}, {
594 		.name		= "EN32KHz-AP",
595 		.id		= MAX8998_EN32KHZ_AP,
596 		.ops		= &max8998_others_ops,
597 		.type		= REGULATOR_VOLTAGE,
598 		.owner		= THIS_MODULE,
599 	}, {
600 		.name		= "EN32KHz-CP",
601 		.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 
626 static 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 
656 static 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);
676 	if (!rdata) {
677 		of_node_put(regulators_np);
678 		return -ENOMEM;
679 	}
680 
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;
689 		rdata->initdata = of_get_regulator_init_data(iodev->dev,
690 							     reg_np,
691 							     &regulators[i]);
692 		rdata->reg_node = reg_np;
693 		++rdata;
694 	}
695 	pdata->num_regulators = rdata - pdata->regulators;
696 
697 	of_node_put(reg_np);
698 	of_node_put(regulators_np);
699 
700 	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 
744 static int max8998_pmic_probe(struct platform_device *pdev)
745 {
746 	struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
747 	struct max8998_platform_data *pdata = iodev->pdata;
748 	struct regulator_config config = { };
749 	struct regulator_dev *rdev;
750 	struct max8998_data *max8998;
751 	struct i2c_client *i2c;
752 	int i, ret;
753 	unsigned int v;
754 
755 	if (!pdata) {
756 		dev_err(pdev->dev.parent, "No platform init data supplied\n");
757 		return -ENODEV;
758 	}
759 
760 	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 
766 	max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
767 			       GFP_KERNEL);
768 	if (!max8998)
769 		return -ENOMEM;
770 
771 	max8998->dev = &pdev->dev;
772 	max8998->iodev = iodev;
773 	max8998->num_regulators = pdata->num_regulators;
774 	platform_set_drvdata(pdev, max8998);
775 	i2c = max8998->iodev->i2c;
776 
777 	max8998->buck1_idx = pdata->buck1_default_idx;
778 	max8998->buck2_idx = pdata->buck2_default_idx;
779 
780 	/* 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) {
789 			dev_err(&pdev->dev,
790 				"MAX8998 SET1 GPIO defined as 0 !\n");
791 			WARN_ON(!pdata->buck1_set1);
792 			return -EIO;
793 		}
794 		/* Check if SET2 is not equal to 0 */
795 		if (!pdata->buck1_set2) {
796 			dev_err(&pdev->dev,
797 				"MAX8998 SET2 GPIO defined as 0 !\n");
798 			WARN_ON(!pdata->buck1_set2);
799 			return -EIO;
800 		}
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);
810 
811 		/* 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++;
818 
819 			max8998->buck1_vol[v] = i;
820 			ret = max8998_write_reg(i2c,
821 					MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
822 			if (ret)
823 				return ret;
824 		}
825 	}
826 
827 	if (gpio_is_valid(pdata->buck2_set3)) {
828 		/* Check if SET3 is not equal to 0 */
829 		if (!pdata->buck2_set3) {
830 			dev_err(&pdev->dev,
831 				"MAX8998 SET3 GPIO defined as 0 !\n");
832 			WARN_ON(!pdata->buck2_set3);
833 			return -EIO;
834 		}
835 		gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
836 		gpio_direction_output(pdata->buck2_set3,
837 				      max8998->buck2_idx & 0x1);
838 
839 		/* 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++;
846 
847 			max8998->buck2_vol[v] = i;
848 			ret = max8998_write_reg(i2c,
849 					MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
850 			if (ret)
851 				return ret;
852 		}
853 	}
854 
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;
863 
864 			regulators[index].n_voltages = count;
865 			regulators[index].min_uV = desc->min;
866 			regulators[index].uV_step = desc->step;
867 		}
868 
869 		config.dev = max8998->dev;
870 		config.of_node = pdata->regulators[i].reg_node;
871 		config.init_data = pdata->regulators[i].initdata;
872 		config.driver_data = max8998;
873 
874 		rdev = devm_regulator_register(&pdev->dev, &regulators[index],
875 					       &config);
876 		if (IS_ERR(rdev)) {
877 			ret = PTR_ERR(rdev);
878 			dev_err(max8998->dev, "regulator %s init failed (%d)\n",
879 						regulators[index].name, ret);
880 			return ret;
881 		}
882 	}
883 
884 
885 	return 0;
886 }
887 
888 static const struct platform_device_id max8998_pmic_id[] = {
889 	{ "max8998-pmic", TYPE_MAX8998 },
890 	{ "lp3974-pmic", TYPE_LP3974 },
891 	{ }
892 };
893 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
894 
895 static struct platform_driver max8998_pmic_driver = {
896 	.driver = {
897 		.name = "max8998-pmic",
898 	},
899 	.probe = max8998_pmic_probe,
900 	.id_table = max8998_pmic_id,
901 };
902 
903 static int __init max8998_pmic_init(void)
904 {
905 	return platform_driver_register(&max8998_pmic_driver);
906 }
907 subsys_initcall(max8998_pmic_init);
908 
909 static void __exit max8998_pmic_cleanup(void)
910 {
911 	platform_driver_unregister(&max8998_pmic_driver);
912 }
913 module_exit(max8998_pmic_cleanup);
914 
915 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
916 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
917 MODULE_LICENSE("GPL");
918