xref: /OK3568_Linux_fs/kernel/drivers/media/pci/cx18/cx18-controls.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  cx18 ioctl control functions
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Derived from ivtv-controls.c
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "cx18-driver.h"
13*4882a593Smuzhiyun #include "cx18-cards.h"
14*4882a593Smuzhiyun #include "cx18-ioctl.h"
15*4882a593Smuzhiyun #include "cx18-audio.h"
16*4882a593Smuzhiyun #include "cx18-mailbox.h"
17*4882a593Smuzhiyun #include "cx18-controls.h"
18*4882a593Smuzhiyun 
cx18_s_stream_vbi_fmt(struct cx2341x_handler * cxhdl,u32 fmt)19*4882a593Smuzhiyun static int cx18_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
22*4882a593Smuzhiyun 	int type = cxhdl->stream_type->val;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	if (atomic_read(&cx->ana_capturing) > 0)
25*4882a593Smuzhiyun 		return -EBUSY;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
28*4882a593Smuzhiyun 	    !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
29*4882a593Smuzhiyun 	      type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
30*4882a593Smuzhiyun 	      type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
31*4882a593Smuzhiyun 		/* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
32*4882a593Smuzhiyun 		cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
33*4882a593Smuzhiyun 		CX18_DEBUG_INFO("disabled insertion of sliced VBI data into the MPEG stream\n");
34*4882a593Smuzhiyun 		return 0;
35*4882a593Smuzhiyun 	}
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	/* Allocate sliced VBI buffers if needed. */
38*4882a593Smuzhiyun 	if (cx->vbi.sliced_mpeg_data[0] == NULL) {
39*4882a593Smuzhiyun 		int i;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 		for (i = 0; i < CX18_VBI_FRAMES; i++) {
42*4882a593Smuzhiyun 			cx->vbi.sliced_mpeg_data[i] =
43*4882a593Smuzhiyun 			       kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
44*4882a593Smuzhiyun 			if (cx->vbi.sliced_mpeg_data[i] == NULL) {
45*4882a593Smuzhiyun 				while (--i >= 0) {
46*4882a593Smuzhiyun 					kfree(cx->vbi.sliced_mpeg_data[i]);
47*4882a593Smuzhiyun 					cx->vbi.sliced_mpeg_data[i] = NULL;
48*4882a593Smuzhiyun 				}
49*4882a593Smuzhiyun 				cx->vbi.insert_mpeg =
50*4882a593Smuzhiyun 						  V4L2_MPEG_STREAM_VBI_FMT_NONE;
51*4882a593Smuzhiyun 				CX18_WARN("Unable to allocate buffers for sliced VBI data insertion\n");
52*4882a593Smuzhiyun 				return -ENOMEM;
53*4882a593Smuzhiyun 			}
54*4882a593Smuzhiyun 		}
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	cx->vbi.insert_mpeg = fmt;
58*4882a593Smuzhiyun 	CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,when sliced VBI is enabled\n");
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/*
61*4882a593Smuzhiyun 	 * If our current settings have no lines set for capture, store a valid,
62*4882a593Smuzhiyun 	 * default set of service lines to capture, in our current settings.
63*4882a593Smuzhiyun 	 */
64*4882a593Smuzhiyun 	if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
65*4882a593Smuzhiyun 		if (cx->is_60hz)
66*4882a593Smuzhiyun 			cx->vbi.sliced_in->service_set =
67*4882a593Smuzhiyun 							V4L2_SLICED_CAPTION_525;
68*4882a593Smuzhiyun 		else
69*4882a593Smuzhiyun 			cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
70*4882a593Smuzhiyun 		cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 	return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
cx18_s_video_encoding(struct cx2341x_handler * cxhdl,u32 val)75*4882a593Smuzhiyun static int cx18_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
78*4882a593Smuzhiyun 	int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
79*4882a593Smuzhiyun 	struct v4l2_subdev_format format = {
80*4882a593Smuzhiyun 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
81*4882a593Smuzhiyun 	};
82*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *fmt = &format.format;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* fix videodecoder resolution */
85*4882a593Smuzhiyun 	fmt->width = cxhdl->width / (is_mpeg1 ? 2 : 1);
86*4882a593Smuzhiyun 	fmt->height = cxhdl->height;
87*4882a593Smuzhiyun 	fmt->code = MEDIA_BUS_FMT_FIXED;
88*4882a593Smuzhiyun 	v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
cx18_s_audio_sampling_freq(struct cx2341x_handler * cxhdl,u32 idx)92*4882a593Smuzhiyun static int cx18_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	static const u32 freqs[3] = { 44100, 48000, 32000 };
95*4882a593Smuzhiyun 	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* The audio clock of the digitizer must match the codec sample
98*4882a593Smuzhiyun 	   rate otherwise you get some very strange effects. */
99*4882a593Smuzhiyun 	if (idx < ARRAY_SIZE(freqs))
100*4882a593Smuzhiyun 		cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
101*4882a593Smuzhiyun 	return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
cx18_s_audio_mode(struct cx2341x_handler * cxhdl,u32 val)104*4882a593Smuzhiyun static int cx18_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	cx->dualwatch_stereo_mode = val;
109*4882a593Smuzhiyun 	return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun const struct cx2341x_handler_ops cx18_cxhdl_ops = {
113*4882a593Smuzhiyun 	.s_audio_mode = cx18_s_audio_mode,
114*4882a593Smuzhiyun 	.s_audio_sampling_freq = cx18_s_audio_sampling_freq,
115*4882a593Smuzhiyun 	.s_video_encoding = cx18_s_video_encoding,
116*4882a593Smuzhiyun 	.s_stream_vbi_fmt = cx18_s_stream_vbi_fmt,
117*4882a593Smuzhiyun };
118