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>
223012e814SLinus Walleij #include <linux/of.h>
233012e814SLinus Walleij #include <linux/gpio/consumer.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 
1954f2352cfSJavier Martinez Canillas #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
196452534e5SVenu Byravarasu {							\
19724282a1cSLaxman Dewangan 	.name = "TPS65090_RAILS"#_id,			\
19824282a1cSLaxman Dewangan 	.supply_name = _sname,				\
1998620ca9fSLaxman Dewangan 	.id = TPS65090_REGULATOR_##_id,			\
2004f2352cfSJavier Martinez Canillas 	.n_voltages = _nvolt,				\
20124282a1cSLaxman Dewangan 	.ops = &_ops,					\
2024f2352cfSJavier Martinez Canillas 	.fixed_uV = _volt,				\
20324282a1cSLaxman Dewangan 	.enable_reg = _en_reg,				\
204ed11f1eaSDoug Anderson 	.enable_val = _en_bits,				\
205ed11f1eaSDoug Anderson 	.enable_mask = _en_bits,			\
206452534e5SVenu Byravarasu 	.type = REGULATOR_VOLTAGE,			\
207452534e5SVenu Byravarasu 	.owner = THIS_MODULE,				\
208452534e5SVenu Byravarasu }
209452534e5SVenu Byravarasu 
2104f2352cfSJavier Martinez Canillas #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
2114f2352cfSJavier Martinez Canillas 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
2124f2352cfSJavier Martinez Canillas 
2134f2352cfSJavier Martinez Canillas #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
2144f2352cfSJavier Martinez Canillas 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
2154f2352cfSJavier Martinez Canillas 
21624282a1cSLaxman Dewangan static struct regulator_desc tps65090_regulator_desc[] = {
2174f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(DCDC1, "vsys1",   0x0C, BIT(CTRL_EN_BIT), 5000000,
218ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
2194f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(DCDC2, "vsys2",   0x0D, BIT(CTRL_EN_BIT), 3300000,
220ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
2214f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(DCDC3, "vsys3",   0x0E, BIT(CTRL_EN_BIT),
222ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
223ed11f1eaSDoug Anderson 
2244f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET1,  "infet1",  0x0F,
225ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
226ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2274f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET2,  "infet2",  0x10,
228ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
229ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2304f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET3,  "infet3",  0x11,
231ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
232ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2334f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET4,  "infet4",  0x12,
234ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
235ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2364f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET5,  "infet5",  0x13,
237ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
238ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2394f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET6,  "infet6",  0x14,
240ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
241ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2424f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET7,  "infet7",  0x15,
243ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
244ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
245ed11f1eaSDoug Anderson 
2464f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(LDO1,  "vsys-l1", 0, 0, 5000000,
247ed11f1eaSDoug Anderson 			    tps65090_ldo_ops),
2484f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(LDO2,  "vsys-l2", 0, 0, 3300000,
249ed11f1eaSDoug Anderson 			    tps65090_ldo_ops),
250452534e5SVenu Byravarasu };
251452534e5SVenu Byravarasu 
25224282a1cSLaxman Dewangan static inline bool is_dcdc(int id)
253452534e5SVenu Byravarasu {
25424282a1cSLaxman Dewangan 	switch (id) {
2558620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC1:
2568620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC2:
2578620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC3:
25824282a1cSLaxman Dewangan 		return true;
25924282a1cSLaxman Dewangan 	default:
26024282a1cSLaxman Dewangan 		return false;
261452534e5SVenu Byravarasu 	}
26224282a1cSLaxman Dewangan }
26324282a1cSLaxman Dewangan 
264a5023574SBill Pemberton static int tps65090_config_ext_control(
26524282a1cSLaxman Dewangan 	struct tps65090_regulator *ri, bool enable)
26624282a1cSLaxman Dewangan {
26724282a1cSLaxman Dewangan 	int ret;
26824282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
26924282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
27024282a1cSLaxman Dewangan 
27124282a1cSLaxman Dewangan 	if (enable)
27224282a1cSLaxman Dewangan 		ret = tps65090_set_bits(parent, reg_en_reg, 1);
27324282a1cSLaxman Dewangan 	else
27424282a1cSLaxman Dewangan 		ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
27524282a1cSLaxman Dewangan 	if (ret < 0)
27624282a1cSLaxman Dewangan 		dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
27724282a1cSLaxman Dewangan 	return ret;
27824282a1cSLaxman Dewangan }
27924282a1cSLaxman Dewangan 
280a5023574SBill Pemberton static int tps65090_regulator_disable_ext_control(
28124282a1cSLaxman Dewangan 		struct tps65090_regulator *ri,
28224282a1cSLaxman Dewangan 		struct tps65090_regulator_plat_data *tps_pdata)
28324282a1cSLaxman Dewangan {
28424282a1cSLaxman Dewangan 	int ret = 0;
28524282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
28624282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
28724282a1cSLaxman Dewangan 
28824282a1cSLaxman Dewangan 	/*
28924282a1cSLaxman Dewangan 	 * First enable output for internal control if require.
29024282a1cSLaxman Dewangan 	 * And then disable external control.
29124282a1cSLaxman Dewangan 	 */
29224282a1cSLaxman Dewangan 	if (tps_pdata->reg_init_data->constraints.always_on ||
29324282a1cSLaxman Dewangan 			tps_pdata->reg_init_data->constraints.boot_on) {
29424282a1cSLaxman Dewangan 		ret =  tps65090_set_bits(parent, reg_en_reg, 0);
29524282a1cSLaxman Dewangan 		if (ret < 0) {
29624282a1cSLaxman Dewangan 			dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
29724282a1cSLaxman Dewangan 			return ret;
29824282a1cSLaxman Dewangan 		}
29924282a1cSLaxman Dewangan 	}
30024282a1cSLaxman Dewangan 	return tps65090_config_ext_control(ri, false);
301452534e5SVenu Byravarasu }
302452534e5SVenu Byravarasu 
3036c7a7a0eSLaxman Dewangan #ifdef CONFIG_OF
3046c7a7a0eSLaxman Dewangan static struct of_regulator_match tps65090_matches[] = {
3056c7a7a0eSLaxman Dewangan 	{ .name = "dcdc1", },
3066c7a7a0eSLaxman Dewangan 	{ .name = "dcdc2", },
3076c7a7a0eSLaxman Dewangan 	{ .name = "dcdc3", },
3086c7a7a0eSLaxman Dewangan 	{ .name = "fet1",  },
3096c7a7a0eSLaxman Dewangan 	{ .name = "fet2",  },
3106c7a7a0eSLaxman Dewangan 	{ .name = "fet3",  },
3116c7a7a0eSLaxman Dewangan 	{ .name = "fet4",  },
3126c7a7a0eSLaxman Dewangan 	{ .name = "fet5",  },
3136c7a7a0eSLaxman Dewangan 	{ .name = "fet6",  },
3146c7a7a0eSLaxman Dewangan 	{ .name = "fet7",  },
3156c7a7a0eSLaxman Dewangan 	{ .name = "ldo1",  },
3166c7a7a0eSLaxman Dewangan 	{ .name = "ldo2",  },
3176c7a7a0eSLaxman Dewangan };
3186c7a7a0eSLaxman Dewangan 
3196c7a7a0eSLaxman Dewangan static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
3206c7a7a0eSLaxman Dewangan 		struct platform_device *pdev,
3216c7a7a0eSLaxman Dewangan 		struct of_regulator_match **tps65090_reg_matches)
3226c7a7a0eSLaxman Dewangan {
3236c7a7a0eSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
3246c7a7a0eSLaxman Dewangan 	struct device_node *np = pdev->dev.parent->of_node;
3256c7a7a0eSLaxman Dewangan 	struct device_node *regulators;
3266c7a7a0eSLaxman Dewangan 	int idx = 0, ret;
3276c7a7a0eSLaxman Dewangan 	struct tps65090_regulator_plat_data *reg_pdata;
3286c7a7a0eSLaxman Dewangan 
3296c7a7a0eSLaxman Dewangan 	tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
3306c7a7a0eSLaxman Dewangan 				GFP_KERNEL);
3310ad91c69SSachin Kamat 	if (!tps65090_pdata)
3326c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3336c7a7a0eSLaxman Dewangan 
334a86854d0SKees Cook 	reg_pdata = devm_kcalloc(&pdev->dev,
335a86854d0SKees Cook 				 TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
336a86854d0SKees Cook 				 GFP_KERNEL);
3370ad91c69SSachin Kamat 	if (!reg_pdata)
3386c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3396c7a7a0eSLaxman Dewangan 
3404c850eadSLaxman Dewangan 	regulators = of_get_child_by_name(np, "regulators");
3416c7a7a0eSLaxman Dewangan 	if (!regulators) {
3426c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev, "regulator node not found\n");
3436c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENODEV);
3446c7a7a0eSLaxman Dewangan 	}
3456c7a7a0eSLaxman Dewangan 
34609a228e7SAxel Lin 	ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
3476c7a7a0eSLaxman Dewangan 			ARRAY_SIZE(tps65090_matches));
348026cdfe6SSachin Kamat 	of_node_put(regulators);
3496c7a7a0eSLaxman Dewangan 	if (ret < 0) {
3506c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev,
3516c7a7a0eSLaxman Dewangan 			"Error parsing regulator init data: %d\n", ret);
3526c7a7a0eSLaxman Dewangan 		return ERR_PTR(ret);
3536c7a7a0eSLaxman Dewangan 	}
3546c7a7a0eSLaxman Dewangan 
3556c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = tps65090_matches;
3566c7a7a0eSLaxman Dewangan 	for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
3576c7a7a0eSLaxman Dewangan 		struct regulator_init_data *ri_data;
3586c7a7a0eSLaxman Dewangan 		struct tps65090_regulator_plat_data *rpdata;
3596c7a7a0eSLaxman Dewangan 
3606c7a7a0eSLaxman Dewangan 		rpdata = &reg_pdata[idx];
3616c7a7a0eSLaxman Dewangan 		ri_data = tps65090_matches[idx].init_data;
3626c7a7a0eSLaxman Dewangan 		if (!ri_data || !tps65090_matches[idx].of_node)
3636c7a7a0eSLaxman Dewangan 			continue;
3646c7a7a0eSLaxman Dewangan 
3656c7a7a0eSLaxman Dewangan 		rpdata->reg_init_data = ri_data;
3666c7a7a0eSLaxman Dewangan 		rpdata->enable_ext_control = of_property_read_bool(
3676c7a7a0eSLaxman Dewangan 					tps65090_matches[idx].of_node,
3686c7a7a0eSLaxman Dewangan 					"ti,enable-ext-control");
3693012e814SLinus Walleij 		if (rpdata->enable_ext_control) {
3703012e814SLinus Walleij 			enum gpiod_flags gflags;
3713012e814SLinus Walleij 
3723012e814SLinus Walleij 			if (ri_data->constraints.always_on ||
3733012e814SLinus Walleij 			    ri_data->constraints.boot_on)
3743012e814SLinus Walleij 				gflags = GPIOD_OUT_HIGH;
3753012e814SLinus Walleij 			else
3763012e814SLinus Walleij 				gflags = GPIOD_OUT_LOW;
37763239e4bSLinus Walleij 			gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
3783012e814SLinus Walleij 
3793012e814SLinus Walleij 			rpdata->gpiod = devm_gpiod_get_from_of_node(&pdev->dev,
3803012e814SLinus Walleij 								    tps65090_matches[idx].of_node,
3813012e814SLinus Walleij 								    "dcdc-ext-control-gpios", 0,
3823012e814SLinus Walleij 								    gflags,
3833012e814SLinus Walleij 								    "tps65090");
3843012e814SLinus Walleij 			if (IS_ERR(rpdata->gpiod))
3853012e814SLinus Walleij 				return ERR_CAST(rpdata->gpiod);
3863012e814SLinus Walleij 			if (!rpdata->gpiod)
3873012e814SLinus Walleij 				dev_err(&pdev->dev,
3883012e814SLinus Walleij 					"could not find DCDC external control GPIO\n");
3893012e814SLinus Walleij 		}
3906c7a7a0eSLaxman Dewangan 
39129041449SDoug Anderson 		if (of_property_read_u32(tps65090_matches[idx].of_node,
39229041449SDoug Anderson 					 "ti,overcurrent-wait",
39329041449SDoug Anderson 					 &rpdata->overcurrent_wait) == 0)
39429041449SDoug Anderson 			rpdata->overcurrent_wait_valid = true;
39529041449SDoug Anderson 
3966c7a7a0eSLaxman Dewangan 		tps65090_pdata->reg_pdata[idx] = rpdata;
3976c7a7a0eSLaxman Dewangan 	}
3986c7a7a0eSLaxman Dewangan 	return tps65090_pdata;
3996c7a7a0eSLaxman Dewangan }
4006c7a7a0eSLaxman Dewangan #else
4016c7a7a0eSLaxman Dewangan static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
4026c7a7a0eSLaxman Dewangan 			struct platform_device *pdev,
4036c7a7a0eSLaxman Dewangan 			struct of_regulator_match **tps65090_reg_matches)
4046c7a7a0eSLaxman Dewangan {
4056c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = NULL;
4066c7a7a0eSLaxman Dewangan 	return NULL;
4076c7a7a0eSLaxman Dewangan }
4086c7a7a0eSLaxman Dewangan #endif
4096c7a7a0eSLaxman Dewangan 
410a5023574SBill Pemberton static int tps65090_regulator_probe(struct platform_device *pdev)
411452534e5SVenu Byravarasu {
41206c4998bSAxel Lin 	struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
413452534e5SVenu Byravarasu 	struct tps65090_regulator *ri = NULL;
414c172708dSMark Brown 	struct regulator_config config = { };
415452534e5SVenu Byravarasu 	struct regulator_dev *rdev;
41624282a1cSLaxman Dewangan 	struct tps65090_regulator_plat_data *tps_pdata;
41724282a1cSLaxman Dewangan 	struct tps65090_regulator *pmic;
41824282a1cSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
4196c7a7a0eSLaxman Dewangan 	struct of_regulator_match *tps65090_reg_matches = NULL;
42024282a1cSLaxman Dewangan 	int num;
42124282a1cSLaxman Dewangan 	int ret;
422452534e5SVenu Byravarasu 
42324282a1cSLaxman Dewangan 	dev_dbg(&pdev->dev, "Probing regulator\n");
424452534e5SVenu Byravarasu 
42524282a1cSLaxman Dewangan 	tps65090_pdata = dev_get_platdata(pdev->dev.parent);
4266c7a7a0eSLaxman Dewangan 	if (!tps65090_pdata && tps65090_mfd->dev->of_node)
4276c7a7a0eSLaxman Dewangan 		tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
4286c7a7a0eSLaxman Dewangan 					&tps65090_reg_matches);
4296c7a7a0eSLaxman Dewangan 	if (IS_ERR_OR_NULL(tps65090_pdata)) {
43024282a1cSLaxman Dewangan 		dev_err(&pdev->dev, "Platform data missing\n");
4316c7a7a0eSLaxman Dewangan 		return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
432452534e5SVenu Byravarasu 	}
433452534e5SVenu Byravarasu 
434a86854d0SKees Cook 	pmic = devm_kcalloc(&pdev->dev,
435a86854d0SKees Cook 			    TPS65090_REGULATOR_MAX, sizeof(*pmic),
43624282a1cSLaxman Dewangan 			    GFP_KERNEL);
4370ad91c69SSachin Kamat 	if (!pmic)
43824282a1cSLaxman Dewangan 		return -ENOMEM;
439452534e5SVenu Byravarasu 
4408620ca9fSLaxman Dewangan 	for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
44124282a1cSLaxman Dewangan 		tps_pdata = tps65090_pdata->reg_pdata[num];
44224282a1cSLaxman Dewangan 
44324282a1cSLaxman Dewangan 		ri = &pmic[num];
44424282a1cSLaxman Dewangan 		ri->dev = &pdev->dev;
44524282a1cSLaxman Dewangan 		ri->desc = &tps65090_regulator_desc[num];
446c122c5b6SDoug Anderson 		if (tps_pdata) {
447c122c5b6SDoug Anderson 			ri->overcurrent_wait_valid =
448c122c5b6SDoug Anderson 				tps_pdata->overcurrent_wait_valid;
44929041449SDoug Anderson 			ri->overcurrent_wait = tps_pdata->overcurrent_wait;
450c122c5b6SDoug Anderson 		}
45124282a1cSLaxman Dewangan 
45224282a1cSLaxman Dewangan 		/*
45324282a1cSLaxman Dewangan 		 * TPS5090 DCDC support the control from external digital input.
454f329b175SLaxman Dewangan 		 * Configure it as per platform data.
45524282a1cSLaxman Dewangan 		 */
45624282a1cSLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
457f329b175SLaxman Dewangan 			if (tps_pdata->enable_ext_control) {
4583012e814SLinus Walleij 				config.ena_gpiod = tps_pdata->gpiod;
459f329b175SLaxman Dewangan 				ri->desc->ops = &tps65090_ext_control_ops;
460f329b175SLaxman Dewangan 			} else {
46124282a1cSLaxman Dewangan 				ret = tps65090_regulator_disable_ext_control(
46224282a1cSLaxman Dewangan 						ri, tps_pdata);
46324282a1cSLaxman Dewangan 				if (ret < 0) {
46424282a1cSLaxman Dewangan 					dev_err(&pdev->dev,
46524282a1cSLaxman Dewangan 						"failed disable ext control\n");
4669738efaeSSachin Kamat 					return ret;
46724282a1cSLaxman Dewangan 				}
46824282a1cSLaxman Dewangan 			}
469f329b175SLaxman Dewangan 		}
470f329b175SLaxman Dewangan 
4716c7a7a0eSLaxman Dewangan 		config.dev = pdev->dev.parent;
47224282a1cSLaxman Dewangan 		config.driver_data = ri;
47324282a1cSLaxman Dewangan 		config.regmap = tps65090_mfd->rmap;
47424282a1cSLaxman Dewangan 		if (tps_pdata)
47524282a1cSLaxman Dewangan 			config.init_data = tps_pdata->reg_init_data;
47624282a1cSLaxman Dewangan 		else
47724282a1cSLaxman Dewangan 			config.init_data = NULL;
4786c7a7a0eSLaxman Dewangan 		if (tps65090_reg_matches)
4796c7a7a0eSLaxman Dewangan 			config.of_node = tps65090_reg_matches[num].of_node;
4806c7a7a0eSLaxman Dewangan 		else
4816c7a7a0eSLaxman Dewangan 			config.of_node = NULL;
48224282a1cSLaxman Dewangan 
4839738efaeSSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
48424282a1cSLaxman Dewangan 		if (IS_ERR(rdev)) {
48524282a1cSLaxman Dewangan 			dev_err(&pdev->dev, "failed to register regulator %s\n",
48624282a1cSLaxman Dewangan 				ri->desc->name);
4879738efaeSSachin Kamat 			return PTR_ERR(rdev);
48824282a1cSLaxman Dewangan 		}
48924282a1cSLaxman Dewangan 		ri->rdev = rdev;
490f329b175SLaxman Dewangan 
49129041449SDoug Anderson 		if (ri->overcurrent_wait_valid) {
49229041449SDoug Anderson 			ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
49329041449SDoug Anderson 			if (ret < 0)
49429041449SDoug Anderson 				return ret;
49529041449SDoug Anderson 		}
49629041449SDoug Anderson 
497f329b175SLaxman Dewangan 		/* Enable external control if it is require */
498f329b175SLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
499f329b175SLaxman Dewangan 				tps_pdata->enable_ext_control) {
500f329b175SLaxman Dewangan 			ret = tps65090_config_ext_control(ri, true);
5019738efaeSSachin Kamat 			if (ret < 0)
5029738efaeSSachin Kamat 				return ret;
503f329b175SLaxman Dewangan 		}
50424282a1cSLaxman Dewangan 	}
50524282a1cSLaxman Dewangan 
50624282a1cSLaxman Dewangan 	platform_set_drvdata(pdev, pmic);
507452534e5SVenu Byravarasu 	return 0;
508452534e5SVenu Byravarasu }
509452534e5SVenu Byravarasu 
510452534e5SVenu Byravarasu static struct platform_driver tps65090_regulator_driver = {
511452534e5SVenu Byravarasu 	.driver	= {
5128620ca9fSLaxman Dewangan 		.name	= "tps65090-pmic",
513452534e5SVenu Byravarasu 	},
514452534e5SVenu Byravarasu 	.probe		= tps65090_regulator_probe,
515452534e5SVenu Byravarasu };
516452534e5SVenu Byravarasu 
517452534e5SVenu Byravarasu static int __init tps65090_regulator_init(void)
518452534e5SVenu Byravarasu {
519452534e5SVenu Byravarasu 	return platform_driver_register(&tps65090_regulator_driver);
520452534e5SVenu Byravarasu }
521452534e5SVenu Byravarasu subsys_initcall(tps65090_regulator_init);
522452534e5SVenu Byravarasu 
523452534e5SVenu Byravarasu static void __exit tps65090_regulator_exit(void)
524452534e5SVenu Byravarasu {
525452534e5SVenu Byravarasu 	platform_driver_unregister(&tps65090_regulator_driver);
526452534e5SVenu Byravarasu }
527452534e5SVenu Byravarasu module_exit(tps65090_regulator_exit);
528452534e5SVenu Byravarasu 
529452534e5SVenu Byravarasu MODULE_DESCRIPTION("tps65090 regulator driver");
530452534e5SVenu Byravarasu MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
531452534e5SVenu Byravarasu MODULE_LICENSE("GPL v2");
532d95420b8SAxel Lin MODULE_ALIAS("platform:tps65090-pmic");
533