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 struct tps65090_regulator {
33 struct regulator_desc *desc;
34 struct regulator_dev *rdev;
37 static struct regulator_ops tps65090_ext_control_ops = {
40 static struct regulator_ops tps65090_reg_contol_ops = {
41 .enable = regulator_enable_regmap,
42 .disable = regulator_disable_regmap,
43 .is_enabled = regulator_is_enabled_regmap,
46 static struct regulator_ops tps65090_ldo_ops = {
49 #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
51 .name = "TPS65090_RAILS"#_id, \
52 .supply_name = _sname, \
53 .id = TPS65090_REGULATOR_##_id, \
55 .enable_reg = _en_reg, \
56 .enable_mask = BIT(0), \
57 .type = REGULATOR_VOLTAGE, \
58 .owner = THIS_MODULE, \
61 static struct regulator_desc tps65090_regulator_desc[] = {
62 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops),
63 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops),
64 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops),
65 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops),
66 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops),
67 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops),
68 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops),
69 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops),
70 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops),
71 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops),
72 tps65090_REG_DESC(LDO1, "vsys-l1", 0, tps65090_ldo_ops),
73 tps65090_REG_DESC(LDO2, "vsys-l2", 0, tps65090_ldo_ops),
76 static inline bool is_dcdc(int id)
79 case TPS65090_REGULATOR_DCDC1:
80 case TPS65090_REGULATOR_DCDC2:
81 case TPS65090_REGULATOR_DCDC3:
88 static int tps65090_config_ext_control(
89 struct tps65090_regulator *ri, bool enable)
92 struct device *parent = ri->dev->parent;
93 unsigned int reg_en_reg = ri->desc->enable_reg;
96 ret = tps65090_set_bits(parent, reg_en_reg, 1);
98 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
100 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
104 static int tps65090_regulator_disable_ext_control(
105 struct tps65090_regulator *ri,
106 struct tps65090_regulator_plat_data *tps_pdata)
109 struct device *parent = ri->dev->parent;
110 unsigned int reg_en_reg = ri->desc->enable_reg;
113 * First enable output for internal control if require.
114 * And then disable external control.
116 if (tps_pdata->reg_init_data->constraints.always_on ||
117 tps_pdata->reg_init_data->constraints.boot_on) {
118 ret = tps65090_set_bits(parent, reg_en_reg, 0);
120 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
124 return tps65090_config_ext_control(ri, false);
127 static void tps65090_configure_regulator_config(
128 struct tps65090_regulator_plat_data *tps_pdata,
129 struct regulator_config *config)
131 if (gpio_is_valid(tps_pdata->gpio)) {
132 int gpio_flag = GPIOF_OUT_INIT_LOW;
134 if (tps_pdata->reg_init_data->constraints.always_on ||
135 tps_pdata->reg_init_data->constraints.boot_on)
136 gpio_flag = GPIOF_OUT_INIT_HIGH;
138 config->ena_gpio = tps_pdata->gpio;
139 config->ena_gpio_flags = gpio_flag;
144 static struct of_regulator_match tps65090_matches[] = {
145 { .name = "dcdc1", },
146 { .name = "dcdc2", },
147 { .name = "dcdc3", },
159 static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
160 struct platform_device *pdev,
161 struct of_regulator_match **tps65090_reg_matches)
163 struct tps65090_platform_data *tps65090_pdata;
164 struct device_node *np = pdev->dev.parent->of_node;
165 struct device_node *regulators;
167 struct tps65090_regulator_plat_data *reg_pdata;
169 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
171 if (!tps65090_pdata) {
172 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
173 return ERR_PTR(-ENOMEM);
176 reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
177 sizeof(*reg_pdata), GFP_KERNEL);
179 dev_err(&pdev->dev, "Memory alloc for reg_pdata failed\n");
180 return ERR_PTR(-ENOMEM);
183 regulators = of_get_child_by_name(np, "regulators");
185 dev_err(&pdev->dev, "regulator node not found\n");
186 return ERR_PTR(-ENODEV);
189 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
190 ARRAY_SIZE(tps65090_matches));
193 "Error parsing regulator init data: %d\n", ret);
197 *tps65090_reg_matches = tps65090_matches;
198 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
199 struct regulator_init_data *ri_data;
200 struct tps65090_regulator_plat_data *rpdata;
202 rpdata = ®_pdata[idx];
203 ri_data = tps65090_matches[idx].init_data;
204 if (!ri_data || !tps65090_matches[idx].of_node)
207 rpdata->reg_init_data = ri_data;
208 rpdata->enable_ext_control = of_property_read_bool(
209 tps65090_matches[idx].of_node,
210 "ti,enable-ext-control");
211 if (rpdata->enable_ext_control)
212 rpdata->gpio = of_get_named_gpio(np,
213 "dcdc-ext-control-gpios", 0);
215 tps65090_pdata->reg_pdata[idx] = rpdata;
217 return tps65090_pdata;
220 static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
221 struct platform_device *pdev,
222 struct of_regulator_match **tps65090_reg_matches)
224 *tps65090_reg_matches = NULL;
229 static int tps65090_regulator_probe(struct platform_device *pdev)
231 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
232 struct tps65090_regulator *ri = NULL;
233 struct regulator_config config = { };
234 struct regulator_dev *rdev;
235 struct tps65090_regulator_plat_data *tps_pdata;
236 struct tps65090_regulator *pmic;
237 struct tps65090_platform_data *tps65090_pdata;
238 struct of_regulator_match *tps65090_reg_matches = NULL;
242 dev_dbg(&pdev->dev, "Probing regulator\n");
244 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
245 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
246 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
247 &tps65090_reg_matches);
248 if (IS_ERR_OR_NULL(tps65090_pdata)) {
249 dev_err(&pdev->dev, "Platform data missing\n");
250 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
253 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
256 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
260 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
261 tps_pdata = tps65090_pdata->reg_pdata[num];
264 ri->dev = &pdev->dev;
265 ri->desc = &tps65090_regulator_desc[num];
268 * TPS5090 DCDC support the control from external digital input.
269 * Configure it as per platform data.
271 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
272 if (tps_pdata->enable_ext_control) {
273 tps65090_configure_regulator_config(
275 ri->desc->ops = &tps65090_ext_control_ops;
277 ret = tps65090_regulator_disable_ext_control(
281 "failed disable ext control\n");
287 config.dev = pdev->dev.parent;
288 config.driver_data = ri;
289 config.regmap = tps65090_mfd->rmap;
291 config.init_data = tps_pdata->reg_init_data;
293 config.init_data = NULL;
294 if (tps65090_reg_matches)
295 config.of_node = tps65090_reg_matches[num].of_node;
297 config.of_node = NULL;
299 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
301 dev_err(&pdev->dev, "failed to register regulator %s\n",
303 return PTR_ERR(rdev);
307 /* Enable external control if it is require */
308 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
309 tps_pdata->enable_ext_control) {
310 ret = tps65090_config_ext_control(ri, true);
316 platform_set_drvdata(pdev, pmic);
320 static struct platform_driver tps65090_regulator_driver = {
322 .name = "tps65090-pmic",
323 .owner = THIS_MODULE,
325 .probe = tps65090_regulator_probe,
328 static int __init tps65090_regulator_init(void)
330 return platform_driver_register(&tps65090_regulator_driver);
332 subsys_initcall(tps65090_regulator_init);
334 static void __exit tps65090_regulator_exit(void)
336 platform_driver_unregister(&tps65090_regulator_driver);
338 module_exit(tps65090_regulator_exit);
340 MODULE_DESCRIPTION("tps65090 regulator driver");
341 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
342 MODULE_LICENSE("GPL v2");
343 MODULE_ALIAS("platform:tps65090-pmic");