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 <asm/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 err(fmt, args...) \
31 printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
33 #define dbg(fmt, args...) \
34 pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args)
36 /* Time to wait for next frame VSYNC interrupt while stopping operation. */
37 #define FIMC_SHUTDOWN_TIMEOUT ((100*HZ)/1000)
38 #define MAX_FIMC_CLOCKS 2
39 #define FIMC_MODULE_NAME "s5p-fimc"
40 #define FIMC_MAX_DEVS 4
41 #define FIMC_MAX_OUT_BUFS 4
42 #define SCALER_MAX_HRATIO 64
43 #define SCALER_MAX_VRATIO 64
44 #define DMA_MIN_SIZE 8
45 #define FIMC_CAMIF_MAX_HEIGHT 0x2000
47 /* indices to the clocks array */
72 #define fimc_m2m_active(dev) test_bit(ST_M2M_RUN, &(dev)->state)
73 #define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
75 #define fimc_capture_running(dev) test_bit(ST_CAPT_RUN, &(dev)->state)
76 #define fimc_capture_pending(dev) test_bit(ST_CAPT_PEND, &(dev)->state)
77 #define fimc_capture_busy(dev) test_bit(ST_CAPT_BUSY, &(dev)->state)
89 FIMC_FMT_RGB444 = 0x10,
95 FIMC_FMT_YCBCR420 = 0x20,
100 FIMC_FMT_YCBCR444_LOCAL,
101 FIMC_FMT_JPEG = 0x40,
102 FIMC_FMT_RAW8 = 0x80,
107 #define fimc_fmt_is_rgb(x) (!!((x) & 0x10))
108 #define fimc_fmt_is_jpeg(x) (!!((x) & 0x40))
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 * @flags: flags indicating which operation mode format applies to
148 enum v4l2_mbus_pixelcode mbus_code;
154 u8 depth[VIDEO_MAX_PLANES];
156 #define FMT_FLAGS_CAM (1 << 0)
157 #define FMT_FLAGS_M2M_IN (1 << 1)
158 #define FMT_FLAGS_M2M_OUT (1 << 2)
159 #define FMT_FLAGS_M2M (1 << 1 | 1 << 2)
160 #define FMT_HAS_ALPHA (1 << 3)
164 * struct fimc_dma_offset - pixel offset information for DMA
165 * @y_h: y value horizontal offset
166 * @y_v: y value vertical offset
167 * @cb_h: cb value horizontal offset
168 * @cb_v: cb value vertical offset
169 * @cr_h: cr value horizontal offset
170 * @cr_v: cr value vertical offset
172 struct fimc_dma_offset {
182 * struct fimc_effect - color effect information
184 * @pat_cb: cr value when type is "arbitrary"
185 * @pat_cr: cr value when type is "arbitrary"
194 * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
195 * @scaleup_h: flag indicating scaling up horizontally
196 * @scaleup_v: flag indicating scaling up vertically
197 * @copy_mode: flag indicating transparent DMA transfer (no scaling
198 * and color format conversion)
199 * @enabled: flag indicating if the scaler is used
200 * @hfactor: horizontal shift factor
201 * @vfactor: vertical shift factor
202 * @pre_hratio: horizontal ratio of the prescaler
203 * @pre_vratio: vertical ratio of the prescaler
204 * @pre_dst_width: the prescaler's destination width
205 * @pre_dst_height: the prescaler's destination height
206 * @main_hratio: the main scaler's horizontal ratio
207 * @main_vratio: the main scaler's vertical ratio
208 * @real_width: source pixel (width - offset)
209 * @real_height: source pixel (height - offset)
212 unsigned int scaleup_h:1;
213 unsigned int scaleup_v:1;
214 unsigned int copy_mode:1;
215 unsigned int enabled:1;
229 * struct fimc_addr - the FIMC physical address set for DMA
230 * @y: luminance plane physical address
231 * @cb: Cb plane physical address
232 * @cr: Cr plane physical address
241 * struct fimc_vid_buffer - the driver's video buffer
242 * @vb: v4l videobuf buffer
243 * @list: linked list structure for buffer queue
244 * @paddr: precalculated physical address set
245 * @index: buffer index for the output DMA engine
247 struct fimc_vid_buffer {
248 struct vb2_buffer vb;
249 struct list_head list;
250 struct fimc_addr paddr;
255 * struct fimc_frame - source/target frame properties
256 * @f_width: image full width (virtual screen size)
257 * @f_height: image full height (virtual screen size)
258 * @o_width: original image width as set by S_FMT
259 * @o_height: original image height as set by S_FMT
260 * @offs_h: image horizontal pixel offset
261 * @offs_v: image vertical pixel offset
262 * @width: image pixel width
263 * @height: image pixel weight
264 * @payload: image size in bytes (w x h x bpp)
265 * @paddr: image frame buffer physical addresses
266 * @dma_offset: DMA offset in bytes
267 * @fmt: fimc color format pointer
278 unsigned long payload[VIDEO_MAX_PLANES];
279 struct fimc_addr paddr;
280 struct fimc_dma_offset dma_offset;
281 struct fimc_fmt *fmt;
286 * struct fimc_m2m_device - v4l2 memory-to-memory device data
287 * @vfd: the video device node for v4l2 m2m mode
288 * @m2m_dev: v4l2 memory-to-memory device data
289 * @ctx: hardware context data
290 * @refcnt: the reference counter
292 struct fimc_m2m_device {
293 struct video_device *vfd;
294 struct v4l2_m2m_dev *m2m_dev;
295 struct fimc_ctx *ctx;
299 #define FIMC_SD_PAD_SINK 0
300 #define FIMC_SD_PAD_SOURCE 1
301 #define FIMC_SD_PADS_NUM 2
304 * struct fimc_vid_cap - camera capture device information
305 * @ctx: hardware context data
306 * @vfd: video device node for camera capture mode
307 * @subdev: subdev exposing the FIMC processing block
308 * @vd_pad: fimc video capture node pad
309 * @sd_pads: fimc video processing block pads
310 * @mf: media bus format at the FIMC camera input (and the scaler output) pad
311 * @pending_buf_q: the pending buffer queue head
312 * @active_buf_q: the queue head of buffers scheduled in hardware
313 * @vbq: the capture am video buffer queue
314 * @active_buf_cnt: number of video buffers scheduled in hardware
315 * @buf_index: index for managing the output DMA buffers
316 * @frame_count: the frame counter for statistics
317 * @reqbufs_count: the number of buffers requested in REQBUFS ioctl
318 * @input_index: input (camera sensor) index
319 * @refcnt: driver's private reference counter
320 * @input: capture input type, grp_id of the attached subdev
321 * @user_subdev_api: true if subdevs are not configured by the host driver
323 struct fimc_vid_cap {
324 struct fimc_ctx *ctx;
325 struct vb2_alloc_ctx *alloc_ctx;
326 struct video_device *vfd;
327 struct v4l2_subdev subdev;
328 struct media_pad vd_pad;
329 struct v4l2_mbus_framefmt mf;
330 struct media_pad sd_pads[FIMC_SD_PADS_NUM];
331 struct list_head pending_buf_q;
332 struct list_head active_buf_q;
333 struct vb2_queue vbq;
336 unsigned int frame_count;
337 unsigned int reqbufs_count;
341 bool user_subdev_api;
345 * struct fimc_pix_limit - image pixel size limits in various IP configurations
347 * @scaler_en_w: max input pixel width when the scaler is enabled
348 * @scaler_dis_w: max input pixel width when the scaler is disabled
349 * @in_rot_en_h: max input width with the input rotator is on
350 * @in_rot_dis_w: max input width with the input rotator is off
351 * @out_rot_en_w: max output width with the output rotator on
352 * @out_rot_dis_w: max output width with the output rotator off
354 struct fimc_pix_limit {
364 * struct fimc_variant - FIMC device variant information
365 * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
366 * @has_inp_rot: set if has input rotator
367 * @has_out_rot: set if has output rotator
368 * @has_cistatus2: 1 if CISTATUS2 register is present in this IP revision
369 * @has_mainscaler_ext: 1 if extended mainscaler ratios in CIEXTEN register
370 * are present in this IP revision
371 * @has_cam_if: set if this instance has a camera input interface
372 * @pix_limit: pixel size constraints for the scaler
373 * @min_inp_pixsize: minimum input pixel size
374 * @min_out_pixsize: minimum output pixel size
375 * @hor_offs_align: horizontal pixel offset aligment
376 * @min_vsize_align: minimum vertical pixel size alignment
377 * @out_buf_count: the number of buffers in output DMA sequence
379 struct fimc_variant {
380 unsigned int pix_hoff:1;
381 unsigned int has_inp_rot:1;
382 unsigned int has_out_rot:1;
383 unsigned int has_cistatus2:1;
384 unsigned int has_mainscaler_ext:1;
385 unsigned int has_cam_if:1;
386 unsigned int has_alpha:1;
387 struct fimc_pix_limit *pix_limit;
396 * struct fimc_drvdata - per device type driver data
397 * @variant: variant information for this device
398 * @num_entities: number of fimc instances available in a SoC
399 * @lclk_frequency: local bus clock frequency
401 struct fimc_drvdata {
402 struct fimc_variant *variant[FIMC_MAX_DEVS];
404 unsigned long lclk_frequency;
407 #define fimc_get_drvdata(_pdev) \
408 ((struct fimc_drvdata *) platform_get_device_id(_pdev)->driver_data)
413 * struct fimc_dev - abstraction for FIMC entity
414 * @slock: the spinlock protecting this data structure
415 * @lock: the mutex protecting this data structure
416 * @pdev: pointer to the FIMC platform device
417 * @pdata: pointer to the device platform data
418 * @variant: the IP variant information
419 * @id: FIMC device index (0..FIMC_MAX_DEVS)
420 * @clock: clocks required for FIMC operation
421 * @regs: the mapped hardware registers
422 * @irq_queue: interrupt handler waitqueue
423 * @v4l2_dev: root v4l2_device
424 * @m2m: memory-to-memory V4L2 device information
425 * @vid_cap: camera capture device information
426 * @state: flags used to synchronize m2m and capture mode operation
427 * @alloc_ctx: videobuf2 memory allocator context
428 * @pipeline: fimc video capture pipeline data structure
433 struct platform_device *pdev;
434 struct s5p_platform_fimc *pdata;
435 struct fimc_variant *variant;
437 struct clk *clock[MAX_FIMC_CLOCKS];
439 wait_queue_head_t irq_queue;
440 struct v4l2_device *v4l2_dev;
441 struct fimc_m2m_device m2m;
442 struct fimc_vid_cap vid_cap;
444 struct vb2_alloc_ctx *alloc_ctx;
445 struct fimc_pipeline pipeline;
449 * struct fimc_ctrls - v4l2 controls structure
450 * @handler: the control handler
451 * @colorfx: image effect control
452 * @colorfx_cbcr: Cb/Cr coefficients control
453 * @rotate: image rotation control
454 * @hflip: horizontal flip control
455 * @vflip: vertical flip control
456 * @alpha: RGB alpha control
457 * @ready: true if @handler is initialized
460 struct v4l2_ctrl_handler handler;
462 struct v4l2_ctrl *colorfx;
463 struct v4l2_ctrl *colorfx_cbcr;
465 struct v4l2_ctrl *rotate;
466 struct v4l2_ctrl *hflip;
467 struct v4l2_ctrl *vflip;
468 struct v4l2_ctrl *alpha;
473 * fimc_ctx - the device context data
474 * @s_frame: source frame properties
475 * @d_frame: destination frame properties
476 * @out_order_1p: output 1-plane YCBCR order
477 * @out_order_2p: output 2-plane YCBCR order
478 * @in_order_1p input 1-plane YCBCR order
479 * @in_order_2p: input 2-plane YCBCR order
480 * @in_path: input mode (DMA or camera)
481 * @out_path: output mode (DMA or FIFO)
482 * @scaler: image scaler properties
483 * @effect: image effect
484 * @rotation: image clockwise rotation in degrees
485 * @hflip: indicates image horizontal flip if set
486 * @vflip: indicates image vertical flip if set
487 * @flags: additional flags for image conversion
488 * @state: flags to keep track of user configuration
489 * @fimc_dev: the FIMC device this context applies to
490 * @m2m_ctx: memory-to-memory device context
491 * @fh: v4l2 file handle
492 * @ctrls: v4l2 controls structure
495 struct fimc_frame s_frame;
496 struct fimc_frame d_frame;
501 enum fimc_datapath in_path;
502 enum fimc_datapath out_path;
503 struct fimc_scaler scaler;
504 struct fimc_effect effect;
506 unsigned int hflip:1;
507 unsigned int vflip:1;
510 struct fimc_dev *fimc_dev;
511 struct v4l2_m2m_ctx *m2m_ctx;
513 struct fimc_ctrls ctrls;
516 #define fh_to_ctx(__fh) container_of(__fh, struct fimc_ctx, fh)
518 static inline void set_frame_bounds(struct fimc_frame *f, u32 width, u32 height)
521 f->o_height = height;
523 f->f_height = height;
526 static inline void set_frame_crop(struct fimc_frame *f,
527 u32 left, u32 top, u32 width, u32 height)
535 static inline u32 fimc_get_format_depth(struct fimc_fmt *ff)
540 for (i = 0; i < ff->colplanes; i++)
541 depth += ff->depth[i];
545 static inline bool fimc_capture_active(struct fimc_dev *fimc)
550 spin_lock_irqsave(&fimc->slock, flags);
551 ret = !!(fimc->state & (1 << ST_CAPT_RUN) ||
552 fimc->state & (1 << ST_CAPT_PEND));
553 spin_unlock_irqrestore(&fimc->slock, flags);
557 static inline void fimc_ctx_state_set(u32 state, struct fimc_ctx *ctx)
561 spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
563 spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
566 static inline bool fimc_ctx_state_is_set(u32 mask, struct fimc_ctx *ctx)
571 spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
572 ret = (ctx->state & mask) == mask;
573 spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
577 static inline int tiled_fmt(struct fimc_fmt *fmt)
579 return fmt->fourcc == V4L2_PIX_FMT_NV12MT;
582 /* Return the alpha component bit mask */
583 static inline int fimc_get_alpha_mask(struct fimc_fmt *fmt)
585 switch (fmt->color) {
586 case FIMC_FMT_RGB444: return 0x0f;
587 case FIMC_FMT_RGB555: return 0x01;
588 case FIMC_FMT_RGB888: return 0xff;
593 static inline struct fimc_frame *ctx_get_frame(struct fimc_ctx *ctx,
594 enum v4l2_buf_type type)
596 struct fimc_frame *frame;
598 if (V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == type) {
599 if (fimc_ctx_state_is_set(FIMC_CTX_M2M, ctx))
600 frame = &ctx->s_frame;
602 return ERR_PTR(-EINVAL);
603 } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
604 frame = &ctx->d_frame;
606 v4l2_err(ctx->fimc_dev->v4l2_dev,
607 "Wrong buffer/video queue type (%d)\n", type);
608 return ERR_PTR(-EINVAL);
614 /* -----------------------------------------------------*/
616 int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
617 struct v4l2_fmtdesc *f);
618 int fimc_ctrls_create(struct fimc_ctx *ctx);
619 void fimc_ctrls_delete(struct fimc_ctx *ctx);
620 void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active);
621 void fimc_alpha_ctrl_update(struct fimc_ctx *ctx);
622 int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f);
623 void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
624 struct v4l2_pix_format_mplane *pix);
625 struct fimc_fmt *fimc_find_format(const u32 *pixelformat, const u32 *mbus_code,
626 unsigned int mask, int index);
627 struct fimc_fmt *fimc_get_format(unsigned int index);
629 int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
630 int dw, int dh, int rotation);
631 int fimc_set_scaler_info(struct fimc_ctx *ctx);
632 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags);
633 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
634 struct fimc_frame *frame, struct fimc_addr *paddr);
635 void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f);
636 void fimc_set_yuv_order(struct fimc_ctx *ctx);
637 void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f);
638 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf);
640 int fimc_register_m2m_device(struct fimc_dev *fimc,
641 struct v4l2_device *v4l2_dev);
642 void fimc_unregister_m2m_device(struct fimc_dev *fimc);
643 int fimc_register_driver(void);
644 void fimc_unregister_driver(void);
646 /* -----------------------------------------------------*/
648 void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state);
650 /* -----------------------------------------------------*/
652 int fimc_initialize_capture_subdev(struct fimc_dev *fimc);
653 void fimc_unregister_capture_subdev(struct fimc_dev *fimc);
654 int fimc_capture_ctrls_create(struct fimc_dev *fimc);
655 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
657 int fimc_capture_suspend(struct fimc_dev *fimc);
658 int fimc_capture_resume(struct fimc_dev *fimc);
661 * Buffer list manipulation functions. Must be called with fimc.slock held.
665 * fimc_active_queue_add - add buffer to the capture active buffers queue
666 * @buf: buffer to add to the active buffers list
668 static inline void fimc_active_queue_add(struct fimc_vid_cap *vid_cap,
669 struct fimc_vid_buffer *buf)
671 list_add_tail(&buf->list, &vid_cap->active_buf_q);
672 vid_cap->active_buf_cnt++;
676 * fimc_active_queue_pop - pop buffer from the capture active buffers queue
678 * The caller must assure the active_buf_q list is not empty.
680 static inline struct fimc_vid_buffer *fimc_active_queue_pop(
681 struct fimc_vid_cap *vid_cap)
683 struct fimc_vid_buffer *buf;
684 buf = list_entry(vid_cap->active_buf_q.next,
685 struct fimc_vid_buffer, list);
686 list_del(&buf->list);
687 vid_cap->active_buf_cnt--;
692 * fimc_pending_queue_add - add buffer to the capture pending buffers queue
693 * @buf: buffer to add to the pending buffers list
695 static inline void fimc_pending_queue_add(struct fimc_vid_cap *vid_cap,
696 struct fimc_vid_buffer *buf)
698 list_add_tail(&buf->list, &vid_cap->pending_buf_q);
702 * fimc_pending_queue_pop - pop buffer from the capture pending buffers queue
704 * The caller must assure the pending_buf_q list is not empty.
706 static inline struct fimc_vid_buffer *fimc_pending_queue_pop(
707 struct fimc_vid_cap *vid_cap)
709 struct fimc_vid_buffer *buf;
710 buf = list_entry(vid_cap->pending_buf_q.next,
711 struct fimc_vid_buffer, list);
712 list_del(&buf->list);
716 #endif /* FIMC_CORE_H_ */