hdlcd_drv.c 14 KB

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