2 * Regulator driver for Active-semi act8846 PMIC chip for rk29xx
4 * Copyright (C) 2010, 2011 ROCKCHIP, Inc.
6 * Based on act8846.c that is work by zhangqing<zhangqing@rock-chips.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/bug.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/act8846.h>
20 #include <mach/gpio.h>
21 #include <linux/delay.h>
22 #include <mach/iomux.h>
23 #include <linux/slab.h>
26 #define DBG(x...) printk(KERN_INFO x)
31 #define DBG_INFO(x...) printk(KERN_INFO x)
33 #define DBG_INFO(x...)
40 struct i2c_client *i2c;
42 struct regulator_dev **rdev;
45 static u8 act8846_reg_read(struct act8846 *act8846, u8 reg);
46 static int act8846_set_bits(struct act8846 *act8846, u8 reg, u16 mask, u16 val);
49 #define act8846_BUCK1_SET_VOL_BASE 0x10
50 #define act8846_BUCK2_SET_VOL_BASE 0x20
51 #define act8846_BUCK3_SET_VOL_BASE 0x30
52 #define act8846_BUCK4_SET_VOL_BASE 0x40
54 #define act8846_BUCK2_SLP_VOL_BASE 0x21
55 #define act8846_BUCK3_SLP_VOL_BASE 0x31
56 #define act8846_BUCK4_SLP_VOL_BASE 0x41
58 #define act8846_LDO1_SET_VOL_BASE 0x50
59 #define act8846_LDO2_SET_VOL_BASE 0x58
60 #define act8846_LDO3_SET_VOL_BASE 0x60
61 #define act8846_LDO4_SET_VOL_BASE 0x68
62 #define act8846_LDO5_SET_VOL_BASE 0x70
63 #define act8846_LDO6_SET_VOL_BASE 0x80
64 #define act8846_LDO7_SET_VOL_BASE 0x90
65 #define act8846_LDO8_SET_VOL_BASE 0xa0
66 //#define act8846_LDO9_SET_VOL_BASE 0xb1
68 #define act8846_BUCK1_CONTR_BASE 0x12
69 #define act8846_BUCK2_CONTR_BASE 0x22
70 #define act8846_BUCK3_CONTR_BASE 0x32
71 #define act8846_BUCK4_CONTR_BASE 0x42
73 #define act8846_LDO1_CONTR_BASE 0x51
74 #define act8846_LDO2_CONTR_BASE 0x59
75 #define act8846_LDO3_CONTR_BASE 0x61
76 #define act8846_LDO4_CONTR_BASE 0x69
77 #define act8846_LDO5_CONTR_BASE 0x71
78 #define act8846_LDO6_CONTR_BASE 0x81
79 #define act8846_LDO7_CONTR_BASE 0x91
80 #define act8846_LDO8_CONTR_BASE 0xa1
81 //#define act8846_LDO9_CONTR_BASE 0xb1
83 #define BUCK_VOL_MASK 0x3f
84 #define LDO_VOL_MASK 0x3f
86 #define VOL_MIN_IDX 0x00
87 #define VOL_MAX_IDX 0x3f
89 const static int buck_set_vol_base_addr[] = {
90 act8846_BUCK1_SET_VOL_BASE,
91 act8846_BUCK2_SET_VOL_BASE,
92 act8846_BUCK3_SET_VOL_BASE,
93 act8846_BUCK4_SET_VOL_BASE,
95 const static int buck_contr_base_addr[] = {
96 act8846_BUCK1_CONTR_BASE,
97 act8846_BUCK2_CONTR_BASE,
98 act8846_BUCK3_CONTR_BASE,
99 act8846_BUCK4_CONTR_BASE,
101 #define act8846_BUCK_SET_VOL_REG(x) (buck_set_vol_base_addr[x])
102 #define act8846_BUCK_CONTR_REG(x) (buck_contr_base_addr[x])
105 const static int ldo_set_vol_base_addr[] = {
106 act8846_LDO1_SET_VOL_BASE,
107 act8846_LDO2_SET_VOL_BASE,
108 act8846_LDO3_SET_VOL_BASE,
109 act8846_LDO4_SET_VOL_BASE,
110 act8846_LDO5_SET_VOL_BASE,
111 act8846_LDO6_SET_VOL_BASE,
112 act8846_LDO7_SET_VOL_BASE,
113 act8846_LDO8_SET_VOL_BASE,
114 // act8846_LDO9_SET_VOL_BASE,
116 const static int ldo_contr_base_addr[] = {
117 act8846_LDO1_CONTR_BASE,
118 act8846_LDO2_CONTR_BASE,
119 act8846_LDO3_CONTR_BASE,
120 act8846_LDO4_CONTR_BASE,
121 act8846_LDO5_CONTR_BASE,
122 act8846_LDO6_CONTR_BASE,
123 act8846_LDO7_CONTR_BASE,
124 act8846_LDO8_CONTR_BASE,
125 // act8846_LDO9_CONTR_BASE,
127 #define act8846_LDO_SET_VOL_REG(x) (ldo_set_vol_base_addr[x])
128 #define act8846_LDO_CONTR_REG(x) (ldo_contr_base_addr[x])
130 const static int buck_voltage_map[] = {
131 600, 625, 650, 675, 700, 725, 750, 775,
132 800, 825, 850, 875, 900, 925, 950, 975,
133 1000, 1025, 1050, 1075, 1100, 1125, 1150,
134 1175, 1200, 1250, 1300, 1350, 1400, 1450,
135 1500, 1550, 1600, 1650, 1700, 1750, 1800,
136 1850, 1900, 1950, 2000, 2050, 2100, 2150,
137 2200, 2250, 2300, 2350, 2400, 2500, 2600,
138 2700, 2800, 2850, 2900, 3000, 3100, 3200,
139 3300, 3400, 3500, 3600, 3700, 3800, 3900,
142 const static int ldo_voltage_map[] = {
143 600, 625, 650, 675, 700, 725, 750, 775,
144 800, 825, 850, 875, 900, 925, 950, 975,
145 1000, 1025, 1050, 1075, 1100, 1125, 1150,
146 1175, 1200, 1250, 1300, 1350, 1400, 1450,
147 1500, 1550, 1600, 1650, 1700, 1750, 1800,
148 1850, 1900, 1950, 2000, 2050, 2100, 2150,
149 2200, 2250, 2300, 2350, 2400, 2500, 2600,
150 2700, 2800, 2850, 2900, 3000, 3100, 3200,
151 3300, 3400, 3500, 3600, 3700, 3800, 3900,
154 static int act8846_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
156 if (index >= ARRAY_SIZE(ldo_voltage_map))
158 return 1000 * ldo_voltage_map[index];
160 static int act8846_ldo_is_enabled(struct regulator_dev *dev)
162 struct act8846 *act8846 = rdev_get_drvdata(dev);
163 int ldo = rdev_get_id(dev) - ACT8846_LDO1;
166 val = act8846_reg_read(act8846, act8846_LDO_CONTR_REG(ldo));
175 static int act8846_ldo_enable(struct regulator_dev *dev)
177 struct act8846 *act8846 = rdev_get_drvdata(dev);
178 int ldo= rdev_get_id(dev) - ACT8846_LDO1;
181 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0x80);
184 static int act8846_ldo_disable(struct regulator_dev *dev)
186 struct act8846 *act8846 = rdev_get_drvdata(dev);
187 int ldo= rdev_get_id(dev) - ACT8846_LDO1;
190 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0);
193 static int act8846_ldo_get_voltage(struct regulator_dev *dev)
195 struct act8846 *act8846 = rdev_get_drvdata(dev);
196 int ldo= rdev_get_id(dev) - ACT8846_LDO1;
199 reg = act8846_reg_read(act8846,act8846_LDO_SET_VOL_REG(ldo));
201 val = 1000 * ldo_voltage_map[reg];
204 static int act8846_ldo_set_voltage(struct regulator_dev *dev,
205 int min_uV, int max_uV,unsigned *selector)
207 struct act8846 *act8846 = rdev_get_drvdata(dev);
208 int ldo= rdev_get_id(dev) - ACT8846_LDO1;
209 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
210 const int *vol_map =ldo_voltage_map;
213 if (min_vol < vol_map[VOL_MIN_IDX] ||
214 min_vol > vol_map[VOL_MAX_IDX])
217 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
218 if (vol_map[val] >= min_vol)
222 if (vol_map[val] > max_vol)
225 ret = act8846_set_bits(act8846, act8846_LDO_SET_VOL_REG(ldo),
230 static unsigned int act8846_ldo_get_mode(struct regulator_dev *dev)
232 struct act8846 *act8846 = rdev_get_drvdata(dev);
233 int ldo = rdev_get_id(dev) - ACT8846_LDO1;
236 val = act8846_reg_read(act8846, act8846_LDO_CONTR_REG(ldo));
242 return REGULATOR_MODE_NORMAL;
244 return REGULATOR_MODE_STANDBY;
247 static int act8846_ldo_set_mode(struct regulator_dev *dev, unsigned int mode)
249 struct act8846 *act8846 = rdev_get_drvdata(dev);
250 int ldo = rdev_get_id(dev) - ACT8846_LDO1;
254 case REGULATOR_MODE_NORMAL:
255 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, mask);
256 case REGULATOR_MODE_STANDBY:
257 return act8846_set_bits(act8846, act8846_LDO_CONTR_REG(ldo), mask, 0);
259 printk("error:pmu_act8846 only lowpower and nomal mode\n");
265 static struct regulator_ops act8846_ldo_ops = {
266 .set_voltage = act8846_ldo_set_voltage,
267 .get_voltage = act8846_ldo_get_voltage,
268 .list_voltage = act8846_ldo_list_voltage,
269 .is_enabled = act8846_ldo_is_enabled,
270 .enable = act8846_ldo_enable,
271 .disable = act8846_ldo_disable,
272 .get_mode = act8846_ldo_get_mode,
273 .set_mode = act8846_ldo_set_mode,
277 static int act8846_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
279 if (index >= ARRAY_SIZE(buck_voltage_map))
281 return 1000 * buck_voltage_map[index];
283 static int act8846_dcdc_is_enabled(struct regulator_dev *dev)
285 struct act8846 *act8846 = rdev_get_drvdata(dev);
286 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
289 val = act8846_reg_read(act8846, act8846_BUCK_CONTR_REG(buck));
298 static int act8846_dcdc_enable(struct regulator_dev *dev)
300 struct act8846 *act8846 = rdev_get_drvdata(dev);
301 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
303 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0x80);
306 static int act8846_dcdc_disable(struct regulator_dev *dev)
308 struct act8846 *act8846 = rdev_get_drvdata(dev);
309 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
311 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0);
313 static int act8846_dcdc_get_voltage(struct regulator_dev *dev)
315 struct act8846 *act8846 = rdev_get_drvdata(dev);
316 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
319 reg = act8846_reg_read(act8846,act8846_BUCK_SET_VOL_REG(buck));
320 reg &= BUCK_VOL_MASK;
322 val = 1000 * buck_voltage_map[reg];
326 static int act8846_dcdc_set_voltage(struct regulator_dev *dev,
327 int min_uV, int max_uV,unsigned *selector)
329 struct act8846 *act8846 = rdev_get_drvdata(dev);
330 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
331 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
332 const int *vol_map = buck_voltage_map;
336 DBG("%s, min_uV = %d, max_uV = %d!\n", __func__, min_uV, max_uV);
337 if (min_vol < vol_map[VOL_MIN_IDX] ||
338 min_vol > vol_map[VOL_MAX_IDX])
341 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
342 if (vol_map[val] >= min_vol)
346 if (vol_map[val] > max_vol)
347 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
348 ret = act8846_set_bits(act8846, act8846_BUCK_SET_VOL_REG(buck),
352 static int act8846_dcdc_set_sleep_voltage(struct regulator_dev *dev,
355 struct act8846 *act8846 = rdev_get_drvdata(dev);
356 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
357 int min_vol = uV / 1000,max_vol = uV / 1000;
358 const int *vol_map = buck_voltage_map;
362 DBG("%s, min_uV = %d, max_uV = %d!\n", __func__, min_uV, max_uV);
363 if (min_vol < vol_map[VOL_MIN_IDX] ||
364 min_vol > vol_map[VOL_MAX_IDX])
367 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
368 if (vol_map[val] >= min_vol)
372 if (vol_map[val] > max_vol)
373 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
374 ret = act8846_set_bits(act8846, (act8846_BUCK_SET_VOL_REG(buck) + 0x01),
378 static unsigned int act8846_dcdc_get_mode(struct regulator_dev *dev)
380 struct act8846 *act8846 = rdev_get_drvdata(dev);
381 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
384 val = act8846_reg_read(act8846, act8846_BUCK_CONTR_REG(buck));
390 return REGULATOR_MODE_NORMAL;
392 return REGULATOR_MODE_STANDBY;
395 static int act8846_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
397 struct act8846 *act8846 = rdev_get_drvdata(dev);
398 int buck = rdev_get_id(dev) - ACT8846_DCDC1;
402 case REGULATOR_MODE_STANDBY:
403 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, 0);
404 case REGULATOR_MODE_NORMAL:
405 return act8846_set_bits(act8846, act8846_BUCK_CONTR_REG(buck), mask, mask);
407 printk("error:pmu_act8846 only powersave and pwm mode\n");
413 static struct regulator_ops act8846_dcdc_ops = {
414 .set_voltage = act8846_dcdc_set_voltage,
415 .get_voltage = act8846_dcdc_get_voltage,
416 .list_voltage= act8846_dcdc_list_voltage,
417 .is_enabled = act8846_dcdc_is_enabled,
418 .enable = act8846_dcdc_enable,
419 .disable = act8846_dcdc_disable,
420 .get_mode = act8846_dcdc_get_mode,
421 .set_mode = act8846_dcdc_set_mode,
422 .set_suspend_voltage = act8846_dcdc_set_sleep_voltage,
424 static struct regulator_desc regulators[] = {
429 .ops = &act8846_dcdc_ops,
430 .n_voltages = ARRAY_SIZE(buck_voltage_map),
431 .type = REGULATOR_VOLTAGE,
432 .owner = THIS_MODULE,
437 .ops = &act8846_dcdc_ops,
438 .n_voltages = ARRAY_SIZE(buck_voltage_map),
439 .type = REGULATOR_VOLTAGE,
440 .owner = THIS_MODULE,
445 .ops = &act8846_dcdc_ops,
446 .n_voltages = ARRAY_SIZE(buck_voltage_map),
447 .type = REGULATOR_VOLTAGE,
448 .owner = THIS_MODULE,
453 .ops = &act8846_dcdc_ops,
454 .n_voltages = ARRAY_SIZE(buck_voltage_map),
455 .type = REGULATOR_VOLTAGE,
456 .owner = THIS_MODULE,
462 .ops = &act8846_ldo_ops,
463 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
464 .type = REGULATOR_VOLTAGE,
465 .owner = THIS_MODULE,
470 .ops = &act8846_ldo_ops,
471 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
472 .type = REGULATOR_VOLTAGE,
473 .owner = THIS_MODULE,
478 .ops = &act8846_ldo_ops,
479 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
480 .type = REGULATOR_VOLTAGE,
481 .owner = THIS_MODULE,
486 .ops = &act8846_ldo_ops,
487 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
488 .type = REGULATOR_VOLTAGE,
489 .owner = THIS_MODULE,
495 .ops = &act8846_ldo_ops,
496 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
497 .type = REGULATOR_VOLTAGE,
498 .owner = THIS_MODULE,
503 .ops = &act8846_ldo_ops,
504 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
505 .type = REGULATOR_VOLTAGE,
506 .owner = THIS_MODULE,
511 .ops = &act8846_ldo_ops,
512 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
513 .type = REGULATOR_VOLTAGE,
514 .owner = THIS_MODULE,
519 .ops = &act8846_ldo_ops,
520 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
521 .type = REGULATOR_VOLTAGE,
522 .owner = THIS_MODULE,
527 .ops = &act8846_ldo_ops,
528 .n_voltages = ARRAY_SIZE(ldo_voltage_map),
529 .type = REGULATOR_VOLTAGE,
530 .owner = THIS_MODULE,
537 static int act8846_i2c_read(struct i2c_client *i2c, char reg, int count, u16 *dest)
540 struct i2c_adapter *adap;
541 struct i2c_msg msgs[2];
551 msgs[0].addr = i2c->addr;
553 msgs[0].flags = i2c->flags;
555 msgs[0].scl_rate = 200*1000;
557 msgs[1].buf = (u8 *)dest;
558 msgs[1].addr = i2c->addr;
559 msgs[1].flags = i2c->flags | I2C_M_RD;
561 msgs[1].scl_rate = 200*1000;
562 ret = i2c_transfer(adap, msgs, 2);
564 DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
569 static int act8846_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
573 struct i2c_adapter *adap;
586 msg.addr = i2c->addr;
587 msg.buf = &tx_buf[0];
589 msg.flags = i2c->flags;
590 msg.scl_rate = 200*1000;
592 ret = i2c_transfer(adap, &msg, 1);
596 static u8 act8846_reg_read(struct act8846 *act8846, u8 reg)
600 mutex_lock(&act8846->io_lock);
602 act8846_i2c_read(act8846->i2c, reg, 1, &val);
604 DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
606 mutex_unlock(&act8846->io_lock);
611 static int act8846_set_bits(struct act8846 *act8846, u8 reg, u16 mask, u16 val)
616 mutex_lock(&act8846->io_lock);
618 ret = act8846_i2c_read(act8846->i2c, reg, 1, &tmp);
619 DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
620 tmp = (tmp & ~mask) | val;
622 ret = act8846_i2c_write(act8846->i2c, reg, 1, tmp);
623 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
625 act8846_i2c_read(act8846->i2c, reg, 1, &tmp);
626 DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
627 mutex_unlock(&act8846->io_lock);
631 static int __devinit setup_regulators(struct act8846 *act8846, struct act8846_platform_data *pdata)
635 act8846->num_regulators = pdata->num_regulators;
636 act8846->rdev = kcalloc(pdata->num_regulators,
637 sizeof(struct regulator_dev *), GFP_KERNEL);
638 if (!act8846->rdev) {
641 /* Instantiate the regulators */
642 for (i = 0; i < pdata->num_regulators; i++) {
643 int id = pdata->regulators[i].id;
644 act8846->rdev[i] = regulator_register(®ulators[id],
645 act8846->dev, pdata->regulators[i].initdata, act8846);
647 if (IS_ERR(act8846->rdev[i])) {
648 err = PTR_ERR(act8846->rdev[i]);
649 dev_err(act8846->dev, "regulator init failed: %d\n",
658 regulator_unregister(act8846->rdev[i]);
659 kfree(act8846->rdev);
660 act8846->rdev = NULL;
664 static int __devinit act8846_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
666 struct act8846 *act8846;
667 struct act8846_platform_data *pdata = i2c->dev.platform_data;
669 act8846 = kzalloc(sizeof(struct act8846), GFP_KERNEL);
670 if (act8846 == NULL) {
675 act8846->dev = &i2c->dev;
676 i2c_set_clientdata(i2c, act8846);
677 mutex_init(&act8846->io_lock);
679 ret = act8846_reg_read(act8846,0x22);
680 if ((ret < 0) || (ret == 0xff)){
681 printk("The device is not act8846 \n");
686 ret = setup_regulators(act8846, pdata);
690 dev_warn(act8846->dev, "No platform init data supplied\n");
692 pdata->set_init(act8846);
701 static int __devexit act8846_i2c_remove(struct i2c_client *i2c)
703 struct act8846 *act8846 = i2c_get_clientdata(i2c);
706 for (i = 0; i < act8846->num_regulators; i++)
707 if (act8846->rdev[i])
708 regulator_unregister(act8846->rdev[i]);
709 kfree(act8846->rdev);
710 i2c_set_clientdata(i2c, NULL);
716 static const struct i2c_device_id act8846_i2c_id[] = {
721 MODULE_DEVICE_TABLE(i2c, act8846_i2c_id);
723 static struct i2c_driver act8846_i2c_driver = {
726 .owner = THIS_MODULE,
728 .probe = act8846_i2c_probe,
729 .remove = __devexit_p(act8846_i2c_remove),
730 .id_table = act8846_i2c_id,
733 static int __init act8846_module_init(void)
736 ret = i2c_add_driver(&act8846_i2c_driver);
738 pr_err("Failed to register I2C driver: %d\n", ret);
741 //module_init(act8846_module_init);
742 //subsys_initcall(act8846_module_init);
743 //rootfs_initcall(act8846_module_init);
744 subsys_initcall_sync(act8846_module_init);
746 static void __exit act8846_module_exit(void)
748 i2c_del_driver(&act8846_i2c_driver);
750 module_exit(act8846_module_exit);
752 MODULE_LICENSE("GPL");
753 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
754 MODULE_DESCRIPTION("act8846 PMIC driver");