Merge tag 'media/v4.20-2' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-microblaze.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 #ifdef CONFIG_MEDIA_CONTROLLER
140         struct media_device             mdev;
141         struct media_pad                vid_cap_pad;
142         struct media_pad                vid_out_pad;
143         struct media_pad                vbi_cap_pad;
144         struct media_pad                vbi_out_pad;
145         struct media_pad                sdr_cap_pad;
146 #endif
147         struct v4l2_ctrl_handler        ctrl_hdl_user_gen;
148         struct v4l2_ctrl_handler        ctrl_hdl_user_vid;
149         struct v4l2_ctrl_handler        ctrl_hdl_user_aud;
150         struct v4l2_ctrl_handler        ctrl_hdl_streaming;
151         struct v4l2_ctrl_handler        ctrl_hdl_sdtv_cap;
152         struct v4l2_ctrl_handler        ctrl_hdl_loop_cap;
153         struct v4l2_ctrl_handler        ctrl_hdl_fb;
154         struct video_device             vid_cap_dev;
155         struct v4l2_ctrl_handler        ctrl_hdl_vid_cap;
156         struct video_device             vid_out_dev;
157         struct v4l2_ctrl_handler        ctrl_hdl_vid_out;
158         struct video_device             vbi_cap_dev;
159         struct v4l2_ctrl_handler        ctrl_hdl_vbi_cap;
160         struct video_device             vbi_out_dev;
161         struct v4l2_ctrl_handler        ctrl_hdl_vbi_out;
162         struct video_device             radio_rx_dev;
163         struct v4l2_ctrl_handler        ctrl_hdl_radio_rx;
164         struct video_device             radio_tx_dev;
165         struct v4l2_ctrl_handler        ctrl_hdl_radio_tx;
166         struct video_device             sdr_cap_dev;
167         struct v4l2_ctrl_handler        ctrl_hdl_sdr_cap;
168         spinlock_t                      slock;
169         struct mutex                    mutex;
170
171         /* capabilities */
172         u32                             vid_cap_caps;
173         u32                             vid_out_caps;
174         u32                             vbi_cap_caps;
175         u32                             vbi_out_caps;
176         u32                             sdr_cap_caps;
177         u32                             radio_rx_caps;
178         u32                             radio_tx_caps;
179
180         /* supported features */
181         bool                            multiplanar;
182         unsigned                        num_inputs;
183         u8                              input_type[MAX_INPUTS];
184         u8                              input_name_counter[MAX_INPUTS];
185         unsigned                        num_outputs;
186         u8                              output_type[MAX_OUTPUTS];
187         u8                              output_name_counter[MAX_OUTPUTS];
188         bool                            has_audio_inputs;
189         bool                            has_audio_outputs;
190         bool                            has_vid_cap;
191         bool                            has_vid_out;
192         bool                            has_vbi_cap;
193         bool                            has_raw_vbi_cap;
194         bool                            has_sliced_vbi_cap;
195         bool                            has_vbi_out;
196         bool                            has_raw_vbi_out;
197         bool                            has_sliced_vbi_out;
198         bool                            has_radio_rx;
199         bool                            has_radio_tx;
200         bool                            has_sdr_cap;
201         bool                            has_fb;
202
203         bool                            can_loop_video;
204
205         /* controls */
206         struct v4l2_ctrl                *brightness;
207         struct v4l2_ctrl                *contrast;
208         struct v4l2_ctrl                *saturation;
209         struct v4l2_ctrl                *hue;
210         struct {
211                 /* autogain/gain cluster */
212                 struct v4l2_ctrl        *autogain;
213                 struct v4l2_ctrl        *gain;
214         };
215         struct v4l2_ctrl                *volume;
216         struct v4l2_ctrl                *mute;
217         struct v4l2_ctrl                *alpha;
218         struct v4l2_ctrl                *button;
219         struct v4l2_ctrl                *boolean;
220         struct v4l2_ctrl                *int32;
221         struct v4l2_ctrl                *int64;
222         struct v4l2_ctrl                *menu;
223         struct v4l2_ctrl                *string;
224         struct v4l2_ctrl                *bitmask;
225         struct v4l2_ctrl                *int_menu;
226         struct v4l2_ctrl                *test_pattern;
227         struct v4l2_ctrl                *colorspace;
228         struct v4l2_ctrl                *rgb_range_cap;
229         struct v4l2_ctrl                *real_rgb_range_cap;
230         struct {
231                 /* std_signal_mode/standard cluster */
232                 struct v4l2_ctrl        *ctrl_std_signal_mode;
233                 struct v4l2_ctrl        *ctrl_standard;
234         };
235         struct {
236                 /* dv_timings_signal_mode/timings cluster */
237                 struct v4l2_ctrl        *ctrl_dv_timings_signal_mode;
238                 struct v4l2_ctrl        *ctrl_dv_timings;
239         };
240         struct v4l2_ctrl                *ctrl_has_crop_cap;
241         struct v4l2_ctrl                *ctrl_has_compose_cap;
242         struct v4l2_ctrl                *ctrl_has_scaler_cap;
243         struct v4l2_ctrl                *ctrl_has_crop_out;
244         struct v4l2_ctrl                *ctrl_has_compose_out;
245         struct v4l2_ctrl                *ctrl_has_scaler_out;
246         struct v4l2_ctrl                *ctrl_tx_mode;
247         struct v4l2_ctrl                *ctrl_tx_rgb_range;
248
249         struct v4l2_ctrl                *radio_tx_rds_pi;
250         struct v4l2_ctrl                *radio_tx_rds_pty;
251         struct v4l2_ctrl                *radio_tx_rds_mono_stereo;
252         struct v4l2_ctrl                *radio_tx_rds_art_head;
253         struct v4l2_ctrl                *radio_tx_rds_compressed;
254         struct v4l2_ctrl                *radio_tx_rds_dyn_pty;
255         struct v4l2_ctrl                *radio_tx_rds_ta;
256         struct v4l2_ctrl                *radio_tx_rds_tp;
257         struct v4l2_ctrl                *radio_tx_rds_ms;
258         struct v4l2_ctrl                *radio_tx_rds_psname;
259         struct v4l2_ctrl                *radio_tx_rds_radiotext;
260
261         struct v4l2_ctrl                *radio_rx_rds_pty;
262         struct v4l2_ctrl                *radio_rx_rds_ta;
263         struct v4l2_ctrl                *radio_rx_rds_tp;
264         struct v4l2_ctrl                *radio_rx_rds_ms;
265         struct v4l2_ctrl                *radio_rx_rds_psname;
266         struct v4l2_ctrl                *radio_rx_rds_radiotext;
267
268         unsigned                        input_brightness[MAX_INPUTS];
269         unsigned                        osd_mode;
270         unsigned                        button_pressed;
271         bool                            sensor_hflip;
272         bool                            sensor_vflip;
273         bool                            hflip;
274         bool                            vflip;
275         bool                            vbi_cap_interlaced;
276         bool                            loop_video;
277         bool                            reduced_fps;
278
279         /* Framebuffer */
280         unsigned long                   video_pbase;
281         void                            *video_vbase;
282         u32                             video_buffer_size;
283         int                             display_width;
284         int                             display_height;
285         int                             display_byte_stride;
286         int                             bits_per_pixel;
287         int                             bytes_per_pixel;
288         struct fb_info                  fb_info;
289         struct fb_var_screeninfo        fb_defined;
290         struct fb_fix_screeninfo        fb_fix;
291
292         /* Error injection */
293         bool                            queue_setup_error;
294         bool                            buf_prepare_error;
295         bool                            start_streaming_error;
296         bool                            dqbuf_error;
297         bool                            seq_wrap;
298         bool                            time_wrap;
299         u64                             time_wrap_offset;
300         unsigned                        perc_dropped_buffers;
301         enum vivid_signal_mode          std_signal_mode;
302         unsigned                        query_std_last;
303         v4l2_std_id                     query_std;
304         enum tpg_video_aspect           std_aspect_ratio;
305
306         enum vivid_signal_mode          dv_timings_signal_mode;
307         char                            **query_dv_timings_qmenu;
308         unsigned                        query_dv_timings_size;
309         unsigned                        query_dv_timings_last;
310         unsigned                        query_dv_timings;
311         enum tpg_video_aspect           dv_timings_aspect_ratio;
312
313         /* Input */
314         unsigned                        input;
315         v4l2_std_id                     std_cap;
316         struct v4l2_dv_timings          dv_timings_cap;
317         u32                             service_set_cap;
318         struct vivid_vbi_gen_data       vbi_gen;
319         u8                              *edid;
320         unsigned                        edid_blocks;
321         unsigned                        edid_max_blocks;
322         unsigned                        webcam_size_idx;
323         unsigned                        webcam_ival_idx;
324         unsigned                        tv_freq;
325         unsigned                        tv_audmode;
326         unsigned                        tv_field_cap;
327         unsigned                        tv_audio_input;
328
329         /* Capture Overlay */
330         struct v4l2_framebuffer         fb_cap;
331         struct v4l2_fh                  *overlay_cap_owner;
332         void                            *fb_vbase_cap;
333         int                             overlay_cap_top, overlay_cap_left;
334         enum v4l2_field                 overlay_cap_field;
335         void                            *bitmap_cap;
336         struct v4l2_clip                clips_cap[MAX_CLIPS];
337         struct v4l2_clip                try_clips_cap[MAX_CLIPS];
338         unsigned                        clipcount_cap;
339
340         /* Output */
341         unsigned                        output;
342         v4l2_std_id                     std_out;
343         struct v4l2_dv_timings          dv_timings_out;
344         u32                             colorspace_out;
345         u32                             ycbcr_enc_out;
346         u32                             hsv_enc_out;
347         u32                             quantization_out;
348         u32                             xfer_func_out;
349         u32                             service_set_out;
350         unsigned                        bytesperline_out[TPG_MAX_PLANES];
351         unsigned                        tv_field_out;
352         unsigned                        tv_audio_output;
353         bool                            vbi_out_have_wss;
354         u8                              vbi_out_wss[2];
355         bool                            vbi_out_have_cc[2];
356         u8                              vbi_out_cc[2][2];
357         bool                            dvi_d_out;
358         u8                              *scaled_line;
359         u8                              *blended_line;
360         unsigned                        cur_scaled_line;
361
362         /* Output Overlay */
363         void                            *fb_vbase_out;
364         bool                            overlay_out_enabled;
365         int                             overlay_out_top, overlay_out_left;
366         void                            *bitmap_out;
367         struct v4l2_clip                clips_out[MAX_CLIPS];
368         struct v4l2_clip                try_clips_out[MAX_CLIPS];
369         unsigned                        clipcount_out;
370         unsigned                        fbuf_out_flags;
371         u32                             chromakey_out;
372         u8                              global_alpha_out;
373
374         /* video capture */
375         struct tpg_data                 tpg;
376         unsigned                        ms_vid_cap;
377         bool                            must_blank[VIDEO_MAX_FRAME];
378
379         const struct vivid_fmt          *fmt_cap;
380         struct v4l2_fract               timeperframe_vid_cap;
381         enum v4l2_field                 field_cap;
382         struct v4l2_rect                src_rect;
383         struct v4l2_rect                fmt_cap_rect;
384         struct v4l2_rect                crop_cap;
385         struct v4l2_rect                compose_cap;
386         struct v4l2_rect                crop_bounds_cap;
387         struct vb2_queue                vb_vid_cap_q;
388         struct list_head                vid_cap_active;
389         struct vb2_queue                vb_vbi_cap_q;
390         struct list_head                vbi_cap_active;
391
392         /* thread for generating video capture stream */
393         struct task_struct              *kthread_vid_cap;
394         unsigned long                   jiffies_vid_cap;
395         u32                             cap_seq_offset;
396         u32                             cap_seq_count;
397         bool                            cap_seq_resync;
398         u32                             vid_cap_seq_start;
399         u32                             vid_cap_seq_count;
400         bool                            vid_cap_streaming;
401         u32                             vbi_cap_seq_start;
402         u32                             vbi_cap_seq_count;
403         bool                            vbi_cap_streaming;
404         bool                            stream_sliced_vbi_cap;
405
406         /* video output */
407         const struct vivid_fmt          *fmt_out;
408         struct v4l2_fract               timeperframe_vid_out;
409         enum v4l2_field                 field_out;
410         struct v4l2_rect                sink_rect;
411         struct v4l2_rect                fmt_out_rect;
412         struct v4l2_rect                crop_out;
413         struct v4l2_rect                compose_out;
414         struct v4l2_rect                compose_bounds_out;
415         struct vb2_queue                vb_vid_out_q;
416         struct list_head                vid_out_active;
417         struct vb2_queue                vb_vbi_out_q;
418         struct list_head                vbi_out_active;
419
420         /* video loop precalculated rectangles */
421
422         /*
423          * Intersection between what the output side composes and the capture side
424          * crops. I.e., what actually needs to be copied from the output buffer to
425          * the capture buffer.
426          */
427         struct v4l2_rect                loop_vid_copy;
428         /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
429         struct v4l2_rect                loop_vid_out;
430         /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
431         struct v4l2_rect                loop_vid_cap;
432         /*
433          * The intersection of the framebuffer, the overlay output window and
434          * loop_vid_copy. I.e., the part of the framebuffer that actually should be
435          * blended with the compose_out rectangle. This uses the framebuffer origin.
436          */
437         struct v4l2_rect                loop_fb_copy;
438         /* The same as loop_fb_copy but with compose_out origin. */
439         struct v4l2_rect                loop_vid_overlay;
440         /*
441          * The part of the capture buffer that (after scaling) corresponds
442          * to loop_vid_overlay.
443          */
444         struct v4l2_rect                loop_vid_overlay_cap;
445
446         /* thread for generating video output stream */
447         struct task_struct              *kthread_vid_out;
448         unsigned long                   jiffies_vid_out;
449         u32                             out_seq_offset;
450         u32                             out_seq_count;
451         bool                            out_seq_resync;
452         u32                             vid_out_seq_start;
453         u32                             vid_out_seq_count;
454         bool                            vid_out_streaming;
455         u32                             vbi_out_seq_start;
456         u32                             vbi_out_seq_count;
457         bool                            vbi_out_streaming;
458         bool                            stream_sliced_vbi_out;
459
460         /* SDR capture */
461         struct vb2_queue                vb_sdr_cap_q;
462         struct list_head                sdr_cap_active;
463         u32                             sdr_pixelformat; /* v4l2 format id */
464         unsigned                        sdr_buffersize;
465         unsigned                        sdr_adc_freq;
466         unsigned                        sdr_fm_freq;
467         unsigned                        sdr_fm_deviation;
468         int                             sdr_fixp_src_phase;
469         int                             sdr_fixp_mod_phase;
470
471         bool                            tstamp_src_is_soe;
472         bool                            has_crop_cap;
473         bool                            has_compose_cap;
474         bool                            has_scaler_cap;
475         bool                            has_crop_out;
476         bool                            has_compose_out;
477         bool                            has_scaler_out;
478
479         /* thread for generating SDR stream */
480         struct task_struct              *kthread_sdr_cap;
481         unsigned long                   jiffies_sdr_cap;
482         u32                             sdr_cap_seq_offset;
483         u32                             sdr_cap_seq_count;
484         bool                            sdr_cap_seq_resync;
485
486         /* RDS generator */
487         struct vivid_rds_gen            rds_gen;
488
489         /* Radio receiver */
490         unsigned                        radio_rx_freq;
491         unsigned                        radio_rx_audmode;
492         int                             radio_rx_sig_qual;
493         unsigned                        radio_rx_hw_seek_mode;
494         bool                            radio_rx_hw_seek_prog_lim;
495         bool                            radio_rx_rds_controls;
496         bool                            radio_rx_rds_enabled;
497         unsigned                        radio_rx_rds_use_alternates;
498         unsigned                        radio_rx_rds_last_block;
499         struct v4l2_fh                  *radio_rx_rds_owner;
500
501         /* Radio transmitter */
502         unsigned                        radio_tx_freq;
503         unsigned                        radio_tx_subchans;
504         bool                            radio_tx_rds_controls;
505         unsigned                        radio_tx_rds_last_block;
506         struct v4l2_fh                  *radio_tx_rds_owner;
507
508         /* Shared between radio receiver and transmitter */
509         bool                            radio_rds_loop;
510         ktime_t                         radio_rds_init_time;
511
512         /* CEC */
513         struct cec_adapter              *cec_rx_adap;
514         struct cec_adapter              *cec_tx_adap[MAX_OUTPUTS];
515         struct workqueue_struct         *cec_workqueue;
516         spinlock_t                      cec_slock;
517         struct list_head                cec_work_list;
518         unsigned int                    cec_xfer_time_jiffies;
519         unsigned long                   cec_xfer_start_jiffies;
520         u8                              cec_output2bus_map[MAX_OUTPUTS];
521
522         /* CEC OSD String */
523         char                            osd[14];
524         unsigned long                   osd_jiffies;
525 };
526
527 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
528 {
529         return dev->input_type[dev->input] == WEBCAM;
530 }
531
532 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
533 {
534         return dev->input_type[dev->input] == TV;
535 }
536
537 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
538 {
539         return dev->input_type[dev->input] == SVID;
540 }
541
542 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
543 {
544         return dev->input_type[dev->input] == HDMI;
545 }
546
547 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
548 {
549         return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
550 }
551
552 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
553 {
554         return dev->output_type[dev->output] == SVID;
555 }
556
557 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
558 {
559         return dev->output_type[dev->output] == HDMI;
560 }
561
562 #endif