hdlcd_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (C) 2013-2015 ARM Limited
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file COPYING in the main directory of this archive
  7. * for more details.
  8. *
  9. * ARM HDLCD Driver
  10. */
  11. #include <linux/module.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/clk.h>
  14. #include <linux/component.h>
  15. #include <linux/console.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/list.h>
  18. #include <linux/of_graph.h>
  19. #include <linux/of_reserved_mem.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <drm/drm_aperture.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_debugfs.h>
  26. #include <drm/drm_drv.h>
  27. #include <drm/drm_fbdev_dma.h>
  28. #include <drm/drm_gem_dma_helper.h>
  29. #include <drm/drm_gem_framebuffer_helper.h>
  30. #include <drm/drm_modeset_helper.h>
  31. #include <drm/drm_module.h>
  32. #include <drm/drm_of.h>
  33. #include <drm/drm_probe_helper.h>
  34. #include <drm/drm_vblank.h>
  35. #include "hdlcd_drv.h"
  36. #include "hdlcd_regs.h"
  37. static irqreturn_t hdlcd_irq(int irq, void *arg)
  38. {
  39. struct hdlcd_drm_private *hdlcd = arg;
  40. unsigned long irq_status;
  41. irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
  42. #ifdef CONFIG_DEBUG_FS
  43. if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
  44. atomic_inc(&hdlcd->buffer_underrun_count);
  45. if (irq_status & HDLCD_INTERRUPT_DMA_END)
  46. atomic_inc(&hdlcd->dma_end_count);
  47. if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
  48. atomic_inc(&hdlcd->bus_error_count);
  49. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  50. atomic_inc(&hdlcd->vsync_count);
  51. #endif
  52. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  53. drm_crtc_handle_vblank(&hdlcd->crtc);
  54. /* acknowledge interrupt(s) */
  55. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
  56. return IRQ_HANDLED;
  57. }
  58. static int hdlcd_irq_install(struct hdlcd_drm_private *hdlcd)
  59. {
  60. int ret;
  61. /* Ensure interrupts are disabled */
  62. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
  63. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
  64. ret = request_irq(hdlcd->irq, hdlcd_irq, 0, "hdlcd", hdlcd);
  65. if (ret)
  66. return ret;
  67. #ifdef CONFIG_DEBUG_FS
  68. /* enable debug interrupts */
  69. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, HDLCD_DEBUG_INT_MASK);
  70. #endif
  71. return 0;
  72. }
  73. static void hdlcd_irq_uninstall(struct hdlcd_drm_private *hdlcd)
  74. {
  75. /* disable all the interrupts that we might have enabled */
  76. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
  77. free_irq(hdlcd->irq, hdlcd);
  78. }
  79. static int hdlcd_load(struct drm_device *drm, unsigned long flags)
  80. {
  81. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  82. struct platform_device *pdev = to_platform_device(drm->dev);
  83. u32 version;
  84. int ret;
  85. hdlcd->clk = devm_clk_get(drm->dev, "pxlclk");
  86. if (IS_ERR(hdlcd->clk))
  87. return PTR_ERR(hdlcd->clk);
  88. #ifdef CONFIG_DEBUG_FS
  89. atomic_set(&hdlcd->buffer_underrun_count, 0);
  90. atomic_set(&hdlcd->bus_error_count, 0);
  91. atomic_set(&hdlcd->vsync_count, 0);
  92. atomic_set(&hdlcd->dma_end_count, 0);
  93. #endif
  94. hdlcd->mmio = devm_platform_ioremap_resource(pdev, 0);
  95. if (IS_ERR(hdlcd->mmio)) {
  96. DRM_ERROR("failed to map control registers area\n");
  97. ret = PTR_ERR(hdlcd->mmio);
  98. hdlcd->mmio = NULL;
  99. return ret;
  100. }
  101. version = hdlcd_read(hdlcd, HDLCD_REG_VERSION);
  102. if ((version & HDLCD_PRODUCT_MASK) != HDLCD_PRODUCT_ID) {
  103. DRM_ERROR("unknown product id: 0x%x\n", version);
  104. return -EINVAL;
  105. }
  106. DRM_INFO("found ARM HDLCD version r%dp%d\n",
  107. (version & HDLCD_VERSION_MAJOR_MASK) >> 8,
  108. version & HDLCD_VERSION_MINOR_MASK);
  109. /* Get the optional framebuffer memory resource */
  110. ret = of_reserved_mem_device_init(drm->dev);
  111. if (ret && ret != -ENODEV)
  112. return ret;
  113. ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
  114. if (ret)
  115. goto setup_fail;
  116. ret = hdlcd_setup_crtc(drm);
  117. if (ret < 0) {
  118. DRM_ERROR("failed to create crtc\n");
  119. goto setup_fail;
  120. }
  121. ret = platform_get_irq(pdev, 0);
  122. if (ret < 0)
  123. goto irq_fail;
  124. hdlcd->irq = ret;
  125. ret = hdlcd_irq_install(hdlcd);
  126. if (ret < 0) {
  127. DRM_ERROR("failed to install IRQ handler\n");
  128. goto irq_fail;
  129. }
  130. return 0;
  131. irq_fail:
  132. drm_crtc_cleanup(&hdlcd->crtc);
  133. setup_fail:
  134. of_reserved_mem_device_release(drm->dev);
  135. return ret;
  136. }
  137. static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
  138. .fb_create = drm_gem_fb_create,
  139. .atomic_check = drm_atomic_helper_check,
  140. .atomic_commit = drm_atomic_helper_commit,
  141. };
  142. static int hdlcd_setup_mode_config(struct drm_device *drm)
  143. {
  144. int ret;
  145. ret = drmm_mode_config_init(drm);
  146. if (ret)
  147. return ret;
  148. drm->mode_config.min_width = 0;
  149. drm->mode_config.min_height = 0;
  150. drm->mode_config.max_width = HDLCD_MAX_XRES;
  151. drm->mode_config.max_height = HDLCD_MAX_YRES;
  152. drm->mode_config.funcs = &hdlcd_mode_config_funcs;
  153. return 0;
  154. }
  155. #ifdef CONFIG_DEBUG_FS
  156. static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
  157. {
  158. struct drm_debugfs_entry *entry = m->private;
  159. struct drm_device *drm = entry->dev;
  160. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  161. seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
  162. seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
  163. seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
  164. seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
  165. return 0;
  166. }
  167. static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
  168. {
  169. struct drm_debugfs_entry *entry = m->private;
  170. struct drm_device *drm = entry->dev;
  171. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  172. unsigned long clkrate = clk_get_rate(hdlcd->clk);
  173. unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
  174. seq_printf(m, "hw : %lu\n", clkrate);
  175. seq_printf(m, "mode: %lu\n", mode_clock);
  176. return 0;
  177. }
  178. static struct drm_debugfs_info hdlcd_debugfs_list[] = {
  179. { "interrupt_count", hdlcd_show_underrun_count, 0 },
  180. { "clocks", hdlcd_show_pxlclock, 0 },
  181. };
  182. #endif
  183. DEFINE_DRM_GEM_DMA_FOPS(fops);
  184. static const struct drm_driver hdlcd_driver = {
  185. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  186. DRM_GEM_DMA_DRIVER_OPS,
  187. .fops = &fops,
  188. .name = "hdlcd",
  189. .desc = "ARM HDLCD Controller DRM",
  190. .date = "20151021",
  191. .major = 1,
  192. .minor = 0,
  193. };
  194. static int hdlcd_drm_bind(struct device *dev)
  195. {
  196. struct drm_device *drm;
  197. struct hdlcd_drm_private *hdlcd;
  198. int ret;
  199. hdlcd = devm_drm_dev_alloc(dev, &hdlcd_driver, typeof(*hdlcd), base);
  200. if (IS_ERR(hdlcd))
  201. return PTR_ERR(hdlcd);
  202. drm = &hdlcd->base;
  203. dev_set_drvdata(dev, drm);
  204. ret = hdlcd_setup_mode_config(drm);
  205. if (ret)
  206. goto err_free;
  207. ret = hdlcd_load(drm, 0);
  208. if (ret)
  209. goto err_free;
  210. /* Set the CRTC's port so that the encoder component can find it */
  211. hdlcd->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  212. ret = component_bind_all(dev, drm);
  213. if (ret) {
  214. DRM_ERROR("Failed to bind all components\n");
  215. goto err_unload;
  216. }
  217. ret = pm_runtime_set_active(dev);
  218. if (ret)
  219. goto err_pm_active;
  220. pm_runtime_enable(dev);
  221. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  222. if (ret < 0) {
  223. DRM_ERROR("failed to initialise vblank\n");
  224. goto err_vblank;
  225. }
  226. /*
  227. * If EFI left us running, take over from simple framebuffer
  228. * drivers. Read HDLCD_REG_COMMAND to see if we are enabled.
  229. */
  230. if (hdlcd_read(hdlcd, HDLCD_REG_COMMAND)) {
  231. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  232. drm_aperture_remove_framebuffers(&hdlcd_driver);
  233. }
  234. drm_mode_config_reset(drm);
  235. drm_kms_helper_poll_init(drm);
  236. #ifdef CONFIG_DEBUG_FS
  237. drm_debugfs_add_files(drm, hdlcd_debugfs_list, ARRAY_SIZE(hdlcd_debugfs_list));
  238. #endif
  239. ret = drm_dev_register(drm, 0);
  240. if (ret)
  241. goto err_register;
  242. drm_fbdev_dma_setup(drm, 32);
  243. return 0;
  244. err_register:
  245. drm_kms_helper_poll_fini(drm);
  246. err_vblank:
  247. pm_runtime_disable(drm->dev);
  248. err_pm_active:
  249. drm_atomic_helper_shutdown(drm);
  250. component_unbind_all(dev, drm);
  251. err_unload:
  252. of_node_put(hdlcd->crtc.port);
  253. hdlcd->crtc.port = NULL;
  254. hdlcd_irq_uninstall(hdlcd);
  255. of_reserved_mem_device_release(drm->dev);
  256. err_free:
  257. dev_set_drvdata(dev, NULL);
  258. return ret;
  259. }
  260. static void hdlcd_drm_unbind(struct device *dev)
  261. {
  262. struct drm_device *drm = dev_get_drvdata(dev);
  263. struct hdlcd_drm_private *hdlcd = drm_to_hdlcd_priv(drm);
  264. drm_dev_unregister(drm);
  265. drm_kms_helper_poll_fini(drm);
  266. component_unbind_all(dev, drm);
  267. of_node_put(hdlcd->crtc.port);
  268. hdlcd->crtc.port = NULL;
  269. pm_runtime_get_sync(dev);
  270. drm_atomic_helper_shutdown(drm);
  271. hdlcd_irq_uninstall(hdlcd);
  272. pm_runtime_put(dev);
  273. if (pm_runtime_enabled(dev))
  274. pm_runtime_disable(dev);
  275. of_reserved_mem_device_release(dev);
  276. dev_set_drvdata(dev, NULL);
  277. }
  278. static const struct component_master_ops hdlcd_master_ops = {
  279. .bind = hdlcd_drm_bind,
  280. .unbind = hdlcd_drm_unbind,
  281. };
  282. static int compare_dev(struct device *dev, void *data)
  283. {
  284. return dev->of_node == data;
  285. }
  286. static int hdlcd_probe(struct platform_device *pdev)
  287. {
  288. struct device_node *port;
  289. struct component_match *match = NULL;
  290. /* there is only one output port inside each device, find it */
  291. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  292. if (!port)
  293. return -ENODEV;
  294. drm_of_component_match_add(&pdev->dev, &match, compare_dev, port);
  295. of_node_put(port);
  296. return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
  297. match);
  298. }
  299. static void hdlcd_remove(struct platform_device *pdev)
  300. {
  301. component_master_del(&pdev->dev, &hdlcd_master_ops);
  302. }
  303. static void hdlcd_shutdown(struct platform_device *pdev)
  304. {
  305. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  306. }
  307. static const struct of_device_id hdlcd_of_match[] = {
  308. { .compatible = "arm,hdlcd" },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(of, hdlcd_of_match);
  312. static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
  313. {
  314. struct drm_device *drm = dev_get_drvdata(dev);
  315. return drm_mode_config_helper_suspend(drm);
  316. }
  317. static int __maybe_unused hdlcd_pm_resume(struct device *dev)
  318. {
  319. struct drm_device *drm = dev_get_drvdata(dev);
  320. drm_mode_config_helper_resume(drm);
  321. return 0;
  322. }
  323. static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
  324. static struct platform_driver hdlcd_platform_driver = {
  325. .probe = hdlcd_probe,
  326. .remove_new = hdlcd_remove,
  327. .shutdown = hdlcd_shutdown,
  328. .driver = {
  329. .name = "hdlcd",
  330. .pm = &hdlcd_pm_ops,
  331. .of_match_table = hdlcd_of_match,
  332. },
  333. };
  334. drm_module_platform_driver(hdlcd_platform_driver);
  335. MODULE_AUTHOR("Liviu Dudau");
  336. MODULE_DESCRIPTION("ARM HDLCD DRM driver");
  337. MODULE_LICENSE("GPL v2");