2 * Driver for Regulator part of Palmas PMIC Chips
4 * Copyright 2011-2012 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Ian Lartey <ian@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/slab.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/palmas.h>
27 #include <linux/of_platform.h>
28 #include <linux/regulator/of_regulator.h>
37 static const struct regs_info palmas_regs_info[] = {
40 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
41 .ctrl_addr = PALMAS_SMPS12_CTRL,
42 .tstep_addr = PALMAS_SMPS12_TSTEP,
46 .vsel_addr = PALMAS_SMPS12_VOLTAGE,
47 .ctrl_addr = PALMAS_SMPS12_CTRL,
48 .tstep_addr = PALMAS_SMPS12_TSTEP,
52 .vsel_addr = PALMAS_SMPS3_VOLTAGE,
53 .ctrl_addr = PALMAS_SMPS3_CTRL,
57 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
58 .ctrl_addr = PALMAS_SMPS45_CTRL,
59 .tstep_addr = PALMAS_SMPS45_TSTEP,
63 .vsel_addr = PALMAS_SMPS45_VOLTAGE,
64 .ctrl_addr = PALMAS_SMPS45_CTRL,
65 .tstep_addr = PALMAS_SMPS45_TSTEP,
69 .vsel_addr = PALMAS_SMPS6_VOLTAGE,
70 .ctrl_addr = PALMAS_SMPS6_CTRL,
71 .tstep_addr = PALMAS_SMPS6_TSTEP,
75 .vsel_addr = PALMAS_SMPS7_VOLTAGE,
76 .ctrl_addr = PALMAS_SMPS7_CTRL,
80 .vsel_addr = PALMAS_SMPS8_VOLTAGE,
81 .ctrl_addr = PALMAS_SMPS8_CTRL,
82 .tstep_addr = PALMAS_SMPS8_TSTEP,
86 .vsel_addr = PALMAS_SMPS9_VOLTAGE,
87 .ctrl_addr = PALMAS_SMPS9_CTRL,
94 .vsel_addr = PALMAS_LDO1_VOLTAGE,
95 .ctrl_addr = PALMAS_LDO1_CTRL,
99 .vsel_addr = PALMAS_LDO2_VOLTAGE,
100 .ctrl_addr = PALMAS_LDO2_CTRL,
104 .vsel_addr = PALMAS_LDO3_VOLTAGE,
105 .ctrl_addr = PALMAS_LDO3_CTRL,
109 .vsel_addr = PALMAS_LDO4_VOLTAGE,
110 .ctrl_addr = PALMAS_LDO4_CTRL,
114 .vsel_addr = PALMAS_LDO5_VOLTAGE,
115 .ctrl_addr = PALMAS_LDO5_CTRL,
119 .vsel_addr = PALMAS_LDO6_VOLTAGE,
120 .ctrl_addr = PALMAS_LDO6_CTRL,
124 .vsel_addr = PALMAS_LDO7_VOLTAGE,
125 .ctrl_addr = PALMAS_LDO7_CTRL,
129 .vsel_addr = PALMAS_LDO8_VOLTAGE,
130 .ctrl_addr = PALMAS_LDO8_CTRL,
134 .vsel_addr = PALMAS_LDO9_VOLTAGE,
135 .ctrl_addr = PALMAS_LDO9_CTRL,
139 .vsel_addr = PALMAS_LDOLN_VOLTAGE,
140 .ctrl_addr = PALMAS_LDOLN_CTRL,
144 .vsel_addr = PALMAS_LDOUSB_VOLTAGE,
145 .ctrl_addr = PALMAS_LDOUSB_CTRL,
149 #define SMPS_CTRL_MODE_OFF 0x00
150 #define SMPS_CTRL_MODE_ON 0x01
151 #define SMPS_CTRL_MODE_ECO 0x02
152 #define SMPS_CTRL_MODE_PWM 0x03
154 /* These values are derived from the data sheet. And are the number of steps
155 * where there is a voltage change, the ranges at beginning and end of register
156 * max/min values where there are no change are ommitted.
158 * So they are basically (maxV-minV)/stepV
160 #define PALMAS_SMPS_NUM_VOLTAGES 117
161 #define PALMAS_SMPS10_NUM_VOLTAGES 2
162 #define PALMAS_LDO_NUM_VOLTAGES 50
164 #define SMPS10_VSEL (1<<3)
165 #define SMPS10_BOOST_EN (1<<2)
166 #define SMPS10_BYPASS_EN (1<<1)
167 #define SMPS10_SWITCH_EN (1<<0)
169 #define REGULATOR_SLAVE 0
171 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
176 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
178 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
181 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
186 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
188 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
191 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
196 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
198 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
201 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
206 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
208 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
211 static int palmas_is_enabled_smps(struct regulator_dev *dev)
213 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
214 int id = rdev_get_id(dev);
217 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
219 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
220 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
225 static int palmas_enable_smps(struct regulator_dev *dev)
227 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
228 int id = rdev_get_id(dev);
231 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
233 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
234 reg |= SMPS_CTRL_MODE_ON;
236 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
241 static int palmas_disable_smps(struct regulator_dev *dev)
243 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
244 int id = rdev_get_id(dev);
247 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
249 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
251 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
257 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
259 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
260 int id = rdev_get_id(dev);
263 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
264 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
267 case REGULATOR_MODE_NORMAL:
268 reg |= SMPS_CTRL_MODE_ON;
270 case REGULATOR_MODE_IDLE:
271 reg |= SMPS_CTRL_MODE_ECO;
273 case REGULATOR_MODE_FAST:
274 reg |= SMPS_CTRL_MODE_PWM;
279 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
284 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
286 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
287 int id = rdev_get_id(dev);
290 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
291 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
292 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
295 case SMPS_CTRL_MODE_ON:
296 return REGULATOR_MODE_NORMAL;
297 case SMPS_CTRL_MODE_ECO:
298 return REGULATOR_MODE_IDLE;
299 case SMPS_CTRL_MODE_PWM:
300 return REGULATOR_MODE_FAST;
306 static int palmas_list_voltage_smps(struct regulator_dev *dev,
309 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
310 int id = rdev_get_id(dev);
313 /* Read the multiplier set in VSEL register to return
314 * the correct voltage.
321 else if (selector < 6)
322 return 500000 * mult;
324 /* Voltage is linear mapping starting from selector 6,
325 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
326 * RANGE is either x1 or x2
328 return (490000 + ((selector - 5) * 10000)) * mult;
331 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
332 int min_uV, int max_uV)
334 struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
335 int id = rdev_get_id(rdev);
341 if (pmic->range[id]) { /* RANGE is x2 */
342 if (min_uV < 1000000)
344 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
345 } else { /* RANGE is x1 */
348 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
351 /* Map back into a voltage to verify we're still in bounds */
352 voltage = palmas_list_voltage_smps(rdev, ret);
353 if (voltage < min_uV || voltage > max_uV)
359 static struct regulator_ops palmas_ops_smps = {
360 .is_enabled = palmas_is_enabled_smps,
361 .enable = palmas_enable_smps,
362 .disable = palmas_disable_smps,
363 .set_mode = palmas_set_mode_smps,
364 .get_mode = palmas_get_mode_smps,
365 .get_voltage_sel = regulator_get_voltage_sel_regmap,
366 .set_voltage_sel = regulator_set_voltage_sel_regmap,
367 .list_voltage = palmas_list_voltage_smps,
368 .map_voltage = palmas_map_voltage_smps,
371 static struct regulator_ops palmas_ops_smps10 = {
372 .is_enabled = regulator_is_enabled_regmap,
373 .enable = regulator_enable_regmap,
374 .disable = regulator_disable_regmap,
375 .get_voltage_sel = regulator_get_voltage_sel_regmap,
376 .set_voltage_sel = regulator_set_voltage_sel_regmap,
377 .list_voltage = regulator_list_voltage_linear,
378 .map_voltage = regulator_map_voltage_linear,
381 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
383 struct palmas_pmic *pmic = rdev_get_drvdata(dev);
384 int id = rdev_get_id(dev);
387 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®);
389 reg &= PALMAS_LDO1_CTRL_STATUS;
394 static struct regulator_ops palmas_ops_ldo = {
395 .is_enabled = palmas_is_enabled_ldo,
396 .enable = regulator_enable_regmap,
397 .disable = regulator_disable_regmap,
398 .get_voltage_sel = regulator_get_voltage_sel_regmap,
399 .set_voltage_sel = regulator_set_voltage_sel_regmap,
400 .list_voltage = regulator_list_voltage_linear,
401 .map_voltage = regulator_map_voltage_linear,
405 * setup the hardware based sleep configuration of the SMPS/LDO regulators
406 * from the platform data. This is different to the software based control
407 * supported by the regulator framework as it is controlled by toggling
408 * pins on the PMIC such as PREQ, SYSEN, ...
410 static int palmas_smps_init(struct palmas *palmas, int id,
411 struct palmas_reg_init *reg_init)
417 addr = palmas_regs_info[id].ctrl_addr;
419 ret = palmas_smps_read(palmas, addr, ®);
424 case PALMAS_REG_SMPS10:
425 if (reg_init->mode_sleep) {
426 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
427 reg |= reg_init->mode_sleep <<
428 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
432 if (reg_init->warm_reset)
433 reg |= PALMAS_SMPS12_CTRL_WR_S;
435 if (reg_init->roof_floor)
436 reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
438 if (reg_init->mode_sleep) {
439 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
440 reg |= reg_init->mode_sleep <<
441 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
445 ret = palmas_smps_write(palmas, addr, reg);
449 if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
450 addr = palmas_regs_info[id].tstep_addr;
452 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
454 ret = palmas_smps_write(palmas, addr, reg);
459 if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
460 addr = palmas_regs_info[id].vsel_addr;
462 reg = reg_init->vsel;
464 ret = palmas_smps_write(palmas, addr, reg);
473 static int palmas_ldo_init(struct palmas *palmas, int id,
474 struct palmas_reg_init *reg_init)
480 addr = palmas_regs_info[id].ctrl_addr;
482 ret = palmas_ldo_read(palmas, addr, ®);
486 if (reg_init->warm_reset)
487 reg |= PALMAS_LDO1_CTRL_WR_S;
489 if (reg_init->mode_sleep)
490 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
492 ret = palmas_ldo_write(palmas, addr, reg);
499 static struct of_regulator_match palmas_matches[] = {
500 { .name = "smps12", },
501 { .name = "smps123", },
502 { .name = "smps3", },
503 { .name = "smps45", },
504 { .name = "smps457", },
505 { .name = "smps6", },
506 { .name = "smps7", },
507 { .name = "smps8", },
508 { .name = "smps9", },
509 { .name = "smps10", },
519 { .name = "ldoln", },
520 { .name = "ldousb", },
523 static void palmas_dt_to_pdata(struct device *dev,
524 struct device_node *node,
525 struct palmas_pmic_platform_data *pdata)
527 struct device_node *regulators;
531 node = of_node_get(node);
532 regulators = of_find_node_by_name(node, "regulators");
534 dev_info(dev, "regulator node not found\n");
538 ret = of_regulator_match(dev, regulators, palmas_matches,
540 of_node_put(regulators);
542 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
546 for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
547 if (!palmas_matches[idx].init_data ||
548 !palmas_matches[idx].of_node)
551 pdata->reg_data[idx] = palmas_matches[idx].init_data;
553 pdata->reg_init[idx] = devm_kzalloc(dev,
554 sizeof(struct palmas_reg_init), GFP_KERNEL);
556 ret = of_property_read_u32(palmas_matches[idx].of_node,
557 "ti,warm_reset", &prop);
559 pdata->reg_init[idx]->warm_reset = prop;
561 ret = of_property_read_u32(palmas_matches[idx].of_node,
562 "ti,roof_floor", &prop);
564 pdata->reg_init[idx]->roof_floor = prop;
566 ret = of_property_read_u32(palmas_matches[idx].of_node,
567 "ti,mode_sleep", &prop);
569 pdata->reg_init[idx]->mode_sleep = prop;
571 ret = of_property_read_u32(palmas_matches[idx].of_node,
574 pdata->reg_init[idx]->tstep = prop;
576 ret = of_property_read_u32(palmas_matches[idx].of_node,
579 pdata->reg_init[idx]->vsel = prop;
582 ret = of_property_read_u32(node, "ti,ldo6_vibrator", &prop);
584 pdata->ldo6_vibrator = prop;
588 static int palmas_probe(struct platform_device *pdev)
590 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
591 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
592 struct device_node *node = pdev->dev.of_node;
593 struct regulator_dev *rdev;
594 struct regulator_config config = { };
595 struct palmas_pmic *pmic;
596 struct palmas_reg_init *reg_init;
598 unsigned int addr, reg;
600 if (node && !pdata) {
601 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
606 palmas_dt_to_pdata(&pdev->dev, node, pdata);
609 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
613 pmic->dev = &pdev->dev;
614 pmic->palmas = palmas;
616 platform_set_drvdata(pdev, pmic);
618 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, ®);
622 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
625 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
628 config.regmap = palmas->regmap[REGULATOR_SLAVE];
629 config.dev = &pdev->dev;
630 config.driver_data = pmic;
632 for (id = 0; id < PALMAS_REG_LDO1; id++) {
635 * Miss out regulators which are not available due
636 * to slaving configurations.
639 case PALMAS_REG_SMPS12:
640 case PALMAS_REG_SMPS3:
644 case PALMAS_REG_SMPS123:
648 case PALMAS_REG_SMPS45:
649 case PALMAS_REG_SMPS7:
653 case PALMAS_REG_SMPS457:
658 /* Initialise sleep/init values from platform data */
659 if (pdata && pdata->reg_init[id]) {
660 reg_init = pdata->reg_init[id];
661 ret = palmas_smps_init(palmas, id, reg_init);
663 goto err_unregister_regulator;
666 /* Register the regulators */
667 pmic->desc[id].name = palmas_regs_info[id].name;
668 pmic->desc[id].id = id;
671 case PALMAS_REG_SMPS10:
672 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
673 pmic->desc[id].ops = &palmas_ops_smps10;
674 pmic->desc[id].vsel_reg =
675 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
677 pmic->desc[id].vsel_mask = SMPS10_VSEL;
678 pmic->desc[id].enable_reg =
679 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
680 PALMAS_SMPS10_STATUS);
681 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
682 pmic->desc[id].min_uV = 3750000;
683 pmic->desc[id].uV_step = 1250000;
687 * Read and store the RANGE bit for later use
688 * This must be done before regulator is probed,
689 * otherwise we error in probe with unsupportable ranges.
691 addr = palmas_regs_info[id].vsel_addr;
693 ret = palmas_smps_read(pmic->palmas, addr, ®);
695 goto err_unregister_regulator;
696 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
699 pmic->desc[id].ops = &palmas_ops_smps;
700 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
701 pmic->desc[id].vsel_reg =
702 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
703 palmas_regs_info[id].vsel_addr);
704 pmic->desc[id].vsel_mask =
705 PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
708 pmic->desc[id].type = REGULATOR_VOLTAGE;
709 pmic->desc[id].owner = THIS_MODULE;
712 config.init_data = pdata->reg_data[id];
714 config.init_data = NULL;
716 config.of_node = palmas_matches[id].of_node;
718 rdev = regulator_register(&pmic->desc[id], &config);
721 "failed to register %s regulator\n",
724 goto err_unregister_regulator;
727 /* Save regulator for cleanup */
728 pmic->rdev[id] = rdev;
731 /* Start this loop from the id left from previous loop */
732 for (; id < PALMAS_NUM_REGS; id++) {
734 /* Miss out regulators which are not available due
735 * to alternate functions.
738 /* Register the regulators */
739 pmic->desc[id].name = palmas_regs_info[id].name;
740 pmic->desc[id].id = id;
741 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
743 pmic->desc[id].ops = &palmas_ops_ldo;
745 pmic->desc[id].type = REGULATOR_VOLTAGE;
746 pmic->desc[id].owner = THIS_MODULE;
747 pmic->desc[id].min_uV = 900000;
748 pmic->desc[id].uV_step = 50000;
749 pmic->desc[id].linear_min_sel = 1;
750 pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
751 palmas_regs_info[id].vsel_addr);
752 pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
753 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
754 palmas_regs_info[id].ctrl_addr);
755 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
758 config.init_data = pdata->reg_data[id];
760 config.init_data = NULL;
762 config.of_node = palmas_matches[id].of_node;
764 rdev = regulator_register(&pmic->desc[id], &config);
767 "failed to register %s regulator\n",
770 goto err_unregister_regulator;
773 /* Save regulator for cleanup */
774 pmic->rdev[id] = rdev;
776 /* Initialise sleep/init values from platform data */
778 reg_init = pdata->reg_init[id];
780 ret = palmas_ldo_init(palmas, id, reg_init);
782 regulator_unregister(pmic->rdev[id]);
783 goto err_unregister_regulator;
791 err_unregister_regulator:
793 regulator_unregister(pmic->rdev[id]);
797 static int palmas_remove(struct platform_device *pdev)
799 struct palmas_pmic *pmic = platform_get_drvdata(pdev);
802 for (id = 0; id < PALMAS_NUM_REGS; id++)
803 regulator_unregister(pmic->rdev[id]);
807 static struct of_device_id of_palmas_match_tbl[] = {
808 { .compatible = "ti,palmas-pmic", },
812 static struct platform_driver palmas_driver = {
814 .name = "palmas-pmic",
815 .of_match_table = of_palmas_match_tbl,
816 .owner = THIS_MODULE,
818 .probe = palmas_probe,
819 .remove = palmas_remove,
822 static int __init palmas_init(void)
824 return platform_driver_register(&palmas_driver);
826 subsys_initcall(palmas_init);
828 static void __exit palmas_exit(void)
830 platform_driver_unregister(&palmas_driver);
832 module_exit(palmas_exit);
834 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
835 MODULE_DESCRIPTION("Palmas voltage regulator driver");
836 MODULE_LICENSE("GPL");
837 MODULE_ALIAS("platform:palmas-pmic");
838 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);