Merge tag 'v3.7-rc1' into staging/for_v3.8
[platform/adaptation/renesas_rcar/renesas_kernel.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 };
71
72 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
73 {
74         unsigned int ramp_value = RAMP_RATE_NO_CTRL;
75
76         switch (ramp_delay) {
77         case 1 ... 13750:
78                 ramp_value = RAMP_RATE_13P75MV;
79                 break;
80         case 13751 ... 27500:
81                 ramp_value = RAMP_RATE_27P5MV;
82                 break;
83         case 27501 ... 55000:
84                 ramp_value = RAMP_RATE_55MV;
85                 break;
86         case 55001 ... 100000:
87                 break;
88         default:
89                 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
90                         rdev->desc->name, ramp_delay);
91         }
92
93         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
94                                   MAX77686_RAMP_RATE_MASK, ramp_value << 6);
95 }
96
97 static struct regulator_ops max77686_ops = {
98         .list_voltage           = regulator_list_voltage_linear,
99         .map_voltage            = regulator_map_voltage_linear,
100         .is_enabled             = regulator_is_enabled_regmap,
101         .enable                 = regulator_enable_regmap,
102         .disable                = regulator_disable_regmap,
103         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
104         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
105         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
106 };
107
108 static struct regulator_ops max77686_buck_dvs_ops = {
109         .list_voltage           = regulator_list_voltage_linear,
110         .map_voltage            = regulator_map_voltage_linear,
111         .is_enabled             = regulator_is_enabled_regmap,
112         .enable                 = regulator_enable_regmap,
113         .disable                = regulator_disable_regmap,
114         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
115         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
116         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
117         .set_ramp_delay         = max77686_set_ramp_delay,
118 };
119
120 #define regulator_desc_ldo(num)         {                               \
121         .name           = "LDO"#num,                                    \
122         .id             = MAX77686_LDO##num,                            \
123         .ops            = &max77686_ops,                                \
124         .type           = REGULATOR_VOLTAGE,                            \
125         .owner          = THIS_MODULE,                                  \
126         .min_uV         = MAX77686_LDO_MINUV,                           \
127         .uV_step        = MAX77686_LDO_UVSTEP,                          \
128         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
129         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
130         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
131         .vsel_mask      = MAX77686_VSEL_MASK,                           \
132         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
133         .enable_mask    = MAX77686_OPMODE_MASK                          \
134                         << MAX77686_OPMODE_SHIFT,                       \
135 }
136 #define regulator_desc_ldo_low(num)             {                       \
137         .name           = "LDO"#num,                                    \
138         .id             = MAX77686_LDO##num,                            \
139         .ops            = &max77686_ops,                                \
140         .type           = REGULATOR_VOLTAGE,                            \
141         .owner          = THIS_MODULE,                                  \
142         .min_uV         = MAX77686_LDO_LOW_MINUV,                       \
143         .uV_step        = MAX77686_LDO_LOW_UVSTEP,                      \
144         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
145         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
146         .vsel_reg       = MAX77686_REG_LDO1CTRL1 + num - 1,             \
147         .vsel_mask      = MAX77686_VSEL_MASK,                           \
148         .enable_reg     = MAX77686_REG_LDO1CTRL1 + num - 1,             \
149         .enable_mask    = MAX77686_OPMODE_MASK                          \
150                         << MAX77686_OPMODE_SHIFT,                       \
151 }
152 #define regulator_desc_buck(num)                {                       \
153         .name           = "BUCK"#num,                                   \
154         .id             = MAX77686_BUCK##num,                           \
155         .ops            = &max77686_ops,                                \
156         .type           = REGULATOR_VOLTAGE,                            \
157         .owner          = THIS_MODULE,                                  \
158         .min_uV         = MAX77686_BUCK_MINUV,                          \
159         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
160         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
161         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
162         .vsel_reg       = MAX77686_REG_BUCK5OUT + (num - 5) * 2,        \
163         .vsel_mask      = MAX77686_VSEL_MASK,                           \
164         .enable_reg     = MAX77686_REG_BUCK5CTRL + (num - 5) * 2,       \
165         .enable_mask    = MAX77686_OPMODE_MASK,                         \
166 }
167 #define regulator_desc_buck1(num)               {                       \
168         .name           = "BUCK"#num,                                   \
169         .id             = MAX77686_BUCK##num,                           \
170         .ops            = &max77686_ops,                                \
171         .type           = REGULATOR_VOLTAGE,                            \
172         .owner          = THIS_MODULE,                                  \
173         .min_uV         = MAX77686_BUCK_MINUV,                          \
174         .uV_step        = MAX77686_BUCK_UVSTEP,                         \
175         .ramp_delay     = MAX77686_RAMP_DELAY,                          \
176         .n_voltages     = MAX77686_VSEL_MASK + 1,                       \
177         .vsel_reg       = MAX77686_REG_BUCK1OUT,                        \
178         .vsel_mask      = MAX77686_VSEL_MASK,                           \
179         .enable_reg     = MAX77686_REG_BUCK1CTRL,                       \
180         .enable_mask    = MAX77686_OPMODE_MASK,                         \
181 }
182 #define regulator_desc_buck_dvs(num)            {                       \
183         .name           = "BUCK"#num,                                   \
184         .id             = MAX77686_BUCK##num,                           \
185         .ops            = &max77686_buck_dvs_ops,                       \
186         .type           = REGULATOR_VOLTAGE,                            \
187         .owner          = THIS_MODULE,                                  \
188         .min_uV         = MAX77686_DVS_MINUV,                           \
189         .uV_step        = MAX77686_DVS_UVSTEP,                          \
190         .ramp_delay     = MAX77686_DVS_RAMP_DELAY,                      \
191         .n_voltages     = MAX77686_DVS_VSEL_MASK + 1,                   \
192         .vsel_reg       = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10,      \
193         .vsel_mask      = MAX77686_DVS_VSEL_MASK,                       \
194         .enable_reg     = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10,     \
195         .enable_mask    = MAX77686_OPMODE_MASK                          \
196                         << MAX77686_OPMODE_BUCK234_SHIFT,               \
197 }
198
199 static struct regulator_desc regulators[] = {
200         regulator_desc_ldo_low(1),
201         regulator_desc_ldo_low(2),
202         regulator_desc_ldo(3),
203         regulator_desc_ldo(4),
204         regulator_desc_ldo(5),
205         regulator_desc_ldo_low(6),
206         regulator_desc_ldo_low(7),
207         regulator_desc_ldo_low(8),
208         regulator_desc_ldo(9),
209         regulator_desc_ldo(10),
210         regulator_desc_ldo(11),
211         regulator_desc_ldo(12),
212         regulator_desc_ldo(13),
213         regulator_desc_ldo(14),
214         regulator_desc_ldo_low(15),
215         regulator_desc_ldo(16),
216         regulator_desc_ldo(17),
217         regulator_desc_ldo(18),
218         regulator_desc_ldo(19),
219         regulator_desc_ldo(20),
220         regulator_desc_ldo(21),
221         regulator_desc_ldo(22),
222         regulator_desc_ldo(23),
223         regulator_desc_ldo(24),
224         regulator_desc_ldo(25),
225         regulator_desc_ldo(26),
226         regulator_desc_buck1(1),
227         regulator_desc_buck_dvs(2),
228         regulator_desc_buck_dvs(3),
229         regulator_desc_buck_dvs(4),
230         regulator_desc_buck(5),
231         regulator_desc_buck(6),
232         regulator_desc_buck(7),
233         regulator_desc_buck(8),
234         regulator_desc_buck(9),
235 };
236
237 #ifdef CONFIG_OF
238 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
239                                         struct max77686_platform_data *pdata)
240 {
241         struct device_node *pmic_np, *regulators_np;
242         struct max77686_regulator_data *rdata;
243         struct of_regulator_match rmatch;
244         unsigned int i;
245
246         pmic_np = iodev->dev->of_node;
247         regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
248         if (!regulators_np) {
249                 dev_err(iodev->dev, "could not find regulators sub-node\n");
250                 return -EINVAL;
251         }
252
253         pdata->num_regulators = ARRAY_SIZE(regulators);
254         rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
255                              pdata->num_regulators, GFP_KERNEL);
256         if (!rdata) {
257                 dev_err(iodev->dev,
258                         "could not allocate memory for regulator data\n");
259                 return -ENOMEM;
260         }
261
262         for (i = 0; i < pdata->num_regulators; i++) {
263                 rmatch.name = regulators[i].name;
264                 rmatch.init_data = NULL;
265                 rmatch.of_node = NULL;
266                 of_regulator_match(iodev->dev, regulators_np, &rmatch, 1);
267                 rdata[i].initdata = rmatch.init_data;
268                 rdata[i].of_node = rmatch.of_node;
269         }
270
271         pdata->regulators = rdata;
272
273         return 0;
274 }
275 #else
276 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
277                                         struct max77686_platform_data *pdata)
278 {
279         return 0;
280 }
281 #endif /* CONFIG_OF */
282
283 static __devinit int max77686_pmic_probe(struct platform_device *pdev)
284 {
285         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
286         struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
287         struct max77686_data *max77686;
288         int i, ret = 0;
289         struct regulator_config config = { };
290
291         dev_dbg(&pdev->dev, "%s\n", __func__);
292
293         if (!pdata) {
294                 dev_err(&pdev->dev, "no platform data found for regulator\n");
295                 return -ENODEV;
296         }
297
298         if (iodev->dev->of_node) {
299                 ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
300                 if (ret)
301                         return ret;
302         }
303
304         if (pdata->num_regulators != MAX77686_REGULATORS) {
305                 dev_err(&pdev->dev,
306                         "Invalid initial data for regulator's initialiation\n");
307                 return -EINVAL;
308         }
309
310         max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
311                                 GFP_KERNEL);
312         if (!max77686)
313                 return -ENOMEM;
314
315         config.dev = &pdev->dev;
316         config.regmap = iodev->regmap;
317         platform_set_drvdata(pdev, max77686);
318
319         for (i = 0; i < MAX77686_REGULATORS; i++) {
320                 config.init_data = pdata->regulators[i].initdata;
321                 config.of_node = pdata->regulators[i].of_node;
322
323                 max77686->rdev[i] = regulator_register(&regulators[i], &config);
324                 if (IS_ERR(max77686->rdev[i])) {
325                         ret = PTR_ERR(max77686->rdev[i]);
326                         dev_err(&pdev->dev,
327                                 "regulator init failed for %d\n", i);
328                         max77686->rdev[i] = NULL;
329                         goto err;
330                 }
331         }
332
333         return 0;
334 err:
335         while (--i >= 0)
336                 regulator_unregister(max77686->rdev[i]);
337         return ret;
338 }
339
340 static int __devexit max77686_pmic_remove(struct platform_device *pdev)
341 {
342         struct max77686_data *max77686 = platform_get_drvdata(pdev);
343         int i;
344
345         for (i = 0; i < MAX77686_REGULATORS; i++)
346                 regulator_unregister(max77686->rdev[i]);
347
348         return 0;
349 }
350
351 static const struct platform_device_id max77686_pmic_id[] = {
352         {"max77686-pmic", 0},
353         { },
354 };
355 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
356
357 static struct platform_driver max77686_pmic_driver = {
358         .driver = {
359                 .name = "max77686-pmic",
360                 .owner = THIS_MODULE,
361         },
362         .probe = max77686_pmic_probe,
363         .remove = __devexit_p(max77686_pmic_remove),
364         .id_table = max77686_pmic_id,
365 };
366
367 static int __init max77686_pmic_init(void)
368 {
369         return platform_driver_register(&max77686_pmic_driver);
370 }
371 subsys_initcall(max77686_pmic_init);
372
373 static void __exit max77686_pmic_cleanup(void)
374 {
375         platform_driver_unregister(&max77686_pmic_driver);
376 }
377 module_exit(max77686_pmic_cleanup);
378
379 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
380 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
381 MODULE_LICENSE("GPL");