mdp4_kms.c 11 KB

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