Path: blob/master/drivers/media/video/cx231xx/cx231xx-i2c.c
17956 views
/*1cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices23Copyright (C) 2008 <srinivasa.deevi at conexant dot com>4Based on em28xx driver5Based on Cx23885 driver67This program is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2 of the License, or10(at your option) any later version.1112This program is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with this program; if not, write to the Free Software19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.20*/2122#include <linux/module.h>23#include <linux/kernel.h>24#include <linux/usb.h>25#include <linux/i2c.h>26#include <media/v4l2-common.h>27#include <media/tuner.h>2829#include "cx231xx.h"3031/* ----------------------------------------------------------- */3233static unsigned int i2c_scan;34module_param(i2c_scan, int, 0444);35MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");3637static unsigned int i2c_debug;38module_param(i2c_debug, int, 0644);39MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");4041#define dprintk1(lvl, fmt, args...) \42do { \43if (i2c_debug >= lvl) { \44printk(fmt, ##args); \45} \46} while (0)4748#define dprintk2(lvl, fmt, args...) \49do { \50if (i2c_debug >= lvl) { \51printk(KERN_DEBUG "%s at %s: " fmt, \52dev->name, __func__ , ##args); \53} \54} while (0)5556static inline bool is_tuner(struct cx231xx *dev, struct cx231xx_i2c *bus,57const struct i2c_msg *msg, int tuner_type)58{59if (bus->nr != dev->board.tuner_i2c_master)60return false;6162if (msg->addr != dev->board.tuner_addr)63return false;6465if (dev->tuner_type != tuner_type)66return false;6768return true;69}7071/*72* cx231xx_i2c_send_bytes()73*/74int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap,75const struct i2c_msg *msg)76{77struct cx231xx_i2c *bus = i2c_adap->algo_data;78struct cx231xx *dev = bus->dev;79struct cx231xx_i2c_xfer_data req_data;80int status = 0;81u16 size = 0;82u8 loop = 0;83u8 saddr_len = 1;84u8 *buf_ptr = NULL;85u16 saddr = 0;86u8 need_gpio = 0;8788if (is_tuner(dev, bus, msg, TUNER_XC5000)) {89size = msg->len;9091if (size == 2) { /* register write sub addr */92/* Just writing sub address will cause problem93* to XC5000. So ignore the request */94return 0;95} else if (size == 4) { /* register write with sub addr */96if (msg->len >= 2)97saddr = msg->buf[0] << 8 | msg->buf[1];98else if (msg->len == 1)99saddr = msg->buf[0];100101switch (saddr) {102case 0x0000: /* start tuner calibration mode */103need_gpio = 1;104/* FW Loading is done */105dev->xc_fw_load_done = 1;106break;107case 0x000D: /* Set signal source */108case 0x0001: /* Set TV standard - Video */109case 0x0002: /* Set TV standard - Audio */110case 0x0003: /* Set RF Frequency */111need_gpio = 1;112break;113default:114if (dev->xc_fw_load_done)115need_gpio = 1;116break;117}118119if (need_gpio) {120dprintk1(1,121"GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n",122msg->addr, msg->len, saddr);123124return dev->cx231xx_gpio_i2c_write(dev,125msg->addr,126msg->buf,127msg->len);128}129}130131/* special case for Xc5000 tuner case */132saddr_len = 1;133134/* adjust the length to correct length */135size -= saddr_len;136buf_ptr = (u8 *) (msg->buf + 1);137138do {139/* prepare xfer_data struct */140req_data.dev_addr = msg->addr;141req_data.direction = msg->flags;142req_data.saddr_len = saddr_len;143req_data.saddr_dat = msg->buf[0];144req_data.buf_size = size > 16 ? 16 : size;145req_data.p_buffer = (u8 *) (buf_ptr + loop * 16);146147bus->i2c_nostop = (size > 16) ? 1 : 0;148bus->i2c_reserve = (loop == 0) ? 0 : 1;149150/* usb send command */151status = dev->cx231xx_send_usb_command(bus, &req_data);152loop++;153154if (size >= 16)155size -= 16;156else157size = 0;158159} while (size > 0);160161bus->i2c_nostop = 0;162bus->i2c_reserve = 0;163164} else { /* regular case */165166/* prepare xfer_data struct */167req_data.dev_addr = msg->addr;168req_data.direction = msg->flags;169req_data.saddr_len = 0;170req_data.saddr_dat = 0;171req_data.buf_size = msg->len;172req_data.p_buffer = msg->buf;173174/* usb send command */175status = dev->cx231xx_send_usb_command(bus, &req_data);176}177178return status < 0 ? status : 0;179}180181/*182* cx231xx_i2c_recv_bytes()183* read a byte from the i2c device184*/185static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap,186const struct i2c_msg *msg)187{188struct cx231xx_i2c *bus = i2c_adap->algo_data;189struct cx231xx *dev = bus->dev;190struct cx231xx_i2c_xfer_data req_data;191int status = 0;192u16 saddr = 0;193u8 need_gpio = 0;194195if (is_tuner(dev, bus, msg, TUNER_XC5000)) {196if (msg->len == 2)197saddr = msg->buf[0] << 8 | msg->buf[1];198else if (msg->len == 1)199saddr = msg->buf[0];200201if (dev->xc_fw_load_done) {202203switch (saddr) {204case 0x0009: /* BUSY check */205dprintk1(1,206"GPIO R E A D: Special case BUSY check \n");207/*Try read BUSY register, just set it to zero*/208msg->buf[0] = 0;209if (msg->len == 2)210msg->buf[1] = 0;211return 0;212case 0x0004: /* read Lock status */213need_gpio = 1;214break;215216}217218if (need_gpio) {219/* this is a special case to handle Xceive tuner220clock stretch issue with gpio based I2C */221222dprintk1(1,223"GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n",224msg->addr, msg->len,225msg->buf[0] << 8 | msg->buf[1]);226227status =228dev->cx231xx_gpio_i2c_write(dev, msg->addr,229msg->buf,230msg->len);231status =232dev->cx231xx_gpio_i2c_read(dev, msg->addr,233msg->buf,234msg->len);235return status;236}237}238239/* prepare xfer_data struct */240req_data.dev_addr = msg->addr;241req_data.direction = msg->flags;242req_data.saddr_len = msg->len;243req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1];244req_data.buf_size = msg->len;245req_data.p_buffer = msg->buf;246247/* usb send command */248status = dev->cx231xx_send_usb_command(bus, &req_data);249250} else {251252/* prepare xfer_data struct */253req_data.dev_addr = msg->addr;254req_data.direction = msg->flags;255req_data.saddr_len = 0;256req_data.saddr_dat = 0;257req_data.buf_size = msg->len;258req_data.p_buffer = msg->buf;259260/* usb send command */261status = dev->cx231xx_send_usb_command(bus, &req_data);262}263264return status < 0 ? status : 0;265}266267/*268* cx231xx_i2c_recv_bytes_with_saddr()269* read a byte from the i2c device270*/271static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap,272const struct i2c_msg *msg1,273const struct i2c_msg *msg2)274{275struct cx231xx_i2c *bus = i2c_adap->algo_data;276struct cx231xx *dev = bus->dev;277struct cx231xx_i2c_xfer_data req_data;278int status = 0;279u16 saddr = 0;280u8 need_gpio = 0;281282if (msg1->len == 2)283saddr = msg1->buf[0] << 8 | msg1->buf[1];284else if (msg1->len == 1)285saddr = msg1->buf[0];286287if (is_tuner(dev, bus, msg2, TUNER_XC5000)) {288if ((msg2->len < 16)) {289290dprintk1(1,291"i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n",292msg2->addr, msg2->len, saddr, msg1->len);293294switch (saddr) {295case 0x0008: /* read FW load status */296need_gpio = 1;297break;298case 0x0004: /* read Lock status */299need_gpio = 1;300break;301}302303if (need_gpio) {304status =305dev->cx231xx_gpio_i2c_write(dev, msg1->addr,306msg1->buf,307msg1->len);308status =309dev->cx231xx_gpio_i2c_read(dev, msg2->addr,310msg2->buf,311msg2->len);312return status;313}314}315}316317/* prepare xfer_data struct */318req_data.dev_addr = msg2->addr;319req_data.direction = msg2->flags;320req_data.saddr_len = msg1->len;321req_data.saddr_dat = saddr;322req_data.buf_size = msg2->len;323req_data.p_buffer = msg2->buf;324325/* usb send command */326status = dev->cx231xx_send_usb_command(bus, &req_data);327328return status < 0 ? status : 0;329}330331/*332* cx231xx_i2c_check_for_device()333* check if there is a i2c_device at the supplied address334*/335static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap,336const struct i2c_msg *msg)337{338struct cx231xx_i2c *bus = i2c_adap->algo_data;339struct cx231xx *dev = bus->dev;340struct cx231xx_i2c_xfer_data req_data;341int status = 0;342343/* prepare xfer_data struct */344req_data.dev_addr = msg->addr;345req_data.direction = msg->flags;346req_data.saddr_len = 0;347req_data.saddr_dat = 0;348req_data.buf_size = 0;349req_data.p_buffer = NULL;350351/* usb send command */352status = dev->cx231xx_send_usb_command(bus, &req_data);353354return status < 0 ? status : 0;355}356357/*358* cx231xx_i2c_xfer()359* the main i2c transfer function360*/361static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap,362struct i2c_msg msgs[], int num)363{364struct cx231xx_i2c *bus = i2c_adap->algo_data;365struct cx231xx *dev = bus->dev;366int addr, rc, i, byte;367368if (num <= 0)369return 0;370mutex_lock(&dev->i2c_lock);371for (i = 0; i < num; i++) {372373addr = msgs[i].addr >> 1;374375dprintk2(2, "%s %s addr=%x len=%d:",376(msgs[i].flags & I2C_M_RD) ? "read" : "write",377i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);378if (!msgs[i].len) {379/* no len: check only for device presence */380rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]);381if (rc < 0) {382dprintk2(2, " no device\n");383mutex_unlock(&dev->i2c_lock);384return rc;385}386387} else if (msgs[i].flags & I2C_M_RD) {388/* read bytes */389rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]);390if (i2c_debug >= 2) {391for (byte = 0; byte < msgs[i].len; byte++)392printk(" %02x", msgs[i].buf[byte]);393}394} else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&395msgs[i].addr == msgs[i + 1].addr396&& (msgs[i].len <= 2) && (bus->nr < 3)) {397/* read bytes */398rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap,399&msgs[i],400&msgs[i + 1]);401if (i2c_debug >= 2) {402for (byte = 0; byte < msgs[i].len; byte++)403printk(" %02x", msgs[i].buf[byte]);404}405i++;406} else {407/* write bytes */408if (i2c_debug >= 2) {409for (byte = 0; byte < msgs[i].len; byte++)410printk(" %02x", msgs[i].buf[byte]);411}412rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]);413}414if (rc < 0)415goto err;416if (i2c_debug >= 2)417printk("\n");418}419mutex_unlock(&dev->i2c_lock);420return num;421err:422dprintk2(2, " ERROR: %i\n", rc);423mutex_unlock(&dev->i2c_lock);424return rc;425}426427/* ----------------------------------------------------------- */428429/*430* functionality()431*/432static u32 functionality(struct i2c_adapter *adap)433{434return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;435}436437static struct i2c_algorithm cx231xx_algo = {438.master_xfer = cx231xx_i2c_xfer,439.functionality = functionality,440};441442static struct i2c_adapter cx231xx_adap_template = {443.owner = THIS_MODULE,444.name = "cx231xx",445.algo = &cx231xx_algo,446};447448static struct i2c_client cx231xx_client_template = {449.name = "cx231xx internal",450};451452/* ----------------------------------------------------------- */453454/*455* i2c_devs456* incomplete list of known devices457*/458static char *i2c_devs[128] = {459[0x60 >> 1] = "colibri",460[0x88 >> 1] = "hammerhead",461[0x8e >> 1] = "CIR",462[0x32 >> 1] = "GeminiIII",463[0x02 >> 1] = "Aquarius",464[0xa0 >> 1] = "eeprom",465[0xc0 >> 1] = "tuner",466[0xc2 >> 1] = "tuner",467};468469/*470* cx231xx_do_i2c_scan()471* check i2c address range for devices472*/473void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c)474{475unsigned char buf;476int i, rc;477478cx231xx_info(": Checking for I2C devices ..\n");479for (i = 0; i < 128; i++) {480c->addr = i;481rc = i2c_master_recv(c, &buf, 0);482if (rc < 0)483continue;484cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n",485dev->name, i << 1,486i2c_devs[i] ? i2c_devs[i] : "???");487}488cx231xx_info(": Completed Checking for I2C devices.\n");489}490491/*492* cx231xx_i2c_register()493* register i2c bus494*/495int cx231xx_i2c_register(struct cx231xx_i2c *bus)496{497struct cx231xx *dev = bus->dev;498499BUG_ON(!dev->cx231xx_send_usb_command);500501memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap));502memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo));503memcpy(&bus->i2c_client, &cx231xx_client_template,504sizeof(bus->i2c_client));505506bus->i2c_adap.dev.parent = &dev->udev->dev;507508strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));509510bus->i2c_algo.data = bus;511bus->i2c_adap.algo_data = bus;512i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);513i2c_add_adapter(&bus->i2c_adap);514515bus->i2c_client.adapter = &bus->i2c_adap;516517if (0 == bus->i2c_rc) {518if (i2c_scan)519cx231xx_do_i2c_scan(dev, &bus->i2c_client);520} else521cx231xx_warn("%s: i2c bus %d register FAILED\n",522dev->name, bus->nr);523524return bus->i2c_rc;525}526527/*528* cx231xx_i2c_unregister()529* unregister i2c_bus530*/531int cx231xx_i2c_unregister(struct cx231xx_i2c *bus)532{533i2c_del_adapter(&bus->i2c_adap);534return 0;535}536537538