mdp4_kms.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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], file);
  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. /* construct non-private planes: */
  161. plane = mdp4_plane_init(dev, VG1, false);
  162. if (IS_ERR(plane)) {
  163. dev_err(dev->dev, "failed to construct plane for VG1\n");
  164. ret = PTR_ERR(plane);
  165. goto fail;
  166. }
  167. priv->planes[priv->num_planes++] = plane;
  168. plane = mdp4_plane_init(dev, VG2, false);
  169. if (IS_ERR(plane)) {
  170. dev_err(dev->dev, "failed to construct plane for VG2\n");
  171. ret = PTR_ERR(plane);
  172. goto fail;
  173. }
  174. priv->planes[priv->num_planes++] = plane;
  175. /* the CRTCs get constructed with a private plane: */
  176. plane = mdp4_plane_init(dev, RGB1, true);
  177. if (IS_ERR(plane)) {
  178. dev_err(dev->dev, "failed to construct plane for RGB1\n");
  179. ret = PTR_ERR(plane);
  180. goto fail;
  181. }
  182. crtc = mdp4_crtc_init(dev, plane, priv->num_crtcs, 1, DMA_E);
  183. if (IS_ERR(crtc)) {
  184. dev_err(dev->dev, "failed to construct crtc for DMA_E\n");
  185. ret = PTR_ERR(crtc);
  186. goto fail;
  187. }
  188. priv->crtcs[priv->num_crtcs++] = crtc;
  189. encoder = mdp4_dtv_encoder_init(dev);
  190. if (IS_ERR(encoder)) {
  191. dev_err(dev->dev, "failed to construct DTV encoder\n");
  192. ret = PTR_ERR(encoder);
  193. goto fail;
  194. }
  195. encoder->possible_crtcs = 0x1; /* DTV can be hooked to DMA_E */
  196. priv->encoders[priv->num_encoders++] = encoder;
  197. ret = hdmi_init(dev, encoder);
  198. if (ret) {
  199. dev_err(dev->dev, "failed to initialize HDMI\n");
  200. goto fail;
  201. }
  202. return 0;
  203. fail:
  204. return ret;
  205. }
  206. static const char *iommu_ports[] = {
  207. "mdp_port0_cb0", "mdp_port1_cb0",
  208. };
  209. struct msm_kms *mdp4_kms_init(struct drm_device *dev)
  210. {
  211. struct platform_device *pdev = dev->platformdev;
  212. struct mdp4_platform_config *config = mdp4_get_config(pdev);
  213. struct mdp4_kms *mdp4_kms;
  214. struct msm_kms *kms = NULL;
  215. int ret;
  216. mdp4_kms = kzalloc(sizeof(*mdp4_kms), GFP_KERNEL);
  217. if (!mdp4_kms) {
  218. dev_err(dev->dev, "failed to allocate kms\n");
  219. ret = -ENOMEM;
  220. goto fail;
  221. }
  222. kms = &mdp4_kms->base;
  223. kms->funcs = &kms_funcs;
  224. mdp4_kms->dev = dev;
  225. mdp4_kms->mmio = msm_ioremap(pdev, NULL, "MDP4");
  226. if (IS_ERR(mdp4_kms->mmio)) {
  227. ret = PTR_ERR(mdp4_kms->mmio);
  228. goto fail;
  229. }
  230. mdp4_kms->dsi_pll_vdda = devm_regulator_get(&pdev->dev, "dsi_pll_vdda");
  231. if (IS_ERR(mdp4_kms->dsi_pll_vdda))
  232. mdp4_kms->dsi_pll_vdda = NULL;
  233. mdp4_kms->dsi_pll_vddio = devm_regulator_get(&pdev->dev, "dsi_pll_vddio");
  234. if (IS_ERR(mdp4_kms->dsi_pll_vddio))
  235. mdp4_kms->dsi_pll_vddio = NULL;
  236. mdp4_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
  237. if (IS_ERR(mdp4_kms->vdd))
  238. mdp4_kms->vdd = NULL;
  239. if (mdp4_kms->vdd) {
  240. ret = regulator_enable(mdp4_kms->vdd);
  241. if (ret) {
  242. dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
  243. goto fail;
  244. }
  245. }
  246. mdp4_kms->clk = devm_clk_get(&pdev->dev, "core_clk");
  247. if (IS_ERR(mdp4_kms->clk)) {
  248. dev_err(dev->dev, "failed to get core_clk\n");
  249. ret = PTR_ERR(mdp4_kms->clk);
  250. goto fail;
  251. }
  252. mdp4_kms->pclk = devm_clk_get(&pdev->dev, "iface_clk");
  253. if (IS_ERR(mdp4_kms->pclk))
  254. mdp4_kms->pclk = NULL;
  255. // XXX if (rev >= MDP_REV_42) { ???
  256. mdp4_kms->lut_clk = devm_clk_get(&pdev->dev, "lut_clk");
  257. if (IS_ERR(mdp4_kms->lut_clk)) {
  258. dev_err(dev->dev, "failed to get lut_clk\n");
  259. ret = PTR_ERR(mdp4_kms->lut_clk);
  260. goto fail;
  261. }
  262. clk_set_rate(mdp4_kms->clk, config->max_clk);
  263. clk_set_rate(mdp4_kms->lut_clk, config->max_clk);
  264. if (!config->iommu) {
  265. dev_err(dev->dev, "no iommu\n");
  266. ret = -ENXIO;
  267. goto fail;
  268. }
  269. /* make sure things are off before attaching iommu (bootloader could
  270. * have left things on, in which case we'll start getting faults if
  271. * we don't disable):
  272. */
  273. mdp4_write(mdp4_kms, REG_MDP4_DTV_ENABLE, 0);
  274. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ENABLE, 0);
  275. mdp4_write(mdp4_kms, REG_MDP4_DSI_ENABLE, 0);
  276. mdelay(16);
  277. ret = msm_iommu_attach(dev, config->iommu,
  278. iommu_ports, ARRAY_SIZE(iommu_ports));
  279. if (ret)
  280. goto fail;
  281. mdp4_kms->id = msm_register_iommu(dev, config->iommu);
  282. if (mdp4_kms->id < 0) {
  283. ret = mdp4_kms->id;
  284. dev_err(dev->dev, "failed to register mdp4 iommu: %d\n", ret);
  285. goto fail;
  286. }
  287. ret = modeset_init(mdp4_kms);
  288. if (ret) {
  289. dev_err(dev->dev, "modeset_init failed: %d\n", ret);
  290. goto fail;
  291. }
  292. return kms;
  293. fail:
  294. if (kms)
  295. mdp4_destroy(kms);
  296. return ERR_PTR(ret);
  297. }
  298. static struct mdp4_platform_config *mdp4_get_config(struct platform_device *dev)
  299. {
  300. static struct mdp4_platform_config config = {};
  301. #ifdef CONFIG_OF
  302. /* TODO */
  303. #else
  304. if (cpu_is_apq8064())
  305. config.max_clk = 266667000;
  306. else
  307. config.max_clk = 200000000;
  308. config.iommu = msm_get_iommu_domain(DISPLAY_READ_DOMAIN);
  309. #endif
  310. return &config;
  311. }