// SPDX-License-Identifier: GPL-2.0-only /* * ispresizer.c * * TI OMAP3 ISP - Resizer module * * Copyright (C) 2010 Nokia Corporation * Copyright (C) 2009 Texas Instruments, Inc * * Contacts: Laurent Pinchart <[email protected]> * Sakari Ailus <[email protected]> */ #include <linux/device.h> #include <linux/mm.h> #include <linux/module.h> #include "isp.h" #include "ispreg.h" #include "ispresizer.h" /* * Resizer Constants */ #define MIN_RESIZE_VALUE … #define MID_RESIZE_VALUE … #define MAX_RESIZE_VALUE … #define MIN_IN_WIDTH … #define MIN_IN_HEIGHT … #define MAX_IN_WIDTH_MEMORY_MODE … #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES1 … #define MAX_IN_WIDTH_ONTHEFLY_MODE_ES2 … #define MAX_IN_HEIGHT … #define MIN_OUT_WIDTH … #define MIN_OUT_HEIGHT … #define MAX_OUT_HEIGHT … /* * Resizer Use Constraints * "TRM ES3.1, table 12-46" */ #define MAX_4TAP_OUT_WIDTH_ES1 … #define MAX_7TAP_OUT_WIDTH_ES1 … #define MAX_4TAP_OUT_WIDTH_ES2 … #define MAX_7TAP_OUT_WIDTH_ES2 … #define MAX_4TAP_OUT_WIDTH_3630 … #define MAX_7TAP_OUT_WIDTH_3630 … /* * Constants for ratio calculation */ #define RESIZE_DIVISOR … #define DEFAULT_PHASE … /* * Default (and only) configuration of filter coefficients. * 7-tap mode is for scale factors 0.25x to 0.5x. * 4-tap mode is for scale factors 0.5x to 4.0x. * There shouldn't be any reason to recalculate these, EVER. */ static const struct isprsz_coef filter_coefs = …; /* * __resizer_get_format - helper function for getting resizer format * @res : pointer to resizer private structure * @pad : pad number * @sd_state: V4L2 subdev state * @which : wanted subdev format * return zero */ static struct v4l2_mbus_framefmt * __resizer_get_format(struct isp_res_device *res, struct v4l2_subdev_state *sd_state, unsigned int pad, enum v4l2_subdev_format_whence which) { … } /* * __resizer_get_crop - helper function for getting resizer crop rectangle * @res : pointer to resizer private structure * @sd_state: V4L2 subdev state * @which : wanted subdev crop rectangle */ static struct v4l2_rect * __resizer_get_crop(struct isp_res_device *res, struct v4l2_subdev_state *sd_state, enum v4l2_subdev_format_whence which) { … } /* * resizer_set_filters - Set resizer filters * @res: Device context. * @h_coeff: horizontal coefficient * @v_coeff: vertical coefficient * Return none */ static void resizer_set_filters(struct isp_res_device *res, const u16 *h_coeff, const u16 *v_coeff) { … } /* * resizer_set_bilinear - Chrominance horizontal algorithm select * @res: Device context. * @type: Filtering interpolation type. * * Filtering that is same as luminance processing is * intended only for downsampling, and bilinear interpolation * is intended only for upsampling. */ static void resizer_set_bilinear(struct isp_res_device *res, enum resizer_chroma_algo type) { … } /* * resizer_set_ycpos - Luminance and chrominance order * @res: Device context. * @pixelcode: pixel code. */ static void resizer_set_ycpos(struct isp_res_device *res, u32 pixelcode) { … } /* * resizer_set_phase - Setup horizontal and vertical starting phase * @res: Device context. * @h_phase: horizontal phase parameters. * @v_phase: vertical phase parameters. * * Horizontal and vertical phase range is 0 to 7 */ static void resizer_set_phase(struct isp_res_device *res, u32 h_phase, u32 v_phase) { … } /* * resizer_set_luma - Setup luminance enhancer parameters * @res: Device context. * @luma: Structure for luminance enhancer parameters. * * Algorithm select: * 0x0: Disable * 0x1: [-1 2 -1]/2 high-pass filter * 0x2: [-1 -2 6 -2 -1]/4 high-pass filter * * Maximum gain: * The data is coded in U4Q4 representation. * * Slope: * The data is coded in U4Q4 representation. * * Coring offset: * The data is coded in U8Q0 representation. * * The new luminance value is computed as: * Y += HPF(Y) x max(GAIN, (HPF(Y) - CORE) x SLOP + 8) >> 4. */ static void resizer_set_luma(struct isp_res_device *res, struct resizer_luma_yenh *luma) { … } /* * resizer_set_source - Input source select * @res: Device context. * @source: Input source type * * If this field is set to RESIZER_INPUT_VP, the resizer input is fed from * Preview/CCDC engine, otherwise from memory. */ static void resizer_set_source(struct isp_res_device *res, enum resizer_input_entity source) { … } /* * resizer_set_ratio - Setup horizontal and vertical resizing value * @res: Device context. * @ratio: Structure for ratio parameters. * * Resizing range from 64 to 1024 */ static void resizer_set_ratio(struct isp_res_device *res, const struct resizer_ratio *ratio) { … } /* * resizer_set_dst_size - Setup the output height and width * @res: Device context. * @width: Output width. * @height: Output height. * * Width : * The value must be EVEN. * * Height: * The number of bytes written to SDRAM must be * a multiple of 16-bytes if the vertical resizing factor * is greater than 1x (upsizing) */ static void resizer_set_output_size(struct isp_res_device *res, u32 width, u32 height) { … } /* * resizer_set_output_offset - Setup memory offset for the output lines. * @res: Device context. * @offset: Memory offset. * * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte * boundary; the 5 LSBs are read-only. For optimal use of SDRAM bandwidth, * the SDRAM line offset must be set on a 256-byte boundary */ static void resizer_set_output_offset(struct isp_res_device *res, u32 offset) { … } /* * resizer_set_start - Setup vertical and horizontal start position * @res: Device context. * @left: Horizontal start position. * @top: Vertical start position. * * Vertical start line: * This field makes sense only when the resizer obtains its input * from the preview engine/CCDC * * Horizontal start pixel: * Pixels are coded on 16 bits for YUV and 8 bits for color separate data. * When the resizer gets its input from SDRAM, this field must be set * to <= 15 for YUV 16-bit data and <= 31 for 8-bit color separate data */ static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top) { … } /* * resizer_set_input_size - Setup the input size * @res: Device context. * @width: The range is 0 to 4095 pixels * @height: The range is 0 to 4095 lines */ static void resizer_set_input_size(struct isp_res_device *res, u32 width, u32 height) { … } /* * resizer_set_src_offs - Setup the memory offset for the input lines * @res: Device context. * @offset: Memory offset. * * The 5 LSBs are forced to be zeros by the hardware to align on a 32-byte * boundary; the 5 LSBs are read-only. This field must be programmed to be * 0x0 if the resizer input is from preview engine/CCDC. */ static void resizer_set_input_offset(struct isp_res_device *res, u32 offset) { … } /* * resizer_set_intype - Input type select * @res: Device context. * @type: Pixel format type. */ static void resizer_set_intype(struct isp_res_device *res, enum resizer_colors_type type) { … } /* * __resizer_set_inaddr - Helper function for set input address * @res : pointer to resizer private data structure * @addr: input address * return none */ static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr) { … } /* * The data rate at the horizontal resizer output must not exceed half the * functional clock or 100 MP/s, whichever is lower. According to the TRM * there's no similar requirement for the vertical resizer output. However * experience showed that vertical upscaling by 4 leads to SBL overflows (with * data rates at the resizer output exceeding 300 MP/s). Limiting the resizer * output data rate to the functional clock or 200 MP/s, whichever is lower, * seems to get rid of SBL overflows. * * The maximum data rate at the output of the horizontal resizer can thus be * computed with * * max intermediate rate <= L3 clock * input height / output height * max intermediate rate <= L3 clock / 2 * * The maximum data rate at the resizer input is then * * max input rate <= max intermediate rate * input width / output width * * where the input width and height are the resizer input crop rectangle size. * The TRM doesn't clearly explain if that's a maximum instant data rate or a * maximum average data rate. */ void omap3isp_resizer_max_rate(struct isp_res_device *res, unsigned int *max_rate) { … } /* * When the resizer processes images from memory, the driver must slow down read * requests on the input to at least comply with the internal data rate * requirements. If the application real-time requirements can cope with slower * processing, the resizer can be slowed down even more to put less pressure on * the overall system. * * When the resizer processes images on the fly (either from the CCDC or the * preview module), the same data rate requirements apply but they can't be * enforced at the resizer level. The image input module (sensor, CCP2 or * preview module) must not provide image data faster than the resizer can * process. * * For live image pipelines, the data rate is set by the frame format, size and * rate. The sensor output frame rate must not exceed the maximum resizer data * rate. * * The resizer slows down read requests by inserting wait cycles in the SBL * requests. The maximum number of 256-byte requests per second can be computed * as (the data rate is multiplied by 2 to convert from pixels per second to * bytes per second) * * request per second = data rate * 2 / 256 * cycles per request = cycles per second / requests per second * * The number of cycles per second is controlled by the L3 clock, leading to * * cycles per request = L3 frequency / 2 * 256 / data rate */ static void resizer_adjust_bandwidth(struct isp_res_device *res) { … } /* * omap3isp_resizer_busy - Checks if ISP resizer is busy. * * Returns busy field from ISPRSZ_PCR register. */ int omap3isp_resizer_busy(struct isp_res_device *res) { … } /* * resizer_set_inaddr - Sets the memory address of the input frame. * @addr: 32bit memory address aligned on 32byte boundary. */ static void resizer_set_inaddr(struct isp_res_device *res, u32 addr) { … } /* * Configures the memory address to which the output frame is written. * @addr: 32bit memory address aligned on 32byte boundary. * Note: For SBL efficiency reasons the address should be on a 256-byte * boundary. */ static void resizer_set_outaddr(struct isp_res_device *res, u32 addr) { … } /* * resizer_print_status - Prints the values of the resizer module registers. */ #define RSZ_PRINT_REGISTER(isp, name) … static void resizer_print_status(struct isp_res_device *res) { … } /* * resizer_calc_ratios - Helper function for calculating resizer ratios * @res: pointer to resizer private data structure * @input: input frame size * @output: output frame size * @ratio : return calculated ratios * return none * * The resizer uses a polyphase sample rate converter. The upsampling filter * has a fixed number of phases that depend on the resizing ratio. As the ratio * computation depends on the number of phases, we need to compute a first * approximation and then refine it. * * The input/output/ratio relationship is given by the OMAP34xx TRM: * * - 8-phase, 4-tap mode (RSZ = 64 ~ 512) * iw = (32 * sph + (ow - 1) * hrsz + 16) >> 8 + 7 * ih = (32 * spv + (oh - 1) * vrsz + 16) >> 8 + 4 * - 4-phase, 7-tap mode (RSZ = 513 ~ 1024) * iw = (64 * sph + (ow - 1) * hrsz + 32) >> 8 + 7 * ih = (64 * spv + (oh - 1) * vrsz + 32) >> 8 + 7 * * iw and ih are the input width and height after cropping. Those equations need * to be satisfied exactly for the resizer to work correctly. * * The equations can't be easily reverted, as the >> 8 operation is not linear. * In addition, not all input sizes can be achieved for a given output size. To * get the highest input size lower than or equal to the requested input size, * we need to compute the highest resizing ratio that satisfies the following * inequality (taking the 4-tap mode width equation as an example) * * iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7 * * (where iw is the requested input width) which can be rewritten as * * iw - 7 >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 * (iw - 7) << 8 >= 32 * sph + (ow - 1) * hrsz + 16 - b * ((iw - 7) << 8) + b >= 32 * sph + (ow - 1) * hrsz + 16 * * where b is the value of the 8 least significant bits of the right hand side * expression of the last inequality. The highest resizing ratio value will be * achieved when b is equal to its maximum value of 255. That resizing ratio * value will still satisfy the original inequality, as b will disappear when * the expression will be shifted right by 8. * * The reverted equations thus become * * - 8-phase, 4-tap mode * hrsz = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / (ow - 1) * vrsz = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / (oh - 1) * - 4-phase, 7-tap mode * hrsz = ((iw - 7) * 256 + 255 - 32 - 64 * sph) / (ow - 1) * vrsz = ((ih - 7) * 256 + 255 - 32 - 64 * spv) / (oh - 1) * * The ratios are integer values, and are rounded down to ensure that the * cropped input size is not bigger than the uncropped input size. * * As the number of phases/taps, used to select the correct equations to compute * the ratio, depends on the ratio, we start with the 4-tap mode equations to * compute an approximation of the ratio, and switch to the 7-tap mode equations * if the approximation is higher than the ratio threshold. * * As the 7-tap mode equations will return a ratio smaller than or equal to the * 4-tap mode equations, the resulting ratio could become lower than or equal to * the ratio threshold. This 'equations loop' isn't an issue as long as the * correct equations are used to compute the final input size. Starting with the * 4-tap mode equations ensure that, in case of values resulting in a 'ratio * loop', the smallest of the ratio values will be used, never exceeding the * requested input size. * * We first clamp the output size according to the hardware capability to avoid * auto-cropping the input more than required to satisfy the TRM equations. The * minimum output size is achieved with a scaling factor of 1024. It is thus * computed using the 7-tap equations. * * min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1 * min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1 * * Similarly, the maximum output size is achieved with a scaling factor of 64 * and computed using the 4-tap equations. * * max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1 * max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1 * * The additional +255 term compensates for the round down operation performed * by the TRM equations when shifting the value right by 8 bits. * * We then compute and clamp the ratios (x1/4 ~ x4). Clamping the output size to * the maximum value guarantees that the ratio value will never be smaller than * the minimum, but it could still slightly exceed the maximum. Clamping the * ratio will thus result in a resizing factor slightly larger than the * requested value. * * To accommodate that, and make sure the TRM equations are satisfied exactly, we * compute the input crop rectangle as the last step. * * As if the situation wasn't complex enough, the maximum output width depends * on the vertical resizing ratio. Fortunately, the output height doesn't * depend on the horizontal resizing ratio. We can then start by computing the * output height and the vertical ratio, and then move to computing the output * width and the horizontal ratio. */ static void resizer_calc_ratios(struct isp_res_device *res, struct v4l2_rect *input, struct v4l2_mbus_framefmt *output, struct resizer_ratio *ratio) { … } /* * resizer_set_crop_params - Setup hardware with cropping parameters * @res : resizer private structure * @input : format on sink pad * @output : format on source pad * return none */ static void resizer_set_crop_params(struct isp_res_device *res, const struct v4l2_mbus_framefmt *input, const struct v4l2_mbus_framefmt *output) { … } static void resizer_configure(struct isp_res_device *res) { … } /* ----------------------------------------------------------------------------- * Interrupt handling */ static void resizer_enable_oneshot(struct isp_res_device *res) { … } void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res) { … } static void resizer_isr_buffer(struct isp_res_device *res) { … } /* * omap3isp_resizer_isr - ISP resizer interrupt handler * * Manage the resizer video buffers and configure shadowed and busy-locked * registers. */ void omap3isp_resizer_isr(struct isp_res_device *res) { … } /* ----------------------------------------------------------------------------- * ISP video operations */ static int resizer_video_queue(struct isp_video *video, struct isp_buffer *buffer) { … } static const struct isp_video_operations resizer_video_ops = …; /* ----------------------------------------------------------------------------- * V4L2 subdev operations */ /* * resizer_set_stream - Enable/Disable streaming on resizer subdev * @sd: ISP resizer V4L2 subdev * @enable: 1 == Enable, 0 == Disable * * The resizer hardware can't be enabled without a memory buffer to write to. * As the s_stream operation is called in response to a STREAMON call without * any buffer queued yet, just update the state field and return immediately. * The resizer will be enabled in resizer_video_queue(). */ static int resizer_set_stream(struct v4l2_subdev *sd, int enable) { … } /* * resizer_try_crop - mangles crop parameters. */ static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink, const struct v4l2_mbus_framefmt *source, struct v4l2_rect *crop) { … } /* * resizer_get_selection - Retrieve a selection rectangle on a pad * @sd: ISP resizer V4L2 subdevice * @sd_state: V4L2 subdev state * @sel: Selection rectangle * * The only supported rectangles are the crop rectangles on the sink pad. * * Return 0 on success or a negative error code otherwise. */ static int resizer_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_selection *sel) { … } /* * resizer_set_selection - Set a selection rectangle on a pad * @sd: ISP resizer V4L2 subdevice * @sd_state: V4L2 subdev state * @sel: Selection rectangle * * The only supported rectangle is the actual crop rectangle on the sink pad. * * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag * was always set. * * Return 0 on success or a negative error code otherwise. */ static int resizer_set_selection(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_selection *sel) { … } /* resizer pixel formats */ static const unsigned int resizer_formats[] = …; static unsigned int resizer_max_in_width(struct isp_res_device *res) { … } /* * resizer_try_format - Handle try format by pad subdev method * @res : ISP resizer device * @sd_state: V4L2 subdev state * @pad : pad num * @fmt : pointer to v4l2 format structure * @which : wanted subdev format */ static void resizer_try_format(struct isp_res_device *res, struct v4l2_subdev_state *sd_state, unsigned int pad, struct v4l2_mbus_framefmt *fmt, enum v4l2_subdev_format_whence which) { … } /* * resizer_enum_mbus_code - Handle pixel format enumeration * @sd : pointer to v4l2 subdev structure * @sd_state: V4L2 subdev state * @code : pointer to v4l2_subdev_mbus_code_enum structure * return -EINVAL or zero on success */ static int resizer_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_mbus_code_enum *code) { … } static int resizer_enum_frame_size(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_frame_size_enum *fse) { … } /* * resizer_get_format - Handle get format by pads subdev method * @sd : pointer to v4l2 subdev structure * @sd_state: V4L2 subdev state * @fmt : pointer to v4l2 subdev format structure * return -EINVAL or zero on success */ static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_format *fmt) { … } /* * resizer_set_format - Handle set format by pads subdev method * @sd : pointer to v4l2 subdev structure * @sd_state: V4L2 subdev state * @fmt : pointer to v4l2 subdev format structure * return -EINVAL or zero on success */ static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_format *fmt) { … } static int resizer_link_validate(struct v4l2_subdev *sd, struct media_link *link, struct v4l2_subdev_format *source_fmt, struct v4l2_subdev_format *sink_fmt) { … } /* * resizer_init_formats - Initialize formats on all pads * @sd: ISP resizer V4L2 subdevice * @fh: V4L2 subdev file handle * * Initialize all pad formats with default values. If fh is not NULL, try * formats are initialized on the file handle. Otherwise active formats are * initialized on the device. */ static int resizer_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) { … } /* subdev video operations */ static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = …; /* subdev pad operations */ static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = …; /* subdev operations */ static const struct v4l2_subdev_ops resizer_v4l2_ops = …; /* subdev internal operations */ static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = …; /* ----------------------------------------------------------------------------- * Media entity operations */ /* * resizer_link_setup - Setup resizer connections. * @entity : Pointer to media entity structure * @local : Pointer to local pad array * @remote : Pointer to remote pad array * @flags : Link flags * return -EINVAL or zero on success */ static int resizer_link_setup(struct media_entity *entity, const struct media_pad *local, const struct media_pad *remote, u32 flags) { … } /* media operations */ static const struct media_entity_operations resizer_media_ops = …; void omap3isp_resizer_unregister_entities(struct isp_res_device *res) { … } int omap3isp_resizer_register_entities(struct isp_res_device *res, struct v4l2_device *vdev) { … } /* ----------------------------------------------------------------------------- * ISP resizer initialization and cleanup */ /* * resizer_init_entities - Initialize resizer subdev and media entity. * @res : Pointer to resizer device structure * return -ENOMEM or zero on success */ static int resizer_init_entities(struct isp_res_device *res) { … } /* * isp_resizer_init - Resizer initialization. * @isp : Pointer to ISP device * return -ENOMEM or zero on success */ int omap3isp_resizer_init(struct isp_device *isp) { … } void omap3isp_resizer_cleanup(struct isp_device *isp) { … }