]> rtime.felk.cvut.cz Git - hercules2020/nv-tegra/linux-4.4.git/blob - drivers/media/platform/vivid/vivid-core.h
8156d169df881c6270db8e6b5d5428244b71848c
[hercules2020/nv-tegra/linux-4.4.git] / drivers / media / platform / vivid / vivid-core.h
1 /*
2  * vivid-core.h - core datastructures
3  *
4  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you may redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17  * SOFTWARE.
18  */
19
20 #ifndef _VIVID_CORE_H_
21 #define _VIVID_CORE_H_
22
23 #include <linux/fb.h>
24 #include <media/videobuf2-v4l2.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/sensor_common.h>
29 #include "vivid-tpg.h"
30 #include "vivid-rds-gen.h"
31 #include "vivid-vbi-gen.h"
32
33 #define dprintk(dev, level, fmt, arg...) \
34         v4l2_dbg(level, vivid_debug, &dev->v4l2_dev, fmt, ## arg)
35
36 /* Maximum allowed frame rate
37  *
38  * vivid will allow setting timeperframe in [1/FPS_MAX - FPS_MAX/1] range.
39  *
40  * Ideally FPS_MAX should be infinity, i.e. practically UINT_MAX, but that
41  * might hit application errors when they manipulate these values.
42  *
43  * Besides, for tpf < 10ms image-generation logic should be changed, to avoid
44  * producing frames with equal content.
45  */
46 #define FPS_MAX 100
47
48 /* The maximum number of clip rectangles */
49 #define MAX_CLIPS  16
50 /* The maximum number of inputs */
51 #define MAX_INPUTS 16
52 /* The maximum number of outputs */
53 #define MAX_OUTPUTS 16
54 /* The maximum up or down scaling factor is 4 */
55 #define MAX_ZOOM  4
56 /* The maximum image width/height are set to 4K DMT */
57 #define MAX_WIDTH  4096
58 #define MAX_HEIGHT 2160
59 /* The minimum image width/height */
60 #define MIN_WIDTH  16
61 #define MIN_HEIGHT 16
62 /* Metadata height max/default */
63 #define MAX_METADATA_HEIGHT 16
64 #define DEF_METADATA_HEIGHT 1
65 /* The data_offset of plane 0 for the multiplanar formats */
66 #define PLANE0_DATA_OFFSET 128
67
68 /* The supported TV frequency range in MHz */
69 #define MIN_TV_FREQ (44U * 16U)
70 #define MAX_TV_FREQ (958U * 16U)
71
72 /* The number of samples returned in every SDR buffer */
73 #define SDR_CAP_SAMPLES_PER_BUF 0x4000
74
75 /* used by the threads to know when to resync internal counters */
76 #define JIFFIES_PER_DAY (3600U * 24U * HZ)
77 #define JIFFIES_RESYNC (JIFFIES_PER_DAY * (0xf0000000U / JIFFIES_PER_DAY))
78
79 extern const struct v4l2_rect vivid_min_rect;
80 extern const struct v4l2_rect vivid_max_rect;
81 extern unsigned vivid_debug;
82
83 struct vivid_fmt {
84         u32     fourcc;          /* v4l2 format id */
85         bool    is_yuv;
86         bool    can_do_overlay;
87         bool    is_metadata[TPG_MAX_PLANES];
88         u8      vdownsampling[TPG_MAX_PLANES];
89         u8      packedpixels;
90         u32     alpha_mask;
91         u8      planes;
92         u8      buffers;
93         u32     data_offset[TPG_MAX_PLANES];
94         u32     bit_depth[TPG_MAX_PLANES];
95 };
96
97 extern struct vivid_fmt vivid_formats[];
98
99 /* buffer for one video frame */
100 struct vivid_buffer {
101         /* common v4l buffer stuff -- must be first */
102         struct vb2_v4l2_buffer vb;
103         struct list_head        list;
104 };
105
106 enum vivid_input {
107         WEBCAM,
108         TV,
109         SVID,
110         HDMI,
111 };
112
113 enum vivid_signal_mode {
114         CURRENT_DV_TIMINGS,
115         CURRENT_STD = CURRENT_DV_TIMINGS,
116         NO_SIGNAL,
117         NO_LOCK,
118         OUT_OF_RANGE,
119         SELECTED_DV_TIMINGS,
120         SELECTED_STD = SELECTED_DV_TIMINGS,
121         CYCLE_DV_TIMINGS,
122         CYCLE_STD = CYCLE_DV_TIMINGS,
123         CUSTOM_DV_TIMINGS,
124 };
125
126 enum vivid_colorspace {
127         VIVID_CS_170M,
128         VIVID_CS_709,
129         VIVID_CS_SRGB,
130         VIVID_CS_ADOBERGB,
131         VIVID_CS_2020,
132         VIVID_CS_DCI_P3,
133         VIVID_CS_240M,
134         VIVID_CS_SYS_M,
135         VIVID_CS_SYS_BG,
136 };
137
138 #define VIVID_INVALID_SIGNAL(mode) \
139         ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
140
141 struct vivid_dev {
142         unsigned                        inst;
143         struct v4l2_device              v4l2_dev;
144         struct v4l2_ctrl_handler        ctrl_hdl_user_gen;
145         struct v4l2_ctrl_handler        ctrl_hdl_user_vid;
146         struct v4l2_ctrl_handler        ctrl_hdl_user_aud;
147         struct v4l2_ctrl_handler        ctrl_hdl_streaming;
148         struct v4l2_ctrl_handler        ctrl_hdl_sdtv_cap;
149         struct v4l2_ctrl_handler        ctrl_hdl_loop_cap;
150         struct video_device             vid_cap_dev;
151         struct v4l2_ctrl_handler        ctrl_hdl_vid_cap;
152         struct video_device             vid_out_dev;
153         struct v4l2_ctrl_handler        ctrl_hdl_vid_out;
154         struct video_device             vbi_cap_dev;
155         struct v4l2_ctrl_handler        ctrl_hdl_vbi_cap;
156         struct video_device             vbi_out_dev;
157         struct v4l2_ctrl_handler        ctrl_hdl_vbi_out;
158         struct video_device             radio_rx_dev;
159         struct v4l2_ctrl_handler        ctrl_hdl_radio_rx;
160         struct video_device             radio_tx_dev;
161         struct v4l2_ctrl_handler        ctrl_hdl_radio_tx;
162         struct video_device             sdr_cap_dev;
163         struct v4l2_ctrl_handler        ctrl_hdl_sdr_cap;
164         spinlock_t                      slock;
165         struct mutex                    mutex;
166         struct mutex                    mutex_framerate;
167
168         /* capabilities */
169         u32                             vid_cap_caps;
170         u32                             vid_out_caps;
171         u32                             vbi_cap_caps;
172         u32                             vbi_out_caps;
173         u32                             sdr_cap_caps;
174         u32                             radio_rx_caps;
175         u32                             radio_tx_caps;
176
177         /* supported features */
178         bool                            multiplanar;
179         unsigned                        num_inputs;
180         u8                              input_type[MAX_INPUTS];
181         u8                              input_name_counter[MAX_INPUTS];
182         unsigned                        num_outputs;
183         u8                              output_type[MAX_OUTPUTS];
184         u8                              output_name_counter[MAX_OUTPUTS];
185         bool                            has_audio_inputs;
186         bool                            has_audio_outputs;
187         bool                            has_vid_cap;
188         bool                            has_vid_out;
189         bool                            has_vbi_cap;
190         bool                            has_raw_vbi_cap;
191         bool                            has_sliced_vbi_cap;
192         bool                            has_vbi_out;
193         bool                            has_raw_vbi_out;
194         bool                            has_sliced_vbi_out;
195         bool                            has_radio_rx;
196         bool                            has_radio_tx;
197         bool                            has_sdr_cap;
198         bool                            has_fb;
199
200         bool                            can_loop_video;
201
202         /* controls */
203         struct v4l2_ctrl                *brightness;
204         struct v4l2_ctrl                *contrast;
205         struct v4l2_ctrl                *saturation;
206         struct v4l2_ctrl                *hue;
207         struct {
208                 /* autogain/gain cluster */
209                 struct v4l2_ctrl        *autogain;
210                 struct v4l2_ctrl        *gain;
211         };
212         struct v4l2_ctrl                *volume;
213         struct v4l2_ctrl                *mute;
214         struct v4l2_ctrl                *alpha;
215         struct v4l2_ctrl                *button;
216         struct v4l2_ctrl                *boolean;
217         struct v4l2_ctrl                *int32;
218         struct v4l2_ctrl                *int64;
219         struct v4l2_ctrl                *menu;
220         struct v4l2_ctrl                *string;
221         struct v4l2_ctrl                *bitmask;
222         struct v4l2_ctrl                *int_menu;
223         struct v4l2_ctrl                *test_pattern;
224         struct v4l2_ctrl                *colorspace;
225         struct v4l2_ctrl                *rgb_range_cap;
226         struct v4l2_ctrl                *real_rgb_range_cap;
227         struct v4l2_ctrl                *framelength;
228         struct v4l2_ctrl                *ctrl_signalprops;
229         struct v4l2_ctrl                *ctrl_imageprops;
230         struct v4l2_ctrl                *ctrl_controlprops;
231         struct v4l2_ctrl                *ctrl_dvtimings;
232         struct v4l2_ctrl                *ctrl_sensormodes;
233         struct {
234                 /* std_signal_mode/standard cluster */
235                 struct v4l2_ctrl        *ctrl_std_signal_mode;
236                 struct v4l2_ctrl        *ctrl_standard;
237         };
238         struct {
239                 /* dv_timings_signal_mode/timings cluster */
240                 struct v4l2_ctrl        *ctrl_dv_timings_signal_mode;
241                 struct v4l2_ctrl        *ctrl_dv_timings;
242         };
243         struct v4l2_ctrl                *ctrl_has_crop_cap;
244         struct v4l2_ctrl                *ctrl_has_compose_cap;
245         struct v4l2_ctrl                *ctrl_has_scaler_cap;
246         struct v4l2_ctrl                *ctrl_has_crop_out;
247         struct v4l2_ctrl                *ctrl_has_compose_out;
248         struct v4l2_ctrl                *ctrl_has_scaler_out;
249         struct v4l2_ctrl                *ctrl_tx_mode;
250         struct v4l2_ctrl                *ctrl_tx_rgb_range;
251
252         struct v4l2_ctrl                *radio_tx_rds_pi;
253         struct v4l2_ctrl                *radio_tx_rds_pty;
254         struct v4l2_ctrl                *radio_tx_rds_mono_stereo;
255         struct v4l2_ctrl                *radio_tx_rds_art_head;
256         struct v4l2_ctrl                *radio_tx_rds_compressed;
257         struct v4l2_ctrl                *radio_tx_rds_dyn_pty;
258         struct v4l2_ctrl                *radio_tx_rds_ta;
259         struct v4l2_ctrl                *radio_tx_rds_tp;
260         struct v4l2_ctrl                *radio_tx_rds_ms;
261         struct v4l2_ctrl                *radio_tx_rds_psname;
262         struct v4l2_ctrl                *radio_tx_rds_radiotext;
263
264         struct v4l2_ctrl                *radio_rx_rds_pty;
265         struct v4l2_ctrl                *radio_rx_rds_ta;
266         struct v4l2_ctrl                *radio_rx_rds_tp;
267         struct v4l2_ctrl                *radio_rx_rds_ms;
268         struct v4l2_ctrl                *radio_rx_rds_psname;
269         struct v4l2_ctrl                *radio_rx_rds_radiotext;
270
271         unsigned                        input_brightness[MAX_INPUTS];
272         unsigned                        osd_mode;
273         unsigned                        button_pressed;
274         bool                            sensor_hflip;
275         bool                            sensor_vflip;
276         bool                            hflip;
277         bool                            vflip;
278         bool                            vbi_cap_interlaced;
279         bool                            loop_video;
280
281         /* Framebuffer */
282         unsigned long                   video_pbase;
283         void                            *video_vbase;
284         u32                             video_buffer_size;
285         int                             display_width;
286         int                             display_height;
287         int                             display_byte_stride;
288         int                             bits_per_pixel;
289         int                             bytes_per_pixel;
290         struct fb_info                  fb_info;
291         struct fb_var_screeninfo        fb_defined;
292         struct fb_fix_screeninfo        fb_fix;
293
294         /* Error injection */
295         bool                            queue_setup_error;
296         bool                            buf_prepare_error;
297         bool                            start_streaming_error;
298         bool                            dqbuf_error;
299         bool                            seq_wrap;
300         bool                            time_wrap;
301         __kernel_time_t                 time_wrap_offset;
302         unsigned                        perc_dropped_buffers;
303         enum vivid_signal_mode          std_signal_mode;
304         unsigned                        query_std_last;
305         v4l2_std_id                     query_std;
306         enum tpg_video_aspect           std_aspect_ratio;
307
308         enum vivid_signal_mode          dv_timings_signal_mode;
309         char                            **query_dv_timings_qmenu;
310         unsigned                        query_dv_timings_size;
311         unsigned                        query_dv_timings_last;
312         unsigned                        query_dv_timings;
313         enum tpg_video_aspect           dv_timings_aspect_ratio;
314
315         /* Input */
316         unsigned                        input;
317         v4l2_std_id                     std_cap;
318         struct v4l2_dv_timings          dv_timings_cap;
319         u32                             service_set_cap;
320         struct vivid_vbi_gen_data       vbi_gen;
321         u8                              *edid;
322         unsigned                        edid_blocks;
323         unsigned                        edid_max_blocks;
324         unsigned                        webcam_size_idx;
325         unsigned                        webcam_ival_idx;
326         unsigned                        tv_freq;
327         unsigned                        tv_audmode;
328         unsigned                        tv_field_cap;
329         unsigned                        tv_audio_input;
330
331         /* Capture Overlay */
332         struct v4l2_framebuffer         fb_cap;
333         struct v4l2_fh                  *overlay_cap_owner;
334         void                            *fb_vbase_cap;
335         int                             overlay_cap_top, overlay_cap_left;
336         enum v4l2_field                 overlay_cap_field;
337         void                            *bitmap_cap;
338         struct v4l2_clip                clips_cap[MAX_CLIPS];
339         struct v4l2_clip                try_clips_cap[MAX_CLIPS];
340         unsigned                        clipcount_cap;
341
342         /* Output */
343         unsigned                        output;
344         v4l2_std_id                     std_out;
345         struct v4l2_dv_timings          dv_timings_out;
346         u32                             colorspace_out;
347         u32                             ycbcr_enc_out;
348         u32                             quantization_out;
349         u32                             xfer_func_out;
350         u32                             service_set_out;
351         unsigned                        bytesperline_out[TPG_MAX_PLANES];
352         unsigned                        height_out[TPG_MAX_PLANES];
353         unsigned                        tv_field_out;
354         unsigned                        tv_audio_output;
355         bool                            vbi_out_have_wss;
356         u8                              vbi_out_wss[2];
357         bool                            vbi_out_have_cc[2];
358         u8                              vbi_out_cc[2][2];
359         bool                            dvi_d_out;
360         u8                              *scaled_line;
361         u8                              *blended_line;
362         unsigned                        cur_scaled_line;
363
364         /* Output Overlay */
365         void                            *fb_vbase_out;
366         bool                            overlay_out_enabled;
367         int                             overlay_out_top, overlay_out_left;
368         void                            *bitmap_out;
369         struct v4l2_clip                clips_out[MAX_CLIPS];
370         struct v4l2_clip                try_clips_out[MAX_CLIPS];
371         unsigned                        clipcount_out;
372         unsigned                        fbuf_out_flags;
373         u32                             chromakey_out;
374         u8                              global_alpha_out;
375
376         /* video capture */
377         struct tpg_data                 tpg;
378         unsigned                        ms_vid_cap;
379         bool                            must_blank[VIDEO_MAX_FRAME];
380
381         struct vivid_fmt                *fmt_cap;
382         struct v4l2_fract               timeperframe_vid_cap;
383         enum v4l2_field                 field_cap;
384         struct v4l2_rect                src_rect;
385         struct v4l2_rect                fmt_cap_rect;
386         struct v4l2_rect                crop_cap;
387         struct v4l2_rect                compose_cap;
388         struct v4l2_rect                crop_bounds_cap;
389         struct vb2_queue                vb_vid_cap_q;
390         struct list_head                vid_cap_active;
391         struct vb2_queue                vb_vbi_cap_q;
392         struct list_head                vbi_cap_active;
393
394         /* thread for generating video capture stream */
395         struct task_struct              *kthread_vid_cap;
396         unsigned long                   jiffies_vid_cap;
397         u32                             cap_seq_offset;
398         u32                             cap_seq_count;
399         bool                            cap_seq_resync;
400         u32                             vid_cap_seq_start;
401         u32                             vid_cap_seq_count;
402         bool                            vid_cap_streaming;
403         u32                             vbi_cap_seq_start;
404         u32                             vbi_cap_seq_count;
405         bool                            vbi_cap_streaming;
406         bool                            stream_sliced_vbi_cap;
407         u32                             embedded_data_height;
408         u32                             fmt_out_metadata_height;
409
410         /* added for NV sensor emulation */
411         struct sensor_properties        sensor_props;
412
413         /* video output */
414         struct vivid_fmt                *fmt_out;
415         struct v4l2_fract               timeperframe_vid_out;
416         enum v4l2_field                 field_out;
417         struct v4l2_rect                sink_rect;
418         struct v4l2_rect                fmt_out_rect;
419         struct v4l2_rect                crop_out;
420         struct v4l2_rect                compose_out;
421         struct v4l2_rect                compose_bounds_out;
422         struct vb2_queue                vb_vid_out_q;
423         struct list_head                vid_out_active;
424         struct vb2_queue                vb_vbi_out_q;
425         struct list_head                vbi_out_active;
426
427         /* video loop precalculated rectangles */
428
429         /*
430          * Intersection between what the output side composes and the capture side
431          * crops. I.e., what actually needs to be copied from the output buffer to
432          * the capture buffer.
433          */
434         struct v4l2_rect                loop_vid_copy;
435         /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
436         struct v4l2_rect                loop_vid_out;
437         /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
438         struct v4l2_rect                loop_vid_cap;
439         /*
440          * The intersection of the framebuffer, the overlay output window and
441          * loop_vid_copy. I.e., the part of the framebuffer that actually should be
442          * blended with the compose_out rectangle. This uses the framebuffer origin.
443          */
444         struct v4l2_rect                loop_fb_copy;
445         /* The same as loop_fb_copy but with compose_out origin. */
446         struct v4l2_rect                loop_vid_overlay;
447         /*
448          * The part of the capture buffer that (after scaling) corresponds
449          * to loop_vid_overlay.
450          */
451         struct v4l2_rect                loop_vid_overlay_cap;
452
453         /* thread for generating video output stream */
454         struct task_struct              *kthread_vid_out;
455         unsigned long                   jiffies_vid_out;
456         u32                             out_seq_offset;
457         u32                             out_seq_count;
458         bool                            out_seq_resync;
459         u32                             vid_out_seq_start;
460         u32                             vid_out_seq_count;
461         bool                            vid_out_streaming;
462         u32                             vbi_out_seq_start;
463         u32                             vbi_out_seq_count;
464         bool                            vbi_out_streaming;
465         bool                            stream_sliced_vbi_out;
466
467         /* SDR capture */
468         struct vb2_queue                vb_sdr_cap_q;
469         struct list_head                sdr_cap_active;
470         u32                             sdr_pixelformat; /* v4l2 format id */
471         unsigned                        sdr_buffersize;
472         unsigned                        sdr_adc_freq;
473         unsigned                        sdr_fm_freq;
474         unsigned                        sdr_fm_deviation;
475         int                             sdr_fixp_src_phase;
476         int                             sdr_fixp_mod_phase;
477
478         bool                            tstamp_src_is_soe;
479         bool                            has_crop_cap;
480         bool                            has_compose_cap;
481         bool                            has_scaler_cap;
482         bool                            has_crop_out;
483         bool                            has_compose_out;
484         bool                            has_scaler_out;
485
486         /* thread for generating SDR stream */
487         struct task_struct              *kthread_sdr_cap;
488         unsigned long                   jiffies_sdr_cap;
489         u32                             sdr_cap_seq_offset;
490         u32                             sdr_cap_seq_count;
491         bool                            sdr_cap_seq_resync;
492
493         /* RDS generator */
494         struct vivid_rds_gen            rds_gen;
495
496         /* Radio receiver */
497         unsigned                        radio_rx_freq;
498         unsigned                        radio_rx_audmode;
499         int                             radio_rx_sig_qual;
500         unsigned                        radio_rx_hw_seek_mode;
501         bool                            radio_rx_hw_seek_prog_lim;
502         bool                            radio_rx_rds_controls;
503         bool                            radio_rx_rds_enabled;
504         unsigned                        radio_rx_rds_use_alternates;
505         unsigned                        radio_rx_rds_last_block;
506         struct v4l2_fh                  *radio_rx_rds_owner;
507
508         /* Radio transmitter */
509         unsigned                        radio_tx_freq;
510         unsigned                        radio_tx_subchans;
511         bool                            radio_tx_rds_controls;
512         unsigned                        radio_tx_rds_last_block;
513         struct v4l2_fh                  *radio_tx_rds_owner;
514
515         /* Shared between radio receiver and transmitter */
516         bool                            radio_rds_loop;
517         struct timespec                 radio_rds_init_ts;
518 };
519
520 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
521 {
522         return dev->input_type[dev->input] == WEBCAM;
523 }
524
525 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
526 {
527         return dev->input_type[dev->input] == TV;
528 }
529
530 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
531 {
532         return dev->input_type[dev->input] == SVID;
533 }
534
535 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
536 {
537         return dev->input_type[dev->input] == HDMI;
538 }
539
540 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
541 {
542         return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
543 }
544
545 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
546 {
547         return dev->output_type[dev->output] == SVID;
548 }
549
550 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
551 {
552         return dev->output_type[dev->output] == HDMI;
553 }
554
555 #endif