imx-drm-core.c 14 KB

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