meson_drv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_fb_cma_helper.h>
  36. #include <drm/drm_rect.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include "meson_drv.h"
  39. #include "meson_plane.h"
  40. #include "meson_crtc.h"
  41. #include "meson_venc_cvbs.h"
  42. #include "meson_vpp.h"
  43. #include "meson_viu.h"
  44. #include "meson_venc.h"
  45. #include "meson_canvas.h"
  46. #include "meson_registers.h"
  47. #define DRIVER_NAME "meson"
  48. #define DRIVER_DESC "Amlogic Meson DRM driver"
  49. /**
  50. * DOC: Video Processing Unit
  51. *
  52. * VPU Handles the Global Video Processing, it includes management of the
  53. * clocks gates, blocks reset lines and power domains.
  54. *
  55. * What is missing :
  56. *
  57. * - Full reset of entire video processing HW blocks
  58. * - Scaling and setup of the VPU clock
  59. * - Bus clock gates
  60. * - Powering up video processing HW blocks
  61. * - Powering Up HDMI controller and PHY
  62. */
  63. static void meson_fb_output_poll_changed(struct drm_device *dev)
  64. {
  65. struct meson_drm *priv = dev->dev_private;
  66. drm_fbdev_cma_hotplug_event(priv->fbdev);
  67. }
  68. static const struct drm_mode_config_funcs meson_mode_config_funcs = {
  69. .output_poll_changed = meson_fb_output_poll_changed,
  70. .atomic_check = drm_atomic_helper_check,
  71. .atomic_commit = drm_atomic_helper_commit,
  72. .fb_create = drm_fb_cma_create,
  73. };
  74. static irqreturn_t meson_irq(int irq, void *arg)
  75. {
  76. struct drm_device *dev = arg;
  77. struct meson_drm *priv = dev->dev_private;
  78. (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
  79. meson_crtc_irq(priv);
  80. return IRQ_HANDLED;
  81. }
  82. DEFINE_DRM_GEM_CMA_FOPS(fops);
  83. static struct drm_driver meson_driver = {
  84. .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
  85. DRIVER_MODESET | DRIVER_PRIME |
  86. DRIVER_ATOMIC,
  87. /* IRQ */
  88. .irq_handler = meson_irq,
  89. /* PRIME Ops */
  90. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  91. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  92. .gem_prime_import = drm_gem_prime_import,
  93. .gem_prime_export = drm_gem_prime_export,
  94. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  95. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  96. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  97. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  98. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  99. /* GEM Ops */
  100. .dumb_create = drm_gem_cma_dumb_create,
  101. .gem_free_object_unlocked = drm_gem_cma_free_object,
  102. .gem_vm_ops = &drm_gem_cma_vm_ops,
  103. /* Misc */
  104. .fops = &fops,
  105. .name = DRIVER_NAME,
  106. .desc = DRIVER_DESC,
  107. .date = "20161109",
  108. .major = 1,
  109. .minor = 0,
  110. };
  111. static bool meson_vpu_has_available_connectors(struct device *dev)
  112. {
  113. struct device_node *ep, *remote;
  114. /* Parses each endpoint and check if remote exists */
  115. for_each_endpoint_of_node(dev->of_node, ep) {
  116. /* If the endpoint node exists, consider it enabled */
  117. remote = of_graph_get_remote_port(ep);
  118. if (remote)
  119. return true;
  120. }
  121. return false;
  122. }
  123. static struct regmap_config meson_regmap_config = {
  124. .reg_bits = 32,
  125. .val_bits = 32,
  126. .reg_stride = 4,
  127. .max_register = 0x1000,
  128. };
  129. static int meson_drv_bind_master(struct device *dev, bool has_components)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev);
  132. struct meson_drm *priv;
  133. struct drm_device *drm;
  134. struct resource *res;
  135. void __iomem *regs;
  136. int ret;
  137. /* Checks if an output connector is available */
  138. if (!meson_vpu_has_available_connectors(dev)) {
  139. dev_err(dev, "No output connector available\n");
  140. return -ENODEV;
  141. }
  142. drm = drm_dev_alloc(&meson_driver, dev);
  143. if (IS_ERR(drm))
  144. return PTR_ERR(drm);
  145. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  146. if (!priv) {
  147. ret = -ENOMEM;
  148. goto free_drm;
  149. }
  150. drm->dev_private = priv;
  151. priv->drm = drm;
  152. priv->dev = dev;
  153. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
  154. regs = devm_ioremap_resource(dev, res);
  155. if (IS_ERR(regs))
  156. return PTR_ERR(regs);
  157. priv->io_base = regs;
  158. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
  159. /* Simply ioremap since it may be a shared register zone */
  160. regs = devm_ioremap(dev, res->start, resource_size(res));
  161. if (!regs)
  162. return -EADDRNOTAVAIL;
  163. priv->hhi = devm_regmap_init_mmio(dev, regs,
  164. &meson_regmap_config);
  165. if (IS_ERR(priv->hhi)) {
  166. dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
  167. return PTR_ERR(priv->hhi);
  168. }
  169. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
  170. /* Simply ioremap since it may be a shared register zone */
  171. regs = devm_ioremap(dev, res->start, resource_size(res));
  172. if (!regs)
  173. return -EADDRNOTAVAIL;
  174. priv->dmc = devm_regmap_init_mmio(dev, regs,
  175. &meson_regmap_config);
  176. if (IS_ERR(priv->dmc)) {
  177. dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
  178. return PTR_ERR(priv->dmc);
  179. }
  180. priv->vsync_irq = platform_get_irq(pdev, 0);
  181. drm_vblank_init(drm, 1);
  182. drm_mode_config_init(drm);
  183. drm->mode_config.max_width = 3840;
  184. drm->mode_config.max_height = 2160;
  185. drm->mode_config.funcs = &meson_mode_config_funcs;
  186. /* Hardware Initialization */
  187. meson_venc_init(priv);
  188. meson_vpp_init(priv);
  189. meson_viu_init(priv);
  190. /* Encoder Initialization */
  191. ret = meson_venc_cvbs_create(priv);
  192. if (ret)
  193. goto free_drm;
  194. if (has_components) {
  195. ret = component_bind_all(drm->dev, drm);
  196. if (ret) {
  197. dev_err(drm->dev, "Couldn't bind all components\n");
  198. goto free_drm;
  199. }
  200. }
  201. ret = meson_plane_create(priv);
  202. if (ret)
  203. goto free_drm;
  204. ret = meson_crtc_create(priv);
  205. if (ret)
  206. goto free_drm;
  207. ret = drm_irq_install(drm, priv->vsync_irq);
  208. if (ret)
  209. goto free_drm;
  210. drm_mode_config_reset(drm);
  211. priv->fbdev = drm_fbdev_cma_init(drm, 32,
  212. drm->mode_config.num_connector);
  213. if (IS_ERR(priv->fbdev)) {
  214. ret = PTR_ERR(priv->fbdev);
  215. goto free_drm;
  216. }
  217. drm_kms_helper_poll_init(drm);
  218. platform_set_drvdata(pdev, priv);
  219. ret = drm_dev_register(drm, 0);
  220. if (ret)
  221. goto free_drm;
  222. return 0;
  223. free_drm:
  224. drm_dev_unref(drm);
  225. return ret;
  226. }
  227. static int meson_drv_bind(struct device *dev)
  228. {
  229. return meson_drv_bind_master(dev, true);
  230. }
  231. static void meson_drv_unbind(struct device *dev)
  232. {
  233. struct drm_device *drm = dev_get_drvdata(dev);
  234. struct meson_drm *priv = drm->dev_private;
  235. drm_dev_unregister(drm);
  236. drm_kms_helper_poll_fini(drm);
  237. drm_fbdev_cma_fini(priv->fbdev);
  238. drm_mode_config_cleanup(drm);
  239. drm_dev_unref(drm);
  240. }
  241. static const struct component_master_ops meson_drv_master_ops = {
  242. .bind = meson_drv_bind,
  243. .unbind = meson_drv_unbind,
  244. };
  245. static int compare_of(struct device *dev, void *data)
  246. {
  247. DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
  248. dev->of_node, data);
  249. return dev->of_node == data;
  250. }
  251. /* Possible connectors nodes to ignore */
  252. static const struct of_device_id connectors_match[] = {
  253. { .compatible = "composite-video-connector" },
  254. { .compatible = "svideo-connector" },
  255. { .compatible = "hdmi-connector" },
  256. { .compatible = "dvi-connector" },
  257. {}
  258. };
  259. static int meson_probe_remote(struct platform_device *pdev,
  260. struct component_match **match,
  261. struct device_node *parent,
  262. struct device_node *remote)
  263. {
  264. struct device_node *ep, *remote_node;
  265. int count = 1;
  266. /* If node is a connector, return and do not add to match table */
  267. if (of_match_node(connectors_match, remote))
  268. return 1;
  269. component_match_add(&pdev->dev, match, compare_of, remote);
  270. for_each_endpoint_of_node(remote, ep) {
  271. remote_node = of_graph_get_remote_port_parent(ep);
  272. if (!remote_node ||
  273. remote_node == parent || /* Ignore parent endpoint */
  274. !of_device_is_available(remote_node))
  275. continue;
  276. count += meson_probe_remote(pdev, match, remote, remote_node);
  277. of_node_put(remote_node);
  278. }
  279. return count;
  280. }
  281. static int meson_drv_probe(struct platform_device *pdev)
  282. {
  283. struct component_match *match = NULL;
  284. struct device_node *np = pdev->dev.of_node;
  285. struct device_node *ep, *remote;
  286. int count = 0;
  287. for_each_endpoint_of_node(np, ep) {
  288. remote = of_graph_get_remote_port_parent(ep);
  289. if (!remote || !of_device_is_available(remote))
  290. continue;
  291. count += meson_probe_remote(pdev, &match, np, remote);
  292. }
  293. if (count && !match)
  294. return meson_drv_bind_master(&pdev->dev, false);
  295. /* If some endpoints were found, initialize the nodes */
  296. if (count) {
  297. dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
  298. return component_master_add_with_match(&pdev->dev,
  299. &meson_drv_master_ops,
  300. match);
  301. }
  302. /* If no output endpoints were available, simply bail out */
  303. return 0;
  304. };
  305. static const struct of_device_id dt_match[] = {
  306. { .compatible = "amlogic,meson-gxbb-vpu" },
  307. { .compatible = "amlogic,meson-gxl-vpu" },
  308. { .compatible = "amlogic,meson-gxm-vpu" },
  309. {}
  310. };
  311. MODULE_DEVICE_TABLE(of, dt_match);
  312. static struct platform_driver meson_drm_platform_driver = {
  313. .probe = meson_drv_probe,
  314. .driver = {
  315. .name = "meson-drm",
  316. .of_match_table = dt_match,
  317. },
  318. };
  319. module_platform_driver(meson_drm_platform_driver);
  320. MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
  321. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  322. MODULE_DESCRIPTION(DRIVER_DESC);
  323. MODULE_LICENSE("GPL");