pl111_drv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <linux/of.h>
  60. #include <linux/of_graph.h>
  61. #include <drm/drmP.h>
  62. #include <drm/drm_atomic_helper.h>
  63. #include <drm/drm_crtc_helper.h>
  64. #include <drm/drm_gem_cma_helper.h>
  65. #include <drm/drm_gem_framebuffer_helper.h>
  66. #include <drm/drm_fb_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. #include "pl111_versatile.h"
  73. #define DRIVER_DESC "DRM module for PL111"
  74. static const struct drm_mode_config_funcs mode_config_funcs = {
  75. .fb_create = drm_gem_fb_create,
  76. .atomic_check = drm_atomic_helper_check,
  77. .atomic_commit = drm_atomic_helper_commit,
  78. };
  79. static int pl111_modeset_init(struct drm_device *dev)
  80. {
  81. struct drm_mode_config *mode_config;
  82. struct pl111_drm_dev_private *priv = dev->dev_private;
  83. struct device_node *np = dev->dev->of_node;
  84. struct device_node *remote;
  85. struct drm_panel *panel = NULL;
  86. struct drm_bridge *bridge = NULL;
  87. bool defer = false;
  88. int ret = 0;
  89. int i;
  90. drm_mode_config_init(dev);
  91. mode_config = &dev->mode_config;
  92. mode_config->funcs = &mode_config_funcs;
  93. mode_config->min_width = 1;
  94. mode_config->max_width = 1024;
  95. mode_config->min_height = 1;
  96. mode_config->max_height = 768;
  97. i = 0;
  98. for_each_endpoint_of_node(np, remote) {
  99. struct drm_panel *tmp_panel;
  100. struct drm_bridge *tmp_bridge;
  101. dev_dbg(dev->dev, "checking endpoint %d\n", i);
  102. ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
  103. 0, i,
  104. &tmp_panel,
  105. &tmp_bridge);
  106. if (ret) {
  107. if (ret == -EPROBE_DEFER) {
  108. /*
  109. * Something deferred, but that is often just
  110. * another way of saying -ENODEV, but let's
  111. * cast a vote for later deferral.
  112. */
  113. defer = true;
  114. } else if (ret != -ENODEV) {
  115. /* Continue, maybe something else is working */
  116. dev_err(dev->dev,
  117. "endpoint %d returns %d\n", i, ret);
  118. }
  119. }
  120. if (tmp_panel) {
  121. dev_info(dev->dev,
  122. "found panel on endpoint %d\n", i);
  123. panel = tmp_panel;
  124. }
  125. if (tmp_bridge) {
  126. dev_info(dev->dev,
  127. "found bridge on endpoint %d\n", i);
  128. bridge = tmp_bridge;
  129. }
  130. i++;
  131. }
  132. /*
  133. * If we can't find neither panel nor bridge on any of the
  134. * endpoints, and any of them retured -EPROBE_DEFER, then
  135. * let's defer this driver too.
  136. */
  137. if ((!panel && !bridge) && defer)
  138. return -EPROBE_DEFER;
  139. if (panel) {
  140. bridge = drm_panel_bridge_add(panel,
  141. DRM_MODE_CONNECTOR_Unknown);
  142. if (IS_ERR(bridge)) {
  143. ret = PTR_ERR(bridge);
  144. goto out_config;
  145. }
  146. } else if (bridge) {
  147. dev_info(dev->dev, "Using non-panel bridge\n");
  148. } else {
  149. dev_err(dev->dev, "No bridge, exiting\n");
  150. return -ENODEV;
  151. }
  152. priv->bridge = bridge;
  153. if (panel) {
  154. priv->panel = panel;
  155. priv->connector = panel->connector;
  156. }
  157. ret = pl111_display_init(dev);
  158. if (ret != 0) {
  159. dev_err(dev->dev, "Failed to init display\n");
  160. goto out_bridge;
  161. }
  162. ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
  163. bridge);
  164. if (ret)
  165. return ret;
  166. if (!priv->variant->broken_vblank) {
  167. ret = drm_vblank_init(dev, 1);
  168. if (ret != 0) {
  169. dev_err(dev->dev, "Failed to init vblank\n");
  170. goto out_bridge;
  171. }
  172. }
  173. drm_mode_config_reset(dev);
  174. drm_fb_cma_fbdev_init(dev, priv->variant->fb_bpp, 0);
  175. drm_kms_helper_poll_init(dev);
  176. goto finish;
  177. out_bridge:
  178. if (panel)
  179. drm_panel_bridge_remove(bridge);
  180. out_config:
  181. drm_mode_config_cleanup(dev);
  182. finish:
  183. return ret;
  184. }
  185. DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
  186. static struct drm_driver pl111_drm_driver = {
  187. .driver_features =
  188. DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  189. .lastclose = drm_fb_helper_lastclose,
  190. .ioctls = NULL,
  191. .fops = &drm_fops,
  192. .name = "pl111",
  193. .desc = DRIVER_DESC,
  194. .date = "20170317",
  195. .major = 1,
  196. .minor = 0,
  197. .patchlevel = 0,
  198. .dumb_create = drm_gem_cma_dumb_create,
  199. .gem_free_object_unlocked = drm_gem_cma_free_object,
  200. .gem_vm_ops = &drm_gem_cma_vm_ops,
  201. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  202. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  203. .gem_prime_import = drm_gem_prime_import,
  204. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  205. .gem_prime_export = drm_gem_prime_export,
  206. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  207. #if defined(CONFIG_DEBUG_FS)
  208. .debugfs_init = pl111_debugfs_init,
  209. #endif
  210. };
  211. static int pl111_amba_probe(struct amba_device *amba_dev,
  212. const struct amba_id *id)
  213. {
  214. struct device *dev = &amba_dev->dev;
  215. struct pl111_drm_dev_private *priv;
  216. const struct pl111_variant_data *variant = id->data;
  217. struct drm_device *drm;
  218. int ret;
  219. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  220. if (!priv)
  221. return -ENOMEM;
  222. drm = drm_dev_alloc(&pl111_drm_driver, dev);
  223. if (IS_ERR(drm))
  224. return PTR_ERR(drm);
  225. amba_set_drvdata(amba_dev, drm);
  226. priv->drm = drm;
  227. drm->dev_private = priv;
  228. priv->variant = variant;
  229. if (of_property_read_u32(dev->of_node, "max-memory-bandwidth",
  230. &priv->memory_bw)) {
  231. dev_info(dev, "no max memory bandwidth specified, assume unlimited\n");
  232. priv->memory_bw = 0;
  233. }
  234. /* The two variants swap this register */
  235. if (variant->is_pl110) {
  236. priv->ienb = CLCD_PL110_IENB;
  237. priv->ctrl = CLCD_PL110_CNTL;
  238. } else {
  239. priv->ienb = CLCD_PL111_IENB;
  240. priv->ctrl = CLCD_PL111_CNTL;
  241. }
  242. priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
  243. if (IS_ERR(priv->regs)) {
  244. dev_err(dev, "%s failed mmio\n", __func__);
  245. return PTR_ERR(priv->regs);
  246. }
  247. /* This may override some variant settings */
  248. ret = pl111_versatile_init(dev, priv);
  249. if (ret)
  250. goto dev_unref;
  251. /* turn off interrupts before requesting the irq */
  252. writel(0, priv->regs + priv->ienb);
  253. ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
  254. variant->name, priv);
  255. if (ret != 0) {
  256. dev_err(dev, "%s failed irq %d\n", __func__, ret);
  257. return ret;
  258. }
  259. ret = pl111_modeset_init(drm);
  260. if (ret != 0)
  261. goto dev_unref;
  262. ret = drm_dev_register(drm, 0);
  263. if (ret < 0)
  264. goto dev_unref;
  265. return 0;
  266. dev_unref:
  267. drm_dev_unref(drm);
  268. return ret;
  269. }
  270. static int pl111_amba_remove(struct amba_device *amba_dev)
  271. {
  272. struct drm_device *drm = amba_get_drvdata(amba_dev);
  273. struct pl111_drm_dev_private *priv = drm->dev_private;
  274. drm_dev_unregister(drm);
  275. drm_fb_cma_fbdev_fini(drm);
  276. if (priv->panel)
  277. drm_panel_bridge_remove(priv->bridge);
  278. drm_mode_config_cleanup(drm);
  279. drm_dev_unref(drm);
  280. return 0;
  281. }
  282. /*
  283. * This early variant lacks the 565 and 444 pixel formats.
  284. */
  285. static const u32 pl110_pixel_formats[] = {
  286. DRM_FORMAT_ABGR8888,
  287. DRM_FORMAT_XBGR8888,
  288. DRM_FORMAT_ARGB8888,
  289. DRM_FORMAT_XRGB8888,
  290. DRM_FORMAT_ABGR1555,
  291. DRM_FORMAT_XBGR1555,
  292. DRM_FORMAT_ARGB1555,
  293. DRM_FORMAT_XRGB1555,
  294. };
  295. static const struct pl111_variant_data pl110_variant = {
  296. .name = "PL110",
  297. .is_pl110 = true,
  298. .formats = pl110_pixel_formats,
  299. .nformats = ARRAY_SIZE(pl110_pixel_formats),
  300. .fb_bpp = 16,
  301. };
  302. /* RealView, Versatile Express etc use this modern variant */
  303. static const u32 pl111_pixel_formats[] = {
  304. DRM_FORMAT_ABGR8888,
  305. DRM_FORMAT_XBGR8888,
  306. DRM_FORMAT_ARGB8888,
  307. DRM_FORMAT_XRGB8888,
  308. DRM_FORMAT_BGR565,
  309. DRM_FORMAT_RGB565,
  310. DRM_FORMAT_ABGR1555,
  311. DRM_FORMAT_XBGR1555,
  312. DRM_FORMAT_ARGB1555,
  313. DRM_FORMAT_XRGB1555,
  314. DRM_FORMAT_ABGR4444,
  315. DRM_FORMAT_XBGR4444,
  316. DRM_FORMAT_ARGB4444,
  317. DRM_FORMAT_XRGB4444,
  318. };
  319. static const struct pl111_variant_data pl111_variant = {
  320. .name = "PL111",
  321. .formats = pl111_pixel_formats,
  322. .nformats = ARRAY_SIZE(pl111_pixel_formats),
  323. .fb_bpp = 32,
  324. };
  325. static const struct amba_id pl111_id_table[] = {
  326. {
  327. .id = 0x00041110,
  328. .mask = 0x000fffff,
  329. .data = (void*)&pl110_variant,
  330. },
  331. {
  332. .id = 0x00041111,
  333. .mask = 0x000fffff,
  334. .data = (void*)&pl111_variant,
  335. },
  336. {0, 0},
  337. };
  338. static struct amba_driver pl111_amba_driver __maybe_unused = {
  339. .drv = {
  340. .name = "drm-clcd-pl111",
  341. },
  342. .probe = pl111_amba_probe,
  343. .remove = pl111_amba_remove,
  344. .id_table = pl111_id_table,
  345. };
  346. #ifdef CONFIG_ARM_AMBA
  347. module_amba_driver(pl111_amba_driver);
  348. #endif
  349. MODULE_DESCRIPTION(DRIVER_DESC);
  350. MODULE_AUTHOR("ARM Ltd.");
  351. MODULE_LICENSE("GPL");