arcpgu_drv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * ARC PGU DRM driver.
  3. *
  4. * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/clk.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <linux/of_reserved_mem.h>
  22. #include "arcpgu.h"
  23. #include "arcpgu_regs.h"
  24. static void arcpgu_fb_output_poll_changed(struct drm_device *dev)
  25. {
  26. struct arcpgu_drm_private *arcpgu = dev->dev_private;
  27. drm_fbdev_cma_hotplug_event(arcpgu->fbdev);
  28. }
  29. static struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = {
  30. .fb_create = drm_fb_cma_create,
  31. .output_poll_changed = arcpgu_fb_output_poll_changed,
  32. .atomic_check = drm_atomic_helper_check,
  33. .atomic_commit = drm_atomic_helper_commit,
  34. };
  35. static void arcpgu_setup_mode_config(struct drm_device *drm)
  36. {
  37. drm_mode_config_init(drm);
  38. drm->mode_config.min_width = 0;
  39. drm->mode_config.min_height = 0;
  40. drm->mode_config.max_width = 1920;
  41. drm->mode_config.max_height = 1080;
  42. drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs;
  43. }
  44. static int arcpgu_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  45. {
  46. int ret;
  47. ret = drm_gem_mmap(filp, vma);
  48. if (ret)
  49. return ret;
  50. vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
  51. return 0;
  52. }
  53. static const struct file_operations arcpgu_drm_ops = {
  54. .owner = THIS_MODULE,
  55. .open = drm_open,
  56. .release = drm_release,
  57. .unlocked_ioctl = drm_ioctl,
  58. .compat_ioctl = drm_compat_ioctl,
  59. .poll = drm_poll,
  60. .read = drm_read,
  61. .llseek = no_llseek,
  62. .mmap = arcpgu_gem_mmap,
  63. };
  64. static void arcpgu_lastclose(struct drm_device *drm)
  65. {
  66. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  67. drm_fbdev_cma_restore_mode(arcpgu->fbdev);
  68. }
  69. static int arcpgu_load(struct drm_device *drm)
  70. {
  71. struct platform_device *pdev = to_platform_device(drm->dev);
  72. struct arcpgu_drm_private *arcpgu;
  73. struct device_node *encoder_node;
  74. struct resource *res;
  75. int ret;
  76. arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL);
  77. if (arcpgu == NULL)
  78. return -ENOMEM;
  79. drm->dev_private = arcpgu;
  80. arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
  81. if (IS_ERR(arcpgu->clk))
  82. return PTR_ERR(arcpgu->clk);
  83. arcpgu_setup_mode_config(drm);
  84. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  85. arcpgu->regs = devm_ioremap_resource(&pdev->dev, res);
  86. if (IS_ERR(arcpgu->regs))
  87. return PTR_ERR(arcpgu->regs);
  88. dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
  89. arc_pgu_read(arcpgu, ARCPGU_REG_ID));
  90. /* Get the optional framebuffer memory resource */
  91. ret = of_reserved_mem_device_init(drm->dev);
  92. if (ret && ret != -ENODEV)
  93. return ret;
  94. if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
  95. return -ENODEV;
  96. if (arc_pgu_setup_crtc(drm) < 0)
  97. return -ENODEV;
  98. /* find the encoder node and initialize it */
  99. encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0);
  100. if (encoder_node) {
  101. ret = arcpgu_drm_hdmi_init(drm, encoder_node);
  102. of_node_put(encoder_node);
  103. if (ret < 0)
  104. return ret;
  105. } else {
  106. ret = arcpgu_drm_sim_init(drm, NULL);
  107. if (ret < 0)
  108. return ret;
  109. }
  110. drm_mode_config_reset(drm);
  111. drm_kms_helper_poll_init(drm);
  112. arcpgu->fbdev = drm_fbdev_cma_init(drm, 16,
  113. drm->mode_config.num_connector);
  114. if (IS_ERR(arcpgu->fbdev)) {
  115. ret = PTR_ERR(arcpgu->fbdev);
  116. arcpgu->fbdev = NULL;
  117. return -ENODEV;
  118. }
  119. platform_set_drvdata(pdev, arcpgu);
  120. return 0;
  121. }
  122. static int arcpgu_unload(struct drm_device *drm)
  123. {
  124. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  125. if (arcpgu->fbdev) {
  126. drm_fbdev_cma_fini(arcpgu->fbdev);
  127. arcpgu->fbdev = NULL;
  128. }
  129. drm_kms_helper_poll_fini(drm);
  130. drm_vblank_cleanup(drm);
  131. drm_mode_config_cleanup(drm);
  132. return 0;
  133. }
  134. static struct drm_driver arcpgu_drm_driver = {
  135. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  136. DRIVER_ATOMIC,
  137. .lastclose = arcpgu_lastclose,
  138. .name = "drm-arcpgu",
  139. .desc = "ARC PGU Controller",
  140. .date = "20160219",
  141. .major = 1,
  142. .minor = 0,
  143. .patchlevel = 0,
  144. .fops = &arcpgu_drm_ops,
  145. .dumb_create = drm_gem_cma_dumb_create,
  146. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  147. .dumb_destroy = drm_gem_dumb_destroy,
  148. .get_vblank_counter = drm_vblank_no_hw_counter,
  149. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  150. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  151. .gem_free_object_unlocked = drm_gem_cma_free_object,
  152. .gem_vm_ops = &drm_gem_cma_vm_ops,
  153. .gem_prime_export = drm_gem_prime_export,
  154. .gem_prime_import = drm_gem_prime_import,
  155. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  156. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  157. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  158. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  159. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  160. };
  161. static int arcpgu_probe(struct platform_device *pdev)
  162. {
  163. struct drm_device *drm;
  164. int ret;
  165. drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
  166. if (IS_ERR(drm))
  167. return PTR_ERR(drm);
  168. ret = arcpgu_load(drm);
  169. if (ret)
  170. goto err_unref;
  171. ret = drm_dev_register(drm, 0);
  172. if (ret)
  173. goto err_unload;
  174. return 0;
  175. err_unload:
  176. arcpgu_unload(drm);
  177. err_unref:
  178. drm_dev_unref(drm);
  179. return ret;
  180. }
  181. static int arcpgu_remove(struct platform_device *pdev)
  182. {
  183. struct drm_device *drm = platform_get_drvdata(pdev);
  184. drm_dev_unregister(drm);
  185. arcpgu_unload(drm);
  186. drm_dev_unref(drm);
  187. return 0;
  188. }
  189. static const struct of_device_id arcpgu_of_table[] = {
  190. {.compatible = "snps,arcpgu"},
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, arcpgu_of_table);
  194. static struct platform_driver arcpgu_platform_driver = {
  195. .probe = arcpgu_probe,
  196. .remove = arcpgu_remove,
  197. .driver = {
  198. .name = "arcpgu",
  199. .of_match_table = arcpgu_of_table,
  200. },
  201. };
  202. module_platform_driver(arcpgu_platform_driver);
  203. MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
  204. MODULE_DESCRIPTION("ARC PGU DRM driver");
  205. MODULE_LICENSE("GPL");