imx-drm-core.c 13 KB

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