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>
19 #include <linux/module.h>
23 #include "ispresizer.h"
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: */
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.
112 * __resizer_get_format - helper function for getting resizer format
113 * @res : pointer to resizer private structure
115 * @fh : V4L2 subdev file handle
116 * @which : wanted subdev format
119 static struct v4l2_mbus_framefmt *
120 __resizer_get_format(struct isp_res_device *res, struct v4l2_subdev_fh *fh,
121 unsigned int pad, enum v4l2_subdev_format_whence which)
123 if (which == V4L2_SUBDEV_FORMAT_TRY)
124 return v4l2_subdev_get_try_format(fh, pad);
126 return &res->formats[pad];
130 * __resizer_get_crop - helper function for getting resizer crop rectangle
131 * @res : pointer to resizer private structure
132 * @fh : V4L2 subdev file handle
133 * @which : wanted subdev crop rectangle
135 static struct v4l2_rect *
136 __resizer_get_crop(struct isp_res_device *res, struct v4l2_subdev_fh *fh,
137 enum v4l2_subdev_format_whence which)
139 if (which == V4L2_SUBDEV_FORMAT_TRY)
140 return v4l2_subdev_get_try_crop(fh, RESZ_PAD_SINK);
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
152 static void resizer_set_filters(struct isp_res_device *res, const u16 *h_coeff,
155 struct isp_device *isp = to_isp_device(res);
156 u32 startaddr_h, startaddr_v, tmp_h, tmp_v;
159 startaddr_h = ISPRSZ_HFILT10;
160 startaddr_v = ISPRSZ_VFILT10;
162 for (i = 0; i < COEFF_CNT; i += 2) {
164 (h_coeff[i + 1] << ISPRSZ_HFILT_COEF1_SHIFT);
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);
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,
192 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
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,
202 enum v4l2_mbus_pixelcode pixelcode)
204 struct isp_device *isp = to_isp_device(res);
207 case V4L2_MBUS_FMT_YUYV8_1X16:
208 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
211 case V4L2_MBUS_FMT_UYVY8_1X16:
212 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
221 * resizer_set_phase - Setup horizontal and vertical starting phase
222 * @res: Device context.
223 * @h_phase: horizontal phase parameters.
224 * @v_phase: vertical phase parameters.
226 * Horizontal and vertical phase range is 0 to 7
228 static void resizer_set_phase(struct isp_res_device *res, u32 h_phase,
231 struct isp_device *isp = to_isp_device(res);
234 rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
235 ~(ISPRSZ_CNT_HSTPH_MASK | ISPRSZ_CNT_VSTPH_MASK);
236 rgval |= (h_phase << ISPRSZ_CNT_HSTPH_SHIFT) & ISPRSZ_CNT_HSTPH_MASK;
237 rgval |= (v_phase << ISPRSZ_CNT_VSTPH_SHIFT) & ISPRSZ_CNT_VSTPH_MASK;
239 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
243 * resizer_set_luma - Setup luminance enhancer parameters
244 * @res: Device context.
245 * @luma: Structure for luminance enhancer parameters.
249 * 0x1: [-1 2 -1]/2 high-pass filter
250 * 0x2: [-1 -2 6 -2 -1]/4 high-pass filter
253 * The data is coded in U4Q4 representation.
256 * The data is coded in U4Q4 representation.
259 * The data is coded in U8Q0 representation.
261 * The new luminance value is computed as:
262 * Y += HPF(Y) x max(GAIN, (HPF(Y) - CORE) x SLOP + 8) >> 4.
264 static void resizer_set_luma(struct isp_res_device *res,
265 struct resizer_luma_yenh *luma)
267 struct isp_device *isp = to_isp_device(res);
270 rgval = (luma->algo << ISPRSZ_YENH_ALGO_SHIFT)
271 & ISPRSZ_YENH_ALGO_MASK;
272 rgval |= (luma->gain << ISPRSZ_YENH_GAIN_SHIFT)
273 & ISPRSZ_YENH_GAIN_MASK;
274 rgval |= (luma->slope << ISPRSZ_YENH_SLOP_SHIFT)
275 & ISPRSZ_YENH_SLOP_MASK;
276 rgval |= (luma->core << ISPRSZ_YENH_CORE_SHIFT)
277 & ISPRSZ_YENH_CORE_MASK;
279 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_YENH);
283 * resizer_set_source - Input source select
284 * @res: Device context.
285 * @source: Input source type
287 * If this field is set to RESIZER_INPUT_VP, the resizer input is fed from
288 * Preview/CCDC engine, otherwise from memory.
290 static void resizer_set_source(struct isp_res_device *res,
291 enum resizer_input_entity source)
293 struct isp_device *isp = to_isp_device(res);
295 if (source == RESIZER_INPUT_MEMORY)
296 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
299 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
304 * resizer_set_ratio - Setup horizontal and vertical resizing value
305 * @res: Device context.
306 * @ratio: Structure for ratio parameters.
308 * Resizing range from 64 to 1024
310 static void resizer_set_ratio(struct isp_res_device *res,
311 const struct resizer_ratio *ratio)
313 struct isp_device *isp = to_isp_device(res);
314 const u16 *h_filter, *v_filter;
317 rgval = isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT) &
318 ~(ISPRSZ_CNT_HRSZ_MASK | ISPRSZ_CNT_VRSZ_MASK);
319 rgval |= ((ratio->horz - 1) << ISPRSZ_CNT_HRSZ_SHIFT)
320 & ISPRSZ_CNT_HRSZ_MASK;
321 rgval |= ((ratio->vert - 1) << ISPRSZ_CNT_VRSZ_SHIFT)
322 & ISPRSZ_CNT_VRSZ_MASK;
323 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
325 /* prepare horizontal filter coefficients */
326 if (ratio->horz > MID_RESIZE_VALUE)
327 h_filter = &filter_coefs.h_filter_coef_7tap[0];
329 h_filter = &filter_coefs.h_filter_coef_4tap[0];
331 /* prepare vertical filter coefficients */
332 if (ratio->vert > MID_RESIZE_VALUE)
333 v_filter = &filter_coefs.v_filter_coef_7tap[0];
335 v_filter = &filter_coefs.v_filter_coef_4tap[0];
337 resizer_set_filters(res, h_filter, v_filter);
341 * resizer_set_dst_size - Setup the output height and width
342 * @res: Device context.
343 * @width: Output width.
344 * @height: Output height.
347 * The value must be EVEN.
350 * The number of bytes written to SDRAM must be
351 * a multiple of 16-bytes if the vertical resizing factor
352 * is greater than 1x (upsizing)
354 static void resizer_set_output_size(struct isp_res_device *res,
355 u32 width, u32 height)
357 struct isp_device *isp = to_isp_device(res);
360 dev_dbg(isp->dev, "Output size[w/h]: %dx%d\n", width, height);
361 rgval = (width << ISPRSZ_OUT_SIZE_HORZ_SHIFT)
362 & ISPRSZ_OUT_SIZE_HORZ_MASK;
363 rgval |= (height << ISPRSZ_OUT_SIZE_VERT_SHIFT)
364 & ISPRSZ_OUT_SIZE_VERT_MASK;
365 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_OUT_SIZE);
369 * resizer_set_output_offset - Setup memory offset for the output lines.
370 * @res: Device context.
371 * @offset: Memory offset.
373 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
374 * boundary; the 5 LSBs are read-only. For optimal use of SDRAM bandwidth,
375 * the SDRAM line offset must be set on a 256-byte boundary
377 static void resizer_set_output_offset(struct isp_res_device *res, u32 offset)
379 struct isp_device *isp = to_isp_device(res);
381 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTOFF);
385 * resizer_set_start - Setup vertical and horizontal start position
386 * @res: Device context.
387 * @left: Horizontal start position.
388 * @top: Vertical start position.
390 * Vertical start line:
391 * This field makes sense only when the resizer obtains its input
392 * from the preview engine/CCDC
394 * Horizontal start pixel:
395 * Pixels are coded on 16 bits for YUV and 8 bits for color separate data.
396 * When the resizer gets its input from SDRAM, this field must be set
397 * to <= 15 for YUV 16-bit data and <= 31 for 8-bit color separate data
399 static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top)
401 struct isp_device *isp = to_isp_device(res);
404 rgval = (left << ISPRSZ_IN_START_HORZ_ST_SHIFT)
405 & ISPRSZ_IN_START_HORZ_ST_MASK;
406 rgval |= (top << ISPRSZ_IN_START_VERT_ST_SHIFT)
407 & ISPRSZ_IN_START_VERT_ST_MASK;
409 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_START);
413 * resizer_set_input_size - Setup the input size
414 * @res: Device context.
415 * @width: The range is 0 to 4095 pixels
416 * @height: The range is 0 to 4095 lines
418 static void resizer_set_input_size(struct isp_res_device *res,
419 u32 width, u32 height)
421 struct isp_device *isp = to_isp_device(res);
424 dev_dbg(isp->dev, "Input size[w/h]: %dx%d\n", width, height);
426 rgval = (width << ISPRSZ_IN_SIZE_HORZ_SHIFT)
427 & ISPRSZ_IN_SIZE_HORZ_MASK;
428 rgval |= (height << ISPRSZ_IN_SIZE_VERT_SHIFT)
429 & ISPRSZ_IN_SIZE_VERT_MASK;
431 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_SIZE);
435 * resizer_set_src_offs - Setup the memory offset for the input lines
436 * @res: Device context.
437 * @offset: Memory offset.
439 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
440 * boundary; the 5 LSBs are read-only. This field must be programmed to be
441 * 0x0 if the resizer input is from preview engine/CCDC.
443 static void resizer_set_input_offset(struct isp_res_device *res, u32 offset)
445 struct isp_device *isp = to_isp_device(res);
447 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INOFF);
451 * resizer_set_intype - Input type select
452 * @res: Device context.
453 * @type: Pixel format type.
455 static void resizer_set_intype(struct isp_res_device *res,
456 enum resizer_colors_type type)
458 struct isp_device *isp = to_isp_device(res);
460 if (type == RSZ_COLOR8)
461 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
464 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
469 * __resizer_set_inaddr - Helper function for set input address
470 * @res : pointer to resizer private data structure
471 * @addr: input address
474 static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr)
476 struct isp_device *isp = to_isp_device(res);
478 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INADD);
482 * The data rate at the horizontal resizer output must not exceed half the
483 * functional clock or 100 MP/s, whichever is lower. According to the TRM
484 * there's no similar requirement for the vertical resizer output. However
485 * experience showed that vertical upscaling by 4 leads to SBL overflows (with
486 * data rates at the resizer output exceeding 300 MP/s). Limiting the resizer
487 * output data rate to the functional clock or 200 MP/s, whichever is lower,
488 * seems to get rid of SBL overflows.
490 * The maximum data rate at the output of the horizontal resizer can thus be
493 * max intermediate rate <= L3 clock * input height / output height
494 * max intermediate rate <= L3 clock / 2
496 * The maximum data rate at the resizer input is then
498 * max input rate <= max intermediate rate * input width / output width
500 * where the input width and height are the resizer input crop rectangle size.
501 * The TRM doesn't clearly explain if that's a maximum instant data rate or a
502 * maximum average data rate.
504 void omap3isp_resizer_max_rate(struct isp_res_device *res,
505 unsigned int *max_rate)
507 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
508 const struct v4l2_mbus_framefmt *ofmt = &res->formats[RESZ_PAD_SOURCE];
509 unsigned long limit = min(pipe->l3_ick, 200000000UL);
512 clock = div_u64((u64)limit * res->crop.active.height, ofmt->height);
513 clock = min(clock, limit / 2);
514 *max_rate = div_u64((u64)clock * res->crop.active.width, ofmt->width);
518 * When the resizer processes images from memory, the driver must slow down read
519 * requests on the input to at least comply with the internal data rate
520 * requirements. If the application real-time requirements can cope with slower
521 * processing, the resizer can be slowed down even more to put less pressure on
522 * the overall system.
524 * When the resizer processes images on the fly (either from the CCDC or the
525 * preview module), the same data rate requirements apply but they can't be
526 * enforced at the resizer level. The image input module (sensor, CCP2 or
527 * preview module) must not provide image data faster than the resizer can
530 * For live image pipelines, the data rate is set by the frame format, size and
531 * rate. The sensor output frame rate must not exceed the maximum resizer data
534 * The resizer slows down read requests by inserting wait cycles in the SBL
535 * requests. The maximum number of 256-byte requests per second can be computed
536 * as (the data rate is multiplied by 2 to convert from pixels per second to
539 * request per second = data rate * 2 / 256
540 * cycles per request = cycles per second / requests per second
542 * The number of cycles per second is controlled by the L3 clock, leading to
544 * cycles per request = L3 frequency / 2 * 256 / data rate
546 static void resizer_adjust_bandwidth(struct isp_res_device *res)
548 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
549 struct isp_device *isp = to_isp_device(res);
550 unsigned long l3_ick = pipe->l3_ick;
551 struct v4l2_fract *timeperframe;
552 unsigned int cycles_per_frame;
553 unsigned int requests_per_frame;
554 unsigned int cycles_per_request;
555 unsigned int granularity;
556 unsigned int minimum;
557 unsigned int maximum;
560 if (res->input != RESIZER_INPUT_MEMORY) {
561 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
562 ISPSBL_SDR_REQ_RSZ_EXP_MASK);
566 switch (isp->revision) {
567 case ISP_REVISION_1_0:
568 case ISP_REVISION_2_0:
573 case ISP_REVISION_15_0:
578 /* Compute the minimum number of cycles per request, based on the
579 * pipeline maximum data rate. This is an absolute lower bound if we
580 * don't want SBL overflows, so round the value up.
582 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
584 minimum = DIV_ROUND_UP(cycles_per_request, granularity);
586 /* Compute the maximum number of cycles per request, based on the
587 * requested frame rate. This is a soft upper bound to achieve a frame
588 * rate equal or higher than the requested value, so round the value
591 timeperframe = &pipe->max_timeperframe;
593 requests_per_frame = DIV_ROUND_UP(res->crop.active.width * 2, 256)
594 * res->crop.active.height;
595 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
596 timeperframe->denominator);
597 cycles_per_request = cycles_per_frame / requests_per_frame;
599 maximum = cycles_per_request / granularity;
601 value = max(minimum, maximum);
603 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
604 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
605 ISPSBL_SDR_REQ_RSZ_EXP_MASK,
606 value << ISPSBL_SDR_REQ_RSZ_EXP_SHIFT);
610 * omap3isp_resizer_busy - Checks if ISP resizer is busy.
612 * Returns busy field from ISPRSZ_PCR register.
614 int omap3isp_resizer_busy(struct isp_res_device *res)
616 struct isp_device *isp = to_isp_device(res);
618 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR) &
623 * resizer_set_inaddr - Sets the memory address of the input frame.
624 * @addr: 32bit memory address aligned on 32byte boundary.
626 static void resizer_set_inaddr(struct isp_res_device *res, u32 addr)
628 res->addr_base = addr;
630 /* This will handle crop settings in stream off state */
631 if (res->crop_offset)
632 addr += res->crop_offset & ~0x1f;
634 __resizer_set_inaddr(res, addr);
638 * Configures the memory address to which the output frame is written.
639 * @addr: 32bit memory address aligned on 32byte boundary.
640 * Note: For SBL efficiency reasons the address should be on a 256-byte
643 static void resizer_set_outaddr(struct isp_res_device *res, u32 addr)
645 struct isp_device *isp = to_isp_device(res);
648 * Set output address. This needs to be in its own function
649 * because it changes often.
651 isp_reg_writel(isp, addr << ISPRSZ_SDR_OUTADD_ADDR_SHIFT,
652 OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTADD);
656 * resizer_print_status - Prints the values of the resizer module registers.
658 #define RSZ_PRINT_REGISTER(isp, name)\
659 dev_dbg(isp->dev, "###RSZ " #name "=0x%08x\n", \
660 isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_##name))
662 static void resizer_print_status(struct isp_res_device *res)
664 struct isp_device *isp = to_isp_device(res);
666 dev_dbg(isp->dev, "-------------Resizer Register dump----------\n");
668 RSZ_PRINT_REGISTER(isp, PCR);
669 RSZ_PRINT_REGISTER(isp, CNT);
670 RSZ_PRINT_REGISTER(isp, OUT_SIZE);
671 RSZ_PRINT_REGISTER(isp, IN_START);
672 RSZ_PRINT_REGISTER(isp, IN_SIZE);
673 RSZ_PRINT_REGISTER(isp, SDR_INADD);
674 RSZ_PRINT_REGISTER(isp, SDR_INOFF);
675 RSZ_PRINT_REGISTER(isp, SDR_OUTADD);
676 RSZ_PRINT_REGISTER(isp, SDR_OUTOFF);
677 RSZ_PRINT_REGISTER(isp, YENH);
679 dev_dbg(isp->dev, "--------------------------------------------\n");
683 * resizer_calc_ratios - Helper function for calculating resizer ratios
684 * @res: pointer to resizer private data structure
685 * @input: input frame size
686 * @output: output frame size
687 * @ratio : return calculated ratios
690 * The resizer uses a polyphase sample rate converter. The upsampling filter
691 * has a fixed number of phases that depend on the resizing ratio. As the ratio
692 * computation depends on the number of phases, we need to compute a first
693 * approximation and then refine it.
695 * The input/output/ratio relationship is given by the OMAP34xx TRM:
697 * - 8-phase, 4-tap mode (RSZ = 64 ~ 512)
698 * iw = (32 * sph + (ow - 1) * hrsz + 16) >> 8 + 7
699 * ih = (32 * spv + (oh - 1) * vrsz + 16) >> 8 + 4
700 * - 4-phase, 7-tap mode (RSZ = 513 ~ 1024)
701 * iw = (64 * sph + (ow - 1) * hrsz + 32) >> 8 + 7
702 * ih = (64 * spv + (oh - 1) * vrsz + 32) >> 8 + 7
704 * iw and ih are the input width and height after cropping. Those equations need
705 * to be satisfied exactly for the resizer to work correctly.
707 * The equations can't be easily reverted, as the >> 8 operation is not linear.
708 * In addition, not all input sizes can be achieved for a given output size. To
709 * get the highest input size lower than or equal to the requested input size,
710 * we need to compute the highest resizing ratio that satisfies the following
711 * inequality (taking the 4-tap mode width equation as an example)
713 * iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7
715 * (where iw is the requested input width) which can be rewritten as
717 * iw - 7 >= (32 * sph + (ow - 1) * hrsz + 16) >> 8
718 * (iw - 7) << 8 >= 32 * sph + (ow - 1) * hrsz + 16 - b
719 * ((iw - 7) << 8) + b >= 32 * sph + (ow - 1) * hrsz + 16
721 * where b is the value of the 8 least significant bits of the right hand side
722 * expression of the last inequality. The highest resizing ratio value will be
723 * achieved when b is equal to its maximum value of 255. That resizing ratio
724 * value will still satisfy the original inequality, as b will disappear when
725 * the expression will be shifted right by 8.
727 * The reverted equations thus become
729 * - 8-phase, 4-tap mode
730 * hrsz = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / (ow - 1)
731 * vrsz = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / (oh - 1)
732 * - 4-phase, 7-tap mode
733 * hrsz = ((iw - 7) * 256 + 255 - 32 - 64 * sph) / (ow - 1)
734 * vrsz = ((ih - 7) * 256 + 255 - 32 - 64 * spv) / (oh - 1)
736 * The ratios are integer values, and are rounded down to ensure that the
737 * cropped input size is not bigger than the uncropped input size.
739 * As the number of phases/taps, used to select the correct equations to compute
740 * the ratio, depends on the ratio, we start with the 4-tap mode equations to
741 * compute an approximation of the ratio, and switch to the 7-tap mode equations
742 * if the approximation is higher than the ratio threshold.
744 * As the 7-tap mode equations will return a ratio smaller than or equal to the
745 * 4-tap mode equations, the resulting ratio could become lower than or equal to
746 * the ratio threshold. This 'equations loop' isn't an issue as long as the
747 * correct equations are used to compute the final input size. Starting with the
748 * 4-tap mode equations ensure that, in case of values resulting in a 'ratio
749 * loop', the smallest of the ratio values will be used, never exceeding the
750 * requested input size.
752 * We first clamp the output size according to the hardware capability to avoid
753 * auto-cropping the input more than required to satisfy the TRM equations. The
754 * minimum output size is achieved with a scaling factor of 1024. It is thus
755 * computed using the 7-tap equations.
757 * min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1
758 * min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1
760 * Similarly, the maximum output size is achieved with a scaling factor of 64
761 * and computed using the 4-tap equations.
763 * max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1
764 * max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1
766 * The additional +255 term compensates for the round down operation performed
767 * by the TRM equations when shifting the value right by 8 bits.
769 * We then compute and clamp the ratios (x1/4 ~ x4). Clamping the output size to
770 * the maximum value guarantees that the ratio value will never be smaller than
771 * the minimum, but it could still slightly exceed the maximum. Clamping the
772 * ratio will thus result in a resizing factor slightly larger than the
775 * To accommodate that, and make sure the TRM equations are satisfied exactly, we
776 * compute the input crop rectangle as the last step.
778 * As if the situation wasn't complex enough, the maximum output width depends
779 * on the vertical resizing ratio. Fortunately, the output height doesn't
780 * depend on the horizontal resizing ratio. We can then start by computing the
781 * output height and the vertical ratio, and then move to computing the output
782 * width and the horizontal ratio.
784 static void resizer_calc_ratios(struct isp_res_device *res,
785 struct v4l2_rect *input,
786 struct v4l2_mbus_framefmt *output,
787 struct resizer_ratio *ratio)
789 struct isp_device *isp = to_isp_device(res);
790 const unsigned int spv = DEFAULT_PHASE;
791 const unsigned int sph = DEFAULT_PHASE;
792 unsigned int upscaled_width;
793 unsigned int upscaled_height;
794 unsigned int min_width;
795 unsigned int min_height;
796 unsigned int max_width;
797 unsigned int max_height;
798 unsigned int width_alignment;
803 * Clamp the output height based on the hardware capabilities and
804 * compute the vertical resizing ratio.
806 min_height = ((input->height - 7) * 256 - 32 - 64 * spv) / 1024 + 1;
807 min_height = max_t(unsigned int, min_height, MIN_OUT_HEIGHT);
808 max_height = ((input->height - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1;
809 max_height = min_t(unsigned int, max_height, MAX_OUT_HEIGHT);
810 output->height = clamp(output->height, min_height, max_height);
812 ratio->vert = ((input->height - 4) * 256 + 255 - 16 - 32 * spv)
813 / (output->height - 1);
814 if (ratio->vert > MID_RESIZE_VALUE)
815 ratio->vert = ((input->height - 7) * 256 + 255 - 32 - 64 * spv)
816 / (output->height - 1);
817 ratio->vert = clamp_t(unsigned int, ratio->vert,
818 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
820 if (ratio->vert <= MID_RESIZE_VALUE) {
821 upscaled_height = (output->height - 1) * ratio->vert
823 height = (upscaled_height >> 8) + 4;
825 upscaled_height = (output->height - 1) * ratio->vert
827 height = (upscaled_height >> 8) + 7;
831 * Compute the minimum and maximum output widths based on the hardware
832 * capabilities. The maximum depends on the vertical resizing ratio.
834 min_width = ((input->width - 7) * 256 - 32 - 64 * sph) / 1024 + 1;
835 min_width = max_t(unsigned int, min_width, MIN_OUT_WIDTH);
837 if (ratio->vert <= MID_RESIZE_VALUE) {
838 switch (isp->revision) {
839 case ISP_REVISION_1_0:
840 max_width = MAX_4TAP_OUT_WIDTH_ES1;
843 case ISP_REVISION_2_0:
845 max_width = MAX_4TAP_OUT_WIDTH_ES2;
848 case ISP_REVISION_15_0:
849 max_width = MAX_4TAP_OUT_WIDTH_3630;
853 switch (isp->revision) {
854 case ISP_REVISION_1_0:
855 max_width = MAX_7TAP_OUT_WIDTH_ES1;
858 case ISP_REVISION_2_0:
860 max_width = MAX_7TAP_OUT_WIDTH_ES2;
863 case ISP_REVISION_15_0:
864 max_width = MAX_7TAP_OUT_WIDTH_3630;
868 max_width = min(((input->width - 7) * 256 + 255 - 16 - 32 * sph) / 64
872 * The output width must be even, and must be a multiple of 16 bytes
873 * when upscaling vertically. Clamp the output width to the valid range.
874 * Take the alignment into account (the maximum width in 7-tap mode on
875 * ES2 isn't a multiple of 8) and align the result up to make sure it
876 * won't be smaller than the minimum.
878 width_alignment = ratio->vert < 256 ? 8 : 2;
879 output->width = clamp(output->width, min_width,
880 max_width & ~(width_alignment - 1));
881 output->width = ALIGN(output->width, width_alignment);
883 ratio->horz = ((input->width - 7) * 256 + 255 - 16 - 32 * sph)
884 / (output->width - 1);
885 if (ratio->horz > MID_RESIZE_VALUE)
886 ratio->horz = ((input->width - 7) * 256 + 255 - 32 - 64 * sph)
887 / (output->width - 1);
888 ratio->horz = clamp_t(unsigned int, ratio->horz,
889 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
891 if (ratio->horz <= MID_RESIZE_VALUE) {
892 upscaled_width = (output->width - 1) * ratio->horz
894 width = (upscaled_width >> 8) + 7;
896 upscaled_width = (output->width - 1) * ratio->horz
898 width = (upscaled_width >> 8) + 7;
901 /* Center the new crop rectangle. */
902 input->left += (input->width - width) / 2;
903 input->top += (input->height - height) / 2;
904 input->width = width;
905 input->height = height;
909 * resizer_set_crop_params - Setup hardware with cropping parameters
910 * @res : resizer private structure
911 * @input : format on sink pad
912 * @output : format on source pad
915 static void resizer_set_crop_params(struct isp_res_device *res,
916 const struct v4l2_mbus_framefmt *input,
917 const struct v4l2_mbus_framefmt *output)
919 resizer_set_ratio(res, &res->ratio);
921 /* Set chrominance horizontal algorithm */
922 if (res->ratio.horz >= RESIZE_DIVISOR)
923 resizer_set_bilinear(res, RSZ_THE_SAME);
925 resizer_set_bilinear(res, RSZ_BILINEAR);
927 resizer_adjust_bandwidth(res);
929 if (res->input == RESIZER_INPUT_MEMORY) {
930 /* Calculate additional offset for crop */
931 res->crop_offset = (res->crop.active.top * input->width +
932 res->crop.active.left) * 2;
934 * Write lowest 4 bits of horizontal pixel offset (in pixels),
935 * vertical start must be 0.
937 resizer_set_start(res, (res->crop_offset / 2) & 0xf, 0);
940 * Set start (read) address for cropping, in bytes.
941 * Lowest 5 bits must be zero.
943 __resizer_set_inaddr(res,
944 res->addr_base + (res->crop_offset & ~0x1f));
947 * Set vertical start line and horizontal starting pixel.
948 * If the input is from CCDC/PREV, horizontal start field is
949 * in bytes (twice number of pixels).
951 resizer_set_start(res, res->crop.active.left * 2,
952 res->crop.active.top);
953 /* Input address and offset must be 0 for preview/ccdc input */
954 __resizer_set_inaddr(res, 0);
955 resizer_set_input_offset(res, 0);
958 /* Set the input size */
959 resizer_set_input_size(res, res->crop.active.width,
960 res->crop.active.height);
963 static void resizer_configure(struct isp_res_device *res)
965 struct v4l2_mbus_framefmt *informat, *outformat;
966 struct resizer_luma_yenh luma = {0, 0, 0, 0};
968 resizer_set_source(res, res->input);
970 informat = &res->formats[RESZ_PAD_SINK];
971 outformat = &res->formats[RESZ_PAD_SOURCE];
974 if (res->input == RESIZER_INPUT_VP)
975 resizer_set_input_offset(res, 0);
977 resizer_set_input_offset(res, ALIGN(informat->width, 0x10) * 2);
979 /* YUV422 interleaved, default phase, no luma enhancement */
980 resizer_set_intype(res, RSZ_YUV422);
981 resizer_set_ycpos(res, informat->code);
982 resizer_set_phase(res, DEFAULT_PHASE, DEFAULT_PHASE);
983 resizer_set_luma(res, &luma);
985 /* RESZ_PAD_SOURCE */
986 resizer_set_output_offset(res, ALIGN(outformat->width * 2, 32));
987 resizer_set_output_size(res, outformat->width, outformat->height);
989 resizer_set_crop_params(res, informat, outformat);
992 /* -----------------------------------------------------------------------------
996 static void resizer_enable_oneshot(struct isp_res_device *res)
998 struct isp_device *isp = to_isp_device(res);
1000 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR,
1001 ISPRSZ_PCR_ENABLE | ISPRSZ_PCR_ONESHOT);
1004 void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res)
1007 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1008 * condition, the module was paused and now we have a buffer queued
1009 * on the output again. Restart the pipeline if running in continuous
1012 if (res->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1013 res->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1014 resizer_enable_oneshot(res);
1015 isp_video_dmaqueue_flags_clr(&res->video_out);
1019 static void resizer_isr_buffer(struct isp_res_device *res)
1021 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
1022 struct isp_buffer *buffer;
1025 if (res->state == ISP_PIPELINE_STREAM_STOPPED)
1028 /* Complete the output buffer and, if reading from memory, the input
1031 buffer = omap3isp_video_buffer_next(&res->video_out);
1032 if (buffer != NULL) {
1033 resizer_set_outaddr(res, buffer->dma);
1037 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1039 if (res->input == RESIZER_INPUT_MEMORY) {
1040 buffer = omap3isp_video_buffer_next(&res->video_in);
1042 resizer_set_inaddr(res, buffer->dma);
1043 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1046 if (res->state == ISP_PIPELINE_STREAM_SINGLESHOT) {
1047 if (isp_pipeline_ready(pipe))
1048 omap3isp_pipeline_set_stream(pipe,
1049 ISP_PIPELINE_STREAM_SINGLESHOT);
1051 /* If an underrun occurs, the video queue operation handler will
1052 * restart the resizer. Otherwise restart it immediately.
1055 resizer_enable_oneshot(res);
1060 * omap3isp_resizer_isr - ISP resizer interrupt handler
1062 * Manage the resizer video buffers and configure shadowed and busy-locked
1065 void omap3isp_resizer_isr(struct isp_res_device *res)
1067 struct v4l2_mbus_framefmt *informat, *outformat;
1069 if (omap3isp_module_sync_is_stopping(&res->wait, &res->stopping))
1072 if (res->applycrop) {
1073 outformat = __resizer_get_format(res, NULL, RESZ_PAD_SOURCE,
1074 V4L2_SUBDEV_FORMAT_ACTIVE);
1075 informat = __resizer_get_format(res, NULL, RESZ_PAD_SINK,
1076 V4L2_SUBDEV_FORMAT_ACTIVE);
1077 resizer_set_crop_params(res, informat, outformat);
1081 resizer_isr_buffer(res);
1084 /* -----------------------------------------------------------------------------
1085 * ISP video operations
1088 static int resizer_video_queue(struct isp_video *video,
1089 struct isp_buffer *buffer)
1091 struct isp_res_device *res = &video->isp->isp_res;
1093 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1094 resizer_set_inaddr(res, buffer->dma);
1097 * We now have a buffer queued on the output. Despite what the
1098 * TRM says, the resizer can't be restarted immediately.
1099 * Enabling it in one shot mode in the middle of a frame (or at
1100 * least asynchronously to the frame) results in the output
1101 * being shifted randomly left/right and up/down, as if the
1102 * hardware didn't synchronize itself to the beginning of the
1105 * Restart the resizer on the next sync interrupt if running in
1106 * continuous mode or when starting the stream.
1108 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1109 resizer_set_outaddr(res, buffer->dma);
1114 static const struct isp_video_operations resizer_video_ops = {
1115 .queue = resizer_video_queue,
1118 /* -----------------------------------------------------------------------------
1119 * V4L2 subdev operations
1123 * resizer_set_stream - Enable/Disable streaming on resizer subdev
1124 * @sd: ISP resizer V4L2 subdev
1125 * @enable: 1 == Enable, 0 == Disable
1127 * The resizer hardware can't be enabled without a memory buffer to write to.
1128 * As the s_stream operation is called in response to a STREAMON call without
1129 * any buffer queued yet, just update the state field and return immediately.
1130 * The resizer will be enabled in resizer_video_queue().
1132 static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
1134 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1135 struct isp_video *video_out = &res->video_out;
1136 struct isp_device *isp = to_isp_device(res);
1137 struct device *dev = to_device(res);
1139 if (res->state == ISP_PIPELINE_STREAM_STOPPED) {
1140 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1143 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1144 resizer_configure(res);
1145 resizer_print_status(res);
1149 case ISP_PIPELINE_STREAM_CONTINUOUS:
1150 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1151 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1152 resizer_enable_oneshot(res);
1153 isp_video_dmaqueue_flags_clr(video_out);
1157 case ISP_PIPELINE_STREAM_SINGLESHOT:
1158 if (res->input == RESIZER_INPUT_MEMORY)
1159 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_READ);
1160 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1162 resizer_enable_oneshot(res);
1165 case ISP_PIPELINE_STREAM_STOPPED:
1166 if (omap3isp_module_sync_idle(&sd->entity, &res->wait,
1168 dev_dbg(dev, "%s: module stop timeout.\n", sd->name);
1169 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_RESIZER_READ |
1170 OMAP3_ISP_SBL_RESIZER_WRITE);
1171 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1172 isp_video_dmaqueue_flags_clr(video_out);
1176 res->state = enable;
1181 * resizer_try_crop - mangles crop parameters.
1183 static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
1184 const struct v4l2_mbus_framefmt *source,
1185 struct v4l2_rect *crop)
1187 const unsigned int spv = DEFAULT_PHASE;
1188 const unsigned int sph = DEFAULT_PHASE;
1190 /* Crop rectangle is constrained by the output size so that zoom ratio
1191 * cannot exceed +/-4.0.
1193 unsigned int min_width =
1194 ((32 * sph + (source->width - 1) * 64 + 16) >> 8) + 7;
1195 unsigned int min_height =
1196 ((32 * spv + (source->height - 1) * 64 + 16) >> 8) + 4;
1197 unsigned int max_width =
1198 ((64 * sph + (source->width - 1) * 1024 + 32) >> 8) + 7;
1199 unsigned int max_height =
1200 ((64 * spv + (source->height - 1) * 1024 + 32) >> 8) + 7;
1202 crop->width = clamp_t(u32, crop->width, min_width, max_width);
1203 crop->height = clamp_t(u32, crop->height, min_height, max_height);
1205 /* Crop can not go beyond of the input rectangle */
1206 crop->left = clamp_t(u32, crop->left, 0, sink->width - MIN_IN_WIDTH);
1207 crop->width = clamp_t(u32, crop->width, MIN_IN_WIDTH,
1208 sink->width - crop->left);
1209 crop->top = clamp_t(u32, crop->top, 0, sink->height - MIN_IN_HEIGHT);
1210 crop->height = clamp_t(u32, crop->height, MIN_IN_HEIGHT,
1211 sink->height - crop->top);
1215 * resizer_get_selection - Retrieve a selection rectangle on a pad
1216 * @sd: ISP resizer V4L2 subdevice
1217 * @fh: V4L2 subdev file handle
1218 * @sel: Selection rectangle
1220 * The only supported rectangles are the crop rectangles on the sink pad.
1222 * Return 0 on success or a negative error code otherwise.
1224 static int resizer_get_selection(struct v4l2_subdev *sd,
1225 struct v4l2_subdev_fh *fh,
1226 struct v4l2_subdev_selection *sel)
1228 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1229 struct v4l2_mbus_framefmt *format_source;
1230 struct v4l2_mbus_framefmt *format_sink;
1231 struct resizer_ratio ratio;
1233 if (sel->pad != RESZ_PAD_SINK)
1236 format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1238 format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1241 switch (sel->target) {
1242 case V4L2_SEL_TGT_CROP_BOUNDS:
1245 sel->r.width = INT_MAX;
1246 sel->r.height = INT_MAX;
1248 resizer_try_crop(format_sink, format_source, &sel->r);
1249 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1252 case V4L2_SEL_TGT_CROP:
1253 sel->r = *__resizer_get_crop(res, fh, sel->which);
1254 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1265 * resizer_set_selection - Set a selection rectangle on a pad
1266 * @sd: ISP resizer V4L2 subdevice
1267 * @fh: V4L2 subdev file handle
1268 * @sel: Selection rectangle
1270 * The only supported rectangle is the actual crop rectangle on the sink pad.
1272 * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag
1275 * Return 0 on success or a negative error code otherwise.
1277 static int resizer_set_selection(struct v4l2_subdev *sd,
1278 struct v4l2_subdev_fh *fh,
1279 struct v4l2_subdev_selection *sel)
1281 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1282 struct isp_device *isp = to_isp_device(res);
1283 struct v4l2_mbus_framefmt *format_sink, *format_source;
1284 struct resizer_ratio ratio;
1286 if (sel->target != V4L2_SEL_TGT_CROP ||
1287 sel->pad != RESZ_PAD_SINK)
1290 format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1292 format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1295 dev_dbg(isp->dev, "%s: L=%d,T=%d,W=%d,H=%d,which=%d\n", __func__,
1296 sel->r.left, sel->r.top, sel->r.width, sel->r.height,
1299 dev_dbg(isp->dev, "%s: input=%dx%d, output=%dx%d\n", __func__,
1300 format_sink->width, format_sink->height,
1301 format_source->width, format_source->height);
1303 /* Clamp the crop rectangle to the bounds, and then mangle it further to
1304 * fulfill the TRM equations. Store the clamped but otherwise unmangled
1305 * rectangle to avoid cropping the input multiple times: when an
1306 * application sets the output format, the current crop rectangle is
1307 * mangled during crop rectangle computation, which would lead to a new,
1308 * smaller input crop rectangle every time the output size is set if we
1309 * stored the mangled rectangle.
1311 resizer_try_crop(format_sink, format_source, &sel->r);
1312 *__resizer_get_crop(res, fh, sel->which) = sel->r;
1313 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1315 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1319 res->crop.active = sel->r;
1322 * set_selection can be called while streaming is on. In this case the
1323 * crop values will be set in the next IRQ.
1325 if (res->state != ISP_PIPELINE_STREAM_STOPPED)
1331 /* resizer pixel formats */
1332 static const unsigned int resizer_formats[] = {
1333 V4L2_MBUS_FMT_UYVY8_1X16,
1334 V4L2_MBUS_FMT_YUYV8_1X16,
1337 static unsigned int resizer_max_in_width(struct isp_res_device *res)
1339 struct isp_device *isp = to_isp_device(res);
1341 if (res->input == RESIZER_INPUT_MEMORY) {
1342 return MAX_IN_WIDTH_MEMORY_MODE;
1344 if (isp->revision == ISP_REVISION_1_0)
1345 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
1347 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
1352 * resizer_try_format - Handle try format by pad subdev method
1353 * @res : ISP resizer device
1354 * @fh : V4L2 subdev file handle
1356 * @fmt : pointer to v4l2 format structure
1357 * @which : wanted subdev format
1359 static void resizer_try_format(struct isp_res_device *res,
1360 struct v4l2_subdev_fh *fh, unsigned int pad,
1361 struct v4l2_mbus_framefmt *fmt,
1362 enum v4l2_subdev_format_whence which)
1364 struct v4l2_mbus_framefmt *format;
1365 struct resizer_ratio ratio;
1366 struct v4l2_rect crop;
1370 if (fmt->code != V4L2_MBUS_FMT_YUYV8_1X16 &&
1371 fmt->code != V4L2_MBUS_FMT_UYVY8_1X16)
1372 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1374 fmt->width = clamp_t(u32, fmt->width, MIN_IN_WIDTH,
1375 resizer_max_in_width(res));
1376 fmt->height = clamp_t(u32, fmt->height, MIN_IN_HEIGHT,
1380 case RESZ_PAD_SOURCE:
1381 format = __resizer_get_format(res, fh, RESZ_PAD_SINK, which);
1382 fmt->code = format->code;
1384 crop = *__resizer_get_crop(res, fh, which);
1385 resizer_calc_ratios(res, &crop, fmt, &ratio);
1389 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1390 fmt->field = V4L2_FIELD_NONE;
1394 * resizer_enum_mbus_code - Handle pixel format enumeration
1395 * @sd : pointer to v4l2 subdev structure
1396 * @fh : V4L2 subdev file handle
1397 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1398 * return -EINVAL or zero on success
1400 static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
1401 struct v4l2_subdev_fh *fh,
1402 struct v4l2_subdev_mbus_code_enum *code)
1404 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1405 struct v4l2_mbus_framefmt *format;
1407 if (code->pad == RESZ_PAD_SINK) {
1408 if (code->index >= ARRAY_SIZE(resizer_formats))
1411 code->code = resizer_formats[code->index];
1413 if (code->index != 0)
1416 format = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1417 V4L2_SUBDEV_FORMAT_TRY);
1418 code->code = format->code;
1424 static int resizer_enum_frame_size(struct v4l2_subdev *sd,
1425 struct v4l2_subdev_fh *fh,
1426 struct v4l2_subdev_frame_size_enum *fse)
1428 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1429 struct v4l2_mbus_framefmt format;
1431 if (fse->index != 0)
1434 format.code = fse->code;
1437 resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1438 fse->min_width = format.width;
1439 fse->min_height = format.height;
1441 if (format.code != fse->code)
1444 format.code = fse->code;
1447 resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1448 fse->max_width = format.width;
1449 fse->max_height = format.height;
1455 * resizer_get_format - Handle get format by pads subdev method
1456 * @sd : pointer to v4l2 subdev structure
1457 * @fh : V4L2 subdev file handle
1458 * @fmt : pointer to v4l2 subdev format structure
1459 * return -EINVAL or zero on success
1461 static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1462 struct v4l2_subdev_format *fmt)
1464 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1465 struct v4l2_mbus_framefmt *format;
1467 format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1471 fmt->format = *format;
1476 * resizer_set_format - Handle set format by pads subdev method
1477 * @sd : pointer to v4l2 subdev structure
1478 * @fh : V4L2 subdev file handle
1479 * @fmt : pointer to v4l2 subdev format structure
1480 * return -EINVAL or zero on success
1482 static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1483 struct v4l2_subdev_format *fmt)
1485 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1486 struct v4l2_mbus_framefmt *format;
1487 struct v4l2_rect *crop;
1489 format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1493 resizer_try_format(res, fh, fmt->pad, &fmt->format, fmt->which);
1494 *format = fmt->format;
1496 if (fmt->pad == RESZ_PAD_SINK) {
1497 /* reset crop rectangle */
1498 crop = __resizer_get_crop(res, fh, fmt->which);
1501 crop->width = fmt->format.width;
1502 crop->height = fmt->format.height;
1504 /* Propagate the format from sink to source */
1505 format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1507 *format = fmt->format;
1508 resizer_try_format(res, fh, RESZ_PAD_SOURCE, format,
1512 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1513 /* Compute and store the active crop rectangle and resizer
1514 * ratios. format already points to the source pad active
1517 res->crop.active = res->crop.request;
1518 resizer_calc_ratios(res, &res->crop.active, format,
1525 static int resizer_link_validate(struct v4l2_subdev *sd,
1526 struct media_link *link,
1527 struct v4l2_subdev_format *source_fmt,
1528 struct v4l2_subdev_format *sink_fmt)
1530 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1531 struct isp_pipeline *pipe = to_isp_pipeline(&sd->entity);
1533 omap3isp_resizer_max_rate(res, &pipe->max_rate);
1535 return v4l2_subdev_link_validate_default(sd, link,
1536 source_fmt, sink_fmt);
1540 * resizer_init_formats - Initialize formats on all pads
1541 * @sd: ISP resizer V4L2 subdevice
1542 * @fh: V4L2 subdev file handle
1544 * Initialize all pad formats with default values. If fh is not NULL, try
1545 * formats are initialized on the file handle. Otherwise active formats are
1546 * initialized on the device.
1548 static int resizer_init_formats(struct v4l2_subdev *sd,
1549 struct v4l2_subdev_fh *fh)
1551 struct v4l2_subdev_format format;
1553 memset(&format, 0, sizeof(format));
1554 format.pad = RESZ_PAD_SINK;
1555 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1556 format.format.code = V4L2_MBUS_FMT_YUYV8_1X16;
1557 format.format.width = 4096;
1558 format.format.height = 4096;
1559 resizer_set_format(sd, fh, &format);
1564 /* subdev video operations */
1565 static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
1566 .s_stream = resizer_set_stream,
1569 /* subdev pad operations */
1570 static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
1571 .enum_mbus_code = resizer_enum_mbus_code,
1572 .enum_frame_size = resizer_enum_frame_size,
1573 .get_fmt = resizer_get_format,
1574 .set_fmt = resizer_set_format,
1575 .get_selection = resizer_get_selection,
1576 .set_selection = resizer_set_selection,
1577 .link_validate = resizer_link_validate,
1580 /* subdev operations */
1581 static const struct v4l2_subdev_ops resizer_v4l2_ops = {
1582 .video = &resizer_v4l2_video_ops,
1583 .pad = &resizer_v4l2_pad_ops,
1586 /* subdev internal operations */
1587 static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
1588 .open = resizer_init_formats,
1591 /* -----------------------------------------------------------------------------
1592 * Media entity operations
1596 * resizer_link_setup - Setup resizer connections.
1597 * @entity : Pointer to media entity structure
1598 * @local : Pointer to local pad array
1599 * @remote : Pointer to remote pad array
1600 * @flags : Link flags
1601 * return -EINVAL or zero on success
1603 static int resizer_link_setup(struct media_entity *entity,
1604 const struct media_pad *local,
1605 const struct media_pad *remote, u32 flags)
1607 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1608 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1610 switch (local->index | media_entity_type(remote->entity)) {
1611 case RESZ_PAD_SINK | MEDIA_ENT_T_DEVNODE:
1612 /* read from memory */
1613 if (flags & MEDIA_LNK_FL_ENABLED) {
1614 if (res->input == RESIZER_INPUT_VP)
1616 res->input = RESIZER_INPUT_MEMORY;
1618 if (res->input == RESIZER_INPUT_MEMORY)
1619 res->input = RESIZER_INPUT_NONE;
1623 case RESZ_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
1624 /* read from ccdc or previewer */
1625 if (flags & MEDIA_LNK_FL_ENABLED) {
1626 if (res->input == RESIZER_INPUT_MEMORY)
1628 res->input = RESIZER_INPUT_VP;
1630 if (res->input == RESIZER_INPUT_VP)
1631 res->input = RESIZER_INPUT_NONE;
1635 case RESZ_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
1636 /* resizer always write to memory */
1646 /* media operations */
1647 static const struct media_entity_operations resizer_media_ops = {
1648 .link_setup = resizer_link_setup,
1649 .link_validate = v4l2_subdev_link_validate,
1652 void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
1654 v4l2_device_unregister_subdev(&res->subdev);
1655 omap3isp_video_unregister(&res->video_in);
1656 omap3isp_video_unregister(&res->video_out);
1659 int omap3isp_resizer_register_entities(struct isp_res_device *res,
1660 struct v4l2_device *vdev)
1664 /* Register the subdev and video nodes. */
1665 ret = v4l2_device_register_subdev(vdev, &res->subdev);
1669 ret = omap3isp_video_register(&res->video_in, vdev);
1673 ret = omap3isp_video_register(&res->video_out, vdev);
1680 omap3isp_resizer_unregister_entities(res);
1684 /* -----------------------------------------------------------------------------
1685 * ISP resizer initialization and cleanup
1689 * resizer_init_entities - Initialize resizer subdev and media entity.
1690 * @res : Pointer to resizer device structure
1691 * return -ENOMEM or zero on success
1693 static int resizer_init_entities(struct isp_res_device *res)
1695 struct v4l2_subdev *sd = &res->subdev;
1696 struct media_pad *pads = res->pads;
1697 struct media_entity *me = &sd->entity;
1700 res->input = RESIZER_INPUT_NONE;
1702 v4l2_subdev_init(sd, &resizer_v4l2_ops);
1703 sd->internal_ops = &resizer_v4l2_internal_ops;
1704 strlcpy(sd->name, "OMAP3 ISP resizer", sizeof(sd->name));
1705 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
1706 v4l2_set_subdevdata(sd, res);
1707 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1709 pads[RESZ_PAD_SINK].flags = MEDIA_PAD_FL_SINK
1710 | MEDIA_PAD_FL_MUST_CONNECT;
1711 pads[RESZ_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1713 me->ops = &resizer_media_ops;
1714 ret = media_entity_init(me, RESZ_PADS_NUM, pads, 0);
1718 resizer_init_formats(sd, NULL);
1720 res->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1721 res->video_in.ops = &resizer_video_ops;
1722 res->video_in.isp = to_isp_device(res);
1723 res->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1724 res->video_in.bpl_alignment = 32;
1725 res->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1726 res->video_out.ops = &resizer_video_ops;
1727 res->video_out.isp = to_isp_device(res);
1728 res->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1729 res->video_out.bpl_alignment = 32;
1731 ret = omap3isp_video_init(&res->video_in, "resizer");
1733 goto error_video_in;
1735 ret = omap3isp_video_init(&res->video_out, "resizer");
1737 goto error_video_out;
1739 res->video_out.video.entity.flags |= MEDIA_ENT_FL_DEFAULT;
1741 /* Connect the video nodes to the resizer subdev. */
1742 ret = media_entity_create_link(&res->video_in.video.entity, 0,
1743 &res->subdev.entity, RESZ_PAD_SINK, 0);
1747 ret = media_entity_create_link(&res->subdev.entity, RESZ_PAD_SOURCE,
1748 &res->video_out.video.entity, 0, 0);
1755 omap3isp_video_cleanup(&res->video_out);
1757 omap3isp_video_cleanup(&res->video_in);
1759 media_entity_cleanup(&res->subdev.entity);
1764 * isp_resizer_init - Resizer initialization.
1765 * @isp : Pointer to ISP device
1766 * return -ENOMEM or zero on success
1768 int omap3isp_resizer_init(struct isp_device *isp)
1770 struct isp_res_device *res = &isp->isp_res;
1772 init_waitqueue_head(&res->wait);
1773 atomic_set(&res->stopping, 0);
1774 return resizer_init_entities(res);
1777 void omap3isp_resizer_cleanup(struct isp_device *isp)
1779 struct isp_res_device *res = &isp->isp_res;
1781 omap3isp_video_cleanup(&res->video_in);
1782 omap3isp_video_cleanup(&res->video_out);
1783 media_entity_cleanup(&res->subdev.entity);