drm/prime: add return check for dma_buf_fd
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / regulator / sc2711-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.2 unprotect dcdc/ldo before turn on and off
16  *              remove dcdc/ldo calibration
17  * To Fix:
18  *
19  *
20  */
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/version.h>
25 #include <linux/spinlock.h>
26 #include <linux/debugfs.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
29 #include <linux/delay.h>
30 #include <linux/err.h>
31 #include <linux/io.h>
32 #include <linux/platform_device.h>
33 #include <linux/of.h>
34 #include <linux/of_device.h>
35 #include <linux/regulator/of_regulator.h>
36
37 #include <linux/regulator/consumer.h>
38 #include <linux/regulator/driver.h>
39 #include <linux/regulator/machine.h>
40
41 #include <mach/hardware.h>
42 #include <mach/sci.h>
43 #include <mach/sci_glb_regs.h>
44 #include <mach/adi.h>
45 #include <mach/adc.h>
46 #include <mach/arch_misc.h>
47
48 #define REGULATOR_ROOT_DIR      "sprd-regulator"
49
50 #undef debug
51 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
52 #define debug0(format, arg...)  //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
53 #define debug2(format, arg...)  pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
54
55 #ifndef ANA_REG_OR
56 #define ANA_REG_OR(_r, _b)      sci_adi_write(_r, _b, 0)
57 #endif
58
59 #ifndef ANA_REG_BIC
60 #define ANA_REG_BIC(_r, _b)     sci_adi_write(_r, 0, _b)
61 #endif
62
63 #ifndef ANA_REG_GET
64 #define ANA_REG_GET(_r)         sci_adi_read(_r)
65 #endif
66
67 #ifndef ANA_REG_SET
68 #define ANA_REG_SET(_r, _v, _m) sci_adi_write((_r), ((_v) & (_m)), (_m))
69 #endif
70
71 struct sci_regulator_regs {
72         int typ;
73         u32 pd_set, pd_set_bit;
74         /**
75          * at new feature, some LDOs had only set, no rst bits.
76          * and DCDCs voltage and trimming controller is the same register
77          */
78         u32 pd_rst, pd_rst_bit;
79         u32 slp_ctl, slp_ctl_bit;
80         u32 vol_trm, vol_trm_bits;
81         u32 cal_ctl, cal_ctl_bits;
82         u32 vol_def;
83         u32 vol_ctl, vol_ctl_bits;
84         u32 vol_sel_cnt, vol_sel[];
85 };
86
87 struct sci_regulator_data {
88         struct regulator_dev *rdev;
89 };
90
91 struct sci_regulator_desc {
92         struct regulator_desc desc;
93         struct sci_regulator_ops *ops;
94         struct sci_regulator_regs *regs;
95         struct sci_regulator_data data; /* FIXME: dynamic */
96 #if defined(CONFIG_DEBUG_FS)
97         struct dentry *debugfs;
98 #endif
99 };
100
101 enum {
102         VDD_TYP_LDO = 0,
103         VDD_TYP_LDO_D = 1,
104         VDD_TYP_DCDC = 2,
105         VDD_TYP_LPREF = 3,
106         VDD_TYP_BOOST = 4,
107 };
108
109 #define REGU_VERIFY_DLY (1000)  /*ms */
110
111 static u16 ana_status_bondopt4;
112 static u32 ana_chip_id;
113
114 static DEFINE_MUTEX(adc_chan_mutex);
115 extern int sci_efuse_calibration_get(u32 * p_cal_data);
116
117 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
118                      VOL_TRM, VOL_TRM_BITS, CAL_CTL, CAL_CTL_BITS, VOL_DEF, \
119                      VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...)   \
120 do {                                                                                                            \
121         static struct sci_regulator_regs REGS_##VDD = { \
122                 .typ            = TYP,                                                                  \
123                 .pd_set = PD_SET,                                       \
124                 .pd_set_bit = SET_BIT,                                  \
125                 .pd_rst = PD_RST,                                       \
126                 .pd_rst_bit = RST_BIT,                                  \
127                 .slp_ctl = SLP_CTL,                                     \
128                 .slp_ctl_bit = SLP_CTL_BIT,                             \
129                 .vol_trm = VOL_TRM,                                 \
130                 .vol_trm_bits = VOL_TRM_BITS,                       \
131                 .cal_ctl = CAL_CTL,                                                             \
132                 .cal_ctl_bits = CAL_CTL_BITS,                                           \
133                 .vol_def = VOL_DEF,                                                                     \
134                 .vol_ctl = VOL_CTL,                                     \
135                 .vol_ctl_bits = VOL_CTL_BITS,                           \
136                 .vol_sel_cnt = VOL_SEL_CNT,                             \
137                 .vol_sel = {__VA_ARGS__},                               \
138         };                                                                                                              \
139         static struct sci_regulator_desc DESC_##VDD = {                 \
140                 .desc.name = #VDD,                                                                      \
141                 .desc.id = 0,                                                                           \
142                 .desc.ops = 0,                                                                          \
143                 .desc.type = REGULATOR_VOLTAGE,                                         \
144                 .desc.owner = THIS_MODULE,                                                      \
145                 .regs = &REGS_##VDD,                                                            \
146         };                                                                                                              \
147         sci_regulator_register(pdev, &DESC_##VDD);                              \
148 } while (0)
149
150 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
151 {
152         return (struct sci_regulator_desc *)rdev->desc;
153 }
154
155 /* standard ldo ops*/
156 static int ldo_turn_on(struct regulator_dev *rdev)
157 {
158         struct sci_regulator_desc *desc = __get_desc(rdev);
159         const struct sci_regulator_regs *regs = desc->regs;
160
161         debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
162                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
163                regs->pd_rst, __ffs(regs->pd_rst_bit));
164
165         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
166                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
167                                   BITS_PWR_WR_PROT_VALUE(0x6e7f));
168
169         if (regs->pd_rst)
170                 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
171
172         if (regs->pd_set)
173                 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
174
175         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
176                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
177
178         debug2("regu %p (%s), turn on\n", regs, desc->desc.name);
179         return 0;
180 }
181
182 static int ldo_turn_off(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 !defined(CONFIG_REGULATOR_CAL_DUMMY)
192         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
193                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
194                                   BITS_PWR_WR_PROT_VALUE(0x6e7f));
195
196         if (regs->pd_set)
197                 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
198
199         if (regs->pd_rst)
200                 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
201
202         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
203                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
204
205 #endif
206
207         debug2("regu %p (%s), turn off\n", regs, desc->desc.name);
208         return 0;
209 }
210
211 static int ldo_is_on(struct regulator_dev *rdev)
212 {
213         int ret = -EINVAL;
214         struct sci_regulator_desc *desc = __get_desc(rdev);
215         const struct sci_regulator_regs *regs = desc->regs;
216
217         debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
218                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
219                regs->pd_rst, __ffs(regs->pd_rst_bit));
220
221         if (regs->pd_rst && regs->pd_set) {
222                 /*for pd_rst has higher prioty than pd_set, what's more, their reset values are the same, 0 */
223                 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
224                 /* FIXME: when reset, pd_set & pd_rst are all zero, always get here */
225                 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
226                         ret = -EINVAL;
227         } else if (regs->pd_rst) {
228                 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
229         } else if (regs->pd_set) {      /* new feature */
230                 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
231         }
232
233         debug2("regu %p (%s) return %d\n", regs, desc->desc.name, ret);
234         return ret;
235 }
236
237 #if 0                           /* FIXME: todo later */
238 static int ldo_enable_time(struct regulator_dev *rdev)
239 {
240         return 1000 * 1;        /*Microseconds */
241 }
242 #endif
243
244 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
245 {
246         struct sci_regulator_desc *desc = __get_desc(rdev);
247         const struct sci_regulator_regs *regs = desc->regs;
248
249         debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
250               regs->slp_ctl, regs->slp_ctl_bit, mode);
251
252         if (!regs->slp_ctl)
253                 return -EINVAL;
254
255         if (mode == REGULATOR_MODE_STANDBY) {   /* disable auto slp */
256                 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
257         } else {
258                 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
259         }
260         return 0;
261 }
262
263 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
264                            int max_uV, unsigned *selector)
265 {
266         struct sci_regulator_desc *desc = __get_desc(rdev);
267         const struct sci_regulator_regs *regs = desc->regs;
268         int mv = min_uV / 1000;
269         int ret = -EINVAL;
270
271         debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
272
273         if (!regs->vol_ctl && regs->vol_trm && regs->vol_sel_cnt == 2) {
274                 int shft = __ffs(regs->vol_trm_bits);
275                 u32 trim =      /* vol_sel[0]: output (mV), [1]:step (uV) */
276                     DIV_ROUND_UP((mv - regs->vol_sel[0]) * 1000,
277                                  regs->vol_sel[1]);
278
279                 ret = trim > (regs->vol_trm_bits >> shft);
280                 WARN(0 != ret,
281                      "warning: regulator (%s) not support %dmV\n",
282                      desc->desc.name, mv);
283
284                 if (0 == ret) {
285                         ANA_REG_SET(regs->vol_trm,
286                                     trim << __ffs(regs->vol_trm_bits),
287                                     regs->vol_trm_bits);
288                 }
289                 return ret;
290         }
291
292         return ret;
293 }
294
295 static int ldo_get_voltage(struct regulator_dev *rdev)
296 {
297         struct sci_regulator_desc *desc = __get_desc(rdev);
298         const struct sci_regulator_regs *regs = desc->regs;
299         u32 vol;
300
301         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
302                regs, desc->desc.name, regs->vol_ctl, 0, regs->vol_ctl_bits);
303
304         if (!regs->vol_ctl && regs->vol_trm && regs->vol_sel_cnt == 2) {
305                 int shft = __ffs(regs->vol_trm_bits);
306                 u32 trim =
307                     (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft;
308                 vol = regs->vol_sel[0] * 1000 + trim * regs->vol_sel[1];
309                 debug2("regu %p (%s), voltage %d\n", regs, desc->desc.name,
310                        vol);
311                 return vol;
312         }
313
314         return -EFAULT;
315 }
316
317 /* FIXME: get dcdc cal offset config from uboot */
318 #define DCDC_CAL_CONF_BASE      (SPRD_IRAM0_BASE + 0x1f00)
319 #define DCDC_MAX_CNT            (4)
320 struct dcdc_cal_t {
321         char name[32];
322         int cal_vol;
323 };
324
325 typedef struct {
326         u16 ideal_vol;
327         const char name[14];
328 }vol_para_t;
329
330 #define PP_VOL_PARA             ( 0x50005c20 )  /* assert in iram2 */
331 #define TO_IRAM2(_p_)   ( SPRD_IRAM2_BASE + (u32)(_p_) - SPRD_IRAM2_PHYS )
332 #define IN_IRAM2(_p_)   ( (u32)(_p_) >= SPRD_IRAM2_PHYS && (u32)(_p_) < SPRD_IRAM2_PHYS + SPRD_IRAM2_SIZE )
333
334 int regulator_default_get(const char con_id[])
335 {
336         int i = 0, res = 0;
337         vol_para_t *pvol_para = (vol_para_t *)__raw_readl((void *)TO_IRAM2(PP_VOL_PARA));
338
339         if (!(IN_IRAM2(pvol_para)))
340                 return 0;
341
342         pvol_para = (vol_para_t *)TO_IRAM2(pvol_para);
343
344         if(strcmp((pvol_para)[0].name, "volpara_begin") || (0xfaed != (pvol_para)[0].ideal_vol))
345                 return 0;
346
347         while(0 != strcmp((pvol_para)[i++].name, "volpara_end")) {
348                 if (0 == strcmp((pvol_para)[i].name, con_id)) {
349                         debug("%s name %s, ideal_vol %d\n", __func__, (pvol_para)[i].name, (pvol_para)[i].ideal_vol);
350                         return res = (pvol_para)[i].ideal_vol;
351                 }
352         }
353         return res;
354 }
355
356 static int __init_trimming(struct regulator_dev *rdev)
357 {
358         struct sci_regulator_desc *desc = __get_desc(rdev);
359         const struct sci_regulator_regs *regs = desc->regs;
360         int ctl_vol, to_vol;
361
362         if (!regs->vol_trm)
363                 goto exit;
364
365         to_vol = regulator_default_get(desc->desc.name);
366         if (!to_vol) {
367                 to_vol = regs->vol_def;
368         }
369
370         if (to_vol && rdev->desc->ops->get_voltage) {
371                 ctl_vol = rdev->desc->ops->get_voltage(rdev);
372                 rdev->constraints->uV_offset = ctl_vol - to_vol * 1000;//uV
373                 debug("regu %p (%s), uV offset %d\n", regs, desc->desc.name, rdev->constraints->uV_offset);
374         }
375 exit:
376         return 0;
377 }
378
379 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
380 {
381         int i, j = -1;
382         int ds, min_ds = 100;   /* mV, the max range of small voltage */
383         for (i = 0; i < regs->vol_sel_cnt; i++) {
384                 ds = vol - regs->vol_sel[i];
385                 if (ds >= 0 && ds < min_ds) {
386                         min_ds = ds;
387                         j = i;
388                 }
389         }
390         return j;
391 }
392
393 static int dcdc_get_trimming_step(struct regulator_dev *rdev, int to_vol)
394 {
395         struct sci_regulator_desc *desc = __get_desc(rdev);
396
397         /* FIXME: vddmem step 200/32mV */
398         return (0 == strcmp(desc->desc.name, "vddmem") ) ? (1000 * 200 / 32) : (1000 * 100 / 32) /*uV */;
399 }
400
401 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
402 {
403         int vol = rdev->desc->ops->get_voltage(rdev) / 1000;
404         if (vol > old_vol) {
405                 /* FIXME: for dcdc, each step (50mV) takes 10us */
406                 int dly = (vol - old_vol) * 10 / 50;
407                 WARN_ON(dly > 1000);
408                 udelay(dly);
409         }
410         return 0;
411 }
412
413 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
414                             int max_uV, unsigned *selector)
415 {
416         struct sci_regulator_desc *desc = __get_desc(rdev);
417         const struct sci_regulator_regs *regs = desc->regs;
418         int i, mv = min_uV / 1000;
419         int old_vol = rdev->desc->ops->get_voltage(rdev) / 1000;
420
421         debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
422
423         BUG_ON(0 != __ffs(regs->vol_trm_bits));
424         BUG_ON(regs->vol_sel_cnt > 8);
425
426         if (!regs->vol_ctl)
427                 return -EACCES;
428
429         /* found the closely vol ctrl bits */
430         i = __match_dcdc_vol(regs, mv);
431         if (i < 0)
432                 return WARN(-EINVAL,
433                             "not found %s closely ctrl bits for %dmV\n",
434                             desc->desc.name, mv);
435
436         debug2("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
437                mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
438
439 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
440         /* dcdc calibration control bits (default 00000),
441          * small adjust voltage: 100/32mv ~= 3.125mv
442          */
443         {
444                 int shft = __ffs(regs->vol_ctl_bits);
445                 int j = (int)(mv - regs->vol_sel[i]) * 1000 / dcdc_get_trimming_step(rdev, 0);
446
447                 if (0 == strcmp(desc->desc.name, "vddmem") )
448                         j += 0x10;
449
450                 BUG_ON(j > (regs->vol_trm_bits >> __ffs(regs->vol_trm_bits)));
451
452                 if (regs->vol_trm == regs->vol_ctl) {   /* new feature */
453                         ANA_REG_SET(regs->vol_ctl, j | (i << shft),
454                                     regs->vol_trm_bits | regs->vol_ctl_bits);
455                 } else {
456                         BUG_ON(1);
457                 }
458         }
459
460         __dcdc_enable_time(rdev, old_vol);
461 #endif
462
463         return 0;
464 }
465
466 /** CONFIG_ARCH_SCX35
467         bonding option 5
468         bonding option 4        dcdc_wrf_ctl[2]
469         bonding option 3
470         bonding option 2        dcdc_mem_ctl[2]
471         bonding option 1        dcdc_mem_ctl[1]
472         bonding option 0
473  */
474 static int dcdc_get_voltage(struct regulator_dev *rdev)
475 {
476         struct sci_regulator_desc *desc =
477             (struct sci_regulator_desc *)rdev->desc;
478         const struct sci_regulator_regs *regs = desc->regs;
479         u32 mv;
480         int cal = 0;            /* uV */
481         int i, shft = __ffs(regs->vol_ctl_bits);
482
483         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
484                regs, desc->desc.name, regs->vol_ctl,
485                shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
486
487         if (!regs->vol_ctl)
488                 return -EINVAL;
489
490         BUG_ON(0 != __ffs(regs->vol_trm_bits));
491         BUG_ON(regs->vol_sel_cnt > 8);
492
493         i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
494
495         mv = regs->vol_sel[i];
496
497         if (regs->vol_trm) {
498                 /*check the reset relative bit of vol ctl */
499                 if (regs->vol_trm != regs->vol_ctl) {
500                         u32 vol_bits =
501                             (~ANA_REG_GET(regs->vol_ctl) &
502                              (regs->vol_ctl_bits << 4)) >> 4;
503
504                         if (i != vol_bits) {
505                                 return -EFAULT;
506                         }
507                 }
508
509                 cal = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits);
510                 if (0 == strcmp(desc->desc.name, "vddmem") )
511                         cal -= 0x10;
512
513                 cal *= dcdc_get_trimming_step(rdev, 0); /*uV */
514         }
515
516         debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv, cal / 1000);
517
518         return (mv * 1000 + cal) /*uV */;
519 }
520
521 /* standard boost ops*/
522 #define MAX_CURRENT_SINK        (500)   /*FIXME: max current sink */
523 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
524                                    int max_uA)
525 {
526         struct sci_regulator_desc *desc = __get_desc(rdev);
527         const struct sci_regulator_regs *regs = desc->regs;
528         int ma = min_uA / 1000;
529         int ret = -EACCES;
530         int i, shft = __ffs(regs->vol_ctl_bits);
531         int trim = (int)regs->vol_def / 1000;
532         int steps = (regs->vol_ctl_bits >> shft) + 1;
533
534         debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
535
536         if (!regs->vol_ctl)
537                 goto exit;
538
539         if (trim > 0) {
540                 trim <<= __ffs(regs->vol_trm_bits);
541         }
542
543         i = ma * steps / MAX_CURRENT_SINK;
544         if (i >= 0 && i < steps) {
545                 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
546                             regs->vol_ctl_bits | regs->vol_trm_bits);
547
548                 ret = 0;
549         }
550
551         WARN(0 != ret,
552              "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
553
554 exit:
555         return ret;
556 }
557
558 static int boost_get_current_limit(struct regulator_dev *rdev)
559 {
560         struct sci_regulator_desc *desc = __get_desc(rdev);
561         const struct sci_regulator_regs *regs = desc->regs;
562         u32 cur;
563         int i, shft = __ffs(regs->vol_ctl_bits);
564         int steps = (regs->vol_ctl_bits >> shft) + 1;
565
566         debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
567                regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
568
569         if (!regs->vol_ctl)
570                 return -EACCES;
571
572         i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
573         cur = i * MAX_CURRENT_SINK / steps;
574         debug2("regu %p (%s), current %d\n", regs, desc->desc.name, cur);
575         return cur * 1000;
576 }
577
578 static int adc_sample_bit = 1;  /*12bits mode */
579 static short adc_data[3][2]
580 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
581     = {
582         {4200, 3320},           /* same as nv adc_t */
583         {3600, 2844},
584         {400, 316},             /* 0.4@VBAT, Reserved IdealC Value */
585 }
586 #endif
587 ;
588
589 static int __is_valid_adc_cal(void)
590 {
591         return 0 != adc_data[0][0];
592 }
593
594 static int __init __adc_cal_setup(char *str)
595 {
596         u32 *p = (u32 *) adc_data;
597         *p = simple_strtoul(str, &str, 0);
598         if (*p++ && *++str) {
599                 *p = simple_strtoul(str, &str, 0);
600                 if (*p) {
601                         debug("%d : %d -- %d : %d\n",
602                               (int)adc_data[0][0], (int)adc_data[0][1],
603                               (int)adc_data[1][0], (int)adc_data[1][1]);
604                         if (adc_data[0][1] < BIT(10)
605                             && adc_data[1][1] < BIT(10))
606                                 adc_sample_bit = 0;     /*10bits mode */
607                 }
608         }
609         return 0;
610 }
611 early_param("adc_cal", __adc_cal_setup);
612
613 static int __init __adc_cal_fuse_setup(void)
614 {
615         if (!__is_valid_adc_cal() &&
616             sci_efuse_calibration_get((u32 *) adc_data)) {
617                 debug("%d : %d -- %d : %d\n",
618                       (int)adc_data[0][0], (int)adc_data[0][1],
619                       (int)adc_data[1][0], (int)adc_data[1][1]);
620         }
621
622         return 0;
623 }
624
625 static int __adc2vbat(int adc_res)
626 {
627         int t = adc_data[0][0] - adc_data[1][0];
628         t *= (adc_res - adc_data[0][1]);
629         t /= (adc_data[0][1] - adc_data[1][1]);
630         t += adc_data[0][0];
631         return t;
632 }
633
634 #define MEASURE_TIMES   (15)
635
636 static void __dump_adc_result(u32 adc_val[])
637 {
638 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
639         int i;
640         for (i = 0; i < MEASURE_TIMES; i++) {
641                 printk("%d ", adc_val[i]);
642         }
643         printk("\n");
644 #endif
645 }
646
647 static int cmp_val(const void *a, const void *b)
648 {
649         return *(int *)a - *(int *)b;
650 }
651
652 /**
653  * __adc_voltage - get regulator output voltage through auxadc
654  * @regulator: regulator source
655  *
656  * This returns the current regulator voltage in mV.
657  *
658  * NOTE: If the regulator is disabled it will return the voltage value. This
659  * function should not be used to determine regulator state.
660  */
661 static int regu_adc_voltage(struct regulator_dev *rdev)
662 {
663         struct sci_regulator_desc *desc = __get_desc(rdev);
664         const struct sci_regulator_regs *regs = desc->regs;
665
666         int ret, adc_chan = regs->cal_ctl_bits >> 16;
667         u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
668         u32 adc_res, adc_val[MEASURE_TIMES];
669         u32 chan_numerators = 1, chan_denominators = 1;
670         u32 bat_numerators, bat_denominators;
671
672         struct adc_sample_data data = {
673                 .channel_id = adc_chan,
674                 .channel_type = 0,      /*sw */
675                 .hw_channel_delay = 0,  /*reserved */
676                 .scale = 1,     /*big scale */
677                 .pbuf = &adc_val[0],
678                 .sample_num = MEASURE_TIMES,
679                 .sample_bits = adc_sample_bit,
680                 .sample_speed = 0,      /*quick mode */
681                 .signal_mode = 0,       /*resistance path */
682         };
683
684         if (!__is_valid_adc_cal())
685                 return -EACCES;
686
687         if (!regs->cal_ctl)
688                 return -EINVAL;
689
690         /* enable ldo cal before adc sampling and ldo calibration */
691         if (0 == regs->typ) {
692                 mutex_lock(&adc_chan_mutex);
693                 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
694                 debug0("%s adc channel %d : %04x\n",
695                        desc->desc.name, data.channel_id, ldo_cal_sel);
696         }
697
698         ret = sci_adc_get_values(&data);
699         BUG_ON(0 != ret);
700
701         /* close ldo cal and release multiplexed aux adc channel */
702         if (0 == regs->typ) {
703                 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
704                 mutex_unlock(&adc_chan_mutex);
705         }
706
707         __dump_adc_result(adc_val);
708         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
709         /*__dump_adc_result(adc_val);*/
710
711         sci_adc_get_vol_ratio(data.channel_id, data.scale,
712                               &chan_numerators, &chan_denominators);
713
714         sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators,
715                               &bat_denominators);
716
717         adc_res = adc_val[MEASURE_TIMES / 2];
718         debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
719               desc->desc.name, data.channel_id, ldo_cal_sel,
720               chan_numerators, chan_denominators, adc_res);
721
722         if (adc_res == 0)
723                 return -EAGAIN;
724         else
725                 return __adc2vbat(adc_res)
726                     * (bat_numerators * chan_denominators)
727                     / (bat_denominators * chan_numerators);
728 }
729
730 /**
731  * regulator_strongly_disable - strongly disable regulator output
732  * @regulator: regulator source
733  *
734  * Strongly try disable the regulator output voltage or current.
735  * NOTE: this *will* disable the regulator output even if other consumer
736  * devices have it enabled. This should be used for situations when device
737  * had unbalanced with calls to regulator_enable().
738  * *Not* recommended to call this function before try to balance the use_count.
739  */
740 int regulator_strongly_disable(struct regulator *regulator)
741 {
742         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
743         int ret = 0;
744
745         if (rdev)
746                 while (rdev->use_count--)
747                         regulator_disable(regulator);
748
749         return ret;
750 }
751
752 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
753
754 static struct regulator_ops ldo_ops = {
755         .enable = ldo_turn_on,
756         .disable = ldo_turn_off,
757         .is_enabled = ldo_is_on,
758         .set_voltage = ldo_set_voltage,
759         .get_voltage = ldo_get_voltage,
760         .set_mode = ldo_set_mode,
761 /*      .enable_time = ldo_enable_time, */
762 };
763
764 static struct regulator_ops dcdc_ops = {
765         .enable = ldo_turn_on,
766         .disable = ldo_turn_off,
767         .is_enabled = ldo_is_on,
768         .set_voltage = dcdc_set_voltage,
769         .get_voltage = dcdc_get_voltage,
770 };
771
772 static struct regulator_ops boost_ops = {
773         .enable = ldo_turn_on,
774         .disable = ldo_turn_off,
775         .is_enabled = ldo_is_on,
776         .set_current_limit = boost_set_current_limit,
777         .get_current_limit = boost_get_current_limit,
778         .set_mode = ldo_set_mode,
779 };
780
781 /*
782  * Consider the following machine :-
783  *
784  *   Regulator-1 -+-> [Consumer A @ 1.8V]
785  *                |
786  *                +-> [Consumer B @ 1.8V]
787  *
788  *   Regulator-2 ---> [Consumer C @ 3.3V]
789  *
790  * The drivers for consumers A & B must be mapped to the correct regulator in
791  * order to control their power supply. This mapping can be achieved in board/machine
792  * initialisation code by creating a struct regulator_consumer_supply for each regulator.
793  * Alternatively, we built a regulator supply-consumers map, the format is as follow:
794  *
795  *      supply source-1, consumer A, consumer B, ..., NULL
796  *      supply source-2, consumer C, ..., NULL
797  *      ...
798  *      NULL
799  *
800  */
801 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
802                                                         const char *supply_name,
803                                                         int *num)
804 {
805         char **map = (char **)dev_get_platdata(dev);
806         int i, n;
807         struct regulator_consumer_supply *consumer_supplies = NULL;
808
809         if (!supply_name || !(map && map[0]))
810                 return NULL;
811
812         for (i = 0; map[i] || map[i + 1]; i++) {
813                 if (map[i] && 0 == strcmp(map[i], supply_name))
814                         break;
815         }
816
817         /* i++; *//* Do not skip supply name */
818
819         for (n = 0; map[i + n]; n++) ;
820
821         if (n) {
822                 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
823                 consumer_supplies =
824                     kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
825                 BUG_ON(!consumer_supplies);
826                 for (n = 0; map[i]; i++, n++) {
827                         consumer_supplies[n].supply = map[i];
828                 }
829                 if (num)
830                         *num = n;
831         }
832         return consumer_supplies;
833 }
834
835 #if defined(CONFIG_DEBUG_FS)
836 static struct dentry *debugfs_root = NULL;
837
838 static u32 ana_addr = 0;
839 static int debugfs_ana_addr_get(void *data, u64 * val)
840 {
841         if (ana_addr < PAGE_SIZE) {
842                 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
843         } else {
844                 void *addr = ioremap(ana_addr, PAGE_SIZE);
845                 *val = __raw_readl(addr);
846                 iounmap(addr);
847         }
848         return 0;
849 }
850
851 static int debugfs_ana_addr_set(void *data, u64 val)
852 {
853         if (ana_addr < PAGE_SIZE) {
854                 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val,
855                             -1);
856         } else {
857                 void *addr = ioremap(ana_addr, PAGE_SIZE);
858                 __raw_writel(val, addr);
859                 iounmap(addr);
860         }
861         return 0;
862 }
863
864 static int adc_chan = 5 /*VBAT*/;
865 static int debugfs_adc_chan_get(void *pdata, u64 * val)
866 {
867         int i, ret;
868         u32 adc_res, adc_val[MEASURE_TIMES];
869         struct adc_sample_data data = {
870                 .channel_id = adc_chan,
871                 .channel_type = 0,      /*sw */
872                 .hw_channel_delay = 0,  /*reserved */
873                 .scale = 1,     /*big scale */
874                 .pbuf = &adc_val[0],
875                 .sample_num = MEASURE_TIMES,
876                 .sample_bits = adc_sample_bit,
877                 .sample_speed = 0,      /*quick mode */
878                 .signal_mode = 0,       /*resistance path */
879         };
880
881         ret = sci_adc_get_values(&data);
882         BUG_ON(0 != ret);
883
884         for (i = 0; i < MEASURE_TIMES; i++) {
885                 printk("%d ", adc_val[i]);
886         }
887         printk("\n");
888
889         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
890         adc_res = adc_val[MEASURE_TIMES / 2];
891         pr_info("adc chan %d, result value %d, vbat %d\n",
892                 data.channel_id, adc_res, __adc2vbat(adc_res));
893         *val = adc_res;
894         return 0;
895 }
896
897 static int debugfs_adc_chan_set(void *data, u64 val)
898 {
899         adc_chan = val;
900         return 0;
901 }
902
903 static int debugfs_enable_get(void *data, u64 * val)
904 {
905         struct regulator_dev *rdev = data;
906         if (rdev && rdev->desc->ops->is_enabled)
907                 *val = rdev->desc->ops->is_enabled(rdev);
908         else
909                 *val = -1;
910         return 0;
911 }
912
913 static int debugfs_enable_set(void *data, u64 val)
914 {
915         struct regulator_dev *rdev = data;
916         if (rdev && rdev->desc->ops->enable)
917                 (val) ? rdev->desc->ops->enable(rdev)
918                     : rdev->desc->ops->disable(rdev);
919         return 0;
920 }
921
922 static int debugfs_voltage_get(void *data, u64 * val)
923 {
924         struct regulator_dev *rdev = data;
925         if (rdev)
926                 *val = regu_adc_voltage(rdev);
927         else
928                 *val = -1;
929         return 0;
930 }
931
932 static int debugfs_voltage_set(void *data, u64 val)
933 {
934         struct regulator_dev *rdev = data;
935         if (rdev && rdev->desc->ops->set_voltage) {
936                 u32 min_uV = (u32)val * 1000;
937                 min_uV += rdev->constraints->uV_offset;
938                 rdev->desc->ops->set_voltage(rdev, min_uV, min_uV, 0);
939         }
940         return 0;
941 }
942
943 static int debugfs_boost_get(void *data, u64 * val)
944 {
945         struct regulator_dev *rdev = data;
946         if (rdev && rdev->desc->ops->get_current_limit)
947                 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
948         else
949                 *val = -1;
950         return 0;
951 }
952
953 static int debugfs_boost_set(void *data, u64 val)
954 {
955         struct regulator_dev *rdev = data;
956         if (rdev && rdev->desc->ops->set_current_limit)
957                 rdev->desc->ops->set_current_limit(rdev, val * 1000,
958                                                    val * 1000);
959         return 0;
960 }
961
962 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
963                         debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
964 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
965                         debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
966 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
967                         debugfs_enable_get, debugfs_enable_set, "%llu\n");
968 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
969                         debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
970 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
971                         debugfs_boost_get, debugfs_boost_set, "%llu\n");
972
973 static void rdev_init_debugfs(struct regulator_dev *rdev)
974 {
975         struct sci_regulator_desc *desc = __get_desc(rdev);
976         desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
977         if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
978                 pr_warn("Failed to create debugfs directory\n");
979                 rdev->debugfs = NULL;
980                 return;
981         }
982
983         debugfs_create_file("enable", S_IRUGO | S_IWUSR,
984                             desc->debugfs, rdev, &fops_enable);
985
986         if (desc->desc.type == REGULATOR_CURRENT)
987                 debugfs_create_file("current", S_IRUGO | S_IWUSR,
988                                     desc->debugfs, rdev, &fops_boost);
989         else
990                 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
991                                     desc->debugfs, rdev, &fops_ldo);
992 }
993 #else
994 static void rdev_init_debugfs(struct regulator_dev *rdev)
995 {
996 }
997 #endif
998
999 #ifdef CONFIG_OF
1000 #include <linux/of.h>
1001 #define reg_info(format, arg...) pr_info("reg: " "@@@%s: " format, __func__, ## arg)
1002 #endif
1003
1004 static inline int __strcmp(const char *cs, const char *ct)
1005 {
1006         if (!cs || !ct) return -1;
1007         return strcmp(cs, ct);
1008 }
1009
1010 void *sci_regulator_register(struct platform_device *pdev,
1011                                        struct sci_regulator_desc *desc)
1012 {
1013         static atomic_t idx = ATOMIC_INIT(1);   /* 0: dummy */
1014         struct regulator_dev *rdev;
1015         struct regulator_ops *__regs_ops[] = {
1016                 &ldo_ops, 0, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops,
1017                 0,
1018         };
1019         struct regulator_consumer_supply consumer_supplies_default[] = {
1020                 [0] = {
1021                        .supply = desc->desc.name,
1022                        }
1023         };
1024 #ifndef CONFIG_OF
1025         struct regulator_init_data init_data = {
1026                 .supply_regulator = 0,
1027                 .constraints = {
1028                                 .min_uV = 0,
1029                                 .max_uV = 4200 * 1000,
1030                                 .valid_modes_mask =
1031                                 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
1032                                 .valid_ops_mask =
1033                                 REGULATOR_CHANGE_STATUS |
1034                                 REGULATOR_CHANGE_VOLTAGE |
1035                                 REGULATOR_CHANGE_MODE,
1036                                 },
1037                 .num_consumer_supplies = 1,
1038                 .consumer_supplies = consumer_supplies_default,
1039                 .regulator_init = 0,
1040                 .driver_data = 0,
1041         };
1042
1043         struct regulator_config config = { };
1044
1045         if (sci_ana_chip_ver_is_compatible(SC2711AA)) {
1046                 if (0 == strcmp(desc->desc.name, "vdd18") ||
1047                         0 == strcmp(desc->desc.name, "vddemmcio") ||
1048                         0 == strcmp(desc->desc.name, "vddcamd") ||
1049                         0 == strcmp(desc->desc.name, "vddcamio")) {
1050
1051                         desc->regs->vol_sel[0] = 1400;
1052                         debug("%s vol base %d\n", desc->desc.name, desc->regs->vol_sel[0]);
1053                 }
1054         }
1055
1056 #else
1057         struct regulator_config config = { };
1058         struct regulator_init_data *init_data;
1059         struct device_node *dev_np;
1060         struct device_node *node_np;
1061         dev_np = pdev->dev.of_node;
1062         node_np = of_get_child_by_name(dev_np, desc->desc.name);
1063         init_data = of_get_regulator_init_data(&pdev->dev, node_np);
1064         if(!init_data || 0 != __strcmp(init_data->constraints.name, desc->desc.name)){
1065                 dev_err(&pdev->dev, "out of memory or %s not found\n", desc->desc.name);
1066                 return NULL;
1067         }
1068         reg_info("[%d]%s range %d - %d\n", idx.counter, init_data->constraints.name,
1069                 init_data->constraints.min_uV, init_data->constraints.max_uV);
1070
1071         init_data->supply_regulator = 0;
1072         init_data->constraints.min_uV = 0,
1073         init_data->constraints.max_uV = 4200 * 1000;
1074         init_data->constraints.valid_modes_mask = 
1075                 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1076         init_data->constraints.valid_ops_mask = REGULATOR_CHANGE_MODE |
1077                 REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE;
1078         init_data->num_consumer_supplies = 1;
1079         init_data->consumer_supplies = consumer_supplies_default;
1080 #endif
1081
1082         desc->desc.id = atomic_inc_return(&idx) - 1;
1083
1084         if (0 == strcmp(desc->desc.name, "vddrf0")) {
1085                 if (ana_status_bondopt4) { /* BONDOPT4 */
1086                         desc->regs->vol_def = 2800;
1087                 } else {
1088                         desc->regs->vol_def = 1800;
1089                 }
1090         }
1091
1092         BUG_ON(desc->regs->pd_set
1093                && desc->regs->pd_set == desc->regs->pd_rst
1094                && desc->regs->pd_set_bit == desc->regs->pd_rst_bit);
1095
1096         BUG_ON(desc->regs->typ >= ARRAY_SIZE(__regs_ops));
1097         if (!desc->desc.ops)
1098                 desc->desc.ops = __regs_ops[desc->regs->typ];
1099
1100 #ifndef CONFIG_OF
1101         init_data.consumer_supplies =
1102             set_supply_map(&pdev->dev, desc->desc.name,
1103                            &init_data.num_consumer_supplies);
1104
1105         if (!init_data.consumer_supplies)
1106                 init_data.consumer_supplies = consumer_supplies_default;
1107
1108         debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1109         config.dev = &pdev->dev;
1110         config.init_data = &init_data;
1111         config.driver_data = NULL;
1112         config.of_node = NULL;
1113         rdev = regulator_register(&desc->desc, &config);
1114         if (init_data.consumer_supplies != consumer_supplies_default)
1115                 kfree(init_data.consumer_supplies);
1116 #else
1117         init_data->consumer_supplies =
1118             set_supply_map(&pdev->dev, desc->desc.name,
1119                            &init_data->num_consumer_supplies);
1120
1121         if (!init_data->consumer_supplies)
1122                 init_data->consumer_supplies = consumer_supplies_default;
1123
1124         debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1125         config.dev = &pdev->dev;
1126         config.init_data = init_data;
1127         config.driver_data = NULL;
1128         config.of_node = node_np;
1129         rdev = regulator_register(&desc->desc, &config);
1130         if (init_data->consumer_supplies != consumer_supplies_default)
1131                 kfree(init_data->consumer_supplies);
1132 #endif
1133
1134         if (!IS_ERR(rdev)) {
1135                 rdev->reg_data = rdev;
1136                 desc->data.rdev = rdev;
1137                 __init_trimming(rdev);
1138                 rdev_init_debugfs(rdev);
1139         }
1140         return rdev;
1141 }
1142
1143 /**
1144  * IMPORTANT!!!
1145  * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1146  * so i autogen all regulators non-variable description in plat or mach directory,
1147  * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1148  * just like other regulator vendor drivers.
1149  */
1150 static int sci_regulator_probe(struct platform_device *pdev)
1151 {
1152 #ifdef CONFIG_DEBUG_FS
1153         debugfs_root =
1154                 debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1155         if (IS_ERR(debugfs_root) || !debugfs_root) {
1156                 WARN(!debugfs_root,
1157                          "%s: Failed to create debugfs directory\n", REGULATOR_ROOT_DIR);
1158                 debugfs_root = NULL;
1159         }
1160
1161         /* compatible with 8810 adc test */
1162         debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1163                            debugfs_root, (u32 *) & ana_addr);
1164         debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1165                                 debugfs_root, &ana_addr, &fops_ana_addr);
1166         debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1167                                 debugfs_root, &adc_chan, &fops_adc_chan);
1168         debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1169                            debugfs_root, (u64 *) & adc_data);
1170
1171         { /* vddarm/vddcore/vddmem common debugfs interface */
1172                 char str[NAME_MAX];
1173                 struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1174                 sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1175                 debugfs_create_symlink("dcdcarm", vol_root, str);
1176                 sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1177                 debugfs_create_symlink("dcdccore", vol_root, str);
1178                 sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1179                 debugfs_create_symlink("dcdcmem", vol_root, str);
1180         }
1181 #endif
1182
1183         ana_chip_id = (sci_get_ana_chip_id() | sci_get_ana_chip_ver());
1184         ana_status_bondopt4 = (ANA_REG_GET(ANA_REG_GLB_LDO_V_CTRL9) >> 15) & 0x1;
1185
1186         pr_info("sc271x ana chip id: (0x%08x), bond opt (0x%08x)\n", ana_chip_id, ana_status_bondopt4);
1187
1188 #include CONFIG_REGULATOR_SPRD_MAP
1189
1190         return 0;
1191 }
1192
1193 #ifdef CONFIG_OF
1194 static struct of_device_id sprd_regulator_of_match[] = {
1195         { .compatible = "sprd,sc2711-regulator", },
1196         { }
1197 };
1198 #endif
1199
1200 static struct platform_driver sci_regulator_driver = {
1201         .driver = {
1202                    .name = "sc2711-regulator",
1203                    .owner = THIS_MODULE,
1204                    .of_match_table = of_match_ptr(sprd_regulator_of_match),
1205                    },
1206         .probe = sci_regulator_probe,
1207 };
1208
1209 static int __init regu_driver_init(void)
1210 {
1211         __adc_cal_fuse_setup();
1212
1213         return platform_driver_register(&sci_regulator_driver);
1214 }
1215
1216 int __init sci_regulator_init(void)
1217 {
1218 #ifndef CONFIG_OF
1219         static struct platform_device regulator_device = {
1220                 .name = "sc2711-regulator",
1221                 .id = -1,
1222         };
1223
1224         sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
1225                           BITS_PWR_WR_PROT_VALUE(0x6e7f));
1226         WARN_ON(!
1227                 (sci_adi_read(ANA_REG_GLB_PWR_WR_PROT_VALUE) &
1228                  BIT_PWR_WR_PROT));
1229         sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
1230
1231         __adc_cal_fuse_setup();
1232         return platform_device_register(&regulator_device);
1233 #else
1234         return of_platform_populate(of_find_node_by_path("/sprd-regulators"),
1235                         sprd_regulator_of_match, NULL, NULL);
1236 #endif
1237 }
1238
1239 subsys_initcall(regu_driver_init);
1240
1241 MODULE_LICENSE("GPL v2");
1242 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1243 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");
1244 MODULE_VERSION("0.2");