1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * max8973-regulator.c -- Maxim max8973A
4  *
5  * Regulator driver for MAXIM 8973A DC-DC step-down switching regulator.
6  *
7  * Copyright (c) 2012, NVIDIA Corporation.
8  *
9  * Author: Laxman Dewangan <ldewangan@nvidia.com>
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regulator/max8973-regulator.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/gpio.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/of_gpio.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/regmap.h>
29 #include <linux/thermal.h>
30 #include <linux/irq.h>
31 #include <linux/interrupt.h>
32 
33 /* Register definitions */
34 #define MAX8973_VOUT					0x0
35 #define MAX8973_VOUT_DVS				0x1
36 #define MAX8973_CONTROL1				0x2
37 #define MAX8973_CONTROL2				0x3
38 #define MAX8973_CHIPID1					0x4
39 #define MAX8973_CHIPID2					0x5
40 
41 #define MAX8973_MAX_VOUT_REG				2
42 
43 /* MAX8973_VOUT */
44 #define MAX8973_VOUT_ENABLE				BIT(7)
45 #define MAX8973_VOUT_MASK				0x7F
46 
47 /* MAX8973_VOUT_DVS */
48 #define MAX8973_DVS_VOUT_MASK				0x7F
49 
50 /* MAX8973_CONTROL1 */
51 #define MAX8973_SNS_ENABLE				BIT(7)
52 #define MAX8973_FPWM_EN_M				BIT(6)
53 #define MAX8973_NFSR_ENABLE				BIT(5)
54 #define MAX8973_AD_ENABLE				BIT(4)
55 #define MAX8973_BIAS_ENABLE				BIT(3)
56 #define MAX8973_FREQSHIFT_9PER				BIT(2)
57 
58 #define MAX8973_RAMP_12mV_PER_US			0x0
59 #define MAX8973_RAMP_25mV_PER_US			0x1
60 #define MAX8973_RAMP_50mV_PER_US			0x2
61 #define MAX8973_RAMP_200mV_PER_US			0x3
62 #define MAX8973_RAMP_MASK				0x3
63 
64 /* MAX8973_CONTROL2 */
65 #define MAX8973_WDTMR_ENABLE				BIT(6)
66 #define MAX8973_DISCH_ENBABLE				BIT(5)
67 #define MAX8973_FT_ENABLE				BIT(4)
68 #define MAX77621_T_JUNCTION_120				BIT(7)
69 
70 #define MAX8973_CKKADV_TRIP_MASK			0xC
71 #define MAX8973_CKKADV_TRIP_DISABLE			0xC
72 #define MAX8973_CKKADV_TRIP_75mV_PER_US			0x0
73 #define MAX8973_CKKADV_TRIP_150mV_PER_US		0x4
74 #define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS	0x8
75 #define MAX8973_CONTROL_CLKADV_TRIP_MASK		0x00030000
76 
77 #define MAX8973_INDUCTOR_MIN_30_PER			0x0
78 #define MAX8973_INDUCTOR_NOMINAL			0x1
79 #define MAX8973_INDUCTOR_PLUS_30_PER			0x2
80 #define MAX8973_INDUCTOR_PLUS_60_PER			0x3
81 #define MAX8973_CONTROL_INDUCTOR_VALUE_MASK		0x00300000
82 
83 #define MAX8973_MIN_VOLATGE				606250
84 #define MAX8973_MAX_VOLATGE				1400000
85 #define MAX8973_VOLATGE_STEP				6250
86 #define MAX8973_BUCK_N_VOLTAGE				0x80
87 
88 #define MAX77621_CHIPID_TJINT_S				BIT(0)
89 
90 #define MAX77621_NORMAL_OPERATING_TEMP			100000
91 #define MAX77621_TJINT_WARNING_TEMP_120			120000
92 #define MAX77621_TJINT_WARNING_TEMP_140			140000
93 
94 enum device_id {
95 	MAX8973,
96 	MAX77621
97 };
98 
99 /* Maxim 8973 chip information */
100 struct max8973_chip {
101 	struct device *dev;
102 	struct regulator_desc desc;
103 	struct regmap *regmap;
104 	bool enable_external_control;
105 	int dvs_gpio;
106 	int lru_index[MAX8973_MAX_VOUT_REG];
107 	int curr_vout_val[MAX8973_MAX_VOUT_REG];
108 	int curr_vout_reg;
109 	int curr_gpio_val;
110 	struct regulator_ops ops;
111 	enum device_id id;
112 	int junction_temp_warning;
113 	int irq;
114 	struct thermal_zone_device *tz_device;
115 };
116 
117 /*
118  * find_voltage_set_register: Find new voltage configuration register (VOUT).
119  * The finding of the new VOUT register will be based on the LRU mechanism.
120  * Each VOUT register will have different voltage configured . This
121  * Function will look if any of the VOUT register have requested voltage set
122  * or not.
123  *     - If it is already there then it will make that register as most
124  *       recently used and return as found so that caller need not to set
125  *       the VOUT register but need to set the proper gpios to select this
126  *       VOUT register.
127  *     - If requested voltage is not found then it will use the least
128  *       recently mechanism to get new VOUT register for new configuration
129  *       and will return not_found so that caller need to set new VOUT
130  *       register and then gpios (both).
131  */
132 static bool find_voltage_set_register(struct max8973_chip *tps,
133 		int req_vsel, int *vout_reg, int *gpio_val)
134 {
135 	int i;
136 	bool found = false;
137 	int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
138 	int found_index = MAX8973_MAX_VOUT_REG - 1;
139 
140 	for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
141 		if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
142 			new_vout_reg = tps->lru_index[i];
143 			found_index = i;
144 			found = true;
145 			goto update_lru_index;
146 		}
147 	}
148 
149 update_lru_index:
150 	for (i = found_index; i > 0; i--)
151 		tps->lru_index[i] = tps->lru_index[i - 1];
152 
153 	tps->lru_index[0] = new_vout_reg;
154 	*gpio_val = new_vout_reg;
155 	*vout_reg = MAX8973_VOUT + new_vout_reg;
156 	return found;
157 }
158 
159 static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
160 {
161 	struct max8973_chip *max = rdev_get_drvdata(rdev);
162 	unsigned int data;
163 	int ret;
164 
165 	ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
166 	if (ret < 0) {
167 		dev_err(max->dev, "register %d read failed, err = %d\n",
168 			max->curr_vout_reg, ret);
169 		return ret;
170 	}
171 	return data & MAX8973_VOUT_MASK;
172 }
173 
174 static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
175 	     unsigned vsel)
176 {
177 	struct max8973_chip *max = rdev_get_drvdata(rdev);
178 	int ret;
179 	bool found = false;
180 	int vout_reg = max->curr_vout_reg;
181 	int gpio_val = max->curr_gpio_val;
182 
183 	/*
184 	 * If gpios are available to select the VOUT register then least
185 	 * recently used register for new configuration.
186 	 */
187 	if (gpio_is_valid(max->dvs_gpio))
188 		found = find_voltage_set_register(max, vsel,
189 					&vout_reg, &gpio_val);
190 
191 	if (!found) {
192 		ret = regmap_update_bits(max->regmap, vout_reg,
193 					MAX8973_VOUT_MASK, vsel);
194 		if (ret < 0) {
195 			dev_err(max->dev, "register %d update failed, err %d\n",
196 				 vout_reg, ret);
197 			return ret;
198 		}
199 		max->curr_vout_reg = vout_reg;
200 		max->curr_vout_val[gpio_val] = vsel;
201 	}
202 
203 	/* Select proper VOUT register vio gpios */
204 	if (gpio_is_valid(max->dvs_gpio)) {
205 		gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
206 		max->curr_gpio_val = gpio_val;
207 	}
208 	return 0;
209 }
210 
211 static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
212 {
213 	struct max8973_chip *max = rdev_get_drvdata(rdev);
214 	int ret;
215 	int pwm;
216 
217 	/* Enable force PWM mode in FAST mode only. */
218 	switch (mode) {
219 	case REGULATOR_MODE_FAST:
220 		pwm = MAX8973_FPWM_EN_M;
221 		break;
222 
223 	case REGULATOR_MODE_NORMAL:
224 		pwm = 0;
225 		break;
226 
227 	default:
228 		return -EINVAL;
229 	}
230 
231 	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
232 				MAX8973_FPWM_EN_M, pwm);
233 	if (ret < 0)
234 		dev_err(max->dev, "register %d update failed, err %d\n",
235 				MAX8973_CONTROL1, ret);
236 	return ret;
237 }
238 
239 static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
240 {
241 	struct max8973_chip *max = rdev_get_drvdata(rdev);
242 	unsigned int data;
243 	int ret;
244 
245 	ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
246 	if (ret < 0) {
247 		dev_err(max->dev, "register %d read failed, err %d\n",
248 				MAX8973_CONTROL1, ret);
249 		return ret;
250 	}
251 	return (data & MAX8973_FPWM_EN_M) ?
252 		REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
253 }
254 
255 static int max8973_set_current_limit(struct regulator_dev *rdev,
256 		int min_ua, int max_ua)
257 {
258 	struct max8973_chip *max = rdev_get_drvdata(rdev);
259 	unsigned int val;
260 	int ret;
261 
262 	if (max_ua <= 9000000)
263 		val = MAX8973_CKKADV_TRIP_75mV_PER_US;
264 	else if (max_ua <= 12000000)
265 		val = MAX8973_CKKADV_TRIP_150mV_PER_US;
266 	else
267 		val = MAX8973_CKKADV_TRIP_DISABLE;
268 
269 	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL2,
270 			MAX8973_CKKADV_TRIP_MASK, val);
271 	if (ret < 0) {
272 		dev_err(max->dev, "register %d update failed: %d\n",
273 				MAX8973_CONTROL2, ret);
274 		return ret;
275 	}
276 	return 0;
277 }
278 
279 static int max8973_get_current_limit(struct regulator_dev *rdev)
280 {
281 	struct max8973_chip *max = rdev_get_drvdata(rdev);
282 	unsigned int control2;
283 	int ret;
284 
285 	ret = regmap_read(max->regmap, MAX8973_CONTROL2, &control2);
286 	if (ret < 0) {
287 		dev_err(max->dev, "register %d read failed: %d\n",
288 				MAX8973_CONTROL2, ret);
289 		return ret;
290 	}
291 	switch (control2 & MAX8973_CKKADV_TRIP_MASK) {
292 	case MAX8973_CKKADV_TRIP_DISABLE:
293 		return 15000000;
294 	case MAX8973_CKKADV_TRIP_150mV_PER_US:
295 		return 12000000;
296 	case MAX8973_CKKADV_TRIP_75mV_PER_US:
297 		return 9000000;
298 	default:
299 		break;
300 	}
301 	return 9000000;
302 }
303 
304 static const unsigned int max8973_buck_ramp_table[] = {
305 	12000, 25000, 50000, 200000
306 };
307 
308 static const struct regulator_ops max8973_dcdc_ops = {
309 	.get_voltage_sel	= max8973_dcdc_get_voltage_sel,
310 	.set_voltage_sel	= max8973_dcdc_set_voltage_sel,
311 	.list_voltage		= regulator_list_voltage_linear,
312 	.set_mode		= max8973_dcdc_set_mode,
313 	.get_mode		= max8973_dcdc_get_mode,
314 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
315 	.set_ramp_delay		= regulator_set_ramp_delay_regmap,
316 };
317 
318 static int max8973_init_dcdc(struct max8973_chip *max,
319 			     struct max8973_regulator_platform_data *pdata)
320 {
321 	int ret;
322 	uint8_t	control1 = 0;
323 	uint8_t control2 = 0;
324 	unsigned int data;
325 
326 	ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
327 	if (ret < 0) {
328 		dev_err(max->dev, "register %d read failed, err = %d",
329 				MAX8973_CONTROL1, ret);
330 		return ret;
331 	}
332 	control1 = data & MAX8973_RAMP_MASK;
333 	switch (control1) {
334 	case MAX8973_RAMP_12mV_PER_US:
335 		max->desc.ramp_delay = 12000;
336 		break;
337 	case MAX8973_RAMP_25mV_PER_US:
338 		max->desc.ramp_delay = 25000;
339 		break;
340 	case MAX8973_RAMP_50mV_PER_US:
341 		max->desc.ramp_delay = 50000;
342 		break;
343 	case MAX8973_RAMP_200mV_PER_US:
344 		max->desc.ramp_delay = 200000;
345 		break;
346 	}
347 
348 	if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
349 		control1 |= MAX8973_SNS_ENABLE;
350 
351 	if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
352 		control1 |= MAX8973_NFSR_ENABLE;
353 
354 	if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
355 		control1 |= MAX8973_AD_ENABLE;
356 
357 	if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE) {
358 		control1 |= MAX8973_BIAS_ENABLE;
359 		max->desc.enable_time = 20;
360 	} else {
361 		max->desc.enable_time = 240;
362 	}
363 
364 	if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
365 		control1 |= MAX8973_FREQSHIFT_9PER;
366 
367 	if ((pdata->junction_temp_warning == MAX77621_TJINT_WARNING_TEMP_120) &&
368 	    (max->id == MAX77621))
369 		control2 |= MAX77621_T_JUNCTION_120;
370 
371 	if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
372 		control2 |= MAX8973_DISCH_ENBABLE;
373 
374 	/*  Clock advance trip configuration */
375 	switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
376 	case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
377 		control2 |= MAX8973_CKKADV_TRIP_DISABLE;
378 		break;
379 
380 	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
381 		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
382 		break;
383 
384 	case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
385 		control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
386 		break;
387 
388 	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
389 		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
390 		break;
391 	}
392 
393 	/* Configure inductor value */
394 	switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
395 	case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
396 		control2 |= MAX8973_INDUCTOR_NOMINAL;
397 		break;
398 
399 	case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
400 		control2 |= MAX8973_INDUCTOR_MIN_30_PER;
401 		break;
402 
403 	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
404 		control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
405 		break;
406 
407 	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
408 		control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
409 		break;
410 	}
411 
412 	ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
413 	if (ret < 0) {
414 		dev_err(max->dev, "register %d write failed, err = %d",
415 				MAX8973_CONTROL1, ret);
416 		return ret;
417 	}
418 
419 	ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
420 	if (ret < 0) {
421 		dev_err(max->dev, "register %d write failed, err = %d",
422 				MAX8973_CONTROL2, ret);
423 		return ret;
424 	}
425 
426 	/* If external control is enabled then disable EN bit */
427 	if (max->enable_external_control && (max->id == MAX8973)) {
428 		ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
429 						MAX8973_VOUT_ENABLE, 0);
430 		if (ret < 0)
431 			dev_err(max->dev, "register %d update failed, err = %d",
432 				MAX8973_VOUT, ret);
433 	}
434 	return ret;
435 }
436 
437 static int max8973_thermal_read_temp(struct thermal_zone_device *tz, int *temp)
438 {
439 	struct max8973_chip *mchip = thermal_zone_device_priv(tz);
440 	unsigned int val;
441 	int ret;
442 
443 	ret = regmap_read(mchip->regmap, MAX8973_CHIPID1, &val);
444 	if (ret < 0) {
445 		dev_err(mchip->dev, "Failed to read register CHIPID1, %d", ret);
446 		return ret;
447 	}
448 
449 	/* +1 degC to trigger cool device */
450 	if (val & MAX77621_CHIPID_TJINT_S)
451 		*temp = mchip->junction_temp_warning + 1000;
452 	else
453 		*temp = MAX77621_NORMAL_OPERATING_TEMP;
454 
455 	return 0;
456 }
457 
458 static irqreturn_t max8973_thermal_irq(int irq, void *data)
459 {
460 	struct max8973_chip *mchip = data;
461 
462 	thermal_zone_device_update(mchip->tz_device,
463 				   THERMAL_EVENT_UNSPECIFIED);
464 
465 	return IRQ_HANDLED;
466 }
467 
468 static const struct thermal_zone_device_ops max77621_tz_ops = {
469 	.get_temp = max8973_thermal_read_temp,
470 };
471 
472 static int max8973_thermal_init(struct max8973_chip *mchip)
473 {
474 	struct thermal_zone_device *tzd;
475 	struct irq_data *irq_data;
476 	unsigned long irq_flags = 0;
477 	int ret;
478 
479 	if (mchip->id != MAX77621)
480 		return 0;
481 
482 	tzd = devm_thermal_of_zone_register(mchip->dev, 0, mchip,
483 					    &max77621_tz_ops);
484 	if (IS_ERR(tzd)) {
485 		ret = PTR_ERR(tzd);
486 		dev_err(mchip->dev, "Failed to register thermal sensor: %d\n",
487 			ret);
488 		return ret;
489 	}
490 
491 	if (mchip->irq <= 0)
492 		return 0;
493 
494 	irq_data = irq_get_irq_data(mchip->irq);
495 	if (irq_data)
496 		irq_flags = irqd_get_trigger_type(irq_data);
497 
498 	ret = devm_request_threaded_irq(mchip->dev, mchip->irq, NULL,
499 					max8973_thermal_irq,
500 					IRQF_ONESHOT | IRQF_SHARED | irq_flags,
501 					dev_name(mchip->dev), mchip);
502 	if (ret < 0) {
503 		dev_err(mchip->dev, "Failed to request irq %d, %d\n",
504 			mchip->irq, ret);
505 		return ret;
506 	}
507 
508 	return 0;
509 }
510 
511 static const struct regmap_config max8973_regmap_config = {
512 	.reg_bits		= 8,
513 	.val_bits		= 8,
514 	.max_register		= MAX8973_CHIPID2,
515 	.cache_type		= REGCACHE_RBTREE,
516 };
517 
518 static struct max8973_regulator_platform_data *max8973_parse_dt(
519 		struct device *dev)
520 {
521 	struct max8973_regulator_platform_data *pdata;
522 	struct device_node *np = dev->of_node;
523 	int ret;
524 	u32 pval;
525 	bool etr_enable;
526 	bool etr_sensitivity_high;
527 
528 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
529 	if (!pdata)
530 		return NULL;
531 
532 	pdata->enable_ext_control = of_property_read_bool(np,
533 						"maxim,externally-enable");
534 	pdata->dvs_gpio = of_get_named_gpio(np, "maxim,dvs-gpio", 0);
535 
536 	ret = of_property_read_u32(np, "maxim,dvs-default-state", &pval);
537 	if (!ret)
538 		pdata->dvs_def_state = pval;
539 
540 	if (of_property_read_bool(np, "maxim,enable-remote-sense"))
541 		pdata->control_flags  |= MAX8973_CONTROL_REMOTE_SENSE_ENABLE;
542 
543 	if (of_property_read_bool(np, "maxim,enable-falling-slew-rate"))
544 		pdata->control_flags  |=
545 				MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE;
546 
547 	if (of_property_read_bool(np, "maxim,enable-active-discharge"))
548 		pdata->control_flags  |=
549 				MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE;
550 
551 	if (of_property_read_bool(np, "maxim,enable-frequency-shift"))
552 		pdata->control_flags  |= MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE;
553 
554 	if (of_property_read_bool(np, "maxim,enable-bias-control"))
555 		pdata->control_flags  |= MAX8973_CONTROL_BIAS_ENABLE;
556 
557 	etr_enable = of_property_read_bool(np, "maxim,enable-etr");
558 	etr_sensitivity_high = of_property_read_bool(np,
559 				"maxim,enable-high-etr-sensitivity");
560 	if (etr_sensitivity_high)
561 		etr_enable = true;
562 
563 	if (etr_enable) {
564 		if (etr_sensitivity_high)
565 			pdata->control_flags |=
566 				MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US;
567 		else
568 			pdata->control_flags |=
569 				MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US;
570 	} else {
571 		pdata->control_flags |= MAX8973_CONTROL_CLKADV_TRIP_DISABLED;
572 	}
573 
574 	pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_140;
575 	ret = of_property_read_u32(np, "junction-warn-millicelsius", &pval);
576 	if (!ret && (pval <= MAX77621_TJINT_WARNING_TEMP_120))
577 		pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_120;
578 
579 	return pdata;
580 }
581 
582 static const struct of_device_id of_max8973_match_tbl[] = {
583 	{ .compatible = "maxim,max8973", .data = (void *)MAX8973, },
584 	{ .compatible = "maxim,max77621", .data = (void *)MAX77621, },
585 	{ },
586 };
587 MODULE_DEVICE_TABLE(of, of_max8973_match_tbl);
588 
589 static int max8973_probe(struct i2c_client *client)
590 {
591 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
592 	struct max8973_regulator_platform_data *pdata;
593 	struct regulator_init_data *ridata;
594 	struct regulator_config config = { };
595 	struct regulator_dev *rdev;
596 	struct max8973_chip *max;
597 	bool pdata_from_dt = false;
598 	unsigned int chip_id;
599 	struct gpio_desc *gpiod;
600 	enum gpiod_flags gflags;
601 	int ret;
602 
603 	pdata = dev_get_platdata(&client->dev);
604 
605 	if (!pdata && client->dev.of_node) {
606 		pdata = max8973_parse_dt(&client->dev);
607 		pdata_from_dt = true;
608 	}
609 
610 	if (!pdata) {
611 		dev_err(&client->dev, "No Platform data");
612 		return -EIO;
613 	}
614 
615 	if (pdata->dvs_gpio == -EPROBE_DEFER)
616 		return -EPROBE_DEFER;
617 
618 	max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
619 	if (!max)
620 		return -ENOMEM;
621 
622 	max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
623 	if (IS_ERR(max->regmap)) {
624 		ret = PTR_ERR(max->regmap);
625 		dev_err(&client->dev, "regmap init failed, err %d\n", ret);
626 		return ret;
627 	}
628 
629 	if (client->dev.of_node) {
630 		const struct of_device_id *match;
631 
632 		match = of_match_device(of_match_ptr(of_max8973_match_tbl),
633 				&client->dev);
634 		if (!match)
635 			return -ENODATA;
636 		max->id = (u32)((uintptr_t)match->data);
637 	} else {
638 		max->id = id->driver_data;
639 	}
640 
641 	ret = regmap_read(max->regmap, MAX8973_CHIPID1, &chip_id);
642 	if (ret < 0) {
643 		dev_err(&client->dev, "register CHIPID1 read failed, %d", ret);
644 		return ret;
645 	}
646 
647 	dev_info(&client->dev, "CHIP-ID OTP: 0x%02x ID_M: 0x%02x\n",
648 			(chip_id >> 4) & 0xF, (chip_id >> 1) & 0x7);
649 
650 	i2c_set_clientdata(client, max);
651 	max->ops = max8973_dcdc_ops;
652 	max->dev = &client->dev;
653 	max->desc.name = id->name;
654 	max->desc.id = 0;
655 	max->desc.ops = &max->ops;
656 	max->desc.type = REGULATOR_VOLTAGE;
657 	max->desc.owner = THIS_MODULE;
658 	max->desc.min_uV = MAX8973_MIN_VOLATGE;
659 	max->desc.uV_step = MAX8973_VOLATGE_STEP;
660 	max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
661 	max->desc.ramp_reg = MAX8973_CONTROL1;
662 	max->desc.ramp_mask = MAX8973_RAMP_MASK;
663 	max->desc.ramp_delay_table = max8973_buck_ramp_table;
664 	max->desc.n_ramp_values = ARRAY_SIZE(max8973_buck_ramp_table);
665 
666 	max->dvs_gpio = (pdata->dvs_gpio) ? pdata->dvs_gpio : -EINVAL;
667 	max->enable_external_control = pdata->enable_ext_control;
668 	max->curr_gpio_val = pdata->dvs_def_state;
669 	max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
670 	max->junction_temp_warning = pdata->junction_temp_warning;
671 
672 	max->lru_index[0] = max->curr_vout_reg;
673 
674 	if (gpio_is_valid(max->dvs_gpio)) {
675 		int gpio_flags;
676 		int i;
677 
678 		gpio_flags = (pdata->dvs_def_state) ?
679 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
680 		ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
681 				gpio_flags, "max8973-dvs");
682 		if (ret) {
683 			dev_err(&client->dev,
684 				"gpio_request for gpio %d failed, err = %d\n",
685 				max->dvs_gpio, ret);
686 			return ret;
687 		}
688 
689 		/*
690 		 * Initialize the lru index with vout_reg id
691 		 * The index 0 will be most recently used and
692 		 * set with the max->curr_vout_reg */
693 		for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
694 			max->lru_index[i] = i;
695 		max->lru_index[0] = max->curr_vout_reg;
696 		max->lru_index[max->curr_vout_reg] = 0;
697 	} else {
698 		/*
699 		 * If there is no DVS GPIO, the VOUT register
700 		 * address is fixed.
701 		 */
702 		max->ops.set_voltage_sel = regulator_set_voltage_sel_regmap;
703 		max->ops.get_voltage_sel = regulator_get_voltage_sel_regmap;
704 		max->desc.vsel_reg = max->curr_vout_reg;
705 		max->desc.vsel_mask = MAX8973_VOUT_MASK;
706 	}
707 
708 	if (pdata_from_dt)
709 		pdata->reg_init_data = of_get_regulator_init_data(&client->dev,
710 					client->dev.of_node, &max->desc);
711 
712 	ridata = pdata->reg_init_data;
713 	switch (max->id) {
714 	case MAX8973:
715 		if (!pdata->enable_ext_control) {
716 			max->desc.enable_reg = MAX8973_VOUT;
717 			max->desc.enable_mask = MAX8973_VOUT_ENABLE;
718 			max->ops.enable = regulator_enable_regmap;
719 			max->ops.disable = regulator_disable_regmap;
720 			max->ops.is_enabled = regulator_is_enabled_regmap;
721 			break;
722 		}
723 
724 		if (ridata && (ridata->constraints.always_on ||
725 			       ridata->constraints.boot_on))
726 			gflags = GPIOD_OUT_HIGH;
727 		else
728 			gflags = GPIOD_OUT_LOW;
729 		gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
730 		gpiod = devm_gpiod_get_optional(&client->dev,
731 						"maxim,enable",
732 						gflags);
733 		if (IS_ERR(gpiod))
734 			return PTR_ERR(gpiod);
735 		if (gpiod) {
736 			config.ena_gpiod = gpiod;
737 			max->enable_external_control = true;
738 		}
739 
740 		break;
741 
742 	case MAX77621:
743 		/*
744 		 * We do not let the core switch this regulator on/off,
745 		 * we just leave it on.
746 		 */
747 		gpiod = devm_gpiod_get_optional(&client->dev,
748 						"maxim,enable",
749 						GPIOD_OUT_HIGH);
750 		if (IS_ERR(gpiod))
751 			return PTR_ERR(gpiod);
752 		if (gpiod)
753 			max->enable_external_control = true;
754 
755 		max->desc.enable_reg = MAX8973_VOUT;
756 		max->desc.enable_mask = MAX8973_VOUT_ENABLE;
757 		max->ops.enable = regulator_enable_regmap;
758 		max->ops.disable = regulator_disable_regmap;
759 		max->ops.is_enabled = regulator_is_enabled_regmap;
760 		max->ops.set_current_limit = max8973_set_current_limit;
761 		max->ops.get_current_limit = max8973_get_current_limit;
762 		break;
763 	default:
764 		break;
765 	}
766 
767 	ret = max8973_init_dcdc(max, pdata);
768 	if (ret < 0) {
769 		dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
770 		return ret;
771 	}
772 
773 	config.dev = &client->dev;
774 	config.init_data = pdata->reg_init_data;
775 	config.driver_data = max;
776 	config.of_node = client->dev.of_node;
777 	config.regmap = max->regmap;
778 
779 	/*
780 	 * Register the regulators
781 	 * Turn the GPIO descriptor over to the regulator core for
782 	 * lifecycle management if we pass an ena_gpiod.
783 	 */
784 	if (config.ena_gpiod)
785 		devm_gpiod_unhinge(&client->dev, config.ena_gpiod);
786 	rdev = devm_regulator_register(&client->dev, &max->desc, &config);
787 	if (IS_ERR(rdev)) {
788 		ret = PTR_ERR(rdev);
789 		dev_err(max->dev, "regulator register failed, err %d\n", ret);
790 		return ret;
791 	}
792 
793 	max8973_thermal_init(max);
794 	return 0;
795 }
796 
797 static const struct i2c_device_id max8973_id[] = {
798 	{.name = "max8973", .driver_data = MAX8973},
799 	{.name = "max77621", .driver_data = MAX77621},
800 	{},
801 };
802 MODULE_DEVICE_TABLE(i2c, max8973_id);
803 
804 static struct i2c_driver max8973_i2c_driver = {
805 	.driver = {
806 		.name = "max8973",
807 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
808 		.of_match_table = of_max8973_match_tbl,
809 	},
810 	.probe = max8973_probe,
811 	.id_table = max8973_id,
812 };
813 
814 static int __init max8973_init(void)
815 {
816 	return i2c_add_driver(&max8973_i2c_driver);
817 }
818 subsys_initcall(max8973_init);
819 
820 static void __exit max8973_cleanup(void)
821 {
822 	i2c_del_driver(&max8973_i2c_driver);
823 }
824 module_exit(max8973_cleanup);
825 
826 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
827 MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
828 MODULE_LICENSE("GPL v2");
829