]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/cx18/cx18-av-core.c
V4L/DVB (10854): cx18: Correct comments about vertical and horizontal blanking timings
[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  *  Copyright (C) 2008  Andy Walls <awalls@radix.net>
8  *
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU General Public License
11  *  as published by the Free Software Foundation; either version 2
12  *  of the License, or (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  *  02110-1301, USA.
23  */
24
25 #include <media/v4l2-chip-ident.h>
26 #include "cx18-driver.h"
27 #include "cx18-io.h"
28 #include "cx18-cards.h"
29
30 int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
31 {
32         u32 reg = 0xc40000 + (addr & ~3);
33         u32 mask = 0xff;
34         int shift = (addr & 3) * 8;
35         u32 x = cx18_read_reg(cx, reg);
36
37         x = (x & ~(mask << shift)) | ((u32)value << shift);
38         cx18_write_reg(cx, x, reg);
39         return 0;
40 }
41
42 int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
43 {
44         u32 reg = 0xc40000 + (addr & ~3);
45         int shift = (addr & 3) * 8;
46         u32 x = cx18_read_reg(cx, reg);
47
48         x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
49         cx18_write_reg_expect(cx, x, reg,
50                                 ((u32)eval << shift), ((u32)mask << shift));
51         return 0;
52 }
53
54 int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
55 {
56         cx18_write_reg(cx, value, 0xc40000 + addr);
57         return 0;
58 }
59
60 int
61 cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
62 {
63         cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
64         return 0;
65 }
66
67 int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
68 {
69         cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
70         return 0;
71 }
72
73 u8 cx18_av_read(struct cx18 *cx, u16 addr)
74 {
75         u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
76         int shift = (addr & 3) * 8;
77
78         return (x >> shift) & 0xff;
79 }
80
81 u32 cx18_av_read4(struct cx18 *cx, u16 addr)
82 {
83         return cx18_read_reg(cx, 0xc40000 + addr);
84 }
85
86 int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
87                    u8 or_value)
88 {
89         return cx18_av_write(cx, addr,
90                              (cx18_av_read(cx, addr) & and_mask) |
91                              or_value);
92 }
93
94 int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
95                    u32 or_value)
96 {
97         return cx18_av_write4(cx, addr,
98                              (cx18_av_read4(cx, addr) & and_mask) |
99                              or_value);
100 }
101
102 static void cx18_av_initialize(struct cx18 *cx)
103 {
104         struct cx18_av_state *state = &cx->av_state;
105         u32 v;
106
107         cx18_av_loadfw(cx);
108         /* Stop 8051 code execution */
109         cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
110                                                  0x03000000, 0x13000000);
111
112         /* initallize the PLL by toggling sleep bit */
113         v = cx18_av_read4(cx, CXADEC_HOST_REG1);
114         /* enable sleep mode - register appears to be read only... */
115         cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
116         /* disable sleep mode */
117         cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
118                                                     v & 0xfffe, 0xffff);
119
120         /* initialize DLLs */
121         v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
122         /* disable FLD */
123         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
124         /* enable FLD */
125         cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
126
127         v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
128         /* disable FLD */
129         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
130         /* enable FLD */
131         cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
132
133         /* set analog bias currents. Set Vreg to 1.20V. */
134         cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
135
136         v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
137         /* enable TUNE_FIL_RST */
138         cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
139         /* disable TUNE_FIL_RST */
140         cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
141                               v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
142
143         /* enable 656 output */
144         cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
145
146         /* video output drive strength */
147         cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
148
149         /* reset video */
150         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
151         cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
152
153         /* set video to auto-detect */
154         /* Clear bits 11-12 to enable slow locking mode.  Set autodetect mode */
155         /* set the comb notch = 1 */
156         cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
157
158         /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
159         /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
160         cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
161
162         /* Set VGA_TRACK_RANGE to 0x20 */
163         cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
164
165         /*
166          * Initial VBI setup
167          * VIP-1.1, 10 bit mode, enable Raw, disable sliced,
168          * don't clamp raw samples when codes are in use, 1 byte user D-words,
169          * IDID0 has line #, RP code V bit transition on VBLANK, data during
170          * blanking intervals
171          */
172         cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);
173
174         /* Set the video input.
175            The setting in MODE_CTRL gets lost when we do the above setup */
176         /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
177         /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
178
179         v = cx18_av_read4(cx, CXADEC_AFE_CTRL);
180         v &= 0xFFFBFFFF;            /* turn OFF bit 18 for droop_comp_ch1 */
181         v &= 0xFFFF7FFF;            /* turn OFF bit 9 for clamp_sel_ch1 */
182         v &= 0xFFFFFFFE;            /* turn OFF bit 0 for 12db_ch1 */
183         /* v |= 0x00000001;*/            /* turn ON bit 0 for 12db_ch1 */
184         cx18_av_write4(cx, CXADEC_AFE_CTRL, v);
185
186 /*      if(dwEnable && dw3DCombAvailable) { */
187 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
188 /*    } else { */
189 /*              CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
190 /*    } */
191         cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
192         state->default_volume = 228 - cx18_av_read(cx, 0x8d4);
193         state->default_volume = ((state->default_volume / 2) + 23) << 9;
194 }
195
196 static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)
197 {
198         struct cx18 *cx = v4l2_get_subdevdata(sd);
199
200         cx18_av_initialize(cx);
201         return 0;
202 }
203
204 static int cx18_av_init(struct v4l2_subdev *sd, u32 val)
205 {
206         struct cx18_av_state *state = to_cx18_av_state(sd);
207         struct cx18 *cx = v4l2_get_subdevdata(sd);
208
209         switch (val) {
210         case CX18_AV_INIT_PLLS:
211                 /*
212                  * The crystal freq used in calculations in this driver will be
213                  * 28.636360 MHz.
214                  * Aim to run the PLLs' VCOs near 400 MHz to minimze errors.
215                  */
216
217                 /*
218                  * VDCLK  Integer = 0x0f, Post Divider = 0x04
219                  * AIMCLK Integer = 0x0e, Post Divider = 0x16
220                  */
221                 cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);
222
223                 /* VDCLK Fraction = 0x2be2fe */
224                 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */
225                 cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);
226
227                 /* AIMCLK Fraction = 0x05227ad */
228                 /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/
229                 cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);
230
231                 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
232                 cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);
233                 break;
234
235         case CX18_AV_INIT_NORMAL:
236         default:
237                 if (!state->is_initialized) {
238                         /* initialize on first use */
239                         state->is_initialized = 1;
240                         cx18_av_initialize(cx);
241                 }
242                 break;
243         }
244         return 0;
245 }
246
247 void cx18_av_std_setup(struct cx18 *cx)
248 {
249         struct cx18_av_state *state = &cx->av_state;
250         struct v4l2_subdev *sd = &state->sd;
251         v4l2_std_id std = state->std;
252         int hblank, hactive, burst, vblank, vactive, sc;
253         int vblank656, src_decimation;
254         int luma_lpf, uv_lpf, comb;
255         u32 pll_int, pll_frac, pll_post;
256
257         /* datasheet startup, step 8d */
258         if (std & ~V4L2_STD_NTSC)
259                 cx18_av_write(cx, 0x49f, 0x11);
260         else
261                 cx18_av_write(cx, 0x49f, 0x14);
262
263         if (std & V4L2_STD_625_50) {
264                 /* FIXME - revisit these for Sliced VBI */
265                 hblank = 132;
266                 hactive = 720;
267                 burst = 93;
268                 vblank = 36;
269                 vactive = 580;
270                 vblank656 = 40;
271                 src_decimation = 0x21f;
272
273                 luma_lpf = 2;
274                 if (std & V4L2_STD_PAL) {
275                         uv_lpf = 1;
276                         comb = 0x20;
277                         sc = 688739;
278                 } else if (std == V4L2_STD_PAL_Nc) {
279                         uv_lpf = 1;
280                         comb = 0x20;
281                         sc = 556453;
282                 } else { /* SECAM */
283                         uv_lpf = 0;
284                         comb = 0;
285                         sc = 672351;
286                 }
287         } else {
288                 /*
289                  * The following relationships of half line counts should hold:
290                  * 525 = vsync + vactive + vblank656
291                  * 12 = vblank656 - vblank
292                  *
293                  * vsync:     always 6 half-lines of vsync pulses
294                  * vactive:   half lines of active video
295                  * vblank656: half lines, after line 3/mid-266, of blanked video
296                  * vblank:    half lines, after line 9/272, of blanked video
297                  *
298                  * As far as I can tell:
299                  * vblank656 starts counting from the falling edge of the first
300                  *      vsync pulse (start of line 4 or mid-266)
301                  * vblank starts counting from the after the 6 vsync pulses and
302                  *      6 or 5 equalization pulses (start of line 10 or 272)
303                  *
304                  * For 525 line systems the driver will extract VBI information
305                  * from lines 10-21 and lines 273-284.
306                  */
307                 vblank656 = 38; /* lines  4 -  22  &  266 - 284 */
308                 vblank = 26;    /* lines 10 -  22  &  272 - 284 */
309                 vactive = 481;  /* lines 23 - 263  &  285 - 525 */
310
311                 /*
312                  * For a 13.5 Mpps clock and 15,734.26 Hz line rate, a line is
313                  * is 858 pixels = 720 active + 138 blanking.  The Hsync leading
314                  * edge should happen 1.2 us * 13.5 Mpps ~= 16 pixels after the
315                  * end of active video, leaving 122 pixels of hblank to ignore
316                  * before active video starts.
317                  */
318                 hactive = 720;
319                 hblank = 122;
320                 luma_lpf = 1;
321                 uv_lpf = 1;
322
323                 src_decimation = 0x21f;
324                 if (std == V4L2_STD_PAL_60) {
325                         burst = 0x5b;
326                         luma_lpf = 2;
327                         comb = 0x20;
328                         sc = 688739;
329                 } else if (std == V4L2_STD_PAL_M) {
330                         burst = 0x61;
331                         comb = 0x20;
332                         sc = 555452;
333                 } else {
334                         burst = 0x5b;
335                         comb = 0x66;
336                         sc = 556063;
337                 }
338         }
339
340         /* DEBUG: Displays configured PLL frequency */
341         pll_int = cx18_av_read(cx, 0x108);
342         pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
343         pll_post = cx18_av_read(cx, 0x109);
344         CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",
345                             pll_int, pll_frac, pll_post);
346
347         if (pll_post) {
348                 int fin, fsc, pll;
349
350                 pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
351                 pll /= pll_post;
352                 CX18_DEBUG_INFO_DEV(sd, "PLL = %d.%06d MHz\n",
353                                     pll / 1000000, pll % 1000000);
354                 CX18_DEBUG_INFO_DEV(sd, "PLL/8 = %d.%06d MHz\n",
355                                     pll / 8000000, (pll / 8) % 1000000);
356
357                 fin = ((u64)src_decimation * pll) >> 12;
358                 CX18_DEBUG_INFO_DEV(sd, "ADC Sampling freq = %d.%06d MHz\n",
359                                     fin / 1000000, fin % 1000000);
360
361                 fsc = (((u64)sc) * pll) >> 24L;
362                 CX18_DEBUG_INFO_DEV(sd,
363                                     "Chroma sub-carrier freq = %d.%06d MHz\n",
364                                     fsc / 1000000, fsc % 1000000);
365
366                 CX18_DEBUG_INFO_DEV(sd, "hblank %i, hactive %i, vblank %i, "
367                                     "vactive %i, vblank656 %i, src_dec %i, "
368                                     "burst 0x%02x, luma_lpf %i, uv_lpf %i, "
369                                     "comb 0x%02x, sc 0x%06x\n",
370                                     hblank, hactive, vblank, vactive, vblank656,
371                                     src_decimation, burst, luma_lpf, uv_lpf,
372                                     comb, sc);
373         }
374
375         /* Sets horizontal blanking delay and active lines */
376         cx18_av_write(cx, 0x470, hblank);
377         cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
378                                                 (hactive << 4)));
379         cx18_av_write(cx, 0x472, hactive >> 4);
380
381         /* Sets burst gate delay */
382         cx18_av_write(cx, 0x473, burst);
383
384         /* Sets vertical blanking delay and active duration */
385         cx18_av_write(cx, 0x474, vblank);
386         cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
387                                                 (vactive << 4)));
388         cx18_av_write(cx, 0x476, vactive >> 4);
389         cx18_av_write(cx, 0x477, vblank656);
390
391         /* Sets src decimation rate */
392         cx18_av_write(cx, 0x478, 0xff & src_decimation);
393         cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
394
395         /* Sets Luma and UV Low pass filters */
396         cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
397
398         /* Enables comb filters */
399         cx18_av_write(cx, 0x47b, comb);
400
401         /* Sets SC Step*/
402         cx18_av_write(cx, 0x47c, sc);
403         cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
404         cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
405
406         if (std & V4L2_STD_625_50) {
407                 state->slicer_line_delay = 1;
408                 state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
409         } else {
410                 state->slicer_line_delay = 0;
411                 state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
412         }
413         cx18_av_write(cx, 0x47f, state->slicer_line_delay);
414 }
415
416 static int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
417                                    struct v4l2_decode_vbi_line *vbi_line)
418 {
419         struct cx18 *cx = v4l2_get_subdevdata(sd);
420         return cx18_av_vbi(cx, VIDIOC_INT_DECODE_VBI_LINE, vbi_line);
421 }
422
423 static int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
424 {
425         struct cx18 *cx = v4l2_get_subdevdata(sd);
426         return cx18_av_audio(cx, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
427 }
428
429 static void input_change(struct cx18 *cx)
430 {
431         struct cx18_av_state *state = &cx->av_state;
432         v4l2_std_id std = state->std;
433         u8 v;
434
435         /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
436         cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
437         cx18_av_and_or(cx, 0x401, ~0x60, 0);
438         cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
439
440         if (std & V4L2_STD_525_60) {
441                 if (std == V4L2_STD_NTSC_M_JP) {
442                         /* Japan uses EIAJ audio standard */
443                         cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
444                         cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
445                 } else if (std == V4L2_STD_NTSC_M_KR) {
446                         /* South Korea uses A2 audio standard */
447                         cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
448                         cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
449                 } else {
450                         /* Others use the BTSC audio standard */
451                         cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
452                         cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
453                 }
454         } else if (std & V4L2_STD_PAL) {
455                 /* Follow tuner change procedure for PAL */
456                 cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
457                 cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
458         } else if (std & V4L2_STD_SECAM) {
459                 /* Select autodetect for SECAM */
460                 cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
461                 cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
462         }
463
464         v = cx18_av_read(cx, 0x803);
465         if (v & 0x10) {
466                 /* restart audio decoder microcontroller */
467                 v &= ~0x10;
468                 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
469                 v |= 0x10;
470                 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
471         }
472 }
473
474 static int cx18_av_s_frequency(struct v4l2_subdev *sd,
475                                struct v4l2_frequency *freq)
476 {
477         struct cx18 *cx = v4l2_get_subdevdata(sd);
478         input_change(cx);
479         return 0;
480 }
481
482 static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
483                                         enum cx18_av_audio_input aud_input)
484 {
485         struct cx18_av_state *state = &cx->av_state;
486         struct v4l2_subdev *sd = &state->sd;
487         u8 is_composite = (vid_input >= CX18_AV_COMPOSITE1 &&
488                            vid_input <= CX18_AV_COMPOSITE8);
489         u8 reg;
490         u8 v;
491
492         CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",
493                             vid_input, aud_input);
494
495         if (is_composite) {
496                 reg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
497         } else {
498                 int luma = vid_input & 0xf0;
499                 int chroma = vid_input & 0xf00;
500
501                 if ((vid_input & ~0xff0) ||
502                     luma < CX18_AV_SVIDEO_LUMA1 ||
503                     luma > CX18_AV_SVIDEO_LUMA8 ||
504                     chroma < CX18_AV_SVIDEO_CHROMA4 ||
505                     chroma > CX18_AV_SVIDEO_CHROMA8) {
506                         CX18_ERR_DEV(sd, "0x%04x is not a valid video input!\n",
507                                      vid_input);
508                         return -EINVAL;
509                 }
510                 reg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
511                 if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
512                         reg &= 0x3f;
513                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
514                 } else {
515                         reg &= 0xcf;
516                         reg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
517                 }
518         }
519
520         switch (aud_input) {
521         case CX18_AV_AUDIO_SERIAL1:
522         case CX18_AV_AUDIO_SERIAL2:
523                 /* do nothing, use serial audio input */
524                 break;
525         case CX18_AV_AUDIO4: reg &= ~0x30; break;
526         case CX18_AV_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
527         case CX18_AV_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
528         case CX18_AV_AUDIO7: reg &= ~0xc0; break;
529         case CX18_AV_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
530
531         default:
532                 CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",
533                              aud_input);
534                 return -EINVAL;
535         }
536
537         cx18_av_write_expect(cx, 0x103, reg, reg, 0xf7);
538         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
539         cx18_av_and_or(cx, 0x401, ~0x6, is_composite ? 0 : 0x02);
540
541         /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
542         v = cx18_av_read(cx, 0x102);
543         if (reg & 0x80)
544                 v &= ~0x2;
545         else
546                 v |= 0x2;
547         /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
548         if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
549                 v |= 0x4;
550         else
551                 v &= ~0x4;
552         cx18_av_write_expect(cx, 0x102, v, v, 0x17);
553
554         /*cx18_av_and_or4(cx, 0x104, ~0x001b4180, 0x00004180);*/
555
556         state->vid_input = vid_input;
557         state->aud_input = aud_input;
558         cx18_av_audio_set_path(cx);
559         input_change(cx);
560         return 0;
561 }
562
563 static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
564                                    const struct v4l2_routing *route)
565 {
566         struct cx18_av_state *state = to_cx18_av_state(sd);
567         struct cx18 *cx = v4l2_get_subdevdata(sd);
568         return set_input(cx, route->input, state->aud_input);
569 }
570
571 static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
572                                    const struct v4l2_routing *route)
573 {
574         struct cx18_av_state *state = to_cx18_av_state(sd);
575         struct cx18 *cx = v4l2_get_subdevdata(sd);
576         return set_input(cx, state->vid_input, route->input);
577 }
578
579 static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
580 {
581         struct cx18_av_state *state = to_cx18_av_state(sd);
582         struct cx18 *cx = v4l2_get_subdevdata(sd);
583         u8 vpres;
584         u8 mode;
585         int val = 0;
586
587         if (state->radio)
588                 return 0;
589
590         vpres = cx18_av_read(cx, 0x40e) & 0x20;
591         vt->signal = vpres ? 0xffff : 0x0;
592
593         vt->capability |=
594                     V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
595                     V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
596
597         mode = cx18_av_read(cx, 0x804);
598
599         /* get rxsubchans and audmode */
600         if ((mode & 0xf) == 1)
601                 val |= V4L2_TUNER_SUB_STEREO;
602         else
603                 val |= V4L2_TUNER_SUB_MONO;
604
605         if (mode == 2 || mode == 4)
606                 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
607
608         if (mode & 0x10)
609                 val |= V4L2_TUNER_SUB_SAP;
610
611         vt->rxsubchans = val;
612         vt->audmode = state->audmode;
613         return 0;
614 }
615
616 static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
617 {
618         struct cx18_av_state *state = to_cx18_av_state(sd);
619         struct cx18 *cx = v4l2_get_subdevdata(sd);
620         u8 v;
621
622         if (state->radio)
623                 return 0;
624
625         v = cx18_av_read(cx, 0x809);
626         v &= ~0xf;
627
628         switch (vt->audmode) {
629         case V4L2_TUNER_MODE_MONO:
630                 /* mono      -> mono
631                    stereo    -> mono
632                    bilingual -> lang1 */
633                 break;
634         case V4L2_TUNER_MODE_STEREO:
635         case V4L2_TUNER_MODE_LANG1:
636                 /* mono      -> mono
637                    stereo    -> stereo
638                    bilingual -> lang1 */
639                 v |= 0x4;
640                 break;
641         case V4L2_TUNER_MODE_LANG1_LANG2:
642                 /* mono      -> mono
643                    stereo    -> stereo
644                    bilingual -> lang1/lang2 */
645                 v |= 0x7;
646                 break;
647         case V4L2_TUNER_MODE_LANG2:
648                 /* mono      -> mono
649                    stereo    -> stereo
650                    bilingual -> lang2 */
651                 v |= 0x1;
652                 break;
653         default:
654                 return -EINVAL;
655         }
656         cx18_av_write_expect(cx, 0x809, v, v, 0xff);
657         state->audmode = vt->audmode;
658         return 0;
659 }
660
661 static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
662 {
663         struct cx18_av_state *state = to_cx18_av_state(sd);
664         struct cx18 *cx = v4l2_get_subdevdata(sd);
665
666         u8 fmt = 0;     /* zero is autodetect */
667         u8 pal_m = 0;
668
669         if (state->radio == 0 && state->std == norm)
670                 return 0;
671
672         state->radio = 0;
673         state->std = norm;
674
675         /* First tests should be against specific std */
676         if (state->std == V4L2_STD_NTSC_M_JP) {
677                 fmt = 0x2;
678         } else if (state->std == V4L2_STD_NTSC_443) {
679                 fmt = 0x3;
680         } else if (state->std == V4L2_STD_PAL_M) {
681                 pal_m = 1;
682                 fmt = 0x5;
683         } else if (state->std == V4L2_STD_PAL_N) {
684                 fmt = 0x6;
685         } else if (state->std == V4L2_STD_PAL_Nc) {
686                 fmt = 0x7;
687         } else if (state->std == V4L2_STD_PAL_60) {
688                 fmt = 0x8;
689         } else {
690                 /* Then, test against generic ones */
691                 if (state->std & V4L2_STD_NTSC)
692                         fmt = 0x1;
693                 else if (state->std & V4L2_STD_PAL)
694                         fmt = 0x4;
695                 else if (state->std & V4L2_STD_SECAM)
696                         fmt = 0xc;
697         }
698
699         CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);
700
701         /* Follow step 9 of section 3.16 in the cx18_av datasheet.
702            Without this PAL may display a vertical ghosting effect.
703            This happens for example with the Yuan MPC622. */
704         if (fmt >= 4 && fmt < 8) {
705                 /* Set format to NTSC-M */
706                 cx18_av_and_or(cx, 0x400, ~0xf, 1);
707                 /* Turn off LCOMB */
708                 cx18_av_and_or(cx, 0x47b, ~6, 0);
709         }
710         cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
711         cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
712         cx18_av_std_setup(cx);
713         input_change(cx);
714         return 0;
715 }
716
717 static int cx18_av_s_radio(struct v4l2_subdev *sd)
718 {
719         struct cx18_av_state *state = to_cx18_av_state(sd);
720         state->radio = 1;
721         return 0;
722 }
723
724 static int cx18_av_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
725 {
726         struct cx18 *cx = v4l2_get_subdevdata(sd);
727
728         switch (ctrl->id) {
729         case V4L2_CID_BRIGHTNESS:
730                 if (ctrl->value < 0 || ctrl->value > 255) {
731                         CX18_ERR_DEV(sd, "invalid brightness setting %d\n",
732                                      ctrl->value);
733                         return -ERANGE;
734                 }
735
736                 cx18_av_write(cx, 0x414, ctrl->value - 128);
737                 break;
738
739         case V4L2_CID_CONTRAST:
740                 if (ctrl->value < 0 || ctrl->value > 127) {
741                         CX18_ERR_DEV(sd, "invalid contrast setting %d\n",
742                                      ctrl->value);
743                         return -ERANGE;
744                 }
745
746                 cx18_av_write(cx, 0x415, ctrl->value << 1);
747                 break;
748
749         case V4L2_CID_SATURATION:
750                 if (ctrl->value < 0 || ctrl->value > 127) {
751                         CX18_ERR_DEV(sd, "invalid saturation setting %d\n",
752                                      ctrl->value);
753                         return -ERANGE;
754                 }
755
756                 cx18_av_write(cx, 0x420, ctrl->value << 1);
757                 cx18_av_write(cx, 0x421, ctrl->value << 1);
758                 break;
759
760         case V4L2_CID_HUE:
761                 if (ctrl->value < -128 || ctrl->value > 127) {
762                         CX18_ERR_DEV(sd, "invalid hue setting %d\n",
763                                      ctrl->value);
764                         return -ERANGE;
765                 }
766
767                 cx18_av_write(cx, 0x422, ctrl->value);
768                 break;
769
770         case V4L2_CID_AUDIO_VOLUME:
771         case V4L2_CID_AUDIO_BASS:
772         case V4L2_CID_AUDIO_TREBLE:
773         case V4L2_CID_AUDIO_BALANCE:
774         case V4L2_CID_AUDIO_MUTE:
775                 return cx18_av_audio(cx, VIDIOC_S_CTRL, ctrl);
776
777         default:
778                 return -EINVAL;
779         }
780         return 0;
781 }
782
783 static int cx18_av_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
784 {
785         struct cx18 *cx = v4l2_get_subdevdata(sd);
786
787         switch (ctrl->id) {
788         case V4L2_CID_BRIGHTNESS:
789                 ctrl->value = (s8)cx18_av_read(cx, 0x414) + 128;
790                 break;
791         case V4L2_CID_CONTRAST:
792                 ctrl->value = cx18_av_read(cx, 0x415) >> 1;
793                 break;
794         case V4L2_CID_SATURATION:
795                 ctrl->value = cx18_av_read(cx, 0x420) >> 1;
796                 break;
797         case V4L2_CID_HUE:
798                 ctrl->value = (s8)cx18_av_read(cx, 0x422);
799                 break;
800         case V4L2_CID_AUDIO_VOLUME:
801         case V4L2_CID_AUDIO_BASS:
802         case V4L2_CID_AUDIO_TREBLE:
803         case V4L2_CID_AUDIO_BALANCE:
804         case V4L2_CID_AUDIO_MUTE:
805                 return cx18_av_audio(cx, VIDIOC_G_CTRL, ctrl);
806         default:
807                 return -EINVAL;
808         }
809         return 0;
810 }
811
812 static int cx18_av_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
813 {
814         struct cx18_av_state *state = to_cx18_av_state(sd);
815
816         switch (qc->id) {
817         case V4L2_CID_BRIGHTNESS:
818                 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
819         case V4L2_CID_CONTRAST:
820         case V4L2_CID_SATURATION:
821                 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
822         case V4L2_CID_HUE:
823                 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
824         default:
825                 break;
826         }
827
828         switch (qc->id) {
829         case V4L2_CID_AUDIO_VOLUME:
830                 return v4l2_ctrl_query_fill(qc, 0, 65535,
831                         65535 / 100, state->default_volume);
832         case V4L2_CID_AUDIO_MUTE:
833                 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
834         case V4L2_CID_AUDIO_BALANCE:
835         case V4L2_CID_AUDIO_BASS:
836         case V4L2_CID_AUDIO_TREBLE:
837                 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
838         default:
839                 return -EINVAL;
840         }
841         return -EINVAL;
842 }
843
844 static int cx18_av_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
845 {
846         struct cx18 *cx = v4l2_get_subdevdata(sd);
847
848         switch (fmt->type) {
849         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
850                 return cx18_av_vbi(cx, VIDIOC_G_FMT, fmt);
851         default:
852                 return -EINVAL;
853         }
854         return 0;
855 }
856
857 static int cx18_av_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
858 {
859         struct cx18_av_state *state = to_cx18_av_state(sd);
860         struct cx18 *cx = v4l2_get_subdevdata(sd);
861
862         struct v4l2_pix_format *pix;
863         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
864         int is_50Hz = !(state->std & V4L2_STD_525_60);
865
866         switch (fmt->type) {
867         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
868                 pix = &(fmt->fmt.pix);
869
870                 Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
871                 Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
872
873                 Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
874                 Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
875
876                 /*
877                  * This adjustment reflects the excess of vactive, set in
878                  * cx18_av_std_setup(), above standard values:
879                  *
880                  * 480 + 1 for 60 Hz systems
881                  * 576 + 4 for 50 Hz systems
882                  */
883                 Vlines = pix->height + (is_50Hz ? 4 : 1);
884
885                 /*
886                  * Invalid height and width scaling requests are:
887                  * 1. width less than 1/16 of the source width
888                  * 2. width greater than the source width
889                  * 3. height less than 1/8 of the source height
890                  * 4. height greater than the source height
891                  */
892                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
893                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
894                         CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",
895                                      pix->width, pix->height);
896                         return -ERANGE;
897                 }
898
899                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
900                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
901                 VSC &= 0x1fff;
902
903                 if (pix->width >= 385)
904                         filter = 0;
905                 else if (pix->width > 192)
906                         filter = 1;
907                 else if (pix->width > 96)
908                         filter = 2;
909                 else
910                         filter = 3;
911
912                 CX18_DEBUG_INFO_DEV(sd,
913                                     "decoder set size %dx%d -> scale  %ux%u\n",
914                                     pix->width, pix->height, HSC, VSC);
915
916                 /* HSCALE=HSC */
917                 cx18_av_write(cx, 0x418, HSC & 0xff);
918                 cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
919                 cx18_av_write(cx, 0x41a, HSC >> 16);
920                 /* VSCALE=VSC */
921                 cx18_av_write(cx, 0x41c, VSC & 0xff);
922                 cx18_av_write(cx, 0x41d, VSC >> 8);
923                 /* VS_INTRLACE=1 VFILT=filter */
924                 cx18_av_write(cx, 0x41e, 0x8 | filter);
925                 break;
926
927         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
928                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
929
930         case V4L2_BUF_TYPE_VBI_CAPTURE:
931                 return cx18_av_vbi(cx, VIDIOC_S_FMT, fmt);
932
933         default:
934                 return -EINVAL;
935         }
936         return 0;
937 }
938
939 static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
940 {
941         struct cx18 *cx = v4l2_get_subdevdata(sd);
942
943         CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");
944         if (enable) {
945                 cx18_av_write(cx, 0x115, 0x8c);
946                 cx18_av_write(cx, 0x116, 0x07);
947         } else {
948                 cx18_av_write(cx, 0x115, 0x00);
949                 cx18_av_write(cx, 0x116, 0x00);
950         }
951         return 0;
952 }
953
954 static void log_video_status(struct cx18 *cx)
955 {
956         static const char *const fmt_strs[] = {
957                 "0x0",
958                 "NTSC-M", "NTSC-J", "NTSC-4.43",
959                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
960                 "0x9", "0xA", "0xB",
961                 "SECAM",
962                 "0xD", "0xE", "0xF"
963         };
964
965         struct cx18_av_state *state = &cx->av_state;
966         struct v4l2_subdev *sd = &state->sd;
967         u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
968         u8 gen_stat1 = cx18_av_read(cx, 0x40d);
969         u8 gen_stat2 = cx18_av_read(cx, 0x40e);
970         int vid_input = state->vid_input;
971
972         CX18_INFO_DEV(sd, "Video signal:              %spresent\n",
973                       (gen_stat2 & 0x20) ? "" : "not ");
974         CX18_INFO_DEV(sd, "Detected format:           %s\n",
975                       fmt_strs[gen_stat1 & 0xf]);
976
977         CX18_INFO_DEV(sd, "Specified standard:        %s\n",
978                       vidfmt_sel ? fmt_strs[vidfmt_sel]
979                                  : "automatic detection");
980
981         if (vid_input >= CX18_AV_COMPOSITE1 &&
982             vid_input <= CX18_AV_COMPOSITE8) {
983                 CX18_INFO_DEV(sd, "Specified video input:     Composite %d\n",
984                               vid_input - CX18_AV_COMPOSITE1 + 1);
985         } else {
986                 CX18_INFO_DEV(sd, "Specified video input:     "
987                               "S-Video (Luma In%d, Chroma In%d)\n",
988                               (vid_input & 0xf0) >> 4,
989                               (vid_input & 0xf00) >> 8);
990         }
991
992         CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",
993                       state->audclk_freq);
994 }
995
996 static void log_audio_status(struct cx18 *cx)
997 {
998         struct cx18_av_state *state = &cx->av_state;
999         struct v4l2_subdev *sd = &state->sd;
1000         u8 download_ctl = cx18_av_read(cx, 0x803);
1001         u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
1002         u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
1003         u8 audio_config = cx18_av_read(cx, 0x808);
1004         u8 pref_mode = cx18_av_read(cx, 0x809);
1005         u8 afc0 = cx18_av_read(cx, 0x80b);
1006         u8 mute_ctl = cx18_av_read(cx, 0x8d3);
1007         int aud_input = state->aud_input;
1008         char *p;
1009
1010         switch (mod_det_stat0) {
1011         case 0x00: p = "mono"; break;
1012         case 0x01: p = "stereo"; break;
1013         case 0x02: p = "dual"; break;
1014         case 0x04: p = "tri"; break;
1015         case 0x10: p = "mono with SAP"; break;
1016         case 0x11: p = "stereo with SAP"; break;
1017         case 0x12: p = "dual with SAP"; break;
1018         case 0x14: p = "tri with SAP"; break;
1019         case 0xfe: p = "forced mode"; break;
1020         default: p = "not defined"; break;
1021         }
1022         CX18_INFO_DEV(sd, "Detected audio mode:       %s\n", p);
1023
1024         switch (mod_det_stat1) {
1025         case 0x00: p = "not defined"; break;
1026         case 0x01: p = "EIAJ"; break;
1027         case 0x02: p = "A2-M"; break;
1028         case 0x03: p = "A2-BG"; break;
1029         case 0x04: p = "A2-DK1"; break;
1030         case 0x05: p = "A2-DK2"; break;
1031         case 0x06: p = "A2-DK3"; break;
1032         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1033         case 0x08: p = "AM-L"; break;
1034         case 0x09: p = "NICAM-BG"; break;
1035         case 0x0a: p = "NICAM-DK"; break;
1036         case 0x0b: p = "NICAM-I"; break;
1037         case 0x0c: p = "NICAM-L"; break;
1038         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1039         case 0x0e: p = "IF FM Radio"; break;
1040         case 0x0f: p = "BTSC"; break;
1041         case 0x10: p = "detected chrominance"; break;
1042         case 0xfd: p = "unknown audio standard"; break;
1043         case 0xfe: p = "forced audio standard"; break;
1044         case 0xff: p = "no detected audio standard"; break;
1045         default: p = "not defined"; break;
1046         }
1047         CX18_INFO_DEV(sd, "Detected audio standard:   %s\n", p);
1048         CX18_INFO_DEV(sd, "Audio muted:               %s\n",
1049                       (mute_ctl & 0x2) ? "yes" : "no");
1050         CX18_INFO_DEV(sd, "Audio microcontroller:     %s\n",
1051                       (download_ctl & 0x10) ? "running" : "stopped");
1052
1053         switch (audio_config >> 4) {
1054         case 0x00: p = "undefined"; break;
1055         case 0x01: p = "BTSC"; break;
1056         case 0x02: p = "EIAJ"; break;
1057         case 0x03: p = "A2-M"; break;
1058         case 0x04: p = "A2-BG"; break;
1059         case 0x05: p = "A2-DK1"; break;
1060         case 0x06: p = "A2-DK2"; break;
1061         case 0x07: p = "A2-DK3"; break;
1062         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1063         case 0x09: p = "AM-L"; break;
1064         case 0x0a: p = "NICAM-BG"; break;
1065         case 0x0b: p = "NICAM-DK"; break;
1066         case 0x0c: p = "NICAM-I"; break;
1067         case 0x0d: p = "NICAM-L"; break;
1068         case 0x0e: p = "FM radio"; break;
1069         case 0x0f: p = "automatic detection"; break;
1070         default: p = "undefined"; break;
1071         }
1072         CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);
1073
1074         if ((audio_config >> 4) < 0xF) {
1075                 switch (audio_config & 0xF) {
1076                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1077                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1078                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1079                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1080                 case 0x04: p = "STEREO"; break;
1081                 case 0x05: p = "DUAL1 (AC)"; break;
1082                 case 0x06: p = "DUAL2 (BC)"; break;
1083                 case 0x07: p = "DUAL3 (AB)"; break;
1084                 default: p = "undefined";
1085                 }
1086                 CX18_INFO_DEV(sd, "Configured audio mode:     %s\n", p);
1087         } else {
1088                 switch (audio_config & 0xF) {
1089                 case 0x00: p = "BG"; break;
1090                 case 0x01: p = "DK1"; break;
1091                 case 0x02: p = "DK2"; break;
1092                 case 0x03: p = "DK3"; break;
1093                 case 0x04: p = "I"; break;
1094                 case 0x05: p = "L"; break;
1095                 case 0x06: p = "BTSC"; break;
1096                 case 0x07: p = "EIAJ"; break;
1097                 case 0x08: p = "A2-M"; break;
1098                 case 0x09: p = "FM Radio (4.5 MHz)"; break;
1099                 case 0x0a: p = "FM Radio (5.5 MHz)"; break;
1100                 case 0x0b: p = "S-Video"; break;
1101                 case 0x0f: p = "automatic standard and mode detection"; break;
1102                 default: p = "undefined"; break;
1103                 }
1104                 CX18_INFO_DEV(sd, "Configured audio system:   %s\n", p);
1105         }
1106
1107         if (aud_input)
1108                 CX18_INFO_DEV(sd, "Specified audio input:     Tuner (In%d)\n",
1109                               aud_input);
1110         else
1111                 CX18_INFO_DEV(sd, "Specified audio input:     External\n");
1112
1113         switch (pref_mode & 0xf) {
1114         case 0: p = "mono/language A"; break;
1115         case 1: p = "language B"; break;
1116         case 2: p = "language C"; break;
1117         case 3: p = "analog fallback"; break;
1118         case 4: p = "stereo"; break;
1119         case 5: p = "language AC"; break;
1120         case 6: p = "language BC"; break;
1121         case 7: p = "language AB"; break;
1122         default: p = "undefined"; break;
1123         }
1124         CX18_INFO_DEV(sd, "Preferred audio mode:      %s\n", p);
1125
1126         if ((audio_config & 0xf) == 0xf) {
1127                 switch ((afc0 >> 3) & 0x1) {
1128                 case 0: p = "system DK"; break;
1129                 case 1: p = "system L"; break;
1130                 }
1131                 CX18_INFO_DEV(sd, "Selected 65 MHz format:    %s\n", p);
1132
1133                 switch (afc0 & 0x7) {
1134                 case 0: p = "Chroma"; break;
1135                 case 1: p = "BTSC"; break;
1136                 case 2: p = "EIAJ"; break;
1137                 case 3: p = "A2-M"; break;
1138                 case 4: p = "autodetect"; break;
1139                 default: p = "undefined"; break;
1140                 }
1141                 CX18_INFO_DEV(sd, "Selected 45 MHz format:    %s\n", p);
1142         }
1143 }
1144
1145 static int cx18_av_log_status(struct v4l2_subdev *sd)
1146 {
1147         struct cx18 *cx = v4l2_get_subdevdata(sd);
1148         log_video_status(cx);
1149         log_audio_status(cx);
1150         return 0;
1151 }
1152
1153 static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)
1154 {
1155         return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;
1156 }
1157
1158 static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,
1159                                 struct v4l2_dbg_chip_ident *chip)
1160 {
1161         struct cx18_av_state *state = to_cx18_av_state(sd);
1162
1163         if (cx18_av_dbg_match(&chip->match)) {
1164                 chip->ident = state->id;
1165                 chip->revision = state->rev;
1166         }
1167         return 0;
1168 }
1169
1170 #ifdef CONFIG_VIDEO_ADV_DEBUG
1171 static int cx18_av_g_register(struct v4l2_subdev *sd,
1172                               struct v4l2_dbg_register *reg)
1173 {
1174         struct cx18 *cx = v4l2_get_subdevdata(sd);
1175
1176         if (!cx18_av_dbg_match(&reg->match))
1177                 return -EINVAL;
1178         if ((reg->reg & 0x3) != 0)
1179                 return -EINVAL;
1180         if (!capable(CAP_SYS_ADMIN))
1181                 return -EPERM;
1182         reg->size = 4;
1183         reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
1184         return 0;
1185 }
1186
1187 static int cx18_av_s_register(struct v4l2_subdev *sd,
1188                               struct v4l2_dbg_register *reg)
1189 {
1190         struct cx18 *cx = v4l2_get_subdevdata(sd);
1191
1192         if (!cx18_av_dbg_match(&reg->match))
1193                 return -EINVAL;
1194         if ((reg->reg & 0x3) != 0)
1195                 return -EINVAL;
1196         if (!capable(CAP_SYS_ADMIN))
1197                 return -EPERM;
1198         cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
1199         return 0;
1200 }
1201 #endif
1202
1203 static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
1204         .g_chip_ident = cx18_av_g_chip_ident,
1205         .log_status = cx18_av_log_status,
1206         .init = cx18_av_init,
1207         .reset = cx18_av_reset,
1208         .queryctrl = cx18_av_queryctrl,
1209         .g_ctrl = cx18_av_g_ctrl,
1210         .s_ctrl = cx18_av_s_ctrl,
1211 #ifdef CONFIG_VIDEO_ADV_DEBUG
1212         .g_register = cx18_av_g_register,
1213         .s_register = cx18_av_s_register,
1214 #endif
1215 };
1216
1217 static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
1218         .s_radio = cx18_av_s_radio,
1219         .s_frequency = cx18_av_s_frequency,
1220         .g_tuner = cx18_av_g_tuner,
1221         .s_tuner = cx18_av_s_tuner,
1222         .s_std = cx18_av_s_std,
1223 };
1224
1225 static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
1226         .s_clock_freq = cx18_av_s_clock_freq,
1227         .s_routing = cx18_av_s_audio_routing,
1228 };
1229
1230 static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
1231         .s_routing = cx18_av_s_video_routing,
1232         .decode_vbi_line = cx18_av_decode_vbi_line,
1233         .s_stream = cx18_av_s_stream,
1234         .g_fmt = cx18_av_g_fmt,
1235         .s_fmt = cx18_av_s_fmt,
1236 };
1237
1238 static const struct v4l2_subdev_ops cx18_av_ops = {
1239         .core = &cx18_av_general_ops,
1240         .tuner = &cx18_av_tuner_ops,
1241         .audio = &cx18_av_audio_ops,
1242         .video = &cx18_av_video_ops,
1243 };
1244
1245 int cx18_av_probe(struct cx18 *cx)
1246 {
1247         struct cx18_av_state *state = &cx->av_state;
1248         struct v4l2_subdev *sd;
1249
1250         state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
1251         state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)
1252                     ? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;
1253
1254         state->vid_input = CX18_AV_COMPOSITE7;
1255         state->aud_input = CX18_AV_AUDIO8;
1256         state->audclk_freq = 48000;
1257         state->audmode = V4L2_TUNER_MODE_LANG1;
1258         state->slicer_line_delay = 0;
1259         state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
1260
1261         sd = &state->sd;
1262         v4l2_subdev_init(sd, &cx18_av_ops);
1263         v4l2_set_subdevdata(sd, cx);
1264         snprintf(sd->name, sizeof(sd->name),
1265                  "%s %03x", cx->v4l2_dev.name, (state->rev >> 4));
1266         sd->grp_id = CX18_HW_418_AV;
1267         return v4l2_device_register_subdev(&cx->v4l2_dev, sd);
1268 }