mdp4_kms.c 9.2 KB

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