exynos_drm_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Inki Dae <inki.dae@samsung.com>
  5. * Seung-Woo Kim <sw0312.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <drm/drmP.h>
  14. #include <linux/kernel.h>
  15. #include <linux/wait.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <drm/exynos_drm.h>
  20. #include "exynos_drm_drv.h"
  21. #include "exynos_drm_hdmi.h"
  22. #define to_context(dev) platform_get_drvdata(to_platform_device(dev))
  23. #define to_subdrv(dev) to_context(dev)
  24. #define get_ctx_from_subdrv(subdrv) container_of(subdrv,\
  25. struct drm_hdmi_context, subdrv);
  26. /* platform device pointer for common drm hdmi device. */
  27. static struct platform_device *exynos_drm_hdmi_pdev;
  28. /* Common hdmi subdrv needs to access the hdmi and mixer though context.
  29. * These should be initialied by the repective drivers */
  30. static struct exynos_drm_hdmi_context *hdmi_ctx;
  31. static struct exynos_drm_hdmi_context *mixer_ctx;
  32. /* these callback points shoud be set by specific drivers. */
  33. static struct exynos_hdmi_ops *hdmi_ops;
  34. static struct exynos_mixer_ops *mixer_ops;
  35. struct drm_hdmi_context {
  36. struct exynos_drm_subdrv subdrv;
  37. struct exynos_drm_hdmi_context *hdmi_ctx;
  38. struct exynos_drm_hdmi_context *mixer_ctx;
  39. bool enabled[MIXER_WIN_NR];
  40. };
  41. int exynos_platform_device_hdmi_register(void)
  42. {
  43. struct platform_device *pdev;
  44. if (exynos_drm_hdmi_pdev)
  45. return -EEXIST;
  46. pdev = platform_device_register_simple(
  47. "exynos-drm-hdmi", -1, NULL, 0);
  48. if (IS_ERR(pdev))
  49. return PTR_ERR(pdev);
  50. exynos_drm_hdmi_pdev = pdev;
  51. return 0;
  52. }
  53. void exynos_platform_device_hdmi_unregister(void)
  54. {
  55. if (exynos_drm_hdmi_pdev) {
  56. platform_device_unregister(exynos_drm_hdmi_pdev);
  57. exynos_drm_hdmi_pdev = NULL;
  58. }
  59. }
  60. void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
  61. {
  62. if (ctx)
  63. hdmi_ctx = ctx;
  64. }
  65. void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
  66. {
  67. if (ctx)
  68. mixer_ctx = ctx;
  69. }
  70. void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
  71. {
  72. DRM_DEBUG_KMS("%s\n", __FILE__);
  73. if (ops)
  74. hdmi_ops = ops;
  75. }
  76. void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
  77. {
  78. DRM_DEBUG_KMS("%s\n", __FILE__);
  79. if (ops)
  80. mixer_ops = ops;
  81. }
  82. static bool drm_hdmi_is_connected(struct device *dev)
  83. {
  84. struct drm_hdmi_context *ctx = to_context(dev);
  85. DRM_DEBUG_KMS("%s\n", __FILE__);
  86. if (hdmi_ops && hdmi_ops->is_connected)
  87. return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
  88. return false;
  89. }
  90. static struct edid *drm_hdmi_get_edid(struct device *dev,
  91. struct drm_connector *connector)
  92. {
  93. struct drm_hdmi_context *ctx = to_context(dev);
  94. DRM_DEBUG_KMS("%s\n", __FILE__);
  95. if (hdmi_ops && hdmi_ops->get_edid)
  96. return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
  97. return NULL;
  98. }
  99. static int drm_hdmi_check_timing(struct device *dev, void *timing)
  100. {
  101. struct drm_hdmi_context *ctx = to_context(dev);
  102. int ret = 0;
  103. DRM_DEBUG_KMS("%s\n", __FILE__);
  104. /*
  105. * Both, mixer and hdmi should be able to handle the requested mode.
  106. * If any of the two fails, return mode as BAD.
  107. */
  108. if (mixer_ops && mixer_ops->check_timing)
  109. ret = mixer_ops->check_timing(ctx->mixer_ctx->ctx, timing);
  110. if (ret)
  111. return ret;
  112. if (hdmi_ops && hdmi_ops->check_timing)
  113. return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
  114. return 0;
  115. }
  116. static int drm_hdmi_power_on(struct device *dev, int mode)
  117. {
  118. struct drm_hdmi_context *ctx = to_context(dev);
  119. DRM_DEBUG_KMS("%s\n", __FILE__);
  120. if (hdmi_ops && hdmi_ops->power_on)
  121. return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
  122. return 0;
  123. }
  124. static struct exynos_drm_display_ops drm_hdmi_display_ops = {
  125. .type = EXYNOS_DISPLAY_TYPE_HDMI,
  126. .is_connected = drm_hdmi_is_connected,
  127. .get_edid = drm_hdmi_get_edid,
  128. .check_timing = drm_hdmi_check_timing,
  129. .power_on = drm_hdmi_power_on,
  130. };
  131. static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
  132. {
  133. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  134. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  135. struct exynos_drm_manager *manager = subdrv->manager;
  136. DRM_DEBUG_KMS("%s\n", __FILE__);
  137. if (mixer_ops && mixer_ops->enable_vblank)
  138. return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
  139. manager->pipe);
  140. return 0;
  141. }
  142. static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
  143. {
  144. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  145. DRM_DEBUG_KMS("%s\n", __FILE__);
  146. if (mixer_ops && mixer_ops->disable_vblank)
  147. return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
  148. }
  149. static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
  150. {
  151. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  152. DRM_DEBUG_KMS("%s\n", __FILE__);
  153. if (mixer_ops && mixer_ops->wait_for_vblank)
  154. mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
  155. }
  156. static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
  157. struct drm_connector *connector,
  158. const struct drm_display_mode *mode,
  159. struct drm_display_mode *adjusted_mode)
  160. {
  161. struct drm_display_mode *m;
  162. int mode_ok;
  163. DRM_DEBUG_KMS("%s\n", __FILE__);
  164. drm_mode_set_crtcinfo(adjusted_mode, 0);
  165. mode_ok = drm_hdmi_check_timing(subdrv_dev, adjusted_mode);
  166. /* just return if user desired mode exists. */
  167. if (mode_ok == 0)
  168. return;
  169. /*
  170. * otherwise, find the most suitable mode among modes and change it
  171. * to adjusted_mode.
  172. */
  173. list_for_each_entry(m, &connector->modes, head) {
  174. mode_ok = drm_hdmi_check_timing(subdrv_dev, m);
  175. if (mode_ok == 0) {
  176. struct drm_mode_object base;
  177. struct list_head head;
  178. DRM_INFO("desired mode doesn't exist so\n");
  179. DRM_INFO("use the most suitable mode among modes.\n");
  180. DRM_DEBUG_KMS("Adjusted Mode: [%d]x[%d] [%d]Hz\n",
  181. m->hdisplay, m->vdisplay, m->vrefresh);
  182. /* preserve display mode header while copying. */
  183. head = adjusted_mode->head;
  184. base = adjusted_mode->base;
  185. memcpy(adjusted_mode, m, sizeof(*m));
  186. adjusted_mode->head = head;
  187. adjusted_mode->base = base;
  188. break;
  189. }
  190. }
  191. }
  192. static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
  193. {
  194. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  195. DRM_DEBUG_KMS("%s\n", __FILE__);
  196. if (hdmi_ops && hdmi_ops->mode_set)
  197. hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
  198. }
  199. static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
  200. unsigned int *width, unsigned int *height)
  201. {
  202. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  203. DRM_DEBUG_KMS("%s\n", __FILE__);
  204. if (hdmi_ops && hdmi_ops->get_max_resol)
  205. hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
  206. }
  207. static void drm_hdmi_commit(struct device *subdrv_dev)
  208. {
  209. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  210. DRM_DEBUG_KMS("%s\n", __FILE__);
  211. if (hdmi_ops && hdmi_ops->commit)
  212. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  213. }
  214. static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
  215. {
  216. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  217. DRM_DEBUG_KMS("%s\n", __FILE__);
  218. if (mixer_ops && mixer_ops->dpms)
  219. mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
  220. if (hdmi_ops && hdmi_ops->dpms)
  221. hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
  222. }
  223. static void drm_hdmi_apply(struct device *subdrv_dev)
  224. {
  225. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  226. int i;
  227. DRM_DEBUG_KMS("%s\n", __FILE__);
  228. for (i = 0; i < MIXER_WIN_NR; i++) {
  229. if (!ctx->enabled[i])
  230. continue;
  231. if (mixer_ops && mixer_ops->win_commit)
  232. mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
  233. }
  234. if (hdmi_ops && hdmi_ops->commit)
  235. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  236. }
  237. static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
  238. .dpms = drm_hdmi_dpms,
  239. .apply = drm_hdmi_apply,
  240. .enable_vblank = drm_hdmi_enable_vblank,
  241. .disable_vblank = drm_hdmi_disable_vblank,
  242. .wait_for_vblank = drm_hdmi_wait_for_vblank,
  243. .mode_fixup = drm_hdmi_mode_fixup,
  244. .mode_set = drm_hdmi_mode_set,
  245. .get_max_resol = drm_hdmi_get_max_resol,
  246. .commit = drm_hdmi_commit,
  247. };
  248. static void drm_mixer_mode_set(struct device *subdrv_dev,
  249. struct exynos_drm_overlay *overlay)
  250. {
  251. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  252. DRM_DEBUG_KMS("%s\n", __FILE__);
  253. if (mixer_ops && mixer_ops->win_mode_set)
  254. mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
  255. }
  256. static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
  257. {
  258. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  259. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  260. DRM_DEBUG_KMS("%s\n", __FILE__);
  261. if (win < 0 || win > MIXER_WIN_NR) {
  262. DRM_ERROR("mixer window[%d] is wrong\n", win);
  263. return;
  264. }
  265. if (mixer_ops && mixer_ops->win_commit)
  266. mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
  267. ctx->enabled[win] = true;
  268. }
  269. static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
  270. {
  271. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  272. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  273. DRM_DEBUG_KMS("%s\n", __FILE__);
  274. if (win < 0 || win > MIXER_WIN_NR) {
  275. DRM_ERROR("mixer window[%d] is wrong\n", win);
  276. return;
  277. }
  278. if (mixer_ops && mixer_ops->win_disable)
  279. mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
  280. ctx->enabled[win] = false;
  281. }
  282. static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
  283. .mode_set = drm_mixer_mode_set,
  284. .commit = drm_mixer_commit,
  285. .disable = drm_mixer_disable,
  286. };
  287. static struct exynos_drm_manager hdmi_manager = {
  288. .pipe = -1,
  289. .ops = &drm_hdmi_manager_ops,
  290. .overlay_ops = &drm_hdmi_overlay_ops,
  291. .display_ops = &drm_hdmi_display_ops,
  292. };
  293. static int hdmi_subdrv_probe(struct drm_device *drm_dev,
  294. struct device *dev)
  295. {
  296. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  297. struct drm_hdmi_context *ctx;
  298. DRM_DEBUG_KMS("%s\n", __FILE__);
  299. if (!hdmi_ctx) {
  300. DRM_ERROR("hdmi context not initialized.\n");
  301. return -EFAULT;
  302. }
  303. if (!mixer_ctx) {
  304. DRM_ERROR("mixer context not initialized.\n");
  305. return -EFAULT;
  306. }
  307. ctx = get_ctx_from_subdrv(subdrv);
  308. if (!ctx) {
  309. DRM_ERROR("no drm hdmi context.\n");
  310. return -EFAULT;
  311. }
  312. ctx->hdmi_ctx = hdmi_ctx;
  313. ctx->mixer_ctx = mixer_ctx;
  314. ctx->hdmi_ctx->drm_dev = drm_dev;
  315. ctx->mixer_ctx->drm_dev = drm_dev;
  316. if (mixer_ops->iommu_on)
  317. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
  318. return 0;
  319. }
  320. static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  321. {
  322. struct drm_hdmi_context *ctx;
  323. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  324. ctx = get_ctx_from_subdrv(subdrv);
  325. if (mixer_ops->iommu_on)
  326. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
  327. }
  328. static int exynos_drm_hdmi_probe(struct platform_device *pdev)
  329. {
  330. struct device *dev = &pdev->dev;
  331. struct exynos_drm_subdrv *subdrv;
  332. struct drm_hdmi_context *ctx;
  333. DRM_DEBUG_KMS("%s\n", __FILE__);
  334. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  335. if (!ctx) {
  336. DRM_LOG_KMS("failed to alloc common hdmi context.\n");
  337. return -ENOMEM;
  338. }
  339. subdrv = &ctx->subdrv;
  340. subdrv->dev = dev;
  341. subdrv->manager = &hdmi_manager;
  342. subdrv->probe = hdmi_subdrv_probe;
  343. subdrv->remove = hdmi_subdrv_remove;
  344. platform_set_drvdata(pdev, subdrv);
  345. exynos_drm_subdrv_register(subdrv);
  346. return 0;
  347. }
  348. static int exynos_drm_hdmi_remove(struct platform_device *pdev)
  349. {
  350. struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
  351. DRM_DEBUG_KMS("%s\n", __FILE__);
  352. exynos_drm_subdrv_unregister(&ctx->subdrv);
  353. return 0;
  354. }
  355. struct platform_driver exynos_drm_common_hdmi_driver = {
  356. .probe = exynos_drm_hdmi_probe,
  357. .remove = exynos_drm_hdmi_remove,
  358. .driver = {
  359. .name = "exynos-drm-hdmi",
  360. .owner = THIS_MODULE,
  361. },
  362. };