blob: e2e4610d55506847f8ea5a96537af878891c199c [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
Laurent Pinchart3fdfeda2012-12-07 07:48:55 -030085 * during streaming), the driver assumes all filters that can be configured
86 * during streaming are enabled when computing sink crop and source format
87 * limits.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -030088 *
89 * If a filter is disabled, additional cropping is automatically added at the
90 * preview engine input by the driver to avoid overflow at line and frame end.
91 * This is completely transparent for applications.
92 *
93 * Median filter 4 pixels
94 * Noise filter,
95 * Faulty pixels correction 4 pixels, 4 lines
Laurent Pinchart1f69fd92011-09-21 20:05:45 -030096 * Color suppression 2 pixels
97 * or luma enhancement
98 * -------------------------------------------------------------
Laurent Pinchart3fdfeda2012-12-07 07:48:55 -030099 * Maximum total 10 pixels, 4 lines
Laurent Pinchart1f69fd92011-09-21 20:05:45 -0300100 *
101 * The color suppression and luma enhancement filters are applied after bayer to
102 * YUV conversion. They thus can crop one pixel on the left and one pixel on the
103 * right side of the image without changing the color pattern. When both those
104 * filters are disabled, the driver must crop the two pixels on the same side of
105 * the image to avoid changing the bayer pattern. The left margin is thus set to
Laurent Pinchart3fdfeda2012-12-07 07:48:55 -0300106 * 6 pixels and the right margin to 4 pixels.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -0300107 */
108
Laurent Pinchart3fdfeda2012-12-07 07:48:55 -0300109#define PREV_MARGIN_LEFT 6
110#define PREV_MARGIN_RIGHT 4
111#define PREV_MARGIN_TOP 2
112#define PREV_MARGIN_BOTTOM 2
Laurent Pinchart1f69fd92011-09-21 20:05:45 -0300113
Laurent Pinchart059dc1d2011-10-03 07:56:15 -0300114#define PREV_MIN_IN_WIDTH 64
115#define PREV_MIN_IN_HEIGHT 8
116#define PREV_MAX_IN_HEIGHT 16384
117
Laurent Pinchartec0cae72011-10-03 07:56:15 -0300118#define PREV_MIN_OUT_WIDTH 0
119#define PREV_MIN_OUT_HEIGHT 0
120#define PREV_MAX_OUT_WIDTH_REV_1 1280
121#define PREV_MAX_OUT_WIDTH_REV_2 3300
122#define PREV_MAX_OUT_WIDTH_REV_15 4096
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300123
124/*
125 * Coeficient Tables for the submodules in Preview.
126 * Array is initialised with the values from.the tables text file.
127 */
128
129/*
130 * CFA Filter Coefficient Table
131 *
132 */
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300133static u32 cfa_coef_table[4][OMAP3ISP_PREV_CFA_BLK_SIZE] = {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300134#include "cfa_coef_table.h"
135};
136
137/*
138 * Default Gamma Correction Table - All components
139 */
140static u32 gamma_table[] = {
141#include "gamma_table.h"
142};
143
144/*
145 * Noise Filter Threshold table
146 */
147static u32 noise_filter_table[] = {
148#include "noise_filter_table.h"
149};
150
151/*
152 * Luminance Enhancement Table
153 */
154static u32 luma_enhance_table[] = {
155#include "luma_enhance_table.h"
156};
157
158/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300159 * preview_config_luma_enhancement - Configure the Luminance Enhancement table
160 */
161static void
162preview_config_luma_enhancement(struct isp_prev_device *prev,
163 const struct prev_params *params)
164{
165 struct isp_device *isp = to_isp_device(prev);
166 const struct omap3isp_prev_luma *yt = &params->luma;
167 unsigned int i;
168
169 isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
170 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
171 for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
172 isp_reg_writel(isp, yt->table[i],
173 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
174 }
175}
176
177/*
178 * preview_enable_luma_enhancement - Enable/disable Luminance Enhancement
179 */
180static void
181preview_enable_luma_enhancement(struct isp_prev_device *prev, bool enable)
182{
183 struct isp_device *isp = to_isp_device(prev);
184
185 if (enable)
186 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
187 ISPPRV_PCR_YNENHEN);
188 else
189 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
190 ISPPRV_PCR_YNENHEN);
191}
192
193/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300194 * preview_enable_invalaw - Enable/disable Inverse A-Law decompression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300195 */
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300196static void preview_enable_invalaw(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300197{
198 struct isp_device *isp = to_isp_device(prev);
199
200 if (enable)
201 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
Laurent Pincharte023a112012-10-23 08:11:55 -0300202 ISPPRV_PCR_INVALAW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300203 else
204 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
Laurent Pincharte023a112012-10-23 08:11:55 -0300205 ISPPRV_PCR_INVALAW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300206}
207
208/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300209 * preview_config_hmed - Configure the Horizontal Median Filter
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300210 */
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300211static void preview_config_hmed(struct isp_prev_device *prev,
212 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300213{
214 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300215 const struct omap3isp_prev_hmed *hmed = &params->hmed;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300216
217 isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
218 (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
219 (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
220 OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
221}
222
223/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300224 * preview_enable_hmed - Enable/disable the Horizontal Median Filter
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300225 */
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300226static void preview_enable_hmed(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300227{
228 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300229
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300230 if (enable)
231 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
232 ISPPRV_PCR_HMEDEN);
233 else
234 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
235 ISPPRV_PCR_HMEDEN);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300236}
237
238/*
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300239 * preview_config_cfa - Configure CFA Interpolation for Bayer formats
240 *
241 * The CFA table is organised in four blocks, one per Bayer component. The
242 * hardware expects blocks to follow the Bayer order of the input data, while
243 * the driver stores the table in GRBG order in memory. The blocks need to be
244 * reordered to support non-GRBG Bayer patterns.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300245 */
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300246static void preview_config_cfa(struct isp_prev_device *prev,
247 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300248{
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300249 static const unsigned int cfa_coef_order[4][4] = {
250 { 0, 1, 2, 3 }, /* GRBG */
251 { 1, 0, 3, 2 }, /* RGGB */
252 { 2, 3, 0, 1 }, /* BGGR */
253 { 3, 2, 1, 0 }, /* GBRG */
254 };
255 const unsigned int *order = cfa_coef_order[prev->params.cfa_order];
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300256 const struct omap3isp_prev_cfa *cfa = &params->cfa;
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300257 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300258 unsigned int i;
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300259 unsigned int j;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300260
261 isp_reg_writel(isp,
262 (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
263 (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
264 OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
265
266 isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
267 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
268
Laurent Pinchart6fd206c2012-06-18 11:24:48 -0300269 for (i = 0; i < 4; ++i) {
270 const __u32 *block = cfa->table[order[i]];
271
272 for (j = 0; j < OMAP3ISP_PREV_CFA_BLK_SIZE; ++j)
273 isp_reg_writel(isp, block[j], OMAP3_ISP_IOMEM_PREV,
274 ISPPRV_SET_TBL_DATA);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300275 }
276}
277
278/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300279 * preview_config_chroma_suppression - Configure Chroma Suppression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300280 */
281static void
282preview_config_chroma_suppression(struct isp_prev_device *prev,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300283 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300284{
285 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300286 const struct omap3isp_prev_csup *cs = &params->csup;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300287
288 isp_reg_writel(isp,
289 cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
290 (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
291 OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
292}
293
294/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300295 * preview_enable_chroma_suppression - Enable/disable Chrominance Suppression
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300296 */
297static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300298preview_enable_chroma_suppression(struct isp_prev_device *prev, bool enable)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300299{
300 struct isp_device *isp = to_isp_device(prev);
301
302 if (enable)
303 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
304 ISPPRV_PCR_SUPEN);
305 else
306 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
307 ISPPRV_PCR_SUPEN);
308}
309
310/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300311 * preview_config_whitebalance - Configure White Balance parameters
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300312 *
313 * Coefficient matrix always with default values.
314 */
315static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300316preview_config_whitebalance(struct isp_prev_device *prev,
317 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300318{
319 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300320 const struct omap3isp_prev_wbal *wbal = &params->wbal;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300321 u32 val;
322
323 isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
324
325 val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
326 val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
327 val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
328 val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
329 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
330
331 isp_reg_writel(isp,
332 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
333 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
334 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
335 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
336 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
337 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
338 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
339 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
340 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
341 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
342 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
343 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
344 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
345 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
346 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
347 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
348 OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
349}
350
351/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300352 * preview_config_blkadj - Configure Black Adjustment
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300353 */
354static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300355preview_config_blkadj(struct isp_prev_device *prev,
356 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300357{
358 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300359 const struct omap3isp_prev_blkadj *blkadj = &params->blkadj;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300360
361 isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
362 (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
363 (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
364 OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
365}
366
367/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300368 * preview_config_rgb_blending - Configure RGB-RGB Blending
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300369 */
370static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300371preview_config_rgb_blending(struct isp_prev_device *prev,
372 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300373{
374 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300375 const struct omap3isp_prev_rgbtorgb *rgbrgb = &params->rgb2rgb;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300376 u32 val;
377
378 val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
379 val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
380 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
381
382 val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
383 val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
384 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
385
386 val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
387 val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
388 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
389
390 val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
391 val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
392 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
393
394 val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
395 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
396
397 val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
398 val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
399 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
400
401 val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
402 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
403}
404
405/*
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300406 * preview_config_csc - Configure Color Space Conversion (RGB to YCbYCr)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300407 */
408static void
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300409preview_config_csc(struct isp_prev_device *prev,
410 const struct prev_params *params)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300411{
412 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300413 const struct omap3isp_prev_csc *csc = &params->csc;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300414 u32 val;
415
416 val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
417 val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
418 val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
419 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
420
421 val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
422 val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
423 val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
424 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
425
426 val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
427 val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
428 val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
429 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
430
431 val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
432 val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
433 val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
434 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
435}
436
437/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300438 * preview_config_yc_range - Configure the max and min Y and C values
439 */
440static void
441preview_config_yc_range(struct isp_prev_device *prev,
442 const struct prev_params *params)
443{
444 struct isp_device *isp = to_isp_device(prev);
445 const struct omap3isp_prev_yclimit *yc = &params->yclimit;
446
447 isp_reg_writel(isp,
448 yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
449 yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
450 yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
451 yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
452 OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
453}
454
455/*
456 * preview_config_dcor - Configure Couplet Defect Correction
457 */
458static void
459preview_config_dcor(struct isp_prev_device *prev,
460 const struct prev_params *params)
461{
462 struct isp_device *isp = to_isp_device(prev);
463 const struct omap3isp_prev_dcor *dcor = &params->dcor;
464
465 isp_reg_writel(isp, dcor->detect_correct[0],
466 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
467 isp_reg_writel(isp, dcor->detect_correct[1],
468 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
469 isp_reg_writel(isp, dcor->detect_correct[2],
470 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
471 isp_reg_writel(isp, dcor->detect_correct[3],
472 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
473 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
474 ISPPRV_PCR_DCCOUP,
475 dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
476}
477
478/*
479 * preview_enable_dcor - Enable/disable Couplet Defect Correction
480 */
481static void preview_enable_dcor(struct isp_prev_device *prev, bool enable)
482{
483 struct isp_device *isp = to_isp_device(prev);
484
485 if (enable)
486 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
487 ISPPRV_PCR_DCOREN);
488 else
489 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
490 ISPPRV_PCR_DCOREN);
491}
492
493/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300494 * preview_enable_drkframe_capture - Enable/disable Dark Frame Capture
495 */
496static void
497preview_enable_drkframe_capture(struct isp_prev_device *prev, bool enable)
498{
499 struct isp_device *isp = to_isp_device(prev);
500
501 if (enable)
502 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
503 ISPPRV_PCR_DRKFCAP);
504 else
505 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
506 ISPPRV_PCR_DRKFCAP);
507}
508
509/*
510 * preview_enable_drkframe - Enable/disable Dark Frame Subtraction
511 */
512static void preview_enable_drkframe(struct isp_prev_device *prev, bool enable)
513{
514 struct isp_device *isp = to_isp_device(prev);
515
516 if (enable)
517 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
518 ISPPRV_PCR_DRKFEN);
519 else
520 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
521 ISPPRV_PCR_DRKFEN);
522}
523
524/*
525 * preview_config_noisefilter - Configure the Noise Filter
526 */
527static void
528preview_config_noisefilter(struct isp_prev_device *prev,
529 const struct prev_params *params)
530{
531 struct isp_device *isp = to_isp_device(prev);
532 const struct omap3isp_prev_nf *nf = &params->nf;
533 unsigned int i;
534
535 isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
536 isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
537 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
538 for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
539 isp_reg_writel(isp, nf->table[i],
540 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
541 }
542}
543
544/*
545 * preview_enable_noisefilter - Enable/disable the Noise Filter
546 */
547static void
548preview_enable_noisefilter(struct isp_prev_device *prev, bool enable)
549{
550 struct isp_device *isp = to_isp_device(prev);
551
552 if (enable)
553 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
554 ISPPRV_PCR_NFEN);
555 else
556 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
557 ISPPRV_PCR_NFEN);
558}
559
560/*
561 * preview_config_gammacorrn - Configure the Gamma Correction tables
562 */
563static void
564preview_config_gammacorrn(struct isp_prev_device *prev,
565 const struct prev_params *params)
566{
567 struct isp_device *isp = to_isp_device(prev);
568 const struct omap3isp_prev_gtables *gt = &params->gamma;
569 unsigned int i;
570
571 isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
572 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
573 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
574 isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
575 ISPPRV_SET_TBL_DATA);
576
577 isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
578 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
579 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
580 isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
581 ISPPRV_SET_TBL_DATA);
582
583 isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
584 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
585 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
586 isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
587 ISPPRV_SET_TBL_DATA);
588}
589
590/*
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300591 * preview_enable_gammacorrn - Enable/disable Gamma Correction
592 *
593 * When gamma correction is disabled, the module is bypassed and its output is
594 * the 8 MSB of the 10-bit input .
595 */
596static void
597preview_enable_gammacorrn(struct isp_prev_device *prev, bool enable)
598{
599 struct isp_device *isp = to_isp_device(prev);
600
601 if (enable)
602 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
603 ISPPRV_PCR_GAMMA_BYPASS);
604 else
605 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
606 ISPPRV_PCR_GAMMA_BYPASS);
607}
608
609/*
Laurent Pinchart7bec7ef2012-07-06 09:07:33 -0300610 * preview_config_contrast - Configure the Contrast
611 *
612 * Value should be programmed before enabling the module.
613 */
614static void
615preview_config_contrast(struct isp_prev_device *prev,
616 const struct prev_params *params)
617{
618 struct isp_device *isp = to_isp_device(prev);
619
620 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
621 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
622 params->contrast << ISPPRV_CNT_BRT_CNT_SHIFT);
623}
624
625/*
626 * preview_config_brightness - Configure the Brightness
627 */
628static void
629preview_config_brightness(struct isp_prev_device *prev,
630 const struct prev_params *params)
631{
632 struct isp_device *isp = to_isp_device(prev);
633
634 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
635 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
636 params->brightness << ISPPRV_CNT_BRT_BRT_SHIFT);
637}
638
639/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300640 * preview_update_contrast - Updates the contrast.
641 * @contrast: Pointer to hold the current programmed contrast value.
642 *
643 * Value should be programmed before enabling the module.
644 */
645static void
646preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
647{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300648 struct prev_params *params;
649 unsigned long flags;
650
651 spin_lock_irqsave(&prev->params.lock, flags);
652 params = (prev->params.active & OMAP3ISP_PREV_CONTRAST)
653 ? &prev->params.params[0] : &prev->params.params[1];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300654
655 if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
656 params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300657 params->update |= OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300658 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300659 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300660}
661
662/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300663 * preview_update_brightness - Updates the brightness in preview module.
664 * @brightness: Pointer to hold the current programmed brightness value.
665 *
666 */
667static void
668preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
669{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300670 struct prev_params *params;
671 unsigned long flags;
672
673 spin_lock_irqsave(&prev->params.lock, flags);
674 params = (prev->params.active & OMAP3ISP_PREV_BRIGHTNESS)
675 ? &prev->params.params[0] : &prev->params.params[1];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300676
677 if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
678 params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300679 params->update |= OMAP3ISP_PREV_BRIGHTNESS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300680 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300681 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300682}
683
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300684static u32
685preview_params_lock(struct isp_prev_device *prev, u32 update, bool shadow)
686{
687 u32 active = prev->params.active;
688
689 if (shadow) {
690 /* Mark all shadow parameters we are going to touch as busy. */
691 prev->params.params[0].busy |= ~active & update;
692 prev->params.params[1].busy |= active & update;
693 } else {
694 /* Mark all active parameters we are going to touch as busy. */
695 update = (prev->params.params[0].update & active)
696 | (prev->params.params[1].update & ~active);
697
698 prev->params.params[0].busy |= active & update;
699 prev->params.params[1].busy |= ~active & update;
700 }
701
702 return update;
703}
704
705static void
706preview_params_unlock(struct isp_prev_device *prev, u32 update, bool shadow)
707{
708 u32 active = prev->params.active;
709
710 if (shadow) {
711 /* Set the update flag for shadow parameters that have been
712 * updated and clear the busy flag for all shadow parameters.
713 */
714 prev->params.params[0].update |= (~active & update);
715 prev->params.params[1].update |= (active & update);
716 prev->params.params[0].busy &= active;
717 prev->params.params[1].busy &= ~active;
718 } else {
719 /* Clear the update flag for active parameters that have been
720 * applied and the busy flag for all active parameters.
721 */
722 prev->params.params[0].update &= ~(active & update);
723 prev->params.params[1].update &= ~(~active & update);
724 prev->params.params[0].busy &= ~active;
725 prev->params.params[1].busy &= active;
726 }
727}
728
729static void preview_params_switch(struct isp_prev_device *prev)
730{
731 u32 to_switch;
732
733 /* Switch active parameters with updated shadow parameters when the
734 * shadow parameter has been updated and neither the active not the
735 * shadow parameter is busy.
736 */
737 to_switch = (prev->params.params[0].update & ~prev->params.active)
738 | (prev->params.params[1].update & prev->params.active);
739 to_switch &= ~(prev->params.params[0].busy |
740 prev->params.params[1].busy);
741 if (to_switch == 0)
742 return;
743
744 prev->params.active ^= to_switch;
745
746 /* Remove the update flag for the shadow copy of parameters we have
747 * switched.
748 */
749 prev->params.params[0].update &= ~(~prev->params.active & to_switch);
750 prev->params.params[1].update &= ~(prev->params.active & to_switch);
751}
752
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300753/* preview parameters update structure */
754struct preview_update {
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300755 void (*config)(struct isp_prev_device *, const struct prev_params *);
756 void (*enable)(struct isp_prev_device *, bool);
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300757 unsigned int param_offset;
758 unsigned int param_size;
759 unsigned int config_offset;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300760 bool skip;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300761};
762
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300763/* Keep the array indexed by the OMAP3ISP_PREV_* bit number. */
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300764static const struct preview_update update_attrs[] = {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300765 /* OMAP3ISP_PREV_LUMAENH */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300766 preview_config_luma_enhancement,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300767 preview_enable_luma_enhancement,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300768 offsetof(struct prev_params, luma),
769 FIELD_SIZEOF(struct prev_params, luma),
770 offsetof(struct omap3isp_prev_update_config, luma),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300771 }, /* OMAP3ISP_PREV_INVALAW */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300772 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300773 preview_enable_invalaw,
774 }, /* OMAP3ISP_PREV_HRZ_MED */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300775 preview_config_hmed,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300776 preview_enable_hmed,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300777 offsetof(struct prev_params, hmed),
778 FIELD_SIZEOF(struct prev_params, hmed),
779 offsetof(struct omap3isp_prev_update_config, hmed),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300780 }, /* OMAP3ISP_PREV_CFA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300781 preview_config_cfa,
Laurent Pinchartb2da46e2012-04-19 13:38:29 -0300782 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300783 offsetof(struct prev_params, cfa),
784 FIELD_SIZEOF(struct prev_params, cfa),
785 offsetof(struct omap3isp_prev_update_config, cfa),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300786 }, /* OMAP3ISP_PREV_CHROMA_SUPP */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300787 preview_config_chroma_suppression,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300788 preview_enable_chroma_suppression,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300789 offsetof(struct prev_params, csup),
790 FIELD_SIZEOF(struct prev_params, csup),
791 offsetof(struct omap3isp_prev_update_config, csup),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300792 }, /* OMAP3ISP_PREV_WB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300793 preview_config_whitebalance,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300794 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300795 offsetof(struct prev_params, wbal),
796 FIELD_SIZEOF(struct prev_params, wbal),
797 offsetof(struct omap3isp_prev_update_config, wbal),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300798 }, /* OMAP3ISP_PREV_BLKADJ */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300799 preview_config_blkadj,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300800 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300801 offsetof(struct prev_params, blkadj),
802 FIELD_SIZEOF(struct prev_params, blkadj),
803 offsetof(struct omap3isp_prev_update_config, blkadj),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300804 }, /* OMAP3ISP_PREV_RGB2RGB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300805 preview_config_rgb_blending,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300806 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300807 offsetof(struct prev_params, rgb2rgb),
808 FIELD_SIZEOF(struct prev_params, rgb2rgb),
809 offsetof(struct omap3isp_prev_update_config, rgb2rgb),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300810 }, /* OMAP3ISP_PREV_COLOR_CONV */ {
Laurent Pinchart61738502012-04-17 13:05:12 -0300811 preview_config_csc,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300812 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300813 offsetof(struct prev_params, csc),
814 FIELD_SIZEOF(struct prev_params, csc),
815 offsetof(struct omap3isp_prev_update_config, csc),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300816 }, /* OMAP3ISP_PREV_YC_LIMIT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300817 preview_config_yc_range,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300818 NULL,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300819 offsetof(struct prev_params, yclimit),
820 FIELD_SIZEOF(struct prev_params, yclimit),
821 offsetof(struct omap3isp_prev_update_config, yclimit),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300822 }, /* OMAP3ISP_PREV_DEFECT_COR */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300823 preview_config_dcor,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300824 preview_enable_dcor,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300825 offsetof(struct prev_params, dcor),
826 FIELD_SIZEOF(struct prev_params, dcor),
827 offsetof(struct omap3isp_prev_update_config, dcor),
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300828 }, /* Previously OMAP3ISP_PREV_GAMMABYPASS, not used anymore */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300829 NULL,
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300830 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300831 }, /* OMAP3ISP_PREV_DRK_FRM_CAPTURE */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300832 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300833 preview_enable_drkframe_capture,
834 }, /* OMAP3ISP_PREV_DRK_FRM_SUBTRACT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300835 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300836 preview_enable_drkframe,
837 }, /* OMAP3ISP_PREV_LENS_SHADING */ {
Laurent Pinchartfbabfa82012-07-06 08:26:57 -0300838 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300839 preview_enable_drkframe,
840 }, /* OMAP3ISP_PREV_NF */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300841 preview_config_noisefilter,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300842 preview_enable_noisefilter,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300843 offsetof(struct prev_params, nf),
844 FIELD_SIZEOF(struct prev_params, nf),
845 offsetof(struct omap3isp_prev_update_config, nf),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300846 }, /* OMAP3ISP_PREV_GAMMA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300847 preview_config_gammacorrn,
Laurent Pinchartac9dad92012-07-06 09:12:44 -0300848 preview_enable_gammacorrn,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300849 offsetof(struct prev_params, gamma),
850 FIELD_SIZEOF(struct prev_params, gamma),
851 offsetof(struct omap3isp_prev_update_config, gamma),
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300852 }, /* OMAP3ISP_PREV_CONTRAST */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300853 preview_config_contrast,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300854 NULL,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300855 0, 0, 0, true,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300856 }, /* OMAP3ISP_PREV_BRIGHTNESS */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300857 preview_config_brightness,
Laurent Pinchartbac387e2012-04-09 09:25:34 -0300858 NULL,
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300859 0, 0, 0, true,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300860 },
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300861};
862
863/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300864 * preview_config - Copy and update local structure with userspace preview
865 * configuration.
866 * @prev: ISP preview engine
867 * @cfg: Configuration
868 *
869 * Return zero if success or -EFAULT if the configuration can't be copied from
870 * userspace.
871 */
872static int preview_config(struct isp_prev_device *prev,
873 struct omap3isp_prev_update_config *cfg)
874{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300875 unsigned long flags;
876 unsigned int i;
877 int rval = 0;
878 u32 update;
879 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300880
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300881 if (cfg->update == 0)
882 return 0;
883
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300884 /* Mark the shadow parameters we're going to update as busy. */
885 spin_lock_irqsave(&prev->params.lock, flags);
886 preview_params_lock(prev, cfg->update, true);
887 active = prev->params.active;
888 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300889
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300890 update = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300891
892 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300893 const struct preview_update *attr = &update_attrs[i];
894 struct prev_params *params;
895 unsigned int bit = 1 << i;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300896
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300897 if (attr->skip || !(cfg->update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300898 continue;
899
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300900 params = &prev->params.params[!!(active & bit)];
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300901
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300902 if (cfg->flag & bit) {
903 void __user *from = *(void * __user *)
904 ((void *)cfg + attr->config_offset);
905 void *to = (void *)params + attr->param_offset;
906 size_t size = attr->param_size;
907
908 if (to && from && size) {
909 if (copy_from_user(to, from, size)) {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300910 rval = -EFAULT;
911 break;
912 }
913 }
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300914 params->features |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300915 } else {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300916 params->features &= ~bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300917 }
918
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300919 update |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300920 }
921
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300922 spin_lock_irqsave(&prev->params.lock, flags);
923 preview_params_unlock(prev, update, true);
924 preview_params_switch(prev);
925 spin_unlock_irqrestore(&prev->params.lock, flags);
926
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300927 return rval;
928}
929
930/*
931 * preview_setup_hw - Setup preview registers and/or internal memory
932 * @prev: pointer to preview private structure
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300933 * @update: Bitmask of parameters to setup
934 * @active: Bitmask of parameters active in set 0
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300935 * Note: can be called from interrupt context
936 * Return none
937 */
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300938static void preview_setup_hw(struct isp_prev_device *prev, u32 update,
939 u32 active)
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300940{
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300941 unsigned int i;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300942 u32 features;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300943
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300944 if (update == 0)
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300945 return;
946
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300947 features = (prev->params.params[0].features & active)
948 | (prev->params.params[1].features & ~active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300949
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300950 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
951 const struct preview_update *attr = &update_attrs[i];
952 struct prev_params *params;
953 unsigned int bit = 1 << i;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300954
955 if (!(update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300956 continue;
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300957
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300958 params = &prev->params.params[!(active & bit)];
959
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300960 if (params->features & bit) {
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300961 if (attr->config)
962 attr->config(prev, params);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300963 if (attr->enable)
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300964 attr->enable(prev, true);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300965 } else {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300966 if (attr->enable)
Laurent Pinchart6f1dd562012-07-06 08:43:27 -0300967 attr->enable(prev, false);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -0300968 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300969 }
970}
971
972/*
973 * preview_config_ycpos - Configure byte layout of YUV image.
974 * @mode: Indicates the required byte layout.
975 */
976static void
977preview_config_ycpos(struct isp_prev_device *prev,
978 enum v4l2_mbus_pixelcode pixelcode)
979{
980 struct isp_device *isp = to_isp_device(prev);
981 enum preview_ycpos_mode mode;
982
983 switch (pixelcode) {
984 case V4L2_MBUS_FMT_YUYV8_1X16:
985 mode = YCPOS_CrYCbY;
986 break;
987 case V4L2_MBUS_FMT_UYVY8_1X16:
988 mode = YCPOS_YCrYCb;
989 break;
990 default:
991 return;
992 }
993
994 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
995 ISPPRV_PCR_YCPOS_CrYCbY,
996 mode << ISPPRV_PCR_YCPOS_SHIFT);
997}
998
999/*
1000 * preview_config_averager - Enable / disable / configure averager
1001 * @average: Average value to be configured.
1002 */
1003static void preview_config_averager(struct isp_prev_device *prev, u8 average)
1004{
1005 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001006
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001007 isp_reg_writel(isp, ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
1008 ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
1009 average, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001010}
1011
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001012
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001013/*
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001014 * preview_config_input_format - Configure the input format
1015 * @prev: The preview engine
Laurent Pincharte023a112012-10-23 08:11:55 -03001016 * @info: Sink pad format information
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001017 *
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001018 * Enable and configure CFA interpolation for Bayer formats and disable it for
1019 * greyscale formats.
1020 *
1021 * The CFA table is organised in four blocks, one per Bayer component. The
1022 * hardware expects blocks to follow the Bayer order of the input data, while
1023 * the driver stores the table in GRBG order in memory. The blocks need to be
1024 * reordered to support non-GRBG Bayer patterns.
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001025 */
1026static void preview_config_input_format(struct isp_prev_device *prev,
Laurent Pincharte023a112012-10-23 08:11:55 -03001027 const struct isp_format_info *info)
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001028{
1029 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001030 struct prev_params *params;
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001031
Laurent Pincharte023a112012-10-23 08:11:55 -03001032 if (info->width == 8)
1033 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1034 ISPPRV_PCR_WIDTH);
1035 else
1036 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1037 ISPPRV_PCR_WIDTH);
1038
1039 switch (info->flavor) {
1040 case V4L2_MBUS_FMT_SGRBG8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001041 prev->params.cfa_order = 0;
1042 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001043 case V4L2_MBUS_FMT_SRGGB8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001044 prev->params.cfa_order = 1;
1045 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001046 case V4L2_MBUS_FMT_SBGGR8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001047 prev->params.cfa_order = 2;
1048 break;
Laurent Pincharte023a112012-10-23 08:11:55 -03001049 case V4L2_MBUS_FMT_SGBRG8_1X8:
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001050 prev->params.cfa_order = 3;
1051 break;
1052 default:
1053 /* Disable CFA for non-Bayer formats. */
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001054 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1055 ISPPRV_PCR_CFAEN);
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001056 return;
1057 }
1058
1059 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR, ISPPRV_PCR_CFAEN);
1060 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1061 ISPPRV_PCR_CFAFMT_MASK, ISPPRV_PCR_CFAFMT_BAYER);
1062
1063 params = (prev->params.active & OMAP3ISP_PREV_CFA)
1064 ? &prev->params.params[0] : &prev->params.params[1];
1065
1066 preview_config_cfa(prev, params);
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001067}
1068
1069/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001070 * preview_config_input_size - Configure the input frame size
1071 *
1072 * The preview engine crops several rows and columns internally depending on
1073 * which processing blocks are enabled. The driver assumes all those blocks are
1074 * enabled when reporting source pad formats to userspace. If this assumption is
1075 * not true, rows and columns must be manually cropped at the preview engine
1076 * input to avoid overflows at the end of lines and frames.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001077 *
1078 * See the explanation at the PREV_MARGIN_* definitions for more details.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001079 */
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001080static void preview_config_input_size(struct isp_prev_device *prev, u32 active)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001081{
Florian Vaussardfe529a22014-01-17 16:37:38 -03001082 const struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001083 struct isp_device *isp = to_isp_device(prev);
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001084 unsigned int sph = prev->crop.left;
1085 unsigned int eph = prev->crop.left + prev->crop.width - 1;
1086 unsigned int slv = prev->crop.top;
1087 unsigned int elv = prev->crop.top + prev->crop.height - 1;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001088 u32 features;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001089
Florian Vaussardfe529a22014-01-17 16:37:38 -03001090 if (format->code != V4L2_MBUS_FMT_Y8_1X8 &&
1091 format->code != V4L2_MBUS_FMT_Y10_1X10) {
1092 sph -= 2;
1093 eph += 2;
1094 slv -= 2;
1095 elv += 2;
1096 }
1097
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001098 features = (prev->params.params[0].features & active)
1099 | (prev->params.params[1].features & ~active);
1100
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001101 if (features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001102 sph -= 2;
1103 eph += 2;
1104 slv -= 2;
1105 elv += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001106 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001107 if (features & OMAP3ISP_PREV_HRZ_MED) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001108 sph -= 2;
1109 eph += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001110 }
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001111 if (features & (OMAP3ISP_PREV_CHROMA_SUPP | OMAP3ISP_PREV_LUMAENH))
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001112 sph -= 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001113
1114 isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
1115 OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
1116 isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
1117 OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
1118}
1119
1120/*
1121 * preview_config_inlineoffset - Configures the Read address line offset.
1122 * @prev: Preview module
1123 * @offset: Line offset
1124 *
1125 * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
1126 * However, a hardware bug requires the memory start address to be aligned on a
1127 * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
1128 * well.
1129 */
1130static void
1131preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
1132{
1133 struct isp_device *isp = to_isp_device(prev);
1134
1135 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1136 ISPPRV_RADR_OFFSET);
1137}
1138
1139/*
1140 * preview_set_inaddr - Sets memory address of input frame.
1141 * @addr: 32bit memory address aligned on 32byte boundary.
1142 *
1143 * Configures the memory address from which the input frame is to be read.
1144 */
1145static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
1146{
1147 struct isp_device *isp = to_isp_device(prev);
1148
1149 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
1150}
1151
1152/*
1153 * preview_config_outlineoffset - Configures the Write address line offset.
1154 * @offset: Line Offset for the preview output.
1155 *
1156 * The offset must be a multiple of 32 bytes.
1157 */
1158static void preview_config_outlineoffset(struct isp_prev_device *prev,
1159 u32 offset)
1160{
1161 struct isp_device *isp = to_isp_device(prev);
1162
1163 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1164 ISPPRV_WADD_OFFSET);
1165}
1166
1167/*
1168 * preview_set_outaddr - Sets the memory address to store output frame
1169 * @addr: 32bit memory address aligned on 32byte boundary.
1170 *
1171 * Configures the memory address to which the output frame is written.
1172 */
1173static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
1174{
1175 struct isp_device *isp = to_isp_device(prev);
1176
1177 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
1178}
1179
1180static void preview_adjust_bandwidth(struct isp_prev_device *prev)
1181{
1182 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1183 struct isp_device *isp = to_isp_device(prev);
1184 const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
1185 unsigned long l3_ick = pipe->l3_ick;
1186 struct v4l2_fract *timeperframe;
1187 unsigned int cycles_per_frame;
1188 unsigned int requests_per_frame;
1189 unsigned int cycles_per_request;
1190 unsigned int minimum;
1191 unsigned int maximum;
1192 unsigned int value;
1193
1194 if (prev->input != PREVIEW_INPUT_MEMORY) {
1195 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1196 ISPSBL_SDR_REQ_PRV_EXP_MASK);
1197 return;
1198 }
1199
1200 /* Compute the minimum number of cycles per request, based on the
1201 * pipeline maximum data rate. This is an absolute lower bound if we
1202 * don't want SBL overflows, so round the value up.
1203 */
1204 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
1205 pipe->max_rate);
1206 minimum = DIV_ROUND_UP(cycles_per_request, 32);
1207
1208 /* Compute the maximum number of cycles per request, based on the
1209 * requested frame rate. This is a soft upper bound to achieve a frame
1210 * rate equal or higher than the requested value, so round the value
1211 * down.
1212 */
1213 timeperframe = &pipe->max_timeperframe;
1214
1215 requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
1216 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
1217 timeperframe->denominator);
1218 cycles_per_request = cycles_per_frame / requests_per_frame;
1219
1220 maximum = cycles_per_request / 32;
1221
1222 value = max(minimum, maximum);
1223
1224 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
1225 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1226 ISPSBL_SDR_REQ_PRV_EXP_MASK,
1227 value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
1228}
1229
1230/*
1231 * omap3isp_preview_busy - Gets busy state of preview module.
1232 */
1233int omap3isp_preview_busy(struct isp_prev_device *prev)
1234{
1235 struct isp_device *isp = to_isp_device(prev);
1236
1237 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
1238 & ISPPRV_PCR_BUSY;
1239}
1240
1241/*
1242 * omap3isp_preview_restore_context - Restores the values of preview registers
1243 */
1244void omap3isp_preview_restore_context(struct isp_device *isp)
1245{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001246 struct isp_prev_device *prev = &isp->isp_prev;
1247 const u32 update = OMAP3ISP_PREV_FEATURES_END - 1;
1248
1249 prev->params.params[0].update = prev->params.active & update;
1250 prev->params.params[1].update = ~prev->params.active & update;
1251
1252 preview_setup_hw(prev, update, prev->params.active);
1253
1254 prev->params.params[0].update = 0;
1255 prev->params.params[1].update = 0;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001256}
1257
1258/*
1259 * preview_print_status - Dump preview module registers to the kernel log
1260 */
1261#define PREV_PRINT_REGISTER(isp, name)\
1262 dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
1263 isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
1264
1265static void preview_print_status(struct isp_prev_device *prev)
1266{
1267 struct isp_device *isp = to_isp_device(prev);
1268
1269 dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
1270
1271 PREV_PRINT_REGISTER(isp, PCR);
1272 PREV_PRINT_REGISTER(isp, HORZ_INFO);
1273 PREV_PRINT_REGISTER(isp, VERT_INFO);
1274 PREV_PRINT_REGISTER(isp, RSDR_ADDR);
1275 PREV_PRINT_REGISTER(isp, RADR_OFFSET);
1276 PREV_PRINT_REGISTER(isp, DSDR_ADDR);
1277 PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
1278 PREV_PRINT_REGISTER(isp, WSDR_ADDR);
1279 PREV_PRINT_REGISTER(isp, WADD_OFFSET);
1280 PREV_PRINT_REGISTER(isp, AVE);
1281 PREV_PRINT_REGISTER(isp, HMED);
1282 PREV_PRINT_REGISTER(isp, NF);
1283 PREV_PRINT_REGISTER(isp, WB_DGAIN);
1284 PREV_PRINT_REGISTER(isp, WBGAIN);
1285 PREV_PRINT_REGISTER(isp, WBSEL);
1286 PREV_PRINT_REGISTER(isp, CFA);
1287 PREV_PRINT_REGISTER(isp, BLKADJOFF);
1288 PREV_PRINT_REGISTER(isp, RGB_MAT1);
1289 PREV_PRINT_REGISTER(isp, RGB_MAT2);
1290 PREV_PRINT_REGISTER(isp, RGB_MAT3);
1291 PREV_PRINT_REGISTER(isp, RGB_MAT4);
1292 PREV_PRINT_REGISTER(isp, RGB_MAT5);
1293 PREV_PRINT_REGISTER(isp, RGB_OFF1);
1294 PREV_PRINT_REGISTER(isp, RGB_OFF2);
1295 PREV_PRINT_REGISTER(isp, CSC0);
1296 PREV_PRINT_REGISTER(isp, CSC1);
1297 PREV_PRINT_REGISTER(isp, CSC2);
1298 PREV_PRINT_REGISTER(isp, CSC_OFFSET);
1299 PREV_PRINT_REGISTER(isp, CNT_BRT);
1300 PREV_PRINT_REGISTER(isp, CSUP);
1301 PREV_PRINT_REGISTER(isp, SETUP_YC);
1302 PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
1303 PREV_PRINT_REGISTER(isp, CDC_THR0);
1304 PREV_PRINT_REGISTER(isp, CDC_THR1);
1305 PREV_PRINT_REGISTER(isp, CDC_THR2);
1306 PREV_PRINT_REGISTER(isp, CDC_THR3);
1307
1308 dev_dbg(isp->dev, "--------------------------------------------\n");
1309}
1310
1311/*
1312 * preview_init_params - init image processing parameters.
1313 * @prev: pointer to previewer private structure
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001314 */
1315static void preview_init_params(struct isp_prev_device *prev)
1316{
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001317 struct prev_params *params;
1318 unsigned int i;
1319
1320 spin_lock_init(&prev->params.lock);
1321
1322 prev->params.active = ~0;
1323 prev->params.params[0].busy = 0;
1324 prev->params.params[0].update = OMAP3ISP_PREV_FEATURES_END - 1;
1325 prev->params.params[1].busy = 0;
1326 prev->params.params[1].update = 0;
1327
1328 params = &prev->params.params[0];
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001329
1330 /* Init values */
1331 params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
1332 params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001333 params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
1334 memcpy(params->cfa.table, cfa_coef_table,
1335 sizeof(params->cfa.table));
1336 params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
1337 params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
1338 params->csup.gain = FLR_CSUP_GAIN;
1339 params->csup.thres = FLR_CSUP_THRES;
1340 params->csup.hypf_en = 0;
1341 memcpy(params->luma.table, luma_enhance_table,
1342 sizeof(params->luma.table));
1343 params->nf.spread = FLR_NF_STRGTH;
1344 memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
1345 params->dcor.couplet_mode_en = 1;
1346 for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
1347 params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
1348 memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
1349 memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
1350 memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
1351 params->wbal.dgain = FLR_WBAL_DGAIN;
1352 params->wbal.coef0 = FLR_WBAL_COEF;
1353 params->wbal.coef1 = FLR_WBAL_COEF;
1354 params->wbal.coef2 = FLR_WBAL_COEF;
1355 params->wbal.coef3 = FLR_WBAL_COEF;
Laurent Pinchart9b001842012-04-05 13:51:17 -03001356 params->blkadj.red = FLR_BLKADJ_RED;
1357 params->blkadj.green = FLR_BLKADJ_GREEN;
1358 params->blkadj.blue = FLR_BLKADJ_BLUE;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001359 params->rgb2rgb = flr_rgb2rgb;
Laurent Pinchart9b001842012-04-05 13:51:17 -03001360 params->csc = flr_prev_csc;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001361 params->yclimit.minC = ISPPRV_YC_MIN;
1362 params->yclimit.maxC = ISPPRV_YC_MAX;
1363 params->yclimit.minY = ISPPRV_YC_MIN;
1364 params->yclimit.maxY = ISPPRV_YC_MAX;
1365
Laurent Pinchart3108e022012-04-05 12:38:23 -03001366 params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
1367 | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
1368 | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
1369 | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
1370 | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
1371 | OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001372}
1373
1374/*
1375 * preview_max_out_width - Handle previewer hardware ouput limitations
1376 * @isp_revision : ISP revision
1377 * returns maximum width output for current isp revision
1378 */
1379static unsigned int preview_max_out_width(struct isp_prev_device *prev)
1380{
1381 struct isp_device *isp = to_isp_device(prev);
1382
1383 switch (isp->revision) {
1384 case ISP_REVISION_1_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001385 return PREV_MAX_OUT_WIDTH_REV_1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001386
1387 case ISP_REVISION_2_0:
1388 default:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001389 return PREV_MAX_OUT_WIDTH_REV_2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001390
1391 case ISP_REVISION_15_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001392 return PREV_MAX_OUT_WIDTH_REV_15;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001393 }
1394}
1395
1396static void preview_configure(struct isp_prev_device *prev)
1397{
1398 struct isp_device *isp = to_isp_device(prev);
Laurent Pincharte023a112012-10-23 08:11:55 -03001399 const struct isp_format_info *info;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001400 struct v4l2_mbus_framefmt *format;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001401 unsigned long flags;
1402 u32 update;
1403 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001404
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001405 spin_lock_irqsave(&prev->params.lock, flags);
1406 /* Mark all active parameters we are going to touch as busy. */
1407 update = preview_params_lock(prev, 0, false);
1408 active = prev->params.active;
1409 spin_unlock_irqrestore(&prev->params.lock, flags);
1410
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001411 /* PREV_PAD_SINK */
1412 format = &prev->formats[PREV_PAD_SINK];
Laurent Pincharte023a112012-10-23 08:11:55 -03001413 info = omap3isp_video_format_info(format->code);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001414
1415 preview_adjust_bandwidth(prev);
1416
Laurent Pincharte023a112012-10-23 08:11:55 -03001417 preview_config_input_format(prev, info);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001418 preview_config_input_size(prev, active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001419
1420 if (prev->input == PREVIEW_INPUT_CCDC)
1421 preview_config_inlineoffset(prev, 0);
1422 else
Laurent Pincharte023a112012-10-23 08:11:55 -03001423 preview_config_inlineoffset(prev, ALIGN(format->width, 0x20) *
1424 info->bpp);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001425
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001426 preview_setup_hw(prev, update, active);
1427
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001428 /* PREV_PAD_SOURCE */
1429 format = &prev->formats[PREV_PAD_SOURCE];
1430
1431 if (prev->output & PREVIEW_OUTPUT_MEMORY)
Laurent Pinchart6fd206c2012-06-18 11:24:48 -03001432 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1433 ISPPRV_PCR_SDRPORT);
1434 else
1435 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1436 ISPPRV_PCR_SDRPORT);
1437
1438 if (prev->output & PREVIEW_OUTPUT_RESIZER)
1439 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1440 ISPPRV_PCR_RSZPORT);
1441 else
1442 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1443 ISPPRV_PCR_RSZPORT);
1444
1445 if (prev->output & PREVIEW_OUTPUT_MEMORY)
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001446 preview_config_outlineoffset(prev,
1447 ALIGN(format->width, 0x10) * 2);
1448
Laurent Pincharte4bc6272011-09-21 07:54:44 -03001449 preview_config_averager(prev, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001450 preview_config_ycpos(prev, format->code);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001451
1452 spin_lock_irqsave(&prev->params.lock, flags);
1453 preview_params_unlock(prev, update, false);
1454 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001455}
1456
1457/* -----------------------------------------------------------------------------
1458 * Interrupt handling
1459 */
1460
1461static void preview_enable_oneshot(struct isp_prev_device *prev)
1462{
1463 struct isp_device *isp = to_isp_device(prev);
1464
1465 /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
1466 * bit is set. As the preview engine is used in single-shot mode, we
1467 * need to set PCR.SOURCE before enabling the preview engine.
1468 */
1469 if (prev->input == PREVIEW_INPUT_MEMORY)
1470 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1471 ISPPRV_PCR_SOURCE);
1472
1473 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1474 ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
1475}
1476
1477void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
1478{
1479 /*
1480 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1481 * condition, the module was paused and now we have a buffer queued
1482 * on the output again. Restart the pipeline if running in continuous
1483 * mode.
1484 */
1485 if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1486 prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1487 preview_enable_oneshot(prev);
1488 isp_video_dmaqueue_flags_clr(&prev->video_out);
1489 }
1490}
1491
1492static void preview_isr_buffer(struct isp_prev_device *prev)
1493{
1494 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1495 struct isp_buffer *buffer;
1496 int restart = 0;
1497
1498 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001499 buffer = omap3isp_video_buffer_next(&prev->video_in);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001500 if (buffer != NULL)
1501 preview_set_inaddr(prev, buffer->isp_addr);
1502 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1503 }
1504
1505 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001506 buffer = omap3isp_video_buffer_next(&prev->video_out);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001507 if (buffer != NULL) {
1508 preview_set_outaddr(prev, buffer->isp_addr);
1509 restart = 1;
1510 }
1511 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1512 }
1513
1514 switch (prev->state) {
1515 case ISP_PIPELINE_STREAM_SINGLESHOT:
1516 if (isp_pipeline_ready(pipe))
1517 omap3isp_pipeline_set_stream(pipe,
1518 ISP_PIPELINE_STREAM_SINGLESHOT);
1519 break;
1520
1521 case ISP_PIPELINE_STREAM_CONTINUOUS:
1522 /* If an underrun occurs, the video queue operation handler will
1523 * restart the preview engine. Otherwise restart it immediately.
1524 */
1525 if (restart)
1526 preview_enable_oneshot(prev);
1527 break;
1528
1529 case ISP_PIPELINE_STREAM_STOPPED:
1530 default:
1531 return;
1532 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001533}
1534
1535/*
1536 * omap3isp_preview_isr - ISP preview engine interrupt handler
1537 *
1538 * Manage the preview engine video buffers and configure shadowed registers.
1539 */
1540void omap3isp_preview_isr(struct isp_prev_device *prev)
1541{
1542 unsigned long flags;
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001543 u32 update;
1544 u32 active;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001545
1546 if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
1547 return;
1548
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001549 spin_lock_irqsave(&prev->params.lock, flags);
1550 preview_params_switch(prev);
1551 update = preview_params_lock(prev, 0, false);
1552 active = prev->params.active;
1553 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001554
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001555 preview_setup_hw(prev, update, active);
1556 preview_config_input_size(prev, active);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001557
1558 if (prev->input == PREVIEW_INPUT_MEMORY ||
1559 prev->output & PREVIEW_OUTPUT_MEMORY)
1560 preview_isr_buffer(prev);
1561 else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1562 preview_enable_oneshot(prev);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03001563
1564 spin_lock_irqsave(&prev->params.lock, flags);
1565 preview_params_unlock(prev, update, false);
1566 spin_unlock_irqrestore(&prev->params.lock, flags);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001567}
1568
1569/* -----------------------------------------------------------------------------
1570 * ISP video operations
1571 */
1572
1573static int preview_video_queue(struct isp_video *video,
1574 struct isp_buffer *buffer)
1575{
1576 struct isp_prev_device *prev = &video->isp->isp_prev;
1577
1578 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1579 preview_set_inaddr(prev, buffer->isp_addr);
1580
1581 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1582 preview_set_outaddr(prev, buffer->isp_addr);
1583
1584 return 0;
1585}
1586
1587static const struct isp_video_operations preview_video_ops = {
1588 .queue = preview_video_queue,
1589};
1590
1591/* -----------------------------------------------------------------------------
1592 * V4L2 subdev operations
1593 */
1594
1595/*
1596 * preview_s_ctrl - Handle set control subdev method
1597 * @ctrl: pointer to v4l2 control structure
1598 */
1599static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
1600{
1601 struct isp_prev_device *prev =
1602 container_of(ctrl->handler, struct isp_prev_device, ctrls);
1603
1604 switch (ctrl->id) {
1605 case V4L2_CID_BRIGHTNESS:
1606 preview_update_brightness(prev, ctrl->val);
1607 break;
1608 case V4L2_CID_CONTRAST:
1609 preview_update_contrast(prev, ctrl->val);
1610 break;
1611 }
1612
1613 return 0;
1614}
1615
1616static const struct v4l2_ctrl_ops preview_ctrl_ops = {
1617 .s_ctrl = preview_s_ctrl,
1618};
1619
1620/*
1621 * preview_ioctl - Handle preview module private ioctl's
1622 * @prev: pointer to preview context structure
1623 * @cmd: configuration command
1624 * @arg: configuration argument
1625 * return -EINVAL or zero on success
1626 */
1627static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1628{
1629 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1630
1631 switch (cmd) {
1632 case VIDIOC_OMAP3ISP_PRV_CFG:
1633 return preview_config(prev, arg);
1634
1635 default:
1636 return -ENOIOCTLCMD;
1637 }
1638}
1639
1640/*
1641 * preview_set_stream - Enable/Disable streaming on preview subdev
1642 * @sd : pointer to v4l2 subdev structure
1643 * @enable: 1 == Enable, 0 == Disable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001644 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001645 */
1646static int preview_set_stream(struct v4l2_subdev *sd, int enable)
1647{
1648 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1649 struct isp_video *video_out = &prev->video_out;
1650 struct isp_device *isp = to_isp_device(prev);
1651 struct device *dev = to_device(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001652
1653 if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
1654 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1655 return 0;
1656
1657 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1658 preview_configure(prev);
1659 atomic_set(&prev->stopping, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001660 preview_print_status(prev);
1661 }
1662
1663 switch (enable) {
1664 case ISP_PIPELINE_STREAM_CONTINUOUS:
1665 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1666 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1667
1668 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
1669 !(prev->output & PREVIEW_OUTPUT_MEMORY))
1670 preview_enable_oneshot(prev);
1671
1672 isp_video_dmaqueue_flags_clr(video_out);
1673 break;
1674
1675 case ISP_PIPELINE_STREAM_SINGLESHOT:
1676 if (prev->input == PREVIEW_INPUT_MEMORY)
1677 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1678 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1679 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1680
1681 preview_enable_oneshot(prev);
1682 break;
1683
1684 case ISP_PIPELINE_STREAM_STOPPED:
1685 if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
1686 &prev->stopping))
1687 dev_dbg(dev, "%s: stop timeout.\n", sd->name);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001688 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1689 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1690 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001691 isp_video_dmaqueue_flags_clr(video_out);
1692 break;
1693 }
1694
1695 prev->state = enable;
1696 return 0;
1697}
1698
1699static struct v4l2_mbus_framefmt *
1700__preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1701 unsigned int pad, enum v4l2_subdev_format_whence which)
1702{
1703 if (which == V4L2_SUBDEV_FORMAT_TRY)
1704 return v4l2_subdev_get_try_format(fh, pad);
1705 else
1706 return &prev->formats[pad];
1707}
1708
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001709static struct v4l2_rect *
1710__preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1711 enum v4l2_subdev_format_whence which)
1712{
1713 if (which == V4L2_SUBDEV_FORMAT_TRY)
1714 return v4l2_subdev_get_try_crop(fh, PREV_PAD_SINK);
1715 else
1716 return &prev->crop;
1717}
1718
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001719/* previewer format descriptions */
1720static const unsigned int preview_input_fmts[] = {
Laurent Pincharte023a112012-10-23 08:11:55 -03001721 V4L2_MBUS_FMT_Y8_1X8,
1722 V4L2_MBUS_FMT_SGRBG8_1X8,
1723 V4L2_MBUS_FMT_SRGGB8_1X8,
1724 V4L2_MBUS_FMT_SBGGR8_1X8,
1725 V4L2_MBUS_FMT_SGBRG8_1X8,
Laurent Pinchartb2da46e2012-04-19 13:38:29 -03001726 V4L2_MBUS_FMT_Y10_1X10,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001727 V4L2_MBUS_FMT_SGRBG10_1X10,
1728 V4L2_MBUS_FMT_SRGGB10_1X10,
1729 V4L2_MBUS_FMT_SBGGR10_1X10,
1730 V4L2_MBUS_FMT_SGBRG10_1X10,
1731};
1732
1733static const unsigned int preview_output_fmts[] = {
1734 V4L2_MBUS_FMT_UYVY8_1X16,
1735 V4L2_MBUS_FMT_YUYV8_1X16,
1736};
1737
1738/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001739 * preview_try_format - Validate a format
1740 * @prev: ISP preview engine
1741 * @fh: V4L2 subdev file handle
1742 * @pad: pad number
1743 * @fmt: format to be validated
1744 * @which: try/active format selector
1745 *
1746 * Validate and adjust the given format for the given pad based on the preview
1747 * engine limits and the format and crop rectangles on other pads.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001748 */
1749static void preview_try_format(struct isp_prev_device *prev,
1750 struct v4l2_subdev_fh *fh, unsigned int pad,
1751 struct v4l2_mbus_framefmt *fmt,
1752 enum v4l2_subdev_format_whence which)
1753{
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001754 enum v4l2_mbus_pixelcode pixelcode;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001755 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001756 unsigned int i;
1757
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001758 switch (pad) {
1759 case PREV_PAD_SINK:
1760 /* When reading data from the CCDC, the input size has already
1761 * been mangled by the CCDC output pad so it can be accepted
1762 * as-is.
1763 *
1764 * When reading data from memory, clamp the requested width and
1765 * height. The TRM doesn't specify a minimum input height, make
1766 * sure we got enough lines to enable the noise filter and color
1767 * filter array interpolation.
1768 */
1769 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001770 fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
1771 preview_max_out_width(prev));
1772 fmt->height = clamp_t(u32, fmt->height,
1773 PREV_MIN_IN_HEIGHT,
1774 PREV_MAX_IN_HEIGHT);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001775 }
1776
1777 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1778
1779 for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
1780 if (fmt->code == preview_input_fmts[i])
1781 break;
1782 }
1783
1784 /* If not found, use SGRBG10 as default */
1785 if (i >= ARRAY_SIZE(preview_input_fmts))
1786 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1787 break;
1788
1789 case PREV_PAD_SOURCE:
1790 pixelcode = fmt->code;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001791 *fmt = *__preview_get_format(prev, fh, PREV_PAD_SINK, which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001792
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001793 switch (pixelcode) {
1794 case V4L2_MBUS_FMT_YUYV8_1X16:
1795 case V4L2_MBUS_FMT_UYVY8_1X16:
1796 fmt->code = pixelcode;
1797 break;
1798
1799 default:
1800 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1801 break;
1802 }
1803
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001804 /* The preview module output size is configurable through the
1805 * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
1806 * is not supported yet, hardcode the output size to the crop
1807 * rectangle size.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001808 */
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001809 crop = __preview_get_crop(prev, fh, which);
1810 fmt->width = crop->width;
1811 fmt->height = crop->height;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001812
1813 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1814 break;
1815 }
1816
1817 fmt->field = V4L2_FIELD_NONE;
1818}
1819
1820/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001821 * preview_try_crop - Validate a crop rectangle
1822 * @prev: ISP preview engine
1823 * @sink: format on the sink pad
1824 * @crop: crop rectangle to be validated
1825 *
1826 * The preview engine crops lines and columns for its internal operation,
1827 * depending on which filters are enabled. Enforce minimum crop margins to
1828 * handle that transparently for userspace.
1829 *
1830 * See the explanation at the PREV_MARGIN_* definitions for more details.
1831 */
1832static void preview_try_crop(struct isp_prev_device *prev,
1833 const struct v4l2_mbus_framefmt *sink,
1834 struct v4l2_rect *crop)
1835{
1836 unsigned int left = PREV_MARGIN_LEFT;
1837 unsigned int right = sink->width - PREV_MARGIN_RIGHT;
1838 unsigned int top = PREV_MARGIN_TOP;
1839 unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
1840
1841 /* When processing data on-the-fly from the CCDC, at least 2 pixels must
1842 * be cropped from the left and right sides of the image. As we don't
1843 * know which filters will be enabled, increase the left and right
1844 * margins by two.
1845 */
1846 if (prev->input == PREVIEW_INPUT_CCDC) {
1847 left += 2;
1848 right -= 2;
1849 }
1850
Laurent Pinchart3fdfeda2012-12-07 07:48:55 -03001851 /* The CFA filter crops 4 lines and 4 columns in Bayer mode, and 2 lines
1852 * and no columns in other modes. Increase the margins based on the sink
1853 * format.
1854 */
1855 if (sink->code != V4L2_MBUS_FMT_Y8_1X8 &&
1856 sink->code != V4L2_MBUS_FMT_Y10_1X10) {
1857 left += 2;
1858 right -= 2;
1859 top += 2;
1860 bottom -= 2;
1861 }
1862
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001863 /* Restrict left/top to even values to keep the Bayer pattern. */
1864 crop->left &= ~1;
1865 crop->top &= ~1;
1866
1867 crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
1868 crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
1869 crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
1870 right - crop->left);
1871 crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
1872 bottom - crop->top);
1873}
1874
1875/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001876 * preview_enum_mbus_code - Handle pixel format enumeration
1877 * @sd : pointer to v4l2 subdev structure
1878 * @fh : V4L2 subdev file handle
1879 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1880 * return -EINVAL or zero on success
1881 */
1882static int preview_enum_mbus_code(struct v4l2_subdev *sd,
1883 struct v4l2_subdev_fh *fh,
1884 struct v4l2_subdev_mbus_code_enum *code)
1885{
1886 switch (code->pad) {
1887 case PREV_PAD_SINK:
1888 if (code->index >= ARRAY_SIZE(preview_input_fmts))
1889 return -EINVAL;
1890
1891 code->code = preview_input_fmts[code->index];
1892 break;
1893 case PREV_PAD_SOURCE:
1894 if (code->index >= ARRAY_SIZE(preview_output_fmts))
1895 return -EINVAL;
1896
1897 code->code = preview_output_fmts[code->index];
1898 break;
1899 default:
1900 return -EINVAL;
1901 }
1902
1903 return 0;
1904}
1905
1906static int preview_enum_frame_size(struct v4l2_subdev *sd,
1907 struct v4l2_subdev_fh *fh,
1908 struct v4l2_subdev_frame_size_enum *fse)
1909{
1910 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1911 struct v4l2_mbus_framefmt format;
1912
1913 if (fse->index != 0)
1914 return -EINVAL;
1915
1916 format.code = fse->code;
1917 format.width = 1;
1918 format.height = 1;
1919 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1920 fse->min_width = format.width;
1921 fse->min_height = format.height;
1922
1923 if (format.code != fse->code)
1924 return -EINVAL;
1925
1926 format.code = fse->code;
1927 format.width = -1;
1928 format.height = -1;
1929 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1930 fse->max_width = format.width;
1931 fse->max_height = format.height;
1932
1933 return 0;
1934}
1935
1936/*
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001937 * preview_get_selection - Retrieve a selection rectangle on a pad
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001938 * @sd: ISP preview V4L2 subdevice
1939 * @fh: V4L2 subdev file handle
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001940 * @sel: Selection rectangle
1941 *
1942 * The only supported rectangles are the crop rectangles on the sink pad.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001943 *
1944 * Return 0 on success or a negative error code otherwise.
1945 */
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001946static int preview_get_selection(struct v4l2_subdev *sd,
1947 struct v4l2_subdev_fh *fh,
1948 struct v4l2_subdev_selection *sel)
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001949{
1950 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1951 struct v4l2_mbus_framefmt *format;
1952
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001953 if (sel->pad != PREV_PAD_SINK)
1954 return -EINVAL;
1955
1956 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001957 case V4L2_SEL_TGT_CROP_BOUNDS:
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001958 sel->r.left = 0;
1959 sel->r.top = 0;
1960 sel->r.width = INT_MAX;
1961 sel->r.height = INT_MAX;
1962
1963 format = __preview_get_format(prev, fh, PREV_PAD_SINK,
1964 sel->which);
1965 preview_try_crop(prev, format, &sel->r);
1966 break;
1967
Sakari Ailus5689b282012-05-18 09:31:18 -03001968 case V4L2_SEL_TGT_CROP:
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001969 sel->r = *__preview_get_crop(prev, fh, sel->which);
1970 break;
1971
1972 default:
1973 return -EINVAL;
1974 }
1975
1976 return 0;
1977}
1978
1979/*
1980 * preview_set_selection - Set a selection rectangle on a pad
1981 * @sd: ISP preview V4L2 subdevice
1982 * @fh: V4L2 subdev file handle
1983 * @sel: Selection rectangle
1984 *
1985 * The only supported rectangle is the actual crop rectangle on the sink pad.
1986 *
1987 * Return 0 on success or a negative error code otherwise.
1988 */
1989static int preview_set_selection(struct v4l2_subdev *sd,
1990 struct v4l2_subdev_fh *fh,
1991 struct v4l2_subdev_selection *sel)
1992{
1993 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1994 struct v4l2_mbus_framefmt *format;
1995
Sakari Ailus5689b282012-05-18 09:31:18 -03001996 if (sel->target != V4L2_SEL_TGT_CROP ||
Laurent Pinchart684c8e22012-04-20 05:47:49 -03001997 sel->pad != PREV_PAD_SINK)
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001998 return -EINVAL;
1999
2000 /* The crop rectangle can't be changed while streaming. */
2001 if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
2002 return -EBUSY;
2003
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002004 /* Modifying the crop rectangle always changes the format on the source
2005 * pad. If the KEEP_CONFIG flag is set, just return the current crop
2006 * rectangle.
2007 */
Sakari Ailus563df3d2012-06-13 16:01:10 -03002008 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002009 sel->r = *__preview_get_crop(prev, fh, sel->which);
2010 return 0;
2011 }
2012
2013 format = __preview_get_format(prev, fh, PREV_PAD_SINK, sel->which);
2014 preview_try_crop(prev, format, &sel->r);
2015 *__preview_get_crop(prev, fh, sel->which) = sel->r;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002016
2017 /* Update the source format. */
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002018 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE, sel->which);
2019 preview_try_format(prev, fh, PREV_PAD_SOURCE, format, sel->which);
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002020
2021 return 0;
2022}
2023
2024/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002025 * preview_get_format - Handle get format by pads subdev method
2026 * @sd : pointer to v4l2 subdev structure
2027 * @fh : V4L2 subdev file handle
2028 * @fmt: pointer to v4l2 subdev format structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002029 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002030 */
2031static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2032 struct v4l2_subdev_format *fmt)
2033{
2034 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2035 struct v4l2_mbus_framefmt *format;
2036
2037 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2038 if (format == NULL)
2039 return -EINVAL;
2040
2041 fmt->format = *format;
2042 return 0;
2043}
2044
2045/*
2046 * preview_set_format - Handle set format by pads subdev method
2047 * @sd : pointer to v4l2 subdev structure
2048 * @fh : V4L2 subdev file handle
2049 * @fmt: pointer to v4l2 subdev format structure
2050 * return -EINVAL or zero on success
2051 */
2052static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
2053 struct v4l2_subdev_format *fmt)
2054{
2055 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2056 struct v4l2_mbus_framefmt *format;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002057 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002058
2059 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
2060 if (format == NULL)
2061 return -EINVAL;
2062
2063 preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
2064 *format = fmt->format;
2065
2066 /* Propagate the format from sink to source */
2067 if (fmt->pad == PREV_PAD_SINK) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03002068 /* Reset the crop rectangle. */
2069 crop = __preview_get_crop(prev, fh, fmt->which);
2070 crop->left = 0;
2071 crop->top = 0;
2072 crop->width = fmt->format.width;
2073 crop->height = fmt->format.height;
2074
2075 preview_try_crop(prev, &fmt->format, crop);
2076
2077 /* Update the source format. */
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002078 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
2079 fmt->which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002080 preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
2081 fmt->which);
2082 }
2083
2084 return 0;
2085}
2086
2087/*
2088 * preview_init_formats - Initialize formats on all pads
2089 * @sd: ISP preview V4L2 subdevice
2090 * @fh: V4L2 subdev file handle
2091 *
2092 * Initialize all pad formats with default values. If fh is not NULL, try
2093 * formats are initialized on the file handle. Otherwise active formats are
2094 * initialized on the device.
2095 */
2096static int preview_init_formats(struct v4l2_subdev *sd,
2097 struct v4l2_subdev_fh *fh)
2098{
2099 struct v4l2_subdev_format format;
2100
2101 memset(&format, 0, sizeof(format));
2102 format.pad = PREV_PAD_SINK;
2103 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
2104 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
2105 format.format.width = 4096;
2106 format.format.height = 4096;
2107 preview_set_format(sd, fh, &format);
2108
2109 return 0;
2110}
2111
2112/* subdev core operations */
2113static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
2114 .ioctl = preview_ioctl,
2115};
2116
2117/* subdev video operations */
2118static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
2119 .s_stream = preview_set_stream,
2120};
2121
2122/* subdev pad operations */
2123static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
2124 .enum_mbus_code = preview_enum_mbus_code,
2125 .enum_frame_size = preview_enum_frame_size,
2126 .get_fmt = preview_get_format,
2127 .set_fmt = preview_set_format,
Laurent Pinchart684c8e22012-04-20 05:47:49 -03002128 .get_selection = preview_get_selection,
2129 .set_selection = preview_set_selection,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002130};
2131
2132/* subdev operations */
2133static const struct v4l2_subdev_ops preview_v4l2_ops = {
2134 .core = &preview_v4l2_core_ops,
2135 .video = &preview_v4l2_video_ops,
2136 .pad = &preview_v4l2_pad_ops,
2137};
2138
2139/* subdev internal operations */
2140static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
2141 .open = preview_init_formats,
2142};
2143
2144/* -----------------------------------------------------------------------------
2145 * Media entity operations
2146 */
2147
2148/*
2149 * preview_link_setup - Setup previewer connections.
2150 * @entity : Pointer to media entity structure
2151 * @local : Pointer to local pad array
2152 * @remote : Pointer to remote pad array
2153 * @flags : Link flags
2154 * return -EINVAL or zero on success
2155 */
2156static int preview_link_setup(struct media_entity *entity,
2157 const struct media_pad *local,
2158 const struct media_pad *remote, u32 flags)
2159{
2160 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2161 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2162
2163 switch (local->index | media_entity_type(remote->entity)) {
2164 case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
2165 /* read from memory */
2166 if (flags & MEDIA_LNK_FL_ENABLED) {
2167 if (prev->input == PREVIEW_INPUT_CCDC)
2168 return -EBUSY;
2169 prev->input = PREVIEW_INPUT_MEMORY;
2170 } else {
2171 if (prev->input == PREVIEW_INPUT_MEMORY)
2172 prev->input = PREVIEW_INPUT_NONE;
2173 }
2174 break;
2175
2176 case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2177 /* read from ccdc */
2178 if (flags & MEDIA_LNK_FL_ENABLED) {
2179 if (prev->input == PREVIEW_INPUT_MEMORY)
2180 return -EBUSY;
2181 prev->input = PREVIEW_INPUT_CCDC;
2182 } else {
2183 if (prev->input == PREVIEW_INPUT_CCDC)
2184 prev->input = PREVIEW_INPUT_NONE;
2185 }
2186 break;
2187
2188 /*
2189 * The ISP core doesn't support pipelines with multiple video outputs.
2190 * Revisit this when it will be implemented, and return -EBUSY for now.
2191 */
2192
2193 case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
2194 /* write to memory */
2195 if (flags & MEDIA_LNK_FL_ENABLED) {
2196 if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
2197 return -EBUSY;
2198 prev->output |= PREVIEW_OUTPUT_MEMORY;
2199 } else {
2200 prev->output &= ~PREVIEW_OUTPUT_MEMORY;
2201 }
2202 break;
2203
2204 case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
2205 /* write to resizer */
2206 if (flags & MEDIA_LNK_FL_ENABLED) {
2207 if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
2208 return -EBUSY;
2209 prev->output |= PREVIEW_OUTPUT_RESIZER;
2210 } else {
2211 prev->output &= ~PREVIEW_OUTPUT_RESIZER;
2212 }
2213 break;
2214
2215 default:
2216 return -EINVAL;
2217 }
2218
2219 return 0;
2220}
2221
2222/* media operations */
2223static const struct media_entity_operations preview_media_ops = {
2224 .link_setup = preview_link_setup,
Sakari Ailus20d4ab72012-01-11 13:27:02 -03002225 .link_validate = v4l2_subdev_link_validate,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002226};
2227
Laurent Pinchart39099d02011-09-22 16:59:26 -03002228void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
2229{
2230 v4l2_device_unregister_subdev(&prev->subdev);
2231 omap3isp_video_unregister(&prev->video_in);
2232 omap3isp_video_unregister(&prev->video_out);
2233}
2234
2235int omap3isp_preview_register_entities(struct isp_prev_device *prev,
2236 struct v4l2_device *vdev)
2237{
2238 int ret;
2239
2240 /* Register the subdev and video nodes. */
2241 ret = v4l2_device_register_subdev(vdev, &prev->subdev);
2242 if (ret < 0)
2243 goto error;
2244
2245 ret = omap3isp_video_register(&prev->video_in, vdev);
2246 if (ret < 0)
2247 goto error;
2248
2249 ret = omap3isp_video_register(&prev->video_out, vdev);
2250 if (ret < 0)
2251 goto error;
2252
2253 return 0;
2254
2255error:
2256 omap3isp_preview_unregister_entities(prev);
2257 return ret;
2258}
2259
2260/* -----------------------------------------------------------------------------
2261 * ISP previewer initialisation and cleanup
2262 */
2263
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002264/*
Laurent Pinchart39099d02011-09-22 16:59:26 -03002265 * preview_init_entities - Initialize subdev and media entity.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002266 * @prev : Pointer to preview structure
2267 * return -ENOMEM or zero on success
2268 */
2269static int preview_init_entities(struct isp_prev_device *prev)
2270{
2271 struct v4l2_subdev *sd = &prev->subdev;
2272 struct media_pad *pads = prev->pads;
2273 struct media_entity *me = &sd->entity;
2274 int ret;
2275
2276 prev->input = PREVIEW_INPUT_NONE;
2277
2278 v4l2_subdev_init(sd, &preview_v4l2_ops);
2279 sd->internal_ops = &preview_v4l2_internal_ops;
2280 strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
2281 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2282 v4l2_set_subdevdata(sd, prev);
2283 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2284
2285 v4l2_ctrl_handler_init(&prev->ctrls, 2);
2286 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
2287 ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
2288 ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
2289 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
2290 ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
2291 ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
2292 v4l2_ctrl_handler_setup(&prev->ctrls);
2293 sd->ctrl_handler = &prev->ctrls;
2294
2295 pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2296 pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2297
2298 me->ops = &preview_media_ops;
2299 ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
2300 if (ret < 0)
2301 return ret;
2302
2303 preview_init_formats(sd, NULL);
2304
2305 /* According to the OMAP34xx TRM, video buffers need to be aligned on a
2306 * 32 bytes boundary. However, an undocumented hardware bug requires a
2307 * 64 bytes boundary at the preview engine input.
2308 */
2309 prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2310 prev->video_in.ops = &preview_video_ops;
2311 prev->video_in.isp = to_isp_device(prev);
2312 prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2313 prev->video_in.bpl_alignment = 64;
2314 prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2315 prev->video_out.ops = &preview_video_ops;
2316 prev->video_out.isp = to_isp_device(prev);
2317 prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2318 prev->video_out.bpl_alignment = 32;
2319
2320 ret = omap3isp_video_init(&prev->video_in, "preview");
2321 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002322 goto error_video_in;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002323
2324 ret = omap3isp_video_init(&prev->video_out, "preview");
2325 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002326 goto error_video_out;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002327
2328 /* Connect the video nodes to the previewer subdev. */
2329 ret = media_entity_create_link(&prev->video_in.video.entity, 0,
2330 &prev->subdev.entity, PREV_PAD_SINK, 0);
2331 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002332 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002333
2334 ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
2335 &prev->video_out.video.entity, 0, 0);
2336 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002337 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002338
2339 return 0;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002340
2341error_link:
2342 omap3isp_video_cleanup(&prev->video_out);
2343error_video_out:
2344 omap3isp_video_cleanup(&prev->video_in);
2345error_video_in:
2346 media_entity_cleanup(&prev->subdev.entity);
2347 return ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002348}
2349
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002350/*
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03002351 * omap3isp_preview_init - Previewer initialization.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002352 * @dev : Pointer to ISP device
2353 * return -ENOMEM or zero on success
2354 */
2355int omap3isp_preview_init(struct isp_device *isp)
2356{
2357 struct isp_prev_device *prev = &isp->isp_prev;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002358
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002359 init_waitqueue_head(&prev->wait);
Laurent Pinchartb0b29e12012-03-26 10:24:50 -03002360
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002361 preview_init_params(prev);
2362
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002363 return preview_init_entities(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002364}
Laurent Pinchart39099d02011-09-22 16:59:26 -03002365
2366void omap3isp_preview_cleanup(struct isp_device *isp)
2367{
2368 struct isp_prev_device *prev = &isp->isp_prev;
2369
2370 v4l2_ctrl_handler_free(&prev->ctrls);
2371 omap3isp_video_cleanup(&prev->video_in);
2372 omap3isp_video_cleanup(&prev->video_out);
2373 media_entity_cleanup(&prev->subdev.entity);
2374}