pl111_drv.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "pl111_drm.h"
  69. #define DRIVER_DESC "DRM module for PL111"
  70. static const struct drm_mode_config_funcs mode_config_funcs = {
  71. .fb_create = drm_gem_fb_create,
  72. .atomic_check = drm_atomic_helper_check,
  73. .atomic_commit = drm_atomic_helper_commit,
  74. };
  75. static int pl111_modeset_init(struct drm_device *dev)
  76. {
  77. struct drm_mode_config *mode_config;
  78. struct pl111_drm_dev_private *priv = dev->dev_private;
  79. int ret = 0;
  80. drm_mode_config_init(dev);
  81. mode_config = &dev->mode_config;
  82. mode_config->funcs = &mode_config_funcs;
  83. mode_config->min_width = 1;
  84. mode_config->max_width = 1024;
  85. mode_config->min_height = 1;
  86. mode_config->max_height = 768;
  87. ret = pl111_connector_init(dev);
  88. if (ret) {
  89. dev_err(dev->dev, "Failed to create pl111_drm_connector\n");
  90. goto out_config;
  91. }
  92. /* Don't actually attach if we didn't find a drm_panel
  93. * attached to us. This will allow a kernel to include both
  94. * the fbdev pl111 driver and this one, and choose between
  95. * them based on which subsystem has support for the panel.
  96. */
  97. if (!priv->connector.panel) {
  98. dev_info(dev->dev,
  99. "Disabling due to lack of DRM panel device.\n");
  100. ret = -ENODEV;
  101. goto out_config;
  102. }
  103. ret = pl111_display_init(dev);
  104. if (ret != 0) {
  105. dev_err(dev->dev, "Failed to init display\n");
  106. goto out_config;
  107. }
  108. ret = drm_vblank_init(dev, 1);
  109. if (ret != 0) {
  110. dev_err(dev->dev, "Failed to init vblank\n");
  111. goto out_config;
  112. }
  113. drm_mode_config_reset(dev);
  114. priv->fbdev = drm_fbdev_cma_init(dev, 32,
  115. dev->mode_config.num_connector);
  116. drm_kms_helper_poll_init(dev);
  117. goto finish;
  118. out_config:
  119. drm_mode_config_cleanup(dev);
  120. finish:
  121. return ret;
  122. }
  123. DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
  124. static void pl111_lastclose(struct drm_device *dev)
  125. {
  126. struct pl111_drm_dev_private *priv = dev->dev_private;
  127. drm_fbdev_cma_restore_mode(priv->fbdev);
  128. }
  129. static struct drm_driver pl111_drm_driver = {
  130. .driver_features =
  131. DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  132. .lastclose = pl111_lastclose,
  133. .ioctls = NULL,
  134. .fops = &drm_fops,
  135. .name = "pl111",
  136. .desc = DRIVER_DESC,
  137. .date = "20170317",
  138. .major = 1,
  139. .minor = 0,
  140. .patchlevel = 0,
  141. .dumb_create = drm_gem_cma_dumb_create,
  142. .gem_free_object_unlocked = drm_gem_cma_free_object,
  143. .gem_vm_ops = &drm_gem_cma_vm_ops,
  144. .enable_vblank = pl111_enable_vblank,
  145. .disable_vblank = pl111_disable_vblank,
  146. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  147. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  148. .gem_prime_import = drm_gem_prime_import,
  149. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  150. .gem_prime_export = drm_gem_prime_export,
  151. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  152. #if defined(CONFIG_DEBUG_FS)
  153. .debugfs_init = pl111_debugfs_init,
  154. #endif
  155. };
  156. static int pl111_amba_probe(struct amba_device *amba_dev,
  157. const struct amba_id *id)
  158. {
  159. struct device *dev = &amba_dev->dev;
  160. struct pl111_drm_dev_private *priv;
  161. struct drm_device *drm;
  162. int ret;
  163. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  164. if (!priv)
  165. return -ENOMEM;
  166. drm = drm_dev_alloc(&pl111_drm_driver, dev);
  167. if (IS_ERR(drm))
  168. return PTR_ERR(drm);
  169. amba_set_drvdata(amba_dev, drm);
  170. priv->drm = drm;
  171. drm->dev_private = priv;
  172. priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
  173. if (IS_ERR(priv->regs)) {
  174. dev_err(dev, "%s failed mmio\n", __func__);
  175. return PTR_ERR(priv->regs);
  176. }
  177. /* turn off interrupts before requesting the irq */
  178. writel(0, priv->regs + CLCD_PL111_IENB);
  179. ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
  180. "pl111", priv);
  181. if (ret != 0) {
  182. dev_err(dev, "%s failed irq %d\n", __func__, ret);
  183. return ret;
  184. }
  185. ret = pl111_modeset_init(drm);
  186. if (ret != 0)
  187. goto dev_unref;
  188. ret = drm_dev_register(drm, 0);
  189. if (ret < 0)
  190. goto dev_unref;
  191. return 0;
  192. dev_unref:
  193. drm_dev_unref(drm);
  194. return ret;
  195. }
  196. static int pl111_amba_remove(struct amba_device *amba_dev)
  197. {
  198. struct drm_device *drm = amba_get_drvdata(amba_dev);
  199. struct pl111_drm_dev_private *priv = drm->dev_private;
  200. drm_dev_unregister(drm);
  201. if (priv->fbdev)
  202. drm_fbdev_cma_fini(priv->fbdev);
  203. drm_mode_config_cleanup(drm);
  204. drm_dev_unref(drm);
  205. return 0;
  206. }
  207. static struct amba_id pl111_id_table[] = {
  208. {
  209. .id = 0x00041111,
  210. .mask = 0x000fffff,
  211. },
  212. {0, 0},
  213. };
  214. static struct amba_driver pl111_amba_driver __maybe_unused = {
  215. .drv = {
  216. .name = "drm-clcd-pl111",
  217. },
  218. .probe = pl111_amba_probe,
  219. .remove = pl111_amba_remove,
  220. .id_table = pl111_id_table,
  221. };
  222. #ifdef CONFIG_ARM_AMBA
  223. module_amba_driver(pl111_amba_driver);
  224. #endif
  225. MODULE_DESCRIPTION(DRIVER_DESC);
  226. MODULE_AUTHOR("ARM Ltd.");
  227. MODULE_LICENSE("GPL");