hdlcd_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) 2013-2015 ARM Limited
  3. * Author: Liviu Dudau <[email protected]>
  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_fb_helper.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->dev_private;
  82. struct platform_device *pdev = to_platform_device(drm->dev);
  83. struct resource *res;
  84. u32 version;
  85. int ret;
  86. hdlcd->clk = devm_clk_get(drm->dev, "pxlclk");
  87. if (IS_ERR(hdlcd->clk))
  88. return PTR_ERR(hdlcd->clk);
  89. #ifdef CONFIG_DEBUG_FS
  90. atomic_set(&hdlcd->buffer_underrun_count, 0);
  91. atomic_set(&hdlcd->bus_error_count, 0);
  92. atomic_set(&hdlcd->vsync_count, 0);
  93. atomic_set(&hdlcd->dma_end_count, 0);
  94. #endif
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. hdlcd->mmio = devm_ioremap_resource(drm->dev, res);
  97. if (IS_ERR(hdlcd->mmio)) {
  98. DRM_ERROR("failed to map control registers area\n");
  99. ret = PTR_ERR(hdlcd->mmio);
  100. hdlcd->mmio = NULL;
  101. return ret;
  102. }
  103. version = hdlcd_read(hdlcd, HDLCD_REG_VERSION);
  104. if ((version & HDLCD_PRODUCT_MASK) != HDLCD_PRODUCT_ID) {
  105. DRM_ERROR("unknown product id: 0x%x\n", version);
  106. return -EINVAL;
  107. }
  108. DRM_INFO("found ARM HDLCD version r%dp%d\n",
  109. (version & HDLCD_VERSION_MAJOR_MASK) >> 8,
  110. version & HDLCD_VERSION_MINOR_MASK);
  111. /* Get the optional framebuffer memory resource */
  112. ret = of_reserved_mem_device_init(drm->dev);
  113. if (ret && ret != -ENODEV)
  114. return ret;
  115. ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
  116. if (ret)
  117. goto setup_fail;
  118. ret = hdlcd_setup_crtc(drm);
  119. if (ret < 0) {
  120. DRM_ERROR("failed to create crtc\n");
  121. goto setup_fail;
  122. }
  123. ret = platform_get_irq(pdev, 0);
  124. if (ret < 0)
  125. goto irq_fail;
  126. hdlcd->irq = ret;
  127. ret = hdlcd_irq_install(hdlcd);
  128. if (ret < 0) {
  129. DRM_ERROR("failed to install IRQ handler\n");
  130. goto irq_fail;
  131. }
  132. return 0;
  133. irq_fail:
  134. drm_crtc_cleanup(&hdlcd->crtc);
  135. setup_fail:
  136. of_reserved_mem_device_release(drm->dev);
  137. return ret;
  138. }
  139. static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
  140. .fb_create = drm_gem_fb_create,
  141. .atomic_check = drm_atomic_helper_check,
  142. .atomic_commit = drm_atomic_helper_commit,
  143. };
  144. static void hdlcd_setup_mode_config(struct drm_device *drm)
  145. {
  146. drm_mode_config_init(drm);
  147. drm->mode_config.min_width = 0;
  148. drm->mode_config.min_height = 0;
  149. drm->mode_config.max_width = HDLCD_MAX_XRES;
  150. drm->mode_config.max_height = HDLCD_MAX_YRES;
  151. drm->mode_config.funcs = &hdlcd_mode_config_funcs;
  152. }
  153. #ifdef CONFIG_DEBUG_FS
  154. static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
  155. {
  156. struct drm_info_node *node = (struct drm_info_node *)m->private;
  157. struct drm_device *drm = node->minor->dev;
  158. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  159. seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
  160. seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
  161. seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
  162. seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
  163. return 0;
  164. }
  165. static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
  166. {
  167. struct drm_info_node *node = (struct drm_info_node *)m->private;
  168. struct drm_device *drm = node->minor->dev;
  169. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  170. unsigned long clkrate = clk_get_rate(hdlcd->clk);
  171. unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
  172. seq_printf(m, "hw : %lu\n", clkrate);
  173. seq_printf(m, "mode: %lu\n", mode_clock);
  174. return 0;
  175. }
  176. static struct drm_info_list hdlcd_debugfs_list[] = {
  177. { "interrupt_count", hdlcd_show_underrun_count, 0 },
  178. { "clocks", hdlcd_show_pxlclock, 0 },
  179. };
  180. static void hdlcd_debugfs_init(struct drm_minor *minor)
  181. {
  182. drm_debugfs_create_files(hdlcd_debugfs_list,
  183. ARRAY_SIZE(hdlcd_debugfs_list),
  184. minor->debugfs_root, minor);
  185. }
  186. #endif
  187. DEFINE_DRM_GEM_DMA_FOPS(fops);
  188. static const struct drm_driver hdlcd_driver = {
  189. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  190. DRM_GEM_DMA_DRIVER_OPS,
  191. #ifdef CONFIG_DEBUG_FS
  192. .debugfs_init = hdlcd_debugfs_init,
  193. #endif
  194. .fops = &fops,
  195. .name = "hdlcd",
  196. .desc = "ARM HDLCD Controller DRM",
  197. .date = "20151021",
  198. .major = 1,
  199. .minor = 0,
  200. };
  201. static int hdlcd_drm_bind(struct device *dev)
  202. {
  203. struct drm_device *drm;
  204. struct hdlcd_drm_private *hdlcd;
  205. int ret;
  206. hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
  207. if (!hdlcd)
  208. return -ENOMEM;
  209. drm = drm_dev_alloc(&hdlcd_driver, dev);
  210. if (IS_ERR(drm))
  211. return PTR_ERR(drm);
  212. drm->dev_private = hdlcd;
  213. dev_set_drvdata(dev, drm);
  214. hdlcd_setup_mode_config(drm);
  215. ret = hdlcd_load(drm, 0);
  216. if (ret)
  217. goto err_free;
  218. /* Set the CRTC's port so that the encoder component can find it */
  219. hdlcd->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  220. ret = component_bind_all(dev, drm);
  221. if (ret) {
  222. DRM_ERROR("Failed to bind all components\n");
  223. goto err_unload;
  224. }
  225. ret = pm_runtime_set_active(dev);
  226. if (ret)
  227. goto err_pm_active;
  228. pm_runtime_enable(dev);
  229. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  230. if (ret < 0) {
  231. DRM_ERROR("failed to initialise vblank\n");
  232. goto err_vblank;
  233. }
  234. /*
  235. * If EFI left us running, take over from simple framebuffer
  236. * drivers. Read HDLCD_REG_COMMAND to see if we are enabled.
  237. */
  238. if (hdlcd_read(hdlcd, HDLCD_REG_COMMAND)) {
  239. hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
  240. drm_aperture_remove_framebuffers(false, &hdlcd_driver);
  241. }
  242. drm_mode_config_reset(drm);
  243. drm_kms_helper_poll_init(drm);
  244. ret = drm_dev_register(drm, 0);
  245. if (ret)
  246. goto err_register;
  247. drm_fbdev_generic_setup(drm, 32);
  248. return 0;
  249. err_register:
  250. drm_kms_helper_poll_fini(drm);
  251. err_vblank:
  252. pm_runtime_disable(drm->dev);
  253. err_pm_active:
  254. drm_atomic_helper_shutdown(drm);
  255. component_unbind_all(dev, drm);
  256. err_unload:
  257. of_node_put(hdlcd->crtc.port);
  258. hdlcd->crtc.port = NULL;
  259. hdlcd_irq_uninstall(hdlcd);
  260. of_reserved_mem_device_release(drm->dev);
  261. err_free:
  262. drm_mode_config_cleanup(drm);
  263. dev_set_drvdata(dev, NULL);
  264. drm_dev_put(drm);
  265. return ret;
  266. }
  267. static void hdlcd_drm_unbind(struct device *dev)
  268. {
  269. struct drm_device *drm = dev_get_drvdata(dev);
  270. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  271. drm_dev_unregister(drm);
  272. drm_kms_helper_poll_fini(drm);
  273. component_unbind_all(dev, drm);
  274. of_node_put(hdlcd->crtc.port);
  275. hdlcd->crtc.port = NULL;
  276. pm_runtime_get_sync(dev);
  277. drm_atomic_helper_shutdown(drm);
  278. hdlcd_irq_uninstall(hdlcd);
  279. pm_runtime_put(dev);
  280. if (pm_runtime_enabled(dev))
  281. pm_runtime_disable(dev);
  282. of_reserved_mem_device_release(dev);
  283. drm_mode_config_cleanup(drm);
  284. drm->dev_private = NULL;
  285. dev_set_drvdata(dev, NULL);
  286. drm_dev_put(drm);
  287. }
  288. static const struct component_master_ops hdlcd_master_ops = {
  289. .bind = hdlcd_drm_bind,
  290. .unbind = hdlcd_drm_unbind,
  291. };
  292. static int compare_dev(struct device *dev, void *data)
  293. {
  294. return dev->of_node == data;
  295. }
  296. static int hdlcd_probe(struct platform_device *pdev)
  297. {
  298. struct device_node *port;
  299. struct component_match *match = NULL;
  300. /* there is only one output port inside each device, find it */
  301. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  302. if (!port)
  303. return -ENODEV;
  304. drm_of_component_match_add(&pdev->dev, &match, compare_dev, port);
  305. of_node_put(port);
  306. return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
  307. match);
  308. }
  309. static int hdlcd_remove(struct platform_device *pdev)
  310. {
  311. component_master_del(&pdev->dev, &hdlcd_master_ops);
  312. return 0;
  313. }
  314. static const struct of_device_id hdlcd_of_match[] = {
  315. { .compatible = "arm,hdlcd" },
  316. {},
  317. };
  318. MODULE_DEVICE_TABLE(of, hdlcd_of_match);
  319. static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
  320. {
  321. struct drm_device *drm = dev_get_drvdata(dev);
  322. return drm_mode_config_helper_suspend(drm);
  323. }
  324. static int __maybe_unused hdlcd_pm_resume(struct device *dev)
  325. {
  326. struct drm_device *drm = dev_get_drvdata(dev);
  327. drm_mode_config_helper_resume(drm);
  328. return 0;
  329. }
  330. static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
  331. static struct platform_driver hdlcd_platform_driver = {
  332. .probe = hdlcd_probe,
  333. .remove = hdlcd_remove,
  334. .driver = {
  335. .name = "hdlcd",
  336. .pm = &hdlcd_pm_ops,
  337. .of_match_table = hdlcd_of_match,
  338. },
  339. };
  340. drm_module_platform_driver(hdlcd_platform_driver);
  341. MODULE_AUTHOR("Liviu Dudau");
  342. MODULE_DESCRIPTION("ARM HDLCD DRM driver");
  343. MODULE_LICENSE("GPL v2");