exynos_drm_hdmi.c 10 KB

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