]> err.no Git - linux-2.6/blob - drivers/media/video/cx18/cx18-av-core.c
V4L/DVB (8340): videobuf: Fix gather spelling
[linux-2.6] / drivers / media / video / cx18 / cx18-av-core.c
1 /*
2  *  cx18 ADEC audio functions
3  *
4  *  Derived from cx25840-core.c
5  *
6  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version 2
11  *  of the License, or (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  *  02110-1301, USA.
22  */
23
24 #include "cx18-driver.h"
25
26 int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
27 {
28         u32 x = readl(cx->reg_mem + 0xc40000 + (addr & ~3));
29         u32 mask = 0xff;
30         int shift = (addr & 3) * 8;
31
32         x = (x & ~(mask << shift)) | ((u32)value << shift);
33         writel(x, cx->reg_mem + 0xc40000 + (addr & ~3));
34         return 0;
35 }
36
37 int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
38 {
39         writel(value, cx->reg_mem + 0xc40000 + addr);
40         return 0;
41 }
42
43 u8 cx18_av_read(struct cx18 *cx, u16 addr)
44 {
45         u32 x = readl(cx->reg_mem + 0xc40000 + (addr & ~3));
46         int shift = (addr & 3) * 8;
47
48         return (x >> shift) & 0xff;
49 }
50
51 u32 cx18_av_read4(struct cx18 *cx, u16 addr)
52 {
53         return readl(cx->reg_mem + 0xc40000 + addr);
54 }
55
56 int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
57                    u8 or_value)
58 {
59         return cx18_av_write(cx, addr,
60                              (cx18_av_read(cx, addr) & and_mask) |
61                              or_value);
62 }
63
64 int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
65                    u32 or_value)
66 {
67         return cx18_av_write4(cx, addr,
68                              (cx18_av_read4(cx, addr) & and_mask) |
69                              or_value);
70 }
71
72 /* ----------------------------------------------------------------------- */
73
74 static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
75                                         enum cx18_av_audio_input aud_input);
76 static void log_audio_status(struct cx18 *cx);
77 static void log_video_status(struct cx18 *cx);
78
79 /* ----------------------------------------------------------------------- */
80
81 static void cx18_av_initialize(struct cx18 *cx)
82 {
83         u32 v;
84
85         cx18_av_loadfw(cx);
86         /* Stop 8051 code execution */
87         cx18_av_write4(cx, CXADEC_DL_CTL, 0x03000000);
88
89         /* initallize the PLL by toggling sleep bit */
90         v = cx18_av_read4(cx, CXADEC_HOST_REG1);
91         /* enable sleep mode */
92         cx18_av_write4(cx, CXADEC_HOST_REG1, v | 1);
93         /* disable sleep mode */
94         cx18_av_write4(cx, CXADEC_HOST_REG1, v & 0xfffe);
95
96         /* initialize DLLs */
97         v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
98         /* disable FLD */
99         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
100         /* enable FLD */
101         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
102
103         v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
104         /* disable FLD */
105         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
106         /* enable FLD */
107         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
108
109         /* set analog bias currents. Set Vreg to 1.20V. */
110         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
111
112         v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
113         /* enable TUNE_FIL_RST */
114         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v);
115         /* disable TUNE_FIL_RST */
116         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v & 0xFFFFFFFE);
117
118         /* enable 656 output */
119         cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
120
121         /* video output drive strength */
122         cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
123
124         /* reset video */
125         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
126         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
127
128         /* set video to auto-detect */
129         /* Clear bits 11-12 to enable slow locking mode.  Set autodetect mode */
130         /* set the comb notch = 1 */
131         cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
132
133         /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
134         /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
135         cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
136
137         /* Set VGA_TRACK_RANGE to 0x20 */
138         cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
139
140         /* Enable VBI capture */
141         cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253F);
142         /* cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253E); */
143
144         /* Set the video input.
145            The setting in MODE_CTRL gets lost when we do the above setup */
146         /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
147         /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
148
149         v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
150         v &= 0xFFFBFFFF;            /* turn OFF bit 18 for droop_comp_ch1 */
151         v &= 0xFFFF7FFF;            /* turn OFF bit 9 for clamp_sel_ch1 */
152         v &= 0xFFFFFFFE;            /* turn OFF bit 0 for 12db_ch1 */
153         /* v |= 0x00000001;*/            /* turn ON bit 0 for 12db_ch1 */
154         cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
155
156 /*      if(dwEnable && dw3DCombAvailable) { */
157 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
158 /*    } else { */
159 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
160 /*    } */
161         cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
162 }
163
164 /* ----------------------------------------------------------------------- */
165
166 static void input_change(struct cx18 *cx)
167 {
168         struct cx18_av_state *state = &cx->av_state;
169         v4l2_std_id std = state->std;
170
171         /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
172         cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
173         cx18_av_and_or(cx, 0x401, ~0x60, 0);
174         cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
175
176         if (std & V4L2_STD_525_60) {
177                 if (std == V4L2_STD_NTSC_M_JP) {
178                         /* Japan uses EIAJ audio standard */
179                         cx18_av_write(cx, 0x808, 0xf7);
180                         cx18_av_write(cx, 0x80b, 0x02);
181                 } else if (std == V4L2_STD_NTSC_M_KR) {
182                         /* South Korea uses A2 audio standard */
183                         cx18_av_write(cx, 0x808, 0xf8);
184                         cx18_av_write(cx, 0x80b, 0x03);
185                 } else {
186                         /* Others use the BTSC audio standard */
187                         cx18_av_write(cx, 0x808, 0xf6);
188                         cx18_av_write(cx, 0x80b, 0x01);
189                 }
190         } else if (std & V4L2_STD_PAL) {
191                 /* Follow tuner change procedure for PAL */
192                 cx18_av_write(cx, 0x808, 0xff);
193                 cx18_av_write(cx, 0x80b, 0x03);
194         } else if (std & V4L2_STD_SECAM) {
195                 /* Select autodetect for SECAM */
196                 cx18_av_write(cx, 0x808, 0xff);
197                 cx18_av_write(cx, 0x80b, 0x03);
198         }
199
200         if (cx18_av_read(cx, 0x803) & 0x10) {
201                 /* restart audio decoder microcontroller */
202                 cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
203                 cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
204         }
205 }
206
207 static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
208                                         enum cx18_av_audio_input aud_input)
209 {
210         struct cx18_av_state *state = &cx->av_state;
211         u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
212                            vid_input <= CX18_AV_COMPOSITE8);
213         u8 reg;
214
215         CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
216                         vid_input, aud_input);
217
218         if (is_composite) {
219                 reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
220         } else {
221                 int luma = vid_input & 0xf0;
222                 int chroma = vid_input & 0xf00;
223
224                 if ((vid_input & ~0xff0) ||
225                     luma < CX18_AV_SVIDEO_LUMA1 ||
226                     luma > CX18_AV_SVIDEO_LUMA8 ||
227                     chroma < CX18_AV_SVIDEO_CHROMA4 ||
228                     chroma > CX18_AV_SVIDEO_CHROMA8) {
229                         CX18_ERR("0x%04x is not a valid video input!\n",
230                                         vid_input);
231                         return -EINVAL;
232                 }
233                 reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
234                 if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
235                         reg &= 0x3f;
236                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
237                 } else {
238                         reg &= 0xcf;
239                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
240                 }
241         }
242
243         switch (aud_input) {
244         case CX18_AV_AUDIO_SERIAL1:
245         case CX18_AV_AUDIO_SERIAL2:
246                 /* do nothing, use serial audio input */
247                 break;
248         case CX18_AV_AUDIO4: reg &= ~0x30; break;
249         case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
250         case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
251         case CX18_AV_AUDIO7: reg &= ~0xc0; break;
252         case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
253
254         default:
255                 CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
256                 return -EINVAL;
257         }
258
259         cx18_av_write(cx, 0x103, reg);
260         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
261         cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
262         /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
263         cx18_av_and_or(cx, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
264         /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
265         if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
266                 cx18_av_and_or(cx, 0x102, ~0x4, 4);
267         else
268                 cx18_av_and_or(cx, 0x102, ~0x4, 0);
269         /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
270
271         state->vid_input = vid_input;
272         state->aud_input = aud_input;
273         cx18_av_audio_set_path(cx);
274         input_change(cx);
275         return 0;
276 }
277
278 /* ----------------------------------------------------------------------- */
279
280 static int set_v4lstd(struct cx18 *cx)
281 {
282         struct cx18_av_state *state = &cx->av_state;
283         u8 fmt = 0;     /* zero is autodetect */
284         u8 pal_m = 0;
285
286         /* First tests should be against specific std */
287         if (state->std == V4L2_STD_NTSC_M_JP) {
288                 fmt = 0x2;
289         } else if (state->std == V4L2_STD_NTSC_443) {
290                 fmt = 0x3;
291         } else if (state->std == V4L2_STD_PAL_M) {
292                 pal_m = 1;
293                 fmt = 0x5;
294         } else if (state->std == V4L2_STD_PAL_N) {
295                 fmt = 0x6;
296         } else if (state->std == V4L2_STD_PAL_Nc) {
297                 fmt = 0x7;
298         } else if (state->std == V4L2_STD_PAL_60) {
299                 fmt = 0x8;
300         } else {
301                 /* Then, test against generic ones */
302                 if (state->std & V4L2_STD_NTSC)
303                         fmt = 0x1;
304                 else if (state->std & V4L2_STD_PAL)
305                         fmt = 0x4;
306                 else if (state->std & V4L2_STD_SECAM)
307                         fmt = 0xc;
308         }
309
310         CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
311
312         /* Follow step 9 of section 3.16 in the cx18_av datasheet.
313            Without this PAL may display a vertical ghosting effect.
314            This happens for example with the Yuan MPC622. */
315         if (fmt >= 4 && fmt < 8) {
316                 /* Set format to NTSC-M */
317                 cx18_av_and_or(cx, 0x400, ~0xf, 1);
318                 /* Turn off LCOMB */
319                 cx18_av_and_or(cx, 0x47b, ~6, 0);
320         }
321         cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
322         cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
323         cx18_av_vbi_setup(cx);
324         input_change(cx);
325         return 0;
326 }
327
328 /* ----------------------------------------------------------------------- */
329
330 static int set_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
331 {
332         switch (ctrl->id) {
333         case V4L2_CID_BRIGHTNESS:
334                 if (ctrl->value < 0 || ctrl->value > 255) {
335                         CX18_ERR("invalid brightness setting %d\n",
336                                     ctrl->value);
337                         return -ERANGE;
338                 }
339
340                 cx18_av_write(cx, 0x414, ctrl->value - 128);
341                 break;
342
343         case V4L2_CID_CONTRAST:
344                 if (ctrl->value < 0 || ctrl->value > 127) {
345                         CX18_ERR("invalid contrast setting %d\n",
346                                     ctrl->value);
347                         return -ERANGE;
348                 }
349
350                 cx18_av_write(cx, 0x415, ctrl->value << 1);
351                 break;
352
353         case V4L2_CID_SATURATION:
354                 if (ctrl->value < 0 || ctrl->value > 127) {
355                         CX18_ERR("invalid saturation setting %d\n",
356                                     ctrl->value);
357                         return -ERANGE;
358                 }
359
360                 cx18_av_write(cx, 0x420, ctrl->value << 1);
361                 cx18_av_write(cx, 0x421, ctrl->value << 1);
362                 break;
363
364         case V4L2_CID_HUE:
365                 if (ctrl->value < -127 || ctrl->value > 127) {
366                         CX18_ERR("invalid hue setting %d\n", ctrl->value);
367                         return -ERANGE;
368                 }
369
370                 cx18_av_write(cx, 0x422, ctrl->value);
371                 break;
372
373         case V4L2_CID_AUDIO_VOLUME:
374         case V4L2_CID_AUDIO_BASS:
375         case V4L2_CID_AUDIO_TREBLE:
376         case V4L2_CID_AUDIO_BALANCE:
377         case V4L2_CID_AUDIO_MUTE:
378                 return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
379
380         default:
381                 return -EINVAL;
382         }
383
384         return 0;
385 }
386
387 static int get_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
388 {
389         switch (ctrl->id) {
390         case V4L2_CID_BRIGHTNESS:
391                 ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
392                 break;
393         case V4L2_CID_CONTRAST:
394                 ctrl->value = cx18_av_read(cx, 0x415) >> 1;
395                 break;
396         case V4L2_CID_SATURATION:
397                 ctrl->value = cx18_av_read(cx, 0x420) >> 1;
398                 break;
399         case V4L2_CID_HUE:
400                 ctrl->value = (s8)cx18_av_read(cx, 0x422);
401                 break;
402         case V4L2_CID_AUDIO_VOLUME:
403         case V4L2_CID_AUDIO_BASS:
404         case V4L2_CID_AUDIO_TREBLE:
405         case V4L2_CID_AUDIO_BALANCE:
406         case V4L2_CID_AUDIO_MUTE:
407                 return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
408         default:
409                 return -EINVAL;
410         }
411
412         return 0;
413 }
414
415 /* ----------------------------------------------------------------------- */
416
417 static int get_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
418 {
419         switch (fmt->type) {
420         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
421                 return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
422         default:
423                 return -EINVAL;
424         }
425
426         return 0;
427 }
428
429 static int set_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
430 {
431         struct cx18_av_state *state = &cx->av_state;
432         struct v4l2_pix_format *pix;
433         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
434         int is_50Hz = !(state->std & V4L2_STD_525_60);
435
436         switch (fmt->type) {
437         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
438                 pix = &(fmt->fmt.pix);
439
440                 Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
441                 Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
442
443                 Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
444                 Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
445
446                 Vlines = pix->height + (is_50Hz ? 4 : 7);
447
448                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
449                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
450                         CX18_ERR("%dx%d is not a valid size!\n",
451                                     pix->width, pix->height);
452                         return -ERANGE;
453                 }
454
455                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
456                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
457                 VSC &= 0x1fff;
458
459                 if (pix->width >= 385)
460                         filter = 0;
461                 else if (pix->width > 192)
462                         filter = 1;
463                 else if (pix->width > 96)
464                         filter = 2;
465                 else
466                         filter = 3;
467
468                 CX18_DEBUG_INFO("decoder set size %dx%d -> scale  %ux%u\n",
469                             pix->width, pix->height, HSC, VSC);
470
471                 /* HSCALE=HSC */
472                 cx18_av_write(cx, 0x418, HSC & 0xff);
473                 cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
474                 cx18_av_write(cx, 0x41a, HSC >> 16);
475                 /* VSCALE=VSC */
476                 cx18_av_write(cx, 0x41c, VSC & 0xff);
477                 cx18_av_write(cx, 0x41d, VSC >> 8);
478                 /* VS_INTRLACE=1 VFILT=filter */
479                 cx18_av_write(cx, 0x41e, 0x8 | filter);
480                 break;
481
482         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
483                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
484
485         case V4L2_BUF_TYPE_VBI_CAPTURE:
486                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
487
488         default:
489                 return -EINVAL;
490         }
491
492         return 0;
493 }
494
495 /* ----------------------------------------------------------------------- */
496
497 int cx18_av_cmd(struct cx18 *cx, unsigned int cmd, void *arg)
498 {
499         struct cx18_av_state *state = &cx->av_state;
500         struct v4l2_tuner *vt = arg;
501         struct v4l2_routing *route = arg;
502
503         /* ignore these commands */
504         switch (cmd) {
505         case TUNER_SET_TYPE_ADDR:
506                 return 0;
507         }
508
509         if (!state->is_initialized) {
510                 CX18_DEBUG_INFO("cmd %08x triggered fw load\n", cmd);
511                 /* initialize on first use */
512                 state->is_initialized = 1;
513                 cx18_av_initialize(cx);
514         }
515
516         switch (cmd) {
517         case VIDIOC_INT_DECODE_VBI_LINE:
518                 return cx18_av_vbi(cx, cmd, arg);
519
520         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
521                 return cx18_av_audio(cx, cmd, arg);
522
523         case VIDIOC_STREAMON:
524                 CX18_DEBUG_INFO("enable output\n");
525                 cx18_av_write(cx, 0x115, 0x8c);
526                 cx18_av_write(cx, 0x116, 0x07);
527                 break;
528
529         case VIDIOC_STREAMOFF:
530                 CX18_DEBUG_INFO("disable output\n");
531                 cx18_av_write(cx, 0x115, 0x00);
532                 cx18_av_write(cx, 0x116, 0x00);
533                 break;
534
535         case VIDIOC_LOG_STATUS:
536                 log_video_status(cx);
537                 log_audio_status(cx);
538                 break;
539
540         case VIDIOC_G_CTRL:
541                 return get_v4lctrl(cx, (struct v4l2_control *)arg);
542
543         case VIDIOC_S_CTRL:
544                 return set_v4lctrl(cx, (struct v4l2_control *)arg);
545
546         case VIDIOC_QUERYCTRL:
547         {
548                 struct v4l2_queryctrl *qc = arg;
549
550                 switch (qc->id) {
551                 case V4L2_CID_BRIGHTNESS:
552                 case V4L2_CID_CONTRAST:
553                 case V4L2_CID_SATURATION:
554                 case V4L2_CID_HUE:
555                         return v4l2_ctrl_query_fill_std(qc);
556                 default:
557                         break;
558                 }
559
560                 switch (qc->id) {
561                 case V4L2_CID_AUDIO_VOLUME:
562                 case V4L2_CID_AUDIO_MUTE:
563                 case V4L2_CID_AUDIO_BALANCE:
564                 case V4L2_CID_AUDIO_BASS:
565                 case V4L2_CID_AUDIO_TREBLE:
566                         return v4l2_ctrl_query_fill_std(qc);
567                 default:
568                         return -EINVAL;
569                 }
570                 return -EINVAL;
571         }
572
573         case VIDIOC_G_STD:
574                 *(v4l2_std_id *)arg = state->std;
575                 break;
576
577         case VIDIOC_S_STD:
578                 if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
579                         return 0;
580                 state->radio = 0;
581                 state->std = *(v4l2_std_id *)arg;
582                 return set_v4lstd(cx);
583
584         case AUDC_SET_RADIO:
585                 state->radio = 1;
586                 break;
587
588         case VIDIOC_INT_G_VIDEO_ROUTING:
589                 route->input = state->vid_input;
590                 route->output = 0;
591                 break;
592
593         case VIDIOC_INT_S_VIDEO_ROUTING:
594                 return set_input(cx, route->input, state->aud_input);
595
596         case VIDIOC_INT_G_AUDIO_ROUTING:
597                 route->input = state->aud_input;
598                 route->output = 0;
599                 break;
600
601         case VIDIOC_INT_S_AUDIO_ROUTING:
602                 return set_input(cx, state->vid_input, route->input);
603
604         case VIDIOC_S_FREQUENCY:
605                 input_change(cx);
606                 break;
607
608         case VIDIOC_G_TUNER:
609         {
610                 u8 vpres = cx18_av_read(cx, 0x40e) & 0x20;
611                 u8 mode;
612                 int val = 0;
613
614                 if (state->radio)
615                         break;
616
617                 vt->signal = vpres ? 0xffff : 0x0;
618
619                 vt->capability |=
620                     V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
621                     V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
622
623                 mode = cx18_av_read(cx, 0x804);
624
625                 /* get rxsubchans and audmode */
626                 if ((mode & 0xf) == 1)
627                         val |= V4L2_TUNER_SUB_STEREO;
628                 else
629                         val |= V4L2_TUNER_SUB_MONO;
630
631                 if (mode == 2 || mode == 4)
632                         val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
633
634                 if (mode & 0x10)
635                         val |= V4L2_TUNER_SUB_SAP;
636
637                 vt->rxsubchans = val;
638                 vt->audmode = state->audmode;
639                 break;
640         }
641
642         case VIDIOC_S_TUNER:
643                 if (state->radio)
644                         break;
645
646                 switch (vt->audmode) {
647                 case V4L2_TUNER_MODE_MONO:
648                         /* mono      -> mono
649                            stereo    -> mono
650                            bilingual -> lang1 */
651                         cx18_av_and_or(cx, 0x809, ~0xf, 0x00);
652                         break;
653                 case V4L2_TUNER_MODE_STEREO:
654                 case V4L2_TUNER_MODE_LANG1:
655                         /* mono      -> mono
656                            stereo    -> stereo
657                            bilingual -> lang1 */
658                         cx18_av_and_or(cx, 0x809, ~0xf, 0x04);
659                         break;
660                 case V4L2_TUNER_MODE_LANG1_LANG2:
661                         /* mono      -> mono
662                            stereo    -> stereo
663                            bilingual -> lang1/lang2 */
664                         cx18_av_and_or(cx, 0x809, ~0xf, 0x07);
665                         break;
666                 case V4L2_TUNER_MODE_LANG2:
667                         /* mono      -> mono
668                            stereo    -> stereo
669                            bilingual -> lang2 */
670                         cx18_av_and_or(cx, 0x809, ~0xf, 0x01);
671                         break;
672                 default:
673                         return -EINVAL;
674                 }
675                 state->audmode = vt->audmode;
676                 break;
677
678         case VIDIOC_G_FMT:
679                 return get_v4lfmt(cx, (struct v4l2_format *)arg);
680
681         case VIDIOC_S_FMT:
682                 return set_v4lfmt(cx, (struct v4l2_format *)arg);
683
684         case VIDIOC_INT_RESET:
685                 cx18_av_initialize(cx);
686                 break;
687
688         default:
689                 return -EINVAL;
690         }
691
692         return 0;
693 }
694
695 /* ----------------------------------------------------------------------- */
696
697 /* ----------------------------------------------------------------------- */
698
699 static void log_video_status(struct cx18 *cx)
700 {
701         static const char *const fmt_strs[] = {
702                 "0x0",
703                 "NTSC-M", "NTSC-J", "NTSC-4.43",
704                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
705                 "0x9", "0xA", "0xB",
706                 "SECAM",
707                 "0xD", "0xE", "0xF"
708         };
709
710         struct cx18_av_state *state = &cx->av_state;
711         u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
712         u8 gen_stat1 = cx18_av_read(cx, 0x40d);
713         u8 gen_stat2 = cx18_av_read(cx, 0x40e);
714         int vid_input = state->vid_input;
715
716         CX18_INFO("Video signal:              %spresent\n",
717                     (gen_stat2 & 0x20) ? "" : "not ");
718         CX18_INFO("Detected format:           %s\n",
719                     fmt_strs[gen_stat1 & 0xf]);
720
721         CX18_INFO("Specified standard:        %s\n",
722                     vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
723
724         if (vid_input >= CX18_AV_COMPOSITE1 &&
725             vid_input <= CX18_AV_COMPOSITE8) {
726                 CX18_INFO("Specified video input:     Composite %d\n",
727                         vid_input - CX18_AV_COMPOSITE1 + 1);
728         } else {
729                 CX18_INFO("Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
730                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
731         }
732
733         CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
734 }
735
736 /* ----------------------------------------------------------------------- */
737
738 static void log_audio_status(struct cx18 *cx)
739 {
740         struct cx18_av_state *state = &cx->av_state;
741         u8 download_ctl = cx18_av_read(cx, 0x803);
742         u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
743         u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
744         u8 audio_config = cx18_av_read(cx, 0x808);
745         u8 pref_mode = cx18_av_read(cx, 0x809);
746         u8 afc0 = cx18_av_read(cx, 0x80b);
747         u8 mute_ctl = cx18_av_read(cx, 0x8d3);
748         int aud_input = state->aud_input;
749         char *p;
750
751         switch (mod_det_stat0) {
752         case 0x00: p = "mono"; break;
753         case 0x01: p = "stereo"; break;
754         case 0x02: p = "dual"; break;
755         case 0x04: p = "tri"; break;
756         case 0x10: p = "mono with SAP"; break;
757         case 0x11: p = "stereo with SAP"; break;
758         case 0x12: p = "dual with SAP"; break;
759         case 0x14: p = "tri with SAP"; break;
760         case 0xfe: p = "forced mode"; break;
761         default: p = "not defined"; break;
762         }
763         CX18_INFO("Detected audio mode:       %s\n", p);
764
765         switch (mod_det_stat1) {
766         case 0x00: p = "not defined"; break;
767         case 0x01: p = "EIAJ"; break;
768         case 0x02: p = "A2-M"; break;
769         case 0x03: p = "A2-BG"; break;
770         case 0x04: p = "A2-DK1"; break;
771         case 0x05: p = "A2-DK2"; break;
772         case 0x06: p = "A2-DK3"; break;
773         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
774         case 0x08: p = "AM-L"; break;
775         case 0x09: p = "NICAM-BG"; break;
776         case 0x0a: p = "NICAM-DK"; break;
777         case 0x0b: p = "NICAM-I"; break;
778         case 0x0c: p = "NICAM-L"; break;
779         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
780         case 0x0e: p = "IF FM Radio"; break;
781         case 0x0f: p = "BTSC"; break;
782         case 0x10: p = "detected chrominance"; break;
783         case 0xfd: p = "unknown audio standard"; break;
784         case 0xfe: p = "forced audio standard"; break;
785         case 0xff: p = "no detected audio standard"; break;
786         default: p = "not defined"; break;
787         }
788         CX18_INFO("Detected audio standard:   %s\n", p);
789         CX18_INFO("Audio muted:               %s\n",
790                     (mute_ctl & 0x2) ? "yes" : "no");
791         CX18_INFO("Audio microcontroller:     %s\n",
792                     (download_ctl & 0x10) ? "running" : "stopped");
793
794         switch (audio_config >> 4) {
795         case 0x00: p = "undefined"; break;
796         case 0x01: p = "BTSC"; break;
797         case 0x02: p = "EIAJ"; break;
798         case 0x03: p = "A2-M"; break;
799         case 0x04: p = "A2-BG"; break;
800         case 0x05: p = "A2-DK1"; break;
801         case 0x06: p = "A2-DK2"; break;
802         case 0x07: p = "A2-DK3"; break;
803         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
804         case 0x09: p = "AM-L"; break;
805         case 0x0a: p = "NICAM-BG"; break;
806         case 0x0b: p = "NICAM-DK"; break;
807         case 0x0c: p = "NICAM-I"; break;
808         case 0x0d: p = "NICAM-L"; break;
809         case 0x0e: p = "FM radio"; break;
810         case 0x0f: p = "automatic detection"; break;
811         default: p = "undefined"; break;
812         }
813         CX18_INFO("Configured audio standard: %s\n", p);
814
815         if ((audio_config >> 4) < 0xF) {
816                 switch (audio_config & 0xF) {
817                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
818                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
819                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
820                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
821                 case 0x04: p = "STEREO"; break;
822                 case 0x05: p = "DUAL1 (AC)"; break;
823                 case 0x06: p = "DUAL2 (BC)"; break;
824                 case 0x07: p = "DUAL3 (AB)"; break;
825                 default: p = "undefined";
826                 }
827                 CX18_INFO("Configured audio mode:     %s\n", p);
828         } else {
829                 switch (audio_config & 0xF) {
830                 case 0x00: p = "BG"; break;
831                 case 0x01: p = "DK1"; break;
832                 case 0x02: p = "DK2"; break;
833                 case 0x03: p = "DK3"; break;
834                 case 0x04: p = "I"; break;
835                 case 0x05: p = "L"; break;
836                 case 0x06: p = "BTSC"; break;
837                 case 0x07: p = "EIAJ"; break;
838                 case 0x08: p = "A2-M"; break;
839                 case 0x09: p = "FM Radio (4.5 MHz)"; break;
840                 case 0x0a: p = "FM Radio (5.5 MHz)"; break;
841                 case 0x0b: p = "S-Video"; break;
842                 case 0x0f: p = "automatic standard and mode detection"; break;
843                 default: p = "undefined"; break;
844                 }
845                 CX18_INFO("Configured audio system:   %s\n", p);
846         }
847
848         if (aud_input)
849                 CX18_INFO("Specified audio input:     Tuner (In%d)\n",
850                                 aud_input);
851         else
852                 CX18_INFO("Specified audio input:     External\n");
853
854         switch (pref_mode & 0xf) {
855         case 0: p = "mono/language A"; break;
856         case 1: p = "language B"; break;
857         case 2: p = "language C"; break;
858         case 3: p = "analog fallback"; break;
859         case 4: p = "stereo"; break;
860         case 5: p = "language AC"; break;
861         case 6: p = "language BC"; break;
862         case 7: p = "language AB"; break;
863         default: p = "undefined"; break;
864         }
865         CX18_INFO("Preferred audio mode:      %s\n", p);
866
867         if ((audio_config & 0xf) == 0xf) {
868                 switch ((afc0 >> 3) & 0x1) {
869                 case 0: p = "system DK"; break;
870                 case 1: p = "system L"; break;
871                 }
872                 CX18_INFO("Selected 65 MHz format:    %s\n", p);
873
874                 switch (afc0 & 0x7) {
875                 case 0: p = "Chroma"; break;
876                 case 1: p = "BTSC"; break;
877                 case 2: p = "EIAJ"; break;
878                 case 3: p = "A2-M"; break;
879                 case 4: p = "autodetect"; break;
880                 default: p = "undefined"; break;
881                 }
882                 CX18_INFO("Selected 45 MHz format:    %s\n", p);
883         }
884 }