armada_drv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Russell King
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/component.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_graph.h>
  10. #include <linux/platform_device.h>
  11. #include <drm/drm_aperture.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_ioctl.h>
  15. #include <drm/drm_managed.h>
  16. #include <drm/drm_prime.h>
  17. #include <drm/drm_probe_helper.h>
  18. #include <drm/drm_of.h>
  19. #include <drm/drm_vblank.h>
  20. #include "armada_crtc.h"
  21. #include "armada_drm.h"
  22. #include "armada_gem.h"
  23. #include "armada_fb.h"
  24. #include "armada_hw.h"
  25. #include <drm/armada_drm.h>
  26. #include "armada_ioctlP.h"
  27. static const struct drm_ioctl_desc armada_ioctls[] = {
  28. DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
  29. DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
  30. DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
  31. };
  32. DEFINE_DRM_GEM_FOPS(armada_drm_fops);
  33. static const struct drm_driver armada_drm_driver = {
  34. .gem_prime_import = armada_gem_prime_import,
  35. .dumb_create = armada_gem_dumb_create,
  36. .major = 1,
  37. .minor = 0,
  38. .name = "armada-drm",
  39. .desc = "Armada SoC DRM",
  40. .date = "20120730",
  41. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  42. .ioctls = armada_ioctls,
  43. .num_ioctls = ARRAY_SIZE(armada_ioctls),
  44. .fops = &armada_drm_fops,
  45. };
  46. static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
  47. .fb_create = armada_fb_create,
  48. .atomic_check = drm_atomic_helper_check,
  49. .atomic_commit = drm_atomic_helper_commit,
  50. };
  51. static int armada_drm_bind(struct device *dev)
  52. {
  53. struct armada_private *priv;
  54. struct resource *mem = NULL;
  55. int ret, n;
  56. for (n = 0; ; n++) {
  57. struct resource *r = platform_get_resource(to_platform_device(dev),
  58. IORESOURCE_MEM, n);
  59. if (!r)
  60. break;
  61. /* Resources above 64K are graphics memory */
  62. if (resource_size(r) > SZ_64K)
  63. mem = r;
  64. else
  65. return -EINVAL;
  66. }
  67. if (!mem)
  68. return -ENXIO;
  69. if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
  70. "armada-drm"))
  71. return -EBUSY;
  72. priv = devm_drm_dev_alloc(dev, &armada_drm_driver,
  73. struct armada_private, drm);
  74. if (IS_ERR(priv)) {
  75. dev_err(dev, "[" DRM_NAME ":%s] devm_drm_dev_alloc failed: %li\n",
  76. __func__, PTR_ERR(priv));
  77. return PTR_ERR(priv);
  78. }
  79. /* Remove early framebuffers */
  80. ret = drm_aperture_remove_framebuffers(&armada_drm_driver);
  81. if (ret) {
  82. dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
  83. __func__, ret);
  84. return ret;
  85. }
  86. dev_set_drvdata(dev, &priv->drm);
  87. /* Mode setting support */
  88. drm_mode_config_init(&priv->drm);
  89. priv->drm.mode_config.min_width = 320;
  90. priv->drm.mode_config.min_height = 200;
  91. /*
  92. * With vscale enabled, the maximum width is 1920 due to the
  93. * 1920 by 3 lines RAM
  94. */
  95. priv->drm.mode_config.max_width = 1920;
  96. priv->drm.mode_config.max_height = 2048;
  97. priv->drm.mode_config.preferred_depth = 24;
  98. priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
  99. drm_mm_init(&priv->linear, mem->start, resource_size(mem));
  100. mutex_init(&priv->linear_lock);
  101. ret = component_bind_all(dev, &priv->drm);
  102. if (ret)
  103. goto err_kms;
  104. ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
  105. if (ret)
  106. goto err_comp;
  107. drm_mode_config_reset(&priv->drm);
  108. drm_kms_helper_poll_init(&priv->drm);
  109. ret = drm_dev_register(&priv->drm, 0);
  110. if (ret)
  111. goto err_poll;
  112. #ifdef CONFIG_DEBUG_FS
  113. armada_drm_debugfs_init(priv->drm.primary);
  114. #endif
  115. armada_fbdev_setup(&priv->drm);
  116. return 0;
  117. err_poll:
  118. drm_kms_helper_poll_fini(&priv->drm);
  119. err_comp:
  120. component_unbind_all(dev, &priv->drm);
  121. err_kms:
  122. drm_mode_config_cleanup(&priv->drm);
  123. drm_mm_takedown(&priv->linear);
  124. dev_set_drvdata(dev, NULL);
  125. return ret;
  126. }
  127. static void armada_drm_unbind(struct device *dev)
  128. {
  129. struct drm_device *drm = dev_get_drvdata(dev);
  130. struct armada_private *priv = drm_to_armada_dev(drm);
  131. drm_kms_helper_poll_fini(&priv->drm);
  132. drm_dev_unregister(&priv->drm);
  133. drm_atomic_helper_shutdown(&priv->drm);
  134. component_unbind_all(dev, &priv->drm);
  135. drm_mode_config_cleanup(&priv->drm);
  136. drm_mm_takedown(&priv->linear);
  137. dev_set_drvdata(dev, NULL);
  138. }
  139. static void armada_add_endpoints(struct device *dev,
  140. struct component_match **match, struct device_node *dev_node)
  141. {
  142. struct device_node *ep, *remote;
  143. for_each_endpoint_of_node(dev_node, ep) {
  144. remote = of_graph_get_remote_port_parent(ep);
  145. if (remote && of_device_is_available(remote))
  146. drm_of_component_match_add(dev, match, component_compare_of,
  147. remote);
  148. of_node_put(remote);
  149. }
  150. }
  151. static const struct component_master_ops armada_master_ops = {
  152. .bind = armada_drm_bind,
  153. .unbind = armada_drm_unbind,
  154. };
  155. static int armada_drm_probe(struct platform_device *pdev)
  156. {
  157. struct component_match *match = NULL;
  158. struct device *dev = &pdev->dev;
  159. int ret;
  160. ret = drm_of_component_probe(dev, component_compare_dev_name, &armada_master_ops);
  161. if (ret != -EINVAL)
  162. return ret;
  163. if (dev->platform_data) {
  164. char **devices = dev->platform_data;
  165. struct device *d;
  166. int i;
  167. for (i = 0; devices[i]; i++)
  168. component_match_add(dev, &match, component_compare_dev_name,
  169. devices[i]);
  170. if (i == 0) {
  171. dev_err(dev, "missing 'ports' property\n");
  172. return -ENODEV;
  173. }
  174. for (i = 0; devices[i]; i++) {
  175. d = bus_find_device_by_name(&platform_bus_type, NULL,
  176. devices[i]);
  177. if (d && d->of_node)
  178. armada_add_endpoints(dev, &match, d->of_node);
  179. put_device(d);
  180. }
  181. }
  182. return component_master_add_with_match(&pdev->dev, &armada_master_ops,
  183. match);
  184. }
  185. static void armada_drm_remove(struct platform_device *pdev)
  186. {
  187. component_master_del(&pdev->dev, &armada_master_ops);
  188. }
  189. static void armada_drm_shutdown(struct platform_device *pdev)
  190. {
  191. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  192. }
  193. static const struct platform_device_id armada_drm_platform_ids[] = {
  194. {
  195. .name = "armada-drm",
  196. }, {
  197. .name = "armada-510-drm",
  198. },
  199. { },
  200. };
  201. MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
  202. static struct platform_driver armada_drm_platform_driver = {
  203. .probe = armada_drm_probe,
  204. .remove_new = armada_drm_remove,
  205. .shutdown = armada_drm_shutdown,
  206. .driver = {
  207. .name = "armada-drm",
  208. },
  209. .id_table = armada_drm_platform_ids,
  210. };
  211. static int __init armada_drm_init(void)
  212. {
  213. int ret;
  214. if (drm_firmware_drivers_only())
  215. return -ENODEV;
  216. ret = platform_driver_register(&armada_lcd_platform_driver);
  217. if (ret)
  218. return ret;
  219. ret = platform_driver_register(&armada_drm_platform_driver);
  220. if (ret)
  221. platform_driver_unregister(&armada_lcd_platform_driver);
  222. return ret;
  223. }
  224. module_init(armada_drm_init);
  225. static void __exit armada_drm_exit(void)
  226. {
  227. platform_driver_unregister(&armada_drm_platform_driver);
  228. platform_driver_unregister(&armada_lcd_platform_driver);
  229. }
  230. module_exit(armada_drm_exit);
  231. MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
  232. MODULE_DESCRIPTION("Armada DRM Driver");
  233. MODULE_LICENSE("GPL");
  234. MODULE_ALIAS("platform:armada-drm");