1 /*
2  *
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  */
17 
18 /*
19 
20    This source file is specifically designed to interface with the
21    cx2584x, in kernels 2.6.16 or newer.
22 
23 */
24 
25 #include "pvrusb2-cx2584x-v4l.h"
26 
27 
28 #include "pvrusb2-hdw-internal.h"
29 #include "pvrusb2-debug.h"
30 #include <media/drv-intf/cx25840.h>
31 #include <linux/videodev2.h>
32 #include <media/v4l2-common.h>
33 #include <linux/errno.h>
34 
35 
36 struct routing_scheme_item {
37 	int vid;
38 	int aud;
39 };
40 
41 struct routing_scheme {
42 	const struct routing_scheme_item *def;
43 	unsigned int cnt;
44 };
45 
46 static const struct routing_scheme_item routing_scheme0[] = {
47 	[PVR2_CVAL_INPUT_TV] = {
48 		.vid = CX25840_COMPOSITE7,
49 		.aud = CX25840_AUDIO8,
50 	},
51 	[PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
52 		.vid = CX25840_COMPOSITE3,
53 		.aud = CX25840_AUDIO_SERIAL,
54 	},
55 	[PVR2_CVAL_INPUT_COMPOSITE] = {
56 		.vid = CX25840_COMPOSITE3,
57 		.aud = CX25840_AUDIO_SERIAL,
58 	},
59 	[PVR2_CVAL_INPUT_SVIDEO] = {
60 		.vid = CX25840_SVIDEO1,
61 		.aud = CX25840_AUDIO_SERIAL,
62 	},
63 };
64 
65 static const struct routing_scheme routing_def0 = {
66 	.def = routing_scheme0,
67 	.cnt = ARRAY_SIZE(routing_scheme0),
68 };
69 
70 /* Specific to gotview device */
71 static const struct routing_scheme_item routing_schemegv[] = {
72 	[PVR2_CVAL_INPUT_TV] = {
73 		.vid = CX25840_COMPOSITE2,
74 		.aud = CX25840_AUDIO5,
75 	},
76 	[PVR2_CVAL_INPUT_RADIO] = {
77 		/* line-in is used for radio and composite.  A GPIO is
78 		   used to switch between the two choices. */
79 		.vid = CX25840_COMPOSITE1,
80 		.aud = CX25840_AUDIO_SERIAL,
81 	},
82 	[PVR2_CVAL_INPUT_COMPOSITE] = {
83 		.vid = CX25840_COMPOSITE1,
84 		.aud = CX25840_AUDIO_SERIAL,
85 	},
86 	[PVR2_CVAL_INPUT_SVIDEO] = {
87 		.vid = (CX25840_SVIDEO_LUMA3|CX25840_SVIDEO_CHROMA4),
88 		.aud = CX25840_AUDIO_SERIAL,
89 	},
90 };
91 
92 static const struct routing_scheme routing_defgv = {
93 	.def = routing_schemegv,
94 	.cnt = ARRAY_SIZE(routing_schemegv),
95 };
96 
97 /* Specific to grabster av400 device */
98 static const struct routing_scheme_item routing_schemeav400[] = {
99 	[PVR2_CVAL_INPUT_COMPOSITE] = {
100 		.vid = CX25840_COMPOSITE1,
101 		.aud = CX25840_AUDIO_SERIAL,
102 	},
103 	[PVR2_CVAL_INPUT_SVIDEO] = {
104 		.vid = (CX25840_SVIDEO_LUMA2|CX25840_SVIDEO_CHROMA4),
105 		.aud = CX25840_AUDIO_SERIAL,
106 	},
107 };
108 
109 static const struct routing_scheme routing_defav400 = {
110 	.def = routing_schemeav400,
111 	.cnt = ARRAY_SIZE(routing_schemeav400),
112 };
113 
114 static const struct routing_scheme_item routing_scheme160xxx[] = {
115 	[PVR2_CVAL_INPUT_TV] = {
116 		.vid = CX25840_COMPOSITE7,
117 		.aud = CX25840_AUDIO8,
118 	},
119 	[PVR2_CVAL_INPUT_RADIO] = {
120 		.vid = CX25840_COMPOSITE4,
121 		.aud = CX25840_AUDIO6,
122 	},
123 	[PVR2_CVAL_INPUT_COMPOSITE] = {
124 		.vid = CX25840_COMPOSITE3,
125 		.aud = CX25840_AUDIO_SERIAL,
126 	},
127 	[PVR2_CVAL_INPUT_SVIDEO] = {
128 		.vid = CX25840_SVIDEO1,
129 		.aud = CX25840_AUDIO_SERIAL,
130 	},
131 };
132 
133 static const struct routing_scheme routing_def160xxx = {
134 	.def = routing_scheme160xxx,
135 	.cnt = ARRAY_SIZE(routing_scheme160xxx),
136 };
137 
138 static const struct routing_scheme *routing_schemes[] = {
139 	[PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
140 	[PVR2_ROUTING_SCHEME_GOTVIEW] = &routing_defgv,
141 	[PVR2_ROUTING_SCHEME_AV400] = &routing_defav400,
142 	[PVR2_ROUTING_SCHEME_HAUP160XXX] = &routing_def160xxx,
143 };
144 
145 void pvr2_cx25840_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
146 {
147 	pvr2_trace(PVR2_TRACE_CHIPS, "subdev cx2584x update...");
148 	if (hdw->input_dirty || hdw->force_dirty) {
149 		enum cx25840_video_input vid_input;
150 		enum cx25840_audio_input aud_input;
151 		const struct routing_scheme *sp;
152 		unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
153 
154 		sp = (sid < ARRAY_SIZE(routing_schemes)) ?
155 			routing_schemes[sid] : NULL;
156 		if ((sp == NULL) ||
157 		    (hdw->input_val < 0) ||
158 		    (hdw->input_val >= sp->cnt)) {
159 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
160 				   "*** WARNING *** subdev cx2584x set_input: Invalid routing scheme (%u) and/or input (%d)",
161 				   sid, hdw->input_val);
162 			return;
163 		}
164 		vid_input = sp->def[hdw->input_val].vid;
165 		aud_input = sp->def[hdw->input_val].aud;
166 		pvr2_trace(PVR2_TRACE_CHIPS,
167 			   "subdev cx2584x set_input vid=0x%x aud=0x%x",
168 			   vid_input, aud_input);
169 		sd->ops->video->s_routing(sd, (u32)vid_input, 0, 0);
170 		sd->ops->audio->s_routing(sd, (u32)aud_input, 0, 0);
171 	}
172 }
173