mdp4_kms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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_gem.h"
  19. #include "msm_mmu.h"
  20. #include "mdp4_kms.h"
  21. static struct mdp4_platform_config *mdp4_get_config(struct platform_device *dev);
  22. static int mdp4_hw_init(struct msm_kms *kms)
  23. {
  24. struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
  25. struct drm_device *dev = mdp4_kms->dev;
  26. uint32_t version, major, minor, dmap_cfg, vg_cfg;
  27. unsigned long clk;
  28. int ret = 0;
  29. pm_runtime_get_sync(dev->dev);
  30. mdp4_enable(mdp4_kms);
  31. version = mdp4_read(mdp4_kms, REG_MDP4_VERSION);
  32. mdp4_disable(mdp4_kms);
  33. major = FIELD(version, MDP4_VERSION_MAJOR);
  34. minor = FIELD(version, MDP4_VERSION_MINOR);
  35. DBG("found MDP4 version v%d.%d", major, minor);
  36. if (major != 4) {
  37. dev_err(dev->dev, "unexpected MDP version: v%d.%d\n",
  38. major, minor);
  39. ret = -ENXIO;
  40. goto out;
  41. }
  42. mdp4_kms->rev = minor;
  43. if (mdp4_kms->rev > 1) {
  44. mdp4_write(mdp4_kms, REG_MDP4_CS_CONTROLLER0, 0x0707ffff);
  45. mdp4_write(mdp4_kms, REG_MDP4_CS_CONTROLLER1, 0x03073f3f);
  46. }
  47. mdp4_write(mdp4_kms, REG_MDP4_PORTMAP_MODE, 0x3);
  48. /* max read pending cmd config, 3 pending requests: */
  49. mdp4_write(mdp4_kms, REG_MDP4_READ_CNFG, 0x02222);
  50. clk = clk_get_rate(mdp4_kms->clk);
  51. if ((mdp4_kms->rev >= 1) || (clk >= 90000000)) {
  52. dmap_cfg = 0x47; /* 16 bytes-burst x 8 req */
  53. vg_cfg = 0x47; /* 16 bytes-burs x 8 req */
  54. } else {
  55. dmap_cfg = 0x27; /* 8 bytes-burst x 8 req */
  56. vg_cfg = 0x43; /* 16 bytes-burst x 4 req */
  57. }
  58. DBG("fetch config: dmap=%02x, vg=%02x", dmap_cfg, vg_cfg);
  59. mdp4_write(mdp4_kms, REG_MDP4_DMA_FETCH_CONFIG(DMA_P), dmap_cfg);
  60. mdp4_write(mdp4_kms, REG_MDP4_DMA_FETCH_CONFIG(DMA_E), dmap_cfg);
  61. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(VG1), vg_cfg);
  62. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(VG2), vg_cfg);
  63. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(RGB1), vg_cfg);
  64. mdp4_write(mdp4_kms, REG_MDP4_PIPE_FETCH_CONFIG(RGB2), vg_cfg);
  65. if (mdp4_kms->rev >= 2)
  66. mdp4_write(mdp4_kms, REG_MDP4_LAYERMIXER_IN_CFG_UPDATE_METHOD, 1);
  67. mdp4_write(mdp4_kms, REG_MDP4_LAYERMIXER_IN_CFG, 0);
  68. /* disable CSC matrix / YUV by default: */
  69. mdp4_write(mdp4_kms, REG_MDP4_PIPE_OP_MODE(VG1), 0);
  70. mdp4_write(mdp4_kms, REG_MDP4_PIPE_OP_MODE(VG2), 0);
  71. mdp4_write(mdp4_kms, REG_MDP4_DMA_P_OP_MODE, 0);
  72. mdp4_write(mdp4_kms, REG_MDP4_DMA_S_OP_MODE, 0);
  73. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CSC_CONFIG(1), 0);
  74. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CSC_CONFIG(2), 0);
  75. if (mdp4_kms->rev > 1)
  76. mdp4_write(mdp4_kms, REG_MDP4_RESET_STATUS, 1);
  77. dev->mode_config.allow_fb_modifiers = true;
  78. out:
  79. pm_runtime_put_sync(dev->dev);
  80. return ret;
  81. }
  82. static void mdp4_prepare_commit(struct msm_kms *kms, struct drm_atomic_state *state)
  83. {
  84. struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
  85. int i;
  86. struct drm_crtc *crtc;
  87. struct drm_crtc_state *crtc_state;
  88. mdp4_enable(mdp4_kms);
  89. /* see 119ecb7fd */
  90. for_each_new_crtc_in_state(state, crtc, crtc_state, i)
  91. drm_crtc_vblank_get(crtc);
  92. }
  93. static void mdp4_complete_commit(struct msm_kms *kms, struct drm_atomic_state *state)
  94. {
  95. struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
  96. int i;
  97. struct drm_crtc *crtc;
  98. struct drm_crtc_state *crtc_state;
  99. drm_atomic_helper_wait_for_vblanks(mdp4_kms->dev, state);
  100. /* see 119ecb7fd */
  101. for_each_new_crtc_in_state(state, crtc, crtc_state, i)
  102. drm_crtc_vblank_put(crtc);
  103. mdp4_disable(mdp4_kms);
  104. }
  105. static void mdp4_wait_for_crtc_commit_done(struct msm_kms *kms,
  106. struct drm_crtc *crtc)
  107. {
  108. mdp4_crtc_wait_for_commit_done(crtc);
  109. }
  110. static long mdp4_round_pixclk(struct msm_kms *kms, unsigned long rate,
  111. struct drm_encoder *encoder)
  112. {
  113. /* if we had >1 encoder, we'd need something more clever: */
  114. switch (encoder->encoder_type) {
  115. case DRM_MODE_ENCODER_TMDS:
  116. return mdp4_dtv_round_pixclk(encoder, rate);
  117. case DRM_MODE_ENCODER_LVDS:
  118. case DRM_MODE_ENCODER_DSI:
  119. default:
  120. return rate;
  121. }
  122. }
  123. static const char * const iommu_ports[] = {
  124. "mdp_port0_cb0", "mdp_port1_cb0",
  125. };
  126. static void mdp4_destroy(struct msm_kms *kms)
  127. {
  128. struct mdp4_kms *mdp4_kms = to_mdp4_kms(to_mdp_kms(kms));
  129. struct device *dev = mdp4_kms->dev->dev;
  130. struct msm_gem_address_space *aspace = kms->aspace;
  131. if (mdp4_kms->blank_cursor_iova)
  132. msm_gem_put_iova(mdp4_kms->blank_cursor_bo, kms->aspace);
  133. drm_gem_object_put_unlocked(mdp4_kms->blank_cursor_bo);
  134. if (aspace) {
  135. aspace->mmu->funcs->detach(aspace->mmu,
  136. iommu_ports, ARRAY_SIZE(iommu_ports));
  137. msm_gem_address_space_put(aspace);
  138. }
  139. if (mdp4_kms->rpm_enabled)
  140. pm_runtime_disable(dev);
  141. kfree(mdp4_kms);
  142. }
  143. static const struct mdp_kms_funcs kms_funcs = {
  144. .base = {
  145. .hw_init = mdp4_hw_init,
  146. .irq_preinstall = mdp4_irq_preinstall,
  147. .irq_postinstall = mdp4_irq_postinstall,
  148. .irq_uninstall = mdp4_irq_uninstall,
  149. .irq = mdp4_irq,
  150. .enable_vblank = mdp4_enable_vblank,
  151. .disable_vblank = mdp4_disable_vblank,
  152. .prepare_commit = mdp4_prepare_commit,
  153. .complete_commit = mdp4_complete_commit,
  154. .wait_for_crtc_commit_done = mdp4_wait_for_crtc_commit_done,
  155. .get_format = mdp_get_format,
  156. .round_pixclk = mdp4_round_pixclk,
  157. .destroy = mdp4_destroy,
  158. },
  159. .set_irqmask = mdp4_set_irqmask,
  160. };
  161. int mdp4_disable(struct mdp4_kms *mdp4_kms)
  162. {
  163. DBG("");
  164. clk_disable_unprepare(mdp4_kms->clk);
  165. if (mdp4_kms->pclk)
  166. clk_disable_unprepare(mdp4_kms->pclk);
  167. clk_disable_unprepare(mdp4_kms->lut_clk);
  168. if (mdp4_kms->axi_clk)
  169. clk_disable_unprepare(mdp4_kms->axi_clk);
  170. return 0;
  171. }
  172. int mdp4_enable(struct mdp4_kms *mdp4_kms)
  173. {
  174. DBG("");
  175. clk_prepare_enable(mdp4_kms->clk);
  176. if (mdp4_kms->pclk)
  177. clk_prepare_enable(mdp4_kms->pclk);
  178. clk_prepare_enable(mdp4_kms->lut_clk);
  179. if (mdp4_kms->axi_clk)
  180. clk_prepare_enable(mdp4_kms->axi_clk);
  181. return 0;
  182. }
  183. static int mdp4_modeset_init_intf(struct mdp4_kms *mdp4_kms,
  184. int intf_type)
  185. {
  186. struct drm_device *dev = mdp4_kms->dev;
  187. struct msm_drm_private *priv = dev->dev_private;
  188. struct drm_encoder *encoder;
  189. struct drm_connector *connector;
  190. struct device_node *panel_node;
  191. int dsi_id;
  192. int ret;
  193. switch (intf_type) {
  194. case DRM_MODE_ENCODER_LVDS:
  195. /*
  196. * bail out early if there is no panel node (no need to
  197. * initialize LCDC encoder and LVDS connector)
  198. */
  199. panel_node = of_graph_get_remote_node(dev->dev->of_node, 0, 0);
  200. if (!panel_node)
  201. return 0;
  202. encoder = mdp4_lcdc_encoder_init(dev, panel_node);
  203. if (IS_ERR(encoder)) {
  204. dev_err(dev->dev, "failed to construct LCDC encoder\n");
  205. return PTR_ERR(encoder);
  206. }
  207. /* LCDC can be hooked to DMA_P (TODO: Add DMA_S later?) */
  208. encoder->possible_crtcs = 1 << DMA_P;
  209. connector = mdp4_lvds_connector_init(dev, panel_node, encoder);
  210. if (IS_ERR(connector)) {
  211. dev_err(dev->dev, "failed to initialize LVDS connector\n");
  212. return PTR_ERR(connector);
  213. }
  214. priv->encoders[priv->num_encoders++] = encoder;
  215. priv->connectors[priv->num_connectors++] = connector;
  216. break;
  217. case DRM_MODE_ENCODER_TMDS:
  218. encoder = mdp4_dtv_encoder_init(dev);
  219. if (IS_ERR(encoder)) {
  220. dev_err(dev->dev, "failed to construct DTV encoder\n");
  221. return PTR_ERR(encoder);
  222. }
  223. /* DTV can be hooked to DMA_E: */
  224. encoder->possible_crtcs = 1 << 1;
  225. if (priv->hdmi) {
  226. /* Construct bridge/connector for HDMI: */
  227. ret = msm_hdmi_modeset_init(priv->hdmi, dev, encoder);
  228. if (ret) {
  229. dev_err(dev->dev, "failed to initialize HDMI: %d\n", ret);
  230. return ret;
  231. }
  232. }
  233. priv->encoders[priv->num_encoders++] = encoder;
  234. break;
  235. case DRM_MODE_ENCODER_DSI:
  236. /* only DSI1 supported for now */
  237. dsi_id = 0;
  238. if (!priv->dsi[dsi_id])
  239. break;
  240. encoder = mdp4_dsi_encoder_init(dev);
  241. if (IS_ERR(encoder)) {
  242. ret = PTR_ERR(encoder);
  243. dev_err(dev->dev,
  244. "failed to construct DSI encoder: %d\n", ret);
  245. return ret;
  246. }
  247. /* TODO: Add DMA_S later? */
  248. encoder->possible_crtcs = 1 << DMA_P;
  249. priv->encoders[priv->num_encoders++] = encoder;
  250. ret = msm_dsi_modeset_init(priv->dsi[dsi_id], dev, encoder);
  251. if (ret) {
  252. dev_err(dev->dev, "failed to initialize DSI: %d\n",
  253. ret);
  254. return ret;
  255. }
  256. break;
  257. default:
  258. dev_err(dev->dev, "Invalid or unsupported interface\n");
  259. return -EINVAL;
  260. }
  261. return 0;
  262. }
  263. static int modeset_init(struct mdp4_kms *mdp4_kms)
  264. {
  265. struct drm_device *dev = mdp4_kms->dev;
  266. struct msm_drm_private *priv = dev->dev_private;
  267. struct drm_plane *plane;
  268. struct drm_crtc *crtc;
  269. int i, ret;
  270. static const enum mdp4_pipe rgb_planes[] = {
  271. RGB1, RGB2,
  272. };
  273. static const enum mdp4_pipe vg_planes[] = {
  274. VG1, VG2,
  275. };
  276. static const enum mdp4_dma mdp4_crtcs[] = {
  277. DMA_P, DMA_E,
  278. };
  279. static const char * const mdp4_crtc_names[] = {
  280. "DMA_P", "DMA_E",
  281. };
  282. static const int mdp4_intfs[] = {
  283. DRM_MODE_ENCODER_LVDS,
  284. DRM_MODE_ENCODER_DSI,
  285. DRM_MODE_ENCODER_TMDS,
  286. };
  287. /* construct non-private planes: */
  288. for (i = 0; i < ARRAY_SIZE(vg_planes); i++) {
  289. plane = mdp4_plane_init(dev, vg_planes[i], false);
  290. if (IS_ERR(plane)) {
  291. dev_err(dev->dev,
  292. "failed to construct plane for VG%d\n", i + 1);
  293. ret = PTR_ERR(plane);
  294. goto fail;
  295. }
  296. priv->planes[priv->num_planes++] = plane;
  297. }
  298. for (i = 0; i < ARRAY_SIZE(mdp4_crtcs); i++) {
  299. plane = mdp4_plane_init(dev, rgb_planes[i], true);
  300. if (IS_ERR(plane)) {
  301. dev_err(dev->dev,
  302. "failed to construct plane for RGB%d\n", i + 1);
  303. ret = PTR_ERR(plane);
  304. goto fail;
  305. }
  306. crtc = mdp4_crtc_init(dev, plane, priv->num_crtcs, i,
  307. mdp4_crtcs[i]);
  308. if (IS_ERR(crtc)) {
  309. dev_err(dev->dev, "failed to construct crtc for %s\n",
  310. mdp4_crtc_names[i]);
  311. ret = PTR_ERR(crtc);
  312. goto fail;
  313. }
  314. priv->crtcs[priv->num_crtcs++] = crtc;
  315. }
  316. /*
  317. * we currently set up two relatively fixed paths:
  318. *
  319. * LCDC/LVDS path: RGB1 -> DMA_P -> LCDC -> LVDS
  320. * or
  321. * DSI path: RGB1 -> DMA_P -> DSI1 -> DSI Panel
  322. *
  323. * DTV/HDMI path: RGB2 -> DMA_E -> DTV -> HDMI
  324. */
  325. for (i = 0; i < ARRAY_SIZE(mdp4_intfs); i++) {
  326. ret = mdp4_modeset_init_intf(mdp4_kms, mdp4_intfs[i]);
  327. if (ret) {
  328. dev_err(dev->dev, "failed to initialize intf: %d, %d\n",
  329. i, ret);
  330. goto fail;
  331. }
  332. }
  333. return 0;
  334. fail:
  335. return ret;
  336. }
  337. struct msm_kms *mdp4_kms_init(struct drm_device *dev)
  338. {
  339. struct platform_device *pdev = to_platform_device(dev->dev);
  340. struct mdp4_platform_config *config = mdp4_get_config(pdev);
  341. struct mdp4_kms *mdp4_kms;
  342. struct msm_kms *kms = NULL;
  343. struct msm_gem_address_space *aspace;
  344. int irq, ret;
  345. mdp4_kms = kzalloc(sizeof(*mdp4_kms), GFP_KERNEL);
  346. if (!mdp4_kms) {
  347. dev_err(dev->dev, "failed to allocate kms\n");
  348. ret = -ENOMEM;
  349. goto fail;
  350. }
  351. mdp_kms_init(&mdp4_kms->base, &kms_funcs);
  352. kms = &mdp4_kms->base.base;
  353. mdp4_kms->dev = dev;
  354. mdp4_kms->mmio = msm_ioremap(pdev, NULL, "MDP4");
  355. if (IS_ERR(mdp4_kms->mmio)) {
  356. ret = PTR_ERR(mdp4_kms->mmio);
  357. goto fail;
  358. }
  359. irq = platform_get_irq(pdev, 0);
  360. if (irq < 0) {
  361. ret = irq;
  362. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  363. goto fail;
  364. }
  365. kms->irq = irq;
  366. /* NOTE: driver for this regulator still missing upstream.. use
  367. * _get_exclusive() and ignore the error if it does not exist
  368. * (and hope that the bootloader left it on for us)
  369. */
  370. mdp4_kms->vdd = devm_regulator_get_exclusive(&pdev->dev, "vdd");
  371. if (IS_ERR(mdp4_kms->vdd))
  372. mdp4_kms->vdd = NULL;
  373. if (mdp4_kms->vdd) {
  374. ret = regulator_enable(mdp4_kms->vdd);
  375. if (ret) {
  376. dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
  377. goto fail;
  378. }
  379. }
  380. mdp4_kms->clk = devm_clk_get(&pdev->dev, "core_clk");
  381. if (IS_ERR(mdp4_kms->clk)) {
  382. dev_err(dev->dev, "failed to get core_clk\n");
  383. ret = PTR_ERR(mdp4_kms->clk);
  384. goto fail;
  385. }
  386. mdp4_kms->pclk = devm_clk_get(&pdev->dev, "iface_clk");
  387. if (IS_ERR(mdp4_kms->pclk))
  388. mdp4_kms->pclk = NULL;
  389. // XXX if (rev >= MDP_REV_42) { ???
  390. mdp4_kms->lut_clk = devm_clk_get(&pdev->dev, "lut_clk");
  391. if (IS_ERR(mdp4_kms->lut_clk)) {
  392. dev_err(dev->dev, "failed to get lut_clk\n");
  393. ret = PTR_ERR(mdp4_kms->lut_clk);
  394. goto fail;
  395. }
  396. mdp4_kms->axi_clk = devm_clk_get(&pdev->dev, "bus_clk");
  397. if (IS_ERR(mdp4_kms->axi_clk)) {
  398. dev_err(dev->dev, "failed to get axi_clk\n");
  399. ret = PTR_ERR(mdp4_kms->axi_clk);
  400. goto fail;
  401. }
  402. clk_set_rate(mdp4_kms->clk, config->max_clk);
  403. clk_set_rate(mdp4_kms->lut_clk, config->max_clk);
  404. pm_runtime_enable(dev->dev);
  405. mdp4_kms->rpm_enabled = true;
  406. /* make sure things are off before attaching iommu (bootloader could
  407. * have left things on, in which case we'll start getting faults if
  408. * we don't disable):
  409. */
  410. mdp4_enable(mdp4_kms);
  411. mdp4_write(mdp4_kms, REG_MDP4_DTV_ENABLE, 0);
  412. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ENABLE, 0);
  413. mdp4_write(mdp4_kms, REG_MDP4_DSI_ENABLE, 0);
  414. mdp4_disable(mdp4_kms);
  415. mdelay(16);
  416. if (config->iommu) {
  417. aspace = msm_gem_address_space_create(&pdev->dev,
  418. config->iommu, "mdp4");
  419. if (IS_ERR(aspace)) {
  420. ret = PTR_ERR(aspace);
  421. goto fail;
  422. }
  423. kms->aspace = aspace;
  424. ret = aspace->mmu->funcs->attach(aspace->mmu, iommu_ports,
  425. ARRAY_SIZE(iommu_ports));
  426. if (ret)
  427. goto fail;
  428. } else {
  429. dev_info(dev->dev, "no iommu, fallback to phys "
  430. "contig buffers for scanout\n");
  431. aspace = NULL;
  432. }
  433. ret = modeset_init(mdp4_kms);
  434. if (ret) {
  435. dev_err(dev->dev, "modeset_init failed: %d\n", ret);
  436. goto fail;
  437. }
  438. mdp4_kms->blank_cursor_bo = msm_gem_new(dev, SZ_16K, MSM_BO_WC);
  439. if (IS_ERR(mdp4_kms->blank_cursor_bo)) {
  440. ret = PTR_ERR(mdp4_kms->blank_cursor_bo);
  441. dev_err(dev->dev, "could not allocate blank-cursor bo: %d\n", ret);
  442. mdp4_kms->blank_cursor_bo = NULL;
  443. goto fail;
  444. }
  445. ret = msm_gem_get_iova(mdp4_kms->blank_cursor_bo, kms->aspace,
  446. &mdp4_kms->blank_cursor_iova);
  447. if (ret) {
  448. dev_err(dev->dev, "could not pin blank-cursor bo: %d\n", ret);
  449. goto fail;
  450. }
  451. dev->mode_config.min_width = 0;
  452. dev->mode_config.min_height = 0;
  453. dev->mode_config.max_width = 2048;
  454. dev->mode_config.max_height = 2048;
  455. return kms;
  456. fail:
  457. if (kms)
  458. mdp4_destroy(kms);
  459. return ERR_PTR(ret);
  460. }
  461. static struct mdp4_platform_config *mdp4_get_config(struct platform_device *dev)
  462. {
  463. static struct mdp4_platform_config config = {};
  464. /* TODO: Chips that aren't apq8064 have a 200 Mhz max_clk */
  465. config.max_clk = 266667000;
  466. config.iommu = iommu_domain_alloc(&platform_bus_type);
  467. if (config.iommu) {
  468. config.iommu->geometry.aperture_start = 0x1000;
  469. config.iommu->geometry.aperture_end = 0xffffffff;
  470. }
  471. return &config;
  472. }