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.
15 * 0.2 unprotect dcdc/ldo before turn on and off
16 * remove dcdc/ldo calibration
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>
32 #include <linux/platform_device.h>
34 #include <linux/of_device.h>
35 #include <linux/regulator/of_regulator.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/regulator/driver.h>
39 #include <linux/regulator/machine.h>
41 #include <mach/hardware.h>
43 #include <mach/sci_glb_regs.h>
46 #include <mach/arch_misc.h>
48 #define REGULATOR_ROOT_DIR "sprd-regulator"
51 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
52 #define debug0(format, arg...) //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
53 #define debug2(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
56 #define ANA_REG_OR(_r, _b) sci_adi_write(_r, _b, 0)
60 #define ANA_REG_BIC(_r, _b) sci_adi_write(_r, 0, _b)
64 #define ANA_REG_GET(_r) sci_adi_read(_r)
68 #define ANA_REG_SET(_r, _v, _m) sci_adi_write((_r), ((_v) & (_m)), (_m))
71 struct sci_regulator_regs {
73 u32 pd_set, pd_set_bit;
75 * at new feature, some LDOs had only set, no rst bits.
76 * and DCDCs voltage and trimming controller is the same register
78 u32 pd_rst, pd_rst_bit;
79 u32 slp_ctl, slp_ctl_bit;
80 u32 vol_trm, vol_trm_bits;
81 u32 cal_ctl, cal_ctl_bits;
83 u32 vol_ctl, vol_ctl_bits;
84 u32 vol_sel_cnt, vol_sel[];
87 struct sci_regulator_data {
88 struct regulator_dev *rdev;
91 struct sci_regulator_desc {
92 struct regulator_desc desc;
93 struct sci_regulator_ops *ops;
94 struct sci_regulator_regs *regs;
95 struct sci_regulator_data data; /* FIXME: dynamic */
96 #if defined(CONFIG_DEBUG_FS)
97 struct dentry *debugfs;
109 #define REGU_VERIFY_DLY (1000) /*ms */
111 static u16 ana_status_bondopt4;
112 static u32 ana_chip_id;
114 static DEFINE_MUTEX(adc_chan_mutex);
115 extern int sci_efuse_calibration_get(u32 * p_cal_data);
117 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
118 VOL_TRM, VOL_TRM_BITS, CAL_CTL, CAL_CTL_BITS, VOL_DEF, \
119 VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...) \
121 static struct sci_regulator_regs REGS_##VDD = { \
124 .pd_set_bit = SET_BIT, \
126 .pd_rst_bit = RST_BIT, \
127 .slp_ctl = SLP_CTL, \
128 .slp_ctl_bit = SLP_CTL_BIT, \
129 .vol_trm = VOL_TRM, \
130 .vol_trm_bits = VOL_TRM_BITS, \
131 .cal_ctl = CAL_CTL, \
132 .cal_ctl_bits = CAL_CTL_BITS, \
133 .vol_def = VOL_DEF, \
134 .vol_ctl = VOL_CTL, \
135 .vol_ctl_bits = VOL_CTL_BITS, \
136 .vol_sel_cnt = VOL_SEL_CNT, \
137 .vol_sel = {__VA_ARGS__}, \
139 static struct sci_regulator_desc DESC_##VDD = { \
143 .desc.type = REGULATOR_VOLTAGE, \
144 .desc.owner = THIS_MODULE, \
145 .regs = ®S_##VDD, \
147 sci_regulator_register(pdev, &DESC_##VDD); \
150 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
152 return (struct sci_regulator_desc *)rdev->desc;
155 /* standard ldo ops*/
156 static int ldo_turn_on(struct regulator_dev *rdev)
158 struct sci_regulator_desc *desc = __get_desc(rdev);
159 const struct sci_regulator_regs *regs = desc->regs;
161 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
162 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
163 regs->pd_rst, __ffs(regs->pd_rst_bit));
165 if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
166 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
167 BITS_PWR_WR_PROT_VALUE(0x6e7f));
170 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
173 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
175 if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
176 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
178 debug2("regu %p (%s), turn on\n", regs, desc->desc.name);
182 static int ldo_turn_off(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));
191 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
192 if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
193 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
194 BITS_PWR_WR_PROT_VALUE(0x6e7f));
197 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
200 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
202 if (regs->pd_set == ANA_REG_GLB_LDO_DCDC_PD)
203 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
207 debug2("regu %p (%s), turn off\n", regs, desc->desc.name);
211 static int ldo_is_on(struct regulator_dev *rdev)
214 struct sci_regulator_desc *desc = __get_desc(rdev);
215 const struct sci_regulator_regs *regs = desc->regs;
217 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
218 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
219 regs->pd_rst, __ffs(regs->pd_rst_bit));
221 if (regs->pd_rst && regs->pd_set) {
222 /*for pd_rst has higher prioty than pd_set, what's more, their reset values are the same, 0 */
223 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
224 /* FIXME: when reset, pd_set & pd_rst are all zero, always get here */
225 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
227 } else if (regs->pd_rst) {
228 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
229 } else if (regs->pd_set) { /* new feature */
230 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
233 debug2("regu %p (%s) return %d\n", regs, desc->desc.name, ret);
237 #if 0 /* FIXME: todo later */
238 static int ldo_enable_time(struct regulator_dev *rdev)
240 return 1000 * 1; /*Microseconds */
244 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
246 struct sci_regulator_desc *desc = __get_desc(rdev);
247 const struct sci_regulator_regs *regs = desc->regs;
249 debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
250 regs->slp_ctl, regs->slp_ctl_bit, mode);
255 if (mode == REGULATOR_MODE_STANDBY) { /* disable auto slp */
256 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
258 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
263 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
264 int max_uV, unsigned *selector)
266 struct sci_regulator_desc *desc = __get_desc(rdev);
267 const struct sci_regulator_regs *regs = desc->regs;
268 int mv = min_uV / 1000;
271 debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
273 if (!regs->vol_ctl && regs->vol_trm && regs->vol_sel_cnt == 2) {
274 int shft = __ffs(regs->vol_trm_bits);
275 u32 trim = /* vol_sel[0]: output (mV), [1]:step (uV) */
276 DIV_ROUND_UP((mv - regs->vol_sel[0]) * 1000,
279 ret = trim > (regs->vol_trm_bits >> shft);
281 "warning: regulator (%s) not support %dmV\n",
282 desc->desc.name, mv);
285 ANA_REG_SET(regs->vol_trm,
286 trim << __ffs(regs->vol_trm_bits),
295 static int ldo_get_voltage(struct regulator_dev *rdev)
297 struct sci_regulator_desc *desc = __get_desc(rdev);
298 const struct sci_regulator_regs *regs = desc->regs;
301 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
302 regs, desc->desc.name, regs->vol_ctl, 0, regs->vol_ctl_bits);
304 if (!regs->vol_ctl && regs->vol_trm && regs->vol_sel_cnt == 2) {
305 int shft = __ffs(regs->vol_trm_bits);
307 (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft;
308 vol = regs->vol_sel[0] * 1000 + trim * regs->vol_sel[1];
309 debug2("regu %p (%s), voltage %d\n", regs, desc->desc.name,
317 /* FIXME: get dcdc cal offset config from uboot */
318 #define DCDC_CAL_CONF_BASE (SPRD_IRAM0_BASE + 0x1f00)
319 #define DCDC_MAX_CNT (4)
330 #define PP_VOL_PARA ( 0x50005c20 ) /* assert in iram2 */
331 #define TO_IRAM2(_p_) ( SPRD_IRAM2_BASE + (u32)(_p_) - SPRD_IRAM2_PHYS )
332 #define IN_IRAM2(_p_) ( (u32)(_p_) >= SPRD_IRAM2_PHYS && (u32)(_p_) < SPRD_IRAM2_PHYS + SPRD_IRAM2_SIZE )
334 int regulator_default_get(const char con_id[])
337 vol_para_t *pvol_para = (vol_para_t *)__raw_readl((void *)TO_IRAM2(PP_VOL_PARA));
339 if (!(IN_IRAM2(pvol_para)))
342 pvol_para = (vol_para_t *)TO_IRAM2(pvol_para);
344 if(strcmp((pvol_para)[0].name, "volpara_begin") || (0xfaed != (pvol_para)[0].ideal_vol))
347 while(0 != strcmp((pvol_para)[i++].name, "volpara_end")) {
348 if (0 == strcmp((pvol_para)[i].name, con_id)) {
349 debug("%s name %s, ideal_vol %d\n", __func__, (pvol_para)[i].name, (pvol_para)[i].ideal_vol);
350 return res = (pvol_para)[i].ideal_vol;
356 static int __init_trimming(struct regulator_dev *rdev)
358 struct sci_regulator_desc *desc = __get_desc(rdev);
359 const struct sci_regulator_regs *regs = desc->regs;
365 to_vol = regulator_default_get(desc->desc.name);
367 to_vol = regs->vol_def;
370 if (to_vol && rdev->desc->ops->get_voltage) {
371 ctl_vol = rdev->desc->ops->get_voltage(rdev);
372 rdev->constraints->uV_offset = ctl_vol - to_vol * 1000;//uV
373 debug("regu %p (%s), uV offset %d\n", regs, desc->desc.name, rdev->constraints->uV_offset);
379 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
382 int ds, min_ds = 100; /* mV, the max range of small voltage */
383 for (i = 0; i < regs->vol_sel_cnt; i++) {
384 ds = vol - regs->vol_sel[i];
385 if (ds >= 0 && ds < min_ds) {
393 static int dcdc_get_trimming_step(struct regulator_dev *rdev, int to_vol)
395 struct sci_regulator_desc *desc = __get_desc(rdev);
397 /* FIXME: vddmem step 200/32mV */
398 return (0 == strcmp(desc->desc.name, "vddmem") ) ? (1000 * 200 / 32) : (1000 * 100 / 32) /*uV */;
401 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
403 int vol = rdev->desc->ops->get_voltage(rdev) / 1000;
405 /* FIXME: for dcdc, each step (50mV) takes 10us */
406 int dly = (vol - old_vol) * 10 / 50;
413 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
414 int max_uV, unsigned *selector)
416 struct sci_regulator_desc *desc = __get_desc(rdev);
417 const struct sci_regulator_regs *regs = desc->regs;
418 int i, mv = min_uV / 1000;
419 int old_vol = rdev->desc->ops->get_voltage(rdev) / 1000;
421 debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
423 BUG_ON(0 != __ffs(regs->vol_trm_bits));
424 BUG_ON(regs->vol_sel_cnt > 8);
429 /* found the closely vol ctrl bits */
430 i = __match_dcdc_vol(regs, mv);
433 "not found %s closely ctrl bits for %dmV\n",
434 desc->desc.name, mv);
436 debug2("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
437 mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
439 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
440 /* dcdc calibration control bits (default 00000),
441 * small adjust voltage: 100/32mv ~= 3.125mv
444 int shft = __ffs(regs->vol_ctl_bits);
445 int j = (int)(mv - regs->vol_sel[i]) * 1000 / dcdc_get_trimming_step(rdev, 0);
447 if (0 == strcmp(desc->desc.name, "vddmem") )
450 BUG_ON(j > (regs->vol_trm_bits >> __ffs(regs->vol_trm_bits)));
452 if (regs->vol_trm == regs->vol_ctl) { /* new feature */
453 ANA_REG_SET(regs->vol_ctl, j | (i << shft),
454 regs->vol_trm_bits | regs->vol_ctl_bits);
460 __dcdc_enable_time(rdev, old_vol);
466 /** CONFIG_ARCH_SCX35
468 bonding option 4 dcdc_wrf_ctl[2]
470 bonding option 2 dcdc_mem_ctl[2]
471 bonding option 1 dcdc_mem_ctl[1]
474 static int dcdc_get_voltage(struct regulator_dev *rdev)
476 struct sci_regulator_desc *desc =
477 (struct sci_regulator_desc *)rdev->desc;
478 const struct sci_regulator_regs *regs = desc->regs;
480 int cal = 0; /* uV */
481 int i, shft = __ffs(regs->vol_ctl_bits);
483 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
484 regs, desc->desc.name, regs->vol_ctl,
485 shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
490 BUG_ON(0 != __ffs(regs->vol_trm_bits));
491 BUG_ON(regs->vol_sel_cnt > 8);
493 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft;
495 mv = regs->vol_sel[i];
498 /*check the reset relative bit of vol ctl */
499 if (regs->vol_trm != regs->vol_ctl) {
501 (~ANA_REG_GET(regs->vol_ctl) &
502 (regs->vol_ctl_bits << 4)) >> 4;
509 cal = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits);
510 if (0 == strcmp(desc->desc.name, "vddmem") )
513 cal *= dcdc_get_trimming_step(rdev, 0); /*uV */
516 debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv, cal / 1000);
518 return (mv * 1000 + cal) /*uV */;
521 /* standard boost ops*/
522 #define MAX_CURRENT_SINK (500) /*FIXME: max current sink */
523 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
526 struct sci_regulator_desc *desc = __get_desc(rdev);
527 const struct sci_regulator_regs *regs = desc->regs;
528 int ma = min_uA / 1000;
530 int i, shft = __ffs(regs->vol_ctl_bits);
531 int trim = (int)regs->vol_def / 1000;
532 int steps = (regs->vol_ctl_bits >> shft) + 1;
534 debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
540 trim <<= __ffs(regs->vol_trm_bits);
543 i = ma * steps / MAX_CURRENT_SINK;
544 if (i >= 0 && i < steps) {
545 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
546 regs->vol_ctl_bits | regs->vol_trm_bits);
552 "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
558 static int boost_get_current_limit(struct regulator_dev *rdev)
560 struct sci_regulator_desc *desc = __get_desc(rdev);
561 const struct sci_regulator_regs *regs = desc->regs;
563 int i, shft = __ffs(regs->vol_ctl_bits);
564 int steps = (regs->vol_ctl_bits >> shft) + 1;
566 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
567 regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
572 i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
573 cur = i * MAX_CURRENT_SINK / steps;
574 debug2("regu %p (%s), current %d\n", regs, desc->desc.name, cur);
578 static int adc_sample_bit = 1; /*12bits mode */
579 static short adc_data[3][2]
580 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
582 {4200, 3320}, /* same as nv adc_t */
584 {400, 316}, /* 0.4@VBAT, Reserved IdealC Value */
589 static int __is_valid_adc_cal(void)
591 return 0 != adc_data[0][0];
594 static int __init __adc_cal_setup(char *str)
596 u32 *p = (u32 *) adc_data;
597 *p = simple_strtoul(str, &str, 0);
598 if (*p++ && *++str) {
599 *p = simple_strtoul(str, &str, 0);
601 debug("%d : %d -- %d : %d\n",
602 (int)adc_data[0][0], (int)adc_data[0][1],
603 (int)adc_data[1][0], (int)adc_data[1][1]);
604 if (adc_data[0][1] < BIT(10)
605 && adc_data[1][1] < BIT(10))
606 adc_sample_bit = 0; /*10bits mode */
611 early_param("adc_cal", __adc_cal_setup);
613 static int __init __adc_cal_fuse_setup(void)
615 if (!__is_valid_adc_cal() &&
616 sci_efuse_calibration_get((u32 *) adc_data)) {
617 debug("%d : %d -- %d : %d\n",
618 (int)adc_data[0][0], (int)adc_data[0][1],
619 (int)adc_data[1][0], (int)adc_data[1][1]);
625 static int __adc2vbat(int adc_res)
627 int t = adc_data[0][0] - adc_data[1][0];
628 t *= (adc_res - adc_data[0][1]);
629 t /= (adc_data[0][1] - adc_data[1][1]);
634 #define MEASURE_TIMES (15)
636 static void __dump_adc_result(u32 adc_val[])
638 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
640 for (i = 0; i < MEASURE_TIMES; i++) {
641 printk("%d ", adc_val[i]);
647 static int cmp_val(const void *a, const void *b)
649 return *(int *)a - *(int *)b;
653 * __adc_voltage - get regulator output voltage through auxadc
654 * @regulator: regulator source
656 * This returns the current regulator voltage in mV.
658 * NOTE: If the regulator is disabled it will return the voltage value. This
659 * function should not be used to determine regulator state.
661 static int regu_adc_voltage(struct regulator_dev *rdev)
663 struct sci_regulator_desc *desc = __get_desc(rdev);
664 const struct sci_regulator_regs *regs = desc->regs;
666 int ret, adc_chan = regs->cal_ctl_bits >> 16;
667 u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
668 u32 adc_res, adc_val[MEASURE_TIMES];
669 u32 chan_numerators = 1, chan_denominators = 1;
670 u32 bat_numerators, bat_denominators;
672 struct adc_sample_data data = {
673 .channel_id = adc_chan,
674 .channel_type = 0, /*sw */
675 .hw_channel_delay = 0, /*reserved */
676 .scale = 1, /*big scale */
678 .sample_num = MEASURE_TIMES,
679 .sample_bits = adc_sample_bit,
680 .sample_speed = 0, /*quick mode */
681 .signal_mode = 0, /*resistance path */
684 if (!__is_valid_adc_cal())
690 /* enable ldo cal before adc sampling and ldo calibration */
691 if (0 == regs->typ) {
692 mutex_lock(&adc_chan_mutex);
693 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
694 debug0("%s adc channel %d : %04x\n",
695 desc->desc.name, data.channel_id, ldo_cal_sel);
698 ret = sci_adc_get_values(&data);
701 /* close ldo cal and release multiplexed aux adc channel */
702 if (0 == regs->typ) {
703 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
704 mutex_unlock(&adc_chan_mutex);
707 __dump_adc_result(adc_val);
708 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
709 /*__dump_adc_result(adc_val);*/
711 sci_adc_get_vol_ratio(data.channel_id, data.scale,
712 &chan_numerators, &chan_denominators);
714 sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators,
717 adc_res = adc_val[MEASURE_TIMES / 2];
718 debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
719 desc->desc.name, data.channel_id, ldo_cal_sel,
720 chan_numerators, chan_denominators, adc_res);
725 return __adc2vbat(adc_res)
726 * (bat_numerators * chan_denominators)
727 / (bat_denominators * chan_numerators);
731 * regulator_strongly_disable - strongly disable regulator output
732 * @regulator: regulator source
734 * Strongly try disable the regulator output voltage or current.
735 * NOTE: this *will* disable the regulator output even if other consumer
736 * devices have it enabled. This should be used for situations when device
737 * had unbalanced with calls to regulator_enable().
738 * *Not* recommended to call this function before try to balance the use_count.
740 int regulator_strongly_disable(struct regulator *regulator)
742 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
746 while (rdev->use_count--)
747 regulator_disable(regulator);
752 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
754 static struct regulator_ops ldo_ops = {
755 .enable = ldo_turn_on,
756 .disable = ldo_turn_off,
757 .is_enabled = ldo_is_on,
758 .set_voltage = ldo_set_voltage,
759 .get_voltage = ldo_get_voltage,
760 .set_mode = ldo_set_mode,
761 /* .enable_time = ldo_enable_time, */
764 static struct regulator_ops dcdc_ops = {
765 .enable = ldo_turn_on,
766 .disable = ldo_turn_off,
767 .is_enabled = ldo_is_on,
768 .set_voltage = dcdc_set_voltage,
769 .get_voltage = dcdc_get_voltage,
772 static struct regulator_ops boost_ops = {
773 .enable = ldo_turn_on,
774 .disable = ldo_turn_off,
775 .is_enabled = ldo_is_on,
776 .set_current_limit = boost_set_current_limit,
777 .get_current_limit = boost_get_current_limit,
778 .set_mode = ldo_set_mode,
782 * Consider the following machine :-
784 * Regulator-1 -+-> [Consumer A @ 1.8V]
786 * +-> [Consumer B @ 1.8V]
788 * Regulator-2 ---> [Consumer C @ 3.3V]
790 * The drivers for consumers A & B must be mapped to the correct regulator in
791 * order to control their power supply. This mapping can be achieved in board/machine
792 * initialisation code by creating a struct regulator_consumer_supply for each regulator.
793 * Alternatively, we built a regulator supply-consumers map, the format is as follow:
795 * supply source-1, consumer A, consumer B, ..., NULL
796 * supply source-2, consumer C, ..., NULL
801 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
802 const char *supply_name,
805 char **map = (char **)dev_get_platdata(dev);
807 struct regulator_consumer_supply *consumer_supplies = NULL;
809 if (!supply_name || !(map && map[0]))
812 for (i = 0; map[i] || map[i + 1]; i++) {
813 if (map[i] && 0 == strcmp(map[i], supply_name))
817 /* i++; *//* Do not skip supply name */
819 for (n = 0; map[i + n]; n++) ;
822 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
824 kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
825 BUG_ON(!consumer_supplies);
826 for (n = 0; map[i]; i++, n++) {
827 consumer_supplies[n].supply = map[i];
832 return consumer_supplies;
835 #if defined(CONFIG_DEBUG_FS)
836 static struct dentry *debugfs_root = NULL;
838 static u32 ana_addr = 0;
839 static int debugfs_ana_addr_get(void *data, u64 * val)
841 if (ana_addr < PAGE_SIZE) {
842 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
844 void *addr = ioremap(ana_addr, PAGE_SIZE);
845 *val = __raw_readl(addr);
851 static int debugfs_ana_addr_set(void *data, u64 val)
853 if (ana_addr < PAGE_SIZE) {
854 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val,
857 void *addr = ioremap(ana_addr, PAGE_SIZE);
858 __raw_writel(val, addr);
864 static int adc_chan = 5 /*VBAT*/;
865 static int debugfs_adc_chan_get(void *pdata, u64 * val)
868 u32 adc_res, adc_val[MEASURE_TIMES];
869 struct adc_sample_data data = {
870 .channel_id = adc_chan,
871 .channel_type = 0, /*sw */
872 .hw_channel_delay = 0, /*reserved */
873 .scale = 1, /*big scale */
875 .sample_num = MEASURE_TIMES,
876 .sample_bits = adc_sample_bit,
877 .sample_speed = 0, /*quick mode */
878 .signal_mode = 0, /*resistance path */
881 ret = sci_adc_get_values(&data);
884 for (i = 0; i < MEASURE_TIMES; i++) {
885 printk("%d ", adc_val[i]);
889 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
890 adc_res = adc_val[MEASURE_TIMES / 2];
891 pr_info("adc chan %d, result value %d, vbat %d\n",
892 data.channel_id, adc_res, __adc2vbat(adc_res));
897 static int debugfs_adc_chan_set(void *data, u64 val)
903 static int debugfs_enable_get(void *data, u64 * val)
905 struct regulator_dev *rdev = data;
906 if (rdev && rdev->desc->ops->is_enabled)
907 *val = rdev->desc->ops->is_enabled(rdev);
913 static int debugfs_enable_set(void *data, u64 val)
915 struct regulator_dev *rdev = data;
916 if (rdev && rdev->desc->ops->enable)
917 (val) ? rdev->desc->ops->enable(rdev)
918 : rdev->desc->ops->disable(rdev);
922 static int debugfs_voltage_get(void *data, u64 * val)
924 struct regulator_dev *rdev = data;
926 *val = regu_adc_voltage(rdev);
932 static int debugfs_voltage_set(void *data, u64 val)
934 struct regulator_dev *rdev = data;
935 if (rdev && rdev->desc->ops->set_voltage) {
936 u32 min_uV = (u32)val * 1000;
937 min_uV += rdev->constraints->uV_offset;
938 rdev->desc->ops->set_voltage(rdev, min_uV, min_uV, 0);
943 static int debugfs_boost_get(void *data, u64 * val)
945 struct regulator_dev *rdev = data;
946 if (rdev && rdev->desc->ops->get_current_limit)
947 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
953 static int debugfs_boost_set(void *data, u64 val)
955 struct regulator_dev *rdev = data;
956 if (rdev && rdev->desc->ops->set_current_limit)
957 rdev->desc->ops->set_current_limit(rdev, val * 1000,
962 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
963 debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
964 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
965 debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
966 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
967 debugfs_enable_get, debugfs_enable_set, "%llu\n");
968 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
969 debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
970 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
971 debugfs_boost_get, debugfs_boost_set, "%llu\n");
973 static void rdev_init_debugfs(struct regulator_dev *rdev)
975 struct sci_regulator_desc *desc = __get_desc(rdev);
976 desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
977 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
978 pr_warn("Failed to create debugfs directory\n");
979 rdev->debugfs = NULL;
983 debugfs_create_file("enable", S_IRUGO | S_IWUSR,
984 desc->debugfs, rdev, &fops_enable);
986 if (desc->desc.type == REGULATOR_CURRENT)
987 debugfs_create_file("current", S_IRUGO | S_IWUSR,
988 desc->debugfs, rdev, &fops_boost);
990 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
991 desc->debugfs, rdev, &fops_ldo);
994 static void rdev_init_debugfs(struct regulator_dev *rdev)
1000 #include <linux/of.h>
1001 #define reg_info(format, arg...) pr_info("reg: " "@@@%s: " format, __func__, ## arg)
1004 static inline int __strcmp(const char *cs, const char *ct)
1006 if (!cs || !ct) return -1;
1007 return strcmp(cs, ct);
1010 void *sci_regulator_register(struct platform_device *pdev,
1011 struct sci_regulator_desc *desc)
1013 static atomic_t idx = ATOMIC_INIT(1); /* 0: dummy */
1014 struct regulator_dev *rdev;
1015 struct regulator_ops *__regs_ops[] = {
1016 &ldo_ops, 0, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops,
1019 struct regulator_consumer_supply consumer_supplies_default[] = {
1021 .supply = desc->desc.name,
1025 struct regulator_init_data init_data = {
1026 .supply_regulator = 0,
1029 .max_uV = 4200 * 1000,
1031 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
1033 REGULATOR_CHANGE_STATUS |
1034 REGULATOR_CHANGE_VOLTAGE |
1035 REGULATOR_CHANGE_MODE,
1037 .num_consumer_supplies = 1,
1038 .consumer_supplies = consumer_supplies_default,
1039 .regulator_init = 0,
1043 struct regulator_config config = { };
1045 if (sci_ana_chip_ver_is_compatible(SC2711AA)) {
1046 if (0 == strcmp(desc->desc.name, "vdd18") ||
1047 0 == strcmp(desc->desc.name, "vddemmcio") ||
1048 0 == strcmp(desc->desc.name, "vddcamd") ||
1049 0 == strcmp(desc->desc.name, "vddcamio")) {
1051 desc->regs->vol_sel[0] = 1400;
1052 debug("%s vol base %d\n", desc->desc.name, desc->regs->vol_sel[0]);
1057 struct regulator_config config = { };
1058 struct regulator_init_data *init_data;
1059 struct device_node *dev_np;
1060 struct device_node *node_np;
1061 dev_np = pdev->dev.of_node;
1062 node_np = of_get_child_by_name(dev_np, desc->desc.name);
1063 init_data = of_get_regulator_init_data(&pdev->dev, node_np);
1064 if(!init_data || 0 != __strcmp(init_data->constraints.name, desc->desc.name)){
1065 dev_err(&pdev->dev, "out of memory or %s not found\n", desc->desc.name);
1068 reg_info("[%d]%s range %d - %d\n", idx.counter, init_data->constraints.name,
1069 init_data->constraints.min_uV, init_data->constraints.max_uV);
1071 init_data->supply_regulator = 0;
1072 init_data->constraints.min_uV = 0,
1073 init_data->constraints.max_uV = 4200 * 1000;
1074 init_data->constraints.valid_modes_mask =
1075 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1076 init_data->constraints.valid_ops_mask = REGULATOR_CHANGE_MODE |
1077 REGULATOR_CHANGE_STATUS | REGULATOR_CHANGE_VOLTAGE;
1078 init_data->num_consumer_supplies = 1;
1079 init_data->consumer_supplies = consumer_supplies_default;
1082 desc->desc.id = atomic_inc_return(&idx) - 1;
1084 if (0 == strcmp(desc->desc.name, "vddrf0")) {
1085 if (ana_status_bondopt4) { /* BONDOPT4 */
1086 desc->regs->vol_def = 2800;
1088 desc->regs->vol_def = 1800;
1092 BUG_ON(desc->regs->pd_set
1093 && desc->regs->pd_set == desc->regs->pd_rst
1094 && desc->regs->pd_set_bit == desc->regs->pd_rst_bit);
1096 BUG_ON(desc->regs->typ >= ARRAY_SIZE(__regs_ops));
1097 if (!desc->desc.ops)
1098 desc->desc.ops = __regs_ops[desc->regs->typ];
1101 init_data.consumer_supplies =
1102 set_supply_map(&pdev->dev, desc->desc.name,
1103 &init_data.num_consumer_supplies);
1105 if (!init_data.consumer_supplies)
1106 init_data.consumer_supplies = consumer_supplies_default;
1108 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1109 config.dev = &pdev->dev;
1110 config.init_data = &init_data;
1111 config.driver_data = NULL;
1112 config.of_node = NULL;
1113 rdev = regulator_register(&desc->desc, &config);
1114 if (init_data.consumer_supplies != consumer_supplies_default)
1115 kfree(init_data.consumer_supplies);
1117 init_data->consumer_supplies =
1118 set_supply_map(&pdev->dev, desc->desc.name,
1119 &init_data->num_consumer_supplies);
1121 if (!init_data->consumer_supplies)
1122 init_data->consumer_supplies = consumer_supplies_default;
1124 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1125 config.dev = &pdev->dev;
1126 config.init_data = init_data;
1127 config.driver_data = NULL;
1128 config.of_node = node_np;
1129 rdev = regulator_register(&desc->desc, &config);
1130 if (init_data->consumer_supplies != consumer_supplies_default)
1131 kfree(init_data->consumer_supplies);
1134 if (!IS_ERR(rdev)) {
1135 rdev->reg_data = rdev;
1136 desc->data.rdev = rdev;
1137 __init_trimming(rdev);
1138 rdev_init_debugfs(rdev);
1145 * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1146 * so i autogen all regulators non-variable description in plat or mach directory,
1147 * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1148 * just like other regulator vendor drivers.
1150 static int sci_regulator_probe(struct platform_device *pdev)
1152 #ifdef CONFIG_DEBUG_FS
1154 debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1155 if (IS_ERR(debugfs_root) || !debugfs_root) {
1157 "%s: Failed to create debugfs directory\n", REGULATOR_ROOT_DIR);
1158 debugfs_root = NULL;
1161 /* compatible with 8810 adc test */
1162 debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1163 debugfs_root, (u32 *) & ana_addr);
1164 debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1165 debugfs_root, &ana_addr, &fops_ana_addr);
1166 debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1167 debugfs_root, &adc_chan, &fops_adc_chan);
1168 debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1169 debugfs_root, (u64 *) & adc_data);
1171 { /* vddarm/vddcore/vddmem common debugfs interface */
1173 struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1174 sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1175 debugfs_create_symlink("dcdcarm", vol_root, str);
1176 sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1177 debugfs_create_symlink("dcdccore", vol_root, str);
1178 sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1179 debugfs_create_symlink("dcdcmem", vol_root, str);
1183 ana_chip_id = (sci_get_ana_chip_id() | sci_get_ana_chip_ver());
1184 ana_status_bondopt4 = (ANA_REG_GET(ANA_REG_GLB_LDO_V_CTRL9) >> 15) & 0x1;
1186 pr_info("sc271x ana chip id: (0x%08x), bond opt (0x%08x)\n", ana_chip_id, ana_status_bondopt4);
1188 #include CONFIG_REGULATOR_SPRD_MAP
1194 static struct of_device_id sprd_regulator_of_match[] = {
1195 { .compatible = "sprd,sc2711-regulator", },
1200 static struct platform_driver sci_regulator_driver = {
1202 .name = "sc2711-regulator",
1203 .owner = THIS_MODULE,
1204 .of_match_table = of_match_ptr(sprd_regulator_of_match),
1206 .probe = sci_regulator_probe,
1209 static int __init regu_driver_init(void)
1211 __adc_cal_fuse_setup();
1213 return platform_driver_register(&sci_regulator_driver);
1216 int __init sci_regulator_init(void)
1219 static struct platform_device regulator_device = {
1220 .name = "sc2711-regulator",
1224 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE,
1225 BITS_PWR_WR_PROT_VALUE(0x6e7f));
1227 (sci_adi_read(ANA_REG_GLB_PWR_WR_PROT_VALUE) &
1229 sci_adi_raw_write(ANA_REG_GLB_PWR_WR_PROT_VALUE, 0);
1231 __adc_cal_fuse_setup();
1232 return platform_device_register(®ulator_device);
1234 return of_platform_populate(of_find_node_by_path("/sprd-regulators"),
1235 sprd_regulator_of_match, NULL, NULL);
1239 subsys_initcall(regu_driver_init);
1241 MODULE_LICENSE("GPL v2");
1242 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1243 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");
1244 MODULE_VERSION("0.2");