2 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
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.
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>
20 #include <linux/sizes.h>
22 #include <media/media-entity.h>
23 #include <media/videobuf2-core.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-mem2mem.h>
27 #include <media/v4l2-mediabus.h>
28 #include <media/s5p_fimc.h>
30 #define dbg(fmt, args...) \
31 pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args)
33 /* Time to wait for next frame VSYNC interrupt while stopping operation. */
34 #define FIMC_SHUTDOWN_TIMEOUT ((100*HZ)/1000)
35 #define MAX_FIMC_CLOCKS 2
36 #define FIMC_MODULE_NAME "s5p-fimc"
37 #define FIMC_MAX_DEVS 4
38 #define FIMC_MAX_OUT_BUFS 4
39 #define SCALER_MAX_HRATIO 64
40 #define SCALER_MAX_VRATIO 64
41 #define DMA_MIN_SIZE 8
42 #define FIMC_CAMIF_MAX_HEIGHT 0x2000
43 #define FIMC_MAX_JPEG_BUF_SIZE (10 * SZ_1M)
44 #define FIMC_MAX_PLANES 3
46 /* indices to the clocks array */
71 #define fimc_m2m_active(dev) test_bit(ST_M2M_RUN, &(dev)->state)
72 #define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
74 #define fimc_capture_running(dev) test_bit(ST_CAPT_RUN, &(dev)->state)
75 #define fimc_capture_pending(dev) test_bit(ST_CAPT_PEND, &(dev)->state)
76 #define fimc_capture_busy(dev) test_bit(ST_CAPT_BUSY, &(dev)->state)
88 FIMC_FMT_RGB444 = 0x10,
94 FIMC_FMT_YCBCR420 = 0x20,
99 FIMC_FMT_YCBCR444_LOCAL,
100 FIMC_FMT_RAW8 = 0x40,
103 FIMC_FMT_JPEG = 0x80,
104 FIMC_FMT_YUYV_JPEG = 0x100,
107 #define fimc_fmt_is_user_defined(x) (!!((x) & 0x180))
108 #define fimc_fmt_is_rgb(x) (!!((x) & 0x10))
110 #define IS_M2M(__strt) ((__strt) == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || \
111 __strt == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
113 /* The hardware context state. */
114 #define FIMC_PARAMS (1 << 0)
115 #define FIMC_SRC_FMT (1 << 3)
116 #define FIMC_DST_FMT (1 << 4)
117 #define FIMC_COMPOSE (1 << 5)
118 #define FIMC_CTX_M2M (1 << 16)
119 #define FIMC_CTX_CAP (1 << 17)
120 #define FIMC_CTX_SHUT (1 << 18)
122 /* Image conversion flags */
123 #define FIMC_IN_DMA_ACCESS_TILED (1 << 0)
124 #define FIMC_IN_DMA_ACCESS_LINEAR (0 << 0)
125 #define FIMC_OUT_DMA_ACCESS_TILED (1 << 1)
126 #define FIMC_OUT_DMA_ACCESS_LINEAR (0 << 1)
127 #define FIMC_SCAN_MODE_PROGRESSIVE (0 << 2)
128 #define FIMC_SCAN_MODE_INTERLACED (1 << 2)
130 * YCbCr data dynamic range for RGB-YUV color conversion.
131 * Y/Cb/Cr: (0 ~ 255) */
132 #define FIMC_COLOR_RANGE_WIDE (0 << 3)
133 /* Y (16 ~ 235), Cb/Cr (16 ~ 240) */
134 #define FIMC_COLOR_RANGE_NARROW (1 << 3)
137 * struct fimc_fmt - the driver's internal color format data
138 * @mbus_code: Media Bus pixel code, -1 if not applicable
139 * @name: format description
140 * @fourcc: the fourcc code for this format, 0 if not applicable
141 * @color: the corresponding fimc_color_fmt
142 * @memplanes: number of physically non-contiguous data planes
143 * @colplanes: number of physically contiguous data planes
144 * @depth: per plane driver's private 'number of bits per pixel'
145 * @mdataplanes: bitmask indicating meta data plane(s), (1 << plane_no)
146 * @flags: flags indicating which operation mode format applies to
149 enum v4l2_mbus_pixelcode mbus_code;
155 u8 depth[VIDEO_MAX_PLANES];
158 #define FMT_FLAGS_CAM (1 << 0)
159 #define FMT_FLAGS_M2M_IN (1 << 1)
160 #define FMT_FLAGS_M2M_OUT (1 << 2)
161 #define FMT_FLAGS_M2M (1 << 1 | 1 << 2)
162 #define FMT_HAS_ALPHA (1 << 3)
163 #define FMT_FLAGS_COMPRESSED (1 << 4)
167 * struct fimc_dma_offset - pixel offset information for DMA
168 * @y_h: y value horizontal offset
169 * @y_v: y value vertical offset
170 * @cb_h: cb value horizontal offset
171 * @cb_v: cb value vertical offset
172 * @cr_h: cr value horizontal offset
173 * @cr_v: cr value vertical offset
175 struct fimc_dma_offset {
185 * struct fimc_effect - color effect information
187 * @pat_cb: cr value when type is "arbitrary"
188 * @pat_cr: cr value when type is "arbitrary"
197 * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
198 * @scaleup_h: flag indicating scaling up horizontally
199 * @scaleup_v: flag indicating scaling up vertically
200 * @copy_mode: flag indicating transparent DMA transfer (no scaling
201 * and color format conversion)
202 * @enabled: flag indicating if the scaler is used
203 * @hfactor: horizontal shift factor
204 * @vfactor: vertical shift factor
205 * @pre_hratio: horizontal ratio of the prescaler
206 * @pre_vratio: vertical ratio of the prescaler
207 * @pre_dst_width: the prescaler's destination width
208 * @pre_dst_height: the prescaler's destination height
209 * @main_hratio: the main scaler's horizontal ratio
210 * @main_vratio: the main scaler's vertical ratio
211 * @real_width: source pixel (width - offset)
212 * @real_height: source pixel (height - offset)
215 unsigned int scaleup_h:1;
216 unsigned int scaleup_v:1;
217 unsigned int copy_mode:1;
218 unsigned int enabled:1;
232 * struct fimc_addr - the FIMC physical address set for DMA
233 * @y: luminance plane physical address
234 * @cb: Cb plane physical address
235 * @cr: Cr plane physical address
244 * struct fimc_vid_buffer - the driver's video buffer
245 * @vb: v4l videobuf buffer
246 * @list: linked list structure for buffer queue
247 * @paddr: precalculated physical address set
248 * @index: buffer index for the output DMA engine
250 struct fimc_vid_buffer {
251 struct vb2_buffer vb;
252 struct list_head list;
253 struct fimc_addr paddr;
258 * struct fimc_frame - source/target frame properties
259 * @f_width: image full width (virtual screen size)
260 * @f_height: image full height (virtual screen size)
261 * @o_width: original image width as set by S_FMT
262 * @o_height: original image height as set by S_FMT
263 * @offs_h: image horizontal pixel offset
264 * @offs_v: image vertical pixel offset
265 * @width: image pixel width
266 * @height: image pixel weight
267 * @payload: image size in bytes (w x h x bpp)
268 * @paddr: image frame buffer physical addresses
269 * @dma_offset: DMA offset in bytes
270 * @fmt: fimc color format pointer
281 unsigned int payload[VIDEO_MAX_PLANES];
282 struct fimc_addr paddr;
283 struct fimc_dma_offset dma_offset;
284 struct fimc_fmt *fmt;
289 * struct fimc_m2m_device - v4l2 memory-to-memory device data
290 * @vfd: the video device node for v4l2 m2m mode
291 * @m2m_dev: v4l2 memory-to-memory device data
292 * @ctx: hardware context data
293 * @refcnt: the reference counter
295 struct fimc_m2m_device {
296 struct video_device vfd;
297 struct v4l2_m2m_dev *m2m_dev;
298 struct fimc_ctx *ctx;
302 #define FIMC_SD_PAD_SINK 0
303 #define FIMC_SD_PAD_SOURCE 1
304 #define FIMC_SD_PADS_NUM 2
307 * struct fimc_vid_cap - camera capture device information
308 * @ctx: hardware context data
309 * @vfd: video device node for camera capture mode
310 * @subdev: subdev exposing the FIMC processing block
311 * @vd_pad: fimc video capture node pad
312 * @sd_pads: fimc video processing block pads
313 * @mf: media bus format at the FIMC camera input (and the scaler output) pad
314 * @pending_buf_q: the pending buffer queue head
315 * @active_buf_q: the queue head of buffers scheduled in hardware
316 * @vbq: the capture am video buffer queue
317 * @active_buf_cnt: number of video buffers scheduled in hardware
318 * @buf_index: index for managing the output DMA buffers
319 * @frame_count: the frame counter for statistics
320 * @reqbufs_count: the number of buffers requested in REQBUFS ioctl
321 * @input_index: input (camera sensor) index
322 * @refcnt: driver's private reference counter
323 * @input: capture input type, grp_id of the attached subdev
324 * @user_subdev_api: true if subdevs are not configured by the host driver
326 struct fimc_vid_cap {
327 struct fimc_ctx *ctx;
328 struct vb2_alloc_ctx *alloc_ctx;
329 struct video_device vfd;
330 struct v4l2_subdev subdev;
331 struct media_pad vd_pad;
332 struct v4l2_mbus_framefmt mf;
333 struct media_pad sd_pads[FIMC_SD_PADS_NUM];
334 struct list_head pending_buf_q;
335 struct list_head active_buf_q;
336 struct vb2_queue vbq;
339 unsigned int frame_count;
340 unsigned int reqbufs_count;
344 bool user_subdev_api;
348 * struct fimc_pix_limit - image pixel size limits in various IP configurations
350 * @scaler_en_w: max input pixel width when the scaler is enabled
351 * @scaler_dis_w: max input pixel width when the scaler is disabled
352 * @in_rot_en_h: max input width with the input rotator is on
353 * @in_rot_dis_w: max input width with the input rotator is off
354 * @out_rot_en_w: max output width with the output rotator on
355 * @out_rot_dis_w: max output width with the output rotator off
357 struct fimc_pix_limit {
367 * struct fimc_variant - FIMC device variant information
368 * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
369 * @has_inp_rot: set if has input rotator
370 * @has_out_rot: set if has output rotator
371 * @has_cistatus2: 1 if CISTATUS2 register is present in this IP revision
372 * @has_mainscaler_ext: 1 if extended mainscaler ratios in CIEXTEN register
373 * are present in this IP revision
374 * @has_cam_if: set if this instance has a camera input interface
375 * @pix_limit: pixel size constraints for the scaler
376 * @min_inp_pixsize: minimum input pixel size
377 * @min_out_pixsize: minimum output pixel size
378 * @hor_offs_align: horizontal pixel offset aligment
379 * @min_vsize_align: minimum vertical pixel size alignment
380 * @out_buf_count: the number of buffers in output DMA sequence
382 struct fimc_variant {
383 unsigned int pix_hoff:1;
384 unsigned int has_inp_rot:1;
385 unsigned int has_out_rot:1;
386 unsigned int has_cistatus2:1;
387 unsigned int has_mainscaler_ext:1;
388 unsigned int has_cam_if:1;
389 unsigned int has_alpha:1;
390 struct fimc_pix_limit *pix_limit;
399 * struct fimc_drvdata - per device type driver data
400 * @variant: variant information for this device
401 * @num_entities: number of fimc instances available in a SoC
402 * @lclk_frequency: local bus clock frequency
404 struct fimc_drvdata {
405 struct fimc_variant *variant[FIMC_MAX_DEVS];
407 unsigned long lclk_frequency;
410 #define fimc_get_drvdata(_pdev) \
411 ((struct fimc_drvdata *) platform_get_device_id(_pdev)->driver_data)
416 * struct fimc_dev - abstraction for FIMC entity
417 * @slock: the spinlock protecting this data structure
418 * @lock: the mutex protecting this data structure
419 * @pdev: pointer to the FIMC platform device
420 * @pdata: pointer to the device platform data
421 * @variant: the IP variant information
422 * @id: FIMC device index (0..FIMC_MAX_DEVS)
423 * @clock: clocks required for FIMC operation
424 * @regs: the mapped hardware registers
425 * @irq_queue: interrupt handler waitqueue
426 * @v4l2_dev: root v4l2_device
427 * @m2m: memory-to-memory V4L2 device information
428 * @vid_cap: camera capture device information
429 * @state: flags used to synchronize m2m and capture mode operation
430 * @alloc_ctx: videobuf2 memory allocator context
431 * @pipeline: fimc video capture pipeline data structure
436 struct platform_device *pdev;
437 struct s5p_platform_fimc *pdata;
438 struct fimc_variant *variant;
440 struct clk *clock[MAX_FIMC_CLOCKS];
442 wait_queue_head_t irq_queue;
443 struct v4l2_device *v4l2_dev;
444 struct fimc_m2m_device m2m;
445 struct fimc_vid_cap vid_cap;
447 struct vb2_alloc_ctx *alloc_ctx;
448 struct fimc_pipeline pipeline;
449 const struct fimc_pipeline_ops *pipeline_ops;
453 * struct fimc_ctrls - v4l2 controls structure
454 * @handler: the control handler
455 * @colorfx: image effect control
456 * @colorfx_cbcr: Cb/Cr coefficients control
457 * @rotate: image rotation control
458 * @hflip: horizontal flip control
459 * @vflip: vertical flip control
460 * @alpha: RGB alpha control
461 * @ready: true if @handler is initialized
464 struct v4l2_ctrl_handler handler;
466 struct v4l2_ctrl *colorfx;
467 struct v4l2_ctrl *colorfx_cbcr;
469 struct v4l2_ctrl *rotate;
470 struct v4l2_ctrl *hflip;
471 struct v4l2_ctrl *vflip;
472 struct v4l2_ctrl *alpha;
477 * fimc_ctx - the device context data
478 * @s_frame: source frame properties
479 * @d_frame: destination frame properties
480 * @out_order_1p: output 1-plane YCBCR order
481 * @out_order_2p: output 2-plane YCBCR order
482 * @in_order_1p input 1-plane YCBCR order
483 * @in_order_2p: input 2-plane YCBCR order
484 * @in_path: input mode (DMA or camera)
485 * @out_path: output mode (DMA or FIFO)
486 * @scaler: image scaler properties
487 * @effect: image effect
488 * @rotation: image clockwise rotation in degrees
489 * @hflip: indicates image horizontal flip if set
490 * @vflip: indicates image vertical flip if set
491 * @flags: additional flags for image conversion
492 * @state: flags to keep track of user configuration
493 * @fimc_dev: the FIMC device this context applies to
494 * @m2m_ctx: memory-to-memory device context
495 * @fh: v4l2 file handle
496 * @ctrls: v4l2 controls structure
499 struct fimc_frame s_frame;
500 struct fimc_frame d_frame;
505 enum fimc_datapath in_path;
506 enum fimc_datapath out_path;
507 struct fimc_scaler scaler;
508 struct fimc_effect effect;
510 unsigned int hflip:1;
511 unsigned int vflip:1;
514 struct fimc_dev *fimc_dev;
515 struct v4l2_m2m_ctx *m2m_ctx;
517 struct fimc_ctrls ctrls;
520 #define fh_to_ctx(__fh) container_of(__fh, struct fimc_ctx, fh)
522 static inline void set_frame_bounds(struct fimc_frame *f, u32 width, u32 height)
525 f->o_height = height;
527 f->f_height = height;
530 static inline void set_frame_crop(struct fimc_frame *f,
531 u32 left, u32 top, u32 width, u32 height)
539 static inline u32 fimc_get_format_depth(struct fimc_fmt *ff)
544 for (i = 0; i < ff->colplanes; i++)
545 depth += ff->depth[i];
549 static inline bool fimc_capture_active(struct fimc_dev *fimc)
554 spin_lock_irqsave(&fimc->slock, flags);
555 ret = !!(fimc->state & (1 << ST_CAPT_RUN) ||
556 fimc->state & (1 << ST_CAPT_PEND));
557 spin_unlock_irqrestore(&fimc->slock, flags);
561 static inline void fimc_ctx_state_set(u32 state, struct fimc_ctx *ctx)
565 spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
567 spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
570 static inline bool fimc_ctx_state_is_set(u32 mask, struct fimc_ctx *ctx)
575 spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
576 ret = (ctx->state & mask) == mask;
577 spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
581 static inline int tiled_fmt(struct fimc_fmt *fmt)
583 return fmt->fourcc == V4L2_PIX_FMT_NV12MT;
586 static inline bool fimc_jpeg_fourcc(u32 pixelformat)
588 return (pixelformat == V4L2_PIX_FMT_JPEG ||
589 pixelformat == V4L2_PIX_FMT_S5C_UYVY_JPG);
592 static inline bool fimc_user_defined_mbus_fmt(u32 code)
594 return (code == V4L2_MBUS_FMT_JPEG_1X8 ||
595 code == V4L2_MBUS_FMT_S5C_UYVY_JPEG_1X8);
598 /* Return the alpha component bit mask */
599 static inline int fimc_get_alpha_mask(struct fimc_fmt *fmt)
601 switch (fmt->color) {
602 case FIMC_FMT_RGB444: return 0x0f;
603 case FIMC_FMT_RGB555: return 0x01;
604 case FIMC_FMT_RGB888: return 0xff;
609 static inline struct fimc_frame *ctx_get_frame(struct fimc_ctx *ctx,
610 enum v4l2_buf_type type)
612 struct fimc_frame *frame;
614 if (V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == type) {
615 if (fimc_ctx_state_is_set(FIMC_CTX_M2M, ctx))
616 frame = &ctx->s_frame;
618 return ERR_PTR(-EINVAL);
619 } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
620 frame = &ctx->d_frame;
622 v4l2_err(ctx->fimc_dev->v4l2_dev,
623 "Wrong buffer/video queue type (%d)\n", type);
624 return ERR_PTR(-EINVAL);
630 /* -----------------------------------------------------*/
632 int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
633 struct v4l2_fmtdesc *f);
634 int fimc_ctrls_create(struct fimc_ctx *ctx);
635 void fimc_ctrls_delete(struct fimc_ctx *ctx);
636 void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active);
637 void fimc_alpha_ctrl_update(struct fimc_ctx *ctx);
638 int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f);
639 void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
640 struct v4l2_pix_format_mplane *pix);
641 struct fimc_fmt *fimc_find_format(const u32 *pixelformat, const u32 *mbus_code,
642 unsigned int mask, int index);
643 struct fimc_fmt *fimc_get_format(unsigned int index);
645 int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
646 int dw, int dh, int rotation);
647 int fimc_set_scaler_info(struct fimc_ctx *ctx);
648 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags);
649 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
650 struct fimc_frame *frame, struct fimc_addr *paddr);
651 void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f);
652 void fimc_set_yuv_order(struct fimc_ctx *ctx);
653 void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f);
654 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf);
656 int fimc_register_m2m_device(struct fimc_dev *fimc,
657 struct v4l2_device *v4l2_dev);
658 void fimc_unregister_m2m_device(struct fimc_dev *fimc);
659 int fimc_register_driver(void);
660 void fimc_unregister_driver(void);
662 /* -----------------------------------------------------*/
664 void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state);
666 /* -----------------------------------------------------*/
668 int fimc_initialize_capture_subdev(struct fimc_dev *fimc);
669 void fimc_unregister_capture_subdev(struct fimc_dev *fimc);
670 int fimc_capture_ctrls_create(struct fimc_dev *fimc);
671 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
673 int fimc_capture_suspend(struct fimc_dev *fimc);
674 int fimc_capture_resume(struct fimc_dev *fimc);
677 * Buffer list manipulation functions. Must be called with fimc.slock held.
681 * fimc_active_queue_add - add buffer to the capture active buffers queue
682 * @buf: buffer to add to the active buffers list
684 static inline void fimc_active_queue_add(struct fimc_vid_cap *vid_cap,
685 struct fimc_vid_buffer *buf)
687 list_add_tail(&buf->list, &vid_cap->active_buf_q);
688 vid_cap->active_buf_cnt++;
692 * fimc_active_queue_pop - pop buffer from the capture active buffers queue
694 * The caller must assure the active_buf_q list is not empty.
696 static inline struct fimc_vid_buffer *fimc_active_queue_pop(
697 struct fimc_vid_cap *vid_cap)
699 struct fimc_vid_buffer *buf;
700 buf = list_entry(vid_cap->active_buf_q.next,
701 struct fimc_vid_buffer, list);
702 list_del(&buf->list);
703 vid_cap->active_buf_cnt--;
708 * fimc_pending_queue_add - add buffer to the capture pending buffers queue
709 * @buf: buffer to add to the pending buffers list
711 static inline void fimc_pending_queue_add(struct fimc_vid_cap *vid_cap,
712 struct fimc_vid_buffer *buf)
714 list_add_tail(&buf->list, &vid_cap->pending_buf_q);
718 * fimc_pending_queue_pop - pop buffer from the capture pending buffers queue
720 * The caller must assure the pending_buf_q list is not empty.
722 static inline struct fimc_vid_buffer *fimc_pending_queue_pop(
723 struct fimc_vid_cap *vid_cap)
725 struct fimc_vid_buffer *buf;
726 buf = list_entry(vid_cap->pending_buf_q.next,
727 struct fimc_vid_buffer, list);
728 list_del(&buf->list);
732 #endif /* FIMC_CORE_H_ */