[media] s5p-fimc: Convert to use media pipeline operations
[pandora-kernel.git] / drivers / media / video / s5p-fimc / fimc-core.h
1 /*
2  * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #ifndef FIMC_CORE_H_
10 #define FIMC_CORE_H_
11
12 /*#define DEBUG*/
13
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/videodev2.h>
19 #include <linux/io.h>
20
21 #include <media/media-entity.h>
22 #include <media/videobuf2-core.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-mem2mem.h>
26 #include <media/v4l2-mediabus.h>
27 #include <media/s5p_fimc.h>
28
29 #include "regs-fimc.h"
30
31 #define err(fmt, args...) \
32         printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
33
34 #define dbg(fmt, args...) \
35         pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args)
36
37 /* Time to wait for next frame VSYNC interrupt while stopping operation. */
38 #define FIMC_SHUTDOWN_TIMEOUT   ((100*HZ)/1000)
39 #define MAX_FIMC_CLOCKS         2
40 #define FIMC_MODULE_NAME        "s5p-fimc"
41 #define FIMC_MAX_DEVS           4
42 #define FIMC_MAX_OUT_BUFS       4
43 #define SCALER_MAX_HRATIO       64
44 #define SCALER_MAX_VRATIO       64
45 #define DMA_MIN_SIZE            8
46
47 /* indices to the clocks array */
48 enum {
49         CLK_BUS,
50         CLK_GATE,
51 };
52
53 enum fimc_dev_flags {
54         ST_LPM,
55         /* m2m node */
56         ST_M2M_RUN,
57         ST_M2M_PEND,
58         ST_M2M_SUSPENDING,
59         ST_M2M_SUSPENDED,
60         /* capture node */
61         ST_CAPT_PEND,
62         ST_CAPT_RUN,
63         ST_CAPT_STREAM,
64         ST_CAPT_ISP_STREAM,
65         ST_CAPT_SHUT,
66         ST_CAPT_BUSY,
67         ST_CAPT_APPLY_CFG,
68 };
69
70 #define fimc_m2m_active(dev) test_bit(ST_M2M_RUN, &(dev)->state)
71 #define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
72
73 #define fimc_capture_running(dev) test_bit(ST_CAPT_RUN, &(dev)->state)
74 #define fimc_capture_pending(dev) test_bit(ST_CAPT_PEND, &(dev)->state)
75 #define fimc_capture_busy(dev) test_bit(ST_CAPT_BUSY, &(dev)->state)
76
77 enum fimc_datapath {
78         FIMC_CAMERA,
79         FIMC_DMA,
80         FIMC_LCDFIFO,
81         FIMC_WRITEBACK
82 };
83
84 enum fimc_color_fmt {
85         S5P_FIMC_RGB565 = 0x10,
86         S5P_FIMC_RGB666,
87         S5P_FIMC_RGB888,
88         S5P_FIMC_RGB30_LOCAL,
89         S5P_FIMC_YCBCR420 = 0x20,
90         S5P_FIMC_YCBYCR422,
91         S5P_FIMC_YCRYCB422,
92         S5P_FIMC_CBYCRY422,
93         S5P_FIMC_CRYCBY422,
94         S5P_FIMC_YCBCR444_LOCAL,
95 };
96
97 #define fimc_fmt_is_rgb(x) ((x) & 0x10)
98
99 #define IS_M2M(__strt) ((__strt) == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || \
100                         __strt == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
101
102 /* Cb/Cr chrominance components order for 2 plane Y/CbCr 4:2:2 formats. */
103 #define S5P_FIMC_LSB_CRCB       S5P_CIOCTRL_ORDER422_2P_LSB_CRCB
104
105 /* The embedded image effect selection */
106 #define S5P_FIMC_EFFECT_ORIGINAL        S5P_CIIMGEFF_FIN_BYPASS
107 #define S5P_FIMC_EFFECT_ARBITRARY       S5P_CIIMGEFF_FIN_ARBITRARY
108 #define S5P_FIMC_EFFECT_NEGATIVE        S5P_CIIMGEFF_FIN_NEGATIVE
109 #define S5P_FIMC_EFFECT_ARTFREEZE       S5P_CIIMGEFF_FIN_ARTFREEZE
110 #define S5P_FIMC_EFFECT_EMBOSSING       S5P_CIIMGEFF_FIN_EMBOSSING
111 #define S5P_FIMC_EFFECT_SIKHOUETTE      S5P_CIIMGEFF_FIN_SILHOUETTE
112
113 /* The hardware context state. */
114 #define FIMC_PARAMS             (1 << 0)
115 #define FIMC_SRC_ADDR           (1 << 1)
116 #define FIMC_DST_ADDR           (1 << 2)
117 #define FIMC_SRC_FMT            (1 << 3)
118 #define FIMC_DST_FMT            (1 << 4)
119 #define FIMC_CTX_M2M            (1 << 5)
120 #define FIMC_CTX_CAP            (1 << 6)
121 #define FIMC_CTX_SHUT           (1 << 7)
122
123 /* Image conversion flags */
124 #define FIMC_IN_DMA_ACCESS_TILED        (1 << 0)
125 #define FIMC_IN_DMA_ACCESS_LINEAR       (0 << 0)
126 #define FIMC_OUT_DMA_ACCESS_TILED       (1 << 1)
127 #define FIMC_OUT_DMA_ACCESS_LINEAR      (0 << 1)
128 #define FIMC_SCAN_MODE_PROGRESSIVE      (0 << 2)
129 #define FIMC_SCAN_MODE_INTERLACED       (1 << 2)
130 /*
131  * YCbCr data dynamic range for RGB-YUV color conversion.
132  * Y/Cb/Cr: (0 ~ 255) */
133 #define FIMC_COLOR_RANGE_WIDE           (0 << 3)
134 /* Y (16 ~ 235), Cb/Cr (16 ~ 240) */
135 #define FIMC_COLOR_RANGE_NARROW         (1 << 3)
136
137 /**
138  * struct fimc_fmt - the driver's internal color format data
139  * @mbus_code: Media Bus pixel code, -1 if not applicable
140  * @name: format description
141  * @fourcc: the fourcc code for this format, 0 if not applicable
142  * @color: the corresponding fimc_color_fmt
143  * @memplanes: number of physically non-contiguous data planes
144  * @colplanes: number of physically contiguous data planes
145  * @depth: per plane driver's private 'number of bits per pixel'
146  * @flags: flags indicating which operation mode format applies to
147  */
148 struct fimc_fmt {
149         enum v4l2_mbus_pixelcode mbus_code;
150         char    *name;
151         u32     fourcc;
152         u32     color;
153         u16     memplanes;
154         u16     colplanes;
155         u8      depth[VIDEO_MAX_PLANES];
156         u16     flags;
157 #define FMT_FLAGS_CAM   (1 << 0)
158 #define FMT_FLAGS_M2M   (1 << 1)
159 };
160
161 /**
162  * struct fimc_dma_offset - pixel offset information for DMA
163  * @y_h:        y value horizontal offset
164  * @y_v:        y value vertical offset
165  * @cb_h:       cb value horizontal offset
166  * @cb_v:       cb value vertical offset
167  * @cr_h:       cr value horizontal offset
168  * @cr_v:       cr value vertical offset
169  */
170 struct fimc_dma_offset {
171         int     y_h;
172         int     y_v;
173         int     cb_h;
174         int     cb_v;
175         int     cr_h;
176         int     cr_v;
177 };
178
179 /**
180  * struct fimc_effect - color effect information
181  * @type:       effect type
182  * @pat_cb:     cr value when type is "arbitrary"
183  * @pat_cr:     cr value when type is "arbitrary"
184  */
185 struct fimc_effect {
186         u32     type;
187         u8      pat_cb;
188         u8      pat_cr;
189 };
190
191 /**
192  * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
193  * @scaleup_h:          flag indicating scaling up horizontally
194  * @scaleup_v:          flag indicating scaling up vertically
195  * @copy_mode:          flag indicating transparent DMA transfer (no scaling
196  *                      and color format conversion)
197  * @enabled:            flag indicating if the scaler is used
198  * @hfactor:            horizontal shift factor
199  * @vfactor:            vertical shift factor
200  * @pre_hratio:         horizontal ratio of the prescaler
201  * @pre_vratio:         vertical ratio of the prescaler
202  * @pre_dst_width:      the prescaler's destination width
203  * @pre_dst_height:     the prescaler's destination height
204  * @main_hratio:        the main scaler's horizontal ratio
205  * @main_vratio:        the main scaler's vertical ratio
206  * @real_width:         source pixel (width - offset)
207  * @real_height:        source pixel (height - offset)
208  */
209 struct fimc_scaler {
210         unsigned int scaleup_h:1;
211         unsigned int scaleup_v:1;
212         unsigned int copy_mode:1;
213         unsigned int enabled:1;
214         u32     hfactor;
215         u32     vfactor;
216         u32     pre_hratio;
217         u32     pre_vratio;
218         u32     pre_dst_width;
219         u32     pre_dst_height;
220         u32     main_hratio;
221         u32     main_vratio;
222         u32     real_width;
223         u32     real_height;
224 };
225
226 /**
227  * struct fimc_addr - the FIMC physical address set for DMA
228  * @y:   luminance plane physical address
229  * @cb:  Cb plane physical address
230  * @cr:  Cr plane physical address
231  */
232 struct fimc_addr {
233         u32     y;
234         u32     cb;
235         u32     cr;
236 };
237
238 /**
239  * struct fimc_vid_buffer - the driver's video buffer
240  * @vb:    v4l videobuf buffer
241  * @list:  linked list structure for buffer queue
242  * @paddr: precalculated physical address set
243  * @index: buffer index for the output DMA engine
244  */
245 struct fimc_vid_buffer {
246         struct vb2_buffer       vb;
247         struct list_head        list;
248         struct fimc_addr        paddr;
249         int                     index;
250 };
251
252 /**
253  * struct fimc_frame - source/target frame properties
254  * @f_width:    image full width (virtual screen size)
255  * @f_height:   image full height (virtual screen size)
256  * @o_width:    original image width as set by S_FMT
257  * @o_height:   original image height as set by S_FMT
258  * @offs_h:     image horizontal pixel offset
259  * @offs_v:     image vertical pixel offset
260  * @width:      image pixel width
261  * @height:     image pixel weight
262  * @payload:    image size in bytes (w x h x bpp)
263  * @paddr:      image frame buffer physical addresses
264  * @dma_offset: DMA offset in bytes
265  * @fmt:        fimc color format pointer
266  */
267 struct fimc_frame {
268         u32     f_width;
269         u32     f_height;
270         u32     o_width;
271         u32     o_height;
272         u32     offs_h;
273         u32     offs_v;
274         u32     width;
275         u32     height;
276         unsigned long           payload[VIDEO_MAX_PLANES];
277         struct fimc_addr        paddr;
278         struct fimc_dma_offset  dma_offset;
279         struct fimc_fmt         *fmt;
280 };
281
282 /**
283  * struct fimc_m2m_device - v4l2 memory-to-memory device data
284  * @vfd: the video device node for v4l2 m2m mode
285  * @m2m_dev: v4l2 memory-to-memory device data
286  * @ctx: hardware context data
287  * @refcnt: the reference counter
288  */
289 struct fimc_m2m_device {
290         struct video_device     *vfd;
291         struct v4l2_m2m_dev     *m2m_dev;
292         struct fimc_ctx         *ctx;
293         int                     refcnt;
294 };
295
296 /**
297  * struct fimc_vid_cap - camera capture device information
298  * @ctx: hardware context data
299  * @vfd: video device node for camera capture mode
300  * @vd_pad: fimc video capture node pad
301  * @fmt: Media Bus format configured at selected image sensor
302  * @pending_buf_q: the pending buffer queue head
303  * @active_buf_q: the queue head of buffers scheduled in hardware
304  * @vbq: the capture am video buffer queue
305  * @active_buf_cnt: number of video buffers scheduled in hardware
306  * @buf_index: index for managing the output DMA buffers
307  * @frame_count: the frame counter for statistics
308  * @reqbufs_count: the number of buffers requested in REQBUFS ioctl
309  * @input_index: input (camera sensor) index
310  * @refcnt: driver's private reference counter
311  * @input: capture input type, grp_id of the attached subdev
312  * @user_subdev_api: true if subdevs are not configured by the host driver
313  */
314 struct fimc_vid_cap {
315         struct fimc_ctx                 *ctx;
316         struct vb2_alloc_ctx            *alloc_ctx;
317         struct video_device             *vfd;
318         struct media_pad                vd_pad;
319         struct v4l2_mbus_framefmt       fmt;
320         struct list_head                pending_buf_q;
321         struct list_head                active_buf_q;
322         struct vb2_queue                vbq;
323         int                             active_buf_cnt;
324         int                             buf_index;
325         unsigned int                    frame_count;
326         unsigned int                    reqbufs_count;
327         int                             input_index;
328         int                             refcnt;
329         u32                             input;
330         bool                            user_subdev_api;
331 };
332
333 /**
334  *  struct fimc_pix_limit - image pixel size limits in various IP configurations
335  *
336  *  @scaler_en_w: max input pixel width when the scaler is enabled
337  *  @scaler_dis_w: max input pixel width when the scaler is disabled
338  *  @in_rot_en_h: max input width with the input rotator is on
339  *  @in_rot_dis_w: max input width with the input rotator is off
340  *  @out_rot_en_w: max output width with the output rotator on
341  *  @out_rot_dis_w: max output width with the output rotator off
342  */
343 struct fimc_pix_limit {
344         u16 scaler_en_w;
345         u16 scaler_dis_w;
346         u16 in_rot_en_h;
347         u16 in_rot_dis_w;
348         u16 out_rot_en_w;
349         u16 out_rot_dis_w;
350 };
351
352 /**
353  * struct samsung_fimc_variant - camera interface variant information
354  *
355  * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
356  * @has_inp_rot: set if has input rotator
357  * @has_out_rot: set if has output rotator
358  * @has_cistatus2: 1 if CISTATUS2 register is present in this IP revision
359  * @has_mainscaler_ext: 1 if extended mainscaler ratios in CIEXTEN register
360  *                       are present in this IP revision
361  * @has_cam_if: set if this instance has a camera input interface
362  * @pix_limit: pixel size constraints for the scaler
363  * @min_inp_pixsize: minimum input pixel size
364  * @min_out_pixsize: minimum output pixel size
365  * @hor_offs_align: horizontal pixel offset aligment
366  * @out_buf_count: the number of buffers in output DMA sequence
367  */
368 struct samsung_fimc_variant {
369         unsigned int    pix_hoff:1;
370         unsigned int    has_inp_rot:1;
371         unsigned int    has_out_rot:1;
372         unsigned int    has_cistatus2:1;
373         unsigned int    has_mainscaler_ext:1;
374         unsigned int    has_cam_if:1;
375         struct fimc_pix_limit *pix_limit;
376         u16             min_inp_pixsize;
377         u16             min_out_pixsize;
378         u16             hor_offs_align;
379         u16             out_buf_count;
380 };
381
382 /**
383  * struct samsung_fimc_driverdata - per device type driver data for init time.
384  *
385  * @variant: the variant information for this driver.
386  * @dev_cnt: number of fimc sub-devices available in SoC
387  * @lclk_frequency: fimc bus clock frequency
388  */
389 struct samsung_fimc_driverdata {
390         struct samsung_fimc_variant *variant[FIMC_MAX_DEVS];
391         unsigned long   lclk_frequency;
392         int             num_entities;
393 };
394
395 struct fimc_pipeline {
396         struct media_pipeline *pipe;
397         struct v4l2_subdev *sensor;
398         struct v4l2_subdev *csis;
399 };
400
401 struct fimc_ctx;
402
403 /**
404  * struct fimc_dev - abstraction for FIMC entity
405  * @slock:      the spinlock protecting this data structure
406  * @lock:       the mutex protecting this data structure
407  * @pdev:       pointer to the FIMC platform device
408  * @pdata:      pointer to the device platform data
409  * @variant:    the IP variant information
410  * @id:         FIMC device index (0..FIMC_MAX_DEVS)
411  * @num_clocks: the number of clocks managed by this device instance
412  * @clock:      clocks required for FIMC operation
413  * @regs:       the mapped hardware registers
414  * @regs_res:   the resource claimed for IO registers
415  * @irq:        FIMC interrupt number
416  * @irq_queue:  interrupt handler waitqueue
417  * @v4l2_dev:   root v4l2_device
418  * @m2m:        memory-to-memory V4L2 device information
419  * @vid_cap:    camera capture device information
420  * @state:      flags used to synchronize m2m and capture mode operation
421  * @alloc_ctx:  videobuf2 memory allocator context
422  * @pipeline:   fimc video capture pipeline data structure
423  */
424 struct fimc_dev {
425         spinlock_t                      slock;
426         struct mutex                    lock;
427         struct platform_device          *pdev;
428         struct s5p_platform_fimc        *pdata;
429         struct samsung_fimc_variant     *variant;
430         u16                             id;
431         u16                             num_clocks;
432         struct clk                      *clock[MAX_FIMC_CLOCKS];
433         void __iomem                    *regs;
434         struct resource                 *regs_res;
435         int                             irq;
436         wait_queue_head_t               irq_queue;
437         struct v4l2_device              *v4l2_dev;
438         struct fimc_m2m_device          m2m;
439         struct fimc_vid_cap             vid_cap;
440         unsigned long                   state;
441         struct vb2_alloc_ctx            *alloc_ctx;
442         struct fimc_pipeline            pipeline;
443 };
444
445 /**
446  * fimc_ctx - the device context data
447  * @slock:              spinlock protecting this data structure
448  * @s_frame:            source frame properties
449  * @d_frame:            destination frame properties
450  * @out_order_1p:       output 1-plane YCBCR order
451  * @out_order_2p:       output 2-plane YCBCR order
452  * @in_order_1p         input 1-plane YCBCR order
453  * @in_order_2p:        input 2-plane YCBCR order
454  * @in_path:            input mode (DMA or camera)
455  * @out_path:           output mode (DMA or FIFO)
456  * @scaler:             image scaler properties
457  * @effect:             image effect
458  * @rotation:           image clockwise rotation in degrees
459  * @hflip:              indicates image horizontal flip if set
460  * @vflip:              indicates image vertical flip if set
461  * @flags:              additional flags for image conversion
462  * @state:              flags to keep track of user configuration
463  * @fimc_dev:           the FIMC device this context applies to
464  * @m2m_ctx:            memory-to-memory device context
465  * @fh:                 v4l2 file handle
466  * @ctrl_handler:       v4l2 controls handler
467  * @ctrl_rotate         image rotation control
468  * @ctrl_hflip          horizontal flip control
469  * @ctrl_vflip          vartical flip control
470  * @ctrls_rdy:          true if the control handler is initialized
471  */
472 struct fimc_ctx {
473         spinlock_t              slock;
474         struct fimc_frame       s_frame;
475         struct fimc_frame       d_frame;
476         u32                     out_order_1p;
477         u32                     out_order_2p;
478         u32                     in_order_1p;
479         u32                     in_order_2p;
480         enum fimc_datapath      in_path;
481         enum fimc_datapath      out_path;
482         struct fimc_scaler      scaler;
483         struct fimc_effect      effect;
484         int                     rotation;
485         unsigned int            hflip:1;
486         unsigned int            vflip:1;
487         u32                     flags;
488         u32                     state;
489         struct fimc_dev         *fimc_dev;
490         struct v4l2_m2m_ctx     *m2m_ctx;
491         struct v4l2_fh          fh;
492         struct v4l2_ctrl_handler ctrl_handler;
493         struct v4l2_ctrl        *ctrl_rotate;
494         struct v4l2_ctrl        *ctrl_hflip;
495         struct v4l2_ctrl        *ctrl_vflip;
496         bool                    ctrls_rdy;
497 };
498
499 #define fh_to_ctx(__fh) container_of(__fh, struct fimc_ctx, fh)
500
501 static inline bool fimc_capture_active(struct fimc_dev *fimc)
502 {
503         unsigned long flags;
504         bool ret;
505
506         spin_lock_irqsave(&fimc->slock, flags);
507         ret = !!(fimc->state & (1 << ST_CAPT_RUN) ||
508                  fimc->state & (1 << ST_CAPT_PEND));
509         spin_unlock_irqrestore(&fimc->slock, flags);
510         return ret;
511 }
512
513 static inline void fimc_ctx_state_lock_set(u32 state, struct fimc_ctx *ctx)
514 {
515         unsigned long flags;
516
517         spin_lock_irqsave(&ctx->slock, flags);
518         ctx->state |= state;
519         spin_unlock_irqrestore(&ctx->slock, flags);
520 }
521
522 static inline bool fimc_ctx_state_is_set(u32 mask, struct fimc_ctx *ctx)
523 {
524         unsigned long flags;
525         bool ret;
526
527         spin_lock_irqsave(&ctx->slock, flags);
528         ret = (ctx->state & mask) == mask;
529         spin_unlock_irqrestore(&ctx->slock, flags);
530         return ret;
531 }
532
533 static inline int tiled_fmt(struct fimc_fmt *fmt)
534 {
535         return fmt->fourcc == V4L2_PIX_FMT_NV12MT;
536 }
537
538 static inline void fimc_hw_clear_irq(struct fimc_dev *dev)
539 {
540         u32 cfg = readl(dev->regs + S5P_CIGCTRL);
541         cfg |= S5P_CIGCTRL_IRQ_CLR;
542         writel(cfg, dev->regs + S5P_CIGCTRL);
543 }
544
545 static inline void fimc_hw_enable_scaler(struct fimc_dev *dev, bool on)
546 {
547         u32 cfg = readl(dev->regs + S5P_CISCCTRL);
548         if (on)
549                 cfg |= S5P_CISCCTRL_SCALERSTART;
550         else
551                 cfg &= ~S5P_CISCCTRL_SCALERSTART;
552         writel(cfg, dev->regs + S5P_CISCCTRL);
553 }
554
555 static inline void fimc_hw_activate_input_dma(struct fimc_dev *dev, bool on)
556 {
557         u32 cfg = readl(dev->regs + S5P_MSCTRL);
558         if (on)
559                 cfg |= S5P_MSCTRL_ENVID;
560         else
561                 cfg &= ~S5P_MSCTRL_ENVID;
562         writel(cfg, dev->regs + S5P_MSCTRL);
563 }
564
565 static inline void fimc_hw_dis_capture(struct fimc_dev *dev)
566 {
567         u32 cfg = readl(dev->regs + S5P_CIIMGCPT);
568         cfg &= ~(S5P_CIIMGCPT_IMGCPTEN | S5P_CIIMGCPT_IMGCPTEN_SC);
569         writel(cfg, dev->regs + S5P_CIIMGCPT);
570 }
571
572 /**
573  * fimc_hw_set_dma_seq - configure output DMA buffer sequence
574  * @mask: each bit corresponds to one of 32 output buffer registers set
575  *        1 to include buffer in the sequence, 0 to disable
576  *
577  * This function mask output DMA ring buffers, i.e. it allows to configure
578  * which of the output buffer address registers will be used by the DMA
579  * engine.
580  */
581 static inline void fimc_hw_set_dma_seq(struct fimc_dev *dev, u32 mask)
582 {
583         writel(mask, dev->regs + S5P_CIFCNTSEQ);
584 }
585
586 static inline struct fimc_frame *ctx_get_frame(struct fimc_ctx *ctx,
587                                                enum v4l2_buf_type type)
588 {
589         struct fimc_frame *frame;
590
591         if (V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == type) {
592                 if (fimc_ctx_state_is_set(FIMC_CTX_M2M, ctx))
593                         frame = &ctx->s_frame;
594                 else
595                         return ERR_PTR(-EINVAL);
596         } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
597                 frame = &ctx->d_frame;
598         } else {
599                 v4l2_err(ctx->fimc_dev->v4l2_dev,
600                         "Wrong buffer/video queue type (%d)\n", type);
601                 return ERR_PTR(-EINVAL);
602         }
603
604         return frame;
605 }
606
607 /* Return an index to the buffer actually being written. */
608 static inline u32 fimc_hw_get_frame_index(struct fimc_dev *dev)
609 {
610         u32 reg;
611
612         if (dev->variant->has_cistatus2) {
613                 reg = readl(dev->regs + S5P_CISTATUS2) & 0x3F;
614                 return reg > 0 ? --reg : reg;
615         } else {
616                 reg = readl(dev->regs + S5P_CISTATUS);
617                 return (reg & S5P_CISTATUS_FRAMECNT_MASK) >>
618                         S5P_CISTATUS_FRAMECNT_SHIFT;
619         }
620 }
621
622 /* -----------------------------------------------------*/
623 /* fimc-reg.c                                           */
624 void fimc_hw_reset(struct fimc_dev *fimc);
625 void fimc_hw_set_rotation(struct fimc_ctx *ctx);
626 void fimc_hw_set_target_format(struct fimc_ctx *ctx);
627 void fimc_hw_set_out_dma(struct fimc_ctx *ctx);
628 void fimc_hw_en_lastirq(struct fimc_dev *fimc, int enable);
629 void fimc_hw_en_irq(struct fimc_dev *fimc, int enable);
630 void fimc_hw_set_prescaler(struct fimc_ctx *ctx);
631 void fimc_hw_set_mainscaler(struct fimc_ctx *ctx);
632 void fimc_hw_en_capture(struct fimc_ctx *ctx);
633 void fimc_hw_set_effect(struct fimc_ctx *ctx);
634 void fimc_hw_set_in_dma(struct fimc_ctx *ctx);
635 void fimc_hw_set_input_path(struct fimc_ctx *ctx);
636 void fimc_hw_set_output_path(struct fimc_ctx *ctx);
637 void fimc_hw_set_input_addr(struct fimc_dev *fimc, struct fimc_addr *paddr);
638 void fimc_hw_set_output_addr(struct fimc_dev *fimc, struct fimc_addr *paddr,
639                              int index);
640 int fimc_hw_set_camera_source(struct fimc_dev *fimc,
641                               struct s5p_fimc_isp_info *cam);
642 int fimc_hw_set_camera_offset(struct fimc_dev *fimc, struct fimc_frame *f);
643 int fimc_hw_set_camera_polarity(struct fimc_dev *fimc,
644                                 struct s5p_fimc_isp_info *cam);
645 int fimc_hw_set_camera_type(struct fimc_dev *fimc,
646                             struct s5p_fimc_isp_info *cam);
647
648 /* -----------------------------------------------------*/
649 /* fimc-core.c */
650 int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
651                                 struct v4l2_fmtdesc *f);
652 int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr);
653 int fimc_ctrls_create(struct fimc_ctx *ctx);
654 void fimc_ctrls_delete(struct fimc_ctx *ctx);
655 void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active);
656 int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f);
657 void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
658                                struct v4l2_pix_format_mplane *pix);
659 struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
660                                   unsigned int mask, int index);
661
662 int fimc_check_scaler_ratio(int sw, int sh, int dw, int dh, int rot);
663 int fimc_set_scaler_info(struct fimc_ctx *ctx);
664 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags);
665 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
666                       struct fimc_frame *frame, struct fimc_addr *paddr);
667 void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f);
668 void fimc_set_yuv_order(struct fimc_ctx *ctx);
669 void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f);
670
671 int fimc_register_m2m_device(struct fimc_dev *fimc,
672                              struct v4l2_device *v4l2_dev);
673 void fimc_unregister_m2m_device(struct fimc_dev *fimc);
674 int fimc_register_driver(void);
675 void fimc_unregister_driver(void);
676
677 /* -----------------------------------------------------*/
678 /* fimc-capture.c                                       */
679 int fimc_register_capture_device(struct fimc_dev *fimc,
680                                  struct v4l2_device *v4l2_dev);
681 void fimc_unregister_capture_device(struct fimc_dev *fimc);
682 int fimc_capture_ctrls_create(struct fimc_dev *fimc);
683 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
684                              struct fimc_vid_buffer *fimc_vb);
685 int fimc_capture_suspend(struct fimc_dev *fimc);
686 int fimc_capture_resume(struct fimc_dev *fimc);
687
688 /* Locking: the caller holds fimc->slock */
689 static inline void fimc_activate_capture(struct fimc_ctx *ctx)
690 {
691         fimc_hw_enable_scaler(ctx->fimc_dev, ctx->scaler.enabled);
692         fimc_hw_en_capture(ctx);
693 }
694
695 static inline void fimc_deactivate_capture(struct fimc_dev *fimc)
696 {
697         fimc_hw_en_lastirq(fimc, true);
698         fimc_hw_dis_capture(fimc);
699         fimc_hw_enable_scaler(fimc, false);
700         fimc_hw_en_lastirq(fimc, false);
701 }
702
703 /*
704  * Add buf to the capture active buffers queue.
705  * Locking: Need to be called with fimc_dev::slock held.
706  */
707 static inline void active_queue_add(struct fimc_vid_cap *vid_cap,
708                                     struct fimc_vid_buffer *buf)
709 {
710         list_add_tail(&buf->list, &vid_cap->active_buf_q);
711         vid_cap->active_buf_cnt++;
712 }
713
714 /*
715  * Pop a video buffer from the capture active buffers queue
716  * Locking: Need to be called with fimc_dev::slock held.
717  */
718 static inline struct fimc_vid_buffer *
719 active_queue_pop(struct fimc_vid_cap *vid_cap)
720 {
721         struct fimc_vid_buffer *buf;
722         buf = list_entry(vid_cap->active_buf_q.next,
723                          struct fimc_vid_buffer, list);
724         list_del(&buf->list);
725         vid_cap->active_buf_cnt--;
726         return buf;
727 }
728
729 /* Add video buffer to the capture pending buffers queue */
730 static inline void fimc_pending_queue_add(struct fimc_vid_cap *vid_cap,
731                                           struct fimc_vid_buffer *buf)
732 {
733         list_add_tail(&buf->list, &vid_cap->pending_buf_q);
734 }
735
736 /* Add video buffer to the capture pending buffers queue */
737 static inline struct fimc_vid_buffer *
738 pending_queue_pop(struct fimc_vid_cap *vid_cap)
739 {
740         struct fimc_vid_buffer *buf;
741         buf = list_entry(vid_cap->pending_buf_q.next,
742                         struct fimc_vid_buffer, list);
743         list_del(&buf->list);
744         return buf;
745 }
746
747 #endif /* FIMC_CORE_H_ */