xref: /openbmc/linux/drivers/regulator/max8998.c (revision 8fdf9062)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max8998.c - Voltage regulator driver for the Maxim 8998
4 //
5 //  Copyright (C) 2009-2010 Samsung Electronics
6 //  Kyungmin Park <kyungmin.park@samsung.com>
7 //  Marek Szyprowski <m.szyprowski@samsung.com>
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/i2c.h>
12 #include <linux/err.h>
13 #include <linux/gpio.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/of_regulator.h>
22 #include <linux/mfd/max8998.h>
23 #include <linux/mfd/max8998-private.h>
24 
25 struct max8998_data {
26 	struct device		*dev;
27 	struct max8998_dev	*iodev;
28 	int			num_regulators;
29 	u8                      buck1_vol[4]; /* voltages for selection */
30 	u8                      buck2_vol[2];
31 	unsigned int		buck1_idx; /* index to last changed voltage */
32 					   /* value in a set */
33 	unsigned int		buck2_idx;
34 };
35 
36 struct voltage_map_desc {
37 	int min;
38 	int max;
39 	int step;
40 };
41 
42 /* Voltage maps in uV*/
43 static const struct voltage_map_desc ldo23_voltage_map_desc = {
44 	.min = 800000,	.step = 50000,	.max = 1300000,
45 };
46 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
47 	.min = 1600000,	.step = 100000,	.max = 3600000,
48 };
49 static const struct voltage_map_desc ldo8_voltage_map_desc = {
50 	.min = 3000000,	.step = 100000,	.max = 3600000,
51 };
52 static const struct voltage_map_desc ldo9_voltage_map_desc = {
53 	.min = 2800000,	.step = 100000,	.max = 3100000,
54 };
55 static const struct voltage_map_desc ldo10_voltage_map_desc = {
56 	.min = 950000,	.step = 50000,	.max = 1300000,
57 };
58 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
59 	.min = 800000,	.step = 100000,	.max = 3300000,
60 };
61 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
62 	.min = 1200000,	.step = 100000,	.max = 3300000,
63 };
64 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
65 	.min = 1600000,	.step = 100000,	.max = 3600000,
66 };
67 static const struct voltage_map_desc buck12_voltage_map_desc = {
68 	.min = 750000,	.step = 25000,	.max = 1525000,
69 };
70 static const struct voltage_map_desc buck3_voltage_map_desc = {
71 	.min = 1600000,	.step = 100000,	.max = 3600000,
72 };
73 static const struct voltage_map_desc buck4_voltage_map_desc = {
74 	.min = 800000,	.step = 100000,	.max = 2300000,
75 };
76 
77 static const struct voltage_map_desc *ldo_voltage_map[] = {
78 	NULL,
79 	NULL,
80 	&ldo23_voltage_map_desc,	/* LDO2 */
81 	&ldo23_voltage_map_desc,	/* LDO3 */
82 	&ldo456711_voltage_map_desc,	/* LDO4 */
83 	&ldo456711_voltage_map_desc,	/* LDO5 */
84 	&ldo456711_voltage_map_desc,	/* LDO6 */
85 	&ldo456711_voltage_map_desc,	/* LDO7 */
86 	&ldo8_voltage_map_desc,		/* LDO8 */
87 	&ldo9_voltage_map_desc,		/* LDO9 */
88 	&ldo10_voltage_map_desc,	/* LDO10 */
89 	&ldo456711_voltage_map_desc,	/* LDO11 */
90 	&ldo1213_voltage_map_desc,	/* LDO12 */
91 	&ldo1213_voltage_map_desc,	/* LDO13 */
92 	&ldo1415_voltage_map_desc,	/* LDO14 */
93 	&ldo1415_voltage_map_desc,	/* LDO15 */
94 	&ldo1617_voltage_map_desc,	/* LDO16 */
95 	&ldo1617_voltage_map_desc,	/* LDO17 */
96 	&buck12_voltage_map_desc,	/* BUCK1 */
97 	&buck12_voltage_map_desc,	/* BUCK2 */
98 	&buck3_voltage_map_desc,	/* BUCK3 */
99 	&buck4_voltage_map_desc,	/* BUCK4 */
100 };
101 
102 static int max8998_get_enable_register(struct regulator_dev *rdev,
103 					int *reg, int *shift)
104 {
105 	int ldo = rdev_get_id(rdev);
106 
107 	switch (ldo) {
108 	case MAX8998_LDO2 ... MAX8998_LDO5:
109 		*reg = MAX8998_REG_ONOFF1;
110 		*shift = 3 - (ldo - MAX8998_LDO2);
111 		break;
112 	case MAX8998_LDO6 ... MAX8998_LDO13:
113 		*reg = MAX8998_REG_ONOFF2;
114 		*shift = 7 - (ldo - MAX8998_LDO6);
115 		break;
116 	case MAX8998_LDO14 ... MAX8998_LDO17:
117 		*reg = MAX8998_REG_ONOFF3;
118 		*shift = 7 - (ldo - MAX8998_LDO14);
119 		break;
120 	case MAX8998_BUCK1 ... MAX8998_BUCK4:
121 		*reg = MAX8998_REG_ONOFF1;
122 		*shift = 7 - (ldo - MAX8998_BUCK1);
123 		break;
124 	case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
125 		*reg = MAX8998_REG_ONOFF4;
126 		*shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
127 		break;
128 	case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
129 		*reg = MAX8998_REG_CHGR2;
130 		*shift = 7 - (ldo - MAX8998_ESAFEOUT1);
131 		break;
132 	default:
133 		return -EINVAL;
134 	}
135 
136 	return 0;
137 }
138 
139 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
140 {
141 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
142 	struct i2c_client *i2c = max8998->iodev->i2c;
143 	int ret, reg, shift = 8;
144 	u8 val;
145 
146 	ret = max8998_get_enable_register(rdev, &reg, &shift);
147 	if (ret)
148 		return ret;
149 
150 	ret = max8998_read_reg(i2c, reg, &val);
151 	if (ret)
152 		return ret;
153 
154 	return val & (1 << shift);
155 }
156 
157 static int max8998_ldo_enable(struct regulator_dev *rdev)
158 {
159 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
160 	struct i2c_client *i2c = max8998->iodev->i2c;
161 	int reg, shift = 8, ret;
162 
163 	ret = max8998_get_enable_register(rdev, &reg, &shift);
164 	if (ret)
165 		return ret;
166 
167 	return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
168 }
169 
170 static int max8998_ldo_disable(struct regulator_dev *rdev)
171 {
172 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
173 	struct i2c_client *i2c = max8998->iodev->i2c;
174 	int reg, shift = 8, ret;
175 
176 	ret = max8998_get_enable_register(rdev, &reg, &shift);
177 	if (ret)
178 		return ret;
179 
180 	return max8998_update_reg(i2c, reg, 0, 1<<shift);
181 }
182 
183 static int max8998_get_voltage_register(struct regulator_dev *rdev,
184 				int *_reg, int *_shift, int *_mask)
185 {
186 	int ldo = rdev_get_id(rdev);
187 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
188 	int reg, shift = 0, mask = 0xff;
189 
190 	switch (ldo) {
191 	case MAX8998_LDO2 ... MAX8998_LDO3:
192 		reg = MAX8998_REG_LDO2_LDO3;
193 		mask = 0xf;
194 		if (ldo == MAX8998_LDO2)
195 			shift = 4;
196 		else
197 			shift = 0;
198 		break;
199 	case MAX8998_LDO4 ... MAX8998_LDO7:
200 		reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
201 		break;
202 	case MAX8998_LDO8 ... MAX8998_LDO9:
203 		reg = MAX8998_REG_LDO8_LDO9;
204 		mask = 0xf;
205 		if (ldo == MAX8998_LDO8)
206 			shift = 4;
207 		else
208 			shift = 0;
209 		break;
210 	case MAX8998_LDO10 ... MAX8998_LDO11:
211 		reg = MAX8998_REG_LDO10_LDO11;
212 		if (ldo == MAX8998_LDO10) {
213 			shift = 5;
214 			mask = 0x7;
215 		} else {
216 			shift = 0;
217 			mask = 0x1f;
218 		}
219 		break;
220 	case MAX8998_LDO12 ... MAX8998_LDO17:
221 		reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
222 		break;
223 	case MAX8998_BUCK1:
224 		reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
225 		break;
226 	case MAX8998_BUCK2:
227 		reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
228 		break;
229 	case MAX8998_BUCK3:
230 		reg = MAX8998_REG_BUCK3;
231 		break;
232 	case MAX8998_BUCK4:
233 		reg = MAX8998_REG_BUCK4;
234 		break;
235 	default:
236 		return -EINVAL;
237 	}
238 
239 	*_reg = reg;
240 	*_shift = shift;
241 	*_mask = mask;
242 
243 	return 0;
244 }
245 
246 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
247 {
248 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
249 	struct i2c_client *i2c = max8998->iodev->i2c;
250 	int reg, shift = 0, mask, ret;
251 	u8 val;
252 
253 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
254 	if (ret)
255 		return ret;
256 
257 	ret = max8998_read_reg(i2c, reg, &val);
258 	if (ret)
259 		return ret;
260 
261 	val >>= shift;
262 	val &= mask;
263 
264 	return val;
265 }
266 
267 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
268 				       unsigned selector)
269 {
270 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
271 	struct i2c_client *i2c = max8998->iodev->i2c;
272 	int reg, shift = 0, mask, ret;
273 
274 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
275 	if (ret)
276 		return ret;
277 
278 	ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
279 
280 	return ret;
281 }
282 
283 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
284 {
285 	gpio_set_value(gpio1, v & 0x1);
286 	gpio_set_value(gpio2, (v >> 1) & 0x1);
287 }
288 
289 static inline void buck2_gpio_set(int gpio, int v)
290 {
291 	gpio_set_value(gpio, v & 0x1);
292 }
293 
294 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
295 					unsigned selector)
296 {
297 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
298 	struct max8998_platform_data *pdata = max8998->iodev->pdata;
299 	struct i2c_client *i2c = max8998->iodev->i2c;
300 	int buck = rdev_get_id(rdev);
301 	int reg, shift = 0, mask, ret, j;
302 	static u8 buck1_last_val;
303 
304 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
305 	if (ret)
306 		return ret;
307 
308 	switch (buck) {
309 	case MAX8998_BUCK1:
310 		dev_dbg(max8998->dev,
311 			"BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
312 			"buck1_vol3:%d, buck1_vol4:%d\n",
313 			selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
314 			max8998->buck1_vol[2], max8998->buck1_vol[3]);
315 
316 		if (gpio_is_valid(pdata->buck1_set1) &&
317 		    gpio_is_valid(pdata->buck1_set2)) {
318 
319 			/* check if requested voltage */
320 			/* value is already defined */
321 			for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
322 				if (max8998->buck1_vol[j] == selector) {
323 					max8998->buck1_idx = j;
324 					buck1_gpio_set(pdata->buck1_set1,
325 						       pdata->buck1_set2, j);
326 					goto buck1_exit;
327 				}
328 			}
329 
330 			if (pdata->buck_voltage_lock)
331 				return -EINVAL;
332 
333 			/* no predefine regulator found */
334 			max8998->buck1_idx = (buck1_last_val % 2) + 2;
335 			dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
336 				max8998->buck1_idx);
337 			max8998->buck1_vol[max8998->buck1_idx] = selector;
338 			ret = max8998_get_voltage_register(rdev, &reg,
339 							   &shift,
340 							   &mask);
341 			ret = max8998_write_reg(i2c, reg, selector);
342 			buck1_gpio_set(pdata->buck1_set1,
343 				       pdata->buck1_set2, max8998->buck1_idx);
344 			buck1_last_val++;
345 buck1_exit:
346 			dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
347 				i2c->name, gpio_get_value(pdata->buck1_set1),
348 				gpio_get_value(pdata->buck1_set2));
349 			break;
350 		} else {
351 			ret = max8998_write_reg(i2c, reg, selector);
352 		}
353 		break;
354 
355 	case MAX8998_BUCK2:
356 		dev_dbg(max8998->dev,
357 			"BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
358 			selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
359 		if (gpio_is_valid(pdata->buck2_set3)) {
360 
361 			/* check if requested voltage */
362 			/* value is already defined */
363 			for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
364 				if (max8998->buck2_vol[j] == selector) {
365 					max8998->buck2_idx = j;
366 					buck2_gpio_set(pdata->buck2_set3, j);
367 					goto buck2_exit;
368 				}
369 			}
370 
371 			if (pdata->buck_voltage_lock)
372 				return -EINVAL;
373 
374 			max8998_get_voltage_register(rdev,
375 					&reg, &shift, &mask);
376 			ret = max8998_write_reg(i2c, reg, selector);
377 			max8998->buck2_vol[max8998->buck2_idx] = selector;
378 			buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
379 buck2_exit:
380 			dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
381 				gpio_get_value(pdata->buck2_set3));
382 		} else {
383 			ret = max8998_write_reg(i2c, reg, selector);
384 		}
385 		break;
386 
387 	case MAX8998_BUCK3:
388 	case MAX8998_BUCK4:
389 		ret = max8998_update_reg(i2c, reg, selector<<shift,
390 					 mask<<shift);
391 		break;
392 	}
393 
394 	return ret;
395 }
396 
397 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
398 					     unsigned int old_selector,
399 					     unsigned int new_selector)
400 {
401 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
402 	struct i2c_client *i2c = max8998->iodev->i2c;
403 	const struct voltage_map_desc *desc;
404 	int buck = rdev_get_id(rdev);
405 	u8 val = 0;
406 	int difference, ret;
407 
408 	if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
409 		return -EINVAL;
410 
411 	desc = ldo_voltage_map[buck];
412 
413 	/* Voltage stabilization */
414 	ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
415 	if (ret)
416 		return ret;
417 
418 	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
419 	/* MAX8998 has ENRAMP bit implemented, so test it*/
420 	if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
421 		return 0;
422 
423 	difference = (new_selector - old_selector) * desc->step / 1000;
424 	if (difference > 0)
425 		return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
426 
427 	return 0;
428 }
429 
430 static struct regulator_ops max8998_ldo_ops = {
431 	.list_voltage		= regulator_list_voltage_linear,
432 	.map_voltage		= regulator_map_voltage_linear,
433 	.is_enabled		= max8998_ldo_is_enabled,
434 	.enable			= max8998_ldo_enable,
435 	.disable		= max8998_ldo_disable,
436 	.get_voltage_sel	= max8998_get_voltage_sel,
437 	.set_voltage_sel	= max8998_set_voltage_ldo_sel,
438 };
439 
440 static struct regulator_ops max8998_buck_ops = {
441 	.list_voltage		= regulator_list_voltage_linear,
442 	.map_voltage		= regulator_map_voltage_linear,
443 	.is_enabled		= max8998_ldo_is_enabled,
444 	.enable			= max8998_ldo_enable,
445 	.disable		= max8998_ldo_disable,
446 	.get_voltage_sel	= max8998_get_voltage_sel,
447 	.set_voltage_sel	= max8998_set_voltage_buck_sel,
448 	.set_voltage_time_sel	= max8998_set_voltage_buck_time_sel,
449 };
450 
451 static struct regulator_ops max8998_others_ops = {
452 	.is_enabled		= max8998_ldo_is_enabled,
453 	.enable			= max8998_ldo_enable,
454 	.disable		= max8998_ldo_disable,
455 };
456 
457 static struct regulator_desc regulators[] = {
458 	{
459 		.name		= "LDO2",
460 		.id		= MAX8998_LDO2,
461 		.ops		= &max8998_ldo_ops,
462 		.type		= REGULATOR_VOLTAGE,
463 		.owner		= THIS_MODULE,
464 	}, {
465 		.name		= "LDO3",
466 		.id		= MAX8998_LDO3,
467 		.ops		= &max8998_ldo_ops,
468 		.type		= REGULATOR_VOLTAGE,
469 		.owner		= THIS_MODULE,
470 	}, {
471 		.name		= "LDO4",
472 		.id		= MAX8998_LDO4,
473 		.ops		= &max8998_ldo_ops,
474 		.type		= REGULATOR_VOLTAGE,
475 		.owner		= THIS_MODULE,
476 	}, {
477 		.name		= "LDO5",
478 		.id		= MAX8998_LDO5,
479 		.ops		= &max8998_ldo_ops,
480 		.type		= REGULATOR_VOLTAGE,
481 		.owner		= THIS_MODULE,
482 	}, {
483 		.name		= "LDO6",
484 		.id		= MAX8998_LDO6,
485 		.ops		= &max8998_ldo_ops,
486 		.type		= REGULATOR_VOLTAGE,
487 		.owner		= THIS_MODULE,
488 	}, {
489 		.name		= "LDO7",
490 		.id		= MAX8998_LDO7,
491 		.ops		= &max8998_ldo_ops,
492 		.type		= REGULATOR_VOLTAGE,
493 		.owner		= THIS_MODULE,
494 	}, {
495 		.name		= "LDO8",
496 		.id		= MAX8998_LDO8,
497 		.ops		= &max8998_ldo_ops,
498 		.type		= REGULATOR_VOLTAGE,
499 		.owner		= THIS_MODULE,
500 	}, {
501 		.name		= "LDO9",
502 		.id		= MAX8998_LDO9,
503 		.ops		= &max8998_ldo_ops,
504 		.type		= REGULATOR_VOLTAGE,
505 		.owner		= THIS_MODULE,
506 	}, {
507 		.name		= "LDO10",
508 		.id		= MAX8998_LDO10,
509 		.ops		= &max8998_ldo_ops,
510 		.type		= REGULATOR_VOLTAGE,
511 		.owner		= THIS_MODULE,
512 	}, {
513 		.name		= "LDO11",
514 		.id		= MAX8998_LDO11,
515 		.ops		= &max8998_ldo_ops,
516 		.type		= REGULATOR_VOLTAGE,
517 		.owner		= THIS_MODULE,
518 	}, {
519 		.name		= "LDO12",
520 		.id		= MAX8998_LDO12,
521 		.ops		= &max8998_ldo_ops,
522 		.type		= REGULATOR_VOLTAGE,
523 		.owner		= THIS_MODULE,
524 	}, {
525 		.name		= "LDO13",
526 		.id		= MAX8998_LDO13,
527 		.ops		= &max8998_ldo_ops,
528 		.type		= REGULATOR_VOLTAGE,
529 		.owner		= THIS_MODULE,
530 	}, {
531 		.name		= "LDO14",
532 		.id		= MAX8998_LDO14,
533 		.ops		= &max8998_ldo_ops,
534 		.type		= REGULATOR_VOLTAGE,
535 		.owner		= THIS_MODULE,
536 	}, {
537 		.name		= "LDO15",
538 		.id		= MAX8998_LDO15,
539 		.ops		= &max8998_ldo_ops,
540 		.type		= REGULATOR_VOLTAGE,
541 		.owner		= THIS_MODULE,
542 	}, {
543 		.name		= "LDO16",
544 		.id		= MAX8998_LDO16,
545 		.ops		= &max8998_ldo_ops,
546 		.type		= REGULATOR_VOLTAGE,
547 		.owner		= THIS_MODULE,
548 	}, {
549 		.name		= "LDO17",
550 		.id		= MAX8998_LDO17,
551 		.ops		= &max8998_ldo_ops,
552 		.type		= REGULATOR_VOLTAGE,
553 		.owner		= THIS_MODULE,
554 	}, {
555 		.name		= "BUCK1",
556 		.id		= MAX8998_BUCK1,
557 		.ops		= &max8998_buck_ops,
558 		.type		= REGULATOR_VOLTAGE,
559 		.owner		= THIS_MODULE,
560 	}, {
561 		.name		= "BUCK2",
562 		.id		= MAX8998_BUCK2,
563 		.ops		= &max8998_buck_ops,
564 		.type		= REGULATOR_VOLTAGE,
565 		.owner		= THIS_MODULE,
566 	}, {
567 		.name		= "BUCK3",
568 		.id		= MAX8998_BUCK3,
569 		.ops		= &max8998_buck_ops,
570 		.type		= REGULATOR_VOLTAGE,
571 		.owner		= THIS_MODULE,
572 	}, {
573 		.name		= "BUCK4",
574 		.id		= MAX8998_BUCK4,
575 		.ops		= &max8998_buck_ops,
576 		.type		= REGULATOR_VOLTAGE,
577 		.owner		= THIS_MODULE,
578 	}, {
579 		.name		= "EN32KHz-AP",
580 		.id		= MAX8998_EN32KHZ_AP,
581 		.ops		= &max8998_others_ops,
582 		.type		= REGULATOR_VOLTAGE,
583 		.owner		= THIS_MODULE,
584 	}, {
585 		.name		= "EN32KHz-CP",
586 		.id		= MAX8998_EN32KHZ_CP,
587 		.ops		= &max8998_others_ops,
588 		.type		= REGULATOR_VOLTAGE,
589 		.owner		= THIS_MODULE,
590 	}, {
591 		.name		= "ENVICHG",
592 		.id		= MAX8998_ENVICHG,
593 		.ops		= &max8998_others_ops,
594 		.type		= REGULATOR_VOLTAGE,
595 		.owner		= THIS_MODULE,
596 	}, {
597 		.name		= "ESAFEOUT1",
598 		.id		= MAX8998_ESAFEOUT1,
599 		.ops		= &max8998_others_ops,
600 		.type		= REGULATOR_VOLTAGE,
601 		.owner		= THIS_MODULE,
602 	}, {
603 		.name		= "ESAFEOUT2",
604 		.id		= MAX8998_ESAFEOUT2,
605 		.ops		= &max8998_others_ops,
606 		.type		= REGULATOR_VOLTAGE,
607 		.owner		= THIS_MODULE,
608 	}
609 };
610 
611 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
612 			struct max8998_platform_data *pdata,
613 			struct device_node *pmic_np)
614 {
615 	int gpio;
616 
617 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
618 	if (!gpio_is_valid(gpio)) {
619 		dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
620 		return -EINVAL;
621 	}
622 	pdata->buck1_set1 = gpio;
623 
624 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
625 	if (!gpio_is_valid(gpio)) {
626 		dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
627 		return -EINVAL;
628 	}
629 	pdata->buck1_set2 = gpio;
630 
631 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
632 	if (!gpio_is_valid(gpio)) {
633 		dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
634 		return -EINVAL;
635 	}
636 	pdata->buck2_set3 = gpio;
637 
638 	return 0;
639 }
640 
641 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
642 					struct max8998_platform_data *pdata)
643 {
644 	struct device_node *pmic_np = iodev->dev->of_node;
645 	struct device_node *regulators_np, *reg_np;
646 	struct max8998_regulator_data *rdata;
647 	unsigned int i;
648 	int ret;
649 
650 	regulators_np = of_get_child_by_name(pmic_np, "regulators");
651 	if (!regulators_np) {
652 		dev_err(iodev->dev, "could not find regulators sub-node\n");
653 		return -EINVAL;
654 	}
655 
656 	/* count the number of regulators to be supported in pmic */
657 	pdata->num_regulators = of_get_child_count(regulators_np);
658 
659 	rdata = devm_kcalloc(iodev->dev,
660 			     pdata->num_regulators, sizeof(*rdata),
661 			     GFP_KERNEL);
662 	if (!rdata) {
663 		of_node_put(regulators_np);
664 		return -ENOMEM;
665 	}
666 
667 	pdata->regulators = rdata;
668 	for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
669 		reg_np = of_get_child_by_name(regulators_np,
670 							regulators[i].name);
671 		if (!reg_np)
672 			continue;
673 
674 		rdata->id = regulators[i].id;
675 		rdata->initdata = of_get_regulator_init_data(iodev->dev,
676 							     reg_np,
677 							     &regulators[i]);
678 		rdata->reg_node = reg_np;
679 		++rdata;
680 	}
681 	pdata->num_regulators = rdata - pdata->regulators;
682 
683 	of_node_put(reg_np);
684 	of_node_put(regulators_np);
685 
686 	ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
687 	if (ret)
688 		return -EINVAL;
689 
690 	if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
691 		pdata->buck_voltage_lock = true;
692 
693 	ret = of_property_read_u32(pmic_np,
694 					"max8998,pmic-buck1-default-dvs-idx",
695 					&pdata->buck1_default_idx);
696 	if (!ret && pdata->buck1_default_idx >= 4) {
697 		pdata->buck1_default_idx = 0;
698 		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
699 	}
700 
701 	ret = of_property_read_u32(pmic_np,
702 					"max8998,pmic-buck2-default-dvs-idx",
703 					&pdata->buck2_default_idx);
704 	if (!ret && pdata->buck2_default_idx >= 2) {
705 		pdata->buck2_default_idx = 0;
706 		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
707 	}
708 
709 	ret = of_property_read_u32_array(pmic_np,
710 					"max8998,pmic-buck1-dvs-voltage",
711 					pdata->buck1_voltage,
712 					ARRAY_SIZE(pdata->buck1_voltage));
713 	if (ret) {
714 		dev_err(iodev->dev, "buck1 voltages not specified\n");
715 		return -EINVAL;
716 	}
717 
718 	ret = of_property_read_u32_array(pmic_np,
719 					"max8998,pmic-buck2-dvs-voltage",
720 					pdata->buck2_voltage,
721 					ARRAY_SIZE(pdata->buck2_voltage));
722 	if (ret) {
723 		dev_err(iodev->dev, "buck2 voltages not specified\n");
724 		return -EINVAL;
725 	}
726 
727 	return 0;
728 }
729 
730 static int max8998_pmic_probe(struct platform_device *pdev)
731 {
732 	struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
733 	struct max8998_platform_data *pdata = iodev->pdata;
734 	struct regulator_config config = { };
735 	struct regulator_dev *rdev;
736 	struct max8998_data *max8998;
737 	struct i2c_client *i2c;
738 	int i, ret;
739 	unsigned int v;
740 
741 	if (!pdata) {
742 		dev_err(pdev->dev.parent, "No platform init data supplied\n");
743 		return -ENODEV;
744 	}
745 
746 	if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
747 		ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
748 		if (ret)
749 			return ret;
750 	}
751 
752 	max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
753 			       GFP_KERNEL);
754 	if (!max8998)
755 		return -ENOMEM;
756 
757 	max8998->dev = &pdev->dev;
758 	max8998->iodev = iodev;
759 	max8998->num_regulators = pdata->num_regulators;
760 	platform_set_drvdata(pdev, max8998);
761 	i2c = max8998->iodev->i2c;
762 
763 	max8998->buck1_idx = pdata->buck1_default_idx;
764 	max8998->buck2_idx = pdata->buck2_default_idx;
765 
766 	/* NOTE: */
767 	/* For unused GPIO NOT marked as -1 (thereof equal to 0)  WARN_ON */
768 	/* will be displayed */
769 
770 	/* Check if MAX8998 voltage selection GPIOs are defined */
771 	if (gpio_is_valid(pdata->buck1_set1) &&
772 	    gpio_is_valid(pdata->buck1_set2)) {
773 		/* Check if SET1 is not equal to 0 */
774 		if (!pdata->buck1_set1) {
775 			dev_err(&pdev->dev,
776 				"MAX8998 SET1 GPIO defined as 0 !\n");
777 			WARN_ON(!pdata->buck1_set1);
778 			return -EIO;
779 		}
780 		/* Check if SET2 is not equal to 0 */
781 		if (!pdata->buck1_set2) {
782 			dev_err(&pdev->dev,
783 				"MAX8998 SET2 GPIO defined as 0 !\n");
784 			WARN_ON(!pdata->buck1_set2);
785 			return -EIO;
786 		}
787 
788 		gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
789 		gpio_direction_output(pdata->buck1_set1,
790 				      max8998->buck1_idx & 0x1);
791 
792 
793 		gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
794 		gpio_direction_output(pdata->buck1_set2,
795 				      (max8998->buck1_idx >> 1) & 0x1);
796 
797 		/* Set predefined values for BUCK1 registers */
798 		for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
799 			i = 0;
800 			while (buck12_voltage_map_desc.min +
801 			       buck12_voltage_map_desc.step*i
802 			       < pdata->buck1_voltage[v])
803 				i++;
804 
805 			max8998->buck1_vol[v] = i;
806 			ret = max8998_write_reg(i2c,
807 					MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
808 			if (ret)
809 				return ret;
810 		}
811 	}
812 
813 	if (gpio_is_valid(pdata->buck2_set3)) {
814 		/* Check if SET3 is not equal to 0 */
815 		if (!pdata->buck2_set3) {
816 			dev_err(&pdev->dev,
817 				"MAX8998 SET3 GPIO defined as 0 !\n");
818 			WARN_ON(!pdata->buck2_set3);
819 			return -EIO;
820 		}
821 		gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
822 		gpio_direction_output(pdata->buck2_set3,
823 				      max8998->buck2_idx & 0x1);
824 
825 		/* Set predefined values for BUCK2 registers */
826 		for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
827 			i = 0;
828 			while (buck12_voltage_map_desc.min +
829 			       buck12_voltage_map_desc.step*i
830 			       < pdata->buck2_voltage[v])
831 				i++;
832 
833 			max8998->buck2_vol[v] = i;
834 			ret = max8998_write_reg(i2c,
835 					MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
836 			if (ret)
837 				return ret;
838 		}
839 	}
840 
841 	for (i = 0; i < pdata->num_regulators; i++) {
842 		const struct voltage_map_desc *desc;
843 		int id = pdata->regulators[i].id;
844 		int index = id - MAX8998_LDO2;
845 
846 		desc = ldo_voltage_map[id];
847 		if (desc && regulators[index].ops != &max8998_others_ops) {
848 			int count = (desc->max - desc->min) / desc->step + 1;
849 
850 			regulators[index].n_voltages = count;
851 			regulators[index].min_uV = desc->min;
852 			regulators[index].uV_step = desc->step;
853 		}
854 
855 		config.dev = max8998->dev;
856 		config.of_node = pdata->regulators[i].reg_node;
857 		config.init_data = pdata->regulators[i].initdata;
858 		config.driver_data = max8998;
859 
860 		rdev = devm_regulator_register(&pdev->dev, &regulators[index],
861 					       &config);
862 		if (IS_ERR(rdev)) {
863 			ret = PTR_ERR(rdev);
864 			dev_err(max8998->dev, "regulator %s init failed (%d)\n",
865 						regulators[index].name, ret);
866 			return ret;
867 		}
868 	}
869 
870 
871 	return 0;
872 }
873 
874 static const struct platform_device_id max8998_pmic_id[] = {
875 	{ "max8998-pmic", TYPE_MAX8998 },
876 	{ "lp3974-pmic", TYPE_LP3974 },
877 	{ }
878 };
879 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
880 
881 static struct platform_driver max8998_pmic_driver = {
882 	.driver = {
883 		.name = "max8998-pmic",
884 	},
885 	.probe = max8998_pmic_probe,
886 	.id_table = max8998_pmic_id,
887 };
888 
889 static int __init max8998_pmic_init(void)
890 {
891 	return platform_driver_register(&max8998_pmic_driver);
892 }
893 subsys_initcall(max8998_pmic_init);
894 
895 static void __exit max8998_pmic_cleanup(void)
896 {
897 	platform_driver_unregister(&max8998_pmic_driver);
898 }
899 module_exit(max8998_pmic_cleanup);
900 
901 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
902 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
903 MODULE_LICENSE("GPL");
904