xref: /linux/drivers/gpu/drm/tve200/tve200_drv.c (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
1 /*
2  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
3  * Parts of this file were based on sources as follows:
4  *
5  * Copyright (C) 2006-2008 Intel Corporation
6  * Copyright (C) 2007 Amos Lee <amos_lee@storlinksemi.com>
7  * Copyright (C) 2007 Dave Airlie <airlied@linux.ie>
8  * Copyright (C) 2011 Texas Instruments
9  * Copyright (C) 2017 Eric Anholt
10  *
11  * This program is free software and is provided to you under the terms of the
12  * GNU General Public License version 2 as published by the Free Software
13  * Foundation, and any use by you of this program is subject to the terms of
14  * such GNU licence.
15  */
16 
17 /**
18  * DOC: Faraday TV Encoder TVE200 DRM Driver
19  *
20  * The Faraday TV Encoder TVE200 is also known as the Gemini TV Interface
21  * Controller (TVC) and is found in the Gemini Chipset from Storlink
22  * Semiconductor (later Storm Semiconductor, later Cortina Systems)
23  * but also in the Grain Media GM8180 chipset. On the Gemini the module
24  * is connected to 8 data lines and a single clock line, comprising an
25  * 8-bit BT.656 interface.
26  *
27  * This is a very basic YUV display driver. The datasheet specifies that
28  * it supports the ITU BT.656 standard. It requires a 27 MHz clock which is
29  * the hallmark of any TV encoder supporting both PAL and NTSC.
30  *
31  * This driver exposes a standard KMS interface for this TV encoder.
32  */
33 
34 #include <linux/clk.h>
35 #include <linux/dma-buf.h>
36 #include <linux/irq.h>
37 #include <linux/io.h>
38 #include <linux/module.h>
39 #include <linux/platform_device.h>
40 #include <linux/shmem_fs.h>
41 #include <linux/slab.h>
42 #include <linux/version.h>
43 
44 #include <drm/drmP.h>
45 #include <drm/drm_atomic_helper.h>
46 #include <drm/drm_crtc_helper.h>
47 #include <drm/drm_gem_cma_helper.h>
48 #include <drm/drm_gem_framebuffer_helper.h>
49 #include <drm/drm_fb_helper.h>
50 #include <drm/drm_fb_cma_helper.h>
51 #include <drm/drm_panel.h>
52 #include <drm/drm_of.h>
53 #include <drm/drm_bridge.h>
54 
55 #include "tve200_drm.h"
56 
57 #define DRIVER_DESC      "DRM module for Faraday TVE200"
58 
59 static const struct drm_mode_config_funcs mode_config_funcs = {
60 	.fb_create = drm_gem_fb_create,
61 	.atomic_check = drm_atomic_helper_check,
62 	.atomic_commit = drm_atomic_helper_commit,
63 };
64 
65 static int tve200_modeset_init(struct drm_device *dev)
66 {
67 	struct drm_mode_config *mode_config;
68 	struct tve200_drm_dev_private *priv = dev->dev_private;
69 	struct drm_panel *panel;
70 	struct drm_bridge *bridge;
71 	int ret = 0;
72 
73 	drm_mode_config_init(dev);
74 	mode_config = &dev->mode_config;
75 	mode_config->funcs = &mode_config_funcs;
76 	mode_config->min_width = 352;
77 	mode_config->max_width = 720;
78 	mode_config->min_height = 240;
79 	mode_config->max_height = 576;
80 
81 	ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
82 					  0, 0, &panel, &bridge);
83 	if (ret && ret != -ENODEV)
84 		return ret;
85 	if (panel) {
86 		bridge = drm_panel_bridge_add(panel,
87 					      DRM_MODE_CONNECTOR_Unknown);
88 		if (IS_ERR(bridge)) {
89 			ret = PTR_ERR(bridge);
90 			goto out_bridge;
91 		}
92 	} else {
93 		/*
94 		 * TODO: when we are using a different bridge than a panel
95 		 * (such as a dumb VGA connector) we need to devise a different
96 		 * method to get the connector out of the bridge.
97 		 */
98 		dev_err(dev->dev, "the bridge is not a panel\n");
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 = panel->connector;
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 
130 	/*
131 	 * Passing in 16 here will make the RGB656 mode the default
132 	 * Passing in 32 will use XRGB8888 mode
133 	 */
134 	drm_fb_cma_fbdev_init(dev, 16, 0);
135 	drm_kms_helper_poll_init(dev);
136 
137 	goto finish;
138 
139 out_bridge:
140 	if (panel)
141 		drm_panel_bridge_remove(bridge);
142 	drm_mode_config_cleanup(dev);
143 finish:
144 	return ret;
145 }
146 
147 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
148 
149 static struct drm_driver tve200_drm_driver = {
150 	.driver_features =
151 		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
152 	.lastclose = drm_fb_helper_lastclose,
153 	.ioctls = NULL,
154 	.fops = &drm_fops,
155 	.name = "tve200",
156 	.desc = DRIVER_DESC,
157 	.date = "20170703",
158 	.major = 1,
159 	.minor = 0,
160 	.patchlevel = 0,
161 	.dumb_create = drm_gem_cma_dumb_create,
162 	.gem_free_object_unlocked = drm_gem_cma_free_object,
163 	.gem_vm_ops = &drm_gem_cma_vm_ops,
164 
165 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
166 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
167 	.gem_prime_import = drm_gem_prime_import,
168 	.gem_prime_export = drm_gem_prime_export,
169 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
170 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
171 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
172 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
173 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
174 };
175 
176 static int tve200_probe(struct platform_device *pdev)
177 {
178 	struct device *dev = &pdev->dev;
179 	struct tve200_drm_dev_private *priv;
180 	struct drm_device *drm;
181 	struct resource *res;
182 	int irq;
183 	int ret;
184 
185 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
186 	if (!priv)
187 		return -ENOMEM;
188 
189 	drm = drm_dev_alloc(&tve200_drm_driver, dev);
190 	if (IS_ERR(drm))
191 		return PTR_ERR(drm);
192 	platform_set_drvdata(pdev, drm);
193 	priv->drm = drm;
194 	drm->dev_private = priv;
195 
196 	/* Clock the silicon so we can access the registers */
197 	priv->pclk = devm_clk_get(dev, "PCLK");
198 	if (IS_ERR(priv->pclk)) {
199 		dev_err(dev, "unable to get PCLK\n");
200 		ret = PTR_ERR(priv->pclk);
201 		goto dev_unref;
202 	}
203 	ret = clk_prepare_enable(priv->pclk);
204 	if (ret) {
205 		dev_err(dev, "failed to enable PCLK\n");
206 		goto dev_unref;
207 	}
208 
209 	/* This clock is for the pixels (27MHz) */
210 	priv->clk = devm_clk_get(dev, "TVE");
211 	if (IS_ERR(priv->clk)) {
212 		dev_err(dev, "unable to get TVE clock\n");
213 		ret = PTR_ERR(priv->clk);
214 		goto clk_disable;
215 	}
216 
217 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 	priv->regs = devm_ioremap_resource(dev, res);
219 	if (IS_ERR(priv->regs)) {
220 		dev_err(dev, "%s failed mmio\n", __func__);
221 		ret = -EINVAL;
222 		goto clk_disable;
223 	}
224 
225 	irq = platform_get_irq(pdev, 0);
226 	if (!irq) {
227 		ret = -EINVAL;
228 		goto clk_disable;
229 	}
230 
231 	/* turn off interrupts before requesting the irq */
232 	writel(0, priv->regs + TVE200_INT_EN);
233 
234 	ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
235 	if (ret) {
236 		dev_err(dev, "failed to request irq %d\n", ret);
237 		goto clk_disable;
238 	}
239 
240 	ret = tve200_modeset_init(drm);
241 	if (ret)
242 		goto clk_disable;
243 
244 	ret = drm_dev_register(drm, 0);
245 	if (ret < 0)
246 		goto clk_disable;
247 
248 	return 0;
249 
250 clk_disable:
251 	clk_disable_unprepare(priv->pclk);
252 dev_unref:
253 	drm_dev_unref(drm);
254 	return ret;
255 }
256 
257 static int tve200_remove(struct platform_device *pdev)
258 {
259 	struct drm_device *drm = platform_get_drvdata(pdev);
260 	struct tve200_drm_dev_private *priv = drm->dev_private;
261 
262 	drm_dev_unregister(drm);
263 	drm_fb_cma_fbdev_fini(drm);
264 	if (priv->panel)
265 		drm_panel_bridge_remove(priv->bridge);
266 	drm_mode_config_cleanup(drm);
267 	clk_disable_unprepare(priv->pclk);
268 	drm_dev_unref(drm);
269 
270 	return 0;
271 }
272 
273 static const struct of_device_id tve200_of_match[] = {
274 	{
275 		.compatible = "faraday,tve200",
276 	},
277 	{},
278 };
279 
280 static struct platform_driver tve200_driver = {
281 	.driver = {
282 		.name           = "tve200",
283 		.of_match_table = of_match_ptr(tve200_of_match),
284 	},
285 	.probe = tve200_probe,
286 	.remove = tve200_remove,
287 };
288 module_platform_driver(tve200_driver);
289 
290 MODULE_DESCRIPTION(DRIVER_DESC);
291 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
292 MODULE_LICENSE("GPL");
293