imx-drm-core.c 15 KB

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