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/init.h>
21 #include <linux/gpio.h>
22 #include <linux/of_gpio.h>
23 #include <linux/slab.h>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/regulator/of_regulator.h>
29 #include <linux/mfd/tps65090.h>
31 #define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */
33 #define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */
36 * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
38 * @dev: Pointer to our device.
39 * @desc: The struct regulator_desc for the regulator.
40 * @rdev: The struct regulator_dev for the regulator.
41 * @overcurrent_wait_valid: True if overcurrent_wait is valid.
42 * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
45 struct tps65090_regulator {
47 struct regulator_desc *desc;
48 struct regulator_dev *rdev;
49 bool overcurrent_wait_valid;
53 static struct regulator_ops tps65090_ext_control_ops = {
57 * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
59 * This will set the overcurrent wait time based on what's in the regulator
62 * @ri: Overall regulator data
63 * @rdev: Regulator device
65 * Return: 0 if no error, non-zero if there was an error writing the register.
67 static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
68 struct regulator_dev *rdev)
72 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
73 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
74 ri->overcurrent_wait << CTRL_WT_BIT);
76 dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
77 rdev->desc->enable_reg);
83 static struct regulator_ops tps65090_reg_contol_ops = {
84 .enable = regulator_enable_regmap,
85 .disable = regulator_disable_regmap,
86 .is_enabled = regulator_is_enabled_regmap,
89 static struct regulator_ops tps65090_ldo_ops = {
92 #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
94 .name = "TPS65090_RAILS"#_id, \
95 .supply_name = _sname, \
96 .id = TPS65090_REGULATOR_##_id, \
98 .enable_reg = _en_reg, \
99 .enable_mask = BIT(0), \
100 .type = REGULATOR_VOLTAGE, \
101 .owner = THIS_MODULE, \
104 static struct regulator_desc tps65090_regulator_desc[] = {
105 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops),
106 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops),
107 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops),
108 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops),
109 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops),
110 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops),
111 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops),
112 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops),
113 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops),
114 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops),
115 tps65090_REG_DESC(LDO1, "vsys-l1", 0, tps65090_ldo_ops),
116 tps65090_REG_DESC(LDO2, "vsys-l2", 0, tps65090_ldo_ops),
119 static inline bool is_dcdc(int id)
122 case TPS65090_REGULATOR_DCDC1:
123 case TPS65090_REGULATOR_DCDC2:
124 case TPS65090_REGULATOR_DCDC3:
131 static int tps65090_config_ext_control(
132 struct tps65090_regulator *ri, bool enable)
135 struct device *parent = ri->dev->parent;
136 unsigned int reg_en_reg = ri->desc->enable_reg;
139 ret = tps65090_set_bits(parent, reg_en_reg, 1);
141 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
143 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
147 static int tps65090_regulator_disable_ext_control(
148 struct tps65090_regulator *ri,
149 struct tps65090_regulator_plat_data *tps_pdata)
152 struct device *parent = ri->dev->parent;
153 unsigned int reg_en_reg = ri->desc->enable_reg;
156 * First enable output for internal control if require.
157 * And then disable external control.
159 if (tps_pdata->reg_init_data->constraints.always_on ||
160 tps_pdata->reg_init_data->constraints.boot_on) {
161 ret = tps65090_set_bits(parent, reg_en_reg, 0);
163 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
167 return tps65090_config_ext_control(ri, false);
170 static void tps65090_configure_regulator_config(
171 struct tps65090_regulator_plat_data *tps_pdata,
172 struct regulator_config *config)
174 if (gpio_is_valid(tps_pdata->gpio)) {
175 int gpio_flag = GPIOF_OUT_INIT_LOW;
177 if (tps_pdata->reg_init_data->constraints.always_on ||
178 tps_pdata->reg_init_data->constraints.boot_on)
179 gpio_flag = GPIOF_OUT_INIT_HIGH;
181 config->ena_gpio = tps_pdata->gpio;
182 config->ena_gpio_flags = gpio_flag;
187 static struct of_regulator_match tps65090_matches[] = {
188 { .name = "dcdc1", },
189 { .name = "dcdc2", },
190 { .name = "dcdc3", },
202 static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
203 struct platform_device *pdev,
204 struct of_regulator_match **tps65090_reg_matches)
206 struct tps65090_platform_data *tps65090_pdata;
207 struct device_node *np = pdev->dev.parent->of_node;
208 struct device_node *regulators;
210 struct tps65090_regulator_plat_data *reg_pdata;
212 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
215 return ERR_PTR(-ENOMEM);
217 reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
218 sizeof(*reg_pdata), GFP_KERNEL);
220 return ERR_PTR(-ENOMEM);
222 regulators = of_get_child_by_name(np, "regulators");
224 dev_err(&pdev->dev, "regulator node not found\n");
225 return ERR_PTR(-ENODEV);
228 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
229 ARRAY_SIZE(tps65090_matches));
230 of_node_put(regulators);
233 "Error parsing regulator init data: %d\n", ret);
237 *tps65090_reg_matches = tps65090_matches;
238 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
239 struct regulator_init_data *ri_data;
240 struct tps65090_regulator_plat_data *rpdata;
242 rpdata = ®_pdata[idx];
243 ri_data = tps65090_matches[idx].init_data;
244 if (!ri_data || !tps65090_matches[idx].of_node)
247 rpdata->reg_init_data = ri_data;
248 rpdata->enable_ext_control = of_property_read_bool(
249 tps65090_matches[idx].of_node,
250 "ti,enable-ext-control");
251 if (rpdata->enable_ext_control)
252 rpdata->gpio = of_get_named_gpio(np,
253 "dcdc-ext-control-gpios", 0);
255 if (of_property_read_u32(tps65090_matches[idx].of_node,
256 "ti,overcurrent-wait",
257 &rpdata->overcurrent_wait) == 0)
258 rpdata->overcurrent_wait_valid = true;
260 tps65090_pdata->reg_pdata[idx] = rpdata;
262 return tps65090_pdata;
265 static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
266 struct platform_device *pdev,
267 struct of_regulator_match **tps65090_reg_matches)
269 *tps65090_reg_matches = NULL;
274 static int tps65090_regulator_probe(struct platform_device *pdev)
276 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
277 struct tps65090_regulator *ri = NULL;
278 struct regulator_config config = { };
279 struct regulator_dev *rdev;
280 struct tps65090_regulator_plat_data *tps_pdata;
281 struct tps65090_regulator *pmic;
282 struct tps65090_platform_data *tps65090_pdata;
283 struct of_regulator_match *tps65090_reg_matches = NULL;
287 dev_dbg(&pdev->dev, "Probing regulator\n");
289 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
290 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
291 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
292 &tps65090_reg_matches);
293 if (IS_ERR_OR_NULL(tps65090_pdata)) {
294 dev_err(&pdev->dev, "Platform data missing\n");
295 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
298 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
303 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
304 tps_pdata = tps65090_pdata->reg_pdata[num];
307 ri->dev = &pdev->dev;
308 ri->desc = &tps65090_regulator_desc[num];
309 ri->overcurrent_wait_valid = tps_pdata->overcurrent_wait_valid;
310 ri->overcurrent_wait = tps_pdata->overcurrent_wait;
313 * TPS5090 DCDC support the control from external digital input.
314 * Configure it as per platform data.
316 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
317 if (tps_pdata->enable_ext_control) {
318 tps65090_configure_regulator_config(
320 ri->desc->ops = &tps65090_ext_control_ops;
322 ret = tps65090_regulator_disable_ext_control(
326 "failed disable ext control\n");
332 config.dev = pdev->dev.parent;
333 config.driver_data = ri;
334 config.regmap = tps65090_mfd->rmap;
336 config.init_data = tps_pdata->reg_init_data;
338 config.init_data = NULL;
339 if (tps65090_reg_matches)
340 config.of_node = tps65090_reg_matches[num].of_node;
342 config.of_node = NULL;
344 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
346 dev_err(&pdev->dev, "failed to register regulator %s\n",
348 return PTR_ERR(rdev);
352 if (ri->overcurrent_wait_valid) {
353 ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
358 /* Enable external control if it is require */
359 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
360 tps_pdata->enable_ext_control) {
361 ret = tps65090_config_ext_control(ri, true);
367 platform_set_drvdata(pdev, pmic);
371 static struct platform_driver tps65090_regulator_driver = {
373 .name = "tps65090-pmic",
374 .owner = THIS_MODULE,
376 .probe = tps65090_regulator_probe,
379 static int __init tps65090_regulator_init(void)
381 return platform_driver_register(&tps65090_regulator_driver);
383 subsys_initcall(tps65090_regulator_init);
385 static void __exit tps65090_regulator_exit(void)
387 platform_driver_unregister(&tps65090_regulator_driver);
389 module_exit(tps65090_regulator_exit);
391 MODULE_DESCRIPTION("tps65090 regulator driver");
392 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
393 MODULE_LICENSE("GPL v2");
394 MODULE_ALIAS("platform:tps65090-pmic");