regulator: tps65090: Support for external control of DCDC
[linux-2.6.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/delay.h>
21 #include <linux/init.h>
22 #include <linux/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/mfd/tps65090.h>
29 #include <linux/regulator/tps65090-regulator.h>
30
31 struct tps65090_regulator_info {
32         int             id;
33         /* Regulator register address.*/
34         u8              reg_en_reg;
35         u8              en_bit;
36
37         /* used by regulator core */
38         struct regulator_desc   desc;
39 };
40
41 struct tps65090_regulator {
42         struct tps65090_regulator_info  *rinfo;
43         struct device                   *dev;
44         struct regulator_dev            *rdev;
45         bool                            enable_ext_control;
46         int                             gpio;
47         int                             gpio_state;
48 };
49
50 static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
51 {
52         return rdev_get_dev(rdev)->parent->parent;
53 }
54
55 static inline bool is_dcdc(int id)
56 {
57         if ((id == TPS65090_REGULATOR_DCDC1) ||
58                         (id == TPS65090_REGULATOR_DCDC2) ||
59                         (id == TPS65090_REGULATOR_DCDC2))
60                 return true;
61         return false;
62 }
63
64 static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
65 {
66         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
67         struct device *parent = to_tps65090_dev(rdev);
68         uint8_t control = 0;
69         int ret;
70
71         if (is_dcdc(ri->rinfo->desc.id) && ri->enable_ext_control) {
72                 if (gpio_is_valid(ri->gpio))
73                         return ri->gpio_state;
74                 return 1;
75         }
76
77         ret = tps65090_read(parent, ri->rinfo->reg_en_reg, &control);
78         if (ret < 0) {
79                 dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
80                         ri->rinfo->reg_en_reg);
81                 return ret;
82         }
83         return (((control >> ri->rinfo->en_bit) & 1) == 1);
84 }
85
86 static int tps65090_reg_enable(struct regulator_dev *rdev)
87 {
88         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
89         struct device *parent = to_tps65090_dev(rdev);
90         int ret;
91
92         if (is_dcdc(ri->rinfo->desc.id) && ri->enable_ext_control) {
93                 if (gpio_is_valid(ri->gpio)) {
94                         gpio_set_value(ri->gpio, 1);
95                         ri->gpio_state = 1;
96                 }
97                 return 0;
98         }
99
100         ret = tps65090_set_bits(parent, ri->rinfo->reg_en_reg,
101                                 ri->rinfo->en_bit);
102         if (ret < 0)
103                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
104                         ri->rinfo->reg_en_reg);
105         return ret;
106 }
107
108 static int tps65090_reg_disable(struct regulator_dev *rdev)
109 {
110         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
111         struct device *parent = to_tps65090_dev(rdev);
112         int ret;
113
114         if (is_dcdc(ri->rinfo->desc.id) && ri->enable_ext_control) {
115                 if (gpio_is_valid(ri->gpio)) {
116                         gpio_set_value(ri->gpio, 0);
117                         ri->gpio_state = 0;
118                 }
119                 return 0;
120         }
121
122         ret = tps65090_clr_bits(parent, ri->rinfo->reg_en_reg,
123                                 ri->rinfo->en_bit);
124         if (ret < 0)
125                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
126                         ri->rinfo->reg_en_reg);
127
128         return ret;
129 }
130
131 static struct regulator_ops tps65090_ops = {
132         .enable         = tps65090_reg_enable,
133         .disable        = tps65090_reg_disable,
134         .is_enabled     = tps65090_reg_is_enabled,
135 };
136
137 static struct regulator_ops tps65090_ldo_ops = {
138 };
139
140 #define tps65090_REG(_id, _en_reg, _en_bit, _ops)       \
141 {                                                       \
142         .reg_en_reg     = _en_reg,                      \
143         .en_bit         = _en_bit,                      \
144         .id             = TPS65090_REGULATOR_##_id,     \
145         .desc = {                                       \
146                 .name = tps65090_rails(_id),            \
147                 .id = TPS65090_REGULATOR_##_id,         \
148                 .ops = &_ops,                           \
149                 .type = REGULATOR_VOLTAGE,              \
150                 .owner = THIS_MODULE,                   \
151         },                                              \
152 }
153
154 static struct tps65090_regulator_info TPS65090_regulator_info[] = {
155         tps65090_REG(DCDC1, 12, 0, tps65090_ops),
156         tps65090_REG(DCDC2, 13, 0, tps65090_ops),
157         tps65090_REG(DCDC3, 14, 0, tps65090_ops),
158         tps65090_REG(LDO1, 0, 0, tps65090_ldo_ops),
159         tps65090_REG(LDO2, 0, 0, tps65090_ldo_ops),
160         tps65090_REG(FET1,  15, 0, tps65090_ops),
161         tps65090_REG(FET2,  16, 0, tps65090_ops),
162         tps65090_REG(FET3,  17, 0, tps65090_ops),
163         tps65090_REG(FET4,  18, 0, tps65090_ops),
164         tps65090_REG(FET5,  19, 0, tps65090_ops),
165         tps65090_REG(FET6,  20, 0, tps65090_ops),
166         tps65090_REG(FET7,  21, 0, tps65090_ops),
167 };
168
169 static inline struct tps65090_regulator_info *find_regulator_info(int id)
170 {
171         struct tps65090_regulator_info *rinfo;
172         int i;
173
174         for (i = 0; i < ARRAY_SIZE(TPS65090_regulator_info); i++) {
175                 rinfo = &TPS65090_regulator_info[i];
176                 if (rinfo->desc.id == id)
177                         return rinfo;
178         }
179         return NULL;
180 }
181
182 static int __devinit tps65090_regulator_preinit(int id,
183                 struct tps65090_regulator *ri,
184                 struct tps65090_regulator_platform_data *tps_pdata)
185 {
186         int ret = 0;
187         struct device *parent = ri->dev->parent;
188
189         if (!tps_pdata->enable_ext_control) {
190                 ret =  tps65090_clr_bits(parent,
191                                 ri->rinfo->reg_en_reg, 1);
192                 if (ret < 0) {
193                         dev_err(ri->dev, "Error in clr reg 0x%x\n",
194                                 ri->rinfo->reg_en_reg);
195                         return ret;
196                 }
197         }
198
199         if (gpio_is_valid(tps_pdata->gpio)) {
200                 int gpio_flag = GPIOF_OUT_INIT_LOW;
201                 const char *sname;
202
203                 sname = tps_pdata->reg_init_data->constraints.name;
204                 if (!sname)
205                         sname = ri->rinfo->desc.name;
206                 ri->gpio_state = 0;
207                 if (tps_pdata->reg_init_data->constraints.always_on ||
208                                 tps_pdata->reg_init_data->constraints.boot_on) {
209                         gpio_flag = GPIOF_OUT_INIT_HIGH;
210                         ri->gpio_state = 1;
211                 }
212
213                 ret = gpio_request_one(tps_pdata->gpio, gpio_flag, sname);
214                 if (ret < 0) {
215                         dev_err(ri->dev, "gpio request failed, e %d\n", ret);
216                         return ret;
217                 }
218         }
219         ret = tps65090_set_bits(parent, ri->rinfo->reg_en_reg, 1);
220         if (ret < 0) {
221                 dev_err(ri->dev, "Error in setting reg 0x%x\n",
222                                 ri->rinfo->reg_en_reg);
223                 return ret;
224         }
225         ri->enable_ext_control = true;
226         ri->gpio = tps_pdata->gpio;
227         return ret;
228 }
229
230 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
231 {
232         struct tps65090_regulator_info *rinfo = NULL;
233         struct tps65090_regulator *ri;
234         struct tps65090_regulator *pmic;
235         struct regulator_dev *rdev;
236         struct tps65090_regulator_platform_data *tps_pdata;
237         struct tps65090_platform_data *tps65090_pdata;
238         int id;
239         int num;
240         int ret;
241
242         dev_dbg(&pdev->dev, "Probing regulator\n");
243
244         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
245         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata) {
246                 dev_err(&pdev->dev, "Proper platform data missing\n");
247                 return -EINVAL;
248         }
249
250         pmic = devm_kzalloc(&pdev->dev,
251                         tps65090_pdata->num_reg_pdata * sizeof(*pmic),
252                         GFP_KERNEL);
253         if (!pmic) {
254                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
255                 return -ENOMEM;
256         }
257
258         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
259                 tps_pdata = tps65090_pdata->reg_pdata[num];
260                 if (!tps_pdata || !tps_pdata->reg_init_data) {
261                         dev_err(&pdev->dev,
262                                 "Null platform data for regultor %d\n", num);
263                         ret = -EINVAL;
264                         goto scrub;
265                 }
266
267                 id = tps_pdata->id;
268                 rinfo = find_regulator_info(id);
269                 if (!rinfo) {
270                         dev_err(&pdev->dev,
271                                 "invalid regulator ID %d specified\n", id);
272                         ret = -EINVAL;
273                         goto scrub;
274                 }
275
276                 ri = &pmic[num];
277                 ri->dev = &pdev->dev;
278                 ri->rinfo = rinfo;
279
280                 if (is_dcdc(id)) {
281                         ret = tps65090_regulator_preinit(id, ri, tps_pdata);
282                         if (ret < 0) {
283                                 dev_err(&pdev->dev,
284                                         "failed to preinit regulator %d\n", id);
285                                 goto scrub;
286                         }
287                 }
288                 rdev = regulator_register(&ri->rinfo->desc, &pdev->dev,
289                                 tps_pdata->reg_init_data, ri);
290                 if (IS_ERR(rdev)) {
291                         dev_err(&pdev->dev, "failed to register regulator %s\n",
292                                 ri->rinfo->desc.name);
293                         ret = PTR_ERR(rdev);
294                         goto scrub;
295                 }
296                 ri->rdev = rdev;
297         }
298
299         platform_set_drvdata(pdev, pmic);
300         return 0;
301
302 scrub:
303         while (--num >= 0) {
304                 ri = &pmic[num];
305                 regulator_unregister(ri->rdev);
306                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
307                                 if (gpio_is_valid(ri->gpio))
308                                         gpio_free(ri->gpio);
309                 }
310         }
311         return ret;
312 }
313
314 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
315 {
316         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
317         struct tps65090_platform_data *tps65090_pdata;
318         struct tps65090_regulator *ri;
319         int num;
320
321         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
322         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata)
323                 return 0;
324
325         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
326                 ri = &pmic[num];
327                 regulator_unregister(ri->rdev);
328                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
329                                 if (gpio_is_valid(ri->gpio))
330                                         gpio_free(ri->gpio);
331                 }
332         }
333         return 0;
334 }
335
336 static struct platform_driver tps65090_regulator_driver = {
337         .driver = {
338                 .name   = "tps65090-pmic",
339                 .owner  = THIS_MODULE,
340         },
341         .probe          = tps65090_regulator_probe,
342         .remove         = __devexit_p(tps65090_regulator_remove),
343 };
344
345 static int __init tps65090_regulator_init(void)
346 {
347         return platform_driver_register(&tps65090_regulator_driver);
348 }
349 subsys_initcall(tps65090_regulator_init);
350
351 static void __exit tps65090_regulator_exit(void)
352 {
353         platform_driver_unregister(&tps65090_regulator_driver);
354 }
355 module_exit(tps65090_regulator_exit);
356
357 MODULE_DESCRIPTION("tps65090 regulator driver");
358 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
359 MODULE_LICENSE("GPL v2");