blob: d5cce423283ce4fb52503b05865ac72f020c7d5b [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 Pinchart059dc1d2011-10-03 07:56:15 -030079#define PREV_MIN_IN_WIDTH 64
80#define PREV_MIN_IN_HEIGHT 8
81#define PREV_MAX_IN_HEIGHT 16384
82
83#define PREV_MIN_OUT_WIDTH 0
84#define PREV_MIN_OUT_HEIGHT 0
85#define PREV_MAX_OUT_WIDTH 1280
86#define PREV_MAX_OUT_WIDTH_ES2 3300
87#define PREV_MAX_OUT_WIDTH_3630 4096
Laurent Pinchartde1135d2011-02-12 18:05:06 -030088
89/*
90 * Coeficient Tables for the submodules in Preview.
91 * Array is initialised with the values from.the tables text file.
92 */
93
94/*
95 * CFA Filter Coefficient Table
96 *
97 */
98static u32 cfa_coef_table[] = {
99#include "cfa_coef_table.h"
100};
101
102/*
103 * Default Gamma Correction Table - All components
104 */
105static u32 gamma_table[] = {
106#include "gamma_table.h"
107};
108
109/*
110 * Noise Filter Threshold table
111 */
112static u32 noise_filter_table[] = {
113#include "noise_filter_table.h"
114};
115
116/*
117 * Luminance Enhancement Table
118 */
119static u32 luma_enhance_table[] = {
120#include "luma_enhance_table.h"
121};
122
123/*
124 * preview_enable_invalaw - Enable/Disable Inverse A-Law module in Preview.
125 * @enable: 1 - Reverse the A-Law done in CCDC.
126 */
127static void
128preview_enable_invalaw(struct isp_prev_device *prev, u8 enable)
129{
130 struct isp_device *isp = to_isp_device(prev);
131
132 if (enable)
133 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
134 ISPPRV_PCR_WIDTH | ISPPRV_PCR_INVALAW);
135 else
136 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
137 ISPPRV_PCR_WIDTH | ISPPRV_PCR_INVALAW);
138}
139
140/*
141 * preview_enable_drkframe_capture - Enable/Disable of the darkframe capture.
142 * @prev -
143 * @enable: 1 - Enable, 0 - Disable
144 *
145 * NOTE: PRV_WSDR_ADDR and PRV_WADD_OFFSET must be set also
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300146 * The process is applied for each captured frame.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300147 */
148static void
149preview_enable_drkframe_capture(struct isp_prev_device *prev, u8 enable)
150{
151 struct isp_device *isp = to_isp_device(prev);
152
153 if (enable)
154 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
155 ISPPRV_PCR_DRKFCAP);
156 else
157 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
158 ISPPRV_PCR_DRKFCAP);
159}
160
161/*
162 * preview_enable_drkframe - Enable/Disable of the darkframe subtract.
163 * @enable: 1 - Acquires memory bandwidth since the pixels in each frame is
164 * subtracted with the pixels in the current frame.
165 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 * The process is applied for each captured frame.
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300167 */
168static void
169preview_enable_drkframe(struct isp_prev_device *prev, u8 enable)
170{
171 struct isp_device *isp = to_isp_device(prev);
172
173 if (enable)
174 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
175 ISPPRV_PCR_DRKFEN);
176 else
177 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
178 ISPPRV_PCR_DRKFEN);
179}
180
181/*
182 * preview_config_drkf_shadcomp - Configures shift value in shading comp.
183 * @scomp_shtval: 3bit value of shift used in shading compensation.
184 */
185static void
186preview_config_drkf_shadcomp(struct isp_prev_device *prev,
187 const void *scomp_shtval)
188{
189 struct isp_device *isp = to_isp_device(prev);
190 const u32 *shtval = scomp_shtval;
191
192 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
193 ISPPRV_PCR_SCOMP_SFT_MASK,
194 *shtval << ISPPRV_PCR_SCOMP_SFT_SHIFT);
195}
196
197/*
198 * preview_enable_hmed - Enables/Disables of the Horizontal Median Filter.
199 * @enable: 1 - Enables Horizontal Median Filter.
200 */
201static void
202preview_enable_hmed(struct isp_prev_device *prev, u8 enable)
203{
204 struct isp_device *isp = to_isp_device(prev);
205
206 if (enable)
207 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
208 ISPPRV_PCR_HMEDEN);
209 else
210 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
211 ISPPRV_PCR_HMEDEN);
212}
213
214/*
215 * preview_config_hmed - Configures the Horizontal Median Filter.
216 * @prev_hmed: Structure containing the odd and even distance between the
217 * pixels in the image along with the filter threshold.
218 */
219static void
220preview_config_hmed(struct isp_prev_device *prev, const void *prev_hmed)
221{
222 struct isp_device *isp = to_isp_device(prev);
223 const struct omap3isp_prev_hmed *hmed = prev_hmed;
224
225 isp_reg_writel(isp, (hmed->odddist == 1 ? 0 : ISPPRV_HMED_ODDDIST) |
226 (hmed->evendist == 1 ? 0 : ISPPRV_HMED_EVENDIST) |
227 (hmed->thres << ISPPRV_HMED_THRESHOLD_SHIFT),
228 OMAP3_ISP_IOMEM_PREV, ISPPRV_HMED);
229}
230
231/*
232 * preview_config_noisefilter - Configures the Noise Filter.
233 * @prev_nf: Structure containing the noisefilter table, strength to be used
234 * for the noise filter and the defect correction enable flag.
235 */
236static void
237preview_config_noisefilter(struct isp_prev_device *prev, const void *prev_nf)
238{
239 struct isp_device *isp = to_isp_device(prev);
240 const struct omap3isp_prev_nf *nf = prev_nf;
241 unsigned int i;
242
243 isp_reg_writel(isp, nf->spread, OMAP3_ISP_IOMEM_PREV, ISPPRV_NF);
244 isp_reg_writel(isp, ISPPRV_NF_TABLE_ADDR,
245 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
246 for (i = 0; i < OMAP3ISP_PREV_NF_TBL_SIZE; i++) {
247 isp_reg_writel(isp, nf->table[i],
248 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
249 }
250}
251
252/*
253 * preview_config_dcor - Configures the defect correction
254 * @prev_dcor: Structure containing the defect correct thresholds
255 */
256static void
257preview_config_dcor(struct isp_prev_device *prev, const void *prev_dcor)
258{
259 struct isp_device *isp = to_isp_device(prev);
260 const struct omap3isp_prev_dcor *dcor = prev_dcor;
261
262 isp_reg_writel(isp, dcor->detect_correct[0],
263 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR0);
264 isp_reg_writel(isp, dcor->detect_correct[1],
265 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR1);
266 isp_reg_writel(isp, dcor->detect_correct[2],
267 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR2);
268 isp_reg_writel(isp, dcor->detect_correct[3],
269 OMAP3_ISP_IOMEM_PREV, ISPPRV_CDC_THR3);
270 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
271 ISPPRV_PCR_DCCOUP,
272 dcor->couplet_mode_en ? ISPPRV_PCR_DCCOUP : 0);
273}
274
275/*
276 * preview_config_cfa - Configures the CFA Interpolation parameters.
277 * @prev_cfa: Structure containing the CFA interpolation table, CFA format
278 * in the image, vertical and horizontal gradient threshold.
279 */
280static void
281preview_config_cfa(struct isp_prev_device *prev, const void *prev_cfa)
282{
283 struct isp_device *isp = to_isp_device(prev);
284 const struct omap3isp_prev_cfa *cfa = prev_cfa;
285 unsigned int i;
286
287 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
288 ISPPRV_PCR_CFAFMT_MASK,
289 cfa->format << ISPPRV_PCR_CFAFMT_SHIFT);
290
291 isp_reg_writel(isp,
292 (cfa->gradthrs_vert << ISPPRV_CFA_GRADTH_VER_SHIFT) |
293 (cfa->gradthrs_horz << ISPPRV_CFA_GRADTH_HOR_SHIFT),
294 OMAP3_ISP_IOMEM_PREV, ISPPRV_CFA);
295
296 isp_reg_writel(isp, ISPPRV_CFA_TABLE_ADDR,
297 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
298
299 for (i = 0; i < OMAP3ISP_PREV_CFA_TBL_SIZE; i++) {
300 isp_reg_writel(isp, cfa->table[i],
301 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
302 }
303}
304
305/*
306 * preview_config_gammacorrn - Configures the Gamma Correction table values
307 * @gtable: Structure containing the table for red, blue, green gamma table.
308 */
309static void
310preview_config_gammacorrn(struct isp_prev_device *prev, const void *gtable)
311{
312 struct isp_device *isp = to_isp_device(prev);
313 const struct omap3isp_prev_gtables *gt = gtable;
314 unsigned int i;
315
316 isp_reg_writel(isp, ISPPRV_REDGAMMA_TABLE_ADDR,
317 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
318 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
319 isp_reg_writel(isp, gt->red[i], OMAP3_ISP_IOMEM_PREV,
320 ISPPRV_SET_TBL_DATA);
321
322 isp_reg_writel(isp, ISPPRV_GREENGAMMA_TABLE_ADDR,
323 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
324 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
325 isp_reg_writel(isp, gt->green[i], OMAP3_ISP_IOMEM_PREV,
326 ISPPRV_SET_TBL_DATA);
327
328 isp_reg_writel(isp, ISPPRV_BLUEGAMMA_TABLE_ADDR,
329 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
330 for (i = 0; i < OMAP3ISP_PREV_GAMMA_TBL_SIZE; i++)
331 isp_reg_writel(isp, gt->blue[i], OMAP3_ISP_IOMEM_PREV,
332 ISPPRV_SET_TBL_DATA);
333}
334
335/*
336 * preview_config_luma_enhancement - Sets the Luminance Enhancement table.
337 * @ytable: Structure containing the table for Luminance Enhancement table.
338 */
339static void
340preview_config_luma_enhancement(struct isp_prev_device *prev,
341 const void *ytable)
342{
343 struct isp_device *isp = to_isp_device(prev);
344 const struct omap3isp_prev_luma *yt = ytable;
345 unsigned int i;
346
347 isp_reg_writel(isp, ISPPRV_YENH_TABLE_ADDR,
348 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_ADDR);
349 for (i = 0; i < OMAP3ISP_PREV_YENH_TBL_SIZE; i++) {
350 isp_reg_writel(isp, yt->table[i],
351 OMAP3_ISP_IOMEM_PREV, ISPPRV_SET_TBL_DATA);
352 }
353}
354
355/*
356 * preview_config_chroma_suppression - Configures the Chroma Suppression.
357 * @csup: Structure containing the threshold value for suppression
358 * and the hypass filter enable flag.
359 */
360static void
361preview_config_chroma_suppression(struct isp_prev_device *prev,
362 const void *csup)
363{
364 struct isp_device *isp = to_isp_device(prev);
365 const struct omap3isp_prev_csup *cs = csup;
366
367 isp_reg_writel(isp,
368 cs->gain | (cs->thres << ISPPRV_CSUP_THRES_SHIFT) |
369 (cs->hypf_en << ISPPRV_CSUP_HPYF_SHIFT),
370 OMAP3_ISP_IOMEM_PREV, ISPPRV_CSUP);
371}
372
373/*
374 * preview_enable_noisefilter - Enables/Disables the Noise Filter.
375 * @enable: 1 - Enables the Noise Filter.
376 */
377static void
378preview_enable_noisefilter(struct isp_prev_device *prev, u8 enable)
379{
380 struct isp_device *isp = to_isp_device(prev);
381
382 if (enable)
383 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
384 ISPPRV_PCR_NFEN);
385 else
386 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
387 ISPPRV_PCR_NFEN);
388}
389
390/*
391 * preview_enable_dcor - Enables/Disables the defect correction.
392 * @enable: 1 - Enables the defect correction.
393 */
394static void
395preview_enable_dcor(struct isp_prev_device *prev, u8 enable)
396{
397 struct isp_device *isp = to_isp_device(prev);
398
399 if (enable)
400 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
401 ISPPRV_PCR_DCOREN);
402 else
403 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
404 ISPPRV_PCR_DCOREN);
405}
406
407/*
408 * preview_enable_cfa - Enable/Disable the CFA Interpolation.
409 * @enable: 1 - Enables the CFA.
410 */
411static void
412preview_enable_cfa(struct isp_prev_device *prev, u8 enable)
413{
414 struct isp_device *isp = to_isp_device(prev);
415
416 if (enable)
417 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
418 ISPPRV_PCR_CFAEN);
419 else
420 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
421 ISPPRV_PCR_CFAEN);
422}
423
424/*
425 * preview_enable_gammabypass - Enables/Disables the GammaByPass
426 * @enable: 1 - Bypasses Gamma - 10bit input is cropped to 8MSB.
427 * 0 - Goes through Gamma Correction. input and output is 10bit.
428 */
429static void
430preview_enable_gammabypass(struct isp_prev_device *prev, u8 enable)
431{
432 struct isp_device *isp = to_isp_device(prev);
433
434 if (enable)
435 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
436 ISPPRV_PCR_GAMMA_BYPASS);
437 else
438 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
439 ISPPRV_PCR_GAMMA_BYPASS);
440}
441
442/*
443 * preview_enable_luma_enhancement - Enables/Disables Luminance Enhancement
444 * @enable: 1 - Enable the Luminance Enhancement.
445 */
446static void
447preview_enable_luma_enhancement(struct isp_prev_device *prev, u8 enable)
448{
449 struct isp_device *isp = to_isp_device(prev);
450
451 if (enable)
452 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
453 ISPPRV_PCR_YNENHEN);
454 else
455 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
456 ISPPRV_PCR_YNENHEN);
457}
458
459/*
460 * preview_enable_chroma_suppression - Enables/Disables Chrominance Suppr.
461 * @enable: 1 - Enable the Chrominance Suppression.
462 */
463static void
464preview_enable_chroma_suppression(struct isp_prev_device *prev, u8 enable)
465{
466 struct isp_device *isp = to_isp_device(prev);
467
468 if (enable)
469 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
470 ISPPRV_PCR_SUPEN);
471 else
472 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
473 ISPPRV_PCR_SUPEN);
474}
475
476/*
477 * preview_config_whitebalance - Configures the White Balance parameters.
478 * @prev_wbal: Structure containing the digital gain and white balance
479 * coefficient.
480 *
481 * Coefficient matrix always with default values.
482 */
483static void
484preview_config_whitebalance(struct isp_prev_device *prev, const void *prev_wbal)
485{
486 struct isp_device *isp = to_isp_device(prev);
487 const struct omap3isp_prev_wbal *wbal = prev_wbal;
488 u32 val;
489
490 isp_reg_writel(isp, wbal->dgain, OMAP3_ISP_IOMEM_PREV, ISPPRV_WB_DGAIN);
491
492 val = wbal->coef0 << ISPPRV_WBGAIN_COEF0_SHIFT;
493 val |= wbal->coef1 << ISPPRV_WBGAIN_COEF1_SHIFT;
494 val |= wbal->coef2 << ISPPRV_WBGAIN_COEF2_SHIFT;
495 val |= wbal->coef3 << ISPPRV_WBGAIN_COEF3_SHIFT;
496 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_WBGAIN);
497
498 isp_reg_writel(isp,
499 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_0_SHIFT |
500 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_1_SHIFT |
501 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N0_2_SHIFT |
502 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N0_3_SHIFT |
503 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_0_SHIFT |
504 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_1_SHIFT |
505 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N1_2_SHIFT |
506 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N1_3_SHIFT |
507 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_0_SHIFT |
508 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_1_SHIFT |
509 ISPPRV_WBSEL_COEF0 << ISPPRV_WBSEL_N2_2_SHIFT |
510 ISPPRV_WBSEL_COEF1 << ISPPRV_WBSEL_N2_3_SHIFT |
511 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_0_SHIFT |
512 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_1_SHIFT |
513 ISPPRV_WBSEL_COEF2 << ISPPRV_WBSEL_N3_2_SHIFT |
514 ISPPRV_WBSEL_COEF3 << ISPPRV_WBSEL_N3_3_SHIFT,
515 OMAP3_ISP_IOMEM_PREV, ISPPRV_WBSEL);
516}
517
518/*
519 * preview_config_blkadj - Configures the Black Adjustment parameters.
520 * @prev_blkadj: Structure containing the black adjustment towards red, green,
521 * blue.
522 */
523static void
524preview_config_blkadj(struct isp_prev_device *prev, const void *prev_blkadj)
525{
526 struct isp_device *isp = to_isp_device(prev);
527 const struct omap3isp_prev_blkadj *blkadj = prev_blkadj;
528
529 isp_reg_writel(isp, (blkadj->blue << ISPPRV_BLKADJOFF_B_SHIFT) |
530 (blkadj->green << ISPPRV_BLKADJOFF_G_SHIFT) |
531 (blkadj->red << ISPPRV_BLKADJOFF_R_SHIFT),
532 OMAP3_ISP_IOMEM_PREV, ISPPRV_BLKADJOFF);
533}
534
535/*
536 * preview_config_rgb_blending - Configures the RGB-RGB Blending matrix.
537 * @rgb2rgb: Structure containing the rgb to rgb blending matrix and the rgb
538 * offset.
539 */
540static void
541preview_config_rgb_blending(struct isp_prev_device *prev, const void *rgb2rgb)
542{
543 struct isp_device *isp = to_isp_device(prev);
544 const struct omap3isp_prev_rgbtorgb *rgbrgb = rgb2rgb;
545 u32 val;
546
547 val = (rgbrgb->matrix[0][0] & 0xfff) << ISPPRV_RGB_MAT1_MTX_RR_SHIFT;
548 val |= (rgbrgb->matrix[0][1] & 0xfff) << ISPPRV_RGB_MAT1_MTX_GR_SHIFT;
549 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT1);
550
551 val = (rgbrgb->matrix[0][2] & 0xfff) << ISPPRV_RGB_MAT2_MTX_BR_SHIFT;
552 val |= (rgbrgb->matrix[1][0] & 0xfff) << ISPPRV_RGB_MAT2_MTX_RG_SHIFT;
553 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT2);
554
555 val = (rgbrgb->matrix[1][1] & 0xfff) << ISPPRV_RGB_MAT3_MTX_GG_SHIFT;
556 val |= (rgbrgb->matrix[1][2] & 0xfff) << ISPPRV_RGB_MAT3_MTX_BG_SHIFT;
557 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT3);
558
559 val = (rgbrgb->matrix[2][0] & 0xfff) << ISPPRV_RGB_MAT4_MTX_RB_SHIFT;
560 val |= (rgbrgb->matrix[2][1] & 0xfff) << ISPPRV_RGB_MAT4_MTX_GB_SHIFT;
561 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT4);
562
563 val = (rgbrgb->matrix[2][2] & 0xfff) << ISPPRV_RGB_MAT5_MTX_BB_SHIFT;
564 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_MAT5);
565
566 val = (rgbrgb->offset[0] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFR_SHIFT;
567 val |= (rgbrgb->offset[1] & 0x3ff) << ISPPRV_RGB_OFF1_MTX_OFFG_SHIFT;
568 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF1);
569
570 val = (rgbrgb->offset[2] & 0x3ff) << ISPPRV_RGB_OFF2_MTX_OFFB_SHIFT;
571 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_RGB_OFF2);
572}
573
574/*
575 * Configures the RGB-YCbYCr conversion matrix
576 * @prev_csc: Structure containing the RGB to YCbYCr matrix and the
577 * YCbCr offset.
578 */
579static void
580preview_config_rgb_to_ycbcr(struct isp_prev_device *prev, const void *prev_csc)
581{
582 struct isp_device *isp = to_isp_device(prev);
583 const struct omap3isp_prev_csc *csc = prev_csc;
584 u32 val;
585
586 val = (csc->matrix[0][0] & 0x3ff) << ISPPRV_CSC0_RY_SHIFT;
587 val |= (csc->matrix[0][1] & 0x3ff) << ISPPRV_CSC0_GY_SHIFT;
588 val |= (csc->matrix[0][2] & 0x3ff) << ISPPRV_CSC0_BY_SHIFT;
589 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC0);
590
591 val = (csc->matrix[1][0] & 0x3ff) << ISPPRV_CSC1_RCB_SHIFT;
592 val |= (csc->matrix[1][1] & 0x3ff) << ISPPRV_CSC1_GCB_SHIFT;
593 val |= (csc->matrix[1][2] & 0x3ff) << ISPPRV_CSC1_BCB_SHIFT;
594 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC1);
595
596 val = (csc->matrix[2][0] & 0x3ff) << ISPPRV_CSC2_RCR_SHIFT;
597 val |= (csc->matrix[2][1] & 0x3ff) << ISPPRV_CSC2_GCR_SHIFT;
598 val |= (csc->matrix[2][2] & 0x3ff) << ISPPRV_CSC2_BCR_SHIFT;
599 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC2);
600
601 val = (csc->offset[0] & 0xff) << ISPPRV_CSC_OFFSET_Y_SHIFT;
602 val |= (csc->offset[1] & 0xff) << ISPPRV_CSC_OFFSET_CB_SHIFT;
603 val |= (csc->offset[2] & 0xff) << ISPPRV_CSC_OFFSET_CR_SHIFT;
604 isp_reg_writel(isp, val, OMAP3_ISP_IOMEM_PREV, ISPPRV_CSC_OFFSET);
605}
606
607/*
608 * preview_update_contrast - Updates the contrast.
609 * @contrast: Pointer to hold the current programmed contrast value.
610 *
611 * Value should be programmed before enabling the module.
612 */
613static void
614preview_update_contrast(struct isp_prev_device *prev, u8 contrast)
615{
616 struct prev_params *params = &prev->params;
617
618 if (params->contrast != (contrast * ISPPRV_CONTRAST_UNITS)) {
619 params->contrast = contrast * ISPPRV_CONTRAST_UNITS;
620 prev->update |= PREV_CONTRAST;
621 }
622}
623
624/*
625 * preview_config_contrast - Configures the Contrast.
626 * @params: Contrast value (u8 pointer, U8Q0 format).
627 *
628 * Value should be programmed before enabling the module.
629 */
630static void
631preview_config_contrast(struct isp_prev_device *prev, const void *params)
632{
633 struct isp_device *isp = to_isp_device(prev);
634
635 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
636 0xff << ISPPRV_CNT_BRT_CNT_SHIFT,
637 *(u8 *)params << ISPPRV_CNT_BRT_CNT_SHIFT);
638}
639
640/*
641 * preview_update_brightness - Updates the brightness in preview module.
642 * @brightness: Pointer to hold the current programmed brightness value.
643 *
644 */
645static void
646preview_update_brightness(struct isp_prev_device *prev, u8 brightness)
647{
648 struct prev_params *params = &prev->params;
649
650 if (params->brightness != (brightness * ISPPRV_BRIGHT_UNITS)) {
651 params->brightness = brightness * ISPPRV_BRIGHT_UNITS;
652 prev->update |= PREV_BRIGHTNESS;
653 }
654}
655
656/*
657 * preview_config_brightness - Configures the brightness.
658 * @params: Brightness value (u8 pointer, U8Q0 format).
659 */
660static void
661preview_config_brightness(struct isp_prev_device *prev, const void *params)
662{
663 struct isp_device *isp = to_isp_device(prev);
664
665 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_CNT_BRT,
666 0xff << ISPPRV_CNT_BRT_BRT_SHIFT,
667 *(u8 *)params << ISPPRV_CNT_BRT_BRT_SHIFT);
668}
669
670/*
671 * preview_config_yc_range - Configures the max and min Y and C values.
672 * @yclimit: Structure containing the range of Y and C values.
673 */
674static void
675preview_config_yc_range(struct isp_prev_device *prev, const void *yclimit)
676{
677 struct isp_device *isp = to_isp_device(prev);
678 const struct omap3isp_prev_yclimit *yc = yclimit;
679
680 isp_reg_writel(isp,
681 yc->maxC << ISPPRV_SETUP_YC_MAXC_SHIFT |
682 yc->maxY << ISPPRV_SETUP_YC_MAXY_SHIFT |
683 yc->minC << ISPPRV_SETUP_YC_MINC_SHIFT |
684 yc->minY << ISPPRV_SETUP_YC_MINY_SHIFT,
685 OMAP3_ISP_IOMEM_PREV, ISPPRV_SETUP_YC);
686}
687
688/* preview parameters update structure */
689struct preview_update {
690 int cfg_bit;
691 int feature_bit;
692 void (*config)(struct isp_prev_device *, const void *);
693 void (*enable)(struct isp_prev_device *, u8);
694};
695
696static struct preview_update update_attrs[] = {
697 {OMAP3ISP_PREV_LUMAENH, PREV_LUMA_ENHANCE,
698 preview_config_luma_enhancement,
699 preview_enable_luma_enhancement},
700 {OMAP3ISP_PREV_INVALAW, PREV_INVERSE_ALAW,
701 NULL,
702 preview_enable_invalaw},
703 {OMAP3ISP_PREV_HRZ_MED, PREV_HORZ_MEDIAN_FILTER,
704 preview_config_hmed,
705 preview_enable_hmed},
706 {OMAP3ISP_PREV_CFA, PREV_CFA,
707 preview_config_cfa,
708 preview_enable_cfa},
709 {OMAP3ISP_PREV_CHROMA_SUPP, PREV_CHROMA_SUPPRESS,
710 preview_config_chroma_suppression,
711 preview_enable_chroma_suppression},
712 {OMAP3ISP_PREV_WB, PREV_WB,
713 preview_config_whitebalance,
714 NULL},
715 {OMAP3ISP_PREV_BLKADJ, PREV_BLKADJ,
716 preview_config_blkadj,
717 NULL},
718 {OMAP3ISP_PREV_RGB2RGB, PREV_RGB2RGB,
719 preview_config_rgb_blending,
720 NULL},
721 {OMAP3ISP_PREV_COLOR_CONV, PREV_COLOR_CONV,
722 preview_config_rgb_to_ycbcr,
723 NULL},
724 {OMAP3ISP_PREV_YC_LIMIT, PREV_YCLIMITS,
725 preview_config_yc_range,
726 NULL},
727 {OMAP3ISP_PREV_DEFECT_COR, PREV_DEFECT_COR,
728 preview_config_dcor,
729 preview_enable_dcor},
730 {OMAP3ISP_PREV_GAMMABYPASS, PREV_GAMMA_BYPASS,
731 NULL,
732 preview_enable_gammabypass},
733 {OMAP3ISP_PREV_DRK_FRM_CAPTURE, PREV_DARK_FRAME_CAPTURE,
734 NULL,
735 preview_enable_drkframe_capture},
736 {OMAP3ISP_PREV_DRK_FRM_SUBTRACT, PREV_DARK_FRAME_SUBTRACT,
737 NULL,
738 preview_enable_drkframe},
739 {OMAP3ISP_PREV_LENS_SHADING, PREV_LENS_SHADING,
740 preview_config_drkf_shadcomp,
741 preview_enable_drkframe},
742 {OMAP3ISP_PREV_NF, PREV_NOISE_FILTER,
743 preview_config_noisefilter,
744 preview_enable_noisefilter},
745 {OMAP3ISP_PREV_GAMMA, PREV_GAMMA,
746 preview_config_gammacorrn,
747 NULL},
748 {-1, PREV_CONTRAST,
749 preview_config_contrast,
750 NULL},
751 {-1, PREV_BRIGHTNESS,
752 preview_config_brightness,
753 NULL},
754};
755
756/*
757 * __preview_get_ptrs - helper function which return pointers to members
758 * of params and config structures.
759 * @params - pointer to preview_params structure.
760 * @param - return pointer to appropriate structure field.
761 * @configs - pointer to update config structure.
762 * @config - return pointer to appropriate structure field.
763 * @bit - for which feature to return pointers.
Michael Jones2d4e9d12011-02-28 08:29:03 -0300764 * Return size of corresponding prev_params member
Laurent Pinchartde1135d2011-02-12 18:05:06 -0300765 */
766static u32
767__preview_get_ptrs(struct prev_params *params, void **param,
768 struct omap3isp_prev_update_config *configs,
769 void __user **config, u32 bit)
770{
771#define CHKARG(cfgs, cfg, field) \
772 if (cfgs && cfg) { \
773 *(cfg) = (cfgs)->field; \
774 }
775
776 switch (bit) {
777 case PREV_HORZ_MEDIAN_FILTER:
778 *param = &params->hmed;
779 CHKARG(configs, config, hmed)
780 return sizeof(params->hmed);
781 case PREV_NOISE_FILTER:
782 *param = &params->nf;
783 CHKARG(configs, config, nf)
784 return sizeof(params->nf);
785 break;
786 case PREV_CFA:
787 *param = &params->cfa;
788 CHKARG(configs, config, cfa)
789 return sizeof(params->cfa);
790 case PREV_LUMA_ENHANCE:
791 *param = &params->luma;
792 CHKARG(configs, config, luma)
793 return sizeof(params->luma);
794 case PREV_CHROMA_SUPPRESS:
795 *param = &params->csup;
796 CHKARG(configs, config, csup)
797 return sizeof(params->csup);
798 case PREV_DEFECT_COR:
799 *param = &params->dcor;
800 CHKARG(configs, config, dcor)
801 return sizeof(params->dcor);
802 case PREV_BLKADJ:
803 *param = &params->blk_adj;
804 CHKARG(configs, config, blkadj)
805 return sizeof(params->blk_adj);
806 case PREV_YCLIMITS:
807 *param = &params->yclimit;
808 CHKARG(configs, config, yclimit)
809 return sizeof(params->yclimit);
810 case PREV_RGB2RGB:
811 *param = &params->rgb2rgb;
812 CHKARG(configs, config, rgb2rgb)
813 return sizeof(params->rgb2rgb);
814 case PREV_COLOR_CONV:
815 *param = &params->rgb2ycbcr;
816 CHKARG(configs, config, csc)
817 return sizeof(params->rgb2ycbcr);
818 case PREV_WB:
819 *param = &params->wbal;
820 CHKARG(configs, config, wbal)
821 return sizeof(params->wbal);
822 case PREV_GAMMA:
823 *param = &params->gamma;
824 CHKARG(configs, config, gamma)
825 return sizeof(params->gamma);
826 case PREV_CONTRAST:
827 *param = &params->contrast;
828 return 0;
829 case PREV_BRIGHTNESS:
830 *param = &params->brightness;
831 return 0;
832 default:
833 *param = NULL;
834 *config = NULL;
835 break;
836 }
837 return 0;
838}
839
840/*
841 * preview_config - Copy and update local structure with userspace preview
842 * configuration.
843 * @prev: ISP preview engine
844 * @cfg: Configuration
845 *
846 * Return zero if success or -EFAULT if the configuration can't be copied from
847 * userspace.
848 */
849static int preview_config(struct isp_prev_device *prev,
850 struct omap3isp_prev_update_config *cfg)
851{
852 struct prev_params *params;
853 struct preview_update *attr;
854 int i, bit, rval = 0;
855
856 params = &prev->params;
857
858 if (prev->state != ISP_PIPELINE_STREAM_STOPPED) {
859 unsigned long flags;
860
861 spin_lock_irqsave(&prev->lock, flags);
862 prev->shadow_update = 1;
863 spin_unlock_irqrestore(&prev->lock, flags);
864 }
865
866 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
867 attr = &update_attrs[i];
868 bit = 0;
869
870 if (!(cfg->update & attr->cfg_bit))
871 continue;
872
873 bit = cfg->flag & attr->cfg_bit;
874 if (bit) {
875 void *to = NULL, __user *from = NULL;
876 unsigned long sz = 0;
877
878 sz = __preview_get_ptrs(params, &to, cfg, &from,
879 bit);
880 if (to && from && sz) {
881 if (copy_from_user(to, from, sz)) {
882 rval = -EFAULT;
883 break;
884 }
885 }
886 params->features |= attr->feature_bit;
887 } else {
888 params->features &= ~attr->feature_bit;
889 }
890
891 prev->update |= attr->feature_bit;
892 }
893
894 prev->shadow_update = 0;
895 return rval;
896}
897
898/*
899 * preview_setup_hw - Setup preview registers and/or internal memory
900 * @prev: pointer to preview private structure
901 * Note: can be called from interrupt context
902 * Return none
903 */
904static void preview_setup_hw(struct isp_prev_device *prev)
905{
906 struct prev_params *params = &prev->params;
907 struct preview_update *attr;
908 int i, bit;
909 void *param_ptr;
910
911 for (i = 0; i < ARRAY_SIZE(update_attrs); i++) {
912 attr = &update_attrs[i];
913
914 if (!(prev->update & attr->feature_bit))
915 continue;
916 bit = params->features & attr->feature_bit;
917 if (bit) {
918 if (attr->config) {
919 __preview_get_ptrs(params, &param_ptr, NULL,
920 NULL, bit);
921 attr->config(prev, param_ptr);
922 }
923 if (attr->enable)
924 attr->enable(prev, 1);
925 } else
926 if (attr->enable)
927 attr->enable(prev, 0);
928
929 prev->update &= ~attr->feature_bit;
930 }
931}
932
933/*
934 * preview_config_ycpos - Configure byte layout of YUV image.
935 * @mode: Indicates the required byte layout.
936 */
937static void
938preview_config_ycpos(struct isp_prev_device *prev,
939 enum v4l2_mbus_pixelcode pixelcode)
940{
941 struct isp_device *isp = to_isp_device(prev);
942 enum preview_ycpos_mode mode;
943
944 switch (pixelcode) {
945 case V4L2_MBUS_FMT_YUYV8_1X16:
946 mode = YCPOS_CrYCbY;
947 break;
948 case V4L2_MBUS_FMT_UYVY8_1X16:
949 mode = YCPOS_YCrYCb;
950 break;
951 default:
952 return;
953 }
954
955 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
956 ISPPRV_PCR_YCPOS_CrYCbY,
957 mode << ISPPRV_PCR_YCPOS_SHIFT);
958}
959
960/*
961 * preview_config_averager - Enable / disable / configure averager
962 * @average: Average value to be configured.
963 */
964static void preview_config_averager(struct isp_prev_device *prev, u8 average)
965{
966 struct isp_device *isp = to_isp_device(prev);
967 int reg = 0;
968
969 if (prev->params.cfa.format == OMAP3ISP_CFAFMT_BAYER)
970 reg = ISPPRV_AVE_EVENDIST_2 << ISPPRV_AVE_EVENDIST_SHIFT |
971 ISPPRV_AVE_ODDDIST_2 << ISPPRV_AVE_ODDDIST_SHIFT |
972 average;
973 else if (prev->params.cfa.format == OMAP3ISP_CFAFMT_RGBFOVEON)
974 reg = ISPPRV_AVE_EVENDIST_3 << ISPPRV_AVE_EVENDIST_SHIFT |
975 ISPPRV_AVE_ODDDIST_3 << ISPPRV_AVE_ODDDIST_SHIFT |
976 average;
977 isp_reg_writel(isp, reg, OMAP3_ISP_IOMEM_PREV, ISPPRV_AVE);
978}
979
980/*
981 * preview_config_input_size - Configure the input frame size
982 *
983 * The preview engine crops several rows and columns internally depending on
984 * which processing blocks are enabled. The driver assumes all those blocks are
985 * enabled when reporting source pad formats to userspace. If this assumption is
986 * not true, rows and columns must be manually cropped at the preview engine
987 * input to avoid overflows at the end of lines and frames.
988 */
989static void preview_config_input_size(struct isp_prev_device *prev)
990{
991 struct isp_device *isp = to_isp_device(prev);
992 struct prev_params *params = &prev->params;
993 struct v4l2_mbus_framefmt *format = &prev->formats[PREV_PAD_SINK];
994 unsigned int sph = 0;
995 unsigned int eph = format->width - 1;
996 unsigned int slv = 0;
997 unsigned int elv = format->height - 1;
998
999 if (prev->input == PREVIEW_INPUT_CCDC) {
1000 sph += 2;
1001 eph -= 2;
1002 }
1003
1004 /*
1005 * Median filter 4 pixels
1006 * Noise filter 4 pixels, 4 lines
1007 * or faulty pixels correction
1008 * CFA filter 4 pixels, 4 lines in Bayer mode
1009 * 2 lines in other modes
1010 * Color suppression 2 pixels
1011 * or luma enhancement
1012 * -------------------------------------------------------------
1013 * Maximum total 14 pixels, 8 lines
1014 */
1015
1016 if (!(params->features & PREV_CFA)) {
1017 sph += 2;
1018 eph -= 2;
1019 slv += 2;
1020 elv -= 2;
1021 }
1022 if (!(params->features & (PREV_DEFECT_COR | PREV_NOISE_FILTER))) {
1023 sph += 2;
1024 eph -= 2;
1025 slv += 2;
1026 elv -= 2;
1027 }
1028 if (!(params->features & PREV_HORZ_MEDIAN_FILTER)) {
1029 sph += 2;
1030 eph -= 2;
1031 }
1032 if (!(params->features & (PREV_CHROMA_SUPPRESS | PREV_LUMA_ENHANCE)))
1033 sph += 2;
1034
1035 isp_reg_writel(isp, (sph << ISPPRV_HORZ_INFO_SPH_SHIFT) | eph,
1036 OMAP3_ISP_IOMEM_PREV, ISPPRV_HORZ_INFO);
1037 isp_reg_writel(isp, (slv << ISPPRV_VERT_INFO_SLV_SHIFT) | elv,
1038 OMAP3_ISP_IOMEM_PREV, ISPPRV_VERT_INFO);
1039}
1040
1041/*
1042 * preview_config_inlineoffset - Configures the Read address line offset.
1043 * @prev: Preview module
1044 * @offset: Line offset
1045 *
1046 * According to the TRM, the line offset must be aligned on a 32 bytes boundary.
1047 * However, a hardware bug requires the memory start address to be aligned on a
1048 * 64 bytes boundary, so the offset probably should be aligned on 64 bytes as
1049 * well.
1050 */
1051static void
1052preview_config_inlineoffset(struct isp_prev_device *prev, u32 offset)
1053{
1054 struct isp_device *isp = to_isp_device(prev);
1055
1056 isp_reg_writel(isp, offset & 0xffff, OMAP3_ISP_IOMEM_PREV,
1057 ISPPRV_RADR_OFFSET);
1058}
1059
1060/*
1061 * preview_set_inaddr - Sets memory address of input frame.
1062 * @addr: 32bit memory address aligned on 32byte boundary.
1063 *
1064 * Configures the memory address from which the input frame is to be read.
1065 */
1066static void preview_set_inaddr(struct isp_prev_device *prev, u32 addr)
1067{
1068 struct isp_device *isp = to_isp_device(prev);
1069
1070 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_PREV, ISPPRV_RSDR_ADDR);
1071}
1072
1073/*
1074 * preview_config_outlineoffset - Configures the Write address line offset.
1075 * @offset: Line Offset for the preview output.
1076 *
1077 * The offset must be a multiple of 32 bytes.
1078 */
1079static void preview_config_outlineoffset(struct isp_prev_device *prev,
1080 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_WADD_OFFSET);
1086}
1087
1088/*
1089 * preview_set_outaddr - Sets the memory address to store output frame
1090 * @addr: 32bit memory address aligned on 32byte boundary.
1091 *
1092 * Configures the memory address to which the output frame is written.
1093 */
1094static void preview_set_outaddr(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_WSDR_ADDR);
1099}
1100
1101static void preview_adjust_bandwidth(struct isp_prev_device *prev)
1102{
1103 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1104 struct isp_device *isp = to_isp_device(prev);
1105 const struct v4l2_mbus_framefmt *ifmt = &prev->formats[PREV_PAD_SINK];
1106 unsigned long l3_ick = pipe->l3_ick;
1107 struct v4l2_fract *timeperframe;
1108 unsigned int cycles_per_frame;
1109 unsigned int requests_per_frame;
1110 unsigned int cycles_per_request;
1111 unsigned int minimum;
1112 unsigned int maximum;
1113 unsigned int value;
1114
1115 if (prev->input != PREVIEW_INPUT_MEMORY) {
1116 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1117 ISPSBL_SDR_REQ_PRV_EXP_MASK);
1118 return;
1119 }
1120
1121 /* Compute the minimum number of cycles per request, based on the
1122 * pipeline maximum data rate. This is an absolute lower bound if we
1123 * don't want SBL overflows, so round the value up.
1124 */
1125 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
1126 pipe->max_rate);
1127 minimum = DIV_ROUND_UP(cycles_per_request, 32);
1128
1129 /* Compute the maximum number of cycles per request, based on the
1130 * requested frame rate. This is a soft upper bound to achieve a frame
1131 * rate equal or higher than the requested value, so round the value
1132 * down.
1133 */
1134 timeperframe = &pipe->max_timeperframe;
1135
1136 requests_per_frame = DIV_ROUND_UP(ifmt->width * 2, 256) * ifmt->height;
1137 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
1138 timeperframe->denominator);
1139 cycles_per_request = cycles_per_frame / requests_per_frame;
1140
1141 maximum = cycles_per_request / 32;
1142
1143 value = max(minimum, maximum);
1144
1145 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
1146 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
1147 ISPSBL_SDR_REQ_PRV_EXP_MASK,
1148 value << ISPSBL_SDR_REQ_PRV_EXP_SHIFT);
1149}
1150
1151/*
1152 * omap3isp_preview_busy - Gets busy state of preview module.
1153 */
1154int omap3isp_preview_busy(struct isp_prev_device *prev)
1155{
1156 struct isp_device *isp = to_isp_device(prev);
1157
1158 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR)
1159 & ISPPRV_PCR_BUSY;
1160}
1161
1162/*
1163 * omap3isp_preview_restore_context - Restores the values of preview registers
1164 */
1165void omap3isp_preview_restore_context(struct isp_device *isp)
1166{
1167 isp->isp_prev.update = PREV_FEATURES_END - 1;
1168 preview_setup_hw(&isp->isp_prev);
1169}
1170
1171/*
1172 * preview_print_status - Dump preview module registers to the kernel log
1173 */
1174#define PREV_PRINT_REGISTER(isp, name)\
1175 dev_dbg(isp->dev, "###PRV " #name "=0x%08x\n", \
1176 isp_reg_readl(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_##name))
1177
1178static void preview_print_status(struct isp_prev_device *prev)
1179{
1180 struct isp_device *isp = to_isp_device(prev);
1181
1182 dev_dbg(isp->dev, "-------------Preview Register dump----------\n");
1183
1184 PREV_PRINT_REGISTER(isp, PCR);
1185 PREV_PRINT_REGISTER(isp, HORZ_INFO);
1186 PREV_PRINT_REGISTER(isp, VERT_INFO);
1187 PREV_PRINT_REGISTER(isp, RSDR_ADDR);
1188 PREV_PRINT_REGISTER(isp, RADR_OFFSET);
1189 PREV_PRINT_REGISTER(isp, DSDR_ADDR);
1190 PREV_PRINT_REGISTER(isp, DRKF_OFFSET);
1191 PREV_PRINT_REGISTER(isp, WSDR_ADDR);
1192 PREV_PRINT_REGISTER(isp, WADD_OFFSET);
1193 PREV_PRINT_REGISTER(isp, AVE);
1194 PREV_PRINT_REGISTER(isp, HMED);
1195 PREV_PRINT_REGISTER(isp, NF);
1196 PREV_PRINT_REGISTER(isp, WB_DGAIN);
1197 PREV_PRINT_REGISTER(isp, WBGAIN);
1198 PREV_PRINT_REGISTER(isp, WBSEL);
1199 PREV_PRINT_REGISTER(isp, CFA);
1200 PREV_PRINT_REGISTER(isp, BLKADJOFF);
1201 PREV_PRINT_REGISTER(isp, RGB_MAT1);
1202 PREV_PRINT_REGISTER(isp, RGB_MAT2);
1203 PREV_PRINT_REGISTER(isp, RGB_MAT3);
1204 PREV_PRINT_REGISTER(isp, RGB_MAT4);
1205 PREV_PRINT_REGISTER(isp, RGB_MAT5);
1206 PREV_PRINT_REGISTER(isp, RGB_OFF1);
1207 PREV_PRINT_REGISTER(isp, RGB_OFF2);
1208 PREV_PRINT_REGISTER(isp, CSC0);
1209 PREV_PRINT_REGISTER(isp, CSC1);
1210 PREV_PRINT_REGISTER(isp, CSC2);
1211 PREV_PRINT_REGISTER(isp, CSC_OFFSET);
1212 PREV_PRINT_REGISTER(isp, CNT_BRT);
1213 PREV_PRINT_REGISTER(isp, CSUP);
1214 PREV_PRINT_REGISTER(isp, SETUP_YC);
1215 PREV_PRINT_REGISTER(isp, SET_TBL_ADDR);
1216 PREV_PRINT_REGISTER(isp, CDC_THR0);
1217 PREV_PRINT_REGISTER(isp, CDC_THR1);
1218 PREV_PRINT_REGISTER(isp, CDC_THR2);
1219 PREV_PRINT_REGISTER(isp, CDC_THR3);
1220
1221 dev_dbg(isp->dev, "--------------------------------------------\n");
1222}
1223
1224/*
1225 * preview_init_params - init image processing parameters.
1226 * @prev: pointer to previewer private structure
1227 * return none
1228 */
1229static void preview_init_params(struct isp_prev_device *prev)
1230{
1231 struct prev_params *params = &prev->params;
1232 int i = 0;
1233
1234 /* Init values */
1235 params->contrast = ISPPRV_CONTRAST_DEF * ISPPRV_CONTRAST_UNITS;
1236 params->brightness = ISPPRV_BRIGHT_DEF * ISPPRV_BRIGHT_UNITS;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001237 params->cfa.format = OMAP3ISP_CFAFMT_BAYER;
1238 memcpy(params->cfa.table, cfa_coef_table,
1239 sizeof(params->cfa.table));
1240 params->cfa.gradthrs_horz = FLR_CFA_GRADTHRS_HORZ;
1241 params->cfa.gradthrs_vert = FLR_CFA_GRADTHRS_VERT;
1242 params->csup.gain = FLR_CSUP_GAIN;
1243 params->csup.thres = FLR_CSUP_THRES;
1244 params->csup.hypf_en = 0;
1245 memcpy(params->luma.table, luma_enhance_table,
1246 sizeof(params->luma.table));
1247 params->nf.spread = FLR_NF_STRGTH;
1248 memcpy(params->nf.table, noise_filter_table, sizeof(params->nf.table));
1249 params->dcor.couplet_mode_en = 1;
1250 for (i = 0; i < OMAP3ISP_PREV_DETECT_CORRECT_CHANNELS; i++)
1251 params->dcor.detect_correct[i] = DEF_DETECT_CORRECT_VAL;
1252 memcpy(params->gamma.blue, gamma_table, sizeof(params->gamma.blue));
1253 memcpy(params->gamma.green, gamma_table, sizeof(params->gamma.green));
1254 memcpy(params->gamma.red, gamma_table, sizeof(params->gamma.red));
1255 params->wbal.dgain = FLR_WBAL_DGAIN;
1256 params->wbal.coef0 = FLR_WBAL_COEF;
1257 params->wbal.coef1 = FLR_WBAL_COEF;
1258 params->wbal.coef2 = FLR_WBAL_COEF;
1259 params->wbal.coef3 = FLR_WBAL_COEF;
1260 params->blk_adj.red = FLR_BLKADJ_RED;
1261 params->blk_adj.green = FLR_BLKADJ_GREEN;
1262 params->blk_adj.blue = FLR_BLKADJ_BLUE;
1263 params->rgb2rgb = flr_rgb2rgb;
1264 params->rgb2ycbcr = flr_prev_csc;
1265 params->yclimit.minC = ISPPRV_YC_MIN;
1266 params->yclimit.maxC = ISPPRV_YC_MAX;
1267 params->yclimit.minY = ISPPRV_YC_MIN;
1268 params->yclimit.maxY = ISPPRV_YC_MAX;
1269
1270 params->features = PREV_CFA | PREV_DEFECT_COR | PREV_NOISE_FILTER
1271 | PREV_GAMMA | PREV_BLKADJ | PREV_YCLIMITS
1272 | PREV_RGB2RGB | PREV_COLOR_CONV | PREV_WB
1273 | PREV_BRIGHTNESS | PREV_CONTRAST;
1274
1275 prev->update = PREV_FEATURES_END - 1;
1276}
1277
1278/*
1279 * preview_max_out_width - Handle previewer hardware ouput limitations
1280 * @isp_revision : ISP revision
1281 * returns maximum width output for current isp revision
1282 */
1283static unsigned int preview_max_out_width(struct isp_prev_device *prev)
1284{
1285 struct isp_device *isp = to_isp_device(prev);
1286
1287 switch (isp->revision) {
1288 case ISP_REVISION_1_0:
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001289 return PREV_MAX_OUT_WIDTH;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001290
1291 case ISP_REVISION_2_0:
1292 default:
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001293 return PREV_MAX_OUT_WIDTH_ES2;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001294
1295 case ISP_REVISION_15_0:
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001296 return PREV_MAX_OUT_WIDTH_3630;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001297 }
1298}
1299
1300static void preview_configure(struct isp_prev_device *prev)
1301{
1302 struct isp_device *isp = to_isp_device(prev);
1303 struct v4l2_mbus_framefmt *format;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001304
1305 preview_setup_hw(prev);
1306
1307 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1308 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1309 ISPPRV_PCR_SDRPORT);
1310 else
1311 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1312 ISPPRV_PCR_SDRPORT);
1313
1314 if (prev->output & PREVIEW_OUTPUT_RESIZER)
1315 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1316 ISPPRV_PCR_RSZPORT);
1317 else
1318 isp_reg_clr(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1319 ISPPRV_PCR_RSZPORT);
1320
1321 /* PREV_PAD_SINK */
1322 format = &prev->formats[PREV_PAD_SINK];
1323
1324 preview_adjust_bandwidth(prev);
1325
1326 preview_config_input_size(prev);
1327
1328 if (prev->input == PREVIEW_INPUT_CCDC)
1329 preview_config_inlineoffset(prev, 0);
1330 else
1331 preview_config_inlineoffset(prev,
1332 ALIGN(format->width, 0x20) * 2);
1333
1334 /* PREV_PAD_SOURCE */
1335 format = &prev->formats[PREV_PAD_SOURCE];
1336
1337 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1338 preview_config_outlineoffset(prev,
1339 ALIGN(format->width, 0x10) * 2);
1340
Laurent Pincharte4bc6272011-09-21 07:54:44 -03001341 preview_config_averager(prev, 0);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001342 preview_config_ycpos(prev, format->code);
1343}
1344
1345/* -----------------------------------------------------------------------------
1346 * Interrupt handling
1347 */
1348
1349static void preview_enable_oneshot(struct isp_prev_device *prev)
1350{
1351 struct isp_device *isp = to_isp_device(prev);
1352
1353 /* The PCR.SOURCE bit is automatically reset to 0 when the PCR.ENABLE
1354 * bit is set. As the preview engine is used in single-shot mode, we
1355 * need to set PCR.SOURCE before enabling the preview engine.
1356 */
1357 if (prev->input == PREVIEW_INPUT_MEMORY)
1358 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1359 ISPPRV_PCR_SOURCE);
1360
1361 isp_reg_set(isp, OMAP3_ISP_IOMEM_PREV, ISPPRV_PCR,
1362 ISPPRV_PCR_EN | ISPPRV_PCR_ONESHOT);
1363}
1364
1365void omap3isp_preview_isr_frame_sync(struct isp_prev_device *prev)
1366{
1367 /*
1368 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1369 * condition, the module was paused and now we have a buffer queued
1370 * on the output again. Restart the pipeline if running in continuous
1371 * mode.
1372 */
1373 if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1374 prev->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1375 preview_enable_oneshot(prev);
1376 isp_video_dmaqueue_flags_clr(&prev->video_out);
1377 }
1378}
1379
1380static void preview_isr_buffer(struct isp_prev_device *prev)
1381{
1382 struct isp_pipeline *pipe = to_isp_pipeline(&prev->subdev.entity);
1383 struct isp_buffer *buffer;
1384 int restart = 0;
1385
1386 if (prev->input == PREVIEW_INPUT_MEMORY) {
1387 buffer = omap3isp_video_buffer_next(&prev->video_in,
1388 prev->error);
1389 if (buffer != NULL)
1390 preview_set_inaddr(prev, buffer->isp_addr);
1391 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1392 }
1393
1394 if (prev->output & PREVIEW_OUTPUT_MEMORY) {
1395 buffer = omap3isp_video_buffer_next(&prev->video_out,
1396 prev->error);
1397 if (buffer != NULL) {
1398 preview_set_outaddr(prev, buffer->isp_addr);
1399 restart = 1;
1400 }
1401 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1402 }
1403
1404 switch (prev->state) {
1405 case ISP_PIPELINE_STREAM_SINGLESHOT:
1406 if (isp_pipeline_ready(pipe))
1407 omap3isp_pipeline_set_stream(pipe,
1408 ISP_PIPELINE_STREAM_SINGLESHOT);
1409 break;
1410
1411 case ISP_PIPELINE_STREAM_CONTINUOUS:
1412 /* If an underrun occurs, the video queue operation handler will
1413 * restart the preview engine. Otherwise restart it immediately.
1414 */
1415 if (restart)
1416 preview_enable_oneshot(prev);
1417 break;
1418
1419 case ISP_PIPELINE_STREAM_STOPPED:
1420 default:
1421 return;
1422 }
1423
1424 prev->error = 0;
1425}
1426
1427/*
1428 * omap3isp_preview_isr - ISP preview engine interrupt handler
1429 *
1430 * Manage the preview engine video buffers and configure shadowed registers.
1431 */
1432void omap3isp_preview_isr(struct isp_prev_device *prev)
1433{
1434 unsigned long flags;
1435
1436 if (omap3isp_module_sync_is_stopping(&prev->wait, &prev->stopping))
1437 return;
1438
1439 spin_lock_irqsave(&prev->lock, flags);
1440 if (prev->shadow_update)
1441 goto done;
1442
1443 preview_setup_hw(prev);
1444 preview_config_input_size(prev);
1445
1446done:
1447 spin_unlock_irqrestore(&prev->lock, flags);
1448
1449 if (prev->input == PREVIEW_INPUT_MEMORY ||
1450 prev->output & PREVIEW_OUTPUT_MEMORY)
1451 preview_isr_buffer(prev);
1452 else if (prev->state == ISP_PIPELINE_STREAM_CONTINUOUS)
1453 preview_enable_oneshot(prev);
1454}
1455
1456/* -----------------------------------------------------------------------------
1457 * ISP video operations
1458 */
1459
1460static int preview_video_queue(struct isp_video *video,
1461 struct isp_buffer *buffer)
1462{
1463 struct isp_prev_device *prev = &video->isp->isp_prev;
1464
1465 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1466 preview_set_inaddr(prev, buffer->isp_addr);
1467
1468 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1469 preview_set_outaddr(prev, buffer->isp_addr);
1470
1471 return 0;
1472}
1473
1474static const struct isp_video_operations preview_video_ops = {
1475 .queue = preview_video_queue,
1476};
1477
1478/* -----------------------------------------------------------------------------
1479 * V4L2 subdev operations
1480 */
1481
1482/*
1483 * preview_s_ctrl - Handle set control subdev method
1484 * @ctrl: pointer to v4l2 control structure
1485 */
1486static int preview_s_ctrl(struct v4l2_ctrl *ctrl)
1487{
1488 struct isp_prev_device *prev =
1489 container_of(ctrl->handler, struct isp_prev_device, ctrls);
1490
1491 switch (ctrl->id) {
1492 case V4L2_CID_BRIGHTNESS:
1493 preview_update_brightness(prev, ctrl->val);
1494 break;
1495 case V4L2_CID_CONTRAST:
1496 preview_update_contrast(prev, ctrl->val);
1497 break;
1498 }
1499
1500 return 0;
1501}
1502
1503static const struct v4l2_ctrl_ops preview_ctrl_ops = {
1504 .s_ctrl = preview_s_ctrl,
1505};
1506
1507/*
1508 * preview_ioctl - Handle preview module private ioctl's
1509 * @prev: pointer to preview context structure
1510 * @cmd: configuration command
1511 * @arg: configuration argument
1512 * return -EINVAL or zero on success
1513 */
1514static long preview_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1515{
1516 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1517
1518 switch (cmd) {
1519 case VIDIOC_OMAP3ISP_PRV_CFG:
1520 return preview_config(prev, arg);
1521
1522 default:
1523 return -ENOIOCTLCMD;
1524 }
1525}
1526
1527/*
1528 * preview_set_stream - Enable/Disable streaming on preview subdev
1529 * @sd : pointer to v4l2 subdev structure
1530 * @enable: 1 == Enable, 0 == Disable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001531 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001532 */
1533static int preview_set_stream(struct v4l2_subdev *sd, int enable)
1534{
1535 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1536 struct isp_video *video_out = &prev->video_out;
1537 struct isp_device *isp = to_isp_device(prev);
1538 struct device *dev = to_device(prev);
1539 unsigned long flags;
1540
1541 if (prev->state == ISP_PIPELINE_STREAM_STOPPED) {
1542 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1543 return 0;
1544
1545 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1546 preview_configure(prev);
1547 atomic_set(&prev->stopping, 0);
1548 prev->error = 0;
1549 preview_print_status(prev);
1550 }
1551
1552 switch (enable) {
1553 case ISP_PIPELINE_STREAM_CONTINUOUS:
1554 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1555 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1556
1557 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED ||
1558 !(prev->output & PREVIEW_OUTPUT_MEMORY))
1559 preview_enable_oneshot(prev);
1560
1561 isp_video_dmaqueue_flags_clr(video_out);
1562 break;
1563
1564 case ISP_PIPELINE_STREAM_SINGLESHOT:
1565 if (prev->input == PREVIEW_INPUT_MEMORY)
1566 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1567 if (prev->output & PREVIEW_OUTPUT_MEMORY)
1568 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1569
1570 preview_enable_oneshot(prev);
1571 break;
1572
1573 case ISP_PIPELINE_STREAM_STOPPED:
1574 if (omap3isp_module_sync_idle(&sd->entity, &prev->wait,
1575 &prev->stopping))
1576 dev_dbg(dev, "%s: stop timeout.\n", sd->name);
1577 spin_lock_irqsave(&prev->lock, flags);
1578 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_READ);
1579 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_PREVIEW_WRITE);
1580 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_PREVIEW);
1581 spin_unlock_irqrestore(&prev->lock, flags);
1582 isp_video_dmaqueue_flags_clr(video_out);
1583 break;
1584 }
1585
1586 prev->state = enable;
1587 return 0;
1588}
1589
1590static struct v4l2_mbus_framefmt *
1591__preview_get_format(struct isp_prev_device *prev, struct v4l2_subdev_fh *fh,
1592 unsigned int pad, enum v4l2_subdev_format_whence which)
1593{
1594 if (which == V4L2_SUBDEV_FORMAT_TRY)
1595 return v4l2_subdev_get_try_format(fh, pad);
1596 else
1597 return &prev->formats[pad];
1598}
1599
1600/* previewer format descriptions */
1601static const unsigned int preview_input_fmts[] = {
1602 V4L2_MBUS_FMT_SGRBG10_1X10,
1603 V4L2_MBUS_FMT_SRGGB10_1X10,
1604 V4L2_MBUS_FMT_SBGGR10_1X10,
1605 V4L2_MBUS_FMT_SGBRG10_1X10,
1606};
1607
1608static const unsigned int preview_output_fmts[] = {
1609 V4L2_MBUS_FMT_UYVY8_1X16,
1610 V4L2_MBUS_FMT_YUYV8_1X16,
1611};
1612
1613/*
1614 * preview_try_format - Handle try format by pad subdev method
1615 * @prev: ISP preview device
1616 * @fh : V4L2 subdev file handle
1617 * @pad: pad num
1618 * @fmt: pointer to v4l2 format structure
1619 */
1620static void preview_try_format(struct isp_prev_device *prev,
1621 struct v4l2_subdev_fh *fh, unsigned int pad,
1622 struct v4l2_mbus_framefmt *fmt,
1623 enum v4l2_subdev_format_whence which)
1624{
1625 struct v4l2_mbus_framefmt *format;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001626 enum v4l2_mbus_pixelcode pixelcode;
1627 unsigned int i;
1628
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001629 switch (pad) {
1630 case PREV_PAD_SINK:
1631 /* When reading data from the CCDC, the input size has already
1632 * been mangled by the CCDC output pad so it can be accepted
1633 * as-is.
1634 *
1635 * When reading data from memory, clamp the requested width and
1636 * height. The TRM doesn't specify a minimum input height, make
1637 * sure we got enough lines to enable the noise filter and color
1638 * filter array interpolation.
1639 */
1640 if (prev->input == PREVIEW_INPUT_MEMORY) {
Laurent Pinchart059dc1d2011-10-03 07:56:15 -03001641 fmt->width = clamp_t(u32, fmt->width, PREV_MIN_IN_WIDTH,
1642 preview_max_out_width(prev));
1643 fmt->height = clamp_t(u32, fmt->height,
1644 PREV_MIN_IN_HEIGHT,
1645 PREV_MAX_IN_HEIGHT);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001646 }
1647
1648 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1649
1650 for (i = 0; i < ARRAY_SIZE(preview_input_fmts); i++) {
1651 if (fmt->code == preview_input_fmts[i])
1652 break;
1653 }
1654
1655 /* If not found, use SGRBG10 as default */
1656 if (i >= ARRAY_SIZE(preview_input_fmts))
1657 fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
1658 break;
1659
1660 case PREV_PAD_SOURCE:
1661 pixelcode = fmt->code;
1662 format = __preview_get_format(prev, fh, PREV_PAD_SINK, which);
1663 memcpy(fmt, format, sizeof(*fmt));
1664
1665 /* The preview module output size is configurable through the
1666 * input interface (horizontal and vertical cropping) and the
1667 * averager (horizontal scaling by 1/1, 1/2, 1/4 or 1/8). In
1668 * spite of this, hardcode the output size to the biggest
1669 * possible value for simplicity reasons.
1670 */
1671 switch (pixelcode) {
1672 case V4L2_MBUS_FMT_YUYV8_1X16:
1673 case V4L2_MBUS_FMT_UYVY8_1X16:
1674 fmt->code = pixelcode;
1675 break;
1676
1677 default:
1678 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1679 break;
1680 }
1681
1682 /* The TRM states (12.1.4.7.1.2) that 2 pixels must be cropped
1683 * from the left and right sides when the input source is the
1684 * CCDC. This seems not to be needed in practice, investigation
1685 * is required.
1686 */
1687 if (prev->input == PREVIEW_INPUT_CCDC)
1688 fmt->width -= 4;
1689
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001690 /* Assume that all blocks are enabled and crop pixels and lines
1691 * accordingly. See preview_config_input_size() for more
1692 * information.
1693 */
1694 fmt->width -= 14;
1695 fmt->height -= 8;
1696
1697 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1698 break;
1699 }
1700
1701 fmt->field = V4L2_FIELD_NONE;
1702}
1703
1704/*
1705 * preview_enum_mbus_code - Handle pixel format enumeration
1706 * @sd : pointer to v4l2 subdev structure
1707 * @fh : V4L2 subdev file handle
1708 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1709 * return -EINVAL or zero on success
1710 */
1711static int preview_enum_mbus_code(struct v4l2_subdev *sd,
1712 struct v4l2_subdev_fh *fh,
1713 struct v4l2_subdev_mbus_code_enum *code)
1714{
1715 switch (code->pad) {
1716 case PREV_PAD_SINK:
1717 if (code->index >= ARRAY_SIZE(preview_input_fmts))
1718 return -EINVAL;
1719
1720 code->code = preview_input_fmts[code->index];
1721 break;
1722 case PREV_PAD_SOURCE:
1723 if (code->index >= ARRAY_SIZE(preview_output_fmts))
1724 return -EINVAL;
1725
1726 code->code = preview_output_fmts[code->index];
1727 break;
1728 default:
1729 return -EINVAL;
1730 }
1731
1732 return 0;
1733}
1734
1735static int preview_enum_frame_size(struct v4l2_subdev *sd,
1736 struct v4l2_subdev_fh *fh,
1737 struct v4l2_subdev_frame_size_enum *fse)
1738{
1739 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1740 struct v4l2_mbus_framefmt format;
1741
1742 if (fse->index != 0)
1743 return -EINVAL;
1744
1745 format.code = fse->code;
1746 format.width = 1;
1747 format.height = 1;
1748 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1749 fse->min_width = format.width;
1750 fse->min_height = format.height;
1751
1752 if (format.code != fse->code)
1753 return -EINVAL;
1754
1755 format.code = fse->code;
1756 format.width = -1;
1757 format.height = -1;
1758 preview_try_format(prev, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1759 fse->max_width = format.width;
1760 fse->max_height = format.height;
1761
1762 return 0;
1763}
1764
1765/*
1766 * preview_get_format - Handle get format by pads subdev method
1767 * @sd : pointer to v4l2 subdev structure
1768 * @fh : V4L2 subdev file handle
1769 * @fmt: pointer to v4l2 subdev format structure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001770 * return -EINVAL or zero on success
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001771 */
1772static int preview_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1773 struct v4l2_subdev_format *fmt)
1774{
1775 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1776 struct v4l2_mbus_framefmt *format;
1777
1778 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
1779 if (format == NULL)
1780 return -EINVAL;
1781
1782 fmt->format = *format;
1783 return 0;
1784}
1785
1786/*
1787 * preview_set_format - Handle set format by pads subdev method
1788 * @sd : pointer to v4l2 subdev structure
1789 * @fh : V4L2 subdev file handle
1790 * @fmt: pointer to v4l2 subdev format structure
1791 * return -EINVAL or zero on success
1792 */
1793static int preview_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1794 struct v4l2_subdev_format *fmt)
1795{
1796 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1797 struct v4l2_mbus_framefmt *format;
1798
1799 format = __preview_get_format(prev, fh, fmt->pad, fmt->which);
1800 if (format == NULL)
1801 return -EINVAL;
1802
1803 preview_try_format(prev, fh, fmt->pad, &fmt->format, fmt->which);
1804 *format = fmt->format;
1805
1806 /* Propagate the format from sink to source */
1807 if (fmt->pad == PREV_PAD_SINK) {
1808 format = __preview_get_format(prev, fh, PREV_PAD_SOURCE,
1809 fmt->which);
1810 *format = fmt->format;
1811 preview_try_format(prev, fh, PREV_PAD_SOURCE, format,
1812 fmt->which);
1813 }
1814
1815 return 0;
1816}
1817
1818/*
1819 * preview_init_formats - Initialize formats on all pads
1820 * @sd: ISP preview V4L2 subdevice
1821 * @fh: V4L2 subdev file handle
1822 *
1823 * Initialize all pad formats with default values. If fh is not NULL, try
1824 * formats are initialized on the file handle. Otherwise active formats are
1825 * initialized on the device.
1826 */
1827static int preview_init_formats(struct v4l2_subdev *sd,
1828 struct v4l2_subdev_fh *fh)
1829{
1830 struct v4l2_subdev_format format;
1831
1832 memset(&format, 0, sizeof(format));
1833 format.pad = PREV_PAD_SINK;
1834 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1835 format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
1836 format.format.width = 4096;
1837 format.format.height = 4096;
1838 preview_set_format(sd, fh, &format);
1839
1840 return 0;
1841}
1842
1843/* subdev core operations */
1844static const struct v4l2_subdev_core_ops preview_v4l2_core_ops = {
1845 .ioctl = preview_ioctl,
1846};
1847
1848/* subdev video operations */
1849static const struct v4l2_subdev_video_ops preview_v4l2_video_ops = {
1850 .s_stream = preview_set_stream,
1851};
1852
1853/* subdev pad operations */
1854static const struct v4l2_subdev_pad_ops preview_v4l2_pad_ops = {
1855 .enum_mbus_code = preview_enum_mbus_code,
1856 .enum_frame_size = preview_enum_frame_size,
1857 .get_fmt = preview_get_format,
1858 .set_fmt = preview_set_format,
1859};
1860
1861/* subdev operations */
1862static const struct v4l2_subdev_ops preview_v4l2_ops = {
1863 .core = &preview_v4l2_core_ops,
1864 .video = &preview_v4l2_video_ops,
1865 .pad = &preview_v4l2_pad_ops,
1866};
1867
1868/* subdev internal operations */
1869static const struct v4l2_subdev_internal_ops preview_v4l2_internal_ops = {
1870 .open = preview_init_formats,
1871};
1872
1873/* -----------------------------------------------------------------------------
1874 * Media entity operations
1875 */
1876
1877/*
1878 * preview_link_setup - Setup previewer connections.
1879 * @entity : Pointer to media entity structure
1880 * @local : Pointer to local pad array
1881 * @remote : Pointer to remote pad array
1882 * @flags : Link flags
1883 * return -EINVAL or zero on success
1884 */
1885static int preview_link_setup(struct media_entity *entity,
1886 const struct media_pad *local,
1887 const struct media_pad *remote, u32 flags)
1888{
1889 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1890 struct isp_prev_device *prev = v4l2_get_subdevdata(sd);
1891
1892 switch (local->index | media_entity_type(remote->entity)) {
1893 case PREV_PAD_SINK | MEDIA_ENT_T_DEVNODE:
1894 /* read from memory */
1895 if (flags & MEDIA_LNK_FL_ENABLED) {
1896 if (prev->input == PREVIEW_INPUT_CCDC)
1897 return -EBUSY;
1898 prev->input = PREVIEW_INPUT_MEMORY;
1899 } else {
1900 if (prev->input == PREVIEW_INPUT_MEMORY)
1901 prev->input = PREVIEW_INPUT_NONE;
1902 }
1903 break;
1904
1905 case PREV_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
1906 /* read from ccdc */
1907 if (flags & MEDIA_LNK_FL_ENABLED) {
1908 if (prev->input == PREVIEW_INPUT_MEMORY)
1909 return -EBUSY;
1910 prev->input = PREVIEW_INPUT_CCDC;
1911 } else {
1912 if (prev->input == PREVIEW_INPUT_CCDC)
1913 prev->input = PREVIEW_INPUT_NONE;
1914 }
1915 break;
1916
1917 /*
1918 * The ISP core doesn't support pipelines with multiple video outputs.
1919 * Revisit this when it will be implemented, and return -EBUSY for now.
1920 */
1921
1922 case PREV_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
1923 /* write to memory */
1924 if (flags & MEDIA_LNK_FL_ENABLED) {
1925 if (prev->output & ~PREVIEW_OUTPUT_MEMORY)
1926 return -EBUSY;
1927 prev->output |= PREVIEW_OUTPUT_MEMORY;
1928 } else {
1929 prev->output &= ~PREVIEW_OUTPUT_MEMORY;
1930 }
1931 break;
1932
1933 case PREV_PAD_SOURCE | MEDIA_ENT_T_V4L2_SUBDEV:
1934 /* write to resizer */
1935 if (flags & MEDIA_LNK_FL_ENABLED) {
1936 if (prev->output & ~PREVIEW_OUTPUT_RESIZER)
1937 return -EBUSY;
1938 prev->output |= PREVIEW_OUTPUT_RESIZER;
1939 } else {
1940 prev->output &= ~PREVIEW_OUTPUT_RESIZER;
1941 }
1942 break;
1943
1944 default:
1945 return -EINVAL;
1946 }
1947
1948 return 0;
1949}
1950
1951/* media operations */
1952static const struct media_entity_operations preview_media_ops = {
1953 .link_setup = preview_link_setup,
1954};
1955
Laurent Pinchart39099d02011-09-22 16:59:26 -03001956void omap3isp_preview_unregister_entities(struct isp_prev_device *prev)
1957{
1958 v4l2_device_unregister_subdev(&prev->subdev);
1959 omap3isp_video_unregister(&prev->video_in);
1960 omap3isp_video_unregister(&prev->video_out);
1961}
1962
1963int omap3isp_preview_register_entities(struct isp_prev_device *prev,
1964 struct v4l2_device *vdev)
1965{
1966 int ret;
1967
1968 /* Register the subdev and video nodes. */
1969 ret = v4l2_device_register_subdev(vdev, &prev->subdev);
1970 if (ret < 0)
1971 goto error;
1972
1973 ret = omap3isp_video_register(&prev->video_in, vdev);
1974 if (ret < 0)
1975 goto error;
1976
1977 ret = omap3isp_video_register(&prev->video_out, vdev);
1978 if (ret < 0)
1979 goto error;
1980
1981 return 0;
1982
1983error:
1984 omap3isp_preview_unregister_entities(prev);
1985 return ret;
1986}
1987
1988/* -----------------------------------------------------------------------------
1989 * ISP previewer initialisation and cleanup
1990 */
1991
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001992/*
Laurent Pinchart39099d02011-09-22 16:59:26 -03001993 * preview_init_entities - Initialize subdev and media entity.
Laurent Pinchartde1135d2011-02-12 18:05:06 -03001994 * @prev : Pointer to preview structure
1995 * return -ENOMEM or zero on success
1996 */
1997static int preview_init_entities(struct isp_prev_device *prev)
1998{
1999 struct v4l2_subdev *sd = &prev->subdev;
2000 struct media_pad *pads = prev->pads;
2001 struct media_entity *me = &sd->entity;
2002 int ret;
2003
2004 prev->input = PREVIEW_INPUT_NONE;
2005
2006 v4l2_subdev_init(sd, &preview_v4l2_ops);
2007 sd->internal_ops = &preview_v4l2_internal_ops;
2008 strlcpy(sd->name, "OMAP3 ISP preview", sizeof(sd->name));
2009 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
2010 v4l2_set_subdevdata(sd, prev);
2011 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2012
2013 v4l2_ctrl_handler_init(&prev->ctrls, 2);
2014 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_BRIGHTNESS,
2015 ISPPRV_BRIGHT_LOW, ISPPRV_BRIGHT_HIGH,
2016 ISPPRV_BRIGHT_STEP, ISPPRV_BRIGHT_DEF);
2017 v4l2_ctrl_new_std(&prev->ctrls, &preview_ctrl_ops, V4L2_CID_CONTRAST,
2018 ISPPRV_CONTRAST_LOW, ISPPRV_CONTRAST_HIGH,
2019 ISPPRV_CONTRAST_STEP, ISPPRV_CONTRAST_DEF);
2020 v4l2_ctrl_handler_setup(&prev->ctrls);
2021 sd->ctrl_handler = &prev->ctrls;
2022
2023 pads[PREV_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2024 pads[PREV_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2025
2026 me->ops = &preview_media_ops;
2027 ret = media_entity_init(me, PREV_PADS_NUM, pads, 0);
2028 if (ret < 0)
2029 return ret;
2030
2031 preview_init_formats(sd, NULL);
2032
2033 /* According to the OMAP34xx TRM, video buffers need to be aligned on a
2034 * 32 bytes boundary. However, an undocumented hardware bug requires a
2035 * 64 bytes boundary at the preview engine input.
2036 */
2037 prev->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2038 prev->video_in.ops = &preview_video_ops;
2039 prev->video_in.isp = to_isp_device(prev);
2040 prev->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2041 prev->video_in.bpl_alignment = 64;
2042 prev->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2043 prev->video_out.ops = &preview_video_ops;
2044 prev->video_out.isp = to_isp_device(prev);
2045 prev->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
2046 prev->video_out.bpl_alignment = 32;
2047
2048 ret = omap3isp_video_init(&prev->video_in, "preview");
2049 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002050 goto error_video_in;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002051
2052 ret = omap3isp_video_init(&prev->video_out, "preview");
2053 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002054 goto error_video_out;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002055
2056 /* Connect the video nodes to the previewer subdev. */
2057 ret = media_entity_create_link(&prev->video_in.video.entity, 0,
2058 &prev->subdev.entity, PREV_PAD_SINK, 0);
2059 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002060 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002061
2062 ret = media_entity_create_link(&prev->subdev.entity, PREV_PAD_SOURCE,
2063 &prev->video_out.video.entity, 0, 0);
2064 if (ret < 0)
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002065 goto error_link;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002066
2067 return 0;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002068
2069error_link:
2070 omap3isp_video_cleanup(&prev->video_out);
2071error_video_out:
2072 omap3isp_video_cleanup(&prev->video_in);
2073error_video_in:
2074 media_entity_cleanup(&prev->subdev.entity);
2075 return ret;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002076}
2077
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002078/*
2079 * isp_preview_init - Previewer initialization.
2080 * @dev : Pointer to ISP device
2081 * return -ENOMEM or zero on success
2082 */
2083int omap3isp_preview_init(struct isp_device *isp)
2084{
2085 struct isp_prev_device *prev = &isp->isp_prev;
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002086
2087 spin_lock_init(&prev->lock);
2088 init_waitqueue_head(&prev->wait);
2089 preview_init_params(prev);
2090
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03002091 return preview_init_entities(prev);
Laurent Pinchartde1135d2011-02-12 18:05:06 -03002092}
Laurent Pinchart39099d02011-09-22 16:59:26 -03002093
2094void omap3isp_preview_cleanup(struct isp_device *isp)
2095{
2096 struct isp_prev_device *prev = &isp->isp_prev;
2097
2098 v4l2_ctrl_handler_free(&prev->ctrls);
2099 omap3isp_video_cleanup(&prev->video_in);
2100 omap3isp_video_cleanup(&prev->video_out);
2101 media_entity_cleanup(&prev->subdev.entity);
2102}