exynos_drm_vidi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 <drm/exynos_drm.h>
  17. #include <drm/drm_edid.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include "exynos_drm_drv.h"
  20. #include "exynos_drm_crtc.h"
  21. #include "exynos_drm_encoder.h"
  22. /* vidi has totally three virtual windows. */
  23. #define WINDOWS_NR 3
  24. #define get_vidi_context(dev) platform_get_drvdata(to_platform_device(dev))
  25. struct vidi_win_data {
  26. unsigned int offset_x;
  27. unsigned int offset_y;
  28. unsigned int ovl_width;
  29. unsigned int ovl_height;
  30. unsigned int fb_width;
  31. unsigned int fb_height;
  32. unsigned int bpp;
  33. dma_addr_t dma_addr;
  34. unsigned int buf_offsize;
  35. unsigned int line_size; /* bytes */
  36. bool enabled;
  37. };
  38. struct vidi_context {
  39. struct exynos_drm_subdrv subdrv;
  40. struct drm_crtc *crtc;
  41. struct vidi_win_data win_data[WINDOWS_NR];
  42. struct edid *raw_edid;
  43. unsigned int clkdiv;
  44. unsigned int default_win;
  45. unsigned long irq_flags;
  46. unsigned int connected;
  47. bool vblank_on;
  48. bool suspended;
  49. bool direct_vblank;
  50. struct work_struct work;
  51. struct mutex lock;
  52. };
  53. static const char fake_edid_info[] = {
  54. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
  55. 0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
  56. 0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
  57. 0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  58. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
  59. 0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
  60. 0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
  61. 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
  62. 0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  63. 0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
  64. 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
  65. 0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
  66. 0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
  67. 0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
  68. 0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
  69. 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
  70. 0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  71. 0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x06
  76. };
  77. static bool vidi_display_is_connected(struct device *dev)
  78. {
  79. struct vidi_context *ctx = get_vidi_context(dev);
  80. /*
  81. * connection request would come from user side
  82. * to do hotplug through specific ioctl.
  83. */
  84. return ctx->connected ? true : false;
  85. }
  86. static struct edid *vidi_get_edid(struct device *dev,
  87. struct drm_connector *connector)
  88. {
  89. struct vidi_context *ctx = get_vidi_context(dev);
  90. struct edid *edid;
  91. int edid_len;
  92. /*
  93. * the edid data comes from user side and it would be set
  94. * to ctx->raw_edid through specific ioctl.
  95. */
  96. if (!ctx->raw_edid) {
  97. DRM_DEBUG_KMS("raw_edid is null.\n");
  98. return ERR_PTR(-EFAULT);
  99. }
  100. edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
  101. edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
  102. if (!edid) {
  103. DRM_DEBUG_KMS("failed to allocate edid\n");
  104. return ERR_PTR(-ENOMEM);
  105. }
  106. return edid;
  107. }
  108. static void *vidi_get_panel(struct device *dev)
  109. {
  110. /* TODO. */
  111. return NULL;
  112. }
  113. static int vidi_check_mode(struct device *dev, struct drm_display_mode *mode)
  114. {
  115. /* TODO. */
  116. return 0;
  117. }
  118. static int vidi_display_power_on(struct device *dev, int mode)
  119. {
  120. /* TODO */
  121. return 0;
  122. }
  123. static struct exynos_drm_display_ops vidi_display_ops = {
  124. .type = EXYNOS_DISPLAY_TYPE_VIDI,
  125. .is_connected = vidi_display_is_connected,
  126. .get_edid = vidi_get_edid,
  127. .get_panel = vidi_get_panel,
  128. .check_mode = vidi_check_mode,
  129. .power_on = vidi_display_power_on,
  130. };
  131. static void vidi_dpms(struct device *subdrv_dev, int mode)
  132. {
  133. struct vidi_context *ctx = get_vidi_context(subdrv_dev);
  134. DRM_DEBUG_KMS("%d\n", mode);
  135. mutex_lock(&ctx->lock);
  136. switch (mode) {
  137. case DRM_MODE_DPMS_ON:
  138. /* TODO. */
  139. break;
  140. case DRM_MODE_DPMS_STANDBY:
  141. case DRM_MODE_DPMS_SUSPEND:
  142. case DRM_MODE_DPMS_OFF:
  143. /* TODO. */
  144. break;
  145. default:
  146. DRM_DEBUG_KMS("unspecified mode %d\n", mode);
  147. break;
  148. }
  149. mutex_unlock(&ctx->lock);
  150. }
  151. static void vidi_apply(struct device *subdrv_dev)
  152. {
  153. struct vidi_context *ctx = get_vidi_context(subdrv_dev);
  154. struct exynos_drm_manager *mgr = ctx->subdrv.manager;
  155. struct exynos_drm_manager_ops *mgr_ops = mgr->ops;
  156. struct exynos_drm_overlay_ops *ovl_ops = mgr->overlay_ops;
  157. struct vidi_win_data *win_data;
  158. int i;
  159. for (i = 0; i < WINDOWS_NR; i++) {
  160. win_data = &ctx->win_data[i];
  161. if (win_data->enabled && (ovl_ops && ovl_ops->commit))
  162. ovl_ops->commit(subdrv_dev, i);
  163. }
  164. if (mgr_ops && mgr_ops->commit)
  165. mgr_ops->commit(subdrv_dev);
  166. }
  167. static void vidi_commit(struct device *dev)
  168. {
  169. struct vidi_context *ctx = get_vidi_context(dev);
  170. if (ctx->suspended)
  171. return;
  172. }
  173. static int vidi_enable_vblank(struct device *dev)
  174. {
  175. struct vidi_context *ctx = get_vidi_context(dev);
  176. if (ctx->suspended)
  177. return -EPERM;
  178. if (!test_and_set_bit(0, &ctx->irq_flags))
  179. ctx->vblank_on = true;
  180. ctx->direct_vblank = true;
  181. /*
  182. * in case of page flip request, vidi_finish_pageflip function
  183. * will not be called because direct_vblank is true and then
  184. * that function will be called by overlay_ops->commit callback
  185. */
  186. schedule_work(&ctx->work);
  187. return 0;
  188. }
  189. static void vidi_disable_vblank(struct device *dev)
  190. {
  191. struct vidi_context *ctx = get_vidi_context(dev);
  192. if (ctx->suspended)
  193. return;
  194. if (test_and_clear_bit(0, &ctx->irq_flags))
  195. ctx->vblank_on = false;
  196. }
  197. static struct exynos_drm_manager_ops vidi_manager_ops = {
  198. .dpms = vidi_dpms,
  199. .apply = vidi_apply,
  200. .commit = vidi_commit,
  201. .enable_vblank = vidi_enable_vblank,
  202. .disable_vblank = vidi_disable_vblank,
  203. };
  204. static void vidi_win_mode_set(struct device *dev,
  205. struct exynos_drm_overlay *overlay)
  206. {
  207. struct vidi_context *ctx = get_vidi_context(dev);
  208. struct vidi_win_data *win_data;
  209. int win;
  210. unsigned long offset;
  211. if (!overlay) {
  212. dev_err(dev, "overlay is NULL\n");
  213. return;
  214. }
  215. win = overlay->zpos;
  216. if (win == DEFAULT_ZPOS)
  217. win = ctx->default_win;
  218. if (win < 0 || win >= WINDOWS_NR)
  219. return;
  220. offset = overlay->fb_x * (overlay->bpp >> 3);
  221. offset += overlay->fb_y * overlay->pitch;
  222. DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
  223. win_data = &ctx->win_data[win];
  224. win_data->offset_x = overlay->crtc_x;
  225. win_data->offset_y = overlay->crtc_y;
  226. win_data->ovl_width = overlay->crtc_width;
  227. win_data->ovl_height = overlay->crtc_height;
  228. win_data->fb_width = overlay->fb_width;
  229. win_data->fb_height = overlay->fb_height;
  230. win_data->dma_addr = overlay->dma_addr[0] + offset;
  231. win_data->bpp = overlay->bpp;
  232. win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
  233. (overlay->bpp >> 3);
  234. win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
  235. /*
  236. * some parts of win_data should be transferred to user side
  237. * through specific ioctl.
  238. */
  239. DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
  240. win_data->offset_x, win_data->offset_y);
  241. DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
  242. win_data->ovl_width, win_data->ovl_height);
  243. DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
  244. DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
  245. overlay->fb_width, overlay->crtc_width);
  246. }
  247. static void vidi_win_commit(struct device *dev, int zpos)
  248. {
  249. struct vidi_context *ctx = get_vidi_context(dev);
  250. struct vidi_win_data *win_data;
  251. int win = zpos;
  252. if (ctx->suspended)
  253. return;
  254. if (win == DEFAULT_ZPOS)
  255. win = ctx->default_win;
  256. if (win < 0 || win >= WINDOWS_NR)
  257. return;
  258. win_data = &ctx->win_data[win];
  259. win_data->enabled = true;
  260. DRM_DEBUG_KMS("dma_addr = 0x%x\n", win_data->dma_addr);
  261. if (ctx->vblank_on)
  262. schedule_work(&ctx->work);
  263. }
  264. static void vidi_win_disable(struct device *dev, int zpos)
  265. {
  266. struct vidi_context *ctx = get_vidi_context(dev);
  267. struct vidi_win_data *win_data;
  268. int win = zpos;
  269. if (win == DEFAULT_ZPOS)
  270. win = ctx->default_win;
  271. if (win < 0 || win >= WINDOWS_NR)
  272. return;
  273. win_data = &ctx->win_data[win];
  274. win_data->enabled = false;
  275. /* TODO. */
  276. }
  277. static struct exynos_drm_overlay_ops vidi_overlay_ops = {
  278. .mode_set = vidi_win_mode_set,
  279. .commit = vidi_win_commit,
  280. .disable = vidi_win_disable,
  281. };
  282. static struct exynos_drm_manager vidi_manager = {
  283. .pipe = -1,
  284. .ops = &vidi_manager_ops,
  285. .overlay_ops = &vidi_overlay_ops,
  286. .display_ops = &vidi_display_ops,
  287. };
  288. static void vidi_fake_vblank_handler(struct work_struct *work)
  289. {
  290. struct vidi_context *ctx = container_of(work, struct vidi_context,
  291. work);
  292. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  293. struct exynos_drm_manager *manager = subdrv->manager;
  294. if (manager->pipe < 0)
  295. return;
  296. /* refresh rate is about 50Hz. */
  297. usleep_range(16000, 20000);
  298. mutex_lock(&ctx->lock);
  299. if (ctx->direct_vblank) {
  300. drm_handle_vblank(subdrv->drm_dev, manager->pipe);
  301. ctx->direct_vblank = false;
  302. mutex_unlock(&ctx->lock);
  303. return;
  304. }
  305. mutex_unlock(&ctx->lock);
  306. exynos_drm_crtc_finish_pageflip(subdrv->drm_dev, manager->pipe);
  307. }
  308. static int vidi_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
  309. {
  310. /*
  311. * enable drm irq mode.
  312. * - with irq_enabled = 1, we can use the vblank feature.
  313. *
  314. * P.S. note that we wouldn't use drm irq handler but
  315. * just specific driver own one instead because
  316. * drm framework supports only one irq handler.
  317. */
  318. drm_dev->irq_enabled = 1;
  319. /*
  320. * with vblank_disable_allowed = 1, vblank interrupt will be disabled
  321. * by drm timer once a current process gives up ownership of
  322. * vblank event.(after drm_vblank_put function is called)
  323. */
  324. drm_dev->vblank_disable_allowed = 1;
  325. return 0;
  326. }
  327. static void vidi_subdrv_remove(struct drm_device *drm_dev, struct device *dev)
  328. {
  329. /* TODO. */
  330. }
  331. static int vidi_power_on(struct vidi_context *ctx, bool enable)
  332. {
  333. struct exynos_drm_subdrv *subdrv = &ctx->subdrv;
  334. struct device *dev = subdrv->dev;
  335. if (enable) {
  336. ctx->suspended = false;
  337. /* if vblank was enabled status, enable it again. */
  338. if (test_and_clear_bit(0, &ctx->irq_flags))
  339. vidi_enable_vblank(dev);
  340. vidi_apply(dev);
  341. } else {
  342. ctx->suspended = true;
  343. }
  344. return 0;
  345. }
  346. static int vidi_show_connection(struct device *dev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. int rc;
  350. struct vidi_context *ctx = get_vidi_context(dev);
  351. mutex_lock(&ctx->lock);
  352. rc = sprintf(buf, "%d\n", ctx->connected);
  353. mutex_unlock(&ctx->lock);
  354. return rc;
  355. }
  356. static int vidi_store_connection(struct device *dev,
  357. struct device_attribute *attr,
  358. const char *buf, size_t len)
  359. {
  360. struct vidi_context *ctx = get_vidi_context(dev);
  361. int ret;
  362. ret = kstrtoint(buf, 0, &ctx->connected);
  363. if (ret)
  364. return ret;
  365. if (ctx->connected > 1)
  366. return -EINVAL;
  367. /* use fake edid data for test. */
  368. if (!ctx->raw_edid)
  369. ctx->raw_edid = (struct edid *)fake_edid_info;
  370. /* if raw_edid isn't same as fake data then it can't be tested. */
  371. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  372. DRM_DEBUG_KMS("edid data is not fake data.\n");
  373. return -EINVAL;
  374. }
  375. DRM_DEBUG_KMS("requested connection.\n");
  376. drm_helper_hpd_irq_event(ctx->subdrv.drm_dev);
  377. return len;
  378. }
  379. static DEVICE_ATTR(connection, 0644, vidi_show_connection,
  380. vidi_store_connection);
  381. int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
  382. struct drm_file *file_priv)
  383. {
  384. struct vidi_context *ctx = NULL;
  385. struct drm_encoder *encoder;
  386. struct exynos_drm_manager *manager;
  387. struct exynos_drm_display_ops *display_ops;
  388. struct drm_exynos_vidi_connection *vidi = data;
  389. int edid_len;
  390. if (!vidi) {
  391. DRM_DEBUG_KMS("user data for vidi is null.\n");
  392. return -EINVAL;
  393. }
  394. if (vidi->connection > 1) {
  395. DRM_DEBUG_KMS("connection should be 0 or 1.\n");
  396. return -EINVAL;
  397. }
  398. list_for_each_entry(encoder, &drm_dev->mode_config.encoder_list,
  399. head) {
  400. manager = exynos_drm_get_manager(encoder);
  401. display_ops = manager->display_ops;
  402. if (display_ops->type == EXYNOS_DISPLAY_TYPE_VIDI) {
  403. ctx = get_vidi_context(manager->dev);
  404. break;
  405. }
  406. }
  407. if (!ctx) {
  408. DRM_DEBUG_KMS("not found virtual device type encoder.\n");
  409. return -EINVAL;
  410. }
  411. if (ctx->connected == vidi->connection) {
  412. DRM_DEBUG_KMS("same connection request.\n");
  413. return -EINVAL;
  414. }
  415. if (vidi->connection) {
  416. struct edid *raw_edid = (struct edid *)(uint32_t)vidi->edid;
  417. if (!drm_edid_is_valid(raw_edid)) {
  418. DRM_DEBUG_KMS("edid data is invalid.\n");
  419. return -EINVAL;
  420. }
  421. edid_len = (1 + raw_edid->extensions) * EDID_LENGTH;
  422. ctx->raw_edid = kmemdup(raw_edid, edid_len, GFP_KERNEL);
  423. if (!ctx->raw_edid) {
  424. DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
  425. return -ENOMEM;
  426. }
  427. } else {
  428. /*
  429. * with connection = 0, free raw_edid
  430. * only if raw edid data isn't same as fake data.
  431. */
  432. if (ctx->raw_edid && ctx->raw_edid !=
  433. (struct edid *)fake_edid_info) {
  434. kfree(ctx->raw_edid);
  435. ctx->raw_edid = NULL;
  436. }
  437. }
  438. ctx->connected = vidi->connection;
  439. drm_helper_hpd_irq_event(ctx->subdrv.drm_dev);
  440. return 0;
  441. }
  442. static int vidi_probe(struct platform_device *pdev)
  443. {
  444. struct device *dev = &pdev->dev;
  445. struct vidi_context *ctx;
  446. struct exynos_drm_subdrv *subdrv;
  447. int ret;
  448. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  449. if (!ctx)
  450. return -ENOMEM;
  451. ctx->default_win = 0;
  452. INIT_WORK(&ctx->work, vidi_fake_vblank_handler);
  453. subdrv = &ctx->subdrv;
  454. subdrv->dev = dev;
  455. subdrv->manager = &vidi_manager;
  456. subdrv->probe = vidi_subdrv_probe;
  457. subdrv->remove = vidi_subdrv_remove;
  458. mutex_init(&ctx->lock);
  459. platform_set_drvdata(pdev, ctx);
  460. ret = device_create_file(dev, &dev_attr_connection);
  461. if (ret < 0)
  462. DRM_INFO("failed to create connection sysfs.\n");
  463. exynos_drm_subdrv_register(subdrv);
  464. return 0;
  465. }
  466. static int vidi_remove(struct platform_device *pdev)
  467. {
  468. struct vidi_context *ctx = platform_get_drvdata(pdev);
  469. exynos_drm_subdrv_unregister(&ctx->subdrv);
  470. if (ctx->raw_edid != (struct edid *)fake_edid_info) {
  471. kfree(ctx->raw_edid);
  472. ctx->raw_edid = NULL;
  473. }
  474. return 0;
  475. }
  476. #ifdef CONFIG_PM_SLEEP
  477. static int vidi_suspend(struct device *dev)
  478. {
  479. struct vidi_context *ctx = get_vidi_context(dev);
  480. return vidi_power_on(ctx, false);
  481. }
  482. static int vidi_resume(struct device *dev)
  483. {
  484. struct vidi_context *ctx = get_vidi_context(dev);
  485. return vidi_power_on(ctx, true);
  486. }
  487. #endif
  488. static const struct dev_pm_ops vidi_pm_ops = {
  489. SET_SYSTEM_SLEEP_PM_OPS(vidi_suspend, vidi_resume)
  490. };
  491. struct platform_driver vidi_driver = {
  492. .probe = vidi_probe,
  493. .remove = vidi_remove,
  494. .driver = {
  495. .name = "exynos-drm-vidi",
  496. .owner = THIS_MODULE,
  497. .pm = &vidi_pm_ops,
  498. },
  499. };