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