sti_drv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
  5. */
  6. #include <linux/component.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <drm/clients/drm_client_setup.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_debugfs.h>
  18. #include <drm/drm_drv.h>
  19. #include <drm/drm_fbdev_dma.h>
  20. #include <drm/drm_gem_dma_helper.h>
  21. #include <drm/drm_gem_framebuffer_helper.h>
  22. #include <drm/drm_of.h>
  23. #include <drm/drm_print.h>
  24. #include <drm/drm_probe_helper.h>
  25. #include "sti_drv.h"
  26. #include "sti_plane.h"
  27. #define DRIVER_NAME "sti"
  28. #define DRIVER_DESC "STMicroelectronics SoC DRM"
  29. #define DRIVER_MAJOR 1
  30. #define DRIVER_MINOR 0
  31. #define STI_MAX_FB_HEIGHT 4096
  32. #define STI_MAX_FB_WIDTH 4096
  33. static int sti_drm_fps_get(void *data, u64 *val)
  34. {
  35. struct drm_device *drm_dev = data;
  36. struct drm_plane *p;
  37. unsigned int i = 0;
  38. *val = 0;
  39. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  40. struct sti_plane *plane = to_sti_plane(p);
  41. *val |= plane->fps_info.output << i;
  42. i++;
  43. }
  44. return 0;
  45. }
  46. static int sti_drm_fps_set(void *data, u64 val)
  47. {
  48. struct drm_device *drm_dev = data;
  49. struct drm_plane *p;
  50. unsigned int i = 0;
  51. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  52. struct sti_plane *plane = to_sti_plane(p);
  53. memset(&plane->fps_info, 0, sizeof(plane->fps_info));
  54. plane->fps_info.output = (val >> i) & 1;
  55. i++;
  56. }
  57. return 0;
  58. }
  59. DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
  60. sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
  61. static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
  62. {
  63. struct drm_info_node *node = s->private;
  64. struct drm_device *dev = node->minor->dev;
  65. struct drm_plane *p;
  66. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  67. struct sti_plane *plane = to_sti_plane(p);
  68. seq_printf(s, "%s%s\n",
  69. plane->fps_info.fps_str,
  70. plane->fps_info.fips_str);
  71. }
  72. return 0;
  73. }
  74. static struct drm_info_list sti_drm_dbg_list[] = {
  75. {"fps_get", sti_drm_fps_dbg_show, 0},
  76. };
  77. static void sti_drm_dbg_init(struct drm_minor *minor)
  78. {
  79. drm_debugfs_create_files(sti_drm_dbg_list,
  80. ARRAY_SIZE(sti_drm_dbg_list),
  81. minor->debugfs_root, minor);
  82. debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
  83. minor->dev, &sti_drm_fps_fops);
  84. DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
  85. }
  86. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  87. .fb_create = drm_gem_fb_create,
  88. .atomic_check = drm_atomic_helper_check,
  89. .atomic_commit = drm_atomic_helper_commit,
  90. };
  91. static void sti_mode_config_init(struct drm_device *dev)
  92. {
  93. dev->mode_config.min_width = 0;
  94. dev->mode_config.min_height = 0;
  95. /*
  96. * set max width and height as default value.
  97. * this value would be used to check framebuffer size limitation
  98. * at drm_mode_addfb().
  99. */
  100. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  101. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  102. dev->mode_config.funcs = &sti_mode_config_funcs;
  103. dev->mode_config.normalize_zpos = true;
  104. }
  105. DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
  106. static const struct drm_driver sti_driver = {
  107. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  108. .fops = &sti_driver_fops,
  109. DRM_GEM_DMA_DRIVER_OPS,
  110. DRM_FBDEV_DMA_DRIVER_OPS,
  111. .debugfs_init = sti_drm_dbg_init,
  112. .name = DRIVER_NAME,
  113. .desc = DRIVER_DESC,
  114. .major = DRIVER_MAJOR,
  115. .minor = DRIVER_MINOR,
  116. };
  117. static int sti_init(struct drm_device *ddev)
  118. {
  119. struct sti_private *private;
  120. private = kzalloc_obj(*private);
  121. if (!private)
  122. return -ENOMEM;
  123. ddev->dev_private = (void *)private;
  124. dev_set_drvdata(ddev->dev, ddev);
  125. private->drm_dev = ddev;
  126. drm_mode_config_init(ddev);
  127. sti_mode_config_init(ddev);
  128. drm_kms_helper_poll_init(ddev);
  129. return 0;
  130. }
  131. static void sti_cleanup(struct drm_device *ddev)
  132. {
  133. struct sti_private *private = ddev->dev_private;
  134. drm_kms_helper_poll_fini(ddev);
  135. drm_atomic_helper_shutdown(ddev);
  136. drm_mode_config_cleanup(ddev);
  137. component_unbind_all(ddev->dev, ddev);
  138. dev_set_drvdata(ddev->dev, NULL);
  139. kfree(private);
  140. ddev->dev_private = NULL;
  141. }
  142. static int sti_bind(struct device *dev)
  143. {
  144. struct drm_device *ddev;
  145. int ret;
  146. ddev = drm_dev_alloc(&sti_driver, dev);
  147. if (IS_ERR(ddev))
  148. return PTR_ERR(ddev);
  149. ret = sti_init(ddev);
  150. if (ret)
  151. goto err_drm_dev_put;
  152. ret = component_bind_all(ddev->dev, ddev);
  153. if (ret)
  154. goto err_cleanup;
  155. ret = drm_dev_register(ddev, 0);
  156. if (ret)
  157. goto err_cleanup;
  158. drm_mode_config_reset(ddev);
  159. drm_client_setup(ddev, NULL);
  160. return 0;
  161. err_cleanup:
  162. sti_cleanup(ddev);
  163. err_drm_dev_put:
  164. drm_dev_put(ddev);
  165. return ret;
  166. }
  167. static void sti_unbind(struct device *dev)
  168. {
  169. struct drm_device *ddev = dev_get_drvdata(dev);
  170. drm_dev_unregister(ddev);
  171. sti_cleanup(ddev);
  172. drm_dev_put(ddev);
  173. }
  174. static const struct component_master_ops sti_ops = {
  175. .bind = sti_bind,
  176. .unbind = sti_unbind,
  177. };
  178. static int sti_platform_probe(struct platform_device *pdev)
  179. {
  180. struct device *dev = &pdev->dev;
  181. int ret;
  182. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  183. if (ret)
  184. return ret;
  185. devm_of_platform_populate(dev);
  186. return drm_of_component_probe(dev, component_compare_of, &sti_ops);
  187. }
  188. static void sti_platform_remove(struct platform_device *pdev)
  189. {
  190. component_master_del(&pdev->dev, &sti_ops);
  191. }
  192. static void sti_platform_shutdown(struct platform_device *pdev)
  193. {
  194. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  195. }
  196. static const struct of_device_id sti_dt_ids[] = {
  197. { .compatible = "st,sti-display-subsystem", },
  198. { /* end node */ },
  199. };
  200. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  201. static struct platform_driver sti_platform_driver = {
  202. .probe = sti_platform_probe,
  203. .remove = sti_platform_remove,
  204. .shutdown = sti_platform_shutdown,
  205. .driver = {
  206. .name = DRIVER_NAME,
  207. .of_match_table = sti_dt_ids,
  208. },
  209. };
  210. static struct platform_driver * const drivers[] = {
  211. &sti_tvout_driver,
  212. &sti_hqvdp_driver,
  213. &sti_hdmi_driver,
  214. &sti_hda_driver,
  215. &sti_dvo_driver,
  216. &sti_vtg_driver,
  217. &sti_compositor_driver,
  218. &sti_platform_driver,
  219. };
  220. static int sti_drm_init(void)
  221. {
  222. if (drm_firmware_drivers_only())
  223. return -ENODEV;
  224. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  225. }
  226. module_init(sti_drm_init);
  227. static void sti_drm_exit(void)
  228. {
  229. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  230. }
  231. module_exit(sti_drm_exit);
  232. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  233. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  234. MODULE_LICENSE("GPL");