Merge branches 'iommu/fixes', 'x86/vt-d', 'x86/amd', 'ppc/pamu', 'core' and 'arm...
[firefly-linux-kernel-4.4.55.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         int id = rdev_get_id(rdev);
79
80         if (id == MAX77686_BUCK1)
81                 val = 0x1;
82         else
83                 val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT;
84
85         max77686->opmode[id] = val;
86         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
87                                   rdev->desc->enable_mask,
88                                   val);
89 }
90
91 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
92 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
93                                      unsigned int mode)
94 {
95         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
96         unsigned int val;
97         int id = rdev_get_id(rdev);
98
99         /* BUCK[5-9] doesn't support this feature */
100         if (id >= MAX77686_BUCK5)
101                 return 0;
102
103         switch (mode) {
104         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
105                 val = 0x2 << MAX77686_OPMODE_SHIFT;
106                 break;
107         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
108                 val = 0x3 << MAX77686_OPMODE_SHIFT;
109                 break;
110         default:
111                 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
112                         rdev->desc->name, mode);
113                 return -EINVAL;
114         }
115
116         max77686->opmode[id] = val;
117         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
118                                   rdev->desc->enable_mask,
119                                   val);
120 }
121
122 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
123 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
124                                      unsigned int mode)
125 {
126         unsigned int val;
127         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
128
129         switch (mode) {
130         case REGULATOR_MODE_STANDBY:                    /* switch off */
131                 val = 0x1 << MAX77686_OPMODE_SHIFT;
132                 break;
133         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
134                 val = 0x2 << MAX77686_OPMODE_SHIFT;
135                 break;
136         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
137                 val = 0x3 << MAX77686_OPMODE_SHIFT;
138                 break;
139         default:
140                 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
141                         rdev->desc->name, mode);
142                 return -EINVAL;
143         }
144
145         max77686->opmode[rdev_get_id(rdev)] = val;
146         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
147                                   rdev->desc->enable_mask,
148                                   val);
149 }
150
151 static int max77686_enable(struct regulator_dev *rdev)
152 {
153         struct max77686_data *max77686 = rdev_get_drvdata(rdev);
154
155         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
156                                   rdev->desc->enable_mask,
157                                   max77686->opmode[rdev_get_id(rdev)]);
158 }
159
160 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
161 {
162         unsigned int ramp_value = RAMP_RATE_NO_CTRL;
163
164         switch (ramp_delay) {
165         case 1 ... 13750:
166                 ramp_value = RAMP_RATE_13P75MV;
167                 break;
168         case 13751 ... 27500:
169                 ramp_value = RAMP_RATE_27P5MV;
170                 break;
171         case 27501 ... 55000:
172                 ramp_value = RAMP_RATE_55MV;
173                 break;
174         case 55001 ... 100000:
175                 break;
176         default:
177                 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
178                         rdev->desc->name, ramp_delay);
179         }
180
181         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
182                                   MAX77686_RAMP_RATE_MASK, ramp_value << 6);
183 }
184
185 static struct regulator_ops max77686_ops = {
186         .list_voltage           = regulator_list_voltage_linear,
187         .map_voltage            = regulator_map_voltage_linear,
188         .is_enabled             = regulator_is_enabled_regmap,
189         .enable                 = max77686_enable,
190         .disable                = regulator_disable_regmap,
191         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
192         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
193         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
194         .set_suspend_mode       = max77686_set_suspend_mode,
195 };
196
197 static struct regulator_ops max77686_ldo_ops = {
198         .list_voltage           = regulator_list_voltage_linear,
199         .map_voltage            = regulator_map_voltage_linear,
200         .is_enabled             = regulator_is_enabled_regmap,
201         .enable                 = max77686_enable,
202         .disable                = regulator_disable_regmap,
203         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
204         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
205         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
206         .set_suspend_mode       = max77686_ldo_set_suspend_mode,
207 };
208
209 static struct regulator_ops max77686_buck1_ops = {
210         .list_voltage           = regulator_list_voltage_linear,
211         .map_voltage            = regulator_map_voltage_linear,
212         .is_enabled             = regulator_is_enabled_regmap,
213         .enable                 = max77686_enable,
214         .disable                = regulator_disable_regmap,
215         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
216         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
217         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
218         .set_suspend_disable    = max77686_buck_set_suspend_disable,
219 };
220
221 static struct regulator_ops max77686_buck_dvs_ops = {
222         .list_voltage           = regulator_list_voltage_linear,
223         .map_voltage            = regulator_map_voltage_linear,
224         .is_enabled             = regulator_is_enabled_regmap,
225         .enable                 = max77686_enable,
226         .disable                = regulator_disable_regmap,
227         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
228         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
229         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
230         .set_ramp_delay         = max77686_set_ramp_delay,
231         .set_suspend_disable    = max77686_buck_set_suspend_disable,
232 };
233
234 #define regulator_desc_ldo(num)         {                               \
235         .name           = "LDO"#num,                                    \
236         .id             = MAX77686_LDO##num,                            \
237         .ops            = &max77686_ops,                                \
238         .type           = REGULATOR_VOLTAGE,                            \
239         .owner          = THIS_MODULE,                                  \
240         .min_uV         = MAX77686_LDO_MINUV,                           \
241         .uV_step        = MAX77686_LDO_UVSTEP,                          \
242         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
243         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
244         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
245         .vsel_mask      = MAX77686_VSEL_MASK,                           \
246         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
247         .enable_mask    = MAX77686_OPMODE_MASK                          \
248                         << MAX77686_OPMODE_SHIFT,                       \
249 }
250 #define regulator_desc_lpm_ldo(num)     {                               \
251         .name           = "LDO"#num,                                    \
252         .id             = MAX77686_LDO##num,                            \
253         .ops            = &max77686_ldo_ops,                            \
254         .type           = REGULATOR_VOLTAGE,                            \
255         .owner          = THIS_MODULE,                                  \
256         .min_uV         = MAX77686_LDO_MINUV,                           \
257         .uV_step        = MAX77686_LDO_UVSTEP,                          \
258         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
259         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
260         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
261         .vsel_mask      = MAX77686_VSEL_MASK,                           \
262         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
263         .enable_mask    = MAX77686_OPMODE_MASK                          \
264                         << MAX77686_OPMODE_SHIFT,                       \
265 }
266 #define regulator_desc_ldo_low(num)             {                       \
267         .name           = "LDO"#num,                                    \
268         .id             = MAX77686_LDO##num,                            \
269         .ops            = &max77686_ldo_ops,                            \
270         .type           = REGULATOR_VOLTAGE,                            \
271         .owner          = THIS_MODULE,                                  \
272         .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
273         .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
274         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
275         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
276         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
277         .vsel_mask      = MAX77686_VSEL_MASK,                           \
278         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
279         .enable_mask    = MAX77686_OPMODE_MASK                          \
280                         << MAX77686_OPMODE_SHIFT,                       \
281 }
282 #define regulator_desc_ldo1_low(num)            {                       \
283         .name           = "LDO"#num,                                    \
284         .id             = MAX77686_LDO##num,                            \
285         .ops            = &max77686_ops,                                \
286         .type           = REGULATOR_VOLTAGE,                            \
287         .owner          = THIS_MODULE,                                  \
288         .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
289         .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
290         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
291         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
292         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
293         .vsel_mask      = MAX77686_VSEL_MASK,                           \
294         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
295         .enable_mask    = MAX77686_OPMODE_MASK                          \
296                         << MAX77686_OPMODE_SHIFT,                       \
297 }
298 #define regulator_desc_buck(num)                {                       \
299         .name           = "BUCK"#num,                                   \
300         .id             = MAX77686_BUCK##num,                           \
301         .ops            = &max77686_ops,                                \
302         .type           = REGULATOR_VOLTAGE,                            \
303         .owner          = THIS_MODULE,                                  \
304         .min_uV         = MAX77686_BUCK_MINUV,                          \
305         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
306         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
307         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
308         .vsel_reg       = MAX77686_REG_BUCK5OUT + (num - 5) * 2,        \
309         .vsel_mask      = MAX77686_VSEL_MASK,                           \
310         .enable_reg     = MAX77686_REG_BUCK5CTRL + (num - 5) * 2,       \
311         .enable_mask    = MAX77686_OPMODE_MASK,                         \
312 }
313 #define regulator_desc_buck1(num)               {                       \
314         .name           = "BUCK"#num,                                   \
315         .id             = MAX77686_BUCK##num,                           \
316         .ops            = &max77686_buck1_ops,                          \
317         .type           = REGULATOR_VOLTAGE,                            \
318         .owner          = THIS_MODULE,                                  \
319         .min_uV         = MAX77686_BUCK_MINUV,                          \
320         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
321         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
322         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
323         .vsel_reg       = MAX77686_REG_BUCK1OUT,                        \
324         .vsel_mask      = MAX77686_VSEL_MASK,                           \
325         .enable_reg     = MAX77686_REG_BUCK1CTRL,                       \
326         .enable_mask    = MAX77686_OPMODE_MASK,                         \
327 }
328 #define regulator_desc_buck_dvs(num)            {                       \
329         .name           = "BUCK"#num,                                   \
330         .id             = MAX77686_BUCK##num,                           \
331         .ops            = &max77686_buck_dvs_ops,                       \
332         .type           = REGULATOR_VOLTAGE,                            \
333         .owner          = THIS_MODULE,                                  \
334         .min_uV         = MAX77686_DVS_MINUV,                           \
335         .uV_step        = MAX77686_DVS_UVSTEP,                          \
336         .ramp_delay     = MAX77686_DVS_RAMP_DELAY,                      \
337         .n_voltages     = MAX77686_DVS_VSEL_MASK + 1,                   \
338         .vsel_reg       = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10,      \
339         .vsel_mask      = MAX77686_DVS_VSEL_MASK,                       \
340         .enable_reg     = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10,     \
341         .enable_mask    = MAX77686_OPMODE_MASK                          \
342                         << MAX77686_OPMODE_BUCK234_SHIFT,               \
343 }
344
345 static struct regulator_desc regulators[] = {
346         regulator_desc_ldo1_low(1),
347         regulator_desc_ldo_low(2),
348         regulator_desc_ldo(3),
349         regulator_desc_ldo(4),
350         regulator_desc_ldo(5),
351         regulator_desc_ldo_low(6),
352         regulator_desc_ldo_low(7),
353         regulator_desc_ldo_low(8),
354         regulator_desc_ldo(9),
355         regulator_desc_lpm_ldo(10),
356         regulator_desc_lpm_ldo(11),
357         regulator_desc_lpm_ldo(12),
358         regulator_desc_ldo(13),
359         regulator_desc_lpm_ldo(14),
360         regulator_desc_ldo_low(15),
361         regulator_desc_lpm_ldo(16),
362         regulator_desc_ldo(17),
363         regulator_desc_ldo(18),
364         regulator_desc_ldo(19),
365         regulator_desc_ldo(20),
366         regulator_desc_ldo(21),
367         regulator_desc_ldo(22),
368         regulator_desc_ldo(23),
369         regulator_desc_ldo(24),
370         regulator_desc_ldo(25),
371         regulator_desc_ldo(26),
372         regulator_desc_buck1(1),
373         regulator_desc_buck_dvs(2),
374         regulator_desc_buck_dvs(3),
375         regulator_desc_buck_dvs(4),
376         regulator_desc_buck(5),
377         regulator_desc_buck(6),
378         regulator_desc_buck(7),
379         regulator_desc_buck(8),
380         regulator_desc_buck(9),
381 };
382
383 #ifdef CONFIG_OF
384 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
385                                         struct max77686_platform_data *pdata)
386 {
387         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
388         struct device_node *pmic_np, *regulators_np;
389         struct max77686_regulator_data *rdata;
390         struct of_regulator_match rmatch;
391         unsigned int i;
392
393         pmic_np = iodev->dev->of_node;
394         regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
395         if (!regulators_np) {
396                 dev_err(&pdev->dev, "could not find regulators sub-node\n");
397                 return -EINVAL;
398         }
399
400         pdata->num_regulators = ARRAY_SIZE(regulators);
401         rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
402                              pdata->num_regulators, GFP_KERNEL);
403         if (!rdata) {
404                 dev_err(&pdev->dev,
405                         "could not allocate memory for regulator data\n");
406                 return -ENOMEM;
407         }
408
409         for (i = 0; i < pdata->num_regulators; i++) {
410                 rmatch.name = regulators[i].name;
411                 rmatch.init_data = NULL;
412                 rmatch.of_node = NULL;
413                 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
414                 rdata[i].initdata = rmatch.init_data;
415                 rdata[i].of_node = rmatch.of_node;
416         }
417
418         pdata->regulators = rdata;
419
420         return 0;
421 }
422 #else
423 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
424                                         struct max77686_platform_data *pdata)
425 {
426         return 0;
427 }
428 #endif /* CONFIG_OF */
429
430 static int max77686_pmic_probe(struct platform_device *pdev)
431 {
432         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
433         struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
434         struct max77686_data *max77686;
435         int i, ret = 0;
436         struct regulator_config config = { };
437
438         dev_dbg(&pdev->dev, "%s\n", __func__);
439
440         if (!pdata) {
441                 dev_err(&pdev->dev, "no platform data found for regulator\n");
442                 return -ENODEV;
443         }
444
445         if (iodev->dev->of_node) {
446                 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
447                 if (ret)
448                         return ret;
449         }
450
451         if (pdata->num_regulators != MAX77686_REGULATORS) {
452                 dev_err(&pdev->dev,
453                         "Invalid initial data for regulator's initialiation\n");
454                 return -EINVAL;
455         }
456
457         max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
458                                 GFP_KERNEL);
459         if (!max77686)
460                 return -ENOMEM;
461
462         config.dev = &pdev->dev;
463         config.regmap = iodev->regmap;
464         config.driver_data = max77686;
465         platform_set_drvdata(pdev, max77686);
466
467         for (i = 0; i < MAX77686_REGULATORS; i++) {
468                 config.init_data = pdata->regulators[i].initdata;
469                 config.of_node = pdata->regulators[i].of_node;
470
471                 max77686->opmode[i] = regulators[i].enable_mask;
472                 max77686->rdev[i] = regulator_register(&regulators[i], &config);
473                 if (IS_ERR(max77686->rdev[i])) {
474                         ret = PTR_ERR(max77686->rdev[i]);
475                         dev_err(&pdev->dev,
476                                 "regulator init failed for %d\n", i);
477                         max77686->rdev[i] = NULL;
478                         goto err;
479                 }
480         }
481
482         return 0;
483 err:
484         while (--i >= 0)
485                 regulator_unregister(max77686->rdev[i]);
486         return ret;
487 }
488
489 static int max77686_pmic_remove(struct platform_device *pdev)
490 {
491         struct max77686_data *max77686 = platform_get_drvdata(pdev);
492         int i;
493
494         for (i = 0; i < MAX77686_REGULATORS; i++)
495                 regulator_unregister(max77686->rdev[i]);
496
497         return 0;
498 }
499
500 static const struct platform_device_id max77686_pmic_id[] = {
501         {"max77686-pmic", 0},
502         { },
503 };
504 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
505
506 static struct platform_driver max77686_pmic_driver = {
507         .driver = {
508                 .name = "max77686-pmic",
509                 .owner = THIS_MODULE,
510         },
511         .probe = max77686_pmic_probe,
512         .remove = max77686_pmic_remove,
513         .id_table = max77686_pmic_id,
514 };
515
516 static int __init max77686_pmic_init(void)
517 {
518         return platform_driver_register(&max77686_pmic_driver);
519 }
520 subsys_initcall(max77686_pmic_init);
521
522 static void __exit max77686_pmic_cleanup(void)
523 {
524         platform_driver_unregister(&max77686_pmic_driver);
525 }
526 module_exit(max77686_pmic_cleanup);
527
528 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
529 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
530 MODULE_LICENSE("GPL");