sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / platform / omap3isp / ispresizer.c
blob0b6a87508584f4eb5201f3f2b0bf284c1b7380f7
1 /*
2 * ispresizer.c
4 * TI OMAP3 ISP - Resizer module
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/device.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
21 #include "isp.h"
22 #include "ispreg.h"
23 #include "ispresizer.h"
26 * Resizer Constants
28 #define MIN_RESIZE_VALUE 64
29 #define MID_RESIZE_VALUE 512
30 #define MAX_RESIZE_VALUE 1024
32 #define MIN_IN_WIDTH 32
33 #define MIN_IN_HEIGHT 32
34 #define MAX_IN_WIDTH_MEMORY_MODE 4095
35 #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES1 1280
36 #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES2 4095
37 #define MAX_IN_HEIGHT 4095
39 #define MIN_OUT_WIDTH 16
40 #define MIN_OUT_HEIGHT 2
41 #define MAX_OUT_HEIGHT 4095
44 * Resizer Use Constraints
45 * "TRM ES3.1, table 12-46"
47 #define MAX_4TAP_OUT_WIDTH_ES1 1280
48 #define MAX_7TAP_OUT_WIDTH_ES1 640
49 #define MAX_4TAP_OUT_WIDTH_ES2 3312
50 #define MAX_7TAP_OUT_WIDTH_ES2 1650
51 #define MAX_4TAP_OUT_WIDTH_3630 4096
52 #define MAX_7TAP_OUT_WIDTH_3630 2048
55 * Constants for ratio calculation
57 #define RESIZE_DIVISOR 256
58 #define DEFAULT_PHASE 1
61 * Default (and only) configuration of filter coefficients.
62 * 7-tap mode is for scale factors 0.25x to 0.5x.
63 * 4-tap mode is for scale factors 0.5x to 4.0x.
64 * There shouldn't be any reason to recalculate these, EVER.
66 static const struct isprsz_coef filter_coefs = {
67 /* For 8-phase 4-tap horizontal filter: */
69 0x0000, 0x0100, 0x0000, 0x0000,
70 0x03FA, 0x00F6, 0x0010, 0x0000,
71 0x03F9, 0x00DB, 0x002C, 0x0000,
72 0x03FB, 0x00B3, 0x0053, 0x03FF,
73 0x03FD, 0x0082, 0x0084, 0x03FD,
74 0x03FF, 0x0053, 0x00B3, 0x03FB,
75 0x0000, 0x002C, 0x00DB, 0x03F9,
76 0x0000, 0x0010, 0x00F6, 0x03FA
78 /* For 8-phase 4-tap vertical filter: */
80 0x0000, 0x0100, 0x0000, 0x0000,
81 0x03FA, 0x00F6, 0x0010, 0x0000,
82 0x03F9, 0x00DB, 0x002C, 0x0000,
83 0x03FB, 0x00B3, 0x0053, 0x03FF,
84 0x03FD, 0x0082, 0x0084, 0x03FD,
85 0x03FF, 0x0053, 0x00B3, 0x03FB,
86 0x0000, 0x002C, 0x00DB, 0x03F9,
87 0x0000, 0x0010, 0x00F6, 0x03FA
89 /* For 4-phase 7-tap horizontal filter: */
90 #define DUMMY 0
92 0x0004, 0x0023, 0x005A, 0x0058, 0x0023, 0x0004, 0x0000, DUMMY,
93 0x0002, 0x0018, 0x004d, 0x0060, 0x0031, 0x0008, 0x0000, DUMMY,
94 0x0001, 0x000f, 0x003f, 0x0062, 0x003f, 0x000f, 0x0001, DUMMY,
95 0x0000, 0x0008, 0x0031, 0x0060, 0x004d, 0x0018, 0x0002, DUMMY
97 /* For 4-phase 7-tap vertical filter: */
99 0x0004, 0x0023, 0x005A, 0x0058, 0x0023, 0x0004, 0x0000, DUMMY,
100 0x0002, 0x0018, 0x004d, 0x0060, 0x0031, 0x0008, 0x0000, DUMMY,
101 0x0001, 0x000f, 0x003f, 0x0062, 0x003f, 0x000f, 0x0001, DUMMY,
102 0x0000, 0x0008, 0x0031, 0x0060, 0x004d, 0x0018, 0x0002, DUMMY
105 * The dummy padding is required in 7-tap mode because of how the
106 * registers are arranged physically.
108 #undef DUMMY
112 * __resizer_get_format - helper function for getting resizer format
113 * @res : pointer to resizer private structure
114 * @pad : pad number
115 * @cfg: V4L2 subdev pad configuration
116 * @which : wanted subdev format
117 * return zero
119 static struct v4l2_mbus_framefmt *
120 __resizer_get_format(struct isp_res_device *res, struct v4l2_subdev_pad_config *cfg,
121 unsigned int pad, enum v4l2_subdev_format_whence which)
123 if (which == V4L2_SUBDEV_FORMAT_TRY)
124 return v4l2_subdev_get_try_format(&res->subdev, cfg, pad);
125 else
126 return &res->formats[pad];
130 * __resizer_get_crop - helper function for getting resizer crop rectangle
131 * @res : pointer to resizer private structure
132 * @cfg: V4L2 subdev pad configuration
133 * @which : wanted subdev crop rectangle
135 static struct v4l2_rect *
136 __resizer_get_crop(struct isp_res_device *res, struct v4l2_subdev_pad_config *cfg,
137 enum v4l2_subdev_format_whence which)
139 if (which == V4L2_SUBDEV_FORMAT_TRY)
140 return v4l2_subdev_get_try_crop(&res->subdev, cfg, RESZ_PAD_SINK);
141 else
142 return &res->crop.request;
146 * resizer_set_filters - Set resizer filters
147 * @res: Device context.
148 * @h_coeff: horizontal coefficient
149 * @v_coeff: vertical coefficient
150 * Return none
152 static void resizer_set_filters(struct isp_res_device *res, const u16 *h_coeff,
153 const u16 *v_coeff)
155 struct isp_device *isp = to_isp_device(res);
156 u32 startaddr_h, startaddr_v, tmp_h, tmp_v;
157 int i;
159 startaddr_h = ISPRSZ_HFILT10;
160 startaddr_v = ISPRSZ_VFILT10;
162 for (i = 0; i < COEFF_CNT; i += 2) {
163 tmp_h = h_coeff[i] |
164 (h_coeff[i + 1] << ISPRSZ_HFILT_COEF1_SHIFT);
165 tmp_v = v_coeff[i] |
166 (v_coeff[i + 1] << ISPRSZ_VFILT_COEF1_SHIFT);
167 isp_reg_writel(isp, tmp_h, OMAP3_ISP_IOMEM_RESZ, startaddr_h);
168 isp_reg_writel(isp, tmp_v, OMAP3_ISP_IOMEM_RESZ, startaddr_v);
169 startaddr_h += 4;
170 startaddr_v += 4;
175 * resizer_set_bilinear - Chrominance horizontal algorithm select
176 * @res: Device context.
177 * @type: Filtering interpolation type.
179 * Filtering that is same as luminance processing is
180 * intended only for downsampling, and bilinear interpolation
181 * is intended only for upsampling.
183 static void resizer_set_bilinear(struct isp_res_device *res,
184 enum resizer_chroma_algo type)
186 struct isp_device *isp = to_isp_device(res);
188 if (type == RSZ_BILINEAR)
189 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
190 ISPRSZ_CNT_CBILIN);
191 else
192 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
193 ISPRSZ_CNT_CBILIN);
197 * resizer_set_ycpos - Luminance and chrominance order
198 * @res: Device context.
199 * @pixelcode: pixel code.
201 static void resizer_set_ycpos(struct isp_res_device *res, u32 pixelcode)
203 struct isp_device *isp = to_isp_device(res);
205 switch (pixelcode) {
206 case MEDIA_BUS_FMT_YUYV8_1X16:
207 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
208 ISPRSZ_CNT_YCPOS);
209 break;
210 case MEDIA_BUS_FMT_UYVY8_1X16:
211 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
212 ISPRSZ_CNT_YCPOS);
213 break;
214 default:
215 return;
220 * resizer_set_phase - Setup horizontal and vertical starting phase
221 * @res: Device context.
222 * @h_phase: horizontal phase parameters.
223 * @v_phase: vertical phase parameters.
225 * Horizontal and vertical phase range is 0 to 7
227 static void resizer_set_phase(struct isp_res_device *res, u32 h_phase,
228 u32 v_phase)
230 struct isp_device *isp = to_isp_device(res);
231 u32 rgval;
233 rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
234 ~(ISPRSZ_CNT_HSTPH_MASK | ISPRSZ_CNT_VSTPH_MASK);
235 rgval |= (h_phase << ISPRSZ_CNT_HSTPH_SHIFT) & ISPRSZ_CNT_HSTPH_MASK;
236 rgval |= (v_phase << ISPRSZ_CNT_VSTPH_SHIFT) & ISPRSZ_CNT_VSTPH_MASK;
238 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
242 * resizer_set_luma - Setup luminance enhancer parameters
243 * @res: Device context.
244 * @luma: Structure for luminance enhancer parameters.
246 * Algorithm select:
247 * 0x0: Disable
248 * 0x1: [-1 2 -1]/2 high-pass filter
249 * 0x2: [-1 -2 6 -2 -1]/4 high-pass filter
251 * Maximum gain:
252 * The data is coded in U4Q4 representation.
254 * Slope:
255 * The data is coded in U4Q4 representation.
257 * Coring offset:
258 * The data is coded in U8Q0 representation.
260 * The new luminance value is computed as:
261 * Y += HPF(Y) x max(GAIN, (HPF(Y) - CORE) x SLOP + 8) >> 4.
263 static void resizer_set_luma(struct isp_res_device *res,
264 struct resizer_luma_yenh *luma)
266 struct isp_device *isp = to_isp_device(res);
267 u32 rgval;
269 rgval = (luma->algo << ISPRSZ_YENH_ALGO_SHIFT)
270 & ISPRSZ_YENH_ALGO_MASK;
271 rgval |= (luma->gain << ISPRSZ_YENH_GAIN_SHIFT)
272 & ISPRSZ_YENH_GAIN_MASK;
273 rgval |= (luma->slope << ISPRSZ_YENH_SLOP_SHIFT)
274 & ISPRSZ_YENH_SLOP_MASK;
275 rgval |= (luma->core << ISPRSZ_YENH_CORE_SHIFT)
276 & ISPRSZ_YENH_CORE_MASK;
278 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_YENH);
282 * resizer_set_source - Input source select
283 * @res: Device context.
284 * @source: Input source type
286 * If this field is set to RESIZER_INPUT_VP, the resizer input is fed from
287 * Preview/CCDC engine, otherwise from memory.
289 static void resizer_set_source(struct isp_res_device *res,
290 enum resizer_input_entity source)
292 struct isp_device *isp = to_isp_device(res);
294 if (source == RESIZER_INPUT_MEMORY)
295 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
296 ISPRSZ_CNT_INPSRC);
297 else
298 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
299 ISPRSZ_CNT_INPSRC);
303 * resizer_set_ratio - Setup horizontal and vertical resizing value
304 * @res: Device context.
305 * @ratio: Structure for ratio parameters.
307 * Resizing range from 64 to 1024
309 static void resizer_set_ratio(struct isp_res_device *res,
310 const struct resizer_ratio *ratio)
312 struct isp_device *isp = to_isp_device(res);
313 const u16 *h_filter, *v_filter;
314 u32 rgval;
316 rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
317 ~(ISPRSZ_CNT_HRSZ_MASK | ISPRSZ_CNT_VRSZ_MASK);
318 rgval |= ((ratio->horz - 1) << ISPRSZ_CNT_HRSZ_SHIFT)
319 & ISPRSZ_CNT_HRSZ_MASK;
320 rgval |= ((ratio->vert - 1) << ISPRSZ_CNT_VRSZ_SHIFT)
321 & ISPRSZ_CNT_VRSZ_MASK;
322 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
324 /* prepare horizontal filter coefficients */
325 if (ratio->horz > MID_RESIZE_VALUE)
326 h_filter = &filter_coefs.h_filter_coef_7tap[0];
327 else
328 h_filter = &filter_coefs.h_filter_coef_4tap[0];
330 /* prepare vertical filter coefficients */
331 if (ratio->vert > MID_RESIZE_VALUE)
332 v_filter = &filter_coefs.v_filter_coef_7tap[0];
333 else
334 v_filter = &filter_coefs.v_filter_coef_4tap[0];
336 resizer_set_filters(res, h_filter, v_filter);
340 * resizer_set_dst_size - Setup the output height and width
341 * @res: Device context.
342 * @width: Output width.
343 * @height: Output height.
345 * Width :
346 * The value must be EVEN.
348 * Height:
349 * The number of bytes written to SDRAM must be
350 * a multiple of 16-bytes if the vertical resizing factor
351 * is greater than 1x (upsizing)
353 static void resizer_set_output_size(struct isp_res_device *res,
354 u32 width, u32 height)
356 struct isp_device *isp = to_isp_device(res);
357 u32 rgval;
359 rgval = (width << ISPRSZ_OUT_SIZE_HORZ_SHIFT)
360 & ISPRSZ_OUT_SIZE_HORZ_MASK;
361 rgval |= (height << ISPRSZ_OUT_SIZE_VERT_SHIFT)
362 & ISPRSZ_OUT_SIZE_VERT_MASK;
363 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_OUT_SIZE);
367 * resizer_set_output_offset - Setup memory offset for the output lines.
368 * @res: Device context.
369 * @offset: Memory offset.
371 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
372 * boundary; the 5 LSBs are read-only. For optimal use of SDRAM bandwidth,
373 * the SDRAM line offset must be set on a 256-byte boundary
375 static void resizer_set_output_offset(struct isp_res_device *res, u32 offset)
377 struct isp_device *isp = to_isp_device(res);
379 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTOFF);
383 * resizer_set_start - Setup vertical and horizontal start position
384 * @res: Device context.
385 * @left: Horizontal start position.
386 * @top: Vertical start position.
388 * Vertical start line:
389 * This field makes sense only when the resizer obtains its input
390 * from the preview engine/CCDC
392 * Horizontal start pixel:
393 * Pixels are coded on 16 bits for YUV and 8 bits for color separate data.
394 * When the resizer gets its input from SDRAM, this field must be set
395 * to <= 15 for YUV 16-bit data and <= 31 for 8-bit color separate data
397 static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top)
399 struct isp_device *isp = to_isp_device(res);
400 u32 rgval;
402 rgval = (left << ISPRSZ_IN_START_HORZ_ST_SHIFT)
403 & ISPRSZ_IN_START_HORZ_ST_MASK;
404 rgval |= (top << ISPRSZ_IN_START_VERT_ST_SHIFT)
405 & ISPRSZ_IN_START_VERT_ST_MASK;
407 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_START);
411 * resizer_set_input_size - Setup the input size
412 * @res: Device context.
413 * @width: The range is 0 to 4095 pixels
414 * @height: The range is 0 to 4095 lines
416 static void resizer_set_input_size(struct isp_res_device *res,
417 u32 width, u32 height)
419 struct isp_device *isp = to_isp_device(res);
420 u32 rgval;
422 rgval = (width << ISPRSZ_IN_SIZE_HORZ_SHIFT)
423 & ISPRSZ_IN_SIZE_HORZ_MASK;
424 rgval |= (height << ISPRSZ_IN_SIZE_VERT_SHIFT)
425 & ISPRSZ_IN_SIZE_VERT_MASK;
427 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_SIZE);
431 * resizer_set_src_offs - Setup the memory offset for the input lines
432 * @res: Device context.
433 * @offset: Memory offset.
435 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
436 * boundary; the 5 LSBs are read-only. This field must be programmed to be
437 * 0x0 if the resizer input is from preview engine/CCDC.
439 static void resizer_set_input_offset(struct isp_res_device *res, u32 offset)
441 struct isp_device *isp = to_isp_device(res);
443 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INOFF);
447 * resizer_set_intype - Input type select
448 * @res: Device context.
449 * @type: Pixel format type.
451 static void resizer_set_intype(struct isp_res_device *res,
452 enum resizer_colors_type type)
454 struct isp_device *isp = to_isp_device(res);
456 if (type == RSZ_COLOR8)
457 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
458 ISPRSZ_CNT_INPTYP);
459 else
460 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
461 ISPRSZ_CNT_INPTYP);
465 * __resizer_set_inaddr - Helper function for set input address
466 * @res : pointer to resizer private data structure
467 * @addr: input address
468 * return none
470 static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr)
472 struct isp_device *isp = to_isp_device(res);
474 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INADD);
478 * The data rate at the horizontal resizer output must not exceed half the
479 * functional clock or 100 MP/s, whichever is lower. According to the TRM
480 * there's no similar requirement for the vertical resizer output. However
481 * experience showed that vertical upscaling by 4 leads to SBL overflows (with
482 * data rates at the resizer output exceeding 300 MP/s). Limiting the resizer
483 * output data rate to the functional clock or 200 MP/s, whichever is lower,
484 * seems to get rid of SBL overflows.
486 * The maximum data rate at the output of the horizontal resizer can thus be
487 * computed with
489 * max intermediate rate <= L3 clock * input height / output height
490 * max intermediate rate <= L3 clock / 2
492 * The maximum data rate at the resizer input is then
494 * max input rate <= max intermediate rate * input width / output width
496 * where the input width and height are the resizer input crop rectangle size.
497 * The TRM doesn't clearly explain if that's a maximum instant data rate or a
498 * maximum average data rate.
500 void omap3isp_resizer_max_rate(struct isp_res_device *res,
501 unsigned int *max_rate)
503 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
504 const struct v4l2_mbus_framefmt *ofmt = &res->formats[RESZ_PAD_SOURCE];
505 unsigned long limit = min(pipe->l3_ick, 200000000UL);
506 unsigned long clock;
508 clock = div_u64((u64)limit * res->crop.active.height, ofmt->height);
509 clock = min(clock, limit / 2);
510 *max_rate = div_u64((u64)clock * res->crop.active.width, ofmt->width);
514 * When the resizer processes images from memory, the driver must slow down read
515 * requests on the input to at least comply with the internal data rate
516 * requirements. If the application real-time requirements can cope with slower
517 * processing, the resizer can be slowed down even more to put less pressure on
518 * the overall system.
520 * When the resizer processes images on the fly (either from the CCDC or the
521 * preview module), the same data rate requirements apply but they can't be
522 * enforced at the resizer level. The image input module (sensor, CCP2 or
523 * preview module) must not provide image data faster than the resizer can
524 * process.
526 * For live image pipelines, the data rate is set by the frame format, size and
527 * rate. The sensor output frame rate must not exceed the maximum resizer data
528 * rate.
530 * The resizer slows down read requests by inserting wait cycles in the SBL
531 * requests. The maximum number of 256-byte requests per second can be computed
532 * as (the data rate is multiplied by 2 to convert from pixels per second to
533 * bytes per second)
535 * request per second = data rate * 2 / 256
536 * cycles per request = cycles per second / requests per second
538 * The number of cycles per second is controlled by the L3 clock, leading to
540 * cycles per request = L3 frequency / 2 * 256 / data rate
542 static void resizer_adjust_bandwidth(struct isp_res_device *res)
544 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
545 struct isp_device *isp = to_isp_device(res);
546 unsigned long l3_ick = pipe->l3_ick;
547 struct v4l2_fract *timeperframe;
548 unsigned int cycles_per_frame;
549 unsigned int requests_per_frame;
550 unsigned int cycles_per_request;
551 unsigned int granularity;
552 unsigned int minimum;
553 unsigned int maximum;
554 unsigned int value;
556 if (res->input != RESIZER_INPUT_MEMORY) {
557 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
558 ISPSBL_SDR_REQ_RSZ_EXP_MASK);
559 return;
562 switch (isp->revision) {
563 case ISP_REVISION_1_0:
564 case ISP_REVISION_2_0:
565 default:
566 granularity = 1024;
567 break;
569 case ISP_REVISION_15_0:
570 granularity = 32;
571 break;
574 /* Compute the minimum number of cycles per request, based on the
575 * pipeline maximum data rate. This is an absolute lower bound if we
576 * don't want SBL overflows, so round the value up.
578 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
579 pipe->max_rate);
580 minimum = DIV_ROUND_UP(cycles_per_request, granularity);
582 /* Compute the maximum number of cycles per request, based on the
583 * requested frame rate. This is a soft upper bound to achieve a frame
584 * rate equal or higher than the requested value, so round the value
585 * down.
587 timeperframe = &pipe->max_timeperframe;
589 requests_per_frame = DIV_ROUND_UP(res->crop.active.width * 2, 256)
590 * res->crop.active.height;
591 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
592 timeperframe->denominator);
593 cycles_per_request = cycles_per_frame / requests_per_frame;
595 maximum = cycles_per_request / granularity;
597 value = max(minimum, maximum);
599 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
600 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
601 ISPSBL_SDR_REQ_RSZ_EXP_MASK,
602 value << ISPSBL_SDR_REQ_RSZ_EXP_SHIFT);
606 * omap3isp_resizer_busy - Checks if ISP resizer is busy.
608 * Returns busy field from ISPRSZ_PCR register.
610 int omap3isp_resizer_busy(struct isp_res_device *res)
612 struct isp_device *isp = to_isp_device(res);
614 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR) &
615 ISPRSZ_PCR_BUSY;
619 * resizer_set_inaddr - Sets the memory address of the input frame.
620 * @addr: 32bit memory address aligned on 32byte boundary.
622 static void resizer_set_inaddr(struct isp_res_device *res, u32 addr)
624 res->addr_base = addr;
626 /* This will handle crop settings in stream off state */
627 if (res->crop_offset)
628 addr += res->crop_offset & ~0x1f;
630 __resizer_set_inaddr(res, addr);
634 * Configures the memory address to which the output frame is written.
635 * @addr: 32bit memory address aligned on 32byte boundary.
636 * Note: For SBL efficiency reasons the address should be on a 256-byte
637 * boundary.
639 static void resizer_set_outaddr(struct isp_res_device *res, u32 addr)
641 struct isp_device *isp = to_isp_device(res);
644 * Set output address. This needs to be in its own function
645 * because it changes often.
647 isp_reg_writel(isp, addr << ISPRSZ_SDR_OUTADD_ADDR_SHIFT,
648 OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTADD);
652 * resizer_print_status - Prints the values of the resizer module registers.
654 #define RSZ_PRINT_REGISTER(isp, name)\
655 dev_dbg(isp->dev, "###RSZ " #name "=0x%08x\n", \
656 isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_##name))
658 static void resizer_print_status(struct isp_res_device *res)
660 struct isp_device *isp = to_isp_device(res);
662 dev_dbg(isp->dev, "-------------Resizer Register dump----------\n");
664 RSZ_PRINT_REGISTER(isp, PCR);
665 RSZ_PRINT_REGISTER(isp, CNT);
666 RSZ_PRINT_REGISTER(isp, OUT_SIZE);
667 RSZ_PRINT_REGISTER(isp, IN_START);
668 RSZ_PRINT_REGISTER(isp, IN_SIZE);
669 RSZ_PRINT_REGISTER(isp, SDR_INADD);
670 RSZ_PRINT_REGISTER(isp, SDR_INOFF);
671 RSZ_PRINT_REGISTER(isp, SDR_OUTADD);
672 RSZ_PRINT_REGISTER(isp, SDR_OUTOFF);
673 RSZ_PRINT_REGISTER(isp, YENH);
675 dev_dbg(isp->dev, "--------------------------------------------\n");
679 * resizer_calc_ratios - Helper function for calculating resizer ratios
680 * @res: pointer to resizer private data structure
681 * @input: input frame size
682 * @output: output frame size
683 * @ratio : return calculated ratios
684 * return none
686 * The resizer uses a polyphase sample rate converter. The upsampling filter
687 * has a fixed number of phases that depend on the resizing ratio. As the ratio
688 * computation depends on the number of phases, we need to compute a first
689 * approximation and then refine it.
691 * The input/output/ratio relationship is given by the OMAP34xx TRM:
693 * - 8-phase, 4-tap mode (RSZ = 64 ~ 512)
694 * iw = (32 * sph + (ow - 1) * hrsz + 16) >> 8 + 7
695 * ih = (32 * spv + (oh - 1) * vrsz + 16) >> 8 + 4
696 * - 4-phase, 7-tap mode (RSZ = 513 ~ 1024)
697 * iw = (64 * sph + (ow - 1) * hrsz + 32) >> 8 + 7
698 * ih = (64 * spv + (oh - 1) * vrsz + 32) >> 8 + 7
700 * iw and ih are the input width and height after cropping. Those equations need
701 * to be satisfied exactly for the resizer to work correctly.
703 * The equations can't be easily reverted, as the >> 8 operation is not linear.
704 * In addition, not all input sizes can be achieved for a given output size. To
705 * get the highest input size lower than or equal to the requested input size,
706 * we need to compute the highest resizing ratio that satisfies the following
707 * inequality (taking the 4-tap mode width equation as an example)
709 * iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7
711 * (where iw is the requested input width) which can be rewritten as
713 * iw - 7 >= (32 * sph + (ow - 1) * hrsz + 16) >> 8
714 * (iw - 7) << 8 >= 32 * sph + (ow - 1) * hrsz + 16 - b
715 * ((iw - 7) << 8) + b >= 32 * sph + (ow - 1) * hrsz + 16
717 * where b is the value of the 8 least significant bits of the right hand side
718 * expression of the last inequality. The highest resizing ratio value will be
719 * achieved when b is equal to its maximum value of 255. That resizing ratio
720 * value will still satisfy the original inequality, as b will disappear when
721 * the expression will be shifted right by 8.
723 * The reverted equations thus become
725 * - 8-phase, 4-tap mode
726 * hrsz = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / (ow - 1)
727 * vrsz = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / (oh - 1)
728 * - 4-phase, 7-tap mode
729 * hrsz = ((iw - 7) * 256 + 255 - 32 - 64 * sph) / (ow - 1)
730 * vrsz = ((ih - 7) * 256 + 255 - 32 - 64 * spv) / (oh - 1)
732 * The ratios are integer values, and are rounded down to ensure that the
733 * cropped input size is not bigger than the uncropped input size.
735 * As the number of phases/taps, used to select the correct equations to compute
736 * the ratio, depends on the ratio, we start with the 4-tap mode equations to
737 * compute an approximation of the ratio, and switch to the 7-tap mode equations
738 * if the approximation is higher than the ratio threshold.
740 * As the 7-tap mode equations will return a ratio smaller than or equal to the
741 * 4-tap mode equations, the resulting ratio could become lower than or equal to
742 * the ratio threshold. This 'equations loop' isn't an issue as long as the
743 * correct equations are used to compute the final input size. Starting with the
744 * 4-tap mode equations ensure that, in case of values resulting in a 'ratio
745 * loop', the smallest of the ratio values will be used, never exceeding the
746 * requested input size.
748 * We first clamp the output size according to the hardware capability to avoid
749 * auto-cropping the input more than required to satisfy the TRM equations. The
750 * minimum output size is achieved with a scaling factor of 1024. It is thus
751 * computed using the 7-tap equations.
753 * min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1
754 * min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1
756 * Similarly, the maximum output size is achieved with a scaling factor of 64
757 * and computed using the 4-tap equations.
759 * max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1
760 * max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1
762 * The additional +255 term compensates for the round down operation performed
763 * by the TRM equations when shifting the value right by 8 bits.
765 * We then compute and clamp the ratios (x1/4 ~ x4). Clamping the output size to
766 * the maximum value guarantees that the ratio value will never be smaller than
767 * the minimum, but it could still slightly exceed the maximum. Clamping the
768 * ratio will thus result in a resizing factor slightly larger than the
769 * requested value.
771 * To accommodate that, and make sure the TRM equations are satisfied exactly, we
772 * compute the input crop rectangle as the last step.
774 * As if the situation wasn't complex enough, the maximum output width depends
775 * on the vertical resizing ratio. Fortunately, the output height doesn't
776 * depend on the horizontal resizing ratio. We can then start by computing the
777 * output height and the vertical ratio, and then move to computing the output
778 * width and the horizontal ratio.
780 static void resizer_calc_ratios(struct isp_res_device *res,
781 struct v4l2_rect *input,
782 struct v4l2_mbus_framefmt *output,
783 struct resizer_ratio *ratio)
785 struct isp_device *isp = to_isp_device(res);
786 const unsigned int spv = DEFAULT_PHASE;
787 const unsigned int sph = DEFAULT_PHASE;
788 unsigned int upscaled_width;
789 unsigned int upscaled_height;
790 unsigned int min_width;
791 unsigned int min_height;
792 unsigned int max_width;
793 unsigned int max_height;
794 unsigned int width_alignment;
795 unsigned int width;
796 unsigned int height;
799 * Clamp the output height based on the hardware capabilities and
800 * compute the vertical resizing ratio.
802 min_height = ((input->height - 7) * 256 - 32 - 64 * spv) / 1024 + 1;
803 min_height = max_t(unsigned int, min_height, MIN_OUT_HEIGHT);
804 max_height = ((input->height - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1;
805 max_height = min_t(unsigned int, max_height, MAX_OUT_HEIGHT);
806 output->height = clamp(output->height, min_height, max_height);
808 ratio->vert = ((input->height - 4) * 256 + 255 - 16 - 32 * spv)
809 / (output->height - 1);
810 if (ratio->vert > MID_RESIZE_VALUE)
811 ratio->vert = ((input->height - 7) * 256 + 255 - 32 - 64 * spv)
812 / (output->height - 1);
813 ratio->vert = clamp_t(unsigned int, ratio->vert,
814 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
816 if (ratio->vert <= MID_RESIZE_VALUE) {
817 upscaled_height = (output->height - 1) * ratio->vert
818 + 32 * spv + 16;
819 height = (upscaled_height >> 8) + 4;
820 } else {
821 upscaled_height = (output->height - 1) * ratio->vert
822 + 64 * spv + 32;
823 height = (upscaled_height >> 8) + 7;
827 * Compute the minimum and maximum output widths based on the hardware
828 * capabilities. The maximum depends on the vertical resizing ratio.
830 min_width = ((input->width - 7) * 256 - 32 - 64 * sph) / 1024 + 1;
831 min_width = max_t(unsigned int, min_width, MIN_OUT_WIDTH);
833 if (ratio->vert <= MID_RESIZE_VALUE) {
834 switch (isp->revision) {
835 case ISP_REVISION_1_0:
836 max_width = MAX_4TAP_OUT_WIDTH_ES1;
837 break;
839 case ISP_REVISION_2_0:
840 default:
841 max_width = MAX_4TAP_OUT_WIDTH_ES2;
842 break;
844 case ISP_REVISION_15_0:
845 max_width = MAX_4TAP_OUT_WIDTH_3630;
846 break;
848 } else {
849 switch (isp->revision) {
850 case ISP_REVISION_1_0:
851 max_width = MAX_7TAP_OUT_WIDTH_ES1;
852 break;
854 case ISP_REVISION_2_0:
855 default:
856 max_width = MAX_7TAP_OUT_WIDTH_ES2;
857 break;
859 case ISP_REVISION_15_0:
860 max_width = MAX_7TAP_OUT_WIDTH_3630;
861 break;
864 max_width = min(((input->width - 7) * 256 + 255 - 16 - 32 * sph) / 64
865 + 1, max_width);
868 * The output width must be even, and must be a multiple of 16 bytes
869 * when upscaling vertically. Clamp the output width to the valid range.
870 * Take the alignment into account (the maximum width in 7-tap mode on
871 * ES2 isn't a multiple of 8) and align the result up to make sure it
872 * won't be smaller than the minimum.
874 width_alignment = ratio->vert < 256 ? 8 : 2;
875 output->width = clamp(output->width, min_width,
876 max_width & ~(width_alignment - 1));
877 output->width = ALIGN(output->width, width_alignment);
879 ratio->horz = ((input->width - 7) * 256 + 255 - 16 - 32 * sph)
880 / (output->width - 1);
881 if (ratio->horz > MID_RESIZE_VALUE)
882 ratio->horz = ((input->width - 7) * 256 + 255 - 32 - 64 * sph)
883 / (output->width - 1);
884 ratio->horz = clamp_t(unsigned int, ratio->horz,
885 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
887 if (ratio->horz <= MID_RESIZE_VALUE) {
888 upscaled_width = (output->width - 1) * ratio->horz
889 + 32 * sph + 16;
890 width = (upscaled_width >> 8) + 7;
891 } else {
892 upscaled_width = (output->width - 1) * ratio->horz
893 + 64 * sph + 32;
894 width = (upscaled_width >> 8) + 7;
897 /* Center the new crop rectangle. */
898 input->left += (input->width - width) / 2;
899 input->top += (input->height - height) / 2;
900 input->width = width;
901 input->height = height;
905 * resizer_set_crop_params - Setup hardware with cropping parameters
906 * @res : resizer private structure
907 * @input : format on sink pad
908 * @output : format on source pad
909 * return none
911 static void resizer_set_crop_params(struct isp_res_device *res,
912 const struct v4l2_mbus_framefmt *input,
913 const struct v4l2_mbus_framefmt *output)
915 resizer_set_ratio(res, &res->ratio);
917 /* Set chrominance horizontal algorithm */
918 if (res->ratio.horz >= RESIZE_DIVISOR)
919 resizer_set_bilinear(res, RSZ_THE_SAME);
920 else
921 resizer_set_bilinear(res, RSZ_BILINEAR);
923 resizer_adjust_bandwidth(res);
925 if (res->input == RESIZER_INPUT_MEMORY) {
926 /* Calculate additional offset for crop */
927 res->crop_offset = (res->crop.active.top * input->width +
928 res->crop.active.left) * 2;
930 * Write lowest 4 bits of horizontal pixel offset (in pixels),
931 * vertical start must be 0.
933 resizer_set_start(res, (res->crop_offset / 2) & 0xf, 0);
936 * Set start (read) address for cropping, in bytes.
937 * Lowest 5 bits must be zero.
939 __resizer_set_inaddr(res,
940 res->addr_base + (res->crop_offset & ~0x1f));
941 } else {
943 * Set vertical start line and horizontal starting pixel.
944 * If the input is from CCDC/PREV, horizontal start field is
945 * in bytes (twice number of pixels).
947 resizer_set_start(res, res->crop.active.left * 2,
948 res->crop.active.top);
949 /* Input address and offset must be 0 for preview/ccdc input */
950 __resizer_set_inaddr(res, 0);
951 resizer_set_input_offset(res, 0);
954 /* Set the input size */
955 resizer_set_input_size(res, res->crop.active.width,
956 res->crop.active.height);
959 static void resizer_configure(struct isp_res_device *res)
961 struct v4l2_mbus_framefmt *informat, *outformat;
962 struct resizer_luma_yenh luma = {0, 0, 0, 0};
964 resizer_set_source(res, res->input);
966 informat = &res->formats[RESZ_PAD_SINK];
967 outformat = &res->formats[RESZ_PAD_SOURCE];
969 /* RESZ_PAD_SINK */
970 if (res->input == RESIZER_INPUT_VP)
971 resizer_set_input_offset(res, 0);
972 else
973 resizer_set_input_offset(res, ALIGN(informat->width, 0x10) * 2);
975 /* YUV422 interleaved, default phase, no luma enhancement */
976 resizer_set_intype(res, RSZ_YUV422);
977 resizer_set_ycpos(res, informat->code);
978 resizer_set_phase(res, DEFAULT_PHASE, DEFAULT_PHASE);
979 resizer_set_luma(res, &luma);
981 /* RESZ_PAD_SOURCE */
982 resizer_set_output_offset(res, ALIGN(outformat->width * 2, 32));
983 resizer_set_output_size(res, outformat->width, outformat->height);
985 resizer_set_crop_params(res, informat, outformat);
988 /* -----------------------------------------------------------------------------
989 * Interrupt handling
992 static void resizer_enable_oneshot(struct isp_res_device *res)
994 struct isp_device *isp = to_isp_device(res);
996 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR,
997 ISPRSZ_PCR_ENABLE | ISPRSZ_PCR_ONESHOT);
1000 void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res)
1003 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1004 * condition, the module was paused and now we have a buffer queued
1005 * on the output again. Restart the pipeline if running in continuous
1006 * mode.
1008 if (res->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1009 res->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1010 resizer_enable_oneshot(res);
1011 isp_video_dmaqueue_flags_clr(&res->video_out);
1015 static void resizer_isr_buffer(struct isp_res_device *res)
1017 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
1018 struct isp_buffer *buffer;
1019 int restart = 0;
1021 if (res->state == ISP_PIPELINE_STREAM_STOPPED)
1022 return;
1024 /* Complete the output buffer and, if reading from memory, the input
1025 * buffer.
1027 buffer = omap3isp_video_buffer_next(&res->video_out);
1028 if (buffer != NULL) {
1029 resizer_set_outaddr(res, buffer->dma);
1030 restart = 1;
1033 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1035 if (res->input == RESIZER_INPUT_MEMORY) {
1036 buffer = omap3isp_video_buffer_next(&res->video_in);
1037 if (buffer != NULL)
1038 resizer_set_inaddr(res, buffer->dma);
1039 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1042 if (res->state == ISP_PIPELINE_STREAM_SINGLESHOT) {
1043 if (isp_pipeline_ready(pipe))
1044 omap3isp_pipeline_set_stream(pipe,
1045 ISP_PIPELINE_STREAM_SINGLESHOT);
1046 } else {
1047 /* If an underrun occurs, the video queue operation handler will
1048 * restart the resizer. Otherwise restart it immediately.
1050 if (restart)
1051 resizer_enable_oneshot(res);
1056 * omap3isp_resizer_isr - ISP resizer interrupt handler
1058 * Manage the resizer video buffers and configure shadowed and busy-locked
1059 * registers.
1061 void omap3isp_resizer_isr(struct isp_res_device *res)
1063 struct v4l2_mbus_framefmt *informat, *outformat;
1064 unsigned long flags;
1066 if (omap3isp_module_sync_is_stopping(&res->wait, &res->stopping))
1067 return;
1069 spin_lock_irqsave(&res->lock, flags);
1071 if (res->applycrop) {
1072 outformat = __resizer_get_format(res, NULL, RESZ_PAD_SOURCE,
1073 V4L2_SUBDEV_FORMAT_ACTIVE);
1074 informat = __resizer_get_format(res, NULL, RESZ_PAD_SINK,
1075 V4L2_SUBDEV_FORMAT_ACTIVE);
1076 resizer_set_crop_params(res, informat, outformat);
1077 res->applycrop = 0;
1080 spin_unlock_irqrestore(&res->lock, flags);
1082 resizer_isr_buffer(res);
1085 /* -----------------------------------------------------------------------------
1086 * ISP video operations
1089 static int resizer_video_queue(struct isp_video *video,
1090 struct isp_buffer *buffer)
1092 struct isp_res_device *res = &video->isp->isp_res;
1094 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1095 resizer_set_inaddr(res, buffer->dma);
1098 * We now have a buffer queued on the output. Despite what the
1099 * TRM says, the resizer can't be restarted immediately.
1100 * Enabling it in one shot mode in the middle of a frame (or at
1101 * least asynchronously to the frame) results in the output
1102 * being shifted randomly left/right and up/down, as if the
1103 * hardware didn't synchronize itself to the beginning of the
1104 * frame correctly.
1106 * Restart the resizer on the next sync interrupt if running in
1107 * continuous mode or when starting the stream.
1109 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1110 resizer_set_outaddr(res, buffer->dma);
1112 return 0;
1115 static const struct isp_video_operations resizer_video_ops = {
1116 .queue = resizer_video_queue,
1119 /* -----------------------------------------------------------------------------
1120 * V4L2 subdev operations
1124 * resizer_set_stream - Enable/Disable streaming on resizer subdev
1125 * @sd: ISP resizer V4L2 subdev
1126 * @enable: 1 == Enable, 0 == Disable
1128 * The resizer hardware can't be enabled without a memory buffer to write to.
1129 * As the s_stream operation is called in response to a STREAMON call without
1130 * any buffer queued yet, just update the state field and return immediately.
1131 * The resizer will be enabled in resizer_video_queue().
1133 static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
1135 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1136 struct isp_video *video_out = &res->video_out;
1137 struct isp_device *isp = to_isp_device(res);
1138 struct device *dev = to_device(res);
1140 if (res->state == ISP_PIPELINE_STREAM_STOPPED) {
1141 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1142 return 0;
1144 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1145 resizer_configure(res);
1146 resizer_print_status(res);
1149 switch (enable) {
1150 case ISP_PIPELINE_STREAM_CONTINUOUS:
1151 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1152 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1153 resizer_enable_oneshot(res);
1154 isp_video_dmaqueue_flags_clr(video_out);
1156 break;
1158 case ISP_PIPELINE_STREAM_SINGLESHOT:
1159 if (res->input == RESIZER_INPUT_MEMORY)
1160 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_READ);
1161 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1163 resizer_enable_oneshot(res);
1164 break;
1166 case ISP_PIPELINE_STREAM_STOPPED:
1167 if (omap3isp_module_sync_idle(&sd->entity, &res->wait,
1168 &res->stopping))
1169 dev_dbg(dev, "%s: module stop timeout.\n", sd->name);
1170 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_RESIZER_READ |
1171 OMAP3_ISP_SBL_RESIZER_WRITE);
1172 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1173 isp_video_dmaqueue_flags_clr(video_out);
1174 break;
1177 res->state = enable;
1178 return 0;
1182 * resizer_try_crop - mangles crop parameters.
1184 static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
1185 const struct v4l2_mbus_framefmt *source,
1186 struct v4l2_rect *crop)
1188 const unsigned int spv = DEFAULT_PHASE;
1189 const unsigned int sph = DEFAULT_PHASE;
1191 /* Crop rectangle is constrained by the output size so that zoom ratio
1192 * cannot exceed +/-4.0.
1194 unsigned int min_width =
1195 ((32 * sph + (source->width - 1) * 64 + 16) >> 8) + 7;
1196 unsigned int min_height =
1197 ((32 * spv + (source->height - 1) * 64 + 16) >> 8) + 4;
1198 unsigned int max_width =
1199 ((64 * sph + (source->width - 1) * 1024 + 32) >> 8) + 7;
1200 unsigned int max_height =
1201 ((64 * spv + (source->height - 1) * 1024 + 32) >> 8) + 7;
1203 crop->width = clamp_t(u32, crop->width, min_width, max_width);
1204 crop->height = clamp_t(u32, crop->height, min_height, max_height);
1206 /* Crop can not go beyond of the input rectangle */
1207 crop->left = clamp_t(u32, crop->left, 0, sink->width - MIN_IN_WIDTH);
1208 crop->width = clamp_t(u32, crop->width, MIN_IN_WIDTH,
1209 sink->width - crop->left);
1210 crop->top = clamp_t(u32, crop->top, 0, sink->height - MIN_IN_HEIGHT);
1211 crop->height = clamp_t(u32, crop->height, MIN_IN_HEIGHT,
1212 sink->height - crop->top);
1216 * resizer_get_selection - Retrieve a selection rectangle on a pad
1217 * @sd: ISP resizer V4L2 subdevice
1218 * @cfg: V4L2 subdev pad configuration
1219 * @sel: Selection rectangle
1221 * The only supported rectangles are the crop rectangles on the sink pad.
1223 * Return 0 on success or a negative error code otherwise.
1225 static int resizer_get_selection(struct v4l2_subdev *sd,
1226 struct v4l2_subdev_pad_config *cfg,
1227 struct v4l2_subdev_selection *sel)
1229 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1230 struct v4l2_mbus_framefmt *format_source;
1231 struct v4l2_mbus_framefmt *format_sink;
1232 struct resizer_ratio ratio;
1234 if (sel->pad != RESZ_PAD_SINK)
1235 return -EINVAL;
1237 format_sink = __resizer_get_format(res, cfg, RESZ_PAD_SINK,
1238 sel->which);
1239 format_source = __resizer_get_format(res, cfg, RESZ_PAD_SOURCE,
1240 sel->which);
1242 switch (sel->target) {
1243 case V4L2_SEL_TGT_CROP_BOUNDS:
1244 sel->r.left = 0;
1245 sel->r.top = 0;
1246 sel->r.width = INT_MAX;
1247 sel->r.height = INT_MAX;
1249 resizer_try_crop(format_sink, format_source, &sel->r);
1250 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1251 break;
1253 case V4L2_SEL_TGT_CROP:
1254 sel->r = *__resizer_get_crop(res, cfg, sel->which);
1255 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1256 break;
1258 default:
1259 return -EINVAL;
1262 return 0;
1266 * resizer_set_selection - Set a selection rectangle on a pad
1267 * @sd: ISP resizer V4L2 subdevice
1268 * @cfg: V4L2 subdev pad configuration
1269 * @sel: Selection rectangle
1271 * The only supported rectangle is the actual crop rectangle on the sink pad.
1273 * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag
1274 * was always set.
1276 * Return 0 on success or a negative error code otherwise.
1278 static int resizer_set_selection(struct v4l2_subdev *sd,
1279 struct v4l2_subdev_pad_config *cfg,
1280 struct v4l2_subdev_selection *sel)
1282 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1283 struct isp_device *isp = to_isp_device(res);
1284 const struct v4l2_mbus_framefmt *format_sink;
1285 struct v4l2_mbus_framefmt format_source;
1286 struct resizer_ratio ratio;
1287 unsigned long flags;
1289 if (sel->target != V4L2_SEL_TGT_CROP ||
1290 sel->pad != RESZ_PAD_SINK)
1291 return -EINVAL;
1293 format_sink = __resizer_get_format(res, cfg, RESZ_PAD_SINK,
1294 sel->which);
1295 format_source = *__resizer_get_format(res, cfg, RESZ_PAD_SOURCE,
1296 sel->which);
1298 dev_dbg(isp->dev, "%s(%s): req %ux%u -> (%d,%d)/%ux%u -> %ux%u\n",
1299 __func__, sel->which == V4L2_SUBDEV_FORMAT_TRY ? "try" : "act",
1300 format_sink->width, format_sink->height,
1301 sel->r.left, sel->r.top, sel->r.width, sel->r.height,
1302 format_source.width, format_source.height);
1304 /* Clamp the crop rectangle to the bounds, and then mangle it further to
1305 * fulfill the TRM equations. Store the clamped but otherwise unmangled
1306 * rectangle to avoid cropping the input multiple times: when an
1307 * application sets the output format, the current crop rectangle is
1308 * mangled during crop rectangle computation, which would lead to a new,
1309 * smaller input crop rectangle every time the output size is set if we
1310 * stored the mangled rectangle.
1312 resizer_try_crop(format_sink, &format_source, &sel->r);
1313 *__resizer_get_crop(res, cfg, sel->which) = sel->r;
1314 resizer_calc_ratios(res, &sel->r, &format_source, &ratio);
1316 dev_dbg(isp->dev, "%s(%s): got %ux%u -> (%d,%d)/%ux%u -> %ux%u\n",
1317 __func__, sel->which == V4L2_SUBDEV_FORMAT_TRY ? "try" : "act",
1318 format_sink->width, format_sink->height,
1319 sel->r.left, sel->r.top, sel->r.width, sel->r.height,
1320 format_source.width, format_source.height);
1322 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1323 *__resizer_get_format(res, cfg, RESZ_PAD_SOURCE, sel->which) =
1324 format_source;
1325 return 0;
1328 /* Update the source format, resizing ratios and crop rectangle. If
1329 * streaming is on the IRQ handler will reprogram the resizer after the
1330 * current frame. We thus we need to protect against race conditions.
1332 spin_lock_irqsave(&res->lock, flags);
1334 *__resizer_get_format(res, cfg, RESZ_PAD_SOURCE, sel->which) =
1335 format_source;
1337 res->ratio = ratio;
1338 res->crop.active = sel->r;
1340 if (res->state != ISP_PIPELINE_STREAM_STOPPED)
1341 res->applycrop = 1;
1343 spin_unlock_irqrestore(&res->lock, flags);
1345 return 0;
1348 /* resizer pixel formats */
1349 static const unsigned int resizer_formats[] = {
1350 MEDIA_BUS_FMT_UYVY8_1X16,
1351 MEDIA_BUS_FMT_YUYV8_1X16,
1354 static unsigned int resizer_max_in_width(struct isp_res_device *res)
1356 struct isp_device *isp = to_isp_device(res);
1358 if (res->input == RESIZER_INPUT_MEMORY) {
1359 return MAX_IN_WIDTH_MEMORY_MODE;
1360 } else {
1361 if (isp->revision == ISP_REVISION_1_0)
1362 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
1363 else
1364 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
1369 * resizer_try_format - Handle try format by pad subdev method
1370 * @res : ISP resizer device
1371 * @cfg: V4L2 subdev pad configuration
1372 * @pad : pad num
1373 * @fmt : pointer to v4l2 format structure
1374 * @which : wanted subdev format
1376 static void resizer_try_format(struct isp_res_device *res,
1377 struct v4l2_subdev_pad_config *cfg, unsigned int pad,
1378 struct v4l2_mbus_framefmt *fmt,
1379 enum v4l2_subdev_format_whence which)
1381 struct v4l2_mbus_framefmt *format;
1382 struct resizer_ratio ratio;
1383 struct v4l2_rect crop;
1385 switch (pad) {
1386 case RESZ_PAD_SINK:
1387 if (fmt->code != MEDIA_BUS_FMT_YUYV8_1X16 &&
1388 fmt->code != MEDIA_BUS_FMT_UYVY8_1X16)
1389 fmt->code = MEDIA_BUS_FMT_YUYV8_1X16;
1391 fmt->width = clamp_t(u32, fmt->width, MIN_IN_WIDTH,
1392 resizer_max_in_width(res));
1393 fmt->height = clamp_t(u32, fmt->height, MIN_IN_HEIGHT,
1394 MAX_IN_HEIGHT);
1395 break;
1397 case RESZ_PAD_SOURCE:
1398 format = __resizer_get_format(res, cfg, RESZ_PAD_SINK, which);
1399 fmt->code = format->code;
1401 crop = *__resizer_get_crop(res, cfg, which);
1402 resizer_calc_ratios(res, &crop, fmt, &ratio);
1403 break;
1406 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1407 fmt->field = V4L2_FIELD_NONE;
1411 * resizer_enum_mbus_code - Handle pixel format enumeration
1412 * @sd : pointer to v4l2 subdev structure
1413 * @cfg: V4L2 subdev pad configuration
1414 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1415 * return -EINVAL or zero on success
1417 static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
1418 struct v4l2_subdev_pad_config *cfg,
1419 struct v4l2_subdev_mbus_code_enum *code)
1421 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1422 struct v4l2_mbus_framefmt *format;
1424 if (code->pad == RESZ_PAD_SINK) {
1425 if (code->index >= ARRAY_SIZE(resizer_formats))
1426 return -EINVAL;
1428 code->code = resizer_formats[code->index];
1429 } else {
1430 if (code->index != 0)
1431 return -EINVAL;
1433 format = __resizer_get_format(res, cfg, RESZ_PAD_SINK,
1434 code->which);
1435 code->code = format->code;
1438 return 0;
1441 static int resizer_enum_frame_size(struct v4l2_subdev *sd,
1442 struct v4l2_subdev_pad_config *cfg,
1443 struct v4l2_subdev_frame_size_enum *fse)
1445 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1446 struct v4l2_mbus_framefmt format;
1448 if (fse->index != 0)
1449 return -EINVAL;
1451 format.code = fse->code;
1452 format.width = 1;
1453 format.height = 1;
1454 resizer_try_format(res, cfg, fse->pad, &format, fse->which);
1455 fse->min_width = format.width;
1456 fse->min_height = format.height;
1458 if (format.code != fse->code)
1459 return -EINVAL;
1461 format.code = fse->code;
1462 format.width = -1;
1463 format.height = -1;
1464 resizer_try_format(res, cfg, fse->pad, &format, fse->which);
1465 fse->max_width = format.width;
1466 fse->max_height = format.height;
1468 return 0;
1472 * resizer_get_format - Handle get format by pads subdev method
1473 * @sd : pointer to v4l2 subdev structure
1474 * @cfg: V4L2 subdev pad configuration
1475 * @fmt : pointer to v4l2 subdev format structure
1476 * return -EINVAL or zero on success
1478 static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1479 struct v4l2_subdev_format *fmt)
1481 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1482 struct v4l2_mbus_framefmt *format;
1484 format = __resizer_get_format(res, cfg, fmt->pad, fmt->which);
1485 if (format == NULL)
1486 return -EINVAL;
1488 fmt->format = *format;
1489 return 0;
1493 * resizer_set_format - Handle set format by pads subdev method
1494 * @sd : pointer to v4l2 subdev structure
1495 * @cfg: V4L2 subdev pad configuration
1496 * @fmt : pointer to v4l2 subdev format structure
1497 * return -EINVAL or zero on success
1499 static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1500 struct v4l2_subdev_format *fmt)
1502 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1503 struct v4l2_mbus_framefmt *format;
1504 struct v4l2_rect *crop;
1506 format = __resizer_get_format(res, cfg, fmt->pad, fmt->which);
1507 if (format == NULL)
1508 return -EINVAL;
1510 resizer_try_format(res, cfg, fmt->pad, &fmt->format, fmt->which);
1511 *format = fmt->format;
1513 if (fmt->pad == RESZ_PAD_SINK) {
1514 /* reset crop rectangle */
1515 crop = __resizer_get_crop(res, cfg, fmt->which);
1516 crop->left = 0;
1517 crop->top = 0;
1518 crop->width = fmt->format.width;
1519 crop->height = fmt->format.height;
1521 /* Propagate the format from sink to source */
1522 format = __resizer_get_format(res, cfg, RESZ_PAD_SOURCE,
1523 fmt->which);
1524 *format = fmt->format;
1525 resizer_try_format(res, cfg, RESZ_PAD_SOURCE, format,
1526 fmt->which);
1529 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1530 /* Compute and store the active crop rectangle and resizer
1531 * ratios. format already points to the source pad active
1532 * format.
1534 res->crop.active = res->crop.request;
1535 resizer_calc_ratios(res, &res->crop.active, format,
1536 &res->ratio);
1539 return 0;
1542 static int resizer_link_validate(struct v4l2_subdev *sd,
1543 struct media_link *link,
1544 struct v4l2_subdev_format *source_fmt,
1545 struct v4l2_subdev_format *sink_fmt)
1547 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1548 struct isp_pipeline *pipe = to_isp_pipeline(&sd->entity);
1550 omap3isp_resizer_max_rate(res, &pipe->max_rate);
1552 return v4l2_subdev_link_validate_default(sd, link,
1553 source_fmt, sink_fmt);
1557 * resizer_init_formats - Initialize formats on all pads
1558 * @sd: ISP resizer V4L2 subdevice
1559 * @fh: V4L2 subdev file handle
1561 * Initialize all pad formats with default values. If fh is not NULL, try
1562 * formats are initialized on the file handle. Otherwise active formats are
1563 * initialized on the device.
1565 static int resizer_init_formats(struct v4l2_subdev *sd,
1566 struct v4l2_subdev_fh *fh)
1568 struct v4l2_subdev_format format;
1570 memset(&format, 0, sizeof(format));
1571 format.pad = RESZ_PAD_SINK;
1572 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1573 format.format.code = MEDIA_BUS_FMT_YUYV8_1X16;
1574 format.format.width = 4096;
1575 format.format.height = 4096;
1576 resizer_set_format(sd, fh ? fh->pad : NULL, &format);
1578 return 0;
1581 /* subdev video operations */
1582 static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
1583 .s_stream = resizer_set_stream,
1586 /* subdev pad operations */
1587 static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
1588 .enum_mbus_code = resizer_enum_mbus_code,
1589 .enum_frame_size = resizer_enum_frame_size,
1590 .get_fmt = resizer_get_format,
1591 .set_fmt = resizer_set_format,
1592 .get_selection = resizer_get_selection,
1593 .set_selection = resizer_set_selection,
1594 .link_validate = resizer_link_validate,
1597 /* subdev operations */
1598 static const struct v4l2_subdev_ops resizer_v4l2_ops = {
1599 .video = &resizer_v4l2_video_ops,
1600 .pad = &resizer_v4l2_pad_ops,
1603 /* subdev internal operations */
1604 static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
1605 .open = resizer_init_formats,
1608 /* -----------------------------------------------------------------------------
1609 * Media entity operations
1613 * resizer_link_setup - Setup resizer connections.
1614 * @entity : Pointer to media entity structure
1615 * @local : Pointer to local pad array
1616 * @remote : Pointer to remote pad array
1617 * @flags : Link flags
1618 * return -EINVAL or zero on success
1620 static int resizer_link_setup(struct media_entity *entity,
1621 const struct media_pad *local,
1622 const struct media_pad *remote, u32 flags)
1624 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1625 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1626 unsigned int index = local->index;
1628 /* FIXME: this is actually a hack! */
1629 if (is_media_entity_v4l2_subdev(remote->entity))
1630 index |= 2 << 16;
1632 switch (index) {
1633 case RESZ_PAD_SINK:
1634 /* read from memory */
1635 if (flags & MEDIA_LNK_FL_ENABLED) {
1636 if (res->input == RESIZER_INPUT_VP)
1637 return -EBUSY;
1638 res->input = RESIZER_INPUT_MEMORY;
1639 } else {
1640 if (res->input == RESIZER_INPUT_MEMORY)
1641 res->input = RESIZER_INPUT_NONE;
1643 break;
1645 case RESZ_PAD_SINK | 2 << 16:
1646 /* read from ccdc or previewer */
1647 if (flags & MEDIA_LNK_FL_ENABLED) {
1648 if (res->input == RESIZER_INPUT_MEMORY)
1649 return -EBUSY;
1650 res->input = RESIZER_INPUT_VP;
1651 } else {
1652 if (res->input == RESIZER_INPUT_VP)
1653 res->input = RESIZER_INPUT_NONE;
1655 break;
1657 case RESZ_PAD_SOURCE:
1658 /* resizer always write to memory */
1659 break;
1661 default:
1662 return -EINVAL;
1665 return 0;
1668 /* media operations */
1669 static const struct media_entity_operations resizer_media_ops = {
1670 .link_setup = resizer_link_setup,
1671 .link_validate = v4l2_subdev_link_validate,
1674 void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
1676 v4l2_device_unregister_subdev(&res->subdev);
1677 omap3isp_video_unregister(&res->video_in);
1678 omap3isp_video_unregister(&res->video_out);
1681 int omap3isp_resizer_register_entities(struct isp_res_device *res,
1682 struct v4l2_device *vdev)
1684 int ret;
1686 /* Register the subdev and video nodes. */
1687 ret = v4l2_device_register_subdev(vdev, &res->subdev);
1688 if (ret < 0)
1689 goto error;
1691 ret = omap3isp_video_register(&res->video_in, vdev);
1692 if (ret < 0)
1693 goto error;
1695 ret = omap3isp_video_register(&res->video_out, vdev);
1696 if (ret < 0)
1697 goto error;
1699 return 0;
1701 error:
1702 omap3isp_resizer_unregister_entities(res);
1703 return ret;
1706 /* -----------------------------------------------------------------------------
1707 * ISP resizer initialization and cleanup
1711 * resizer_init_entities - Initialize resizer subdev and media entity.
1712 * @res : Pointer to resizer device structure
1713 * return -ENOMEM or zero on success
1715 static int resizer_init_entities(struct isp_res_device *res)
1717 struct v4l2_subdev *sd = &res->subdev;
1718 struct media_pad *pads = res->pads;
1719 struct media_entity *me = &sd->entity;
1720 int ret;
1722 res->input = RESIZER_INPUT_NONE;
1724 v4l2_subdev_init(sd, &resizer_v4l2_ops);
1725 sd->internal_ops = &resizer_v4l2_internal_ops;
1726 strlcpy(sd->name, "OMAP3 ISP resizer", sizeof(sd->name));
1727 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
1728 v4l2_set_subdevdata(sd, res);
1729 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1731 pads[RESZ_PAD_SINK].flags = MEDIA_PAD_FL_SINK
1732 | MEDIA_PAD_FL_MUST_CONNECT;
1733 pads[RESZ_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1735 me->ops = &resizer_media_ops;
1736 ret = media_entity_pads_init(me, RESZ_PADS_NUM, pads);
1737 if (ret < 0)
1738 return ret;
1740 resizer_init_formats(sd, NULL);
1742 res->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1743 res->video_in.ops = &resizer_video_ops;
1744 res->video_in.isp = to_isp_device(res);
1745 res->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1746 res->video_in.bpl_alignment = 32;
1747 res->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1748 res->video_out.ops = &resizer_video_ops;
1749 res->video_out.isp = to_isp_device(res);
1750 res->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1751 res->video_out.bpl_alignment = 32;
1753 ret = omap3isp_video_init(&res->video_in, "resizer");
1754 if (ret < 0)
1755 goto error_video_in;
1757 ret = omap3isp_video_init(&res->video_out, "resizer");
1758 if (ret < 0)
1759 goto error_video_out;
1761 res->video_out.video.entity.flags |= MEDIA_ENT_FL_DEFAULT;
1763 return 0;
1765 error_video_out:
1766 omap3isp_video_cleanup(&res->video_in);
1767 error_video_in:
1768 media_entity_cleanup(&res->subdev.entity);
1769 return ret;
1773 * isp_resizer_init - Resizer initialization.
1774 * @isp : Pointer to ISP device
1775 * return -ENOMEM or zero on success
1777 int omap3isp_resizer_init(struct isp_device *isp)
1779 struct isp_res_device *res = &isp->isp_res;
1781 init_waitqueue_head(&res->wait);
1782 atomic_set(&res->stopping, 0);
1783 spin_lock_init(&res->lock);
1785 return resizer_init_entities(res);
1788 void omap3isp_resizer_cleanup(struct isp_device *isp)
1790 struct isp_res_device *res = &isp->isp_res;
1792 omap3isp_video_cleanup(&res->video_in);
1793 omap3isp_video_cleanup(&res->video_out);
1794 media_entity_cleanup(&res->subdev.entity);