Merge tag 'lsk-android-14.03' into develop-3.10
[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         u8 vout_val;
367         ret = ricoh619_set_bits(parent, (0x16 + ri->id), (0xf << 0));
368         if (ret < 0) {
369                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
370                 return ret;
371         }
372         udelay(ri->delay);
373         return ret;
374 }
375
376 static int ricoh619_reg_suspend_disable(struct regulator_dev *rdev)
377 {
378         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
379         struct device *parent = to_ricoh619_dev(rdev);
380         int ret;
381         ret = ricoh619_clr_bits(parent, (0x16 + ri->id), (0xf <<0));
382         if (ret < 0)
383                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
384
385         return ret;
386 }
387
388 static struct regulator_ops ricoh619_ops = {
389         .list_voltage                   = ricoh619_list_voltage,
390         .set_voltage                    = ricoh619_set_voltage,
391         .get_voltage                    = ricoh619_get_voltage,
392         .set_suspend_voltage = ricoh619_set_suspend_voltage,
393         .set_voltage_time_sel = ricoh619_dcdc_set_voltage_time_sel,
394         .get_mode = ricoh619_dcdc_get_mode,
395         .set_mode = ricoh619_dcdc_set_mode,
396         .enable                         = ricoh619_reg_enable,
397         .disable                                = ricoh619_reg_disable,
398         .set_suspend_mode = ricoh619_dcdc_set_suspend_mode,
399         .set_suspend_enable                             = ricoh619_reg_suspend_enable,
400         .set_suspend_disable                            = ricoh619_reg_suspend_disable,
401         .is_enabled                     = ricoh619_reg_is_enabled,
402 };
403
404 #define RICOH619_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \
405                 _vout_mask, _ds_reg, _min_uv, _max_uv, _step_uV, _nsteps,    \
406                 _ops, _delay, _eco_reg, _eco_bit, _eco_slp_reg, _eco_slp_bit)           \
407 {                                                               \
408         .reg_en_reg     = _en_reg,                              \
409         .en_bit         = _en_bit,                              \
410         .reg_disc_reg   = _disc_reg,                            \
411         .disc_bit       = _disc_bit,                            \
412         .vout_reg       = _vout_reg,                            \
413         .vout_mask      = _vout_mask,                           \
414         .sleep_reg      = _ds_reg,                              \
415         .min_uV         = _min_uv,                      \
416         .max_uV         = _max_uv ,                     \
417         .step_uV        = _step_uV,                             \
418         .nsteps         = _nsteps,                              \
419         .delay          = _delay,                               \
420         .id             = RICOH619_ID_##_id,                    \
421         .sleep_id       = RICOH619_DS_##_id,                    \
422         .eco_reg                        =  _eco_reg,                            \
423         .eco_bit                        =  _eco_bit,                            \
424         .eco_slp_reg            =  _eco_slp_reg,                                \
425         .eco_slp_bit            =  _eco_slp_bit,                                \
426         .desc = {                                               \
427                 .name = ricoh619_rails(_id),                    \
428                 .id = RICOH619_ID_##_id,                        \
429                 .n_voltages = _nsteps,                          \
430                 .ops = &_ops,                                   \
431                 .type = REGULATOR_VOLTAGE,                      \
432                 .owner = THIS_MODULE,                           \
433         },                                                      \
434 }
435
436 static struct ricoh619_regulator ricoh619_regulator_data[] = {
437         RICOH619_REG(DC1, 0x2C, 0, 0x2C, 1, 0x36, 0xFF, 0x3B,
438                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
439                         0x00, 0, 0x00, 0),
440
441         RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
442                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
443                         0x00, 0, 0x00, 0),
444
445         RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
446                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
447                         0x00, 0, 0x00, 0),
448
449         RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
450                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
451                         0x00, 0, 0x00, 0),
452
453         RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
454                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
455                         0x00, 0, 0x00, 0),
456                         
457         RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
458                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
459                         0x48, 0, 0x4A, 0),
460
461         RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
462                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
463                         0x48, 1, 0x4A, 1),
464
465         RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
466                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
467                         0x48, 2, 0x4A, 2),
468
469         RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
470                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
471                         0x48, 3, 0x4A, 3),
472
473         RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
474                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
475                         0x48, 4, 0x4A, 4),
476
477         RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
478                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
479                         0x48, 5, 0x4A, 5),
480
481         RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
482                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
483                         0x00, 0, 0x00, 0),
484
485         RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
486                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
487                         0x00, 0, 0x00, 0),
488
489         RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
490                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
491                         0x00, 0, 0x00, 0),
492
493         RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
494                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
495                         0x00, 0, 0x00, 0),
496
497         RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
498                         1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
499                         0x00, 0, 0x00, 0),
500
501         RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
502                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
503                         0x00, 0, 0x00, 0),
504 };
505
506 static inline struct ricoh619_regulator *find_regulator_info(int id)
507 {
508         struct ricoh619_regulator *ri;
509         int i;
510
511         for (i = 0; i < ARRAY_SIZE(ricoh619_regulator_data); i++) {
512                 ri = &ricoh619_regulator_data[i];
513                 if (ri->desc.id == id)
514                         return ri;
515         }
516         return NULL;
517 }
518
519 static int ricoh619_regulator_preinit(struct device *parent,
520                 struct ricoh619_regulator *ri,
521                 struct ricoh619_regulator_platform_data *ricoh619_pdata)
522 {
523         int ret = 0;
524
525         if (!ricoh619_pdata->init_apply)
526                 return 0;
527 /*
528         if (ricoh619_pdata->init_uV >= 0) {
529                 ret = __ricoh619_set_voltage(parent, ri,
530                                 ricoh619_pdata->init_uV,
531                                 ricoh619_pdata->init_uV, 0);
532                 if (ret < 0) {
533                         dev_err(ri->dev, "Not able to initialize voltage %d "
534                                 "for rail %d err %d\n", ricoh619_pdata->init_uV,
535                                 ri->desc.id, ret);
536                         return ret;
537                 }
538         }
539 */
540         if (ricoh619_pdata->init_enable)
541                 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
542                                                         (1 << ri->en_bit));
543         else
544                 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
545                                                         (1 << ri->en_bit));
546         if (ret < 0)
547                 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
548                         (ricoh619_pdata->init_enable) ? "enable" : "disable",
549                         ri->desc.id, ret);
550
551         return ret;
552 }
553
554 static inline int ricoh619_cache_regulator_register(struct device *parent,
555         struct ricoh619_regulator *ri)
556 {
557         ri->vout_reg_cache = 0;
558         return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
559 }
560
561 #ifdef CONFIG_OF
562 static struct of_regulator_match ricoh619_regulator_matches[] = {
563         { .name = "ricoh619_dc1",},
564         { .name = "ricoh619_dc2",},
565         { .name = "ricoh619_dc3",},
566         { .name = "ricoh619_dc4",},
567         { .name = "ricoh619_dc5",},
568         { .name = "ricoh619_ldo1",},
569         { .name = "ricoh619_ldo2",},
570         { .name = "ricoh619_ldo3",},
571         { .name = "ricoh619_ldo4",},
572         { .name = "ricoh619_ldo5",},
573         { .name = "ricoh619_ldo6",},
574         { .name = "ricoh619_ldo7",},
575         { .name = "ricoh619_ldo8",},
576         { .name = "ricoh619_ldo9",},
577         { .name = "ricoh619_ldo10",},
578         { .name = "ricoh619_ldortc1",},
579         { .name = "ricoh619_ldortc2",},
580 };
581 #endif
582
583 #ifdef CONFIG_OF
584 static int ricoh619_regulator_dt_init(struct platform_device *pdev,
585                                     struct regulator_config *config,
586                                     int regidx)
587 {
588         struct device_node *nproot, *np;
589         int rcount;
590         nproot = of_node_get(pdev->dev.parent->of_node);
591         if (!nproot)
592                 return -ENODEV;
593         np = of_find_node_by_name(nproot, "regulators");
594         if (!np) {
595                 dev_err(&pdev->dev, "failed to find regulators node\n");
596                 return -ENODEV;
597         }
598
599         rcount = of_regulator_match(&pdev->dev, np,
600                                 &ricoh619_regulator_matches[regidx], 1);
601         of_node_put(np);
602         if (rcount < 0)
603                 return -ENODEV;
604         config->init_data = ricoh619_regulator_matches[regidx].init_data;
605         config->of_node = ricoh619_regulator_matches[regidx].of_node;
606
607         return 0;
608 }
609 #else
610 #define ricoh619_regulator_dt_init(x, y, z)     (-1)
611 #endif
612
613 static int ricoh619_regulator_probe(struct platform_device *pdev)
614 {
615         struct ricoh619_regulator *ri = NULL;
616         struct regulator_dev *rdev;
617         struct regulator_config config = { };
618         struct regulator_init_data *pdata_regulator = dev_get_platdata(&pdev->dev);
619         int err,id=0;
620         
621         rdev = devm_kzalloc(&pdev->dev, RICOH619_NUM_REGULATOR *
622                                 sizeof(*rdev), GFP_KERNEL);
623         if (!rdev) {
624                 dev_err(&pdev->dev, "Mmemory alloc failed\n");
625                 return -ENOMEM;
626         }
627
628         for (id = 0; id < RICOH619_NUM_REGULATOR; ++id) {
629
630         ri = find_regulator_info(id);
631         if (!ri) {
632                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
633                 err = -EINVAL;
634         }
635
636         ri->dev = &pdev->dev;
637         config.dev = &pdev->dev;
638         config.driver_data = ri;
639
640         if (ricoh619_regulator_matches)
641                 config.of_node = ricoh619_regulator_matches[id].of_node;
642
643         if (ricoh619_regulator_dt_init(pdev, &config, id))
644                 if (pdata_regulator)
645                         config.init_data = &pdata_regulator;
646
647         rdev = regulator_register(&ri->desc, &config);
648         if (IS_ERR_OR_NULL(rdev)) {
649                 dev_err(&pdev->dev, "failed to register regulator %s\n",
650                                 ri->desc.name);
651                 return PTR_ERR(rdev);
652         }
653         }
654
655         platform_set_drvdata(pdev, rdev);
656         return 0;
657 }
658
659 static int ricoh619_regulator_remove(struct platform_device *pdev)
660 {
661         struct regulator_dev *rdev = platform_get_drvdata(pdev);
662
663         regulator_unregister(rdev);
664         return 0;
665 }
666
667 static struct platform_driver ricoh619_regulator_driver = {
668         .driver = {
669                 .name   = "ricoh619-regulator",
670                 .owner  = THIS_MODULE,
671         },
672         .probe          = ricoh619_regulator_probe,
673         .remove         = ricoh619_regulator_remove,
674 };
675
676 static int __init ricoh619_regulator_init(void)
677 {
678
679         return platform_driver_register(&ricoh619_regulator_driver);
680 }
681 subsys_initcall_sync(ricoh619_regulator_init);
682
683 static void __exit ricoh619_regulator_exit(void)
684 {
685         platform_driver_unregister(&ricoh619_regulator_driver);
686 }
687 module_exit(ricoh619_regulator_exit);
688
689 MODULE_DESCRIPTION("RICOH619 regulator driver");
690 MODULE_ALIAS("platform:ricoh619-regulator");
691 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
692 MODULE_LICENSE("GPL");