mtk_cec.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Jie Qiu <jie.qiu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include "mtk_cec.h"
  20. #define TR_CONFIG 0x00
  21. #define CLEAR_CEC_IRQ BIT(15)
  22. #define CEC_CKGEN 0x04
  23. #define CEC_32K_PDN BIT(19)
  24. #define PDN BIT(16)
  25. #define RX_EVENT 0x54
  26. #define HDMI_PORD BIT(25)
  27. #define HDMI_HTPLG BIT(24)
  28. #define HDMI_PORD_INT_EN BIT(9)
  29. #define HDMI_HTPLG_INT_EN BIT(8)
  30. #define RX_GEN_WD 0x58
  31. #define HDMI_PORD_INT_32K_STATUS BIT(26)
  32. #define RX_RISC_INT_32K_STATUS BIT(25)
  33. #define HDMI_HTPLG_INT_32K_STATUS BIT(24)
  34. #define HDMI_PORD_INT_32K_CLR BIT(18)
  35. #define RX_INT_32K_CLR BIT(17)
  36. #define HDMI_HTPLG_INT_32K_CLR BIT(16)
  37. #define HDMI_PORD_INT_32K_STA_MASK BIT(10)
  38. #define RX_RISC_INT_32K_STA_MASK BIT(9)
  39. #define HDMI_HTPLG_INT_32K_STA_MASK BIT(8)
  40. #define HDMI_PORD_INT_32K_EN BIT(2)
  41. #define RX_INT_32K_EN BIT(1)
  42. #define HDMI_HTPLG_INT_32K_EN BIT(0)
  43. #define NORMAL_INT_CTRL 0x5C
  44. #define HDMI_HTPLG_INT_STA BIT(0)
  45. #define HDMI_PORD_INT_STA BIT(1)
  46. #define HDMI_HTPLG_INT_CLR BIT(16)
  47. #define HDMI_PORD_INT_CLR BIT(17)
  48. #define HDMI_FULL_INT_CLR BIT(20)
  49. struct mtk_cec {
  50. void __iomem *regs;
  51. struct clk *clk;
  52. int irq;
  53. bool hpd;
  54. void (*hpd_event)(bool hpd, struct device *dev);
  55. struct device *hdmi_dev;
  56. spinlock_t lock;
  57. };
  58. static void mtk_cec_clear_bits(struct mtk_cec *cec, unsigned int offset,
  59. unsigned int bits)
  60. {
  61. void __iomem *reg = cec->regs + offset;
  62. u32 tmp;
  63. tmp = readl(reg);
  64. tmp &= ~bits;
  65. writel(tmp, reg);
  66. }
  67. static void mtk_cec_set_bits(struct mtk_cec *cec, unsigned int offset,
  68. unsigned int bits)
  69. {
  70. void __iomem *reg = cec->regs + offset;
  71. u32 tmp;
  72. tmp = readl(reg);
  73. tmp |= bits;
  74. writel(tmp, reg);
  75. }
  76. static void mtk_cec_mask(struct mtk_cec *cec, unsigned int offset,
  77. unsigned int val, unsigned int mask)
  78. {
  79. u32 tmp = readl(cec->regs + offset) & ~mask;
  80. tmp |= val & mask;
  81. writel(val, cec->regs + offset);
  82. }
  83. void mtk_cec_set_hpd_event(struct device *dev,
  84. void (*hpd_event)(bool hpd, struct device *dev),
  85. struct device *hdmi_dev)
  86. {
  87. struct mtk_cec *cec = dev_get_drvdata(dev);
  88. unsigned long flags;
  89. spin_lock_irqsave(&cec->lock, flags);
  90. cec->hdmi_dev = hdmi_dev;
  91. cec->hpd_event = hpd_event;
  92. spin_unlock_irqrestore(&cec->lock, flags);
  93. }
  94. bool mtk_cec_hpd_high(struct device *dev)
  95. {
  96. struct mtk_cec *cec = dev_get_drvdata(dev);
  97. unsigned int status;
  98. status = readl(cec->regs + RX_EVENT);
  99. return (status & (HDMI_PORD | HDMI_HTPLG)) == (HDMI_PORD | HDMI_HTPLG);
  100. }
  101. static void mtk_cec_htplg_irq_init(struct mtk_cec *cec)
  102. {
  103. mtk_cec_mask(cec, CEC_CKGEN, 0 | CEC_32K_PDN, PDN | CEC_32K_PDN);
  104. mtk_cec_set_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR |
  105. RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR);
  106. mtk_cec_mask(cec, RX_GEN_WD, 0, HDMI_PORD_INT_32K_CLR | RX_INT_32K_CLR |
  107. HDMI_HTPLG_INT_32K_CLR | HDMI_PORD_INT_32K_EN |
  108. RX_INT_32K_EN | HDMI_HTPLG_INT_32K_EN);
  109. }
  110. static void mtk_cec_htplg_irq_enable(struct mtk_cec *cec)
  111. {
  112. mtk_cec_set_bits(cec, RX_EVENT, HDMI_PORD_INT_EN | HDMI_HTPLG_INT_EN);
  113. }
  114. static void mtk_cec_htplg_irq_disable(struct mtk_cec *cec)
  115. {
  116. mtk_cec_clear_bits(cec, RX_EVENT, HDMI_PORD_INT_EN | HDMI_HTPLG_INT_EN);
  117. }
  118. static void mtk_cec_clear_htplg_irq(struct mtk_cec *cec)
  119. {
  120. mtk_cec_set_bits(cec, TR_CONFIG, CLEAR_CEC_IRQ);
  121. mtk_cec_set_bits(cec, NORMAL_INT_CTRL, HDMI_HTPLG_INT_CLR |
  122. HDMI_PORD_INT_CLR | HDMI_FULL_INT_CLR);
  123. mtk_cec_set_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR |
  124. RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR);
  125. usleep_range(5, 10);
  126. mtk_cec_clear_bits(cec, NORMAL_INT_CTRL, HDMI_HTPLG_INT_CLR |
  127. HDMI_PORD_INT_CLR | HDMI_FULL_INT_CLR);
  128. mtk_cec_clear_bits(cec, TR_CONFIG, CLEAR_CEC_IRQ);
  129. mtk_cec_clear_bits(cec, RX_GEN_WD, HDMI_PORD_INT_32K_CLR |
  130. RX_INT_32K_CLR | HDMI_HTPLG_INT_32K_CLR);
  131. }
  132. static void mtk_cec_hpd_event(struct mtk_cec *cec, bool hpd)
  133. {
  134. void (*hpd_event)(bool hpd, struct device *dev);
  135. struct device *hdmi_dev;
  136. unsigned long flags;
  137. spin_lock_irqsave(&cec->lock, flags);
  138. hpd_event = cec->hpd_event;
  139. hdmi_dev = cec->hdmi_dev;
  140. spin_unlock_irqrestore(&cec->lock, flags);
  141. if (hpd_event)
  142. hpd_event(hpd, hdmi_dev);
  143. }
  144. static irqreturn_t mtk_cec_htplg_isr_thread(int irq, void *arg)
  145. {
  146. struct device *dev = arg;
  147. struct mtk_cec *cec = dev_get_drvdata(dev);
  148. bool hpd;
  149. mtk_cec_clear_htplg_irq(cec);
  150. hpd = mtk_cec_hpd_high(dev);
  151. if (cec->hpd != hpd) {
  152. dev_dbg(dev, "hotplug event! cur hpd = %d, hpd = %d\n",
  153. cec->hpd, hpd);
  154. cec->hpd = hpd;
  155. mtk_cec_hpd_event(cec, hpd);
  156. }
  157. return IRQ_HANDLED;
  158. }
  159. static int mtk_cec_probe(struct platform_device *pdev)
  160. {
  161. struct device *dev = &pdev->dev;
  162. struct mtk_cec *cec;
  163. struct resource *res;
  164. int ret;
  165. cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
  166. if (!cec)
  167. return -ENOMEM;
  168. platform_set_drvdata(pdev, cec);
  169. spin_lock_init(&cec->lock);
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. cec->regs = devm_ioremap_resource(dev, res);
  172. if (IS_ERR(cec->regs)) {
  173. ret = PTR_ERR(cec->regs);
  174. dev_err(dev, "Failed to ioremap cec: %d\n", ret);
  175. return ret;
  176. }
  177. cec->clk = devm_clk_get(dev, NULL);
  178. if (IS_ERR(cec->clk)) {
  179. ret = PTR_ERR(cec->clk);
  180. dev_err(dev, "Failed to get cec clock: %d\n", ret);
  181. return ret;
  182. }
  183. cec->irq = platform_get_irq(pdev, 0);
  184. if (cec->irq < 0) {
  185. dev_err(dev, "Failed to get cec irq: %d\n", cec->irq);
  186. return cec->irq;
  187. }
  188. ret = devm_request_threaded_irq(dev, cec->irq, NULL,
  189. mtk_cec_htplg_isr_thread,
  190. IRQF_SHARED | IRQF_TRIGGER_LOW |
  191. IRQF_ONESHOT, "hdmi hpd", dev);
  192. if (ret) {
  193. dev_err(dev, "Failed to register cec irq: %d\n", ret);
  194. return ret;
  195. }
  196. ret = clk_prepare_enable(cec->clk);
  197. if (ret) {
  198. dev_err(dev, "Failed to enable cec clock: %d\n", ret);
  199. return ret;
  200. }
  201. mtk_cec_htplg_irq_init(cec);
  202. mtk_cec_htplg_irq_enable(cec);
  203. return 0;
  204. }
  205. static int mtk_cec_remove(struct platform_device *pdev)
  206. {
  207. struct mtk_cec *cec = platform_get_drvdata(pdev);
  208. mtk_cec_htplg_irq_disable(cec);
  209. clk_disable_unprepare(cec->clk);
  210. return 0;
  211. }
  212. static const struct of_device_id mtk_cec_of_ids[] = {
  213. { .compatible = "mediatek,mt8173-cec", },
  214. {}
  215. };
  216. struct platform_driver mtk_cec_driver = {
  217. .probe = mtk_cec_probe,
  218. .remove = mtk_cec_remove,
  219. .driver = {
  220. .name = "mediatek-cec",
  221. .of_match_table = mtk_cec_of_ids,
  222. },
  223. };