hdlcd_drv.c 12 KB

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