exynos_drm_hdmi.c 10 KB

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