imx-drm-core.c 15 KB

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