sti_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "sti_crtc.h"
  18. #include "sti_drv.h"
  19. #include "sti_plane.h"
  20. #define DRIVER_NAME "sti"
  21. #define DRIVER_DESC "STMicroelectronics SoC DRM"
  22. #define DRIVER_DATE "20140601"
  23. #define DRIVER_MAJOR 1
  24. #define DRIVER_MINOR 0
  25. #define STI_MAX_FB_HEIGHT 4096
  26. #define STI_MAX_FB_WIDTH 4096
  27. static int sti_drm_fps_get(void *data, u64 *val)
  28. {
  29. struct drm_device *drm_dev = data;
  30. struct drm_plane *p;
  31. unsigned int i = 0;
  32. *val = 0;
  33. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  34. struct sti_plane *plane = to_sti_plane(p);
  35. *val |= plane->fps_info.output << i;
  36. i++;
  37. }
  38. return 0;
  39. }
  40. static int sti_drm_fps_set(void *data, u64 val)
  41. {
  42. struct drm_device *drm_dev = data;
  43. struct drm_plane *p;
  44. unsigned int i = 0;
  45. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  46. struct sti_plane *plane = to_sti_plane(p);
  47. plane->fps_info.output = (val >> i) & 1;
  48. i++;
  49. }
  50. return 0;
  51. }
  52. DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
  53. sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
  54. static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
  55. {
  56. struct drm_info_node *node = s->private;
  57. struct drm_device *dev = node->minor->dev;
  58. struct drm_plane *p;
  59. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  60. struct sti_plane *plane = to_sti_plane(p);
  61. seq_printf(s, "%s%s\n",
  62. plane->fps_info.fps_str,
  63. plane->fps_info.fips_str);
  64. }
  65. return 0;
  66. }
  67. static struct drm_info_list sti_drm_dbg_list[] = {
  68. {"fps_get", sti_drm_fps_dbg_show, 0},
  69. };
  70. static int sti_drm_debugfs_create(struct dentry *root,
  71. struct drm_minor *minor,
  72. const char *name,
  73. const struct file_operations *fops)
  74. {
  75. struct drm_device *dev = minor->dev;
  76. struct drm_info_node *node;
  77. struct dentry *ent;
  78. ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops);
  79. if (IS_ERR(ent))
  80. return PTR_ERR(ent);
  81. node = kmalloc(sizeof(*node), GFP_KERNEL);
  82. if (!node) {
  83. debugfs_remove(ent);
  84. return -ENOMEM;
  85. }
  86. node->minor = minor;
  87. node->dent = ent;
  88. node->info_ent = (void *)fops;
  89. mutex_lock(&minor->debugfs_lock);
  90. list_add(&node->list, &minor->debugfs_list);
  91. mutex_unlock(&minor->debugfs_lock);
  92. return 0;
  93. }
  94. static int sti_drm_dbg_init(struct drm_minor *minor)
  95. {
  96. int ret;
  97. ret = drm_debugfs_create_files(sti_drm_dbg_list,
  98. ARRAY_SIZE(sti_drm_dbg_list),
  99. minor->debugfs_root, minor);
  100. if (ret)
  101. goto err;
  102. ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show",
  103. &sti_drm_fps_fops);
  104. if (ret)
  105. goto err;
  106. DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
  107. return 0;
  108. err:
  109. DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
  110. return ret;
  111. }
  112. void sti_drm_dbg_cleanup(struct drm_minor *minor)
  113. {
  114. drm_debugfs_remove_files(sti_drm_dbg_list,
  115. ARRAY_SIZE(sti_drm_dbg_list), minor);
  116. drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops,
  117. 1, minor);
  118. }
  119. static void sti_atomic_schedule(struct sti_private *private,
  120. struct drm_atomic_state *state)
  121. {
  122. private->commit.state = state;
  123. schedule_work(&private->commit.work);
  124. }
  125. static void sti_atomic_complete(struct sti_private *private,
  126. struct drm_atomic_state *state)
  127. {
  128. struct drm_device *drm = private->drm_dev;
  129. /*
  130. * Everything below can be run asynchronously without the need to grab
  131. * any modeset locks at all under one condition: It must be guaranteed
  132. * that the asynchronous work has either been cancelled (if the driver
  133. * supports it, which at least requires that the framebuffers get
  134. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  135. * before the new state gets committed on the software side with
  136. * drm_atomic_helper_swap_state().
  137. *
  138. * This scheme allows new atomic state updates to be prepared and
  139. * checked in parallel to the asynchronous completion of the previous
  140. * update. Which is important since compositors need to figure out the
  141. * composition of the next frame right after having submitted the
  142. * current layout.
  143. */
  144. drm_atomic_helper_commit_modeset_disables(drm, state);
  145. drm_atomic_helper_commit_planes(drm, state, false);
  146. drm_atomic_helper_commit_modeset_enables(drm, state);
  147. drm_atomic_helper_wait_for_vblanks(drm, state);
  148. drm_atomic_helper_cleanup_planes(drm, state);
  149. drm_atomic_state_free(state);
  150. }
  151. static void sti_atomic_work(struct work_struct *work)
  152. {
  153. struct sti_private *private = container_of(work,
  154. struct sti_private, commit.work);
  155. sti_atomic_complete(private, private->commit.state);
  156. }
  157. static int sti_atomic_commit(struct drm_device *drm,
  158. struct drm_atomic_state *state, bool nonblock)
  159. {
  160. struct sti_private *private = drm->dev_private;
  161. int err;
  162. err = drm_atomic_helper_prepare_planes(drm, state);
  163. if (err)
  164. return err;
  165. /* serialize outstanding nonblocking commits */
  166. mutex_lock(&private->commit.lock);
  167. flush_work(&private->commit.work);
  168. /*
  169. * This is the point of no return - everything below never fails except
  170. * when the hw goes bonghits. Which means we can commit the new state on
  171. * the software side now.
  172. */
  173. drm_atomic_helper_swap_state(state, true);
  174. if (nonblock)
  175. sti_atomic_schedule(private, state);
  176. else
  177. sti_atomic_complete(private, state);
  178. mutex_unlock(&private->commit.lock);
  179. return 0;
  180. }
  181. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  182. .fb_create = drm_fb_cma_create,
  183. .atomic_check = drm_atomic_helper_check,
  184. .atomic_commit = sti_atomic_commit,
  185. };
  186. static void sti_mode_config_init(struct drm_device *dev)
  187. {
  188. dev->mode_config.min_width = 0;
  189. dev->mode_config.min_height = 0;
  190. /*
  191. * set max width and height as default value.
  192. * this value would be used to check framebuffer size limitation
  193. * at drm_mode_addfb().
  194. */
  195. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  196. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  197. dev->mode_config.funcs = &sti_mode_config_funcs;
  198. }
  199. static int sti_load(struct drm_device *dev, unsigned long flags)
  200. {
  201. struct sti_private *private;
  202. int ret;
  203. private = kzalloc(sizeof(*private), GFP_KERNEL);
  204. if (!private) {
  205. DRM_ERROR("Failed to allocate private\n");
  206. return -ENOMEM;
  207. }
  208. dev->dev_private = (void *)private;
  209. private->drm_dev = dev;
  210. mutex_init(&private->commit.lock);
  211. INIT_WORK(&private->commit.work, sti_atomic_work);
  212. drm_mode_config_init(dev);
  213. drm_kms_helper_poll_init(dev);
  214. sti_mode_config_init(dev);
  215. ret = component_bind_all(dev->dev, dev);
  216. if (ret) {
  217. drm_kms_helper_poll_fini(dev);
  218. drm_mode_config_cleanup(dev);
  219. kfree(private);
  220. return ret;
  221. }
  222. drm_mode_config_reset(dev);
  223. drm_helper_disable_unused_functions(dev);
  224. drm_fbdev_cma_init(dev, 32,
  225. dev->mode_config.num_crtc,
  226. dev->mode_config.num_connector);
  227. return 0;
  228. }
  229. static const struct file_operations sti_driver_fops = {
  230. .owner = THIS_MODULE,
  231. .open = drm_open,
  232. .mmap = drm_gem_cma_mmap,
  233. .poll = drm_poll,
  234. .read = drm_read,
  235. .unlocked_ioctl = drm_ioctl,
  236. #ifdef CONFIG_COMPAT
  237. .compat_ioctl = drm_compat_ioctl,
  238. #endif
  239. .release = drm_release,
  240. };
  241. static struct drm_driver sti_driver = {
  242. .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
  243. DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  244. .load = sti_load,
  245. .gem_free_object_unlocked = drm_gem_cma_free_object,
  246. .gem_vm_ops = &drm_gem_cma_vm_ops,
  247. .dumb_create = drm_gem_cma_dumb_create,
  248. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  249. .dumb_destroy = drm_gem_dumb_destroy,
  250. .fops = &sti_driver_fops,
  251. .get_vblank_counter = drm_vblank_no_hw_counter,
  252. .enable_vblank = sti_crtc_enable_vblank,
  253. .disable_vblank = sti_crtc_disable_vblank,
  254. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  255. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  256. .gem_prime_export = drm_gem_prime_export,
  257. .gem_prime_import = drm_gem_prime_import,
  258. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  259. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  260. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  261. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  262. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  263. .debugfs_init = sti_drm_dbg_init,
  264. .debugfs_cleanup = sti_drm_dbg_cleanup,
  265. .name = DRIVER_NAME,
  266. .desc = DRIVER_DESC,
  267. .date = DRIVER_DATE,
  268. .major = DRIVER_MAJOR,
  269. .minor = DRIVER_MINOR,
  270. };
  271. static int compare_of(struct device *dev, void *data)
  272. {
  273. return dev->of_node == data;
  274. }
  275. static int sti_bind(struct device *dev)
  276. {
  277. return drm_platform_init(&sti_driver, to_platform_device(dev));
  278. }
  279. static void sti_unbind(struct device *dev)
  280. {
  281. drm_put_dev(dev_get_drvdata(dev));
  282. }
  283. static const struct component_master_ops sti_ops = {
  284. .bind = sti_bind,
  285. .unbind = sti_unbind,
  286. };
  287. static int sti_platform_probe(struct platform_device *pdev)
  288. {
  289. struct device *dev = &pdev->dev;
  290. struct device_node *node = dev->of_node;
  291. struct device_node *child_np;
  292. struct component_match *match = NULL;
  293. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  294. of_platform_populate(node, NULL, NULL, dev);
  295. child_np = of_get_next_available_child(node, NULL);
  296. while (child_np) {
  297. component_match_add(dev, &match, compare_of, child_np);
  298. of_node_put(child_np);
  299. child_np = of_get_next_available_child(node, child_np);
  300. }
  301. return component_master_add_with_match(dev, &sti_ops, match);
  302. }
  303. static int sti_platform_remove(struct platform_device *pdev)
  304. {
  305. component_master_del(&pdev->dev, &sti_ops);
  306. of_platform_depopulate(&pdev->dev);
  307. return 0;
  308. }
  309. static const struct of_device_id sti_dt_ids[] = {
  310. { .compatible = "st,sti-display-subsystem", },
  311. { /* end node */ },
  312. };
  313. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  314. static struct platform_driver sti_platform_driver = {
  315. .probe = sti_platform_probe,
  316. .remove = sti_platform_remove,
  317. .driver = {
  318. .name = DRIVER_NAME,
  319. .of_match_table = sti_dt_ids,
  320. },
  321. };
  322. static struct platform_driver * const drivers[] = {
  323. &sti_tvout_driver,
  324. &sti_vtac_driver,
  325. &sti_hqvdp_driver,
  326. &sti_hdmi_driver,
  327. &sti_hda_driver,
  328. &sti_dvo_driver,
  329. &sti_vtg_driver,
  330. &sti_compositor_driver,
  331. &sti_platform_driver,
  332. };
  333. static int sti_drm_init(void)
  334. {
  335. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  336. }
  337. module_init(sti_drm_init);
  338. static void sti_drm_exit(void)
  339. {
  340. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  341. }
  342. module_exit(sti_drm_exit);
  343. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  344. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  345. MODULE_LICENSE("GPL");