blob: 691b92a3c3e7c18bc8b95f1d57e99c6fc9606043 [file] [log] [blame]
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001/*
2 * isppreview.c
3 *
4 * TI OMAP3 ISP driver - Preview module
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc.
8 *
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * 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.
20 *
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
24 * 02110-1301 USA
25 */
26
27#include <linux/device.h>
28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/mutex.h>
31#include <linux/uaccess.h>
32
33#include "isp.h"
34#include "ispreg.h"
35#include "isppreview.h"
36
Lucas De Marchi25985ed2011-03-30 22:57:33 -030037/* Default values in Office Fluorescent Light for RGBtoRGB Blending */
Laurent Pinchartde1135d2011-02-12 18:05:06 -030038static struct omap3isp_prev_rgbtorgb flr_rgb2rgb = {
39 { /* RGB-RGB Matrix */
40 {0x01E2, 0x0F30, 0x0FEE},
41 {0x0F9B, 0x01AC, 0x0FB9},
42 {0x0FE0, 0x0EC0, 0x0260}
43 }, /* RGB Offset */
44 {0x0000, 0x0000, 0x0000}
45};
46
Lucas De Marchi25985ed2011-03-30 22:57:33 -030047/* Default values in Office Fluorescent Light for RGB to YUV Conversion*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030048static struct omap3isp_prev_csc flr_prev_csc = {
49 { /* CSC Coef Matrix */
50 {66, 129, 25},
51 {-38, -75, 112},
52 {112, -94 , -18}
53 }, /* CSC Offset */
54 {0x0, 0x0, 0x0}
55};
56
Lucas De Marchi25985ed2011-03-30 22:57:33 -030057/* Default values in Office Fluorescent Light for CFA Gradient*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030058#define FLR_CFA_GRADTHRS_HORZ 0x28
59#define FLR_CFA_GRADTHRS_VERT 0x28
60
Lucas De Marchi25985ed2011-03-30 22:57:33 -030061/* Default values in Office Fluorescent Light for Chroma Suppression*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030062#define FLR_CSUP_GAIN 0x0D
63#define FLR_CSUP_THRES 0xEB
64
Lucas De Marchi25985ed2011-03-30 22:57:33 -030065/* Default values in Office Fluorescent Light for Noise Filter*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030066#define FLR_NF_STRGTH 0x03
67
68/* Default values for White Balance */
69#define FLR_WBAL_DGAIN 0x100
70#define FLR_WBAL_COEF 0x20
71
Lucas De Marchi25985ed2011-03-30 22:57:33 -030072/* Default values in Office Fluorescent Light for Black Adjustment*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030073#define FLR_BLKADJ_BLUE 0x0
74#define FLR_BLKADJ_GREEN 0x0
75#define FLR_BLKADJ_RED 0x0
76
77#define DEF_DETECT_CORRECT_VAL 0xe
78
Laurent Pinchart1f69fd92011-09-21 20:05:45 -030079/*
80 * Margins and image size limits.
81 *
82 * The preview engine crops several rows and columns internally depending on
83 * which filters are enabled. To avoid format changes when the filters are
84 * enabled or disabled (which would prevent them from being turned on or off
85 * during streaming), the driver assumes all the filters are enabled when
86 * computing sink crop and source format limits.
87 *
88 * If a filter is disabled, additional cropping is automatically added at the
89 * preview engine input by the driver to avoid overflow at line and frame end.
90 * This is completely transparent for applications.
91 *
92 * Median filter 4 pixels
93 * Noise filter,
94 * Faulty pixels correction 4 pixels, 4 lines
95 * CFA filter 4 pixels, 4 lines in Bayer mode
96 * 2 lines in other modes
97 * Color suppression 2 pixels
98 * or luma enhancement
99 * -------------------------------------------------------------
100 * Maximum total 14 pixels, 8 lines
101 *
102 * The color suppression and luma enhancement filters are applied after bayer to
103 * YUV conversion. They thus can crop one pixel on the left and one pixel on the
104 * right side of the image without changing the color pattern. When both those
105 * filters are disabled, the driver must crop the two pixels on the same side of
106 * the image to avoid changing the bayer pattern. The left margin is thus set to
107 * 8 pixels and the right margin to 6 pixels.
108 */
109
110#define PREV_MARGIN_LEFT 8
111#define PREV_MARGIN_RIGHT 6
112#define PREV_MARGIN_TOP 4
113#define PREV_MARGIN_BOTTOM 4
114
Laurent Pinchart059dc1d2011-10-03 07:56:15 -0300115#define PREV_MIN_IN_WIDTH 64
116#define PREV_MIN_IN_HEIGHT 8
117#define PREV_MAX_IN_HEIGHT 16384
118
Laurent Pinchartec0cae72011-10-03 07:56:15 -0300119#define PREV_MIN_OUT_WIDTH 0
120#define PREV_MIN_OUT_HEIGHT 0
121#define PREV_MAX_OUT_WIDTH_REV_1 1280
122#define PREV_MAX_OUT_WIDTH_REV_2 3300
123#define PREV_MAX_OUT_WIDTH_REV_15 4096
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300124
125/*
126 * Coeficient Tables for the submodules in Preview.
127 * Array is initialised with the values from.the tables text file.
128 */
129
130/*
131 * CFA Filter Coefficient Table
132 *
133 */
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300134static u32 cfa_coef_table[4][OMAP3ISP_PREV_CFA_BLK_SIZE] = {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300135#include "cfa_coef_table.h"
136};
137
138/*
139 * Default Gamma Correction Table - All components
140 */
141static u32 gamma_table[] = {
142#include "gamma_table.h"
143};
144
145/*
146 * Noise Filter Threshold table
147 */
148static u32 noise_filter_table[] = {
149#include "noise_filter_table.h"
150};
151
152/*
153 * Luminance Enhancement Table
154 */
155static u32 luma_enhance_table[] = {
156#include "luma_enhance_table.h"
157};
158
159/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300160 * preview_config_luma_enhancement - Configure the Luminance Enhancement table
161 */
162static void
163preview_config_luma_enhancement(struct isp_prev_device *prev,
164 const struct prev_params *params)
165{
166 struct isp_device *isp = to_isp_device(prev);
167 const struct omap3isp_prev_luma *yt = &params->luma;
168 unsigned int i;
169
170 isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
171 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
172 for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
173 isp_reg_writel(isp, yt->table[i],
174 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
175 }
176}
177
178/*
179 * preview_enable_luma_enhancement - Enable/disable Luminance Enhancement
180 */
181static void
182preview_enable_luma_enhancement(struct isp_prev_device *prev, bool enable)
183{
184 struct isp_device *isp = to_isp_device(prev);
185
186 if (enable)
187 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
188 ISPPRV_PCR_YNENHEN);
189 else
190 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
191 ISPPRV_PCR_YNENHEN);
192}
193
194/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300195 * preview_enable_invalaw - Enable/disable Inverse A-Law decompression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300196 */
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300197static void preview_enable_invalaw(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300198{
199 struct isp_device *isp = to_isp_device(prev);
200
201 if (enable)
202 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
Laurent Pincharte023a112012-10-23 08:11:55 -0300203 ISPPRV_PCR_INVALAW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300204 else
205 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
Laurent Pincharte023a112012-10-23 08:11:55 -0300206 ISPPRV_PCR_INVALAW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300207}
208
209/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300210 * preview_config_hmed - Configure the Horizontal Median Filter
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300211 */
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300212static void preview_config_hmed(struct isp_prev_device *prev,
213 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300214{
215 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300216 const struct omap3isp_prev_hmed *hmed = &params->hmed;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300217
218 isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
219 (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
220 (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
221 OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
222}
223
224/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300225 * preview_enable_hmed - Enable/disable the Horizontal Median Filter
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300226 */
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300227static void preview_enable_hmed(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300228{
229 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300230
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300231 if (enable)
232 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
233 ISPPRV_PCR_HMEDEN);
234 else
235 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
236 ISPPRV_PCR_HMEDEN);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300237}
238
239/*
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300240 * preview_config_cfa - Configure CFA Interpolation for Bayer formats
241 *
242 * The CFA table is organised in four blocks, one per Bayer component. The
243 * hardware expects blocks to follow the Bayer order of the input data, while
244 * the driver stores the table in GRBG order in memory. The blocks need to be
245 * reordered to support non-GRBG Bayer patterns.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300246 */
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300247static void preview_config_cfa(struct isp_prev_device *prev,
248 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300249{
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300250 static const unsigned int cfa_coef_order[4][4] = {
251 { 0, 1, 2, 3 }, /* GRBG */
252 { 1, 0, 3, 2 }, /* RGGB */
253 { 2, 3, 0, 1 }, /* BGGR */
254 { 3, 2, 1, 0 }, /* GBRG */
255 };
256 const unsigned int *order = cfa_coef_order[prev->params.cfa_order];
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300257 const struct omap3isp_prev_cfa *cfa = &params->cfa;
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300258 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300259 unsigned int i;
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300260 unsigned int j;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300261
262 isp_reg_writel(isp,
263 (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
264 (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
265 OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
266
267 isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
268 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
269
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300270 for (i = 0; i < 4; ++i) {
271 const __u32 *block = cfa->table[order[i]];
272
273 for (j = 0; j < OMAP3ISP_PREV_CFA_BLK_SIZE; ++j)
274 isp_reg_writel(isp, block[j], OMAP3_ISP_IOMEM_PREV,
275 ISPPRV_SET_TBL_DATA);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300276 }
277}
278
279/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300280 * preview_config_chroma_suppression - Configure Chroma Suppression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300281 */
282static void
283preview_config_chroma_suppression(struct isp_prev_device *prev,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300284 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300285{
286 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300287 const struct omap3isp_prev_csup *cs = &params->csup;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300288
289 isp_reg_writel(isp,
290 cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
291 (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
292 OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
293}
294
295/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300296 * preview_enable_chroma_suppression - Enable/disable Chrominance Suppression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300297 */
298static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300299preview_enable_chroma_suppression(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300300{
301 struct isp_device *isp = to_isp_device(prev);
302
303 if (enable)
304 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
305 ISPPRV_PCR_SUPEN);
306 else
307 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
308 ISPPRV_PCR_SUPEN);
309}
310
311/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300312 * preview_config_whitebalance - Configure White Balance parameters
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300313 *
314 * Coefficient matrix always with default values.
315 */
316static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300317preview_config_whitebalance(struct isp_prev_device *prev,
318 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300319{
320 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300321 const struct omap3isp_prev_wbal *wbal = &params->wbal;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300322 u32 val;
323
324 isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
325
326 val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
327 val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
328 val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
329 val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
330 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
331
332 isp_reg_writel(isp,
333 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
334 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
335 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
336 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
337 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
338 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
339 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
340 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
341 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
342 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
343 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
344 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
345 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
346 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
347 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
348 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
349 OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
350}
351
352/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300353 * preview_config_blkadj - Configure Black Adjustment
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300354 */
355static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300356preview_config_blkadj(struct isp_prev_device *prev,
357 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300358{
359 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300360 const struct omap3isp_prev_blkadj *blkadj = &params->blkadj;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300361
362 isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
363 (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
364 (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
365 OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
366}
367
368/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300369 * preview_config_rgb_blending - Configure RGB-RGB Blending
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300370 */
371static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300372preview_config_rgb_blending(struct isp_prev_device *prev,
373 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300374{
375 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300376 const struct omap3isp_prev_rgbtorgb *rgbrgb = &params->rgb2rgb;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300377 u32 val;
378
379 val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
380 val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
381 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
382
383 val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
384 val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
385 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
386
387 val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
388 val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
389 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
390
391 val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
392 val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
393 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
394
395 val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
396 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
397
398 val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
399 val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
400 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
401
402 val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
403 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
404}
405
406/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300407 * preview_config_csc - Configure Color Space Conversion (RGB to YCbYCr)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300408 */
409static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300410preview_config_csc(struct isp_prev_device *prev,
411 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300412{
413 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300414 const struct omap3isp_prev_csc *csc = &params->csc;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300415 u32 val;
416
417 val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
418 val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
419 val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
420 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
421
422 val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
423 val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
424 val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
425 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
426
427 val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
428 val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
429 val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
430 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
431
432 val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
433 val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
434 val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
435 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
436}
437
438/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300439 * preview_config_yc_range - Configure the max and min Y and C values
440 */
441static void
442preview_config_yc_range(struct isp_prev_device *prev,
443 const struct prev_params *params)
444{
445 struct isp_device *isp = to_isp_device(prev);
446 const struct omap3isp_prev_yclimit *yc = &params->yclimit;
447
448 isp_reg_writel(isp,
449 yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
450 yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
451 yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
452 yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
453 OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
454}
455
456/*
457 * preview_config_dcor - Configure Couplet Defect Correction
458 */
459static void
460preview_config_dcor(struct isp_prev_device *prev,
461 const struct prev_params *params)
462{
463 struct isp_device *isp = to_isp_device(prev);
464 const struct omap3isp_prev_dcor *dcor = &params->dcor;
465
466 isp_reg_writel(isp, dcor->detect_correct[0],
467 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
468 isp_reg_writel(isp, dcor->detect_correct[1],
469 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
470 isp_reg_writel(isp, dcor->detect_correct[2],
471 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
472 isp_reg_writel(isp, dcor->detect_correct[3],
473 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
474 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
475 ISPPRV_PCR_DCCOUP,
476 dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
477}
478
479/*
480 * preview_enable_dcor - Enable/disable Couplet Defect Correction
481 */
482static void preview_enable_dcor(struct isp_prev_device *prev, bool enable)
483{
484 struct isp_device *isp = to_isp_device(prev);
485
486 if (enable)
487 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
488 ISPPRV_PCR_DCOREN);
489 else
490 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
491 ISPPRV_PCR_DCOREN);
492}
493
494/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300495 * preview_enable_drkframe_capture - Enable/disable Dark Frame Capture
496 */
497static void
498preview_enable_drkframe_capture(struct isp_prev_device *prev, bool enable)
499{
500 struct isp_device *isp = to_isp_device(prev);
501
502 if (enable)
503 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
504 ISPPRV_PCR_DRKFCAP);
505 else
506 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
507 ISPPRV_PCR_DRKFCAP);
508}
509
510/*
511 * preview_enable_drkframe - Enable/disable Dark Frame Subtraction
512 */
513static void preview_enable_drkframe(struct isp_prev_device *prev, bool enable)
514{
515 struct isp_device *isp = to_isp_device(prev);
516
517 if (enable)
518 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
519 ISPPRV_PCR_DRKFEN);
520 else
521 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
522 ISPPRV_PCR_DRKFEN);
523}
524
525/*
526 * preview_config_noisefilter - Configure the Noise Filter
527 */
528static void
529preview_config_noisefilter(struct isp_prev_device *prev,
530 const struct prev_params *params)
531{
532 struct isp_device *isp = to_isp_device(prev);
533 const struct omap3isp_prev_nf *nf = &params->nf;
534 unsigned int i;
535
536 isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
537 isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
538 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
539 for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
540 isp_reg_writel(isp, nf->table[i],
541 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
542 }
543}
544
545/*
546 * preview_enable_noisefilter - Enable/disable the Noise Filter
547 */
548static void
549preview_enable_noisefilter(struct isp_prev_device *prev, bool enable)
550{
551 struct isp_device *isp = to_isp_device(prev);
552
553 if (enable)
554 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
555 ISPPRV_PCR_NFEN);
556 else
557 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
558 ISPPRV_PCR_NFEN);
559}
560
561/*
562 * preview_config_gammacorrn - Configure the Gamma Correction tables
563 */
564static void
565preview_config_gammacorrn(struct isp_prev_device *prev,
566 const struct prev_params *params)
567{
568 struct isp_device *isp = to_isp_device(prev);
569 const struct omap3isp_prev_gtables *gt = &params->gamma;
570 unsigned int i;
571
572 isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
573 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
574 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
575 isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
576 ISPPRV_SET_TBL_DATA);
577
578 isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
579 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
580 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
581 isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
582 ISPPRV_SET_TBL_DATA);
583
584 isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
585 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
586 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
587 isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
588 ISPPRV_SET_TBL_DATA);
589}
590
591/*
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300592 * preview_enable_gammacorrn - Enable/disable Gamma Correction
593 *
594 * When gamma correction is disabled, the module is bypassed and its output is
595 * the 8 MSB of the 10-bit input .
596 */
597static void
598preview_enable_gammacorrn(struct isp_prev_device *prev, bool enable)
599{
600 struct isp_device *isp = to_isp_device(prev);
601
602 if (enable)
603 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
604 ISPPRV_PCR_GAMMA_BYPASS);
605 else
606 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
607 ISPPRV_PCR_GAMMA_BYPASS);
608}
609
610/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300611 * preview_config_contrast - Configure the Contrast
612 *
613 * Value should be programmed before enabling the module.
614 */
615static void
616preview_config_contrast(struct isp_prev_device *prev,
617 const struct prev_params *params)
618{
619 struct isp_device *isp = to_isp_device(prev);
620
621 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
622 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
623 params->contrast << ISPPRV_CNT_BRT_CNT_SHIFT);
624}
625
626/*
627 * preview_config_brightness - Configure the Brightness
628 */
629static void
630preview_config_brightness(struct isp_prev_device *prev,
631 const struct prev_params *params)
632{
633 struct isp_device *isp = to_isp_device(prev);
634
635 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
636 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
637 params->brightness << ISPPRV_CNT_BRT_BRT_SHIFT);
638}
639
640/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300641 * preview_update_contrast - Updates the contrast.
642 * @contrast: Pointer to hold the current programmed contrast value.
643 *
644 * Value should be programmed before enabling the module.
645 */
646static void
647preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
648{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300649 struct prev_params *params;
650 unsigned long flags;
651
652 spin_lock_irqsave(&prev->params.lock, flags);
653 params = (prev->params.active & OMAP3ISP_PREV_CONTRAST)
654 ? &prev->params.params[0] : &prev->params.params[1];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300655
656 if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
657 params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300658 params->update |= OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300659 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300660 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300661}
662
663/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300664 * preview_update_brightness - Updates the brightness in preview module.
665 * @brightness: Pointer to hold the current programmed brightness value.
666 *
667 */
668static void
669preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
670{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300671 struct prev_params *params;
672 unsigned long flags;
673
674 spin_lock_irqsave(&prev->params.lock, flags);
675 params = (prev->params.active & OMAP3ISP_PREV_BRIGHTNESS)
676 ? &prev->params.params[0] : &prev->params.params[1];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300677
678 if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
679 params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300680 params->update |= OMAP3ISP_PREV_BRIGHTNESS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300681 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300682 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300683}
684
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300685static u32
686preview_params_lock(struct isp_prev_device *prev, u32 update, bool shadow)
687{
688 u32 active = prev->params.active;
689
690 if (shadow) {
691 /* Mark all shadow parameters we are going to touch as busy. */
692 prev->params.params[0].busy |= ~active & update;
693 prev->params.params[1].busy |= active & update;
694 } else {
695 /* Mark all active parameters we are going to touch as busy. */
696 update = (prev->params.params[0].update & active)
697 | (prev->params.params[1].update & ~active);
698
699 prev->params.params[0].busy |= active & update;
700 prev->params.params[1].busy |= ~active & update;
701 }
702
703 return update;
704}
705
706static void
707preview_params_unlock(struct isp_prev_device *prev, u32 update, bool shadow)
708{
709 u32 active = prev->params.active;
710
711 if (shadow) {
712 /* Set the update flag for shadow parameters that have been
713 * updated and clear the busy flag for all shadow parameters.
714 */
715 prev->params.params[0].update |= (~active & update);
716 prev->params.params[1].update |= (active & update);
717 prev->params.params[0].busy &= active;
718 prev->params.params[1].busy &= ~active;
719 } else {
720 /* Clear the update flag for active parameters that have been
721 * applied and the busy flag for all active parameters.
722 */
723 prev->params.params[0].update &= ~(active & update);
724 prev->params.params[1].update &= ~(~active & update);
725 prev->params.params[0].busy &= ~active;
726 prev->params.params[1].busy &= active;
727 }
728}
729
730static void preview_params_switch(struct isp_prev_device *prev)
731{
732 u32 to_switch;
733
734 /* Switch active parameters with updated shadow parameters when the
735 * shadow parameter has been updated and neither the active not the
736 * shadow parameter is busy.
737 */
738 to_switch = (prev->params.params[0].update & ~prev->params.active)
739 | (prev->params.params[1].update & prev->params.active);
740 to_switch &= ~(prev->params.params[0].busy |
741 prev->params.params[1].busy);
742 if (to_switch == 0)
743 return;
744
745 prev->params.active ^= to_switch;
746
747 /* Remove the update flag for the shadow copy of parameters we have
748 * switched.
749 */
750 prev->params.params[0].update &= ~(~prev->params.active & to_switch);
751 prev->params.params[1].update &= ~(prev->params.active & to_switch);
752}
753
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300754/* preview parameters update structure */
755struct preview_update {
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300756 void (*config)(struct isp_prev_device *, const struct prev_params *);
757 void (*enable)(struct isp_prev_device *, bool);
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300758 unsigned int param_offset;
759 unsigned int param_size;
760 unsigned int config_offset;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300761 bool skip;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300762};
763
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300764/* Keep the array indexed by the OMAP3ISP_PREV_* bit number. */
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300765static const struct preview_update update_attrs[] = {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300766 /* OMAP3ISP_PREV_LUMAENH */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300767 preview_config_luma_enhancement,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300768 preview_enable_luma_enhancement,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300769 offsetof(struct prev_params, luma),
770 FIELD_SIZEOF(struct prev_params, luma),
771 offsetof(struct omap3isp_prev_update_config, luma),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300772 }, /* OMAP3ISP_PREV_INVALAW */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300773 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300774 preview_enable_invalaw,
775 }, /* OMAP3ISP_PREV_HRZ_MED */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300776 preview_config_hmed,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300777 preview_enable_hmed,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300778 offsetof(struct prev_params, hmed),
779 FIELD_SIZEOF(struct prev_params, hmed),
780 offsetof(struct omap3isp_prev_update_config, hmed),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300781 }, /* OMAP3ISP_PREV_CFA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300782 preview_config_cfa,
Laurent Pinchartb2da46e2012-04-19 13:38:29 -0300783 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300784 offsetof(struct prev_params, cfa),
785 FIELD_SIZEOF(struct prev_params, cfa),
786 offsetof(struct omap3isp_prev_update_config, cfa),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300787 }, /* OMAP3ISP_PREV_CHROMA_SUPP */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300788 preview_config_chroma_suppression,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300789 preview_enable_chroma_suppression,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300790 offsetof(struct prev_params, csup),
791 FIELD_SIZEOF(struct prev_params, csup),
792 offsetof(struct omap3isp_prev_update_config, csup),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300793 }, /* OMAP3ISP_PREV_WB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300794 preview_config_whitebalance,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300795 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300796 offsetof(struct prev_params, wbal),
797 FIELD_SIZEOF(struct prev_params, wbal),
798 offsetof(struct omap3isp_prev_update_config, wbal),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300799 }, /* OMAP3ISP_PREV_BLKADJ */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300800 preview_config_blkadj,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300801 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300802 offsetof(struct prev_params, blkadj),
803 FIELD_SIZEOF(struct prev_params, blkadj),
804 offsetof(struct omap3isp_prev_update_config, blkadj),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300805 }, /* OMAP3ISP_PREV_RGB2RGB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300806 preview_config_rgb_blending,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300807 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300808 offsetof(struct prev_params, rgb2rgb),
809 FIELD_SIZEOF(struct prev_params, rgb2rgb),
810 offsetof(struct omap3isp_prev_update_config, rgb2rgb),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300811 }, /* OMAP3ISP_PREV_COLOR_CONV */ {
Laurent Pinchart61738502012-04-17 13:05:12 -0300812 preview_config_csc,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300813 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300814 offsetof(struct prev_params, csc),
815 FIELD_SIZEOF(struct prev_params, csc),
816 offsetof(struct omap3isp_prev_update_config, csc),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300817 }, /* OMAP3ISP_PREV_YC_LIMIT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300818 preview_config_yc_range,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300819 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300820 offsetof(struct prev_params, yclimit),
821 FIELD_SIZEOF(struct prev_params, yclimit),
822 offsetof(struct omap3isp_prev_update_config, yclimit),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300823 }, /* OMAP3ISP_PREV_DEFECT_COR */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300824 preview_config_dcor,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300825 preview_enable_dcor,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300826 offsetof(struct prev_params, dcor),
827 FIELD_SIZEOF(struct prev_params, dcor),
828 offsetof(struct omap3isp_prev_update_config, dcor),
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300829 }, /* Previously OMAP3ISP_PREV_GAMMABYPASS, not used anymore */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300830 NULL,
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300831 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300832 }, /* OMAP3ISP_PREV_DRK_FRM_CAPTURE */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300833 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300834 preview_enable_drkframe_capture,
835 }, /* OMAP3ISP_PREV_DRK_FRM_SUBTRACT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300836 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300837 preview_enable_drkframe,
838 }, /* OMAP3ISP_PREV_LENS_SHADING */ {
Laurent Pinchartfbabfa82012-07-06 08:26:57 -0300839 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300840 preview_enable_drkframe,
841 }, /* OMAP3ISP_PREV_NF */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300842 preview_config_noisefilter,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300843 preview_enable_noisefilter,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300844 offsetof(struct prev_params, nf),
845 FIELD_SIZEOF(struct prev_params, nf),
846 offsetof(struct omap3isp_prev_update_config, nf),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300847 }, /* OMAP3ISP_PREV_GAMMA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300848 preview_config_gammacorrn,
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300849 preview_enable_gammacorrn,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300850 offsetof(struct prev_params, gamma),
851 FIELD_SIZEOF(struct prev_params, gamma),
852 offsetof(struct omap3isp_prev_update_config, gamma),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300853 }, /* OMAP3ISP_PREV_CONTRAST */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300854 preview_config_contrast,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300855 NULL,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300856 0, 0, 0, true,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300857 }, /* OMAP3ISP_PREV_BRIGHTNESS */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300858 preview_config_brightness,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300859 NULL,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300860 0, 0, 0, true,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300861 },
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300862};
863
864/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300865 * preview_config - Copy and update local structure with userspace preview
866 * configuration.
867 * @prev: ISP preview engine
868 * @cfg: Configuration
869 *
870 * Return zero if success or -EFAULT if the configuration can't be copied from
871 * userspace.
872 */
873static int preview_config(struct isp_prev_device *prev,
874 struct omap3isp_prev_update_config *cfg)
875{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300876 unsigned long flags;
877 unsigned int i;
878 int rval = 0;
879 u32 update;
880 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300881
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300882 if (cfg->update == 0)
883 return 0;
884
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300885 /* Mark the shadow parameters we're going to update as busy. */
886 spin_lock_irqsave(&prev->params.lock, flags);
887 preview_params_lock(prev, cfg->update, true);
888 active = prev->params.active;
889 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300890
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300891 update = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300892
893 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300894 const struct preview_update *attr = &update_attrs[i];
895 struct prev_params *params;
896 unsigned int bit = 1 << i;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300897
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300898 if (attr->skip || !(cfg->update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300899 continue;
900
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300901 params = &prev->params.params[!!(active & bit)];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300902
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300903 if (cfg->flag & bit) {
904 void __user *from = *(void * __user *)
905 ((void *)cfg + attr->config_offset);
906 void *to = (void *)params + attr->param_offset;
907 size_t size = attr->param_size;
908
909 if (to && from && size) {
910 if (copy_from_user(to, from, size)) {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300911 rval = -EFAULT;
912 break;
913 }
914 }
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300915 params->features |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300916 } else {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300917 params->features &= ~bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300918 }
919
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300920 update |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300921 }
922
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300923 spin_lock_irqsave(&prev->params.lock, flags);
924 preview_params_unlock(prev, update, true);
925 preview_params_switch(prev);
926 spin_unlock_irqrestore(&prev->params.lock, flags);
927
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300928 return rval;
929}
930
931/*
932 * preview_setup_hw - Setup preview registers and/or internal memory
933 * @prev: pointer to preview private structure
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300934 * @update: Bitmask of parameters to setup
935 * @active: Bitmask of parameters active in set 0
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300936 * Note: can be called from interrupt context
937 * Return none
938 */
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300939static void preview_setup_hw(struct isp_prev_device *prev, u32 update,
940 u32 active)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300941{
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300942 unsigned int i;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300943 u32 features;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300944
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300945 if (update == 0)
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300946 return;
947
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300948 features = (prev->params.params[0].features & active)
949 | (prev->params.params[1].features & ~active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300950
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300951 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
952 const struct preview_update *attr = &update_attrs[i];
953 struct prev_params *params;
954 unsigned int bit = 1 << i;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300955
956 if (!(update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300957 continue;
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300958
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300959 params = &prev->params.params[!(active & bit)];
960
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300961 if (params->features & bit) {
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300962 if (attr->config)
963 attr->config(prev, params);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300964 if (attr->enable)
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300965 attr->enable(prev, true);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300966 } else {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300967 if (attr->enable)
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300968 attr->enable(prev, false);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300969 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300970 }
971}
972
973/*
974 * preview_config_ycpos - Configure byte layout of YUV image.
975 * @mode: Indicates the required byte layout.
976 */
977static void
978preview_config_ycpos(struct isp_prev_device *prev,
979 enum v4l2_mbus_pixelcode pixelcode)
980{
981 struct isp_device *isp = to_isp_device(prev);
982 enum preview_ycpos_mode mode;
983
984 switch (pixelcode) {
985 case V4L2_MBUS_FMT_YUYV8_1X16:
986 mode = YCPOS_CrYCbY;
987 break;
988 case V4L2_MBUS_FMT_UYVY8_1X16:
989 mode = YCPOS_YCrYCb;
990 break;
991 default:
992 return;
993 }
994
995 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
996 ISPPRV_PCR_YCPOS_CrYCbY,
997 mode << ISPPRV_PCR_YCPOS_SHIFT);
998}
999
1000/*
1001 * preview_config_averager - Enable / disable / configure averager
1002 * @average: Average value to be configured.
1003 */
1004static void preview_config_averager(struct isp_prev_device *prev, u8 average)
1005{
1006 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001007
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001008 isp_reg_writel(isp, ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
1009 ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
1010 average, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001011}
1012
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001013
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001014/*
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001015 * preview_config_input_format - Configure the input format
1016 * @prev: The preview engine
Laurent Pincharte023a112012-10-23 08:11:55 -03001017 * @info: Sink pad format information
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001018 *
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001019 * Enable and configure CFA interpolation for Bayer formats and disable it for
1020 * greyscale formats.
1021 *
1022 * The CFA table is organised in four blocks, one per Bayer component. The
1023 * hardware expects blocks to follow the Bayer order of the input data, while
1024 * the driver stores the table in GRBG order in memory. The blocks need to be
1025 * reordered to support non-GRBG Bayer patterns.
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001026 */
1027static void preview_config_input_format(struct isp_prev_device *prev,
Laurent Pincharte023a112012-10-23 08:11:55 -03001028 const struct isp_format_info *info)
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001029{
1030 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001031 struct prev_params *params;
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001032
Laurent Pincharte023a112012-10-23 08:11:55 -03001033 if (info->width == 8)
1034 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1035 ISPPRV_PCR_WIDTH);
1036 else
1037 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1038 ISPPRV_PCR_WIDTH);
1039
1040 switch (info->flavor) {
1041 case V4L2_MBUS_FMT_SGRBG8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001042 prev->params.cfa_order = 0;
1043 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001044 case V4L2_MBUS_FMT_SRGGB8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001045 prev->params.cfa_order = 1;
1046 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001047 case V4L2_MBUS_FMT_SBGGR8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001048 prev->params.cfa_order = 2;
1049 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001050 case V4L2_MBUS_FMT_SGBRG8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001051 prev->params.cfa_order = 3;
1052 break;
1053 default:
1054 /* Disable CFA for non-Bayer formats. */
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001055 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1056 ISPPRV_PCR_CFAEN);
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001057 return;
1058 }
1059
1060 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR, ISPPRV_PCR_CFAEN);
1061 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1062 ISPPRV_PCR_CFAFMT_MASK, ISPPRV_PCR_CFAFMT_BAYER);
1063
1064 params = (prev->params.active & OMAP3ISP_PREV_CFA)
1065 ? &prev->params.params[0] : &prev->params.params[1];
1066
1067 preview_config_cfa(prev, params);
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001068}
1069
1070/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001071 * preview_config_input_size - Configure the input frame size
1072 *
1073 * The preview engine crops several rows and columns internally depending on
1074 * which processing blocks are enabled. The driver assumes all those blocks are
1075 * enabled when reporting source pad formats to userspace. If this assumption is
1076 * not true, rows and columns must be manually cropped at the preview engine
1077 * input to avoid overflows at the end of lines and frames.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001078 *
1079 * See the explanation at the PREV_MARGIN_* definitions for more details.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001080 */
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001081static void preview_config_input_size(struct isp_prev_device *prev, u32 active)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001082{
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001083 const struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001084 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001085 unsigned int sph = prev->crop.left;
1086 unsigned int eph = prev->crop.left + prev->crop.width - 1;
1087 unsigned int slv = prev->crop.top;
1088 unsigned int elv = prev->crop.top + prev->crop.height - 1;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001089 u32 features;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001090
Laurent Pincharte023a112012-10-23 08:11:55 -03001091 if (format->code != V4L2_MBUS_FMT_Y8_1X8 &&
1092 format->code != V4L2_MBUS_FMT_Y10_1X10) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001093 sph -= 2;
1094 eph += 2;
1095 slv -= 2;
1096 elv += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001097 }
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001098
1099 features = (prev->params.params[0].features & active)
1100 | (prev->params.params[1].features & ~active);
1101
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001102 if (features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001103 sph -= 2;
1104 eph += 2;
1105 slv -= 2;
1106 elv += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001107 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001108 if (features & OMAP3ISP_PREV_HRZ_MED) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001109 sph -= 2;
1110 eph += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001111 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001112 if (features & (OMAP3ISP_PREV_CHROMA_SUPP | OMAP3ISP_PREV_LUMAENH))
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001113 sph -= 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001114
1115 isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
1116 OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
1117 isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
1118 OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
1119}
1120
1121/*
1122 * preview_config_inlineoffset - Configures the Read address line offset.
1123 * @prev: Preview module
1124 * @offset: Line offset
1125 *
1126 * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
1127 * However, a hardware bug requires the memory start address to be aligned on a
1128 * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
1129 * well.
1130 */
1131static void
1132preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
1133{
1134 struct isp_device *isp = to_isp_device(prev);
1135
1136 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1137 ISPPRV_RADR_OFFSET);
1138}
1139
1140/*
1141 * preview_set_inaddr - Sets memory address of input frame.
1142 * @addr: 32bit memory address aligned on 32byte boundary.
1143 *
1144 * Configures the memory address from which the input frame is to be read.
1145 */
1146static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
1147{
1148 struct isp_device *isp = to_isp_device(prev);
1149
1150 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
1151}
1152
1153/*
1154 * preview_config_outlineoffset - Configures the Write address line offset.
1155 * @offset: Line Offset for the preview output.
1156 *
1157 * The offset must be a multiple of 32 bytes.
1158 */
1159static void preview_config_outlineoffset(struct isp_prev_device *prev,
1160 u32 offset)
1161{
1162 struct isp_device *isp = to_isp_device(prev);
1163
1164 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1165 ISPPRV_WADD_OFFSET);
1166}
1167
1168/*
1169 * preview_set_outaddr - Sets the memory address to store output frame
1170 * @addr: 32bit memory address aligned on 32byte boundary.
1171 *
1172 * Configures the memory address to which the output frame is written.
1173 */
1174static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
1175{
1176 struct isp_device *isp = to_isp_device(prev);
1177
1178 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
1179}
1180
1181static void preview_adjust_bandwidth(struct isp_prev_device *prev)
1182{
1183 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1184 struct isp_device *isp = to_isp_device(prev);
1185 const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
1186 unsigned long l3_ick = pipe->l3_ick;
1187 struct v4l2_fract *timeperframe;
1188 unsigned int cycles_per_frame;
1189 unsigned int requests_per_frame;
1190 unsigned int cycles_per_request;
1191 unsigned int minimum;
1192 unsigned int maximum;
1193 unsigned int value;
1194
1195 if (prev->input != PREVIEW_INPUT_MEMORY) {
1196 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1197 ISPSBL_SDR_REQ_PRV_EXP_MASK);
1198 return;
1199 }
1200
1201 /* Compute the minimum number of cycles per request, based on the
1202 * pipeline maximum data rate. This is an absolute lower bound if we
1203 * don't want SBL overflows, so round the value up.
1204 */
1205 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
1206 pipe->max_rate);
1207 minimum = DIV_ROUND_UP(cycles_per_request, 32);
1208
1209 /* Compute the maximum number of cycles per request, based on the
1210 * requested frame rate. This is a soft upper bound to achieve a frame
1211 * rate equal or higher than the requested value, so round the value
1212 * down.
1213 */
1214 timeperframe = &pipe->max_timeperframe;
1215
1216 requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
1217 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
1218 timeperframe->denominator);
1219 cycles_per_request = cycles_per_frame / requests_per_frame;
1220
1221 maximum = cycles_per_request / 32;
1222
1223 value = max(minimum, maximum);
1224
1225 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
1226 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1227 ISPSBL_SDR_REQ_PRV_EXP_MASK,
1228 value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
1229}
1230
1231/*
1232 * omap3isp_preview_busy - Gets busy state of preview module.
1233 */
1234int omap3isp_preview_busy(struct isp_prev_device *prev)
1235{
1236 struct isp_device *isp = to_isp_device(prev);
1237
1238 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
1239 & ISPPRV_PCR_BUSY;
1240}
1241
1242/*
1243 * omap3isp_preview_restore_context - Restores the values of preview registers
1244 */
1245void omap3isp_preview_restore_context(struct isp_device *isp)
1246{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001247 struct isp_prev_device *prev = &isp->isp_prev;
1248 const u32 update = OMAP3ISP_PREV_FEATURES_END - 1;
1249
1250 prev->params.params[0].update = prev->params.active & update;
1251 prev->params.params[1].update = ~prev->params.active & update;
1252
1253 preview_setup_hw(prev, update, prev->params.active);
1254
1255 prev->params.params[0].update = 0;
1256 prev->params.params[1].update = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001257}
1258
1259/*
1260 * preview_print_status - Dump preview module registers to the kernel log
1261 */
1262#define PREV_PRINT_REGISTER(isp, name)\
1263 dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
1264 isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
1265
1266static void preview_print_status(struct isp_prev_device *prev)
1267{
1268 struct isp_device *isp = to_isp_device(prev);
1269
1270 dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
1271
1272 PREV_PRINT_REGISTER(isp, PCR);
1273 PREV_PRINT_REGISTER(isp, HORZ_INFO);
1274 PREV_PRINT_REGISTER(isp, VERT_INFO);
1275 PREV_PRINT_REGISTER(isp, RSDR_ADDR);
1276 PREV_PRINT_REGISTER(isp, RADR_OFFSET);
1277 PREV_PRINT_REGISTER(isp, DSDR_ADDR);
1278 PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
1279 PREV_PRINT_REGISTER(isp, WSDR_ADDR);
1280 PREV_PRINT_REGISTER(isp, WADD_OFFSET);
1281 PREV_PRINT_REGISTER(isp, AVE);
1282 PREV_PRINT_REGISTER(isp, HMED);
1283 PREV_PRINT_REGISTER(isp, NF);
1284 PREV_PRINT_REGISTER(isp, WB_DGAIN);
1285 PREV_PRINT_REGISTER(isp, WBGAIN);
1286 PREV_PRINT_REGISTER(isp, WBSEL);
1287 PREV_PRINT_REGISTER(isp, CFA);
1288 PREV_PRINT_REGISTER(isp, BLKADJOFF);
1289 PREV_PRINT_REGISTER(isp, RGB_MAT1);
1290 PREV_PRINT_REGISTER(isp, RGB_MAT2);
1291 PREV_PRINT_REGISTER(isp, RGB_MAT3);
1292 PREV_PRINT_REGISTER(isp, RGB_MAT4);
1293 PREV_PRINT_REGISTER(isp, RGB_MAT5);
1294 PREV_PRINT_REGISTER(isp, RGB_OFF1);
1295 PREV_PRINT_REGISTER(isp, RGB_OFF2);
1296 PREV_PRINT_REGISTER(isp, CSC0);
1297 PREV_PRINT_REGISTER(isp, CSC1);
1298 PREV_PRINT_REGISTER(isp, CSC2);
1299 PREV_PRINT_REGISTER(isp, CSC_OFFSET);
1300 PREV_PRINT_REGISTER(isp, CNT_BRT);
1301 PREV_PRINT_REGISTER(isp, CSUP);
1302 PREV_PRINT_REGISTER(isp, SETUP_YC);
1303 PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
1304 PREV_PRINT_REGISTER(isp, CDC_THR0);
1305 PREV_PRINT_REGISTER(isp, CDC_THR1);
1306 PREV_PRINT_REGISTER(isp, CDC_THR2);
1307 PREV_PRINT_REGISTER(isp, CDC_THR3);
1308
1309 dev_dbg(isp->dev, "--------------------------------------------\n");
1310}
1311
1312/*
1313 * preview_init_params - init image processing parameters.
1314 * @prev: pointer to previewer private structure
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001315 */
1316static void preview_init_params(struct isp_prev_device *prev)
1317{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001318 struct prev_params *params;
1319 unsigned int i;
1320
1321 spin_lock_init(&prev->params.lock);
1322
1323 prev->params.active = ~0;
1324 prev->params.params[0].busy = 0;
1325 prev->params.params[0].update = OMAP3ISP_PREV_FEATURES_END - 1;
1326 prev->params.params[1].busy = 0;
1327 prev->params.params[1].update = 0;
1328
1329 params = &prev->params.params[0];
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001330
1331 /* Init values */
1332 params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
1333 params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001334 params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
1335 memcpy(params->cfa.table, cfa_coef_table,
1336 sizeof(params->cfa.table));
1337 params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
1338 params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
1339 params->csup.gain = FLR_CSUP_GAIN;
1340 params->csup.thres = FLR_CSUP_THRES;
1341 params->csup.hypf_en = 0;
1342 memcpy(params->luma.table, luma_enhance_table,
1343 sizeof(params->luma.table));
1344 params->nf.spread = FLR_NF_STRGTH;
1345 memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
1346 params->dcor.couplet_mode_en = 1;
1347 for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
1348 params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
1349 memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
1350 memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
1351 memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
1352 params->wbal.dgain = FLR_WBAL_DGAIN;
1353 params->wbal.coef0 = FLR_WBAL_COEF;
1354 params->wbal.coef1 = FLR_WBAL_COEF;
1355 params->wbal.coef2 = FLR_WBAL_COEF;
1356 params->wbal.coef3 = FLR_WBAL_COEF;
Laurent Pinchart9b001842012-04-05 13:51:17 -03001357 params->blkadj.red = FLR_BLKADJ_RED;
1358 params->blkadj.green = FLR_BLKADJ_GREEN;
1359 params->blkadj.blue = FLR_BLKADJ_BLUE;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001360 params->rgb2rgb = flr_rgb2rgb;
Laurent Pinchart9b001842012-04-05 13:51:17 -03001361 params->csc = flr_prev_csc;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001362 params->yclimit.minC = ISPPRV_YC_MIN;
1363 params->yclimit.maxC = ISPPRV_YC_MAX;
1364 params->yclimit.minY = ISPPRV_YC_MIN;
1365 params->yclimit.maxY = ISPPRV_YC_MAX;
1366
Laurent Pinchart3108e022012-04-05 12:38:23 -03001367 params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
1368 | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
1369 | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
1370 | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
1371 | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
1372 | OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001373}
1374
1375/*
1376 * preview_max_out_width - Handle previewer hardware ouput limitations
1377 * @isp_revision : ISP revision
1378 * returns maximum width output for current isp revision
1379 */
1380static unsigned int preview_max_out_width(struct isp_prev_device *prev)
1381{
1382 struct isp_device *isp = to_isp_device(prev);
1383
1384 switch (isp->revision) {
1385 case ISP_REVISION_1_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001386 return PREV_MAX_OUT_WIDTH_REV_1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001387
1388 case ISP_REVISION_2_0:
1389 default:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001390 return PREV_MAX_OUT_WIDTH_REV_2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001391
1392 case ISP_REVISION_15_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001393 return PREV_MAX_OUT_WIDTH_REV_15;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001394 }
1395}
1396
1397static void preview_configure(struct isp_prev_device *prev)
1398{
1399 struct isp_device *isp = to_isp_device(prev);
Laurent Pincharte023a112012-10-23 08:11:55 -03001400 const struct isp_format_info *info;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001401 struct v4l2_mbus_framefmt *format;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001402 unsigned long flags;
1403 u32 update;
1404 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001405
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001406 spin_lock_irqsave(&prev->params.lock, flags);
1407 /* Mark all active parameters we are going to touch as busy. */
1408 update = preview_params_lock(prev, 0, false);
1409 active = prev->params.active;
1410 spin_unlock_irqrestore(&prev->params.lock, flags);
1411
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001412 /* PREV_PAD_SINK */
1413 format = &prev->formats[PREV_PAD_SINK];
Laurent Pincharte023a112012-10-23 08:11:55 -03001414 info = omap3isp_video_format_info(format->code);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001415
1416 preview_adjust_bandwidth(prev);
1417
Laurent Pincharte023a112012-10-23 08:11:55 -03001418 preview_config_input_format(prev, info);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001419 preview_config_input_size(prev, active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001420
1421 if (prev->input == PREVIEW_INPUT_CCDC)
1422 preview_config_inlineoffset(prev, 0);
1423 else
Laurent Pincharte023a112012-10-23 08:11:55 -03001424 preview_config_inlineoffset(prev, ALIGN(format->width, 0x20) *
1425 info->bpp);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001426
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001427 preview_setup_hw(prev, update, active);
1428
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001429 /* PREV_PAD_SOURCE */
1430 format = &prev->formats[PREV_PAD_SOURCE];
1431
1432 if (prev->output & PREVIEW_OUTPUT_MEMORY)
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001433 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1434 ISPPRV_PCR_SDRPORT);
1435 else
1436 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1437 ISPPRV_PCR_SDRPORT);
1438
1439 if (prev->output & PREVIEW_OUTPUT_RESIZER)
1440 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1441 ISPPRV_PCR_RSZPORT);
1442 else
1443 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1444 ISPPRV_PCR_RSZPORT);
1445
1446 if (prev->output & PREVIEW_OUTPUT_MEMORY)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001447 preview_config_outlineoffset(prev,
1448 ALIGN(format->width, 0x10) * 2);
1449
Laurent Pincharte4bc6272011-09-21 07:54:44 -03001450 preview_config_averager(prev, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001451 preview_config_ycpos(prev, format->code);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001452
1453 spin_lock_irqsave(&prev->params.lock, flags);
1454 preview_params_unlock(prev, update, false);
1455 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001456}
1457
1458/* -----------------------------------------------------------------------------
1459 * Interrupt handling
1460 */
1461
1462static void preview_enable_oneshot(struct isp_prev_device *prev)
1463{
1464 struct isp_device *isp = to_isp_device(prev);
1465
1466 /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
1467 * bit is set. As the preview engine is used in single-shot mode, we
1468 * need to set PCR.SOURCE before enabling the preview engine.
1469 */
1470 if (prev->input == PREVIEW_INPUT_MEMORY)
1471 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1472 ISPPRV_PCR_SOURCE);
1473
1474 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1475 ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
1476}
1477
1478void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
1479{
1480 /*
1481 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1482 * condition, the module was paused and now we have a buffer queued
1483 * on the output again. Restart the pipeline if running in continuous
1484 * mode.
1485 */
1486 if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1487 prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1488 preview_enable_oneshot(prev);
1489 isp_video_dmaqueue_flags_clr(&prev->video_out);
1490 }
1491}
1492
1493static void preview_isr_buffer(struct isp_prev_device *prev)
1494{
1495 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1496 struct isp_buffer *buffer;
1497 int restart = 0;
1498
1499 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001500 buffer = omap3isp_video_buffer_next(&prev->video_in);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001501 if (buffer != NULL)
1502 preview_set_inaddr(prev, buffer->isp_addr);
1503 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1504 }
1505
1506 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001507 buffer = omap3isp_video_buffer_next(&prev->video_out);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001508 if (buffer != NULL) {
1509 preview_set_outaddr(prev, buffer->isp_addr);
1510 restart = 1;
1511 }
1512 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1513 }
1514
1515 switch (prev->state) {
1516 case ISP_PIPELINE_STREAM_SINGLESHOT:
1517 if (isp_pipeline_ready(pipe))
1518 omap3isp_pipeline_set_stream(pipe,
1519 ISP_PIPELINE_STREAM_SINGLESHOT);
1520 break;
1521
1522 case ISP_PIPELINE_STREAM_CONTINUOUS:
1523 /* If an underrun occurs, the video queue operation handler will
1524 * restart the preview engine. Otherwise restart it immediately.
1525 */
1526 if (restart)
1527 preview_enable_oneshot(prev);
1528 break;
1529
1530 case ISP_PIPELINE_STREAM_STOPPED:
1531 default:
1532 return;
1533 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001534}
1535
1536/*
1537 * omap3isp_preview_isr - ISP preview engine interrupt handler
1538 *
1539 * Manage the preview engine video buffers and configure shadowed registers.
1540 */
1541void omap3isp_preview_isr(struct isp_prev_device *prev)
1542{
1543 unsigned long flags;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001544 u32 update;
1545 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001546
1547 if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
1548 return;
1549
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001550 spin_lock_irqsave(&prev->params.lock, flags);
1551 preview_params_switch(prev);
1552 update = preview_params_lock(prev, 0, false);
1553 active = prev->params.active;
1554 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001555
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001556 preview_setup_hw(prev, update, active);
1557 preview_config_input_size(prev, active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001558
1559 if (prev->input == PREVIEW_INPUT_MEMORY ||
1560 prev->output & PREVIEW_OUTPUT_MEMORY)
1561 preview_isr_buffer(prev);
1562 else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1563 preview_enable_oneshot(prev);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001564
1565 spin_lock_irqsave(&prev->params.lock, flags);
1566 preview_params_unlock(prev, update, false);
1567 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001568}
1569
1570/* -----------------------------------------------------------------------------
1571 * ISP video operations
1572 */
1573
1574static int preview_video_queue(struct isp_video *video,
1575 struct isp_buffer *buffer)
1576{
1577 struct isp_prev_device *prev = &video->isp->isp_prev;
1578
1579 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1580 preview_set_inaddr(prev, buffer->isp_addr);
1581
1582 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1583 preview_set_outaddr(prev, buffer->isp_addr);
1584
1585 return 0;
1586}
1587
1588static const struct isp_video_operations preview_video_ops = {
1589 .queue = preview_video_queue,
1590};
1591
1592/* -----------------------------------------------------------------------------
1593 * V4L2 subdev operations
1594 */
1595
1596/*
1597 * preview_s_ctrl - Handle set control subdev method
1598 * @ctrl: pointer to v4l2 control structure
1599 */
1600static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
1601{
1602 struct isp_prev_device *prev =
1603 container_of(ctrl->handler, struct isp_prev_device, ctrls);
1604
1605 switch (ctrl->id) {
1606 case V4L2_CID_BRIGHTNESS:
1607 preview_update_brightness(prev, ctrl->val);
1608 break;
1609 case V4L2_CID_CONTRAST:
1610 preview_update_contrast(prev, ctrl->val);
1611 break;
1612 }
1613
1614 return 0;
1615}
1616
1617static const struct v4l2_ctrl_ops preview_ctrl_ops = {
1618 .s_ctrl = preview_s_ctrl,
1619};
1620
1621/*
1622 * preview_ioctl - Handle preview module private ioctl's
1623 * @prev: pointer to preview context structure
1624 * @cmd: configuration command
1625 * @arg: configuration argument
1626 * return -EINVAL or zero on success
1627 */
1628static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1629{
1630 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1631
1632 switch (cmd) {
1633 case VIDIOC_OMAP3ISP_PRV_CFG:
1634 return preview_config(prev, arg);
1635
1636 default:
1637 return -ENOIOCTLCMD;
1638 }
1639}
1640
1641/*
1642 * preview_set_stream - Enable/Disable streaming on preview subdev
1643 * @sd : pointer to v4l2 subdev structure
1644 * @enable: 1 == Enable, 0 == Disable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001645 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001646 */
1647static int preview_set_stream(struct v4l2_subdev *sd, int enable)
1648{
1649 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1650 struct isp_video *video_out = &prev->video_out;
1651 struct isp_device *isp = to_isp_device(prev);
1652 struct device *dev = to_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001653
1654 if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
1655 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1656 return 0;
1657
1658 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1659 preview_configure(prev);
1660 atomic_set(&prev->stopping, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001661 preview_print_status(prev);
1662 }
1663
1664 switch (enable) {
1665 case ISP_PIPELINE_STREAM_CONTINUOUS:
1666 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1667 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1668
1669 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
1670 !(prev->output & PREVIEW_OUTPUT_MEMORY))
1671 preview_enable_oneshot(prev);
1672
1673 isp_video_dmaqueue_flags_clr(video_out);
1674 break;
1675
1676 case ISP_PIPELINE_STREAM_SINGLESHOT:
1677 if (prev->input == PREVIEW_INPUT_MEMORY)
1678 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1679 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1680 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1681
1682 preview_enable_oneshot(prev);
1683 break;
1684
1685 case ISP_PIPELINE_STREAM_STOPPED:
1686 if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
1687 &prev->stopping))
1688 dev_dbg(dev, "%s: stop timeout.\n", sd->name);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001689 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1690 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1691 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001692 isp_video_dmaqueue_flags_clr(video_out);
1693 break;
1694 }
1695
1696 prev->state = enable;
1697 return 0;
1698}
1699
1700static struct v4l2_mbus_framefmt *
1701__preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1702 unsigned int pad, enum v4l2_subdev_format_whence which)
1703{
1704 if (which == V4L2_SUBDEV_FORMAT_TRY)
1705 return v4l2_subdev_get_try_format(fh, pad);
1706 else
1707 return &prev->formats[pad];
1708}
1709
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001710static struct v4l2_rect *
1711__preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1712 enum v4l2_subdev_format_whence which)
1713{
1714 if (which == V4L2_SUBDEV_FORMAT_TRY)
1715 return v4l2_subdev_get_try_crop(fh, PREV_PAD_SINK);
1716 else
1717 return &prev->crop;
1718}
1719
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001720/* previewer format descriptions */
1721static const unsigned int preview_input_fmts[] = {
Laurent Pincharte023a112012-10-23 08:11:55 -03001722 V4L2_MBUS_FMT_Y8_1X8,
1723 V4L2_MBUS_FMT_SGRBG8_1X8,
1724 V4L2_MBUS_FMT_SRGGB8_1X8,
1725 V4L2_MBUS_FMT_SBGGR8_1X8,
1726 V4L2_MBUS_FMT_SGBRG8_1X8,
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001727 V4L2_MBUS_FMT_Y10_1X10,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001728 V4L2_MBUS_FMT_SGRBG10_1X10,
1729 V4L2_MBUS_FMT_SRGGB10_1X10,
1730 V4L2_MBUS_FMT_SBGGR10_1X10,
1731 V4L2_MBUS_FMT_SGBRG10_1X10,
1732};
1733
1734static const unsigned int preview_output_fmts[] = {
1735 V4L2_MBUS_FMT_UYVY8_1X16,
1736 V4L2_MBUS_FMT_YUYV8_1X16,
1737};
1738
1739/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001740 * preview_try_format - Validate a format
1741 * @prev: ISP preview engine
1742 * @fh: V4L2 subdev file handle
1743 * @pad: pad number
1744 * @fmt: format to be validated
1745 * @which: try/active format selector
1746 *
1747 * Validate and adjust the given format for the given pad based on the preview
1748 * engine limits and the format and crop rectangles on other pads.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001749 */
1750static void preview_try_format(struct isp_prev_device *prev,
1751 struct v4l2_subdev_fh *fh, unsigned int pad,
1752 struct v4l2_mbus_framefmt *fmt,
1753 enum v4l2_subdev_format_whence which)
1754{
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001755 enum v4l2_mbus_pixelcode pixelcode;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001756 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001757 unsigned int i;
1758
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001759 switch (pad) {
1760 case PREV_PAD_SINK:
1761 /* When reading data from the CCDC, the input size has already
1762 * been mangled by the CCDC output pad so it can be accepted
1763 * as-is.
1764 *
1765 * When reading data from memory, clamp the requested width and
1766 * height. The TRM doesn't specify a minimum input height, make
1767 * sure we got enough lines to enable the noise filter and color
1768 * filter array interpolation.
1769 */
1770 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001771 fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
1772 preview_max_out_width(prev));
1773 fmt->height = clamp_t(u32, fmt->height,
1774 PREV_MIN_IN_HEIGHT,
1775 PREV_MAX_IN_HEIGHT);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001776 }
1777
1778 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1779
1780 for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
1781 if (fmt->code == preview_input_fmts[i])
1782 break;
1783 }
1784
1785 /* If not found, use SGRBG10 as default */
1786 if (i >= ARRAY_SIZE(preview_input_fmts))
1787 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1788 break;
1789
1790 case PREV_PAD_SOURCE:
1791 pixelcode = fmt->code;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001792 *fmt = *__preview_get_format(prev, fh, PREV_PAD_SINK, which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001793
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001794 switch (pixelcode) {
1795 case V4L2_MBUS_FMT_YUYV8_1X16:
1796 case V4L2_MBUS_FMT_UYVY8_1X16:
1797 fmt->code = pixelcode;
1798 break;
1799
1800 default:
1801 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1802 break;
1803 }
1804
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001805 /* The preview module output size is configurable through the
1806 * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
1807 * is not supported yet, hardcode the output size to the crop
1808 * rectangle size.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001809 */
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001810 crop = __preview_get_crop(prev, fh, which);
1811 fmt->width = crop->width;
1812 fmt->height = crop->height;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001813
1814 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1815 break;
1816 }
1817
1818 fmt->field = V4L2_FIELD_NONE;
1819}
1820
1821/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001822 * preview_try_crop - Validate a crop rectangle
1823 * @prev: ISP preview engine
1824 * @sink: format on the sink pad
1825 * @crop: crop rectangle to be validated
1826 *
1827 * The preview engine crops lines and columns for its internal operation,
1828 * depending on which filters are enabled. Enforce minimum crop margins to
1829 * handle that transparently for userspace.
1830 *
1831 * See the explanation at the PREV_MARGIN_* definitions for more details.
1832 */
1833static void preview_try_crop(struct isp_prev_device *prev,
1834 const struct v4l2_mbus_framefmt *sink,
1835 struct v4l2_rect *crop)
1836{
1837 unsigned int left = PREV_MARGIN_LEFT;
1838 unsigned int right = sink->width - PREV_MARGIN_RIGHT;
1839 unsigned int top = PREV_MARGIN_TOP;
1840 unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
1841
1842 /* When processing data on-the-fly from the CCDC, at least 2 pixels must
1843 * be cropped from the left and right sides of the image. As we don't
1844 * know which filters will be enabled, increase the left and right
1845 * margins by two.
1846 */
1847 if (prev->input == PREVIEW_INPUT_CCDC) {
1848 left += 2;
1849 right -= 2;
1850 }
1851
1852 /* Restrict left/top to even values to keep the Bayer pattern. */
1853 crop->left &= ~1;
1854 crop->top &= ~1;
1855
1856 crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
1857 crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
1858 crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
1859 right - crop->left);
1860 crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
1861 bottom - crop->top);
1862}
1863
1864/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001865 * preview_enum_mbus_code - Handle pixel format enumeration
1866 * @sd : pointer to v4l2 subdev structure
1867 * @fh : V4L2 subdev file handle
1868 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1869 * return -EINVAL or zero on success
1870 */
1871static int preview_enum_mbus_code(struct v4l2_subdev *sd,
1872 struct v4l2_subdev_fh *fh,
1873 struct v4l2_subdev_mbus_code_enum *code)
1874{
1875 switch (code->pad) {
1876 case PREV_PAD_SINK:
1877 if (code->index >= ARRAY_SIZE(preview_input_fmts))
1878 return -EINVAL;
1879
1880 code->code = preview_input_fmts[code->index];
1881 break;
1882 case PREV_PAD_SOURCE:
1883 if (code->index >= ARRAY_SIZE(preview_output_fmts))
1884 return -EINVAL;
1885
1886 code->code = preview_output_fmts[code->index];
1887 break;
1888 default:
1889 return -EINVAL;
1890 }
1891
1892 return 0;
1893}
1894
1895static int preview_enum_frame_size(struct v4l2_subdev *sd,
1896 struct v4l2_subdev_fh *fh,
1897 struct v4l2_subdev_frame_size_enum *fse)
1898{
1899 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1900 struct v4l2_mbus_framefmt format;
1901
1902 if (fse->index != 0)
1903 return -EINVAL;
1904
1905 format.code = fse->code;
1906 format.width = 1;
1907 format.height = 1;
1908 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1909 fse->min_width = format.width;
1910 fse->min_height = format.height;
1911
1912 if (format.code != fse->code)
1913 return -EINVAL;
1914
1915 format.code = fse->code;
1916 format.width = -1;
1917 format.height = -1;
1918 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1919 fse->max_width = format.width;
1920 fse->max_height = format.height;
1921
1922 return 0;
1923}
1924
1925/*
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001926 * preview_get_selection - Retrieve a selection rectangle on a pad
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001927 * @sd: ISP preview V4L2 subdevice
1928 * @fh: V4L2 subdev file handle
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001929 * @sel: Selection rectangle
1930 *
1931 * The only supported rectangles are the crop rectangles on the sink pad.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001932 *
1933 * Return 0 on success or a negative error code otherwise.
1934 */
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001935static int preview_get_selection(struct v4l2_subdev *sd,
1936 struct v4l2_subdev_fh *fh,
1937 struct v4l2_subdev_selection *sel)
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001938{
1939 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1940 struct v4l2_mbus_framefmt *format;
1941
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001942 if (sel->pad != PREV_PAD_SINK)
1943 return -EINVAL;
1944
1945 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001946 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001947 sel->r.left = 0;
1948 sel->r.top = 0;
1949 sel->r.width = INT_MAX;
1950 sel->r.height = INT_MAX;
1951
1952 format = __preview_get_format(prev, fh, PREV_PAD_SINK,
1953 sel->which);
1954 preview_try_crop(prev, format, &sel->r);
1955 break;
1956
Sakari Ailus5689b282012-05-18 09:31:18 -03001957 case V4L2_SEL_TGT_CROP:
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001958 sel->r = *__preview_get_crop(prev, fh, sel->which);
1959 break;
1960
1961 default:
1962 return -EINVAL;
1963 }
1964
1965 return 0;
1966}
1967
1968/*
1969 * preview_set_selection - Set a selection rectangle on a pad
1970 * @sd: ISP preview V4L2 subdevice
1971 * @fh: V4L2 subdev file handle
1972 * @sel: Selection rectangle
1973 *
1974 * The only supported rectangle is the actual crop rectangle on the sink pad.
1975 *
1976 * Return 0 on success or a negative error code otherwise.
1977 */
1978static int preview_set_selection(struct v4l2_subdev *sd,
1979 struct v4l2_subdev_fh *fh,
1980 struct v4l2_subdev_selection *sel)
1981{
1982 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1983 struct v4l2_mbus_framefmt *format;
1984
Sakari Ailus5689b282012-05-18 09:31:18 -03001985 if (sel->target != V4L2_SEL_TGT_CROP ||
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001986 sel->pad != PREV_PAD_SINK)
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001987 return -EINVAL;
1988
1989 /* The crop rectangle can't be changed while streaming. */
1990 if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
1991 return -EBUSY;
1992
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001993 /* Modifying the crop rectangle always changes the format on the source
1994 * pad. If the KEEP_CONFIG flag is set, just return the current crop
1995 * rectangle.
1996 */
Sakari Ailus563df3d2012-06-13 16:01:10 -03001997 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001998 sel->r = *__preview_get_crop(prev, fh, sel->which);
1999 return 0;
2000 }
2001
2002 format = __preview_get_format(prev, fh, PREV_PAD_SINK, sel->which);
2003 preview_try_crop(prev, format, &sel->r);
2004 *__preview_get_crop(prev, fh, sel->which) = sel->r;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002005
2006 /* Update the source format. */
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002007 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE, sel->which);
2008 preview_try_format(prev, fh, PREV_PAD_SOURCE, format, sel->which);
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002009
2010 return 0;
2011}
2012
2013/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002014 * preview_get_format - Handle get format by pads subdev method
2015 * @sd : pointer to v4l2 subdev structure
2016 * @fh : V4L2 subdev file handle
2017 * @fmt: pointer to v4l2 subdev format structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002018 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002019 */
2020static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2021 struct v4l2_subdev_format *fmt)
2022{
2023 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2024 struct v4l2_mbus_framefmt *format;
2025
2026 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2027 if (format == NULL)
2028 return -EINVAL;
2029
2030 fmt->format = *format;
2031 return 0;
2032}
2033
2034/*
2035 * preview_set_format - Handle set format by pads subdev method
2036 * @sd : pointer to v4l2 subdev structure
2037 * @fh : V4L2 subdev file handle
2038 * @fmt: pointer to v4l2 subdev format structure
2039 * return -EINVAL or zero on success
2040 */
2041static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2042 struct v4l2_subdev_format *fmt)
2043{
2044 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2045 struct v4l2_mbus_framefmt *format;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002046 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002047
2048 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2049 if (format == NULL)
2050 return -EINVAL;
2051
2052 preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
2053 *format = fmt->format;
2054
2055 /* Propagate the format from sink to source */
2056 if (fmt->pad == PREV_PAD_SINK) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002057 /* Reset the crop rectangle. */
2058 crop = __preview_get_crop(prev, fh, fmt->which);
2059 crop->left = 0;
2060 crop->top = 0;
2061 crop->width = fmt->format.width;
2062 crop->height = fmt->format.height;
2063
2064 preview_try_crop(prev, &fmt->format, crop);
2065
2066 /* Update the source format. */
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002067 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
2068 fmt->which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002069 preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
2070 fmt->which);
2071 }
2072
2073 return 0;
2074}
2075
2076/*
2077 * preview_init_formats - Initialize formats on all pads
2078 * @sd: ISP preview V4L2 subdevice
2079 * @fh: V4L2 subdev file handle
2080 *
2081 * Initialize all pad formats with default values. If fh is not NULL, try
2082 * formats are initialized on the file handle. Otherwise active formats are
2083 * initialized on the device.
2084 */
2085static int preview_init_formats(struct v4l2_subdev *sd,
2086 struct v4l2_subdev_fh *fh)
2087{
2088 struct v4l2_subdev_format format;
2089
2090 memset(&format, 0, sizeof(format));
2091 format.pad = PREV_PAD_SINK;
2092 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2093 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2094 format.format.width = 4096;
2095 format.format.height = 4096;
2096 preview_set_format(sd, fh, &format);
2097
2098 return 0;
2099}
2100
2101/* subdev core operations */
2102static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
2103 .ioctl = preview_ioctl,
2104};
2105
2106/* subdev video operations */
2107static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
2108 .s_stream = preview_set_stream,
2109};
2110
2111/* subdev pad operations */
2112static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
2113 .enum_mbus_code = preview_enum_mbus_code,
2114 .enum_frame_size = preview_enum_frame_size,
2115 .get_fmt = preview_get_format,
2116 .set_fmt = preview_set_format,
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002117 .get_selection = preview_get_selection,
2118 .set_selection = preview_set_selection,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002119};
2120
2121/* subdev operations */
2122static const struct v4l2_subdev_ops preview_v4l2_ops = {
2123 .core = &preview_v4l2_core_ops,
2124 .video = &preview_v4l2_video_ops,
2125 .pad = &preview_v4l2_pad_ops,
2126};
2127
2128/* subdev internal operations */
2129static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
2130 .open = preview_init_formats,
2131};
2132
2133/* -----------------------------------------------------------------------------
2134 * Media entity operations
2135 */
2136
2137/*
2138 * preview_link_setup - Setup previewer connections.
2139 * @entity : Pointer to media entity structure
2140 * @local : Pointer to local pad array
2141 * @remote : Pointer to remote pad array
2142 * @flags : Link flags
2143 * return -EINVAL or zero on success
2144 */
2145static int preview_link_setup(struct media_entity *entity,
2146 const struct media_pad *local,
2147 const struct media_pad *remote, u32 flags)
2148{
2149 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2150 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2151
2152 switch (local->index | media_entity_type(remote->entity)) {
2153 case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
2154 /* read from memory */
2155 if (flags & MEDIA_LNK_FL_ENABLED) {
2156 if (prev->input == PREVIEW_INPUT_CCDC)
2157 return -EBUSY;
2158 prev->input = PREVIEW_INPUT_MEMORY;
2159 } else {
2160 if (prev->input == PREVIEW_INPUT_MEMORY)
2161 prev->input = PREVIEW_INPUT_NONE;
2162 }
2163 break;
2164
2165 case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2166 /* read from ccdc */
2167 if (flags & MEDIA_LNK_FL_ENABLED) {
2168 if (prev->input == PREVIEW_INPUT_MEMORY)
2169 return -EBUSY;
2170 prev->input = PREVIEW_INPUT_CCDC;
2171 } else {
2172 if (prev->input == PREVIEW_INPUT_CCDC)
2173 prev->input = PREVIEW_INPUT_NONE;
2174 }
2175 break;
2176
2177 /*
2178 * The ISP core doesn't support pipelines with multiple video outputs.
2179 * Revisit this when it will be implemented, and return -EBUSY for now.
2180 */
2181
2182 case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
2183 /* write to memory */
2184 if (flags & MEDIA_LNK_FL_ENABLED) {
2185 if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
2186 return -EBUSY;
2187 prev->output |= PREVIEW_OUTPUT_MEMORY;
2188 } else {
2189 prev->output &= ~PREVIEW_OUTPUT_MEMORY;
2190 }
2191 break;
2192
2193 case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
2194 /* write to resizer */
2195 if (flags & MEDIA_LNK_FL_ENABLED) {
2196 if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
2197 return -EBUSY;
2198 prev->output |= PREVIEW_OUTPUT_RESIZER;
2199 } else {
2200 prev->output &= ~PREVIEW_OUTPUT_RESIZER;
2201 }
2202 break;
2203
2204 default:
2205 return -EINVAL;
2206 }
2207
2208 return 0;
2209}
2210
2211/* media operations */
2212static const struct media_entity_operations preview_media_ops = {
2213 .link_setup = preview_link_setup,
Sakari Ailus20d4ab72012-01-11 13:27:02 -03002214 .link_validate = v4l2_subdev_link_validate,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002215};
2216
Laurent Pinchart39099d02011-09-22 16:59:26 -03002217void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
2218{
2219 v4l2_device_unregister_subdev(&prev->subdev);
2220 omap3isp_video_unregister(&prev->video_in);
2221 omap3isp_video_unregister(&prev->video_out);
2222}
2223
2224int omap3isp_preview_register_entities(struct isp_prev_device *prev,
2225 struct v4l2_device *vdev)
2226{
2227 int ret;
2228
2229 /* Register the subdev and video nodes. */
2230 ret = v4l2_device_register_subdev(vdev, &prev->subdev);
2231 if (ret < 0)
2232 goto error;
2233
2234 ret = omap3isp_video_register(&prev->video_in, vdev);
2235 if (ret < 0)
2236 goto error;
2237
2238 ret = omap3isp_video_register(&prev->video_out, vdev);
2239 if (ret < 0)
2240 goto error;
2241
2242 return 0;
2243
2244error:
2245 omap3isp_preview_unregister_entities(prev);
2246 return ret;
2247}
2248
2249/* -----------------------------------------------------------------------------
2250 * ISP previewer initialisation and cleanup
2251 */
2252
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002253/*
Laurent Pinchart39099d02011-09-22 16:59:26 -03002254 * preview_init_entities - Initialize subdev and media entity.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002255 * @prev : Pointer to preview structure
2256 * return -ENOMEM or zero on success
2257 */
2258static int preview_init_entities(struct isp_prev_device *prev)
2259{
2260 struct v4l2_subdev *sd = &prev->subdev;
2261 struct media_pad *pads = prev->pads;
2262 struct media_entity *me = &sd->entity;
2263 int ret;
2264
2265 prev->input = PREVIEW_INPUT_NONE;
2266
2267 v4l2_subdev_init(sd, &preview_v4l2_ops);
2268 sd->internal_ops = &preview_v4l2_internal_ops;
2269 strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
2270 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2271 v4l2_set_subdevdata(sd, prev);
2272 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2273
2274 v4l2_ctrl_handler_init(&prev->ctrls, 2);
2275 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
2276 ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
2277 ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
2278 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
2279 ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
2280 ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
2281 v4l2_ctrl_handler_setup(&prev->ctrls);
2282 sd->ctrl_handler = &prev->ctrls;
2283
2284 pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2285 pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2286
2287 me->ops = &preview_media_ops;
2288 ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
2289 if (ret < 0)
2290 return ret;
2291
2292 preview_init_formats(sd, NULL);
2293
2294 /* According to the OMAP34xx TRM, video buffers need to be aligned on a
2295 * 32 bytes boundary. However, an undocumented hardware bug requires a
2296 * 64 bytes boundary at the preview engine input.
2297 */
2298 prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2299 prev->video_in.ops = &preview_video_ops;
2300 prev->video_in.isp = to_isp_device(prev);
2301 prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2302 prev->video_in.bpl_alignment = 64;
2303 prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2304 prev->video_out.ops = &preview_video_ops;
2305 prev->video_out.isp = to_isp_device(prev);
2306 prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2307 prev->video_out.bpl_alignment = 32;
2308
2309 ret = omap3isp_video_init(&prev->video_in, "preview");
2310 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002311 goto error_video_in;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002312
2313 ret = omap3isp_video_init(&prev->video_out, "preview");
2314 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002315 goto error_video_out;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002316
2317 /* Connect the video nodes to the previewer subdev. */
2318 ret = media_entity_create_link(&prev->video_in.video.entity, 0,
2319 &prev->subdev.entity, PREV_PAD_SINK, 0);
2320 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002321 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002322
2323 ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
2324 &prev->video_out.video.entity, 0, 0);
2325 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002326 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002327
2328 return 0;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002329
2330error_link:
2331 omap3isp_video_cleanup(&prev->video_out);
2332error_video_out:
2333 omap3isp_video_cleanup(&prev->video_in);
2334error_video_in:
2335 media_entity_cleanup(&prev->subdev.entity);
2336 return ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002337}
2338
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002339/*
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03002340 * omap3isp_preview_init - Previewer initialization.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002341 * @dev : Pointer to ISP device
2342 * return -ENOMEM or zero on success
2343 */
2344int omap3isp_preview_init(struct isp_device *isp)
2345{
2346 struct isp_prev_device *prev = &isp->isp_prev;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002347
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002348 init_waitqueue_head(&prev->wait);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03002349
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002350 preview_init_params(prev);
2351
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002352 return preview_init_entities(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002353}
Laurent Pinchart39099d02011-09-22 16:59:26 -03002354
2355void omap3isp_preview_cleanup(struct isp_device *isp)
2356{
2357 struct isp_prev_device *prev = &isp->isp_prev;
2358
2359 v4l2_ctrl_handler_free(&prev->ctrls);
2360 omap3isp_video_cleanup(&prev->video_in);
2361 omap3isp_video_cleanup(&prev->video_out);
2362 media_entity_cleanup(&prev->subdev.entity);
2363}