blob: 954b09c948ebe3b1089bb603d839d542c01939db [file] [log] [blame]
Thomas Gleixnere5593552019-06-01 10:08:57 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij179c02f2017-08-20 12:05:55 +02002/*
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
Linus Walleij179c02f2017-08-20 12:05:55 +020011 */
12
13/**
14 * DOC: Faraday TV Encoder TVE200 DRM Driver
15 *
16 * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
17 * Controller (TVC) and is found in the Gemini Chipset from Storlink
18 * Semiconductor (later Storm Semiconductor, later Cortina Systems)
19 * but also in the Grain Media GM8180 chipset. On the Gemini the module
20 * is connected to 8 data lines and a single clock line, comprising an
21 * 8-bit BT.656 interface.
22 *
23 * This is a very basic YUV display driver. The datasheet specifies that
24 * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
25 * the hallmark of any TV encoder supporting both PAL and NTSC.
26 *
27 * This driver exposes a standard KMS interface for this TV encoder.
28 */
29
30#include <linux/clk.h>
31#include <linux/dma-buf.h>
32#include <linux/irq.h>
33#include <linux/io.h>
34#include <linux/module.h>
35#include <linux/platform_device.h>
36#include <linux/shmem_fs.h>
37#include <linux/slab.h>
38#include <linux/version.h>
39
Linus Walleij179c02f2017-08-20 12:05:55 +020040#include <drm/drm_atomic_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010041#include <drm/drm_bridge.h>
Sam Ravnborg91328eb2019-06-30 08:18:53 +020042#include <drm/drm_drv.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010043#include <drm/drm_fb_cma_helper.h>
44#include <drm/drm_fb_helper.h>
Linus Walleij179c02f2017-08-20 12:05:55 +020045#include <drm/drm_gem_cma_helper.h>
Noralf Trønnes57b8a4b2017-09-24 14:26:24 +020046#include <drm/drm_gem_framebuffer_helper.h>
Linus Walleij14b469f92017-09-02 22:07:11 +020047#include <drm/drm_of.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010048#include <drm/drm_panel.h>
49#include <drm/drm_probe_helper.h>
Sam Ravnborg91328eb2019-06-30 08:18:53 +020050#include <drm/drm_vblank.h>
Linus Walleij179c02f2017-08-20 12:05:55 +020051
52#include "tve200_drm.h"
53
54#define DRIVER_DESC "DRM module for Faraday TVE200"
55
56static const struct drm_mode_config_funcs mode_config_funcs = {
Noralf Trønnes57b8a4b2017-09-24 14:26:24 +020057 .fb_create = drm_gem_fb_create,
Linus Walleij179c02f2017-08-20 12:05:55 +020058 .atomic_check = drm_atomic_helper_check,
59 .atomic_commit = drm_atomic_helper_commit,
60};
61
62static int tve200_modeset_init(struct drm_device *dev)
63{
64 struct drm_mode_config *mode_config;
65 struct tve200_drm_dev_private *priv = dev->dev_private;
Linus Walleij14b469f92017-09-02 22:07:11 +020066 struct drm_panel *panel;
67 struct drm_bridge *bridge;
Linus Walleij179c02f2017-08-20 12:05:55 +020068 int ret = 0;
69
70 drm_mode_config_init(dev);
71 mode_config = &dev->mode_config;
72 mode_config->funcs = &mode_config_funcs;
73 mode_config->min_width = 352;
74 mode_config->max_width = 720;
75 mode_config->min_height = 240;
76 mode_config->max_height = 576;
77
Linus Walleij14b469f92017-09-02 22:07:11 +020078 ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
79 0, 0, &panel, &bridge);
80 if (ret && ret != -ENODEV)
81 return ret;
82 if (panel) {
Laurent Pinchart89958b72019-09-04 16:28:04 +030083 bridge = drm_panel_bridge_add_typed(panel,
84 DRM_MODE_CONNECTOR_Unknown);
Linus Walleij14b469f92017-09-02 22:07:11 +020085 if (IS_ERR(bridge)) {
86 ret = PTR_ERR(bridge);
87 goto out_bridge;
88 }
Linus Walleij9ab12e82017-09-11 00:08:01 +020089 } else {
90 /*
91 * TODO: when we are using a different bridge than a panel
92 * (such as a dumb VGA connector) we need to devise a different
93 * method to get the connector out of the bridge.
94 */
95 dev_err(dev->dev, "the bridge is not a panel\n");
96 goto out_bridge;
Linus Walleij179c02f2017-08-20 12:05:55 +020097 }
98
Linus Walleij179c02f2017-08-20 12:05:55 +020099 ret = tve200_display_init(dev);
100 if (ret) {
101 dev_err(dev->dev, "failed to init display\n");
Linus Walleij14b469f92017-09-02 22:07:11 +0200102 goto out_bridge;
Linus Walleij179c02f2017-08-20 12:05:55 +0200103 }
104
Linus Walleij9ab12e82017-09-11 00:08:01 +0200105 ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
106 bridge);
107 if (ret) {
108 dev_err(dev->dev, "failed to attach bridge\n");
Linus Walleij14b469f92017-09-02 22:07:11 +0200109 goto out_bridge;
110 }
Linus Walleij9ab12e82017-09-11 00:08:01 +0200111
Linus Walleij14b469f92017-09-02 22:07:11 +0200112 priv->panel = panel;
113 priv->connector = panel->connector;
114 priv->bridge = bridge;
115
116 dev_info(dev->dev, "attached to panel %s\n",
117 dev_name(panel->dev));
118
Linus Walleij179c02f2017-08-20 12:05:55 +0200119 ret = drm_vblank_init(dev, 1);
120 if (ret) {
121 dev_err(dev->dev, "failed to init vblank\n");
Linus Walleij14b469f92017-09-02 22:07:11 +0200122 goto out_bridge;
Linus Walleij179c02f2017-08-20 12:05:55 +0200123 }
124
125 drm_mode_config_reset(dev);
Linus Walleij179c02f2017-08-20 12:05:55 +0200126 drm_kms_helper_poll_init(dev);
127
128 goto finish;
129
Linus Walleij14b469f92017-09-02 22:07:11 +0200130out_bridge:
131 if (panel)
132 drm_panel_bridge_remove(bridge);
Linus Walleij179c02f2017-08-20 12:05:55 +0200133 drm_mode_config_cleanup(dev);
134finish:
135 return ret;
136}
137
138DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
139
Linus Walleij179c02f2017-08-20 12:05:55 +0200140static struct drm_driver tve200_drm_driver = {
Daniel Vetter0424fda2019-06-17 17:39:24 +0200141 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
Linus Walleij179c02f2017-08-20 12:05:55 +0200142 .ioctls = NULL,
143 .fops = &drm_fops,
144 .name = "tve200",
145 .desc = DRIVER_DESC,
146 .date = "20170703",
147 .major = 1,
148 .minor = 0,
149 .patchlevel = 0,
150 .dumb_create = drm_gem_cma_dumb_create,
151 .gem_free_object_unlocked = drm_gem_cma_free_object,
152 .gem_vm_ops = &drm_gem_cma_vm_ops,
153
Linus Walleij179c02f2017-08-20 12:05:55 +0200154 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
155 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Linus Walleij179c02f2017-08-20 12:05:55 +0200156 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
157 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
158 .gem_prime_vmap = drm_gem_cma_prime_vmap,
159 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
160 .gem_prime_mmap = drm_gem_cma_prime_mmap,
161};
162
163static int tve200_probe(struct platform_device *pdev)
164{
165 struct device *dev = &pdev->dev;
166 struct tve200_drm_dev_private *priv;
167 struct drm_device *drm;
168 struct resource *res;
169 int irq;
170 int ret;
171
172 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
173 if (!priv)
174 return -ENOMEM;
175
176 drm = drm_dev_alloc(&tve200_drm_driver, dev);
177 if (IS_ERR(drm))
178 return PTR_ERR(drm);
179 platform_set_drvdata(pdev, drm);
180 priv->drm = drm;
181 drm->dev_private = priv;
182
183 /* Clock the silicon so we can access the registers */
184 priv->pclk = devm_clk_get(dev, "PCLK");
185 if (IS_ERR(priv->pclk)) {
186 dev_err(dev, "unable to get PCLK\n");
187 ret = PTR_ERR(priv->pclk);
188 goto dev_unref;
189 }
190 ret = clk_prepare_enable(priv->pclk);
191 if (ret) {
192 dev_err(dev, "failed to enable PCLK\n");
193 goto dev_unref;
194 }
195
196 /* This clock is for the pixels (27MHz) */
197 priv->clk = devm_clk_get(dev, "TVE");
198 if (IS_ERR(priv->clk)) {
199 dev_err(dev, "unable to get TVE clock\n");
200 ret = PTR_ERR(priv->clk);
201 goto clk_disable;
202 }
203
204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
205 priv->regs = devm_ioremap_resource(dev, res);
Dan Carpenter44390ef2017-09-25 13:25:20 +0300206 if (IS_ERR(priv->regs)) {
Linus Walleij179c02f2017-08-20 12:05:55 +0200207 dev_err(dev, "%s failed mmio\n", __func__);
208 ret = -EINVAL;
209 goto clk_disable;
210 }
211
212 irq = platform_get_irq(pdev, 0);
213 if (!irq) {
214 ret = -EINVAL;
215 goto clk_disable;
216 }
217
218 /* turn off interrupts before requesting the irq */
219 writel(0, priv->regs + TVE200_INT_EN);
220
221 ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
222 if (ret) {
223 dev_err(dev, "failed to request irq %d\n", ret);
224 goto clk_disable;
225 }
226
227 ret = tve200_modeset_init(drm);
228 if (ret)
229 goto clk_disable;
230
231 ret = drm_dev_register(drm, 0);
232 if (ret < 0)
233 goto clk_disable;
234
Noralf Trønnes1e70d7a2018-09-08 15:46:45 +0200235 /*
236 * Passing in 16 here will make the RGB565 mode the default
237 * Passing in 32 will use XRGB8888 mode
238 */
239 drm_fbdev_generic_setup(drm, 16);
240
Linus Walleij179c02f2017-08-20 12:05:55 +0200241 return 0;
242
243clk_disable:
244 clk_disable_unprepare(priv->pclk);
245dev_unref:
Fernando Ramos808bad32018-11-15 23:16:23 +0100246 drm_dev_put(drm);
Linus Walleij179c02f2017-08-20 12:05:55 +0200247 return ret;
248}
249
250static int tve200_remove(struct platform_device *pdev)
251{
252 struct drm_device *drm = platform_get_drvdata(pdev);
253 struct tve200_drm_dev_private *priv = drm->dev_private;
254
255 drm_dev_unregister(drm);
Linus Walleij14b469f92017-09-02 22:07:11 +0200256 if (priv->panel)
257 drm_panel_bridge_remove(priv->bridge);
Linus Walleij179c02f2017-08-20 12:05:55 +0200258 drm_mode_config_cleanup(drm);
259 clk_disable_unprepare(priv->pclk);
Fernando Ramos808bad32018-11-15 23:16:23 +0100260 drm_dev_put(drm);
Linus Walleij179c02f2017-08-20 12:05:55 +0200261
262 return 0;
263}
264
265static const struct of_device_id tve200_of_match[] = {
266 {
267 .compatible = "faraday,tve200",
268 },
269 {},
270};
271
272static struct platform_driver tve200_driver = {
273 .driver = {
274 .name = "tve200",
275 .of_match_table = of_match_ptr(tve200_of_match),
276 },
277 .probe = tve200_probe,
278 .remove = tve200_remove,
279};
280module_platform_driver(tve200_driver);
281
282MODULE_DESCRIPTION(DRIVER_DESC);
283MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
284MODULE_LICENSE("GPL");