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>
296219929fSLaxman Dewangan #include <linux/platform_device.h>
306219929fSLaxman Dewangan #include <linux/regulator/driver.h>
316219929fSLaxman Dewangan #include <linux/regulator/machine.h>
326219929fSLaxman Dewangan #include <linux/regulator/tps62360.h>
336219929fSLaxman Dewangan #include <linux/gpio.h>
346219929fSLaxman Dewangan #include <linux/i2c.h>
356219929fSLaxman Dewangan #include <linux/slab.h>
366219929fSLaxman Dewangan #include <linux/regmap.h>
376219929fSLaxman Dewangan 
386219929fSLaxman Dewangan /* Register definitions */
396219929fSLaxman Dewangan #define REG_VSET0		0
406219929fSLaxman Dewangan #define REG_VSET1		1
416219929fSLaxman Dewangan #define REG_VSET2		2
426219929fSLaxman Dewangan #define REG_VSET3		3
436219929fSLaxman Dewangan #define REG_CONTROL		4
446219929fSLaxman Dewangan #define REG_TEMP		5
456219929fSLaxman Dewangan #define REG_RAMPCTRL		6
466219929fSLaxman Dewangan #define REG_CHIPID		8
476219929fSLaxman Dewangan 
48d1cf4f65SAxel Lin enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
496219929fSLaxman Dewangan 
506219929fSLaxman Dewangan #define TPS62360_BASE_VOLTAGE	770
516219929fSLaxman Dewangan #define TPS62360_N_VOLTAGES	64
526219929fSLaxman Dewangan 
536219929fSLaxman Dewangan #define TPS62361_BASE_VOLTAGE	500
546219929fSLaxman Dewangan #define TPS62361_N_VOLTAGES	128
556219929fSLaxman Dewangan 
566219929fSLaxman Dewangan /* tps 62360 chip information */
576219929fSLaxman Dewangan struct tps62360_chip {
586219929fSLaxman Dewangan 	struct device *dev;
596219929fSLaxman Dewangan 	struct regulator_desc desc;
606219929fSLaxman Dewangan 	struct regulator_dev *rdev;
616219929fSLaxman Dewangan 	struct regmap *regmap;
626219929fSLaxman Dewangan 	int chip_id;
636219929fSLaxman Dewangan 	int vsel0_gpio;
646219929fSLaxman Dewangan 	int vsel1_gpio;
656219929fSLaxman Dewangan 	int voltage_base;
666219929fSLaxman Dewangan 	u8 voltage_reg_mask;
676219929fSLaxman Dewangan 	bool en_internal_pulldn;
686219929fSLaxman Dewangan 	bool en_force_pwm;
696219929fSLaxman Dewangan 	bool en_discharge;
706219929fSLaxman Dewangan 	bool valid_gpios;
716219929fSLaxman Dewangan 	int lru_index[4];
726219929fSLaxman Dewangan 	int curr_vset_vsel[4];
736219929fSLaxman Dewangan 	int curr_vset_id;
74a60cfce0SLaxman Dewangan 	int change_uv_per_us;
756219929fSLaxman Dewangan };
766219929fSLaxman Dewangan 
776219929fSLaxman Dewangan /*
786219929fSLaxman Dewangan  * find_voltage_set_register: Find new voltage configuration register
796219929fSLaxman Dewangan  * (VSET) id.
806219929fSLaxman Dewangan  * The finding of the new VSET register will be based on the LRU mechanism.
816219929fSLaxman Dewangan  * Each VSET register will have different voltage configured . This
826219929fSLaxman Dewangan  * Function will look if any of the VSET register have requested voltage set
836219929fSLaxman Dewangan  * or not.
846219929fSLaxman Dewangan  *     - If it is already there then it will make that register as most
856219929fSLaxman Dewangan  *       recently used and return as found so that caller need not to set
866219929fSLaxman Dewangan  *       the VSET register but need to set the proper gpios to select this
876219929fSLaxman Dewangan  *       VSET register.
886219929fSLaxman Dewangan  *     - If requested voltage is not found then it will use the least
896219929fSLaxman Dewangan  *       recently mechanism to get new VSET register for new configuration
906219929fSLaxman Dewangan  *       and will return not_found so that caller need to set new VSET
916219929fSLaxman Dewangan  *       register and then gpios (both).
926219929fSLaxman Dewangan  */
936219929fSLaxman Dewangan static bool find_voltage_set_register(struct tps62360_chip *tps,
946219929fSLaxman Dewangan 		int req_vsel, int *vset_reg_id)
956219929fSLaxman Dewangan {
966219929fSLaxman Dewangan 	int i;
976219929fSLaxman Dewangan 	bool found = false;
986219929fSLaxman Dewangan 	int new_vset_reg = tps->lru_index[3];
996219929fSLaxman Dewangan 	int found_index = 3;
1006219929fSLaxman Dewangan 	for (i = 0; i < 4; ++i) {
1016219929fSLaxman Dewangan 		if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
1026219929fSLaxman Dewangan 			new_vset_reg = tps->lru_index[i];
1036219929fSLaxman Dewangan 			found_index = i;
1046219929fSLaxman Dewangan 			found = true;
1056219929fSLaxman Dewangan 			goto update_lru_index;
1066219929fSLaxman Dewangan 		}
1076219929fSLaxman Dewangan 	}
1086219929fSLaxman Dewangan 
1096219929fSLaxman Dewangan update_lru_index:
1106219929fSLaxman Dewangan 	for (i = found_index; i > 0; i--)
1116219929fSLaxman Dewangan 		tps->lru_index[i] = tps->lru_index[i - 1];
1126219929fSLaxman Dewangan 
1136219929fSLaxman Dewangan 	tps->lru_index[0] = new_vset_reg;
1146219929fSLaxman Dewangan 	*vset_reg_id = new_vset_reg;
1156219929fSLaxman Dewangan 	return found;
1166219929fSLaxman Dewangan }
1176219929fSLaxman Dewangan 
118a60cfce0SLaxman Dewangan static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
1196219929fSLaxman Dewangan {
1206219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1216219929fSLaxman Dewangan 	int vsel;
1226219929fSLaxman Dewangan 	unsigned int data;
1236219929fSLaxman Dewangan 	int ret;
1246219929fSLaxman Dewangan 
1256219929fSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
1266219929fSLaxman Dewangan 	if (ret < 0) {
1276219929fSLaxman Dewangan 		dev_err(tps->dev, "%s: Error in reading register %d\n",
1286219929fSLaxman Dewangan 			__func__, REG_VSET0 + tps->curr_vset_id);
1296219929fSLaxman Dewangan 		return ret;
1306219929fSLaxman Dewangan 	}
1316219929fSLaxman Dewangan 	vsel = (int)data & tps->voltage_reg_mask;
132a60cfce0SLaxman Dewangan 	return vsel;
1336219929fSLaxman Dewangan }
1346219929fSLaxman Dewangan 
1356219929fSLaxman Dewangan static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
1366219929fSLaxman Dewangan 	     int min_uV, int max_uV, unsigned *selector)
1376219929fSLaxman Dewangan {
1386219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1396219929fSLaxman Dewangan 	int vsel;
1406219929fSLaxman Dewangan 	int ret;
1416219929fSLaxman Dewangan 	bool found = false;
1426219929fSLaxman Dewangan 	int new_vset_id = tps->curr_vset_id;
1436219929fSLaxman Dewangan 
1446219929fSLaxman Dewangan 	if (max_uV < min_uV)
1456219929fSLaxman Dewangan 		return -EINVAL;
1466219929fSLaxman Dewangan 
1476219929fSLaxman Dewangan 	if (min_uV >
1486219929fSLaxman Dewangan 		((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
1496219929fSLaxman Dewangan 		return -EINVAL;
1506219929fSLaxman Dewangan 
1516219929fSLaxman Dewangan 	if (max_uV < tps->voltage_base * 1000)
1526219929fSLaxman Dewangan 		return -EINVAL;
1536219929fSLaxman Dewangan 
1546219929fSLaxman Dewangan 	vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 10000);
1556219929fSLaxman Dewangan 	if (selector)
1566219929fSLaxman Dewangan 		*selector = (vsel & tps->voltage_reg_mask);
1576219929fSLaxman Dewangan 
1586219929fSLaxman Dewangan 	/*
1596219929fSLaxman Dewangan 	 * If gpios are available to select the VSET register then least
1606219929fSLaxman Dewangan 	 * recently used register for new configuration.
1616219929fSLaxman Dewangan 	 */
1626219929fSLaxman Dewangan 	if (tps->valid_gpios)
1636219929fSLaxman Dewangan 		found = find_voltage_set_register(tps, vsel, &new_vset_id);
1646219929fSLaxman Dewangan 
1656219929fSLaxman Dewangan 	if (!found) {
1666219929fSLaxman Dewangan 		ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
1676219929fSLaxman Dewangan 				tps->voltage_reg_mask, vsel);
1686219929fSLaxman Dewangan 		if (ret < 0) {
1696219929fSLaxman Dewangan 			dev_err(tps->dev, "%s: Error in updating register %d\n",
1706219929fSLaxman Dewangan 				 __func__, REG_VSET0 + new_vset_id);
1716219929fSLaxman Dewangan 			return ret;
1726219929fSLaxman Dewangan 		}
1736219929fSLaxman Dewangan 		tps->curr_vset_id = new_vset_id;
1746219929fSLaxman Dewangan 		tps->curr_vset_vsel[new_vset_id] = vsel;
1756219929fSLaxman Dewangan 	}
1766219929fSLaxman Dewangan 
1776219929fSLaxman Dewangan 	/* Select proper VSET register vio gpios */
1786219929fSLaxman Dewangan 	if (tps->valid_gpios) {
1796219929fSLaxman Dewangan 		gpio_set_value_cansleep(tps->vsel0_gpio,
1806219929fSLaxman Dewangan 					new_vset_id & 0x1);
1816219929fSLaxman Dewangan 		gpio_set_value_cansleep(tps->vsel1_gpio,
1826219929fSLaxman Dewangan 					(new_vset_id >> 1) & 0x1);
1836219929fSLaxman Dewangan 	}
1846219929fSLaxman Dewangan 	return 0;
1856219929fSLaxman Dewangan }
1866219929fSLaxman Dewangan 
1876219929fSLaxman Dewangan static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
1886219929fSLaxman Dewangan 					unsigned selector)
1896219929fSLaxman Dewangan {
1906219929fSLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
1916219929fSLaxman Dewangan 
19246783a04SAxel Lin 	if (selector >= tps->desc.n_voltages)
1936219929fSLaxman Dewangan 		return -EINVAL;
1946219929fSLaxman Dewangan 	return (tps->voltage_base + selector * 10) * 1000;
1956219929fSLaxman Dewangan }
1966219929fSLaxman Dewangan 
197a60cfce0SLaxman Dewangan static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev,
198a60cfce0SLaxman Dewangan 		unsigned int old_selector, unsigned int new_selector)
199a60cfce0SLaxman Dewangan {
200a60cfce0SLaxman Dewangan 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
201a60cfce0SLaxman Dewangan 	int old_uV, new_uV;
202a60cfce0SLaxman Dewangan 
203a60cfce0SLaxman Dewangan 	old_uV = tps62360_dcdc_list_voltage(rdev, old_selector);
204a60cfce0SLaxman Dewangan 	if (old_uV < 0)
205a60cfce0SLaxman Dewangan 		return old_uV;
206a60cfce0SLaxman Dewangan 
207a60cfce0SLaxman Dewangan 	new_uV = tps62360_dcdc_list_voltage(rdev, new_selector);
208a60cfce0SLaxman Dewangan 	if (new_uV < 0)
209a60cfce0SLaxman Dewangan 		return new_uV;
210a60cfce0SLaxman Dewangan 
211a60cfce0SLaxman Dewangan 	return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
212a60cfce0SLaxman Dewangan }
213a60cfce0SLaxman Dewangan 
2146219929fSLaxman Dewangan static struct regulator_ops tps62360_dcdc_ops = {
215a60cfce0SLaxman Dewangan 	.get_voltage_sel	= tps62360_dcdc_get_voltage_sel,
2166219929fSLaxman Dewangan 	.set_voltage		= tps62360_dcdc_set_voltage,
2176219929fSLaxman Dewangan 	.list_voltage		= tps62360_dcdc_list_voltage,
218a60cfce0SLaxman Dewangan 	.set_voltage_time_sel	= tps62360_set_voltage_time_sel,
2196219929fSLaxman Dewangan };
2206219929fSLaxman Dewangan 
2216219929fSLaxman Dewangan static int tps62360_init_force_pwm(struct tps62360_chip *tps,
2226219929fSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata,
2236219929fSLaxman Dewangan 	int vset_id)
2246219929fSLaxman Dewangan {
2256219929fSLaxman Dewangan 	unsigned int data;
2266219929fSLaxman Dewangan 	int ret;
2276219929fSLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
2286219929fSLaxman Dewangan 	if (ret < 0) {
2296219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() fails in writing reg %d\n",
2306219929fSLaxman Dewangan 			__func__, REG_VSET0 + vset_id);
2316219929fSLaxman Dewangan 		return ret;
2326219929fSLaxman Dewangan 	}
2336219929fSLaxman Dewangan 	tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
2346219929fSLaxman Dewangan 	if (pdata->en_force_pwm)
2356219929fSLaxman Dewangan 		data |= BIT(7);
2366219929fSLaxman Dewangan 	else
2376219929fSLaxman Dewangan 		data &= ~BIT(7);
2386219929fSLaxman Dewangan 	ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
2396219929fSLaxman Dewangan 	if (ret < 0)
2406219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() fails in writing reg %d\n",
2416219929fSLaxman Dewangan 				__func__, REG_VSET0 + vset_id);
2426219929fSLaxman Dewangan 	return ret;
2436219929fSLaxman Dewangan }
2446219929fSLaxman Dewangan 
2456219929fSLaxman Dewangan static int tps62360_init_dcdc(struct tps62360_chip *tps,
2466219929fSLaxman Dewangan 		struct tps62360_regulator_platform_data *pdata)
2476219929fSLaxman Dewangan {
2486219929fSLaxman Dewangan 	int ret;
2496219929fSLaxman Dewangan 	int i;
250a60cfce0SLaxman Dewangan 	unsigned int ramp_ctrl;
2516219929fSLaxman Dewangan 
2526219929fSLaxman Dewangan 	/* Initailize internal pull up/down control */
2536219929fSLaxman Dewangan 	if (tps->en_internal_pulldn)
2546219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
2556219929fSLaxman Dewangan 	else
2566219929fSLaxman Dewangan 		ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
2576219929fSLaxman Dewangan 	if (ret < 0) {
2586219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() fails in writing reg %d\n",
2596219929fSLaxman Dewangan 			__func__, REG_CONTROL);
2606219929fSLaxman Dewangan 		return ret;
2616219929fSLaxman Dewangan 	}
2626219929fSLaxman Dewangan 
2636219929fSLaxman Dewangan 	/* Initailize force PWM mode */
2646219929fSLaxman Dewangan 	if (tps->valid_gpios) {
2656219929fSLaxman Dewangan 		for (i = 0; i < 4; ++i) {
2666219929fSLaxman Dewangan 			ret = tps62360_init_force_pwm(tps, pdata, i);
2676219929fSLaxman Dewangan 			if (ret < 0)
2686219929fSLaxman Dewangan 				return ret;
2696219929fSLaxman Dewangan 		}
2706219929fSLaxman Dewangan 	} else {
2716219929fSLaxman Dewangan 		ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
2726219929fSLaxman Dewangan 		if (ret < 0)
2736219929fSLaxman Dewangan 			return ret;
2746219929fSLaxman Dewangan 	}
2756219929fSLaxman Dewangan 
2766219929fSLaxman Dewangan 	/* Reset output discharge path to reduce power consumption */
2776219929fSLaxman Dewangan 	ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
278a60cfce0SLaxman Dewangan 	if (ret < 0) {
2796219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() fails in updating reg %d\n",
2806219929fSLaxman Dewangan 			__func__, REG_RAMPCTRL);
2816219929fSLaxman Dewangan 		return ret;
2826219929fSLaxman Dewangan 	}
2836219929fSLaxman Dewangan 
284a60cfce0SLaxman Dewangan 	/* Get ramp value from ramp control register */
285a60cfce0SLaxman Dewangan 	ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
286a60cfce0SLaxman Dewangan 	if (ret < 0) {
287a60cfce0SLaxman Dewangan 		dev_err(tps->dev, "%s() fails in reading reg %d\n",
288a60cfce0SLaxman Dewangan 			__func__, REG_RAMPCTRL);
289a60cfce0SLaxman Dewangan 		return ret;
290a60cfce0SLaxman Dewangan 	}
291a60cfce0SLaxman Dewangan 	ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
292a60cfce0SLaxman Dewangan 
293a60cfce0SLaxman Dewangan 	/* ramp mV/us = 32/(2^ramp_ctrl) */
294a60cfce0SLaxman Dewangan 	tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
295a60cfce0SLaxman Dewangan 	return ret;
296a60cfce0SLaxman Dewangan }
297a60cfce0SLaxman Dewangan 
2986219929fSLaxman Dewangan static const struct regmap_config tps62360_regmap_config = {
2996219929fSLaxman Dewangan 	.reg_bits		= 8,
3006219929fSLaxman Dewangan 	.val_bits		= 8,
30116ea003bSLaxman Dewangan 	.max_register		= REG_CHIPID,
30216ea003bSLaxman Dewangan 	.cache_type		= REGCACHE_RBTREE,
3036219929fSLaxman Dewangan };
3046219929fSLaxman Dewangan 
3056219929fSLaxman Dewangan static int __devinit tps62360_probe(struct i2c_client *client,
3066219929fSLaxman Dewangan 				     const struct i2c_device_id *id)
3076219929fSLaxman Dewangan {
308c172708dSMark Brown 	struct regulator_config config = { };
3096219929fSLaxman Dewangan 	struct tps62360_regulator_platform_data *pdata;
3106219929fSLaxman Dewangan 	struct regulator_dev *rdev;
3116219929fSLaxman Dewangan 	struct tps62360_chip *tps;
3126219929fSLaxman Dewangan 	int ret;
3136219929fSLaxman Dewangan 	int i;
3146219929fSLaxman Dewangan 
3156219929fSLaxman Dewangan 	pdata = client->dev.platform_data;
3166219929fSLaxman Dewangan 	if (!pdata) {
3176219929fSLaxman Dewangan 		dev_err(&client->dev, "%s() Err: Platform data not found\n",
3186219929fSLaxman Dewangan 						__func__);
3196219929fSLaxman Dewangan 		return -EIO;
3206219929fSLaxman Dewangan 	}
3216219929fSLaxman Dewangan 
3226219929fSLaxman Dewangan 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
3236219929fSLaxman Dewangan 	if (!tps) {
3246219929fSLaxman Dewangan 		dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
3256219929fSLaxman Dewangan 						__func__);
3266219929fSLaxman Dewangan 		return -ENOMEM;
3276219929fSLaxman Dewangan 	}
3286219929fSLaxman Dewangan 
3296219929fSLaxman Dewangan 	tps->en_force_pwm = pdata->en_force_pwm;
3306219929fSLaxman Dewangan 	tps->en_discharge = pdata->en_discharge;
3316219929fSLaxman Dewangan 	tps->en_internal_pulldn = pdata->en_internal_pulldn;
3326219929fSLaxman Dewangan 	tps->vsel0_gpio = pdata->vsel0_gpio;
3336219929fSLaxman Dewangan 	tps->vsel1_gpio = pdata->vsel1_gpio;
3346219929fSLaxman Dewangan 	tps->dev = &client->dev;
335d1cf4f65SAxel Lin 
336d1cf4f65SAxel Lin 	switch (id->driver_data) {
337d1cf4f65SAxel Lin 	case TPS62360:
338d1cf4f65SAxel Lin 	case TPS62362:
339d1cf4f65SAxel Lin 		tps->voltage_base = TPS62360_BASE_VOLTAGE;
340d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x3F;
341d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62360_N_VOLTAGES;
342d1cf4f65SAxel Lin 		break;
343d1cf4f65SAxel Lin 	case TPS62361:
344d1cf4f65SAxel Lin 	case TPS62363:
345d1cf4f65SAxel Lin 		tps->voltage_base = TPS62361_BASE_VOLTAGE;
346d1cf4f65SAxel Lin 		tps->voltage_reg_mask = 0x7F;
347d1cf4f65SAxel Lin 		tps->desc.n_voltages = TPS62361_N_VOLTAGES;
348d1cf4f65SAxel Lin 		break;
349d1cf4f65SAxel Lin 	default:
350d1cf4f65SAxel Lin 		return -ENODEV;
351d1cf4f65SAxel Lin 	}
3526219929fSLaxman Dewangan 
3536219929fSLaxman Dewangan 	tps->desc.name = id->name;
3546219929fSLaxman Dewangan 	tps->desc.id = 0;
3556219929fSLaxman Dewangan 	tps->desc.ops = &tps62360_dcdc_ops;
3566219929fSLaxman Dewangan 	tps->desc.type = REGULATOR_VOLTAGE;
3576219929fSLaxman Dewangan 	tps->desc.owner = THIS_MODULE;
3589a4bdd87SAxel Lin 	tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
3596219929fSLaxman Dewangan 	if (IS_ERR(tps->regmap)) {
3606219929fSLaxman Dewangan 		ret = PTR_ERR(tps->regmap);
3616219929fSLaxman Dewangan 		dev_err(&client->dev, "%s() Err: Failed to allocate register"
3626219929fSLaxman Dewangan 			"map: %d\n", __func__, ret);
3636219929fSLaxman Dewangan 		return ret;
3646219929fSLaxman Dewangan 	}
3656219929fSLaxman Dewangan 	i2c_set_clientdata(client, tps);
3666219929fSLaxman Dewangan 
3676219929fSLaxman Dewangan 	tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
3686219929fSLaxman Dewangan 				(pdata->vsel0_def_state & 1);
3696219929fSLaxman Dewangan 	tps->lru_index[0] = tps->curr_vset_id;
3706219929fSLaxman Dewangan 	tps->valid_gpios = false;
3716219929fSLaxman Dewangan 
3726219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
3736219929fSLaxman Dewangan 		ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
3746219929fSLaxman Dewangan 		if (ret) {
3756219929fSLaxman Dewangan 			dev_err(&client->dev,
3766219929fSLaxman Dewangan 				"Err: Could not obtain vsel0 GPIO %d: %d\n",
3776219929fSLaxman Dewangan 						tps->vsel0_gpio, ret);
3786219929fSLaxman Dewangan 			goto err_gpio0;
3796219929fSLaxman Dewangan 		}
3806219929fSLaxman Dewangan 		ret = gpio_direction_output(tps->vsel0_gpio,
3816219929fSLaxman Dewangan 					pdata->vsel0_def_state);
3826219929fSLaxman Dewangan 		if (ret) {
3836219929fSLaxman Dewangan 			dev_err(&client->dev, "Err: Could not set direction of"
3846219929fSLaxman Dewangan 				"vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
3856219929fSLaxman Dewangan 			gpio_free(tps->vsel0_gpio);
3866219929fSLaxman Dewangan 			goto err_gpio0;
3876219929fSLaxman Dewangan 		}
3886219929fSLaxman Dewangan 
3896219929fSLaxman Dewangan 		ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
3906219929fSLaxman Dewangan 		if (ret) {
3916219929fSLaxman Dewangan 			dev_err(&client->dev,
3926219929fSLaxman Dewangan 				"Err: Could not obtain vsel1 GPIO %d: %d\n",
3936219929fSLaxman Dewangan 						tps->vsel1_gpio, ret);
3946219929fSLaxman Dewangan 			goto err_gpio1;
3956219929fSLaxman Dewangan 		}
3966219929fSLaxman Dewangan 		ret = gpio_direction_output(tps->vsel1_gpio,
3976219929fSLaxman Dewangan 					pdata->vsel1_def_state);
3986219929fSLaxman Dewangan 		if (ret) {
3996219929fSLaxman Dewangan 			dev_err(&client->dev, "Err: Could not set direction of"
4006219929fSLaxman Dewangan 				"vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
4016219929fSLaxman Dewangan 			gpio_free(tps->vsel1_gpio);
4026219929fSLaxman Dewangan 			goto err_gpio1;
4036219929fSLaxman Dewangan 		}
4046219929fSLaxman Dewangan 		tps->valid_gpios = true;
4056219929fSLaxman Dewangan 
4066219929fSLaxman Dewangan 		/*
4076219929fSLaxman Dewangan 		 * Initialize the lru index with vset_reg id
4086219929fSLaxman Dewangan 		 * The index 0 will be most recently used and
4096219929fSLaxman Dewangan 		 * set with the tps->curr_vset_id */
4106219929fSLaxman Dewangan 		for (i = 0; i < 4; ++i)
4116219929fSLaxman Dewangan 			tps->lru_index[i] = i;
4126219929fSLaxman Dewangan 		tps->lru_index[0] = tps->curr_vset_id;
4136219929fSLaxman Dewangan 		tps->lru_index[tps->curr_vset_id] = 0;
4146219929fSLaxman Dewangan 	}
4156219929fSLaxman Dewangan 
4166219929fSLaxman Dewangan 	ret = tps62360_init_dcdc(tps, pdata);
4176219929fSLaxman Dewangan 	if (ret < 0) {
4186219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
4196219929fSLaxman Dewangan 				__func__, ret);
4206219929fSLaxman Dewangan 		goto err_init;
4216219929fSLaxman Dewangan 	}
4226219929fSLaxman Dewangan 
423c172708dSMark Brown 	config.dev = &client->dev;
424c172708dSMark Brown 	config.init_data = &pdata->reg_init_data;
425c172708dSMark Brown 	config.driver_data = tps;
426c172708dSMark Brown 
4276219929fSLaxman Dewangan 	/* Register the regulators */
428c172708dSMark Brown 	rdev = regulator_register(&tps->desc, &config);
4296219929fSLaxman Dewangan 	if (IS_ERR(rdev)) {
4306219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() Err: Failed to register %s\n",
4316219929fSLaxman Dewangan 				__func__, id->name);
4326219929fSLaxman Dewangan 		ret = PTR_ERR(rdev);
4336219929fSLaxman Dewangan 		goto err_init;
4346219929fSLaxman Dewangan 	}
4356219929fSLaxman Dewangan 
4366219929fSLaxman Dewangan 	tps->rdev = rdev;
4376219929fSLaxman Dewangan 	return 0;
4386219929fSLaxman Dewangan 
4396219929fSLaxman Dewangan err_init:
4406219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel1_gpio))
4416219929fSLaxman Dewangan 		gpio_free(tps->vsel1_gpio);
4426219929fSLaxman Dewangan err_gpio1:
4436219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio))
4446219929fSLaxman Dewangan 		gpio_free(tps->vsel0_gpio);
4456219929fSLaxman Dewangan err_gpio0:
4466219929fSLaxman Dewangan 	return ret;
4476219929fSLaxman Dewangan }
4486219929fSLaxman Dewangan 
4496219929fSLaxman Dewangan /**
4506219929fSLaxman Dewangan  * tps62360_remove - tps62360 driver i2c remove handler
4516219929fSLaxman Dewangan  * @client: i2c driver client device structure
4526219929fSLaxman Dewangan  *
4536219929fSLaxman Dewangan  * Unregister TPS driver as an i2c client device driver
4546219929fSLaxman Dewangan  */
4556219929fSLaxman Dewangan static int __devexit tps62360_remove(struct i2c_client *client)
4566219929fSLaxman Dewangan {
4576219929fSLaxman Dewangan 	struct tps62360_chip *tps = i2c_get_clientdata(client);
4586219929fSLaxman Dewangan 
4596219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel1_gpio))
4606219929fSLaxman Dewangan 		gpio_free(tps->vsel1_gpio);
4616219929fSLaxman Dewangan 
4626219929fSLaxman Dewangan 	if (gpio_is_valid(tps->vsel0_gpio))
4636219929fSLaxman Dewangan 		gpio_free(tps->vsel0_gpio);
4646219929fSLaxman Dewangan 
4656219929fSLaxman Dewangan 	regulator_unregister(tps->rdev);
4666219929fSLaxman Dewangan 	return 0;
4676219929fSLaxman Dewangan }
4686219929fSLaxman Dewangan 
4696219929fSLaxman Dewangan static void tps62360_shutdown(struct i2c_client *client)
4706219929fSLaxman Dewangan {
4716219929fSLaxman Dewangan 	struct tps62360_chip *tps = i2c_get_clientdata(client);
4726219929fSLaxman Dewangan 	int st;
4736219929fSLaxman Dewangan 
4746219929fSLaxman Dewangan 	if (!tps->en_discharge)
4756219929fSLaxman Dewangan 		return;
4766219929fSLaxman Dewangan 
4776219929fSLaxman Dewangan 	/* Configure the output discharge path */
4786219929fSLaxman Dewangan 	st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
4796219929fSLaxman Dewangan 	if (st < 0)
4806219929fSLaxman Dewangan 		dev_err(tps->dev, "%s() fails in updating reg %d\n",
4816219929fSLaxman Dewangan 			__func__, REG_RAMPCTRL);
4826219929fSLaxman Dewangan }
4836219929fSLaxman Dewangan 
4846219929fSLaxman Dewangan static const struct i2c_device_id tps62360_id[] = {
4856219929fSLaxman Dewangan 	{.name = "tps62360", .driver_data = TPS62360},
4866219929fSLaxman Dewangan 	{.name = "tps62361", .driver_data = TPS62361},
487d1cf4f65SAxel Lin 	{.name = "tps62362", .driver_data = TPS62362},
488d1cf4f65SAxel Lin 	{.name = "tps62363", .driver_data = TPS62363},
4896219929fSLaxman Dewangan 	{},
4906219929fSLaxman Dewangan };
4916219929fSLaxman Dewangan 
4926219929fSLaxman Dewangan MODULE_DEVICE_TABLE(i2c, tps62360_id);
4936219929fSLaxman Dewangan 
4946219929fSLaxman Dewangan static struct i2c_driver tps62360_i2c_driver = {
4956219929fSLaxman Dewangan 	.driver = {
4966219929fSLaxman Dewangan 		.name = "tps62360",
4976219929fSLaxman Dewangan 		.owner = THIS_MODULE,
4986219929fSLaxman Dewangan 	},
4996219929fSLaxman Dewangan 	.probe = tps62360_probe,
5006219929fSLaxman Dewangan 	.remove = __devexit_p(tps62360_remove),
5016219929fSLaxman Dewangan 	.shutdown = tps62360_shutdown,
5026219929fSLaxman Dewangan 	.id_table = tps62360_id,
5036219929fSLaxman Dewangan };
5046219929fSLaxman Dewangan 
5056219929fSLaxman Dewangan static int __init tps62360_init(void)
5066219929fSLaxman Dewangan {
5076219929fSLaxman Dewangan 	return i2c_add_driver(&tps62360_i2c_driver);
5086219929fSLaxman Dewangan }
5096219929fSLaxman Dewangan subsys_initcall(tps62360_init);
5106219929fSLaxman Dewangan 
5116219929fSLaxman Dewangan static void __exit tps62360_cleanup(void)
5126219929fSLaxman Dewangan {
5136219929fSLaxman Dewangan 	i2c_del_driver(&tps62360_i2c_driver);
5146219929fSLaxman Dewangan }
5156219929fSLaxman Dewangan module_exit(tps62360_cleanup);
5166219929fSLaxman Dewangan 
5176219929fSLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
518d1cf4f65SAxel Lin MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
5196219929fSLaxman Dewangan MODULE_LICENSE("GPL v2");
520