1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ESM (Error Signal Monitor) driver for TI TPS6594/TPS6593/LP8764 PMICs 4 * 5 * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/ 6 */ 7 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> 13 14 #include <linux/mfd/tps6594.h> 15 16 #define TPS6594_DEV_REV_1 0x08 17 18 static irqreturn_t tps6594_esm_isr(int irq, void *dev_id) 19 { 20 struct platform_device *pdev = dev_id; 21 int i; 22 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); 26 return IRQ_HANDLED; 27 } 28 } 29 30 return IRQ_NONE; 31 } 32 33 static int tps6594_esm_probe(struct platform_device *pdev) 34 { 35 struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent); 36 struct device *dev = &pdev->dev; 37 unsigned int rev; 38 int irq; 39 int ret; 40 int i; 41 42 /* 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. 47 */ 48 ret = regmap_read(tps->regmap, TPS6594_REG_DEV_REV, &rev); 49 if (ret) 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"); 55 56 for (i = 0; i < pdev->num_resources; i++) { 57 irq = platform_get_irq_byname(pdev, pdev->resource[i].name); 58 if (irq < 0) 59 return dev_err_probe(dev, irq, "Failed to get %s irq\n", 60 pdev->resource[i].name); 61 62 ret = devm_request_threaded_irq(dev, irq, NULL, 63 tps6594_esm_isr, IRQF_ONESHOT, 64 pdev->resource[i].name, pdev); 65 if (ret) 66 return dev_err_probe(dev, ret, "Failed to request irq\n"); 67 } 68 69 ret = regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_MODE_CFG, 70 TPS6594_BIT_ESM_SOC_EN | TPS6594_BIT_ESM_SOC_ENDRV); 71 if (ret) 72 return dev_err_probe(dev, ret, "Failed to configure ESM\n"); 73 74 ret = regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG, 75 TPS6594_BIT_ESM_SOC_START); 76 if (ret) 77 return dev_err_probe(dev, ret, "Failed to start ESM\n"); 78 79 pm_runtime_enable(dev); 80 pm_runtime_get_sync(dev); 81 82 return 0; 83 } 84 85 static int tps6594_esm_remove(struct platform_device *pdev) 86 { 87 struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent); 88 struct device *dev = &pdev->dev; 89 int ret; 90 91 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG, 92 TPS6594_BIT_ESM_SOC_START); 93 if (ret) { 94 dev_err(dev, "Failed to stop ESM\n"); 95 goto out; 96 } 97 98 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_MODE_CFG, 99 TPS6594_BIT_ESM_SOC_EN | TPS6594_BIT_ESM_SOC_ENDRV); 100 if (ret) 101 dev_err(dev, "Failed to unconfigure ESM\n"); 102 103 out: 104 pm_runtime_put_sync(dev); 105 pm_runtime_disable(dev); 106 107 return ret; 108 } 109 110 static int tps6594_esm_suspend(struct device *dev) 111 { 112 struct tps6594 *tps = dev_get_drvdata(dev->parent); 113 int ret; 114 115 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG, 116 TPS6594_BIT_ESM_SOC_START); 117 118 pm_runtime_put_sync(dev); 119 120 return ret; 121 } 122 123 static int tps6594_esm_resume(struct device *dev) 124 { 125 struct tps6594 *tps = dev_get_drvdata(dev->parent); 126 127 pm_runtime_get_sync(dev); 128 129 return regmap_set_bits(tps->regmap, TPS6594_REG_ESM_SOC_START_REG, 130 TPS6594_BIT_ESM_SOC_START); 131 } 132 133 static DEFINE_SIMPLE_DEV_PM_OPS(tps6594_esm_pm_ops, tps6594_esm_suspend, tps6594_esm_resume); 134 135 static struct platform_driver tps6594_esm_driver = { 136 .driver = { 137 .name = "tps6594-esm", 138 .pm = pm_sleep_ptr(&tps6594_esm_pm_ops), 139 }, 140 .probe = tps6594_esm_probe, 141 .remove = tps6594_esm_remove, 142 }; 143 144 module_platform_driver(tps6594_esm_driver); 145 146 MODULE_ALIAS("platform:tps6594-esm"); 147 MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>"); 148 MODULE_DESCRIPTION("TPS6594 Error Signal Monitor Driver"); 149 MODULE_LICENSE("GPL"); 150