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