imx-drm-core.c 14 KB

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