pl111_drv.c 11 KB

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