#ifndef CAMIF_CORE_H_
#define CAMIF_CORE_H_
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <media/media-entity.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mediabus.h>
#include <media/videobuf2-v4l2.h>
#include <media/drv-intf/s3c_camif.h>
#define S3C_CAMIF_DRIVER_NAME "s3c-camif"
#define CAMIF_REQ_BUFS_MIN 3
#define CAMIF_MAX_OUT_BUFS 4
#define CAMIF_MAX_PIX_WIDTH 4096
#define CAMIF_MAX_PIX_HEIGHT 4096
#define SCALER_MAX_RATIO 64
#define CAMIF_DEF_WIDTH 640
#define CAMIF_DEF_HEIGHT 480
#define CAMIF_STOP_TIMEOUT 1500 /* ms */
#define S3C244X_CAMIF_IP_REV 0x20 /* 2.0 */
#define S3C2450_CAMIF_IP_REV 0x30 /* 3.0 - not implemented, not tested */
#define S3C6400_CAMIF_IP_REV 0x31 /* 3.1 - not implemented, not tested */
#define S3C6410_CAMIF_IP_REV 0x32 /* 3.2 */
#define ST_VP_PENDING (1 << 0)
#define ST_VP_RUNNING (1 << 1)
#define ST_VP_STREAMING (1 << 2)
#define ST_VP_SENSOR_STREAMING (1 << 3)
#define ST_VP_ABORTING (1 << 4)
#define ST_VP_OFF (1 << 5)
#define ST_VP_LASTIRQ (1 << 6)
#define ST_VP_CONFIG (1 << 8)
#define CAMIF_SD_PAD_SINK 0
#define CAMIF_SD_PAD_SOURCE_C 1
#define CAMIF_SD_PAD_SOURCE_P 2
#define CAMIF_SD_PADS_NUM 3
enum img_fmt {
IMG_FMT_RGB565 = 0x0010,
IMG_FMT_RGB666,
IMG_FMT_XRGB8888,
IMG_FMT_YCBCR420 = 0x0020,
IMG_FMT_YCRCB420,
IMG_FMT_YCBCR422P,
IMG_FMT_YCBYCR422 = 0x0040,
IMG_FMT_YCRYCB422,
IMG_FMT_CBYCRY422,
IMG_FMT_CRYCBY422,
};
#define img_fmt_is_rgb(x) ((x) & 0x10)
#define img_fmt_is_ycbcr(x) ((x) & 0x60)
#define FMT_FL_S3C24XX_CODEC (1 << 0)
#define FMT_FL_S3C24XX_PREVIEW (1 << 1)
#define FMT_FL_S3C64XX (1 << 2)
struct camif_fmt {
u32 fourcc;
u32 color;
u16 colplanes;
u16 flags;
u8 depth;
u8 ybpp;
};
struct camif_dma_offset {
int initial;
int line;
};
struct camif_frame {
u16 f_width;
u16 f_height;
struct v4l2_rect rect;
struct camif_dma_offset dma_offset;
};
enum {
CLK_GATE,
CLK_CAM,
CLK_MAX_NUM,
};
struct vp_pix_limits {
u16 max_out_width;
u16 max_sc_out_width;
u16 out_width_align;
u16 max_height;
u8 min_out_width;
u16 out_hor_offset_align;
};
struct camif_pix_limits {
u16 win_hor_offset_align;
};
struct s3c_camif_variant {
struct vp_pix_limits vp_pix_limits[2];
struct camif_pix_limits pix_limits;
u8 ip_revision;
u8 has_img_effect;
unsigned int vp_offset;
};
struct s3c_camif_drvdata {
const struct s3c_camif_variant *variant;
unsigned long bus_clk_freq;
};
struct camif_scaler {
u8 scaleup_h;
u8 scaleup_v;
u8 copy;
u8 enable;
u32 h_shift;
u32 v_shift;
u32 pre_h_ratio;
u32 pre_v_ratio;
u32 pre_dst_width;
u32 pre_dst_height;
u32 main_h_ratio;
u32 main_v_ratio;
};
struct camif_dev;
struct camif_vp {
wait_queue_head_t irq_queue;
int irq;
struct camif_dev *camif;
struct media_pad pad;
struct video_device vdev;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_fh *owner;
struct vb2_queue vb_queue;
struct list_head pending_buf_q;
struct list_head active_buf_q;
unsigned int active_buffers;
unsigned int buf_index;
unsigned int frame_sequence;
unsigned int reqbufs_count;
struct camif_scaler scaler;
const struct camif_fmt *out_fmt;
unsigned int payload;
struct camif_frame out_frame;
unsigned int state;
u16 fmt_flags;
u8 id;
u16 rotation;
u8 hflip;
u8 vflip;
unsigned int offset;
};
#define VP_CODEC 0
#define VP_PREVIEW 1
#define CAMIF_VP_NUM 2
struct camif_dev {
struct media_device media_dev;
struct v4l2_device v4l2_dev;
struct v4l2_subdev subdev;
struct v4l2_mbus_framefmt mbus_fmt;
struct v4l2_rect camif_crop;
struct media_pad pads[CAMIF_SD_PADS_NUM];
int stream_count;
struct cam_sensor {
struct v4l2_subdev *sd;
short power_count;
short stream_count;
} sensor;
struct media_pipeline *m_pipeline;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_ctrl *ctrl_test_pattern;
struct {
struct v4l2_ctrl *ctrl_colorfx;
struct v4l2_ctrl *ctrl_colorfx_cbcr;
};
u8 test_pattern;
u8 colorfx;
u8 colorfx_cb;
u8 colorfx_cr;
struct camif_vp vp[CAMIF_VP_NUM];
const struct s3c_camif_variant *variant;
struct device *dev;
struct s3c_camif_plat_data pdata;
struct clk *clock[CLK_MAX_NUM];
struct mutex lock;
spinlock_t slock;
void __iomem *io_base;
};
struct camif_addr {
dma_addr_t y;
dma_addr_t cb;
dma_addr_t cr;
};
struct camif_buffer {
struct vb2_v4l2_buffer vb;
struct list_head list;
struct camif_addr paddr;
unsigned int index;
};
const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
const u32 *pixelformat, int index);
int s3c_camif_register_video_node(struct camif_dev *camif, int idx);
void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx);
irqreturn_t s3c_camif_irq_handler(int irq, void *priv);
int s3c_camif_create_subdev(struct camif_dev *camif);
void s3c_camif_unregister_subdev(struct camif_dev *camif);
int s3c_camif_set_defaults(struct camif_dev *camif);
int s3c_camif_get_scaler_config(struct camif_vp *vp,
struct camif_scaler *scaler);
static inline void camif_active_queue_add(struct camif_vp *vp,
struct camif_buffer *buf)
{
list_add_tail(&buf->list, &vp->active_buf_q);
vp->active_buffers++;
}
static inline struct camif_buffer *camif_active_queue_pop(
struct camif_vp *vp)
{
struct camif_buffer *buf = list_first_entry(&vp->active_buf_q,
struct camif_buffer, list);
list_del(&buf->list);
vp->active_buffers--;
return buf;
}
static inline struct camif_buffer *camif_active_queue_peek(
struct camif_vp *vp, int index)
{
struct camif_buffer *tmp, *buf;
if (WARN_ON(list_empty(&vp->active_buf_q)))
return NULL;
list_for_each_entry_safe(buf, tmp, &vp->active_buf_q, list) {
if (buf->index == index) {
list_del(&buf->list);
vp->active_buffers--;
return buf;
}
}
return NULL;
}
static inline void camif_pending_queue_add(struct camif_vp *vp,
struct camif_buffer *buf)
{
list_add_tail(&buf->list, &vp->pending_buf_q);
}
static inline struct camif_buffer *camif_pending_queue_pop(
struct camif_vp *vp)
{
struct camif_buffer *buf = list_first_entry(&vp->pending_buf_q,
struct camif_buffer, list);
list_del(&buf->list);
return buf;
}
#endif /* CAMIF_CORE_H_ */