regulator: wm8994: Convert wm8994_ldo1_ops to regulator_[list|map]_voltage_linear
[linux-2.6-microblaze.git] / drivers / regulator / wm8994-regulator.c
1 /*
2  * wm8994-regulator.c  --  Regulator driver for the WM8994
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23
24 #include <linux/mfd/wm8994/core.h>
25 #include <linux/mfd/wm8994/registers.h>
26 #include <linux/mfd/wm8994/pdata.h>
27
28 struct wm8994_ldo {
29         int enable;
30         bool is_enabled;
31         struct regulator_dev *regulator;
32         struct wm8994 *wm8994;
33 };
34
35 #define WM8994_LDO1_MAX_SELECTOR 0x7
36 #define WM8994_LDO2_MAX_SELECTOR 0x3
37
38 static int wm8994_ldo_enable(struct regulator_dev *rdev)
39 {
40         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
41
42         /* If we have no soft control assume that the LDO is always enabled. */
43         if (!ldo->enable)
44                 return 0;
45
46         gpio_set_value_cansleep(ldo->enable, 1);
47         ldo->is_enabled = true;
48
49         return 0;
50 }
51
52 static int wm8994_ldo_disable(struct regulator_dev *rdev)
53 {
54         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
55
56         /* If we have no soft control assume that the LDO is always enabled. */
57         if (!ldo->enable)
58                 return -EINVAL;
59
60         gpio_set_value_cansleep(ldo->enable, 0);
61         ldo->is_enabled = false;
62
63         return 0;
64 }
65
66 static int wm8994_ldo_is_enabled(struct regulator_dev *rdev)
67 {
68         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
69
70         return ldo->is_enabled;
71 }
72
73 static int wm8994_ldo_enable_time(struct regulator_dev *rdev)
74 {
75         /* 3ms is fairly conservative but this shouldn't be too performance
76          * critical; can be tweaked per-system if required. */
77         return 3000;
78 }
79
80 static struct regulator_ops wm8994_ldo1_ops = {
81         .enable = wm8994_ldo_enable,
82         .disable = wm8994_ldo_disable,
83         .is_enabled = wm8994_ldo_is_enabled,
84         .enable_time = wm8994_ldo_enable_time,
85
86         .list_voltage = regulator_list_voltage_linear,
87         .map_voltage = regulator_map_voltage_linear,
88         .get_voltage_sel = regulator_get_voltage_sel_regmap,
89         .set_voltage_sel = regulator_set_voltage_sel_regmap,
90 };
91
92 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
93                                     unsigned int selector)
94 {
95         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
96
97         if (selector > WM8994_LDO2_MAX_SELECTOR)
98                 return -EINVAL;
99
100         switch (ldo->wm8994->type) {
101         case WM8994:
102                 return (selector * 100000) + 900000;
103         case WM8958:
104                 return (selector * 100000) + 1000000;
105         case WM1811:
106                 switch (selector) {
107                 case 0:
108                         return -EINVAL;
109                 default:
110                         return (selector * 100000) + 950000;
111                 }
112                 break;
113         default:
114                 return -EINVAL;
115         }
116 }
117
118 static struct regulator_ops wm8994_ldo2_ops = {
119         .enable = wm8994_ldo_enable,
120         .disable = wm8994_ldo_disable,
121         .is_enabled = wm8994_ldo_is_enabled,
122         .enable_time = wm8994_ldo_enable_time,
123
124         .list_voltage = wm8994_ldo2_list_voltage,
125         .get_voltage_sel = regulator_get_voltage_sel_regmap,
126         .set_voltage_sel = regulator_set_voltage_sel_regmap,
127 };
128
129 static const struct regulator_desc wm8994_ldo_desc[] = {
130         {
131                 .name = "LDO1",
132                 .id = 1,
133                 .type = REGULATOR_VOLTAGE,
134                 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
135                 .vsel_reg = WM8994_LDO_1,
136                 .vsel_mask = WM8994_LDO1_VSEL_MASK,
137                 .ops = &wm8994_ldo1_ops,
138                 .min_uV = 2400000,
139                 .uV_step = 100000,
140                 .owner = THIS_MODULE,
141         },
142         {
143                 .name = "LDO2",
144                 .id = 2,
145                 .type = REGULATOR_VOLTAGE,
146                 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
147                 .vsel_reg = WM8994_LDO_2,
148                 .vsel_mask = WM8994_LDO2_VSEL_MASK,
149                 .ops = &wm8994_ldo2_ops,
150                 .owner = THIS_MODULE,
151         },
152 };
153
154 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
155 {
156         struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
157         struct wm8994_pdata *pdata = wm8994->dev->platform_data;
158         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
159         struct regulator_config config = { };
160         struct wm8994_ldo *ldo;
161         int ret;
162
163         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
164
165         ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
166         if (ldo == NULL) {
167                 dev_err(&pdev->dev, "Unable to allocate private data\n");
168                 return -ENOMEM;
169         }
170
171         ldo->wm8994 = wm8994;
172
173         if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
174                 ldo->enable = pdata->ldo[id].enable;
175
176                 ret = gpio_request_one(ldo->enable, 0, "WM8994 LDO enable");
177                 if (ret < 0) {
178                         dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
179                                 ret);
180                         goto err;
181                 }
182         } else
183                 ldo->is_enabled = true;
184
185         config.dev = wm8994->dev;
186         config.driver_data = ldo;
187         config.regmap = wm8994->regmap;
188         if (pdata)
189                 config.init_data = pdata->ldo[id].init_data;
190
191         ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
192         if (IS_ERR(ldo->regulator)) {
193                 ret = PTR_ERR(ldo->regulator);
194                 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
195                         id + 1, ret);
196                 goto err_gpio;
197         }
198
199         platform_set_drvdata(pdev, ldo);
200
201         return 0;
202
203 err_gpio:
204         if (gpio_is_valid(ldo->enable))
205                 gpio_free(ldo->enable);
206 err:
207         return ret;
208 }
209
210 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
211 {
212         struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
213
214         platform_set_drvdata(pdev, NULL);
215
216         regulator_unregister(ldo->regulator);
217         if (gpio_is_valid(ldo->enable))
218                 gpio_free(ldo->enable);
219
220         return 0;
221 }
222
223 static struct platform_driver wm8994_ldo_driver = {
224         .probe = wm8994_ldo_probe,
225         .remove = __devexit_p(wm8994_ldo_remove),
226         .driver         = {
227                 .name   = "wm8994-ldo",
228                 .owner  = THIS_MODULE,
229         },
230 };
231
232 static int __init wm8994_ldo_init(void)
233 {
234         int ret;
235
236         ret = platform_driver_register(&wm8994_ldo_driver);
237         if (ret != 0)
238                 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
239
240         return ret;
241 }
242 subsys_initcall(wm8994_ldo_init);
243
244 static void __exit wm8994_ldo_exit(void)
245 {
246         platform_driver_unregister(&wm8994_ldo_driver);
247 }
248 module_exit(wm8994_ldo_exit);
249
250 /* Module information */
251 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
252 MODULE_DESCRIPTION("WM8994 LDO driver");
253 MODULE_LICENSE("GPL");
254 MODULE_ALIAS("platform:wm8994-ldo");