UPSTREAM: clk: rockchip: release io resource when failing to init clk
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / ricoh619-regulator.c
1 /*
2  * drivers/regulator/ricoh619-regulator.c
3  *
4  * Regulator driver for RICOH619 power management chip.
5  *
6  * Copyright (C) 2012-2013 RICOH COMPANY,LTD
7  *
8  * Based on code
9  *      Copyright (C) 2011 NVIDIA Corporation
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, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25  
26 /*#define DEBUG                 1*/
27 /*#define VERBOSE_DEBUG         1*/
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
36 #include <linux/mfd/ricoh619.h>
37 #include <linux/regulator/ricoh619-regulator.h>
38 #include <linux/of.h>
39 #include <linux/regulator/of_regulator.h>
40
41 struct ricoh619_regulator {
42         int             id;
43         int             sleep_id;
44         /* Regulator register address.*/
45         u8              reg_en_reg;
46         u8              en_bit;
47         u8              reg_disc_reg;
48         u8              disc_bit;
49         u8              vout_reg;
50         u8              vout_mask;
51         u8              vout_reg_cache;
52         u8              sleep_reg;
53         u8              eco_reg;
54         u8              eco_bit;
55         u8              eco_slp_reg;
56         u8              eco_slp_bit;
57
58         /* chip constraints on regulator behavior */
59         int                     min_uV;
60         int                     max_uV;
61         int                     step_uV;
62         int                     nsteps;
63
64         /* regulator specific turn-on delay */
65         u16                     delay;
66
67         /* used by regulator core */
68         struct regulator_desc   desc;
69
70         /* Device */
71         struct device           *dev;
72 };
73
74 //static unsigned int ricoh619_suspend_status = 0;
75
76 static inline struct device *to_ricoh619_dev(struct regulator_dev *rdev)
77 {
78         return rdev_get_dev(rdev)->parent->parent;
79 }
80 /*
81 static int ricoh619_regulator_enable_time(struct regulator_dev *rdev)
82 {
83         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
84
85         return ri->delay;
86 }
87 */
88 static int ricoh619_reg_is_enabled(struct regulator_dev *rdev)
89 {
90         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
91         struct device *parent = to_ricoh619_dev(rdev);
92         uint8_t control;
93         int ret;
94
95         ret = ricoh619_read(parent, ri->reg_en_reg, &control);
96         if (ret < 0) {
97                 dev_err(&rdev->dev, "Error in reading the control register\n");
98                 return ret;
99         }
100         return (((control >> ri->en_bit) & 1) == 1);
101 }
102
103 static int ricoh619_reg_enable(struct regulator_dev *rdev)
104 {
105         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
106         struct device *parent = to_ricoh619_dev(rdev);
107         int ret;
108         ret = ricoh619_set_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
109         if (ret < 0) {
110                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
111                 return ret;
112         }
113         udelay(ri->delay);
114         return ret;
115 }
116
117 static int ricoh619_reg_disable(struct regulator_dev *rdev)
118 {
119         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
120         struct device *parent = to_ricoh619_dev(rdev);
121         int ret;
122         ret = ricoh619_clr_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
123         if (ret < 0)
124                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
125
126         return ret;
127 }
128
129 static int ricoh619_list_voltage(struct regulator_dev *rdev, unsigned index)
130 {
131         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
132
133         return ri->min_uV + (ri->step_uV * index);
134 }
135
136 static int __ricoh619_set_s_voltage(struct device *parent,
137                 struct ricoh619_regulator *ri, int min_uV, int max_uV)
138 {
139         int vsel;
140         int ret;
141
142         if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
143                 return -EDOM;
144
145         vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
146         if (vsel > ri->nsteps)
147                 return -EDOM;
148
149         ret = ricoh619_update(parent, ri->sleep_reg, vsel, ri->vout_mask);
150         if (ret < 0)
151                 dev_err(ri->dev, "Error in writing the sleep register\n");
152         return ret;
153 }
154
155 static int __ricoh619_set_voltage(struct device *parent,
156                 struct ricoh619_regulator *ri, int min_uV, int max_uV,
157                 unsigned *selector)
158 {
159         int vsel;
160         int ret;
161         uint8_t vout_val;
162
163         if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
164                 return -EDOM;
165
166         vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
167         if (vsel > ri->nsteps)
168                 return -EDOM;
169
170         if (selector)
171                 *selector = vsel;
172
173         vout_val = (ri->vout_reg_cache & ~ri->vout_mask) |
174                                 (vsel & ri->vout_mask);
175         ret = ricoh619_write(parent, ri->vout_reg, vout_val);
176         if (ret < 0)
177                 dev_err(ri->dev, "Error in writing the Voltage register\n");
178         else
179                 ri->vout_reg_cache = vout_val;
180
181         return ret;
182 }
183
184 static int ricoh619_set_voltage(struct regulator_dev *rdev,
185                 int min_uV, int max_uV, unsigned *selector)
186 {
187         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
188         struct device *parent = to_ricoh619_dev(rdev);
189
190 //      if(ricoh619_suspend_status)
191 //              return -EBUSY;
192
193         return __ricoh619_set_voltage(parent, ri, min_uV, max_uV, selector);
194 }
195
196 static int ricoh619_set_suspend_voltage(struct regulator_dev *rdev,
197                 int uV)
198 {
199         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
200         struct device *parent = to_ricoh619_dev(rdev);
201
202         return __ricoh619_set_s_voltage(parent, ri, uV, uV);
203 }
204
205 static int ricoh619_get_voltage(struct regulator_dev *rdev)
206 {
207         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
208         struct device *parent = to_ricoh619_dev(rdev);
209         uint8_t vsel;
210         int ret;
211
212         ret = ricoh619_read(parent, ri->vout_reg, &vsel);
213         return ricoh619_list_voltage(rdev,vsel);
214 }
215
216  int ricoh619_regulator_enable_eco_mode(struct regulator_dev *rdev)
217 {
218         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
219         struct device *parent = to_ricoh619_dev(rdev);
220         int ret;
221
222         ret = ricoh619_set_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
223         if (ret < 0)
224                 dev_err(&rdev->dev, "Error Enable LDO eco mode\n");
225
226         return ret;
227 }
228 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_mode);
229
230 int ricoh619_regulator_disable_eco_mode(struct regulator_dev *rdev)
231 {
232         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
233         struct device *parent = to_ricoh619_dev(rdev);
234         int ret;
235
236         ret = ricoh619_clr_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
237         if (ret < 0)
238                 dev_err(&rdev->dev, "Error Disable LDO eco mode\n");
239
240         return ret;
241 }
242 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_mode);
243
244 int ricoh619_regulator_enable_eco_slp_mode(struct regulator_dev *rdev)
245 {
246         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
247         struct device *parent = to_ricoh619_dev(rdev);
248         int ret;
249
250         ret = ricoh619_set_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
251         if (ret < 0)
252                 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
253
254         return ret;
255 }
256 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_slp_mode);
257
258 int ricoh619_regulator_disable_eco_slp_mode(struct regulator_dev *rdev)
259 {
260         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
261         struct device *parent = to_ricoh619_dev(rdev);
262         int ret;
263
264         ret = ricoh619_clr_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
265         if (ret < 0)
266                 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
267
268         return ret;
269 }
270 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_slp_mode);
271
272 static unsigned int ricoh619_dcdc_get_mode(struct regulator_dev *rdev)
273 {
274         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
275         struct device *parent = to_ricoh619_dev(rdev);
276         int ret;
277         uint8_t control;
278         u8 mask = 0x30;
279         
280         ret = ricoh619_read(parent, ri->reg_en_reg,&control);
281         if (ret < 0) {
282                 return ret;
283         }
284         control=(control & mask) >> 4;
285         switch (control) {
286         case 1:
287                 return REGULATOR_MODE_FAST;
288         case 0:
289                 return REGULATOR_MODE_NORMAL;
290         case 2:
291                 return REGULATOR_MODE_STANDBY;
292         case 3:
293                 return REGULATOR_MODE_NORMAL;
294         default:
295                 return -1;
296         }
297
298 }
299 static int ricoh619_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
300 {
301         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
302         struct device *parent = to_ricoh619_dev(rdev);
303         int ret;
304         uint8_t control;
305         
306         ret = ricoh619_read(parent, ri->reg_en_reg,&control);
307         switch(mode)
308         {
309         case REGULATOR_MODE_FAST:
310                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x10));
311         case REGULATOR_MODE_NORMAL:
312                 return ricoh619_write(parent, ri->reg_en_reg, (control & 0xcf));
313         case REGULATOR_MODE_STANDBY:
314                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x20));       
315         default:
316                 printk("error:pmu_619 only powersave pwm psm mode\n");
317                 return -EINVAL;
318         }
319         
320
321 }
322
323 static int ricoh619_dcdc_set_voltage_time_sel(struct regulator_dev *rdev,   unsigned int old_selector,
324                                      unsigned int new_selector)
325 {
326         int old_volt, new_volt;
327         
328         old_volt = ricoh619_list_voltage(rdev, old_selector);
329         if (old_volt < 0)
330                 return old_volt;
331         
332         new_volt = ricoh619_list_voltage(rdev, new_selector);
333         if (new_volt < 0)
334                 return new_volt;
335
336         return DIV_ROUND_UP(abs(old_volt - new_volt)*2, 14000);
337 }
338 static int ricoh619_dcdc_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
339 {
340         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
341         struct device *parent = to_ricoh619_dev(rdev);
342         int ret;
343         uint8_t control;
344         
345         ret = ricoh619_read(parent, ri->reg_en_reg,&control);
346         switch(mode)
347         {
348         case REGULATOR_MODE_FAST:
349                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0x3f) | 0x40));
350         case REGULATOR_MODE_NORMAL:
351                 return ricoh619_write(parent, ri->reg_en_reg, (control & 0x3f));
352         case REGULATOR_MODE_STANDBY:
353                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0x3f) | 0x80));       
354         default:
355                 printk("error:pmu_619 only powersave pwm psm mode\n");
356                 return -EINVAL;
357         }
358         
359
360 }
361 static int ricoh619_reg_suspend_enable(struct regulator_dev *rdev)
362 {
363         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
364         struct device *parent = to_ricoh619_dev(rdev);
365         int ret;
366         ret = ricoh619_set_bits(parent, (0x16 + ri->id), (0xf << 0));
367         if (ret < 0) {
368                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
369                 return ret;
370         }
371         udelay(ri->delay);
372         return ret;
373 }
374
375 static int ricoh619_reg_suspend_disable(struct regulator_dev *rdev)
376 {
377         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
378         struct device *parent = to_ricoh619_dev(rdev);
379         int ret;
380         ret = ricoh619_clr_bits(parent, (0x16 + ri->id), (0xf <<0));
381         if (ret < 0)
382                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
383
384         return ret;
385 }
386
387 static struct regulator_ops ricoh619_ops = {
388         .list_voltage                   = ricoh619_list_voltage,
389         .set_voltage                    = ricoh619_set_voltage,
390         .get_voltage                    = ricoh619_get_voltage,
391         .set_suspend_voltage = ricoh619_set_suspend_voltage,
392         .set_voltage_time_sel = ricoh619_dcdc_set_voltage_time_sel,
393         .get_mode = ricoh619_dcdc_get_mode,
394         .set_mode = ricoh619_dcdc_set_mode,
395         .enable                         = ricoh619_reg_enable,
396         .disable                                = ricoh619_reg_disable,
397         .set_suspend_mode = ricoh619_dcdc_set_suspend_mode,
398         .set_suspend_enable                             = ricoh619_reg_suspend_enable,
399         .set_suspend_disable                            = ricoh619_reg_suspend_disable,
400         .is_enabled                     = ricoh619_reg_is_enabled,
401 };
402
403 #define RICOH619_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \
404                 _vout_mask, _ds_reg, _min_uv, _max_uv, _step_uV, _nsteps,    \
405                 _ops, _delay, _eco_reg, _eco_bit, _eco_slp_reg, _eco_slp_bit)           \
406 {                                                               \
407         .reg_en_reg     = _en_reg,                              \
408         .en_bit         = _en_bit,                              \
409         .reg_disc_reg   = _disc_reg,                            \
410         .disc_bit       = _disc_bit,                            \
411         .vout_reg       = _vout_reg,                            \
412         .vout_mask      = _vout_mask,                           \
413         .sleep_reg      = _ds_reg,                              \
414         .min_uV         = _min_uv,                      \
415         .max_uV         = _max_uv ,                     \
416         .step_uV        = _step_uV,                             \
417         .nsteps         = _nsteps,                              \
418         .delay          = _delay,                               \
419         .id             = RICOH619_ID_##_id,                    \
420         .sleep_id       = RICOH619_DS_##_id,                    \
421         .eco_reg                        =  _eco_reg,                            \
422         .eco_bit                        =  _eco_bit,                            \
423         .eco_slp_reg            =  _eco_slp_reg,                                \
424         .eco_slp_bit            =  _eco_slp_bit,                                \
425         .desc = {                                               \
426                 .name = ricoh619_rails(_id),                    \
427                 .id = RICOH619_ID_##_id,                        \
428                 .n_voltages = _nsteps,                          \
429                 .ops = &_ops,                                   \
430                 .type = REGULATOR_VOLTAGE,                      \
431                 .owner = THIS_MODULE,                           \
432         },                                                      \
433 }
434
435 static struct ricoh619_regulator ricoh619_regulator_data[] = {
436         RICOH619_REG(DC1, 0x2C, 0, 0x2C, 1, 0x36, 0xFF, 0x3B,
437                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
438                         0x00, 0, 0x00, 0),
439
440         RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
441                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
442                         0x00, 0, 0x00, 0),
443
444         RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
445                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
446                         0x00, 0, 0x00, 0),
447
448         RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
449                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
450                         0x00, 0, 0x00, 0),
451
452         RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
453                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
454                         0x00, 0, 0x00, 0),
455                         
456         RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
457                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
458                         0x48, 0, 0x4A, 0),
459
460         RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
461                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
462                         0x48, 1, 0x4A, 1),
463
464         RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
465                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
466                         0x48, 2, 0x4A, 2),
467
468         RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
469                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
470                         0x48, 3, 0x4A, 3),
471
472         RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
473                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
474                         0x48, 4, 0x4A, 4),
475
476         RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
477                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
478                         0x48, 5, 0x4A, 5),
479
480         RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
481                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
482                         0x00, 0, 0x00, 0),
483
484         RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
485                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
486                         0x00, 0, 0x00, 0),
487
488         RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
489                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
490                         0x00, 0, 0x00, 0),
491
492         RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
493                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
494                         0x00, 0, 0x00, 0),
495
496         RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
497                         1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
498                         0x00, 0, 0x00, 0),
499
500         RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
501                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
502                         0x00, 0, 0x00, 0),
503 };
504
505 static inline struct ricoh619_regulator *find_regulator_info(int id)
506 {
507         struct ricoh619_regulator *ri;
508         int i;
509
510         for (i = 0; i < ARRAY_SIZE(ricoh619_regulator_data); i++) {
511                 ri = &ricoh619_regulator_data[i];
512                 if (ri->desc.id == id)
513                         return ri;
514         }
515         return NULL;
516 }
517 #if 0
518 static int ricoh619_regulator_preinit(struct device *parent,
519                 struct ricoh619_regulator *ri,
520                 struct ricoh619_regulator_platform_data *ricoh619_pdata)
521 {
522         int ret = 0;
523
524         if (!ricoh619_pdata->init_apply)
525                 return 0;
526 /*
527         if (ricoh619_pdata->init_uV >= 0) {
528                 ret = __ricoh619_set_voltage(parent, ri,
529                                 ricoh619_pdata->init_uV,
530                                 ricoh619_pdata->init_uV, 0);
531                 if (ret < 0) {
532                         dev_err(ri->dev, "Not able to initialize voltage %d "
533                                 "for rail %d err %d\n", ricoh619_pdata->init_uV,
534                                 ri->desc.id, ret);
535                         return ret;
536                 }
537         }
538 */
539         if (ricoh619_pdata->init_enable)
540                 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
541                                                         (1 << ri->en_bit));
542         else
543                 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
544                                                         (1 << ri->en_bit));
545         if (ret < 0)
546                 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
547                         (ricoh619_pdata->init_enable) ? "enable" : "disable",
548                         ri->desc.id, ret);
549
550         return ret;
551 }
552 #endif
553 static inline int ricoh619_cache_regulator_register(struct device *parent,
554         struct ricoh619_regulator *ri)
555 {
556         ri->vout_reg_cache = 0;
557         return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
558 }
559
560 #ifdef CONFIG_OF
561 static struct of_regulator_match ricoh619_regulator_matches[] = {
562         { .name = "ricoh619_dc1",},
563         { .name = "ricoh619_dc2",},
564         { .name = "ricoh619_dc3",},
565         { .name = "ricoh619_dc4",},
566         { .name = "ricoh619_dc5",},
567         { .name = "ricoh619_ldo1",},
568         { .name = "ricoh619_ldo2",},
569         { .name = "ricoh619_ldo3",},
570         { .name = "ricoh619_ldo4",},
571         { .name = "ricoh619_ldo5",},
572         { .name = "ricoh619_ldo6",},
573         { .name = "ricoh619_ldo7",},
574         { .name = "ricoh619_ldo8",},
575         { .name = "ricoh619_ldo9",},
576         { .name = "ricoh619_ldo10",},
577         { .name = "ricoh619_ldortc1",},
578         { .name = "ricoh619_ldortc2",},
579 };
580 #endif
581
582 #ifdef CONFIG_OF
583 static int ricoh619_regulator_dt_init(struct platform_device *pdev,
584                                     struct regulator_config *config,
585                                     int regidx)
586 {
587         struct device_node *nproot, *np;
588         int rcount;
589         nproot = of_node_get(pdev->dev.parent->of_node);
590         if (!nproot)
591                 return -ENODEV;
592         np = of_find_node_by_name(nproot, "regulators");
593         if (!np) {
594                 dev_err(&pdev->dev, "failed to find regulators node\n");
595                 return -ENODEV;
596         }
597
598         rcount = of_regulator_match(&pdev->dev, np,
599                                 &ricoh619_regulator_matches[regidx], 1);
600         of_node_put(np);
601         if (rcount < 0)
602                 return -ENODEV;
603         config->init_data = ricoh619_regulator_matches[regidx].init_data;
604         config->of_node = ricoh619_regulator_matches[regidx].of_node;
605
606         return 0;
607 }
608 #else
609 #define ricoh619_regulator_dt_init(x, y, z)     (-1)
610 #endif
611
612 static int ricoh619_regulator_probe(struct platform_device *pdev)
613 {
614         struct ricoh619_regulator *ri = NULL;
615         struct regulator_dev *rdev;
616         struct regulator_config config = { };
617         int err,id=0;
618         
619         rdev = devm_kzalloc(&pdev->dev, RICOH619_NUM_REGULATOR *
620                                 sizeof(*rdev), GFP_KERNEL);
621         if (!rdev) {
622                 dev_err(&pdev->dev, "Mmemory alloc failed\n");
623                 return -ENOMEM;
624         }
625
626         for (id = 0; id < RICOH619_NUM_REGULATOR; ++id) {
627
628         ri = find_regulator_info(id);
629         if (!ri) {
630                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
631                 err = -EINVAL;
632         }
633
634         ri->dev = &pdev->dev;
635         config.dev = &pdev->dev;
636         config.driver_data = ri;
637
638         config.of_node = ricoh619_regulator_matches[id].of_node;
639
640         err = ricoh619_regulator_dt_init(pdev, &config, id);
641         if (err < 0) {
642                 dev_err(&pdev->dev, "failed to regulator dt init\n");
643         }
644
645         rdev = regulator_register(&ri->desc, &config);
646         if (IS_ERR_OR_NULL(rdev)) {
647                 dev_err(&pdev->dev, "failed to register regulator %s\n",
648                                 ri->desc.name);
649                 return PTR_ERR(rdev);
650         }
651         }
652
653         platform_set_drvdata(pdev, rdev);
654         return 0;
655 }
656
657 static int ricoh619_regulator_remove(struct platform_device *pdev)
658 {
659         struct regulator_dev *rdev = platform_get_drvdata(pdev);
660
661         regulator_unregister(rdev);
662         return 0;
663 }
664
665 static struct platform_driver ricoh619_regulator_driver = {
666         .driver = {
667                 .name   = "ricoh619-regulator",
668                 .owner  = THIS_MODULE,
669         },
670         .probe          = ricoh619_regulator_probe,
671         .remove         = ricoh619_regulator_remove,
672 };
673
674 static int __init ricoh619_regulator_init(void)
675 {
676
677         return platform_driver_register(&ricoh619_regulator_driver);
678 }
679 subsys_initcall_sync(ricoh619_regulator_init);
680
681 static void __exit ricoh619_regulator_exit(void)
682 {
683         platform_driver_unregister(&ricoh619_regulator_driver);
684 }
685 module_exit(ricoh619_regulator_exit);
686
687 MODULE_DESCRIPTION("RICOH619 regulator driver");
688 MODULE_ALIAS("platform:ricoh619-regulator");
689 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
690 MODULE_LICENSE("GPL");