xref: /openbmc/linux/drivers/regulator/max8998.c (revision fcbd8037)
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 static int max8998_get_enable_register(struct regulator_dev *rdev,
37 					int *reg, int *shift)
38 {
39 	int ldo = rdev_get_id(rdev);
40 
41 	switch (ldo) {
42 	case MAX8998_LDO2 ... MAX8998_LDO5:
43 		*reg = MAX8998_REG_ONOFF1;
44 		*shift = 3 - (ldo - MAX8998_LDO2);
45 		break;
46 	case MAX8998_LDO6 ... MAX8998_LDO13:
47 		*reg = MAX8998_REG_ONOFF2;
48 		*shift = 7 - (ldo - MAX8998_LDO6);
49 		break;
50 	case MAX8998_LDO14 ... MAX8998_LDO17:
51 		*reg = MAX8998_REG_ONOFF3;
52 		*shift = 7 - (ldo - MAX8998_LDO14);
53 		break;
54 	case MAX8998_BUCK1 ... MAX8998_BUCK4:
55 		*reg = MAX8998_REG_ONOFF1;
56 		*shift = 7 - (ldo - MAX8998_BUCK1);
57 		break;
58 	case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
59 		*reg = MAX8998_REG_ONOFF4;
60 		*shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
61 		break;
62 	case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
63 		*reg = MAX8998_REG_CHGR2;
64 		*shift = 7 - (ldo - MAX8998_ESAFEOUT1);
65 		break;
66 	default:
67 		return -EINVAL;
68 	}
69 
70 	return 0;
71 }
72 
73 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
74 {
75 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
76 	struct i2c_client *i2c = max8998->iodev->i2c;
77 	int ret, reg, shift = 8;
78 	u8 val;
79 
80 	ret = max8998_get_enable_register(rdev, &reg, &shift);
81 	if (ret)
82 		return ret;
83 
84 	ret = max8998_read_reg(i2c, reg, &val);
85 	if (ret)
86 		return ret;
87 
88 	return val & (1 << shift);
89 }
90 
91 static int max8998_ldo_enable(struct regulator_dev *rdev)
92 {
93 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
94 	struct i2c_client *i2c = max8998->iodev->i2c;
95 	int reg, shift = 8, ret;
96 
97 	ret = max8998_get_enable_register(rdev, &reg, &shift);
98 	if (ret)
99 		return ret;
100 
101 	return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
102 }
103 
104 static int max8998_ldo_disable(struct regulator_dev *rdev)
105 {
106 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
107 	struct i2c_client *i2c = max8998->iodev->i2c;
108 	int reg, shift = 8, ret;
109 
110 	ret = max8998_get_enable_register(rdev, &reg, &shift);
111 	if (ret)
112 		return ret;
113 
114 	return max8998_update_reg(i2c, reg, 0, 1<<shift);
115 }
116 
117 static int max8998_get_voltage_register(struct regulator_dev *rdev,
118 				int *_reg, int *_shift, int *_mask)
119 {
120 	int ldo = rdev_get_id(rdev);
121 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
122 	int reg, shift = 0, mask = 0xff;
123 
124 	switch (ldo) {
125 	case MAX8998_LDO2 ... MAX8998_LDO3:
126 		reg = MAX8998_REG_LDO2_LDO3;
127 		mask = 0xf;
128 		if (ldo == MAX8998_LDO2)
129 			shift = 4;
130 		else
131 			shift = 0;
132 		break;
133 	case MAX8998_LDO4 ... MAX8998_LDO7:
134 		reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
135 		break;
136 	case MAX8998_LDO8 ... MAX8998_LDO9:
137 		reg = MAX8998_REG_LDO8_LDO9;
138 		mask = 0xf;
139 		if (ldo == MAX8998_LDO8)
140 			shift = 4;
141 		else
142 			shift = 0;
143 		break;
144 	case MAX8998_LDO10 ... MAX8998_LDO11:
145 		reg = MAX8998_REG_LDO10_LDO11;
146 		if (ldo == MAX8998_LDO10) {
147 			shift = 5;
148 			mask = 0x7;
149 		} else {
150 			shift = 0;
151 			mask = 0x1f;
152 		}
153 		break;
154 	case MAX8998_LDO12 ... MAX8998_LDO17:
155 		reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
156 		break;
157 	case MAX8998_BUCK1:
158 		reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
159 		break;
160 	case MAX8998_BUCK2:
161 		reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
162 		break;
163 	case MAX8998_BUCK3:
164 		reg = MAX8998_REG_BUCK3;
165 		break;
166 	case MAX8998_BUCK4:
167 		reg = MAX8998_REG_BUCK4;
168 		break;
169 	default:
170 		return -EINVAL;
171 	}
172 
173 	*_reg = reg;
174 	*_shift = shift;
175 	*_mask = mask;
176 
177 	return 0;
178 }
179 
180 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
181 {
182 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
183 	struct i2c_client *i2c = max8998->iodev->i2c;
184 	int reg, shift = 0, mask, ret;
185 	u8 val;
186 
187 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
188 	if (ret)
189 		return ret;
190 
191 	ret = max8998_read_reg(i2c, reg, &val);
192 	if (ret)
193 		return ret;
194 
195 	val >>= shift;
196 	val &= mask;
197 
198 	return val;
199 }
200 
201 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
202 				       unsigned selector)
203 {
204 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
205 	struct i2c_client *i2c = max8998->iodev->i2c;
206 	int reg, shift = 0, mask, ret;
207 
208 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
209 	if (ret)
210 		return ret;
211 
212 	ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
213 
214 	return ret;
215 }
216 
217 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
218 {
219 	gpio_set_value(gpio1, v & 0x1);
220 	gpio_set_value(gpio2, (v >> 1) & 0x1);
221 }
222 
223 static inline void buck2_gpio_set(int gpio, int v)
224 {
225 	gpio_set_value(gpio, v & 0x1);
226 }
227 
228 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
229 					unsigned selector)
230 {
231 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
232 	struct max8998_platform_data *pdata = max8998->iodev->pdata;
233 	struct i2c_client *i2c = max8998->iodev->i2c;
234 	int buck = rdev_get_id(rdev);
235 	int reg, shift = 0, mask, ret, j;
236 	static u8 buck1_last_val;
237 
238 	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
239 	if (ret)
240 		return ret;
241 
242 	switch (buck) {
243 	case MAX8998_BUCK1:
244 		dev_dbg(max8998->dev,
245 			"BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
246 			"buck1_vol3:%d, buck1_vol4:%d\n",
247 			selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
248 			max8998->buck1_vol[2], max8998->buck1_vol[3]);
249 
250 		if (gpio_is_valid(pdata->buck1_set1) &&
251 		    gpio_is_valid(pdata->buck1_set2)) {
252 
253 			/* check if requested voltage */
254 			/* value is already defined */
255 			for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
256 				if (max8998->buck1_vol[j] == selector) {
257 					max8998->buck1_idx = j;
258 					buck1_gpio_set(pdata->buck1_set1,
259 						       pdata->buck1_set2, j);
260 					goto buck1_exit;
261 				}
262 			}
263 
264 			if (pdata->buck_voltage_lock)
265 				return -EINVAL;
266 
267 			/* no predefine regulator found */
268 			max8998->buck1_idx = (buck1_last_val % 2) + 2;
269 			dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
270 				max8998->buck1_idx);
271 			max8998->buck1_vol[max8998->buck1_idx] = selector;
272 			ret = max8998_get_voltage_register(rdev, &reg,
273 							   &shift,
274 							   &mask);
275 			ret = max8998_write_reg(i2c, reg, selector);
276 			buck1_gpio_set(pdata->buck1_set1,
277 				       pdata->buck1_set2, max8998->buck1_idx);
278 			buck1_last_val++;
279 buck1_exit:
280 			dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
281 				i2c->name, gpio_get_value(pdata->buck1_set1),
282 				gpio_get_value(pdata->buck1_set2));
283 			break;
284 		} else {
285 			ret = max8998_write_reg(i2c, reg, selector);
286 		}
287 		break;
288 
289 	case MAX8998_BUCK2:
290 		dev_dbg(max8998->dev,
291 			"BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
292 			selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
293 		if (gpio_is_valid(pdata->buck2_set3)) {
294 
295 			/* check if requested voltage */
296 			/* value is already defined */
297 			for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
298 				if (max8998->buck2_vol[j] == selector) {
299 					max8998->buck2_idx = j;
300 					buck2_gpio_set(pdata->buck2_set3, j);
301 					goto buck2_exit;
302 				}
303 			}
304 
305 			if (pdata->buck_voltage_lock)
306 				return -EINVAL;
307 
308 			max8998_get_voltage_register(rdev,
309 					&reg, &shift, &mask);
310 			ret = max8998_write_reg(i2c, reg, selector);
311 			max8998->buck2_vol[max8998->buck2_idx] = selector;
312 			buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
313 buck2_exit:
314 			dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
315 				gpio_get_value(pdata->buck2_set3));
316 		} else {
317 			ret = max8998_write_reg(i2c, reg, selector);
318 		}
319 		break;
320 
321 	case MAX8998_BUCK3:
322 	case MAX8998_BUCK4:
323 		ret = max8998_update_reg(i2c, reg, selector<<shift,
324 					 mask<<shift);
325 		break;
326 	}
327 
328 	return ret;
329 }
330 
331 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
332 					     unsigned int old_selector,
333 					     unsigned int new_selector)
334 {
335 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
336 	struct i2c_client *i2c = max8998->iodev->i2c;
337 	int buck = rdev_get_id(rdev);
338 	u8 val = 0;
339 	int difference, ret;
340 
341 	if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
342 		return -EINVAL;
343 
344 	/* Voltage stabilization */
345 	ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
346 	if (ret)
347 		return ret;
348 
349 	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
350 	/* MAX8998 has ENRAMP bit implemented, so test it*/
351 	if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
352 		return 0;
353 
354 	difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000;
355 	if (difference > 0)
356 		return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
357 
358 	return 0;
359 }
360 
361 static const struct regulator_ops max8998_ldo_ops = {
362 	.list_voltage		= regulator_list_voltage_linear,
363 	.map_voltage		= regulator_map_voltage_linear,
364 	.is_enabled		= max8998_ldo_is_enabled,
365 	.enable			= max8998_ldo_enable,
366 	.disable		= max8998_ldo_disable,
367 	.get_voltage_sel	= max8998_get_voltage_sel,
368 	.set_voltage_sel	= max8998_set_voltage_ldo_sel,
369 };
370 
371 static const struct regulator_ops max8998_buck_ops = {
372 	.list_voltage		= regulator_list_voltage_linear,
373 	.map_voltage		= regulator_map_voltage_linear,
374 	.is_enabled		= max8998_ldo_is_enabled,
375 	.enable			= max8998_ldo_enable,
376 	.disable		= max8998_ldo_disable,
377 	.get_voltage_sel	= max8998_get_voltage_sel,
378 	.set_voltage_sel	= max8998_set_voltage_buck_sel,
379 	.set_voltage_time_sel	= max8998_set_voltage_buck_time_sel,
380 };
381 
382 static const struct regulator_ops max8998_others_ops = {
383 	.is_enabled		= max8998_ldo_is_enabled,
384 	.enable			= max8998_ldo_enable,
385 	.disable		= max8998_ldo_disable,
386 };
387 
388 #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
389 	{ \
390 		.name = #_name, \
391 		.id = MAX8998_##_name, \
392 		.ops = _ops, \
393 		.min_uV = (_min), \
394 		.uV_step = (_step), \
395 		.n_voltages = ((_max) - (_min)) / (_step) + 1, \
396 		.type = REGULATOR_VOLTAGE, \
397 		.owner = THIS_MODULE, \
398 	}
399 
400 #define MAX8998_OTHERS_REG(_name, _id) \
401 	{ \
402 		.name = #_name, \
403 		.id = _id, \
404 		.ops = &max8998_others_ops, \
405 		.type = REGULATOR_VOLTAGE, \
406 		.owner = THIS_MODULE, \
407 	}
408 
409 static const struct regulator_desc regulators[] = {
410 	MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000),
411 	MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000),
412 	MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000),
413 	MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000),
414 	MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000),
415 	MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000),
416 	MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000),
417 	MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000),
418 	MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000),
419 	MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000),
420 	MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000),
421 	MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000),
422 	MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000),
423 	MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000),
424 	MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000),
425 	MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000),
426 	MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000),
427 	MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000),
428 	MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000),
429 	MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000),
430 	MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
431 	MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
432 	MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG),
433 	MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1),
434 	MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2),
435 };
436 
437 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
438 			struct max8998_platform_data *pdata,
439 			struct device_node *pmic_np)
440 {
441 	int gpio;
442 
443 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
444 	if (!gpio_is_valid(gpio)) {
445 		dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
446 		return -EINVAL;
447 	}
448 	pdata->buck1_set1 = gpio;
449 
450 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
451 	if (!gpio_is_valid(gpio)) {
452 		dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
453 		return -EINVAL;
454 	}
455 	pdata->buck1_set2 = gpio;
456 
457 	gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
458 	if (!gpio_is_valid(gpio)) {
459 		dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
460 		return -EINVAL;
461 	}
462 	pdata->buck2_set3 = gpio;
463 
464 	return 0;
465 }
466 
467 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
468 					struct max8998_platform_data *pdata)
469 {
470 	struct device_node *pmic_np = iodev->dev->of_node;
471 	struct device_node *regulators_np, *reg_np;
472 	struct max8998_regulator_data *rdata;
473 	unsigned int i;
474 	int ret;
475 
476 	regulators_np = of_get_child_by_name(pmic_np, "regulators");
477 	if (!regulators_np) {
478 		dev_err(iodev->dev, "could not find regulators sub-node\n");
479 		return -EINVAL;
480 	}
481 
482 	/* count the number of regulators to be supported in pmic */
483 	pdata->num_regulators = of_get_child_count(regulators_np);
484 
485 	rdata = devm_kcalloc(iodev->dev,
486 			     pdata->num_regulators, sizeof(*rdata),
487 			     GFP_KERNEL);
488 	if (!rdata) {
489 		of_node_put(regulators_np);
490 		return -ENOMEM;
491 	}
492 
493 	pdata->regulators = rdata;
494 	for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
495 		reg_np = of_get_child_by_name(regulators_np,
496 							regulators[i].name);
497 		if (!reg_np)
498 			continue;
499 
500 		rdata->id = regulators[i].id;
501 		rdata->initdata = of_get_regulator_init_data(iodev->dev,
502 							     reg_np,
503 							     &regulators[i]);
504 		rdata->reg_node = reg_np;
505 		++rdata;
506 	}
507 	pdata->num_regulators = rdata - pdata->regulators;
508 
509 	of_node_put(reg_np);
510 	of_node_put(regulators_np);
511 
512 	ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
513 	if (ret)
514 		return -EINVAL;
515 
516 	if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
517 		pdata->buck_voltage_lock = true;
518 
519 	ret = of_property_read_u32(pmic_np,
520 					"max8998,pmic-buck1-default-dvs-idx",
521 					&pdata->buck1_default_idx);
522 	if (!ret && pdata->buck1_default_idx >= 4) {
523 		pdata->buck1_default_idx = 0;
524 		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
525 	}
526 
527 	ret = of_property_read_u32(pmic_np,
528 					"max8998,pmic-buck2-default-dvs-idx",
529 					&pdata->buck2_default_idx);
530 	if (!ret && pdata->buck2_default_idx >= 2) {
531 		pdata->buck2_default_idx = 0;
532 		dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
533 	}
534 
535 	ret = of_property_read_u32_array(pmic_np,
536 					"max8998,pmic-buck1-dvs-voltage",
537 					pdata->buck1_voltage,
538 					ARRAY_SIZE(pdata->buck1_voltage));
539 	if (ret) {
540 		dev_err(iodev->dev, "buck1 voltages not specified\n");
541 		return -EINVAL;
542 	}
543 
544 	ret = of_property_read_u32_array(pmic_np,
545 					"max8998,pmic-buck2-dvs-voltage",
546 					pdata->buck2_voltage,
547 					ARRAY_SIZE(pdata->buck2_voltage));
548 	if (ret) {
549 		dev_err(iodev->dev, "buck2 voltages not specified\n");
550 		return -EINVAL;
551 	}
552 
553 	return 0;
554 }
555 
556 static int max8998_pmic_probe(struct platform_device *pdev)
557 {
558 	struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
559 	struct max8998_platform_data *pdata = iodev->pdata;
560 	struct regulator_config config = { };
561 	struct regulator_dev *rdev;
562 	struct max8998_data *max8998;
563 	struct i2c_client *i2c;
564 	int i, ret;
565 	unsigned int v;
566 
567 	if (!pdata) {
568 		dev_err(pdev->dev.parent, "No platform init data supplied\n");
569 		return -ENODEV;
570 	}
571 
572 	if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
573 		ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
574 		if (ret)
575 			return ret;
576 	}
577 
578 	max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
579 			       GFP_KERNEL);
580 	if (!max8998)
581 		return -ENOMEM;
582 
583 	max8998->dev = &pdev->dev;
584 	max8998->iodev = iodev;
585 	max8998->num_regulators = pdata->num_regulators;
586 	platform_set_drvdata(pdev, max8998);
587 	i2c = max8998->iodev->i2c;
588 
589 	max8998->buck1_idx = pdata->buck1_default_idx;
590 	max8998->buck2_idx = pdata->buck2_default_idx;
591 
592 	/* NOTE: */
593 	/* For unused GPIO NOT marked as -1 (thereof equal to 0)  WARN_ON */
594 	/* will be displayed */
595 
596 	/* Check if MAX8998 voltage selection GPIOs are defined */
597 	if (gpio_is_valid(pdata->buck1_set1) &&
598 	    gpio_is_valid(pdata->buck1_set2)) {
599 		/* Check if SET1 is not equal to 0 */
600 		if (!pdata->buck1_set1) {
601 			dev_err(&pdev->dev,
602 				"MAX8998 SET1 GPIO defined as 0 !\n");
603 			WARN_ON(!pdata->buck1_set1);
604 			return -EIO;
605 		}
606 		/* Check if SET2 is not equal to 0 */
607 		if (!pdata->buck1_set2) {
608 			dev_err(&pdev->dev,
609 				"MAX8998 SET2 GPIO defined as 0 !\n");
610 			WARN_ON(!pdata->buck1_set2);
611 			return -EIO;
612 		}
613 
614 		gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
615 		gpio_direction_output(pdata->buck1_set1,
616 				      max8998->buck1_idx & 0x1);
617 
618 
619 		gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
620 		gpio_direction_output(pdata->buck1_set2,
621 				      (max8998->buck1_idx >> 1) & 0x1);
622 
623 		/* Set predefined values for BUCK1 registers */
624 		for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
625 			int index = MAX8998_BUCK1 - MAX8998_LDO2;
626 
627 			i = 0;
628 			while (regulators[index].min_uV +
629 			       regulators[index].uV_step * i
630 			       < pdata->buck1_voltage[v])
631 				i++;
632 
633 			max8998->buck1_vol[v] = i;
634 			ret = max8998_write_reg(i2c,
635 					MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
636 			if (ret)
637 				return ret;
638 		}
639 	}
640 
641 	if (gpio_is_valid(pdata->buck2_set3)) {
642 		/* Check if SET3 is not equal to 0 */
643 		if (!pdata->buck2_set3) {
644 			dev_err(&pdev->dev,
645 				"MAX8998 SET3 GPIO defined as 0 !\n");
646 			WARN_ON(!pdata->buck2_set3);
647 			return -EIO;
648 		}
649 		gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
650 		gpio_direction_output(pdata->buck2_set3,
651 				      max8998->buck2_idx & 0x1);
652 
653 		/* Set predefined values for BUCK2 registers */
654 		for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
655 			int index = MAX8998_BUCK2 - MAX8998_LDO2;
656 
657 			i = 0;
658 			while (regulators[index].min_uV +
659 			       regulators[index].uV_step * i
660 			       < pdata->buck2_voltage[v])
661 				i++;
662 
663 			max8998->buck2_vol[v] = i;
664 			ret = max8998_write_reg(i2c,
665 					MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
666 			if (ret)
667 				return ret;
668 		}
669 	}
670 
671 	for (i = 0; i < pdata->num_regulators; i++) {
672 		int index = pdata->regulators[i].id - MAX8998_LDO2;
673 
674 		config.dev = max8998->dev;
675 		config.of_node = pdata->regulators[i].reg_node;
676 		config.init_data = pdata->regulators[i].initdata;
677 		config.driver_data = max8998;
678 
679 		rdev = devm_regulator_register(&pdev->dev, &regulators[index],
680 					       &config);
681 		if (IS_ERR(rdev)) {
682 			ret = PTR_ERR(rdev);
683 			dev_err(max8998->dev, "regulator %s init failed (%d)\n",
684 						regulators[index].name, ret);
685 			return ret;
686 		}
687 	}
688 
689 	return 0;
690 }
691 
692 static const struct platform_device_id max8998_pmic_id[] = {
693 	{ "max8998-pmic", TYPE_MAX8998 },
694 	{ "lp3974-pmic", TYPE_LP3974 },
695 	{ }
696 };
697 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
698 
699 static struct platform_driver max8998_pmic_driver = {
700 	.driver = {
701 		.name = "max8998-pmic",
702 	},
703 	.probe = max8998_pmic_probe,
704 	.id_table = max8998_pmic_id,
705 };
706 
707 static int __init max8998_pmic_init(void)
708 {
709 	return platform_driver_register(&max8998_pmic_driver);
710 }
711 subsys_initcall(max8998_pmic_init);
712 
713 static void __exit max8998_pmic_cleanup(void)
714 {
715 	platform_driver_unregister(&max8998_pmic_driver);
716 }
717 module_exit(max8998_pmic_cleanup);
718 
719 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
720 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
721 MODULE_LICENSE("GPL");
722