sti_drv.c 11 KB

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