imx-drm-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Freescale i.MX drm driver
  3. *
  4. * Copyright (C) 2011 Sascha Hauer, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/component.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reservation.h>
  22. #include <drm/drmP.h>
  23. #include <drm/drm_atomic.h>
  24. #include <drm/drm_atomic_helper.h>
  25. #include <drm/drm_fb_helper.h>
  26. #include <drm/drm_crtc_helper.h>
  27. #include <drm/drm_gem_cma_helper.h>
  28. #include <drm/drm_fb_cma_helper.h>
  29. #include <drm/drm_plane_helper.h>
  30. #include <drm/drm_of.h>
  31. #include <video/imx-ipu-v3.h>
  32. #include "imx-drm.h"
  33. #define MAX_CRTC 4
  34. struct imx_drm_component {
  35. struct device_node *of_node;
  36. struct list_head list;
  37. };
  38. struct imx_drm_device {
  39. struct drm_device *drm;
  40. struct imx_drm_crtc *crtc[MAX_CRTC];
  41. unsigned int pipes;
  42. struct drm_fbdev_cma *fbhelper;
  43. struct drm_atomic_state *state;
  44. };
  45. struct imx_drm_crtc {
  46. struct drm_crtc *crtc;
  47. struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
  48. };
  49. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  50. static int legacyfb_depth = 16;
  51. module_param(legacyfb_depth, int, 0444);
  52. #endif
  53. static void imx_drm_driver_lastclose(struct drm_device *drm)
  54. {
  55. struct imx_drm_device *imxdrm = drm->dev_private;
  56. drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
  57. }
  58. static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
  59. {
  60. struct imx_drm_device *imxdrm = drm->dev_private;
  61. struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
  62. int ret;
  63. if (!imx_drm_crtc)
  64. return -EINVAL;
  65. if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
  66. return -ENOSYS;
  67. ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
  68. imx_drm_crtc->crtc);
  69. return ret;
  70. }
  71. static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
  72. {
  73. struct imx_drm_device *imxdrm = drm->dev_private;
  74. struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
  75. if (!imx_drm_crtc)
  76. return;
  77. if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
  78. return;
  79. imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
  80. }
  81. static const struct file_operations imx_drm_driver_fops = {
  82. .owner = THIS_MODULE,
  83. .open = drm_open,
  84. .release = drm_release,
  85. .unlocked_ioctl = drm_ioctl,
  86. .mmap = drm_gem_cma_mmap,
  87. .poll = drm_poll,
  88. .read = drm_read,
  89. .llseek = noop_llseek,
  90. };
  91. void imx_drm_connector_destroy(struct drm_connector *connector)
  92. {
  93. drm_connector_unregister(connector);
  94. drm_connector_cleanup(connector);
  95. }
  96. EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
  97. void imx_drm_encoder_destroy(struct drm_encoder *encoder)
  98. {
  99. drm_encoder_cleanup(encoder);
  100. }
  101. EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
  102. static void imx_drm_output_poll_changed(struct drm_device *drm)
  103. {
  104. struct imx_drm_device *imxdrm = drm->dev_private;
  105. drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
  106. }
  107. static int imx_drm_atomic_check(struct drm_device *dev,
  108. struct drm_atomic_state *state)
  109. {
  110. int ret;
  111. ret = drm_atomic_helper_check_modeset(dev, state);
  112. if (ret)
  113. return ret;
  114. ret = drm_atomic_helper_check_planes(dev, state);
  115. if (ret)
  116. return ret;
  117. /*
  118. * Check modeset again in case crtc_state->mode_changed is
  119. * updated in plane's ->atomic_check callback.
  120. */
  121. ret = drm_atomic_helper_check_modeset(dev, state);
  122. if (ret)
  123. return ret;
  124. return ret;
  125. }
  126. static int imx_drm_atomic_commit(struct drm_device *dev,
  127. struct drm_atomic_state *state,
  128. bool nonblock)
  129. {
  130. struct drm_plane_state *plane_state;
  131. struct drm_plane *plane;
  132. struct dma_buf *dma_buf;
  133. int i;
  134. /*
  135. * If the plane fb has an dma-buf attached, fish out the exclusive
  136. * fence for the atomic helper to wait on.
  137. */
  138. for_each_plane_in_state(state, plane, plane_state, i) {
  139. if ((plane->state->fb != plane_state->fb) && plane_state->fb) {
  140. dma_buf = drm_fb_cma_get_gem_obj(plane_state->fb,
  141. 0)->base.dma_buf;
  142. if (!dma_buf)
  143. continue;
  144. plane_state->fence =
  145. reservation_object_get_excl_rcu(dma_buf->resv);
  146. }
  147. }
  148. return drm_atomic_helper_commit(dev, state, nonblock);
  149. }
  150. static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
  151. .fb_create = drm_fb_cma_create,
  152. .output_poll_changed = imx_drm_output_poll_changed,
  153. .atomic_check = imx_drm_atomic_check,
  154. .atomic_commit = imx_drm_atomic_commit,
  155. };
  156. static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
  157. {
  158. struct drm_device *dev = state->dev;
  159. drm_atomic_helper_commit_modeset_disables(dev, state);
  160. drm_atomic_helper_commit_planes(dev, state,
  161. DRM_PLANE_COMMIT_ACTIVE_ONLY |
  162. DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
  163. drm_atomic_helper_commit_modeset_enables(dev, state);
  164. drm_atomic_helper_commit_hw_done(state);
  165. drm_atomic_helper_wait_for_vblanks(dev, state);
  166. drm_atomic_helper_cleanup_planes(dev, state);
  167. }
  168. static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
  169. .atomic_commit_tail = imx_drm_atomic_commit_tail,
  170. };
  171. /*
  172. * imx_drm_add_crtc - add a new crtc
  173. */
  174. int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
  175. struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
  176. const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
  177. struct device_node *port)
  178. {
  179. struct imx_drm_device *imxdrm = drm->dev_private;
  180. struct imx_drm_crtc *imx_drm_crtc;
  181. /*
  182. * The vblank arrays are dimensioned by MAX_CRTC - we can't
  183. * pass IDs greater than this to those functions.
  184. */
  185. if (imxdrm->pipes >= MAX_CRTC)
  186. return -EINVAL;
  187. if (imxdrm->drm->open_count)
  188. return -EBUSY;
  189. imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
  190. if (!imx_drm_crtc)
  191. return -ENOMEM;
  192. imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
  193. imx_drm_crtc->crtc = crtc;
  194. crtc->port = port;
  195. imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
  196. *new_crtc = imx_drm_crtc;
  197. drm_crtc_helper_add(crtc,
  198. imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
  199. drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  200. imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
  204. /*
  205. * imx_drm_remove_crtc - remove a crtc
  206. */
  207. int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
  208. {
  209. struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
  210. unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
  211. drm_crtc_cleanup(imx_drm_crtc->crtc);
  212. imxdrm->crtc[pipe] = NULL;
  213. kfree(imx_drm_crtc);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
  217. int imx_drm_encoder_parse_of(struct drm_device *drm,
  218. struct drm_encoder *encoder, struct device_node *np)
  219. {
  220. uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
  221. /*
  222. * If we failed to find the CRTC(s) which this encoder is
  223. * supposed to be connected to, it's because the CRTC has
  224. * not been registered yet. Defer probing, and hope that
  225. * the required CRTC is added later.
  226. */
  227. if (crtc_mask == 0)
  228. return -EPROBE_DEFER;
  229. encoder->possible_crtcs = crtc_mask;
  230. /* FIXME: this is the mask of outputs which can clone this output. */
  231. encoder->possible_clones = ~0;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
  235. static const struct drm_ioctl_desc imx_drm_ioctls[] = {
  236. /* none so far */
  237. };
  238. static struct drm_driver imx_drm_driver = {
  239. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  240. DRIVER_ATOMIC,
  241. .lastclose = imx_drm_driver_lastclose,
  242. .gem_free_object_unlocked = drm_gem_cma_free_object,
  243. .gem_vm_ops = &drm_gem_cma_vm_ops,
  244. .dumb_create = drm_gem_cma_dumb_create,
  245. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  246. .dumb_destroy = drm_gem_dumb_destroy,
  247. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  248. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  249. .gem_prime_import = drm_gem_prime_import,
  250. .gem_prime_export = drm_gem_prime_export,
  251. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  252. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  253. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  254. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  255. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  256. .get_vblank_counter = drm_vblank_no_hw_counter,
  257. .enable_vblank = imx_drm_enable_vblank,
  258. .disable_vblank = imx_drm_disable_vblank,
  259. .ioctls = imx_drm_ioctls,
  260. .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
  261. .fops = &imx_drm_driver_fops,
  262. .name = "imx-drm",
  263. .desc = "i.MX DRM graphics",
  264. .date = "20120507",
  265. .major = 1,
  266. .minor = 0,
  267. .patchlevel = 0,
  268. };
  269. static int compare_of(struct device *dev, void *data)
  270. {
  271. struct device_node *np = data;
  272. /* Special case for DI, dev->of_node may not be set yet */
  273. if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
  274. struct ipu_client_platformdata *pdata = dev->platform_data;
  275. return pdata->of_node == np;
  276. }
  277. /* Special case for LDB, one device for two channels */
  278. if (of_node_cmp(np->name, "lvds-channel") == 0) {
  279. np = of_get_parent(np);
  280. of_node_put(np);
  281. }
  282. return dev->of_node == np;
  283. }
  284. static int imx_drm_bind(struct device *dev)
  285. {
  286. struct drm_device *drm;
  287. struct imx_drm_device *imxdrm;
  288. int ret;
  289. drm = drm_dev_alloc(&imx_drm_driver, dev);
  290. if (IS_ERR(drm))
  291. return PTR_ERR(drm);
  292. imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
  293. if (!imxdrm) {
  294. ret = -ENOMEM;
  295. goto err_unref;
  296. }
  297. imxdrm->drm = drm;
  298. drm->dev_private = imxdrm;
  299. /*
  300. * enable drm irq mode.
  301. * - with irq_enabled = true, we can use the vblank feature.
  302. *
  303. * P.S. note that we wouldn't use drm irq handler but
  304. * just specific driver own one instead because
  305. * drm framework supports only one irq handler and
  306. * drivers can well take care of their interrupts
  307. */
  308. drm->irq_enabled = true;
  309. /*
  310. * set max width and height as default value(4096x4096).
  311. * this value would be used to check framebuffer size limitation
  312. * at drm_mode_addfb().
  313. */
  314. drm->mode_config.min_width = 64;
  315. drm->mode_config.min_height = 64;
  316. drm->mode_config.max_width = 4096;
  317. drm->mode_config.max_height = 4096;
  318. drm->mode_config.funcs = &imx_drm_mode_config_funcs;
  319. drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
  320. drm_mode_config_init(drm);
  321. ret = drm_vblank_init(drm, MAX_CRTC);
  322. if (ret)
  323. goto err_kms;
  324. dev_set_drvdata(dev, drm);
  325. /* Now try and bind all our sub-components */
  326. ret = component_bind_all(dev, drm);
  327. if (ret)
  328. goto err_vblank;
  329. drm_mode_config_reset(drm);
  330. /*
  331. * All components are now initialised, so setup the fb helper.
  332. * The fb helper takes copies of key hardware information, so the
  333. * crtcs/connectors/encoders must not change after this point.
  334. */
  335. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  336. if (legacyfb_depth != 16 && legacyfb_depth != 32) {
  337. dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
  338. legacyfb_depth = 16;
  339. }
  340. imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
  341. drm->mode_config.num_crtc, MAX_CRTC);
  342. if (IS_ERR(imxdrm->fbhelper)) {
  343. ret = PTR_ERR(imxdrm->fbhelper);
  344. imxdrm->fbhelper = NULL;
  345. goto err_unbind;
  346. }
  347. #endif
  348. drm_kms_helper_poll_init(drm);
  349. ret = drm_dev_register(drm, 0);
  350. if (ret)
  351. goto err_fbhelper;
  352. return 0;
  353. err_fbhelper:
  354. drm_kms_helper_poll_fini(drm);
  355. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  356. if (imxdrm->fbhelper)
  357. drm_fbdev_cma_fini(imxdrm->fbhelper);
  358. err_unbind:
  359. #endif
  360. component_unbind_all(drm->dev, drm);
  361. err_vblank:
  362. drm_vblank_cleanup(drm);
  363. err_kms:
  364. drm_mode_config_cleanup(drm);
  365. err_unref:
  366. drm_dev_unref(drm);
  367. return ret;
  368. }
  369. static void imx_drm_unbind(struct device *dev)
  370. {
  371. struct drm_device *drm = dev_get_drvdata(dev);
  372. struct imx_drm_device *imxdrm = drm->dev_private;
  373. drm_dev_unregister(drm);
  374. drm_kms_helper_poll_fini(drm);
  375. if (imxdrm->fbhelper)
  376. drm_fbdev_cma_fini(imxdrm->fbhelper);
  377. drm_mode_config_cleanup(drm);
  378. component_unbind_all(drm->dev, drm);
  379. dev_set_drvdata(dev, NULL);
  380. drm_dev_unref(drm);
  381. }
  382. static const struct component_master_ops imx_drm_ops = {
  383. .bind = imx_drm_bind,
  384. .unbind = imx_drm_unbind,
  385. };
  386. static int imx_drm_platform_probe(struct platform_device *pdev)
  387. {
  388. int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
  389. if (!ret)
  390. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  391. return ret;
  392. }
  393. static int imx_drm_platform_remove(struct platform_device *pdev)
  394. {
  395. component_master_del(&pdev->dev, &imx_drm_ops);
  396. return 0;
  397. }
  398. #ifdef CONFIG_PM_SLEEP
  399. static int imx_drm_suspend(struct device *dev)
  400. {
  401. struct drm_device *drm_dev = dev_get_drvdata(dev);
  402. struct imx_drm_device *imxdrm;
  403. /* The drm_dev is NULL before .load hook is called */
  404. if (drm_dev == NULL)
  405. return 0;
  406. drm_kms_helper_poll_disable(drm_dev);
  407. imxdrm = drm_dev->dev_private;
  408. imxdrm->state = drm_atomic_helper_suspend(drm_dev);
  409. if (IS_ERR(imxdrm->state)) {
  410. drm_kms_helper_poll_enable(drm_dev);
  411. return PTR_ERR(imxdrm->state);
  412. }
  413. return 0;
  414. }
  415. static int imx_drm_resume(struct device *dev)
  416. {
  417. struct drm_device *drm_dev = dev_get_drvdata(dev);
  418. struct imx_drm_device *imx_drm;
  419. if (drm_dev == NULL)
  420. return 0;
  421. imx_drm = drm_dev->dev_private;
  422. drm_atomic_helper_resume(drm_dev, imx_drm->state);
  423. drm_kms_helper_poll_enable(drm_dev);
  424. return 0;
  425. }
  426. #endif
  427. static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
  428. static const struct of_device_id imx_drm_dt_ids[] = {
  429. { .compatible = "fsl,imx-display-subsystem", },
  430. { /* sentinel */ },
  431. };
  432. MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
  433. static struct platform_driver imx_drm_pdrv = {
  434. .probe = imx_drm_platform_probe,
  435. .remove = imx_drm_platform_remove,
  436. .driver = {
  437. .name = "imx-drm",
  438. .pm = &imx_drm_pm_ops,
  439. .of_match_table = imx_drm_dt_ids,
  440. },
  441. };
  442. module_platform_driver(imx_drm_pdrv);
  443. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  444. MODULE_DESCRIPTION("i.MX drm driver core");
  445. MODULE_LICENSE("GPL");