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