]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/regulator/max77686.c
Linux 3.8-rc5
[~shefty/rdma-dev.git] / drivers / regulator / max77686.c
1 /*
2  * max77686.c - Regulator driver for the Maxim 77686
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * Chiwoong Byun <woong.byun@smasung.com>
6  * Jonghwa Lee <jonghwa3.lee@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * This driver is based on max8997.c
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/bug.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/gpio.h>
30 #include <linux/slab.h>
31 #include <linux/platform_device.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/regulator/of_regulator.h>
35 #include <linux/mfd/max77686.h>
36 #include <linux/mfd/max77686-private.h>
37
38 #define MAX77686_LDO_MINUV      800000
39 #define MAX77686_LDO_UVSTEP     50000
40 #define MAX77686_LDO_LOW_MINUV  800000
41 #define MAX77686_LDO_LOW_UVSTEP 25000
42 #define MAX77686_BUCK_MINUV     750000
43 #define MAX77686_BUCK_UVSTEP    50000
44 #define MAX77686_RAMP_DELAY     100000                  /* uV/us */
45 #define MAX77686_DVS_RAMP_DELAY 27500                   /* uV/us */
46 #define MAX77686_DVS_MINUV      600000
47 #define MAX77686_DVS_UVSTEP     12500
48
49 #define MAX77686_OPMODE_SHIFT   6
50 #define MAX77686_OPMODE_BUCK234_SHIFT   4
51 #define MAX77686_OPMODE_MASK    0x3
52
53 #define MAX77686_VSEL_MASK      0x3F
54 #define MAX77686_DVS_VSEL_MASK  0xFF
55
56 #define MAX77686_RAMP_RATE_MASK 0xC0
57
58 #define MAX77686_REGULATORS     MAX77686_REG_MAX
59 #define MAX77686_LDOS           26
60
61 enum max77686_ramp_rate {
62         RAMP_RATE_13P75MV,
63         RAMP_RATE_27P5MV,
64         RAMP_RATE_55MV,
65         RAMP_RATE_NO_CTRL,      /* 100mV/us */
66 };
67
68 struct max77686_data {
69         struct regulator_dev *rdev[MAX77686_REGULATORS];
70         unsigned int opmode[MAX77686_REGULATORS];
71 };
72
73 /* Some BUCKS supports Normal[ON/OFF] mode during suspend */
74 static int max77686_buck_set_suspend_disable(struct regulator_dev *rdev)
75 {
76         unsigned int val;
77         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
78
79         if (rdev->desc->id == MAX77686_BUCK1)
80                 val = 0x1;
81         else
82                 val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT;
83
84         max77686->opmode[rdev->desc->id] = val;
85         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
86                                   rdev->desc->enable_mask,
87                                   val);
88 }
89
90 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
91 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
92                                      unsigned int mode)
93 {
94         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
95         unsigned int val;
96
97         /* BUCK[5-9] doesn't support this feature */
98         if (rdev->desc->id >= MAX77686_BUCK5)
99                 return 0;
100
101         switch (mode) {
102         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
103                 val = 0x2 << MAX77686_OPMODE_SHIFT;
104                 break;
105         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
106                 val = 0x3 << MAX77686_OPMODE_SHIFT;
107                 break;
108         default:
109                 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
110                         rdev->desc->name, mode);
111                 return -EINVAL;
112         }
113
114         max77686->opmode[rdev->desc->id] = val;
115         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
116                                   rdev->desc->enable_mask,
117                                   val);
118 }
119
120 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
121 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
122                                      unsigned int mode)
123 {
124         unsigned int val;
125         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
126
127         switch (mode) {
128         case REGULATOR_MODE_STANDBY:                    /* switch off */
129                 val = 0x1 << MAX77686_OPMODE_SHIFT;
130                 break;
131         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
132                 val = 0x2 << MAX77686_OPMODE_SHIFT;
133                 break;
134         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
135                 val = 0x3 << MAX77686_OPMODE_SHIFT;
136                 break;
137         default:
138                 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
139                         rdev->desc->name, mode);
140                 return -EINVAL;
141         }
142
143         max77686->opmode[rdev->desc->id] = val;
144         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
145                                   rdev->desc->enable_mask,
146                                   val);
147 }
148
149 static int max77686_enable(struct regulator_dev *rdev)
150 {
151         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
152
153         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
154                                   rdev->desc->enable_mask,
155                                   max77686->opmode[rdev->desc->id]);
156 }
157
158 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
159 {
160         unsigned int ramp_value = RAMP_RATE_NO_CTRL;
161
162         switch (ramp_delay) {
163         case 1 ... 13750:
164                 ramp_value = RAMP_RATE_13P75MV;
165                 break;
166         case 13751 ... 27500:
167                 ramp_value = RAMP_RATE_27P5MV;
168                 break;
169         case 27501 ... 55000:
170                 ramp_value = RAMP_RATE_55MV;
171                 break;
172         case 55001 ... 100000:
173                 break;
174         default:
175                 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
176                         rdev->desc->name, ramp_delay);
177         }
178
179         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
180                                   MAX77686_RAMP_RATE_MASK, ramp_value << 6);
181 }
182
183 static struct regulator_ops max77686_ops = {
184         .list_voltage           = regulator_list_voltage_linear,
185         .map_voltage            = regulator_map_voltage_linear,
186         .is_enabled             = regulator_is_enabled_regmap,
187         .enable                 = max77686_enable,
188         .disable                = regulator_disable_regmap,
189         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
190         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
191         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
192         .set_suspend_mode       = max77686_set_suspend_mode,
193 };
194
195 static struct regulator_ops max77686_ldo_ops = {
196         .list_voltage           = regulator_list_voltage_linear,
197         .map_voltage            = regulator_map_voltage_linear,
198         .is_enabled             = regulator_is_enabled_regmap,
199         .enable                 = max77686_enable,
200         .disable                = regulator_disable_regmap,
201         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
202         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
203         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
204         .set_suspend_mode       = max77686_ldo_set_suspend_mode,
205 };
206
207 static struct regulator_ops max77686_buck1_ops = {
208         .list_voltage           = regulator_list_voltage_linear,
209         .map_voltage            = regulator_map_voltage_linear,
210         .is_enabled             = regulator_is_enabled_regmap,
211         .enable                 = max77686_enable,
212         .disable                = regulator_disable_regmap,
213         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
214         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
215         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
216         .set_suspend_disable    = max77686_buck_set_suspend_disable,
217 };
218
219 static struct regulator_ops max77686_buck_dvs_ops = {
220         .list_voltage           = regulator_list_voltage_linear,
221         .map_voltage            = regulator_map_voltage_linear,
222         .is_enabled             = regulator_is_enabled_regmap,
223         .enable                 = max77686_enable,
224         .disable                = regulator_disable_regmap,
225         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
226         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
227         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
228         .set_ramp_delay         = max77686_set_ramp_delay,
229         .set_suspend_disable    = max77686_buck_set_suspend_disable,
230 };
231
232 #define regulator_desc_ldo(num)         {                               \
233         .name           = "LDO"#num,                                    \
234         .id             = MAX77686_LDO##num,                            \
235         .ops            = &max77686_ops,                                \
236         .type           = REGULATOR_VOLTAGE,                            \
237         .owner          = THIS_MODULE,                                  \
238         .min_uV         = MAX77686_LDO_MINUV,                           \
239         .uV_step        = MAX77686_LDO_UVSTEP,                          \
240         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
241         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
242         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
243         .vsel_mask      = MAX77686_VSEL_MASK,                           \
244         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
245         .enable_mask    = MAX77686_OPMODE_MASK                          \
246                         << MAX77686_OPMODE_SHIFT,                       \
247 }
248 #define regulator_desc_lpm_ldo(num)     {                               \
249         .name           = "LDO"#num,                                    \
250         .id             = MAX77686_LDO##num,                            \
251         .ops            = &max77686_ldo_ops,                            \
252         .type           = REGULATOR_VOLTAGE,                            \
253         .owner          = THIS_MODULE,                                  \
254         .min_uV         = MAX77686_LDO_MINUV,                           \
255         .uV_step        = MAX77686_LDO_UVSTEP,                          \
256         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
257         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
258         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
259         .vsel_mask      = MAX77686_VSEL_MASK,                           \
260         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
261         .enable_mask    = MAX77686_OPMODE_MASK                          \
262                         << MAX77686_OPMODE_SHIFT,                       \
263 }
264 #define regulator_desc_ldo_low(num)             {                       \
265         .name           = "LDO"#num,                                    \
266         .id             = MAX77686_LDO##num,                            \
267         .ops            = &max77686_ldo_ops,                            \
268         .type           = REGULATOR_VOLTAGE,                            \
269         .owner          = THIS_MODULE,                                  \
270         .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
271         .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
272         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
273         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
274         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
275         .vsel_mask      = MAX77686_VSEL_MASK,                           \
276         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
277         .enable_mask    = MAX77686_OPMODE_MASK                          \
278                         << MAX77686_OPMODE_SHIFT,                       \
279 }
280 #define regulator_desc_ldo1_low(num)            {                       \
281         .name           = "LDO"#num,                                    \
282         .id             = MAX77686_LDO##num,                            \
283         .ops            = &max77686_ops,                                \
284         .type           = REGULATOR_VOLTAGE,                            \
285         .owner          = THIS_MODULE,                                  \
286         .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
287         .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
288         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
289         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
290         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
291         .vsel_mask      = MAX77686_VSEL_MASK,                           \
292         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
293         .enable_mask    = MAX77686_OPMODE_MASK                          \
294                         << MAX77686_OPMODE_SHIFT,                       \
295 }
296 #define regulator_desc_buck(num)                {                       \
297         .name           = "BUCK"#num,                                   \
298         .id             = MAX77686_BUCK##num,                           \
299         .ops            = &max77686_ops,                                \
300         .type           = REGULATOR_VOLTAGE,                            \
301         .owner          = THIS_MODULE,                                  \
302         .min_uV         = MAX77686_BUCK_MINUV,                          \
303         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
304         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
305         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
306         .vsel_reg       = MAX77686_REG_BUCK5OUT + (num - 5) * 2,        \
307         .vsel_mask      = MAX77686_VSEL_MASK,                           \
308         .enable_reg     = MAX77686_REG_BUCK5CTRL + (num - 5) * 2,       \
309         .enable_mask    = MAX77686_OPMODE_MASK,                         \
310 }
311 #define regulator_desc_buck1(num)               {                       \
312         .name           = "BUCK"#num,                                   \
313         .id             = MAX77686_BUCK##num,                           \
314         .ops            = &max77686_buck1_ops,                          \
315         .type           = REGULATOR_VOLTAGE,                            \
316         .owner          = THIS_MODULE,                                  \
317         .min_uV         = MAX77686_BUCK_MINUV,                          \
318         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
319         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
320         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
321         .vsel_reg       = MAX77686_REG_BUCK1OUT,                        \
322         .vsel_mask      = MAX77686_VSEL_MASK,                           \
323         .enable_reg     = MAX77686_REG_BUCK1CTRL,                       \
324         .enable_mask    = MAX77686_OPMODE_MASK,                         \
325 }
326 #define regulator_desc_buck_dvs(num)            {                       \
327         .name           = "BUCK"#num,                                   \
328         .id             = MAX77686_BUCK##num,                           \
329         .ops            = &max77686_buck_dvs_ops,                       \
330         .type           = REGULATOR_VOLTAGE,                            \
331         .owner          = THIS_MODULE,                                  \
332         .min_uV         = MAX77686_DVS_MINUV,                           \
333         .uV_step        = MAX77686_DVS_UVSTEP,                          \
334         .ramp_delay     = MAX77686_DVS_RAMP_DELAY,                      \
335         .n_voltages     = MAX77686_DVS_VSEL_MASK + 1,                   \
336         .vsel_reg       = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10,      \
337         .vsel_mask      = MAX77686_DVS_VSEL_MASK,                       \
338         .enable_reg     = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10,     \
339         .enable_mask    = MAX77686_OPMODE_MASK                          \
340                         << MAX77686_OPMODE_BUCK234_SHIFT,               \
341 }
342
343 static struct regulator_desc regulators[] = {
344         regulator_desc_ldo1_low(1),
345         regulator_desc_ldo_low(2),
346         regulator_desc_ldo(3),
347         regulator_desc_ldo(4),
348         regulator_desc_ldo(5),
349         regulator_desc_ldo_low(6),
350         regulator_desc_ldo_low(7),
351         regulator_desc_ldo_low(8),
352         regulator_desc_ldo(9),
353         regulator_desc_lpm_ldo(10),
354         regulator_desc_lpm_ldo(11),
355         regulator_desc_lpm_ldo(12),
356         regulator_desc_ldo(13),
357         regulator_desc_lpm_ldo(14),
358         regulator_desc_ldo_low(15),
359         regulator_desc_lpm_ldo(16),
360         regulator_desc_ldo(17),
361         regulator_desc_ldo(18),
362         regulator_desc_ldo(19),
363         regulator_desc_ldo(20),
364         regulator_desc_ldo(21),
365         regulator_desc_ldo(22),
366         regulator_desc_ldo(23),
367         regulator_desc_ldo(24),
368         regulator_desc_ldo(25),
369         regulator_desc_ldo(26),
370         regulator_desc_buck1(1),
371         regulator_desc_buck_dvs(2),
372         regulator_desc_buck_dvs(3),
373         regulator_desc_buck_dvs(4),
374         regulator_desc_buck(5),
375         regulator_desc_buck(6),
376         regulator_desc_buck(7),
377         regulator_desc_buck(8),
378         regulator_desc_buck(9),
379 };
380
381 #ifdef CONFIG_OF
382 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
383                                         struct max77686_platform_data *pdata)
384 {
385         struct device_node *pmic_np, *regulators_np;
386         struct max77686_regulator_data *rdata;
387         struct of_regulator_match rmatch;
388         unsigned int i;
389
390         pmic_np = iodev->dev->of_node;
391         regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
392         if (!regulators_np) {
393                 dev_err(iodev->dev, "could not find regulators sub-node\n");
394                 return -EINVAL;
395         }
396
397         pdata->num_regulators = ARRAY_SIZE(regulators);
398         rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
399                              pdata->num_regulators, GFP_KERNEL);
400         if (!rdata) {
401                 dev_err(iodev->dev,
402                         "could not allocate memory for regulator data\n");
403                 return -ENOMEM;
404         }
405
406         for (i = 0; i < pdata->num_regulators; i++) {
407                 rmatch.name = regulators[i].name;
408                 rmatch.init_data = NULL;
409                 rmatch.of_node = NULL;
410                 of_regulator_match(iodev->dev, regulators_np, &rmatch, 1);
411                 rdata[i].initdata = rmatch.init_data;
412                 rdata[i].of_node = rmatch.of_node;
413         }
414
415         pdata->regulators = rdata;
416
417         return 0;
418 }
419 #else
420 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
421                                         struct max77686_platform_data *pdata)
422 {
423         return 0;
424 }
425 #endif /* CONFIG_OF */
426
427 static int max77686_pmic_probe(struct platform_device *pdev)
428 {
429         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
430         struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
431         struct max77686_data *max77686;
432         int i, ret = 0;
433         struct regulator_config config = { };
434
435         dev_dbg(&pdev->dev, "%s\n", __func__);
436
437         if (!pdata) {
438                 dev_err(&pdev->dev, "no platform data found for regulator\n");
439                 return -ENODEV;
440         }
441
442         if (iodev->dev->of_node) {
443                 ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
444                 if (ret)
445                         return ret;
446         }
447
448         if (pdata->num_regulators != MAX77686_REGULATORS) {
449                 dev_err(&pdev->dev,
450                         "Invalid initial data for regulator's initialiation\n");
451                 return -EINVAL;
452         }
453
454         max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
455                                 GFP_KERNEL);
456         if (!max77686)
457                 return -ENOMEM;
458
459         config.dev = &pdev->dev;
460         config.regmap = iodev->regmap;
461         config.driver_data = max77686;
462         platform_set_drvdata(pdev, max77686);
463
464         for (i = 0; i < MAX77686_REGULATORS; i++) {
465                 config.init_data = pdata->regulators[i].initdata;
466                 config.of_node = pdata->regulators[i].of_node;
467
468                 max77686->opmode[i] = regulators[i].enable_mask;
469                 max77686->rdev[i] = regulator_register(&regulators[i], &config);
470                 if (IS_ERR(max77686->rdev[i])) {
471                         ret = PTR_ERR(max77686->rdev[i]);
472                         dev_err(&pdev->dev,
473                                 "regulator init failed for %d\n", i);
474                         max77686->rdev[i] = NULL;
475                         goto err;
476                 }
477         }
478
479         return 0;
480 err:
481         while (--i >= 0)
482                 regulator_unregister(max77686->rdev[i]);
483         return ret;
484 }
485
486 static int max77686_pmic_remove(struct platform_device *pdev)
487 {
488         struct max77686_data *max77686 = platform_get_drvdata(pdev);
489         int i;
490
491         for (i = 0; i < MAX77686_REGULATORS; i++)
492                 regulator_unregister(max77686->rdev[i]);
493
494         return 0;
495 }
496
497 static const struct platform_device_id max77686_pmic_id[] = {
498         {"max77686-pmic", 0},
499         { },
500 };
501 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
502
503 static struct platform_driver max77686_pmic_driver = {
504         .driver = {
505                 .name = "max77686-pmic",
506                 .owner = THIS_MODULE,
507         },
508         .probe = max77686_pmic_probe,
509         .remove = max77686_pmic_remove,
510         .id_table = max77686_pmic_id,
511 };
512
513 static int __init max77686_pmic_init(void)
514 {
515         return platform_driver_register(&max77686_pmic_driver);
516 }
517 subsys_initcall(max77686_pmic_init);
518
519 static void __exit max77686_pmic_cleanup(void)
520 {
521         platform_driver_unregister(&max77686_pmic_driver);
522 }
523 module_exit(max77686_pmic_cleanup);
524
525 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
526 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
527 MODULE_LICENSE("GPL");