1 /*
2  * max8973-regulator.c -- Maxim max8973
3  *
4  * Regulator driver for MAXIM 8973 DC-DC step-down switching regulator.
5  *
6  * Copyright (c) 2012, NVIDIA Corporation.
7  *
8  * Author: Laxman Dewangan <ldewangan@nvidia.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation version 2.
13  *
14  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15  * whether express or implied; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22  * 02111-1307, USA
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/regulator/max8973-regulator.h>
35 #include <linux/regulator/of_regulator.h>
36 #include <linux/gpio.h>
37 #include <linux/of_gpio.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <linux/regmap.h>
41 
42 /* Register definitions */
43 #define MAX8973_VOUT					0x0
44 #define MAX8973_VOUT_DVS				0x1
45 #define MAX8973_CONTROL1				0x2
46 #define MAX8973_CONTROL2				0x3
47 #define MAX8973_CHIPID1					0x4
48 #define MAX8973_CHIPID2					0x5
49 
50 #define MAX8973_MAX_VOUT_REG				2
51 
52 /* MAX8973_VOUT */
53 #define MAX8973_VOUT_ENABLE				BIT(7)
54 #define MAX8973_VOUT_MASK				0x7F
55 
56 /* MAX8973_VOUT_DVS */
57 #define MAX8973_DVS_VOUT_MASK				0x7F
58 
59 /* MAX8973_CONTROL1 */
60 #define MAX8973_SNS_ENABLE				BIT(7)
61 #define MAX8973_FPWM_EN_M				BIT(6)
62 #define MAX8973_NFSR_ENABLE				BIT(5)
63 #define MAX8973_AD_ENABLE				BIT(4)
64 #define MAX8973_BIAS_ENABLE				BIT(3)
65 #define MAX8973_FREQSHIFT_9PER				BIT(2)
66 
67 #define MAX8973_RAMP_12mV_PER_US			0x0
68 #define MAX8973_RAMP_25mV_PER_US			0x1
69 #define MAX8973_RAMP_50mV_PER_US			0x2
70 #define MAX8973_RAMP_200mV_PER_US			0x3
71 #define MAX8973_RAMP_MASK				0x3
72 
73 /* MAX8973_CONTROL2 */
74 #define MAX8973_WDTMR_ENABLE				BIT(6)
75 #define MAX8973_DISCH_ENBABLE				BIT(5)
76 #define MAX8973_FT_ENABLE				BIT(4)
77 
78 #define MAX8973_CKKADV_TRIP_MASK			0xC
79 #define MAX8973_CKKADV_TRIP_DISABLE			0xC
80 #define MAX8973_CKKADV_TRIP_75mV_PER_US			0x0
81 #define MAX8973_CKKADV_TRIP_150mV_PER_US		0x4
82 #define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS	0x8
83 #define MAX8973_CONTROL_CLKADV_TRIP_MASK		0x00030000
84 
85 #define MAX8973_INDUCTOR_MIN_30_PER			0x0
86 #define MAX8973_INDUCTOR_NOMINAL			0x1
87 #define MAX8973_INDUCTOR_PLUS_30_PER			0x2
88 #define MAX8973_INDUCTOR_PLUS_60_PER			0x3
89 #define MAX8973_CONTROL_INDUCTOR_VALUE_MASK		0x00300000
90 
91 #define MAX8973_MIN_VOLATGE				606250
92 #define MAX8973_MAX_VOLATGE				1400000
93 #define MAX8973_VOLATGE_STEP				6250
94 #define MAX8973_BUCK_N_VOLTAGE				0x80
95 
96 enum device_id {
97 	MAX8973,
98 	MAX77621
99 };
100 
101 /* Maxim 8973 chip information */
102 struct max8973_chip {
103 	struct device *dev;
104 	struct regulator_desc desc;
105 	struct regmap *regmap;
106 	bool enable_external_control;
107 	int enable_gpio;
108 	int dvs_gpio;
109 	int lru_index[MAX8973_MAX_VOUT_REG];
110 	int curr_vout_val[MAX8973_MAX_VOUT_REG];
111 	int curr_vout_reg;
112 	int curr_gpio_val;
113 	struct regulator_ops ops;
114 	enum device_id id;
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_ramp_delay(struct regulator_dev *rdev,
256 		int ramp_delay)
257 {
258 	struct max8973_chip *max = rdev_get_drvdata(rdev);
259 	unsigned int control;
260 	int ret;
261 	int ret_val;
262 
263 	/* Set ramp delay */
264 	if (ramp_delay < 25000) {
265 		control = MAX8973_RAMP_12mV_PER_US;
266 		ret_val = 12000;
267 	} else if (ramp_delay < 50000) {
268 		control = MAX8973_RAMP_25mV_PER_US;
269 		ret_val = 25000;
270 	} else if (ramp_delay < 200000) {
271 		control = MAX8973_RAMP_50mV_PER_US;
272 		ret_val = 50000;
273 	} else {
274 		control = MAX8973_RAMP_200mV_PER_US;
275 		ret_val = 200000;
276 	}
277 
278 	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
279 			MAX8973_RAMP_MASK, control);
280 	if (ret < 0)
281 		dev_err(max->dev, "register %d update failed, %d",
282 				MAX8973_CONTROL1, ret);
283 	return ret;
284 }
285 
286 static int max8973_set_current_limit(struct regulator_dev *rdev,
287 		int min_ua, int max_ua)
288 {
289 	struct max8973_chip *max = rdev_get_drvdata(rdev);
290 	unsigned int val;
291 	int ret;
292 
293 	if (max_ua <= 9000000)
294 		val = MAX8973_CKKADV_TRIP_75mV_PER_US;
295 	else if (max_ua <= 12000000)
296 		val = MAX8973_CKKADV_TRIP_150mV_PER_US;
297 	else
298 		val = MAX8973_CKKADV_TRIP_DISABLE;
299 
300 	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL2,
301 			MAX8973_CKKADV_TRIP_MASK, val);
302 	if (ret < 0) {
303 		dev_err(max->dev, "register %d update failed: %d\n",
304 				MAX8973_CONTROL2, ret);
305 		return ret;
306 	}
307 	return 0;
308 }
309 
310 static int max8973_get_current_limit(struct regulator_dev *rdev)
311 {
312 	struct max8973_chip *max = rdev_get_drvdata(rdev);
313 	unsigned int control2;
314 	int ret;
315 
316 	ret = regmap_read(max->regmap, MAX8973_CONTROL2, &control2);
317 	if (ret < 0) {
318 		dev_err(max->dev, "register %d read failed: %d\n",
319 				MAX8973_CONTROL2, ret);
320 		return ret;
321 	}
322 	switch (control2 & MAX8973_CKKADV_TRIP_MASK) {
323 	case MAX8973_CKKADV_TRIP_DISABLE:
324 		return 15000000;
325 	case MAX8973_CKKADV_TRIP_150mV_PER_US:
326 		return 12000000;
327 	case MAX8973_CKKADV_TRIP_75mV_PER_US:
328 		return 9000000;
329 	default:
330 		break;
331 	}
332 	return 9000000;
333 }
334 
335 static const struct regulator_ops max8973_dcdc_ops = {
336 	.get_voltage_sel	= max8973_dcdc_get_voltage_sel,
337 	.set_voltage_sel	= max8973_dcdc_set_voltage_sel,
338 	.list_voltage		= regulator_list_voltage_linear,
339 	.set_mode		= max8973_dcdc_set_mode,
340 	.get_mode		= max8973_dcdc_get_mode,
341 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
342 	.set_ramp_delay		= max8973_set_ramp_delay,
343 };
344 
345 static int max8973_init_dcdc(struct max8973_chip *max,
346 			     struct max8973_regulator_platform_data *pdata)
347 {
348 	int ret;
349 	uint8_t	control1 = 0;
350 	uint8_t control2 = 0;
351 	unsigned int data;
352 
353 	ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
354 	if (ret < 0) {
355 		dev_err(max->dev, "register %d read failed, err = %d",
356 				MAX8973_CONTROL1, ret);
357 		return ret;
358 	}
359 	control1 = data & MAX8973_RAMP_MASK;
360 	switch (control1) {
361 	case MAX8973_RAMP_12mV_PER_US:
362 		max->desc.ramp_delay = 12000;
363 		break;
364 	case MAX8973_RAMP_25mV_PER_US:
365 		max->desc.ramp_delay = 25000;
366 		break;
367 	case MAX8973_RAMP_50mV_PER_US:
368 		max->desc.ramp_delay = 50000;
369 		break;
370 	case MAX8973_RAMP_200mV_PER_US:
371 		max->desc.ramp_delay = 200000;
372 		break;
373 	}
374 
375 	if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
376 		control1 |= MAX8973_SNS_ENABLE;
377 
378 	if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
379 		control1 |= MAX8973_NFSR_ENABLE;
380 
381 	if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
382 		control1 |= MAX8973_AD_ENABLE;
383 
384 	if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE) {
385 		control1 |= MAX8973_BIAS_ENABLE;
386 		max->desc.enable_time = 20;
387 	} else {
388 		max->desc.enable_time = 240;
389 	}
390 
391 	if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
392 		control1 |= MAX8973_FREQSHIFT_9PER;
393 
394 	if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
395 		control2 |= MAX8973_DISCH_ENBABLE;
396 
397 	/*  Clock advance trip configuration */
398 	switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
399 	case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
400 		control2 |= MAX8973_CKKADV_TRIP_DISABLE;
401 		break;
402 
403 	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
404 		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
405 		break;
406 
407 	case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
408 		control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
409 		break;
410 
411 	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
412 		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
413 		break;
414 	}
415 
416 	/* Configure inductor value */
417 	switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
418 	case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
419 		control2 |= MAX8973_INDUCTOR_NOMINAL;
420 		break;
421 
422 	case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
423 		control2 |= MAX8973_INDUCTOR_MIN_30_PER;
424 		break;
425 
426 	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
427 		control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
428 		break;
429 
430 	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
431 		control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
432 		break;
433 	}
434 
435 	ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
436 	if (ret < 0) {
437 		dev_err(max->dev, "register %d write failed, err = %d",
438 				MAX8973_CONTROL1, ret);
439 		return ret;
440 	}
441 
442 	ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
443 	if (ret < 0) {
444 		dev_err(max->dev, "register %d write failed, err = %d",
445 				MAX8973_CONTROL2, ret);
446 		return ret;
447 	}
448 
449 	/* If external control is enabled then disable EN bit */
450 	if (max->enable_external_control && (max->id == MAX8973)) {
451 		ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
452 						MAX8973_VOUT_ENABLE, 0);
453 		if (ret < 0)
454 			dev_err(max->dev, "register %d update failed, err = %d",
455 				MAX8973_VOUT, ret);
456 	}
457 	return ret;
458 }
459 
460 static const struct regmap_config max8973_regmap_config = {
461 	.reg_bits		= 8,
462 	.val_bits		= 8,
463 	.max_register		= MAX8973_CHIPID2,
464 	.cache_type		= REGCACHE_RBTREE,
465 };
466 
467 static struct max8973_regulator_platform_data *max8973_parse_dt(
468 		struct device *dev)
469 {
470 	struct max8973_regulator_platform_data *pdata;
471 	struct device_node *np = dev->of_node;
472 	int ret;
473 	u32 pval;
474 	bool etr_enable;
475 	bool etr_sensitivity_high;
476 
477 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
478 	if (!pdata)
479 		return NULL;
480 
481 	pdata->enable_ext_control = of_property_read_bool(np,
482 						"maxim,externally-enable");
483 	pdata->enable_gpio = of_get_named_gpio(np, "maxim,enable-gpio", 0);
484 	pdata->dvs_gpio = of_get_named_gpio(np, "maxim,dvs-gpio", 0);
485 
486 	ret = of_property_read_u32(np, "maxim,dvs-default-state", &pval);
487 	if (!ret)
488 		pdata->dvs_def_state = pval;
489 
490 	if (of_property_read_bool(np, "maxim,enable-remote-sense"))
491 		pdata->control_flags  |= MAX8973_CONTROL_REMOTE_SENSE_ENABLE;
492 
493 	if (of_property_read_bool(np, "maxim,enable-falling-slew-rate"))
494 		pdata->control_flags  |=
495 				MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE;
496 
497 	if (of_property_read_bool(np, "maxim,enable-active-discharge"))
498 		pdata->control_flags  |=
499 				MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE;
500 
501 	if (of_property_read_bool(np, "maxim,enable-frequency-shift"))
502 		pdata->control_flags  |= MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE;
503 
504 	if (of_property_read_bool(np, "maxim,enable-bias-control"))
505 		pdata->control_flags  |= MAX8973_CONTROL_BIAS_ENABLE;
506 
507 	etr_enable = of_property_read_bool(np, "maxim,enable-etr");
508 	etr_sensitivity_high = of_property_read_bool(np,
509 				"maxim,enable-high-etr-sensitivity");
510 	if (etr_sensitivity_high)
511 		etr_enable = true;
512 
513 	if (etr_enable) {
514 		if (etr_sensitivity_high)
515 			pdata->control_flags |=
516 				MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US;
517 		else
518 			pdata->control_flags |=
519 				MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US;
520 	} else {
521 		pdata->control_flags |= MAX8973_CONTROL_CLKADV_TRIP_DISABLED;
522 	}
523 
524 	return pdata;
525 }
526 
527 static const struct of_device_id of_max8973_match_tbl[] = {
528 	{ .compatible = "maxim,max8973", .data = (void *)MAX8973, },
529 	{ .compatible = "maxim,max77621", .data = (void *)MAX77621, },
530 	{ },
531 };
532 MODULE_DEVICE_TABLE(of, of_max8973_match_tbl);
533 
534 static int max8973_probe(struct i2c_client *client,
535 			 const struct i2c_device_id *id)
536 {
537 	struct max8973_regulator_platform_data *pdata;
538 	struct regulator_init_data *ridata;
539 	struct regulator_config config = { };
540 	struct regulator_dev *rdev;
541 	struct max8973_chip *max;
542 	bool pdata_from_dt = false;
543 	unsigned int chip_id;
544 	int ret;
545 
546 	pdata = dev_get_platdata(&client->dev);
547 
548 	if (!pdata && client->dev.of_node) {
549 		pdata = max8973_parse_dt(&client->dev);
550 		pdata_from_dt = true;
551 	}
552 
553 	if (!pdata) {
554 		dev_err(&client->dev, "No Platform data");
555 		return -EIO;
556 	}
557 
558 	if ((pdata->dvs_gpio == -EPROBE_DEFER) ||
559 		(pdata->enable_gpio == -EPROBE_DEFER))
560 		return -EPROBE_DEFER;
561 
562 	max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
563 	if (!max)
564 		return -ENOMEM;
565 
566 	max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
567 	if (IS_ERR(max->regmap)) {
568 		ret = PTR_ERR(max->regmap);
569 		dev_err(&client->dev, "regmap init failed, err %d\n", ret);
570 		return ret;
571 	}
572 
573 	if (client->dev.of_node) {
574 		const struct of_device_id *match;
575 
576 		match = of_match_device(of_match_ptr(of_max8973_match_tbl),
577 				&client->dev);
578 		if (!match)
579 			return -ENODATA;
580 		max->id = (u32)((uintptr_t)match->data);
581 	} else {
582 		max->id = id->driver_data;
583 	}
584 
585 	ret = regmap_read(max->regmap, MAX8973_CHIPID1, &chip_id);
586 	if (ret < 0) {
587 		dev_err(&client->dev, "register CHIPID1 read failed, %d", ret);
588 		return ret;
589 	}
590 
591 	dev_info(&client->dev, "CHIP-ID OTP: 0x%02x ID_M: 0x%02x\n",
592 			(chip_id >> 4) & 0xF, (chip_id >> 1) & 0x7);
593 
594 	i2c_set_clientdata(client, max);
595 	max->ops = max8973_dcdc_ops;
596 	max->dev = &client->dev;
597 	max->desc.name = id->name;
598 	max->desc.id = 0;
599 	max->desc.ops = &max->ops;
600 	max->desc.type = REGULATOR_VOLTAGE;
601 	max->desc.owner = THIS_MODULE;
602 	max->desc.min_uV = MAX8973_MIN_VOLATGE;
603 	max->desc.uV_step = MAX8973_VOLATGE_STEP;
604 	max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
605 
606 	max->dvs_gpio = (pdata->dvs_gpio) ? pdata->dvs_gpio : -EINVAL;
607 	max->enable_gpio = (pdata->enable_gpio) ? pdata->enable_gpio : -EINVAL;
608 	max->enable_external_control = pdata->enable_ext_control;
609 	max->curr_gpio_val = pdata->dvs_def_state;
610 	max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
611 
612 	if (gpio_is_valid(max->enable_gpio))
613 		max->enable_external_control = true;
614 
615 	max->lru_index[0] = max->curr_vout_reg;
616 
617 	if (gpio_is_valid(max->dvs_gpio)) {
618 		int gpio_flags;
619 		int i;
620 
621 		gpio_flags = (pdata->dvs_def_state) ?
622 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
623 		ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
624 				gpio_flags, "max8973-dvs");
625 		if (ret) {
626 			dev_err(&client->dev,
627 				"gpio_request for gpio %d failed, err = %d\n",
628 				max->dvs_gpio, ret);
629 			return ret;
630 		}
631 
632 		/*
633 		 * Initialize the lru index with vout_reg id
634 		 * The index 0 will be most recently used and
635 		 * set with the max->curr_vout_reg */
636 		for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
637 			max->lru_index[i] = i;
638 		max->lru_index[0] = max->curr_vout_reg;
639 		max->lru_index[max->curr_vout_reg] = 0;
640 	} else {
641 		/*
642 		 * If there is no DVS GPIO, the VOUT register
643 		 * address is fixed.
644 		 */
645 		max->ops.set_voltage_sel = regulator_set_voltage_sel_regmap;
646 		max->ops.get_voltage_sel = regulator_get_voltage_sel_regmap;
647 		max->desc.vsel_reg = max->curr_vout_reg;
648 		max->desc.vsel_mask = MAX8973_VOUT_MASK;
649 	}
650 
651 	if (pdata_from_dt)
652 		pdata->reg_init_data = of_get_regulator_init_data(&client->dev,
653 					client->dev.of_node, &max->desc);
654 
655 	ridata = pdata->reg_init_data;
656 	switch (max->id) {
657 	case MAX8973:
658 		if (!pdata->enable_ext_control) {
659 			max->desc.enable_reg = MAX8973_VOUT;
660 			max->desc.enable_mask = MAX8973_VOUT_ENABLE;
661 			max->ops.enable = regulator_enable_regmap;
662 			max->ops.disable = regulator_disable_regmap;
663 			max->ops.is_enabled = regulator_is_enabled_regmap;
664 			break;
665 		}
666 
667 		if (gpio_is_valid(max->enable_gpio)) {
668 			config.ena_gpio_flags = GPIOF_OUT_INIT_LOW;
669 			if (ridata && (ridata->constraints.always_on ||
670 					ridata->constraints.boot_on))
671 				config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
672 			config.ena_gpio = max->enable_gpio;
673 		}
674 		break;
675 
676 	case MAX77621:
677 		if (gpio_is_valid(max->enable_gpio)) {
678 			ret = devm_gpio_request_one(&client->dev,
679 					max->enable_gpio, GPIOF_OUT_INIT_HIGH,
680 					"max8973-en-gpio");
681 			if (ret) {
682 				dev_err(&client->dev,
683 					"gpio_request for gpio %d failed: %d\n",
684 					max->enable_gpio, ret);
685 				return ret;
686 			}
687 		}
688 
689 		max->desc.enable_reg = MAX8973_VOUT;
690 		max->desc.enable_mask = MAX8973_VOUT_ENABLE;
691 		max->ops.enable = regulator_enable_regmap;
692 		max->ops.disable = regulator_disable_regmap;
693 		max->ops.is_enabled = regulator_is_enabled_regmap;
694 		max->ops.set_current_limit = max8973_set_current_limit;
695 		max->ops.get_current_limit = max8973_get_current_limit;
696 		break;
697 	default:
698 		break;
699 	}
700 
701 	ret = max8973_init_dcdc(max, pdata);
702 	if (ret < 0) {
703 		dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
704 		return ret;
705 	}
706 
707 	config.dev = &client->dev;
708 	config.init_data = pdata->reg_init_data;
709 	config.driver_data = max;
710 	config.of_node = client->dev.of_node;
711 	config.regmap = max->regmap;
712 
713 	/* Register the regulators */
714 	rdev = devm_regulator_register(&client->dev, &max->desc, &config);
715 	if (IS_ERR(rdev)) {
716 		ret = PTR_ERR(rdev);
717 		dev_err(max->dev, "regulator register failed, err %d\n", ret);
718 		return ret;
719 	}
720 
721 	return 0;
722 }
723 
724 static const struct i2c_device_id max8973_id[] = {
725 	{.name = "max8973", .driver_data = MAX8973},
726 	{.name = "max77621", .driver_data = MAX77621},
727 	{},
728 };
729 MODULE_DEVICE_TABLE(i2c, max8973_id);
730 
731 static struct i2c_driver max8973_i2c_driver = {
732 	.driver = {
733 		.name = "max8973",
734 		.of_match_table = of_max8973_match_tbl,
735 	},
736 	.probe = max8973_probe,
737 	.id_table = max8973_id,
738 };
739 
740 static int __init max8973_init(void)
741 {
742 	return i2c_add_driver(&max8973_i2c_driver);
743 }
744 subsys_initcall(max8973_init);
745 
746 static void __exit max8973_cleanup(void)
747 {
748 	i2c_del_driver(&max8973_i2c_driver);
749 }
750 module_exit(max8973_cleanup);
751 
752 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
753 MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
754 MODULE_LICENSE("GPL v2");
755