sti_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 void sti_output_poll_changed(struct drm_device *ddev)
  182. {
  183. struct sti_private *private = ddev->dev_private;
  184. if (!ddev->mode_config.num_connector)
  185. return;
  186. if (private->fbdev) {
  187. drm_fbdev_cma_hotplug_event(private->fbdev);
  188. return;
  189. }
  190. private->fbdev = drm_fbdev_cma_init(ddev, 32,
  191. ddev->mode_config.num_crtc,
  192. ddev->mode_config.num_connector);
  193. if (IS_ERR(private->fbdev))
  194. private->fbdev = NULL;
  195. }
  196. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  197. .fb_create = drm_fb_cma_create,
  198. .output_poll_changed = sti_output_poll_changed,
  199. .atomic_check = drm_atomic_helper_check,
  200. .atomic_commit = sti_atomic_commit,
  201. };
  202. static void sti_mode_config_init(struct drm_device *dev)
  203. {
  204. dev->mode_config.min_width = 0;
  205. dev->mode_config.min_height = 0;
  206. /*
  207. * set max width and height as default value.
  208. * this value would be used to check framebuffer size limitation
  209. * at drm_mode_addfb().
  210. */
  211. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  212. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  213. dev->mode_config.funcs = &sti_mode_config_funcs;
  214. }
  215. static const struct file_operations sti_driver_fops = {
  216. .owner = THIS_MODULE,
  217. .open = drm_open,
  218. .mmap = drm_gem_cma_mmap,
  219. .poll = drm_poll,
  220. .read = drm_read,
  221. .unlocked_ioctl = drm_ioctl,
  222. #ifdef CONFIG_COMPAT
  223. .compat_ioctl = drm_compat_ioctl,
  224. #endif
  225. .release = drm_release,
  226. };
  227. static struct drm_driver sti_driver = {
  228. .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
  229. DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  230. .gem_free_object_unlocked = drm_gem_cma_free_object,
  231. .gem_vm_ops = &drm_gem_cma_vm_ops,
  232. .dumb_create = drm_gem_cma_dumb_create,
  233. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  234. .dumb_destroy = drm_gem_dumb_destroy,
  235. .fops = &sti_driver_fops,
  236. .get_vblank_counter = drm_vblank_no_hw_counter,
  237. .enable_vblank = sti_crtc_enable_vblank,
  238. .disable_vblank = sti_crtc_disable_vblank,
  239. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  240. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  241. .gem_prime_export = drm_gem_prime_export,
  242. .gem_prime_import = drm_gem_prime_import,
  243. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  244. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  245. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  246. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  247. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  248. .debugfs_init = sti_drm_dbg_init,
  249. .debugfs_cleanup = sti_drm_dbg_cleanup,
  250. .name = DRIVER_NAME,
  251. .desc = DRIVER_DESC,
  252. .date = DRIVER_DATE,
  253. .major = DRIVER_MAJOR,
  254. .minor = DRIVER_MINOR,
  255. };
  256. static int compare_of(struct device *dev, void *data)
  257. {
  258. return dev->of_node == data;
  259. }
  260. static int sti_init(struct drm_device *ddev)
  261. {
  262. struct sti_private *private;
  263. private = kzalloc(sizeof(*private), GFP_KERNEL);
  264. if (!private)
  265. return -ENOMEM;
  266. ddev->dev_private = (void *)private;
  267. dev_set_drvdata(ddev->dev, ddev);
  268. private->drm_dev = ddev;
  269. mutex_init(&private->commit.lock);
  270. INIT_WORK(&private->commit.work, sti_atomic_work);
  271. drm_mode_config_init(ddev);
  272. sti_mode_config_init(ddev);
  273. drm_kms_helper_poll_init(ddev);
  274. return 0;
  275. }
  276. static void sti_cleanup(struct drm_device *ddev)
  277. {
  278. struct sti_private *private = ddev->dev_private;
  279. if (private->fbdev) {
  280. drm_fbdev_cma_fini(private->fbdev);
  281. private->fbdev = NULL;
  282. }
  283. drm_kms_helper_poll_fini(ddev);
  284. drm_vblank_cleanup(ddev);
  285. kfree(private);
  286. ddev->dev_private = NULL;
  287. }
  288. static int sti_bind(struct device *dev)
  289. {
  290. struct drm_device *ddev;
  291. int ret;
  292. ddev = drm_dev_alloc(&sti_driver, dev);
  293. if (!ddev)
  294. return -ENOMEM;
  295. ddev->platformdev = to_platform_device(dev);
  296. ret = sti_init(ddev);
  297. if (ret)
  298. goto err_drm_dev_unref;
  299. ret = component_bind_all(ddev->dev, ddev);
  300. if (ret)
  301. goto err_cleanup;
  302. ret = drm_dev_register(ddev, 0);
  303. if (ret)
  304. goto err_register;
  305. drm_mode_config_reset(ddev);
  306. return 0;
  307. err_register:
  308. drm_mode_config_cleanup(ddev);
  309. err_cleanup:
  310. sti_cleanup(ddev);
  311. err_drm_dev_unref:
  312. drm_dev_unref(ddev);
  313. return ret;
  314. }
  315. static void sti_unbind(struct device *dev)
  316. {
  317. struct drm_device *ddev = dev_get_drvdata(dev);
  318. drm_dev_unregister(ddev);
  319. sti_cleanup(ddev);
  320. drm_dev_unref(ddev);
  321. }
  322. static const struct component_master_ops sti_ops = {
  323. .bind = sti_bind,
  324. .unbind = sti_unbind,
  325. };
  326. static int sti_platform_probe(struct platform_device *pdev)
  327. {
  328. struct device *dev = &pdev->dev;
  329. struct device_node *node = dev->of_node;
  330. struct device_node *child_np;
  331. struct component_match *match = NULL;
  332. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  333. of_platform_populate(node, NULL, NULL, dev);
  334. child_np = of_get_next_available_child(node, NULL);
  335. while (child_np) {
  336. component_match_add(dev, &match, compare_of, child_np);
  337. of_node_put(child_np);
  338. child_np = of_get_next_available_child(node, child_np);
  339. }
  340. return component_master_add_with_match(dev, &sti_ops, match);
  341. }
  342. static int sti_platform_remove(struct platform_device *pdev)
  343. {
  344. component_master_del(&pdev->dev, &sti_ops);
  345. of_platform_depopulate(&pdev->dev);
  346. return 0;
  347. }
  348. static const struct of_device_id sti_dt_ids[] = {
  349. { .compatible = "st,sti-display-subsystem", },
  350. { /* end node */ },
  351. };
  352. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  353. static struct platform_driver sti_platform_driver = {
  354. .probe = sti_platform_probe,
  355. .remove = sti_platform_remove,
  356. .driver = {
  357. .name = DRIVER_NAME,
  358. .of_match_table = sti_dt_ids,
  359. },
  360. };
  361. static struct platform_driver * const drivers[] = {
  362. &sti_tvout_driver,
  363. &sti_vtac_driver,
  364. &sti_hqvdp_driver,
  365. &sti_hdmi_driver,
  366. &sti_hda_driver,
  367. &sti_dvo_driver,
  368. &sti_vtg_driver,
  369. &sti_compositor_driver,
  370. &sti_platform_driver,
  371. };
  372. static int sti_drm_init(void)
  373. {
  374. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  375. }
  376. module_init(sti_drm_init);
  377. static void sti_drm_exit(void)
  378. {
  379. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  380. }
  381. module_exit(sti_drm_exit);
  382. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  383. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  384. MODULE_LICENSE("GPL");