Path: blob/master/drivers/media/video/cx18/cx18-av-core.c
17745 views
/*1* cx18 ADEC audio functions2*3* Derived from cx25840-core.c4*5* Copyright (C) 2007 Hans Verkuil <[email protected]>6* Copyright (C) 2008 Andy Walls <[email protected]>7*8* This program is free software; you can redistribute it and/or9* modify it under the terms of the GNU General Public License10* as published by the Free Software Foundation; either version 211* 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 of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA21* 02110-1301, USA.22*/2324#include <media/v4l2-chip-ident.h>25#include "cx18-driver.h"26#include "cx18-io.h"27#include "cx18-cards.h"2829int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)30{31u32 reg = 0xc40000 + (addr & ~3);32u32 mask = 0xff;33int shift = (addr & 3) * 8;34u32 x = cx18_read_reg(cx, reg);3536x = (x & ~(mask << shift)) | ((u32)value << shift);37cx18_write_reg(cx, x, reg);38return 0;39}4041int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)42{43u32 reg = 0xc40000 + (addr & ~3);44int shift = (addr & 3) * 8;45u32 x = cx18_read_reg(cx, reg);4647x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);48cx18_write_reg_expect(cx, x, reg,49((u32)eval << shift), ((u32)mask << shift));50return 0;51}5253int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)54{55cx18_write_reg(cx, value, 0xc40000 + addr);56return 0;57}5859int60cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)61{62cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);63return 0;64}6566int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)67{68cx18_write_reg_noretry(cx, value, 0xc40000 + addr);69return 0;70}7172u8 cx18_av_read(struct cx18 *cx, u16 addr)73{74u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));75int shift = (addr & 3) * 8;7677return (x >> shift) & 0xff;78}7980u32 cx18_av_read4(struct cx18 *cx, u16 addr)81{82return cx18_read_reg(cx, 0xc40000 + addr);83}8485int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,86u8 or_value)87{88return cx18_av_write(cx, addr,89(cx18_av_read(cx, addr) & and_mask) |90or_value);91}9293int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,94u32 or_value)95{96return cx18_av_write4(cx, addr,97(cx18_av_read4(cx, addr) & and_mask) |98or_value);99}100101static void cx18_av_init(struct cx18 *cx)102{103/*104* The crystal freq used in calculations in this driver will be105* 28.636360 MHz.106* Aim to run the PLLs' VCOs near 400 MHz to minimze errors.107*/108109/*110* VDCLK Integer = 0x0f, Post Divider = 0x04111* AIMCLK Integer = 0x0e, Post Divider = 0x16112*/113cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);114115/* VDCLK Fraction = 0x2be2fe */116/* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */117cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);118119/* AIMCLK Fraction = 0x05227ad */120/* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/121cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);122123/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */124cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);125}126127static void cx18_av_initialize(struct v4l2_subdev *sd)128{129struct cx18_av_state *state = to_cx18_av_state(sd);130struct cx18 *cx = v4l2_get_subdevdata(sd);131int default_volume;132u32 v;133134cx18_av_loadfw(cx);135/* Stop 8051 code execution */136cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,1370x03000000, 0x13000000);138139/* initallize the PLL by toggling sleep bit */140v = cx18_av_read4(cx, CXADEC_HOST_REG1);141/* enable sleep mode - register appears to be read only... */142cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);143/* disable sleep mode */144cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,145v & 0xfffe, 0xffff);146147/* initialize DLLs */148v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;149/* disable FLD */150cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);151/* enable FLD */152cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);153154v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;155/* disable FLD */156cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);157/* enable FLD */158cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);159160/* set analog bias currents. Set Vreg to 1.20V. */161cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);162163v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;164/* enable TUNE_FIL_RST */165cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);166/* disable TUNE_FIL_RST */167cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,168v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);169170/* enable 656 output */171cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);172173/* video output drive strength */174cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);175176/* reset video */177cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);178cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);179180/*181* Disable Video Auto-config of the Analog Front End and Video PLL.182*183* Since we only use BT.656 pixel mode, which works for both 525 and 625184* line systems, it's just easier for us to set registers185* 0x102 (CXADEC_CHIP_CTRL), 0x104-0x106 (CXADEC_AFE_CTRL),186* 0x108-0x109 (CXADEC_PLL_CTRL1), and 0x10c-0x10f (CXADEC_VID_PLL_FRAC)187* ourselves, than to run around cleaning up after the auto-config.188*189* (Note: my CX23418 chip doesn't seem to let the ACFG_DIS bit190* get set to 1, but OTOH, it doesn't seem to do AFE and VID PLL191* autoconfig either.)192*193* As a default, also turn off Dual mode for ADC2 and set ADC2 to CH3.194*/195cx18_av_and_or4(cx, CXADEC_CHIP_CTRL, 0xFFFBFFFF, 0x00120000);196197/* Setup the Video and and Aux/Audio PLLs */198cx18_av_init(cx);199200/* set video to auto-detect */201/* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */202/* set the comb notch = 1 */203cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);204205/* Enable wtw_en in CRUSH_CTRL (Set bit 22) */206/* Enable maj_sel in CRUSH_CTRL (Set bit 20) */207cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);208209/* Set VGA_TRACK_RANGE to 0x20 */210cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);211212/*213* Initial VBI setup214* VIP-1.1, 10 bit mode, enable Raw, disable sliced,215* don't clamp raw samples when codes are in use, 1 byte user D-words,216* IDID0 has line #, RP code V bit transition on VBLANK, data during217* blanking intervals218*/219cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);220221/* Set the video input.222The setting in MODE_CTRL gets lost when we do the above setup */223/* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */224/* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */225226/*227* Analog Front End (AFE)228* Default to luma on ch1/ADC1, chroma on ch2/ADC2, SIF on ch3/ADC2229* bypass_ch[1-3] use filter230* droop_comp_ch[1-3] disable231* clamp_en_ch[1-3] disable232* aud_in_sel ADC2233* luma_in_sel ADC1234* chroma_in_sel ADC2235* clamp_sel_ch[2-3] midcode236* clamp_sel_ch1 video decoder237* vga_sel_ch3 audio decoder238* vga_sel_ch[1-2] video decoder239* half_bw_ch[1-3] disable240* +12db_ch[1-3] disable241*/242cx18_av_and_or4(cx, CXADEC_AFE_CTRL, 0xFF000000, 0x00005D00);243244/* if(dwEnable && dw3DCombAvailable) { */245/* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */246/* } else { */247/* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */248/* } */249cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);250default_volume = cx18_av_read(cx, 0x8d4);251/*252* Enforce the legacy volume scale mapping limits to avoid253* -ERANGE errors when initializing the volume control254*/255if (default_volume > 228) {256/* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */257default_volume = 228;258cx18_av_write(cx, 0x8d4, 228);259} else if (default_volume < 20) {260/* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */261default_volume = 20;262cx18_av_write(cx, 0x8d4, 20);263}264default_volume = (((228 - default_volume) >> 1) + 23) << 9;265state->volume->cur.val = state->volume->default_value = default_volume;266v4l2_ctrl_handler_setup(&state->hdl);267}268269static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)270{271cx18_av_initialize(sd);272return 0;273}274275static int cx18_av_load_fw(struct v4l2_subdev *sd)276{277struct cx18_av_state *state = to_cx18_av_state(sd);278279if (!state->is_initialized) {280/* initialize on first use */281state->is_initialized = 1;282cx18_av_initialize(sd);283}284return 0;285}286287void cx18_av_std_setup(struct cx18 *cx)288{289struct cx18_av_state *state = &cx->av_state;290struct v4l2_subdev *sd = &state->sd;291v4l2_std_id std = state->std;292293/*294* Video ADC crystal clock to pixel clock SRC decimation ratio295* 28.636360 MHz/13.5 Mpps * 256 = 0x21f.07b296*/297const int src_decimation = 0x21f;298299int hblank, hactive, burst, vblank, vactive, sc;300int vblank656;301int luma_lpf, uv_lpf, comb;302u32 pll_int, pll_frac, pll_post;303304/* datasheet startup, step 8d */305if (std & ~V4L2_STD_NTSC)306cx18_av_write(cx, 0x49f, 0x11);307else308cx18_av_write(cx, 0x49f, 0x14);309310/*311* Note: At the end of a field, there are 3 sets of half line duration312* (double horizontal rate) pulses:313*314* 5 (625) or 6 (525) half-lines to blank for the vertical retrace315* 5 (625) or 6 (525) vertical sync pulses of half line duration316* 5 (625) or 6 (525) half-lines of equalization pulses317*/318if (std & V4L2_STD_625_50) {319/*320* The following relationships of half line counts should hold:321* 625 = vblank656 + vactive322* 10 = vblank656 - vblank = vsync pulses + equalization pulses323*324* vblank656: half lines after line 625/mid-313 of blanked video325* vblank: half lines, after line 5/317, of blanked video326* vactive: half lines of active video +327* 5 half lines after the end of active video328*329* As far as I can tell:330* vblank656 starts counting from the falling edge of the first331* vsync pulse (start of line 1 or mid-313)332* vblank starts counting from the after the 5 vsync pulses and333* 5 or 4 equalization pulses (start of line 6 or 318)334*335* For 625 line systems the driver will extract VBI information336* from lines 6-23 and lines 318-335 (but the slicer can only337* handle 17 lines, not the 18 in the vblank region).338* In addition, we need vblank656 and vblank to be one whole339* line longer, to cover line 24 and 336, so the SAV/EAV RP340* codes get generated such that the encoder can actually341* extract line 23 & 335 (WSS). We'll lose 1 line in each field342* at the top of the screen.343*344* It appears the 5 half lines that happen after active345* video must be included in vactive (579 instead of 574),346* otherwise the colors get badly displayed in various regions347* of the screen. I guess the chroma comb filter gets confused348* without them (at least when a PVR-350 is the PAL source).349*/350vblank656 = 48; /* lines 1 - 24 & 313 - 336 */351vblank = 38; /* lines 6 - 24 & 318 - 336 */352vactive = 579; /* lines 24 - 313 & 337 - 626 */353354/*355* For a 13.5 Mpps clock and 15,625 Hz line rate, a line is356* is 864 pixels = 720 active + 144 blanking. ITU-R BT.601357* specifies 12 luma clock periods or ~ 0.9 * 13.5 Mpps after358* the end of active video to start a horizontal line, so that359* leaves 132 pixels of hblank to ignore.360*/361hblank = 132;362hactive = 720;363364/*365* Burst gate delay (for 625 line systems)366* Hsync leading edge to color burst rise = 5.6 us367* Color burst width = 2.25 us368* Gate width = 4 pixel clocks369* (5.6 us + 2.25/2 us) * 13.5 Mpps + 4/2 clocks = 92.79 clocks370*/371burst = 93;372luma_lpf = 2;373if (std & V4L2_STD_PAL) {374uv_lpf = 1;375comb = 0x20;376/* sc = 4433618.75 * src_decimation/28636360 * 2^13 */377sc = 688700;378} else if (std == V4L2_STD_PAL_Nc) {379uv_lpf = 1;380comb = 0x20;381/* sc = 3582056.25 * src_decimation/28636360 * 2^13 */382sc = 556422;383} else { /* SECAM */384uv_lpf = 0;385comb = 0;386/* (fr + fb)/2 = (4406260 + 4250000)/2 = 4328130 */387/* sc = 4328130 * src_decimation/28636360 * 2^13 */388sc = 672314;389}390} else {391/*392* The following relationships of half line counts should hold:393* 525 = prevsync + vblank656 + vactive394* 12 = vblank656 - vblank = vsync pulses + equalization pulses395*396* prevsync: 6 half-lines before the vsync pulses397* vblank656: half lines, after line 3/mid-266, of blanked video398* vblank: half lines, after line 9/272, of blanked video399* vactive: half lines of active video400*401* As far as I can tell:402* vblank656 starts counting from the falling edge of the first403* vsync pulse (start of line 4 or mid-266)404* vblank starts counting from the after the 6 vsync pulses and405* 6 or 5 equalization pulses (start of line 10 or 272)406*407* For 525 line systems the driver will extract VBI information408* from lines 10-21 and lines 273-284.409*/410vblank656 = 38; /* lines 4 - 22 & 266 - 284 */411vblank = 26; /* lines 10 - 22 & 272 - 284 */412vactive = 481; /* lines 23 - 263 & 285 - 525 */413414/*415* For a 13.5 Mpps clock and 15,734.26 Hz line rate, a line is416* is 858 pixels = 720 active + 138 blanking. The Hsync leading417* edge should happen 1.2 us * 13.5 Mpps ~= 16 pixels after the418* end of active video, leaving 122 pixels of hblank to ignore419* before active video starts.420*/421hactive = 720;422hblank = 122;423luma_lpf = 1;424uv_lpf = 1;425426/*427* Burst gate delay (for 525 line systems)428* Hsync leading edge to color burst rise = 5.3 us429* Color burst width = 2.5 us430* Gate width = 4 pixel clocks431* (5.3 us + 2.5/2 us) * 13.5 Mpps + 4/2 clocks = 90.425 clocks432*/433if (std == V4L2_STD_PAL_60) {434burst = 90;435luma_lpf = 2;436comb = 0x20;437/* sc = 4433618.75 * src_decimation/28636360 * 2^13 */438sc = 688700;439} else if (std == V4L2_STD_PAL_M) {440/* The 97 needs to be verified against PAL-M timings */441burst = 97;442comb = 0x20;443/* sc = 3575611.49 * src_decimation/28636360 * 2^13 */444sc = 555421;445} else {446burst = 90;447comb = 0x66;448/* sc = 3579545.45.. * src_decimation/28636360 * 2^13 */449sc = 556032;450}451}452453/* DEBUG: Displays configured PLL frequency */454pll_int = cx18_av_read(cx, 0x108);455pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;456pll_post = cx18_av_read(cx, 0x109);457CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",458pll_int, pll_frac, pll_post);459460if (pll_post) {461int fsc, pll;462u64 tmp;463464pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;465pll /= pll_post;466CX18_DEBUG_INFO_DEV(sd, "Video PLL = %d.%06d MHz\n",467pll / 1000000, pll % 1000000);468CX18_DEBUG_INFO_DEV(sd, "Pixel rate = %d.%06d Mpixel/sec\n",469pll / 8000000, (pll / 8) % 1000000);470471CX18_DEBUG_INFO_DEV(sd, "ADC XTAL/pixel clock decimation ratio "472"= %d.%03d\n", src_decimation / 256,473((src_decimation % 256) * 1000) / 256);474475tmp = 28636360 * (u64) sc;476do_div(tmp, src_decimation);477fsc = tmp >> 13;478CX18_DEBUG_INFO_DEV(sd,479"Chroma sub-carrier initial freq = %d.%06d "480"MHz\n", fsc / 1000000, fsc % 1000000);481482CX18_DEBUG_INFO_DEV(sd, "hblank %i, hactive %i, vblank %i, "483"vactive %i, vblank656 %i, src_dec %i, "484"burst 0x%02x, luma_lpf %i, uv_lpf %i, "485"comb 0x%02x, sc 0x%06x\n",486hblank, hactive, vblank, vactive, vblank656,487src_decimation, burst, luma_lpf, uv_lpf,488comb, sc);489}490491/* Sets horizontal blanking delay and active lines */492cx18_av_write(cx, 0x470, hblank);493cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |494(hactive << 4)));495cx18_av_write(cx, 0x472, hactive >> 4);496497/* Sets burst gate delay */498cx18_av_write(cx, 0x473, burst);499500/* Sets vertical blanking delay and active duration */501cx18_av_write(cx, 0x474, vblank);502cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |503(vactive << 4)));504cx18_av_write(cx, 0x476, vactive >> 4);505cx18_av_write(cx, 0x477, vblank656);506507/* Sets src decimation rate */508cx18_av_write(cx, 0x478, 0xff & src_decimation);509cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));510511/* Sets Luma and UV Low pass filters */512cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));513514/* Enables comb filters */515cx18_av_write(cx, 0x47b, comb);516517/* Sets SC Step*/518cx18_av_write(cx, 0x47c, sc);519cx18_av_write(cx, 0x47d, 0xff & sc >> 8);520cx18_av_write(cx, 0x47e, 0xff & sc >> 16);521522if (std & V4L2_STD_625_50) {523state->slicer_line_delay = 1;524state->slicer_line_offset = (6 + state->slicer_line_delay - 2);525} else {526state->slicer_line_delay = 0;527state->slicer_line_offset = (10 + state->slicer_line_delay - 2);528}529cx18_av_write(cx, 0x47f, state->slicer_line_delay);530}531532static void input_change(struct cx18 *cx)533{534struct cx18_av_state *state = &cx->av_state;535v4l2_std_id std = state->std;536u8 v;537538/* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */539cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);540cx18_av_and_or(cx, 0x401, ~0x60, 0);541cx18_av_and_or(cx, 0x401, ~0x60, 0x60);542543if (std & V4L2_STD_525_60) {544if (std == V4L2_STD_NTSC_M_JP) {545/* Japan uses EIAJ audio standard */546cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);547cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);548} else if (std == V4L2_STD_NTSC_M_KR) {549/* South Korea uses A2 audio standard */550cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);551cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);552} else {553/* Others use the BTSC audio standard */554cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);555cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);556}557} else if (std & V4L2_STD_PAL) {558/* Follow tuner change procedure for PAL */559cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);560cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);561} else if (std & V4L2_STD_SECAM) {562/* Select autodetect for SECAM */563cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);564cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);565}566567v = cx18_av_read(cx, 0x803);568if (v & 0x10) {569/* restart audio decoder microcontroller */570v &= ~0x10;571cx18_av_write_expect(cx, 0x803, v, v, 0x1f);572v |= 0x10;573cx18_av_write_expect(cx, 0x803, v, v, 0x1f);574}575}576577static int cx18_av_s_frequency(struct v4l2_subdev *sd,578struct v4l2_frequency *freq)579{580struct cx18 *cx = v4l2_get_subdevdata(sd);581input_change(cx);582return 0;583}584585static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,586enum cx18_av_audio_input aud_input)587{588struct cx18_av_state *state = &cx->av_state;589struct v4l2_subdev *sd = &state->sd;590591enum analog_signal_type {592NONE, CVBS, Y, C, SIF, Pb, Pr593} ch[3] = {NONE, NONE, NONE};594595u8 afe_mux_cfg;596u8 adc2_cfg;597u8 input_mode;598u32 afe_cfg;599int i;600601CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",602vid_input, aud_input);603604if (vid_input >= CX18_AV_COMPOSITE1 &&605vid_input <= CX18_AV_COMPOSITE8) {606afe_mux_cfg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);607ch[0] = CVBS;608input_mode = 0x0;609} else if (vid_input >= CX18_AV_COMPONENT_LUMA1) {610int luma = vid_input & 0xf000;611int r_chroma = vid_input & 0xf0000;612int b_chroma = vid_input & 0xf00000;613614if ((vid_input & ~0xfff000) ||615luma < CX18_AV_COMPONENT_LUMA1 ||616luma > CX18_AV_COMPONENT_LUMA8 ||617r_chroma < CX18_AV_COMPONENT_R_CHROMA4 ||618r_chroma > CX18_AV_COMPONENT_R_CHROMA6 ||619b_chroma < CX18_AV_COMPONENT_B_CHROMA7 ||620b_chroma > CX18_AV_COMPONENT_B_CHROMA8) {621CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",622vid_input);623return -EINVAL;624}625afe_mux_cfg = (luma - CX18_AV_COMPONENT_LUMA1) >> 12;626ch[0] = Y;627afe_mux_cfg |= (r_chroma - CX18_AV_COMPONENT_R_CHROMA4) >> 12;628ch[1] = Pr;629afe_mux_cfg |= (b_chroma - CX18_AV_COMPONENT_B_CHROMA7) >> 14;630ch[2] = Pb;631input_mode = 0x6;632} else {633int luma = vid_input & 0xf0;634int chroma = vid_input & 0xf00;635636if ((vid_input & ~0xff0) ||637luma < CX18_AV_SVIDEO_LUMA1 ||638luma > CX18_AV_SVIDEO_LUMA8 ||639chroma < CX18_AV_SVIDEO_CHROMA4 ||640chroma > CX18_AV_SVIDEO_CHROMA8) {641CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",642vid_input);643return -EINVAL;644}645afe_mux_cfg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);646ch[0] = Y;647if (chroma >= CX18_AV_SVIDEO_CHROMA7) {648afe_mux_cfg &= 0x3f;649afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;650ch[2] = C;651} else {652afe_mux_cfg &= 0xcf;653afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;654ch[1] = C;655}656input_mode = 0x2;657}658659switch (aud_input) {660case CX18_AV_AUDIO_SERIAL1:661case CX18_AV_AUDIO_SERIAL2:662/* do nothing, use serial audio input */663break;664case CX18_AV_AUDIO4:665afe_mux_cfg &= ~0x30;666ch[1] = SIF;667break;668case CX18_AV_AUDIO5:669afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x10;670ch[1] = SIF;671break;672case CX18_AV_AUDIO6:673afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x20;674ch[1] = SIF;675break;676case CX18_AV_AUDIO7:677afe_mux_cfg &= ~0xc0;678ch[2] = SIF;679break;680case CX18_AV_AUDIO8:681afe_mux_cfg = (afe_mux_cfg & ~0xc0) | 0x40;682ch[2] = SIF;683break;684685default:686CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",687aud_input);688return -EINVAL;689}690691/* Set up analog front end multiplexers */692cx18_av_write_expect(cx, 0x103, afe_mux_cfg, afe_mux_cfg, 0xf7);693/* Set INPUT_MODE to Composite, S-Video, or Component */694cx18_av_and_or(cx, 0x401, ~0x6, input_mode);695696/* Set CH_SEL_ADC2 to 1 if input comes from CH3 */697adc2_cfg = cx18_av_read(cx, 0x102);698if (ch[2] == NONE)699adc2_cfg &= ~0x2; /* No sig on CH3, set ADC2 to CH2 for input */700else701adc2_cfg |= 0x2; /* Signal on CH3, set ADC2 to CH3 for input */702703/* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */704if (ch[1] != NONE && ch[2] != NONE)705adc2_cfg |= 0x4; /* Set dual mode */706else707adc2_cfg &= ~0x4; /* Clear dual mode */708cx18_av_write_expect(cx, 0x102, adc2_cfg, adc2_cfg, 0x17);709710/* Configure the analog front end */711afe_cfg = cx18_av_read4(cx, CXADEC_AFE_CTRL);712afe_cfg &= 0xff000000;713afe_cfg |= 0x00005000; /* CHROMA_IN, AUD_IN: ADC2; LUMA_IN: ADC1 */714if (ch[1] != NONE && ch[2] != NONE)715afe_cfg |= 0x00000030; /* half_bw_ch[2-3] since in dual mode */716717for (i = 0; i < 3; i++) {718switch (ch[i]) {719default:720case NONE:721/* CLAMP_SEL = Fixed to midcode clamp level */722afe_cfg |= (0x00000200 << i);723break;724case CVBS:725case Y:726if (i > 0)727afe_cfg |= 0x00002000; /* LUMA_IN_SEL: ADC2 */728break;729case C:730case Pb:731case Pr:732/* CLAMP_SEL = Fixed to midcode clamp level */733afe_cfg |= (0x00000200 << i);734if (i == 0 && ch[i] == C)735afe_cfg &= ~0x00001000; /* CHROMA_IN_SEL ADC1 */736break;737case SIF:738/*739* VGA_GAIN_SEL = Audio Decoder740* CLAMP_SEL = Fixed to midcode clamp level741*/742afe_cfg |= (0x00000240 << i);743if (i == 0)744afe_cfg &= ~0x00004000; /* AUD_IN_SEL ADC1 */745break;746}747}748749cx18_av_write4(cx, CXADEC_AFE_CTRL, afe_cfg);750751state->vid_input = vid_input;752state->aud_input = aud_input;753cx18_av_audio_set_path(cx);754input_change(cx);755return 0;756}757758static int cx18_av_s_video_routing(struct v4l2_subdev *sd,759u32 input, u32 output, u32 config)760{761struct cx18_av_state *state = to_cx18_av_state(sd);762struct cx18 *cx = v4l2_get_subdevdata(sd);763return set_input(cx, input, state->aud_input);764}765766static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,767u32 input, u32 output, u32 config)768{769struct cx18_av_state *state = to_cx18_av_state(sd);770struct cx18 *cx = v4l2_get_subdevdata(sd);771return set_input(cx, state->vid_input, input);772}773774static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)775{776struct cx18_av_state *state = to_cx18_av_state(sd);777struct cx18 *cx = v4l2_get_subdevdata(sd);778u8 vpres;779u8 mode;780int val = 0;781782if (state->radio)783return 0;784785vpres = cx18_av_read(cx, 0x40e) & 0x20;786vt->signal = vpres ? 0xffff : 0x0;787788vt->capability |=789V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |790V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;791792mode = cx18_av_read(cx, 0x804);793794/* get rxsubchans and audmode */795if ((mode & 0xf) == 1)796val |= V4L2_TUNER_SUB_STEREO;797else798val |= V4L2_TUNER_SUB_MONO;799800if (mode == 2 || mode == 4)801val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;802803if (mode & 0x10)804val |= V4L2_TUNER_SUB_SAP;805806vt->rxsubchans = val;807vt->audmode = state->audmode;808return 0;809}810811static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)812{813struct cx18_av_state *state = to_cx18_av_state(sd);814struct cx18 *cx = v4l2_get_subdevdata(sd);815u8 v;816817if (state->radio)818return 0;819820v = cx18_av_read(cx, 0x809);821v &= ~0xf;822823switch (vt->audmode) {824case V4L2_TUNER_MODE_MONO:825/* mono -> mono826stereo -> mono827bilingual -> lang1 */828break;829case V4L2_TUNER_MODE_STEREO:830case V4L2_TUNER_MODE_LANG1:831/* mono -> mono832stereo -> stereo833bilingual -> lang1 */834v |= 0x4;835break;836case V4L2_TUNER_MODE_LANG1_LANG2:837/* mono -> mono838stereo -> stereo839bilingual -> lang1/lang2 */840v |= 0x7;841break;842case V4L2_TUNER_MODE_LANG2:843/* mono -> mono844stereo -> stereo845bilingual -> lang2 */846v |= 0x1;847break;848default:849return -EINVAL;850}851cx18_av_write_expect(cx, 0x809, v, v, 0xff);852state->audmode = vt->audmode;853return 0;854}855856static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)857{858struct cx18_av_state *state = to_cx18_av_state(sd);859struct cx18 *cx = v4l2_get_subdevdata(sd);860861u8 fmt = 0; /* zero is autodetect */862u8 pal_m = 0;863864if (state->radio == 0 && state->std == norm)865return 0;866867state->radio = 0;868state->std = norm;869870/* First tests should be against specific std */871if (state->std == V4L2_STD_NTSC_M_JP) {872fmt = 0x2;873} else if (state->std == V4L2_STD_NTSC_443) {874fmt = 0x3;875} else if (state->std == V4L2_STD_PAL_M) {876pal_m = 1;877fmt = 0x5;878} else if (state->std == V4L2_STD_PAL_N) {879fmt = 0x6;880} else if (state->std == V4L2_STD_PAL_Nc) {881fmt = 0x7;882} else if (state->std == V4L2_STD_PAL_60) {883fmt = 0x8;884} else {885/* Then, test against generic ones */886if (state->std & V4L2_STD_NTSC)887fmt = 0x1;888else if (state->std & V4L2_STD_PAL)889fmt = 0x4;890else if (state->std & V4L2_STD_SECAM)891fmt = 0xc;892}893894CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);895896/* Follow step 9 of section 3.16 in the cx18_av datasheet.897Without this PAL may display a vertical ghosting effect.898This happens for example with the Yuan MPC622. */899if (fmt >= 4 && fmt < 8) {900/* Set format to NTSC-M */901cx18_av_and_or(cx, 0x400, ~0xf, 1);902/* Turn off LCOMB */903cx18_av_and_or(cx, 0x47b, ~6, 0);904}905cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);906cx18_av_and_or(cx, 0x403, ~0x3, pal_m);907cx18_av_std_setup(cx);908input_change(cx);909return 0;910}911912static int cx18_av_s_radio(struct v4l2_subdev *sd)913{914struct cx18_av_state *state = to_cx18_av_state(sd);915state->radio = 1;916return 0;917}918919static int cx18_av_s_ctrl(struct v4l2_ctrl *ctrl)920{921struct v4l2_subdev *sd = to_sd(ctrl);922struct cx18 *cx = v4l2_get_subdevdata(sd);923924switch (ctrl->id) {925case V4L2_CID_BRIGHTNESS:926cx18_av_write(cx, 0x414, ctrl->val - 128);927break;928929case V4L2_CID_CONTRAST:930cx18_av_write(cx, 0x415, ctrl->val << 1);931break;932933case V4L2_CID_SATURATION:934cx18_av_write(cx, 0x420, ctrl->val << 1);935cx18_av_write(cx, 0x421, ctrl->val << 1);936break;937938case V4L2_CID_HUE:939cx18_av_write(cx, 0x422, ctrl->val);940break;941942default:943return -EINVAL;944}945return 0;946}947948static int cx18_av_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)949{950struct cx18_av_state *state = to_cx18_av_state(sd);951struct cx18 *cx = v4l2_get_subdevdata(sd);952int HSC, VSC, Vsrc, Hsrc, filter, Vlines;953int is_50Hz = !(state->std & V4L2_STD_525_60);954955if (fmt->code != V4L2_MBUS_FMT_FIXED)956return -EINVAL;957958fmt->field = V4L2_FIELD_INTERLACED;959fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;960961Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;962Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;963964Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;965Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;966967/*968* This adjustment reflects the excess of vactive, set in969* cx18_av_std_setup(), above standard values:970*971* 480 + 1 for 60 Hz systems972* 576 + 3 for 50 Hz systems973*/974Vlines = fmt->height + (is_50Hz ? 3 : 1);975976/*977* Invalid height and width scaling requests are:978* 1. width less than 1/16 of the source width979* 2. width greater than the source width980* 3. height less than 1/8 of the source height981* 4. height greater than the source height982*/983if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||984(Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {985CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",986fmt->width, fmt->height);987return -ERANGE;988}989990HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);991VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));992VSC &= 0x1fff;993994if (fmt->width >= 385)995filter = 0;996else if (fmt->width > 192)997filter = 1;998else if (fmt->width > 96)999filter = 2;1000else1001filter = 3;10021003CX18_DEBUG_INFO_DEV(sd,1004"decoder set size %dx%d -> scale %ux%u\n",1005fmt->width, fmt->height, HSC, VSC);10061007/* HSCALE=HSC */1008cx18_av_write(cx, 0x418, HSC & 0xff);1009cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);1010cx18_av_write(cx, 0x41a, HSC >> 16);1011/* VSCALE=VSC */1012cx18_av_write(cx, 0x41c, VSC & 0xff);1013cx18_av_write(cx, 0x41d, VSC >> 8);1014/* VS_INTRLACE=1 VFILT=filter */1015cx18_av_write(cx, 0x41e, 0x8 | filter);1016return 0;1017}10181019static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)1020{1021struct cx18 *cx = v4l2_get_subdevdata(sd);10221023CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");1024if (enable) {1025cx18_av_write(cx, 0x115, 0x8c);1026cx18_av_write(cx, 0x116, 0x07);1027} else {1028cx18_av_write(cx, 0x115, 0x00);1029cx18_av_write(cx, 0x116, 0x00);1030}1031return 0;1032}10331034static void log_video_status(struct cx18 *cx)1035{1036static const char *const fmt_strs[] = {1037"0x0",1038"NTSC-M", "NTSC-J", "NTSC-4.43",1039"PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",1040"0x9", "0xA", "0xB",1041"SECAM",1042"0xD", "0xE", "0xF"1043};10441045struct cx18_av_state *state = &cx->av_state;1046struct v4l2_subdev *sd = &state->sd;1047u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;1048u8 gen_stat1 = cx18_av_read(cx, 0x40d);1049u8 gen_stat2 = cx18_av_read(cx, 0x40e);1050int vid_input = state->vid_input;10511052CX18_INFO_DEV(sd, "Video signal: %spresent\n",1053(gen_stat2 & 0x20) ? "" : "not ");1054CX18_INFO_DEV(sd, "Detected format: %s\n",1055fmt_strs[gen_stat1 & 0xf]);10561057CX18_INFO_DEV(sd, "Specified standard: %s\n",1058vidfmt_sel ? fmt_strs[vidfmt_sel]1059: "automatic detection");10601061if (vid_input >= CX18_AV_COMPOSITE1 &&1062vid_input <= CX18_AV_COMPOSITE8) {1063CX18_INFO_DEV(sd, "Specified video input: Composite %d\n",1064vid_input - CX18_AV_COMPOSITE1 + 1);1065} else {1066CX18_INFO_DEV(sd, "Specified video input: "1067"S-Video (Luma In%d, Chroma In%d)\n",1068(vid_input & 0xf0) >> 4,1069(vid_input & 0xf00) >> 8);1070}10711072CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",1073state->audclk_freq);1074}10751076static void log_audio_status(struct cx18 *cx)1077{1078struct cx18_av_state *state = &cx->av_state;1079struct v4l2_subdev *sd = &state->sd;1080u8 download_ctl = cx18_av_read(cx, 0x803);1081u8 mod_det_stat0 = cx18_av_read(cx, 0x804);1082u8 mod_det_stat1 = cx18_av_read(cx, 0x805);1083u8 audio_config = cx18_av_read(cx, 0x808);1084u8 pref_mode = cx18_av_read(cx, 0x809);1085u8 afc0 = cx18_av_read(cx, 0x80b);1086u8 mute_ctl = cx18_av_read(cx, 0x8d3);1087int aud_input = state->aud_input;1088char *p;10891090switch (mod_det_stat0) {1091case 0x00: p = "mono"; break;1092case 0x01: p = "stereo"; break;1093case 0x02: p = "dual"; break;1094case 0x04: p = "tri"; break;1095case 0x10: p = "mono with SAP"; break;1096case 0x11: p = "stereo with SAP"; break;1097case 0x12: p = "dual with SAP"; break;1098case 0x14: p = "tri with SAP"; break;1099case 0xfe: p = "forced mode"; break;1100default: p = "not defined"; break;1101}1102CX18_INFO_DEV(sd, "Detected audio mode: %s\n", p);11031104switch (mod_det_stat1) {1105case 0x00: p = "not defined"; break;1106case 0x01: p = "EIAJ"; break;1107case 0x02: p = "A2-M"; break;1108case 0x03: p = "A2-BG"; break;1109case 0x04: p = "A2-DK1"; break;1110case 0x05: p = "A2-DK2"; break;1111case 0x06: p = "A2-DK3"; break;1112case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;1113case 0x08: p = "AM-L"; break;1114case 0x09: p = "NICAM-BG"; break;1115case 0x0a: p = "NICAM-DK"; break;1116case 0x0b: p = "NICAM-I"; break;1117case 0x0c: p = "NICAM-L"; break;1118case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;1119case 0x0e: p = "IF FM Radio"; break;1120case 0x0f: p = "BTSC"; break;1121case 0x10: p = "detected chrominance"; break;1122case 0xfd: p = "unknown audio standard"; break;1123case 0xfe: p = "forced audio standard"; break;1124case 0xff: p = "no detected audio standard"; break;1125default: p = "not defined"; break;1126}1127CX18_INFO_DEV(sd, "Detected audio standard: %s\n", p);1128CX18_INFO_DEV(sd, "Audio muted: %s\n",1129(mute_ctl & 0x2) ? "yes" : "no");1130CX18_INFO_DEV(sd, "Audio microcontroller: %s\n",1131(download_ctl & 0x10) ? "running" : "stopped");11321133switch (audio_config >> 4) {1134case 0x00: p = "undefined"; break;1135case 0x01: p = "BTSC"; break;1136case 0x02: p = "EIAJ"; break;1137case 0x03: p = "A2-M"; break;1138case 0x04: p = "A2-BG"; break;1139case 0x05: p = "A2-DK1"; break;1140case 0x06: p = "A2-DK2"; break;1141case 0x07: p = "A2-DK3"; break;1142case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;1143case 0x09: p = "AM-L"; break;1144case 0x0a: p = "NICAM-BG"; break;1145case 0x0b: p = "NICAM-DK"; break;1146case 0x0c: p = "NICAM-I"; break;1147case 0x0d: p = "NICAM-L"; break;1148case 0x0e: p = "FM radio"; break;1149case 0x0f: p = "automatic detection"; break;1150default: p = "undefined"; break;1151}1152CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);11531154if ((audio_config >> 4) < 0xF) {1155switch (audio_config & 0xF) {1156case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;1157case 0x01: p = "MONO2 (LANGUAGE B)"; break;1158case 0x02: p = "MONO3 (STEREO forced MONO)"; break;1159case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;1160case 0x04: p = "STEREO"; break;1161case 0x05: p = "DUAL1 (AC)"; break;1162case 0x06: p = "DUAL2 (BC)"; break;1163case 0x07: p = "DUAL3 (AB)"; break;1164default: p = "undefined";1165}1166CX18_INFO_DEV(sd, "Configured audio mode: %s\n", p);1167} else {1168switch (audio_config & 0xF) {1169case 0x00: p = "BG"; break;1170case 0x01: p = "DK1"; break;1171case 0x02: p = "DK2"; break;1172case 0x03: p = "DK3"; break;1173case 0x04: p = "I"; break;1174case 0x05: p = "L"; break;1175case 0x06: p = "BTSC"; break;1176case 0x07: p = "EIAJ"; break;1177case 0x08: p = "A2-M"; break;1178case 0x09: p = "FM Radio (4.5 MHz)"; break;1179case 0x0a: p = "FM Radio (5.5 MHz)"; break;1180case 0x0b: p = "S-Video"; break;1181case 0x0f: p = "automatic standard and mode detection"; break;1182default: p = "undefined"; break;1183}1184CX18_INFO_DEV(sd, "Configured audio system: %s\n", p);1185}11861187if (aud_input)1188CX18_INFO_DEV(sd, "Specified audio input: Tuner (In%d)\n",1189aud_input);1190else1191CX18_INFO_DEV(sd, "Specified audio input: External\n");11921193switch (pref_mode & 0xf) {1194case 0: p = "mono/language A"; break;1195case 1: p = "language B"; break;1196case 2: p = "language C"; break;1197case 3: p = "analog fallback"; break;1198case 4: p = "stereo"; break;1199case 5: p = "language AC"; break;1200case 6: p = "language BC"; break;1201case 7: p = "language AB"; break;1202default: p = "undefined"; break;1203}1204CX18_INFO_DEV(sd, "Preferred audio mode: %s\n", p);12051206if ((audio_config & 0xf) == 0xf) {1207switch ((afc0 >> 3) & 0x1) {1208case 0: p = "system DK"; break;1209case 1: p = "system L"; break;1210}1211CX18_INFO_DEV(sd, "Selected 65 MHz format: %s\n", p);12121213switch (afc0 & 0x7) {1214case 0: p = "Chroma"; break;1215case 1: p = "BTSC"; break;1216case 2: p = "EIAJ"; break;1217case 3: p = "A2-M"; break;1218case 4: p = "autodetect"; break;1219default: p = "undefined"; break;1220}1221CX18_INFO_DEV(sd, "Selected 45 MHz format: %s\n", p);1222}1223}12241225static int cx18_av_log_status(struct v4l2_subdev *sd)1226{1227struct cx18 *cx = v4l2_get_subdevdata(sd);1228log_video_status(cx);1229log_audio_status(cx);1230return 0;1231}12321233static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)1234{1235return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;1236}12371238static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,1239struct v4l2_dbg_chip_ident *chip)1240{1241struct cx18_av_state *state = to_cx18_av_state(sd);12421243if (cx18_av_dbg_match(&chip->match)) {1244chip->ident = state->id;1245chip->revision = state->rev;1246}1247return 0;1248}12491250#ifdef CONFIG_VIDEO_ADV_DEBUG1251static int cx18_av_g_register(struct v4l2_subdev *sd,1252struct v4l2_dbg_register *reg)1253{1254struct cx18 *cx = v4l2_get_subdevdata(sd);12551256if (!cx18_av_dbg_match(®->match))1257return -EINVAL;1258if ((reg->reg & 0x3) != 0)1259return -EINVAL;1260if (!capable(CAP_SYS_ADMIN))1261return -EPERM;1262reg->size = 4;1263reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);1264return 0;1265}12661267static int cx18_av_s_register(struct v4l2_subdev *sd,1268struct v4l2_dbg_register *reg)1269{1270struct cx18 *cx = v4l2_get_subdevdata(sd);12711272if (!cx18_av_dbg_match(®->match))1273return -EINVAL;1274if ((reg->reg & 0x3) != 0)1275return -EINVAL;1276if (!capable(CAP_SYS_ADMIN))1277return -EPERM;1278cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);1279return 0;1280}1281#endif12821283static const struct v4l2_ctrl_ops cx18_av_ctrl_ops = {1284.s_ctrl = cx18_av_s_ctrl,1285};12861287static const struct v4l2_subdev_core_ops cx18_av_general_ops = {1288.g_chip_ident = cx18_av_g_chip_ident,1289.log_status = cx18_av_log_status,1290.load_fw = cx18_av_load_fw,1291.reset = cx18_av_reset,1292.g_ctrl = v4l2_subdev_g_ctrl,1293.s_ctrl = v4l2_subdev_s_ctrl,1294.s_ext_ctrls = v4l2_subdev_s_ext_ctrls,1295.try_ext_ctrls = v4l2_subdev_try_ext_ctrls,1296.g_ext_ctrls = v4l2_subdev_g_ext_ctrls,1297.queryctrl = v4l2_subdev_queryctrl,1298.querymenu = v4l2_subdev_querymenu,1299.s_std = cx18_av_s_std,1300#ifdef CONFIG_VIDEO_ADV_DEBUG1301.g_register = cx18_av_g_register,1302.s_register = cx18_av_s_register,1303#endif1304};13051306static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {1307.s_radio = cx18_av_s_radio,1308.s_frequency = cx18_av_s_frequency,1309.g_tuner = cx18_av_g_tuner,1310.s_tuner = cx18_av_s_tuner,1311};13121313static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {1314.s_clock_freq = cx18_av_s_clock_freq,1315.s_routing = cx18_av_s_audio_routing,1316};13171318static const struct v4l2_subdev_video_ops cx18_av_video_ops = {1319.s_routing = cx18_av_s_video_routing,1320.s_stream = cx18_av_s_stream,1321.s_mbus_fmt = cx18_av_s_mbus_fmt,1322};13231324static const struct v4l2_subdev_vbi_ops cx18_av_vbi_ops = {1325.decode_vbi_line = cx18_av_decode_vbi_line,1326.g_sliced_fmt = cx18_av_g_sliced_fmt,1327.s_sliced_fmt = cx18_av_s_sliced_fmt,1328.s_raw_fmt = cx18_av_s_raw_fmt,1329};13301331static const struct v4l2_subdev_ops cx18_av_ops = {1332.core = &cx18_av_general_ops,1333.tuner = &cx18_av_tuner_ops,1334.audio = &cx18_av_audio_ops,1335.video = &cx18_av_video_ops,1336.vbi = &cx18_av_vbi_ops,1337};13381339int cx18_av_probe(struct cx18 *cx)1340{1341struct cx18_av_state *state = &cx->av_state;1342struct v4l2_subdev *sd;1343int err;13441345state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;1346state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)1347? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;13481349state->vid_input = CX18_AV_COMPOSITE7;1350state->aud_input = CX18_AV_AUDIO8;1351state->audclk_freq = 48000;1352state->audmode = V4L2_TUNER_MODE_LANG1;1353state->slicer_line_delay = 0;1354state->slicer_line_offset = (10 + state->slicer_line_delay - 2);13551356sd = &state->sd;1357v4l2_subdev_init(sd, &cx18_av_ops);1358v4l2_set_subdevdata(sd, cx);1359snprintf(sd->name, sizeof(sd->name),1360"%s %03x", cx->v4l2_dev.name, (state->rev >> 4));1361sd->grp_id = CX18_HW_418_AV;1362v4l2_ctrl_handler_init(&state->hdl, 9);1363v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,1364V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);1365v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,1366V4L2_CID_CONTRAST, 0, 127, 1, 64);1367v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,1368V4L2_CID_SATURATION, 0, 127, 1, 64);1369v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,1370V4L2_CID_HUE, -128, 127, 1, 0);13711372state->volume = v4l2_ctrl_new_std(&state->hdl,1373&cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,13740, 65535, 65535 / 100, 0);1375v4l2_ctrl_new_std(&state->hdl,1376&cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,13770, 1, 1, 0);1378v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,1379V4L2_CID_AUDIO_BALANCE,13800, 65535, 65535 / 100, 32768);1381v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,1382V4L2_CID_AUDIO_BASS,13830, 65535, 65535 / 100, 32768);1384v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,1385V4L2_CID_AUDIO_TREBLE,13860, 65535, 65535 / 100, 32768);1387sd->ctrl_handler = &state->hdl;1388if (state->hdl.error) {1389int err = state->hdl.error;13901391v4l2_ctrl_handler_free(&state->hdl);1392return err;1393}1394err = v4l2_device_register_subdev(&cx->v4l2_dev, sd);1395if (err)1396v4l2_ctrl_handler_free(&state->hdl);1397else1398cx18_av_init(cx);1399return err;1400}140114021403