blob: 5f72bb854e60db88c7db4ce3e0d940c25c421c44 [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 Totheafea212010-07-31 14:48:45 -030027int saa7164_api_set_gop_size(struct saa7164_port *port)
28{
29 struct saa7164_dev *dev = port->dev;
30 tmComResEncVideoGopStructure_t gs;
31 int ret;
32
33 dprintk(DBGLVL_ENC, "%s()\n", __func__);
34
Steven Toth3ed43cf2010-07-31 14:58:35 -030035 gs.ucRefFrameDist = port->encoder_params.refdist;
Steven Totheafea212010-07-31 14:48:45 -030036 gs.ucGOPSize = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
37 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
38 EU_VIDEO_GOP_STRUCTURE_CONTROL,
39 sizeof(gs), &gs);
40 if (ret != SAA_OK)
41 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
42
43 return ret;
44}
45
Steven Toth7615e432010-07-31 14:44:53 -030046int saa7164_api_set_encoder(struct saa7164_port *port)
47{
48 struct saa7164_dev *dev = port->dev;
49 tmComResEncVideoBitRate_t vb;
50 tmComResEncAudioBitRate_t ab;
51 int ret;
52
Steven Totheafea212010-07-31 14:48:45 -030053 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
54 port->hwcfg.sourceid);
Steven Toth7615e432010-07-31 14:44:53 -030055
Steven Tothf91d0952010-07-31 15:03:31 -030056 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
57 port->encoder_profile = EU_PROFILE_PS_DVD;
58 else
59 port->encoder_profile = EU_PROFILE_TS_HQ;
60
Steven Toth7615e432010-07-31 14:44:53 -030061 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
62 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
63 if (ret != SAA_OK)
64 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
65
66 /* Establish video bitrates */
Steven Toth2600d712010-07-31 14:51:30 -030067 if (port->encoder_params.bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
68 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
69 else
70 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
Steven Toth7615e432010-07-31 14:44:53 -030071 vb.dwVideoBitRate = port->encoder_params.bitrate;
Steven Toth968b11b2010-07-31 14:59:38 -030072 vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
Steven Toth7615e432010-07-31 14:44:53 -030073 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
74 EU_VIDEO_BIT_RATE_CONTROL, sizeof(tmComResEncVideoBitRate_t), &vb);
75 if (ret != SAA_OK)
76 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
77
78 /* Establish audio bitrates */
79 ab.ucAudioBitRateMode = 0;
80 ab.dwAudioBitRate = 384000;
81 ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
82 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
83 EU_AUDIO_BIT_RATE_CONTROL, sizeof(tmComResEncAudioBitRate_t), &ab);
84 if (ret != SAA_OK)
85 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
86
87 saa7164_api_set_aspect_ratio(port);
Steven Totheafea212010-07-31 14:48:45 -030088 saa7164_api_set_gop_size(port);
Steven Toth7615e432010-07-31 14:44:53 -030089
90 return ret;
91}
92
93int saa7164_api_get_encoder(struct saa7164_port *port)
94{
95 struct saa7164_dev *dev = port->dev;
96 tmComResEncVideoBitRate_t v;
97 tmComResEncAudioBitRate_t a;
98 tmComResEncVideoInputAspectRatio_t ar;
99 int ret;
100
101 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__, port->hwcfg.sourceid);
102
103 port->encoder_profile = 0;
104 port->video_format = 0;
105 port->video_resolution = 0;
106 port->audio_format = 0;
107
108 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
109 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
110 if (ret != SAA_OK)
111 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
112
113 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
114 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
115 if (ret != SAA_OK)
116 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
117
118 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
119 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
120 if (ret != SAA_OK)
121 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
122
123 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
124 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
125 if (ret != SAA_OK)
126 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
127
128 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
129 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
130 if (ret != SAA_OK)
131 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
132
133 /* Aspect Ratio */
134 ar.width = 0;
135 ar.height = 0;
136 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
137 EU_VIDEO_INPUT_ASPECT_CONTROL,
138 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
139 if (ret != SAA_OK)
140 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
141
142 dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
143 dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
144 dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
145 dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
146 dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n", v.ucVideoBitRateMode);
147 dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n", v.dwVideoBitRate);
148 dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n", v.dwVideoBitRatePeak);
149 dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n", a.ucAudioBitRateMode);
150 dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n", a.dwAudioBitRate);
151 dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n", a.dwAudioBitRatePeak);
152 dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n", ar.width, ar.height);
153
154 return ret;
155}
156
157int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
158{
159 struct saa7164_dev *dev = port->dev;
160 tmComResEncVideoInputAspectRatio_t ar;
161 int ret;
162
163 dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
164 port->encoder_params.ctl_aspect);
165
166 switch (port->encoder_params.ctl_aspect) {
167 case V4L2_MPEG_VIDEO_ASPECT_1x1:
168 ar.width = 1;
169 ar.height = 1;
170 break;
171 case V4L2_MPEG_VIDEO_ASPECT_4x3:
172 ar.width = 4;
173 ar.height = 3;
174 break;
175 case V4L2_MPEG_VIDEO_ASPECT_16x9:
176 ar.width = 16;
177 ar.height = 9;
178 break;
179 case V4L2_MPEG_VIDEO_ASPECT_221x100:
180 ar.width = 221;
181 ar.height = 100;
182 break;
183 default:
184 BUG();
185 }
186
187 dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
188 port->encoder_params.ctl_aspect,
189 ar.width, ar.height);
190
191 /* Aspect Ratio */
192 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
193 EU_VIDEO_INPUT_ASPECT_CONTROL,
194 sizeof(tmComResEncVideoInputAspectRatio_t), &ar);
195 if (ret != SAA_OK)
196 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
197
198 return ret;
199}
200
201int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
202{
203 struct saa7164_dev *dev = port->dev;
204 int ret;
205 u16 val;
206
207 if (ctl == PU_BRIGHTNESS_CONTROL)
208 val = port->ctl_brightness;
209 else
210 if (ctl == PU_CONTRAST_CONTROL)
211 val = port->ctl_contrast;
212 else
213 if (ctl == PU_HUE_CONTROL)
214 val = port->ctl_hue;
215 else
216 if (ctl == PU_SATURATION_CONTROL)
217 val = port->ctl_saturation;
218 else
219 if (ctl == PU_SHARPNESS_CONTROL)
220 val = port->ctl_sharpness;
221 else
222 return -EINVAL;
223
224 dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
225 __func__, port->encunit.vsourceid, ctl, val);
226
227 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
228 ctl, sizeof(u16), &val);
229 if (ret != SAA_OK)
230 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
231
232 return ret;
233}
234
235int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
236{
237 struct saa7164_dev *dev = port->dev;
238 int ret;
239 u16 val;
240
241 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
242 ctl, sizeof(u16), &val);
243 if (ret != SAA_OK) {
244 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
245 return ret;
246 }
247
248 dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
249 __func__, ctl, val);
250
251 if (ctl == PU_BRIGHTNESS_CONTROL)
252 port->ctl_brightness = val;
253 else
254 if (ctl == PU_CONTRAST_CONTROL)
255 port->ctl_contrast = val;
256 else
257 if (ctl == PU_HUE_CONTROL)
258 port->ctl_hue = val;
259 else
260 if (ctl == PU_SATURATION_CONTROL)
261 port->ctl_saturation = val;
262 else
263 if (ctl == PU_SHARPNESS_CONTROL)
264 port->ctl_sharpness = val;
265
266 return ret;
267}
268
269int saa7164_api_set_videomux(struct saa7164_port *port)
270{
271 struct saa7164_dev *dev = port->dev;
272 u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
273 int ret;
274
275 dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
276 __func__, port->mux_input, inputs[ port->mux_input - 1 ]);
277
278 /* Audio Mute */
279 ret = saa7164_api_audio_mute(port, 1);
280 if (ret != SAA_OK)
281 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
282
283 /* Video Mux */
284 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
285 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
286 if (ret != SAA_OK)
287 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
288 /* Audio Mux */
289 ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
290 SU_INPUT_SELECT_CONTROL, sizeof(u8), &inputs[ port->mux_input - 1 ]);
291 if (ret != SAA_OK)
292 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
293 /* Audio UnMute */
294 ret = saa7164_api_audio_mute(port, 0);
295 if (ret != SAA_OK)
296 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
297
298 return ret;
299}
300
301int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
302{
303 struct saa7164_dev *dev = port->dev;
304 u8 v = mute;
305 int ret;
306
307 dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
308
309 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
310 MUTE_CONTROL, sizeof(u8), &v);
311 if (ret != SAA_OK)
312 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
313
314 return ret;
315}
316
317/* 0 = silence, 0xff = full */
318int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
319{
320 struct saa7164_dev *dev = port->dev;
321 s16 v, min, max;
322 int ret;
323
324 dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
325
326 /* Obtain the min/max ranges */
327 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
328 VOLUME_CONTROL, sizeof(u16), &min);
329 if (ret != SAA_OK)
330 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
331
332 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
333 VOLUME_CONTROL, sizeof(u16), &max);
334 if (ret != SAA_OK)
335 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
336
337 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
338 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
339 if (ret != SAA_OK)
340 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
341
342 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
343
344 v = level;
345 if (v < min)
346 v = min;
347 if (v > max)
348 v = max;
349
350 /* Left */
351 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
352 ( 0x01 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
353 if (ret != SAA_OK)
354 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
355
356 /* Right */
357 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
358 ( 0x02 << 8 ) | VOLUME_CONTROL, sizeof(s16), &v);
359 if (ret != SAA_OK)
360 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
361
362 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
363 ( 0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
364 if (ret != SAA_OK)
365 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
366
367 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__, level, min, max, v);
368
369 return ret;
370}
371
372int saa7164_api_set_audio_std(struct saa7164_port *port)
373{
374 struct saa7164_dev *dev = port->dev;
375 tmComResAudioDefaults_t lvl;
376 tmComResTunerStandard_t tvaudio;
377 int ret;
378
379 dprintk(DBGLVL_API, "%s()\n", __func__);
380
381 /* Establish default levels */
382 lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
383 lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
384 lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
385 lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
386 lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
387 lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
388 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
389 AUDIO_DEFAULT_CONTROL, sizeof(tmComResAudioDefaults_t), &lvl);
390 if (ret != SAA_OK)
391 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
392
393 /* Manually select the appropriate TV audio standard */
394 if (port->encodernorm.id & V4L2_STD_NTSC) {
395 tvaudio.std = TU_STANDARD_NTSC_M;
396 tvaudio.country = 1;
397 } else {
398 tvaudio.std = TU_STANDARD_PAL_I;
399 tvaudio.country = 44;
400 }
401
402 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
403 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
404 if (ret != SAA_OK)
405 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n", __func__, ret);
406 return ret;
407}
408
409int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
410{
411 struct saa7164_dev *dev = port->dev;
412 tmComResTunerStandardAuto_t p;
413 int ret;
414
415 dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
416
417 /* Disable TV Audio autodetect if not already set (buggy) */
418 if (autodetect)
419 p.mode = TU_STANDARD_AUTO;
420 else
421 p.mode = TU_STANDARD_MANUAL;
422 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
423 TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
424 if (ret != SAA_OK)
425 printk(KERN_ERR "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n", __func__, ret);
426
427 return ret;
428}
429
430int saa7164_api_get_videomux(struct saa7164_port *port)
431{
432 struct saa7164_dev *dev = port->dev;
433 int ret;
434
435 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
436 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
437 if (ret != SAA_OK)
438 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
439
440 dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
441 __func__, port->mux_input);
442
443 return ret;
444}
445
446int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
447{
448 struct saa7164_dev *dev = port->dev;
449
450 u16 len = 0;
451 u8 buf[256];
452 int ret;
453 u8 mas;
454
455 dprintk(DBGLVL_API, "%s()\n", __func__);
456
457 if (port->nr == 0)
458 mas = 0xd0;
459 else
460 mas = 0xe0;
461
462 memset(buf, 0, sizeof(buf));
463
464 buf[0x00] = 0x04;
465 buf[0x01] = 0x00;
466 buf[0x02] = 0x00;
467 buf[0x03] = 0x00;
468
469 buf[0x04] = 0x04;
470 buf[0x05] = 0x00;
471 buf[0x06] = 0x00;
472 buf[0x07] = 0x00;
473
474 buf[0x08] = reg;
475 buf[0x09] = 0x26;
476 buf[0x0a] = mas;
477 buf[0x0b] = 0xb0;
478
479 buf[0x0c] = val;
480 buf[0x0d] = 0x00;
481 buf[0x0e] = 0x00;
482 buf[0x0f] = 0x00;
483
484 ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
485 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
486 if (ret != SAA_OK) {
487 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
488 return -EIO;
489 }
490
491 ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
492 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
493 if (ret != SAA_OK)
494 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
495
496 //saa7164_dumphex16(dev, buf, 16);
497
498 return ret == SAA_OK ? 0 : -EIO;
499}
500
501/* Disable the IF block AGC controls */
502int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
503{
504 struct saa7164_dev *dev = port->dev;
505 int ret = 0;
506 u8 agc_disable;
507
508 dprintk(DBGLVL_API, "%s(%p, 0x%x)\n", __func__, port, std);
509
510 if (std & V4L2_STD_NTSC) {
511 dprintk(DBGLVL_API, " NTSC\n");
512 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
513 agc_disable = 0;
514 } else if (std & V4L2_STD_PAL_I) {
515 dprintk(DBGLVL_API, " PAL-I\n");
516 saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
517 agc_disable = 0;
518 } else if (std & V4L2_STD_PAL_M) {
519 dprintk(DBGLVL_API, " PAL-M\n");
520 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
521 agc_disable = 0;
522 } else if (std & V4L2_STD_PAL_N) {
523 dprintk(DBGLVL_API, " PAL-N\n");
524 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
525 agc_disable = 0;
526 } else if (std & V4L2_STD_PAL_Nc) {
527 dprintk(DBGLVL_API, " PAL-Nc\n");
528 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
529 agc_disable = 0;
530 } else if (std & V4L2_STD_PAL_B) {
531 dprintk(DBGLVL_API, " PAL-B\n");
532 saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
533 agc_disable = 0;
534 } else if (std & V4L2_STD_PAL_DK) {
535 dprintk(DBGLVL_API, " PAL-DK\n");
536 saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
537 agc_disable = 0;
538 } else if (std & V4L2_STD_SECAM_L) {
539 dprintk(DBGLVL_API, " SECAM-L\n");
540 saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
541 agc_disable = 0;
542 } else {
543 /* Unknown standard, assume DTV */
544 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
545 saa7164_api_set_dif(port, 0x00, 0x80); /* Undefined Video Standard */
546 agc_disable = 1;
547 }
548
549 saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
550 saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
551 saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
552 saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
553 msleep(100);
554 saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
555 msleep(100);
556
557 return ret;
558}
559
560/* Ensure the dif is in the correct state for the operating mode
561 * (analog / dtv). We only configure the diff through the analog encoder
562 * so when we're in digital mode we need to find the appropriate encoder
563 * and use it to configure the DIF.
564 */
565int saa7164_api_initialize_dif(struct saa7164_port *port)
566{
567 struct saa7164_dev *dev = port->dev;
568 struct saa7164_port *p = 0;
569 int ret = -EINVAL;
570 u32 std = 0;
571
572 if (port->type == SAA7164_MPEG_ENCODER) {
573 /* Pick any analog standard to init the diff.
574 * we'll come back during encoder_init'
575 * and set the correct standard if requried.
576 */
577 std = V4L2_STD_NTSC;
578 } else
579 if (port->type == SAA7164_MPEG_DVB) {
580 if (port->nr == SAA7164_PORT_TS1)
581 p = &dev->ports[ SAA7164_PORT_ENC1 ];
582 else
583 p = &dev->ports[ SAA7164_PORT_ENC2 ];
584 } else
585 BUG();
586
587 if (p)
588 ret = saa7164_api_configure_dif(p, std);
589
590 return ret;
591}
592
Steven Tothadd3f582010-07-31 14:43:07 -0300593int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
Steven Toth443c12282009-05-09 21:17:28 -0300594{
595 int ret;
596
597 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
598 SAA_STATE_CONTROL, sizeof(mode), &mode);
599 if (ret != SAA_OK)
600 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
601
602 return ret;
603}
604
605int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
606{
607 int ret;
608
609 ret = saa7164_cmd_send(dev, 0, GET_CUR,
610 GET_FW_VERSION_CONTROL, sizeof(u32), version);
611 if (ret != SAA_OK)
612 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
613
614 return ret;
615}
616
617int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
618{
619 u8 reg[] = { 0x0f, 0x00 };
620
621 if (buflen < 128)
622 return -ENOMEM;
623
624 /* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
625 /* TODO: Pull the details from the boards struct */
626 return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
627 &reg[0], 128, buf);
628}
629
Steven Toth443c12282009-05-09 21:17:28 -0300630
631int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
Steven Tothadd3f582010-07-31 14:43:07 -0300632 struct saa7164_port *port,
Steven Toth443c12282009-05-09 21:17:28 -0300633 tmComResTSFormatDescrHeader_t *tsfmt)
634{
635 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
636 dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
637 dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
638 dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
639 dprintk(DBGLVL_API, " bguid = (....)\n");
640
641 /* Cache the hardware configuration in the port */
642
643 port->bufcounter = port->hwcfg.BARLocation;
644 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
645 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
646 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
647 port->bufptr32l = port->hwcfg.BARLocation +
648 (4 * sizeof(u32)) +
649 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
650 port->bufptr32h = port->hwcfg.BARLocation +
651 (4 * sizeof(u32)) +
652 (sizeof(u32) * port->hwcfg.buffercount);
653 port->bufptr64 = port->hwcfg.BARLocation +
654 (4 * sizeof(u32)) +
655 (sizeof(u32) * port->hwcfg.buffercount);
656 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
657 port->hwcfg.BARLocation);
658
659 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
660 port->nr);
661
662 return 0;
663}
664
Steven Toth7615e432010-07-31 14:44:53 -0300665int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
666 struct saa7164_port *port,
667 tmComResPSFormatDescrHeader_t *fmt)
668{
669 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
670 dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
671 dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
672 dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
673
674 /* Cache the hardware configuration in the port */
675 /* TODO: CHECK THIS in the port config */
676 port->bufcounter = port->hwcfg.BARLocation;
677 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
678 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
679 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
680 port->bufptr32l = port->hwcfg.BARLocation +
681 (4 * sizeof(u32)) +
682 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
683 port->bufptr32h = port->hwcfg.BARLocation +
684 (4 * sizeof(u32)) +
685 (sizeof(u32) * port->hwcfg.buffercount);
686 port->bufptr64 = port->hwcfg.BARLocation +
687 (4 * sizeof(u32)) +
688 (sizeof(u32) * port->hwcfg.buffercount);
689 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
690 port->hwcfg.BARLocation);
691
692 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
693 port->nr);
694
695 return 0;
696}
697
Steven Toth443c12282009-05-09 21:17:28 -0300698int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
699{
Steven Toth7615e432010-07-31 14:44:53 -0300700 struct saa7164_port *tsport = 0;
701 struct saa7164_port *encport = 0;
Steven Toth443c12282009-05-09 21:17:28 -0300702 u32 idx, next_offset;
703 int i;
704 tmComResDescrHeader_t *hdr, *t;
705 tmComResExtDevDescrHeader_t *exthdr;
706 tmComResPathDescrHeader_t *pathhdr;
707 tmComResAntTermDescrHeader_t *anttermhdr;
708 tmComResTunerDescrHeader_t *tunerunithdr;
709 tmComResDMATermDescrHeader_t *vcoutputtermhdr;
710 tmComResTSFormatDescrHeader_t *tsfmt;
Steven Toth7615e432010-07-31 14:44:53 -0300711 tmComResPSFormatDescrHeader_t *psfmt;
712 tmComResSelDescrHeader_t *psel;
713 tmComResProcDescrHeader_t *pdh;
714 tmComResAFeatureDescrHeader_t *afd;
715 tmComResEncoderDescrHeader_t *edh;
Steven Toth443c12282009-05-09 21:17:28 -0300716 u32 currpath = 0;
717
718 dprintk(DBGLVL_API,
Steven Toth207b42c2009-05-09 21:30:05 -0300719 "%s(?,?,%d) sizeof(tmComResDescrHeader_t) = %d bytes\n",
720 __func__, len, (u32)sizeof(tmComResDescrHeader_t));
Steven Toth443c12282009-05-09 21:17:28 -0300721
722 for (idx = 0; idx < (len - sizeof(tmComResDescrHeader_t)); ) {
723
724 hdr = (tmComResDescrHeader_t *)(buf + idx);
725
726 if (hdr->type != CS_INTERFACE)
727 return SAA_ERR_NOT_SUPPORTED;
728
729 dprintk(DBGLVL_API, "@ 0x%x = \n", idx);
730 switch (hdr->subtype) {
731 case GENERAL_REQUEST:
732 dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
733 break;
734 case VC_TUNER_PATH:
735 dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
736 pathhdr = (tmComResPathDescrHeader_t *)(buf + idx);
737 dprintk(DBGLVL_API, " pathid = 0x%x\n",
738 pathhdr->pathid);
739 currpath = pathhdr->pathid;
740 break;
741 case VC_INPUT_TERMINAL:
742 dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
743 anttermhdr =
744 (tmComResAntTermDescrHeader_t *)(buf + idx);
745 dprintk(DBGLVL_API, " terminalid = 0x%x\n",
746 anttermhdr->terminalid);
747 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
748 anttermhdr->terminaltype);
749 switch (anttermhdr->terminaltype) {
750 case ITT_ANTENNA:
751 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
752 break;
753 case LINE_CONNECTOR:
754 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
755 break;
756 case SPDIF_CONNECTOR:
757 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
758 break;
759 case COMPOSITE_CONNECTOR:
760 dprintk(DBGLVL_API,
761 " = COMPOSITE_CONNECTOR\n");
762 break;
763 case SVIDEO_CONNECTOR:
764 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
765 break;
766 case COMPONENT_CONNECTOR:
767 dprintk(DBGLVL_API,
768 " = COMPONENT_CONNECTOR\n");
769 break;
770 case STANDARD_DMA:
771 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
772 break;
773 default:
774 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
775 anttermhdr->terminaltype);
776 }
777 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
778 anttermhdr->assocterminal);
779 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
780 anttermhdr->iterminal);
781 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
782 anttermhdr->controlsize);
783 break;
784 case VC_OUTPUT_TERMINAL:
785 dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
786 vcoutputtermhdr =
787 (tmComResDMATermDescrHeader_t *)(buf + idx);
788 dprintk(DBGLVL_API, " unitid = 0x%x\n",
789 vcoutputtermhdr->unitid);
790 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
791 vcoutputtermhdr->terminaltype);
792 switch (vcoutputtermhdr->terminaltype) {
793 case ITT_ANTENNA:
794 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
795 break;
796 case LINE_CONNECTOR:
797 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
798 break;
799 case SPDIF_CONNECTOR:
800 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
801 break;
802 case COMPOSITE_CONNECTOR:
803 dprintk(DBGLVL_API,
804 " = COMPOSITE_CONNECTOR\n");
805 break;
806 case SVIDEO_CONNECTOR:
807 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
808 break;
809 case COMPONENT_CONNECTOR:
810 dprintk(DBGLVL_API,
811 " = COMPONENT_CONNECTOR\n");
812 break;
813 case STANDARD_DMA:
814 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
815 break;
816 default:
817 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
818 vcoutputtermhdr->terminaltype);
819 }
820 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
821 vcoutputtermhdr->assocterminal);
822 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
823 vcoutputtermhdr->sourceid);
824 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
825 vcoutputtermhdr->iterminal);
826 dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
827 vcoutputtermhdr->BARLocation);
828 dprintk(DBGLVL_API, " flags = 0x%x\n",
829 vcoutputtermhdr->flags);
830 dprintk(DBGLVL_API, " interruptid = 0x%x\n",
831 vcoutputtermhdr->interruptid);
832 dprintk(DBGLVL_API, " buffercount = 0x%x\n",
833 vcoutputtermhdr->buffercount);
834 dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
835 vcoutputtermhdr->metadatasize);
836 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
837 vcoutputtermhdr->controlsize);
838 dprintk(DBGLVL_API, " numformats = 0x%x\n",
839 vcoutputtermhdr->numformats);
840
841 t = (tmComResDescrHeader_t *)
842 ((tmComResDMATermDescrHeader_t *)(buf + idx));
843 next_offset = idx + (vcoutputtermhdr->len);
844 for (i = 0; i < vcoutputtermhdr->numformats; i++) {
845 t = (tmComResDescrHeader_t *)
846 (buf + next_offset);
847 switch (t->subtype) {
848 case VS_FORMAT_MPEG2TS:
849 tsfmt =
850 (tmComResTSFormatDescrHeader_t *)t;
851 if (currpath == 1)
Steven Toth7615e432010-07-31 14:44:53 -0300852 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
Steven Toth443c12282009-05-09 21:17:28 -0300853 else
Steven Toth7615e432010-07-31 14:44:53 -0300854 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
855 memcpy(&tsport->hwcfg, vcoutputtermhdr,
Steven Toth443c12282009-05-09 21:17:28 -0300856 sizeof(*vcoutputtermhdr));
857 saa7164_api_configure_port_mpeg2ts(dev,
Steven Toth7615e432010-07-31 14:44:53 -0300858 tsport, tsfmt);
Steven Toth443c12282009-05-09 21:17:28 -0300859 break;
860 case VS_FORMAT_MPEG2PS:
Steven Toth7615e432010-07-31 14:44:53 -0300861 psfmt =
862 (tmComResPSFormatDescrHeader_t *)t;
863 if (currpath == 1)
864 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
865 else
866 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
867 memcpy(&encport->hwcfg, vcoutputtermhdr,
868 sizeof(*vcoutputtermhdr));
869 saa7164_api_configure_port_mpeg2ps(dev,
870 encport, psfmt);
Steven Toth443c12282009-05-09 21:17:28 -0300871 break;
872 case VS_FORMAT_VBI:
873 dprintk(DBGLVL_API,
874 " = VS_FORMAT_VBI\n");
875 break;
876 case VS_FORMAT_RDS:
877 dprintk(DBGLVL_API,
878 " = VS_FORMAT_RDS\n");
879 break;
880 case VS_FORMAT_UNCOMPRESSED:
881 dprintk(DBGLVL_API,
882 " = VS_FORMAT_UNCOMPRESSED\n");
883 break;
884 case VS_FORMAT_TYPE:
885 dprintk(DBGLVL_API,
886 " = VS_FORMAT_TYPE\n");
887 break;
888 default:
889 dprintk(DBGLVL_API,
890 " = undefined (0x%x)\n",
891 t->subtype);
892 }
893 next_offset += t->len;
894 }
895
896 break;
897 case TUNER_UNIT:
898 dprintk(DBGLVL_API, " TUNER_UNIT\n");
899 tunerunithdr =
900 (tmComResTunerDescrHeader_t *)(buf + idx);
901 dprintk(DBGLVL_API, " unitid = 0x%x\n",
902 tunerunithdr->unitid);
903 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
904 tunerunithdr->sourceid);
905 dprintk(DBGLVL_API, " iunit = 0x%x\n",
906 tunerunithdr->iunit);
907 dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
908 tunerunithdr->tuningstandards);
909 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
910 tunerunithdr->controlsize);
911 dprintk(DBGLVL_API, " controls = 0x%x\n",
912 tunerunithdr->controls);
Steven Toth7615e432010-07-31 14:44:53 -0300913
914 if (tunerunithdr->unitid == tunerunithdr->iunit) {
915 if (currpath == 1)
916 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
917 else
918 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
919 memcpy(&encport->tunerunit, tunerunithdr,
920 sizeof(tmComResTunerDescrHeader_t));
921 dprintk(DBGLVL_API, " (becomes dev->enc[%d] tuner)\n", encport->nr);
922 }
Steven Toth443c12282009-05-09 21:17:28 -0300923 break;
924 case VC_SELECTOR_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -0300925 psel = (tmComResSelDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -0300926 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -0300927 dprintk(DBGLVL_API, " unitid = 0x%x\n",
928 psel->unitid);
929 dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
930 psel->nrinpins);
931 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
932 psel->sourceid);
Steven Toth443c12282009-05-09 21:17:28 -0300933 break;
934 case VC_PROCESSING_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -0300935 pdh = (tmComResProcDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -0300936 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -0300937 dprintk(DBGLVL_API, " unitid = 0x%x\n",
938 pdh->unitid);
939 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
940 pdh->sourceid);
941 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
942 pdh->controlsize);
943 if (pdh->controlsize == 0x04) {
944 if (currpath == 1)
945 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
946 else
947 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
948 memcpy(&encport->vidproc, pdh,
949 sizeof(tmComResProcDescrHeader_t));
950 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
951 }
Steven Toth443c12282009-05-09 21:17:28 -0300952 break;
953 case FEATURE_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -0300954 afd = (tmComResAFeatureDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -0300955 dprintk(DBGLVL_API, " FEATURE_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -0300956 dprintk(DBGLVL_API, " unitid = 0x%x\n",
957 afd->unitid);
958 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
959 afd->sourceid);
960 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
961 afd->controlsize);
962 if (currpath == 1)
963 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
964 else
965 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
966 memcpy(&encport->audfeat, afd,
967 sizeof(tmComResAFeatureDescrHeader_t));
968 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
Steven Toth443c12282009-05-09 21:17:28 -0300969 break;
970 case ENCODER_UNIT:
Steven Toth7615e432010-07-31 14:44:53 -0300971 edh = (tmComResEncoderDescrHeader_t *)(buf + idx);
Steven Toth443c12282009-05-09 21:17:28 -0300972 dprintk(DBGLVL_API, " ENCODER_UNIT\n");
Steven Toth7615e432010-07-31 14:44:53 -0300973 dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
974 dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
975 dprintk(DBGLVL_API, " vsourceid = 0x%x\n", edh->vsourceid);
976 dprintk(DBGLVL_API, " asourceid = 0x%x\n", edh->asourceid);
977 dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
978 if (edh->iunit == edh->unitid) {
979 if (currpath == 1)
980 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
981 else
982 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
983 memcpy(&encport->encunit, edh,
984 sizeof(tmComResEncoderDescrHeader_t));
985 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
986 }
Steven Toth443c12282009-05-09 21:17:28 -0300987 break;
988 case EXTENSION_UNIT:
989 dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
990 exthdr = (tmComResExtDevDescrHeader_t *)(buf + idx);
991 dprintk(DBGLVL_API, " unitid = 0x%x\n",
992 exthdr->unitid);
993 dprintk(DBGLVL_API, " deviceid = 0x%x\n",
994 exthdr->deviceid);
995 dprintk(DBGLVL_API, " devicetype = 0x%x\n",
996 exthdr->devicetype);
997 if (exthdr->devicetype & 0x1)
998 dprintk(DBGLVL_API, " = Decoder Device\n");
999 if (exthdr->devicetype & 0x2)
1000 dprintk(DBGLVL_API, " = GPIO Source\n");
1001 if (exthdr->devicetype & 0x4)
1002 dprintk(DBGLVL_API, " = Video Decoder\n");
1003 if (exthdr->devicetype & 0x8)
1004 dprintk(DBGLVL_API, " = Audio Decoder\n");
1005 if (exthdr->devicetype & 0x20)
1006 dprintk(DBGLVL_API, " = Crossbar\n");
1007 if (exthdr->devicetype & 0x40)
1008 dprintk(DBGLVL_API, " = Tuner\n");
1009 if (exthdr->devicetype & 0x80)
1010 dprintk(DBGLVL_API, " = IF PLL\n");
1011 if (exthdr->devicetype & 0x100)
1012 dprintk(DBGLVL_API, " = Demodulator\n");
1013 if (exthdr->devicetype & 0x200)
1014 dprintk(DBGLVL_API, " = RDS Decoder\n");
1015 if (exthdr->devicetype & 0x400)
1016 dprintk(DBGLVL_API, " = Encoder\n");
1017 if (exthdr->devicetype & 0x800)
1018 dprintk(DBGLVL_API, " = IR Decoder\n");
1019 if (exthdr->devicetype & 0x1000)
1020 dprintk(DBGLVL_API, " = EEPROM\n");
1021 if (exthdr->devicetype & 0x2000)
1022 dprintk(DBGLVL_API,
1023 " = VBI Decoder\n");
1024 if (exthdr->devicetype & 0x10000)
1025 dprintk(DBGLVL_API,
1026 " = Streaming Device\n");
1027 if (exthdr->devicetype & 0x20000)
1028 dprintk(DBGLVL_API,
1029 " = DRM Device\n");
1030 if (exthdr->devicetype & 0x40000000)
1031 dprintk(DBGLVL_API,
1032 " = Generic Device\n");
1033 if (exthdr->devicetype & 0x80000000)
1034 dprintk(DBGLVL_API,
1035 " = Config Space Device\n");
1036 dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
1037 exthdr->numgpiopins);
1038 dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
1039 exthdr->numgpiogroups);
1040 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1041 exthdr->controlsize);
Steven Toth7615e432010-07-31 14:44:53 -03001042 if (exthdr->devicetype & 0x80) {
1043 if (currpath == 1)
1044 encport = &dev->ports[ SAA7164_PORT_ENC1 ];
1045 else
1046 encport = &dev->ports[ SAA7164_PORT_ENC2 ];
1047 memcpy(&encport->ifunit, exthdr,
1048 sizeof(tmComResExtDevDescrHeader_t));
1049 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n", encport->nr);
1050 }
Steven Toth443c12282009-05-09 21:17:28 -03001051 break;
1052 case PVC_INFRARED_UNIT:
1053 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1054 break;
1055 case DRM_UNIT:
1056 dprintk(DBGLVL_API, " DRM_UNIT\n");
1057 break;
1058 default:
1059 dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1060 }
1061
1062 dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
1063 dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
1064 dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
1065 dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
1066
1067 idx += hdr->len;
1068 }
1069
1070 return 0;
1071}
1072
1073int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1074{
1075 int ret;
1076 u32 buflen = 0;
1077 u8 *buf;
1078
1079 dprintk(DBGLVL_API, "%s()\n", __func__);
1080
1081 /* Get the total descriptor length */
1082 ret = saa7164_cmd_send(dev, 0, GET_LEN,
1083 GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1084 if (ret != SAA_OK)
1085 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1086
1087 dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1088 __func__, buflen);
1089
1090 /* Allocate enough storage for all of the descs */
1091 buf = kzalloc(buflen, GFP_KERNEL);
1092 if (buf == NULL)
1093 return SAA_ERR_NO_RESOURCES;
1094
1095 /* Retrieve them */
1096 ret = saa7164_cmd_send(dev, 0, GET_CUR,
1097 GET_DESCRIPTORS_CONTROL, buflen, buf);
1098 if (ret != SAA_OK) {
1099 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1100 goto out;
1101 }
1102
Ingo Molnarb1912a852009-09-21 15:23:45 -03001103 if (saa_debug & DBGLVL_API)
Steven Toth443c12282009-05-09 21:17:28 -03001104 saa7164_dumphex16(dev, buf, (buflen/16)*16);
1105
1106 saa7164_api_dump_subdevs(dev, buf, buflen);
1107
1108out:
1109 kfree(buf);
1110 return ret;
1111}
1112
1113int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1114 u32 datalen, u8 *data)
1115{
1116 struct saa7164_dev *dev = bus->dev;
1117 u16 len = 0;
1118 int unitid;
1119 u32 regval;
1120 u8 buf[256];
1121 int ret;
1122
1123 dprintk(DBGLVL_API, "%s()\n", __func__);
1124
1125 if (reglen > 4)
1126 return -EIO;
1127
1128 if (reglen == 1)
1129 regval = *(reg);
1130 else
1131 if (reglen == 2)
1132 regval = ((*(reg) << 8) || *(reg+1));
1133 else
1134 if (reglen == 3)
1135 regval = ((*(reg) << 16) | (*(reg+1) << 8) | *(reg+2));
1136 else
1137 if (reglen == 4)
1138 regval = ((*(reg) << 24) | (*(reg+1) << 16) |
1139 (*(reg+2) << 8) | *(reg+3));
1140
1141 /* Prepare the send buffer */
1142 /* Bytes 00-03 source register length
1143 * 04-07 source bytes to read
1144 * 08... register address
1145 */
1146 memset(buf, 0, sizeof(buf));
1147 memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
1148 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1149 *((u32 *)(buf + 1 * sizeof(u32))) = datalen;
1150
1151 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1152 if (unitid < 0) {
1153 printk(KERN_ERR
1154 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1155 __func__, addr);
1156 return -EIO;
1157 }
1158
1159 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1160 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1161 if (ret != SAA_OK) {
1162 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1163 return -EIO;
1164 }
1165
1166 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1167
Ingo Molnarb1912a852009-09-21 15:23:45 -03001168 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001169 saa7164_dumphex16(dev, buf, 2 * 16);
1170
1171 ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1172 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1173 if (ret != SAA_OK)
1174 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1175 else {
Ingo Molnarb1912a852009-09-21 15:23:45 -03001176 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001177 saa7164_dumphex16(dev, buf, sizeof(buf));
1178 memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1179 }
1180
1181 return ret == SAA_OK ? 0 : -EIO;
1182}
1183
1184/* For a given 8 bit i2c address device, write the buffer */
1185int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
1186 u8 *data)
1187{
1188 struct saa7164_dev *dev = bus->dev;
1189 u16 len = 0;
1190 int unitid;
1191 int reglen;
1192 u8 buf[256];
1193 int ret;
1194
1195 dprintk(DBGLVL_API, "%s()\n", __func__);
1196
1197 if ((datalen == 0) || (datalen > 232))
1198 return -EIO;
1199
1200 memset(buf, 0, sizeof(buf));
1201
1202 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1203 if (unitid < 0) {
1204 printk(KERN_ERR
1205 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1206 __func__, addr);
1207 return -EIO;
1208 }
1209
1210 reglen = saa7164_i2caddr_to_reglen(bus, addr);
Roel Kluinad695512010-01-03 14:04:42 -03001211 if (reglen < 0) {
Steven Toth443c12282009-05-09 21:17:28 -03001212 printk(KERN_ERR
1213 "%s() error, cannot translate regaddr to reglen\n",
1214 __func__);
1215 return -EIO;
1216 }
1217
1218 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1219 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1220 if (ret != SAA_OK) {
1221 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1222 return -EIO;
1223 }
1224
1225 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1226
1227 /* Prepare the send buffer */
1228 /* Bytes 00-03 dest register length
1229 * 04-07 dest bytes to write
1230 * 08... register address
1231 */
1232 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1233 *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1234 memcpy((buf + 2 * sizeof(u32)), data, datalen);
1235
Ingo Molnarb1912a852009-09-21 15:23:45 -03001236 if (saa_debug & DBGLVL_I2C)
Steven Toth443c12282009-05-09 21:17:28 -03001237 saa7164_dumphex16(dev, buf, sizeof(buf));
1238
1239 ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1240 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1241 if (ret != SAA_OK)
1242 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1243
1244 return ret == SAA_OK ? 0 : -EIO;
1245}
1246
1247
1248int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1249 u8 pin, u8 state)
1250{
1251 int ret;
1252 tmComResGPIO_t t;
1253
1254 dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1255 __func__, unitid, pin, state);
1256
1257 if ((pin > 7) || (state > 2))
1258 return SAA_ERR_BAD_PARAMETER;
1259
1260 t.pin = pin;
1261 t.state = state;
1262
1263 ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1264 EXU_GPIO_CONTROL, sizeof(t), &t);
1265 if (ret != SAA_OK)
1266 printk(KERN_ERR "%s() error, ret = 0x%x\n",
1267 __func__, ret);
1268
1269 return ret;
1270}
1271
1272int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1273 u8 pin)
1274{
1275 return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1276}
1277
1278int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1279 u8 pin)
1280{
1281 return saa7164_api_modify_gpio(dev, unitid, pin, 0);
1282}
1283
1284
1285