sti_drv.c 12 KB

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