regulator: tps65090: align driver with mainline
[linux-3.10.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/init.h>
21 #include <linux/gpio.h>
22 #include <linux/of_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/regulator/of_regulator.h>
29 #include <linux/mfd/tps65090.h>
30
31 struct tps65090_regulator {
32         struct device           *dev;
33         struct regulator_desc   *desc;
34         struct regulator_dev    *rdev;
35
36         int                     wait_timeout_us;
37 };
38
39 static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
40 {
41         return rdev_get_dev(rdev)->parent;
42 }
43
44 static inline bool is_dcdc(int id)
45 {
46         switch (id) {
47         case TPS65090_REGULATOR_DCDC1:
48         case TPS65090_REGULATOR_DCDC2:
49         case TPS65090_REGULATOR_DCDC3:
50                 return true;
51         default:
52                 return false;
53         }
54 }
55
56 static int tps65090_reg_enable(struct regulator_dev *rdev)
57 {
58         struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
59         struct device *parent = to_tps65090_dev(rdev);
60
61         /* Setup wait_time for current limited timeout, WTFET[1:0]@bits[3:2] */
62         if (ri->wait_timeout_us > 0) {
63                 int wait_timeout = ri->wait_timeout_us;
64                 int ret;
65                 u8 en_reg = ri->desc->enable_reg;
66
67                 if (wait_timeout <= 200)
68                         ret = tps65090_update_bits(parent, en_reg, 0xc, 0x0);
69                 else if (wait_timeout <= 800)
70                         ret = tps65090_update_bits(parent, en_reg, 0xc, 0x4);
71                 else if (wait_timeout <= 1600)
72                         ret = tps65090_update_bits(parent, en_reg, 0xc, 0x8);
73                 else
74                         ret = tps65090_update_bits(parent, en_reg, 0xc, 0xc);
75
76                 if (ret < 0) {
77                         dev_err(&rdev->dev, "Error updating reg 0x%x WTFET\n",
78                                 en_reg);
79                         return ret;
80                 }
81         }
82
83         return regulator_enable_regmap(rdev);
84 }
85
86 static int tps65090_config_ext_control(
87         struct tps65090_regulator *ri, bool enable)
88 {
89         int ret;
90         struct device *parent = ri->dev->parent;
91         unsigned int reg_en_reg = ri->desc->enable_reg;
92
93         if (enable)
94                 ret = tps65090_set_bits(parent, reg_en_reg, 1);
95         else
96                 ret =  tps65090_clr_bits(parent, reg_en_reg, 1);
97         if (ret < 0)
98                 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
99         return ret;
100 }
101
102 static int tps65090_regulator_disable_ext_control(
103                 struct tps65090_regulator *ri,
104                 struct tps65090_regulator_plat_data *tps_pdata)
105 {
106         int ret;
107         struct device *parent = ri->dev->parent;
108         unsigned int reg_en_reg = ri->desc->enable_reg;
109
110         /*
111          * First enable output for internal control if require.
112          * And then disable external control.
113          */
114         if (tps_pdata->reg_init_data->constraints.always_on ||
115                         tps_pdata->reg_init_data->constraints.boot_on) {
116                 ret =  tps65090_set_bits(parent, reg_en_reg, 0);
117                 if (ret < 0) {
118                         dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
119                         return ret;
120                 }
121         }
122         return tps65090_config_ext_control(ri, false);
123 }
124
125 static void tps65090_configure_regulator_config(
126                 struct tps65090_regulator_plat_data *tps_pdata,
127                 struct regulator_config *config)
128 {
129         if (gpio_is_valid(tps_pdata->gpio)) {
130                 int gpio_flag = GPIOF_OUT_INIT_LOW;
131
132                 if (tps_pdata->reg_init_data->constraints.always_on ||
133                                 tps_pdata->reg_init_data->constraints.boot_on)
134                         gpio_flag = GPIOF_OUT_INIT_HIGH;
135
136                 config->ena_gpio = tps_pdata->gpio;
137                 config->ena_gpio_flags = gpio_flag;
138         }
139 }
140
141 static struct regulator_ops tps65090_ext_control_ops = {
142 };
143
144 static struct regulator_ops tps65090_reg_contol_ops = {
145         .enable         = tps65090_reg_enable,
146         .disable        = regulator_disable_regmap,
147         .is_enabled     = regulator_is_enabled_regmap,
148 };
149
150 static struct regulator_ops tps65090_ldo_ops = {
151 };
152
153 #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops)   \
154 {                                                       \
155         .name = "TPS65090-RAILS-"#_id,                  \
156         .supply_name = _sname,                          \
157         .id = TPS65090_REGULATOR_##_id,                 \
158         .ops = &_ops,                                   \
159         .enable_reg = _en_reg,                          \
160         .enable_mask = BIT(0),                          \
161         .type = REGULATOR_VOLTAGE,                      \
162         .owner = THIS_MODULE,                           \
163 }
164
165 static struct regulator_desc tps65090_regulator_desc[] = {
166         tps65090_REG_DESC(DCDC1, "vsys1",   0x0C, tps65090_reg_contol_ops),
167         tps65090_REG_DESC(DCDC2, "vsys2",   0x0D, tps65090_reg_contol_ops),
168         tps65090_REG_DESC(DCDC3, "vsys3",   0x0E, tps65090_reg_contol_ops),
169         tps65090_REG_DESC(FET1,  "infet1",  0x0F, tps65090_reg_contol_ops),
170         tps65090_REG_DESC(FET2,  "infet2",  0x10, tps65090_reg_contol_ops),
171         tps65090_REG_DESC(FET3,  "infet3",  0x11, tps65090_reg_contol_ops),
172         tps65090_REG_DESC(FET4,  "infet4",  0x12, tps65090_reg_contol_ops),
173         tps65090_REG_DESC(FET5,  "infet5",  0x13, tps65090_reg_contol_ops),
174         tps65090_REG_DESC(FET6,  "infet6",  0x14, tps65090_reg_contol_ops),
175         tps65090_REG_DESC(FET7,  "infet7",  0x15, tps65090_reg_contol_ops),
176         tps65090_REG_DESC(LDO1,  "vsys-l1", 0,    tps65090_ldo_ops),
177         tps65090_REG_DESC(LDO2,  "vsys-l2", 0,    tps65090_ldo_ops),
178 };
179
180 #ifdef CONFIG_OF
181 static struct of_regulator_match tps65090_matches[] = {
182         { .name = "dcdc1", },
183         { .name = "dcdc2", },
184         { .name = "dcdc3", },
185         { .name = "fet1",  },
186         { .name = "fet2",  },
187         { .name = "fet3",  },
188         { .name = "fet4",  },
189         { .name = "fet5",  },
190         { .name = "fet6",  },
191         { .name = "fet7",  },
192         { .name = "ldo1",  },
193         { .name = "ldo2",  },
194 };
195
196 static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
197                 struct platform_device *pdev,
198                 struct of_regulator_match **tps65090_reg_matches)
199 {
200         struct tps65090_platform_data *tps65090_pdata;
201         struct device_node *np = pdev->dev.parent->of_node;
202         struct device_node *regulators;
203         int idx = 0, ret;
204         struct tps65090_regulator_plat_data *reg_pdata;
205
206         tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
207                                 GFP_KERNEL);
208         if (!tps65090_pdata) {
209                 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
210                 return ERR_PTR(-ENOMEM);
211         }
212
213         reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
214                                 sizeof(*reg_pdata), GFP_KERNEL);
215         if (!reg_pdata) {
216                 dev_err(&pdev->dev, "Memory alloc for reg_pdata failed\n");
217                 return ERR_PTR(-ENOMEM);
218         }
219
220         regulators = of_find_node_by_name(np, "regulators");
221         if (!regulators) {
222                 dev_err(&pdev->dev, "regulator node not found\n");
223                 return ERR_PTR(-ENODEV);
224         }
225
226         ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
227                         ARRAY_SIZE(tps65090_matches));
228         if (ret < 0) {
229                 dev_err(&pdev->dev,
230                         "Error parsing regulator init data: %d\n", ret);
231                 return ERR_PTR(ret);
232         }
233
234         *tps65090_reg_matches = tps65090_matches;
235         for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
236                 struct regulator_init_data *ri_data;
237                 struct tps65090_regulator_plat_data *rpdata;
238
239                 rpdata = &reg_pdata[idx];
240                 ri_data = tps65090_matches[idx].init_data;
241                 if (!ri_data || !tps65090_matches[idx].of_node)
242                         continue;
243
244                 rpdata->reg_init_data = ri_data;
245                 rpdata->enable_ext_control = of_property_read_bool(
246                                         tps65090_matches[idx].of_node,
247                                         "ti,enable-ext-control");
248                 if (rpdata->enable_ext_control)
249                         rpdata->gpio = of_get_named_gpio(np,
250                                         "dcdc-ext-control-gpios", 0);
251
252                 tps65090_pdata->reg_pdata[idx] = rpdata;
253         }
254         return tps65090_pdata;
255 }
256 #else
257 static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
258                         struct platform_device *pdev,
259                         struct of_regulator_match **tps65090_reg_matches)
260 {
261         *tps65090_reg_matches = NULL;
262         return NULL;
263 }
264 #endif
265
266 static int tps65090_regulator_probe(struct platform_device *pdev)
267 {
268         struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
269         struct tps65090_regulator *ri = NULL;
270         struct regulator_config config = { };
271         struct regulator_dev *rdev;
272         struct tps65090_regulator_plat_data *tps_pdata;
273         struct tps65090_regulator *pmic;
274         struct tps65090_platform_data *tps65090_pdata;
275         struct of_regulator_match *tps65090_reg_matches = NULL;
276         int num;
277         int ret;
278
279         dev_dbg(&pdev->dev, "Probing regulator\n");
280
281         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
282         if (!tps65090_pdata && tps65090_mfd->dev->of_node)
283                 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
284                                         &tps65090_reg_matches);
285         if (IS_ERR_OR_NULL(tps65090_pdata)) {
286                 dev_err(&pdev->dev, "Platform data missing\n");
287                 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
288         }
289
290         pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
291                         GFP_KERNEL);
292         if (!pmic) {
293                 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
294                 return -ENOMEM;
295         }
296
297         for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
298                 tps_pdata = tps65090_pdata->reg_pdata[num];
299
300                 ri = &pmic[num];
301                 ri->dev = &pdev->dev;
302                 ri->desc = &tps65090_regulator_desc[num];
303                 if (tps_pdata)
304                         ri->wait_timeout_us = tps_pdata->wait_timeout_us;
305
306                 /*
307                  * TPS5090 DCDC support the control from external digital input.
308                  * Configure it as per platform data.
309                  */
310                 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
311                         if (tps_pdata->enable_ext_control) {
312                                 tps65090_configure_regulator_config(
313                                                 tps_pdata, &config);
314                                 ri->desc->ops = &tps65090_ext_control_ops;
315                         } else {
316                                 ret = tps65090_regulator_disable_ext_control(
317                                                 ri, tps_pdata);
318                                 if (ret < 0) {
319                                         dev_err(&pdev->dev,
320                                                 "failed disable ext control\n");
321                                         goto scrub;
322                                 }
323                         }
324                 }
325
326                 config.dev = pdev->dev.parent;
327                 config.driver_data = ri;
328                 config.regmap = tps65090_mfd->rmap;
329                 if (tps_pdata)
330                         config.init_data = tps_pdata->reg_init_data;
331                 else
332                         config.init_data = NULL;
333                 if (tps65090_reg_matches)
334                         config.of_node = tps65090_reg_matches[num].of_node;
335                 else
336                         config.of_node = NULL;
337
338                 rdev = regulator_register(ri->desc, &config);
339                 if (IS_ERR(rdev)) {
340                         dev_err(&pdev->dev, "failed to register regulator %s\n",
341                                 ri->desc->name);
342                         ret = PTR_ERR(rdev);
343                         goto scrub;
344                 }
345                 ri->rdev = rdev;
346
347                 /* Enable external control if it is require */
348                 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
349                                 tps_pdata->enable_ext_control) {
350                         ret = tps65090_config_ext_control(ri, true);
351                         if (ret < 0) {
352                                 /* Increment num to get unregister rdev */
353                                 num++;
354                                 goto scrub;
355                         }
356                 }
357         }
358
359         platform_set_drvdata(pdev, pmic);
360         return 0;
361
362 scrub:
363         while (--num >= 0) {
364                 ri = &pmic[num];
365                 regulator_unregister(ri->rdev);
366         }
367         return ret;
368 }
369
370 static int tps65090_regulator_remove(struct platform_device *pdev)
371 {
372         struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
373         struct tps65090_regulator *ri;
374         int num;
375
376         for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) {
377                 ri = &pmic[num];
378                 regulator_unregister(ri->rdev);
379         }
380         return 0;
381 }
382
383 static struct platform_driver tps65090_regulator_driver = {
384         .driver = {
385                 .name   = "tps65090-pmic",
386                 .owner  = THIS_MODULE,
387         },
388         .probe          = tps65090_regulator_probe,
389         .remove         = tps65090_regulator_remove,
390 };
391
392 static int __init tps65090_regulator_init(void)
393 {
394         return platform_driver_register(&tps65090_regulator_driver);
395 }
396 subsys_initcall(tps65090_regulator_init);
397
398 static void __exit tps65090_regulator_exit(void)
399 {
400         platform_driver_unregister(&tps65090_regulator_driver);
401 }
402 module_exit(tps65090_regulator_exit);
403
404 MODULE_DESCRIPTION("tps65090 regulator driver");
405 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
406 MODULE_LICENSE("GPL v2");
407 MODULE_ALIAS("platform:tps65090-pmic");