linux/drivers/staging/media/omap4iss/iss_ipipe.c
<<
>>
Prefs
   1/*
   2 * TI OMAP4 ISS V4L2 Driver - ISP IPIPE module
   3 *
   4 * Copyright (C) 2012 Texas Instruments, Inc.
   5 *
   6 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/uaccess.h>
  16#include <linux/delay.h>
  17#include <linux/device.h>
  18#include <linux/dma-mapping.h>
  19#include <linux/mm.h>
  20#include <linux/sched.h>
  21
  22#include "iss.h"
  23#include "iss_regs.h"
  24#include "iss_ipipe.h"
  25
  26static struct v4l2_mbus_framefmt *
  27__ipipe_get_format(struct iss_ipipe_device *ipipe,
  28                   struct v4l2_subdev_pad_config *cfg,
  29                   unsigned int pad,
  30                   enum v4l2_subdev_format_whence which);
  31
  32static const unsigned int ipipe_fmts[] = {
  33        MEDIA_BUS_FMT_SGRBG10_1X10,
  34        MEDIA_BUS_FMT_SRGGB10_1X10,
  35        MEDIA_BUS_FMT_SBGGR10_1X10,
  36        MEDIA_BUS_FMT_SGBRG10_1X10,
  37};
  38
  39/*
  40 * ipipe_print_status - Print current IPIPE Module register values.
  41 * @ipipe: Pointer to ISS ISP IPIPE device.
  42 *
  43 * Also prints other debug information stored in the IPIPE module.
  44 */
  45#define IPIPE_PRINT_REGISTER(iss, name)\
  46        dev_dbg(iss->dev, "###IPIPE " #name "=0x%08x\n", \
  47                iss_reg_read(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_##name))
  48
  49static void ipipe_print_status(struct iss_ipipe_device *ipipe)
  50{
  51        struct iss_device *iss = to_iss_device(ipipe);
  52
  53        dev_dbg(iss->dev, "-------------IPIPE Register dump-------------\n");
  54
  55        IPIPE_PRINT_REGISTER(iss, SRC_EN);
  56        IPIPE_PRINT_REGISTER(iss, SRC_MODE);
  57        IPIPE_PRINT_REGISTER(iss, SRC_FMT);
  58        IPIPE_PRINT_REGISTER(iss, SRC_COL);
  59        IPIPE_PRINT_REGISTER(iss, SRC_VPS);
  60        IPIPE_PRINT_REGISTER(iss, SRC_VSZ);
  61        IPIPE_PRINT_REGISTER(iss, SRC_HPS);
  62        IPIPE_PRINT_REGISTER(iss, SRC_HSZ);
  63        IPIPE_PRINT_REGISTER(iss, GCK_MMR);
  64        IPIPE_PRINT_REGISTER(iss, YUV_PHS);
  65
  66        dev_dbg(iss->dev, "-----------------------------------------------\n");
  67}
  68
  69/*
  70 * ipipe_enable - Enable/Disable IPIPE.
  71 * @enable: enable flag
  72 *
  73 */
  74static void ipipe_enable(struct iss_ipipe_device *ipipe, u8 enable)
  75{
  76        struct iss_device *iss = to_iss_device(ipipe);
  77
  78        iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_EN,
  79                       IPIPE_SRC_EN_EN, enable ? IPIPE_SRC_EN_EN : 0);
  80}
  81
  82/* -----------------------------------------------------------------------------
  83 * Format- and pipeline-related configuration helpers
  84 */
  85
  86static void ipipe_configure(struct iss_ipipe_device *ipipe)
  87{
  88        struct iss_device *iss = to_iss_device(ipipe);
  89        struct v4l2_mbus_framefmt *format;
  90
  91        /* IPIPE_PAD_SINK */
  92        format = &ipipe->formats[IPIPE_PAD_SINK];
  93
  94        /* NOTE: Currently just supporting pipeline IN: RGB, OUT: YUV422 */
  95        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_FMT,
  96                      IPIPE_SRC_FMT_RAW2YUV);
  97
  98        /* Enable YUV444 -> YUV422 conversion */
  99        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_YUV_PHS,
 100                      IPIPE_YUV_PHS_LPF);
 101
 102        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VPS, 0);
 103        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HPS, 0);
 104        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_VSZ,
 105                      (format->height - 2) & IPIPE_SRC_VSZ_MASK);
 106        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_HSZ,
 107                      (format->width - 1) & IPIPE_SRC_HSZ_MASK);
 108
 109        /* Ignore ipipeif_wrt signal, and operate on-the-fly.  */
 110        iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_MODE,
 111                    IPIPE_SRC_MODE_WRT | IPIPE_SRC_MODE_OST);
 112
 113        /* HACK: Values tuned for Ducati SW (OV) */
 114        iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_SRC_COL,
 115                      IPIPE_SRC_COL_EE_B | IPIPE_SRC_COL_EO_GB |
 116                      IPIPE_SRC_COL_OE_GR | IPIPE_SRC_COL_OO_R);
 117
 118        /* IPIPE_PAD_SOURCE_VP */
 119        format = &ipipe->formats[IPIPE_PAD_SOURCE_VP];
 120        /* Do nothing? */
 121}
 122
 123/* -----------------------------------------------------------------------------
 124 * V4L2 subdev operations
 125 */
 126
 127/*
 128 * ipipe_set_stream - Enable/Disable streaming on the IPIPE module
 129 * @sd: ISP IPIPE V4L2 subdevice
 130 * @enable: Enable/disable stream
 131 */
 132static int ipipe_set_stream(struct v4l2_subdev *sd, int enable)
 133{
 134        struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
 135        struct iss_device *iss = to_iss_device(ipipe);
 136        int ret = 0;
 137
 138        if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED) {
 139                if (enable == ISS_PIPELINE_STREAM_STOPPED)
 140                        return 0;
 141
 142                omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
 143
 144                /* Enable clk_arm_g0 */
 145                iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_MMR,
 146                              IPIPE_GCK_MMR_REG);
 147
 148                /* Enable clk_pix_g[3:0] */
 149                iss_reg_write(iss, OMAP4_ISS_MEM_ISP_IPIPE, IPIPE_GCK_PIX,
 150                              IPIPE_GCK_PIX_G3 | IPIPE_GCK_PIX_G2 |
 151                              IPIPE_GCK_PIX_G1 | IPIPE_GCK_PIX_G0);
 152        }
 153
 154        switch (enable) {
 155        case ISS_PIPELINE_STREAM_CONTINUOUS:
 156
 157                ipipe_configure(ipipe);
 158                ipipe_print_status(ipipe);
 159
 160                atomic_set(&ipipe->stopping, 0);
 161                ipipe_enable(ipipe, 1);
 162                break;
 163
 164        case ISS_PIPELINE_STREAM_STOPPED:
 165                if (ipipe->state == ISS_PIPELINE_STREAM_STOPPED)
 166                        return 0;
 167                if (omap4iss_module_sync_idle(&sd->entity, &ipipe->wait,
 168                                              &ipipe->stopping))
 169                        ret = -ETIMEDOUT;
 170
 171                ipipe_enable(ipipe, 0);
 172                omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_IPIPE);
 173                break;
 174        }
 175
 176        ipipe->state = enable;
 177        return ret;
 178}
 179
 180static struct v4l2_mbus_framefmt *
 181__ipipe_get_format(struct iss_ipipe_device *ipipe,
 182                   struct v4l2_subdev_pad_config *cfg,
 183                   unsigned int pad,
 184                   enum v4l2_subdev_format_whence which)
 185{
 186        if (which == V4L2_SUBDEV_FORMAT_TRY)
 187                return v4l2_subdev_get_try_format(&ipipe->subdev, cfg, pad);
 188
 189        return &ipipe->formats[pad];
 190}
 191
 192/*
 193 * ipipe_try_format - Try video format on a pad
 194 * @ipipe: ISS IPIPE device
 195 * @cfg: V4L2 subdev pad config
 196 * @pad: Pad number
 197 * @fmt: Format
 198 */
 199static void
 200ipipe_try_format(struct iss_ipipe_device *ipipe,
 201                 struct v4l2_subdev_pad_config *cfg,
 202                 unsigned int pad,
 203                 struct v4l2_mbus_framefmt *fmt,
 204                 enum v4l2_subdev_format_whence which)
 205{
 206        struct v4l2_mbus_framefmt *format;
 207        unsigned int width = fmt->width;
 208        unsigned int height = fmt->height;
 209        unsigned int i;
 210
 211        switch (pad) {
 212        case IPIPE_PAD_SINK:
 213                for (i = 0; i < ARRAY_SIZE(ipipe_fmts); i++) {
 214                        if (fmt->code == ipipe_fmts[i])
 215                                break;
 216                }
 217
 218                /* If not found, use SGRBG10 as default */
 219                if (i >= ARRAY_SIZE(ipipe_fmts))
 220                        fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
 221
 222                /* Clamp the input size. */
 223                fmt->width = clamp_t(u32, width, 1, 8192);
 224                fmt->height = clamp_t(u32, height, 1, 8192);
 225                fmt->colorspace = V4L2_COLORSPACE_SRGB;
 226                break;
 227
 228        case IPIPE_PAD_SOURCE_VP:
 229                format = __ipipe_get_format(ipipe, cfg, IPIPE_PAD_SINK, which);
 230                memcpy(fmt, format, sizeof(*fmt));
 231
 232                fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
 233                fmt->width = clamp_t(u32, width, 32, fmt->width);
 234                fmt->height = clamp_t(u32, height, 32, fmt->height);
 235                fmt->colorspace = V4L2_COLORSPACE_JPEG;
 236                break;
 237        }
 238
 239        fmt->field = V4L2_FIELD_NONE;
 240}
 241
 242/*
 243 * ipipe_enum_mbus_code - Handle pixel format enumeration
 244 * @sd     : pointer to v4l2 subdev structure
 245 * @cfg    : V4L2 subdev pad config
 246 * @code   : pointer to v4l2_subdev_mbus_code_enum structure
 247 * return -EINVAL or zero on success
 248 */
 249static int ipipe_enum_mbus_code(struct v4l2_subdev *sd,
 250                                struct v4l2_subdev_pad_config *cfg,
 251                                struct v4l2_subdev_mbus_code_enum *code)
 252{
 253        switch (code->pad) {
 254        case IPIPE_PAD_SINK:
 255                if (code->index >= ARRAY_SIZE(ipipe_fmts))
 256                        return -EINVAL;
 257
 258                code->code = ipipe_fmts[code->index];
 259                break;
 260
 261        case IPIPE_PAD_SOURCE_VP:
 262                /* FIXME: Forced format conversion inside IPIPE ? */
 263                if (code->index != 0)
 264                        return -EINVAL;
 265
 266                code->code = MEDIA_BUS_FMT_UYVY8_1X16;
 267                break;
 268
 269        default:
 270                return -EINVAL;
 271        }
 272
 273        return 0;
 274}
 275
 276static int ipipe_enum_frame_size(struct v4l2_subdev *sd,
 277                                 struct v4l2_subdev_pad_config *cfg,
 278                                 struct v4l2_subdev_frame_size_enum *fse)
 279{
 280        struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
 281        struct v4l2_mbus_framefmt format;
 282
 283        if (fse->index != 0)
 284                return -EINVAL;
 285
 286        format.code = fse->code;
 287        format.width = 1;
 288        format.height = 1;
 289        ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
 290        fse->min_width = format.width;
 291        fse->min_height = format.height;
 292
 293        if (format.code != fse->code)
 294                return -EINVAL;
 295
 296        format.code = fse->code;
 297        format.width = -1;
 298        format.height = -1;
 299        ipipe_try_format(ipipe, cfg, fse->pad, &format, fse->which);
 300        fse->max_width = format.width;
 301        fse->max_height = format.height;
 302
 303        return 0;
 304}
 305
 306/*
 307 * ipipe_get_format - Retrieve the video format on a pad
 308 * @sd : ISP IPIPE V4L2 subdevice
 309 * @cfg: V4L2 subdev pad config
 310 * @fmt: Format
 311 *
 312 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
 313 * to the format type.
 314 */
 315static int ipipe_get_format(struct v4l2_subdev *sd,
 316                            struct v4l2_subdev_pad_config *cfg,
 317                            struct v4l2_subdev_format *fmt)
 318{
 319        struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
 320        struct v4l2_mbus_framefmt *format;
 321
 322        format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
 323        if (!format)
 324                return -EINVAL;
 325
 326        fmt->format = *format;
 327        return 0;
 328}
 329
 330/*
 331 * ipipe_set_format - Set the video format on a pad
 332 * @sd : ISP IPIPE V4L2 subdevice
 333 * @cfg: V4L2 subdev pad config
 334 * @fmt: Format
 335 *
 336 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
 337 * to the format type.
 338 */
 339static int ipipe_set_format(struct v4l2_subdev *sd,
 340                            struct v4l2_subdev_pad_config *cfg,
 341                            struct v4l2_subdev_format *fmt)
 342{
 343        struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
 344        struct v4l2_mbus_framefmt *format;
 345
 346        format = __ipipe_get_format(ipipe, cfg, fmt->pad, fmt->which);
 347        if (!format)
 348                return -EINVAL;
 349
 350        ipipe_try_format(ipipe, cfg, fmt->pad, &fmt->format, fmt->which);
 351        *format = fmt->format;
 352
 353        /* Propagate the format from sink to source */
 354        if (fmt->pad == IPIPE_PAD_SINK) {
 355                format = __ipipe_get_format(ipipe, cfg, IPIPE_PAD_SOURCE_VP,
 356                                            fmt->which);
 357                *format = fmt->format;
 358                ipipe_try_format(ipipe, cfg, IPIPE_PAD_SOURCE_VP, format,
 359                                 fmt->which);
 360        }
 361
 362        return 0;
 363}
 364
 365static int ipipe_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 366                               struct v4l2_subdev_format *source_fmt,
 367                               struct v4l2_subdev_format *sink_fmt)
 368{
 369        /* Check if the two ends match */
 370        if (source_fmt->format.width != sink_fmt->format.width ||
 371            source_fmt->format.height != sink_fmt->format.height)
 372                return -EPIPE;
 373
 374        if (source_fmt->format.code != sink_fmt->format.code)
 375                return -EPIPE;
 376
 377        return 0;
 378}
 379
 380/*
 381 * ipipe_init_formats - Initialize formats on all pads
 382 * @sd: ISP IPIPE V4L2 subdevice
 383 * @fh: V4L2 subdev file handle
 384 *
 385 * Initialize all pad formats with default values. If fh is not NULL, try
 386 * formats are initialized on the file handle. Otherwise active formats are
 387 * initialized on the device.
 388 */
 389static int ipipe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 390{
 391        struct v4l2_subdev_format format;
 392
 393        memset(&format, 0, sizeof(format));
 394        format.pad = IPIPE_PAD_SINK;
 395        format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
 396        format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
 397        format.format.width = 4096;
 398        format.format.height = 4096;
 399        ipipe_set_format(sd, fh ? fh->pad : NULL, &format);
 400
 401        return 0;
 402}
 403
 404/* V4L2 subdev video operations */
 405static const struct v4l2_subdev_video_ops ipipe_v4l2_video_ops = {
 406        .s_stream = ipipe_set_stream,
 407};
 408
 409/* V4L2 subdev pad operations */
 410static const struct v4l2_subdev_pad_ops ipipe_v4l2_pad_ops = {
 411        .enum_mbus_code = ipipe_enum_mbus_code,
 412        .enum_frame_size = ipipe_enum_frame_size,
 413        .get_fmt = ipipe_get_format,
 414        .set_fmt = ipipe_set_format,
 415        .link_validate = ipipe_link_validate,
 416};
 417
 418/* V4L2 subdev operations */
 419static const struct v4l2_subdev_ops ipipe_v4l2_ops = {
 420        .video = &ipipe_v4l2_video_ops,
 421        .pad = &ipipe_v4l2_pad_ops,
 422};
 423
 424/* V4L2 subdev internal operations */
 425static const struct v4l2_subdev_internal_ops ipipe_v4l2_internal_ops = {
 426        .open = ipipe_init_formats,
 427};
 428
 429/* -----------------------------------------------------------------------------
 430 * Media entity operations
 431 */
 432
 433/*
 434 * ipipe_link_setup - Setup IPIPE connections
 435 * @entity: IPIPE media entity
 436 * @local: Pad at the local end of the link
 437 * @remote: Pad at the remote end of the link
 438 * @flags: Link flags
 439 *
 440 * return -EINVAL or zero on success
 441 */
 442static int ipipe_link_setup(struct media_entity *entity,
 443                            const struct media_pad *local,
 444                            const struct media_pad *remote, u32 flags)
 445{
 446        struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
 447        struct iss_ipipe_device *ipipe = v4l2_get_subdevdata(sd);
 448        struct iss_device *iss = to_iss_device(ipipe);
 449
 450        if (!is_media_entity_v4l2_subdev(remote->entity))
 451                return -EINVAL;
 452
 453        switch (local->index) {
 454        case IPIPE_PAD_SINK:
 455                /* Read from IPIPEIF. */
 456                if (!(flags & MEDIA_LNK_FL_ENABLED)) {
 457                        ipipe->input = IPIPE_INPUT_NONE;
 458                        break;
 459                }
 460
 461                if (ipipe->input != IPIPE_INPUT_NONE)
 462                        return -EBUSY;
 463
 464                if (remote->entity == &iss->ipipeif.subdev.entity)
 465                        ipipe->input = IPIPE_INPUT_IPIPEIF;
 466
 467                break;
 468
 469        case IPIPE_PAD_SOURCE_VP:
 470                /* Send to RESIZER */
 471                if (flags & MEDIA_LNK_FL_ENABLED) {
 472                        if (ipipe->output & ~IPIPE_OUTPUT_VP)
 473                                return -EBUSY;
 474                        ipipe->output |= IPIPE_OUTPUT_VP;
 475                } else {
 476                        ipipe->output &= ~IPIPE_OUTPUT_VP;
 477                }
 478                break;
 479
 480        default:
 481                return -EINVAL;
 482        }
 483
 484        return 0;
 485}
 486
 487/* media operations */
 488static const struct media_entity_operations ipipe_media_ops = {
 489        .link_setup = ipipe_link_setup,
 490        .link_validate = v4l2_subdev_link_validate,
 491};
 492
 493/*
 494 * ipipe_init_entities - Initialize V4L2 subdev and media entity
 495 * @ipipe: ISS ISP IPIPE module
 496 *
 497 * Return 0 on success and a negative error code on failure.
 498 */
 499static int ipipe_init_entities(struct iss_ipipe_device *ipipe)
 500{
 501        struct v4l2_subdev *sd = &ipipe->subdev;
 502        struct media_pad *pads = ipipe->pads;
 503        struct media_entity *me = &sd->entity;
 504        int ret;
 505
 506        ipipe->input = IPIPE_INPUT_NONE;
 507
 508        v4l2_subdev_init(sd, &ipipe_v4l2_ops);
 509        sd->internal_ops = &ipipe_v4l2_internal_ops;
 510        strlcpy(sd->name, "OMAP4 ISS ISP IPIPE", sizeof(sd->name));
 511        sd->grp_id = BIT(16);   /* group ID for iss subdevs */
 512        v4l2_set_subdevdata(sd, ipipe);
 513        sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 514
 515        pads[IPIPE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
 516        pads[IPIPE_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
 517
 518        me->ops = &ipipe_media_ops;
 519        ret = media_entity_pads_init(me, IPIPE_PADS_NUM, pads);
 520        if (ret < 0)
 521                return ret;
 522
 523        ipipe_init_formats(sd, NULL);
 524
 525        return 0;
 526}
 527
 528void omap4iss_ipipe_unregister_entities(struct iss_ipipe_device *ipipe)
 529{
 530        v4l2_device_unregister_subdev(&ipipe->subdev);
 531}
 532
 533int omap4iss_ipipe_register_entities(struct iss_ipipe_device *ipipe,
 534                                     struct v4l2_device *vdev)
 535{
 536        int ret;
 537
 538        /* Register the subdev and video node. */
 539        ret = v4l2_device_register_subdev(vdev, &ipipe->subdev);
 540        if (ret < 0)
 541                goto error;
 542
 543        return 0;
 544
 545error:
 546        omap4iss_ipipe_unregister_entities(ipipe);
 547        return ret;
 548}
 549
 550/* -----------------------------------------------------------------------------
 551 * ISP IPIPE initialisation and cleanup
 552 */
 553
 554/*
 555 * omap4iss_ipipe_init - IPIPE module initialization.
 556 * @iss: Device pointer specific to the OMAP4 ISS.
 557 *
 558 * TODO: Get the initialisation values from platform data.
 559 *
 560 * Return 0 on success or a negative error code otherwise.
 561 */
 562int omap4iss_ipipe_init(struct iss_device *iss)
 563{
 564        struct iss_ipipe_device *ipipe = &iss->ipipe;
 565
 566        ipipe->state = ISS_PIPELINE_STREAM_STOPPED;
 567        init_waitqueue_head(&ipipe->wait);
 568
 569        return ipipe_init_entities(ipipe);
 570}
 571
 572/*
 573 * omap4iss_ipipe_cleanup - IPIPE module cleanup.
 574 * @iss: Device pointer specific to the OMAP4 ISS.
 575 */
 576void omap4iss_ipipe_cleanup(struct iss_device *iss)
 577{
 578        struct iss_ipipe_device *ipipe = &iss->ipipe;
 579
 580        media_entity_cleanup(&ipipe->subdev.entity);
 581}
 582