f633897f051c864e2a26b4e9d1efe002b9d7369b
[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/slab.h>
23 #include <linux/err.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 #include <linux/mfd/tps65090.h>
28 #include <linux/regulator/tps65090-regulator.h>
29
30 struct tps65090_regulator_info {
31         int             id;
32         /* Regulator register address.*/
33         u8              reg_en_reg;
34         u8              en_bit;
35
36         /* used by regulator core */
37         struct regulator_desc   desc;
38 };
39
40 struct tps65090_regulator {
41         struct tps65090_regulator_info  *rinfo;
42         struct device                   *dev;
43         struct regulator_dev            *rdev;
44 };
45
46 static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
47 {
48         return rdev_get_dev(rdev)->parent->parent;
49 }
50
51 static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
52 {
53         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
54         struct device *parent = to_tps65090_dev(rdev);
55         uint8_t control = 0;
56         int ret;
57
58         ret = tps65090_read(parent, ri->rinfo->reg_en_reg, &control);
59         if (ret < 0) {
60                 dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
61                         ri->rinfo->reg_en_reg);
62                 return ret;
63         }
64         return (((control >> ri->rinfo->en_bit) & 1) == 1);
65 }
66
67 static int tps65090_reg_enable(struct regulator_dev *rdev)
68 {
69         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
70         struct device *parent = to_tps65090_dev(rdev);
71         int ret;
72
73         ret = tps65090_set_bits(parent, ri->rinfo->reg_en_reg,
74                                 ri->rinfo->en_bit);
75         if (ret < 0)
76                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
77                         ri->rinfo->reg_en_reg);
78         return ret;
79 }
80
81 static int tps65090_reg_disable(struct regulator_dev *rdev)
82 {
83         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
84         struct device *parent = to_tps65090_dev(rdev);
85         int ret;
86
87         ret = tps65090_clr_bits(parent, ri->rinfo->reg_en_reg,
88                                 ri->rinfo->en_bit);
89         if (ret < 0)
90                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
91                         ri->rinfo->reg_en_reg);
92
93         return ret;
94 }
95
96 static struct regulator_ops tps65090_ops = {
97         .enable         = tps65090_reg_enable,
98         .disable        = tps65090_reg_disable,
99         .is_enabled     = tps65090_reg_is_enabled,
100 };
101
102 static struct regulator_ops tps65090_ldo_ops = {
103 };
104
105 #define tps65090_REG(_id, _en_reg, _en_bit, _ops)       \
106 {                                                       \
107         .reg_en_reg     = _en_reg,                      \
108         .en_bit         = _en_bit,                      \
109         .id             = TPS65090_REGULATOR_##_id,     \
110         .desc = {                                       \
111                 .name = tps65090_rails(_id),            \
112                 .id = TPS65090_REGULATOR_##_id,         \
113                 .ops = &_ops,                           \
114                 .type = REGULATOR_VOLTAGE,              \
115                 .owner = THIS_MODULE,                   \
116         },                                              \
117 }
118
119 static struct tps65090_regulator_info TPS65090_regulator_info[] = {
120         tps65090_REG(DCDC1, 12, 0, tps65090_ops),
121         tps65090_REG(DCDC2, 13, 0, tps65090_ops),
122         tps65090_REG(DCDC3, 14, 0, tps65090_ops),
123         tps65090_REG(LDO1, 0, 0, tps65090_ldo_ops),
124         tps65090_REG(LDO2, 0, 0, tps65090_ldo_ops),
125         tps65090_REG(FET1,  15, 0, tps65090_ops),
126         tps65090_REG(FET2,  16, 0, tps65090_ops),
127         tps65090_REG(FET3,  17, 0, tps65090_ops),
128         tps65090_REG(FET4,  18, 0, tps65090_ops),
129         tps65090_REG(FET5,  19, 0, tps65090_ops),
130         tps65090_REG(FET6,  20, 0, tps65090_ops),
131         tps65090_REG(FET7,  21, 0, tps65090_ops),
132 };
133
134 static inline struct tps65090_regulator_info *find_regulator_info(int id)
135 {
136         struct tps65090_regulator_info *rinfo;
137         int i;
138
139         for (i = 0; i < ARRAY_SIZE(TPS65090_regulator_info); i++) {
140                 rinfo = &TPS65090_regulator_info[i];
141                 if (rinfo->desc.id == id)
142                         return rinfo;
143         }
144         return NULL;
145 }
146
147 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
148 {
149         struct tps65090_regulator_info *rinfo = NULL;
150         struct tps65090_regulator *ri;
151         struct tps65090_regulator *pmic;
152         struct regulator_dev *rdev;
153         struct tps65090_regulator_platform_data *tps_pdata;
154         struct tps65090_platform_data *tps65090_pdata;
155         int id;
156         int num;
157         int ret;
158
159         dev_dbg(&pdev->dev, "Probing regulator\n");
160
161         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
162         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata) {
163                 dev_err(&pdev->dev, "Proper platform data missing\n");
164                 return -EINVAL;
165         }
166
167         pmic = devm_kzalloc(&pdev->dev,
168                         tps65090_pdata->num_reg_pdata * sizeof(*pmic),
169                         GFP_KERNEL);
170         if (!pmic) {
171                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
172                 return -ENOMEM;
173         }
174
175         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
176                 tps_pdata = tps65090_pdata->reg_pdata[num];
177                 if (!tps_pdata || !tps_pdata->reg_init_data) {
178                         dev_err(&pdev->dev,
179                                 "Null platform data for regultor %d\n", num);
180                         ret = -EINVAL;
181                         goto scrub;
182                 }
183
184                 id = tps_pdata->id;
185                 rinfo = find_regulator_info(id);
186                 if (!rinfo) {
187                         dev_err(&pdev->dev,
188                                 "invalid regulator ID %d specified\n", id);
189                         ret = -EINVAL;
190                         goto scrub;
191                 }
192
193                 ri = &pmic[num];
194                 ri->dev = &pdev->dev;
195                 ri->rinfo = rinfo;
196                 rdev = regulator_register(&ri->rinfo->desc, &pdev->dev,
197                                 tps_pdata->reg_init_data, ri);
198                 if (IS_ERR(rdev)) {
199                         dev_err(&pdev->dev, "failed to register regulator %s\n",
200                                 ri->rinfo->desc.name);
201                         ret = PTR_ERR(rdev);
202                         goto scrub;
203                 }
204                 ri->rdev = rdev;
205         }
206
207         platform_set_drvdata(pdev, pmic);
208         return 0;
209
210 scrub:
211         while (--num >= 0) {
212                 ri = &pmic[num];
213                 regulator_unregister(ri->rdev);
214         }
215         return ret;
216 }
217
218 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
219 {
220         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
221         struct tps65090_platform_data *tps65090_pdata;
222         struct tps65090_regulator *ri;
223         int num;
224
225         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
226         if (!tps65090_pdata || !tps65090_pdata->num_reg_pdata)
227                 return 0;
228
229         for (num = 0; num < tps65090_pdata->num_reg_pdata; ++num) {
230                 ri = &pmic[num];
231                 regulator_unregister(ri->rdev);
232         }
233         return 0;
234 }
235
236 static struct platform_driver tps65090_regulator_driver = {
237         .driver = {
238                 .name   = "tps65090-pmic",
239                 .owner  = THIS_MODULE,
240         },
241         .probe          = tps65090_regulator_probe,
242         .remove         = __devexit_p(tps65090_regulator_remove),
243 };
244
245 static int __init tps65090_regulator_init(void)
246 {
247         return platform_driver_register(&tps65090_regulator_driver);
248 }
249 subsys_initcall(tps65090_regulator_init);
250
251 static void __exit tps65090_regulator_exit(void)
252 {
253         platform_driver_unregister(&tps65090_regulator_driver);
254 }
255 module_exit(tps65090_regulator_exit);
256
257 MODULE_DESCRIPTION("tps65090 regulator driver");
258 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
259 MODULE_LICENSE("GPL v2");