mdp5_kms.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_mmu.h"
  19. #include "mdp5_kms.h"
  20. static const char *iommu_ports[] = {
  21. "mdp_0",
  22. };
  23. static struct mdp5_platform_config *mdp5_get_config(struct platform_device *dev);
  24. static int mdp5_hw_init(struct msm_kms *kms)
  25. {
  26. struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
  27. struct drm_device *dev = mdp5_kms->dev;
  28. uint32_t version, major, minor;
  29. int ret = 0;
  30. pm_runtime_get_sync(dev->dev);
  31. mdp5_enable(mdp5_kms);
  32. version = mdp5_read(mdp5_kms, REG_MDP5_MDP_VERSION);
  33. mdp5_disable(mdp5_kms);
  34. major = FIELD(version, MDP5_MDP_VERSION_MAJOR);
  35. minor = FIELD(version, MDP5_MDP_VERSION_MINOR);
  36. DBG("found MDP5 version v%d.%d", major, minor);
  37. if ((major != 1) || ((minor != 0) && (minor != 2))) {
  38. dev_err(dev->dev, "unexpected MDP version: v%d.%d\n",
  39. major, minor);
  40. ret = -ENXIO;
  41. goto out;
  42. }
  43. mdp5_kms->rev = minor;
  44. /* Magic unknown register writes:
  45. *
  46. * W VBIF:0x004 00000001 (mdss_mdp.c:839)
  47. * W MDP5:0x2e0 0xe9 (mdss_mdp.c:839)
  48. * W MDP5:0x2e4 0x55 (mdss_mdp.c:839)
  49. * W MDP5:0x3ac 0xc0000ccc (mdss_mdp.c:839)
  50. * W MDP5:0x3b4 0xc0000ccc (mdss_mdp.c:839)
  51. * W MDP5:0x3bc 0xcccccc (mdss_mdp.c:839)
  52. * W MDP5:0x4a8 0xcccc0c0 (mdss_mdp.c:839)
  53. * W MDP5:0x4b0 0xccccc0c0 (mdss_mdp.c:839)
  54. * W MDP5:0x4b8 0xccccc000 (mdss_mdp.c:839)
  55. *
  56. * Downstream fbdev driver gets these register offsets/values
  57. * from DT.. not really sure what these registers are or if
  58. * different values for different boards/SoC's, etc. I guess
  59. * they are the golden registers.
  60. *
  61. * Not setting these does not seem to cause any problem. But
  62. * we may be getting lucky with the bootloader initializing
  63. * them for us. OTOH, if we can always count on the bootloader
  64. * setting the golden registers, then perhaps we don't need to
  65. * care.
  66. */
  67. mdp5_write(mdp5_kms, REG_MDP5_DISP_INTF_SEL, 0);
  68. mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(0), 0);
  69. mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(1), 0);
  70. mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(2), 0);
  71. mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(3), 0);
  72. out:
  73. pm_runtime_put_sync(dev->dev);
  74. return ret;
  75. }
  76. static long mdp5_round_pixclk(struct msm_kms *kms, unsigned long rate,
  77. struct drm_encoder *encoder)
  78. {
  79. return rate;
  80. }
  81. static void mdp5_preclose(struct msm_kms *kms, struct drm_file *file)
  82. {
  83. struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
  84. struct msm_drm_private *priv = mdp5_kms->dev->dev_private;
  85. unsigned i;
  86. for (i = 0; i < priv->num_crtcs; i++)
  87. mdp5_crtc_cancel_pending_flip(priv->crtcs[i], file);
  88. }
  89. static void mdp5_destroy(struct msm_kms *kms)
  90. {
  91. struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
  92. struct msm_mmu *mmu = mdp5_kms->mmu;
  93. if (mmu) {
  94. mmu->funcs->detach(mmu, iommu_ports, ARRAY_SIZE(iommu_ports));
  95. mmu->funcs->destroy(mmu);
  96. }
  97. kfree(mdp5_kms);
  98. }
  99. static const struct mdp_kms_funcs kms_funcs = {
  100. .base = {
  101. .hw_init = mdp5_hw_init,
  102. .irq_preinstall = mdp5_irq_preinstall,
  103. .irq_postinstall = mdp5_irq_postinstall,
  104. .irq_uninstall = mdp5_irq_uninstall,
  105. .irq = mdp5_irq,
  106. .enable_vblank = mdp5_enable_vblank,
  107. .disable_vblank = mdp5_disable_vblank,
  108. .get_format = mdp_get_format,
  109. .round_pixclk = mdp5_round_pixclk,
  110. .preclose = mdp5_preclose,
  111. .destroy = mdp5_destroy,
  112. },
  113. .set_irqmask = mdp5_set_irqmask,
  114. };
  115. int mdp5_disable(struct mdp5_kms *mdp5_kms)
  116. {
  117. DBG("");
  118. clk_disable_unprepare(mdp5_kms->ahb_clk);
  119. clk_disable_unprepare(mdp5_kms->axi_clk);
  120. clk_disable_unprepare(mdp5_kms->core_clk);
  121. clk_disable_unprepare(mdp5_kms->lut_clk);
  122. return 0;
  123. }
  124. int mdp5_enable(struct mdp5_kms *mdp5_kms)
  125. {
  126. DBG("");
  127. clk_prepare_enable(mdp5_kms->ahb_clk);
  128. clk_prepare_enable(mdp5_kms->axi_clk);
  129. clk_prepare_enable(mdp5_kms->core_clk);
  130. clk_prepare_enable(mdp5_kms->lut_clk);
  131. return 0;
  132. }
  133. static int modeset_init(struct mdp5_kms *mdp5_kms)
  134. {
  135. static const enum mdp5_pipe crtcs[] = {
  136. SSPP_RGB0, SSPP_RGB1, SSPP_RGB2,
  137. };
  138. struct drm_device *dev = mdp5_kms->dev;
  139. struct msm_drm_private *priv = dev->dev_private;
  140. struct drm_encoder *encoder;
  141. int i, ret;
  142. /* construct CRTCs: */
  143. for (i = 0; i < ARRAY_SIZE(crtcs); i++) {
  144. struct drm_plane *plane;
  145. struct drm_crtc *crtc;
  146. plane = mdp5_plane_init(dev, crtcs[i], true);
  147. if (IS_ERR(plane)) {
  148. ret = PTR_ERR(plane);
  149. dev_err(dev->dev, "failed to construct plane for %s (%d)\n",
  150. pipe2name(crtcs[i]), ret);
  151. goto fail;
  152. }
  153. crtc = mdp5_crtc_init(dev, plane, i);
  154. if (IS_ERR(crtc)) {
  155. ret = PTR_ERR(crtc);
  156. dev_err(dev->dev, "failed to construct crtc for %s (%d)\n",
  157. pipe2name(crtcs[i]), ret);
  158. goto fail;
  159. }
  160. priv->crtcs[priv->num_crtcs++] = crtc;
  161. }
  162. /* Construct encoder for HDMI: */
  163. encoder = mdp5_encoder_init(dev, 3, INTF_HDMI);
  164. if (IS_ERR(encoder)) {
  165. dev_err(dev->dev, "failed to construct encoder\n");
  166. ret = PTR_ERR(encoder);
  167. goto fail;
  168. }
  169. /* NOTE: the vsync and error irq's are actually associated with
  170. * the INTF/encoder.. the easiest way to deal with this (ie. what
  171. * we do now) is assume a fixed relationship between crtc's and
  172. * encoders. I'm not sure if there is ever a need to more freely
  173. * assign crtcs to encoders, but if there is then we need to take
  174. * care of error and vblank irq's that the crtc has registered,
  175. * and also update user-requested vblank_mask.
  176. */
  177. encoder->possible_crtcs = BIT(0);
  178. mdp5_crtc_set_intf(priv->crtcs[0], 3, INTF_HDMI);
  179. priv->encoders[priv->num_encoders++] = encoder;
  180. /* Construct bridge/connector for HDMI: */
  181. mdp5_kms->hdmi = hdmi_init(dev, encoder);
  182. if (IS_ERR(mdp5_kms->hdmi)) {
  183. ret = PTR_ERR(mdp5_kms->hdmi);
  184. dev_err(dev->dev, "failed to initialize HDMI: %d\n", ret);
  185. goto fail;
  186. }
  187. return 0;
  188. fail:
  189. return ret;
  190. }
  191. static int get_clk(struct platform_device *pdev, struct clk **clkp,
  192. const char *name)
  193. {
  194. struct device *dev = &pdev->dev;
  195. struct clk *clk = devm_clk_get(dev, name);
  196. if (IS_ERR(clk)) {
  197. dev_err(dev, "failed to get %s (%ld)\n", name, PTR_ERR(clk));
  198. return PTR_ERR(clk);
  199. }
  200. *clkp = clk;
  201. return 0;
  202. }
  203. struct msm_kms *mdp5_kms_init(struct drm_device *dev)
  204. {
  205. struct platform_device *pdev = dev->platformdev;
  206. struct mdp5_platform_config *config = mdp5_get_config(pdev);
  207. struct mdp5_kms *mdp5_kms;
  208. struct msm_kms *kms = NULL;
  209. struct msm_mmu *mmu;
  210. int ret;
  211. mdp5_kms = kzalloc(sizeof(*mdp5_kms), GFP_KERNEL);
  212. if (!mdp5_kms) {
  213. dev_err(dev->dev, "failed to allocate kms\n");
  214. ret = -ENOMEM;
  215. goto fail;
  216. }
  217. mdp_kms_init(&mdp5_kms->base, &kms_funcs);
  218. kms = &mdp5_kms->base.base;
  219. mdp5_kms->dev = dev;
  220. mdp5_kms->smp_blk_cnt = config->smp_blk_cnt;
  221. mdp5_kms->mmio = msm_ioremap(pdev, "mdp_phys", "MDP5");
  222. if (IS_ERR(mdp5_kms->mmio)) {
  223. ret = PTR_ERR(mdp5_kms->mmio);
  224. goto fail;
  225. }
  226. mdp5_kms->vbif = msm_ioremap(pdev, "vbif_phys", "VBIF");
  227. if (IS_ERR(mdp5_kms->vbif)) {
  228. ret = PTR_ERR(mdp5_kms->vbif);
  229. goto fail;
  230. }
  231. mdp5_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
  232. if (IS_ERR(mdp5_kms->vdd)) {
  233. ret = PTR_ERR(mdp5_kms->vdd);
  234. goto fail;
  235. }
  236. ret = regulator_enable(mdp5_kms->vdd);
  237. if (ret) {
  238. dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
  239. goto fail;
  240. }
  241. ret = get_clk(pdev, &mdp5_kms->axi_clk, "bus_clk");
  242. if (ret)
  243. goto fail;
  244. ret = get_clk(pdev, &mdp5_kms->ahb_clk, "iface_clk");
  245. if (ret)
  246. goto fail;
  247. ret = get_clk(pdev, &mdp5_kms->src_clk, "core_clk_src");
  248. if (ret)
  249. goto fail;
  250. ret = get_clk(pdev, &mdp5_kms->core_clk, "core_clk");
  251. if (ret)
  252. goto fail;
  253. ret = get_clk(pdev, &mdp5_kms->lut_clk, "lut_clk");
  254. if (ret)
  255. goto fail;
  256. ret = get_clk(pdev, &mdp5_kms->vsync_clk, "vsync_clk");
  257. if (ret)
  258. goto fail;
  259. ret = clk_set_rate(mdp5_kms->src_clk, config->max_clk);
  260. /* make sure things are off before attaching iommu (bootloader could
  261. * have left things on, in which case we'll start getting faults if
  262. * we don't disable):
  263. */
  264. mdp5_enable(mdp5_kms);
  265. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(0), 0);
  266. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(1), 0);
  267. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(2), 0);
  268. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(3), 0);
  269. mdp5_disable(mdp5_kms);
  270. mdelay(16);
  271. if (config->iommu) {
  272. mmu = msm_iommu_new(dev, config->iommu);
  273. if (IS_ERR(mmu)) {
  274. ret = PTR_ERR(mmu);
  275. dev_err(dev->dev, "failed to init iommu: %d\n", ret);
  276. goto fail;
  277. }
  278. ret = mmu->funcs->attach(mmu, iommu_ports,
  279. ARRAY_SIZE(iommu_ports));
  280. if (ret) {
  281. dev_err(dev->dev, "failed to attach iommu: %d\n", ret);
  282. mmu->funcs->destroy(mmu);
  283. goto fail;
  284. }
  285. } else {
  286. dev_info(dev->dev, "no iommu, fallback to phys "
  287. "contig buffers for scanout\n");
  288. mmu = NULL;
  289. }
  290. mdp5_kms->mmu = mmu;
  291. mdp5_kms->id = msm_register_mmu(dev, mmu);
  292. if (mdp5_kms->id < 0) {
  293. ret = mdp5_kms->id;
  294. dev_err(dev->dev, "failed to register mdp5 iommu: %d\n", ret);
  295. goto fail;
  296. }
  297. ret = modeset_init(mdp5_kms);
  298. if (ret) {
  299. dev_err(dev->dev, "modeset_init failed: %d\n", ret);
  300. goto fail;
  301. }
  302. return kms;
  303. fail:
  304. if (kms)
  305. mdp5_destroy(kms);
  306. return ERR_PTR(ret);
  307. }
  308. static struct mdp5_platform_config *mdp5_get_config(struct platform_device *dev)
  309. {
  310. static struct mdp5_platform_config config = {};
  311. #ifdef CONFIG_OF
  312. /* TODO */
  313. #endif
  314. return &config;
  315. }