xref: /OK3568_Linux_fs/kernel/drivers/regulator/tps65090-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Regulator driver for tps65090 power management chip.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/delay.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun #include <linux/regulator/driver.h>
18*4882a593Smuzhiyun #include <linux/regulator/machine.h>
19*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
20*4882a593Smuzhiyun #include <linux/mfd/tps65090.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define MAX_CTRL_READ_TRIES	5
23*4882a593Smuzhiyun #define MAX_FET_ENABLE_TRIES	1000
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define CTRL_EN_BIT		0 /* Regulator enable bit, active high */
26*4882a593Smuzhiyun #define CTRL_WT_BIT		2 /* Regulator wait time 0 bit */
27*4882a593Smuzhiyun #define CTRL_PG_BIT		4 /* Regulator power good bit, 1=good */
28*4882a593Smuzhiyun #define CTRL_TO_BIT		7 /* Regulator timeout bit, 1=wait */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define MAX_OVERCURRENT_WAIT	3 /* Overcurrent wait must be <= this */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * @dev: Pointer to our device.
36*4882a593Smuzhiyun  * @desc: The struct regulator_desc for the regulator.
37*4882a593Smuzhiyun  * @rdev: The struct regulator_dev for the regulator.
38*4882a593Smuzhiyun  * @overcurrent_wait_valid: True if overcurrent_wait is valid.
39*4882a593Smuzhiyun  * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun struct tps65090_regulator {
43*4882a593Smuzhiyun 	struct device		*dev;
44*4882a593Smuzhiyun 	struct regulator_desc	*desc;
45*4882a593Smuzhiyun 	struct regulator_dev	*rdev;
46*4882a593Smuzhiyun 	bool			overcurrent_wait_valid;
47*4882a593Smuzhiyun 	int			overcurrent_wait;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static const struct regulator_ops tps65090_ext_control_ops = {
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * This will set the overcurrent wait time based on what's in the regulator
57*4882a593Smuzhiyun  * info.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * @ri:		Overall regulator data
60*4882a593Smuzhiyun  * @rdev:	Regulator device
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Return: 0 if no error, non-zero if there was an error writing the register.
63*4882a593Smuzhiyun  */
tps65090_reg_set_overcurrent_wait(struct tps65090_regulator * ri,struct regulator_dev * rdev)64*4882a593Smuzhiyun static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
65*4882a593Smuzhiyun 					     struct regulator_dev *rdev)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	int ret;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
70*4882a593Smuzhiyun 				 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
71*4882a593Smuzhiyun 				 ri->overcurrent_wait << CTRL_WT_BIT);
72*4882a593Smuzhiyun 	if (ret) {
73*4882a593Smuzhiyun 		dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
74*4882a593Smuzhiyun 			rdev->desc->enable_reg);
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return ret;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * tps65090_try_enable_fet - Try to enable a FET
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * @rdev:	Regulator device
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get
86*4882a593Smuzhiyun  * set, or some other -ve value if another error occurred (e.g. i2c error)
87*4882a593Smuzhiyun  */
tps65090_try_enable_fet(struct regulator_dev * rdev)88*4882a593Smuzhiyun static int tps65090_try_enable_fet(struct regulator_dev *rdev)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	unsigned int control;
91*4882a593Smuzhiyun 	int ret, i;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
94*4882a593Smuzhiyun 				 rdev->desc->enable_mask,
95*4882a593Smuzhiyun 				 rdev->desc->enable_mask);
96*4882a593Smuzhiyun 	if (ret < 0) {
97*4882a593Smuzhiyun 		dev_err(&rdev->dev, "Error in updating reg %#x\n",
98*4882a593Smuzhiyun 			rdev->desc->enable_reg);
99*4882a593Smuzhiyun 		return ret;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	for (i = 0; i < MAX_CTRL_READ_TRIES; i++) {
103*4882a593Smuzhiyun 		ret = regmap_read(rdev->regmap, rdev->desc->enable_reg,
104*4882a593Smuzhiyun 				  &control);
105*4882a593Smuzhiyun 		if (ret < 0)
106*4882a593Smuzhiyun 			return ret;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 		if (!(control & BIT(CTRL_TO_BIT)))
109*4882a593Smuzhiyun 			break;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 		usleep_range(1000, 1500);
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 	if (!(control & BIT(CTRL_PG_BIT)))
114*4882a593Smuzhiyun 		return -ENOTRECOVERABLE;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * tps65090_fet_enable - Enable a FET, trying a few times if it fails
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * Some versions of the tps65090 have issues when turning on the FETs.
123*4882a593Smuzhiyun  * This function goes through several steps to ensure the best chance of the
124*4882a593Smuzhiyun  * FET going on.  Specifically:
125*4882a593Smuzhiyun  * - We'll make sure that we bump the "overcurrent wait" to the maximum, which
126*4882a593Smuzhiyun  *   increases the chances that we'll turn on properly.
127*4882a593Smuzhiyun  * - We'll retry turning the FET on multiple times (turning off in between).
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * @rdev:	Regulator device
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * Return: 0 if ok, non-zero if it fails.
132*4882a593Smuzhiyun  */
tps65090_fet_enable(struct regulator_dev * rdev)133*4882a593Smuzhiyun static int tps65090_fet_enable(struct regulator_dev *rdev)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	int ret, tries;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/*
138*4882a593Smuzhiyun 	 * Try enabling multiple times until we succeed since sometimes the
139*4882a593Smuzhiyun 	 * first try times out.
140*4882a593Smuzhiyun 	 */
141*4882a593Smuzhiyun 	tries = 0;
142*4882a593Smuzhiyun 	while (true) {
143*4882a593Smuzhiyun 		ret = tps65090_try_enable_fet(rdev);
144*4882a593Smuzhiyun 		if (!ret)
145*4882a593Smuzhiyun 			break;
146*4882a593Smuzhiyun 		if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES)
147*4882a593Smuzhiyun 			goto err;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		/* Try turning the FET off (and then on again) */
150*4882a593Smuzhiyun 		ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
151*4882a593Smuzhiyun 					 rdev->desc->enable_mask, 0);
152*4882a593Smuzhiyun 		if (ret)
153*4882a593Smuzhiyun 			goto err;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		tries++;
156*4882a593Smuzhiyun 	}
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (tries)
159*4882a593Smuzhiyun 		dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n",
160*4882a593Smuzhiyun 			 rdev->desc->enable_reg, tries);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	return 0;
163*4882a593Smuzhiyun err:
164*4882a593Smuzhiyun 	dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
165*4882a593Smuzhiyun 	WARN_ON(1);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	return ret;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun static const struct regulator_ops tps65090_reg_control_ops = {
171*4882a593Smuzhiyun 	.enable		= regulator_enable_regmap,
172*4882a593Smuzhiyun 	.disable	= regulator_disable_regmap,
173*4882a593Smuzhiyun 	.is_enabled	= regulator_is_enabled_regmap,
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun static const struct regulator_ops tps65090_fet_control_ops = {
177*4882a593Smuzhiyun 	.enable		= tps65090_fet_enable,
178*4882a593Smuzhiyun 	.disable	= regulator_disable_regmap,
179*4882a593Smuzhiyun 	.is_enabled	= regulator_is_enabled_regmap,
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun static const struct regulator_ops tps65090_ldo_ops = {
183*4882a593Smuzhiyun };
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
186*4882a593Smuzhiyun {							\
187*4882a593Smuzhiyun 	.name = "TPS65090_RAILS"#_id,			\
188*4882a593Smuzhiyun 	.supply_name = _sname,				\
189*4882a593Smuzhiyun 	.id = TPS65090_REGULATOR_##_id,			\
190*4882a593Smuzhiyun 	.n_voltages = _nvolt,				\
191*4882a593Smuzhiyun 	.ops = &_ops,					\
192*4882a593Smuzhiyun 	.fixed_uV = _volt,				\
193*4882a593Smuzhiyun 	.enable_reg = _en_reg,				\
194*4882a593Smuzhiyun 	.enable_val = _en_bits,				\
195*4882a593Smuzhiyun 	.enable_mask = _en_bits,			\
196*4882a593Smuzhiyun 	.type = REGULATOR_VOLTAGE,			\
197*4882a593Smuzhiyun 	.owner = THIS_MODULE,				\
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
201*4882a593Smuzhiyun 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
204*4882a593Smuzhiyun 	tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun static struct regulator_desc tps65090_regulator_desc[] = {
207*4882a593Smuzhiyun 	tps65090_REG_FIXEDV(DCDC1, "vsys1",   0x0C, BIT(CTRL_EN_BIT), 5000000,
208*4882a593Smuzhiyun 			    tps65090_reg_control_ops),
209*4882a593Smuzhiyun 	tps65090_REG_FIXEDV(DCDC2, "vsys2",   0x0D, BIT(CTRL_EN_BIT), 3300000,
210*4882a593Smuzhiyun 			    tps65090_reg_control_ops),
211*4882a593Smuzhiyun 	tps65090_REG_SWITCH(DCDC3, "vsys3",   0x0E, BIT(CTRL_EN_BIT),
212*4882a593Smuzhiyun 			    tps65090_reg_control_ops),
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET1,  "infet1",  0x0F,
215*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
216*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
217*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET2,  "infet2",  0x10,
218*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
219*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
220*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET3,  "infet3",  0x11,
221*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
222*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
223*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET4,  "infet4",  0x12,
224*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
225*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
226*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET5,  "infet5",  0x13,
227*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
228*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
229*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET6,  "infet6",  0x14,
230*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
231*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
232*4882a593Smuzhiyun 	tps65090_REG_SWITCH(FET7,  "infet7",  0x15,
233*4882a593Smuzhiyun 			    BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
234*4882a593Smuzhiyun 			    tps65090_fet_control_ops),
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	tps65090_REG_FIXEDV(LDO1,  "vsys-l1", 0, 0, 5000000,
237*4882a593Smuzhiyun 			    tps65090_ldo_ops),
238*4882a593Smuzhiyun 	tps65090_REG_FIXEDV(LDO2,  "vsys-l2", 0, 0, 3300000,
239*4882a593Smuzhiyun 			    tps65090_ldo_ops),
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
is_dcdc(int id)242*4882a593Smuzhiyun static inline bool is_dcdc(int id)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	switch (id) {
245*4882a593Smuzhiyun 	case TPS65090_REGULATOR_DCDC1:
246*4882a593Smuzhiyun 	case TPS65090_REGULATOR_DCDC2:
247*4882a593Smuzhiyun 	case TPS65090_REGULATOR_DCDC3:
248*4882a593Smuzhiyun 		return true;
249*4882a593Smuzhiyun 	default:
250*4882a593Smuzhiyun 		return false;
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
tps65090_config_ext_control(struct tps65090_regulator * ri,bool enable)254*4882a593Smuzhiyun static int tps65090_config_ext_control(
255*4882a593Smuzhiyun 	struct tps65090_regulator *ri, bool enable)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	int ret;
258*4882a593Smuzhiyun 	struct device *parent = ri->dev->parent;
259*4882a593Smuzhiyun 	unsigned int reg_en_reg = ri->desc->enable_reg;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	if (enable)
262*4882a593Smuzhiyun 		ret = tps65090_set_bits(parent, reg_en_reg, 1);
263*4882a593Smuzhiyun 	else
264*4882a593Smuzhiyun 		ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
265*4882a593Smuzhiyun 	if (ret < 0)
266*4882a593Smuzhiyun 		dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
267*4882a593Smuzhiyun 	return ret;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
tps65090_regulator_disable_ext_control(struct tps65090_regulator * ri,struct tps65090_regulator_plat_data * tps_pdata)270*4882a593Smuzhiyun static int tps65090_regulator_disable_ext_control(
271*4882a593Smuzhiyun 		struct tps65090_regulator *ri,
272*4882a593Smuzhiyun 		struct tps65090_regulator_plat_data *tps_pdata)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	int ret = 0;
275*4882a593Smuzhiyun 	struct device *parent = ri->dev->parent;
276*4882a593Smuzhiyun 	unsigned int reg_en_reg = ri->desc->enable_reg;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/*
279*4882a593Smuzhiyun 	 * First enable output for internal control if require.
280*4882a593Smuzhiyun 	 * And then disable external control.
281*4882a593Smuzhiyun 	 */
282*4882a593Smuzhiyun 	if (tps_pdata->reg_init_data->constraints.always_on ||
283*4882a593Smuzhiyun 			tps_pdata->reg_init_data->constraints.boot_on) {
284*4882a593Smuzhiyun 		ret =  tps65090_set_bits(parent, reg_en_reg, 0);
285*4882a593Smuzhiyun 		if (ret < 0) {
286*4882a593Smuzhiyun 			dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
287*4882a593Smuzhiyun 			return ret;
288*4882a593Smuzhiyun 		}
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 	return tps65090_config_ext_control(ri, false);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun #ifdef CONFIG_OF
294*4882a593Smuzhiyun static struct of_regulator_match tps65090_matches[] = {
295*4882a593Smuzhiyun 	{ .name = "dcdc1", },
296*4882a593Smuzhiyun 	{ .name = "dcdc2", },
297*4882a593Smuzhiyun 	{ .name = "dcdc3", },
298*4882a593Smuzhiyun 	{ .name = "fet1",  },
299*4882a593Smuzhiyun 	{ .name = "fet2",  },
300*4882a593Smuzhiyun 	{ .name = "fet3",  },
301*4882a593Smuzhiyun 	{ .name = "fet4",  },
302*4882a593Smuzhiyun 	{ .name = "fet5",  },
303*4882a593Smuzhiyun 	{ .name = "fet6",  },
304*4882a593Smuzhiyun 	{ .name = "fet7",  },
305*4882a593Smuzhiyun 	{ .name = "ldo1",  },
306*4882a593Smuzhiyun 	{ .name = "ldo2",  },
307*4882a593Smuzhiyun };
308*4882a593Smuzhiyun 
tps65090_parse_dt_reg_data(struct platform_device * pdev,struct of_regulator_match ** tps65090_reg_matches)309*4882a593Smuzhiyun static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
310*4882a593Smuzhiyun 		struct platform_device *pdev,
311*4882a593Smuzhiyun 		struct of_regulator_match **tps65090_reg_matches)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct tps65090_platform_data *tps65090_pdata;
314*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.parent->of_node;
315*4882a593Smuzhiyun 	struct device_node *regulators;
316*4882a593Smuzhiyun 	int idx = 0, ret;
317*4882a593Smuzhiyun 	struct tps65090_regulator_plat_data *reg_pdata;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
320*4882a593Smuzhiyun 				GFP_KERNEL);
321*4882a593Smuzhiyun 	if (!tps65090_pdata)
322*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	reg_pdata = devm_kcalloc(&pdev->dev,
325*4882a593Smuzhiyun 				 TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
326*4882a593Smuzhiyun 				 GFP_KERNEL);
327*4882a593Smuzhiyun 	if (!reg_pdata)
328*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	regulators = of_get_child_by_name(np, "regulators");
331*4882a593Smuzhiyun 	if (!regulators) {
332*4882a593Smuzhiyun 		dev_err(&pdev->dev, "regulator node not found\n");
333*4882a593Smuzhiyun 		return ERR_PTR(-ENODEV);
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
337*4882a593Smuzhiyun 			ARRAY_SIZE(tps65090_matches));
338*4882a593Smuzhiyun 	of_node_put(regulators);
339*4882a593Smuzhiyun 	if (ret < 0) {
340*4882a593Smuzhiyun 		dev_err(&pdev->dev,
341*4882a593Smuzhiyun 			"Error parsing regulator init data: %d\n", ret);
342*4882a593Smuzhiyun 		return ERR_PTR(ret);
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	*tps65090_reg_matches = tps65090_matches;
346*4882a593Smuzhiyun 	for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
347*4882a593Smuzhiyun 		struct regulator_init_data *ri_data;
348*4882a593Smuzhiyun 		struct tps65090_regulator_plat_data *rpdata;
349*4882a593Smuzhiyun 		struct device_node *np;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		rpdata = &reg_pdata[idx];
352*4882a593Smuzhiyun 		ri_data = tps65090_matches[idx].init_data;
353*4882a593Smuzhiyun 		if (!ri_data)
354*4882a593Smuzhiyun 			continue;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 		np = tps65090_matches[idx].of_node;
357*4882a593Smuzhiyun 		if (!np)
358*4882a593Smuzhiyun 			continue;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 		rpdata->reg_init_data = ri_data;
361*4882a593Smuzhiyun 		rpdata->enable_ext_control = of_property_read_bool(np,
362*4882a593Smuzhiyun 						"ti,enable-ext-control");
363*4882a593Smuzhiyun 		if (rpdata->enable_ext_control) {
364*4882a593Smuzhiyun 			enum gpiod_flags gflags;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 			if (ri_data->constraints.always_on ||
367*4882a593Smuzhiyun 			    ri_data->constraints.boot_on)
368*4882a593Smuzhiyun 				gflags = GPIOD_OUT_HIGH;
369*4882a593Smuzhiyun 			else
370*4882a593Smuzhiyun 				gflags = GPIOD_OUT_LOW;
371*4882a593Smuzhiyun 			gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 			rpdata->gpiod = devm_fwnode_gpiod_get(
374*4882a593Smuzhiyun 							&pdev->dev,
375*4882a593Smuzhiyun 							of_fwnode_handle(np),
376*4882a593Smuzhiyun 							"dcdc-ext-control",
377*4882a593Smuzhiyun 							gflags,
378*4882a593Smuzhiyun 							"tps65090");
379*4882a593Smuzhiyun 			if (PTR_ERR(rpdata->gpiod) == -ENOENT) {
380*4882a593Smuzhiyun 				dev_err(&pdev->dev,
381*4882a593Smuzhiyun 					"could not find DCDC external control GPIO\n");
382*4882a593Smuzhiyun 				rpdata->gpiod = NULL;
383*4882a593Smuzhiyun 			} else if (IS_ERR(rpdata->gpiod))
384*4882a593Smuzhiyun 				return ERR_CAST(rpdata->gpiod);
385*4882a593Smuzhiyun 		}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		if (of_property_read_u32(np, "ti,overcurrent-wait",
388*4882a593Smuzhiyun 					 &rpdata->overcurrent_wait) == 0)
389*4882a593Smuzhiyun 			rpdata->overcurrent_wait_valid = true;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		tps65090_pdata->reg_pdata[idx] = rpdata;
392*4882a593Smuzhiyun 	}
393*4882a593Smuzhiyun 	return tps65090_pdata;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun #else
tps65090_parse_dt_reg_data(struct platform_device * pdev,struct of_regulator_match ** tps65090_reg_matches)396*4882a593Smuzhiyun static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
397*4882a593Smuzhiyun 			struct platform_device *pdev,
398*4882a593Smuzhiyun 			struct of_regulator_match **tps65090_reg_matches)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun 	*tps65090_reg_matches = NULL;
401*4882a593Smuzhiyun 	return NULL;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun #endif
404*4882a593Smuzhiyun 
tps65090_regulator_probe(struct platform_device * pdev)405*4882a593Smuzhiyun static int tps65090_regulator_probe(struct platform_device *pdev)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
408*4882a593Smuzhiyun 	struct tps65090_regulator *ri = NULL;
409*4882a593Smuzhiyun 	struct regulator_config config = { };
410*4882a593Smuzhiyun 	struct regulator_dev *rdev;
411*4882a593Smuzhiyun 	struct tps65090_regulator_plat_data *tps_pdata;
412*4882a593Smuzhiyun 	struct tps65090_regulator *pmic;
413*4882a593Smuzhiyun 	struct tps65090_platform_data *tps65090_pdata;
414*4882a593Smuzhiyun 	struct of_regulator_match *tps65090_reg_matches = NULL;
415*4882a593Smuzhiyun 	int num;
416*4882a593Smuzhiyun 	int ret;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "Probing regulator\n");
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	tps65090_pdata = dev_get_platdata(pdev->dev.parent);
421*4882a593Smuzhiyun 	if (!tps65090_pdata && tps65090_mfd->dev->of_node)
422*4882a593Smuzhiyun 		tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
423*4882a593Smuzhiyun 					&tps65090_reg_matches);
424*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(tps65090_pdata)) {
425*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Platform data missing\n");
426*4882a593Smuzhiyun 		return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	pmic = devm_kcalloc(&pdev->dev,
430*4882a593Smuzhiyun 			    TPS65090_REGULATOR_MAX, sizeof(*pmic),
431*4882a593Smuzhiyun 			    GFP_KERNEL);
432*4882a593Smuzhiyun 	if (!pmic)
433*4882a593Smuzhiyun 		return -ENOMEM;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
436*4882a593Smuzhiyun 		tps_pdata = tps65090_pdata->reg_pdata[num];
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 		ri = &pmic[num];
439*4882a593Smuzhiyun 		ri->dev = &pdev->dev;
440*4882a593Smuzhiyun 		ri->desc = &tps65090_regulator_desc[num];
441*4882a593Smuzhiyun 		if (tps_pdata) {
442*4882a593Smuzhiyun 			ri->overcurrent_wait_valid =
443*4882a593Smuzhiyun 				tps_pdata->overcurrent_wait_valid;
444*4882a593Smuzhiyun 			ri->overcurrent_wait = tps_pdata->overcurrent_wait;
445*4882a593Smuzhiyun 		}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 		/*
448*4882a593Smuzhiyun 		 * TPS5090 DCDC support the control from external digital input.
449*4882a593Smuzhiyun 		 * Configure it as per platform data.
450*4882a593Smuzhiyun 		 */
451*4882a593Smuzhiyun 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
452*4882a593Smuzhiyun 			if (tps_pdata->enable_ext_control) {
453*4882a593Smuzhiyun 				config.ena_gpiod = tps_pdata->gpiod;
454*4882a593Smuzhiyun 				ri->desc->ops = &tps65090_ext_control_ops;
455*4882a593Smuzhiyun 			} else {
456*4882a593Smuzhiyun 				ret = tps65090_regulator_disable_ext_control(
457*4882a593Smuzhiyun 						ri, tps_pdata);
458*4882a593Smuzhiyun 				if (ret < 0) {
459*4882a593Smuzhiyun 					dev_err(&pdev->dev,
460*4882a593Smuzhiyun 						"failed disable ext control\n");
461*4882a593Smuzhiyun 					return ret;
462*4882a593Smuzhiyun 				}
463*4882a593Smuzhiyun 			}
464*4882a593Smuzhiyun 		}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		config.dev = pdev->dev.parent;
467*4882a593Smuzhiyun 		config.driver_data = ri;
468*4882a593Smuzhiyun 		config.regmap = tps65090_mfd->rmap;
469*4882a593Smuzhiyun 		if (tps_pdata)
470*4882a593Smuzhiyun 			config.init_data = tps_pdata->reg_init_data;
471*4882a593Smuzhiyun 		else
472*4882a593Smuzhiyun 			config.init_data = NULL;
473*4882a593Smuzhiyun 		if (tps65090_reg_matches)
474*4882a593Smuzhiyun 			config.of_node = tps65090_reg_matches[num].of_node;
475*4882a593Smuzhiyun 		else
476*4882a593Smuzhiyun 			config.of_node = NULL;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 		/*
479*4882a593Smuzhiyun 		 * Hand the GPIO descriptor management over to the regulator
480*4882a593Smuzhiyun 		 * core, remove it from devres management.
481*4882a593Smuzhiyun 		 */
482*4882a593Smuzhiyun 		if (config.ena_gpiod)
483*4882a593Smuzhiyun 			devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
484*4882a593Smuzhiyun 		rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
485*4882a593Smuzhiyun 		if (IS_ERR(rdev)) {
486*4882a593Smuzhiyun 			dev_err(&pdev->dev, "failed to register regulator %s\n",
487*4882a593Smuzhiyun 				ri->desc->name);
488*4882a593Smuzhiyun 			return PTR_ERR(rdev);
489*4882a593Smuzhiyun 		}
490*4882a593Smuzhiyun 		ri->rdev = rdev;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		if (ri->overcurrent_wait_valid) {
493*4882a593Smuzhiyun 			ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
494*4882a593Smuzhiyun 			if (ret < 0)
495*4882a593Smuzhiyun 				return ret;
496*4882a593Smuzhiyun 		}
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		/* Enable external control if it is require */
499*4882a593Smuzhiyun 		if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
500*4882a593Smuzhiyun 				tps_pdata->enable_ext_control) {
501*4882a593Smuzhiyun 			ret = tps65090_config_ext_control(ri, true);
502*4882a593Smuzhiyun 			if (ret < 0)
503*4882a593Smuzhiyun 				return ret;
504*4882a593Smuzhiyun 		}
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	platform_set_drvdata(pdev, pmic);
508*4882a593Smuzhiyun 	return 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun static struct platform_driver tps65090_regulator_driver = {
512*4882a593Smuzhiyun 	.driver	= {
513*4882a593Smuzhiyun 		.name	= "tps65090-pmic",
514*4882a593Smuzhiyun 	},
515*4882a593Smuzhiyun 	.probe		= tps65090_regulator_probe,
516*4882a593Smuzhiyun };
517*4882a593Smuzhiyun 
tps65090_regulator_init(void)518*4882a593Smuzhiyun static int __init tps65090_regulator_init(void)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	return platform_driver_register(&tps65090_regulator_driver);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun subsys_initcall(tps65090_regulator_init);
523*4882a593Smuzhiyun 
tps65090_regulator_exit(void)524*4882a593Smuzhiyun static void __exit tps65090_regulator_exit(void)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	platform_driver_unregister(&tps65090_regulator_driver);
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun module_exit(tps65090_regulator_exit);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun MODULE_DESCRIPTION("tps65090 regulator driver");
531*4882a593Smuzhiyun MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
532*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
533*4882a593Smuzhiyun MODULE_ALIAS("platform:tps65090-pmic");
534