regulator: palma: Disable smps10 boost during suspend
[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_DCDC3))
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                 return ret;
210         }
211
212         if (gpio_is_valid(tps_pdata->gpio)) {
213                 int gpio_flag = GPIOF_OUT_INIT_LOW;
214                 const char *sname;
215
216                 sname = tps_pdata->reg_init_data->constraints.name;
217                 if (!sname)
218                         sname = ri->rinfo->desc.name;
219                 ri->gpio_state = 0;
220                 if (tps_pdata->reg_init_data->constraints.always_on ||
221                                 tps_pdata->reg_init_data->constraints.boot_on) {
222                         gpio_flag = GPIOF_OUT_INIT_HIGH;
223                         ri->gpio_state = 1;
224                 }
225
226                 ret = gpio_request_one(tps_pdata->gpio, gpio_flag, sname);
227                 if (ret < 0) {
228                         dev_err(ri->dev, "gpio request failed, e %d\n", ret);
229                         return ret;
230                 }
231         }
232         ret = tps65090_set_bits(parent, ri->rinfo->reg_en_reg, 1);
233         if (ret < 0) {
234                 dev_err(ri->dev, "Error in setting reg 0x%x\n",
235                                 ri->rinfo->reg_en_reg);
236                 return ret;
237         }
238         ri->enable_ext_control = true;
239         ri->gpio = tps_pdata->gpio;
240         return ret;
241 }
242
243 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
244 {
245         struct tps65090_regulator_info *rinfo = NULL;
246         struct tps65090_regulator *ri;
247         struct tps65090_regulator *pmic;
248         struct regulator_dev *rdev;
249         struct tps65090_regulator_platform_data *tps_pdata;
250         struct tps65090_platform_data *tps65090_pdata;
251         int id;
252         int num;
253         int ret;
254
255         dev_dbg(&pdev->dev, "Probing regulator\n");
256
257         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
258         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata) {
259                 dev_err(&pdev->dev, "Proper platform data missing\n");
260                 return -EINVAL;
261         }
262
263         pmic = devm_kzalloc(&pdev->dev,
264                         tps65090_pdata->num_reg_pdata * sizeof(*pmic),
265                         GFP_KERNEL);
266         if (!pmic) {
267                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
268                 return -ENOMEM;
269         }
270
271         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
272                 tps_pdata = tps65090_pdata->reg_pdata[num];
273                 if (!tps_pdata || !tps_pdata->reg_init_data) {
274                         dev_err(&pdev->dev,
275                                 "Null platform data for regultor %d\n", num);
276                         ret = -EINVAL;
277                         goto scrub;
278                 }
279
280                 id = tps_pdata->id;
281                 rinfo = find_regulator_info(id);
282                 if (!rinfo) {
283                         dev_err(&pdev->dev,
284                                 "invalid regulator ID %d specified\n", id);
285                         ret = -EINVAL;
286                         goto scrub;
287                 }
288
289                 ri = &pmic[num];
290                 ri->dev = &pdev->dev;
291                 ri->rinfo = rinfo;
292
293                 if (is_dcdc(id)) {
294                         ret = tps65090_regulator_preinit(id, ri, tps_pdata);
295                         if (ret < 0) {
296                                 dev_err(&pdev->dev,
297                                         "failed to preinit regulator %d\n", id);
298                                 goto scrub;
299                         }
300                 }
301                 rdev = regulator_register(&ri->rinfo->desc, &pdev->dev,
302                                 tps_pdata->reg_init_data, ri, NULL);
303                 if (IS_ERR(rdev)) {
304                         dev_err(&pdev->dev, "failed to register regulator %s\n",
305                                 ri->rinfo->desc.name);
306                         ret = PTR_ERR(rdev);
307                         goto scrub;
308                 }
309                 ri->rdev = rdev;
310         }
311
312         platform_set_drvdata(pdev, pmic);
313         return 0;
314
315 scrub:
316         while (--num >= 0) {
317                 ri = &pmic[num];
318                 regulator_unregister(ri->rdev);
319                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
320                                 if (gpio_is_valid(ri->gpio))
321                                         gpio_free(ri->gpio);
322                 }
323         }
324         return ret;
325 }
326
327 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
328 {
329         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
330         struct tps65090_platform_data *tps65090_pdata;
331         struct tps65090_regulator *ri;
332         int num;
333
334         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
335         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata)
336                 return 0;
337
338         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
339                 ri = &pmic[num];
340                 regulator_unregister(ri->rdev);
341                 if (is_dcdc(ri->rinfo->desc.id) && (ri->enable_ext_control)) {
342                                 if (gpio_is_valid(ri->gpio))
343                                         gpio_free(ri->gpio);
344                 }
345         }
346         return 0;
347 }
348
349 static struct platform_driver tps65090_regulator_driver = {
350         .driver = {
351                 .name   = "tps65090-pmic",
352                 .owner  = THIS_MODULE,
353         },
354         .probe          = tps65090_regulator_probe,
355         .remove         = __devexit_p(tps65090_regulator_remove),
356 };
357
358 static int __init tps65090_regulator_init(void)
359 {
360         return platform_driver_register(&tps65090_regulator_driver);
361 }
362 subsys_initcall(tps65090_regulator_init);
363
364 static void __exit tps65090_regulator_exit(void)
365 {
366         platform_driver_unregister(&tps65090_regulator_driver);
367 }
368 module_exit(tps65090_regulator_exit);
369
370 MODULE_DESCRIPTION("tps65090 regulator driver");
371 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
372 MODULE_LICENSE("GPL v2");