2 * Driver for Regulator part of Palmas PMIC Chips
4 * Copyright 2011-2012 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/palmas.h>
33 static const struct regs_info palmas_regs_info[] = {
36 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
37 .ctrl_addr = PALMAS_SMPS12_CTRL,
38 .tstep_addr = PALMAS_SMPS12_TSTEP,
42 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
43 .ctrl_addr = PALMAS_SMPS12_CTRL,
44 .tstep_addr = PALMAS_SMPS12_TSTEP,
48 .vsel_addr = PALMAS_SMPS3_VOLTAGE,
49 .ctrl_addr = PALMAS_SMPS3_CTRL,
53 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
54 .ctrl_addr = PALMAS_SMPS45_CTRL,
55 .tstep_addr = PALMAS_SMPS45_TSTEP,
59 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
60 .ctrl_addr = PALMAS_SMPS45_CTRL,
61 .tstep_addr = PALMAS_SMPS45_TSTEP,
65 .vsel_addr = PALMAS_SMPS6_VOLTAGE,
66 .ctrl_addr = PALMAS_SMPS6_CTRL,
67 .tstep_addr = PALMAS_SMPS6_TSTEP,
71 .vsel_addr = PALMAS_SMPS7_VOLTAGE,
72 .ctrl_addr = PALMAS_SMPS7_CTRL,
76 .vsel_addr = PALMAS_SMPS8_VOLTAGE,
77 .ctrl_addr = PALMAS_SMPS8_CTRL,
78 .tstep_addr = PALMAS_SMPS8_TSTEP,
82 .vsel_addr = PALMAS_SMPS9_VOLTAGE,
83 .ctrl_addr = PALMAS_SMPS9_CTRL,
90 .vsel_addr = PALMAS_LDO1_VOLTAGE,
91 .ctrl_addr = PALMAS_LDO1_CTRL,
95 .vsel_addr = PALMAS_LDO2_VOLTAGE,
96 .ctrl_addr = PALMAS_LDO2_CTRL,
100 .vsel_addr = PALMAS_LDO3_VOLTAGE,
101 .ctrl_addr = PALMAS_LDO3_CTRL,
105 .vsel_addr = PALMAS_LDO4_VOLTAGE,
106 .ctrl_addr = PALMAS_LDO4_CTRL,
110 .vsel_addr = PALMAS_LDO5_VOLTAGE,
111 .ctrl_addr = PALMAS_LDO5_CTRL,
115 .vsel_addr = PALMAS_LDO6_VOLTAGE,
116 .ctrl_addr = PALMAS_LDO6_CTRL,
120 .vsel_addr = PALMAS_LDO7_VOLTAGE,
121 .ctrl_addr = PALMAS_LDO7_CTRL,
125 .vsel_addr = PALMAS_LDO8_VOLTAGE,
126 .ctrl_addr = PALMAS_LDO8_CTRL,
130 .vsel_addr = PALMAS_LDO9_VOLTAGE,
131 .ctrl_addr = PALMAS_LDO9_CTRL,
135 .vsel_addr = PALMAS_LDOLN_VOLTAGE,
136 .ctrl_addr = PALMAS_LDOLN_CTRL,
140 .vsel_addr = PALMAS_LDOUSB_VOLTAGE,
141 .ctrl_addr = PALMAS_LDOUSB_CTRL,
145 #define SMPS_CTRL_MODE_OFF 0x00
146 #define SMPS_CTRL_MODE_ON 0x01
147 #define SMPS_CTRL_MODE_ECO 0x02
148 #define SMPS_CTRL_MODE_PWM 0x03
150 /* These values are derived from the data sheet. And are the number of steps
151 * where there is a voltage change, the ranges at beginning and end of register
152 * max/min values where there are no change are ommitted.
154 * So they are basically (maxV-minV)/stepV
156 #define PALMAS_SMPS_NUM_VOLTAGES 116
157 #define PALMAS_SMPS10_NUM_VOLTAGES 2
158 #define PALMAS_LDO_NUM_VOLTAGES 50
160 #define SMPS10_VSEL (1<<3)
161 #define SMPS10_BOOST_EN (1<<2)
162 #define SMPS10_BYPASS_EN (1<<1)
163 #define SMPS10_SWITCH_EN (1<<0)
165 #define REGULATOR_SLAVE 0
167 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
172 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
174 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
177 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
182 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
184 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
187 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
192 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
194 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
197 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
202 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
204 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
207 static int palmas_is_enabled_smps(struct regulator_dev *dev)
209 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
210 int id = rdev_get_id(dev);
213 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
215 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
216 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
221 static int palmas_enable_smps(struct regulator_dev *dev)
223 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
224 int id = rdev_get_id(dev);
227 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
229 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
230 reg |= SMPS_CTRL_MODE_ON;
232 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
237 static int palmas_disable_smps(struct regulator_dev *dev)
239 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
240 int id = rdev_get_id(dev);
243 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
245 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
247 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
253 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
255 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
256 int id = rdev_get_id(dev);
259 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
260 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
263 case REGULATOR_MODE_NORMAL:
264 reg |= SMPS_CTRL_MODE_ON;
266 case REGULATOR_MODE_IDLE:
267 reg |= SMPS_CTRL_MODE_ECO;
269 case REGULATOR_MODE_FAST:
270 reg |= SMPS_CTRL_MODE_PWM;
275 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
280 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
282 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
283 int id = rdev_get_id(dev);
286 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
287 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
288 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
291 case SMPS_CTRL_MODE_ON:
292 return REGULATOR_MODE_NORMAL;
293 case SMPS_CTRL_MODE_ECO:
294 return REGULATOR_MODE_IDLE;
295 case SMPS_CTRL_MODE_PWM:
296 return REGULATOR_MODE_FAST;
302 static int palmas_list_voltage_smps(struct regulator_dev *dev,
305 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
306 int id = rdev_get_id(dev);
312 /* Read the multiplier set in VSEL register to return
313 * the correct voltage.
318 /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
319 * as defined in data sheet. RANGE is either x1 or x2
321 return (490000 + (selector * 10000)) * mult;
324 static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
326 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
327 int id = rdev_get_id(dev);
332 addr = palmas_regs_info[id].vsel_addr;
334 palmas_smps_read(pmic->palmas, addr, ®);
336 selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
338 /* Adjust selector to match list_voltage ranges */
339 if ((selector > 0) && (selector < 6))
350 static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
353 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
354 int id = rdev_get_id(dev);
355 unsigned int reg = 0;
358 addr = palmas_regs_info[id].vsel_addr;
360 /* Make sure we don't change the value of RANGE */
362 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
364 /* Adjust the linux selector into range used in VSEL register */
368 palmas_smps_write(pmic->palmas, addr, reg);
373 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
374 int min_uV, int max_uV)
376 struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
377 int id = rdev_get_id(rdev);
383 if (pmic->range[id]) { /* RANGE is x2 */
384 if (min_uV < 1000000)
386 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 1;
387 } else { /* RANGE is x1 */
390 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 1;
393 /* Map back into a voltage to verify we're still in bounds */
394 voltage = palmas_list_voltage_smps(rdev, ret);
395 if (voltage < min_uV || voltage > max_uV)
401 static struct regulator_ops palmas_ops_smps = {
402 .is_enabled = palmas_is_enabled_smps,
403 .enable = palmas_enable_smps,
404 .disable = palmas_disable_smps,
405 .set_mode = palmas_set_mode_smps,
406 .get_mode = palmas_get_mode_smps,
407 .get_voltage_sel = palmas_get_voltage_smps_sel,
408 .set_voltage_sel = palmas_set_voltage_smps_sel,
409 .list_voltage = palmas_list_voltage_smps,
410 .map_voltage = palmas_map_voltage_smps,
413 static struct regulator_ops palmas_ops_smps10 = {
414 .is_enabled = regulator_is_enabled_regmap,
415 .enable = regulator_enable_regmap,
416 .disable = regulator_disable_regmap,
417 .get_voltage_sel = regulator_get_voltage_sel_regmap,
418 .set_voltage_sel = regulator_set_voltage_sel_regmap,
419 .list_voltage = regulator_list_voltage_linear,
420 .map_voltage = regulator_map_voltage_linear,
423 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
425 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
426 int id = rdev_get_id(dev);
429 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
431 reg &= PALMAS_LDO1_CTRL_STATUS;
436 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
442 /* voltage is 0.85V + (selector * 0.05v) */
443 return 850000 + (selector * 50000);
446 static int palmas_map_voltage_ldo(struct regulator_dev *rdev,
447 int min_uV, int max_uV)
456 ret = DIV_ROUND_UP(min_uV - 900000, 50000) + 1;
458 /* Map back into a voltage to verify we're still in bounds */
459 voltage = palmas_list_voltage_ldo(rdev, ret);
460 if (voltage < min_uV || voltage > max_uV)
466 static struct regulator_ops palmas_ops_ldo = {
467 .is_enabled = palmas_is_enabled_ldo,
468 .enable = regulator_enable_regmap,
469 .disable = regulator_disable_regmap,
470 .get_voltage_sel = regulator_get_voltage_sel_regmap,
471 .set_voltage_sel = regulator_set_voltage_sel_regmap,
472 .list_voltage = palmas_list_voltage_ldo,
473 .map_voltage = palmas_map_voltage_ldo,
477 * setup the hardware based sleep configuration of the SMPS/LDO regulators
478 * from the platform data. This is different to the software based control
479 * supported by the regulator framework as it is controlled by toggling
480 * pins on the PMIC such as PREQ, SYSEN, ...
482 static int palmas_smps_init(struct palmas *palmas, int id,
483 struct palmas_reg_init *reg_init)
489 addr = palmas_regs_info[id].ctrl_addr;
491 ret = palmas_smps_read(palmas, addr, ®);
496 case PALMAS_REG_SMPS10:
497 if (reg_init->mode_sleep) {
498 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
499 reg |= reg_init->mode_sleep <<
500 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
504 if (reg_init->warm_reset)
505 reg |= PALMAS_SMPS12_CTRL_WR_S;
507 if (reg_init->roof_floor)
508 reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
510 if (reg_init->mode_sleep) {
511 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
512 reg |= reg_init->mode_sleep <<
513 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
517 ret = palmas_smps_write(palmas, addr, reg);
521 if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
522 addr = palmas_regs_info[id].tstep_addr;
524 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
526 ret = palmas_smps_write(palmas, addr, reg);
531 if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
532 addr = palmas_regs_info[id].vsel_addr;
534 reg = reg_init->vsel;
536 ret = palmas_smps_write(palmas, addr, reg);
545 static int palmas_ldo_init(struct palmas *palmas, int id,
546 struct palmas_reg_init *reg_init)
552 addr = palmas_regs_info[id].ctrl_addr;
554 ret = palmas_ldo_read(palmas, addr, ®);
558 if (reg_init->warm_reset)
559 reg |= PALMAS_LDO1_CTRL_WR_S;
561 if (reg_init->mode_sleep)
562 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
564 ret = palmas_ldo_write(palmas, addr, reg);
571 static __devinit int palmas_probe(struct platform_device *pdev)
573 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
574 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
575 struct regulator_dev *rdev;
576 struct regulator_config config = { };
577 struct palmas_pmic *pmic;
578 struct palmas_reg_init *reg_init;
580 unsigned int addr, reg;
584 if (!pdata->reg_data)
587 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
591 pmic->dev = &pdev->dev;
592 pmic->palmas = palmas;
594 platform_set_drvdata(pdev, pmic);
596 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, ®);
600 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
603 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
606 config.regmap = palmas->regmap[REGULATOR_SLAVE];
607 config.dev = &pdev->dev;
608 config.driver_data = pmic;
610 for (id = 0; id < PALMAS_REG_LDO1; id++) {
613 * Miss out regulators which are not available due
614 * to slaving configurations.
617 case PALMAS_REG_SMPS12:
618 case PALMAS_REG_SMPS3:
622 case PALMAS_REG_SMPS123:
626 case PALMAS_REG_SMPS45:
627 case PALMAS_REG_SMPS7:
631 case PALMAS_REG_SMPS457:
636 /* Register the regulators */
637 pmic->desc[id].name = palmas_regs_info[id].name;
638 pmic->desc[id].id = id;
641 case PALMAS_REG_SMPS10:
642 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
643 pmic->desc[id].ops = &palmas_ops_smps10;
644 pmic->desc[id].vsel_reg =
645 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
647 pmic->desc[id].vsel_mask = SMPS10_VSEL;
648 pmic->desc[id].enable_reg =
649 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
650 PALMAS_SMPS10_STATUS);
651 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
652 pmic->desc[id].min_uV = 3750000;
653 pmic->desc[id].uV_step = 1250000;
656 pmic->desc[id].ops = &palmas_ops_smps;
657 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
660 pmic->desc[id].type = REGULATOR_VOLTAGE;
661 pmic->desc[id].owner = THIS_MODULE;
663 /* Initialise sleep/init values from platform data */
664 if (pdata && pdata->reg_init) {
665 reg_init = pdata->reg_init[id];
667 ret = palmas_smps_init(palmas, id, reg_init);
669 goto err_unregister_regulator;
674 * read and store the RANGE bit for later use
675 * This must be done before regulator is probed otherwise
676 * we error in probe with unsuportable ranges.
678 if (id != PALMAS_REG_SMPS10) {
679 addr = palmas_regs_info[id].vsel_addr;
681 ret = palmas_smps_read(pmic->palmas, addr, ®);
683 goto err_unregister_regulator;
684 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
688 if (pdata && pdata->reg_data)
689 config.init_data = pdata->reg_data[id];
691 config.init_data = NULL;
693 rdev = regulator_register(&pmic->desc[id], &config);
696 "failed to register %s regulator\n",
699 goto err_unregister_regulator;
702 /* Save regulator for cleanup */
703 pmic->rdev[id] = rdev;
706 /* Start this loop from the id left from previous loop */
707 for (; id < PALMAS_NUM_REGS; id++) {
709 /* Miss out regulators which are not available due
710 * to alternate functions.
713 /* Register the regulators */
714 pmic->desc[id].name = palmas_regs_info[id].name;
715 pmic->desc[id].id = id;
716 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
718 pmic->desc[id].ops = &palmas_ops_ldo;
720 pmic->desc[id].type = REGULATOR_VOLTAGE;
721 pmic->desc[id].owner = THIS_MODULE;
722 pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
723 palmas_regs_info[id].vsel_addr);
724 pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
725 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
726 palmas_regs_info[id].ctrl_addr);
727 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
729 if (pdata && pdata->reg_data)
730 config.init_data = pdata->reg_data[id];
732 config.init_data = NULL;
734 rdev = regulator_register(&pmic->desc[id], &config);
737 "failed to register %s regulator\n",
740 goto err_unregister_regulator;
743 /* Save regulator for cleanup */
744 pmic->rdev[id] = rdev;
746 /* Initialise sleep/init values from platform data */
747 if (pdata->reg_init) {
748 reg_init = pdata->reg_init[id];
750 ret = palmas_ldo_init(palmas, id, reg_init);
752 regulator_unregister(pmic->rdev[id]);
753 goto err_unregister_regulator;
761 err_unregister_regulator:
763 regulator_unregister(pmic->rdev[id]);
767 static int __devexit palmas_remove(struct platform_device *pdev)
769 struct palmas_pmic *pmic = platform_get_drvdata(pdev);
772 for (id = 0; id < PALMAS_NUM_REGS; id++)
773 regulator_unregister(pmic->rdev[id]);
777 static struct platform_driver palmas_driver = {
779 .name = "palmas-pmic",
780 .owner = THIS_MODULE,
782 .probe = palmas_probe,
783 .remove = __devexit_p(palmas_remove),
786 static int __init palmas_init(void)
788 return platform_driver_register(&palmas_driver);
790 subsys_initcall(palmas_init);
792 static void __exit palmas_exit(void)
794 platform_driver_unregister(&palmas_driver);
796 module_exit(palmas_exit);
798 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
799 MODULE_DESCRIPTION("Palmas voltage regulator driver");
800 MODULE_LICENSE("GPL");
801 MODULE_ALIAS("platform:palmas-pmic");