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