perf: Do not export power_frequency, but power_start event
[linux-2.6.git] / drivers / regulator / ab8500.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License Terms: GNU General Public License v2
5  *
6  * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7  *
8  * AB8500 peripheral regulators
9  *
10  * AB8500 supports the following regulators,
11  * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT,
12  *        VAUX1/2/3, VANA
13  *
14  * for DB8500 cut 1.0 and previous versions of the silicon, all accesses
15  * to registers are through the DB8500 SPI. In cut 1.1 onwards, these
16  * accesses are through the DB8500 PRCMU I2C
17  *
18  */
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/ab8500.h>
24 #include <linux/mfd/abx500.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 #include <linux/regulator/ab8500.h>
28
29 /**
30  * struct ab8500_regulator_info - ab8500 regulator information
31  * @desc: regulator description
32  * @ab8500: ab8500 parent
33  * @regulator_dev: regulator device
34  * @max_uV: maximum voltage (for variable voltage supplies)
35  * @min_uV: minimum voltage (for variable voltage supplies)
36  * @fixed_uV: typical voltage (for fixed voltage supplies)
37  * @update_bank: bank to control on/off
38  * @update_reg: register to control on/off
39  * @mask: mask to enable/disable regulator
40  * @enable: bits to enable the regulator in normal(high power) mode
41  * @voltage_bank: bank to control regulator voltage
42  * @voltage_reg: register to control regulator voltage
43  * @voltage_mask: mask to control regulator voltage
44  * @supported_voltages: supported voltage table
45  * @voltages_len: number of supported voltages for the regulator
46  */
47 struct ab8500_regulator_info {
48         struct device           *dev;
49         struct regulator_desc   desc;
50         struct ab8500           *ab8500;
51         struct regulator_dev    *regulator;
52         int max_uV;
53         int min_uV;
54         int fixed_uV;
55         u8 update_bank;
56         u8 update_reg;
57         u8 mask;
58         u8 enable;
59         u8 voltage_bank;
60         u8 voltage_reg;
61         u8 voltage_mask;
62         int const *supported_voltages;
63         int voltages_len;
64 };
65
66 /* voltage tables for the vauxn/vintcore supplies */
67 static const int ldo_vauxn_voltages[] = {
68         1100000,
69         1200000,
70         1300000,
71         1400000,
72         1500000,
73         1800000,
74         1850000,
75         1900000,
76         2500000,
77         2650000,
78         2700000,
79         2750000,
80         2800000,
81         2900000,
82         3000000,
83         3300000,
84 };
85
86 static const int ldo_vintcore_voltages[] = {
87         1200000,
88         1225000,
89         1250000,
90         1275000,
91         1300000,
92         1325000,
93         1350000,
94 };
95
96 static int ab8500_regulator_enable(struct regulator_dev *rdev)
97 {
98         int regulator_id, ret;
99         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
100
101         regulator_id = rdev_get_id(rdev);
102         if (regulator_id >= AB8500_NUM_REGULATORS)
103                 return -EINVAL;
104
105         ret = abx500_mask_and_set_register_interruptible(info->dev,
106                 info->update_bank, info->update_reg, info->mask, info->enable);
107         if (ret < 0)
108                 dev_err(rdev_get_dev(rdev),
109                         "couldn't set enable bits for regulator\n");
110         return ret;
111 }
112
113 static int ab8500_regulator_disable(struct regulator_dev *rdev)
114 {
115         int regulator_id, ret;
116         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
117
118         regulator_id = rdev_get_id(rdev);
119         if (regulator_id >= AB8500_NUM_REGULATORS)
120                 return -EINVAL;
121
122         ret = abx500_mask_and_set_register_interruptible(info->dev,
123                 info->update_bank, info->update_reg, info->mask, 0x0);
124         if (ret < 0)
125                 dev_err(rdev_get_dev(rdev),
126                         "couldn't set disable bits for regulator\n");
127         return ret;
128 }
129
130 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
131 {
132         int regulator_id, ret;
133         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
134         u8 value;
135
136         regulator_id = rdev_get_id(rdev);
137         if (regulator_id >= AB8500_NUM_REGULATORS)
138                 return -EINVAL;
139
140         ret = abx500_get_register_interruptible(info->dev,
141                 info->update_bank, info->update_reg, &value);
142         if (ret < 0) {
143                 dev_err(rdev_get_dev(rdev),
144                         "couldn't read 0x%x register\n", info->update_reg);
145                 return ret;
146         }
147
148         if (value & info->mask)
149                 return true;
150         else
151                 return false;
152 }
153
154 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
155 {
156         int regulator_id;
157         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
158
159         regulator_id = rdev_get_id(rdev);
160         if (regulator_id >= AB8500_NUM_REGULATORS)
161                 return -EINVAL;
162
163         /* return the uV for the fixed regulators */
164         if (info->fixed_uV)
165                 return info->fixed_uV;
166
167         if (selector >= info->voltages_len)
168                 return -EINVAL;
169
170         return info->supported_voltages[selector];
171 }
172
173 static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
174 {
175         int regulator_id, ret;
176         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
177         u8 value;
178
179         regulator_id = rdev_get_id(rdev);
180         if (regulator_id >= AB8500_NUM_REGULATORS)
181                 return -EINVAL;
182
183         ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
184                 info->voltage_reg, &value);
185         if (ret < 0) {
186                 dev_err(rdev_get_dev(rdev),
187                         "couldn't read voltage reg for regulator\n");
188                 return ret;
189         }
190
191         /* vintcore has a different layout */
192         value &= info->voltage_mask;
193         if (regulator_id == AB8500_LDO_INTCORE)
194                 ret = info->supported_voltages[value >> 0x3];
195         else
196                 ret = info->supported_voltages[value];
197
198         return ret;
199 }
200
201 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
202                 int min_uV, int max_uV)
203 {
204         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
205         int i;
206
207         /* check the supported voltage */
208         for (i = 0; i < info->voltages_len; i++) {
209                 if ((info->supported_voltages[i] >= min_uV) &&
210                     (info->supported_voltages[i] <= max_uV))
211                         return i;
212         }
213
214         return -EINVAL;
215 }
216
217 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
218                 int min_uV, int max_uV)
219 {
220         int regulator_id, ret;
221         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
222
223         regulator_id = rdev_get_id(rdev);
224         if (regulator_id >= AB8500_NUM_REGULATORS)
225                 return -EINVAL;
226
227         /* get the appropriate voltages within the range */
228         ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
229         if (ret < 0) {
230                 dev_err(rdev_get_dev(rdev),
231                                 "couldn't get best voltage for regulator\n");
232                 return ret;
233         }
234
235         /* set the registers for the request */
236         ret = abx500_mask_and_set_register_interruptible(info->dev,
237                 info->voltage_bank, info->voltage_reg,
238                 info->voltage_mask, (u8)ret);
239         if (ret < 0)
240                 dev_err(rdev_get_dev(rdev),
241                 "couldn't set voltage reg for regulator\n");
242
243         return ret;
244 }
245
246 static struct regulator_ops ab8500_regulator_ops = {
247         .enable         = ab8500_regulator_enable,
248         .disable        = ab8500_regulator_disable,
249         .is_enabled     = ab8500_regulator_is_enabled,
250         .get_voltage    = ab8500_regulator_get_voltage,
251         .set_voltage    = ab8500_regulator_set_voltage,
252         .list_voltage   = ab8500_list_voltage,
253 };
254
255 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
256 {
257         int regulator_id;
258         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
259
260         regulator_id = rdev_get_id(rdev);
261         if (regulator_id >= AB8500_NUM_REGULATORS)
262                 return -EINVAL;
263
264         return info->fixed_uV;
265 }
266
267 static struct regulator_ops ab8500_ldo_fixed_ops = {
268         .enable         = ab8500_regulator_enable,
269         .disable        = ab8500_regulator_disable,
270         .is_enabled     = ab8500_regulator_is_enabled,
271         .get_voltage    = ab8500_fixed_get_voltage,
272         .list_voltage   = ab8500_list_voltage,
273 };
274
275 #define AB8500_LDO(_id, min, max, bank, reg, reg_mask,          \
276                 reg_enable, volt_bank, volt_reg, volt_mask,     \
277                 voltages, len_volts)                            \
278 {                                                               \
279         .desc   = {                                             \
280                 .name   = "LDO-" #_id,                          \
281                 .ops    = &ab8500_regulator_ops,                \
282                 .type   = REGULATOR_VOLTAGE,                    \
283                 .id     = AB8500_LDO_##_id,                     \
284                 .owner  = THIS_MODULE,                          \
285         },                                                      \
286         .min_uV         = (min) * 1000,                         \
287         .max_uV         = (max) * 1000,                         \
288         .update_bank    = bank,                                 \
289         .update_reg     = reg,                                  \
290         .mask           = reg_mask,                             \
291         .enable         = reg_enable,                           \
292         .voltage_bank   = volt_bank,                            \
293         .voltage_reg    = volt_reg,                             \
294         .voltage_mask   = volt_mask,                            \
295         .supported_voltages = voltages,                         \
296         .voltages_len   = len_volts,                            \
297         .fixed_uV       = 0,                                    \
298 }
299
300 #define AB8500_FIXED_LDO(_id, fixed, bank, reg,         \
301                         reg_mask, reg_enable)           \
302 {                                                       \
303         .desc   = {                                     \
304                 .name   = "LDO-" #_id,                  \
305                 .ops    = &ab8500_ldo_fixed_ops,        \
306                 .type   = REGULATOR_VOLTAGE,            \
307                 .id     = AB8500_LDO_##_id,             \
308                 .owner  = THIS_MODULE,                  \
309         },                                              \
310         .fixed_uV       = fixed * 1000,                 \
311         .update_bank    = bank,                         \
312         .update_reg     = reg,                          \
313         .mask           = reg_mask,                     \
314         .enable         = reg_enable,                   \
315 }
316
317 static struct ab8500_regulator_info ab8500_regulator_info[] = {
318         /*
319          * Variable Voltage LDOs
320          * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
321          *      volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
322          *      num supported volts
323          */
324         AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
325                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
326         AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
327                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
328         AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
329                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
330         AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x4, 0x4, 0x03, 0x80, 0x38,
331                 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
332
333         /*
334          * Fixed Voltage LDOs
335          *               name,  o/p uV, ctrl bank, ctrl reg, enable, disable
336          */
337         AB8500_FIXED_LDO(TVOUT,   2000, 0x03,      0x80,     0x2,    0x2),
338         AB8500_FIXED_LDO(AUDIO,   2000, 0x03,      0x83,     0x2,    0x2),
339         AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03,      0x83,     0x4,    0x4),
340         AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03,      0x83,     0x8,    0x8),
341         AB8500_FIXED_LDO(DMIC,    1800, 0x03,      0x83,     0x10,   0x10),
342         AB8500_FIXED_LDO(ANA,     1200, 0x03,      0x83,     0xc,    0x4),
343 };
344
345 static inline struct ab8500_regulator_info *find_regulator_info(int id)
346 {
347         struct ab8500_regulator_info *info;
348         int i;
349
350         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
351                 info = &ab8500_regulator_info[i];
352                 if (info->desc.id == id)
353                         return info;
354         }
355         return NULL;
356 }
357
358 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
359 {
360         struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
361         struct ab8500_platform_data *pdata;
362         int i, err;
363
364         if (!ab8500) {
365                 dev_err(&pdev->dev, "null mfd parent\n");
366                 return -EINVAL;
367         }
368         pdata = dev_get_platdata(ab8500->dev);
369
370         /* register all regulators */
371         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
372                 struct ab8500_regulator_info *info = NULL;
373
374                 /* assign per-regulator data */
375                 info = &ab8500_regulator_info[i];
376                 info->dev = &pdev->dev;
377                 info->ab8500 = ab8500;
378
379                 info->regulator = regulator_register(&info->desc, &pdev->dev,
380                                 pdata->regulator[i], info);
381                 if (IS_ERR(info->regulator)) {
382                         err = PTR_ERR(info->regulator);
383                         dev_err(&pdev->dev, "failed to register regulator %s\n",
384                                         info->desc.name);
385                         /* when we fail, un-register all earlier regulators */
386                         while (--i >= 0) {
387                                 info = &ab8500_regulator_info[i];
388                                 regulator_unregister(info->regulator);
389                         }
390                         return err;
391                 }
392         }
393
394         return 0;
395 }
396
397 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
398 {
399         int i;
400
401         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
402                 struct ab8500_regulator_info *info = NULL;
403                 info = &ab8500_regulator_info[i];
404                 regulator_unregister(info->regulator);
405         }
406
407         return 0;
408 }
409
410 static struct platform_driver ab8500_regulator_driver = {
411         .probe = ab8500_regulator_probe,
412         .remove = __devexit_p(ab8500_regulator_remove),
413         .driver         = {
414                 .name   = "ab8500-regulator",
415                 .owner  = THIS_MODULE,
416         },
417 };
418
419 static int __init ab8500_regulator_init(void)
420 {
421         int ret;
422
423         ret = platform_driver_register(&ab8500_regulator_driver);
424         if (ret != 0)
425                 pr_err("Failed to register ab8500 regulator: %d\n", ret);
426
427         return ret;
428 }
429 subsys_initcall(ab8500_regulator_init);
430
431 static void __exit ab8500_regulator_exit(void)
432 {
433         platform_driver_unregister(&ab8500_regulator_driver);
434 }
435 module_exit(ab8500_regulator_exit);
436
437 MODULE_LICENSE("GPL v2");
438 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
439 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
440 MODULE_ALIAS("platform:ab8500-regulator");