162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * ispccdc.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * TI OMAP3 ISP - CCDC module
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2009-2010 Nokia Corporation
862306a36Sopenharmony_ci * Copyright (C) 2009 Texas Instruments, Inc.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
1162306a36Sopenharmony_ci *	     Sakari Ailus <sakari.ailus@iki.fi>
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/uaccess.h>
1662306a36Sopenharmony_ci#include <linux/delay.h>
1762306a36Sopenharmony_ci#include <linux/device.h>
1862306a36Sopenharmony_ci#include <linux/dma-mapping.h>
1962306a36Sopenharmony_ci#include <linux/mm.h>
2062306a36Sopenharmony_ci#include <linux/sched.h>
2162306a36Sopenharmony_ci#include <linux/slab.h>
2262306a36Sopenharmony_ci#include <media/v4l2-event.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "isp.h"
2562306a36Sopenharmony_ci#include "ispreg.h"
2662306a36Sopenharmony_ci#include "ispccdc.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define CCDC_MIN_WIDTH		32
2962306a36Sopenharmony_ci#define CCDC_MIN_HEIGHT		32
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic struct v4l2_mbus_framefmt *
3262306a36Sopenharmony_ci__ccdc_get_format(struct isp_ccdc_device *ccdc,
3362306a36Sopenharmony_ci		  struct v4l2_subdev_state *sd_state,
3462306a36Sopenharmony_ci		  unsigned int pad, enum v4l2_subdev_format_whence which);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic const unsigned int ccdc_fmts[] = {
3762306a36Sopenharmony_ci	MEDIA_BUS_FMT_Y8_1X8,
3862306a36Sopenharmony_ci	MEDIA_BUS_FMT_Y10_1X10,
3962306a36Sopenharmony_ci	MEDIA_BUS_FMT_Y12_1X12,
4062306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGRBG8_1X8,
4162306a36Sopenharmony_ci	MEDIA_BUS_FMT_SRGGB8_1X8,
4262306a36Sopenharmony_ci	MEDIA_BUS_FMT_SBGGR8_1X8,
4362306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGBRG8_1X8,
4462306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGRBG10_1X10,
4562306a36Sopenharmony_ci	MEDIA_BUS_FMT_SRGGB10_1X10,
4662306a36Sopenharmony_ci	MEDIA_BUS_FMT_SBGGR10_1X10,
4762306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGBRG10_1X10,
4862306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGRBG12_1X12,
4962306a36Sopenharmony_ci	MEDIA_BUS_FMT_SRGGB12_1X12,
5062306a36Sopenharmony_ci	MEDIA_BUS_FMT_SBGGR12_1X12,
5162306a36Sopenharmony_ci	MEDIA_BUS_FMT_SGBRG12_1X12,
5262306a36Sopenharmony_ci	MEDIA_BUS_FMT_YUYV8_2X8,
5362306a36Sopenharmony_ci	MEDIA_BUS_FMT_UYVY8_2X8,
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * ccdc_print_status - Print current CCDC Module register values.
5862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * Also prints other debug information stored in the CCDC module.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ci#define CCDC_PRINT_REGISTER(isp, name)\
6362306a36Sopenharmony_ci	dev_dbg(isp->dev, "###CCDC " #name "=0x%08x\n", \
6462306a36Sopenharmony_ci		isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_##name))
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic void ccdc_print_status(struct isp_ccdc_device *ccdc)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	dev_dbg(isp->dev, "-------------CCDC Register dump-------------\n");
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PCR);
7362306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, SYN_MODE);
7462306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, HD_VD_WID);
7562306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PIX_LINES);
7662306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, HORZ_INFO);
7762306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, VERT_START);
7862306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, VERT_LINES);
7962306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, CULLING);
8062306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, HSIZE_OFF);
8162306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, SDOFST);
8262306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, SDR_ADDR);
8362306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, CLAMP);
8462306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, DCSUB);
8562306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, COLPTN);
8662306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, BLKCMP);
8762306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, FPC);
8862306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, FPC_ADDR);
8962306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, VDINT);
9062306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, ALAW);
9162306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, REC656IF);
9262306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, CFG);
9362306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, FMTCFG);
9462306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, FMT_HORZ);
9562306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, FMT_VERT);
9662306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PRGEVEN0);
9762306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PRGEVEN1);
9862306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PRGODD0);
9962306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, PRGODD1);
10062306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, VP_OUT);
10162306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, LSC_CONFIG);
10262306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, LSC_INITIAL);
10362306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, LSC_TABLE_BASE);
10462306a36Sopenharmony_ci	CCDC_PRINT_REGISTER(isp, LSC_TABLE_OFFSET);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	dev_dbg(isp->dev, "--------------------------------------------\n");
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/*
11062306a36Sopenharmony_ci * omap3isp_ccdc_busy - Get busy state of the CCDC.
11162306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_ciint omap3isp_ccdc_busy(struct isp_ccdc_device *ccdc)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR) &
11862306a36Sopenharmony_ci		ISPCCDC_PCR_BUSY;
11962306a36Sopenharmony_ci}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
12262306a36Sopenharmony_ci * Lens Shading Compensation
12362306a36Sopenharmony_ci */
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * ccdc_lsc_validate_config - Check that LSC configuration is valid.
12762306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
12862306a36Sopenharmony_ci * @lsc_cfg: the LSC configuration to check.
12962306a36Sopenharmony_ci *
13062306a36Sopenharmony_ci * Returns 0 if the LSC configuration is valid, or -EINVAL if invalid.
13162306a36Sopenharmony_ci */
13262306a36Sopenharmony_cistatic int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
13362306a36Sopenharmony_ci				    struct omap3isp_ccdc_lsc_config *lsc_cfg)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
13662306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
13762306a36Sopenharmony_ci	unsigned int paxel_width, paxel_height;
13862306a36Sopenharmony_ci	unsigned int paxel_shift_x, paxel_shift_y;
13962306a36Sopenharmony_ci	unsigned int min_width, min_height, min_size;
14062306a36Sopenharmony_ci	unsigned int input_width, input_height;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	paxel_shift_x = lsc_cfg->gain_mode_m;
14362306a36Sopenharmony_ci	paxel_shift_y = lsc_cfg->gain_mode_n;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	if ((paxel_shift_x < 2) || (paxel_shift_x > 6) ||
14662306a36Sopenharmony_ci	    (paxel_shift_y < 2) || (paxel_shift_y > 6)) {
14762306a36Sopenharmony_ci		dev_dbg(isp->dev, "CCDC: LSC: Invalid paxel size\n");
14862306a36Sopenharmony_ci		return -EINVAL;
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	if (lsc_cfg->offset & 3) {
15262306a36Sopenharmony_ci		dev_dbg(isp->dev,
15362306a36Sopenharmony_ci			"CCDC: LSC: Offset must be a multiple of 4\n");
15462306a36Sopenharmony_ci		return -EINVAL;
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	if ((lsc_cfg->initial_x & 1) || (lsc_cfg->initial_y & 1)) {
15862306a36Sopenharmony_ci		dev_dbg(isp->dev, "CCDC: LSC: initial_x and y must be even\n");
15962306a36Sopenharmony_ci		return -EINVAL;
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	format = __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
16362306a36Sopenharmony_ci				   V4L2_SUBDEV_FORMAT_ACTIVE);
16462306a36Sopenharmony_ci	input_width = format->width;
16562306a36Sopenharmony_ci	input_height = format->height;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	/* Calculate minimum bytesize for validation */
16862306a36Sopenharmony_ci	paxel_width = 1 << paxel_shift_x;
16962306a36Sopenharmony_ci	min_width = ((input_width + lsc_cfg->initial_x + paxel_width - 1)
17062306a36Sopenharmony_ci		     >> paxel_shift_x) + 1;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	paxel_height = 1 << paxel_shift_y;
17362306a36Sopenharmony_ci	min_height = ((input_height + lsc_cfg->initial_y + paxel_height - 1)
17462306a36Sopenharmony_ci		     >> paxel_shift_y) + 1;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	min_size = 4 * min_width * min_height;
17762306a36Sopenharmony_ci	if (min_size > lsc_cfg->size) {
17862306a36Sopenharmony_ci		dev_dbg(isp->dev, "CCDC: LSC: too small table\n");
17962306a36Sopenharmony_ci		return -EINVAL;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci	if (lsc_cfg->offset < (min_width * 4)) {
18262306a36Sopenharmony_ci		dev_dbg(isp->dev, "CCDC: LSC: Offset is too small\n");
18362306a36Sopenharmony_ci		return -EINVAL;
18462306a36Sopenharmony_ci	}
18562306a36Sopenharmony_ci	if ((lsc_cfg->size / lsc_cfg->offset) < min_height) {
18662306a36Sopenharmony_ci		dev_dbg(isp->dev, "CCDC: LSC: Wrong size/offset combination\n");
18762306a36Sopenharmony_ci		return -EINVAL;
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci	return 0;
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci/*
19362306a36Sopenharmony_ci * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
19462306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
19562306a36Sopenharmony_ci */
19662306a36Sopenharmony_cistatic void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc,
19762306a36Sopenharmony_ci				   dma_addr_t addr)
19862306a36Sopenharmony_ci{
19962306a36Sopenharmony_ci	isp_reg_writel(to_isp_device(ccdc), addr,
20062306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci/*
20462306a36Sopenharmony_ci * ccdc_lsc_setup_regs - Configures the lens shading compensation module
20562306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
20662306a36Sopenharmony_ci */
20762306a36Sopenharmony_cistatic void ccdc_lsc_setup_regs(struct isp_ccdc_device *ccdc,
20862306a36Sopenharmony_ci				struct omap3isp_ccdc_lsc_config *cfg)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
21162306a36Sopenharmony_ci	int reg;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	isp_reg_writel(isp, cfg->offset, OMAP3_ISP_IOMEM_CCDC,
21462306a36Sopenharmony_ci		       ISPCCDC_LSC_TABLE_OFFSET);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	reg = 0;
21762306a36Sopenharmony_ci	reg |= cfg->gain_mode_n << ISPCCDC_LSC_GAIN_MODE_N_SHIFT;
21862306a36Sopenharmony_ci	reg |= cfg->gain_mode_m << ISPCCDC_LSC_GAIN_MODE_M_SHIFT;
21962306a36Sopenharmony_ci	reg |= cfg->gain_format << ISPCCDC_LSC_GAIN_FORMAT_SHIFT;
22062306a36Sopenharmony_ci	isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	reg = 0;
22362306a36Sopenharmony_ci	reg &= ~ISPCCDC_LSC_INITIAL_X_MASK;
22462306a36Sopenharmony_ci	reg |= cfg->initial_x << ISPCCDC_LSC_INITIAL_X_SHIFT;
22562306a36Sopenharmony_ci	reg &= ~ISPCCDC_LSC_INITIAL_Y_MASK;
22662306a36Sopenharmony_ci	reg |= cfg->initial_y << ISPCCDC_LSC_INITIAL_Y_SHIFT;
22762306a36Sopenharmony_ci	isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC,
22862306a36Sopenharmony_ci		       ISPCCDC_LSC_INITIAL);
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_cistatic int ccdc_lsc_wait_prefetch(struct isp_ccdc_device *ccdc)
23262306a36Sopenharmony_ci{
23362306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
23462306a36Sopenharmony_ci	unsigned int wait;
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
23762306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* timeout 1 ms */
24062306a36Sopenharmony_ci	for (wait = 0; wait < 1000; wait++) {
24162306a36Sopenharmony_ci		if (isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS) &
24262306a36Sopenharmony_ci				  IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ) {
24362306a36Sopenharmony_ci			isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
24462306a36Sopenharmony_ci				       OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
24562306a36Sopenharmony_ci			return 0;
24662306a36Sopenharmony_ci		}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci		rmb();
24962306a36Sopenharmony_ci		udelay(1);
25062306a36Sopenharmony_ci	}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	return -ETIMEDOUT;
25362306a36Sopenharmony_ci}
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci/*
25662306a36Sopenharmony_ci * __ccdc_lsc_enable - Enables/Disables the Lens Shading Compensation module.
25762306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
25862306a36Sopenharmony_ci * @enable: 0 Disables LSC, 1 Enables LSC.
25962306a36Sopenharmony_ci */
26062306a36Sopenharmony_cistatic int __ccdc_lsc_enable(struct isp_ccdc_device *ccdc, int enable)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
26362306a36Sopenharmony_ci	const struct v4l2_mbus_framefmt *format =
26462306a36Sopenharmony_ci		__ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
26562306a36Sopenharmony_ci				  V4L2_SUBDEV_FORMAT_ACTIVE);
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	if ((format->code != MEDIA_BUS_FMT_SGRBG10_1X10) &&
26862306a36Sopenharmony_ci	    (format->code != MEDIA_BUS_FMT_SRGGB10_1X10) &&
26962306a36Sopenharmony_ci	    (format->code != MEDIA_BUS_FMT_SBGGR10_1X10) &&
27062306a36Sopenharmony_ci	    (format->code != MEDIA_BUS_FMT_SGBRG10_1X10))
27162306a36Sopenharmony_ci		return -EINVAL;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	if (enable)
27462306a36Sopenharmony_ci		omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_LSC_READ);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
27762306a36Sopenharmony_ci			ISPCCDC_LSC_ENABLE, enable ? ISPCCDC_LSC_ENABLE : 0);
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	if (enable) {
28062306a36Sopenharmony_ci		if (ccdc_lsc_wait_prefetch(ccdc) < 0) {
28162306a36Sopenharmony_ci			isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC,
28262306a36Sopenharmony_ci				    ISPCCDC_LSC_CONFIG, ISPCCDC_LSC_ENABLE);
28362306a36Sopenharmony_ci			ccdc->lsc.state = LSC_STATE_STOPPED;
28462306a36Sopenharmony_ci			dev_warn(to_device(ccdc), "LSC prefetch timeout\n");
28562306a36Sopenharmony_ci			return -ETIMEDOUT;
28662306a36Sopenharmony_ci		}
28762306a36Sopenharmony_ci		ccdc->lsc.state = LSC_STATE_RUNNING;
28862306a36Sopenharmony_ci	} else {
28962306a36Sopenharmony_ci		ccdc->lsc.state = LSC_STATE_STOPPING;
29062306a36Sopenharmony_ci	}
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	return 0;
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic int ccdc_lsc_busy(struct isp_ccdc_device *ccdc)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG) &
30062306a36Sopenharmony_ci			     ISPCCDC_LSC_BUSY;
30162306a36Sopenharmony_ci}
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci/*
30462306a36Sopenharmony_ci * __ccdc_lsc_configure - Apply a new configuration to the LSC engine
30562306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device
30662306a36Sopenharmony_ci * @req: New configuration request
30762306a36Sopenharmony_ci */
30862306a36Sopenharmony_cistatic int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
30962306a36Sopenharmony_ci				struct ispccdc_lsc_config_req *req)
31062306a36Sopenharmony_ci{
31162306a36Sopenharmony_ci	if (!req->enable)
31262306a36Sopenharmony_ci		return -EINVAL;
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	if (ccdc_lsc_validate_config(ccdc, &req->config) < 0) {
31562306a36Sopenharmony_ci		dev_dbg(to_device(ccdc), "Discard LSC configuration\n");
31662306a36Sopenharmony_ci		return -EINVAL;
31762306a36Sopenharmony_ci	}
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	if (ccdc_lsc_busy(ccdc))
32062306a36Sopenharmony_ci		return -EBUSY;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	ccdc_lsc_setup_regs(ccdc, &req->config);
32362306a36Sopenharmony_ci	ccdc_lsc_program_table(ccdc, req->table.dma);
32462306a36Sopenharmony_ci	return 0;
32562306a36Sopenharmony_ci}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci/*
32862306a36Sopenharmony_ci * ccdc_lsc_error_handler - Handle LSC prefetch error scenario.
32962306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * Disables LSC, and defers enablement to shadow registers update time.
33262306a36Sopenharmony_ci */
33362306a36Sopenharmony_cistatic void ccdc_lsc_error_handler(struct isp_ccdc_device *ccdc)
33462306a36Sopenharmony_ci{
33562306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
33662306a36Sopenharmony_ci	/*
33762306a36Sopenharmony_ci	 * From OMAP3 TRM: When this event is pending, the module
33862306a36Sopenharmony_ci	 * goes into transparent mode (output =input). Normal
33962306a36Sopenharmony_ci	 * operation can be resumed at the start of the next frame
34062306a36Sopenharmony_ci	 * after:
34162306a36Sopenharmony_ci	 *  1) Clearing this event
34262306a36Sopenharmony_ci	 *  2) Disabling the LSC module
34362306a36Sopenharmony_ci	 *  3) Enabling it
34462306a36Sopenharmony_ci	 */
34562306a36Sopenharmony_ci	isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
34662306a36Sopenharmony_ci		    ISPCCDC_LSC_ENABLE);
34762306a36Sopenharmony_ci	ccdc->lsc.state = LSC_STATE_STOPPED;
34862306a36Sopenharmony_ci}
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_cistatic void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
35162306a36Sopenharmony_ci				  struct ispccdc_lsc_config_req *req)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	if (req == NULL)
35662306a36Sopenharmony_ci		return;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	if (req->table.addr) {
35962306a36Sopenharmony_ci		sg_free_table(&req->table.sgt);
36062306a36Sopenharmony_ci		dma_free_coherent(isp->dev, req->config.size, req->table.addr,
36162306a36Sopenharmony_ci				  req->table.dma);
36262306a36Sopenharmony_ci	}
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	kfree(req);
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_cistatic void ccdc_lsc_free_queue(struct isp_ccdc_device *ccdc,
36862306a36Sopenharmony_ci				struct list_head *queue)
36962306a36Sopenharmony_ci{
37062306a36Sopenharmony_ci	struct ispccdc_lsc_config_req *req, *n;
37162306a36Sopenharmony_ci	unsigned long flags;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
37462306a36Sopenharmony_ci	list_for_each_entry_safe(req, n, queue, list) {
37562306a36Sopenharmony_ci		list_del(&req->list);
37662306a36Sopenharmony_ci		spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
37762306a36Sopenharmony_ci		ccdc_lsc_free_request(ccdc, req);
37862306a36Sopenharmony_ci		spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
37962306a36Sopenharmony_ci	}
38062306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
38162306a36Sopenharmony_ci}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_cistatic void ccdc_lsc_free_table_work(struct work_struct *work)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc;
38662306a36Sopenharmony_ci	struct ispccdc_lsc *lsc;
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	lsc = container_of(work, struct ispccdc_lsc, table_work);
38962306a36Sopenharmony_ci	ccdc = container_of(lsc, struct isp_ccdc_device, lsc);
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	ccdc_lsc_free_queue(ccdc, &lsc->free_queue);
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci/*
39562306a36Sopenharmony_ci * ccdc_lsc_config - Configure the LSC module from a userspace request
39662306a36Sopenharmony_ci *
39762306a36Sopenharmony_ci * Store the request LSC configuration in the LSC engine request pointer. The
39862306a36Sopenharmony_ci * configuration will be applied to the hardware when the CCDC will be enabled,
39962306a36Sopenharmony_ci * or at the next LSC interrupt if the CCDC is already running.
40062306a36Sopenharmony_ci */
40162306a36Sopenharmony_cistatic int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
40262306a36Sopenharmony_ci			   struct omap3isp_ccdc_update_config *config)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
40562306a36Sopenharmony_ci	struct ispccdc_lsc_config_req *req;
40662306a36Sopenharmony_ci	unsigned long flags;
40762306a36Sopenharmony_ci	u16 update;
40862306a36Sopenharmony_ci	int ret;
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	update = config->update &
41162306a36Sopenharmony_ci		 (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC);
41262306a36Sopenharmony_ci	if (!update)
41362306a36Sopenharmony_ci		return 0;
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	if (update != (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC)) {
41662306a36Sopenharmony_ci		dev_dbg(to_device(ccdc),
41762306a36Sopenharmony_ci			"%s: Both LSC configuration and table need to be supplied\n",
41862306a36Sopenharmony_ci			__func__);
41962306a36Sopenharmony_ci		return -EINVAL;
42062306a36Sopenharmony_ci	}
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	req = kzalloc(sizeof(*req), GFP_KERNEL);
42362306a36Sopenharmony_ci	if (req == NULL)
42462306a36Sopenharmony_ci		return -ENOMEM;
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	if (config->flag & OMAP3ISP_CCDC_CONFIG_LSC) {
42762306a36Sopenharmony_ci		if (copy_from_user(&req->config, config->lsc_cfg,
42862306a36Sopenharmony_ci				   sizeof(req->config))) {
42962306a36Sopenharmony_ci			ret = -EFAULT;
43062306a36Sopenharmony_ci			goto done;
43162306a36Sopenharmony_ci		}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci		req->enable = 1;
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci		req->table.addr = dma_alloc_coherent(isp->dev, req->config.size,
43662306a36Sopenharmony_ci						     &req->table.dma,
43762306a36Sopenharmony_ci						     GFP_KERNEL);
43862306a36Sopenharmony_ci		if (req->table.addr == NULL) {
43962306a36Sopenharmony_ci			ret = -ENOMEM;
44062306a36Sopenharmony_ci			goto done;
44162306a36Sopenharmony_ci		}
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci		ret = dma_get_sgtable(isp->dev, &req->table.sgt,
44462306a36Sopenharmony_ci				      req->table.addr, req->table.dma,
44562306a36Sopenharmony_ci				      req->config.size);
44662306a36Sopenharmony_ci		if (ret < 0)
44762306a36Sopenharmony_ci			goto done;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci		dma_sync_sg_for_cpu(isp->dev, req->table.sgt.sgl,
45062306a36Sopenharmony_ci				    req->table.sgt.nents, DMA_TO_DEVICE);
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci		if (copy_from_user(req->table.addr, config->lsc,
45362306a36Sopenharmony_ci				   req->config.size)) {
45462306a36Sopenharmony_ci			ret = -EFAULT;
45562306a36Sopenharmony_ci			goto done;
45662306a36Sopenharmony_ci		}
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci		dma_sync_sg_for_device(isp->dev, req->table.sgt.sgl,
45962306a36Sopenharmony_ci				       req->table.sgt.nents, DMA_TO_DEVICE);
46062306a36Sopenharmony_ci	}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
46362306a36Sopenharmony_ci	if (ccdc->lsc.request) {
46462306a36Sopenharmony_ci		list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
46562306a36Sopenharmony_ci		schedule_work(&ccdc->lsc.table_work);
46662306a36Sopenharmony_ci	}
46762306a36Sopenharmony_ci	ccdc->lsc.request = req;
46862306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci	ret = 0;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_cidone:
47362306a36Sopenharmony_ci	if (ret < 0)
47462306a36Sopenharmony_ci		ccdc_lsc_free_request(ccdc, req);
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	return ret;
47762306a36Sopenharmony_ci}
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_cistatic inline int ccdc_lsc_is_configured(struct isp_ccdc_device *ccdc)
48062306a36Sopenharmony_ci{
48162306a36Sopenharmony_ci	unsigned long flags;
48262306a36Sopenharmony_ci	int ret;
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
48562306a36Sopenharmony_ci	ret = ccdc->lsc.active != NULL;
48662306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	return ret;
48962306a36Sopenharmony_ci}
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_cistatic int ccdc_lsc_enable(struct isp_ccdc_device *ccdc)
49262306a36Sopenharmony_ci{
49362306a36Sopenharmony_ci	struct ispccdc_lsc *lsc = &ccdc->lsc;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	if (lsc->state != LSC_STATE_STOPPED)
49662306a36Sopenharmony_ci		return -EINVAL;
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	if (lsc->active) {
49962306a36Sopenharmony_ci		list_add_tail(&lsc->active->list, &lsc->free_queue);
50062306a36Sopenharmony_ci		lsc->active = NULL;
50162306a36Sopenharmony_ci	}
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	if (__ccdc_lsc_configure(ccdc, lsc->request) < 0) {
50462306a36Sopenharmony_ci		omap3isp_sbl_disable(to_isp_device(ccdc),
50562306a36Sopenharmony_ci				OMAP3_ISP_SBL_CCDC_LSC_READ);
50662306a36Sopenharmony_ci		list_add_tail(&lsc->request->list, &lsc->free_queue);
50762306a36Sopenharmony_ci		lsc->request = NULL;
50862306a36Sopenharmony_ci		goto done;
50962306a36Sopenharmony_ci	}
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	lsc->active = lsc->request;
51262306a36Sopenharmony_ci	lsc->request = NULL;
51362306a36Sopenharmony_ci	__ccdc_lsc_enable(ccdc, 1);
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_cidone:
51662306a36Sopenharmony_ci	if (!list_empty(&lsc->free_queue))
51762306a36Sopenharmony_ci		schedule_work(&lsc->table_work);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	return 0;
52062306a36Sopenharmony_ci}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
52362306a36Sopenharmony_ci * Parameters configuration
52462306a36Sopenharmony_ci */
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci/*
52762306a36Sopenharmony_ci * ccdc_configure_clamp - Configure optical-black or digital clamping
52862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
52962306a36Sopenharmony_ci *
53062306a36Sopenharmony_ci * The CCDC performs either optical-black or digital clamp. Configure and enable
53162306a36Sopenharmony_ci * the selected clamp method.
53262306a36Sopenharmony_ci */
53362306a36Sopenharmony_cistatic void ccdc_configure_clamp(struct isp_ccdc_device *ccdc)
53462306a36Sopenharmony_ci{
53562306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
53662306a36Sopenharmony_ci	u32 clamp;
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	if (ccdc->obclamp) {
53962306a36Sopenharmony_ci		clamp  = ccdc->clamp.obgain << ISPCCDC_CLAMP_OBGAIN_SHIFT;
54062306a36Sopenharmony_ci		clamp |= ccdc->clamp.oblen << ISPCCDC_CLAMP_OBSLEN_SHIFT;
54162306a36Sopenharmony_ci		clamp |= ccdc->clamp.oblines << ISPCCDC_CLAMP_OBSLN_SHIFT;
54262306a36Sopenharmony_ci		clamp |= ccdc->clamp.obstpixel << ISPCCDC_CLAMP_OBST_SHIFT;
54362306a36Sopenharmony_ci		isp_reg_writel(isp, clamp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP);
54462306a36Sopenharmony_ci	} else {
54562306a36Sopenharmony_ci		isp_reg_writel(isp, ccdc->clamp.dcsubval,
54662306a36Sopenharmony_ci			       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_DCSUB);
54762306a36Sopenharmony_ci	}
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP,
55062306a36Sopenharmony_ci			ISPCCDC_CLAMP_CLAMPEN,
55162306a36Sopenharmony_ci			ccdc->obclamp ? ISPCCDC_CLAMP_CLAMPEN : 0);
55262306a36Sopenharmony_ci}
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci/*
55562306a36Sopenharmony_ci * ccdc_configure_fpc - Configure Faulty Pixel Correction
55662306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
55762306a36Sopenharmony_ci */
55862306a36Sopenharmony_cistatic void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
55962306a36Sopenharmony_ci{
56062306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC, ISPCCDC_FPC_FPCEN);
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	if (!ccdc->fpc_en)
56562306a36Sopenharmony_ci		return;
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	isp_reg_writel(isp, ccdc->fpc.dma, OMAP3_ISP_IOMEM_CCDC,
56862306a36Sopenharmony_ci		       ISPCCDC_FPC_ADDR);
56962306a36Sopenharmony_ci	/* The FPNUM field must be set before enabling FPC. */
57062306a36Sopenharmony_ci	isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
57162306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
57262306a36Sopenharmony_ci	isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT) |
57362306a36Sopenharmony_ci		       ISPCCDC_FPC_FPCEN, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
57462306a36Sopenharmony_ci}
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci/*
57762306a36Sopenharmony_ci * ccdc_configure_black_comp - Configure Black Level Compensation.
57862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
57962306a36Sopenharmony_ci */
58062306a36Sopenharmony_cistatic void ccdc_configure_black_comp(struct isp_ccdc_device *ccdc)
58162306a36Sopenharmony_ci{
58262306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
58362306a36Sopenharmony_ci	u32 blcomp;
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	blcomp  = ccdc->blcomp.b_mg << ISPCCDC_BLKCMP_B_MG_SHIFT;
58662306a36Sopenharmony_ci	blcomp |= ccdc->blcomp.gb_g << ISPCCDC_BLKCMP_GB_G_SHIFT;
58762306a36Sopenharmony_ci	blcomp |= ccdc->blcomp.gr_cy << ISPCCDC_BLKCMP_GR_CY_SHIFT;
58862306a36Sopenharmony_ci	blcomp |= ccdc->blcomp.r_ye << ISPCCDC_BLKCMP_R_YE_SHIFT;
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci	isp_reg_writel(isp, blcomp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_BLKCMP);
59162306a36Sopenharmony_ci}
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci/*
59462306a36Sopenharmony_ci * ccdc_configure_lpf - Configure Low-Pass Filter (LPF).
59562306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
59662306a36Sopenharmony_ci */
59762306a36Sopenharmony_cistatic void ccdc_configure_lpf(struct isp_ccdc_device *ccdc)
59862306a36Sopenharmony_ci{
59962306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE,
60262306a36Sopenharmony_ci			ISPCCDC_SYN_MODE_LPF,
60362306a36Sopenharmony_ci			ccdc->lpf ? ISPCCDC_SYN_MODE_LPF : 0);
60462306a36Sopenharmony_ci}
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci/*
60762306a36Sopenharmony_ci * ccdc_configure_alaw - Configure A-law compression.
60862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
60962306a36Sopenharmony_ci */
61062306a36Sopenharmony_cistatic void ccdc_configure_alaw(struct isp_ccdc_device *ccdc)
61162306a36Sopenharmony_ci{
61262306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
61362306a36Sopenharmony_ci	const struct isp_format_info *info;
61462306a36Sopenharmony_ci	u32 alaw = 0;
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci	info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	switch (info->width) {
61962306a36Sopenharmony_ci	case 8:
62062306a36Sopenharmony_ci		return;
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	case 10:
62362306a36Sopenharmony_ci		alaw = ISPCCDC_ALAW_GWDI_9_0;
62462306a36Sopenharmony_ci		break;
62562306a36Sopenharmony_ci	case 11:
62662306a36Sopenharmony_ci		alaw = ISPCCDC_ALAW_GWDI_10_1;
62762306a36Sopenharmony_ci		break;
62862306a36Sopenharmony_ci	case 12:
62962306a36Sopenharmony_ci		alaw = ISPCCDC_ALAW_GWDI_11_2;
63062306a36Sopenharmony_ci		break;
63162306a36Sopenharmony_ci	case 13:
63262306a36Sopenharmony_ci		alaw = ISPCCDC_ALAW_GWDI_12_3;
63362306a36Sopenharmony_ci		break;
63462306a36Sopenharmony_ci	}
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	if (ccdc->alaw)
63762306a36Sopenharmony_ci		alaw |= ISPCCDC_ALAW_CCDTBL;
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci	isp_reg_writel(isp, alaw, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_ALAW);
64062306a36Sopenharmony_ci}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci/*
64362306a36Sopenharmony_ci * ccdc_config_imgattr - Configure sensor image specific attributes.
64462306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
64562306a36Sopenharmony_ci * @colptn: Color pattern of the sensor.
64662306a36Sopenharmony_ci */
64762306a36Sopenharmony_cistatic void ccdc_config_imgattr(struct isp_ccdc_device *ccdc, u32 colptn)
64862306a36Sopenharmony_ci{
64962306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	isp_reg_writel(isp, colptn, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_COLPTN);
65262306a36Sopenharmony_ci}
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci/*
65562306a36Sopenharmony_ci * ccdc_config - Set CCDC configuration from userspace
65662306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
65762306a36Sopenharmony_ci * @ccdc_struct: Structure containing CCDC configuration sent from userspace.
65862306a36Sopenharmony_ci *
65962306a36Sopenharmony_ci * Returns 0 if successful, -EINVAL if the pointer to the configuration
66062306a36Sopenharmony_ci * structure is null, or the copy_from_user function fails to copy user space
66162306a36Sopenharmony_ci * memory to kernel space memory.
66262306a36Sopenharmony_ci */
66362306a36Sopenharmony_cistatic int ccdc_config(struct isp_ccdc_device *ccdc,
66462306a36Sopenharmony_ci		       struct omap3isp_ccdc_update_config *ccdc_struct)
66562306a36Sopenharmony_ci{
66662306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
66762306a36Sopenharmony_ci	unsigned long flags;
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lock, flags);
67062306a36Sopenharmony_ci	ccdc->shadow_update = 1;
67162306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lock, flags);
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	if (OMAP3ISP_CCDC_ALAW & ccdc_struct->update) {
67462306a36Sopenharmony_ci		ccdc->alaw = !!(OMAP3ISP_CCDC_ALAW & ccdc_struct->flag);
67562306a36Sopenharmony_ci		ccdc->update |= OMAP3ISP_CCDC_ALAW;
67662306a36Sopenharmony_ci	}
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci	if (OMAP3ISP_CCDC_LPF & ccdc_struct->update) {
67962306a36Sopenharmony_ci		ccdc->lpf = !!(OMAP3ISP_CCDC_LPF & ccdc_struct->flag);
68062306a36Sopenharmony_ci		ccdc->update |= OMAP3ISP_CCDC_LPF;
68162306a36Sopenharmony_ci	}
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci	if (OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->update) {
68462306a36Sopenharmony_ci		if (copy_from_user(&ccdc->clamp, ccdc_struct->bclamp,
68562306a36Sopenharmony_ci				   sizeof(ccdc->clamp))) {
68662306a36Sopenharmony_ci			ccdc->shadow_update = 0;
68762306a36Sopenharmony_ci			return -EFAULT;
68862306a36Sopenharmony_ci		}
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci		ccdc->obclamp = !!(OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->flag);
69162306a36Sopenharmony_ci		ccdc->update |= OMAP3ISP_CCDC_BLCLAMP;
69262306a36Sopenharmony_ci	}
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci	if (OMAP3ISP_CCDC_BCOMP & ccdc_struct->update) {
69562306a36Sopenharmony_ci		if (copy_from_user(&ccdc->blcomp, ccdc_struct->blcomp,
69662306a36Sopenharmony_ci				   sizeof(ccdc->blcomp))) {
69762306a36Sopenharmony_ci			ccdc->shadow_update = 0;
69862306a36Sopenharmony_ci			return -EFAULT;
69962306a36Sopenharmony_ci		}
70062306a36Sopenharmony_ci
70162306a36Sopenharmony_ci		ccdc->update |= OMAP3ISP_CCDC_BCOMP;
70262306a36Sopenharmony_ci	}
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci	ccdc->shadow_update = 0;
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_ci	if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
70762306a36Sopenharmony_ci		struct omap3isp_ccdc_fpc fpc;
70862306a36Sopenharmony_ci		struct ispccdc_fpc fpc_old = { .addr = NULL, };
70962306a36Sopenharmony_ci		struct ispccdc_fpc fpc_new;
71062306a36Sopenharmony_ci		u32 size;
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ci		if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
71362306a36Sopenharmony_ci			return -EBUSY;
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci		ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci		if (ccdc->fpc_en) {
71862306a36Sopenharmony_ci			if (copy_from_user(&fpc, ccdc_struct->fpc, sizeof(fpc)))
71962306a36Sopenharmony_ci				return -EFAULT;
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci			size = fpc.fpnum * 4;
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci			/*
72462306a36Sopenharmony_ci			 * The table address must be 64-bytes aligned, which is
72562306a36Sopenharmony_ci			 * guaranteed by dma_alloc_coherent().
72662306a36Sopenharmony_ci			 */
72762306a36Sopenharmony_ci			fpc_new.fpnum = fpc.fpnum;
72862306a36Sopenharmony_ci			fpc_new.addr = dma_alloc_coherent(isp->dev, size,
72962306a36Sopenharmony_ci							  &fpc_new.dma,
73062306a36Sopenharmony_ci							  GFP_KERNEL);
73162306a36Sopenharmony_ci			if (fpc_new.addr == NULL)
73262306a36Sopenharmony_ci				return -ENOMEM;
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci			if (copy_from_user(fpc_new.addr,
73562306a36Sopenharmony_ci					   (__force void __user *)(long)fpc.fpcaddr,
73662306a36Sopenharmony_ci					   size)) {
73762306a36Sopenharmony_ci				dma_free_coherent(isp->dev, size, fpc_new.addr,
73862306a36Sopenharmony_ci						  fpc_new.dma);
73962306a36Sopenharmony_ci				return -EFAULT;
74062306a36Sopenharmony_ci			}
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci			fpc_old = ccdc->fpc;
74362306a36Sopenharmony_ci			ccdc->fpc = fpc_new;
74462306a36Sopenharmony_ci		}
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_ci		ccdc_configure_fpc(ccdc);
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci		if (fpc_old.addr != NULL)
74962306a36Sopenharmony_ci			dma_free_coherent(isp->dev, fpc_old.fpnum * 4,
75062306a36Sopenharmony_ci					  fpc_old.addr, fpc_old.dma);
75162306a36Sopenharmony_ci	}
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci	return ccdc_lsc_config(ccdc, ccdc_struct);
75462306a36Sopenharmony_ci}
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_cistatic void ccdc_apply_controls(struct isp_ccdc_device *ccdc)
75762306a36Sopenharmony_ci{
75862306a36Sopenharmony_ci	if (ccdc->update & OMAP3ISP_CCDC_ALAW) {
75962306a36Sopenharmony_ci		ccdc_configure_alaw(ccdc);
76062306a36Sopenharmony_ci		ccdc->update &= ~OMAP3ISP_CCDC_ALAW;
76162306a36Sopenharmony_ci	}
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci	if (ccdc->update & OMAP3ISP_CCDC_LPF) {
76462306a36Sopenharmony_ci		ccdc_configure_lpf(ccdc);
76562306a36Sopenharmony_ci		ccdc->update &= ~OMAP3ISP_CCDC_LPF;
76662306a36Sopenharmony_ci	}
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_ci	if (ccdc->update & OMAP3ISP_CCDC_BLCLAMP) {
76962306a36Sopenharmony_ci		ccdc_configure_clamp(ccdc);
77062306a36Sopenharmony_ci		ccdc->update &= ~OMAP3ISP_CCDC_BLCLAMP;
77162306a36Sopenharmony_ci	}
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	if (ccdc->update & OMAP3ISP_CCDC_BCOMP) {
77462306a36Sopenharmony_ci		ccdc_configure_black_comp(ccdc);
77562306a36Sopenharmony_ci		ccdc->update &= ~OMAP3ISP_CCDC_BCOMP;
77662306a36Sopenharmony_ci	}
77762306a36Sopenharmony_ci}
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci/*
78062306a36Sopenharmony_ci * omap3isp_ccdc_restore_context - Restore values of the CCDC module registers
78162306a36Sopenharmony_ci * @isp: Pointer to ISP device
78262306a36Sopenharmony_ci */
78362306a36Sopenharmony_civoid omap3isp_ccdc_restore_context(struct isp_device *isp)
78462306a36Sopenharmony_ci{
78562306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG, ISPCCDC_CFG_VDLC);
78862306a36Sopenharmony_ci
78962306a36Sopenharmony_ci	ccdc->update = OMAP3ISP_CCDC_ALAW | OMAP3ISP_CCDC_LPF
79062306a36Sopenharmony_ci		     | OMAP3ISP_CCDC_BLCLAMP | OMAP3ISP_CCDC_BCOMP;
79162306a36Sopenharmony_ci	ccdc_apply_controls(ccdc);
79262306a36Sopenharmony_ci	ccdc_configure_fpc(ccdc);
79362306a36Sopenharmony_ci}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
79662306a36Sopenharmony_ci * Format- and pipeline-related configuration helpers
79762306a36Sopenharmony_ci */
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci/*
80062306a36Sopenharmony_ci * ccdc_config_vp - Configure the Video Port.
80162306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
80262306a36Sopenharmony_ci */
80362306a36Sopenharmony_cistatic void ccdc_config_vp(struct isp_ccdc_device *ccdc)
80462306a36Sopenharmony_ci{
80562306a36Sopenharmony_ci	struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
80662306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
80762306a36Sopenharmony_ci	const struct isp_format_info *info;
80862306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
80962306a36Sopenharmony_ci	unsigned long l3_ick = pipe->l3_ick;
81062306a36Sopenharmony_ci	unsigned int max_div = isp->revision == ISP_REVISION_15_0 ? 64 : 8;
81162306a36Sopenharmony_ci	unsigned int div = 0;
81262306a36Sopenharmony_ci	u32 fmtcfg = ISPCCDC_FMTCFG_VPEN;
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ci	format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	if (!format->code) {
81762306a36Sopenharmony_ci		/* Disable the video port when the input format isn't supported.
81862306a36Sopenharmony_ci		 * This is indicated by a pixel code set to 0.
81962306a36Sopenharmony_ci		 */
82062306a36Sopenharmony_ci		isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
82162306a36Sopenharmony_ci		return;
82262306a36Sopenharmony_ci	}
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci	isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
82562306a36Sopenharmony_ci		       (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
82662306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
82762306a36Sopenharmony_ci	isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
82862306a36Sopenharmony_ci		       ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
82962306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
83262306a36Sopenharmony_ci		       (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
83362306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
83462306a36Sopenharmony_ci
83562306a36Sopenharmony_ci	info = omap3isp_video_format_info(ccdc->formats[CCDC_PAD_SINK].code);
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci	switch (info->width) {
83862306a36Sopenharmony_ci	case 8:
83962306a36Sopenharmony_ci	case 10:
84062306a36Sopenharmony_ci		fmtcfg |= ISPCCDC_FMTCFG_VPIN_9_0;
84162306a36Sopenharmony_ci		break;
84262306a36Sopenharmony_ci	case 11:
84362306a36Sopenharmony_ci		fmtcfg |= ISPCCDC_FMTCFG_VPIN_10_1;
84462306a36Sopenharmony_ci		break;
84562306a36Sopenharmony_ci	case 12:
84662306a36Sopenharmony_ci		fmtcfg |= ISPCCDC_FMTCFG_VPIN_11_2;
84762306a36Sopenharmony_ci		break;
84862306a36Sopenharmony_ci	case 13:
84962306a36Sopenharmony_ci		fmtcfg |= ISPCCDC_FMTCFG_VPIN_12_3;
85062306a36Sopenharmony_ci		break;
85162306a36Sopenharmony_ci	}
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci	if (pipe->input)
85462306a36Sopenharmony_ci		div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
85562306a36Sopenharmony_ci	else if (pipe->external_rate)
85662306a36Sopenharmony_ci		div = l3_ick / pipe->external_rate;
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci	div = clamp(div, 2U, max_div);
85962306a36Sopenharmony_ci	fmtcfg |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci	isp_reg_writel(isp, fmtcfg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
86262306a36Sopenharmony_ci}
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci/*
86562306a36Sopenharmony_ci * ccdc_config_outlineoffset - Configure memory saving output line offset
86662306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
86762306a36Sopenharmony_ci * @bpl: Number of bytes per line when stored in memory.
86862306a36Sopenharmony_ci * @field: Field order when storing interlaced formats in memory.
86962306a36Sopenharmony_ci *
87062306a36Sopenharmony_ci * Configure the offsets for the line output control:
87162306a36Sopenharmony_ci *
87262306a36Sopenharmony_ci * - The horizontal line offset is defined as the number of bytes between the
87362306a36Sopenharmony_ci *   start of two consecutive lines in memory. Set it to the given bytes per
87462306a36Sopenharmony_ci *   line value.
87562306a36Sopenharmony_ci *
87662306a36Sopenharmony_ci * - The field offset value is defined as the number of lines to offset the
87762306a36Sopenharmony_ci *   start of the field identified by FID = 1. Set it to one.
87862306a36Sopenharmony_ci *
87962306a36Sopenharmony_ci * - The line offset values are defined as the number of lines (as defined by
88062306a36Sopenharmony_ci *   the horizontal line offset) between the start of two consecutive lines for
88162306a36Sopenharmony_ci *   all combinations of odd/even lines in odd/even fields. When interleaving
88262306a36Sopenharmony_ci *   fields set them all to two lines, and to one line otherwise.
88362306a36Sopenharmony_ci */
88462306a36Sopenharmony_cistatic void ccdc_config_outlineoffset(struct isp_ccdc_device *ccdc,
88562306a36Sopenharmony_ci				      unsigned int bpl,
88662306a36Sopenharmony_ci				      enum v4l2_field field)
88762306a36Sopenharmony_ci{
88862306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
88962306a36Sopenharmony_ci	u32 sdofst = 0;
89062306a36Sopenharmony_ci
89162306a36Sopenharmony_ci	isp_reg_writel(isp, bpl & 0xffff, OMAP3_ISP_IOMEM_CCDC,
89262306a36Sopenharmony_ci		       ISPCCDC_HSIZE_OFF);
89362306a36Sopenharmony_ci
89462306a36Sopenharmony_ci	switch (field) {
89562306a36Sopenharmony_ci	case V4L2_FIELD_INTERLACED_TB:
89662306a36Sopenharmony_ci	case V4L2_FIELD_INTERLACED_BT:
89762306a36Sopenharmony_ci		/* When interleaving fields in memory offset field one by one
89862306a36Sopenharmony_ci		 * line and set the line offset to two lines.
89962306a36Sopenharmony_ci		 */
90062306a36Sopenharmony_ci		sdofst |= (1 << ISPCCDC_SDOFST_LOFST0_SHIFT)
90162306a36Sopenharmony_ci		       |  (1 << ISPCCDC_SDOFST_LOFST1_SHIFT)
90262306a36Sopenharmony_ci		       |  (1 << ISPCCDC_SDOFST_LOFST2_SHIFT)
90362306a36Sopenharmony_ci		       |  (1 << ISPCCDC_SDOFST_LOFST3_SHIFT);
90462306a36Sopenharmony_ci		break;
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	default:
90762306a36Sopenharmony_ci		/* In all other cases set the line offsets to one line. */
90862306a36Sopenharmony_ci		break;
90962306a36Sopenharmony_ci	}
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci	isp_reg_writel(isp, sdofst, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST);
91262306a36Sopenharmony_ci}
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci/*
91562306a36Sopenharmony_ci * ccdc_set_outaddr - Set memory address to save output image
91662306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
91762306a36Sopenharmony_ci * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
91862306a36Sopenharmony_ci *
91962306a36Sopenharmony_ci * Sets the memory address where the output will be saved.
92062306a36Sopenharmony_ci */
92162306a36Sopenharmony_cistatic void ccdc_set_outaddr(struct isp_ccdc_device *ccdc, u32 addr)
92262306a36Sopenharmony_ci{
92362306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci	isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDR_ADDR);
92662306a36Sopenharmony_ci}
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci/*
92962306a36Sopenharmony_ci * omap3isp_ccdc_max_rate - Calculate maximum input data rate based on the input
93062306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
93162306a36Sopenharmony_ci * @max_rate: Maximum calculated data rate.
93262306a36Sopenharmony_ci *
93362306a36Sopenharmony_ci * Returns in *max_rate less value between calculated and passed
93462306a36Sopenharmony_ci */
93562306a36Sopenharmony_civoid omap3isp_ccdc_max_rate(struct isp_ccdc_device *ccdc,
93662306a36Sopenharmony_ci			    unsigned int *max_rate)
93762306a36Sopenharmony_ci{
93862306a36Sopenharmony_ci	struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
93962306a36Sopenharmony_ci	unsigned int rate;
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_ci	if (pipe == NULL)
94262306a36Sopenharmony_ci		return;
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_ci	/*
94562306a36Sopenharmony_ci	 * TRM says that for parallel sensors the maximum data rate
94662306a36Sopenharmony_ci	 * should be 90% form L3/2 clock, otherwise just L3/2.
94762306a36Sopenharmony_ci	 */
94862306a36Sopenharmony_ci	if (ccdc->input == CCDC_INPUT_PARALLEL)
94962306a36Sopenharmony_ci		rate = pipe->l3_ick / 2 * 9 / 10;
95062306a36Sopenharmony_ci	else
95162306a36Sopenharmony_ci		rate = pipe->l3_ick / 2;
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci	*max_rate = min(*max_rate, rate);
95462306a36Sopenharmony_ci}
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci/*
95762306a36Sopenharmony_ci * ccdc_config_sync_if - Set CCDC sync interface configuration
95862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
95962306a36Sopenharmony_ci * @parcfg: Parallel interface platform data (may be NULL)
96062306a36Sopenharmony_ci * @data_size: Data size
96162306a36Sopenharmony_ci */
96262306a36Sopenharmony_cistatic void ccdc_config_sync_if(struct isp_ccdc_device *ccdc,
96362306a36Sopenharmony_ci				struct isp_parallel_cfg *parcfg,
96462306a36Sopenharmony_ci				unsigned int data_size)
96562306a36Sopenharmony_ci{
96662306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
96762306a36Sopenharmony_ci	const struct v4l2_mbus_framefmt *format;
96862306a36Sopenharmony_ci	u32 syn_mode = ISPCCDC_SYN_MODE_VDHDEN;
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci	format = &ccdc->formats[CCDC_PAD_SINK];
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci	if (format->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
97362306a36Sopenharmony_ci	    format->code == MEDIA_BUS_FMT_UYVY8_2X8) {
97462306a36Sopenharmony_ci		/* According to the OMAP3 TRM the input mode only affects SYNC
97562306a36Sopenharmony_ci		 * mode, enabling BT.656 mode should take precedence. However,
97662306a36Sopenharmony_ci		 * in practice setting the input mode to YCbCr data on 8 bits
97762306a36Sopenharmony_ci		 * seems to be required in BT.656 mode. In SYNC mode set it to
97862306a36Sopenharmony_ci		 * YCbCr on 16 bits as the bridge is enabled in that case.
97962306a36Sopenharmony_ci		 */
98062306a36Sopenharmony_ci		if (ccdc->bt656)
98162306a36Sopenharmony_ci			syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR8;
98262306a36Sopenharmony_ci		else
98362306a36Sopenharmony_ci			syn_mode |= ISPCCDC_SYN_MODE_INPMOD_YCBCR16;
98462306a36Sopenharmony_ci	}
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	switch (data_size) {
98762306a36Sopenharmony_ci	case 8:
98862306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
98962306a36Sopenharmony_ci		break;
99062306a36Sopenharmony_ci	case 10:
99162306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
99262306a36Sopenharmony_ci		break;
99362306a36Sopenharmony_ci	case 11:
99462306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
99562306a36Sopenharmony_ci		break;
99662306a36Sopenharmony_ci	case 12:
99762306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
99862306a36Sopenharmony_ci		break;
99962306a36Sopenharmony_ci	}
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci	if (parcfg && parcfg->data_pol)
100262306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	if (parcfg && parcfg->hs_pol)
100562306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci	/* The polarity of the vertical sync signal output by the BT.656
100862306a36Sopenharmony_ci	 * decoder is not documented and seems to be active low.
100962306a36Sopenharmony_ci	 */
101062306a36Sopenharmony_ci	if ((parcfg && parcfg->vs_pol) || ccdc->bt656)
101162306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	if (parcfg && parcfg->fld_pol)
101462306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_FLDPOL;
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ci	isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci	/* The CCDC_CFG.Y8POS bit is used in YCbCr8 input mode only. The
101962306a36Sopenharmony_ci	 * hardware seems to ignore it in all other input modes.
102062306a36Sopenharmony_ci	 */
102162306a36Sopenharmony_ci	if (format->code == MEDIA_BUS_FMT_UYVY8_2X8)
102262306a36Sopenharmony_ci		isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
102362306a36Sopenharmony_ci			    ISPCCDC_CFG_Y8POS);
102462306a36Sopenharmony_ci	else
102562306a36Sopenharmony_ci		isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
102662306a36Sopenharmony_ci			    ISPCCDC_CFG_Y8POS);
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci	/* Enable or disable BT.656 mode, including error correction for the
102962306a36Sopenharmony_ci	 * synchronization codes.
103062306a36Sopenharmony_ci	 */
103162306a36Sopenharmony_ci	if (ccdc->bt656)
103262306a36Sopenharmony_ci		isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
103362306a36Sopenharmony_ci			    ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
103462306a36Sopenharmony_ci	else
103562306a36Sopenharmony_ci		isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
103662306a36Sopenharmony_ci			    ISPCCDC_REC656IF_R656ON | ISPCCDC_REC656IF_ECCFVH);
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci}
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci/* CCDC formats descriptions */
104162306a36Sopenharmony_cistatic const u32 ccdc_sgrbg_pattern =
104262306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
104362306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
104462306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
104562306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
104662306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
104762306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
104862306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
104962306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
105062306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
105162306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
105262306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
105362306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
105462306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
105562306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
105662306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
105762306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_cistatic const u32 ccdc_srggb_pattern =
106062306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
106162306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
106262306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
106362306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
106462306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
106562306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
106662306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
106762306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
106862306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
106962306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
107062306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
107162306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
107262306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
107362306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
107462306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
107562306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_cistatic const u32 ccdc_sbggr_pattern =
107862306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
107962306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
108062306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
108162306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
108262306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
108362306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
108462306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
108562306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
108662306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
108762306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
108862306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
108962306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
109062306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
109162306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
109262306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
109362306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
109462306a36Sopenharmony_ci
109562306a36Sopenharmony_cistatic const u32 ccdc_sgbrg_pattern =
109662306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
109762306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
109862306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
109962306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
110062306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
110162306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
110262306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
110362306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
110462306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
110562306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
110662306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gb_G  << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
110762306a36Sopenharmony_ci	ISPCCDC_COLPTN_B_Mg  << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
110862306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
110962306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
111062306a36Sopenharmony_ci	ISPCCDC_COLPTN_R_Ye  << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
111162306a36Sopenharmony_ci	ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
111262306a36Sopenharmony_ci
111362306a36Sopenharmony_cistatic void ccdc_configure(struct isp_ccdc_device *ccdc)
111462306a36Sopenharmony_ci{
111562306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
111662306a36Sopenharmony_ci	struct isp_parallel_cfg *parcfg = NULL;
111762306a36Sopenharmony_ci	struct v4l2_subdev *sensor;
111862306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
111962306a36Sopenharmony_ci	const struct v4l2_rect *crop;
112062306a36Sopenharmony_ci	const struct isp_format_info *fmt_info;
112162306a36Sopenharmony_ci	struct v4l2_subdev_format fmt_src = {
112262306a36Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
112362306a36Sopenharmony_ci	};
112462306a36Sopenharmony_ci	unsigned int depth_out;
112562306a36Sopenharmony_ci	unsigned int depth_in = 0;
112662306a36Sopenharmony_ci	struct media_pad *pad;
112762306a36Sopenharmony_ci	unsigned long flags;
112862306a36Sopenharmony_ci	unsigned int bridge;
112962306a36Sopenharmony_ci	unsigned int shift;
113062306a36Sopenharmony_ci	unsigned int nph;
113162306a36Sopenharmony_ci	unsigned int sph;
113262306a36Sopenharmony_ci	u32 syn_mode;
113362306a36Sopenharmony_ci	u32 ccdc_pattern;
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci	ccdc->bt656 = false;
113662306a36Sopenharmony_ci	ccdc->fields = 0;
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci	pad = media_pad_remote_pad_first(&ccdc->pads[CCDC_PAD_SINK]);
113962306a36Sopenharmony_ci	sensor = media_entity_to_v4l2_subdev(pad->entity);
114062306a36Sopenharmony_ci	if (ccdc->input == CCDC_INPUT_PARALLEL) {
114162306a36Sopenharmony_ci		struct v4l2_subdev *sd =
114262306a36Sopenharmony_ci			to_isp_pipeline(&ccdc->subdev.entity)->external;
114362306a36Sopenharmony_ci		struct isp_bus_cfg *bus_cfg;
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci		bus_cfg = v4l2_subdev_to_bus_cfg(sd);
114662306a36Sopenharmony_ci		if (WARN_ON(!bus_cfg))
114762306a36Sopenharmony_ci			return;
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_ci		parcfg = &bus_cfg->bus.parallel;
115062306a36Sopenharmony_ci		ccdc->bt656 = parcfg->bt656;
115162306a36Sopenharmony_ci	}
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci	/* CCDC_PAD_SINK */
115462306a36Sopenharmony_ci	format = &ccdc->formats[CCDC_PAD_SINK];
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci	/* Compute the lane shifter shift value and enable the bridge when the
115762306a36Sopenharmony_ci	 * input format is a non-BT.656 YUV variant.
115862306a36Sopenharmony_ci	 */
115962306a36Sopenharmony_ci	fmt_src.pad = pad->index;
116062306a36Sopenharmony_ci	if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
116162306a36Sopenharmony_ci		fmt_info = omap3isp_video_format_info(fmt_src.format.code);
116262306a36Sopenharmony_ci		depth_in = fmt_info->width;
116362306a36Sopenharmony_ci	}
116462306a36Sopenharmony_ci
116562306a36Sopenharmony_ci	fmt_info = omap3isp_video_format_info(format->code);
116662306a36Sopenharmony_ci	depth_out = fmt_info->width;
116762306a36Sopenharmony_ci	shift = depth_in - depth_out;
116862306a36Sopenharmony_ci
116962306a36Sopenharmony_ci	if (ccdc->bt656)
117062306a36Sopenharmony_ci		bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
117162306a36Sopenharmony_ci	else if (fmt_info->code == MEDIA_BUS_FMT_YUYV8_2X8)
117262306a36Sopenharmony_ci		bridge = ISPCTRL_PAR_BRIDGE_LENDIAN;
117362306a36Sopenharmony_ci	else if (fmt_info->code == MEDIA_BUS_FMT_UYVY8_2X8)
117462306a36Sopenharmony_ci		bridge = ISPCTRL_PAR_BRIDGE_BENDIAN;
117562306a36Sopenharmony_ci	else
117662306a36Sopenharmony_ci		bridge = ISPCTRL_PAR_BRIDGE_DISABLE;
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci	omap3isp_configure_bridge(isp, ccdc->input, parcfg, shift, bridge);
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	/* Configure the sync interface. */
118162306a36Sopenharmony_ci	ccdc_config_sync_if(ccdc, parcfg, depth_out);
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci	syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci	/* Use the raw, unprocessed data when writing to memory. The H3A and
118662306a36Sopenharmony_ci	 * histogram modules are still fed with lens shading corrected data.
118762306a36Sopenharmony_ci	 */
118862306a36Sopenharmony_ci	syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci	if (ccdc->output & CCDC_OUTPUT_MEMORY)
119162306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_WEN;
119262306a36Sopenharmony_ci	else
119362306a36Sopenharmony_ci		syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
119462306a36Sopenharmony_ci
119562306a36Sopenharmony_ci	if (ccdc->output & CCDC_OUTPUT_RESIZER)
119662306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
119762306a36Sopenharmony_ci	else
119862306a36Sopenharmony_ci		syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
119962306a36Sopenharmony_ci
120062306a36Sopenharmony_ci	/* Mosaic filter */
120162306a36Sopenharmony_ci	switch (format->code) {
120262306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SRGGB10_1X10:
120362306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SRGGB12_1X12:
120462306a36Sopenharmony_ci		ccdc_pattern = ccdc_srggb_pattern;
120562306a36Sopenharmony_ci		break;
120662306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SBGGR10_1X10:
120762306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SBGGR12_1X12:
120862306a36Sopenharmony_ci		ccdc_pattern = ccdc_sbggr_pattern;
120962306a36Sopenharmony_ci		break;
121062306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SGBRG10_1X10:
121162306a36Sopenharmony_ci	case MEDIA_BUS_FMT_SGBRG12_1X12:
121262306a36Sopenharmony_ci		ccdc_pattern = ccdc_sgbrg_pattern;
121362306a36Sopenharmony_ci		break;
121462306a36Sopenharmony_ci	default:
121562306a36Sopenharmony_ci		/* Use GRBG */
121662306a36Sopenharmony_ci		ccdc_pattern = ccdc_sgrbg_pattern;
121762306a36Sopenharmony_ci		break;
121862306a36Sopenharmony_ci	}
121962306a36Sopenharmony_ci	ccdc_config_imgattr(ccdc, ccdc_pattern);
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	/* Generate VD0 on the last line of the image and VD1 on the
122262306a36Sopenharmony_ci	 * 2/3 height line.
122362306a36Sopenharmony_ci	 */
122462306a36Sopenharmony_ci	isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
122562306a36Sopenharmony_ci		       ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
122662306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	/* CCDC_PAD_SOURCE_OF */
122962306a36Sopenharmony_ci	format = &ccdc->formats[CCDC_PAD_SOURCE_OF];
123062306a36Sopenharmony_ci	crop = &ccdc->crop;
123162306a36Sopenharmony_ci
123262306a36Sopenharmony_ci	/* The horizontal coordinates are expressed in pixel clock cycles. We
123362306a36Sopenharmony_ci	 * need two cycles per pixel in BT.656 mode, and one cycle per pixel in
123462306a36Sopenharmony_ci	 * SYNC mode regardless of the format as the bridge is enabled for YUV
123562306a36Sopenharmony_ci	 * formats in that case.
123662306a36Sopenharmony_ci	 */
123762306a36Sopenharmony_ci	if (ccdc->bt656) {
123862306a36Sopenharmony_ci		sph = crop->left * 2;
123962306a36Sopenharmony_ci		nph = crop->width * 2 - 1;
124062306a36Sopenharmony_ci	} else {
124162306a36Sopenharmony_ci		sph = crop->left;
124262306a36Sopenharmony_ci		nph = crop->width - 1;
124362306a36Sopenharmony_ci	}
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci	isp_reg_writel(isp, (sph << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
124662306a36Sopenharmony_ci		       (nph << ISPCCDC_HORZ_INFO_NPH_SHIFT),
124762306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
124862306a36Sopenharmony_ci	isp_reg_writel(isp, (crop->top << ISPCCDC_VERT_START_SLV0_SHIFT) |
124962306a36Sopenharmony_ci		       (crop->top << ISPCCDC_VERT_START_SLV1_SHIFT),
125062306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
125162306a36Sopenharmony_ci	isp_reg_writel(isp, (crop->height - 1)
125262306a36Sopenharmony_ci			<< ISPCCDC_VERT_LINES_NLV_SHIFT,
125362306a36Sopenharmony_ci		       OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_ci	ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value,
125662306a36Sopenharmony_ci				  format->field);
125762306a36Sopenharmony_ci
125862306a36Sopenharmony_ci	/* When interleaving fields enable processing of the field input signal.
125962306a36Sopenharmony_ci	 * This will cause the line output control module to apply the field
126062306a36Sopenharmony_ci	 * offset to field 1.
126162306a36Sopenharmony_ci	 */
126262306a36Sopenharmony_ci	if (ccdc->formats[CCDC_PAD_SINK].field == V4L2_FIELD_ALTERNATE &&
126362306a36Sopenharmony_ci	    (format->field == V4L2_FIELD_INTERLACED_TB ||
126462306a36Sopenharmony_ci	     format->field == V4L2_FIELD_INTERLACED_BT))
126562306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_FLDMODE;
126662306a36Sopenharmony_ci
126762306a36Sopenharmony_ci	/* The CCDC outputs data in UYVY order by default. Swap bytes to get
126862306a36Sopenharmony_ci	 * YUYV.
126962306a36Sopenharmony_ci	 */
127062306a36Sopenharmony_ci	if (format->code == MEDIA_BUS_FMT_YUYV8_1X16)
127162306a36Sopenharmony_ci		isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
127262306a36Sopenharmony_ci			    ISPCCDC_CFG_BSWD);
127362306a36Sopenharmony_ci	else
127462306a36Sopenharmony_ci		isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
127562306a36Sopenharmony_ci			    ISPCCDC_CFG_BSWD);
127662306a36Sopenharmony_ci
127762306a36Sopenharmony_ci	/* Use PACK8 mode for 1byte per pixel formats. Check for BT.656 mode
127862306a36Sopenharmony_ci	 * explicitly as the driver reports 1X16 instead of 2X8 at the OF pad
127962306a36Sopenharmony_ci	 * for simplicity.
128062306a36Sopenharmony_ci	 */
128162306a36Sopenharmony_ci	if (omap3isp_video_format_info(format->code)->width <= 8 || ccdc->bt656)
128262306a36Sopenharmony_ci		syn_mode |= ISPCCDC_SYN_MODE_PACK8;
128362306a36Sopenharmony_ci	else
128462306a36Sopenharmony_ci		syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci	isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci	/* CCDC_PAD_SOURCE_VP */
128962306a36Sopenharmony_ci	ccdc_config_vp(ccdc);
129062306a36Sopenharmony_ci
129162306a36Sopenharmony_ci	/* Lens shading correction. */
129262306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
129362306a36Sopenharmony_ci	if (ccdc->lsc.request == NULL)
129462306a36Sopenharmony_ci		goto unlock;
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	WARN_ON(ccdc->lsc.active);
129762306a36Sopenharmony_ci
129862306a36Sopenharmony_ci	/* Get last good LSC configuration. If it is not supported for
129962306a36Sopenharmony_ci	 * the current active resolution discard it.
130062306a36Sopenharmony_ci	 */
130162306a36Sopenharmony_ci	if (ccdc->lsc.active == NULL &&
130262306a36Sopenharmony_ci	    __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
130362306a36Sopenharmony_ci		ccdc->lsc.active = ccdc->lsc.request;
130462306a36Sopenharmony_ci	} else {
130562306a36Sopenharmony_ci		list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
130662306a36Sopenharmony_ci		schedule_work(&ccdc->lsc.table_work);
130762306a36Sopenharmony_ci	}
130862306a36Sopenharmony_ci
130962306a36Sopenharmony_ci	ccdc->lsc.request = NULL;
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ciunlock:
131262306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_ci	ccdc_apply_controls(ccdc);
131562306a36Sopenharmony_ci}
131662306a36Sopenharmony_ci
131762306a36Sopenharmony_cistatic void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
131862306a36Sopenharmony_ci{
131962306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci	/* Avoid restarting the CCDC when streaming is stopping. */
132262306a36Sopenharmony_ci	if (enable && ccdc->stopping & CCDC_STOP_REQUEST)
132362306a36Sopenharmony_ci		return;
132462306a36Sopenharmony_ci
132562306a36Sopenharmony_ci	isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
132662306a36Sopenharmony_ci			ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
132762306a36Sopenharmony_ci
132862306a36Sopenharmony_ci	ccdc->running = enable;
132962306a36Sopenharmony_ci}
133062306a36Sopenharmony_ci
133162306a36Sopenharmony_cistatic int ccdc_disable(struct isp_ccdc_device *ccdc)
133262306a36Sopenharmony_ci{
133362306a36Sopenharmony_ci	unsigned long flags;
133462306a36Sopenharmony_ci	int ret = 0;
133562306a36Sopenharmony_ci
133662306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lock, flags);
133762306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
133862306a36Sopenharmony_ci		ccdc->stopping = CCDC_STOP_REQUEST;
133962306a36Sopenharmony_ci	if (!ccdc->running)
134062306a36Sopenharmony_ci		ccdc->stopping = CCDC_STOP_FINISHED;
134162306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lock, flags);
134262306a36Sopenharmony_ci
134362306a36Sopenharmony_ci	ret = wait_event_timeout(ccdc->wait,
134462306a36Sopenharmony_ci				 ccdc->stopping == CCDC_STOP_FINISHED,
134562306a36Sopenharmony_ci				 msecs_to_jiffies(2000));
134662306a36Sopenharmony_ci	if (ret == 0) {
134762306a36Sopenharmony_ci		ret = -ETIMEDOUT;
134862306a36Sopenharmony_ci		dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
134962306a36Sopenharmony_ci	}
135062306a36Sopenharmony_ci
135162306a36Sopenharmony_ci	omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
135262306a36Sopenharmony_ci
135362306a36Sopenharmony_ci	mutex_lock(&ccdc->ioctl_lock);
135462306a36Sopenharmony_ci	ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
135562306a36Sopenharmony_ci	ccdc->lsc.request = ccdc->lsc.active;
135662306a36Sopenharmony_ci	ccdc->lsc.active = NULL;
135762306a36Sopenharmony_ci	cancel_work_sync(&ccdc->lsc.table_work);
135862306a36Sopenharmony_ci	ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
135962306a36Sopenharmony_ci	mutex_unlock(&ccdc->ioctl_lock);
136062306a36Sopenharmony_ci
136162306a36Sopenharmony_ci	ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
136262306a36Sopenharmony_ci
136362306a36Sopenharmony_ci	return ret > 0 ? 0 : ret;
136462306a36Sopenharmony_ci}
136562306a36Sopenharmony_ci
136662306a36Sopenharmony_cistatic void ccdc_enable(struct isp_ccdc_device *ccdc)
136762306a36Sopenharmony_ci{
136862306a36Sopenharmony_ci	if (ccdc_lsc_is_configured(ccdc))
136962306a36Sopenharmony_ci		__ccdc_lsc_enable(ccdc, 1);
137062306a36Sopenharmony_ci	__ccdc_enable(ccdc, 1);
137162306a36Sopenharmony_ci}
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
137462306a36Sopenharmony_ci * Interrupt handling
137562306a36Sopenharmony_ci */
137662306a36Sopenharmony_ci
137762306a36Sopenharmony_ci/*
137862306a36Sopenharmony_ci * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
137962306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
138062306a36Sopenharmony_ci *
138162306a36Sopenharmony_ci * Returns zero if the CCDC is idle and the image has been written to
138262306a36Sopenharmony_ci * memory, too.
138362306a36Sopenharmony_ci */
138462306a36Sopenharmony_cistatic int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
138562306a36Sopenharmony_ci{
138662306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci	return omap3isp_ccdc_busy(ccdc)
138962306a36Sopenharmony_ci		| (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
139062306a36Sopenharmony_ci		   ISPSBL_CCDC_WR_0_DATA_READY)
139162306a36Sopenharmony_ci		| (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
139262306a36Sopenharmony_ci		   ISPSBL_CCDC_WR_0_DATA_READY)
139362306a36Sopenharmony_ci		| (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
139462306a36Sopenharmony_ci		   ISPSBL_CCDC_WR_0_DATA_READY)
139562306a36Sopenharmony_ci		| (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
139662306a36Sopenharmony_ci		   ISPSBL_CCDC_WR_0_DATA_READY);
139762306a36Sopenharmony_ci}
139862306a36Sopenharmony_ci
139962306a36Sopenharmony_ci/*
140062306a36Sopenharmony_ci * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
140162306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
140262306a36Sopenharmony_ci * @max_wait: Max retry count in us for wait for idle/busy transition.
140362306a36Sopenharmony_ci */
140462306a36Sopenharmony_cistatic int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
140562306a36Sopenharmony_ci			      unsigned int max_wait)
140662306a36Sopenharmony_ci{
140762306a36Sopenharmony_ci	unsigned int wait = 0;
140862306a36Sopenharmony_ci
140962306a36Sopenharmony_ci	if (max_wait == 0)
141062306a36Sopenharmony_ci		max_wait = 10000; /* 10 ms */
141162306a36Sopenharmony_ci
141262306a36Sopenharmony_ci	for (wait = 0; wait <= max_wait; wait++) {
141362306a36Sopenharmony_ci		if (!ccdc_sbl_busy(ccdc))
141462306a36Sopenharmony_ci			return 0;
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci		rmb();
141762306a36Sopenharmony_ci		udelay(1);
141862306a36Sopenharmony_ci	}
141962306a36Sopenharmony_ci
142062306a36Sopenharmony_ci	return -EBUSY;
142162306a36Sopenharmony_ci}
142262306a36Sopenharmony_ci
142362306a36Sopenharmony_ci/* ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
142462306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
142562306a36Sopenharmony_ci * @event: Pointing which event trigger handler
142662306a36Sopenharmony_ci *
142762306a36Sopenharmony_ci * Return 1 when the event and stopping request combination is satisfied,
142862306a36Sopenharmony_ci * zero otherwise.
142962306a36Sopenharmony_ci */
143062306a36Sopenharmony_cistatic int ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
143162306a36Sopenharmony_ci{
143262306a36Sopenharmony_ci	int rval = 0;
143362306a36Sopenharmony_ci
143462306a36Sopenharmony_ci	switch ((ccdc->stopping & 3) | event) {
143562306a36Sopenharmony_ci	case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
143662306a36Sopenharmony_ci		if (ccdc->lsc.state != LSC_STATE_STOPPED)
143762306a36Sopenharmony_ci			__ccdc_lsc_enable(ccdc, 0);
143862306a36Sopenharmony_ci		__ccdc_enable(ccdc, 0);
143962306a36Sopenharmony_ci		ccdc->stopping = CCDC_STOP_EXECUTED;
144062306a36Sopenharmony_ci		return 1;
144162306a36Sopenharmony_ci
144262306a36Sopenharmony_ci	case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
144362306a36Sopenharmony_ci		ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
144462306a36Sopenharmony_ci		if (ccdc->lsc.state == LSC_STATE_STOPPED)
144562306a36Sopenharmony_ci			ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
144662306a36Sopenharmony_ci		rval = 1;
144762306a36Sopenharmony_ci		break;
144862306a36Sopenharmony_ci
144962306a36Sopenharmony_ci	case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
145062306a36Sopenharmony_ci		ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
145162306a36Sopenharmony_ci		rval = 1;
145262306a36Sopenharmony_ci		break;
145362306a36Sopenharmony_ci
145462306a36Sopenharmony_ci	case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
145562306a36Sopenharmony_ci		return 1;
145662306a36Sopenharmony_ci	}
145762306a36Sopenharmony_ci
145862306a36Sopenharmony_ci	if (ccdc->stopping == CCDC_STOP_FINISHED) {
145962306a36Sopenharmony_ci		wake_up(&ccdc->wait);
146062306a36Sopenharmony_ci		rval = 1;
146162306a36Sopenharmony_ci	}
146262306a36Sopenharmony_ci
146362306a36Sopenharmony_ci	return rval;
146462306a36Sopenharmony_ci}
146562306a36Sopenharmony_ci
146662306a36Sopenharmony_cistatic void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
146762306a36Sopenharmony_ci{
146862306a36Sopenharmony_ci	struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
146962306a36Sopenharmony_ci	struct video_device *vdev = ccdc->subdev.devnode;
147062306a36Sopenharmony_ci	struct v4l2_event event;
147162306a36Sopenharmony_ci
147262306a36Sopenharmony_ci	/* Frame number propagation */
147362306a36Sopenharmony_ci	atomic_inc(&pipe->frame_number);
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci	memset(&event, 0, sizeof(event));
147662306a36Sopenharmony_ci	event.type = V4L2_EVENT_FRAME_SYNC;
147762306a36Sopenharmony_ci	event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ci	v4l2_event_queue(vdev, &event);
148062306a36Sopenharmony_ci}
148162306a36Sopenharmony_ci
148262306a36Sopenharmony_ci/*
148362306a36Sopenharmony_ci * ccdc_lsc_isr - Handle LSC events
148462306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
148562306a36Sopenharmony_ci * @events: LSC events
148662306a36Sopenharmony_ci */
148762306a36Sopenharmony_cistatic void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
148862306a36Sopenharmony_ci{
148962306a36Sopenharmony_ci	unsigned long flags;
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_ci	if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
149262306a36Sopenharmony_ci		struct isp_pipeline *pipe =
149362306a36Sopenharmony_ci			to_isp_pipeline(&ccdc->subdev.entity);
149462306a36Sopenharmony_ci
149562306a36Sopenharmony_ci		ccdc_lsc_error_handler(ccdc);
149662306a36Sopenharmony_ci		pipe->error = true;
149762306a36Sopenharmony_ci		dev_dbg(to_device(ccdc), "lsc prefetch error\n");
149862306a36Sopenharmony_ci	}
149962306a36Sopenharmony_ci
150062306a36Sopenharmony_ci	if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
150162306a36Sopenharmony_ci		return;
150262306a36Sopenharmony_ci
150362306a36Sopenharmony_ci	/* LSC_DONE interrupt occur, there are two cases
150462306a36Sopenharmony_ci	 * 1. stopping for reconfiguration
150562306a36Sopenharmony_ci	 * 2. stopping because of STREAM OFF command
150662306a36Sopenharmony_ci	 */
150762306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
150862306a36Sopenharmony_ci
150962306a36Sopenharmony_ci	if (ccdc->lsc.state == LSC_STATE_STOPPING)
151062306a36Sopenharmony_ci		ccdc->lsc.state = LSC_STATE_STOPPED;
151162306a36Sopenharmony_ci
151262306a36Sopenharmony_ci	if (ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
151362306a36Sopenharmony_ci		goto done;
151462306a36Sopenharmony_ci
151562306a36Sopenharmony_ci	if (ccdc->lsc.state != LSC_STATE_RECONFIG)
151662306a36Sopenharmony_ci		goto done;
151762306a36Sopenharmony_ci
151862306a36Sopenharmony_ci	/* LSC is in STOPPING state, change to the new state */
151962306a36Sopenharmony_ci	ccdc->lsc.state = LSC_STATE_STOPPED;
152062306a36Sopenharmony_ci
152162306a36Sopenharmony_ci	/* This is an exception. Start of frame and LSC_DONE interrupt
152262306a36Sopenharmony_ci	 * have been received on the same time. Skip this event and wait
152362306a36Sopenharmony_ci	 * for better times.
152462306a36Sopenharmony_ci	 */
152562306a36Sopenharmony_ci	if (events & IRQ0STATUS_HS_VS_IRQ)
152662306a36Sopenharmony_ci		goto done;
152762306a36Sopenharmony_ci
152862306a36Sopenharmony_ci	/* The LSC engine is stopped at this point. Enable it if there's a
152962306a36Sopenharmony_ci	 * pending request.
153062306a36Sopenharmony_ci	 */
153162306a36Sopenharmony_ci	if (ccdc->lsc.request == NULL)
153262306a36Sopenharmony_ci		goto done;
153362306a36Sopenharmony_ci
153462306a36Sopenharmony_ci	ccdc_lsc_enable(ccdc);
153562306a36Sopenharmony_ci
153662306a36Sopenharmony_cidone:
153762306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
153862306a36Sopenharmony_ci}
153962306a36Sopenharmony_ci
154062306a36Sopenharmony_ci/*
154162306a36Sopenharmony_ci * Check whether the CCDC has captured all fields necessary to complete the
154262306a36Sopenharmony_ci * buffer.
154362306a36Sopenharmony_ci */
154462306a36Sopenharmony_cistatic bool ccdc_has_all_fields(struct isp_ccdc_device *ccdc)
154562306a36Sopenharmony_ci{
154662306a36Sopenharmony_ci	struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
154762306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
154862306a36Sopenharmony_ci	enum v4l2_field of_field = ccdc->formats[CCDC_PAD_SOURCE_OF].field;
154962306a36Sopenharmony_ci	enum v4l2_field field;
155062306a36Sopenharmony_ci
155162306a36Sopenharmony_ci	/* When the input is progressive fields don't matter. */
155262306a36Sopenharmony_ci	if (of_field == V4L2_FIELD_NONE)
155362306a36Sopenharmony_ci		return true;
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_ci	/* Read the current field identifier. */
155662306a36Sopenharmony_ci	field = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE)
155762306a36Sopenharmony_ci	      & ISPCCDC_SYN_MODE_FLDSTAT
155862306a36Sopenharmony_ci	      ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
155962306a36Sopenharmony_ci
156062306a36Sopenharmony_ci	/* When capturing fields in alternate order just store the current field
156162306a36Sopenharmony_ci	 * identifier in the pipeline.
156262306a36Sopenharmony_ci	 */
156362306a36Sopenharmony_ci	if (of_field == V4L2_FIELD_ALTERNATE) {
156462306a36Sopenharmony_ci		pipe->field = field;
156562306a36Sopenharmony_ci		return true;
156662306a36Sopenharmony_ci	}
156762306a36Sopenharmony_ci
156862306a36Sopenharmony_ci	/* The format is interlaced. Make sure we've captured both fields. */
156962306a36Sopenharmony_ci	ccdc->fields |= field == V4L2_FIELD_BOTTOM
157062306a36Sopenharmony_ci		      ? CCDC_FIELD_BOTTOM : CCDC_FIELD_TOP;
157162306a36Sopenharmony_ci
157262306a36Sopenharmony_ci	if (ccdc->fields != CCDC_FIELD_BOTH)
157362306a36Sopenharmony_ci		return false;
157462306a36Sopenharmony_ci
157562306a36Sopenharmony_ci	/* Verify that the field just captured corresponds to the last field
157662306a36Sopenharmony_ci	 * needed based on the desired field order.
157762306a36Sopenharmony_ci	 */
157862306a36Sopenharmony_ci	if ((of_field == V4L2_FIELD_INTERLACED_TB && field == V4L2_FIELD_TOP) ||
157962306a36Sopenharmony_ci	    (of_field == V4L2_FIELD_INTERLACED_BT && field == V4L2_FIELD_BOTTOM))
158062306a36Sopenharmony_ci		return false;
158162306a36Sopenharmony_ci
158262306a36Sopenharmony_ci	/* The buffer can be completed, reset the fields for the next buffer. */
158362306a36Sopenharmony_ci	ccdc->fields = 0;
158462306a36Sopenharmony_ci
158562306a36Sopenharmony_ci	return true;
158662306a36Sopenharmony_ci}
158762306a36Sopenharmony_ci
158862306a36Sopenharmony_cistatic int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
158962306a36Sopenharmony_ci{
159062306a36Sopenharmony_ci	struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
159162306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
159262306a36Sopenharmony_ci	struct isp_buffer *buffer;
159362306a36Sopenharmony_ci
159462306a36Sopenharmony_ci	/* The CCDC generates VD0 interrupts even when disabled (the datasheet
159562306a36Sopenharmony_ci	 * doesn't explicitly state if that's supposed to happen or not, so it
159662306a36Sopenharmony_ci	 * can be considered as a hardware bug or as a feature, but we have to
159762306a36Sopenharmony_ci	 * deal with it anyway). Disabling the CCDC when no buffer is available
159862306a36Sopenharmony_ci	 * would thus not be enough, we need to handle the situation explicitly.
159962306a36Sopenharmony_ci	 */
160062306a36Sopenharmony_ci	if (list_empty(&ccdc->video_out.dmaqueue))
160162306a36Sopenharmony_ci		return 0;
160262306a36Sopenharmony_ci
160362306a36Sopenharmony_ci	/* We're in continuous mode, and memory writes were disabled due to a
160462306a36Sopenharmony_ci	 * buffer underrun. Re-enable them now that we have a buffer. The buffer
160562306a36Sopenharmony_ci	 * address has been set in ccdc_video_queue.
160662306a36Sopenharmony_ci	 */
160762306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
160862306a36Sopenharmony_ci		ccdc->underrun = 0;
160962306a36Sopenharmony_ci		return 1;
161062306a36Sopenharmony_ci	}
161162306a36Sopenharmony_ci
161262306a36Sopenharmony_ci	/* Wait for the CCDC to become idle. */
161362306a36Sopenharmony_ci	if (ccdc_sbl_wait_idle(ccdc, 1000)) {
161462306a36Sopenharmony_ci		dev_info(isp->dev, "CCDC won't become idle!\n");
161562306a36Sopenharmony_ci		media_entity_enum_set(&isp->crashed, &ccdc->subdev.entity);
161662306a36Sopenharmony_ci		omap3isp_pipeline_cancel_stream(pipe);
161762306a36Sopenharmony_ci		return 0;
161862306a36Sopenharmony_ci	}
161962306a36Sopenharmony_ci
162062306a36Sopenharmony_ci	/* Don't restart CCDC if we're just about to stop streaming. */
162162306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
162262306a36Sopenharmony_ci	    ccdc->stopping & CCDC_STOP_REQUEST)
162362306a36Sopenharmony_ci		return 0;
162462306a36Sopenharmony_ci
162562306a36Sopenharmony_ci	if (!ccdc_has_all_fields(ccdc))
162662306a36Sopenharmony_ci		return 1;
162762306a36Sopenharmony_ci
162862306a36Sopenharmony_ci	buffer = omap3isp_video_buffer_next(&ccdc->video_out);
162962306a36Sopenharmony_ci	if (buffer != NULL)
163062306a36Sopenharmony_ci		ccdc_set_outaddr(ccdc, buffer->dma);
163162306a36Sopenharmony_ci
163262306a36Sopenharmony_ci	pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
163362306a36Sopenharmony_ci
163462306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
163562306a36Sopenharmony_ci	    isp_pipeline_ready(pipe))
163662306a36Sopenharmony_ci		omap3isp_pipeline_set_stream(pipe,
163762306a36Sopenharmony_ci					ISP_PIPELINE_STREAM_SINGLESHOT);
163862306a36Sopenharmony_ci
163962306a36Sopenharmony_ci	return buffer != NULL;
164062306a36Sopenharmony_ci}
164162306a36Sopenharmony_ci
164262306a36Sopenharmony_ci/*
164362306a36Sopenharmony_ci * ccdc_vd0_isr - Handle VD0 event
164462306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
164562306a36Sopenharmony_ci *
164662306a36Sopenharmony_ci * Executes LSC deferred enablement before next frame starts.
164762306a36Sopenharmony_ci */
164862306a36Sopenharmony_cistatic void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
164962306a36Sopenharmony_ci{
165062306a36Sopenharmony_ci	unsigned long flags;
165162306a36Sopenharmony_ci	int restart = 0;
165262306a36Sopenharmony_ci
165362306a36Sopenharmony_ci	/* In BT.656 mode the CCDC doesn't generate an HS/VS interrupt. We thus
165462306a36Sopenharmony_ci	 * need to increment the frame counter here.
165562306a36Sopenharmony_ci	 */
165662306a36Sopenharmony_ci	if (ccdc->bt656) {
165762306a36Sopenharmony_ci		struct isp_pipeline *pipe =
165862306a36Sopenharmony_ci			to_isp_pipeline(&ccdc->subdev.entity);
165962306a36Sopenharmony_ci
166062306a36Sopenharmony_ci		atomic_inc(&pipe->frame_number);
166162306a36Sopenharmony_ci	}
166262306a36Sopenharmony_ci
166362306a36Sopenharmony_ci	/* Emulate a VD1 interrupt for BT.656 mode, as we can't stop the CCDC in
166462306a36Sopenharmony_ci	 * the VD1 interrupt handler in that mode without risking a CCDC stall
166562306a36Sopenharmony_ci	 * if a short frame is received.
166662306a36Sopenharmony_ci	 */
166762306a36Sopenharmony_ci	if (ccdc->bt656) {
166862306a36Sopenharmony_ci		spin_lock_irqsave(&ccdc->lock, flags);
166962306a36Sopenharmony_ci		if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
167062306a36Sopenharmony_ci		    ccdc->output & CCDC_OUTPUT_MEMORY) {
167162306a36Sopenharmony_ci			if (ccdc->lsc.state != LSC_STATE_STOPPED)
167262306a36Sopenharmony_ci				__ccdc_lsc_enable(ccdc, 0);
167362306a36Sopenharmony_ci			__ccdc_enable(ccdc, 0);
167462306a36Sopenharmony_ci		}
167562306a36Sopenharmony_ci		ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1);
167662306a36Sopenharmony_ci		spin_unlock_irqrestore(&ccdc->lock, flags);
167762306a36Sopenharmony_ci	}
167862306a36Sopenharmony_ci
167962306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lock, flags);
168062306a36Sopenharmony_ci	if (ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
168162306a36Sopenharmony_ci		spin_unlock_irqrestore(&ccdc->lock, flags);
168262306a36Sopenharmony_ci		return;
168362306a36Sopenharmony_ci	}
168462306a36Sopenharmony_ci
168562306a36Sopenharmony_ci	if (ccdc->output & CCDC_OUTPUT_MEMORY)
168662306a36Sopenharmony_ci		restart = ccdc_isr_buffer(ccdc);
168762306a36Sopenharmony_ci
168862306a36Sopenharmony_ci	if (!ccdc->shadow_update)
168962306a36Sopenharmony_ci		ccdc_apply_controls(ccdc);
169062306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lock, flags);
169162306a36Sopenharmony_ci
169262306a36Sopenharmony_ci	if (restart)
169362306a36Sopenharmony_ci		ccdc_enable(ccdc);
169462306a36Sopenharmony_ci}
169562306a36Sopenharmony_ci
169662306a36Sopenharmony_ci/*
169762306a36Sopenharmony_ci * ccdc_vd1_isr - Handle VD1 event
169862306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
169962306a36Sopenharmony_ci */
170062306a36Sopenharmony_cistatic void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
170162306a36Sopenharmony_ci{
170262306a36Sopenharmony_ci	unsigned long flags;
170362306a36Sopenharmony_ci
170462306a36Sopenharmony_ci	/* In BT.656 mode the synchronization signals are generated by the CCDC
170562306a36Sopenharmony_ci	 * from the embedded sync codes. The VD0 and VD1 interrupts are thus
170662306a36Sopenharmony_ci	 * only triggered when the CCDC is enabled, unlike external sync mode
170762306a36Sopenharmony_ci	 * where the line counter runs even when the CCDC is stopped. We can't
170862306a36Sopenharmony_ci	 * disable the CCDC at VD1 time, as no VD0 interrupt would be generated
170962306a36Sopenharmony_ci	 * for a short frame, which would result in the CCDC being stopped and
171062306a36Sopenharmony_ci	 * no VD interrupt generated anymore. The CCDC is stopped from the VD0
171162306a36Sopenharmony_ci	 * interrupt handler instead for BT.656.
171262306a36Sopenharmony_ci	 */
171362306a36Sopenharmony_ci	if (ccdc->bt656)
171462306a36Sopenharmony_ci		return;
171562306a36Sopenharmony_ci
171662306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
171762306a36Sopenharmony_ci
171862306a36Sopenharmony_ci	/*
171962306a36Sopenharmony_ci	 * Depending on the CCDC pipeline state, CCDC stopping should be
172062306a36Sopenharmony_ci	 * handled differently. In SINGLESHOT we emulate an internal CCDC
172162306a36Sopenharmony_ci	 * stopping because the CCDC hw works only in continuous mode.
172262306a36Sopenharmony_ci	 * When CONTINUOUS pipeline state is used and the CCDC writes it's
172362306a36Sopenharmony_ci	 * data to memory the CCDC and LSC are stopped immediately but
172462306a36Sopenharmony_ci	 * without change the CCDC stopping state machine. The CCDC
172562306a36Sopenharmony_ci	 * stopping state machine should be used only when user request
172662306a36Sopenharmony_ci	 * for stopping is received (SINGLESHOT is an exception).
172762306a36Sopenharmony_ci	 */
172862306a36Sopenharmony_ci	switch (ccdc->state) {
172962306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_SINGLESHOT:
173062306a36Sopenharmony_ci		ccdc->stopping = CCDC_STOP_REQUEST;
173162306a36Sopenharmony_ci		break;
173262306a36Sopenharmony_ci
173362306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_CONTINUOUS:
173462306a36Sopenharmony_ci		if (ccdc->output & CCDC_OUTPUT_MEMORY) {
173562306a36Sopenharmony_ci			if (ccdc->lsc.state != LSC_STATE_STOPPED)
173662306a36Sopenharmony_ci				__ccdc_lsc_enable(ccdc, 0);
173762306a36Sopenharmony_ci			__ccdc_enable(ccdc, 0);
173862306a36Sopenharmony_ci		}
173962306a36Sopenharmony_ci		break;
174062306a36Sopenharmony_ci
174162306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_STOPPED:
174262306a36Sopenharmony_ci		break;
174362306a36Sopenharmony_ci	}
174462306a36Sopenharmony_ci
174562306a36Sopenharmony_ci	if (ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
174662306a36Sopenharmony_ci		goto done;
174762306a36Sopenharmony_ci
174862306a36Sopenharmony_ci	if (ccdc->lsc.request == NULL)
174962306a36Sopenharmony_ci		goto done;
175062306a36Sopenharmony_ci
175162306a36Sopenharmony_ci	/*
175262306a36Sopenharmony_ci	 * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
175362306a36Sopenharmony_ci	 * do the appropriate changes in registers
175462306a36Sopenharmony_ci	 */
175562306a36Sopenharmony_ci	if (ccdc->lsc.state == LSC_STATE_RUNNING) {
175662306a36Sopenharmony_ci		__ccdc_lsc_enable(ccdc, 0);
175762306a36Sopenharmony_ci		ccdc->lsc.state = LSC_STATE_RECONFIG;
175862306a36Sopenharmony_ci		goto done;
175962306a36Sopenharmony_ci	}
176062306a36Sopenharmony_ci
176162306a36Sopenharmony_ci	/* LSC has been in STOPPED state, enable it */
176262306a36Sopenharmony_ci	if (ccdc->lsc.state == LSC_STATE_STOPPED)
176362306a36Sopenharmony_ci		ccdc_lsc_enable(ccdc);
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_cidone:
176662306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
176762306a36Sopenharmony_ci}
176862306a36Sopenharmony_ci
176962306a36Sopenharmony_ci/*
177062306a36Sopenharmony_ci * omap3isp_ccdc_isr - Configure CCDC during interframe time.
177162306a36Sopenharmony_ci * @ccdc: Pointer to ISP CCDC device.
177262306a36Sopenharmony_ci * @events: CCDC events
177362306a36Sopenharmony_ci */
177462306a36Sopenharmony_ciint omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
177562306a36Sopenharmony_ci{
177662306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
177762306a36Sopenharmony_ci		return 0;
177862306a36Sopenharmony_ci
177962306a36Sopenharmony_ci	if (events & IRQ0STATUS_CCDC_VD1_IRQ)
178062306a36Sopenharmony_ci		ccdc_vd1_isr(ccdc);
178162306a36Sopenharmony_ci
178262306a36Sopenharmony_ci	ccdc_lsc_isr(ccdc, events);
178362306a36Sopenharmony_ci
178462306a36Sopenharmony_ci	if (events & IRQ0STATUS_CCDC_VD0_IRQ)
178562306a36Sopenharmony_ci		ccdc_vd0_isr(ccdc);
178662306a36Sopenharmony_ci
178762306a36Sopenharmony_ci	if (events & IRQ0STATUS_HS_VS_IRQ)
178862306a36Sopenharmony_ci		ccdc_hs_vs_isr(ccdc);
178962306a36Sopenharmony_ci
179062306a36Sopenharmony_ci	return 0;
179162306a36Sopenharmony_ci}
179262306a36Sopenharmony_ci
179362306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
179462306a36Sopenharmony_ci * ISP video operations
179562306a36Sopenharmony_ci */
179662306a36Sopenharmony_ci
179762306a36Sopenharmony_cistatic int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
179862306a36Sopenharmony_ci{
179962306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
180062306a36Sopenharmony_ci	unsigned long flags;
180162306a36Sopenharmony_ci	bool restart = false;
180262306a36Sopenharmony_ci
180362306a36Sopenharmony_ci	if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
180462306a36Sopenharmony_ci		return -ENODEV;
180562306a36Sopenharmony_ci
180662306a36Sopenharmony_ci	ccdc_set_outaddr(ccdc, buffer->dma);
180762306a36Sopenharmony_ci
180862306a36Sopenharmony_ci	/* We now have a buffer queued on the output, restart the pipeline
180962306a36Sopenharmony_ci	 * on the next CCDC interrupt if running in continuous mode (or when
181062306a36Sopenharmony_ci	 * starting the stream) in external sync mode, or immediately in BT.656
181162306a36Sopenharmony_ci	 * sync mode as no CCDC interrupt is generated when the CCDC is stopped
181262306a36Sopenharmony_ci	 * in that case.
181362306a36Sopenharmony_ci	 */
181462306a36Sopenharmony_ci	spin_lock_irqsave(&ccdc->lock, flags);
181562306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && !ccdc->running &&
181662306a36Sopenharmony_ci	    ccdc->bt656)
181762306a36Sopenharmony_ci		restart = true;
181862306a36Sopenharmony_ci	else
181962306a36Sopenharmony_ci		ccdc->underrun = 1;
182062306a36Sopenharmony_ci	spin_unlock_irqrestore(&ccdc->lock, flags);
182162306a36Sopenharmony_ci
182262306a36Sopenharmony_ci	if (restart)
182362306a36Sopenharmony_ci		ccdc_enable(ccdc);
182462306a36Sopenharmony_ci
182562306a36Sopenharmony_ci	return 0;
182662306a36Sopenharmony_ci}
182762306a36Sopenharmony_ci
182862306a36Sopenharmony_cistatic const struct isp_video_operations ccdc_video_ops = {
182962306a36Sopenharmony_ci	.queue = ccdc_video_queue,
183062306a36Sopenharmony_ci};
183162306a36Sopenharmony_ci
183262306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
183362306a36Sopenharmony_ci * V4L2 subdev operations
183462306a36Sopenharmony_ci */
183562306a36Sopenharmony_ci
183662306a36Sopenharmony_ci/*
183762306a36Sopenharmony_ci * ccdc_ioctl - CCDC module private ioctl's
183862306a36Sopenharmony_ci * @sd: ISP CCDC V4L2 subdevice
183962306a36Sopenharmony_ci * @cmd: ioctl command
184062306a36Sopenharmony_ci * @arg: ioctl argument
184162306a36Sopenharmony_ci *
184262306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise.
184362306a36Sopenharmony_ci */
184462306a36Sopenharmony_cistatic long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
184562306a36Sopenharmony_ci{
184662306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
184762306a36Sopenharmony_ci	int ret;
184862306a36Sopenharmony_ci
184962306a36Sopenharmony_ci	switch (cmd) {
185062306a36Sopenharmony_ci	case VIDIOC_OMAP3ISP_CCDC_CFG:
185162306a36Sopenharmony_ci		mutex_lock(&ccdc->ioctl_lock);
185262306a36Sopenharmony_ci		ret = ccdc_config(ccdc, arg);
185362306a36Sopenharmony_ci		mutex_unlock(&ccdc->ioctl_lock);
185462306a36Sopenharmony_ci		break;
185562306a36Sopenharmony_ci
185662306a36Sopenharmony_ci	default:
185762306a36Sopenharmony_ci		return -ENOIOCTLCMD;
185862306a36Sopenharmony_ci	}
185962306a36Sopenharmony_ci
186062306a36Sopenharmony_ci	return ret;
186162306a36Sopenharmony_ci}
186262306a36Sopenharmony_ci
186362306a36Sopenharmony_cistatic int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
186462306a36Sopenharmony_ci				struct v4l2_event_subscription *sub)
186562306a36Sopenharmony_ci{
186662306a36Sopenharmony_ci	if (sub->type != V4L2_EVENT_FRAME_SYNC)
186762306a36Sopenharmony_ci		return -EINVAL;
186862306a36Sopenharmony_ci
186962306a36Sopenharmony_ci	/* line number is zero at frame start */
187062306a36Sopenharmony_ci	if (sub->id != 0)
187162306a36Sopenharmony_ci		return -EINVAL;
187262306a36Sopenharmony_ci
187362306a36Sopenharmony_ci	return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
187462306a36Sopenharmony_ci}
187562306a36Sopenharmony_ci
187662306a36Sopenharmony_cistatic int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
187762306a36Sopenharmony_ci				  struct v4l2_event_subscription *sub)
187862306a36Sopenharmony_ci{
187962306a36Sopenharmony_ci	return v4l2_event_unsubscribe(fh, sub);
188062306a36Sopenharmony_ci}
188162306a36Sopenharmony_ci
188262306a36Sopenharmony_ci/*
188362306a36Sopenharmony_ci * ccdc_set_stream - Enable/Disable streaming on the CCDC module
188462306a36Sopenharmony_ci * @sd: ISP CCDC V4L2 subdevice
188562306a36Sopenharmony_ci * @enable: Enable/disable stream
188662306a36Sopenharmony_ci *
188762306a36Sopenharmony_ci * When writing to memory, the CCDC hardware can't be enabled without a memory
188862306a36Sopenharmony_ci * buffer to write to. As the s_stream operation is called in response to a
188962306a36Sopenharmony_ci * STREAMON call without any buffer queued yet, just update the enabled field
189062306a36Sopenharmony_ci * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
189162306a36Sopenharmony_ci *
189262306a36Sopenharmony_ci * When not writing to memory enable the CCDC immediately.
189362306a36Sopenharmony_ci */
189462306a36Sopenharmony_cistatic int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
189562306a36Sopenharmony_ci{
189662306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
189762306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
189862306a36Sopenharmony_ci	int ret = 0;
189962306a36Sopenharmony_ci
190062306a36Sopenharmony_ci	if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
190162306a36Sopenharmony_ci		if (enable == ISP_PIPELINE_STREAM_STOPPED)
190262306a36Sopenharmony_ci			return 0;
190362306a36Sopenharmony_ci
190462306a36Sopenharmony_ci		omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
190562306a36Sopenharmony_ci		isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
190662306a36Sopenharmony_ci			    ISPCCDC_CFG_VDLC);
190762306a36Sopenharmony_ci
190862306a36Sopenharmony_ci		ccdc_configure(ccdc);
190962306a36Sopenharmony_ci
191062306a36Sopenharmony_ci		ccdc_print_status(ccdc);
191162306a36Sopenharmony_ci	}
191262306a36Sopenharmony_ci
191362306a36Sopenharmony_ci	switch (enable) {
191462306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_CONTINUOUS:
191562306a36Sopenharmony_ci		if (ccdc->output & CCDC_OUTPUT_MEMORY)
191662306a36Sopenharmony_ci			omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
191762306a36Sopenharmony_ci
191862306a36Sopenharmony_ci		if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
191962306a36Sopenharmony_ci			ccdc_enable(ccdc);
192062306a36Sopenharmony_ci
192162306a36Sopenharmony_ci		ccdc->underrun = 0;
192262306a36Sopenharmony_ci		break;
192362306a36Sopenharmony_ci
192462306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_SINGLESHOT:
192562306a36Sopenharmony_ci		if (ccdc->output & CCDC_OUTPUT_MEMORY &&
192662306a36Sopenharmony_ci		    ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
192762306a36Sopenharmony_ci			omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
192862306a36Sopenharmony_ci
192962306a36Sopenharmony_ci		ccdc_enable(ccdc);
193062306a36Sopenharmony_ci		break;
193162306a36Sopenharmony_ci
193262306a36Sopenharmony_ci	case ISP_PIPELINE_STREAM_STOPPED:
193362306a36Sopenharmony_ci		ret = ccdc_disable(ccdc);
193462306a36Sopenharmony_ci		if (ccdc->output & CCDC_OUTPUT_MEMORY)
193562306a36Sopenharmony_ci			omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
193662306a36Sopenharmony_ci		omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
193762306a36Sopenharmony_ci		ccdc->underrun = 0;
193862306a36Sopenharmony_ci		break;
193962306a36Sopenharmony_ci	}
194062306a36Sopenharmony_ci
194162306a36Sopenharmony_ci	ccdc->state = enable;
194262306a36Sopenharmony_ci	return ret;
194362306a36Sopenharmony_ci}
194462306a36Sopenharmony_ci
194562306a36Sopenharmony_cistatic struct v4l2_mbus_framefmt *
194662306a36Sopenharmony_ci__ccdc_get_format(struct isp_ccdc_device *ccdc,
194762306a36Sopenharmony_ci		  struct v4l2_subdev_state *sd_state,
194862306a36Sopenharmony_ci		  unsigned int pad, enum v4l2_subdev_format_whence which)
194962306a36Sopenharmony_ci{
195062306a36Sopenharmony_ci	if (which == V4L2_SUBDEV_FORMAT_TRY)
195162306a36Sopenharmony_ci		return v4l2_subdev_get_try_format(&ccdc->subdev, sd_state,
195262306a36Sopenharmony_ci						  pad);
195362306a36Sopenharmony_ci	else
195462306a36Sopenharmony_ci		return &ccdc->formats[pad];
195562306a36Sopenharmony_ci}
195662306a36Sopenharmony_ci
195762306a36Sopenharmony_cistatic struct v4l2_rect *
195862306a36Sopenharmony_ci__ccdc_get_crop(struct isp_ccdc_device *ccdc,
195962306a36Sopenharmony_ci		struct v4l2_subdev_state *sd_state,
196062306a36Sopenharmony_ci		enum v4l2_subdev_format_whence which)
196162306a36Sopenharmony_ci{
196262306a36Sopenharmony_ci	if (which == V4L2_SUBDEV_FORMAT_TRY)
196362306a36Sopenharmony_ci		return v4l2_subdev_get_try_crop(&ccdc->subdev, sd_state,
196462306a36Sopenharmony_ci						CCDC_PAD_SOURCE_OF);
196562306a36Sopenharmony_ci	else
196662306a36Sopenharmony_ci		return &ccdc->crop;
196762306a36Sopenharmony_ci}
196862306a36Sopenharmony_ci
196962306a36Sopenharmony_ci/*
197062306a36Sopenharmony_ci * ccdc_try_format - Try video format on a pad
197162306a36Sopenharmony_ci * @ccdc: ISP CCDC device
197262306a36Sopenharmony_ci * @cfg : V4L2 subdev pad configuration
197362306a36Sopenharmony_ci * @pad: Pad number
197462306a36Sopenharmony_ci * @fmt: Format
197562306a36Sopenharmony_ci */
197662306a36Sopenharmony_cistatic void
197762306a36Sopenharmony_ciccdc_try_format(struct isp_ccdc_device *ccdc,
197862306a36Sopenharmony_ci		struct v4l2_subdev_state *sd_state,
197962306a36Sopenharmony_ci		unsigned int pad, struct v4l2_mbus_framefmt *fmt,
198062306a36Sopenharmony_ci		enum v4l2_subdev_format_whence which)
198162306a36Sopenharmony_ci{
198262306a36Sopenharmony_ci	const struct isp_format_info *info;
198362306a36Sopenharmony_ci	u32 pixelcode;
198462306a36Sopenharmony_ci	unsigned int width = fmt->width;
198562306a36Sopenharmony_ci	unsigned int height = fmt->height;
198662306a36Sopenharmony_ci	struct v4l2_rect *crop;
198762306a36Sopenharmony_ci	enum v4l2_field field;
198862306a36Sopenharmony_ci	unsigned int i;
198962306a36Sopenharmony_ci
199062306a36Sopenharmony_ci	switch (pad) {
199162306a36Sopenharmony_ci	case CCDC_PAD_SINK:
199262306a36Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
199362306a36Sopenharmony_ci			if (fmt->code == ccdc_fmts[i])
199462306a36Sopenharmony_ci				break;
199562306a36Sopenharmony_ci		}
199662306a36Sopenharmony_ci
199762306a36Sopenharmony_ci		/* If not found, use SGRBG10 as default */
199862306a36Sopenharmony_ci		if (i >= ARRAY_SIZE(ccdc_fmts))
199962306a36Sopenharmony_ci			fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
200062306a36Sopenharmony_ci
200162306a36Sopenharmony_ci		/* Clamp the input size. */
200262306a36Sopenharmony_ci		fmt->width = clamp_t(u32, width, 32, 4096);
200362306a36Sopenharmony_ci		fmt->height = clamp_t(u32, height, 32, 4096);
200462306a36Sopenharmony_ci
200562306a36Sopenharmony_ci		/* Default to progressive field order. */
200662306a36Sopenharmony_ci		if (fmt->field == V4L2_FIELD_ANY)
200762306a36Sopenharmony_ci			fmt->field = V4L2_FIELD_NONE;
200862306a36Sopenharmony_ci
200962306a36Sopenharmony_ci		break;
201062306a36Sopenharmony_ci
201162306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_OF:
201262306a36Sopenharmony_ci		pixelcode = fmt->code;
201362306a36Sopenharmony_ci		field = fmt->field;
201462306a36Sopenharmony_ci		*fmt = *__ccdc_get_format(ccdc, sd_state, CCDC_PAD_SINK,
201562306a36Sopenharmony_ci					  which);
201662306a36Sopenharmony_ci
201762306a36Sopenharmony_ci		/* In SYNC mode the bridge converts YUV formats from 2X8 to
201862306a36Sopenharmony_ci		 * 1X16. In BT.656 no such conversion occurs. As we don't know
201962306a36Sopenharmony_ci		 * at this point whether the source will use SYNC or BT.656 mode
202062306a36Sopenharmony_ci		 * let's pretend the conversion always occurs. The CCDC will be
202162306a36Sopenharmony_ci		 * configured to pack bytes in BT.656, hiding the inaccuracy.
202262306a36Sopenharmony_ci		 * In all cases bytes can be swapped.
202362306a36Sopenharmony_ci		 */
202462306a36Sopenharmony_ci		if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
202562306a36Sopenharmony_ci		    fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
202662306a36Sopenharmony_ci			/* Use the user requested format if YUV. */
202762306a36Sopenharmony_ci			if (pixelcode == MEDIA_BUS_FMT_YUYV8_2X8 ||
202862306a36Sopenharmony_ci			    pixelcode == MEDIA_BUS_FMT_UYVY8_2X8 ||
202962306a36Sopenharmony_ci			    pixelcode == MEDIA_BUS_FMT_YUYV8_1X16 ||
203062306a36Sopenharmony_ci			    pixelcode == MEDIA_BUS_FMT_UYVY8_1X16)
203162306a36Sopenharmony_ci				fmt->code = pixelcode;
203262306a36Sopenharmony_ci
203362306a36Sopenharmony_ci			if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8)
203462306a36Sopenharmony_ci				fmt->code = MEDIA_BUS_FMT_YUYV8_1X16;
203562306a36Sopenharmony_ci			else if (fmt->code == MEDIA_BUS_FMT_UYVY8_2X8)
203662306a36Sopenharmony_ci				fmt->code = MEDIA_BUS_FMT_UYVY8_1X16;
203762306a36Sopenharmony_ci		}
203862306a36Sopenharmony_ci
203962306a36Sopenharmony_ci		/* Hardcode the output size to the crop rectangle size. */
204062306a36Sopenharmony_ci		crop = __ccdc_get_crop(ccdc, sd_state, which);
204162306a36Sopenharmony_ci		fmt->width = crop->width;
204262306a36Sopenharmony_ci		fmt->height = crop->height;
204362306a36Sopenharmony_ci
204462306a36Sopenharmony_ci		/* When input format is interlaced with alternating fields the
204562306a36Sopenharmony_ci		 * CCDC can interleave the fields.
204662306a36Sopenharmony_ci		 */
204762306a36Sopenharmony_ci		if (fmt->field == V4L2_FIELD_ALTERNATE &&
204862306a36Sopenharmony_ci		    (field == V4L2_FIELD_INTERLACED_TB ||
204962306a36Sopenharmony_ci		     field == V4L2_FIELD_INTERLACED_BT)) {
205062306a36Sopenharmony_ci			fmt->field = field;
205162306a36Sopenharmony_ci			fmt->height *= 2;
205262306a36Sopenharmony_ci		}
205362306a36Sopenharmony_ci
205462306a36Sopenharmony_ci		break;
205562306a36Sopenharmony_ci
205662306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_VP:
205762306a36Sopenharmony_ci		*fmt = *__ccdc_get_format(ccdc, sd_state, CCDC_PAD_SINK,
205862306a36Sopenharmony_ci					  which);
205962306a36Sopenharmony_ci
206062306a36Sopenharmony_ci		/* The video port interface truncates the data to 10 bits. */
206162306a36Sopenharmony_ci		info = omap3isp_video_format_info(fmt->code);
206262306a36Sopenharmony_ci		fmt->code = info->truncated;
206362306a36Sopenharmony_ci
206462306a36Sopenharmony_ci		/* YUV formats are not supported by the video port. */
206562306a36Sopenharmony_ci		if (fmt->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
206662306a36Sopenharmony_ci		    fmt->code == MEDIA_BUS_FMT_UYVY8_2X8)
206762306a36Sopenharmony_ci			fmt->code = 0;
206862306a36Sopenharmony_ci
206962306a36Sopenharmony_ci		/* The number of lines that can be clocked out from the video
207062306a36Sopenharmony_ci		 * port output must be at least one line less than the number
207162306a36Sopenharmony_ci		 * of input lines.
207262306a36Sopenharmony_ci		 */
207362306a36Sopenharmony_ci		fmt->width = clamp_t(u32, width, 32, fmt->width);
207462306a36Sopenharmony_ci		fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
207562306a36Sopenharmony_ci		break;
207662306a36Sopenharmony_ci	}
207762306a36Sopenharmony_ci
207862306a36Sopenharmony_ci	/* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
207962306a36Sopenharmony_ci	 * stored on 2 bytes.
208062306a36Sopenharmony_ci	 */
208162306a36Sopenharmony_ci	fmt->colorspace = V4L2_COLORSPACE_SRGB;
208262306a36Sopenharmony_ci}
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci/*
208562306a36Sopenharmony_ci * ccdc_try_crop - Validate a crop rectangle
208662306a36Sopenharmony_ci * @ccdc: ISP CCDC device
208762306a36Sopenharmony_ci * @sink: format on the sink pad
208862306a36Sopenharmony_ci * @crop: crop rectangle to be validated
208962306a36Sopenharmony_ci */
209062306a36Sopenharmony_cistatic void ccdc_try_crop(struct isp_ccdc_device *ccdc,
209162306a36Sopenharmony_ci			  const struct v4l2_mbus_framefmt *sink,
209262306a36Sopenharmony_ci			  struct v4l2_rect *crop)
209362306a36Sopenharmony_ci{
209462306a36Sopenharmony_ci	const struct isp_format_info *info;
209562306a36Sopenharmony_ci	unsigned int max_width;
209662306a36Sopenharmony_ci
209762306a36Sopenharmony_ci	/* For Bayer formats, restrict left/top and width/height to even values
209862306a36Sopenharmony_ci	 * to keep the Bayer pattern.
209962306a36Sopenharmony_ci	 */
210062306a36Sopenharmony_ci	info = omap3isp_video_format_info(sink->code);
210162306a36Sopenharmony_ci	if (info->flavor != MEDIA_BUS_FMT_Y8_1X8) {
210262306a36Sopenharmony_ci		crop->left &= ~1;
210362306a36Sopenharmony_ci		crop->top &= ~1;
210462306a36Sopenharmony_ci	}
210562306a36Sopenharmony_ci
210662306a36Sopenharmony_ci	crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
210762306a36Sopenharmony_ci	crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
210862306a36Sopenharmony_ci
210962306a36Sopenharmony_ci	/* The data formatter truncates the number of horizontal output pixels
211062306a36Sopenharmony_ci	 * to a multiple of 16. To avoid clipping data, allow callers to request
211162306a36Sopenharmony_ci	 * an output size bigger than the input size up to the nearest multiple
211262306a36Sopenharmony_ci	 * of 16.
211362306a36Sopenharmony_ci	 */
211462306a36Sopenharmony_ci	max_width = (sink->width - crop->left + 15) & ~15;
211562306a36Sopenharmony_ci	crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
211662306a36Sopenharmony_ci		    & ~15;
211762306a36Sopenharmony_ci	crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
211862306a36Sopenharmony_ci			       sink->height - crop->top);
211962306a36Sopenharmony_ci
212062306a36Sopenharmony_ci	/* Odd width/height values don't make sense for Bayer formats. */
212162306a36Sopenharmony_ci	if (info->flavor != MEDIA_BUS_FMT_Y8_1X8) {
212262306a36Sopenharmony_ci		crop->width &= ~1;
212362306a36Sopenharmony_ci		crop->height &= ~1;
212462306a36Sopenharmony_ci	}
212562306a36Sopenharmony_ci}
212662306a36Sopenharmony_ci
212762306a36Sopenharmony_ci/*
212862306a36Sopenharmony_ci * ccdc_enum_mbus_code - Handle pixel format enumeration
212962306a36Sopenharmony_ci * @sd     : pointer to v4l2 subdev structure
213062306a36Sopenharmony_ci * @cfg : V4L2 subdev pad configuration
213162306a36Sopenharmony_ci * @code   : pointer to v4l2_subdev_mbus_code_enum structure
213262306a36Sopenharmony_ci * return -EINVAL or zero on success
213362306a36Sopenharmony_ci */
213462306a36Sopenharmony_cistatic int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
213562306a36Sopenharmony_ci			       struct v4l2_subdev_state *sd_state,
213662306a36Sopenharmony_ci			       struct v4l2_subdev_mbus_code_enum *code)
213762306a36Sopenharmony_ci{
213862306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
213962306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
214062306a36Sopenharmony_ci
214162306a36Sopenharmony_ci	switch (code->pad) {
214262306a36Sopenharmony_ci	case CCDC_PAD_SINK:
214362306a36Sopenharmony_ci		if (code->index >= ARRAY_SIZE(ccdc_fmts))
214462306a36Sopenharmony_ci			return -EINVAL;
214562306a36Sopenharmony_ci
214662306a36Sopenharmony_ci		code->code = ccdc_fmts[code->index];
214762306a36Sopenharmony_ci		break;
214862306a36Sopenharmony_ci
214962306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_OF:
215062306a36Sopenharmony_ci		format = __ccdc_get_format(ccdc, sd_state, code->pad,
215162306a36Sopenharmony_ci					   code->which);
215262306a36Sopenharmony_ci
215362306a36Sopenharmony_ci		if (format->code == MEDIA_BUS_FMT_YUYV8_2X8 ||
215462306a36Sopenharmony_ci		    format->code == MEDIA_BUS_FMT_UYVY8_2X8) {
215562306a36Sopenharmony_ci			/* In YUV mode the CCDC can swap bytes. */
215662306a36Sopenharmony_ci			if (code->index == 0)
215762306a36Sopenharmony_ci				code->code = MEDIA_BUS_FMT_YUYV8_1X16;
215862306a36Sopenharmony_ci			else if (code->index == 1)
215962306a36Sopenharmony_ci				code->code = MEDIA_BUS_FMT_UYVY8_1X16;
216062306a36Sopenharmony_ci			else
216162306a36Sopenharmony_ci				return -EINVAL;
216262306a36Sopenharmony_ci		} else {
216362306a36Sopenharmony_ci			/* In raw mode, no configurable format confversion is
216462306a36Sopenharmony_ci			 * available.
216562306a36Sopenharmony_ci			 */
216662306a36Sopenharmony_ci			if (code->index == 0)
216762306a36Sopenharmony_ci				code->code = format->code;
216862306a36Sopenharmony_ci			else
216962306a36Sopenharmony_ci				return -EINVAL;
217062306a36Sopenharmony_ci		}
217162306a36Sopenharmony_ci		break;
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_VP:
217462306a36Sopenharmony_ci		/* The CCDC supports no configurable format conversion
217562306a36Sopenharmony_ci		 * compatible with the video port. Enumerate a single output
217662306a36Sopenharmony_ci		 * format code.
217762306a36Sopenharmony_ci		 */
217862306a36Sopenharmony_ci		if (code->index != 0)
217962306a36Sopenharmony_ci			return -EINVAL;
218062306a36Sopenharmony_ci
218162306a36Sopenharmony_ci		format = __ccdc_get_format(ccdc, sd_state, code->pad,
218262306a36Sopenharmony_ci					   code->which);
218362306a36Sopenharmony_ci
218462306a36Sopenharmony_ci		/* A pixel code equal to 0 means that the video port doesn't
218562306a36Sopenharmony_ci		 * support the input format. Don't enumerate any pixel code.
218662306a36Sopenharmony_ci		 */
218762306a36Sopenharmony_ci		if (format->code == 0)
218862306a36Sopenharmony_ci			return -EINVAL;
218962306a36Sopenharmony_ci
219062306a36Sopenharmony_ci		code->code = format->code;
219162306a36Sopenharmony_ci		break;
219262306a36Sopenharmony_ci
219362306a36Sopenharmony_ci	default:
219462306a36Sopenharmony_ci		return -EINVAL;
219562306a36Sopenharmony_ci	}
219662306a36Sopenharmony_ci
219762306a36Sopenharmony_ci	return 0;
219862306a36Sopenharmony_ci}
219962306a36Sopenharmony_ci
220062306a36Sopenharmony_cistatic int ccdc_enum_frame_size(struct v4l2_subdev *sd,
220162306a36Sopenharmony_ci				struct v4l2_subdev_state *sd_state,
220262306a36Sopenharmony_ci				struct v4l2_subdev_frame_size_enum *fse)
220362306a36Sopenharmony_ci{
220462306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
220562306a36Sopenharmony_ci	struct v4l2_mbus_framefmt format;
220662306a36Sopenharmony_ci
220762306a36Sopenharmony_ci	if (fse->index != 0)
220862306a36Sopenharmony_ci		return -EINVAL;
220962306a36Sopenharmony_ci
221062306a36Sopenharmony_ci	format.code = fse->code;
221162306a36Sopenharmony_ci	format.width = 1;
221262306a36Sopenharmony_ci	format.height = 1;
221362306a36Sopenharmony_ci	ccdc_try_format(ccdc, sd_state, fse->pad, &format, fse->which);
221462306a36Sopenharmony_ci	fse->min_width = format.width;
221562306a36Sopenharmony_ci	fse->min_height = format.height;
221662306a36Sopenharmony_ci
221762306a36Sopenharmony_ci	if (format.code != fse->code)
221862306a36Sopenharmony_ci		return -EINVAL;
221962306a36Sopenharmony_ci
222062306a36Sopenharmony_ci	format.code = fse->code;
222162306a36Sopenharmony_ci	format.width = -1;
222262306a36Sopenharmony_ci	format.height = -1;
222362306a36Sopenharmony_ci	ccdc_try_format(ccdc, sd_state, fse->pad, &format, fse->which);
222462306a36Sopenharmony_ci	fse->max_width = format.width;
222562306a36Sopenharmony_ci	fse->max_height = format.height;
222662306a36Sopenharmony_ci
222762306a36Sopenharmony_ci	return 0;
222862306a36Sopenharmony_ci}
222962306a36Sopenharmony_ci
223062306a36Sopenharmony_ci/*
223162306a36Sopenharmony_ci * ccdc_get_selection - Retrieve a selection rectangle on a pad
223262306a36Sopenharmony_ci * @sd: ISP CCDC V4L2 subdevice
223362306a36Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
223462306a36Sopenharmony_ci * @sel: Selection rectangle
223562306a36Sopenharmony_ci *
223662306a36Sopenharmony_ci * The only supported rectangles are the crop rectangles on the output formatter
223762306a36Sopenharmony_ci * source pad.
223862306a36Sopenharmony_ci *
223962306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise.
224062306a36Sopenharmony_ci */
224162306a36Sopenharmony_cistatic int ccdc_get_selection(struct v4l2_subdev *sd,
224262306a36Sopenharmony_ci			      struct v4l2_subdev_state *sd_state,
224362306a36Sopenharmony_ci			      struct v4l2_subdev_selection *sel)
224462306a36Sopenharmony_ci{
224562306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
224662306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
224762306a36Sopenharmony_ci
224862306a36Sopenharmony_ci	if (sel->pad != CCDC_PAD_SOURCE_OF)
224962306a36Sopenharmony_ci		return -EINVAL;
225062306a36Sopenharmony_ci
225162306a36Sopenharmony_ci	switch (sel->target) {
225262306a36Sopenharmony_ci	case V4L2_SEL_TGT_CROP_BOUNDS:
225362306a36Sopenharmony_ci		sel->r.left = 0;
225462306a36Sopenharmony_ci		sel->r.top = 0;
225562306a36Sopenharmony_ci		sel->r.width = INT_MAX;
225662306a36Sopenharmony_ci		sel->r.height = INT_MAX;
225762306a36Sopenharmony_ci
225862306a36Sopenharmony_ci		format = __ccdc_get_format(ccdc, sd_state, CCDC_PAD_SINK,
225962306a36Sopenharmony_ci					   sel->which);
226062306a36Sopenharmony_ci		ccdc_try_crop(ccdc, format, &sel->r);
226162306a36Sopenharmony_ci		break;
226262306a36Sopenharmony_ci
226362306a36Sopenharmony_ci	case V4L2_SEL_TGT_CROP:
226462306a36Sopenharmony_ci		sel->r = *__ccdc_get_crop(ccdc, sd_state, sel->which);
226562306a36Sopenharmony_ci		break;
226662306a36Sopenharmony_ci
226762306a36Sopenharmony_ci	default:
226862306a36Sopenharmony_ci		return -EINVAL;
226962306a36Sopenharmony_ci	}
227062306a36Sopenharmony_ci
227162306a36Sopenharmony_ci	return 0;
227262306a36Sopenharmony_ci}
227362306a36Sopenharmony_ci
227462306a36Sopenharmony_ci/*
227562306a36Sopenharmony_ci * ccdc_set_selection - Set a selection rectangle on a pad
227662306a36Sopenharmony_ci * @sd: ISP CCDC V4L2 subdevice
227762306a36Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
227862306a36Sopenharmony_ci * @sel: Selection rectangle
227962306a36Sopenharmony_ci *
228062306a36Sopenharmony_ci * The only supported rectangle is the actual crop rectangle on the output
228162306a36Sopenharmony_ci * formatter source pad.
228262306a36Sopenharmony_ci *
228362306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise.
228462306a36Sopenharmony_ci */
228562306a36Sopenharmony_cistatic int ccdc_set_selection(struct v4l2_subdev *sd,
228662306a36Sopenharmony_ci			      struct v4l2_subdev_state *sd_state,
228762306a36Sopenharmony_ci			      struct v4l2_subdev_selection *sel)
228862306a36Sopenharmony_ci{
228962306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
229062306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
229162306a36Sopenharmony_ci
229262306a36Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP ||
229362306a36Sopenharmony_ci	    sel->pad != CCDC_PAD_SOURCE_OF)
229462306a36Sopenharmony_ci		return -EINVAL;
229562306a36Sopenharmony_ci
229662306a36Sopenharmony_ci	/* The crop rectangle can't be changed while streaming. */
229762306a36Sopenharmony_ci	if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
229862306a36Sopenharmony_ci		return -EBUSY;
229962306a36Sopenharmony_ci
230062306a36Sopenharmony_ci	/* Modifying the crop rectangle always changes the format on the source
230162306a36Sopenharmony_ci	 * pad. If the KEEP_CONFIG flag is set, just return the current crop
230262306a36Sopenharmony_ci	 * rectangle.
230362306a36Sopenharmony_ci	 */
230462306a36Sopenharmony_ci	if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
230562306a36Sopenharmony_ci		sel->r = *__ccdc_get_crop(ccdc, sd_state, sel->which);
230662306a36Sopenharmony_ci		return 0;
230762306a36Sopenharmony_ci	}
230862306a36Sopenharmony_ci
230962306a36Sopenharmony_ci	format = __ccdc_get_format(ccdc, sd_state, CCDC_PAD_SINK, sel->which);
231062306a36Sopenharmony_ci	ccdc_try_crop(ccdc, format, &sel->r);
231162306a36Sopenharmony_ci	*__ccdc_get_crop(ccdc, sd_state, sel->which) = sel->r;
231262306a36Sopenharmony_ci
231362306a36Sopenharmony_ci	/* Update the source format. */
231462306a36Sopenharmony_ci	format = __ccdc_get_format(ccdc, sd_state, CCDC_PAD_SOURCE_OF,
231562306a36Sopenharmony_ci				   sel->which);
231662306a36Sopenharmony_ci	ccdc_try_format(ccdc, sd_state, CCDC_PAD_SOURCE_OF, format,
231762306a36Sopenharmony_ci			sel->which);
231862306a36Sopenharmony_ci
231962306a36Sopenharmony_ci	return 0;
232062306a36Sopenharmony_ci}
232162306a36Sopenharmony_ci
232262306a36Sopenharmony_ci/*
232362306a36Sopenharmony_ci * ccdc_get_format - Retrieve the video format on a pad
232462306a36Sopenharmony_ci * @sd : ISP CCDC V4L2 subdevice
232562306a36Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
232662306a36Sopenharmony_ci * @fmt: Format
232762306a36Sopenharmony_ci *
232862306a36Sopenharmony_ci * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
232962306a36Sopenharmony_ci * to the format type.
233062306a36Sopenharmony_ci */
233162306a36Sopenharmony_cistatic int ccdc_get_format(struct v4l2_subdev *sd,
233262306a36Sopenharmony_ci			   struct v4l2_subdev_state *sd_state,
233362306a36Sopenharmony_ci			   struct v4l2_subdev_format *fmt)
233462306a36Sopenharmony_ci{
233562306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
233662306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
233762306a36Sopenharmony_ci
233862306a36Sopenharmony_ci	format = __ccdc_get_format(ccdc, sd_state, fmt->pad, fmt->which);
233962306a36Sopenharmony_ci	if (format == NULL)
234062306a36Sopenharmony_ci		return -EINVAL;
234162306a36Sopenharmony_ci
234262306a36Sopenharmony_ci	fmt->format = *format;
234362306a36Sopenharmony_ci	return 0;
234462306a36Sopenharmony_ci}
234562306a36Sopenharmony_ci
234662306a36Sopenharmony_ci/*
234762306a36Sopenharmony_ci * ccdc_set_format - Set the video format on a pad
234862306a36Sopenharmony_ci * @sd : ISP CCDC V4L2 subdevice
234962306a36Sopenharmony_ci * @cfg: V4L2 subdev pad configuration
235062306a36Sopenharmony_ci * @fmt: Format
235162306a36Sopenharmony_ci *
235262306a36Sopenharmony_ci * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
235362306a36Sopenharmony_ci * to the format type.
235462306a36Sopenharmony_ci */
235562306a36Sopenharmony_cistatic int ccdc_set_format(struct v4l2_subdev *sd,
235662306a36Sopenharmony_ci			   struct v4l2_subdev_state *sd_state,
235762306a36Sopenharmony_ci			   struct v4l2_subdev_format *fmt)
235862306a36Sopenharmony_ci{
235962306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
236062306a36Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
236162306a36Sopenharmony_ci	struct v4l2_rect *crop;
236262306a36Sopenharmony_ci
236362306a36Sopenharmony_ci	format = __ccdc_get_format(ccdc, sd_state, fmt->pad, fmt->which);
236462306a36Sopenharmony_ci	if (format == NULL)
236562306a36Sopenharmony_ci		return -EINVAL;
236662306a36Sopenharmony_ci
236762306a36Sopenharmony_ci	ccdc_try_format(ccdc, sd_state, fmt->pad, &fmt->format, fmt->which);
236862306a36Sopenharmony_ci	*format = fmt->format;
236962306a36Sopenharmony_ci
237062306a36Sopenharmony_ci	/* Propagate the format from sink to source */
237162306a36Sopenharmony_ci	if (fmt->pad == CCDC_PAD_SINK) {
237262306a36Sopenharmony_ci		/* Reset the crop rectangle. */
237362306a36Sopenharmony_ci		crop = __ccdc_get_crop(ccdc, sd_state, fmt->which);
237462306a36Sopenharmony_ci		crop->left = 0;
237562306a36Sopenharmony_ci		crop->top = 0;
237662306a36Sopenharmony_ci		crop->width = fmt->format.width;
237762306a36Sopenharmony_ci		crop->height = fmt->format.height;
237862306a36Sopenharmony_ci
237962306a36Sopenharmony_ci		ccdc_try_crop(ccdc, &fmt->format, crop);
238062306a36Sopenharmony_ci
238162306a36Sopenharmony_ci		/* Update the source formats. */
238262306a36Sopenharmony_ci		format = __ccdc_get_format(ccdc, sd_state, CCDC_PAD_SOURCE_OF,
238362306a36Sopenharmony_ci					   fmt->which);
238462306a36Sopenharmony_ci		*format = fmt->format;
238562306a36Sopenharmony_ci		ccdc_try_format(ccdc, sd_state, CCDC_PAD_SOURCE_OF, format,
238662306a36Sopenharmony_ci				fmt->which);
238762306a36Sopenharmony_ci
238862306a36Sopenharmony_ci		format = __ccdc_get_format(ccdc, sd_state, CCDC_PAD_SOURCE_VP,
238962306a36Sopenharmony_ci					   fmt->which);
239062306a36Sopenharmony_ci		*format = fmt->format;
239162306a36Sopenharmony_ci		ccdc_try_format(ccdc, sd_state, CCDC_PAD_SOURCE_VP, format,
239262306a36Sopenharmony_ci				fmt->which);
239362306a36Sopenharmony_ci	}
239462306a36Sopenharmony_ci
239562306a36Sopenharmony_ci	return 0;
239662306a36Sopenharmony_ci}
239762306a36Sopenharmony_ci
239862306a36Sopenharmony_ci/*
239962306a36Sopenharmony_ci * Decide whether desired output pixel code can be obtained with
240062306a36Sopenharmony_ci * the lane shifter by shifting the input pixel code.
240162306a36Sopenharmony_ci * @in: input pixelcode to shifter
240262306a36Sopenharmony_ci * @out: output pixelcode from shifter
240362306a36Sopenharmony_ci * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
240462306a36Sopenharmony_ci *
240562306a36Sopenharmony_ci * return true if the combination is possible
240662306a36Sopenharmony_ci * return false otherwise
240762306a36Sopenharmony_ci */
240862306a36Sopenharmony_cistatic bool ccdc_is_shiftable(u32 in, u32 out, unsigned int additional_shift)
240962306a36Sopenharmony_ci{
241062306a36Sopenharmony_ci	const struct isp_format_info *in_info, *out_info;
241162306a36Sopenharmony_ci
241262306a36Sopenharmony_ci	if (in == out)
241362306a36Sopenharmony_ci		return true;
241462306a36Sopenharmony_ci
241562306a36Sopenharmony_ci	in_info = omap3isp_video_format_info(in);
241662306a36Sopenharmony_ci	out_info = omap3isp_video_format_info(out);
241762306a36Sopenharmony_ci
241862306a36Sopenharmony_ci	if ((in_info->flavor == 0) || (out_info->flavor == 0))
241962306a36Sopenharmony_ci		return false;
242062306a36Sopenharmony_ci
242162306a36Sopenharmony_ci	if (in_info->flavor != out_info->flavor)
242262306a36Sopenharmony_ci		return false;
242362306a36Sopenharmony_ci
242462306a36Sopenharmony_ci	return in_info->width - out_info->width + additional_shift <= 6;
242562306a36Sopenharmony_ci}
242662306a36Sopenharmony_ci
242762306a36Sopenharmony_cistatic int ccdc_link_validate(struct v4l2_subdev *sd,
242862306a36Sopenharmony_ci			      struct media_link *link,
242962306a36Sopenharmony_ci			      struct v4l2_subdev_format *source_fmt,
243062306a36Sopenharmony_ci			      struct v4l2_subdev_format *sink_fmt)
243162306a36Sopenharmony_ci{
243262306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
243362306a36Sopenharmony_ci	unsigned long parallel_shift;
243462306a36Sopenharmony_ci
243562306a36Sopenharmony_ci	/* Check if the two ends match */
243662306a36Sopenharmony_ci	if (source_fmt->format.width != sink_fmt->format.width ||
243762306a36Sopenharmony_ci	    source_fmt->format.height != sink_fmt->format.height)
243862306a36Sopenharmony_ci		return -EPIPE;
243962306a36Sopenharmony_ci
244062306a36Sopenharmony_ci	/* We've got a parallel sensor here. */
244162306a36Sopenharmony_ci	if (ccdc->input == CCDC_INPUT_PARALLEL) {
244262306a36Sopenharmony_ci		struct v4l2_subdev *sd =
244362306a36Sopenharmony_ci			media_entity_to_v4l2_subdev(link->source->entity);
244462306a36Sopenharmony_ci		struct isp_bus_cfg *bus_cfg;
244562306a36Sopenharmony_ci
244662306a36Sopenharmony_ci		bus_cfg = v4l2_subdev_to_bus_cfg(sd);
244762306a36Sopenharmony_ci		if (WARN_ON(!bus_cfg))
244862306a36Sopenharmony_ci			return -EPIPE;
244962306a36Sopenharmony_ci
245062306a36Sopenharmony_ci		parallel_shift = bus_cfg->bus.parallel.data_lane_shift;
245162306a36Sopenharmony_ci	} else {
245262306a36Sopenharmony_ci		parallel_shift = 0;
245362306a36Sopenharmony_ci	}
245462306a36Sopenharmony_ci
245562306a36Sopenharmony_ci	/* Lane shifter may be used to drop bits on CCDC sink pad */
245662306a36Sopenharmony_ci	if (!ccdc_is_shiftable(source_fmt->format.code,
245762306a36Sopenharmony_ci			       sink_fmt->format.code, parallel_shift))
245862306a36Sopenharmony_ci		return -EPIPE;
245962306a36Sopenharmony_ci
246062306a36Sopenharmony_ci	return 0;
246162306a36Sopenharmony_ci}
246262306a36Sopenharmony_ci
246362306a36Sopenharmony_ci/*
246462306a36Sopenharmony_ci * ccdc_init_formats - Initialize formats on all pads
246562306a36Sopenharmony_ci * @sd: ISP CCDC V4L2 subdevice
246662306a36Sopenharmony_ci * @fh: V4L2 subdev file handle
246762306a36Sopenharmony_ci *
246862306a36Sopenharmony_ci * Initialize all pad formats with default values. If fh is not NULL, try
246962306a36Sopenharmony_ci * formats are initialized on the file handle. Otherwise active formats are
247062306a36Sopenharmony_ci * initialized on the device.
247162306a36Sopenharmony_ci */
247262306a36Sopenharmony_cistatic int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
247362306a36Sopenharmony_ci{
247462306a36Sopenharmony_ci	struct v4l2_subdev_format format;
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_ci	memset(&format, 0, sizeof(format));
247762306a36Sopenharmony_ci	format.pad = CCDC_PAD_SINK;
247862306a36Sopenharmony_ci	format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
247962306a36Sopenharmony_ci	format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
248062306a36Sopenharmony_ci	format.format.width = 4096;
248162306a36Sopenharmony_ci	format.format.height = 4096;
248262306a36Sopenharmony_ci	ccdc_set_format(sd, fh ? fh->state : NULL, &format);
248362306a36Sopenharmony_ci
248462306a36Sopenharmony_ci	return 0;
248562306a36Sopenharmony_ci}
248662306a36Sopenharmony_ci
248762306a36Sopenharmony_ci/* V4L2 subdev core operations */
248862306a36Sopenharmony_cistatic const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
248962306a36Sopenharmony_ci	.ioctl = ccdc_ioctl,
249062306a36Sopenharmony_ci	.subscribe_event = ccdc_subscribe_event,
249162306a36Sopenharmony_ci	.unsubscribe_event = ccdc_unsubscribe_event,
249262306a36Sopenharmony_ci};
249362306a36Sopenharmony_ci
249462306a36Sopenharmony_ci/* V4L2 subdev video operations */
249562306a36Sopenharmony_cistatic const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
249662306a36Sopenharmony_ci	.s_stream = ccdc_set_stream,
249762306a36Sopenharmony_ci};
249862306a36Sopenharmony_ci
249962306a36Sopenharmony_ci/* V4L2 subdev pad operations */
250062306a36Sopenharmony_cistatic const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
250162306a36Sopenharmony_ci	.enum_mbus_code = ccdc_enum_mbus_code,
250262306a36Sopenharmony_ci	.enum_frame_size = ccdc_enum_frame_size,
250362306a36Sopenharmony_ci	.get_fmt = ccdc_get_format,
250462306a36Sopenharmony_ci	.set_fmt = ccdc_set_format,
250562306a36Sopenharmony_ci	.get_selection = ccdc_get_selection,
250662306a36Sopenharmony_ci	.set_selection = ccdc_set_selection,
250762306a36Sopenharmony_ci	.link_validate = ccdc_link_validate,
250862306a36Sopenharmony_ci};
250962306a36Sopenharmony_ci
251062306a36Sopenharmony_ci/* V4L2 subdev operations */
251162306a36Sopenharmony_cistatic const struct v4l2_subdev_ops ccdc_v4l2_ops = {
251262306a36Sopenharmony_ci	.core = &ccdc_v4l2_core_ops,
251362306a36Sopenharmony_ci	.video = &ccdc_v4l2_video_ops,
251462306a36Sopenharmony_ci	.pad = &ccdc_v4l2_pad_ops,
251562306a36Sopenharmony_ci};
251662306a36Sopenharmony_ci
251762306a36Sopenharmony_ci/* V4L2 subdev internal operations */
251862306a36Sopenharmony_cistatic const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
251962306a36Sopenharmony_ci	.open = ccdc_init_formats,
252062306a36Sopenharmony_ci};
252162306a36Sopenharmony_ci
252262306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
252362306a36Sopenharmony_ci * Media entity operations
252462306a36Sopenharmony_ci */
252562306a36Sopenharmony_ci
252662306a36Sopenharmony_ci/*
252762306a36Sopenharmony_ci * ccdc_link_setup - Setup CCDC connections
252862306a36Sopenharmony_ci * @entity: CCDC media entity
252962306a36Sopenharmony_ci * @local: Pad at the local end of the link
253062306a36Sopenharmony_ci * @remote: Pad at the remote end of the link
253162306a36Sopenharmony_ci * @flags: Link flags
253262306a36Sopenharmony_ci *
253362306a36Sopenharmony_ci * return -EINVAL or zero on success
253462306a36Sopenharmony_ci */
253562306a36Sopenharmony_cistatic int ccdc_link_setup(struct media_entity *entity,
253662306a36Sopenharmony_ci			   const struct media_pad *local,
253762306a36Sopenharmony_ci			   const struct media_pad *remote, u32 flags)
253862306a36Sopenharmony_ci{
253962306a36Sopenharmony_ci	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
254062306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
254162306a36Sopenharmony_ci	struct isp_device *isp = to_isp_device(ccdc);
254262306a36Sopenharmony_ci	unsigned int index = local->index;
254362306a36Sopenharmony_ci
254462306a36Sopenharmony_ci	/* FIXME: this is actually a hack! */
254562306a36Sopenharmony_ci	if (is_media_entity_v4l2_subdev(remote->entity))
254662306a36Sopenharmony_ci		index |= 2 << 16;
254762306a36Sopenharmony_ci
254862306a36Sopenharmony_ci	switch (index) {
254962306a36Sopenharmony_ci	case CCDC_PAD_SINK | 2 << 16:
255062306a36Sopenharmony_ci		/* Read from the sensor (parallel interface), CCP2, CSI2a or
255162306a36Sopenharmony_ci		 * CSI2c.
255262306a36Sopenharmony_ci		 */
255362306a36Sopenharmony_ci		if (!(flags & MEDIA_LNK_FL_ENABLED)) {
255462306a36Sopenharmony_ci			ccdc->input = CCDC_INPUT_NONE;
255562306a36Sopenharmony_ci			break;
255662306a36Sopenharmony_ci		}
255762306a36Sopenharmony_ci
255862306a36Sopenharmony_ci		if (ccdc->input != CCDC_INPUT_NONE)
255962306a36Sopenharmony_ci			return -EBUSY;
256062306a36Sopenharmony_ci
256162306a36Sopenharmony_ci		if (remote->entity == &isp->isp_ccp2.subdev.entity)
256262306a36Sopenharmony_ci			ccdc->input = CCDC_INPUT_CCP2B;
256362306a36Sopenharmony_ci		else if (remote->entity == &isp->isp_csi2a.subdev.entity)
256462306a36Sopenharmony_ci			ccdc->input = CCDC_INPUT_CSI2A;
256562306a36Sopenharmony_ci		else if (remote->entity == &isp->isp_csi2c.subdev.entity)
256662306a36Sopenharmony_ci			ccdc->input = CCDC_INPUT_CSI2C;
256762306a36Sopenharmony_ci		else
256862306a36Sopenharmony_ci			ccdc->input = CCDC_INPUT_PARALLEL;
256962306a36Sopenharmony_ci
257062306a36Sopenharmony_ci		break;
257162306a36Sopenharmony_ci
257262306a36Sopenharmony_ci	/*
257362306a36Sopenharmony_ci	 * The ISP core doesn't support pipelines with multiple video outputs.
257462306a36Sopenharmony_ci	 * Revisit this when it will be implemented, and return -EBUSY for now.
257562306a36Sopenharmony_ci	 */
257662306a36Sopenharmony_ci
257762306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_VP | 2 << 16:
257862306a36Sopenharmony_ci		/* Write to preview engine, histogram and H3A. When none of
257962306a36Sopenharmony_ci		 * those links are active, the video port can be disabled.
258062306a36Sopenharmony_ci		 */
258162306a36Sopenharmony_ci		if (flags & MEDIA_LNK_FL_ENABLED) {
258262306a36Sopenharmony_ci			if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
258362306a36Sopenharmony_ci				return -EBUSY;
258462306a36Sopenharmony_ci			ccdc->output |= CCDC_OUTPUT_PREVIEW;
258562306a36Sopenharmony_ci		} else {
258662306a36Sopenharmony_ci			ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
258762306a36Sopenharmony_ci		}
258862306a36Sopenharmony_ci		break;
258962306a36Sopenharmony_ci
259062306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_OF:
259162306a36Sopenharmony_ci		/* Write to memory */
259262306a36Sopenharmony_ci		if (flags & MEDIA_LNK_FL_ENABLED) {
259362306a36Sopenharmony_ci			if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
259462306a36Sopenharmony_ci				return -EBUSY;
259562306a36Sopenharmony_ci			ccdc->output |= CCDC_OUTPUT_MEMORY;
259662306a36Sopenharmony_ci		} else {
259762306a36Sopenharmony_ci			ccdc->output &= ~CCDC_OUTPUT_MEMORY;
259862306a36Sopenharmony_ci		}
259962306a36Sopenharmony_ci		break;
260062306a36Sopenharmony_ci
260162306a36Sopenharmony_ci	case CCDC_PAD_SOURCE_OF | 2 << 16:
260262306a36Sopenharmony_ci		/* Write to resizer */
260362306a36Sopenharmony_ci		if (flags & MEDIA_LNK_FL_ENABLED) {
260462306a36Sopenharmony_ci			if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
260562306a36Sopenharmony_ci				return -EBUSY;
260662306a36Sopenharmony_ci			ccdc->output |= CCDC_OUTPUT_RESIZER;
260762306a36Sopenharmony_ci		} else {
260862306a36Sopenharmony_ci			ccdc->output &= ~CCDC_OUTPUT_RESIZER;
260962306a36Sopenharmony_ci		}
261062306a36Sopenharmony_ci		break;
261162306a36Sopenharmony_ci
261262306a36Sopenharmony_ci	default:
261362306a36Sopenharmony_ci		return -EINVAL;
261462306a36Sopenharmony_ci	}
261562306a36Sopenharmony_ci
261662306a36Sopenharmony_ci	return 0;
261762306a36Sopenharmony_ci}
261862306a36Sopenharmony_ci
261962306a36Sopenharmony_ci/* media operations */
262062306a36Sopenharmony_cistatic const struct media_entity_operations ccdc_media_ops = {
262162306a36Sopenharmony_ci	.link_setup = ccdc_link_setup,
262262306a36Sopenharmony_ci	.link_validate = v4l2_subdev_link_validate,
262362306a36Sopenharmony_ci};
262462306a36Sopenharmony_ci
262562306a36Sopenharmony_civoid omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
262662306a36Sopenharmony_ci{
262762306a36Sopenharmony_ci	v4l2_device_unregister_subdev(&ccdc->subdev);
262862306a36Sopenharmony_ci	omap3isp_video_unregister(&ccdc->video_out);
262962306a36Sopenharmony_ci}
263062306a36Sopenharmony_ci
263162306a36Sopenharmony_ciint omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
263262306a36Sopenharmony_ci	struct v4l2_device *vdev)
263362306a36Sopenharmony_ci{
263462306a36Sopenharmony_ci	int ret;
263562306a36Sopenharmony_ci
263662306a36Sopenharmony_ci	/* Register the subdev and video node. */
263762306a36Sopenharmony_ci	ccdc->subdev.dev = vdev->mdev->dev;
263862306a36Sopenharmony_ci	ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
263962306a36Sopenharmony_ci	if (ret < 0)
264062306a36Sopenharmony_ci		goto error;
264162306a36Sopenharmony_ci
264262306a36Sopenharmony_ci	ret = omap3isp_video_register(&ccdc->video_out, vdev);
264362306a36Sopenharmony_ci	if (ret < 0)
264462306a36Sopenharmony_ci		goto error;
264562306a36Sopenharmony_ci
264662306a36Sopenharmony_ci	return 0;
264762306a36Sopenharmony_ci
264862306a36Sopenharmony_cierror:
264962306a36Sopenharmony_ci	omap3isp_ccdc_unregister_entities(ccdc);
265062306a36Sopenharmony_ci	return ret;
265162306a36Sopenharmony_ci}
265262306a36Sopenharmony_ci
265362306a36Sopenharmony_ci/* -----------------------------------------------------------------------------
265462306a36Sopenharmony_ci * ISP CCDC initialisation and cleanup
265562306a36Sopenharmony_ci */
265662306a36Sopenharmony_ci
265762306a36Sopenharmony_ci/*
265862306a36Sopenharmony_ci * ccdc_init_entities - Initialize V4L2 subdev and media entity
265962306a36Sopenharmony_ci * @ccdc: ISP CCDC module
266062306a36Sopenharmony_ci *
266162306a36Sopenharmony_ci * Return 0 on success and a negative error code on failure.
266262306a36Sopenharmony_ci */
266362306a36Sopenharmony_cistatic int ccdc_init_entities(struct isp_ccdc_device *ccdc)
266462306a36Sopenharmony_ci{
266562306a36Sopenharmony_ci	struct v4l2_subdev *sd = &ccdc->subdev;
266662306a36Sopenharmony_ci	struct media_pad *pads = ccdc->pads;
266762306a36Sopenharmony_ci	struct media_entity *me = &sd->entity;
266862306a36Sopenharmony_ci	int ret;
266962306a36Sopenharmony_ci
267062306a36Sopenharmony_ci	ccdc->input = CCDC_INPUT_NONE;
267162306a36Sopenharmony_ci
267262306a36Sopenharmony_ci	v4l2_subdev_init(sd, &ccdc_v4l2_ops);
267362306a36Sopenharmony_ci	sd->internal_ops = &ccdc_v4l2_internal_ops;
267462306a36Sopenharmony_ci	strscpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
267562306a36Sopenharmony_ci	sd->grp_id = 1 << 16;	/* group ID for isp subdevs */
267662306a36Sopenharmony_ci	v4l2_set_subdevdata(sd, ccdc);
267762306a36Sopenharmony_ci	sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
267862306a36Sopenharmony_ci
267962306a36Sopenharmony_ci	pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK
268062306a36Sopenharmony_ci				    | MEDIA_PAD_FL_MUST_CONNECT;
268162306a36Sopenharmony_ci	pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
268262306a36Sopenharmony_ci	pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
268362306a36Sopenharmony_ci
268462306a36Sopenharmony_ci	me->ops = &ccdc_media_ops;
268562306a36Sopenharmony_ci	ret = media_entity_pads_init(me, CCDC_PADS_NUM, pads);
268662306a36Sopenharmony_ci	if (ret < 0)
268762306a36Sopenharmony_ci		return ret;
268862306a36Sopenharmony_ci
268962306a36Sopenharmony_ci	ccdc_init_formats(sd, NULL);
269062306a36Sopenharmony_ci
269162306a36Sopenharmony_ci	ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
269262306a36Sopenharmony_ci	ccdc->video_out.ops = &ccdc_video_ops;
269362306a36Sopenharmony_ci	ccdc->video_out.isp = to_isp_device(ccdc);
269462306a36Sopenharmony_ci	ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
269562306a36Sopenharmony_ci	ccdc->video_out.bpl_alignment = 32;
269662306a36Sopenharmony_ci
269762306a36Sopenharmony_ci	ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
269862306a36Sopenharmony_ci	if (ret < 0)
269962306a36Sopenharmony_ci		goto error;
270062306a36Sopenharmony_ci
270162306a36Sopenharmony_ci	return 0;
270262306a36Sopenharmony_ci
270362306a36Sopenharmony_cierror:
270462306a36Sopenharmony_ci	media_entity_cleanup(me);
270562306a36Sopenharmony_ci	return ret;
270662306a36Sopenharmony_ci}
270762306a36Sopenharmony_ci
270862306a36Sopenharmony_ci/*
270962306a36Sopenharmony_ci * omap3isp_ccdc_init - CCDC module initialization.
271062306a36Sopenharmony_ci * @isp: Device pointer specific to the OMAP3 ISP.
271162306a36Sopenharmony_ci *
271262306a36Sopenharmony_ci * TODO: Get the initialisation values from platform data.
271362306a36Sopenharmony_ci *
271462306a36Sopenharmony_ci * Return 0 on success or a negative error code otherwise.
271562306a36Sopenharmony_ci */
271662306a36Sopenharmony_ciint omap3isp_ccdc_init(struct isp_device *isp)
271762306a36Sopenharmony_ci{
271862306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
271962306a36Sopenharmony_ci	int ret;
272062306a36Sopenharmony_ci
272162306a36Sopenharmony_ci	spin_lock_init(&ccdc->lock);
272262306a36Sopenharmony_ci	init_waitqueue_head(&ccdc->wait);
272362306a36Sopenharmony_ci	mutex_init(&ccdc->ioctl_lock);
272462306a36Sopenharmony_ci
272562306a36Sopenharmony_ci	ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
272662306a36Sopenharmony_ci
272762306a36Sopenharmony_ci	INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
272862306a36Sopenharmony_ci	ccdc->lsc.state = LSC_STATE_STOPPED;
272962306a36Sopenharmony_ci	INIT_LIST_HEAD(&ccdc->lsc.free_queue);
273062306a36Sopenharmony_ci	spin_lock_init(&ccdc->lsc.req_lock);
273162306a36Sopenharmony_ci
273262306a36Sopenharmony_ci	ccdc->clamp.oblen = 0;
273362306a36Sopenharmony_ci	ccdc->clamp.dcsubval = 0;
273462306a36Sopenharmony_ci
273562306a36Sopenharmony_ci	ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
273662306a36Sopenharmony_ci	ccdc_apply_controls(ccdc);
273762306a36Sopenharmony_ci
273862306a36Sopenharmony_ci	ret = ccdc_init_entities(ccdc);
273962306a36Sopenharmony_ci	if (ret < 0) {
274062306a36Sopenharmony_ci		mutex_destroy(&ccdc->ioctl_lock);
274162306a36Sopenharmony_ci		return ret;
274262306a36Sopenharmony_ci	}
274362306a36Sopenharmony_ci
274462306a36Sopenharmony_ci	return 0;
274562306a36Sopenharmony_ci}
274662306a36Sopenharmony_ci
274762306a36Sopenharmony_ci/*
274862306a36Sopenharmony_ci * omap3isp_ccdc_cleanup - CCDC module cleanup.
274962306a36Sopenharmony_ci * @isp: Device pointer specific to the OMAP3 ISP.
275062306a36Sopenharmony_ci */
275162306a36Sopenharmony_civoid omap3isp_ccdc_cleanup(struct isp_device *isp)
275262306a36Sopenharmony_ci{
275362306a36Sopenharmony_ci	struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
275462306a36Sopenharmony_ci
275562306a36Sopenharmony_ci	omap3isp_video_cleanup(&ccdc->video_out);
275662306a36Sopenharmony_ci	media_entity_cleanup(&ccdc->subdev.entity);
275762306a36Sopenharmony_ci
275862306a36Sopenharmony_ci	/* Free LSC requests. As the CCDC is stopped there's no active request,
275962306a36Sopenharmony_ci	 * so only the pending request and the free queue need to be handled.
276062306a36Sopenharmony_ci	 */
276162306a36Sopenharmony_ci	ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
276262306a36Sopenharmony_ci	cancel_work_sync(&ccdc->lsc.table_work);
276362306a36Sopenharmony_ci	ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
276462306a36Sopenharmony_ci
276562306a36Sopenharmony_ci	if (ccdc->fpc.addr != NULL)
276662306a36Sopenharmony_ci		dma_free_coherent(isp->dev, ccdc->fpc.fpnum * 4, ccdc->fpc.addr,
276762306a36Sopenharmony_ci				  ccdc->fpc.dma);
276862306a36Sopenharmony_ci
276962306a36Sopenharmony_ci	mutex_destroy(&ccdc->ioctl_lock);
277062306a36Sopenharmony_ci}
2771