mtk_drm_drv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: YT SHEN <yt.shen@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_gem.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <linux/component.h>
  21. #include <linux/iommu.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/pm_runtime.h>
  25. #include "mtk_drm_crtc.h"
  26. #include "mtk_drm_ddp.h"
  27. #include "mtk_drm_ddp_comp.h"
  28. #include "mtk_drm_drv.h"
  29. #include "mtk_drm_fb.h"
  30. #include "mtk_drm_gem.h"
  31. #define DRIVER_NAME "mediatek"
  32. #define DRIVER_DESC "Mediatek SoC DRM"
  33. #define DRIVER_DATE "20150513"
  34. #define DRIVER_MAJOR 1
  35. #define DRIVER_MINOR 0
  36. static void mtk_atomic_schedule(struct mtk_drm_private *private,
  37. struct drm_atomic_state *state)
  38. {
  39. private->commit.state = state;
  40. schedule_work(&private->commit.work);
  41. }
  42. static void mtk_atomic_wait_for_fences(struct drm_atomic_state *state)
  43. {
  44. struct drm_plane *plane;
  45. struct drm_plane_state *plane_state;
  46. int i;
  47. for_each_plane_in_state(state, plane, plane_state, i)
  48. mtk_fb_wait(plane->state->fb);
  49. }
  50. static void mtk_atomic_complete(struct mtk_drm_private *private,
  51. struct drm_atomic_state *state)
  52. {
  53. struct drm_device *drm = private->drm;
  54. mtk_atomic_wait_for_fences(state);
  55. drm_atomic_helper_commit_modeset_disables(drm, state);
  56. drm_atomic_helper_commit_planes(drm, state, false);
  57. drm_atomic_helper_commit_modeset_enables(drm, state);
  58. drm_atomic_helper_wait_for_vblanks(drm, state);
  59. drm_atomic_helper_cleanup_planes(drm, state);
  60. drm_atomic_state_free(state);
  61. }
  62. static void mtk_atomic_work(struct work_struct *work)
  63. {
  64. struct mtk_drm_private *private = container_of(work,
  65. struct mtk_drm_private, commit.work);
  66. mtk_atomic_complete(private, private->commit.state);
  67. }
  68. static int mtk_atomic_commit(struct drm_device *drm,
  69. struct drm_atomic_state *state,
  70. bool async)
  71. {
  72. struct mtk_drm_private *private = drm->dev_private;
  73. int ret;
  74. ret = drm_atomic_helper_prepare_planes(drm, state);
  75. if (ret)
  76. return ret;
  77. mutex_lock(&private->commit.lock);
  78. flush_work(&private->commit.work);
  79. drm_atomic_helper_swap_state(state, true);
  80. if (async)
  81. mtk_atomic_schedule(private, state);
  82. else
  83. mtk_atomic_complete(private, state);
  84. mutex_unlock(&private->commit.lock);
  85. return 0;
  86. }
  87. static const struct drm_mode_config_funcs mtk_drm_mode_config_funcs = {
  88. .fb_create = mtk_drm_mode_fb_create,
  89. .atomic_check = drm_atomic_helper_check,
  90. .atomic_commit = mtk_atomic_commit,
  91. };
  92. static const enum mtk_ddp_comp_id mtk_ddp_main[] = {
  93. DDP_COMPONENT_OVL0,
  94. DDP_COMPONENT_COLOR0,
  95. DDP_COMPONENT_AAL,
  96. DDP_COMPONENT_OD,
  97. DDP_COMPONENT_RDMA0,
  98. DDP_COMPONENT_UFOE,
  99. DDP_COMPONENT_DSI0,
  100. DDP_COMPONENT_PWM0,
  101. };
  102. static const enum mtk_ddp_comp_id mtk_ddp_ext[] = {
  103. DDP_COMPONENT_OVL1,
  104. DDP_COMPONENT_COLOR1,
  105. DDP_COMPONENT_GAMMA,
  106. DDP_COMPONENT_RDMA1,
  107. DDP_COMPONENT_DPI0,
  108. };
  109. static int mtk_drm_kms_init(struct drm_device *drm)
  110. {
  111. struct mtk_drm_private *private = drm->dev_private;
  112. struct platform_device *pdev;
  113. struct device_node *np;
  114. int ret;
  115. if (!iommu_present(&platform_bus_type))
  116. return -EPROBE_DEFER;
  117. pdev = of_find_device_by_node(private->mutex_node);
  118. if (!pdev) {
  119. dev_err(drm->dev, "Waiting for disp-mutex device %s\n",
  120. private->mutex_node->full_name);
  121. of_node_put(private->mutex_node);
  122. return -EPROBE_DEFER;
  123. }
  124. private->mutex_dev = &pdev->dev;
  125. drm_mode_config_init(drm);
  126. drm->mode_config.min_width = 64;
  127. drm->mode_config.min_height = 64;
  128. /*
  129. * set max width and height as default value(4096x4096).
  130. * this value would be used to check framebuffer size limitation
  131. * at drm_mode_addfb().
  132. */
  133. drm->mode_config.max_width = 4096;
  134. drm->mode_config.max_height = 4096;
  135. drm->mode_config.funcs = &mtk_drm_mode_config_funcs;
  136. ret = component_bind_all(drm->dev, drm);
  137. if (ret)
  138. goto err_config_cleanup;
  139. /*
  140. * We currently support two fixed data streams, each optional,
  141. * and each statically assigned to a crtc:
  142. * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ...
  143. */
  144. ret = mtk_drm_crtc_create(drm, mtk_ddp_main, ARRAY_SIZE(mtk_ddp_main));
  145. if (ret < 0)
  146. goto err_component_unbind;
  147. /* ... and OVL1 -> COLOR1 -> GAMMA -> RDMA1 -> DPI0. */
  148. ret = mtk_drm_crtc_create(drm, mtk_ddp_ext, ARRAY_SIZE(mtk_ddp_ext));
  149. if (ret < 0)
  150. goto err_component_unbind;
  151. /* Use OVL device for all DMA memory allocations */
  152. np = private->comp_node[mtk_ddp_main[0]] ?:
  153. private->comp_node[mtk_ddp_ext[0]];
  154. pdev = of_find_device_by_node(np);
  155. if (!pdev) {
  156. ret = -ENODEV;
  157. dev_err(drm->dev, "Need at least one OVL device\n");
  158. goto err_component_unbind;
  159. }
  160. private->dma_dev = &pdev->dev;
  161. /*
  162. * We don't use the drm_irq_install() helpers provided by the DRM
  163. * core, so we need to set this manually in order to allow the
  164. * DRM_IOCTL_WAIT_VBLANK to operate correctly.
  165. */
  166. drm->irq_enabled = true;
  167. ret = drm_vblank_init(drm, MAX_CRTC);
  168. if (ret < 0)
  169. goto err_component_unbind;
  170. drm_kms_helper_poll_init(drm);
  171. drm_mode_config_reset(drm);
  172. return 0;
  173. err_component_unbind:
  174. component_unbind_all(drm->dev, drm);
  175. err_config_cleanup:
  176. drm_mode_config_cleanup(drm);
  177. return ret;
  178. }
  179. static void mtk_drm_kms_deinit(struct drm_device *drm)
  180. {
  181. drm_kms_helper_poll_fini(drm);
  182. drm_vblank_cleanup(drm);
  183. component_unbind_all(drm->dev, drm);
  184. drm_mode_config_cleanup(drm);
  185. }
  186. static const struct file_operations mtk_drm_fops = {
  187. .owner = THIS_MODULE,
  188. .open = drm_open,
  189. .release = drm_release,
  190. .unlocked_ioctl = drm_ioctl,
  191. .mmap = mtk_drm_gem_mmap,
  192. .poll = drm_poll,
  193. .read = drm_read,
  194. #ifdef CONFIG_COMPAT
  195. .compat_ioctl = drm_compat_ioctl,
  196. #endif
  197. };
  198. static struct drm_driver mtk_drm_driver = {
  199. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
  200. DRIVER_ATOMIC,
  201. .get_vblank_counter = drm_vblank_count,
  202. .enable_vblank = mtk_drm_crtc_enable_vblank,
  203. .disable_vblank = mtk_drm_crtc_disable_vblank,
  204. .gem_free_object_unlocked = mtk_drm_gem_free_object,
  205. .gem_vm_ops = &drm_gem_cma_vm_ops,
  206. .dumb_create = mtk_drm_gem_dumb_create,
  207. .dumb_map_offset = mtk_drm_gem_dumb_map_offset,
  208. .dumb_destroy = drm_gem_dumb_destroy,
  209. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  210. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  211. .gem_prime_export = drm_gem_prime_export,
  212. .gem_prime_import = drm_gem_prime_import,
  213. .gem_prime_get_sg_table = mtk_gem_prime_get_sg_table,
  214. .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table,
  215. .gem_prime_mmap = mtk_drm_gem_mmap_buf,
  216. .fops = &mtk_drm_fops,
  217. .name = DRIVER_NAME,
  218. .desc = DRIVER_DESC,
  219. .date = DRIVER_DATE,
  220. .major = DRIVER_MAJOR,
  221. .minor = DRIVER_MINOR,
  222. };
  223. static int compare_of(struct device *dev, void *data)
  224. {
  225. return dev->of_node == data;
  226. }
  227. static int mtk_drm_bind(struct device *dev)
  228. {
  229. struct mtk_drm_private *private = dev_get_drvdata(dev);
  230. struct drm_device *drm;
  231. int ret;
  232. drm = drm_dev_alloc(&mtk_drm_driver, dev);
  233. if (!drm)
  234. return -ENOMEM;
  235. drm_dev_set_unique(drm, dev_name(dev));
  236. drm->dev_private = private;
  237. private->drm = drm;
  238. ret = mtk_drm_kms_init(drm);
  239. if (ret < 0)
  240. goto err_free;
  241. ret = drm_dev_register(drm, 0);
  242. if (ret < 0)
  243. goto err_deinit;
  244. ret = drm_connector_register_all(drm);
  245. if (ret < 0)
  246. goto err_unregister;
  247. return 0;
  248. err_unregister:
  249. drm_dev_unregister(drm);
  250. err_deinit:
  251. mtk_drm_kms_deinit(drm);
  252. err_free:
  253. drm_dev_unref(drm);
  254. return ret;
  255. }
  256. static void mtk_drm_unbind(struct device *dev)
  257. {
  258. struct mtk_drm_private *private = dev_get_drvdata(dev);
  259. drm_put_dev(private->drm);
  260. private->drm = NULL;
  261. }
  262. static const struct component_master_ops mtk_drm_ops = {
  263. .bind = mtk_drm_bind,
  264. .unbind = mtk_drm_unbind,
  265. };
  266. static const struct of_device_id mtk_ddp_comp_dt_ids[] = {
  267. { .compatible = "mediatek,mt8173-disp-ovl", .data = (void *)MTK_DISP_OVL },
  268. { .compatible = "mediatek,mt8173-disp-rdma", .data = (void *)MTK_DISP_RDMA },
  269. { .compatible = "mediatek,mt8173-disp-wdma", .data = (void *)MTK_DISP_WDMA },
  270. { .compatible = "mediatek,mt8173-disp-color", .data = (void *)MTK_DISP_COLOR },
  271. { .compatible = "mediatek,mt8173-disp-aal", .data = (void *)MTK_DISP_AAL},
  272. { .compatible = "mediatek,mt8173-disp-gamma", .data = (void *)MTK_DISP_GAMMA, },
  273. { .compatible = "mediatek,mt8173-disp-ufoe", .data = (void *)MTK_DISP_UFOE },
  274. { .compatible = "mediatek,mt8173-dsi", .data = (void *)MTK_DSI },
  275. { .compatible = "mediatek,mt8173-dpi", .data = (void *)MTK_DPI },
  276. { .compatible = "mediatek,mt8173-disp-mutex", .data = (void *)MTK_DISP_MUTEX },
  277. { .compatible = "mediatek,mt8173-disp-pwm", .data = (void *)MTK_DISP_PWM },
  278. { .compatible = "mediatek,mt8173-disp-od", .data = (void *)MTK_DISP_OD },
  279. { }
  280. };
  281. static int mtk_drm_probe(struct platform_device *pdev)
  282. {
  283. struct device *dev = &pdev->dev;
  284. struct mtk_drm_private *private;
  285. struct resource *mem;
  286. struct device_node *node;
  287. struct component_match *match = NULL;
  288. int ret;
  289. int i;
  290. private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL);
  291. if (!private)
  292. return -ENOMEM;
  293. mutex_init(&private->commit.lock);
  294. INIT_WORK(&private->commit.work, mtk_atomic_work);
  295. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. private->config_regs = devm_ioremap_resource(dev, mem);
  297. if (IS_ERR(private->config_regs)) {
  298. ret = PTR_ERR(private->config_regs);
  299. dev_err(dev, "Failed to ioremap mmsys-config resource: %d\n",
  300. ret);
  301. return ret;
  302. }
  303. /* Iterate over sibling DISP function blocks */
  304. for_each_child_of_node(dev->of_node->parent, node) {
  305. const struct of_device_id *of_id;
  306. enum mtk_ddp_comp_type comp_type;
  307. int comp_id;
  308. of_id = of_match_node(mtk_ddp_comp_dt_ids, node);
  309. if (!of_id)
  310. continue;
  311. if (!of_device_is_available(node)) {
  312. dev_dbg(dev, "Skipping disabled component %s\n",
  313. node->full_name);
  314. continue;
  315. }
  316. comp_type = (enum mtk_ddp_comp_type)of_id->data;
  317. if (comp_type == MTK_DISP_MUTEX) {
  318. private->mutex_node = of_node_get(node);
  319. continue;
  320. }
  321. comp_id = mtk_ddp_comp_get_id(node, comp_type);
  322. if (comp_id < 0) {
  323. dev_warn(dev, "Skipping unknown component %s\n",
  324. node->full_name);
  325. continue;
  326. }
  327. private->comp_node[comp_id] = of_node_get(node);
  328. /*
  329. * Currently only the OVL, RDMA, DSI, and DPI blocks have
  330. * separate component platform drivers and initialize their own
  331. * DDP component structure. The others are initialized here.
  332. */
  333. if (comp_type == MTK_DISP_OVL ||
  334. comp_type == MTK_DISP_RDMA ||
  335. comp_type == MTK_DSI ||
  336. comp_type == MTK_DPI) {
  337. dev_info(dev, "Adding component match for %s\n",
  338. node->full_name);
  339. component_match_add(dev, &match, compare_of, node);
  340. } else {
  341. struct mtk_ddp_comp *comp;
  342. comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
  343. if (!comp) {
  344. ret = -ENOMEM;
  345. goto err_node;
  346. }
  347. ret = mtk_ddp_comp_init(dev, node, comp, comp_id, NULL);
  348. if (ret)
  349. goto err_node;
  350. private->ddp_comp[comp_id] = comp;
  351. }
  352. }
  353. if (!private->mutex_node) {
  354. dev_err(dev, "Failed to find disp-mutex node\n");
  355. ret = -ENODEV;
  356. goto err_node;
  357. }
  358. pm_runtime_enable(dev);
  359. platform_set_drvdata(pdev, private);
  360. ret = component_master_add_with_match(dev, &mtk_drm_ops, match);
  361. if (ret)
  362. goto err_pm;
  363. return 0;
  364. err_pm:
  365. pm_runtime_disable(dev);
  366. err_node:
  367. of_node_put(private->mutex_node);
  368. for (i = 0; i < DDP_COMPONENT_ID_MAX; i++)
  369. of_node_put(private->comp_node[i]);
  370. return ret;
  371. }
  372. static int mtk_drm_remove(struct platform_device *pdev)
  373. {
  374. struct mtk_drm_private *private = platform_get_drvdata(pdev);
  375. struct drm_device *drm = private->drm;
  376. int i;
  377. drm_connector_unregister_all(drm);
  378. drm_dev_unregister(drm);
  379. mtk_drm_kms_deinit(drm);
  380. drm_dev_unref(drm);
  381. component_master_del(&pdev->dev, &mtk_drm_ops);
  382. pm_runtime_disable(&pdev->dev);
  383. of_node_put(private->mutex_node);
  384. for (i = 0; i < DDP_COMPONENT_ID_MAX; i++)
  385. of_node_put(private->comp_node[i]);
  386. return 0;
  387. }
  388. #ifdef CONFIG_PM_SLEEP
  389. static int mtk_drm_sys_suspend(struct device *dev)
  390. {
  391. struct mtk_drm_private *private = dev_get_drvdata(dev);
  392. struct drm_device *drm = private->drm;
  393. drm_kms_helper_poll_disable(drm);
  394. private->suspend_state = drm_atomic_helper_suspend(drm);
  395. if (IS_ERR(private->suspend_state)) {
  396. drm_kms_helper_poll_enable(drm);
  397. return PTR_ERR(private->suspend_state);
  398. }
  399. DRM_DEBUG_DRIVER("mtk_drm_sys_suspend\n");
  400. return 0;
  401. }
  402. static int mtk_drm_sys_resume(struct device *dev)
  403. {
  404. struct mtk_drm_private *private = dev_get_drvdata(dev);
  405. struct drm_device *drm = private->drm;
  406. drm_atomic_helper_resume(drm, private->suspend_state);
  407. drm_kms_helper_poll_enable(drm);
  408. DRM_DEBUG_DRIVER("mtk_drm_sys_resume\n");
  409. return 0;
  410. }
  411. #endif
  412. static SIMPLE_DEV_PM_OPS(mtk_drm_pm_ops, mtk_drm_sys_suspend,
  413. mtk_drm_sys_resume);
  414. static const struct of_device_id mtk_drm_of_ids[] = {
  415. { .compatible = "mediatek,mt8173-mmsys", },
  416. { }
  417. };
  418. static struct platform_driver mtk_drm_platform_driver = {
  419. .probe = mtk_drm_probe,
  420. .remove = mtk_drm_remove,
  421. .driver = {
  422. .name = "mediatek-drm",
  423. .of_match_table = mtk_drm_of_ids,
  424. .pm = &mtk_drm_pm_ops,
  425. },
  426. };
  427. static struct platform_driver * const mtk_drm_drivers[] = {
  428. &mtk_ddp_driver,
  429. &mtk_disp_ovl_driver,
  430. &mtk_disp_rdma_driver,
  431. &mtk_dpi_driver,
  432. &mtk_drm_platform_driver,
  433. &mtk_dsi_driver,
  434. &mtk_mipi_tx_driver,
  435. };
  436. static int __init mtk_drm_init(void)
  437. {
  438. int ret;
  439. int i;
  440. for (i = 0; i < ARRAY_SIZE(mtk_drm_drivers); i++) {
  441. ret = platform_driver_register(mtk_drm_drivers[i]);
  442. if (ret < 0) {
  443. pr_err("Failed to register %s driver: %d\n",
  444. mtk_drm_drivers[i]->driver.name, ret);
  445. goto err;
  446. }
  447. }
  448. return 0;
  449. err:
  450. while (--i >= 0)
  451. platform_driver_unregister(mtk_drm_drivers[i]);
  452. return ret;
  453. }
  454. static void __exit mtk_drm_exit(void)
  455. {
  456. int i;
  457. for (i = ARRAY_SIZE(mtk_drm_drivers) - 1; i >= 0; i--)
  458. platform_driver_unregister(mtk_drm_drivers[i]);
  459. }
  460. module_init(mtk_drm_init);
  461. module_exit(mtk_drm_exit);
  462. MODULE_AUTHOR("YT SHEN <yt.shen@mediatek.com>");
  463. MODULE_DESCRIPTION("Mediatek SoC DRM driver");
  464. MODULE_LICENSE("GPL v2");