exynos_drm_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. if (exynos_drm_hdmi_pdev)
  44. return -EEXIST;
  45. exynos_drm_hdmi_pdev = platform_device_register_simple(
  46. "exynos-drm-hdmi", -1, NULL, 0);
  47. if (IS_ERR_OR_NULL(exynos_drm_hdmi_pdev))
  48. return PTR_ERR(exynos_drm_hdmi_pdev);
  49. return 0;
  50. }
  51. void exynos_platform_device_hdmi_unregister(void)
  52. {
  53. if (exynos_drm_hdmi_pdev)
  54. platform_device_unregister(exynos_drm_hdmi_pdev);
  55. }
  56. void exynos_hdmi_drv_attach(struct exynos_drm_hdmi_context *ctx)
  57. {
  58. if (ctx)
  59. hdmi_ctx = ctx;
  60. }
  61. void exynos_mixer_drv_attach(struct exynos_drm_hdmi_context *ctx)
  62. {
  63. if (ctx)
  64. mixer_ctx = ctx;
  65. }
  66. void exynos_hdmi_ops_register(struct exynos_hdmi_ops *ops)
  67. {
  68. DRM_DEBUG_KMS("%s\n", __FILE__);
  69. if (ops)
  70. hdmi_ops = ops;
  71. }
  72. void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
  73. {
  74. DRM_DEBUG_KMS("%s\n", __FILE__);
  75. if (ops)
  76. mixer_ops = ops;
  77. }
  78. static bool drm_hdmi_is_connected(struct device *dev)
  79. {
  80. struct drm_hdmi_context *ctx = to_context(dev);
  81. DRM_DEBUG_KMS("%s\n", __FILE__);
  82. if (hdmi_ops && hdmi_ops->is_connected)
  83. return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
  84. return false;
  85. }
  86. static struct edid *drm_hdmi_get_edid(struct device *dev,
  87. struct drm_connector *connector)
  88. {
  89. struct drm_hdmi_context *ctx = to_context(dev);
  90. DRM_DEBUG_KMS("%s\n", __FILE__);
  91. if (hdmi_ops && hdmi_ops->get_edid)
  92. return hdmi_ops->get_edid(ctx->hdmi_ctx->ctx, connector);
  93. return NULL;
  94. }
  95. static int drm_hdmi_check_timing(struct device *dev, void *timing)
  96. {
  97. struct drm_hdmi_context *ctx = to_context(dev);
  98. int ret = 0;
  99. DRM_DEBUG_KMS("%s\n", __FILE__);
  100. /*
  101. * Both, mixer and hdmi should be able to handle the requested mode.
  102. * If any of the two fails, return mode as BAD.
  103. */
  104. if (mixer_ops && mixer_ops->check_timing)
  105. ret = mixer_ops->check_timing(ctx->mixer_ctx->ctx, timing);
  106. if (ret)
  107. return ret;
  108. if (hdmi_ops && hdmi_ops->check_timing)
  109. return hdmi_ops->check_timing(ctx->hdmi_ctx->ctx, timing);
  110. return 0;
  111. }
  112. static int drm_hdmi_power_on(struct device *dev, int mode)
  113. {
  114. struct drm_hdmi_context *ctx = to_context(dev);
  115. DRM_DEBUG_KMS("%s\n", __FILE__);
  116. if (hdmi_ops && hdmi_ops->power_on)
  117. return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
  118. return 0;
  119. }
  120. static struct exynos_drm_display_ops drm_hdmi_display_ops = {
  121. .type = EXYNOS_DISPLAY_TYPE_HDMI,
  122. .is_connected = drm_hdmi_is_connected,
  123. .get_edid = drm_hdmi_get_edid,
  124. .check_timing = drm_hdmi_check_timing,
  125. .power_on = drm_hdmi_power_on,
  126. };
  127. static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
  128. {
  129. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  130. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  131. struct exynos_drm_manager *manager = subdrv->manager;
  132. DRM_DEBUG_KMS("%s\n", __FILE__);
  133. if (mixer_ops && mixer_ops->enable_vblank)
  134. return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
  135. manager->pipe);
  136. return 0;
  137. }
  138. static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
  139. {
  140. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  141. DRM_DEBUG_KMS("%s\n", __FILE__);
  142. if (mixer_ops && mixer_ops->disable_vblank)
  143. return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
  144. }
  145. static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
  146. {
  147. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  148. DRM_DEBUG_KMS("%s\n", __FILE__);
  149. if (mixer_ops && mixer_ops->wait_for_vblank)
  150. mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
  151. }
  152. static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
  153. struct drm_connector *connector,
  154. const struct drm_display_mode *mode,
  155. struct drm_display_mode *adjusted_mode)
  156. {
  157. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  158. DRM_DEBUG_KMS("%s\n", __FILE__);
  159. if (hdmi_ops && hdmi_ops->mode_fixup)
  160. hdmi_ops->mode_fixup(ctx->hdmi_ctx->ctx, connector, mode,
  161. adjusted_mode);
  162. }
  163. static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
  164. {
  165. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  166. DRM_DEBUG_KMS("%s\n", __FILE__);
  167. if (hdmi_ops && hdmi_ops->mode_set)
  168. hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
  169. }
  170. static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
  171. unsigned int *width, unsigned int *height)
  172. {
  173. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  174. DRM_DEBUG_KMS("%s\n", __FILE__);
  175. if (hdmi_ops && hdmi_ops->get_max_resol)
  176. hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
  177. }
  178. static void drm_hdmi_commit(struct device *subdrv_dev)
  179. {
  180. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  181. DRM_DEBUG_KMS("%s\n", __FILE__);
  182. if (hdmi_ops && hdmi_ops->commit)
  183. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  184. }
  185. static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
  186. {
  187. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  188. DRM_DEBUG_KMS("%s\n", __FILE__);
  189. if (mixer_ops && mixer_ops->dpms)
  190. mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
  191. if (hdmi_ops && hdmi_ops->dpms)
  192. hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
  193. }
  194. static void drm_hdmi_apply(struct device *subdrv_dev)
  195. {
  196. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  197. int i;
  198. DRM_DEBUG_KMS("%s\n", __FILE__);
  199. for (i = 0; i < MIXER_WIN_NR; i++) {
  200. if (!ctx->enabled[i])
  201. continue;
  202. if (mixer_ops && mixer_ops->win_commit)
  203. mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
  204. }
  205. if (hdmi_ops && hdmi_ops->commit)
  206. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  207. }
  208. static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
  209. .dpms = drm_hdmi_dpms,
  210. .apply = drm_hdmi_apply,
  211. .enable_vblank = drm_hdmi_enable_vblank,
  212. .disable_vblank = drm_hdmi_disable_vblank,
  213. .wait_for_vblank = drm_hdmi_wait_for_vblank,
  214. .mode_fixup = drm_hdmi_mode_fixup,
  215. .mode_set = drm_hdmi_mode_set,
  216. .get_max_resol = drm_hdmi_get_max_resol,
  217. .commit = drm_hdmi_commit,
  218. };
  219. static void drm_mixer_mode_set(struct device *subdrv_dev,
  220. struct exynos_drm_overlay *overlay)
  221. {
  222. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  223. DRM_DEBUG_KMS("%s\n", __FILE__);
  224. if (mixer_ops && mixer_ops->win_mode_set)
  225. mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
  226. }
  227. static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
  228. {
  229. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  230. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  231. DRM_DEBUG_KMS("%s\n", __FILE__);
  232. if (win < 0 || win > MIXER_WIN_NR) {
  233. DRM_ERROR("mixer window[%d] is wrong\n", win);
  234. return;
  235. }
  236. if (mixer_ops && mixer_ops->win_commit)
  237. mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
  238. ctx->enabled[win] = true;
  239. }
  240. static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
  241. {
  242. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  243. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  244. DRM_DEBUG_KMS("%s\n", __FILE__);
  245. if (win < 0 || win > MIXER_WIN_NR) {
  246. DRM_ERROR("mixer window[%d] is wrong\n", win);
  247. return;
  248. }
  249. if (mixer_ops && mixer_ops->win_disable)
  250. mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
  251. ctx->enabled[win] = false;
  252. }
  253. static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
  254. .mode_set = drm_mixer_mode_set,
  255. .commit = drm_mixer_commit,
  256. .disable = drm_mixer_disable,
  257. };
  258. static struct exynos_drm_manager hdmi_manager = {
  259. .pipe = -1,
  260. .ops = &drm_hdmi_manager_ops,
  261. .overlay_ops = &drm_hdmi_overlay_ops,
  262. .display_ops = &drm_hdmi_display_ops,
  263. };
  264. static int hdmi_subdrv_probe(struct drm_device *drm_dev,
  265. struct device *dev)
  266. {
  267. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  268. struct drm_hdmi_context *ctx;
  269. DRM_DEBUG_KMS("%s\n", __FILE__);
  270. if (!hdmi_ctx) {
  271. DRM_ERROR("hdmi context not initialized.\n");
  272. return -EFAULT;
  273. }
  274. if (!mixer_ctx) {
  275. DRM_ERROR("mixer context not initialized.\n");
  276. return -EFAULT;
  277. }
  278. ctx = get_ctx_from_subdrv(subdrv);
  279. if (!ctx) {
  280. DRM_ERROR("no drm hdmi context.\n");
  281. return -EFAULT;
  282. }
  283. ctx->hdmi_ctx = hdmi_ctx;
  284. ctx->mixer_ctx = mixer_ctx;
  285. ctx->hdmi_ctx->drm_dev = drm_dev;
  286. ctx->mixer_ctx->drm_dev = drm_dev;
  287. if (mixer_ops->iommu_on)
  288. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
  289. return 0;
  290. }
  291. static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  292. {
  293. struct drm_hdmi_context *ctx;
  294. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  295. ctx = get_ctx_from_subdrv(subdrv);
  296. if (mixer_ops->iommu_on)
  297. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
  298. }
  299. static int exynos_drm_hdmi_probe(struct platform_device *pdev)
  300. {
  301. struct device *dev = &pdev->dev;
  302. struct exynos_drm_subdrv *subdrv;
  303. struct drm_hdmi_context *ctx;
  304. DRM_DEBUG_KMS("%s\n", __FILE__);
  305. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  306. if (!ctx) {
  307. DRM_LOG_KMS("failed to alloc common hdmi context.\n");
  308. return -ENOMEM;
  309. }
  310. subdrv = &ctx->subdrv;
  311. subdrv->dev = dev;
  312. subdrv->manager = &hdmi_manager;
  313. subdrv->probe = hdmi_subdrv_probe;
  314. subdrv->remove = hdmi_subdrv_remove;
  315. platform_set_drvdata(pdev, subdrv);
  316. exynos_drm_subdrv_register(subdrv);
  317. return 0;
  318. }
  319. static int exynos_drm_hdmi_remove(struct platform_device *pdev)
  320. {
  321. struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
  322. DRM_DEBUG_KMS("%s\n", __FILE__);
  323. exynos_drm_subdrv_unregister(&ctx->subdrv);
  324. return 0;
  325. }
  326. struct platform_driver exynos_drm_common_hdmi_driver = {
  327. .probe = exynos_drm_hdmi_probe,
  328. .remove = exynos_drm_hdmi_remove,
  329. .driver = {
  330. .name = "exynos-drm-hdmi",
  331. .owner = THIS_MODULE,
  332. },
  333. };