sti_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. static 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, 0);
  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_check(struct drm_device *dev,
  158. struct drm_atomic_state *state)
  159. {
  160. int ret;
  161. ret = drm_atomic_helper_check_modeset(dev, state);
  162. if (ret)
  163. return ret;
  164. ret = drm_atomic_normalize_zpos(dev, state);
  165. if (ret)
  166. return ret;
  167. ret = drm_atomic_helper_check_planes(dev, state);
  168. if (ret)
  169. return ret;
  170. return ret;
  171. }
  172. static int sti_atomic_commit(struct drm_device *drm,
  173. struct drm_atomic_state *state, bool nonblock)
  174. {
  175. struct sti_private *private = drm->dev_private;
  176. int err;
  177. err = drm_atomic_helper_prepare_planes(drm, state);
  178. if (err)
  179. return err;
  180. /* serialize outstanding nonblocking commits */
  181. mutex_lock(&private->commit.lock);
  182. flush_work(&private->commit.work);
  183. /*
  184. * This is the point of no return - everything below never fails except
  185. * when the hw goes bonghits. Which means we can commit the new state on
  186. * the software side now.
  187. */
  188. drm_atomic_helper_swap_state(state, true);
  189. if (nonblock)
  190. sti_atomic_schedule(private, state);
  191. else
  192. sti_atomic_complete(private, state);
  193. mutex_unlock(&private->commit.lock);
  194. return 0;
  195. }
  196. static void sti_output_poll_changed(struct drm_device *ddev)
  197. {
  198. struct sti_private *private = ddev->dev_private;
  199. if (!ddev->mode_config.num_connector)
  200. return;
  201. if (private->fbdev) {
  202. drm_fbdev_cma_hotplug_event(private->fbdev);
  203. return;
  204. }
  205. private->fbdev = drm_fbdev_cma_init(ddev, 32,
  206. ddev->mode_config.num_crtc,
  207. ddev->mode_config.num_connector);
  208. if (IS_ERR(private->fbdev))
  209. private->fbdev = NULL;
  210. }
  211. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  212. .fb_create = drm_fb_cma_create,
  213. .output_poll_changed = sti_output_poll_changed,
  214. .atomic_check = sti_atomic_check,
  215. .atomic_commit = sti_atomic_commit,
  216. };
  217. static void sti_mode_config_init(struct drm_device *dev)
  218. {
  219. dev->mode_config.min_width = 0;
  220. dev->mode_config.min_height = 0;
  221. /*
  222. * set max width and height as default value.
  223. * this value would be used to check framebuffer size limitation
  224. * at drm_mode_addfb().
  225. */
  226. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  227. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  228. dev->mode_config.funcs = &sti_mode_config_funcs;
  229. }
  230. static const struct file_operations sti_driver_fops = {
  231. .owner = THIS_MODULE,
  232. .open = drm_open,
  233. .mmap = drm_gem_cma_mmap,
  234. .poll = drm_poll,
  235. .read = drm_read,
  236. .unlocked_ioctl = drm_ioctl,
  237. #ifdef CONFIG_COMPAT
  238. .compat_ioctl = drm_compat_ioctl,
  239. #endif
  240. .release = drm_release,
  241. };
  242. static struct drm_driver sti_driver = {
  243. .driver_features = DRIVER_MODESET |
  244. DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
  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_init(struct drm_device *ddev)
  276. {
  277. struct sti_private *private;
  278. private = kzalloc(sizeof(*private), GFP_KERNEL);
  279. if (!private)
  280. return -ENOMEM;
  281. ddev->dev_private = (void *)private;
  282. dev_set_drvdata(ddev->dev, ddev);
  283. private->drm_dev = ddev;
  284. mutex_init(&private->commit.lock);
  285. INIT_WORK(&private->commit.work, sti_atomic_work);
  286. drm_mode_config_init(ddev);
  287. sti_mode_config_init(ddev);
  288. drm_kms_helper_poll_init(ddev);
  289. return 0;
  290. }
  291. static void sti_cleanup(struct drm_device *ddev)
  292. {
  293. struct sti_private *private = ddev->dev_private;
  294. if (private->fbdev) {
  295. drm_fbdev_cma_fini(private->fbdev);
  296. private->fbdev = NULL;
  297. }
  298. drm_kms_helper_poll_fini(ddev);
  299. drm_vblank_cleanup(ddev);
  300. kfree(private);
  301. ddev->dev_private = NULL;
  302. }
  303. static int sti_bind(struct device *dev)
  304. {
  305. struct drm_device *ddev;
  306. int ret;
  307. ddev = drm_dev_alloc(&sti_driver, dev);
  308. if (IS_ERR(ddev))
  309. return PTR_ERR(ddev);
  310. ddev->platformdev = to_platform_device(dev);
  311. ret = sti_init(ddev);
  312. if (ret)
  313. goto err_drm_dev_unref;
  314. ret = component_bind_all(ddev->dev, ddev);
  315. if (ret)
  316. goto err_cleanup;
  317. ret = drm_dev_register(ddev, 0);
  318. if (ret)
  319. goto err_register;
  320. drm_mode_config_reset(ddev);
  321. return 0;
  322. err_register:
  323. drm_mode_config_cleanup(ddev);
  324. err_cleanup:
  325. sti_cleanup(ddev);
  326. err_drm_dev_unref:
  327. drm_dev_unref(ddev);
  328. return ret;
  329. }
  330. static void sti_unbind(struct device *dev)
  331. {
  332. struct drm_device *ddev = dev_get_drvdata(dev);
  333. drm_dev_unregister(ddev);
  334. sti_cleanup(ddev);
  335. drm_dev_unref(ddev);
  336. }
  337. static const struct component_master_ops sti_ops = {
  338. .bind = sti_bind,
  339. .unbind = sti_unbind,
  340. };
  341. static int sti_platform_probe(struct platform_device *pdev)
  342. {
  343. struct device *dev = &pdev->dev;
  344. struct device_node *node = dev->of_node;
  345. struct device_node *child_np;
  346. struct component_match *match = NULL;
  347. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  348. of_platform_populate(node, NULL, NULL, dev);
  349. child_np = of_get_next_available_child(node, NULL);
  350. while (child_np) {
  351. component_match_add(dev, &match, compare_of, child_np);
  352. of_node_put(child_np);
  353. child_np = of_get_next_available_child(node, child_np);
  354. }
  355. return component_master_add_with_match(dev, &sti_ops, match);
  356. }
  357. static int sti_platform_remove(struct platform_device *pdev)
  358. {
  359. component_master_del(&pdev->dev, &sti_ops);
  360. of_platform_depopulate(&pdev->dev);
  361. return 0;
  362. }
  363. static const struct of_device_id sti_dt_ids[] = {
  364. { .compatible = "st,sti-display-subsystem", },
  365. { /* end node */ },
  366. };
  367. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  368. static struct platform_driver sti_platform_driver = {
  369. .probe = sti_platform_probe,
  370. .remove = sti_platform_remove,
  371. .driver = {
  372. .name = DRIVER_NAME,
  373. .of_match_table = sti_dt_ids,
  374. },
  375. };
  376. static struct platform_driver * const drivers[] = {
  377. &sti_tvout_driver,
  378. &sti_vtac_driver,
  379. &sti_hqvdp_driver,
  380. &sti_hdmi_driver,
  381. &sti_hda_driver,
  382. &sti_dvo_driver,
  383. &sti_vtg_driver,
  384. &sti_compositor_driver,
  385. &sti_platform_driver,
  386. };
  387. static int sti_drm_init(void)
  388. {
  389. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  390. }
  391. module_init(sti_drm_init);
  392. static void sti_drm_exit(void)
  393. {
  394. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  395. }
  396. module_exit(sti_drm_exit);
  397. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  398. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  399. MODULE_LICENSE("GPL");