mtk_drm_ddp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <linux/clk.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include "mtk_drm_ddp.h"
  20. #include "mtk_drm_ddp_comp.h"
  21. #define DISP_REG_CONFIG_DISP_OVL0_MOUT_EN 0x040
  22. #define DISP_REG_CONFIG_DISP_OVL1_MOUT_EN 0x044
  23. #define DISP_REG_CONFIG_DISP_OD_MOUT_EN 0x048
  24. #define DISP_REG_CONFIG_DISP_GAMMA_MOUT_EN 0x04c
  25. #define DISP_REG_CONFIG_DISP_UFOE_MOUT_EN 0x050
  26. #define DISP_REG_CONFIG_DISP_COLOR0_SEL_IN 0x084
  27. #define DISP_REG_CONFIG_DISP_COLOR1_SEL_IN 0x088
  28. #define DISP_REG_CONFIG_DPI_SEL_IN 0x0ac
  29. #define DISP_REG_CONFIG_DISP_RDMA1_MOUT_EN 0x0c8
  30. #define DISP_REG_CONFIG_MMSYS_CG_CON0 0x100
  31. #define DISP_REG_CONFIG_DISP_OVL_MOUT_EN 0x030
  32. #define DISP_REG_CONFIG_OUT_SEL 0x04c
  33. #define DISP_REG_CONFIG_DSI_SEL 0x050
  34. #define DISP_REG_MUTEX_EN(n) (0x20 + 0x20 * (n))
  35. #define DISP_REG_MUTEX(n) (0x24 + 0x20 * (n))
  36. #define DISP_REG_MUTEX_RST(n) (0x28 + 0x20 * (n))
  37. #define DISP_REG_MUTEX_MOD(n) (0x2c + 0x20 * (n))
  38. #define DISP_REG_MUTEX_SOF(n) (0x30 + 0x20 * (n))
  39. #define INT_MUTEX BIT(1)
  40. #define MT8173_MUTEX_MOD_DISP_OVL0 BIT(11)
  41. #define MT8173_MUTEX_MOD_DISP_OVL1 BIT(12)
  42. #define MT8173_MUTEX_MOD_DISP_RDMA0 BIT(13)
  43. #define MT8173_MUTEX_MOD_DISP_RDMA1 BIT(14)
  44. #define MT8173_MUTEX_MOD_DISP_RDMA2 BIT(15)
  45. #define MT8173_MUTEX_MOD_DISP_WDMA0 BIT(16)
  46. #define MT8173_MUTEX_MOD_DISP_WDMA1 BIT(17)
  47. #define MT8173_MUTEX_MOD_DISP_COLOR0 BIT(18)
  48. #define MT8173_MUTEX_MOD_DISP_COLOR1 BIT(19)
  49. #define MT8173_MUTEX_MOD_DISP_AAL BIT(20)
  50. #define MT8173_MUTEX_MOD_DISP_GAMMA BIT(21)
  51. #define MT8173_MUTEX_MOD_DISP_UFOE BIT(22)
  52. #define MT8173_MUTEX_MOD_DISP_PWM0 BIT(23)
  53. #define MT8173_MUTEX_MOD_DISP_PWM1 BIT(24)
  54. #define MT8173_MUTEX_MOD_DISP_OD BIT(25)
  55. #define MT2701_MUTEX_MOD_DISP_OVL BIT(3)
  56. #define MT2701_MUTEX_MOD_DISP_WDMA BIT(6)
  57. #define MT2701_MUTEX_MOD_DISP_COLOR BIT(7)
  58. #define MT2701_MUTEX_MOD_DISP_BLS BIT(9)
  59. #define MT2701_MUTEX_MOD_DISP_RDMA0 BIT(10)
  60. #define MT2701_MUTEX_MOD_DISP_RDMA1 BIT(12)
  61. #define MUTEX_SOF_SINGLE_MODE 0
  62. #define MUTEX_SOF_DSI0 1
  63. #define MUTEX_SOF_DSI1 2
  64. #define MUTEX_SOF_DPI0 3
  65. #define OVL0_MOUT_EN_COLOR0 0x1
  66. #define OD_MOUT_EN_RDMA0 0x1
  67. #define UFOE_MOUT_EN_DSI0 0x1
  68. #define COLOR0_SEL_IN_OVL0 0x1
  69. #define OVL1_MOUT_EN_COLOR1 0x1
  70. #define GAMMA_MOUT_EN_RDMA1 0x1
  71. #define RDMA1_MOUT_DPI0 0x2
  72. #define DPI0_SEL_IN_RDMA1 0x1
  73. #define COLOR1_SEL_IN_OVL1 0x1
  74. #define OVL_MOUT_EN_RDMA 0x1
  75. #define BLS_TO_DSI_RDMA1_TO_DPI1 0x8
  76. #define DSI_SEL_IN_BLS 0x0
  77. struct mtk_disp_mutex {
  78. int id;
  79. bool claimed;
  80. };
  81. struct mtk_ddp {
  82. struct device *dev;
  83. struct clk *clk;
  84. void __iomem *regs;
  85. struct mtk_disp_mutex mutex[10];
  86. const unsigned int *mutex_mod;
  87. };
  88. static const unsigned int mt2701_mutex_mod[DDP_COMPONENT_ID_MAX] = {
  89. [DDP_COMPONENT_BLS] = MT2701_MUTEX_MOD_DISP_BLS,
  90. [DDP_COMPONENT_COLOR0] = MT2701_MUTEX_MOD_DISP_COLOR,
  91. [DDP_COMPONENT_OVL0] = MT2701_MUTEX_MOD_DISP_OVL,
  92. [DDP_COMPONENT_RDMA0] = MT2701_MUTEX_MOD_DISP_RDMA0,
  93. [DDP_COMPONENT_RDMA1] = MT2701_MUTEX_MOD_DISP_RDMA1,
  94. [DDP_COMPONENT_WDMA0] = MT2701_MUTEX_MOD_DISP_WDMA,
  95. };
  96. static const unsigned int mt8173_mutex_mod[DDP_COMPONENT_ID_MAX] = {
  97. [DDP_COMPONENT_AAL] = MT8173_MUTEX_MOD_DISP_AAL,
  98. [DDP_COMPONENT_COLOR0] = MT8173_MUTEX_MOD_DISP_COLOR0,
  99. [DDP_COMPONENT_COLOR1] = MT8173_MUTEX_MOD_DISP_COLOR1,
  100. [DDP_COMPONENT_GAMMA] = MT8173_MUTEX_MOD_DISP_GAMMA,
  101. [DDP_COMPONENT_OD] = MT8173_MUTEX_MOD_DISP_OD,
  102. [DDP_COMPONENT_OVL0] = MT8173_MUTEX_MOD_DISP_OVL0,
  103. [DDP_COMPONENT_OVL1] = MT8173_MUTEX_MOD_DISP_OVL1,
  104. [DDP_COMPONENT_PWM0] = MT8173_MUTEX_MOD_DISP_PWM0,
  105. [DDP_COMPONENT_PWM1] = MT8173_MUTEX_MOD_DISP_PWM1,
  106. [DDP_COMPONENT_RDMA0] = MT8173_MUTEX_MOD_DISP_RDMA0,
  107. [DDP_COMPONENT_RDMA1] = MT8173_MUTEX_MOD_DISP_RDMA1,
  108. [DDP_COMPONENT_RDMA2] = MT8173_MUTEX_MOD_DISP_RDMA2,
  109. [DDP_COMPONENT_UFOE] = MT8173_MUTEX_MOD_DISP_UFOE,
  110. [DDP_COMPONENT_WDMA0] = MT8173_MUTEX_MOD_DISP_WDMA0,
  111. [DDP_COMPONENT_WDMA1] = MT8173_MUTEX_MOD_DISP_WDMA1,
  112. };
  113. static unsigned int mtk_ddp_mout_en(enum mtk_ddp_comp_id cur,
  114. enum mtk_ddp_comp_id next,
  115. unsigned int *addr)
  116. {
  117. unsigned int value;
  118. if (cur == DDP_COMPONENT_OVL0 && next == DDP_COMPONENT_COLOR0) {
  119. *addr = DISP_REG_CONFIG_DISP_OVL0_MOUT_EN;
  120. value = OVL0_MOUT_EN_COLOR0;
  121. } else if (cur == DDP_COMPONENT_OVL0 && next == DDP_COMPONENT_RDMA0) {
  122. *addr = DISP_REG_CONFIG_DISP_OVL_MOUT_EN;
  123. value = OVL_MOUT_EN_RDMA;
  124. } else if (cur == DDP_COMPONENT_OD && next == DDP_COMPONENT_RDMA0) {
  125. *addr = DISP_REG_CONFIG_DISP_OD_MOUT_EN;
  126. value = OD_MOUT_EN_RDMA0;
  127. } else if (cur == DDP_COMPONENT_UFOE && next == DDP_COMPONENT_DSI0) {
  128. *addr = DISP_REG_CONFIG_DISP_UFOE_MOUT_EN;
  129. value = UFOE_MOUT_EN_DSI0;
  130. } else if (cur == DDP_COMPONENT_OVL1 && next == DDP_COMPONENT_COLOR1) {
  131. *addr = DISP_REG_CONFIG_DISP_OVL1_MOUT_EN;
  132. value = OVL1_MOUT_EN_COLOR1;
  133. } else if (cur == DDP_COMPONENT_GAMMA && next == DDP_COMPONENT_RDMA1) {
  134. *addr = DISP_REG_CONFIG_DISP_GAMMA_MOUT_EN;
  135. value = GAMMA_MOUT_EN_RDMA1;
  136. } else if (cur == DDP_COMPONENT_RDMA1 && next == DDP_COMPONENT_DPI0) {
  137. *addr = DISP_REG_CONFIG_DISP_RDMA1_MOUT_EN;
  138. value = RDMA1_MOUT_DPI0;
  139. } else {
  140. value = 0;
  141. }
  142. return value;
  143. }
  144. static unsigned int mtk_ddp_sel_in(enum mtk_ddp_comp_id cur,
  145. enum mtk_ddp_comp_id next,
  146. unsigned int *addr)
  147. {
  148. unsigned int value;
  149. if (cur == DDP_COMPONENT_OVL0 && next == DDP_COMPONENT_COLOR0) {
  150. *addr = DISP_REG_CONFIG_DISP_COLOR0_SEL_IN;
  151. value = COLOR0_SEL_IN_OVL0;
  152. } else if (cur == DDP_COMPONENT_RDMA1 && next == DDP_COMPONENT_DPI0) {
  153. *addr = DISP_REG_CONFIG_DPI_SEL_IN;
  154. value = DPI0_SEL_IN_RDMA1;
  155. } else if (cur == DDP_COMPONENT_OVL1 && next == DDP_COMPONENT_COLOR1) {
  156. *addr = DISP_REG_CONFIG_DISP_COLOR1_SEL_IN;
  157. value = COLOR1_SEL_IN_OVL1;
  158. } else if (cur == DDP_COMPONENT_BLS && next == DDP_COMPONENT_DSI0) {
  159. *addr = DISP_REG_CONFIG_DSI_SEL;
  160. value = DSI_SEL_IN_BLS;
  161. } else {
  162. value = 0;
  163. }
  164. return value;
  165. }
  166. static void mtk_ddp_sout_sel(void __iomem *config_regs,
  167. enum mtk_ddp_comp_id cur,
  168. enum mtk_ddp_comp_id next)
  169. {
  170. if (cur == DDP_COMPONENT_BLS && next == DDP_COMPONENT_DSI0)
  171. writel_relaxed(BLS_TO_DSI_RDMA1_TO_DPI1,
  172. config_regs + DISP_REG_CONFIG_OUT_SEL);
  173. }
  174. void mtk_ddp_add_comp_to_path(void __iomem *config_regs,
  175. enum mtk_ddp_comp_id cur,
  176. enum mtk_ddp_comp_id next)
  177. {
  178. unsigned int addr, value, reg;
  179. value = mtk_ddp_mout_en(cur, next, &addr);
  180. if (value) {
  181. reg = readl_relaxed(config_regs + addr) | value;
  182. writel_relaxed(reg, config_regs + addr);
  183. }
  184. mtk_ddp_sout_sel(config_regs, cur, next);
  185. value = mtk_ddp_sel_in(cur, next, &addr);
  186. if (value) {
  187. reg = readl_relaxed(config_regs + addr) | value;
  188. writel_relaxed(reg, config_regs + addr);
  189. }
  190. }
  191. void mtk_ddp_remove_comp_from_path(void __iomem *config_regs,
  192. enum mtk_ddp_comp_id cur,
  193. enum mtk_ddp_comp_id next)
  194. {
  195. unsigned int addr, value, reg;
  196. value = mtk_ddp_mout_en(cur, next, &addr);
  197. if (value) {
  198. reg = readl_relaxed(config_regs + addr) & ~value;
  199. writel_relaxed(reg, config_regs + addr);
  200. }
  201. value = mtk_ddp_sel_in(cur, next, &addr);
  202. if (value) {
  203. reg = readl_relaxed(config_regs + addr) & ~value;
  204. writel_relaxed(reg, config_regs + addr);
  205. }
  206. }
  207. struct mtk_disp_mutex *mtk_disp_mutex_get(struct device *dev, unsigned int id)
  208. {
  209. struct mtk_ddp *ddp = dev_get_drvdata(dev);
  210. if (id >= 10)
  211. return ERR_PTR(-EINVAL);
  212. if (ddp->mutex[id].claimed)
  213. return ERR_PTR(-EBUSY);
  214. ddp->mutex[id].claimed = true;
  215. return &ddp->mutex[id];
  216. }
  217. void mtk_disp_mutex_put(struct mtk_disp_mutex *mutex)
  218. {
  219. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  220. mutex[mutex->id]);
  221. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  222. mutex->claimed = false;
  223. }
  224. int mtk_disp_mutex_prepare(struct mtk_disp_mutex *mutex)
  225. {
  226. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  227. mutex[mutex->id]);
  228. return clk_prepare_enable(ddp->clk);
  229. }
  230. void mtk_disp_mutex_unprepare(struct mtk_disp_mutex *mutex)
  231. {
  232. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  233. mutex[mutex->id]);
  234. clk_disable_unprepare(ddp->clk);
  235. }
  236. void mtk_disp_mutex_add_comp(struct mtk_disp_mutex *mutex,
  237. enum mtk_ddp_comp_id id)
  238. {
  239. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  240. mutex[mutex->id]);
  241. unsigned int reg;
  242. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  243. switch (id) {
  244. case DDP_COMPONENT_DSI0:
  245. reg = MUTEX_SOF_DSI0;
  246. break;
  247. case DDP_COMPONENT_DSI1:
  248. reg = MUTEX_SOF_DSI0;
  249. break;
  250. case DDP_COMPONENT_DPI0:
  251. reg = MUTEX_SOF_DPI0;
  252. break;
  253. default:
  254. reg = readl_relaxed(ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  255. reg |= ddp->mutex_mod[id];
  256. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  257. return;
  258. }
  259. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_SOF(mutex->id));
  260. }
  261. void mtk_disp_mutex_remove_comp(struct mtk_disp_mutex *mutex,
  262. enum mtk_ddp_comp_id id)
  263. {
  264. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  265. mutex[mutex->id]);
  266. unsigned int reg;
  267. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  268. switch (id) {
  269. case DDP_COMPONENT_DSI0:
  270. case DDP_COMPONENT_DSI1:
  271. case DDP_COMPONENT_DPI0:
  272. writel_relaxed(MUTEX_SOF_SINGLE_MODE,
  273. ddp->regs + DISP_REG_MUTEX_SOF(mutex->id));
  274. break;
  275. default:
  276. reg = readl_relaxed(ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  277. reg &= ~(ddp->mutex_mod[id]);
  278. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  279. break;
  280. }
  281. }
  282. void mtk_disp_mutex_enable(struct mtk_disp_mutex *mutex)
  283. {
  284. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  285. mutex[mutex->id]);
  286. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  287. writel(1, ddp->regs + DISP_REG_MUTEX_EN(mutex->id));
  288. }
  289. void mtk_disp_mutex_disable(struct mtk_disp_mutex *mutex)
  290. {
  291. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  292. mutex[mutex->id]);
  293. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  294. writel(0, ddp->regs + DISP_REG_MUTEX_EN(mutex->id));
  295. }
  296. void mtk_disp_mutex_acquire(struct mtk_disp_mutex *mutex)
  297. {
  298. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  299. mutex[mutex->id]);
  300. u32 tmp;
  301. writel(1, ddp->regs + DISP_REG_MUTEX_EN(mutex->id));
  302. writel(1, ddp->regs + DISP_REG_MUTEX(mutex->id));
  303. if (readl_poll_timeout_atomic(ddp->regs + DISP_REG_MUTEX(mutex->id),
  304. tmp, tmp & INT_MUTEX, 1, 10000))
  305. pr_err("could not acquire mutex %d\n", mutex->id);
  306. }
  307. void mtk_disp_mutex_release(struct mtk_disp_mutex *mutex)
  308. {
  309. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  310. mutex[mutex->id]);
  311. writel(0, ddp->regs + DISP_REG_MUTEX(mutex->id));
  312. }
  313. static int mtk_ddp_probe(struct platform_device *pdev)
  314. {
  315. struct device *dev = &pdev->dev;
  316. struct mtk_ddp *ddp;
  317. struct resource *regs;
  318. int i;
  319. ddp = devm_kzalloc(dev, sizeof(*ddp), GFP_KERNEL);
  320. if (!ddp)
  321. return -ENOMEM;
  322. for (i = 0; i < 10; i++)
  323. ddp->mutex[i].id = i;
  324. ddp->clk = devm_clk_get(dev, NULL);
  325. if (IS_ERR(ddp->clk)) {
  326. dev_err(dev, "Failed to get clock\n");
  327. return PTR_ERR(ddp->clk);
  328. }
  329. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  330. ddp->regs = devm_ioremap_resource(dev, regs);
  331. if (IS_ERR(ddp->regs)) {
  332. dev_err(dev, "Failed to map mutex registers\n");
  333. return PTR_ERR(ddp->regs);
  334. }
  335. ddp->mutex_mod = of_device_get_match_data(dev);
  336. platform_set_drvdata(pdev, ddp);
  337. return 0;
  338. }
  339. static int mtk_ddp_remove(struct platform_device *pdev)
  340. {
  341. return 0;
  342. }
  343. static const struct of_device_id ddp_driver_dt_match[] = {
  344. { .compatible = "mediatek,mt2701-disp-mutex", .data = mt2701_mutex_mod},
  345. { .compatible = "mediatek,mt8173-disp-mutex", .data = mt8173_mutex_mod},
  346. {},
  347. };
  348. MODULE_DEVICE_TABLE(of, ddp_driver_dt_match);
  349. struct platform_driver mtk_ddp_driver = {
  350. .probe = mtk_ddp_probe,
  351. .remove = mtk_ddp_remove,
  352. .driver = {
  353. .name = "mediatek-ddp",
  354. .owner = THIS_MODULE,
  355. .of_match_table = ddp_driver_dt_match,
  356. },
  357. };