V4L/DVB (10700): saa7115: don't access reg 0x87 if it is not present.
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-i2c-cmd-v4l2.c
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  *  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 #include "pvrusb2-i2c-cmd-v4l2.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-debug.h"
25 #include <linux/videodev2.h>
26 #include <media/v4l2-common.h>
27
28 static void execute_init(struct pvr2_hdw *hdw)
29 {
30         u32 dummy = 0;
31         pvr2_trace(PVR2_TRACE_CHIPS, "i2c v4l2 init");
32         pvr2_i2c_core_cmd(hdw, VIDIOC_INT_INIT, &dummy);
33 }
34
35
36 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_init = {
37         .update = execute_init,
38         .name = "v4l2_init",
39 };
40
41
42 static void set_standard(struct pvr2_hdw *hdw)
43 {
44         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_standard");
45
46         if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
47                 pvr2_i2c_core_cmd(hdw,AUDC_SET_RADIO,NULL);
48         } else {
49                 v4l2_std_id vs;
50                 vs = hdw->std_mask_cur;
51                 pvr2_i2c_core_cmd(hdw,VIDIOC_S_STD,&vs);
52         }
53         hdw->tuner_signal_stale = !0;
54         hdw->cropcap_stale = !0;
55 }
56
57
58 static int check_standard(struct pvr2_hdw *hdw)
59 {
60         return (hdw->input_dirty != 0) || (hdw->std_dirty != 0);
61 }
62
63
64 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_standard = {
65         .check = check_standard,
66         .update = set_standard,
67         .name = "v4l2_standard",
68 };
69
70
71 static void set_bcsh(struct pvr2_hdw *hdw)
72 {
73         struct v4l2_control ctrl;
74         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_bcsh"
75                    " b=%d c=%d s=%d h=%d",
76                    hdw->brightness_val,hdw->contrast_val,
77                    hdw->saturation_val,hdw->hue_val);
78         memset(&ctrl,0,sizeof(ctrl));
79         ctrl.id = V4L2_CID_BRIGHTNESS;
80         ctrl.value = hdw->brightness_val;
81         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
82         ctrl.id = V4L2_CID_CONTRAST;
83         ctrl.value = hdw->contrast_val;
84         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
85         ctrl.id = V4L2_CID_SATURATION;
86         ctrl.value = hdw->saturation_val;
87         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
88         ctrl.id = V4L2_CID_HUE;
89         ctrl.value = hdw->hue_val;
90         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
91 }
92
93
94 static int check_bcsh(struct pvr2_hdw *hdw)
95 {
96         return (hdw->brightness_dirty ||
97                 hdw->contrast_dirty ||
98                 hdw->saturation_dirty ||
99                 hdw->hue_dirty);
100 }
101
102
103 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_bcsh = {
104         .check = check_bcsh,
105         .update = set_bcsh,
106         .name = "v4l2_bcsh",
107 };
108
109
110 static void set_volume(struct pvr2_hdw *hdw)
111 {
112         struct v4l2_control ctrl;
113         pvr2_trace(PVR2_TRACE_CHIPS,
114                    "i2c v4l2 set_volume"
115                    "(vol=%d bal=%d bas=%d treb=%d mute=%d)",
116                    hdw->volume_val,
117                    hdw->balance_val,
118                    hdw->bass_val,
119                    hdw->treble_val,
120                    hdw->mute_val);
121         memset(&ctrl,0,sizeof(ctrl));
122         ctrl.id = V4L2_CID_AUDIO_MUTE;
123         ctrl.value = hdw->mute_val ? 1 : 0;
124         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
125         ctrl.id = V4L2_CID_AUDIO_VOLUME;
126         ctrl.value = hdw->volume_val;
127         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
128         ctrl.id = V4L2_CID_AUDIO_BALANCE;
129         ctrl.value = hdw->balance_val;
130         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
131         ctrl.id = V4L2_CID_AUDIO_BASS;
132         ctrl.value = hdw->bass_val;
133         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
134         ctrl.id = V4L2_CID_AUDIO_TREBLE;
135         ctrl.value = hdw->treble_val;
136         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
137 }
138
139
140 static int check_volume(struct pvr2_hdw *hdw)
141 {
142         return (hdw->volume_dirty ||
143                 hdw->balance_dirty ||
144                 hdw->bass_dirty ||
145                 hdw->treble_dirty ||
146                 hdw->mute_dirty);
147 }
148
149
150 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_volume = {
151         .check = check_volume,
152         .update = set_volume,
153         .name = "v4l2_volume",
154 };
155
156
157 static void set_audiomode(struct pvr2_hdw *hdw)
158 {
159         struct v4l2_tuner vt;
160         memset(&vt,0,sizeof(vt));
161         vt.audmode = hdw->audiomode_val;
162         pvr2_i2c_core_cmd(hdw,VIDIOC_S_TUNER,&vt);
163 }
164
165
166 static int check_audiomode(struct pvr2_hdw *hdw)
167 {
168         return (hdw->input_dirty ||
169                 hdw->audiomode_dirty);
170 }
171
172
173 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_audiomode = {
174         .check = check_audiomode,
175         .update = set_audiomode,
176         .name = "v4l2_audiomode",
177 };
178
179
180 static void set_frequency(struct pvr2_hdw *hdw)
181 {
182         unsigned long fv;
183         struct v4l2_frequency freq;
184         fv = pvr2_hdw_get_cur_freq(hdw);
185         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_freq(%lu)",fv);
186         if (hdw->tuner_signal_stale) {
187                 pvr2_i2c_core_status_poll(hdw);
188         }
189         memset(&freq,0,sizeof(freq));
190         if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
191                 // ((fv * 1000) / 62500)
192                 freq.frequency = (fv * 2) / 125;
193         } else {
194                 freq.frequency = fv / 62500;
195         }
196         /* tuner-core currently doesn't seem to care about this, but
197            let's set it anyway for completeness. */
198         if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
199                 freq.type = V4L2_TUNER_RADIO;
200         } else {
201                 freq.type = V4L2_TUNER_ANALOG_TV;
202         }
203         freq.tuner = 0;
204         pvr2_i2c_core_cmd(hdw,VIDIOC_S_FREQUENCY,&freq);
205 }
206
207
208 static int check_frequency(struct pvr2_hdw *hdw)
209 {
210         return hdw->freqDirty != 0;
211 }
212
213
214 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_frequency = {
215         .check = check_frequency,
216         .update = set_frequency,
217         .name = "v4l2_freq",
218 };
219
220
221 static void set_size(struct pvr2_hdw *hdw)
222 {
223         struct v4l2_format fmt;
224
225         memset(&fmt,0,sizeof(fmt));
226
227         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
228         fmt.fmt.pix.width = hdw->res_hor_val;
229         fmt.fmt.pix.height = hdw->res_ver_val;
230
231         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_size(%dx%d)",
232                            fmt.fmt.pix.width,fmt.fmt.pix.height);
233
234         pvr2_i2c_core_cmd(hdw,VIDIOC_S_FMT,&fmt);
235 }
236
237
238 static int check_size(struct pvr2_hdw *hdw)
239 {
240         return (hdw->res_hor_dirty || hdw->res_ver_dirty);
241 }
242
243
244 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_size = {
245         .check = check_size,
246         .update = set_size,
247         .name = "v4l2_size",
248 };
249
250
251 static void set_crop(struct pvr2_hdw *hdw)
252 {
253         struct v4l2_crop crop;
254
255         memset(&crop, 0, sizeof crop);
256         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
257         crop.c.left = hdw->cropl_val;
258         crop.c.top = hdw->cropt_val;
259         crop.c.height = hdw->croph_val;
260         crop.c.width = hdw->cropw_val;
261
262         pvr2_trace(PVR2_TRACE_CHIPS,
263                    "i2c v4l2 set_crop crop=%d:%d:%d:%d",
264                    crop.c.width, crop.c.height, crop.c.left, crop.c.top);
265
266         pvr2_i2c_core_cmd(hdw, VIDIOC_S_CROP, &crop);
267 }
268
269 static int check_crop(struct pvr2_hdw *hdw)
270 {
271         return (hdw->cropl_dirty || hdw->cropt_dirty ||
272                 hdw->cropw_dirty || hdw->croph_dirty);
273 }
274
275 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_crop = {
276         .check = check_crop,
277         .update = set_crop,
278         .name = "v4l2_crop",
279 };
280
281
282 static void do_log(struct pvr2_hdw *hdw)
283 {
284         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 do_log()");
285         pvr2_i2c_core_cmd(hdw,VIDIOC_LOG_STATUS,NULL);
286
287 }
288
289
290 static int check_log(struct pvr2_hdw *hdw)
291 {
292         return hdw->log_requested != 0;
293 }
294
295
296 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_log = {
297         .check = check_log,
298         .update = do_log,
299         .name = "v4l2_log",
300 };
301
302
303 void pvr2_v4l2_cmd_stream(struct pvr2_i2c_client *cp,int fl)
304 {
305         pvr2_i2c_client_cmd(cp,
306                             (fl ? VIDIOC_STREAMON : VIDIOC_STREAMOFF),NULL);
307 }
308
309
310 void pvr2_v4l2_cmd_status_poll(struct pvr2_i2c_client *cp)
311 {
312         int stat;
313         struct pvr2_hdw *hdw = cp->hdw;
314         if (hdw->cropcap_stale) {
315                 hdw->cropcap_info.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
316                 stat = pvr2_i2c_client_cmd(cp, VIDIOC_CROPCAP,
317                                            &hdw->cropcap_info);
318                 if (stat == 0) {
319                         /* Check was successful, so the data is no
320                            longer considered stale. */
321                         hdw->cropcap_stale = 0;
322                 }
323         }
324         pvr2_i2c_client_cmd(cp, VIDIOC_G_TUNER, &hdw->tuner_signal_info);
325 }
326
327
328 /*
329   Stuff for Emacs to see, in order to encourage consistent editing style:
330   *** Local Variables: ***
331   *** mode: c ***
332   *** fill-column: 70 ***
333   *** tab-width: 8 ***
334   *** c-basic-offset: 8 ***
335   *** End: ***
336   */