regulator: ab8500-ext: Provide a set_voltage call-back operation
[firefly-linux-kernel-4.4.55.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_set_voltage(struct regulator_dev *rdev, int min_uV,
233                                   int max_uV, unsigned *selector)
234 {
235         struct regulation_constraints *regu_constraints = rdev->constraints;
236
237         if (!regu_constraints) {
238                 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
239                 return -EINVAL;
240         }
241
242         if (regu_constraints->min_uV == min_uV &&
243             regu_constraints->max_uV == max_uV)
244                 return 0;
245
246         dev_err(rdev_get_dev(rdev),
247                 "Requested min %duV max %duV != constrained min %duV max %duV\n",
248                 min_uV, max_uV,
249                 regu_constraints->min_uV, regu_constraints->max_uV);
250
251         return -EINVAL;
252 }
253
254 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
255                                    unsigned selector)
256 {
257         struct regulation_constraints *regu_constraints = rdev->constraints;
258
259         if (regu_constraints == NULL) {
260                 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
261                 return -EINVAL;
262         }
263         /* return the uV for the fixed regulators */
264         if (regu_constraints->min_uV && regu_constraints->max_uV) {
265                 if (regu_constraints->min_uV == regu_constraints->max_uV)
266                         return regu_constraints->min_uV;
267         }
268         return -EINVAL;
269 }
270
271 static struct regulator_ops ab8500_ext_regulator_ops = {
272         .enable                 = ab8500_ext_regulator_enable,
273         .disable                = ab8500_ext_regulator_disable,
274         .is_enabled             = ab8500_ext_regulator_is_enabled,
275         .set_mode               = ab8500_ext_regulator_set_mode,
276         .get_mode               = ab8500_ext_regulator_get_mode,
277         .set_voltage            = ab8500_ext_set_voltage,
278         .list_voltage           = ab8500_ext_list_voltage,
279 };
280
281 static struct ab8500_ext_regulator_info
282                 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
283         [AB8500_EXT_SUPPLY1] = {
284                 .desc = {
285                         .name           = "VEXTSUPPLY1",
286                         .ops            = &ab8500_ext_regulator_ops,
287                         .type           = REGULATOR_VOLTAGE,
288                         .id             = AB8500_EXT_SUPPLY1,
289                         .owner          = THIS_MODULE,
290                         .n_voltages     = 1,
291                 },
292                 .update_bank            = 0x04,
293                 .update_reg             = 0x08,
294                 .update_mask            = 0x03,
295                 .update_val             = 0x01,
296                 .update_val_hp          = 0x01,
297                 .update_val_lp          = 0x03,
298                 .update_val_hw          = 0x02,
299         },
300         [AB8500_EXT_SUPPLY2] = {
301                 .desc = {
302                         .name           = "VEXTSUPPLY2",
303                         .ops            = &ab8500_ext_regulator_ops,
304                         .type           = REGULATOR_VOLTAGE,
305                         .id             = AB8500_EXT_SUPPLY2,
306                         .owner          = THIS_MODULE,
307                         .n_voltages     = 1,
308                 },
309                 .update_bank            = 0x04,
310                 .update_reg             = 0x08,
311                 .update_mask            = 0x0c,
312                 .update_val             = 0x04,
313                 .update_val_hp          = 0x04,
314                 .update_val_lp          = 0x0c,
315                 .update_val_hw          = 0x08,
316         },
317         [AB8500_EXT_SUPPLY3] = {
318                 .desc = {
319                         .name           = "VEXTSUPPLY3",
320                         .ops            = &ab8500_ext_regulator_ops,
321                         .type           = REGULATOR_VOLTAGE,
322                         .id             = AB8500_EXT_SUPPLY3,
323                         .owner          = THIS_MODULE,
324                         .n_voltages     = 1,
325                 },
326                 .update_bank            = 0x04,
327                 .update_reg             = 0x08,
328                 .update_mask            = 0x30,
329                 .update_val             = 0x10,
330                 .update_val_hp          = 0x10,
331                 .update_val_lp          = 0x30,
332                 .update_val_hw          = 0x20,
333         },
334 };
335
336 int ab8500_ext_regulator_init(struct platform_device *pdev)
337 {
338         struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
339         struct ab8500_platform_data *ppdata;
340         struct ab8500_regulator_platform_data *pdata;
341         struct regulator_config config = { };
342         int i, err;
343
344         if (!ab8500) {
345                 dev_err(&pdev->dev, "null mfd parent\n");
346                 return -EINVAL;
347         }
348         ppdata = dev_get_platdata(ab8500->dev);
349         if (!ppdata) {
350                 dev_err(&pdev->dev, "null parent pdata\n");
351                 return -EINVAL;
352         }
353
354         pdata = ppdata->regulator;
355         if (!pdata) {
356                 dev_err(&pdev->dev, "null pdata\n");
357                 return -EINVAL;
358         }
359
360         /* make sure the platform data has the correct size */
361         if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
362                 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
363                 return -EINVAL;
364         }
365
366         /* check for AB8500 2.x */
367         if (is_ab8500_2p0_or_earlier(ab8500)) {
368                 struct ab8500_ext_regulator_info *info;
369
370                 /* VextSupply3LPn is inverted on AB8500 2.x */
371                 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
372                 info->update_val = 0x30;
373                 info->update_val_hp = 0x30;
374                 info->update_val_lp = 0x10;
375         }
376
377         /* register all regulators */
378         for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
379                 struct ab8500_ext_regulator_info *info = NULL;
380
381                 /* assign per-regulator data */
382                 info = &ab8500_ext_regulator_info[i];
383                 info->dev = &pdev->dev;
384                 info->cfg = (struct ab8500_ext_regulator_cfg *)
385                         pdata->ext_regulator[i].driver_data;
386
387                 config.dev = &pdev->dev;
388                 config.init_data = &pdata->ext_regulator[i];
389                 config.driver_data = info;
390
391                 /* register regulator with framework */
392                 info->rdev = regulator_register(&info->desc, &config);
393                 if (IS_ERR(info->rdev)) {
394                         err = PTR_ERR(info->rdev);
395                         dev_err(&pdev->dev, "failed to register regulator %s\n",
396                                         info->desc.name);
397                         /* when we fail, un-register all earlier regulators */
398                         while (--i >= 0) {
399                                 info = &ab8500_ext_regulator_info[i];
400                                 regulator_unregister(info->rdev);
401                         }
402                         return err;
403                 }
404
405                 dev_dbg(rdev_get_dev(info->rdev),
406                         "%s-probed\n", info->desc.name);
407         }
408
409         return 0;
410 }
411
412 void ab8500_ext_regulator_exit(struct platform_device *pdev)
413 {
414         int i;
415
416         for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
417                 struct ab8500_ext_regulator_info *info = NULL;
418                 info = &ab8500_ext_regulator_info[i];
419
420                 dev_vdbg(rdev_get_dev(info->rdev),
421                         "%s-remove\n", info->desc.name);
422
423                 regulator_unregister(info->rdev);
424         }
425 }
426
427 MODULE_LICENSE("GPL v2");
428 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
429 MODULE_DESCRIPTION("AB8500 external regulator driver");
430 MODULE_ALIAS("platform:ab8500-ext-regulator");