imx-drm-core.c 9.7 KB

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