imx-drm-core.c 11 KB

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