xref: /openbmc/linux/drivers/regulator/max8998.c (revision 9a29ad52)
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 = max8998->iodev->pdata;
313 	struct i2c_client *i2c = max8998->iodev->i2c;
314 	int buck = rdev_get_id(rdev);
315 	int reg, shift = 0, mask, ret, j;
316 	static u8 buck1_last_val;
317 
318 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
319 	if (ret)
320 		return ret;
321 
322 	switch (buck) {
323 	case MAX8998_BUCK1:
324 		dev_dbg(max8998->dev,
325 			"BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
326 			"buck1_vol3:%d, buck1_vol4:%d\n",
327 			selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
328 			max8998->buck1_vol[2], max8998->buck1_vol[3]);
329 
330 		if (gpio_is_valid(pdata->buck1_set1) &&
331 		    gpio_is_valid(pdata->buck1_set2)) {
332 
333 			/* check if requested voltage */
334 			/* value is already defined */
335 			for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
336 				if (max8998->buck1_vol[j] == selector) {
337 					max8998->buck1_idx = j;
338 					buck1_gpio_set(pdata->buck1_set1,
339 						       pdata->buck1_set2, j);
340 					goto buck1_exit;
341 				}
342 			}
343 
344 			if (pdata->buck_voltage_lock)
345 				return -EINVAL;
346 
347 			/* no predefine regulator found */
348 			max8998->buck1_idx = (buck1_last_val % 2) + 2;
349 			dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
350 				max8998->buck1_idx);
351 			max8998->buck1_vol[max8998->buck1_idx] = selector;
352 			ret = max8998_get_voltage_register(rdev, &reg,
353 							   &shift,
354 							   &mask);
355 			ret = max8998_write_reg(i2c, reg, selector);
356 			buck1_gpio_set(pdata->buck1_set1,
357 				       pdata->buck1_set2, max8998->buck1_idx);
358 			buck1_last_val++;
359 buck1_exit:
360 			dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
361 				i2c->name, gpio_get_value(pdata->buck1_set1),
362 				gpio_get_value(pdata->buck1_set2));
363 			break;
364 		} else {
365 			ret = max8998_write_reg(i2c, reg, selector);
366 		}
367 		break;
368 
369 	case MAX8998_BUCK2:
370 		dev_dbg(max8998->dev,
371 			"BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
372 			selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
373 		if (gpio_is_valid(pdata->buck2_set3)) {
374 
375 			/* check if requested voltage */
376 			/* value is already defined */
377 			for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
378 				if (max8998->buck2_vol[j] == selector) {
379 					max8998->buck2_idx = j;
380 					buck2_gpio_set(pdata->buck2_set3, j);
381 					goto buck2_exit;
382 				}
383 			}
384 
385 			if (pdata->buck_voltage_lock)
386 				return -EINVAL;
387 
388 			max8998_get_voltage_register(rdev,
389 					&reg, &shift, &mask);
390 			ret = max8998_write_reg(i2c, reg, selector);
391 			max8998->buck2_vol[max8998->buck2_idx] = selector;
392 			buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
393 buck2_exit:
394 			dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
395 				gpio_get_value(pdata->buck2_set3));
396 		} else {
397 			ret = max8998_write_reg(i2c, reg, selector);
398 		}
399 		break;
400 
401 	case MAX8998_BUCK3:
402 	case MAX8998_BUCK4:
403 		ret = max8998_update_reg(i2c, reg, selector<<shift,
404 					 mask<<shift);
405 		break;
406 	}
407 
408 	return ret;
409 }
410 
411 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
412 					     unsigned int old_selector,
413 					     unsigned int new_selector)
414 {
415 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
416 	struct i2c_client *i2c = max8998->iodev->i2c;
417 	const struct voltage_map_desc *desc;
418 	int buck = rdev_get_id(rdev);
419 	u8 val = 0;
420 	int difference, ret;
421 
422 	if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
423 		return -EINVAL;
424 
425 	desc = ldo_voltage_map[buck];
426 
427 	/* Voltage stabilization */
428 	ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
429 	if (ret)
430 		return ret;
431 
432 	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
433 	/* MAX8998 has ENRAMP bit implemented, so test it*/
434 	if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
435 		return 0;
436 
437 	difference = (new_selector - old_selector) * desc->step / 1000;
438 	if (difference > 0)
439 		return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
440 
441 	return 0;
442 }
443 
444 static struct regulator_ops max8998_ldo_ops = {
445 	.list_voltage		= regulator_list_voltage_linear,
446 	.map_voltage		= regulator_map_voltage_linear,
447 	.is_enabled		= max8998_ldo_is_enabled,
448 	.enable			= max8998_ldo_enable,
449 	.disable		= max8998_ldo_disable,
450 	.get_voltage_sel	= max8998_get_voltage_sel,
451 	.set_voltage_sel	= max8998_set_voltage_ldo_sel,
452 };
453 
454 static struct regulator_ops max8998_buck_ops = {
455 	.list_voltage		= regulator_list_voltage_linear,
456 	.map_voltage		= regulator_map_voltage_linear,
457 	.is_enabled		= max8998_ldo_is_enabled,
458 	.enable			= max8998_ldo_enable,
459 	.disable		= max8998_ldo_disable,
460 	.get_voltage_sel	= max8998_get_voltage_sel,
461 	.set_voltage_sel	= max8998_set_voltage_buck_sel,
462 	.set_voltage_time_sel	= max8998_set_voltage_buck_time_sel,
463 };
464 
465 static struct regulator_ops max8998_others_ops = {
466 	.is_enabled		= max8998_ldo_is_enabled,
467 	.enable			= max8998_ldo_enable,
468 	.disable		= max8998_ldo_disable,
469 };
470 
471 static struct regulator_desc regulators[] = {
472 	{
473 		.name		= "LDO2",
474 		.id		= MAX8998_LDO2,
475 		.ops		= &max8998_ldo_ops,
476 		.type		= REGULATOR_VOLTAGE,
477 		.owner		= THIS_MODULE,
478 	}, {
479 		.name		= "LDO3",
480 		.id		= MAX8998_LDO3,
481 		.ops		= &max8998_ldo_ops,
482 		.type		= REGULATOR_VOLTAGE,
483 		.owner		= THIS_MODULE,
484 	}, {
485 		.name		= "LDO4",
486 		.id		= MAX8998_LDO4,
487 		.ops		= &max8998_ldo_ops,
488 		.type		= REGULATOR_VOLTAGE,
489 		.owner		= THIS_MODULE,
490 	}, {
491 		.name		= "LDO5",
492 		.id		= MAX8998_LDO5,
493 		.ops		= &max8998_ldo_ops,
494 		.type		= REGULATOR_VOLTAGE,
495 		.owner		= THIS_MODULE,
496 	}, {
497 		.name		= "LDO6",
498 		.id		= MAX8998_LDO6,
499 		.ops		= &max8998_ldo_ops,
500 		.type		= REGULATOR_VOLTAGE,
501 		.owner		= THIS_MODULE,
502 	}, {
503 		.name		= "LDO7",
504 		.id		= MAX8998_LDO7,
505 		.ops		= &max8998_ldo_ops,
506 		.type		= REGULATOR_VOLTAGE,
507 		.owner		= THIS_MODULE,
508 	}, {
509 		.name		= "LDO8",
510 		.id		= MAX8998_LDO8,
511 		.ops		= &max8998_ldo_ops,
512 		.type		= REGULATOR_VOLTAGE,
513 		.owner		= THIS_MODULE,
514 	}, {
515 		.name		= "LDO9",
516 		.id		= MAX8998_LDO9,
517 		.ops		= &max8998_ldo_ops,
518 		.type		= REGULATOR_VOLTAGE,
519 		.owner		= THIS_MODULE,
520 	}, {
521 		.name		= "LDO10",
522 		.id		= MAX8998_LDO10,
523 		.ops		= &max8998_ldo_ops,
524 		.type		= REGULATOR_VOLTAGE,
525 		.owner		= THIS_MODULE,
526 	}, {
527 		.name		= "LDO11",
528 		.id		= MAX8998_LDO11,
529 		.ops		= &max8998_ldo_ops,
530 		.type		= REGULATOR_VOLTAGE,
531 		.owner		= THIS_MODULE,
532 	}, {
533 		.name		= "LDO12",
534 		.id		= MAX8998_LDO12,
535 		.ops		= &max8998_ldo_ops,
536 		.type		= REGULATOR_VOLTAGE,
537 		.owner		= THIS_MODULE,
538 	}, {
539 		.name		= "LDO13",
540 		.id		= MAX8998_LDO13,
541 		.ops		= &max8998_ldo_ops,
542 		.type		= REGULATOR_VOLTAGE,
543 		.owner		= THIS_MODULE,
544 	}, {
545 		.name		= "LDO14",
546 		.id		= MAX8998_LDO14,
547 		.ops		= &max8998_ldo_ops,
548 		.type		= REGULATOR_VOLTAGE,
549 		.owner		= THIS_MODULE,
550 	}, {
551 		.name		= "LDO15",
552 		.id		= MAX8998_LDO15,
553 		.ops		= &max8998_ldo_ops,
554 		.type		= REGULATOR_VOLTAGE,
555 		.owner		= THIS_MODULE,
556 	}, {
557 		.name		= "LDO16",
558 		.id		= MAX8998_LDO16,
559 		.ops		= &max8998_ldo_ops,
560 		.type		= REGULATOR_VOLTAGE,
561 		.owner		= THIS_MODULE,
562 	}, {
563 		.name		= "LDO17",
564 		.id		= MAX8998_LDO17,
565 		.ops		= &max8998_ldo_ops,
566 		.type		= REGULATOR_VOLTAGE,
567 		.owner		= THIS_MODULE,
568 	}, {
569 		.name		= "BUCK1",
570 		.id		= MAX8998_BUCK1,
571 		.ops		= &max8998_buck_ops,
572 		.type		= REGULATOR_VOLTAGE,
573 		.owner		= THIS_MODULE,
574 	}, {
575 		.name		= "BUCK2",
576 		.id		= MAX8998_BUCK2,
577 		.ops		= &max8998_buck_ops,
578 		.type		= REGULATOR_VOLTAGE,
579 		.owner		= THIS_MODULE,
580 	}, {
581 		.name		= "BUCK3",
582 		.id		= MAX8998_BUCK3,
583 		.ops		= &max8998_buck_ops,
584 		.type		= REGULATOR_VOLTAGE,
585 		.owner		= THIS_MODULE,
586 	}, {
587 		.name		= "BUCK4",
588 		.id		= MAX8998_BUCK4,
589 		.ops		= &max8998_buck_ops,
590 		.type		= REGULATOR_VOLTAGE,
591 		.owner		= THIS_MODULE,
592 	}, {
593 		.name		= "EN32KHz-AP",
594 		.id		= MAX8998_EN32KHZ_AP,
595 		.ops		= &max8998_others_ops,
596 		.type		= REGULATOR_VOLTAGE,
597 		.owner		= THIS_MODULE,
598 	}, {
599 		.name		= "EN32KHz-CP",
600 		.id		= MAX8998_EN32KHZ_CP,
601 		.ops		= &max8998_others_ops,
602 		.type		= REGULATOR_VOLTAGE,
603 		.owner		= THIS_MODULE,
604 	}, {
605 		.name		= "ENVICHG",
606 		.id		= MAX8998_ENVICHG,
607 		.ops		= &max8998_others_ops,
608 		.type		= REGULATOR_VOLTAGE,
609 		.owner		= THIS_MODULE,
610 	}, {
611 		.name		= "ESAFEOUT1",
612 		.id		= MAX8998_ESAFEOUT1,
613 		.ops		= &max8998_others_ops,
614 		.type		= REGULATOR_VOLTAGE,
615 		.owner		= THIS_MODULE,
616 	}, {
617 		.name		= "ESAFEOUT2",
618 		.id		= MAX8998_ESAFEOUT2,
619 		.ops		= &max8998_others_ops,
620 		.type		= REGULATOR_VOLTAGE,
621 		.owner		= THIS_MODULE,
622 	}
623 };
624 
625 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
626 			struct max8998_platform_data *pdata,
627 			struct device_node *pmic_np)
628 {
629 	int gpio;
630 
631 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
632 	if (!gpio_is_valid(gpio)) {
633 		dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
634 		return -EINVAL;
635 	}
636 	pdata->buck1_set1 = gpio;
637 
638 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
639 	if (!gpio_is_valid(gpio)) {
640 		dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
641 		return -EINVAL;
642 	}
643 	pdata->buck1_set2 = gpio;
644 
645 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
646 	if (!gpio_is_valid(gpio)) {
647 		dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
648 		return -EINVAL;
649 	}
650 	pdata->buck2_set3 = gpio;
651 
652 	return 0;
653 }
654 
655 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
656 					struct max8998_platform_data *pdata)
657 {
658 	struct device_node *pmic_np = iodev->dev->of_node;
659 	struct device_node *regulators_np, *reg_np;
660 	struct max8998_regulator_data *rdata;
661 	unsigned int i;
662 	int ret;
663 
664 	regulators_np = of_get_child_by_name(pmic_np, "regulators");
665 	if (!regulators_np) {
666 		dev_err(iodev->dev, "could not find regulators sub-node\n");
667 		return -EINVAL;
668 	}
669 
670 	/* count the number of regulators to be supported in pmic */
671 	pdata->num_regulators = of_get_child_count(regulators_np);
672 
673 	rdata = devm_kcalloc(iodev->dev,
674 			     pdata->num_regulators, sizeof(*rdata),
675 			     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