xref: /OK3568_Linux_fs/kernel/drivers/regulator/tps62360-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * tps62360.c -- TI tps62360
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2012, NVIDIA Corporation.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Author: Laxman Dewangan <ldewangan@nvidia.com>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
11*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License as
12*4882a593Smuzhiyun  * published by the Free Software Foundation version 2.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15*4882a593Smuzhiyun  * whether express or implied; without even the implied warranty of
16*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17*4882a593Smuzhiyun  * General Public License for more details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
21*4882a593Smuzhiyun  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22*4882a593Smuzhiyun  * 02111-1307, USA
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <linux/kernel.h>
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/init.h>
28*4882a593Smuzhiyun #include <linux/err.h>
29*4882a593Smuzhiyun #include <linux/of.h>
30*4882a593Smuzhiyun #include <linux/of_device.h>
31*4882a593Smuzhiyun #include <linux/of_gpio.h>
32*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
33*4882a593Smuzhiyun #include <linux/platform_device.h>
34*4882a593Smuzhiyun #include <linux/regulator/driver.h>
35*4882a593Smuzhiyun #include <linux/regulator/machine.h>
36*4882a593Smuzhiyun #include <linux/regulator/tps62360.h>
37*4882a593Smuzhiyun #include <linux/gpio.h>
38*4882a593Smuzhiyun #include <linux/i2c.h>
39*4882a593Smuzhiyun #include <linux/slab.h>
40*4882a593Smuzhiyun #include <linux/regmap.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* Register definitions */
43*4882a593Smuzhiyun #define REG_VSET0		0
44*4882a593Smuzhiyun #define REG_VSET1		1
45*4882a593Smuzhiyun #define REG_VSET2		2
46*4882a593Smuzhiyun #define REG_VSET3		3
47*4882a593Smuzhiyun #define REG_CONTROL		4
48*4882a593Smuzhiyun #define REG_TEMP		5
49*4882a593Smuzhiyun #define REG_RAMPCTRL		6
50*4882a593Smuzhiyun #define REG_CHIPID		8
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define FORCE_PWM_ENABLE	BIT(7)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define TPS62360_BASE_VOLTAGE	770000
57*4882a593Smuzhiyun #define TPS62360_N_VOLTAGES	64
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define TPS62361_BASE_VOLTAGE	500000
60*4882a593Smuzhiyun #define TPS62361_N_VOLTAGES	128
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* tps 62360 chip information */
63*4882a593Smuzhiyun struct tps62360_chip {
64*4882a593Smuzhiyun 	struct device *dev;
65*4882a593Smuzhiyun 	struct regulator_desc desc;
66*4882a593Smuzhiyun 	struct regulator_dev *rdev;
67*4882a593Smuzhiyun 	struct regmap *regmap;
68*4882a593Smuzhiyun 	int vsel0_gpio;
69*4882a593Smuzhiyun 	int vsel1_gpio;
70*4882a593Smuzhiyun 	u8 voltage_reg_mask;
71*4882a593Smuzhiyun 	bool en_internal_pulldn;
72*4882a593Smuzhiyun 	bool en_discharge;
73*4882a593Smuzhiyun 	bool valid_gpios;
74*4882a593Smuzhiyun 	int lru_index[4];
75*4882a593Smuzhiyun 	int curr_vset_vsel[4];
76*4882a593Smuzhiyun 	int curr_vset_id;
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * find_voltage_set_register: Find new voltage configuration register
81*4882a593Smuzhiyun  * (VSET) id.
82*4882a593Smuzhiyun  * The finding of the new VSET register will be based on the LRU mechanism.
83*4882a593Smuzhiyun  * Each VSET register will have different voltage configured . This
84*4882a593Smuzhiyun  * Function will look if any of the VSET register have requested voltage set
85*4882a593Smuzhiyun  * or not.
86*4882a593Smuzhiyun  *     - If it is already there then it will make that register as most
87*4882a593Smuzhiyun  *       recently used and return as found so that caller need not to set
88*4882a593Smuzhiyun  *       the VSET register but need to set the proper gpios to select this
89*4882a593Smuzhiyun  *       VSET register.
90*4882a593Smuzhiyun  *     - If requested voltage is not found then it will use the least
91*4882a593Smuzhiyun  *       recently mechanism to get new VSET register for new configuration
92*4882a593Smuzhiyun  *       and will return not_found so that caller need to set new VSET
93*4882a593Smuzhiyun  *       register and then gpios (both).
94*4882a593Smuzhiyun  */
find_voltage_set_register(struct tps62360_chip * tps,int req_vsel,int * vset_reg_id)95*4882a593Smuzhiyun static bool find_voltage_set_register(struct tps62360_chip *tps,
96*4882a593Smuzhiyun 		int req_vsel, int *vset_reg_id)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	int i;
99*4882a593Smuzhiyun 	bool found = false;
100*4882a593Smuzhiyun 	int new_vset_reg = tps->lru_index[3];
101*4882a593Smuzhiyun 	int found_index = 3;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	for (i = 0; i < 4; ++i) {
104*4882a593Smuzhiyun 		if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
105*4882a593Smuzhiyun 			new_vset_reg = tps->lru_index[i];
106*4882a593Smuzhiyun 			found_index = i;
107*4882a593Smuzhiyun 			found = true;
108*4882a593Smuzhiyun 			goto update_lru_index;
109*4882a593Smuzhiyun 		}
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun update_lru_index:
113*4882a593Smuzhiyun 	for (i = found_index; i > 0; i--)
114*4882a593Smuzhiyun 		tps->lru_index[i] = tps->lru_index[i - 1];
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	tps->lru_index[0] = new_vset_reg;
117*4882a593Smuzhiyun 	*vset_reg_id = new_vset_reg;
118*4882a593Smuzhiyun 	return found;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
tps62360_dcdc_get_voltage_sel(struct regulator_dev * dev)121*4882a593Smuzhiyun static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
124*4882a593Smuzhiyun 	int vsel;
125*4882a593Smuzhiyun 	unsigned int data;
126*4882a593Smuzhiyun 	int ret;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
129*4882a593Smuzhiyun 	if (ret < 0) {
130*4882a593Smuzhiyun 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
131*4882a593Smuzhiyun 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
132*4882a593Smuzhiyun 		return ret;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 	vsel = (int)data & tps->voltage_reg_mask;
135*4882a593Smuzhiyun 	return vsel;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
tps62360_dcdc_set_voltage_sel(struct regulator_dev * dev,unsigned selector)138*4882a593Smuzhiyun static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
139*4882a593Smuzhiyun 					 unsigned selector)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	struct tps62360_chip *tps = rdev_get_drvdata(dev);
142*4882a593Smuzhiyun 	int ret;
143*4882a593Smuzhiyun 	bool found = false;
144*4882a593Smuzhiyun 	int new_vset_id = tps->curr_vset_id;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/*
147*4882a593Smuzhiyun 	 * If gpios are available to select the VSET register then least
148*4882a593Smuzhiyun 	 * recently used register for new configuration.
149*4882a593Smuzhiyun 	 */
150*4882a593Smuzhiyun 	if (tps->valid_gpios)
151*4882a593Smuzhiyun 		found = find_voltage_set_register(tps, selector, &new_vset_id);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (!found) {
154*4882a593Smuzhiyun 		ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
155*4882a593Smuzhiyun 				tps->voltage_reg_mask, selector);
156*4882a593Smuzhiyun 		if (ret < 0) {
157*4882a593Smuzhiyun 			dev_err(tps->dev,
158*4882a593Smuzhiyun 				"%s(): register %d update failed with err %d\n",
159*4882a593Smuzhiyun 				 __func__, REG_VSET0 + new_vset_id, ret);
160*4882a593Smuzhiyun 			return ret;
161*4882a593Smuzhiyun 		}
162*4882a593Smuzhiyun 		tps->curr_vset_id = new_vset_id;
163*4882a593Smuzhiyun 		tps->curr_vset_vsel[new_vset_id] = selector;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* Select proper VSET register vio gpios */
167*4882a593Smuzhiyun 	if (tps->valid_gpios) {
168*4882a593Smuzhiyun 		gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
169*4882a593Smuzhiyun 		gpio_set_value_cansleep(tps->vsel1_gpio,
170*4882a593Smuzhiyun 					(new_vset_id >> 1) & 0x1);
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 	return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
tps62360_set_mode(struct regulator_dev * rdev,unsigned int mode)175*4882a593Smuzhiyun static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
178*4882a593Smuzhiyun 	int i;
179*4882a593Smuzhiyun 	int val;
180*4882a593Smuzhiyun 	int ret;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* Enable force PWM mode in FAST mode only. */
183*4882a593Smuzhiyun 	switch (mode) {
184*4882a593Smuzhiyun 	case REGULATOR_MODE_FAST:
185*4882a593Smuzhiyun 		val = FORCE_PWM_ENABLE;
186*4882a593Smuzhiyun 		break;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	case REGULATOR_MODE_NORMAL:
189*4882a593Smuzhiyun 		val = 0;
190*4882a593Smuzhiyun 		break;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	default:
193*4882a593Smuzhiyun 		return -EINVAL;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if (!tps->valid_gpios) {
197*4882a593Smuzhiyun 		ret = regmap_update_bits(tps->regmap,
198*4882a593Smuzhiyun 			REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
199*4882a593Smuzhiyun 		if (ret < 0)
200*4882a593Smuzhiyun 			dev_err(tps->dev,
201*4882a593Smuzhiyun 				"%s(): register %d update failed with err %d\n",
202*4882a593Smuzhiyun 				__func__, REG_VSET0 + tps->curr_vset_id, ret);
203*4882a593Smuzhiyun 		return ret;
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* If gpios are valid then all register set need to be control */
207*4882a593Smuzhiyun 	for (i = 0; i < 4; ++i) {
208*4882a593Smuzhiyun 		ret = regmap_update_bits(tps->regmap,
209*4882a593Smuzhiyun 					REG_VSET0 + i, FORCE_PWM_ENABLE, val);
210*4882a593Smuzhiyun 		if (ret < 0) {
211*4882a593Smuzhiyun 			dev_err(tps->dev,
212*4882a593Smuzhiyun 				"%s(): register %d update failed with err %d\n",
213*4882a593Smuzhiyun 				__func__, REG_VSET0 + i, ret);
214*4882a593Smuzhiyun 			return ret;
215*4882a593Smuzhiyun 		}
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 	return ret;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
tps62360_get_mode(struct regulator_dev * rdev)220*4882a593Smuzhiyun static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct tps62360_chip *tps = rdev_get_drvdata(rdev);
223*4882a593Smuzhiyun 	unsigned int data;
224*4882a593Smuzhiyun 	int ret;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
227*4882a593Smuzhiyun 	if (ret < 0) {
228*4882a593Smuzhiyun 		dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
229*4882a593Smuzhiyun 			__func__, REG_VSET0 + tps->curr_vset_id, ret);
230*4882a593Smuzhiyun 		return ret;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 	return (data & FORCE_PWM_ENABLE) ?
233*4882a593Smuzhiyun 				REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun static const struct regulator_ops tps62360_dcdc_ops = {
237*4882a593Smuzhiyun 	.get_voltage_sel	= tps62360_dcdc_get_voltage_sel,
238*4882a593Smuzhiyun 	.set_voltage_sel	= tps62360_dcdc_set_voltage_sel,
239*4882a593Smuzhiyun 	.list_voltage		= regulator_list_voltage_linear,
240*4882a593Smuzhiyun 	.map_voltage		= regulator_map_voltage_linear,
241*4882a593Smuzhiyun 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
242*4882a593Smuzhiyun 	.set_mode		= tps62360_set_mode,
243*4882a593Smuzhiyun 	.get_mode		= tps62360_get_mode,
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun 
tps62360_init_dcdc(struct tps62360_chip * tps,struct tps62360_regulator_platform_data * pdata)246*4882a593Smuzhiyun static int tps62360_init_dcdc(struct tps62360_chip *tps,
247*4882a593Smuzhiyun 		struct tps62360_regulator_platform_data *pdata)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	int ret;
250*4882a593Smuzhiyun 	unsigned int ramp_ctrl;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* Initialize internal pull up/down control */
253*4882a593Smuzhiyun 	if (tps->en_internal_pulldn)
254*4882a593Smuzhiyun 		ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
255*4882a593Smuzhiyun 	else
256*4882a593Smuzhiyun 		ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
257*4882a593Smuzhiyun 	if (ret < 0) {
258*4882a593Smuzhiyun 		dev_err(tps->dev,
259*4882a593Smuzhiyun 			"%s(): register %d write failed with err %d\n",
260*4882a593Smuzhiyun 			__func__, REG_CONTROL, ret);
261*4882a593Smuzhiyun 		return ret;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/* Reset output discharge path to reduce power consumption */
265*4882a593Smuzhiyun 	ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
266*4882a593Smuzhiyun 	if (ret < 0) {
267*4882a593Smuzhiyun 		dev_err(tps->dev,
268*4882a593Smuzhiyun 			"%s(): register %d update failed with err %d\n",
269*4882a593Smuzhiyun 			__func__, REG_RAMPCTRL, ret);
270*4882a593Smuzhiyun 		return ret;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* Get ramp value from ramp control register */
274*4882a593Smuzhiyun 	ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
275*4882a593Smuzhiyun 	if (ret < 0) {
276*4882a593Smuzhiyun 		dev_err(tps->dev,
277*4882a593Smuzhiyun 			"%s(): register %d read failed with err %d\n",
278*4882a593Smuzhiyun 			__func__, REG_RAMPCTRL, ret);
279*4882a593Smuzhiyun 		return ret;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 	ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	/* ramp mV/us = 32/(2^ramp_ctrl) */
284*4882a593Smuzhiyun 	tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
285*4882a593Smuzhiyun 	return ret;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun static const struct regmap_config tps62360_regmap_config = {
289*4882a593Smuzhiyun 	.reg_bits		= 8,
290*4882a593Smuzhiyun 	.val_bits		= 8,
291*4882a593Smuzhiyun 	.max_register		= REG_CHIPID,
292*4882a593Smuzhiyun 	.cache_type		= REGCACHE_RBTREE,
293*4882a593Smuzhiyun };
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun static struct tps62360_regulator_platform_data *
of_get_tps62360_platform_data(struct device * dev,const struct regulator_desc * desc)296*4882a593Smuzhiyun 	of_get_tps62360_platform_data(struct device *dev,
297*4882a593Smuzhiyun 				      const struct regulator_desc *desc)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct tps62360_regulator_platform_data *pdata;
300*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
303*4882a593Smuzhiyun 	if (!pdata)
304*4882a593Smuzhiyun 		return NULL;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
307*4882a593Smuzhiyun 							  desc);
308*4882a593Smuzhiyun 	if (!pdata->reg_init_data) {
309*4882a593Smuzhiyun 		dev_err(dev, "Not able to get OF regulator init data\n");
310*4882a593Smuzhiyun 		return NULL;
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
314*4882a593Smuzhiyun 	pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	if (of_find_property(np, "ti,vsel0-state-high", NULL))
317*4882a593Smuzhiyun 		pdata->vsel0_def_state = 1;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (of_find_property(np, "ti,vsel1-state-high", NULL))
320*4882a593Smuzhiyun 		pdata->vsel1_def_state = 1;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (of_find_property(np, "ti,enable-pull-down", NULL))
323*4882a593Smuzhiyun 		pdata->en_internal_pulldn = true;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (of_find_property(np, "ti,enable-vout-discharge", NULL))
326*4882a593Smuzhiyun 		pdata->en_discharge = true;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	return pdata;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun #if defined(CONFIG_OF)
332*4882a593Smuzhiyun static const struct of_device_id tps62360_of_match[] = {
333*4882a593Smuzhiyun 	 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
334*4882a593Smuzhiyun 	 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
335*4882a593Smuzhiyun 	 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
336*4882a593Smuzhiyun 	 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
337*4882a593Smuzhiyun 	{},
338*4882a593Smuzhiyun };
339*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tps62360_of_match);
340*4882a593Smuzhiyun #endif
341*4882a593Smuzhiyun 
tps62360_probe(struct i2c_client * client,const struct i2c_device_id * id)342*4882a593Smuzhiyun static int tps62360_probe(struct i2c_client *client,
343*4882a593Smuzhiyun 				     const struct i2c_device_id *id)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct regulator_config config = { };
346*4882a593Smuzhiyun 	struct tps62360_regulator_platform_data *pdata;
347*4882a593Smuzhiyun 	struct regulator_dev *rdev;
348*4882a593Smuzhiyun 	struct tps62360_chip *tps;
349*4882a593Smuzhiyun 	int ret;
350*4882a593Smuzhiyun 	int i;
351*4882a593Smuzhiyun 	int chip_id;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	pdata = dev_get_platdata(&client->dev);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
356*4882a593Smuzhiyun 	if (!tps)
357*4882a593Smuzhiyun 		return -ENOMEM;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	tps->desc.name = client->name;
360*4882a593Smuzhiyun 	tps->desc.id = 0;
361*4882a593Smuzhiyun 	tps->desc.ops = &tps62360_dcdc_ops;
362*4882a593Smuzhiyun 	tps->desc.type = REGULATOR_VOLTAGE;
363*4882a593Smuzhiyun 	tps->desc.owner = THIS_MODULE;
364*4882a593Smuzhiyun 	tps->desc.uV_step = 10000;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (client->dev.of_node) {
367*4882a593Smuzhiyun 		const struct of_device_id *match;
368*4882a593Smuzhiyun 		match = of_match_device(of_match_ptr(tps62360_of_match),
369*4882a593Smuzhiyun 				&client->dev);
370*4882a593Smuzhiyun 		if (!match) {
371*4882a593Smuzhiyun 			dev_err(&client->dev, "Error: No device match found\n");
372*4882a593Smuzhiyun 			return -ENODEV;
373*4882a593Smuzhiyun 		}
374*4882a593Smuzhiyun 		chip_id = (int)(long)match->data;
375*4882a593Smuzhiyun 		if (!pdata)
376*4882a593Smuzhiyun 			pdata = of_get_tps62360_platform_data(&client->dev,
377*4882a593Smuzhiyun 							      &tps->desc);
378*4882a593Smuzhiyun 	} else if (id) {
379*4882a593Smuzhiyun 		chip_id = id->driver_data;
380*4882a593Smuzhiyun 	} else {
381*4882a593Smuzhiyun 		dev_err(&client->dev, "No device tree match or id table match found\n");
382*4882a593Smuzhiyun 		return -ENODEV;
383*4882a593Smuzhiyun 	}
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (!pdata) {
386*4882a593Smuzhiyun 		dev_err(&client->dev, "%s(): Platform data not found\n",
387*4882a593Smuzhiyun 						__func__);
388*4882a593Smuzhiyun 		return -EIO;
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	tps->en_discharge = pdata->en_discharge;
392*4882a593Smuzhiyun 	tps->en_internal_pulldn = pdata->en_internal_pulldn;
393*4882a593Smuzhiyun 	tps->vsel0_gpio = pdata->vsel0_gpio;
394*4882a593Smuzhiyun 	tps->vsel1_gpio = pdata->vsel1_gpio;
395*4882a593Smuzhiyun 	tps->dev = &client->dev;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	switch (chip_id) {
398*4882a593Smuzhiyun 	case TPS62360:
399*4882a593Smuzhiyun 	case TPS62362:
400*4882a593Smuzhiyun 		tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
401*4882a593Smuzhiyun 		tps->voltage_reg_mask = 0x3F;
402*4882a593Smuzhiyun 		tps->desc.n_voltages = TPS62360_N_VOLTAGES;
403*4882a593Smuzhiyun 		break;
404*4882a593Smuzhiyun 	case TPS62361:
405*4882a593Smuzhiyun 	case TPS62363:
406*4882a593Smuzhiyun 		tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
407*4882a593Smuzhiyun 		tps->voltage_reg_mask = 0x7F;
408*4882a593Smuzhiyun 		tps->desc.n_voltages = TPS62361_N_VOLTAGES;
409*4882a593Smuzhiyun 		break;
410*4882a593Smuzhiyun 	default:
411*4882a593Smuzhiyun 		return -ENODEV;
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
415*4882a593Smuzhiyun 	if (IS_ERR(tps->regmap)) {
416*4882a593Smuzhiyun 		ret = PTR_ERR(tps->regmap);
417*4882a593Smuzhiyun 		dev_err(&client->dev,
418*4882a593Smuzhiyun 			"%s(): regmap allocation failed with err %d\n",
419*4882a593Smuzhiyun 			__func__, ret);
420*4882a593Smuzhiyun 		return ret;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 	i2c_set_clientdata(client, tps);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
425*4882a593Smuzhiyun 				(pdata->vsel0_def_state & 1);
426*4882a593Smuzhiyun 	tps->lru_index[0] = tps->curr_vset_id;
427*4882a593Smuzhiyun 	tps->valid_gpios = false;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
430*4882a593Smuzhiyun 		int gpio_flags;
431*4882a593Smuzhiyun 		gpio_flags = (pdata->vsel0_def_state) ?
432*4882a593Smuzhiyun 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
433*4882a593Smuzhiyun 		ret = devm_gpio_request_one(&client->dev, tps->vsel0_gpio,
434*4882a593Smuzhiyun 				gpio_flags, "tps62360-vsel0");
435*4882a593Smuzhiyun 		if (ret) {
436*4882a593Smuzhiyun 			dev_err(&client->dev,
437*4882a593Smuzhiyun 				"%s(): Could not obtain vsel0 GPIO %d: %d\n",
438*4882a593Smuzhiyun 				__func__, tps->vsel0_gpio, ret);
439*4882a593Smuzhiyun 			return ret;
440*4882a593Smuzhiyun 		}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 		gpio_flags = (pdata->vsel1_def_state) ?
443*4882a593Smuzhiyun 				GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
444*4882a593Smuzhiyun 		ret = devm_gpio_request_one(&client->dev, tps->vsel1_gpio,
445*4882a593Smuzhiyun 				gpio_flags, "tps62360-vsel1");
446*4882a593Smuzhiyun 		if (ret) {
447*4882a593Smuzhiyun 			dev_err(&client->dev,
448*4882a593Smuzhiyun 				"%s(): Could not obtain vsel1 GPIO %d: %d\n",
449*4882a593Smuzhiyun 				__func__, tps->vsel1_gpio, ret);
450*4882a593Smuzhiyun 			return ret;
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 		tps->valid_gpios = true;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		/*
455*4882a593Smuzhiyun 		 * Initialize the lru index with vset_reg id
456*4882a593Smuzhiyun 		 * The index 0 will be most recently used and
457*4882a593Smuzhiyun 		 * set with the tps->curr_vset_id */
458*4882a593Smuzhiyun 		for (i = 0; i < 4; ++i)
459*4882a593Smuzhiyun 			tps->lru_index[i] = i;
460*4882a593Smuzhiyun 		tps->lru_index[0] = tps->curr_vset_id;
461*4882a593Smuzhiyun 		tps->lru_index[tps->curr_vset_id] = 0;
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	ret = tps62360_init_dcdc(tps, pdata);
465*4882a593Smuzhiyun 	if (ret < 0) {
466*4882a593Smuzhiyun 		dev_err(tps->dev, "%s(): Init failed with err = %d\n",
467*4882a593Smuzhiyun 				__func__, ret);
468*4882a593Smuzhiyun 		return ret;
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	config.dev = &client->dev;
472*4882a593Smuzhiyun 	config.init_data = pdata->reg_init_data;
473*4882a593Smuzhiyun 	config.driver_data = tps;
474*4882a593Smuzhiyun 	config.of_node = client->dev.of_node;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	/* Register the regulators */
477*4882a593Smuzhiyun 	rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
478*4882a593Smuzhiyun 	if (IS_ERR(rdev)) {
479*4882a593Smuzhiyun 		dev_err(tps->dev,
480*4882a593Smuzhiyun 			"%s(): regulator register failed with err %s\n",
481*4882a593Smuzhiyun 			__func__, id->name);
482*4882a593Smuzhiyun 		return PTR_ERR(rdev);
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	tps->rdev = rdev;
486*4882a593Smuzhiyun 	return 0;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
tps62360_shutdown(struct i2c_client * client)489*4882a593Smuzhiyun static void tps62360_shutdown(struct i2c_client *client)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun 	struct tps62360_chip *tps = i2c_get_clientdata(client);
492*4882a593Smuzhiyun 	int st;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	if (!tps->en_discharge)
495*4882a593Smuzhiyun 		return;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	/* Configure the output discharge path */
498*4882a593Smuzhiyun 	st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
499*4882a593Smuzhiyun 	if (st < 0)
500*4882a593Smuzhiyun 		dev_err(tps->dev,
501*4882a593Smuzhiyun 			"%s(): register %d update failed with err %d\n",
502*4882a593Smuzhiyun 			__func__, REG_RAMPCTRL, st);
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun static const struct i2c_device_id tps62360_id[] = {
506*4882a593Smuzhiyun 	{.name = "tps62360", .driver_data = TPS62360},
507*4882a593Smuzhiyun 	{.name = "tps62361", .driver_data = TPS62361},
508*4882a593Smuzhiyun 	{.name = "tps62362", .driver_data = TPS62362},
509*4882a593Smuzhiyun 	{.name = "tps62363", .driver_data = TPS62363},
510*4882a593Smuzhiyun 	{},
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, tps62360_id);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun static struct i2c_driver tps62360_i2c_driver = {
516*4882a593Smuzhiyun 	.driver = {
517*4882a593Smuzhiyun 		.name = "tps62360",
518*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(tps62360_of_match),
519*4882a593Smuzhiyun 	},
520*4882a593Smuzhiyun 	.probe = tps62360_probe,
521*4882a593Smuzhiyun 	.shutdown = tps62360_shutdown,
522*4882a593Smuzhiyun 	.id_table = tps62360_id,
523*4882a593Smuzhiyun };
524*4882a593Smuzhiyun 
tps62360_init(void)525*4882a593Smuzhiyun static int __init tps62360_init(void)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	return i2c_add_driver(&tps62360_i2c_driver);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun subsys_initcall(tps62360_init);
530*4882a593Smuzhiyun 
tps62360_cleanup(void)531*4882a593Smuzhiyun static void __exit tps62360_cleanup(void)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	i2c_del_driver(&tps62360_i2c_driver);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun module_exit(tps62360_cleanup);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
538*4882a593Smuzhiyun MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
539*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
540