xref: /linux/drivers/regulator/tps62360-regulator.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1e634cf4eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26219929fSLaxman Dewangan /*
36219929fSLaxman Dewangan  * tps62360.c -- TI tps62360
46219929fSLaxman Dewangan  *
5d1cf4f65SAxel Lin  * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
66219929fSLaxman Dewangan  *
76219929fSLaxman Dewangan  * Copyright (c) 2012, NVIDIA Corporation.
86219929fSLaxman Dewangan  *
96219929fSLaxman Dewangan  * Author: Laxman Dewangan <ldewangan@nvidia.com>
106219929fSLaxman Dewangan  */
116219929fSLaxman Dewangan 
126219929fSLaxman Dewangan #include <linux/kernel.h>
136219929fSLaxman Dewangan #include <linux/module.h>
146219929fSLaxman Dewangan #include <linux/init.h>
156219929fSLaxman Dewangan #include <linux/err.h>
16684ae39bSLaxman Dewangan #include <linux/of.h>
17684ae39bSLaxman Dewangan #include <linux/of_device.h>
18684ae39bSLaxman Dewangan #include <linux/regulator/of_regulator.h>
196219929fSLaxman Dewangan #include <linux/platform_device.h>
206219929fSLaxman Dewangan #include <linux/regulator/driver.h>
216219929fSLaxman Dewangan #include <linux/regulator/machine.h>
226219929fSLaxman Dewangan #include <linux/regulator/tps62360.h>
236a8b5bb0SMaíra Canal #include <linux/gpio/consumer.h>
246219929fSLaxman Dewangan #include <linux/i2c.h>
256219929fSLaxman Dewangan #include <linux/slab.h>
266219929fSLaxman Dewangan #include <linux/regmap.h>
276219929fSLaxman Dewangan 
286219929fSLaxman Dewangan /* Register definitions */
296219929fSLaxman Dewangan #define REG_VSET0		0
306219929fSLaxman Dewangan #define REG_VSET1		1
316219929fSLaxman Dewangan #define REG_VSET2		2
326219929fSLaxman Dewangan #define REG_VSET3		3
336219929fSLaxman Dewangan #define REG_CONTROL		4
346219929fSLaxman Dewangan #define REG_TEMP		5
356219929fSLaxman Dewangan #define REG_RAMPCTRL		6
366219929fSLaxman Dewangan #define REG_CHIPID		8
376219929fSLaxman Dewangan 
389a00630cSLaxman Dewangan #define FORCE_PWM_ENABLE	BIT(7)
399a00630cSLaxman Dewangan 
40d1cf4f65SAxel Lin enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
416219929fSLaxman Dewangan 
422935fb18SLaxman Dewangan #define TPS62360_BASE_VOLTAGE	770000
436219929fSLaxman Dewangan #define TPS62360_N_VOLTAGES	64
446219929fSLaxman Dewangan 
452935fb18SLaxman Dewangan #define TPS62361_BASE_VOLTAGE	500000
466219929fSLaxman Dewangan #define TPS62361_N_VOLTAGES	128
476219929fSLaxman Dewangan 
486219929fSLaxman Dewangan /* tps 62360 chip information */
496219929fSLaxman Dewangan struct tps62360_chip {
506219929fSLaxman Dewangan 	struct device *dev;
516219929fSLaxman Dewangan 	struct regulator_desc desc;
526219929fSLaxman Dewangan 	struct regulator_dev *rdev;
536219929fSLaxman Dewangan 	struct regmap *regmap;
546a8b5bb0SMaíra Canal 	struct gpio_desc *vsel0_gpio;
556a8b5bb0SMaíra Canal 	struct gpio_desc *vsel1_gpio;
566219929fSLaxman Dewangan 	u8 voltage_reg_mask;
576219929fSLaxman Dewangan 	bool en_internal_pulldn;
586219929fSLaxman Dewangan 	bool en_discharge;
596219929fSLaxman Dewangan 	bool valid_gpios;
606219929fSLaxman Dewangan 	int lru_index[4];
616219929fSLaxman Dewangan 	int curr_vset_vsel[4];
626219929fSLaxman Dewangan 	int curr_vset_id;
636219929fSLaxman Dewangan };
646219929fSLaxman Dewangan 
656219929fSLaxman Dewangan /*
666219929fSLaxman Dewangan  * find_voltage_set_register: Find new voltage configuration register
676219929fSLaxman Dewangan  * (VSET) id.
686219929fSLaxman Dewangan  * The finding of the new VSET register will be based on the LRU mechanism.
696219929fSLaxman Dewangan  * Each VSET register will have different voltage configured . This
706219929fSLaxman Dewangan  * Function will look if any of the VSET register have requested voltage set
716219929fSLaxman Dewangan  * or not.
726219929fSLaxman Dewangan  *     - If it is already there then it will make that register as most
736219929fSLaxman Dewangan  *       recently used and return as found so that caller need not to set
746219929fSLaxman Dewangan  *       the VSET register but need to set the proper gpios to select this
756219929fSLaxman Dewangan  *       VSET register.
766219929fSLaxman Dewangan  *     - If requested voltage is not found then it will use the least
776219929fSLaxman Dewangan  *       recently mechanism to get new VSET register for new configuration
786219929fSLaxman Dewangan  *       and will return not_found so that caller need to set new VSET
796219929fSLaxman Dewangan  *       register and then gpios (both).
806219929fSLaxman Dewangan  */
find_voltage_set_register(struct tps62360_chip * tps,int req_vsel,int * vset_reg_id)816219929fSLaxman Dewangan static bool find_voltage_set_register(struct tps62360_chip *tps,
826219929fSLaxman Dewangan 		int req_vsel, int *vset_reg_id)
836219929fSLaxman Dewangan {
846219929fSLaxman Dewangan 	int i;
856219929fSLaxman Dewangan 	bool found = false;
866219929fSLaxman Dewangan 	int new_vset_reg = tps->lru_index[3];
876219929fSLaxman Dewangan 	int found_index = 3;
882935fb18SLaxman Dewangan 
896219929fSLaxman Dewangan 	for (i = 0; i < 4; ++i) {
906219929fSLaxman Dewangan 		if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
916219929fSLaxman Dewangan 			new_vset_reg = tps->lru_index[i];
926219929fSLaxman Dewangan 			found_index = i;
936219929fSLaxman Dewangan 			found = true;
946219929fSLaxman Dewangan 			goto update_lru_index;
956219929fSLaxman Dewangan 		}
966219929fSLaxman Dewangan 	}
976219929fSLaxman Dewangan 
986219929fSLaxman Dewangan update_lru_index:
996219929fSLaxman Dewangan 	for (i = found_index; i > 0; i--)
1006219929fSLaxman Dewangan 		tps->lru_index[i] = tps->lru_index[i - 1];
1016219929fSLaxman Dewangan 
1026219929fSLaxman Dewangan 	tps->lru_index[0] = new_vset_reg;
1036219929fSLaxman Dewangan 	*vset_reg_id = new_vset_reg;
1046219929fSLaxman Dewangan 	return found;
1056219929fSLaxman Dewangan }
1066219929fSLaxman Dewangan 
tps62360_dcdc_get_voltage_sel(struct regulator_dev * dev)107a60cfce0SLaxman Dewangan static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
1086219929fSLaxman Dewangan {
1096219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1106219929fSLaxman Dewangan 	int vsel;
1116219929fSLaxman Dewangan 	unsigned int data;
1126219929fSLaxman Dewangan 	int ret;
1136219929fSLaxman Dewangan 
1146219929fSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
1156219929fSLaxman Dewangan 	if (ret < 0) {
1162935fb18SLaxman Dewangan 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
1172935fb18SLaxman Dewangan 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
1186219929fSLaxman Dewangan 		return ret;
1196219929fSLaxman Dewangan 	}
1206219929fSLaxman Dewangan 	vsel = (int)data & tps->voltage_reg_mask;
121a60cfce0SLaxman Dewangan 	return vsel;
1226219929fSLaxman Dewangan }
1236219929fSLaxman Dewangan 
tps62360_dcdc_set_voltage_sel(struct regulator_dev * dev,unsigned selector)12441097afdSAxel Lin static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
12541097afdSAxel Lin 					 unsigned selector)
1266219929fSLaxman Dewangan {
1276219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1286219929fSLaxman Dewangan 	int ret;
1296219929fSLaxman Dewangan 	bool found = false;
1306219929fSLaxman Dewangan 	int new_vset_id = tps->curr_vset_id;
1316219929fSLaxman Dewangan 
1326219929fSLaxman Dewangan 	/*
1336219929fSLaxman Dewangan 	 * If gpios are available to select the VSET register then least
1346219929fSLaxman Dewangan 	 * recently used register for new configuration.
1356219929fSLaxman Dewangan 	 */
1366219929fSLaxman Dewangan 	if (tps->valid_gpios)
13741097afdSAxel Lin 		found = find_voltage_set_register(tps, selector, &new_vset_id);
1386219929fSLaxman Dewangan 
1396219929fSLaxman Dewangan 	if (!found) {
1406219929fSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
14141097afdSAxel Lin 				tps->voltage_reg_mask, selector);
1426219929fSLaxman Dewangan 		if (ret < 0) {
1432935fb18SLaxman Dewangan 			dev_err(tps->dev,
1442935fb18SLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
1452935fb18SLaxman Dewangan 				 __func__, REG_VSET0 + new_vset_id, ret);
1466219929fSLaxman Dewangan 			return ret;
1476219929fSLaxman Dewangan 		}
1486219929fSLaxman Dewangan 		tps->curr_vset_id = new_vset_id;
14941097afdSAxel Lin 		tps->curr_vset_vsel[new_vset_id] = selector;
1506219929fSLaxman Dewangan 	}
1516219929fSLaxman Dewangan 
1526219929fSLaxman Dewangan 	/* Select proper VSET register vio gpios */
1536219929fSLaxman Dewangan 	if (tps->valid_gpios) {
1546a8b5bb0SMaíra Canal 		gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
1556a8b5bb0SMaíra Canal 		gpiod_set_value_cansleep(tps->vsel1_gpio,
1566219929fSLaxman Dewangan 					(new_vset_id >> 1) & 0x1);
1576219929fSLaxman Dewangan 	}
1586219929fSLaxman Dewangan 	return 0;
1596219929fSLaxman Dewangan }
1606219929fSLaxman Dewangan 
tps62360_set_mode(struct regulator_dev * rdev,unsigned int mode)1619a00630cSLaxman Dewangan static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
1629a00630cSLaxman Dewangan {
1639a00630cSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
1649a00630cSLaxman Dewangan 	int i;
1659a00630cSLaxman Dewangan 	int val;
1669a00630cSLaxman Dewangan 	int ret;
1679a00630cSLaxman Dewangan 
1689a00630cSLaxman Dewangan 	/* Enable force PWM mode in FAST mode only. */
1699a00630cSLaxman Dewangan 	switch (mode) {
1709a00630cSLaxman Dewangan 	case REGULATOR_MODE_FAST:
1719a00630cSLaxman Dewangan 		val = FORCE_PWM_ENABLE;
1729a00630cSLaxman Dewangan 		break;
1739a00630cSLaxman Dewangan 
1749a00630cSLaxman Dewangan 	case REGULATOR_MODE_NORMAL:
1759a00630cSLaxman Dewangan 		val = 0;
1769a00630cSLaxman Dewangan 		break;
1779a00630cSLaxman Dewangan 
1789a00630cSLaxman Dewangan 	default:
1799a00630cSLaxman Dewangan 		return -EINVAL;
1809a00630cSLaxman Dewangan 	}
1819a00630cSLaxman Dewangan 
1829a00630cSLaxman Dewangan 	if (!tps->valid_gpios) {
1839a00630cSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap,
1849a00630cSLaxman Dewangan 			REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
1859a00630cSLaxman Dewangan 		if (ret < 0)
1869a00630cSLaxman Dewangan 			dev_err(tps->dev,
1879a00630cSLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
1889a00630cSLaxman Dewangan 				__func__, REG_VSET0 + tps->curr_vset_id, ret);
1899a00630cSLaxman Dewangan 		return ret;
1909a00630cSLaxman Dewangan 	}
1919a00630cSLaxman Dewangan 
1929a00630cSLaxman Dewangan 	/* If gpios are valid then all register set need to be control */
1939a00630cSLaxman Dewangan 	for (i = 0; i < 4; ++i) {
1949a00630cSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap,
1959a00630cSLaxman Dewangan 					REG_VSET0 + i, FORCE_PWM_ENABLE, val);
1969a00630cSLaxman Dewangan 		if (ret < 0) {
1979a00630cSLaxman Dewangan 			dev_err(tps->dev,
1989a00630cSLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
1999a00630cSLaxman Dewangan 				__func__, REG_VSET0 + i, ret);
2009a00630cSLaxman Dewangan 			return ret;
2019a00630cSLaxman Dewangan 		}
2029a00630cSLaxman Dewangan 	}
2039a00630cSLaxman Dewangan 	return ret;
2049a00630cSLaxman Dewangan }
2059a00630cSLaxman Dewangan 
tps62360_get_mode(struct regulator_dev * rdev)2069a00630cSLaxman Dewangan static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
2079a00630cSLaxman Dewangan {
2089a00630cSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
2099a00630cSLaxman Dewangan 	unsigned int data;
2109a00630cSLaxman Dewangan 	int ret;
2119a00630cSLaxman Dewangan 
2129a00630cSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
2139a00630cSLaxman Dewangan 	if (ret < 0) {
2149a00630cSLaxman Dewangan 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
2159a00630cSLaxman Dewangan 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
2169a00630cSLaxman Dewangan 		return ret;
2179a00630cSLaxman Dewangan 	}
2189a00630cSLaxman Dewangan 	return (data & FORCE_PWM_ENABLE) ?
2199a00630cSLaxman Dewangan 				REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
2209a00630cSLaxman Dewangan }
2219a00630cSLaxman Dewangan 
22201167e88SRikard Falkeborn static const struct regulator_ops tps62360_dcdc_ops = {
223a60cfce0SLaxman Dewangan 	.get_voltage_sel	= tps62360_dcdc_get_voltage_sel,
22441097afdSAxel Lin 	.set_voltage_sel	= tps62360_dcdc_set_voltage_sel,
2257f225ba5SAxel Lin 	.list_voltage		= regulator_list_voltage_linear,
22641097afdSAxel Lin 	.map_voltage		= regulator_map_voltage_linear,
2270072f0a8SAxel Lin 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
2289a00630cSLaxman Dewangan 	.set_mode		= tps62360_set_mode,
2299a00630cSLaxman Dewangan 	.get_mode		= tps62360_get_mode,
2306219929fSLaxman Dewangan };
2316219929fSLaxman Dewangan 
tps62360_init_dcdc(struct tps62360_chip * tps,struct tps62360_regulator_platform_data * pdata)232a5023574SBill Pemberton static int tps62360_init_dcdc(struct tps62360_chip *tps,
2336219929fSLaxman Dewangan 		struct tps62360_regulator_platform_data *pdata)
2346219929fSLaxman Dewangan {
2356219929fSLaxman Dewangan 	int ret;
236a60cfce0SLaxman Dewangan 	unsigned int ramp_ctrl;
2376219929fSLaxman Dewangan 
2382935fb18SLaxman Dewangan 	/* Initialize internal pull up/down control */
2396219929fSLaxman Dewangan 	if (tps->en_internal_pulldn)
2406219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
2416219929fSLaxman Dewangan 	else
2426219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
2436219929fSLaxman Dewangan 	if (ret < 0) {
2442935fb18SLaxman Dewangan 		dev_err(tps->dev,
2452935fb18SLaxman Dewangan 			"%s(): register %d write failed with err %d\n",
2462935fb18SLaxman Dewangan 			__func__, REG_CONTROL, ret);
2476219929fSLaxman Dewangan 		return ret;
2486219929fSLaxman Dewangan 	}
2496219929fSLaxman Dewangan 
2506219929fSLaxman Dewangan 	/* Reset output discharge path to reduce power consumption */
2516219929fSLaxman Dewangan 	ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
252a60cfce0SLaxman Dewangan 	if (ret < 0) {
2532935fb18SLaxman Dewangan 		dev_err(tps->dev,
2542935fb18SLaxman Dewangan 			"%s(): register %d update failed with err %d\n",
2552935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, ret);
2566219929fSLaxman Dewangan 		return ret;
2576219929fSLaxman Dewangan 	}
2586219929fSLaxman Dewangan 
259a60cfce0SLaxman Dewangan 	/* Get ramp value from ramp control register */
260a60cfce0SLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
261a60cfce0SLaxman Dewangan 	if (ret < 0) {
2622935fb18SLaxman Dewangan 		dev_err(tps->dev,
2632935fb18SLaxman Dewangan 			"%s(): register %d read failed with err %d\n",
2642935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, ret);
265a60cfce0SLaxman Dewangan 		return ret;
266a60cfce0SLaxman Dewangan 	}
2671864b670SAxel Lin 	ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
268a60cfce0SLaxman Dewangan 
269a60cfce0SLaxman Dewangan 	/* ramp mV/us = 32/(2^ramp_ctrl) */
2700072f0a8SAxel Lin 	tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
271a60cfce0SLaxman Dewangan 	return ret;
272a60cfce0SLaxman Dewangan }
273a60cfce0SLaxman Dewangan 
2746219929fSLaxman Dewangan static const struct regmap_config tps62360_regmap_config = {
2756219929fSLaxman Dewangan 	.reg_bits		= 8,
2766219929fSLaxman Dewangan 	.val_bits		= 8,
27716ea003bSLaxman Dewangan 	.max_register		= REG_CHIPID,
278*fe258f54SBo Liu 	.cache_type		= REGCACHE_MAPLE,
2796219929fSLaxman Dewangan };
2806219929fSLaxman Dewangan 
281684ae39bSLaxman Dewangan static struct tps62360_regulator_platform_data *
of_get_tps62360_platform_data(struct device * dev,const struct regulator_desc * desc)282072e78b1SJavier Martinez Canillas 	of_get_tps62360_platform_data(struct device *dev,
283072e78b1SJavier Martinez Canillas 				      const struct regulator_desc *desc)
284684ae39bSLaxman Dewangan {
285684ae39bSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata;
286684ae39bSLaxman Dewangan 	struct device_node *np = dev->of_node;
287684ae39bSLaxman Dewangan 
288684ae39bSLaxman Dewangan 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
28933e63ba6SSachin Kamat 	if (!pdata)
290684ae39bSLaxman Dewangan 		return NULL;
291684ae39bSLaxman Dewangan 
292072e78b1SJavier Martinez Canillas 	pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
293072e78b1SJavier Martinez Canillas 							  desc);
294684ae39bSLaxman Dewangan 	if (!pdata->reg_init_data) {
295684ae39bSLaxman Dewangan 		dev_err(dev, "Not able to get OF regulator init data\n");
296684ae39bSLaxman Dewangan 		return NULL;
297684ae39bSLaxman Dewangan 	}
298684ae39bSLaxman Dewangan 
2995bd73a16SRob Herring 	pdata->vsel0_def_state = of_property_read_bool(np, "ti,vsel0-state-high");
3005bd73a16SRob Herring 	pdata->vsel1_def_state = of_property_read_bool(np, "ti,vsel1-state-high");
3015bd73a16SRob Herring 	pdata->en_internal_pulldn = of_property_read_bool(np, "ti,enable-pull-down");
3025bd73a16SRob Herring 	pdata->en_discharge = of_property_read_bool(np, "ti,enable-vout-discharge");
303684ae39bSLaxman Dewangan 
304684ae39bSLaxman Dewangan 	return pdata;
305684ae39bSLaxman Dewangan }
306684ae39bSLaxman Dewangan 
307684ae39bSLaxman Dewangan #if defined(CONFIG_OF)
308684ae39bSLaxman Dewangan static const struct of_device_id tps62360_of_match[] = {
309684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
310684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
311684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
312684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
313684ae39bSLaxman Dewangan 	{},
314be15411dSAxel Lin };
315684ae39bSLaxman Dewangan MODULE_DEVICE_TABLE(of, tps62360_of_match);
316684ae39bSLaxman Dewangan #endif
317684ae39bSLaxman Dewangan 
tps62360_probe(struct i2c_client * client)31818804160SUwe Kleine-König static int tps62360_probe(struct i2c_client *client)
3196219929fSLaxman Dewangan {
32018804160SUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
321c172708dSMark Brown 	struct regulator_config config = { };
3226219929fSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata;
3236219929fSLaxman Dewangan 	struct regulator_dev *rdev;
3246219929fSLaxman Dewangan 	struct tps62360_chip *tps;
3256219929fSLaxman Dewangan 	int ret;
3266219929fSLaxman Dewangan 	int i;
327684ae39bSLaxman Dewangan 	int chip_id;
3286a8b5bb0SMaíra Canal 	int gpio_flags;
3296219929fSLaxman Dewangan 
330dff91d0bSJingoo Han 	pdata = dev_get_platdata(&client->dev);
331684ae39bSLaxman Dewangan 
332072e78b1SJavier Martinez Canillas 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
333072e78b1SJavier Martinez Canillas 	if (!tps)
334072e78b1SJavier Martinez Canillas 		return -ENOMEM;
335072e78b1SJavier Martinez Canillas 
336072e78b1SJavier Martinez Canillas 	tps->desc.name = client->name;
337072e78b1SJavier Martinez Canillas 	tps->desc.id = 0;
338072e78b1SJavier Martinez Canillas 	tps->desc.ops = &tps62360_dcdc_ops;
339072e78b1SJavier Martinez Canillas 	tps->desc.type = REGULATOR_VOLTAGE;
340072e78b1SJavier Martinez Canillas 	tps->desc.owner = THIS_MODULE;
341072e78b1SJavier Martinez Canillas 	tps->desc.uV_step = 10000;
342072e78b1SJavier Martinez Canillas 
343684ae39bSLaxman Dewangan 	if (client->dev.of_node) {
344684ae39bSLaxman Dewangan 		const struct of_device_id *match;
345684ae39bSLaxman Dewangan 		match = of_match_device(of_match_ptr(tps62360_of_match),
346684ae39bSLaxman Dewangan 				&client->dev);
347684ae39bSLaxman Dewangan 		if (!match) {
348684ae39bSLaxman Dewangan 			dev_err(&client->dev, "Error: No device match found\n");
349684ae39bSLaxman Dewangan 			return -ENODEV;
350684ae39bSLaxman Dewangan 		}
351541f597fSDavid Howells 		chip_id = (int)(long)match->data;
352684ae39bSLaxman Dewangan 		if (!pdata)
353072e78b1SJavier Martinez Canillas 			pdata = of_get_tps62360_platform_data(&client->dev,
354072e78b1SJavier Martinez Canillas 							      &tps->desc);
3550a62d03bSTuomas Tynkkynen 	} else if (id) {
3560a62d03bSTuomas Tynkkynen 		chip_id = id->driver_data;
3570a62d03bSTuomas Tynkkynen 	} else {
3580a62d03bSTuomas Tynkkynen 		dev_err(&client->dev, "No device tree match or id table match found\n");
3590a62d03bSTuomas Tynkkynen 		return -ENODEV;
360684ae39bSLaxman Dewangan 	}
361684ae39bSLaxman Dewangan 
3626219929fSLaxman Dewangan 	if (!pdata) {
3632935fb18SLaxman Dewangan 		dev_err(&client->dev, "%s(): Platform data not found\n",
3646219929fSLaxman Dewangan 						__func__);
3656219929fSLaxman Dewangan 		return -EIO;
3666219929fSLaxman Dewangan 	}
3676219929fSLaxman Dewangan 
3686219929fSLaxman Dewangan 	tps->en_discharge = pdata->en_discharge;
3696219929fSLaxman Dewangan 	tps->en_internal_pulldn = pdata->en_internal_pulldn;
3706219929fSLaxman Dewangan 	tps->dev = &client->dev;
371d1cf4f65SAxel Lin 
372684ae39bSLaxman Dewangan 	switch (chip_id) {
373d1cf4f65SAxel Lin 	case TPS62360:
374d1cf4f65SAxel Lin 	case TPS62362:
375b5152415SAxel Lin 		tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
376d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x3F;
377d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62360_N_VOLTAGES;
378d1cf4f65SAxel Lin 		break;
379d1cf4f65SAxel Lin 	case TPS62361:
380d1cf4f65SAxel Lin 	case TPS62363:
381b5152415SAxel Lin 		tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
382d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x7F;
383d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62361_N_VOLTAGES;
384d1cf4f65SAxel Lin 		break;
385d1cf4f65SAxel Lin 	default:
386d1cf4f65SAxel Lin 		return -ENODEV;
387d1cf4f65SAxel Lin 	}
3886219929fSLaxman Dewangan 
3899a4bdd87SAxel Lin 	tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
3906219929fSLaxman Dewangan 	if (IS_ERR(tps->regmap)) {
3916219929fSLaxman Dewangan 		ret = PTR_ERR(tps->regmap);
3922935fb18SLaxman Dewangan 		dev_err(&client->dev,
3932935fb18SLaxman Dewangan 			"%s(): regmap allocation failed with err %d\n",
3942935fb18SLaxman Dewangan 			__func__, ret);
3956219929fSLaxman Dewangan 		return ret;
3966219929fSLaxman Dewangan 	}
3976219929fSLaxman Dewangan 	i2c_set_clientdata(client, tps);
3986219929fSLaxman Dewangan 
3996219929fSLaxman Dewangan 	tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
4006219929fSLaxman Dewangan 				(pdata->vsel0_def_state & 1);
4016219929fSLaxman Dewangan 	tps->lru_index[0] = tps->curr_vset_id;
4026219929fSLaxman Dewangan 	tps->valid_gpios = false;
4036219929fSLaxman Dewangan 
4042935fb18SLaxman Dewangan 	gpio_flags = (pdata->vsel0_def_state) ?
4056a8b5bb0SMaíra Canal 			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
4066a8b5bb0SMaíra Canal 	tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags);
4076a8b5bb0SMaíra Canal 	if (IS_ERR(tps->vsel0_gpio)) {
4086219929fSLaxman Dewangan 		dev_err(&client->dev,
4096a8b5bb0SMaíra Canal 			"%s(): Could not obtain vsel0 GPIO: %ld\n",
4106a8b5bb0SMaíra Canal 			__func__, PTR_ERR(tps->vsel0_gpio));
4116a8b5bb0SMaíra Canal 		return PTR_ERR(tps->vsel0_gpio);
4126219929fSLaxman Dewangan 	}
4136219929fSLaxman Dewangan 
4142935fb18SLaxman Dewangan 	gpio_flags = (pdata->vsel1_def_state) ?
4156a8b5bb0SMaíra Canal 			GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
4166a8b5bb0SMaíra Canal 	tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags);
4176a8b5bb0SMaíra Canal 	if (IS_ERR(tps->vsel1_gpio)) {
4186219929fSLaxman Dewangan 		dev_err(&client->dev,
4196a8b5bb0SMaíra Canal 			"%s(): Could not obtain vsel1 GPIO: %ld\n",
4206a8b5bb0SMaíra Canal 			__func__, PTR_ERR(tps->vsel1_gpio));
4216a8b5bb0SMaíra Canal 		return PTR_ERR(tps->vsel1_gpio);
4226219929fSLaxman Dewangan 	}
4236a8b5bb0SMaíra Canal 
4246a8b5bb0SMaíra Canal 	if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) {
4256219929fSLaxman Dewangan 		tps->valid_gpios = true;
4266219929fSLaxman Dewangan 
4276219929fSLaxman Dewangan 		/*
4286219929fSLaxman Dewangan 		 * Initialize the lru index with vset_reg id
4296219929fSLaxman Dewangan 		 * The index 0 will be most recently used and
4306219929fSLaxman Dewangan 		 * set with the tps->curr_vset_id */
4316219929fSLaxman Dewangan 		for (i = 0; i < 4; ++i)
4326219929fSLaxman Dewangan 			tps->lru_index[i] = i;
4336219929fSLaxman Dewangan 		tps->lru_index[0] = tps->curr_vset_id;
4346219929fSLaxman Dewangan 		tps->lru_index[tps->curr_vset_id] = 0;
4356219929fSLaxman Dewangan 	}
4366219929fSLaxman Dewangan 
4376219929fSLaxman Dewangan 	ret = tps62360_init_dcdc(tps, pdata);
4386219929fSLaxman Dewangan 	if (ret < 0) {
4392935fb18SLaxman Dewangan 		dev_err(tps->dev, "%s(): Init failed with err = %d\n",
4406219929fSLaxman Dewangan 				__func__, ret);
4418a8e3d59SLaxman Dewangan 		return ret;
4426219929fSLaxman Dewangan 	}
4436219929fSLaxman Dewangan 
444c172708dSMark Brown 	config.dev = &client->dev;
4458bdca009SLaxman Dewangan 	config.init_data = pdata->reg_init_data;
446c172708dSMark Brown 	config.driver_data = tps;
4479fc3815eSLaxman Dewangan 	config.of_node = client->dev.of_node;
448c172708dSMark Brown 
4496219929fSLaxman Dewangan 	/* Register the regulators */
45058c6e938SSachin Kamat 	rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
4516219929fSLaxman Dewangan 	if (IS_ERR(rdev)) {
4522935fb18SLaxman Dewangan 		dev_err(tps->dev,
4532935fb18SLaxman Dewangan 			"%s(): regulator register failed with err %s\n",
4546219929fSLaxman Dewangan 			__func__, id->name);
4558a8e3d59SLaxman Dewangan 		return PTR_ERR(rdev);
4566219929fSLaxman Dewangan 	}
4576219929fSLaxman Dewangan 
4586219929fSLaxman Dewangan 	tps->rdev = rdev;
4596219929fSLaxman Dewangan 	return 0;
4606219929fSLaxman Dewangan }
4616219929fSLaxman Dewangan 
tps62360_shutdown(struct i2c_client * client)4626219929fSLaxman Dewangan static void tps62360_shutdown(struct i2c_client *client)
4636219929fSLaxman Dewangan {
4646219929fSLaxman Dewangan 	struct tps62360_chip *tps = i2c_get_clientdata(client);
4656219929fSLaxman Dewangan 	int st;
4666219929fSLaxman Dewangan 
4676219929fSLaxman Dewangan 	if (!tps->en_discharge)
4686219929fSLaxman Dewangan 		return;
4696219929fSLaxman Dewangan 
4706219929fSLaxman Dewangan 	/* Configure the output discharge path */
4716219929fSLaxman Dewangan 	st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
4726219929fSLaxman Dewangan 	if (st < 0)
4732935fb18SLaxman Dewangan 		dev_err(tps->dev,
4742935fb18SLaxman Dewangan 			"%s(): register %d update failed with err %d\n",
4752935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, st);
4766219929fSLaxman Dewangan }
4776219929fSLaxman Dewangan 
4786219929fSLaxman Dewangan static const struct i2c_device_id tps62360_id[] = {
4796219929fSLaxman Dewangan 	{.name = "tps62360", .driver_data = TPS62360},
4806219929fSLaxman Dewangan 	{.name = "tps62361", .driver_data = TPS62361},
481d1cf4f65SAxel Lin 	{.name = "tps62362", .driver_data = TPS62362},
482d1cf4f65SAxel Lin 	{.name = "tps62363", .driver_data = TPS62363},
4836219929fSLaxman Dewangan 	{},
4846219929fSLaxman Dewangan };
4856219929fSLaxman Dewangan 
4866219929fSLaxman Dewangan MODULE_DEVICE_TABLE(i2c, tps62360_id);
4876219929fSLaxman Dewangan 
4886219929fSLaxman Dewangan static struct i2c_driver tps62360_i2c_driver = {
4896219929fSLaxman Dewangan 	.driver = {
4906219929fSLaxman Dewangan 		.name = "tps62360",
491259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
492684ae39bSLaxman Dewangan 		.of_match_table = of_match_ptr(tps62360_of_match),
4936219929fSLaxman Dewangan 	},
494964e1865SUwe Kleine-König 	.probe = tps62360_probe,
4956219929fSLaxman Dewangan 	.shutdown = tps62360_shutdown,
4966219929fSLaxman Dewangan 	.id_table = tps62360_id,
4976219929fSLaxman Dewangan };
4986219929fSLaxman Dewangan 
tps62360_init(void)4996219929fSLaxman Dewangan static int __init tps62360_init(void)
5006219929fSLaxman Dewangan {
5016219929fSLaxman Dewangan 	return i2c_add_driver(&tps62360_i2c_driver);
5026219929fSLaxman Dewangan }
5036219929fSLaxman Dewangan subsys_initcall(tps62360_init);
5046219929fSLaxman Dewangan 
tps62360_cleanup(void)5056219929fSLaxman Dewangan static void __exit tps62360_cleanup(void)
5066219929fSLaxman Dewangan {
5076219929fSLaxman Dewangan 	i2c_del_driver(&tps62360_i2c_driver);
5086219929fSLaxman Dewangan }
5096219929fSLaxman Dewangan module_exit(tps62360_cleanup);
5106219929fSLaxman Dewangan 
5116219929fSLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
512d1cf4f65SAxel Lin MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
5136219929fSLaxman Dewangan MODULE_LICENSE("GPL v2");
514