pl111_drv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
  3. *
  4. * Parts of this file were based on sources as follows:
  5. *
  6. * Copyright (c) 2006-2008 Intel Corporation
  7. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  8. * Copyright (C) 2011 Texas Instruments
  9. *
  10. * This program is free software and is provided to you under the terms of the
  11. * GNU General Public License version 2 as published by the Free Software
  12. * Foundation, and any use by you of this program is subject to the terms of
  13. * such GNU licence.
  14. *
  15. */
  16. /**
  17. * DOC: ARM PrimeCell PL111 CLCD Driver
  18. *
  19. * The PL111 is a simple LCD controller that can support TFT and STN
  20. * displays. This driver exposes a standard KMS interface for them.
  21. *
  22. * This driver uses the same Device Tree binding as the fbdev CLCD
  23. * driver. While the fbdev driver supports panels that may be
  24. * connected to the CLCD internally to the CLCD driver, in DRM the
  25. * panels get split out to drivers/gpu/drm/panels/. This means that,
  26. * in converting from using fbdev to using DRM, you also need to write
  27. * a panel driver (which may be as simple as an entry in
  28. * panel-simple.c).
  29. *
  30. * The driver currently doesn't expose the cursor. The DRM API for
  31. * cursors requires support for 64x64 ARGB8888 cursor images, while
  32. * the hardware can only support 64x64 monochrome with masking
  33. * cursors. While one could imagine trying to hack something together
  34. * to look at the ARGB8888 and program reasonable in monochrome, we
  35. * just don't expose the cursor at all instead, and leave cursor
  36. * support to the X11 software cursor layer.
  37. *
  38. * TODO:
  39. *
  40. * - Fix race between setting plane base address and getting IRQ for
  41. * vsync firing the pageflip completion.
  42. *
  43. * - Expose the correct set of formats we can support based on the
  44. * "arm,pl11x,tft-r0g0b0-pads" DT property.
  45. *
  46. * - Use the "max-memory-bandwidth" DT property to filter the
  47. * supported formats.
  48. *
  49. * - Read back hardware state at boot to skip reprogramming the
  50. * hardware when doing a no-op modeset.
  51. *
  52. * - Use the CLKSEL bit to support switching between the two external
  53. * clock parents.
  54. */
  55. #include <linux/amba/bus.h>
  56. #include <linux/amba/clcd-regs.h>
  57. #include <linux/version.h>
  58. #include <linux/shmem_fs.h>
  59. #include <linux/dma-buf.h>
  60. #include <linux/module.h>
  61. #include <linux/slab.h>
  62. #include <drm/drmP.h>
  63. #include <drm/drm_atomic_helper.h>
  64. #include <drm/drm_crtc_helper.h>
  65. #include <drm/drm_gem_cma_helper.h>
  66. #include <drm/drm_gem_framebuffer_helper.h>
  67. #include <drm/drm_fb_cma_helper.h>
  68. #include <drm/drm_of.h>
  69. #include <drm/drm_bridge.h>
  70. #include <drm/drm_panel.h>
  71. #include "pl111_drm.h"
  72. #define DRIVER_DESC "DRM module for PL111"
  73. static const struct drm_mode_config_funcs mode_config_funcs = {
  74. .fb_create = drm_gem_fb_create,
  75. .atomic_check = drm_atomic_helper_check,
  76. .atomic_commit = drm_atomic_helper_commit,
  77. };
  78. static int pl111_modeset_init(struct drm_device *dev)
  79. {
  80. struct drm_mode_config *mode_config;
  81. struct pl111_drm_dev_private *priv = dev->dev_private;
  82. struct drm_panel *panel;
  83. struct drm_bridge *bridge;
  84. int ret = 0;
  85. drm_mode_config_init(dev);
  86. mode_config = &dev->mode_config;
  87. mode_config->funcs = &mode_config_funcs;
  88. mode_config->min_width = 1;
  89. mode_config->max_width = 1024;
  90. mode_config->min_height = 1;
  91. mode_config->max_height = 768;
  92. ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
  93. 0, 0, &panel, &bridge);
  94. if (ret && ret != -ENODEV)
  95. return ret;
  96. if (panel) {
  97. bridge = drm_panel_bridge_add(panel,
  98. DRM_MODE_CONNECTOR_Unknown);
  99. if (IS_ERR(bridge)) {
  100. ret = PTR_ERR(bridge);
  101. goto out_config;
  102. }
  103. /*
  104. * TODO: when we are using a different bridge than a panel
  105. * (such as a dumb VGA connector) we need to devise a different
  106. * method to get the connector out of the bridge.
  107. */
  108. }
  109. ret = pl111_display_init(dev);
  110. if (ret != 0) {
  111. dev_err(dev->dev, "Failed to init display\n");
  112. goto out_bridge;
  113. }
  114. ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
  115. bridge);
  116. if (ret)
  117. return ret;
  118. priv->bridge = bridge;
  119. priv->panel = panel;
  120. priv->connector = panel->connector;
  121. ret = drm_vblank_init(dev, 1);
  122. if (ret != 0) {
  123. dev_err(dev->dev, "Failed to init vblank\n");
  124. goto out_bridge;
  125. }
  126. drm_mode_config_reset(dev);
  127. priv->fbdev = drm_fbdev_cma_init(dev, 32,
  128. dev->mode_config.num_connector);
  129. drm_kms_helper_poll_init(dev);
  130. goto finish;
  131. out_bridge:
  132. if (panel)
  133. drm_panel_bridge_remove(bridge);
  134. out_config:
  135. drm_mode_config_cleanup(dev);
  136. finish:
  137. return ret;
  138. }
  139. DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
  140. static void pl111_lastclose(struct drm_device *dev)
  141. {
  142. struct pl111_drm_dev_private *priv = dev->dev_private;
  143. drm_fbdev_cma_restore_mode(priv->fbdev);
  144. }
  145. static struct drm_driver pl111_drm_driver = {
  146. .driver_features =
  147. DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  148. .lastclose = pl111_lastclose,
  149. .ioctls = NULL,
  150. .fops = &drm_fops,
  151. .name = "pl111",
  152. .desc = DRIVER_DESC,
  153. .date = "20170317",
  154. .major = 1,
  155. .minor = 0,
  156. .patchlevel = 0,
  157. .dumb_create = drm_gem_cma_dumb_create,
  158. .gem_free_object_unlocked = drm_gem_cma_free_object,
  159. .gem_vm_ops = &drm_gem_cma_vm_ops,
  160. .enable_vblank = pl111_enable_vblank,
  161. .disable_vblank = pl111_disable_vblank,
  162. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  163. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  164. .gem_prime_import = drm_gem_prime_import,
  165. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  166. .gem_prime_export = drm_gem_prime_export,
  167. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  168. #if defined(CONFIG_DEBUG_FS)
  169. .debugfs_init = pl111_debugfs_init,
  170. #endif
  171. };
  172. static int pl111_amba_probe(struct amba_device *amba_dev,
  173. const struct amba_id *id)
  174. {
  175. struct device *dev = &amba_dev->dev;
  176. struct pl111_drm_dev_private *priv;
  177. struct drm_device *drm;
  178. int ret;
  179. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  180. if (!priv)
  181. return -ENOMEM;
  182. drm = drm_dev_alloc(&pl111_drm_driver, dev);
  183. if (IS_ERR(drm))
  184. return PTR_ERR(drm);
  185. amba_set_drvdata(amba_dev, drm);
  186. priv->drm = drm;
  187. drm->dev_private = priv;
  188. priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
  189. if (IS_ERR(priv->regs)) {
  190. dev_err(dev, "%s failed mmio\n", __func__);
  191. return PTR_ERR(priv->regs);
  192. }
  193. /* turn off interrupts before requesting the irq */
  194. writel(0, priv->regs + CLCD_PL111_IENB);
  195. ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
  196. "pl111", priv);
  197. if (ret != 0) {
  198. dev_err(dev, "%s failed irq %d\n", __func__, ret);
  199. return ret;
  200. }
  201. ret = pl111_modeset_init(drm);
  202. if (ret != 0)
  203. goto dev_unref;
  204. ret = drm_dev_register(drm, 0);
  205. if (ret < 0)
  206. goto dev_unref;
  207. return 0;
  208. dev_unref:
  209. drm_dev_unref(drm);
  210. return ret;
  211. }
  212. static int pl111_amba_remove(struct amba_device *amba_dev)
  213. {
  214. struct drm_device *drm = amba_get_drvdata(amba_dev);
  215. struct pl111_drm_dev_private *priv = drm->dev_private;
  216. drm_dev_unregister(drm);
  217. if (priv->fbdev)
  218. drm_fbdev_cma_fini(priv->fbdev);
  219. if (priv->panel)
  220. drm_panel_bridge_remove(priv->bridge);
  221. drm_mode_config_cleanup(drm);
  222. drm_dev_unref(drm);
  223. return 0;
  224. }
  225. static struct amba_id pl111_id_table[] = {
  226. {
  227. .id = 0x00041111,
  228. .mask = 0x000fffff,
  229. },
  230. {0, 0},
  231. };
  232. static struct amba_driver pl111_amba_driver __maybe_unused = {
  233. .drv = {
  234. .name = "drm-clcd-pl111",
  235. },
  236. .probe = pl111_amba_probe,
  237. .remove = pl111_amba_remove,
  238. .id_table = pl111_id_table,
  239. };
  240. #ifdef CONFIG_ARM_AMBA
  241. module_amba_driver(pl111_amba_driver);
  242. #endif
  243. MODULE_DESCRIPTION(DRIVER_DESC);
  244. MODULE_AUTHOR("ARM Ltd.");
  245. MODULE_LICENSE("GPL");