hdlcd_drv.c 14 KB

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