imx-drm-core.c 11 KB

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