Loading...
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/drm_atomic_helper.h>
41#include <drm/drm_bridge.h>
42#include <drm/drm_client_setup.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
58static 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
64static 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
133out_bridge:
134 if (panel)
135 drm_panel_bridge_remove(bridge);
136 drm_mode_config_cleanup(dev);
137finish:
138 return ret;
139}
140
141DEFINE_DRM_GEM_DMA_FOPS(drm_fops);
142
143static 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 .date = "20170703",
150 .major = 1,
151 .minor = 0,
152 .patchlevel = 0,
153 DRM_GEM_DMA_DRIVER_OPS,
154 DRM_FBDEV_DMA_DRIVER_OPS,
155};
156
157static int tve200_probe(struct platform_device *pdev)
158{
159 struct device *dev = &pdev->dev;
160 struct tve200_drm_dev_private *priv;
161 struct drm_device *drm;
162 int irq;
163 int ret;
164
165 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
166 if (!priv)
167 return -ENOMEM;
168
169 drm = drm_dev_alloc(&tve200_drm_driver, dev);
170 if (IS_ERR(drm))
171 return PTR_ERR(drm);
172 platform_set_drvdata(pdev, drm);
173 priv->drm = drm;
174 drm->dev_private = priv;
175
176 /* Clock the silicon so we can access the registers */
177 priv->pclk = devm_clk_get(dev, "PCLK");
178 if (IS_ERR(priv->pclk)) {
179 dev_err(dev, "unable to get PCLK\n");
180 ret = PTR_ERR(priv->pclk);
181 goto dev_unref;
182 }
183 ret = clk_prepare_enable(priv->pclk);
184 if (ret) {
185 dev_err(dev, "failed to enable PCLK\n");
186 goto dev_unref;
187 }
188
189 /* This clock is for the pixels (27MHz) */
190 priv->clk = devm_clk_get(dev, "TVE");
191 if (IS_ERR(priv->clk)) {
192 dev_err(dev, "unable to get TVE clock\n");
193 ret = PTR_ERR(priv->clk);
194 goto clk_disable;
195 }
196
197 priv->regs = devm_platform_ioremap_resource(pdev, 0);
198 if (IS_ERR(priv->regs)) {
199 dev_err(dev, "%s failed mmio\n", __func__);
200 ret = -EINVAL;
201 goto clk_disable;
202 }
203
204 irq = platform_get_irq(pdev, 0);
205 if (irq < 0) {
206 ret = irq;
207 goto clk_disable;
208 }
209
210 /* turn off interrupts before requesting the irq */
211 writel(0, priv->regs + TVE200_INT_EN);
212
213 ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
214 if (ret) {
215 dev_err(dev, "failed to request irq %d\n", ret);
216 goto clk_disable;
217 }
218
219 ret = tve200_modeset_init(drm);
220 if (ret)
221 goto clk_disable;
222
223 ret = drm_dev_register(drm, 0);
224 if (ret < 0)
225 goto clk_disable;
226
227 drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565);
228
229 return 0;
230
231clk_disable:
232 clk_disable_unprepare(priv->pclk);
233dev_unref:
234 drm_dev_put(drm);
235 return ret;
236}
237
238static void tve200_remove(struct platform_device *pdev)
239{
240 struct drm_device *drm = platform_get_drvdata(pdev);
241 struct tve200_drm_dev_private *priv = drm->dev_private;
242
243 drm_dev_unregister(drm);
244 drm_atomic_helper_shutdown(drm);
245 if (priv->panel)
246 drm_panel_bridge_remove(priv->bridge);
247 drm_mode_config_cleanup(drm);
248 clk_disable_unprepare(priv->pclk);
249 drm_dev_put(drm);
250}
251
252static void tve200_shutdown(struct platform_device *pdev)
253{
254 drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
255}
256
257static const struct of_device_id tve200_of_match[] = {
258 {
259 .compatible = "faraday,tve200",
260 },
261 {},
262};
263
264static struct platform_driver tve200_driver = {
265 .driver = {
266 .name = "tve200",
267 .of_match_table = tve200_of_match,
268 },
269 .probe = tve200_probe,
270 .remove = tve200_remove,
271 .shutdown = tve200_shutdown,
272};
273drm_module_platform_driver(tve200_driver);
274
275MODULE_DESCRIPTION(DRIVER_DESC);
276MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
277MODULE_LICENSE("GPL");
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
39#include <drm/drm_atomic_helper.h>
40#include <drm/drm_bridge.h>
41#include <drm/drm_drv.h>
42#include <drm/drm_fb_cma_helper.h>
43#include <drm/drm_fb_helper.h>
44#include <drm/drm_gem_cma_helper.h>
45#include <drm/drm_gem_framebuffer_helper.h>
46#include <drm/drm_of.h>
47#include <drm/drm_panel.h>
48#include <drm/drm_probe_helper.h>
49#include <drm/drm_vblank.h>
50
51#include "tve200_drm.h"
52
53#define DRIVER_DESC "DRM module for Faraday TVE200"
54
55static 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
61static 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_typed(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 = drm_panel_bridge_connector(bridge);
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
129out_bridge:
130 if (panel)
131 drm_panel_bridge_remove(bridge);
132 drm_mode_config_cleanup(dev);
133finish:
134 return ret;
135}
136
137DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
138
139static const struct drm_driver tve200_drm_driver = {
140 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
141 .ioctls = NULL,
142 .fops = &drm_fops,
143 .name = "tve200",
144 .desc = DRIVER_DESC,
145 .date = "20170703",
146 .major = 1,
147 .minor = 0,
148 .patchlevel = 0,
149 DRM_GEM_CMA_DRIVER_OPS,
150};
151
152static int tve200_probe(struct platform_device *pdev)
153{
154 struct device *dev = &pdev->dev;
155 struct tve200_drm_dev_private *priv;
156 struct drm_device *drm;
157 struct resource *res;
158 int irq;
159 int ret;
160
161 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
162 if (!priv)
163 return -ENOMEM;
164
165 drm = drm_dev_alloc(&tve200_drm_driver, dev);
166 if (IS_ERR(drm))
167 return PTR_ERR(drm);
168 platform_set_drvdata(pdev, drm);
169 priv->drm = drm;
170 drm->dev_private = priv;
171
172 /* Clock the silicon so we can access the registers */
173 priv->pclk = devm_clk_get(dev, "PCLK");
174 if (IS_ERR(priv->pclk)) {
175 dev_err(dev, "unable to get PCLK\n");
176 ret = PTR_ERR(priv->pclk);
177 goto dev_unref;
178 }
179 ret = clk_prepare_enable(priv->pclk);
180 if (ret) {
181 dev_err(dev, "failed to enable PCLK\n");
182 goto dev_unref;
183 }
184
185 /* This clock is for the pixels (27MHz) */
186 priv->clk = devm_clk_get(dev, "TVE");
187 if (IS_ERR(priv->clk)) {
188 dev_err(dev, "unable to get TVE clock\n");
189 ret = PTR_ERR(priv->clk);
190 goto clk_disable;
191 }
192
193 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
194 priv->regs = devm_ioremap_resource(dev, res);
195 if (IS_ERR(priv->regs)) {
196 dev_err(dev, "%s failed mmio\n", __func__);
197 ret = -EINVAL;
198 goto clk_disable;
199 }
200
201 irq = platform_get_irq(pdev, 0);
202 if (irq < 0) {
203 ret = irq;
204 goto clk_disable;
205 }
206
207 /* turn off interrupts before requesting the irq */
208 writel(0, priv->regs + TVE200_INT_EN);
209
210 ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv);
211 if (ret) {
212 dev_err(dev, "failed to request irq %d\n", ret);
213 goto clk_disable;
214 }
215
216 ret = tve200_modeset_init(drm);
217 if (ret)
218 goto clk_disable;
219
220 ret = drm_dev_register(drm, 0);
221 if (ret < 0)
222 goto clk_disable;
223
224 /*
225 * Passing in 16 here will make the RGB565 mode the default
226 * Passing in 32 will use XRGB8888 mode
227 */
228 drm_fbdev_generic_setup(drm, 16);
229
230 return 0;
231
232clk_disable:
233 clk_disable_unprepare(priv->pclk);
234dev_unref:
235 drm_dev_put(drm);
236 return ret;
237}
238
239static int tve200_remove(struct platform_device *pdev)
240{
241 struct drm_device *drm = platform_get_drvdata(pdev);
242 struct tve200_drm_dev_private *priv = drm->dev_private;
243
244 drm_dev_unregister(drm);
245 if (priv->panel)
246 drm_panel_bridge_remove(priv->bridge);
247 drm_mode_config_cleanup(drm);
248 clk_disable_unprepare(priv->pclk);
249 drm_dev_put(drm);
250
251 return 0;
252}
253
254static const struct of_device_id tve200_of_match[] = {
255 {
256 .compatible = "faraday,tve200",
257 },
258 {},
259};
260
261static struct platform_driver tve200_driver = {
262 .driver = {
263 .name = "tve200",
264 .of_match_table = of_match_ptr(tve200_of_match),
265 },
266 .probe = tve200_probe,
267 .remove = tve200_remove,
268};
269module_platform_driver(tve200_driver);
270
271MODULE_DESCRIPTION(DRIVER_DESC);
272MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
273MODULE_LICENSE("GPL");