mtk_drm_ddp_comp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #include "mtk_drm_crtc.h"
  27. #define DISP_OD_EN 0x0000
  28. #define DISP_OD_INTEN 0x0008
  29. #define DISP_OD_INTSTA 0x000c
  30. #define DISP_OD_CFG 0x0020
  31. #define DISP_OD_SIZE 0x0030
  32. #define DISP_DITHER_5 0x0114
  33. #define DISP_DITHER_7 0x011c
  34. #define DISP_DITHER_15 0x013c
  35. #define DISP_DITHER_16 0x0140
  36. #define DISP_REG_UFO_START 0x0000
  37. #define DISP_AAL_EN 0x0000
  38. #define DISP_AAL_SIZE 0x0030
  39. #define DISP_GAMMA_EN 0x0000
  40. #define DISP_GAMMA_CFG 0x0020
  41. #define DISP_GAMMA_SIZE 0x0030
  42. #define DISP_GAMMA_LUT 0x0700
  43. #define LUT_10BIT_MASK 0x03ff
  44. #define OD_RELAYMODE BIT(0)
  45. #define UFO_BYPASS BIT(2)
  46. #define AAL_EN BIT(0)
  47. #define GAMMA_EN BIT(0)
  48. #define GAMMA_LUT_EN BIT(1)
  49. #define DISP_DITHERING BIT(2)
  50. #define DITHER_LSB_ERR_SHIFT_R(x) (((x) & 0x7) << 28)
  51. #define DITHER_OVFLW_BIT_R(x) (((x) & 0x7) << 24)
  52. #define DITHER_ADD_LSHIFT_R(x) (((x) & 0x7) << 20)
  53. #define DITHER_ADD_RSHIFT_R(x) (((x) & 0x7) << 16)
  54. #define DITHER_NEW_BIT_MODE BIT(0)
  55. #define DITHER_LSB_ERR_SHIFT_B(x) (((x) & 0x7) << 28)
  56. #define DITHER_OVFLW_BIT_B(x) (((x) & 0x7) << 24)
  57. #define DITHER_ADD_LSHIFT_B(x) (((x) & 0x7) << 20)
  58. #define DITHER_ADD_RSHIFT_B(x) (((x) & 0x7) << 16)
  59. #define DITHER_LSB_ERR_SHIFT_G(x) (((x) & 0x7) << 12)
  60. #define DITHER_OVFLW_BIT_G(x) (((x) & 0x7) << 8)
  61. #define DITHER_ADD_LSHIFT_G(x) (((x) & 0x7) << 4)
  62. #define DITHER_ADD_RSHIFT_G(x) (((x) & 0x7) << 0)
  63. void mtk_dither_set(struct mtk_ddp_comp *comp, unsigned int bpc,
  64. unsigned int CFG)
  65. {
  66. /* If bpc equal to 0, the dithering function didn't be enabled */
  67. if (bpc == 0)
  68. return;
  69. if (bpc >= MTK_MIN_BPC) {
  70. writel(0, comp->regs + DISP_DITHER_5);
  71. writel(0, comp->regs + DISP_DITHER_7);
  72. writel(DITHER_LSB_ERR_SHIFT_R(MTK_MAX_BPC - bpc) |
  73. DITHER_ADD_LSHIFT_R(MTK_MAX_BPC - bpc) |
  74. DITHER_NEW_BIT_MODE,
  75. comp->regs + DISP_DITHER_15);
  76. writel(DITHER_LSB_ERR_SHIFT_B(MTK_MAX_BPC - bpc) |
  77. DITHER_ADD_LSHIFT_B(MTK_MAX_BPC - bpc) |
  78. DITHER_LSB_ERR_SHIFT_G(MTK_MAX_BPC - bpc) |
  79. DITHER_ADD_LSHIFT_G(MTK_MAX_BPC - bpc),
  80. comp->regs + DISP_DITHER_16);
  81. writel(DISP_DITHERING, comp->regs + CFG);
  82. }
  83. }
  84. static void mtk_od_config(struct mtk_ddp_comp *comp, unsigned int w,
  85. unsigned int h, unsigned int vrefresh,
  86. unsigned int bpc)
  87. {
  88. writel(w << 16 | h, comp->regs + DISP_OD_SIZE);
  89. writel(OD_RELAYMODE, comp->regs + DISP_OD_CFG);
  90. mtk_dither_set(comp, bpc, DISP_OD_CFG);
  91. }
  92. static void mtk_od_start(struct mtk_ddp_comp *comp)
  93. {
  94. writel(1, comp->regs + DISP_OD_EN);
  95. }
  96. static void mtk_ufoe_start(struct mtk_ddp_comp *comp)
  97. {
  98. writel(UFO_BYPASS, comp->regs + DISP_REG_UFO_START);
  99. }
  100. static void mtk_aal_config(struct mtk_ddp_comp *comp, unsigned int w,
  101. unsigned int h, unsigned int vrefresh,
  102. unsigned int bpc)
  103. {
  104. writel(h << 16 | w, comp->regs + DISP_AAL_SIZE);
  105. }
  106. static void mtk_aal_start(struct mtk_ddp_comp *comp)
  107. {
  108. writel(AAL_EN, comp->regs + DISP_AAL_EN);
  109. }
  110. static void mtk_aal_stop(struct mtk_ddp_comp *comp)
  111. {
  112. writel_relaxed(0x0, comp->regs + DISP_AAL_EN);
  113. }
  114. static void mtk_gamma_config(struct mtk_ddp_comp *comp, unsigned int w,
  115. unsigned int h, unsigned int vrefresh,
  116. unsigned int bpc)
  117. {
  118. writel(h << 16 | w, comp->regs + DISP_GAMMA_SIZE);
  119. mtk_dither_set(comp, bpc, DISP_GAMMA_CFG);
  120. }
  121. static void mtk_gamma_start(struct mtk_ddp_comp *comp)
  122. {
  123. writel(GAMMA_EN, comp->regs + DISP_GAMMA_EN);
  124. }
  125. static void mtk_gamma_stop(struct mtk_ddp_comp *comp)
  126. {
  127. writel_relaxed(0x0, comp->regs + DISP_GAMMA_EN);
  128. }
  129. static void mtk_gamma_set(struct mtk_ddp_comp *comp,
  130. struct drm_crtc_state *state)
  131. {
  132. unsigned int i, reg;
  133. struct drm_color_lut *lut;
  134. void __iomem *lut_base;
  135. u32 word;
  136. if (state->gamma_lut) {
  137. reg = readl(comp->regs + DISP_GAMMA_CFG);
  138. reg = reg | GAMMA_LUT_EN;
  139. writel(reg, comp->regs + DISP_GAMMA_CFG);
  140. lut_base = comp->regs + DISP_GAMMA_LUT;
  141. lut = (struct drm_color_lut *)state->gamma_lut->data;
  142. for (i = 0; i < MTK_LUT_SIZE; i++) {
  143. word = (((lut[i].red >> 6) & LUT_10BIT_MASK) << 20) +
  144. (((lut[i].green >> 6) & LUT_10BIT_MASK) << 10) +
  145. ((lut[i].blue >> 6) & LUT_10BIT_MASK);
  146. writel(word, (lut_base + i * 4));
  147. }
  148. }
  149. }
  150. static const struct mtk_ddp_comp_funcs ddp_aal = {
  151. .gamma_set = mtk_gamma_set,
  152. .config = mtk_aal_config,
  153. .start = mtk_aal_start,
  154. .stop = mtk_aal_stop,
  155. };
  156. static const struct mtk_ddp_comp_funcs ddp_gamma = {
  157. .gamma_set = mtk_gamma_set,
  158. .config = mtk_gamma_config,
  159. .start = mtk_gamma_start,
  160. .stop = mtk_gamma_stop,
  161. };
  162. static const struct mtk_ddp_comp_funcs ddp_od = {
  163. .config = mtk_od_config,
  164. .start = mtk_od_start,
  165. };
  166. static const struct mtk_ddp_comp_funcs ddp_ufoe = {
  167. .start = mtk_ufoe_start,
  168. };
  169. static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
  170. [MTK_DISP_OVL] = "ovl",
  171. [MTK_DISP_RDMA] = "rdma",
  172. [MTK_DISP_WDMA] = "wdma",
  173. [MTK_DISP_COLOR] = "color",
  174. [MTK_DISP_AAL] = "aal",
  175. [MTK_DISP_GAMMA] = "gamma",
  176. [MTK_DISP_UFOE] = "ufoe",
  177. [MTK_DSI] = "dsi",
  178. [MTK_DPI] = "dpi",
  179. [MTK_DISP_PWM] = "pwm",
  180. [MTK_DISP_MUTEX] = "mutex",
  181. [MTK_DISP_OD] = "od",
  182. [MTK_DISP_BLS] = "bls",
  183. };
  184. struct mtk_ddp_comp_match {
  185. enum mtk_ddp_comp_type type;
  186. int alias_id;
  187. const struct mtk_ddp_comp_funcs *funcs;
  188. };
  189. static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_ID_MAX] = {
  190. [DDP_COMPONENT_AAL0] = { MTK_DISP_AAL, 0, &ddp_aal },
  191. [DDP_COMPONENT_AAL1] = { MTK_DISP_AAL, 1, &ddp_aal },
  192. [DDP_COMPONENT_BLS] = { MTK_DISP_BLS, 0, NULL },
  193. [DDP_COMPONENT_COLOR0] = { MTK_DISP_COLOR, 0, NULL },
  194. [DDP_COMPONENT_COLOR1] = { MTK_DISP_COLOR, 1, NULL },
  195. [DDP_COMPONENT_DPI0] = { MTK_DPI, 0, NULL },
  196. [DDP_COMPONENT_DPI1] = { MTK_DPI, 1, NULL },
  197. [DDP_COMPONENT_DSI0] = { MTK_DSI, 0, NULL },
  198. [DDP_COMPONENT_DSI1] = { MTK_DSI, 1, NULL },
  199. [DDP_COMPONENT_DSI2] = { MTK_DSI, 2, NULL },
  200. [DDP_COMPONENT_DSI3] = { MTK_DSI, 3, NULL },
  201. [DDP_COMPONENT_GAMMA] = { MTK_DISP_GAMMA, 0, &ddp_gamma },
  202. [DDP_COMPONENT_OD0] = { MTK_DISP_OD, 0, &ddp_od },
  203. [DDP_COMPONENT_OD1] = { MTK_DISP_OD, 1, &ddp_od },
  204. [DDP_COMPONENT_OVL0] = { MTK_DISP_OVL, 0, NULL },
  205. [DDP_COMPONENT_OVL1] = { MTK_DISP_OVL, 1, NULL },
  206. [DDP_COMPONENT_PWM0] = { MTK_DISP_PWM, 0, NULL },
  207. [DDP_COMPONENT_PWM1] = { MTK_DISP_PWM, 1, NULL },
  208. [DDP_COMPONENT_PWM2] = { MTK_DISP_PWM, 2, NULL },
  209. [DDP_COMPONENT_RDMA0] = { MTK_DISP_RDMA, 0, NULL },
  210. [DDP_COMPONENT_RDMA1] = { MTK_DISP_RDMA, 1, NULL },
  211. [DDP_COMPONENT_RDMA2] = { MTK_DISP_RDMA, 2, NULL },
  212. [DDP_COMPONENT_UFOE] = { MTK_DISP_UFOE, 0, &ddp_ufoe },
  213. [DDP_COMPONENT_WDMA0] = { MTK_DISP_WDMA, 0, NULL },
  214. [DDP_COMPONENT_WDMA1] = { MTK_DISP_WDMA, 1, NULL },
  215. };
  216. int mtk_ddp_comp_get_id(struct device_node *node,
  217. enum mtk_ddp_comp_type comp_type)
  218. {
  219. int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
  220. int i;
  221. for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
  222. if (comp_type == mtk_ddp_matches[i].type &&
  223. (id < 0 || id == mtk_ddp_matches[i].alias_id))
  224. return i;
  225. }
  226. return -EINVAL;
  227. }
  228. int mtk_ddp_comp_init(struct device *dev, struct device_node *node,
  229. struct mtk_ddp_comp *comp, enum mtk_ddp_comp_id comp_id,
  230. const struct mtk_ddp_comp_funcs *funcs)
  231. {
  232. enum mtk_ddp_comp_type type;
  233. struct device_node *larb_node;
  234. struct platform_device *larb_pdev;
  235. if (comp_id < 0 || comp_id >= DDP_COMPONENT_ID_MAX)
  236. return -EINVAL;
  237. type = mtk_ddp_matches[comp_id].type;
  238. comp->id = comp_id;
  239. comp->funcs = funcs ?: mtk_ddp_matches[comp_id].funcs;
  240. if (comp_id == DDP_COMPONENT_BLS ||
  241. comp_id == DDP_COMPONENT_DPI0 ||
  242. comp_id == DDP_COMPONENT_DPI1 ||
  243. comp_id == DDP_COMPONENT_DSI0 ||
  244. comp_id == DDP_COMPONENT_DSI1 ||
  245. comp_id == DDP_COMPONENT_DSI2 ||
  246. comp_id == DDP_COMPONENT_DSI3 ||
  247. comp_id == DDP_COMPONENT_PWM0) {
  248. comp->regs = NULL;
  249. comp->clk = NULL;
  250. comp->irq = 0;
  251. return 0;
  252. }
  253. comp->regs = of_iomap(node, 0);
  254. comp->irq = of_irq_get(node, 0);
  255. comp->clk = of_clk_get(node, 0);
  256. if (IS_ERR(comp->clk))
  257. return PTR_ERR(comp->clk);
  258. /* Only DMA capable components need the LARB property */
  259. comp->larb_dev = NULL;
  260. if (type != MTK_DISP_OVL &&
  261. type != MTK_DISP_RDMA &&
  262. type != MTK_DISP_WDMA)
  263. return 0;
  264. larb_node = of_parse_phandle(node, "mediatek,larb", 0);
  265. if (!larb_node) {
  266. dev_err(dev,
  267. "Missing mediadek,larb phandle in %pOF node\n", node);
  268. return -EINVAL;
  269. }
  270. larb_pdev = of_find_device_by_node(larb_node);
  271. if (!larb_pdev) {
  272. dev_warn(dev, "Waiting for larb device %pOF\n", larb_node);
  273. of_node_put(larb_node);
  274. return -EPROBE_DEFER;
  275. }
  276. of_node_put(larb_node);
  277. comp->larb_dev = &larb_pdev->dev;
  278. return 0;
  279. }
  280. int mtk_ddp_comp_register(struct drm_device *drm, struct mtk_ddp_comp *comp)
  281. {
  282. struct mtk_drm_private *private = drm->dev_private;
  283. if (private->ddp_comp[comp->id])
  284. return -EBUSY;
  285. private->ddp_comp[comp->id] = comp;
  286. return 0;
  287. }
  288. void mtk_ddp_comp_unregister(struct drm_device *drm, struct mtk_ddp_comp *comp)
  289. {
  290. struct mtk_drm_private *private = drm->dev_private;
  291. private->ddp_comp[comp->id] = NULL;
  292. }