linux/drivers/media/platform/omap3isp/ispccdc.c
<<
>>
Prefs
   1/*
   2 * ispccdc.c
   3 *
   4 * TI OMAP3 ISP - CCDC module
   5 *
   6 * Copyright (C) 2009-2010 Nokia Corporation
   7 * Copyright (C) 2009 Texas Instruments, Inc.
   8 *
   9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10 *           Sakari Ailus <sakari.ailus@iki.fi>
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License version 2 as
  14 * published by the Free Software Foundation.
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/uaccess.h>
  19#include <linux/delay.h>
  20#include <linux/device.h>
  21#include <linux/dma-mapping.h>
  22#include <linux/mm.h>
  23#include <linux/sched.h>
  24#include <linux/slab.h>
  25#include <media/v4l2-event.h>
  26
  27#include "isp.h"
  28#include "ispreg.h"
  29#include "ispccdc.h"
  30
  31#define CCDC_MIN_WIDTH          32
  32#define CCDC_MIN_HEIGHT         32
  33
  34static struct v4l2_mbus_framefmt *
  35__ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_pad_config *cfg,
  36                  unsigned int pad, enum v4l2_subdev_format_whence which);
  37
  38static const unsigned int ccdc_fmts[] = {
  39        MEDIA_BUS_FMT_Y8_1X8,
  40        MEDIA_BUS_FMT_Y10_1X10,
  41        MEDIA_BUS_FMT_Y12_1X12,
  42        MEDIA_BUS_FMT_SGRBG8_1X8,
  43        MEDIA_BUS_FMT_SRGGB8_1X8,
  44        MEDIA_BUS_FMT_SBGGR8_1X8,
  45        MEDIA_BUS_FMT_SGBRG8_1X8,
  46        MEDIA_BUS_FMT_SGRBG10_1X10,
  47        MEDIA_BUS_FMT_SRGGB10_1X10,
  48        MEDIA_BUS_FMT_SBGGR10_1X10,
  49        MEDIA_BUS_FMT_SGBRG10_1X10,
  50        MEDIA_BUS_FMT_SGRBG12_1X12,
  51        MEDIA_BUS_FMT_SRGGB12_1X12,
  52        MEDIA_BUS_FMT_SBGGR12_1X12,
  53        MEDIA_BUS_FMT_SGBRG12_1X12,
  54        MEDIA_BUS_FMT_YUYV8_2X8,
  55        MEDIA_BUS_FMT_UYVY8_2X8,
  56};
  57
  58/*
  59 * ccdc_print_status - Print current CCDC Module register values.
  60 * @ccdc: Pointer to ISP CCDC device.
  61 *
  62 * Also prints other debug information stored in the CCDC module.
  63 */
  64#define CCDC_PRINT_REGISTER(isp, name)\
  65        dev_dbg(isp->dev, "###CCDC " #name "=0x%08x\n", \
  66                isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_##name))
  67
  68static void ccdc_print_status(struct isp_ccdc_device *ccdc)
  69{
  70        struct isp_device *isp = to_isp_device(ccdc);
  71
  72        dev_dbg(isp->dev, "-------------CCDC Register dump-------------\n");
  73
  74        CCDC_PRINT_REGISTER(isp, PCR);
  75        CCDC_PRINT_REGISTER(isp, SYN_MODE);
  76        CCDC_PRINT_REGISTER(isp, HD_VD_WID);
  77        CCDC_PRINT_REGISTER(isp, PIX_LINES);
  78        CCDC_PRINT_REGISTER(isp, HORZ_INFO);
  79        CCDC_PRINT_REGISTER(isp, VERT_START);
  80        CCDC_PRINT_REGISTER(isp, VERT_LINES);
  81        CCDC_PRINT_REGISTER(isp, CULLING);
  82        CCDC_PRINT_REGISTER(isp, HSIZE_OFF);
  83        CCDC_PRINT_REGISTER(isp, SDOFST);
  84        CCDC_PRINT_REGISTER(isp, SDR_ADDR);
  85        CCDC_PRINT_REGISTER(isp, CLAMP);
  86        CCDC_PRINT_REGISTER(isp, DCSUB);
  87        CCDC_PRINT_REGISTER(isp, COLPTN);
  88        CCDC_PRINT_REGISTER(isp, BLKCMP);
  89        CCDC_PRINT_REGISTER(isp, FPC);
  90        CCDC_PRINT_REGISTER(isp, FPC_ADDR);
  91        CCDC_PRINT_REGISTER(isp, VDINT);
  92        CCDC_PRINT_REGISTER(isp, ALAW);
  93        CCDC_PRINT_REGISTER(isp, REC656IF);
  94        CCDC_PRINT_REGISTER(isp, CFG);
  95        CCDC_PRINT_REGISTER(isp, FMTCFG);
  96        CCDC_PRINT_REGISTER(isp, FMT_HORZ);
  97        CCDC_PRINT_REGISTER(isp, FMT_VERT);
  98        CCDC_PRINT_REGISTER(isp, PRGEVEN0);
  99        CCDC_PRINT_REGISTER(isp, PRGEVEN1);
 100        CCDC_PRINT_REGISTER(isp, PRGODD0);
 101        CCDC_PRINT_REGISTER(isp, PRGODD1);
 102        CCDC_PRINT_REGISTER(isp, VP_OUT);
 103        CCDC_PRINT_REGISTER(isp, LSC_CONFIG);
 104        CCDC_PRINT_REGISTER(isp, LSC_INITIAL);
 105        CCDC_PRINT_REGISTER(isp, LSC_TABLE_BASE);
 106        CCDC_PRINT_REGISTER(isp, LSC_TABLE_OFFSET);
 107
 108        dev_dbg(isp->dev, "--------------------------------------------\n");
 109}
 110
 111/*
 112 * omap3isp_ccdc_busy - Get busy state of the CCDC.
 113 * @ccdc: Pointer to ISP CCDC device.
 114 */
 115int omap3isp_ccdc_busy(struct isp_ccdc_device *ccdc)
 116{
 117        struct isp_device *isp = to_isp_device(ccdc);
 118
 119        return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR) &
 120                ISPCCDC_PCR_BUSY;
 121}
 122
 123/* -----------------------------------------------------------------------------
 124 * Lens Shading Compensation
 125 */
 126
 127/*
 128 * ccdc_lsc_validate_config - Check that LSC configuration is valid.
 129 * @ccdc: Pointer to ISP CCDC device.
 130 * @lsc_cfg: the LSC configuration to check.
 131 *
 132 * Returns 0 if the LSC configuration is valid, or -EINVAL if invalid.
 133 */
 134static int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
 135                                    struct omap3isp_ccdc_lsc_config *lsc_cfg)
 136{
 137        struct isp_device *isp = to_isp_device(ccdc);
 138        struct v4l2_mbus_framefmt *format;
 139        unsigned int paxel_width, paxel_height;
 140        unsigned int paxel_shift_x, paxel_shift_y;
 141        unsigned int min_width, min_height, min_size;
 142        unsigned int input_width, input_height;
 143
 144        paxel_shift_x = lsc_cfg->gain_mode_m;
 145        paxel_shift_y = lsc_cfg->gain_mode_n;
 146
 147        if ((paxel_shift_x < 2) || (paxel_shift_x > 6) ||
 148            (paxel_shift_y < 2) || (paxel_shift_y > 6)) {
 149                dev_dbg(isp->dev, "CCDC: LSC: Invalid paxel size\n");
 150                return -EINVAL;
 151        }
 152
 153        if (lsc_cfg->offset & 3) {
 154                dev_dbg(isp->dev,
 155                        "CCDC: LSC: Offset must be a multiple of 4\n");
 156                return -EINVAL;
 157        }
 158
 159        if ((lsc_cfg->initial_x & 1) || (lsc_cfg->initial_y & 1)) {
 160                dev_dbg(isp->dev, "CCDC: LSC: initial_x and y must be even\n");
 161                return -EINVAL;
 162        }
 163
 164        format = __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
 165                                   V4L2_SUBDEV_FORMAT_ACTIVE);
 166        input_width = format->width;
 167        input_height = format->height;
 168
 169        /* Calculate minimum bytesize for validation */
 170        paxel_width = 1 << paxel_shift_x;
 171        min_width = ((input_width + lsc_cfg->initial_x + paxel_width - 1)
 172                     >> paxel_shift_x) + 1;
 173
 174        paxel_height = 1 << paxel_shift_y;
 175        min_height = ((input_height + lsc_cfg->initial_y + paxel_height - 1)
 176                     >> paxel_shift_y) + 1;
 177
 178        min_size = 4 * min_width * min_height;
 179        if (min_size > lsc_cfg->size) {
 180                dev_dbg(isp->dev, "CCDC: LSC: too small table\n");
 181                return -EINVAL;
 182        }
 183        if (lsc_cfg->offset < (min_width * 4)) {
 184                dev_dbg(isp->dev, "CCDC: LSC: Offset is too small\n");
 185                return -EINVAL;
 186        }
 187        if ((lsc_cfg->size / lsc_cfg->offset) < min_height) {
 188                dev_dbg(isp->dev, "CCDC: LSC: Wrong size/offset combination\n");
 189                return -EINVAL;
 190        }
 191        return 0;
 192}
 193
 194/*
 195 * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
 196 * @ccdc: Pointer to ISP CCDC device.
 197 */
 198static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc,
 199                                   dma_addr_t addr)
 200{
 201        isp_reg_writel(to_isp_device(ccdc), addr,
 202                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
 203}
 204
 205/*
 206 * ccdc_lsc_setup_regs - Configures the lens shading compensation module
 207 * @ccdc: Pointer to ISP CCDC device.
 208 */
 209static void ccdc_lsc_setup_regs(struct isp_ccdc_device *ccdc,
 210                                struct omap3isp_ccdc_lsc_config *cfg)
 211{
 212        struct isp_device *isp = to_isp_device(ccdc);
 213        int reg;
 214
 215        isp_reg_writel(isp, cfg->offset, OMAP3_ISP_IOMEM_CCDC,
 216                       ISPCCDC_LSC_TABLE_OFFSET);
 217
 218        reg = 0;
 219        reg |= cfg->gain_mode_n << ISPCCDC_LSC_GAIN_MODE_N_SHIFT;
 220        reg |= cfg->gain_mode_m << ISPCCDC_LSC_GAIN_MODE_M_SHIFT;
 221        reg |= cfg->gain_format << ISPCCDC_LSC_GAIN_FORMAT_SHIFT;
 222        isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG);
 223
 224        reg = 0;
 225        reg &= ~ISPCCDC_LSC_INITIAL_X_MASK;
 226        reg |= cfg->initial_x << ISPCCDC_LSC_INITIAL_X_SHIFT;
 227        reg &= ~ISPCCDC_LSC_INITIAL_Y_MASK;
 228        reg |= cfg->initial_y << ISPCCDC_LSC_INITIAL_Y_SHIFT;
 229        isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC,
 230                       ISPCCDC_LSC_INITIAL);
 231}
 232
 233static int ccdc_lsc_wait_prefetch(struct isp_ccdc_device *ccdc)
 234{
 235        struct isp_device *isp = to_isp_device(ccdc);
 236        unsigned int wait;
 237
 238        isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
 239                       OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
 240
 241        /* timeout 1 ms */
 242        for (wait = 0; wait < 1000; wait++) {
 243                if (isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS) &
 244                                  IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ) {
 245                        isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
 246                                       OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
 247                        return 0;
 248                }
 249
 250                rmb();
 251                udelay(1);
 252        }
 253
 254        return -ETIMEDOUT;
 255}
 256
 257/*
 258 * __ccdc_lsc_enable - Enables/Disables the Lens Shading Compensation module.
 259 * @ccdc: Pointer to ISP CCDC device.
 260 * @enable: 0 Disables LSC, 1 Enables LSC.
 261 */
 262static int __ccdc_lsc_enable(struct isp_ccdc_device *ccdc, int enable)
 263{
 264        struct isp_device *isp = to_isp_device(ccdc);
 265        const struct v4l2_mbus_framefmt *format =
 266                __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
 267                                  V4L2_SUBDEV_FORMAT_ACTIVE);
 268
 269        if ((format->code != MEDIA_BUS_FMT_SGRBG10_1X10) &&
 270            (format->code != MEDIA_BUS_FMT_SRGGB10_1X10) &&
 271            (format->code != MEDIA_BUS_FMT_SBGGR10_1X10) &&
 272            (format->code != MEDIA_BUS_FMT_SGBRG10_1X10))
 273                return -EINVAL;
 274
 275        if (enable)
 276                omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_LSC_READ);
 277
 278        isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
 279                        ISPCCDC_LSC_ENABLE, enable ? ISPCCDC_LSC_ENABLE : 0);
 280
 281        if (enable) {
 282                if (ccdc_lsc_wait_prefetch(ccdc) < 0) {
 283                        isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC,
 284                                    ISPCCDC_LSC_CONFIG, ISPCCDC_LSC_ENABLE);
 285                        ccdc->lsc.state = LSC_STATE_STOPPED;
 286                        dev_warn(to_device(ccdc), "LSC prefetch timeout\n");
 287                        return -ETIMEDOUT;
 288                }
 289                ccdc->lsc.state = LSC_STATE_RUNNING;
 290        } else {
 291                ccdc->lsc.state = LSC_STATE_STOPPING;
 292        }
 293
 294        return 0;
 295}
 296
 297static int ccdc_lsc_busy(struct isp_ccdc_device *ccdc)
 298{
 299        struct isp_device *isp = to_isp_device(ccdc);
 300
 301        return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG) &
 302                             ISPCCDC_LSC_BUSY;
 303}
 304
 305/* __ccdc_lsc_configure - Apply a new configuration to the LSC engine
 306 * @ccdc: Pointer to ISP CCDC device
 307 * @req: New configuration request
 308 *
 309 * context: in_interrupt()
 310 */
 311static int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
 312                                struct ispccdc_lsc_config_req *req)
 313{
 314        if (!req->enable)
 315                return -EINVAL;
 316
 317        if (ccdc_lsc_validate_config(ccdc, &req->config) < 0) {
 318                dev_dbg(to_device(ccdc), "Discard LSC configuration\n");
 319                return -EINVAL;
 320        }
 321
 322        if (ccdc_lsc_busy(ccdc))
 323                return -EBUSY;
 324
 325        ccdc_lsc_setup_regs(ccdc, &req->config);
 326        ccdc_lsc_program_table(ccdc, req->table.dma);
 327        return 0;
 328}
 329
 330/*
 331 * ccdc_lsc_error_handler - Handle LSC prefetch error scenario.
 332 * @ccdc: Pointer to ISP CCDC device.
 333 *
 334 * Disables LSC, and defers enablement to shadow registers update time.
 335 */
 336static void ccdc_lsc_error_handler(struct isp_ccdc_device *ccdc)
 337{
 338        struct isp_device *isp = to_isp_device(ccdc);
 339        /*
 340         * From OMAP3 TRM: When this event is pending, the module
 341         * goes into transparent mode (output =input). Normal
 342         * operation can be resumed at the start of the next frame
 343         * after:
 344         *  1) Clearing this event
 345         *  2) Disabling the LSC module
 346         *  3) Enabling it
 347         */
 348        isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
 349                    ISPCCDC_LSC_ENABLE);
 350        ccdc->lsc.state = LSC_STATE_STOPPED;
 351}
 352
 353static void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
 354                                  struct ispccdc_lsc_config_req *req)
 355{
 356        struct isp_device *isp = to_isp_device(ccdc);
 357
 358        if (req == NULL)
 359                return;
 360
 361        if (req->table.addr) {
 362                sg_free_table(&req->table.sgt);
 363                dma_free_coherent(isp->dev, req->config.size, req->table.addr,
 364                                  req->table.dma);
 365        }
 366
 367        kfree(req);
 368}
 369
 370static void ccdc_lsc_free_queue(struct isp_ccdc_device *ccdc,
 371                                struct list_head *queue)
 372{
 373        struct ispccdc_lsc_config_req *req, *n;
 374        unsigned long flags;
 375
 376        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
 377        list_for_each_entry_safe(req, n, queue, list) {
 378                list_del(&req->list);
 379                spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
 380                ccdc_lsc_free_request(ccdc, req);
 381                spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
 382        }
 383        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
 384}
 385
 386static void ccdc_lsc_free_table_work(struct work_struct *work)
 387{
 388        struct isp_ccdc_device *ccdc;
 389        struct ispccdc_lsc *lsc;
 390
 391        lsc = container_of(work, struct ispccdc_lsc, table_work);
 392        ccdc = container_of(lsc, struct isp_ccdc_device, lsc);
 393
 394        ccdc_lsc_free_queue(ccdc, &lsc->free_queue);
 395}
 396
 397/*
 398 * ccdc_lsc_config - Configure the LSC module from a userspace request
 399 *
 400 * Store the request LSC configuration in the LSC engine request pointer. The
 401 * configuration will be applied to the hardware when the CCDC will be enabled,
 402 * or at the next LSC interrupt if the CCDC is already running.
 403 */
 404static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
 405                           struct omap3isp_ccdc_update_config *config)
 406{
 407        struct isp_device *isp = to_isp_device(ccdc);
 408        struct ispccdc_lsc_config_req *req;
 409        unsigned long flags;
 410        u16 update;
 411        int ret;
 412
 413        update = config->update &
 414                 (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC);
 415        if (!update)
 416                return 0;
 417
 418        if (update != (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC)) {
 419                dev_dbg(to_device(ccdc),
 420                        "%s: Both LSC configuration and table need to be supplied\n",
 421                        __func__);
 422                return -EINVAL;
 423        }
 424
 425        req = kzalloc(sizeof(*req), GFP_KERNEL);
 426        if (req == NULL)
 427                return -ENOMEM;
 428
 429        if (config->flag & OMAP3ISP_CCDC_CONFIG_LSC) {
 430                if (copy_from_user(&req->config, config->lsc_cfg,
 431                                   sizeof(req->config))) {
 432                        ret = -EFAULT;
 433                        goto done;
 434                }
 435
 436                req->enable = 1;
 437
 438                req->table.addr = dma_alloc_coherent(isp->dev, req->config.size,
 439                                                     &req->table.dma,
 440                                                     GFP_KERNEL);
 441                if (req->table.addr == NULL) {
 442                        ret = -ENOMEM;
 443                        goto done;
 444                }
 445
 446                ret = dma_get_sgtable(isp->dev, &req->table.sgt,
 447                                      req->table.addr, req->table.dma,
 448                                      req->config.size);
 449                if (ret < 0)
 450                        goto done;
 451
 452                dma_sync_sg_for_cpu(isp->dev, req->table.sgt.sgl,
 453                                    req->table.sgt.nents, DMA_TO_DEVICE);
 454
 455                if (copy_from_user(req->table.addr, config->lsc,
 456                                   req->config.size)) {
 457                        ret = -EFAULT;
 458                        goto done;
 459                }
 460
 461                dma_sync_sg_for_device(isp->dev, req->table.sgt.sgl,
 462                                       req->table.sgt.nents, DMA_TO_DEVICE);
 463        }
 464
 465        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
 466        if (ccdc->lsc.request) {
 467                list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
 468                schedule_work(&ccdc->lsc.table_work);
 469        }
 470        ccdc->lsc.request = req;
 471        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
 472
 473        ret = 0;
 474
 475done:
 476        if (ret < 0)
 477                ccdc_lsc_free_request(ccdc, req);
 478
 479        return ret;
 480}
 481
 482static inline int ccdc_lsc_is_configured(struct isp_ccdc_device *ccdc)
 483{
 484        unsigned long flags;
 485        int ret;
 486
 487        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
 488        ret = ccdc->lsc.active != NULL;
 489        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
 490
 491        return ret;
 492}
 493
 494static int ccdc_lsc_enable(struct isp_ccdc_device *ccdc)
 495{
 496        struct ispccdc_lsc *lsc = &ccdc->lsc;
 497
 498        if (lsc->state != LSC_STATE_STOPPED)
 499                return -EINVAL;
 500
 501        if (lsc->active) {
 502                list_add_tail(&lsc->active->list, &lsc->free_queue);
 503                lsc->active = NULL;
 504        }
 505
 506        if (__ccdc_lsc_configure(ccdc, lsc->request) < 0) {
 507                omap3isp_sbl_disable(to_isp_device(ccdc),
 508                                OMAP3_ISP_SBL_CCDC_LSC_READ);
 509                list_add_tail(&lsc->request->list, &lsc->free_queue);
 510                lsc->request = NULL;
 511                goto done;
 512        }
 513
 514        lsc->active = lsc->request;
 515        lsc->request = NULL;
 516        __ccdc_lsc_enable(ccdc, 1);
 517
 518done:
 519        if (!list_empty(&lsc->free_queue))
 520                schedule_work(&lsc->table_work);
 521
 522        return 0;
 523}
 524
 525/* -----------------------------------------------------------------------------
 526 * Parameters configuration
 527 */
 528
 529/*
 530 * ccdc_configure_clamp - Configure optical-black or digital clamping
 531 * @ccdc: Pointer to ISP CCDC device.
 532 *
 533 * The CCDC performs either optical-black or digital clamp. Configure and enable
 534 * the selected clamp method.
 535 */
 536static void ccdc_configure_clamp(struct isp_ccdc_device *ccdc)
 537{
 538        struct isp_device *isp = to_isp_device(ccdc);
 539        u32 clamp;
 540
 541        if (ccdc->obclamp) {
 542                clamp  = ccdc->clamp.obgain << ISPCCDC_CLAMP_OBGAIN_SHIFT;
 543                clamp |= ccdc->clamp.oblen << ISPCCDC_CLAMP_OBSLEN_SHIFT;
 544                clamp |= ccdc->clamp.oblines << ISPCCDC_CLAMP_OBSLN_SHIFT;
 545                clamp |= ccdc->clamp.obstpixel << ISPCCDC_CLAMP_OBST_SHIFT;
 546                isp_reg_writel(isp, clamp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP);
 547        } else {
 548                isp_reg_writel(isp, ccdc->clamp.dcsubval,
 549                               OMAP3_ISP_IOMEM_CCDC, ISPCCDC_DCSUB);
 550        }
 551
 552        isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP,
 553                        ISPCCDC_CLAMP_CLAMPEN,
 554                        ccdc->obclamp ? ISPCCDC_CLAMP_CLAMPEN : 0);
 555}
 556
 557/*
 558 * ccdc_configure_fpc - Configure Faulty Pixel Correction
 559 * @ccdc: Pointer to ISP CCDC device.
 560 */
 561static void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
 562{
 563        struct isp_device *isp = to_isp_device(ccdc);
 564
 565        isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC, ISPCCDC_FPC_FPCEN);
 566
 567        if (!ccdc->fpc_en)
 568                return;
 569
 570        isp_reg_writel(isp, ccdc->fpc.dma, OMAP3_ISP_IOMEM_CCDC,
 571                       ISPCCDC_FPC_ADDR);
 572        /* The FPNUM field must be set before enabling FPC. */
 573        isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
 574                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
 575        isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT) |
 576                       ISPCCDC_FPC_FPCEN, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
 577}
 578
 579/*
 580 * ccdc_configure_black_comp - Configure Black Level Compensation.
 581 * @ccdc: Pointer to ISP CCDC device.
 582 */
 583static void ccdc_configure_black_comp(struct isp_ccdc_device *ccdc)
 584{
 585        struct isp_device *isp = to_isp_device(ccdc);
 586        u32 blcomp;
 587
 588        blcomp  = ccdc->blcomp.b_mg << ISPCCDC_BLKCMP_B_MG_SHIFT;
 589        blcomp |= ccdc->blcomp.gb_g << ISPCCDC_BLKCMP_GB_G_SHIFT;
 590        blcomp |= ccdc->blcomp.gr_cy << ISPCCDC_BLKCMP_GR_CY_SHIFT;
 591        blcomp |= ccdc->blcomp.r_ye << ISPCCDC_BLKCMP_R_YE_SHIFT;
 592
 593        isp_reg_writel(isp, blcomp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_BLKCMP);
 594}
 595
 596/*
 597 * ccdc_configure_lpf - Configure Low-Pass Filter (LPF).
 598 * @ccdc: Pointer to ISP CCDC device.
 599 */
 600static void ccdc_configure_lpf(struct isp_ccdc_device *ccdc)
 601{
 602        struct isp_device *isp = to_isp_device(ccdc);
 603
 604        isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE,
 605                        ISPCCDC_SYN_MODE_LPF,
 606                        ccdc->lpf ? ISPCCDC_SYN_MODE_LPF : 0);
 607}
 608
 609/*
 610 * ccdc_configure_alaw - Configure A-law compression.
 611 * @ccdc: Pointer to ISP CCDC device.
 612 */
 613static void ccdc_configure_alaw(struct isp_ccdc_device *ccdc)
 614{
 615        struct isp_device *isp = to_isp_device(ccdc);
 616        const struct isp_format_info *info;
 617        u32 alaw = 0;
 618
 619        info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
 620
 621        switch (info->width) {
 622        case 8:
 623                return;
 624
 625        case 10:
 626                alaw = ISPCCDC_ALAW_GWDI_9_0;
 627                break;
 628        case 11:
 629                alaw = ISPCCDC_ALAW_GWDI_10_1;
 630                break;
 631        case 12:
 632                alaw = ISPCCDC_ALAW_GWDI_11_2;
 633                break;
 634        case 13:
 635                alaw = ISPCCDC_ALAW_GWDI_12_3;
 636                break;
 637        }
 638
 639        if (ccdc->alaw)
 640                alaw |= ISPCCDC_ALAW_CCDTBL;
 641
 642        isp_reg_writel(isp, alaw, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_ALAW);
 643}
 644
 645/*
 646 * ccdc_config_imgattr - Configure sensor image specific attributes.
 647 * @ccdc: Pointer to ISP CCDC device.
 648 * @colptn: Color pattern of the sensor.
 649 */
 650static void ccdc_config_imgattr(struct isp_ccdc_device *ccdc, u32 colptn)
 651{
 652        struct isp_device *isp = to_isp_device(ccdc);
 653
 654        isp_reg_writel(isp, colptn, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_COLPTN);
 655}
 656
 657/*
 658 * ccdc_config - Set CCDC configuration from userspace
 659 * @ccdc: Pointer to ISP CCDC device.
 660 * @ccdc_struct: Structure containing CCDC configuration sent from userspace.
 661 *
 662 * Returns 0 if successful, -EINVAL if the pointer to the configuration
 663 * structure is null, or the copy_from_user function fails to copy user space
 664 * memory to kernel space memory.
 665 */
 666static int ccdc_config(struct isp_ccdc_device *ccdc,
 667                       struct omap3isp_ccdc_update_config *ccdc_struct)
 668{
 669        struct isp_device *isp = to_isp_device(ccdc);
 670        unsigned long flags;
 671
 672        spin_lock_irqsave(&ccdc->lock, flags);
 673        ccdc->shadow_update = 1;
 674        spin_unlock_irqrestore(&ccdc->lock, flags);
 675
 676        if (OMAP3ISP_CCDC_ALAW & ccdc_struct->update) {
 677                ccdc->alaw = !!(OMAP3ISP_CCDC_ALAW & ccdc_struct->flag);
 678                ccdc->update |= OMAP3ISP_CCDC_ALAW;
 679        }
 680
 681        if (OMAP3ISP_CCDC_LPF & ccdc_struct->update) {
 682                ccdc->lpf = !!(OMAP3ISP_CCDC_LPF & ccdc_struct->flag);
 683                ccdc->update |= OMAP3ISP_CCDC_LPF;
 684        }
 685
 686        if (OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->update) {
 687                if (copy_from_user(&ccdc->clamp, ccdc_struct->bclamp,
 688                                   sizeof(ccdc->clamp))) {
 689                        ccdc->shadow_update = 0;
 690                        return -EFAULT;
 691                }
 692
 693                ccdc->obclamp = !!(OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->flag);
 694                ccdc->update |= OMAP3ISP_CCDC_BLCLAMP;
 695        }
 696
 697        if (OMAP3ISP_CCDC_BCOMP & ccdc_struct->update) {
 698                if (copy_from_user(&ccdc->blcomp, ccdc_struct->blcomp,
 699                                   sizeof(ccdc->blcomp))) {
 700                        ccdc->shadow_update = 0;
 701                        return -EFAULT;
 702                }
 703
 704                ccdc->update |= OMAP3ISP_CCDC_BCOMP;
 705        }
 706
 707        ccdc->shadow_update = 0;
 708
 709        if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
 710                struct omap3isp_ccdc_fpc fpc;
 711                struct ispccdc_fpc fpc_old = { .addr = NULL, };
 712                struct ispccdc_fpc fpc_new;
 713                u32 size;
 714
 715                if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
 716                        return -EBUSY;
 717
 718                ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
 719
 720                if (ccdc->fpc_en) {
 721                        if (copy_from_user(&fpc, ccdc_struct->fpc, sizeof(fpc)))
 722                                return -EFAULT;
 723
 724                        size = fpc.fpnum * 4;
 725
 726                        /*
 727                         * The table address must be 64-bytes aligned, which is
 728                         * guaranteed by dma_alloc_coherent().
 729                         */
 730                        fpc_new.fpnum = fpc.fpnum;
 731                        fpc_new.addr = dma_alloc_coherent(isp->dev, size,
 732                                                          &fpc_new.dma,
 733                                                          GFP_KERNEL);
 734                        if (fpc_new.addr == NULL)
 735                                return -ENOMEM;
 736
 737                        if (copy_from_user(fpc_new.addr,
 738                                           (__force void __user *)fpc.fpcaddr,
 739                                           size)) {
 740                                dma_free_coherent(isp->dev, size, fpc_new.addr,
 741                                                  fpc_new.dma);
 742                                return -EFAULT;
 743                        }
 744
 745                        fpc_old = ccdc->fpc;
 746                        ccdc->fpc = fpc_new;
 747                }
 748
 749                ccdc_configure_fpc(ccdc);
 750
 751                if (fpc_old.addr != NULL)
 752                        dma_free_coherent(isp->dev, fpc_old.fpnum * 4,
 753                                          fpc_old.addr, fpc_old.dma);
 754        }
 755
 756        return ccdc_lsc_config(ccdc, ccdc_struct);
 757}
 758
 759static void ccdc_apply_controls(struct isp_ccdc_device *ccdc)
 760{
 761        if (ccdc->update & OMAP3ISP_CCDC_ALAW) {
 762                ccdc_configure_alaw(ccdc);
 763                ccdc->update &= ~OMAP3ISP_CCDC_ALAW;
 764        }
 765
 766        if (ccdc->update & OMAP3ISP_CCDC_LPF) {
 767                ccdc_configure_lpf(ccdc);
 768                ccdc->update &= ~OMAP3ISP_CCDC_LPF;
 769        }
 770
 771        if (ccdc->update & OMAP3ISP_CCDC_BLCLAMP) {
 772                ccdc_configure_clamp(ccdc);
 773                ccdc->update &= ~OMAP3ISP_CCDC_BLCLAMP;
 774        }
 775
 776        if (ccdc->update & OMAP3ISP_CCDC_BCOMP) {
 777                ccdc_configure_black_comp(ccdc);
 778                ccdc->update &= ~OMAP3ISP_CCDC_BCOMP;
 779        }
 780}
 781
 782/*
 783 * omap3isp_ccdc_restore_context - Restore values of the CCDC module registers
 784 * @isp: Pointer to ISP device
 785 */
 786void omap3isp_ccdc_restore_context(struct isp_device *isp)
 787{
 788        struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
 789
 790        isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG, ISPCCDC_CFG_VDLC);
 791
 792        ccdc->update = OMAP3ISP_CCDC_ALAW | OMAP3ISP_CCDC_LPF
 793                     | OMAP3ISP_CCDC_BLCLAMP | OMAP3ISP_CCDC_BCOMP;
 794        ccdc_apply_controls(ccdc);
 795        ccdc_configure_fpc(ccdc);
 796}
 797
 798/* -----------------------------------------------------------------------------
 799 * Format- and pipeline-related configuration helpers
 800 */
 801
 802/*
 803 * ccdc_config_vp - Configure the Video Port.
 804 * @ccdc: Pointer to ISP CCDC device.
 805 */
 806static void ccdc_config_vp(struct isp_ccdc_device *ccdc)
 807{
 808        struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
 809        struct isp_device *isp = to_isp_device(ccdc);
 810        const struct isp_format_info *info;
 811        struct v4l2_mbus_framefmt *format;
 812        unsigned long l3_ick = pipe->l3_ick;
 813        unsigned int max_div = isp->revision == ISP_REVISION_15_0 ? 64 : 8;
 814        unsigned int div = 0;
 815        u32 fmtcfg = ISPCCDC_FMTCFG_VPEN;
 816
 817        format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
 818
 819        if (!format->code) {
 820                /* Disable the video port when the input format isn't supported.
 821                 * This is indicated by a pixel code set to 0.
 822                 */
 823                isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
 824                return;
 825        }
 826
 827        isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
 828                       (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
 829                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
 830        isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
 831                       ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
 832                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
 833
 834        isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
 835                       (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
 836                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
 837
 838        info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
 839
 840        switch (info->width) {
 841        case 8:
 842        case 10:
 843                fmtcfg |= ISPCCDC_FMTCFG_VPIN_9_0;
 844                break;
 845        case 11:
 846                fmtcfg |= ISPCCDC_FMTCFG_VPIN_10_1;
 847                break;
 848        case 12:
 849                fmtcfg |= ISPCCDC_FMTCFG_VPIN_11_2;
 850                break;
 851        case 13:
 852                fmtcfg |= ISPCCDC_FMTCFG_VPIN_12_3;
 853                break;
 854        }
 855
 856        if (pipe->input)
 857                div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
 858        else if (pipe->external_rate)
 859                div = l3_ick / pipe->external_rate;
 860
 861        div = clamp(div, 2U, max_div);
 862        fmtcfg |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
 863
 864        isp_reg_writel(isp, fmtcfg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
 865}
 866
 867/*
 868 * ccdc_config_outlineoffset - Configure memory saving output line offset
 869 * @ccdc: Pointer to ISP CCDC device.
 870 * @bpl: Number of bytes per line when stored in memory.
 871 * @field: Field order when storing interlaced formats in memory.
 872 *
 873 * Configure the offsets for the line output control:
 874 *
 875 * - The horizontal line offset is defined as the number of bytes between the
 876 *   start of two consecutive lines in memory. Set it to the given bytes per
 877 *   line value.
 878 *
 879 * - The field offset value is defined as the number of lines to offset the
 880 *   start of the field identified by FID = 1. Set it to one.
 881 *
 882 * - The line offset values are defined as the number of lines (as defined by
 883 *   the horizontal line offset) between the start of two consecutive lines for
 884 *   all combinations of odd/even lines in odd/even fields. When interleaving
 885 *   fields set them all to two lines, and to one line otherwise.
 886 */
 887static void ccdc_config_outlineoffset(struct isp_ccdc_device *ccdc,
 888                                      unsigned int bpl,
 889                                      enum v4l2_field field)
 890{
 891        struct isp_device *isp = to_isp_device(ccdc);
 892        u32 sdofst = 0;
 893
 894        isp_reg_writel(isp, bpl & 0xffff, OMAP3_ISP_IOMEM_CCDC,
 895                       ISPCCDC_HSIZE_OFF);
 896
 897        switch (field) {
 898        case V4L2_FIELD_INTERLACED_TB:
 899        case V4L2_FIELD_INTERLACED_BT:
 900                /* When interleaving fields in memory offset field one by one
 901                 * line and set the line offset to two lines.
 902                 */
 903                sdofst |= (1 << ISPCCDC_SDOFST_LOFST0_SHIFT)
 904                       |  (1 << ISPCCDC_SDOFST_LOFST1_SHIFT)
 905                       |  (1 << ISPCCDC_SDOFST_LOFST2_SHIFT)
 906                       |  (1 << ISPCCDC_SDOFST_LOFST3_SHIFT);
 907                break;
 908
 909        default:
 910                /* In all other cases set the line offsets to one line. */
 911                break;
 912        }
 913
 914        isp_reg_writel(isp, sdofst, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST);
 915}
 916
 917/*
 918 * ccdc_set_outaddr - Set memory address to save output image
 919 * @ccdc: Pointer to ISP CCDC device.
 920 * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
 921 *
 922 * Sets the memory address where the output will be saved.
 923 */
 924static void ccdc_set_outaddr(struct isp_ccdc_device *ccdc, u32 addr)
 925{
 926        struct isp_device *isp = to_isp_device(ccdc);
 927
 928        isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDR_ADDR);
 929}
 930
 931/*
 932 * omap3isp_ccdc_max_rate - Calculate maximum input data rate based on the input
 933 * @ccdc: Pointer to ISP CCDC device.
 934 * @max_rate: Maximum calculated data rate.
 935 *
 936 * Returns in *max_rate less value between calculated and passed
 937 */
 938void omap3isp_ccdc_max_rate(struct isp_ccdc_device *ccdc,
 939                            unsigned int *max_rate)
 940{
 941        struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
 942        unsigned int rate;
 943
 944        if (pipe == NULL)
 945                return;
 946
 947        /*
 948         * TRM says that for parallel sensors the maximum data rate
 949         * should be 90% form L3/2 clock, otherwise just L3/2.
 950         */
 951        if (ccdc->input == CCDC_INPUT_PARALLEL)
 952                rate = pipe->l3_ick / 2 * 9 / 10;
 953        else
 954                rate = pipe->l3_ick / 2;
 955
 956        *max_rate = min(*max_rate, rate);
 957}
 958
 959/*
 960 * ccdc_config_sync_if - Set CCDC sync interface configuration
 961 * @ccdc: Pointer to ISP CCDC device.
 962 * @parcfg: Parallel interface platform data (may be NULL)
 963 * @data_size: Data size
 964 */
 965static void ccdc_config_sync_if(struct isp_ccdc_device *ccdc,
 966                                struct isp_parallel_cfg *parcfg,
 967                                unsigned int data_size)
 968{
 969        struct isp_device *isp = to_isp_device(ccdc);
 970        const struct v4l2_mbus_framefmt *format;
 971        u32 syn_mode = ISPCCDC_SYN_MODE_VDHDEN;
 972
 973        format = &ccdc->formats[CCDC_PAD_SINK];
 974
 975        if (format->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
 976            format->code == MEDIA_BUS_FMT_UYVY8_2X8) {
 977                /* According to the OMAP3 TRM the input mode only affects SYNC
 978                 * mode, enabling BT.656 mode should take precedence. However,
 979                 * in practice setting the input mode to YCbCr data on 8 bits
 980                 * seems to be required in BT.656 mode. In SYNC mode set it to
 981                 * YCbCr on 16 bits as the bridge is enabled in that case.
 982                 */
 983                if (ccdc->bt656)
 984                        syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR8;
 985                else
 986                        syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR16;
 987        }
 988
 989        switch (data_size) {
 990        case 8:
 991                syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
 992                break;
 993        case 10:
 994                syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
 995                break;
 996        case 11:
 997                syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
 998                break;
 999        case 12:
1000                syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
1001                break;
1002        }
1003
1004        if (parcfg && parcfg->data_pol)
1005                syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
1006
1007        if (parcfg && parcfg->hs_pol)
1008                syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
1009
1010        /* The polarity of the vertical sync signal output by the BT.656
1011         * decoder is not documented and seems to be active low.
1012         */
1013        if ((parcfg && parcfg->vs_pol) || ccdc->bt656)
1014                syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
1015
1016        if (parcfg && parcfg->fld_pol)
1017                syn_mode |= ISPCCDC_SYN_MODE_FLDPOL;
1018
1019        isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1020
1021        /* The CCDC_CFG.Y8POS bit is used in YCbCr8 input mode only. The
1022         * hardware seems to ignore it in all other input modes.
1023         */
1024        if (format->code == MEDIA_BUS_FMT_UYVY8_2X8)
1025                isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1026                            ISPCCDC_CFG_Y8POS);
1027        else
1028                isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1029                            ISPCCDC_CFG_Y8POS);
1030
1031        /* Enable or disable BT.656 mode, including error correction for the
1032         * synchronization codes.
1033         */
1034        if (ccdc->bt656)
1035                isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
1036                            ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
1037        else
1038                isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
1039                            ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
1040
1041}
1042
1043/* CCDC formats descriptions */
1044static const u32 ccdc_sgrbg_pattern =
1045        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1046        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1047        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1048        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1049        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1050        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1051        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1052        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1053        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1054        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1055        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1056        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1057        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1058        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1059        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1060        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1061
1062static const u32 ccdc_srggb_pattern =
1063        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1064        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1065        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1066        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1067        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1068        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1069        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1070        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1071        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1072        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1073        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1074        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1075        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1076        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1077        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1078        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1079
1080static const u32 ccdc_sbggr_pattern =
1081        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1082        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1083        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1084        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1085        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1086        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1087        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1088        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1089        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1090        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1091        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1092        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1093        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1094        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1095        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1096        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1097
1098static const u32 ccdc_sgbrg_pattern =
1099        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1100        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1101        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1102        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1103        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1104        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1105        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1106        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1107        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1108        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1109        ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1110        ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1111        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1112        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1113        ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1114        ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1115
1116static void ccdc_configure(struct isp_ccdc_device *ccdc)
1117{
1118        struct isp_device *isp = to_isp_device(ccdc);
1119        struct isp_parallel_cfg *parcfg = NULL;
1120        struct v4l2_subdev *sensor;
1121        struct v4l2_mbus_framefmt *format;
1122        const struct v4l2_rect *crop;
1123        const struct isp_format_info *fmt_info;
1124        struct v4l2_subdev_format fmt_src;
1125        unsigned int depth_out;
1126        unsigned int depth_in = 0;
1127        struct media_pad *pad;
1128        unsigned long flags;
1129        unsigned int bridge;
1130        unsigned int shift;
1131        unsigned int nph;
1132        unsigned int sph;
1133        u32 syn_mode;
1134        u32 ccdc_pattern;
1135
1136        ccdc->bt656 = false;
1137        ccdc->fields = 0;
1138
1139        pad = media_entity_remote_pad(&ccdc->pads[CCDC_PAD_SINK]);
1140        sensor = media_entity_to_v4l2_subdev(pad->entity);
1141        if (ccdc->input == CCDC_INPUT_PARALLEL) {
1142                struct v4l2_mbus_config cfg;
1143                int ret;
1144
1145                ret = v4l2_subdev_call(sensor, video, g_mbus_config, &cfg);
1146                if (!ret)
1147                        ccdc->bt656 = cfg.type == V4L2_MBUS_BT656;
1148
1149                parcfg = &((struct isp_bus_cfg *)sensor->host_priv)
1150                        ->bus.parallel;
1151        }
1152
1153        /* CCDC_PAD_SINK */
1154        format = &ccdc->formats[CCDC_PAD_SINK];
1155
1156        /* Compute the lane shifter shift value and enable the bridge when the
1157         * input format is a non-BT.656 YUV variant.
1158         */
1159        fmt_src.pad = pad->index;
1160        fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1161        if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
1162                fmt_info = omap3isp_video_format_info(fmt_src.format.code);
1163                depth_in = fmt_info->width;
1164        }
1165
1166        fmt_info = omap3isp_video_format_info(format->code);
1167        depth_out = fmt_info->width;
1168        shift = depth_in - depth_out;
1169
1170        if (ccdc->bt656)
1171                bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
1172        else if (fmt_info->code == MEDIA_BUS_FMT_YUYV8_2X8)
1173                bridge = ISPCTRL_PAR_BRIDGE_LENDIAN;
1174        else if (fmt_info->code == MEDIA_BUS_FMT_UYVY8_2X8)
1175                bridge = ISPCTRL_PAR_BRIDGE_BENDIAN;
1176        else
1177                bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
1178
1179        omap3isp_configure_bridge(isp, ccdc->input, parcfg, shift, bridge);
1180
1181        /* Configure the sync interface. */
1182        ccdc_config_sync_if(ccdc, parcfg, depth_out);
1183
1184        syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1185
1186        /* Use the raw, unprocessed data when writing to memory. The H3A and
1187         * histogram modules are still fed with lens shading corrected data.
1188         */
1189        syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
1190
1191        if (ccdc->output & CCDC_OUTPUT_MEMORY)
1192                syn_mode |= ISPCCDC_SYN_MODE_WEN;
1193        else
1194                syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
1195
1196        if (ccdc->output & CCDC_OUTPUT_RESIZER)
1197                syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
1198        else
1199                syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
1200
1201        /* Mosaic filter */
1202        switch (format->code) {
1203        case MEDIA_BUS_FMT_SRGGB10_1X10:
1204        case MEDIA_BUS_FMT_SRGGB12_1X12:
1205                ccdc_pattern = ccdc_srggb_pattern;
1206                break;
1207        case MEDIA_BUS_FMT_SBGGR10_1X10:
1208        case MEDIA_BUS_FMT_SBGGR12_1X12:
1209                ccdc_pattern = ccdc_sbggr_pattern;
1210                break;
1211        case MEDIA_BUS_FMT_SGBRG10_1X10:
1212        case MEDIA_BUS_FMT_SGBRG12_1X12:
1213                ccdc_pattern = ccdc_sgbrg_pattern;
1214                break;
1215        default:
1216                /* Use GRBG */
1217                ccdc_pattern = ccdc_sgrbg_pattern;
1218                break;
1219        }
1220        ccdc_config_imgattr(ccdc, ccdc_pattern);
1221
1222        /* Generate VD0 on the last line of the image and VD1 on the
1223         * 2/3 height line.
1224         */
1225        isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
1226                       ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
1227                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
1228
1229        /* CCDC_PAD_SOURCE_OF */
1230        format = &ccdc->formats[CCDC_PAD_SOURCE_OF];
1231        crop = &ccdc->crop;
1232
1233        /* The horizontal coordinates are expressed in pixel clock cycles. We
1234         * need two cycles per pixel in BT.656 mode, and one cycle per pixel in
1235         * SYNC mode regardless of the format as the bridge is enabled for YUV
1236         * formats in that case.
1237         */
1238        if (ccdc->bt656) {
1239                sph = crop->left * 2;
1240                nph = crop->width * 2 - 1;
1241        } else {
1242                sph = crop->left;
1243                nph = crop->width - 1;
1244        }
1245
1246        isp_reg_writel(isp, (sph << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
1247                       (nph << ISPCCDC_HORZ_INFO_NPH_SHIFT),
1248                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
1249        isp_reg_writel(isp, (crop->top << ISPCCDC_VERT_START_SLV0_SHIFT) |
1250                       (crop->top << ISPCCDC_VERT_START_SLV1_SHIFT),
1251                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
1252        isp_reg_writel(isp, (crop->height - 1)
1253                        << ISPCCDC_VERT_LINES_NLV_SHIFT,
1254                       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
1255
1256        ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value,
1257                                  format->field);
1258
1259        /* When interleaving fields enable processing of the field input signal.
1260         * This will cause the line output control module to apply the field
1261         * offset to field 1.
1262         */
1263        if (ccdc->formats[CCDC_PAD_SINK].field == V4L2_FIELD_ALTERNATE &&
1264            (format->field == V4L2_FIELD_INTERLACED_TB ||
1265             format->field == V4L2_FIELD_INTERLACED_BT))
1266                syn_mode |= ISPCCDC_SYN_MODE_FLDMODE;
1267
1268        /* The CCDC outputs data in UYVY order by default. Swap bytes to get
1269         * YUYV.
1270         */
1271        if (format->code == MEDIA_BUS_FMT_YUYV8_1X16)
1272                isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1273                            ISPCCDC_CFG_BSWD);
1274        else
1275                isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1276                            ISPCCDC_CFG_BSWD);
1277
1278        /* Use PACK8 mode for 1byte per pixel formats. Check for BT.656 mode
1279         * explicitly as the driver reports 1X16 instead of 2X8 at the OF pad
1280         * for simplicity.
1281         */
1282        if (omap3isp_video_format_info(format->code)->width <= 8 || ccdc->bt656)
1283                syn_mode |= ISPCCDC_SYN_MODE_PACK8;
1284        else
1285                syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
1286
1287        isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1288
1289        /* CCDC_PAD_SOURCE_VP */
1290        ccdc_config_vp(ccdc);
1291
1292        /* Lens shading correction. */
1293        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1294        if (ccdc->lsc.request == NULL)
1295                goto unlock;
1296
1297        WARN_ON(ccdc->lsc.active);
1298
1299        /* Get last good LSC configuration. If it is not supported for
1300         * the current active resolution discard it.
1301         */
1302        if (ccdc->lsc.active == NULL &&
1303            __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
1304                ccdc->lsc.active = ccdc->lsc.request;
1305        } else {
1306                list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
1307                schedule_work(&ccdc->lsc.table_work);
1308        }
1309
1310        ccdc->lsc.request = NULL;
1311
1312unlock:
1313        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1314
1315        ccdc_apply_controls(ccdc);
1316}
1317
1318static void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
1319{
1320        struct isp_device *isp = to_isp_device(ccdc);
1321
1322        isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
1323                        ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
1324
1325        ccdc->running = enable;
1326}
1327
1328static int ccdc_disable(struct isp_ccdc_device *ccdc)
1329{
1330        unsigned long flags;
1331        int ret = 0;
1332
1333        spin_lock_irqsave(&ccdc->lock, flags);
1334        if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1335                ccdc->stopping = CCDC_STOP_REQUEST;
1336        if (!ccdc->running)
1337                ccdc->stopping = CCDC_STOP_FINISHED;
1338        spin_unlock_irqrestore(&ccdc->lock, flags);
1339
1340        ret = wait_event_timeout(ccdc->wait,
1341                                 ccdc->stopping == CCDC_STOP_FINISHED,
1342                                 msecs_to_jiffies(2000));
1343        if (ret == 0) {
1344                ret = -ETIMEDOUT;
1345                dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
1346        }
1347
1348        omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
1349
1350        mutex_lock(&ccdc->ioctl_lock);
1351        ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
1352        ccdc->lsc.request = ccdc->lsc.active;
1353        ccdc->lsc.active = NULL;
1354        cancel_work_sync(&ccdc->lsc.table_work);
1355        ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
1356        mutex_unlock(&ccdc->ioctl_lock);
1357
1358        ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
1359
1360        return ret > 0 ? 0 : ret;
1361}
1362
1363static void ccdc_enable(struct isp_ccdc_device *ccdc)
1364{
1365        if (ccdc_lsc_is_configured(ccdc))
1366                __ccdc_lsc_enable(ccdc, 1);
1367        __ccdc_enable(ccdc, 1);
1368}
1369
1370/* -----------------------------------------------------------------------------
1371 * Interrupt handling
1372 */
1373
1374/*
1375 * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
1376 * @ccdc: Pointer to ISP CCDC device.
1377 *
1378 * Returns zero if the CCDC is idle and the image has been written to
1379 * memory, too.
1380 */
1381static int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
1382{
1383        struct isp_device *isp = to_isp_device(ccdc);
1384
1385        return omap3isp_ccdc_busy(ccdc)
1386                | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
1387                   ISPSBL_CCDC_WR_0_DATA_READY)
1388                | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
1389                   ISPSBL_CCDC_WR_0_DATA_READY)
1390                | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
1391                   ISPSBL_CCDC_WR_0_DATA_READY)
1392                | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
1393                   ISPSBL_CCDC_WR_0_DATA_READY);
1394}
1395
1396/*
1397 * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
1398 * @ccdc: Pointer to ISP CCDC device.
1399 * @max_wait: Max retry count in us for wait for idle/busy transition.
1400 */
1401static int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
1402                              unsigned int max_wait)
1403{
1404        unsigned int wait = 0;
1405
1406        if (max_wait == 0)
1407                max_wait = 10000; /* 10 ms */
1408
1409        for (wait = 0; wait <= max_wait; wait++) {
1410                if (!ccdc_sbl_busy(ccdc))
1411                        return 0;
1412
1413                rmb();
1414                udelay(1);
1415        }
1416
1417        return -EBUSY;
1418}
1419
1420/* ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
1421 * @ccdc: Pointer to ISP CCDC device.
1422 * @event: Pointing which event trigger handler
1423 *
1424 * Return 1 when the event and stopping request combination is satisfied,
1425 * zero otherwise.
1426 */
1427static int ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
1428{
1429        int rval = 0;
1430
1431        switch ((ccdc->stopping & 3) | event) {
1432        case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
1433                if (ccdc->lsc.state != LSC_STATE_STOPPED)
1434                        __ccdc_lsc_enable(ccdc, 0);
1435                __ccdc_enable(ccdc, 0);
1436                ccdc->stopping = CCDC_STOP_EXECUTED;
1437                return 1;
1438
1439        case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
1440                ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
1441                if (ccdc->lsc.state == LSC_STATE_STOPPED)
1442                        ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1443                rval = 1;
1444                break;
1445
1446        case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
1447                ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1448                rval = 1;
1449                break;
1450
1451        case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
1452                return 1;
1453        }
1454
1455        if (ccdc->stopping == CCDC_STOP_FINISHED) {
1456                wake_up(&ccdc->wait);
1457                rval = 1;
1458        }
1459
1460        return rval;
1461}
1462
1463static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
1464{
1465        struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1466        struct video_device *vdev = ccdc->subdev.devnode;
1467        struct v4l2_event event;
1468
1469        /* Frame number propagation */
1470        atomic_inc(&pipe->frame_number);
1471
1472        memset(&event, 0, sizeof(event));
1473        event.type = V4L2_EVENT_FRAME_SYNC;
1474        event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
1475
1476        v4l2_event_queue(vdev, &event);
1477}
1478
1479/*
1480 * ccdc_lsc_isr - Handle LSC events
1481 * @ccdc: Pointer to ISP CCDC device.
1482 * @events: LSC events
1483 */
1484static void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
1485{
1486        unsigned long flags;
1487
1488        if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
1489                struct isp_pipeline *pipe =
1490                        to_isp_pipeline(&ccdc->subdev.entity);
1491
1492                ccdc_lsc_error_handler(ccdc);
1493                pipe->error = true;
1494                dev_dbg(to_device(ccdc), "lsc prefetch error\n");
1495        }
1496
1497        if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
1498                return;
1499
1500        /* LSC_DONE interrupt occur, there are two cases
1501         * 1. stopping for reconfiguration
1502         * 2. stopping because of STREAM OFF command
1503         */
1504        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1505
1506        if (ccdc->lsc.state == LSC_STATE_STOPPING)
1507                ccdc->lsc.state = LSC_STATE_STOPPED;
1508
1509        if (ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
1510                goto done;
1511
1512        if (ccdc->lsc.state != LSC_STATE_RECONFIG)
1513                goto done;
1514
1515        /* LSC is in STOPPING state, change to the new state */
1516        ccdc->lsc.state = LSC_STATE_STOPPED;
1517
1518        /* This is an exception. Start of frame and LSC_DONE interrupt
1519         * have been received on the same time. Skip this event and wait
1520         * for better times.
1521         */
1522        if (events & IRQ0STATUS_HS_VS_IRQ)
1523                goto done;
1524
1525        /* The LSC engine is stopped at this point. Enable it if there's a
1526         * pending request.
1527         */
1528        if (ccdc->lsc.request == NULL)
1529                goto done;
1530
1531        ccdc_lsc_enable(ccdc);
1532
1533done:
1534        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1535}
1536
1537/*
1538 * Check whether the CCDC has captured all fields necessary to complete the
1539 * buffer.
1540 */
1541static bool ccdc_has_all_fields(struct isp_ccdc_device *ccdc)
1542{
1543        struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1544        struct isp_device *isp = to_isp_device(ccdc);
1545        enum v4l2_field of_field = ccdc->formats[CCDC_PAD_SOURCE_OF].field;
1546        enum v4l2_field field;
1547
1548        /* When the input is progressive fields don't matter. */
1549        if (of_field == V4L2_FIELD_NONE)
1550                return true;
1551
1552        /* Read the current field identifier. */
1553        field = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE)
1554              & ISPCCDC_SYN_MODE_FLDSTAT
1555              ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
1556
1557        /* When capturing fields in alternate order just store the current field
1558         * identifier in the pipeline.
1559         */
1560        if (of_field == V4L2_FIELD_ALTERNATE) {
1561                pipe->field = field;
1562                return true;
1563        }
1564
1565        /* The format is interlaced. Make sure we've captured both fields. */
1566        ccdc->fields |= field == V4L2_FIELD_BOTTOM
1567                      ? CCDC_FIELD_BOTTOM : CCDC_FIELD_TOP;
1568
1569        if (ccdc->fields != CCDC_FIELD_BOTH)
1570                return false;
1571
1572        /* Verify that the field just captured corresponds to the last field
1573         * needed based on the desired field order.
1574         */
1575        if ((of_field == V4L2_FIELD_INTERLACED_TB && field == V4L2_FIELD_TOP) ||
1576            (of_field == V4L2_FIELD_INTERLACED_BT && field == V4L2_FIELD_BOTTOM))
1577                return false;
1578
1579        /* The buffer can be completed, reset the fields for the next buffer. */
1580        ccdc->fields = 0;
1581
1582        return true;
1583}
1584
1585static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
1586{
1587        struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1588        struct isp_device *isp = to_isp_device(ccdc);
1589        struct isp_buffer *buffer;
1590
1591        /* The CCDC generates VD0 interrupts even when disabled (the datasheet
1592         * doesn't explicitly state if that's supposed to happen or not, so it
1593         * can be considered as a hardware bug or as a feature, but we have to
1594         * deal with it anyway). Disabling the CCDC when no buffer is available
1595         * would thus not be enough, we need to handle the situation explicitly.
1596         */
1597        if (list_empty(&ccdc->video_out.dmaqueue))
1598                return 0;
1599
1600        /* We're in continuous mode, and memory writes were disabled due to a
1601         * buffer underrun. Reenable them now that we have a buffer. The buffer
1602         * address has been set in ccdc_video_queue.
1603         */
1604        if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
1605                ccdc->underrun = 0;
1606                return 1;
1607        }
1608
1609        /* Wait for the CCDC to become idle. */
1610        if (ccdc_sbl_wait_idle(ccdc, 1000)) {
1611                dev_info(isp->dev, "CCDC won't become idle!\n");
1612                media_entity_enum_set(&isp->crashed, &ccdc->subdev.entity);
1613                omap3isp_pipeline_cancel_stream(pipe);
1614                return 0;
1615        }
1616
1617        if (!ccdc_has_all_fields(ccdc))
1618                return 1;
1619
1620        buffer = omap3isp_video_buffer_next(&ccdc->video_out);
1621        if (buffer != NULL)
1622                ccdc_set_outaddr(ccdc, buffer->dma);
1623
1624        pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1625
1626        if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1627            isp_pipeline_ready(pipe))
1628                omap3isp_pipeline_set_stream(pipe,
1629                                        ISP_PIPELINE_STREAM_SINGLESHOT);
1630
1631        return buffer != NULL;
1632}
1633
1634/*
1635 * ccdc_vd0_isr - Handle VD0 event
1636 * @ccdc: Pointer to ISP CCDC device.
1637 *
1638 * Executes LSC deferred enablement before next frame starts.
1639 */
1640static void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
1641{
1642        unsigned long flags;
1643        int restart = 0;
1644
1645        /* In BT.656 mode the CCDC doesn't generate an HS/VS interrupt. We thus
1646         * need to increment the frame counter here.
1647         */
1648        if (ccdc->bt656) {
1649                struct isp_pipeline *pipe =
1650                        to_isp_pipeline(&ccdc->subdev.entity);
1651
1652                atomic_inc(&pipe->frame_number);
1653        }
1654
1655        /* Emulate a VD1 interrupt for BT.656 mode, as we can't stop the CCDC in
1656         * the VD1 interrupt handler in that mode without risking a CCDC stall
1657         * if a short frame is received.
1658         */
1659        if (ccdc->bt656) {
1660                spin_lock_irqsave(&ccdc->lock, flags);
1661                if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1662                    ccdc->output & CCDC_OUTPUT_MEMORY) {
1663                        if (ccdc->lsc.state != LSC_STATE_STOPPED)
1664                                __ccdc_lsc_enable(ccdc, 0);
1665                        __ccdc_enable(ccdc, 0);
1666                }
1667                ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1);
1668                spin_unlock_irqrestore(&ccdc->lock, flags);
1669        }
1670
1671        if (ccdc->output & CCDC_OUTPUT_MEMORY)
1672                restart = ccdc_isr_buffer(ccdc);
1673
1674        spin_lock_irqsave(&ccdc->lock, flags);
1675
1676        if (ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
1677                spin_unlock_irqrestore(&ccdc->lock, flags);
1678                return;
1679        }
1680
1681        if (!ccdc->shadow_update)
1682                ccdc_apply_controls(ccdc);
1683        spin_unlock_irqrestore(&ccdc->lock, flags);
1684
1685        if (restart)
1686                ccdc_enable(ccdc);
1687}
1688
1689/*
1690 * ccdc_vd1_isr - Handle VD1 event
1691 * @ccdc: Pointer to ISP CCDC device.
1692 */
1693static void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
1694{
1695        unsigned long flags;
1696
1697        /* In BT.656 mode the synchronization signals are generated by the CCDC
1698         * from the embedded sync codes. The VD0 and VD1 interrupts are thus
1699         * only triggered when the CCDC is enabled, unlike external sync mode
1700         * where the line counter runs even when the CCDC is stopped. We can't
1701         * disable the CCDC at VD1 time, as no VD0 interrupt would be generated
1702         * for a short frame, which would result in the CCDC being stopped and
1703         * no VD interrupt generated anymore. The CCDC is stopped from the VD0
1704         * interrupt handler instead for BT.656.
1705         */
1706        if (ccdc->bt656)
1707                return;
1708
1709        spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1710
1711        /*
1712         * Depending on the CCDC pipeline state, CCDC stopping should be
1713         * handled differently. In SINGLESHOT we emulate an internal CCDC
1714         * stopping because the CCDC hw works only in continuous mode.
1715         * When CONTINUOUS pipeline state is used and the CCDC writes it's
1716         * data to memory the CCDC and LSC are stopped immediately but
1717         * without change the CCDC stopping state machine. The CCDC
1718         * stopping state machine should be used only when user request
1719         * for stopping is received (SINGLESHOT is an exeption).
1720         */
1721        switch (ccdc->state) {
1722        case ISP_PIPELINE_STREAM_SINGLESHOT:
1723                ccdc->stopping = CCDC_STOP_REQUEST;
1724                break;
1725
1726        case ISP_PIPELINE_STREAM_CONTINUOUS:
1727                if (ccdc->output & CCDC_OUTPUT_MEMORY) {
1728                        if (ccdc->lsc.state != LSC_STATE_STOPPED)
1729                                __ccdc_lsc_enable(ccdc, 0);
1730                        __ccdc_enable(ccdc, 0);
1731                }
1732                break;
1733
1734        case ISP_PIPELINE_STREAM_STOPPED:
1735                break;
1736        }
1737
1738        if (ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
1739                goto done;
1740
1741        if (ccdc->lsc.request == NULL)
1742                goto done;
1743
1744        /*
1745         * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
1746         * do the appropriate changes in registers
1747         */
1748        if (ccdc->lsc.state == LSC_STATE_RUNNING) {
1749                __ccdc_lsc_enable(ccdc, 0);
1750                ccdc->lsc.state = LSC_STATE_RECONFIG;
1751                goto done;
1752        }
1753
1754        /* LSC has been in STOPPED state, enable it */
1755        if (ccdc->lsc.state == LSC_STATE_STOPPED)
1756                ccdc_lsc_enable(ccdc);
1757
1758done:
1759        spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1760}
1761
1762/*
1763 * omap3isp_ccdc_isr - Configure CCDC during interframe time.
1764 * @ccdc: Pointer to ISP CCDC device.
1765 * @events: CCDC events
1766 */
1767int omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
1768{
1769        if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
1770                return 0;
1771
1772        if (events & IRQ0STATUS_CCDC_VD1_IRQ)
1773                ccdc_vd1_isr(ccdc);
1774
1775        ccdc_lsc_isr(ccdc, events);
1776
1777        if (events & IRQ0STATUS_CCDC_VD0_IRQ)
1778                ccdc_vd0_isr(ccdc);
1779
1780        if (events & IRQ0STATUS_HS_VS_IRQ)
1781                ccdc_hs_vs_isr(ccdc);
1782
1783        return 0;
1784}
1785
1786/* -----------------------------------------------------------------------------
1787 * ISP video operations
1788 */
1789
1790static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
1791{
1792        struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
1793        unsigned long flags;
1794        bool restart = false;
1795
1796        if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
1797                return -ENODEV;
1798
1799        ccdc_set_outaddr(ccdc, buffer->dma);
1800
1801        /* We now have a buffer queued on the output, restart the pipeline
1802         * on the next CCDC interrupt if running in continuous mode (or when
1803         * starting the stream) in external sync mode, or immediately in BT.656
1804         * sync mode as no CCDC interrupt is generated when the CCDC is stopped
1805         * in that case.
1806         */
1807        spin_lock_irqsave(&ccdc->lock, flags);
1808        if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && !ccdc->running &&
1809            ccdc->bt656)
1810                restart = true;
1811        else
1812                ccdc->underrun = 1;
1813        spin_unlock_irqrestore(&ccdc->lock, flags);
1814
1815        if (restart)
1816                ccdc_enable(ccdc);
1817
1818        return 0;
1819}
1820
1821static const struct isp_video_operations ccdc_video_ops = {
1822        .queue = ccdc_video_queue,
1823};
1824
1825/* -----------------------------------------------------------------------------
1826 * V4L2 subdev operations
1827 */
1828
1829/*
1830 * ccdc_ioctl - CCDC module private ioctl's
1831 * @sd: ISP CCDC V4L2 subdevice
1832 * @cmd: ioctl command
1833 * @arg: ioctl argument
1834 *
1835 * Return 0 on success or a negative error code otherwise.
1836 */
1837static long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1838{
1839        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1840        int ret;
1841
1842        switch (cmd) {
1843        case VIDIOC_OMAP3ISP_CCDC_CFG:
1844                mutex_lock(&ccdc->ioctl_lock);
1845                ret = ccdc_config(ccdc, arg);
1846                mutex_unlock(&ccdc->ioctl_lock);
1847                break;
1848
1849        default:
1850                return -ENOIOCTLCMD;
1851        }
1852
1853        return ret;
1854}
1855
1856static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1857                                struct v4l2_event_subscription *sub)
1858{
1859        if (sub->type != V4L2_EVENT_FRAME_SYNC)
1860                return -EINVAL;
1861
1862        /* line number is zero at frame start */
1863        if (sub->id != 0)
1864                return -EINVAL;
1865
1866        return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
1867}
1868
1869static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1870                                  struct v4l2_event_subscription *sub)
1871{
1872        return v4l2_event_unsubscribe(fh, sub);
1873}
1874
1875/*
1876 * ccdc_set_stream - Enable/Disable streaming on the CCDC module
1877 * @sd: ISP CCDC V4L2 subdevice
1878 * @enable: Enable/disable stream
1879 *
1880 * When writing to memory, the CCDC hardware can't be enabled without a memory
1881 * buffer to write to. As the s_stream operation is called in response to a
1882 * STREAMON call without any buffer queued yet, just update the enabled field
1883 * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
1884 *
1885 * When not writing to memory enable the CCDC immediately.
1886 */
1887static int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
1888{
1889        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1890        struct isp_device *isp = to_isp_device(ccdc);
1891        int ret = 0;
1892
1893        if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
1894                if (enable == ISP_PIPELINE_STREAM_STOPPED)
1895                        return 0;
1896
1897                omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
1898                isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1899                            ISPCCDC_CFG_VDLC);
1900
1901                ccdc_configure(ccdc);
1902
1903                ccdc_print_status(ccdc);
1904        }
1905
1906        switch (enable) {
1907        case ISP_PIPELINE_STREAM_CONTINUOUS:
1908                if (ccdc->output & CCDC_OUTPUT_MEMORY)
1909                        omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1910
1911                if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
1912                        ccdc_enable(ccdc);
1913
1914                ccdc->underrun = 0;
1915                break;
1916
1917        case ISP_PIPELINE_STREAM_SINGLESHOT:
1918                if (ccdc->output & CCDC_OUTPUT_MEMORY &&
1919                    ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
1920                        omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1921
1922                ccdc_enable(ccdc);
1923                break;
1924
1925        case ISP_PIPELINE_STREAM_STOPPED:
1926                ret = ccdc_disable(ccdc);
1927                if (ccdc->output & CCDC_OUTPUT_MEMORY)
1928                        omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1929                omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
1930                ccdc->underrun = 0;
1931                break;
1932        }
1933
1934        ccdc->state = enable;
1935        return ret;
1936}
1937
1938static struct v4l2_mbus_framefmt *
1939__ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_pad_config *cfg,
1940                  unsigned int pad, enum v4l2_subdev_format_whence which)
1941{
1942        if (which == V4L2_SUBDEV_FORMAT_TRY)
1943                return v4l2_subdev_get_try_format(&ccdc->subdev, cfg, pad);
1944        else
1945                return &ccdc->formats[pad];
1946}
1947
1948static struct v4l2_rect *
1949__ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_pad_config *cfg,
1950                enum v4l2_subdev_format_whence which)
1951{
1952        if (which == V4L2_SUBDEV_FORMAT_TRY)
1953                return v4l2_subdev_get_try_crop(&ccdc->subdev, cfg, CCDC_PAD_SOURCE_OF);
1954        else
1955                return &ccdc->crop;
1956}
1957
1958/*
1959 * ccdc_try_format - Try video format on a pad
1960 * @ccdc: ISP CCDC device
1961 * @cfg : V4L2 subdev pad configuration
1962 * @pad: Pad number
1963 * @fmt: Format
1964 */
1965static void
1966ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_pad_config *cfg,
1967                unsigned int pad, struct v4l2_mbus_framefmt *fmt,
1968                enum v4l2_subdev_format_whence which)
1969{
1970        const struct isp_format_info *info;
1971        u32 pixelcode;
1972        unsigned int width = fmt->width;
1973        unsigned int height = fmt->height;
1974        struct v4l2_rect *crop;
1975        enum v4l2_field field;
1976        unsigned int i;
1977
1978        switch (pad) {
1979        case CCDC_PAD_SINK:
1980                for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
1981                        if (fmt->code == ccdc_fmts[i])
1982                                break;
1983                }
1984
1985                /* If not found, use SGRBG10 as default */
1986                if (i >= ARRAY_SIZE(ccdc_fmts))
1987                        fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1988
1989                /* Clamp the input size. */
1990                fmt->width = clamp_t(u32, width, 32, 4096);
1991                fmt->height = clamp_t(u32, height, 32, 4096);
1992
1993                /* Default to progressive field order. */
1994                if (fmt->field == V4L2_FIELD_ANY)
1995                        fmt->field = V4L2_FIELD_NONE;
1996
1997                break;
1998
1999        case CCDC_PAD_SOURCE_OF:
2000                pixelcode = fmt->code;
2001                field = fmt->field;
2002                *fmt = *__ccdc_get_format(ccdc, cfg, CCDC_PAD_SINK, which);
2003
2004                /* In SYNC mode the bridge converts YUV formats from 2X8 to
2005                 * 1X16. In BT.656 no such conversion occurs. As we don't know
2006                 * at this point whether the source will use SYNC or BT.656 mode
2007                 * let's pretend the conversion always occurs. The CCDC will be
2008                 * configured to pack bytes in BT.656, hiding the inaccuracy.
2009                 * In all cases bytes can be swapped.
2010                 */
2011                if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
2012                    fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
2013                        /* Use the user requested format if YUV. */
2014                        if (pixelcode == MEDIA_BUS_FMT_YUYV8_2X8 ||
2015                            pixelcode == MEDIA_BUS_FMT_UYVY8_2X8 ||
2016                            pixelcode == MEDIA_BUS_FMT_YUYV8_1X16 ||
2017                            pixelcode == MEDIA_BUS_FMT_UYVY8_1X16)
2018                                fmt->code = pixelcode;
2019
2020                        if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8)
2021                                fmt->code = MEDIA_BUS_FMT_YUYV8_1X16;
2022                        else if (fmt->code == MEDIA_BUS_FMT_UYVY8_2X8)
2023                                fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
2024                }
2025
2026                /* Hardcode the output size to the crop rectangle size. */
2027                crop = __ccdc_get_crop(ccdc, cfg, which);
2028                fmt->width = crop->width;
2029                fmt->height = crop->height;
2030
2031                /* When input format is interlaced with alternating fields the
2032                 * CCDC can interleave the fields.
2033                 */
2034                if (fmt->field == V4L2_FIELD_ALTERNATE &&
2035                    (field == V4L2_FIELD_INTERLACED_TB ||
2036                     field == V4L2_FIELD_INTERLACED_BT)) {
2037                        fmt->field = field;
2038                        fmt->height *= 2;
2039                }
2040
2041                break;
2042
2043        case CCDC_PAD_SOURCE_VP:
2044                *fmt = *__ccdc_get_format(ccdc, cfg, CCDC_PAD_SINK, which);
2045
2046                /* The video port interface truncates the data to 10 bits. */
2047                info = omap3isp_video_format_info(fmt->code);
2048                fmt->code = info->truncated;
2049
2050                /* YUV formats are not supported by the video port. */
2051                if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
2052                    fmt->code == MEDIA_BUS_FMT_UYVY8_2X8)
2053                        fmt->code = 0;
2054
2055                /* The number of lines that can be clocked out from the video
2056                 * port output must be at least one line less than the number
2057                 * of input lines.
2058                 */
2059                fmt->width = clamp_t(u32, width, 32, fmt->width);
2060                fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
2061                break;
2062        }
2063
2064        /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
2065         * stored on 2 bytes.
2066         */
2067        fmt->colorspace = V4L2_COLORSPACE_SRGB;
2068}
2069
2070/*
2071 * ccdc_try_crop - Validate a crop rectangle
2072 * @ccdc: ISP CCDC device
2073 * @sink: format on the sink pad
2074 * @crop: crop rectangle to be validated
2075 */
2076static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
2077                          const struct v4l2_mbus_framefmt *sink,
2078                          struct v4l2_rect *crop)
2079{
2080        const struct isp_format_info *info;
2081        unsigned int max_width;
2082
2083        /* For Bayer formats, restrict left/top and width/height to even values
2084         * to keep the Bayer pattern.
2085         */
2086        info = omap3isp_video_format_info(sink->code);
2087        if (info->flavor != MEDIA_BUS_FMT_Y8_1X8) {
2088                crop->left &= ~1;
2089                crop->top &= ~1;
2090        }
2091
2092        crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
2093        crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
2094
2095        /* The data formatter truncates the number of horizontal output pixels
2096         * to a multiple of 16. To avoid clipping data, allow callers to request
2097         * an output size bigger than the input size up to the nearest multiple
2098         * of 16.
2099         */
2100        max_width = (sink->width - crop->left + 15) & ~15;
2101        crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
2102                    & ~15;
2103        crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
2104                               sink->height - crop->top);
2105
2106        /* Odd width/height values don't make sense for Bayer formats. */
2107        if (info->flavor != MEDIA_BUS_FMT_Y8_1X8) {
2108                crop->width &= ~1;
2109                crop->height &= ~1;
2110        }
2111}
2112
2113/*
2114 * ccdc_enum_mbus_code - Handle pixel format enumeration
2115 * @sd     : pointer to v4l2 subdev structure
2116 * @cfg : V4L2 subdev pad configuration
2117 * @code   : pointer to v4l2_subdev_mbus_code_enum structure
2118 * return -EINVAL or zero on success
2119 */
2120static int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
2121                               struct v4l2_subdev_pad_config *cfg,
2122                               struct v4l2_subdev_mbus_code_enum *code)
2123{
2124        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2125        struct v4l2_mbus_framefmt *format;
2126
2127        switch (code->pad) {
2128        case CCDC_PAD_SINK:
2129                if (code->index >= ARRAY_SIZE(ccdc_fmts))
2130                        return -EINVAL;
2131
2132                code->code = ccdc_fmts[code->index];
2133                break;
2134
2135        case CCDC_PAD_SOURCE_OF:
2136                format = __ccdc_get_format(ccdc, cfg, code->pad,
2137                                           code->which);
2138
2139                if (format->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
2140                    format->code == MEDIA_BUS_FMT_UYVY8_2X8) {
2141                        /* In YUV mode the CCDC can swap bytes. */
2142                        if (code->index == 0)
2143                                code->code = MEDIA_BUS_FMT_YUYV8_1X16;
2144                        else if (code->index == 1)
2145                                code->code = MEDIA_BUS_FMT_UYVY8_1X16;
2146                        else
2147                                return -EINVAL;
2148                } else {
2149                        /* In raw mode, no configurable format confversion is
2150                         * available.
2151                         */
2152                        if (code->index == 0)
2153                                code->code = format->code;
2154                        else
2155                                return -EINVAL;
2156                }
2157                break;
2158
2159        case CCDC_PAD_SOURCE_VP:
2160                /* The CCDC supports no configurable format conversion
2161                 * compatible with the video port. Enumerate a single output
2162                 * format code.
2163                 */
2164                if (code->index != 0)
2165                        return -EINVAL;
2166
2167                format = __ccdc_get_format(ccdc, cfg, code->pad,
2168                                           code->which);
2169
2170                /* A pixel code equal to 0 means that the video port doesn't
2171                 * support the input format. Don't enumerate any pixel code.
2172                 */
2173                if (format->code == 0)
2174                        return -EINVAL;
2175
2176                code->code = format->code;
2177                break;
2178
2179        default:
2180                return -EINVAL;
2181        }
2182
2183        return 0;
2184}
2185
2186static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
2187                                struct v4l2_subdev_pad_config *cfg,
2188                                struct v4l2_subdev_frame_size_enum *fse)
2189{
2190        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2191        struct v4l2_mbus_framefmt format;
2192
2193        if (fse->index != 0)
2194                return -EINVAL;
2195
2196        format.code = fse->code;
2197        format.width = 1;
2198        format.height = 1;
2199        ccdc_try_format(ccdc, cfg, fse->pad, &format, fse->which);
2200        fse->min_width = format.width;
2201        fse->min_height = format.height;
2202
2203        if (format.code != fse->code)
2204                return -EINVAL;
2205
2206        format.code = fse->code;
2207        format.width = -1;
2208        format.height = -1;
2209        ccdc_try_format(ccdc, cfg, fse->pad, &format, fse->which);
2210        fse->max_width = format.width;
2211        fse->max_height = format.height;
2212
2213        return 0;
2214}
2215
2216/*
2217 * ccdc_get_selection - Retrieve a selection rectangle on a pad
2218 * @sd: ISP CCDC V4L2 subdevice
2219 * @cfg: V4L2 subdev pad configuration
2220 * @sel: Selection rectangle
2221 *
2222 * The only supported rectangles are the crop rectangles on the output formatter
2223 * source pad.
2224 *
2225 * Return 0 on success or a negative error code otherwise.
2226 */
2227static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
2228                              struct v4l2_subdev_selection *sel)
2229{
2230        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2231        struct v4l2_mbus_framefmt *format;
2232
2233        if (sel->pad != CCDC_PAD_SOURCE_OF)
2234                return -EINVAL;
2235
2236        switch (sel->target) {
2237        case V4L2_SEL_TGT_CROP_BOUNDS:
2238                sel->r.left = 0;
2239                sel->r.top = 0;
2240                sel->r.width = INT_MAX;
2241                sel->r.height = INT_MAX;
2242
2243                format = __ccdc_get_format(ccdc, cfg, CCDC_PAD_SINK, sel->which);
2244                ccdc_try_crop(ccdc, format, &sel->r);
2245                break;
2246
2247        case V4L2_SEL_TGT_CROP:
2248                sel->r = *__ccdc_get_crop(ccdc, cfg, sel->which);
2249                break;
2250
2251        default:
2252                return -EINVAL;
2253        }
2254
2255        return 0;
2256}
2257
2258/*
2259 * ccdc_set_selection - Set a selection rectangle on a pad
2260 * @sd: ISP CCDC V4L2 subdevice
2261 * @cfg: V4L2 subdev pad configuration
2262 * @sel: Selection rectangle
2263 *
2264 * The only supported rectangle is the actual crop rectangle on the output
2265 * formatter source pad.
2266 *
2267 * Return 0 on success or a negative error code otherwise.
2268 */
2269static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
2270                              struct v4l2_subdev_selection *sel)
2271{
2272        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2273        struct v4l2_mbus_framefmt *format;
2274
2275        if (sel->target != V4L2_SEL_TGT_CROP ||
2276            sel->pad != CCDC_PAD_SOURCE_OF)
2277                return -EINVAL;
2278
2279        /* The crop rectangle can't be changed while streaming. */
2280        if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
2281                return -EBUSY;
2282
2283        /* Modifying the crop rectangle always changes the format on the source
2284         * pad. If the KEEP_CONFIG flag is set, just return the current crop
2285         * rectangle.
2286         */
2287        if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
2288                sel->r = *__ccdc_get_crop(ccdc, cfg, sel->which);
2289                return 0;
2290        }
2291
2292        format = __ccdc_get_format(ccdc, cfg, CCDC_PAD_SINK, sel->which);
2293        ccdc_try_crop(ccdc, format, &sel->r);
2294        *__ccdc_get_crop(ccdc, cfg, sel->which) = sel->r;
2295
2296        /* Update the source format. */
2297        format = __ccdc_get_format(ccdc, cfg, CCDC_PAD_SOURCE_OF, sel->which);
2298        ccdc_try_format(ccdc, cfg, CCDC_PAD_SOURCE_OF, format, sel->which);
2299
2300        return 0;
2301}
2302
2303/*
2304 * ccdc_get_format - Retrieve the video format on a pad
2305 * @sd : ISP CCDC V4L2 subdevice
2306 * @cfg: V4L2 subdev pad configuration
2307 * @fmt: Format
2308 *
2309 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2310 * to the format type.
2311 */
2312static int ccdc_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
2313                           struct v4l2_subdev_format *fmt)
2314{
2315        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2316        struct v4l2_mbus_framefmt *format;
2317
2318        format = __ccdc_get_format(ccdc, cfg, fmt->pad, fmt->which);
2319        if (format == NULL)
2320                return -EINVAL;
2321
2322        fmt->format = *format;
2323        return 0;
2324}
2325
2326/*
2327 * ccdc_set_format - Set the video format on a pad
2328 * @sd : ISP CCDC V4L2 subdevice
2329 * @cfg: V4L2 subdev pad configuration
2330 * @fmt: Format
2331 *
2332 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2333 * to the format type.
2334 */
2335static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
2336                           struct v4l2_subdev_format *fmt)
2337{
2338        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2339        struct v4l2_mbus_framefmt *format;
2340        struct v4l2_rect *crop;
2341
2342        format = __ccdc_get_format(ccdc, cfg, fmt->pad, fmt->which);
2343        if (format == NULL)
2344                return -EINVAL;
2345
2346        ccdc_try_format(ccdc, cfg, fmt->pad, &fmt->format, fmt->which);
2347        *format = fmt->format;
2348
2349        /* Propagate the format from sink to source */
2350        if (fmt->pad == CCDC_PAD_SINK) {
2351                /* Reset the crop rectangle. */
2352                crop = __ccdc_get_crop(ccdc, cfg, fmt->which);
2353                crop->left = 0;
2354                crop->top = 0;
2355                crop->width = fmt->format.width;
2356                crop->height = fmt->format.height;
2357
2358                ccdc_try_crop(ccdc, &fmt->format, crop);
2359
2360                /* Update the source formats. */
2361                format = __ccdc_get_format(ccdc, cfg, CCDC_PAD_SOURCE_OF,
2362                                           fmt->which);
2363                *format = fmt->format;
2364                ccdc_try_format(ccdc, cfg, CCDC_PAD_SOURCE_OF, format,
2365                                fmt->which);
2366
2367                format = __ccdc_get_format(ccdc, cfg, CCDC_PAD_SOURCE_VP,
2368                                           fmt->which);
2369                *format = fmt->format;
2370                ccdc_try_format(ccdc, cfg, CCDC_PAD_SOURCE_VP, format,
2371                                fmt->which);
2372        }
2373
2374        return 0;
2375}
2376
2377/*
2378 * Decide whether desired output pixel code can be obtained with
2379 * the lane shifter by shifting the input pixel code.
2380 * @in: input pixelcode to shifter
2381 * @out: output pixelcode from shifter
2382 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
2383 *
2384 * return true if the combination is possible
2385 * return false otherwise
2386 */
2387static bool ccdc_is_shiftable(u32 in, u32 out, unsigned int additional_shift)
2388{
2389        const struct isp_format_info *in_info, *out_info;
2390
2391        if (in == out)
2392                return true;
2393
2394        in_info = omap3isp_video_format_info(in);
2395        out_info = omap3isp_video_format_info(out);
2396
2397        if ((in_info->flavor == 0) || (out_info->flavor == 0))
2398                return false;
2399
2400        if (in_info->flavor != out_info->flavor)
2401                return false;
2402
2403        return in_info->width - out_info->width + additional_shift <= 6;
2404}
2405
2406static int ccdc_link_validate(struct v4l2_subdev *sd,
2407                              struct media_link *link,
2408                              struct v4l2_subdev_format *source_fmt,
2409                              struct v4l2_subdev_format *sink_fmt)
2410{
2411        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2412        unsigned long parallel_shift;
2413
2414        /* Check if the two ends match */
2415        if (source_fmt->format.width != sink_fmt->format.width ||
2416            source_fmt->format.height != sink_fmt->format.height)
2417                return -EPIPE;
2418
2419        /* We've got a parallel sensor here. */
2420        if (ccdc->input == CCDC_INPUT_PARALLEL) {
2421                struct isp_parallel_cfg *parcfg =
2422                        &((struct isp_bus_cfg *)
2423                          media_entity_to_v4l2_subdev(link->source->entity)
2424                          ->host_priv)->bus.parallel;
2425                parallel_shift = parcfg->data_lane_shift;
2426        } else {
2427                parallel_shift = 0;
2428        }
2429
2430        /* Lane shifter may be used to drop bits on CCDC sink pad */
2431        if (!ccdc_is_shiftable(source_fmt->format.code,
2432                               sink_fmt->format.code, parallel_shift))
2433                return -EPIPE;
2434
2435        return 0;
2436}
2437
2438/*
2439 * ccdc_init_formats - Initialize formats on all pads
2440 * @sd: ISP CCDC V4L2 subdevice
2441 * @fh: V4L2 subdev file handle
2442 *
2443 * Initialize all pad formats with default values. If fh is not NULL, try
2444 * formats are initialized on the file handle. Otherwise active formats are
2445 * initialized on the device.
2446 */
2447static int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2448{
2449        struct v4l2_subdev_format format;
2450
2451        memset(&format, 0, sizeof(format));
2452        format.pad = CCDC_PAD_SINK;
2453        format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2454        format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
2455        format.format.width = 4096;
2456        format.format.height = 4096;
2457        ccdc_set_format(sd, fh ? fh->pad : NULL, &format);
2458
2459        return 0;
2460}
2461
2462/* V4L2 subdev core operations */
2463static const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
2464        .ioctl = ccdc_ioctl,
2465        .subscribe_event = ccdc_subscribe_event,
2466        .unsubscribe_event = ccdc_unsubscribe_event,
2467};
2468
2469/* V4L2 subdev video operations */
2470static const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
2471        .s_stream = ccdc_set_stream,
2472};
2473
2474/* V4L2 subdev pad operations */
2475static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
2476        .enum_mbus_code = ccdc_enum_mbus_code,
2477        .enum_frame_size = ccdc_enum_frame_size,
2478        .get_fmt = ccdc_get_format,
2479        .set_fmt = ccdc_set_format,
2480        .get_selection = ccdc_get_selection,
2481        .set_selection = ccdc_set_selection,
2482        .link_validate = ccdc_link_validate,
2483};
2484
2485/* V4L2 subdev operations */
2486static const struct v4l2_subdev_ops ccdc_v4l2_ops = {
2487        .core = &ccdc_v4l2_core_ops,
2488        .video = &ccdc_v4l2_video_ops,
2489        .pad = &ccdc_v4l2_pad_ops,
2490};
2491
2492/* V4L2 subdev internal operations */
2493static const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
2494        .open = ccdc_init_formats,
2495};
2496
2497/* -----------------------------------------------------------------------------
2498 * Media entity operations
2499 */
2500
2501/*
2502 * ccdc_link_setup - Setup CCDC connections
2503 * @entity: CCDC media entity
2504 * @local: Pad at the local end of the link
2505 * @remote: Pad at the remote end of the link
2506 * @flags: Link flags
2507 *
2508 * return -EINVAL or zero on success
2509 */
2510static int ccdc_link_setup(struct media_entity *entity,
2511                           const struct media_pad *local,
2512                           const struct media_pad *remote, u32 flags)
2513{
2514        struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2515        struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2516        struct isp_device *isp = to_isp_device(ccdc);
2517        unsigned int index = local->index;
2518
2519        /* FIXME: this is actually a hack! */
2520        if (is_media_entity_v4l2_subdev(remote->entity))
2521                index |= 2 << 16;
2522
2523        switch (index) {
2524        case CCDC_PAD_SINK | 2 << 16:
2525                /* Read from the sensor (parallel interface), CCP2, CSI2a or
2526                 * CSI2c.
2527                 */
2528                if (!(flags & MEDIA_LNK_FL_ENABLED)) {
2529                        ccdc->input = CCDC_INPUT_NONE;
2530                        break;
2531                }
2532
2533                if (ccdc->input != CCDC_INPUT_NONE)
2534                        return -EBUSY;
2535
2536                if (remote->entity == &isp->isp_ccp2.subdev.entity)
2537                        ccdc->input = CCDC_INPUT_CCP2B;
2538                else if (remote->entity == &isp->isp_csi2a.subdev.entity)
2539                        ccdc->input = CCDC_INPUT_CSI2A;
2540                else if (remote->entity == &isp->isp_csi2c.subdev.entity)
2541                        ccdc->input = CCDC_INPUT_CSI2C;
2542                else
2543                        ccdc->input = CCDC_INPUT_PARALLEL;
2544
2545                break;
2546
2547        /*
2548         * The ISP core doesn't support pipelines with multiple video outputs.
2549         * Revisit this when it will be implemented, and return -EBUSY for now.
2550         */
2551
2552        case CCDC_PAD_SOURCE_VP | 2 << 16:
2553                /* Write to preview engine, histogram and H3A. When none of
2554                 * those links are active, the video port can be disabled.
2555                 */
2556                if (flags & MEDIA_LNK_FL_ENABLED) {
2557                        if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
2558                                return -EBUSY;
2559                        ccdc->output |= CCDC_OUTPUT_PREVIEW;
2560                } else {
2561                        ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
2562                }
2563                break;
2564
2565        case CCDC_PAD_SOURCE_OF:
2566                /* Write to memory */
2567                if (flags & MEDIA_LNK_FL_ENABLED) {
2568                        if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
2569                                return -EBUSY;
2570                        ccdc->output |= CCDC_OUTPUT_MEMORY;
2571                } else {
2572                        ccdc->output &= ~CCDC_OUTPUT_MEMORY;
2573                }
2574                break;
2575
2576        case CCDC_PAD_SOURCE_OF | 2 << 16:
2577                /* Write to resizer */
2578                if (flags & MEDIA_LNK_FL_ENABLED) {
2579                        if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
2580                                return -EBUSY;
2581                        ccdc->output |= CCDC_OUTPUT_RESIZER;
2582                } else {
2583                        ccdc->output &= ~CCDC_OUTPUT_RESIZER;
2584                }
2585                break;
2586
2587        default:
2588                return -EINVAL;
2589        }
2590
2591        return 0;
2592}
2593
2594/* media operations */
2595static const struct media_entity_operations ccdc_media_ops = {
2596        .link_setup = ccdc_link_setup,
2597        .link_validate = v4l2_subdev_link_validate,
2598};
2599
2600void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
2601{
2602        v4l2_device_unregister_subdev(&ccdc->subdev);
2603        omap3isp_video_unregister(&ccdc->video_out);
2604}
2605
2606int omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
2607        struct v4l2_device *vdev)
2608{
2609        int ret;
2610
2611        /* Register the subdev and video node. */
2612        ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
2613        if (ret < 0)
2614                goto error;
2615
2616        ret = omap3isp_video_register(&ccdc->video_out, vdev);
2617        if (ret < 0)
2618                goto error;
2619
2620        return 0;
2621
2622error:
2623        omap3isp_ccdc_unregister_entities(ccdc);
2624        return ret;
2625}
2626
2627/* -----------------------------------------------------------------------------
2628 * ISP CCDC initialisation and cleanup
2629 */
2630
2631/*
2632 * ccdc_init_entities - Initialize V4L2 subdev and media entity
2633 * @ccdc: ISP CCDC module
2634 *
2635 * Return 0 on success and a negative error code on failure.
2636 */
2637static int ccdc_init_entities(struct isp_ccdc_device *ccdc)
2638{
2639        struct v4l2_subdev *sd = &ccdc->subdev;
2640        struct media_pad *pads = ccdc->pads;
2641        struct media_entity *me = &sd->entity;
2642        int ret;
2643
2644        ccdc->input = CCDC_INPUT_NONE;
2645
2646        v4l2_subdev_init(sd, &ccdc_v4l2_ops);
2647        sd->internal_ops = &ccdc_v4l2_internal_ops;
2648        strlcpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
2649        sd->grp_id = 1 << 16;   /* group ID for isp subdevs */
2650        v4l2_set_subdevdata(sd, ccdc);
2651        sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
2652
2653        pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK
2654                                    | MEDIA_PAD_FL_MUST_CONNECT;
2655        pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
2656        pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
2657
2658        me->ops = &ccdc_media_ops;
2659        ret = media_entity_pads_init(me, CCDC_PADS_NUM, pads);
2660        if (ret < 0)
2661                return ret;
2662
2663        ccdc_init_formats(sd, NULL);
2664
2665        ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2666        ccdc->video_out.ops = &ccdc_video_ops;
2667        ccdc->video_out.isp = to_isp_device(ccdc);
2668        ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
2669        ccdc->video_out.bpl_alignment = 32;
2670
2671        ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
2672        if (ret < 0)
2673                goto error;
2674
2675        return 0;
2676
2677error:
2678        media_entity_cleanup(me);
2679        return ret;
2680}
2681
2682/*
2683 * omap3isp_ccdc_init - CCDC module initialization.
2684 * @isp: Device pointer specific to the OMAP3 ISP.
2685 *
2686 * TODO: Get the initialisation values from platform data.
2687 *
2688 * Return 0 on success or a negative error code otherwise.
2689 */
2690int omap3isp_ccdc_init(struct isp_device *isp)
2691{
2692        struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2693        int ret;
2694
2695        spin_lock_init(&ccdc->lock);
2696        init_waitqueue_head(&ccdc->wait);
2697        mutex_init(&ccdc->ioctl_lock);
2698
2699        ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
2700
2701        INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
2702        ccdc->lsc.state = LSC_STATE_STOPPED;
2703        INIT_LIST_HEAD(&ccdc->lsc.free_queue);
2704        spin_lock_init(&ccdc->lsc.req_lock);
2705
2706        ccdc->clamp.oblen = 0;
2707        ccdc->clamp.dcsubval = 0;
2708
2709        ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
2710        ccdc_apply_controls(ccdc);
2711
2712        ret = ccdc_init_entities(ccdc);
2713        if (ret < 0) {
2714                mutex_destroy(&ccdc->ioctl_lock);
2715                return ret;
2716        }
2717
2718        return 0;
2719}
2720
2721/*
2722 * omap3isp_ccdc_cleanup - CCDC module cleanup.
2723 * @isp: Device pointer specific to the OMAP3 ISP.
2724 */
2725void omap3isp_ccdc_cleanup(struct isp_device *isp)
2726{
2727        struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2728
2729        omap3isp_video_cleanup(&ccdc->video_out);
2730        media_entity_cleanup(&ccdc->subdev.entity);
2731
2732        /* Free LSC requests. As the CCDC is stopped there's no active request,
2733         * so only the pending request and the free queue need to be handled.
2734         */
2735        ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
2736        cancel_work_sync(&ccdc->lsc.table_work);
2737        ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
2738
2739        if (ccdc->fpc.addr != NULL)
2740                dma_free_coherent(isp->dev, ccdc->fpc.fpnum * 4, ccdc->fpc.addr,
2741                                  ccdc->fpc.dma);
2742
2743        mutex_destroy(&ccdc->ioctl_lock);
2744}
2745