pl111_drv.c 12 KB

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