2 * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
4 * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
5 * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
16 #include <linux/irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/sched.h>
19 #include <linux/spinlock.h>
20 #include <linux/types.h>
21 #include <linux/videodev2.h>
23 #include <media/media-entity.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-mediabus.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/drv-intf/s3c_camif.h>
31 #define S3C_CAMIF_DRIVER_NAME "s3c-camif"
32 #define CAMIF_REQ_BUFS_MIN 3
33 #define CAMIF_MAX_OUT_BUFS 4
34 #define CAMIF_MAX_PIX_WIDTH 4096
35 #define CAMIF_MAX_PIX_HEIGHT 4096
36 #define SCALER_MAX_RATIO 64
37 #define CAMIF_DEF_WIDTH 640
38 #define CAMIF_DEF_HEIGHT 480
39 #define CAMIF_STOP_TIMEOUT 1500 /* ms */
41 #define S3C244X_CAMIF_IP_REV 0x20 /* 2.0 */
42 #define S3C2450_CAMIF_IP_REV 0x30 /* 3.0 - not implemented, not tested */
43 #define S3C6400_CAMIF_IP_REV 0x31 /* 3.1 - not implemented, not tested */
44 #define S3C6410_CAMIF_IP_REV 0x32 /* 3.2 */
46 /* struct camif_vp::state */
48 #define ST_VP_PENDING (1 << 0)
49 #define ST_VP_RUNNING (1 << 1)
50 #define ST_VP_STREAMING (1 << 2)
51 #define ST_VP_SENSOR_STREAMING (1 << 3)
53 #define ST_VP_ABORTING (1 << 4)
54 #define ST_VP_OFF (1 << 5)
55 #define ST_VP_LASTIRQ (1 << 6)
57 #define ST_VP_CONFIG (1 << 8)
59 #define CAMIF_SD_PAD_SINK 0
60 #define CAMIF_SD_PAD_SOURCE_C 1
61 #define CAMIF_SD_PAD_SOURCE_P 2
62 #define CAMIF_SD_PADS_NUM 3
65 IMG_FMT_RGB565
= 0x0010,
68 IMG_FMT_YCBCR420
= 0x0020,
71 IMG_FMT_YCBYCR422
= 0x0040,
77 #define img_fmt_is_rgb(x) ((x) & 0x10)
78 #define img_fmt_is_ycbcr(x) ((x) & 0x60)
80 /* Possible values for struct camif_fmt::flags */
81 #define FMT_FL_S3C24XX_CODEC (1 << 0)
82 #define FMT_FL_S3C24XX_PREVIEW (1 << 1)
83 #define FMT_FL_S3C64XX (1 << 2)
86 * struct camif_fmt - pixel format description
87 * @fourcc: fourcc code for this format, 0 if not applicable
88 * @color: a corresponding enum img_fmt
89 * @colplanes: number of physically contiguous data planes
90 * @flags: indicate for which SoCs revisions this format is valid
91 * @depth: bits per pixel (total)
92 * @ybpp: number of luminance bytes per pixel
105 * struct camif_dma_offset - pixel offset information for DMA
106 * @initial: offset (in pixels) to first pixel
107 * @line: offset (in pixels) from end of line to start of next line
109 struct camif_dma_offset
{
115 * struct camif_frame - source/target frame properties
116 * @f_width: full pixel width
117 * @f_height: full pixel height
118 * @rect: crop/composition rectangle
119 * @dma_offset: DMA offset configuration
124 struct v4l2_rect rect
;
125 struct camif_dma_offset dma_offset
;
128 /* CAMIF clocks enumeration */
135 struct vp_pix_limits
{
137 u16 max_sc_out_width
;
141 u16 out_hor_offset_align
;
144 struct camif_pix_limits
{
145 u16 win_hor_offset_align
;
149 * struct s3c_camif_variant - CAMIF variant structure
150 * @vp_pix_limits: pixel limits for the codec and preview paths
151 * @camif_pix_limits: pixel limits for the camera input interface
152 * @ip_revision: the CAMIF IP revision: 0x20 for s3c244x, 0x32 for s3c6410
154 struct s3c_camif_variant
{
155 struct vp_pix_limits vp_pix_limits
[2];
156 struct camif_pix_limits pix_limits
;
159 unsigned int vp_offset
;
162 struct s3c_camif_drvdata
{
163 const struct s3c_camif_variant
*variant
;
164 unsigned long bus_clk_freq
;
167 struct camif_scaler
{
185 * struct camif_vp - CAMIF data processing path structure (codec/preview)
186 * @irq_queue: interrupt handling waitqueue
187 * @irq: interrupt number for this data path
188 * @camif: pointer to the camif structure
189 * @pad: media pad for the video node
191 * @ctrl_handler: video node controls handler
192 * @owner: file handle that own the streaming
193 * @pending_buf_q: pending (empty) buffers queue head
194 * @active_buf_q: active (being written) buffers queue head
195 * @active_buffers: counter of buffer set up at the DMA engine
196 * @buf_index: identifier of a last empty buffer set up in H/W
197 * @frame_sequence: image frame sequence counter
198 * @reqbufs_count: the number of buffers requested
199 * @scaler: the scaler structure
200 * @out_fmt: pixel format at this video path output
201 * @payload: the output data frame payload size
202 * @out_frame: the output pixel resolution
203 * @state: the video path's state
204 * @fmt_flags: flags determining supported pixel formats
205 * @id: CAMIF id, 0 - codec, 1 - preview
206 * @rotation: current image rotation value
207 * @hflip: apply horizontal flip if set
208 * @vflip: apply vertical flip if set
211 wait_queue_head_t irq_queue
;
213 struct camif_dev
*camif
;
214 struct media_pad pad
;
215 struct video_device vdev
;
216 struct v4l2_ctrl_handler ctrl_handler
;
217 struct v4l2_fh
*owner
;
218 struct vb2_queue vb_queue
;
219 struct list_head pending_buf_q
;
220 struct list_head active_buf_q
;
221 unsigned int active_buffers
;
222 unsigned int buf_index
;
223 unsigned int frame_sequence
;
224 unsigned int reqbufs_count
;
225 struct camif_scaler scaler
;
226 const struct camif_fmt
*out_fmt
;
227 unsigned int payload
;
228 struct camif_frame out_frame
;
238 /* Video processing path enumeration */
241 #define CAMIF_VP_NUM 2
244 * struct camif_dev - the CAMIF driver private data structure
245 * @media_dev: top-level media device structure
246 * @v4l2_dev: root v4l2_device
247 * @subdev: camera interface ("catchcam") subdev
248 * @mbus_fmt: camera input media bus format
249 * @camif_crop: camera input interface crop rectangle
250 * @pads: the camif subdev's media pads
251 * @stream_count: the camera interface streaming reference counter
252 * @sensor: image sensor data structure
253 * @m_pipeline: video entity pipeline description
254 * @ctrl_handler: v4l2 control handler (owned by @subdev)
255 * @test_pattern: test pattern controls
256 * @vp: video path (DMA) description (codec/preview)
257 * @alloc_ctx: memory buffer allocator context
258 * @variant: variant information for this device
259 * @dev: pointer to the CAMIF device struct
260 * @pdata: a copy of the driver's platform data
261 * @clock: clocks required for the CAMIF operation
262 * @lock: mutex protecting this data structure
263 * @slock: spinlock protecting CAMIF registers
264 * @io_base: start address of the mmaped CAMIF registers
267 struct media_device media_dev
;
268 struct v4l2_device v4l2_dev
;
269 struct v4l2_subdev subdev
;
270 struct v4l2_mbus_framefmt mbus_fmt
;
271 struct v4l2_rect camif_crop
;
272 struct media_pad pads
[CAMIF_SD_PADS_NUM
];
276 struct v4l2_subdev
*sd
;
280 struct media_pipeline
*m_pipeline
;
282 struct v4l2_ctrl_handler ctrl_handler
;
283 struct v4l2_ctrl
*ctrl_test_pattern
;
285 struct v4l2_ctrl
*ctrl_colorfx
;
286 struct v4l2_ctrl
*ctrl_colorfx_cbcr
;
293 struct camif_vp vp
[CAMIF_VP_NUM
];
294 struct vb2_alloc_ctx
*alloc_ctx
;
296 const struct s3c_camif_variant
*variant
;
298 struct s3c_camif_plat_data pdata
;
299 struct clk
*clock
[CLK_MAX_NUM
];
302 void __iomem
*io_base
;
306 * struct camif_addr - Y/Cb/Cr DMA start address structure
307 * @y: luminance plane dma address
308 * @cb: Cb plane dma address
309 * @cr: Cr plane dma address
318 * struct camif_buffer - the camif video buffer structure
320 * @list: list head for the buffers queue
321 * @paddr: DMA start addresses
322 * @index: an identifier of this buffer at the DMA engine
324 struct camif_buffer
{
325 struct vb2_v4l2_buffer vb
;
326 struct list_head list
;
327 struct camif_addr paddr
;
331 const struct camif_fmt
*s3c_camif_find_format(struct camif_vp
*vp
,
332 const u32
*pixelformat
, int index
);
333 int s3c_camif_register_video_node(struct camif_dev
*camif
, int idx
);
334 void s3c_camif_unregister_video_node(struct camif_dev
*camif
, int idx
);
335 irqreturn_t
s3c_camif_irq_handler(int irq
, void *priv
);
336 int s3c_camif_create_subdev(struct camif_dev
*camif
);
337 void s3c_camif_unregister_subdev(struct camif_dev
*camif
);
338 int s3c_camif_set_defaults(struct camif_dev
*camif
);
339 int s3c_camif_get_scaler_config(struct camif_vp
*vp
,
340 struct camif_scaler
*scaler
);
342 static inline void camif_active_queue_add(struct camif_vp
*vp
,
343 struct camif_buffer
*buf
)
345 list_add_tail(&buf
->list
, &vp
->active_buf_q
);
346 vp
->active_buffers
++;
349 static inline struct camif_buffer
*camif_active_queue_pop(
352 struct camif_buffer
*buf
= list_first_entry(&vp
->active_buf_q
,
353 struct camif_buffer
, list
);
354 list_del(&buf
->list
);
355 vp
->active_buffers
--;
359 static inline struct camif_buffer
*camif_active_queue_peek(
360 struct camif_vp
*vp
, int index
)
362 struct camif_buffer
*tmp
, *buf
;
364 if (WARN_ON(list_empty(&vp
->active_buf_q
)))
367 list_for_each_entry_safe(buf
, tmp
, &vp
->active_buf_q
, list
) {
368 if (buf
->index
== index
) {
369 list_del(&buf
->list
);
370 vp
->active_buffers
--;
378 static inline void camif_pending_queue_add(struct camif_vp
*vp
,
379 struct camif_buffer
*buf
)
381 list_add_tail(&buf
->list
, &vp
->pending_buf_q
);
384 static inline struct camif_buffer
*camif_pending_queue_pop(
387 struct camif_buffer
*buf
= list_first_entry(&vp
->pending_buf_q
,
388 struct camif_buffer
, list
);
389 list_del(&buf
->list
);
393 #endif /* CAMIF_CORE_H_ */