2 * Copyright (C) 2012 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 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/debugfs.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
28 #include <mach/hardware.h>
30 #include <mach/sci_glb_regs.h>
34 #define debug(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
35 #define debug0(format, arg...)
38 #define ANA_REG_OR(_r, _b) sci_adi_write(_r, _b, 0)
42 #define ANA_REG_BIC(_r, _b) sci_adi_write(_r, 0, _b)
46 #define ANA_REG_GET(_r) sci_adi_read(_r)
50 #define ANA_REG_SET(_r, _v, _m) sci_adi_write(_r, _v, _m)
53 struct sci_regulator_regs {
55 u32 pd_set, pd_set_bit;
56 u32 pd_rst, pd_rst_bit;
57 u32 slp_ctl, slp_ctl_bit;
58 u32 vol_trm, vol_trm_bits;
59 u32 vol_ctl, vol_ctl_bits;
60 u32 vol_sel_cnt, vol_sel[];
63 struct sci_regulator_desc {
64 struct regulator_desc desc;
65 const struct sci_regulator_regs *regs;
66 #if defined(CONFIG_DEBUG_FS)
67 struct dentry *debugfs;
85 #define SCI_REGU_REG(VDD, TYP, PD_SET, SET_BIT, PD_RST, RST_BIT, SLP_CTL, SLP_CTL_BIT, \
86 VOL_TRM, VOL_TRM_BITS, VOL_CTL, VOL_CTL_BITS, VOL_SEL_CNT, ...) \
88 static const struct sci_regulator_regs REGS_##VDD = { \
91 .pd_set_bit = SET_BIT, \
93 .pd_rst_bit = RST_BIT, \
95 .slp_ctl_bit = SLP_CTL_BIT, \
97 .vol_trm_bits = VOL_TRM_BITS, \
99 .vol_ctl_bits = VOL_CTL_BITS, \
100 .vol_sel_cnt = VOL_SEL_CNT, \
101 .vol_sel = {__VA_ARGS__}, \
103 static struct sci_regulator_desc DESC_##VDD = { \
107 .desc.type = REGULATOR_VOLTAGE, \
108 .desc.owner = THIS_MODULE, \
109 .regs = ®S_##VDD, \
111 sci_regulator_register(pdev, &DESC_##VDD); \
114 int reguator_is_trimming(struct regulator_dev *rdev);
115 int ldo_trimming_callback(void *data);
116 int __def_callback(void *data)
121 int ldo_trimming_callback(void *)
122 __attribute__ ((weak, alias("__def_callback")));
124 /* standard ldo ops*/
125 int sci_ldo_op(const struct sci_regulator_regs *regs, int op)
129 debug0("regu %p op(%d), set %08x[%d], rst %08x[%d]\n", regs, op,
130 regs->pd_set, __ffs(regs->pd_set_bit), regs->pd_rst,
131 __ffs(regs->pd_rst_bit));
133 if (!regs->pd_rst || !regs->pd_set)
138 ANA_REG_OR(regs->pd_rst, regs->pd_rst_bit);
139 ANA_REG_BIC(regs->pd_set, regs->pd_set_bit);
142 ANA_REG_OR(regs->pd_set, regs->pd_set_bit);
143 ANA_REG_BIC(regs->pd_rst, regs->pd_rst_bit);
146 ret = ! !(ANA_REG_GET(regs->pd_rst) & regs->pd_rst_bit);
147 if (ret == ! !(ANA_REG_GET(regs->pd_set) & regs->pd_set_bit))
156 static int ldo_turn_on(struct regulator_dev *rdev)
158 struct sci_regulator_desc *desc =
159 (struct sci_regulator_desc *)rdev->desc;
160 const struct sci_regulator_regs *regs = desc->regs;
162 int ret = sci_ldo_op(regs, VDD_ON);
164 debug0("regu %p (%s), turn on\n", regs, desc->desc.name);
165 /* notify ldo trimming when first turn on */
166 if (0 == ret && regs->vol_trm && !reguator_is_trimming(rdev)) {
167 ldo_trimming_callback((void *)desc->desc.name);
172 static int ldo_turn_off(struct regulator_dev *rdev)
174 return sci_ldo_op(((struct sci_regulator_desc *)(rdev->desc))->regs,
178 static int ldo_is_on(struct regulator_dev *rdev)
180 return sci_ldo_op(((struct sci_regulator_desc *)(rdev->desc))->regs,
184 static int ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
186 struct sci_regulator_desc *desc =
187 (struct sci_regulator_desc *)rdev->desc;
188 const struct sci_regulator_regs *regs = desc->regs;
189 debug("regu %p (%s), slp %08x[%d] mode %x\n", regs, desc->desc.name,
190 regs->slp_ctl, regs->slp_ctl_bit, mode);
194 if (mode == REGULATOR_MODE_STANDBY) { /* disable auto slp */
195 ANA_REG_BIC(regs->slp_ctl, regs->slp_ctl_bit);
197 ANA_REG_OR(regs->slp_ctl, regs->slp_ctl_bit);
202 static int ldo_set_voltage(struct regulator_dev *rdev, int min_uV,
203 int max_uV, unsigned *selector)
205 static const int vol_bits[4] = { 0xa, 0x9, 0x6, 0x5 };
206 struct sci_regulator_desc *desc =
207 (struct sci_regulator_desc *)rdev->desc;
208 const struct sci_regulator_regs *regs = desc->regs;
209 int mv = min_uV / 1000;
211 int i, shft = __ffs(regs->vol_ctl_bits);
212 BUG_ON(regs->vol_sel_cnt > 4);
213 debug("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
217 for (i = 0; i < regs->vol_sel_cnt; i++) {
218 if (regs->vol_sel[i] == mv) {
219 ANA_REG_SET(regs->vol_ctl, vol_bits[i] << shft,
221 //clear_bit(desc->desc.id, trimming_state);
228 "warning: regulator (%s) not support %dmV\n", desc->desc.name, mv);
232 static int ldo_get_voltage(struct regulator_dev *rdev)
234 struct sci_regulator_desc *desc =
235 (struct sci_regulator_desc *)rdev->desc;
236 const struct sci_regulator_regs *regs = desc->regs;
238 int i, shft = __ffs(regs->vol_ctl_bits);
240 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x\n",
241 regs, desc->desc.name, regs->vol_ctl, shft, regs->vol_ctl_bits);
246 BUG_ON(regs->vol_sel_cnt != 4);
247 vol_bits = ((ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits) >> shft);
249 if ((vol_bits & BIT(0)) ^ (vol_bits & BIT(1))
250 && (vol_bits & BIT(2)) ^ (vol_bits & BIT(3))) {
251 i = (vol_bits & BIT(0)) | ((vol_bits >> 1) & BIT(1));
252 vol = regs->vol_sel[i];
253 debug("regu %p (%s), voltage %d\n", regs, desc->desc.name, vol);
259 static unsigned long trimming_state[2] = { 0, 0 }; /* 64 bits */
261 int reguator_is_trimming(struct regulator_dev *rdev)
266 BUG_ON(!(id > 0 && id < sizeof(trimming_state) * 8));
268 return test_bit(id, trimming_state);
271 static int ldo_init_trimming(struct regulator_dev *rdev)
273 struct sci_regulator_desc *desc =
274 (struct sci_regulator_desc *)rdev->desc;
275 const struct sci_regulator_regs *regs = desc->regs;
277 int shft = __ffs(regs->vol_trm_bits);
283 trim = (ANA_REG_GET(regs->vol_trm) & regs->vol_trm_bits) >> shft;
284 if (trim != 0x10 /* 100 % */ ) {
285 debug("regu %p (%s) trimming ok\n", regs, desc->desc.name);
286 set_bit(desc->desc.id, trimming_state);
288 } else if (1 == ldo_is_on(rdev)) { /* some LDOs had been turned in uboot-spl */
289 ret = ldo_turn_on(rdev);
297 * ldo trimming step about 0.7%, range 90% ~ 110%. that all maps as follow.
333 static int ldo_set_trimming(struct regulator_dev *rdev, int ctl_vol, int to_vol)
335 struct sci_regulator_desc *desc =
336 (struct sci_regulator_desc *)rdev->desc;
337 const struct sci_regulator_regs *regs = desc->regs;
338 int ret = -EINVAL, cal_vol;
343 cal_vol = ctl_vol - to_vol * 90 / 100; /* cal range 90% ~ 110% */
344 if (!regs->vol_trm || cal_vol < 0 || cal_vol >= to_vol * 20 / 100)
347 /* always update voltage ctrl bits */
348 ret = ldo_set_voltage(rdev, to_vol * 1000, to_vol * 1000, 0);
349 if (IS_ERR_VALUE(ret) && regs->vol_ctl)
353 u32 trim = /* assert 5 valid trim bits */
354 (cal_vol * 100 * 32) / (to_vol * 20) & 0x1f;
356 ("regu %p (%s) trimming %u = %u %+dmv, got [%02X] %u.%03u%%\n",
357 regs, desc->desc.name, ctl_vol, to_vol,
358 (cal_vol - to_vol / 10), trim, ctl_vol * 100 / to_vol,
359 (ctl_vol * 100 * 1000 / to_vol) % 1000);
361 ANA_REG_SET(regs->vol_trm, trim << __ffs(regs->vol_trm_bits),
363 set_bit(desc->desc.id, trimming_state);
371 int regulator_set_trimming(struct regulator *regulator, int ctl_vol, int to_vol)
373 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
374 struct sci_regulator_desc *desc =
375 (struct sci_regulator_desc *)rdev->desc;
376 const struct sci_regulator_regs *regs = desc->regs;
378 return (2 /*DCDC*/ == regs->typ)
379 ? regulator_set_voltage(regulator, ctl_vol, ctl_vol)
380 : ldo_set_trimming(rdev, ctl_vol, to_vol);
383 int regulator_get_trimming_step(struct regulator *regulator, int def_vol)
385 struct regulator_dev *rdev = regulator_get_drvdata(regulator);
386 struct sci_regulator_desc *desc =
387 (struct sci_regulator_desc *)rdev->desc;
388 const struct sci_regulator_regs *regs = desc->regs;
390 return (2 /*DCDC*/ == regs->typ)
392 : def_vol * 7 / 1000;
395 static int __match_dcdc_vol(const struct sci_regulator_regs *regs, u32 vol)
398 int ds, min_ds = 100; /* mV, the max range of small voltage */
399 for (i = 0; i < regs->vol_sel_cnt; i++) {
400 ds = vol - regs->vol_sel[i];
401 if (ds >= 0 && ds < min_ds) {
409 /* standard dcdc ops*/
410 static int dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
411 int max_uV, unsigned *selector)
413 struct sci_regulator_desc *desc =
414 (struct sci_regulator_desc *)rdev->desc;
415 const struct sci_regulator_regs *regs = desc->regs;
416 int mv = min_uV / 1000;
417 int i, shft = __ffs(regs->vol_ctl_bits);
418 int max = regs->vol_ctl_bits >> shft;
420 debug0("regu %p (%s) %d %d\n", regs, desc->desc.name, min_uV, max_uV);
423 BUG_ON(regs->vol_sel_cnt > 8);
428 /* found the closely vol ctrl bits */
429 i = __match_dcdc_vol(regs, mv);
433 debug("regu %p (%s) %d = %d %+dmv\n", regs, desc->desc.name,
434 mv, regs->vol_sel[i], mv - regs->vol_sel[i]);
436 if (regs->vol_trm) { /* small adjust first */
437 /* dcdc calibration control bits (default 00000),
438 * small adjust voltage: 100/32mv ~= 3.125mv
440 int j = ((mv - regs->vol_sel[i]) * 32) / (100) % 32;
441 ANA_REG_SET(regs->vol_trm,
443 BITS_DCDC_CAL_RST(BITS_DCDC_CAL(-1) - j), -1);
446 ANA_REG_SET(regs->vol_ctl, i | (max - i) << 4, -1);
450 static int dcdc_get_voltage(struct regulator_dev *rdev)
452 struct sci_regulator_desc *desc =
453 (struct sci_regulator_desc *)rdev->desc;
454 const struct sci_regulator_regs *regs = desc->regs;
456 int cal = 0; /* mV */
457 int i, shft = __ffs(regs->vol_ctl_bits);
459 debug0("regu %p (%s), vol ctl %08x, shft %d, mask %08x, sel %d\n",
460 regs, desc->desc.name, regs->vol_ctl,
461 shft, regs->vol_ctl_bits, regs->vol_sel_cnt);
464 BUG_ON(regs->vol_sel_cnt > 8);
469 i = (ANA_REG_GET(regs->vol_ctl) & regs->vol_ctl_bits);
471 /*check the reset relative bit of vol ctl */
473 (~ANA_REG_GET(regs->vol_ctl) & (regs->vol_ctl_bits << 4)) >> 4;
478 mv = regs->vol_sel[i];
481 int j = ANA_REG_GET(regs->vol_trm) & BITS_DCDC_CAL(-1);
482 cal = DIV_ROUND_CLOSEST(j * 100, 32);
485 debug("regu %p (%s) %d +%dmv\n", regs, desc->desc.name, mv, cal);
486 return (mv + cal) * 1000;
489 /* standard ldo-D-Die ops*/
490 static int usbd_turn_on(struct regulator_dev *rdev)
492 const struct sci_regulator_regs *regs =
493 ((struct sci_regulator_desc *)(rdev->desc))->regs;
494 sci_glb_clr(regs->pd_set, regs->pd_set_bit);
498 static int usbd_turn_off(struct regulator_dev *rdev)
500 const struct sci_regulator_regs *regs =
501 ((struct sci_regulator_desc *)(rdev->desc))->regs;
502 sci_glb_set(regs->pd_set, regs->pd_set_bit);
506 static int usbd_is_on(struct regulator_dev *rdev)
508 const struct sci_regulator_regs *regs =
509 ((struct sci_regulator_desc *)(rdev->desc))->regs;
510 return !sci_glb_read(regs->pd_set, regs->pd_set_bit);
513 static struct regulator_ops ldo_ops = {
514 .enable = ldo_turn_on,
515 .disable = ldo_turn_off,
516 .is_enabled = ldo_is_on,
517 .set_voltage = ldo_set_voltage,
518 .get_voltage = ldo_get_voltage,
519 .set_mode = ldo_set_mode,
522 static struct regulator_ops usbd_ops = {
523 .enable = usbd_turn_on,
524 .disable = usbd_turn_off,
525 .is_enabled = usbd_is_on,
528 static struct regulator_ops dcdc_ops = {
529 .enable = ldo_turn_on,
530 .disable = ldo_turn_off,
531 .is_enabled = ldo_is_on,
532 .set_voltage = dcdc_set_voltage,
533 .get_voltage = dcdc_get_voltage,
537 * Consider the following machine :-
539 * Regulator-1 -+-> [Consumer A @ 1.8V]
541 * +-> [Consumer B @ 1.8V]
543 * Regulator-2 ---> [Consumer C @ 3.3V]
545 * The drivers for consumers A & B must be mapped to the correct regulator in
546 * order to control their power supply. This mapping can be achieved in board/machine
547 * initialisation code by creating a struct regulator_consumer_supply for each regulator.
548 * Alternatively, we built a regulator supply-consumers map, the format is as follow:
550 * supply source-1, consumer A, consumer B, ..., NULL
551 * supply source-2, consumer C, ..., NULL
556 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
557 const char *supply_name,
560 char **map = (char **)dev_get_platdata(dev);
562 struct regulator_consumer_supply *consumer_supplies = NULL;
564 if (!supply_name || !(map && map[0]))
567 for (i = 0; map[i] || map[i + 1]; i++) {
568 if (map[i] && 0 == strcmp(map[i], supply_name))
572 /* i++; *//* Do not skip supply name */
574 for (n = 0; map[i + n]; n++) ;
577 pr_info("supply %s consumers %d - %d\n", supply_name, i, n);
579 kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
580 BUG_ON(!consumer_supplies);
581 for (n = 0; map[i]; i++, n++) {
582 consumer_supplies[n].supply = map[i];
587 return consumer_supplies;
590 #if defined(CONFIG_DEBUG_FS)
591 static struct dentry *debugfs_root = NULL;
593 static int debugfs_enable_get(void *data, u64 * val)
595 struct regulator_dev *rdev = data;
596 if (rdev && rdev->desc->ops->is_enabled)
597 *val = rdev->desc->ops->is_enabled(rdev);
603 static int debugfs_enable_set(void *data, u64 val)
605 struct regulator_dev *rdev = data;
606 if (rdev && rdev->desc->ops->enable)
607 (val) ? rdev->desc->ops->enable(rdev)
608 : rdev->desc->ops->disable(rdev);
612 static int debugfs_voltage_get(void *data, u64 * val)
614 struct regulator_dev *rdev = data;
615 if (rdev && rdev->desc->ops->get_voltage)
616 *val = rdev->desc->ops->get_voltage(rdev) / 1000;
622 static int debugfs_voltage_set(void *data, u64 val)
624 struct regulator_dev *rdev = data;
625 if (rdev && rdev->desc->ops->set_voltage)
626 rdev->desc->ops->set_voltage(rdev, val * 1000, val * 1000, 0);
630 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
631 debugfs_enable_get, debugfs_enable_set, "%llu\n");
632 DEFINE_SIMPLE_ATTRIBUTE(fops_voltage,
633 debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
635 static void rdev_init_debugfs(struct regulator_dev *rdev)
637 struct sci_regulator_desc *desc =
638 (struct sci_regulator_desc *)rdev->desc;
640 desc->debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
641 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
642 pr_warn("Failed to create debugfs directory\n");
643 rdev->debugfs = NULL;
647 debugfs_create_file("enable", S_IRUGO | S_IWUSR,
648 desc->debugfs, rdev, &fops_enable);
650 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
651 desc->debugfs, rdev, &fops_voltage);
654 static void rdev_init_debugfs(struct regulator_dev *rdev)
659 void *__devinit sci_regulator_register(struct platform_device *pdev,
660 struct sci_regulator_desc *desc)
662 static atomic_t __devinitdata idx = ATOMIC_INIT(1); /* 0: dummy */
663 struct regulator_dev *rdev;
664 struct regulator_ops *__regs_ops[] = {
665 &ldo_ops, &usbd_ops, &dcdc_ops, 0,
667 struct regulator_consumer_supply consumer_supplies_default[] = {
671 .supply = desc->desc.name,
674 struct regulator_init_data init_data = {
675 .supply_regulator = 0,
678 .max_uV = 4200 * 1000,
680 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY,
682 REGULATOR_CHANGE_STATUS |
683 REGULATOR_CHANGE_VOLTAGE |
684 REGULATOR_CHANGE_MODE,
686 .num_consumer_supplies = 1,
687 .consumer_supplies = consumer_supplies_default,
692 BUG_ON(desc->regs->typ > 3);
694 desc->desc.ops = __regs_ops[desc->regs->typ];
696 desc->desc.id = atomic_inc_return(&idx) - 1;
698 init_data.consumer_supplies =
699 set_supply_map(&pdev->dev, desc->desc.name,
700 &init_data.num_consumer_supplies);
702 if (!init_data.consumer_supplies)
703 init_data.consumer_supplies = consumer_supplies_default;
705 debug0("regu %p (%s)\n", desc->regs, desc->desc.name);
706 rdev = regulator_register(&desc->desc, &pdev->dev, &init_data, 0, 0);
707 if (init_data.consumer_supplies != consumer_supplies_default)
708 kfree(init_data.consumer_supplies);
711 rdev->reg_data = rdev;
712 if (desc->desc.ops == &ldo_ops)
713 ldo_init_trimming(rdev);
714 rdev_init_debugfs(rdev);
721 * spreadtrum power regulators is intergrated on the chip, include LDOs and DCDCs.
722 * so i autogen all regulators non-variable description in plat or mach directory,
723 * which named __regulator_map.h, BUT register all in regulator driver probe func,
724 * just like other regulator vendor drivers.
726 static int __devinit sci_regulator_probe(struct platform_device *pdev)
728 debug0("platform device %p\n", pdev);
729 #include CONFIG_REGULATOR_SPRD_MAP
733 static struct platform_driver sci_regulator_driver = {
735 .name = "sprd-regulator",
736 .owner = THIS_MODULE,
738 .probe = sci_regulator_probe,
741 static int __init sci_regulator_init(void)
743 #ifdef CONFIG_DEBUG_FS
745 debugfs_create_dir(sci_regulator_driver.driver.name, NULL);
746 if (IS_ERR(debugfs_root) || !debugfs_root) {
747 pr_warn("%s: Failed to create debugfs directory\n",
748 sci_regulator_driver.driver.name);
752 return platform_driver_register(&sci_regulator_driver);
755 subsys_initcall(sci_regulator_init);
757 MODULE_LICENSE("GPL v2");
758 MODULE_DESCRIPTION("Spreadtrum voltage regulator driver");
759 MODULE_AUTHOR("robot <zhulin.lian@spreadtrum.com>");