4 * TI OMAP3 ISP - CCDC module
6 * Copyright (C) 2009-2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc.
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 #include <linux/module.h>
28 #include <linux/uaccess.h>
29 #include <linux/delay.h>
30 #include <linux/device.h>
31 #include <linux/dma-mapping.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <media/v4l2-event.h>
41 #define CCDC_MIN_WIDTH 32
42 #define CCDC_MIN_HEIGHT 32
44 static struct v4l2_mbus_framefmt *
45 __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
46 unsigned int pad, enum v4l2_subdev_format_whence which);
48 static const unsigned int ccdc_fmts[] = {
50 V4L2_MBUS_FMT_Y10_1X10,
51 V4L2_MBUS_FMT_Y12_1X12,
52 V4L2_MBUS_FMT_SGRBG8_1X8,
53 V4L2_MBUS_FMT_SRGGB8_1X8,
54 V4L2_MBUS_FMT_SBGGR8_1X8,
55 V4L2_MBUS_FMT_SGBRG8_1X8,
56 V4L2_MBUS_FMT_SGRBG10_1X10,
57 V4L2_MBUS_FMT_SRGGB10_1X10,
58 V4L2_MBUS_FMT_SBGGR10_1X10,
59 V4L2_MBUS_FMT_SGBRG10_1X10,
60 V4L2_MBUS_FMT_SGRBG12_1X12,
61 V4L2_MBUS_FMT_SRGGB12_1X12,
62 V4L2_MBUS_FMT_SBGGR12_1X12,
63 V4L2_MBUS_FMT_SGBRG12_1X12,
67 * ccdc_print_status - Print current CCDC Module register values.
68 * @ccdc: Pointer to ISP CCDC device.
70 * Also prints other debug information stored in the CCDC module.
72 #define CCDC_PRINT_REGISTER(isp, name)\
73 dev_dbg(isp->dev, "###CCDC " #name "=0x%08x\n", \
74 isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_##name))
76 static void ccdc_print_status(struct isp_ccdc_device *ccdc)
78 struct isp_device *isp = to_isp_device(ccdc);
80 dev_dbg(isp->dev, "-------------CCDC Register dump-------------\n");
82 CCDC_PRINT_REGISTER(isp, PCR);
83 CCDC_PRINT_REGISTER(isp, SYN_MODE);
84 CCDC_PRINT_REGISTER(isp, HD_VD_WID);
85 CCDC_PRINT_REGISTER(isp, PIX_LINES);
86 CCDC_PRINT_REGISTER(isp, HORZ_INFO);
87 CCDC_PRINT_REGISTER(isp, VERT_START);
88 CCDC_PRINT_REGISTER(isp, VERT_LINES);
89 CCDC_PRINT_REGISTER(isp, CULLING);
90 CCDC_PRINT_REGISTER(isp, HSIZE_OFF);
91 CCDC_PRINT_REGISTER(isp, SDOFST);
92 CCDC_PRINT_REGISTER(isp, SDR_ADDR);
93 CCDC_PRINT_REGISTER(isp, CLAMP);
94 CCDC_PRINT_REGISTER(isp, DCSUB);
95 CCDC_PRINT_REGISTER(isp, COLPTN);
96 CCDC_PRINT_REGISTER(isp, BLKCMP);
97 CCDC_PRINT_REGISTER(isp, FPC);
98 CCDC_PRINT_REGISTER(isp, FPC_ADDR);
99 CCDC_PRINT_REGISTER(isp, VDINT);
100 CCDC_PRINT_REGISTER(isp, ALAW);
101 CCDC_PRINT_REGISTER(isp, REC656IF);
102 CCDC_PRINT_REGISTER(isp, CFG);
103 CCDC_PRINT_REGISTER(isp, FMTCFG);
104 CCDC_PRINT_REGISTER(isp, FMT_HORZ);
105 CCDC_PRINT_REGISTER(isp, FMT_VERT);
106 CCDC_PRINT_REGISTER(isp, PRGEVEN0);
107 CCDC_PRINT_REGISTER(isp, PRGEVEN1);
108 CCDC_PRINT_REGISTER(isp, PRGODD0);
109 CCDC_PRINT_REGISTER(isp, PRGODD1);
110 CCDC_PRINT_REGISTER(isp, VP_OUT);
111 CCDC_PRINT_REGISTER(isp, LSC_CONFIG);
112 CCDC_PRINT_REGISTER(isp, LSC_INITIAL);
113 CCDC_PRINT_REGISTER(isp, LSC_TABLE_BASE);
114 CCDC_PRINT_REGISTER(isp, LSC_TABLE_OFFSET);
116 dev_dbg(isp->dev, "--------------------------------------------\n");
120 * omap3isp_ccdc_busy - Get busy state of the CCDC.
121 * @ccdc: Pointer to ISP CCDC device.
123 int omap3isp_ccdc_busy(struct isp_ccdc_device *ccdc)
125 struct isp_device *isp = to_isp_device(ccdc);
127 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR) &
131 /* -----------------------------------------------------------------------------
132 * Lens Shading Compensation
136 * ccdc_lsc_validate_config - Check that LSC configuration is valid.
137 * @ccdc: Pointer to ISP CCDC device.
138 * @lsc_cfg: the LSC configuration to check.
140 * Returns 0 if the LSC configuration is valid, or -EINVAL if invalid.
142 static int ccdc_lsc_validate_config(struct isp_ccdc_device *ccdc,
143 struct omap3isp_ccdc_lsc_config *lsc_cfg)
145 struct isp_device *isp = to_isp_device(ccdc);
146 struct v4l2_mbus_framefmt *format;
147 unsigned int paxel_width, paxel_height;
148 unsigned int paxel_shift_x, paxel_shift_y;
149 unsigned int min_width, min_height, min_size;
150 unsigned int input_width, input_height;
152 paxel_shift_x = lsc_cfg->gain_mode_m;
153 paxel_shift_y = lsc_cfg->gain_mode_n;
155 if ((paxel_shift_x < 2) || (paxel_shift_x > 6) ||
156 (paxel_shift_y < 2) || (paxel_shift_y > 6)) {
157 dev_dbg(isp->dev, "CCDC: LSC: Invalid paxel size\n");
161 if (lsc_cfg->offset & 3) {
162 dev_dbg(isp->dev, "CCDC: LSC: Offset must be a multiple of "
167 if ((lsc_cfg->initial_x & 1) || (lsc_cfg->initial_y & 1)) {
168 dev_dbg(isp->dev, "CCDC: LSC: initial_x and y must be even\n");
172 format = __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
173 V4L2_SUBDEV_FORMAT_ACTIVE);
174 input_width = format->width;
175 input_height = format->height;
177 /* Calculate minimum bytesize for validation */
178 paxel_width = 1 << paxel_shift_x;
179 min_width = ((input_width + lsc_cfg->initial_x + paxel_width - 1)
180 >> paxel_shift_x) + 1;
182 paxel_height = 1 << paxel_shift_y;
183 min_height = ((input_height + lsc_cfg->initial_y + paxel_height - 1)
184 >> paxel_shift_y) + 1;
186 min_size = 4 * min_width * min_height;
187 if (min_size > lsc_cfg->size) {
188 dev_dbg(isp->dev, "CCDC: LSC: too small table\n");
191 if (lsc_cfg->offset < (min_width * 4)) {
192 dev_dbg(isp->dev, "CCDC: LSC: Offset is too small\n");
195 if ((lsc_cfg->size / lsc_cfg->offset) < min_height) {
196 dev_dbg(isp->dev, "CCDC: LSC: Wrong size/offset combination\n");
203 * ccdc_lsc_program_table - Program Lens Shading Compensation table address.
204 * @ccdc: Pointer to ISP CCDC device.
206 static void ccdc_lsc_program_table(struct isp_ccdc_device *ccdc, u32 addr)
208 isp_reg_writel(to_isp_device(ccdc), addr,
209 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_TABLE_BASE);
213 * ccdc_lsc_setup_regs - Configures the lens shading compensation module
214 * @ccdc: Pointer to ISP CCDC device.
216 static void ccdc_lsc_setup_regs(struct isp_ccdc_device *ccdc,
217 struct omap3isp_ccdc_lsc_config *cfg)
219 struct isp_device *isp = to_isp_device(ccdc);
222 isp_reg_writel(isp, cfg->offset, OMAP3_ISP_IOMEM_CCDC,
223 ISPCCDC_LSC_TABLE_OFFSET);
226 reg |= cfg->gain_mode_n << ISPCCDC_LSC_GAIN_MODE_N_SHIFT;
227 reg |= cfg->gain_mode_m << ISPCCDC_LSC_GAIN_MODE_M_SHIFT;
228 reg |= cfg->gain_format << ISPCCDC_LSC_GAIN_FORMAT_SHIFT;
229 isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG);
232 reg &= ~ISPCCDC_LSC_INITIAL_X_MASK;
233 reg |= cfg->initial_x << ISPCCDC_LSC_INITIAL_X_SHIFT;
234 reg &= ~ISPCCDC_LSC_INITIAL_Y_MASK;
235 reg |= cfg->initial_y << ISPCCDC_LSC_INITIAL_Y_SHIFT;
236 isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_CCDC,
237 ISPCCDC_LSC_INITIAL);
240 static int ccdc_lsc_wait_prefetch(struct isp_ccdc_device *ccdc)
242 struct isp_device *isp = to_isp_device(ccdc);
245 isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
246 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
249 for (wait = 0; wait < 1000; wait++) {
250 if (isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS) &
251 IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ) {
252 isp_reg_writel(isp, IRQ0STATUS_CCDC_LSC_PREF_COMP_IRQ,
253 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
265 * __ccdc_lsc_enable - Enables/Disables the Lens Shading Compensation module.
266 * @ccdc: Pointer to ISP CCDC device.
267 * @enable: 0 Disables LSC, 1 Enables LSC.
269 static int __ccdc_lsc_enable(struct isp_ccdc_device *ccdc, int enable)
271 struct isp_device *isp = to_isp_device(ccdc);
272 const struct v4l2_mbus_framefmt *format =
273 __ccdc_get_format(ccdc, NULL, CCDC_PAD_SINK,
274 V4L2_SUBDEV_FORMAT_ACTIVE);
276 if ((format->code != V4L2_MBUS_FMT_SGRBG10_1X10) &&
277 (format->code != V4L2_MBUS_FMT_SRGGB10_1X10) &&
278 (format->code != V4L2_MBUS_FMT_SBGGR10_1X10) &&
279 (format->code != V4L2_MBUS_FMT_SGBRG10_1X10))
283 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_LSC_READ);
285 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
286 ISPCCDC_LSC_ENABLE, enable ? ISPCCDC_LSC_ENABLE : 0);
289 if (ccdc_lsc_wait_prefetch(ccdc) < 0) {
290 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC,
291 ISPCCDC_LSC_CONFIG, ISPCCDC_LSC_ENABLE);
292 ccdc->lsc.state = LSC_STATE_STOPPED;
293 dev_warn(to_device(ccdc), "LSC prefecth timeout\n");
296 ccdc->lsc.state = LSC_STATE_RUNNING;
298 ccdc->lsc.state = LSC_STATE_STOPPING;
304 static int ccdc_lsc_busy(struct isp_ccdc_device *ccdc)
306 struct isp_device *isp = to_isp_device(ccdc);
308 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG) &
312 /* __ccdc_lsc_configure - Apply a new configuration to the LSC engine
313 * @ccdc: Pointer to ISP CCDC device
314 * @req: New configuration request
316 * context: in_interrupt()
318 static int __ccdc_lsc_configure(struct isp_ccdc_device *ccdc,
319 struct ispccdc_lsc_config_req *req)
324 if (ccdc_lsc_validate_config(ccdc, &req->config) < 0) {
325 dev_dbg(to_device(ccdc), "Discard LSC configuration\n");
329 if (ccdc_lsc_busy(ccdc))
332 ccdc_lsc_setup_regs(ccdc, &req->config);
333 ccdc_lsc_program_table(ccdc, req->table);
338 * ccdc_lsc_error_handler - Handle LSC prefetch error scenario.
339 * @ccdc: Pointer to ISP CCDC device.
341 * Disables LSC, and defers enablement to shadow registers update time.
343 static void ccdc_lsc_error_handler(struct isp_ccdc_device *ccdc)
345 struct isp_device *isp = to_isp_device(ccdc);
347 * From OMAP3 TRM: When this event is pending, the module
348 * goes into transparent mode (output =input). Normal
349 * operation can be resumed at the start of the next frame
351 * 1) Clearing this event
352 * 2) Disabling the LSC module
355 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_LSC_CONFIG,
357 ccdc->lsc.state = LSC_STATE_STOPPED;
360 static void ccdc_lsc_free_request(struct isp_ccdc_device *ccdc,
361 struct ispccdc_lsc_config_req *req)
363 struct isp_device *isp = to_isp_device(ccdc);
369 dma_unmap_sg(isp->dev, req->iovm->sgt->sgl,
370 req->iovm->sgt->nents, DMA_TO_DEVICE);
372 omap_iommu_vfree(isp->domain, isp->dev, req->table);
376 static void ccdc_lsc_free_queue(struct isp_ccdc_device *ccdc,
377 struct list_head *queue)
379 struct ispccdc_lsc_config_req *req, *n;
382 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
383 list_for_each_entry_safe(req, n, queue, list) {
384 list_del(&req->list);
385 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
386 ccdc_lsc_free_request(ccdc, req);
387 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
389 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
392 static void ccdc_lsc_free_table_work(struct work_struct *work)
394 struct isp_ccdc_device *ccdc;
395 struct ispccdc_lsc *lsc;
397 lsc = container_of(work, struct ispccdc_lsc, table_work);
398 ccdc = container_of(lsc, struct isp_ccdc_device, lsc);
400 ccdc_lsc_free_queue(ccdc, &lsc->free_queue);
404 * ccdc_lsc_config - Configure the LSC module from a userspace request
406 * Store the request LSC configuration in the LSC engine request pointer. The
407 * configuration will be applied to the hardware when the CCDC will be enabled,
408 * or at the next LSC interrupt if the CCDC is already running.
410 static int ccdc_lsc_config(struct isp_ccdc_device *ccdc,
411 struct omap3isp_ccdc_update_config *config)
413 struct isp_device *isp = to_isp_device(ccdc);
414 struct ispccdc_lsc_config_req *req;
420 update = config->update &
421 (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC);
425 if (update != (OMAP3ISP_CCDC_CONFIG_LSC | OMAP3ISP_CCDC_TBL_LSC)) {
426 dev_dbg(to_device(ccdc), "%s: Both LSC configuration and table "
427 "need to be supplied\n", __func__);
431 req = kzalloc(sizeof(*req), GFP_KERNEL);
435 if (config->flag & OMAP3ISP_CCDC_CONFIG_LSC) {
436 if (copy_from_user(&req->config, config->lsc_cfg,
437 sizeof(req->config))) {
444 req->table = omap_iommu_vmalloc(isp->domain, isp->dev, 0,
445 req->config.size, IOMMU_FLAG);
446 if (IS_ERR_VALUE(req->table)) {
452 req->iovm = omap_find_iovm_area(isp->dev, req->table);
453 if (req->iovm == NULL) {
458 if (!dma_map_sg(isp->dev, req->iovm->sgt->sgl,
459 req->iovm->sgt->nents, DMA_TO_DEVICE)) {
465 dma_sync_sg_for_cpu(isp->dev, req->iovm->sgt->sgl,
466 req->iovm->sgt->nents, DMA_TO_DEVICE);
468 table = omap_da_to_va(isp->dev, req->table);
469 if (copy_from_user(table, config->lsc, req->config.size)) {
474 dma_sync_sg_for_device(isp->dev, req->iovm->sgt->sgl,
475 req->iovm->sgt->nents, DMA_TO_DEVICE);
478 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
479 if (ccdc->lsc.request) {
480 list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
481 schedule_work(&ccdc->lsc.table_work);
483 ccdc->lsc.request = req;
484 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
490 ccdc_lsc_free_request(ccdc, req);
495 static inline int ccdc_lsc_is_configured(struct isp_ccdc_device *ccdc)
499 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
500 if (ccdc->lsc.active) {
501 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
504 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
508 static int ccdc_lsc_enable(struct isp_ccdc_device *ccdc)
510 struct ispccdc_lsc *lsc = &ccdc->lsc;
512 if (lsc->state != LSC_STATE_STOPPED)
516 list_add_tail(&lsc->active->list, &lsc->free_queue);
520 if (__ccdc_lsc_configure(ccdc, lsc->request) < 0) {
521 omap3isp_sbl_disable(to_isp_device(ccdc),
522 OMAP3_ISP_SBL_CCDC_LSC_READ);
523 list_add_tail(&lsc->request->list, &lsc->free_queue);
528 lsc->active = lsc->request;
530 __ccdc_lsc_enable(ccdc, 1);
533 if (!list_empty(&lsc->free_queue))
534 schedule_work(&lsc->table_work);
539 /* -----------------------------------------------------------------------------
540 * Parameters configuration
544 * ccdc_configure_clamp - Configure optical-black or digital clamping
545 * @ccdc: Pointer to ISP CCDC device.
547 * The CCDC performs either optical-black or digital clamp. Configure and enable
548 * the selected clamp method.
550 static void ccdc_configure_clamp(struct isp_ccdc_device *ccdc)
552 struct isp_device *isp = to_isp_device(ccdc);
556 clamp = ccdc->clamp.obgain << ISPCCDC_CLAMP_OBGAIN_SHIFT;
557 clamp |= ccdc->clamp.oblen << ISPCCDC_CLAMP_OBSLEN_SHIFT;
558 clamp |= ccdc->clamp.oblines << ISPCCDC_CLAMP_OBSLN_SHIFT;
559 clamp |= ccdc->clamp.obstpixel << ISPCCDC_CLAMP_OBST_SHIFT;
560 isp_reg_writel(isp, clamp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP);
562 isp_reg_writel(isp, ccdc->clamp.dcsubval,
563 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_DCSUB);
566 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CLAMP,
567 ISPCCDC_CLAMP_CLAMPEN,
568 ccdc->obclamp ? ISPCCDC_CLAMP_CLAMPEN : 0);
572 * ccdc_configure_fpc - Configure Faulty Pixel Correction
573 * @ccdc: Pointer to ISP CCDC device.
575 static void ccdc_configure_fpc(struct isp_ccdc_device *ccdc)
577 struct isp_device *isp = to_isp_device(ccdc);
579 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC, ISPCCDC_FPC_FPCEN);
584 isp_reg_writel(isp, ccdc->fpc.fpcaddr, OMAP3_ISP_IOMEM_CCDC,
586 /* The FPNUM field must be set before enabling FPC. */
587 isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT),
588 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
589 isp_reg_writel(isp, (ccdc->fpc.fpnum << ISPCCDC_FPC_FPNUM_SHIFT) |
590 ISPCCDC_FPC_FPCEN, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FPC);
594 * ccdc_configure_black_comp - Configure Black Level Compensation.
595 * @ccdc: Pointer to ISP CCDC device.
597 static void ccdc_configure_black_comp(struct isp_ccdc_device *ccdc)
599 struct isp_device *isp = to_isp_device(ccdc);
602 blcomp = ccdc->blcomp.b_mg << ISPCCDC_BLKCMP_B_MG_SHIFT;
603 blcomp |= ccdc->blcomp.gb_g << ISPCCDC_BLKCMP_GB_G_SHIFT;
604 blcomp |= ccdc->blcomp.gr_cy << ISPCCDC_BLKCMP_GR_CY_SHIFT;
605 blcomp |= ccdc->blcomp.r_ye << ISPCCDC_BLKCMP_R_YE_SHIFT;
607 isp_reg_writel(isp, blcomp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_BLKCMP);
611 * ccdc_configure_lpf - Configure Low-Pass Filter (LPF).
612 * @ccdc: Pointer to ISP CCDC device.
614 static void ccdc_configure_lpf(struct isp_ccdc_device *ccdc)
616 struct isp_device *isp = to_isp_device(ccdc);
618 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE,
619 ISPCCDC_SYN_MODE_LPF,
620 ccdc->lpf ? ISPCCDC_SYN_MODE_LPF : 0);
624 * ccdc_configure_alaw - Configure A-law compression.
625 * @ccdc: Pointer to ISP CCDC device.
627 static void ccdc_configure_alaw(struct isp_ccdc_device *ccdc)
629 struct isp_device *isp = to_isp_device(ccdc);
632 switch (ccdc->syncif.datsz) {
637 alaw = ISPCCDC_ALAW_GWDI_9_0;
640 alaw = ISPCCDC_ALAW_GWDI_10_1;
643 alaw = ISPCCDC_ALAW_GWDI_11_2;
646 alaw = ISPCCDC_ALAW_GWDI_12_3;
651 alaw |= ISPCCDC_ALAW_CCDTBL;
653 isp_reg_writel(isp, alaw, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_ALAW);
657 * ccdc_config_imgattr - Configure sensor image specific attributes.
658 * @ccdc: Pointer to ISP CCDC device.
659 * @colptn: Color pattern of the sensor.
661 static void ccdc_config_imgattr(struct isp_ccdc_device *ccdc, u32 colptn)
663 struct isp_device *isp = to_isp_device(ccdc);
665 isp_reg_writel(isp, colptn, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_COLPTN);
669 * ccdc_config - Set CCDC configuration from userspace
670 * @ccdc: Pointer to ISP CCDC device.
671 * @userspace_add: Structure containing CCDC configuration sent from userspace.
673 * Returns 0 if successful, -EINVAL if the pointer to the configuration
674 * structure is null, or the copy_from_user function fails to copy user space
675 * memory to kernel space memory.
677 static int ccdc_config(struct isp_ccdc_device *ccdc,
678 struct omap3isp_ccdc_update_config *ccdc_struct)
680 struct isp_device *isp = to_isp_device(ccdc);
683 spin_lock_irqsave(&ccdc->lock, flags);
684 ccdc->shadow_update = 1;
685 spin_unlock_irqrestore(&ccdc->lock, flags);
687 if (OMAP3ISP_CCDC_ALAW & ccdc_struct->update) {
688 ccdc->alaw = !!(OMAP3ISP_CCDC_ALAW & ccdc_struct->flag);
689 ccdc->update |= OMAP3ISP_CCDC_ALAW;
692 if (OMAP3ISP_CCDC_LPF & ccdc_struct->update) {
693 ccdc->lpf = !!(OMAP3ISP_CCDC_LPF & ccdc_struct->flag);
694 ccdc->update |= OMAP3ISP_CCDC_LPF;
697 if (OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->update) {
698 if (copy_from_user(&ccdc->clamp, ccdc_struct->bclamp,
699 sizeof(ccdc->clamp))) {
700 ccdc->shadow_update = 0;
704 ccdc->obclamp = !!(OMAP3ISP_CCDC_BLCLAMP & ccdc_struct->flag);
705 ccdc->update |= OMAP3ISP_CCDC_BLCLAMP;
708 if (OMAP3ISP_CCDC_BCOMP & ccdc_struct->update) {
709 if (copy_from_user(&ccdc->blcomp, ccdc_struct->blcomp,
710 sizeof(ccdc->blcomp))) {
711 ccdc->shadow_update = 0;
715 ccdc->update |= OMAP3ISP_CCDC_BCOMP;
718 ccdc->shadow_update = 0;
720 if (OMAP3ISP_CCDC_FPC & ccdc_struct->update) {
725 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
728 ccdc->fpc_en = !!(OMAP3ISP_CCDC_FPC & ccdc_struct->flag);
731 if (copy_from_user(&ccdc->fpc, ccdc_struct->fpc,
736 * table_new must be 64-bytes aligned, but it's
737 * already done by omap_iommu_vmalloc().
739 size = ccdc->fpc.fpnum * 4;
740 table_new = omap_iommu_vmalloc(isp->domain, isp->dev,
741 0, size, IOMMU_FLAG);
742 if (IS_ERR_VALUE(table_new))
745 if (copy_from_user(omap_da_to_va(isp->dev, table_new),
746 (__force void __user *)
747 ccdc->fpc.fpcaddr, size)) {
748 omap_iommu_vfree(isp->domain, isp->dev,
753 table_old = ccdc->fpc.fpcaddr;
754 ccdc->fpc.fpcaddr = table_new;
757 ccdc_configure_fpc(ccdc);
759 omap_iommu_vfree(isp->domain, isp->dev, table_old);
762 return ccdc_lsc_config(ccdc, ccdc_struct);
765 static void ccdc_apply_controls(struct isp_ccdc_device *ccdc)
767 if (ccdc->update & OMAP3ISP_CCDC_ALAW) {
768 ccdc_configure_alaw(ccdc);
769 ccdc->update &= ~OMAP3ISP_CCDC_ALAW;
772 if (ccdc->update & OMAP3ISP_CCDC_LPF) {
773 ccdc_configure_lpf(ccdc);
774 ccdc->update &= ~OMAP3ISP_CCDC_LPF;
777 if (ccdc->update & OMAP3ISP_CCDC_BLCLAMP) {
778 ccdc_configure_clamp(ccdc);
779 ccdc->update &= ~OMAP3ISP_CCDC_BLCLAMP;
782 if (ccdc->update & OMAP3ISP_CCDC_BCOMP) {
783 ccdc_configure_black_comp(ccdc);
784 ccdc->update &= ~OMAP3ISP_CCDC_BCOMP;
789 * omap3isp_ccdc_restore_context - Restore values of the CCDC module registers
790 * @dev: Pointer to ISP device
792 void omap3isp_ccdc_restore_context(struct isp_device *isp)
794 struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
796 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG, ISPCCDC_CFG_VDLC);
798 ccdc->update = OMAP3ISP_CCDC_ALAW | OMAP3ISP_CCDC_LPF
799 | OMAP3ISP_CCDC_BLCLAMP | OMAP3ISP_CCDC_BCOMP;
800 ccdc_apply_controls(ccdc);
801 ccdc_configure_fpc(ccdc);
804 /* -----------------------------------------------------------------------------
805 * Format- and pipeline-related configuration helpers
809 * ccdc_config_vp - Configure the Video Port.
810 * @ccdc: Pointer to ISP CCDC device.
812 static void ccdc_config_vp(struct isp_ccdc_device *ccdc)
814 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
815 struct isp_device *isp = to_isp_device(ccdc);
816 unsigned long l3_ick = pipe->l3_ick;
817 unsigned int max_div = isp->revision == ISP_REVISION_15_0 ? 64 : 8;
818 unsigned int div = 0;
821 fmtcfg_vp = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG)
822 & ~(ISPCCDC_FMTCFG_VPIN_MASK | ISPCCDC_FMTCFG_VPIF_FRQ_MASK);
824 switch (ccdc->syncif.datsz) {
827 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_9_0;
830 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_10_1;
833 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_11_2;
836 fmtcfg_vp |= ISPCCDC_FMTCFG_VPIN_12_3;
841 div = DIV_ROUND_UP(l3_ick, pipe->max_rate);
842 else if (pipe->external_rate)
843 div = l3_ick / pipe->external_rate;
845 div = clamp(div, 2U, max_div);
846 fmtcfg_vp |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
848 isp_reg_writel(isp, fmtcfg_vp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
852 * ccdc_enable_vp - Enable Video Port.
853 * @ccdc: Pointer to ISP CCDC device.
854 * @enable: 0 Disables VP, 1 Enables VP
856 * This is needed for outputting image to Preview, H3A and HIST ISP submodules.
858 static void ccdc_enable_vp(struct isp_ccdc_device *ccdc, u8 enable)
860 struct isp_device *isp = to_isp_device(ccdc);
862 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG,
863 ISPCCDC_FMTCFG_VPEN, enable ? ISPCCDC_FMTCFG_VPEN : 0);
867 * ccdc_config_outlineoffset - Configure memory saving output line offset
868 * @ccdc: Pointer to ISP CCDC device.
869 * @offset: Address offset to start a new line. Must be twice the
870 * Output width and aligned on 32 byte boundary
871 * @oddeven: Specifies the odd/even line pattern to be chosen to store the
873 * @numlines: Set the value 0-3 for +1-4lines, 4-7 for -1-4lines.
875 * - Configures the output line offset when stored in memory
876 * - Sets the odd/even line pattern to store the output
877 * (EVENEVEN (1), ODDEVEN (2), EVENODD (3), ODDODD (4))
878 * - Configures the number of even and odd line fields in case of rearranging
881 static void ccdc_config_outlineoffset(struct isp_ccdc_device *ccdc,
882 u32 offset, u8 oddeven, u8 numlines)
884 struct isp_device *isp = to_isp_device(ccdc);
886 isp_reg_writel(isp, offset & 0xffff,
887 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HSIZE_OFF);
889 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
890 ISPCCDC_SDOFST_FINV);
892 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
893 ISPCCDC_SDOFST_FOFST_4L);
897 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
898 (numlines & 0x7) << ISPCCDC_SDOFST_LOFST0_SHIFT);
901 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
902 (numlines & 0x7) << ISPCCDC_SDOFST_LOFST1_SHIFT);
905 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
906 (numlines & 0x7) << ISPCCDC_SDOFST_LOFST2_SHIFT);
909 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDOFST,
910 (numlines & 0x7) << ISPCCDC_SDOFST_LOFST3_SHIFT);
918 * ccdc_set_outaddr - Set memory address to save output image
919 * @ccdc: Pointer to ISP CCDC device.
920 * @addr: ISP MMU Mapped 32-bit memory address aligned on 32 byte boundary.
922 * Sets the memory address where the output will be saved.
924 static void ccdc_set_outaddr(struct isp_ccdc_device *ccdc, u32 addr)
926 struct isp_device *isp = to_isp_device(ccdc);
928 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SDR_ADDR);
932 * omap3isp_ccdc_max_rate - Calculate maximum input data rate based on the input
933 * @ccdc: Pointer to ISP CCDC device.
934 * @max_rate: Maximum calculated data rate.
936 * Returns in *max_rate less value between calculated and passed
938 void omap3isp_ccdc_max_rate(struct isp_ccdc_device *ccdc,
939 unsigned int *max_rate)
941 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
948 * TRM says that for parallel sensors the maximum data rate
949 * should be 90% form L3/2 clock, otherwise just L3/2.
951 if (ccdc->input == CCDC_INPUT_PARALLEL)
952 rate = pipe->l3_ick / 2 * 9 / 10;
954 rate = pipe->l3_ick / 2;
956 *max_rate = min(*max_rate, rate);
960 * ccdc_config_sync_if - Set CCDC sync interface configuration
961 * @ccdc: Pointer to ISP CCDC device.
962 * @syncif: Structure containing the sync parameters like field state, CCDC in
963 * master/slave mode, raw/yuv data, polarity of data, field, hs, vs
966 static void ccdc_config_sync_if(struct isp_ccdc_device *ccdc,
967 struct ispccdc_syncif *syncif)
969 struct isp_device *isp = to_isp_device(ccdc);
970 u32 syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC,
973 syn_mode |= ISPCCDC_SYN_MODE_VDHDEN;
976 syn_mode |= ISPCCDC_SYN_MODE_FLDSTAT;
978 syn_mode &= ~ISPCCDC_SYN_MODE_FLDSTAT;
980 syn_mode &= ~ISPCCDC_SYN_MODE_DATSIZ_MASK;
981 switch (syncif->datsz) {
983 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_8;
986 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_10;
989 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_11;
992 syn_mode |= ISPCCDC_SYN_MODE_DATSIZ_12;
997 syn_mode |= ISPCCDC_SYN_MODE_FLDMODE;
999 syn_mode &= ~ISPCCDC_SYN_MODE_FLDMODE;
1001 if (syncif->datapol)
1002 syn_mode |= ISPCCDC_SYN_MODE_DATAPOL;
1004 syn_mode &= ~ISPCCDC_SYN_MODE_DATAPOL;
1007 syn_mode |= ISPCCDC_SYN_MODE_FLDPOL;
1009 syn_mode &= ~ISPCCDC_SYN_MODE_FLDPOL;
1012 syn_mode |= ISPCCDC_SYN_MODE_HDPOL;
1014 syn_mode &= ~ISPCCDC_SYN_MODE_HDPOL;
1017 syn_mode |= ISPCCDC_SYN_MODE_VDPOL;
1019 syn_mode &= ~ISPCCDC_SYN_MODE_VDPOL;
1021 if (syncif->ccdc_mastermode) {
1022 syn_mode |= ISPCCDC_SYN_MODE_FLDOUT | ISPCCDC_SYN_MODE_VDHDOUT;
1024 syncif->hs_width << ISPCCDC_HD_VD_WID_HDW_SHIFT
1025 | syncif->vs_width << ISPCCDC_HD_VD_WID_VDW_SHIFT,
1026 OMAP3_ISP_IOMEM_CCDC,
1030 syncif->ppln << ISPCCDC_PIX_LINES_PPLN_SHIFT
1031 | syncif->hlprf << ISPCCDC_PIX_LINES_HLPRF_SHIFT,
1032 OMAP3_ISP_IOMEM_CCDC,
1035 syn_mode &= ~(ISPCCDC_SYN_MODE_FLDOUT |
1036 ISPCCDC_SYN_MODE_VDHDOUT);
1038 isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1040 if (!syncif->bt_r656_en)
1041 isp_reg_clr(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_REC656IF,
1042 ISPCCDC_REC656IF_R656ON);
1045 /* CCDC formats descriptions */
1046 static const u32 ccdc_sgrbg_pattern =
1047 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1048 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1049 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1050 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1051 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1052 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1053 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1054 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1055 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1056 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1057 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1058 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1059 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1060 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1061 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1062 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1064 static const u32 ccdc_srggb_pattern =
1065 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1066 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1067 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1068 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1069 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1070 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1071 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1072 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1073 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1074 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1075 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1076 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1077 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1078 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1079 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1080 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1082 static const u32 ccdc_sbggr_pattern =
1083 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1084 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1085 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1086 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1087 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1088 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1089 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1090 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1091 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1092 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1093 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1094 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1095 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1096 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1097 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1098 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1100 static const u32 ccdc_sgbrg_pattern =
1101 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC0_SHIFT |
1102 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC1_SHIFT |
1103 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP0PLC2_SHIFT |
1104 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP0PLC3_SHIFT |
1105 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC0_SHIFT |
1106 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC1_SHIFT |
1107 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP1PLC2_SHIFT |
1108 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP1PLC3_SHIFT |
1109 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC0_SHIFT |
1110 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC1_SHIFT |
1111 ISPCCDC_COLPTN_Gb_G << ISPCCDC_COLPTN_CP2PLC2_SHIFT |
1112 ISPCCDC_COLPTN_B_Mg << ISPCCDC_COLPTN_CP2PLC3_SHIFT |
1113 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC0_SHIFT |
1114 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC1_SHIFT |
1115 ISPCCDC_COLPTN_R_Ye << ISPCCDC_COLPTN_CP3PLC2_SHIFT |
1116 ISPCCDC_COLPTN_Gr_Cy << ISPCCDC_COLPTN_CP3PLC3_SHIFT;
1118 static void ccdc_configure(struct isp_ccdc_device *ccdc)
1120 struct isp_device *isp = to_isp_device(ccdc);
1121 struct isp_parallel_platform_data *pdata = NULL;
1122 struct v4l2_subdev *sensor;
1123 struct v4l2_mbus_framefmt *format;
1124 const struct v4l2_rect *crop;
1125 const struct isp_format_info *fmt_info;
1126 struct v4l2_subdev_format fmt_src;
1127 unsigned int depth_out;
1128 unsigned int depth_in = 0;
1129 struct media_pad *pad;
1130 unsigned long flags;
1135 pad = media_entity_remote_source(&ccdc->pads[CCDC_PAD_SINK]);
1136 sensor = media_entity_to_v4l2_subdev(pad->entity);
1137 if (ccdc->input == CCDC_INPUT_PARALLEL)
1138 pdata = &((struct isp_v4l2_subdevs_group *)sensor->host_priv)
1141 /* Compute shift value for lane shifter to configure the bridge. */
1142 fmt_src.pad = pad->index;
1143 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1144 if (!v4l2_subdev_call(sensor, pad, get_fmt, NULL, &fmt_src)) {
1145 fmt_info = omap3isp_video_format_info(fmt_src.format.code);
1146 depth_in = fmt_info->bpp;
1149 fmt_info = omap3isp_video_format_info
1150 (isp->isp_ccdc.formats[CCDC_PAD_SINK].code);
1151 depth_out = fmt_info->bpp;
1153 shift = depth_in - depth_out;
1154 omap3isp_configure_bridge(isp, ccdc->input, pdata, shift);
1156 ccdc->syncif.datsz = depth_out;
1157 ccdc->syncif.hdpol = pdata ? pdata->hs_pol : 0;
1158 ccdc->syncif.vdpol = pdata ? pdata->vs_pol : 0;
1159 ccdc_config_sync_if(ccdc, &ccdc->syncif);
1162 format = &ccdc->formats[CCDC_PAD_SINK];
1164 syn_mode = isp_reg_readl(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1166 /* Use the raw, unprocessed data when writing to memory. The H3A and
1167 * histogram modules are still fed with lens shading corrected data.
1169 syn_mode &= ~ISPCCDC_SYN_MODE_VP2SDR;
1171 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1172 syn_mode |= ISPCCDC_SYN_MODE_WEN;
1174 syn_mode &= ~ISPCCDC_SYN_MODE_WEN;
1176 if (ccdc->output & CCDC_OUTPUT_RESIZER)
1177 syn_mode |= ISPCCDC_SYN_MODE_SDR2RSZ;
1179 syn_mode &= ~ISPCCDC_SYN_MODE_SDR2RSZ;
1181 /* Use PACK8 mode for 1byte per pixel formats. */
1182 if (omap3isp_video_format_info(format->code)->bpp <= 8)
1183 syn_mode |= ISPCCDC_SYN_MODE_PACK8;
1185 syn_mode &= ~ISPCCDC_SYN_MODE_PACK8;
1187 isp_reg_writel(isp, syn_mode, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_SYN_MODE);
1190 switch (format->code) {
1191 case V4L2_MBUS_FMT_SRGGB10_1X10:
1192 case V4L2_MBUS_FMT_SRGGB12_1X12:
1193 ccdc_pattern = ccdc_srggb_pattern;
1195 case V4L2_MBUS_FMT_SBGGR10_1X10:
1196 case V4L2_MBUS_FMT_SBGGR12_1X12:
1197 ccdc_pattern = ccdc_sbggr_pattern;
1199 case V4L2_MBUS_FMT_SGBRG10_1X10:
1200 case V4L2_MBUS_FMT_SGBRG12_1X12:
1201 ccdc_pattern = ccdc_sgbrg_pattern;
1205 ccdc_pattern = ccdc_sgrbg_pattern;
1208 ccdc_config_imgattr(ccdc, ccdc_pattern);
1210 /* Generate VD0 on the last line of the image and VD1 on the
1213 isp_reg_writel(isp, ((format->height - 2) << ISPCCDC_VDINT_0_SHIFT) |
1214 ((format->height * 2 / 3) << ISPCCDC_VDINT_1_SHIFT),
1215 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VDINT);
1217 /* CCDC_PAD_SOURCE_OF */
1220 isp_reg_writel(isp, (crop->left << ISPCCDC_HORZ_INFO_SPH_SHIFT) |
1221 ((crop->width - 1) << ISPCCDC_HORZ_INFO_NPH_SHIFT),
1222 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_HORZ_INFO);
1223 isp_reg_writel(isp, crop->top << ISPCCDC_VERT_START_SLV0_SHIFT,
1224 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_START);
1225 isp_reg_writel(isp, (crop->height - 1)
1226 << ISPCCDC_VERT_LINES_NLV_SHIFT,
1227 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VERT_LINES);
1229 ccdc_config_outlineoffset(ccdc, ccdc->video_out.bpl_value, 0, 0);
1231 /* CCDC_PAD_SOURCE_VP */
1232 format = &ccdc->formats[CCDC_PAD_SOURCE_VP];
1234 isp_reg_writel(isp, (0 << ISPCCDC_FMT_HORZ_FMTSPH_SHIFT) |
1235 (format->width << ISPCCDC_FMT_HORZ_FMTLNH_SHIFT),
1236 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_HORZ);
1237 isp_reg_writel(isp, (0 << ISPCCDC_FMT_VERT_FMTSLV_SHIFT) |
1238 ((format->height + 1) << ISPCCDC_FMT_VERT_FMTLNV_SHIFT),
1239 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMT_VERT);
1241 isp_reg_writel(isp, (format->width << ISPCCDC_VP_OUT_HORZ_NUM_SHIFT) |
1242 (format->height << ISPCCDC_VP_OUT_VERT_NUM_SHIFT),
1243 OMAP3_ISP_IOMEM_CCDC, ISPCCDC_VP_OUT);
1245 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1246 if (ccdc->lsc.request == NULL)
1249 WARN_ON(ccdc->lsc.active);
1251 /* Get last good LSC configuration. If it is not supported for
1252 * the current active resolution discard it.
1254 if (ccdc->lsc.active == NULL &&
1255 __ccdc_lsc_configure(ccdc, ccdc->lsc.request) == 0) {
1256 ccdc->lsc.active = ccdc->lsc.request;
1258 list_add_tail(&ccdc->lsc.request->list, &ccdc->lsc.free_queue);
1259 schedule_work(&ccdc->lsc.table_work);
1262 ccdc->lsc.request = NULL;
1265 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1267 ccdc_apply_controls(ccdc);
1270 static void __ccdc_enable(struct isp_ccdc_device *ccdc, int enable)
1272 struct isp_device *isp = to_isp_device(ccdc);
1274 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_PCR,
1275 ISPCCDC_PCR_EN, enable ? ISPCCDC_PCR_EN : 0);
1278 static int ccdc_disable(struct isp_ccdc_device *ccdc)
1280 unsigned long flags;
1283 spin_lock_irqsave(&ccdc->lock, flags);
1284 if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1285 ccdc->stopping = CCDC_STOP_REQUEST;
1286 spin_unlock_irqrestore(&ccdc->lock, flags);
1288 ret = wait_event_timeout(ccdc->wait,
1289 ccdc->stopping == CCDC_STOP_FINISHED,
1290 msecs_to_jiffies(2000));
1293 dev_warn(to_device(ccdc), "CCDC stop timeout!\n");
1296 omap3isp_sbl_disable(to_isp_device(ccdc), OMAP3_ISP_SBL_CCDC_LSC_READ);
1298 mutex_lock(&ccdc->ioctl_lock);
1299 ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
1300 ccdc->lsc.request = ccdc->lsc.active;
1301 ccdc->lsc.active = NULL;
1302 cancel_work_sync(&ccdc->lsc.table_work);
1303 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
1304 mutex_unlock(&ccdc->ioctl_lock);
1306 ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
1308 return ret > 0 ? 0 : ret;
1311 static void ccdc_enable(struct isp_ccdc_device *ccdc)
1313 if (ccdc_lsc_is_configured(ccdc))
1314 __ccdc_lsc_enable(ccdc, 1);
1315 __ccdc_enable(ccdc, 1);
1318 /* -----------------------------------------------------------------------------
1319 * Interrupt handling
1323 * ccdc_sbl_busy - Poll idle state of CCDC and related SBL memory write bits
1324 * @ccdc: Pointer to ISP CCDC device.
1326 * Returns zero if the CCDC is idle and the image has been written to
1329 static int ccdc_sbl_busy(struct isp_ccdc_device *ccdc)
1331 struct isp_device *isp = to_isp_device(ccdc);
1333 return omap3isp_ccdc_busy(ccdc)
1334 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_0) &
1335 ISPSBL_CCDC_WR_0_DATA_READY)
1336 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_1) &
1337 ISPSBL_CCDC_WR_0_DATA_READY)
1338 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_2) &
1339 ISPSBL_CCDC_WR_0_DATA_READY)
1340 | (isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_CCDC_WR_3) &
1341 ISPSBL_CCDC_WR_0_DATA_READY);
1345 * ccdc_sbl_wait_idle - Wait until the CCDC and related SBL are idle
1346 * @ccdc: Pointer to ISP CCDC device.
1347 * @max_wait: Max retry count in us for wait for idle/busy transition.
1349 static int ccdc_sbl_wait_idle(struct isp_ccdc_device *ccdc,
1350 unsigned int max_wait)
1352 unsigned int wait = 0;
1355 max_wait = 10000; /* 10 ms */
1357 for (wait = 0; wait <= max_wait; wait++) {
1358 if (!ccdc_sbl_busy(ccdc))
1368 /* __ccdc_handle_stopping - Handle CCDC and/or LSC stopping sequence
1369 * @ccdc: Pointer to ISP CCDC device.
1370 * @event: Pointing which event trigger handler
1372 * Return 1 when the event and stopping request combination is satisfied,
1375 static int __ccdc_handle_stopping(struct isp_ccdc_device *ccdc, u32 event)
1379 switch ((ccdc->stopping & 3) | event) {
1380 case CCDC_STOP_REQUEST | CCDC_EVENT_VD1:
1381 if (ccdc->lsc.state != LSC_STATE_STOPPED)
1382 __ccdc_lsc_enable(ccdc, 0);
1383 __ccdc_enable(ccdc, 0);
1384 ccdc->stopping = CCDC_STOP_EXECUTED;
1387 case CCDC_STOP_EXECUTED | CCDC_EVENT_VD0:
1388 ccdc->stopping |= CCDC_STOP_CCDC_FINISHED;
1389 if (ccdc->lsc.state == LSC_STATE_STOPPED)
1390 ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1394 case CCDC_STOP_EXECUTED | CCDC_EVENT_LSC_DONE:
1395 ccdc->stopping |= CCDC_STOP_LSC_FINISHED;
1399 case CCDC_STOP_EXECUTED | CCDC_EVENT_VD1:
1403 if (ccdc->stopping == CCDC_STOP_FINISHED) {
1404 wake_up(&ccdc->wait);
1411 static void ccdc_hs_vs_isr(struct isp_ccdc_device *ccdc)
1413 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1414 struct video_device *vdev = ccdc->subdev.devnode;
1415 struct v4l2_event event;
1417 /* Frame number propagation */
1418 atomic_inc(&pipe->frame_number);
1420 memset(&event, 0, sizeof(event));
1421 event.type = V4L2_EVENT_FRAME_SYNC;
1422 event.u.frame_sync.frame_sequence = atomic_read(&pipe->frame_number);
1424 v4l2_event_queue(vdev, &event);
1428 * ccdc_lsc_isr - Handle LSC events
1429 * @ccdc: Pointer to ISP CCDC device.
1430 * @events: LSC events
1432 static void ccdc_lsc_isr(struct isp_ccdc_device *ccdc, u32 events)
1434 unsigned long flags;
1436 if (events & IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ) {
1437 struct isp_pipeline *pipe =
1438 to_isp_pipeline(&ccdc->subdev.entity);
1440 ccdc_lsc_error_handler(ccdc);
1442 dev_dbg(to_device(ccdc), "lsc prefetch error\n");
1445 if (!(events & IRQ0STATUS_CCDC_LSC_DONE_IRQ))
1448 /* LSC_DONE interrupt occur, there are two cases
1449 * 1. stopping for reconfiguration
1450 * 2. stopping because of STREAM OFF command
1452 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1454 if (ccdc->lsc.state == LSC_STATE_STOPPING)
1455 ccdc->lsc.state = LSC_STATE_STOPPED;
1457 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_LSC_DONE))
1460 if (ccdc->lsc.state != LSC_STATE_RECONFIG)
1463 /* LSC is in STOPPING state, change to the new state */
1464 ccdc->lsc.state = LSC_STATE_STOPPED;
1466 /* This is an exception. Start of frame and LSC_DONE interrupt
1467 * have been received on the same time. Skip this event and wait
1470 if (events & IRQ0STATUS_HS_VS_IRQ)
1473 /* The LSC engine is stopped at this point. Enable it if there's a
1476 if (ccdc->lsc.request == NULL)
1479 ccdc_lsc_enable(ccdc);
1482 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1485 static int ccdc_isr_buffer(struct isp_ccdc_device *ccdc)
1487 struct isp_pipeline *pipe = to_isp_pipeline(&ccdc->subdev.entity);
1488 struct isp_device *isp = to_isp_device(ccdc);
1489 struct isp_buffer *buffer;
1492 /* The CCDC generates VD0 interrupts even when disabled (the datasheet
1493 * doesn't explicitly state if that's supposed to happen or not, so it
1494 * can be considered as a hardware bug or as a feature, but we have to
1495 * deal with it anyway). Disabling the CCDC when no buffer is available
1496 * would thus not be enough, we need to handle the situation explicitly.
1498 if (list_empty(&ccdc->video_out.dmaqueue))
1501 /* We're in continuous mode, and memory writes were disabled due to a
1502 * buffer underrun. Reenable them now that we have a buffer. The buffer
1503 * address has been set in ccdc_video_queue.
1505 if (ccdc->state == ISP_PIPELINE_STREAM_CONTINUOUS && ccdc->underrun) {
1511 if (ccdc_sbl_wait_idle(ccdc, 1000)) {
1512 dev_info(isp->dev, "CCDC won't become idle!\n");
1516 buffer = omap3isp_video_buffer_next(&ccdc->video_out);
1517 if (buffer != NULL) {
1518 ccdc_set_outaddr(ccdc, buffer->isp_addr);
1522 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1524 if (ccdc->state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1525 isp_pipeline_ready(pipe))
1526 omap3isp_pipeline_set_stream(pipe,
1527 ISP_PIPELINE_STREAM_SINGLESHOT);
1534 * ccdc_vd0_isr - Handle VD0 event
1535 * @ccdc: Pointer to ISP CCDC device.
1537 * Executes LSC deferred enablement before next frame starts.
1539 static void ccdc_vd0_isr(struct isp_ccdc_device *ccdc)
1541 unsigned long flags;
1544 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1545 restart = ccdc_isr_buffer(ccdc);
1547 spin_lock_irqsave(&ccdc->lock, flags);
1548 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD0)) {
1549 spin_unlock_irqrestore(&ccdc->lock, flags);
1553 if (!ccdc->shadow_update)
1554 ccdc_apply_controls(ccdc);
1555 spin_unlock_irqrestore(&ccdc->lock, flags);
1562 * ccdc_vd1_isr - Handle VD1 event
1563 * @ccdc: Pointer to ISP CCDC device.
1565 static void ccdc_vd1_isr(struct isp_ccdc_device *ccdc)
1567 unsigned long flags;
1569 spin_lock_irqsave(&ccdc->lsc.req_lock, flags);
1572 * Depending on the CCDC pipeline state, CCDC stopping should be
1573 * handled differently. In SINGLESHOT we emulate an internal CCDC
1574 * stopping because the CCDC hw works only in continuous mode.
1575 * When CONTINUOUS pipeline state is used and the CCDC writes it's
1576 * data to memory the CCDC and LSC are stopped immediately but
1577 * without change the CCDC stopping state machine. The CCDC
1578 * stopping state machine should be used only when user request
1579 * for stopping is received (SINGLESHOT is an exeption).
1581 switch (ccdc->state) {
1582 case ISP_PIPELINE_STREAM_SINGLESHOT:
1583 ccdc->stopping = CCDC_STOP_REQUEST;
1586 case ISP_PIPELINE_STREAM_CONTINUOUS:
1587 if (ccdc->output & CCDC_OUTPUT_MEMORY) {
1588 if (ccdc->lsc.state != LSC_STATE_STOPPED)
1589 __ccdc_lsc_enable(ccdc, 0);
1590 __ccdc_enable(ccdc, 0);
1594 case ISP_PIPELINE_STREAM_STOPPED:
1598 if (__ccdc_handle_stopping(ccdc, CCDC_EVENT_VD1))
1601 if (ccdc->lsc.request == NULL)
1605 * LSC need to be reconfigured. Stop it here and on next LSC_DONE IRQ
1606 * do the appropriate changes in registers
1608 if (ccdc->lsc.state == LSC_STATE_RUNNING) {
1609 __ccdc_lsc_enable(ccdc, 0);
1610 ccdc->lsc.state = LSC_STATE_RECONFIG;
1614 /* LSC has been in STOPPED state, enable it */
1615 if (ccdc->lsc.state == LSC_STATE_STOPPED)
1616 ccdc_lsc_enable(ccdc);
1619 spin_unlock_irqrestore(&ccdc->lsc.req_lock, flags);
1623 * omap3isp_ccdc_isr - Configure CCDC during interframe time.
1624 * @ccdc: Pointer to ISP CCDC device.
1625 * @events: CCDC events
1627 int omap3isp_ccdc_isr(struct isp_ccdc_device *ccdc, u32 events)
1629 if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED)
1632 if (events & IRQ0STATUS_CCDC_VD1_IRQ)
1635 ccdc_lsc_isr(ccdc, events);
1637 if (events & IRQ0STATUS_CCDC_VD0_IRQ)
1640 if (events & IRQ0STATUS_HS_VS_IRQ)
1641 ccdc_hs_vs_isr(ccdc);
1646 /* -----------------------------------------------------------------------------
1647 * ISP video operations
1650 static int ccdc_video_queue(struct isp_video *video, struct isp_buffer *buffer)
1652 struct isp_ccdc_device *ccdc = &video->isp->isp_ccdc;
1654 if (!(ccdc->output & CCDC_OUTPUT_MEMORY))
1657 ccdc_set_outaddr(ccdc, buffer->isp_addr);
1659 /* We now have a buffer queued on the output, restart the pipeline
1660 * on the next CCDC interrupt if running in continuous mode (or when
1661 * starting the stream).
1668 static const struct isp_video_operations ccdc_video_ops = {
1669 .queue = ccdc_video_queue,
1672 /* -----------------------------------------------------------------------------
1673 * V4L2 subdev operations
1677 * ccdc_ioctl - CCDC module private ioctl's
1678 * @sd: ISP CCDC V4L2 subdevice
1679 * @cmd: ioctl command
1680 * @arg: ioctl argument
1682 * Return 0 on success or a negative error code otherwise.
1684 static long ccdc_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1686 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1690 case VIDIOC_OMAP3ISP_CCDC_CFG:
1691 mutex_lock(&ccdc->ioctl_lock);
1692 ret = ccdc_config(ccdc, arg);
1693 mutex_unlock(&ccdc->ioctl_lock);
1697 return -ENOIOCTLCMD;
1703 static int ccdc_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1704 struct v4l2_event_subscription *sub)
1706 if (sub->type != V4L2_EVENT_FRAME_SYNC)
1709 /* line number is zero at frame start */
1713 return v4l2_event_subscribe(fh, sub, OMAP3ISP_CCDC_NEVENTS, NULL);
1716 static int ccdc_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1717 struct v4l2_event_subscription *sub)
1719 return v4l2_event_unsubscribe(fh, sub);
1723 * ccdc_set_stream - Enable/Disable streaming on the CCDC module
1724 * @sd: ISP CCDC V4L2 subdevice
1725 * @enable: Enable/disable stream
1727 * When writing to memory, the CCDC hardware can't be enabled without a memory
1728 * buffer to write to. As the s_stream operation is called in response to a
1729 * STREAMON call without any buffer queued yet, just update the enabled field
1730 * and return immediately. The CCDC will be enabled in ccdc_isr_buffer().
1732 * When not writing to memory enable the CCDC immediately.
1734 static int ccdc_set_stream(struct v4l2_subdev *sd, int enable)
1736 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1737 struct isp_device *isp = to_isp_device(ccdc);
1740 if (ccdc->state == ISP_PIPELINE_STREAM_STOPPED) {
1741 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1744 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_CCDC);
1745 isp_reg_set(isp, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_CFG,
1748 ccdc_configure(ccdc);
1750 /* TODO: Don't configure the video port if all of its output
1751 * links are inactive.
1753 ccdc_config_vp(ccdc);
1754 ccdc_enable_vp(ccdc, 1);
1755 ccdc_print_status(ccdc);
1759 case ISP_PIPELINE_STREAM_CONTINUOUS:
1760 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1761 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1763 if (ccdc->underrun || !(ccdc->output & CCDC_OUTPUT_MEMORY))
1769 case ISP_PIPELINE_STREAM_SINGLESHOT:
1770 if (ccdc->output & CCDC_OUTPUT_MEMORY &&
1771 ccdc->state != ISP_PIPELINE_STREAM_SINGLESHOT)
1772 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1777 case ISP_PIPELINE_STREAM_STOPPED:
1778 ret = ccdc_disable(ccdc);
1779 if (ccdc->output & CCDC_OUTPUT_MEMORY)
1780 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_CCDC_WRITE);
1781 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_CCDC);
1786 ccdc->state = enable;
1790 static struct v4l2_mbus_framefmt *
1791 __ccdc_get_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1792 unsigned int pad, enum v4l2_subdev_format_whence which)
1794 if (which == V4L2_SUBDEV_FORMAT_TRY)
1795 return v4l2_subdev_get_try_format(fh, pad);
1797 return &ccdc->formats[pad];
1800 static struct v4l2_rect *
1801 __ccdc_get_crop(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1802 enum v4l2_subdev_format_whence which)
1804 if (which == V4L2_SUBDEV_FORMAT_TRY)
1805 return v4l2_subdev_get_try_crop(fh, CCDC_PAD_SOURCE_OF);
1811 * ccdc_try_format - Try video format on a pad
1812 * @ccdc: ISP CCDC device
1813 * @fh : V4L2 subdev file handle
1818 ccdc_try_format(struct isp_ccdc_device *ccdc, struct v4l2_subdev_fh *fh,
1819 unsigned int pad, struct v4l2_mbus_framefmt *fmt,
1820 enum v4l2_subdev_format_whence which)
1822 struct v4l2_mbus_framefmt *format;
1823 const struct isp_format_info *info;
1824 unsigned int width = fmt->width;
1825 unsigned int height = fmt->height;
1826 struct v4l2_rect *crop;
1831 /* TODO: If the CCDC output formatter pad is connected directly
1832 * to the resizer, only YUV formats can be used.
1834 for (i = 0; i < ARRAY_SIZE(ccdc_fmts); i++) {
1835 if (fmt->code == ccdc_fmts[i])
1839 /* If not found, use SGRBG10 as default */
1840 if (i >= ARRAY_SIZE(ccdc_fmts))
1841 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1843 /* Clamp the input size. */
1844 fmt->width = clamp_t(u32, width, 32, 4096);
1845 fmt->height = clamp_t(u32, height, 32, 4096);
1848 case CCDC_PAD_SOURCE_OF:
1849 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1850 memcpy(fmt, format, sizeof(*fmt));
1852 /* Hardcode the output size to the crop rectangle size. */
1853 crop = __ccdc_get_crop(ccdc, fh, which);
1854 fmt->width = crop->width;
1855 fmt->height = crop->height;
1858 case CCDC_PAD_SOURCE_VP:
1859 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, which);
1860 memcpy(fmt, format, sizeof(*fmt));
1862 /* The video port interface truncates the data to 10 bits. */
1863 info = omap3isp_video_format_info(fmt->code);
1864 fmt->code = info->truncated;
1866 /* The number of lines that can be clocked out from the video
1867 * port output must be at least one line less than the number
1870 fmt->width = clamp_t(u32, width, 32, fmt->width);
1871 fmt->height = clamp_t(u32, height, 32, fmt->height - 1);
1875 /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
1876 * stored on 2 bytes.
1878 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1879 fmt->field = V4L2_FIELD_NONE;
1883 * ccdc_try_crop - Validate a crop rectangle
1884 * @ccdc: ISP CCDC device
1885 * @sink: format on the sink pad
1886 * @crop: crop rectangle to be validated
1888 static void ccdc_try_crop(struct isp_ccdc_device *ccdc,
1889 const struct v4l2_mbus_framefmt *sink,
1890 struct v4l2_rect *crop)
1892 const struct isp_format_info *info;
1893 unsigned int max_width;
1895 /* For Bayer formats, restrict left/top and width/height to even values
1896 * to keep the Bayer pattern.
1898 info = omap3isp_video_format_info(sink->code);
1899 if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1904 crop->left = clamp_t(u32, crop->left, 0, sink->width - CCDC_MIN_WIDTH);
1905 crop->top = clamp_t(u32, crop->top, 0, sink->height - CCDC_MIN_HEIGHT);
1907 /* The data formatter truncates the number of horizontal output pixels
1908 * to a multiple of 16. To avoid clipping data, allow callers to request
1909 * an output size bigger than the input size up to the nearest multiple
1912 max_width = (sink->width - crop->left + 15) & ~15;
1913 crop->width = clamp_t(u32, crop->width, CCDC_MIN_WIDTH, max_width)
1915 crop->height = clamp_t(u32, crop->height, CCDC_MIN_HEIGHT,
1916 sink->height - crop->top);
1918 /* Odd width/height values don't make sense for Bayer formats. */
1919 if (info->flavor != V4L2_MBUS_FMT_Y8_1X8) {
1926 * ccdc_enum_mbus_code - Handle pixel format enumeration
1927 * @sd : pointer to v4l2 subdev structure
1928 * @fh : V4L2 subdev file handle
1929 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1930 * return -EINVAL or zero on success
1932 static int ccdc_enum_mbus_code(struct v4l2_subdev *sd,
1933 struct v4l2_subdev_fh *fh,
1934 struct v4l2_subdev_mbus_code_enum *code)
1936 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1937 struct v4l2_mbus_framefmt *format;
1939 switch (code->pad) {
1941 if (code->index >= ARRAY_SIZE(ccdc_fmts))
1944 code->code = ccdc_fmts[code->index];
1947 case CCDC_PAD_SOURCE_OF:
1948 case CCDC_PAD_SOURCE_VP:
1949 /* No format conversion inside CCDC */
1950 if (code->index != 0)
1953 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK,
1954 V4L2_SUBDEV_FORMAT_TRY);
1956 code->code = format->code;
1966 static int ccdc_enum_frame_size(struct v4l2_subdev *sd,
1967 struct v4l2_subdev_fh *fh,
1968 struct v4l2_subdev_frame_size_enum *fse)
1970 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
1971 struct v4l2_mbus_framefmt format;
1973 if (fse->index != 0)
1976 format.code = fse->code;
1979 ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1980 fse->min_width = format.width;
1981 fse->min_height = format.height;
1983 if (format.code != fse->code)
1986 format.code = fse->code;
1989 ccdc_try_format(ccdc, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1990 fse->max_width = format.width;
1991 fse->max_height = format.height;
1997 * ccdc_get_selection - Retrieve a selection rectangle on a pad
1998 * @sd: ISP CCDC V4L2 subdevice
1999 * @fh: V4L2 subdev file handle
2000 * @sel: Selection rectangle
2002 * The only supported rectangles are the crop rectangles on the output formatter
2005 * Return 0 on success or a negative error code otherwise.
2007 static int ccdc_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2008 struct v4l2_subdev_selection *sel)
2010 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2011 struct v4l2_mbus_framefmt *format;
2013 if (sel->pad != CCDC_PAD_SOURCE_OF)
2016 switch (sel->target) {
2017 case V4L2_SEL_TGT_CROP_BOUNDS:
2020 sel->r.width = INT_MAX;
2021 sel->r.height = INT_MAX;
2023 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
2024 ccdc_try_crop(ccdc, format, &sel->r);
2027 case V4L2_SEL_TGT_CROP:
2028 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
2039 * ccdc_set_selection - Set a selection rectangle on a pad
2040 * @sd: ISP CCDC V4L2 subdevice
2041 * @fh: V4L2 subdev file handle
2042 * @sel: Selection rectangle
2044 * The only supported rectangle is the actual crop rectangle on the output
2045 * formatter source pad.
2047 * Return 0 on success or a negative error code otherwise.
2049 static int ccdc_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2050 struct v4l2_subdev_selection *sel)
2052 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2053 struct v4l2_mbus_framefmt *format;
2055 if (sel->target != V4L2_SEL_TGT_CROP ||
2056 sel->pad != CCDC_PAD_SOURCE_OF)
2059 /* The crop rectangle can't be changed while streaming. */
2060 if (ccdc->state != ISP_PIPELINE_STREAM_STOPPED)
2063 /* Modifying the crop rectangle always changes the format on the source
2064 * pad. If the KEEP_CONFIG flag is set, just return the current crop
2067 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
2068 sel->r = *__ccdc_get_crop(ccdc, fh, sel->which);
2072 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SINK, sel->which);
2073 ccdc_try_crop(ccdc, format, &sel->r);
2074 *__ccdc_get_crop(ccdc, fh, sel->which) = sel->r;
2076 /* Update the source format. */
2077 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF, sel->which);
2078 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format, sel->which);
2084 * ccdc_get_format - Retrieve the video format on a pad
2085 * @sd : ISP CCDC V4L2 subdevice
2086 * @fh : V4L2 subdev file handle
2089 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2090 * to the format type.
2092 static int ccdc_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2093 struct v4l2_subdev_format *fmt)
2095 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2096 struct v4l2_mbus_framefmt *format;
2098 format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2102 fmt->format = *format;
2107 * ccdc_set_format - Set the video format on a pad
2108 * @sd : ISP CCDC V4L2 subdevice
2109 * @fh : V4L2 subdev file handle
2112 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
2113 * to the format type.
2115 static int ccdc_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2116 struct v4l2_subdev_format *fmt)
2118 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2119 struct v4l2_mbus_framefmt *format;
2120 struct v4l2_rect *crop;
2122 format = __ccdc_get_format(ccdc, fh, fmt->pad, fmt->which);
2126 ccdc_try_format(ccdc, fh, fmt->pad, &fmt->format, fmt->which);
2127 *format = fmt->format;
2129 /* Propagate the format from sink to source */
2130 if (fmt->pad == CCDC_PAD_SINK) {
2131 /* Reset the crop rectangle. */
2132 crop = __ccdc_get_crop(ccdc, fh, fmt->which);
2135 crop->width = fmt->format.width;
2136 crop->height = fmt->format.height;
2138 ccdc_try_crop(ccdc, &fmt->format, crop);
2140 /* Update the source formats. */
2141 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_OF,
2143 *format = fmt->format;
2144 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_OF, format,
2147 format = __ccdc_get_format(ccdc, fh, CCDC_PAD_SOURCE_VP,
2149 *format = fmt->format;
2150 ccdc_try_format(ccdc, fh, CCDC_PAD_SOURCE_VP, format,
2158 * Decide whether desired output pixel code can be obtained with
2159 * the lane shifter by shifting the input pixel code.
2160 * @in: input pixelcode to shifter
2161 * @out: output pixelcode from shifter
2162 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
2164 * return true if the combination is possible
2165 * return false otherwise
2167 static bool ccdc_is_shiftable(enum v4l2_mbus_pixelcode in,
2168 enum v4l2_mbus_pixelcode out,
2169 unsigned int additional_shift)
2171 const struct isp_format_info *in_info, *out_info;
2176 in_info = omap3isp_video_format_info(in);
2177 out_info = omap3isp_video_format_info(out);
2179 if ((in_info->flavor == 0) || (out_info->flavor == 0))
2182 if (in_info->flavor != out_info->flavor)
2185 return in_info->bpp - out_info->bpp + additional_shift <= 6;
2188 static int ccdc_link_validate(struct v4l2_subdev *sd,
2189 struct media_link *link,
2190 struct v4l2_subdev_format *source_fmt,
2191 struct v4l2_subdev_format *sink_fmt)
2193 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2194 unsigned long parallel_shift;
2196 /* Check if the two ends match */
2197 if (source_fmt->format.width != sink_fmt->format.width ||
2198 source_fmt->format.height != sink_fmt->format.height)
2201 /* We've got a parallel sensor here. */
2202 if (ccdc->input == CCDC_INPUT_PARALLEL) {
2203 struct isp_parallel_platform_data *pdata =
2204 &((struct isp_v4l2_subdevs_group *)
2205 media_entity_to_v4l2_subdev(link->source->entity)
2206 ->host_priv)->bus.parallel;
2207 parallel_shift = pdata->data_lane_shift * 2;
2212 /* Lane shifter may be used to drop bits on CCDC sink pad */
2213 if (!ccdc_is_shiftable(source_fmt->format.code,
2214 sink_fmt->format.code, parallel_shift))
2221 * ccdc_init_formats - Initialize formats on all pads
2222 * @sd: ISP CCDC V4L2 subdevice
2223 * @fh: V4L2 subdev file handle
2225 * Initialize all pad formats with default values. If fh is not NULL, try
2226 * formats are initialized on the file handle. Otherwise active formats are
2227 * initialized on the device.
2229 static int ccdc_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2231 struct v4l2_subdev_format format;
2233 memset(&format, 0, sizeof(format));
2234 format.pad = CCDC_PAD_SINK;
2235 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2236 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2237 format.format.width = 4096;
2238 format.format.height = 4096;
2239 ccdc_set_format(sd, fh, &format);
2244 /* V4L2 subdev core operations */
2245 static const struct v4l2_subdev_core_ops ccdc_v4l2_core_ops = {
2246 .ioctl = ccdc_ioctl,
2247 .subscribe_event = ccdc_subscribe_event,
2248 .unsubscribe_event = ccdc_unsubscribe_event,
2251 /* V4L2 subdev video operations */
2252 static const struct v4l2_subdev_video_ops ccdc_v4l2_video_ops = {
2253 .s_stream = ccdc_set_stream,
2256 /* V4L2 subdev pad operations */
2257 static const struct v4l2_subdev_pad_ops ccdc_v4l2_pad_ops = {
2258 .enum_mbus_code = ccdc_enum_mbus_code,
2259 .enum_frame_size = ccdc_enum_frame_size,
2260 .get_fmt = ccdc_get_format,
2261 .set_fmt = ccdc_set_format,
2262 .get_selection = ccdc_get_selection,
2263 .set_selection = ccdc_set_selection,
2264 .link_validate = ccdc_link_validate,
2267 /* V4L2 subdev operations */
2268 static const struct v4l2_subdev_ops ccdc_v4l2_ops = {
2269 .core = &ccdc_v4l2_core_ops,
2270 .video = &ccdc_v4l2_video_ops,
2271 .pad = &ccdc_v4l2_pad_ops,
2274 /* V4L2 subdev internal operations */
2275 static const struct v4l2_subdev_internal_ops ccdc_v4l2_internal_ops = {
2276 .open = ccdc_init_formats,
2279 /* -----------------------------------------------------------------------------
2280 * Media entity operations
2284 * ccdc_link_setup - Setup CCDC connections
2285 * @entity: CCDC media entity
2286 * @local: Pad at the local end of the link
2287 * @remote: Pad at the remote end of the link
2288 * @flags: Link flags
2290 * return -EINVAL or zero on success
2292 static int ccdc_link_setup(struct media_entity *entity,
2293 const struct media_pad *local,
2294 const struct media_pad *remote, u32 flags)
2296 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2297 struct isp_ccdc_device *ccdc = v4l2_get_subdevdata(sd);
2298 struct isp_device *isp = to_isp_device(ccdc);
2300 switch (local->index | media_entity_type(remote->entity)) {
2301 case CCDC_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2302 /* Read from the sensor (parallel interface), CCP2, CSI2a or
2305 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
2306 ccdc->input = CCDC_INPUT_NONE;
2310 if (ccdc->input != CCDC_INPUT_NONE)
2313 if (remote->entity == &isp->isp_ccp2.subdev.entity)
2314 ccdc->input = CCDC_INPUT_CCP2B;
2315 else if (remote->entity == &isp->isp_csi2a.subdev.entity)
2316 ccdc->input = CCDC_INPUT_CSI2A;
2317 else if (remote->entity == &isp->isp_csi2c.subdev.entity)
2318 ccdc->input = CCDC_INPUT_CSI2C;
2320 ccdc->input = CCDC_INPUT_PARALLEL;
2325 * The ISP core doesn't support pipelines with multiple video outputs.
2326 * Revisit this when it will be implemented, and return -EBUSY for now.
2329 case CCDC_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
2330 /* Write to preview engine, histogram and H3A. When none of
2331 * those links are active, the video port can be disabled.
2333 if (flags & MEDIA_LNK_FL_ENABLED) {
2334 if (ccdc->output & ~CCDC_OUTPUT_PREVIEW)
2336 ccdc->output |= CCDC_OUTPUT_PREVIEW;
2338 ccdc->output &= ~CCDC_OUTPUT_PREVIEW;
2342 case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_DEVNODE:
2343 /* Write to memory */
2344 if (flags & MEDIA_LNK_FL_ENABLED) {
2345 if (ccdc->output & ~CCDC_OUTPUT_MEMORY)
2347 ccdc->output |= CCDC_OUTPUT_MEMORY;
2349 ccdc->output &= ~CCDC_OUTPUT_MEMORY;
2353 case CCDC_PAD_SOURCE_OF | MEDIA_ENT_T_V4L2_SUBDEV:
2354 /* Write to resizer */
2355 if (flags & MEDIA_LNK_FL_ENABLED) {
2356 if (ccdc->output & ~CCDC_OUTPUT_RESIZER)
2358 ccdc->output |= CCDC_OUTPUT_RESIZER;
2360 ccdc->output &= ~CCDC_OUTPUT_RESIZER;
2371 /* media operations */
2372 static const struct media_entity_operations ccdc_media_ops = {
2373 .link_setup = ccdc_link_setup,
2374 .link_validate = v4l2_subdev_link_validate,
2377 void omap3isp_ccdc_unregister_entities(struct isp_ccdc_device *ccdc)
2379 v4l2_device_unregister_subdev(&ccdc->subdev);
2380 omap3isp_video_unregister(&ccdc->video_out);
2383 int omap3isp_ccdc_register_entities(struct isp_ccdc_device *ccdc,
2384 struct v4l2_device *vdev)
2388 /* Register the subdev and video node. */
2389 ret = v4l2_device_register_subdev(vdev, &ccdc->subdev);
2393 ret = omap3isp_video_register(&ccdc->video_out, vdev);
2400 omap3isp_ccdc_unregister_entities(ccdc);
2404 /* -----------------------------------------------------------------------------
2405 * ISP CCDC initialisation and cleanup
2409 * ccdc_init_entities - Initialize V4L2 subdev and media entity
2410 * @ccdc: ISP CCDC module
2412 * Return 0 on success and a negative error code on failure.
2414 static int ccdc_init_entities(struct isp_ccdc_device *ccdc)
2416 struct v4l2_subdev *sd = &ccdc->subdev;
2417 struct media_pad *pads = ccdc->pads;
2418 struct media_entity *me = &sd->entity;
2421 ccdc->input = CCDC_INPUT_NONE;
2423 v4l2_subdev_init(sd, &ccdc_v4l2_ops);
2424 sd->internal_ops = &ccdc_v4l2_internal_ops;
2425 strlcpy(sd->name, "OMAP3 ISP CCDC", sizeof(sd->name));
2426 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2427 v4l2_set_subdevdata(sd, ccdc);
2428 sd->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
2430 pads[CCDC_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2431 pads[CCDC_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
2432 pads[CCDC_PAD_SOURCE_OF].flags = MEDIA_PAD_FL_SOURCE;
2434 me->ops = &ccdc_media_ops;
2435 ret = media_entity_init(me, CCDC_PADS_NUM, pads, 0);
2439 ccdc_init_formats(sd, NULL);
2441 ccdc->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2442 ccdc->video_out.ops = &ccdc_video_ops;
2443 ccdc->video_out.isp = to_isp_device(ccdc);
2444 ccdc->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
2445 ccdc->video_out.bpl_alignment = 32;
2447 ret = omap3isp_video_init(&ccdc->video_out, "CCDC");
2451 /* Connect the CCDC subdev to the video node. */
2452 ret = media_entity_create_link(&ccdc->subdev.entity, CCDC_PAD_SOURCE_OF,
2453 &ccdc->video_out.video.entity, 0, 0);
2460 omap3isp_video_cleanup(&ccdc->video_out);
2462 media_entity_cleanup(me);
2467 * omap3isp_ccdc_init - CCDC module initialization.
2468 * @dev: Device pointer specific to the OMAP3 ISP.
2470 * TODO: Get the initialisation values from platform data.
2472 * Return 0 on success or a negative error code otherwise.
2474 int omap3isp_ccdc_init(struct isp_device *isp)
2476 struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2479 spin_lock_init(&ccdc->lock);
2480 init_waitqueue_head(&ccdc->wait);
2481 mutex_init(&ccdc->ioctl_lock);
2483 ccdc->stopping = CCDC_STOP_NOT_REQUESTED;
2485 INIT_WORK(&ccdc->lsc.table_work, ccdc_lsc_free_table_work);
2486 ccdc->lsc.state = LSC_STATE_STOPPED;
2487 INIT_LIST_HEAD(&ccdc->lsc.free_queue);
2488 spin_lock_init(&ccdc->lsc.req_lock);
2490 ccdc->syncif.ccdc_mastermode = 0;
2491 ccdc->syncif.datapol = 0;
2492 ccdc->syncif.datsz = 0;
2493 ccdc->syncif.fldmode = 0;
2494 ccdc->syncif.fldout = 0;
2495 ccdc->syncif.fldpol = 0;
2496 ccdc->syncif.fldstat = 0;
2498 ccdc->clamp.oblen = 0;
2499 ccdc->clamp.dcsubval = 0;
2501 ccdc->update = OMAP3ISP_CCDC_BLCLAMP;
2502 ccdc_apply_controls(ccdc);
2504 ret = ccdc_init_entities(ccdc);
2506 mutex_destroy(&ccdc->ioctl_lock);
2514 * omap3isp_ccdc_cleanup - CCDC module cleanup.
2515 * @dev: Device pointer specific to the OMAP3 ISP.
2517 void omap3isp_ccdc_cleanup(struct isp_device *isp)
2519 struct isp_ccdc_device *ccdc = &isp->isp_ccdc;
2521 omap3isp_video_cleanup(&ccdc->video_out);
2522 media_entity_cleanup(&ccdc->subdev.entity);
2524 /* Free LSC requests. As the CCDC is stopped there's no active request,
2525 * so only the pending request and the free queue need to be handled.
2527 ccdc_lsc_free_request(ccdc, ccdc->lsc.request);
2528 cancel_work_sync(&ccdc->lsc.table_work);
2529 ccdc_lsc_free_queue(ccdc, &ccdc->lsc.free_queue);
2531 if (ccdc->fpc.fpcaddr != 0)
2532 omap_iommu_vfree(isp->domain, isp->dev, ccdc->fpc.fpcaddr);
2534 mutex_destroy(&ccdc->ioctl_lock);