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