exynos_drm_vidi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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_fb.h"
  24. #include "exynos_drm_plane.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 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 const enum drm_plane_type vidi_win_types[WINDOWS_NR] = {
  82. DRM_PLANE_TYPE_PRIMARY,
  83. DRM_PLANE_TYPE_OVERLAY,
  84. DRM_PLANE_TYPE_CURSOR,
  85. };
  86. static int vidi_enable_vblank(struct exynos_drm_crtc *crtc)
  87. {
  88. struct vidi_context *ctx = crtc->ctx;
  89. if (ctx->suspended)
  90. return -EPERM;
  91. if (!test_and_set_bit(0, &ctx->irq_flags))
  92. ctx->vblank_on = true;
  93. ctx->direct_vblank = true;
  94. /*
  95. * in case of page flip request, vidi_finish_pageflip function
  96. * will not be called because direct_vblank is true and then
  97. * that function will be called by crtc_ops->update_plane callback
  98. */
  99. schedule_work(&ctx->work);
  100. return 0;
  101. }
  102. static void vidi_disable_vblank(struct exynos_drm_crtc *crtc)
  103. {
  104. struct vidi_context *ctx = crtc->ctx;
  105. if (ctx->suspended)
  106. return;
  107. if (test_and_clear_bit(0, &ctx->irq_flags))
  108. ctx->vblank_on = false;
  109. }
  110. static void vidi_update_plane(struct exynos_drm_crtc *crtc,
  111. struct exynos_drm_plane *plane)
  112. {
  113. struct drm_plane_state *state = plane->base.state;
  114. struct vidi_context *ctx = crtc->ctx;
  115. dma_addr_t addr;
  116. if (ctx->suspended)
  117. return;
  118. addr = exynos_drm_fb_dma_addr(state->fb, 0);
  119. DRM_DEBUG_KMS("dma_addr = %pad\n", &addr);
  120. if (ctx->vblank_on)
  121. schedule_work(&ctx->work);
  122. }
  123. static void vidi_enable(struct exynos_drm_crtc *crtc)
  124. {
  125. struct vidi_context *ctx = crtc->ctx;
  126. mutex_lock(&ctx->lock);
  127. ctx->suspended = false;
  128. /* if vblank was enabled status, enable it again. */
  129. if (test_and_clear_bit(0, &ctx->irq_flags))
  130. vidi_enable_vblank(ctx->crtc);
  131. mutex_unlock(&ctx->lock);
  132. }
  133. static void vidi_disable(struct exynos_drm_crtc *crtc)
  134. {
  135. struct vidi_context *ctx = crtc->ctx;
  136. mutex_lock(&ctx->lock);
  137. ctx->suspended = true;
  138. mutex_unlock(&ctx->lock);
  139. }
  140. static int vidi_ctx_initialize(struct vidi_context *ctx,
  141. struct drm_device *drm_dev)
  142. {
  143. struct exynos_drm_private *priv = drm_dev->dev_private;
  144. ctx->drm_dev = drm_dev;
  145. ctx->pipe = priv->pipe++;
  146. return 0;
  147. }
  148. static const struct exynos_drm_crtc_ops vidi_crtc_ops = {
  149. .enable = vidi_enable,
  150. .disable = vidi_disable,
  151. .enable_vblank = vidi_enable_vblank,
  152. .disable_vblank = vidi_disable_vblank,
  153. .update_plane = vidi_update_plane,
  154. };
  155. static void vidi_fake_vblank_handler(struct work_struct *work)
  156. {
  157. struct vidi_context *ctx = container_of(work, struct vidi_context,
  158. work);
  159. int win;
  160. if (ctx->pipe < 0)
  161. return;
  162. /* refresh rate is about 50Hz. */
  163. usleep_range(16000, 20000);
  164. mutex_lock(&ctx->lock);
  165. if (ctx->direct_vblank) {
  166. drm_crtc_handle_vblank(&ctx->crtc->base);
  167. ctx->direct_vblank = false;
  168. mutex_unlock(&ctx->lock);
  169. return;
  170. }
  171. mutex_unlock(&ctx->lock);
  172. for (win = 0 ; win < WINDOWS_NR ; win++) {
  173. struct exynos_drm_plane *plane = &ctx->planes[win];
  174. if (!plane->pending_fb)
  175. continue;
  176. exynos_drm_crtc_finish_update(ctx->crtc, plane);
  177. }
  178. }
  179. static ssize_t vidi_show_connection(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct vidi_context *ctx = dev_get_drvdata(dev);
  183. int rc;
  184. mutex_lock(&ctx->lock);
  185. rc = sprintf(buf, "%d\n", ctx->connected);
  186. mutex_unlock(&ctx->lock);
  187. return rc;
  188. }
  189. static ssize_t vidi_store_connection(struct device *dev,
  190. struct device_attribute *attr,
  191. const char *buf, size_t len)
  192. {
  193. struct vidi_context *ctx = dev_get_drvdata(dev);
  194. int ret;
  195. ret = kstrtoint(buf, 0, &ctx->connected);
  196. if (ret)
  197. return ret;
  198. if (ctx->connected > 1)
  199. return -EINVAL;
  200. /* use fake edid data for test. */
  201. if (!ctx->raw_edid)
  202. ctx->raw_edid = (struct edid *)fake_edid_info;
  203. /* if raw_edid isn't same as fake data then it can't be tested. */
  204. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  205. DRM_DEBUG_KMS("edid data is not fake data.\n");
  206. return -EINVAL;
  207. }
  208. DRM_DEBUG_KMS("requested connection.\n");
  209. drm_helper_hpd_irq_event(ctx->drm_dev);
  210. return len;
  211. }
  212. static DEVICE_ATTR(connection, 0644, vidi_show_connection,
  213. vidi_store_connection);
  214. int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
  215. struct drm_file *file_priv)
  216. {
  217. struct vidi_context *ctx = dev_get_drvdata(drm_dev->dev);
  218. struct drm_exynos_vidi_connection *vidi = data;
  219. if (!vidi) {
  220. DRM_DEBUG_KMS("user data for vidi is null.\n");
  221. return -EINVAL;
  222. }
  223. if (vidi->connection > 1) {
  224. DRM_DEBUG_KMS("connection should be 0 or 1.\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;
  233. raw_edid = (struct edid *)(unsigned long)vidi->edid;
  234. if (!drm_edid_is_valid(raw_edid)) {
  235. DRM_DEBUG_KMS("edid data is invalid.\n");
  236. return -EINVAL;
  237. }
  238. ctx->raw_edid = drm_edid_duplicate(raw_edid);
  239. if (!ctx->raw_edid) {
  240. DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
  241. return -ENOMEM;
  242. }
  243. } else {
  244. /*
  245. * with connection = 0, free raw_edid
  246. * only if raw edid data isn't same as fake data.
  247. */
  248. if (ctx->raw_edid && ctx->raw_edid !=
  249. (struct edid *)fake_edid_info) {
  250. kfree(ctx->raw_edid);
  251. ctx->raw_edid = NULL;
  252. }
  253. }
  254. ctx->connected = vidi->connection;
  255. drm_helper_hpd_irq_event(ctx->drm_dev);
  256. return 0;
  257. }
  258. static enum drm_connector_status vidi_detect(struct drm_connector *connector,
  259. bool force)
  260. {
  261. struct vidi_context *ctx = ctx_from_connector(connector);
  262. /*
  263. * connection request would come from user side
  264. * to do hotplug through specific ioctl.
  265. */
  266. return ctx->connected ? connector_status_connected :
  267. connector_status_disconnected;
  268. }
  269. static void vidi_connector_destroy(struct drm_connector *connector)
  270. {
  271. }
  272. static const struct drm_connector_funcs vidi_connector_funcs = {
  273. .dpms = drm_atomic_helper_connector_dpms,
  274. .fill_modes = drm_helper_probe_single_connector_modes,
  275. .detect = vidi_detect,
  276. .destroy = vidi_connector_destroy,
  277. .reset = drm_atomic_helper_connector_reset,
  278. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  279. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  280. };
  281. static int vidi_get_modes(struct drm_connector *connector)
  282. {
  283. struct vidi_context *ctx = ctx_from_connector(connector);
  284. struct edid *edid;
  285. int edid_len;
  286. /*
  287. * the edid data comes from user side and it would be set
  288. * to ctx->raw_edid through specific ioctl.
  289. */
  290. if (!ctx->raw_edid) {
  291. DRM_DEBUG_KMS("raw_edid is null.\n");
  292. return -EFAULT;
  293. }
  294. edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
  295. edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
  296. if (!edid) {
  297. DRM_DEBUG_KMS("failed to allocate edid\n");
  298. return -ENOMEM;
  299. }
  300. drm_mode_connector_update_edid_property(connector, edid);
  301. return drm_add_edid_modes(connector, edid);
  302. }
  303. static struct drm_encoder *vidi_best_encoder(struct drm_connector *connector)
  304. {
  305. struct vidi_context *ctx = ctx_from_connector(connector);
  306. return &ctx->encoder;
  307. }
  308. static const struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
  309. .get_modes = vidi_get_modes,
  310. .best_encoder = vidi_best_encoder,
  311. };
  312. static int vidi_create_connector(struct drm_encoder *encoder)
  313. {
  314. struct vidi_context *ctx = encoder_to_vidi(encoder);
  315. struct drm_connector *connector = &ctx->connector;
  316. int ret;
  317. connector->polled = DRM_CONNECTOR_POLL_HPD;
  318. ret = drm_connector_init(ctx->drm_dev, connector,
  319. &vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
  320. if (ret) {
  321. DRM_ERROR("Failed to initialize connector with drm\n");
  322. return ret;
  323. }
  324. drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
  325. drm_connector_register(connector);
  326. drm_mode_connector_attach_encoder(connector, encoder);
  327. return 0;
  328. }
  329. static void exynos_vidi_mode_set(struct drm_encoder *encoder,
  330. struct drm_display_mode *mode,
  331. struct drm_display_mode *adjusted_mode)
  332. {
  333. }
  334. static void exynos_vidi_enable(struct drm_encoder *encoder)
  335. {
  336. }
  337. static void exynos_vidi_disable(struct drm_encoder *encoder)
  338. {
  339. }
  340. static const struct drm_encoder_helper_funcs exynos_vidi_encoder_helper_funcs = {
  341. .mode_set = exynos_vidi_mode_set,
  342. .enable = exynos_vidi_enable,
  343. .disable = exynos_vidi_disable,
  344. };
  345. static const struct drm_encoder_funcs exynos_vidi_encoder_funcs = {
  346. .destroy = drm_encoder_cleanup,
  347. };
  348. static int vidi_bind(struct device *dev, struct device *master, void *data)
  349. {
  350. struct vidi_context *ctx = dev_get_drvdata(dev);
  351. struct drm_device *drm_dev = data;
  352. struct drm_encoder *encoder = &ctx->encoder;
  353. struct exynos_drm_plane *exynos_plane;
  354. struct exynos_drm_plane_config plane_config = { 0 };
  355. unsigned int i;
  356. int pipe, ret;
  357. vidi_ctx_initialize(ctx, drm_dev);
  358. plane_config.pixel_formats = formats;
  359. plane_config.num_pixel_formats = ARRAY_SIZE(formats);
  360. for (i = 0; i < WINDOWS_NR; i++) {
  361. plane_config.zpos = i;
  362. plane_config.type = vidi_win_types[i];
  363. ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
  364. 1 << ctx->pipe, &plane_config);
  365. if (ret)
  366. return ret;
  367. }
  368. exynos_plane = &ctx->planes[DEFAULT_WIN];
  369. ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
  370. ctx->pipe, EXYNOS_DISPLAY_TYPE_VIDI,
  371. &vidi_crtc_ops, ctx);
  372. if (IS_ERR(ctx->crtc)) {
  373. DRM_ERROR("failed to create crtc.\n");
  374. return PTR_ERR(ctx->crtc);
  375. }
  376. pipe = exynos_drm_crtc_get_pipe_from_type(drm_dev,
  377. EXYNOS_DISPLAY_TYPE_VIDI);
  378. if (pipe < 0)
  379. return pipe;
  380. encoder->possible_crtcs = 1 << pipe;
  381. DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
  382. drm_encoder_init(drm_dev, encoder, &exynos_vidi_encoder_funcs,
  383. DRM_MODE_ENCODER_TMDS, NULL);
  384. drm_encoder_helper_add(encoder, &exynos_vidi_encoder_helper_funcs);
  385. ret = vidi_create_connector(encoder);
  386. if (ret) {
  387. DRM_ERROR("failed to create connector ret = %d\n", ret);
  388. drm_encoder_cleanup(encoder);
  389. return ret;
  390. }
  391. return 0;
  392. }
  393. static void vidi_unbind(struct device *dev, struct device *master, void *data)
  394. {
  395. }
  396. static const struct component_ops vidi_component_ops = {
  397. .bind = vidi_bind,
  398. .unbind = vidi_unbind,
  399. };
  400. static int vidi_probe(struct platform_device *pdev)
  401. {
  402. struct vidi_context *ctx;
  403. int ret;
  404. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  405. if (!ctx)
  406. return -ENOMEM;
  407. ctx->pdev = pdev;
  408. INIT_WORK(&ctx->work, vidi_fake_vblank_handler);
  409. mutex_init(&ctx->lock);
  410. platform_set_drvdata(pdev, ctx);
  411. ret = device_create_file(&pdev->dev, &dev_attr_connection);
  412. if (ret < 0) {
  413. DRM_ERROR("failed to create connection sysfs.\n");
  414. return ret;
  415. }
  416. ret = component_add(&pdev->dev, &vidi_component_ops);
  417. if (ret)
  418. goto err_remove_file;
  419. return ret;
  420. err_remove_file:
  421. device_remove_file(&pdev->dev, &dev_attr_connection);
  422. return ret;
  423. }
  424. static int vidi_remove(struct platform_device *pdev)
  425. {
  426. struct vidi_context *ctx = platform_get_drvdata(pdev);
  427. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  428. kfree(ctx->raw_edid);
  429. ctx->raw_edid = NULL;
  430. return -EINVAL;
  431. }
  432. component_del(&pdev->dev, &vidi_component_ops);
  433. return 0;
  434. }
  435. struct platform_driver vidi_driver = {
  436. .probe = vidi_probe,
  437. .remove = vidi_remove,
  438. .driver = {
  439. .name = "exynos-drm-vidi",
  440. .owner = THIS_MODULE,
  441. },
  442. };