tve200_drv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  4. * Parts of this file were based on sources as follows:
  5. *
  6. * Copyright (C) 2006-2008 Intel Corporation
  7. * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
  8. * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
  9. * Copyright (C) 2011 Texas Instruments
  10. * Copyright (C) 2017 Eric Anholt
  11. */
  12. /**
  13. * DOC: Faraday TV Encoder TVE200 DRM Driver
  14. *
  15. * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
  16. * Controller (TVC) and is found in the Gemini Chipset from Storlink
  17. * Semiconductor (later Storm Semiconductor, later Cortina Systems)
  18. * but also in the Grain Media GM8180 chipset. On the Gemini the module
  19. * is connected to 8 data lines and a single clock line, comprising an
  20. * 8-bit BT.656 interface.
  21. *
  22. * This is a very basic YUV display driver. The datasheet specifies that
  23. * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
  24. * the hallmark of any TV encoder supporting both PAL and NTSC.
  25. *
  26. * This driver exposes a standard KMS interface for this TV encoder.
  27. */
  28. #include <linux/clk.h>
  29. #include <linux/dma-buf.h>
  30. #include <linux/irq.h>
  31. #include <linux/io.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/shmem_fs.h>
  36. #include <linux/slab.h>
  37. #include <drm/clients/drm_client_setup.h>
  38. #include <drm/drm_atomic_helper.h>
  39. #include <drm/drm_bridge.h>
  40. #include <drm/drm_drv.h>
  41. #include <drm/drm_fbdev_dma.h>
  42. #include <drm/drm_fourcc.h>
  43. #include <drm/drm_gem_dma_helper.h>
  44. #include <drm/drm_gem_framebuffer_helper.h>
  45. #include <drm/drm_module.h>
  46. #include <drm/drm_of.h>
  47. #include <drm/drm_panel.h>
  48. #include <drm/drm_probe_helper.h>
  49. #include <drm/drm_vblank.h>
  50. #include "tve200_drm.h"
  51. #define DRIVER_DESC "DRM module for Faraday TVE200"
  52. static const struct drm_mode_config_funcs mode_config_funcs = {
  53. .fb_create = drm_gem_fb_create,
  54. .atomic_check = drm_atomic_helper_check,
  55. .atomic_commit = drm_atomic_helper_commit,
  56. };
  57. static int tve200_modeset_init(struct drm_device *dev)
  58. {
  59. struct drm_mode_config *mode_config;
  60. struct tve200_drm_dev_private *priv = dev->dev_private;
  61. struct drm_panel *panel;
  62. struct drm_bridge *bridge;
  63. int ret;
  64. drm_mode_config_init(dev);
  65. mode_config = &dev->mode_config;
  66. mode_config->funcs = &mode_config_funcs;
  67. mode_config->min_width = 352;
  68. mode_config->max_width = 720;
  69. mode_config->min_height = 240;
  70. mode_config->max_height = 576;
  71. ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
  72. 0, 0, &panel, &bridge);
  73. if (ret && ret != -ENODEV)
  74. return ret;
  75. if (panel) {
  76. bridge = drm_panel_bridge_add_typed(panel,
  77. DRM_MODE_CONNECTOR_Unknown);
  78. if (IS_ERR(bridge)) {
  79. ret = PTR_ERR(bridge);
  80. goto out_bridge;
  81. }
  82. } else {
  83. /*
  84. * TODO: when we are using a different bridge than a panel
  85. * (such as a dumb VGA connector) we need to devise a different
  86. * method to get the connector out of the bridge.
  87. */
  88. dev_err(dev->dev, "the bridge is not a panel\n");
  89. ret = -EINVAL;
  90. goto out_bridge;
  91. }
  92. ret = tve200_display_init(dev);
  93. if (ret) {
  94. dev_err(dev->dev, "failed to init display\n");
  95. goto out_bridge;
  96. }
  97. ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
  98. bridge);
  99. if (ret) {
  100. dev_err(dev->dev, "failed to attach bridge\n");
  101. goto out_bridge;
  102. }
  103. priv->panel = panel;
  104. priv->connector = drm_panel_bridge_connector(bridge);
  105. priv->bridge = bridge;
  106. dev_info(dev->dev, "attached to panel %s\n",
  107. dev_name(panel->dev));
  108. ret = drm_vblank_init(dev, 1);
  109. if (ret) {
  110. dev_err(dev->dev, "failed to init vblank\n");
  111. goto out_bridge;
  112. }
  113. drm_mode_config_reset(dev);
  114. drm_kms_helper_poll_init(dev);
  115. goto finish;
  116. out_bridge:
  117. if (panel)
  118. drm_panel_bridge_remove(bridge);
  119. drm_mode_config_cleanup(dev);
  120. finish:
  121. return ret;
  122. }
  123. DEFINE_DRM_GEM_DMA_FOPS(drm_fops);
  124. static const struct drm_driver tve200_drm_driver = {
  125. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  126. .ioctls = NULL,
  127. .fops = &drm_fops,
  128. .name = "tve200",
  129. .desc = DRIVER_DESC,
  130. .major = 1,
  131. .minor = 0,
  132. .patchlevel = 0,
  133. DRM_GEM_DMA_DRIVER_OPS,
  134. DRM_FBDEV_DMA_DRIVER_OPS,
  135. };
  136. static int tve200_probe(struct platform_device *pdev)
  137. {
  138. struct device *dev = &pdev->dev;
  139. struct tve200_drm_dev_private *priv;
  140. struct drm_device *drm;
  141. int irq;
  142. int ret;
  143. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. drm = drm_dev_alloc(&tve200_drm_driver, dev);
  147. if (IS_ERR(drm))
  148. return PTR_ERR(drm);
  149. platform_set_drvdata(pdev, drm);
  150. priv->drm = drm;
  151. drm->dev_private = priv;
  152. /* Clock the silicon so we can access the registers */
  153. priv->pclk = devm_clk_get(dev, "PCLK");
  154. if (IS_ERR(priv->pclk)) {
  155. dev_err(dev, "unable to get PCLK\n");
  156. ret = PTR_ERR(priv->pclk);
  157. goto dev_unref;
  158. }
  159. ret = clk_prepare_enable(priv->pclk);
  160. if (ret) {
  161. dev_err(dev, "failed to enable PCLK\n");
  162. goto dev_unref;
  163. }
  164. /* This clock is for the pixels (27MHz) */
  165. priv->clk = devm_clk_get(dev, "TVE");
  166. if (IS_ERR(priv->clk)) {
  167. dev_err(dev, "unable to get TVE clock\n");
  168. ret = PTR_ERR(priv->clk);
  169. goto clk_disable;
  170. }
  171. priv->regs = devm_platform_ioremap_resource(pdev, 0);
  172. if (IS_ERR(priv->regs)) {
  173. dev_err(dev, "%s failed mmio\n", __func__);
  174. ret = -EINVAL;
  175. goto clk_disable;
  176. }
  177. irq = platform_get_irq(pdev, 0);
  178. if (irq < 0) {
  179. ret = irq;
  180. goto clk_disable;
  181. }
  182. /* turn off interrupts before requesting the irq */
  183. writel(0, priv->regs + TVE200_INT_EN);
  184. ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
  185. if (ret) {
  186. dev_err(dev, "failed to request irq %d\n", ret);
  187. goto clk_disable;
  188. }
  189. ret = tve200_modeset_init(drm);
  190. if (ret)
  191. goto clk_disable;
  192. ret = drm_dev_register(drm, 0);
  193. if (ret < 0)
  194. goto clk_disable;
  195. drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565);
  196. return 0;
  197. clk_disable:
  198. clk_disable_unprepare(priv->pclk);
  199. dev_unref:
  200. drm_dev_put(drm);
  201. return ret;
  202. }
  203. static void tve200_remove(struct platform_device *pdev)
  204. {
  205. struct drm_device *drm = platform_get_drvdata(pdev);
  206. struct tve200_drm_dev_private *priv = drm->dev_private;
  207. drm_dev_unregister(drm);
  208. drm_atomic_helper_shutdown(drm);
  209. if (priv->panel)
  210. drm_panel_bridge_remove(priv->bridge);
  211. drm_mode_config_cleanup(drm);
  212. clk_disable_unprepare(priv->pclk);
  213. drm_dev_put(drm);
  214. }
  215. static void tve200_shutdown(struct platform_device *pdev)
  216. {
  217. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  218. }
  219. static const struct of_device_id tve200_of_match[] = {
  220. {
  221. .compatible = "faraday,tve200",
  222. },
  223. {},
  224. };
  225. static struct platform_driver tve200_driver = {
  226. .driver = {
  227. .name = "tve200",
  228. .of_match_table = tve200_of_match,
  229. },
  230. .probe = tve200_probe,
  231. .remove = tve200_remove,
  232. .shutdown = tve200_shutdown,
  233. };
  234. drm_module_platform_driver(tve200_driver);
  235. MODULE_DESCRIPTION(DRIVER_DESC);
  236. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  237. MODULE_LICENSE("GPL");