hdlcd_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/list.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/of_reserved_mem.h>
  19. #include <linux/pm_runtime.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_atomic_helper.h>
  22. #include <drm/drm_crtc.h>
  23. #include <drm/drm_crtc_helper.h>
  24. #include <drm/drm_fb_helper.h>
  25. #include <drm/drm_fb_cma_helper.h>
  26. #include <drm/drm_gem_cma_helper.h>
  27. #include <drm/drm_gem_framebuffer_helper.h>
  28. #include <drm/drm_modeset_helper.h>
  29. #include <drm/drm_of.h>
  30. #include "hdlcd_drv.h"
  31. #include "hdlcd_regs.h"
  32. static int hdlcd_load(struct drm_device *drm, unsigned long flags)
  33. {
  34. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  35. struct platform_device *pdev = to_platform_device(drm->dev);
  36. struct resource *res;
  37. u32 version;
  38. int ret;
  39. hdlcd->clk = devm_clk_get(drm->dev, "pxlclk");
  40. if (IS_ERR(hdlcd->clk))
  41. return PTR_ERR(hdlcd->clk);
  42. #ifdef CONFIG_DEBUG_FS
  43. atomic_set(&hdlcd->buffer_underrun_count, 0);
  44. atomic_set(&hdlcd->bus_error_count, 0);
  45. atomic_set(&hdlcd->vsync_count, 0);
  46. atomic_set(&hdlcd->dma_end_count, 0);
  47. #endif
  48. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  49. hdlcd->mmio = devm_ioremap_resource(drm->dev, res);
  50. if (IS_ERR(hdlcd->mmio)) {
  51. DRM_ERROR("failed to map control registers area\n");
  52. ret = PTR_ERR(hdlcd->mmio);
  53. hdlcd->mmio = NULL;
  54. return ret;
  55. }
  56. version = hdlcd_read(hdlcd, HDLCD_REG_VERSION);
  57. if ((version & HDLCD_PRODUCT_MASK) != HDLCD_PRODUCT_ID) {
  58. DRM_ERROR("unknown product id: 0x%x\n", version);
  59. return -EINVAL;
  60. }
  61. DRM_INFO("found ARM HDLCD version r%dp%d\n",
  62. (version & HDLCD_VERSION_MAJOR_MASK) >> 8,
  63. version & HDLCD_VERSION_MINOR_MASK);
  64. /* Get the optional framebuffer memory resource */
  65. ret = of_reserved_mem_device_init(drm->dev);
  66. if (ret && ret != -ENODEV)
  67. return ret;
  68. ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
  69. if (ret)
  70. goto setup_fail;
  71. ret = hdlcd_setup_crtc(drm);
  72. if (ret < 0) {
  73. DRM_ERROR("failed to create crtc\n");
  74. goto setup_fail;
  75. }
  76. ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
  77. if (ret < 0) {
  78. DRM_ERROR("failed to install IRQ handler\n");
  79. goto irq_fail;
  80. }
  81. return 0;
  82. irq_fail:
  83. drm_crtc_cleanup(&hdlcd->crtc);
  84. setup_fail:
  85. of_reserved_mem_device_release(drm->dev);
  86. return ret;
  87. }
  88. static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
  89. .fb_create = drm_gem_fb_create,
  90. .atomic_check = drm_atomic_helper_check,
  91. .atomic_commit = drm_atomic_helper_commit,
  92. };
  93. static void hdlcd_setup_mode_config(struct drm_device *drm)
  94. {
  95. drm_mode_config_init(drm);
  96. drm->mode_config.min_width = 0;
  97. drm->mode_config.min_height = 0;
  98. drm->mode_config.max_width = HDLCD_MAX_XRES;
  99. drm->mode_config.max_height = HDLCD_MAX_YRES;
  100. drm->mode_config.funcs = &hdlcd_mode_config_funcs;
  101. }
  102. static irqreturn_t hdlcd_irq(int irq, void *arg)
  103. {
  104. struct drm_device *drm = arg;
  105. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  106. unsigned long irq_status;
  107. irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
  108. #ifdef CONFIG_DEBUG_FS
  109. if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
  110. atomic_inc(&hdlcd->buffer_underrun_count);
  111. if (irq_status & HDLCD_INTERRUPT_DMA_END)
  112. atomic_inc(&hdlcd->dma_end_count);
  113. if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
  114. atomic_inc(&hdlcd->bus_error_count);
  115. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  116. atomic_inc(&hdlcd->vsync_count);
  117. #endif
  118. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  119. drm_crtc_handle_vblank(&hdlcd->crtc);
  120. /* acknowledge interrupt(s) */
  121. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
  122. return IRQ_HANDLED;
  123. }
  124. static void hdlcd_irq_preinstall(struct drm_device *drm)
  125. {
  126. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  127. /* Ensure interrupts are disabled */
  128. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
  129. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
  130. }
  131. static int hdlcd_irq_postinstall(struct drm_device *drm)
  132. {
  133. #ifdef CONFIG_DEBUG_FS
  134. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  135. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  136. /* enable debug interrupts */
  137. irq_mask |= HDLCD_DEBUG_INT_MASK;
  138. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  139. #endif
  140. return 0;
  141. }
  142. static void hdlcd_irq_uninstall(struct drm_device *drm)
  143. {
  144. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  145. /* disable all the interrupts that we might have enabled */
  146. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  147. #ifdef CONFIG_DEBUG_FS
  148. /* disable debug interrupts */
  149. irq_mask &= ~HDLCD_DEBUG_INT_MASK;
  150. #endif
  151. /* disable vsync interrupts */
  152. irq_mask &= ~HDLCD_INTERRUPT_VSYNC;
  153. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  154. }
  155. #ifdef CONFIG_DEBUG_FS
  156. static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
  157. {
  158. struct drm_info_node *node = (struct drm_info_node *)m->private;
  159. struct drm_device *drm = node->minor->dev;
  160. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  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_info_node *node = (struct drm_info_node *)m->private;
  170. struct drm_device *drm = node->minor->dev;
  171. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  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_info_list hdlcd_debugfs_list[] = {
  179. { "interrupt_count", hdlcd_show_underrun_count, 0 },
  180. { "clocks", hdlcd_show_pxlclock, 0 },
  181. };
  182. static int hdlcd_debugfs_init(struct drm_minor *minor)
  183. {
  184. return drm_debugfs_create_files(hdlcd_debugfs_list,
  185. ARRAY_SIZE(hdlcd_debugfs_list), minor->debugfs_root, minor);
  186. }
  187. #endif
  188. DEFINE_DRM_GEM_CMA_FOPS(fops);
  189. static struct drm_driver hdlcd_driver = {
  190. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  191. DRIVER_MODESET | DRIVER_PRIME |
  192. DRIVER_ATOMIC,
  193. .irq_handler = hdlcd_irq,
  194. .irq_preinstall = hdlcd_irq_preinstall,
  195. .irq_postinstall = hdlcd_irq_postinstall,
  196. .irq_uninstall = hdlcd_irq_uninstall,
  197. .gem_free_object_unlocked = drm_gem_cma_free_object,
  198. .gem_print_info = drm_gem_cma_print_info,
  199. .gem_vm_ops = &drm_gem_cma_vm_ops,
  200. .dumb_create = drm_gem_cma_dumb_create,
  201. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  202. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  203. .gem_prime_export = drm_gem_prime_export,
  204. .gem_prime_import = drm_gem_prime_import,
  205. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  206. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  207. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  208. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  209. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  210. #ifdef CONFIG_DEBUG_FS
  211. .debugfs_init = hdlcd_debugfs_init,
  212. #endif
  213. .fops = &fops,
  214. .name = "hdlcd",
  215. .desc = "ARM HDLCD Controller DRM",
  216. .date = "20151021",
  217. .major = 1,
  218. .minor = 0,
  219. };
  220. static int hdlcd_drm_bind(struct device *dev)
  221. {
  222. struct drm_device *drm;
  223. struct hdlcd_drm_private *hdlcd;
  224. int ret;
  225. hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
  226. if (!hdlcd)
  227. return -ENOMEM;
  228. drm = drm_dev_alloc(&hdlcd_driver, dev);
  229. if (IS_ERR(drm))
  230. return PTR_ERR(drm);
  231. drm->dev_private = hdlcd;
  232. dev_set_drvdata(dev, drm);
  233. hdlcd_setup_mode_config(drm);
  234. ret = hdlcd_load(drm, 0);
  235. if (ret)
  236. goto err_free;
  237. /* Set the CRTC's port so that the encoder component can find it */
  238. hdlcd->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  239. ret = component_bind_all(dev, drm);
  240. if (ret) {
  241. DRM_ERROR("Failed to bind all components\n");
  242. goto err_unload;
  243. }
  244. ret = pm_runtime_set_active(dev);
  245. if (ret)
  246. goto err_pm_active;
  247. pm_runtime_enable(dev);
  248. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  249. if (ret < 0) {
  250. DRM_ERROR("failed to initialise vblank\n");
  251. goto err_vblank;
  252. }
  253. drm_mode_config_reset(drm);
  254. drm_kms_helper_poll_init(drm);
  255. ret = drm_dev_register(drm, 0);
  256. if (ret)
  257. goto err_register;
  258. drm_fbdev_generic_setup(drm, 32);
  259. return 0;
  260. err_register:
  261. drm_kms_helper_poll_fini(drm);
  262. err_vblank:
  263. pm_runtime_disable(drm->dev);
  264. err_pm_active:
  265. drm_atomic_helper_shutdown(drm);
  266. component_unbind_all(dev, drm);
  267. err_unload:
  268. of_node_put(hdlcd->crtc.port);
  269. hdlcd->crtc.port = NULL;
  270. drm_irq_uninstall(drm);
  271. of_reserved_mem_device_release(drm->dev);
  272. err_free:
  273. drm_mode_config_cleanup(drm);
  274. dev_set_drvdata(dev, NULL);
  275. drm_dev_put(drm);
  276. return ret;
  277. }
  278. static void hdlcd_drm_unbind(struct device *dev)
  279. {
  280. struct drm_device *drm = dev_get_drvdata(dev);
  281. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  282. drm_dev_unregister(drm);
  283. drm_kms_helper_poll_fini(drm);
  284. component_unbind_all(dev, drm);
  285. of_node_put(hdlcd->crtc.port);
  286. hdlcd->crtc.port = NULL;
  287. pm_runtime_get_sync(dev);
  288. drm_crtc_vblank_off(&hdlcd->crtc);
  289. drm_irq_uninstall(drm);
  290. drm_atomic_helper_shutdown(drm);
  291. pm_runtime_put(dev);
  292. if (pm_runtime_enabled(dev))
  293. pm_runtime_disable(dev);
  294. of_reserved_mem_device_release(dev);
  295. drm_mode_config_cleanup(drm);
  296. drm->dev_private = NULL;
  297. dev_set_drvdata(dev, NULL);
  298. drm_dev_put(drm);
  299. }
  300. static const struct component_master_ops hdlcd_master_ops = {
  301. .bind = hdlcd_drm_bind,
  302. .unbind = hdlcd_drm_unbind,
  303. };
  304. static int compare_dev(struct device *dev, void *data)
  305. {
  306. return dev->of_node == data;
  307. }
  308. static int hdlcd_probe(struct platform_device *pdev)
  309. {
  310. struct device_node *port;
  311. struct component_match *match = NULL;
  312. /* there is only one output port inside each device, find it */
  313. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  314. if (!port)
  315. return -ENODEV;
  316. drm_of_component_match_add(&pdev->dev, &match, compare_dev, port);
  317. of_node_put(port);
  318. return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
  319. match);
  320. }
  321. static int hdlcd_remove(struct platform_device *pdev)
  322. {
  323. component_master_del(&pdev->dev, &hdlcd_master_ops);
  324. return 0;
  325. }
  326. static const struct of_device_id hdlcd_of_match[] = {
  327. { .compatible = "arm,hdlcd" },
  328. {},
  329. };
  330. MODULE_DEVICE_TABLE(of, hdlcd_of_match);
  331. static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
  332. {
  333. struct drm_device *drm = dev_get_drvdata(dev);
  334. return drm_mode_config_helper_suspend(drm);
  335. }
  336. static int __maybe_unused hdlcd_pm_resume(struct device *dev)
  337. {
  338. struct drm_device *drm = dev_get_drvdata(dev);
  339. drm_mode_config_helper_resume(drm);
  340. return 0;
  341. }
  342. static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
  343. static struct platform_driver hdlcd_platform_driver = {
  344. .probe = hdlcd_probe,
  345. .remove = hdlcd_remove,
  346. .driver = {
  347. .name = "hdlcd",
  348. .pm = &hdlcd_pm_ops,
  349. .of_match_table = hdlcd_of_match,
  350. },
  351. };
  352. module_platform_driver(hdlcd_platform_driver);
  353. MODULE_AUTHOR("Liviu Dudau");
  354. MODULE_DESCRIPTION("ARM HDLCD DRM driver");
  355. MODULE_LICENSE("GPL v2");