blob: 814751deb218c950b3692e0a0e8c2226dfbc6c40 [file] [log] [blame]
Steven Toth443c12282009-05-09 21:17:28 -03001/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
Steven Toth9b8b0192010-07-31 14:39:44 -03004 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
Steven Toth443c12282009-05-09 21:17:28 -03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Steven Toth443c12282009-05-09 21:17:28 -030024
25#include "saa7164.h"
26
Steven Tothe8ce2f22010-07-31 16:06:06 -030027int saa7164_api_set_vbi_format(struct saa7164_port *port)
28{
29 struct saa7164_dev *dev = port->dev;
30 tmComResProbeCommit_t fmt, rsp;
31 int ret;
32
33 dprintk(DBGLVL_API, "%s(nr=%d)\n", __func__, port->nr);
34
35 fmt.bmHint = 0;
36 fmt.bFormatIndex = 1;
37 fmt.bFrameIndex = 1;
38
39 /* Probe, see if it can support this format */
40 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
41 SET_CUR, SAA_PROBE_CONTROL, sizeof(fmt), &fmt);
42 if (ret != SAA_OK)
43 printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
44
45 /* See of the format change was successful */
46 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
47 GET_CUR, SAA_PROBE_CONTROL, sizeof(rsp), &rsp);
48 if (ret != SAA_OK) {
49 printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
50 } else {
51 /* Compare requested vs received, should be same */
52 if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
53 /* Ask the device to select the negotiated format */
54 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
55 SET_CUR, SAA_COMMIT_CONTROL, sizeof(fmt), &fmt);
56 if (ret != SAA_OK)
57 printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
58 __func__, ret);
59
60 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
61 GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
62 if (ret != SAA_OK)
63 printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
64 __func__, ret);
65
66 if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0)
67 printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
68 __func__, ret);
69
70 dprintk(DBGLVL_API, "rsp.bmHint = 0x%x\n", rsp.bmHint);
71 dprintk(DBGLVL_API, "rsp.bFormatIndex = 0x%x\n", rsp.bFormatIndex);
72 dprintk(DBGLVL_API, "rsp.bFrameIndex = 0x%x\n", rsp.bFrameIndex);
73 } else
74 printk(KERN_ERR "%s() compare failed\n", __func__);
75 }
76
77 if (ret == SAA_OK)
78 dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
79
80 return ret;
81}
82
Steven Totheafea212010-07-31 14:48:45 -030083int saa7164_api_set_gop_size(struct saa7164_port *port)
84{
85 struct saa7164_dev *dev = port->dev;
86 tmComResEncVideoGopStructure_t gs;
87 int ret;
88
89 dprintk(DBGLVL_ENC, "%s()\n", __func__);
90
Steven Toth3ed43cf2010-07-31 14:58:35 -030091 gs.ucRefFrameDist = port->encoder_params.refdist;
Steven Toth5fa56cc2010-07-31 15:05:35 -030092 gs.ucGOPSize = port->encoder_params.gop_size;
Steven Totheafea212010-07-31 14:48:45 -030093 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
94 EU_VIDEO_GOP_STRUCTURE_CONTROL,
95 sizeof(gs), &gs);
96 if (ret != SAA_OK)
97 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
98
99 return ret;
100}
101
Steven Toth7615e432010-07-31 14:44:53 -0300102int saa7164_api_set_encoder(struct saa7164_port *port)
103{
104 struct saa7164_dev *dev = port->dev;
105 tmComResEncVideoBitRate_t vb;
106 tmComResEncAudioBitRate_t ab;
107 int ret;
108
Steven Totheafea212010-07-31 14:48:45 -0300109 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
110 port->hwcfg.sourceid);
Steven Toth7615e432010-07-31 14:44:53 -0300111
Steven Tothf91d0952010-07-31 15:03:31 -0300112 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
113 port->encoder_profile = EU_PROFILE_PS_DVD;
114 else
115 port->encoder_profile = EU_PROFILE_TS_HQ;
116
Steven Toth7615e432010-07-31 14:44:53 -0300117 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
118 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
119 if (ret != SAA_OK)
120 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
121
Steven Tothf6eeece2010-07-31 15:28:18 -0300122 /* Resolution */
123 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
124 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
125 if (ret != SAA_OK)
126 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
127
Steven Toth7615e432010-07-31 14:44:53 -0300128 /* Establish video bitrates */
Steven Toth2600d712010-07-31 14:51:30 -0300129 if (port->encoder_params.bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
130 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
131 else
132 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
Steven Toth7615e432010-07-31 14:44:53 -0300133 vb.dwVideoBitRate = port->encoder_params.bitrate;
Steven Toth968b11b2010-07-31 14:59:38 -0300134 vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
Steven Toth7615e432010-07-31 14:44:53 -0300135 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
136 EU_VIDEO_BIT_RATE_CONTROL, sizeof(tmComResEncVideoBitRate_t), &vb);
137 if (ret != SAA_OK)
138 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
139
140 /* Establish audio bitrates */
141 ab.ucAudioBitRateMode = 0;
142 ab.dwAudioBitRate = 384000;
143 ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
144 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
145 EU_AUDIO_BIT_RATE_CONTROL, sizeof(tmComResEncAudioBitRate_t), &ab);
146 if (ret != SAA_OK)
147 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
148
149 saa7164_api_set_aspect_ratio(port);
Steven Totheafea212010-07-31 14:48:45 -0300150 saa7164_api_set_gop_size(port);
Steven Toth7615e432010-07-31 14:44:53 -0300151
152 return ret;
153}
154
155int saa7164_api_get_encoder(struct saa7164_port *port)
156{
157 struct saa7164_dev *dev = port->dev;
158 tmComResEncVideoBitRate_t v;
159 tmComResEncAudioBitRate_t a;
160 tmComResEncVideoInputAspectRatio_t ar;
161 int ret;
162
163 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
164
165 port->encoder_profile = 0;
166 port->video_format = 0;
167 port->video_resolution = 0;
168 port->audio_format = 0;
169
170 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
171 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
172 if (ret != SAA_OK)
173 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
174
175 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
Steven Tothf6eeece2010-07-31 15:28:18 -0300176 EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8), &port->video_resolution);
177 if (ret != SAA_OK)
178 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
179
180 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
Steven Toth7615e432010-07-31 14:44:53 -0300181 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
182 if (ret != SAA_OK)
183 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
184
185 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
186 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
187 if (ret != SAA_OK)
188 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
189
190 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
191 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
192 if (ret != SAA_OK)
193 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
194
195 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
196 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
197 if (ret != SAA_OK)
198 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
199
200 /* Aspect Ratio */
201 ar.width = 0;
202 ar.height = 0;
203 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
204 EU_VIDEO_INPUT_ASPECT_CONTROL,
205 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
206 if (ret != SAA_OK)
207 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
208
209 dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
210 dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
211 dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
212 dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
213 dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n", v.ucVideoBitRateMode);
214 dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n", v.dwVideoBitRate);
215 dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n", v.dwVideoBitRatePeak);
216 dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n", a.ucAudioBitRateMode);
217 dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n", a.dwAudioBitRate);
218 dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n", a.dwAudioBitRatePeak);
219 dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n", ar.width, ar.height);
220
221 return ret;
222}
223
224int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
225{
226 struct saa7164_dev *dev = port->dev;
227 tmComResEncVideoInputAspectRatio_t ar;
228 int ret;
229
230 dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
231 port->encoder_params.ctl_aspect);
232
233 switch (port->encoder_params.ctl_aspect) {
234 case V4L2_MPEG_VIDEO_ASPECT_1x1:
235 ar.width = 1;
236 ar.height = 1;
237 break;
238 case V4L2_MPEG_VIDEO_ASPECT_4x3:
239 ar.width = 4;
240 ar.height = 3;
241 break;
242 case V4L2_MPEG_VIDEO_ASPECT_16x9:
243 ar.width = 16;
244 ar.height = 9;
245 break;
246 case V4L2_MPEG_VIDEO_ASPECT_221x100:
247 ar.width = 221;
248 ar.height = 100;
249 break;
250 default:
251 BUG();
252 }
253
254 dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
255 port->encoder_params.ctl_aspect,
256 ar.width, ar.height);
257
258 /* Aspect Ratio */
259 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
260 EU_VIDEO_INPUT_ASPECT_CONTROL,
261 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
262 if (ret != SAA_OK)
263 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
264
265 return ret;
266}
267
268int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
269{
270 struct saa7164_dev *dev = port->dev;
271 int ret;
272 u16 val;
273
274 if (ctl == PU_BRIGHTNESS_CONTROL)
275 val = port->ctl_brightness;
276 else
277 if (ctl == PU_CONTRAST_CONTROL)
278 val = port->ctl_contrast;
279 else
280 if (ctl == PU_HUE_CONTROL)
281 val = port->ctl_hue;
282 else
283 if (ctl == PU_SATURATION_CONTROL)
284 val = port->ctl_saturation;
285 else
286 if (ctl == PU_SHARPNESS_CONTROL)
287 val = port->ctl_sharpness;
288 else
289 return -EINVAL;
290
291 dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
292 __func__, port->encunit.vsourceid, ctl, val);
293
294 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
295 ctl, sizeof(u16), &val);
296 if (ret != SAA_OK)
297 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
298
299 return ret;
300}
301
302int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
303{
304 struct saa7164_dev *dev = port->dev;
305 int ret;
306 u16 val;
307
308 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
309 ctl, sizeof(u16), &val);
310 if (ret != SAA_OK) {
311 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
312 return ret;
313 }
314
315 dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
316 __func__, ctl, val);
317
318 if (ctl == PU_BRIGHTNESS_CONTROL)
319 port->ctl_brightness = val;
320 else
321 if (ctl == PU_CONTRAST_CONTROL)
322 port->ctl_contrast = val;
323 else
324 if (ctl == PU_HUE_CONTROL)
325 port->ctl_hue = val;
326 else
327 if (ctl == PU_SATURATION_CONTROL)
328 port->ctl_saturation = val;
329 else
330 if (ctl == PU_SHARPNESS_CONTROL)
331 port->ctl_sharpness = val;
332
333 return ret;
334}
335
336int saa7164_api_set_videomux(struct saa7164_port *port)
337{
338 struct saa7164_dev *dev = port->dev;
339 u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
340 int ret;
341
342 dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
343 __func__, port->mux_input, inputs[ port->mux_input - 1 ]);
344
345 /* Audio Mute */
346 ret = saa7164_api_audio_mute(port, 1);
347 if (ret != SAA_OK)
348 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
349
350 /* Video Mux */
351 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
352 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
353 if (ret != SAA_OK)
354 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
355 /* Audio Mux */
356 ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
357 SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[ port->mux_input - 1 ]);
358 if (ret != SAA_OK)
359 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
360 /* Audio UnMute */
361 ret = saa7164_api_audio_mute(port, 0);
362 if (ret != SAA_OK)
363 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
364
365 return ret;
366}
367
368int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
369{
370 struct saa7164_dev *dev = port->dev;
371 u8 v = mute;
372 int ret;
373
374 dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
375
376 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
377 MUTE_CONTROL, sizeof(u8), &v);
378 if (ret != SAA_OK)
379 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
380
381 return ret;
382}
383
384/* 0 = silence, 0xff = full */
385int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
386{
387 struct saa7164_dev *dev = port->dev;
388 s16 v, min, max;
389 int ret;
390
391 dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
392
393 /* Obtain the min/max ranges */
394 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
395 VOLUME_CONTROL, sizeof(u16), &min);
396 if (ret != SAA_OK)
397 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
398
399 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
400 VOLUME_CONTROL, sizeof(u16), &max);
401 if (ret != SAA_OK)
402 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
403
404 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
405 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
406 if (ret != SAA_OK)
407 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
408
409 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
410
411 v = level;
412 if (v < min)
413 v = min;
414 if (v > max)
415 v = max;
416
417 /* Left */
418 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
419 ( 0x01 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
420 if (ret != SAA_OK)
421 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
422
423 /* Right */
424 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
425 ( 0x02 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
426 if (ret != SAA_OK)
427 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
428
429 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
430 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
431 if (ret != SAA_OK)
432 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
433
434 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
435
436 return ret;
437}
438
439int saa7164_api_set_audio_std(struct saa7164_port *port)
440{
441 struct saa7164_dev *dev = port->dev;
442 tmComResAudioDefaults_t lvl;
443 tmComResTunerStandard_t tvaudio;
444 int ret;
445
446 dprintk(DBGLVL_API, "%s()\n", __func__);
447
448 /* Establish default levels */
449 lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
450 lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
451 lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
452 lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
453 lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
454 lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
455 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
456 AUDIO_DEFAULT_CONTROL, sizeof(tmComResAudioDefaults_t), &lvl);
457 if (ret != SAA_OK)
458 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
459
460 /* Manually select the appropriate TV audio standard */
461 if (port->encodernorm.id & V4L2_STD_NTSC) {
462 tvaudio.std = TU_STANDARD_NTSC_M;
463 tvaudio.country = 1;
464 } else {
465 tvaudio.std = TU_STANDARD_PAL_I;
466 tvaudio.country = 44;
467 }
468
469 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
470 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
471 if (ret != SAA_OK)
472 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
473 return ret;
474}
475
476int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
477{
478 struct saa7164_dev *dev = port->dev;
479 tmComResTunerStandardAuto_t p;
480 int ret;
481
482 dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
483
484 /* Disable TV Audio autodetect if not already set (buggy) */
485 if (autodetect)
486 p.mode = TU_STANDARD_AUTO;
487 else
488 p.mode = TU_STANDARD_MANUAL;
489 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
490 TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
491 if (ret != SAA_OK)
492 printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
493
494 return ret;
495}
496
497int saa7164_api_get_videomux(struct saa7164_port *port)
498{
499 struct saa7164_dev *dev = port->dev;
500 int ret;
501
502 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
503 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
504 if (ret != SAA_OK)
505 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
506
507 dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
508 __func__, port->mux_input);
509
510 return ret;
511}
512
513int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
514{
515 struct saa7164_dev *dev = port->dev;
516
517 u16 len = 0;
518 u8 buf[256];
519 int ret;
520 u8 mas;
521
Steven Tothe8ce2f22010-07-31 16:06:06 -0300522 dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
523 port->nr, port->type, val);
Steven Toth7615e432010-07-31 14:44:53 -0300524
525 if (port->nr == 0)
526 mas = 0xd0;
527 else
528 mas = 0xe0;
529
530 memset(buf, 0, sizeof(buf));
531
532 buf[0x00] = 0x04;
533 buf[0x01] = 0x00;
534 buf[0x02] = 0x00;
535 buf[0x03] = 0x00;
536
537 buf[0x04] = 0x04;
538 buf[0x05] = 0x00;
539 buf[0x06] = 0x00;
540 buf[0x07] = 0x00;
541
542 buf[0x08] = reg;
543 buf[0x09] = 0x26;
544 buf[0x0a] = mas;
545 buf[0x0b] = 0xb0;
546
547 buf[0x0c] = val;
548 buf[0x0d] = 0x00;
549 buf[0x0e] = 0x00;
550 buf[0x0f] = 0x00;
551
552 ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
553 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
554 if (ret != SAA_OK) {
555 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
556 return -EIO;
557 }
558
559 ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
560 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
561 if (ret != SAA_OK)
562 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
563
564 //saa7164_dumphex16(dev, buf, 16);
565
566 return ret == SAA_OK ? 0 : -EIO;
567}
568
569/* Disable the IF block AGC controls */
570int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
571{
572 struct saa7164_dev *dev = port->dev;
573 int ret = 0;
574 u8 agc_disable;
575
Steven Tothe8ce2f22010-07-31 16:06:06 -0300576 dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
Steven Toth7615e432010-07-31 14:44:53 -0300577
578 if (std & V4L2_STD_NTSC) {
579 dprintk(DBGLVL_API, " NTSC\n");
580 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
581 agc_disable = 0;
582 } else if (std & V4L2_STD_PAL_I) {
583 dprintk(DBGLVL_API, " PAL-I\n");
584 saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
585 agc_disable = 0;
586 } else if (std & V4L2_STD_PAL_M) {
587 dprintk(DBGLVL_API, " PAL-M\n");
588 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
589 agc_disable = 0;
590 } else if (std & V4L2_STD_PAL_N) {
591 dprintk(DBGLVL_API, " PAL-N\n");
592 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
593 agc_disable = 0;
594 } else if (std & V4L2_STD_PAL_Nc) {
595 dprintk(DBGLVL_API, " PAL-Nc\n");
596 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
597 agc_disable = 0;
598 } else if (std & V4L2_STD_PAL_B) {
599 dprintk(DBGLVL_API, " PAL-B\n");
600 saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
601 agc_disable = 0;
602 } else if (std & V4L2_STD_PAL_DK) {
603 dprintk(DBGLVL_API, " PAL-DK\n");
604 saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
605 agc_disable = 0;
606 } else if (std & V4L2_STD_SECAM_L) {
607 dprintk(DBGLVL_API, " SECAM-L\n");
608 saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
609 agc_disable = 0;
610 } else {
611 /* Unknown standard, assume DTV */
612 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
613 saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
614 agc_disable = 1;
615 }
616
617 saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
618 saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
619 saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
620 saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
621 msleep(100);
622 saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
623 msleep(100);
624
625 return ret;
626}
627
628/* Ensure the dif is in the correct state for the operating mode
629 * (analog / dtv). We only configure the diff through the analog encoder
630 * so when we're in digital mode we need to find the appropriate encoder
631 * and use it to configure the DIF.
632 */
633int saa7164_api_initialize_dif(struct saa7164_port *port)
634{
635 struct saa7164_dev *dev = port->dev;
636 struct saa7164_port *p = 0;
637 int ret = -EINVAL;
638 u32 std = 0;
639
Steven Tothe8ce2f22010-07-31 16:06:06 -0300640 dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
641 port->nr, port->type);
642
Steven Toth7615e432010-07-31 14:44:53 -0300643 if (port->type == SAA7164_MPEG_ENCODER) {
644 /* Pick any analog standard to init the diff.
645 * we'll come back during encoder_init'
646 * and set the correct standard if requried.
647 */
648 std = V4L2_STD_NTSC;
649 } else
650 if (port->type == SAA7164_MPEG_DVB) {
651 if (port->nr == SAA7164_PORT_TS1)
652 p = &dev->ports[ SAA7164_PORT_ENC1 ];
653 else
654 p = &dev->ports[ SAA7164_PORT_ENC2 ];
655 } else
Steven Tothe8ce2f22010-07-31 16:06:06 -0300656 if (port->type == SAA7164_MPEG_VBI) {
657 std = V4L2_STD_NTSC;
658 if (port->nr == SAA7164_PORT_VBI1)
659 p = &dev->ports[ SAA7164_PORT_ENC1 ];
660 else
661 p = &dev->ports[ SAA7164_PORT_ENC2 ];
662 } else
Steven Toth7615e432010-07-31 14:44:53 -0300663 BUG();
664
665 if (p)
666 ret = saa7164_api_configure_dif(p, std);
667
668 return ret;
669}
670
Steven Tothadd3f582010-07-31 14:43:07 -0300671int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
Steven Toth443c12282009-05-09 21:17:28 -0300672{
Steven Tothe8ce2f22010-07-31 16:06:06 -0300673 struct saa7164_dev *dev = port->dev;
674
Steven Toth443c12282009-05-09 21:17:28 -0300675 int ret;
676
Steven Tothe8ce2f22010-07-31 16:06:06 -0300677 dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
678 __func__, port->nr, port->hwcfg.unitid, mode);
679
Steven Toth443c12282009-05-09 21:17:28 -0300680 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
681 SAA_STATE_CONTROL, sizeof(mode), &mode);
682 if (ret != SAA_OK)
Steven Tothe8ce2f22010-07-31 16:06:06 -0300683 printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
684 __func__, port->nr, port->hwcfg.unitid, ret);
Steven Toth443c12282009-05-09 21:17:28 -0300685
686 return ret;
687}
688
689int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
690{
691 int ret;
692
693 ret = saa7164_cmd_send(dev, 0, GET_CUR,
694 GET_FW_VERSION_CONTROL, sizeof(u32), version);
695 if (ret != SAA_OK)
696 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
697
698 return ret;
699}
700
701int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
702{
703 u8 reg[] = { 0x0f, 0x00 };
704
705 if (buflen < 128)
706 return -ENOMEM;
707
708 /* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
709 /* TODO: Pull the details from the boards struct */
710 return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
711 &reg[0], 128, buf);
712}
713
Steven Toth443c12282009-05-09 21:17:28 -0300714
Steven Tothe8ce2f22010-07-31 16:06:06 -0300715int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
716 struct saa7164_port *port)
717{
718 tmComResVBIFormatDescrHeader_t *fmt = &port->vbi_fmt_ntsc;
719
720 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
721 dprintk(DBGLVL_API, " VideoStandard = 0x%x\n", fmt->VideoStandard);
722 dprintk(DBGLVL_API, " StartLine = %d\n", fmt->StartLine);
723 dprintk(DBGLVL_API, " EndLine = %d\n", fmt->EndLine);
724 dprintk(DBGLVL_API, " FieldRate = %d\n", fmt->FieldRate);
725 dprintk(DBGLVL_API, " bNumLines = %d\n", fmt->bNumLines);
726 dprintk(DBGLVL_API, " = VS_FORMAT_VBI (becomes dev->en[%d])\n",
727 port->nr);
728
729 return 0;
730}
731
Steven Toth443c12282009-05-09 21:17:28 -0300732int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
Steven Tothadd3f582010-07-31 14:43:07 -0300733 struct saa7164_port *port,
Steven Toth443c12282009-05-09 21:17:28 -0300734 tmComResTSFormatDescrHeader_t *tsfmt)
735{
736 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
737 dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
738 dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
739 dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
740 dprintk(DBGLVL_API, " bguid = (....)\n");
741
742 /* Cache the hardware configuration in the port */
743
744 port->bufcounter = port->hwcfg.BARLocation;
745 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
746 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
747 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
748 port->bufptr32l = port->hwcfg.BARLocation +
749 (4 * sizeof(u32)) +
750 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
751 port->bufptr32h = port->hwcfg.BARLocation +
752 (4 * sizeof(u32)) +
753 (sizeof(u32) * port->hwcfg.buffercount);
754 port->bufptr64 = port->hwcfg.BARLocation +
755 (4 * sizeof(u32)) +
756 (sizeof(u32) * port->hwcfg.buffercount);
757 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
758 port->hwcfg.BARLocation);
759
760 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
761 port->nr);
762
763 return 0;
764}
765
Steven Toth7615e432010-07-31 14:44:53 -0300766int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
767 struct saa7164_port *port,
768 tmComResPSFormatDescrHeader_t *fmt)
769{
770 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
771 dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
772 dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
773 dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
774
775 /* Cache the hardware configuration in the port */
776 /* TODO: CHECK THIS in the port config */
777 port->bufcounter = port->hwcfg.BARLocation;
778 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
779 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
780 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
781 port->bufptr32l = port->hwcfg.BARLocation +
782 (4 * sizeof(u32)) +
783 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
784 port->bufptr32h = port->hwcfg.BARLocation +
785 (4 * sizeof(u32)) +
786 (sizeof(u32) * port->hwcfg.buffercount);
787 port->bufptr64 = port->hwcfg.BARLocation +
788 (4 * sizeof(u32)) +
789 (sizeof(u32) * port->hwcfg.buffercount);
790 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
791 port->hwcfg.BARLocation);
792
793 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
794 port->nr);
795
796 return 0;
797}
798
Steven Toth443c12282009-05-09 21:17:28 -0300799int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
800{
Steven Toth7615e432010-07-31 14:44:53 -0300801 struct saa7164_port *tsport = 0;
802 struct saa7164_port *encport = 0;
Steven Tothe8ce2f22010-07-31 16:06:06 -0300803 struct saa7164_port *vbiport = 0;
Steven Toth443c12282009-05-09 21:17:28 -0300804 u32 idx, next_offset;
805 int i;
806 tmComResDescrHeader_t *hdr, *t;
807 tmComResExtDevDescrHeader_t *exthdr;
808 tmComResPathDescrHeader_t *pathhdr;
809 tmComResAntTermDescrHeader_t *anttermhdr;
810 tmComResTunerDescrHeader_t *tunerunithdr;
811 tmComResDMATermDescrHeader_t *vcoutputtermhdr;
812 tmComResTSFormatDescrHeader_t *tsfmt;
Steven Toth7615e432010-07-31 14:44:53 -0300813 tmComResPSFormatDescrHeader_t *psfmt;
814 tmComResSelDescrHeader_t *psel;
815 tmComResProcDescrHeader_t *pdh;
816 tmComResAFeatureDescrHeader_t *afd;
817 tmComResEncoderDescrHeader_t *edh;
Steven Tothe8ce2f22010-07-31 16:06:06 -0300818 tmComResVBIFormatDescrHeader_t *vbifmt;
Steven Toth443c12282009-05-09 21:17:28 -0300819 u32 currpath = 0;
820
821 dprintk(DBGLVL_API,
Steven Toth207b42c2009-05-09 21:30:05 -0300822 "%s(?,?,%d) sizeof(tmComResDescrHeader_t) = %d bytes\n",
823 __func__, len, (u32)sizeof(tmComResDescrHeader_t));
Steven Toth443c12282009-05-09 21:17:28 -0300824
825 for (idx = 0; idx < (len - sizeof(tmComResDescrHeader_t)); ) {
826
827 hdr = (tmComResDescrHeader_t *)(buf + idx);
828
829 if (hdr->type != CS_INTERFACE)
830 return SAA_ERR_NOT_SUPPORTED;
831
832 dprintk(DBGLVL_API, "@ 0x%x = \n", idx);
833 switch (hdr->subtype) {
834 case GENERAL_REQUEST:
835 dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
836 break;
837 case VC_TUNER_PATH:
838 dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
839 pathhdr = (tmComResPathDescrHeader_t *)(buf + idx);
840 dprintk(DBGLVL_API, " pathid = 0x%x\n",
841 pathhdr->pathid);
842 currpath = pathhdr->pathid;
843 break;
844 case VC_INPUT_TERMINAL:
845 dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
846 anttermhdr =
847 (tmComResAntTermDescrHeader_t *)(buf + idx);
848 dprintk(DBGLVL_API, " terminalid = 0x%x\n",
849 anttermhdr->terminalid);
850 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
851 anttermhdr->terminaltype);
852 switch (anttermhdr->terminaltype) {
853 case ITT_ANTENNA:
854 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
855 break;
856 case LINE_CONNECTOR:
857 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
858 break;
859 case SPDIF_CONNECTOR:
860 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
861 break;
862 case COMPOSITE_CONNECTOR:
863 dprintk(DBGLVL_API,
864 " = COMPOSITE_CONNECTOR\n");
865 break;
866 case SVIDEO_CONNECTOR:
867 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
868 break;
869 case COMPONENT_CONNECTOR:
870 dprintk(DBGLVL_API,
871 " = COMPONENT_CONNECTOR\n");
872 break;
873 case STANDARD_DMA:
874 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
875 break;
876 default:
877 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
878 anttermhdr->terminaltype);
879 }
880 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
881 anttermhdr->assocterminal);
882 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
883 anttermhdr->iterminal);
884 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
885 anttermhdr->controlsize);
886 break;
887 case VC_OUTPUT_TERMINAL:
888 dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
889 vcoutputtermhdr =
890 (tmComResDMATermDescrHeader_t *)(buf + idx);
891 dprintk(DBGLVL_API, " unitid = 0x%x\n",
892 vcoutputtermhdr->unitid);
893 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
894 vcoutputtermhdr->terminaltype);
895 switch (vcoutputtermhdr->terminaltype) {
896 case ITT_ANTENNA:
897 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
898 break;
899 case LINE_CONNECTOR:
900 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
901 break;
902 case SPDIF_CONNECTOR:
903 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
904 break;
905 case COMPOSITE_CONNECTOR:
906 dprintk(DBGLVL_API,
907 " = COMPOSITE_CONNECTOR\n");
908 break;
909 case SVIDEO_CONNECTOR:
910 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
911 break;
912 case COMPONENT_CONNECTOR:
913 dprintk(DBGLVL_API,
914 " = COMPONENT_CONNECTOR\n");
915 break;
916 case STANDARD_DMA:
917 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
918 break;
919 default:
920 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
921 vcoutputtermhdr->terminaltype);
922 }
923 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
924 vcoutputtermhdr->assocterminal);
925 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
926 vcoutputtermhdr->sourceid);
927 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
928 vcoutputtermhdr->iterminal);
929 dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
930 vcoutputtermhdr->BARLocation);
931 dprintk(DBGLVL_API, " flags = 0x%x\n",
932 vcoutputtermhdr->flags);
933 dprintk(DBGLVL_API, " interruptid = 0x%x\n",
934 vcoutputtermhdr->interruptid);
935 dprintk(DBGLVL_API, " buffercount = 0x%x\n",
936 vcoutputtermhdr->buffercount);
937 dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
938 vcoutputtermhdr->metadatasize);
939 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
940 vcoutputtermhdr->controlsize);
941 dprintk(DBGLVL_API, " numformats = 0x%x\n",
942 vcoutputtermhdr->numformats);
943
944 t = (tmComResDescrHeader_t *)
945 ((tmComResDMATermDescrHeader_t *)(buf + idx));
946 next_offset = idx + (vcoutputtermhdr->len);
947 for (i = 0; i < vcoutputtermhdr->numformats; i++) {
948 t = (tmComResDescrHeader_t *)
949 (buf + next_offset);
950 switch (t->subtype) {
951 case VS_FORMAT_MPEG2TS:
952 tsfmt =
953 (tmComResTSFormatDescrHeader_t *)t;
954 if (currpath == 1)
Steven Toth7615e432010-07-31 14:44:53 -0300955 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
Steven Toth443c12282009-05-09 21:17:28 -0300956 else
Steven Toth7615e432010-07-31 14:44:53 -0300957 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
958 memcpy(&tsport->hwcfg, vcoutputtermhdr,
Steven Toth443c12282009-05-09 21:17:28 -0300959 sizeof(*vcoutputtermhdr));
960 saa7164_api_configure_port_mpeg2ts(dev,
Steven Toth7615e432010-07-31 14:44:53 -0300961 tsport, tsfmt);
Steven Toth443c12282009-05-09 21:17:28 -0300962 break;
963 case VS_FORMAT_MPEG2PS:
Steven Toth7615e432010-07-31 14:44:53 -0300964 psfmt =
965 (tmComResPSFormatDescrHeader_t *)t;
966 if (currpath == 1)
967 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
968 else
969 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
970 memcpy(&encport->hwcfg, vcoutputtermhdr,
971 sizeof(*vcoutputtermhdr));
972 saa7164_api_configure_port_mpeg2ps(dev,
973 encport, psfmt);
Steven Toth443c12282009-05-09 21:17:28 -0300974 break;
975 case VS_FORMAT_VBI:
Steven Tothe8ce2f22010-07-31 16:06:06 -0300976 vbifmt =
977 (tmComResVBIFormatDescrHeader_t *)t;
978 if (currpath == 1)
979 vbiport = &dev->ports[ SAA7164_PORT_VBI1 ];
980 else
981 vbiport = &dev->ports[ SAA7164_PORT_VBI2 ];
982 memcpy(&vbiport->hwcfg, vcoutputtermhdr,
983 sizeof(*vcoutputtermhdr));
984 memcpy(&vbiport->vbi_fmt_ntsc, vbifmt, sizeof(*vbifmt));
985 saa7164_api_configure_port_vbi(dev,
986 vbiport);
Steven Toth443c12282009-05-09 21:17:28 -0300987 break;
988 case VS_FORMAT_RDS:
989 dprintk(DBGLVL_API,
990 " = VS_FORMAT_RDS\n");
991 break;
992 case VS_FORMAT_UNCOMPRESSED:
993 dprintk(DBGLVL_API,
994 " = VS_FORMAT_UNCOMPRESSED\n");
995 break;
996 case VS_FORMAT_TYPE:
997 dprintk(DBGLVL_API,
998 " = VS_FORMAT_TYPE\n");
999 break;
1000 default:
1001 dprintk(DBGLVL_API,
1002 " = undefined (0x%x)\n",
1003 t->subtype);
1004 }
1005 next_offset += t->len;
1006 }
1007
1008 break;
1009 case TUNER_UNIT:
1010 dprintk(DBGLVL_API, " TUNER_UNIT\n");
1011 tunerunithdr =
1012 (tmComResTunerDescrHeader_t *)(buf + idx);
1013 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1014 tunerunithdr->unitid);
1015 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1016 tunerunithdr->sourceid);
1017 dprintk(DBGLVL_API, " iunit = 0x%x\n",
1018 tunerunithdr->iunit);
1019 dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
1020 tunerunithdr->tuningstandards);
1021 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1022 tunerunithdr->controlsize);
1023 dprintk(DBGLVL_API, " controls = 0x%x\n",
1024 tunerunithdr->controls);
Steven Toth7615e432010-07-31 14:44:53 -03001025
1026 if (tunerunithdr->unitid == tunerunithdr->iunit) {
1027 if (currpath == 1)
1028 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1029 else
1030 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1031 memcpy(&encport->tunerunit, tunerunithdr,
1032 sizeof(tmComResTunerDescrHeader_t));
1033 dprintk(DBGLVL_API, " (becomes dev->enc[%d] tuner)\n", encport->nr);
1034 }
Steven Toth443c12282009-05-09 21:17:28 -03001035 break;
1036 case VC_SELECTOR_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -03001037 psel = (tmComResSelDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -03001038 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -03001039 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1040 psel->unitid);
1041 dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
1042 psel->nrinpins);
1043 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1044 psel->sourceid);
Steven Toth443c12282009-05-09 21:17:28 -03001045 break;
1046 case VC_PROCESSING_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -03001047 pdh = (tmComResProcDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -03001048 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -03001049 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1050 pdh->unitid);
1051 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1052 pdh->sourceid);
1053 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1054 pdh->controlsize);
1055 if (pdh->controlsize == 0x04) {
1056 if (currpath == 1)
1057 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1058 else
1059 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1060 memcpy(&encport->vidproc, pdh,
1061 sizeof(tmComResProcDescrHeader_t));
1062 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1063 }
Steven Toth443c12282009-05-09 21:17:28 -03001064 break;
1065 case FEATURE_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -03001066 afd = (tmComResAFeatureDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -03001067 dprintk(DBGLVL_API, " FEATURE_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -03001068 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1069 afd->unitid);
1070 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1071 afd->sourceid);
1072 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1073 afd->controlsize);
1074 if (currpath == 1)
1075 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1076 else
1077 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1078 memcpy(&encport->audfeat, afd,
1079 sizeof(tmComResAFeatureDescrHeader_t));
1080 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
Steven Toth443c12282009-05-09 21:17:28 -03001081 break;
1082 case ENCODER_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -03001083 edh = (tmComResEncoderDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -03001084 dprintk(DBGLVL_API, " ENCODER_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -03001085 dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
1086 dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
1087 dprintk(DBGLVL_API, " vsourceid = 0x%x\n", edh->vsourceid);
1088 dprintk(DBGLVL_API, " asourceid = 0x%x\n", edh->asourceid);
1089 dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
1090 if (edh->iunit == edh->unitid) {
1091 if (currpath == 1)
1092 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1093 else
1094 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1095 memcpy(&encport->encunit, edh,
1096 sizeof(tmComResEncoderDescrHeader_t));
1097 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1098 }
Steven Toth443c12282009-05-09 21:17:28 -03001099 break;
1100 case EXTENSION_UNIT:
1101 dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
1102 exthdr = (tmComResExtDevDescrHeader_t *)(buf + idx);
1103 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1104 exthdr->unitid);
1105 dprintk(DBGLVL_API, " deviceid = 0x%x\n",
1106 exthdr->deviceid);
1107 dprintk(DBGLVL_API, " devicetype = 0x%x\n",
1108 exthdr->devicetype);
1109 if (exthdr->devicetype & 0x1)
1110 dprintk(DBGLVL_API, " = Decoder Device\n");
1111 if (exthdr->devicetype & 0x2)
1112 dprintk(DBGLVL_API, " = GPIO Source\n");
1113 if (exthdr->devicetype & 0x4)
1114 dprintk(DBGLVL_API, " = Video Decoder\n");
1115 if (exthdr->devicetype & 0x8)
1116 dprintk(DBGLVL_API, " = Audio Decoder\n");
1117 if (exthdr->devicetype & 0x20)
1118 dprintk(DBGLVL_API, " = Crossbar\n");
1119 if (exthdr->devicetype & 0x40)
1120 dprintk(DBGLVL_API, " = Tuner\n");
1121 if (exthdr->devicetype & 0x80)
1122 dprintk(DBGLVL_API, " = IF PLL\n");
1123 if (exthdr->devicetype & 0x100)
1124 dprintk(DBGLVL_API, " = Demodulator\n");
1125 if (exthdr->devicetype & 0x200)
1126 dprintk(DBGLVL_API, " = RDS Decoder\n");
1127 if (exthdr->devicetype & 0x400)
1128 dprintk(DBGLVL_API, " = Encoder\n");
1129 if (exthdr->devicetype & 0x800)
1130 dprintk(DBGLVL_API, " = IR Decoder\n");
1131 if (exthdr->devicetype & 0x1000)
1132 dprintk(DBGLVL_API, " = EEPROM\n");
1133 if (exthdr->devicetype & 0x2000)
1134 dprintk(DBGLVL_API,
1135 " = VBI Decoder\n");
1136 if (exthdr->devicetype & 0x10000)
1137 dprintk(DBGLVL_API,
1138 " = Streaming Device\n");
1139 if (exthdr->devicetype & 0x20000)
1140 dprintk(DBGLVL_API,
1141 " = DRM Device\n");
1142 if (exthdr->devicetype & 0x40000000)
1143 dprintk(DBGLVL_API,
1144 " = Generic Device\n");
1145 if (exthdr->devicetype & 0x80000000)
1146 dprintk(DBGLVL_API,
1147 " = Config Space Device\n");
1148 dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
1149 exthdr->numgpiopins);
1150 dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
1151 exthdr->numgpiogroups);
1152 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1153 exthdr->controlsize);
Steven Toth7615e432010-07-31 14:44:53 -03001154 if (exthdr->devicetype & 0x80) {
1155 if (currpath == 1)
1156 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1157 else
1158 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1159 memcpy(&encport->ifunit, exthdr,
1160 sizeof(tmComResExtDevDescrHeader_t));
1161 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1162 }
Steven Toth443c12282009-05-09 21:17:28 -03001163 break;
1164 case PVC_INFRARED_UNIT:
1165 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1166 break;
1167 case DRM_UNIT:
1168 dprintk(DBGLVL_API, " DRM_UNIT\n");
1169 break;
1170 default:
1171 dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1172 }
1173
1174 dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
1175 dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
1176 dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
1177 dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
1178
1179 idx += hdr->len;
1180 }
1181
1182 return 0;
1183}
1184
1185int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1186{
1187 int ret;
1188 u32 buflen = 0;
1189 u8 *buf;
1190
1191 dprintk(DBGLVL_API, "%s()\n", __func__);
1192
1193 /* Get the total descriptor length */
1194 ret = saa7164_cmd_send(dev, 0, GET_LEN,
1195 GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1196 if (ret != SAA_OK)
1197 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1198
1199 dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1200 __func__, buflen);
1201
1202 /* Allocate enough storage for all of the descs */
1203 buf = kzalloc(buflen, GFP_KERNEL);
1204 if (buf == NULL)
1205 return SAA_ERR_NO_RESOURCES;
1206
1207 /* Retrieve them */
1208 ret = saa7164_cmd_send(dev, 0, GET_CUR,
1209 GET_DESCRIPTORS_CONTROL, buflen, buf);
1210 if (ret != SAA_OK) {
1211 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1212 goto out;
1213 }
1214
Ingo Molnarb1912a852009-09-21 15:23:45 -03001215 if (saa_debug & DBGLVL_API)
Steven Toth443c12282009-05-09 21:17:28 -03001216 saa7164_dumphex16(dev, buf, (buflen/16)*16);
1217
1218 saa7164_api_dump_subdevs(dev, buf, buflen);
1219
1220out:
1221 kfree(buf);
1222 return ret;
1223}
1224
1225int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1226 u32 datalen, u8 *data)
1227{
1228 struct saa7164_dev *dev = bus->dev;
1229 u16 len = 0;
1230 int unitid;
1231 u32 regval;
1232 u8 buf[256];
1233 int ret;
1234
1235 dprintk(DBGLVL_API, "%s()\n", __func__);
1236
1237 if (reglen > 4)
1238 return -EIO;
1239
1240 if (reglen == 1)
1241 regval = *(reg);
1242 else
1243 if (reglen == 2)
1244 regval = ((*(reg) << 8) || *(reg+1));
1245 else
1246 if (reglen == 3)
1247 regval = ((*(reg) << 16) | (*(reg+1) << 8) | *(reg+2));
1248 else
1249 if (reglen == 4)
1250 regval = ((*(reg) << 24) | (*(reg+1) << 16) |
1251 (*(reg+2) << 8) | *(reg+3));
1252
1253 /* Prepare the send buffer */
1254 /* Bytes 00-03 source register length
1255 * 04-07 source bytes to read
1256 * 08... register address
1257 */
1258 memset(buf, 0, sizeof(buf));
1259 memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
1260 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1261 *((u32 *)(buf + 1 * sizeof(u32))) = datalen;
1262
1263 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1264 if (unitid < 0) {
1265 printk(KERN_ERR
1266 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1267 __func__, addr);
1268 return -EIO;
1269 }
1270
1271 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1272 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1273 if (ret != SAA_OK) {
1274 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1275 return -EIO;
1276 }
1277
1278 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1279
Ingo Molnarb1912a852009-09-21 15:23:45 -03001280 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001281 saa7164_dumphex16(dev, buf, 2 * 16);
1282
1283 ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1284 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1285 if (ret != SAA_OK)
1286 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1287 else {
Ingo Molnarb1912a852009-09-21 15:23:45 -03001288 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001289 saa7164_dumphex16(dev, buf, sizeof(buf));
1290 memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1291 }
1292
1293 return ret == SAA_OK ? 0 : -EIO;
1294}
1295
1296/* For a given 8 bit i2c address device, write the buffer */
1297int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
1298 u8 *data)
1299{
1300 struct saa7164_dev *dev = bus->dev;
1301 u16 len = 0;
1302 int unitid;
1303 int reglen;
1304 u8 buf[256];
1305 int ret;
1306
1307 dprintk(DBGLVL_API, "%s()\n", __func__);
1308
1309 if ((datalen == 0) || (datalen > 232))
1310 return -EIO;
1311
1312 memset(buf, 0, sizeof(buf));
1313
1314 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1315 if (unitid < 0) {
1316 printk(KERN_ERR
1317 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1318 __func__, addr);
1319 return -EIO;
1320 }
1321
1322 reglen = saa7164_i2caddr_to_reglen(bus, addr);
Roel Kluinad695512010-01-03 14:04:42 -03001323 if (reglen < 0) {
Steven Toth443c12282009-05-09 21:17:28 -03001324 printk(KERN_ERR
1325 "%s() error, cannot translate regaddr to reglen\n",
1326 __func__);
1327 return -EIO;
1328 }
1329
1330 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1331 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1332 if (ret != SAA_OK) {
1333 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1334 return -EIO;
1335 }
1336
1337 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1338
1339 /* Prepare the send buffer */
1340 /* Bytes 00-03 dest register length
1341 * 04-07 dest bytes to write
1342 * 08... register address
1343 */
1344 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1345 *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1346 memcpy((buf + 2 * sizeof(u32)), data, datalen);
1347
Ingo Molnarb1912a852009-09-21 15:23:45 -03001348 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001349 saa7164_dumphex16(dev, buf, sizeof(buf));
1350
1351 ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1352 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1353 if (ret != SAA_OK)
1354 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1355
1356 return ret == SAA_OK ? 0 : -EIO;
1357}
1358
1359
1360int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1361 u8 pin, u8 state)
1362{
1363 int ret;
1364 tmComResGPIO_t t;
1365
1366 dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1367 __func__, unitid, pin, state);
1368
1369 if ((pin > 7) || (state > 2))
1370 return SAA_ERR_BAD_PARAMETER;
1371
1372 t.pin = pin;
1373 t.state = state;
1374
1375 ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1376 EXU_GPIO_CONTROL, sizeof(t), &t);
1377 if (ret != SAA_OK)
1378 printk(KERN_ERR "%s() error, ret = 0x%x\n",
1379 __func__, ret);
1380
1381 return ret;
1382}
1383
1384int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1385 u8 pin)
1386{
1387 return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1388}
1389
1390int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1391 u8 pin)
1392{
1393 return saa7164_api_modify_gpio(dev, unitid, pin, 0);
1394}
1395
1396
1397