imx-drm-core.c 13 KB

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