Path: blob/master/drivers/media/video/cx25840/cx25840-core.c
17745 views
/* cx25840 - Conexant CX25840 audio/video decoder driver1*2* Copyright (C) 2004 Ulf Eklund3*4* Based on the saa7115 driver and on the first version of Chris Kennedy's5* cx25840 driver.6*7* Changes by Tyler Trafford <[email protected]>8* - cleanup/rewrite for V4L2 API (2005)9*10* VBI support by Hans Verkuil <[email protected]>.11*12* NTSC sliced VBI support by Christopher Neufeld <[email protected]>13* with additional fixes by Hans Verkuil <[email protected]>.14*15* CX23885 support by Steven Toth <[email protected]>.16*17* CX2388[578] IRQ handling, IO Pin mux configuration and other small fixes are18* Copyright (C) 2010 Andy Walls <[email protected]>19*20* This program is free software; you can redistribute it and/or21* modify it under the terms of the GNU General Public License22* as published by the Free Software Foundation; either version 223* of the License, or (at your option) any later version.24*25* This program is distributed in the hope that it will be useful,26* but WITHOUT ANY WARRANTY; without even the implied warranty of27* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the28* GNU General Public License for more details.29*30* You should have received a copy of the GNU General Public License31* along with this program; if not, write to the Free Software32* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.33*/343536#include <linux/kernel.h>37#include <linux/module.h>38#include <linux/slab.h>39#include <linux/videodev2.h>40#include <linux/i2c.h>41#include <linux/delay.h>42#include <media/v4l2-common.h>43#include <media/v4l2-chip-ident.h>44#include <media/cx25840.h>4546#include "cx25840-core.h"4748MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");49MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");50MODULE_LICENSE("GPL");5152#define CX25840_VID_INT_STAT_REG 0x41053#define CX25840_VID_INT_STAT_BITS 0x0000ffff54#define CX25840_VID_INT_MASK_BITS 0xffff000055#define CX25840_VID_INT_MASK_SHFT 1656#define CX25840_VID_INT_MASK_REG 0x4125758#define CX23885_AUD_MC_INT_MASK_REG 0x80c59#define CX23885_AUD_MC_INT_STAT_BITS 0xffff000060#define CX23885_AUD_MC_INT_CTRL_BITS 0x0000ffff61#define CX23885_AUD_MC_INT_STAT_SHFT 166263#define CX25840_AUD_INT_CTRL_REG 0x81264#define CX25840_AUD_INT_STAT_REG 0x8136566#define CX23885_PIN_CTRL_IRQ_REG 0x12367#define CX23885_PIN_CTRL_IRQ_IR_STAT 0x4068#define CX23885_PIN_CTRL_IRQ_AUD_STAT 0x2069#define CX23885_PIN_CTRL_IRQ_VID_STAT 0x107071#define CX25840_IR_STATS_REG 0x21072#define CX25840_IR_IRQEN_REG 0x2147374static int cx25840_debug;7576module_param_named(debug,cx25840_debug, int, 0644);7778MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");798081/* ----------------------------------------------------------------------- */8283int cx25840_write(struct i2c_client *client, u16 addr, u8 value)84{85u8 buffer[3];86buffer[0] = addr >> 8;87buffer[1] = addr & 0xff;88buffer[2] = value;89return i2c_master_send(client, buffer, 3);90}9192int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)93{94u8 buffer[6];95buffer[0] = addr >> 8;96buffer[1] = addr & 0xff;97buffer[2] = value & 0xff;98buffer[3] = (value >> 8) & 0xff;99buffer[4] = (value >> 16) & 0xff;100buffer[5] = value >> 24;101return i2c_master_send(client, buffer, 6);102}103104u8 cx25840_read(struct i2c_client * client, u16 addr)105{106struct i2c_msg msgs[2];107u8 tx_buf[2], rx_buf[1];108109/* Write register address */110tx_buf[0] = addr >> 8;111tx_buf[1] = addr & 0xff;112msgs[0].addr = client->addr;113msgs[0].flags = 0;114msgs[0].len = 2;115msgs[0].buf = (char *) tx_buf;116117/* Read data from register */118msgs[1].addr = client->addr;119msgs[1].flags = I2C_M_RD;120msgs[1].len = 1;121msgs[1].buf = (char *) rx_buf;122123if (i2c_transfer(client->adapter, msgs, 2) < 2)124return 0;125126return rx_buf[0];127}128129u32 cx25840_read4(struct i2c_client * client, u16 addr)130{131struct i2c_msg msgs[2];132u8 tx_buf[2], rx_buf[4];133134/* Write register address */135tx_buf[0] = addr >> 8;136tx_buf[1] = addr & 0xff;137msgs[0].addr = client->addr;138msgs[0].flags = 0;139msgs[0].len = 2;140msgs[0].buf = (char *) tx_buf;141142/* Read data from registers */143msgs[1].addr = client->addr;144msgs[1].flags = I2C_M_RD;145msgs[1].len = 4;146msgs[1].buf = (char *) rx_buf;147148if (i2c_transfer(client->adapter, msgs, 2) < 2)149return 0;150151return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |152rx_buf[0];153}154155int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,156u8 or_value)157{158return cx25840_write(client, addr,159(cx25840_read(client, addr) & and_mask) |160or_value);161}162163int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,164u32 or_value)165{166return cx25840_write4(client, addr,167(cx25840_read4(client, addr) & and_mask) |168or_value);169}170171/* ----------------------------------------------------------------------- */172173static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,174enum cx25840_audio_input aud_input);175176/* ----------------------------------------------------------------------- */177178static int cx23885_s_io_pin_config(struct v4l2_subdev *sd, size_t n,179struct v4l2_subdev_io_pin_config *p)180{181struct i2c_client *client = v4l2_get_subdevdata(sd);182int i;183u32 pin_ctrl;184u8 gpio_oe, gpio_data, strength;185186pin_ctrl = cx25840_read4(client, 0x120);187gpio_oe = cx25840_read(client, 0x160);188gpio_data = cx25840_read(client, 0x164);189190for (i = 0; i < n; i++) {191strength = p[i].strength;192if (strength > CX25840_PIN_DRIVE_FAST)193strength = CX25840_PIN_DRIVE_FAST;194195switch (p[i].pin) {196case CX23885_PIN_IRQ_N_GPIO16:197if (p[i].function != CX23885_PAD_IRQ_N) {198/* GPIO16 */199pin_ctrl &= ~(0x1 << 25);200} else {201/* IRQ_N */202if (p[i].flags &203(V4L2_SUBDEV_IO_PIN_DISABLE |204V4L2_SUBDEV_IO_PIN_INPUT)) {205pin_ctrl &= ~(0x1 << 25);206} else {207pin_ctrl |= (0x1 << 25);208}209if (p[i].flags &210V4L2_SUBDEV_IO_PIN_ACTIVE_LOW) {211pin_ctrl &= ~(0x1 << 24);212} else {213pin_ctrl |= (0x1 << 24);214}215}216break;217case CX23885_PIN_IR_RX_GPIO19:218if (p[i].function != CX23885_PAD_GPIO19) {219/* IR_RX */220gpio_oe |= (0x1 << 0);221pin_ctrl &= ~(0x3 << 18);222pin_ctrl |= (strength << 18);223} else {224/* GPIO19 */225gpio_oe &= ~(0x1 << 0);226if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {227gpio_data &= ~(0x1 << 0);228gpio_data |= ((p[i].value & 0x1) << 0);229}230pin_ctrl &= ~(0x3 << 12);231pin_ctrl |= (strength << 12);232}233break;234case CX23885_PIN_IR_TX_GPIO20:235if (p[i].function != CX23885_PAD_GPIO20) {236/* IR_TX */237gpio_oe |= (0x1 << 1);238if (p[i].flags & V4L2_SUBDEV_IO_PIN_DISABLE)239pin_ctrl &= ~(0x1 << 10);240else241pin_ctrl |= (0x1 << 10);242pin_ctrl &= ~(0x3 << 18);243pin_ctrl |= (strength << 18);244} else {245/* GPIO20 */246gpio_oe &= ~(0x1 << 1);247if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {248gpio_data &= ~(0x1 << 1);249gpio_data |= ((p[i].value & 0x1) << 1);250}251pin_ctrl &= ~(0x3 << 12);252pin_ctrl |= (strength << 12);253}254break;255case CX23885_PIN_I2S_SDAT_GPIO21:256if (p[i].function != CX23885_PAD_GPIO21) {257/* I2S_SDAT */258/* TODO: Input or Output config */259gpio_oe |= (0x1 << 2);260pin_ctrl &= ~(0x3 << 22);261pin_ctrl |= (strength << 22);262} else {263/* GPIO21 */264gpio_oe &= ~(0x1 << 2);265if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {266gpio_data &= ~(0x1 << 2);267gpio_data |= ((p[i].value & 0x1) << 2);268}269pin_ctrl &= ~(0x3 << 12);270pin_ctrl |= (strength << 12);271}272break;273case CX23885_PIN_I2S_WCLK_GPIO22:274if (p[i].function != CX23885_PAD_GPIO22) {275/* I2S_WCLK */276/* TODO: Input or Output config */277gpio_oe |= (0x1 << 3);278pin_ctrl &= ~(0x3 << 22);279pin_ctrl |= (strength << 22);280} else {281/* GPIO22 */282gpio_oe &= ~(0x1 << 3);283if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {284gpio_data &= ~(0x1 << 3);285gpio_data |= ((p[i].value & 0x1) << 3);286}287pin_ctrl &= ~(0x3 << 12);288pin_ctrl |= (strength << 12);289}290break;291case CX23885_PIN_I2S_BCLK_GPIO23:292if (p[i].function != CX23885_PAD_GPIO23) {293/* I2S_BCLK */294/* TODO: Input or Output config */295gpio_oe |= (0x1 << 4);296pin_ctrl &= ~(0x3 << 22);297pin_ctrl |= (strength << 22);298} else {299/* GPIO23 */300gpio_oe &= ~(0x1 << 4);301if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {302gpio_data &= ~(0x1 << 4);303gpio_data |= ((p[i].value & 0x1) << 4);304}305pin_ctrl &= ~(0x3 << 12);306pin_ctrl |= (strength << 12);307}308break;309}310}311312cx25840_write(client, 0x164, gpio_data);313cx25840_write(client, 0x160, gpio_oe);314cx25840_write4(client, 0x120, pin_ctrl);315return 0;316}317318static int common_s_io_pin_config(struct v4l2_subdev *sd, size_t n,319struct v4l2_subdev_io_pin_config *pincfg)320{321struct cx25840_state *state = to_state(sd);322323if (is_cx2388x(state))324return cx23885_s_io_pin_config(sd, n, pincfg);325return 0;326}327328/* ----------------------------------------------------------------------- */329330static void init_dll1(struct i2c_client *client)331{332/* This is the Hauppauge sequence used to333* initialize the Delay Lock Loop 1 (ADC DLL). */334cx25840_write(client, 0x159, 0x23);335cx25840_write(client, 0x15a, 0x87);336cx25840_write(client, 0x15b, 0x06);337udelay(10);338cx25840_write(client, 0x159, 0xe1);339udelay(10);340cx25840_write(client, 0x15a, 0x86);341cx25840_write(client, 0x159, 0xe0);342cx25840_write(client, 0x159, 0xe1);343cx25840_write(client, 0x15b, 0x10);344}345346static void init_dll2(struct i2c_client *client)347{348/* This is the Hauppauge sequence used to349* initialize the Delay Lock Loop 2 (ADC DLL). */350cx25840_write(client, 0x15d, 0xe3);351cx25840_write(client, 0x15e, 0x86);352cx25840_write(client, 0x15f, 0x06);353udelay(10);354cx25840_write(client, 0x15d, 0xe1);355cx25840_write(client, 0x15d, 0xe0);356cx25840_write(client, 0x15d, 0xe1);357}358359static void cx25836_initialize(struct i2c_client *client)360{361/* reset configuration is described on page 3-77 of the CX25836 datasheet */362/* 2. */363cx25840_and_or(client, 0x000, ~0x01, 0x01);364cx25840_and_or(client, 0x000, ~0x01, 0x00);365/* 3a. */366cx25840_and_or(client, 0x15a, ~0x70, 0x00);367/* 3b. */368cx25840_and_or(client, 0x15b, ~0x1e, 0x06);369/* 3c. */370cx25840_and_or(client, 0x159, ~0x02, 0x02);371/* 3d. */372udelay(10);373/* 3e. */374cx25840_and_or(client, 0x159, ~0x02, 0x00);375/* 3f. */376cx25840_and_or(client, 0x159, ~0xc0, 0xc0);377/* 3g. */378cx25840_and_or(client, 0x159, ~0x01, 0x00);379cx25840_and_or(client, 0x159, ~0x01, 0x01);380/* 3h. */381cx25840_and_or(client, 0x15b, ~0x1e, 0x10);382}383384static void cx25840_work_handler(struct work_struct *work)385{386struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);387cx25840_loadfw(state->c);388wake_up(&state->fw_wait);389}390391static void cx25840_initialize(struct i2c_client *client)392{393DEFINE_WAIT(wait);394struct cx25840_state *state = to_state(i2c_get_clientdata(client));395struct workqueue_struct *q;396397/* datasheet startup in numbered steps, refer to page 3-77 */398/* 2. */399cx25840_and_or(client, 0x803, ~0x10, 0x00);400/* The default of this register should be 4, but I get 0 instead.401* Set this register to 4 manually. */402cx25840_write(client, 0x000, 0x04);403/* 3. */404init_dll1(client);405init_dll2(client);406cx25840_write(client, 0x136, 0x0a);407/* 4. */408cx25840_write(client, 0x13c, 0x01);409cx25840_write(client, 0x13c, 0x00);410/* 5. */411/* Do the firmware load in a work handler to prevent.412Otherwise the kernel is blocked waiting for the413bit-banging i2c interface to finish uploading the414firmware. */415INIT_WORK(&state->fw_work, cx25840_work_handler);416init_waitqueue_head(&state->fw_wait);417q = create_singlethread_workqueue("cx25840_fw");418prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);419queue_work(q, &state->fw_work);420schedule();421finish_wait(&state->fw_wait, &wait);422destroy_workqueue(q);423424/* 6. */425cx25840_write(client, 0x115, 0x8c);426cx25840_write(client, 0x116, 0x07);427cx25840_write(client, 0x118, 0x02);428/* 7. */429cx25840_write(client, 0x4a5, 0x80);430cx25840_write(client, 0x4a5, 0x00);431cx25840_write(client, 0x402, 0x00);432/* 8. */433cx25840_and_or(client, 0x401, ~0x18, 0);434cx25840_and_or(client, 0x4a2, ~0x10, 0x10);435/* steps 8c and 8d are done in change_input() */436/* 10. */437cx25840_write(client, 0x8d3, 0x1f);438cx25840_write(client, 0x8e3, 0x03);439440cx25840_std_setup(client);441442/* trial and error says these are needed to get audio */443cx25840_write(client, 0x914, 0xa0);444cx25840_write(client, 0x918, 0xa0);445cx25840_write(client, 0x919, 0x01);446447/* stereo preferred */448cx25840_write(client, 0x809, 0x04);449/* AC97 shift */450cx25840_write(client, 0x8cf, 0x0f);451452/* (re)set input */453set_input(client, state->vid_input, state->aud_input);454455/* start microcontroller */456cx25840_and_or(client, 0x803, ~0x10, 0x10);457}458459static void cx23885_initialize(struct i2c_client *client)460{461DEFINE_WAIT(wait);462struct cx25840_state *state = to_state(i2c_get_clientdata(client));463struct workqueue_struct *q;464465/*466* Come out of digital power down467* The CX23888, at least, needs this, otherwise registers aside from468* 0x0-0x2 can't be read or written.469*/470cx25840_write(client, 0x000, 0);471472/* Internal Reset */473cx25840_and_or(client, 0x102, ~0x01, 0x01);474cx25840_and_or(client, 0x102, ~0x01, 0x00);475476/* Stop microcontroller */477cx25840_and_or(client, 0x803, ~0x10, 0x00);478479/* DIF in reset? */480cx25840_write(client, 0x398, 0);481482/*483* Trust the default xtal, no division484* '885: 28.636363... MHz485* '887: 25.000000 MHz486* '888: 50.000000 MHz487*/488cx25840_write(client, 0x2, 0x76);489490/* Power up all the PLL's and DLL */491cx25840_write(client, 0x1, 0x40);492493/* Sys PLL */494switch (state->id) {495case V4L2_IDENT_CX23888_AV:496/*497* 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz498* 572.73 MHz before post divide499*/500cx25840_write4(client, 0x11c, 0x00e8ba26);501cx25840_write4(client, 0x118, 0x0000040b);502break;503case V4L2_IDENT_CX23887_AV:504/*505* 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz506* 572.73 MHz before post divide507*/508cx25840_write4(client, 0x11c, 0x01d1744c);509cx25840_write4(client, 0x118, 0x00000416);510break;511case V4L2_IDENT_CX23885_AV:512default:513/*514* 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz515* 572.73 MHz before post divide516*/517cx25840_write4(client, 0x11c, 0x00000000);518cx25840_write4(client, 0x118, 0x00000414);519break;520}521522/* Disable DIF bypass */523cx25840_write4(client, 0x33c, 0x00000001);524525/* DIF Src phase inc */526cx25840_write4(client, 0x340, 0x0df7df83);527528/*529* Vid PLL530* Setup for a BT.656 pixel clock of 13.5 Mpixels/second531*532* 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz533* 432.0 MHz before post divide534*/535cx25840_write4(client, 0x10c, 0x002be2c9);536cx25840_write4(client, 0x108, 0x0000040f);537538/* Luma */539cx25840_write4(client, 0x414, 0x00107d12);540541/* Chroma */542cx25840_write4(client, 0x420, 0x3d008282);543544/*545* Aux PLL546* Initial setup for audio sample clock:547* 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz548* Initial I2S output/master clock(?):549* 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz550*/551switch (state->id) {552case V4L2_IDENT_CX23888_AV:553/*554* 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz555* 368.64 MHz before post divide556* 122.88 MHz / 0xa = 12.288 MHz557*/558cx25840_write4(client, 0x114, 0x00bedfa4);559cx25840_write4(client, 0x110, 0x000a0307);560break;561case V4L2_IDENT_CX23887_AV:562/*563* 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz564* 368.64 MHz before post divide565* 122.88 MHz / 0xa = 12.288 MHz566*/567cx25840_write4(client, 0x114, 0x017dbf48);568cx25840_write4(client, 0x110, 0x000a030e);569break;570case V4L2_IDENT_CX23885_AV:571default:572/*573* 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz574* 368.64 MHz before post divide575* 122.88 MHz / 0xa = 12.288 MHz576*/577cx25840_write4(client, 0x114, 0x01bf0c9e);578cx25840_write4(client, 0x110, 0x000a030c);579break;580};581582/* ADC2 input select */583cx25840_write(client, 0x102, 0x10);584585/* VIN1 & VIN5 */586cx25840_write(client, 0x103, 0x11);587588/* Enable format auto detect */589cx25840_write(client, 0x400, 0);590/* Fast subchroma lock */591/* White crush, Chroma AGC & Chroma Killer enabled */592cx25840_write(client, 0x401, 0xe8);593594/* Select AFE clock pad output source */595cx25840_write(client, 0x144, 0x05);596597/* Drive GPIO2 direction and values for HVR1700598* where an onboard mux selects the output of demodulator599* vs the 417. Failure to set this results in no DTV.600* It's safe to set this across all Hauppauge boards601* currently, regardless of the board type.602*/603cx25840_write(client, 0x160, 0x1d);604cx25840_write(client, 0x164, 0x00);605606/* Do the firmware load in a work handler to prevent.607Otherwise the kernel is blocked waiting for the608bit-banging i2c interface to finish uploading the609firmware. */610INIT_WORK(&state->fw_work, cx25840_work_handler);611init_waitqueue_head(&state->fw_wait);612q = create_singlethread_workqueue("cx25840_fw");613prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);614queue_work(q, &state->fw_work);615schedule();616finish_wait(&state->fw_wait, &wait);617destroy_workqueue(q);618619cx25840_std_setup(client);620621/* (re)set input */622set_input(client, state->vid_input, state->aud_input);623624/* start microcontroller */625cx25840_and_or(client, 0x803, ~0x10, 0x10);626627/* Disable and clear video interrupts - we don't use them */628cx25840_write4(client, CX25840_VID_INT_STAT_REG, 0xffffffff);629630/* Disable and clear audio interrupts - we don't use them */631cx25840_write(client, CX25840_AUD_INT_CTRL_REG, 0xff);632cx25840_write(client, CX25840_AUD_INT_STAT_REG, 0xff);633}634635/* ----------------------------------------------------------------------- */636637static void cx231xx_initialize(struct i2c_client *client)638{639DEFINE_WAIT(wait);640struct cx25840_state *state = to_state(i2c_get_clientdata(client));641struct workqueue_struct *q;642643/* Internal Reset */644cx25840_and_or(client, 0x102, ~0x01, 0x01);645cx25840_and_or(client, 0x102, ~0x01, 0x00);646647/* Stop microcontroller */648cx25840_and_or(client, 0x803, ~0x10, 0x00);649650/* DIF in reset? */651cx25840_write(client, 0x398, 0);652653/* Trust the default xtal, no division */654/* This changes for the cx23888 products */655cx25840_write(client, 0x2, 0x76);656657/* Bring down the regulator for AUX clk */658cx25840_write(client, 0x1, 0x40);659660/* Disable DIF bypass */661cx25840_write4(client, 0x33c, 0x00000001);662663/* DIF Src phase inc */664cx25840_write4(client, 0x340, 0x0df7df83);665666/* Luma */667cx25840_write4(client, 0x414, 0x00107d12);668669/* Chroma */670cx25840_write4(client, 0x420, 0x3d008282);671672/* ADC2 input select */673cx25840_write(client, 0x102, 0x10);674675/* VIN1 & VIN5 */676cx25840_write(client, 0x103, 0x11);677678/* Enable format auto detect */679cx25840_write(client, 0x400, 0);680/* Fast subchroma lock */681/* White crush, Chroma AGC & Chroma Killer enabled */682cx25840_write(client, 0x401, 0xe8);683684/* Do the firmware load in a work handler to prevent.685Otherwise the kernel is blocked waiting for the686bit-banging i2c interface to finish uploading the687firmware. */688INIT_WORK(&state->fw_work, cx25840_work_handler);689init_waitqueue_head(&state->fw_wait);690q = create_singlethread_workqueue("cx25840_fw");691prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);692queue_work(q, &state->fw_work);693schedule();694finish_wait(&state->fw_wait, &wait);695destroy_workqueue(q);696697cx25840_std_setup(client);698699/* (re)set input */700set_input(client, state->vid_input, state->aud_input);701702/* start microcontroller */703cx25840_and_or(client, 0x803, ~0x10, 0x10);704}705706/* ----------------------------------------------------------------------- */707708void cx25840_std_setup(struct i2c_client *client)709{710struct cx25840_state *state = to_state(i2c_get_clientdata(client));711v4l2_std_id std = state->std;712int hblank, hactive, burst, vblank, vactive, sc;713int vblank656, src_decimation;714int luma_lpf, uv_lpf, comb;715u32 pll_int, pll_frac, pll_post;716717/* datasheet startup, step 8d */718if (std & ~V4L2_STD_NTSC)719cx25840_write(client, 0x49f, 0x11);720else721cx25840_write(client, 0x49f, 0x14);722723if (std & V4L2_STD_625_50) {724hblank = 132;725hactive = 720;726burst = 93;727vblank = 36;728vactive = 580;729vblank656 = 40;730src_decimation = 0x21f;731luma_lpf = 2;732733if (std & V4L2_STD_SECAM) {734uv_lpf = 0;735comb = 0;736sc = 0x0a425f;737} else if (std == V4L2_STD_PAL_Nc) {738uv_lpf = 1;739comb = 0x20;740sc = 556453;741} else {742uv_lpf = 1;743comb = 0x20;744sc = 688739;745}746} else {747hactive = 720;748hblank = 122;749vactive = 487;750luma_lpf = 1;751uv_lpf = 1;752753src_decimation = 0x21f;754if (std == V4L2_STD_PAL_60) {755vblank = 26;756vblank656 = 26;757burst = 0x5b;758luma_lpf = 2;759comb = 0x20;760sc = 688739;761} else if (std == V4L2_STD_PAL_M) {762vblank = 20;763vblank656 = 24;764burst = 0x61;765comb = 0x20;766sc = 555452;767} else {768vblank = 26;769vblank656 = 26;770burst = 0x5b;771comb = 0x66;772sc = 556063;773}774}775776/* DEBUG: Displays configured PLL frequency */777if (!is_cx231xx(state)) {778pll_int = cx25840_read(client, 0x108);779pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;780pll_post = cx25840_read(client, 0x109);781v4l_dbg(1, cx25840_debug, client,782"PLL regs = int: %u, frac: %u, post: %u\n",783pll_int, pll_frac, pll_post);784785if (pll_post) {786int fin, fsc;787int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;788789pll /= pll_post;790v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",791pll / 1000000, pll % 1000000);792v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",793pll / 8000000, (pll / 8) % 1000000);794795fin = ((u64)src_decimation * pll) >> 12;796v4l_dbg(1, cx25840_debug, client,797"ADC Sampling freq = %d.%06d MHz\n",798fin / 1000000, fin % 1000000);799800fsc = (((u64)sc) * pll) >> 24L;801v4l_dbg(1, cx25840_debug, client,802"Chroma sub-carrier freq = %d.%06d MHz\n",803fsc / 1000000, fsc % 1000000);804805v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "806"vblank %i, vactive %i, vblank656 %i, src_dec %i, "807"burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "808"sc 0x%06x\n",809hblank, hactive, vblank, vactive, vblank656,810src_decimation, burst, luma_lpf, uv_lpf, comb, sc);811}812}813814/* Sets horizontal blanking delay and active lines */815cx25840_write(client, 0x470, hblank);816cx25840_write(client, 0x471,8170xff & (((hblank >> 8) & 0x3) | (hactive << 4)));818cx25840_write(client, 0x472, hactive >> 4);819820/* Sets burst gate delay */821cx25840_write(client, 0x473, burst);822823/* Sets vertical blanking delay and active duration */824cx25840_write(client, 0x474, vblank);825cx25840_write(client, 0x475,8260xff & (((vblank >> 8) & 0x3) | (vactive << 4)));827cx25840_write(client, 0x476, vactive >> 4);828cx25840_write(client, 0x477, vblank656);829830/* Sets src decimation rate */831cx25840_write(client, 0x478, 0xff & src_decimation);832cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));833834/* Sets Luma and UV Low pass filters */835cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));836837/* Enables comb filters */838cx25840_write(client, 0x47b, comb);839840/* Sets SC Step*/841cx25840_write(client, 0x47c, sc);842cx25840_write(client, 0x47d, 0xff & sc >> 8);843cx25840_write(client, 0x47e, 0xff & sc >> 16);844845/* Sets VBI parameters */846if (std & V4L2_STD_625_50) {847cx25840_write(client, 0x47f, 0x01);848state->vbi_line_offset = 5;849} else {850cx25840_write(client, 0x47f, 0x00);851state->vbi_line_offset = 8;852}853}854855/* ----------------------------------------------------------------------- */856857static void input_change(struct i2c_client *client)858{859struct cx25840_state *state = to_state(i2c_get_clientdata(client));860v4l2_std_id std = state->std;861862/* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */863if (std & V4L2_STD_SECAM) {864cx25840_write(client, 0x402, 0);865}866else {867cx25840_write(client, 0x402, 0x04);868cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);869}870cx25840_and_or(client, 0x401, ~0x60, 0);871cx25840_and_or(client, 0x401, ~0x60, 0x60);872873/* Don't write into audio registers on cx2583x chips */874if (is_cx2583x(state))875return;876877cx25840_and_or(client, 0x810, ~0x01, 1);878879if (state->radio) {880cx25840_write(client, 0x808, 0xf9);881cx25840_write(client, 0x80b, 0x00);882}883else if (std & V4L2_STD_525_60) {884/* Certain Hauppauge PVR150 models have a hardware bug885that causes audio to drop out. For these models the886audio standard must be set explicitly.887To be precise: it affects cards with tuner models88885, 99 and 112 (model numbers from tveeprom). */889int hw_fix = state->pvr150_workaround;890891if (std == V4L2_STD_NTSC_M_JP) {892/* Japan uses EIAJ audio standard */893cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);894} else if (std == V4L2_STD_NTSC_M_KR) {895/* South Korea uses A2 audio standard */896cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);897} else {898/* Others use the BTSC audio standard */899cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);900}901cx25840_write(client, 0x80b, 0x00);902} else if (std & V4L2_STD_PAL) {903/* Autodetect audio standard and audio system */904cx25840_write(client, 0x808, 0xff);905/* Since system PAL-L is pretty much non-existent and906not used by any public broadcast network, force9076.5 MHz carrier to be interpreted as System DK,908this avoids DK audio detection instability */909cx25840_write(client, 0x80b, 0x00);910} else if (std & V4L2_STD_SECAM) {911/* Autodetect audio standard and audio system */912cx25840_write(client, 0x808, 0xff);913/* If only one of SECAM-DK / SECAM-L is required, then force9146.5MHz carrier, else autodetect it */915if ((std & V4L2_STD_SECAM_DK) &&916!(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {917/* 6.5 MHz carrier to be interpreted as System DK */918cx25840_write(client, 0x80b, 0x00);919} else if (!(std & V4L2_STD_SECAM_DK) &&920(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {921/* 6.5 MHz carrier to be interpreted as System L */922cx25840_write(client, 0x80b, 0x08);923} else {924/* 6.5 MHz carrier to be autodetected */925cx25840_write(client, 0x80b, 0x10);926}927}928929cx25840_and_or(client, 0x810, ~0x01, 0);930}931932static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,933enum cx25840_audio_input aud_input)934{935struct cx25840_state *state = to_state(i2c_get_clientdata(client));936u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&937vid_input <= CX25840_COMPOSITE8);938u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==939CX25840_COMPONENT_ON;940int luma = vid_input & 0xf0;941int chroma = vid_input & 0xf00;942u8 reg;943944v4l_dbg(1, cx25840_debug, client,945"decoder set video input %d, audio input %d\n",946vid_input, aud_input);947948if (vid_input >= CX25840_VIN1_CH1) {949v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",950vid_input);951reg = vid_input & 0xff;952is_composite = !is_component &&953((vid_input & CX25840_SVIDEO_ON) != CX25840_SVIDEO_ON);954955v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",956reg, is_composite);957} else if (is_composite) {958reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);959} else {960if ((vid_input & ~0xff0) ||961luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||962chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {963v4l_err(client, "0x%04x is not a valid video input!\n",964vid_input);965return -EINVAL;966}967reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);968if (chroma >= CX25840_SVIDEO_CHROMA7) {969reg &= 0x3f;970reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;971} else {972reg &= 0xcf;973reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;974}975}976977/* The caller has previously prepared the correct routing978* configuration in reg (for the cx23885) so we have no979* need to attempt to flip bits for earlier av decoders.980*/981if (!is_cx2388x(state) && !is_cx231xx(state)) {982switch (aud_input) {983case CX25840_AUDIO_SERIAL:984/* do nothing, use serial audio input */985break;986case CX25840_AUDIO4: reg &= ~0x30; break;987case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;988case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;989case CX25840_AUDIO7: reg &= ~0xc0; break;990case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;991992default:993v4l_err(client, "0x%04x is not a valid audio input!\n",994aud_input);995return -EINVAL;996}997}998999cx25840_write(client, 0x103, reg);10001001/* Set INPUT_MODE to Composite, S-Video or Component */1002if (is_component)1003cx25840_and_or(client, 0x401, ~0x6, 0x6);1004else1005cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);10061007if (!is_cx2388x(state) && !is_cx231xx(state)) {1008/* Set CH_SEL_ADC2 to 1 if input comes from CH3 */1009cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);1010/* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */1011if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)1012cx25840_and_or(client, 0x102, ~0x4, 4);1013else1014cx25840_and_or(client, 0x102, ~0x4, 0);1015} else {1016/* Set DUAL_MODE_ADC2 to 1 if component*/1017cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);1018if (is_composite) {1019/* ADC2 input select channel 2 */1020cx25840_and_or(client, 0x102, ~0x2, 0);1021} else if (!is_component) {1022/* S-Video */1023if (chroma >= CX25840_SVIDEO_CHROMA7) {1024/* ADC2 input select channel 3 */1025cx25840_and_or(client, 0x102, ~0x2, 2);1026} else {1027/* ADC2 input select channel 2 */1028cx25840_and_or(client, 0x102, ~0x2, 0);1029}1030}1031}10321033state->vid_input = vid_input;1034state->aud_input = aud_input;1035cx25840_audio_set_path(client);1036input_change(client);10371038if (is_cx2388x(state)) {1039/* Audio channel 1 src : Parallel 1 */1040cx25840_write(client, 0x124, 0x03);10411042/* Select AFE clock pad output source */1043cx25840_write(client, 0x144, 0x05);10441045/* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */1046cx25840_write(client, 0x914, 0xa0);10471048/* I2S_OUT_CTL:1049* I2S_IN_SONY_MODE, LEFT SAMPLE on WS=11050* I2S_OUT_MASTER_MODE = Master1051*/1052cx25840_write(client, 0x918, 0xa0);1053cx25840_write(client, 0x919, 0x01);1054} else if (is_cx231xx(state)) {1055/* Audio channel 1 src : Parallel 1 */1056cx25840_write(client, 0x124, 0x03);10571058/* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */1059cx25840_write(client, 0x914, 0xa0);10601061/* I2S_OUT_CTL:1062* I2S_IN_SONY_MODE, LEFT SAMPLE on WS=11063* I2S_OUT_MASTER_MODE = Master1064*/1065cx25840_write(client, 0x918, 0xa0);1066cx25840_write(client, 0x919, 0x01);1067}10681069return 0;1070}10711072/* ----------------------------------------------------------------------- */10731074static int set_v4lstd(struct i2c_client *client)1075{1076struct cx25840_state *state = to_state(i2c_get_clientdata(client));1077u8 fmt = 0; /* zero is autodetect */1078u8 pal_m = 0;10791080/* First tests should be against specific std */1081if (state->std == V4L2_STD_NTSC_M_JP) {1082fmt = 0x2;1083} else if (state->std == V4L2_STD_NTSC_443) {1084fmt = 0x3;1085} else if (state->std == V4L2_STD_PAL_M) {1086pal_m = 1;1087fmt = 0x5;1088} else if (state->std == V4L2_STD_PAL_N) {1089fmt = 0x6;1090} else if (state->std == V4L2_STD_PAL_Nc) {1091fmt = 0x7;1092} else if (state->std == V4L2_STD_PAL_60) {1093fmt = 0x8;1094} else {1095/* Then, test against generic ones */1096if (state->std & V4L2_STD_NTSC)1097fmt = 0x1;1098else if (state->std & V4L2_STD_PAL)1099fmt = 0x4;1100else if (state->std & V4L2_STD_SECAM)1101fmt = 0xc;1102}11031104v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);11051106/* Follow step 9 of section 3.16 in the cx25840 datasheet.1107Without this PAL may display a vertical ghosting effect.1108This happens for example with the Yuan MPC622. */1109if (fmt >= 4 && fmt < 8) {1110/* Set format to NTSC-M */1111cx25840_and_or(client, 0x400, ~0xf, 1);1112/* Turn off LCOMB */1113cx25840_and_or(client, 0x47b, ~6, 0);1114}1115cx25840_and_or(client, 0x400, ~0xf, fmt);1116cx25840_and_or(client, 0x403, ~0x3, pal_m);1117cx25840_std_setup(client);1118if (!is_cx2583x(state))1119input_change(client);1120return 0;1121}11221123/* ----------------------------------------------------------------------- */11241125static int cx25840_s_ctrl(struct v4l2_ctrl *ctrl)1126{1127struct v4l2_subdev *sd = to_sd(ctrl);1128struct i2c_client *client = v4l2_get_subdevdata(sd);11291130switch (ctrl->id) {1131case V4L2_CID_BRIGHTNESS:1132cx25840_write(client, 0x414, ctrl->val - 128);1133break;11341135case V4L2_CID_CONTRAST:1136cx25840_write(client, 0x415, ctrl->val << 1);1137break;11381139case V4L2_CID_SATURATION:1140cx25840_write(client, 0x420, ctrl->val << 1);1141cx25840_write(client, 0x421, ctrl->val << 1);1142break;11431144case V4L2_CID_HUE:1145cx25840_write(client, 0x422, ctrl->val);1146break;11471148default:1149return -EINVAL;1150}11511152return 0;1153}11541155/* ----------------------------------------------------------------------- */11561157static int cx25840_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)1158{1159struct cx25840_state *state = to_state(sd);1160struct i2c_client *client = v4l2_get_subdevdata(sd);1161int HSC, VSC, Vsrc, Hsrc, filter, Vlines;1162int is_50Hz = !(state->std & V4L2_STD_525_60);11631164if (fmt->code != V4L2_MBUS_FMT_FIXED)1165return -EINVAL;11661167fmt->field = V4L2_FIELD_INTERLACED;1168fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;11691170Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;1171Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;11721173Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;1174Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;11751176Vlines = fmt->height + (is_50Hz ? 4 : 7);11771178if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||1179(Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {1180v4l_err(client, "%dx%d is not a valid size!\n",1181fmt->width, fmt->height);1182return -ERANGE;1183}11841185HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);1186VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));1187VSC &= 0x1fff;11881189if (fmt->width >= 385)1190filter = 0;1191else if (fmt->width > 192)1192filter = 1;1193else if (fmt->width > 96)1194filter = 2;1195else1196filter = 3;11971198v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",1199fmt->width, fmt->height, HSC, VSC);12001201/* HSCALE=HSC */1202cx25840_write(client, 0x418, HSC & 0xff);1203cx25840_write(client, 0x419, (HSC >> 8) & 0xff);1204cx25840_write(client, 0x41a, HSC >> 16);1205/* VSCALE=VSC */1206cx25840_write(client, 0x41c, VSC & 0xff);1207cx25840_write(client, 0x41d, VSC >> 8);1208/* VS_INTRLACE=1 VFILT=filter */1209cx25840_write(client, 0x41e, 0x8 | filter);1210return 0;1211}12121213/* ----------------------------------------------------------------------- */12141215static void log_video_status(struct i2c_client *client)1216{1217static const char *const fmt_strs[] = {1218"0x0",1219"NTSC-M", "NTSC-J", "NTSC-4.43",1220"PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",1221"0x9", "0xA", "0xB",1222"SECAM",1223"0xD", "0xE", "0xF"1224};12251226struct cx25840_state *state = to_state(i2c_get_clientdata(client));1227u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;1228u8 gen_stat1 = cx25840_read(client, 0x40d);1229u8 gen_stat2 = cx25840_read(client, 0x40e);1230int vid_input = state->vid_input;12311232v4l_info(client, "Video signal: %spresent\n",1233(gen_stat2 & 0x20) ? "" : "not ");1234v4l_info(client, "Detected format: %s\n",1235fmt_strs[gen_stat1 & 0xf]);12361237v4l_info(client, "Specified standard: %s\n",1238vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");12391240if (vid_input >= CX25840_COMPOSITE1 &&1241vid_input <= CX25840_COMPOSITE8) {1242v4l_info(client, "Specified video input: Composite %d\n",1243vid_input - CX25840_COMPOSITE1 + 1);1244} else {1245v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",1246(vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);1247}12481249v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);1250}12511252/* ----------------------------------------------------------------------- */12531254static void log_audio_status(struct i2c_client *client)1255{1256struct cx25840_state *state = to_state(i2c_get_clientdata(client));1257u8 download_ctl = cx25840_read(client, 0x803);1258u8 mod_det_stat0 = cx25840_read(client, 0x804);1259u8 mod_det_stat1 = cx25840_read(client, 0x805);1260u8 audio_config = cx25840_read(client, 0x808);1261u8 pref_mode = cx25840_read(client, 0x809);1262u8 afc0 = cx25840_read(client, 0x80b);1263u8 mute_ctl = cx25840_read(client, 0x8d3);1264int aud_input = state->aud_input;1265char *p;12661267switch (mod_det_stat0) {1268case 0x00: p = "mono"; break;1269case 0x01: p = "stereo"; break;1270case 0x02: p = "dual"; break;1271case 0x04: p = "tri"; break;1272case 0x10: p = "mono with SAP"; break;1273case 0x11: p = "stereo with SAP"; break;1274case 0x12: p = "dual with SAP"; break;1275case 0x14: p = "tri with SAP"; break;1276case 0xfe: p = "forced mode"; break;1277default: p = "not defined";1278}1279v4l_info(client, "Detected audio mode: %s\n", p);12801281switch (mod_det_stat1) {1282case 0x00: p = "not defined"; break;1283case 0x01: p = "EIAJ"; break;1284case 0x02: p = "A2-M"; break;1285case 0x03: p = "A2-BG"; break;1286case 0x04: p = "A2-DK1"; break;1287case 0x05: p = "A2-DK2"; break;1288case 0x06: p = "A2-DK3"; break;1289case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;1290case 0x08: p = "AM-L"; break;1291case 0x09: p = "NICAM-BG"; break;1292case 0x0a: p = "NICAM-DK"; break;1293case 0x0b: p = "NICAM-I"; break;1294case 0x0c: p = "NICAM-L"; break;1295case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;1296case 0x0e: p = "IF FM Radio"; break;1297case 0x0f: p = "BTSC"; break;1298case 0x10: p = "high-deviation FM"; break;1299case 0x11: p = "very high-deviation FM"; break;1300case 0xfd: p = "unknown audio standard"; break;1301case 0xfe: p = "forced audio standard"; break;1302case 0xff: p = "no detected audio standard"; break;1303default: p = "not defined";1304}1305v4l_info(client, "Detected audio standard: %s\n", p);1306v4l_info(client, "Audio microcontroller: %s\n",1307(download_ctl & 0x10) ?1308((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");13091310switch (audio_config >> 4) {1311case 0x00: p = "undefined"; break;1312case 0x01: p = "BTSC"; break;1313case 0x02: p = "EIAJ"; break;1314case 0x03: p = "A2-M"; break;1315case 0x04: p = "A2-BG"; break;1316case 0x05: p = "A2-DK1"; break;1317case 0x06: p = "A2-DK2"; break;1318case 0x07: p = "A2-DK3"; break;1319case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;1320case 0x09: p = "AM-L"; break;1321case 0x0a: p = "NICAM-BG"; break;1322case 0x0b: p = "NICAM-DK"; break;1323case 0x0c: p = "NICAM-I"; break;1324case 0x0d: p = "NICAM-L"; break;1325case 0x0e: p = "FM radio"; break;1326case 0x0f: p = "automatic detection"; break;1327default: p = "undefined";1328}1329v4l_info(client, "Configured audio standard: %s\n", p);13301331if ((audio_config >> 4) < 0xF) {1332switch (audio_config & 0xF) {1333case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;1334case 0x01: p = "MONO2 (LANGUAGE B)"; break;1335case 0x02: p = "MONO3 (STEREO forced MONO)"; break;1336case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;1337case 0x04: p = "STEREO"; break;1338case 0x05: p = "DUAL1 (AB)"; break;1339case 0x06: p = "DUAL2 (AC) (FM)"; break;1340case 0x07: p = "DUAL3 (BC) (FM)"; break;1341case 0x08: p = "DUAL4 (AC) (AM)"; break;1342case 0x09: p = "DUAL5 (BC) (AM)"; break;1343case 0x0a: p = "SAP"; break;1344default: p = "undefined";1345}1346v4l_info(client, "Configured audio mode: %s\n", p);1347} else {1348switch (audio_config & 0xF) {1349case 0x00: p = "BG"; break;1350case 0x01: p = "DK1"; break;1351case 0x02: p = "DK2"; break;1352case 0x03: p = "DK3"; break;1353case 0x04: p = "I"; break;1354case 0x05: p = "L"; break;1355case 0x06: p = "BTSC"; break;1356case 0x07: p = "EIAJ"; break;1357case 0x08: p = "A2-M"; break;1358case 0x09: p = "FM Radio"; break;1359case 0x0f: p = "automatic standard and mode detection"; break;1360default: p = "undefined";1361}1362v4l_info(client, "Configured audio system: %s\n", p);1363}13641365if (aud_input) {1366v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);1367} else {1368v4l_info(client, "Specified audio input: External\n");1369}13701371switch (pref_mode & 0xf) {1372case 0: p = "mono/language A"; break;1373case 1: p = "language B"; break;1374case 2: p = "language C"; break;1375case 3: p = "analog fallback"; break;1376case 4: p = "stereo"; break;1377case 5: p = "language AC"; break;1378case 6: p = "language BC"; break;1379case 7: p = "language AB"; break;1380default: p = "undefined";1381}1382v4l_info(client, "Preferred audio mode: %s\n", p);13831384if ((audio_config & 0xf) == 0xf) {1385switch ((afc0 >> 3) & 0x3) {1386case 0: p = "system DK"; break;1387case 1: p = "system L"; break;1388case 2: p = "autodetect"; break;1389default: p = "undefined";1390}1391v4l_info(client, "Selected 65 MHz format: %s\n", p);13921393switch (afc0 & 0x7) {1394case 0: p = "chroma"; break;1395case 1: p = "BTSC"; break;1396case 2: p = "EIAJ"; break;1397case 3: p = "A2-M"; break;1398case 4: p = "autodetect"; break;1399default: p = "undefined";1400}1401v4l_info(client, "Selected 45 MHz format: %s\n", p);1402}1403}14041405/* ----------------------------------------------------------------------- */14061407/* This load_fw operation must be called to load the driver's firmware.1408Without this the audio standard detection will fail and you will1409only get mono.14101411Since loading the firmware is often problematic when the driver is1412compiled into the kernel I recommend postponing calling this function1413until the first open of the video device. Another reason for1414postponing it is that loading this firmware takes a long time (seconds)1415due to the slow i2c bus speed. So it will speed up the boot process if1416you can avoid loading the fw as long as the video device isn't used. */1417static int cx25840_load_fw(struct v4l2_subdev *sd)1418{1419struct cx25840_state *state = to_state(sd);1420struct i2c_client *client = v4l2_get_subdevdata(sd);14211422if (!state->is_initialized) {1423/* initialize and load firmware */1424state->is_initialized = 1;1425if (is_cx2583x(state))1426cx25836_initialize(client);1427else if (is_cx2388x(state))1428cx23885_initialize(client);1429else if (is_cx231xx(state))1430cx231xx_initialize(client);1431else1432cx25840_initialize(client);1433}1434return 0;1435}14361437#ifdef CONFIG_VIDEO_ADV_DEBUG1438static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)1439{1440struct i2c_client *client = v4l2_get_subdevdata(sd);14411442if (!v4l2_chip_match_i2c_client(client, ®->match))1443return -EINVAL;1444if (!capable(CAP_SYS_ADMIN))1445return -EPERM;1446reg->size = 1;1447reg->val = cx25840_read(client, reg->reg & 0x0fff);1448return 0;1449}14501451static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)1452{1453struct i2c_client *client = v4l2_get_subdevdata(sd);14541455if (!v4l2_chip_match_i2c_client(client, ®->match))1456return -EINVAL;1457if (!capable(CAP_SYS_ADMIN))1458return -EPERM;1459cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);1460return 0;1461}1462#endif14631464static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)1465{1466struct cx25840_state *state = to_state(sd);1467struct i2c_client *client = v4l2_get_subdevdata(sd);1468u8 v;14691470if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))1471return 0;14721473v4l_dbg(1, cx25840_debug, client, "%s audio output\n",1474enable ? "enable" : "disable");14751476if (enable) {1477v = cx25840_read(client, 0x115) | 0x80;1478cx25840_write(client, 0x115, v);1479v = cx25840_read(client, 0x116) | 0x03;1480cx25840_write(client, 0x116, v);1481} else {1482v = cx25840_read(client, 0x115) & ~(0x80);1483cx25840_write(client, 0x115, v);1484v = cx25840_read(client, 0x116) & ~(0x03);1485cx25840_write(client, 0x116, v);1486}1487return 0;1488}14891490static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)1491{1492struct cx25840_state *state = to_state(sd);1493struct i2c_client *client = v4l2_get_subdevdata(sd);1494u8 v;14951496v4l_dbg(1, cx25840_debug, client, "%s video output\n",1497enable ? "enable" : "disable");1498if (enable) {1499if (is_cx2388x(state) || is_cx231xx(state)) {1500v = cx25840_read(client, 0x421) | 0x0b;1501cx25840_write(client, 0x421, v);1502} else {1503v = cx25840_read(client, 0x115) | 0x0c;1504cx25840_write(client, 0x115, v);1505v = cx25840_read(client, 0x116) | 0x04;1506cx25840_write(client, 0x116, v);1507}1508} else {1509if (is_cx2388x(state) || is_cx231xx(state)) {1510v = cx25840_read(client, 0x421) & ~(0x0b);1511cx25840_write(client, 0x421, v);1512} else {1513v = cx25840_read(client, 0x115) & ~(0x0c);1514cx25840_write(client, 0x115, v);1515v = cx25840_read(client, 0x116) & ~(0x04);1516cx25840_write(client, 0x116, v);1517}1518}1519return 0;1520}15211522static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)1523{1524struct cx25840_state *state = to_state(sd);1525struct i2c_client *client = v4l2_get_subdevdata(sd);15261527if (state->radio == 0 && state->std == std)1528return 0;1529state->radio = 0;1530state->std = std;1531return set_v4lstd(client);1532}15331534static int cx25840_s_radio(struct v4l2_subdev *sd)1535{1536struct cx25840_state *state = to_state(sd);15371538state->radio = 1;1539return 0;1540}15411542static int cx25840_s_video_routing(struct v4l2_subdev *sd,1543u32 input, u32 output, u32 config)1544{1545struct cx25840_state *state = to_state(sd);1546struct i2c_client *client = v4l2_get_subdevdata(sd);15471548return set_input(client, input, state->aud_input);1549}15501551static int cx25840_s_audio_routing(struct v4l2_subdev *sd,1552u32 input, u32 output, u32 config)1553{1554struct cx25840_state *state = to_state(sd);1555struct i2c_client *client = v4l2_get_subdevdata(sd);15561557return set_input(client, state->vid_input, input);1558}15591560static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)1561{1562struct i2c_client *client = v4l2_get_subdevdata(sd);15631564input_change(client);1565return 0;1566}15671568static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)1569{1570struct cx25840_state *state = to_state(sd);1571struct i2c_client *client = v4l2_get_subdevdata(sd);1572u8 vpres = cx25840_read(client, 0x40e) & 0x20;1573u8 mode;1574int val = 0;15751576if (state->radio)1577return 0;15781579vt->signal = vpres ? 0xffff : 0x0;1580if (is_cx2583x(state))1581return 0;15821583vt->capability |=1584V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |1585V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;15861587mode = cx25840_read(client, 0x804);15881589/* get rxsubchans and audmode */1590if ((mode & 0xf) == 1)1591val |= V4L2_TUNER_SUB_STEREO;1592else1593val |= V4L2_TUNER_SUB_MONO;15941595if (mode == 2 || mode == 4)1596val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;15971598if (mode & 0x10)1599val |= V4L2_TUNER_SUB_SAP;16001601vt->rxsubchans = val;1602vt->audmode = state->audmode;1603return 0;1604}16051606static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)1607{1608struct cx25840_state *state = to_state(sd);1609struct i2c_client *client = v4l2_get_subdevdata(sd);16101611if (state->radio || is_cx2583x(state))1612return 0;16131614switch (vt->audmode) {1615case V4L2_TUNER_MODE_MONO:1616/* mono -> mono1617stereo -> mono1618bilingual -> lang1 */1619cx25840_and_or(client, 0x809, ~0xf, 0x00);1620break;1621case V4L2_TUNER_MODE_STEREO:1622case V4L2_TUNER_MODE_LANG1:1623/* mono -> mono1624stereo -> stereo1625bilingual -> lang1 */1626cx25840_and_or(client, 0x809, ~0xf, 0x04);1627break;1628case V4L2_TUNER_MODE_LANG1_LANG2:1629/* mono -> mono1630stereo -> stereo1631bilingual -> lang1/lang2 */1632cx25840_and_or(client, 0x809, ~0xf, 0x07);1633break;1634case V4L2_TUNER_MODE_LANG2:1635/* mono -> mono1636stereo -> stereo1637bilingual -> lang2 */1638cx25840_and_or(client, 0x809, ~0xf, 0x01);1639break;1640default:1641return -EINVAL;1642}1643state->audmode = vt->audmode;1644return 0;1645}16461647static int cx25840_reset(struct v4l2_subdev *sd, u32 val)1648{1649struct cx25840_state *state = to_state(sd);1650struct i2c_client *client = v4l2_get_subdevdata(sd);16511652if (is_cx2583x(state))1653cx25836_initialize(client);1654else if (is_cx2388x(state))1655cx23885_initialize(client);1656else if (is_cx231xx(state))1657cx231xx_initialize(client);1658else1659cx25840_initialize(client);1660return 0;1661}16621663static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)1664{1665struct cx25840_state *state = to_state(sd);1666struct i2c_client *client = v4l2_get_subdevdata(sd);16671668return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);1669}16701671static int cx25840_log_status(struct v4l2_subdev *sd)1672{1673struct cx25840_state *state = to_state(sd);1674struct i2c_client *client = v4l2_get_subdevdata(sd);16751676log_video_status(client);1677if (!is_cx2583x(state))1678log_audio_status(client);1679cx25840_ir_log_status(sd);1680v4l2_ctrl_handler_log_status(&state->hdl, sd->name);1681return 0;1682}16831684static int cx23885_irq_handler(struct v4l2_subdev *sd, u32 status,1685bool *handled)1686{1687struct cx25840_state *state = to_state(sd);1688struct i2c_client *c = v4l2_get_subdevdata(sd);1689u8 irq_stat, aud_stat, aud_en, ir_stat, ir_en;1690u32 vid_stat, aud_mc_stat;1691bool block_handled;1692int ret = 0;16931694irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);1695v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (entry): %s %s %s\n",1696irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",1697irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",1698irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");16991700if ((is_cx23885(state) || is_cx23887(state))) {1701ir_stat = cx25840_read(c, CX25840_IR_STATS_REG);1702ir_en = cx25840_read(c, CX25840_IR_IRQEN_REG);1703v4l_dbg(2, cx25840_debug, c,1704"AV Core ir IRQ status: %#04x disables: %#04x\n",1705ir_stat, ir_en);1706if (irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT) {1707block_handled = false;1708ret = cx25840_ir_irq_handler(sd,1709status, &block_handled);1710if (block_handled)1711*handled = true;1712}1713}17141715aud_stat = cx25840_read(c, CX25840_AUD_INT_STAT_REG);1716aud_en = cx25840_read(c, CX25840_AUD_INT_CTRL_REG);1717v4l_dbg(2, cx25840_debug, c,1718"AV Core audio IRQ status: %#04x disables: %#04x\n",1719aud_stat, aud_en);1720aud_mc_stat = cx25840_read4(c, CX23885_AUD_MC_INT_MASK_REG);1721v4l_dbg(2, cx25840_debug, c,1722"AV Core audio MC IRQ status: %#06x enables: %#06x\n",1723aud_mc_stat >> CX23885_AUD_MC_INT_STAT_SHFT,1724aud_mc_stat & CX23885_AUD_MC_INT_CTRL_BITS);1725if (irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT) {1726if (aud_stat) {1727cx25840_write(c, CX25840_AUD_INT_STAT_REG, aud_stat);1728*handled = true;1729}1730}17311732vid_stat = cx25840_read4(c, CX25840_VID_INT_STAT_REG);1733v4l_dbg(2, cx25840_debug, c,1734"AV Core video IRQ status: %#06x disables: %#06x\n",1735vid_stat & CX25840_VID_INT_STAT_BITS,1736vid_stat >> CX25840_VID_INT_MASK_SHFT);1737if (irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT) {1738if (vid_stat & CX25840_VID_INT_STAT_BITS) {1739cx25840_write4(c, CX25840_VID_INT_STAT_REG, vid_stat);1740*handled = true;1741}1742}17431744irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);1745v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (exit): %s %s %s\n",1746irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",1747irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",1748irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");17491750return ret;1751}17521753static int cx25840_irq_handler(struct v4l2_subdev *sd, u32 status,1754bool *handled)1755{1756struct cx25840_state *state = to_state(sd);17571758*handled = false;17591760/* Only support the CX2388[578] AV Core for now */1761if (is_cx2388x(state))1762return cx23885_irq_handler(sd, status, handled);17631764return -ENODEV;1765}17661767/* ----------------------------------------------------------------------- */17681769static const struct v4l2_ctrl_ops cx25840_ctrl_ops = {1770.s_ctrl = cx25840_s_ctrl,1771};17721773static const struct v4l2_subdev_core_ops cx25840_core_ops = {1774.log_status = cx25840_log_status,1775.g_chip_ident = cx25840_g_chip_ident,1776.g_ctrl = v4l2_subdev_g_ctrl,1777.s_ctrl = v4l2_subdev_s_ctrl,1778.s_ext_ctrls = v4l2_subdev_s_ext_ctrls,1779.try_ext_ctrls = v4l2_subdev_try_ext_ctrls,1780.g_ext_ctrls = v4l2_subdev_g_ext_ctrls,1781.queryctrl = v4l2_subdev_queryctrl,1782.querymenu = v4l2_subdev_querymenu,1783.s_std = cx25840_s_std,1784.reset = cx25840_reset,1785.load_fw = cx25840_load_fw,1786.s_io_pin_config = common_s_io_pin_config,1787#ifdef CONFIG_VIDEO_ADV_DEBUG1788.g_register = cx25840_g_register,1789.s_register = cx25840_s_register,1790#endif1791.interrupt_service_routine = cx25840_irq_handler,1792};17931794static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {1795.s_frequency = cx25840_s_frequency,1796.s_radio = cx25840_s_radio,1797.g_tuner = cx25840_g_tuner,1798.s_tuner = cx25840_s_tuner,1799};18001801static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {1802.s_clock_freq = cx25840_s_clock_freq,1803.s_routing = cx25840_s_audio_routing,1804.s_stream = cx25840_s_audio_stream,1805};18061807static const struct v4l2_subdev_video_ops cx25840_video_ops = {1808.s_routing = cx25840_s_video_routing,1809.s_mbus_fmt = cx25840_s_mbus_fmt,1810.s_stream = cx25840_s_stream,1811};18121813static const struct v4l2_subdev_vbi_ops cx25840_vbi_ops = {1814.decode_vbi_line = cx25840_decode_vbi_line,1815.s_raw_fmt = cx25840_s_raw_fmt,1816.s_sliced_fmt = cx25840_s_sliced_fmt,1817.g_sliced_fmt = cx25840_g_sliced_fmt,1818};18191820static const struct v4l2_subdev_ops cx25840_ops = {1821.core = &cx25840_core_ops,1822.tuner = &cx25840_tuner_ops,1823.audio = &cx25840_audio_ops,1824.video = &cx25840_video_ops,1825.vbi = &cx25840_vbi_ops,1826.ir = &cx25840_ir_ops,1827};18281829/* ----------------------------------------------------------------------- */18301831static u32 get_cx2388x_ident(struct i2c_client *client)1832{1833u32 ret;18341835/* Come out of digital power down */1836cx25840_write(client, 0x000, 0);18371838/* Detecting whether the part is cx23885/7/8 is more1839* difficult than it needs to be. No ID register. Instead we1840* probe certain registers indicated in the datasheets to look1841* for specific defaults that differ between the silicon designs. */18421843/* It's either 885/7 if the IR Tx Clk Divider register exists */1844if (cx25840_read4(client, 0x204) & 0xffff) {1845/* CX23885 returns bogus repetitive byte values for the DIF,1846* which doesn't exist for it. (Ex. 8a8a8a8a or 31313131) */1847ret = cx25840_read4(client, 0x300);1848if (((ret & 0xffff0000) >> 16) == (ret & 0xffff)) {1849/* No DIF */1850ret = V4L2_IDENT_CX23885_AV;1851} else {1852/* CX23887 has a broken DIF, but the registers1853* appear valid (but unused), good enough to detect. */1854ret = V4L2_IDENT_CX23887_AV;1855}1856} else if (cx25840_read4(client, 0x300) & 0x0fffffff) {1857/* DIF PLL Freq Word reg exists; chip must be a CX23888 */1858ret = V4L2_IDENT_CX23888_AV;1859} else {1860v4l_err(client, "Unable to detect h/w, assuming cx23887\n");1861ret = V4L2_IDENT_CX23887_AV;1862}18631864/* Back into digital power down */1865cx25840_write(client, 0x000, 2);1866return ret;1867}18681869static int cx25840_probe(struct i2c_client *client,1870const struct i2c_device_id *did)1871{1872struct cx25840_state *state;1873struct v4l2_subdev *sd;1874int default_volume;1875u32 id = V4L2_IDENT_NONE;1876u16 device_id;18771878/* Check if the adapter supports the needed features */1879if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))1880return -EIO;18811882v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);18831884device_id = cx25840_read(client, 0x101) << 8;1885device_id |= cx25840_read(client, 0x100);1886v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);18871888/* The high byte of the device ID should be1889* 0x83 for the cx2583x and 0x84 for the cx2584x */1890if ((device_id & 0xff00) == 0x8300) {1891id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;1892} else if ((device_id & 0xff00) == 0x8400) {1893id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);1894} else if (device_id == 0x0000) {1895id = get_cx2388x_ident(client);1896} else if ((device_id & 0xfff0) == 0x5A30) {1897/* The CX23100 (0x5A3C = 23100) doesn't have an A/V decoder */1898id = V4L2_IDENT_CX2310X_AV;1899} else if ((device_id & 0xff) == (device_id >> 8)) {1900v4l_err(client,1901"likely a confused/unresponsive cx2388[578] A/V decoder"1902" found @ 0x%x (%s)\n",1903client->addr << 1, client->adapter->name);1904v4l_err(client, "A method to reset it from the cx25840 driver"1905" software is not known at this time\n");1906return -ENODEV;1907} else {1908v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");1909return -ENODEV;1910}19111912state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);1913if (state == NULL)1914return -ENOMEM;19151916sd = &state->sd;1917v4l2_i2c_subdev_init(sd, client, &cx25840_ops);19181919switch (id) {1920case V4L2_IDENT_CX23885_AV:1921v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",1922client->addr << 1, client->adapter->name);1923break;1924case V4L2_IDENT_CX23887_AV:1925v4l_info(client, "cx23887 A/V decoder found @ 0x%x (%s)\n",1926client->addr << 1, client->adapter->name);1927break;1928case V4L2_IDENT_CX23888_AV:1929v4l_info(client, "cx23888 A/V decoder found @ 0x%x (%s)\n",1930client->addr << 1, client->adapter->name);1931break;1932case V4L2_IDENT_CX2310X_AV:1933v4l_info(client, "cx%d A/V decoder found @ 0x%x (%s)\n",1934device_id, client->addr << 1, client->adapter->name);1935break;1936case V4L2_IDENT_CX25840:1937case V4L2_IDENT_CX25841:1938case V4L2_IDENT_CX25842:1939case V4L2_IDENT_CX25843:1940/* Note: revision '(device_id & 0x0f) == 2' was never built. The1941marking skips from 0x1 == 22 to 0x3 == 23. */1942v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",1943(device_id & 0xfff0) >> 4,1944(device_id & 0x0f) < 3 ? (device_id & 0x0f) + 11945: (device_id & 0x0f),1946client->addr << 1, client->adapter->name);1947break;1948case V4L2_IDENT_CX25836:1949case V4L2_IDENT_CX25837:1950default:1951v4l_info(client, "cx25%3x-%x found @ 0x%x (%s)\n",1952(device_id & 0xfff0) >> 4, device_id & 0x0f,1953client->addr << 1, client->adapter->name);1954break;1955}19561957state->c = client;1958state->vid_input = CX25840_COMPOSITE7;1959state->aud_input = CX25840_AUDIO8;1960state->audclk_freq = 48000;1961state->audmode = V4L2_TUNER_MODE_LANG1;1962state->vbi_line_offset = 8;1963state->id = id;1964state->rev = device_id;1965v4l2_ctrl_handler_init(&state->hdl, 9);1966v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,1967V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);1968v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,1969V4L2_CID_CONTRAST, 0, 127, 1, 64);1970v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,1971V4L2_CID_SATURATION, 0, 127, 1, 64);1972v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,1973V4L2_CID_HUE, -128, 127, 1, 0);1974if (!is_cx2583x(state)) {1975default_volume = cx25840_read(client, 0x8d4);1976/*1977* Enforce the legacy PVR-350/MSP3400 to PVR-150/CX25843 volume1978* scale mapping limits to avoid -ERANGE errors when1979* initializing the volume control1980*/1981if (default_volume > 228) {1982/* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */1983default_volume = 228;1984cx25840_write(client, 0x8d4, 228);1985}1986else if (default_volume < 20) {1987/* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */1988default_volume = 20;1989cx25840_write(client, 0x8d4, 20);1990}1991default_volume = (((228 - default_volume) >> 1) + 23) << 9;19921993state->volume = v4l2_ctrl_new_std(&state->hdl,1994&cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,19950, 65535, 65535 / 100, default_volume);1996state->mute = v4l2_ctrl_new_std(&state->hdl,1997&cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,19980, 1, 1, 0);1999v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,2000V4L2_CID_AUDIO_BALANCE,20010, 65535, 65535 / 100, 32768);2002v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,2003V4L2_CID_AUDIO_BASS,20040, 65535, 65535 / 100, 32768);2005v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,2006V4L2_CID_AUDIO_TREBLE,20070, 65535, 65535 / 100, 32768);2008}2009sd->ctrl_handler = &state->hdl;2010if (state->hdl.error) {2011int err = state->hdl.error;20122013v4l2_ctrl_handler_free(&state->hdl);2014kfree(state);2015return err;2016}2017if (!is_cx2583x(state))2018v4l2_ctrl_cluster(2, &state->volume);2019v4l2_ctrl_handler_setup(&state->hdl);20202021if (client->dev.platform_data) {2022struct cx25840_platform_data *pdata = client->dev.platform_data;20232024state->pvr150_workaround = pdata->pvr150_workaround;2025}20262027cx25840_ir_probe(sd);2028return 0;2029}20302031static int cx25840_remove(struct i2c_client *client)2032{2033struct v4l2_subdev *sd = i2c_get_clientdata(client);2034struct cx25840_state *state = to_state(sd);20352036cx25840_ir_remove(sd);2037v4l2_device_unregister_subdev(sd);2038v4l2_ctrl_handler_free(&state->hdl);2039kfree(state);2040return 0;2041}20422043static const struct i2c_device_id cx25840_id[] = {2044{ "cx25840", 0 },2045{ }2046};2047MODULE_DEVICE_TABLE(i2c, cx25840_id);20482049static struct i2c_driver cx25840_driver = {2050.driver = {2051.owner = THIS_MODULE,2052.name = "cx25840",2053},2054.probe = cx25840_probe,2055.remove = cx25840_remove,2056.id_table = cx25840_id,2057};20582059static __init int init_cx25840(void)2060{2061return i2c_add_driver(&cx25840_driver);2062}20632064static __exit void exit_cx25840(void)2065{2066i2c_del_driver(&cx25840_driver);2067}20682069module_init(init_cx25840);2070module_exit(exit_cx25840);207120722073