blob: ad28c5d3ad83ff08871cb559b8bb67f14909fce8 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
Mike Iselyd8554972006-06-26 20:58:46 -03003 *
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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23
24 This source file is specifically designed to interface with the
25 saa711x support that is available in the v4l available starting
26 with linux 2.6.15.
27
28*/
29
30#include "pvrusb2-video-v4l.h"
Mike Iselyd8554972006-06-26 20:58:46 -030031
32
Mike Isely6f956512009-03-07 00:43:26 -030033#include "pvrusb2-i2c-cmd-v4l2.h"
34
Mike Iselyd8554972006-06-26 20:58:46 -030035#include "pvrusb2-hdw-internal.h"
36#include "pvrusb2-debug.h"
37#include <linux/videodev2.h>
38#include <media/v4l2-common.h>
39#include <media/saa7115.h>
40#include <linux/errno.h>
41#include <linux/slab.h>
42
Mike Iselyf5174af2007-11-26 02:07:26 -030043struct routing_scheme {
44 const int *def;
45 unsigned int cnt;
46};
47
48
49static const int routing_scheme0[] = {
50 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
51 /* In radio mode, we mute the video, but point at one
52 spot just to stay consistent */
53 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
54 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
55 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
56};
57
58static const struct routing_scheme routing_schemes[] = {
59 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
60 .def = routing_scheme0,
61 .cnt = ARRAY_SIZE(routing_scheme0),
62 },
63};
64
Mike Isely6f956512009-03-07 00:43:26 -030065struct pvr2_v4l_decoder {
66 struct pvr2_i2c_handler handler;
67 struct pvr2_decoder_ctrl ctrl;
68 struct pvr2_i2c_client *client;
69 struct pvr2_hdw *hdw;
70 unsigned long stale_mask;
71};
72
73
74
Mike Iselyd8554972006-06-26 20:58:46 -030075static void set_input(struct pvr2_v4l_decoder *ctxt)
76{
77 struct pvr2_hdw *hdw = ctxt->hdw;
78 struct v4l2_routing route;
Mike Iselyf5174af2007-11-26 02:07:26 -030079 const struct routing_scheme *sp;
80 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
Mike Iselyd8554972006-06-26 20:58:46 -030081
82 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
Mike Iselyf5174af2007-11-26 02:07:26 -030083
84 if ((sid < ARRAY_SIZE(routing_schemes)) &&
Harvey Harrisona6a3a172008-04-28 16:50:03 -070085 ((sp = routing_schemes + sid) != NULL) &&
Mike Iselyf5174af2007-11-26 02:07:26 -030086 (hdw->input_val >= 0) &&
87 (hdw->input_val < sp->cnt)) {
88 route.input = sp->def[hdw->input_val];
89 } else {
90 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
91 "*** WARNING *** i2c v4l2 set_input:"
92 " Invalid routing scheme (%u) and/or input (%d)",
93 sid,hdw->input_val);
Mike Iselyd8554972006-06-26 20:58:46 -030094 return;
95 }
Mike Iselyf5174af2007-11-26 02:07:26 -030096
Mike Iselyd8554972006-06-26 20:58:46 -030097 route.output = 0;
98 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
99}
100
101
102static int check_input(struct pvr2_v4l_decoder *ctxt)
103{
104 struct pvr2_hdw *hdw = ctxt->hdw;
105 return hdw->input_dirty != 0;
106}
107
108
109static void set_audio(struct pvr2_v4l_decoder *ctxt)
110{
111 u32 val;
112 struct pvr2_hdw *hdw = ctxt->hdw;
113
114 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
115 hdw->srate_val);
116 switch (hdw->srate_val) {
117 default:
Mike Iselyb30d2442006-06-25 20:05:01 -0300118 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
Mike Iselyd8554972006-06-26 20:58:46 -0300119 val = 48000;
120 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300121 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
Mike Iselyd8554972006-06-26 20:58:46 -0300122 val = 44100;
123 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300124 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
125 val = 32000;
126 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300127 }
128 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
129}
130
131
132static int check_audio(struct pvr2_v4l_decoder *ctxt)
133{
134 struct pvr2_hdw *hdw = ctxt->hdw;
135 return hdw->srate_dirty != 0;
136}
137
138
139struct pvr2_v4l_decoder_ops {
140 void (*update)(struct pvr2_v4l_decoder *);
141 int (*check)(struct pvr2_v4l_decoder *);
142};
143
144
145static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
146 { .update = set_input, .check = check_input},
147 { .update = set_audio, .check = check_audio},
148};
149
150
151static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
152{
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300153 ctxt->client->handler = NULL;
Mike Isely681c7392007-11-26 01:48:52 -0300154 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300155 kfree(ctxt);
156}
157
158
159static int decoder_check(struct pvr2_v4l_decoder *ctxt)
160{
161 unsigned long msk;
162 unsigned int idx;
163
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300164 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300165 msk = 1 << idx;
166 if (ctxt->stale_mask & msk) continue;
167 if (decoder_ops[idx].check(ctxt)) {
168 ctxt->stale_mask |= msk;
169 }
170 }
171 return ctxt->stale_mask != 0;
172}
173
174
175static void decoder_update(struct pvr2_v4l_decoder *ctxt)
176{
177 unsigned long msk;
178 unsigned int idx;
179
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300180 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300181 msk = 1 << idx;
182 if (!(ctxt->stale_mask & msk)) continue;
183 ctxt->stale_mask &= ~msk;
184 decoder_ops[idx].update(ctxt);
185 }
186}
187
188
189static int decoder_detect(struct pvr2_i2c_client *cp)
190{
191 /* Attempt to query the decoder - let's see if it will answer */
192 struct v4l2_tuner vt;
193 int ret;
194
195 memset(&vt,0,sizeof(vt));
196 ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
197 return ret == 0; /* Return true if it answered */
198}
199
200
201static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
202{
203 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
204 pvr2_v4l2_cmd_stream(ctxt->client,fl);
205}
206
207
Mike Iselyd8554972006-06-26 20:58:46 -0300208static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
209{
210 return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
211}
212
213
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100214static const struct pvr2_i2c_handler_functions hfuncs = {
Mike Iselyd8554972006-06-26 20:58:46 -0300215 .detach = (void (*)(void *))decoder_detach,
216 .check = (int (*)(void *))decoder_check,
217 .update = (void (*)(void *))decoder_update,
218 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
219};
220
221
222int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
223 struct pvr2_i2c_client *cp)
224{
225 struct pvr2_v4l_decoder *ctxt;
226
227 if (hdw->decoder_ctrl) return 0;
228 if (cp->handler) return 0;
229 if (!decoder_detect(cp)) return 0;
230
Mike Iselyca545f72007-01-20 00:37:11 -0300231 ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300232 if (!ctxt) return 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300233
234 ctxt->handler.func_data = ctxt;
235 ctxt->handler.func_table = &hfuncs;
236 ctxt->ctrl.ctxt = ctxt;
237 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
238 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
Mike Iselyd8554972006-06-26 20:58:46 -0300239 ctxt->client = cp;
240 ctxt->hdw = hdw;
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300241 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
Mike Isely681c7392007-11-26 01:48:52 -0300242 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
Mike Iselyd8554972006-06-26 20:58:46 -0300243 cp->handler = &ctxt->handler;
244 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
245 cp->client->addr);
246 return !0;
247}
248
249
Mike Isely6f956512009-03-07 00:43:26 -0300250void pvr2_saa7115_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
251{
252 if (hdw->srate_dirty) {
253 u32 val;
254 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_audio %d",
255 hdw->srate_val);
256 switch (hdw->srate_val) {
257 default:
258 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
259 val = 48000;
260 break;
261 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
262 val = 44100;
263 break;
264 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
265 val = 32000;
266 break;
267 }
268 sd->ops->audio->s_clock_freq(sd, val);
269 }
270 if (hdw->input_dirty) {
271 struct v4l2_routing route;
272 const struct routing_scheme *sp;
273 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
274 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_input(%d)",
275 hdw->input_val);
276 if ((sid < ARRAY_SIZE(routing_schemes)) &&
277 ((sp = routing_schemes + sid) != NULL) &&
278 (hdw->input_val >= 0) &&
279 (hdw->input_val < sp->cnt)) {
280 route.input = sp->def[hdw->input_val];
281 } else {
282 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
283 "*** WARNING *** subdev v4l2 set_input:"
284 " Invalid routing scheme (%u) and/or input (%d)",
285 sid, hdw->input_val);
286 return;
287 }
288 route.output = 0;
289 sd->ops->video->s_routing(sd, &route);
290 }
291}
Mike Iselyd8554972006-06-26 20:58:46 -0300292
293
294/*
295 Stuff for Emacs to see, in order to encourage consistent editing style:
296 *** Local Variables: ***
297 *** mode: c ***
298 *** fill-column: 70 ***
299 *** tab-width: 8 ***
300 *** c-basic-offset: 8 ***
301 *** End: ***
302 */