usb: gadget: fix: Call usb_gadget_connect() for dummy_udc
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / regulator / sc2723-regulator_dt.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 #ifdef CONFIG_OF
34 #include <linux/of.h>
35 #include <linux/of_device.h>
36 #include <linux/of_address.h>
37 #include <linux/regulator/of_regulator.h>
38 #endif
39 #include <linux/regulator/consumer.h>
40 #include <linux/regulator/driver.h>
41 #include <linux/regulator/machine.h>
42
43 #include <soc/sprd/sci.h>
44 #include <soc/sprd/sci_glb_regs.h>
45 #include <soc/sprd/adi.h>
46 #include <soc/sprd/adc.h>
47
48 #define REGULATOR_ROOT_DIR      "sprd-regulator"
49
50 #undef debug
51 #define debug(format, arg...) pr_debug("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 #define CONFIG_REGULATOR_ADC_DEBUG
71 struct sci_regulator_regs {
72         int typ; /* BIT4: default on/off(0: off, 1: on); BIT0~BIT3: dcdc/ldo type(0: ldo; 2: dcdc) */
73         int hide_offset;
74         unsigned long pd_set;
75         u32 pd_set_bit;
76         unsigned long pwr_sel;  /* otp pwr select reg */
77         u32 pwr_sel_bit;        /* 0: otp enable(from emmc), 1: otp disable(from sw register) */
78         unsigned long vol_trm;
79         u32 vol_trm_bits;
80         unsigned long cal_ctl;
81         u32 cal_ctl_bits, cal_chan;
82         u32 min_uV, max_uV, step_uV;
83         int otp_delta;
84         u32 vol_def;
85         unsigned long vol_ctl;
86         u32 vol_ctl_bits;
87         u32 vol_sel_cnt;
88         u32 *vol_sel;
89 };
90
91 struct sci_regulator_data {
92         struct regulator_dev *rdev;
93 };
94
95 struct sci_regulator_desc {
96         struct regulator_desc desc;
97         struct regulator_init_data *init_data;
98         struct sci_regulator_regs regs;
99         struct sci_regulator_data data; /* FIXME: dynamic */
100 #if defined(CONFIG_DEBUG_FS)
101         struct dentry *debugfs;
102 #endif
103 };
104
105 enum {
106         VDD_TYP_LDO = 0,
107         VDD_TYP_LDO_D = 1,
108         VDD_TYP_DCDC = 2,
109         VDD_TYP_LPREF = 3,
110         VDD_TYP_BOOST = 4,
111 };
112
113 #define REGU_VERIFY_DLY (1000)  /*ms */
114
115 static u32 dcdc_vol_select[] = { 1100000, 700000, 800000, 900000, 1000000, 650000, 1200000, 1300000 };  /* uV */
116
117 static struct sci_regulator_desc *sci_desc_list = NULL;
118
119 static atomic_t idx = ATOMIC_INIT(1);   /* 0: dummy */
120
121 static u32 ana_chip_id;
122 static u16 ana_mixed_ctl, otp_pwr_sel;
123
124 static DEFINE_MUTEX(adc_chan_mutex);
125 static int regulator_get_trimming_step(struct regulator_dev *rdev, int to_vol);
126 static int __is_valid_adc_cal(void);
127
128 #ifdef CONFIG_OTP_SPRD_EFUSE
129 extern int sci_efuse_get_cal(unsigned int * pdata, int num);
130 #else
131 int sci_efuse_get_cal(unsigned int * pdata, int num){return 0;}
132 #endif
133
134 static int get_regu_offset(struct regulator_dev *rdev, int des_uV);
135
136 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
137 {
138         return (struct sci_regulator_desc *)rdev->desc;
139 }
140
141 /* standard ldo ops*/
142 static int ldo_turn_on(struct regulator_dev *rdev)
143 {
144         struct sci_regulator_desc *desc = __get_desc(rdev);
145         struct sci_regulator_regs *regs = &desc->regs;
146
147         debug0("regu 0x%p (%s), power down 0x%08x[%d]\n", regs,
148                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit));
149
150         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
151                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
152                                   BITS_PWR_WR_PROT_VALUE(0x6e7f));
153
154         if (regs->pd_set)
155                 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
156
157         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
158                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
159
160         debug("regu 0x%p (%s), turn on\n", regs, desc->desc.name);
161         return 0;
162 }
163
164 static int ldo_turn_off(struct regulator_dev *rdev)
165 {
166         struct sci_regulator_desc *desc = __get_desc(rdev);
167         struct sci_regulator_regs *regs = &desc->regs;
168
169         debug0("regu 0x%p (%s), power down 0x%08x[%d]\n", regs,
170                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit));
171
172 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
173         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
174                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
175                                   BITS_PWR_WR_PROT_VALUE(0x6e7f));
176
177         if (regs->pd_set)
178                 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
179
180         if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
181                 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
182
183 #endif
184
185         debug("regu 0x%p (%s), turn off\n", regs, desc->desc.name);
186         return 0;
187 }
188
189 static int ldo_is_on(struct regulator_dev *rdev)
190 {
191         int ret = -EINVAL;
192         struct sci_regulator_desc *desc = __get_desc(rdev);
193         struct sci_regulator_regs *regs = &desc->regs;
194
195         debug0("regu 0x%p (%s), power down 0x%08x[%d]\n", regs,
196                desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit));
197
198         if (regs->pd_set) {
199                 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
200         }
201
202         debug2("regu 0x%p (%s) turn on, return %d\n", regs, desc->desc.name,
203                ret);
204         return ret;
205 }
206
207 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
208 {
209 #if 0
210         struct sci_regulator_desc *desc = __get_desc(rdev);
211         struct sci_regulator_regs *regs = &desc->regs;
212
213         debug("regu 0x%p (%s), slp 0x%08x[%d] mode %x\n", regs, desc->desc.name,
214               regs->slp_ctl, regs->slp_ctl_bit, mode);
215
216         if (!regs->slp_ctl)
217                 return -EINVAL;
218
219         if (mode == REGULATOR_MODE_STANDBY) {   /* disable auto slp */
220                 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
221         } else {
222                 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
223         }
224 #endif
225
226         return 0;
227 }
228
229 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
230                            int max_uV, unsigned *selector)
231 {
232         struct sci_regulator_desc *desc = __get_desc(rdev);
233         struct sci_regulator_regs *regs = &desc->regs;
234         //int mv = min_uV / 1000;
235         int ret = -EINVAL;
236
237         debug("regu 0x%p (%s) set voltage, %d(uV) %d(uV)\n", regs,
238               desc->desc.name, min_uV, max_uV);
239
240         min_uV += get_regu_offset(rdev, min_uV);
241
242         if(min_uV < regs->min_uV){
243             debug("warning: regulator (%s) target voltage %d lower than min_uV,modify target to min_uV %d(uV)\n",desc->desc.name, min_uV,regs->min_uV);
244             min_uV = regs->min_uV;
245         }
246
247         if(min_uV > regs->max_uV){
248             debug("warning: regulator (%s) target voltage %d higher than max_uV,modify target to max_uV %d(uV)\n",desc->desc.name, min_uV,regs->max_uV);
249             min_uV = regs->max_uV;
250         }
251
252         if (regs->vol_trm) {
253                 int shft = __ffs(regs->vol_trm_bits);
254                 u32 trim =
255                     DIV_ROUND_UP((int)(min_uV - regs->min_uV), regs->step_uV);
256
257                 ret = trim > (regs->vol_trm_bits >> shft);
258                 WARN(0 != ret,
259                      "warning: regulator (%s) not support %d(uV)\n",
260                      desc->desc.name, min_uV);
261
262                 if (0 == ret) {
263                         ANA_REG_SET(regs->vol_trm,
264                                     trim << shft, regs->vol_trm_bits);
265                 }
266         }
267
268         return ret;
269 }
270
271 static int ldo_get_voltage(struct regulator_dev *rdev)
272 {
273         struct sci_regulator_desc *desc = __get_desc(rdev);
274         struct sci_regulator_regs *regs = &desc->regs;
275         u32 vol;
276
277         debug0("regu 0x%p (%s), vol trm 0x%08x, mask 0x%08x\n",
278                regs, desc->desc.name, regs->vol_trm, regs->vol_trm_bits);
279
280         if (regs->vol_trm) {
281                 int shft = __ffs(regs->vol_trm_bits);
282                 u32 trim =
283                     (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft;
284                 vol = regs->min_uV + trim * regs->step_uV;
285                 debug2("regu 0x%p (%s), voltage %d\n", regs, desc->desc.name,
286                        vol);
287                 return vol;
288         }
289
290         return -EFAULT;
291 }
292
293 #ifdef CONFIG_OTP_SPRD_ADIE_EFUSE
294 extern u32 __adie_efuse_read(int blk_index);
295 extern int sci_otp_get_offset(const char *name);
296 #else
297 u32 __adie_efuse_read(int blk_index) {return 0;}
298 int sci_otp_get_offset(const char *name){return 0;}
299 #endif
300
301 static int get_regu_offset(struct regulator_dev *rdev, int des_uV)
302 {
303         struct sci_regulator_desc *desc = __get_desc(rdev);
304         struct sci_regulator_regs *regs = &desc->regs;
305         int delta = 0;
306
307         if ((desc->regs.typ & BIT(4)))
308                 return 0;
309
310         delta = (des_uV/1000) * regs->otp_delta / ((int)regs->vol_def/1000);
311         delta *=(int)regs->step_uV;
312
313         return delta;
314 }
315
316 static int set_regu_offset(struct regulator_dev *rdev)
317 {
318         struct sci_regulator_desc *desc = __get_desc(rdev);
319         struct sci_regulator_regs *regs = &desc->regs;
320         const char *regu_name = desc->desc.name;
321         int efuse_data = 0;
322
323         if (NULL == regu_name)
324                 return -1;
325         rdev->constraints->uV_offset = 0;
326         efuse_data = sci_otp_get_offset(regu_name);
327         regs->otp_delta = efuse_data;
328         debug("%s otp delta: %d, voltage offset: %d(uV)\n", desc->desc.name,
329               efuse_data, rdev->constraints->uV_offset);
330
331         /* switch sw register control from otp emmc only for vddmem/vdd25 */
332         if ((0 == strcmp(regu_name, "vddmem"))
333             || (0 == strcmp(regu_name, "vdd25"))) {
334                 if (regs->pwr_sel) {
335                         int shft = __ffs(regs->vol_trm_bits);
336                         u32 trim = 0;
337
338                         if (regs->vol_trm) {
339                                 trim =
340                                     (ANA_REG_GET(regs->vol_trm) &
341                                      regs->vol_trm_bits) >> shft;
342                                 trim += efuse_data;
343                                 ANA_REG_SET(regs->vol_trm,
344                                             trim << shft, regs->vol_trm_bits);
345                         }
346
347                         /* set pwr sel bit for sw control */
348                         ANA_REG_OR(regs->pwr_sel, regs->pwr_sel_bit);
349                 }
350         }
351
352         return 0;
353 }
354
355 /* FIXME: get dcdc cal offset config from uboot */
356 typedef struct {
357         u16 ideal_vol;
358         const char name[14];
359 } vol_para_t;
360
361 static void __iomem *spl_start_base = NULL;
362
363 #if defined(CONFIG_ARCH_SCX35L)
364 #define SPRD_SPL_PHYS   ( 0x50003000 )
365 #else
366 #define SPRD_SPL_PHYS   ( 0x50005000 )
367 #endif
368 #define SPRD_SPL_SIZE   ( SZ_32K )
369 #define PP_VOL_PARA             ( SPRD_SPL_PHYS + 0xC20 )       /* assert in iram2 */
370 #define TO_IRAM2(_p_)   ( (unsigned long)spl_start_base + (unsigned long)(_p_) - SPRD_SPL_PHYS )
371 #define IN_IRAM2(_p_)   ( (unsigned long)(_p_) >= SPRD_SPL_PHYS && (unsigned long)(_p_) < SPRD_SPL_PHYS + SPRD_SPL_SIZE )
372
373 int regulator_default_get(const char con_id[])
374 {
375         int i = 0, res = 0;
376         vol_para_t *pvol_para =
377             (vol_para_t *) __raw_readl((void *)TO_IRAM2(PP_VOL_PARA));
378         debug0("pvol_para phy_addr 0x%08x\n", pvol_para);
379
380         if (!(IN_IRAM2(pvol_para)))
381                 return 0;
382
383         pvol_para = (vol_para_t *) TO_IRAM2(pvol_para);
384         debug0("pvol_para vir_addr 0x%08x\n", pvol_para);
385         if (strcmp((pvol_para)[0].name, "volpara_begin")
386             || (0xfaed != (pvol_para)[0].ideal_vol))
387                 return 0;
388
389         while (0 != strcmp((pvol_para)[i++].name, "volpara_end")) {
390                 if (0 == strcmp((pvol_para)[i].name, con_id)) {
391                         debug("%s name %s, ideal_vol %d\n", __func__,
392                               (pvol_para)[i].name, (pvol_para)[i].ideal_vol);
393                         return res = (pvol_para)[i].ideal_vol;
394                 }
395         }
396         return res;
397 }
398
399 static int __init_trimming(struct regulator_dev *rdev)
400 {
401         struct sci_regulator_desc *desc = __get_desc(rdev);
402         struct sci_regulator_regs *regs = &desc->regs;
403         int ctl_vol, to_vol;
404         uint otp_ana_flag = 0;
405
406         if (!regs->vol_trm)
407                 return -1;
408
409         if (!__is_valid_adc_cal())
410                 return -2;
411
412         otp_ana_flag = (u8) __adie_efuse_read(0);
413         debug("emmeory block(0) data %#x\n", otp_ana_flag);;
414         if (!(otp_ana_flag & BIT(7))) {
415                 set_regu_offset(rdev);
416         } else {
417
418                 to_vol = regulator_default_get(desc->desc.name);
419                 to_vol *= 1000; //uV
420                 if (!to_vol)
421                         to_vol = regs->vol_def;
422
423                 if (to_vol && rdev->desc->ops->get_voltage) {
424                         ctl_vol = rdev->desc->ops->get_voltage(rdev);
425                         rdev->constraints->uV_offset = ctl_vol - to_vol;        //uV
426                         debug("regu 0x%p (%s), uV offset %d\n", regs,
427                               desc->desc.name, rdev->constraints->uV_offset);
428                 }
429         }
430
431         return 0;
432 }
433
434 static int regulator_get_trimming_step(struct regulator_dev *rdev, int to_vol)
435 {
436         struct sci_regulator_desc *desc = __get_desc(rdev);
437         struct sci_regulator_regs *regs = &desc->regs;
438
439         BUG_ON(!regs->step_uV);
440
441         return regs->step_uV;
442 }
443
444 static int __match_dcdc_vol(struct sci_regulator_regs *regs, u32 vol)
445 {
446         int i, j = -1;
447         int ds, min_ds = 100 * 1000;    /* uV, the max range of small voltage */
448
449         for (i = 0; i < regs->vol_sel_cnt; i++) {
450                 ds = vol - regs->vol_sel[i];
451                 if (ds >= 0 && ds < min_ds) {
452                         min_ds = ds;
453                         j = i;
454                 }
455         }
456         return j;
457 }
458
459 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
460 {
461         int vol = rdev->desc->ops->get_voltage(rdev);
462         if (vol > old_vol) {
463                 /* FIXME: for dcdc, each step (50mV) takes 10us */
464                 int dly = (vol - old_vol) * 10 / (50 * 1000);
465                 WARN_ON(dly > 1000);
466                 udelay(dly);
467         }
468         return 0;
469 }
470
471 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
472                             int max_uV, unsigned *selector)
473 {
474         struct sci_regulator_desc *desc = __get_desc(rdev);
475         struct sci_regulator_regs *regs = &desc->regs;
476         int i = 0;
477         //int mV = min_uV / 1000;
478         int old_vol = rdev->desc->ops->get_voltage(rdev);
479
480         debug0("regu 0x%p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
481
482         if (regs->vol_ctl) {
483                 /* found the closely vol ctrl bits */
484                 i = __match_dcdc_vol(regs, min_uV);
485                 if (i < 0)
486                         return WARN(-EINVAL,
487                                     "not found %s closely ctrl bits for %d(uV)\n",
488                                     desc->desc.name, min_uV);
489         }
490 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
491         /* dcdc calibration control bits (default 00000),
492          * small adjust voltage: 100/32mv ~= 3.125mv
493          */
494         {
495                 int shft_trm = __ffs(regs->vol_trm_bits);
496                 int shft_ctl = 0;
497                 int step = 0;
498                 int j = 0;
499
500                 if (regs->vol_ctl) {
501                         shft_ctl = __ffs(regs->vol_ctl_bits);
502                         step = regulator_get_trimming_step(rdev, 0);
503
504                         j = DIV_ROUND_UP((int)(min_uV - (int)regs->vol_sel[i]),
505                                          step);
506
507                         debug("regu 0x%p (%s) %d = %d %+duV(trim %#x)\n", regs,
508                               desc->desc.name, min_uV, regs->vol_sel[i],
509                               min_uV - regs->vol_sel[i], j);
510                 } else {
511                         j = DIV_ROUND_UP((int)(min_uV - regs->min_uV),
512                                          regs->step_uV);
513
514                         debug("regu 0x%p (%s) %d = %d %+duV(trim %#x)\n", regs,
515                               desc->desc.name, min_uV, regs->min_uV,
516                               min_uV - regs->min_uV, j);
517                 }
518
519                 BUG_ON(j > (regs->vol_trm_bits >> shft_trm));
520
521                 if (regs->vol_trm == regs->vol_ctl) {
522                         ANA_REG_SET(regs->vol_ctl,
523                                     (j << shft_trm) | (i << shft_ctl),
524                                     regs->vol_trm_bits | regs->vol_ctl_bits);
525                 } else {
526                         if (regs->vol_trm) {    /* small adjust first */
527                                 ANA_REG_SET(regs->vol_trm, j << shft_trm,
528                                             regs->vol_trm_bits);
529                         }
530
531                         if (regs->vol_ctl) {
532                                 ANA_REG_SET(regs->vol_ctl, i << shft_ctl,
533                                             regs->vol_ctl_bits);
534                         }
535                 }
536         }
537
538         __dcdc_enable_time(rdev, old_vol);
539 #endif
540
541         return 0;
542 }
543
544 static int dcdc_set_voltage_step(struct regulator_dev *rdev, int min_uV,
545                                  int max_uV, unsigned *selector)
546 {
547         int to_vol = min_uV;
548         int step = 25 * 1000;   /*uV */
549         int vol = rdev->desc->ops->get_voltage(rdev);
550         struct sci_regulator_desc *desc = __get_desc(rdev);
551         struct sci_regulator_regs *regs = &desc->regs;
552
553         to_vol += get_regu_offset(rdev, min_uV);
554         to_vol -= regs->hide_offset*1000;
555
556         if(to_vol < regs->min_uV){
557             debug("warning: regulator (%s) target voltage %d lower than min_uV,modify target to min_uV %d(uV)\n",desc->desc.name, to_vol,regs->min_uV);
558             to_vol = regs->min_uV;
559         }
560
561         if(to_vol > regs->max_uV){
562             debug("warning: regulator (%s) target voltage %d higher than max_uV,modify target to max_uV %d(uV)\n",desc->desc.name, to_vol,regs->max_uV);
563             to_vol = regs->max_uV;
564         }
565
566         if (vol < to_vol) {
567                 do {            /*FIXME: dcdc sw step up for eliminate overshoot (+65mV) */
568                         vol += step;
569                         if (vol > to_vol)
570                                 vol = to_vol;
571                         dcdc_set_voltage(rdev, vol, vol, selector);
572                 } while (vol < to_vol);
573         } else {
574                 do {
575                         vol -= step;
576                         if (vol < to_vol)
577                                 vol = to_vol;
578                         dcdc_set_voltage(rdev, vol, vol, selector);
579                 } while (vol > to_vol);
580         }
581         return 0;
582 }
583
584 static int dcdc_get_voltage(struct regulator_dev *rdev)
585 {
586         struct sci_regulator_desc *desc = __get_desc(rdev);
587         struct sci_regulator_regs *regs = &desc->regs;
588         u32 uV;
589         int i, cal = 0 /* uV */ ;
590
591         if (regs->vol_ctl) {
592                 int shft_ctl = __ffs(regs->vol_ctl_bits);
593                 int shft_trm = __ffs(regs->vol_trm_bits);
594
595                 debug0
596                     ("regu 0x%p (%s), vol ctl 0x%08x, shft %d, mask 0x%08x, sel %d\n",
597                      regs, desc->desc.name, regs->vol_ctl, shft_ctl,
598                      regs->vol_ctl_bits, regs->vol_sel_cnt);
599
600                 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >>
601                     shft_ctl;
602                 uV = regs->vol_sel[i];
603
604                 if (regs->vol_trm) {
605                         cal =
606                             (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >>
607                             shft_trm;
608                         cal *= regulator_get_trimming_step(rdev, 0);    /* uV */
609                 }
610
611         } else if (regs->vol_trm) {
612                 int shft_trm = __ffs(regs->vol_trm_bits);
613                 u32 trim =
614                     (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >>
615                     shft_trm;
616                 uV = regs->min_uV + trim * regs->step_uV;
617         }
618
619         uV += regs->hide_offset*1000;
620         debug2("%s get voltage, %d +%duv\n", desc->desc.name, uV, cal);
621
622         return (uV + cal) /*uV */ ;
623 }
624
625
626 /* standard boost ops*/
627 #define MAX_CURRENT_SINK        (500)   /*FIXME: max current sink */
628 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
629                                    int max_uA)
630 {
631         struct sci_regulator_desc *desc = __get_desc(rdev);
632         struct sci_regulator_regs *regs = &desc->regs;
633         int ma = min_uA / 1000;
634         int ret = -EACCES;
635         int i, shft = __ffs(regs->vol_ctl_bits);
636         int trim = (int)regs->vol_def / (1000 * 1000);
637         int steps = (regs->vol_ctl_bits >> shft) + 1;
638
639         debug("regu 0x%p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
640
641         if (!regs->vol_ctl)
642                 goto exit;
643
644         if (trim > 0) {
645                 trim <<= __ffs(regs->vol_trm_bits);
646         }
647
648         i = ma * steps / MAX_CURRENT_SINK;
649         if (i >= 0 && i < steps) {
650                 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
651                             regs->vol_ctl_bits | regs->vol_trm_bits);
652
653                 ret = 0;
654         }
655
656         WARN(0 != ret,
657              "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
658
659 exit:
660         return ret;
661 }
662
663 static int boost_get_current_limit(struct regulator_dev *rdev)
664 {
665         struct sci_regulator_desc *desc = __get_desc(rdev);
666         struct sci_regulator_regs *regs = &desc->regs;
667         u32 cur;
668         int i, shft = __ffs(regs->vol_ctl_bits);
669         int steps = (regs->vol_ctl_bits >> shft) + 1;
670
671         debug0("regu 0x%p (%s), vol ctl 0x%08x, shft %d, mask 0x%08x\n",
672                regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
673
674         if (!regs->vol_ctl)
675                 return -EACCES;
676
677         i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
678         cur = i * MAX_CURRENT_SINK / steps;
679         debug("regu 0x%p (%s) get current %d\n", regs, desc->desc.name, cur);
680         return cur * 1000;
681 }
682
683 static int adc_sample_bit = 1;  /*12bits mode */
684 static short adc_data[3][2]
685 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
686     = {
687         {4200, 3387},           /* same as nv adc_t */
688         {3600, 2905},
689         {400, 316},             /* 0.4@VBAT, Reserved IdealC Value */
690 }
691 #endif
692 ;
693
694 static int __is_valid_adc_cal(void)
695 {
696         return 0 != adc_data[0][0];
697 }
698
699 static int __init __adc_cal_setup(char *str)
700 {
701         u32 *p = (u32 *) adc_data;
702         *p = simple_strtoul(str, &str, 0);
703         if (*p++ && *++str) {
704                 *p = simple_strtoul(str, &str, 0);
705                 if (*p) {
706                         debug("%d : %d -- %d : %d\n",
707                               (int)adc_data[0][0], (int)adc_data[0][1],
708                               (int)adc_data[1][0], (int)adc_data[1][1]);
709                         if (adc_data[0][1] < BIT(10)
710                             && adc_data[1][1] < BIT(10))
711                                 adc_sample_bit = 0;     /*10bits mode */
712                 }
713         }
714
715         return 0;
716 }
717
718 early_param("adc_cal", __adc_cal_setup);
719
720 static int __init __adc_cal_fuse_setup(void)
721 {
722         if (!__is_valid_adc_cal() &&
723             (0 == sci_efuse_get_cal((u32 *) adc_data, 2))) {
724                 debug("%d : %d -- %d : %d\n",
725                       (int)adc_data[0][0], (int)adc_data[0][1],
726                       (int)adc_data[1][0], (int)adc_data[1][1]);
727         }
728
729         return 0;
730 }
731
732 static int __adc2vbat(int adc_res)
733 {
734         int t = adc_data[0][0] - adc_data[1][0];
735         t *= (adc_res - adc_data[0][1]);
736         t /= (adc_data[0][1] - adc_data[1][1]);
737         t += adc_data[0][0];
738         return t;
739 }
740
741 #define MEASURE_TIMES   (15)
742
743 static void __dump_adc_result(u32 adc_val[])
744 {
745 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
746         int i;
747         for (i = 0; i < MEASURE_TIMES; i++) {
748                 printk("%d ", adc_val[i]);
749         }
750         printk("\n");
751 #endif
752 }
753
754 static int cmp_val(const void *a, const void *b)
755 {
756         return *(int *)a - *(int *)b;
757 }
758
759 /**
760  * __adc_voltage - get regulator output voltage through auxadc
761  * @regulator: regulator source
762  *
763  * This returns the current regulator voltage in mV.
764  *
765  * NOTE: If the regulator is disabled it will return the voltage value. This
766  * function should not be used to determine regulator state.
767  */
768 static int regu_adc_voltage(struct regulator_dev *rdev)
769 {
770         struct sci_regulator_desc *desc = __get_desc(rdev);
771         struct sci_regulator_regs *regs = &desc->regs;
772
773         int ret, adc_chan = regs->cal_chan;
774         u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
775         u32 adc_res, adc_val[MEASURE_TIMES];
776         u32 ratio = 1, chan_numerators = 1, chan_denominators = 1;
777         u32 bat_numerators, bat_denominators;
778
779         struct adc_sample_data data = {
780                 .channel_id = adc_chan,
781                 .channel_type = 0,      /*sw */
782                 .hw_channel_delay = 0,  /*reserved */
783                 .scale = (((adc_chan != 13) && (adc_chan != 14)) ? 1 : 0),      /* chanel = 13/14: small scale, others: big scale */
784                 .pbuf = &adc_val[0],
785                 .sample_num = MEASURE_TIMES,
786                 .sample_bits = adc_sample_bit,
787                 .sample_speed = 0,      /*quick mode */
788                 .signal_mode = 0,       /*resistance path */
789         };
790
791         if (!__is_valid_adc_cal())
792                 return -EACCES;
793
794         if (!regs->cal_ctl)
795                 return -EINVAL;
796
797         /* enable ldo cal before adc sampling and ldo calibration */
798         if (ldo_cal_sel) {
799                 mutex_lock(&adc_chan_mutex);
800                 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
801                 debug0("%s adc channel %d : %04x\n",
802                        desc->desc.name, data.channel_id, ldo_cal_sel);
803         }
804
805         ret = sci_adc_get_values(&data);
806         BUG_ON(0 != ret);
807
808         /* close ldo cal and release multiplexed aux adc channel */
809         if (ldo_cal_sel) {
810                 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
811                 mutex_unlock(&adc_chan_mutex);
812         }
813
814         __dump_adc_result(adc_val);
815         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
816         /*__dump_adc_result(adc_val);*/
817
818         ratio =
819             (u32) sci_adc_get_ratio(data.channel_id, data.scale, ldo_cal_sel);
820         chan_numerators = ratio >> 16;
821         chan_denominators = ratio & 0xFFFF;
822
823         ratio = (u32) sci_adc_get_ratio(ADC_CHANNEL_VBAT, 1, 0);
824         bat_numerators = ratio >> 16;
825         bat_denominators = ratio & 0xFFFF;
826
827         adc_res = adc_val[MEASURE_TIMES / 2];
828         debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
829               desc->desc.name, data.channel_id, ldo_cal_sel,
830               chan_numerators, chan_denominators, adc_res);
831
832         if (adc_res == 0)
833                 return -EAGAIN;
834         else
835                 return __adc2vbat(adc_res)
836                     * (bat_numerators * chan_denominators)
837                     / (bat_denominators * chan_numerators);
838 }
839
840 /**
841  * regulator_strongly_disable - strongly disable regulator output
842  * @regulator: regulator source
843  *
844  * Strongly try disable the regulator output voltage or current.
845  * NOTE: this *will* disable the regulator output even if other consumer
846  * devices have it enabled. This should be used for situations when device
847  * had unbalanced with calls to regulator_enable().
848  * *Not* recommended to call this function before try to balance the use_count.
849  */
850 int regulator_strongly_disable(struct regulator *regulator)
851 {
852         struct regulator_dev *rdev = regulator_get_drvdata(regulator);
853         int ret = 0;
854
855         if (rdev)
856                 while (rdev->use_count--)
857                         regulator_disable(regulator);
858
859         return ret;
860 }
861
862 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
863
864 static struct regulator_ops ldo_ops = {
865         .enable = ldo_turn_on,
866         .disable = ldo_turn_off,
867         .is_enabled = ldo_is_on,
868         .set_voltage = ldo_set_voltage,
869         .get_voltage = ldo_get_voltage,
870         .set_mode = ldo_set_mode,
871 };
872
873 static struct regulator_ops dcdc_ops = {
874         .enable = ldo_turn_on,
875         .disable = ldo_turn_off,
876         .is_enabled = ldo_is_on,
877         .set_voltage = dcdc_set_voltage_step,
878         .get_voltage = dcdc_get_voltage,
879 };
880
881 static struct regulator_ops boost_ops = {
882         .enable = ldo_turn_on,
883         .disable = ldo_turn_off,
884         .is_enabled = ldo_is_on,
885         .set_current_limit = boost_set_current_limit,
886         .get_current_limit = boost_get_current_limit,
887         .set_mode = ldo_set_mode,
888 };
889
890 /*
891  * Consider the following machine :-
892  *
893  *   Regulator-1 -+-> [Consumer A @ 1.8V]
894  *                |
895  *                +-> [Consumer B @ 1.8V]
896  *
897  *   Regulator-2 ---> [Consumer C @ 3.3V]
898  *
899  * The drivers for consumers A & B must be mapped to the correct regulator in
900  * order to control their power supply. This mapping can be achieved in board/machine
901  * initialisation code by creating a struct regulator_consumer_supply for each regulator.
902  * Alternatively, we built a regulator supply-consumers map, the format is as follow:
903  *
904  *      supply source-1, consumer A, consumer B, ..., NULL
905  *      supply source-2, consumer C, ..., NULL
906  *      ...
907  *      NULL
908  *
909  */
910 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
911                                                         const char *supply_name,
912                                                         int *num)
913 {
914         char **map = (char **)dev_get_platdata(dev);
915         int i, n;
916         struct regulator_consumer_supply *consumer_supplies = NULL;
917
918         if (!supply_name || !(map && map[0]))
919                 return NULL;
920
921         for (i = 0; map[i] || map[i + 1]; i++) {
922                 if (map[i] && 0 == strcmp(map[i], supply_name))
923                         break;
924         }
925
926         /* i++; *//* Do not skip supply name */
927
928         for (n = 0; map[i + n]; n++) ;
929
930         if (n) {
931                 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
932                 consumer_supplies =
933                     kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
934                 BUG_ON(!consumer_supplies);
935                 for (n = 0; map[i]; i++, n++) {
936                         consumer_supplies[n].supply = map[i];
937                 }
938                 if (num)
939                         *num = n;
940         }
941         return consumer_supplies;
942 }
943
944 #if defined(CONFIG_DEBUG_FS)
945 static struct dentry *debugfs_root = NULL;
946
947 static u32 ana_addr = 0;
948 static int debugfs_ana_addr_get(void *data, u64 * val)
949 {
950         if (ana_addr < PAGE_SIZE) {
951                 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
952         } else {
953                 void *addr = ioremap(ana_addr, PAGE_SIZE);
954                 *val = __raw_readl(addr);
955                 iounmap(addr);
956         }
957         return 0;
958 }
959
960 static int debugfs_ana_addr_set(void *data, u64 val)
961 {
962         if (ana_addr < PAGE_SIZE) {
963                 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val,
964                             -1);
965         } else {
966                 void *addr = ioremap(ana_addr, PAGE_SIZE);
967                 __raw_writel(val, addr);
968                 iounmap(addr);
969         }
970         return 0;
971 }
972
973 static int adc_chan = 5 /*VBAT*/;
974 static int debugfs_adc_chan_get(void *pdata, u64 * val)
975 {
976         int i, ret;
977         u32 adc_res, adc_val[MEASURE_TIMES];
978         struct adc_sample_data data = {
979                 .channel_id = adc_chan,
980                 .channel_type = 0,      /*sw */
981                 .hw_channel_delay = 0,  /*reserved */
982                 .scale = 1,     /*big scale */
983                 .pbuf = &adc_val[0],
984                 .sample_num = MEASURE_TIMES,
985                 .sample_bits = adc_sample_bit,
986                 .sample_speed = 0,      /*quick mode */
987                 .signal_mode = 0,       /*resistance path */
988         };
989
990         ret = sci_adc_get_values(&data);
991         BUG_ON(0 != ret);
992
993         for (i = 0; i < MEASURE_TIMES; i++) {
994                 printk("%d ", adc_val[i]);
995         }
996         printk("\n");
997
998         sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
999         adc_res = adc_val[MEASURE_TIMES / 2];
1000         pr_info("adc chan %d, result value %d, vbat %d\n",
1001                 data.channel_id, adc_res, __adc2vbat(adc_res));
1002         *val = adc_res;
1003         return 0;
1004 }
1005
1006 static int debugfs_adc_chan_set(void *data, u64 val)
1007 {
1008         adc_chan = val;
1009         return 0;
1010 }
1011
1012 static int debugfs_enable_get(void *data, u64 * val)
1013 {
1014         struct regulator_dev *rdev = data;
1015         if (rdev && rdev->desc->ops->is_enabled)
1016                 *val = rdev->desc->ops->is_enabled(rdev);
1017         else
1018                 *val = -1;
1019         return 0;
1020 }
1021
1022 static int debugfs_enable_set(void *data, u64 val)
1023 {
1024         struct regulator_dev *rdev = data;
1025         if (rdev && rdev->desc->ops->enable)
1026                 (val) ? rdev->desc->ops->enable(rdev)
1027                     : rdev->desc->ops->disable(rdev);
1028         return 0;
1029 }
1030
1031 static int debugfs_voltage_get(void *data, u64 * val)
1032 {
1033         struct regulator_dev *rdev = data;
1034         if (rdev)
1035                 *val = regu_adc_voltage(rdev);
1036         else
1037                 *val = -1;
1038         return 0;
1039 }
1040
1041 static int debugfs_voltage_set(void *data, u64 val)
1042 {
1043         struct regulator_dev *rdev = data;
1044         u32 min_uV;
1045         if (rdev && rdev->desc->ops->set_voltage) {
1046                      min_uV = (u32)val * 1000;
1047                 min_uV += rdev->constraints->uV_offset;
1048                 rdev->desc->ops->set_voltage(rdev, min_uV, min_uV, 0);
1049         }
1050         return 0;
1051 }
1052
1053 static int debugfs_boost_get(void *data, u64 * val)
1054 {
1055         struct regulator_dev *rdev = data;
1056         if (rdev && rdev->desc->ops->get_current_limit)
1057                 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
1058         else
1059                 *val = -1;
1060         return 0;
1061 }
1062
1063 static int debugfs_boost_set(void *data, u64 val)
1064 {
1065         struct regulator_dev *rdev = data;
1066         if (rdev && rdev->desc->ops->set_current_limit)
1067                 rdev->desc->ops->set_current_limit(rdev, val * 1000,
1068                                                    val * 1000);
1069         return 0;
1070 }
1071
1072 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
1073                         debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
1074 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
1075                         debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
1076 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
1077                         debugfs_enable_get, debugfs_enable_set, "%llu\n");
1078 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
1079                         debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
1080 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
1081                         debugfs_boost_get, debugfs_boost_set, "%llu\n");
1082
1083 static void rdev_init_debugfs(struct regulator_dev *rdev)
1084 {
1085         struct sci_regulator_desc *desc = __get_desc(rdev);
1086         desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
1087         if (IS_ERR_OR_NULL(rdev->debugfs)) {
1088                 pr_warn("Failed to create debugfs directory\n");
1089                 rdev->debugfs = NULL;
1090                 return;
1091         }
1092
1093         debugfs_create_file("enable", S_IRUGO | S_IWUSR,
1094                             desc->debugfs, rdev, &fops_enable);
1095
1096         if (desc->desc.type == REGULATOR_CURRENT)
1097                 debugfs_create_file("current", S_IRUGO | S_IWUSR,
1098                                     desc->debugfs, rdev, &fops_boost);
1099         else
1100                 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
1101                                     desc->debugfs, rdev, &fops_ldo);
1102 }
1103 #else
1104 static void rdev_init_debugfs(struct regulator_dev *rdev)
1105 {
1106 }
1107 #endif
1108
1109 static inline int __strcmp(const char *cs, const char *ct)
1110 {
1111         if (!cs || !ct)
1112                 return -1;
1113
1114         return strcmp(cs, ct);
1115 }
1116
1117 static struct of_device_id sprd_regulator_of_match[] = {
1118         {.compatible = "sprd,sc2723-regulator",},
1119         {}
1120 };
1121
1122 #undef REGS_ANA_GLB_BASE
1123 #ifndef REGS_ANA_GLB_BASE
1124 #define REGS_ANA_GLB_PHYS                       (SPRD_ADI_PHYS + 0x8800)
1125 #define REGS_ANA_GLB_BASE                       (SPRD_ADI_BASE + 0x8800)
1126 #define REGS_ANA_GLB_SIZE                       (SZ_2K) //0x40038800 ~ 0x40039000
1127 #endif
1128
1129 #define IS_VALID_ANA_ADDR(pa, pa_base, size)    ( (pa) >= (pa_base) && (pa) < (pa_base) + (size) )
1130 #define PHY_TO_VIR(pa, pa_base, va_base)                ( (pa) - (pa_base) + (va_base) )
1131
1132 static unsigned long phy2vir(const u32 reg_phy)
1133 {
1134         unsigned long reg_vir = 0;
1135
1136         if (!reg_phy)
1137                 return 0;
1138
1139         if (IS_VALID_ANA_ADDR(reg_phy, REGS_ANA_GLB_PHYS, REGS_ANA_GLB_SIZE)) {
1140                 reg_vir =
1141                     (unsigned
1142                      long)(PHY_TO_VIR(reg_phy, REGS_ANA_GLB_PHYS,
1143                                       REGS_ANA_GLB_BASE));
1144         } else {
1145                 WARN(1, "reg(0x%08x) physical address is invalid!\n", reg_phy);
1146         }
1147
1148         return reg_vir;
1149 }
1150
1151 static int reconfig_regulator(struct sci_regulator_desc *desc)
1152 {
1153         struct sci_regulator_regs *regs = &desc->regs;
1154
1155         /* Fixme: Config DCDC  linear/no linear control
1156          *   accoring to BIT14 of Reg(0x40038800 + 0x0118)
1157          */
1158         if (ana_mixed_ctl & BIT_DCDC_V_CTRL_MODE) {
1159                 /* dcdc linear control */
1160                 if ((0 == strcmp(desc->desc.name, "vddcore"))
1161                     || (0 == strcmp(desc->desc.name, "vddarm"))) {
1162                         regs->vol_ctl = 0;
1163                         regs->vol_ctl_bits = 0;
1164                 }
1165         } else {
1166                 /* dcdc Non-linear control */
1167                 if ((0 == strcmp(desc->desc.name, "vddcore"))
1168                     || (0 == strcmp(desc->desc.name, "vddarm"))) {
1169                         regs->vol_ctl = regs->vol_trm;
1170                         regs->vol_trm_bits =
1171                             (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4));
1172                         regs->vol_ctl_bits = (BIT(5) | BIT(6) | BIT(7));
1173                         regs->vol_sel_cnt = ARRAY_SIZE(dcdc_vol_select);
1174                         regs->vol_sel = dcdc_vol_select;
1175                 }
1176         }
1177
1178         return 0;
1179 }
1180
1181 static int of_regu_read_reg(struct device_node *np, int idx,
1182                             struct sci_regulator_regs *regs)
1183 {
1184         const __be32 *cell;
1185         u32 reg_phy;
1186         int count;
1187
1188         if (!np || !regs)
1189                 return -EINVAL;
1190
1191         cell = of_get_property(np, "reg", &count);
1192         count /= 4;
1193         if (cell)
1194                 WARN_ON(idx >= count);
1195
1196         cell = of_get_address(np, idx, NULL, NULL);
1197
1198         switch (idx) {
1199         case 0:         /* ldo/dcdc power down */
1200                 if (cell) {
1201                         reg_phy = (u32) be32_to_cpu(*(cell++));
1202                         regs->pd_set = phy2vir(reg_phy);
1203                         regs->pd_set_bit = (u32) be32_to_cpu(*(cell++));
1204                         debug0
1205                             ("reg(%s) pd_set phy addr: 0x%08x, vir addr: 0x%08x, msk: 0x%08x\n",
1206                              np->name, reg_phy, regs->pd_set, regs->pd_set_bit);
1207                 }
1208                 break;
1209         case 1:         /* ldo/dcdc voltage trim */
1210                 if (cell) {
1211                         reg_phy = (u32) be32_to_cpu(*(cell++));
1212                         regs->vol_trm = phy2vir(reg_phy);
1213                         regs->vol_trm_bits = (u32) be32_to_cpu(*(cell++));
1214                         debug0
1215                             ("reg(%s) vol_trm phy addr: 0x%08x, vir addr: 0x%08x, msk: 0x%08x\n",
1216                              np->name, reg_phy, regs->vol_trm,
1217                              regs->vol_trm_bits);
1218                 }
1219                 break;
1220         case 2:         /* otp pwr select */
1221                 if (cell) {
1222                         reg_phy = (u32) be32_to_cpu(*(cell++));
1223                         regs->pwr_sel = phy2vir(reg_phy);
1224                         regs->pwr_sel_bit = (u32) be32_to_cpu(*(cell++));
1225                         debug0
1226                             ("reg(%s) otp_pwr_sel phy addr: 0x%08x, vir addr: 0x%08x, msk: 0x%08x\n",
1227                              np->name, reg_phy, regs->pwr_sel,
1228                              regs->pwr_sel_bit);
1229                 }
1230                 break;
1231         default:
1232                 break;
1233         }
1234
1235         return 0;
1236 }
1237
1238 static int sci_regulator_parse_dt(struct platform_device *pdev,
1239                                   struct device_node *np,
1240                                   struct sci_regulator_desc *desc,
1241                                   struct regulator_consumer_supply *supply,
1242                                   int sz)
1243 {
1244         struct sci_regulator_regs *regs = &desc->regs;
1245         const __be32 *tmp;
1246         u32 data[8] = { 0 };
1247         u32 tmp_val_u32;
1248         int type = 0, cnt = 0, ret = 0;
1249
1250         if (!pdev || !np || !desc || !supply) {
1251                 return -EINVAL;
1252         }
1253
1254         desc->desc.name = np->name;
1255         desc->desc.id = (atomic_inc_return(&idx) - 1);
1256         desc->desc.type = REGULATOR_VOLTAGE;
1257         desc->desc.owner = THIS_MODULE;
1258
1259         supply[0].dev_name = NULL;
1260         supply[0].supply = np->name;
1261         desc->init_data = of_get_regulator_init_data(&pdev->dev, np);
1262         if (!desc->init_data
1263             || 0 != __strcmp(desc->init_data->constraints.name, np->name)) {
1264                 dev_err(&pdev->dev,
1265                         "failed to parse regulator(%s) init data! \n",
1266                         np->name);
1267                 return -EINVAL;
1268         }
1269
1270         desc->init_data->supply_regulator = 0;
1271         desc->init_data->constraints.valid_modes_mask =
1272             REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1273         desc->init_data->constraints.valid_ops_mask =
1274             REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS |
1275             REGULATOR_CHANGE_VOLTAGE;
1276         desc->init_data->num_consumer_supplies = sz;
1277
1278         desc->init_data->consumer_supplies =
1279             set_supply_map(&pdev->dev, desc->desc.name,
1280                            &desc->init_data->num_consumer_supplies);
1281
1282         if (!desc->init_data->consumer_supplies)
1283                 desc->init_data->consumer_supplies = supply;
1284
1285         /* Fill struct sci_regulator_regs variable desc->regs */
1286         type = (of_property_read_bool(np, "dcdc") ? 2 : 0);
1287         regs->typ = type;
1288
1289         if (of_property_read_bool(np, "default-on"))
1290                 regs->typ |= BIT(4);
1291
1292         of_regu_read_reg(np, 0, regs);
1293         of_regu_read_reg(np, 1, regs);
1294         of_regu_read_reg(np, 2, regs);
1295
1296         regs->min_uV = desc->init_data->constraints.min_uV;
1297         regs->max_uV = desc->init_data->constraints.max_uV;
1298
1299         ret =
1300             of_property_read_u32(np, "regulator-step-microvolt", &tmp_val_u32);
1301         if (!ret)
1302                 regs->step_uV = tmp_val_u32;
1303
1304         ret =
1305             of_property_read_u32(np, "regulator-default-microvolt",
1306                                  &tmp_val_u32);
1307         if (!ret)
1308                 regs->vol_def = tmp_val_u32;
1309
1310         ret = of_property_read_u32(np, "hide-offset", &tmp_val_u32);
1311         if (!ret)
1312                 regs->hide_offset = ((int)tmp_val_u32 - 1000);/*base value is 1000*/
1313
1314         debug("[%d] %s type %d, range %d(uV) - %d(uV), step %d(uV), default %d(uV) - (%s)\n", (idx.counter - 1),
1315                 np->name, type, desc->init_data->constraints.min_uV, desc->init_data->constraints.max_uV,
1316                 regs->step_uV, regs->vol_def, (regs->typ & BIT(4)) ? "on" : "off");
1317
1318
1319         tmp = of_get_property(np, "regulator-cal-channel", &cnt);
1320         if (tmp) {
1321                 cnt /= 4;
1322                 ret =
1323                     of_property_read_u32_array(np, "regulator-cal-channel",
1324                                                data, cnt);
1325                 if (!ret) {
1326                         regs->cal_ctl = phy2vir(data[0]);
1327                         regs->cal_ctl_bits = data[1];
1328                         regs->cal_chan = data[2];
1329                 }
1330
1331                 debug0
1332                     ("cal_ctl (phyaddr: 0x%08x, vir addr: 0x%08x, msk: 0x%08x, chan: %d)\n",
1333                      data[0], regs->cal_ctl, regs->cal_ctl_bits,
1334                      regs->cal_chan);
1335         }
1336 #if 0
1337         tmp = of_get_property(np, "regulator-selects", &cnt);
1338         if (tmp) {
1339                 debug0("prop regulator-selects count(%d)\n", cnt);
1340                 cnt /= 4;
1341                 ret =
1342                     of_property_read_u32_array(np, "regulator-selects", data,
1343                                                cnt);
1344                 if (!ret) {
1345                         int i = 0;
1346
1347                         /* Dynamically allocate memory for voltage select array */
1348                         regs->vol_sel =
1349                             kzalloc(cnt * sizeof(*regs->vol_sel), GFP_KERNEL);
1350                         if (!regs->vol_sel) {
1351                                 pr_err
1352                                     ("%s() failed to allocate memory for voltage select\n",
1353                                      __func__);
1354                                 return -ENOMEM;
1355                         }
1356
1357                         regs->vol_sel_cnt = cnt;
1358                         for (i = 0; i < cnt; i++) {
1359                                 regs->vol_sel[i] = data[i];
1360                         }
1361                 }
1362         }
1363 #endif
1364
1365         return 0;
1366 }
1367
1368 static int sci_regulator_register_dt(struct platform_device *pdev)
1369 {
1370         struct sci_regulator_desc *sci_desc = NULL;
1371
1372         struct regulator_dev *rdev;
1373         struct regulator_ops *__regs_ops[] = {
1374                 &ldo_ops, 0, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops, 0,
1375         };
1376         struct regulator_consumer_supply consumer_supplies_default[1] = { };
1377         struct regulator_config config = { };
1378
1379         struct device_node *dev_np = pdev->dev.of_node;
1380         struct device_node *child_np;
1381         int regu_cnt = 0, ret = 0;
1382
1383         regu_cnt = of_get_child_count(dev_np);
1384         if (!regu_cnt)
1385                 return -EINVAL;
1386
1387         regu_cnt -= 1;          /* exclude dummy node */
1388
1389         sci_desc_list =
1390             devm_kzalloc(&pdev->dev,
1391                          regu_cnt * sizeof(struct sci_regulator_desc),
1392                          GFP_KERNEL);
1393         if (!sci_desc_list) {
1394                 dev_err(&pdev->dev,
1395                         "failed allocate memory for sci_regulator_desc list\n");
1396                 return -ENOMEM;
1397         }
1398         sci_desc = sci_desc_list;
1399
1400         debug
1401             ("regulators desc list 0x%p, count %d, sci_regulator_desc size %d\n",
1402              sci_desc, regu_cnt, sizeof(struct sci_regulator_desc));
1403
1404         for_each_child_of_node(dev_np, child_np) {
1405                 if (0 == strcmp(child_np->name, "dummy"))       /* skip dummy node */
1406                         continue;
1407
1408
1409                 if(0 == strcmp(child_np->name, "vddbigarm"))
1410                         continue;
1411
1412                 ret = sci_regulator_parse_dt(pdev, child_np, sci_desc, consumer_supplies_default, ARRAY_SIZE(consumer_supplies_default));
1413                 if(ret) {
1414                         dev_err(&pdev->dev, "failed to parse regulator(%s) dts\n", child_np->name);
1415                         continue;
1416                 }
1417
1418                 reconfig_regulator(sci_desc);
1419
1420                 BUG_ON((sci_desc->regs.typ & (BIT(4) - 1)) >=
1421                        ARRAY_SIZE(__regs_ops));
1422                 if (!sci_desc->desc.ops)
1423                         sci_desc->desc.ops =
1424                             __regs_ops[sci_desc->regs.typ & (BIT(4) - 1)];
1425
1426                 config.dev = &pdev->dev;
1427                 config.init_data = sci_desc->init_data;
1428                 config.driver_data = NULL;
1429                 config.of_node = child_np;
1430                 rdev = regulator_register(&sci_desc->desc, &config);
1431
1432                 debug0("regulator_desc 0x%p, rdev 0x%p\n", &sci_desc->desc,
1433                        rdev);
1434
1435                 if (sci_desc->init_data->consumer_supplies !=
1436                     consumer_supplies_default)
1437                         kfree(sci_desc->init_data->consumer_supplies);
1438
1439                 if (!IS_ERR_OR_NULL(rdev)) {
1440                         rdev->reg_data = rdev;
1441                         sci_desc->data.rdev = rdev;
1442                         __init_trimming(rdev);
1443                         rdev_init_debugfs(rdev);
1444                 }
1445
1446                 sci_desc++;
1447         }
1448
1449         return 0;
1450 }
1451
1452 /**
1453  * IMPORTANT!!!
1454  * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1455  * so i autogen all regulators non-variable description in plat or mach directory,
1456  * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1457  * just like other regulator vendor drivers.
1458  */
1459 static int sci_regulator_probe(struct platform_device *pdev)
1460 {
1461 #ifdef CONFIG_DEBUG_FS
1462         debugfs_root = debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1463         if (IS_ERR_OR_NULL(debugfs_root)) {
1464                 WARN(!debugfs_root,
1465                      "%s: Failed to create debugfs directory\n",
1466                      REGULATOR_ROOT_DIR);
1467                 debugfs_root = NULL;
1468         }
1469
1470         /* compatible with 8810 adc test */
1471         debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1472                            debugfs_root, (u32 *) & ana_addr);
1473         debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1474                             debugfs_root, &ana_addr, &fops_ana_addr);
1475         debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1476                             debugfs_root, &adc_chan, &fops_adc_chan);
1477         debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1478                            debugfs_root, (u64 *) & adc_data);
1479
1480         {                       /* vddarm/vddcore/vddmem common debugfs interface */
1481                 char str[NAME_MAX];
1482                 struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1483                 sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1484                 debugfs_create_symlink("dcdcarm", vol_root, str);
1485                 sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1486                 debugfs_create_symlink("dcdccore", vol_root, str);
1487                 sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1488                 debugfs_create_symlink("dcdcmem", vol_root, str);
1489         }
1490 #endif
1491
1492         ana_chip_id = ((u32) ANA_REG_GET(ANA_REG_GLB_CHIP_ID_HIGH) << 16) |
1493             ((u32) ANA_REG_GET(ANA_REG_GLB_CHIP_ID_LOW) & 0xFFFF);
1494         ana_mixed_ctl = ANA_REG_GET(ANA_REG_GLB_MIXED_CTRL0);
1495         otp_pwr_sel = ANA_REG_GET(ANA_REG_GLB_PWR_SEL);
1496
1497         pr_info
1498             ("sc272x ana chipid:(0x%08x), ana_mixed_ctl:(0x%08x), otp_sel:(0x%08x)\n",
1499              ana_chip_id, ana_mixed_ctl, otp_pwr_sel);
1500
1501         spl_start_base = ioremap(SPRD_SPL_PHYS, 0x8000);
1502         if (spl_start_base) {
1503                 pr_info("%s remap iram phy addr(%#x) to vir addr (0x%p) ok!\n",
1504                         __func__, SPRD_SPL_PHYS, spl_start_base);
1505         } else {
1506                 pr_info("%s remap iram phy addr(%#x) error!\n", __func__,
1507                         SPRD_SPL_PHYS);
1508         }
1509
1510         sci_regulator_register_dt(pdev);
1511
1512         if (spl_start_base) {
1513                 iounmap(spl_start_base);
1514                 spl_start_base = NULL;
1515         }
1516
1517         return 0;
1518 }
1519
1520 static int sci_regulator_remove(struct platform_device *pdev)
1521 {
1522         if (sci_desc_list) {
1523                 if (sci_desc_list->init_data) {
1524                         kfree(sci_desc_list->init_data);
1525                 }
1526                 kfree(sci_desc_list);
1527         }
1528
1529         return 0;
1530 }
1531
1532 static struct platform_driver sci_regulator_driver = {
1533         .driver = {
1534                    .name = "sc2723-regulator",
1535                    .owner = THIS_MODULE,
1536                    .of_match_table = of_match_ptr(sprd_regulator_of_match),
1537                    },
1538         .probe = sci_regulator_probe,
1539         .remove = sci_regulator_remove
1540 };
1541
1542 static int __init regu_driver_init(void)
1543 {
1544         __adc_cal_fuse_setup();
1545
1546         return platform_driver_register(&sci_regulator_driver);
1547 }
1548
1549 int __init sci_regulator_init(void)
1550 {
1551 #if 0
1552         return of_platform_populate(of_find_node_by_path("/sprd-regulators"),
1553                                     sprd_regulator_of_match, NULL, NULL);
1554 #else
1555         return 0;
1556 #endif
1557 }
1558
1559 subsys_initcall(regu_driver_init);
1560
1561 MODULE_LICENSE("GPL v2");
1562 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1563 MODULE_AUTHOR("kevin <ronghua.yu@spreadtrum.com>");
1564 MODULE_VERSION("0.6");