pl111_drv.c 9.4 KB

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