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