4 * TI OMAP3 ISP driver - Preview module
6 * Copyright (C) 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.
17 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/uaccess.h>
25 #include "isppreview.h"
27 /* Default values in Office Fluorescent Light for RGBtoRGB Blending */
28 static struct omap3isp_prev_rgbtorgb flr_rgb2rgb = {
29 { /* RGB-RGB Matrix */
30 {0x01E2, 0x0F30, 0x0FEE},
31 {0x0F9B, 0x01AC, 0x0FB9},
32 {0x0FE0, 0x0EC0, 0x0260}
34 {0x0000, 0x0000, 0x0000}
37 /* Default values in Office Fluorescent Light for RGB to YUV Conversion*/
38 static struct omap3isp_prev_csc flr_prev_csc = {
39 { /* CSC Coef Matrix */
47 /* Default values in Office Fluorescent Light for CFA Gradient*/
48 #define FLR_CFA_GRADTHRS_HORZ 0x28
49 #define FLR_CFA_GRADTHRS_VERT 0x28
51 /* Default values in Office Fluorescent Light for Chroma Suppression*/
52 #define FLR_CSUP_GAIN 0x0D
53 #define FLR_CSUP_THRES 0xEB
55 /* Default values in Office Fluorescent Light for Noise Filter*/
56 #define FLR_NF_STRGTH 0x03
58 /* Default values for White Balance */
59 #define FLR_WBAL_DGAIN 0x100
60 #define FLR_WBAL_COEF 0x20
62 /* Default values in Office Fluorescent Light for Black Adjustment*/
63 #define FLR_BLKADJ_BLUE 0x0
64 #define FLR_BLKADJ_GREEN 0x0
65 #define FLR_BLKADJ_RED 0x0
67 #define DEF_DETECT_CORRECT_VAL 0xe
70 * Margins and image size limits.
72 * The preview engine crops several rows and columns internally depending on
73 * which filters are enabled. To avoid format changes when the filters are
74 * enabled or disabled (which would prevent them from being turned on or off
75 * during streaming), the driver assumes all filters that can be configured
76 * during streaming are enabled when computing sink crop and source format
79 * If a filter is disabled, additional cropping is automatically added at the
80 * preview engine input by the driver to avoid overflow at line and frame end.
81 * This is completely transparent for applications.
83 * Median filter 4 pixels
85 * Faulty pixels correction 4 pixels, 4 lines
86 * Color suppression 2 pixels
88 * -------------------------------------------------------------
89 * Maximum total 10 pixels, 4 lines
91 * The color suppression and luma enhancement filters are applied after bayer to
92 * YUV conversion. They thus can crop one pixel on the left and one pixel on the
93 * right side of the image without changing the color pattern. When both those
94 * filters are disabled, the driver must crop the two pixels on the same side of
95 * the image to avoid changing the bayer pattern. The left margin is thus set to
96 * 6 pixels and the right margin to 4 pixels.
99 #define PREV_MARGIN_LEFT 6
100 #define PREV_MARGIN_RIGHT 4
101 #define PREV_MARGIN_TOP 2
102 #define PREV_MARGIN_BOTTOM 2
104 #define PREV_MIN_IN_WIDTH 64
105 #define PREV_MIN_IN_HEIGHT 8
106 #define PREV_MAX_IN_HEIGHT 16384
108 #define PREV_MIN_OUT_WIDTH 0
109 #define PREV_MIN_OUT_HEIGHT 0
110 #define PREV_MAX_OUT_WIDTH_REV_1 1280
111 #define PREV_MAX_OUT_WIDTH_REV_2 3300
112 #define PREV_MAX_OUT_WIDTH_REV_15 4096
115 * Coefficient Tables for the submodules in Preview.
116 * Array is initialised with the values from.the tables text file.
120 * CFA Filter Coefficient Table
123 static u32 cfa_coef_table[4][OMAP3ISP_PREV_CFA_BLK_SIZE] = {
124 #include "cfa_coef_table.h"
128 * Default Gamma Correction Table - All components
130 static u32 gamma_table[] = {
131 #include "gamma_table.h"
135 * Noise Filter Threshold table
137 static u32 noise_filter_table[] = {
138 #include "noise_filter_table.h"
142 * Luminance Enhancement Table
144 static u32 luma_enhance_table[] = {
145 #include "luma_enhance_table.h"
149 * preview_config_luma_enhancement - Configure the Luminance Enhancement table
152 preview_config_luma_enhancement(struct isp_prev_device *prev,
153 const struct prev_params *params)
155 struct isp_device *isp = to_isp_device(prev);
156 const struct omap3isp_prev_luma *yt = ¶ms->luma;
159 isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
160 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
161 for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
162 isp_reg_writel(isp, yt->table[i],
163 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
168 * preview_enable_luma_enhancement - Enable/disable Luminance Enhancement
171 preview_enable_luma_enhancement(struct isp_prev_device *prev, bool enable)
173 struct isp_device *isp = to_isp_device(prev);
176 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
179 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
184 * preview_enable_invalaw - Enable/disable Inverse A-Law decompression
186 static void preview_enable_invalaw(struct isp_prev_device *prev, bool enable)
188 struct isp_device *isp = to_isp_device(prev);
191 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
194 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
199 * preview_config_hmed - Configure the Horizontal Median Filter
201 static void preview_config_hmed(struct isp_prev_device *prev,
202 const struct prev_params *params)
204 struct isp_device *isp = to_isp_device(prev);
205 const struct omap3isp_prev_hmed *hmed = ¶ms->hmed;
207 isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
208 (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
209 (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
210 OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
214 * preview_enable_hmed - Enable/disable the Horizontal Median Filter
216 static void preview_enable_hmed(struct isp_prev_device *prev, bool enable)
218 struct isp_device *isp = to_isp_device(prev);
221 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
224 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
229 * preview_config_cfa - Configure CFA Interpolation for Bayer formats
231 * The CFA table is organised in four blocks, one per Bayer component. The
232 * hardware expects blocks to follow the Bayer order of the input data, while
233 * the driver stores the table in GRBG order in memory. The blocks need to be
234 * reordered to support non-GRBG Bayer patterns.
236 static void preview_config_cfa(struct isp_prev_device *prev,
237 const struct prev_params *params)
239 static const unsigned int cfa_coef_order[4][4] = {
240 { 0, 1, 2, 3 }, /* GRBG */
241 { 1, 0, 3, 2 }, /* RGGB */
242 { 2, 3, 0, 1 }, /* BGGR */
243 { 3, 2, 1, 0 }, /* GBRG */
245 const unsigned int *order = cfa_coef_order[prev->params.cfa_order];
246 const struct omap3isp_prev_cfa *cfa = ¶ms->cfa;
247 struct isp_device *isp = to_isp_device(prev);
252 (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
253 (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
254 OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
256 isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
257 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
259 for (i = 0; i < 4; ++i) {
260 const __u32 *block = cfa->table[order[i]];
262 for (j = 0; j < OMAP3ISP_PREV_CFA_BLK_SIZE; ++j)
263 isp_reg_writel(isp, block[j], OMAP3_ISP_IOMEM_PREV,
264 ISPPRV_SET_TBL_DATA);
269 * preview_config_chroma_suppression - Configure Chroma Suppression
272 preview_config_chroma_suppression(struct isp_prev_device *prev,
273 const struct prev_params *params)
275 struct isp_device *isp = to_isp_device(prev);
276 const struct omap3isp_prev_csup *cs = ¶ms->csup;
279 cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
280 (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
281 OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
285 * preview_enable_chroma_suppression - Enable/disable Chrominance Suppression
288 preview_enable_chroma_suppression(struct isp_prev_device *prev, bool enable)
290 struct isp_device *isp = to_isp_device(prev);
293 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
296 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
301 * preview_config_whitebalance - Configure White Balance parameters
303 * Coefficient matrix always with default values.
306 preview_config_whitebalance(struct isp_prev_device *prev,
307 const struct prev_params *params)
309 struct isp_device *isp = to_isp_device(prev);
310 const struct omap3isp_prev_wbal *wbal = ¶ms->wbal;
313 isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
315 val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
316 val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
317 val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
318 val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
319 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
322 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
323 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
324 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
325 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
326 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
327 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
328 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
329 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
330 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
331 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
332 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
333 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
334 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
335 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
336 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
337 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
338 OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
342 * preview_config_blkadj - Configure Black Adjustment
345 preview_config_blkadj(struct isp_prev_device *prev,
346 const struct prev_params *params)
348 struct isp_device *isp = to_isp_device(prev);
349 const struct omap3isp_prev_blkadj *blkadj = ¶ms->blkadj;
351 isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
352 (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
353 (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
354 OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
358 * preview_config_rgb_blending - Configure RGB-RGB Blending
361 preview_config_rgb_blending(struct isp_prev_device *prev,
362 const struct prev_params *params)
364 struct isp_device *isp = to_isp_device(prev);
365 const struct omap3isp_prev_rgbtorgb *rgbrgb = ¶ms->rgb2rgb;
368 val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
369 val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
370 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
372 val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
373 val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
374 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
376 val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
377 val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
378 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
380 val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
381 val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
382 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
384 val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
385 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
387 val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
388 val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
389 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
391 val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
392 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
396 * preview_config_csc - Configure Color Space Conversion (RGB to YCbYCr)
399 preview_config_csc(struct isp_prev_device *prev,
400 const struct prev_params *params)
402 struct isp_device *isp = to_isp_device(prev);
403 const struct omap3isp_prev_csc *csc = ¶ms->csc;
406 val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
407 val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
408 val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
409 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
411 val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
412 val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
413 val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
414 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
416 val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
417 val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
418 val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
419 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
421 val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
422 val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
423 val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
424 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
428 * preview_config_yc_range - Configure the max and min Y and C values
431 preview_config_yc_range(struct isp_prev_device *prev,
432 const struct prev_params *params)
434 struct isp_device *isp = to_isp_device(prev);
435 const struct omap3isp_prev_yclimit *yc = ¶ms->yclimit;
438 yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
439 yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
440 yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
441 yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
442 OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
446 * preview_config_dcor - Configure Couplet Defect Correction
449 preview_config_dcor(struct isp_prev_device *prev,
450 const struct prev_params *params)
452 struct isp_device *isp = to_isp_device(prev);
453 const struct omap3isp_prev_dcor *dcor = ¶ms->dcor;
455 isp_reg_writel(isp, dcor->detect_correct[0],
456 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
457 isp_reg_writel(isp, dcor->detect_correct[1],
458 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
459 isp_reg_writel(isp, dcor->detect_correct[2],
460 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
461 isp_reg_writel(isp, dcor->detect_correct[3],
462 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
463 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
465 dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
469 * preview_enable_dcor - Enable/disable Couplet Defect Correction
471 static void preview_enable_dcor(struct isp_prev_device *prev, bool enable)
473 struct isp_device *isp = to_isp_device(prev);
476 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
479 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
484 * preview_enable_drkframe_capture - Enable/disable Dark Frame Capture
487 preview_enable_drkframe_capture(struct isp_prev_device *prev, bool enable)
489 struct isp_device *isp = to_isp_device(prev);
492 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
495 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
500 * preview_enable_drkframe - Enable/disable Dark Frame Subtraction
502 static void preview_enable_drkframe(struct isp_prev_device *prev, bool enable)
504 struct isp_device *isp = to_isp_device(prev);
507 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
510 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
515 * preview_config_noisefilter - Configure the Noise Filter
518 preview_config_noisefilter(struct isp_prev_device *prev,
519 const struct prev_params *params)
521 struct isp_device *isp = to_isp_device(prev);
522 const struct omap3isp_prev_nf *nf = ¶ms->nf;
525 isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
526 isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
527 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
528 for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
529 isp_reg_writel(isp, nf->table[i],
530 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
535 * preview_enable_noisefilter - Enable/disable the Noise Filter
538 preview_enable_noisefilter(struct isp_prev_device *prev, bool enable)
540 struct isp_device *isp = to_isp_device(prev);
543 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
546 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
551 * preview_config_gammacorrn - Configure the Gamma Correction tables
554 preview_config_gammacorrn(struct isp_prev_device *prev,
555 const struct prev_params *params)
557 struct isp_device *isp = to_isp_device(prev);
558 const struct omap3isp_prev_gtables *gt = ¶ms->gamma;
561 isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
562 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
563 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
564 isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
565 ISPPRV_SET_TBL_DATA);
567 isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
568 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
569 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
570 isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
571 ISPPRV_SET_TBL_DATA);
573 isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
574 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
575 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
576 isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
577 ISPPRV_SET_TBL_DATA);
581 * preview_enable_gammacorrn - Enable/disable Gamma Correction
583 * When gamma correction is disabled, the module is bypassed and its output is
584 * the 8 MSB of the 10-bit input .
587 preview_enable_gammacorrn(struct isp_prev_device *prev, bool enable)
589 struct isp_device *isp = to_isp_device(prev);
592 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
593 ISPPRV_PCR_GAMMA_BYPASS);
595 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
596 ISPPRV_PCR_GAMMA_BYPASS);
600 * preview_config_contrast - Configure the Contrast
602 * Value should be programmed before enabling the module.
605 preview_config_contrast(struct isp_prev_device *prev,
606 const struct prev_params *params)
608 struct isp_device *isp = to_isp_device(prev);
610 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
611 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
612 params->contrast << ISPPRV_CNT_BRT_CNT_SHIFT);
616 * preview_config_brightness - Configure the Brightness
619 preview_config_brightness(struct isp_prev_device *prev,
620 const struct prev_params *params)
622 struct isp_device *isp = to_isp_device(prev);
624 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
625 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
626 params->brightness << ISPPRV_CNT_BRT_BRT_SHIFT);
630 * preview_update_contrast - Updates the contrast.
631 * @contrast: Pointer to hold the current programmed contrast value.
633 * Value should be programmed before enabling the module.
636 preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
638 struct prev_params *params;
641 spin_lock_irqsave(&prev->params.lock, flags);
642 params = (prev->params.active & OMAP3ISP_PREV_CONTRAST)
643 ? &prev->params.params[0] : &prev->params.params[1];
645 if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
646 params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
647 params->update |= OMAP3ISP_PREV_CONTRAST;
649 spin_unlock_irqrestore(&prev->params.lock, flags);
653 * preview_update_brightness - Updates the brightness in preview module.
654 * @brightness: Pointer to hold the current programmed brightness value.
658 preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
660 struct prev_params *params;
663 spin_lock_irqsave(&prev->params.lock, flags);
664 params = (prev->params.active & OMAP3ISP_PREV_BRIGHTNESS)
665 ? &prev->params.params[0] : &prev->params.params[1];
667 if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
668 params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
669 params->update |= OMAP3ISP_PREV_BRIGHTNESS;
671 spin_unlock_irqrestore(&prev->params.lock, flags);
675 preview_params_lock(struct isp_prev_device *prev, u32 update, bool shadow)
677 u32 active = prev->params.active;
680 /* Mark all shadow parameters we are going to touch as busy. */
681 prev->params.params[0].busy |= ~active & update;
682 prev->params.params[1].busy |= active & update;
684 /* Mark all active parameters we are going to touch as busy. */
685 update = (prev->params.params[0].update & active)
686 | (prev->params.params[1].update & ~active);
688 prev->params.params[0].busy |= active & update;
689 prev->params.params[1].busy |= ~active & update;
696 preview_params_unlock(struct isp_prev_device *prev, u32 update, bool shadow)
698 u32 active = prev->params.active;
701 /* Set the update flag for shadow parameters that have been
702 * updated and clear the busy flag for all shadow parameters.
704 prev->params.params[0].update |= (~active & update);
705 prev->params.params[1].update |= (active & update);
706 prev->params.params[0].busy &= active;
707 prev->params.params[1].busy &= ~active;
709 /* Clear the update flag for active parameters that have been
710 * applied and the busy flag for all active parameters.
712 prev->params.params[0].update &= ~(active & update);
713 prev->params.params[1].update &= ~(~active & update);
714 prev->params.params[0].busy &= ~active;
715 prev->params.params[1].busy &= active;
719 static void preview_params_switch(struct isp_prev_device *prev)
723 /* Switch active parameters with updated shadow parameters when the
724 * shadow parameter has been updated and neither the active not the
725 * shadow parameter is busy.
727 to_switch = (prev->params.params[0].update & ~prev->params.active)
728 | (prev->params.params[1].update & prev->params.active);
729 to_switch &= ~(prev->params.params[0].busy |
730 prev->params.params[1].busy);
734 prev->params.active ^= to_switch;
736 /* Remove the update flag for the shadow copy of parameters we have
739 prev->params.params[0].update &= ~(~prev->params.active & to_switch);
740 prev->params.params[1].update &= ~(prev->params.active & to_switch);
743 /* preview parameters update structure */
744 struct preview_update {
745 void (*config)(struct isp_prev_device *, const struct prev_params *);
746 void (*enable)(struct isp_prev_device *, bool);
747 unsigned int param_offset;
748 unsigned int param_size;
749 unsigned int config_offset;
753 /* Keep the array indexed by the OMAP3ISP_PREV_* bit number. */
754 static const struct preview_update update_attrs[] = {
755 /* OMAP3ISP_PREV_LUMAENH */ {
756 preview_config_luma_enhancement,
757 preview_enable_luma_enhancement,
758 offsetof(struct prev_params, luma),
759 FIELD_SIZEOF(struct prev_params, luma),
760 offsetof(struct omap3isp_prev_update_config, luma),
761 }, /* OMAP3ISP_PREV_INVALAW */ {
763 preview_enable_invalaw,
764 }, /* OMAP3ISP_PREV_HRZ_MED */ {
767 offsetof(struct prev_params, hmed),
768 FIELD_SIZEOF(struct prev_params, hmed),
769 offsetof(struct omap3isp_prev_update_config, hmed),
770 }, /* OMAP3ISP_PREV_CFA */ {
773 offsetof(struct prev_params, cfa),
774 FIELD_SIZEOF(struct prev_params, cfa),
775 offsetof(struct omap3isp_prev_update_config, cfa),
776 }, /* OMAP3ISP_PREV_CHROMA_SUPP */ {
777 preview_config_chroma_suppression,
778 preview_enable_chroma_suppression,
779 offsetof(struct prev_params, csup),
780 FIELD_SIZEOF(struct prev_params, csup),
781 offsetof(struct omap3isp_prev_update_config, csup),
782 }, /* OMAP3ISP_PREV_WB */ {
783 preview_config_whitebalance,
785 offsetof(struct prev_params, wbal),
786 FIELD_SIZEOF(struct prev_params, wbal),
787 offsetof(struct omap3isp_prev_update_config, wbal),
788 }, /* OMAP3ISP_PREV_BLKADJ */ {
789 preview_config_blkadj,
791 offsetof(struct prev_params, blkadj),
792 FIELD_SIZEOF(struct prev_params, blkadj),
793 offsetof(struct omap3isp_prev_update_config, blkadj),
794 }, /* OMAP3ISP_PREV_RGB2RGB */ {
795 preview_config_rgb_blending,
797 offsetof(struct prev_params, rgb2rgb),
798 FIELD_SIZEOF(struct prev_params, rgb2rgb),
799 offsetof(struct omap3isp_prev_update_config, rgb2rgb),
800 }, /* OMAP3ISP_PREV_COLOR_CONV */ {
803 offsetof(struct prev_params, csc),
804 FIELD_SIZEOF(struct prev_params, csc),
805 offsetof(struct omap3isp_prev_update_config, csc),
806 }, /* OMAP3ISP_PREV_YC_LIMIT */ {
807 preview_config_yc_range,
809 offsetof(struct prev_params, yclimit),
810 FIELD_SIZEOF(struct prev_params, yclimit),
811 offsetof(struct omap3isp_prev_update_config, yclimit),
812 }, /* OMAP3ISP_PREV_DEFECT_COR */ {
815 offsetof(struct prev_params, dcor),
816 FIELD_SIZEOF(struct prev_params, dcor),
817 offsetof(struct omap3isp_prev_update_config, dcor),
818 }, /* Previously OMAP3ISP_PREV_GAMMABYPASS, not used anymore */ {
821 }, /* OMAP3ISP_PREV_DRK_FRM_CAPTURE */ {
823 preview_enable_drkframe_capture,
824 }, /* OMAP3ISP_PREV_DRK_FRM_SUBTRACT */ {
826 preview_enable_drkframe,
827 }, /* OMAP3ISP_PREV_LENS_SHADING */ {
829 preview_enable_drkframe,
830 }, /* OMAP3ISP_PREV_NF */ {
831 preview_config_noisefilter,
832 preview_enable_noisefilter,
833 offsetof(struct prev_params, nf),
834 FIELD_SIZEOF(struct prev_params, nf),
835 offsetof(struct omap3isp_prev_update_config, nf),
836 }, /* OMAP3ISP_PREV_GAMMA */ {
837 preview_config_gammacorrn,
838 preview_enable_gammacorrn,
839 offsetof(struct prev_params, gamma),
840 FIELD_SIZEOF(struct prev_params, gamma),
841 offsetof(struct omap3isp_prev_update_config, gamma),
842 }, /* OMAP3ISP_PREV_CONTRAST */ {
843 preview_config_contrast,
846 }, /* OMAP3ISP_PREV_BRIGHTNESS */ {
847 preview_config_brightness,
854 * preview_config - Copy and update local structure with userspace preview
856 * @prev: ISP preview engine
857 * @cfg: Configuration
859 * Return zero if success or -EFAULT if the configuration can't be copied from
862 static int preview_config(struct isp_prev_device *prev,
863 struct omap3isp_prev_update_config *cfg)
871 if (cfg->update == 0)
874 /* Mark the shadow parameters we're going to update as busy. */
875 spin_lock_irqsave(&prev->params.lock, flags);
876 preview_params_lock(prev, cfg->update, true);
877 active = prev->params.active;
878 spin_unlock_irqrestore(&prev->params.lock, flags);
882 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
883 const struct preview_update *attr = &update_attrs[i];
884 struct prev_params *params;
885 unsigned int bit = 1 << i;
887 if (attr->skip || !(cfg->update & bit))
890 params = &prev->params.params[!!(active & bit)];
892 if (cfg->flag & bit) {
893 void __user *from = *(void * __user *)
894 ((void *)cfg + attr->config_offset);
895 void *to = (void *)params + attr->param_offset;
896 size_t size = attr->param_size;
898 if (to && from && size) {
899 if (copy_from_user(to, from, size)) {
904 params->features |= bit;
906 params->features &= ~bit;
912 spin_lock_irqsave(&prev->params.lock, flags);
913 preview_params_unlock(prev, update, true);
914 preview_params_switch(prev);
915 spin_unlock_irqrestore(&prev->params.lock, flags);
921 * preview_setup_hw - Setup preview registers and/or internal memory
922 * @prev: pointer to preview private structure
923 * @update: Bitmask of parameters to setup
924 * @active: Bitmask of parameters active in set 0
925 * Note: can be called from interrupt context
928 static void preview_setup_hw(struct isp_prev_device *prev, u32 update,
937 features = (prev->params.params[0].features & active)
938 | (prev->params.params[1].features & ~active);
940 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
941 const struct preview_update *attr = &update_attrs[i];
942 struct prev_params *params;
943 unsigned int bit = 1 << i;
948 params = &prev->params.params[!(active & bit)];
950 if (params->features & bit) {
952 attr->config(prev, params);
954 attr->enable(prev, true);
957 attr->enable(prev, false);
963 * preview_config_ycpos - Configure byte layout of YUV image.
964 * @prev: pointer to previewer private structure
965 * @pixelcode: pixel code
968 preview_config_ycpos(struct isp_prev_device *prev,
969 enum v4l2_mbus_pixelcode pixelcode)
971 struct isp_device *isp = to_isp_device(prev);
972 enum preview_ycpos_mode mode;
975 case V4L2_MBUS_FMT_YUYV8_1X16:
978 case V4L2_MBUS_FMT_UYVY8_1X16:
985 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
986 ISPPRV_PCR_YCPOS_CrYCbY,
987 mode << ISPPRV_PCR_YCPOS_SHIFT);
991 * preview_config_averager - Enable / disable / configure averager
992 * @average: Average value to be configured.
994 static void preview_config_averager(struct isp_prev_device *prev, u8 average)
996 struct isp_device *isp = to_isp_device(prev);
998 isp_reg_writel(isp, ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
999 ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
1000 average, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
1005 * preview_config_input_format - Configure the input format
1006 * @prev: The preview engine
1007 * @info: Sink pad format information
1009 * Enable and configure CFA interpolation for Bayer formats and disable it for
1010 * greyscale formats.
1012 * The CFA table is organised in four blocks, one per Bayer component. The
1013 * hardware expects blocks to follow the Bayer order of the input data, while
1014 * the driver stores the table in GRBG order in memory. The blocks need to be
1015 * reordered to support non-GRBG Bayer patterns.
1017 static void preview_config_input_format(struct isp_prev_device *prev,
1018 const struct isp_format_info *info)
1020 struct isp_device *isp = to_isp_device(prev);
1021 struct prev_params *params;
1023 if (info->width == 8)
1024 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1027 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1030 switch (info->flavor) {
1031 case V4L2_MBUS_FMT_SGRBG8_1X8:
1032 prev->params.cfa_order = 0;
1034 case V4L2_MBUS_FMT_SRGGB8_1X8:
1035 prev->params.cfa_order = 1;
1037 case V4L2_MBUS_FMT_SBGGR8_1X8:
1038 prev->params.cfa_order = 2;
1040 case V4L2_MBUS_FMT_SGBRG8_1X8:
1041 prev->params.cfa_order = 3;
1044 /* Disable CFA for non-Bayer formats. */
1045 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1050 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR, ISPPRV_PCR_CFAEN);
1051 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1052 ISPPRV_PCR_CFAFMT_MASK, ISPPRV_PCR_CFAFMT_BAYER);
1054 params = (prev->params.active & OMAP3ISP_PREV_CFA)
1055 ? &prev->params.params[0] : &prev->params.params[1];
1057 preview_config_cfa(prev, params);
1061 * preview_config_input_size - Configure the input frame size
1063 * The preview engine crops several rows and columns internally depending on
1064 * which processing blocks are enabled. The driver assumes all those blocks are
1065 * enabled when reporting source pad formats to userspace. If this assumption is
1066 * not true, rows and columns must be manually cropped at the preview engine
1067 * input to avoid overflows at the end of lines and frames.
1069 * See the explanation at the PREV_MARGIN_* definitions for more details.
1071 static void preview_config_input_size(struct isp_prev_device *prev, u32 active)
1073 const struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
1074 struct isp_device *isp = to_isp_device(prev);
1075 unsigned int sph = prev->crop.left;
1076 unsigned int eph = prev->crop.left + prev->crop.width - 1;
1077 unsigned int slv = prev->crop.top;
1078 unsigned int elv = prev->crop.top + prev->crop.height - 1;
1081 if (format->code != V4L2_MBUS_FMT_Y8_1X8 &&
1082 format->code != V4L2_MBUS_FMT_Y10_1X10) {
1089 features = (prev->params.params[0].features & active)
1090 | (prev->params.params[1].features & ~active);
1092 if (features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
1098 if (features & OMAP3ISP_PREV_HRZ_MED) {
1102 if (features & (OMAP3ISP_PREV_CHROMA_SUPP | OMAP3ISP_PREV_LUMAENH))
1105 isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
1106 OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
1107 isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
1108 OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
1112 * preview_config_inlineoffset - Configures the Read address line offset.
1113 * @prev: Preview module
1114 * @offset: Line offset
1116 * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
1117 * However, a hardware bug requires the memory start address to be aligned on a
1118 * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
1122 preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
1124 struct isp_device *isp = to_isp_device(prev);
1126 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1127 ISPPRV_RADR_OFFSET);
1131 * preview_set_inaddr - Sets memory address of input frame.
1132 * @addr: 32bit memory address aligned on 32byte boundary.
1134 * Configures the memory address from which the input frame is to be read.
1136 static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
1138 struct isp_device *isp = to_isp_device(prev);
1140 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
1144 * preview_config_outlineoffset - Configures the Write address line offset.
1145 * @offset: Line Offset for the preview output.
1147 * The offset must be a multiple of 32 bytes.
1149 static void preview_config_outlineoffset(struct isp_prev_device *prev,
1152 struct isp_device *isp = to_isp_device(prev);
1154 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1155 ISPPRV_WADD_OFFSET);
1159 * preview_set_outaddr - Sets the memory address to store output frame
1160 * @addr: 32bit memory address aligned on 32byte boundary.
1162 * Configures the memory address to which the output frame is written.
1164 static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
1166 struct isp_device *isp = to_isp_device(prev);
1168 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
1171 static void preview_adjust_bandwidth(struct isp_prev_device *prev)
1173 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1174 struct isp_device *isp = to_isp_device(prev);
1175 const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
1176 unsigned long l3_ick = pipe->l3_ick;
1177 struct v4l2_fract *timeperframe;
1178 unsigned int cycles_per_frame;
1179 unsigned int requests_per_frame;
1180 unsigned int cycles_per_request;
1181 unsigned int minimum;
1182 unsigned int maximum;
1185 if (prev->input != PREVIEW_INPUT_MEMORY) {
1186 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1187 ISPSBL_SDR_REQ_PRV_EXP_MASK);
1191 /* Compute the minimum number of cycles per request, based on the
1192 * pipeline maximum data rate. This is an absolute lower bound if we
1193 * don't want SBL overflows, so round the value up.
1195 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
1197 minimum = DIV_ROUND_UP(cycles_per_request, 32);
1199 /* Compute the maximum number of cycles per request, based on the
1200 * requested frame rate. This is a soft upper bound to achieve a frame
1201 * rate equal or higher than the requested value, so round the value
1204 timeperframe = &pipe->max_timeperframe;
1206 requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
1207 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
1208 timeperframe->denominator);
1209 cycles_per_request = cycles_per_frame / requests_per_frame;
1211 maximum = cycles_per_request / 32;
1213 value = max(minimum, maximum);
1215 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
1216 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1217 ISPSBL_SDR_REQ_PRV_EXP_MASK,
1218 value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
1222 * omap3isp_preview_busy - Gets busy state of preview module.
1224 int omap3isp_preview_busy(struct isp_prev_device *prev)
1226 struct isp_device *isp = to_isp_device(prev);
1228 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
1233 * omap3isp_preview_restore_context - Restores the values of preview registers
1235 void omap3isp_preview_restore_context(struct isp_device *isp)
1237 struct isp_prev_device *prev = &isp->isp_prev;
1238 const u32 update = OMAP3ISP_PREV_FEATURES_END - 1;
1240 prev->params.params[0].update = prev->params.active & update;
1241 prev->params.params[1].update = ~prev->params.active & update;
1243 preview_setup_hw(prev, update, prev->params.active);
1245 prev->params.params[0].update = 0;
1246 prev->params.params[1].update = 0;
1250 * preview_print_status - Dump preview module registers to the kernel log
1252 #define PREV_PRINT_REGISTER(isp, name)\
1253 dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
1254 isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
1256 static void preview_print_status(struct isp_prev_device *prev)
1258 struct isp_device *isp = to_isp_device(prev);
1260 dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
1262 PREV_PRINT_REGISTER(isp, PCR);
1263 PREV_PRINT_REGISTER(isp, HORZ_INFO);
1264 PREV_PRINT_REGISTER(isp, VERT_INFO);
1265 PREV_PRINT_REGISTER(isp, RSDR_ADDR);
1266 PREV_PRINT_REGISTER(isp, RADR_OFFSET);
1267 PREV_PRINT_REGISTER(isp, DSDR_ADDR);
1268 PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
1269 PREV_PRINT_REGISTER(isp, WSDR_ADDR);
1270 PREV_PRINT_REGISTER(isp, WADD_OFFSET);
1271 PREV_PRINT_REGISTER(isp, AVE);
1272 PREV_PRINT_REGISTER(isp, HMED);
1273 PREV_PRINT_REGISTER(isp, NF);
1274 PREV_PRINT_REGISTER(isp, WB_DGAIN);
1275 PREV_PRINT_REGISTER(isp, WBGAIN);
1276 PREV_PRINT_REGISTER(isp, WBSEL);
1277 PREV_PRINT_REGISTER(isp, CFA);
1278 PREV_PRINT_REGISTER(isp, BLKADJOFF);
1279 PREV_PRINT_REGISTER(isp, RGB_MAT1);
1280 PREV_PRINT_REGISTER(isp, RGB_MAT2);
1281 PREV_PRINT_REGISTER(isp, RGB_MAT3);
1282 PREV_PRINT_REGISTER(isp, RGB_MAT4);
1283 PREV_PRINT_REGISTER(isp, RGB_MAT5);
1284 PREV_PRINT_REGISTER(isp, RGB_OFF1);
1285 PREV_PRINT_REGISTER(isp, RGB_OFF2);
1286 PREV_PRINT_REGISTER(isp, CSC0);
1287 PREV_PRINT_REGISTER(isp, CSC1);
1288 PREV_PRINT_REGISTER(isp, CSC2);
1289 PREV_PRINT_REGISTER(isp, CSC_OFFSET);
1290 PREV_PRINT_REGISTER(isp, CNT_BRT);
1291 PREV_PRINT_REGISTER(isp, CSUP);
1292 PREV_PRINT_REGISTER(isp, SETUP_YC);
1293 PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
1294 PREV_PRINT_REGISTER(isp, CDC_THR0);
1295 PREV_PRINT_REGISTER(isp, CDC_THR1);
1296 PREV_PRINT_REGISTER(isp, CDC_THR2);
1297 PREV_PRINT_REGISTER(isp, CDC_THR3);
1299 dev_dbg(isp->dev, "--------------------------------------------\n");
1303 * preview_init_params - init image processing parameters.
1304 * @prev: pointer to previewer private structure
1306 static void preview_init_params(struct isp_prev_device *prev)
1308 struct prev_params *params;
1311 spin_lock_init(&prev->params.lock);
1313 prev->params.active = ~0;
1314 prev->params.params[0].busy = 0;
1315 prev->params.params[0].update = OMAP3ISP_PREV_FEATURES_END - 1;
1316 prev->params.params[1].busy = 0;
1317 prev->params.params[1].update = 0;
1319 params = &prev->params.params[0];
1322 params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
1323 params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
1324 params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
1325 memcpy(params->cfa.table, cfa_coef_table,
1326 sizeof(params->cfa.table));
1327 params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
1328 params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
1329 params->csup.gain = FLR_CSUP_GAIN;
1330 params->csup.thres = FLR_CSUP_THRES;
1331 params->csup.hypf_en = 0;
1332 memcpy(params->luma.table, luma_enhance_table,
1333 sizeof(params->luma.table));
1334 params->nf.spread = FLR_NF_STRGTH;
1335 memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
1336 params->dcor.couplet_mode_en = 1;
1337 for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
1338 params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
1339 memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
1340 memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
1341 memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
1342 params->wbal.dgain = FLR_WBAL_DGAIN;
1343 params->wbal.coef0 = FLR_WBAL_COEF;
1344 params->wbal.coef1 = FLR_WBAL_COEF;
1345 params->wbal.coef2 = FLR_WBAL_COEF;
1346 params->wbal.coef3 = FLR_WBAL_COEF;
1347 params->blkadj.red = FLR_BLKADJ_RED;
1348 params->blkadj.green = FLR_BLKADJ_GREEN;
1349 params->blkadj.blue = FLR_BLKADJ_BLUE;
1350 params->rgb2rgb = flr_rgb2rgb;
1351 params->csc = flr_prev_csc;
1352 params->yclimit.minC = ISPPRV_YC_MIN;
1353 params->yclimit.maxC = ISPPRV_YC_MAX;
1354 params->yclimit.minY = ISPPRV_YC_MIN;
1355 params->yclimit.maxY = ISPPRV_YC_MAX;
1357 params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
1358 | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
1359 | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
1360 | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
1361 | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
1362 | OMAP3ISP_PREV_CONTRAST;
1366 * preview_max_out_width - Handle previewer hardware output limitations
1367 * @prev: pointer to previewer private structure
1368 * returns maximum width output for current isp revision
1370 static unsigned int preview_max_out_width(struct isp_prev_device *prev)
1372 struct isp_device *isp = to_isp_device(prev);
1374 switch (isp->revision) {
1375 case ISP_REVISION_1_0:
1376 return PREV_MAX_OUT_WIDTH_REV_1;
1378 case ISP_REVISION_2_0:
1380 return PREV_MAX_OUT_WIDTH_REV_2;
1382 case ISP_REVISION_15_0:
1383 return PREV_MAX_OUT_WIDTH_REV_15;
1387 static void preview_configure(struct isp_prev_device *prev)
1389 struct isp_device *isp = to_isp_device(prev);
1390 const struct isp_format_info *info;
1391 struct v4l2_mbus_framefmt *format;
1392 unsigned long flags;
1396 spin_lock_irqsave(&prev->params.lock, flags);
1397 /* Mark all active parameters we are going to touch as busy. */
1398 update = preview_params_lock(prev, 0, false);
1399 active = prev->params.active;
1400 spin_unlock_irqrestore(&prev->params.lock, flags);
1403 format = &prev->formats[PREV_PAD_SINK];
1404 info = omap3isp_video_format_info(format->code);
1406 preview_adjust_bandwidth(prev);
1408 preview_config_input_format(prev, info);
1409 preview_config_input_size(prev, active);
1411 if (prev->input == PREVIEW_INPUT_CCDC)
1412 preview_config_inlineoffset(prev, 0);
1414 preview_config_inlineoffset(prev, ALIGN(format->width, 0x20) *
1417 preview_setup_hw(prev, update, active);
1419 /* PREV_PAD_SOURCE */
1420 format = &prev->formats[PREV_PAD_SOURCE];
1422 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1423 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1424 ISPPRV_PCR_SDRPORT);
1426 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1427 ISPPRV_PCR_SDRPORT);
1429 if (prev->output & PREVIEW_OUTPUT_RESIZER)
1430 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1431 ISPPRV_PCR_RSZPORT);
1433 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1434 ISPPRV_PCR_RSZPORT);
1436 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1437 preview_config_outlineoffset(prev,
1438 ALIGN(format->width, 0x10) * 2);
1440 preview_config_averager(prev, 0);
1441 preview_config_ycpos(prev, format->code);
1443 spin_lock_irqsave(&prev->params.lock, flags);
1444 preview_params_unlock(prev, update, false);
1445 spin_unlock_irqrestore(&prev->params.lock, flags);
1448 /* -----------------------------------------------------------------------------
1449 * Interrupt handling
1452 static void preview_enable_oneshot(struct isp_prev_device *prev)
1454 struct isp_device *isp = to_isp_device(prev);
1456 /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
1457 * bit is set. As the preview engine is used in single-shot mode, we
1458 * need to set PCR.SOURCE before enabling the preview engine.
1460 if (prev->input == PREVIEW_INPUT_MEMORY)
1461 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1464 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1465 ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
1468 void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
1471 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1472 * condition, the module was paused and now we have a buffer queued
1473 * on the output again. Restart the pipeline if running in continuous
1476 if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1477 prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1478 preview_enable_oneshot(prev);
1479 isp_video_dmaqueue_flags_clr(&prev->video_out);
1483 static void preview_isr_buffer(struct isp_prev_device *prev)
1485 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1486 struct isp_buffer *buffer;
1489 if (prev->input == PREVIEW_INPUT_MEMORY) {
1490 buffer = omap3isp_video_buffer_next(&prev->video_in);
1492 preview_set_inaddr(prev, buffer->dma);
1493 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1496 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
1497 buffer = omap3isp_video_buffer_next(&prev->video_out);
1498 if (buffer != NULL) {
1499 preview_set_outaddr(prev, buffer->dma);
1502 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1505 switch (prev->state) {
1506 case ISP_PIPELINE_STREAM_SINGLESHOT:
1507 if (isp_pipeline_ready(pipe))
1508 omap3isp_pipeline_set_stream(pipe,
1509 ISP_PIPELINE_STREAM_SINGLESHOT);
1512 case ISP_PIPELINE_STREAM_CONTINUOUS:
1513 /* If an underrun occurs, the video queue operation handler will
1514 * restart the preview engine. Otherwise restart it immediately.
1517 preview_enable_oneshot(prev);
1520 case ISP_PIPELINE_STREAM_STOPPED:
1527 * omap3isp_preview_isr - ISP preview engine interrupt handler
1529 * Manage the preview engine video buffers and configure shadowed registers.
1531 void omap3isp_preview_isr(struct isp_prev_device *prev)
1533 unsigned long flags;
1537 if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
1540 spin_lock_irqsave(&prev->params.lock, flags);
1541 preview_params_switch(prev);
1542 update = preview_params_lock(prev, 0, false);
1543 active = prev->params.active;
1544 spin_unlock_irqrestore(&prev->params.lock, flags);
1546 preview_setup_hw(prev, update, active);
1547 preview_config_input_size(prev, active);
1549 if (prev->input == PREVIEW_INPUT_MEMORY ||
1550 prev->output & PREVIEW_OUTPUT_MEMORY)
1551 preview_isr_buffer(prev);
1552 else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1553 preview_enable_oneshot(prev);
1555 spin_lock_irqsave(&prev->params.lock, flags);
1556 preview_params_unlock(prev, update, false);
1557 spin_unlock_irqrestore(&prev->params.lock, flags);
1560 /* -----------------------------------------------------------------------------
1561 * ISP video operations
1564 static int preview_video_queue(struct isp_video *video,
1565 struct isp_buffer *buffer)
1567 struct isp_prev_device *prev = &video->isp->isp_prev;
1569 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1570 preview_set_inaddr(prev, buffer->dma);
1572 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1573 preview_set_outaddr(prev, buffer->dma);
1578 static const struct isp_video_operations preview_video_ops = {
1579 .queue = preview_video_queue,
1582 /* -----------------------------------------------------------------------------
1583 * V4L2 subdev operations
1587 * preview_s_ctrl - Handle set control subdev method
1588 * @ctrl: pointer to v4l2 control structure
1590 static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
1592 struct isp_prev_device *prev =
1593 container_of(ctrl->handler, struct isp_prev_device, ctrls);
1596 case V4L2_CID_BRIGHTNESS:
1597 preview_update_brightness(prev, ctrl->val);
1599 case V4L2_CID_CONTRAST:
1600 preview_update_contrast(prev, ctrl->val);
1607 static const struct v4l2_ctrl_ops preview_ctrl_ops = {
1608 .s_ctrl = preview_s_ctrl,
1612 * preview_ioctl - Handle preview module private ioctl's
1613 * @sd: pointer to v4l2 subdev structure
1614 * @cmd: configuration command
1615 * @arg: configuration argument
1616 * return -EINVAL or zero on success
1618 static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1620 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1623 case VIDIOC_OMAP3ISP_PRV_CFG:
1624 return preview_config(prev, arg);
1627 return -ENOIOCTLCMD;
1632 * preview_set_stream - Enable/Disable streaming on preview subdev
1633 * @sd : pointer to v4l2 subdev structure
1634 * @enable: 1 == Enable, 0 == Disable
1635 * return -EINVAL or zero on success
1637 static int preview_set_stream(struct v4l2_subdev *sd, int enable)
1639 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1640 struct isp_video *video_out = &prev->video_out;
1641 struct isp_device *isp = to_isp_device(prev);
1642 struct device *dev = to_device(prev);
1644 if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
1645 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1648 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1649 preview_configure(prev);
1650 atomic_set(&prev->stopping, 0);
1651 preview_print_status(prev);
1655 case ISP_PIPELINE_STREAM_CONTINUOUS:
1656 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1657 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1659 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
1660 !(prev->output & PREVIEW_OUTPUT_MEMORY))
1661 preview_enable_oneshot(prev);
1663 isp_video_dmaqueue_flags_clr(video_out);
1666 case ISP_PIPELINE_STREAM_SINGLESHOT:
1667 if (prev->input == PREVIEW_INPUT_MEMORY)
1668 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1669 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1670 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1672 preview_enable_oneshot(prev);
1675 case ISP_PIPELINE_STREAM_STOPPED:
1676 if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
1678 dev_dbg(dev, "%s: stop timeout.\n", sd->name);
1679 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1680 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1681 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1682 isp_video_dmaqueue_flags_clr(video_out);
1686 prev->state = enable;
1690 static struct v4l2_mbus_framefmt *
1691 __preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1692 unsigned int pad, enum v4l2_subdev_format_whence which)
1694 if (which == V4L2_SUBDEV_FORMAT_TRY)
1695 return v4l2_subdev_get_try_format(fh, pad);
1697 return &prev->formats[pad];
1700 static struct v4l2_rect *
1701 __preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1702 enum v4l2_subdev_format_whence which)
1704 if (which == V4L2_SUBDEV_FORMAT_TRY)
1705 return v4l2_subdev_get_try_crop(fh, PREV_PAD_SINK);
1710 /* previewer format descriptions */
1711 static const unsigned int preview_input_fmts[] = {
1712 V4L2_MBUS_FMT_Y8_1X8,
1713 V4L2_MBUS_FMT_SGRBG8_1X8,
1714 V4L2_MBUS_FMT_SRGGB8_1X8,
1715 V4L2_MBUS_FMT_SBGGR8_1X8,
1716 V4L2_MBUS_FMT_SGBRG8_1X8,
1717 V4L2_MBUS_FMT_Y10_1X10,
1718 V4L2_MBUS_FMT_SGRBG10_1X10,
1719 V4L2_MBUS_FMT_SRGGB10_1X10,
1720 V4L2_MBUS_FMT_SBGGR10_1X10,
1721 V4L2_MBUS_FMT_SGBRG10_1X10,
1724 static const unsigned int preview_output_fmts[] = {
1725 V4L2_MBUS_FMT_UYVY8_1X16,
1726 V4L2_MBUS_FMT_YUYV8_1X16,
1730 * preview_try_format - Validate a format
1731 * @prev: ISP preview engine
1732 * @fh: V4L2 subdev file handle
1734 * @fmt: format to be validated
1735 * @which: try/active format selector
1737 * Validate and adjust the given format for the given pad based on the preview
1738 * engine limits and the format and crop rectangles on other pads.
1740 static void preview_try_format(struct isp_prev_device *prev,
1741 struct v4l2_subdev_fh *fh, unsigned int pad,
1742 struct v4l2_mbus_framefmt *fmt,
1743 enum v4l2_subdev_format_whence which)
1745 enum v4l2_mbus_pixelcode pixelcode;
1746 struct v4l2_rect *crop;
1751 /* When reading data from the CCDC, the input size has already
1752 * been mangled by the CCDC output pad so it can be accepted
1755 * When reading data from memory, clamp the requested width and
1756 * height. The TRM doesn't specify a minimum input height, make
1757 * sure we got enough lines to enable the noise filter and color
1758 * filter array interpolation.
1760 if (prev->input == PREVIEW_INPUT_MEMORY) {
1761 fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
1762 preview_max_out_width(prev));
1763 fmt->height = clamp_t(u32, fmt->height,
1765 PREV_MAX_IN_HEIGHT);
1768 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1770 for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
1771 if (fmt->code == preview_input_fmts[i])
1775 /* If not found, use SGRBG10 as default */
1776 if (i >= ARRAY_SIZE(preview_input_fmts))
1777 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1780 case PREV_PAD_SOURCE:
1781 pixelcode = fmt->code;
1782 *fmt = *__preview_get_format(prev, fh, PREV_PAD_SINK, which);
1784 switch (pixelcode) {
1785 case V4L2_MBUS_FMT_YUYV8_1X16:
1786 case V4L2_MBUS_FMT_UYVY8_1X16:
1787 fmt->code = pixelcode;
1791 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1795 /* The preview module output size is configurable through the
1796 * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
1797 * is not supported yet, hardcode the output size to the crop
1800 crop = __preview_get_crop(prev, fh, which);
1801 fmt->width = crop->width;
1802 fmt->height = crop->height;
1804 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1808 fmt->field = V4L2_FIELD_NONE;
1812 * preview_try_crop - Validate a crop rectangle
1813 * @prev: ISP preview engine
1814 * @sink: format on the sink pad
1815 * @crop: crop rectangle to be validated
1817 * The preview engine crops lines and columns for its internal operation,
1818 * depending on which filters are enabled. Enforce minimum crop margins to
1819 * handle that transparently for userspace.
1821 * See the explanation at the PREV_MARGIN_* definitions for more details.
1823 static void preview_try_crop(struct isp_prev_device *prev,
1824 const struct v4l2_mbus_framefmt *sink,
1825 struct v4l2_rect *crop)
1827 unsigned int left = PREV_MARGIN_LEFT;
1828 unsigned int right = sink->width - PREV_MARGIN_RIGHT;
1829 unsigned int top = PREV_MARGIN_TOP;
1830 unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
1832 /* When processing data on-the-fly from the CCDC, at least 2 pixels must
1833 * be cropped from the left and right sides of the image. As we don't
1834 * know which filters will be enabled, increase the left and right
1837 if (prev->input == PREVIEW_INPUT_CCDC) {
1842 /* The CFA filter crops 4 lines and 4 columns in Bayer mode, and 2 lines
1843 * and no columns in other modes. Increase the margins based on the sink
1846 if (sink->code != V4L2_MBUS_FMT_Y8_1X8 &&
1847 sink->code != V4L2_MBUS_FMT_Y10_1X10) {
1854 /* Restrict left/top to even values to keep the Bayer pattern. */
1858 crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
1859 crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
1860 crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
1861 right - crop->left);
1862 crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
1863 bottom - crop->top);
1867 * preview_enum_mbus_code - Handle pixel format enumeration
1868 * @sd : pointer to v4l2 subdev structure
1869 * @fh : V4L2 subdev file handle
1870 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1871 * return -EINVAL or zero on success
1873 static int preview_enum_mbus_code(struct v4l2_subdev *sd,
1874 struct v4l2_subdev_fh *fh,
1875 struct v4l2_subdev_mbus_code_enum *code)
1877 switch (code->pad) {
1879 if (code->index >= ARRAY_SIZE(preview_input_fmts))
1882 code->code = preview_input_fmts[code->index];
1884 case PREV_PAD_SOURCE:
1885 if (code->index >= ARRAY_SIZE(preview_output_fmts))
1888 code->code = preview_output_fmts[code->index];
1897 static int preview_enum_frame_size(struct v4l2_subdev *sd,
1898 struct v4l2_subdev_fh *fh,
1899 struct v4l2_subdev_frame_size_enum *fse)
1901 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1902 struct v4l2_mbus_framefmt format;
1904 if (fse->index != 0)
1907 format.code = fse->code;
1910 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1911 fse->min_width = format.width;
1912 fse->min_height = format.height;
1914 if (format.code != fse->code)
1917 format.code = fse->code;
1920 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1921 fse->max_width = format.width;
1922 fse->max_height = format.height;
1928 * preview_get_selection - Retrieve a selection rectangle on a pad
1929 * @sd: ISP preview V4L2 subdevice
1930 * @fh: V4L2 subdev file handle
1931 * @sel: Selection rectangle
1933 * The only supported rectangles are the crop rectangles on the sink pad.
1935 * Return 0 on success or a negative error code otherwise.
1937 static int preview_get_selection(struct v4l2_subdev *sd,
1938 struct v4l2_subdev_fh *fh,
1939 struct v4l2_subdev_selection *sel)
1941 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1942 struct v4l2_mbus_framefmt *format;
1944 if (sel->pad != PREV_PAD_SINK)
1947 switch (sel->target) {
1948 case V4L2_SEL_TGT_CROP_BOUNDS:
1951 sel->r.width = INT_MAX;
1952 sel->r.height = INT_MAX;
1954 format = __preview_get_format(prev, fh, PREV_PAD_SINK,
1956 preview_try_crop(prev, format, &sel->r);
1959 case V4L2_SEL_TGT_CROP:
1960 sel->r = *__preview_get_crop(prev, fh, sel->which);
1971 * preview_set_selection - Set a selection rectangle on a pad
1972 * @sd: ISP preview V4L2 subdevice
1973 * @fh: V4L2 subdev file handle
1974 * @sel: Selection rectangle
1976 * The only supported rectangle is the actual crop rectangle on the sink pad.
1978 * Return 0 on success or a negative error code otherwise.
1980 static int preview_set_selection(struct v4l2_subdev *sd,
1981 struct v4l2_subdev_fh *fh,
1982 struct v4l2_subdev_selection *sel)
1984 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1985 struct v4l2_mbus_framefmt *format;
1987 if (sel->target != V4L2_SEL_TGT_CROP ||
1988 sel->pad != PREV_PAD_SINK)
1991 /* The crop rectangle can't be changed while streaming. */
1992 if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
1995 /* Modifying the crop rectangle always changes the format on the source
1996 * pad. If the KEEP_CONFIG flag is set, just return the current crop
1999 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
2000 sel->r = *__preview_get_crop(prev, fh, sel->which);
2004 format = __preview_get_format(prev, fh, PREV_PAD_SINK, sel->which);
2005 preview_try_crop(prev, format, &sel->r);
2006 *__preview_get_crop(prev, fh, sel->which) = sel->r;
2008 /* Update the source format. */
2009 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE, sel->which);
2010 preview_try_format(prev, fh, PREV_PAD_SOURCE, format, sel->which);
2016 * preview_get_format - Handle get format by pads subdev method
2017 * @sd : pointer to v4l2 subdev structure
2018 * @fh : V4L2 subdev file handle
2019 * @fmt: pointer to v4l2 subdev format structure
2020 * return -EINVAL or zero on success
2022 static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2023 struct v4l2_subdev_format *fmt)
2025 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2026 struct v4l2_mbus_framefmt *format;
2028 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2032 fmt->format = *format;
2037 * preview_set_format - Handle set format by pads subdev method
2038 * @sd : pointer to v4l2 subdev structure
2039 * @fh : V4L2 subdev file handle
2040 * @fmt: pointer to v4l2 subdev format structure
2041 * return -EINVAL or zero on success
2043 static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2044 struct v4l2_subdev_format *fmt)
2046 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2047 struct v4l2_mbus_framefmt *format;
2048 struct v4l2_rect *crop;
2050 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2054 preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
2055 *format = fmt->format;
2057 /* Propagate the format from sink to source */
2058 if (fmt->pad == PREV_PAD_SINK) {
2059 /* Reset the crop rectangle. */
2060 crop = __preview_get_crop(prev, fh, fmt->which);
2063 crop->width = fmt->format.width;
2064 crop->height = fmt->format.height;
2066 preview_try_crop(prev, &fmt->format, crop);
2068 /* Update the source format. */
2069 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
2071 preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
2079 * preview_init_formats - Initialize formats on all pads
2080 * @sd: ISP preview V4L2 subdevice
2081 * @fh: V4L2 subdev file handle
2083 * Initialize all pad formats with default values. If fh is not NULL, try
2084 * formats are initialized on the file handle. Otherwise active formats are
2085 * initialized on the device.
2087 static int preview_init_formats(struct v4l2_subdev *sd,
2088 struct v4l2_subdev_fh *fh)
2090 struct v4l2_subdev_format format;
2092 memset(&format, 0, sizeof(format));
2093 format.pad = PREV_PAD_SINK;
2094 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2095 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2096 format.format.width = 4096;
2097 format.format.height = 4096;
2098 preview_set_format(sd, fh, &format);
2103 /* subdev core operations */
2104 static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
2105 .ioctl = preview_ioctl,
2108 /* subdev video operations */
2109 static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
2110 .s_stream = preview_set_stream,
2113 /* subdev pad operations */
2114 static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
2115 .enum_mbus_code = preview_enum_mbus_code,
2116 .enum_frame_size = preview_enum_frame_size,
2117 .get_fmt = preview_get_format,
2118 .set_fmt = preview_set_format,
2119 .get_selection = preview_get_selection,
2120 .set_selection = preview_set_selection,
2123 /* subdev operations */
2124 static const struct v4l2_subdev_ops preview_v4l2_ops = {
2125 .core = &preview_v4l2_core_ops,
2126 .video = &preview_v4l2_video_ops,
2127 .pad = &preview_v4l2_pad_ops,
2130 /* subdev internal operations */
2131 static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
2132 .open = preview_init_formats,
2135 /* -----------------------------------------------------------------------------
2136 * Media entity operations
2140 * preview_link_setup - Setup previewer connections.
2141 * @entity : Pointer to media entity structure
2142 * @local : Pointer to local pad array
2143 * @remote : Pointer to remote pad array
2144 * @flags : Link flags
2145 * return -EINVAL or zero on success
2147 static int preview_link_setup(struct media_entity *entity,
2148 const struct media_pad *local,
2149 const struct media_pad *remote, u32 flags)
2151 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2152 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2154 switch (local->index | media_entity_type(remote->entity)) {
2155 case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
2156 /* read from memory */
2157 if (flags & MEDIA_LNK_FL_ENABLED) {
2158 if (prev->input == PREVIEW_INPUT_CCDC)
2160 prev->input = PREVIEW_INPUT_MEMORY;
2162 if (prev->input == PREVIEW_INPUT_MEMORY)
2163 prev->input = PREVIEW_INPUT_NONE;
2167 case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2168 /* read from ccdc */
2169 if (flags & MEDIA_LNK_FL_ENABLED) {
2170 if (prev->input == PREVIEW_INPUT_MEMORY)
2172 prev->input = PREVIEW_INPUT_CCDC;
2174 if (prev->input == PREVIEW_INPUT_CCDC)
2175 prev->input = PREVIEW_INPUT_NONE;
2180 * The ISP core doesn't support pipelines with multiple video outputs.
2181 * Revisit this when it will be implemented, and return -EBUSY for now.
2184 case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
2185 /* write to memory */
2186 if (flags & MEDIA_LNK_FL_ENABLED) {
2187 if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
2189 prev->output |= PREVIEW_OUTPUT_MEMORY;
2191 prev->output &= ~PREVIEW_OUTPUT_MEMORY;
2195 case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
2196 /* write to resizer */
2197 if (flags & MEDIA_LNK_FL_ENABLED) {
2198 if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
2200 prev->output |= PREVIEW_OUTPUT_RESIZER;
2202 prev->output &= ~PREVIEW_OUTPUT_RESIZER;
2213 /* media operations */
2214 static const struct media_entity_operations preview_media_ops = {
2215 .link_setup = preview_link_setup,
2216 .link_validate = v4l2_subdev_link_validate,
2219 void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
2221 v4l2_device_unregister_subdev(&prev->subdev);
2222 omap3isp_video_unregister(&prev->video_in);
2223 omap3isp_video_unregister(&prev->video_out);
2226 int omap3isp_preview_register_entities(struct isp_prev_device *prev,
2227 struct v4l2_device *vdev)
2231 /* Register the subdev and video nodes. */
2232 ret = v4l2_device_register_subdev(vdev, &prev->subdev);
2236 ret = omap3isp_video_register(&prev->video_in, vdev);
2240 ret = omap3isp_video_register(&prev->video_out, vdev);
2247 omap3isp_preview_unregister_entities(prev);
2251 /* -----------------------------------------------------------------------------
2252 * ISP previewer initialisation and cleanup
2256 * preview_init_entities - Initialize subdev and media entity.
2257 * @prev : Pointer to preview structure
2258 * return -ENOMEM or zero on success
2260 static int preview_init_entities(struct isp_prev_device *prev)
2262 struct v4l2_subdev *sd = &prev->subdev;
2263 struct media_pad *pads = prev->pads;
2264 struct media_entity *me = &sd->entity;
2267 prev->input = PREVIEW_INPUT_NONE;
2269 v4l2_subdev_init(sd, &preview_v4l2_ops);
2270 sd->internal_ops = &preview_v4l2_internal_ops;
2271 strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
2272 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2273 v4l2_set_subdevdata(sd, prev);
2274 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2276 v4l2_ctrl_handler_init(&prev->ctrls, 2);
2277 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
2278 ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
2279 ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
2280 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
2281 ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
2282 ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
2283 v4l2_ctrl_handler_setup(&prev->ctrls);
2284 sd->ctrl_handler = &prev->ctrls;
2286 pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK
2287 | MEDIA_PAD_FL_MUST_CONNECT;
2288 pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2290 me->ops = &preview_media_ops;
2291 ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
2295 preview_init_formats(sd, NULL);
2297 /* According to the OMAP34xx TRM, video buffers need to be aligned on a
2298 * 32 bytes boundary. However, an undocumented hardware bug requires a
2299 * 64 bytes boundary at the preview engine input.
2301 prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2302 prev->video_in.ops = &preview_video_ops;
2303 prev->video_in.isp = to_isp_device(prev);
2304 prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2305 prev->video_in.bpl_alignment = 64;
2306 prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2307 prev->video_out.ops = &preview_video_ops;
2308 prev->video_out.isp = to_isp_device(prev);
2309 prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2310 prev->video_out.bpl_alignment = 32;
2312 ret = omap3isp_video_init(&prev->video_in, "preview");
2314 goto error_video_in;
2316 ret = omap3isp_video_init(&prev->video_out, "preview");
2318 goto error_video_out;
2320 /* Connect the video nodes to the previewer subdev. */
2321 ret = media_entity_create_link(&prev->video_in.video.entity, 0,
2322 &prev->subdev.entity, PREV_PAD_SINK, 0);
2326 ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
2327 &prev->video_out.video.entity, 0, 0);
2334 omap3isp_video_cleanup(&prev->video_out);
2336 omap3isp_video_cleanup(&prev->video_in);
2338 media_entity_cleanup(&prev->subdev.entity);
2343 * omap3isp_preview_init - Previewer initialization.
2344 * @isp : Pointer to ISP device
2345 * return -ENOMEM or zero on success
2347 int omap3isp_preview_init(struct isp_device *isp)
2349 struct isp_prev_device *prev = &isp->isp_prev;
2351 init_waitqueue_head(&prev->wait);
2353 preview_init_params(prev);
2355 return preview_init_entities(prev);
2358 void omap3isp_preview_cleanup(struct isp_device *isp)
2360 struct isp_prev_device *prev = &isp->isp_prev;
2362 v4l2_ctrl_handler_free(&prev->ctrls);
2363 omap3isp_video_cleanup(&prev->video_in);
2364 omap3isp_video_cleanup(&prev->video_out);
2365 media_entity_cleanup(&prev->subdev.entity);