sti_drv.c 8.7 KB

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