mtk_drm_ddp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include "mtk_drm_ddp.h"
  19. #include "mtk_drm_ddp_comp.h"
  20. #define DISP_REG_CONFIG_DISP_OVL0_MOUT_EN 0x040
  21. #define DISP_REG_CONFIG_DISP_OVL1_MOUT_EN 0x044
  22. #define DISP_REG_CONFIG_DISP_OD_MOUT_EN 0x048
  23. #define DISP_REG_CONFIG_DISP_GAMMA_MOUT_EN 0x04c
  24. #define DISP_REG_CONFIG_DISP_UFOE_MOUT_EN 0x050
  25. #define DISP_REG_CONFIG_DISP_COLOR0_SEL_IN 0x084
  26. #define DISP_REG_CONFIG_DISP_COLOR1_SEL_IN 0x088
  27. #define DISP_REG_CONFIG_DPI_SEL_IN 0x0ac
  28. #define DISP_REG_CONFIG_DISP_RDMA1_MOUT_EN 0x0c8
  29. #define DISP_REG_CONFIG_MMSYS_CG_CON0 0x100
  30. #define DISP_REG_MUTEX_EN(n) (0x20 + 0x20 * (n))
  31. #define DISP_REG_MUTEX_RST(n) (0x28 + 0x20 * (n))
  32. #define DISP_REG_MUTEX_MOD(n) (0x2c + 0x20 * (n))
  33. #define DISP_REG_MUTEX_SOF(n) (0x30 + 0x20 * (n))
  34. #define MUTEX_MOD_DISP_OVL0 BIT(11)
  35. #define MUTEX_MOD_DISP_OVL1 BIT(12)
  36. #define MUTEX_MOD_DISP_RDMA0 BIT(13)
  37. #define MUTEX_MOD_DISP_RDMA1 BIT(14)
  38. #define MUTEX_MOD_DISP_RDMA2 BIT(15)
  39. #define MUTEX_MOD_DISP_WDMA0 BIT(16)
  40. #define MUTEX_MOD_DISP_WDMA1 BIT(17)
  41. #define MUTEX_MOD_DISP_COLOR0 BIT(18)
  42. #define MUTEX_MOD_DISP_COLOR1 BIT(19)
  43. #define MUTEX_MOD_DISP_AAL BIT(20)
  44. #define MUTEX_MOD_DISP_GAMMA BIT(21)
  45. #define MUTEX_MOD_DISP_UFOE BIT(22)
  46. #define MUTEX_MOD_DISP_PWM0 BIT(23)
  47. #define MUTEX_MOD_DISP_PWM1 BIT(24)
  48. #define MUTEX_MOD_DISP_OD BIT(25)
  49. #define MUTEX_SOF_SINGLE_MODE 0
  50. #define MUTEX_SOF_DSI0 1
  51. #define MUTEX_SOF_DSI1 2
  52. #define MUTEX_SOF_DPI0 3
  53. #define OVL0_MOUT_EN_COLOR0 0x1
  54. #define OD_MOUT_EN_RDMA0 0x1
  55. #define UFOE_MOUT_EN_DSI0 0x1
  56. #define COLOR0_SEL_IN_OVL0 0x1
  57. #define OVL1_MOUT_EN_COLOR1 0x1
  58. #define GAMMA_MOUT_EN_RDMA1 0x1
  59. #define RDMA1_MOUT_DPI0 0x2
  60. #define DPI0_SEL_IN_RDMA1 0x1
  61. #define COLOR1_SEL_IN_OVL1 0x1
  62. struct mtk_disp_mutex {
  63. int id;
  64. bool claimed;
  65. };
  66. struct mtk_ddp {
  67. struct device *dev;
  68. struct clk *clk;
  69. void __iomem *regs;
  70. struct mtk_disp_mutex mutex[10];
  71. };
  72. static const unsigned int mutex_mod[DDP_COMPONENT_ID_MAX] = {
  73. [DDP_COMPONENT_AAL] = MUTEX_MOD_DISP_AAL,
  74. [DDP_COMPONENT_COLOR0] = MUTEX_MOD_DISP_COLOR0,
  75. [DDP_COMPONENT_COLOR1] = MUTEX_MOD_DISP_COLOR1,
  76. [DDP_COMPONENT_GAMMA] = MUTEX_MOD_DISP_GAMMA,
  77. [DDP_COMPONENT_OD] = MUTEX_MOD_DISP_OD,
  78. [DDP_COMPONENT_OVL0] = MUTEX_MOD_DISP_OVL0,
  79. [DDP_COMPONENT_OVL1] = MUTEX_MOD_DISP_OVL1,
  80. [DDP_COMPONENT_PWM0] = MUTEX_MOD_DISP_PWM0,
  81. [DDP_COMPONENT_PWM1] = MUTEX_MOD_DISP_PWM1,
  82. [DDP_COMPONENT_RDMA0] = MUTEX_MOD_DISP_RDMA0,
  83. [DDP_COMPONENT_RDMA1] = MUTEX_MOD_DISP_RDMA1,
  84. [DDP_COMPONENT_RDMA2] = MUTEX_MOD_DISP_RDMA2,
  85. [DDP_COMPONENT_UFOE] = MUTEX_MOD_DISP_UFOE,
  86. [DDP_COMPONENT_WDMA0] = MUTEX_MOD_DISP_WDMA0,
  87. [DDP_COMPONENT_WDMA1] = MUTEX_MOD_DISP_WDMA1,
  88. };
  89. static unsigned int mtk_ddp_mout_en(enum mtk_ddp_comp_id cur,
  90. enum mtk_ddp_comp_id next,
  91. unsigned int *addr)
  92. {
  93. unsigned int value;
  94. if (cur == DDP_COMPONENT_OVL0 && next == DDP_COMPONENT_COLOR0) {
  95. *addr = DISP_REG_CONFIG_DISP_OVL0_MOUT_EN;
  96. value = OVL0_MOUT_EN_COLOR0;
  97. } else if (cur == DDP_COMPONENT_OD && next == DDP_COMPONENT_RDMA0) {
  98. *addr = DISP_REG_CONFIG_DISP_OD_MOUT_EN;
  99. value = OD_MOUT_EN_RDMA0;
  100. } else if (cur == DDP_COMPONENT_UFOE && next == DDP_COMPONENT_DSI0) {
  101. *addr = DISP_REG_CONFIG_DISP_UFOE_MOUT_EN;
  102. value = UFOE_MOUT_EN_DSI0;
  103. } else if (cur == DDP_COMPONENT_OVL1 && next == DDP_COMPONENT_COLOR1) {
  104. *addr = DISP_REG_CONFIG_DISP_OVL1_MOUT_EN;
  105. value = OVL1_MOUT_EN_COLOR1;
  106. } else if (cur == DDP_COMPONENT_GAMMA && next == DDP_COMPONENT_RDMA1) {
  107. *addr = DISP_REG_CONFIG_DISP_GAMMA_MOUT_EN;
  108. value = GAMMA_MOUT_EN_RDMA1;
  109. } else if (cur == DDP_COMPONENT_RDMA1 && next == DDP_COMPONENT_DPI0) {
  110. *addr = DISP_REG_CONFIG_DISP_RDMA1_MOUT_EN;
  111. value = RDMA1_MOUT_DPI0;
  112. } else {
  113. value = 0;
  114. }
  115. return value;
  116. }
  117. static unsigned int mtk_ddp_sel_in(enum mtk_ddp_comp_id cur,
  118. enum mtk_ddp_comp_id next,
  119. unsigned int *addr)
  120. {
  121. unsigned int value;
  122. if (cur == DDP_COMPONENT_OVL0 && next == DDP_COMPONENT_COLOR0) {
  123. *addr = DISP_REG_CONFIG_DISP_COLOR0_SEL_IN;
  124. value = COLOR0_SEL_IN_OVL0;
  125. } else if (cur == DDP_COMPONENT_RDMA1 && next == DDP_COMPONENT_DPI0) {
  126. *addr = DISP_REG_CONFIG_DPI_SEL_IN;
  127. value = DPI0_SEL_IN_RDMA1;
  128. } else if (cur == DDP_COMPONENT_OVL1 && next == DDP_COMPONENT_COLOR1) {
  129. *addr = DISP_REG_CONFIG_DISP_COLOR1_SEL_IN;
  130. value = COLOR1_SEL_IN_OVL1;
  131. } else {
  132. value = 0;
  133. }
  134. return value;
  135. }
  136. void mtk_ddp_add_comp_to_path(void __iomem *config_regs,
  137. enum mtk_ddp_comp_id cur,
  138. enum mtk_ddp_comp_id next)
  139. {
  140. unsigned int addr, value, reg;
  141. value = mtk_ddp_mout_en(cur, next, &addr);
  142. if (value) {
  143. reg = readl_relaxed(config_regs + addr) | value;
  144. writel_relaxed(reg, config_regs + addr);
  145. }
  146. value = mtk_ddp_sel_in(cur, next, &addr);
  147. if (value) {
  148. reg = readl_relaxed(config_regs + addr) | value;
  149. writel_relaxed(reg, config_regs + addr);
  150. }
  151. }
  152. void mtk_ddp_remove_comp_from_path(void __iomem *config_regs,
  153. enum mtk_ddp_comp_id cur,
  154. enum mtk_ddp_comp_id next)
  155. {
  156. unsigned int addr, value, reg;
  157. value = mtk_ddp_mout_en(cur, next, &addr);
  158. if (value) {
  159. reg = readl_relaxed(config_regs + addr) & ~value;
  160. writel_relaxed(reg, config_regs + addr);
  161. }
  162. value = mtk_ddp_sel_in(cur, next, &addr);
  163. if (value) {
  164. reg = readl_relaxed(config_regs + addr) & ~value;
  165. writel_relaxed(reg, config_regs + addr);
  166. }
  167. }
  168. struct mtk_disp_mutex *mtk_disp_mutex_get(struct device *dev, unsigned int id)
  169. {
  170. struct mtk_ddp *ddp = dev_get_drvdata(dev);
  171. if (id >= 10)
  172. return ERR_PTR(-EINVAL);
  173. if (ddp->mutex[id].claimed)
  174. return ERR_PTR(-EBUSY);
  175. ddp->mutex[id].claimed = true;
  176. return &ddp->mutex[id];
  177. }
  178. void mtk_disp_mutex_put(struct mtk_disp_mutex *mutex)
  179. {
  180. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  181. mutex[mutex->id]);
  182. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  183. mutex->claimed = false;
  184. }
  185. int mtk_disp_mutex_prepare(struct mtk_disp_mutex *mutex)
  186. {
  187. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  188. mutex[mutex->id]);
  189. return clk_prepare_enable(ddp->clk);
  190. }
  191. void mtk_disp_mutex_unprepare(struct mtk_disp_mutex *mutex)
  192. {
  193. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  194. mutex[mutex->id]);
  195. clk_disable_unprepare(ddp->clk);
  196. }
  197. void mtk_disp_mutex_add_comp(struct mtk_disp_mutex *mutex,
  198. enum mtk_ddp_comp_id id)
  199. {
  200. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  201. mutex[mutex->id]);
  202. unsigned int reg;
  203. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  204. switch (id) {
  205. case DDP_COMPONENT_DSI0:
  206. reg = MUTEX_SOF_DSI0;
  207. break;
  208. case DDP_COMPONENT_DSI1:
  209. reg = MUTEX_SOF_DSI0;
  210. break;
  211. case DDP_COMPONENT_DPI0:
  212. reg = MUTEX_SOF_DPI0;
  213. break;
  214. default:
  215. reg = readl_relaxed(ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  216. reg |= mutex_mod[id];
  217. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  218. return;
  219. }
  220. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_SOF(mutex->id));
  221. }
  222. void mtk_disp_mutex_remove_comp(struct mtk_disp_mutex *mutex,
  223. enum mtk_ddp_comp_id id)
  224. {
  225. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  226. mutex[mutex->id]);
  227. unsigned int reg;
  228. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  229. switch (id) {
  230. case DDP_COMPONENT_DSI0:
  231. case DDP_COMPONENT_DSI1:
  232. case DDP_COMPONENT_DPI0:
  233. writel_relaxed(MUTEX_SOF_SINGLE_MODE,
  234. ddp->regs + DISP_REG_MUTEX_SOF(mutex->id));
  235. break;
  236. default:
  237. reg = readl_relaxed(ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  238. reg &= ~mutex_mod[id];
  239. writel_relaxed(reg, ddp->regs + DISP_REG_MUTEX_MOD(mutex->id));
  240. break;
  241. }
  242. }
  243. void mtk_disp_mutex_enable(struct mtk_disp_mutex *mutex)
  244. {
  245. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  246. mutex[mutex->id]);
  247. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  248. writel(1, ddp->regs + DISP_REG_MUTEX_EN(mutex->id));
  249. }
  250. void mtk_disp_mutex_disable(struct mtk_disp_mutex *mutex)
  251. {
  252. struct mtk_ddp *ddp = container_of(mutex, struct mtk_ddp,
  253. mutex[mutex->id]);
  254. WARN_ON(&ddp->mutex[mutex->id] != mutex);
  255. writel(0, ddp->regs + DISP_REG_MUTEX_EN(mutex->id));
  256. }
  257. static int mtk_ddp_probe(struct platform_device *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct mtk_ddp *ddp;
  261. struct resource *regs;
  262. int i;
  263. ddp = devm_kzalloc(dev, sizeof(*ddp), GFP_KERNEL);
  264. if (!ddp)
  265. return -ENOMEM;
  266. for (i = 0; i < 10; i++)
  267. ddp->mutex[i].id = i;
  268. ddp->clk = devm_clk_get(dev, NULL);
  269. if (IS_ERR(ddp->clk)) {
  270. dev_err(dev, "Failed to get clock\n");
  271. return PTR_ERR(ddp->clk);
  272. }
  273. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  274. ddp->regs = devm_ioremap_resource(dev, regs);
  275. if (IS_ERR(ddp->regs)) {
  276. dev_err(dev, "Failed to map mutex registers\n");
  277. return PTR_ERR(ddp->regs);
  278. }
  279. platform_set_drvdata(pdev, ddp);
  280. return 0;
  281. }
  282. static int mtk_ddp_remove(struct platform_device *pdev)
  283. {
  284. return 0;
  285. }
  286. static const struct of_device_id ddp_driver_dt_match[] = {
  287. { .compatible = "mediatek,mt8173-disp-mutex" },
  288. {},
  289. };
  290. MODULE_DEVICE_TABLE(of, ddp_driver_dt_match);
  291. struct platform_driver mtk_ddp_driver = {
  292. .probe = mtk_ddp_probe,
  293. .remove = mtk_ddp_remove,
  294. .driver = {
  295. .name = "mediatek-ddp",
  296. .owner = THIS_MODULE,
  297. .of_match_table = ddp_driver_dt_match,
  298. },
  299. };