19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2452534e5SVenu Byravarasu /*
3452534e5SVenu Byravarasu  * Regulator driver for tps65090 power management chip.
4452534e5SVenu Byravarasu  *
5452534e5SVenu Byravarasu  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
6452534e5SVenu Byravarasu 
7452534e5SVenu Byravarasu  */
8452534e5SVenu Byravarasu 
9452534e5SVenu Byravarasu #include <linux/module.h>
10ed11f1eaSDoug Anderson #include <linux/delay.h>
11452534e5SVenu Byravarasu #include <linux/init.h>
123012e814SLinus Walleij #include <linux/of.h>
133012e814SLinus Walleij #include <linux/gpio/consumer.h>
14452534e5SVenu Byravarasu #include <linux/slab.h>
15452534e5SVenu Byravarasu #include <linux/err.h>
16452534e5SVenu Byravarasu #include <linux/platform_device.h>
17452534e5SVenu Byravarasu #include <linux/regulator/driver.h>
18452534e5SVenu Byravarasu #include <linux/regulator/machine.h>
196c7a7a0eSLaxman Dewangan #include <linux/regulator/of_regulator.h>
20452534e5SVenu Byravarasu #include <linux/mfd/tps65090.h>
21452534e5SVenu Byravarasu 
22ed11f1eaSDoug Anderson #define MAX_CTRL_READ_TRIES	5
23ed11f1eaSDoug Anderson #define MAX_FET_ENABLE_TRIES	1000
24ed11f1eaSDoug Anderson 
25ed11f1eaSDoug Anderson #define CTRL_EN_BIT		0 /* Regulator enable bit, active high */
2629041449SDoug Anderson #define CTRL_WT_BIT		2 /* Regulator wait time 0 bit */
27ed11f1eaSDoug Anderson #define CTRL_PG_BIT		4 /* Regulator power good bit, 1=good */
28ed11f1eaSDoug Anderson #define CTRL_TO_BIT		7 /* Regulator timeout bit, 1=wait */
2929041449SDoug Anderson 
3029041449SDoug Anderson #define MAX_OVERCURRENT_WAIT	3 /* Overcurrent wait must be <= this */
3129041449SDoug Anderson 
3229041449SDoug Anderson /**
3329041449SDoug Anderson  * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
3429041449SDoug Anderson  *
3529041449SDoug Anderson  * @dev: Pointer to our device.
3629041449SDoug Anderson  * @desc: The struct regulator_desc for the regulator.
3729041449SDoug Anderson  * @rdev: The struct regulator_dev for the regulator.
3829041449SDoug Anderson  * @overcurrent_wait_valid: True if overcurrent_wait is valid.
3929041449SDoug Anderson  * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
4029041449SDoug Anderson  */
4129041449SDoug Anderson 
42452534e5SVenu Byravarasu struct tps65090_regulator {
43452534e5SVenu Byravarasu 	struct device		*dev;
4424282a1cSLaxman Dewangan 	struct regulator_desc	*desc;
4524282a1cSLaxman Dewangan 	struct regulator_dev	*rdev;
4629041449SDoug Anderson 	bool			overcurrent_wait_valid;
4729041449SDoug Anderson 	int			overcurrent_wait;
48452534e5SVenu Byravarasu };
49452534e5SVenu Byravarasu 
507d844ac3SRikard Falkeborn static const struct regulator_ops tps65090_ext_control_ops = {
51f329b175SLaxman Dewangan };
52f329b175SLaxman Dewangan 
5329041449SDoug Anderson /**
5429041449SDoug Anderson  * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
5529041449SDoug Anderson  *
5629041449SDoug Anderson  * This will set the overcurrent wait time based on what's in the regulator
5729041449SDoug Anderson  * info.
5829041449SDoug Anderson  *
5929041449SDoug Anderson  * @ri:		Overall regulator data
6029041449SDoug Anderson  * @rdev:	Regulator device
6129041449SDoug Anderson  *
6229041449SDoug Anderson  * Return: 0 if no error, non-zero if there was an error writing the register.
6329041449SDoug Anderson  */
tps65090_reg_set_overcurrent_wait(struct tps65090_regulator * ri,struct regulator_dev * rdev)6429041449SDoug Anderson static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
6529041449SDoug Anderson 					     struct regulator_dev *rdev)
6629041449SDoug Anderson {
6729041449SDoug Anderson 	int ret;
6829041449SDoug Anderson 
6929041449SDoug Anderson 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
7029041449SDoug Anderson 				 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
7129041449SDoug Anderson 				 ri->overcurrent_wait << CTRL_WT_BIT);
7229041449SDoug Anderson 	if (ret) {
7329041449SDoug Anderson 		dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
7429041449SDoug Anderson 			rdev->desc->enable_reg);
7529041449SDoug Anderson 	}
7629041449SDoug Anderson 
7729041449SDoug Anderson 	return ret;
7829041449SDoug Anderson }
7929041449SDoug Anderson 
80ed11f1eaSDoug Anderson /**
81ed11f1eaSDoug Anderson  * tps65090_try_enable_fet - Try to enable a FET
82ed11f1eaSDoug Anderson  *
83ed11f1eaSDoug Anderson  * @rdev:	Regulator device
84ed11f1eaSDoug Anderson  *
85ed11f1eaSDoug Anderson  * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get
86ed11f1eaSDoug Anderson  * set, or some other -ve value if another error occurred (e.g. i2c error)
87ed11f1eaSDoug Anderson  */
tps65090_try_enable_fet(struct regulator_dev * rdev)88ed11f1eaSDoug Anderson static int tps65090_try_enable_fet(struct regulator_dev *rdev)
89ed11f1eaSDoug Anderson {
90ed11f1eaSDoug Anderson 	unsigned int control;
91ed11f1eaSDoug Anderson 	int ret, i;
92ed11f1eaSDoug Anderson 
93ed11f1eaSDoug Anderson 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
94ed11f1eaSDoug Anderson 				 rdev->desc->enable_mask,
95ed11f1eaSDoug Anderson 				 rdev->desc->enable_mask);
96ed11f1eaSDoug Anderson 	if (ret < 0) {
97ed11f1eaSDoug Anderson 		dev_err(&rdev->dev, "Error in updating reg %#x\n",
98ed11f1eaSDoug Anderson 			rdev->desc->enable_reg);
99ed11f1eaSDoug Anderson 		return ret;
100ed11f1eaSDoug Anderson 	}
101ed11f1eaSDoug Anderson 
102ed11f1eaSDoug Anderson 	for (i = 0; i < MAX_CTRL_READ_TRIES; i++) {
103ed11f1eaSDoug Anderson 		ret = regmap_read(rdev->regmap, rdev->desc->enable_reg,
104ed11f1eaSDoug Anderson 				  &control);
105ed11f1eaSDoug Anderson 		if (ret < 0)
106ed11f1eaSDoug Anderson 			return ret;
107ed11f1eaSDoug Anderson 
108ed11f1eaSDoug Anderson 		if (!(control & BIT(CTRL_TO_BIT)))
109ed11f1eaSDoug Anderson 			break;
110ed11f1eaSDoug Anderson 
111ed11f1eaSDoug Anderson 		usleep_range(1000, 1500);
112ed11f1eaSDoug Anderson 	}
113ed11f1eaSDoug Anderson 	if (!(control & BIT(CTRL_PG_BIT)))
114ed11f1eaSDoug Anderson 		return -ENOTRECOVERABLE;
115ed11f1eaSDoug Anderson 
116ed11f1eaSDoug Anderson 	return 0;
117ed11f1eaSDoug Anderson }
118ed11f1eaSDoug Anderson 
119ed11f1eaSDoug Anderson /**
120ed11f1eaSDoug Anderson  * tps65090_fet_enable - Enable a FET, trying a few times if it fails
121ed11f1eaSDoug Anderson  *
122ed11f1eaSDoug Anderson  * Some versions of the tps65090 have issues when turning on the FETs.
123ed11f1eaSDoug Anderson  * This function goes through several steps to ensure the best chance of the
124ed11f1eaSDoug Anderson  * FET going on.  Specifically:
125ed11f1eaSDoug Anderson  * - We'll make sure that we bump the "overcurrent wait" to the maximum, which
126ed11f1eaSDoug Anderson  *   increases the chances that we'll turn on properly.
127ed11f1eaSDoug Anderson  * - We'll retry turning the FET on multiple times (turning off in between).
128ed11f1eaSDoug Anderson  *
129ed11f1eaSDoug Anderson  * @rdev:	Regulator device
130ed11f1eaSDoug Anderson  *
131ed11f1eaSDoug Anderson  * Return: 0 if ok, non-zero if it fails.
132ed11f1eaSDoug Anderson  */
tps65090_fet_enable(struct regulator_dev * rdev)133ed11f1eaSDoug Anderson static int tps65090_fet_enable(struct regulator_dev *rdev)
134ed11f1eaSDoug Anderson {
135ed11f1eaSDoug Anderson 	int ret, tries;
136ed11f1eaSDoug Anderson 
137ed11f1eaSDoug Anderson 	/*
138ed11f1eaSDoug Anderson 	 * Try enabling multiple times until we succeed since sometimes the
139ed11f1eaSDoug Anderson 	 * first try times out.
140ed11f1eaSDoug Anderson 	 */
141ed11f1eaSDoug Anderson 	tries = 0;
142ed11f1eaSDoug Anderson 	while (true) {
143ed11f1eaSDoug Anderson 		ret = tps65090_try_enable_fet(rdev);
144ed11f1eaSDoug Anderson 		if (!ret)
145ed11f1eaSDoug Anderson 			break;
146ed11f1eaSDoug Anderson 		if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES)
147ed11f1eaSDoug Anderson 			goto err;
148ed11f1eaSDoug Anderson 
149ed11f1eaSDoug Anderson 		/* Try turning the FET off (and then on again) */
150ed11f1eaSDoug Anderson 		ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
151ed11f1eaSDoug Anderson 					 rdev->desc->enable_mask, 0);
152ed11f1eaSDoug Anderson 		if (ret)
153ed11f1eaSDoug Anderson 			goto err;
154ed11f1eaSDoug Anderson 
155ed11f1eaSDoug Anderson 		tries++;
156ed11f1eaSDoug Anderson 	}
157ed11f1eaSDoug Anderson 
158ed11f1eaSDoug Anderson 	if (tries)
159ed11f1eaSDoug Anderson 		dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n",
160ed11f1eaSDoug Anderson 			 rdev->desc->enable_reg, tries);
161ed11f1eaSDoug Anderson 
162ed11f1eaSDoug Anderson 	return 0;
163ed11f1eaSDoug Anderson err:
164ed11f1eaSDoug Anderson 	dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
165ed11f1eaSDoug Anderson 	WARN_ON(1);
166ed11f1eaSDoug Anderson 
167ed11f1eaSDoug Anderson 	return ret;
168ed11f1eaSDoug Anderson }
169ed11f1eaSDoug Anderson 
1707d844ac3SRikard Falkeborn static const struct regulator_ops tps65090_reg_control_ops = {
17106c4998bSAxel Lin 	.enable		= regulator_enable_regmap,
17206c4998bSAxel Lin 	.disable	= regulator_disable_regmap,
17306c4998bSAxel Lin 	.is_enabled	= regulator_is_enabled_regmap,
174452534e5SVenu Byravarasu };
175452534e5SVenu Byravarasu 
1767d844ac3SRikard Falkeborn static const struct regulator_ops tps65090_fet_control_ops = {
177ed11f1eaSDoug Anderson 	.enable		= tps65090_fet_enable,
178ed11f1eaSDoug Anderson 	.disable	= regulator_disable_regmap,
179ed11f1eaSDoug Anderson 	.is_enabled	= regulator_is_enabled_regmap,
180ed11f1eaSDoug Anderson };
181ed11f1eaSDoug Anderson 
1827d844ac3SRikard Falkeborn static const struct regulator_ops tps65090_ldo_ops = {
1833a81ef8cSLaxman Dewangan };
1843a81ef8cSLaxman Dewangan 
1854f2352cfSJavier Martinez Canillas #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
186452534e5SVenu Byravarasu {							\
18724282a1cSLaxman Dewangan 	.name = "TPS65090_RAILS"#_id,			\
18824282a1cSLaxman Dewangan 	.supply_name = _sname,				\
1898620ca9fSLaxman Dewangan 	.id = TPS65090_REGULATOR_##_id,			\
1904f2352cfSJavier Martinez Canillas 	.n_voltages = _nvolt,				\
19124282a1cSLaxman Dewangan 	.ops = &_ops,					\
1924f2352cfSJavier Martinez Canillas 	.fixed_uV = _volt,				\
19324282a1cSLaxman Dewangan 	.enable_reg = _en_reg,				\
194ed11f1eaSDoug Anderson 	.enable_val = _en_bits,				\
195ed11f1eaSDoug Anderson 	.enable_mask = _en_bits,			\
196452534e5SVenu Byravarasu 	.type = REGULATOR_VOLTAGE,			\
197452534e5SVenu Byravarasu 	.owner = THIS_MODULE,				\
198452534e5SVenu Byravarasu }
199452534e5SVenu Byravarasu 
2004f2352cfSJavier Martinez Canillas #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
2014f2352cfSJavier Martinez Canillas 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
2024f2352cfSJavier Martinez Canillas 
2034f2352cfSJavier Martinez Canillas #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
2044f2352cfSJavier Martinez Canillas 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
2054f2352cfSJavier Martinez Canillas 
20624282a1cSLaxman Dewangan static struct regulator_desc tps65090_regulator_desc[] = {
2074f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(DCDC1, "vsys1",   0x0C, BIT(CTRL_EN_BIT), 5000000,
208ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
2094f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(DCDC2, "vsys2",   0x0D, BIT(CTRL_EN_BIT), 3300000,
210ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
2114f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(DCDC3, "vsys3",   0x0E, BIT(CTRL_EN_BIT),
212ed11f1eaSDoug Anderson 			    tps65090_reg_control_ops),
213ed11f1eaSDoug Anderson 
2144f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET1,  "infet1",  0x0F,
215ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
216ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2174f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET2,  "infet2",  0x10,
218ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
219ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2204f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET3,  "infet3",  0x11,
221ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
222ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2234f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET4,  "infet4",  0x12,
224ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
225ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2264f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET5,  "infet5",  0x13,
227ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
228ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2294f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET6,  "infet6",  0x14,
230ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
231ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
2324f2352cfSJavier Martinez Canillas 	tps65090_REG_SWITCH(FET7,  "infet7",  0x15,
233ed11f1eaSDoug Anderson 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
234ed11f1eaSDoug Anderson 			    tps65090_fet_control_ops),
235ed11f1eaSDoug Anderson 
2364f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(LDO1,  "vsys-l1", 0, 0, 5000000,
237ed11f1eaSDoug Anderson 			    tps65090_ldo_ops),
2384f2352cfSJavier Martinez Canillas 	tps65090_REG_FIXEDV(LDO2,  "vsys-l2", 0, 0, 3300000,
239ed11f1eaSDoug Anderson 			    tps65090_ldo_ops),
240452534e5SVenu Byravarasu };
241452534e5SVenu Byravarasu 
is_dcdc(int id)24224282a1cSLaxman Dewangan static inline bool is_dcdc(int id)
243452534e5SVenu Byravarasu {
24424282a1cSLaxman Dewangan 	switch (id) {
2458620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC1:
2468620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC2:
2478620ca9fSLaxman Dewangan 	case TPS65090_REGULATOR_DCDC3:
24824282a1cSLaxman Dewangan 		return true;
24924282a1cSLaxman Dewangan 	default:
25024282a1cSLaxman Dewangan 		return false;
251452534e5SVenu Byravarasu 	}
25224282a1cSLaxman Dewangan }
25324282a1cSLaxman Dewangan 
tps65090_config_ext_control(struct tps65090_regulator * ri,bool enable)254a5023574SBill Pemberton static int tps65090_config_ext_control(
25524282a1cSLaxman Dewangan 	struct tps65090_regulator *ri, bool enable)
25624282a1cSLaxman Dewangan {
25724282a1cSLaxman Dewangan 	int ret;
25824282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
25924282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
26024282a1cSLaxman Dewangan 
26124282a1cSLaxman Dewangan 	if (enable)
26224282a1cSLaxman Dewangan 		ret = tps65090_set_bits(parent, reg_en_reg, 1);
26324282a1cSLaxman Dewangan 	else
26424282a1cSLaxman Dewangan 		ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
26524282a1cSLaxman Dewangan 	if (ret < 0)
26624282a1cSLaxman Dewangan 		dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
26724282a1cSLaxman Dewangan 	return ret;
26824282a1cSLaxman Dewangan }
26924282a1cSLaxman Dewangan 
tps65090_regulator_disable_ext_control(struct tps65090_regulator * ri,struct tps65090_regulator_plat_data * tps_pdata)270a5023574SBill Pemberton static int tps65090_regulator_disable_ext_control(
27124282a1cSLaxman Dewangan 		struct tps65090_regulator *ri,
27224282a1cSLaxman Dewangan 		struct tps65090_regulator_plat_data *tps_pdata)
27324282a1cSLaxman Dewangan {
27424282a1cSLaxman Dewangan 	int ret = 0;
27524282a1cSLaxman Dewangan 	struct device *parent = ri->dev->parent;
27624282a1cSLaxman Dewangan 	unsigned int reg_en_reg = ri->desc->enable_reg;
27724282a1cSLaxman Dewangan 
27824282a1cSLaxman Dewangan 	/*
27924282a1cSLaxman Dewangan 	 * First enable output for internal control if require.
28024282a1cSLaxman Dewangan 	 * And then disable external control.
28124282a1cSLaxman Dewangan 	 */
28224282a1cSLaxman Dewangan 	if (tps_pdata->reg_init_data->constraints.always_on ||
28324282a1cSLaxman Dewangan 			tps_pdata->reg_init_data->constraints.boot_on) {
28424282a1cSLaxman Dewangan 		ret =  tps65090_set_bits(parent, reg_en_reg, 0);
28524282a1cSLaxman Dewangan 		if (ret < 0) {
28624282a1cSLaxman Dewangan 			dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
28724282a1cSLaxman Dewangan 			return ret;
28824282a1cSLaxman Dewangan 		}
28924282a1cSLaxman Dewangan 	}
29024282a1cSLaxman Dewangan 	return tps65090_config_ext_control(ri, false);
291452534e5SVenu Byravarasu }
292452534e5SVenu Byravarasu 
2936c7a7a0eSLaxman Dewangan #ifdef CONFIG_OF
2946c7a7a0eSLaxman Dewangan static struct of_regulator_match tps65090_matches[] = {
2956c7a7a0eSLaxman Dewangan 	{ .name = "dcdc1", },
2966c7a7a0eSLaxman Dewangan 	{ .name = "dcdc2", },
2976c7a7a0eSLaxman Dewangan 	{ .name = "dcdc3", },
2986c7a7a0eSLaxman Dewangan 	{ .name = "fet1",  },
2996c7a7a0eSLaxman Dewangan 	{ .name = "fet2",  },
3006c7a7a0eSLaxman Dewangan 	{ .name = "fet3",  },
3016c7a7a0eSLaxman Dewangan 	{ .name = "fet4",  },
3026c7a7a0eSLaxman Dewangan 	{ .name = "fet5",  },
3036c7a7a0eSLaxman Dewangan 	{ .name = "fet6",  },
3046c7a7a0eSLaxman Dewangan 	{ .name = "fet7",  },
3056c7a7a0eSLaxman Dewangan 	{ .name = "ldo1",  },
3066c7a7a0eSLaxman Dewangan 	{ .name = "ldo2",  },
3076c7a7a0eSLaxman Dewangan };
3086c7a7a0eSLaxman Dewangan 
tps65090_parse_dt_reg_data(struct platform_device * pdev,struct of_regulator_match ** tps65090_reg_matches)3096c7a7a0eSLaxman Dewangan static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
3106c7a7a0eSLaxman Dewangan 		struct platform_device *pdev,
3116c7a7a0eSLaxman Dewangan 		struct of_regulator_match **tps65090_reg_matches)
3126c7a7a0eSLaxman Dewangan {
3136c7a7a0eSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
3146c7a7a0eSLaxman Dewangan 	struct device_node *np = pdev->dev.parent->of_node;
3156c7a7a0eSLaxman Dewangan 	struct device_node *regulators;
3166c7a7a0eSLaxman Dewangan 	int idx = 0, ret;
3176c7a7a0eSLaxman Dewangan 	struct tps65090_regulator_plat_data *reg_pdata;
3186c7a7a0eSLaxman Dewangan 
3196c7a7a0eSLaxman Dewangan 	tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
3206c7a7a0eSLaxman Dewangan 				GFP_KERNEL);
3210ad91c69SSachin Kamat 	if (!tps65090_pdata)
3226c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3236c7a7a0eSLaxman Dewangan 
324a86854d0SKees Cook 	reg_pdata = devm_kcalloc(&pdev->dev,
325a86854d0SKees Cook 				 TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
326a86854d0SKees Cook 				 GFP_KERNEL);
3270ad91c69SSachin Kamat 	if (!reg_pdata)
3286c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENOMEM);
3296c7a7a0eSLaxman Dewangan 
3304c850eadSLaxman Dewangan 	regulators = of_get_child_by_name(np, "regulators");
3316c7a7a0eSLaxman Dewangan 	if (!regulators) {
3326c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev, "regulator node not found\n");
3336c7a7a0eSLaxman Dewangan 		return ERR_PTR(-ENODEV);
3346c7a7a0eSLaxman Dewangan 	}
3356c7a7a0eSLaxman Dewangan 
33609a228e7SAxel Lin 	ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
3376c7a7a0eSLaxman Dewangan 			ARRAY_SIZE(tps65090_matches));
338026cdfe6SSachin Kamat 	of_node_put(regulators);
3396c7a7a0eSLaxman Dewangan 	if (ret < 0) {
3406c7a7a0eSLaxman Dewangan 		dev_err(&pdev->dev,
3416c7a7a0eSLaxman Dewangan 			"Error parsing regulator init data: %d\n", ret);
3426c7a7a0eSLaxman Dewangan 		return ERR_PTR(ret);
3436c7a7a0eSLaxman Dewangan 	}
3446c7a7a0eSLaxman Dewangan 
3456c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = tps65090_matches;
3466c7a7a0eSLaxman Dewangan 	for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
3476c7a7a0eSLaxman Dewangan 		struct regulator_init_data *ri_data;
3486c7a7a0eSLaxman Dewangan 		struct tps65090_regulator_plat_data *rpdata;
34951d98ff8SDmitry Torokhov 		struct device_node *np;
3506c7a7a0eSLaxman Dewangan 
3516c7a7a0eSLaxman Dewangan 		rpdata = &reg_pdata[idx];
3526c7a7a0eSLaxman Dewangan 		ri_data = tps65090_matches[idx].init_data;
35351d98ff8SDmitry Torokhov 		if (!ri_data)
35451d98ff8SDmitry Torokhov 			continue;
35551d98ff8SDmitry Torokhov 
35651d98ff8SDmitry Torokhov 		np = tps65090_matches[idx].of_node;
35751d98ff8SDmitry Torokhov 		if (!np)
3586c7a7a0eSLaxman Dewangan 			continue;
3596c7a7a0eSLaxman Dewangan 
3606c7a7a0eSLaxman Dewangan 		rpdata->reg_init_data = ri_data;
36151d98ff8SDmitry Torokhov 		rpdata->enable_ext_control = of_property_read_bool(np,
3626c7a7a0eSLaxman Dewangan 						"ti,enable-ext-control");
3633012e814SLinus Walleij 		if (rpdata->enable_ext_control) {
3643012e814SLinus Walleij 			enum gpiod_flags gflags;
3653012e814SLinus Walleij 
3663012e814SLinus Walleij 			if (ri_data->constraints.always_on ||
3673012e814SLinus Walleij 			    ri_data->constraints.boot_on)
3683012e814SLinus Walleij 				gflags = GPIOD_OUT_HIGH;
3693012e814SLinus Walleij 			else
3703012e814SLinus Walleij 				gflags = GPIOD_OUT_LOW;
37163239e4bSLinus Walleij 			gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
3723012e814SLinus Walleij 
37351d98ff8SDmitry Torokhov 			rpdata->gpiod = devm_fwnode_gpiod_get(
37451d98ff8SDmitry Torokhov 							&pdev->dev,
37551d98ff8SDmitry Torokhov 							of_fwnode_handle(np),
37651d98ff8SDmitry Torokhov 							"dcdc-ext-control",
3773012e814SLinus Walleij 							gflags,
3783012e814SLinus Walleij 							"tps65090");
379025bf377SWaibel Georg 			if (PTR_ERR(rpdata->gpiod) == -ENOENT) {
3803012e814SLinus Walleij 				dev_err(&pdev->dev,
3813012e814SLinus Walleij 					"could not find DCDC external control GPIO\n");
382025bf377SWaibel Georg 				rpdata->gpiod = NULL;
383025bf377SWaibel Georg 			} else if (IS_ERR(rpdata->gpiod))
384025bf377SWaibel Georg 				return ERR_CAST(rpdata->gpiod);
3853012e814SLinus Walleij 		}
3866c7a7a0eSLaxman Dewangan 
38751d98ff8SDmitry Torokhov 		if (of_property_read_u32(np, "ti,overcurrent-wait",
38829041449SDoug Anderson 					 &rpdata->overcurrent_wait) == 0)
38929041449SDoug Anderson 			rpdata->overcurrent_wait_valid = true;
39029041449SDoug Anderson 
3916c7a7a0eSLaxman Dewangan 		tps65090_pdata->reg_pdata[idx] = rpdata;
3926c7a7a0eSLaxman Dewangan 	}
3936c7a7a0eSLaxman Dewangan 	return tps65090_pdata;
3946c7a7a0eSLaxman Dewangan }
3956c7a7a0eSLaxman Dewangan #else
tps65090_parse_dt_reg_data(struct platform_device * pdev,struct of_regulator_match ** tps65090_reg_matches)3966c7a7a0eSLaxman Dewangan static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
3976c7a7a0eSLaxman Dewangan 			struct platform_device *pdev,
3986c7a7a0eSLaxman Dewangan 			struct of_regulator_match **tps65090_reg_matches)
3996c7a7a0eSLaxman Dewangan {
4006c7a7a0eSLaxman Dewangan 	*tps65090_reg_matches = NULL;
4016c7a7a0eSLaxman Dewangan 	return NULL;
4026c7a7a0eSLaxman Dewangan }
4036c7a7a0eSLaxman Dewangan #endif
4046c7a7a0eSLaxman Dewangan 
tps65090_regulator_probe(struct platform_device * pdev)405a5023574SBill Pemberton static int tps65090_regulator_probe(struct platform_device *pdev)
406452534e5SVenu Byravarasu {
40706c4998bSAxel Lin 	struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
408452534e5SVenu Byravarasu 	struct tps65090_regulator *ri = NULL;
409c172708dSMark Brown 	struct regulator_config config = { };
410452534e5SVenu Byravarasu 	struct regulator_dev *rdev;
41124282a1cSLaxman Dewangan 	struct tps65090_regulator_plat_data *tps_pdata;
41224282a1cSLaxman Dewangan 	struct tps65090_regulator *pmic;
41324282a1cSLaxman Dewangan 	struct tps65090_platform_data *tps65090_pdata;
4146c7a7a0eSLaxman Dewangan 	struct of_regulator_match *tps65090_reg_matches = NULL;
41524282a1cSLaxman Dewangan 	int num;
41624282a1cSLaxman Dewangan 	int ret;
417452534e5SVenu Byravarasu 
41824282a1cSLaxman Dewangan 	dev_dbg(&pdev->dev, "Probing regulator\n");
419452534e5SVenu Byravarasu 
42024282a1cSLaxman Dewangan 	tps65090_pdata = dev_get_platdata(pdev->dev.parent);
4216c7a7a0eSLaxman Dewangan 	if (!tps65090_pdata && tps65090_mfd->dev->of_node)
4226c7a7a0eSLaxman Dewangan 		tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
4236c7a7a0eSLaxman Dewangan 					&tps65090_reg_matches);
4246c7a7a0eSLaxman Dewangan 	if (IS_ERR_OR_NULL(tps65090_pdata)) {
42524282a1cSLaxman Dewangan 		dev_err(&pdev->dev, "Platform data missing\n");
4266c7a7a0eSLaxman Dewangan 		return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
427452534e5SVenu Byravarasu 	}
428452534e5SVenu Byravarasu 
429a86854d0SKees Cook 	pmic = devm_kcalloc(&pdev->dev,
430a86854d0SKees Cook 			    TPS65090_REGULATOR_MAX, sizeof(*pmic),
43124282a1cSLaxman Dewangan 			    GFP_KERNEL);
4320ad91c69SSachin Kamat 	if (!pmic)
43324282a1cSLaxman Dewangan 		return -ENOMEM;
434452534e5SVenu Byravarasu 
4358620ca9fSLaxman Dewangan 	for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
43624282a1cSLaxman Dewangan 		tps_pdata = tps65090_pdata->reg_pdata[num];
43724282a1cSLaxman Dewangan 
43824282a1cSLaxman Dewangan 		ri = &pmic[num];
43924282a1cSLaxman Dewangan 		ri->dev = &pdev->dev;
44024282a1cSLaxman Dewangan 		ri->desc = &tps65090_regulator_desc[num];
441c122c5b6SDoug Anderson 		if (tps_pdata) {
442c122c5b6SDoug Anderson 			ri->overcurrent_wait_valid =
443c122c5b6SDoug Anderson 				tps_pdata->overcurrent_wait_valid;
44429041449SDoug Anderson 			ri->overcurrent_wait = tps_pdata->overcurrent_wait;
445c122c5b6SDoug Anderson 		}
44624282a1cSLaxman Dewangan 
44724282a1cSLaxman Dewangan 		/*
44824282a1cSLaxman Dewangan 		 * TPS5090 DCDC support the control from external digital input.
449f329b175SLaxman Dewangan 		 * Configure it as per platform data.
45024282a1cSLaxman Dewangan 		 */
45124282a1cSLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
452f329b175SLaxman Dewangan 			if (tps_pdata->enable_ext_control) {
4533012e814SLinus Walleij 				config.ena_gpiod = tps_pdata->gpiod;
454f329b175SLaxman Dewangan 				ri->desc->ops = &tps65090_ext_control_ops;
455f329b175SLaxman Dewangan 			} else {
45624282a1cSLaxman Dewangan 				ret = tps65090_regulator_disable_ext_control(
45724282a1cSLaxman Dewangan 						ri, tps_pdata);
45824282a1cSLaxman Dewangan 				if (ret < 0) {
45924282a1cSLaxman Dewangan 					dev_err(&pdev->dev,
46024282a1cSLaxman Dewangan 						"failed disable ext control\n");
4619738efaeSSachin Kamat 					return ret;
46224282a1cSLaxman Dewangan 				}
46324282a1cSLaxman Dewangan 			}
464f329b175SLaxman Dewangan 		}
465f329b175SLaxman Dewangan 
4666c7a7a0eSLaxman Dewangan 		config.dev = pdev->dev.parent;
46724282a1cSLaxman Dewangan 		config.driver_data = ri;
46824282a1cSLaxman Dewangan 		config.regmap = tps65090_mfd->rmap;
46924282a1cSLaxman Dewangan 		if (tps_pdata)
47024282a1cSLaxman Dewangan 			config.init_data = tps_pdata->reg_init_data;
47124282a1cSLaxman Dewangan 		else
47224282a1cSLaxman Dewangan 			config.init_data = NULL;
4736c7a7a0eSLaxman Dewangan 		if (tps65090_reg_matches)
4746c7a7a0eSLaxman Dewangan 			config.of_node = tps65090_reg_matches[num].of_node;
4756c7a7a0eSLaxman Dewangan 		else
4766c7a7a0eSLaxman Dewangan 			config.of_node = NULL;
47724282a1cSLaxman Dewangan 
478870311e5SLinus Walleij 		/*
479870311e5SLinus Walleij 		 * Hand the GPIO descriptor management over to the regulator
480870311e5SLinus Walleij 		 * core, remove it from devres management.
481870311e5SLinus Walleij 		 */
482870311e5SLinus Walleij 		if (config.ena_gpiod)
483870311e5SLinus Walleij 			devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
4849738efaeSSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
48524282a1cSLaxman Dewangan 		if (IS_ERR(rdev)) {
48624282a1cSLaxman Dewangan 			dev_err(&pdev->dev, "failed to register regulator %s\n",
48724282a1cSLaxman Dewangan 				ri->desc->name);
4889738efaeSSachin Kamat 			return PTR_ERR(rdev);
48924282a1cSLaxman Dewangan 		}
49024282a1cSLaxman Dewangan 		ri->rdev = rdev;
491f329b175SLaxman Dewangan 
49229041449SDoug Anderson 		if (ri->overcurrent_wait_valid) {
49329041449SDoug Anderson 			ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
49429041449SDoug Anderson 			if (ret < 0)
49529041449SDoug Anderson 				return ret;
49629041449SDoug Anderson 		}
49729041449SDoug Anderson 
498f329b175SLaxman Dewangan 		/* Enable external control if it is require */
499f329b175SLaxman Dewangan 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
500f329b175SLaxman Dewangan 				tps_pdata->enable_ext_control) {
501f329b175SLaxman Dewangan 			ret = tps65090_config_ext_control(ri, true);
5029738efaeSSachin Kamat 			if (ret < 0)
5039738efaeSSachin Kamat 				return ret;
504f329b175SLaxman Dewangan 		}
50524282a1cSLaxman Dewangan 	}
50624282a1cSLaxman Dewangan 
50724282a1cSLaxman Dewangan 	platform_set_drvdata(pdev, pmic);
508452534e5SVenu Byravarasu 	return 0;
509452534e5SVenu Byravarasu }
510452534e5SVenu Byravarasu 
511452534e5SVenu Byravarasu static struct platform_driver tps65090_regulator_driver = {
512452534e5SVenu Byravarasu 	.driver	= {
5138620ca9fSLaxman Dewangan 		.name	= "tps65090-pmic",
514*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
515452534e5SVenu Byravarasu 	},
516452534e5SVenu Byravarasu 	.probe		= tps65090_regulator_probe,
517452534e5SVenu Byravarasu };
518452534e5SVenu Byravarasu 
tps65090_regulator_init(void)519452534e5SVenu Byravarasu static int __init tps65090_regulator_init(void)
520452534e5SVenu Byravarasu {
521452534e5SVenu Byravarasu 	return platform_driver_register(&tps65090_regulator_driver);
522452534e5SVenu Byravarasu }
523452534e5SVenu Byravarasu subsys_initcall(tps65090_regulator_init);
524452534e5SVenu Byravarasu 
tps65090_regulator_exit(void)525452534e5SVenu Byravarasu static void __exit tps65090_regulator_exit(void)
526452534e5SVenu Byravarasu {
527452534e5SVenu Byravarasu 	platform_driver_unregister(&tps65090_regulator_driver);
528452534e5SVenu Byravarasu }
529452534e5SVenu Byravarasu module_exit(tps65090_regulator_exit);
530452534e5SVenu Byravarasu 
531452534e5SVenu Byravarasu MODULE_DESCRIPTION("tps65090 regulator driver");
532452534e5SVenu Byravarasu MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
533452534e5SVenu Byravarasu MODULE_LICENSE("GPL v2");
534d95420b8SAxel Lin MODULE_ALIAS("platform:tps65090-pmic");
535