2 * Regulator driver for tps65090 power management chip.
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/gpio.h>
23 #include <linux/of_gpio.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/mfd/tps65090.h>
32 #define MAX_CTRL_READ_TRIES 5
33 #define MAX_FET_ENABLE_TRIES 1000
35 #define CTRL_EN_BIT 0 /* Regulator enable bit, active high */
36 #define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */
37 #define CTRL_PG_BIT 4 /* Regulator power good bit, 1=good */
38 #define CTRL_TO_BIT 7 /* Regulator timeout bit, 1=wait */
40 #define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */
43 * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
45 * @dev: Pointer to our device.
46 * @desc: The struct regulator_desc for the regulator.
47 * @rdev: The struct regulator_dev for the regulator.
48 * @overcurrent_wait_valid: True if overcurrent_wait is valid.
49 * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
52 struct tps65090_regulator
{
54 struct regulator_desc
*desc
;
55 struct regulator_dev
*rdev
;
56 bool overcurrent_wait_valid
;
60 static struct regulator_ops tps65090_ext_control_ops
= {
64 * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
66 * This will set the overcurrent wait time based on what's in the regulator
69 * @ri: Overall regulator data
70 * @rdev: Regulator device
72 * Return: 0 if no error, non-zero if there was an error writing the register.
74 static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator
*ri
,
75 struct regulator_dev
*rdev
)
79 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
80 MAX_OVERCURRENT_WAIT
<< CTRL_WT_BIT
,
81 ri
->overcurrent_wait
<< CTRL_WT_BIT
);
83 dev_err(&rdev
->dev
, "Error updating overcurrent wait %#x\n",
84 rdev
->desc
->enable_reg
);
91 * tps65090_try_enable_fet - Try to enable a FET
93 * @rdev: Regulator device
95 * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get
96 * set, or some other -ve value if another error occurred (e.g. i2c error)
98 static int tps65090_try_enable_fet(struct regulator_dev
*rdev
)
100 unsigned int control
;
103 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
104 rdev
->desc
->enable_mask
,
105 rdev
->desc
->enable_mask
);
107 dev_err(&rdev
->dev
, "Error in updating reg %#x\n",
108 rdev
->desc
->enable_reg
);
112 for (i
= 0; i
< MAX_CTRL_READ_TRIES
; i
++) {
113 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->enable_reg
,
118 if (!(control
& BIT(CTRL_TO_BIT
)))
121 usleep_range(1000, 1500);
123 if (!(control
& BIT(CTRL_PG_BIT
)))
124 return -ENOTRECOVERABLE
;
130 * tps65090_fet_enable - Enable a FET, trying a few times if it fails
132 * Some versions of the tps65090 have issues when turning on the FETs.
133 * This function goes through several steps to ensure the best chance of the
134 * FET going on. Specifically:
135 * - We'll make sure that we bump the "overcurrent wait" to the maximum, which
136 * increases the chances that we'll turn on properly.
137 * - We'll retry turning the FET on multiple times (turning off in between).
139 * @rdev: Regulator device
141 * Return: 0 if ok, non-zero if it fails.
143 static int tps65090_fet_enable(struct regulator_dev
*rdev
)
148 * Try enabling multiple times until we succeed since sometimes the
149 * first try times out.
153 ret
= tps65090_try_enable_fet(rdev
);
156 if (ret
!= -ENOTRECOVERABLE
|| tries
== MAX_FET_ENABLE_TRIES
)
159 /* Try turning the FET off (and then on again) */
160 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
161 rdev
->desc
->enable_mask
, 0);
169 dev_warn(&rdev
->dev
, "reg %#x enable ok after %d tries\n",
170 rdev
->desc
->enable_reg
, tries
);
174 dev_warn(&rdev
->dev
, "reg %#x enable failed\n", rdev
->desc
->enable_reg
);
180 static struct regulator_ops tps65090_reg_control_ops
= {
181 .enable
= regulator_enable_regmap
,
182 .disable
= regulator_disable_regmap
,
183 .is_enabled
= regulator_is_enabled_regmap
,
186 static struct regulator_ops tps65090_fet_control_ops
= {
187 .enable
= tps65090_fet_enable
,
188 .disable
= regulator_disable_regmap
,
189 .is_enabled
= regulator_is_enabled_regmap
,
192 static struct regulator_ops tps65090_ldo_ops
= {
195 #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
197 .name = "TPS65090_RAILS"#_id, \
198 .supply_name = _sname, \
199 .id = TPS65090_REGULATOR_##_id, \
200 .n_voltages = _nvolt, \
203 .enable_reg = _en_reg, \
204 .enable_val = _en_bits, \
205 .enable_mask = _en_bits, \
206 .type = REGULATOR_VOLTAGE, \
207 .owner = THIS_MODULE, \
210 #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
211 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
213 #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
214 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
216 static struct regulator_desc tps65090_regulator_desc
[] = {
217 tps65090_REG_FIXEDV(DCDC1
, "vsys1", 0x0C, BIT(CTRL_EN_BIT
), 5000000,
218 tps65090_reg_control_ops
),
219 tps65090_REG_FIXEDV(DCDC2
, "vsys2", 0x0D, BIT(CTRL_EN_BIT
), 3300000,
220 tps65090_reg_control_ops
),
221 tps65090_REG_SWITCH(DCDC3
, "vsys3", 0x0E, BIT(CTRL_EN_BIT
),
222 tps65090_reg_control_ops
),
224 tps65090_REG_SWITCH(FET1
, "infet1", 0x0F,
225 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
226 tps65090_fet_control_ops
),
227 tps65090_REG_SWITCH(FET2
, "infet2", 0x10,
228 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
229 tps65090_fet_control_ops
),
230 tps65090_REG_SWITCH(FET3
, "infet3", 0x11,
231 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
232 tps65090_fet_control_ops
),
233 tps65090_REG_SWITCH(FET4
, "infet4", 0x12,
234 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
235 tps65090_fet_control_ops
),
236 tps65090_REG_SWITCH(FET5
, "infet5", 0x13,
237 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
238 tps65090_fet_control_ops
),
239 tps65090_REG_SWITCH(FET6
, "infet6", 0x14,
240 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
241 tps65090_fet_control_ops
),
242 tps65090_REG_SWITCH(FET7
, "infet7", 0x15,
243 BIT(CTRL_EN_BIT
) | BIT(CTRL_PG_BIT
),
244 tps65090_fet_control_ops
),
246 tps65090_REG_FIXEDV(LDO1
, "vsys-l1", 0, 0, 5000000,
248 tps65090_REG_FIXEDV(LDO2
, "vsys-l2", 0, 0, 3300000,
252 static inline bool is_dcdc(int id
)
255 case TPS65090_REGULATOR_DCDC1
:
256 case TPS65090_REGULATOR_DCDC2
:
257 case TPS65090_REGULATOR_DCDC3
:
264 static int tps65090_config_ext_control(
265 struct tps65090_regulator
*ri
, bool enable
)
268 struct device
*parent
= ri
->dev
->parent
;
269 unsigned int reg_en_reg
= ri
->desc
->enable_reg
;
272 ret
= tps65090_set_bits(parent
, reg_en_reg
, 1);
274 ret
= tps65090_clr_bits(parent
, reg_en_reg
, 1);
276 dev_err(ri
->dev
, "Error in updating reg 0x%x\n", reg_en_reg
);
280 static int tps65090_regulator_disable_ext_control(
281 struct tps65090_regulator
*ri
,
282 struct tps65090_regulator_plat_data
*tps_pdata
)
285 struct device
*parent
= ri
->dev
->parent
;
286 unsigned int reg_en_reg
= ri
->desc
->enable_reg
;
289 * First enable output for internal control if require.
290 * And then disable external control.
292 if (tps_pdata
->reg_init_data
->constraints
.always_on
||
293 tps_pdata
->reg_init_data
->constraints
.boot_on
) {
294 ret
= tps65090_set_bits(parent
, reg_en_reg
, 0);
296 dev_err(ri
->dev
, "Error in set reg 0x%x\n", reg_en_reg
);
300 return tps65090_config_ext_control(ri
, false);
303 static void tps65090_configure_regulator_config(
304 struct tps65090_regulator_plat_data
*tps_pdata
,
305 struct regulator_config
*config
)
307 if (gpio_is_valid(tps_pdata
->gpio
)) {
308 int gpio_flag
= GPIOF_OUT_INIT_LOW
;
310 if (tps_pdata
->reg_init_data
->constraints
.always_on
||
311 tps_pdata
->reg_init_data
->constraints
.boot_on
)
312 gpio_flag
= GPIOF_OUT_INIT_HIGH
;
314 config
->ena_gpio
= tps_pdata
->gpio
;
315 config
->ena_gpio_flags
= gpio_flag
;
320 static struct of_regulator_match tps65090_matches
[] = {
321 { .name
= "dcdc1", },
322 { .name
= "dcdc2", },
323 { .name
= "dcdc3", },
335 static struct tps65090_platform_data
*tps65090_parse_dt_reg_data(
336 struct platform_device
*pdev
,
337 struct of_regulator_match
**tps65090_reg_matches
)
339 struct tps65090_platform_data
*tps65090_pdata
;
340 struct device_node
*np
= pdev
->dev
.parent
->of_node
;
341 struct device_node
*regulators
;
343 struct tps65090_regulator_plat_data
*reg_pdata
;
345 tps65090_pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*tps65090_pdata
),
348 return ERR_PTR(-ENOMEM
);
350 reg_pdata
= devm_kzalloc(&pdev
->dev
, TPS65090_REGULATOR_MAX
*
351 sizeof(*reg_pdata
), GFP_KERNEL
);
353 return ERR_PTR(-ENOMEM
);
355 regulators
= of_get_child_by_name(np
, "regulators");
357 dev_err(&pdev
->dev
, "regulator node not found\n");
358 return ERR_PTR(-ENODEV
);
361 ret
= of_regulator_match(&pdev
->dev
, regulators
, tps65090_matches
,
362 ARRAY_SIZE(tps65090_matches
));
363 of_node_put(regulators
);
366 "Error parsing regulator init data: %d\n", ret
);
370 *tps65090_reg_matches
= tps65090_matches
;
371 for (idx
= 0; idx
< ARRAY_SIZE(tps65090_matches
); idx
++) {
372 struct regulator_init_data
*ri_data
;
373 struct tps65090_regulator_plat_data
*rpdata
;
375 rpdata
= ®_pdata
[idx
];
376 ri_data
= tps65090_matches
[idx
].init_data
;
377 if (!ri_data
|| !tps65090_matches
[idx
].of_node
)
380 rpdata
->reg_init_data
= ri_data
;
381 rpdata
->enable_ext_control
= of_property_read_bool(
382 tps65090_matches
[idx
].of_node
,
383 "ti,enable-ext-control");
384 if (rpdata
->enable_ext_control
)
385 rpdata
->gpio
= of_get_named_gpio(np
,
386 "dcdc-ext-control-gpios", 0);
388 if (of_property_read_u32(tps65090_matches
[idx
].of_node
,
389 "ti,overcurrent-wait",
390 &rpdata
->overcurrent_wait
) == 0)
391 rpdata
->overcurrent_wait_valid
= true;
393 tps65090_pdata
->reg_pdata
[idx
] = rpdata
;
395 return tps65090_pdata
;
398 static inline struct tps65090_platform_data
*tps65090_parse_dt_reg_data(
399 struct platform_device
*pdev
,
400 struct of_regulator_match
**tps65090_reg_matches
)
402 *tps65090_reg_matches
= NULL
;
407 static int tps65090_regulator_probe(struct platform_device
*pdev
)
409 struct tps65090
*tps65090_mfd
= dev_get_drvdata(pdev
->dev
.parent
);
410 struct tps65090_regulator
*ri
= NULL
;
411 struct regulator_config config
= { };
412 struct regulator_dev
*rdev
;
413 struct tps65090_regulator_plat_data
*tps_pdata
;
414 struct tps65090_regulator
*pmic
;
415 struct tps65090_platform_data
*tps65090_pdata
;
416 struct of_regulator_match
*tps65090_reg_matches
= NULL
;
420 dev_dbg(&pdev
->dev
, "Probing regulator\n");
422 tps65090_pdata
= dev_get_platdata(pdev
->dev
.parent
);
423 if (!tps65090_pdata
&& tps65090_mfd
->dev
->of_node
)
424 tps65090_pdata
= tps65090_parse_dt_reg_data(pdev
,
425 &tps65090_reg_matches
);
426 if (IS_ERR_OR_NULL(tps65090_pdata
)) {
427 dev_err(&pdev
->dev
, "Platform data missing\n");
428 return tps65090_pdata
? PTR_ERR(tps65090_pdata
) : -EINVAL
;
431 pmic
= devm_kzalloc(&pdev
->dev
, TPS65090_REGULATOR_MAX
* sizeof(*pmic
),
436 for (num
= 0; num
< TPS65090_REGULATOR_MAX
; num
++) {
437 tps_pdata
= tps65090_pdata
->reg_pdata
[num
];
440 ri
->dev
= &pdev
->dev
;
441 ri
->desc
= &tps65090_regulator_desc
[num
];
443 ri
->overcurrent_wait_valid
=
444 tps_pdata
->overcurrent_wait_valid
;
445 ri
->overcurrent_wait
= tps_pdata
->overcurrent_wait
;
449 * TPS5090 DCDC support the control from external digital input.
450 * Configure it as per platform data.
452 if (tps_pdata
&& is_dcdc(num
) && tps_pdata
->reg_init_data
) {
453 if (tps_pdata
->enable_ext_control
) {
454 tps65090_configure_regulator_config(
456 ri
->desc
->ops
= &tps65090_ext_control_ops
;
458 ret
= tps65090_regulator_disable_ext_control(
462 "failed disable ext control\n");
468 config
.dev
= pdev
->dev
.parent
;
469 config
.driver_data
= ri
;
470 config
.regmap
= tps65090_mfd
->rmap
;
472 config
.init_data
= tps_pdata
->reg_init_data
;
474 config
.init_data
= NULL
;
475 if (tps65090_reg_matches
)
476 config
.of_node
= tps65090_reg_matches
[num
].of_node
;
478 config
.of_node
= NULL
;
480 rdev
= devm_regulator_register(&pdev
->dev
, ri
->desc
, &config
);
482 dev_err(&pdev
->dev
, "failed to register regulator %s\n",
484 return PTR_ERR(rdev
);
488 if (ri
->overcurrent_wait_valid
) {
489 ret
= tps65090_reg_set_overcurrent_wait(ri
, rdev
);
494 /* Enable external control if it is require */
495 if (tps_pdata
&& is_dcdc(num
) && tps_pdata
->reg_init_data
&&
496 tps_pdata
->enable_ext_control
) {
497 ret
= tps65090_config_ext_control(ri
, true);
503 platform_set_drvdata(pdev
, pmic
);
507 static struct platform_driver tps65090_regulator_driver
= {
509 .name
= "tps65090-pmic",
510 .owner
= THIS_MODULE
,
512 .probe
= tps65090_regulator_probe
,
515 static int __init
tps65090_regulator_init(void)
517 return platform_driver_register(&tps65090_regulator_driver
);
519 subsys_initcall(tps65090_regulator_init
);
521 static void __exit
tps65090_regulator_exit(void)
523 platform_driver_unregister(&tps65090_regulator_driver
);
525 module_exit(tps65090_regulator_exit
);
527 MODULE_DESCRIPTION("tps65090 regulator driver");
528 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
529 MODULE_LICENSE("GPL v2");
530 MODULE_ALIAS("platform:tps65090-pmic");