exynos_drm_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. if (ops)
  73. hdmi_ops = ops;
  74. }
  75. void exynos_mixer_ops_register(struct exynos_mixer_ops *ops)
  76. {
  77. if (ops)
  78. mixer_ops = ops;
  79. }
  80. static bool drm_hdmi_is_connected(struct device *dev)
  81. {
  82. struct drm_hdmi_context *ctx = to_context(dev);
  83. if (hdmi_ops && hdmi_ops->is_connected)
  84. return hdmi_ops->is_connected(ctx->hdmi_ctx->ctx);
  85. return false;
  86. }
  87. static struct edid *drm_hdmi_get_edid(struct device *dev,
  88. struct drm_connector *connector)
  89. {
  90. struct drm_hdmi_context *ctx = to_context(dev);
  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_mode(struct device *dev,
  96. struct drm_display_mode *mode)
  97. {
  98. struct drm_hdmi_context *ctx = to_context(dev);
  99. int ret = 0;
  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_mode)
  105. ret = mixer_ops->check_mode(ctx->mixer_ctx->ctx, mode);
  106. if (ret)
  107. return ret;
  108. if (hdmi_ops && hdmi_ops->check_mode)
  109. return hdmi_ops->check_mode(ctx->hdmi_ctx->ctx, mode);
  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. if (hdmi_ops && hdmi_ops->power_on)
  116. return hdmi_ops->power_on(ctx->hdmi_ctx->ctx, mode);
  117. return 0;
  118. }
  119. static struct exynos_drm_display_ops drm_hdmi_display_ops = {
  120. .type = EXYNOS_DISPLAY_TYPE_HDMI,
  121. .is_connected = drm_hdmi_is_connected,
  122. .get_edid = drm_hdmi_get_edid,
  123. .check_mode = drm_hdmi_check_mode,
  124. .power_on = drm_hdmi_power_on,
  125. };
  126. static int drm_hdmi_enable_vblank(struct device *subdrv_dev)
  127. {
  128. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  129. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  130. struct exynos_drm_manager *manager = subdrv->manager;
  131. if (mixer_ops && mixer_ops->enable_vblank)
  132. return mixer_ops->enable_vblank(ctx->mixer_ctx->ctx,
  133. manager->pipe);
  134. return 0;
  135. }
  136. static void drm_hdmi_disable_vblank(struct device *subdrv_dev)
  137. {
  138. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  139. if (mixer_ops && mixer_ops->disable_vblank)
  140. return mixer_ops->disable_vblank(ctx->mixer_ctx->ctx);
  141. }
  142. static void drm_hdmi_wait_for_vblank(struct device *subdrv_dev)
  143. {
  144. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  145. if (mixer_ops && mixer_ops->wait_for_vblank)
  146. mixer_ops->wait_for_vblank(ctx->mixer_ctx->ctx);
  147. }
  148. static void drm_hdmi_mode_fixup(struct device *subdrv_dev,
  149. struct drm_connector *connector,
  150. const struct drm_display_mode *mode,
  151. struct drm_display_mode *adjusted_mode)
  152. {
  153. struct drm_display_mode *m;
  154. int mode_ok;
  155. drm_mode_set_crtcinfo(adjusted_mode, 0);
  156. mode_ok = drm_hdmi_check_mode(subdrv_dev, adjusted_mode);
  157. /* just return if user desired mode exists. */
  158. if (mode_ok == 0)
  159. return;
  160. /*
  161. * otherwise, find the most suitable mode among modes and change it
  162. * to adjusted_mode.
  163. */
  164. list_for_each_entry(m, &connector->modes, head) {
  165. mode_ok = drm_hdmi_check_mode(subdrv_dev, m);
  166. if (mode_ok == 0) {
  167. struct drm_mode_object base;
  168. struct list_head head;
  169. DRM_INFO("desired mode doesn't exist so\n");
  170. DRM_INFO("use the most suitable mode among modes.\n");
  171. DRM_DEBUG_KMS("Adjusted Mode: [%d]x[%d] [%d]Hz\n",
  172. m->hdisplay, m->vdisplay, m->vrefresh);
  173. /* preserve display mode header while copying. */
  174. head = adjusted_mode->head;
  175. base = adjusted_mode->base;
  176. memcpy(adjusted_mode, m, sizeof(*m));
  177. adjusted_mode->head = head;
  178. adjusted_mode->base = base;
  179. break;
  180. }
  181. }
  182. }
  183. static void drm_hdmi_mode_set(struct device *subdrv_dev, void *mode)
  184. {
  185. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  186. if (hdmi_ops && hdmi_ops->mode_set)
  187. hdmi_ops->mode_set(ctx->hdmi_ctx->ctx, mode);
  188. }
  189. static void drm_hdmi_get_max_resol(struct device *subdrv_dev,
  190. unsigned int *width, unsigned int *height)
  191. {
  192. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  193. if (hdmi_ops && hdmi_ops->get_max_resol)
  194. hdmi_ops->get_max_resol(ctx->hdmi_ctx->ctx, width, height);
  195. }
  196. static void drm_hdmi_commit(struct device *subdrv_dev)
  197. {
  198. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  199. if (hdmi_ops && hdmi_ops->commit)
  200. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  201. }
  202. static void drm_hdmi_dpms(struct device *subdrv_dev, int mode)
  203. {
  204. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  205. if (mixer_ops && mixer_ops->dpms)
  206. mixer_ops->dpms(ctx->mixer_ctx->ctx, mode);
  207. if (hdmi_ops && hdmi_ops->dpms)
  208. hdmi_ops->dpms(ctx->hdmi_ctx->ctx, mode);
  209. }
  210. static void drm_hdmi_apply(struct device *subdrv_dev)
  211. {
  212. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  213. int i;
  214. for (i = 0; i < MIXER_WIN_NR; i++) {
  215. if (!ctx->enabled[i])
  216. continue;
  217. if (mixer_ops && mixer_ops->win_commit)
  218. mixer_ops->win_commit(ctx->mixer_ctx->ctx, i);
  219. }
  220. if (hdmi_ops && hdmi_ops->commit)
  221. hdmi_ops->commit(ctx->hdmi_ctx->ctx);
  222. }
  223. static struct exynos_drm_manager_ops drm_hdmi_manager_ops = {
  224. .dpms = drm_hdmi_dpms,
  225. .apply = drm_hdmi_apply,
  226. .enable_vblank = drm_hdmi_enable_vblank,
  227. .disable_vblank = drm_hdmi_disable_vblank,
  228. .wait_for_vblank = drm_hdmi_wait_for_vblank,
  229. .mode_fixup = drm_hdmi_mode_fixup,
  230. .mode_set = drm_hdmi_mode_set,
  231. .get_max_resol = drm_hdmi_get_max_resol,
  232. .commit = drm_hdmi_commit,
  233. };
  234. static void drm_mixer_mode_set(struct device *subdrv_dev,
  235. struct exynos_drm_overlay *overlay)
  236. {
  237. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  238. if (mixer_ops && mixer_ops->win_mode_set)
  239. mixer_ops->win_mode_set(ctx->mixer_ctx->ctx, overlay);
  240. }
  241. static void drm_mixer_commit(struct device *subdrv_dev, int zpos)
  242. {
  243. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  244. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  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_commit)
  250. mixer_ops->win_commit(ctx->mixer_ctx->ctx, win);
  251. ctx->enabled[win] = true;
  252. }
  253. static void drm_mixer_disable(struct device *subdrv_dev, int zpos)
  254. {
  255. struct drm_hdmi_context *ctx = to_context(subdrv_dev);
  256. int win = (zpos == DEFAULT_ZPOS) ? MIXER_DEFAULT_WIN : zpos;
  257. if (win < 0 || win >= MIXER_WIN_NR) {
  258. DRM_ERROR("mixer window[%d] is wrong\n", win);
  259. return;
  260. }
  261. if (mixer_ops && mixer_ops->win_disable)
  262. mixer_ops->win_disable(ctx->mixer_ctx->ctx, win);
  263. ctx->enabled[win] = false;
  264. }
  265. static struct exynos_drm_overlay_ops drm_hdmi_overlay_ops = {
  266. .mode_set = drm_mixer_mode_set,
  267. .commit = drm_mixer_commit,
  268. .disable = drm_mixer_disable,
  269. };
  270. static struct exynos_drm_manager hdmi_manager = {
  271. .pipe = -1,
  272. .ops = &drm_hdmi_manager_ops,
  273. .overlay_ops = &drm_hdmi_overlay_ops,
  274. .display_ops = &drm_hdmi_display_ops,
  275. };
  276. static int hdmi_subdrv_probe(struct drm_device *drm_dev,
  277. struct device *dev)
  278. {
  279. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  280. struct drm_hdmi_context *ctx;
  281. if (!hdmi_ctx) {
  282. DRM_ERROR("hdmi context not initialized.\n");
  283. return -EFAULT;
  284. }
  285. if (!mixer_ctx) {
  286. DRM_ERROR("mixer context not initialized.\n");
  287. return -EFAULT;
  288. }
  289. ctx = get_ctx_from_subdrv(subdrv);
  290. if (!ctx) {
  291. DRM_ERROR("no drm hdmi context.\n");
  292. return -EFAULT;
  293. }
  294. ctx->hdmi_ctx = hdmi_ctx;
  295. ctx->mixer_ctx = mixer_ctx;
  296. ctx->hdmi_ctx->drm_dev = drm_dev;
  297. ctx->mixer_ctx->drm_dev = drm_dev;
  298. if (mixer_ops->iommu_on)
  299. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, true);
  300. return 0;
  301. }
  302. static void hdmi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  303. {
  304. struct drm_hdmi_context *ctx;
  305. struct exynos_drm_subdrv *subdrv = to_subdrv(dev);
  306. ctx = get_ctx_from_subdrv(subdrv);
  307. if (mixer_ops->iommu_on)
  308. mixer_ops->iommu_on(ctx->mixer_ctx->ctx, false);
  309. }
  310. static int exynos_drm_hdmi_probe(struct platform_device *pdev)
  311. {
  312. struct device *dev = &pdev->dev;
  313. struct exynos_drm_subdrv *subdrv;
  314. struct drm_hdmi_context *ctx;
  315. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  316. if (!ctx) {
  317. DRM_LOG_KMS("failed to alloc common hdmi context.\n");
  318. return -ENOMEM;
  319. }
  320. subdrv = &ctx->subdrv;
  321. subdrv->dev = dev;
  322. subdrv->manager = &hdmi_manager;
  323. subdrv->probe = hdmi_subdrv_probe;
  324. subdrv->remove = hdmi_subdrv_remove;
  325. platform_set_drvdata(pdev, subdrv);
  326. exynos_drm_subdrv_register(subdrv);
  327. return 0;
  328. }
  329. static int exynos_drm_hdmi_remove(struct platform_device *pdev)
  330. {
  331. struct drm_hdmi_context *ctx = platform_get_drvdata(pdev);
  332. exynos_drm_subdrv_unregister(&ctx->subdrv);
  333. return 0;
  334. }
  335. struct platform_driver exynos_drm_common_hdmi_driver = {
  336. .probe = exynos_drm_hdmi_probe,
  337. .remove = exynos_drm_hdmi_remove,
  338. .driver = {
  339. .name = "exynos-drm-hdmi",
  340. .owner = THIS_MODULE,
  341. },
  342. };