hdlcd_drv.c 12 KB

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