imx-drm-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <video/imx-ipu-v3.h>
  29. #include "imx-drm.h"
  30. #define MAX_CRTC 4
  31. struct imx_drm_component {
  32. struct device_node *of_node;
  33. struct list_head list;
  34. };
  35. struct imx_drm_device {
  36. struct drm_device *drm;
  37. struct imx_drm_crtc *crtc[MAX_CRTC];
  38. unsigned int pipes;
  39. struct drm_fbdev_cma *fbhelper;
  40. };
  41. struct imx_drm_crtc {
  42. struct drm_crtc *crtc;
  43. struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs;
  44. };
  45. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  46. static int legacyfb_depth = 16;
  47. module_param(legacyfb_depth, int, 0444);
  48. #endif
  49. unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc)
  50. {
  51. return drm_crtc_index(crtc->crtc);
  52. }
  53. EXPORT_SYMBOL_GPL(imx_drm_crtc_id);
  54. static void imx_drm_driver_lastclose(struct drm_device *drm)
  55. {
  56. struct imx_drm_device *imxdrm = drm->dev_private;
  57. drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
  58. }
  59. static int imx_drm_driver_unload(struct drm_device *drm)
  60. {
  61. struct imx_drm_device *imxdrm = drm->dev_private;
  62. drm_kms_helper_poll_fini(drm);
  63. if (imxdrm->fbhelper)
  64. drm_fbdev_cma_fini(imxdrm->fbhelper);
  65. component_unbind_all(drm->dev, drm);
  66. drm_vblank_cleanup(drm);
  67. drm_mode_config_cleanup(drm);
  68. platform_set_drvdata(drm->platformdev, NULL);
  69. return 0;
  70. }
  71. static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc)
  72. {
  73. struct imx_drm_device *imxdrm = crtc->dev->dev_private;
  74. unsigned i;
  75. for (i = 0; i < MAX_CRTC; i++)
  76. if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc)
  77. return imxdrm->crtc[i];
  78. return NULL;
  79. }
  80. int imx_drm_set_bus_config(struct drm_encoder *encoder, u32 bus_format,
  81. int hsync_pin, int vsync_pin, u32 bus_flags)
  82. {
  83. struct imx_drm_crtc_helper_funcs *helper;
  84. struct imx_drm_crtc *imx_crtc;
  85. imx_crtc = imx_drm_find_crtc(encoder->crtc);
  86. if (!imx_crtc)
  87. return -EINVAL;
  88. helper = &imx_crtc->imx_drm_helper_funcs;
  89. if (helper->set_interface_pix_fmt)
  90. return helper->set_interface_pix_fmt(encoder->crtc,
  91. bus_format, hsync_pin, vsync_pin,
  92. bus_flags);
  93. return 0;
  94. }
  95. EXPORT_SYMBOL_GPL(imx_drm_set_bus_config);
  96. int imx_drm_set_bus_format(struct drm_encoder *encoder, u32 bus_format)
  97. {
  98. return imx_drm_set_bus_config(encoder, bus_format, 2, 3,
  99. DRM_BUS_FLAG_DE_HIGH |
  100. DRM_BUS_FLAG_PIXDATA_NEGEDGE);
  101. }
  102. EXPORT_SYMBOL_GPL(imx_drm_set_bus_format);
  103. int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
  104. {
  105. return drm_crtc_vblank_get(imx_drm_crtc->crtc);
  106. }
  107. EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
  108. void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
  109. {
  110. drm_crtc_vblank_put(imx_drm_crtc->crtc);
  111. }
  112. EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
  113. void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
  114. {
  115. drm_crtc_handle_vblank(imx_drm_crtc->crtc);
  116. }
  117. EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
  118. static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
  119. {
  120. struct imx_drm_device *imxdrm = drm->dev_private;
  121. struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
  122. int ret;
  123. if (!imx_drm_crtc)
  124. return -EINVAL;
  125. if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
  126. return -ENOSYS;
  127. ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
  128. imx_drm_crtc->crtc);
  129. return ret;
  130. }
  131. static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
  132. {
  133. struct imx_drm_device *imxdrm = drm->dev_private;
  134. struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe];
  135. if (!imx_drm_crtc)
  136. return;
  137. if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
  138. return;
  139. imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
  140. }
  141. static const struct file_operations imx_drm_driver_fops = {
  142. .owner = THIS_MODULE,
  143. .open = drm_open,
  144. .release = drm_release,
  145. .unlocked_ioctl = drm_ioctl,
  146. .mmap = drm_gem_cma_mmap,
  147. .poll = drm_poll,
  148. .read = drm_read,
  149. .llseek = noop_llseek,
  150. };
  151. void imx_drm_connector_destroy(struct drm_connector *connector)
  152. {
  153. drm_connector_unregister(connector);
  154. drm_connector_cleanup(connector);
  155. }
  156. EXPORT_SYMBOL_GPL(imx_drm_connector_destroy);
  157. void imx_drm_encoder_destroy(struct drm_encoder *encoder)
  158. {
  159. drm_encoder_cleanup(encoder);
  160. }
  161. EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy);
  162. static void imx_drm_output_poll_changed(struct drm_device *drm)
  163. {
  164. struct imx_drm_device *imxdrm = drm->dev_private;
  165. drm_fbdev_cma_hotplug_event(imxdrm->fbhelper);
  166. }
  167. static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = {
  168. .fb_create = drm_fb_cma_create,
  169. .output_poll_changed = imx_drm_output_poll_changed,
  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_init(drm);
  206. ret = drm_vblank_init(drm, MAX_CRTC);
  207. if (ret)
  208. goto err_kms;
  209. platform_set_drvdata(drm->platformdev, drm);
  210. /* Now try and bind all our sub-components */
  211. ret = component_bind_all(drm->dev, drm);
  212. if (ret)
  213. goto err_vblank;
  214. /*
  215. * All components are now added, we can publish the connector sysfs
  216. * entries to userspace. This will generate hotplug events and so
  217. * userspace will expect to be able to access DRM at this point.
  218. */
  219. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  220. ret = drm_connector_register(connector);
  221. if (ret) {
  222. dev_err(drm->dev,
  223. "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
  224. connector->base.id,
  225. connector->name, ret);
  226. goto err_unbind;
  227. }
  228. }
  229. /*
  230. * All components are now initialised, so setup the fb helper.
  231. * The fb helper takes copies of key hardware information, so the
  232. * crtcs/connectors/encoders must not change after this point.
  233. */
  234. #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION)
  235. if (legacyfb_depth != 16 && legacyfb_depth != 32) {
  236. dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n");
  237. legacyfb_depth = 16;
  238. }
  239. drm_helper_disable_unused_functions(drm);
  240. imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth,
  241. drm->mode_config.num_crtc, MAX_CRTC);
  242. if (IS_ERR(imxdrm->fbhelper)) {
  243. ret = PTR_ERR(imxdrm->fbhelper);
  244. imxdrm->fbhelper = NULL;
  245. goto err_unbind;
  246. }
  247. #endif
  248. drm_kms_helper_poll_init(drm);
  249. return 0;
  250. err_unbind:
  251. component_unbind_all(drm->dev, drm);
  252. err_vblank:
  253. drm_vblank_cleanup(drm);
  254. err_kms:
  255. drm_mode_config_cleanup(drm);
  256. return ret;
  257. }
  258. /*
  259. * imx_drm_add_crtc - add a new crtc
  260. */
  261. int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc,
  262. struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane,
  263. const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
  264. struct device_node *port)
  265. {
  266. struct imx_drm_device *imxdrm = drm->dev_private;
  267. struct imx_drm_crtc *imx_drm_crtc;
  268. /*
  269. * The vblank arrays are dimensioned by MAX_CRTC - we can't
  270. * pass IDs greater than this to those functions.
  271. */
  272. if (imxdrm->pipes >= MAX_CRTC)
  273. return -EINVAL;
  274. if (imxdrm->drm->open_count)
  275. return -EBUSY;
  276. imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
  277. if (!imx_drm_crtc)
  278. return -ENOMEM;
  279. imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
  280. imx_drm_crtc->crtc = crtc;
  281. crtc->port = port;
  282. imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc;
  283. *new_crtc = imx_drm_crtc;
  284. drm_crtc_helper_add(crtc,
  285. imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
  286. drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  287. imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL);
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
  291. /*
  292. * imx_drm_remove_crtc - remove a crtc
  293. */
  294. int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
  295. {
  296. struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private;
  297. unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc);
  298. drm_crtc_cleanup(imx_drm_crtc->crtc);
  299. imxdrm->crtc[pipe] = NULL;
  300. kfree(imx_drm_crtc);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
  304. int imx_drm_encoder_parse_of(struct drm_device *drm,
  305. struct drm_encoder *encoder, struct device_node *np)
  306. {
  307. uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np);
  308. /*
  309. * If we failed to find the CRTC(s) which this encoder is
  310. * supposed to be connected to, it's because the CRTC has
  311. * not been registered yet. Defer probing, and hope that
  312. * the required CRTC is added later.
  313. */
  314. if (crtc_mask == 0)
  315. return -EPROBE_DEFER;
  316. encoder->possible_crtcs = crtc_mask;
  317. /* FIXME: this is the mask of outputs which can clone this output. */
  318. encoder->possible_clones = ~0;
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of);
  322. static const struct drm_ioctl_desc imx_drm_ioctls[] = {
  323. /* none so far */
  324. };
  325. static struct drm_driver imx_drm_driver = {
  326. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
  327. .load = imx_drm_driver_load,
  328. .unload = imx_drm_driver_unload,
  329. .lastclose = imx_drm_driver_lastclose,
  330. .gem_free_object_unlocked = drm_gem_cma_free_object,
  331. .gem_vm_ops = &drm_gem_cma_vm_ops,
  332. .dumb_create = drm_gem_cma_dumb_create,
  333. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  334. .dumb_destroy = drm_gem_dumb_destroy,
  335. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  336. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  337. .gem_prime_import = drm_gem_prime_import,
  338. .gem_prime_export = drm_gem_prime_export,
  339. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  340. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  341. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  342. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  343. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  344. .get_vblank_counter = drm_vblank_no_hw_counter,
  345. .enable_vblank = imx_drm_enable_vblank,
  346. .disable_vblank = imx_drm_disable_vblank,
  347. .ioctls = imx_drm_ioctls,
  348. .num_ioctls = ARRAY_SIZE(imx_drm_ioctls),
  349. .fops = &imx_drm_driver_fops,
  350. .name = "imx-drm",
  351. .desc = "i.MX DRM graphics",
  352. .date = "20120507",
  353. .major = 1,
  354. .minor = 0,
  355. .patchlevel = 0,
  356. };
  357. static int compare_of(struct device *dev, void *data)
  358. {
  359. struct device_node *np = data;
  360. /* Special case for DI, dev->of_node may not be set yet */
  361. if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) {
  362. struct ipu_client_platformdata *pdata = dev->platform_data;
  363. return pdata->of_node == np;
  364. }
  365. /* Special case for LDB, one device for two channels */
  366. if (of_node_cmp(np->name, "lvds-channel") == 0) {
  367. np = of_get_parent(np);
  368. of_node_put(np);
  369. }
  370. return dev->of_node == np;
  371. }
  372. static int imx_drm_bind(struct device *dev)
  373. {
  374. return drm_platform_init(&imx_drm_driver, to_platform_device(dev));
  375. }
  376. static void imx_drm_unbind(struct device *dev)
  377. {
  378. drm_put_dev(dev_get_drvdata(dev));
  379. }
  380. static const struct component_master_ops imx_drm_ops = {
  381. .bind = imx_drm_bind,
  382. .unbind = imx_drm_unbind,
  383. };
  384. static int imx_drm_platform_probe(struct platform_device *pdev)
  385. {
  386. int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops);
  387. if (!ret)
  388. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  389. return ret;
  390. }
  391. static int imx_drm_platform_remove(struct platform_device *pdev)
  392. {
  393. component_master_del(&pdev->dev, &imx_drm_ops);
  394. return 0;
  395. }
  396. #ifdef CONFIG_PM_SLEEP
  397. static int imx_drm_suspend(struct device *dev)
  398. {
  399. struct drm_device *drm_dev = dev_get_drvdata(dev);
  400. /* The drm_dev is NULL before .load hook is called */
  401. if (drm_dev == NULL)
  402. return 0;
  403. drm_kms_helper_poll_disable(drm_dev);
  404. return 0;
  405. }
  406. static int imx_drm_resume(struct device *dev)
  407. {
  408. struct drm_device *drm_dev = dev_get_drvdata(dev);
  409. if (drm_dev == NULL)
  410. return 0;
  411. drm_helper_resume_force_mode(drm_dev);
  412. drm_kms_helper_poll_enable(drm_dev);
  413. return 0;
  414. }
  415. #endif
  416. static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume);
  417. static const struct of_device_id imx_drm_dt_ids[] = {
  418. { .compatible = "fsl,imx-display-subsystem", },
  419. { /* sentinel */ },
  420. };
  421. MODULE_DEVICE_TABLE(of, imx_drm_dt_ids);
  422. static struct platform_driver imx_drm_pdrv = {
  423. .probe = imx_drm_platform_probe,
  424. .remove = imx_drm_platform_remove,
  425. .driver = {
  426. .name = "imx-drm",
  427. .pm = &imx_drm_pm_ops,
  428. .of_match_table = imx_drm_dt_ids,
  429. },
  430. };
  431. module_platform_driver(imx_drm_pdrv);
  432. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  433. MODULE_DESCRIPTION("i.MX drm driver core");
  434. MODULE_LICENSE("GPL");