regulator: tps80031: adjust tolerance if req minuV > dev minuV
[linux-2.6.git] / drivers / regulator / tps65090-regulator.c
1 /*
2  * drivers/regulator/tps65090-regulator.c
3  *
4  * Regulator driver for tps65090 power management chip.
5  *
6  * Copyright (C) 2012 NVIDIA Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/mfd/tps65090.h>
33 #include <linux/regulator/tps65090-regulator.h>
34
35 struct tps65090_regulator {
36         int             id;
37         /* Regulator register address.*/
38         u8              reg_en_reg;
39         u8              en_bit;
40
41         /* used by regulator core */
42         struct regulator_desc   desc;
43
44         /* Device */
45         struct device           *dev;
46 };
47
48
49 static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
50 {
51         return rdev_get_dev(rdev)->parent->parent;
52 }
53 static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
54 {
55         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
56         struct device *parent = to_tps65090_dev(rdev);
57         uint8_t control;
58         int ret;
59
60         ret = tps65090_read(parent, ri->reg_en_reg, &control);
61         if (ret < 0) {
62                 dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
63                         ri->reg_en_reg);
64                 return ret;
65         }
66         return (((control >> ri->en_bit) & 1) == 1);
67 }
68
69 static int tps65090_reg_enable(struct regulator_dev *rdev)
70 {
71         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
72         struct device *parent = to_tps65090_dev(rdev);
73         int ret;
74
75         ret = tps65090_set_bits(parent, ri->reg_en_reg, ri->en_bit);
76         if (ret < 0)
77                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
78                         ri->reg_en_reg);
79         return ret;
80 }
81
82 static int tps65090_reg_disable(struct regulator_dev *rdev)
83 {
84         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
85         struct device *parent = to_tps65090_dev(rdev);
86         int ret;
87
88         ret = tps65090_clr_bits(parent, ri->reg_en_reg, ri->en_bit);
89         if (ret < 0)
90                 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
91                         ri->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 #define tps65090_REG(_id, _en_reg, _en_bit, _ops)       \
103 {                                                       \
104         .reg_en_reg     = _en_reg,                      \
105         .en_bit         = _en_bit,                      \
106         .id             = TPS65090_ID_##_id,            \
107         .desc = {                                       \
108                 .name = tps65090_rails(_id),            \
109                 .id = TPS65090_ID_##_id,                \
110                 .ops = &_ops,                           \
111                 .type = REGULATOR_VOLTAGE,              \
112                 .owner = THIS_MODULE,                   \
113         },                                              \
114 }
115
116 static struct tps65090_regulator TPS65090_regulator[] = {
117         tps65090_REG(DCDC1, 12, 0, tps65090_ops),
118         tps65090_REG(DCDC2, 13, 0, tps65090_ops),
119         tps65090_REG(DCDC3, 14, 0, tps65090_ops),
120         tps65090_REG(FET1,  15, 0, tps65090_ops),
121         tps65090_REG(FET2,  16, 0, tps65090_ops),
122         tps65090_REG(FET3,  17, 0, tps65090_ops),
123         tps65090_REG(FET4,  18, 0, tps65090_ops),
124         tps65090_REG(FET5,  19, 0, tps65090_ops),
125         tps65090_REG(FET6,  20, 0, tps65090_ops),
126         tps65090_REG(FET7,  21, 0, tps65090_ops),
127 };
128 static inline struct tps65090_regulator *find_regulator_info(int id)
129 {
130         struct tps65090_regulator *ri;
131         int i;
132
133         for (i = 0; i < ARRAY_SIZE(TPS65090_regulator); i++) {
134                 ri = &TPS65090_regulator[i];
135                 if (ri->desc.id == id)
136                         return ri;
137         }
138         return NULL;
139 }
140 static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
141 {
142         struct tps65090_regulator *ri = NULL;
143         struct regulator_dev *rdev;
144         struct tps65090_regulator_platform_data *tps_pdata;
145         int id = pdev->id;
146
147         dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
148
149         ri = find_regulator_info(id);
150         if (ri == NULL) {
151                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
152                 return -EINVAL;
153         }
154         tps_pdata = pdev->dev.platform_data;
155         ri->dev = &pdev->dev;
156
157         rdev = regulator_register(&ri->desc, &pdev->dev,
158                                 &tps_pdata->regulator, ri);
159         if (IS_ERR_OR_NULL(rdev)) {
160                 dev_err(&pdev->dev, "failed to register regulator %s\n",
161                                 ri->desc.name);
162                 return PTR_ERR(rdev);
163         }
164
165         platform_set_drvdata(pdev, rdev);
166         return 0;
167 }
168
169 static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
170 {
171         struct regulator_dev *rdev = platform_get_drvdata(pdev);
172
173         regulator_unregister(rdev);
174         return 0;
175 }
176
177 static struct platform_driver tps65090_regulator_driver = {
178         .driver = {
179                 .name   = "tps65090-regulator",
180                 .owner  = THIS_MODULE,
181         },
182         .probe          = tps65090_regulator_probe,
183         .remove         = __devexit_p(tps65090_regulator_remove),
184 };
185
186 static int __init tps65090_regulator_init(void)
187 {
188         return platform_driver_register(&tps65090_regulator_driver);
189 }
190 subsys_initcall(tps65090_regulator_init);
191
192 static void __exit tps65090_regulator_exit(void)
193 {
194         platform_driver_unregister(&tps65090_regulator_driver);
195 }
196 module_exit(tps65090_regulator_exit);
197
198 MODULE_DESCRIPTION("tps65090 regulator driver");
199 MODULE_ALIAS("platform:tps65090-regulator");
200 MODULE_LICENSE("GPL");