drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / regulator / sc2713-regulator.c
1 /*
2  * Copyright (C) 2013 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  * Fixes:
15  *              0.4
16  *              Bug#183980 add dcdc and pll enable time
17  *              Change-Id: I6e6e06ee0beb306cd846964d0ba24aef449e5beb
18  *              0.3
19  *              Bug#164001 add dcdc mem/gen/wpa/wrf map
20  *              Change-Id: I07dac5700c0907aca99f6112bd4b5799358a9a88
21  *              0.2
22  *              Bug#164001 shark dcam: add camera ldo calibration
23  *              Change-Id: Icaee2706b8b0985ae6f3122b236d8e278dcc0db2
24  *              0.1
25  *              sc8830: fix adc cal data from cmdline fail
26  *              Change-Id: Id85d58178aca40fdf13b996853711e92e1171801
27  *
28  * To Fix:
29  *
30  *
31  */
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/spinlock.h>
37 #include <linux/debugfs.h>
38 #include <linux/slab.h>
39 #include <linux/sort.h>
40 #include <linux/delay.h>
41 #include <linux/err.h>
42 #include <linux/io.h>
43 #include <linux/platform_device.h>
44 #include <linux/of.h>
45 #include <linux/of_device.h>
46 #include <linux/regulator/of_regulator.h>
47
48 #include <linux/regulator/consumer.h>
49 #include <linux/regulator/driver.h>
50 #include <linux/regulator/machine.h>
51
52 #include <mach/hardware.h>
53 #include <mach/sci.h>
54 #include <mach/sci_glb_regs.h>
55 #include <mach/adi.h>
56 #include <mach/adc.h>
57 #include <mach/arch_misc.h>
58
59 #define REGULATOR_ROOT_DIR      "sprd-regulator"
60
61 #undef debug
62 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
63 #define debug0(format, arg...)  //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
64 #define debug2(format, arg...)  pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
65
66 #ifndef ANA_REG_OR
67 #define ANA_REG_OR(_r, _b)      sci_adi_write(_r, _b, 0)
68 #endif
69
70 #ifndef ANA_REG_BIC
71 #define ANA_REG_BIC(_r, _b)     sci_adi_write(_r, 0, _b)
72 #endif
73
74 #ifndef ANA_REG_GET
75 #define ANA_REG_GET(_r)         sci_adi_read(_r)
76 #endif
77
78 #ifndef ANA_REG_SET
79 #define ANA_REG_SET(_r, _v, _m) sci_adi_write((_r), ((_v) & (_m)), (_m))
80 #endif
81
82 struct sci_regulator_regs {
83         int typ;
84         u32 pd_set, pd_set_bit;
85         /**
86          * at new feature, some LDOs had only set, no rst bits.
87          * and DCDCs voltage and trimming controller is the same register
88          */
89         u32 pd_rst, pd_rst_bit;
90         u32 slp_ctl, slp_ctl_bit;
91         u32 vol_trm, vol_trm_bits;
92         u32 cal_ctl, cal_ctl_bits;
93         u32 vol_def;
94         u32 vol_ctl, vol_ctl_bits;
95         u32 vol_sel_cnt, vol_sel[];
96 };
97
98 /**
99  * struct sci_regulator_ops - sci regulator operations.
100  *
101  * @trimming:
102  *
103  * This struct describes regulator operations which can be implemented by
104  * regulator chip drivers.
105  */
106 struct sci_regulator_ops {
107         int trimming_def_val;   /* trimming controller default value in A-Die */
108         int (*get_trimming_step) (struct regulator_dev * rdev, int);
109         int (*set_trimming) (struct regulator_dev * rdev, int, int, int);
110         int (*calibrate) (struct regulator_dev * rdev, int, int);
111 };
112
113 struct sci_regulator_data {
114         struct delayed_work dwork;
115         struct regulator_dev *rdev;
116 };
117
118 struct sci_regulator_desc {
119         struct regulator_desc desc;
120         struct sci_regulator_ops *ops;
121         const struct sci_regulator_regs *regs;
122         struct sci_regulator_data data; /* FIXME: dynamic */
123 #if defined(CONFIG_DEBUG_FS)
124         struct dentry *debugfs;
125 #endif
126 };
127
128 enum {
129         VDD_TYP_LDO = 0,
130         VDD_TYP_LDO_D = 1,
131         VDD_TYP_DCDC = 2,
132         VDD_TYP_LPREF = 3,
133         VDD_TYP_BOOST = 4,
134 };
135
136 #define REGU_VERIFY_DLY (1000)  /*ms */
137
138 static DEFINE_MUTEX(adc_chan_mutex);
139 static int __is_trimming(struct regulator_dev *);
140 static int __regu_calibrate(struct regulator_dev *, int, int);
141 extern int sci_efuse_calibration_get(u32 * p_cal_data);
142
143 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
144                      VOL_TRM, VOL_TRM_BITS, CAL_CTL, CAL_CTL_BITS, VOL_DEF, \
145                      VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...)   \
146 do {                                                                                                            \
147         static const struct sci_regulator_regs REGS_##VDD = {   \
148                 .typ            = TYP,                                                                  \
149                 .pd_set = PD_SET,                                       \
150                 .pd_set_bit = SET_BIT,                                  \
151                 .pd_rst = PD_RST,                                       \
152                 .pd_rst_bit = RST_BIT,                                  \
153                 .slp_ctl = SLP_CTL,                                     \
154                 .slp_ctl_bit = SLP_CTL_BIT,                             \
155                 .vol_trm = VOL_TRM,                                 \
156                 .vol_trm_bits = VOL_TRM_BITS,                       \
157                 .cal_ctl = CAL_CTL,                                                             \
158                 .cal_ctl_bits = CAL_CTL_BITS,                                           \
159                 .vol_def = VOL_DEF,                                                                     \
160                 .vol_ctl = VOL_CTL,                                     \
161                 .vol_ctl_bits = VOL_CTL_BITS,                           \
162                 .vol_sel_cnt = VOL_SEL_CNT,                             \
163                 .vol_sel = {__VA_ARGS__},                               \
164         };                                                                                                              \
165         static struct sci_regulator_desc DESC_##VDD = {                 \
166                 .desc.name = #VDD,                                                                      \
167                 .desc.id = 0,                                                                           \
168                 .desc.ops = 0,                                                                          \
169                 .desc.type = REGULATOR_VOLTAGE,                                         \
170                 .desc.owner = THIS_MODULE,                                                      \
171                 .regs = &REGS_##VDD,                                                            \
172         };                                                                                                              \
173         sci_regulator_register(pdev, &DESC_##VDD);                              \
174 } while (0)
175
176 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
177 {
178         return (struct sci_regulator_desc *)rdev->desc;
179 }
180
181 /* standard ldo ops*/
182 static int ldo_turn_on(struct regulator_dev *rdev)
183 {
184         struct sci_regulator_desc *desc = __get_desc(rdev);
185         const struct sci_regulator_regs *regs = desc->regs;
186
187         debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
188                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
189                regs->pd_rst, __ffs(regs->pd_rst_bit));
190
191         if (regs->pd_rst)
192                 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
193
194         if (regs->pd_set)
195                 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
196
197         debug2("regu %p (%s), turn on\n", regs, desc->desc.name);
198         /* ldo trimming when first turn on */
199         if (desc->ops && !__is_trimming(rdev))
200                 __regu_calibrate(rdev, 0, 0);
201         return 0;
202 }
203
204 static int ldo_turn_off(struct regulator_dev *rdev)
205 {
206         struct sci_regulator_desc *desc = __get_desc(rdev);
207         const struct sci_regulator_regs *regs = desc->regs;
208
209         debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
210                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
211                regs->pd_rst, __ffs(regs->pd_rst_bit));
212 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
213         if (regs->pd_set)
214                 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
215
216         if (regs->pd_rst)
217                 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
218 #endif
219         debug2("regu %p (%s), turn off\n", regs, desc->desc.name);
220         return 0;
221 }
222
223 static int ldo_is_on(struct regulator_dev *rdev)
224 {
225         int ret = -EINVAL;
226         struct sci_regulator_desc *desc = __get_desc(rdev);
227         const struct sci_regulator_regs *regs = desc->regs;
228
229         debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
230                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
231                regs->pd_rst, __ffs(regs->pd_rst_bit));
232
233         if (regs->pd_rst && regs->pd_set) {
234                 /*for pd_rst has higher prioty than pd_set, what's more, their reset values are the same, 0 */
235                 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
236                 /* FIXME: when reset, pd_set & pd_rst are all zero, always get here */
237                 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
238                         ret = -EINVAL;
239         } else if (regs->pd_rst) {
240                 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
241         } else if (regs->pd_set) {      /* new feature */
242                 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
243         }
244
245         debug2("regu %p (%s) return %d\n", regs, desc->desc.name, ret);
246         return ret;
247 }
248
249 #if 0                           /* FIXME: todo later */
250 static int ldo_enable_time(struct regulator_dev *rdev)
251 {
252         return 1000 * 1;        /*Microseconds */
253 }
254 #endif
255
256 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
257 {
258         struct sci_regulator_desc *desc = __get_desc(rdev);
259         const struct sci_regulator_regs *regs = desc->regs;
260
261         debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
262               regs->slp_ctl, regs->slp_ctl_bit, mode);
263
264         if (!regs->slp_ctl)
265                 return -EINVAL;
266
267         if (mode == REGULATOR_MODE_STANDBY) {   /* disable auto slp */
268                 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
269         } else {
270                 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
271         }
272         return 0;
273 }
274
275 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
276                            int max_uV, unsigned *selector)
277 {
278         static const int vol_bits[4] = { 0xa, 0x9, 0x6, 0x5 };
279         struct sci_regulator_desc *desc = __get_desc(rdev);
280         const struct sci_regulator_regs *regs = desc->regs;
281         int mv = min_uV / 1000;
282         int ret = -EINVAL;
283         int i, shft = __ffs(regs->vol_ctl_bits);
284         int has_rst_bit = !(0x3 == (regs->vol_ctl_bits >> shft));       /* new feature */
285
286         BUG_ON(regs->vol_sel_cnt > 4);
287         debug("regu %p (%s) %d %d (%d)\n", regs, desc->desc.name, min_uV,
288               max_uV, has_rst_bit);
289
290         if (!regs->vol_ctl)
291                 return -EACCES;
292         for (i = 0; i < regs->vol_sel_cnt; i++) {
293                 if (regs->vol_sel[i] == mv) {
294                         ANA_REG_SET(regs->vol_ctl,
295                                     ((!has_rst_bit) ? i : vol_bits[i]) << shft,
296                                     regs->vol_ctl_bits);
297                         /*clear_bit(desc->desc.id, trimming_state); */
298                         ret = 0;
299                         break;
300                 }
301         }
302
303         WARN(0 != ret,
304              "warning: regulator (%s) not support %dmV\n", desc->desc.name, mv);
305         return ret;
306 }
307
308 static int ldo_get_voltage(struct regulator_dev *rdev)
309 {
310         struct sci_regulator_desc *desc = __get_desc(rdev);
311         const struct sci_regulator_regs *regs = desc->regs;
312         u32 vol, vol_bits;
313         int i, shft = __ffs(regs->vol_ctl_bits);
314         int has_rst_bit = !(0x3 == (regs->vol_ctl_bits >> shft));       /* new feature */
315
316         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
317                regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
318
319         if (!regs->vol_ctl)
320                 return -EACCES;
321
322         BUG_ON(regs->vol_sel_cnt != 4);
323         vol_bits = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
324
325         if (!has_rst_bit) {
326                 i = vol_bits;
327         } else if (((vol_bits & BIT(0)) ^ (vol_bits & BIT(1))
328                     && (vol_bits & BIT(2)) ^ (vol_bits & BIT(3)))) {
329                 i = (vol_bits & BIT(0)) | ((vol_bits >> 1) & BIT(1));
330         } else
331                 return -EFAULT;
332
333         vol = regs->vol_sel[i];
334         debug2("regu %p (%s), voltage %d\n", regs, desc->desc.name, vol);
335         return vol * 1000;
336 }
337
338 static unsigned long trimming_state[2] = { 0, 0 };      /* max 64 bits */
339
340 /* FIXME: get dcdc cal offset config from uboot */
341 #define DCDC_CAL_CONF_BASE      (SPRD_IRAM0_BASE + 0x1f00)
342 #define DCDC_MAX_CNT            (4)
343 struct dcdc_cal_t {
344         char name[32];
345         int cal_vol;
346 };
347
348 static int __dcdc_get_offset(struct regulator_dev *rdev)
349 {
350 #ifdef CONFIG_ARCH_SCX35
351         struct sci_regulator_desc *desc = __get_desc(rdev);
352         struct dcdc_cal_t *dcdc = (struct dcdc_cal_t *)DCDC_CAL_CONF_BASE;
353         int i;
354         for (i = 0; i < DCDC_MAX_CNT; i++) {
355                 if (0 == strcmp(dcdc[i].name, desc->desc.name)) {
356                         debug("regu %p (%s) offset %+dmV\n", desc->regs,
357                               desc->desc.name, dcdc[i].cal_vol);
358                         return dcdc[i].cal_vol * 1000;  /*uV */
359                 }
360         }
361 #endif
362         return 0;
363 }
364
365 static int __is_trimming(struct regulator_dev *rdev)
366 {
367         int id;
368         BUG_ON(!rdev);
369         id = rdev->desc->id;
370         BUG_ON(!(id > 0 && id < sizeof(trimming_state) * 8));
371         return test_bit(id, trimming_state);
372 }
373
374 static int __init_trimming(struct regulator_dev *rdev)
375 {
376         struct sci_regulator_desc *desc = __get_desc(rdev);
377         const struct sci_regulator_regs *regs = desc->regs;
378         int ret = -EINVAL;
379         u32 trim = 0;
380
381         if (!regs->vol_trm || !desc->ops)
382                 goto exit;
383
384         trim = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits)
385             >> __ffs(regs->vol_trm_bits);
386         if (0 == regs->vol_def && desc->regs->typ == 2 /*DCDC*/) {
387                 rdev->constraints->uV_offset = __dcdc_get_offset(rdev);
388         } else if (trim != desc->ops->trimming_def_val && !(regs->vol_def & 1)) {
389                 /* some DCDC/LDOs had been calibrated in uboot-spl */
390                 debug("regu %p (%s) trimming ok before startup\n", regs,
391                       desc->desc.name);
392                 set_bit(desc->desc.id, trimming_state);
393                 ret = trim;
394         } else
395                 ret = __regu_calibrate(rdev, 0, 0);
396
397 exit:
398         return ret;
399 }
400
401 /**
402  * ldo trimming step about 0.625%, range 90% ~ 109.375%. that all maps as follow.
403
404         0x1F :  +9.375 : 109.375
405         0x1E :  +8.750 : 108.750
406         0x1D :  +8.125 : 108.125
407         0x1C :  +7.500 : 107.500
408         0x1B :  +6.875 : 106.875
409         0x1A :  +6.250 : 106.250
410         0x19 :  +5.625 : 105.625
411         0x18 :  +5.000 : 105.000
412         0x17 :  +4.375 : 104.375
413         0x16 :  +3.750 : 103.750
414         0x15 :  +3.125 : 103.125
415         0x14 :  +2.500 : 102.500
416         0x13 :  +1.875 : 101.875
417         0x12 :  +1.250 : 101.250
418         0x11 :  +0.625 : 100.625
419         0x10 :  +0.000 : 100.000
420         0x0F :  -0.625 : 99.375
421         0x0E :  -1.250 : 98.750
422         0x0D :  -1.875 : 98.125
423         0x0C :  -2.500 : 97.500
424         0x0B :  -3.125 : 96.875
425         0x0A :  -3.750 : 96.250
426         0x09 :  -4.375 : 95.625
427         0x08 :  -5.000 : 95.000
428         0x07 :  -5.625 : 94.375
429         0x06 :  -6.250 : 93.750
430         0x05 :  -6.875 : 93.125
431         0x04 :  -7.500 : 92.500
432         0x03 :  -8.125 : 91.875
433         0x02 :  -8.750 : 91.250
434         0x01 :  -9.375 : 90.625
435         0x00 : -10.000 : 90.000
436
437  */
438 static int ldo_set_trimming(struct regulator_dev *rdev, int def_vol, int to_vol,
439                             int adc_vol)
440 {
441         struct sci_regulator_desc *desc = __get_desc(rdev);
442         const struct sci_regulator_regs *regs = desc->regs;
443         int ret = -EINVAL;
444
445         /* FIXME: always update voltage ctrl bits */
446 /*
447         ret =
448             rdev->desc->ops->set_voltage(rdev, to_vol * 1000, to_vol * 1000, 0);
449         if (IS_ERR_VALUE(ret) && regs->vol_ctl)
450                 goto exit;
451 */
452         if (regs->vol_trm) {
453                 u32 trim =      /* assert 5 valid trim bits, R = V_IDEAL / V_ADCIN - 1 */
454                     DIV_ROUND_UP((to_vol * 100 - adc_vol * 90) * 32,
455                                  (adc_vol * 20));
456                 if (trim > BIT(5) - 1)
457                         goto exit;
458                 debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
459                       regs, desc->desc.name, to_vol, adc_vol,
460                       (trim * 20 / 32 - 10), trim);
461
462 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
463                 ANA_REG_SET(regs->vol_trm,
464                             trim << __ffs(regs->vol_trm_bits),
465                             regs->vol_trm_bits);
466                 ret = 0;
467 #endif
468         }
469
470 exit:
471         return ret;
472 }
473
474 static int dcdcldo_set_trimming(struct regulator_dev *rdev, int def_vol,
475                                 int to_vol, int adc_vol)
476 {
477         struct sci_regulator_desc *desc = __get_desc(rdev);
478         const struct sci_regulator_regs *regs = desc->regs;
479         int ret = -EINVAL;
480
481         if (regs->vol_trm) {
482                 u32 trim = desc->ops->trimming_def_val;
483                 if (adc_vol > to_vol) {
484                         trim -=
485                             ((adc_vol - to_vol) * 100 * 32) / (adc_vol * 25);
486                 } else {
487                         trim +=
488                             DIV_ROUND_UP((to_vol - adc_vol) * 100 * 32,
489                                          (adc_vol * 25));
490                 }
491                 if (trim > BIT(5) - 1)
492                         goto exit;
493                 debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
494                       regs, desc->desc.name, to_vol, adc_vol,
495                       ((int)trim - 0x10) * 25 / 32, trim);
496
497 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
498                 ANA_REG_SET(regs->vol_trm,
499                             trim << __ffs(regs->vol_trm_bits),
500                             regs->vol_trm_bits);
501                 ret = 0;
502 #endif
503         }
504
505 exit:
506         return ret;
507 }
508
509 static int ldo_get_trimming_step(struct regulator_dev *rdev, int to_vol)
510 {
511         return 1000 * to_vol * 20 / 32; /*uV */
512 }
513
514 static int dcdcldo_get_trimming_step(struct regulator_dev *rdev, int to_vol)
515 {
516         return 1000 * to_vol * 25 / 32; /*uV */
517 }
518
519 /* FIXME: patch for sc7710 BA version */
520 #ifdef CONFIG_ARCH_SC7710
521 /**
522  * lpref trimming step about 1.39%, range -22.22% ~ +20.83%. that all maps as follow.
523         0x0F : +20.833 : 120.833
524         0x0E : +19.444 : 119.444
525         0x0D : +18.056 : 118.056
526         0x0C : +16.667 : 116.667
527         0x0B : +15.278 : 115.278
528         0x0A : +13.889 : 113.889
529         0x09 : +12.500 : 112.500
530         0x08 : +11.111 : 111.111
531         0x07 :  +9.722 : 109.722
532         0x06 :  +8.333 : 108.333
533         0x05 :  +6.944 : 106.944
534         0x04 :  +5.556 : 105.556
535         0x03 :  +4.167 : 104.167
536         0x02 :  +2.778 : 102.778
537         0x01 :  +1.389 : 101.389
538         0x00 :  +0.000 : 100.000
539         0x1F :  -1.389 : 98.611
540         0x1E :  -2.778 : 97.222
541         0x1D :  -4.167 : 95.833
542         0x1C :  -5.556 : 94.444
543         0x1B :  -6.944 : 93.056
544         0x1A :  -8.333 : 91.667
545         0x19 :  -9.722 : 90.278
546         0x18 : -11.111 : 88.889
547         0x17 : -12.500 : 87.500
548         0x16 : -13.889 : 86.111
549         0x15 : -15.278 : 84.722
550         0x14 : -16.667 : 83.333
551         0x13 : -18.056 : 81.944
552         0x12 : -19.444 : 80.556
553         0x11 : -20.833 : 79.167
554         0x10 : -22.222 : 77.778
555 */
556 static int lpref_set_trimming(struct regulator_dev *rdev, int def_vol,
557                               int to_vol, int adc_vol)
558 {
559         struct sci_regulator_desc *desc = __get_desc(rdev);
560         const struct sci_regulator_regs *regs = desc->regs;
561         int ret = -EINVAL;
562         u32 set_bits, rst_bits;
563
564         u32 trim =              /* assert 5 valid trim bits, R = V_IDEAL / V_ADCIN - 1 */
565             (abs(to_vol - adc_vol) * 72 / adc_vol);
566
567         if (to_vol < adc_vol)
568                 trim = ~trim & 0x1f;
569         if (trim < BIT(5) - 1)
570                 trim++;         /* FIXME: a little higher, according asic */
571         else if (trim > BIT(5) - 1)
572                 goto exit;
573
574         debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
575               regs, desc->desc.name, to_vol, adc_vol,
576               (100 - adc_vol * 100 / to_vol), trim);
577
578 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
579         mutex_lock(&adc_chan_mutex);
580         ANA_REG_BIC(ANA_REG_GLB_LDO_SW, BIT_WPA_DCDC_SEL);
581         switch (regs->cal_ctl_bits >> 16) {
582         case 0x1a:              //abb/rf/amp/emmcore/emmio/vddwif0/vddwif1
583                 set_bits = (trim >> 0) & (BIT(0) | BIT(1));
584                 ANA_REG_SET(ANA_REG_GLB_CHGR_CTRL0, set_bits << 8,
585                             BIT(9) | BIT(8));
586                 set_bits = (trim >> 2) & (BIT(0) | BIT(1));
587                 rst_bits = ~set_bits & (BIT(0) | BIT(1));
588                 ANA_REG_SET(ANA_REG_GLB_DCDC_MEM_CTRL2,
589                             set_bits << 10 | rst_bits << 14,
590                             BIT(10) | BIT(11) | BIT(14) | BIT(15));
591                 set_bits = (trim >> 4) & BIT(0);
592                 rst_bits = ~set_bits & BIT(0);
593                 ANA_REG_SET(ANA_REG_GLB_WPA_DCDC_CTRL1,
594                             set_bits << 10 | rst_bits << 14, BIT(10) | BIT(14));
595                 break;
596         case 0x19:              //vdd18/vdd28/vdd25/vddcama/vddcamd/vddmem/vddusb
597                 trim ^= BIT(4);
598                 ANA_REG_SET(ANA_REG_GLB_LDO_TRIM6, trim << 8,
599                             BIT(8) | BIT(9) | BIT(10) | BIT(11) | BIT(12));
600                 break;
601         case 0x18:              //sdio/sim0/sim1/sim2
602                 set_bits = (trim >> 0) & (BIT(0) | BIT(1));
603                 rst_bits = ~set_bits & (BIT(0) | BIT(1));
604                 ANA_REG_SET(ANA_REG_GLB_DCDC_ARM_CTRL2,
605                             set_bits << 10 | rst_bits << 14,
606                             BIT(10) | BIT(11) | BIT(14) | BIT(15));
607                 set_bits = (trim >> 2) & (BIT(0) | BIT(1));
608                 rst_bits = ~set_bits & (BIT(0) | BIT(1));
609                 ANA_REG_SET(ANA_REG_GLB_DCDC_CORE_CTRL2,
610                             set_bits << 10 | rst_bits << 14,
611                             BIT(10) | BIT(11) | BIT(14) | BIT(15));
612                 set_bits = (trim >> 4) & BIT(0);
613                 rst_bits = ~set_bits & BIT(0);
614                 ANA_REG_SET(ANA_REG_GLB_WPA_DCDC_CTRL1,
615                             set_bits << 11 | rst_bits << 15, BIT(11) | BIT(15));
616                 break;
617         default:
618                 break;
619         }
620
621         msleep(1);              /* FIXME: wait for lpref voltage is okay */
622         ANA_REG_OR(ANA_REG_GLB_LDO_SW, BIT_WPA_DCDC_SEL);
623         mutex_unlock(&adc_chan_mutex);
624         ret = 0;
625 #endif
626
627 exit:
628         return ret;
629 }
630
631 static int lpref_get_trimming_step(struct regulator_dev *rdev, int to_vol)
632 {
633         return 1000 * to_vol * 100 / 72;        /*uV */
634 }
635 #endif
636
637 /* standard dcdc ops*/
638 #define BITS_DCDC_CAL_RST(_x_)     ( (_x_) << 5 & (BIT(5)|BIT(6)|BIT(7)|BIT(8)|BIT(9)) )
639 #define BITS_DCDC_CAL(_x_)         ( (_x_) << 0 & (BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4)) )
640 static int dcdc_get_trimming_step(struct regulator_dev *rdev, int to_vol)
641 {
642         struct sci_regulator_desc *desc = __get_desc(rdev);
643         if (0 == strcmp(desc->desc.name, "vddmem")) {   /* FIXME: vddmem step 200/32mV */
644                 return 1000 * 200 / 32; /*uV */
645         }
646         return 1000 * 100 / 32; /*uV */
647 }
648
649 static int dcdc_set_trimming(struct regulator_dev *rdev,
650                              int def_vol, int to_vol, int adc_vol)
651 {
652         struct sci_regulator_desc *desc = __get_desc(rdev);
653         int acc_vol = desc->ops->get_trimming_step(rdev, to_vol);
654 /**
655  * FIXME: no need division?
656         int ctl_vol = DIV_ROUND_UP(def_vol * to_vol * 1000, adc_vol) + acc_vol;
657 */
658         int ctl_vol = 1000 * (to_vol - (adc_vol - def_vol)) + acc_vol;  /*uV */
659
660         /* FIXME: dcdc core ctrl should be keeped after trimming.
661          * but now, uV_offset is used for dcdc set/get correct voltage API.
662          */
663         rdev->constraints->uV_offset = ctl_vol - to_vol * 1000;
664         debug("regu (%s) ctl %d to %d, offset %dmv\n", desc->desc.name,
665               ctl_vol / 1000, to_vol, rdev->constraints->uV_offset / 1000);
666         return rdev->desc->ops->set_voltage(rdev, ctl_vol, ctl_vol, 0);
667 }
668
669 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
670 {
671         int i, j = -1;
672         int ds, min_ds = 100;   /* mV, the max range of small voltage */
673         for (i = 0; i < regs->vol_sel_cnt; i++) {
674                 ds = vol - regs->vol_sel[i];
675                 if (ds >= 0 && ds < min_ds) {
676                         min_ds = ds;
677                         j = i;
678                 }
679         }
680         return j;
681 }
682
683 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
684 {
685         int vol = rdev->desc->ops->get_voltage(rdev) / 1000;
686         if (vol > old_vol) {
687                 /* FIXME: for dcdc, each step (50mV) takes 10us */
688                 int dly = (vol - old_vol) * 10 / 50;
689                 WARN_ON(dly > 1000);
690                 udelay(dly);
691         }
692         return 0;
693 }
694
695 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
696                             int max_uV, unsigned *selector)
697 {
698         struct sci_regulator_desc *desc = __get_desc(rdev);
699         const struct sci_regulator_regs *regs = desc->regs;
700         int i, mv = min_uV / 1000;
701         int old_vol = rdev->desc->ops->get_voltage(rdev) / 1000;
702
703         debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
704
705         BUG_ON(0 != __ffs(regs->vol_trm_bits));
706         BUG_ON(regs->vol_sel_cnt > 8);
707
708         if (!regs->vol_ctl)
709                 return -EACCES;
710
711         /* found the closely vol ctrl bits */
712         i = __match_dcdc_vol(regs, mv);
713         if (i < 0)
714                 return WARN(-EINVAL,
715                             "not found %s closely ctrl bits for %dmV\n",
716                             desc->desc.name, mv);
717
718         debug2("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
719                mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
720
721 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
722         /* dcdc calibration control bits (default 00000),
723          * small adjust voltage: 100/32mv ~= 3.125mv
724          */
725         {
726                 int shft = __ffs(regs->vol_ctl_bits);
727                 int max = regs->vol_ctl_bits >> shft;
728                 int j = (mv - regs->vol_sel[i]) * 1000 /
729                     desc->ops->get_trimming_step(rdev, mv) % 32;
730
731                 if (regs->vol_trm == regs->vol_ctl) {   /* new feature */
732                         ANA_REG_SET(regs->vol_ctl, j | (i << shft),
733                                     regs->vol_trm_bits | regs->vol_ctl_bits);
734                 } else {
735                         if (regs->vol_trm) {    /* small adjust first */
736                                 ANA_REG_SET(regs->vol_trm,
737                                             BITS_DCDC_CAL(j) |
738                                             BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1)
739                                                               - j), -1);
740                         }
741
742                         ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
743                 }
744         }
745
746         __dcdc_enable_time(rdev, old_vol);
747 #endif
748
749         return 0;
750 }
751
752 /** CONFIG_ARCH_SCX35
753         bonding option 5
754         bonding option 4        dcdc_wrf_ctl[2]
755         bonding option 3
756         bonding option 2        dcdc_mem_ctl[2]
757         bonding option 1        dcdc_mem_ctl[1]
758         bonding option 0
759  */
760 static int dcdc_get_voltage(struct regulator_dev *rdev)
761 {
762         struct sci_regulator_desc *desc =
763             (struct sci_regulator_desc *)rdev->desc;
764         const struct sci_regulator_regs *regs = desc->regs;
765         u32 mv;
766         int cal = 0;            /* uV */
767         int i, shft = __ffs(regs->vol_ctl_bits);
768
769         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
770                regs, desc->desc.name, regs->vol_ctl,
771                shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
772
773         if (!regs->vol_ctl)
774                 return -EINVAL;
775
776         BUG_ON(0 != __ffs(regs->vol_trm_bits));
777         BUG_ON(regs->vol_sel_cnt > 8);
778
779         i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
780
781         mv = regs->vol_sel[i];
782
783         if (regs->vol_trm) {
784                 /*check the reset relative bit of vol ctl */
785                 if (regs->vol_trm != regs->vol_ctl) {
786                         u32 vol_bits =
787                             (~ANA_REG_GET(regs->vol_ctl) &
788                              (regs->vol_ctl_bits << 4)) >> 4;
789
790                         if (i != vol_bits) {
791 #if defined(CONFIG_ARCH_SC7710)
792                                 BUG_ON(0 != __ffs(regs->vol_ctl_bits));
793                                 WARN(!(0 == i
794                                        && regs->vol_ctl_bits == vol_bits),
795                                      "the reset relative ctrl bits of %s is invalid, %x",
796                                      desc->desc.name,
797                                      ANA_REG_GET(regs->vol_ctl));
798
799                                 vol_bits = ANA_REG_GET(regs->vol_trm);
800                                 i = vol_bits & regs->vol_trm_bits;
801                                 vol_bits = (~vol_bits
802                                             & (regs->vol_trm_bits << 5)) >> 5;
803                                 WARN(i != vol_bits
804                                      && !(0 == i
805                                           && regs->vol_trm_bits == vol_bits),
806                                      "the reset relative cal ctrl bits of %s is invalid, %x",
807                                      desc->desc.name,
808                                      ANA_REG_GET(regs->vol_trm));
809
810                                 /* FIXME: correct default value */
811                                 if (0 == strcmp(desc->desc.name, "vddarm"))
812                                         mv = 1200;
813 #else
814                                 return -EFAULT;
815 #endif
816                         }
817                 }
818
819                 cal = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits)
820                     * desc->ops->get_trimming_step(rdev, mv);   /*uV */
821         }
822
823         debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv,
824                cal / 1000);
825         return mv * 1000 + cal;
826 }
827
828 #if defined(CONFIG_ARCH_SC7710)
829 /* vddmem trimming: -100mv ~ +100 mV, step 6.25mV */
830 static int vmem_get_voltage(struct regulator_dev *rdev)
831 {
832         struct sci_regulator_desc *desc =
833             (struct sci_regulator_desc *)rdev->desc;
834         const struct sci_regulator_regs *regs = desc->regs;
835         u32 vol_bits;
836         int cal = 0;            /* uV */
837         int i, j, shft = __ffs(regs->vol_ctl_bits);
838
839         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
840                regs, desc->desc.name, regs->vol_ctl,
841                shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
842
843         BUG_ON(0 != __ffs(regs->vol_trm_bits));
844         BUG_ON(regs->vol_sel_cnt > 8);
845
846         i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
847         j = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits);
848
849         vol_bits =
850             (~ANA_REG_GET(regs->vol_ctl) & (regs->vol_ctl_bits << 4)) >> 4;
851
852         if (i != vol_bits)
853                 j = 0x10;
854
855         cal = (j - 0x10) * desc->ops->get_trimming_step(rdev, regs->vol_sel[i]);        /*uV */
856
857         debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name,
858                regs->vol_sel[i], cal / 1000);
859         return regs->vol_sel[i] * 1000 + cal;
860 }
861
862 static int vmem_set_voltage(struct regulator_dev *rdev, int min_uV,
863                             int max_uV, unsigned *selector)
864 {
865         struct sci_regulator_desc *desc = __get_desc(rdev);
866         const struct sci_regulator_regs *regs = desc->regs;
867         int i = 0, j, ctl_vol = min_uV, def_vol, acc_vol;
868         int shft = __ffs(regs->vol_ctl_bits);
869         int max = regs->vol_ctl_bits >> shft;
870
871         debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
872
873         def_vol = regs->vol_sel[i] * 1000;      /*FIXME: fixed vmem@1.8v */
874         acc_vol = desc->ops->get_trimming_step(rdev, 0);        /*uV */
875
876         j = (ctl_vol - def_vol + acc_vol * 0x10) / acc_vol;
877         if (j >= 0 && j < 32) {
878                 debug("regu %p (%s) %d = %d %+dmv\n", regs,
879                       desc->desc.name, ctl_vol / 1000, def_vol / 1000,
880                       (j - 0x10) * acc_vol / 1000);
881
882 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
883                 ANA_REG_SET(regs->vol_trm,
884                             BITS_DCDC_CAL(j) |
885                             BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1) - j), -1);
886
887                 ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
888 #endif
889                 return 0;
890         }
891         return WARN(-EINVAL,
892                     "not found %s closely ctrl bits for %dmV\n",
893                     desc->desc.name, ctl_vol / 1000);
894 }
895 #endif
896
897 /* standard boost ops*/
898 #define MAX_CURRENT_SINK        (500)   /*FIXME: max current sink */
899 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
900                                    int max_uA)
901 {
902         struct sci_regulator_desc *desc = __get_desc(rdev);
903         const struct sci_regulator_regs *regs = desc->regs;
904         int ma = min_uA / 1000;
905         int ret = -EACCES;
906         int i, shft = __ffs(regs->vol_ctl_bits);
907         int trim = (int)regs->vol_def / 1000;
908         int steps = (regs->vol_ctl_bits >> shft) + 1;
909
910         debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
911
912         if (!regs->vol_ctl)
913                 goto exit;
914
915         if (trim > 0) {
916                 trim <<= __ffs(regs->vol_trm_bits);
917         }
918
919         i = ma * steps / MAX_CURRENT_SINK;
920         if (i >= 0 && i < steps) {
921                 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
922                             regs->vol_ctl_bits | regs->vol_trm_bits);
923
924                 ret = 0;
925         }
926
927         WARN(0 != ret,
928              "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
929
930 exit:
931         return ret;
932 }
933
934 static int boost_get_current_limit(struct regulator_dev *rdev)
935 {
936         struct sci_regulator_desc *desc = __get_desc(rdev);
937         const struct sci_regulator_regs *regs = desc->regs;
938         u32 cur;
939         int i, shft = __ffs(regs->vol_ctl_bits);
940         int steps = (regs->vol_ctl_bits >> shft) + 1;
941
942         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
943                regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
944
945         if (!regs->vol_ctl)
946                 return -EACCES;
947
948         i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
949         cur = i * MAX_CURRENT_SINK / steps;
950         debug2("regu %p (%s), current %d\n", regs, desc->desc.name, cur);
951         return cur * 1000;
952 }
953
954 static int adc_sample_bit = 1;  /*12bits mode */
955 static short adc_data[3][2]
956 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
957     = {
958         {4200, 3320},           /* same as nv adc_t */
959         {3600, 2844},
960         {400, 316},             /* 0.4@VBAT, Reserved IdealC Value */
961 }
962 #endif
963 ;
964
965 static int __is_valid_adc_cal(void)
966 {
967         return 0 != adc_data[0][0];
968 }
969
970 static int __init __adc_cal_setup(char *str)
971 {
972         u32 *p = (u32 *) adc_data;
973         *p = simple_strtoul(str, &str, 0);
974         if (*p++ && *++str) {
975                 *p = simple_strtoul(str, &str, 0);
976                 if (*p) {
977                         debug("%d : %d -- %d : %d\n",
978                               (int)adc_data[0][0], (int)adc_data[0][1],
979                               (int)adc_data[1][0], (int)adc_data[1][1]);
980                         if (adc_data[0][1] < BIT(10)
981                             && adc_data[1][1] < BIT(10))
982                                 adc_sample_bit = 0;     /*10bits mode */
983 #if 0
984                         /* FIXME:
985                          * update adc data from kernel parameter,
986                          * and compensate 6~12mV if need.
987                          */
988                         adc_data[0][0] -= 6;
989                         adc_data[1][0] -= 6;
990 #endif
991                 }
992         }
993         return 0;
994 }
995 early_param("adc_cal", __adc_cal_setup);
996
997 static int __init __adc_cal_fuse_setup(void)
998 {
999         if (!__is_valid_adc_cal() &&
1000             sci_efuse_calibration_get((u32 *) adc_data)) {
1001                 debug("%d : %d -- %d : %d\n",
1002                       (int)adc_data[0][0], (int)adc_data[0][1],
1003                       (int)adc_data[1][0], (int)adc_data[1][1]);
1004         }
1005         return 0;
1006 }
1007
1008 static int __adc2vbat(int adc_res)
1009 {
1010         int t = adc_data[0][0] - adc_data[1][0];
1011         t *= (adc_res - adc_data[0][1]);
1012         t /= (adc_data[0][1] - adc_data[1][1]);
1013         t += adc_data[0][0];
1014         return t;
1015 }
1016
1017 #define MEASURE_TIMES   (15)
1018
1019 static void __dump_adc_result(u32 adc_val[])
1020 {
1021 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
1022         int i;
1023         for (i = 0; i < MEASURE_TIMES; i++) {
1024                 printk("%d ", adc_val[i]);
1025         }
1026         printk("\n");
1027 #endif
1028 }
1029
1030 static int cmp_val(const void *a, const void *b)
1031 {
1032         return *(int *)a - *(int *)b;
1033 }
1034
1035 /**
1036  * __adc_voltage - get regulator output voltage through auxadc
1037  * @regulator: regulator source
1038  *
1039  * This returns the current regulator voltage in mV.
1040  *
1041  * NOTE: If the regulator is disabled it will return the voltage value. This
1042  * function should not be used to determine regulator state.
1043  */
1044 static int regu_adc_voltage(struct regulator_dev *rdev)
1045 {
1046         struct sci_regulator_desc *desc = __get_desc(rdev);
1047         const struct sci_regulator_regs *regs = desc->regs;
1048
1049         int ret, adc_chan = regs->cal_ctl_bits >> 16;
1050         u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
1051         u32 adc_res, adc_val[MEASURE_TIMES];
1052         u32 chan_numerators = 1, chan_denominators = 1;
1053         u32 bat_numerators, bat_denominators;
1054
1055         struct adc_sample_data data = {
1056                 .channel_id = adc_chan,
1057                 .channel_type = 0,      /*sw */
1058                 .hw_channel_delay = 0,  /*reserved */
1059                 .scale = 1,     /*big scale */
1060                 .pbuf = &adc_val[0],
1061                 .sample_num = MEASURE_TIMES,
1062                 .sample_bits = adc_sample_bit,
1063                 .sample_speed = 0,      /*quick mode */
1064                 .signal_mode = 0,       /*resistance path */
1065         };
1066
1067         if (!__is_valid_adc_cal())
1068                 return -EACCES;
1069
1070         if (!regs->cal_ctl)
1071                 return -EINVAL;
1072
1073         /* enable ldo cal before adc sampling and ldo calibration */
1074         if (0 == regs->typ) {
1075                 mutex_lock(&adc_chan_mutex);
1076                 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
1077                 debug0("%s adc channel %d : %04x\n",
1078                        desc->desc.name, data.channel_id, ldo_cal_sel);
1079         }
1080
1081         ret = sci_adc_get_values(&data);
1082         BUG_ON(0 != ret);
1083
1084         /* close ldo cal and release multiplexed aux adc channel */
1085         if (0 == regs->typ) {
1086                 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
1087                 mutex_unlock(&adc_chan_mutex);
1088         }
1089
1090         __dump_adc_result(adc_val);
1091         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
1092         /*__dump_adc_result(adc_val);*/
1093
1094         sci_adc_get_vol_ratio(data.channel_id, data.scale,
1095                               &chan_numerators, &chan_denominators);
1096
1097 #ifdef CONFIG_ARCH_SCX35
1098         if (0 == strcmp(desc->desc.name, "vddcamio")) { /* FIXME: others is 1/2 */
1099                 chan_numerators = 1;
1100                 chan_denominators = 3;
1101         } else if (0 == strcmp(desc->desc.name, "vddwrf")) {    /* FIXME: bonding options? */
1102                 chan_numerators = 1;
1103                 chan_denominators = 3;
1104         }
1105 #endif
1106
1107         sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators,
1108                               &bat_denominators);
1109
1110         adc_res = adc_val[MEASURE_TIMES / 2];
1111         debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
1112               desc->desc.name, data.channel_id, ldo_cal_sel,
1113               chan_numerators, chan_denominators, adc_res);
1114
1115         if (adc_res == 0)
1116                 return -EAGAIN;
1117         else
1118                 return __adc2vbat(adc_res)
1119                     * (bat_numerators * chan_denominators)
1120                     / (bat_denominators * chan_numerators);
1121 }
1122
1123 static void do_regu_work(struct work_struct *w)
1124 {
1125         struct sci_regulator_data *data =
1126             container_of(w, struct sci_regulator_data, dwork.work);
1127         struct sci_regulator_desc *desc = __get_desc(data->rdev);
1128         debug0("%s\n", desc->desc.name);
1129         if (!__is_trimming(data->rdev)) {
1130                 mutex_lock(&data->rdev->mutex);
1131                 desc->ops->calibrate(data->rdev, 0, 0);
1132                 mutex_unlock(&data->rdev->mutex);
1133         }
1134 }
1135
1136 int __regu_calibrate(struct regulator_dev *rdev, int def_vol, int to_vol)
1137 {
1138         struct sci_regulator_desc *desc = __get_desc(rdev);
1139         const struct sci_regulator_regs *regs = desc->regs;
1140         int in_calibration(void);
1141         if (in_calibration() || !__is_valid_adc_cal()
1142             || !regs->vol_def || !regs->cal_ctl || !regs->vol_trm) {
1143                 /* FIXME: BYPASS if in CFT or not adc cal or no cal ctl
1144                  * or no def vol.
1145                  */
1146                 return -EACCES;
1147         }
1148
1149         schedule_delayed_work(&desc->data.dwork, msecs_to_jiffies(10));
1150         return 0;
1151 }
1152
1153 /*
1154  * FIXME: ASSERT dcdc/ldo is enabled
1155  */
1156 static int regu_calibrate(struct regulator_dev *rdev, int def_vol, int to_vol)
1157 {
1158         struct sci_regulator_desc *desc = __get_desc(rdev);
1159         const struct sci_regulator_regs *regs = desc->regs;
1160         int ret = 0, retry_count = 1;
1161         int adc_vol = 0, ctl_vol, cal_vol = 0;
1162
1163 retry:
1164         ctl_vol = rdev->desc->ops->get_voltage(rdev);
1165         if (IS_ERR_VALUE(ctl_vol)) {
1166                 debug0("no valid %s vol ctrl bits\n", desc->desc.name);
1167         } else                  /* dcdc/ldo maybe had been adjusted or opened in uboot-spl */
1168                 ctl_vol /= 1000;
1169
1170         if (!def_vol)
1171                 def_vol = (IS_ERR_VALUE(ctl_vol)) ? regs->vol_def : ctl_vol;
1172
1173         if (!to_vol) {
1174                 to_vol = (IS_ERR_VALUE(ctl_vol)) ? regs->vol_def : ctl_vol;
1175
1176                 /* FIXME: Ideal voltage maybe not chip default which in the choice */
1177                 if (to_vol != regs->vol_def) {
1178                         int i = __match_dcdc_vol(regs, regs->vol_def);
1179                         if (i >= 0 && regs->vol_sel[i] != regs->vol_def)
1180                                 to_vol = regs->vol_def;
1181                 }
1182         }
1183
1184         adc_vol = regu_adc_voltage(rdev);
1185         if (adc_vol <= 0) {
1186                 debug("%s default %dmv, maybe not enable\n",
1187                       desc->desc.name, def_vol);
1188                 goto exit;
1189         }
1190
1191         cal_vol = abs(adc_vol - to_vol);
1192         debug("%s default %dmv, from %dmv to %dmv, bias %c%d.%03d%%\n",
1193               desc->desc.name, def_vol, adc_vol, to_vol,
1194               (adc_vol > to_vol) ? '+' : '-',
1195               cal_vol * 100 / adc_vol, cal_vol * 100 * 1000 / adc_vol % 1000);
1196
1197         if (!def_vol || !to_vol || adc_vol <= 0)
1198                 goto exit;
1199
1200         if (abs(adc_vol - def_vol) >= def_vol / 9)      /* adjust limit 10% */
1201                 goto exit;
1202         else if (cal_vol < to_vol / 100) {      /* bias 1% */
1203                 /**
1204                  * FIXME: cal_vol * 100 / adc_vol <= 1 is okay for ldo lpref
1205                  */
1206                 set_bit(desc->desc.id, trimming_state);
1207                 debug("%s is okay\n", desc->desc.name);
1208                 return 0;
1209         } else if (0 == retry_count--) {
1210                 /* FIXME: unfortunately, dcdc/ldo need calibrate again */
1211                 WARN(1, "%s try again\n", desc->desc.name);
1212                 return def_vol;
1213         }
1214
1215         ret = desc->ops->set_trimming(rdev, def_vol, to_vol, adc_vol);
1216         if (IS_ERR_VALUE(ret))
1217                 goto exit;
1218
1219         def_vol = 0;            /*force reacquire */
1220         set_bit(desc->desc.id, trimming_state); /*force set before verify */
1221         msleep(REGU_VERIFY_DLY);        /* wait a moment before cal verify */
1222         goto retry;
1223
1224 exit:
1225         debug("%s failure\n", desc->desc.name);
1226         return -1;
1227 }
1228
1229 static int regu_force_trimming(struct regulator_dev *rdev, int trim)
1230 {
1231         struct sci_regulator_desc *desc = __get_desc(rdev);
1232         const struct sci_regulator_regs *regs = desc->regs;
1233
1234         if (regs->vol_trm)
1235                 ANA_REG_SET(regs->vol_trm,
1236                             trim << __ffs(regs->vol_trm_bits),
1237                             regs->vol_trm_bits);
1238         return 0;
1239 }
1240
1241 /**
1242  * regulator_strongly_disable - strongly disable regulator output
1243  * @regulator: regulator source
1244  *
1245  * Strongly try disable the regulator output voltage or current.
1246  * NOTE: this *will* disable the regulator output even if other consumer
1247  * devices have it enabled. This should be used for situations when device
1248  * had unbalanced with calls to regulator_enable().
1249  * *Not* recommended to call this function before try to balance the use_count.
1250  */
1251 int regulator_strongly_disable(struct regulator *regulator)
1252 {
1253         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
1254         int ret = 0;
1255
1256         if (rdev)
1257                 while (rdev->use_count--)
1258                         regulator_disable(regulator);
1259
1260         return ret;
1261 }
1262
1263 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
1264
1265 /**
1266  * regulator_calibrate - force calibrate the regulator to the ideal value
1267  * @regulator: regulator source
1268  *
1269  */
1270 int regulator_calibrate(struct regulator *regulator, int to_vol)
1271 {
1272         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
1273         int ret = -1;
1274
1275         if (rdev) {
1276                 struct sci_regulator_desc *desc = __get_desc(rdev);
1277                 if (desc && desc->ops) {
1278                         mutex_lock(&rdev->mutex);
1279                         ret = desc->ops->calibrate(rdev, 0, to_vol);
1280                         mutex_unlock(&rdev->mutex);
1281                 }
1282         }
1283
1284         return ret;
1285 }
1286
1287 EXPORT_SYMBOL_GPL(regulator_calibrate);
1288
1289 static struct regulator_ops ldo_ops = {
1290         .enable = ldo_turn_on,
1291         .disable = ldo_turn_off,
1292         .is_enabled = ldo_is_on,
1293         .set_voltage = ldo_set_voltage,
1294         .get_voltage = ldo_get_voltage,
1295         .set_mode = ldo_set_mode,
1296 /*      .enable_time = ldo_enable_time, */
1297 };
1298
1299 static struct regulator_ops usbd_ops = {
1300         .enable = 0,            /* reserved d-die ldo */
1301         .disable = 0,           /* reserved d-die ldo */
1302         .is_enabled = 0,        /* reserved d-die ldo */
1303 };
1304
1305 static struct regulator_ops dcdc_ops = {
1306         .enable = ldo_turn_on,
1307         .disable = ldo_turn_off,
1308         .is_enabled = ldo_is_on,
1309         .set_voltage = dcdc_set_voltage,
1310         .get_voltage = dcdc_get_voltage,
1311 };
1312
1313 static struct regulator_ops boost_ops = {
1314         .enable = ldo_turn_on,
1315         .disable = ldo_turn_off,
1316         .is_enabled = ldo_is_on,
1317         .set_current_limit = boost_set_current_limit,
1318         .get_current_limit = boost_get_current_limit,
1319         .set_mode = ldo_set_mode,
1320 };
1321
1322 static struct sci_regulator_ops sci_ldo_ops = {
1323         .trimming_def_val = 0x10,       /* 100% */
1324         .get_trimming_step = ldo_get_trimming_step,
1325         .set_trimming = ldo_set_trimming,
1326         .calibrate = regu_calibrate,
1327 };
1328
1329 static struct sci_regulator_ops sci_dcdcldo_ops = {
1330         .trimming_def_val = 0x10,       /* 100% */
1331         .get_trimming_step = dcdcldo_get_trimming_step,
1332         .set_trimming = dcdcldo_set_trimming,
1333         .calibrate = regu_calibrate,
1334 };
1335
1336 static struct sci_regulator_ops sci_dcdc_ops = {
1337         .get_trimming_step = dcdc_get_trimming_step,
1338         .set_trimming = dcdc_set_trimming,
1339         .calibrate = regu_calibrate,
1340 };
1341
1342 /*
1343  * Consider the following machine :-
1344  *
1345  *   Regulator-1 -+-> [Consumer A @ 1.8V]
1346  *                |
1347  *                +-> [Consumer B @ 1.8V]
1348  *
1349  *   Regulator-2 ---> [Consumer C @ 3.3V]
1350  *
1351  * The drivers for consumers A & B must be mapped to the correct regulator in
1352  * order to control their power supply. This mapping can be achieved in board/machine
1353  * initialisation code by creating a struct regulator_consumer_supply for each regulator.
1354  * Alternatively, we built a regulator supply-consumers map, the format is as follow:
1355  *
1356  *      supply source-1, consumer A, consumer B, ..., NULL
1357  *      supply source-2, consumer C, ..., NULL
1358  *      ...
1359  *      NULL
1360  *
1361  */
1362 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
1363                                                         const char *supply_name,
1364                                                         int *num)
1365 {
1366         char **map = (char **)dev_get_platdata(dev);
1367         int i, n;
1368         struct regulator_consumer_supply *consumer_supplies = NULL;
1369
1370         if (!supply_name || !(map && map[0]))
1371                 return NULL;
1372
1373         for (i = 0; map[i] || map[i + 1]; i++) {
1374                 if (map[i] && 0 == strcmp(map[i], supply_name))
1375                         break;
1376         }
1377
1378         /* i++; *//* Do not skip supply name */
1379
1380         for (n = 0; map[i + n]; n++) ;
1381
1382         if (n) {
1383                 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
1384                 consumer_supplies =
1385                     kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
1386                 BUG_ON(!consumer_supplies);
1387                 for (n = 0; map[i]; i++, n++) {
1388                         consumer_supplies[n].supply = map[i];
1389                 }
1390                 if (num)
1391                         *num = n;
1392         }
1393         return consumer_supplies;
1394 }
1395
1396 #if defined(CONFIG_DEBUG_FS)
1397 static struct dentry *debugfs_root = NULL;
1398
1399 static u32 ana_addr = 0;
1400 static int debugfs_ana_addr_get(void *data, u64 * val)
1401 {
1402         if (ana_addr < PAGE_SIZE) {
1403                 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
1404         } else {
1405                 void *addr = ioremap(ana_addr, PAGE_SIZE);
1406                 *val = __raw_readl(addr);
1407                 iounmap(addr);
1408         }
1409         return 0;
1410 }
1411
1412 static int debugfs_ana_addr_set(void *data, u64 val)
1413 {
1414         if (ana_addr < PAGE_SIZE) {
1415                 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val,
1416                             -1);
1417         } else {
1418                 void *addr = ioremap(ana_addr, PAGE_SIZE);
1419                 __raw_writel(val, addr);
1420                 iounmap(addr);
1421         }
1422         return 0;
1423 }
1424
1425 static int adc_chan = 5 /*VBAT*/;
1426 static int debugfs_adc_chan_get(void *pdata, u64 * val)
1427 {
1428         int i, ret;
1429         u32 adc_res, adc_val[MEASURE_TIMES];
1430         struct adc_sample_data data = {
1431                 .channel_id = adc_chan,
1432                 .channel_type = 0,      /*sw */
1433                 .hw_channel_delay = 0,  /*reserved */
1434                 .scale = 1,     /*big scale */
1435                 .pbuf = &adc_val[0],
1436                 .sample_num = MEASURE_TIMES,
1437                 .sample_bits = adc_sample_bit,
1438                 .sample_speed = 0,      /*quick mode */
1439                 .signal_mode = 0,       /*resistance path */
1440         };
1441
1442         ret = sci_adc_get_values(&data);
1443         BUG_ON(0 != ret);
1444
1445         for (i = 0; i < MEASURE_TIMES; i++) {
1446                 printk("%d ", adc_val[i]);
1447         }
1448         printk("\n");
1449
1450         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
1451         adc_res = adc_val[MEASURE_TIMES / 2];
1452         pr_info("adc chan %d, result value %d, vbat %d\n",
1453                 data.channel_id, adc_res, __adc2vbat(adc_res));
1454         *val = adc_res;
1455         return 0;
1456 }
1457
1458 static int debugfs_adc_chan_set(void *data, u64 val)
1459 {
1460         adc_chan = val;
1461         return 0;
1462 }
1463
1464 static int debugfs_enable_get(void *data, u64 * val)
1465 {
1466         struct regulator_dev *rdev = data;
1467         if (rdev && rdev->desc->ops->is_enabled)
1468                 *val = rdev->desc->ops->is_enabled(rdev);
1469         else
1470                 *val = -1;
1471         return 0;
1472 }
1473
1474 static int debugfs_enable_set(void *data, u64 val)
1475 {
1476         struct regulator_dev *rdev = data;
1477         if (rdev && rdev->desc->ops->enable)
1478                 (val) ? rdev->desc->ops->enable(rdev)
1479                     : rdev->desc->ops->disable(rdev);
1480         return 0;
1481 }
1482
1483 static int debugfs_voltage_get(void *data, u64 * val)
1484 {
1485         struct regulator_dev *rdev = data;
1486         if (rdev)
1487                 *val = regu_adc_voltage(rdev);
1488         else
1489                 *val = -1;
1490         return 0;
1491 }
1492
1493 static int debugfs_ldo_set(void *data, u64 val)
1494 {
1495         struct regulator_dev *rdev = data;
1496         if (rdev && rdev->desc->ops->set_voltage) {
1497                 if (val < 200)  /* FIXME: debug force trimming */
1498                         regu_force_trimming(rdev, val);
1499                 else
1500                         rdev->desc->ops->set_voltage(rdev, val * 1000,
1501                                                      val * 1000, 0);
1502         }
1503         return 0;
1504 }
1505
1506 static int debugfs_dcdc_set(void *data, u64 val)
1507 {
1508         struct regulator_dev *rdev = data;
1509         struct sci_regulator_desc *desc;
1510
1511         if (rdev) {
1512                 desc = __get_desc(rdev);
1513                 if (val < 200)  /* FIXME: debug force trimming */
1514                         regu_force_trimming(rdev, val);
1515                 else if (desc && desc->ops) {
1516                         mutex_lock(&rdev->mutex);
1517                         desc->ops->calibrate(rdev, 0, val);
1518                         mutex_unlock(&rdev->mutex);
1519                 }
1520         }
1521         return 0;
1522 }
1523
1524 static int debugfs_boost_get(void *data, u64 * val)
1525 {
1526         struct regulator_dev *rdev = data;
1527         if (rdev && rdev->desc->ops->get_current_limit)
1528                 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
1529         else
1530                 *val = -1;
1531         return 0;
1532 }
1533
1534 static int debugfs_boost_set(void *data, u64 val)
1535 {
1536         struct regulator_dev *rdev = data;
1537         if (rdev && rdev->desc->ops->set_current_limit)
1538                 rdev->desc->ops->set_current_limit(rdev, val * 1000,
1539                                                    val * 1000);
1540         return 0;
1541 }
1542
1543 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
1544                         debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
1545 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
1546                         debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
1547 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
1548                         debugfs_enable_get, debugfs_enable_set, "%llu\n");
1549 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
1550                         debugfs_voltage_get, debugfs_ldo_set, "%llu\n");
1551 DEFINE_SIMPLE_ATTRIBUTE(fops_dcdc,
1552                         debugfs_voltage_get, debugfs_dcdc_set, "%llu\n");
1553 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
1554                         debugfs_boost_get, debugfs_boost_set, "%llu\n");
1555
1556 static void rdev_init_debugfs(struct regulator_dev *rdev)
1557 {
1558         struct sci_regulator_desc *desc = __get_desc(rdev);
1559         desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
1560         if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
1561                 pr_warn("Failed to create debugfs directory\n");
1562                 rdev->debugfs = NULL;
1563                 return;
1564         }
1565
1566         debugfs_create_file("enable", S_IRUGO | S_IWUSR,
1567                             desc->debugfs, rdev, &fops_enable);
1568
1569         if (desc->desc.type == REGULATOR_CURRENT)
1570                 debugfs_create_file("current", S_IRUGO | S_IWUSR,
1571                                     desc->debugfs, rdev, &fops_boost);
1572         else
1573                 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
1574                                     desc->debugfs, rdev,
1575                                     (0 ==
1576                                      desc->regs->typ) ? &fops_ldo : &fops_dcdc);
1577 }
1578 #else
1579 static void rdev_init_debugfs(struct regulator_dev *rdev)
1580 {
1581 }
1582 #endif
1583
1584 #ifdef CONFIG_OF
1585 #include <linux/of.h>
1586 #define reg_info(format, arg...) pr_info("reg: " "@@@%s: " format, __func__, ## arg)
1587 #endif
1588
1589 static inline int __strcmp(const char *cs, const char *ct)
1590 {
1591         if (!cs || !ct) return -1;
1592         return strcmp(cs, ct);
1593 }
1594
1595 void * sci_regulator_register(struct platform_device *pdev,
1596                                        struct sci_regulator_desc *desc)
1597 {
1598         static atomic_t idx = ATOMIC_INIT(1);   /* 0: dummy */
1599         struct regulator_dev *rdev;
1600         struct regulator_config config = {};
1601         struct regulator_ops *__regs_ops[] = {
1602                 &ldo_ops, &usbd_ops, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops,
1603                 0,
1604         };
1605         struct sci_regulator_ops *__sci_regs_ops[] = {
1606                 &sci_ldo_ops, 0, &sci_dcdc_ops, 0, 0,
1607         };
1608         struct regulator_consumer_supply consumer_supplies_default[] = {
1609                 [0] = {
1610                        .supply = desc->desc.name,
1611                        }
1612         };
1613 #ifndef CONFIG_OF
1614         struct regulator_init_data init_data = {
1615                 .supply_regulator = 0,
1616                 .constraints = {
1617                                 .min_uV = 0,
1618                                 .max_uV = 4200 * 1000,
1619                                 .valid_modes_mask =
1620                                 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
1621                                 .valid_ops_mask =
1622                                 REGULATOR_CHANGE_STATUS |
1623                                 REGULATOR_CHANGE_VOLTAGE |
1624                                 REGULATOR_CHANGE_MODE,
1625                                 },
1626                 .num_consumer_supplies = 1,
1627                 .consumer_supplies = consumer_supplies_default,
1628                 .regulator_init = 0,
1629                 .driver_data = 0,
1630         };
1631 #else
1632         struct regulator_init_data *init_data;
1633         struct device_node *dev_np;
1634         struct device_node *node_np;
1635         dev_np = pdev->dev.of_node;
1636         node_np = of_get_child_by_name(dev_np, desc->desc.name);
1637         init_data = of_get_regulator_init_data(&pdev->dev, node_np);
1638         if(!init_data || 0 != __strcmp(init_data->constraints.name, desc->desc.name)){
1639                 dev_err(&pdev->dev, "out of memory or %s not found\n", desc->desc.name);
1640                 return NULL;
1641         }
1642         reg_info("[%d]%s range %d - %d\n", idx.counter, init_data->constraints.name,
1643                 init_data->constraints.min_uV, init_data->constraints.max_uV);
1644
1645         init_data->supply_regulator = 0;
1646         init_data->constraints.min_uV = 0,
1647         init_data->constraints.max_uV = 4200 * 1000;
1648         init_data->constraints.valid_modes_mask = 
1649                 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1650         init_data->constraints.valid_ops_mask = REGULATOR_CHANGE_MODE |
1651                 REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE;
1652         init_data->num_consumer_supplies = 1;
1653         init_data->consumer_supplies = consumer_supplies_default;
1654 #endif
1655
1656         desc->desc.id = atomic_inc_return(&idx) - 1;
1657
1658         BUG_ON(desc->regs->pd_set
1659                && desc->regs->pd_set == desc->regs->pd_rst
1660                && desc->regs->pd_set_bit == desc->regs->pd_rst_bit);
1661
1662         if (!desc->ops)
1663                 desc->ops = __sci_regs_ops[desc->regs->typ];
1664
1665         BUG_ON(desc->regs->typ >= ARRAY_SIZE(__regs_ops));
1666         if (!desc->desc.ops)
1667                 desc->desc.ops = __regs_ops[desc->regs->typ];
1668
1669 #ifdef CONFIG_ARCH_SCX35
1670         if (desc->regs->typ == VDD_TYP_BOOST) { /*FIXME: reconfig current sink */
1671 #ifndef CONFIG_OF
1672                 init_data.constraints.min_uA = 0;
1673                 init_data.constraints.max_uA = MAX_CURRENT_SINK * 1000;
1674                 init_data.constraints.valid_ops_mask |=
1675                     REGULATOR_CHANGE_CURRENT;
1676 #else
1677                 init_data->constraints.min_uA = 0;
1678                 init_data->constraints.max_uA = MAX_CURRENT_SINK * 1000;
1679                 init_data->constraints.valid_ops_mask |=
1680                     REGULATOR_CHANGE_CURRENT;
1681 #endif
1682                 desc->desc.type = REGULATOR_CURRENT;
1683         }
1684
1685         if (desc->regs->typ == VDD_TYP_LDO) {   /*FIXME: reconfig dcdcldo ops */
1686                 if ((desc->regs->cal_ctl_bits & 0xFFFF0000) ==
1687                     (BIT(17) | BIT(18) | BIT(20))) {
1688                         desc->ops = &sci_dcdcldo_ops;
1689                 }
1690         }
1691 #endif
1692
1693 /* FIXME: patch for sc7710 BA version */
1694 #ifdef CONFIG_ARCH_SC7710
1695         if (sci_get_ana_chip_id() == ANA_CHIP_ID_BA &&
1696             desc->regs->typ == VDD_TYP_LPREF) {
1697                 static struct sci_regulator_ops sci_lpref_ops = {
1698                         .get_trimming_step = lpref_get_trimming_step,
1699                         .set_trimming = lpref_set_trimming,
1700                         .calibrate = regu_calibrate,
1701                 };
1702
1703                 BUG_ON(VDD_TYP_LPREF != 3);
1704                 sci_lpref_ops.trimming_def_val = sci_ldo_ops.trimming_def_val;
1705                 desc->ops = &sci_lpref_ops;
1706                 desc->desc.ops = &ldo_ops;
1707         }
1708
1709         if (0 == strcmp(desc->desc.name, "vddmem")) {
1710                 static struct sci_regulator_ops sci_vmem_ops = {
1711                         .get_trimming_step = dcdc_get_trimming_step,
1712                         .set_trimming = dcdc_set_trimming,
1713                         .calibrate = regu_calibrate,
1714                 };
1715                 static struct regulator_ops vmem_ops = {
1716                         .enable = ldo_turn_on,
1717                         .disable = ldo_turn_off,
1718                         .is_enabled = ldo_is_on,
1719                         .set_voltage = vmem_set_voltage,
1720                         .get_voltage = vmem_get_voltage,
1721                 };
1722
1723                 if (sci_get_ana_chip_id() == ANA_CHIP_ID_BA) {
1724                         desc->ops = &sci_vmem_ops;
1725                         desc->desc.ops = &vmem_ops;
1726                 } else
1727                         desc->desc.ops = 0;     /*FIXME: reserved for board v1.1.0 */
1728         }
1729 #endif
1730
1731 #ifndef CONFIG_OF
1732         init_data.consumer_supplies =
1733             set_supply_map(&pdev->dev, desc->desc.name,
1734                            &init_data.num_consumer_supplies);
1735
1736         if (!init_data.consumer_supplies)
1737                 init_data.consumer_supplies = consumer_supplies_default;
1738
1739         debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1740 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1741         config.dev = &pdev->dev;
1742         config.init_data = &init_data;
1743         config.driver_data = 0;
1744         rdev = regulator_register(&desc->desc, &config);
1745 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1746         rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0, 0);
1747 #else
1748         rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0);
1749 #endif
1750         if (init_data.consumer_supplies != consumer_supplies_default)
1751                 kfree(init_data.consumer_supplies);
1752 #else /* CONFIG_OF */
1753         init_data->consumer_supplies =
1754             set_supply_map(&pdev->dev, desc->desc.name,
1755                            &init_data->num_consumer_supplies);
1756
1757         if (!init_data->consumer_supplies)
1758                 init_data->consumer_supplies = consumer_supplies_default;
1759
1760         debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1761 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1762         config.dev = &pdev->dev;
1763         config.init_data = init_data;
1764         config.of_node = node_np;
1765         config.driver_data = 0;
1766         rdev = regulator_register(&desc->desc, &config);
1767 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1768         rdev = regulator_register(&desc->desc, &pdev->dev, init_data, 0, 0);
1769 #else
1770         rdev = regulator_register(&desc->desc, &pdev->dev, init_data, 0);
1771 #endif
1772         if (init_data->consumer_supplies != consumer_supplies_default)
1773                 kfree(init_data->consumer_supplies);
1774 #endif /* end of CONFIG_OF */
1775
1776         if (!IS_ERR(rdev)) {
1777                 rdev->reg_data = rdev;
1778                 INIT_DELAYED_WORK(&desc->data.dwork, do_regu_work);
1779                 desc->data.rdev = rdev;
1780                 __init_trimming(rdev);
1781                 rdev_init_debugfs(rdev);
1782         }
1783         return rdev;
1784 }
1785
1786 /**
1787  * IMPORTANT!!!
1788  * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1789  * so i autogen all regulators non-variable description in plat or mach directory,
1790  * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1791  * just like other regulator vendor drivers.
1792  */
1793 static int sci_regulator_probe(struct platform_device *pdev)
1794 {
1795 #ifdef CONFIG_DEBUG_FS
1796                 debugfs_root =
1797                         debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1798                 if (IS_ERR(debugfs_root) || !debugfs_root) {
1799                         WARN(!debugfs_root,
1800                          "%s: Failed to create debugfs directory\n", REGULATOR_ROOT_DIR);
1801                         debugfs_root = NULL;
1802                 }
1803
1804                 debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1805                                    debugfs_root, (u32 *) & ana_addr);
1806                 debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1807                                         debugfs_root, &ana_addr, &fops_ana_addr);
1808                 debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1809                                         debugfs_root, &adc_chan, &fops_adc_chan);
1810                 debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1811                                    debugfs_root, (u64 *) & adc_data);
1812
1813                 {/* vddarm/vddcore/vddmem common debugfs interface */
1814                         char str[NAME_MAX];
1815                         struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1816                         sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1817                         debugfs_create_symlink("dcdcarm", vol_root, str);
1818                         sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1819                         debugfs_create_symlink("dcdccore", vol_root, str);
1820                         sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1821                         debugfs_create_symlink("dcdcmem", vol_root, str);
1822                 }
1823 #endif
1824
1825         pr_info("sc271x ana chip id: (0x%08x), bond opt (0x%08x)\n",
1826                 (sci_get_ana_chip_id() | sci_get_ana_chip_ver()),
1827                 ANA_REG_GET(ANA_REG_GLB_ANA_STATUS));
1828
1829 #include CONFIG_REGULATOR_SPRD_MAP
1830         return 0;
1831 }
1832
1833 #ifdef CONFIG_OF
1834 static struct of_device_id sprd_regulator_of_match[] = {
1835         { .compatible = "sprd,sc2713-regulator", },
1836         { }
1837 };
1838 #endif
1839
1840 static struct platform_driver sci_regulator_driver = {
1841         .driver = {
1842                    .name = "sc2713-regulator",
1843                    .owner = THIS_MODULE,
1844                    .of_match_table = of_match_ptr(sprd_regulator_of_match),
1845                    },
1846         .probe = sci_regulator_probe,
1847 };
1848
1849 static int __init regu_driver_init(void)
1850 {
1851         __adc_cal_fuse_setup();
1852
1853         return platform_driver_register(&sci_regulator_driver);
1854 }
1855
1856 int __init sci_regulator_init(void)
1857 {
1858 #ifndef CONFIG_OF
1859         static struct platform_device regulator_device = {
1860                 .name = "sc2713-regulator",
1861                 .id = -1,
1862         };
1863         static struct platform_device sc2713s_regulator_device = {
1864                 .name = "sc2713s-regulator",
1865                 .id = -1,
1866         };
1867
1868         if (sci_get_ana_chip_id() == 0x2713C000) /* SC2713S */
1869                 return platform_device_register(&sc2713s_regulator_device);
1870         else
1871                 return platform_device_register(&regulator_device);
1872 #else
1873         return of_platform_populate(of_find_node_by_path("/sprd-regulators"),
1874                                 sprd_regulator_of_match, NULL, NULL);
1875 #endif
1876 }
1877
1878 subsys_initcall(regu_driver_init);
1879
1880 MODULE_LICENSE("GPL v2");
1881 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1882 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");
1883 MODULE_VERSION("0.4");