meson_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2016 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (C) 2014 Endless Mobile
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Written by:
  20. * Jasper St. Pierre <jstpierre@mecheye.net>
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/component.h>
  27. #include <linux/of_graph.h>
  28. #include <drm/drmP.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_flip_work.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_plane_helper.h>
  34. #include <drm/drm_gem_cma_helper.h>
  35. #include <drm/drm_gem_framebuffer_helper.h>
  36. #include <drm/drm_fb_cma_helper.h>
  37. #include <drm/drm_rect.h>
  38. #include <drm/drm_fb_helper.h>
  39. #include "meson_drv.h"
  40. #include "meson_plane.h"
  41. #include "meson_crtc.h"
  42. #include "meson_venc_cvbs.h"
  43. #include "meson_vpp.h"
  44. #include "meson_viu.h"
  45. #include "meson_venc.h"
  46. #include "meson_canvas.h"
  47. #include "meson_registers.h"
  48. #define DRIVER_NAME "meson"
  49. #define DRIVER_DESC "Amlogic Meson DRM driver"
  50. /**
  51. * DOC: Video Processing Unit
  52. *
  53. * VPU Handles the Global Video Processing, it includes management of the
  54. * clocks gates, blocks reset lines and power domains.
  55. *
  56. * What is missing :
  57. *
  58. * - Full reset of entire video processing HW blocks
  59. * - Scaling and setup of the VPU clock
  60. * - Bus clock gates
  61. * - Powering up video processing HW blocks
  62. * - Powering Up HDMI controller and PHY
  63. */
  64. static void meson_fb_output_poll_changed(struct drm_device *dev)
  65. {
  66. struct meson_drm *priv = dev->dev_private;
  67. drm_fbdev_cma_hotplug_event(priv->fbdev);
  68. }
  69. static const struct drm_mode_config_funcs meson_mode_config_funcs = {
  70. .output_poll_changed = meson_fb_output_poll_changed,
  71. .atomic_check = drm_atomic_helper_check,
  72. .atomic_commit = drm_atomic_helper_commit,
  73. .fb_create = drm_gem_fb_create,
  74. };
  75. static irqreturn_t meson_irq(int irq, void *arg)
  76. {
  77. struct drm_device *dev = arg;
  78. struct meson_drm *priv = dev->dev_private;
  79. (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
  80. meson_crtc_irq(priv);
  81. return IRQ_HANDLED;
  82. }
  83. DEFINE_DRM_GEM_CMA_FOPS(fops);
  84. static struct drm_driver meson_driver = {
  85. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  86. DRIVER_MODESET | DRIVER_PRIME |
  87. DRIVER_ATOMIC,
  88. /* IRQ */
  89. .irq_handler = meson_irq,
  90. /* PRIME Ops */
  91. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  92. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  93. .gem_prime_import = drm_gem_prime_import,
  94. .gem_prime_export = drm_gem_prime_export,
  95. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  96. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  97. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  98. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  99. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  100. /* GEM Ops */
  101. .dumb_create = drm_gem_cma_dumb_create,
  102. .gem_free_object_unlocked = drm_gem_cma_free_object,
  103. .gem_vm_ops = &drm_gem_cma_vm_ops,
  104. /* Misc */
  105. .fops = &fops,
  106. .name = DRIVER_NAME,
  107. .desc = DRIVER_DESC,
  108. .date = "20161109",
  109. .major = 1,
  110. .minor = 0,
  111. };
  112. static bool meson_vpu_has_available_connectors(struct device *dev)
  113. {
  114. struct device_node *ep, *remote;
  115. /* Parses each endpoint and check if remote exists */
  116. for_each_endpoint_of_node(dev->of_node, ep) {
  117. /* If the endpoint node exists, consider it enabled */
  118. remote = of_graph_get_remote_port(ep);
  119. if (remote)
  120. return true;
  121. }
  122. return false;
  123. }
  124. static struct regmap_config meson_regmap_config = {
  125. .reg_bits = 32,
  126. .val_bits = 32,
  127. .reg_stride = 4,
  128. .max_register = 0x1000,
  129. };
  130. static void meson_vpu_init(struct meson_drm *priv)
  131. {
  132. writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
  133. writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
  134. writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
  135. writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
  136. }
  137. static int meson_drv_bind_master(struct device *dev, bool has_components)
  138. {
  139. struct platform_device *pdev = to_platform_device(dev);
  140. struct meson_drm *priv;
  141. struct drm_device *drm;
  142. struct resource *res;
  143. void __iomem *regs;
  144. int ret;
  145. /* Checks if an output connector is available */
  146. if (!meson_vpu_has_available_connectors(dev)) {
  147. dev_err(dev, "No output connector available\n");
  148. return -ENODEV;
  149. }
  150. drm = drm_dev_alloc(&meson_driver, dev);
  151. if (IS_ERR(drm))
  152. return PTR_ERR(drm);
  153. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  154. if (!priv) {
  155. ret = -ENOMEM;
  156. goto free_drm;
  157. }
  158. drm->dev_private = priv;
  159. priv->drm = drm;
  160. priv->dev = dev;
  161. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
  162. regs = devm_ioremap_resource(dev, res);
  163. if (IS_ERR(regs)) {
  164. ret = PTR_ERR(regs);
  165. goto free_drm;
  166. }
  167. priv->io_base = regs;
  168. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
  169. if (!res) {
  170. ret = -EINVAL;
  171. goto free_drm;
  172. }
  173. /* Simply ioremap since it may be a shared register zone */
  174. regs = devm_ioremap(dev, res->start, resource_size(res));
  175. if (!regs) {
  176. ret = -EADDRNOTAVAIL;
  177. goto free_drm;
  178. }
  179. priv->hhi = devm_regmap_init_mmio(dev, regs,
  180. &meson_regmap_config);
  181. if (IS_ERR(priv->hhi)) {
  182. dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
  183. ret = PTR_ERR(priv->hhi);
  184. goto free_drm;
  185. }
  186. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
  187. if (!res) {
  188. ret = -EINVAL;
  189. goto free_drm;
  190. }
  191. /* Simply ioremap since it may be a shared register zone */
  192. regs = devm_ioremap(dev, res->start, resource_size(res));
  193. if (!regs) {
  194. ret = -EADDRNOTAVAIL;
  195. goto free_drm;
  196. }
  197. priv->dmc = devm_regmap_init_mmio(dev, regs,
  198. &meson_regmap_config);
  199. if (IS_ERR(priv->dmc)) {
  200. dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
  201. ret = PTR_ERR(priv->dmc);
  202. goto free_drm;
  203. }
  204. priv->vsync_irq = platform_get_irq(pdev, 0);
  205. ret = drm_vblank_init(drm, 1);
  206. if (ret)
  207. goto free_drm;
  208. drm_mode_config_init(drm);
  209. drm->mode_config.max_width = 3840;
  210. drm->mode_config.max_height = 2160;
  211. drm->mode_config.funcs = &meson_mode_config_funcs;
  212. /* Hardware Initialization */
  213. meson_vpu_init(priv);
  214. meson_venc_init(priv);
  215. meson_vpp_init(priv);
  216. meson_viu_init(priv);
  217. /* Encoder Initialization */
  218. ret = meson_venc_cvbs_create(priv);
  219. if (ret)
  220. goto free_drm;
  221. if (has_components) {
  222. ret = component_bind_all(drm->dev, drm);
  223. if (ret) {
  224. dev_err(drm->dev, "Couldn't bind all components\n");
  225. goto free_drm;
  226. }
  227. }
  228. ret = meson_plane_create(priv);
  229. if (ret)
  230. goto free_drm;
  231. ret = meson_crtc_create(priv);
  232. if (ret)
  233. goto free_drm;
  234. ret = drm_irq_install(drm, priv->vsync_irq);
  235. if (ret)
  236. goto free_drm;
  237. drm_mode_config_reset(drm);
  238. priv->fbdev = drm_fbdev_cma_init(drm, 32,
  239. drm->mode_config.num_connector);
  240. if (IS_ERR(priv->fbdev)) {
  241. ret = PTR_ERR(priv->fbdev);
  242. goto free_drm;
  243. }
  244. drm_kms_helper_poll_init(drm);
  245. platform_set_drvdata(pdev, priv);
  246. ret = drm_dev_register(drm, 0);
  247. if (ret)
  248. goto free_drm;
  249. return 0;
  250. free_drm:
  251. drm_dev_put(drm);
  252. return ret;
  253. }
  254. static int meson_drv_bind(struct device *dev)
  255. {
  256. return meson_drv_bind_master(dev, true);
  257. }
  258. static void meson_drv_unbind(struct device *dev)
  259. {
  260. struct drm_device *drm = dev_get_drvdata(dev);
  261. struct meson_drm *priv = drm->dev_private;
  262. drm_dev_unregister(drm);
  263. drm_kms_helper_poll_fini(drm);
  264. drm_fbdev_cma_fini(priv->fbdev);
  265. drm_mode_config_cleanup(drm);
  266. drm_dev_put(drm);
  267. }
  268. static const struct component_master_ops meson_drv_master_ops = {
  269. .bind = meson_drv_bind,
  270. .unbind = meson_drv_unbind,
  271. };
  272. static int compare_of(struct device *dev, void *data)
  273. {
  274. DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
  275. dev->of_node, data);
  276. return dev->of_node == data;
  277. }
  278. /* Possible connectors nodes to ignore */
  279. static const struct of_device_id connectors_match[] = {
  280. { .compatible = "composite-video-connector" },
  281. { .compatible = "svideo-connector" },
  282. { .compatible = "hdmi-connector" },
  283. { .compatible = "dvi-connector" },
  284. {}
  285. };
  286. static int meson_probe_remote(struct platform_device *pdev,
  287. struct component_match **match,
  288. struct device_node *parent,
  289. struct device_node *remote)
  290. {
  291. struct device_node *ep, *remote_node;
  292. int count = 1;
  293. /* If node is a connector, return and do not add to match table */
  294. if (of_match_node(connectors_match, remote))
  295. return 1;
  296. component_match_add(&pdev->dev, match, compare_of, remote);
  297. for_each_endpoint_of_node(remote, ep) {
  298. remote_node = of_graph_get_remote_port_parent(ep);
  299. if (!remote_node ||
  300. remote_node == parent || /* Ignore parent endpoint */
  301. !of_device_is_available(remote_node))
  302. continue;
  303. count += meson_probe_remote(pdev, match, remote, remote_node);
  304. of_node_put(remote_node);
  305. }
  306. return count;
  307. }
  308. static int meson_drv_probe(struct platform_device *pdev)
  309. {
  310. struct component_match *match = NULL;
  311. struct device_node *np = pdev->dev.of_node;
  312. struct device_node *ep, *remote;
  313. int count = 0;
  314. for_each_endpoint_of_node(np, ep) {
  315. remote = of_graph_get_remote_port_parent(ep);
  316. if (!remote || !of_device_is_available(remote))
  317. continue;
  318. count += meson_probe_remote(pdev, &match, np, remote);
  319. }
  320. if (count && !match)
  321. return meson_drv_bind_master(&pdev->dev, false);
  322. /* If some endpoints were found, initialize the nodes */
  323. if (count) {
  324. dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
  325. return component_master_add_with_match(&pdev->dev,
  326. &meson_drv_master_ops,
  327. match);
  328. }
  329. /* If no output endpoints were available, simply bail out */
  330. return 0;
  331. };
  332. static const struct of_device_id dt_match[] = {
  333. { .compatible = "amlogic,meson-gxbb-vpu" },
  334. { .compatible = "amlogic,meson-gxl-vpu" },
  335. { .compatible = "amlogic,meson-gxm-vpu" },
  336. {}
  337. };
  338. MODULE_DEVICE_TABLE(of, dt_match);
  339. static struct platform_driver meson_drm_platform_driver = {
  340. .probe = meson_drv_probe,
  341. .driver = {
  342. .name = "meson-drm",
  343. .of_match_table = dt_match,
  344. },
  345. };
  346. module_platform_driver(meson_drm_platform_driver);
  347. MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
  348. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  349. MODULE_DESCRIPTION(DRIVER_DESC);
  350. MODULE_LICENSE("GPL");