usb: gadget: fix: Call usb_gadget_connect() for dummy_udc
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / regulator / sprd-regulator.c
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/debugfs.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27
28 #include <mach/hardware.h>
29 #include <mach/sci.h>
30 #include <mach/sci_glb_regs.h>
31 #include <mach/adi.h>
32
33 #undef debug
34 #define debug(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
35 #define debug0(format, arg...)
36
37 #ifndef ANA_REG_OR
38 #define ANA_REG_OR(_r, _b)      sci_adi_write(_r, _b, 0)
39 #endif
40
41 #ifndef ANA_REG_BIC
42 #define ANA_REG_BIC(_r, _b)     sci_adi_write(_r, 0, _b)
43 #endif
44
45 #ifndef ANA_REG_GET
46 #define ANA_REG_GET(_r)         sci_adi_read(_r)
47 #endif
48
49 #ifndef ANA_REG_SET
50 #define ANA_REG_SET(_r, _v, _m) sci_adi_write(_r, _v, _m)
51 #endif
52
53 struct sci_regulator_regs {
54         int typ;
55         u32 pd_set, pd_set_bit;
56         u32 pd_rst, pd_rst_bit;
57         u32 slp_ctl, slp_ctl_bit;
58         u32 vol_trm, vol_trm_bits;
59         u32 vol_ctl, vol_ctl_bits;
60         u32 vol_sel_cnt, vol_sel[];
61 };
62
63 struct sci_regulator_desc {
64         struct regulator_desc desc;
65         const struct sci_regulator_regs *regs;
66 #if defined(CONFIG_DEBUG_FS)
67         struct dentry *debugfs;
68 #endif
69 };
70
71 enum {
72         VDD_TYP_LDO = 0,
73         VDD_TYP_LDO_D = 1,
74         VDD_TYP_DCDC = 2,
75 };
76
77 enum {
78         VDD_IS_ON = 0,
79         VDD_ON,
80         VDD_OFF,
81         VOL_SET,
82         VOL_GET,
83 };
84
85 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
86                      VOL_TRM, VOL_TRM_BITS, VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...)   \
87 do {                                                                                                            \
88         static const struct sci_regulator_regs REGS_##VDD = {   \
89                 .typ            = TYP,                                                                  \
90                 .pd_set = PD_SET,                                       \
91                 .pd_set_bit = SET_BIT,                                  \
92                 .pd_rst = PD_RST,                                       \
93                 .pd_rst_bit = RST_BIT,                                  \
94                 .slp_ctl = SLP_CTL,                                     \
95                 .slp_ctl_bit = SLP_CTL_BIT,                             \
96                 .vol_trm = VOL_TRM,                                 \
97                 .vol_trm_bits = VOL_TRM_BITS,                       \
98                 .vol_ctl = VOL_CTL,                                     \
99                 .vol_ctl_bits = VOL_CTL_BITS,                           \
100                 .vol_sel_cnt = VOL_SEL_CNT,                             \
101                 .vol_sel = {__VA_ARGS__},                               \
102         };                                                                                                              \
103         static struct sci_regulator_desc DESC_##VDD = {                 \
104                 .desc.name = #VDD,                                                                      \
105                 .desc.id = 0,                                                                           \
106                 .desc.ops = 0,                                                                          \
107                 .desc.type = REGULATOR_VOLTAGE,                                         \
108                 .desc.owner = THIS_MODULE,                                                      \
109                 .regs = &REGS_##VDD,                                                            \
110         };                                                                                                              \
111         sci_regulator_register(pdev, &DESC_##VDD);                              \
112 } while (0)
113
114 int reguator_is_trimming(struct regulator_dev *rdev);
115 int ldo_trimming_callback(void *data);
116 int __def_callback(void *data)
117 {
118         return WARN_ON(1);
119 }
120
121 int ldo_trimming_callback(void *)
122     __attribute__ ((weak, alias("__def_callback")));
123
124 /* standard ldo ops*/
125 int sci_ldo_op(const struct sci_regulator_regs *regs, int op)
126 {
127         int ret = 0;
128
129         debug0("regu %p op(%d), set %08x[%d], rst %08x[%d]\n", regs, op,
130                regs->pd_set, __ffs(regs->pd_set_bit), regs->pd_rst,
131                __ffs(regs->pd_rst_bit));
132
133         if (!regs->pd_rst || !regs->pd_set)
134                 return -EACCES;
135
136         switch (op) {
137         case VDD_ON:
138                 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
139                 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
140                 break;
141         case VDD_OFF:
142                 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
143                 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
144                 break;
145         case VDD_IS_ON:
146                 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
147                 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
148                         return -EINVAL;
149                 break;
150         default:
151                 break;
152         }
153         return ret;
154 }
155
156 static int ldo_turn_on(struct regulator_dev *rdev)
157 {
158         struct sci_regulator_desc *desc =
159             (struct sci_regulator_desc *)rdev->desc;
160         const struct sci_regulator_regs *regs = desc->regs;
161
162         int ret = sci_ldo_op(regs, VDD_ON);
163
164         debug0("regu %p (%s), turn on\n", regs, desc->desc.name);
165         /* notify ldo trimming when first turn on */
166         if (0 == ret && regs->vol_trm && !reguator_is_trimming(rdev)) {
167                 ldo_trimming_callback((void *)desc->desc.name);
168         }
169         return ret;
170 }
171
172 static int ldo_turn_off(struct regulator_dev *rdev)
173 {
174         return sci_ldo_op(((struct sci_regulator_desc *)(rdev->desc))->regs,
175                           VDD_OFF);
176 }
177
178 static int ldo_is_on(struct regulator_dev *rdev)
179 {
180         return sci_ldo_op(((struct sci_regulator_desc *)(rdev->desc))->regs,
181                           VDD_IS_ON);
182 }
183
184 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
185 {
186         struct sci_regulator_desc *desc =
187             (struct sci_regulator_desc *)rdev->desc;
188         const struct sci_regulator_regs *regs = desc->regs;
189         debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
190               regs->slp_ctl, regs->slp_ctl_bit, mode);
191         if (!regs->slp_ctl)
192                 return -EINVAL;
193
194         if (mode == REGULATOR_MODE_STANDBY) {   /* disable auto slp */
195                 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
196         } else {
197                 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
198         }
199         return 0;
200 }
201
202 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
203                            int max_uV, unsigned *selector)
204 {
205         static const int vol_bits[4] = { 0xa, 0x9, 0x6, 0x5 };
206         struct sci_regulator_desc *desc =
207             (struct sci_regulator_desc *)rdev->desc;
208         const struct sci_regulator_regs *regs = desc->regs;
209         int mv = min_uV / 1000;
210         int ret = -EINVAL;
211         int i, shft = __ffs(regs->vol_ctl_bits);
212         BUG_ON(regs->vol_sel_cnt > 4);
213         debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
214
215         if (!regs->vol_ctl)
216                 return -EACCES;
217         for (i = 0; i < regs->vol_sel_cnt; i++) {
218                 if (regs->vol_sel[i] == mv) {
219                         ANA_REG_SET(regs->vol_ctl, vol_bits[i] << shft,
220                                     regs->vol_ctl_bits);
221                         //clear_bit(desc->desc.id, trimming_state);
222                         ret = 0;
223                         break;
224                 }
225         }
226
227         WARN(0 != ret,
228              "warning: regulator (%s) not support %dmV\n", desc->desc.name, mv);
229         return ret;
230 }
231
232 static int ldo_get_voltage(struct regulator_dev *rdev)
233 {
234         struct sci_regulator_desc *desc =
235             (struct sci_regulator_desc *)rdev->desc;
236         const struct sci_regulator_regs *regs = desc->regs;
237         u32 vol, vol_bits;
238         int i, shft = __ffs(regs->vol_ctl_bits);
239
240         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
241                regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
242
243         if (!regs->vol_ctl)
244                 return -EACCES;
245
246         BUG_ON(regs->vol_sel_cnt != 4);
247         vol_bits = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
248
249         if ((vol_bits & BIT(0)) ^ (vol_bits & BIT(1))
250             && (vol_bits & BIT(2)) ^ (vol_bits & BIT(3))) {
251                 i = (vol_bits & BIT(0)) | ((vol_bits >> 1) & BIT(1));
252                 vol = regs->vol_sel[i];
253                 debug("regu %p (%s), voltage %d\n", regs, desc->desc.name, vol);
254                 return vol * 1000;
255         }
256         return -EFAULT;
257 }
258
259 static unsigned long trimming_state[2] = { 0, 0 };      /* 64 bits */
260
261 int reguator_is_trimming(struct regulator_dev *rdev)
262 {
263         int id;
264         BUG_ON(!rdev);
265         id = rdev->desc->id;
266         BUG_ON(!(id > 0 && id < sizeof(trimming_state) * 8));
267
268         return test_bit(id, trimming_state);
269 }
270
271 static int ldo_init_trimming(struct regulator_dev *rdev)
272 {
273         struct sci_regulator_desc *desc =
274             (struct sci_regulator_desc *)rdev->desc;
275         const struct sci_regulator_regs *regs = desc->regs;
276         int ret = -EINVAL;
277         int shft = __ffs(regs->vol_trm_bits);
278         u32 trim;
279
280         if (!regs->vol_trm)
281                 goto exit;
282
283         trim = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft;
284         if (trim != 0x10 /* 100 % */ ) {
285                 debug("regu %p (%s) trimming ok\n", regs, desc->desc.name);
286                 set_bit(desc->desc.id, trimming_state);
287                 ret = trim;
288         } else if (1 == ldo_is_on(rdev)) {      /* some LDOs had been turned in uboot-spl */
289                 ret = ldo_turn_on(rdev);
290         }
291
292 exit:
293         return ret;
294 }
295
296 /**
297  * ldo trimming step about 0.7%, range 90% ~ 110%. that all maps as follow.
298         0x00 : 90.000
299         0x01 : 90.625
300         0x02 : 91.250
301         0x03 : 91.875
302         0x04 : 92.500
303         0x05 : 93.125
304         0x06 : 93.750
305         0x07 : 94.375
306         0x08 : 95.000
307         0x09 : 95.625
308         0x0A : 96.250
309         0x0B : 96.875
310         0x0C : 97.500
311         0x0D : 98.125
312         0x0E : 98.750
313         0x0F : 99.375
314         0x10 : 100.000
315         0x11 : 100.625
316         0x12 : 101.250
317         0x13 : 101.875
318         0x14 : 102.500
319         0x15 : 103.125
320         0x16 : 103.750
321         0x17 : 104.375
322         0x18 : 105.000
323         0x19 : 105.625
324         0x1A : 106.250
325         0x1B : 106.875
326         0x1C : 107.500
327         0x1D : 108.125
328         0x1E : 108.750
329         0x1F : 109.375
330
331         0x20 : 110.000
332  */
333 static int ldo_set_trimming(struct regulator_dev *rdev, int ctl_vol, int to_vol)
334 {
335         struct sci_regulator_desc *desc =
336             (struct sci_regulator_desc *)rdev->desc;
337         const struct sci_regulator_regs *regs = desc->regs;
338         int ret = -EINVAL, cal_vol;
339
340         ctl_vol /= 1000;
341         to_vol /= 1000;
342
343         cal_vol = ctl_vol - to_vol * 90 / 100;  /* cal range 90% ~ 110% */
344         if (!regs->vol_trm || cal_vol < 0 || cal_vol >= to_vol * 20 / 100)
345                 goto exit;
346
347         /* always update voltage ctrl bits */
348         ret = ldo_set_voltage(rdev, to_vol * 1000, to_vol * 1000, 0);
349         if (IS_ERR_VALUE(ret) && regs->vol_ctl)
350                 goto exit;
351
352         else {
353                 u32 trim =      /* assert 5 valid trim bits */
354                     (cal_vol * 100 * 32) / (to_vol * 20) & 0x1f;
355                 debug
356                     ("regu %p (%s) trimming %u = %u %+dmv, got [%02X] %u.%03u%%\n",
357                      regs, desc->desc.name, ctl_vol, to_vol,
358                      (cal_vol - to_vol / 10), trim, ctl_vol * 100 / to_vol,
359                      (ctl_vol * 100 * 1000 / to_vol) % 1000);
360
361                 ANA_REG_SET(regs->vol_trm, trim << __ffs(regs->vol_trm_bits),
362                             regs->vol_trm_bits);
363                 set_bit(desc->desc.id, trimming_state);
364                 ret = 0;
365         }
366
367 exit:
368         return ret;
369 }
370
371 int regulator_set_trimming(struct regulator *regulator, int ctl_vol, int to_vol)
372 {
373         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
374         struct sci_regulator_desc *desc =
375             (struct sci_regulator_desc *)rdev->desc;
376         const struct sci_regulator_regs *regs = desc->regs;
377
378         return (2 /*DCDC*/ == regs->typ)
379             ? regulator_set_voltage(regulator, ctl_vol, ctl_vol)
380             : ldo_set_trimming(rdev, ctl_vol, to_vol);
381 }
382
383 int regulator_get_trimming_step(struct regulator *regulator, int def_vol)
384 {
385         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
386         struct sci_regulator_desc *desc =
387             (struct sci_regulator_desc *)rdev->desc;
388         const struct sci_regulator_regs *regs = desc->regs;
389
390         return (2 /*DCDC*/ == regs->typ)
391             ? 100 / 32
392             : def_vol * 7 / 1000;
393 }
394
395 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
396 {
397         int i, j = -1;
398         int ds, min_ds = 100;   /* mV, the max range of small voltage */
399         for (i = 0; i < regs->vol_sel_cnt; i++) {
400                 ds = vol - regs->vol_sel[i];
401                 if (ds >= 0 && ds < min_ds) {
402                         min_ds = ds;
403                         j = i;
404                 }
405         }
406         return j;
407 }
408
409 /* standard dcdc ops*/
410 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
411                             int max_uV, unsigned *selector)
412 {
413         struct sci_regulator_desc *desc =
414             (struct sci_regulator_desc *)rdev->desc;
415         const struct sci_regulator_regs *regs = desc->regs;
416         int mv = min_uV / 1000;
417         int i, shft = __ffs(regs->vol_ctl_bits);
418         int max = regs->vol_ctl_bits >> shft;
419
420         debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
421
422         BUG_ON(shft != 0);
423         BUG_ON(regs->vol_sel_cnt > 8);
424
425         if (!regs->vol_ctl)
426                 return -EACCES;
427
428         /* found the closely vol ctrl bits */
429         i = __match_dcdc_vol(regs, mv);
430         if (i < 0)
431                 return -EINVAL;
432
433         debug("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
434               mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
435
436         if (regs->vol_trm) {    /* small adjust first */
437                 /* dcdc calibration control bits (default 00000),
438                  * small adjust voltage: 100/32mv ~= 3.125mv
439                  */
440                 int j = ((mv - regs->vol_sel[i]) * 32) / (100) % 32;
441                 ANA_REG_SET(regs->vol_trm,
442                             BITS_DCDC_CAL(j) |
443                             BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1) - j), -1);
444         }
445
446         ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
447         return 0;
448 }
449
450 static int dcdc_get_voltage(struct regulator_dev *rdev)
451 {
452         struct sci_regulator_desc *desc =
453             (struct sci_regulator_desc *)rdev->desc;
454         const struct sci_regulator_regs *regs = desc->regs;
455         u32 mv, vol_bits;
456         int cal = 0;            /* mV */
457         int i, shft = __ffs(regs->vol_ctl_bits);
458
459         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
460                regs, desc->desc.name, regs->vol_ctl,
461                shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
462
463         BUG_ON(shft != 0);
464         BUG_ON(regs->vol_sel_cnt > 8);
465
466         if (!regs->vol_ctl)
467                 return -EINVAL;
468
469         i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits);
470
471         /*check the reset relative bit of vol ctl */
472         vol_bits =
473             (~ANA_REG_GET(regs->vol_ctl) & (regs->vol_ctl_bits << 4)) >> 4;
474
475         if (i != vol_bits)
476                 return -EFAULT;
477
478         mv = regs->vol_sel[i];
479
480         if (regs->vol_trm) {
481                 int j = ANA_REG_GET(regs->vol_trm) & BITS_DCDC_CAL(-1);
482                 cal = DIV_ROUND_CLOSEST(j * 100, 32);
483         }
484
485         debug("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv, cal);
486         return (mv + cal) * 1000;
487 }
488
489 /* standard ldo-D-Die ops*/
490 static int usbd_turn_on(struct regulator_dev *rdev)
491 {
492         const struct sci_regulator_regs *regs =
493             ((struct sci_regulator_desc *)(rdev->desc))->regs;
494         sci_glb_clr(regs->pd_set, regs->pd_set_bit);
495         return 0;
496 }
497
498 static int usbd_turn_off(struct regulator_dev *rdev)
499 {
500         const struct sci_regulator_regs *regs =
501             ((struct sci_regulator_desc *)(rdev->desc))->regs;
502         sci_glb_set(regs->pd_set, regs->pd_set_bit);
503         return 0;
504 }
505
506 static int usbd_is_on(struct regulator_dev *rdev)
507 {
508         const struct sci_regulator_regs *regs =
509             ((struct sci_regulator_desc *)(rdev->desc))->regs;
510         return !sci_glb_read(regs->pd_set, regs->pd_set_bit);
511 }
512
513 static struct regulator_ops ldo_ops = {
514         .enable = ldo_turn_on,
515         .disable = ldo_turn_off,
516         .is_enabled = ldo_is_on,
517         .set_voltage = ldo_set_voltage,
518         .get_voltage = ldo_get_voltage,
519         .set_mode = ldo_set_mode,
520 };
521
522 static struct regulator_ops usbd_ops = {
523         .enable = usbd_turn_on,
524         .disable = usbd_turn_off,
525         .is_enabled = usbd_is_on,
526 };
527
528 static struct regulator_ops dcdc_ops = {
529         .enable = ldo_turn_on,
530         .disable = ldo_turn_off,
531         .is_enabled = ldo_is_on,
532         .set_voltage = dcdc_set_voltage,
533         .get_voltage = dcdc_get_voltage,
534 };
535
536 /*
537  * Consider the following machine :-
538  *
539  *   Regulator-1 -+-> [Consumer A @ 1.8V]
540  *                |
541  *                +-> [Consumer B @ 1.8V]
542  *
543  *   Regulator-2 ---> [Consumer C @ 3.3V]
544  *
545  * The drivers for consumers A & B must be mapped to the correct regulator in
546  * order to control their power supply. This mapping can be achieved in board/machine
547  * initialisation code by creating a struct regulator_consumer_supply for each regulator.
548  * Alternatively, we built a regulator supply-consumers map, the format is as follow:
549  *
550  *      supply source-1, consumer A, consumer B, ..., NULL
551  *      supply source-2, consumer C, ..., NULL
552  *      ...
553  *      NULL
554  *
555  */
556 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
557                                                         const char *supply_name,
558                                                         int *num)
559 {
560         char **map = (char **)dev_get_platdata(dev);
561         int i, n;
562         struct regulator_consumer_supply *consumer_supplies = NULL;
563
564         if (!supply_name || !(map && map[0]))
565                 return NULL;
566
567         for (i = 0; map[i] || map[i + 1]; i++) {
568                 if (map[i] && 0 == strcmp(map[i], supply_name))
569                         break;
570         }
571
572         /* i++; *//* Do not skip supply name */
573
574         for (n = 0; map[i + n]; n++) ;
575
576         if (n) {
577                 pr_info("supply %s consumers %d - %d\n", supply_name, i, n);
578                 consumer_supplies =
579                     kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
580                 BUG_ON(!consumer_supplies);
581                 for (n = 0; map[i]; i++, n++) {
582                         consumer_supplies[n].supply = map[i];
583                 }
584                 if (num)
585                         *num = n;
586         }
587         return consumer_supplies;
588 }
589
590 #if defined(CONFIG_DEBUG_FS)
591 static struct dentry *debugfs_root = NULL;
592
593 static int debugfs_enable_get(void *data, u64 * val)
594 {
595         struct regulator_dev *rdev = data;
596         if (rdev && rdev->desc->ops->is_enabled)
597                 *val = rdev->desc->ops->is_enabled(rdev);
598         else
599                 *val = -1;
600         return 0;
601 }
602
603 static int debugfs_enable_set(void *data, u64 val)
604 {
605         struct regulator_dev *rdev = data;
606         if (rdev && rdev->desc->ops->enable)
607                 (val) ? rdev->desc->ops->enable(rdev)
608                     : rdev->desc->ops->disable(rdev);
609         return 0;
610 }
611
612 static int debugfs_voltage_get(void *data, u64 * val)
613 {
614         struct regulator_dev *rdev = data;
615         if (rdev && rdev->desc->ops->get_voltage)
616                 *val = rdev->desc->ops->get_voltage(rdev) / 1000;
617         else
618                 *val = -1;
619         return 0;
620 }
621
622 static int debugfs_voltage_set(void *data, u64 val)
623 {
624         struct regulator_dev *rdev = data;
625         if (rdev && rdev->desc->ops->set_voltage)
626                 rdev->desc->ops->set_voltage(rdev, val * 1000, val * 1000, 0);
627         return 0;
628 }
629
630 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
631                         debugfs_enable_get, debugfs_enable_set, "%llu\n");
632 DEFINE_SIMPLE_ATTRIBUTE(fops_voltage,
633                         debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
634
635 static void rdev_init_debugfs(struct regulator_dev *rdev)
636 {
637         struct sci_regulator_desc *desc =
638             (struct sci_regulator_desc *)rdev->desc;
639
640         desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
641         if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
642                 pr_warn("Failed to create debugfs directory\n");
643                 rdev->debugfs = NULL;
644                 return;
645         }
646
647         debugfs_create_file("enable", S_IRUGO | S_IWUSR,
648                             desc->debugfs, rdev, &fops_enable);
649
650         debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
651                             desc->debugfs, rdev, &fops_voltage);
652 }
653 #else
654 static void rdev_init_debugfs(struct regulator_dev *rdev)
655 {
656 }
657 #endif
658
659 void *__devinit sci_regulator_register(struct platform_device *pdev,
660                                        struct sci_regulator_desc *desc)
661 {
662         static atomic_t __devinitdata idx = ATOMIC_INIT(1);     /* 0: dummy */
663         struct regulator_dev *rdev;
664         struct regulator_ops *__regs_ops[] = {
665                 &ldo_ops, &usbd_ops, &dcdc_ops, 0,
666         };
667         struct regulator_consumer_supply consumer_supplies_default[] = {
668                 [0] = {
669 //                     .dev = 0,
670                        .dev_name = 0,
671                        .supply = desc->desc.name,
672                        }
673         };
674         struct regulator_init_data init_data = {
675                 .supply_regulator = 0,
676                 .constraints = {
677                                 .min_uV = 0,
678                                 .max_uV = 4200 * 1000,
679                                 .valid_modes_mask =
680                                 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
681                                 .valid_ops_mask =
682                                 REGULATOR_CHANGE_STATUS |
683                                 REGULATOR_CHANGE_VOLTAGE |
684                                 REGULATOR_CHANGE_MODE,
685                                 },
686                 .num_consumer_supplies = 1,
687                 .consumer_supplies = consumer_supplies_default,
688                 .regulator_init = 0,
689                 .driver_data = 0,
690         };
691
692         BUG_ON(desc->regs->typ > 3);
693         if (!desc->desc.ops)
694                 desc->desc.ops = __regs_ops[desc->regs->typ];
695
696         desc->desc.id = atomic_inc_return(&idx) - 1;
697
698         init_data.consumer_supplies =
699             set_supply_map(&pdev->dev, desc->desc.name,
700                            &init_data.num_consumer_supplies);
701
702         if (!init_data.consumer_supplies)
703                 init_data.consumer_supplies = consumer_supplies_default;
704
705         debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
706         rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0, 0);
707         if (init_data.consumer_supplies != consumer_supplies_default)
708                 kfree(init_data.consumer_supplies);
709
710         if (!IS_ERR(rdev)) {
711                 rdev->reg_data = rdev;
712                 if (desc->desc.ops == &ldo_ops)
713                         ldo_init_trimming(rdev);
714                 rdev_init_debugfs(rdev);
715         }
716         return rdev;
717 }
718
719 /**
720  * IMPORTANT!!!
721  * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
722  * so i autogen all regulators non-variable description in plat or mach directory,
723  * which named __regulator_map.h, BUT register all in regulator driver probe func,
724  * just like other regulator vendor drivers.
725  */
726 static int __devinit sci_regulator_probe(struct platform_device *pdev)
727 {
728         debug0("platform device %p\n", pdev);
729 #include CONFIG_REGULATOR_SPRD_MAP
730         return 0;
731 }
732
733 static struct platform_driver sci_regulator_driver = {
734         .driver = {
735                    .name = "sprd-regulator",
736                    .owner = THIS_MODULE,
737                    },
738         .probe = sci_regulator_probe,
739 };
740
741 static int __init sci_regulator_init(void)
742 {
743 #ifdef CONFIG_DEBUG_FS
744         debugfs_root =
745             debugfs_create_dir(sci_regulator_driver.driver.name, NULL);
746         if (IS_ERR(debugfs_root) || !debugfs_root) {
747                 pr_warn("%s: Failed to create debugfs directory\n",
748                         sci_regulator_driver.driver.name);
749                 debugfs_root = NULL;
750         }
751 #endif
752         return platform_driver_register(&sci_regulator_driver);
753 }
754
755 subsys_initcall(sci_regulator_init);
756
757 MODULE_LICENSE("GPL v2");
758 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
759 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");