pl111_drv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * - Use the "max-memory-bandwidth" DT property to filter the
  44. * supported formats.
  45. *
  46. * - Read back hardware state at boot to skip reprogramming the
  47. * hardware when doing a no-op modeset.
  48. *
  49. * - Use the CLKSEL bit to support switching between the two external
  50. * clock parents.
  51. */
  52. #include <linux/amba/bus.h>
  53. #include <linux/amba/clcd-regs.h>
  54. #include <linux/version.h>
  55. #include <linux/shmem_fs.h>
  56. #include <linux/dma-buf.h>
  57. #include <linux/module.h>
  58. #include <linux/slab.h>
  59. #include <drm/drmP.h>
  60. #include <drm/drm_atomic_helper.h>
  61. #include <drm/drm_crtc_helper.h>
  62. #include <drm/drm_gem_cma_helper.h>
  63. #include <drm/drm_gem_framebuffer_helper.h>
  64. #include <drm/drm_fb_helper.h>
  65. #include <drm/drm_fb_cma_helper.h>
  66. #include <drm/drm_of.h>
  67. #include <drm/drm_bridge.h>
  68. #include <drm/drm_panel.h>
  69. #include "pl111_drm.h"
  70. #include "pl111_versatile.h"
  71. #define DRIVER_DESC "DRM module for PL111"
  72. static const struct drm_mode_config_funcs mode_config_funcs = {
  73. .fb_create = drm_gem_fb_create,
  74. .atomic_check = drm_atomic_helper_check,
  75. .atomic_commit = drm_atomic_helper_commit,
  76. };
  77. static int pl111_modeset_init(struct drm_device *dev)
  78. {
  79. struct drm_mode_config *mode_config;
  80. struct pl111_drm_dev_private *priv = dev->dev_private;
  81. struct drm_panel *panel;
  82. struct drm_bridge *bridge;
  83. int ret = 0;
  84. drm_mode_config_init(dev);
  85. mode_config = &dev->mode_config;
  86. mode_config->funcs = &mode_config_funcs;
  87. mode_config->min_width = 1;
  88. mode_config->max_width = 1024;
  89. mode_config->min_height = 1;
  90. mode_config->max_height = 768;
  91. ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
  92. 0, 0, &panel, &bridge);
  93. if (ret && ret != -ENODEV)
  94. return ret;
  95. if (panel) {
  96. bridge = drm_panel_bridge_add(panel,
  97. DRM_MODE_CONNECTOR_Unknown);
  98. if (IS_ERR(bridge)) {
  99. ret = PTR_ERR(bridge);
  100. goto out_config;
  101. }
  102. } else if (bridge) {
  103. dev_info(dev->dev, "Using non-panel bridge\n");
  104. } else {
  105. dev_err(dev->dev, "No bridge, exiting\n");
  106. return -ENODEV;
  107. }
  108. priv->bridge = bridge;
  109. if (panel) {
  110. priv->panel = panel;
  111. priv->connector = panel->connector;
  112. }
  113. ret = pl111_display_init(dev);
  114. if (ret != 0) {
  115. dev_err(dev->dev, "Failed to init display\n");
  116. goto out_bridge;
  117. }
  118. ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
  119. bridge);
  120. if (ret)
  121. return ret;
  122. ret = drm_vblank_init(dev, 1);
  123. if (ret != 0) {
  124. dev_err(dev->dev, "Failed to init vblank\n");
  125. goto out_bridge;
  126. }
  127. drm_mode_config_reset(dev);
  128. drm_fb_cma_fbdev_init(dev, 32, 0);
  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 struct drm_driver pl111_drm_driver = {
  141. .driver_features =
  142. DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  143. .lastclose = drm_fb_helper_lastclose,
  144. .ioctls = NULL,
  145. .fops = &drm_fops,
  146. .name = "pl111",
  147. .desc = DRIVER_DESC,
  148. .date = "20170317",
  149. .major = 1,
  150. .minor = 0,
  151. .patchlevel = 0,
  152. .dumb_create = drm_gem_cma_dumb_create,
  153. .gem_free_object_unlocked = drm_gem_cma_free_object,
  154. .gem_vm_ops = &drm_gem_cma_vm_ops,
  155. .enable_vblank = pl111_enable_vblank,
  156. .disable_vblank = pl111_disable_vblank,
  157. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  158. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  159. .gem_prime_import = drm_gem_prime_import,
  160. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  161. .gem_prime_export = drm_gem_prime_export,
  162. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  163. #if defined(CONFIG_DEBUG_FS)
  164. .debugfs_init = pl111_debugfs_init,
  165. #endif
  166. };
  167. static int pl111_amba_probe(struct amba_device *amba_dev,
  168. const struct amba_id *id)
  169. {
  170. struct device *dev = &amba_dev->dev;
  171. struct pl111_drm_dev_private *priv;
  172. struct pl111_variant_data *variant = id->data;
  173. struct drm_device *drm;
  174. int ret;
  175. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  176. if (!priv)
  177. return -ENOMEM;
  178. drm = drm_dev_alloc(&pl111_drm_driver, dev);
  179. if (IS_ERR(drm))
  180. return PTR_ERR(drm);
  181. amba_set_drvdata(amba_dev, drm);
  182. priv->drm = drm;
  183. drm->dev_private = priv;
  184. priv->variant = variant;
  185. /*
  186. * The PL110 and PL111 variants have two registers
  187. * swapped: interrupt enable and control. For this reason
  188. * we use offsets that we can change per variant.
  189. */
  190. if (variant->is_pl110) {
  191. /*
  192. * The ARM Versatile boards are even more special:
  193. * their PrimeCell ID say they are PL110 but the
  194. * control and interrupt enable registers are anyway
  195. * swapped to the PL111 order so they are not following
  196. * the PL110 datasheet.
  197. */
  198. if (of_machine_is_compatible("arm,versatile-ab") ||
  199. of_machine_is_compatible("arm,versatile-pb")) {
  200. priv->ienb = CLCD_PL111_IENB;
  201. priv->ctrl = CLCD_PL111_CNTL;
  202. } else {
  203. priv->ienb = CLCD_PL110_IENB;
  204. priv->ctrl = CLCD_PL110_CNTL;
  205. }
  206. } else {
  207. priv->ienb = CLCD_PL111_IENB;
  208. priv->ctrl = CLCD_PL111_CNTL;
  209. }
  210. priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
  211. if (IS_ERR(priv->regs)) {
  212. dev_err(dev, "%s failed mmio\n", __func__);
  213. return PTR_ERR(priv->regs);
  214. }
  215. /* turn off interrupts before requesting the irq */
  216. writel(0, priv->regs + priv->ienb);
  217. ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
  218. variant->name, priv);
  219. if (ret != 0) {
  220. dev_err(dev, "%s failed irq %d\n", __func__, ret);
  221. return ret;
  222. }
  223. ret = pl111_versatile_init(dev, priv);
  224. if (ret)
  225. goto dev_unref;
  226. ret = pl111_modeset_init(drm);
  227. if (ret != 0)
  228. goto dev_unref;
  229. ret = drm_dev_register(drm, 0);
  230. if (ret < 0)
  231. goto dev_unref;
  232. return 0;
  233. dev_unref:
  234. drm_dev_unref(drm);
  235. return ret;
  236. }
  237. static int pl111_amba_remove(struct amba_device *amba_dev)
  238. {
  239. struct drm_device *drm = amba_get_drvdata(amba_dev);
  240. struct pl111_drm_dev_private *priv = drm->dev_private;
  241. drm_dev_unregister(drm);
  242. drm_fb_cma_fbdev_fini(drm);
  243. if (priv->panel)
  244. drm_panel_bridge_remove(priv->bridge);
  245. drm_mode_config_cleanup(drm);
  246. drm_dev_unref(drm);
  247. return 0;
  248. }
  249. /*
  250. * This variant exist in early versions like the ARM Integrator
  251. * and this version lacks the 565 and 444 pixel formats.
  252. */
  253. static const u32 pl110_pixel_formats[] = {
  254. DRM_FORMAT_ABGR8888,
  255. DRM_FORMAT_XBGR8888,
  256. DRM_FORMAT_ARGB8888,
  257. DRM_FORMAT_XRGB8888,
  258. DRM_FORMAT_ABGR1555,
  259. DRM_FORMAT_XBGR1555,
  260. DRM_FORMAT_ARGB1555,
  261. DRM_FORMAT_XRGB1555,
  262. };
  263. static const struct pl111_variant_data pl110_variant = {
  264. .name = "PL110",
  265. .is_pl110 = true,
  266. .formats = pl110_pixel_formats,
  267. .nformats = ARRAY_SIZE(pl110_pixel_formats),
  268. };
  269. /* RealView, Versatile Express etc use this modern variant */
  270. static const u32 pl111_pixel_formats[] = {
  271. DRM_FORMAT_ABGR8888,
  272. DRM_FORMAT_XBGR8888,
  273. DRM_FORMAT_ARGB8888,
  274. DRM_FORMAT_XRGB8888,
  275. DRM_FORMAT_BGR565,
  276. DRM_FORMAT_RGB565,
  277. DRM_FORMAT_ABGR1555,
  278. DRM_FORMAT_XBGR1555,
  279. DRM_FORMAT_ARGB1555,
  280. DRM_FORMAT_XRGB1555,
  281. DRM_FORMAT_ABGR4444,
  282. DRM_FORMAT_XBGR4444,
  283. DRM_FORMAT_ARGB4444,
  284. DRM_FORMAT_XRGB4444,
  285. };
  286. static const struct pl111_variant_data pl111_variant = {
  287. .name = "PL111",
  288. .formats = pl111_pixel_formats,
  289. .nformats = ARRAY_SIZE(pl111_pixel_formats),
  290. };
  291. static const struct amba_id pl111_id_table[] = {
  292. {
  293. .id = 0x00041110,
  294. .mask = 0x000fffff,
  295. .data = (void*)&pl110_variant,
  296. },
  297. {
  298. .id = 0x00041111,
  299. .mask = 0x000fffff,
  300. .data = (void*)&pl111_variant,
  301. },
  302. {0, 0},
  303. };
  304. static struct amba_driver pl111_amba_driver __maybe_unused = {
  305. .drv = {
  306. .name = "drm-clcd-pl111",
  307. },
  308. .probe = pl111_amba_probe,
  309. .remove = pl111_amba_remove,
  310. .id_table = pl111_id_table,
  311. };
  312. #ifdef CONFIG_ARM_AMBA
  313. module_amba_driver(pl111_amba_driver);
  314. #endif
  315. MODULE_DESCRIPTION(DRIVER_DESC);
  316. MODULE_AUTHOR("ARM Ltd.");
  317. MODULE_LICENSE("GPL");