Linux-libre 3.10.103-gnu
[librecmc/linux-libre.git] / drivers / regulator / ab8500-ext.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * License Terms: GNU General Public License v2
5  *
6  * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
7  *
8  * This file is based on drivers/regulator/ab8500.c
9  *
10  * AB8500 external regulators
11  *
12  * ab8500-ext supports the following regulators:
13  * - VextSupply3
14  */
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/mfd/abx500.h>
23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/regulator/ab8500.h>
25
26 /**
27  * struct ab8500_ext_regulator_info - ab8500 regulator information
28  * @dev: device pointer
29  * @desc: regulator description
30  * @rdev: regulator device
31  * @cfg: regulator configuration (extension of regulator FW configuration)
32  * @update_bank: bank to control on/off
33  * @update_reg: register to control on/off
34  * @update_mask: mask to enable/disable and set mode of regulator
35  * @update_val: bits holding the regulator current mode
36  * @update_val_hp: bits to set EN pin active (LPn pin deactive)
37  *                 normally this means high power mode
38  * @update_val_lp: bits to set EN pin active and LPn pin active
39  *                 normally this means low power mode
40  * @update_val_hw: bits to set regulator pins in HW control
41  *                 SysClkReq pins and logic will choose mode
42  */
43 struct ab8500_ext_regulator_info {
44         struct device *dev;
45         struct regulator_desc desc;
46         struct regulator_dev *rdev;
47         struct ab8500_ext_regulator_cfg *cfg;
48         u8 update_bank;
49         u8 update_reg;
50         u8 update_mask;
51         u8 update_val;
52         u8 update_val_hp;
53         u8 update_val_lp;
54         u8 update_val_hw;
55 };
56
57 static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
58 {
59         int ret;
60         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
61         u8 regval;
62
63         if (info == NULL) {
64                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
65                 return -EINVAL;
66         }
67
68         /*
69          * To satisfy both HW high power request and SW request, the regulator
70          * must be on in high power.
71          */
72         if (info->cfg && info->cfg->hwreq)
73                 regval = info->update_val_hp;
74         else
75                 regval = info->update_val;
76
77         ret = abx500_mask_and_set_register_interruptible(info->dev,
78                 info->update_bank, info->update_reg,
79                 info->update_mask, regval);
80         if (ret < 0) {
81                 dev_err(rdev_get_dev(info->rdev),
82                         "couldn't set enable bits for regulator\n");
83                 return ret;
84         }
85
86         dev_dbg(rdev_get_dev(rdev),
87                 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
88                 info->desc.name, info->update_bank, info->update_reg,
89                 info->update_mask, regval);
90
91         return 0;
92 }
93
94 static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
95 {
96         int ret;
97         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
98         u8 regval;
99
100         if (info == NULL) {
101                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
102                 return -EINVAL;
103         }
104
105         /*
106          * Set the regulator in HW request mode if configured
107          */
108         if (info->cfg && info->cfg->hwreq)
109                 regval = info->update_val_hw;
110         else
111                 regval = 0;
112
113         ret = abx500_mask_and_set_register_interruptible(info->dev,
114                 info->update_bank, info->update_reg,
115                 info->update_mask, regval);
116         if (ret < 0) {
117                 dev_err(rdev_get_dev(info->rdev),
118                         "couldn't set disable bits for regulator\n");
119                 return ret;
120         }
121
122         dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
123                 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
124                 info->desc.name, info->update_bank, info->update_reg,
125                 info->update_mask, regval);
126
127         return 0;
128 }
129
130 static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
131 {
132         int ret;
133         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
134         u8 regval;
135
136         if (info == NULL) {
137                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
138                 return -EINVAL;
139         }
140
141         ret = abx500_get_register_interruptible(info->dev,
142                 info->update_bank, info->update_reg, &regval);
143         if (ret < 0) {
144                 dev_err(rdev_get_dev(rdev),
145                         "couldn't read 0x%x register\n", info->update_reg);
146                 return ret;
147         }
148
149         dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
150                 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
151                 info->desc.name, info->update_bank, info->update_reg,
152                 info->update_mask, regval);
153
154         if (((regval & info->update_mask) == info->update_val_lp) ||
155             ((regval & info->update_mask) == info->update_val_hp))
156                 return 1;
157         else
158                 return 0;
159 }
160
161 static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
162                                          unsigned int mode)
163 {
164         int ret = 0;
165         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
166         u8 regval;
167
168         if (info == NULL) {
169                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
170                 return -EINVAL;
171         }
172
173         switch (mode) {
174         case REGULATOR_MODE_NORMAL:
175                 regval = info->update_val_hp;
176                 break;
177         case REGULATOR_MODE_IDLE:
178                 regval = info->update_val_lp;
179                 break;
180
181         default:
182                 return -EINVAL;
183         }
184
185         /* If regulator is enabled and info->cfg->hwreq is set, the regulator
186            must be on in high power, so we don't need to write the register with
187            the same value.
188          */
189         if (ab8500_ext_regulator_is_enabled(rdev) &&
190             !(info->cfg && info->cfg->hwreq)) {
191                 ret = abx500_mask_and_set_register_interruptible(info->dev,
192                                         info->update_bank, info->update_reg,
193                                         info->update_mask, regval);
194                 if (ret < 0) {
195                         dev_err(rdev_get_dev(rdev),
196                                 "Could not set regulator mode.\n");
197                         return ret;
198                 }
199
200                 dev_dbg(rdev_get_dev(rdev),
201                         "%s-set_mode (bank, reg, mask, value): "
202                         "0x%x, 0x%x, 0x%x, 0x%x\n",
203                         info->desc.name, info->update_bank, info->update_reg,
204                         info->update_mask, regval);
205         }
206
207         info->update_val = regval;
208
209         return 0;
210 }
211
212 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
213 {
214         struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
215         int ret;
216
217         if (info == NULL) {
218                 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
219                 return -EINVAL;
220         }
221
222         if (info->update_val == info->update_val_hp)
223                 ret = REGULATOR_MODE_NORMAL;
224         else if (info->update_val == info->update_val_lp)
225                 ret = REGULATOR_MODE_IDLE;
226         else
227                 ret = -EINVAL;
228
229         return ret;
230 }
231
232 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
233                                    unsigned selector)
234 {
235         struct regulation_constraints *regu_constraints = rdev->constraints;
236
237         if (regu_constraints == NULL) {
238                 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
239                 return -EINVAL;
240         }
241         /* return the uV for the fixed regulators */
242         if (regu_constraints->min_uV && regu_constraints->max_uV) {
243                 if (regu_constraints->min_uV == regu_constraints->max_uV)
244                         return regu_constraints->min_uV;
245         }
246         return -EINVAL;
247 }
248
249 static struct regulator_ops ab8500_ext_regulator_ops = {
250         .enable                 = ab8500_ext_regulator_enable,
251         .disable                = ab8500_ext_regulator_disable,
252         .is_enabled             = ab8500_ext_regulator_is_enabled,
253         .set_mode               = ab8500_ext_regulator_set_mode,
254         .get_mode               = ab8500_ext_regulator_get_mode,
255         .list_voltage           = ab8500_ext_list_voltage,
256 };
257
258 static struct ab8500_ext_regulator_info
259                 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
260         [AB8500_EXT_SUPPLY1] = {
261                 .desc = {
262                         .name           = "VEXTSUPPLY1",
263                         .ops            = &ab8500_ext_regulator_ops,
264                         .type           = REGULATOR_VOLTAGE,
265                         .id             = AB8500_EXT_SUPPLY1,
266                         .owner          = THIS_MODULE,
267                         .n_voltages     = 1,
268                 },
269                 .update_bank            = 0x04,
270                 .update_reg             = 0x08,
271                 .update_mask            = 0x03,
272                 .update_val             = 0x01,
273                 .update_val_hp          = 0x01,
274                 .update_val_lp          = 0x03,
275                 .update_val_hw          = 0x02,
276         },
277         [AB8500_EXT_SUPPLY2] = {
278                 .desc = {
279                         .name           = "VEXTSUPPLY2",
280                         .ops            = &ab8500_ext_regulator_ops,
281                         .type           = REGULATOR_VOLTAGE,
282                         .id             = AB8500_EXT_SUPPLY2,
283                         .owner          = THIS_MODULE,
284                         .n_voltages     = 1,
285                 },
286                 .update_bank            = 0x04,
287                 .update_reg             = 0x08,
288                 .update_mask            = 0x0c,
289                 .update_val             = 0x04,
290                 .update_val_hp          = 0x04,
291                 .update_val_lp          = 0x0c,
292                 .update_val_hw          = 0x08,
293         },
294         [AB8500_EXT_SUPPLY3] = {
295                 .desc = {
296                         .name           = "VEXTSUPPLY3",
297                         .ops            = &ab8500_ext_regulator_ops,
298                         .type           = REGULATOR_VOLTAGE,
299                         .id             = AB8500_EXT_SUPPLY3,
300                         .owner          = THIS_MODULE,
301                         .n_voltages     = 1,
302                 },
303                 .update_bank            = 0x04,
304                 .update_reg             = 0x08,
305                 .update_mask            = 0x30,
306                 .update_val             = 0x10,
307                 .update_val_hp          = 0x10,
308                 .update_val_lp          = 0x30,
309                 .update_val_hw          = 0x20,
310         },
311 };
312
313 int ab8500_ext_regulator_init(struct platform_device *pdev)
314 {
315         struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
316         struct ab8500_platform_data *ppdata;
317         struct ab8500_regulator_platform_data *pdata;
318         struct regulator_config config = { };
319         int i, err;
320
321         if (!ab8500) {
322                 dev_err(&pdev->dev, "null mfd parent\n");
323                 return -EINVAL;
324         }
325         ppdata = dev_get_platdata(ab8500->dev);
326         if (!ppdata) {
327                 dev_err(&pdev->dev, "null parent pdata\n");
328                 return -EINVAL;
329         }
330
331         pdata = ppdata->regulator;
332         if (!pdata) {
333                 dev_err(&pdev->dev, "null pdata\n");
334                 return -EINVAL;
335         }
336
337         /* make sure the platform data has the correct size */
338         if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
339                 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
340                 return -EINVAL;
341         }
342
343         /* check for AB8500 2.x */
344         if (is_ab8500_2p0_or_earlier(ab8500)) {
345                 struct ab8500_ext_regulator_info *info;
346
347                 /* VextSupply3LPn is inverted on AB8500 2.x */
348                 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
349                 info->update_val = 0x30;
350                 info->update_val_hp = 0x30;
351                 info->update_val_lp = 0x10;
352         }
353
354         /* register all regulators */
355         for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
356                 struct ab8500_ext_regulator_info *info = NULL;
357
358                 /* assign per-regulator data */
359                 info = &ab8500_ext_regulator_info[i];
360                 info->dev = &pdev->dev;
361                 info->cfg = (struct ab8500_ext_regulator_cfg *)
362                         pdata->ext_regulator[i].driver_data;
363
364                 config.dev = &pdev->dev;
365                 config.init_data = &pdata->ext_regulator[i];
366                 config.driver_data = info;
367
368                 /* register regulator with framework */
369                 info->rdev = regulator_register(&info->desc, &config);
370                 if (IS_ERR(info->rdev)) {
371                         err = PTR_ERR(info->rdev);
372                         dev_err(&pdev->dev, "failed to register regulator %s\n",
373                                         info->desc.name);
374                         /* when we fail, un-register all earlier regulators */
375                         while (--i >= 0) {
376                                 info = &ab8500_ext_regulator_info[i];
377                                 regulator_unregister(info->rdev);
378                         }
379                         return err;
380                 }
381
382                 dev_dbg(rdev_get_dev(info->rdev),
383                         "%s-probed\n", info->desc.name);
384         }
385
386         return 0;
387 }
388
389 void ab8500_ext_regulator_exit(struct platform_device *pdev)
390 {
391         int i;
392
393         for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
394                 struct ab8500_ext_regulator_info *info = NULL;
395                 info = &ab8500_ext_regulator_info[i];
396
397                 dev_vdbg(rdev_get_dev(info->rdev),
398                         "%s-remove\n", info->desc.name);
399
400                 regulator_unregister(info->rdev);
401         }
402 }
403
404 MODULE_LICENSE("GPL v2");
405 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
406 MODULE_DESCRIPTION("AB8500 external regulator driver");
407 MODULE_ALIAS("platform:ab8500-ext-regulator");