blob: 47e7f5dbd51680b93bfcccf37660c83a65d3d06e [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/*
24
25 This source file is specifically designed to interface with the
26 cx2584x, in kernels 2.6.16 or newer.
27
28*/
29
30#include "pvrusb2-cx2584x-v4l.h"
31#include "pvrusb2-video-v4l.h"
32#include "pvrusb2-i2c-cmd-v4l2.h"
33
34
35#include "pvrusb2-hdw-internal.h"
36#include "pvrusb2-debug.h"
37#include <media/cx25840.h>
38#include <linux/videodev2.h>
39#include <media/v4l2-common.h>
40#include <linux/errno.h>
41#include <linux/slab.h>
42
43struct pvr2_v4l_cx2584x {
44 struct pvr2_i2c_handler handler;
45 struct pvr2_decoder_ctrl ctrl;
46 struct pvr2_i2c_client *client;
47 struct pvr2_hdw *hdw;
48 unsigned long stale_mask;
49};
50
51
52static void set_input(struct pvr2_v4l_cx2584x *ctxt)
53{
54 struct pvr2_hdw *hdw = ctxt->hdw;
55 struct v4l2_routing route;
56 enum cx25840_video_input vid_input;
57 enum cx25840_audio_input aud_input;
58
59 memset(&route,0,sizeof(route));
60
61 switch(hdw->input_val) {
62 case PVR2_CVAL_INPUT_TV:
63 vid_input = CX25840_COMPOSITE7;
64 aud_input = CX25840_AUDIO8;
65 break;
66 case PVR2_CVAL_INPUT_COMPOSITE:
67 vid_input = CX25840_COMPOSITE3;
68 aud_input = CX25840_AUDIO_SERIAL;
69 break;
70 case PVR2_CVAL_INPUT_SVIDEO:
71 vid_input = CX25840_SVIDEO1;
72 aud_input = CX25840_AUDIO_SERIAL;
73 break;
74 case PVR2_CVAL_INPUT_RADIO:
75 default:
76 // Just set it to be composite input for now...
77 vid_input = CX25840_COMPOSITE3;
78 aud_input = CX25840_AUDIO_SERIAL;
79 break;
80 }
81
82 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x",
83 vid_input,aud_input);
84 route.input = (u32)vid_input;
85 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
86 route.input = (u32)aud_input;
87 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
88}
89
90
91static int check_input(struct pvr2_v4l_cx2584x *ctxt)
92{
93 struct pvr2_hdw *hdw = ctxt->hdw;
94 return hdw->input_dirty != 0;
95}
96
97
98static void set_audio(struct pvr2_v4l_cx2584x *ctxt)
99{
100 u32 val;
101 struct pvr2_hdw *hdw = ctxt->hdw;
102
103 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_audio %d",
104 hdw->srate_val);
105 switch (hdw->srate_val) {
106 default:
107 case PVR2_CVAL_SRATE_48:
108 val = 48000;
109 break;
110 case PVR2_CVAL_SRATE_44_1:
111 val = 44100;
112 break;
113 }
114 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
115}
116
117
118static int check_audio(struct pvr2_v4l_cx2584x *ctxt)
119{
120 struct pvr2_hdw *hdw = ctxt->hdw;
121 return hdw->srate_dirty != 0;
122}
123
124
125struct pvr2_v4l_cx2584x_ops {
126 void (*update)(struct pvr2_v4l_cx2584x *);
127 int (*check)(struct pvr2_v4l_cx2584x *);
128};
129
130
131static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = {
132 { .update = set_input, .check = check_input},
133 { .update = set_audio, .check = check_audio},
134};
135
136
137static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt)
138{
139 ctxt->client->handler = 0;
140 ctxt->hdw->decoder_ctrl = 0;
141 kfree(ctxt);
142}
143
144
145static int decoder_check(struct pvr2_v4l_cx2584x *ctxt)
146{
147 unsigned long msk;
148 unsigned int idx;
149
150 for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
151 idx++) {
152 msk = 1 << idx;
153 if (ctxt->stale_mask & msk) continue;
154 if (decoder_ops[idx].check(ctxt)) {
155 ctxt->stale_mask |= msk;
156 }
157 }
158 return ctxt->stale_mask != 0;
159}
160
161
162static void decoder_update(struct pvr2_v4l_cx2584x *ctxt)
163{
164 unsigned long msk;
165 unsigned int idx;
166
167 for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
168 idx++) {
169 msk = 1 << idx;
170 if (!(ctxt->stale_mask & msk)) continue;
171 ctxt->stale_mask &= ~msk;
172 decoder_ops[idx].update(ctxt);
173 }
174}
175
176
177static void decoder_enable(struct pvr2_v4l_cx2584x *ctxt,int fl)
178{
179 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_enable(%d)",fl);
180 pvr2_v4l2_cmd_stream(ctxt->client,fl);
181}
182
183
184static int decoder_detect(struct pvr2_i2c_client *cp)
185{
186 int ret;
187 /* Attempt to query the decoder - let's see if it will answer */
188 struct v4l2_queryctrl qc;
189
190 memset(&qc,0,sizeof(qc));
191
192 qc.id = V4L2_CID_BRIGHTNESS;
193
194 ret = pvr2_i2c_client_cmd(cp,VIDIOC_QUERYCTRL,&qc);
195 return ret == 0; /* Return true if it answered */
196}
197
198
199static int decoder_is_tuned(struct pvr2_v4l_cx2584x *ctxt)
200{
201 struct v4l2_tuner vt;
202 int ret;
203
204 memset(&vt,0,sizeof(vt));
205 ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
206 if (ret < 0) return -EINVAL;
207 return vt.signal ? 1 : 0;
208}
209
210
211static unsigned int decoder_describe(struct pvr2_v4l_cx2584x *ctxt,
212 char *buf,unsigned int cnt)
213{
214 return scnprintf(buf,cnt,"handler: pvrusb2-cx2584x-v4l");
215}
216
217
218static void decoder_reset(struct pvr2_v4l_cx2584x *ctxt)
219{
220 int ret;
221 ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_RESET,0);
222 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_reset (ret=%d)",ret);
223}
224
225
226const static struct pvr2_i2c_handler_functions hfuncs = {
227 .detach = (void (*)(void *))decoder_detach,
228 .check = (int (*)(void *))decoder_check,
229 .update = (void (*)(void *))decoder_update,
230 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
231};
232
233
234int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw,
235 struct pvr2_i2c_client *cp)
236{
237 struct pvr2_v4l_cx2584x *ctxt;
238
239 if (hdw->decoder_ctrl) return 0;
240 if (cp->handler) return 0;
241 if (!decoder_detect(cp)) return 0;
242
243 ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
244 if (!ctxt) return 0;
245 memset(ctxt,0,sizeof(*ctxt));
246
247 ctxt->handler.func_data = ctxt;
248 ctxt->handler.func_table = &hfuncs;
249 ctxt->ctrl.ctxt = ctxt;
250 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
251 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
252 ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned;
253 ctxt->ctrl.force_reset = (void (*)(void*))decoder_reset;
254 ctxt->client = cp;
255 ctxt->hdw = hdw;
256 ctxt->stale_mask = (1 << (sizeof(decoder_ops)/
257 sizeof(decoder_ops[0]))) - 1;
258 hdw->decoder_ctrl = &ctxt->ctrl;
259 cp->handler = &ctxt->handler;
260 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x cx2584x V4L2 handler set up",
261 cp->client->addr);
262 return !0;
263}
264
265
266
267
268/*
269 Stuff for Emacs to see, in order to encourage consistent editing style:
270 *** Local Variables: ***
271 *** mode: c ***
272 *** fill-column: 70 ***
273 *** tab-width: 8 ***
274 *** c-basic-offset: 8 ***
275 *** End: ***
276 */