arcpgu_drv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_crtc,
  114. drm->mode_config.num_connector);
  115. if (IS_ERR(arcpgu->fbdev)) {
  116. ret = PTR_ERR(arcpgu->fbdev);
  117. arcpgu->fbdev = NULL;
  118. return -ENODEV;
  119. }
  120. platform_set_drvdata(pdev, arcpgu);
  121. return 0;
  122. }
  123. static int arcpgu_unload(struct drm_device *drm)
  124. {
  125. struct arcpgu_drm_private *arcpgu = drm->dev_private;
  126. if (arcpgu->fbdev) {
  127. drm_fbdev_cma_fini(arcpgu->fbdev);
  128. arcpgu->fbdev = NULL;
  129. }
  130. drm_kms_helper_poll_fini(drm);
  131. drm_vblank_cleanup(drm);
  132. drm_mode_config_cleanup(drm);
  133. return 0;
  134. }
  135. static struct drm_driver arcpgu_drm_driver = {
  136. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  137. DRIVER_ATOMIC,
  138. .lastclose = arcpgu_lastclose,
  139. .name = "drm-arcpgu",
  140. .desc = "ARC PGU Controller",
  141. .date = "20160219",
  142. .major = 1,
  143. .minor = 0,
  144. .patchlevel = 0,
  145. .fops = &arcpgu_drm_ops,
  146. .dumb_create = drm_gem_cma_dumb_create,
  147. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  148. .dumb_destroy = drm_gem_dumb_destroy,
  149. .get_vblank_counter = drm_vblank_no_hw_counter,
  150. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  151. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  152. .gem_free_object_unlocked = drm_gem_cma_free_object,
  153. .gem_vm_ops = &drm_gem_cma_vm_ops,
  154. .gem_prime_export = drm_gem_prime_export,
  155. .gem_prime_import = drm_gem_prime_import,
  156. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  157. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  158. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  159. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  160. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  161. };
  162. static int arcpgu_probe(struct platform_device *pdev)
  163. {
  164. struct drm_device *drm;
  165. int ret;
  166. drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
  167. if (IS_ERR(drm))
  168. return PTR_ERR(drm);
  169. ret = arcpgu_load(drm);
  170. if (ret)
  171. goto err_unref;
  172. ret = drm_dev_register(drm, 0);
  173. if (ret)
  174. goto err_unload;
  175. return 0;
  176. err_unload:
  177. arcpgu_unload(drm);
  178. err_unref:
  179. drm_dev_unref(drm);
  180. return ret;
  181. }
  182. static int arcpgu_remove(struct platform_device *pdev)
  183. {
  184. struct drm_device *drm = platform_get_drvdata(pdev);
  185. drm_dev_unregister(drm);
  186. arcpgu_unload(drm);
  187. drm_dev_unref(drm);
  188. return 0;
  189. }
  190. static const struct of_device_id arcpgu_of_table[] = {
  191. {.compatible = "snps,arcpgu"},
  192. {}
  193. };
  194. MODULE_DEVICE_TABLE(of, arcpgu_of_table);
  195. static struct platform_driver arcpgu_platform_driver = {
  196. .probe = arcpgu_probe,
  197. .remove = arcpgu_remove,
  198. .driver = {
  199. .name = "arcpgu",
  200. .of_match_table = arcpgu_of_table,
  201. },
  202. };
  203. module_platform_driver(arcpgu_platform_driver);
  204. MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
  205. MODULE_DESCRIPTION("ARC PGU DRM driver");
  206. MODULE_LICENSE("GPL");