[media] omap3isp: Remove boilerplate disclaimer and FSF address
[cascardo/linux.git] / drivers / media / platform / omap3isp / ispresizer.c
1 /*
2  * ispresizer.c
3  *
4  * TI OMAP3 ISP - Resizer module
5  *
6  * Copyright (C) 2010 Nokia Corporation
7  * Copyright (C) 2009 Texas Instruments, Inc
8  *
9  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  *           Sakari Ailus <sakari.ailus@iki.fi>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #include <linux/device.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
20
21 #include "isp.h"
22 #include "ispreg.h"
23 #include "ispresizer.h"
24
25 /*
26  * Resizer Constants
27  */
28 #define MIN_RESIZE_VALUE                64
29 #define MID_RESIZE_VALUE                512
30 #define MAX_RESIZE_VALUE                1024
31
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
38
39 #define MIN_OUT_WIDTH                   16
40 #define MIN_OUT_HEIGHT                  2
41 #define MAX_OUT_HEIGHT                  4095
42
43 /*
44  * Resizer Use Constraints
45  * "TRM ES3.1, table 12-46"
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
53
54 /*
55  * Constants for ratio calculation
56  */
57 #define RESIZE_DIVISOR                  256
58 #define DEFAULT_PHASE                   1
59
60 /*
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.
65  */
66 static const struct isprsz_coef filter_coefs = {
67         /* For 8-phase 4-tap horizontal filter: */
68         {
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
77         },
78         /* For 8-phase 4-tap vertical filter: */
79         {
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
88         },
89         /* For 4-phase 7-tap horizontal filter: */
90         #define DUMMY 0
91         {
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
96         },
97         /* For 4-phase 7-tap vertical filter: */
98         {
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
103         }
104         /*
105          * The dummy padding is required in 7-tap mode because of how the
106          * registers are arranged physically.
107          */
108         #undef DUMMY
109 };
110
111 /*
112  * __resizer_get_format - helper function for getting resizer format
113  * @res   : pointer to resizer private structure
114  * @pad   : pad number
115  * @fh    : V4L2 subdev file handle
116  * @which : wanted subdev format
117  * return zero
118  */
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)
122 {
123         if (which == V4L2_SUBDEV_FORMAT_TRY)
124                 return v4l2_subdev_get_try_format(fh, pad);
125         else
126                 return &res->formats[pad];
127 }
128
129 /*
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
134  */
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)
138 {
139         if (which == V4L2_SUBDEV_FORMAT_TRY)
140                 return v4l2_subdev_get_try_crop(fh, RESZ_PAD_SINK);
141         else
142                 return &res->crop.request;
143 }
144
145 /*
146  * resizer_set_filters - Set resizer filters
147  * @res: Device context.
148  * @h_coeff: horizontal coefficient
149  * @v_coeff: vertical coefficient
150  * Return none
151  */
152 static void resizer_set_filters(struct isp_res_device *res, const u16 *h_coeff,
153                                 const u16 *v_coeff)
154 {
155         struct isp_device *isp = to_isp_device(res);
156         u32 startaddr_h, startaddr_v, tmp_h, tmp_v;
157         int i;
158
159         startaddr_h = ISPRSZ_HFILT10;
160         startaddr_v = ISPRSZ_VFILT10;
161
162         for (i = 0; i < COEFF_CNT; i += 2) {
163                 tmp_h = h_coeff[i] |
164                         (h_coeff[i + 1] << ISPRSZ_HFILT_COEF1_SHIFT);
165                 tmp_v = v_coeff[i] |
166                         (v_coeff[i + 1] << ISPRSZ_VFILT_COEF1_SHIFT);
167                 isp_reg_writel(isp, tmp_h, OMAP3_ISP_IOMEM_RESZ, startaddr_h);
168                 isp_reg_writel(isp, tmp_v, OMAP3_ISP_IOMEM_RESZ, startaddr_v);
169                 startaddr_h += 4;
170                 startaddr_v += 4;
171         }
172 }
173
174 /*
175  * resizer_set_bilinear - Chrominance horizontal algorithm select
176  * @res: Device context.
177  * @type: Filtering interpolation type.
178  *
179  * Filtering that is same as luminance processing is
180  * intended only for downsampling, and bilinear interpolation
181  * is intended only for upsampling.
182  */
183 static void resizer_set_bilinear(struct isp_res_device *res,
184                                  enum resizer_chroma_algo type)
185 {
186         struct isp_device *isp = to_isp_device(res);
187
188         if (type == RSZ_BILINEAR)
189                 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
190                             ISPRSZ_CNT_CBILIN);
191         else
192                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
193                             ISPRSZ_CNT_CBILIN);
194 }
195
196 /*
197  * resizer_set_ycpos - Luminance and chrominance order
198  * @res: Device context.
199  * @pixelcode: pixel code.
200  */
201 static void resizer_set_ycpos(struct isp_res_device *res,
202                               enum v4l2_mbus_pixelcode pixelcode)
203 {
204         struct isp_device *isp = to_isp_device(res);
205
206         switch (pixelcode) {
207         case V4L2_MBUS_FMT_YUYV8_1X16:
208                 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
209                             ISPRSZ_CNT_YCPOS);
210                 break;
211         case V4L2_MBUS_FMT_UYVY8_1X16:
212                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
213                             ISPRSZ_CNT_YCPOS);
214                 break;
215         default:
216                 return;
217         }
218 }
219
220 /*
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.
225  *
226  * Horizontal and vertical phase range is 0 to 7
227  */
228 static void resizer_set_phase(struct isp_res_device *res, u32 h_phase,
229                               u32 v_phase)
230 {
231         struct isp_device *isp = to_isp_device(res);
232         u32 rgval = 0;
233
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;
238
239         isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT);
240 }
241
242 /*
243  * resizer_set_luma - Setup luminance enhancer parameters
244  * @res: Device context.
245  * @luma: Structure for luminance enhancer parameters.
246  *
247  * Algorithm select:
248  *  0x0: Disable
249  *  0x1: [-1  2 -1]/2 high-pass filter
250  *  0x2: [-1 -2  6 -2 -1]/4 high-pass filter
251  *
252  * Maximum gain:
253  *  The data is coded in U4Q4 representation.
254  *
255  * Slope:
256  *  The data is coded in U4Q4 representation.
257  *
258  * Coring offset:
259  *  The data is coded in U8Q0 representation.
260  *
261  * The new luminance value is computed as:
262  *  Y += HPF(Y) x max(GAIN, (HPF(Y) - CORE) x SLOP + 8) >> 4.
263  */
264 static void resizer_set_luma(struct isp_res_device *res,
265                              struct resizer_luma_yenh *luma)
266 {
267         struct isp_device *isp = to_isp_device(res);
268         u32 rgval = 0;
269
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;
278
279         isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_YENH);
280 }
281
282 /*
283  * resizer_set_source - Input source select
284  * @res: Device context.
285  * @source: Input source type
286  *
287  * If this field is set to RESIZER_INPUT_VP, the resizer input is fed from
288  * Preview/CCDC engine, otherwise from memory.
289  */
290 static void resizer_set_source(struct isp_res_device *res,
291                                enum resizer_input_entity source)
292 {
293         struct isp_device *isp = to_isp_device(res);
294
295         if (source == RESIZER_INPUT_MEMORY)
296                 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
297                             ISPRSZ_CNT_INPSRC);
298         else
299                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
300                             ISPRSZ_CNT_INPSRC);
301 }
302
303 /*
304  * resizer_set_ratio - Setup horizontal and vertical resizing value
305  * @res: Device context.
306  * @ratio: Structure for ratio parameters.
307  *
308  * Resizing range from 64 to 1024
309  */
310 static void resizer_set_ratio(struct isp_res_device *res,
311                               const struct resizer_ratio *ratio)
312 {
313         struct isp_device *isp = to_isp_device(res);
314         const u16 *h_filter, *v_filter;
315         u32 rgval = 0;
316
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);
324
325         /* prepare horizontal filter coefficients */
326         if (ratio->horz > MID_RESIZE_VALUE)
327                 h_filter = &filter_coefs.h_filter_coef_7tap[0];
328         else
329                 h_filter = &filter_coefs.h_filter_coef_4tap[0];
330
331         /* prepare vertical filter coefficients */
332         if (ratio->vert > MID_RESIZE_VALUE)
333                 v_filter = &filter_coefs.v_filter_coef_7tap[0];
334         else
335                 v_filter = &filter_coefs.v_filter_coef_4tap[0];
336
337         resizer_set_filters(res, h_filter, v_filter);
338 }
339
340 /*
341  * resizer_set_dst_size - Setup the output height and width
342  * @res: Device context.
343  * @width: Output width.
344  * @height: Output height.
345  *
346  * Width :
347  *  The value must be EVEN.
348  *
349  * Height:
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)
353  */
354 static void resizer_set_output_size(struct isp_res_device *res,
355                                     u32 width, u32 height)
356 {
357         struct isp_device *isp = to_isp_device(res);
358         u32 rgval = 0;
359
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);
366 }
367
368 /*
369  * resizer_set_output_offset - Setup memory offset for the output lines.
370  * @res: Device context.
371  * @offset: Memory offset.
372  *
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
376  */
377 static void resizer_set_output_offset(struct isp_res_device *res, u32 offset)
378 {
379         struct isp_device *isp = to_isp_device(res);
380
381         isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTOFF);
382 }
383
384 /*
385  * resizer_set_start - Setup vertical and horizontal start position
386  * @res: Device context.
387  * @left: Horizontal start position.
388  * @top: Vertical start position.
389  *
390  * Vertical start line:
391  *  This field makes sense only when the resizer obtains its input
392  *  from the preview engine/CCDC
393  *
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
398  */
399 static void resizer_set_start(struct isp_res_device *res, u32 left, u32 top)
400 {
401         struct isp_device *isp = to_isp_device(res);
402         u32 rgval = 0;
403
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;
408
409         isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_START);
410 }
411
412 /*
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
417  */
418 static void resizer_set_input_size(struct isp_res_device *res,
419                                    u32 width, u32 height)
420 {
421         struct isp_device *isp = to_isp_device(res);
422         u32 rgval = 0;
423
424         dev_dbg(isp->dev, "Input size[w/h]: %dx%d\n", width, height);
425
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;
430
431         isp_reg_writel(isp, rgval, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_IN_SIZE);
432 }
433
434 /*
435  * resizer_set_src_offs - Setup the memory offset for the input lines
436  * @res: Device context.
437  * @offset: Memory offset.
438  *
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.
442  */
443 static void resizer_set_input_offset(struct isp_res_device *res, u32 offset)
444 {
445         struct isp_device *isp = to_isp_device(res);
446
447         isp_reg_writel(isp, offset, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INOFF);
448 }
449
450 /*
451  * resizer_set_intype - Input type select
452  * @res: Device context.
453  * @type: Pixel format type.
454  */
455 static void resizer_set_intype(struct isp_res_device *res,
456                                enum resizer_colors_type type)
457 {
458         struct isp_device *isp = to_isp_device(res);
459
460         if (type == RSZ_COLOR8)
461                 isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
462                             ISPRSZ_CNT_INPTYP);
463         else
464                 isp_reg_clr(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_CNT,
465                             ISPRSZ_CNT_INPTYP);
466 }
467
468 /*
469  * __resizer_set_inaddr - Helper function for set input address
470  * @res : pointer to resizer private data structure
471  * @addr: input address
472  * return none
473  */
474 static void __resizer_set_inaddr(struct isp_res_device *res, u32 addr)
475 {
476         struct isp_device *isp = to_isp_device(res);
477
478         isp_reg_writel(isp, addr, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_INADD);
479 }
480
481 /*
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.
489  *
490  * The maximum data rate at the output of the horizontal resizer can thus be
491  * computed with
492  *
493  * max intermediate rate <= L3 clock * input height / output height
494  * max intermediate rate <= L3 clock / 2
495  *
496  * The maximum data rate at the resizer input is then
497  *
498  * max input rate <= max intermediate rate * input width / output width
499  *
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.
503  */
504 void omap3isp_resizer_max_rate(struct isp_res_device *res,
505                                unsigned int *max_rate)
506 {
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);
510         unsigned long clock;
511
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);
515 }
516
517 /*
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.
523  *
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
528  * process.
529  *
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
532  * rate.
533  *
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
537  * bytes per second)
538  *
539  * request per second = data rate * 2 / 256
540  * cycles per request = cycles per second / requests per second
541  *
542  * The number of cycles per second is controlled by the L3 clock, leading to
543  *
544  * cycles per request = L3 frequency / 2 * 256 / data rate
545  */
546 static void resizer_adjust_bandwidth(struct isp_res_device *res)
547 {
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;
558         unsigned int value;
559
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);
563                 return;
564         }
565
566         switch (isp->revision) {
567         case ISP_REVISION_1_0:
568         case ISP_REVISION_2_0:
569         default:
570                 granularity = 1024;
571                 break;
572
573         case ISP_REVISION_15_0:
574                 granularity = 32;
575                 break;
576         }
577
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.
581          */
582         cycles_per_request = div_u64((u64)l3_ick / 2 * 256 + pipe->max_rate - 1,
583                                      pipe->max_rate);
584         minimum = DIV_ROUND_UP(cycles_per_request, granularity);
585
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
589          * down.
590          */
591         timeperframe = &pipe->max_timeperframe;
592
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;
598
599         maximum = cycles_per_request / granularity;
600
601         value = max(minimum, maximum);
602
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);
607 }
608
609 /*
610  * omap3isp_resizer_busy - Checks if ISP resizer is busy.
611  *
612  * Returns busy field from ISPRSZ_PCR register.
613  */
614 int omap3isp_resizer_busy(struct isp_res_device *res)
615 {
616         struct isp_device *isp = to_isp_device(res);
617
618         return isp_reg_readl(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR) &
619                              ISPRSZ_PCR_BUSY;
620 }
621
622 /*
623  * resizer_set_inaddr - Sets the memory address of the input frame.
624  * @addr: 32bit memory address aligned on 32byte boundary.
625  */
626 static void resizer_set_inaddr(struct isp_res_device *res, u32 addr)
627 {
628         res->addr_base = addr;
629
630         /* This will handle crop settings in stream off state */
631         if (res->crop_offset)
632                 addr += res->crop_offset & ~0x1f;
633
634         __resizer_set_inaddr(res, addr);
635 }
636
637 /*
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
641  * boundary.
642  */
643 static void resizer_set_outaddr(struct isp_res_device *res, u32 addr)
644 {
645         struct isp_device *isp = to_isp_device(res);
646
647         /*
648          * Set output address. This needs to be in its own function
649          * because it changes often.
650          */
651         isp_reg_writel(isp, addr << ISPRSZ_SDR_OUTADD_ADDR_SHIFT,
652                        OMAP3_ISP_IOMEM_RESZ, ISPRSZ_SDR_OUTADD);
653 }
654
655 /*
656  * resizer_print_status - Prints the values of the resizer module registers.
657  */
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))
661
662 static void resizer_print_status(struct isp_res_device *res)
663 {
664         struct isp_device *isp = to_isp_device(res);
665
666         dev_dbg(isp->dev, "-------------Resizer Register dump----------\n");
667
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);
678
679         dev_dbg(isp->dev, "--------------------------------------------\n");
680 }
681
682 /*
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
688  * return none
689  *
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.
694  *
695  * The input/output/ratio relationship is given by the OMAP34xx TRM:
696  *
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
703  *
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.
706  *
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)
712  *
713  *      iw >= (32 * sph + (ow - 1) * hrsz + 16) >> 8 - 7
714  *
715  * (where iw is the requested input width) which can be rewritten as
716  *
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
720  *
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.
726  *
727  * The reverted equations thus become
728  *
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)
735  *
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.
738  *
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.
743  *
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.
751  *
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.
756  *
757  *      min ow = ((iw - 7) * 256 - 32 - 64 * sph) / 1024 + 1
758  *      min oh = ((ih - 7) * 256 - 32 - 64 * spv) / 1024 + 1
759  *
760  * Similarly, the maximum output size is achieved with a scaling factor of 64
761  * and computed using the 4-tap equations.
762  *
763  *      max ow = ((iw - 7) * 256 + 255 - 16 - 32 * sph) / 64 + 1
764  *      max oh = ((ih - 4) * 256 + 255 - 16 - 32 * spv) / 64 + 1
765  *
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.
768  *
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
773  * requested value.
774  *
775  * To accommodate that, and make sure the TRM equations are satisfied exactly, we
776  * compute the input crop rectangle as the last step.
777  *
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.
783  */
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)
788 {
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;
799         unsigned int width;
800         unsigned int height;
801
802         /*
803          * Clamp the output height based on the hardware capabilities and
804          * compute the vertical resizing ratio.
805          */
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);
811
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);
819
820         if (ratio->vert <= MID_RESIZE_VALUE) {
821                 upscaled_height = (output->height - 1) * ratio->vert
822                                 + 32 * spv + 16;
823                 height = (upscaled_height >> 8) + 4;
824         } else {
825                 upscaled_height = (output->height - 1) * ratio->vert
826                                 + 64 * spv + 32;
827                 height = (upscaled_height >> 8) + 7;
828         }
829
830         /*
831          * Compute the minimum and maximum output widths based on the hardware
832          * capabilities. The maximum depends on the vertical resizing ratio.
833          */
834         min_width = ((input->width - 7) * 256 - 32 - 64 * sph) / 1024 + 1;
835         min_width = max_t(unsigned int, min_width, MIN_OUT_WIDTH);
836
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;
841                         break;
842
843                 case ISP_REVISION_2_0:
844                 default:
845                         max_width = MAX_4TAP_OUT_WIDTH_ES2;
846                         break;
847
848                 case ISP_REVISION_15_0:
849                         max_width = MAX_4TAP_OUT_WIDTH_3630;
850                         break;
851                 }
852         } else {
853                 switch (isp->revision) {
854                 case ISP_REVISION_1_0:
855                         max_width = MAX_7TAP_OUT_WIDTH_ES1;
856                         break;
857
858                 case ISP_REVISION_2_0:
859                 default:
860                         max_width = MAX_7TAP_OUT_WIDTH_ES2;
861                         break;
862
863                 case ISP_REVISION_15_0:
864                         max_width = MAX_7TAP_OUT_WIDTH_3630;
865                         break;
866                 }
867         }
868         max_width = min(((input->width - 7) * 256 + 255 - 16 - 32 * sph) / 64
869                         + 1, max_width);
870
871         /*
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.
877          */
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);
882
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);
890
891         if (ratio->horz <= MID_RESIZE_VALUE) {
892                 upscaled_width = (output->width - 1) * ratio->horz
893                                + 32 * sph + 16;
894                 width = (upscaled_width >> 8) + 7;
895         } else {
896                 upscaled_width = (output->width - 1) * ratio->horz
897                                + 64 * sph + 32;
898                 width = (upscaled_width >> 8) + 7;
899         }
900
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;
906 }
907
908 /*
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
913  * return none
914  */
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)
918 {
919         resizer_set_ratio(res, &res->ratio);
920
921         /* Set chrominance horizontal algorithm */
922         if (res->ratio.horz >= RESIZE_DIVISOR)
923                 resizer_set_bilinear(res, RSZ_THE_SAME);
924         else
925                 resizer_set_bilinear(res, RSZ_BILINEAR);
926
927         resizer_adjust_bandwidth(res);
928
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;
933                 /*
934                  * Write lowest 4 bits of horizontal pixel offset (in pixels),
935                  * vertical start must be 0.
936                  */
937                 resizer_set_start(res, (res->crop_offset / 2) & 0xf, 0);
938
939                 /*
940                  * Set start (read) address for cropping, in bytes.
941                  * Lowest 5 bits must be zero.
942                  */
943                 __resizer_set_inaddr(res,
944                                 res->addr_base + (res->crop_offset & ~0x1f));
945         } else {
946                 /*
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).
950                  */
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);
956         }
957
958         /* Set the input size */
959         resizer_set_input_size(res, res->crop.active.width,
960                                res->crop.active.height);
961 }
962
963 static void resizer_configure(struct isp_res_device *res)
964 {
965         struct v4l2_mbus_framefmt *informat, *outformat;
966         struct resizer_luma_yenh luma = {0, 0, 0, 0};
967
968         resizer_set_source(res, res->input);
969
970         informat = &res->formats[RESZ_PAD_SINK];
971         outformat = &res->formats[RESZ_PAD_SOURCE];
972
973         /* RESZ_PAD_SINK */
974         if (res->input == RESIZER_INPUT_VP)
975                 resizer_set_input_offset(res, 0);
976         else
977                 resizer_set_input_offset(res, ALIGN(informat->width, 0x10) * 2);
978
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);
984
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);
988
989         resizer_set_crop_params(res, informat, outformat);
990 }
991
992 /* -----------------------------------------------------------------------------
993  * Interrupt handling
994  */
995
996 static void resizer_enable_oneshot(struct isp_res_device *res)
997 {
998         struct isp_device *isp = to_isp_device(res);
999
1000         isp_reg_set(isp, OMAP3_ISP_IOMEM_RESZ, ISPRSZ_PCR,
1001                     ISPRSZ_PCR_ENABLE | ISPRSZ_PCR_ONESHOT);
1002 }
1003
1004 void omap3isp_resizer_isr_frame_sync(struct isp_res_device *res)
1005 {
1006         /*
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
1010          * mode.
1011          */
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);
1016         }
1017 }
1018
1019 static void resizer_isr_buffer(struct isp_res_device *res)
1020 {
1021         struct isp_pipeline *pipe = to_isp_pipeline(&res->subdev.entity);
1022         struct isp_buffer *buffer;
1023         int restart = 0;
1024
1025         if (res->state == ISP_PIPELINE_STREAM_STOPPED)
1026                 return;
1027
1028         /* Complete the output buffer and, if reading from memory, the input
1029          * buffer.
1030          */
1031         buffer = omap3isp_video_buffer_next(&res->video_out);
1032         if (buffer != NULL) {
1033                 resizer_set_outaddr(res, buffer->dma);
1034                 restart = 1;
1035         }
1036
1037         pipe->state |= ISP_PIPELINE_IDLE_OUTPUT;
1038
1039         if (res->input == RESIZER_INPUT_MEMORY) {
1040                 buffer = omap3isp_video_buffer_next(&res->video_in);
1041                 if (buffer != NULL)
1042                         resizer_set_inaddr(res, buffer->dma);
1043                 pipe->state |= ISP_PIPELINE_IDLE_INPUT;
1044         }
1045
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);
1050         } else {
1051                 /* If an underrun occurs, the video queue operation handler will
1052                  * restart the resizer. Otherwise restart it immediately.
1053                  */
1054                 if (restart)
1055                         resizer_enable_oneshot(res);
1056         }
1057 }
1058
1059 /*
1060  * omap3isp_resizer_isr - ISP resizer interrupt handler
1061  *
1062  * Manage the resizer video buffers and configure shadowed and busy-locked
1063  * registers.
1064  */
1065 void omap3isp_resizer_isr(struct isp_res_device *res)
1066 {
1067         struct v4l2_mbus_framefmt *informat, *outformat;
1068
1069         if (omap3isp_module_sync_is_stopping(&res->wait, &res->stopping))
1070                 return;
1071
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);
1078                 res->applycrop = 0;
1079         }
1080
1081         resizer_isr_buffer(res);
1082 }
1083
1084 /* -----------------------------------------------------------------------------
1085  * ISP video operations
1086  */
1087
1088 static int resizer_video_queue(struct isp_video *video,
1089                                struct isp_buffer *buffer)
1090 {
1091         struct isp_res_device *res = &video->isp->isp_res;
1092
1093         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1094                 resizer_set_inaddr(res, buffer->dma);
1095
1096         /*
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
1103          * frame correctly.
1104          *
1105          * Restart the resizer on the next sync interrupt if running in
1106          * continuous mode or when starting the stream.
1107          */
1108         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1109                 resizer_set_outaddr(res, buffer->dma);
1110
1111         return 0;
1112 }
1113
1114 static const struct isp_video_operations resizer_video_ops = {
1115         .queue = resizer_video_queue,
1116 };
1117
1118 /* -----------------------------------------------------------------------------
1119  * V4L2 subdev operations
1120  */
1121
1122 /*
1123  * resizer_set_stream - Enable/Disable streaming on resizer subdev
1124  * @sd: ISP resizer V4L2 subdev
1125  * @enable: 1 == Enable, 0 == Disable
1126  *
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().
1131  */
1132 static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
1133 {
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);
1138
1139         if (res->state == ISP_PIPELINE_STREAM_STOPPED) {
1140                 if (enable == ISP_PIPELINE_STREAM_STOPPED)
1141                         return 0;
1142
1143                 omap3isp_subclk_enable(isp, OMAP3_ISP_SUBCLK_RESIZER);
1144                 resizer_configure(res);
1145                 resizer_print_status(res);
1146         }
1147
1148         switch (enable) {
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);
1154                 }
1155                 break;
1156
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);
1161
1162                 resizer_enable_oneshot(res);
1163                 break;
1164
1165         case ISP_PIPELINE_STREAM_STOPPED:
1166                 if (omap3isp_module_sync_idle(&sd->entity, &res->wait,
1167                                               &res->stopping))
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);
1173                 break;
1174         }
1175
1176         res->state = enable;
1177         return 0;
1178 }
1179
1180 /*
1181  * resizer_try_crop - mangles crop parameters.
1182  */
1183 static void resizer_try_crop(const struct v4l2_mbus_framefmt *sink,
1184                              const struct v4l2_mbus_framefmt *source,
1185                              struct v4l2_rect *crop)
1186 {
1187         const unsigned int spv = DEFAULT_PHASE;
1188         const unsigned int sph = DEFAULT_PHASE;
1189
1190         /* Crop rectangle is constrained by the output size so that zoom ratio
1191          * cannot exceed +/-4.0.
1192          */
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;
1201
1202         crop->width = clamp_t(u32, crop->width, min_width, max_width);
1203         crop->height = clamp_t(u32, crop->height, min_height, max_height);
1204
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);
1212 }
1213
1214 /*
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
1219  *
1220  * The only supported rectangles are the crop rectangles on the sink pad.
1221  *
1222  * Return 0 on success or a negative error code otherwise.
1223  */
1224 static int resizer_get_selection(struct v4l2_subdev *sd,
1225                                  struct v4l2_subdev_fh *fh,
1226                                  struct v4l2_subdev_selection *sel)
1227 {
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;
1232
1233         if (sel->pad != RESZ_PAD_SINK)
1234                 return -EINVAL;
1235
1236         format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1237                                            sel->which);
1238         format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1239                                              sel->which);
1240
1241         switch (sel->target) {
1242         case V4L2_SEL_TGT_CROP_BOUNDS:
1243                 sel->r.left = 0;
1244                 sel->r.top = 0;
1245                 sel->r.width = INT_MAX;
1246                 sel->r.height = INT_MAX;
1247
1248                 resizer_try_crop(format_sink, format_source, &sel->r);
1249                 resizer_calc_ratios(res, &sel->r, format_source, &ratio);
1250                 break;
1251
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);
1255                 break;
1256
1257         default:
1258                 return -EINVAL;
1259         }
1260
1261         return 0;
1262 }
1263
1264 /*
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
1269  *
1270  * The only supported rectangle is the actual crop rectangle on the sink pad.
1271  *
1272  * FIXME: This function currently behaves as if the KEEP_CONFIG selection flag
1273  * was always set.
1274  *
1275  * Return 0 on success or a negative error code otherwise.
1276  */
1277 static int resizer_set_selection(struct v4l2_subdev *sd,
1278                                  struct v4l2_subdev_fh *fh,
1279                                  struct v4l2_subdev_selection *sel)
1280 {
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;
1285
1286         if (sel->target != V4L2_SEL_TGT_CROP ||
1287             sel->pad != RESZ_PAD_SINK)
1288                 return -EINVAL;
1289
1290         format_sink = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1291                                            sel->which);
1292         format_source = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1293                                              sel->which);
1294
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,
1297                 sel->which);
1298
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);
1302
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.
1310          */
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);
1314
1315         if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1316                 return 0;
1317
1318         res->ratio = ratio;
1319         res->crop.active = sel->r;
1320
1321         /*
1322          * set_selection can be called while streaming is on. In this case the
1323          * crop values will be set in the next IRQ.
1324          */
1325         if (res->state != ISP_PIPELINE_STREAM_STOPPED)
1326                 res->applycrop = 1;
1327
1328         return 0;
1329 }
1330
1331 /* resizer pixel formats */
1332 static const unsigned int resizer_formats[] = {
1333         V4L2_MBUS_FMT_UYVY8_1X16,
1334         V4L2_MBUS_FMT_YUYV8_1X16,
1335 };
1336
1337 static unsigned int resizer_max_in_width(struct isp_res_device *res)
1338 {
1339         struct isp_device *isp = to_isp_device(res);
1340
1341         if (res->input == RESIZER_INPUT_MEMORY) {
1342                 return MAX_IN_WIDTH_MEMORY_MODE;
1343         } else {
1344                 if (isp->revision == ISP_REVISION_1_0)
1345                         return MAX_IN_WIDTH_ONTHEFLY_MODE_ES1;
1346                 else
1347                         return MAX_IN_WIDTH_ONTHEFLY_MODE_ES2;
1348         }
1349 }
1350
1351 /*
1352  * resizer_try_format - Handle try format by pad subdev method
1353  * @res   : ISP resizer device
1354  * @fh    : V4L2 subdev file handle
1355  * @pad   : pad num
1356  * @fmt   : pointer to v4l2 format structure
1357  * @which : wanted subdev format
1358  */
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)
1363 {
1364         struct v4l2_mbus_framefmt *format;
1365         struct resizer_ratio ratio;
1366         struct v4l2_rect crop;
1367
1368         switch (pad) {
1369         case RESZ_PAD_SINK:
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;
1373
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,
1377                                       MAX_IN_HEIGHT);
1378                 break;
1379
1380         case RESZ_PAD_SOURCE:
1381                 format = __resizer_get_format(res, fh, RESZ_PAD_SINK, which);
1382                 fmt->code = format->code;
1383
1384                 crop = *__resizer_get_crop(res, fh, which);
1385                 resizer_calc_ratios(res, &crop, fmt, &ratio);
1386                 break;
1387         }
1388
1389         fmt->colorspace = V4L2_COLORSPACE_JPEG;
1390         fmt->field = V4L2_FIELD_NONE;
1391 }
1392
1393 /*
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
1399  */
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)
1403 {
1404         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1405         struct v4l2_mbus_framefmt *format;
1406
1407         if (code->pad == RESZ_PAD_SINK) {
1408                 if (code->index >= ARRAY_SIZE(resizer_formats))
1409                         return -EINVAL;
1410
1411                 code->code = resizer_formats[code->index];
1412         } else {
1413                 if (code->index != 0)
1414                         return -EINVAL;
1415
1416                 format = __resizer_get_format(res, fh, RESZ_PAD_SINK,
1417                                               V4L2_SUBDEV_FORMAT_TRY);
1418                 code->code = format->code;
1419         }
1420
1421         return 0;
1422 }
1423
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)
1427 {
1428         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1429         struct v4l2_mbus_framefmt format;
1430
1431         if (fse->index != 0)
1432                 return -EINVAL;
1433
1434         format.code = fse->code;
1435         format.width = 1;
1436         format.height = 1;
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;
1440
1441         if (format.code != fse->code)
1442                 return -EINVAL;
1443
1444         format.code = fse->code;
1445         format.width = -1;
1446         format.height = -1;
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;
1450
1451         return 0;
1452 }
1453
1454 /*
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
1460  */
1461 static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1462                               struct v4l2_subdev_format *fmt)
1463 {
1464         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1465         struct v4l2_mbus_framefmt *format;
1466
1467         format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1468         if (format == NULL)
1469                 return -EINVAL;
1470
1471         fmt->format = *format;
1472         return 0;
1473 }
1474
1475 /*
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
1481  */
1482 static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1483                               struct v4l2_subdev_format *fmt)
1484 {
1485         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1486         struct v4l2_mbus_framefmt *format;
1487         struct v4l2_rect *crop;
1488
1489         format = __resizer_get_format(res, fh, fmt->pad, fmt->which);
1490         if (format == NULL)
1491                 return -EINVAL;
1492
1493         resizer_try_format(res, fh, fmt->pad, &fmt->format, fmt->which);
1494         *format = fmt->format;
1495
1496         if (fmt->pad == RESZ_PAD_SINK) {
1497                 /* reset crop rectangle */
1498                 crop = __resizer_get_crop(res, fh, fmt->which);
1499                 crop->left = 0;
1500                 crop->top = 0;
1501                 crop->width = fmt->format.width;
1502                 crop->height = fmt->format.height;
1503
1504                 /* Propagate the format from sink to source */
1505                 format = __resizer_get_format(res, fh, RESZ_PAD_SOURCE,
1506                                               fmt->which);
1507                 *format = fmt->format;
1508                 resizer_try_format(res, fh, RESZ_PAD_SOURCE, format,
1509                                    fmt->which);
1510         }
1511
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
1515                  * format.
1516                  */
1517                 res->crop.active = res->crop.request;
1518                 resizer_calc_ratios(res, &res->crop.active, format,
1519                                        &res->ratio);
1520         }
1521
1522         return 0;
1523 }
1524
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)
1529 {
1530         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1531         struct isp_pipeline *pipe = to_isp_pipeline(&sd->entity);
1532
1533         omap3isp_resizer_max_rate(res, &pipe->max_rate);
1534
1535         return v4l2_subdev_link_validate_default(sd, link,
1536                                                  source_fmt, sink_fmt);
1537 }
1538
1539 /*
1540  * resizer_init_formats - Initialize formats on all pads
1541  * @sd: ISP resizer V4L2 subdevice
1542  * @fh: V4L2 subdev file handle
1543  *
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.
1547  */
1548 static int resizer_init_formats(struct v4l2_subdev *sd,
1549                                 struct v4l2_subdev_fh *fh)
1550 {
1551         struct v4l2_subdev_format format;
1552
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);
1560
1561         return 0;
1562 }
1563
1564 /* subdev video operations */
1565 static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
1566         .s_stream = resizer_set_stream,
1567 };
1568
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,
1578 };
1579
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,
1584 };
1585
1586 /* subdev internal operations */
1587 static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
1588         .open = resizer_init_formats,
1589 };
1590
1591 /* -----------------------------------------------------------------------------
1592  * Media entity operations
1593  */
1594
1595 /*
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
1602  */
1603 static int resizer_link_setup(struct media_entity *entity,
1604                               const struct media_pad *local,
1605                               const struct media_pad *remote, u32 flags)
1606 {
1607         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1608         struct isp_res_device *res = v4l2_get_subdevdata(sd);
1609
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)
1615                                 return -EBUSY;
1616                         res->input = RESIZER_INPUT_MEMORY;
1617                 } else {
1618                         if (res->input == RESIZER_INPUT_MEMORY)
1619                                 res->input = RESIZER_INPUT_NONE;
1620                 }
1621                 break;
1622
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)
1627                                 return -EBUSY;
1628                         res->input = RESIZER_INPUT_VP;
1629                 } else {
1630                         if (res->input == RESIZER_INPUT_VP)
1631                                 res->input = RESIZER_INPUT_NONE;
1632                 }
1633                 break;
1634
1635         case RESZ_PAD_SOURCE | MEDIA_ENT_T_DEVNODE:
1636                 /* resizer always write to memory */
1637                 break;
1638
1639         default:
1640                 return -EINVAL;
1641         }
1642
1643         return 0;
1644 }
1645
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,
1650 };
1651
1652 void omap3isp_resizer_unregister_entities(struct isp_res_device *res)
1653 {
1654         v4l2_device_unregister_subdev(&res->subdev);
1655         omap3isp_video_unregister(&res->video_in);
1656         omap3isp_video_unregister(&res->video_out);
1657 }
1658
1659 int omap3isp_resizer_register_entities(struct isp_res_device *res,
1660                                        struct v4l2_device *vdev)
1661 {
1662         int ret;
1663
1664         /* Register the subdev and video nodes. */
1665         ret = v4l2_device_register_subdev(vdev, &res->subdev);
1666         if (ret < 0)
1667                 goto error;
1668
1669         ret = omap3isp_video_register(&res->video_in, vdev);
1670         if (ret < 0)
1671                 goto error;
1672
1673         ret = omap3isp_video_register(&res->video_out, vdev);
1674         if (ret < 0)
1675                 goto error;
1676
1677         return 0;
1678
1679 error:
1680         omap3isp_resizer_unregister_entities(res);
1681         return ret;
1682 }
1683
1684 /* -----------------------------------------------------------------------------
1685  * ISP resizer initialization and cleanup
1686  */
1687
1688 /*
1689  * resizer_init_entities - Initialize resizer subdev and media entity.
1690  * @res : Pointer to resizer device structure
1691  * return -ENOMEM or zero on success
1692  */
1693 static int resizer_init_entities(struct isp_res_device *res)
1694 {
1695         struct v4l2_subdev *sd = &res->subdev;
1696         struct media_pad *pads = res->pads;
1697         struct media_entity *me = &sd->entity;
1698         int ret;
1699
1700         res->input = RESIZER_INPUT_NONE;
1701
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;
1708
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;
1712
1713         me->ops = &resizer_media_ops;
1714         ret = media_entity_init(me, RESZ_PADS_NUM, pads, 0);
1715         if (ret < 0)
1716                 return ret;
1717
1718         resizer_init_formats(sd, NULL);
1719
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;
1730
1731         ret = omap3isp_video_init(&res->video_in, "resizer");
1732         if (ret < 0)
1733                 goto error_video_in;
1734
1735         ret = omap3isp_video_init(&res->video_out, "resizer");
1736         if (ret < 0)
1737                 goto error_video_out;
1738
1739         res->video_out.video.entity.flags |= MEDIA_ENT_FL_DEFAULT;
1740
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);
1744         if (ret < 0)
1745                 goto error_link;
1746
1747         ret = media_entity_create_link(&res->subdev.entity, RESZ_PAD_SOURCE,
1748                         &res->video_out.video.entity, 0, 0);
1749         if (ret < 0)
1750                 goto error_link;
1751
1752         return 0;
1753
1754 error_link:
1755         omap3isp_video_cleanup(&res->video_out);
1756 error_video_out:
1757         omap3isp_video_cleanup(&res->video_in);
1758 error_video_in:
1759         media_entity_cleanup(&res->subdev.entity);
1760         return ret;
1761 }
1762
1763 /*
1764  * isp_resizer_init - Resizer initialization.
1765  * @isp : Pointer to ISP device
1766  * return -ENOMEM or zero on success
1767  */
1768 int omap3isp_resizer_init(struct isp_device *isp)
1769 {
1770         struct isp_res_device *res = &isp->isp_res;
1771
1772         init_waitqueue_head(&res->wait);
1773         atomic_set(&res->stopping, 0);
1774         return resizer_init_entities(res);
1775 }
1776
1777 void omap3isp_resizer_cleanup(struct isp_device *isp)
1778 {
1779         struct isp_res_device *res = &isp->isp_res;
1780
1781         omap3isp_video_cleanup(&res->video_in);
1782         omap3isp_video_cleanup(&res->video_out);
1783         media_entity_cleanup(&res->subdev.entity);
1784 }