blob: 9df3623a3e4c49405c394f9d163367f89b066bbe [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 cx2584x, in kernels 2.6.16 or newer.
26
27*/
28
29#include "pvrusb2-cx2584x-v4l.h"
30#include "pvrusb2-video-v4l.h"
31#include "pvrusb2-i2c-cmd-v4l2.h"
32
33
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-debug.h"
36#include <media/cx25840.h>
37#include <linux/videodev2.h>
38#include <media/v4l2-common.h>
39#include <linux/errno.h>
40#include <linux/slab.h>
41
Mike Iselyd8554972006-06-26 20:58:46 -030042
Mike Iselyf5174af2007-11-26 02:07:26 -030043struct routing_scheme_item {
44 int vid;
45 int aud;
46};
47
48struct routing_scheme {
49 const struct routing_scheme_item *def;
50 unsigned int cnt;
51};
52
53static const struct routing_scheme_item routing_scheme0[] = {
54 [PVR2_CVAL_INPUT_TV] = {
55 .vid = CX25840_COMPOSITE7,
56 .aud = CX25840_AUDIO8,
57 },
58 [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
59 .vid = CX25840_COMPOSITE3,
60 .aud = CX25840_AUDIO_SERIAL,
61 },
62 [PVR2_CVAL_INPUT_COMPOSITE] = {
63 .vid = CX25840_COMPOSITE3,
64 .aud = CX25840_AUDIO_SERIAL,
65 },
66 [PVR2_CVAL_INPUT_SVIDEO] = {
67 .vid = CX25840_SVIDEO1,
68 .aud = CX25840_AUDIO_SERIAL,
69 },
70};
71
Mike Isely9e2e3ae2007-11-26 02:14:23 -030072/* Specific to gotview device */
73static const struct routing_scheme_item routing_schemegv[] = {
74 [PVR2_CVAL_INPUT_TV] = {
75 .vid = CX25840_COMPOSITE2,
76 .aud = CX25840_AUDIO5,
77 },
Mike Isely1df59f02008-04-21 03:50:39 -030078 [PVR2_CVAL_INPUT_RADIO] = {
79 /* line-in is used for radio and composite. A GPIO is
80 used to switch between the two choices. */
Mike Isely9e2e3ae2007-11-26 02:14:23 -030081 .vid = CX25840_COMPOSITE1,
82 .aud = CX25840_AUDIO_SERIAL,
83 },
84 [PVR2_CVAL_INPUT_COMPOSITE] = {
85 .vid = CX25840_COMPOSITE1,
86 .aud = CX25840_AUDIO_SERIAL,
87 },
88 [PVR2_CVAL_INPUT_SVIDEO] = {
89 .vid = (CX25840_SVIDEO_LUMA3|CX25840_SVIDEO_CHROMA4),
90 .aud = CX25840_AUDIO_SERIAL,
91 },
92};
93
Mike Iselyf5174af2007-11-26 02:07:26 -030094static const struct routing_scheme routing_schemes[] = {
95 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
96 .def = routing_scheme0,
97 .cnt = ARRAY_SIZE(routing_scheme0),
98 },
Mike Isely9e2e3ae2007-11-26 02:14:23 -030099 [PVR2_ROUTING_SCHEME_GOTVIEW] = {
100 .def = routing_schemegv,
101 .cnt = ARRAY_SIZE(routing_schemegv),
102 },
Mike Iselyf5174af2007-11-26 02:07:26 -0300103};
104
Mike Isely634ba262009-03-07 00:54:02 -0300105struct pvr2_v4l_cx2584x {
106 struct pvr2_i2c_handler handler;
107 struct pvr2_decoder_ctrl ctrl;
108 struct pvr2_i2c_client *client;
109 struct pvr2_hdw *hdw;
110 unsigned long stale_mask;
111};
112
113
Mike Iselyd8554972006-06-26 20:58:46 -0300114static void set_input(struct pvr2_v4l_cx2584x *ctxt)
115{
116 struct pvr2_hdw *hdw = ctxt->hdw;
117 struct v4l2_routing route;
118 enum cx25840_video_input vid_input;
119 enum cx25840_audio_input aud_input;
Mike Iselyf5174af2007-11-26 02:07:26 -0300120 const struct routing_scheme *sp;
121 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
Mike Iselyd8554972006-06-26 20:58:46 -0300122
123 memset(&route,0,sizeof(route));
124
Mike Iselyf5174af2007-11-26 02:07:26 -0300125 if ((sid < ARRAY_SIZE(routing_schemes)) &&
Harvey Harrisona6a3a172008-04-28 16:50:03 -0700126 ((sp = routing_schemes + sid) != NULL) &&
Mike Iselyf5174af2007-11-26 02:07:26 -0300127 (hdw->input_val >= 0) &&
128 (hdw->input_val < sp->cnt)) {
129 vid_input = sp->def[hdw->input_val].vid;
130 aud_input = sp->def[hdw->input_val].aud;
131 } else {
132 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
133 "*** WARNING *** i2c cx2584x set_input:"
134 " Invalid routing scheme (%u) and/or input (%d)",
135 sid,hdw->input_val);
136 return;
Mike Iselyd8554972006-06-26 20:58:46 -0300137 }
138
139 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x",
140 vid_input,aud_input);
141 route.input = (u32)vid_input;
142 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
143 route.input = (u32)aud_input;
144 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
145}
146
147
148static int check_input(struct pvr2_v4l_cx2584x *ctxt)
149{
150 struct pvr2_hdw *hdw = ctxt->hdw;
151 return hdw->input_dirty != 0;
152}
153
154
155static void set_audio(struct pvr2_v4l_cx2584x *ctxt)
156{
157 u32 val;
158 struct pvr2_hdw *hdw = ctxt->hdw;
159
160 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_audio %d",
161 hdw->srate_val);
162 switch (hdw->srate_val) {
163 default:
Mike Iselyb30d2442006-06-25 20:05:01 -0300164 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
Mike Iselyd8554972006-06-26 20:58:46 -0300165 val = 48000;
166 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300167 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
Mike Iselyd8554972006-06-26 20:58:46 -0300168 val = 44100;
169 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300170 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
171 val = 32000;
172 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300173 }
174 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
175}
176
177
178static int check_audio(struct pvr2_v4l_cx2584x *ctxt)
179{
180 struct pvr2_hdw *hdw = ctxt->hdw;
181 return hdw->srate_dirty != 0;
182}
183
184
185struct pvr2_v4l_cx2584x_ops {
186 void (*update)(struct pvr2_v4l_cx2584x *);
187 int (*check)(struct pvr2_v4l_cx2584x *);
188};
189
190
191static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = {
192 { .update = set_input, .check = check_input},
193 { .update = set_audio, .check = check_audio},
194};
195
196
197static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt)
198{
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300199 ctxt->client->handler = NULL;
Mike Isely681c7392007-11-26 01:48:52 -0300200 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300201 kfree(ctxt);
202}
203
204
205static int decoder_check(struct pvr2_v4l_cx2584x *ctxt)
206{
207 unsigned long msk;
208 unsigned int idx;
209
Mike Isely27c7b712007-01-20 00:39:17 -0300210 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300211 msk = 1 << idx;
212 if (ctxt->stale_mask & msk) continue;
213 if (decoder_ops[idx].check(ctxt)) {
214 ctxt->stale_mask |= msk;
215 }
216 }
217 return ctxt->stale_mask != 0;
218}
219
220
221static void decoder_update(struct pvr2_v4l_cx2584x *ctxt)
222{
223 unsigned long msk;
224 unsigned int idx;
225
Mike Isely27c7b712007-01-20 00:39:17 -0300226 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300227 msk = 1 << idx;
228 if (!(ctxt->stale_mask & msk)) continue;
229 ctxt->stale_mask &= ~msk;
230 decoder_ops[idx].update(ctxt);
231 }
232}
233
234
235static void decoder_enable(struct pvr2_v4l_cx2584x *ctxt,int fl)
236{
237 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_enable(%d)",fl);
238 pvr2_v4l2_cmd_stream(ctxt->client,fl);
239}
240
241
242static int decoder_detect(struct pvr2_i2c_client *cp)
243{
244 int ret;
245 /* Attempt to query the decoder - let's see if it will answer */
246 struct v4l2_queryctrl qc;
247
248 memset(&qc,0,sizeof(qc));
249
250 qc.id = V4L2_CID_BRIGHTNESS;
251
252 ret = pvr2_i2c_client_cmd(cp,VIDIOC_QUERYCTRL,&qc);
253 return ret == 0; /* Return true if it answered */
254}
255
256
Mike Iselyd8554972006-06-26 20:58:46 -0300257static unsigned int decoder_describe(struct pvr2_v4l_cx2584x *ctxt,
258 char *buf,unsigned int cnt)
259{
260 return scnprintf(buf,cnt,"handler: pvrusb2-cx2584x-v4l");
261}
262
263
264static void decoder_reset(struct pvr2_v4l_cx2584x *ctxt)
265{
266 int ret;
Randy Dunlapc2625bf2006-10-29 11:12:27 -0300267 ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_RESET,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300268 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_reset (ret=%d)",ret);
269}
270
271
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100272static const struct pvr2_i2c_handler_functions hfuncs = {
Mike Iselyd8554972006-06-26 20:58:46 -0300273 .detach = (void (*)(void *))decoder_detach,
274 .check = (int (*)(void *))decoder_check,
275 .update = (void (*)(void *))decoder_update,
276 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
277};
278
279
280int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw,
281 struct pvr2_i2c_client *cp)
282{
283 struct pvr2_v4l_cx2584x *ctxt;
284
285 if (hdw->decoder_ctrl) return 0;
286 if (cp->handler) return 0;
287 if (!decoder_detect(cp)) return 0;
288
Mike Iselyca545f72007-01-20 00:37:11 -0300289 ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300290 if (!ctxt) return 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300291
292 ctxt->handler.func_data = ctxt;
293 ctxt->handler.func_table = &hfuncs;
294 ctxt->ctrl.ctxt = ctxt;
295 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
296 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
Mike Iselyd8554972006-06-26 20:58:46 -0300297 ctxt->ctrl.force_reset = (void (*)(void*))decoder_reset;
298 ctxt->client = cp;
299 ctxt->hdw = hdw;
Mike Isely27c7b712007-01-20 00:39:17 -0300300 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
Mike Isely681c7392007-11-26 01:48:52 -0300301 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
Mike Iselyd8554972006-06-26 20:58:46 -0300302 cp->handler = &ctxt->handler;
Mike Iselyff67c612006-11-19 20:50:31 -0300303 {
304 /*
305 Mike Isely <isely@pobox.com> 19-Nov-2006 - This bit
306 of nuttiness for cx25840 causes that module to
307 correctly set up its video scaling. This is really
308 a problem in the cx25840 module itself, but we work
309 around it here. The problem has not been seen in
310 ivtv because there VBI is supported and set up. We
311 don't do VBI here (at least not yet) and thus we
312 never attempted to even set it up.
313 */
314 struct v4l2_format fmt;
315 memset(&fmt,0,sizeof(fmt));
316 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
317 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_S_FMT,&fmt);
318 }
Mike Iselyd8554972006-06-26 20:58:46 -0300319 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x cx2584x V4L2 handler set up",
320 cp->client->addr);
321 return !0;
322}
323
324
Mike Isely634ba262009-03-07 00:54:02 -0300325void pvr2_cx25840_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
326{
327 if (hdw->input_dirty) {
328 struct v4l2_routing route;
329 enum cx25840_video_input vid_input;
330 enum cx25840_audio_input aud_input;
331 const struct routing_scheme *sp;
332 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
333
334 memset(&route, 0, sizeof(route));
335
336 if ((sid < ARRAY_SIZE(routing_schemes)) &&
337 ((sp = routing_schemes + sid) != NULL) &&
338 (hdw->input_val >= 0) &&
339 (hdw->input_val < sp->cnt)) {
340 vid_input = sp->def[hdw->input_val].vid;
341 aud_input = sp->def[hdw->input_val].aud;
342 } else {
343 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
344 "*** WARNING *** subdev cx2584x set_input:"
345 " Invalid routing scheme (%u)"
346 " and/or input (%d)",
347 sid, hdw->input_val);
348 return;
349 }
350
351 pvr2_trace(PVR2_TRACE_CHIPS,
352 "i2c cx2584x set_input vid=0x%x aud=0x%x",
353 vid_input, aud_input);
354 route.input = (u32)vid_input;
355 sd->ops->video->s_routing(sd, &route);
356 route.input = (u32)aud_input;
357 sd->ops->audio->s_routing(sd, &route);
358 }
359}
Mike Iselyd8554972006-06-26 20:58:46 -0300360
361
Mike Iselyacd92d42009-03-06 23:32:02 -0300362
Mike Iselyd8554972006-06-26 20:58:46 -0300363/*
364 Stuff for Emacs to see, in order to encourage consistent editing style:
365 *** Local Variables: ***
366 *** mode: c ***
367 *** fill-column: 70 ***
368 *** tab-width: 8 ***
369 *** c-basic-offset: 8 ***
370 *** End: ***
371 */