exynos_drm_vidi.c 13 KB

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