]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/cx18/cx18-av-core.c
[WATCHDOG] Merge code clean-up's from Alan Cox.
[linux-2.6-omap-h63xx.git] / 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         struct cx18_av_state *state = &cx->av_state;
84         u32 v;
85
86         cx18_av_loadfw(cx);
87         /* Stop 8051 code execution */
88         cx18_av_write4(cx, CXADEC_DL_CTL, 0x03000000);
89
90         /* initallize the PLL by toggling sleep bit */
91         v = cx18_av_read4(cx, CXADEC_HOST_REG1);
92         /* enable sleep mode */
93         cx18_av_write4(cx, CXADEC_HOST_REG1, v | 1);
94         /* disable sleep mode */
95         cx18_av_write4(cx, CXADEC_HOST_REG1, v & 0xfffe);
96
97         /* initialize DLLs */
98         v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
99         /* disable FLD */
100         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
101         /* enable FLD */
102         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
103
104         v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
105         /* disable FLD */
106         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
107         /* enable FLD */
108         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
109
110         /* set analog bias currents. Set Vreg to 1.20V. */
111         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
112
113         v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
114         /* enable TUNE_FIL_RST */
115         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v);
116         /* disable TUNE_FIL_RST */
117         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL3, v & 0xFFFFFFFE);
118
119         /* enable 656 output */
120         cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
121
122         /* video output drive strength */
123         cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
124
125         /* reset video */
126         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
127         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
128
129         /* set video to auto-detect */
130         /* Clear bits 11-12 to enable slow locking mode.  Set autodetect mode */
131         /* set the comb notch = 1 */
132         cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
133
134         /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
135         /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
136         cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
137
138         /* Set VGA_TRACK_RANGE to 0x20 */
139         cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
140
141         /* Enable VBI capture */
142         cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253F);
143         /* cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4010253E); */
144
145         /* Set the video input.
146            The setting in MODE_CTRL gets lost when we do the above setup */
147         /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
148         /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
149
150         v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
151         v &= 0xFFFBFFFF;            /* turn OFF bit 18 for droop_comp_ch1 */
152         v &= 0xFFFF7FFF;            /* turn OFF bit 9 for clamp_sel_ch1 */
153         v &= 0xFFFFFFFE;            /* turn OFF bit 0 for 12db_ch1 */
154         /* v |= 0x00000001;*/            /* turn ON bit 0 for 12db_ch1 */
155         cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
156
157 /*      if(dwEnable && dw3DCombAvailable) { */
158 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
159 /*    } else { */
160 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
161 /*    } */
162         cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
163         state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
164         state->default_volume = ((state->default_volume / 2) + 23) << 9;
165 }
166
167 /* ----------------------------------------------------------------------- */
168
169 void cx18_av_std_setup(struct cx18 *cx)
170 {
171         struct cx18_av_state *state = &cx->av_state;
172         v4l2_std_id std = state->std;
173         int hblank, hactive, burst, vblank, vactive, sc;
174         int vblank656, src_decimation;
175         int luma_lpf, uv_lpf, comb;
176         u32 pll_int, pll_frac, pll_post;
177
178         /* datasheet startup, step 8d */
179         if (std & ~V4L2_STD_NTSC)
180                 cx18_av_write(cx, 0x49f, 0x11);
181         else
182                 cx18_av_write(cx, 0x49f, 0x14);
183
184         if (std & V4L2_STD_625_50) {
185                 hblank = 132;
186                 hactive = 720;
187                 burst = 93;
188                 vblank = 36;
189                 vactive = 580;
190                 vblank656 = 40;
191                 src_decimation = 0x21f;
192
193                 luma_lpf = 2;
194                 if (std & V4L2_STD_PAL) {
195                         uv_lpf = 1;
196                         comb = 0x20;
197                         sc = 688739;
198                 } else if (std == V4L2_STD_PAL_Nc) {
199                         uv_lpf = 1;
200                         comb = 0x20;
201                         sc = 556453;
202                 } else { /* SECAM */
203                         uv_lpf = 0;
204                         comb = 0;
205                         sc = 672351;
206                 }
207         } else {
208                 hactive = 720;
209                 hblank = 122;
210                 vactive = 487;
211                 luma_lpf = 1;
212                 uv_lpf = 1;
213                 vblank = 26;
214                 vblank656 = 26;
215
216                 src_decimation = 0x21f;
217                 if (std == V4L2_STD_PAL_60) {
218                         burst = 0x5b;
219                         luma_lpf = 2;
220                         comb = 0x20;
221                         sc = 688739;
222                 } else if (std == V4L2_STD_PAL_M) {
223                         burst = 0x61;
224                         comb = 0x20;
225                         sc = 555452;
226                 } else {
227                         burst = 0x5b;
228                         comb = 0x66;
229                         sc = 556063;
230                 }
231         }
232
233         /* DEBUG: Displays configured PLL frequency */
234         pll_int = cx18_av_read(cx, 0x108);
235         pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
236         pll_post = cx18_av_read(cx, 0x109);
237         CX18_DEBUG_INFO("PLL regs = int: %u, frac: %u, post: %u\n",
238                         pll_int, pll_frac, pll_post);
239
240         if (pll_post) {
241                 int fin, fsc;
242                 int pll = 28636363L * ((((u64)pll_int) << 25) + pll_frac);
243
244                 pll >>= 25;
245                 pll /= pll_post;
246                 CX18_DEBUG_INFO("PLL = %d.%06d MHz\n",
247                                         pll / 1000000, pll % 1000000);
248                 CX18_DEBUG_INFO("PLL/8 = %d.%06d MHz\n",
249                                         pll / 8000000, (pll / 8) % 1000000);
250
251                 fin = ((u64)src_decimation * pll) >> 12;
252                 CX18_DEBUG_INFO("ADC Sampling freq = %d.%06d MHz\n",
253                                         fin / 1000000, fin % 1000000);
254
255                 fsc = (((u64)sc) * pll) >> 24L;
256                 CX18_DEBUG_INFO("Chroma sub-carrier freq = %d.%06d MHz\n",
257                                         fsc / 1000000, fsc % 1000000);
258
259                 CX18_DEBUG_INFO("hblank %i, hactive %i, "
260                         "vblank %i , vactive %i, vblank656 %i, src_dec %i,"
261                         "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x,"
262                         " sc 0x%06x\n",
263                         hblank, hactive, vblank, vactive, vblank656,
264                         src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
265         }
266
267         /* Sets horizontal blanking delay and active lines */
268         cx18_av_write(cx, 0x470, hblank);
269         cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
270                                                 (hactive << 4)));
271         cx18_av_write(cx, 0x472, hactive >> 4);
272
273         /* Sets burst gate delay */
274         cx18_av_write(cx, 0x473, burst);
275
276         /* Sets vertical blanking delay and active duration */
277         cx18_av_write(cx, 0x474, vblank);
278         cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
279                                                 (vactive << 4)));
280         cx18_av_write(cx, 0x476, vactive >> 4);
281         cx18_av_write(cx, 0x477, vblank656);
282
283         /* Sets src decimation rate */
284         cx18_av_write(cx, 0x478, 0xff & src_decimation);
285         cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
286
287         /* Sets Luma and UV Low pass filters */
288         cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
289
290         /* Enables comb filters */
291         cx18_av_write(cx, 0x47b, comb);
292
293         /* Sets SC Step*/
294         cx18_av_write(cx, 0x47c, sc);
295         cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
296         cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
297
298         /* Sets VBI parameters */
299         if (std & V4L2_STD_625_50) {
300                 cx18_av_write(cx, 0x47f, 0x01);
301                 state->vbi_line_offset = 5;
302         } else {
303                 cx18_av_write(cx, 0x47f, 0x00);
304                 state->vbi_line_offset = 8;
305         }
306 }
307
308 /* ----------------------------------------------------------------------- */
309
310 static void input_change(struct cx18 *cx)
311 {
312         struct cx18_av_state *state = &cx->av_state;
313         v4l2_std_id std = state->std;
314
315         /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
316         cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
317         cx18_av_and_or(cx, 0x401, ~0x60, 0);
318         cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
319
320         if (std & V4L2_STD_525_60) {
321                 if (std == V4L2_STD_NTSC_M_JP) {
322                         /* Japan uses EIAJ audio standard */
323                         cx18_av_write(cx, 0x808, 0xf7);
324                         cx18_av_write(cx, 0x80b, 0x02);
325                 } else if (std == V4L2_STD_NTSC_M_KR) {
326                         /* South Korea uses A2 audio standard */
327                         cx18_av_write(cx, 0x808, 0xf8);
328                         cx18_av_write(cx, 0x80b, 0x03);
329                 } else {
330                         /* Others use the BTSC audio standard */
331                         cx18_av_write(cx, 0x808, 0xf6);
332                         cx18_av_write(cx, 0x80b, 0x01);
333                 }
334         } else if (std & V4L2_STD_PAL) {
335                 /* Follow tuner change procedure for PAL */
336                 cx18_av_write(cx, 0x808, 0xff);
337                 cx18_av_write(cx, 0x80b, 0x03);
338         } else if (std & V4L2_STD_SECAM) {
339                 /* Select autodetect for SECAM */
340                 cx18_av_write(cx, 0x808, 0xff);
341                 cx18_av_write(cx, 0x80b, 0x03);
342         }
343
344         if (cx18_av_read(cx, 0x803) & 0x10) {
345                 /* restart audio decoder microcontroller */
346                 cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
347                 cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
348         }
349 }
350
351 static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
352                                         enum cx18_av_audio_input aud_input)
353 {
354         struct cx18_av_state *state = &cx->av_state;
355         u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
356                            vid_input <= CX18_AV_COMPOSITE8);
357         u8 reg;
358
359         CX18_DEBUG_INFO("decoder set video input %d, audio input %d\n",
360                         vid_input, aud_input);
361
362         if (is_composite) {
363                 reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
364         } else {
365                 int luma = vid_input & 0xf0;
366                 int chroma = vid_input & 0xf00;
367
368                 if ((vid_input & ~0xff0) ||
369                     luma < CX18_AV_SVIDEO_LUMA1 ||
370                     luma > CX18_AV_SVIDEO_LUMA8 ||
371                     chroma < CX18_AV_SVIDEO_CHROMA4 ||
372                     chroma > CX18_AV_SVIDEO_CHROMA8) {
373                         CX18_ERR("0x%04x is not a valid video input!\n",
374                                         vid_input);
375                         return -EINVAL;
376                 }
377                 reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
378                 if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
379                         reg &= 0x3f;
380                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
381                 } else {
382                         reg &= 0xcf;
383                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
384                 }
385         }
386
387         switch (aud_input) {
388         case CX18_AV_AUDIO_SERIAL1:
389         case CX18_AV_AUDIO_SERIAL2:
390                 /* do nothing, use serial audio input */
391                 break;
392         case CX18_AV_AUDIO4: reg &= ~0x30; break;
393         case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
394         case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
395         case CX18_AV_AUDIO7: reg &= ~0xc0; break;
396         case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
397
398         default:
399                 CX18_ERR("0x%04x is not a valid audio input!\n", aud_input);
400                 return -EINVAL;
401         }
402
403         cx18_av_write(cx, 0x103, reg);
404         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
405         cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
406         /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
407         cx18_av_and_or(cx, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
408         /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
409         if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
410                 cx18_av_and_or(cx, 0x102, ~0x4, 4);
411         else
412                 cx18_av_and_or(cx, 0x102, ~0x4, 0);
413         /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
414
415         state->vid_input = vid_input;
416         state->aud_input = aud_input;
417         cx18_av_audio_set_path(cx);
418         input_change(cx);
419         return 0;
420 }
421
422 /* ----------------------------------------------------------------------- */
423
424 static int set_v4lstd(struct cx18 *cx)
425 {
426         struct cx18_av_state *state = &cx->av_state;
427         u8 fmt = 0;     /* zero is autodetect */
428         u8 pal_m = 0;
429
430         /* First tests should be against specific std */
431         if (state->std == V4L2_STD_NTSC_M_JP) {
432                 fmt = 0x2;
433         } else if (state->std == V4L2_STD_NTSC_443) {
434                 fmt = 0x3;
435         } else if (state->std == V4L2_STD_PAL_M) {
436                 pal_m = 1;
437                 fmt = 0x5;
438         } else if (state->std == V4L2_STD_PAL_N) {
439                 fmt = 0x6;
440         } else if (state->std == V4L2_STD_PAL_Nc) {
441                 fmt = 0x7;
442         } else if (state->std == V4L2_STD_PAL_60) {
443                 fmt = 0x8;
444         } else {
445                 /* Then, test against generic ones */
446                 if (state->std & V4L2_STD_NTSC)
447                         fmt = 0x1;
448                 else if (state->std & V4L2_STD_PAL)
449                         fmt = 0x4;
450                 else if (state->std & V4L2_STD_SECAM)
451                         fmt = 0xc;
452         }
453
454         CX18_DEBUG_INFO("changing video std to fmt %i\n", fmt);
455
456         /* Follow step 9 of section 3.16 in the cx18_av datasheet.
457            Without this PAL may display a vertical ghosting effect.
458            This happens for example with the Yuan MPC622. */
459         if (fmt >= 4 && fmt < 8) {
460                 /* Set format to NTSC-M */
461                 cx18_av_and_or(cx, 0x400, ~0xf, 1);
462                 /* Turn off LCOMB */
463                 cx18_av_and_or(cx, 0x47b, ~6, 0);
464         }
465         cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
466         cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
467         cx18_av_std_setup(cx);
468         input_change(cx);
469         return 0;
470 }
471
472 /* ----------------------------------------------------------------------- */
473
474 static int set_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
475 {
476         switch (ctrl->id) {
477         case V4L2_CID_BRIGHTNESS:
478                 if (ctrl->value < 0 || ctrl->value > 255) {
479                         CX18_ERR("invalid brightness setting %d\n",
480                                     ctrl->value);
481                         return -ERANGE;
482                 }
483
484                 cx18_av_write(cx, 0x414, ctrl->value - 128);
485                 break;
486
487         case V4L2_CID_CONTRAST:
488                 if (ctrl->value < 0 || ctrl->value > 127) {
489                         CX18_ERR("invalid contrast setting %d\n",
490                                     ctrl->value);
491                         return -ERANGE;
492                 }
493
494                 cx18_av_write(cx, 0x415, ctrl->value << 1);
495                 break;
496
497         case V4L2_CID_SATURATION:
498                 if (ctrl->value < 0 || ctrl->value > 127) {
499                         CX18_ERR("invalid saturation setting %d\n",
500                                     ctrl->value);
501                         return -ERANGE;
502                 }
503
504                 cx18_av_write(cx, 0x420, ctrl->value << 1);
505                 cx18_av_write(cx, 0x421, ctrl->value << 1);
506                 break;
507
508         case V4L2_CID_HUE:
509                 if (ctrl->value < -127 || ctrl->value > 127) {
510                         CX18_ERR("invalid hue setting %d\n", ctrl->value);
511                         return -ERANGE;
512                 }
513
514                 cx18_av_write(cx, 0x422, ctrl->value);
515                 break;
516
517         case V4L2_CID_AUDIO_VOLUME:
518         case V4L2_CID_AUDIO_BASS:
519         case V4L2_CID_AUDIO_TREBLE:
520         case V4L2_CID_AUDIO_BALANCE:
521         case V4L2_CID_AUDIO_MUTE:
522                 return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
523
524         default:
525                 return -EINVAL;
526         }
527
528         return 0;
529 }
530
531 static int get_v4lctrl(struct cx18 *cx, struct v4l2_control *ctrl)
532 {
533         switch (ctrl->id) {
534         case V4L2_CID_BRIGHTNESS:
535                 ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
536                 break;
537         case V4L2_CID_CONTRAST:
538                 ctrl->value = cx18_av_read(cx, 0x415) >> 1;
539                 break;
540         case V4L2_CID_SATURATION:
541                 ctrl->value = cx18_av_read(cx, 0x420) >> 1;
542                 break;
543         case V4L2_CID_HUE:
544                 ctrl->value = (s8)cx18_av_read(cx, 0x422);
545                 break;
546         case V4L2_CID_AUDIO_VOLUME:
547         case V4L2_CID_AUDIO_BASS:
548         case V4L2_CID_AUDIO_TREBLE:
549         case V4L2_CID_AUDIO_BALANCE:
550         case V4L2_CID_AUDIO_MUTE:
551                 return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
552         default:
553                 return -EINVAL;
554         }
555
556         return 0;
557 }
558
559 /* ----------------------------------------------------------------------- */
560
561 static int get_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
562 {
563         switch (fmt->type) {
564         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
565                 return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
566         default:
567                 return -EINVAL;
568         }
569
570         return 0;
571 }
572
573 static int set_v4lfmt(struct cx18 *cx, struct v4l2_format *fmt)
574 {
575         struct cx18_av_state *state = &cx->av_state;
576         struct v4l2_pix_format *pix;
577         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
578         int is_50Hz = !(state->std & V4L2_STD_525_60);
579
580         switch (fmt->type) {
581         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
582                 pix = &(fmt->fmt.pix);
583
584                 Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
585                 Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
586
587                 Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
588                 Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
589
590                 Vlines = pix->height + (is_50Hz ? 4 : 7);
591
592                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
593                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
594                         CX18_ERR("%dx%d is not a valid size!\n",
595                                     pix->width, pix->height);
596                         return -ERANGE;
597                 }
598
599                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
600                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
601                 VSC &= 0x1fff;
602
603                 if (pix->width >= 385)
604                         filter = 0;
605                 else if (pix->width > 192)
606                         filter = 1;
607                 else if (pix->width > 96)
608                         filter = 2;
609                 else
610                         filter = 3;
611
612                 CX18_DEBUG_INFO("decoder set size %dx%d -> scale  %ux%u\n",
613                             pix->width, pix->height, HSC, VSC);
614
615                 /* HSCALE=HSC */
616                 cx18_av_write(cx, 0x418, HSC & 0xff);
617                 cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
618                 cx18_av_write(cx, 0x41a, HSC >> 16);
619                 /* VSCALE=VSC */
620                 cx18_av_write(cx, 0x41c, VSC & 0xff);
621                 cx18_av_write(cx, 0x41d, VSC >> 8);
622                 /* VS_INTRLACE=1 VFILT=filter */
623                 cx18_av_write(cx, 0x41e, 0x8 | filter);
624                 break;
625
626         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
627                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
628
629         case V4L2_BUF_TYPE_VBI_CAPTURE:
630                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
631
632         default:
633                 return -EINVAL;
634         }
635
636         return 0;
637 }
638
639 /* ----------------------------------------------------------------------- */
640
641 int cx18_av_cmd(struct cx18 *cx, unsigned int cmd, void *arg)
642 {
643         struct cx18_av_state *state = &cx->av_state;
644         struct v4l2_tuner *vt = arg;
645         struct v4l2_routing *route = arg;
646
647         /* ignore these commands */
648         switch (cmd) {
649         case TUNER_SET_TYPE_ADDR:
650                 return 0;
651         }
652
653         if (!state->is_initialized) {
654                 CX18_DEBUG_INFO("cmd %08x triggered fw load\n", cmd);
655                 /* initialize on first use */
656                 state->is_initialized = 1;
657                 cx18_av_initialize(cx);
658         }
659
660         switch (cmd) {
661         case VIDIOC_INT_DECODE_VBI_LINE:
662                 return cx18_av_vbi(cx, cmd, arg);
663
664         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
665                 return cx18_av_audio(cx, cmd, arg);
666
667         case VIDIOC_STREAMON:
668                 CX18_DEBUG_INFO("enable output\n");
669                 cx18_av_write(cx, 0x115, 0x8c);
670                 cx18_av_write(cx, 0x116, 0x07);
671                 break;
672
673         case VIDIOC_STREAMOFF:
674                 CX18_DEBUG_INFO("disable output\n");
675                 cx18_av_write(cx, 0x115, 0x00);
676                 cx18_av_write(cx, 0x116, 0x00);
677                 break;
678
679         case VIDIOC_LOG_STATUS:
680                 log_video_status(cx);
681                 log_audio_status(cx);
682                 break;
683
684         case VIDIOC_G_CTRL:
685                 return get_v4lctrl(cx, (struct v4l2_control *)arg);
686
687         case VIDIOC_S_CTRL:
688                 return set_v4lctrl(cx, (struct v4l2_control *)arg);
689
690         case VIDIOC_QUERYCTRL:
691         {
692                 struct v4l2_queryctrl *qc = arg;
693
694                 switch (qc->id) {
695                 case V4L2_CID_BRIGHTNESS:
696                 case V4L2_CID_CONTRAST:
697                 case V4L2_CID_SATURATION:
698                 case V4L2_CID_HUE:
699                         return v4l2_ctrl_query_fill_std(qc);
700                 default:
701                         break;
702                 }
703
704                 switch (qc->id) {
705                 case V4L2_CID_AUDIO_VOLUME:
706                         return v4l2_ctrl_query_fill(qc, 0, 65535,
707                                 65535 / 100, state->default_volume);
708                 case V4L2_CID_AUDIO_MUTE:
709                 case V4L2_CID_AUDIO_BALANCE:
710                 case V4L2_CID_AUDIO_BASS:
711                 case V4L2_CID_AUDIO_TREBLE:
712                         return v4l2_ctrl_query_fill_std(qc);
713                 default:
714                         return -EINVAL;
715                 }
716                 return -EINVAL;
717         }
718
719         case VIDIOC_G_STD:
720                 *(v4l2_std_id *)arg = state->std;
721                 break;
722
723         case VIDIOC_S_STD:
724                 if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
725                         return 0;
726                 state->radio = 0;
727                 state->std = *(v4l2_std_id *)arg;
728                 return set_v4lstd(cx);
729
730         case AUDC_SET_RADIO:
731                 state->radio = 1;
732                 break;
733
734         case VIDIOC_INT_G_VIDEO_ROUTING:
735                 route->input = state->vid_input;
736                 route->output = 0;
737                 break;
738
739         case VIDIOC_INT_S_VIDEO_ROUTING:
740                 return set_input(cx, route->input, state->aud_input);
741
742         case VIDIOC_INT_G_AUDIO_ROUTING:
743                 route->input = state->aud_input;
744                 route->output = 0;
745                 break;
746
747         case VIDIOC_INT_S_AUDIO_ROUTING:
748                 return set_input(cx, state->vid_input, route->input);
749
750         case VIDIOC_S_FREQUENCY:
751                 input_change(cx);
752                 break;
753
754         case VIDIOC_G_TUNER:
755         {
756                 u8 vpres = cx18_av_read(cx, 0x40e) & 0x20;
757                 u8 mode;
758                 int val = 0;
759
760                 if (state->radio)
761                         break;
762
763                 vt->signal = vpres ? 0xffff : 0x0;
764
765                 vt->capability |=
766                     V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
767                     V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
768
769                 mode = cx18_av_read(cx, 0x804);
770
771                 /* get rxsubchans and audmode */
772                 if ((mode & 0xf) == 1)
773                         val |= V4L2_TUNER_SUB_STEREO;
774                 else
775                         val |= V4L2_TUNER_SUB_MONO;
776
777                 if (mode == 2 || mode == 4)
778                         val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
779
780                 if (mode & 0x10)
781                         val |= V4L2_TUNER_SUB_SAP;
782
783                 vt->rxsubchans = val;
784                 vt->audmode = state->audmode;
785                 break;
786         }
787
788         case VIDIOC_S_TUNER:
789                 if (state->radio)
790                         break;
791
792                 switch (vt->audmode) {
793                 case V4L2_TUNER_MODE_MONO:
794                         /* mono      -> mono
795                            stereo    -> mono
796                            bilingual -> lang1 */
797                         cx18_av_and_or(cx, 0x809, ~0xf, 0x00);
798                         break;
799                 case V4L2_TUNER_MODE_STEREO:
800                 case V4L2_TUNER_MODE_LANG1:
801                         /* mono      -> mono
802                            stereo    -> stereo
803                            bilingual -> lang1 */
804                         cx18_av_and_or(cx, 0x809, ~0xf, 0x04);
805                         break;
806                 case V4L2_TUNER_MODE_LANG1_LANG2:
807                         /* mono      -> mono
808                            stereo    -> stereo
809                            bilingual -> lang1/lang2 */
810                         cx18_av_and_or(cx, 0x809, ~0xf, 0x07);
811                         break;
812                 case V4L2_TUNER_MODE_LANG2:
813                         /* mono      -> mono
814                            stereo    -> stereo
815                            bilingual -> lang2 */
816                         cx18_av_and_or(cx, 0x809, ~0xf, 0x01);
817                         break;
818                 default:
819                         return -EINVAL;
820                 }
821                 state->audmode = vt->audmode;
822                 break;
823
824         case VIDIOC_G_FMT:
825                 return get_v4lfmt(cx, (struct v4l2_format *)arg);
826
827         case VIDIOC_S_FMT:
828                 return set_v4lfmt(cx, (struct v4l2_format *)arg);
829
830         case VIDIOC_INT_RESET:
831                 cx18_av_initialize(cx);
832                 break;
833
834         default:
835                 return -EINVAL;
836         }
837
838         return 0;
839 }
840
841 /* ----------------------------------------------------------------------- */
842
843 /* ----------------------------------------------------------------------- */
844
845 static void log_video_status(struct cx18 *cx)
846 {
847         static const char *const fmt_strs[] = {
848                 "0x0",
849                 "NTSC-M", "NTSC-J", "NTSC-4.43",
850                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
851                 "0x9", "0xA", "0xB",
852                 "SECAM",
853                 "0xD", "0xE", "0xF"
854         };
855
856         struct cx18_av_state *state = &cx->av_state;
857         u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
858         u8 gen_stat1 = cx18_av_read(cx, 0x40d);
859         u8 gen_stat2 = cx18_av_read(cx, 0x40e);
860         int vid_input = state->vid_input;
861
862         CX18_INFO("Video signal:              %spresent\n",
863                     (gen_stat2 & 0x20) ? "" : "not ");
864         CX18_INFO("Detected format:           %s\n",
865                     fmt_strs[gen_stat1 & 0xf]);
866
867         CX18_INFO("Specified standard:        %s\n",
868                     vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
869
870         if (vid_input >= CX18_AV_COMPOSITE1 &&
871             vid_input <= CX18_AV_COMPOSITE8) {
872                 CX18_INFO("Specified video input:     Composite %d\n",
873                         vid_input - CX18_AV_COMPOSITE1 + 1);
874         } else {
875                 CX18_INFO("Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
876                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
877         }
878
879         CX18_INFO("Specified audioclock freq: %d Hz\n", state->audclk_freq);
880 }
881
882 /* ----------------------------------------------------------------------- */
883
884 static void log_audio_status(struct cx18 *cx)
885 {
886         struct cx18_av_state *state = &cx->av_state;
887         u8 download_ctl = cx18_av_read(cx, 0x803);
888         u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
889         u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
890         u8 audio_config = cx18_av_read(cx, 0x808);
891         u8 pref_mode = cx18_av_read(cx, 0x809);
892         u8 afc0 = cx18_av_read(cx, 0x80b);
893         u8 mute_ctl = cx18_av_read(cx, 0x8d3);
894         int aud_input = state->aud_input;
895         char *p;
896
897         switch (mod_det_stat0) {
898         case 0x00: p = "mono"; break;
899         case 0x01: p = "stereo"; break;
900         case 0x02: p = "dual"; break;
901         case 0x04: p = "tri"; break;
902         case 0x10: p = "mono with SAP"; break;
903         case 0x11: p = "stereo with SAP"; break;
904         case 0x12: p = "dual with SAP"; break;
905         case 0x14: p = "tri with SAP"; break;
906         case 0xfe: p = "forced mode"; break;
907         default: p = "not defined"; break;
908         }
909         CX18_INFO("Detected audio mode:       %s\n", p);
910
911         switch (mod_det_stat1) {
912         case 0x00: p = "not defined"; break;
913         case 0x01: p = "EIAJ"; break;
914         case 0x02: p = "A2-M"; break;
915         case 0x03: p = "A2-BG"; break;
916         case 0x04: p = "A2-DK1"; break;
917         case 0x05: p = "A2-DK2"; break;
918         case 0x06: p = "A2-DK3"; break;
919         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
920         case 0x08: p = "AM-L"; break;
921         case 0x09: p = "NICAM-BG"; break;
922         case 0x0a: p = "NICAM-DK"; break;
923         case 0x0b: p = "NICAM-I"; break;
924         case 0x0c: p = "NICAM-L"; break;
925         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
926         case 0x0e: p = "IF FM Radio"; break;
927         case 0x0f: p = "BTSC"; break;
928         case 0x10: p = "detected chrominance"; break;
929         case 0xfd: p = "unknown audio standard"; break;
930         case 0xfe: p = "forced audio standard"; break;
931         case 0xff: p = "no detected audio standard"; break;
932         default: p = "not defined"; break;
933         }
934         CX18_INFO("Detected audio standard:   %s\n", p);
935         CX18_INFO("Audio muted:               %s\n",
936                     (mute_ctl & 0x2) ? "yes" : "no");
937         CX18_INFO("Audio microcontroller:     %s\n",
938                     (download_ctl & 0x10) ? "running" : "stopped");
939
940         switch (audio_config >> 4) {
941         case 0x00: p = "undefined"; break;
942         case 0x01: p = "BTSC"; break;
943         case 0x02: p = "EIAJ"; break;
944         case 0x03: p = "A2-M"; break;
945         case 0x04: p = "A2-BG"; break;
946         case 0x05: p = "A2-DK1"; break;
947         case 0x06: p = "A2-DK2"; break;
948         case 0x07: p = "A2-DK3"; break;
949         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
950         case 0x09: p = "AM-L"; break;
951         case 0x0a: p = "NICAM-BG"; break;
952         case 0x0b: p = "NICAM-DK"; break;
953         case 0x0c: p = "NICAM-I"; break;
954         case 0x0d: p = "NICAM-L"; break;
955         case 0x0e: p = "FM radio"; break;
956         case 0x0f: p = "automatic detection"; break;
957         default: p = "undefined"; break;
958         }
959         CX18_INFO("Configured audio standard: %s\n", p);
960
961         if ((audio_config >> 4) < 0xF) {
962                 switch (audio_config & 0xF) {
963                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
964                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
965                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
966                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
967                 case 0x04: p = "STEREO"; break;
968                 case 0x05: p = "DUAL1 (AC)"; break;
969                 case 0x06: p = "DUAL2 (BC)"; break;
970                 case 0x07: p = "DUAL3 (AB)"; break;
971                 default: p = "undefined";
972                 }
973                 CX18_INFO("Configured audio mode:     %s\n", p);
974         } else {
975                 switch (audio_config & 0xF) {
976                 case 0x00: p = "BG"; break;
977                 case 0x01: p = "DK1"; break;
978                 case 0x02: p = "DK2"; break;
979                 case 0x03: p = "DK3"; break;
980                 case 0x04: p = "I"; break;
981                 case 0x05: p = "L"; break;
982                 case 0x06: p = "BTSC"; break;
983                 case 0x07: p = "EIAJ"; break;
984                 case 0x08: p = "A2-M"; break;
985                 case 0x09: p = "FM Radio (4.5 MHz)"; break;
986                 case 0x0a: p = "FM Radio (5.5 MHz)"; break;
987                 case 0x0b: p = "S-Video"; break;
988                 case 0x0f: p = "automatic standard and mode detection"; break;
989                 default: p = "undefined"; break;
990                 }
991                 CX18_INFO("Configured audio system:   %s\n", p);
992         }
993
994         if (aud_input)
995                 CX18_INFO("Specified audio input:     Tuner (In%d)\n",
996                                 aud_input);
997         else
998                 CX18_INFO("Specified audio input:     External\n");
999
1000         switch (pref_mode & 0xf) {
1001         case 0: p = "mono/language A"; break;
1002         case 1: p = "language B"; break;
1003         case 2: p = "language C"; break;
1004         case 3: p = "analog fallback"; break;
1005         case 4: p = "stereo"; break;
1006         case 5: p = "language AC"; break;
1007         case 6: p = "language BC"; break;
1008         case 7: p = "language AB"; break;
1009         default: p = "undefined"; break;
1010         }
1011         CX18_INFO("Preferred audio mode:      %s\n", p);
1012
1013         if ((audio_config & 0xf) == 0xf) {
1014                 switch ((afc0 >> 3) & 0x1) {
1015                 case 0: p = "system DK"; break;
1016                 case 1: p = "system L"; break;
1017                 }
1018                 CX18_INFO("Selected 65 MHz format:    %s\n", p);
1019
1020                 switch (afc0 & 0x7) {
1021                 case 0: p = "Chroma"; break;
1022                 case 1: p = "BTSC"; break;
1023                 case 2: p = "EIAJ"; break;
1024                 case 3: p = "A2-M"; break;
1025                 case 4: p = "autodetect"; break;
1026                 default: p = "undefined"; break;
1027                 }
1028                 CX18_INFO("Selected 45 MHz format:    %s\n", p);
1029         }
1030 }