mtk_dmdp_aal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) 2019 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 <linux/pm_runtime.h>
  20. #include <linux/soc/mediatek/mtk-cmdq.h>
  21. #include "mtk_drm_crtc.h"
  22. #include "mtk_drm_ddp_comp.h"
  23. #include "mtk_drm_drv.h"
  24. #include "mtk_log.h"
  25. #include "mtk_dump.h"
  26. #define DMDP_AAL_EN 0x0000
  27. #define DMDP_AAL_CFG 0x0020
  28. #define DMDP_AAL_CFG_MAIN 0x0200
  29. #define DMDP_AAL_SIZE 0x0030
  30. #define DMDP_AAL_OUTPUT_SIZE 0x0034
  31. #define DMDP_AAL_SHADOW_CTRL 0x0F0
  32. #define AAL_BYPASS_SHADOW BIT(0)
  33. #define AAL_READ_WRK_REG BIT(2)
  34. #define DMDP_AAL_DRE_BITPLUS_00 0x048C
  35. #define DMDP_AAL_DRE_BILATERAL 0x053C
  36. #define DMDP_AAL_Y2R_00 0x04BC
  37. #define DMDP_AAL_R2Y_00 0x04D4
  38. #define AAL_EN BIT(0)
  39. struct mtk_dmdp_aal_data {
  40. bool support_shadow;
  41. u32 block_info_00_mask;
  42. };
  43. struct mtk_dmdp_aal {
  44. struct mtk_ddp_comp ddp_comp;
  45. struct drm_crtc *crtc;
  46. const struct mtk_dmdp_aal_data *data;
  47. };
  48. static inline struct mtk_dmdp_aal *comp_to_dmdp_aal(struct mtk_ddp_comp *comp)
  49. {
  50. return container_of(comp, struct mtk_dmdp_aal, ddp_comp);
  51. }
  52. static void mtk_aal_write_mask(void __iomem *address, u32 data, u32 mask)
  53. {
  54. u32 value = data;
  55. if (mask != ~0) {
  56. value = readl(address);
  57. value &= ~mask;
  58. data &= mask;
  59. value |= data;
  60. }
  61. writel(value, address);
  62. }
  63. static void mtk_dmdp_aal_start(struct mtk_ddp_comp *comp,
  64. struct cmdq_pkt *handle)
  65. {
  66. DDPINFO("%s\n", __func__);
  67. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_EN,
  68. AAL_EN, ~0);
  69. }
  70. static void mtk_dmdp_aal_stop(struct mtk_ddp_comp *comp,
  71. struct cmdq_pkt *handle)
  72. {
  73. DDPINFO("%s\n", __func__);
  74. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_EN,
  75. 0x0, ~0);
  76. }
  77. static void mtk_dmdp_aal_bypass(struct mtk_ddp_comp *comp,
  78. struct cmdq_pkt *handle)
  79. {
  80. DDPINFO("%s\n", __func__);
  81. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_EN,
  82. AAL_EN, ~0);
  83. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_CFG,
  84. 0x400003, ~0);
  85. cmdq_pkt_write(handle, comp->cmdq_base,
  86. comp->regs_pa + DMDP_AAL_CFG_MAIN, 0, ~0);
  87. cmdq_pkt_write(handle, comp->cmdq_base,
  88. comp->regs_pa + DMDP_AAL_DRE_BILATERAL, 0, ~0);
  89. }
  90. static void mtk_dmdp_aal_config(struct mtk_ddp_comp *comp,
  91. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  92. {
  93. unsigned int val = (cfg->w << 16) | (cfg->h);
  94. DDPINFO("%s: 0x%08x\n", __func__, val);
  95. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_CFG,
  96. 0, 1);
  97. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_SIZE,
  98. val, ~0);
  99. cmdq_pkt_write(handle, comp->cmdq_base,
  100. comp->regs_pa + DMDP_AAL_OUTPUT_SIZE, val, ~0);
  101. cmdq_pkt_write(handle, comp->cmdq_base,
  102. comp->regs_pa + DMDP_AAL_DRE_BILATERAL, 0, ~0);
  103. cmdq_pkt_write(handle, comp->cmdq_base,
  104. comp->regs_pa + DMDP_AAL_DRE_BITPLUS_00, 0, ~0);
  105. cmdq_pkt_write(handle, comp->cmdq_base,
  106. comp->regs_pa + DMDP_AAL_Y2R_00, 0, ~0);
  107. cmdq_pkt_write(handle, comp->cmdq_base,
  108. comp->regs_pa + DMDP_AAL_R2Y_00, 0, ~0);
  109. }
  110. void mtk_dmdp_aal_first_cfg(struct mtk_ddp_comp *comp,
  111. struct mtk_ddp_config *cfg, struct cmdq_pkt *handle)
  112. {
  113. DDPINFO("%s\n", __func__);
  114. cmdq_pkt_write(handle, comp->cmdq_base, comp->regs_pa + DMDP_AAL_CFG,
  115. 0x00400022, ~0);
  116. mtk_dmdp_aal_config(comp, cfg, handle);
  117. mtk_dmdp_aal_start(comp, handle);
  118. }
  119. static atomic_t g_aal_initialed = ATOMIC_INIT(0);
  120. struct aal_backup { /* structure for backup AAL register value */
  121. unsigned int DRE_MAPPING;
  122. unsigned int DRE_BLOCK_INFO_00;
  123. unsigned int DRE_BLOCK_INFO_01;
  124. unsigned int DRE_BLOCK_INFO_02;
  125. unsigned int DRE_BLOCK_INFO_04;
  126. unsigned int DRE_BLOCK_INFO_05;
  127. unsigned int DRE_BLOCK_INFO_06;
  128. unsigned int DRE_BLOCK_INFO_07;
  129. unsigned int DRE_CHROMA_HIST_00;
  130. unsigned int DRE_CHROMA_HIST_01;
  131. unsigned int DRE_ALPHA_BLEND_00;
  132. unsigned int SRAM_CFG;
  133. unsigned int DUAL_PIPE_INFO_00;
  134. unsigned int DUAL_PIPE_INFO_01;
  135. unsigned int TILE_00;
  136. unsigned int TILE_01;
  137. unsigned int TILE_02;
  138. };
  139. static struct aal_backup g_aal_backup;
  140. #define DMDP_AAL_SRAM_CFG (0x0c4)
  141. #define DMDP_AAL_TILE_02 (0x0F4)
  142. #define DMDP_AAL_DRE_BLOCK_INFO_07 (0x0f8)
  143. #define DMDP_AAL_DRE_MAPPING_00 (0x3b4)
  144. #define DMDP_AAL_DRE_BLOCK_INFO_00 (0x468)
  145. #define DMDP_AAL_DRE_BLOCK_INFO_01 (0x46c)
  146. #define DMDP_AAL_DRE_BLOCK_INFO_02 (0x470)
  147. #define DMDP_AAL_DRE_BLOCK_INFO_03 (0x474)
  148. #define DMDP_AAL_DRE_BLOCK_INFO_04 (0x478)
  149. #define DMDP_AAL_DRE_CHROMA_HIST_00 (0x480)
  150. #define DMDP_AAL_DRE_CHROMA_HIST_01 (0x484)
  151. #define DMDP_AAL_DRE_ALPHA_BLEND_00 (0x488)
  152. #define DMDP_AAL_DRE_BLOCK_INFO_05 (0x4b4)
  153. #define DMDP_AAL_DRE_BLOCK_INFO_06 (0x4b8)
  154. #define DMDP_AAL_DUAL_PIPE_INFO_00 (0x4d0)
  155. #define DMDP_AAL_DUAL_PIPE_INFO_01 (0x4d4)
  156. #define DMDP_AAL_TILE_00 (0x4EC)
  157. #define DMDP_AAL_TILE_01 (0x4F0)
  158. static void ddp_aal_dre3_backup(struct mtk_ddp_comp *comp)
  159. {
  160. g_aal_backup.DRE_BLOCK_INFO_00 =
  161. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_00);
  162. g_aal_backup.DRE_BLOCK_INFO_01 =
  163. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_01);
  164. g_aal_backup.DRE_BLOCK_INFO_02 =
  165. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_02);
  166. g_aal_backup.DRE_BLOCK_INFO_04 =
  167. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_04);
  168. g_aal_backup.DRE_CHROMA_HIST_00 =
  169. readl(comp->regs + DMDP_AAL_DRE_CHROMA_HIST_00);
  170. g_aal_backup.DRE_CHROMA_HIST_01 =
  171. readl(comp->regs + DMDP_AAL_DRE_CHROMA_HIST_01);
  172. g_aal_backup.DRE_ALPHA_BLEND_00 =
  173. readl(comp->regs + DMDP_AAL_DRE_ALPHA_BLEND_00);
  174. g_aal_backup.DRE_BLOCK_INFO_05 =
  175. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_05);
  176. g_aal_backup.DRE_BLOCK_INFO_06 =
  177. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_06);
  178. g_aal_backup.DRE_BLOCK_INFO_07 =
  179. readl(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_07);
  180. g_aal_backup.SRAM_CFG =
  181. readl(comp->regs + DMDP_AAL_SRAM_CFG);
  182. g_aal_backup.DUAL_PIPE_INFO_00 =
  183. readl(comp->regs + DMDP_AAL_DUAL_PIPE_INFO_00);
  184. g_aal_backup.DUAL_PIPE_INFO_01 =
  185. readl(comp->regs + DMDP_AAL_DUAL_PIPE_INFO_01);
  186. g_aal_backup.TILE_00 =
  187. readl(comp->regs + DMDP_AAL_TILE_00);
  188. g_aal_backup.TILE_01 =
  189. readl(comp->regs + DMDP_AAL_TILE_01);
  190. g_aal_backup.TILE_02 =
  191. readl(comp->regs + DMDP_AAL_TILE_02);
  192. }
  193. static void ddp_aal_dre_backup(struct mtk_ddp_comp *comp)
  194. {
  195. g_aal_backup.DRE_MAPPING =
  196. readl(comp->regs + DMDP_AAL_DRE_MAPPING_00);
  197. }
  198. static void mtk_dmdp_aal_backup(struct mtk_ddp_comp *comp)
  199. {
  200. DDPINFO("%s\n", __func__);
  201. ddp_aal_dre_backup(comp);
  202. ddp_aal_dre3_backup(comp);
  203. atomic_set(&g_aal_initialed, 1);
  204. }
  205. static void ddp_aal_dre3_restore(struct mtk_ddp_comp *comp)
  206. {
  207. struct mtk_dmdp_aal *dmdp_aal = comp_to_dmdp_aal(comp);
  208. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_00,
  209. g_aal_backup.DRE_BLOCK_INFO_00 &
  210. (dmdp_aal->data->block_info_00_mask),
  211. dmdp_aal->data->block_info_00_mask);
  212. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_01,
  213. g_aal_backup.DRE_BLOCK_INFO_01, ~0);
  214. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_02,
  215. g_aal_backup.DRE_BLOCK_INFO_02, ~0);
  216. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_04,
  217. g_aal_backup.DRE_BLOCK_INFO_04 & (0x3FF << 13), 0x3FF << 13);
  218. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_CHROMA_HIST_00,
  219. g_aal_backup.DRE_CHROMA_HIST_00, ~0);
  220. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_CHROMA_HIST_01,
  221. g_aal_backup.DRE_CHROMA_HIST_01 & 0x1FFFFFFF, 0x1FFFFFFF);
  222. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_ALPHA_BLEND_00,
  223. g_aal_backup.DRE_ALPHA_BLEND_00, ~0);
  224. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_05,
  225. g_aal_backup.DRE_BLOCK_INFO_05, ~0);
  226. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_06,
  227. g_aal_backup.DRE_BLOCK_INFO_06, ~0);
  228. mtk_aal_write_mask(comp->regs + DMDP_AAL_DRE_BLOCK_INFO_07,
  229. g_aal_backup.DRE_BLOCK_INFO_07, ~0);
  230. mtk_aal_write_mask(comp->regs + DMDP_AAL_SRAM_CFG,
  231. g_aal_backup.SRAM_CFG, 0x1);
  232. mtk_aal_write_mask(comp->regs + DMDP_AAL_DUAL_PIPE_INFO_00,
  233. g_aal_backup.DUAL_PIPE_INFO_00, ~0);
  234. mtk_aal_write_mask(comp->regs + DMDP_AAL_DUAL_PIPE_INFO_01,
  235. g_aal_backup.DUAL_PIPE_INFO_01, ~0);
  236. mtk_aal_write_mask(comp->regs + DMDP_AAL_TILE_00,
  237. g_aal_backup.TILE_00, ~0);
  238. mtk_aal_write_mask(comp->regs + DMDP_AAL_TILE_01,
  239. g_aal_backup.TILE_01, ~0);
  240. mtk_aal_write_mask(comp->regs + DMDP_AAL_TILE_02,
  241. g_aal_backup.TILE_02, ~0);
  242. }
  243. static void ddp_aal_dre_restore(struct mtk_ddp_comp *comp)
  244. {
  245. writel(g_aal_backup.DRE_MAPPING,
  246. comp->regs + DMDP_AAL_DRE_MAPPING_00);
  247. }
  248. static void mtk_dmdp_aal_restore(struct mtk_ddp_comp *comp)
  249. {
  250. if (atomic_read(&g_aal_initialed) != 1)
  251. return;
  252. DDPINFO("%s\n", __func__);
  253. ddp_aal_dre_restore(comp);
  254. ddp_aal_dre3_restore(comp);
  255. }
  256. static void mtk_dmdp_aal_prepare(struct mtk_ddp_comp *comp)
  257. {
  258. #if defined(CONFIG_DRM_MTK_SHADOW_REGISTER_SUPPORT)
  259. struct mtk_dmdp_aal *dmdp_aal = comp_to_dmdp_aal(comp);
  260. #endif
  261. pr_notice("%s\n", __func__);
  262. mtk_ddp_comp_clk_prepare(comp);
  263. #if defined(CONFIG_DRM_MTK_SHADOW_REGISTER_SUPPORT)
  264. if (dmdp_aal->data->support_shadow) {
  265. /* Enable shadow register and read shadow register */
  266. mtk_ddp_write_mask_cpu(comp, 0x0,
  267. DMDP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  268. } else {
  269. /* Bypass shadow register and read shadow register */
  270. mtk_ddp_write_mask_cpu(comp, AAL_BYPASS_SHADOW,
  271. DMDP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  272. }
  273. #else
  274. #if defined(CONFIG_MACH_MT6873)
  275. /* Bypass shadow register and read shadow register */
  276. mtk_ddp_write_mask_cpu(comp, AAL_BYPASS_SHADOW,
  277. DMDP_AAL_SHADOW_CTRL, AAL_BYPASS_SHADOW);
  278. #endif
  279. #endif
  280. mtk_dmdp_aal_restore(comp);
  281. }
  282. static void mtk_dmdp_aal_unprepare(struct mtk_ddp_comp *comp)
  283. {
  284. pr_notice("%s\n", __func__);
  285. mtk_dmdp_aal_backup(comp);
  286. mtk_ddp_comp_clk_unprepare(comp);
  287. }
  288. static const struct mtk_ddp_comp_funcs mtk_dmdp_aal_funcs = {
  289. .config = mtk_dmdp_aal_config,
  290. .first_cfg = mtk_dmdp_aal_first_cfg,
  291. .start = mtk_dmdp_aal_start,
  292. .stop = mtk_dmdp_aal_stop,
  293. .bypass = mtk_dmdp_aal_bypass,
  294. .prepare = mtk_dmdp_aal_prepare,
  295. .unprepare = mtk_dmdp_aal_unprepare,
  296. };
  297. static int mtk_dmdp_aal_bind(struct device *dev, struct device *master,
  298. void *data)
  299. {
  300. struct mtk_dmdp_aal *priv = dev_get_drvdata(dev);
  301. struct drm_device *drm_dev = data;
  302. int ret;
  303. DDPINFO("%s\n", __func__);
  304. ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
  305. if (ret < 0) {
  306. DDPMSG("Failed to register component %s: %d\n",
  307. dev->of_node->full_name, ret);
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. static void mtk_dmdp_aal_unbind(struct device *dev, struct device *master,
  313. void *data)
  314. {
  315. struct mtk_dmdp_aal *priv = dev_get_drvdata(dev);
  316. struct drm_device *drm_dev = data;
  317. mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
  318. }
  319. static const struct component_ops mtk_dmdp_aal_component_ops = {
  320. .bind = mtk_dmdp_aal_bind, .unbind = mtk_dmdp_aal_unbind,
  321. };
  322. void mtk_dmdp_aal_dump(struct mtk_ddp_comp *comp)
  323. {
  324. void __iomem *baddr = comp->regs;
  325. DDPDUMP("== %s REGS ==\n", mtk_dump_comp_str(comp));
  326. mtk_cust_dump_reg(baddr, 0x0, 0x20, 0x30, 0x4D8);
  327. mtk_cust_dump_reg(baddr, 0x200, 0xf4, 0xf8, 0x468);
  328. mtk_cust_dump_reg(baddr, 0x46c, 0x470, 0x474, 0x478);
  329. mtk_cust_dump_reg(baddr, 0x4ec, 0x4f0, 0x528, 0x52c);
  330. }
  331. static int mtk_dmdp_aal_probe(struct platform_device *pdev)
  332. {
  333. struct device *dev = &pdev->dev;
  334. struct mtk_dmdp_aal *priv;
  335. enum mtk_ddp_comp_id comp_id;
  336. int ret;
  337. DDPMSG("%s\n", __func__);
  338. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  339. if (priv == NULL)
  340. return -ENOMEM;
  341. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DMDP_AAL);
  342. if ((int)comp_id < 0) {
  343. DDPMSG("Failed to identify by alias: %d\n", comp_id);
  344. return comp_id;
  345. }
  346. ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
  347. &mtk_dmdp_aal_funcs);
  348. if (ret != 0) {
  349. DDPMSG("Failed to initialize component: %d\n", ret);
  350. return ret;
  351. }
  352. priv->data = of_device_get_match_data(dev);
  353. platform_set_drvdata(pdev, priv);
  354. pm_runtime_enable(dev);
  355. ret = component_add(dev, &mtk_dmdp_aal_component_ops);
  356. if (ret != 0) {
  357. DDPMSG("Failed to add component: %d\n", ret);
  358. pm_runtime_disable(dev);
  359. }
  360. return ret;
  361. }
  362. static int mtk_dmdp_aal_remove(struct platform_device *pdev)
  363. {
  364. component_del(&pdev->dev, &mtk_dmdp_aal_component_ops);
  365. pm_runtime_disable(&pdev->dev);
  366. return 0;
  367. }
  368. static const struct mtk_dmdp_aal_data mt6885_dmdp_aal_driver_data = {
  369. .support_shadow = false,
  370. .block_info_00_mask = 0x3FFFFFF,
  371. };
  372. static const struct mtk_dmdp_aal_data mt6873_dmdp_aal_driver_data = {
  373. .support_shadow = false,
  374. .block_info_00_mask = 0x3FFF3FFF,
  375. };
  376. static const struct of_device_id mtk_dmdp_aal_driver_dt_match[] = {
  377. { .compatible = "mediatek,mt6885-dmdp-aal",
  378. .data = &mt6885_dmdp_aal_driver_data},
  379. { .compatible = "mediatek,mt6873-dmdp-aal",
  380. .data = &mt6873_dmdp_aal_driver_data},
  381. {},
  382. };
  383. MODULE_DEVICE_TABLE(of, mtk_dmdp_aal_driver_dt_match);
  384. struct platform_driver mtk_dmdp_aal_driver = {
  385. .probe = mtk_dmdp_aal_probe,
  386. .remove = mtk_dmdp_aal_remove,
  387. .driver = {
  388. .name = "mediatek-dmdp-aal",
  389. .owner = THIS_MODULE,
  390. .of_match_table = mtk_dmdp_aal_driver_dt_match,
  391. },
  392. };