blob: 755006b946cbc236dda8d05150cae81a15188c79 [file] [log] [blame]
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001/*
2 * isppreview.c
3 *
4 * TI OMAP3 ISP driver - Preview module
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc.
8 *
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 * 02110-1301 USA
25 */
26
27#include <linux/device.h>
28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/mutex.h>
31#include <linux/uaccess.h>
32
33#include "isp.h"
34#include "ispreg.h"
35#include "isppreview.h"
36
Lucas De Marchi25985ed2011-03-30 22:57:33 -030037/* Default values in Office Fluorescent Light for RGBtoRGB Blending */
Laurent Pinchartde1135d2011-02-12 18:05:06 -030038static struct omap3isp_prev_rgbtorgb flr_rgb2rgb = {
39 { /* RGB-RGB Matrix */
40 {0x01E2, 0x0F30, 0x0FEE},
41 {0x0F9B, 0x01AC, 0x0FB9},
42 {0x0FE0, 0x0EC0, 0x0260}
43 }, /* RGB Offset */
44 {0x0000, 0x0000, 0x0000}
45};
46
Lucas De Marchi25985ed2011-03-30 22:57:33 -030047/* Default values in Office Fluorescent Light for RGB to YUV Conversion*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030048static struct omap3isp_prev_csc flr_prev_csc = {
49 { /* CSC Coef Matrix */
50 {66, 129, 25},
51 {-38, -75, 112},
52 {112, -94 , -18}
53 }, /* CSC Offset */
54 {0x0, 0x0, 0x0}
55};
56
Lucas De Marchi25985ed2011-03-30 22:57:33 -030057/* Default values in Office Fluorescent Light for CFA Gradient*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030058#define FLR_CFA_GRADTHRS_HORZ 0x28
59#define FLR_CFA_GRADTHRS_VERT 0x28
60
Lucas De Marchi25985ed2011-03-30 22:57:33 -030061/* Default values in Office Fluorescent Light for Chroma Suppression*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030062#define FLR_CSUP_GAIN 0x0D
63#define FLR_CSUP_THRES 0xEB
64
Lucas De Marchi25985ed2011-03-30 22:57:33 -030065/* Default values in Office Fluorescent Light for Noise Filter*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030066#define FLR_NF_STRGTH 0x03
67
68/* Default values for White Balance */
69#define FLR_WBAL_DGAIN 0x100
70#define FLR_WBAL_COEF 0x20
71
Lucas De Marchi25985ed2011-03-30 22:57:33 -030072/* Default values in Office Fluorescent Light for Black Adjustment*/
Laurent Pinchartde1135d2011-02-12 18:05:06 -030073#define FLR_BLKADJ_BLUE 0x0
74#define FLR_BLKADJ_GREEN 0x0
75#define FLR_BLKADJ_RED 0x0
76
77#define DEF_DETECT_CORRECT_VAL 0xe
78
Laurent Pinchart1f69fd92011-09-21 20:05:45 -030079/*
80 * Margins and image size limits.
81 *
82 * The preview engine crops several rows and columns internally depending on
83 * which filters are enabled. To avoid format changes when the filters are
84 * enabled or disabled (which would prevent them from being turned on or off
85 * during streaming), the driver assumes all the filters are enabled when
86 * computing sink crop and source format limits.
87 *
88 * If a filter is disabled, additional cropping is automatically added at the
89 * preview engine input by the driver to avoid overflow at line and frame end.
90 * This is completely transparent for applications.
91 *
92 * Median filter 4 pixels
93 * Noise filter,
94 * Faulty pixels correction 4 pixels, 4 lines
95 * CFA filter 4 pixels, 4 lines in Bayer mode
96 * 2 lines in other modes
97 * Color suppression 2 pixels
98 * or luma enhancement
99 * -------------------------------------------------------------
100 * Maximum total 14 pixels, 8 lines
101 *
102 * The color suppression and luma enhancement filters are applied after bayer to
103 * YUV conversion. They thus can crop one pixel on the left and one pixel on the
104 * right side of the image without changing the color pattern. When both those
105 * filters are disabled, the driver must crop the two pixels on the same side of
106 * the image to avoid changing the bayer pattern. The left margin is thus set to
107 * 8 pixels and the right margin to 6 pixels.
108 */
109
110#define PREV_MARGIN_LEFT 8
111#define PREV_MARGIN_RIGHT 6
112#define PREV_MARGIN_TOP 4
113#define PREV_MARGIN_BOTTOM 4
114
Laurent Pinchart059dc1d2011-10-03 07:56:15 -0300115#define PREV_MIN_IN_WIDTH 64
116#define PREV_MIN_IN_HEIGHT 8
117#define PREV_MAX_IN_HEIGHT 16384
118
Laurent Pinchartec0cae72011-10-03 07:56:15 -0300119#define PREV_MIN_OUT_WIDTH 0
120#define PREV_MIN_OUT_HEIGHT 0
121#define PREV_MAX_OUT_WIDTH_REV_1 1280
122#define PREV_MAX_OUT_WIDTH_REV_2 3300
123#define PREV_MAX_OUT_WIDTH_REV_15 4096
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300124
125/*
126 * Coeficient Tables for the submodules in Preview.
127 * Array is initialised with the values from.the tables text file.
128 */
129
130/*
131 * CFA Filter Coefficient Table
132 *
133 */
134static u32 cfa_coef_table[] = {
135#include "cfa_coef_table.h"
136};
137
138/*
139 * Default Gamma Correction Table - All components
140 */
141static u32 gamma_table[] = {
142#include "gamma_table.h"
143};
144
145/*
146 * Noise Filter Threshold table
147 */
148static u32 noise_filter_table[] = {
149#include "noise_filter_table.h"
150};
151
152/*
153 * Luminance Enhancement Table
154 */
155static u32 luma_enhance_table[] = {
156#include "luma_enhance_table.h"
157};
158
159/*
160 * preview_enable_invalaw - Enable/Disable Inverse A-Law module in Preview.
161 * @enable: 1 - Reverse the A-Law done in CCDC.
162 */
163static void
164preview_enable_invalaw(struct isp_prev_device *prev, u8 enable)
165{
166 struct isp_device *isp = to_isp_device(prev);
167
168 if (enable)
169 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
170 ISPPRV_PCR_WIDTH | ISPPRV_PCR_INVALAW);
171 else
172 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
173 ISPPRV_PCR_WIDTH | ISPPRV_PCR_INVALAW);
174}
175
176/*
177 * preview_enable_drkframe_capture - Enable/Disable of the darkframe capture.
178 * @prev -
179 * @enable: 1 - Enable, 0 - Disable
180 *
181 * NOTE: PRV_WSDR_ADDR and PRV_WADD_OFFSET must be set also
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300182 * The process is applied for each captured frame.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300183 */
184static void
185preview_enable_drkframe_capture(struct isp_prev_device *prev, u8 enable)
186{
187 struct isp_device *isp = to_isp_device(prev);
188
189 if (enable)
190 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
191 ISPPRV_PCR_DRKFCAP);
192 else
193 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
194 ISPPRV_PCR_DRKFCAP);
195}
196
197/*
198 * preview_enable_drkframe - Enable/Disable of the darkframe subtract.
199 * @enable: 1 - Acquires memory bandwidth since the pixels in each frame is
200 * subtracted with the pixels in the current frame.
201 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300202 * The process is applied for each captured frame.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300203 */
204static void
205preview_enable_drkframe(struct isp_prev_device *prev, u8 enable)
206{
207 struct isp_device *isp = to_isp_device(prev);
208
209 if (enable)
210 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
211 ISPPRV_PCR_DRKFEN);
212 else
213 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
214 ISPPRV_PCR_DRKFEN);
215}
216
217/*
218 * preview_config_drkf_shadcomp - Configures shift value in shading comp.
219 * @scomp_shtval: 3bit value of shift used in shading compensation.
220 */
221static void
222preview_config_drkf_shadcomp(struct isp_prev_device *prev,
223 const void *scomp_shtval)
224{
225 struct isp_device *isp = to_isp_device(prev);
226 const u32 *shtval = scomp_shtval;
227
228 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
229 ISPPRV_PCR_SCOMP_SFT_MASK,
230 *shtval << ISPPRV_PCR_SCOMP_SFT_SHIFT);
231}
232
233/*
234 * preview_enable_hmed - Enables/Disables of the Horizontal Median Filter.
235 * @enable: 1 - Enables Horizontal Median Filter.
236 */
237static void
238preview_enable_hmed(struct isp_prev_device *prev, u8 enable)
239{
240 struct isp_device *isp = to_isp_device(prev);
241
242 if (enable)
243 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
244 ISPPRV_PCR_HMEDEN);
245 else
246 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
247 ISPPRV_PCR_HMEDEN);
248}
249
250/*
251 * preview_config_hmed - Configures the Horizontal Median Filter.
252 * @prev_hmed: Structure containing the odd and even distance between the
253 * pixels in the image along with the filter threshold.
254 */
255static void
256preview_config_hmed(struct isp_prev_device *prev, const void *prev_hmed)
257{
258 struct isp_device *isp = to_isp_device(prev);
259 const struct omap3isp_prev_hmed *hmed = prev_hmed;
260
261 isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
262 (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
263 (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
264 OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
265}
266
267/*
268 * preview_config_noisefilter - Configures the Noise Filter.
269 * @prev_nf: Structure containing the noisefilter table, strength to be used
270 * for the noise filter and the defect correction enable flag.
271 */
272static void
273preview_config_noisefilter(struct isp_prev_device *prev, const void *prev_nf)
274{
275 struct isp_device *isp = to_isp_device(prev);
276 const struct omap3isp_prev_nf *nf = prev_nf;
277 unsigned int i;
278
279 isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
280 isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
281 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
282 for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
283 isp_reg_writel(isp, nf->table[i],
284 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
285 }
286}
287
288/*
289 * preview_config_dcor - Configures the defect correction
290 * @prev_dcor: Structure containing the defect correct thresholds
291 */
292static void
293preview_config_dcor(struct isp_prev_device *prev, const void *prev_dcor)
294{
295 struct isp_device *isp = to_isp_device(prev);
296 const struct omap3isp_prev_dcor *dcor = prev_dcor;
297
298 isp_reg_writel(isp, dcor->detect_correct[0],
299 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
300 isp_reg_writel(isp, dcor->detect_correct[1],
301 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
302 isp_reg_writel(isp, dcor->detect_correct[2],
303 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
304 isp_reg_writel(isp, dcor->detect_correct[3],
305 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
306 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
307 ISPPRV_PCR_DCCOUP,
308 dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
309}
310
311/*
312 * preview_config_cfa - Configures the CFA Interpolation parameters.
313 * @prev_cfa: Structure containing the CFA interpolation table, CFA format
314 * in the image, vertical and horizontal gradient threshold.
315 */
316static void
317preview_config_cfa(struct isp_prev_device *prev, const void *prev_cfa)
318{
319 struct isp_device *isp = to_isp_device(prev);
320 const struct omap3isp_prev_cfa *cfa = prev_cfa;
321 unsigned int i;
322
323 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
324 ISPPRV_PCR_CFAFMT_MASK,
325 cfa->format << ISPPRV_PCR_CFAFMT_SHIFT);
326
327 isp_reg_writel(isp,
328 (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
329 (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
330 OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
331
332 isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
333 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
334
335 for (i = 0; i < OMAP3ISP_PREV_CFA_TBL_SIZE; i++) {
336 isp_reg_writel(isp, cfa->table[i],
337 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
338 }
339}
340
341/*
342 * preview_config_gammacorrn - Configures the Gamma Correction table values
343 * @gtable: Structure containing the table for red, blue, green gamma table.
344 */
345static void
346preview_config_gammacorrn(struct isp_prev_device *prev, const void *gtable)
347{
348 struct isp_device *isp = to_isp_device(prev);
349 const struct omap3isp_prev_gtables *gt = gtable;
350 unsigned int i;
351
352 isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
353 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
354 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
355 isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
356 ISPPRV_SET_TBL_DATA);
357
358 isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
359 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
360 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
361 isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
362 ISPPRV_SET_TBL_DATA);
363
364 isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
365 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
366 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
367 isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
368 ISPPRV_SET_TBL_DATA);
369}
370
371/*
372 * preview_config_luma_enhancement - Sets the Luminance Enhancement table.
373 * @ytable: Structure containing the table for Luminance Enhancement table.
374 */
375static void
376preview_config_luma_enhancement(struct isp_prev_device *prev,
377 const void *ytable)
378{
379 struct isp_device *isp = to_isp_device(prev);
380 const struct omap3isp_prev_luma *yt = ytable;
381 unsigned int i;
382
383 isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
384 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
385 for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
386 isp_reg_writel(isp, yt->table[i],
387 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
388 }
389}
390
391/*
392 * preview_config_chroma_suppression - Configures the Chroma Suppression.
393 * @csup: Structure containing the threshold value for suppression
394 * and the hypass filter enable flag.
395 */
396static void
397preview_config_chroma_suppression(struct isp_prev_device *prev,
398 const void *csup)
399{
400 struct isp_device *isp = to_isp_device(prev);
401 const struct omap3isp_prev_csup *cs = csup;
402
403 isp_reg_writel(isp,
404 cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
405 (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
406 OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
407}
408
409/*
410 * preview_enable_noisefilter - Enables/Disables the Noise Filter.
411 * @enable: 1 - Enables the Noise Filter.
412 */
413static void
414preview_enable_noisefilter(struct isp_prev_device *prev, u8 enable)
415{
416 struct isp_device *isp = to_isp_device(prev);
417
418 if (enable)
419 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
420 ISPPRV_PCR_NFEN);
421 else
422 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
423 ISPPRV_PCR_NFEN);
424}
425
426/*
427 * preview_enable_dcor - Enables/Disables the defect correction.
428 * @enable: 1 - Enables the defect correction.
429 */
430static void
431preview_enable_dcor(struct isp_prev_device *prev, u8 enable)
432{
433 struct isp_device *isp = to_isp_device(prev);
434
435 if (enable)
436 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
437 ISPPRV_PCR_DCOREN);
438 else
439 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
440 ISPPRV_PCR_DCOREN);
441}
442
443/*
444 * preview_enable_cfa - Enable/Disable the CFA Interpolation.
445 * @enable: 1 - Enables the CFA.
446 */
447static void
448preview_enable_cfa(struct isp_prev_device *prev, u8 enable)
449{
450 struct isp_device *isp = to_isp_device(prev);
451
452 if (enable)
453 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
454 ISPPRV_PCR_CFAEN);
455 else
456 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
457 ISPPRV_PCR_CFAEN);
458}
459
460/*
461 * preview_enable_gammabypass - Enables/Disables the GammaByPass
462 * @enable: 1 - Bypasses Gamma - 10bit input is cropped to 8MSB.
463 * 0 - Goes through Gamma Correction. input and output is 10bit.
464 */
465static void
466preview_enable_gammabypass(struct isp_prev_device *prev, u8 enable)
467{
468 struct isp_device *isp = to_isp_device(prev);
469
470 if (enable)
471 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
472 ISPPRV_PCR_GAMMA_BYPASS);
473 else
474 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
475 ISPPRV_PCR_GAMMA_BYPASS);
476}
477
478/*
479 * preview_enable_luma_enhancement - Enables/Disables Luminance Enhancement
480 * @enable: 1 - Enable the Luminance Enhancement.
481 */
482static void
483preview_enable_luma_enhancement(struct isp_prev_device *prev, u8 enable)
484{
485 struct isp_device *isp = to_isp_device(prev);
486
487 if (enable)
488 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
489 ISPPRV_PCR_YNENHEN);
490 else
491 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
492 ISPPRV_PCR_YNENHEN);
493}
494
495/*
496 * preview_enable_chroma_suppression - Enables/Disables Chrominance Suppr.
497 * @enable: 1 - Enable the Chrominance Suppression.
498 */
499static void
500preview_enable_chroma_suppression(struct isp_prev_device *prev, u8 enable)
501{
502 struct isp_device *isp = to_isp_device(prev);
503
504 if (enable)
505 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
506 ISPPRV_PCR_SUPEN);
507 else
508 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
509 ISPPRV_PCR_SUPEN);
510}
511
512/*
513 * preview_config_whitebalance - Configures the White Balance parameters.
514 * @prev_wbal: Structure containing the digital gain and white balance
515 * coefficient.
516 *
517 * Coefficient matrix always with default values.
518 */
519static void
520preview_config_whitebalance(struct isp_prev_device *prev, const void *prev_wbal)
521{
522 struct isp_device *isp = to_isp_device(prev);
523 const struct omap3isp_prev_wbal *wbal = prev_wbal;
524 u32 val;
525
526 isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
527
528 val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
529 val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
530 val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
531 val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
532 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
533
534 isp_reg_writel(isp,
535 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
536 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
537 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
538 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
539 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
540 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
541 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
542 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
543 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
544 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
545 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
546 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
547 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
548 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
549 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
550 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
551 OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
552}
553
554/*
555 * preview_config_blkadj - Configures the Black Adjustment parameters.
556 * @prev_blkadj: Structure containing the black adjustment towards red, green,
557 * blue.
558 */
559static void
560preview_config_blkadj(struct isp_prev_device *prev, const void *prev_blkadj)
561{
562 struct isp_device *isp = to_isp_device(prev);
563 const struct omap3isp_prev_blkadj *blkadj = prev_blkadj;
564
565 isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
566 (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
567 (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
568 OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
569}
570
571/*
572 * preview_config_rgb_blending - Configures the RGB-RGB Blending matrix.
573 * @rgb2rgb: Structure containing the rgb to rgb blending matrix and the rgb
574 * offset.
575 */
576static void
577preview_config_rgb_blending(struct isp_prev_device *prev, const void *rgb2rgb)
578{
579 struct isp_device *isp = to_isp_device(prev);
580 const struct omap3isp_prev_rgbtorgb *rgbrgb = rgb2rgb;
581 u32 val;
582
583 val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
584 val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
585 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
586
587 val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
588 val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
589 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
590
591 val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
592 val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
593 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
594
595 val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
596 val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
597 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
598
599 val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
600 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
601
602 val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
603 val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
604 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
605
606 val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
607 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
608}
609
610/*
611 * Configures the RGB-YCbYCr conversion matrix
612 * @prev_csc: Structure containing the RGB to YCbYCr matrix and the
613 * YCbCr offset.
614 */
615static void
616preview_config_rgb_to_ycbcr(struct isp_prev_device *prev, const void *prev_csc)
617{
618 struct isp_device *isp = to_isp_device(prev);
619 const struct omap3isp_prev_csc *csc = prev_csc;
620 u32 val;
621
622 val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
623 val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
624 val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
625 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
626
627 val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
628 val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
629 val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
630 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
631
632 val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
633 val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
634 val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
635 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
636
637 val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
638 val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
639 val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
640 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
641}
642
643/*
644 * preview_update_contrast - Updates the contrast.
645 * @contrast: Pointer to hold the current programmed contrast value.
646 *
647 * Value should be programmed before enabling the module.
648 */
649static void
650preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
651{
652 struct prev_params *params = &prev->params;
653
654 if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
655 params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300656 prev->update |= OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300657 }
658}
659
660/*
661 * preview_config_contrast - Configures the Contrast.
662 * @params: Contrast value (u8 pointer, U8Q0 format).
663 *
664 * Value should be programmed before enabling the module.
665 */
666static void
667preview_config_contrast(struct isp_prev_device *prev, const void *params)
668{
669 struct isp_device *isp = to_isp_device(prev);
670
671 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
672 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
673 *(u8 *)params << ISPPRV_CNT_BRT_CNT_SHIFT);
674}
675
676/*
677 * preview_update_brightness - Updates the brightness in preview module.
678 * @brightness: Pointer to hold the current programmed brightness value.
679 *
680 */
681static void
682preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
683{
684 struct prev_params *params = &prev->params;
685
686 if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
687 params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300688 prev->update |= OMAP3ISP_PREV_BRIGHTNESS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300689 }
690}
691
692/*
693 * preview_config_brightness - Configures the brightness.
694 * @params: Brightness value (u8 pointer, U8Q0 format).
695 */
696static void
697preview_config_brightness(struct isp_prev_device *prev, const void *params)
698{
699 struct isp_device *isp = to_isp_device(prev);
700
701 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
702 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
703 *(u8 *)params << ISPPRV_CNT_BRT_BRT_SHIFT);
704}
705
706/*
707 * preview_config_yc_range - Configures the max and min Y and C values.
708 * @yclimit: Structure containing the range of Y and C values.
709 */
710static void
711preview_config_yc_range(struct isp_prev_device *prev, const void *yclimit)
712{
713 struct isp_device *isp = to_isp_device(prev);
714 const struct omap3isp_prev_yclimit *yc = yclimit;
715
716 isp_reg_writel(isp,
717 yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
718 yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
719 yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
720 yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
721 OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
722}
723
724/* preview parameters update structure */
725struct preview_update {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300726 void (*config)(struct isp_prev_device *, const void *);
727 void (*enable)(struct isp_prev_device *, u8);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300728 bool skip;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300729};
730
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300731/* Keep the array indexed by the OMAP3ISP_PREV_* bit number. */
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300732static struct preview_update update_attrs[] = {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300733 /* OMAP3ISP_PREV_LUMAENH */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300734 preview_config_luma_enhancement,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300735 preview_enable_luma_enhancement,
736 }, /* OMAP3ISP_PREV_INVALAW */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300737 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300738 preview_enable_invalaw,
739 }, /* OMAP3ISP_PREV_HRZ_MED */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300740 preview_config_hmed,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300741 preview_enable_hmed,
742 }, /* OMAP3ISP_PREV_CFA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300743 preview_config_cfa,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300744 preview_enable_cfa,
745 }, /* OMAP3ISP_PREV_CHROMA_SUPP */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300746 preview_config_chroma_suppression,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300747 preview_enable_chroma_suppression,
748 }, /* OMAP3ISP_PREV_WB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300749 preview_config_whitebalance,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300750 NULL,
751 }, /* OMAP3ISP_PREV_BLKADJ */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300752 preview_config_blkadj,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300753 NULL,
754 }, /* OMAP3ISP_PREV_RGB2RGB */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300755 preview_config_rgb_blending,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300756 NULL,
757 }, /* OMAP3ISP_PREV_COLOR_CONV */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300758 preview_config_rgb_to_ycbcr,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300759 NULL,
760 }, /* OMAP3ISP_PREV_YC_LIMIT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300761 preview_config_yc_range,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300762 NULL,
763 }, /* OMAP3ISP_PREV_DEFECT_COR */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300764 preview_config_dcor,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300765 preview_enable_dcor,
766 }, /* OMAP3ISP_PREV_GAMMABYPASS */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300767 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300768 preview_enable_gammabypass,
769 }, /* OMAP3ISP_PREV_DRK_FRM_CAPTURE */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300770 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300771 preview_enable_drkframe_capture,
772 }, /* OMAP3ISP_PREV_DRK_FRM_SUBTRACT */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300773 NULL,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300774 preview_enable_drkframe,
775 }, /* OMAP3ISP_PREV_LENS_SHADING */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300776 preview_config_drkf_shadcomp,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300777 preview_enable_drkframe,
778 }, /* OMAP3ISP_PREV_NF */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300779 preview_config_noisefilter,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300780 preview_enable_noisefilter,
781 }, /* OMAP3ISP_PREV_GAMMA */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300782 preview_config_gammacorrn,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300783 NULL,
784 }, /* OMAP3ISP_PREV_CONTRAST */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300785 preview_config_contrast,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300786 NULL, true,
787 }, /* OMAP3ISP_PREV_BRIGHTNESS */ {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300788 preview_config_brightness,
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300789 NULL, true,
790 },
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300791};
792
793/*
794 * __preview_get_ptrs - helper function which return pointers to members
795 * of params and config structures.
796 * @params - pointer to preview_params structure.
797 * @param - return pointer to appropriate structure field.
798 * @configs - pointer to update config structure.
799 * @config - return pointer to appropriate structure field.
800 * @bit - for which feature to return pointers.
Michael Jones2d4e9d12011-02-28 08:29:03 -0300801 * Return size of corresponding prev_params member
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300802 */
803static u32
804__preview_get_ptrs(struct prev_params *params, void **param,
805 struct omap3isp_prev_update_config *configs,
806 void __user **config, u32 bit)
807{
808#define CHKARG(cfgs, cfg, field) \
809 if (cfgs && cfg) { \
810 *(cfg) = (cfgs)->field; \
811 }
812
813 switch (bit) {
Laurent Pinchart3108e022012-04-05 12:38:23 -0300814 case OMAP3ISP_PREV_HRZ_MED:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300815 *param = &params->hmed;
816 CHKARG(configs, config, hmed)
817 return sizeof(params->hmed);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300818 case OMAP3ISP_PREV_NF:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300819 *param = &params->nf;
820 CHKARG(configs, config, nf)
821 return sizeof(params->nf);
822 break;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300823 case OMAP3ISP_PREV_CFA:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300824 *param = &params->cfa;
825 CHKARG(configs, config, cfa)
826 return sizeof(params->cfa);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300827 case OMAP3ISP_PREV_LUMAENH:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300828 *param = &params->luma;
829 CHKARG(configs, config, luma)
830 return sizeof(params->luma);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300831 case OMAP3ISP_PREV_CHROMA_SUPP:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300832 *param = &params->csup;
833 CHKARG(configs, config, csup)
834 return sizeof(params->csup);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300835 case OMAP3ISP_PREV_DEFECT_COR:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300836 *param = &params->dcor;
837 CHKARG(configs, config, dcor)
838 return sizeof(params->dcor);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300839 case OMAP3ISP_PREV_BLKADJ:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300840 *param = &params->blk_adj;
841 CHKARG(configs, config, blkadj)
842 return sizeof(params->blk_adj);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300843 case OMAP3ISP_PREV_YC_LIMIT:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300844 *param = &params->yclimit;
845 CHKARG(configs, config, yclimit)
846 return sizeof(params->yclimit);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300847 case OMAP3ISP_PREV_RGB2RGB:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300848 *param = &params->rgb2rgb;
849 CHKARG(configs, config, rgb2rgb)
850 return sizeof(params->rgb2rgb);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300851 case OMAP3ISP_PREV_COLOR_CONV:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300852 *param = &params->rgb2ycbcr;
853 CHKARG(configs, config, csc)
854 return sizeof(params->rgb2ycbcr);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300855 case OMAP3ISP_PREV_WB:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300856 *param = &params->wbal;
857 CHKARG(configs, config, wbal)
858 return sizeof(params->wbal);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300859 case OMAP3ISP_PREV_GAMMA:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300860 *param = &params->gamma;
861 CHKARG(configs, config, gamma)
862 return sizeof(params->gamma);
Laurent Pinchart3108e022012-04-05 12:38:23 -0300863 case OMAP3ISP_PREV_CONTRAST:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300864 *param = &params->contrast;
865 return 0;
Laurent Pinchart3108e022012-04-05 12:38:23 -0300866 case OMAP3ISP_PREV_BRIGHTNESS:
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300867 *param = &params->brightness;
868 return 0;
869 default:
870 *param = NULL;
871 *config = NULL;
872 break;
873 }
874 return 0;
875}
876
877/*
878 * preview_config - Copy and update local structure with userspace preview
879 * configuration.
880 * @prev: ISP preview engine
881 * @cfg: Configuration
882 *
883 * Return zero if success or -EFAULT if the configuration can't be copied from
884 * userspace.
885 */
886static int preview_config(struct isp_prev_device *prev,
887 struct omap3isp_prev_update_config *cfg)
888{
889 struct prev_params *params;
890 struct preview_update *attr;
891 int i, bit, rval = 0;
892
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300893 if (cfg->update == 0)
894 return 0;
895
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300896 params = &prev->params;
897
898 if (prev->state != ISP_PIPELINE_STREAM_STOPPED) {
899 unsigned long flags;
900
901 spin_lock_irqsave(&prev->lock, flags);
902 prev->shadow_update = 1;
903 spin_unlock_irqrestore(&prev->lock, flags);
904 }
905
906 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
907 attr = &update_attrs[i];
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300908 bit = 1 << i;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300909
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300910 if (attr->skip || !(cfg->update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300911 continue;
912
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300913 if (cfg->flag & bit) {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300914 void *to = NULL, __user *from = NULL;
915 unsigned long sz = 0;
916
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300917 sz = __preview_get_ptrs(params, &to, cfg, &from, bit);
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300918 if (to && from && sz) {
919 if (copy_from_user(to, from, sz)) {
920 rval = -EFAULT;
921 break;
922 }
923 }
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300924 params->features |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300925 } else {
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300926 params->features &= ~bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300927 }
928
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300929 prev->update |= bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300930 }
931
932 prev->shadow_update = 0;
933 return rval;
934}
935
936/*
937 * preview_setup_hw - Setup preview registers and/or internal memory
938 * @prev: pointer to preview private structure
939 * Note: can be called from interrupt context
940 * Return none
941 */
942static void preview_setup_hw(struct isp_prev_device *prev)
943{
944 struct prev_params *params = &prev->params;
945 struct preview_update *attr;
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300946 unsigned int bit;
947 unsigned int i;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300948 void *param_ptr;
949
Laurent Pinchartf22926e2012-03-26 10:24:50 -0300950 if (prev->update == 0)
951 return;
952
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300953 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
954 attr = &update_attrs[i];
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300955 bit = 1 << i;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300956
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300957 if (!(prev->update & bit))
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300958 continue;
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300959
960 if (params->features & bit) {
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300961 if (attr->config) {
962 __preview_get_ptrs(params, &param_ptr, NULL,
963 NULL, bit);
964 attr->config(prev, param_ptr);
965 }
966 if (attr->enable)
967 attr->enable(prev, 1);
968 } else
969 if (attr->enable)
970 attr->enable(prev, 0);
971
Laurent Pinchart7ed5de92012-04-05 13:51:17 -0300972 prev->update &= ~bit;
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300973 }
974}
975
976/*
977 * preview_config_ycpos - Configure byte layout of YUV image.
978 * @mode: Indicates the required byte layout.
979 */
980static void
981preview_config_ycpos(struct isp_prev_device *prev,
982 enum v4l2_mbus_pixelcode pixelcode)
983{
984 struct isp_device *isp = to_isp_device(prev);
985 enum preview_ycpos_mode mode;
986
987 switch (pixelcode) {
988 case V4L2_MBUS_FMT_YUYV8_1X16:
989 mode = YCPOS_CrYCbY;
990 break;
991 case V4L2_MBUS_FMT_UYVY8_1X16:
992 mode = YCPOS_YCrYCb;
993 break;
994 default:
995 return;
996 }
997
998 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
999 ISPPRV_PCR_YCPOS_CrYCbY,
1000 mode << ISPPRV_PCR_YCPOS_SHIFT);
1001}
1002
1003/*
1004 * preview_config_averager - Enable / disable / configure averager
1005 * @average: Average value to be configured.
1006 */
1007static void preview_config_averager(struct isp_prev_device *prev, u8 average)
1008{
1009 struct isp_device *isp = to_isp_device(prev);
1010 int reg = 0;
1011
1012 if (prev->params.cfa.format == OMAP3ISP_CFAFMT_BAYER)
1013 reg = ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
1014 ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
1015 average;
1016 else if (prev->params.cfa.format == OMAP3ISP_CFAFMT_RGBFOVEON)
1017 reg = ISPPRV_AVE_EVENDIST_3 << ISPPRV_AVE_EVENDIST_SHIFT |
1018 ISPPRV_AVE_ODDDIST_3 << ISPPRV_AVE_ODDDIST_SHIFT |
1019 average;
1020 isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
1021}
1022
1023/*
1024 * preview_config_input_size - Configure the input frame size
1025 *
1026 * The preview engine crops several rows and columns internally depending on
1027 * which processing blocks are enabled. The driver assumes all those blocks are
1028 * enabled when reporting source pad formats to userspace. If this assumption is
1029 * not true, rows and columns must be manually cropped at the preview engine
1030 * input to avoid overflows at the end of lines and frames.
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001031 *
1032 * See the explanation at the PREV_MARGIN_* definitions for more details.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001033 */
1034static void preview_config_input_size(struct isp_prev_device *prev)
1035{
1036 struct isp_device *isp = to_isp_device(prev);
1037 struct prev_params *params = &prev->params;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001038 unsigned int sph = prev->crop.left;
1039 unsigned int eph = prev->crop.left + prev->crop.width - 1;
1040 unsigned int slv = prev->crop.top;
1041 unsigned int elv = prev->crop.top + prev->crop.height - 1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001042
Laurent Pinchart3108e022012-04-05 12:38:23 -03001043 if (params->features & OMAP3ISP_PREV_CFA) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001044 sph -= 2;
1045 eph += 2;
1046 slv -= 2;
1047 elv += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001048 }
Laurent Pinchart3108e022012-04-05 12:38:23 -03001049 if (params->features & (OMAP3ISP_PREV_DEFECT_COR | OMAP3ISP_PREV_NF)) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001050 sph -= 2;
1051 eph += 2;
1052 slv -= 2;
1053 elv += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001054 }
Laurent Pinchart3108e022012-04-05 12:38:23 -03001055 if (params->features & OMAP3ISP_PREV_HRZ_MED) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001056 sph -= 2;
1057 eph += 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001058 }
Laurent Pinchart3108e022012-04-05 12:38:23 -03001059 if (params->features & (OMAP3ISP_PREV_CHROMA_SUPP |
1060 OMAP3ISP_PREV_LUMAENH))
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001061 sph -= 2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001062
1063 isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
1064 OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
1065 isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
1066 OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
1067}
1068
1069/*
1070 * preview_config_inlineoffset - Configures the Read address line offset.
1071 * @prev: Preview module
1072 * @offset: Line offset
1073 *
1074 * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
1075 * However, a hardware bug requires the memory start address to be aligned on a
1076 * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
1077 * well.
1078 */
1079static void
1080preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
1081{
1082 struct isp_device *isp = to_isp_device(prev);
1083
1084 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1085 ISPPRV_RADR_OFFSET);
1086}
1087
1088/*
1089 * preview_set_inaddr - Sets memory address of input frame.
1090 * @addr: 32bit memory address aligned on 32byte boundary.
1091 *
1092 * Configures the memory address from which the input frame is to be read.
1093 */
1094static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
1095{
1096 struct isp_device *isp = to_isp_device(prev);
1097
1098 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
1099}
1100
1101/*
1102 * preview_config_outlineoffset - Configures the Write address line offset.
1103 * @offset: Line Offset for the preview output.
1104 *
1105 * The offset must be a multiple of 32 bytes.
1106 */
1107static void preview_config_outlineoffset(struct isp_prev_device *prev,
1108 u32 offset)
1109{
1110 struct isp_device *isp = to_isp_device(prev);
1111
1112 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1113 ISPPRV_WADD_OFFSET);
1114}
1115
1116/*
1117 * preview_set_outaddr - Sets the memory address to store output frame
1118 * @addr: 32bit memory address aligned on 32byte boundary.
1119 *
1120 * Configures the memory address to which the output frame is written.
1121 */
1122static void preview_set_outaddr(struct isp_prev_device *prev, u32 addr)
1123{
1124 struct isp_device *isp = to_isp_device(prev);
1125
1126 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_WSDR_ADDR);
1127}
1128
1129static void preview_adjust_bandwidth(struct isp_prev_device *prev)
1130{
1131 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1132 struct isp_device *isp = to_isp_device(prev);
1133 const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
1134 unsigned long l3_ick = pipe->l3_ick;
1135 struct v4l2_fract *timeperframe;
1136 unsigned int cycles_per_frame;
1137 unsigned int requests_per_frame;
1138 unsigned int cycles_per_request;
1139 unsigned int minimum;
1140 unsigned int maximum;
1141 unsigned int value;
1142
1143 if (prev->input != PREVIEW_INPUT_MEMORY) {
1144 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1145 ISPSBL_SDR_REQ_PRV_EXP_MASK);
1146 return;
1147 }
1148
1149 /* Compute the minimum number of cycles per request, based on the
1150 * pipeline maximum data rate. This is an absolute lower bound if we
1151 * don't want SBL overflows, so round the value up.
1152 */
1153 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
1154 pipe->max_rate);
1155 minimum = DIV_ROUND_UP(cycles_per_request, 32);
1156
1157 /* Compute the maximum number of cycles per request, based on the
1158 * requested frame rate. This is a soft upper bound to achieve a frame
1159 * rate equal or higher than the requested value, so round the value
1160 * down.
1161 */
1162 timeperframe = &pipe->max_timeperframe;
1163
1164 requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
1165 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
1166 timeperframe->denominator);
1167 cycles_per_request = cycles_per_frame / requests_per_frame;
1168
1169 maximum = cycles_per_request / 32;
1170
1171 value = max(minimum, maximum);
1172
1173 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
1174 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1175 ISPSBL_SDR_REQ_PRV_EXP_MASK,
1176 value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
1177}
1178
1179/*
1180 * omap3isp_preview_busy - Gets busy state of preview module.
1181 */
1182int omap3isp_preview_busy(struct isp_prev_device *prev)
1183{
1184 struct isp_device *isp = to_isp_device(prev);
1185
1186 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
1187 & ISPPRV_PCR_BUSY;
1188}
1189
1190/*
1191 * omap3isp_preview_restore_context - Restores the values of preview registers
1192 */
1193void omap3isp_preview_restore_context(struct isp_device *isp)
1194{
Laurent Pinchart3108e022012-04-05 12:38:23 -03001195 isp->isp_prev.update = OMAP3ISP_PREV_FEATURES_END - 1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001196 preview_setup_hw(&isp->isp_prev);
1197}
1198
1199/*
1200 * preview_print_status - Dump preview module registers to the kernel log
1201 */
1202#define PREV_PRINT_REGISTER(isp, name)\
1203 dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
1204 isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
1205
1206static void preview_print_status(struct isp_prev_device *prev)
1207{
1208 struct isp_device *isp = to_isp_device(prev);
1209
1210 dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
1211
1212 PREV_PRINT_REGISTER(isp, PCR);
1213 PREV_PRINT_REGISTER(isp, HORZ_INFO);
1214 PREV_PRINT_REGISTER(isp, VERT_INFO);
1215 PREV_PRINT_REGISTER(isp, RSDR_ADDR);
1216 PREV_PRINT_REGISTER(isp, RADR_OFFSET);
1217 PREV_PRINT_REGISTER(isp, DSDR_ADDR);
1218 PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
1219 PREV_PRINT_REGISTER(isp, WSDR_ADDR);
1220 PREV_PRINT_REGISTER(isp, WADD_OFFSET);
1221 PREV_PRINT_REGISTER(isp, AVE);
1222 PREV_PRINT_REGISTER(isp, HMED);
1223 PREV_PRINT_REGISTER(isp, NF);
1224 PREV_PRINT_REGISTER(isp, WB_DGAIN);
1225 PREV_PRINT_REGISTER(isp, WBGAIN);
1226 PREV_PRINT_REGISTER(isp, WBSEL);
1227 PREV_PRINT_REGISTER(isp, CFA);
1228 PREV_PRINT_REGISTER(isp, BLKADJOFF);
1229 PREV_PRINT_REGISTER(isp, RGB_MAT1);
1230 PREV_PRINT_REGISTER(isp, RGB_MAT2);
1231 PREV_PRINT_REGISTER(isp, RGB_MAT3);
1232 PREV_PRINT_REGISTER(isp, RGB_MAT4);
1233 PREV_PRINT_REGISTER(isp, RGB_MAT5);
1234 PREV_PRINT_REGISTER(isp, RGB_OFF1);
1235 PREV_PRINT_REGISTER(isp, RGB_OFF2);
1236 PREV_PRINT_REGISTER(isp, CSC0);
1237 PREV_PRINT_REGISTER(isp, CSC1);
1238 PREV_PRINT_REGISTER(isp, CSC2);
1239 PREV_PRINT_REGISTER(isp, CSC_OFFSET);
1240 PREV_PRINT_REGISTER(isp, CNT_BRT);
1241 PREV_PRINT_REGISTER(isp, CSUP);
1242 PREV_PRINT_REGISTER(isp, SETUP_YC);
1243 PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
1244 PREV_PRINT_REGISTER(isp, CDC_THR0);
1245 PREV_PRINT_REGISTER(isp, CDC_THR1);
1246 PREV_PRINT_REGISTER(isp, CDC_THR2);
1247 PREV_PRINT_REGISTER(isp, CDC_THR3);
1248
1249 dev_dbg(isp->dev, "--------------------------------------------\n");
1250}
1251
1252/*
1253 * preview_init_params - init image processing parameters.
1254 * @prev: pointer to previewer private structure
1255 * return none
1256 */
1257static void preview_init_params(struct isp_prev_device *prev)
1258{
1259 struct prev_params *params = &prev->params;
1260 int i = 0;
1261
1262 /* Init values */
1263 params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
1264 params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001265 params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
1266 memcpy(params->cfa.table, cfa_coef_table,
1267 sizeof(params->cfa.table));
1268 params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
1269 params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
1270 params->csup.gain = FLR_CSUP_GAIN;
1271 params->csup.thres = FLR_CSUP_THRES;
1272 params->csup.hypf_en = 0;
1273 memcpy(params->luma.table, luma_enhance_table,
1274 sizeof(params->luma.table));
1275 params->nf.spread = FLR_NF_STRGTH;
1276 memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
1277 params->dcor.couplet_mode_en = 1;
1278 for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
1279 params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
1280 memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
1281 memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
1282 memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
1283 params->wbal.dgain = FLR_WBAL_DGAIN;
1284 params->wbal.coef0 = FLR_WBAL_COEF;
1285 params->wbal.coef1 = FLR_WBAL_COEF;
1286 params->wbal.coef2 = FLR_WBAL_COEF;
1287 params->wbal.coef3 = FLR_WBAL_COEF;
1288 params->blk_adj.red = FLR_BLKADJ_RED;
1289 params->blk_adj.green = FLR_BLKADJ_GREEN;
1290 params->blk_adj.blue = FLR_BLKADJ_BLUE;
1291 params->rgb2rgb = flr_rgb2rgb;
1292 params->rgb2ycbcr = flr_prev_csc;
1293 params->yclimit.minC = ISPPRV_YC_MIN;
1294 params->yclimit.maxC = ISPPRV_YC_MAX;
1295 params->yclimit.minY = ISPPRV_YC_MIN;
1296 params->yclimit.maxY = ISPPRV_YC_MAX;
1297
Laurent Pinchart3108e022012-04-05 12:38:23 -03001298 params->features = OMAP3ISP_PREV_CFA | OMAP3ISP_PREV_DEFECT_COR
1299 | OMAP3ISP_PREV_NF | OMAP3ISP_PREV_GAMMA
1300 | OMAP3ISP_PREV_BLKADJ | OMAP3ISP_PREV_YC_LIMIT
1301 | OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV
1302 | OMAP3ISP_PREV_WB | OMAP3ISP_PREV_BRIGHTNESS
1303 | OMAP3ISP_PREV_CONTRAST;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001304
Laurent Pinchart3108e022012-04-05 12:38:23 -03001305 prev->update = OMAP3ISP_PREV_FEATURES_END - 1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001306}
1307
1308/*
1309 * preview_max_out_width - Handle previewer hardware ouput limitations
1310 * @isp_revision : ISP revision
1311 * returns maximum width output for current isp revision
1312 */
1313static unsigned int preview_max_out_width(struct isp_prev_device *prev)
1314{
1315 struct isp_device *isp = to_isp_device(prev);
1316
1317 switch (isp->revision) {
1318 case ISP_REVISION_1_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001319 return PREV_MAX_OUT_WIDTH_REV_1;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001320
1321 case ISP_REVISION_2_0:
1322 default:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001323 return PREV_MAX_OUT_WIDTH_REV_2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001324
1325 case ISP_REVISION_15_0:
Laurent Pinchartec0cae72011-10-03 07:56:15 -03001326 return PREV_MAX_OUT_WIDTH_REV_15;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001327 }
1328}
1329
1330static void preview_configure(struct isp_prev_device *prev)
1331{
1332 struct isp_device *isp = to_isp_device(prev);
1333 struct v4l2_mbus_framefmt *format;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001334
1335 preview_setup_hw(prev);
1336
1337 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1338 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1339 ISPPRV_PCR_SDRPORT);
1340 else
1341 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1342 ISPPRV_PCR_SDRPORT);
1343
1344 if (prev->output & PREVIEW_OUTPUT_RESIZER)
1345 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1346 ISPPRV_PCR_RSZPORT);
1347 else
1348 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1349 ISPPRV_PCR_RSZPORT);
1350
1351 /* PREV_PAD_SINK */
1352 format = &prev->formats[PREV_PAD_SINK];
1353
1354 preview_adjust_bandwidth(prev);
1355
1356 preview_config_input_size(prev);
1357
1358 if (prev->input == PREVIEW_INPUT_CCDC)
1359 preview_config_inlineoffset(prev, 0);
1360 else
1361 preview_config_inlineoffset(prev,
1362 ALIGN(format->width, 0x20) * 2);
1363
1364 /* PREV_PAD_SOURCE */
1365 format = &prev->formats[PREV_PAD_SOURCE];
1366
1367 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1368 preview_config_outlineoffset(prev,
1369 ALIGN(format->width, 0x10) * 2);
1370
Laurent Pincharte4bc6272011-09-21 07:54:44 -03001371 preview_config_averager(prev, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001372 preview_config_ycpos(prev, format->code);
1373}
1374
1375/* -----------------------------------------------------------------------------
1376 * Interrupt handling
1377 */
1378
1379static void preview_enable_oneshot(struct isp_prev_device *prev)
1380{
1381 struct isp_device *isp = to_isp_device(prev);
1382
1383 /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
1384 * bit is set. As the preview engine is used in single-shot mode, we
1385 * need to set PCR.SOURCE before enabling the preview engine.
1386 */
1387 if (prev->input == PREVIEW_INPUT_MEMORY)
1388 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1389 ISPPRV_PCR_SOURCE);
1390
1391 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1392 ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
1393}
1394
1395void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
1396{
1397 /*
1398 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1399 * condition, the module was paused and now we have a buffer queued
1400 * on the output again. Restart the pipeline if running in continuous
1401 * mode.
1402 */
1403 if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1404 prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1405 preview_enable_oneshot(prev);
1406 isp_video_dmaqueue_flags_clr(&prev->video_out);
1407 }
1408}
1409
1410static void preview_isr_buffer(struct isp_prev_device *prev)
1411{
1412 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1413 struct isp_buffer *buffer;
1414 int restart = 0;
1415
1416 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001417 buffer = omap3isp_video_buffer_next(&prev->video_in);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001418 if (buffer != NULL)
1419 preview_set_inaddr(prev, buffer->isp_addr);
1420 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1421 }
1422
1423 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
Laurent Pinchart875e2e32011-12-07 08:34:50 -03001424 buffer = omap3isp_video_buffer_next(&prev->video_out);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001425 if (buffer != NULL) {
1426 preview_set_outaddr(prev, buffer->isp_addr);
1427 restart = 1;
1428 }
1429 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1430 }
1431
1432 switch (prev->state) {
1433 case ISP_PIPELINE_STREAM_SINGLESHOT:
1434 if (isp_pipeline_ready(pipe))
1435 omap3isp_pipeline_set_stream(pipe,
1436 ISP_PIPELINE_STREAM_SINGLESHOT);
1437 break;
1438
1439 case ISP_PIPELINE_STREAM_CONTINUOUS:
1440 /* If an underrun occurs, the video queue operation handler will
1441 * restart the preview engine. Otherwise restart it immediately.
1442 */
1443 if (restart)
1444 preview_enable_oneshot(prev);
1445 break;
1446
1447 case ISP_PIPELINE_STREAM_STOPPED:
1448 default:
1449 return;
1450 }
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001451}
1452
1453/*
1454 * omap3isp_preview_isr - ISP preview engine interrupt handler
1455 *
1456 * Manage the preview engine video buffers and configure shadowed registers.
1457 */
1458void omap3isp_preview_isr(struct isp_prev_device *prev)
1459{
1460 unsigned long flags;
1461
1462 if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
1463 return;
1464
1465 spin_lock_irqsave(&prev->lock, flags);
1466 if (prev->shadow_update)
1467 goto done;
1468
1469 preview_setup_hw(prev);
1470 preview_config_input_size(prev);
1471
1472done:
1473 spin_unlock_irqrestore(&prev->lock, flags);
1474
1475 if (prev->input == PREVIEW_INPUT_MEMORY ||
1476 prev->output & PREVIEW_OUTPUT_MEMORY)
1477 preview_isr_buffer(prev);
1478 else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1479 preview_enable_oneshot(prev);
1480}
1481
1482/* -----------------------------------------------------------------------------
1483 * ISP video operations
1484 */
1485
1486static int preview_video_queue(struct isp_video *video,
1487 struct isp_buffer *buffer)
1488{
1489 struct isp_prev_device *prev = &video->isp->isp_prev;
1490
1491 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1492 preview_set_inaddr(prev, buffer->isp_addr);
1493
1494 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1495 preview_set_outaddr(prev, buffer->isp_addr);
1496
1497 return 0;
1498}
1499
1500static const struct isp_video_operations preview_video_ops = {
1501 .queue = preview_video_queue,
1502};
1503
1504/* -----------------------------------------------------------------------------
1505 * V4L2 subdev operations
1506 */
1507
1508/*
1509 * preview_s_ctrl - Handle set control subdev method
1510 * @ctrl: pointer to v4l2 control structure
1511 */
1512static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
1513{
1514 struct isp_prev_device *prev =
1515 container_of(ctrl->handler, struct isp_prev_device, ctrls);
1516
1517 switch (ctrl->id) {
1518 case V4L2_CID_BRIGHTNESS:
1519 preview_update_brightness(prev, ctrl->val);
1520 break;
1521 case V4L2_CID_CONTRAST:
1522 preview_update_contrast(prev, ctrl->val);
1523 break;
1524 }
1525
1526 return 0;
1527}
1528
1529static const struct v4l2_ctrl_ops preview_ctrl_ops = {
1530 .s_ctrl = preview_s_ctrl,
1531};
1532
1533/*
1534 * preview_ioctl - Handle preview module private ioctl's
1535 * @prev: pointer to preview context structure
1536 * @cmd: configuration command
1537 * @arg: configuration argument
1538 * return -EINVAL or zero on success
1539 */
1540static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1541{
1542 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1543
1544 switch (cmd) {
1545 case VIDIOC_OMAP3ISP_PRV_CFG:
1546 return preview_config(prev, arg);
1547
1548 default:
1549 return -ENOIOCTLCMD;
1550 }
1551}
1552
1553/*
1554 * preview_set_stream - Enable/Disable streaming on preview subdev
1555 * @sd : pointer to v4l2 subdev structure
1556 * @enable: 1 == Enable, 0 == Disable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001557 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001558 */
1559static int preview_set_stream(struct v4l2_subdev *sd, int enable)
1560{
1561 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1562 struct isp_video *video_out = &prev->video_out;
1563 struct isp_device *isp = to_isp_device(prev);
1564 struct device *dev = to_device(prev);
1565 unsigned long flags;
1566
1567 if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
1568 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1569 return 0;
1570
1571 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1572 preview_configure(prev);
1573 atomic_set(&prev->stopping, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001574 preview_print_status(prev);
1575 }
1576
1577 switch (enable) {
1578 case ISP_PIPELINE_STREAM_CONTINUOUS:
1579 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1580 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1581
1582 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
1583 !(prev->output & PREVIEW_OUTPUT_MEMORY))
1584 preview_enable_oneshot(prev);
1585
1586 isp_video_dmaqueue_flags_clr(video_out);
1587 break;
1588
1589 case ISP_PIPELINE_STREAM_SINGLESHOT:
1590 if (prev->input == PREVIEW_INPUT_MEMORY)
1591 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1592 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1593 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1594
1595 preview_enable_oneshot(prev);
1596 break;
1597
1598 case ISP_PIPELINE_STREAM_STOPPED:
1599 if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
1600 &prev->stopping))
1601 dev_dbg(dev, "%s: stop timeout.\n", sd->name);
1602 spin_lock_irqsave(&prev->lock, flags);
1603 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1604 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1605 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1606 spin_unlock_irqrestore(&prev->lock, flags);
1607 isp_video_dmaqueue_flags_clr(video_out);
1608 break;
1609 }
1610
1611 prev->state = enable;
1612 return 0;
1613}
1614
1615static struct v4l2_mbus_framefmt *
1616__preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1617 unsigned int pad, enum v4l2_subdev_format_whence which)
1618{
1619 if (which == V4L2_SUBDEV_FORMAT_TRY)
1620 return v4l2_subdev_get_try_format(fh, pad);
1621 else
1622 return &prev->formats[pad];
1623}
1624
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001625static struct v4l2_rect *
1626__preview_get_crop(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1627 enum v4l2_subdev_format_whence which)
1628{
1629 if (which == V4L2_SUBDEV_FORMAT_TRY)
1630 return v4l2_subdev_get_try_crop(fh, PREV_PAD_SINK);
1631 else
1632 return &prev->crop;
1633}
1634
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001635/* previewer format descriptions */
1636static const unsigned int preview_input_fmts[] = {
1637 V4L2_MBUS_FMT_SGRBG10_1X10,
1638 V4L2_MBUS_FMT_SRGGB10_1X10,
1639 V4L2_MBUS_FMT_SBGGR10_1X10,
1640 V4L2_MBUS_FMT_SGBRG10_1X10,
1641};
1642
1643static const unsigned int preview_output_fmts[] = {
1644 V4L2_MBUS_FMT_UYVY8_1X16,
1645 V4L2_MBUS_FMT_YUYV8_1X16,
1646};
1647
1648/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001649 * preview_try_format - Validate a format
1650 * @prev: ISP preview engine
1651 * @fh: V4L2 subdev file handle
1652 * @pad: pad number
1653 * @fmt: format to be validated
1654 * @which: try/active format selector
1655 *
1656 * Validate and adjust the given format for the given pad based on the preview
1657 * engine limits and the format and crop rectangles on other pads.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001658 */
1659static void preview_try_format(struct isp_prev_device *prev,
1660 struct v4l2_subdev_fh *fh, unsigned int pad,
1661 struct v4l2_mbus_framefmt *fmt,
1662 enum v4l2_subdev_format_whence which)
1663{
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001664 enum v4l2_mbus_pixelcode pixelcode;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001665 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001666 unsigned int i;
1667
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001668 switch (pad) {
1669 case PREV_PAD_SINK:
1670 /* When reading data from the CCDC, the input size has already
1671 * been mangled by the CCDC output pad so it can be accepted
1672 * as-is.
1673 *
1674 * When reading data from memory, clamp the requested width and
1675 * height. The TRM doesn't specify a minimum input height, make
1676 * sure we got enough lines to enable the noise filter and color
1677 * filter array interpolation.
1678 */
1679 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001680 fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
1681 preview_max_out_width(prev));
1682 fmt->height = clamp_t(u32, fmt->height,
1683 PREV_MIN_IN_HEIGHT,
1684 PREV_MAX_IN_HEIGHT);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001685 }
1686
1687 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1688
1689 for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
1690 if (fmt->code == preview_input_fmts[i])
1691 break;
1692 }
1693
1694 /* If not found, use SGRBG10 as default */
1695 if (i >= ARRAY_SIZE(preview_input_fmts))
1696 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1697 break;
1698
1699 case PREV_PAD_SOURCE:
1700 pixelcode = fmt->code;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001701 *fmt = *__preview_get_format(prev, fh, PREV_PAD_SINK, which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001702
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001703 switch (pixelcode) {
1704 case V4L2_MBUS_FMT_YUYV8_1X16:
1705 case V4L2_MBUS_FMT_UYVY8_1X16:
1706 fmt->code = pixelcode;
1707 break;
1708
1709 default:
1710 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1711 break;
1712 }
1713
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001714 /* The preview module output size is configurable through the
1715 * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). This
1716 * is not supported yet, hardcode the output size to the crop
1717 * rectangle size.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001718 */
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001719 crop = __preview_get_crop(prev, fh, which);
1720 fmt->width = crop->width;
1721 fmt->height = crop->height;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001722
1723 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1724 break;
1725 }
1726
1727 fmt->field = V4L2_FIELD_NONE;
1728}
1729
1730/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001731 * preview_try_crop - Validate a crop rectangle
1732 * @prev: ISP preview engine
1733 * @sink: format on the sink pad
1734 * @crop: crop rectangle to be validated
1735 *
1736 * The preview engine crops lines and columns for its internal operation,
1737 * depending on which filters are enabled. Enforce minimum crop margins to
1738 * handle that transparently for userspace.
1739 *
1740 * See the explanation at the PREV_MARGIN_* definitions for more details.
1741 */
1742static void preview_try_crop(struct isp_prev_device *prev,
1743 const struct v4l2_mbus_framefmt *sink,
1744 struct v4l2_rect *crop)
1745{
1746 unsigned int left = PREV_MARGIN_LEFT;
1747 unsigned int right = sink->width - PREV_MARGIN_RIGHT;
1748 unsigned int top = PREV_MARGIN_TOP;
1749 unsigned int bottom = sink->height - PREV_MARGIN_BOTTOM;
1750
1751 /* When processing data on-the-fly from the CCDC, at least 2 pixels must
1752 * be cropped from the left and right sides of the image. As we don't
1753 * know which filters will be enabled, increase the left and right
1754 * margins by two.
1755 */
1756 if (prev->input == PREVIEW_INPUT_CCDC) {
1757 left += 2;
1758 right -= 2;
1759 }
1760
1761 /* Restrict left/top to even values to keep the Bayer pattern. */
1762 crop->left &= ~1;
1763 crop->top &= ~1;
1764
1765 crop->left = clamp_t(u32, crop->left, left, right - PREV_MIN_OUT_WIDTH);
1766 crop->top = clamp_t(u32, crop->top, top, bottom - PREV_MIN_OUT_HEIGHT);
1767 crop->width = clamp_t(u32, crop->width, PREV_MIN_OUT_WIDTH,
1768 right - crop->left);
1769 crop->height = clamp_t(u32, crop->height, PREV_MIN_OUT_HEIGHT,
1770 bottom - crop->top);
1771}
1772
1773/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001774 * preview_enum_mbus_code - Handle pixel format enumeration
1775 * @sd : pointer to v4l2 subdev structure
1776 * @fh : V4L2 subdev file handle
1777 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1778 * return -EINVAL or zero on success
1779 */
1780static int preview_enum_mbus_code(struct v4l2_subdev *sd,
1781 struct v4l2_subdev_fh *fh,
1782 struct v4l2_subdev_mbus_code_enum *code)
1783{
1784 switch (code->pad) {
1785 case PREV_PAD_SINK:
1786 if (code->index >= ARRAY_SIZE(preview_input_fmts))
1787 return -EINVAL;
1788
1789 code->code = preview_input_fmts[code->index];
1790 break;
1791 case PREV_PAD_SOURCE:
1792 if (code->index >= ARRAY_SIZE(preview_output_fmts))
1793 return -EINVAL;
1794
1795 code->code = preview_output_fmts[code->index];
1796 break;
1797 default:
1798 return -EINVAL;
1799 }
1800
1801 return 0;
1802}
1803
1804static int preview_enum_frame_size(struct v4l2_subdev *sd,
1805 struct v4l2_subdev_fh *fh,
1806 struct v4l2_subdev_frame_size_enum *fse)
1807{
1808 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1809 struct v4l2_mbus_framefmt format;
1810
1811 if (fse->index != 0)
1812 return -EINVAL;
1813
1814 format.code = fse->code;
1815 format.width = 1;
1816 format.height = 1;
1817 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1818 fse->min_width = format.width;
1819 fse->min_height = format.height;
1820
1821 if (format.code != fse->code)
1822 return -EINVAL;
1823
1824 format.code = fse->code;
1825 format.width = -1;
1826 format.height = -1;
1827 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1828 fse->max_width = format.width;
1829 fse->max_height = format.height;
1830
1831 return 0;
1832}
1833
1834/*
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001835 * preview_get_crop - Retrieve the crop rectangle on a pad
1836 * @sd: ISP preview V4L2 subdevice
1837 * @fh: V4L2 subdev file handle
1838 * @crop: crop rectangle
1839 *
1840 * Return 0 on success or a negative error code otherwise.
1841 */
1842static int preview_get_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1843 struct v4l2_subdev_crop *crop)
1844{
1845 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1846
1847 /* Cropping is only supported on the sink pad. */
1848 if (crop->pad != PREV_PAD_SINK)
1849 return -EINVAL;
1850
1851 crop->rect = *__preview_get_crop(prev, fh, crop->which);
1852 return 0;
1853}
1854
1855/*
1856 * preview_set_crop - Retrieve the crop rectangle on a pad
1857 * @sd: ISP preview V4L2 subdevice
1858 * @fh: V4L2 subdev file handle
1859 * @crop: crop rectangle
1860 *
1861 * Return 0 on success or a negative error code otherwise.
1862 */
1863static int preview_set_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1864 struct v4l2_subdev_crop *crop)
1865{
1866 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1867 struct v4l2_mbus_framefmt *format;
1868
1869 /* Cropping is only supported on the sink pad. */
1870 if (crop->pad != PREV_PAD_SINK)
1871 return -EINVAL;
1872
1873 /* The crop rectangle can't be changed while streaming. */
1874 if (prev->state != ISP_PIPELINE_STREAM_STOPPED)
1875 return -EBUSY;
1876
1877 format = __preview_get_format(prev, fh, PREV_PAD_SINK, crop->which);
1878 preview_try_crop(prev, format, &crop->rect);
1879 *__preview_get_crop(prev, fh, crop->which) = crop->rect;
1880
1881 /* Update the source format. */
1882 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE, crop->which);
1883 preview_try_format(prev, fh, PREV_PAD_SOURCE, format, crop->which);
1884
1885 return 0;
1886}
1887
1888/*
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001889 * preview_get_format - Handle get format by pads subdev method
1890 * @sd : pointer to v4l2 subdev structure
1891 * @fh : V4L2 subdev file handle
1892 * @fmt: pointer to v4l2 subdev format structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001893 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001894 */
1895static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1896 struct v4l2_subdev_format *fmt)
1897{
1898 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1899 struct v4l2_mbus_framefmt *format;
1900
1901 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
1902 if (format == NULL)
1903 return -EINVAL;
1904
1905 fmt->format = *format;
1906 return 0;
1907}
1908
1909/*
1910 * preview_set_format - Handle set format by pads subdev method
1911 * @sd : pointer to v4l2 subdev structure
1912 * @fh : V4L2 subdev file handle
1913 * @fmt: pointer to v4l2 subdev format structure
1914 * return -EINVAL or zero on success
1915 */
1916static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1917 struct v4l2_subdev_format *fmt)
1918{
1919 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1920 struct v4l2_mbus_framefmt *format;
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001921 struct v4l2_rect *crop;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001922
1923 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
1924 if (format == NULL)
1925 return -EINVAL;
1926
1927 preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
1928 *format = fmt->format;
1929
1930 /* Propagate the format from sink to source */
1931 if (fmt->pad == PREV_PAD_SINK) {
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001932 /* Reset the crop rectangle. */
1933 crop = __preview_get_crop(prev, fh, fmt->which);
1934 crop->left = 0;
1935 crop->top = 0;
1936 crop->width = fmt->format.width;
1937 crop->height = fmt->format.height;
1938
1939 preview_try_crop(prev, &fmt->format, crop);
1940
1941 /* Update the source format. */
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001942 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
1943 fmt->which);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001944 preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
1945 fmt->which);
1946 }
1947
1948 return 0;
1949}
1950
1951/*
1952 * preview_init_formats - Initialize formats on all pads
1953 * @sd: ISP preview V4L2 subdevice
1954 * @fh: V4L2 subdev file handle
1955 *
1956 * Initialize all pad formats with default values. If fh is not NULL, try
1957 * formats are initialized on the file handle. Otherwise active formats are
1958 * initialized on the device.
1959 */
1960static int preview_init_formats(struct v4l2_subdev *sd,
1961 struct v4l2_subdev_fh *fh)
1962{
1963 struct v4l2_subdev_format format;
1964
1965 memset(&format, 0, sizeof(format));
1966 format.pad = PREV_PAD_SINK;
1967 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1968 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
1969 format.format.width = 4096;
1970 format.format.height = 4096;
1971 preview_set_format(sd, fh, &format);
1972
1973 return 0;
1974}
1975
1976/* subdev core operations */
1977static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
1978 .ioctl = preview_ioctl,
1979};
1980
1981/* subdev video operations */
1982static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
1983 .s_stream = preview_set_stream,
1984};
1985
1986/* subdev pad operations */
1987static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
1988 .enum_mbus_code = preview_enum_mbus_code,
1989 .enum_frame_size = preview_enum_frame_size,
1990 .get_fmt = preview_get_format,
1991 .set_fmt = preview_set_format,
Laurent Pinchart1f69fd92011-09-21 20:05:45 -03001992 .get_crop = preview_get_crop,
1993 .set_crop = preview_set_crop,
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001994};
1995
1996/* subdev operations */
1997static const struct v4l2_subdev_ops preview_v4l2_ops = {
1998 .core = &preview_v4l2_core_ops,
1999 .video = &preview_v4l2_video_ops,
2000 .pad = &preview_v4l2_pad_ops,
2001};
2002
2003/* subdev internal operations */
2004static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
2005 .open = preview_init_formats,
2006};
2007
2008/* -----------------------------------------------------------------------------
2009 * Media entity operations
2010 */
2011
2012/*
2013 * preview_link_setup - Setup previewer connections.
2014 * @entity : Pointer to media entity structure
2015 * @local : Pointer to local pad array
2016 * @remote : Pointer to remote pad array
2017 * @flags : Link flags
2018 * return -EINVAL or zero on success
2019 */
2020static int preview_link_setup(struct media_entity *entity,
2021 const struct media_pad *local,
2022 const struct media_pad *remote, u32 flags)
2023{
2024 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
2025 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
2026
2027 switch (local->index | media_entity_type(remote->entity)) {
2028 case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
2029 /* read from memory */
2030 if (flags & MEDIA_LNK_FL_ENABLED) {
2031 if (prev->input == PREVIEW_INPUT_CCDC)
2032 return -EBUSY;
2033 prev->input = PREVIEW_INPUT_MEMORY;
2034 } else {
2035 if (prev->input == PREVIEW_INPUT_MEMORY)
2036 prev->input = PREVIEW_INPUT_NONE;
2037 }
2038 break;
2039
2040 case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
2041 /* read from ccdc */
2042 if (flags & MEDIA_LNK_FL_ENABLED) {
2043 if (prev->input == PREVIEW_INPUT_MEMORY)
2044 return -EBUSY;
2045 prev->input = PREVIEW_INPUT_CCDC;
2046 } else {
2047 if (prev->input == PREVIEW_INPUT_CCDC)
2048 prev->input = PREVIEW_INPUT_NONE;
2049 }
2050 break;
2051
2052 /*
2053 * The ISP core doesn't support pipelines with multiple video outputs.
2054 * Revisit this when it will be implemented, and return -EBUSY for now.
2055 */
2056
2057 case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
2058 /* write to memory */
2059 if (flags & MEDIA_LNK_FL_ENABLED) {
2060 if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
2061 return -EBUSY;
2062 prev->output |= PREVIEW_OUTPUT_MEMORY;
2063 } else {
2064 prev->output &= ~PREVIEW_OUTPUT_MEMORY;
2065 }
2066 break;
2067
2068 case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
2069 /* write to resizer */
2070 if (flags & MEDIA_LNK_FL_ENABLED) {
2071 if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
2072 return -EBUSY;
2073 prev->output |= PREVIEW_OUTPUT_RESIZER;
2074 } else {
2075 prev->output &= ~PREVIEW_OUTPUT_RESIZER;
2076 }
2077 break;
2078
2079 default:
2080 return -EINVAL;
2081 }
2082
2083 return 0;
2084}
2085
2086/* media operations */
2087static const struct media_entity_operations preview_media_ops = {
2088 .link_setup = preview_link_setup,
2089};
2090
Laurent Pinchart39099d02011-09-22 16:59:26 -03002091void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
2092{
2093 v4l2_device_unregister_subdev(&prev->subdev);
2094 omap3isp_video_unregister(&prev->video_in);
2095 omap3isp_video_unregister(&prev->video_out);
2096}
2097
2098int omap3isp_preview_register_entities(struct isp_prev_device *prev,
2099 struct v4l2_device *vdev)
2100{
2101 int ret;
2102
2103 /* Register the subdev and video nodes. */
2104 ret = v4l2_device_register_subdev(vdev, &prev->subdev);
2105 if (ret < 0)
2106 goto error;
2107
2108 ret = omap3isp_video_register(&prev->video_in, vdev);
2109 if (ret < 0)
2110 goto error;
2111
2112 ret = omap3isp_video_register(&prev->video_out, vdev);
2113 if (ret < 0)
2114 goto error;
2115
2116 return 0;
2117
2118error:
2119 omap3isp_preview_unregister_entities(prev);
2120 return ret;
2121}
2122
2123/* -----------------------------------------------------------------------------
2124 * ISP previewer initialisation and cleanup
2125 */
2126
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002127/*
Laurent Pinchart39099d02011-09-22 16:59:26 -03002128 * preview_init_entities - Initialize subdev and media entity.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002129 * @prev : Pointer to preview structure
2130 * return -ENOMEM or zero on success
2131 */
2132static int preview_init_entities(struct isp_prev_device *prev)
2133{
2134 struct v4l2_subdev *sd = &prev->subdev;
2135 struct media_pad *pads = prev->pads;
2136 struct media_entity *me = &sd->entity;
2137 int ret;
2138
2139 prev->input = PREVIEW_INPUT_NONE;
2140
2141 v4l2_subdev_init(sd, &preview_v4l2_ops);
2142 sd->internal_ops = &preview_v4l2_internal_ops;
2143 strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
2144 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2145 v4l2_set_subdevdata(sd, prev);
2146 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2147
2148 v4l2_ctrl_handler_init(&prev->ctrls, 2);
2149 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
2150 ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
2151 ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
2152 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
2153 ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
2154 ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
2155 v4l2_ctrl_handler_setup(&prev->ctrls);
2156 sd->ctrl_handler = &prev->ctrls;
2157
2158 pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2159 pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2160
2161 me->ops = &preview_media_ops;
2162 ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
2163 if (ret < 0)
2164 return ret;
2165
2166 preview_init_formats(sd, NULL);
2167
2168 /* According to the OMAP34xx TRM, video buffers need to be aligned on a
2169 * 32 bytes boundary. However, an undocumented hardware bug requires a
2170 * 64 bytes boundary at the preview engine input.
2171 */
2172 prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2173 prev->video_in.ops = &preview_video_ops;
2174 prev->video_in.isp = to_isp_device(prev);
2175 prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2176 prev->video_in.bpl_alignment = 64;
2177 prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2178 prev->video_out.ops = &preview_video_ops;
2179 prev->video_out.isp = to_isp_device(prev);
2180 prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2181 prev->video_out.bpl_alignment = 32;
2182
2183 ret = omap3isp_video_init(&prev->video_in, "preview");
2184 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002185 goto error_video_in;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002186
2187 ret = omap3isp_video_init(&prev->video_out, "preview");
2188 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002189 goto error_video_out;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002190
2191 /* Connect the video nodes to the previewer subdev. */
2192 ret = media_entity_create_link(&prev->video_in.video.entity, 0,
2193 &prev->subdev.entity, PREV_PAD_SINK, 0);
2194 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002195 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002196
2197 ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
2198 &prev->video_out.video.entity, 0, 0);
2199 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002200 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002201
2202 return 0;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002203
2204error_link:
2205 omap3isp_video_cleanup(&prev->video_out);
2206error_video_out:
2207 omap3isp_video_cleanup(&prev->video_in);
2208error_video_in:
2209 media_entity_cleanup(&prev->subdev.entity);
2210 return ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002211}
2212
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002213/*
2214 * isp_preview_init - Previewer initialization.
2215 * @dev : Pointer to ISP device
2216 * return -ENOMEM or zero on success
2217 */
2218int omap3isp_preview_init(struct isp_device *isp)
2219{
2220 struct isp_prev_device *prev = &isp->isp_prev;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002221
2222 spin_lock_init(&prev->lock);
2223 init_waitqueue_head(&prev->wait);
2224 preview_init_params(prev);
2225
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002226 return preview_init_entities(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002227}
Laurent Pinchart39099d02011-09-22 16:59:26 -03002228
2229void omap3isp_preview_cleanup(struct isp_device *isp)
2230{
2231 struct isp_prev_device *prev = &isp->isp_prev;
2232
2233 v4l2_ctrl_handler_free(&prev->ctrls);
2234 omap3isp_video_cleanup(&prev->video_in);
2235 omap3isp_video_cleanup(&prev->video_out);
2236 media_entity_cleanup(&prev->subdev.entity);
2237}