hdlcd_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. if (hdlcd->fbdev)
  89. drm_fbdev_cma_hotplug_event(hdlcd->fbdev);
  90. }
  91. static int hdlcd_atomic_commit(struct drm_device *dev,
  92. struct drm_atomic_state *state, bool nonblock)
  93. {
  94. return drm_atomic_helper_commit(dev, state, false);
  95. }
  96. static const struct drm_mode_config_funcs hdlcd_mode_config_funcs = {
  97. .fb_create = drm_fb_cma_create,
  98. .output_poll_changed = hdlcd_fb_output_poll_changed,
  99. .atomic_check = drm_atomic_helper_check,
  100. .atomic_commit = hdlcd_atomic_commit,
  101. };
  102. static void hdlcd_setup_mode_config(struct drm_device *drm)
  103. {
  104. drm_mode_config_init(drm);
  105. drm->mode_config.min_width = 0;
  106. drm->mode_config.min_height = 0;
  107. drm->mode_config.max_width = HDLCD_MAX_XRES;
  108. drm->mode_config.max_height = HDLCD_MAX_YRES;
  109. drm->mode_config.funcs = &hdlcd_mode_config_funcs;
  110. }
  111. static void hdlcd_lastclose(struct drm_device *drm)
  112. {
  113. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  114. drm_fbdev_cma_restore_mode(hdlcd->fbdev);
  115. }
  116. static irqreturn_t hdlcd_irq(int irq, void *arg)
  117. {
  118. struct drm_device *drm = arg;
  119. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  120. unsigned long irq_status;
  121. irq_status = hdlcd_read(hdlcd, HDLCD_REG_INT_STATUS);
  122. #ifdef CONFIG_DEBUG_FS
  123. if (irq_status & HDLCD_INTERRUPT_UNDERRUN)
  124. atomic_inc(&hdlcd->buffer_underrun_count);
  125. if (irq_status & HDLCD_INTERRUPT_DMA_END)
  126. atomic_inc(&hdlcd->dma_end_count);
  127. if (irq_status & HDLCD_INTERRUPT_BUS_ERROR)
  128. atomic_inc(&hdlcd->bus_error_count);
  129. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  130. atomic_inc(&hdlcd->vsync_count);
  131. #endif
  132. if (irq_status & HDLCD_INTERRUPT_VSYNC)
  133. drm_crtc_handle_vblank(&hdlcd->crtc);
  134. /* acknowledge interrupt(s) */
  135. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, irq_status);
  136. return IRQ_HANDLED;
  137. }
  138. static void hdlcd_irq_preinstall(struct drm_device *drm)
  139. {
  140. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  141. /* Ensure interrupts are disabled */
  142. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, 0);
  143. hdlcd_write(hdlcd, HDLCD_REG_INT_CLEAR, ~0);
  144. }
  145. static int hdlcd_irq_postinstall(struct drm_device *drm)
  146. {
  147. #ifdef CONFIG_DEBUG_FS
  148. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  149. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  150. /* enable debug interrupts */
  151. irq_mask |= HDLCD_DEBUG_INT_MASK;
  152. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  153. #endif
  154. return 0;
  155. }
  156. static void hdlcd_irq_uninstall(struct drm_device *drm)
  157. {
  158. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  159. /* disable all the interrupts that we might have enabled */
  160. unsigned long irq_mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  161. #ifdef CONFIG_DEBUG_FS
  162. /* disable debug interrupts */
  163. irq_mask &= ~HDLCD_DEBUG_INT_MASK;
  164. #endif
  165. /* disable vsync interrupts */
  166. irq_mask &= ~HDLCD_INTERRUPT_VSYNC;
  167. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, irq_mask);
  168. }
  169. static int hdlcd_enable_vblank(struct drm_device *drm, unsigned int crtc)
  170. {
  171. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  172. unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  173. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask | HDLCD_INTERRUPT_VSYNC);
  174. return 0;
  175. }
  176. static void hdlcd_disable_vblank(struct drm_device *drm, unsigned int crtc)
  177. {
  178. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  179. unsigned int mask = hdlcd_read(hdlcd, HDLCD_REG_INT_MASK);
  180. hdlcd_write(hdlcd, HDLCD_REG_INT_MASK, mask & ~HDLCD_INTERRUPT_VSYNC);
  181. }
  182. #ifdef CONFIG_DEBUG_FS
  183. static int hdlcd_show_underrun_count(struct seq_file *m, void *arg)
  184. {
  185. struct drm_info_node *node = (struct drm_info_node *)m->private;
  186. struct drm_device *drm = node->minor->dev;
  187. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  188. seq_printf(m, "underrun : %d\n", atomic_read(&hdlcd->buffer_underrun_count));
  189. seq_printf(m, "dma_end : %d\n", atomic_read(&hdlcd->dma_end_count));
  190. seq_printf(m, "bus_error: %d\n", atomic_read(&hdlcd->bus_error_count));
  191. seq_printf(m, "vsync : %d\n", atomic_read(&hdlcd->vsync_count));
  192. return 0;
  193. }
  194. static int hdlcd_show_pxlclock(struct seq_file *m, void *arg)
  195. {
  196. struct drm_info_node *node = (struct drm_info_node *)m->private;
  197. struct drm_device *drm = node->minor->dev;
  198. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  199. unsigned long clkrate = clk_get_rate(hdlcd->clk);
  200. unsigned long mode_clock = hdlcd->crtc.mode.crtc_clock * 1000;
  201. seq_printf(m, "hw : %lu\n", clkrate);
  202. seq_printf(m, "mode: %lu\n", mode_clock);
  203. return 0;
  204. }
  205. static struct drm_info_list hdlcd_debugfs_list[] = {
  206. { "interrupt_count", hdlcd_show_underrun_count, 0 },
  207. { "clocks", hdlcd_show_pxlclock, 0 },
  208. { "fb", drm_fb_cma_debugfs_show, 0 },
  209. };
  210. static int hdlcd_debugfs_init(struct drm_minor *minor)
  211. {
  212. return drm_debugfs_create_files(hdlcd_debugfs_list,
  213. ARRAY_SIZE(hdlcd_debugfs_list), minor->debugfs_root, minor);
  214. }
  215. static void hdlcd_debugfs_cleanup(struct drm_minor *minor)
  216. {
  217. drm_debugfs_remove_files(hdlcd_debugfs_list,
  218. ARRAY_SIZE(hdlcd_debugfs_list), minor);
  219. }
  220. #endif
  221. static const struct file_operations fops = {
  222. .owner = THIS_MODULE,
  223. .open = drm_open,
  224. .release = drm_release,
  225. .unlocked_ioctl = drm_ioctl,
  226. #ifdef CONFIG_COMPAT
  227. .compat_ioctl = drm_compat_ioctl,
  228. #endif
  229. .poll = drm_poll,
  230. .read = drm_read,
  231. .llseek = noop_llseek,
  232. .mmap = drm_gem_cma_mmap,
  233. };
  234. static struct drm_driver hdlcd_driver = {
  235. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  236. DRIVER_MODESET | DRIVER_PRIME |
  237. DRIVER_ATOMIC,
  238. .lastclose = hdlcd_lastclose,
  239. .irq_handler = hdlcd_irq,
  240. .irq_preinstall = hdlcd_irq_preinstall,
  241. .irq_postinstall = hdlcd_irq_postinstall,
  242. .irq_uninstall = hdlcd_irq_uninstall,
  243. .get_vblank_counter = drm_vblank_no_hw_counter,
  244. .enable_vblank = hdlcd_enable_vblank,
  245. .disable_vblank = hdlcd_disable_vblank,
  246. .gem_free_object_unlocked = drm_gem_cma_free_object,
  247. .gem_vm_ops = &drm_gem_cma_vm_ops,
  248. .dumb_create = drm_gem_cma_dumb_create,
  249. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  250. .dumb_destroy = drm_gem_dumb_destroy,
  251. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  252. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  253. .gem_prime_export = drm_gem_prime_export,
  254. .gem_prime_import = drm_gem_prime_import,
  255. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  256. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  257. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  258. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  259. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  260. #ifdef CONFIG_DEBUG_FS
  261. .debugfs_init = hdlcd_debugfs_init,
  262. .debugfs_cleanup = hdlcd_debugfs_cleanup,
  263. #endif
  264. .fops = &fops,
  265. .name = "hdlcd",
  266. .desc = "ARM HDLCD Controller DRM",
  267. .date = "20151021",
  268. .major = 1,
  269. .minor = 0,
  270. };
  271. static int hdlcd_drm_bind(struct device *dev)
  272. {
  273. struct drm_device *drm;
  274. struct hdlcd_drm_private *hdlcd;
  275. int ret;
  276. hdlcd = devm_kzalloc(dev, sizeof(*hdlcd), GFP_KERNEL);
  277. if (!hdlcd)
  278. return -ENOMEM;
  279. drm = drm_dev_alloc(&hdlcd_driver, dev);
  280. if (!drm)
  281. return -ENOMEM;
  282. drm->dev_private = hdlcd;
  283. dev_set_drvdata(dev, drm);
  284. hdlcd_setup_mode_config(drm);
  285. ret = hdlcd_load(drm, 0);
  286. if (ret)
  287. goto err_free;
  288. ret = drm_dev_register(drm, 0);
  289. if (ret)
  290. goto err_unload;
  291. ret = component_bind_all(dev, drm);
  292. if (ret) {
  293. DRM_ERROR("Failed to bind all components\n");
  294. goto err_unregister;
  295. }
  296. ret = pm_runtime_set_active(dev);
  297. if (ret)
  298. goto err_pm_active;
  299. pm_runtime_enable(dev);
  300. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  301. if (ret < 0) {
  302. DRM_ERROR("failed to initialise vblank\n");
  303. goto err_vblank;
  304. }
  305. drm_mode_config_reset(drm);
  306. drm_kms_helper_poll_init(drm);
  307. hdlcd->fbdev = drm_fbdev_cma_init(drm, 32, drm->mode_config.num_crtc,
  308. drm->mode_config.num_connector);
  309. if (IS_ERR(hdlcd->fbdev)) {
  310. ret = PTR_ERR(hdlcd->fbdev);
  311. hdlcd->fbdev = NULL;
  312. goto err_fbdev;
  313. }
  314. return 0;
  315. err_fbdev:
  316. drm_kms_helper_poll_fini(drm);
  317. drm_mode_config_cleanup(drm);
  318. drm_vblank_cleanup(drm);
  319. err_vblank:
  320. pm_runtime_disable(drm->dev);
  321. err_pm_active:
  322. component_unbind_all(dev, drm);
  323. err_unregister:
  324. drm_dev_unregister(drm);
  325. err_unload:
  326. drm_irq_uninstall(drm);
  327. of_reserved_mem_device_release(drm->dev);
  328. err_free:
  329. dev_set_drvdata(dev, NULL);
  330. drm_dev_unref(drm);
  331. return ret;
  332. }
  333. static void hdlcd_drm_unbind(struct device *dev)
  334. {
  335. struct drm_device *drm = dev_get_drvdata(dev);
  336. struct hdlcd_drm_private *hdlcd = drm->dev_private;
  337. if (hdlcd->fbdev) {
  338. drm_fbdev_cma_fini(hdlcd->fbdev);
  339. hdlcd->fbdev = NULL;
  340. }
  341. drm_kms_helper_poll_fini(drm);
  342. component_unbind_all(dev, drm);
  343. drm_vblank_cleanup(drm);
  344. pm_runtime_get_sync(drm->dev);
  345. drm_irq_uninstall(drm);
  346. pm_runtime_put_sync(drm->dev);
  347. pm_runtime_disable(drm->dev);
  348. of_reserved_mem_device_release(drm->dev);
  349. drm_mode_config_cleanup(drm);
  350. drm_dev_unregister(drm);
  351. drm_dev_unref(drm);
  352. drm->dev_private = NULL;
  353. dev_set_drvdata(dev, NULL);
  354. }
  355. static const struct component_master_ops hdlcd_master_ops = {
  356. .bind = hdlcd_drm_bind,
  357. .unbind = hdlcd_drm_unbind,
  358. };
  359. static int compare_dev(struct device *dev, void *data)
  360. {
  361. return dev->of_node == data;
  362. }
  363. static int hdlcd_probe(struct platform_device *pdev)
  364. {
  365. struct device_node *port, *ep;
  366. struct component_match *match = NULL;
  367. if (!pdev->dev.of_node)
  368. return -ENODEV;
  369. /* there is only one output port inside each device, find it */
  370. ep = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
  371. if (!ep)
  372. return -ENODEV;
  373. if (!of_device_is_available(ep)) {
  374. of_node_put(ep);
  375. return -ENODEV;
  376. }
  377. /* add the remote encoder port as component */
  378. port = of_graph_get_remote_port_parent(ep);
  379. of_node_put(ep);
  380. if (!port || !of_device_is_available(port)) {
  381. of_node_put(port);
  382. return -EAGAIN;
  383. }
  384. component_match_add(&pdev->dev, &match, compare_dev, port);
  385. return component_master_add_with_match(&pdev->dev, &hdlcd_master_ops,
  386. match);
  387. }
  388. static int hdlcd_remove(struct platform_device *pdev)
  389. {
  390. component_master_del(&pdev->dev, &hdlcd_master_ops);
  391. return 0;
  392. }
  393. static const struct of_device_id hdlcd_of_match[] = {
  394. { .compatible = "arm,hdlcd" },
  395. {},
  396. };
  397. MODULE_DEVICE_TABLE(of, hdlcd_of_match);
  398. static int __maybe_unused hdlcd_pm_suspend(struct device *dev)
  399. {
  400. struct drm_device *drm = dev_get_drvdata(dev);
  401. struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
  402. if (!hdlcd)
  403. return 0;
  404. drm_kms_helper_poll_disable(drm);
  405. hdlcd->state = drm_atomic_helper_suspend(drm);
  406. if (IS_ERR(hdlcd->state)) {
  407. drm_kms_helper_poll_enable(drm);
  408. return PTR_ERR(hdlcd->state);
  409. }
  410. return 0;
  411. }
  412. static int __maybe_unused hdlcd_pm_resume(struct device *dev)
  413. {
  414. struct drm_device *drm = dev_get_drvdata(dev);
  415. struct hdlcd_drm_private *hdlcd = drm ? drm->dev_private : NULL;
  416. if (!hdlcd)
  417. return 0;
  418. drm_atomic_helper_resume(drm, hdlcd->state);
  419. drm_kms_helper_poll_enable(drm);
  420. pm_runtime_set_active(dev);
  421. return 0;
  422. }
  423. static SIMPLE_DEV_PM_OPS(hdlcd_pm_ops, hdlcd_pm_suspend, hdlcd_pm_resume);
  424. static struct platform_driver hdlcd_platform_driver = {
  425. .probe = hdlcd_probe,
  426. .remove = hdlcd_remove,
  427. .driver = {
  428. .name = "hdlcd",
  429. .pm = &hdlcd_pm_ops,
  430. .of_match_table = hdlcd_of_match,
  431. },
  432. };
  433. module_platform_driver(hdlcd_platform_driver);
  434. MODULE_AUTHOR("Liviu Dudau");
  435. MODULE_DESCRIPTION("ARM HDLCD DRM driver");
  436. MODULE_LICENSE("GPL v2");