sti_drv.c 6.5 KB

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