2 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
3 * Parts of this file were based on sources as follows:
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
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
18 * DOC: Faraday TV Encoder TVE200 DRM Driver
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.
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.
31 * This driver exposes a standard KMS interface for this TV encoder.
34 #include <linux/clk.h>
35 #include <linux/dma-buf.h>
36 #include <linux/irq.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>
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>
55 #include "tve200_drm.h"
57 #define DRIVER_DESC "DRM module for Faraday TVE200"
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
,
65 static int tve200_modeset_init(struct drm_device
*dev
)
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
;
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;
81 ret
= drm_of_find_panel_or_bridge(dev
->dev
->of_node
,
82 0, 0, &panel
, &bridge
);
83 if (ret
&& ret
!= -ENODEV
)
86 bridge
= drm_panel_bridge_add(panel
,
87 DRM_MODE_CONNECTOR_Unknown
);
89 ret
= PTR_ERR(bridge
);
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.
98 dev_err(dev
->dev
, "the bridge is not a panel\n");
102 ret
= tve200_display_init(dev
);
104 dev_err(dev
->dev
, "failed to init display\n");
108 ret
= drm_simple_display_pipe_attach_bridge(&priv
->pipe
,
111 dev_err(dev
->dev
, "failed to attach bridge\n");
116 priv
->connector
= panel
->connector
;
117 priv
->bridge
= bridge
;
119 dev_info(dev
->dev
, "attached to panel %s\n",
120 dev_name(panel
->dev
));
122 ret
= drm_vblank_init(dev
, 1);
124 dev_err(dev
->dev
, "failed to init vblank\n");
128 drm_mode_config_reset(dev
);
131 * Passing in 16 here will make the RGB656 mode the default
132 * Passing in 32 will use XRGB8888 mode
134 drm_fb_cma_fbdev_init(dev
, 16, 0);
135 drm_kms_helper_poll_init(dev
);
141 drm_panel_bridge_remove(bridge
);
142 drm_mode_config_cleanup(dev
);
147 DEFINE_DRM_GEM_CMA_FOPS(drm_fops
);
149 static struct drm_driver tve200_drm_driver
= {
151 DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
| DRIVER_ATOMIC
,
152 .lastclose
= drm_fb_helper_lastclose
,
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
,
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
,
176 static int tve200_probe(struct platform_device
*pdev
)
178 struct device
*dev
= &pdev
->dev
;
179 struct tve200_drm_dev_private
*priv
;
180 struct drm_device
*drm
;
181 struct resource
*res
;
185 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
189 drm
= drm_dev_alloc(&tve200_drm_driver
, dev
);
192 platform_set_drvdata(pdev
, drm
);
194 drm
->dev_private
= priv
;
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
);
203 ret
= clk_prepare_enable(priv
->pclk
);
205 dev_err(dev
, "failed to enable PCLK\n");
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
);
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__
);
225 irq
= platform_get_irq(pdev
, 0);
231 /* turn off interrupts before requesting the irq */
232 writel(0, priv
->regs
+ TVE200_INT_EN
);
234 ret
= devm_request_irq(dev
, irq
, tve200_irq
, 0, "tve200", priv
);
236 dev_err(dev
, "failed to request irq %d\n", ret
);
240 ret
= tve200_modeset_init(drm
);
244 ret
= drm_dev_register(drm
, 0);
251 clk_disable_unprepare(priv
->pclk
);
257 static int tve200_remove(struct platform_device
*pdev
)
259 struct drm_device
*drm
= platform_get_drvdata(pdev
);
260 struct tve200_drm_dev_private
*priv
= drm
->dev_private
;
262 drm_dev_unregister(drm
);
263 drm_fb_cma_fbdev_fini(drm
);
265 drm_panel_bridge_remove(priv
->bridge
);
266 drm_mode_config_cleanup(drm
);
267 clk_disable_unprepare(priv
->pclk
);
273 static const struct of_device_id tve200_of_match
[] = {
275 .compatible
= "faraday,tve200",
280 static struct platform_driver tve200_driver
= {
283 .of_match_table
= of_match_ptr(tve200_of_match
),
285 .probe
= tve200_probe
,
286 .remove
= tve200_remove
,
288 module_platform_driver(tve200_driver
);
290 MODULE_DESCRIPTION(DRIVER_DESC
);
291 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
292 MODULE_LICENSE("GPL");