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