regulator: palmas: Fix warning message
[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, _sname, _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                 .supply_name = _sname,                  \
148                 .id = TPS65090_REGULATOR_##_id,         \
149                 .ops = &_ops,                           \
150                 .type = REGULATOR_VOLTAGE,              \
151                 .owner = THIS_MODULE,                   \
152         },                                              \
153 }
154
155 static struct tps65090_regulator_info TPS65090_regulator_info[] = {
156         tps65090_REG(DCDC1, "VSYS1",   12, 0, tps65090_ops),
157         tps65090_REG(DCDC2, "VSYS2",   13, 0, tps65090_ops),
158         tps65090_REG(DCDC3, "VSYS3",   14, 0, tps65090_ops),
159         tps65090_REG(LDO1,  "VSYS_L1", 0,  0, tps65090_ldo_ops),
160         tps65090_REG(LDO2,  "VSYS_L2", 0,  0, tps65090_ldo_ops),
161         tps65090_REG(FET1,  "INFET1",  15, 0, tps65090_ops),
162         tps65090_REG(FET2,  "INFET2",  16, 0, tps65090_ops),
163         tps65090_REG(FET3,  "INFET3",  17, 0, tps65090_ops),
164         tps65090_REG(FET4,  "INFET4",  18, 0, tps65090_ops),
165         tps65090_REG(FET5,  "INFET5",  19, 0, tps65090_ops),
166         tps65090_REG(FET6,  "INFET6",  20, 0, tps65090_ops),
167         tps65090_REG(FET7,  "INFET7",  21, 0, tps65090_ops),
168 };
169
170 static inline struct tps65090_regulator_info *find_regulator_info(int id)
171 {
172         struct tps65090_regulator_info *rinfo;
173         int i;
174
175         for (i = 0; i < ARRAY_SIZE(TPS65090_regulator_info); i++) {
176                 rinfo = &TPS65090_regulator_info[i];
177                 if (rinfo->desc.id == id)
178                         return rinfo;
179         }
180         return NULL;
181 }
182
183 static int __devinit tps65090_regulator_preinit(int id,
184                 struct tps65090_regulator *ri,
185                 struct tps65090_regulator_platform_data *tps_pdata)
186 {
187         int ret = 0;
188         struct device *parent = ri->dev->parent;
189
190         if (!tps_pdata->enable_ext_control) {
191                 if (tps_pdata->reg_init_data->constraints.always_on ||
192                                 tps_pdata->reg_init_data->constraints.boot_on) {
193                         ret =  tps65090_set_bits(parent,
194                                         ri->rinfo->reg_en_reg, 0);
195                         if (ret < 0) {
196                                 dev_err(ri->dev, "Error in set reg 0x%x\n",
197                                         ri->rinfo->reg_en_reg);
198                                 return ret;
199                         }
200                 }
201
202                 ret =  tps65090_clr_bits(parent,
203                                 ri->rinfo->reg_en_reg, 1);
204                 if (ret < 0) {
205                         dev_err(ri->dev, "Error in clr reg 0x%x\n",
206                                 ri->rinfo->reg_en_reg);
207                         return ret;
208                 }
209         }
210
211         if (gpio_is_valid(tps_pdata->gpio)) {
212                 int gpio_flag = GPIOF_OUT_INIT_LOW;
213                 const char *sname;
214
215                 sname = tps_pdata->reg_init_data->constraints.name;
216                 if (!sname)
217                         sname = ri->rinfo->desc.name;
218                 ri->gpio_state = 0;
219                 if (tps_pdata->reg_init_data->constraints.always_on ||
220                                 tps_pdata->reg_init_data->constraints.boot_on) {
221                         gpio_flag = GPIOF_OUT_INIT_HIGH;
222                         ri->gpio_state = 1;
223                 }
224
225                 ret = gpio_request_one(tps_pdata->gpio, gpio_flag, sname);
226                 if (ret < 0) {
227                         dev_err(ri->dev, "gpio request failed, e %d\n", ret);
228                         return ret;
229                 }
230         }
231         ret = tps65090_set_bits(parent, ri->rinfo->reg_en_reg, 1);
232         if (ret < 0) {
233                 dev_err(ri->dev, "Error in setting reg 0x%x\n",
234                                 ri->rinfo->reg_en_reg);
235                 return ret;
236         }
237         ri->enable_ext_control = true;
238         ri->gpio = tps_pdata->gpio;
239         return ret;
240 }
241
242 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
243 {
244         struct tps65090_regulator_info *rinfo = NULL;
245         struct tps65090_regulator *ri;
246         struct tps65090_regulator *pmic;
247         struct regulator_dev *rdev;
248         struct tps65090_regulator_platform_data *tps_pdata;
249         struct tps65090_platform_data *tps65090_pdata;
250         int id;
251         int num;
252         int ret;
253
254         dev_dbg(&pdev->dev, "Probing regulator\n");
255
256         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
257         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata) {
258                 dev_err(&pdev->dev, "Proper platform data missing\n");
259                 return -EINVAL;
260         }
261
262         pmic = devm_kzalloc(&pdev->dev,
263                         tps65090_pdata->num_reg_pdata * sizeof(*pmic),
264                         GFP_KERNEL);
265         if (!pmic) {
266                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
267                 return -ENOMEM;
268         }
269
270         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
271                 tps_pdata = tps65090_pdata->reg_pdata[num];
272                 if (!tps_pdata || !tps_pdata->reg_init_data) {
273                         dev_err(&pdev->dev,
274                                 "Null platform data for regultor %d\n", num);
275                         ret = -EINVAL;
276                         goto scrub;
277                 }
278
279                 id = tps_pdata->id;
280                 rinfo = find_regulator_info(id);
281                 if (!rinfo) {
282                         dev_err(&pdev->dev,
283                                 "invalid regulator ID %d specified\n", id);
284                         ret = -EINVAL;
285                         goto scrub;
286                 }
287
288                 ri = &pmic[num];
289                 ri->dev = &pdev->dev;
290                 ri->rinfo = rinfo;
291
292                 if (is_dcdc(id)) {
293                         ret = tps65090_regulator_preinit(id, ri, tps_pdata);
294                         if (ret < 0) {
295                                 dev_err(&pdev->dev,
296                                         "failed to preinit regulator %d\n", id);
297                                 goto scrub;
298                         }
299                 }
300                 rdev = regulator_register(&ri->rinfo->desc, &pdev->dev,
301                                 tps_pdata->reg_init_data, ri, NULL);
302                 if (IS_ERR(rdev)) {
303                         dev_err(&pdev->dev, "failed to register regulator %s\n",
304                                 ri->rinfo->desc.name);
305                         ret = PTR_ERR(rdev);
306                         goto scrub;
307                 }
308                 ri->rdev = rdev;
309         }
310
311         platform_set_drvdata(pdev, pmic);
312         return 0;
313
314 scrub:
315         while (--num >= 0) {
316                 ri = &pmic[num];
317                 regulator_unregister(ri->rdev);
318                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
319                                 if (gpio_is_valid(ri->gpio))
320                                         gpio_free(ri->gpio);
321                 }
322         }
323         return ret;
324 }
325
326 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
327 {
328         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
329         struct tps65090_platform_data *tps65090_pdata;
330         struct tps65090_regulator *ri;
331         int num;
332
333         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
334         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata)
335                 return 0;
336
337         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
338                 ri = &pmic[num];
339                 regulator_unregister(ri->rdev);
340                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
341                                 if (gpio_is_valid(ri->gpio))
342                                         gpio_free(ri->gpio);
343                 }
344         }
345         return 0;
346 }
347
348 static struct platform_driver tps65090_regulator_driver = {
349         .driver = {
350                 .name   = "tps65090-pmic",
351                 .owner  = THIS_MODULE,
352         },
353         .probe          = tps65090_regulator_probe,
354         .remove         = __devexit_p(tps65090_regulator_remove),
355 };
356
357 static int __init tps65090_regulator_init(void)
358 {
359         return platform_driver_register(&tps65090_regulator_driver);
360 }
361 subsys_initcall(tps65090_regulator_init);
362
363 static void __exit tps65090_regulator_exit(void)
364 {
365         platform_driver_unregister(&tps65090_regulator_driver);
366 }
367 module_exit(tps65090_regulator_exit);
368
369 MODULE_DESCRIPTION("tps65090 regulator driver");
370 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
371 MODULE_LICENSE("GPL v2");