sti_drv.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <drm/drmP.h>
  7. #include <linux/component.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of_platform.h>
  12. #include <drm/drm_atomic.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_gem_cma_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include <drm/drm_of.h>
  18. #include "sti_crtc.h"
  19. #include "sti_drv.h"
  20. #include "sti_plane.h"
  21. #define DRIVER_NAME "sti"
  22. #define DRIVER_DESC "STMicroelectronics SoC DRM"
  23. #define DRIVER_DATE "20140601"
  24. #define DRIVER_MAJOR 1
  25. #define DRIVER_MINOR 0
  26. #define STI_MAX_FB_HEIGHT 4096
  27. #define STI_MAX_FB_WIDTH 4096
  28. static int sti_drm_fps_get(void *data, u64 *val)
  29. {
  30. struct drm_device *drm_dev = data;
  31. struct drm_plane *p;
  32. unsigned int i = 0;
  33. *val = 0;
  34. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  35. struct sti_plane *plane = to_sti_plane(p);
  36. *val |= plane->fps_info.output << i;
  37. i++;
  38. }
  39. return 0;
  40. }
  41. static int sti_drm_fps_set(void *data, u64 val)
  42. {
  43. struct drm_device *drm_dev = data;
  44. struct drm_plane *p;
  45. unsigned int i = 0;
  46. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  47. struct sti_plane *plane = to_sti_plane(p);
  48. memset(&plane->fps_info, 0, sizeof(plane->fps_info));
  49. plane->fps_info.output = (val >> i) & 1;
  50. i++;
  51. }
  52. return 0;
  53. }
  54. DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
  55. sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
  56. static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
  57. {
  58. struct drm_info_node *node = s->private;
  59. struct drm_device *dev = node->minor->dev;
  60. struct drm_plane *p;
  61. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  62. struct sti_plane *plane = to_sti_plane(p);
  63. seq_printf(s, "%s%s\n",
  64. plane->fps_info.fps_str,
  65. plane->fps_info.fips_str);
  66. }
  67. return 0;
  68. }
  69. static struct drm_info_list sti_drm_dbg_list[] = {
  70. {"fps_get", sti_drm_fps_dbg_show, 0},
  71. };
  72. static int sti_drm_dbg_init(struct drm_minor *minor)
  73. {
  74. struct dentry *dentry;
  75. int ret;
  76. ret = drm_debugfs_create_files(sti_drm_dbg_list,
  77. ARRAY_SIZE(sti_drm_dbg_list),
  78. minor->debugfs_root, minor);
  79. if (ret)
  80. goto err;
  81. dentry = debugfs_create_file("fps_show", S_IRUGO | S_IWUSR,
  82. minor->debugfs_root, minor->dev,
  83. &sti_drm_fps_fops);
  84. if (!dentry) {
  85. ret = -ENOMEM;
  86. goto err;
  87. }
  88. DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
  89. return 0;
  90. err:
  91. DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
  92. return ret;
  93. }
  94. static int sti_atomic_check(struct drm_device *dev,
  95. struct drm_atomic_state *state)
  96. {
  97. int ret;
  98. ret = drm_atomic_helper_check_modeset(dev, state);
  99. if (ret)
  100. return ret;
  101. ret = drm_atomic_normalize_zpos(dev, state);
  102. if (ret)
  103. return ret;
  104. ret = drm_atomic_helper_check_planes(dev, state);
  105. if (ret)
  106. return ret;
  107. return ret;
  108. }
  109. static void sti_output_poll_changed(struct drm_device *ddev)
  110. {
  111. struct sti_private *private = ddev->dev_private;
  112. drm_fbdev_cma_hotplug_event(private->fbdev);
  113. }
  114. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  115. .fb_create = drm_fb_cma_create,
  116. .output_poll_changed = sti_output_poll_changed,
  117. .atomic_check = sti_atomic_check,
  118. .atomic_commit = drm_atomic_helper_commit,
  119. };
  120. static void sti_mode_config_init(struct drm_device *dev)
  121. {
  122. dev->mode_config.min_width = 0;
  123. dev->mode_config.min_height = 0;
  124. /*
  125. * set max width and height as default value.
  126. * this value would be used to check framebuffer size limitation
  127. * at drm_mode_addfb().
  128. */
  129. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  130. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  131. dev->mode_config.funcs = &sti_mode_config_funcs;
  132. }
  133. DEFINE_DRM_GEM_CMA_FOPS(sti_driver_fops);
  134. static struct drm_driver sti_driver = {
  135. .driver_features = DRIVER_MODESET |
  136. DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  137. .gem_free_object_unlocked = drm_gem_cma_free_object,
  138. .gem_vm_ops = &drm_gem_cma_vm_ops,
  139. .dumb_create = drm_gem_cma_dumb_create,
  140. .fops = &sti_driver_fops,
  141. .enable_vblank = sti_crtc_enable_vblank,
  142. .disable_vblank = sti_crtc_disable_vblank,
  143. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  144. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  145. .gem_prime_export = drm_gem_prime_export,
  146. .gem_prime_import = drm_gem_prime_import,
  147. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  148. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  149. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  150. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  151. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  152. .debugfs_init = sti_drm_dbg_init,
  153. .name = DRIVER_NAME,
  154. .desc = DRIVER_DESC,
  155. .date = DRIVER_DATE,
  156. .major = DRIVER_MAJOR,
  157. .minor = DRIVER_MINOR,
  158. };
  159. static int compare_of(struct device *dev, void *data)
  160. {
  161. return dev->of_node == data;
  162. }
  163. static int sti_init(struct drm_device *ddev)
  164. {
  165. struct sti_private *private;
  166. private = kzalloc(sizeof(*private), GFP_KERNEL);
  167. if (!private)
  168. return -ENOMEM;
  169. ddev->dev_private = (void *)private;
  170. dev_set_drvdata(ddev->dev, ddev);
  171. private->drm_dev = ddev;
  172. drm_mode_config_init(ddev);
  173. sti_mode_config_init(ddev);
  174. drm_kms_helper_poll_init(ddev);
  175. return 0;
  176. }
  177. static void sti_cleanup(struct drm_device *ddev)
  178. {
  179. struct sti_private *private = ddev->dev_private;
  180. if (private->fbdev) {
  181. drm_fbdev_cma_fini(private->fbdev);
  182. private->fbdev = NULL;
  183. }
  184. drm_kms_helper_poll_fini(ddev);
  185. component_unbind_all(ddev->dev, ddev);
  186. kfree(private);
  187. ddev->dev_private = NULL;
  188. }
  189. static int sti_bind(struct device *dev)
  190. {
  191. struct drm_device *ddev;
  192. struct sti_private *private;
  193. struct drm_fbdev_cma *fbdev;
  194. int ret;
  195. ddev = drm_dev_alloc(&sti_driver, dev);
  196. if (IS_ERR(ddev))
  197. return PTR_ERR(ddev);
  198. ret = sti_init(ddev);
  199. if (ret)
  200. goto err_drm_dev_unref;
  201. ret = component_bind_all(ddev->dev, ddev);
  202. if (ret)
  203. goto err_cleanup;
  204. ret = drm_dev_register(ddev, 0);
  205. if (ret)
  206. goto err_register;
  207. drm_mode_config_reset(ddev);
  208. private = ddev->dev_private;
  209. if (ddev->mode_config.num_connector) {
  210. fbdev = drm_fbdev_cma_init(ddev, 32,
  211. ddev->mode_config.num_connector);
  212. if (IS_ERR(fbdev)) {
  213. DRM_DEBUG_DRIVER("Warning: fails to create fbdev\n");
  214. fbdev = NULL;
  215. }
  216. private->fbdev = fbdev;
  217. }
  218. return 0;
  219. err_register:
  220. drm_mode_config_cleanup(ddev);
  221. err_cleanup:
  222. sti_cleanup(ddev);
  223. err_drm_dev_unref:
  224. drm_dev_unref(ddev);
  225. return ret;
  226. }
  227. static void sti_unbind(struct device *dev)
  228. {
  229. struct drm_device *ddev = dev_get_drvdata(dev);
  230. drm_dev_unregister(ddev);
  231. sti_cleanup(ddev);
  232. drm_dev_unref(ddev);
  233. }
  234. static const struct component_master_ops sti_ops = {
  235. .bind = sti_bind,
  236. .unbind = sti_unbind,
  237. };
  238. static int sti_platform_probe(struct platform_device *pdev)
  239. {
  240. struct device *dev = &pdev->dev;
  241. struct device_node *node = dev->of_node;
  242. struct device_node *child_np;
  243. struct component_match *match = NULL;
  244. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  245. devm_of_platform_populate(dev);
  246. child_np = of_get_next_available_child(node, NULL);
  247. while (child_np) {
  248. drm_of_component_match_add(dev, &match, compare_of,
  249. child_np);
  250. child_np = of_get_next_available_child(node, child_np);
  251. }
  252. return component_master_add_with_match(dev, &sti_ops, match);
  253. }
  254. static int sti_platform_remove(struct platform_device *pdev)
  255. {
  256. component_master_del(&pdev->dev, &sti_ops);
  257. return 0;
  258. }
  259. static const struct of_device_id sti_dt_ids[] = {
  260. { .compatible = "st,sti-display-subsystem", },
  261. { /* end node */ },
  262. };
  263. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  264. static struct platform_driver sti_platform_driver = {
  265. .probe = sti_platform_probe,
  266. .remove = sti_platform_remove,
  267. .driver = {
  268. .name = DRIVER_NAME,
  269. .of_match_table = sti_dt_ids,
  270. },
  271. };
  272. static struct platform_driver * const drivers[] = {
  273. &sti_tvout_driver,
  274. &sti_hqvdp_driver,
  275. &sti_hdmi_driver,
  276. &sti_hda_driver,
  277. &sti_dvo_driver,
  278. &sti_vtg_driver,
  279. &sti_compositor_driver,
  280. &sti_platform_driver,
  281. };
  282. static int sti_drm_init(void)
  283. {
  284. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  285. }
  286. module_init(sti_drm_init);
  287. static void sti_drm_exit(void)
  288. {
  289. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  290. }
  291. module_exit(sti_drm_exit);
  292. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  293. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  294. MODULE_LICENSE("GPL");