linux/drivers/media/platform/vivid/vivid-osd.c
<<
>>
Prefs
   1/*
   2 * vivid-osd.c - osd support for testing overlays.
   3 *
   4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   5 *
   6 * This program is free software; you may redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; version 2 of the License.
   9 *
  10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17 * SOFTWARE.
  18 */
  19
  20#include <linux/module.h>
  21#include <linux/errno.h>
  22#include <linux/kernel.h>
  23#include <linux/init.h>
  24#include <linux/sched.h>
  25#include <linux/slab.h>
  26#include <linux/font.h>
  27#include <linux/mutex.h>
  28#include <linux/videodev2.h>
  29#include <linux/kthread.h>
  30#include <linux/freezer.h>
  31#include <linux/fb.h>
  32#include <media/videobuf2-vmalloc.h>
  33#include <media/v4l2-device.h>
  34#include <media/v4l2-ioctl.h>
  35#include <media/v4l2-ctrls.h>
  36#include <media/v4l2-fh.h>
  37#include <media/v4l2-event.h>
  38#include <media/v4l2-common.h>
  39
  40#include "vivid-core.h"
  41#include "vivid-osd.h"
  42
  43#define MAX_OSD_WIDTH  720
  44#define MAX_OSD_HEIGHT 576
  45
  46/*
  47 * Order: white, yellow, cyan, green, magenta, red, blue, black,
  48 * and same again with the alpha bit set (if any)
  49 */
  50static const u16 rgb555[16] = {
  51        0x7fff, 0x7fe0, 0x03ff, 0x03e0, 0x7c1f, 0x7c00, 0x001f, 0x0000,
  52        0xffff, 0xffe0, 0x83ff, 0x83e0, 0xfc1f, 0xfc00, 0x801f, 0x8000
  53};
  54
  55static const u16 rgb565[16] = {
  56        0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000,
  57        0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000
  58};
  59
  60void vivid_clear_fb(struct vivid_dev *dev)
  61{
  62        void *p = dev->video_vbase;
  63        const u16 *rgb = rgb555;
  64        unsigned x, y;
  65
  66        if (dev->fb_defined.green.length == 6)
  67                rgb = rgb565;
  68
  69        for (y = 0; y < dev->display_height; y++) {
  70                u16 *d = p;
  71
  72                for (x = 0; x < dev->display_width; x++)
  73                        d[x] = rgb[(y / 16 + x / 16) % 16];
  74                p += dev->display_byte_stride;
  75        }
  76}
  77
  78/* --------------------------------------------------------------------- */
  79
  80static int vivid_fb_ioctl(struct fb_info *info, unsigned cmd, unsigned long arg)
  81{
  82        struct vivid_dev *dev = (struct vivid_dev *)info->par;
  83
  84        switch (cmd) {
  85        case FBIOGET_VBLANK: {
  86                struct fb_vblank vblank;
  87
  88                memset(&vblank, 0, sizeof(vblank));
  89                vblank.flags = FB_VBLANK_HAVE_COUNT | FB_VBLANK_HAVE_VCOUNT |
  90                        FB_VBLANK_HAVE_VSYNC;
  91                vblank.count = 0;
  92                vblank.vcount = 0;
  93                vblank.hcount = 0;
  94                if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
  95                        return -EFAULT;
  96                return 0;
  97        }
  98
  99        default:
 100                dprintk(dev, 1, "Unknown ioctl %08x\n", cmd);
 101                return -EINVAL;
 102        }
 103        return 0;
 104}
 105
 106/* Framebuffer device handling */
 107
 108static int vivid_fb_set_var(struct vivid_dev *dev, struct fb_var_screeninfo *var)
 109{
 110        dprintk(dev, 1, "vivid_fb_set_var\n");
 111
 112        if (var->bits_per_pixel != 16) {
 113                dprintk(dev, 1, "vivid_fb_set_var - Invalid bpp\n");
 114                return -EINVAL;
 115        }
 116        dev->display_byte_stride = var->xres * dev->bytes_per_pixel;
 117
 118        return 0;
 119}
 120
 121static int vivid_fb_get_fix(struct vivid_dev *dev, struct fb_fix_screeninfo *fix)
 122{
 123        dprintk(dev, 1, "vivid_fb_get_fix\n");
 124        memset(fix, 0, sizeof(struct fb_fix_screeninfo));
 125        strlcpy(fix->id, "vioverlay fb", sizeof(fix->id));
 126        fix->smem_start = dev->video_pbase;
 127        fix->smem_len = dev->video_buffer_size;
 128        fix->type = FB_TYPE_PACKED_PIXELS;
 129        fix->visual = FB_VISUAL_TRUECOLOR;
 130        fix->xpanstep = 1;
 131        fix->ypanstep = 1;
 132        fix->ywrapstep = 0;
 133        fix->line_length = dev->display_byte_stride;
 134        fix->accel = FB_ACCEL_NONE;
 135        return 0;
 136}
 137
 138/* Check the requested display mode, returning -EINVAL if we can't
 139   handle it. */
 140
 141static int _vivid_fb_check_var(struct fb_var_screeninfo *var, struct vivid_dev *dev)
 142{
 143        dprintk(dev, 1, "vivid_fb_check_var\n");
 144
 145        var->bits_per_pixel = 16;
 146        if (var->green.length == 5) {
 147                var->red.offset = 10;
 148                var->red.length = 5;
 149                var->green.offset = 5;
 150                var->green.length = 5;
 151                var->blue.offset = 0;
 152                var->blue.length = 5;
 153                var->transp.offset = 15;
 154                var->transp.length = 1;
 155        } else {
 156                var->red.offset = 11;
 157                var->red.length = 5;
 158                var->green.offset = 5;
 159                var->green.length = 6;
 160                var->blue.offset = 0;
 161                var->blue.length = 5;
 162                var->transp.offset = 0;
 163                var->transp.length = 0;
 164        }
 165        var->xoffset = var->yoffset = 0;
 166        var->left_margin = var->upper_margin = 0;
 167        var->nonstd = 0;
 168
 169        var->vmode &= ~FB_VMODE_MASK;
 170        var->vmode = FB_VMODE_NONINTERLACED;
 171
 172        /* Dummy values */
 173        var->hsync_len = 24;
 174        var->vsync_len = 2;
 175        var->pixclock = 84316;
 176        var->right_margin = 776;
 177        var->lower_margin = 591;
 178        return 0;
 179}
 180
 181static int vivid_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
 182{
 183        struct vivid_dev *dev = (struct vivid_dev *) info->par;
 184
 185        dprintk(dev, 1, "vivid_fb_check_var\n");
 186        return _vivid_fb_check_var(var, dev);
 187}
 188
 189static int vivid_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
 190{
 191        return 0;
 192}
 193
 194static int vivid_fb_set_par(struct fb_info *info)
 195{
 196        int rc = 0;
 197        struct vivid_dev *dev = (struct vivid_dev *) info->par;
 198
 199        dprintk(dev, 1, "vivid_fb_set_par\n");
 200
 201        rc = vivid_fb_set_var(dev, &info->var);
 202        vivid_fb_get_fix(dev, &info->fix);
 203        return rc;
 204}
 205
 206static int vivid_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
 207                                unsigned blue, unsigned transp,
 208                                struct fb_info *info)
 209{
 210        u32 color, *palette;
 211
 212        if (regno >= info->cmap.len)
 213                return -EINVAL;
 214
 215        color = ((transp & 0xFF00) << 16) | ((red & 0xFF00) << 8) |
 216                 (green & 0xFF00) | ((blue & 0xFF00) >> 8);
 217        if (regno >= 16)
 218                return -EINVAL;
 219
 220        palette = info->pseudo_palette;
 221        if (info->var.bits_per_pixel == 16) {
 222                switch (info->var.green.length) {
 223                case 6:
 224                        color = (red & 0xf800) |
 225                                ((green & 0xfc00) >> 5) |
 226                                ((blue & 0xf800) >> 11);
 227                        break;
 228                case 5:
 229                        color = ((red & 0xf800) >> 1) |
 230                                ((green & 0xf800) >> 6) |
 231                                ((blue & 0xf800) >> 11) |
 232                                (transp ? 0x8000 : 0);
 233                        break;
 234                }
 235        }
 236        palette[regno] = color;
 237        return 0;
 238}
 239
 240/* We don't really support blanking. All this does is enable or
 241   disable the OSD. */
 242static int vivid_fb_blank(int blank_mode, struct fb_info *info)
 243{
 244        struct vivid_dev *dev = (struct vivid_dev *)info->par;
 245
 246        dprintk(dev, 1, "Set blanking mode : %d\n", blank_mode);
 247        switch (blank_mode) {
 248        case FB_BLANK_UNBLANK:
 249                break;
 250        case FB_BLANK_NORMAL:
 251        case FB_BLANK_HSYNC_SUSPEND:
 252        case FB_BLANK_VSYNC_SUSPEND:
 253        case FB_BLANK_POWERDOWN:
 254                break;
 255        }
 256        return 0;
 257}
 258
 259static struct fb_ops vivid_fb_ops = {
 260        .owner = THIS_MODULE,
 261        .fb_check_var   = vivid_fb_check_var,
 262        .fb_set_par     = vivid_fb_set_par,
 263        .fb_setcolreg   = vivid_fb_setcolreg,
 264        .fb_fillrect    = cfb_fillrect,
 265        .fb_copyarea    = cfb_copyarea,
 266        .fb_imageblit   = cfb_imageblit,
 267        .fb_cursor      = NULL,
 268        .fb_ioctl       = vivid_fb_ioctl,
 269        .fb_pan_display = vivid_fb_pan_display,
 270        .fb_blank       = vivid_fb_blank,
 271};
 272
 273/* Initialization */
 274
 275
 276/* Setup our initial video mode */
 277static int vivid_fb_init_vidmode(struct vivid_dev *dev)
 278{
 279        struct v4l2_rect start_window;
 280
 281        /* Color mode */
 282
 283        dev->bits_per_pixel = 16;
 284        dev->bytes_per_pixel = dev->bits_per_pixel / 8;
 285
 286        start_window.width = MAX_OSD_WIDTH;
 287        start_window.left = 0;
 288
 289        dev->display_byte_stride = start_window.width * dev->bytes_per_pixel;
 290
 291        /* Vertical size & position */
 292
 293        start_window.height = MAX_OSD_HEIGHT;
 294        start_window.top = 0;
 295
 296        dev->display_width = start_window.width;
 297        dev->display_height = start_window.height;
 298
 299        /* Generate a valid fb_var_screeninfo */
 300
 301        dev->fb_defined.xres = dev->display_width;
 302        dev->fb_defined.yres = dev->display_height;
 303        dev->fb_defined.xres_virtual = dev->display_width;
 304        dev->fb_defined.yres_virtual = dev->display_height;
 305        dev->fb_defined.bits_per_pixel = dev->bits_per_pixel;
 306        dev->fb_defined.vmode = FB_VMODE_NONINTERLACED;
 307        dev->fb_defined.left_margin = start_window.left + 1;
 308        dev->fb_defined.upper_margin = start_window.top + 1;
 309        dev->fb_defined.accel_flags = FB_ACCEL_NONE;
 310        dev->fb_defined.nonstd = 0;
 311        /* set default to 1:5:5:5 */
 312        dev->fb_defined.green.length = 5;
 313
 314        /* We've filled in the most data, let the usual mode check
 315           routine fill in the rest. */
 316        _vivid_fb_check_var(&dev->fb_defined, dev);
 317
 318        /* Generate valid fb_fix_screeninfo */
 319
 320        vivid_fb_get_fix(dev, &dev->fb_fix);
 321
 322        /* Generate valid fb_info */
 323
 324        dev->fb_info.node = -1;
 325        dev->fb_info.flags = FBINFO_FLAG_DEFAULT;
 326        dev->fb_info.fbops = &vivid_fb_ops;
 327        dev->fb_info.par = dev;
 328        dev->fb_info.var = dev->fb_defined;
 329        dev->fb_info.fix = dev->fb_fix;
 330        dev->fb_info.screen_base = (u8 __iomem *)dev->video_vbase;
 331        dev->fb_info.fbops = &vivid_fb_ops;
 332
 333        /* Supply some monitor specs. Bogus values will do for now */
 334        dev->fb_info.monspecs.hfmin = 8000;
 335        dev->fb_info.monspecs.hfmax = 70000;
 336        dev->fb_info.monspecs.vfmin = 10;
 337        dev->fb_info.monspecs.vfmax = 100;
 338
 339        /* Allocate color map */
 340        if (fb_alloc_cmap(&dev->fb_info.cmap, 256, 1)) {
 341                pr_err("abort, unable to alloc cmap\n");
 342                return -ENOMEM;
 343        }
 344
 345        /* Allocate the pseudo palette */
 346        dev->fb_info.pseudo_palette = kmalloc_array(16, sizeof(u32), GFP_KERNEL);
 347
 348        return dev->fb_info.pseudo_palette ? 0 : -ENOMEM;
 349}
 350
 351/* Release any memory we've grabbed */
 352void vivid_fb_release_buffers(struct vivid_dev *dev)
 353{
 354        if (dev->video_vbase == NULL)
 355                return;
 356
 357        /* Release cmap */
 358        if (dev->fb_info.cmap.len)
 359                fb_dealloc_cmap(&dev->fb_info.cmap);
 360
 361        /* Release pseudo palette */
 362        kfree(dev->fb_info.pseudo_palette);
 363        kfree(dev->video_vbase);
 364}
 365
 366/* Initialize the specified card */
 367
 368int vivid_fb_init(struct vivid_dev *dev)
 369{
 370        int ret;
 371
 372        dev->video_buffer_size = MAX_OSD_HEIGHT * MAX_OSD_WIDTH * 2;
 373        dev->video_vbase = kzalloc(dev->video_buffer_size, GFP_KERNEL | GFP_DMA32);
 374        if (dev->video_vbase == NULL)
 375                return -ENOMEM;
 376        dev->video_pbase = virt_to_phys(dev->video_vbase);
 377
 378        pr_info("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
 379                        dev->video_pbase, dev->video_vbase,
 380                        dev->video_buffer_size / 1024);
 381
 382        /* Set the startup video mode information */
 383        ret = vivid_fb_init_vidmode(dev);
 384        if (ret) {
 385                vivid_fb_release_buffers(dev);
 386                return ret;
 387        }
 388
 389        vivid_clear_fb(dev);
 390
 391        /* Register the framebuffer */
 392        if (register_framebuffer(&dev->fb_info) < 0) {
 393                vivid_fb_release_buffers(dev);
 394                return -EINVAL;
 395        }
 396
 397        /* Set the card to the requested mode */
 398        vivid_fb_set_par(&dev->fb_info);
 399        return 0;
 400
 401}
 402