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