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 *routing_schemes[] = {
115 	[PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
116 	[PVR2_ROUTING_SCHEME_GOTVIEW] = &routing_defgv,
117 	[PVR2_ROUTING_SCHEME_AV400] = &routing_defav400,
118 };
119 
120 void pvr2_cx25840_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
121 {
122 	pvr2_trace(PVR2_TRACE_CHIPS, "subdev cx2584x update...");
123 	if (hdw->input_dirty || hdw->force_dirty) {
124 		enum cx25840_video_input vid_input;
125 		enum cx25840_audio_input aud_input;
126 		const struct routing_scheme *sp;
127 		unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
128 
129 		sp = (sid < ARRAY_SIZE(routing_schemes)) ?
130 			routing_schemes[sid] : NULL;
131 		if ((sp == NULL) ||
132 		    (hdw->input_val < 0) ||
133 		    (hdw->input_val >= sp->cnt)) {
134 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
135 				   "*** WARNING *** subdev cx2584x set_input: Invalid routing scheme (%u) and/or input (%d)",
136 				   sid, hdw->input_val);
137 			return;
138 		}
139 		vid_input = sp->def[hdw->input_val].vid;
140 		aud_input = sp->def[hdw->input_val].aud;
141 		pvr2_trace(PVR2_TRACE_CHIPS,
142 			   "subdev cx2584x set_input vid=0x%x aud=0x%x",
143 			   vid_input, aud_input);
144 		sd->ops->video->s_routing(sd, (u32)vid_input, 0, 0);
145 		sd->ops->audio->s_routing(sd, (u32)aud_input, 0, 0);
146 	}
147 }
148