imx-drm-core.c 15 KB

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