exynos_drm_vidi.c 14 KB

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