mtk_disp_ovl.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <drm/drmP.h>
  14. #include <linux/clk.h>
  15. #include <linux/component.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include "mtk_drm_crtc.h"
  20. #include "mtk_drm_ddp_comp.h"
  21. #define DISP_REG_OVL_INTEN 0x0004
  22. #define OVL_FME_CPL_INT BIT(1)
  23. #define DISP_REG_OVL_INTSTA 0x0008
  24. #define DISP_REG_OVL_EN 0x000c
  25. #define DISP_REG_OVL_RST 0x0014
  26. #define DISP_REG_OVL_ROI_SIZE 0x0020
  27. #define DISP_REG_OVL_ROI_BGCLR 0x0028
  28. #define DISP_REG_OVL_SRC_CON 0x002c
  29. #define DISP_REG_OVL_CON(n) (0x0030 + 0x20 * (n))
  30. #define DISP_REG_OVL_SRC_SIZE(n) (0x0038 + 0x20 * (n))
  31. #define DISP_REG_OVL_OFFSET(n) (0x003c + 0x20 * (n))
  32. #define DISP_REG_OVL_PITCH(n) (0x0044 + 0x20 * (n))
  33. #define DISP_REG_OVL_RDMA_CTRL(n) (0x00c0 + 0x20 * (n))
  34. #define DISP_REG_OVL_RDMA_GMC(n) (0x00c8 + 0x20 * (n))
  35. #define DISP_REG_OVL_ADDR(n) (0x0f40 + 0x20 * (n))
  36. #define OVL_RDMA_MEM_GMC 0x40402020
  37. #define OVL_CON_BYTE_SWAP BIT(24)
  38. #define OVL_CON_CLRFMT_RGB565 (0 << 12)
  39. #define OVL_CON_CLRFMT_RGB888 (1 << 12)
  40. #define OVL_CON_CLRFMT_RGBA8888 (2 << 12)
  41. #define OVL_CON_CLRFMT_ARGB8888 (3 << 12)
  42. #define OVL_CON_AEN BIT(8)
  43. #define OVL_CON_ALPHA 0xff
  44. /**
  45. * struct mtk_disp_ovl - DISP_OVL driver structure
  46. * @ddp_comp - structure containing type enum and hardware resources
  47. * @crtc - associated crtc to report vblank events to
  48. */
  49. struct mtk_disp_ovl {
  50. struct mtk_ddp_comp ddp_comp;
  51. struct drm_crtc *crtc;
  52. };
  53. static irqreturn_t mtk_disp_ovl_irq_handler(int irq, void *dev_id)
  54. {
  55. struct mtk_disp_ovl *priv = dev_id;
  56. struct mtk_ddp_comp *ovl = &priv->ddp_comp;
  57. /* Clear frame completion interrupt */
  58. writel(0x0, ovl->regs + DISP_REG_OVL_INTSTA);
  59. if (!priv->crtc)
  60. return IRQ_NONE;
  61. mtk_crtc_ddp_irq(priv->crtc, ovl);
  62. return IRQ_HANDLED;
  63. }
  64. static void mtk_ovl_enable_vblank(struct mtk_ddp_comp *comp,
  65. struct drm_crtc *crtc)
  66. {
  67. struct mtk_disp_ovl *priv = container_of(comp, struct mtk_disp_ovl,
  68. ddp_comp);
  69. priv->crtc = crtc;
  70. writel(0x0, comp->regs + DISP_REG_OVL_INTSTA);
  71. writel_relaxed(OVL_FME_CPL_INT, comp->regs + DISP_REG_OVL_INTEN);
  72. }
  73. static void mtk_ovl_disable_vblank(struct mtk_ddp_comp *comp)
  74. {
  75. struct mtk_disp_ovl *priv = container_of(comp, struct mtk_disp_ovl,
  76. ddp_comp);
  77. priv->crtc = NULL;
  78. writel_relaxed(0x0, comp->regs + DISP_REG_OVL_INTEN);
  79. }
  80. static void mtk_ovl_start(struct mtk_ddp_comp *comp)
  81. {
  82. writel_relaxed(0x1, comp->regs + DISP_REG_OVL_EN);
  83. }
  84. static void mtk_ovl_stop(struct mtk_ddp_comp *comp)
  85. {
  86. writel_relaxed(0x0, comp->regs + DISP_REG_OVL_EN);
  87. }
  88. static void mtk_ovl_config(struct mtk_ddp_comp *comp, unsigned int w,
  89. unsigned int h, unsigned int vrefresh,
  90. unsigned int bpc)
  91. {
  92. if (w != 0 && h != 0)
  93. writel_relaxed(h << 16 | w, comp->regs + DISP_REG_OVL_ROI_SIZE);
  94. writel_relaxed(0x0, comp->regs + DISP_REG_OVL_ROI_BGCLR);
  95. writel(0x1, comp->regs + DISP_REG_OVL_RST);
  96. writel(0x0, comp->regs + DISP_REG_OVL_RST);
  97. }
  98. static void mtk_ovl_layer_on(struct mtk_ddp_comp *comp, unsigned int idx)
  99. {
  100. unsigned int reg;
  101. writel(0x1, comp->regs + DISP_REG_OVL_RDMA_CTRL(idx));
  102. writel(OVL_RDMA_MEM_GMC, comp->regs + DISP_REG_OVL_RDMA_GMC(idx));
  103. reg = readl(comp->regs + DISP_REG_OVL_SRC_CON);
  104. reg = reg | BIT(idx);
  105. writel(reg, comp->regs + DISP_REG_OVL_SRC_CON);
  106. }
  107. static void mtk_ovl_layer_off(struct mtk_ddp_comp *comp, unsigned int idx)
  108. {
  109. unsigned int reg;
  110. reg = readl(comp->regs + DISP_REG_OVL_SRC_CON);
  111. reg = reg & ~BIT(idx);
  112. writel(reg, comp->regs + DISP_REG_OVL_SRC_CON);
  113. writel(0x0, comp->regs + DISP_REG_OVL_RDMA_CTRL(idx));
  114. }
  115. static unsigned int ovl_fmt_convert(unsigned int fmt)
  116. {
  117. switch (fmt) {
  118. default:
  119. case DRM_FORMAT_RGB565:
  120. return OVL_CON_CLRFMT_RGB565;
  121. case DRM_FORMAT_BGR565:
  122. return OVL_CON_CLRFMT_RGB565 | OVL_CON_BYTE_SWAP;
  123. case DRM_FORMAT_RGB888:
  124. return OVL_CON_CLRFMT_RGB888;
  125. case DRM_FORMAT_BGR888:
  126. return OVL_CON_CLRFMT_RGB888 | OVL_CON_BYTE_SWAP;
  127. case DRM_FORMAT_RGBX8888:
  128. case DRM_FORMAT_RGBA8888:
  129. return OVL_CON_CLRFMT_ARGB8888;
  130. case DRM_FORMAT_BGRX8888:
  131. case DRM_FORMAT_BGRA8888:
  132. return OVL_CON_CLRFMT_ARGB8888 | OVL_CON_BYTE_SWAP;
  133. case DRM_FORMAT_XRGB8888:
  134. case DRM_FORMAT_ARGB8888:
  135. return OVL_CON_CLRFMT_RGBA8888;
  136. case DRM_FORMAT_XBGR8888:
  137. case DRM_FORMAT_ABGR8888:
  138. return OVL_CON_CLRFMT_RGBA8888 | OVL_CON_BYTE_SWAP;
  139. }
  140. }
  141. static void mtk_ovl_layer_config(struct mtk_ddp_comp *comp, unsigned int idx,
  142. struct mtk_plane_state *state)
  143. {
  144. struct mtk_plane_pending_state *pending = &state->pending;
  145. unsigned int addr = pending->addr;
  146. unsigned int pitch = pending->pitch & 0xffff;
  147. unsigned int fmt = pending->format;
  148. unsigned int offset = (pending->y << 16) | pending->x;
  149. unsigned int src_size = (pending->height << 16) | pending->width;
  150. unsigned int con;
  151. if (!pending->enable)
  152. mtk_ovl_layer_off(comp, idx);
  153. con = ovl_fmt_convert(fmt);
  154. if (idx != 0)
  155. con |= OVL_CON_AEN | OVL_CON_ALPHA;
  156. writel_relaxed(con, comp->regs + DISP_REG_OVL_CON(idx));
  157. writel_relaxed(pitch, comp->regs + DISP_REG_OVL_PITCH(idx));
  158. writel_relaxed(src_size, comp->regs + DISP_REG_OVL_SRC_SIZE(idx));
  159. writel_relaxed(offset, comp->regs + DISP_REG_OVL_OFFSET(idx));
  160. writel_relaxed(addr, comp->regs + DISP_REG_OVL_ADDR(idx));
  161. if (pending->enable)
  162. mtk_ovl_layer_on(comp, idx);
  163. }
  164. static const struct mtk_ddp_comp_funcs mtk_disp_ovl_funcs = {
  165. .config = mtk_ovl_config,
  166. .start = mtk_ovl_start,
  167. .stop = mtk_ovl_stop,
  168. .enable_vblank = mtk_ovl_enable_vblank,
  169. .disable_vblank = mtk_ovl_disable_vblank,
  170. .layer_on = mtk_ovl_layer_on,
  171. .layer_off = mtk_ovl_layer_off,
  172. .layer_config = mtk_ovl_layer_config,
  173. };
  174. static int mtk_disp_ovl_bind(struct device *dev, struct device *master,
  175. void *data)
  176. {
  177. struct mtk_disp_ovl *priv = dev_get_drvdata(dev);
  178. struct drm_device *drm_dev = data;
  179. int ret;
  180. ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
  181. if (ret < 0) {
  182. dev_err(dev, "Failed to register component %s: %d\n",
  183. dev->of_node->full_name, ret);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static void mtk_disp_ovl_unbind(struct device *dev, struct device *master,
  189. void *data)
  190. {
  191. struct mtk_disp_ovl *priv = dev_get_drvdata(dev);
  192. struct drm_device *drm_dev = data;
  193. mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
  194. }
  195. static const struct component_ops mtk_disp_ovl_component_ops = {
  196. .bind = mtk_disp_ovl_bind,
  197. .unbind = mtk_disp_ovl_unbind,
  198. };
  199. static int mtk_disp_ovl_probe(struct platform_device *pdev)
  200. {
  201. struct device *dev = &pdev->dev;
  202. struct mtk_disp_ovl *priv;
  203. int comp_id;
  204. int irq;
  205. int ret;
  206. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  207. if (!priv)
  208. return -ENOMEM;
  209. irq = platform_get_irq(pdev, 0);
  210. if (irq < 0)
  211. return irq;
  212. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_OVL);
  213. if (comp_id < 0) {
  214. dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
  215. return comp_id;
  216. }
  217. ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
  218. &mtk_disp_ovl_funcs);
  219. if (ret) {
  220. dev_err(dev, "Failed to initialize component: %d\n", ret);
  221. return ret;
  222. }
  223. platform_set_drvdata(pdev, priv);
  224. ret = devm_request_irq(dev, irq, mtk_disp_ovl_irq_handler,
  225. IRQF_TRIGGER_NONE, dev_name(dev), priv);
  226. if (ret < 0) {
  227. dev_err(dev, "Failed to request irq %d: %d\n", irq, ret);
  228. return ret;
  229. }
  230. ret = component_add(dev, &mtk_disp_ovl_component_ops);
  231. if (ret)
  232. dev_err(dev, "Failed to add component: %d\n", ret);
  233. return ret;
  234. }
  235. static int mtk_disp_ovl_remove(struct platform_device *pdev)
  236. {
  237. component_del(&pdev->dev, &mtk_disp_ovl_component_ops);
  238. return 0;
  239. }
  240. static const struct of_device_id mtk_disp_ovl_driver_dt_match[] = {
  241. { .compatible = "mediatek,mt8173-disp-ovl", },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, mtk_disp_ovl_driver_dt_match);
  245. struct platform_driver mtk_disp_ovl_driver = {
  246. .probe = mtk_disp_ovl_probe,
  247. .remove = mtk_disp_ovl_remove,
  248. .driver = {
  249. .name = "mediatek-disp-ovl",
  250. .owner = THIS_MODULE,
  251. .of_match_table = mtk_disp_ovl_driver_dt_match,
  252. },
  253. };