1 /*
2  * TI OMAP4 ISS V4L2 Driver - Generic video node
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #ifndef OMAP4_ISS_VIDEO_H
15 #define OMAP4_ISS_VIDEO_H
16 
17 #include <linux/v4l2-mediabus.h>
18 #include <media/media-entity.h>
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-fh.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-dma-contig.h>
23 
24 #define ISS_VIDEO_DRIVER_NAME		"issvideo"
25 #define ISS_VIDEO_DRIVER_VERSION	"0.0.2"
26 
27 struct iss_device;
28 struct iss_video;
29 struct v4l2_mbus_framefmt;
30 struct v4l2_pix_format;
31 
32 /*
33  * struct iss_format_info - ISS media bus format information
34  * @code: V4L2 media bus format code
35  * @truncated: V4L2 media bus format code for the same format truncated to 10
36  *	bits. Identical to @code if the format is 10 bits wide or less.
37  * @uncompressed: V4L2 media bus format code for the corresponding uncompressed
38  *	format. Identical to @code if the format is not DPCM compressed.
39  * @flavor: V4L2 media bus format code for the same pixel layout but
40  *	shifted to be 8 bits per pixel. =0 if format is not shiftable.
41  * @pixelformat: V4L2 pixel format FCC identifier
42  * @bpp: Bits per pixel
43  * @description: Human-readable format description
44  */
45 struct iss_format_info {
46 	u32 code;
47 	u32 truncated;
48 	u32 uncompressed;
49 	u32 flavor;
50 	u32 pixelformat;
51 	unsigned int bpp;
52 	const char *description;
53 };
54 
55 enum iss_pipeline_stream_state {
56 	ISS_PIPELINE_STREAM_STOPPED = 0,
57 	ISS_PIPELINE_STREAM_CONTINUOUS = 1,
58 	ISS_PIPELINE_STREAM_SINGLESHOT = 2,
59 };
60 
61 enum iss_pipeline_state {
62 	/* The stream has been started on the input video node. */
63 	ISS_PIPELINE_STREAM_INPUT = 1,
64 	/* The stream has been started on the output video node. */
65 	ISS_PIPELINE_STREAM_OUTPUT = (1 << 1),
66 	/* At least one buffer is queued on the input video node. */
67 	ISS_PIPELINE_QUEUE_INPUT = (1 << 2),
68 	/* At least one buffer is queued on the output video node. */
69 	ISS_PIPELINE_QUEUE_OUTPUT = (1 << 3),
70 	/* The input entity is idle, ready to be started. */
71 	ISS_PIPELINE_IDLE_INPUT = (1 << 4),
72 	/* The output entity is idle, ready to be started. */
73 	ISS_PIPELINE_IDLE_OUTPUT = (1 << 5),
74 	/* The pipeline is currently streaming. */
75 	ISS_PIPELINE_STREAM = (1 << 6),
76 };
77 
78 /*
79  * struct iss_pipeline - An OMAP4 ISS hardware pipeline
80  * @entities: Bitmask of entities in the pipeline (indexed by entity ID)
81  * @error: A hardware error occurred during capture
82  */
83 struct iss_pipeline {
84 	struct media_pipeline pipe;
85 	spinlock_t lock;		/* Pipeline state and queue flags */
86 	unsigned int state;
87 	enum iss_pipeline_stream_state stream_state;
88 	struct iss_video *input;
89 	struct iss_video *output;
90 	unsigned int entities;
91 	atomic_t frame_number;
92 	bool do_propagation; /* of frame number */
93 	bool error;
94 	struct v4l2_fract max_timeperframe;
95 	struct v4l2_subdev *external;
96 	unsigned int external_rate;
97 	int external_bpp;
98 };
99 
100 #define to_iss_pipeline(__e) \
101 	container_of((__e)->pipe, struct iss_pipeline, pipe)
102 
103 static inline int iss_pipeline_ready(struct iss_pipeline *pipe)
104 {
105 	return pipe->state == (ISS_PIPELINE_STREAM_INPUT |
106 			       ISS_PIPELINE_STREAM_OUTPUT |
107 			       ISS_PIPELINE_QUEUE_INPUT |
108 			       ISS_PIPELINE_QUEUE_OUTPUT |
109 			       ISS_PIPELINE_IDLE_INPUT |
110 			       ISS_PIPELINE_IDLE_OUTPUT);
111 }
112 
113 /*
114  * struct iss_buffer - ISS buffer
115  * @buffer: ISS video buffer
116  * @iss_addr: Physical address of the buffer.
117  */
118 struct iss_buffer {
119 	/* common v4l buffer stuff -- must be first */
120 	struct vb2_v4l2_buffer	vb;
121 	struct list_head	list;
122 	dma_addr_t iss_addr;
123 };
124 
125 #define to_iss_buffer(buf)	container_of(buf, struct iss_buffer, vb)
126 
127 enum iss_video_dmaqueue_flags {
128 	/* Set if DMA queue becomes empty when ISS_PIPELINE_STREAM_CONTINUOUS */
129 	ISS_VIDEO_DMAQUEUE_UNDERRUN = (1 << 0),
130 	/* Set when queuing buffer to an empty DMA queue */
131 	ISS_VIDEO_DMAQUEUE_QUEUED = (1 << 1),
132 };
133 
134 #define iss_video_dmaqueue_flags_clr(video)	\
135 			({ (video)->dmaqueue_flags = 0; })
136 
137 /*
138  * struct iss_video_operations - ISS video operations
139  * @queue:	Resume streaming when a buffer is queued. Called on VIDIOC_QBUF
140  *		if there was no buffer previously queued.
141  */
142 struct iss_video_operations {
143 	int (*queue)(struct iss_video *video, struct iss_buffer *buffer);
144 };
145 
146 struct iss_video {
147 	struct video_device video;
148 	enum v4l2_buf_type type;
149 	struct media_pad pad;
150 
151 	struct mutex mutex;		/* format and crop settings */
152 	atomic_t active;
153 
154 	struct iss_device *iss;
155 
156 	unsigned int capture_mem;
157 	unsigned int bpl_alignment;	/* alignment value */
158 	unsigned int bpl_zero_padding;	/* whether the alignment is optional */
159 	unsigned int bpl_max;		/* maximum bytes per line value */
160 	unsigned int bpl_value;		/* bytes per line value */
161 	unsigned int bpl_padding;	/* padding at end of line */
162 
163 	/* Pipeline state */
164 	struct iss_pipeline pipe;
165 	struct mutex stream_lock;	/* pipeline and stream states */
166 	bool error;
167 
168 	/* Video buffers queue */
169 	struct vb2_queue *queue;
170 	spinlock_t qlock;		/* protects dmaqueue and error */
171 	struct list_head dmaqueue;
172 	enum iss_video_dmaqueue_flags dmaqueue_flags;
173 	struct vb2_alloc_ctx *alloc_ctx;
174 
175 	const struct iss_video_operations *ops;
176 };
177 
178 #define to_iss_video(vdev)	container_of(vdev, struct iss_video, video)
179 
180 struct iss_video_fh {
181 	struct v4l2_fh vfh;
182 	struct iss_video *video;
183 	struct vb2_queue queue;
184 	struct v4l2_format format;
185 	struct v4l2_fract timeperframe;
186 };
187 
188 #define to_iss_video_fh(fh)	container_of(fh, struct iss_video_fh, vfh)
189 #define iss_video_queue_to_iss_video_fh(q) \
190 				container_of(q, struct iss_video_fh, queue)
191 
192 int omap4iss_video_init(struct iss_video *video, const char *name);
193 void omap4iss_video_cleanup(struct iss_video *video);
194 int omap4iss_video_register(struct iss_video *video,
195 			    struct v4l2_device *vdev);
196 void omap4iss_video_unregister(struct iss_video *video);
197 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video);
198 void omap4iss_video_cancel_stream(struct iss_video *video);
199 struct media_pad *omap4iss_video_remote_pad(struct iss_video *video);
200 
201 const struct iss_format_info *
202 omap4iss_video_format_info(u32 code);
203 
204 #endif /* OMAP4_ISS_VIDEO_H */
205