11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 28f83f268SJie Qiu /* 38f83f268SJie Qiu * Copyright (c) 2014 MediaTek Inc. 48f83f268SJie Qiu * Author: Jie Qiu <jie.qiu@mediatek.com> 58f83f268SJie Qiu */ 68f83f268SJie Qiu #include <linux/clk.h> 78f83f268SJie Qiu #include <linux/delay.h> 88f83f268SJie Qiu #include <linux/io.h> 98f83f268SJie Qiu #include <linux/interrupt.h> 10fdcbe17cSBoris Brezillon #include <linux/module.h> 11ac316725SRandy Dunlap #include <linux/mod_devicetable.h> 128f83f268SJie Qiu #include <linux/platform_device.h> 138f83f268SJie Qiu 148f83f268SJie Qiu #include "mtk_cec.h" 15807e2f3fSMiles Chen #include "mtk_hdmi.h" 16807e2f3fSMiles Chen #include "mtk_drm_drv.h" 178f83f268SJie Qiu 188f83f268SJie Qiu #define TR_CONFIG 0x00 198f83f268SJie Qiu #define CLEAR_CEC_IRQ BIT(15) 208f83f268SJie Qiu 218f83f268SJie Qiu #define CEC_CKGEN 0x04 228f83f268SJie Qiu #define CEC_32K_PDN BIT(19) 238f83f268SJie Qiu #define PDN BIT(16) 248f83f268SJie Qiu 258f83f268SJie Qiu #define RX_EVENT 0x54 268f83f268SJie Qiu #define HDMI_PORD BIT(25) 278f83f268SJie Qiu #define HDMI_HTPLG BIT(24) 288f83f268SJie Qiu #define HDMI_PORD_INT_EN BIT(9) 298f83f268SJie Qiu #define HDMI_HTPLG_INT_EN BIT(8) 308f83f268SJie Qiu 318f83f268SJie Qiu #define RX_GEN_WD 0x58 328f83f268SJie Qiu #define HDMI_PORD_INT_32K_STATUS BIT(26) 338f83f268SJie Qiu #define RX_RISC_INT_32K_STATUS BIT(25) 348f83f268SJie Qiu #define HDMI_HTPLG_INT_32K_STATUS BIT(24) 358f83f268SJie Qiu #define HDMI_PORD_INT_32K_CLR BIT(18) 368f83f268SJie Qiu #define RX_INT_32K_CLR BIT(17) 378f83f268SJie Qiu #define HDMI_HTPLG_INT_32K_CLR BIT(16) 388f83f268SJie Qiu #define HDMI_PORD_INT_32K_STA_MASK BIT(10) 398f83f268SJie Qiu #define RX_RISC_INT_32K_STA_MASK BIT(9) 408f83f268SJie Qiu #define HDMI_HTPLG_INT_32K_STA_MASK BIT(8) 418f83f268SJie Qiu #define HDMI_PORD_INT_32K_EN BIT(2) 428f83f268SJie Qiu #define RX_INT_32K_EN BIT(1) 438f83f268SJie Qiu #define HDMI_HTPLG_INT_32K_EN BIT(0) 448f83f268SJie Qiu 458f83f268SJie Qiu #define NORMAL_INT_CTRL 0x5C 468f83f268SJie Qiu #define HDMI_HTPLG_INT_STA BIT(0) 478f83f268SJie Qiu #define HDMI_PORD_INT_STA BIT(1) 488f83f268SJie Qiu #define HDMI_HTPLG_INT_CLR BIT(16) 498f83f268SJie Qiu #define HDMI_PORD_INT_CLR BIT(17) 508f83f268SJie Qiu #define HDMI_FULL_INT_CLR BIT(20) 518f83f268SJie Qiu 528f83f268SJie Qiu struct mtk_cec { 538f83f268SJie Qiu void __iomem *regs; 548f83f268SJie Qiu struct clk *clk; 558f83f268SJie Qiu int irq; 568f83f268SJie Qiu bool hpd; 578f83f268SJie Qiu void (*hpd_event)(bool hpd, struct device *dev); 588f83f268SJie Qiu struct device *hdmi_dev; 598f83f268SJie Qiu spinlock_t lock; 608f83f268SJie Qiu }; 618f83f268SJie Qiu 628f83f268SJie Qiu static void mtk_cec_clear_bits(struct mtk_cec *cec, unsigned int offset, 638f83f268SJie Qiu unsigned int bits) 648f83f268SJie Qiu { 658f83f268SJie Qiu void __iomem *reg = cec->regs + offset; 668f83f268SJie Qiu u32 tmp; 678f83f268SJie Qiu 688f83f268SJie Qiu tmp = readl(reg); 698f83f268SJie Qiu tmp &= ~bits; 708f83f268SJie Qiu writel(tmp, reg); 718f83f268SJie Qiu } 728f83f268SJie Qiu 738f83f268SJie Qiu static void mtk_cec_set_bits(struct mtk_cec *cec, unsigned int offset, 748f83f268SJie Qiu unsigned int bits) 758f83f268SJie Qiu { 768f83f268SJie Qiu void __iomem *reg = cec->regs + offset; 778f83f268SJie Qiu u32 tmp; 788f83f268SJie Qiu 798f83f268SJie Qiu tmp = readl(reg); 808f83f268SJie Qiu tmp |= bits; 818f83f268SJie Qiu writel(tmp, reg); 828f83f268SJie Qiu } 838f83f268SJie Qiu 848f83f268SJie Qiu static void mtk_cec_mask(struct mtk_cec *cec, unsigned int offset, 858f83f268SJie Qiu unsigned int val, unsigned int mask) 868f83f268SJie Qiu { 878f83f268SJie Qiu u32 tmp = readl(cec->regs + offset) & ~mask; 888f83f268SJie Qiu 898f83f268SJie Qiu tmp |= val & mask; 902c5d69b0SMiles Chen writel(tmp, cec->regs + offset); 918f83f268SJie Qiu } 928f83f268SJie Qiu 938f83f268SJie Qiu void mtk_cec_set_hpd_event(struct device *dev, 948f83f268SJie Qiu void (*hpd_event)(bool hpd, struct device *dev), 958f83f268SJie Qiu struct device *hdmi_dev) 968f83f268SJie Qiu { 978f83f268SJie Qiu struct mtk_cec *cec = dev_get_drvdata(dev); 988f83f268SJie Qiu unsigned long flags; 998f83f268SJie Qiu 1008f83f268SJie Qiu spin_lock_irqsave(&cec->lock, flags); 1018f83f268SJie Qiu cec->hdmi_dev = hdmi_dev; 1028f83f268SJie Qiu cec->hpd_event = hpd_event; 1038f83f268SJie Qiu spin_unlock_irqrestore(&cec->lock, flags); 1048f83f268SJie Qiu } 1058f83f268SJie Qiu 1068f83f268SJie Qiu bool mtk_cec_hpd_high(struct device *dev) 1078f83f268SJie Qiu { 1088f83f268SJie Qiu struct mtk_cec *cec = dev_get_drvdata(dev); 1098f83f268SJie Qiu unsigned int status; 1108f83f268SJie Qiu 1118f83f268SJie Qiu status = readl(cec->regs + RX_EVENT); 1128f83f268SJie Qiu 1138f83f268SJie Qiu return (status & (HDMI_PORD | HDMI_HTPLG)) == (HDMI_PORD | HDMI_HTPLG); 1148f83f268SJie Qiu } 1158f83f268SJie Qiu 1168f83f268SJie Qiu static void mtk_cec_htplg_irq_init(struct mtk_cec *cec) 1178f83f268SJie Qiu { 1188f83f268SJie Qiu mtk_cec_mask(cec, CEC_CKGEN, 0 | CEC_32K_PDN, PDN | CEC_32K_PDN); 1198f83f268SJie Qiu mtk_cec_set_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR | 1208f83f268SJie Qiu RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR); 1218f83f268SJie Qiu mtk_cec_mask(cec, RX_GEN_WD, 0, HDMI_PORD_INT_32K_CLR | RX_INT_32K_CLR | 1228f83f268SJie Qiu HDMI_HTPLG_INT_32K_CLR | HDMI_PORD_INT_32K_EN | 1238f83f268SJie Qiu RX_INT_32K_EN | HDMI_HTPLG_INT_32K_EN); 1248f83f268SJie Qiu } 1258f83f268SJie Qiu 1268f83f268SJie Qiu static void mtk_cec_htplg_irq_enable(struct mtk_cec *cec) 1278f83f268SJie Qiu { 1288f83f268SJie Qiu mtk_cec_set_bits(cec, RX_EVENT, HDMI_PORD_INT_EN | HDMI_HTPLG_INT_EN); 1298f83f268SJie Qiu } 1308f83f268SJie Qiu 1318f83f268SJie Qiu static void mtk_cec_htplg_irq_disable(struct mtk_cec *cec) 1328f83f268SJie Qiu { 1338f83f268SJie Qiu mtk_cec_clear_bits(cec, RX_EVENT, HDMI_PORD_INT_EN | HDMI_HTPLG_INT_EN); 1348f83f268SJie Qiu } 1358f83f268SJie Qiu 1368f83f268SJie Qiu static void mtk_cec_clear_htplg_irq(struct mtk_cec *cec) 1378f83f268SJie Qiu { 1388f83f268SJie Qiu mtk_cec_set_bits(cec, TR_CONFIG, CLEAR_CEC_IRQ); 1398f83f268SJie Qiu mtk_cec_set_bits(cec, NORMAL_INT_CTRL, HDMI_HTPLG_INT_CLR | 1408f83f268SJie Qiu HDMI_PORD_INT_CLR | HDMI_FULL_INT_CLR); 1418f83f268SJie Qiu mtk_cec_set_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR | 1428f83f268SJie Qiu RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR); 1438f83f268SJie Qiu usleep_range(5, 10); 1448f83f268SJie Qiu mtk_cec_clear_bits(cec, NORMAL_INT_CTRL, HDMI_HTPLG_INT_CLR | 1458f83f268SJie Qiu HDMI_PORD_INT_CLR | HDMI_FULL_INT_CLR); 1468f83f268SJie Qiu mtk_cec_clear_bits(cec, TR_CONFIG, CLEAR_CEC_IRQ); 1478f83f268SJie Qiu mtk_cec_clear_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR | 1488f83f268SJie Qiu RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR); 1498f83f268SJie Qiu } 1508f83f268SJie Qiu 1518f83f268SJie Qiu static void mtk_cec_hpd_event(struct mtk_cec *cec, bool hpd) 1528f83f268SJie Qiu { 1538f83f268SJie Qiu void (*hpd_event)(bool hpd, struct device *dev); 1548f83f268SJie Qiu struct device *hdmi_dev; 1558f83f268SJie Qiu unsigned long flags; 1568f83f268SJie Qiu 1578f83f268SJie Qiu spin_lock_irqsave(&cec->lock, flags); 1588f83f268SJie Qiu hpd_event = cec->hpd_event; 1598f83f268SJie Qiu hdmi_dev = cec->hdmi_dev; 1608f83f268SJie Qiu spin_unlock_irqrestore(&cec->lock, flags); 1618f83f268SJie Qiu 1628f83f268SJie Qiu if (hpd_event) 1638f83f268SJie Qiu hpd_event(hpd, hdmi_dev); 1648f83f268SJie Qiu } 1658f83f268SJie Qiu 1668f83f268SJie Qiu static irqreturn_t mtk_cec_htplg_isr_thread(int irq, void *arg) 1678f83f268SJie Qiu { 1688f83f268SJie Qiu struct device *dev = arg; 1698f83f268SJie Qiu struct mtk_cec *cec = dev_get_drvdata(dev); 1708f83f268SJie Qiu bool hpd; 1718f83f268SJie Qiu 1728f83f268SJie Qiu mtk_cec_clear_htplg_irq(cec); 1738f83f268SJie Qiu hpd = mtk_cec_hpd_high(dev); 1748f83f268SJie Qiu 1758f83f268SJie Qiu if (cec->hpd != hpd) { 1768f83f268SJie Qiu dev_dbg(dev, "hotplug event! cur hpd = %d, hpd = %d\n", 1778f83f268SJie Qiu cec->hpd, hpd); 1788f83f268SJie Qiu cec->hpd = hpd; 1798f83f268SJie Qiu mtk_cec_hpd_event(cec, hpd); 1808f83f268SJie Qiu } 1818f83f268SJie Qiu return IRQ_HANDLED; 1828f83f268SJie Qiu } 1838f83f268SJie Qiu 1848f83f268SJie Qiu static int mtk_cec_probe(struct platform_device *pdev) 1858f83f268SJie Qiu { 1868f83f268SJie Qiu struct device *dev = &pdev->dev; 1878f83f268SJie Qiu struct mtk_cec *cec; 1888f83f268SJie Qiu struct resource *res; 1898f83f268SJie Qiu int ret; 1908f83f268SJie Qiu 1918f83f268SJie Qiu cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL); 1928f83f268SJie Qiu if (!cec) 1938f83f268SJie Qiu return -ENOMEM; 1948f83f268SJie Qiu 1958f83f268SJie Qiu platform_set_drvdata(pdev, cec); 1968f83f268SJie Qiu spin_lock_init(&cec->lock); 1978f83f268SJie Qiu 1988f83f268SJie Qiu res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1998f83f268SJie Qiu cec->regs = devm_ioremap_resource(dev, res); 2008f83f268SJie Qiu if (IS_ERR(cec->regs)) { 2018f83f268SJie Qiu ret = PTR_ERR(cec->regs); 2028f83f268SJie Qiu dev_err(dev, "Failed to ioremap cec: %d\n", ret); 2038f83f268SJie Qiu return ret; 2048f83f268SJie Qiu } 2058f83f268SJie Qiu 2068f83f268SJie Qiu cec->clk = devm_clk_get(dev, NULL); 2078f83f268SJie Qiu if (IS_ERR(cec->clk)) { 2088f83f268SJie Qiu ret = PTR_ERR(cec->clk); 2098f83f268SJie Qiu dev_err(dev, "Failed to get cec clock: %d\n", ret); 2108f83f268SJie Qiu return ret; 2118f83f268SJie Qiu } 2128f83f268SJie Qiu 2138f83f268SJie Qiu cec->irq = platform_get_irq(pdev, 0); 214ee5ee188Stangchunyou if (cec->irq < 0) 2158f83f268SJie Qiu return cec->irq; 2168f83f268SJie Qiu 2178f83f268SJie Qiu ret = devm_request_threaded_irq(dev, cec->irq, NULL, 2188f83f268SJie Qiu mtk_cec_htplg_isr_thread, 2198f83f268SJie Qiu IRQF_SHARED | IRQF_TRIGGER_LOW | 2208f83f268SJie Qiu IRQF_ONESHOT, "hdmi hpd", dev); 2218f83f268SJie Qiu if (ret) { 2228f83f268SJie Qiu dev_err(dev, "Failed to register cec irq: %d\n", ret); 2238f83f268SJie Qiu return ret; 2248f83f268SJie Qiu } 2258f83f268SJie Qiu 2268f83f268SJie Qiu ret = clk_prepare_enable(cec->clk); 2278f83f268SJie Qiu if (ret) { 2288f83f268SJie Qiu dev_err(dev, "Failed to enable cec clock: %d\n", ret); 2298f83f268SJie Qiu return ret; 2308f83f268SJie Qiu } 2318f83f268SJie Qiu 2328f83f268SJie Qiu mtk_cec_htplg_irq_init(cec); 2338f83f268SJie Qiu mtk_cec_htplg_irq_enable(cec); 2348f83f268SJie Qiu 2358f83f268SJie Qiu return 0; 2368f83f268SJie Qiu } 2378f83f268SJie Qiu 238*b3af12a0SUwe Kleine-König static void mtk_cec_remove(struct platform_device *pdev) 2398f83f268SJie Qiu { 2408f83f268SJie Qiu struct mtk_cec *cec = platform_get_drvdata(pdev); 2418f83f268SJie Qiu 2428f83f268SJie Qiu mtk_cec_htplg_irq_disable(cec); 2438f83f268SJie Qiu clk_disable_unprepare(cec->clk); 2448f83f268SJie Qiu } 2458f83f268SJie Qiu 2468f83f268SJie Qiu static const struct of_device_id mtk_cec_of_ids[] = { 2478f83f268SJie Qiu { .compatible = "mediatek,mt8173-cec", }, 2488f83f268SJie Qiu {} 2498f83f268SJie Qiu }; 250fdcbe17cSBoris Brezillon MODULE_DEVICE_TABLE(of, mtk_cec_of_ids); 2518f83f268SJie Qiu 2528f83f268SJie Qiu struct platform_driver mtk_cec_driver = { 2538f83f268SJie Qiu .probe = mtk_cec_probe, 254*b3af12a0SUwe Kleine-König .remove_new = mtk_cec_remove, 2558f83f268SJie Qiu .driver = { 2568f83f268SJie Qiu .name = "mediatek-cec", 2578f83f268SJie Qiu .of_match_table = mtk_cec_of_ids, 2588f83f268SJie Qiu }, 2598f83f268SJie Qiu }; 260