2 * Copyright (C) 2013 Spreadtrum Communications Inc.
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.
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.
16 * Bug#183980 add dcdc and pll enable time
17 * Change-Id: I6e6e06ee0beb306cd846964d0ba24aef449e5beb
19 * Bug#164001 add dcdc mem/gen/wpa/wrf map
20 * Change-Id: I07dac5700c0907aca99f6112bd4b5799358a9a88
22 * Bug#164001 shark dcam: add camera ldo calibration
23 * Change-Id: Icaee2706b8b0985ae6f3122b236d8e278dcc0db2
25 * sc8830: fix adc cal data from cmdline fail
26 * Change-Id: Id85d58178aca40fdf13b996853711e92e1171801
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/spinlock.h>
37 #include <linux/debugfs.h>
38 #include <linux/slab.h>
39 #include <linux/sort.h>
40 #include <linux/delay.h>
41 #include <linux/err.h>
43 #include <linux/platform_device.h>
45 #include <linux/of_device.h>
46 #include <linux/regulator/of_regulator.h>
48 #include <linux/regulator/consumer.h>
49 #include <linux/regulator/driver.h>
50 #include <linux/regulator/machine.h>
52 #include <mach/hardware.h>
54 #include <mach/sci_glb_regs.h>
57 #include <mach/arch_misc.h>
59 #define REGULATOR_ROOT_DIR "sprd-regulator"
62 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
63 #define debug0(format, arg...) //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
64 #define debug2(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
67 #define ANA_REG_OR(_r, _b) sci_adi_write(_r, _b, 0)
71 #define ANA_REG_BIC(_r, _b) sci_adi_write(_r, 0, _b)
75 #define ANA_REG_GET(_r) sci_adi_read(_r)
79 #define ANA_REG_SET(_r, _v, _m) sci_adi_write((_r), ((_v) & (_m)), (_m))
82 struct sci_regulator_regs {
84 u32 pd_set, pd_set_bit;
86 * at new feature, some LDOs had only set, no rst bits.
87 * and DCDCs voltage and trimming controller is the same register
89 u32 pd_rst, pd_rst_bit;
90 u32 slp_ctl, slp_ctl_bit;
91 u32 vol_trm, vol_trm_bits;
92 u32 cal_ctl, cal_ctl_bits;
94 u32 vol_ctl, vol_ctl_bits;
95 u32 vol_sel_cnt, vol_sel[];
99 * struct sci_regulator_ops - sci regulator operations.
103 * This struct describes regulator operations which can be implemented by
104 * regulator chip drivers.
106 struct sci_regulator_ops {
107 int trimming_def_val; /* trimming controller default value in A-Die */
108 int (*get_trimming_step) (struct regulator_dev * rdev, int);
109 int (*set_trimming) (struct regulator_dev * rdev, int, int, int);
110 int (*calibrate) (struct regulator_dev * rdev, int, int);
113 struct sci_regulator_data {
114 struct delayed_work dwork;
115 struct regulator_dev *rdev;
118 struct sci_regulator_desc {
119 struct regulator_desc desc;
120 struct sci_regulator_ops *ops;
121 const struct sci_regulator_regs *regs;
122 struct sci_regulator_data data; /* FIXME: dynamic */
123 #if defined(CONFIG_DEBUG_FS)
124 struct dentry *debugfs;
136 #define REGU_VERIFY_DLY (1000) /*ms */
138 static DEFINE_MUTEX(adc_chan_mutex);
139 static int __is_trimming(struct regulator_dev *);
140 static int __regu_calibrate(struct regulator_dev *, int, int);
141 extern int sci_efuse_calibration_get(u32 * p_cal_data);
143 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
144 VOL_TRM, VOL_TRM_BITS, CAL_CTL, CAL_CTL_BITS, VOL_DEF, \
145 VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...) \
147 static const struct sci_regulator_regs REGS_##VDD = { \
150 .pd_set_bit = SET_BIT, \
152 .pd_rst_bit = RST_BIT, \
153 .slp_ctl = SLP_CTL, \
154 .slp_ctl_bit = SLP_CTL_BIT, \
155 .vol_trm = VOL_TRM, \
156 .vol_trm_bits = VOL_TRM_BITS, \
157 .cal_ctl = CAL_CTL, \
158 .cal_ctl_bits = CAL_CTL_BITS, \
159 .vol_def = VOL_DEF, \
160 .vol_ctl = VOL_CTL, \
161 .vol_ctl_bits = VOL_CTL_BITS, \
162 .vol_sel_cnt = VOL_SEL_CNT, \
163 .vol_sel = {__VA_ARGS__}, \
165 static struct sci_regulator_desc DESC_##VDD = { \
169 .desc.type = REGULATOR_VOLTAGE, \
170 .desc.owner = THIS_MODULE, \
171 .regs = ®S_##VDD, \
173 sci_regulator_register(pdev, &DESC_##VDD); \
176 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
178 return (struct sci_regulator_desc *)rdev->desc;
181 /* standard ldo ops*/
182 static int ldo_turn_on(struct regulator_dev *rdev)
184 struct sci_regulator_desc *desc = __get_desc(rdev);
185 const struct sci_regulator_regs *regs = desc->regs;
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));
192 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
195 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
197 debug2("regu %p (%s), turn on\n", regs, desc->desc.name);
198 /* ldo trimming when first turn on */
199 if (desc->ops && !__is_trimming(rdev))
200 __regu_calibrate(rdev, 0, 0);
204 static int ldo_turn_off(struct regulator_dev *rdev)
206 struct sci_regulator_desc *desc = __get_desc(rdev);
207 const struct sci_regulator_regs *regs = desc->regs;
209 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
210 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
211 regs->pd_rst, __ffs(regs->pd_rst_bit));
212 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
214 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
217 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
219 debug2("regu %p (%s), turn off\n", regs, desc->desc.name);
223 static int ldo_is_on(struct regulator_dev *rdev)
226 struct sci_regulator_desc *desc = __get_desc(rdev);
227 const struct sci_regulator_regs *regs = desc->regs;
229 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
230 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
231 regs->pd_rst, __ffs(regs->pd_rst_bit));
233 if (regs->pd_rst && regs->pd_set) {
234 /*for pd_rst has higher prioty than pd_set, what's more, their reset values are the same, 0 */
235 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
236 /* FIXME: when reset, pd_set & pd_rst are all zero, always get here */
237 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
239 } else if (regs->pd_rst) {
240 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
241 } else if (regs->pd_set) { /* new feature */
242 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
245 debug2("regu %p (%s) return %d\n", regs, desc->desc.name, ret);
249 #if 0 /* FIXME: todo later */
250 static int ldo_enable_time(struct regulator_dev *rdev)
252 return 1000 * 1; /*Microseconds */
256 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
258 struct sci_regulator_desc *desc = __get_desc(rdev);
259 const struct sci_regulator_regs *regs = desc->regs;
261 debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
262 regs->slp_ctl, regs->slp_ctl_bit, mode);
267 if (mode == REGULATOR_MODE_STANDBY) { /* disable auto slp */
268 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
270 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
275 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
276 int max_uV, unsigned *selector)
278 static const int vol_bits[4] = { 0xa, 0x9, 0x6, 0x5 };
279 struct sci_regulator_desc *desc = __get_desc(rdev);
280 const struct sci_regulator_regs *regs = desc->regs;
281 int mv = min_uV / 1000;
283 int i, shft = __ffs(regs->vol_ctl_bits);
284 int has_rst_bit = !(0x3 == (regs->vol_ctl_bits >> shft)); /* new feature */
286 BUG_ON(regs->vol_sel_cnt > 4);
287 debug("regu %p (%s) %d %d (%d)\n", regs, desc->desc.name, min_uV,
288 max_uV, has_rst_bit);
292 for (i = 0; i < regs->vol_sel_cnt; i++) {
293 if (regs->vol_sel[i] == mv) {
294 ANA_REG_SET(regs->vol_ctl,
295 ((!has_rst_bit) ? i : vol_bits[i]) << shft,
297 /*clear_bit(desc->desc.id, trimming_state); */
304 "warning: regulator (%s) not support %dmV\n", desc->desc.name, mv);
308 static int ldo_get_voltage(struct regulator_dev *rdev)
310 struct sci_regulator_desc *desc = __get_desc(rdev);
311 const struct sci_regulator_regs *regs = desc->regs;
313 int i, shft = __ffs(regs->vol_ctl_bits);
314 int has_rst_bit = !(0x3 == (regs->vol_ctl_bits >> shft)); /* new feature */
316 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
317 regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
322 BUG_ON(regs->vol_sel_cnt != 4);
323 vol_bits = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
327 } else if (((vol_bits & BIT(0)) ^ (vol_bits & BIT(1))
328 && (vol_bits & BIT(2)) ^ (vol_bits & BIT(3)))) {
329 i = (vol_bits & BIT(0)) | ((vol_bits >> 1) & BIT(1));
333 vol = regs->vol_sel[i];
334 debug2("regu %p (%s), voltage %d\n", regs, desc->desc.name, vol);
338 static unsigned long trimming_state[2] = { 0, 0 }; /* max 64 bits */
340 /* FIXME: get dcdc cal offset config from uboot */
341 #define DCDC_CAL_CONF_BASE (SPRD_IRAM0_BASE + 0x1f00)
342 #define DCDC_MAX_CNT (4)
348 static int __dcdc_get_offset(struct regulator_dev *rdev)
350 #ifdef CONFIG_ARCH_SCX35
351 struct sci_regulator_desc *desc = __get_desc(rdev);
352 struct dcdc_cal_t *dcdc = (struct dcdc_cal_t *)DCDC_CAL_CONF_BASE;
354 for (i = 0; i < DCDC_MAX_CNT; i++) {
355 if (0 == strcmp(dcdc[i].name, desc->desc.name)) {
356 debug("regu %p (%s) offset %+dmV\n", desc->regs,
357 desc->desc.name, dcdc[i].cal_vol);
358 return dcdc[i].cal_vol * 1000; /*uV */
365 static int __is_trimming(struct regulator_dev *rdev)
370 BUG_ON(!(id > 0 && id < sizeof(trimming_state) * 8));
371 return test_bit(id, trimming_state);
374 static int __init_trimming(struct regulator_dev *rdev)
376 struct sci_regulator_desc *desc = __get_desc(rdev);
377 const struct sci_regulator_regs *regs = desc->regs;
381 if (!regs->vol_trm || !desc->ops)
384 trim = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits)
385 >> __ffs(regs->vol_trm_bits);
386 if (0 == regs->vol_def && desc->regs->typ == 2 /*DCDC*/) {
387 rdev->constraints->uV_offset = __dcdc_get_offset(rdev);
388 } else if (trim != desc->ops->trimming_def_val && !(regs->vol_def & 1)) {
389 /* some DCDC/LDOs had been calibrated in uboot-spl */
390 debug("regu %p (%s) trimming ok before startup\n", regs,
392 set_bit(desc->desc.id, trimming_state);
395 ret = __regu_calibrate(rdev, 0, 0);
402 * ldo trimming step about 0.625%, range 90% ~ 109.375%. that all maps as follow.
404 0x1F : +9.375 : 109.375
405 0x1E : +8.750 : 108.750
406 0x1D : +8.125 : 108.125
407 0x1C : +7.500 : 107.500
408 0x1B : +6.875 : 106.875
409 0x1A : +6.250 : 106.250
410 0x19 : +5.625 : 105.625
411 0x18 : +5.000 : 105.000
412 0x17 : +4.375 : 104.375
413 0x16 : +3.750 : 103.750
414 0x15 : +3.125 : 103.125
415 0x14 : +2.500 : 102.500
416 0x13 : +1.875 : 101.875
417 0x12 : +1.250 : 101.250
418 0x11 : +0.625 : 100.625
419 0x10 : +0.000 : 100.000
420 0x0F : -0.625 : 99.375
421 0x0E : -1.250 : 98.750
422 0x0D : -1.875 : 98.125
423 0x0C : -2.500 : 97.500
424 0x0B : -3.125 : 96.875
425 0x0A : -3.750 : 96.250
426 0x09 : -4.375 : 95.625
427 0x08 : -5.000 : 95.000
428 0x07 : -5.625 : 94.375
429 0x06 : -6.250 : 93.750
430 0x05 : -6.875 : 93.125
431 0x04 : -7.500 : 92.500
432 0x03 : -8.125 : 91.875
433 0x02 : -8.750 : 91.250
434 0x01 : -9.375 : 90.625
435 0x00 : -10.000 : 90.000
438 static int ldo_set_trimming(struct regulator_dev *rdev, int def_vol, int to_vol,
441 struct sci_regulator_desc *desc = __get_desc(rdev);
442 const struct sci_regulator_regs *regs = desc->regs;
445 /* FIXME: always update voltage ctrl bits */
448 rdev->desc->ops->set_voltage(rdev, to_vol * 1000, to_vol * 1000, 0);
449 if (IS_ERR_VALUE(ret) && regs->vol_ctl)
453 u32 trim = /* assert 5 valid trim bits, R = V_IDEAL / V_ADCIN - 1 */
454 DIV_ROUND_UP((to_vol * 100 - adc_vol * 90) * 32,
456 if (trim > BIT(5) - 1)
458 debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
459 regs, desc->desc.name, to_vol, adc_vol,
460 (trim * 20 / 32 - 10), trim);
462 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
463 ANA_REG_SET(regs->vol_trm,
464 trim << __ffs(regs->vol_trm_bits),
474 static int dcdcldo_set_trimming(struct regulator_dev *rdev, int def_vol,
475 int to_vol, int adc_vol)
477 struct sci_regulator_desc *desc = __get_desc(rdev);
478 const struct sci_regulator_regs *regs = desc->regs;
482 u32 trim = desc->ops->trimming_def_val;
483 if (adc_vol > to_vol) {
485 ((adc_vol - to_vol) * 100 * 32) / (adc_vol * 25);
488 DIV_ROUND_UP((to_vol - adc_vol) * 100 * 32,
491 if (trim > BIT(5) - 1)
493 debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
494 regs, desc->desc.name, to_vol, adc_vol,
495 ((int)trim - 0x10) * 25 / 32, trim);
497 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
498 ANA_REG_SET(regs->vol_trm,
499 trim << __ffs(regs->vol_trm_bits),
509 static int ldo_get_trimming_step(struct regulator_dev *rdev, int to_vol)
511 return 1000 * to_vol * 20 / 32; /*uV */
514 static int dcdcldo_get_trimming_step(struct regulator_dev *rdev, int to_vol)
516 return 1000 * to_vol * 25 / 32; /*uV */
519 /* FIXME: patch for sc7710 BA version */
520 #ifdef CONFIG_ARCH_SC7710
522 * lpref trimming step about 1.39%, range -22.22% ~ +20.83%. that all maps as follow.
523 0x0F : +20.833 : 120.833
524 0x0E : +19.444 : 119.444
525 0x0D : +18.056 : 118.056
526 0x0C : +16.667 : 116.667
527 0x0B : +15.278 : 115.278
528 0x0A : +13.889 : 113.889
529 0x09 : +12.500 : 112.500
530 0x08 : +11.111 : 111.111
531 0x07 : +9.722 : 109.722
532 0x06 : +8.333 : 108.333
533 0x05 : +6.944 : 106.944
534 0x04 : +5.556 : 105.556
535 0x03 : +4.167 : 104.167
536 0x02 : +2.778 : 102.778
537 0x01 : +1.389 : 101.389
538 0x00 : +0.000 : 100.000
539 0x1F : -1.389 : 98.611
540 0x1E : -2.778 : 97.222
541 0x1D : -4.167 : 95.833
542 0x1C : -5.556 : 94.444
543 0x1B : -6.944 : 93.056
544 0x1A : -8.333 : 91.667
545 0x19 : -9.722 : 90.278
546 0x18 : -11.111 : 88.889
547 0x17 : -12.500 : 87.500
548 0x16 : -13.889 : 86.111
549 0x15 : -15.278 : 84.722
550 0x14 : -16.667 : 83.333
551 0x13 : -18.056 : 81.944
552 0x12 : -19.444 : 80.556
553 0x11 : -20.833 : 79.167
554 0x10 : -22.222 : 77.778
556 static int lpref_set_trimming(struct regulator_dev *rdev, int def_vol,
557 int to_vol, int adc_vol)
559 struct sci_regulator_desc *desc = __get_desc(rdev);
560 const struct sci_regulator_regs *regs = desc->regs;
562 u32 set_bits, rst_bits;
564 u32 trim = /* assert 5 valid trim bits, R = V_IDEAL / V_ADCIN - 1 */
565 (abs(to_vol - adc_vol) * 72 / adc_vol);
567 if (to_vol < adc_vol)
569 if (trim < BIT(5) - 1)
570 trim++; /* FIXME: a little higher, according asic */
571 else if (trim > BIT(5) - 1)
574 debug("regu %p (%s) trimming %d = %d %+d%%, got [%02X]\n",
575 regs, desc->desc.name, to_vol, adc_vol,
576 (100 - adc_vol * 100 / to_vol), trim);
578 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
579 mutex_lock(&adc_chan_mutex);
580 ANA_REG_BIC(ANA_REG_GLB_LDO_SW, BIT_WPA_DCDC_SEL);
581 switch (regs->cal_ctl_bits >> 16) {
582 case 0x1a: //abb/rf/amp/emmcore/emmio/vddwif0/vddwif1
583 set_bits = (trim >> 0) & (BIT(0) | BIT(1));
584 ANA_REG_SET(ANA_REG_GLB_CHGR_CTRL0, set_bits << 8,
586 set_bits = (trim >> 2) & (BIT(0) | BIT(1));
587 rst_bits = ~set_bits & (BIT(0) | BIT(1));
588 ANA_REG_SET(ANA_REG_GLB_DCDC_MEM_CTRL2,
589 set_bits << 10 | rst_bits << 14,
590 BIT(10) | BIT(11) | BIT(14) | BIT(15));
591 set_bits = (trim >> 4) & BIT(0);
592 rst_bits = ~set_bits & BIT(0);
593 ANA_REG_SET(ANA_REG_GLB_WPA_DCDC_CTRL1,
594 set_bits << 10 | rst_bits << 14, BIT(10) | BIT(14));
596 case 0x19: //vdd18/vdd28/vdd25/vddcama/vddcamd/vddmem/vddusb
598 ANA_REG_SET(ANA_REG_GLB_LDO_TRIM6, trim << 8,
599 BIT(8) | BIT(9) | BIT(10) | BIT(11) | BIT(12));
601 case 0x18: //sdio/sim0/sim1/sim2
602 set_bits = (trim >> 0) & (BIT(0) | BIT(1));
603 rst_bits = ~set_bits & (BIT(0) | BIT(1));
604 ANA_REG_SET(ANA_REG_GLB_DCDC_ARM_CTRL2,
605 set_bits << 10 | rst_bits << 14,
606 BIT(10) | BIT(11) | BIT(14) | BIT(15));
607 set_bits = (trim >> 2) & (BIT(0) | BIT(1));
608 rst_bits = ~set_bits & (BIT(0) | BIT(1));
609 ANA_REG_SET(ANA_REG_GLB_DCDC_CORE_CTRL2,
610 set_bits << 10 | rst_bits << 14,
611 BIT(10) | BIT(11) | BIT(14) | BIT(15));
612 set_bits = (trim >> 4) & BIT(0);
613 rst_bits = ~set_bits & BIT(0);
614 ANA_REG_SET(ANA_REG_GLB_WPA_DCDC_CTRL1,
615 set_bits << 11 | rst_bits << 15, BIT(11) | BIT(15));
621 msleep(1); /* FIXME: wait for lpref voltage is okay */
622 ANA_REG_OR(ANA_REG_GLB_LDO_SW, BIT_WPA_DCDC_SEL);
623 mutex_unlock(&adc_chan_mutex);
631 static int lpref_get_trimming_step(struct regulator_dev *rdev, int to_vol)
633 return 1000 * to_vol * 100 / 72; /*uV */
637 /* standard dcdc ops*/
638 #define BITS_DCDC_CAL_RST(_x_) ( (_x_) << 5 & (BIT(5)|BIT(6)|BIT(7)|BIT(8)|BIT(9)) )
639 #define BITS_DCDC_CAL(_x_) ( (_x_) << 0 & (BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4)) )
640 static int dcdc_get_trimming_step(struct regulator_dev *rdev, int to_vol)
642 struct sci_regulator_desc *desc = __get_desc(rdev);
643 if (0 == strcmp(desc->desc.name, "vddmem")) { /* FIXME: vddmem step 200/32mV */
644 return 1000 * 200 / 32; /*uV */
646 return 1000 * 100 / 32; /*uV */
649 static int dcdc_set_trimming(struct regulator_dev *rdev,
650 int def_vol, int to_vol, int adc_vol)
652 struct sci_regulator_desc *desc = __get_desc(rdev);
653 int acc_vol = desc->ops->get_trimming_step(rdev, to_vol);
655 * FIXME: no need division?
656 int ctl_vol = DIV_ROUND_UP(def_vol * to_vol * 1000, adc_vol) + acc_vol;
658 int ctl_vol = 1000 * (to_vol - (adc_vol - def_vol)) + acc_vol; /*uV */
660 /* FIXME: dcdc core ctrl should be keeped after trimming.
661 * but now, uV_offset is used for dcdc set/get correct voltage API.
663 rdev->constraints->uV_offset = ctl_vol - to_vol * 1000;
664 debug("regu (%s) ctl %d to %d, offset %dmv\n", desc->desc.name,
665 ctl_vol / 1000, to_vol, rdev->constraints->uV_offset / 1000);
666 return rdev->desc->ops->set_voltage(rdev, ctl_vol, ctl_vol, 0);
669 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
672 int ds, min_ds = 100; /* mV, the max range of small voltage */
673 for (i = 0; i < regs->vol_sel_cnt; i++) {
674 ds = vol - regs->vol_sel[i];
675 if (ds >= 0 && ds < min_ds) {
683 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
685 int vol = rdev->desc->ops->get_voltage(rdev) / 1000;
687 /* FIXME: for dcdc, each step (50mV) takes 10us */
688 int dly = (vol - old_vol) * 10 / 50;
695 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
696 int max_uV, unsigned *selector)
698 struct sci_regulator_desc *desc = __get_desc(rdev);
699 const struct sci_regulator_regs *regs = desc->regs;
700 int i, mv = min_uV / 1000;
701 int old_vol = rdev->desc->ops->get_voltage(rdev) / 1000;
703 debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
705 BUG_ON(0 != __ffs(regs->vol_trm_bits));
706 BUG_ON(regs->vol_sel_cnt > 8);
711 /* found the closely vol ctrl bits */
712 i = __match_dcdc_vol(regs, mv);
715 "not found %s closely ctrl bits for %dmV\n",
716 desc->desc.name, mv);
718 debug2("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
719 mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
721 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
722 /* dcdc calibration control bits (default 00000),
723 * small adjust voltage: 100/32mv ~= 3.125mv
726 int shft = __ffs(regs->vol_ctl_bits);
727 int max = regs->vol_ctl_bits >> shft;
728 int j = (mv - regs->vol_sel[i]) * 1000 /
729 desc->ops->get_trimming_step(rdev, mv) % 32;
731 if (regs->vol_trm == regs->vol_ctl) { /* new feature */
732 ANA_REG_SET(regs->vol_ctl, j | (i << shft),
733 regs->vol_trm_bits | regs->vol_ctl_bits);
735 if (regs->vol_trm) { /* small adjust first */
736 ANA_REG_SET(regs->vol_trm,
738 BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1)
742 ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
746 __dcdc_enable_time(rdev, old_vol);
752 /** CONFIG_ARCH_SCX35
754 bonding option 4 dcdc_wrf_ctl[2]
756 bonding option 2 dcdc_mem_ctl[2]
757 bonding option 1 dcdc_mem_ctl[1]
760 static int dcdc_get_voltage(struct regulator_dev *rdev)
762 struct sci_regulator_desc *desc =
763 (struct sci_regulator_desc *)rdev->desc;
764 const struct sci_regulator_regs *regs = desc->regs;
766 int cal = 0; /* uV */
767 int i, shft = __ffs(regs->vol_ctl_bits);
769 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
770 regs, desc->desc.name, regs->vol_ctl,
771 shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
776 BUG_ON(0 != __ffs(regs->vol_trm_bits));
777 BUG_ON(regs->vol_sel_cnt > 8);
779 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
781 mv = regs->vol_sel[i];
784 /*check the reset relative bit of vol ctl */
785 if (regs->vol_trm != regs->vol_ctl) {
787 (~ANA_REG_GET(regs->vol_ctl) &
788 (regs->vol_ctl_bits << 4)) >> 4;
791 #if defined(CONFIG_ARCH_SC7710)
792 BUG_ON(0 != __ffs(regs->vol_ctl_bits));
794 && regs->vol_ctl_bits == vol_bits),
795 "the reset relative ctrl bits of %s is invalid, %x",
797 ANA_REG_GET(regs->vol_ctl));
799 vol_bits = ANA_REG_GET(regs->vol_trm);
800 i = vol_bits & regs->vol_trm_bits;
801 vol_bits = (~vol_bits
802 & (regs->vol_trm_bits << 5)) >> 5;
805 && regs->vol_trm_bits == vol_bits),
806 "the reset relative cal ctrl bits of %s is invalid, %x",
808 ANA_REG_GET(regs->vol_trm));
810 /* FIXME: correct default value */
811 if (0 == strcmp(desc->desc.name, "vddarm"))
819 cal = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits)
820 * desc->ops->get_trimming_step(rdev, mv); /*uV */
823 debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv,
825 return mv * 1000 + cal;
828 #if defined(CONFIG_ARCH_SC7710)
829 /* vddmem trimming: -100mv ~ +100 mV, step 6.25mV */
830 static int vmem_get_voltage(struct regulator_dev *rdev)
832 struct sci_regulator_desc *desc =
833 (struct sci_regulator_desc *)rdev->desc;
834 const struct sci_regulator_regs *regs = desc->regs;
836 int cal = 0; /* uV */
837 int i, j, shft = __ffs(regs->vol_ctl_bits);
839 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
840 regs, desc->desc.name, regs->vol_ctl,
841 shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
843 BUG_ON(0 != __ffs(regs->vol_trm_bits));
844 BUG_ON(regs->vol_sel_cnt > 8);
846 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
847 j = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits);
850 (~ANA_REG_GET(regs->vol_ctl) & (regs->vol_ctl_bits << 4)) >> 4;
855 cal = (j - 0x10) * desc->ops->get_trimming_step(rdev, regs->vol_sel[i]); /*uV */
857 debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name,
858 regs->vol_sel[i], cal / 1000);
859 return regs->vol_sel[i] * 1000 + cal;
862 static int vmem_set_voltage(struct regulator_dev *rdev, int min_uV,
863 int max_uV, unsigned *selector)
865 struct sci_regulator_desc *desc = __get_desc(rdev);
866 const struct sci_regulator_regs *regs = desc->regs;
867 int i = 0, j, ctl_vol = min_uV, def_vol, acc_vol;
868 int shft = __ffs(regs->vol_ctl_bits);
869 int max = regs->vol_ctl_bits >> shft;
871 debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
873 def_vol = regs->vol_sel[i] * 1000; /*FIXME: fixed vmem@1.8v */
874 acc_vol = desc->ops->get_trimming_step(rdev, 0); /*uV */
876 j = (ctl_vol - def_vol + acc_vol * 0x10) / acc_vol;
877 if (j >= 0 && j < 32) {
878 debug("regu %p (%s) %d = %d %+dmv\n", regs,
879 desc->desc.name, ctl_vol / 1000, def_vol / 1000,
880 (j - 0x10) * acc_vol / 1000);
882 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
883 ANA_REG_SET(regs->vol_trm,
885 BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1) - j), -1);
887 ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
892 "not found %s closely ctrl bits for %dmV\n",
893 desc->desc.name, ctl_vol / 1000);
897 /* standard boost ops*/
898 #define MAX_CURRENT_SINK (500) /*FIXME: max current sink */
899 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
902 struct sci_regulator_desc *desc = __get_desc(rdev);
903 const struct sci_regulator_regs *regs = desc->regs;
904 int ma = min_uA / 1000;
906 int i, shft = __ffs(regs->vol_ctl_bits);
907 int trim = (int)regs->vol_def / 1000;
908 int steps = (regs->vol_ctl_bits >> shft) + 1;
910 debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
916 trim <<= __ffs(regs->vol_trm_bits);
919 i = ma * steps / MAX_CURRENT_SINK;
920 if (i >= 0 && i < steps) {
921 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
922 regs->vol_ctl_bits | regs->vol_trm_bits);
928 "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
934 static int boost_get_current_limit(struct regulator_dev *rdev)
936 struct sci_regulator_desc *desc = __get_desc(rdev);
937 const struct sci_regulator_regs *regs = desc->regs;
939 int i, shft = __ffs(regs->vol_ctl_bits);
940 int steps = (regs->vol_ctl_bits >> shft) + 1;
942 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
943 regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
948 i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
949 cur = i * MAX_CURRENT_SINK / steps;
950 debug2("regu %p (%s), current %d\n", regs, desc->desc.name, cur);
954 static int adc_sample_bit = 1; /*12bits mode */
955 static short adc_data[3][2]
956 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
958 {4200, 3320}, /* same as nv adc_t */
960 {400, 316}, /* 0.4@VBAT, Reserved IdealC Value */
965 static int __is_valid_adc_cal(void)
967 return 0 != adc_data[0][0];
970 static int __init __adc_cal_setup(char *str)
972 u32 *p = (u32 *) adc_data;
973 *p = simple_strtoul(str, &str, 0);
974 if (*p++ && *++str) {
975 *p = simple_strtoul(str, &str, 0);
977 debug("%d : %d -- %d : %d\n",
978 (int)adc_data[0][0], (int)adc_data[0][1],
979 (int)adc_data[1][0], (int)adc_data[1][1]);
980 if (adc_data[0][1] < BIT(10)
981 && adc_data[1][1] < BIT(10))
982 adc_sample_bit = 0; /*10bits mode */
985 * update adc data from kernel parameter,
986 * and compensate 6~12mV if need.
995 early_param("adc_cal", __adc_cal_setup);
997 static int __init __adc_cal_fuse_setup(void)
999 if (!__is_valid_adc_cal() &&
1000 sci_efuse_calibration_get((u32 *) adc_data)) {
1001 debug("%d : %d -- %d : %d\n",
1002 (int)adc_data[0][0], (int)adc_data[0][1],
1003 (int)adc_data[1][0], (int)adc_data[1][1]);
1008 static int __adc2vbat(int adc_res)
1010 int t = adc_data[0][0] - adc_data[1][0];
1011 t *= (adc_res - adc_data[0][1]);
1012 t /= (adc_data[0][1] - adc_data[1][1]);
1013 t += adc_data[0][0];
1017 #define MEASURE_TIMES (15)
1019 static void __dump_adc_result(u32 adc_val[])
1021 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
1023 for (i = 0; i < MEASURE_TIMES; i++) {
1024 printk("%d ", adc_val[i]);
1030 static int cmp_val(const void *a, const void *b)
1032 return *(int *)a - *(int *)b;
1036 * __adc_voltage - get regulator output voltage through auxadc
1037 * @regulator: regulator source
1039 * This returns the current regulator voltage in mV.
1041 * NOTE: If the regulator is disabled it will return the voltage value. This
1042 * function should not be used to determine regulator state.
1044 static int regu_adc_voltage(struct regulator_dev *rdev)
1046 struct sci_regulator_desc *desc = __get_desc(rdev);
1047 const struct sci_regulator_regs *regs = desc->regs;
1049 int ret, adc_chan = regs->cal_ctl_bits >> 16;
1050 u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
1051 u32 adc_res, adc_val[MEASURE_TIMES];
1052 u32 chan_numerators = 1, chan_denominators = 1;
1053 u32 bat_numerators, bat_denominators;
1055 struct adc_sample_data data = {
1056 .channel_id = adc_chan,
1057 .channel_type = 0, /*sw */
1058 .hw_channel_delay = 0, /*reserved */
1059 .scale = 1, /*big scale */
1060 .pbuf = &adc_val[0],
1061 .sample_num = MEASURE_TIMES,
1062 .sample_bits = adc_sample_bit,
1063 .sample_speed = 0, /*quick mode */
1064 .signal_mode = 0, /*resistance path */
1067 if (!__is_valid_adc_cal())
1073 /* enable ldo cal before adc sampling and ldo calibration */
1074 if (0 == regs->typ) {
1075 mutex_lock(&adc_chan_mutex);
1076 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
1077 debug0("%s adc channel %d : %04x\n",
1078 desc->desc.name, data.channel_id, ldo_cal_sel);
1081 ret = sci_adc_get_values(&data);
1084 /* close ldo cal and release multiplexed aux adc channel */
1085 if (0 == regs->typ) {
1086 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
1087 mutex_unlock(&adc_chan_mutex);
1090 __dump_adc_result(adc_val);
1091 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
1092 /*__dump_adc_result(adc_val);*/
1094 sci_adc_get_vol_ratio(data.channel_id, data.scale,
1095 &chan_numerators, &chan_denominators);
1097 #ifdef CONFIG_ARCH_SCX35
1098 if (0 == strcmp(desc->desc.name, "vddcamio")) { /* FIXME: others is 1/2 */
1099 chan_numerators = 1;
1100 chan_denominators = 3;
1101 } else if (0 == strcmp(desc->desc.name, "vddwrf")) { /* FIXME: bonding options? */
1102 chan_numerators = 1;
1103 chan_denominators = 3;
1107 sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators,
1110 adc_res = adc_val[MEASURE_TIMES / 2];
1111 debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
1112 desc->desc.name, data.channel_id, ldo_cal_sel,
1113 chan_numerators, chan_denominators, adc_res);
1118 return __adc2vbat(adc_res)
1119 * (bat_numerators * chan_denominators)
1120 / (bat_denominators * chan_numerators);
1123 static void do_regu_work(struct work_struct *w)
1125 struct sci_regulator_data *data =
1126 container_of(w, struct sci_regulator_data, dwork.work);
1127 struct sci_regulator_desc *desc = __get_desc(data->rdev);
1128 debug0("%s\n", desc->desc.name);
1129 if (!__is_trimming(data->rdev)) {
1130 mutex_lock(&data->rdev->mutex);
1131 desc->ops->calibrate(data->rdev, 0, 0);
1132 mutex_unlock(&data->rdev->mutex);
1136 int __regu_calibrate(struct regulator_dev *rdev, int def_vol, int to_vol)
1138 struct sci_regulator_desc *desc = __get_desc(rdev);
1139 const struct sci_regulator_regs *regs = desc->regs;
1140 int in_calibration(void);
1141 if (in_calibration() || !__is_valid_adc_cal()
1142 || !regs->vol_def || !regs->cal_ctl || !regs->vol_trm) {
1143 /* FIXME: BYPASS if in CFT or not adc cal or no cal ctl
1149 schedule_delayed_work(&desc->data.dwork, msecs_to_jiffies(10));
1154 * FIXME: ASSERT dcdc/ldo is enabled
1156 static int regu_calibrate(struct regulator_dev *rdev, int def_vol, int to_vol)
1158 struct sci_regulator_desc *desc = __get_desc(rdev);
1159 const struct sci_regulator_regs *regs = desc->regs;
1160 int ret = 0, retry_count = 1;
1161 int adc_vol = 0, ctl_vol, cal_vol = 0;
1164 ctl_vol = rdev->desc->ops->get_voltage(rdev);
1165 if (IS_ERR_VALUE(ctl_vol)) {
1166 debug0("no valid %s vol ctrl bits\n", desc->desc.name);
1167 } else /* dcdc/ldo maybe had been adjusted or opened in uboot-spl */
1171 def_vol = (IS_ERR_VALUE(ctl_vol)) ? regs->vol_def : ctl_vol;
1174 to_vol = (IS_ERR_VALUE(ctl_vol)) ? regs->vol_def : ctl_vol;
1176 /* FIXME: Ideal voltage maybe not chip default which in the choice */
1177 if (to_vol != regs->vol_def) {
1178 int i = __match_dcdc_vol(regs, regs->vol_def);
1179 if (i >= 0 && regs->vol_sel[i] != regs->vol_def)
1180 to_vol = regs->vol_def;
1184 adc_vol = regu_adc_voltage(rdev);
1186 debug("%s default %dmv, maybe not enable\n",
1187 desc->desc.name, def_vol);
1191 cal_vol = abs(adc_vol - to_vol);
1192 debug("%s default %dmv, from %dmv to %dmv, bias %c%d.%03d%%\n",
1193 desc->desc.name, def_vol, adc_vol, to_vol,
1194 (adc_vol > to_vol) ? '+' : '-',
1195 cal_vol * 100 / adc_vol, cal_vol * 100 * 1000 / adc_vol % 1000);
1197 if (!def_vol || !to_vol || adc_vol <= 0)
1200 if (abs(adc_vol - def_vol) >= def_vol / 9) /* adjust limit 10% */
1202 else if (cal_vol < to_vol / 100) { /* bias 1% */
1204 * FIXME: cal_vol * 100 / adc_vol <= 1 is okay for ldo lpref
1206 set_bit(desc->desc.id, trimming_state);
1207 debug("%s is okay\n", desc->desc.name);
1209 } else if (0 == retry_count--) {
1210 /* FIXME: unfortunately, dcdc/ldo need calibrate again */
1211 WARN(1, "%s try again\n", desc->desc.name);
1215 ret = desc->ops->set_trimming(rdev, def_vol, to_vol, adc_vol);
1216 if (IS_ERR_VALUE(ret))
1219 def_vol = 0; /*force reacquire */
1220 set_bit(desc->desc.id, trimming_state); /*force set before verify */
1221 msleep(REGU_VERIFY_DLY); /* wait a moment before cal verify */
1225 debug("%s failure\n", desc->desc.name);
1229 static int regu_force_trimming(struct regulator_dev *rdev, int trim)
1231 struct sci_regulator_desc *desc = __get_desc(rdev);
1232 const struct sci_regulator_regs *regs = desc->regs;
1235 ANA_REG_SET(regs->vol_trm,
1236 trim << __ffs(regs->vol_trm_bits),
1237 regs->vol_trm_bits);
1242 * regulator_strongly_disable - strongly disable regulator output
1243 * @regulator: regulator source
1245 * Strongly try disable the regulator output voltage or current.
1246 * NOTE: this *will* disable the regulator output even if other consumer
1247 * devices have it enabled. This should be used for situations when device
1248 * had unbalanced with calls to regulator_enable().
1249 * *Not* recommended to call this function before try to balance the use_count.
1251 int regulator_strongly_disable(struct regulator *regulator)
1253 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
1257 while (rdev->use_count--)
1258 regulator_disable(regulator);
1263 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
1266 * regulator_calibrate - force calibrate the regulator to the ideal value
1267 * @regulator: regulator source
1270 int regulator_calibrate(struct regulator *regulator, int to_vol)
1272 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
1276 struct sci_regulator_desc *desc = __get_desc(rdev);
1277 if (desc && desc->ops) {
1278 mutex_lock(&rdev->mutex);
1279 ret = desc->ops->calibrate(rdev, 0, to_vol);
1280 mutex_unlock(&rdev->mutex);
1287 EXPORT_SYMBOL_GPL(regulator_calibrate);
1289 static struct regulator_ops ldo_ops = {
1290 .enable = ldo_turn_on,
1291 .disable = ldo_turn_off,
1292 .is_enabled = ldo_is_on,
1293 .set_voltage = ldo_set_voltage,
1294 .get_voltage = ldo_get_voltage,
1295 .set_mode = ldo_set_mode,
1296 /* .enable_time = ldo_enable_time, */
1299 static struct regulator_ops usbd_ops = {
1300 .enable = 0, /* reserved d-die ldo */
1301 .disable = 0, /* reserved d-die ldo */
1302 .is_enabled = 0, /* reserved d-die ldo */
1305 static struct regulator_ops dcdc_ops = {
1306 .enable = ldo_turn_on,
1307 .disable = ldo_turn_off,
1308 .is_enabled = ldo_is_on,
1309 .set_voltage = dcdc_set_voltage,
1310 .get_voltage = dcdc_get_voltage,
1313 static struct regulator_ops boost_ops = {
1314 .enable = ldo_turn_on,
1315 .disable = ldo_turn_off,
1316 .is_enabled = ldo_is_on,
1317 .set_current_limit = boost_set_current_limit,
1318 .get_current_limit = boost_get_current_limit,
1319 .set_mode = ldo_set_mode,
1322 static struct sci_regulator_ops sci_ldo_ops = {
1323 .trimming_def_val = 0x10, /* 100% */
1324 .get_trimming_step = ldo_get_trimming_step,
1325 .set_trimming = ldo_set_trimming,
1326 .calibrate = regu_calibrate,
1329 static struct sci_regulator_ops sci_dcdcldo_ops = {
1330 .trimming_def_val = 0x10, /* 100% */
1331 .get_trimming_step = dcdcldo_get_trimming_step,
1332 .set_trimming = dcdcldo_set_trimming,
1333 .calibrate = regu_calibrate,
1336 static struct sci_regulator_ops sci_dcdc_ops = {
1337 .get_trimming_step = dcdc_get_trimming_step,
1338 .set_trimming = dcdc_set_trimming,
1339 .calibrate = regu_calibrate,
1343 * Consider the following machine :-
1345 * Regulator-1 -+-> [Consumer A @ 1.8V]
1347 * +-> [Consumer B @ 1.8V]
1349 * Regulator-2 ---> [Consumer C @ 3.3V]
1351 * The drivers for consumers A & B must be mapped to the correct regulator in
1352 * order to control their power supply. This mapping can be achieved in board/machine
1353 * initialisation code by creating a struct regulator_consumer_supply for each regulator.
1354 * Alternatively, we built a regulator supply-consumers map, the format is as follow:
1356 * supply source-1, consumer A, consumer B, ..., NULL
1357 * supply source-2, consumer C, ..., NULL
1362 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
1363 const char *supply_name,
1366 char **map = (char **)dev_get_platdata(dev);
1368 struct regulator_consumer_supply *consumer_supplies = NULL;
1370 if (!supply_name || !(map && map[0]))
1373 for (i = 0; map[i] || map[i + 1]; i++) {
1374 if (map[i] && 0 == strcmp(map[i], supply_name))
1378 /* i++; *//* Do not skip supply name */
1380 for (n = 0; map[i + n]; n++) ;
1383 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
1385 kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
1386 BUG_ON(!consumer_supplies);
1387 for (n = 0; map[i]; i++, n++) {
1388 consumer_supplies[n].supply = map[i];
1393 return consumer_supplies;
1396 #if defined(CONFIG_DEBUG_FS)
1397 static struct dentry *debugfs_root = NULL;
1399 static u32 ana_addr = 0;
1400 static int debugfs_ana_addr_get(void *data, u64 * val)
1402 if (ana_addr < PAGE_SIZE) {
1403 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
1405 void *addr = ioremap(ana_addr, PAGE_SIZE);
1406 *val = __raw_readl(addr);
1412 static int debugfs_ana_addr_set(void *data, u64 val)
1414 if (ana_addr < PAGE_SIZE) {
1415 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val,
1418 void *addr = ioremap(ana_addr, PAGE_SIZE);
1419 __raw_writel(val, addr);
1425 static int adc_chan = 5 /*VBAT*/;
1426 static int debugfs_adc_chan_get(void *pdata, u64 * val)
1429 u32 adc_res, adc_val[MEASURE_TIMES];
1430 struct adc_sample_data data = {
1431 .channel_id = adc_chan,
1432 .channel_type = 0, /*sw */
1433 .hw_channel_delay = 0, /*reserved */
1434 .scale = 1, /*big scale */
1435 .pbuf = &adc_val[0],
1436 .sample_num = MEASURE_TIMES,
1437 .sample_bits = adc_sample_bit,
1438 .sample_speed = 0, /*quick mode */
1439 .signal_mode = 0, /*resistance path */
1442 ret = sci_adc_get_values(&data);
1445 for (i = 0; i < MEASURE_TIMES; i++) {
1446 printk("%d ", adc_val[i]);
1450 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
1451 adc_res = adc_val[MEASURE_TIMES / 2];
1452 pr_info("adc chan %d, result value %d, vbat %d\n",
1453 data.channel_id, adc_res, __adc2vbat(adc_res));
1458 static int debugfs_adc_chan_set(void *data, u64 val)
1464 static int debugfs_enable_get(void *data, u64 * val)
1466 struct regulator_dev *rdev = data;
1467 if (rdev && rdev->desc->ops->is_enabled)
1468 *val = rdev->desc->ops->is_enabled(rdev);
1474 static int debugfs_enable_set(void *data, u64 val)
1476 struct regulator_dev *rdev = data;
1477 if (rdev && rdev->desc->ops->enable)
1478 (val) ? rdev->desc->ops->enable(rdev)
1479 : rdev->desc->ops->disable(rdev);
1483 static int debugfs_voltage_get(void *data, u64 * val)
1485 struct regulator_dev *rdev = data;
1487 *val = regu_adc_voltage(rdev);
1493 static int debugfs_ldo_set(void *data, u64 val)
1495 struct regulator_dev *rdev = data;
1496 if (rdev && rdev->desc->ops->set_voltage) {
1497 if (val < 200) /* FIXME: debug force trimming */
1498 regu_force_trimming(rdev, val);
1500 rdev->desc->ops->set_voltage(rdev, val * 1000,
1506 static int debugfs_dcdc_set(void *data, u64 val)
1508 struct regulator_dev *rdev = data;
1509 struct sci_regulator_desc *desc;
1512 desc = __get_desc(rdev);
1513 if (val < 200) /* FIXME: debug force trimming */
1514 regu_force_trimming(rdev, val);
1515 else if (desc && desc->ops) {
1516 mutex_lock(&rdev->mutex);
1517 desc->ops->calibrate(rdev, 0, val);
1518 mutex_unlock(&rdev->mutex);
1524 static int debugfs_boost_get(void *data, u64 * val)
1526 struct regulator_dev *rdev = data;
1527 if (rdev && rdev->desc->ops->get_current_limit)
1528 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
1534 static int debugfs_boost_set(void *data, u64 val)
1536 struct regulator_dev *rdev = data;
1537 if (rdev && rdev->desc->ops->set_current_limit)
1538 rdev->desc->ops->set_current_limit(rdev, val * 1000,
1543 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
1544 debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
1545 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
1546 debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
1547 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
1548 debugfs_enable_get, debugfs_enable_set, "%llu\n");
1549 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
1550 debugfs_voltage_get, debugfs_ldo_set, "%llu\n");
1551 DEFINE_SIMPLE_ATTRIBUTE(fops_dcdc,
1552 debugfs_voltage_get, debugfs_dcdc_set, "%llu\n");
1553 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
1554 debugfs_boost_get, debugfs_boost_set, "%llu\n");
1556 static void rdev_init_debugfs(struct regulator_dev *rdev)
1558 struct sci_regulator_desc *desc = __get_desc(rdev);
1559 desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
1560 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
1561 pr_warn("Failed to create debugfs directory\n");
1562 rdev->debugfs = NULL;
1566 debugfs_create_file("enable", S_IRUGO | S_IWUSR,
1567 desc->debugfs, rdev, &fops_enable);
1569 if (desc->desc.type == REGULATOR_CURRENT)
1570 debugfs_create_file("current", S_IRUGO | S_IWUSR,
1571 desc->debugfs, rdev, &fops_boost);
1573 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
1574 desc->debugfs, rdev,
1576 desc->regs->typ) ? &fops_ldo : &fops_dcdc);
1579 static void rdev_init_debugfs(struct regulator_dev *rdev)
1585 #include <linux/of.h>
1586 #define reg_info(format, arg...) pr_info("reg: " "@@@%s: " format, __func__, ## arg)
1589 static inline int __strcmp(const char *cs, const char *ct)
1591 if (!cs || !ct) return -1;
1592 return strcmp(cs, ct);
1595 void * sci_regulator_register(struct platform_device *pdev,
1596 struct sci_regulator_desc *desc)
1598 static atomic_t idx = ATOMIC_INIT(1); /* 0: dummy */
1599 struct regulator_dev *rdev;
1600 struct regulator_config config = {};
1601 struct regulator_ops *__regs_ops[] = {
1602 &ldo_ops, &usbd_ops, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops,
1605 struct sci_regulator_ops *__sci_regs_ops[] = {
1606 &sci_ldo_ops, 0, &sci_dcdc_ops, 0, 0,
1608 struct regulator_consumer_supply consumer_supplies_default[] = {
1610 .supply = desc->desc.name,
1614 struct regulator_init_data init_data = {
1615 .supply_regulator = 0,
1618 .max_uV = 4200 * 1000,
1620 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
1622 REGULATOR_CHANGE_STATUS |
1623 REGULATOR_CHANGE_VOLTAGE |
1624 REGULATOR_CHANGE_MODE,
1626 .num_consumer_supplies = 1,
1627 .consumer_supplies = consumer_supplies_default,
1628 .regulator_init = 0,
1632 struct regulator_init_data *init_data;
1633 struct device_node *dev_np;
1634 struct device_node *node_np;
1635 dev_np = pdev->dev.of_node;
1636 node_np = of_get_child_by_name(dev_np, desc->desc.name);
1637 init_data = of_get_regulator_init_data(&pdev->dev, node_np);
1638 if(!init_data || 0 != __strcmp(init_data->constraints.name, desc->desc.name)){
1639 dev_err(&pdev->dev, "out of memory or %s not found\n", desc->desc.name);
1642 reg_info("[%d]%s range %d - %d\n", idx.counter, init_data->constraints.name,
1643 init_data->constraints.min_uV, init_data->constraints.max_uV);
1645 init_data->supply_regulator = 0;
1646 init_data->constraints.min_uV = 0,
1647 init_data->constraints.max_uV = 4200 * 1000;
1648 init_data->constraints.valid_modes_mask =
1649 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1650 init_data->constraints.valid_ops_mask = REGULATOR_CHANGE_MODE |
1651 REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE;
1652 init_data->num_consumer_supplies = 1;
1653 init_data->consumer_supplies = consumer_supplies_default;
1656 desc->desc.id = atomic_inc_return(&idx) - 1;
1658 BUG_ON(desc->regs->pd_set
1659 && desc->regs->pd_set == desc->regs->pd_rst
1660 && desc->regs->pd_set_bit == desc->regs->pd_rst_bit);
1663 desc->ops = __sci_regs_ops[desc->regs->typ];
1665 BUG_ON(desc->regs->typ >= ARRAY_SIZE(__regs_ops));
1666 if (!desc->desc.ops)
1667 desc->desc.ops = __regs_ops[desc->regs->typ];
1669 #ifdef CONFIG_ARCH_SCX35
1670 if (desc->regs->typ == VDD_TYP_BOOST) { /*FIXME: reconfig current sink */
1672 init_data.constraints.min_uA = 0;
1673 init_data.constraints.max_uA = MAX_CURRENT_SINK * 1000;
1674 init_data.constraints.valid_ops_mask |=
1675 REGULATOR_CHANGE_CURRENT;
1677 init_data->constraints.min_uA = 0;
1678 init_data->constraints.max_uA = MAX_CURRENT_SINK * 1000;
1679 init_data->constraints.valid_ops_mask |=
1680 REGULATOR_CHANGE_CURRENT;
1682 desc->desc.type = REGULATOR_CURRENT;
1685 if (desc->regs->typ == VDD_TYP_LDO) { /*FIXME: reconfig dcdcldo ops */
1686 if ((desc->regs->cal_ctl_bits & 0xFFFF0000) ==
1687 (BIT(17) | BIT(18) | BIT(20))) {
1688 desc->ops = &sci_dcdcldo_ops;
1693 /* FIXME: patch for sc7710 BA version */
1694 #ifdef CONFIG_ARCH_SC7710
1695 if (sci_get_ana_chip_id() == ANA_CHIP_ID_BA &&
1696 desc->regs->typ == VDD_TYP_LPREF) {
1697 static struct sci_regulator_ops sci_lpref_ops = {
1698 .get_trimming_step = lpref_get_trimming_step,
1699 .set_trimming = lpref_set_trimming,
1700 .calibrate = regu_calibrate,
1703 BUG_ON(VDD_TYP_LPREF != 3);
1704 sci_lpref_ops.trimming_def_val = sci_ldo_ops.trimming_def_val;
1705 desc->ops = &sci_lpref_ops;
1706 desc->desc.ops = &ldo_ops;
1709 if (0 == strcmp(desc->desc.name, "vddmem")) {
1710 static struct sci_regulator_ops sci_vmem_ops = {
1711 .get_trimming_step = dcdc_get_trimming_step,
1712 .set_trimming = dcdc_set_trimming,
1713 .calibrate = regu_calibrate,
1715 static struct regulator_ops vmem_ops = {
1716 .enable = ldo_turn_on,
1717 .disable = ldo_turn_off,
1718 .is_enabled = ldo_is_on,
1719 .set_voltage = vmem_set_voltage,
1720 .get_voltage = vmem_get_voltage,
1723 if (sci_get_ana_chip_id() == ANA_CHIP_ID_BA) {
1724 desc->ops = &sci_vmem_ops;
1725 desc->desc.ops = &vmem_ops;
1727 desc->desc.ops = 0; /*FIXME: reserved for board v1.1.0 */
1732 init_data.consumer_supplies =
1733 set_supply_map(&pdev->dev, desc->desc.name,
1734 &init_data.num_consumer_supplies);
1736 if (!init_data.consumer_supplies)
1737 init_data.consumer_supplies = consumer_supplies_default;
1739 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1740 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1741 config.dev = &pdev->dev;
1742 config.init_data = &init_data;
1743 config.driver_data = 0;
1744 rdev = regulator_register(&desc->desc, &config);
1745 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1746 rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0, 0);
1748 rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0);
1750 if (init_data.consumer_supplies != consumer_supplies_default)
1751 kfree(init_data.consumer_supplies);
1752 #else /* CONFIG_OF */
1753 init_data->consumer_supplies =
1754 set_supply_map(&pdev->dev, desc->desc.name,
1755 &init_data->num_consumer_supplies);
1757 if (!init_data->consumer_supplies)
1758 init_data->consumer_supplies = consumer_supplies_default;
1760 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1761 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1762 config.dev = &pdev->dev;
1763 config.init_data = init_data;
1764 config.of_node = node_np;
1765 config.driver_data = 0;
1766 rdev = regulator_register(&desc->desc, &config);
1767 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1768 rdev = regulator_register(&desc->desc, &pdev->dev, init_data, 0, 0);
1770 rdev = regulator_register(&desc->desc, &pdev->dev, init_data, 0);
1772 if (init_data->consumer_supplies != consumer_supplies_default)
1773 kfree(init_data->consumer_supplies);
1774 #endif /* end of CONFIG_OF */
1776 if (!IS_ERR(rdev)) {
1777 rdev->reg_data = rdev;
1778 INIT_DELAYED_WORK(&desc->data.dwork, do_regu_work);
1779 desc->data.rdev = rdev;
1780 __init_trimming(rdev);
1781 rdev_init_debugfs(rdev);
1788 * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1789 * so i autogen all regulators non-variable description in plat or mach directory,
1790 * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1791 * just like other regulator vendor drivers.
1793 static int sci_regulator_probe(struct platform_device *pdev)
1795 #ifdef CONFIG_DEBUG_FS
1797 debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1798 if (IS_ERR(debugfs_root) || !debugfs_root) {
1800 "%s: Failed to create debugfs directory\n", REGULATOR_ROOT_DIR);
1801 debugfs_root = NULL;
1804 debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1805 debugfs_root, (u32 *) & ana_addr);
1806 debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1807 debugfs_root, &ana_addr, &fops_ana_addr);
1808 debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1809 debugfs_root, &adc_chan, &fops_adc_chan);
1810 debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1811 debugfs_root, (u64 *) & adc_data);
1813 {/* vddarm/vddcore/vddmem common debugfs interface */
1815 struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1816 sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1817 debugfs_create_symlink("dcdcarm", vol_root, str);
1818 sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1819 debugfs_create_symlink("dcdccore", vol_root, str);
1820 sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1821 debugfs_create_symlink("dcdcmem", vol_root, str);
1825 pr_info("sc271x ana chip id: (0x%08x), bond opt (0x%08x)\n",
1826 (sci_get_ana_chip_id() | sci_get_ana_chip_ver()),
1827 ANA_REG_GET(ANA_REG_GLB_ANA_STATUS));
1829 #include CONFIG_REGULATOR_SPRD_MAP
1834 static struct of_device_id sprd_regulator_of_match[] = {
1835 { .compatible = "sprd,sc2713-regulator", },
1840 static struct platform_driver sci_regulator_driver = {
1842 .name = "sc2713-regulator",
1843 .owner = THIS_MODULE,
1844 .of_match_table = of_match_ptr(sprd_regulator_of_match),
1846 .probe = sci_regulator_probe,
1849 static int __init regu_driver_init(void)
1851 __adc_cal_fuse_setup();
1853 return platform_driver_register(&sci_regulator_driver);
1856 int __init sci_regulator_init(void)
1859 static struct platform_device regulator_device = {
1860 .name = "sc2713-regulator",
1863 static struct platform_device sc2713s_regulator_device = {
1864 .name = "sc2713s-regulator",
1868 if (sci_get_ana_chip_id() == 0x2713C000) /* SC2713S */
1869 return platform_device_register(&sc2713s_regulator_device);
1871 return platform_device_register(®ulator_device);
1873 return of_platform_populate(of_find_node_by_path("/sprd-regulators"),
1874 sprd_regulator_of_match, NULL, NULL);
1878 subsys_initcall(regu_driver_init);
1880 MODULE_LICENSE("GPL v2");
1881 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1882 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");
1883 MODULE_VERSION("0.4");