exynos_drm_vidi.c 13 KB

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