exynos_drm_vidi.c 12 KB

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