3 * i2c algorithm for USB-I2C Bridges
5 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <asm/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/errno.h>
35 #include <linux/usb.h>
36 #include <linux/i2c.h>
37 #include "usbvision.h"
43 module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
44 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
46 #define PDEBUG(level, fmt, args...) { \
47 if (i2c_debug & (level)) \
48 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
49 __func__, __LINE__ , ## args); \
52 static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
54 static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
57 static inline int try_write_address(struct i2c_adapter *i2c_adap,
58 unsigned char addr, int retries)
60 struct usb_usbvision *usbvision;
64 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
66 for (i = 0; i <= retries; i++) {
67 ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
71 if (i == retries) /* no success */
76 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
77 PDEBUG(DBG_I2C,"Maybe there's no device at this address");
82 static inline int try_read_address(struct i2c_adapter *i2c_adap,
83 unsigned char addr, int retries)
85 struct usb_usbvision *usbvision;
89 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
90 for (i = 0; i <= retries; i++) {
91 ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
95 if (i == retries) /* no success */
100 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
101 PDEBUG(DBG_I2C,"Maybe there's no device at this address");
106 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
107 struct i2c_msg *msg, int retries,
110 unsigned short flags = msg->flags;
114 if ((flags & I2C_M_TEN)) {
115 /* a ten bit address */
116 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
117 /* try extended address code... */
118 ret = try_write_address(i2c_adap, addr, retries);
120 dev_err(&i2c_adap->dev,
121 "died at extended address code, while writing\n");
125 if (flags & I2C_M_RD) {
126 /* okay, now switch into reading mode */
128 ret = try_read_address(i2c_adap, addr, retries);
130 dev_err(&i2c_adap->dev,
131 "died at extended address code, while reading\n");
136 } else { /* normal 7bit address */
137 addr = (msg->addr << 1);
138 if (flags & I2C_M_RD)
142 if (flags & I2C_M_RD)
143 ret = try_read_address(i2c_adap, addr, retries);
145 ret = try_write_address(i2c_adap, addr, retries);
155 usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
157 struct i2c_msg *pmsg;
158 struct usb_usbvision *usbvision;
160 unsigned char addr = 0;
162 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
164 for (i = 0; i < num; i++) {
166 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
168 PDEBUG(DBG_I2C,"got NAK from device, message #%d", i);
169 return (ret < 0) ? ret : -EREMOTEIO;
172 if (pmsg->flags & I2C_M_RD) {
173 /* read bytes into buffer */
174 ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
175 if (ret < pmsg->len) {
176 return (ret < 0) ? ret : -EREMOTEIO;
179 /* write bytes from buffer */
180 ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
181 if (ret < pmsg->len) {
182 return (ret < 0) ? ret : -EREMOTEIO;
189 static u32 functionality(struct i2c_adapter *adap)
191 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
194 /* -----exported algorithm data: ------------------------------------- */
196 static struct i2c_algorithm usbvision_algo = {
197 .master_xfer = usbvision_i2c_xfer,
199 .functionality = functionality,
203 /* ----------------------------------------------------------------------- */
204 /* usbvision specific I2C functions */
205 /* ----------------------------------------------------------------------- */
206 static struct i2c_adapter i2c_adap_template;
208 int usbvision_i2c_register(struct usb_usbvision *usbvision)
210 static unsigned short saa711x_addrs[] = {
211 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
212 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
215 memcpy(&usbvision->i2c_adap, &i2c_adap_template,
216 sizeof(struct i2c_adapter));
218 sprintf(usbvision->i2c_adap.name, "%s-%d-%s", i2c_adap_template.name,
219 usbvision->dev->bus->busnum, usbvision->dev->devpath);
220 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
221 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
223 i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
225 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
226 printk(KERN_ERR "usbvision_register: can't write reg\n");
230 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
231 PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
233 /* register new adapter to i2c module... */
235 usbvision->i2c_adap.algo = &usbvision_algo;
237 usbvision->i2c_adap.timeout = 100; /* default values, should */
238 usbvision->i2c_adap.retries = 3; /* be replaced by defines */
240 i2c_add_adapter(&usbvision->i2c_adap);
242 PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
244 /* Request the load of the i2c modules we need */
245 switch (usbvision_device_data[usbvision->DevModel].Codec) {
248 v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
249 &usbvision->i2c_adap, "saa7115",
250 "saa7115_auto", 0, saa711x_addrs);
253 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
254 struct v4l2_subdev *sd;
255 enum v4l2_i2c_tuner_type type;
256 struct tuner_setup tun_setup;
258 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
259 &usbvision->i2c_adap, "tuner",
260 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
261 /* depending on whether we found a demod or not, select
263 type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
265 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
266 &usbvision->i2c_adap, "tuner",
267 "tuner", 0, v4l2_i2c_tuner_addrs(type));
269 if (usbvision->tuner_type != -1) {
270 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
271 tun_setup.type = usbvision->tuner_type;
272 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
273 call_all(usbvision, tuner, s_type_addr, &tun_setup);
280 int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
283 i2c_del_adapter(&(usbvision->i2c_adap));
285 PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
291 usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
292 char *buf, short len)
296 for (retries = 5;;) {
297 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
301 /* Initiate byte read cycle */
302 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
304 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
305 (len & 0x07) | 0x18);
309 /* Test for Busy and ACK */
311 /* USBVISION_SER_CONT -> d4 == 0 busy */
312 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
313 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
317 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
318 if ((rc & 0x20) == 0) /* Ack? */
322 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
332 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
334 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
336 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
338 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
342 "usbvision_i2c_read_max4: buffer length > 4\n");
345 if (i2c_debug & DBG_I2C) {
347 for (idx = 0; idx < len; idx++) {
348 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
355 static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
356 unsigned char addr, const char *buf,
361 unsigned char value[6];
362 unsigned char ser_cont;
364 ser_cont = (len & 0x07) | 0x10;
368 for (i = 0; i < len; i++)
369 value[i + 2] = buf[i];
371 for (retries = 5;;) {
372 rc = usb_control_msg(usbvision->dev,
373 usb_sndctrlpipe(usbvision->dev, 1),
375 USB_DIR_OUT | USB_TYPE_VENDOR |
376 USB_RECIP_ENDPOINT, 0,
377 (__u16) USBVISION_SER_ADRS, value,
383 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
384 (len & 0x07) | 0x10);
388 /* Test for Busy and ACK */
390 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
391 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
395 if ((rc & 0x20) == 0) /* Ack? */
399 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
406 if (i2c_debug & DBG_I2C) {
408 for (idx = 0; idx < len; idx++) {
409 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
415 static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
425 count = (len > maxLen) ? maxLen : len;
426 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
432 return (retval < 0) ? retval : -EFAULT;
437 static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
446 count = (len > 3) ? 4 : len;
447 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
449 for (i = 0; i < len; i++)
450 buf[rdcount + i] = temp[i];
454 return (retval < 0) ? retval : -EFAULT;
459 static struct i2c_adapter i2c_adap_template = {
460 .owner = THIS_MODULE,
465 * Overrides for Emacs so that we follow Linus's tabbing style.
466 * ---------------------------------------------------------------------------