sti_drv.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  141. .dumb_destroy = drm_gem_dumb_destroy,
  142. .fops = &sti_driver_fops,
  143. .enable_vblank = sti_crtc_enable_vblank,
  144. .disable_vblank = sti_crtc_disable_vblank,
  145. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  146. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  147. .gem_prime_export = drm_gem_prime_export,
  148. .gem_prime_import = drm_gem_prime_import,
  149. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  150. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  151. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  152. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  153. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  154. .debugfs_init = sti_drm_dbg_init,
  155. .name = DRIVER_NAME,
  156. .desc = DRIVER_DESC,
  157. .date = DRIVER_DATE,
  158. .major = DRIVER_MAJOR,
  159. .minor = DRIVER_MINOR,
  160. };
  161. static int compare_of(struct device *dev, void *data)
  162. {
  163. return dev->of_node == data;
  164. }
  165. static int sti_init(struct drm_device *ddev)
  166. {
  167. struct sti_private *private;
  168. private = kzalloc(sizeof(*private), GFP_KERNEL);
  169. if (!private)
  170. return -ENOMEM;
  171. ddev->dev_private = (void *)private;
  172. dev_set_drvdata(ddev->dev, ddev);
  173. private->drm_dev = ddev;
  174. drm_mode_config_init(ddev);
  175. sti_mode_config_init(ddev);
  176. drm_kms_helper_poll_init(ddev);
  177. return 0;
  178. }
  179. static void sti_cleanup(struct drm_device *ddev)
  180. {
  181. struct sti_private *private = ddev->dev_private;
  182. if (private->fbdev) {
  183. drm_fbdev_cma_fini(private->fbdev);
  184. private->fbdev = NULL;
  185. }
  186. drm_kms_helper_poll_fini(ddev);
  187. drm_vblank_cleanup(ddev);
  188. component_unbind_all(ddev->dev, ddev);
  189. kfree(private);
  190. ddev->dev_private = NULL;
  191. }
  192. static int sti_bind(struct device *dev)
  193. {
  194. struct drm_device *ddev;
  195. struct sti_private *private;
  196. struct drm_fbdev_cma *fbdev;
  197. int ret;
  198. ddev = drm_dev_alloc(&sti_driver, dev);
  199. if (IS_ERR(ddev))
  200. return PTR_ERR(ddev);
  201. ret = sti_init(ddev);
  202. if (ret)
  203. goto err_drm_dev_unref;
  204. ret = component_bind_all(ddev->dev, ddev);
  205. if (ret)
  206. goto err_cleanup;
  207. ret = drm_dev_register(ddev, 0);
  208. if (ret)
  209. goto err_register;
  210. drm_mode_config_reset(ddev);
  211. private = ddev->dev_private;
  212. if (ddev->mode_config.num_connector) {
  213. fbdev = drm_fbdev_cma_init(ddev, 32,
  214. ddev->mode_config.num_connector);
  215. if (IS_ERR(fbdev)) {
  216. DRM_DEBUG_DRIVER("Warning: fails to create fbdev\n");
  217. fbdev = NULL;
  218. }
  219. private->fbdev = fbdev;
  220. }
  221. return 0;
  222. err_register:
  223. drm_mode_config_cleanup(ddev);
  224. err_cleanup:
  225. sti_cleanup(ddev);
  226. err_drm_dev_unref:
  227. drm_dev_unref(ddev);
  228. return ret;
  229. }
  230. static void sti_unbind(struct device *dev)
  231. {
  232. struct drm_device *ddev = dev_get_drvdata(dev);
  233. drm_dev_unregister(ddev);
  234. sti_cleanup(ddev);
  235. drm_dev_unref(ddev);
  236. }
  237. static const struct component_master_ops sti_ops = {
  238. .bind = sti_bind,
  239. .unbind = sti_unbind,
  240. };
  241. static int sti_platform_probe(struct platform_device *pdev)
  242. {
  243. struct device *dev = &pdev->dev;
  244. struct device_node *node = dev->of_node;
  245. struct device_node *child_np;
  246. struct component_match *match = NULL;
  247. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  248. devm_of_platform_populate(dev);
  249. child_np = of_get_next_available_child(node, NULL);
  250. while (child_np) {
  251. drm_of_component_match_add(dev, &match, compare_of,
  252. child_np);
  253. child_np = of_get_next_available_child(node, child_np);
  254. }
  255. return component_master_add_with_match(dev, &sti_ops, match);
  256. }
  257. static int sti_platform_remove(struct platform_device *pdev)
  258. {
  259. component_master_del(&pdev->dev, &sti_ops);
  260. return 0;
  261. }
  262. static const struct of_device_id sti_dt_ids[] = {
  263. { .compatible = "st,sti-display-subsystem", },
  264. { /* end node */ },
  265. };
  266. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  267. static struct platform_driver sti_platform_driver = {
  268. .probe = sti_platform_probe,
  269. .remove = sti_platform_remove,
  270. .driver = {
  271. .name = DRIVER_NAME,
  272. .of_match_table = sti_dt_ids,
  273. },
  274. };
  275. static struct platform_driver * const drivers[] = {
  276. &sti_tvout_driver,
  277. &sti_hqvdp_driver,
  278. &sti_hdmi_driver,
  279. &sti_hda_driver,
  280. &sti_dvo_driver,
  281. &sti_vtg_driver,
  282. &sti_compositor_driver,
  283. &sti_platform_driver,
  284. };
  285. static int sti_drm_init(void)
  286. {
  287. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  288. }
  289. module_init(sti_drm_init);
  290. static void sti_drm_exit(void)
  291. {
  292. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  293. }
  294. module_exit(sti_drm_exit);
  295. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  296. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  297. MODULE_LICENSE("GPL");