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>
46 #include <linux/of_device.h>
47 #include <linux/regulator/of_regulator.h>
49 #include <linux/regulator/consumer.h>
50 #include <linux/regulator/driver.h>
51 #include <linux/regulator/machine.h>
53 #include <mach/hardware.h>
55 #include <mach/sci_glb_regs.h>
58 #include <mach/arch_misc.h>
60 #define REGULATOR_ROOT_DIR "sprd-regulator"
63 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
64 #define debug0(format, arg...) //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
65 #define debug2(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
68 #define ANA_REG_OR(_r, _b) sci_adi_write(_r, _b, 0)
72 #define ANA_REG_BIC(_r, _b) sci_adi_write(_r, 0, _b)
76 #define ANA_REG_GET(_r) sci_adi_read(_r)
80 #define ANA_REG_SET(_r, _v, _m) sci_adi_write((_r), ((_v) & (_m)), (_m))
83 struct sci_regulator_regs {
85 u32 pd_set, pd_set_bit;
87 * at new feature, some LDOs had only set, no rst bits.
88 * and DCDCs voltage and trimming controller is the same register
90 u32 pd_rst, pd_rst_bit;
91 u32 slp_ctl, slp_ctl_bit;
92 u32 vol_trm, vol_trm_bits;
93 u32 cal_ctl, cal_ctl_bits;
95 u32 vol_ctl, vol_ctl_bits;
96 u32 vol_sel_cnt, vol_sel[];
99 struct sci_regulator_data {
100 struct regulator_dev *rdev;
103 struct sci_regulator_desc {
104 struct regulator_desc desc;
105 struct sci_regulator_ops *ops;
106 struct sci_regulator_regs *regs;
107 struct sci_regulator_data data; /* FIXME: dynamic */
108 #if defined(CONFIG_DEBUG_FS)
109 struct dentry *debugfs;
121 #define REGU_VERIFY_DLY (1000) /*ms */
123 /*************************************************************************
124 Reg: 0x40038800 + 0x00E4
125 --------------------------------------------
126 BIT | FieldName | Description
127 --------------------------------------------
129 BIT6 BONDOPT6 28nm/40nm dcdccore/dcdcarm default voltage select:
130 0: dcdccore/dcdcarm = 1.1v, vdd25 = 2.5v
131 1: dcdccore/dcdcarm = 0.9v, vdd25 = 1.8v
132 BIT5 BONDOPT5 crystal 32k buffer select:
133 0: new low power 32k buffer output, 1: backup 32k buffer output
134 BIT4 BONDOPT4 dcdcwrf out voltage select: dcdc_wrf_ctl[2]
135 BIT3 BONDOPT3 charge mode option:
136 0: continues charging, 1: dcdc mode charging
137 BIT2 BONDOPT2 dcdcmem option 2: dcdc_mem_ctl[2]
138 BIT1 BONDOPT1 dcdcmem option 1: dcdc_mem_ctl[1]
143 BIT0 BONDOPT0 New power on reset option:
144 0: manual power on without hardware debounce
145 1: auto power on with 1s hardware debounce
146 **************************************************************************/
147 static u16 ana_status;
148 static u32 ana_chip_id;
150 static DEFINE_MUTEX(adc_chan_mutex);
151 extern int sci_efuse_get_cal(unsigned int * pdata, int num);
153 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
154 VOL_TRM, VOL_TRM_BITS, CAL_CTL, CAL_CTL_BITS, VOL_DEF, \
155 VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...) \
157 static struct sci_regulator_regs REGS_##VDD = { \
160 .pd_set_bit = SET_BIT, \
162 .pd_rst_bit = RST_BIT, \
163 .slp_ctl = SLP_CTL, \
164 .slp_ctl_bit = SLP_CTL_BIT, \
165 .vol_trm = VOL_TRM, \
166 .vol_trm_bits = VOL_TRM_BITS, \
167 .cal_ctl = CAL_CTL, \
168 .cal_ctl_bits = CAL_CTL_BITS, \
169 .vol_def = VOL_DEF, \
170 .vol_ctl = VOL_CTL, \
171 .vol_ctl_bits = VOL_CTL_BITS, \
172 .vol_sel_cnt = VOL_SEL_CNT, \
173 .vol_sel = {__VA_ARGS__}, \
175 static struct sci_regulator_desc DESC_##VDD = { \
179 .desc.type = REGULATOR_VOLTAGE, \
180 .desc.owner = THIS_MODULE, \
181 .regs = ®S_##VDD, \
183 sci_regulator_register(pdev, &DESC_##VDD); \
186 static struct sci_regulator_desc *__get_desc(struct regulator_dev *rdev)
188 return (struct sci_regulator_desc *)rdev->desc;
191 /* standard ldo ops*/
192 static int ldo_turn_on(struct regulator_dev *rdev)
194 struct sci_regulator_desc *desc = __get_desc(rdev);
195 struct sci_regulator_regs *regs = desc->regs;
197 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
198 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
199 regs->pd_rst, __ffs(regs->pd_rst_bit));
202 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
205 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
207 debug("regu %p (%s), turn on\n", regs, desc->desc.name);
211 static int ldo_turn_off(struct regulator_dev *rdev)
213 struct sci_regulator_desc *desc = __get_desc(rdev);
214 struct sci_regulator_regs *regs = desc->regs;
216 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
217 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
218 regs->pd_rst, __ffs(regs->pd_rst_bit));
220 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
222 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
225 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
228 debug("regu %p (%s), turn off\n", regs, desc->desc.name);
232 static int ldo_is_on(struct regulator_dev *rdev)
235 struct sci_regulator_desc *desc = __get_desc(rdev);
236 struct sci_regulator_regs *regs = desc->regs;
238 debug0("regu %p (%s), set %08x[%d], rst %08x[%d]\n", regs,
239 desc->desc.name, regs->pd_set, __ffs(regs->pd_set_bit),
240 regs->pd_rst, __ffs(regs->pd_rst_bit));
242 if (regs->pd_rst && regs->pd_set) {
243 /*for pd_rst has higher prioty than pd_set, what's more, their reset values are the same, 0 */
244 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
245 /* FIXME: when reset, pd_set & pd_rst are all zero, always get here */
246 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
248 } else if (regs->pd_rst) {
249 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
250 } else if (regs->pd_set) { /* new feature */
251 ret = !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit);
254 debug2("regu %p (%s) turn on, return %d\n", regs, desc->desc.name, ret);
258 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
260 struct sci_regulator_desc *desc = __get_desc(rdev);
261 struct sci_regulator_regs *regs = desc->regs;
263 debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
264 regs->slp_ctl, regs->slp_ctl_bit, mode);
269 if (mode == REGULATOR_MODE_STANDBY) { /* disable auto slp */
270 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
272 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
278 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
279 int max_uV, unsigned *selector)
281 struct sci_regulator_desc *desc = __get_desc(rdev);
282 struct sci_regulator_regs *regs = desc->regs;
283 int mv = min_uV / 1000;
285 int i, shft = __ffs(regs->vol_ctl_bits);
287 BUG_ON(regs->vol_sel_cnt > 4);
289 debug("regu %p (%s) set voltage, %d(uV) %d(uV)\n", regs, desc->desc.name, min_uV, max_uV);
294 for (i = 0; i < regs->vol_sel_cnt; i++) {
295 if (regs->vol_sel[i] == mv) {
296 ANA_REG_SET(regs->vol_ctl,
299 //clear_bit(desc->desc.id, trimming_state);
305 WARN(0 != ret, "warning: regulator (%s) not support %dmV\n", desc->desc.name, mv);
309 static int ldo_get_voltage(struct regulator_dev *rdev)
311 struct sci_regulator_desc *desc = __get_desc(rdev);
312 struct sci_regulator_regs *regs = desc->regs;
314 int i, shft = __ffs(regs->vol_ctl_bits);
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 i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
325 vol = regs->vol_sel[i];
327 debug2("regu %p (%s) get voltage %d\n", regs, desc->desc.name, vol);
332 /* FIXME: get dcdc cal offset config from uboot */
340 #define PP_VOL_PARA ( 0x50005C20 ) /* assert in iram2 */
341 #define TO_IRAM2(_p_) ( SPRD_IRAM2_BASE + (u32)(_p_) - SPRD_IRAM2_PHYS )
342 #define IN_IRAM2(_p_) ( (u32)(_p_) >= SPRD_IRAM2_PHYS && (u32)(_p_) < SPRD_IRAM2_PHYS + SPRD_IRAM2_SIZE )
344 int regulator_default_get(const char con_id[])
347 vol_para_t *pvol_para = (vol_para_t *)__raw_readl((void *)TO_IRAM2(PP_VOL_PARA));
349 if (!(IN_IRAM2(pvol_para)))
352 pvol_para = (vol_para_t *)TO_IRAM2(pvol_para);
354 if(strcmp((pvol_para)[0].name, "volpara_begin") || (0xfaed != (pvol_para)[0].ideal_vol))
357 while(0 != strcmp((pvol_para)[i++].name, "volpara_end")) {
358 if (0 == strcmp((pvol_para)[i].name, con_id)) {
359 debug("%s name %s, ideal_vol %d\n", __func__, (pvol_para)[i].name, (pvol_para)[i].ideal_vol);
360 return res = (pvol_para)[i].ideal_vol;
366 static int __init_trimming(struct regulator_dev *rdev)
368 struct sci_regulator_desc *desc = __get_desc(rdev);
369 const struct sci_regulator_regs *regs = desc->regs;
375 to_vol = regulator_default_get(desc->desc.name);
377 to_vol = regs->vol_def;
380 if (to_vol && rdev->desc->ops->get_voltage) {
381 ctl_vol = rdev->desc->ops->get_voltage(rdev);
382 rdev->constraints->uV_offset = ctl_vol - to_vol * 1000;//uV
383 debug("regu %p (%s), uV offset %d\n", regs, desc->desc.name, rdev->constraints->uV_offset);
389 static int dcdc_get_trimming_step(struct regulator_dev *rdev, int to_vol)
391 struct sci_regulator_desc *desc = __get_desc(rdev);
392 if ((0 == strcmp(desc->desc.name, "vddmem")) ||
393 (0 == strcmp(desc->desc.name, "vddwrf"))) { /* FIXME: vddmem/vddwrf step 200/32mV */
394 return 1000 * 200 / 32; /*uV */
396 return 1000 * 100 / 32; /*uV */
399 static int dcdc_initial_value(struct sci_regulator_desc *desc)
401 return ((0 == strcmp(desc->desc.name, "vddmem")) ? 0x10 : 0);
404 static int __match_dcdc_vol(struct sci_regulator_regs *regs, u32 vol)
407 int ds, min_ds = 100; /* mV, the max range of small voltage */
409 for (i = 0; i < regs->vol_sel_cnt; i++) {
410 ds = vol - regs->vol_sel[i];
411 if (ds >= 0 && ds < min_ds) {
418 if ((2 == regs->typ) && (j < 0)) {
419 for (i = 0; i < regs->vol_sel_cnt; i++) {
420 ds = abs(vol - regs->vol_sel[i]);
432 static int __dcdc_enable_time(struct regulator_dev *rdev, int old_vol)
434 int vol = rdev->desc->ops->get_voltage(rdev) / 1000;
436 /* FIXME: for dcdc, each step (50mV) takes 10us */
437 int dly = (vol - old_vol) * 10 / 50;
444 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
445 int max_uV, unsigned *selector)
447 struct sci_regulator_desc *desc = __get_desc(rdev);
448 struct sci_regulator_regs *regs = desc->regs;
449 int i, mv = min_uV / 1000;
450 int old_vol = rdev->desc->ops->get_voltage(rdev) / 1000;
452 debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
454 //BUG_ON(0 != __ffs(regs->vol_trm_bits));
455 BUG_ON(regs->vol_sel_cnt > 8);
460 /* found the closely vol ctrl bits */
461 i = __match_dcdc_vol(regs, mv);
463 return WARN(-EINVAL, "not found %s closely ctrl bits for %dmV\n",
464 desc->desc.name, mv);
466 debug("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
467 mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
469 #if !defined(CONFIG_REGULATOR_CAL_DUMMY)
470 /* dcdc calibration control bits (default 00000),
471 * small adjust voltage: 100/32mv ~= 3.125mv
474 int shft_ctl = __ffs(regs->vol_ctl_bits);
475 int shft_trm = __ffs(regs->vol_trm_bits);
476 int j = (int)(mv - regs->vol_sel[i]) * 1000 / dcdc_get_trimming_step(rdev, mv) % 32;
478 j += dcdc_initial_value(desc);
480 BUG_ON(j > (regs->vol_trm_bits >> shft_trm));
482 if (regs->vol_trm == regs->vol_ctl) { /* new feature */
483 ANA_REG_SET(regs->vol_ctl, (j << shft_trm) | (i << shft_ctl),
484 regs->vol_trm_bits | regs->vol_ctl_bits);
486 if (regs->vol_trm) { /* small adjust first */
487 ANA_REG_SET(regs->vol_trm, j << shft_trm,
492 ANA_REG_SET(regs->vol_ctl, i << shft_ctl,
498 __dcdc_enable_time(rdev, old_vol);
504 static int dcdc_get_voltage(struct regulator_dev *rdev)
506 struct sci_regulator_desc *desc = __get_desc(rdev);
507 struct sci_regulator_regs *regs = desc->regs;
509 int i, cal = 0 /* uV */;
510 int shft_ctl = __ffs(regs->vol_ctl_bits);
511 int shft_trm = __ffs(regs->vol_trm_bits);
513 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
514 regs, desc->desc.name, regs->vol_ctl,
515 shft_ctl, regs->vol_ctl_bits, regs->vol_sel_cnt);
520 //BUG_ON(0 != shft_trm);
521 BUG_ON(regs->vol_sel_cnt > 8);
523 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft_ctl;
525 mv = regs->vol_sel[i];
528 cal = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft_trm;
529 cal -= dcdc_initial_value(desc);
530 cal *= dcdc_get_trimming_step(rdev, 0); /*uV */
533 debug2("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv, cal / 1000);
535 return (mv * 1000 + cal) /*uV */;
539 /* standard boost ops*/
540 #define MAX_CURRENT_SINK (500) /*FIXME: max current sink */
541 static int boost_set_current_limit(struct regulator_dev *rdev, int min_uA,
544 struct sci_regulator_desc *desc = __get_desc(rdev);
545 struct sci_regulator_regs *regs = desc->regs;
546 int ma = min_uA / 1000;
548 int i, shft = __ffs(regs->vol_ctl_bits);
549 int trim = (int)regs->vol_def / 1000;
550 int steps = (regs->vol_ctl_bits >> shft) + 1;
552 debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uA, max_uA);
558 trim <<= __ffs(regs->vol_trm_bits);
561 i = ma * steps / MAX_CURRENT_SINK;
562 if (i >= 0 && i < steps) {
563 ANA_REG_SET(regs->vol_ctl, (i << shft) | trim,
564 regs->vol_ctl_bits | regs->vol_trm_bits);
570 "warning: regulator (%s) not support %dmA\n", desc->desc.name, ma);
576 static int boost_get_current_limit(struct regulator_dev *rdev)
578 struct sci_regulator_desc *desc = __get_desc(rdev);
579 struct sci_regulator_regs *regs = desc->regs;
581 int i, shft = __ffs(regs->vol_ctl_bits);
582 int steps = (regs->vol_ctl_bits >> shft) + 1;
584 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
585 regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
590 i = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
591 cur = i * MAX_CURRENT_SINK / steps;
592 debug("regu %p (%s) get current %d\n", regs, desc->desc.name, cur);
596 static int adc_sample_bit = 1; /*12bits mode */
597 static short adc_data[3][2]
598 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
600 {4200, 3320}, /* same as nv adc_t */
602 {400, 316}, /* 0.4@VBAT, Reserved IdealC Value */
607 static int __is_valid_adc_cal(void)
609 return 0 != adc_data[0][0];
612 static int __init __adc_cal_setup(char *str)
614 u32 *p = (u32 *) adc_data;
615 *p = simple_strtoul(str, &str, 0);
617 if (*p++ && *++str) {
618 *p = simple_strtoul(str, &str, 0);
620 debug("%d : %d -- %d : %d\n",
621 (int)adc_data[0][0], (int)adc_data[0][1],
622 (int)adc_data[1][0], (int)adc_data[1][1]);
623 if (adc_data[0][1] < BIT(10)
624 && adc_data[1][1] < BIT(10))
625 adc_sample_bit = 0; /*10bits mode */
630 early_param("adc_cal", __adc_cal_setup);
632 static int __init __adc_cal_fuse_setup(void)
634 if (!__is_valid_adc_cal() &&
635 (0 == sci_efuse_get_cal((u32 *) adc_data, 2))) {
636 debug("%d : %d -- %d : %d\n",
637 (int)adc_data[0][0], (int)adc_data[0][1],
638 (int)adc_data[1][0], (int)adc_data[1][1]);
643 static int __adc2vbat(int adc_res)
645 int t = adc_data[0][0] - adc_data[1][0];
646 t *= (adc_res - adc_data[0][1]);
647 t /= (adc_data[0][1] - adc_data[1][1]);
652 #define MEASURE_TIMES (15)
654 static void __dump_adc_result(u32 adc_val[])
656 #if defined(CONFIG_REGULATOR_ADC_DEBUG)
658 for (i = 0; i < MEASURE_TIMES; i++) {
659 printk("%d ", adc_val[i]);
665 static int cmp_val(const void *a, const void *b)
667 return *(int *)a - *(int *)b;
671 * __adc_voltage - get regulator output voltage through auxadc
672 * @regulator: regulator source
674 * This returns the current regulator voltage in mV.
676 * NOTE: If the regulator is disabled it will return the voltage value. This
677 * function should not be used to determine regulator state.
679 static int regu_adc_voltage(struct regulator_dev *rdev)
681 struct sci_regulator_desc *desc = __get_desc(rdev);
682 struct sci_regulator_regs *regs = desc->regs;
684 int ret, adc_chan = regs->cal_ctl_bits >> 16;
685 u16 ldo_cal_sel = regs->cal_ctl_bits & 0xFFFF;
686 u32 adc_res, adc_val[MEASURE_TIMES];
687 u32 chan_numerators = 1, chan_denominators = 1;
688 u32 bat_numerators, bat_denominators;
690 struct adc_sample_data data = {
691 .channel_id = adc_chan,
692 .channel_type = 0, /*sw */
693 .hw_channel_delay = 0, /*reserved */
694 .scale = 1, /*big scale */
696 .sample_num = MEASURE_TIMES,
697 .sample_bits = adc_sample_bit,
698 .sample_speed = 0, /*quick mode */
699 .signal_mode = 0, /*resistance path */
702 if (!__is_valid_adc_cal())
708 /* enable ldo cal before adc sampling and ldo calibration */
709 if (0 == regs->typ) {
710 mutex_lock(&adc_chan_mutex);
711 ANA_REG_OR(regs->cal_ctl, ldo_cal_sel);
712 debug0("%s adc channel %d : %04x\n",
713 desc->desc.name, data.channel_id, ldo_cal_sel);
716 ret = sci_adc_get_values(&data);
719 /* close ldo cal and release multiplexed aux adc channel */
720 if (0 == regs->typ) {
721 ANA_REG_BIC(regs->cal_ctl, ldo_cal_sel);
722 mutex_unlock(&adc_chan_mutex);
725 __dump_adc_result(adc_val);
726 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
727 /*__dump_adc_result(adc_val);*/
729 sci_adc_get_vol_ratio(data.channel_id, data.scale,
730 &chan_numerators, &chan_denominators);
732 sci_adc_get_vol_ratio(ADC_CHANNEL_VBAT, 0, &bat_numerators, &bat_denominators);
734 adc_res = adc_val[MEASURE_TIMES / 2];
735 debug("%s adc channel %d : 0x%04x, ratio (%d/%d), result value %d\n",
736 desc->desc.name, data.channel_id, ldo_cal_sel,
737 chan_numerators, chan_denominators, adc_res);
742 return __adc2vbat(adc_res)
743 * (bat_numerators * chan_denominators)
744 / (bat_denominators * chan_numerators);
747 #ifndef CONFIG_REGULATOR_SC2713
749 * regulator_strongly_disable - strongly disable regulator output
750 * @regulator: regulator source
752 * Strongly try disable the regulator output voltage or current.
753 * NOTE: this *will* disable the regulator output even if other consumer
754 * devices have it enabled. This should be used for situations when device
755 * had unbalanced with calls to regulator_enable().
756 * *Not* recommended to call this function before try to balance the use_count.
758 int regulator_strongly_disable(struct regulator *regulator)
760 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
764 while (rdev->use_count--)
765 regulator_disable(regulator);
770 EXPORT_SYMBOL_GPL(regulator_strongly_disable);
773 static struct regulator_ops ldo_ops = {
774 .enable = ldo_turn_on,
775 .disable = ldo_turn_off,
776 .is_enabled = ldo_is_on,
777 .set_voltage = ldo_set_voltage,
778 .get_voltage = ldo_get_voltage,
779 .set_mode = ldo_set_mode,
780 /* .enable_time = ldo_enable_time, */
784 static struct regulator_ops dcdc_ops = {
785 .enable = ldo_turn_on,
786 .disable = ldo_turn_off,
787 .is_enabled = ldo_is_on,
788 .set_voltage = dcdc_set_voltage,
789 .get_voltage = dcdc_get_voltage,
792 static struct regulator_ops boost_ops = {
793 .enable = ldo_turn_on,
794 .disable = ldo_turn_off,
795 .is_enabled = ldo_is_on,
796 .set_current_limit = boost_set_current_limit,
797 .get_current_limit = boost_get_current_limit,
798 .set_mode = ldo_set_mode,
803 * Consider the following machine :-
805 * Regulator-1 -+-> [Consumer A @ 1.8V]
807 * +-> [Consumer B @ 1.8V]
809 * Regulator-2 ---> [Consumer C @ 3.3V]
811 * The drivers for consumers A & B must be mapped to the correct regulator in
812 * order to control their power supply. This mapping can be achieved in board/machine
813 * initialisation code by creating a struct regulator_consumer_supply for each regulator.
814 * Alternatively, we built a regulator supply-consumers map, the format is as follow:
816 * supply source-1, consumer A, consumer B, ..., NULL
817 * supply source-2, consumer C, ..., NULL
822 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
823 const char *supply_name,
826 char **map = (char **)dev_get_platdata(dev);
828 struct regulator_consumer_supply *consumer_supplies = NULL;
830 if (!supply_name || !(map && map[0]))
833 for (i = 0; map[i] || map[i + 1]; i++) {
834 if (map[i] && 0 == strcmp(map[i], supply_name))
838 /* i++; *//* Do not skip supply name */
840 for (n = 0; map[i + n]; n++) ;
843 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
845 kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
846 BUG_ON(!consumer_supplies);
847 for (n = 0; map[i]; i++, n++) {
848 consumer_supplies[n].supply = map[i];
853 return consumer_supplies;
856 #if defined(CONFIG_DEBUG_FS)
857 static struct dentry *debugfs_root = NULL;
859 static u32 ana_addr = 0;
860 static int debugfs_ana_addr_get(void *data, u64 * val)
862 if (ana_addr < PAGE_SIZE) {
863 *val = ANA_REG_GET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK));
865 void *addr = ioremap(ana_addr, PAGE_SIZE);
866 *val = __raw_readl(addr);
872 static int debugfs_ana_addr_set(void *data, u64 val)
874 if (ana_addr < PAGE_SIZE) {
875 ANA_REG_SET(ana_addr + (ANA_REGS_GLB_BASE & PAGE_MASK), val, -1);
877 void *addr = ioremap(ana_addr, PAGE_SIZE);
878 __raw_writel(val, addr);
884 static int adc_chan = 5 /*VBAT*/;
885 static int debugfs_adc_chan_get(void *pdata, u64 * val)
888 u32 adc_res, adc_val[MEASURE_TIMES];
889 struct adc_sample_data data = {
890 .channel_id = adc_chan,
891 .channel_type = 0, /*sw */
892 .hw_channel_delay = 0, /*reserved */
893 .scale = 1, /*big scale */
895 .sample_num = MEASURE_TIMES,
896 .sample_bits = adc_sample_bit,
897 .sample_speed = 0, /*quick mode */
898 .signal_mode = 0, /*resistance path */
901 ret = sci_adc_get_values(&data);
904 for (i = 0; i < MEASURE_TIMES; i++) {
905 printk("%d ", adc_val[i]);
909 sort(adc_val, MEASURE_TIMES, sizeof(u32), cmp_val, 0);
910 adc_res = adc_val[MEASURE_TIMES / 2];
911 pr_info("adc chan %d, result value %d, vbat %d\n",
912 data.channel_id, adc_res, __adc2vbat(adc_res));
917 static int debugfs_adc_chan_set(void *data, u64 val)
923 static int debugfs_enable_get(void *data, u64 * val)
925 struct regulator_dev *rdev = data;
926 if (rdev && rdev->desc->ops->is_enabled)
927 *val = rdev->desc->ops->is_enabled(rdev);
933 static int debugfs_enable_set(void *data, u64 val)
935 struct regulator_dev *rdev = data;
936 if (rdev && rdev->desc->ops->enable)
937 (val) ? rdev->desc->ops->enable(rdev)
938 : rdev->desc->ops->disable(rdev);
942 static int debugfs_voltage_get(void *data, u64 * val)
944 struct regulator_dev *rdev = data;
946 *val = regu_adc_voltage(rdev);
952 static int debugfs_voltage_set(void *data, u64 val)
954 struct regulator_dev *rdev = data;
955 if (rdev && rdev->desc->ops->set_voltage) {
956 u32 min_uV = (u32)val * 1000;
957 min_uV += rdev->constraints->uV_offset;
958 rdev->desc->ops->set_voltage(rdev, min_uV, min_uV, 0);
964 static int debugfs_boost_get(void *data, u64 * val)
966 struct regulator_dev *rdev = data;
967 if (rdev && rdev->desc->ops->get_current_limit)
968 *val = rdev->desc->ops->get_current_limit(rdev) / 1000;
974 static int debugfs_boost_set(void *data, u64 val)
976 struct regulator_dev *rdev = data;
977 if (rdev && rdev->desc->ops->set_current_limit)
978 rdev->desc->ops->set_current_limit(rdev, val * 1000,
983 DEFINE_SIMPLE_ATTRIBUTE(fops_ana_addr,
984 debugfs_ana_addr_get, debugfs_ana_addr_set, "%llu\n");
985 DEFINE_SIMPLE_ATTRIBUTE(fops_adc_chan,
986 debugfs_adc_chan_get, debugfs_adc_chan_set, "%llu\n");
987 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
988 debugfs_enable_get, debugfs_enable_set, "%llu\n");
989 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
990 debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
991 DEFINE_SIMPLE_ATTRIBUTE(fops_boost,
992 debugfs_boost_get, debugfs_boost_set, "%llu\n");
994 static void rdev_init_debugfs(struct regulator_dev *rdev)
996 struct sci_regulator_desc *desc = __get_desc(rdev);
997 desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
998 if (IS_ERR_OR_NULL(rdev->debugfs)) {
999 pr_warn("Failed to create debugfs directory\n");
1000 rdev->debugfs = NULL;
1004 debugfs_create_file("enable", S_IRUGO | S_IWUSR,
1005 desc->debugfs, rdev, &fops_enable);
1007 if (desc->desc.type == REGULATOR_CURRENT)
1008 debugfs_create_file("current", S_IRUGO | S_IWUSR,
1009 desc->debugfs, rdev, &fops_boost);
1011 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
1012 desc->debugfs, rdev, &fops_ldo);
1015 static void rdev_init_debugfs(struct regulator_dev *rdev)
1020 static void *sci_regulator_register(struct platform_device *pdev,
1021 struct sci_regulator_desc *desc)
1023 static atomic_t idx = ATOMIC_INIT(1); /* 0: dummy */
1024 struct regulator_dev *rdev;
1025 struct regulator_ops *__regs_ops[] = {
1026 &ldo_ops, 0, &dcdc_ops, 0 /*lpref_ops */ , &boost_ops,
1029 struct regulator_consumer_supply consumer_supplies_default[] = {
1031 .supply = desc->desc.name,
1034 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1035 struct regulator_config config = { };
1038 struct regulator_init_data init_data = {
1039 .supply_regulator = 0,
1042 .max_uV = 4200 * 1000,
1044 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
1046 REGULATOR_CHANGE_STATUS |
1047 REGULATOR_CHANGE_VOLTAGE |
1048 REGULATOR_CHANGE_MODE,
1050 .num_consumer_supplies = 1,
1051 .consumer_supplies = consumer_supplies_default,
1052 .regulator_init = 0,
1056 desc->desc.id = atomic_inc_return(&idx) - 1;
1058 /* Fixme: Config dynamically dcdc/ldo
1059 * accoring to bit BONDOPT4 & BONDOPT6 for Reg(0x40038800 + 0x00E4)
1063 if ((ana_status >> 6) & 0x1) {
1064 if (0 == strcmp(desc->desc.name, "vddcore")) {
1065 desc->regs->vol_def = 900;
1066 desc->regs->vol_ctl = (u32)ANA_REG_GLB_MP_MISC_CTRL;
1067 desc->regs->vol_ctl_bits = BIT(3)|BIT(4)|BIT(5);
1068 } else if (0 == strcmp(desc->desc.name, "vddarm")) {
1069 desc->regs->vol_def = 900;
1070 desc->regs->vol_ctl = (u32)ANA_REG_GLB_MP_MISC_CTRL;
1071 desc->regs->vol_ctl_bits = BIT(6)|BIT(7)|BIT(8);
1072 } else if (0 == strcmp(desc->desc.name, "vdd25")) {
1073 desc->regs->vol_def = 1800;
1074 desc->regs->vol_ctl = (u32)ANA_REG_GLB_MP_MISC_CTRL;
1075 desc->regs->vol_ctl_bits = BIT(9)|BIT(10);
1076 desc->regs->vol_sel[0] = 2500;
1077 desc->regs->vol_sel[1] = 2750;
1078 desc->regs->vol_sel[2] = 1800;
1079 desc->regs->vol_sel[3] = 1900;
1082 if (0 == strcmp(desc->desc.name, "vddcore")) {
1083 desc->regs->vol_def = 1100;
1084 desc->regs->vol_ctl = (u32)ANA_REG_GLB_DCDC_CORE_ADI;
1085 desc->regs->vol_ctl_bits = BIT(5)|BIT(6)|BIT(7);
1086 } else if (0 == strcmp(desc->desc.name, "vddarm")) {
1087 desc->regs->vol_def = 1100;
1088 desc->regs->vol_ctl = (u32)ANA_REG_GLB_DCDC_ARM_ADI;
1089 desc->regs->vol_ctl_bits = BIT(5)|BIT(6)|BIT(7);
1090 } else if (0 == strcmp(desc->desc.name, "vdd25")) {
1091 desc->regs->vol_def = 2500;
1092 desc->regs->vol_ctl = (u32)ANA_REG_GLB_LDO_V_CTRL0;
1093 desc->regs->vol_ctl_bits = BIT(4)|BIT(5);
1094 desc->regs->vol_sel[0] = 2500;
1095 desc->regs->vol_sel[1] = 2750;
1096 desc->regs->vol_sel[2] = 3000;
1097 desc->regs->vol_sel[3] = 2900;
1102 if ((ana_status >> 4) & 0x1) {
1103 if (0 == strcmp(desc->desc.name, "vddwrf")) {
1104 desc->regs->vol_def = 2800;
1105 desc->regs->vol_sel[0] = 2600;
1106 desc->regs->vol_sel[1] = 2700;
1107 desc->regs->vol_sel[2] = 2800;
1108 desc->regs->vol_sel[3] = 2900;
1109 } else if (0 == strcmp(desc->desc.name, "vddrf1")) {
1110 desc->regs->vol_def = 2850;
1111 desc->regs->vol_ctl = (u32)ANA_REG_GLB_LDO_V_CTRL0;
1112 desc->regs->vol_ctl_bits = BIT(8)|BIT(9);
1115 if (0 == strcmp(desc->desc.name, "vddwrf")) {
1116 desc->regs->vol_def = 1500;
1117 desc->regs->vol_sel[0] = 1300;
1118 desc->regs->vol_sel[1] = 1400;
1119 desc->regs->vol_sel[2] = 1500;
1120 desc->regs->vol_sel[3] = 1600;
1121 } else if (0 == strcmp(desc->desc.name, "vddrf1")) {
1122 desc->regs->vol_def = 1200;
1123 desc->regs->vol_ctl = (u32)ANA_REG_GLB_MP_MISC_CTRL;
1124 desc->regs->vol_ctl_bits = BIT(11)|BIT(12);
1128 BUG_ON(desc->regs->pd_set
1129 && desc->regs->pd_set == desc->regs->pd_rst
1130 && desc->regs->pd_set_bit == desc->regs->pd_rst_bit);
1132 BUG_ON(desc->regs->typ >= ARRAY_SIZE(__regs_ops));
1133 if (!desc->desc.ops)
1134 desc->desc.ops = __regs_ops[desc->regs->typ];
1136 init_data.consumer_supplies =
1137 set_supply_map(&pdev->dev, desc->desc.name,
1138 &init_data.num_consumer_supplies);
1140 if (!init_data.consumer_supplies)
1141 init_data.consumer_supplies = consumer_supplies_default;
1143 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
1144 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1145 config.dev = &pdev->dev;
1146 config.init_data = &init_data;
1147 config.driver_data = NULL;
1148 config.of_node = NULL;
1149 rdev = regulator_register(&desc->desc, &config);
1150 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1151 rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0, 0);
1153 rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0);
1156 if (init_data.consumer_supplies != consumer_supplies_default)
1157 kfree(init_data.consumer_supplies);
1160 if (!IS_ERR_OR_NULL(rdev)) {
1161 rdev->reg_data = rdev;
1162 //INIT_DELAYED_WORK(&desc->data.dwork, do_regu_work);
1163 desc->data.rdev = rdev;
1164 __init_trimming(rdev);
1165 rdev_init_debugfs(rdev);
1172 * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
1173 * so i autogen all regulators non-variable description in plat or mach directory,
1174 * which named __xxxx_regulator_map.h, BUT register all in regulator driver probe func,
1175 * just like other regulator vendor drivers.
1177 static int sci_regulator_probe(struct platform_device *pdev)
1179 debug0("%s %p\n", __func__, pdev);
1180 #include CONFIG_REGULATOR_SPRD_MAP_V1
1184 static struct platform_driver sci_regulator_driver = {
1186 .name = "sc2713s-regulator",
1187 .owner = THIS_MODULE,
1188 .of_match_table = of_match_ptr(sprd_regulator_of_match),
1190 .probe = sci_regulator_probe,
1193 static int __init regu_driver_init(void)
1197 #ifdef CONFIG_DEBUG_FS
1199 debugfs_create_dir(REGULATOR_ROOT_DIR, NULL);
1200 if (IS_ERR_OR_NULL(debugfs_root)) {
1202 "%s: Failed to create debugfs directory\n", REGULATOR_ROOT_DIR);
1203 debugfs_root = NULL;
1206 debugfs_create_u32("ana_addr", S_IRUGO | S_IWUSR,
1207 debugfs_root, (u32 *) & ana_addr);
1208 debugfs_create_file("ana_valu", S_IRUGO | S_IWUSR,
1209 debugfs_root, &ana_addr, &fops_ana_addr);
1210 debugfs_create_file("adc_chan", S_IRUGO | S_IWUSR,
1211 debugfs_root, &adc_chan, &fops_adc_chan);
1212 debugfs_create_u64("adc_data", S_IRUGO | S_IWUSR,
1213 debugfs_root, (u64 *) & adc_data);
1215 {/* vddarm/vddcore/vddmem common debugfs interface */
1217 struct dentry *vol_root = debugfs_create_dir("vol", NULL);
1218 sprintf(str, "../%s/vddarm/voltage", REGULATOR_ROOT_DIR);
1219 debugfs_create_symlink("dcdcarm", vol_root, str);
1220 sprintf(str, "../%s/vddcore/voltage", REGULATOR_ROOT_DIR);
1221 debugfs_create_symlink("dcdccore", vol_root, str);
1222 sprintf(str, "../%s/vddmem/voltage", REGULATOR_ROOT_DIR);
1223 debugfs_create_symlink("dcdcmem", vol_root, str);
1227 ana_chip_id = (sci_get_ana_chip_id() | sci_get_ana_chip_ver());
1228 ana_status = ANA_REG_GET(ANA_REG_GLB_ANA_STATUS);
1230 pr_info("sc271x ana chip id: (0x%08x), bond opt (0x%08x)\n", ana_chip_id, ana_status);
1232 #ifdef CONFIG_REGULATOR_SUPPORT_CAMIO_1200MV
1233 ANA_REG_OR(ANA_REG_GLB_CA_CTRL0, BIT_LDO_CAMIO_V_B2);
1235 ANA_REG_BIC(ANA_REG_GLB_CA_CTRL0, BIT_LDO_CAMIO_V_B2);
1238 #include CONFIG_REGULATOR_SPRD_MAP_V1
1243 static struct platform_driver sci_regulator_driver = {
1245 .name = "sc2713s-regulator",
1246 .owner = THIS_MODULE,
1248 .probe = sci_regulator_probe,
1251 static int __init regu_driver_init(void)
1253 __adc_cal_fuse_setup();
1255 return platform_driver_register(&sci_regulator_driver);
1258 #ifndef CONFIG_REGULATOR_SC2713
1259 int __init sci_regulator_init(void)
1261 static struct platform_device regulator_device = {
1262 .name = "sc2713s-regulator",
1266 return platform_device_register(®ulator_device);
1270 subsys_initcall(regu_driver_init);
1272 MODULE_LICENSE("GPL v2");
1273 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
1274 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");
1275 MODULE_VERSION("0.5");