8425c0d520a8d47b263a24539acf54409d435fe8
[platform/kernel/linux-starfive.git] / drivers / regulator / max77802.c
1 /*
2  * max77802.c - Regulator driver for the Maxim 77802
3  *
4  * Copyright (C) 2013-2014 Google, Inc
5  * Simon Glass <sjg@chromium.org>
6  *
7  * Copyright (C) 2012 Samsung Electronics
8  * Chiwoong Byun <woong.byun@smasung.com>
9  * Jonghwa Lee <jonghwa3.lee@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * This driver is based on max8997.c
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/bug.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/mfd/max77686.h>
35 #include <linux/mfd/max77686-private.h>
36
37 /* Default ramp delay in case it is not manually set */
38 #define MAX77802_RAMP_DELAY             100000          /* uV/us */
39
40 #define MAX77802_OPMODE_SHIFT_LDO       6
41 #define MAX77802_OPMODE_BUCK234_SHIFT   4
42 #define MAX77802_OPMODE_MASK            0x3
43
44 #define MAX77802_VSEL_MASK              0x3F
45 #define MAX77802_DVS_VSEL_MASK          0xFF
46
47 #define MAX77802_RAMP_RATE_MASK_2BIT    0xC0
48 #define MAX77802_RAMP_RATE_SHIFT_2BIT   6
49 #define MAX77802_RAMP_RATE_MASK_4BIT    0xF0
50 #define MAX77802_RAMP_RATE_SHIFT_4BIT   4
51
52 #define MAX77802_OFF_PWRREQ             0x1
53
54 /* MAX77802 has two register formats: 2-bit and 4-bit */
55 static const unsigned int ramp_table_77802_2bit[] = {
56         12500,
57         25000,
58         50000,
59         100000,
60 };
61
62 static unsigned int ramp_table_77802_4bit[] = {
63         1000,   2000,   3030,   4000,
64         5000,   5880,   7140,   8330,
65         9090,   10000,  11110,  12500,
66         16670,  25000,  50000,  100000,
67 };
68
69 struct max77802_regulator_prv {
70         unsigned int opmode[MAX77802_REG_MAX];
71 };
72
73 static int max77802_get_opmode_shift(int id)
74 {
75         if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
76                                      id <= MAX77802_BUCK10))
77                 return 0;
78
79         if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
80                 return MAX77802_OPMODE_BUCK234_SHIFT;
81
82         if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
83                 return MAX77802_OPMODE_SHIFT_LDO;
84
85         return -EINVAL;
86 }
87
88 /**
89  * max77802_set_suspend_disable - Disable the regulator during system suspend
90  * @rdev: regulator to mark as disabled
91  *
92  * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
93  * Configure the regulator so the PMIC will turn it OFF during system suspend.
94  */
95 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
96 {
97         unsigned int val = MAX77802_OFF_PWRREQ;
98         struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
99         int id = rdev_get_id(rdev);
100         int shift = max77802_get_opmode_shift(id);
101
102         max77802->opmode[id] = val;
103         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
104                                   rdev->desc->enable_mask, val << shift);
105 }
106
107 /*
108  * Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state
109  * (Enable Control Logic1 by PWRREQ)
110  *
111  * LDOs 2, 4-19, 22-35.
112  *
113  */
114 static int max77802_ldo_set_suspend_mode_logic1(struct regulator_dev *rdev,
115                                                 unsigned int mode)
116 {
117         struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
118         int id = rdev_get_id(rdev);
119         unsigned int val;
120         int shift = max77802_get_opmode_shift(id);
121
122         switch (mode) {
123         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
124                 val = MAX77802_OPMODE_LP;
125                 break;
126         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
127                 val = MAX77802_OPMODE_NORMAL;
128                 break;
129         case REGULATOR_MODE_STANDBY:                    /* ON/OFF by PWRREQ */
130                 val = MAX77802_OPMODE_STANDBY;
131                 break;
132         default:
133                 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
134                          rdev->desc->name, mode);
135                 return -EINVAL;
136         }
137
138         max77802->opmode[id] = val;
139         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
140                                   rdev->desc->enable_mask, val << shift);
141 }
142
143 /*
144  * Mode 1 (Output[ON/OFF] by PWRREQ) is not supported on some LDOs
145  * (Enable Control Logic2 by PWRREQ)
146  *
147  * LDOs 1, 20, 21, and 3,
148  *
149  */
150 static int max77802_ldo_set_suspend_mode_logic2(struct regulator_dev *rdev,
151                                                 unsigned int mode)
152 {
153         struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
154         int id = rdev_get_id(rdev);
155         unsigned int val;
156         int shift = max77802_get_opmode_shift(id);
157
158         switch (mode) {
159         case REGULATOR_MODE_IDLE:                       /* ON in LP Mode */
160                 val = MAX77802_OPMODE_LP;
161                 break;
162         case REGULATOR_MODE_NORMAL:                     /* ON in Normal Mode */
163                 val = MAX77802_OPMODE_NORMAL;
164                 break;
165         default:
166                 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
167                          rdev->desc->name, mode);
168                 return -EINVAL;
169         }
170
171         max77802->opmode[id] = val;
172         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
173                                   rdev->desc->enable_mask, val << shift);
174 }
175
176 static int max77802_enable(struct regulator_dev *rdev)
177 {
178         struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
179         int id = rdev_get_id(rdev);
180         int shift = max77802_get_opmode_shift(id);
181
182         if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
183                 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
184
185         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
186                                   rdev->desc->enable_mask,
187                                   max77802->opmode[id] << shift);
188 }
189
190 static int max77802_find_ramp_value(struct regulator_dev *rdev,
191                                     const unsigned int limits[], int size,
192                                     unsigned int ramp_delay)
193 {
194         int i;
195
196         for (i = 0; i < size; i++) {
197                 if (ramp_delay <= limits[i])
198                         return i;
199         }
200
201         /* Use maximum value for no ramp control */
202         dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
203                  rdev->desc->name, ramp_delay);
204         return size - 1;
205 }
206
207 /* Used for BUCKs 2-4 */
208 static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
209                                         int ramp_delay)
210 {
211         int id = rdev_get_id(rdev);
212         unsigned int ramp_value;
213
214         if (id > MAX77802_BUCK4) {
215                         dev_warn(&rdev->dev,
216                                  "%s: regulator: ramp delay not supported\n",
217                                  rdev->desc->name);
218                 return -EINVAL;
219         }
220         ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
221                                 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
222
223         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
224                                   MAX77802_RAMP_RATE_MASK_2BIT,
225                                   ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
226 }
227
228 /* For BUCK1, 6 */
229 static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
230                                             int ramp_delay)
231 {
232         unsigned int ramp_value;
233
234         ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
235                                 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
236
237         return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
238                                   MAX77802_RAMP_RATE_MASK_4BIT,
239                                   ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
240 }
241
242 /*
243  * LDOs 2, 4-19, 22-35
244  */
245 static struct regulator_ops max77802_ldo_ops_logic1 = {
246         .list_voltage           = regulator_list_voltage_linear,
247         .map_voltage            = regulator_map_voltage_linear,
248         .is_enabled             = regulator_is_enabled_regmap,
249         .enable                 = max77802_enable,
250         .disable                = regulator_disable_regmap,
251         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
252         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
253         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
254         .set_suspend_enable     = max77802_enable,
255         .set_suspend_disable    = max77802_set_suspend_disable,
256         .set_suspend_mode       = max77802_ldo_set_suspend_mode_logic1,
257 };
258
259 /*
260  * LDOs 1, 20, 21, 3
261  */
262 static struct regulator_ops max77802_ldo_ops_logic2 = {
263         .list_voltage           = regulator_list_voltage_linear,
264         .map_voltage            = regulator_map_voltage_linear,
265         .is_enabled             = regulator_is_enabled_regmap,
266         .enable                 = max77802_enable,
267         .disable                = regulator_disable_regmap,
268         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
269         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
270         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
271         .set_suspend_mode       = max77802_ldo_set_suspend_mode_logic2,
272 };
273
274 /* BUCKS 1, 6 */
275 static struct regulator_ops max77802_buck_16_dvs_ops = {
276         .list_voltage           = regulator_list_voltage_linear,
277         .map_voltage            = regulator_map_voltage_linear,
278         .is_enabled             = regulator_is_enabled_regmap,
279         .enable                 = max77802_enable,
280         .disable                = regulator_disable_regmap,
281         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
282         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
283         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
284         .set_ramp_delay         = max77802_set_ramp_delay_4bit,
285         .set_suspend_enable     = max77802_enable,
286         .set_suspend_disable    = max77802_set_suspend_disable,
287 };
288
289 /* BUCKs 2-4 */
290 static struct regulator_ops max77802_buck_234_ops = {
291         .list_voltage           = regulator_list_voltage_linear,
292         .map_voltage            = regulator_map_voltage_linear,
293         .is_enabled             = regulator_is_enabled_regmap,
294         .enable                 = max77802_enable,
295         .disable                = regulator_disable_regmap,
296         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
297         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
298         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
299         .set_ramp_delay         = max77802_set_ramp_delay_2bit,
300         .set_suspend_enable     = max77802_enable,
301         .set_suspend_disable    = max77802_set_suspend_disable,
302 };
303
304 /* BUCKs 5, 7-10 */
305 static struct regulator_ops max77802_buck_dvs_ops = {
306         .list_voltage           = regulator_list_voltage_linear,
307         .map_voltage            = regulator_map_voltage_linear,
308         .is_enabled             = regulator_is_enabled_regmap,
309         .enable                 = max77802_enable,
310         .disable                = regulator_disable_regmap,
311         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
312         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
313         .set_voltage_time_sel   = regulator_set_voltage_time_sel,
314         .set_ramp_delay         = max77802_set_ramp_delay_2bit,
315         .set_suspend_enable     = max77802_enable,
316         .set_suspend_disable    = max77802_set_suspend_disable,
317 };
318
319 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
320 #define regulator_77802_desc_p_ldo(num, supply, log)    {               \
321         .name           = "LDO"#num,                                    \
322         .id             = MAX77802_LDO##num,                            \
323         .supply_name    = "inl"#supply,                                 \
324         .ops            = &max77802_ldo_ops_logic##log,                 \
325         .type           = REGULATOR_VOLTAGE,                            \
326         .owner          = THIS_MODULE,                                  \
327         .min_uV         = 800000,                                       \
328         .uV_step        = 50000,                                        \
329         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
330         .n_voltages     = 1 << 6,                                       \
331         .vsel_reg       = MAX77802_REG_LDO1CTRL1 + num - 1,             \
332         .vsel_mask      = MAX77802_VSEL_MASK,                           \
333         .enable_reg     = MAX77802_REG_LDO1CTRL1 + num - 1,             \
334         .enable_mask    = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
335 }
336
337 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
338 #define regulator_77802_desc_n_ldo(num, supply, log)   {                \
339         .name           = "LDO"#num,                                    \
340         .id             = MAX77802_LDO##num,                            \
341         .supply_name    = "inl"#supply,                                 \
342         .ops            = &max77802_ldo_ops_logic##log,                 \
343         .type           = REGULATOR_VOLTAGE,                            \
344         .owner          = THIS_MODULE,                                  \
345         .min_uV         = 800000,                                       \
346         .uV_step        = 25000,                                        \
347         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
348         .n_voltages     = 1 << 6,                                       \
349         .vsel_reg       = MAX77802_REG_LDO1CTRL1 + num - 1,             \
350         .vsel_mask      = MAX77802_VSEL_MASK,                           \
351         .enable_reg     = MAX77802_REG_LDO1CTRL1 + num - 1,             \
352         .enable_mask    = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
353 }
354
355 /* BUCKs 1, 6 */
356 #define regulator_77802_desc_16_buck(num)       {               \
357         .name           = "BUCK"#num,                                   \
358         .id             = MAX77802_BUCK##num,                           \
359         .supply_name    = "inb"#num,                                    \
360         .ops            = &max77802_buck_16_dvs_ops,                    \
361         .type           = REGULATOR_VOLTAGE,                            \
362         .owner          = THIS_MODULE,                                  \
363         .min_uV         = 612500,                                       \
364         .uV_step        = 6250,                                         \
365         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
366         .n_voltages     = 1 << 8,                                       \
367         .vsel_reg       = MAX77802_REG_BUCK ## num ## DVS1,             \
368         .vsel_mask      = MAX77802_DVS_VSEL_MASK,                       \
369         .enable_reg     = MAX77802_REG_BUCK ## num ## CTRL,             \
370         .enable_mask    = MAX77802_OPMODE_MASK,                         \
371 }
372
373 /* BUCKS 2-4 */
374 #define regulator_77802_desc_234_buck(num)      {               \
375         .name           = "BUCK"#num,                                   \
376         .id             = MAX77802_BUCK##num,                           \
377         .supply_name    = "inb"#num,                                    \
378         .ops            = &max77802_buck_234_ops,                       \
379         .type           = REGULATOR_VOLTAGE,                            \
380         .owner          = THIS_MODULE,                                  \
381         .min_uV         = 600000,                                       \
382         .uV_step        = 6250,                                         \
383         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
384         .n_voltages     = 0x91,                                         \
385         .vsel_reg       = MAX77802_REG_BUCK ## num ## DVS1,             \
386         .vsel_mask      = MAX77802_DVS_VSEL_MASK,                       \
387         .enable_reg     = MAX77802_REG_BUCK ## num ## CTRL1,            \
388         .enable_mask    = MAX77802_OPMODE_MASK <<                       \
389                                 MAX77802_OPMODE_BUCK234_SHIFT,          \
390 }
391
392 /* BUCK 5 */
393 #define regulator_77802_desc_buck5(num)         {               \
394         .name           = "BUCK"#num,                                   \
395         .id             = MAX77802_BUCK##num,                           \
396         .supply_name    = "inb"#num,                                    \
397         .ops            = &max77802_buck_dvs_ops,                       \
398         .type           = REGULATOR_VOLTAGE,                            \
399         .owner          = THIS_MODULE,                                  \
400         .min_uV         = 750000,                                       \
401         .uV_step        = 50000,                                        \
402         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
403         .n_voltages     = 1 << 6,                                       \
404         .vsel_reg       = MAX77802_REG_BUCK5OUT,                        \
405         .vsel_mask      = MAX77802_VSEL_MASK,                           \
406         .enable_reg     = MAX77802_REG_BUCK5CTRL,                       \
407         .enable_mask    = MAX77802_OPMODE_MASK,                         \
408 }
409
410 /* BUCKs 7-10 */
411 #define regulator_77802_desc_buck7_10(num)      {               \
412         .name           = "BUCK"#num,                                   \
413         .id             = MAX77802_BUCK##num,                           \
414         .supply_name    = "inb"#num,                                    \
415         .ops            = &max77802_buck_dvs_ops,                       \
416         .type           = REGULATOR_VOLTAGE,                            \
417         .owner          = THIS_MODULE,                                  \
418         .min_uV         = 750000,                                       \
419         .uV_step        = 50000,                                        \
420         .ramp_delay     = MAX77802_RAMP_DELAY,                          \
421         .n_voltages     = 1 << 6,                                       \
422         .vsel_reg       = MAX77802_REG_BUCK7OUT + (num - 7) * 3,        \
423         .vsel_mask      = MAX77802_VSEL_MASK,                           \
424         .enable_reg     = MAX77802_REG_BUCK7CTRL + (num - 7) * 3,       \
425         .enable_mask    = MAX77802_OPMODE_MASK,                         \
426 }
427
428 static struct regulator_desc regulators[] = {
429         regulator_77802_desc_16_buck(1),
430         regulator_77802_desc_234_buck(2),
431         regulator_77802_desc_234_buck(3),
432         regulator_77802_desc_234_buck(4),
433         regulator_77802_desc_buck5(5),
434         regulator_77802_desc_16_buck(6),
435         regulator_77802_desc_buck7_10(7),
436         regulator_77802_desc_buck7_10(8),
437         regulator_77802_desc_buck7_10(9),
438         regulator_77802_desc_buck7_10(10),
439         regulator_77802_desc_n_ldo(1, 10, 2),
440         regulator_77802_desc_n_ldo(2, 10, 1),
441         regulator_77802_desc_p_ldo(3, 3, 2),
442         regulator_77802_desc_p_ldo(4, 6, 1),
443         regulator_77802_desc_p_ldo(5, 3, 1),
444         regulator_77802_desc_p_ldo(6, 3, 1),
445         regulator_77802_desc_p_ldo(7, 3, 1),
446         regulator_77802_desc_n_ldo(8, 1, 1),
447         regulator_77802_desc_p_ldo(9, 5, 1),
448         regulator_77802_desc_p_ldo(10, 4, 1),
449         regulator_77802_desc_p_ldo(11, 4, 1),
450         regulator_77802_desc_p_ldo(12, 9, 1),
451         regulator_77802_desc_p_ldo(13, 4, 1),
452         regulator_77802_desc_p_ldo(14, 4, 1),
453         regulator_77802_desc_n_ldo(15, 1, 1),
454         regulator_77802_desc_n_ldo(17, 2, 1),
455         regulator_77802_desc_p_ldo(18, 7, 1),
456         regulator_77802_desc_p_ldo(19, 5, 1),
457         regulator_77802_desc_p_ldo(20, 7, 2),
458         regulator_77802_desc_p_ldo(21, 6, 2),
459         regulator_77802_desc_p_ldo(23, 9, 1),
460         regulator_77802_desc_p_ldo(24, 6, 1),
461         regulator_77802_desc_p_ldo(25, 9, 1),
462         regulator_77802_desc_p_ldo(26, 9, 1),
463         regulator_77802_desc_n_ldo(27, 2, 1),
464         regulator_77802_desc_p_ldo(28, 7, 1),
465         regulator_77802_desc_p_ldo(29, 7, 1),
466         regulator_77802_desc_n_ldo(30, 2, 1),
467         regulator_77802_desc_p_ldo(32, 9, 1),
468         regulator_77802_desc_p_ldo(33, 6, 1),
469         regulator_77802_desc_p_ldo(34, 9, 1),
470         regulator_77802_desc_n_ldo(35, 2, 1),
471 };
472
473 #ifdef CONFIG_OF
474 static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
475                                         struct max77686_platform_data *pdata)
476 {
477         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
478         struct device_node *pmic_np, *regulators_np;
479         struct max77686_regulator_data *rdata;
480         struct of_regulator_match rmatch;
481         unsigned int i;
482
483         pmic_np = iodev->dev->of_node;
484         regulators_np = of_get_child_by_name(pmic_np, "regulators");
485         if (!regulators_np) {
486                 dev_err(&pdev->dev, "could not find regulators sub-node\n");
487                 return -EINVAL;
488         }
489
490         pdata->num_regulators = ARRAY_SIZE(regulators);
491         rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
492                              pdata->num_regulators, GFP_KERNEL);
493         if (!rdata) {
494                 of_node_put(regulators_np);
495                 return -ENOMEM;
496         }
497
498         for (i = 0; i < pdata->num_regulators; i++) {
499                 rmatch.name = regulators[i].name;
500                 rmatch.init_data = NULL;
501                 rmatch.of_node = NULL;
502                 if (of_regulator_match(&pdev->dev, regulators_np, &rmatch,
503                                        1) != 1) {
504                         dev_warn(&pdev->dev, "No matching regulator for '%s'\n",
505                                  rmatch.name);
506                         continue;
507                 }
508                 rdata[i].initdata = rmatch.init_data;
509                 rdata[i].of_node = rmatch.of_node;
510                 rdata[i].id = regulators[i].id;
511         }
512
513         pdata->regulators = rdata;
514         of_node_put(regulators_np);
515
516         return 0;
517 }
518 #else
519 static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
520                                         struct max77686_platform_data *pdata)
521 {
522         return 0;
523 }
524 #endif /* CONFIG_OF */
525
526 static int max77802_pmic_probe(struct platform_device *pdev)
527 {
528         struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
529         struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
530         struct max77802_regulator_prv *max77802;
531         int i, ret = 0, val;
532         struct regulator_config config = { };
533
534         /* This is allocated by the MFD driver */
535         if (!pdata) {
536                 dev_err(&pdev->dev, "no platform data found for regulator\n");
537                 return -ENODEV;
538         }
539
540         max77802 = devm_kzalloc(&pdev->dev,
541                                 sizeof(struct max77802_regulator_prv),
542                                 GFP_KERNEL);
543         if (!max77802)
544                 return -ENOMEM;
545
546         if (iodev->dev->of_node) {
547                 ret = max77802_pmic_dt_parse_pdata(pdev, pdata);
548                 if (ret)
549                         return ret;
550         }
551
552         config.dev = iodev->dev;
553         config.regmap = iodev->regmap;
554         config.driver_data = max77802;
555         platform_set_drvdata(pdev, max77802);
556
557         for (i = 0; i < MAX77802_REG_MAX; i++) {
558                 struct regulator_dev *rdev;
559                 int id = pdata->regulators[i].id;
560                 int shift = max77802_get_opmode_shift(id);
561
562                 config.init_data = pdata->regulators[i].initdata;
563                 config.of_node = pdata->regulators[i].of_node;
564
565                 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
566                 val = val >> shift & MAX77802_OPMODE_MASK;
567
568                 /*
569                  * If the regulator is disabled and the system warm rebooted,
570                  * the hardware reports OFF as the regulator operating mode.
571                  * Default to operating mode NORMAL in that case.
572                  */
573                 if (val == MAX77802_OPMODE_OFF)
574                         max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
575                 else
576                         max77802->opmode[id] = val;
577
578                 rdev = devm_regulator_register(&pdev->dev,
579                                                &regulators[i], &config);
580                 if (IS_ERR(rdev)) {
581                         dev_err(&pdev->dev,
582                                 "regulator init failed for %d\n", i);
583                         return PTR_ERR(rdev);
584                 }
585         }
586
587         return 0;
588 }
589
590 static const struct platform_device_id max77802_pmic_id[] = {
591         {"max77802-pmic", 0},
592         { },
593 };
594 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
595
596 static struct platform_driver max77802_pmic_driver = {
597         .driver = {
598                 .name = "max77802-pmic",
599                 .owner = THIS_MODULE,
600         },
601         .probe = max77802_pmic_probe,
602         .id_table = max77802_pmic_id,
603 };
604
605 module_platform_driver(max77802_pmic_driver);
606
607 MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
608 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
609 MODULE_LICENSE("GPL");