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 /**
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/of.h>
36 #include <linux/platform_device.h>
37 #include <linux/shmem_fs.h>
38 #include <linux/slab.h>
39
40 #include <drm/clients/drm_client_setup.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_bridge.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_fbdev_dma.h>
45 #include <drm/drm_fourcc.h>
46 #include <drm/drm_gem_dma_helper.h>
47 #include <drm/drm_gem_framebuffer_helper.h>
48 #include <drm/drm_module.h>
49 #include <drm/drm_of.h>
50 #include <drm/drm_panel.h>
51 #include <drm/drm_probe_helper.h>
52 #include <drm/drm_vblank.h>
53
54 #include "tve200_drm.h"
55
56 #define DRIVER_DESC "DRM module for Faraday TVE200"
57
58 static const struct drm_mode_config_funcs mode_config_funcs = {
59 .fb_create = drm_gem_fb_create,
60 .atomic_check = drm_atomic_helper_check,
61 .atomic_commit = drm_atomic_helper_commit,
62 };
63
tve200_modeset_init(struct drm_device * dev)64 static int tve200_modeset_init(struct drm_device *dev)
65 {
66 struct drm_mode_config *mode_config;
67 struct tve200_drm_dev_private *priv = dev->dev_private;
68 struct drm_panel *panel;
69 struct drm_bridge *bridge;
70 int ret;
71
72 drm_mode_config_init(dev);
73 mode_config = &dev->mode_config;
74 mode_config->funcs = &mode_config_funcs;
75 mode_config->min_width = 352;
76 mode_config->max_width = 720;
77 mode_config->min_height = 240;
78 mode_config->max_height = 576;
79
80 ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
81 0, 0, &panel, &bridge);
82 if (ret && ret != -ENODEV)
83 return ret;
84 if (panel) {
85 bridge = drm_panel_bridge_add_typed(panel,
86 DRM_MODE_CONNECTOR_Unknown);
87 if (IS_ERR(bridge)) {
88 ret = PTR_ERR(bridge);
89 goto out_bridge;
90 }
91 } else {
92 /*
93 * TODO: when we are using a different bridge than a panel
94 * (such as a dumb VGA connector) we need to devise a different
95 * method to get the connector out of the bridge.
96 */
97 dev_err(dev->dev, "the bridge is not a panel\n");
98 ret = -EINVAL;
99 goto out_bridge;
100 }
101
102 ret = tve200_display_init(dev);
103 if (ret) {
104 dev_err(dev->dev, "failed to init display\n");
105 goto out_bridge;
106 }
107
108 ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
109 bridge);
110 if (ret) {
111 dev_err(dev->dev, "failed to attach bridge\n");
112 goto out_bridge;
113 }
114
115 priv->panel = panel;
116 priv->connector = drm_panel_bridge_connector(bridge);
117 priv->bridge = bridge;
118
119 dev_info(dev->dev, "attached to panel %s\n",
120 dev_name(panel->dev));
121
122 ret = drm_vblank_init(dev, 1);
123 if (ret) {
124 dev_err(dev->dev, "failed to init vblank\n");
125 goto out_bridge;
126 }
127
128 drm_mode_config_reset(dev);
129 drm_kms_helper_poll_init(dev);
130
131 goto finish;
132
133 out_bridge:
134 if (panel)
135 drm_panel_bridge_remove(bridge);
136 drm_mode_config_cleanup(dev);
137 finish:
138 return ret;
139 }
140
141 DEFINE_DRM_GEM_DMA_FOPS(drm_fops);
142
143 static const struct drm_driver tve200_drm_driver = {
144 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
145 .ioctls = NULL,
146 .fops = &drm_fops,
147 .name = "tve200",
148 .desc = DRIVER_DESC,
149 .major = 1,
150 .minor = 0,
151 .patchlevel = 0,
152 DRM_GEM_DMA_DRIVER_OPS,
153 DRM_FBDEV_DMA_DRIVER_OPS,
154 };
155
tve200_probe(struct platform_device * pdev)156 static int tve200_probe(struct platform_device *pdev)
157 {
158 struct device *dev = &pdev->dev;
159 struct tve200_drm_dev_private *priv;
160 struct drm_device *drm;
161 int irq;
162 int ret;
163
164 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
165 if (!priv)
166 return -ENOMEM;
167
168 drm = drm_dev_alloc(&tve200_drm_driver, dev);
169 if (IS_ERR(drm))
170 return PTR_ERR(drm);
171 platform_set_drvdata(pdev, drm);
172 priv->drm = drm;
173 drm->dev_private = priv;
174
175 /* Clock the silicon so we can access the registers */
176 priv->pclk = devm_clk_get(dev, "PCLK");
177 if (IS_ERR(priv->pclk)) {
178 dev_err(dev, "unable to get PCLK\n");
179 ret = PTR_ERR(priv->pclk);
180 goto dev_unref;
181 }
182 ret = clk_prepare_enable(priv->pclk);
183 if (ret) {
184 dev_err(dev, "failed to enable PCLK\n");
185 goto dev_unref;
186 }
187
188 /* This clock is for the pixels (27MHz) */
189 priv->clk = devm_clk_get(dev, "TVE");
190 if (IS_ERR(priv->clk)) {
191 dev_err(dev, "unable to get TVE clock\n");
192 ret = PTR_ERR(priv->clk);
193 goto clk_disable;
194 }
195
196 priv->regs = devm_platform_ioremap_resource(pdev, 0);
197 if (IS_ERR(priv->regs)) {
198 dev_err(dev, "%s failed mmio\n", __func__);
199 ret = -EINVAL;
200 goto clk_disable;
201 }
202
203 irq = platform_get_irq(pdev, 0);
204 if (irq < 0) {
205 ret = irq;
206 goto clk_disable;
207 }
208
209 /* turn off interrupts before requesting the irq */
210 writel(0, priv->regs + TVE200_INT_EN);
211
212 ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
213 if (ret) {
214 dev_err(dev, "failed to request irq %d\n", ret);
215 goto clk_disable;
216 }
217
218 ret = tve200_modeset_init(drm);
219 if (ret)
220 goto clk_disable;
221
222 ret = drm_dev_register(drm, 0);
223 if (ret < 0)
224 goto clk_disable;
225
226 drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565);
227
228 return 0;
229
230 clk_disable:
231 clk_disable_unprepare(priv->pclk);
232 dev_unref:
233 drm_dev_put(drm);
234 return ret;
235 }
236
tve200_remove(struct platform_device * pdev)237 static void tve200_remove(struct platform_device *pdev)
238 {
239 struct drm_device *drm = platform_get_drvdata(pdev);
240 struct tve200_drm_dev_private *priv = drm->dev_private;
241
242 drm_dev_unregister(drm);
243 drm_atomic_helper_shutdown(drm);
244 if (priv->panel)
245 drm_panel_bridge_remove(priv->bridge);
246 drm_mode_config_cleanup(drm);
247 clk_disable_unprepare(priv->pclk);
248 drm_dev_put(drm);
249 }
250
tve200_shutdown(struct platform_device * pdev)251 static void tve200_shutdown(struct platform_device *pdev)
252 {
253 drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
254 }
255
256 static const struct of_device_id tve200_of_match[] = {
257 {
258 .compatible = "faraday,tve200",
259 },
260 {},
261 };
262
263 static struct platform_driver tve200_driver = {
264 .driver = {
265 .name = "tve200",
266 .of_match_table = tve200_of_match,
267 },
268 .probe = tve200_probe,
269 .remove = tve200_remove,
270 .shutdown = tve200_shutdown,
271 };
272 drm_module_platform_driver(tve200_driver);
273
274 MODULE_DESCRIPTION(DRIVER_DESC);
275 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
276 MODULE_LICENSE("GPL");
277