exynos_drm_vidi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* exynos_drm_vidi.c
  2. *
  3. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  4. * Authors:
  5. * Inki Dae <inki.dae@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/platform_device.h>
  16. #include <linux/component.h>
  17. #include <linux/timer.h>
  18. #include <drm/exynos_drm.h>
  19. #include <drm/drm_edid.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include "exynos_drm_drv.h"
  23. #include "exynos_drm_crtc.h"
  24. #include "exynos_drm_fb.h"
  25. #include "exynos_drm_plane.h"
  26. #include "exynos_drm_vidi.h"
  27. /* VIDI uses fixed refresh rate of 50Hz */
  28. #define VIDI_REFRESH_TIME (1000 / 50)
  29. /* vidi has totally three virtual windows. */
  30. #define WINDOWS_NR 3
  31. #define ctx_from_connector(c) container_of(c, struct vidi_context, \
  32. connector)
  33. struct vidi_context {
  34. struct drm_encoder encoder;
  35. struct platform_device *pdev;
  36. struct drm_device *drm_dev;
  37. struct exynos_drm_crtc *crtc;
  38. struct drm_connector connector;
  39. struct exynos_drm_plane planes[WINDOWS_NR];
  40. struct edid *raw_edid;
  41. unsigned int clkdiv;
  42. unsigned int connected;
  43. bool suspended;
  44. struct timer_list timer;
  45. struct mutex lock;
  46. };
  47. static inline struct vidi_context *encoder_to_vidi(struct drm_encoder *e)
  48. {
  49. return container_of(e, struct vidi_context, encoder);
  50. }
  51. static const char fake_edid_info[] = {
  52. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
  53. 0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
  54. 0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
  55. 0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  56. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
  57. 0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
  58. 0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
  59. 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
  60. 0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  61. 0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
  62. 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
  63. 0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
  64. 0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
  65. 0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
  66. 0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
  67. 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
  68. 0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  69. 0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x06
  74. };
  75. static const uint32_t formats[] = {
  76. DRM_FORMAT_XRGB8888,
  77. DRM_FORMAT_ARGB8888,
  78. DRM_FORMAT_NV12,
  79. };
  80. static const enum drm_plane_type vidi_win_types[WINDOWS_NR] = {
  81. DRM_PLANE_TYPE_PRIMARY,
  82. DRM_PLANE_TYPE_OVERLAY,
  83. DRM_PLANE_TYPE_CURSOR,
  84. };
  85. static int vidi_enable_vblank(struct exynos_drm_crtc *crtc)
  86. {
  87. struct vidi_context *ctx = crtc->ctx;
  88. if (ctx->suspended)
  89. return -EPERM;
  90. mod_timer(&ctx->timer,
  91. jiffies + msecs_to_jiffies(VIDI_REFRESH_TIME) - 1);
  92. return 0;
  93. }
  94. static void vidi_disable_vblank(struct exynos_drm_crtc *crtc)
  95. {
  96. }
  97. static void vidi_update_plane(struct exynos_drm_crtc *crtc,
  98. struct exynos_drm_plane *plane)
  99. {
  100. struct drm_plane_state *state = plane->base.state;
  101. struct vidi_context *ctx = crtc->ctx;
  102. dma_addr_t addr;
  103. if (ctx->suspended)
  104. return;
  105. addr = exynos_drm_fb_dma_addr(state->fb, 0);
  106. DRM_DEBUG_KMS("dma_addr = %pad\n", &addr);
  107. }
  108. static void vidi_enable(struct exynos_drm_crtc *crtc)
  109. {
  110. struct vidi_context *ctx = crtc->ctx;
  111. mutex_lock(&ctx->lock);
  112. ctx->suspended = false;
  113. mutex_unlock(&ctx->lock);
  114. drm_crtc_vblank_on(&crtc->base);
  115. }
  116. static void vidi_disable(struct exynos_drm_crtc *crtc)
  117. {
  118. struct vidi_context *ctx = crtc->ctx;
  119. drm_crtc_vblank_off(&crtc->base);
  120. mutex_lock(&ctx->lock);
  121. ctx->suspended = true;
  122. mutex_unlock(&ctx->lock);
  123. }
  124. static const struct exynos_drm_crtc_ops vidi_crtc_ops = {
  125. .enable = vidi_enable,
  126. .disable = vidi_disable,
  127. .enable_vblank = vidi_enable_vblank,
  128. .disable_vblank = vidi_disable_vblank,
  129. .update_plane = vidi_update_plane,
  130. .atomic_flush = exynos_crtc_handle_event,
  131. };
  132. static void vidi_fake_vblank_timer(unsigned long arg)
  133. {
  134. struct vidi_context *ctx = (void *)arg;
  135. if (drm_crtc_handle_vblank(&ctx->crtc->base))
  136. mod_timer(&ctx->timer,
  137. jiffies + msecs_to_jiffies(VIDI_REFRESH_TIME) - 1);
  138. }
  139. static ssize_t vidi_show_connection(struct device *dev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. struct vidi_context *ctx = dev_get_drvdata(dev);
  143. int rc;
  144. mutex_lock(&ctx->lock);
  145. rc = sprintf(buf, "%d\n", ctx->connected);
  146. mutex_unlock(&ctx->lock);
  147. return rc;
  148. }
  149. static ssize_t vidi_store_connection(struct device *dev,
  150. struct device_attribute *attr,
  151. const char *buf, size_t len)
  152. {
  153. struct vidi_context *ctx = dev_get_drvdata(dev);
  154. int ret;
  155. ret = kstrtoint(buf, 0, &ctx->connected);
  156. if (ret)
  157. return ret;
  158. if (ctx->connected > 1)
  159. return -EINVAL;
  160. /* use fake edid data for test. */
  161. if (!ctx->raw_edid)
  162. ctx->raw_edid = (struct edid *)fake_edid_info;
  163. /* if raw_edid isn't same as fake data then it can't be tested. */
  164. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  165. DRM_DEBUG_KMS("edid data is not fake data.\n");
  166. return -EINVAL;
  167. }
  168. DRM_DEBUG_KMS("requested connection.\n");
  169. drm_helper_hpd_irq_event(ctx->drm_dev);
  170. return len;
  171. }
  172. static DEVICE_ATTR(connection, 0644, vidi_show_connection,
  173. vidi_store_connection);
  174. int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
  175. struct drm_file *file_priv)
  176. {
  177. struct vidi_context *ctx = dev_get_drvdata(drm_dev->dev);
  178. struct drm_exynos_vidi_connection *vidi = data;
  179. if (!vidi) {
  180. DRM_DEBUG_KMS("user data for vidi is null.\n");
  181. return -EINVAL;
  182. }
  183. if (vidi->connection > 1) {
  184. DRM_DEBUG_KMS("connection should be 0 or 1.\n");
  185. return -EINVAL;
  186. }
  187. if (ctx->connected == vidi->connection) {
  188. DRM_DEBUG_KMS("same connection request.\n");
  189. return -EINVAL;
  190. }
  191. if (vidi->connection) {
  192. struct edid *raw_edid;
  193. raw_edid = (struct edid *)(unsigned long)vidi->edid;
  194. if (!drm_edid_is_valid(raw_edid)) {
  195. DRM_DEBUG_KMS("edid data is invalid.\n");
  196. return -EINVAL;
  197. }
  198. ctx->raw_edid = drm_edid_duplicate(raw_edid);
  199. if (!ctx->raw_edid) {
  200. DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
  201. return -ENOMEM;
  202. }
  203. } else {
  204. /*
  205. * with connection = 0, free raw_edid
  206. * only if raw edid data isn't same as fake data.
  207. */
  208. if (ctx->raw_edid && ctx->raw_edid !=
  209. (struct edid *)fake_edid_info) {
  210. kfree(ctx->raw_edid);
  211. ctx->raw_edid = NULL;
  212. }
  213. }
  214. ctx->connected = vidi->connection;
  215. drm_helper_hpd_irq_event(ctx->drm_dev);
  216. return 0;
  217. }
  218. static enum drm_connector_status vidi_detect(struct drm_connector *connector,
  219. bool force)
  220. {
  221. struct vidi_context *ctx = ctx_from_connector(connector);
  222. /*
  223. * connection request would come from user side
  224. * to do hotplug through specific ioctl.
  225. */
  226. return ctx->connected ? connector_status_connected :
  227. connector_status_disconnected;
  228. }
  229. static void vidi_connector_destroy(struct drm_connector *connector)
  230. {
  231. }
  232. static const struct drm_connector_funcs vidi_connector_funcs = {
  233. .fill_modes = drm_helper_probe_single_connector_modes,
  234. .detect = vidi_detect,
  235. .destroy = vidi_connector_destroy,
  236. .reset = drm_atomic_helper_connector_reset,
  237. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  238. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  239. };
  240. static int vidi_get_modes(struct drm_connector *connector)
  241. {
  242. struct vidi_context *ctx = ctx_from_connector(connector);
  243. struct edid *edid;
  244. int edid_len;
  245. /*
  246. * the edid data comes from user side and it would be set
  247. * to ctx->raw_edid through specific ioctl.
  248. */
  249. if (!ctx->raw_edid) {
  250. DRM_DEBUG_KMS("raw_edid is null.\n");
  251. return -EFAULT;
  252. }
  253. edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
  254. edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
  255. if (!edid) {
  256. DRM_DEBUG_KMS("failed to allocate edid\n");
  257. return -ENOMEM;
  258. }
  259. drm_mode_connector_update_edid_property(connector, edid);
  260. return drm_add_edid_modes(connector, edid);
  261. }
  262. static const struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
  263. .get_modes = vidi_get_modes,
  264. };
  265. static int vidi_create_connector(struct drm_encoder *encoder)
  266. {
  267. struct vidi_context *ctx = encoder_to_vidi(encoder);
  268. struct drm_connector *connector = &ctx->connector;
  269. int ret;
  270. connector->polled = DRM_CONNECTOR_POLL_HPD;
  271. ret = drm_connector_init(ctx->drm_dev, connector,
  272. &vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  273. if (ret) {
  274. DRM_ERROR("Failed to initialize connector with drm\n");
  275. return ret;
  276. }
  277. drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
  278. drm_mode_connector_attach_encoder(connector, encoder);
  279. return 0;
  280. }
  281. static void exynos_vidi_mode_set(struct drm_encoder *encoder,
  282. struct drm_display_mode *mode,
  283. struct drm_display_mode *adjusted_mode)
  284. {
  285. }
  286. static void exynos_vidi_enable(struct drm_encoder *encoder)
  287. {
  288. }
  289. static void exynos_vidi_disable(struct drm_encoder *encoder)
  290. {
  291. }
  292. static const struct drm_encoder_helper_funcs exynos_vidi_encoder_helper_funcs = {
  293. .mode_set = exynos_vidi_mode_set,
  294. .enable = exynos_vidi_enable,
  295. .disable = exynos_vidi_disable,
  296. };
  297. static const struct drm_encoder_funcs exynos_vidi_encoder_funcs = {
  298. .destroy = drm_encoder_cleanup,
  299. };
  300. static int vidi_bind(struct device *dev, struct device *master, void *data)
  301. {
  302. struct vidi_context *ctx = dev_get_drvdata(dev);
  303. struct drm_device *drm_dev = data;
  304. struct drm_encoder *encoder = &ctx->encoder;
  305. struct exynos_drm_plane *exynos_plane;
  306. struct exynos_drm_plane_config plane_config = { 0 };
  307. unsigned int i;
  308. int ret;
  309. ctx->drm_dev = drm_dev;
  310. plane_config.pixel_formats = formats;
  311. plane_config.num_pixel_formats = ARRAY_SIZE(formats);
  312. for (i = 0; i < WINDOWS_NR; i++) {
  313. plane_config.zpos = i;
  314. plane_config.type = vidi_win_types[i];
  315. ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
  316. &plane_config);
  317. if (ret)
  318. return ret;
  319. }
  320. exynos_plane = &ctx->planes[DEFAULT_WIN];
  321. ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
  322. EXYNOS_DISPLAY_TYPE_VIDI, &vidi_crtc_ops, ctx);
  323. if (IS_ERR(ctx->crtc)) {
  324. DRM_ERROR("failed to create crtc.\n");
  325. return PTR_ERR(ctx->crtc);
  326. }
  327. drm_encoder_init(drm_dev, encoder, &exynos_vidi_encoder_funcs,
  328. DRM_MODE_ENCODER_TMDS, NULL);
  329. drm_encoder_helper_add(encoder, &exynos_vidi_encoder_helper_funcs);
  330. ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_VIDI);
  331. if (ret < 0)
  332. return ret;
  333. ret = vidi_create_connector(encoder);
  334. if (ret) {
  335. DRM_ERROR("failed to create connector ret = %d\n", ret);
  336. drm_encoder_cleanup(encoder);
  337. return ret;
  338. }
  339. return 0;
  340. }
  341. static void vidi_unbind(struct device *dev, struct device *master, void *data)
  342. {
  343. struct vidi_context *ctx = dev_get_drvdata(dev);
  344. del_timer_sync(&ctx->timer);
  345. }
  346. static const struct component_ops vidi_component_ops = {
  347. .bind = vidi_bind,
  348. .unbind = vidi_unbind,
  349. };
  350. static int vidi_probe(struct platform_device *pdev)
  351. {
  352. struct vidi_context *ctx;
  353. int ret;
  354. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  355. if (!ctx)
  356. return -ENOMEM;
  357. ctx->pdev = pdev;
  358. setup_timer(&ctx->timer, vidi_fake_vblank_timer, (unsigned long)ctx);
  359. mutex_init(&ctx->lock);
  360. platform_set_drvdata(pdev, ctx);
  361. ret = device_create_file(&pdev->dev, &dev_attr_connection);
  362. if (ret < 0) {
  363. DRM_ERROR("failed to create connection sysfs.\n");
  364. return ret;
  365. }
  366. ret = component_add(&pdev->dev, &vidi_component_ops);
  367. if (ret)
  368. goto err_remove_file;
  369. return ret;
  370. err_remove_file:
  371. device_remove_file(&pdev->dev, &dev_attr_connection);
  372. return ret;
  373. }
  374. static int vidi_remove(struct platform_device *pdev)
  375. {
  376. struct vidi_context *ctx = platform_get_drvdata(pdev);
  377. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  378. kfree(ctx->raw_edid);
  379. ctx->raw_edid = NULL;
  380. return -EINVAL;
  381. }
  382. component_del(&pdev->dev, &vidi_component_ops);
  383. return 0;
  384. }
  385. struct platform_driver vidi_driver = {
  386. .probe = vidi_probe,
  387. .remove = vidi_remove,
  388. .driver = {
  389. .name = "exynos-drm-vidi",
  390. .owner = THIS_MODULE,
  391. },
  392. };