1 // SPDX-License-Identifier: GPL-2.0
3 * ESM (Error Signal Monitor) driver for TI TPS6594/TPS6593/LP8764 PMICs
5 * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regmap.h>
14 #include <linux/mfd/tps6594.h>
16 #define TPS6594_DEV_REV_1 0x08
18 static irqreturn_t tps6594_esm_isr(int irq, void *dev_id)
20 struct platform_device *pdev = dev_id;
23 for (i = 0 ; i < pdev->num_resources ; i++) {
24 if (irq == platform_get_irq_byname(pdev, pdev->resource[i].name)) {
25 dev_err(pdev->dev.parent, "%s error detected\n", pdev->resource[i].name);
33 static int tps6594_esm_probe(struct platform_device *pdev)
35 struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
36 struct device *dev = &pdev->dev;
43 * Due to a bug in revision 1 of the PMIC, the GPIO3 used for the
44 * SoC ESM function is used to power the load switch instead.
45 * As a consequence, ESM can not be used on those PMIC.
46 * Check the version and return an error in case of revision 1.
48 ret = regmap_read(tps->regmap, TPS6594_REG_DEV_REV, &rev);
50 return dev_err_probe(dev, ret,
51 "Failed to read PMIC revision\n");
52 if (rev == TPS6594_DEV_REV_1)
53 return dev_err_probe(dev, -ENODEV,
54 "ESM not supported for revision 1 PMIC\n");
56 for (i = 0; i < pdev->num_resources; i++) {
57 irq = platform_get_irq_byname(pdev, pdev->resource[i].name);
61 ret = devm_request_threaded_irq(dev, irq, NULL,
62 tps6594_esm_isr, IRQF_ONESHOT,
63 pdev->resource[i].name, pdev);
65 return dev_err_probe(dev, ret, "Failed to request irq\n");
68 ret = regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_MODE_CFG,
69 TPS6594_BIT_ESM_SOC_EN | TPS6594_BIT_ESM_SOC_ENDRV);
71 return dev_err_probe(dev, ret, "Failed to configure ESM\n");
73 ret = regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG,
74 TPS6594_BIT_ESM_SOC_START);
76 return dev_err_probe(dev, ret, "Failed to start ESM\n");
78 pm_runtime_enable(dev);
79 pm_runtime_get_sync(dev);
84 static void tps6594_esm_remove(struct platform_device *pdev)
86 struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
87 struct device *dev = &pdev->dev;
90 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG,
91 TPS6594_BIT_ESM_SOC_START);
93 dev_err(dev, "Failed to stop ESM\n");
97 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_MODE_CFG,
98 TPS6594_BIT_ESM_SOC_EN | TPS6594_BIT_ESM_SOC_ENDRV);
100 dev_err(dev, "Failed to unconfigure ESM\n");
103 pm_runtime_put_sync(dev);
104 pm_runtime_disable(dev);
107 static int tps6594_esm_suspend(struct device *dev)
109 struct tps6594 *tps = dev_get_drvdata(dev->parent);
112 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG,
113 TPS6594_BIT_ESM_SOC_START);
115 pm_runtime_put_sync(dev);
120 static int tps6594_esm_resume(struct device *dev)
122 struct tps6594 *tps = dev_get_drvdata(dev->parent);
124 pm_runtime_get_sync(dev);
126 return regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG,
127 TPS6594_BIT_ESM_SOC_START);
130 static DEFINE_SIMPLE_DEV_PM_OPS(tps6594_esm_pm_ops, tps6594_esm_suspend, tps6594_esm_resume);
132 static struct platform_driver tps6594_esm_driver = {
134 .name = "tps6594-esm",
135 .pm = pm_sleep_ptr(&tps6594_esm_pm_ops),
137 .probe = tps6594_esm_probe,
138 .remove_new = tps6594_esm_remove,
141 module_platform_driver(tps6594_esm_driver);
143 MODULE_ALIAS("platform:tps6594-esm");
144 MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
145 MODULE_DESCRIPTION("TPS6594 Error Signal Monitor Driver");
146 MODULE_LICENSE("GPL");