imx-drm-core.c 11 KB

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