]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/tps65090-regulator.c
regulator: tps65090: Add support for LDO regulators
[karo-tx-linux.git] / drivers / regulator / tps65090-regulator.c
1 /*
2  * Regulator driver for tps65090 power management chip.
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5
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.
9
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
13  * more details.
14
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/>
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/mfd/tps65090.h>
27
28 struct tps65090_regulator {
29         struct device           *dev;
30         struct regulator_desc   *desc;
31         struct regulator_dev    *rdev;
32 };
33
34 static struct regulator_ops tps65090_ops = {
35         .enable = regulator_enable_regmap,
36         .disable = regulator_disable_regmap,
37         .is_enabled = regulator_is_enabled_regmap,
38 };
39
40 static struct regulator_ops tps65090_ldo_ops = {
41 };
42
43 #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops)   \
44 {                                                       \
45         .name = "TPS65090_RAILS"#_id,                   \
46         .supply_name = _sname,                          \
47         .id = TPS65090_REGULATOR_##_id,                 \
48         .ops = &_ops,                                   \
49         .enable_reg = _en_reg,                          \
50         .enable_mask = BIT(0),                          \
51         .type = REGULATOR_VOLTAGE,                      \
52         .owner = THIS_MODULE,                           \
53 }
54
55 static struct regulator_desc tps65090_regulator_desc[] = {
56         tps65090_REG_DESC(DCDC1, "vsys1",   0x0C, tps65090_ops),
57         tps65090_REG_DESC(DCDC2, "vsys2",   0x0D, tps65090_ops),
58         tps65090_REG_DESC(DCDC3, "vsys3",   0x0E, tps65090_ops),
59         tps65090_REG_DESC(FET1,  "infet1",  0x0F, tps65090_ops),
60         tps65090_REG_DESC(FET2,  "infet2",  0x10, tps65090_ops),
61         tps65090_REG_DESC(FET3,  "infet3",  0x11, tps65090_ops),
62         tps65090_REG_DESC(FET4,  "infet4",  0x12, tps65090_ops),
63         tps65090_REG_DESC(FET5,  "infet5",  0x13, tps65090_ops),
64         tps65090_REG_DESC(FET6,  "infet6",  0x14, tps65090_ops),
65         tps65090_REG_DESC(FET7,  "infet7",  0x15, tps65090_ops),
66         tps65090_REG_DESC(LDO1,  "vsys_l1", 0,    tps65090_ldo_ops),
67         tps65090_REG_DESC(LDO2,  "vsys_l2", 0,    tps65090_ldo_ops),
68 };
69
70 static inline bool is_dcdc(int id)
71 {
72         switch (id) {
73         case TPS65090_REGULATOR_DCDC1:
74         case TPS65090_REGULATOR_DCDC2:
75         case TPS65090_REGULATOR_DCDC3:
76                 return true;
77         default:
78                 return false;
79         }
80 }
81
82 static int __devinit tps65090_config_ext_control(
83         struct tps65090_regulator *ri, bool enable)
84 {
85         int ret;
86         struct device *parent = ri->dev->parent;
87         unsigned int reg_en_reg = ri->desc->enable_reg;
88
89         if (enable)
90                 ret = tps65090_set_bits(parent, reg_en_reg, 1);
91         else
92                 ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
93         if (ret < 0)
94                 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
95         return ret;
96 }
97
98 static int __devinit tps65090_regulator_disable_ext_control(
99                 struct tps65090_regulator *ri,
100                 struct tps65090_regulator_plat_data *tps_pdata)
101 {
102         int ret = 0;
103         struct device *parent = ri->dev->parent;
104         unsigned int reg_en_reg = ri->desc->enable_reg;
105
106         /*
107          * First enable output for internal control if require.
108          * And then disable external control.
109          */
110         if (tps_pdata->reg_init_data->constraints.always_on ||
111                         tps_pdata->reg_init_data->constraints.boot_on) {
112                 ret =  tps65090_set_bits(parent, reg_en_reg, 0);
113                 if (ret < 0) {
114                         dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
115                         return ret;
116                 }
117         }
118         return tps65090_config_ext_control(ri, false);
119 }
120
121 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
122 {
123         struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
124         struct tps65090_regulator *ri = NULL;
125         struct regulator_config config = { };
126         struct regulator_dev *rdev;
127         struct tps65090_regulator_plat_data *tps_pdata;
128         struct tps65090_regulator *pmic;
129         struct tps65090_platform_data *tps65090_pdata;
130         int num;
131         int ret;
132
133         dev_dbg(&pdev->dev, "Probing regulator\n");
134
135         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
136         if (!tps65090_pdata) {
137                 dev_err(&pdev->dev, "Platform data missing\n");
138                 return -EINVAL;
139         }
140
141         pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
142                         GFP_KERNEL);
143         if (!pmic) {
144                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
145                 return -ENOMEM;
146         }
147
148         for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
149                 tps_pdata = tps65090_pdata->reg_pdata[num];
150
151                 ri = &pmic[num];
152                 ri->dev = &pdev->dev;
153                 ri->desc = &tps65090_regulator_desc[num];
154
155                 /*
156                  * TPS5090 DCDC support the control from external digital input.
157                  * It may be possible that during boot, the external control is
158                  * enabled. Disabling external control for DCDC.
159                  */
160                 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
161                         ret = tps65090_regulator_disable_ext_control(
162                                                 ri, tps_pdata);
163                         if (ret < 0) {
164                                 dev_err(&pdev->dev,
165                                                 "failed disable ext control\n");
166                                 goto scrub;
167                         }
168                 }
169                 config.dev = &pdev->dev;
170                 config.driver_data = ri;
171                 config.regmap = tps65090_mfd->rmap;
172                 if (tps_pdata)
173                         config.init_data = tps_pdata->reg_init_data;
174                 else
175                         config.init_data = NULL;
176
177                 rdev = regulator_register(ri->desc, &config);
178                 if (IS_ERR(rdev)) {
179                         dev_err(&pdev->dev, "failed to register regulator %s\n",
180                                 ri->desc->name);
181                         ret = PTR_ERR(rdev);
182                         goto scrub;
183                 }
184                 ri->rdev = rdev;
185         }
186
187         platform_set_drvdata(pdev, pmic);
188         return 0;
189
190 scrub:
191         while (--num >= 0) {
192                 ri = &pmic[num];
193                 regulator_unregister(ri->rdev);
194         }
195         return ret;
196 }
197
198 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
199 {
200         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
201         struct tps65090_regulator *ri;
202         int num;
203
204         for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) {
205                 ri = &pmic[num];
206                 regulator_unregister(ri->rdev);
207         }
208         return 0;
209 }
210
211 static struct platform_driver tps65090_regulator_driver = {
212         .driver = {
213                 .name   = "tps65090-pmic",
214                 .owner  = THIS_MODULE,
215         },
216         .probe          = tps65090_regulator_probe,
217         .remove         = __devexit_p(tps65090_regulator_remove),
218 };
219
220 static int __init tps65090_regulator_init(void)
221 {
222         return platform_driver_register(&tps65090_regulator_driver);
223 }
224 subsys_initcall(tps65090_regulator_init);
225
226 static void __exit tps65090_regulator_exit(void)
227 {
228         platform_driver_unregister(&tps65090_regulator_driver);
229 }
230 module_exit(tps65090_regulator_exit);
231
232 MODULE_DESCRIPTION("tps65090 regulator driver");
233 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
234 MODULE_LICENSE("GPL v2");