mdp4_kms.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 "mdp4_kms.h"
  19. static struct mdp4_platform_config *mdp4_get_config(struct platform_device *dev);
  20. static int mdp4_hw_init(struct msm_kms *kms)
  21. {
  22. struct mdp4_kms *mdp4_kms = to_mdp4_kms(kms);
  23. struct drm_device *dev = mdp4_kms->dev;
  24. uint32_t version, major, minor, dmap_cfg, vg_cfg;
  25. unsigned long clk;
  26. int ret = 0;
  27. pm_runtime_get_sync(dev->dev);
  28. version = mdp4_read(mdp4_kms, REG_MDP4_VERSION);
  29. major = FIELD(version, MDP4_VERSION_MAJOR);
  30. minor = FIELD(version, MDP4_VERSION_MINOR);
  31. DBG("found MDP version v%d.%d", major, minor);
  32. if (major != 4) {
  33. dev_err(dev->dev, "unexpected MDP version: v%d.%d\n",
  34. major, minor);
  35. ret = -ENXIO;
  36. goto out;
  37. }
  38. mdp4_kms->rev = minor;
  39. if (mdp4_kms->dsi_pll_vdda) {
  40. if ((mdp4_kms->rev == 2) || (mdp4_kms->rev == 4)) {
  41. ret = regulator_set_voltage(mdp4_kms->dsi_pll_vdda,
  42. 1200000, 1200000);
  43. if (ret) {
  44. dev_err(dev->dev,
  45. "failed to set dsi_pll_vdda voltage: %d\n", ret);
  46. goto out;
  47. }
  48. }
  49. }
  50. if (mdp4_kms->dsi_pll_vddio) {
  51. if (mdp4_kms->rev == 2) {
  52. ret = regulator_set_voltage(mdp4_kms->dsi_pll_vddio,
  53. 1800000, 1800000);
  54. if (ret) {
  55. dev_err(dev->dev,
  56. "failed to set dsi_pll_vddio voltage: %d\n", ret);
  57. goto out;
  58. }
  59. }
  60. }
  61. if (mdp4_kms->rev > 1) {
  62. mdp4_write(mdp4_kms, REG_MDP4_CS_CONTROLLER0, 0x0707ffff);
  63. mdp4_write(mdp4_kms, REG_MDP4_CS_CONTROLLER1, 0x03073f3f);
  64. }
  65. mdp4_write(mdp4_kms, REG_MDP4_PORTMAP_MODE, 0x3);
  66. /* max read pending cmd config, 3 pending requests: */
  67. mdp4_write(mdp4_kms, REG_MDP4_READ_CNFG, 0x02222);
  68. clk = clk_get_rate(mdp4_kms->clk);
  69. if ((mdp4_kms->rev >= 1) || (clk >= 90000000)) {
  70. dmap_cfg = 0x47; /* 16 bytes-burst x 8 req */
  71. vg_cfg = 0x47; /* 16 bytes-burs x 8 req */
  72. } else {
  73. dmap_cfg = 0x27; /* 8 bytes-burst x 8 req */
  74. vg_cfg = 0x43; /* 16 bytes-burst x 4 req */
  75. }
  76. DBG("fetch config: dmap=%02x, vg=%02x", dmap_cfg, vg_cfg);
  77. mdp4_write(mdp4_kms, REG_MDP4_DMA_FETCH_CONFIG(DMA_P), dmap_cfg);
  78. mdp4_write(mdp4_kms, REG_MDP4_DMA_FETCH_CONFIG(DMA_E), dmap_cfg);
  79. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(VG1), vg_cfg);
  80. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(VG2), vg_cfg);
  81. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(RGB1), vg_cfg);
  82. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(RGB2), vg_cfg);
  83. if (mdp4_kms->rev >= 2)
  84. mdp4_write(mdp4_kms, REG_MDP4_LAYERMIXER_IN_CFG_UPDATE_METHOD, 1);
  85. /* disable CSC matrix / YUV by default: */
  86. mdp4_write(mdp4_kms, REG_MDP4_PIPE_OP_MODE(VG1), 0);
  87. mdp4_write(mdp4_kms, REG_MDP4_PIPE_OP_MODE(VG2), 0);
  88. mdp4_write(mdp4_kms, REG_MDP4_DMA_P_OP_MODE, 0);
  89. mdp4_write(mdp4_kms, REG_MDP4_DMA_S_OP_MODE, 0);
  90. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CSC_CONFIG(1), 0);
  91. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CSC_CONFIG(2), 0);
  92. if (mdp4_kms->rev > 1)
  93. mdp4_write(mdp4_kms, REG_MDP4_RESET_STATUS, 1);
  94. out:
  95. pm_runtime_put_sync(dev->dev);
  96. return ret;
  97. }
  98. static long mdp4_round_pixclk(struct msm_kms *kms, unsigned long rate,
  99. struct drm_encoder *encoder)
  100. {
  101. /* if we had >1 encoder, we'd need something more clever: */
  102. return mdp4_dtv_round_pixclk(encoder, rate);
  103. }
  104. static void mdp4_preclose(struct msm_kms *kms, struct drm_file *file)
  105. {
  106. struct mdp4_kms *mdp4_kms = to_mdp4_kms(kms);
  107. struct msm_drm_private *priv = mdp4_kms->dev->dev_private;
  108. unsigned i;
  109. for (i = 0; i < priv->num_crtcs; i++)
  110. mdp4_crtc_cancel_pending_flip(priv->crtcs[i]);
  111. }
  112. static void mdp4_destroy(struct msm_kms *kms)
  113. {
  114. struct mdp4_kms *mdp4_kms = to_mdp4_kms(kms);
  115. kfree(mdp4_kms);
  116. }
  117. static const struct msm_kms_funcs kms_funcs = {
  118. .hw_init = mdp4_hw_init,
  119. .irq_preinstall = mdp4_irq_preinstall,
  120. .irq_postinstall = mdp4_irq_postinstall,
  121. .irq_uninstall = mdp4_irq_uninstall,
  122. .irq = mdp4_irq,
  123. .enable_vblank = mdp4_enable_vblank,
  124. .disable_vblank = mdp4_disable_vblank,
  125. .get_format = mdp4_get_format,
  126. .round_pixclk = mdp4_round_pixclk,
  127. .preclose = mdp4_preclose,
  128. .destroy = mdp4_destroy,
  129. };
  130. int mdp4_disable(struct mdp4_kms *mdp4_kms)
  131. {
  132. DBG("");
  133. clk_disable_unprepare(mdp4_kms->clk);
  134. if (mdp4_kms->pclk)
  135. clk_disable_unprepare(mdp4_kms->pclk);
  136. clk_disable_unprepare(mdp4_kms->lut_clk);
  137. return 0;
  138. }
  139. int mdp4_enable(struct mdp4_kms *mdp4_kms)
  140. {
  141. DBG("");
  142. clk_prepare_enable(mdp4_kms->clk);
  143. if (mdp4_kms->pclk)
  144. clk_prepare_enable(mdp4_kms->pclk);
  145. clk_prepare_enable(mdp4_kms->lut_clk);
  146. return 0;
  147. }
  148. static int modeset_init(struct mdp4_kms *mdp4_kms)
  149. {
  150. struct drm_device *dev = mdp4_kms->dev;
  151. struct msm_drm_private *priv = dev->dev_private;
  152. struct drm_plane *plane;
  153. struct drm_crtc *crtc;
  154. struct drm_encoder *encoder;
  155. int ret;
  156. /*
  157. * NOTE: this is a bit simplistic until we add support
  158. * for more than just RGB1->DMA_E->DTV->HDMI
  159. */
  160. /* the CRTCs get constructed with a private plane: */
  161. plane = mdp4_plane_init(dev, RGB1, true);
  162. if (IS_ERR(plane)) {
  163. dev_err(dev->dev, "failed to construct plane for RGB1\n");
  164. ret = PTR_ERR(plane);
  165. goto fail;
  166. }
  167. crtc = mdp4_crtc_init(dev, plane, priv->num_crtcs, 1, DMA_E);
  168. if (IS_ERR(crtc)) {
  169. dev_err(dev->dev, "failed to construct crtc for DMA_E\n");
  170. ret = PTR_ERR(crtc);
  171. goto fail;
  172. }
  173. priv->crtcs[priv->num_crtcs++] = crtc;
  174. encoder = mdp4_dtv_encoder_init(dev);
  175. if (IS_ERR(encoder)) {
  176. dev_err(dev->dev, "failed to construct DTV encoder\n");
  177. ret = PTR_ERR(encoder);
  178. goto fail;
  179. }
  180. encoder->possible_crtcs = 0x1; /* DTV can be hooked to DMA_E */
  181. priv->encoders[priv->num_encoders++] = encoder;
  182. ret = hdmi_init(dev, encoder);
  183. if (ret) {
  184. dev_err(dev->dev, "failed to initialize HDMI\n");
  185. goto fail;
  186. }
  187. return 0;
  188. fail:
  189. return ret;
  190. }
  191. static const char *iommu_ports[] = {
  192. "mdp_port0_cb0", "mdp_port1_cb0",
  193. };
  194. struct msm_kms *mdp4_kms_init(struct drm_device *dev)
  195. {
  196. struct platform_device *pdev = dev->platformdev;
  197. struct mdp4_platform_config *config = mdp4_get_config(pdev);
  198. struct mdp4_kms *mdp4_kms;
  199. struct msm_kms *kms = NULL;
  200. int ret;
  201. mdp4_kms = kzalloc(sizeof(*mdp4_kms), GFP_KERNEL);
  202. if (!mdp4_kms) {
  203. dev_err(dev->dev, "failed to allocate kms\n");
  204. ret = -ENOMEM;
  205. goto fail;
  206. }
  207. kms = &mdp4_kms->base;
  208. kms->funcs = &kms_funcs;
  209. mdp4_kms->dev = dev;
  210. mdp4_kms->mmio = msm_ioremap(pdev, NULL, "MDP4");
  211. if (IS_ERR(mdp4_kms->mmio)) {
  212. ret = PTR_ERR(mdp4_kms->mmio);
  213. goto fail;
  214. }
  215. mdp4_kms->dsi_pll_vdda = devm_regulator_get(&pdev->dev, "dsi_pll_vdda");
  216. if (IS_ERR(mdp4_kms->dsi_pll_vdda))
  217. mdp4_kms->dsi_pll_vdda = NULL;
  218. mdp4_kms->dsi_pll_vddio = devm_regulator_get(&pdev->dev, "dsi_pll_vddio");
  219. if (IS_ERR(mdp4_kms->dsi_pll_vddio))
  220. mdp4_kms->dsi_pll_vddio = NULL;
  221. mdp4_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
  222. if (IS_ERR(mdp4_kms->vdd))
  223. mdp4_kms->vdd = NULL;
  224. if (mdp4_kms->vdd) {
  225. ret = regulator_enable(mdp4_kms->vdd);
  226. if (ret) {
  227. dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
  228. goto fail;
  229. }
  230. }
  231. mdp4_kms->clk = devm_clk_get(&pdev->dev, "core_clk");
  232. if (IS_ERR(mdp4_kms->clk)) {
  233. dev_err(dev->dev, "failed to get core_clk\n");
  234. ret = PTR_ERR(mdp4_kms->clk);
  235. goto fail;
  236. }
  237. mdp4_kms->pclk = devm_clk_get(&pdev->dev, "iface_clk");
  238. if (IS_ERR(mdp4_kms->pclk))
  239. mdp4_kms->pclk = NULL;
  240. // XXX if (rev >= MDP_REV_42) { ???
  241. mdp4_kms->lut_clk = devm_clk_get(&pdev->dev, "lut_clk");
  242. if (IS_ERR(mdp4_kms->lut_clk)) {
  243. dev_err(dev->dev, "failed to get lut_clk\n");
  244. ret = PTR_ERR(mdp4_kms->lut_clk);
  245. goto fail;
  246. }
  247. clk_set_rate(mdp4_kms->clk, config->max_clk);
  248. clk_set_rate(mdp4_kms->lut_clk, config->max_clk);
  249. if (!config->iommu) {
  250. dev_err(dev->dev, "no iommu\n");
  251. ret = -ENXIO;
  252. goto fail;
  253. }
  254. /* make sure things are off before attaching iommu (bootloader could
  255. * have left things on, in which case we'll start getting faults if
  256. * we don't disable):
  257. */
  258. mdp4_write(mdp4_kms, REG_MDP4_DTV_ENABLE, 0);
  259. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ENABLE, 0);
  260. mdp4_write(mdp4_kms, REG_MDP4_DSI_ENABLE, 0);
  261. mdelay(16);
  262. ret = msm_iommu_attach(dev, config->iommu,
  263. iommu_ports, ARRAY_SIZE(iommu_ports));
  264. if (ret)
  265. goto fail;
  266. mdp4_kms->id = msm_register_iommu(dev, config->iommu);
  267. if (mdp4_kms->id < 0) {
  268. ret = mdp4_kms->id;
  269. dev_err(dev->dev, "failed to register mdp4 iommu: %d\n", ret);
  270. goto fail;
  271. }
  272. ret = modeset_init(mdp4_kms);
  273. if (ret) {
  274. dev_err(dev->dev, "modeset_init failed: %d\n", ret);
  275. goto fail;
  276. }
  277. return kms;
  278. fail:
  279. if (kms)
  280. mdp4_destroy(kms);
  281. return ERR_PTR(ret);
  282. }
  283. static struct mdp4_platform_config *mdp4_get_config(struct platform_device *dev)
  284. {
  285. static struct mdp4_platform_config config = {};
  286. #ifdef CONFIG_OF
  287. /* TODO */
  288. #else
  289. if (cpu_is_apq8064())
  290. config.max_clk = 266667000;
  291. else
  292. config.max_clk = 200000000;
  293. config.iommu = msm_get_iommu_domain(DISPLAY_READ_DOMAIN);
  294. #endif
  295. return &config;
  296. }