16219929fSLaxman Dewangan /*
26219929fSLaxman Dewangan  * tps62360.c -- TI tps62360
36219929fSLaxman Dewangan  *
4d1cf4f65SAxel Lin  * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
56219929fSLaxman Dewangan  *
66219929fSLaxman Dewangan  * Copyright (c) 2012, NVIDIA Corporation.
76219929fSLaxman Dewangan  *
86219929fSLaxman Dewangan  * Author: Laxman Dewangan <ldewangan@nvidia.com>
96219929fSLaxman Dewangan  *
106219929fSLaxman Dewangan  * This program is free software; you can redistribute it and/or
116219929fSLaxman Dewangan  * modify it under the terms of the GNU General Public License as
126219929fSLaxman Dewangan  * published by the Free Software Foundation version 2.
136219929fSLaxman Dewangan  *
146219929fSLaxman Dewangan  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
156219929fSLaxman Dewangan  * whether express or implied; without even the implied warranty of
166219929fSLaxman Dewangan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
176219929fSLaxman Dewangan  * General Public License for more details.
186219929fSLaxman Dewangan  *
196219929fSLaxman Dewangan  * You should have received a copy of the GNU General Public License
206219929fSLaxman Dewangan  * along with this program; if not, write to the Free Software
216219929fSLaxman Dewangan  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
226219929fSLaxman Dewangan  * 02111-1307, USA
236219929fSLaxman Dewangan  */
246219929fSLaxman Dewangan 
256219929fSLaxman Dewangan #include <linux/kernel.h>
266219929fSLaxman Dewangan #include <linux/module.h>
276219929fSLaxman Dewangan #include <linux/init.h>
286219929fSLaxman Dewangan #include <linux/err.h>
29684ae39bSLaxman Dewangan #include <linux/of.h>
30684ae39bSLaxman Dewangan #include <linux/of_device.h>
31684ae39bSLaxman Dewangan #include <linux/of_gpio.h>
32684ae39bSLaxman Dewangan #include <linux/regulator/of_regulator.h>
336219929fSLaxman Dewangan #include <linux/platform_device.h>
346219929fSLaxman Dewangan #include <linux/regulator/driver.h>
356219929fSLaxman Dewangan #include <linux/regulator/machine.h>
366219929fSLaxman Dewangan #include <linux/regulator/tps62360.h>
376219929fSLaxman Dewangan #include <linux/gpio.h>
386219929fSLaxman Dewangan #include <linux/i2c.h>
396219929fSLaxman Dewangan #include <linux/slab.h>
406219929fSLaxman Dewangan #include <linux/regmap.h>
416219929fSLaxman Dewangan 
426219929fSLaxman Dewangan /* Register definitions */
436219929fSLaxman Dewangan #define REG_VSET0		0
446219929fSLaxman Dewangan #define REG_VSET1		1
456219929fSLaxman Dewangan #define REG_VSET2		2
466219929fSLaxman Dewangan #define REG_VSET3		3
476219929fSLaxman Dewangan #define REG_CONTROL		4
486219929fSLaxman Dewangan #define REG_TEMP		5
496219929fSLaxman Dewangan #define REG_RAMPCTRL		6
506219929fSLaxman Dewangan #define REG_CHIPID		8
516219929fSLaxman Dewangan 
529a00630cSLaxman Dewangan #define FORCE_PWM_ENABLE	BIT(7)
539a00630cSLaxman Dewangan 
54d1cf4f65SAxel Lin enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
556219929fSLaxman Dewangan 
562935fb18SLaxman Dewangan #define TPS62360_BASE_VOLTAGE	770000
576219929fSLaxman Dewangan #define TPS62360_N_VOLTAGES	64
586219929fSLaxman Dewangan 
592935fb18SLaxman Dewangan #define TPS62361_BASE_VOLTAGE	500000
606219929fSLaxman Dewangan #define TPS62361_N_VOLTAGES	128
616219929fSLaxman Dewangan 
626219929fSLaxman Dewangan /* tps 62360 chip information */
636219929fSLaxman Dewangan struct tps62360_chip {
646219929fSLaxman Dewangan 	struct device *dev;
656219929fSLaxman Dewangan 	struct regulator_desc desc;
666219929fSLaxman Dewangan 	struct regulator_dev *rdev;
676219929fSLaxman Dewangan 	struct regmap *regmap;
686219929fSLaxman Dewangan 	int chip_id;
696219929fSLaxman Dewangan 	int vsel0_gpio;
706219929fSLaxman Dewangan 	int vsel1_gpio;
716219929fSLaxman Dewangan 	int voltage_base;
726219929fSLaxman Dewangan 	u8 voltage_reg_mask;
736219929fSLaxman Dewangan 	bool en_internal_pulldn;
746219929fSLaxman Dewangan 	bool en_discharge;
756219929fSLaxman Dewangan 	bool valid_gpios;
766219929fSLaxman Dewangan 	int lru_index[4];
776219929fSLaxman Dewangan 	int curr_vset_vsel[4];
786219929fSLaxman Dewangan 	int curr_vset_id;
79a60cfce0SLaxman Dewangan 	int change_uv_per_us;
806219929fSLaxman Dewangan };
816219929fSLaxman Dewangan 
826219929fSLaxman Dewangan /*
836219929fSLaxman Dewangan  * find_voltage_set_register: Find new voltage configuration register
846219929fSLaxman Dewangan  * (VSET) id.
856219929fSLaxman Dewangan  * The finding of the new VSET register will be based on the LRU mechanism.
866219929fSLaxman Dewangan  * Each VSET register will have different voltage configured . This
876219929fSLaxman Dewangan  * Function will look if any of the VSET register have requested voltage set
886219929fSLaxman Dewangan  * or not.
896219929fSLaxman Dewangan  *     - If it is already there then it will make that register as most
906219929fSLaxman Dewangan  *       recently used and return as found so that caller need not to set
916219929fSLaxman Dewangan  *       the VSET register but need to set the proper gpios to select this
926219929fSLaxman Dewangan  *       VSET register.
936219929fSLaxman Dewangan  *     - If requested voltage is not found then it will use the least
946219929fSLaxman Dewangan  *       recently mechanism to get new VSET register for new configuration
956219929fSLaxman Dewangan  *       and will return not_found so that caller need to set new VSET
966219929fSLaxman Dewangan  *       register and then gpios (both).
976219929fSLaxman Dewangan  */
986219929fSLaxman Dewangan static bool find_voltage_set_register(struct tps62360_chip *tps,
996219929fSLaxman Dewangan 		int req_vsel, int *vset_reg_id)
1006219929fSLaxman Dewangan {
1016219929fSLaxman Dewangan 	int i;
1026219929fSLaxman Dewangan 	bool found = false;
1036219929fSLaxman Dewangan 	int new_vset_reg = tps->lru_index[3];
1046219929fSLaxman Dewangan 	int found_index = 3;
1052935fb18SLaxman Dewangan 
1066219929fSLaxman Dewangan 	for (i = 0; i < 4; ++i) {
1076219929fSLaxman Dewangan 		if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
1086219929fSLaxman Dewangan 			new_vset_reg = tps->lru_index[i];
1096219929fSLaxman Dewangan 			found_index = i;
1106219929fSLaxman Dewangan 			found = true;
1116219929fSLaxman Dewangan 			goto update_lru_index;
1126219929fSLaxman Dewangan 		}
1136219929fSLaxman Dewangan 	}
1146219929fSLaxman Dewangan 
1156219929fSLaxman Dewangan update_lru_index:
1166219929fSLaxman Dewangan 	for (i = found_index; i > 0; i--)
1176219929fSLaxman Dewangan 		tps->lru_index[i] = tps->lru_index[i - 1];
1186219929fSLaxman Dewangan 
1196219929fSLaxman Dewangan 	tps->lru_index[0] = new_vset_reg;
1206219929fSLaxman Dewangan 	*vset_reg_id = new_vset_reg;
1216219929fSLaxman Dewangan 	return found;
1226219929fSLaxman Dewangan }
1236219929fSLaxman Dewangan 
124a60cfce0SLaxman Dewangan static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
1256219929fSLaxman Dewangan {
1266219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1276219929fSLaxman Dewangan 	int vsel;
1286219929fSLaxman Dewangan 	unsigned int data;
1296219929fSLaxman Dewangan 	int ret;
1306219929fSLaxman Dewangan 
1316219929fSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
1326219929fSLaxman Dewangan 	if (ret < 0) {
1332935fb18SLaxman Dewangan 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
1342935fb18SLaxman Dewangan 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
1356219929fSLaxman Dewangan 		return ret;
1366219929fSLaxman Dewangan 	}
1376219929fSLaxman Dewangan 	vsel = (int)data & tps->voltage_reg_mask;
138a60cfce0SLaxman Dewangan 	return vsel;
1396219929fSLaxman Dewangan }
1406219929fSLaxman Dewangan 
14141097afdSAxel Lin static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
14241097afdSAxel Lin 					 unsigned selector)
1436219929fSLaxman Dewangan {
1446219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1456219929fSLaxman Dewangan 	int ret;
1466219929fSLaxman Dewangan 	bool found = false;
1476219929fSLaxman Dewangan 	int new_vset_id = tps->curr_vset_id;
1486219929fSLaxman Dewangan 
1496219929fSLaxman Dewangan 	/*
1506219929fSLaxman Dewangan 	 * If gpios are available to select the VSET register then least
1516219929fSLaxman Dewangan 	 * recently used register for new configuration.
1526219929fSLaxman Dewangan 	 */
1536219929fSLaxman Dewangan 	if (tps->valid_gpios)
15441097afdSAxel Lin 		found = find_voltage_set_register(tps, selector, &new_vset_id);
1556219929fSLaxman Dewangan 
1566219929fSLaxman Dewangan 	if (!found) {
1576219929fSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
15841097afdSAxel Lin 				tps->voltage_reg_mask, selector);
1596219929fSLaxman Dewangan 		if (ret < 0) {
1602935fb18SLaxman Dewangan 			dev_err(tps->dev,
1612935fb18SLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
1622935fb18SLaxman Dewangan 				 __func__, REG_VSET0 + new_vset_id, ret);
1636219929fSLaxman Dewangan 			return ret;
1646219929fSLaxman Dewangan 		}
1656219929fSLaxman Dewangan 		tps->curr_vset_id = new_vset_id;
16641097afdSAxel Lin 		tps->curr_vset_vsel[new_vset_id] = selector;
1676219929fSLaxman Dewangan 	}
1686219929fSLaxman Dewangan 
1696219929fSLaxman Dewangan 	/* Select proper VSET register vio gpios */
1706219929fSLaxman Dewangan 	if (tps->valid_gpios) {
1712935fb18SLaxman Dewangan 		gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
1726219929fSLaxman Dewangan 		gpio_set_value_cansleep(tps->vsel1_gpio,
1736219929fSLaxman Dewangan 					(new_vset_id >> 1) & 0x1);
1746219929fSLaxman Dewangan 	}
1756219929fSLaxman Dewangan 	return 0;
1766219929fSLaxman Dewangan }
1776219929fSLaxman Dewangan 
178a60cfce0SLaxman Dewangan static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev,
179a60cfce0SLaxman Dewangan 		unsigned int old_selector, unsigned int new_selector)
180a60cfce0SLaxman Dewangan {
181a60cfce0SLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
182a60cfce0SLaxman Dewangan 	int old_uV, new_uV;
183a60cfce0SLaxman Dewangan 
1847f225ba5SAxel Lin 	old_uV = regulator_list_voltage_linear(rdev, old_selector);
185a60cfce0SLaxman Dewangan 	if (old_uV < 0)
186a60cfce0SLaxman Dewangan 		return old_uV;
187a60cfce0SLaxman Dewangan 
1887f225ba5SAxel Lin 	new_uV = regulator_list_voltage_linear(rdev, new_selector);
189a60cfce0SLaxman Dewangan 	if (new_uV < 0)
190a60cfce0SLaxman Dewangan 		return new_uV;
191a60cfce0SLaxman Dewangan 
192a60cfce0SLaxman Dewangan 	return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
193a60cfce0SLaxman Dewangan }
194a60cfce0SLaxman Dewangan 
1959a00630cSLaxman Dewangan static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
1969a00630cSLaxman Dewangan {
1979a00630cSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
1989a00630cSLaxman Dewangan 	int i;
1999a00630cSLaxman Dewangan 	int val;
2009a00630cSLaxman Dewangan 	int ret;
2019a00630cSLaxman Dewangan 
2029a00630cSLaxman Dewangan 	/* Enable force PWM mode in FAST mode only. */
2039a00630cSLaxman Dewangan 	switch (mode) {
2049a00630cSLaxman Dewangan 	case REGULATOR_MODE_FAST:
2059a00630cSLaxman Dewangan 		val = FORCE_PWM_ENABLE;
2069a00630cSLaxman Dewangan 		break;
2079a00630cSLaxman Dewangan 
2089a00630cSLaxman Dewangan 	case REGULATOR_MODE_NORMAL:
2099a00630cSLaxman Dewangan 		val = 0;
2109a00630cSLaxman Dewangan 		break;
2119a00630cSLaxman Dewangan 
2129a00630cSLaxman Dewangan 	default:
2139a00630cSLaxman Dewangan 		return -EINVAL;
2149a00630cSLaxman Dewangan 	}
2159a00630cSLaxman Dewangan 
2169a00630cSLaxman Dewangan 	if (!tps->valid_gpios) {
2179a00630cSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap,
2189a00630cSLaxman Dewangan 			REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
2199a00630cSLaxman Dewangan 		if (ret < 0)
2209a00630cSLaxman Dewangan 			dev_err(tps->dev,
2219a00630cSLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
2229a00630cSLaxman Dewangan 				__func__, REG_VSET0 + tps->curr_vset_id, ret);
2239a00630cSLaxman Dewangan 		return ret;
2249a00630cSLaxman Dewangan 	}
2259a00630cSLaxman Dewangan 
2269a00630cSLaxman Dewangan 	/* If gpios are valid then all register set need to be control */
2279a00630cSLaxman Dewangan 	for (i = 0; i < 4; ++i) {
2289a00630cSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap,
2299a00630cSLaxman Dewangan 					REG_VSET0 + i, FORCE_PWM_ENABLE, val);
2309a00630cSLaxman Dewangan 		if (ret < 0) {
2319a00630cSLaxman Dewangan 			dev_err(tps->dev,
2329a00630cSLaxman Dewangan 				"%s(): register %d update failed with err %d\n",
2339a00630cSLaxman Dewangan 				__func__, REG_VSET0 + i, ret);
2349a00630cSLaxman Dewangan 			return ret;
2359a00630cSLaxman Dewangan 		}
2369a00630cSLaxman Dewangan 	}
2379a00630cSLaxman Dewangan 	return ret;
2389a00630cSLaxman Dewangan }
2399a00630cSLaxman Dewangan 
2409a00630cSLaxman Dewangan static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
2419a00630cSLaxman Dewangan {
2429a00630cSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
2439a00630cSLaxman Dewangan 	unsigned int data;
2449a00630cSLaxman Dewangan 	int ret;
2459a00630cSLaxman Dewangan 
2469a00630cSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
2479a00630cSLaxman Dewangan 	if (ret < 0) {
2489a00630cSLaxman Dewangan 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
2499a00630cSLaxman Dewangan 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
2509a00630cSLaxman Dewangan 		return ret;
2519a00630cSLaxman Dewangan 	}
2529a00630cSLaxman Dewangan 	return (data & FORCE_PWM_ENABLE) ?
2539a00630cSLaxman Dewangan 				REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
2549a00630cSLaxman Dewangan }
2559a00630cSLaxman Dewangan 
2566219929fSLaxman Dewangan static struct regulator_ops tps62360_dcdc_ops = {
257a60cfce0SLaxman Dewangan 	.get_voltage_sel	= tps62360_dcdc_get_voltage_sel,
25841097afdSAxel Lin 	.set_voltage_sel	= tps62360_dcdc_set_voltage_sel,
2597f225ba5SAxel Lin 	.list_voltage		= regulator_list_voltage_linear,
26041097afdSAxel Lin 	.map_voltage		= regulator_map_voltage_linear,
261a60cfce0SLaxman Dewangan 	.set_voltage_time_sel	= tps62360_set_voltage_time_sel,
2629a00630cSLaxman Dewangan 	.set_mode		= tps62360_set_mode,
2639a00630cSLaxman Dewangan 	.get_mode		= tps62360_get_mode,
2646219929fSLaxman Dewangan };
2656219929fSLaxman Dewangan 
2662935fb18SLaxman Dewangan static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
2676219929fSLaxman Dewangan 		struct tps62360_regulator_platform_data *pdata)
2686219929fSLaxman Dewangan {
2696219929fSLaxman Dewangan 	int ret;
270a60cfce0SLaxman Dewangan 	unsigned int ramp_ctrl;
2716219929fSLaxman Dewangan 
2722935fb18SLaxman Dewangan 	/* Initialize internal pull up/down control */
2736219929fSLaxman Dewangan 	if (tps->en_internal_pulldn)
2746219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
2756219929fSLaxman Dewangan 	else
2766219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
2776219929fSLaxman Dewangan 	if (ret < 0) {
2782935fb18SLaxman Dewangan 		dev_err(tps->dev,
2792935fb18SLaxman Dewangan 			"%s(): register %d write failed with err %d\n",
2802935fb18SLaxman Dewangan 			__func__, REG_CONTROL, ret);
2816219929fSLaxman Dewangan 		return ret;
2826219929fSLaxman Dewangan 	}
2836219929fSLaxman Dewangan 
2846219929fSLaxman Dewangan 	/* Reset output discharge path to reduce power consumption */
2856219929fSLaxman Dewangan 	ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
286a60cfce0SLaxman Dewangan 	if (ret < 0) {
2872935fb18SLaxman Dewangan 		dev_err(tps->dev,
2882935fb18SLaxman Dewangan 			"%s(): register %d update failed with err %d\n",
2892935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, ret);
2906219929fSLaxman Dewangan 		return ret;
2916219929fSLaxman Dewangan 	}
2926219929fSLaxman Dewangan 
293a60cfce0SLaxman Dewangan 	/* Get ramp value from ramp control register */
294a60cfce0SLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
295a60cfce0SLaxman Dewangan 	if (ret < 0) {
2962935fb18SLaxman Dewangan 		dev_err(tps->dev,
2972935fb18SLaxman Dewangan 			"%s(): register %d read failed with err %d\n",
2982935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, ret);
299a60cfce0SLaxman Dewangan 		return ret;
300a60cfce0SLaxman Dewangan 	}
301a60cfce0SLaxman Dewangan 	ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
302a60cfce0SLaxman Dewangan 
303a60cfce0SLaxman Dewangan 	/* ramp mV/us = 32/(2^ramp_ctrl) */
304a60cfce0SLaxman Dewangan 	tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
305a60cfce0SLaxman Dewangan 	return ret;
306a60cfce0SLaxman Dewangan }
307a60cfce0SLaxman Dewangan 
3086219929fSLaxman Dewangan static const struct regmap_config tps62360_regmap_config = {
3096219929fSLaxman Dewangan 	.reg_bits		= 8,
3106219929fSLaxman Dewangan 	.val_bits		= 8,
31116ea003bSLaxman Dewangan 	.max_register		= REG_CHIPID,
31216ea003bSLaxman Dewangan 	.cache_type		= REGCACHE_RBTREE,
3136219929fSLaxman Dewangan };
3146219929fSLaxman Dewangan 
315684ae39bSLaxman Dewangan static struct tps62360_regulator_platform_data *
316684ae39bSLaxman Dewangan 	of_get_tps62360_platform_data(struct device *dev)
317684ae39bSLaxman Dewangan {
318684ae39bSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata;
319684ae39bSLaxman Dewangan 	struct device_node *np = dev->of_node;
320684ae39bSLaxman Dewangan 
321684ae39bSLaxman Dewangan 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
322684ae39bSLaxman Dewangan 	if (!pdata) {
323684ae39bSLaxman Dewangan 		dev_err(dev, "Memory alloc failed for platform data\n");
324684ae39bSLaxman Dewangan 		return NULL;
325684ae39bSLaxman Dewangan 	}
326684ae39bSLaxman Dewangan 
327684ae39bSLaxman Dewangan 	pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
328684ae39bSLaxman Dewangan 	if (!pdata->reg_init_data) {
329684ae39bSLaxman Dewangan 		dev_err(dev, "Not able to get OF regulator init data\n");
330684ae39bSLaxman Dewangan 		return NULL;
331684ae39bSLaxman Dewangan 	}
332684ae39bSLaxman Dewangan 
333684ae39bSLaxman Dewangan 	pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
334684ae39bSLaxman Dewangan 	pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
335684ae39bSLaxman Dewangan 
336684ae39bSLaxman Dewangan 	if (of_find_property(np, "ti,vsel0-state-high", NULL))
337684ae39bSLaxman Dewangan 		pdata->vsel0_def_state = 1;
338684ae39bSLaxman Dewangan 
339684ae39bSLaxman Dewangan 	if (of_find_property(np, "ti,vsel1-state-high", NULL))
340684ae39bSLaxman Dewangan 		pdata->vsel1_def_state = 1;
341684ae39bSLaxman Dewangan 
342684ae39bSLaxman Dewangan 	if (of_find_property(np, "ti,enable-pull-down", NULL))
343684ae39bSLaxman Dewangan 		pdata->en_internal_pulldn = true;
344684ae39bSLaxman Dewangan 
345684ae39bSLaxman Dewangan 	if (of_find_property(np, "ti,enable-vout-discharge", NULL))
346684ae39bSLaxman Dewangan 		pdata->en_discharge = true;
347684ae39bSLaxman Dewangan 
348684ae39bSLaxman Dewangan 	return pdata;
349684ae39bSLaxman Dewangan }
350684ae39bSLaxman Dewangan 
351684ae39bSLaxman Dewangan #if defined(CONFIG_OF)
352684ae39bSLaxman Dewangan static const struct of_device_id tps62360_of_match[] = {
353684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
354684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
355684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
356684ae39bSLaxman Dewangan 	 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
357684ae39bSLaxman Dewangan 	{},
358be15411dSAxel Lin };
359684ae39bSLaxman Dewangan MODULE_DEVICE_TABLE(of, tps62360_of_match);
360684ae39bSLaxman Dewangan #endif
361684ae39bSLaxman Dewangan 
3626219929fSLaxman Dewangan static int __devinit tps62360_probe(struct i2c_client *client,
3636219929fSLaxman Dewangan 				     const struct i2c_device_id *id)
3646219929fSLaxman Dewangan {
365c172708dSMark Brown 	struct regulator_config config = { };
3666219929fSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata;
3676219929fSLaxman Dewangan 	struct regulator_dev *rdev;
3686219929fSLaxman Dewangan 	struct tps62360_chip *tps;
3696219929fSLaxman Dewangan 	int ret;
3706219929fSLaxman Dewangan 	int i;
371684ae39bSLaxman Dewangan 	int chip_id;
3726219929fSLaxman Dewangan 
3736219929fSLaxman Dewangan 	pdata = client->dev.platform_data;
374684ae39bSLaxman Dewangan 	chip_id = id->driver_data;
375684ae39bSLaxman Dewangan 
376684ae39bSLaxman Dewangan 	if (client->dev.of_node) {
377684ae39bSLaxman Dewangan 		const struct of_device_id *match;
378684ae39bSLaxman Dewangan 		match = of_match_device(of_match_ptr(tps62360_of_match),
379684ae39bSLaxman Dewangan 				&client->dev);
380684ae39bSLaxman Dewangan 		if (!match) {
381684ae39bSLaxman Dewangan 			dev_err(&client->dev, "Error: No device match found\n");
382684ae39bSLaxman Dewangan 			return -ENODEV;
383684ae39bSLaxman Dewangan 		}
384684ae39bSLaxman Dewangan 		chip_id = (int)match->data;
385684ae39bSLaxman Dewangan 		if (!pdata)
386684ae39bSLaxman Dewangan 			pdata = of_get_tps62360_platform_data(&client->dev);
387684ae39bSLaxman Dewangan 	}
388684ae39bSLaxman Dewangan 
3896219929fSLaxman Dewangan 	if (!pdata) {
3902935fb18SLaxman Dewangan 		dev_err(&client->dev, "%s(): Platform data not found\n",
3916219929fSLaxman Dewangan 						__func__);
3926219929fSLaxman Dewangan 		return -EIO;
3936219929fSLaxman Dewangan 	}
3946219929fSLaxman Dewangan 
3956219929fSLaxman Dewangan 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
3966219929fSLaxman Dewangan 	if (!tps) {
3972935fb18SLaxman Dewangan 		dev_err(&client->dev, "%s(): Memory allocation failed\n",
3986219929fSLaxman Dewangan 						__func__);
3996219929fSLaxman Dewangan 		return -ENOMEM;
4006219929fSLaxman Dewangan 	}
4016219929fSLaxman Dewangan 
4026219929fSLaxman Dewangan 	tps->en_discharge = pdata->en_discharge;
4036219929fSLaxman Dewangan 	tps->en_internal_pulldn = pdata->en_internal_pulldn;
4046219929fSLaxman Dewangan 	tps->vsel0_gpio = pdata->vsel0_gpio;
4056219929fSLaxman Dewangan 	tps->vsel1_gpio = pdata->vsel1_gpio;
4066219929fSLaxman Dewangan 	tps->dev = &client->dev;
407d1cf4f65SAxel Lin 
408684ae39bSLaxman Dewangan 	switch (chip_id) {
409d1cf4f65SAxel Lin 	case TPS62360:
410d1cf4f65SAxel Lin 	case TPS62362:
411d1cf4f65SAxel Lin 		tps->voltage_base = TPS62360_BASE_VOLTAGE;
412d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x3F;
413d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62360_N_VOLTAGES;
414d1cf4f65SAxel Lin 		break;
415d1cf4f65SAxel Lin 	case TPS62361:
416d1cf4f65SAxel Lin 	case TPS62363:
417d1cf4f65SAxel Lin 		tps->voltage_base = TPS62361_BASE_VOLTAGE;
418d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x7F;
419d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62361_N_VOLTAGES;
420d1cf4f65SAxel Lin 		break;
421d1cf4f65SAxel Lin 	default:
422d1cf4f65SAxel Lin 		return -ENODEV;
423d1cf4f65SAxel Lin 	}
4246219929fSLaxman Dewangan 
4256219929fSLaxman Dewangan 	tps->desc.name = id->name;
4266219929fSLaxman Dewangan 	tps->desc.id = 0;
4276219929fSLaxman Dewangan 	tps->desc.ops = &tps62360_dcdc_ops;
4286219929fSLaxman Dewangan 	tps->desc.type = REGULATOR_VOLTAGE;
4296219929fSLaxman Dewangan 	tps->desc.owner = THIS_MODULE;
4307f225ba5SAxel Lin 	tps->desc.min_uV = tps->voltage_base;
4317f225ba5SAxel Lin 	tps->desc.uV_step = 10000;
4327f225ba5SAxel Lin 
4339a4bdd87SAxel Lin 	tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
4346219929fSLaxman Dewangan 	if (IS_ERR(tps->regmap)) {
4356219929fSLaxman Dewangan 		ret = PTR_ERR(tps->regmap);
4362935fb18SLaxman Dewangan 		dev_err(&client->dev,
4372935fb18SLaxman Dewangan 			"%s(): regmap allocation failed with err %d\n",
4382935fb18SLaxman Dewangan 			__func__, ret);
4396219929fSLaxman Dewangan 		return ret;
4406219929fSLaxman Dewangan 	}
4416219929fSLaxman Dewangan 	i2c_set_clientdata(client, tps);
4426219929fSLaxman Dewangan 
4436219929fSLaxman Dewangan 	tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
4446219929fSLaxman Dewangan 				(pdata->vsel0_def_state & 1);
4456219929fSLaxman Dewangan 	tps->lru_index[0] = tps->curr_vset_id;
4466219929fSLaxman Dewangan 	tps->valid_gpios = false;
4476219929fSLaxman Dewangan 
4486219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
4492935fb18SLaxman Dewangan 		int gpio_flags;
4502935fb18SLaxman Dewangan 		gpio_flags = (pdata->vsel0_def_state) ?
4512935fb18SLaxman Dewangan 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
4522935fb18SLaxman Dewangan 		ret = gpio_request_one(tps->vsel0_gpio,
4532935fb18SLaxman Dewangan 				gpio_flags, "tps62360-vsel0");
4546219929fSLaxman Dewangan 		if (ret) {
4556219929fSLaxman Dewangan 			dev_err(&client->dev,
4562935fb18SLaxman Dewangan 				"%s(): Could not obtain vsel0 GPIO %d: %d\n",
4572935fb18SLaxman Dewangan 				__func__, tps->vsel0_gpio, ret);
4586219929fSLaxman Dewangan 			goto err_gpio0;
4596219929fSLaxman Dewangan 		}
4606219929fSLaxman Dewangan 
4612935fb18SLaxman Dewangan 		gpio_flags = (pdata->vsel1_def_state) ?
4622935fb18SLaxman Dewangan 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
4632935fb18SLaxman Dewangan 		ret = gpio_request_one(tps->vsel1_gpio,
4642935fb18SLaxman Dewangan 				gpio_flags, "tps62360-vsel1");
4656219929fSLaxman Dewangan 		if (ret) {
4666219929fSLaxman Dewangan 			dev_err(&client->dev,
4672935fb18SLaxman Dewangan 				"%s(): Could not obtain vsel1 GPIO %d: %d\n",
4682935fb18SLaxman Dewangan 				__func__, tps->vsel1_gpio, ret);
4696219929fSLaxman Dewangan 			goto err_gpio1;
4706219929fSLaxman Dewangan 		}
4716219929fSLaxman Dewangan 		tps->valid_gpios = true;
4726219929fSLaxman Dewangan 
4736219929fSLaxman Dewangan 		/*
4746219929fSLaxman Dewangan 		 * Initialize the lru index with vset_reg id
4756219929fSLaxman Dewangan 		 * The index 0 will be most recently used and
4766219929fSLaxman Dewangan 		 * set with the tps->curr_vset_id */
4776219929fSLaxman Dewangan 		for (i = 0; i < 4; ++i)
4786219929fSLaxman Dewangan 			tps->lru_index[i] = i;
4796219929fSLaxman Dewangan 		tps->lru_index[0] = tps->curr_vset_id;
4806219929fSLaxman Dewangan 		tps->lru_index[tps->curr_vset_id] = 0;
4816219929fSLaxman Dewangan 	}
4826219929fSLaxman Dewangan 
4836219929fSLaxman Dewangan 	ret = tps62360_init_dcdc(tps, pdata);
4846219929fSLaxman Dewangan 	if (ret < 0) {
4852935fb18SLaxman Dewangan 		dev_err(tps->dev, "%s(): Init failed with err = %d\n",
4866219929fSLaxman Dewangan 				__func__, ret);
4876219929fSLaxman Dewangan 		goto err_init;
4886219929fSLaxman Dewangan 	}
4896219929fSLaxman Dewangan 
490c172708dSMark Brown 	config.dev = &client->dev;
4918bdca009SLaxman Dewangan 	config.init_data = pdata->reg_init_data;
492c172708dSMark Brown 	config.driver_data = tps;
493c172708dSMark Brown 
4946219929fSLaxman Dewangan 	/* Register the regulators */
495c172708dSMark Brown 	rdev = regulator_register(&tps->desc, &config);
4966219929fSLaxman Dewangan 	if (IS_ERR(rdev)) {
4972935fb18SLaxman Dewangan 		dev_err(tps->dev,
4982935fb18SLaxman Dewangan 			"%s(): regulator register failed with err %s\n",
4996219929fSLaxman Dewangan 			__func__, id->name);
5006219929fSLaxman Dewangan 		ret = PTR_ERR(rdev);
5016219929fSLaxman Dewangan 		goto err_init;
5026219929fSLaxman Dewangan 	}
5036219929fSLaxman Dewangan 
5046219929fSLaxman Dewangan 	tps->rdev = rdev;
5056219929fSLaxman Dewangan 	return 0;
5066219929fSLaxman Dewangan 
5076219929fSLaxman Dewangan err_init:
5086219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel1_gpio))
5096219929fSLaxman Dewangan 		gpio_free(tps->vsel1_gpio);
5106219929fSLaxman Dewangan err_gpio1:
5116219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio))
5126219929fSLaxman Dewangan 		gpio_free(tps->vsel0_gpio);
5136219929fSLaxman Dewangan err_gpio0:
5146219929fSLaxman Dewangan 	return ret;
5156219929fSLaxman Dewangan }
5166219929fSLaxman Dewangan 
5176219929fSLaxman Dewangan /**
5186219929fSLaxman Dewangan  * tps62360_remove - tps62360 driver i2c remove handler
5196219929fSLaxman Dewangan  * @client: i2c driver client device structure
5206219929fSLaxman Dewangan  *
5216219929fSLaxman Dewangan  * Unregister TPS driver as an i2c client device driver
5226219929fSLaxman Dewangan  */
5236219929fSLaxman Dewangan static int __devexit tps62360_remove(struct i2c_client *client)
5246219929fSLaxman Dewangan {
5256219929fSLaxman Dewangan 	struct tps62360_chip *tps = i2c_get_clientdata(client);
5266219929fSLaxman Dewangan 
5276219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel1_gpio))
5286219929fSLaxman Dewangan 		gpio_free(tps->vsel1_gpio);
5296219929fSLaxman Dewangan 
5306219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio))
5316219929fSLaxman Dewangan 		gpio_free(tps->vsel0_gpio);
5326219929fSLaxman Dewangan 
5336219929fSLaxman Dewangan 	regulator_unregister(tps->rdev);
5346219929fSLaxman Dewangan 	return 0;
5356219929fSLaxman Dewangan }
5366219929fSLaxman Dewangan 
5376219929fSLaxman Dewangan static void tps62360_shutdown(struct i2c_client *client)
5386219929fSLaxman Dewangan {
5396219929fSLaxman Dewangan 	struct tps62360_chip *tps = i2c_get_clientdata(client);
5406219929fSLaxman Dewangan 	int st;
5416219929fSLaxman Dewangan 
5426219929fSLaxman Dewangan 	if (!tps->en_discharge)
5436219929fSLaxman Dewangan 		return;
5446219929fSLaxman Dewangan 
5456219929fSLaxman Dewangan 	/* Configure the output discharge path */
5466219929fSLaxman Dewangan 	st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
5476219929fSLaxman Dewangan 	if (st < 0)
5482935fb18SLaxman Dewangan 		dev_err(tps->dev,
5492935fb18SLaxman Dewangan 			"%s(): register %d update failed with err %d\n",
5502935fb18SLaxman Dewangan 			__func__, REG_RAMPCTRL, st);
5516219929fSLaxman Dewangan }
5526219929fSLaxman Dewangan 
5536219929fSLaxman Dewangan static const struct i2c_device_id tps62360_id[] = {
5546219929fSLaxman Dewangan 	{.name = "tps62360", .driver_data = TPS62360},
5556219929fSLaxman Dewangan 	{.name = "tps62361", .driver_data = TPS62361},
556d1cf4f65SAxel Lin 	{.name = "tps62362", .driver_data = TPS62362},
557d1cf4f65SAxel Lin 	{.name = "tps62363", .driver_data = TPS62363},
5586219929fSLaxman Dewangan 	{},
5596219929fSLaxman Dewangan };
5606219929fSLaxman Dewangan 
5616219929fSLaxman Dewangan MODULE_DEVICE_TABLE(i2c, tps62360_id);
5626219929fSLaxman Dewangan 
5636219929fSLaxman Dewangan static struct i2c_driver tps62360_i2c_driver = {
5646219929fSLaxman Dewangan 	.driver = {
5656219929fSLaxman Dewangan 		.name = "tps62360",
5666219929fSLaxman Dewangan 		.owner = THIS_MODULE,
567684ae39bSLaxman Dewangan 		.of_match_table = of_match_ptr(tps62360_of_match),
5686219929fSLaxman Dewangan 	},
5696219929fSLaxman Dewangan 	.probe = tps62360_probe,
5706219929fSLaxman Dewangan 	.remove = __devexit_p(tps62360_remove),
5716219929fSLaxman Dewangan 	.shutdown = tps62360_shutdown,
5726219929fSLaxman Dewangan 	.id_table = tps62360_id,
5736219929fSLaxman Dewangan };
5746219929fSLaxman Dewangan 
5756219929fSLaxman Dewangan static int __init tps62360_init(void)
5766219929fSLaxman Dewangan {
5776219929fSLaxman Dewangan 	return i2c_add_driver(&tps62360_i2c_driver);
5786219929fSLaxman Dewangan }
5796219929fSLaxman Dewangan subsys_initcall(tps62360_init);
5806219929fSLaxman Dewangan 
5816219929fSLaxman Dewangan static void __exit tps62360_cleanup(void)
5826219929fSLaxman Dewangan {
5836219929fSLaxman Dewangan 	i2c_del_driver(&tps62360_i2c_driver);
5846219929fSLaxman Dewangan }
5856219929fSLaxman Dewangan module_exit(tps62360_cleanup);
5866219929fSLaxman Dewangan 
5876219929fSLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
588d1cf4f65SAxel Lin MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
5896219929fSLaxman Dewangan MODULE_LICENSE("GPL v2");
590