imx-drm-core.c 14 KB

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