2f4ec0facef133bec4ae5b3636ed36bff9391976
[firefly-linux-kernel-4.4.55.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                                         unsigned *selector)
220 {
221         int regulator_id, ret;
222         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
223
224         regulator_id = rdev_get_id(rdev);
225         if (regulator_id >= AB8500_NUM_REGULATORS)
226                 return -EINVAL;
227
228         /* get the appropriate voltages within the range */
229         ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
230         if (ret < 0) {
231                 dev_err(rdev_get_dev(rdev),
232                                 "couldn't get best voltage for regulator\n");
233                 return ret;
234         }
235
236         *selector = ret;
237
238         /* set the registers for the request */
239         ret = abx500_mask_and_set_register_interruptible(info->dev,
240                 info->voltage_bank, info->voltage_reg,
241                 info->voltage_mask, (u8)ret);
242         if (ret < 0)
243                 dev_err(rdev_get_dev(rdev),
244                 "couldn't set voltage reg for regulator\n");
245
246         return ret;
247 }
248
249 static struct regulator_ops ab8500_regulator_ops = {
250         .enable         = ab8500_regulator_enable,
251         .disable        = ab8500_regulator_disable,
252         .is_enabled     = ab8500_regulator_is_enabled,
253         .get_voltage    = ab8500_regulator_get_voltage,
254         .set_voltage    = ab8500_regulator_set_voltage,
255         .list_voltage   = ab8500_list_voltage,
256 };
257
258 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
259 {
260         int regulator_id;
261         struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
262
263         regulator_id = rdev_get_id(rdev);
264         if (regulator_id >= AB8500_NUM_REGULATORS)
265                 return -EINVAL;
266
267         return info->fixed_uV;
268 }
269
270 static struct regulator_ops ab8500_ldo_fixed_ops = {
271         .enable         = ab8500_regulator_enable,
272         .disable        = ab8500_regulator_disable,
273         .is_enabled     = ab8500_regulator_is_enabled,
274         .get_voltage    = ab8500_fixed_get_voltage,
275         .list_voltage   = ab8500_list_voltage,
276 };
277
278 #define AB8500_LDO(_id, min, max, bank, reg, reg_mask,          \
279                 reg_enable, volt_bank, volt_reg, volt_mask,     \
280                 voltages, len_volts)                            \
281 {                                                               \
282         .desc   = {                                             \
283                 .name   = "LDO-" #_id,                          \
284                 .ops    = &ab8500_regulator_ops,                \
285                 .type   = REGULATOR_VOLTAGE,                    \
286                 .id     = AB8500_LDO_##_id,                     \
287                 .owner  = THIS_MODULE,                          \
288         },                                                      \
289         .min_uV         = (min) * 1000,                         \
290         .max_uV         = (max) * 1000,                         \
291         .update_bank    = bank,                                 \
292         .update_reg     = reg,                                  \
293         .mask           = reg_mask,                             \
294         .enable         = reg_enable,                           \
295         .voltage_bank   = volt_bank,                            \
296         .voltage_reg    = volt_reg,                             \
297         .voltage_mask   = volt_mask,                            \
298         .supported_voltages = voltages,                         \
299         .voltages_len   = len_volts,                            \
300         .fixed_uV       = 0,                                    \
301 }
302
303 #define AB8500_FIXED_LDO(_id, fixed, bank, reg,         \
304                         reg_mask, reg_enable)           \
305 {                                                       \
306         .desc   = {                                     \
307                 .name   = "LDO-" #_id,                  \
308                 .ops    = &ab8500_ldo_fixed_ops,        \
309                 .type   = REGULATOR_VOLTAGE,            \
310                 .id     = AB8500_LDO_##_id,             \
311                 .owner  = THIS_MODULE,                  \
312         },                                              \
313         .fixed_uV       = fixed * 1000,                 \
314         .update_bank    = bank,                         \
315         .update_reg     = reg,                          \
316         .mask           = reg_mask,                     \
317         .enable         = reg_enable,                   \
318 }
319
320 static struct ab8500_regulator_info ab8500_regulator_info[] = {
321         /*
322          * Variable Voltage LDOs
323          * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
324          *      volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
325          *      num supported volts
326          */
327         AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
328                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
329         AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
330                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
331         AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
332                         ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
333         AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x4, 0x4, 0x03, 0x80, 0x38,
334                 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
335
336         /*
337          * Fixed Voltage LDOs
338          *               name,  o/p uV, ctrl bank, ctrl reg, enable, disable
339          */
340         AB8500_FIXED_LDO(TVOUT,   2000, 0x03,      0x80,     0x2,    0x2),
341         AB8500_FIXED_LDO(AUDIO,   2000, 0x03,      0x83,     0x2,    0x2),
342         AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03,      0x83,     0x4,    0x4),
343         AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03,      0x83,     0x8,    0x8),
344         AB8500_FIXED_LDO(DMIC,    1800, 0x03,      0x83,     0x10,   0x10),
345         AB8500_FIXED_LDO(ANA,     1200, 0x03,      0x83,     0xc,    0x4),
346 };
347
348 static inline struct ab8500_regulator_info *find_regulator_info(int id)
349 {
350         struct ab8500_regulator_info *info;
351         int i;
352
353         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
354                 info = &ab8500_regulator_info[i];
355                 if (info->desc.id == id)
356                         return info;
357         }
358         return NULL;
359 }
360
361 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
362 {
363         struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
364         struct ab8500_platform_data *pdata;
365         int i, err;
366
367         if (!ab8500) {
368                 dev_err(&pdev->dev, "null mfd parent\n");
369                 return -EINVAL;
370         }
371         pdata = dev_get_platdata(ab8500->dev);
372
373         /* register all regulators */
374         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
375                 struct ab8500_regulator_info *info = NULL;
376
377                 /* assign per-regulator data */
378                 info = &ab8500_regulator_info[i];
379                 info->dev = &pdev->dev;
380                 info->ab8500 = ab8500;
381
382                 info->regulator = regulator_register(&info->desc, &pdev->dev,
383                                 pdata->regulator[i], info);
384                 if (IS_ERR(info->regulator)) {
385                         err = PTR_ERR(info->regulator);
386                         dev_err(&pdev->dev, "failed to register regulator %s\n",
387                                         info->desc.name);
388                         /* when we fail, un-register all earlier regulators */
389                         while (--i >= 0) {
390                                 info = &ab8500_regulator_info[i];
391                                 regulator_unregister(info->regulator);
392                         }
393                         return err;
394                 }
395         }
396
397         return 0;
398 }
399
400 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
401 {
402         int i;
403
404         for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
405                 struct ab8500_regulator_info *info = NULL;
406                 info = &ab8500_regulator_info[i];
407                 regulator_unregister(info->regulator);
408         }
409
410         return 0;
411 }
412
413 static struct platform_driver ab8500_regulator_driver = {
414         .probe = ab8500_regulator_probe,
415         .remove = __devexit_p(ab8500_regulator_remove),
416         .driver         = {
417                 .name   = "ab8500-regulator",
418                 .owner  = THIS_MODULE,
419         },
420 };
421
422 static int __init ab8500_regulator_init(void)
423 {
424         int ret;
425
426         ret = platform_driver_register(&ab8500_regulator_driver);
427         if (ret != 0)
428                 pr_err("Failed to register ab8500 regulator: %d\n", ret);
429
430         return ret;
431 }
432 subsys_initcall(ab8500_regulator_init);
433
434 static void __exit ab8500_regulator_exit(void)
435 {
436         platform_driver_unregister(&ab8500_regulator_driver);
437 }
438 module_exit(ab8500_regulator_exit);
439
440 MODULE_LICENSE("GPL v2");
441 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
442 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
443 MODULE_ALIAS("platform:ab8500-regulator");