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 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_CMA_FOPS(drm_fops); 142 143 static struct drm_driver tve200_drm_driver = { 144 .driver_features = 145 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC, 146 .ioctls = NULL, 147 .fops = &drm_fops, 148 .name = "tve200", 149 .desc = DRIVER_DESC, 150 .date = "20170703", 151 .major = 1, 152 .minor = 0, 153 .patchlevel = 0, 154 .dumb_create = drm_gem_cma_dumb_create, 155 .gem_free_object_unlocked = drm_gem_cma_free_object, 156 .gem_vm_ops = &drm_gem_cma_vm_ops, 157 158 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 159 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 160 .gem_prime_import = drm_gem_prime_import, 161 .gem_prime_export = drm_gem_prime_export, 162 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 163 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 164 .gem_prime_vmap = drm_gem_cma_prime_vmap, 165 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 166 .gem_prime_mmap = drm_gem_cma_prime_mmap, 167 }; 168 169 static int tve200_probe(struct platform_device *pdev) 170 { 171 struct device *dev = &pdev->dev; 172 struct tve200_drm_dev_private *priv; 173 struct drm_device *drm; 174 struct resource *res; 175 int irq; 176 int ret; 177 178 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 179 if (!priv) 180 return -ENOMEM; 181 182 drm = drm_dev_alloc(&tve200_drm_driver, dev); 183 if (IS_ERR(drm)) 184 return PTR_ERR(drm); 185 platform_set_drvdata(pdev, drm); 186 priv->drm = drm; 187 drm->dev_private = priv; 188 189 /* Clock the silicon so we can access the registers */ 190 priv->pclk = devm_clk_get(dev, "PCLK"); 191 if (IS_ERR(priv->pclk)) { 192 dev_err(dev, "unable to get PCLK\n"); 193 ret = PTR_ERR(priv->pclk); 194 goto dev_unref; 195 } 196 ret = clk_prepare_enable(priv->pclk); 197 if (ret) { 198 dev_err(dev, "failed to enable PCLK\n"); 199 goto dev_unref; 200 } 201 202 /* This clock is for the pixels (27MHz) */ 203 priv->clk = devm_clk_get(dev, "TVE"); 204 if (IS_ERR(priv->clk)) { 205 dev_err(dev, "unable to get TVE clock\n"); 206 ret = PTR_ERR(priv->clk); 207 goto clk_disable; 208 } 209 210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 211 priv->regs = devm_ioremap_resource(dev, res); 212 if (IS_ERR(priv->regs)) { 213 dev_err(dev, "%s failed mmio\n", __func__); 214 ret = -EINVAL; 215 goto clk_disable; 216 } 217 218 irq = platform_get_irq(pdev, 0); 219 if (!irq) { 220 ret = -EINVAL; 221 goto clk_disable; 222 } 223 224 /* turn off interrupts before requesting the irq */ 225 writel(0, priv->regs + TVE200_INT_EN); 226 227 ret = devm_request_irq(dev, irq, tve200_irq, 0, "tve200", priv); 228 if (ret) { 229 dev_err(dev, "failed to request irq %d\n", ret); 230 goto clk_disable; 231 } 232 233 ret = tve200_modeset_init(drm); 234 if (ret) 235 goto clk_disable; 236 237 ret = drm_dev_register(drm, 0); 238 if (ret < 0) 239 goto clk_disable; 240 241 /* 242 * Passing in 16 here will make the RGB565 mode the default 243 * Passing in 32 will use XRGB8888 mode 244 */ 245 drm_fbdev_generic_setup(drm, 16); 246 247 return 0; 248 249 clk_disable: 250 clk_disable_unprepare(priv->pclk); 251 dev_unref: 252 drm_dev_put(drm); 253 return ret; 254 } 255 256 static int tve200_remove(struct platform_device *pdev) 257 { 258 struct drm_device *drm = platform_get_drvdata(pdev); 259 struct tve200_drm_dev_private *priv = drm->dev_private; 260 261 drm_dev_unregister(drm); 262 if (priv->panel) 263 drm_panel_bridge_remove(priv->bridge); 264 drm_mode_config_cleanup(drm); 265 clk_disable_unprepare(priv->pclk); 266 drm_dev_put(drm); 267 268 return 0; 269 } 270 271 static const struct of_device_id tve200_of_match[] = { 272 { 273 .compatible = "faraday,tve200", 274 }, 275 {}, 276 }; 277 278 static struct platform_driver tve200_driver = { 279 .driver = { 280 .name = "tve200", 281 .of_match_table = of_match_ptr(tve200_of_match), 282 }, 283 .probe = tve200_probe, 284 .remove = tve200_remove, 285 }; 286 module_platform_driver(tve200_driver); 287 288 MODULE_DESCRIPTION(DRIVER_DESC); 289 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 290 MODULE_LICENSE("GPL"); 291