Path: blob/master/drivers/media/video/cx18/cx18-i2c.c
17745 views
/*1* cx18 I2C functions2*3* Derived from ivtv-i2c.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/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (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., 59 Temple Place, Suite 330, Boston, MA21* 02111-1307 USA22*/2324#include "cx18-driver.h"25#include "cx18-io.h"26#include "cx18-cards.h"27#include "cx18-gpio.h"28#include "cx18-i2c.h"29#include "cx18-irq.h"3031#define CX18_REG_I2C_1_WR 0xf1500032#define CX18_REG_I2C_1_RD 0xf1500833#define CX18_REG_I2C_2_WR 0xf2510034#define CX18_REG_I2C_2_RD 0xf251083536#define SETSCL_BIT 0x000137#define SETSDL_BIT 0x000238#define GETSCL_BIT 0x000439#define GETSDL_BIT 0x00084041#define CX18_CS5345_I2C_ADDR 0x4c42#define CX18_Z8F0811_IR_TX_I2C_ADDR 0x7043#define CX18_Z8F0811_IR_RX_I2C_ADDR 0x714445/* This array should match the CX18_HW_ defines */46static const u8 hw_addrs[] = {470, /* CX18_HW_TUNER */480, /* CX18_HW_TVEEPROM */49CX18_CS5345_I2C_ADDR, /* CX18_HW_CS5345 */500, /* CX18_HW_DVB */510, /* CX18_HW_418_AV */520, /* CX18_HW_GPIO_MUX */530, /* CX18_HW_GPIO_RESET_CTRL */54CX18_Z8F0811_IR_TX_I2C_ADDR, /* CX18_HW_Z8F0811_IR_TX_HAUP */55CX18_Z8F0811_IR_RX_I2C_ADDR, /* CX18_HW_Z8F0811_IR_RX_HAUP */56};5758/* This array should match the CX18_HW_ defines */59/* This might well become a card-specific array */60static const u8 hw_bus[] = {611, /* CX18_HW_TUNER */620, /* CX18_HW_TVEEPROM */630, /* CX18_HW_CS5345 */640, /* CX18_HW_DVB */650, /* CX18_HW_418_AV */660, /* CX18_HW_GPIO_MUX */670, /* CX18_HW_GPIO_RESET_CTRL */680, /* CX18_HW_Z8F0811_IR_TX_HAUP */690, /* CX18_HW_Z8F0811_IR_RX_HAUP */70};7172/* This array should match the CX18_HW_ defines */73static const char * const hw_devicenames[] = {74"tuner",75"tveeprom",76"cs5345",77"cx23418_DTV",78"cx23418_AV",79"gpio_mux",80"gpio_reset_ctrl",81"ir_tx_z8f0811_haup",82"ir_rx_z8f0811_haup",83};8485static int cx18_i2c_new_ir(struct cx18 *cx, struct i2c_adapter *adap, u32 hw,86const char *type, u8 addr)87{88struct i2c_board_info info;89struct IR_i2c_init_data *init_data = &cx->ir_i2c_init_data;90unsigned short addr_list[2] = { addr, I2C_CLIENT_END };9192memset(&info, 0, sizeof(struct i2c_board_info));93strlcpy(info.type, type, I2C_NAME_SIZE);9495/* Our default information for ir-kbd-i2c.c to use */96switch (hw) {97case CX18_HW_Z8F0811_IR_RX_HAUP:98init_data->ir_codes = RC_MAP_HAUPPAUGE;99init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;100init_data->type = RC_TYPE_RC5;101init_data->name = cx->card_name;102info.platform_data = init_data;103break;104}105106return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?107-1 : 0;108}109110int cx18_i2c_register(struct cx18 *cx, unsigned idx)111{112struct v4l2_subdev *sd;113int bus = hw_bus[idx];114struct i2c_adapter *adap = &cx->i2c_adap[bus];115const char *type = hw_devicenames[idx];116u32 hw = 1 << idx;117118if (idx >= ARRAY_SIZE(hw_addrs))119return -1;120121if (hw == CX18_HW_TUNER) {122/* special tuner group handling */123sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,124adap, type, 0, cx->card_i2c->radio);125if (sd != NULL)126sd->grp_id = hw;127sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,128adap, type, 0, cx->card_i2c->demod);129if (sd != NULL)130sd->grp_id = hw;131sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,132adap, type, 0, cx->card_i2c->tv);133if (sd != NULL)134sd->grp_id = hw;135return sd != NULL ? 0 : -1;136}137138if (hw & CX18_HW_IR_ANY)139return cx18_i2c_new_ir(cx, adap, hw, type, hw_addrs[idx]);140141/* Is it not an I2C device or one we do not wish to register? */142if (!hw_addrs[idx])143return -1;144145/* It's an I2C device other than an analog tuner or IR chip */146sd = v4l2_i2c_new_subdev(&cx->v4l2_dev, adap, type, hw_addrs[idx],147NULL);148if (sd != NULL)149sd->grp_id = hw;150return sd != NULL ? 0 : -1;151}152153/* Find the first member of the subdev group id in hw */154struct v4l2_subdev *cx18_find_hw(struct cx18 *cx, u32 hw)155{156struct v4l2_subdev *result = NULL;157struct v4l2_subdev *sd;158159spin_lock(&cx->v4l2_dev.lock);160v4l2_device_for_each_subdev(sd, &cx->v4l2_dev) {161if (sd->grp_id == hw) {162result = sd;163break;164}165}166spin_unlock(&cx->v4l2_dev.lock);167return result;168}169170static void cx18_setscl(void *data, int state)171{172struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;173int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;174u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;175u32 r = cx18_read_reg(cx, addr);176177if (state)178cx18_write_reg(cx, r | SETSCL_BIT, addr);179else180cx18_write_reg(cx, r & ~SETSCL_BIT, addr);181}182183static void cx18_setsda(void *data, int state)184{185struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;186int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;187u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;188u32 r = cx18_read_reg(cx, addr);189190if (state)191cx18_write_reg(cx, r | SETSDL_BIT, addr);192else193cx18_write_reg(cx, r & ~SETSDL_BIT, addr);194}195196static int cx18_getscl(void *data)197{198struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;199int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;200u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;201202return cx18_read_reg(cx, addr) & GETSCL_BIT;203}204205static int cx18_getsda(void *data)206{207struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;208int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;209u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;210211return cx18_read_reg(cx, addr) & GETSDL_BIT;212}213214/* template for i2c-bit-algo */215static struct i2c_adapter cx18_i2c_adap_template = {216.name = "cx18 i2c driver",217.algo = NULL, /* set by i2c-algo-bit */218.algo_data = NULL, /* filled from template */219.owner = THIS_MODULE,220};221222#define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */223#define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */224225static struct i2c_algo_bit_data cx18_i2c_algo_template = {226.setsda = cx18_setsda,227.setscl = cx18_setscl,228.getsda = cx18_getsda,229.getscl = cx18_getscl,230.udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/231.timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */232};233234/* init + register i2c algo-bit adapter */235int init_cx18_i2c(struct cx18 *cx)236{237int i, err;238CX18_DEBUG_I2C("i2c init\n");239240for (i = 0; i < 2; i++) {241/* Setup algorithm for adapter */242memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,243sizeof(struct i2c_algo_bit_data));244cx->i2c_algo_cb_data[i].cx = cx;245cx->i2c_algo_cb_data[i].bus_index = i;246cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];247248/* Setup adapter */249memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,250sizeof(struct i2c_adapter));251cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];252sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),253" #%d-%d", cx->instance, i);254i2c_set_adapdata(&cx->i2c_adap[i], &cx->v4l2_dev);255cx->i2c_adap[i].dev.parent = &cx->pci_dev->dev;256}257258if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {259/* Reset/Unreset I2C hardware block */260/* Clock select 220MHz */261cx18_write_reg_expect(cx, 0x10000000, 0xc71004,2620x00000000, 0x10001000);263/* Clock Enable */264cx18_write_reg_expect(cx, 0x10001000, 0xc71024,2650x00001000, 0x10001000);266}267/* courtesy of Steven Toth <[email protected]> */268cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);269mdelay(10);270cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);271mdelay(10);272cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);273mdelay(10);274275/* Set to edge-triggered intrs. */276cx18_write_reg(cx, 0x00c00000, 0xc730c8);277/* Clear any stale intrs */278cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,279~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);280281/* Hw I2C1 Clock Freq ~100kHz */282cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);283cx18_setscl(&cx->i2c_algo_cb_data[0], 1);284cx18_setsda(&cx->i2c_algo_cb_data[0], 1);285286/* Hw I2C2 Clock Freq ~100kHz */287cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);288cx18_setscl(&cx->i2c_algo_cb_data[1], 1);289cx18_setsda(&cx->i2c_algo_cb_data[1], 1);290291cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,292core, reset, (u32) CX18_GPIO_RESET_I2C);293294err = i2c_bit_add_bus(&cx->i2c_adap[0]);295if (err)296goto err;297err = i2c_bit_add_bus(&cx->i2c_adap[1]);298if (err)299goto err_del_bus_0;300return 0;301302err_del_bus_0:303i2c_del_adapter(&cx->i2c_adap[0]);304err:305return err;306}307308void exit_cx18_i2c(struct cx18 *cx)309{310int i;311CX18_DEBUG_I2C("i2c exit\n");312cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,313CX18_REG_I2C_1_WR);314cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,315CX18_REG_I2C_2_WR);316317for (i = 0; i < 2; i++) {318i2c_del_adapter(&cx->i2c_adap[i]);319}320}321322/*323Hauppauge HVR1600 should have:32432 cx2422732598 unknown326a0 eeprom327c2 tuner328e? zilog ir329*/330331332