imx-drm-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <drm/drmP.h>
  22. #include <drm/drm_atomic.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_fb_helper.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_gem_cma_helper.h>
  27. #include <drm/drm_fb_cma_helper.h>
  28. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_of.h>
  30. #include <video/imx-ipu-v3.h>
  31. #include "imx-drm.h"
  32. #include "ipuv3-plane.h"
  33. #define MAX_CRTC 4
  34. struct imx_drm_device {
  35. struct drm_device *drm;
  36. unsigned int pipes;
  37. struct drm_fbdev_cma *fbhelper;
  38. struct drm_atomic_state *state;
  39. };
  40. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  41. static int legacyfb_depth = 16;
  42. module_param(legacyfb_depth, int, 0444);
  43. #endif
  44. static void imx_drm_driver_lastclose(struct drm_device *drm)
  45. {
  46. struct imx_drm_device *imxdrm = drm->dev_private;
  47. drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
  48. }
  49. DEFINE_DRM_GEM_CMA_FOPS(imx_drm_driver_fops);
  50. void imx_drm_connector_destroy(struct drm_connector *connector)
  51. {
  52. drm_connector_unregister(connector);
  53. drm_connector_cleanup(connector);
  54. }
  55. EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
  56. void imx_drm_encoder_destroy(struct drm_encoder *encoder)
  57. {
  58. drm_encoder_cleanup(encoder);
  59. }
  60. EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
  61. static void imx_drm_output_poll_changed(struct drm_device *drm)
  62. {
  63. struct imx_drm_device *imxdrm = drm->dev_private;
  64. drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
  65. }
  66. static int imx_drm_atomic_check(struct drm_device *dev,
  67. struct drm_atomic_state *state)
  68. {
  69. int ret;
  70. ret = drm_atomic_helper_check_modeset(dev, state);
  71. if (ret)
  72. return ret;
  73. ret = drm_atomic_helper_check_planes(dev, state);
  74. if (ret)
  75. return ret;
  76. /*
  77. * Check modeset again in case crtc_state->mode_changed is
  78. * updated in plane's ->atomic_check callback.
  79. */
  80. ret = drm_atomic_helper_check_modeset(dev, state);
  81. if (ret)
  82. return ret;
  83. /* Assign PRG/PRE channels and check if all constrains are satisfied. */
  84. ret = ipu_planes_assign_pre(dev, state);
  85. if (ret)
  86. return ret;
  87. return ret;
  88. }
  89. static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
  90. .fb_create = drm_fb_cma_create,
  91. .output_poll_changed = imx_drm_output_poll_changed,
  92. .atomic_check = imx_drm_atomic_check,
  93. .atomic_commit = drm_atomic_helper_commit,
  94. };
  95. static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state)
  96. {
  97. struct drm_device *dev = state->dev;
  98. struct drm_plane *plane;
  99. struct drm_plane_state *old_plane_state, *new_plane_state;
  100. bool plane_disabling = false;
  101. int i;
  102. drm_atomic_helper_commit_modeset_disables(dev, state);
  103. drm_atomic_helper_commit_planes(dev, state,
  104. DRM_PLANE_COMMIT_ACTIVE_ONLY |
  105. DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET);
  106. drm_atomic_helper_commit_modeset_enables(dev, state);
  107. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  108. if (drm_atomic_plane_disabling(old_plane_state, new_plane_state))
  109. plane_disabling = true;
  110. }
  111. if (plane_disabling) {
  112. drm_atomic_helper_wait_for_vblanks(dev, state);
  113. for_each_old_plane_in_state(state, plane, old_plane_state, i)
  114. ipu_plane_disable_deferred(plane);
  115. }
  116. drm_atomic_helper_commit_hw_done(state);
  117. }
  118. static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = {
  119. .atomic_commit_tail = imx_drm_atomic_commit_tail,
  120. };
  121. int imx_drm_encoder_parse_of(struct drm_device *drm,
  122. struct drm_encoder *encoder, struct device_node *np)
  123. {
  124. uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
  125. /*
  126. * If we failed to find the CRTC(s) which this encoder is
  127. * supposed to be connected to, it's because the CRTC has
  128. * not been registered yet. Defer probing, and hope that
  129. * the required CRTC is added later.
  130. */
  131. if (crtc_mask == 0)
  132. return -EPROBE_DEFER;
  133. encoder->possible_crtcs = crtc_mask;
  134. /* FIXME: this is the mask of outputs which can clone this output. */
  135. encoder->possible_clones = ~0;
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
  139. static const struct drm_ioctl_desc imx_drm_ioctls[] = {
  140. /* none so far */
  141. };
  142. static struct drm_driver imx_drm_driver = {
  143. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  144. DRIVER_ATOMIC,
  145. .lastclose = imx_drm_driver_lastclose,
  146. .gem_free_object_unlocked = drm_gem_cma_free_object,
  147. .gem_vm_ops = &drm_gem_cma_vm_ops,
  148. .dumb_create = drm_gem_cma_dumb_create,
  149. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  150. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  151. .gem_prime_import = drm_gem_prime_import,
  152. .gem_prime_export = drm_gem_prime_export,
  153. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  154. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  155. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  156. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  157. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  158. .ioctls = imx_drm_ioctls,
  159. .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
  160. .fops = &imx_drm_driver_fops,
  161. .name = "imx-drm",
  162. .desc = "i.MX DRM graphics",
  163. .date = "20120507",
  164. .major = 1,
  165. .minor = 0,
  166. .patchlevel = 0,
  167. };
  168. static int compare_of(struct device *dev, void *data)
  169. {
  170. struct device_node *np = data;
  171. /* Special case for DI, dev->of_node may not be set yet */
  172. if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
  173. struct ipu_client_platformdata *pdata = dev->platform_data;
  174. return pdata->of_node == np;
  175. }
  176. /* Special case for LDB, one device for two channels */
  177. if (of_node_cmp(np->name, "lvds-channel") == 0) {
  178. np = of_get_parent(np);
  179. of_node_put(np);
  180. }
  181. return dev->of_node == np;
  182. }
  183. static int imx_drm_bind(struct device *dev)
  184. {
  185. struct drm_device *drm;
  186. struct imx_drm_device *imxdrm;
  187. int ret;
  188. drm = drm_dev_alloc(&imx_drm_driver, dev);
  189. if (IS_ERR(drm))
  190. return PTR_ERR(drm);
  191. imxdrm = devm_kzalloc(dev, sizeof(*imxdrm), GFP_KERNEL);
  192. if (!imxdrm) {
  193. ret = -ENOMEM;
  194. goto err_unref;
  195. }
  196. imxdrm->drm = drm;
  197. drm->dev_private = imxdrm;
  198. /*
  199. * enable drm irq mode.
  200. * - with irq_enabled = true, we can use the vblank feature.
  201. *
  202. * P.S. note that we wouldn't use drm irq handler but
  203. * just specific driver own one instead because
  204. * drm framework supports only one irq handler and
  205. * drivers can well take care of their interrupts
  206. */
  207. drm->irq_enabled = true;
  208. /*
  209. * set max width and height as default value(4096x4096).
  210. * this value would be used to check framebuffer size limitation
  211. * at drm_mode_addfb().
  212. */
  213. drm->mode_config.min_width = 1;
  214. drm->mode_config.min_height = 1;
  215. drm->mode_config.max_width = 4096;
  216. drm->mode_config.max_height = 4096;
  217. drm->mode_config.funcs = &imx_drm_mode_config_funcs;
  218. drm->mode_config.helper_private = &imx_drm_mode_config_helpers;
  219. drm_mode_config_init(drm);
  220. ret = drm_vblank_init(drm, MAX_CRTC);
  221. if (ret)
  222. goto err_kms;
  223. dev_set_drvdata(dev, drm);
  224. /* Now try and bind all our sub-components */
  225. ret = component_bind_all(dev, drm);
  226. if (ret)
  227. goto err_kms;
  228. drm_mode_config_reset(drm);
  229. /*
  230. * All components are now initialised, so setup the fb helper.
  231. * The fb helper takes copies of key hardware information, so the
  232. * crtcs/connectors/encoders must not change after this point.
  233. */
  234. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  235. if (legacyfb_depth != 16 && legacyfb_depth != 32) {
  236. dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
  237. legacyfb_depth = 16;
  238. }
  239. imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, MAX_CRTC);
  240. if (IS_ERR(imxdrm->fbhelper)) {
  241. ret = PTR_ERR(imxdrm->fbhelper);
  242. imxdrm->fbhelper = NULL;
  243. goto err_unbind;
  244. }
  245. #endif
  246. drm_kms_helper_poll_init(drm);
  247. ret = drm_dev_register(drm, 0);
  248. if (ret)
  249. goto err_fbhelper;
  250. return 0;
  251. err_fbhelper:
  252. drm_kms_helper_poll_fini(drm);
  253. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  254. if (imxdrm->fbhelper)
  255. drm_fbdev_cma_fini(imxdrm->fbhelper);
  256. err_unbind:
  257. #endif
  258. component_unbind_all(drm->dev, drm);
  259. err_kms:
  260. drm_mode_config_cleanup(drm);
  261. err_unref:
  262. drm_dev_unref(drm);
  263. return ret;
  264. }
  265. static void imx_drm_unbind(struct device *dev)
  266. {
  267. struct drm_device *drm = dev_get_drvdata(dev);
  268. struct imx_drm_device *imxdrm = drm->dev_private;
  269. drm_dev_unregister(drm);
  270. drm_kms_helper_poll_fini(drm);
  271. if (imxdrm->fbhelper)
  272. drm_fbdev_cma_fini(imxdrm->fbhelper);
  273. drm_mode_config_cleanup(drm);
  274. component_unbind_all(drm->dev, drm);
  275. dev_set_drvdata(dev, NULL);
  276. drm_dev_unref(drm);
  277. }
  278. static const struct component_master_ops imx_drm_ops = {
  279. .bind = imx_drm_bind,
  280. .unbind = imx_drm_unbind,
  281. };
  282. static int imx_drm_platform_probe(struct platform_device *pdev)
  283. {
  284. int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
  285. if (!ret)
  286. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  287. return ret;
  288. }
  289. static int imx_drm_platform_remove(struct platform_device *pdev)
  290. {
  291. component_master_del(&pdev->dev, &imx_drm_ops);
  292. return 0;
  293. }
  294. #ifdef CONFIG_PM_SLEEP
  295. static int imx_drm_suspend(struct device *dev)
  296. {
  297. struct drm_device *drm_dev = dev_get_drvdata(dev);
  298. struct imx_drm_device *imxdrm;
  299. /* The drm_dev is NULL before .load hook is called */
  300. if (drm_dev == NULL)
  301. return 0;
  302. drm_kms_helper_poll_disable(drm_dev);
  303. imxdrm = drm_dev->dev_private;
  304. imxdrm->state = drm_atomic_helper_suspend(drm_dev);
  305. if (IS_ERR(imxdrm->state)) {
  306. drm_kms_helper_poll_enable(drm_dev);
  307. return PTR_ERR(imxdrm->state);
  308. }
  309. return 0;
  310. }
  311. static int imx_drm_resume(struct device *dev)
  312. {
  313. struct drm_device *drm_dev = dev_get_drvdata(dev);
  314. struct imx_drm_device *imx_drm;
  315. if (drm_dev == NULL)
  316. return 0;
  317. imx_drm = drm_dev->dev_private;
  318. drm_atomic_helper_resume(drm_dev, imx_drm->state);
  319. drm_kms_helper_poll_enable(drm_dev);
  320. return 0;
  321. }
  322. #endif
  323. static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
  324. static const struct of_device_id imx_drm_dt_ids[] = {
  325. { .compatible = "fsl,imx-display-subsystem", },
  326. { /* sentinel */ },
  327. };
  328. MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
  329. static struct platform_driver imx_drm_pdrv = {
  330. .probe = imx_drm_platform_probe,
  331. .remove = imx_drm_platform_remove,
  332. .driver = {
  333. .name = "imx-drm",
  334. .pm = &imx_drm_pm_ops,
  335. .of_match_table = imx_drm_dt_ids,
  336. },
  337. };
  338. static struct platform_driver * const drivers[] = {
  339. &imx_drm_pdrv,
  340. &ipu_drm_driver,
  341. };
  342. static int __init imx_drm_init(void)
  343. {
  344. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  345. }
  346. module_init(imx_drm_init);
  347. static void __exit imx_drm_exit(void)
  348. {
  349. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  350. }
  351. module_exit(imx_drm_exit);
  352. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  353. MODULE_DESCRIPTION("i.MX drm driver core");
  354. MODULE_LICENSE("GPL");