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 rgval = (width << ISPRSZ_OUT_SIZE_HORZ_SHIFT)
361 & ISPRSZ_OUT_SIZE_HORZ_MASK;
362 rgval |= (height << ISPRSZ_OUT_SIZE_VERT_SHIFT)
363 & ISPRSZ_OUT_SIZE_VERT_MASK;
364 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_OUT_SIZE);
368 * resizer_set_output_offset - Setup memory offset for the output lines.
369 * @res: Device context.
370 * @offset: Memory offset.
372 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
373 * boundary; the 5 LSBs are read-only. For optimal use of SDRAM bandwidth,
374 * the SDRAM line offset must be set on a 256-byte boundary
376 static void resizer_set_output_offset(struct isp_res_device *res, u32 offset)
378 struct isp_device *isp = to_isp_device(res);
380 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTOFF);
384 * resizer_set_start - Setup vertical and horizontal start position
385 * @res: Device context.
386 * @left: Horizontal start position.
387 * @top: Vertical start position.
389 * Vertical start line:
390 * This field makes sense only when the resizer obtains its input
391 * from the preview engine/CCDC
393 * Horizontal start pixel:
394 * Pixels are coded on 16 bits for YUV and 8 bits for color separate data.
395 * When the resizer gets its input from SDRAM, this field must be set
396 * to <= 15 for YUV 16-bit data and <= 31 for 8-bit color separate data
398 static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top)
400 struct isp_device *isp = to_isp_device(res);
403 rgval = (left << ISPRSZ_IN_START_HORZ_ST_SHIFT)
404 & ISPRSZ_IN_START_HORZ_ST_MASK;
405 rgval |= (top << ISPRSZ_IN_START_VERT_ST_SHIFT)
406 & ISPRSZ_IN_START_VERT_ST_MASK;
408 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_START);
412 * resizer_set_input_size - Setup the input size
413 * @res: Device context.
414 * @width: The range is 0 to 4095 pixels
415 * @height: The range is 0 to 4095 lines
417 static void resizer_set_input_size(struct isp_res_device *res,
418 u32 width, u32 height)
420 struct isp_device *isp = to_isp_device(res);
423 rgval = (width << ISPRSZ_IN_SIZE_HORZ_SHIFT)
424 & ISPRSZ_IN_SIZE_HORZ_MASK;
425 rgval |= (height << ISPRSZ_IN_SIZE_VERT_SHIFT)
426 & ISPRSZ_IN_SIZE_VERT_MASK;
428 isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_SIZE);
432 * resizer_set_src_offs - Setup the memory offset for the input lines
433 * @res: Device context.
434 * @offset: Memory offset.
436 * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte
437 * boundary; the 5 LSBs are read-only. This field must be programmed to be
438 * 0x0 if the resizer input is from preview engine/CCDC.
440 static void resizer_set_input_offset(struct isp_res_device *res, u32 offset)
442 struct isp_device *isp = to_isp_device(res);
444 isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INOFF);
448 * resizer_set_intype - Input type select
449 * @res: Device context.
450 * @type: Pixel format type.
452 static void resizer_set_intype(struct isp_res_device *res,
453 enum resizer_colors_type type)
455 struct isp_device *isp = to_isp_device(res);
457 if (type == RSZ_COLOR8)
458 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
461 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
466 * __resizer_set_inaddr - Helper function for set input address
467 * @res : pointer to resizer private data structure
468 * @addr: input address
471 static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr)
473 struct isp_device *isp = to_isp_device(res);
475 isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INADD);
479 * The data rate at the horizontal resizer output must not exceed half the
480 * functional clock or 100 MP/s, whichever is lower. According to the TRM
481 * there's no similar requirement for the vertical resizer output. However
482 * experience showed that vertical upscaling by 4 leads to SBL overflows (with
483 * data rates at the resizer output exceeding 300 MP/s). Limiting the resizer
484 * output data rate to the functional clock or 200 MP/s, whichever is lower,
485 * seems to get rid of SBL overflows.
487 * The maximum data rate at the output of the horizontal resizer can thus be
490 * max intermediate rate <= L3 clock * input height / output height
491 * max intermediate rate <= L3 clock / 2
493 * The maximum data rate at the resizer input is then
495 * max input rate <= max intermediate rate * input width / output width
497 * where the input width and height are the resizer input crop rectangle size.
498 * The TRM doesn't clearly explain if that's a maximum instant data rate or a
499 * maximum average data rate.
501 void omap3isp_resizer_max_rate(struct isp_res_device *res,
502 unsigned int *max_rate)
504 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
505 const struct v4l2_mbus_framefmt *ofmt = &res->formats[RESZ_PAD_SOURCE];
506 unsigned long limit = min(pipe->l3_ick, 200000000UL);
509 clock = div_u64((u64)limit * res->crop.active.height, ofmt->height);
510 clock = min(clock, limit / 2);
511 *max_rate = div_u64((u64)clock * res->crop.active.width, ofmt->width);
515 * When the resizer processes images from memory, the driver must slow down read
516 * requests on the input to at least comply with the internal data rate
517 * requirements. If the application real-time requirements can cope with slower
518 * processing, the resizer can be slowed down even more to put less pressure on
519 * the overall system.
521 * When the resizer processes images on the fly (either from the CCDC or the
522 * preview module), the same data rate requirements apply but they can't be
523 * enforced at the resizer level. The image input module (sensor, CCP2 or
524 * preview module) must not provide image data faster than the resizer can
527 * For live image pipelines, the data rate is set by the frame format, size and
528 * rate. The sensor output frame rate must not exceed the maximum resizer data
531 * The resizer slows down read requests by inserting wait cycles in the SBL
532 * requests. The maximum number of 256-byte requests per second can be computed
533 * as (the data rate is multiplied by 2 to convert from pixels per second to
536 * request per second = data rate * 2 / 256
537 * cycles per request = cycles per second / requests per second
539 * The number of cycles per second is controlled by the L3 clock, leading to
541 * cycles per request = L3 frequency / 2 * 256 / data rate
543 static void resizer_adjust_bandwidth(struct isp_res_device *res)
545 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
546 struct isp_device *isp = to_isp_device(res);
547 unsigned long l3_ick = pipe->l3_ick;
548 struct v4l2_fract *timeperframe;
549 unsigned int cycles_per_frame;
550 unsigned int requests_per_frame;
551 unsigned int cycles_per_request;
552 unsigned int granularity;
553 unsigned int minimum;
554 unsigned int maximum;
557 if (res->input != RESIZER_INPUT_MEMORY) {
558 isp_reg_clr(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
559 ISPSBL_SDR_REQ_RSZ_EXP_MASK);
563 switch (isp->revision) {
564 case ISP_REVISION_1_0:
565 case ISP_REVISION_2_0:
570 case ISP_REVISION_15_0:
575 /* Compute the minimum number of cycles per request, based on the
576 * pipeline maximum data rate. This is an absolute lower bound if we
577 * don't want SBL overflows, so round the value up.
579 cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
581 minimum = DIV_ROUND_UP(cycles_per_request, granularity);
583 /* Compute the maximum number of cycles per request, based on the
584 * requested frame rate. This is a soft upper bound to achieve a frame
585 * rate equal or higher than the requested value, so round the value
588 timeperframe = &pipe->max_timeperframe;
590 requests_per_frame = DIV_ROUND_UP(res->crop.active.width * 2, 256)
591 * res->crop.active.height;
592 cycles_per_frame = div_u64((u64)l3_ick * timeperframe->numerator,
593 timeperframe->denominator);
594 cycles_per_request = cycles_per_frame / requests_per_frame;
596 maximum = cycles_per_request / granularity;
598 value = max(minimum, maximum);
600 dev_dbg(isp->dev, "%s: cycles per request = %u\n", __func__, value);
601 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_SDR_REQ_EXP,
602 ISPSBL_SDR_REQ_RSZ_EXP_MASK,
603 value << ISPSBL_SDR_REQ_RSZ_EXP_SHIFT);
607 * omap3isp_resizer_busy - Checks if ISP resizer is busy.
609 * Returns busy field from ISPRSZ_PCR register.
611 int omap3isp_resizer_busy(struct isp_res_device *res)
613 struct isp_device *isp = to_isp_device(res);
615 return isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR) &
620 * resizer_set_inaddr - Sets the memory address of the input frame.
621 * @addr: 32bit memory address aligned on 32byte boundary.
623 static void resizer_set_inaddr(struct isp_res_device *res, u32 addr)
625 res->addr_base = addr;
627 /* This will handle crop settings in stream off state */
628 if (res->crop_offset)
629 addr += res->crop_offset & ~0x1f;
631 __resizer_set_inaddr(res, addr);
635 * Configures the memory address to which the output frame is written.
636 * @addr: 32bit memory address aligned on 32byte boundary.
637 * Note: For SBL efficiency reasons the address should be on a 256-byte
640 static void resizer_set_outaddr(struct isp_res_device *res, u32 addr)
642 struct isp_device *isp = to_isp_device(res);
645 * Set output address. This needs to be in its own function
646 * because it changes often.
648 isp_reg_writel(isp, addr << ISPRSZ_SDR_OUTADD_ADDR_SHIFT,
649 OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTADD);
653 * resizer_print_status - Prints the values of the resizer module registers.
655 #define RSZ_PRINT_REGISTER(isp, name)\
656 dev_dbg(isp->dev, "###RSZ " #name "=0x%08x\n", \
657 isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_##name))
659 static void resizer_print_status(struct isp_res_device *res)
661 struct isp_device *isp = to_isp_device(res);
663 dev_dbg(isp->dev, "-------------Resizer Register dump----------\n");
665 RSZ_PRINT_REGISTER(isp, PCR);
666 RSZ_PRINT_REGISTER(isp, CNT);
667 RSZ_PRINT_REGISTER(isp, OUT_SIZE);
668 RSZ_PRINT_REGISTER(isp, IN_START);
669 RSZ_PRINT_REGISTER(isp, IN_SIZE);
670 RSZ_PRINT_REGISTER(isp, SDR_INADD);
671 RSZ_PRINT_REGISTER(isp, SDR_INOFF);
672 RSZ_PRINT_REGISTER(isp, SDR_OUTADD);
673 RSZ_PRINT_REGISTER(isp, SDR_OUTOFF);
674 RSZ_PRINT_REGISTER(isp, YENH);
676 dev_dbg(isp->dev, "--------------------------------------------\n");
680 * resizer_calc_ratios - Helper function for calculating resizer ratios
681 * @res: pointer to resizer private data structure
682 * @input: input frame size
683 * @output: output frame size
684 * @ratio : return calculated ratios
687 * The resizer uses a polyphase sample rate converter. The upsampling filter
688 * has a fixed number of phases that depend on the resizing ratio. As the ratio
689 * computation depends on the number of phases, we need to compute a first
690 * approximation and then refine it.
692 * The input/output/ratio relationship is given by the OMAP34xx TRM:
694 * - 8-phase, 4-tap mode (RSZ = 64 ~ 512)
695 * iw = (32 * sph + (ow - 1) * hrsz + 16) >> 8 + 7
696 * ih = (32 * spv + (oh - 1) * vrsz + 16) >> 8 + 4
697 * - 4-phase, 7-tap mode (RSZ = 513 ~ 1024)
698 * iw = (64 * sph + (ow - 1) * hrsz + 32) >> 8 + 7
699 * ih = (64 * spv + (oh - 1) * vrsz + 32) >> 8 + 7
701 * iw and ih are the input width and height after cropping. Those equations need
702 * to be satisfied exactly for the resizer to work correctly.
704 * The equations can't be easily reverted, as the >> 8 operation is not linear.
705 * In addition, not all input sizes can be achieved for a given output size. To
706 * get the highest input size lower than or equal to the requested input size,
707 * we need to compute the highest resizing ratio that satisfies the following
708 * inequality (taking the 4-tap mode width equation as an example)
710 * iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7
712 * (where iw is the requested input width) which can be rewritten as
714 * iw - 7 >= (32 * sph + (ow - 1) * hrsz + 16) >> 8
715 * (iw - 7) << 8 >= 32 * sph + (ow - 1) * hrsz + 16 - b
716 * ((iw - 7) << 8) + b >= 32 * sph + (ow - 1) * hrsz + 16
718 * where b is the value of the 8 least significant bits of the right hand side
719 * expression of the last inequality. The highest resizing ratio value will be
720 * achieved when b is equal to its maximum value of 255. That resizing ratio
721 * value will still satisfy the original inequality, as b will disappear when
722 * the expression will be shifted right by 8.
724 * The reverted equations thus become
726 * - 8-phase, 4-tap mode
727 * hrsz = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / (ow - 1)
728 * vrsz = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / (oh - 1)
729 * - 4-phase, 7-tap mode
730 * hrsz = ((iw - 7) * 256 + 255 - 32 - 64 * sph) / (ow - 1)
731 * vrsz = ((ih - 7) * 256 + 255 - 32 - 64 * spv) / (oh - 1)
733 * The ratios are integer values, and are rounded down to ensure that the
734 * cropped input size is not bigger than the uncropped input size.
736 * As the number of phases/taps, used to select the correct equations to compute
737 * the ratio, depends on the ratio, we start with the 4-tap mode equations to
738 * compute an approximation of the ratio, and switch to the 7-tap mode equations
739 * if the approximation is higher than the ratio threshold.
741 * As the 7-tap mode equations will return a ratio smaller than or equal to the
742 * 4-tap mode equations, the resulting ratio could become lower than or equal to
743 * the ratio threshold. This 'equations loop' isn't an issue as long as the
744 * correct equations are used to compute the final input size. Starting with the
745 * 4-tap mode equations ensure that, in case of values resulting in a 'ratio
746 * loop', the smallest of the ratio values will be used, never exceeding the
747 * requested input size.
749 * We first clamp the output size according to the hardware capability to avoid
750 * auto-cropping the input more than required to satisfy the TRM equations. The
751 * minimum output size is achieved with a scaling factor of 1024. It is thus
752 * computed using the 7-tap equations.
754 * min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1
755 * min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1
757 * Similarly, the maximum output size is achieved with a scaling factor of 64
758 * and computed using the 4-tap equations.
760 * max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1
761 * max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1
763 * The additional +255 term compensates for the round down operation performed
764 * by the TRM equations when shifting the value right by 8 bits.
766 * We then compute and clamp the ratios (x1/4 ~ x4). Clamping the output size to
767 * the maximum value guarantees that the ratio value will never be smaller than
768 * the minimum, but it could still slightly exceed the maximum. Clamping the
769 * ratio will thus result in a resizing factor slightly larger than the
772 * To accommodate that, and make sure the TRM equations are satisfied exactly, we
773 * compute the input crop rectangle as the last step.
775 * As if the situation wasn't complex enough, the maximum output width depends
776 * on the vertical resizing ratio. Fortunately, the output height doesn't
777 * depend on the horizontal resizing ratio. We can then start by computing the
778 * output height and the vertical ratio, and then move to computing the output
779 * width and the horizontal ratio.
781 static void resizer_calc_ratios(struct isp_res_device *res,
782 struct v4l2_rect *input,
783 struct v4l2_mbus_framefmt *output,
784 struct resizer_ratio *ratio)
786 struct isp_device *isp = to_isp_device(res);
787 const unsigned int spv = DEFAULT_PHASE;
788 const unsigned int sph = DEFAULT_PHASE;
789 unsigned int upscaled_width;
790 unsigned int upscaled_height;
791 unsigned int min_width;
792 unsigned int min_height;
793 unsigned int max_width;
794 unsigned int max_height;
795 unsigned int width_alignment;
800 * Clamp the output height based on the hardware capabilities and
801 * compute the vertical resizing ratio.
803 min_height = ((input->height - 7) * 256 - 32 - 64 * spv) / 1024 + 1;
804 min_height = max_t(unsigned int, min_height, MIN_OUT_HEIGHT);
805 max_height = ((input->height - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1;
806 max_height = min_t(unsigned int, max_height, MAX_OUT_HEIGHT);
807 output->height = clamp(output->height, min_height, max_height);
809 ratio->vert = ((input->height - 4) * 256 + 255 - 16 - 32 * spv)
810 / (output->height - 1);
811 if (ratio->vert > MID_RESIZE_VALUE)
812 ratio->vert = ((input->height - 7) * 256 + 255 - 32 - 64 * spv)
813 / (output->height - 1);
814 ratio->vert = clamp_t(unsigned int, ratio->vert,
815 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
817 if (ratio->vert <= MID_RESIZE_VALUE) {
818 upscaled_height = (output->height - 1) * ratio->vert
820 height = (upscaled_height >> 8) + 4;
822 upscaled_height = (output->height - 1) * ratio->vert
824 height = (upscaled_height >> 8) + 7;
828 * Compute the minimum and maximum output widths based on the hardware
829 * capabilities. The maximum depends on the vertical resizing ratio.
831 min_width = ((input->width - 7) * 256 - 32 - 64 * sph) / 1024 + 1;
832 min_width = max_t(unsigned int, min_width, MIN_OUT_WIDTH);
834 if (ratio->vert <= MID_RESIZE_VALUE) {
835 switch (isp->revision) {
836 case ISP_REVISION_1_0:
837 max_width = MAX_4TAP_OUT_WIDTH_ES1;
840 case ISP_REVISION_2_0:
842 max_width = MAX_4TAP_OUT_WIDTH_ES2;
845 case ISP_REVISION_15_0:
846 max_width = MAX_4TAP_OUT_WIDTH_3630;
850 switch (isp->revision) {
851 case ISP_REVISION_1_0:
852 max_width = MAX_7TAP_OUT_WIDTH_ES1;
855 case ISP_REVISION_2_0:
857 max_width = MAX_7TAP_OUT_WIDTH_ES2;
860 case ISP_REVISION_15_0:
861 max_width = MAX_7TAP_OUT_WIDTH_3630;
865 max_width = min(((input->width - 7) * 256 + 255 - 16 - 32 * sph) / 64
869 * The output width must be even, and must be a multiple of 16 bytes
870 * when upscaling vertically. Clamp the output width to the valid range.
871 * Take the alignment into account (the maximum width in 7-tap mode on
872 * ES2 isn't a multiple of 8) and align the result up to make sure it
873 * won't be smaller than the minimum.
875 width_alignment = ratio->vert < 256 ? 8 : 2;
876 output->width = clamp(output->width, min_width,
877 max_width & ~(width_alignment - 1));
878 output->width = ALIGN(output->width, width_alignment);
880 ratio->horz = ((input->width - 7) * 256 + 255 - 16 - 32 * sph)
881 / (output->width - 1);
882 if (ratio->horz > MID_RESIZE_VALUE)
883 ratio->horz = ((input->width - 7) * 256 + 255 - 32 - 64 * sph)
884 / (output->width - 1);
885 ratio->horz = clamp_t(unsigned int, ratio->horz,
886 MIN_RESIZE_VALUE, MAX_RESIZE_VALUE);
888 if (ratio->horz <= MID_RESIZE_VALUE) {
889 upscaled_width = (output->width - 1) * ratio->horz
891 width = (upscaled_width >> 8) + 7;
893 upscaled_width = (output->width - 1) * ratio->horz
895 width = (upscaled_width >> 8) + 7;
898 /* Center the new crop rectangle. */
899 input->left += (input->width - width) / 2;
900 input->top += (input->height - height) / 2;
901 input->width = width;
902 input->height = height;
906 * resizer_set_crop_params - Setup hardware with cropping parameters
907 * @res : resizer private structure
908 * @input : format on sink pad
909 * @output : format on source pad
912 static void resizer_set_crop_params(struct isp_res_device *res,
913 const struct v4l2_mbus_framefmt *input,
914 const struct v4l2_mbus_framefmt *output)
916 resizer_set_ratio(res, &res->ratio);
918 /* Set chrominance horizontal algorithm */
919 if (res->ratio.horz >= RESIZE_DIVISOR)
920 resizer_set_bilinear(res, RSZ_THE_SAME);
922 resizer_set_bilinear(res, RSZ_BILINEAR);
924 resizer_adjust_bandwidth(res);
926 if (res->input == RESIZER_INPUT_MEMORY) {
927 /* Calculate additional offset for crop */
928 res->crop_offset = (res->crop.active.top * input->width +
929 res->crop.active.left) * 2;
931 * Write lowest 4 bits of horizontal pixel offset (in pixels),
932 * vertical start must be 0.
934 resizer_set_start(res, (res->crop_offset / 2) & 0xf, 0);
937 * Set start (read) address for cropping, in bytes.
938 * Lowest 5 bits must be zero.
940 __resizer_set_inaddr(res,
941 res->addr_base + (res->crop_offset & ~0x1f));
944 * Set vertical start line and horizontal starting pixel.
945 * If the input is from CCDC/PREV, horizontal start field is
946 * in bytes (twice number of pixels).
948 resizer_set_start(res, res->crop.active.left * 2,
949 res->crop.active.top);
950 /* Input address and offset must be 0 for preview/ccdc input */
951 __resizer_set_inaddr(res, 0);
952 resizer_set_input_offset(res, 0);
955 /* Set the input size */
956 resizer_set_input_size(res, res->crop.active.width,
957 res->crop.active.height);
960 static void resizer_configure(struct isp_res_device *res)
962 struct v4l2_mbus_framefmt *informat, *outformat;
963 struct resizer_luma_yenh luma = {0, 0, 0, 0};
965 resizer_set_source(res, res->input);
967 informat = &res->formats[RESZ_PAD_SINK];
968 outformat = &res->formats[RESZ_PAD_SOURCE];
971 if (res->input == RESIZER_INPUT_VP)
972 resizer_set_input_offset(res, 0);
974 resizer_set_input_offset(res, ALIGN(informat->width, 0x10) * 2);
976 /* YUV422 interleaved, default phase, no luma enhancement */
977 resizer_set_intype(res, RSZ_YUV422);
978 resizer_set_ycpos(res, informat->code);
979 resizer_set_phase(res, DEFAULT_PHASE, DEFAULT_PHASE);
980 resizer_set_luma(res, &luma);
982 /* RESZ_PAD_SOURCE */
983 resizer_set_output_offset(res, ALIGN(outformat->width * 2, 32));
984 resizer_set_output_size(res, outformat->width, outformat->height);
986 resizer_set_crop_params(res, informat, outformat);
989 /* -----------------------------------------------------------------------------
993 static void resizer_enable_oneshot(struct isp_res_device *res)
995 struct isp_device *isp = to_isp_device(res);
997 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR,
998 ISPRSZ_PCR_ENABLE | ISPRSZ_PCR_ONESHOT);
1001 void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res)
1004 * If ISP_VIDEO_DMAQUEUE_QUEUED is set, DMA queue had an underrun
1005 * condition, the module was paused and now we have a buffer queued
1006 * on the output again. Restart the pipeline if running in continuous
1009 if (res->state == ISP_PIPELINE_STREAM_CONTINUOUS &&
1010 res->video_out.dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1011 resizer_enable_oneshot(res);
1012 isp_video_dmaqueue_flags_clr(&res->video_out);
1016 static void resizer_isr_buffer(struct isp_res_device *res)
1018 struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
1019 struct isp_buffer *buffer;
1022 if (res->state == ISP_PIPELINE_STREAM_STOPPED)
1025 /* Complete the output buffer and, if reading from memory, the input
1028 buffer = omap3isp_video_buffer_next(&res->video_out);
1029 if (buffer != NULL) {
1030 resizer_set_outaddr(res, buffer->dma);
1034 pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1036 if (res->input == RESIZER_INPUT_MEMORY) {
1037 buffer = omap3isp_video_buffer_next(&res->video_in);
1039 resizer_set_inaddr(res, buffer->dma);
1040 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1043 if (res->state == ISP_PIPELINE_STREAM_SINGLESHOT) {
1044 if (isp_pipeline_ready(pipe))
1045 omap3isp_pipeline_set_stream(pipe,
1046 ISP_PIPELINE_STREAM_SINGLESHOT);
1048 /* If an underrun occurs, the video queue operation handler will
1049 * restart the resizer. Otherwise restart it immediately.
1052 resizer_enable_oneshot(res);
1057 * omap3isp_resizer_isr - ISP resizer interrupt handler
1059 * Manage the resizer video buffers and configure shadowed and busy-locked
1062 void omap3isp_resizer_isr(struct isp_res_device *res)
1064 struct v4l2_mbus_framefmt *informat, *outformat;
1065 unsigned long flags;
1067 if (omap3isp_module_sync_is_stopping(&res->wait, &res->stopping))
1070 spin_lock_irqsave(&res->lock, flags);
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 spin_unlock_irqrestore(&res->lock, flags);
1083 resizer_isr_buffer(res);
1086 /* -----------------------------------------------------------------------------
1087 * ISP video operations
1090 static int resizer_video_queue(struct isp_video *video,
1091 struct isp_buffer *buffer)
1093 struct isp_res_device *res = &video->isp->isp_res;
1095 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1096 resizer_set_inaddr(res, buffer->dma);
1099 * We now have a buffer queued on the output. Despite what the
1100 * TRM says, the resizer can't be restarted immediately.
1101 * Enabling it in one shot mode in the middle of a frame (or at
1102 * least asynchronously to the frame) results in the output
1103 * being shifted randomly left/right and up/down, as if the
1104 * hardware didn't synchronize itself to the beginning of the
1107 * Restart the resizer on the next sync interrupt if running in
1108 * continuous mode or when starting the stream.
1110 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1111 resizer_set_outaddr(res, buffer->dma);
1116 static const struct isp_video_operations resizer_video_ops = {
1117 .queue = resizer_video_queue,
1120 /* -----------------------------------------------------------------------------
1121 * V4L2 subdev operations
1125 * resizer_set_stream - Enable/Disable streaming on resizer subdev
1126 * @sd: ISP resizer V4L2 subdev
1127 * @enable: 1 == Enable, 0 == Disable
1129 * The resizer hardware can't be enabled without a memory buffer to write to.
1130 * As the s_stream operation is called in response to a STREAMON call without
1131 * any buffer queued yet, just update the state field and return immediately.
1132 * The resizer will be enabled in resizer_video_queue().
1134 static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
1136 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1137 struct isp_video *video_out = &res->video_out;
1138 struct isp_device *isp = to_isp_device(res);
1139 struct device *dev = to_device(res);
1141 if (res->state == ISP_PIPELINE_STREAM_STOPPED) {
1142 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1145 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1146 resizer_configure(res);
1147 resizer_print_status(res);
1151 case ISP_PIPELINE_STREAM_CONTINUOUS:
1152 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1153 if (video_out->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_QUEUED) {
1154 resizer_enable_oneshot(res);
1155 isp_video_dmaqueue_flags_clr(video_out);
1159 case ISP_PIPELINE_STREAM_SINGLESHOT:
1160 if (res->input == RESIZER_INPUT_MEMORY)
1161 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_READ);
1162 omap3isp_sbl_enable(isp, OMAP3_ISP_SBL_RESIZER_WRITE);
1164 resizer_enable_oneshot(res);
1167 case ISP_PIPELINE_STREAM_STOPPED:
1168 if (omap3isp_module_sync_idle(&sd->entity, &res->wait,
1170 dev_dbg(dev, "%s: module stop timeout.\n", sd->name);
1171 omap3isp_sbl_disable(isp, OMAP3_ISP_SBL_RESIZER_READ |
1172 OMAP3_ISP_SBL_RESIZER_WRITE);
1173 omap3isp_subclk_disable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1174 isp_video_dmaqueue_flags_clr(video_out);
1178 res->state = enable;
1183 * resizer_try_crop - mangles crop parameters.
1185 static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
1186 const struct v4l2_mbus_framefmt *source,
1187 struct v4l2_rect *crop)
1189 const unsigned int spv = DEFAULT_PHASE;
1190 const unsigned int sph = DEFAULT_PHASE;
1192 /* Crop rectangle is constrained by the output size so that zoom ratio
1193 * cannot exceed +/-4.0.
1195 unsigned int min_width =
1196 ((32 * sph + (source->width - 1) * 64 + 16) >> 8) + 7;
1197 unsigned int min_height =
1198 ((32 * spv + (source->height - 1) * 64 + 16) >> 8) + 4;
1199 unsigned int max_width =
1200 ((64 * sph + (source->width - 1) * 1024 + 32) >> 8) + 7;
1201 unsigned int max_height =
1202 ((64 * spv + (source->height - 1) * 1024 + 32) >> 8) + 7;
1204 crop->width = clamp_t(u32, crop->width, min_width, max_width);
1205 crop->height = clamp_t(u32, crop->height, min_height, max_height);
1207 /* Crop can not go beyond of the input rectangle */
1208 crop->left = clamp_t(u32, crop->left, 0, sink->width - MIN_IN_WIDTH);
1209 crop->width = clamp_t(u32, crop->width, MIN_IN_WIDTH,
1210 sink->width - crop->left);
1211 crop->top = clamp_t(u32, crop->top, 0, sink->height - MIN_IN_HEIGHT);
1212 crop->height = clamp_t(u32, crop->height, MIN_IN_HEIGHT,
1213 sink->height - crop->top);
1217 * resizer_get_selection - Retrieve a selection rectangle on a pad
1218 * @sd: ISP resizer V4L2 subdevice
1219 * @fh: V4L2 subdev file handle
1220 * @sel: Selection rectangle
1222 * The only supported rectangles are the crop rectangles on the sink pad.
1224 * Return 0 on success or a negative error code otherwise.
1226 static int resizer_get_selection(struct v4l2_subdev *sd,
1227 struct v4l2_subdev_fh *fh,
1228 struct v4l2_subdev_selection *sel)
1230 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1231 struct v4l2_mbus_framefmt *format_source;
1232 struct v4l2_mbus_framefmt *format_sink;
1233 struct resizer_ratio ratio;
1235 if (sel->pad != RESZ_PAD_SINK)
1238 format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1240 format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1243 switch (sel->target) {
1244 case V4L2_SEL_TGT_CROP_BOUNDS:
1247 sel->r.width = INT_MAX;
1248 sel->r.height = INT_MAX;
1250 resizer_try_crop(format_sink, format_source, &sel->r);
1251 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1254 case V4L2_SEL_TGT_CROP:
1255 sel->r = *__resizer_get_crop(res, fh, sel->which);
1256 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1267 * resizer_set_selection - Set a selection rectangle on a pad
1268 * @sd: ISP resizer V4L2 subdevice
1269 * @fh: V4L2 subdev file handle
1270 * @sel: Selection rectangle
1272 * The only supported rectangle is the actual crop rectangle on the sink pad.
1274 * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag
1277 * Return 0 on success or a negative error code otherwise.
1279 static int resizer_set_selection(struct v4l2_subdev *sd,
1280 struct v4l2_subdev_fh *fh,
1281 struct v4l2_subdev_selection *sel)
1283 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1284 struct isp_device *isp = to_isp_device(res);
1285 const struct v4l2_mbus_framefmt *format_sink;
1286 struct v4l2_mbus_framefmt format_source;
1287 struct resizer_ratio ratio;
1288 unsigned long flags;
1290 if (sel->target != V4L2_SEL_TGT_CROP ||
1291 sel->pad != RESZ_PAD_SINK)
1294 format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1296 format_source = *__resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1299 dev_dbg(isp->dev, "%s(%s): req %ux%u -> (%d,%d)/%ux%u -> %ux%u\n",
1300 __func__, sel->which == V4L2_SUBDEV_FORMAT_TRY ? "try" : "act",
1301 format_sink->width, format_sink->height,
1302 sel->r.left, sel->r.top, sel->r.width, sel->r.height,
1303 format_source.width, format_source.height);
1305 /* Clamp the crop rectangle to the bounds, and then mangle it further to
1306 * fulfill the TRM equations. Store the clamped but otherwise unmangled
1307 * rectangle to avoid cropping the input multiple times: when an
1308 * application sets the output format, the current crop rectangle is
1309 * mangled during crop rectangle computation, which would lead to a new,
1310 * smaller input crop rectangle every time the output size is set if we
1311 * stored the mangled rectangle.
1313 resizer_try_crop(format_sink, &format_source, &sel->r);
1314 *__resizer_get_crop(res, fh, sel->which) = sel->r;
1315 resizer_calc_ratios(res, &sel->r, &format_source, &ratio);
1317 dev_dbg(isp->dev, "%s(%s): got %ux%u -> (%d,%d)/%ux%u -> %ux%u\n",
1318 __func__, sel->which == V4L2_SUBDEV_FORMAT_TRY ? "try" : "act",
1319 format_sink->width, format_sink->height,
1320 sel->r.left, sel->r.top, sel->r.width, sel->r.height,
1321 format_source.width, format_source.height);
1323 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1324 *__resizer_get_format(res, fh, RESZ_PAD_SOURCE, sel->which) =
1329 /* Update the source format, resizing ratios and crop rectangle. If
1330 * streaming is on the IRQ handler will reprogram the resizer after the
1331 * current frame. We thus we need to protect against race conditions.
1333 spin_lock_irqsave(&res->lock, flags);
1335 *__resizer_get_format(res, fh, RESZ_PAD_SOURCE, sel->which) =
1339 res->crop.active = sel->r;
1341 if (res->state != ISP_PIPELINE_STREAM_STOPPED)
1344 spin_unlock_irqrestore(&res->lock, flags);
1349 /* resizer pixel formats */
1350 static const unsigned int resizer_formats[] = {
1351 V4L2_MBUS_FMT_UYVY8_1X16,
1352 V4L2_MBUS_FMT_YUYV8_1X16,
1355 static unsigned int resizer_max_in_width(struct isp_res_device *res)
1357 struct isp_device *isp = to_isp_device(res);
1359 if (res->input == RESIZER_INPUT_MEMORY) {
1360 return MAX_IN_WIDTH_MEMORY_MODE;
1362 if (isp->revision == ISP_REVISION_1_0)
1363 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
1365 return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
1370 * resizer_try_format - Handle try format by pad subdev method
1371 * @res : ISP resizer device
1372 * @fh : V4L2 subdev file handle
1374 * @fmt : pointer to v4l2 format structure
1375 * @which : wanted subdev format
1377 static void resizer_try_format(struct isp_res_device *res,
1378 struct v4l2_subdev_fh *fh, unsigned int pad,
1379 struct v4l2_mbus_framefmt *fmt,
1380 enum v4l2_subdev_format_whence which)
1382 struct v4l2_mbus_framefmt *format;
1383 struct resizer_ratio ratio;
1384 struct v4l2_rect crop;
1388 if (fmt->code != V4L2_MBUS_FMT_YUYV8_1X16 &&
1389 fmt->code != V4L2_MBUS_FMT_UYVY8_1X16)
1390 fmt->code = V4L2_MBUS_FMT_YUYV8_1X16;
1392 fmt->width = clamp_t(u32, fmt->width, MIN_IN_WIDTH,
1393 resizer_max_in_width(res));
1394 fmt->height = clamp_t(u32, fmt->height, MIN_IN_HEIGHT,
1398 case RESZ_PAD_SOURCE:
1399 format = __resizer_get_format(res, fh, RESZ_PAD_SINK, which);
1400 fmt->code = format->code;
1402 crop = *__resizer_get_crop(res, fh, which);
1403 resizer_calc_ratios(res, &crop, fmt, &ratio);
1407 fmt->colorspace = V4L2_COLORSPACE_JPEG;
1408 fmt->field = V4L2_FIELD_NONE;
1412 * resizer_enum_mbus_code - Handle pixel format enumeration
1413 * @sd : pointer to v4l2 subdev structure
1414 * @fh : V4L2 subdev file handle
1415 * @code : pointer to v4l2_subdev_mbus_code_enum structure
1416 * return -EINVAL or zero on success
1418 static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
1419 struct v4l2_subdev_fh *fh,
1420 struct v4l2_subdev_mbus_code_enum *code)
1422 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1423 struct v4l2_mbus_framefmt *format;
1425 if (code->pad == RESZ_PAD_SINK) {
1426 if (code->index >= ARRAY_SIZE(resizer_formats))
1429 code->code = resizer_formats[code->index];
1431 if (code->index != 0)
1434 format = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1435 V4L2_SUBDEV_FORMAT_TRY);
1436 code->code = format->code;
1442 static int resizer_enum_frame_size(struct v4l2_subdev *sd,
1443 struct v4l2_subdev_fh *fh,
1444 struct v4l2_subdev_frame_size_enum *fse)
1446 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1447 struct v4l2_mbus_framefmt format;
1449 if (fse->index != 0)
1452 format.code = fse->code;
1455 resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1456 fse->min_width = format.width;
1457 fse->min_height = format.height;
1459 if (format.code != fse->code)
1462 format.code = fse->code;
1465 resizer_try_format(res, fh, fse->pad, &format, V4L2_SUBDEV_FORMAT_TRY);
1466 fse->max_width = format.width;
1467 fse->max_height = format.height;
1473 * resizer_get_format - Handle get format by pads subdev method
1474 * @sd : pointer to v4l2 subdev structure
1475 * @fh : V4L2 subdev file handle
1476 * @fmt : pointer to v4l2 subdev format structure
1477 * return -EINVAL or zero on success
1479 static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1480 struct v4l2_subdev_format *fmt)
1482 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1483 struct v4l2_mbus_framefmt *format;
1485 format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1489 fmt->format = *format;
1494 * resizer_set_format - Handle set format by pads subdev method
1495 * @sd : pointer to v4l2 subdev structure
1496 * @fh : V4L2 subdev file handle
1497 * @fmt : pointer to v4l2 subdev format structure
1498 * return -EINVAL or zero on success
1500 static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1501 struct v4l2_subdev_format *fmt)
1503 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1504 struct v4l2_mbus_framefmt *format;
1505 struct v4l2_rect *crop;
1507 format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1511 resizer_try_format(res, fh, fmt->pad, &fmt->format, fmt->which);
1512 *format = fmt->format;
1514 if (fmt->pad == RESZ_PAD_SINK) {
1515 /* reset crop rectangle */
1516 crop = __resizer_get_crop(res, fh, fmt->which);
1519 crop->width = fmt->format.width;
1520 crop->height = fmt->format.height;
1522 /* Propagate the format from sink to source */
1523 format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1525 *format = fmt->format;
1526 resizer_try_format(res, fh, RESZ_PAD_SOURCE, format,
1530 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1531 /* Compute and store the active crop rectangle and resizer
1532 * ratios. format already points to the source pad active
1535 res->crop.active = res->crop.request;
1536 resizer_calc_ratios(res, &res->crop.active, format,
1543 static int resizer_link_validate(struct v4l2_subdev *sd,
1544 struct media_link *link,
1545 struct v4l2_subdev_format *source_fmt,
1546 struct v4l2_subdev_format *sink_fmt)
1548 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1549 struct isp_pipeline *pipe = to_isp_pipeline(&sd->entity);
1551 omap3isp_resizer_max_rate(res, &pipe->max_rate);
1553 return v4l2_subdev_link_validate_default(sd, link,
1554 source_fmt, sink_fmt);
1558 * resizer_init_formats - Initialize formats on all pads
1559 * @sd: ISP resizer V4L2 subdevice
1560 * @fh: V4L2 subdev file handle
1562 * Initialize all pad formats with default values. If fh is not NULL, try
1563 * formats are initialized on the file handle. Otherwise active formats are
1564 * initialized on the device.
1566 static int resizer_init_formats(struct v4l2_subdev *sd,
1567 struct v4l2_subdev_fh *fh)
1569 struct v4l2_subdev_format format;
1571 memset(&format, 0, sizeof(format));
1572 format.pad = RESZ_PAD_SINK;
1573 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1574 format.format.code = V4L2_MBUS_FMT_YUYV8_1X16;
1575 format.format.width = 4096;
1576 format.format.height = 4096;
1577 resizer_set_format(sd, fh, &format);
1582 /* subdev video operations */
1583 static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
1584 .s_stream = resizer_set_stream,
1587 /* subdev pad operations */
1588 static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
1589 .enum_mbus_code = resizer_enum_mbus_code,
1590 .enum_frame_size = resizer_enum_frame_size,
1591 .get_fmt = resizer_get_format,
1592 .set_fmt = resizer_set_format,
1593 .get_selection = resizer_get_selection,
1594 .set_selection = resizer_set_selection,
1595 .link_validate = resizer_link_validate,
1598 /* subdev operations */
1599 static const struct v4l2_subdev_ops resizer_v4l2_ops = {
1600 .video = &resizer_v4l2_video_ops,
1601 .pad = &resizer_v4l2_pad_ops,
1604 /* subdev internal operations */
1605 static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
1606 .open = resizer_init_formats,
1609 /* -----------------------------------------------------------------------------
1610 * Media entity operations
1614 * resizer_link_setup - Setup resizer connections.
1615 * @entity : Pointer to media entity structure
1616 * @local : Pointer to local pad array
1617 * @remote : Pointer to remote pad array
1618 * @flags : Link flags
1619 * return -EINVAL or zero on success
1621 static int resizer_link_setup(struct media_entity *entity,
1622 const struct media_pad *local,
1623 const struct media_pad *remote, u32 flags)
1625 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1626 struct isp_res_device *res = v4l2_get_subdevdata(sd);
1628 switch (local->index | media_entity_type(remote->entity)) {
1629 case RESZ_PAD_SINK | MEDIA_ENT_T_DEVNODE:
1630 /* read from memory */
1631 if (flags & MEDIA_LNK_FL_ENABLED) {
1632 if (res->input == RESIZER_INPUT_VP)
1634 res->input = RESIZER_INPUT_MEMORY;
1636 if (res->input == RESIZER_INPUT_MEMORY)
1637 res->input = RESIZER_INPUT_NONE;
1641 case RESZ_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
1642 /* read from ccdc or previewer */
1643 if (flags & MEDIA_LNK_FL_ENABLED) {
1644 if (res->input == RESIZER_INPUT_MEMORY)
1646 res->input = RESIZER_INPUT_VP;
1648 if (res->input == RESIZER_INPUT_VP)
1649 res->input = RESIZER_INPUT_NONE;
1653 case RESZ_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
1654 /* resizer always write to memory */
1664 /* media operations */
1665 static const struct media_entity_operations resizer_media_ops = {
1666 .link_setup = resizer_link_setup,
1667 .link_validate = v4l2_subdev_link_validate,
1670 void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
1672 v4l2_device_unregister_subdev(&res->subdev);
1673 omap3isp_video_unregister(&res->video_in);
1674 omap3isp_video_unregister(&res->video_out);
1677 int omap3isp_resizer_register_entities(struct isp_res_device *res,
1678 struct v4l2_device *vdev)
1682 /* Register the subdev and video nodes. */
1683 ret = v4l2_device_register_subdev(vdev, &res->subdev);
1687 ret = omap3isp_video_register(&res->video_in, vdev);
1691 ret = omap3isp_video_register(&res->video_out, vdev);
1698 omap3isp_resizer_unregister_entities(res);
1702 /* -----------------------------------------------------------------------------
1703 * ISP resizer initialization and cleanup
1707 * resizer_init_entities - Initialize resizer subdev and media entity.
1708 * @res : Pointer to resizer device structure
1709 * return -ENOMEM or zero on success
1711 static int resizer_init_entities(struct isp_res_device *res)
1713 struct v4l2_subdev *sd = &res->subdev;
1714 struct media_pad *pads = res->pads;
1715 struct media_entity *me = &sd->entity;
1718 res->input = RESIZER_INPUT_NONE;
1720 v4l2_subdev_init(sd, &resizer_v4l2_ops);
1721 sd->internal_ops = &resizer_v4l2_internal_ops;
1722 strlcpy(sd->name, "OMAP3 ISP resizer", sizeof(sd->name));
1723 sd->grp_id = 1 << 16; /* group ID for isp subdevs */
1724 v4l2_set_subdevdata(sd, res);
1725 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1727 pads[RESZ_PAD_SINK].flags = MEDIA_PAD_FL_SINK
1728 | MEDIA_PAD_FL_MUST_CONNECT;
1729 pads[RESZ_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1731 me->ops = &resizer_media_ops;
1732 ret = media_entity_init(me, RESZ_PADS_NUM, pads, 0);
1736 resizer_init_formats(sd, NULL);
1738 res->video_in.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1739 res->video_in.ops = &resizer_video_ops;
1740 res->video_in.isp = to_isp_device(res);
1741 res->video_in.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1742 res->video_in.bpl_alignment = 32;
1743 res->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1744 res->video_out.ops = &resizer_video_ops;
1745 res->video_out.isp = to_isp_device(res);
1746 res->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 2 * 3;
1747 res->video_out.bpl_alignment = 32;
1749 ret = omap3isp_video_init(&res->video_in, "resizer");
1751 goto error_video_in;
1753 ret = omap3isp_video_init(&res->video_out, "resizer");
1755 goto error_video_out;
1757 res->video_out.video.entity.flags |= MEDIA_ENT_FL_DEFAULT;
1759 /* Connect the video nodes to the resizer subdev. */
1760 ret = media_entity_create_link(&res->video_in.video.entity, 0,
1761 &res->subdev.entity, RESZ_PAD_SINK, 0);
1765 ret = media_entity_create_link(&res->subdev.entity, RESZ_PAD_SOURCE,
1766 &res->video_out.video.entity, 0, 0);
1773 omap3isp_video_cleanup(&res->video_out);
1775 omap3isp_video_cleanup(&res->video_in);
1777 media_entity_cleanup(&res->subdev.entity);
1782 * isp_resizer_init - Resizer initialization.
1783 * @isp : Pointer to ISP device
1784 * return -ENOMEM or zero on success
1786 int omap3isp_resizer_init(struct isp_device *isp)
1788 struct isp_res_device *res = &isp->isp_res;
1790 init_waitqueue_head(&res->wait);
1791 atomic_set(&res->stopping, 0);
1792 spin_lock_init(&res->lock);
1794 return resizer_init_entities(res);
1797 void omap3isp_resizer_cleanup(struct isp_device *isp)
1799 struct isp_res_device *res = &isp->isp_res;
1801 omap3isp_video_cleanup(&res->video_in);
1802 omap3isp_video_cleanup(&res->video_out);
1803 media_entity_cleanup(&res->subdev.entity);