1452534e5SVenu Byravarasu /*
2452534e5SVenu Byravarasu  * Regulator driver for tps65090 power management chip.
3452534e5SVenu Byravarasu  *
4452534e5SVenu Byravarasu  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5452534e5SVenu Byravarasu 
6452534e5SVenu Byravarasu  * This program is free software; you can redistribute it and/or modify it
7452534e5SVenu Byravarasu  * under the terms and conditions of the GNU General Public License,
8452534e5SVenu Byravarasu  * version 2, as published by the Free Software Foundation.
9452534e5SVenu Byravarasu 
10452534e5SVenu Byravarasu  * This program is distributed in the hope it will be useful, but WITHOUT
11452534e5SVenu Byravarasu  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12452534e5SVenu Byravarasu  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13452534e5SVenu Byravarasu  * more details.
14452534e5SVenu Byravarasu 
15452534e5SVenu Byravarasu  * You should have received a copy of the GNU General Public License
16452534e5SVenu Byravarasu  * along with this program.  If not, see <http://www.gnu.org/licenses/>
17452534e5SVenu Byravarasu  */
18452534e5SVenu Byravarasu 
19452534e5SVenu Byravarasu #include <linux/module.h>
20ed11f1eaSDoug Anderson #include <linux/delay.h>
21452534e5SVenu Byravarasu #include <linux/init.h>
22f329b175SLaxman Dewangan #include <linux/gpio.h>
236c7a7a0eSLaxman Dewangan #include <linux/of_gpio.h>
24452534e5SVenu Byravarasu #include <linux/slab.h>
25452534e5SVenu Byravarasu #include <linux/err.h>
26452534e5SVenu Byravarasu #include <linux/platform_device.h>
27452534e5SVenu Byravarasu #include <linux/regulator/driver.h>
28452534e5SVenu Byravarasu #include <linux/regulator/machine.h>
296c7a7a0eSLaxman Dewangan #include <linux/regulator/of_regulator.h>
30452534e5SVenu Byravarasu #include <linux/mfd/tps65090.h>
31452534e5SVenu Byravarasu 
32ed11f1eaSDoug Anderson #define MAX_CTRL_READ_TRIES	5
33ed11f1eaSDoug Anderson #define MAX_FET_ENABLE_TRIES	1000
34ed11f1eaSDoug Anderson 
35ed11f1eaSDoug Anderson #define CTRL_EN_BIT		0 /* Regulator enable bit, active high */
3629041449SDoug Anderson #define CTRL_WT_BIT		2 /* Regulator wait time 0 bit */
37ed11f1eaSDoug Anderson #define CTRL_PG_BIT		4 /* Regulator power good bit, 1=good */
38ed11f1eaSDoug Anderson #define CTRL_TO_BIT		7 /* Regulator timeout bit, 1=wait */
3929041449SDoug Anderson 
4029041449SDoug Anderson #define MAX_OVERCURRENT_WAIT	3 /* Overcurrent wait must be <= this */
4129041449SDoug Anderson 
4229041449SDoug Anderson /**
4329041449SDoug Anderson  * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
4429041449SDoug Anderson  *
4529041449SDoug Anderson  * @dev: Pointer to our device.
4629041449SDoug Anderson  * @desc: The struct regulator_desc for the regulator.
4729041449SDoug Anderson  * @rdev: The struct regulator_dev for the regulator.
4829041449SDoug Anderson  * @overcurrent_wait_valid: True if overcurrent_wait is valid.
4929041449SDoug Anderson  * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
5029041449SDoug Anderson  */
5129041449SDoug Anderson 
52452534e5SVenu Byravarasu struct tps65090_regulator {
53452534e5SVenu Byravarasu 	struct device		*dev;
5424282a1cSLaxman Dewangan 	struct regulator_desc	*desc;
5524282a1cSLaxman Dewangan 	struct regulator_dev	*rdev;
5629041449SDoug Anderson 	bool			overcurrent_wait_valid;
5729041449SDoug Anderson 	int			overcurrent_wait;
58452534e5SVenu Byravarasu };
59452534e5SVenu Byravarasu 
60f329b175SLaxman Dewangan static struct regulator_ops tps65090_ext_control_ops = {
61f329b175SLaxman Dewangan };
62f329b175SLaxman Dewangan 
6329041449SDoug Anderson /**
6429041449SDoug Anderson  * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
6529041449SDoug Anderson  *
6629041449SDoug Anderson  * This will set the overcurrent wait time based on what's in the regulator
6729041449SDoug Anderson  * info.
6829041449SDoug Anderson  *
6929041449SDoug Anderson  * @ri:		Overall regulator data
7029041449SDoug Anderson  * @rdev:	Regulator device
7129041449SDoug Anderson  *
7229041449SDoug Anderson  * Return: 0 if no error, non-zero if there was an error writing the register.
7329041449SDoug Anderson  */
7429041449SDoug Anderson static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
7529041449SDoug Anderson 					     struct regulator_dev *rdev)
7629041449SDoug Anderson {
7729041449SDoug Anderson 	int ret;
7829041449SDoug Anderson 
7929041449SDoug Anderson 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
8029041449SDoug Anderson 				 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
8129041449SDoug Anderson 				 ri->overcurrent_wait << CTRL_WT_BIT);
8229041449SDoug Anderson 	if (ret) {
8329041449SDoug Anderson 		dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
8429041449SDoug Anderson 			rdev->desc->enable_reg);
8529041449SDoug Anderson 	}
8629041449SDoug Anderson 
8729041449SDoug Anderson 	return ret;
8829041449SDoug Anderson }
8929041449SDoug Anderson 
90ed11f1eaSDoug Anderson /**
91ed11f1eaSDoug Anderson  * tps65090_try_enable_fet - Try to enable a FET
92ed11f1eaSDoug Anderson  *
93ed11f1eaSDoug Anderson  * @rdev:	Regulator device
94ed11f1eaSDoug Anderson  *
95ed11f1eaSDoug Anderson  * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get
96ed11f1eaSDoug Anderson  * set, or some other -ve value if another error occurred (e.g. i2c error)
97ed11f1eaSDoug Anderson  */
98ed11f1eaSDoug Anderson static int tps65090_try_enable_fet(struct regulator_dev *rdev)
99ed11f1eaSDoug Anderson {
100ed11f1eaSDoug Anderson 	unsigned int control;
101ed11f1eaSDoug Anderson 	int ret, i;
102ed11f1eaSDoug Anderson 
103ed11f1eaSDoug Anderson 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
104ed11f1eaSDoug Anderson 				 rdev->desc->enable_mask,
105ed11f1eaSDoug Anderson 				 rdev->desc->enable_mask);
106ed11f1eaSDoug Anderson 	if (ret < 0) {
107ed11f1eaSDoug Anderson 		dev_err(&rdev->dev, "Error in updating reg %#x\n",
108ed11f1eaSDoug Anderson 			rdev->desc->enable_reg);
109ed11f1eaSDoug Anderson 		return ret;
110ed11f1eaSDoug Anderson 	}
111ed11f1eaSDoug Anderson 
112ed11f1eaSDoug Anderson 	for (i = 0; i < MAX_CTRL_READ_TRIES; i++) {
113ed11f1eaSDoug Anderson 		ret = regmap_read(rdev->regmap, rdev->desc->enable_reg,
114ed11f1eaSDoug Anderson 				  &control);
115ed11f1eaSDoug Anderson 		if (ret < 0)
116ed11f1eaSDoug Anderson 			return ret;
117ed11f1eaSDoug Anderson 
118ed11f1eaSDoug Anderson 		if (!(control & BIT(CTRL_TO_BIT)))
119ed11f1eaSDoug Anderson 			break;
120ed11f1eaSDoug Anderson 
121ed11f1eaSDoug Anderson 		usleep_range(1000, 1500);
122ed11f1eaSDoug Anderson 	}
123ed11f1eaSDoug Anderson 	if (!(control & BIT(CTRL_PG_BIT)))
124ed11f1eaSDoug Anderson 		return -ENOTRECOVERABLE;
125ed11f1eaSDoug Anderson 
126ed11f1eaSDoug Anderson 	return 0;
127ed11f1eaSDoug Anderson }
128ed11f1eaSDoug Anderson 
129ed11f1eaSDoug Anderson /**
130ed11f1eaSDoug Anderson  * tps65090_fet_enable - Enable a FET, trying a few times if it fails
131ed11f1eaSDoug Anderson  *
132ed11f1eaSDoug Anderson  * Some versions of the tps65090 have issues when turning on the FETs.
133ed11f1eaSDoug Anderson  * This function goes through several steps to ensure the best chance of the
134ed11f1eaSDoug Anderson  * FET going on.  Specifically:
135ed11f1eaSDoug Anderson  * - We'll make sure that we bump the "overcurrent wait" to the maximum, which
136ed11f1eaSDoug Anderson  *   increases the chances that we'll turn on properly.
137ed11f1eaSDoug Anderson  * - We'll retry turning the FET on multiple times (turning off in between).
138ed11f1eaSDoug Anderson  *
139ed11f1eaSDoug Anderson  * @rdev:	Regulator device
140ed11f1eaSDoug Anderson  *
141ed11f1eaSDoug Anderson  * Return: 0 if ok, non-zero if it fails.
142ed11f1eaSDoug Anderson  */
143ed11f1eaSDoug Anderson static int tps65090_fet_enable(struct regulator_dev *rdev)
144ed11f1eaSDoug Anderson {
145ed11f1eaSDoug Anderson 	int ret, tries;
146ed11f1eaSDoug Anderson 
147ed11f1eaSDoug Anderson 	/*
148ed11f1eaSDoug Anderson 	 * Try enabling multiple times until we succeed since sometimes the
149ed11f1eaSDoug Anderson 	 * first try times out.
150ed11f1eaSDoug Anderson 	 */
151ed11f1eaSDoug Anderson 	tries = 0;
152ed11f1eaSDoug Anderson 	while (true) {
153ed11f1eaSDoug Anderson 		ret = tps65090_try_enable_fet(rdev);
154ed11f1eaSDoug Anderson 		if (!ret)
155ed11f1eaSDoug Anderson 			break;
156ed11f1eaSDoug Anderson 		if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES)
157ed11f1eaSDoug Anderson 			goto err;
158ed11f1eaSDoug Anderson 
159ed11f1eaSDoug Anderson 		/* Try turning the FET off (and then on again) */
160ed11f1eaSDoug Anderson 		ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
161ed11f1eaSDoug Anderson 					 rdev->desc->enable_mask, 0);
162ed11f1eaSDoug Anderson 		if (ret)
163ed11f1eaSDoug Anderson 			goto err;
164ed11f1eaSDoug Anderson 
165ed11f1eaSDoug Anderson 		tries++;
166ed11f1eaSDoug Anderson 	}
167ed11f1eaSDoug Anderson 
168ed11f1eaSDoug Anderson 	if (tries)
169ed11f1eaSDoug Anderson 		dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n",
170ed11f1eaSDoug Anderson 			 rdev->desc->enable_reg, tries);
171ed11f1eaSDoug Anderson 
172ed11f1eaSDoug Anderson 	return 0;
173ed11f1eaSDoug Anderson err:
174ed11f1eaSDoug Anderson 	dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
175ed11f1eaSDoug Anderson 	WARN_ON(1);
176ed11f1eaSDoug Anderson 
177ed11f1eaSDoug Anderson 	return ret;
178ed11f1eaSDoug Anderson }
179ed11f1eaSDoug Anderson 
180ed11f1eaSDoug Anderson static struct regulator_ops tps65090_reg_control_ops = {
18106c4998bSAxel Lin 	.enable		= regulator_enable_regmap,
18206c4998bSAxel Lin 	.disable	= regulator_disable_regmap,
18306c4998bSAxel Lin 	.is_enabled	= regulator_is_enabled_regmap,
184452534e5SVenu Byravarasu };
185452534e5SVenu Byravarasu 
186ed11f1eaSDoug Anderson static struct regulator_ops tps65090_fet_control_ops = {
187ed11f1eaSDoug Anderson 	.enable		= tps65090_fet_enable,
188ed11f1eaSDoug Anderson 	.disable	= regulator_disable_regmap,
189ed11f1eaSDoug Anderson 	.is_enabled	= regulator_is_enabled_regmap,
190ed11f1eaSDoug Anderson };
191ed11f1eaSDoug Anderson 
1923a81ef8cSLaxman Dewangan static struct regulator_ops tps65090_ldo_ops = {
1933a81ef8cSLaxman Dewangan };
1943a81ef8cSLaxman Dewangan 
195ed11f1eaSDoug Anderson #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _ops)	\
196452534e5SVenu Byravarasu {							\
19724282a1cSLaxman Dewangan 	.name = "TPS65090_RAILS"#_id,			\
19824282a1cSLaxman Dewangan 	.supply_name = _sname,				\
1998620ca9fSLaxman Dewangan 	.id = TPS65090_REGULATOR_##_id,			\
20024282a1cSLaxman Dewangan 	.ops = &_ops,					\
20124282a1cSLaxman Dewangan 	.enable_reg = _en_reg,				\
202ed11f1eaSDoug Anderson 	.enable_val = _en_bits,				\
203ed11f1eaSDoug Anderson 	.enable_mask = _en_bits,			\
204452534e5SVenu Byravarasu 	.type = REGULATOR_VOLTAGE,			\
205452534e5SVenu Byravarasu 	.owner = THIS_MODULE,				\
206452534e5SVenu Byravarasu }
207452534e5SVenu Byravarasu 
20824282a1cSLaxman Dewangan static struct regulator_desc tps65090_regulator_desc[] = {
209ed11f1eaSDoug Anderson 	tps65090_REG_DESC(DCDC1, "vsys1",   0x0C, BIT(CTRL_EN_BIT),
210ed11f1eaSDoug Anderson 			  tps65090_reg_control_ops),
211ed11f1eaSDoug Anderson 	tps65090_REG_DESC(DCDC2, "vsys2",   0x0D, BIT(CTRL_EN_BIT),
212ed11f1eaSDoug Anderson 			  tps65090_reg_control_ops),
213ed11f1eaSDoug Anderson 	tps65090_REG_DESC(DCDC3, "vsys3",   0x0E, BIT(CTRL_EN_BIT),
214ed11f1eaSDoug Anderson 			  tps65090_reg_control_ops),
215ed11f1eaSDoug Anderson 
216ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET1,  "infet1",  0x0F,
217ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
218ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
219ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET2,  "infet2",  0x10,
220ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
221ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
222ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET3,  "infet3",  0x11,
223ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
224ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
225ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET4,  "infet4",  0x12,
226ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
227ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
228ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET5,  "infet5",  0x13,
229ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
230ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
231ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET6,  "infet6",  0x14,
232ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
233ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
234ed11f1eaSDoug Anderson 	tps65090_REG_DESC(FET7,  "infet7",  0x15,
235ed11f1eaSDoug Anderson 			  BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
236ed11f1eaSDoug Anderson 			  tps65090_fet_control_ops),
237ed11f1eaSDoug Anderson 
238ed11f1eaSDoug Anderson 	tps65090_REG_DESC(LDO1,  "vsys-l1", 0, 0,
239ed11f1eaSDoug Anderson 			  tps65090_ldo_ops),
240ed11f1eaSDoug Anderson 	tps65090_REG_DESC(LDO2,  "vsys-l2", 0, 0,
241ed11f1eaSDoug Anderson 			  tps65090_ldo_ops),
242452534e5SVenu Byravarasu };
243452534e5SVenu Byravarasu 
24424282a1cSLaxman Dewangan static inline bool is_dcdc(int id)
245452534e5SVenu Byravarasu {
24624282a1cSLaxman Dewangan 	switch (id) {
2478620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC1:
2488620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC2:
2498620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC3:
25024282a1cSLaxman Dewangan 		return true;
25124282a1cSLaxman Dewangan 	default:
25224282a1cSLaxman Dewangan 		return false;
253452534e5SVenu Byravarasu 	}
25424282a1cSLaxman Dewangan }
25524282a1cSLaxman Dewangan 
256a5023574SBill Pemberton static int tps65090_config_ext_control(
25724282a1cSLaxman Dewangan 	struct tps65090_regulator *ri, bool enable)
25824282a1cSLaxman Dewangan {
25924282a1cSLaxman Dewangan 	int ret;
26024282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
26124282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
26224282a1cSLaxman Dewangan 
26324282a1cSLaxman Dewangan 	if (enable)
26424282a1cSLaxman Dewangan 		ret = tps65090_set_bits(parent, reg_en_reg, 1);
26524282a1cSLaxman Dewangan 	else
26624282a1cSLaxman Dewangan 		ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
26724282a1cSLaxman Dewangan 	if (ret < 0)
26824282a1cSLaxman Dewangan 		dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
26924282a1cSLaxman Dewangan 	return ret;
27024282a1cSLaxman Dewangan }
27124282a1cSLaxman Dewangan 
272a5023574SBill Pemberton static int tps65090_regulator_disable_ext_control(
27324282a1cSLaxman Dewangan 		struct tps65090_regulator *ri,
27424282a1cSLaxman Dewangan 		struct tps65090_regulator_plat_data *tps_pdata)
27524282a1cSLaxman Dewangan {
27624282a1cSLaxman Dewangan 	int ret = 0;
27724282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
27824282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
27924282a1cSLaxman Dewangan 
28024282a1cSLaxman Dewangan 	/*
28124282a1cSLaxman Dewangan 	 * First enable output for internal control if require.
28224282a1cSLaxman Dewangan 	 * And then disable external control.
28324282a1cSLaxman Dewangan 	 */
28424282a1cSLaxman Dewangan 	if (tps_pdata->reg_init_data->constraints.always_on ||
28524282a1cSLaxman Dewangan 			tps_pdata->reg_init_data->constraints.boot_on) {
28624282a1cSLaxman Dewangan 		ret =  tps65090_set_bits(parent, reg_en_reg, 0);
28724282a1cSLaxman Dewangan 		if (ret < 0) {
28824282a1cSLaxman Dewangan 			dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
28924282a1cSLaxman Dewangan 			return ret;
29024282a1cSLaxman Dewangan 		}
29124282a1cSLaxman Dewangan 	}
29224282a1cSLaxman Dewangan 	return tps65090_config_ext_control(ri, false);
293452534e5SVenu Byravarasu }
294452534e5SVenu Byravarasu 
295a5023574SBill Pemberton static void tps65090_configure_regulator_config(
296f329b175SLaxman Dewangan 		struct tps65090_regulator_plat_data *tps_pdata,
297f329b175SLaxman Dewangan 		struct regulator_config *config)
298f329b175SLaxman Dewangan {
299f329b175SLaxman Dewangan 	if (gpio_is_valid(tps_pdata->gpio)) {
300f329b175SLaxman Dewangan 		int gpio_flag = GPIOF_OUT_INIT_LOW;
301f329b175SLaxman Dewangan 
302f329b175SLaxman Dewangan 		if (tps_pdata->reg_init_data->constraints.always_on ||
303f329b175SLaxman Dewangan 				tps_pdata->reg_init_data->constraints.boot_on)
304f329b175SLaxman Dewangan 			gpio_flag = GPIOF_OUT_INIT_HIGH;
305f329b175SLaxman Dewangan 
306f329b175SLaxman Dewangan 		config->ena_gpio = tps_pdata->gpio;
307f329b175SLaxman Dewangan 		config->ena_gpio_flags = gpio_flag;
308f329b175SLaxman Dewangan 	}
309f329b175SLaxman Dewangan }
310f329b175SLaxman Dewangan 
3116c7a7a0eSLaxman Dewangan #ifdef CONFIG_OF
3126c7a7a0eSLaxman Dewangan static struct of_regulator_match tps65090_matches[] = {
3136c7a7a0eSLaxman Dewangan 	{ .name = "dcdc1", },
3146c7a7a0eSLaxman Dewangan 	{ .name = "dcdc2", },
3156c7a7a0eSLaxman Dewangan 	{ .name = "dcdc3", },
3166c7a7a0eSLaxman Dewangan 	{ .name = "fet1",  },
3176c7a7a0eSLaxman Dewangan 	{ .name = "fet2",  },
3186c7a7a0eSLaxman Dewangan 	{ .name = "fet3",  },
3196c7a7a0eSLaxman Dewangan 	{ .name = "fet4",  },
3206c7a7a0eSLaxman Dewangan 	{ .name = "fet5",  },
3216c7a7a0eSLaxman Dewangan 	{ .name = "fet6",  },
3226c7a7a0eSLaxman Dewangan 	{ .name = "fet7",  },
3236c7a7a0eSLaxman Dewangan 	{ .name = "ldo1",  },
3246c7a7a0eSLaxman Dewangan 	{ .name = "ldo2",  },
3256c7a7a0eSLaxman Dewangan };
3266c7a7a0eSLaxman Dewangan 
3276c7a7a0eSLaxman Dewangan static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
3286c7a7a0eSLaxman Dewangan 		struct platform_device *pdev,
3296c7a7a0eSLaxman Dewangan 		struct of_regulator_match **tps65090_reg_matches)
3306c7a7a0eSLaxman Dewangan {
3316c7a7a0eSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
3326c7a7a0eSLaxman Dewangan 	struct device_node *np = pdev->dev.parent->of_node;
3336c7a7a0eSLaxman Dewangan 	struct device_node *regulators;
3346c7a7a0eSLaxman Dewangan 	int idx = 0, ret;
3356c7a7a0eSLaxman Dewangan 	struct tps65090_regulator_plat_data *reg_pdata;
3366c7a7a0eSLaxman Dewangan 
3376c7a7a0eSLaxman Dewangan 	tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
3386c7a7a0eSLaxman Dewangan 				GFP_KERNEL);
3390ad91c69SSachin Kamat 	if (!tps65090_pdata)
3406c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3416c7a7a0eSLaxman Dewangan 
3426c7a7a0eSLaxman Dewangan 	reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
3436c7a7a0eSLaxman Dewangan 				sizeof(*reg_pdata), GFP_KERNEL);
3440ad91c69SSachin Kamat 	if (!reg_pdata)
3456c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3466c7a7a0eSLaxman Dewangan 
3474c850eadSLaxman Dewangan 	regulators = of_get_child_by_name(np, "regulators");
3486c7a7a0eSLaxman Dewangan 	if (!regulators) {
3496c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev, "regulator node not found\n");
3506c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENODEV);
3516c7a7a0eSLaxman Dewangan 	}
3526c7a7a0eSLaxman Dewangan 
35309a228e7SAxel Lin 	ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
3546c7a7a0eSLaxman Dewangan 			ARRAY_SIZE(tps65090_matches));
355026cdfe6SSachin Kamat 	of_node_put(regulators);
3566c7a7a0eSLaxman Dewangan 	if (ret < 0) {
3576c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev,
3586c7a7a0eSLaxman Dewangan 			"Error parsing regulator init data: %d\n", ret);
3596c7a7a0eSLaxman Dewangan 		return ERR_PTR(ret);
3606c7a7a0eSLaxman Dewangan 	}
3616c7a7a0eSLaxman Dewangan 
3626c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = tps65090_matches;
3636c7a7a0eSLaxman Dewangan 	for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
3646c7a7a0eSLaxman Dewangan 		struct regulator_init_data *ri_data;
3656c7a7a0eSLaxman Dewangan 		struct tps65090_regulator_plat_data *rpdata;
3666c7a7a0eSLaxman Dewangan 
3676c7a7a0eSLaxman Dewangan 		rpdata = &reg_pdata[idx];
3686c7a7a0eSLaxman Dewangan 		ri_data = tps65090_matches[idx].init_data;
3696c7a7a0eSLaxman Dewangan 		if (!ri_data || !tps65090_matches[idx].of_node)
3706c7a7a0eSLaxman Dewangan 			continue;
3716c7a7a0eSLaxman Dewangan 
3726c7a7a0eSLaxman Dewangan 		rpdata->reg_init_data = ri_data;
3736c7a7a0eSLaxman Dewangan 		rpdata->enable_ext_control = of_property_read_bool(
3746c7a7a0eSLaxman Dewangan 					tps65090_matches[idx].of_node,
3756c7a7a0eSLaxman Dewangan 					"ti,enable-ext-control");
3766c7a7a0eSLaxman Dewangan 		if (rpdata->enable_ext_control)
3776c7a7a0eSLaxman Dewangan 			rpdata->gpio = of_get_named_gpio(np,
3786c7a7a0eSLaxman Dewangan 					"dcdc-ext-control-gpios", 0);
3796c7a7a0eSLaxman Dewangan 
38029041449SDoug Anderson 		if (of_property_read_u32(tps65090_matches[idx].of_node,
38129041449SDoug Anderson 					 "ti,overcurrent-wait",
38229041449SDoug Anderson 					 &rpdata->overcurrent_wait) == 0)
38329041449SDoug Anderson 			rpdata->overcurrent_wait_valid = true;
38429041449SDoug Anderson 
3856c7a7a0eSLaxman Dewangan 		tps65090_pdata->reg_pdata[idx] = rpdata;
3866c7a7a0eSLaxman Dewangan 	}
3876c7a7a0eSLaxman Dewangan 	return tps65090_pdata;
3886c7a7a0eSLaxman Dewangan }
3896c7a7a0eSLaxman Dewangan #else
3906c7a7a0eSLaxman Dewangan static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
3916c7a7a0eSLaxman Dewangan 			struct platform_device *pdev,
3926c7a7a0eSLaxman Dewangan 			struct of_regulator_match **tps65090_reg_matches)
3936c7a7a0eSLaxman Dewangan {
3946c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = NULL;
3956c7a7a0eSLaxman Dewangan 	return NULL;
3966c7a7a0eSLaxman Dewangan }
3976c7a7a0eSLaxman Dewangan #endif
3986c7a7a0eSLaxman Dewangan 
399a5023574SBill Pemberton static int tps65090_regulator_probe(struct platform_device *pdev)
400452534e5SVenu Byravarasu {
40106c4998bSAxel Lin 	struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
402452534e5SVenu Byravarasu 	struct tps65090_regulator *ri = NULL;
403c172708dSMark Brown 	struct regulator_config config = { };
404452534e5SVenu Byravarasu 	struct regulator_dev *rdev;
40524282a1cSLaxman Dewangan 	struct tps65090_regulator_plat_data *tps_pdata;
40624282a1cSLaxman Dewangan 	struct tps65090_regulator *pmic;
40724282a1cSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
4086c7a7a0eSLaxman Dewangan 	struct of_regulator_match *tps65090_reg_matches = NULL;
40924282a1cSLaxman Dewangan 	int num;
41024282a1cSLaxman Dewangan 	int ret;
411452534e5SVenu Byravarasu 
41224282a1cSLaxman Dewangan 	dev_dbg(&pdev->dev, "Probing regulator\n");
413452534e5SVenu Byravarasu 
41424282a1cSLaxman Dewangan 	tps65090_pdata = dev_get_platdata(pdev->dev.parent);
4156c7a7a0eSLaxman Dewangan 	if (!tps65090_pdata && tps65090_mfd->dev->of_node)
4166c7a7a0eSLaxman Dewangan 		tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
4176c7a7a0eSLaxman Dewangan 					&tps65090_reg_matches);
4186c7a7a0eSLaxman Dewangan 	if (IS_ERR_OR_NULL(tps65090_pdata)) {
41924282a1cSLaxman Dewangan 		dev_err(&pdev->dev, "Platform data missing\n");
4206c7a7a0eSLaxman Dewangan 		return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
421452534e5SVenu Byravarasu 	}
422452534e5SVenu Byravarasu 
4238620ca9fSLaxman Dewangan 	pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
42424282a1cSLaxman Dewangan 			GFP_KERNEL);
4250ad91c69SSachin Kamat 	if (!pmic)
42624282a1cSLaxman Dewangan 		return -ENOMEM;
427452534e5SVenu Byravarasu 
4288620ca9fSLaxman Dewangan 	for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
42924282a1cSLaxman Dewangan 		tps_pdata = tps65090_pdata->reg_pdata[num];
43024282a1cSLaxman Dewangan 
43124282a1cSLaxman Dewangan 		ri = &pmic[num];
43224282a1cSLaxman Dewangan 		ri->dev = &pdev->dev;
43324282a1cSLaxman Dewangan 		ri->desc = &tps65090_regulator_desc[num];
434c122c5b6SDoug Anderson 		if (tps_pdata) {
435c122c5b6SDoug Anderson 			ri->overcurrent_wait_valid =
436c122c5b6SDoug Anderson 				tps_pdata->overcurrent_wait_valid;
43729041449SDoug Anderson 			ri->overcurrent_wait = tps_pdata->overcurrent_wait;
438c122c5b6SDoug Anderson 		}
43924282a1cSLaxman Dewangan 
44024282a1cSLaxman Dewangan 		/*
44124282a1cSLaxman Dewangan 		 * TPS5090 DCDC support the control from external digital input.
442f329b175SLaxman Dewangan 		 * Configure it as per platform data.
44324282a1cSLaxman Dewangan 		 */
44424282a1cSLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
445f329b175SLaxman Dewangan 			if (tps_pdata->enable_ext_control) {
446f329b175SLaxman Dewangan 				tps65090_configure_regulator_config(
447f329b175SLaxman Dewangan 						tps_pdata, &config);
448f329b175SLaxman Dewangan 				ri->desc->ops = &tps65090_ext_control_ops;
449f329b175SLaxman Dewangan 			} else {
45024282a1cSLaxman Dewangan 				ret = tps65090_regulator_disable_ext_control(
45124282a1cSLaxman Dewangan 						ri, tps_pdata);
45224282a1cSLaxman Dewangan 				if (ret < 0) {
45324282a1cSLaxman Dewangan 					dev_err(&pdev->dev,
45424282a1cSLaxman Dewangan 						"failed disable ext control\n");
4559738efaeSSachin Kamat 					return ret;
45624282a1cSLaxman Dewangan 				}
45724282a1cSLaxman Dewangan 			}
458f329b175SLaxman Dewangan 		}
459f329b175SLaxman Dewangan 
4606c7a7a0eSLaxman Dewangan 		config.dev = pdev->dev.parent;
46124282a1cSLaxman Dewangan 		config.driver_data = ri;
46224282a1cSLaxman Dewangan 		config.regmap = tps65090_mfd->rmap;
46324282a1cSLaxman Dewangan 		if (tps_pdata)
46424282a1cSLaxman Dewangan 			config.init_data = tps_pdata->reg_init_data;
46524282a1cSLaxman Dewangan 		else
46624282a1cSLaxman Dewangan 			config.init_data = NULL;
4676c7a7a0eSLaxman Dewangan 		if (tps65090_reg_matches)
4686c7a7a0eSLaxman Dewangan 			config.of_node = tps65090_reg_matches[num].of_node;
4696c7a7a0eSLaxman Dewangan 		else
4706c7a7a0eSLaxman Dewangan 			config.of_node = NULL;
47124282a1cSLaxman Dewangan 
4729738efaeSSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
47324282a1cSLaxman Dewangan 		if (IS_ERR(rdev)) {
47424282a1cSLaxman Dewangan 			dev_err(&pdev->dev, "failed to register regulator %s\n",
47524282a1cSLaxman Dewangan 				ri->desc->name);
4769738efaeSSachin Kamat 			return PTR_ERR(rdev);
47724282a1cSLaxman Dewangan 		}
47824282a1cSLaxman Dewangan 		ri->rdev = rdev;
479f329b175SLaxman Dewangan 
48029041449SDoug Anderson 		if (ri->overcurrent_wait_valid) {
48129041449SDoug Anderson 			ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
48229041449SDoug Anderson 			if (ret < 0)
48329041449SDoug Anderson 				return ret;
48429041449SDoug Anderson 		}
48529041449SDoug Anderson 
486f329b175SLaxman Dewangan 		/* Enable external control if it is require */
487f329b175SLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
488f329b175SLaxman Dewangan 				tps_pdata->enable_ext_control) {
489f329b175SLaxman Dewangan 			ret = tps65090_config_ext_control(ri, true);
4909738efaeSSachin Kamat 			if (ret < 0)
4919738efaeSSachin Kamat 				return ret;
492f329b175SLaxman Dewangan 		}
49324282a1cSLaxman Dewangan 	}
49424282a1cSLaxman Dewangan 
49524282a1cSLaxman Dewangan 	platform_set_drvdata(pdev, pmic);
496452534e5SVenu Byravarasu 	return 0;
497452534e5SVenu Byravarasu }
498452534e5SVenu Byravarasu 
499452534e5SVenu Byravarasu static struct platform_driver tps65090_regulator_driver = {
500452534e5SVenu Byravarasu 	.driver	= {
5018620ca9fSLaxman Dewangan 		.name	= "tps65090-pmic",
502452534e5SVenu Byravarasu 		.owner	= THIS_MODULE,
503452534e5SVenu Byravarasu 	},
504452534e5SVenu Byravarasu 	.probe		= tps65090_regulator_probe,
505452534e5SVenu Byravarasu };
506452534e5SVenu Byravarasu 
507452534e5SVenu Byravarasu static int __init tps65090_regulator_init(void)
508452534e5SVenu Byravarasu {
509452534e5SVenu Byravarasu 	return platform_driver_register(&tps65090_regulator_driver);
510452534e5SVenu Byravarasu }
511452534e5SVenu Byravarasu subsys_initcall(tps65090_regulator_init);
512452534e5SVenu Byravarasu 
513452534e5SVenu Byravarasu static void __exit tps65090_regulator_exit(void)
514452534e5SVenu Byravarasu {
515452534e5SVenu Byravarasu 	platform_driver_unregister(&tps65090_regulator_driver);
516452534e5SVenu Byravarasu }
517452534e5SVenu Byravarasu module_exit(tps65090_regulator_exit);
518452534e5SVenu Byravarasu 
519452534e5SVenu Byravarasu MODULE_DESCRIPTION("tps65090 regulator driver");
520452534e5SVenu Byravarasu MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
521452534e5SVenu Byravarasu MODULE_LICENSE("GPL v2");
522d95420b8SAxel Lin MODULE_ALIAS("platform:tps65090-pmic");
523