mtk_drm_ddp_comp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_COLOR_CFG_MAIN 0x0400
  38. #define DISP_COLOR_START_MT2701 0x0f00
  39. #define DISP_COLOR_START_MT8173 0x0c00
  40. #define DISP_COLOR_START(comp) ((comp)->data->color_offset)
  41. #define DISP_COLOR_WIDTH(comp) (DISP_COLOR_START(comp) + 0x50)
  42. #define DISP_COLOR_HEIGHT(comp) (DISP_COLOR_START(comp) + 0x54)
  43. #define DISP_AAL_EN 0x0000
  44. #define DISP_AAL_SIZE 0x0030
  45. #define DISP_GAMMA_EN 0x0000
  46. #define DISP_GAMMA_CFG 0x0020
  47. #define DISP_GAMMA_SIZE 0x0030
  48. #define DISP_GAMMA_LUT 0x0700
  49. #define LUT_10BIT_MASK 0x03ff
  50. #define COLOR_BYPASS_ALL BIT(7)
  51. #define COLOR_SEQ_SEL BIT(13)
  52. #define OD_RELAYMODE BIT(0)
  53. #define UFO_BYPASS BIT(2)
  54. #define AAL_EN BIT(0)
  55. #define GAMMA_EN BIT(0)
  56. #define GAMMA_LUT_EN BIT(1)
  57. #define DISP_DITHERING BIT(2)
  58. #define DITHER_LSB_ERR_SHIFT_R(x) (((x) & 0x7) << 28)
  59. #define DITHER_OVFLW_BIT_R(x) (((x) & 0x7) << 24)
  60. #define DITHER_ADD_LSHIFT_R(x) (((x) & 0x7) << 20)
  61. #define DITHER_ADD_RSHIFT_R(x) (((x) & 0x7) << 16)
  62. #define DITHER_NEW_BIT_MODE BIT(0)
  63. #define DITHER_LSB_ERR_SHIFT_B(x) (((x) & 0x7) << 28)
  64. #define DITHER_OVFLW_BIT_B(x) (((x) & 0x7) << 24)
  65. #define DITHER_ADD_LSHIFT_B(x) (((x) & 0x7) << 20)
  66. #define DITHER_ADD_RSHIFT_B(x) (((x) & 0x7) << 16)
  67. #define DITHER_LSB_ERR_SHIFT_G(x) (((x) & 0x7) << 12)
  68. #define DITHER_OVFLW_BIT_G(x) (((x) & 0x7) << 8)
  69. #define DITHER_ADD_LSHIFT_G(x) (((x) & 0x7) << 4)
  70. #define DITHER_ADD_RSHIFT_G(x) (((x) & 0x7) << 0)
  71. struct mtk_disp_color_data {
  72. unsigned int color_offset;
  73. };
  74. struct mtk_disp_color {
  75. struct mtk_ddp_comp ddp_comp;
  76. const struct mtk_disp_color_data *data;
  77. };
  78. static inline struct mtk_disp_color *comp_to_color(struct mtk_ddp_comp *comp)
  79. {
  80. return container_of(comp, struct mtk_disp_color, ddp_comp);
  81. }
  82. void mtk_dither_set(struct mtk_ddp_comp *comp, unsigned int bpc,
  83. unsigned int CFG)
  84. {
  85. /* If bpc equal to 0, the dithering function didn't be enabled */
  86. if (bpc == 0)
  87. return;
  88. if (bpc >= MTK_MIN_BPC) {
  89. writel(0, comp->regs + DISP_DITHER_5);
  90. writel(0, comp->regs + DISP_DITHER_7);
  91. writel(DITHER_LSB_ERR_SHIFT_R(MTK_MAX_BPC - bpc) |
  92. DITHER_ADD_LSHIFT_R(MTK_MAX_BPC - bpc) |
  93. DITHER_NEW_BIT_MODE,
  94. comp->regs + DISP_DITHER_15);
  95. writel(DITHER_LSB_ERR_SHIFT_B(MTK_MAX_BPC - bpc) |
  96. DITHER_ADD_LSHIFT_B(MTK_MAX_BPC - bpc) |
  97. DITHER_LSB_ERR_SHIFT_G(MTK_MAX_BPC - bpc) |
  98. DITHER_ADD_LSHIFT_G(MTK_MAX_BPC - bpc),
  99. comp->regs + DISP_DITHER_16);
  100. writel(DISP_DITHERING, comp->regs + CFG);
  101. }
  102. }
  103. static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
  104. unsigned int h, unsigned int vrefresh,
  105. unsigned int bpc)
  106. {
  107. struct mtk_disp_color *color = comp_to_color(comp);
  108. writel(w, comp->regs + DISP_COLOR_WIDTH(color));
  109. writel(h, comp->regs + DISP_COLOR_HEIGHT(color));
  110. }
  111. static void mtk_color_start(struct mtk_ddp_comp *comp)
  112. {
  113. struct mtk_disp_color *color = comp_to_color(comp);
  114. writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
  115. comp->regs + DISP_COLOR_CFG_MAIN);
  116. writel(0x1, comp->regs + DISP_COLOR_START(color));
  117. }
  118. static void mtk_od_config(struct mtk_ddp_comp *comp, unsigned int w,
  119. unsigned int h, unsigned int vrefresh,
  120. unsigned int bpc)
  121. {
  122. writel(w << 16 | h, comp->regs + DISP_OD_SIZE);
  123. writel(OD_RELAYMODE, comp->regs + DISP_OD_CFG);
  124. mtk_dither_set(comp, bpc, DISP_OD_CFG);
  125. }
  126. static void mtk_od_start(struct mtk_ddp_comp *comp)
  127. {
  128. writel(1, comp->regs + DISP_OD_EN);
  129. }
  130. static void mtk_ufoe_start(struct mtk_ddp_comp *comp)
  131. {
  132. writel(UFO_BYPASS, comp->regs + DISP_REG_UFO_START);
  133. }
  134. static void mtk_aal_config(struct mtk_ddp_comp *comp, unsigned int w,
  135. unsigned int h, unsigned int vrefresh,
  136. unsigned int bpc)
  137. {
  138. writel(h << 16 | w, comp->regs + DISP_AAL_SIZE);
  139. }
  140. static void mtk_aal_start(struct mtk_ddp_comp *comp)
  141. {
  142. writel(AAL_EN, comp->regs + DISP_AAL_EN);
  143. }
  144. static void mtk_aal_stop(struct mtk_ddp_comp *comp)
  145. {
  146. writel_relaxed(0x0, comp->regs + DISP_AAL_EN);
  147. }
  148. static void mtk_gamma_config(struct mtk_ddp_comp *comp, unsigned int w,
  149. unsigned int h, unsigned int vrefresh,
  150. unsigned int bpc)
  151. {
  152. writel(h << 16 | w, comp->regs + DISP_GAMMA_SIZE);
  153. mtk_dither_set(comp, bpc, DISP_GAMMA_CFG);
  154. }
  155. static void mtk_gamma_start(struct mtk_ddp_comp *comp)
  156. {
  157. writel(GAMMA_EN, comp->regs + DISP_GAMMA_EN);
  158. }
  159. static void mtk_gamma_stop(struct mtk_ddp_comp *comp)
  160. {
  161. writel_relaxed(0x0, comp->regs + DISP_GAMMA_EN);
  162. }
  163. static void mtk_gamma_set(struct mtk_ddp_comp *comp,
  164. struct drm_crtc_state *state)
  165. {
  166. unsigned int i, reg;
  167. struct drm_color_lut *lut;
  168. void __iomem *lut_base;
  169. u32 word;
  170. if (state->gamma_lut) {
  171. reg = readl(comp->regs + DISP_GAMMA_CFG);
  172. reg = reg | GAMMA_LUT_EN;
  173. writel(reg, comp->regs + DISP_GAMMA_CFG);
  174. lut_base = comp->regs + DISP_GAMMA_LUT;
  175. lut = (struct drm_color_lut *)state->gamma_lut->data;
  176. for (i = 0; i < MTK_LUT_SIZE; i++) {
  177. word = (((lut[i].red >> 6) & LUT_10BIT_MASK) << 20) +
  178. (((lut[i].green >> 6) & LUT_10BIT_MASK) << 10) +
  179. ((lut[i].blue >> 6) & LUT_10BIT_MASK);
  180. writel(word, (lut_base + i * 4));
  181. }
  182. }
  183. }
  184. static const struct mtk_ddp_comp_funcs ddp_aal = {
  185. .gamma_set = mtk_gamma_set,
  186. .config = mtk_aal_config,
  187. .start = mtk_aal_start,
  188. .stop = mtk_aal_stop,
  189. };
  190. static const struct mtk_ddp_comp_funcs ddp_gamma = {
  191. .gamma_set = mtk_gamma_set,
  192. .config = mtk_gamma_config,
  193. .start = mtk_gamma_start,
  194. .stop = mtk_gamma_stop,
  195. };
  196. static const struct mtk_ddp_comp_funcs ddp_color = {
  197. .config = mtk_color_config,
  198. .start = mtk_color_start,
  199. };
  200. static const struct mtk_ddp_comp_funcs ddp_od = {
  201. .config = mtk_od_config,
  202. .start = mtk_od_start,
  203. };
  204. static const struct mtk_ddp_comp_funcs ddp_ufoe = {
  205. .start = mtk_ufoe_start,
  206. };
  207. static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
  208. [MTK_DISP_OVL] = "ovl",
  209. [MTK_DISP_RDMA] = "rdma",
  210. [MTK_DISP_WDMA] = "wdma",
  211. [MTK_DISP_COLOR] = "color",
  212. [MTK_DISP_AAL] = "aal",
  213. [MTK_DISP_GAMMA] = "gamma",
  214. [MTK_DISP_UFOE] = "ufoe",
  215. [MTK_DSI] = "dsi",
  216. [MTK_DPI] = "dpi",
  217. [MTK_DISP_PWM] = "pwm",
  218. [MTK_DISP_MUTEX] = "mutex",
  219. [MTK_DISP_OD] = "od",
  220. [MTK_DISP_BLS] = "bls",
  221. };
  222. struct mtk_ddp_comp_match {
  223. enum mtk_ddp_comp_type type;
  224. int alias_id;
  225. const struct mtk_ddp_comp_funcs *funcs;
  226. };
  227. static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_ID_MAX] = {
  228. [DDP_COMPONENT_AAL] = { MTK_DISP_AAL, 0, &ddp_aal },
  229. [DDP_COMPONENT_BLS] = { MTK_DISP_BLS, 0, NULL },
  230. [DDP_COMPONENT_COLOR0] = { MTK_DISP_COLOR, 0, &ddp_color },
  231. [DDP_COMPONENT_COLOR1] = { MTK_DISP_COLOR, 1, &ddp_color },
  232. [DDP_COMPONENT_DPI0] = { MTK_DPI, 0, NULL },
  233. [DDP_COMPONENT_DSI0] = { MTK_DSI, 0, NULL },
  234. [DDP_COMPONENT_DSI1] = { MTK_DSI, 1, NULL },
  235. [DDP_COMPONENT_GAMMA] = { MTK_DISP_GAMMA, 0, &ddp_gamma },
  236. [DDP_COMPONENT_OD] = { MTK_DISP_OD, 0, &ddp_od },
  237. [DDP_COMPONENT_OVL0] = { MTK_DISP_OVL, 0, NULL },
  238. [DDP_COMPONENT_OVL1] = { MTK_DISP_OVL, 1, NULL },
  239. [DDP_COMPONENT_PWM0] = { MTK_DISP_PWM, 0, NULL },
  240. [DDP_COMPONENT_RDMA0] = { MTK_DISP_RDMA, 0, NULL },
  241. [DDP_COMPONENT_RDMA1] = { MTK_DISP_RDMA, 1, NULL },
  242. [DDP_COMPONENT_RDMA2] = { MTK_DISP_RDMA, 2, NULL },
  243. [DDP_COMPONENT_UFOE] = { MTK_DISP_UFOE, 0, &ddp_ufoe },
  244. [DDP_COMPONENT_WDMA0] = { MTK_DISP_WDMA, 0, NULL },
  245. [DDP_COMPONENT_WDMA1] = { MTK_DISP_WDMA, 1, NULL },
  246. };
  247. static const struct mtk_disp_color_data mt2701_color_driver_data = {
  248. .color_offset = DISP_COLOR_START_MT2701,
  249. };
  250. static const struct mtk_disp_color_data mt8173_color_driver_data = {
  251. .color_offset = DISP_COLOR_START_MT8173,
  252. };
  253. static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
  254. { .compatible = "mediatek,mt2701-disp-color",
  255. .data = &mt2701_color_driver_data},
  256. { .compatible = "mediatek,mt8173-disp-color",
  257. .data = &mt8173_color_driver_data},
  258. {},
  259. };
  260. int mtk_ddp_comp_get_id(struct device_node *node,
  261. enum mtk_ddp_comp_type comp_type)
  262. {
  263. int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
  264. int i;
  265. for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
  266. if (comp_type == mtk_ddp_matches[i].type &&
  267. (id < 0 || id == mtk_ddp_matches[i].alias_id))
  268. return i;
  269. }
  270. return -EINVAL;
  271. }
  272. int mtk_ddp_comp_init(struct device *dev, struct device_node *node,
  273. struct mtk_ddp_comp *comp, enum mtk_ddp_comp_id comp_id,
  274. const struct mtk_ddp_comp_funcs *funcs)
  275. {
  276. enum mtk_ddp_comp_type type;
  277. struct device_node *larb_node;
  278. struct platform_device *larb_pdev;
  279. const struct of_device_id *match;
  280. struct mtk_disp_color *color;
  281. if (comp_id < 0 || comp_id >= DDP_COMPONENT_ID_MAX)
  282. return -EINVAL;
  283. type = mtk_ddp_matches[comp_id].type;
  284. if (type == MTK_DISP_COLOR) {
  285. devm_kfree(dev, comp);
  286. color = devm_kzalloc(dev, sizeof(*color), GFP_KERNEL);
  287. if (!color)
  288. return -ENOMEM;
  289. match = of_match_node(mtk_disp_color_driver_dt_match, node);
  290. color->data = match->data;
  291. comp = &color->ddp_comp;
  292. }
  293. comp->id = comp_id;
  294. comp->funcs = funcs ?: mtk_ddp_matches[comp_id].funcs;
  295. if (comp_id == DDP_COMPONENT_BLS ||
  296. comp_id == DDP_COMPONENT_DPI0 ||
  297. comp_id == DDP_COMPONENT_DSI0 ||
  298. comp_id == DDP_COMPONENT_PWM0) {
  299. comp->regs = NULL;
  300. comp->clk = NULL;
  301. comp->irq = 0;
  302. return 0;
  303. }
  304. comp->regs = of_iomap(node, 0);
  305. comp->irq = of_irq_get(node, 0);
  306. comp->clk = of_clk_get(node, 0);
  307. if (IS_ERR(comp->clk))
  308. comp->clk = NULL;
  309. /* Only DMA capable components need the LARB property */
  310. comp->larb_dev = NULL;
  311. if (type != MTK_DISP_OVL &&
  312. type != MTK_DISP_RDMA &&
  313. type != MTK_DISP_WDMA)
  314. return 0;
  315. larb_node = of_parse_phandle(node, "mediatek,larb", 0);
  316. if (!larb_node) {
  317. dev_err(dev,
  318. "Missing mediadek,larb phandle in %s node\n",
  319. node->full_name);
  320. return -EINVAL;
  321. }
  322. larb_pdev = of_find_device_by_node(larb_node);
  323. if (!larb_pdev) {
  324. dev_warn(dev, "Waiting for larb device %s\n",
  325. larb_node->full_name);
  326. of_node_put(larb_node);
  327. return -EPROBE_DEFER;
  328. }
  329. of_node_put(larb_node);
  330. comp->larb_dev = &larb_pdev->dev;
  331. return 0;
  332. }
  333. int mtk_ddp_comp_register(struct drm_device *drm, struct mtk_ddp_comp *comp)
  334. {
  335. struct mtk_drm_private *private = drm->dev_private;
  336. if (private->ddp_comp[comp->id])
  337. return -EBUSY;
  338. private->ddp_comp[comp->id] = comp;
  339. return 0;
  340. }
  341. void mtk_ddp_comp_unregister(struct drm_device *drm, struct mtk_ddp_comp *comp)
  342. {
  343. struct mtk_drm_private *private = drm->dev_private;
  344. private->ddp_comp[comp->id] = NULL;
  345. }