18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * max8973-regulator.c -- Maxim max8973
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Regulator driver for MAXIM 8973 DC-DC step-down switching regulator.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2012, NVIDIA Corporation.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Author: Laxman Dewangan <ldewangan@nvidia.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
118c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as
128c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
158c2ecf20Sopenharmony_ci * whether express or implied; without even the implied warranty of
168c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
178c2ecf20Sopenharmony_ci * General Public License for more details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
208c2ecf20Sopenharmony_ci * along with this program; if not, write to the Free Software
218c2ecf20Sopenharmony_ci * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
228c2ecf20Sopenharmony_ci * 02111-1307, USA
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/init.h>
288c2ecf20Sopenharmony_ci#include <linux/err.h>
298c2ecf20Sopenharmony_ci#include <linux/of.h>
308c2ecf20Sopenharmony_ci#include <linux/of_device.h>
318c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
328c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
338c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
348c2ecf20Sopenharmony_ci#include <linux/regulator/max8973-regulator.h>
358c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
368c2ecf20Sopenharmony_ci#include <linux/gpio.h>
378c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
388c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
398c2ecf20Sopenharmony_ci#include <linux/i2c.h>
408c2ecf20Sopenharmony_ci#include <linux/slab.h>
418c2ecf20Sopenharmony_ci#include <linux/regmap.h>
428c2ecf20Sopenharmony_ci#include <linux/thermal.h>
438c2ecf20Sopenharmony_ci#include <linux/irq.h>
448c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Register definitions */
478c2ecf20Sopenharmony_ci#define MAX8973_VOUT					0x0
488c2ecf20Sopenharmony_ci#define MAX8973_VOUT_DVS				0x1
498c2ecf20Sopenharmony_ci#define MAX8973_CONTROL1				0x2
508c2ecf20Sopenharmony_ci#define MAX8973_CONTROL2				0x3
518c2ecf20Sopenharmony_ci#define MAX8973_CHIPID1					0x4
528c2ecf20Sopenharmony_ci#define MAX8973_CHIPID2					0x5
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define MAX8973_MAX_VOUT_REG				2
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* MAX8973_VOUT */
578c2ecf20Sopenharmony_ci#define MAX8973_VOUT_ENABLE				BIT(7)
588c2ecf20Sopenharmony_ci#define MAX8973_VOUT_MASK				0x7F
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* MAX8973_VOUT_DVS */
618c2ecf20Sopenharmony_ci#define MAX8973_DVS_VOUT_MASK				0x7F
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* MAX8973_CONTROL1 */
648c2ecf20Sopenharmony_ci#define MAX8973_SNS_ENABLE				BIT(7)
658c2ecf20Sopenharmony_ci#define MAX8973_FPWM_EN_M				BIT(6)
668c2ecf20Sopenharmony_ci#define MAX8973_NFSR_ENABLE				BIT(5)
678c2ecf20Sopenharmony_ci#define MAX8973_AD_ENABLE				BIT(4)
688c2ecf20Sopenharmony_ci#define MAX8973_BIAS_ENABLE				BIT(3)
698c2ecf20Sopenharmony_ci#define MAX8973_FREQSHIFT_9PER				BIT(2)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define MAX8973_RAMP_12mV_PER_US			0x0
728c2ecf20Sopenharmony_ci#define MAX8973_RAMP_25mV_PER_US			0x1
738c2ecf20Sopenharmony_ci#define MAX8973_RAMP_50mV_PER_US			0x2
748c2ecf20Sopenharmony_ci#define MAX8973_RAMP_200mV_PER_US			0x3
758c2ecf20Sopenharmony_ci#define MAX8973_RAMP_MASK				0x3
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/* MAX8973_CONTROL2 */
788c2ecf20Sopenharmony_ci#define MAX8973_WDTMR_ENABLE				BIT(6)
798c2ecf20Sopenharmony_ci#define MAX8973_DISCH_ENBABLE				BIT(5)
808c2ecf20Sopenharmony_ci#define MAX8973_FT_ENABLE				BIT(4)
818c2ecf20Sopenharmony_ci#define MAX77621_T_JUNCTION_120				BIT(7)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define MAX8973_CKKADV_TRIP_MASK			0xC
848c2ecf20Sopenharmony_ci#define MAX8973_CKKADV_TRIP_DISABLE			0xC
858c2ecf20Sopenharmony_ci#define MAX8973_CKKADV_TRIP_75mV_PER_US			0x0
868c2ecf20Sopenharmony_ci#define MAX8973_CKKADV_TRIP_150mV_PER_US		0x4
878c2ecf20Sopenharmony_ci#define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS	0x8
888c2ecf20Sopenharmony_ci#define MAX8973_CONTROL_CLKADV_TRIP_MASK		0x00030000
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define MAX8973_INDUCTOR_MIN_30_PER			0x0
918c2ecf20Sopenharmony_ci#define MAX8973_INDUCTOR_NOMINAL			0x1
928c2ecf20Sopenharmony_ci#define MAX8973_INDUCTOR_PLUS_30_PER			0x2
938c2ecf20Sopenharmony_ci#define MAX8973_INDUCTOR_PLUS_60_PER			0x3
948c2ecf20Sopenharmony_ci#define MAX8973_CONTROL_INDUCTOR_VALUE_MASK		0x00300000
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define MAX8973_MIN_VOLATGE				606250
978c2ecf20Sopenharmony_ci#define MAX8973_MAX_VOLATGE				1400000
988c2ecf20Sopenharmony_ci#define MAX8973_VOLATGE_STEP				6250
998c2ecf20Sopenharmony_ci#define MAX8973_BUCK_N_VOLTAGE				0x80
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define MAX77621_CHIPID_TJINT_S				BIT(0)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define MAX77621_NORMAL_OPERATING_TEMP			100000
1048c2ecf20Sopenharmony_ci#define MAX77621_TJINT_WARNING_TEMP_120			120000
1058c2ecf20Sopenharmony_ci#define MAX77621_TJINT_WARNING_TEMP_140			140000
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cienum device_id {
1088c2ecf20Sopenharmony_ci	MAX8973,
1098c2ecf20Sopenharmony_ci	MAX77621
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* Maxim 8973 chip information */
1138c2ecf20Sopenharmony_cistruct max8973_chip {
1148c2ecf20Sopenharmony_ci	struct device *dev;
1158c2ecf20Sopenharmony_ci	struct regulator_desc desc;
1168c2ecf20Sopenharmony_ci	struct regmap *regmap;
1178c2ecf20Sopenharmony_ci	bool enable_external_control;
1188c2ecf20Sopenharmony_ci	int dvs_gpio;
1198c2ecf20Sopenharmony_ci	int lru_index[MAX8973_MAX_VOUT_REG];
1208c2ecf20Sopenharmony_ci	int curr_vout_val[MAX8973_MAX_VOUT_REG];
1218c2ecf20Sopenharmony_ci	int curr_vout_reg;
1228c2ecf20Sopenharmony_ci	int curr_gpio_val;
1238c2ecf20Sopenharmony_ci	struct regulator_ops ops;
1248c2ecf20Sopenharmony_ci	enum device_id id;
1258c2ecf20Sopenharmony_ci	int junction_temp_warning;
1268c2ecf20Sopenharmony_ci	int irq;
1278c2ecf20Sopenharmony_ci	struct thermal_zone_device *tz_device;
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/*
1318c2ecf20Sopenharmony_ci * find_voltage_set_register: Find new voltage configuration register (VOUT).
1328c2ecf20Sopenharmony_ci * The finding of the new VOUT register will be based on the LRU mechanism.
1338c2ecf20Sopenharmony_ci * Each VOUT register will have different voltage configured . This
1348c2ecf20Sopenharmony_ci * Function will look if any of the VOUT register have requested voltage set
1358c2ecf20Sopenharmony_ci * or not.
1368c2ecf20Sopenharmony_ci *     - If it is already there then it will make that register as most
1378c2ecf20Sopenharmony_ci *       recently used and return as found so that caller need not to set
1388c2ecf20Sopenharmony_ci *       the VOUT register but need to set the proper gpios to select this
1398c2ecf20Sopenharmony_ci *       VOUT register.
1408c2ecf20Sopenharmony_ci *     - If requested voltage is not found then it will use the least
1418c2ecf20Sopenharmony_ci *       recently mechanism to get new VOUT register for new configuration
1428c2ecf20Sopenharmony_ci *       and will return not_found so that caller need to set new VOUT
1438c2ecf20Sopenharmony_ci *       register and then gpios (both).
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_cistatic bool find_voltage_set_register(struct max8973_chip *tps,
1468c2ecf20Sopenharmony_ci		int req_vsel, int *vout_reg, int *gpio_val)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int i;
1498c2ecf20Sopenharmony_ci	bool found = false;
1508c2ecf20Sopenharmony_ci	int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
1518c2ecf20Sopenharmony_ci	int found_index = MAX8973_MAX_VOUT_REG - 1;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
1548c2ecf20Sopenharmony_ci		if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
1558c2ecf20Sopenharmony_ci			new_vout_reg = tps->lru_index[i];
1568c2ecf20Sopenharmony_ci			found_index = i;
1578c2ecf20Sopenharmony_ci			found = true;
1588c2ecf20Sopenharmony_ci			goto update_lru_index;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciupdate_lru_index:
1638c2ecf20Sopenharmony_ci	for (i = found_index; i > 0; i--)
1648c2ecf20Sopenharmony_ci		tps->lru_index[i] = tps->lru_index[i - 1];
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	tps->lru_index[0] = new_vout_reg;
1678c2ecf20Sopenharmony_ci	*gpio_val = new_vout_reg;
1688c2ecf20Sopenharmony_ci	*vout_reg = MAX8973_VOUT + new_vout_reg;
1698c2ecf20Sopenharmony_ci	return found;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
1758c2ecf20Sopenharmony_ci	unsigned int data;
1768c2ecf20Sopenharmony_ci	int ret;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
1798c2ecf20Sopenharmony_ci	if (ret < 0) {
1808c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d read failed, err = %d\n",
1818c2ecf20Sopenharmony_ci			max->curr_vout_reg, ret);
1828c2ecf20Sopenharmony_ci		return ret;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci	return data & MAX8973_VOUT_MASK;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
1888c2ecf20Sopenharmony_ci	     unsigned vsel)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
1918c2ecf20Sopenharmony_ci	int ret;
1928c2ecf20Sopenharmony_ci	bool found = false;
1938c2ecf20Sopenharmony_ci	int vout_reg = max->curr_vout_reg;
1948c2ecf20Sopenharmony_ci	int gpio_val = max->curr_gpio_val;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/*
1978c2ecf20Sopenharmony_ci	 * If gpios are available to select the VOUT register then least
1988c2ecf20Sopenharmony_ci	 * recently used register for new configuration.
1998c2ecf20Sopenharmony_ci	 */
2008c2ecf20Sopenharmony_ci	if (gpio_is_valid(max->dvs_gpio))
2018c2ecf20Sopenharmony_ci		found = find_voltage_set_register(max, vsel,
2028c2ecf20Sopenharmony_ci					&vout_reg, &gpio_val);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!found) {
2058c2ecf20Sopenharmony_ci		ret = regmap_update_bits(max->regmap, vout_reg,
2068c2ecf20Sopenharmony_ci					MAX8973_VOUT_MASK, vsel);
2078c2ecf20Sopenharmony_ci		if (ret < 0) {
2088c2ecf20Sopenharmony_ci			dev_err(max->dev, "register %d update failed, err %d\n",
2098c2ecf20Sopenharmony_ci				 vout_reg, ret);
2108c2ecf20Sopenharmony_ci			return ret;
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci		max->curr_vout_reg = vout_reg;
2138c2ecf20Sopenharmony_ci		max->curr_vout_val[gpio_val] = vsel;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Select proper VOUT register vio gpios */
2178c2ecf20Sopenharmony_ci	if (gpio_is_valid(max->dvs_gpio)) {
2188c2ecf20Sopenharmony_ci		gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
2198c2ecf20Sopenharmony_ci		max->curr_gpio_val = gpio_val;
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci	return 0;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
2278c2ecf20Sopenharmony_ci	int ret;
2288c2ecf20Sopenharmony_ci	int pwm;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* Enable force PWM mode in FAST mode only. */
2318c2ecf20Sopenharmony_ci	switch (mode) {
2328c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
2338c2ecf20Sopenharmony_ci		pwm = MAX8973_FPWM_EN_M;
2348c2ecf20Sopenharmony_ci		break;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
2378c2ecf20Sopenharmony_ci		pwm = 0;
2388c2ecf20Sopenharmony_ci		break;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	default:
2418c2ecf20Sopenharmony_ci		return -EINVAL;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
2458c2ecf20Sopenharmony_ci				MAX8973_FPWM_EN_M, pwm);
2468c2ecf20Sopenharmony_ci	if (ret < 0)
2478c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d update failed, err %d\n",
2488c2ecf20Sopenharmony_ci				MAX8973_CONTROL1, ret);
2498c2ecf20Sopenharmony_ci	return ret;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
2558c2ecf20Sopenharmony_ci	unsigned int data;
2568c2ecf20Sopenharmony_ci	int ret;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
2598c2ecf20Sopenharmony_ci	if (ret < 0) {
2608c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d read failed, err %d\n",
2618c2ecf20Sopenharmony_ci				MAX8973_CONTROL1, ret);
2628c2ecf20Sopenharmony_ci		return ret;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci	return (data & MAX8973_FPWM_EN_M) ?
2658c2ecf20Sopenharmony_ci		REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic int max8973_set_ramp_delay(struct regulator_dev *rdev,
2698c2ecf20Sopenharmony_ci		int ramp_delay)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
2728c2ecf20Sopenharmony_ci	unsigned int control;
2738c2ecf20Sopenharmony_ci	int ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* Set ramp delay */
2768c2ecf20Sopenharmony_ci	if (ramp_delay <= 12000)
2778c2ecf20Sopenharmony_ci		control = MAX8973_RAMP_12mV_PER_US;
2788c2ecf20Sopenharmony_ci	else if (ramp_delay <= 25000)
2798c2ecf20Sopenharmony_ci		control = MAX8973_RAMP_25mV_PER_US;
2808c2ecf20Sopenharmony_ci	else if (ramp_delay <= 50000)
2818c2ecf20Sopenharmony_ci		control = MAX8973_RAMP_50mV_PER_US;
2828c2ecf20Sopenharmony_ci	else if (ramp_delay <= 200000)
2838c2ecf20Sopenharmony_ci		control = MAX8973_RAMP_200mV_PER_US;
2848c2ecf20Sopenharmony_ci	else
2858c2ecf20Sopenharmony_ci		return -EINVAL;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
2888c2ecf20Sopenharmony_ci			MAX8973_RAMP_MASK, control);
2898c2ecf20Sopenharmony_ci	if (ret < 0)
2908c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d update failed, %d",
2918c2ecf20Sopenharmony_ci				MAX8973_CONTROL1, ret);
2928c2ecf20Sopenharmony_ci	return ret;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic int max8973_set_current_limit(struct regulator_dev *rdev,
2968c2ecf20Sopenharmony_ci		int min_ua, int max_ua)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
2998c2ecf20Sopenharmony_ci	unsigned int val;
3008c2ecf20Sopenharmony_ci	int ret;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (max_ua <= 9000000)
3038c2ecf20Sopenharmony_ci		val = MAX8973_CKKADV_TRIP_75mV_PER_US;
3048c2ecf20Sopenharmony_ci	else if (max_ua <= 12000000)
3058c2ecf20Sopenharmony_ci		val = MAX8973_CKKADV_TRIP_150mV_PER_US;
3068c2ecf20Sopenharmony_ci	else
3078c2ecf20Sopenharmony_ci		val = MAX8973_CKKADV_TRIP_DISABLE;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	ret = regmap_update_bits(max->regmap, MAX8973_CONTROL2,
3108c2ecf20Sopenharmony_ci			MAX8973_CKKADV_TRIP_MASK, val);
3118c2ecf20Sopenharmony_ci	if (ret < 0) {
3128c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d update failed: %d\n",
3138c2ecf20Sopenharmony_ci				MAX8973_CONTROL2, ret);
3148c2ecf20Sopenharmony_ci		return ret;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci	return 0;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int max8973_get_current_limit(struct regulator_dev *rdev)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct max8973_chip *max = rdev_get_drvdata(rdev);
3228c2ecf20Sopenharmony_ci	unsigned int control2;
3238c2ecf20Sopenharmony_ci	int ret;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	ret = regmap_read(max->regmap, MAX8973_CONTROL2, &control2);
3268c2ecf20Sopenharmony_ci	if (ret < 0) {
3278c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d read failed: %d\n",
3288c2ecf20Sopenharmony_ci				MAX8973_CONTROL2, ret);
3298c2ecf20Sopenharmony_ci		return ret;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci	switch (control2 & MAX8973_CKKADV_TRIP_MASK) {
3328c2ecf20Sopenharmony_ci	case MAX8973_CKKADV_TRIP_DISABLE:
3338c2ecf20Sopenharmony_ci		return 15000000;
3348c2ecf20Sopenharmony_ci	case MAX8973_CKKADV_TRIP_150mV_PER_US:
3358c2ecf20Sopenharmony_ci		return 12000000;
3368c2ecf20Sopenharmony_ci	case MAX8973_CKKADV_TRIP_75mV_PER_US:
3378c2ecf20Sopenharmony_ci		return 9000000;
3388c2ecf20Sopenharmony_ci	default:
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci	return 9000000;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic const struct regulator_ops max8973_dcdc_ops = {
3458c2ecf20Sopenharmony_ci	.get_voltage_sel	= max8973_dcdc_get_voltage_sel,
3468c2ecf20Sopenharmony_ci	.set_voltage_sel	= max8973_dcdc_set_voltage_sel,
3478c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
3488c2ecf20Sopenharmony_ci	.set_mode		= max8973_dcdc_set_mode,
3498c2ecf20Sopenharmony_ci	.get_mode		= max8973_dcdc_get_mode,
3508c2ecf20Sopenharmony_ci	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
3518c2ecf20Sopenharmony_ci	.set_ramp_delay		= max8973_set_ramp_delay,
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic int max8973_init_dcdc(struct max8973_chip *max,
3558c2ecf20Sopenharmony_ci			     struct max8973_regulator_platform_data *pdata)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	int ret;
3588c2ecf20Sopenharmony_ci	uint8_t	control1 = 0;
3598c2ecf20Sopenharmony_ci	uint8_t control2 = 0;
3608c2ecf20Sopenharmony_ci	unsigned int data;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
3638c2ecf20Sopenharmony_ci	if (ret < 0) {
3648c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d read failed, err = %d",
3658c2ecf20Sopenharmony_ci				MAX8973_CONTROL1, ret);
3668c2ecf20Sopenharmony_ci		return ret;
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	control1 = data & MAX8973_RAMP_MASK;
3698c2ecf20Sopenharmony_ci	switch (control1) {
3708c2ecf20Sopenharmony_ci	case MAX8973_RAMP_12mV_PER_US:
3718c2ecf20Sopenharmony_ci		max->desc.ramp_delay = 12000;
3728c2ecf20Sopenharmony_ci		break;
3738c2ecf20Sopenharmony_ci	case MAX8973_RAMP_25mV_PER_US:
3748c2ecf20Sopenharmony_ci		max->desc.ramp_delay = 25000;
3758c2ecf20Sopenharmony_ci		break;
3768c2ecf20Sopenharmony_ci	case MAX8973_RAMP_50mV_PER_US:
3778c2ecf20Sopenharmony_ci		max->desc.ramp_delay = 50000;
3788c2ecf20Sopenharmony_ci		break;
3798c2ecf20Sopenharmony_ci	case MAX8973_RAMP_200mV_PER_US:
3808c2ecf20Sopenharmony_ci		max->desc.ramp_delay = 200000;
3818c2ecf20Sopenharmony_ci		break;
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
3858c2ecf20Sopenharmony_ci		control1 |= MAX8973_SNS_ENABLE;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
3888c2ecf20Sopenharmony_ci		control1 |= MAX8973_NFSR_ENABLE;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
3918c2ecf20Sopenharmony_ci		control1 |= MAX8973_AD_ENABLE;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE) {
3948c2ecf20Sopenharmony_ci		control1 |= MAX8973_BIAS_ENABLE;
3958c2ecf20Sopenharmony_ci		max->desc.enable_time = 20;
3968c2ecf20Sopenharmony_ci	} else {
3978c2ecf20Sopenharmony_ci		max->desc.enable_time = 240;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
4018c2ecf20Sopenharmony_ci		control1 |= MAX8973_FREQSHIFT_9PER;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if ((pdata->junction_temp_warning == MAX77621_TJINT_WARNING_TEMP_120) &&
4048c2ecf20Sopenharmony_ci	    (max->id == MAX77621))
4058c2ecf20Sopenharmony_ci		control2 |= MAX77621_T_JUNCTION_120;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
4088c2ecf20Sopenharmony_ci		control2 |= MAX8973_DISCH_ENBABLE;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/*  Clock advance trip configuration */
4118c2ecf20Sopenharmony_ci	switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
4128c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
4138c2ecf20Sopenharmony_ci		control2 |= MAX8973_CKKADV_TRIP_DISABLE;
4148c2ecf20Sopenharmony_ci		break;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
4178c2ecf20Sopenharmony_ci		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
4188c2ecf20Sopenharmony_ci		break;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
4218c2ecf20Sopenharmony_ci		control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
4228c2ecf20Sopenharmony_ci		break;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
4258c2ecf20Sopenharmony_ci		control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
4268c2ecf20Sopenharmony_ci		break;
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	/* Configure inductor value */
4308c2ecf20Sopenharmony_ci	switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
4318c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
4328c2ecf20Sopenharmony_ci		control2 |= MAX8973_INDUCTOR_NOMINAL;
4338c2ecf20Sopenharmony_ci		break;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
4368c2ecf20Sopenharmony_ci		control2 |= MAX8973_INDUCTOR_MIN_30_PER;
4378c2ecf20Sopenharmony_ci		break;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
4408c2ecf20Sopenharmony_ci		control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
4418c2ecf20Sopenharmony_ci		break;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
4448c2ecf20Sopenharmony_ci		control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
4458c2ecf20Sopenharmony_ci		break;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
4498c2ecf20Sopenharmony_ci	if (ret < 0) {
4508c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d write failed, err = %d",
4518c2ecf20Sopenharmony_ci				MAX8973_CONTROL1, ret);
4528c2ecf20Sopenharmony_ci		return ret;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
4568c2ecf20Sopenharmony_ci	if (ret < 0) {
4578c2ecf20Sopenharmony_ci		dev_err(max->dev, "register %d write failed, err = %d",
4588c2ecf20Sopenharmony_ci				MAX8973_CONTROL2, ret);
4598c2ecf20Sopenharmony_ci		return ret;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	/* If external control is enabled then disable EN bit */
4638c2ecf20Sopenharmony_ci	if (max->enable_external_control && (max->id == MAX8973)) {
4648c2ecf20Sopenharmony_ci		ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
4658c2ecf20Sopenharmony_ci						MAX8973_VOUT_ENABLE, 0);
4668c2ecf20Sopenharmony_ci		if (ret < 0)
4678c2ecf20Sopenharmony_ci			dev_err(max->dev, "register %d update failed, err = %d",
4688c2ecf20Sopenharmony_ci				MAX8973_VOUT, ret);
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci	return ret;
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic int max8973_thermal_read_temp(void *data, int *temp)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct max8973_chip *mchip = data;
4768c2ecf20Sopenharmony_ci	unsigned int val;
4778c2ecf20Sopenharmony_ci	int ret;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	ret = regmap_read(mchip->regmap, MAX8973_CHIPID1, &val);
4808c2ecf20Sopenharmony_ci	if (ret < 0) {
4818c2ecf20Sopenharmony_ci		dev_err(mchip->dev, "Failed to read register CHIPID1, %d", ret);
4828c2ecf20Sopenharmony_ci		return ret;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/* +1 degC to trigger cool devive */
4868c2ecf20Sopenharmony_ci	if (val & MAX77621_CHIPID_TJINT_S)
4878c2ecf20Sopenharmony_ci		*temp = mchip->junction_temp_warning + 1000;
4888c2ecf20Sopenharmony_ci	else
4898c2ecf20Sopenharmony_ci		*temp = MAX77621_NORMAL_OPERATING_TEMP;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	return 0;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic irqreturn_t max8973_thermal_irq(int irq, void *data)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct max8973_chip *mchip = data;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	thermal_zone_device_update(mchip->tz_device,
4998c2ecf20Sopenharmony_ci				   THERMAL_EVENT_UNSPECIFIED);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic const struct thermal_zone_of_device_ops max77621_tz_ops = {
5058c2ecf20Sopenharmony_ci	.get_temp = max8973_thermal_read_temp,
5068c2ecf20Sopenharmony_ci};
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic int max8973_thermal_init(struct max8973_chip *mchip)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	struct thermal_zone_device *tzd;
5118c2ecf20Sopenharmony_ci	struct irq_data *irq_data;
5128c2ecf20Sopenharmony_ci	unsigned long irq_flags = 0;
5138c2ecf20Sopenharmony_ci	int ret;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	if (mchip->id != MAX77621)
5168c2ecf20Sopenharmony_ci		return 0;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	tzd = devm_thermal_zone_of_sensor_register(mchip->dev, 0, mchip,
5198c2ecf20Sopenharmony_ci						   &max77621_tz_ops);
5208c2ecf20Sopenharmony_ci	if (IS_ERR(tzd)) {
5218c2ecf20Sopenharmony_ci		ret = PTR_ERR(tzd);
5228c2ecf20Sopenharmony_ci		dev_err(mchip->dev, "Failed to register thermal sensor: %d\n",
5238c2ecf20Sopenharmony_ci			ret);
5248c2ecf20Sopenharmony_ci		return ret;
5258c2ecf20Sopenharmony_ci	}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (mchip->irq <= 0)
5288c2ecf20Sopenharmony_ci		return 0;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	irq_data = irq_get_irq_data(mchip->irq);
5318c2ecf20Sopenharmony_ci	if (irq_data)
5328c2ecf20Sopenharmony_ci		irq_flags = irqd_get_trigger_type(irq_data);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(mchip->dev, mchip->irq, NULL,
5358c2ecf20Sopenharmony_ci					max8973_thermal_irq,
5368c2ecf20Sopenharmony_ci					IRQF_ONESHOT | IRQF_SHARED | irq_flags,
5378c2ecf20Sopenharmony_ci					dev_name(mchip->dev), mchip);
5388c2ecf20Sopenharmony_ci	if (ret < 0) {
5398c2ecf20Sopenharmony_ci		dev_err(mchip->dev, "Failed to request irq %d, %d\n",
5408c2ecf20Sopenharmony_ci			mchip->irq, ret);
5418c2ecf20Sopenharmony_ci		return ret;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	return 0;
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic const struct regmap_config max8973_regmap_config = {
5488c2ecf20Sopenharmony_ci	.reg_bits		= 8,
5498c2ecf20Sopenharmony_ci	.val_bits		= 8,
5508c2ecf20Sopenharmony_ci	.max_register		= MAX8973_CHIPID2,
5518c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
5528c2ecf20Sopenharmony_ci};
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistatic struct max8973_regulator_platform_data *max8973_parse_dt(
5558c2ecf20Sopenharmony_ci		struct device *dev)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct max8973_regulator_platform_data *pdata;
5588c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
5598c2ecf20Sopenharmony_ci	int ret;
5608c2ecf20Sopenharmony_ci	u32 pval;
5618c2ecf20Sopenharmony_ci	bool etr_enable;
5628c2ecf20Sopenharmony_ci	bool etr_sensitivity_high;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
5658c2ecf20Sopenharmony_ci	if (!pdata)
5668c2ecf20Sopenharmony_ci		return NULL;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	pdata->enable_ext_control = of_property_read_bool(np,
5698c2ecf20Sopenharmony_ci						"maxim,externally-enable");
5708c2ecf20Sopenharmony_ci	pdata->dvs_gpio = of_get_named_gpio(np, "maxim,dvs-gpio", 0);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "maxim,dvs-default-state", &pval);
5738c2ecf20Sopenharmony_ci	if (!ret)
5748c2ecf20Sopenharmony_ci		pdata->dvs_def_state = pval;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "maxim,enable-remote-sense"))
5778c2ecf20Sopenharmony_ci		pdata->control_flags  |= MAX8973_CONTROL_REMOTE_SENSE_ENABLE;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "maxim,enable-falling-slew-rate"))
5808c2ecf20Sopenharmony_ci		pdata->control_flags  |=
5818c2ecf20Sopenharmony_ci				MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "maxim,enable-active-discharge"))
5848c2ecf20Sopenharmony_ci		pdata->control_flags  |=
5858c2ecf20Sopenharmony_ci				MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "maxim,enable-frequency-shift"))
5888c2ecf20Sopenharmony_ci		pdata->control_flags  |= MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "maxim,enable-bias-control"))
5918c2ecf20Sopenharmony_ci		pdata->control_flags  |= MAX8973_CONTROL_BIAS_ENABLE;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	etr_enable = of_property_read_bool(np, "maxim,enable-etr");
5948c2ecf20Sopenharmony_ci	etr_sensitivity_high = of_property_read_bool(np,
5958c2ecf20Sopenharmony_ci				"maxim,enable-high-etr-sensitivity");
5968c2ecf20Sopenharmony_ci	if (etr_sensitivity_high)
5978c2ecf20Sopenharmony_ci		etr_enable = true;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	if (etr_enable) {
6008c2ecf20Sopenharmony_ci		if (etr_sensitivity_high)
6018c2ecf20Sopenharmony_ci			pdata->control_flags |=
6028c2ecf20Sopenharmony_ci				MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US;
6038c2ecf20Sopenharmony_ci		else
6048c2ecf20Sopenharmony_ci			pdata->control_flags |=
6058c2ecf20Sopenharmony_ci				MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US;
6068c2ecf20Sopenharmony_ci	} else {
6078c2ecf20Sopenharmony_ci		pdata->control_flags |= MAX8973_CONTROL_CLKADV_TRIP_DISABLED;
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_140;
6118c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "junction-warn-millicelsius", &pval);
6128c2ecf20Sopenharmony_ci	if (!ret && (pval <= MAX77621_TJINT_WARNING_TEMP_120))
6138c2ecf20Sopenharmony_ci		pdata->junction_temp_warning = MAX77621_TJINT_WARNING_TEMP_120;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	return pdata;
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_cistatic const struct of_device_id of_max8973_match_tbl[] = {
6198c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max8973", .data = (void *)MAX8973, },
6208c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max77621", .data = (void *)MAX77621, },
6218c2ecf20Sopenharmony_ci	{ },
6228c2ecf20Sopenharmony_ci};
6238c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_max8973_match_tbl);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic int max8973_probe(struct i2c_client *client,
6268c2ecf20Sopenharmony_ci			 const struct i2c_device_id *id)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct max8973_regulator_platform_data *pdata;
6298c2ecf20Sopenharmony_ci	struct regulator_init_data *ridata;
6308c2ecf20Sopenharmony_ci	struct regulator_config config = { };
6318c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
6328c2ecf20Sopenharmony_ci	struct max8973_chip *max;
6338c2ecf20Sopenharmony_ci	bool pdata_from_dt = false;
6348c2ecf20Sopenharmony_ci	unsigned int chip_id;
6358c2ecf20Sopenharmony_ci	struct gpio_desc *gpiod;
6368c2ecf20Sopenharmony_ci	enum gpiod_flags gflags;
6378c2ecf20Sopenharmony_ci	int ret;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	pdata = dev_get_platdata(&client->dev);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (!pdata && client->dev.of_node) {
6428c2ecf20Sopenharmony_ci		pdata = max8973_parse_dt(&client->dev);
6438c2ecf20Sopenharmony_ci		pdata_from_dt = true;
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (!pdata) {
6478c2ecf20Sopenharmony_ci		dev_err(&client->dev, "No Platform data");
6488c2ecf20Sopenharmony_ci		return -EIO;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	if (pdata->dvs_gpio == -EPROBE_DEFER)
6528c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
6558c2ecf20Sopenharmony_ci	if (!max)
6568c2ecf20Sopenharmony_ci		return -ENOMEM;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
6598c2ecf20Sopenharmony_ci	if (IS_ERR(max->regmap)) {
6608c2ecf20Sopenharmony_ci		ret = PTR_ERR(max->regmap);
6618c2ecf20Sopenharmony_ci		dev_err(&client->dev, "regmap init failed, err %d\n", ret);
6628c2ecf20Sopenharmony_ci		return ret;
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	if (client->dev.of_node) {
6668c2ecf20Sopenharmony_ci		const struct of_device_id *match;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci		match = of_match_device(of_match_ptr(of_max8973_match_tbl),
6698c2ecf20Sopenharmony_ci				&client->dev);
6708c2ecf20Sopenharmony_ci		if (!match)
6718c2ecf20Sopenharmony_ci			return -ENODATA;
6728c2ecf20Sopenharmony_ci		max->id = (u32)((uintptr_t)match->data);
6738c2ecf20Sopenharmony_ci	} else {
6748c2ecf20Sopenharmony_ci		max->id = id->driver_data;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	ret = regmap_read(max->regmap, MAX8973_CHIPID1, &chip_id);
6788c2ecf20Sopenharmony_ci	if (ret < 0) {
6798c2ecf20Sopenharmony_ci		dev_err(&client->dev, "register CHIPID1 read failed, %d", ret);
6808c2ecf20Sopenharmony_ci		return ret;
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	dev_info(&client->dev, "CHIP-ID OTP: 0x%02x ID_M: 0x%02x\n",
6848c2ecf20Sopenharmony_ci			(chip_id >> 4) & 0xF, (chip_id >> 1) & 0x7);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, max);
6878c2ecf20Sopenharmony_ci	max->ops = max8973_dcdc_ops;
6888c2ecf20Sopenharmony_ci	max->dev = &client->dev;
6898c2ecf20Sopenharmony_ci	max->desc.name = id->name;
6908c2ecf20Sopenharmony_ci	max->desc.id = 0;
6918c2ecf20Sopenharmony_ci	max->desc.ops = &max->ops;
6928c2ecf20Sopenharmony_ci	max->desc.type = REGULATOR_VOLTAGE;
6938c2ecf20Sopenharmony_ci	max->desc.owner = THIS_MODULE;
6948c2ecf20Sopenharmony_ci	max->desc.min_uV = MAX8973_MIN_VOLATGE;
6958c2ecf20Sopenharmony_ci	max->desc.uV_step = MAX8973_VOLATGE_STEP;
6968c2ecf20Sopenharmony_ci	max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	max->dvs_gpio = (pdata->dvs_gpio) ? pdata->dvs_gpio : -EINVAL;
6998c2ecf20Sopenharmony_ci	max->enable_external_control = pdata->enable_ext_control;
7008c2ecf20Sopenharmony_ci	max->curr_gpio_val = pdata->dvs_def_state;
7018c2ecf20Sopenharmony_ci	max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
7028c2ecf20Sopenharmony_ci	max->junction_temp_warning = pdata->junction_temp_warning;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	max->lru_index[0] = max->curr_vout_reg;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	if (gpio_is_valid(max->dvs_gpio)) {
7078c2ecf20Sopenharmony_ci		int gpio_flags;
7088c2ecf20Sopenharmony_ci		int i;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		gpio_flags = (pdata->dvs_def_state) ?
7118c2ecf20Sopenharmony_ci				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
7128c2ecf20Sopenharmony_ci		ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
7138c2ecf20Sopenharmony_ci				gpio_flags, "max8973-dvs");
7148c2ecf20Sopenharmony_ci		if (ret) {
7158c2ecf20Sopenharmony_ci			dev_err(&client->dev,
7168c2ecf20Sopenharmony_ci				"gpio_request for gpio %d failed, err = %d\n",
7178c2ecf20Sopenharmony_ci				max->dvs_gpio, ret);
7188c2ecf20Sopenharmony_ci			return ret;
7198c2ecf20Sopenharmony_ci		}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		/*
7228c2ecf20Sopenharmony_ci		 * Initialize the lru index with vout_reg id
7238c2ecf20Sopenharmony_ci		 * The index 0 will be most recently used and
7248c2ecf20Sopenharmony_ci		 * set with the max->curr_vout_reg */
7258c2ecf20Sopenharmony_ci		for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
7268c2ecf20Sopenharmony_ci			max->lru_index[i] = i;
7278c2ecf20Sopenharmony_ci		max->lru_index[0] = max->curr_vout_reg;
7288c2ecf20Sopenharmony_ci		max->lru_index[max->curr_vout_reg] = 0;
7298c2ecf20Sopenharmony_ci	} else {
7308c2ecf20Sopenharmony_ci		/*
7318c2ecf20Sopenharmony_ci		 * If there is no DVS GPIO, the VOUT register
7328c2ecf20Sopenharmony_ci		 * address is fixed.
7338c2ecf20Sopenharmony_ci		 */
7348c2ecf20Sopenharmony_ci		max->ops.set_voltage_sel = regulator_set_voltage_sel_regmap;
7358c2ecf20Sopenharmony_ci		max->ops.get_voltage_sel = regulator_get_voltage_sel_regmap;
7368c2ecf20Sopenharmony_ci		max->desc.vsel_reg = max->curr_vout_reg;
7378c2ecf20Sopenharmony_ci		max->desc.vsel_mask = MAX8973_VOUT_MASK;
7388c2ecf20Sopenharmony_ci	}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (pdata_from_dt)
7418c2ecf20Sopenharmony_ci		pdata->reg_init_data = of_get_regulator_init_data(&client->dev,
7428c2ecf20Sopenharmony_ci					client->dev.of_node, &max->desc);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	ridata = pdata->reg_init_data;
7458c2ecf20Sopenharmony_ci	switch (max->id) {
7468c2ecf20Sopenharmony_ci	case MAX8973:
7478c2ecf20Sopenharmony_ci		if (!pdata->enable_ext_control) {
7488c2ecf20Sopenharmony_ci			max->desc.enable_reg = MAX8973_VOUT;
7498c2ecf20Sopenharmony_ci			max->desc.enable_mask = MAX8973_VOUT_ENABLE;
7508c2ecf20Sopenharmony_ci			max->ops.enable = regulator_enable_regmap;
7518c2ecf20Sopenharmony_ci			max->ops.disable = regulator_disable_regmap;
7528c2ecf20Sopenharmony_ci			max->ops.is_enabled = regulator_is_enabled_regmap;
7538c2ecf20Sopenharmony_ci			break;
7548c2ecf20Sopenharmony_ci		}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		if (ridata && (ridata->constraints.always_on ||
7578c2ecf20Sopenharmony_ci			       ridata->constraints.boot_on))
7588c2ecf20Sopenharmony_ci			gflags = GPIOD_OUT_HIGH;
7598c2ecf20Sopenharmony_ci		else
7608c2ecf20Sopenharmony_ci			gflags = GPIOD_OUT_LOW;
7618c2ecf20Sopenharmony_ci		gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
7628c2ecf20Sopenharmony_ci		gpiod = devm_gpiod_get_optional(&client->dev,
7638c2ecf20Sopenharmony_ci						"maxim,enable",
7648c2ecf20Sopenharmony_ci						gflags);
7658c2ecf20Sopenharmony_ci		if (IS_ERR(gpiod))
7668c2ecf20Sopenharmony_ci			return PTR_ERR(gpiod);
7678c2ecf20Sopenharmony_ci		if (gpiod) {
7688c2ecf20Sopenharmony_ci			config.ena_gpiod = gpiod;
7698c2ecf20Sopenharmony_ci			max->enable_external_control = true;
7708c2ecf20Sopenharmony_ci		}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci		break;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	case MAX77621:
7758c2ecf20Sopenharmony_ci		/*
7768c2ecf20Sopenharmony_ci		 * We do not let the core switch this regulator on/off,
7778c2ecf20Sopenharmony_ci		 * we just leave it on.
7788c2ecf20Sopenharmony_ci		 */
7798c2ecf20Sopenharmony_ci		gpiod = devm_gpiod_get_optional(&client->dev,
7808c2ecf20Sopenharmony_ci						"maxim,enable",
7818c2ecf20Sopenharmony_ci						GPIOD_OUT_HIGH);
7828c2ecf20Sopenharmony_ci		if (IS_ERR(gpiod))
7838c2ecf20Sopenharmony_ci			return PTR_ERR(gpiod);
7848c2ecf20Sopenharmony_ci		if (gpiod)
7858c2ecf20Sopenharmony_ci			max->enable_external_control = true;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci		max->desc.enable_reg = MAX8973_VOUT;
7888c2ecf20Sopenharmony_ci		max->desc.enable_mask = MAX8973_VOUT_ENABLE;
7898c2ecf20Sopenharmony_ci		max->ops.enable = regulator_enable_regmap;
7908c2ecf20Sopenharmony_ci		max->ops.disable = regulator_disable_regmap;
7918c2ecf20Sopenharmony_ci		max->ops.is_enabled = regulator_is_enabled_regmap;
7928c2ecf20Sopenharmony_ci		max->ops.set_current_limit = max8973_set_current_limit;
7938c2ecf20Sopenharmony_ci		max->ops.get_current_limit = max8973_get_current_limit;
7948c2ecf20Sopenharmony_ci		break;
7958c2ecf20Sopenharmony_ci	default:
7968c2ecf20Sopenharmony_ci		break;
7978c2ecf20Sopenharmony_ci	}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	ret = max8973_init_dcdc(max, pdata);
8008c2ecf20Sopenharmony_ci	if (ret < 0) {
8018c2ecf20Sopenharmony_ci		dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
8028c2ecf20Sopenharmony_ci		return ret;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	config.dev = &client->dev;
8068c2ecf20Sopenharmony_ci	config.init_data = pdata->reg_init_data;
8078c2ecf20Sopenharmony_ci	config.driver_data = max;
8088c2ecf20Sopenharmony_ci	config.of_node = client->dev.of_node;
8098c2ecf20Sopenharmony_ci	config.regmap = max->regmap;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	/*
8128c2ecf20Sopenharmony_ci	 * Register the regulators
8138c2ecf20Sopenharmony_ci	 * Turn the GPIO descriptor over to the regulator core for
8148c2ecf20Sopenharmony_ci	 * lifecycle management if we pass an ena_gpiod.
8158c2ecf20Sopenharmony_ci	 */
8168c2ecf20Sopenharmony_ci	if (config.ena_gpiod)
8178c2ecf20Sopenharmony_ci		devm_gpiod_unhinge(&client->dev, config.ena_gpiod);
8188c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(&client->dev, &max->desc, &config);
8198c2ecf20Sopenharmony_ci	if (IS_ERR(rdev)) {
8208c2ecf20Sopenharmony_ci		ret = PTR_ERR(rdev);
8218c2ecf20Sopenharmony_ci		dev_err(max->dev, "regulator register failed, err %d\n", ret);
8228c2ecf20Sopenharmony_ci		return ret;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	max8973_thermal_init(max);
8268c2ecf20Sopenharmony_ci	return 0;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic const struct i2c_device_id max8973_id[] = {
8308c2ecf20Sopenharmony_ci	{.name = "max8973", .driver_data = MAX8973},
8318c2ecf20Sopenharmony_ci	{.name = "max77621", .driver_data = MAX77621},
8328c2ecf20Sopenharmony_ci	{},
8338c2ecf20Sopenharmony_ci};
8348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max8973_id);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic struct i2c_driver max8973_i2c_driver = {
8378c2ecf20Sopenharmony_ci	.driver = {
8388c2ecf20Sopenharmony_ci		.name = "max8973",
8398c2ecf20Sopenharmony_ci		.of_match_table = of_max8973_match_tbl,
8408c2ecf20Sopenharmony_ci	},
8418c2ecf20Sopenharmony_ci	.probe = max8973_probe,
8428c2ecf20Sopenharmony_ci	.id_table = max8973_id,
8438c2ecf20Sopenharmony_ci};
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic int __init max8973_init(void)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	return i2c_add_driver(&max8973_i2c_driver);
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_cisubsys_initcall(max8973_init);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic void __exit max8973_cleanup(void)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	i2c_del_driver(&max8973_i2c_driver);
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_cimodule_exit(max8973_cleanup);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
8588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAX8973 voltage regulator driver");
8598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
860