1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Split TWL6030 logic from twl-regulator.c:
4 * Copyright (C) 2008 David Brownell
6 * Copyright (C) 2016 Nicolae Rosia <nicolae.rosia@gmail.com>
9 #include <linux/module.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
16 #include <linux/of_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/mfd/twl.h>
21 #include <linux/delay.h>
24 /* start of regulator's PM_RECEIVER control register bank */
27 /* twl resource ID, for resource control state machine */
32 /* used by regulator core */
33 struct regulator_desc desc;
35 /* chip specific features */
36 unsigned long features;
38 /* data passed from board for external get/set voltage */
43 /* LDO control registers ... offset is from the base of its register bank.
44 * The first three registers of all power resource banks help hardware to
45 * manage the various resource groups.
47 /* Common offset in TWL4030/6030 */
49 /* TWL6030 register offsets */
52 #define VREG_VOLTAGE 3
53 #define VREG_VOLTAGE_SMPS 4
54 /* TWL6030 Misc register offsets */
57 #define VREG_BC_PROC 3
58 #define VREG_BC_CLK_RST 4
60 /* TWL6030 LDO register values for CFG_STATE */
61 #define TWL6030_CFG_STATE_OFF 0x00
62 #define TWL6030_CFG_STATE_ON 0x01
63 #define TWL6030_CFG_STATE_OFF2 0x02
64 #define TWL6030_CFG_STATE_SLEEP 0x03
65 #define TWL6030_CFG_STATE_GRP_SHIFT 5
66 #define TWL6030_CFG_STATE_APP_SHIFT 2
67 #define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
68 #define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
69 TWL6030_CFG_STATE_APP_SHIFT)
71 /* Flags for SMPS Voltage reading */
72 #define SMPS_OFFSET_EN BIT(0)
73 #define SMPS_EXTENDED_EN BIT(1)
75 /* twl6032 SMPS EPROM values */
76 #define TWL6030_SMPS_OFFSET 0xB0
77 #define TWL6030_SMPS_MULT 0xB3
78 #define SMPS_MULTOFFSET_SMPS4 BIT(0)
79 #define SMPS_MULTOFFSET_VIO BIT(1)
80 #define SMPS_MULTOFFSET_SMPS3 BIT(6)
83 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
88 status = twl_i2c_read_u8(slave_subgp,
89 &value, info->base + offset);
90 return (status < 0) ? status : value;
94 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
97 return twl_i2c_write_u8(slave_subgp,
98 value, info->base + offset);
101 /* generic power resource operations, which work on all regulators */
102 static int twlreg_grp(struct regulator_dev *rdev)
104 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
109 * Enable/disable regulators by joining/leaving the P1 (processor) group.
110 * We assume nobody else is updating the DEV_GRP registers.
112 /* definition for 6030 family */
113 #define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
114 #define P2_GRP_6030 BIT(1) /* "peripherals" */
115 #define P1_GRP_6030 BIT(0) /* CPU/Linux */
117 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
119 struct twlreg_info *info = rdev_get_drvdata(rdev);
122 if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
123 grp = twlreg_grp(rdev);
131 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
132 val = TWL6030_CFG_STATE_APP(val);
134 return grp && (val == TWL6030_CFG_STATE_ON);
137 #define PB_I2C_BUSY BIT(0)
138 #define PB_I2C_BWEN BIT(1)
141 static int twl6030reg_enable(struct regulator_dev *rdev)
143 struct twlreg_info *info = rdev_get_drvdata(rdev);
147 if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
148 grp = twlreg_grp(rdev);
152 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
153 grp << TWL6030_CFG_STATE_GRP_SHIFT |
154 TWL6030_CFG_STATE_ON);
158 static int twl6030reg_disable(struct regulator_dev *rdev)
160 struct twlreg_info *info = rdev_get_drvdata(rdev);
164 if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
165 grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
167 /* For 6030, set the off state for all grps enabled */
168 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
169 (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
170 TWL6030_CFG_STATE_OFF);
175 static int twl6030reg_get_status(struct regulator_dev *rdev)
177 struct twlreg_info *info = rdev_get_drvdata(rdev);
180 val = twlreg_grp(rdev);
184 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
186 switch (TWL6030_CFG_STATE_APP(val)) {
187 case TWL6030_CFG_STATE_ON:
188 return REGULATOR_STATUS_NORMAL;
190 case TWL6030_CFG_STATE_SLEEP:
191 return REGULATOR_STATUS_STANDBY;
193 case TWL6030_CFG_STATE_OFF:
194 case TWL6030_CFG_STATE_OFF2:
199 return REGULATOR_STATUS_OFF;
202 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
204 struct twlreg_info *info = rdev_get_drvdata(rdev);
208 if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
209 grp = twlreg_grp(rdev);
214 /* Compose the state register settings */
215 val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
216 /* We can only set the mode through state machine commands... */
218 case REGULATOR_MODE_NORMAL:
219 val |= TWL6030_CFG_STATE_ON;
221 case REGULATOR_MODE_STANDBY:
222 val |= TWL6030_CFG_STATE_SLEEP;
229 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
232 static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
233 int max_uV, unsigned *selector)
238 static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
243 static const struct regulator_ops twl6030coresmps_ops = {
244 .set_voltage = twl6030coresmps_set_voltage,
245 .get_voltage = twl6030coresmps_get_voltage,
249 twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
251 struct twlreg_info *info = rdev_get_drvdata(rdev);
253 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
257 static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
259 struct twlreg_info *info = rdev_get_drvdata(rdev);
260 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
265 static const struct regulator_ops twl6030ldo_ops = {
266 .list_voltage = regulator_list_voltage_linear_range,
268 .set_voltage_sel = twl6030ldo_set_voltage_sel,
269 .get_voltage_sel = twl6030ldo_get_voltage_sel,
271 .enable = twl6030reg_enable,
272 .disable = twl6030reg_disable,
273 .is_enabled = twl6030reg_is_enabled,
275 .set_mode = twl6030reg_set_mode,
277 .get_status = twl6030reg_get_status,
280 static const struct regulator_ops twl6030fixed_ops = {
281 .list_voltage = regulator_list_voltage_linear,
283 .enable = twl6030reg_enable,
284 .disable = twl6030reg_disable,
285 .is_enabled = twl6030reg_is_enabled,
287 .set_mode = twl6030reg_set_mode,
289 .get_status = twl6030reg_get_status,
293 * SMPS status and control
296 static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
298 struct twlreg_info *info = rdev_get_drvdata(rdev);
302 switch (info->flags) {
312 voltage = 1350 * 1000;
315 voltage = 1500 * 1000;
318 voltage = 1800 * 1000;
321 voltage = 1900 * 1000;
324 voltage = 2100 * 1000;
327 voltage += (600000 + (12500 * (index - 1)));
330 case SMPS_EXTENDED_EN:
336 voltage = 2084 * 1000;
339 voltage = 2315 * 1000;
342 voltage = 2778 * 1000;
345 voltage = 2932 * 1000;
348 voltage = 3241 * 1000;
351 voltage = (1852000 + (38600 * (index - 1)));
354 case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
360 voltage = 4167 * 1000;
363 voltage = 2315 * 1000;
366 voltage = 2778 * 1000;
369 voltage = 2932 * 1000;
372 voltage = 3241 * 1000;
375 voltage = (2161000 + (38600 * (index - 1)));
383 static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
386 struct twlreg_info *info = rdev_get_drvdata(rdev);
389 switch (info->flags) {
393 else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
394 vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
397 /* Values 1..57 for vsel are linear and can be calculated
398 * values 58..62 are non linear.
400 else if ((min_uV > 1900000) && (min_uV <= 2100000))
402 else if ((min_uV > 1800000) && (min_uV <= 1900000))
404 else if ((min_uV > 1500000) && (min_uV <= 1800000))
406 else if ((min_uV > 1350000) && (min_uV <= 1500000))
408 else if ((min_uV > 1300000) && (min_uV <= 1350000))
416 else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
417 vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
420 /* Values 1..57 for vsel are linear and can be calculated
421 * values 58..62 are non linear.
423 else if ((min_uV > 1900000) && (min_uV <= 2100000))
425 else if ((min_uV > 1800000) && (min_uV <= 1900000))
427 else if ((min_uV > 1500000) && (min_uV <= 1800000))
429 else if ((min_uV > 1350000) && (min_uV <= 1500000))
434 case SMPS_EXTENDED_EN:
437 } else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
438 vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
442 case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
445 } else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
446 vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
455 static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
456 unsigned int selector)
458 struct twlreg_info *info = rdev_get_drvdata(rdev);
460 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
464 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
466 struct twlreg_info *info = rdev_get_drvdata(rdev);
468 return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
471 static const struct regulator_ops twlsmps_ops = {
472 .list_voltage = twl6030smps_list_voltage,
473 .map_voltage = twl6030smps_map_voltage,
475 .set_voltage_sel = twl6030smps_set_voltage_sel,
476 .get_voltage_sel = twl6030smps_get_voltage_sel,
478 .enable = twl6030reg_enable,
479 .disable = twl6030reg_disable,
480 .is_enabled = twl6030reg_is_enabled,
482 .set_mode = twl6030reg_set_mode,
484 .get_status = twl6030reg_get_status,
487 /*----------------------------------------------------------------------*/
488 static const struct regulator_linear_range twl6030ldo_linear_range[] = {
489 REGULATOR_LINEAR_RANGE(0, 0, 0, 0),
490 REGULATOR_LINEAR_RANGE(1000000, 1, 24, 100000),
491 REGULATOR_LINEAR_RANGE(2750000, 31, 31, 0),
494 #define TWL6030_ADJUSTABLE_SMPS(label) \
495 static const struct twlreg_info TWL6030_INFO_##label = { \
498 .id = TWL6030_REG_##label, \
499 .ops = &twl6030coresmps_ops, \
500 .type = REGULATOR_VOLTAGE, \
501 .owner = THIS_MODULE, \
505 #define TWL6030_ADJUSTABLE_LDO(label, offset) \
506 static const struct twlreg_info TWL6030_INFO_##label = { \
510 .id = TWL6030_REG_##label, \
512 .linear_ranges = twl6030ldo_linear_range, \
513 .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
514 .ops = &twl6030ldo_ops, \
515 .type = REGULATOR_VOLTAGE, \
516 .owner = THIS_MODULE, \
520 #define TWL6032_ADJUSTABLE_LDO(label, offset) \
521 static const struct twlreg_info TWL6032_INFO_##label = { \
525 .id = TWL6032_REG_##label, \
527 .linear_ranges = twl6030ldo_linear_range, \
528 .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
529 .ops = &twl6030ldo_ops, \
530 .type = REGULATOR_VOLTAGE, \
531 .owner = THIS_MODULE, \
535 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
536 static const struct twlreg_info TWLFIXED_INFO_##label = { \
541 .id = TWL6030##_REG_##label, \
543 .ops = &twl6030fixed_ops, \
544 .type = REGULATOR_VOLTAGE, \
545 .owner = THIS_MODULE, \
546 .min_uV = mVolts * 1000, \
547 .enable_time = turnon_delay, \
548 .of_map_mode = NULL, \
552 #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
553 static const struct twlreg_info TWLSMPS_INFO_##label = { \
557 .id = TWL6032_REG_##label, \
559 .ops = &twlsmps_ops, \
560 .type = REGULATOR_VOLTAGE, \
561 .owner = THIS_MODULE, \
565 /* VUSBCP is managed *only* by the USB subchip */
566 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
567 /* Turnon-delay and remap configuration values for 6030 are not
568 verified since the specification is not public */
569 TWL6030_ADJUSTABLE_SMPS(VDD1);
570 TWL6030_ADJUSTABLE_SMPS(VDD2);
571 TWL6030_ADJUSTABLE_SMPS(VDD3);
572 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54);
573 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58);
574 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c);
575 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68);
576 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c);
577 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74);
578 /* 6025 are renamed compared to 6030 versions */
579 TWL6032_ADJUSTABLE_LDO(LDO2, 0x54);
580 TWL6032_ADJUSTABLE_LDO(LDO4, 0x58);
581 TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c);
582 TWL6032_ADJUSTABLE_LDO(LDO5, 0x68);
583 TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c);
584 TWL6032_ADJUSTABLE_LDO(LDO7, 0x74);
585 TWL6032_ADJUSTABLE_LDO(LDO6, 0x60);
586 TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64);
587 TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70);
588 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
589 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
590 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
591 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
592 TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
593 TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
594 TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
595 TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
596 TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
598 static u8 twl_get_smps_offset(void)
602 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
603 TWL6030_SMPS_OFFSET);
607 static u8 twl_get_smps_mult(void)
611 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
616 #define TWL_OF_MATCH(comp, family, label) \
618 .compatible = comp, \
619 .data = &family##_INFO_##label, \
622 #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
623 #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
624 #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
625 #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
627 static const struct of_device_id twl_of_match[] = {
628 TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
629 TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
630 TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
631 TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
632 TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
633 TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
634 TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
635 TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
636 TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
637 TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
638 TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
639 TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
640 TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
641 TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
642 TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
643 TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
644 TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
645 TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
646 TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
647 TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
648 TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
649 TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
650 TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
651 TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
652 TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
653 TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
654 TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
657 MODULE_DEVICE_TABLE(of, twl_of_match);
659 static int twlreg_probe(struct platform_device *pdev)
662 struct twlreg_info *info;
663 const struct twlreg_info *template;
664 struct regulator_init_data *initdata;
665 struct regulation_constraints *c;
666 struct regulator_dev *rdev;
667 struct regulator_config config = { };
669 template = of_device_get_match_data(&pdev->dev);
673 id = template->desc.id;
674 initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
679 info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
683 /* Constrain board-specific capabilities according to what
684 * this driver and the chip itself can actually do.
686 c = &initdata->constraints;
687 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
688 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
689 | REGULATOR_CHANGE_MODE
690 | REGULATOR_CHANGE_STATUS;
693 case TWL6032_REG_SMPS3:
694 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
695 info->flags |= SMPS_EXTENDED_EN;
696 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
697 info->flags |= SMPS_OFFSET_EN;
699 case TWL6032_REG_SMPS4:
700 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
701 info->flags |= SMPS_EXTENDED_EN;
702 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
703 info->flags |= SMPS_OFFSET_EN;
705 case TWL6032_REG_VIO:
706 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
707 info->flags |= SMPS_EXTENDED_EN;
708 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
709 info->flags |= SMPS_OFFSET_EN;
713 config.dev = &pdev->dev;
714 config.init_data = initdata;
715 config.driver_data = info;
716 config.of_node = pdev->dev.of_node;
718 rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
720 dev_err(&pdev->dev, "can't register %s, %ld\n",
721 info->desc.name, PTR_ERR(rdev));
722 return PTR_ERR(rdev);
724 platform_set_drvdata(pdev, rdev);
726 /* NOTE: many regulators support short-circuit IRQs (presentable
727 * as REGULATOR_OVER_CURRENT notifications?) configured via:
729 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
730 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
737 MODULE_ALIAS("platform:twl6030_reg");
739 static struct platform_driver twlreg_driver = {
740 .probe = twlreg_probe,
741 /* NOTE: short name, to work around driver model truncation of
742 * "twl_regulator.12" (and friends) to "twl_regulator.1".
745 .name = "twl6030_reg",
746 .of_match_table = of_match_ptr(twl_of_match),
750 static int __init twlreg_init(void)
752 return platform_driver_register(&twlreg_driver);
754 subsys_initcall(twlreg_init);
756 static void __exit twlreg_exit(void)
758 platform_driver_unregister(&twlreg_driver);
760 module_exit(twlreg_exit)
762 MODULE_DESCRIPTION("TWL6030 regulator driver");
763 MODULE_LICENSE("GPL");