mtk_drm_ddp_comp.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Authors:
  4. * YT Shen <yt.shen@mediatek.com>
  5. * CK Hu <ck.hu@mediatek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <drm/drmP.h>
  23. #include "mtk_drm_drv.h"
  24. #include "mtk_drm_plane.h"
  25. #include "mtk_drm_ddp_comp.h"
  26. #define DISP_OD_EN 0x0000
  27. #define DISP_OD_INTEN 0x0008
  28. #define DISP_OD_INTSTA 0x000c
  29. #define DISP_OD_CFG 0x0020
  30. #define DISP_OD_SIZE 0x0030
  31. #define DISP_REG_UFO_START 0x0000
  32. #define DISP_COLOR_CFG_MAIN 0x0400
  33. #define DISP_COLOR_START 0x0c00
  34. #define DISP_COLOR_WIDTH 0x0c50
  35. #define DISP_COLOR_HEIGHT 0x0c54
  36. #define OD_RELAY_MODE BIT(0)
  37. #define UFO_BYPASS BIT(2)
  38. #define COLOR_BYPASS_ALL BIT(7)
  39. #define COLOR_SEQ_SEL BIT(13)
  40. static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
  41. unsigned int h, unsigned int vrefresh)
  42. {
  43. writel(w, comp->regs + DISP_COLOR_WIDTH);
  44. writel(h, comp->regs + DISP_COLOR_HEIGHT);
  45. }
  46. static void mtk_color_start(struct mtk_ddp_comp *comp)
  47. {
  48. writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
  49. comp->regs + DISP_COLOR_CFG_MAIN);
  50. writel(0x1, comp->regs + DISP_COLOR_START);
  51. }
  52. static void mtk_od_config(struct mtk_ddp_comp *comp, unsigned int w,
  53. unsigned int h, unsigned int vrefresh)
  54. {
  55. writel(w << 16 | h, comp->regs + DISP_OD_SIZE);
  56. }
  57. static void mtk_od_start(struct mtk_ddp_comp *comp)
  58. {
  59. writel(OD_RELAY_MODE, comp->regs + DISP_OD_CFG);
  60. writel(1, comp->regs + DISP_OD_EN);
  61. }
  62. static void mtk_ufoe_start(struct mtk_ddp_comp *comp)
  63. {
  64. writel(UFO_BYPASS, comp->regs + DISP_REG_UFO_START);
  65. }
  66. static const struct mtk_ddp_comp_funcs ddp_color = {
  67. .config = mtk_color_config,
  68. .start = mtk_color_start,
  69. };
  70. static const struct mtk_ddp_comp_funcs ddp_od = {
  71. .config = mtk_od_config,
  72. .start = mtk_od_start,
  73. };
  74. static const struct mtk_ddp_comp_funcs ddp_ufoe = {
  75. .start = mtk_ufoe_start,
  76. };
  77. static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
  78. [MTK_DISP_OVL] = "ovl",
  79. [MTK_DISP_RDMA] = "rdma",
  80. [MTK_DISP_WDMA] = "wdma",
  81. [MTK_DISP_COLOR] = "color",
  82. [MTK_DISP_AAL] = "aal",
  83. [MTK_DISP_GAMMA] = "gamma",
  84. [MTK_DISP_UFOE] = "ufoe",
  85. [MTK_DSI] = "dsi",
  86. [MTK_DPI] = "dpi",
  87. [MTK_DISP_PWM] = "pwm",
  88. [MTK_DISP_MUTEX] = "mutex",
  89. [MTK_DISP_OD] = "od",
  90. };
  91. struct mtk_ddp_comp_match {
  92. enum mtk_ddp_comp_type type;
  93. int alias_id;
  94. const struct mtk_ddp_comp_funcs *funcs;
  95. };
  96. static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_ID_MAX] = {
  97. [DDP_COMPONENT_AAL] = { MTK_DISP_AAL, 0, NULL },
  98. [DDP_COMPONENT_COLOR0] = { MTK_DISP_COLOR, 0, &ddp_color },
  99. [DDP_COMPONENT_COLOR1] = { MTK_DISP_COLOR, 1, &ddp_color },
  100. [DDP_COMPONENT_DPI0] = { MTK_DPI, 0, NULL },
  101. [DDP_COMPONENT_DSI0] = { MTK_DSI, 0, NULL },
  102. [DDP_COMPONENT_DSI1] = { MTK_DSI, 1, NULL },
  103. [DDP_COMPONENT_GAMMA] = { MTK_DISP_GAMMA, 0, NULL },
  104. [DDP_COMPONENT_OD] = { MTK_DISP_OD, 0, &ddp_od },
  105. [DDP_COMPONENT_OVL0] = { MTK_DISP_OVL, 0, NULL },
  106. [DDP_COMPONENT_OVL1] = { MTK_DISP_OVL, 1, NULL },
  107. [DDP_COMPONENT_PWM0] = { MTK_DISP_PWM, 0, NULL },
  108. [DDP_COMPONENT_RDMA0] = { MTK_DISP_RDMA, 0, NULL },
  109. [DDP_COMPONENT_RDMA1] = { MTK_DISP_RDMA, 1, NULL },
  110. [DDP_COMPONENT_RDMA2] = { MTK_DISP_RDMA, 2, NULL },
  111. [DDP_COMPONENT_UFOE] = { MTK_DISP_UFOE, 0, &ddp_ufoe },
  112. [DDP_COMPONENT_WDMA0] = { MTK_DISP_WDMA, 0, NULL },
  113. [DDP_COMPONENT_WDMA1] = { MTK_DISP_WDMA, 1, NULL },
  114. };
  115. int mtk_ddp_comp_get_id(struct device_node *node,
  116. enum mtk_ddp_comp_type comp_type)
  117. {
  118. int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
  119. int i;
  120. for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
  121. if (comp_type == mtk_ddp_matches[i].type &&
  122. (id < 0 || id == mtk_ddp_matches[i].alias_id))
  123. return i;
  124. }
  125. return -EINVAL;
  126. }
  127. int mtk_ddp_comp_init(struct device *dev, struct device_node *node,
  128. struct mtk_ddp_comp *comp, enum mtk_ddp_comp_id comp_id,
  129. const struct mtk_ddp_comp_funcs *funcs)
  130. {
  131. enum mtk_ddp_comp_type type;
  132. struct device_node *larb_node;
  133. struct platform_device *larb_pdev;
  134. if (comp_id < 0 || comp_id >= DDP_COMPONENT_ID_MAX)
  135. return -EINVAL;
  136. comp->id = comp_id;
  137. comp->funcs = funcs ?: mtk_ddp_matches[comp_id].funcs;
  138. if (comp_id == DDP_COMPONENT_DPI0 ||
  139. comp_id == DDP_COMPONENT_DSI0 ||
  140. comp_id == DDP_COMPONENT_PWM0) {
  141. comp->regs = NULL;
  142. comp->clk = NULL;
  143. comp->irq = 0;
  144. return 0;
  145. }
  146. comp->regs = of_iomap(node, 0);
  147. comp->irq = of_irq_get(node, 0);
  148. comp->clk = of_clk_get(node, 0);
  149. if (IS_ERR(comp->clk))
  150. comp->clk = NULL;
  151. type = mtk_ddp_matches[comp_id].type;
  152. /* Only DMA capable components need the LARB property */
  153. comp->larb_dev = NULL;
  154. if (type != MTK_DISP_OVL &&
  155. type != MTK_DISP_RDMA &&
  156. type != MTK_DISP_WDMA)
  157. return 0;
  158. larb_node = of_parse_phandle(node, "mediatek,larb", 0);
  159. if (!larb_node) {
  160. dev_err(dev,
  161. "Missing mediadek,larb phandle in %s node\n",
  162. node->full_name);
  163. return -EINVAL;
  164. }
  165. larb_pdev = of_find_device_by_node(larb_node);
  166. if (!larb_pdev) {
  167. dev_warn(dev, "Waiting for larb device %s\n",
  168. larb_node->full_name);
  169. of_node_put(larb_node);
  170. return -EPROBE_DEFER;
  171. }
  172. of_node_put(larb_node);
  173. comp->larb_dev = &larb_pdev->dev;
  174. return 0;
  175. }
  176. int mtk_ddp_comp_register(struct drm_device *drm, struct mtk_ddp_comp *comp)
  177. {
  178. struct mtk_drm_private *private = drm->dev_private;
  179. if (private->ddp_comp[comp->id])
  180. return -EBUSY;
  181. private->ddp_comp[comp->id] = comp;
  182. return 0;
  183. }
  184. void mtk_ddp_comp_unregister(struct drm_device *drm, struct mtk_ddp_comp *comp)
  185. {
  186. struct mtk_drm_private *private = drm->dev_private;
  187. private->ddp_comp[comp->id] = NULL;
  188. }