hdlcd_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. .output_poll_changed = drm_fb_helper_output_poll_changed,
  91. .atomic_check = drm_atomic_helper_check,
  92. .atomic_commit = drm_atomic_helper_commit,
  93. };
  94. static void hdlcd_setup_mode_config(struct drm_device *drm)
  95. {
  96. drm_mode_config_init(drm);
  97. drm->mode_config.min_width = 0;
  98. drm->mode_config.min_height = 0;
  99. drm->mode_config.max_width = HDLCD_MAX_XRES;
  100. drm->mode_config.max_height = HDLCD_MAX_YRES;
  101. drm->mode_config.funcs = &hdlcd_mode_config_funcs;
  102. }
  103. static irqreturn_t hdlcd_irq(int irq, void *arg)
  104. {
  105. struct drm_device *drm = arg;
  106. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  107. unsigned long irq_status;
  108. irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
  109. #ifdef CONFIG_DEBUG_FS
  110. if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
  111. atomic_inc(&hdlcd->buffer_underrun_count);
  112. if (irq_status & HDLCD_INTERRUPT_DMA_END)
  113. atomic_inc(&hdlcd->dma_end_count);
  114. if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
  115. atomic_inc(&hdlcd->bus_error_count);
  116. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  117. atomic_inc(&hdlcd->vsync_count);
  118. #endif
  119. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  120. drm_crtc_handle_vblank(&hdlcd->crtc);
  121. /* acknowledge interrupt(s) */
  122. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
  123. return IRQ_HANDLED;
  124. }
  125. static void hdlcd_irq_preinstall(struct drm_device *drm)
  126. {
  127. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  128. /* Ensure interrupts are disabled */
  129. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
  130. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
  131. }
  132. static int hdlcd_irq_postinstall(struct drm_device *drm)
  133. {
  134. #ifdef CONFIG_DEBUG_FS
  135. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  136. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  137. /* enable debug interrupts */
  138. irq_mask |= HDLCD_DEBUG_INT_MASK;
  139. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  140. #endif
  141. return 0;
  142. }
  143. static void hdlcd_irq_uninstall(struct drm_device *drm)
  144. {
  145. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  146. /* disable all the interrupts that we might have enabled */
  147. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  148. #ifdef CONFIG_DEBUG_FS
  149. /* disable debug interrupts */
  150. irq_mask &= ~HDLCD_DEBUG_INT_MASK;
  151. #endif
  152. /* disable vsync interrupts */
  153. irq_mask &= ~HDLCD_INTERRUPT_VSYNC;
  154. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  155. }
  156. #ifdef CONFIG_DEBUG_FS
  157. static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
  158. {
  159. struct drm_info_node *node = (struct drm_info_node *)m->private;
  160. struct drm_device *drm = node->minor->dev;
  161. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  162. seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
  163. seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
  164. seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
  165. seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
  166. return 0;
  167. }
  168. static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
  169. {
  170. struct drm_info_node *node = (struct drm_info_node *)m->private;
  171. struct drm_device *drm = node->minor->dev;
  172. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  173. unsigned long clkrate = clk_get_rate(hdlcd->clk);
  174. unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
  175. seq_printf(m, "hw : %lu\n", clkrate);
  176. seq_printf(m, "mode: %lu\n", mode_clock);
  177. return 0;
  178. }
  179. static struct drm_info_list hdlcd_debugfs_list[] = {
  180. { "interrupt_count", hdlcd_show_underrun_count, 0 },
  181. { "clocks", hdlcd_show_pxlclock, 0 },
  182. };
  183. static int hdlcd_debugfs_init(struct drm_minor *minor)
  184. {
  185. return drm_debugfs_create_files(hdlcd_debugfs_list,
  186. ARRAY_SIZE(hdlcd_debugfs_list), minor->debugfs_root, minor);
  187. }
  188. #endif
  189. DEFINE_DRM_GEM_CMA_FOPS(fops);
  190. static struct drm_driver hdlcd_driver = {
  191. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  192. DRIVER_MODESET | DRIVER_PRIME |
  193. DRIVER_ATOMIC,
  194. .lastclose = drm_fb_helper_lastclose,
  195. .irq_handler = hdlcd_irq,
  196. .irq_preinstall = hdlcd_irq_preinstall,
  197. .irq_postinstall = hdlcd_irq_postinstall,
  198. .irq_uninstall = hdlcd_irq_uninstall,
  199. .gem_free_object_unlocked = drm_gem_cma_free_object,
  200. .gem_print_info = drm_gem_cma_print_info,
  201. .gem_vm_ops = &drm_gem_cma_vm_ops,
  202. .dumb_create = drm_gem_cma_dumb_create,
  203. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  204. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  205. .gem_prime_export = drm_gem_prime_export,
  206. .gem_prime_import = drm_gem_prime_import,
  207. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  208. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  209. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  210. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  211. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  212. #ifdef CONFIG_DEBUG_FS
  213. .debugfs_init = hdlcd_debugfs_init,
  214. #endif
  215. .fops = &fops,
  216. .name = "hdlcd",
  217. .desc = "ARM HDLCD Controller DRM",
  218. .date = "20151021",
  219. .major = 1,
  220. .minor = 0,
  221. };
  222. static int hdlcd_drm_bind(struct device *dev)
  223. {
  224. struct drm_device *drm;
  225. struct hdlcd_drm_private *hdlcd;
  226. int ret;
  227. hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
  228. if (!hdlcd)
  229. return -ENOMEM;
  230. drm = drm_dev_alloc(&hdlcd_driver, dev);
  231. if (IS_ERR(drm))
  232. return PTR_ERR(drm);
  233. drm->dev_private = hdlcd;
  234. dev_set_drvdata(dev, drm);
  235. hdlcd_setup_mode_config(drm);
  236. ret = hdlcd_load(drm, 0);
  237. if (ret)
  238. goto err_free;
  239. /* Set the CRTC's port so that the encoder component can find it */
  240. hdlcd->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
  241. ret = component_bind_all(dev, drm);
  242. if (ret) {
  243. DRM_ERROR("Failed to bind all components\n");
  244. goto err_unload;
  245. }
  246. ret = pm_runtime_set_active(dev);
  247. if (ret)
  248. goto err_pm_active;
  249. pm_runtime_enable(dev);
  250. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  251. if (ret < 0) {
  252. DRM_ERROR("failed to initialise vblank\n");
  253. goto err_vblank;
  254. }
  255. drm_mode_config_reset(drm);
  256. drm_kms_helper_poll_init(drm);
  257. ret = drm_fb_cma_fbdev_init(drm, 32, 0);
  258. if (ret)
  259. goto err_fbdev;
  260. ret = drm_dev_register(drm, 0);
  261. if (ret)
  262. goto err_register;
  263. return 0;
  264. err_register:
  265. drm_fb_cma_fbdev_fini(drm);
  266. err_fbdev:
  267. drm_kms_helper_poll_fini(drm);
  268. err_vblank:
  269. pm_runtime_disable(drm->dev);
  270. err_pm_active:
  271. drm_atomic_helper_shutdown(drm);
  272. component_unbind_all(dev, drm);
  273. err_unload:
  274. of_node_put(hdlcd->crtc.port);
  275. hdlcd->crtc.port = NULL;
  276. drm_irq_uninstall(drm);
  277. of_reserved_mem_device_release(drm->dev);
  278. err_free:
  279. drm_mode_config_cleanup(drm);
  280. dev_set_drvdata(dev, NULL);
  281. drm_dev_put(drm);
  282. return ret;
  283. }
  284. static void hdlcd_drm_unbind(struct device *dev)
  285. {
  286. struct drm_device *drm = dev_get_drvdata(dev);
  287. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  288. drm_dev_unregister(drm);
  289. drm_fb_cma_fbdev_fini(drm);
  290. drm_kms_helper_poll_fini(drm);
  291. component_unbind_all(dev, drm);
  292. of_node_put(hdlcd->crtc.port);
  293. hdlcd->crtc.port = NULL;
  294. pm_runtime_get_sync(dev);
  295. drm_crtc_vblank_off(&hdlcd->crtc);
  296. drm_irq_uninstall(drm);
  297. drm_atomic_helper_shutdown(drm);
  298. pm_runtime_put(dev);
  299. if (pm_runtime_enabled(dev))
  300. pm_runtime_disable(dev);
  301. of_reserved_mem_device_release(dev);
  302. drm_mode_config_cleanup(drm);
  303. drm->dev_private = NULL;
  304. dev_set_drvdata(dev, NULL);
  305. drm_dev_put(drm);
  306. }
  307. static const struct component_master_ops hdlcd_master_ops = {
  308. .bind = hdlcd_drm_bind,
  309. .unbind = hdlcd_drm_unbind,
  310. };
  311. static int compare_dev(struct device *dev, void *data)
  312. {
  313. return dev->of_node == data;
  314. }
  315. static int hdlcd_probe(struct platform_device *pdev)
  316. {
  317. struct device_node *port;
  318. struct component_match *match = NULL;
  319. /* there is only one output port inside each device, find it */
  320. port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
  321. if (!port)
  322. return -ENODEV;
  323. drm_of_component_match_add(&pdev->dev, &match, compare_dev, port);
  324. of_node_put(port);
  325. return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
  326. match);
  327. }
  328. static int hdlcd_remove(struct platform_device *pdev)
  329. {
  330. component_master_del(&pdev->dev, &hdlcd_master_ops);
  331. return 0;
  332. }
  333. static const struct of_device_id hdlcd_of_match[] = {
  334. { .compatible = "arm,hdlcd" },
  335. {},
  336. };
  337. MODULE_DEVICE_TABLE(of, hdlcd_of_match);
  338. static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
  339. {
  340. struct drm_device *drm = dev_get_drvdata(dev);
  341. return drm_mode_config_helper_suspend(drm);
  342. }
  343. static int __maybe_unused hdlcd_pm_resume(struct device *dev)
  344. {
  345. struct drm_device *drm = dev_get_drvdata(dev);
  346. drm_mode_config_helper_resume(drm);
  347. return 0;
  348. }
  349. static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
  350. static struct platform_driver hdlcd_platform_driver = {
  351. .probe = hdlcd_probe,
  352. .remove = hdlcd_remove,
  353. .driver = {
  354. .name = "hdlcd",
  355. .pm = &hdlcd_pm_ops,
  356. .of_match_table = hdlcd_of_match,
  357. },
  358. };
  359. module_platform_driver(hdlcd_platform_driver);
  360. MODULE_AUTHOR("Liviu Dudau");
  361. MODULE_DESCRIPTION("ARM HDLCD DRM driver");
  362. MODULE_LICENSE("GPL v2");