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