pl111_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
  4. *
  5. * Parts of this file were based on sources as follows:
  6. *
  7. * Copyright (c) 2006-2008 Intel Corporation
  8. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  9. * Copyright (C) 2011 Texas Instruments
  10. */
  11. /**
  12. * DOC: ARM PrimeCell PL110 and PL111 CLCD Driver
  13. *
  14. * The PL110/PL111 is a simple LCD controller that can support TFT
  15. * and STN displays. This driver exposes a standard KMS interface
  16. * for them.
  17. *
  18. * The driver currently doesn't expose the cursor. The DRM API for
  19. * cursors requires support for 64x64 ARGB8888 cursor images, while
  20. * the hardware can only support 64x64 monochrome with masking
  21. * cursors. While one could imagine trying to hack something together
  22. * to look at the ARGB8888 and program reasonable in monochrome, we
  23. * just don't expose the cursor at all instead, and leave cursor
  24. * support to the application software cursor layer.
  25. *
  26. * TODO:
  27. *
  28. * - Fix race between setting plane base address and getting IRQ for
  29. * vsync firing the pageflip completion.
  30. *
  31. * - Read back hardware state at boot to skip reprogramming the
  32. * hardware when doing a no-op modeset.
  33. *
  34. * - Use the CLKSEL bit to support switching between the two external
  35. * clock parents.
  36. */
  37. #include <linux/amba/bus.h>
  38. #include <linux/dma-buf.h>
  39. #include <linux/module.h>
  40. #include <linux/of.h>
  41. #include <linux/of_graph.h>
  42. #include <linux/of_reserved_mem.h>
  43. #include <linux/shmem_fs.h>
  44. #include <linux/slab.h>
  45. #include <drm/drm_atomic_helper.h>
  46. #include <drm/drm_bridge.h>
  47. #include <drm/drm_drv.h>
  48. #include <drm/drm_fbdev_dma.h>
  49. #include <drm/drm_fourcc.h>
  50. #include <drm/drm_gem_dma_helper.h>
  51. #include <drm/drm_gem_framebuffer_helper.h>
  52. #include <drm/drm_of.h>
  53. #include <drm/drm_panel.h>
  54. #include <drm/drm_probe_helper.h>
  55. #include <drm/drm_vblank.h>
  56. #include "pl111_drm.h"
  57. #include "pl111_versatile.h"
  58. #include "pl111_nomadik.h"
  59. #define DRIVER_DESC "DRM module for PL111"
  60. static const struct drm_mode_config_funcs mode_config_funcs = {
  61. .fb_create = drm_gem_fb_create,
  62. .atomic_check = drm_atomic_helper_check,
  63. .atomic_commit = drm_atomic_helper_commit,
  64. };
  65. static int pl111_modeset_init(struct drm_device *dev)
  66. {
  67. struct drm_mode_config *mode_config;
  68. struct pl111_drm_dev_private *priv = dev->dev_private;
  69. struct device_node *np = dev->dev->of_node;
  70. struct device_node *remote;
  71. struct drm_panel *panel = NULL;
  72. struct drm_bridge *bridge = NULL;
  73. bool defer = false;
  74. int ret;
  75. int i;
  76. ret = drmm_mode_config_init(dev);
  77. if (ret)
  78. return ret;
  79. mode_config = &dev->mode_config;
  80. mode_config->funcs = &mode_config_funcs;
  81. mode_config->min_width = 1;
  82. mode_config->max_width = 1024;
  83. mode_config->min_height = 1;
  84. mode_config->max_height = 768;
  85. i = 0;
  86. for_each_endpoint_of_node(np, remote) {
  87. struct drm_panel *tmp_panel;
  88. struct drm_bridge *tmp_bridge;
  89. dev_dbg(dev->dev, "checking endpoint %d\n", i);
  90. ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
  91. 0, i,
  92. &tmp_panel,
  93. &tmp_bridge);
  94. if (ret) {
  95. if (ret == -EPROBE_DEFER) {
  96. /*
  97. * Something deferred, but that is often just
  98. * another way of saying -ENODEV, but let's
  99. * cast a vote for later deferral.
  100. */
  101. defer = true;
  102. } else if (ret != -ENODEV) {
  103. /* Continue, maybe something else is working */
  104. dev_err(dev->dev,
  105. "endpoint %d returns %d\n", i, ret);
  106. }
  107. }
  108. if (tmp_panel) {
  109. dev_info(dev->dev,
  110. "found panel on endpoint %d\n", i);
  111. panel = tmp_panel;
  112. }
  113. if (tmp_bridge) {
  114. dev_info(dev->dev,
  115. "found bridge on endpoint %d\n", i);
  116. bridge = tmp_bridge;
  117. }
  118. i++;
  119. }
  120. /*
  121. * If we can't find neither panel nor bridge on any of the
  122. * endpoints, and any of them retured -EPROBE_DEFER, then
  123. * let's defer this driver too.
  124. */
  125. if ((!panel && !bridge) && defer)
  126. return -EPROBE_DEFER;
  127. if (panel) {
  128. bridge = drm_panel_bridge_add_typed(panel,
  129. DRM_MODE_CONNECTOR_Unknown);
  130. if (IS_ERR(bridge)) {
  131. ret = PTR_ERR(bridge);
  132. goto finish;
  133. }
  134. } else if (bridge) {
  135. dev_info(dev->dev, "Using non-panel bridge\n");
  136. } else {
  137. dev_err(dev->dev, "No bridge, exiting\n");
  138. return -ENODEV;
  139. }
  140. priv->bridge = bridge;
  141. if (panel) {
  142. priv->panel = panel;
  143. priv->connector = drm_panel_bridge_connector(bridge);
  144. }
  145. ret = pl111_display_init(dev);
  146. if (ret != 0) {
  147. dev_err(dev->dev, "Failed to init display\n");
  148. goto out_bridge;
  149. }
  150. ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
  151. bridge);
  152. if (ret)
  153. return ret;
  154. if (!priv->variant->broken_vblank) {
  155. ret = drm_vblank_init(dev, 1);
  156. if (ret != 0) {
  157. dev_err(dev->dev, "Failed to init vblank\n");
  158. goto out_bridge;
  159. }
  160. }
  161. drm_mode_config_reset(dev);
  162. drm_kms_helper_poll_init(dev);
  163. goto finish;
  164. out_bridge:
  165. if (panel)
  166. drm_panel_bridge_remove(bridge);
  167. finish:
  168. return ret;
  169. }
  170. static struct drm_gem_object *
  171. pl111_gem_import_sg_table(struct drm_device *dev,
  172. struct dma_buf_attachment *attach,
  173. struct sg_table *sgt)
  174. {
  175. struct pl111_drm_dev_private *priv = dev->dev_private;
  176. /*
  177. * When using device-specific reserved memory we can't import
  178. * DMA buffers: those are passed by reference in any global
  179. * memory and we can only handle a specific range of memory.
  180. */
  181. if (priv->use_device_memory)
  182. return ERR_PTR(-EINVAL);
  183. return drm_gem_dma_prime_import_sg_table(dev, attach, sgt);
  184. }
  185. DEFINE_DRM_GEM_DMA_FOPS(drm_fops);
  186. static const struct drm_driver pl111_drm_driver = {
  187. .driver_features =
  188. DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  189. .ioctls = NULL,
  190. .fops = &drm_fops,
  191. .name = "pl111",
  192. .desc = DRIVER_DESC,
  193. .date = "20170317",
  194. .major = 1,
  195. .minor = 0,
  196. .patchlevel = 0,
  197. .dumb_create = drm_gem_dma_dumb_create,
  198. .gem_prime_import_sg_table = pl111_gem_import_sg_table,
  199. #if defined(CONFIG_DEBUG_FS)
  200. .debugfs_init = pl111_debugfs_init,
  201. #endif
  202. };
  203. static int pl111_amba_probe(struct amba_device *amba_dev,
  204. const struct amba_id *id)
  205. {
  206. struct device *dev = &amba_dev->dev;
  207. struct pl111_drm_dev_private *priv;
  208. const struct pl111_variant_data *variant = id->data;
  209. struct drm_device *drm;
  210. int ret;
  211. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  212. if (!priv)
  213. return -ENOMEM;
  214. drm = drm_dev_alloc(&pl111_drm_driver, dev);
  215. if (IS_ERR(drm))
  216. return PTR_ERR(drm);
  217. amba_set_drvdata(amba_dev, drm);
  218. priv->drm = drm;
  219. drm->dev_private = priv;
  220. priv->variant = variant;
  221. ret = of_reserved_mem_device_init(dev);
  222. if (!ret) {
  223. dev_info(dev, "using device-specific reserved memory\n");
  224. priv->use_device_memory = true;
  225. }
  226. if (of_property_read_u32(dev->of_node, "max-memory-bandwidth",
  227. &priv->memory_bw)) {
  228. dev_info(dev, "no max memory bandwidth specified, assume unlimited\n");
  229. priv->memory_bw = 0;
  230. }
  231. /* The two main variants swap this register */
  232. if (variant->is_pl110 || variant->is_lcdc) {
  233. priv->ienb = CLCD_PL110_IENB;
  234. priv->ctrl = CLCD_PL110_CNTL;
  235. } else {
  236. priv->ienb = CLCD_PL111_IENB;
  237. priv->ctrl = CLCD_PL111_CNTL;
  238. }
  239. priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
  240. if (IS_ERR(priv->regs)) {
  241. dev_err(dev, "%s failed mmio\n", __func__);
  242. ret = PTR_ERR(priv->regs);
  243. goto dev_put;
  244. }
  245. /* This may override some variant settings */
  246. ret = pl111_versatile_init(dev, priv);
  247. if (ret)
  248. goto dev_put;
  249. pl111_nomadik_init(dev);
  250. /* turn off interrupts before requesting the irq */
  251. writel(0, priv->regs + priv->ienb);
  252. ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
  253. variant->name, priv);
  254. if (ret != 0) {
  255. dev_err(dev, "%s failed irq %d\n", __func__, ret);
  256. return ret;
  257. }
  258. ret = pl111_modeset_init(drm);
  259. if (ret != 0)
  260. goto dev_put;
  261. ret = drm_dev_register(drm, 0);
  262. if (ret < 0)
  263. goto dev_put;
  264. drm_fbdev_dma_setup(drm, priv->variant->fb_depth);
  265. return 0;
  266. dev_put:
  267. drm_dev_put(drm);
  268. of_reserved_mem_device_release(dev);
  269. return ret;
  270. }
  271. static void pl111_amba_remove(struct amba_device *amba_dev)
  272. {
  273. struct device *dev = &amba_dev->dev;
  274. struct drm_device *drm = amba_get_drvdata(amba_dev);
  275. struct pl111_drm_dev_private *priv = drm->dev_private;
  276. drm_dev_unregister(drm);
  277. drm_atomic_helper_shutdown(drm);
  278. if (priv->panel)
  279. drm_panel_bridge_remove(priv->bridge);
  280. drm_dev_put(drm);
  281. of_reserved_mem_device_release(dev);
  282. }
  283. static void pl111_amba_shutdown(struct amba_device *amba_dev)
  284. {
  285. drm_atomic_helper_shutdown(amba_get_drvdata(amba_dev));
  286. }
  287. /*
  288. * This early variant lacks the 565 and 444 pixel formats.
  289. */
  290. static const u32 pl110_pixel_formats[] = {
  291. DRM_FORMAT_ABGR8888,
  292. DRM_FORMAT_XBGR8888,
  293. DRM_FORMAT_ARGB8888,
  294. DRM_FORMAT_XRGB8888,
  295. DRM_FORMAT_ABGR1555,
  296. DRM_FORMAT_XBGR1555,
  297. DRM_FORMAT_ARGB1555,
  298. DRM_FORMAT_XRGB1555,
  299. };
  300. static const struct pl111_variant_data pl110_variant = {
  301. .name = "PL110",
  302. .is_pl110 = true,
  303. .formats = pl110_pixel_formats,
  304. .nformats = ARRAY_SIZE(pl110_pixel_formats),
  305. .fb_depth = 16,
  306. };
  307. /* RealView, Versatile Express etc use this modern variant */
  308. static const u32 pl111_pixel_formats[] = {
  309. DRM_FORMAT_ABGR8888,
  310. DRM_FORMAT_XBGR8888,
  311. DRM_FORMAT_ARGB8888,
  312. DRM_FORMAT_XRGB8888,
  313. DRM_FORMAT_BGR565,
  314. DRM_FORMAT_RGB565,
  315. DRM_FORMAT_ABGR1555,
  316. DRM_FORMAT_XBGR1555,
  317. DRM_FORMAT_ARGB1555,
  318. DRM_FORMAT_XRGB1555,
  319. DRM_FORMAT_ABGR4444,
  320. DRM_FORMAT_XBGR4444,
  321. DRM_FORMAT_ARGB4444,
  322. DRM_FORMAT_XRGB4444,
  323. };
  324. static const struct pl111_variant_data pl111_variant = {
  325. .name = "PL111",
  326. .formats = pl111_pixel_formats,
  327. .nformats = ARRAY_SIZE(pl111_pixel_formats),
  328. .fb_depth = 32,
  329. };
  330. static const u32 pl110_nomadik_pixel_formats[] = {
  331. DRM_FORMAT_RGB888,
  332. DRM_FORMAT_BGR888,
  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 pl110_nomadik_variant = {
  349. .name = "LCDC (PL110 Nomadik)",
  350. .formats = pl110_nomadik_pixel_formats,
  351. .nformats = ARRAY_SIZE(pl110_nomadik_pixel_formats),
  352. .is_lcdc = true,
  353. .st_bitmux_control = true,
  354. .broken_vblank = true,
  355. .fb_depth = 16,
  356. };
  357. static const struct amba_id pl111_id_table[] = {
  358. {
  359. .id = 0x00041110,
  360. .mask = 0x000fffff,
  361. .data = (void *)&pl110_variant,
  362. },
  363. {
  364. .id = 0x00180110,
  365. .mask = 0x00fffffe,
  366. .data = (void *)&pl110_nomadik_variant,
  367. },
  368. {
  369. .id = 0x00041111,
  370. .mask = 0x000fffff,
  371. .data = (void *)&pl111_variant,
  372. },
  373. {0, 0},
  374. };
  375. MODULE_DEVICE_TABLE(amba, pl111_id_table);
  376. static struct amba_driver pl111_amba_driver __maybe_unused = {
  377. .drv = {
  378. .name = "drm-clcd-pl111",
  379. },
  380. .probe = pl111_amba_probe,
  381. .remove = pl111_amba_remove,
  382. .shutdown = pl111_amba_shutdown,
  383. .id_table = pl111_id_table,
  384. };
  385. #ifdef CONFIG_ARM_AMBA
  386. module_amba_driver(pl111_amba_driver);
  387. #endif
  388. MODULE_DESCRIPTION(DRIVER_DESC);
  389. MODULE_AUTHOR("ARM Ltd.");
  390. MODULE_LICENSE("GPL");