3 * i2c algorithm for USB-I2C Bridges
5 * Copyright (c) 1999-2005 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/version.h>
32 #include <linux/utsname.h>
33 #include <linux/init.h>
34 #include <asm/uaccess.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
38 #include <linux/usb.h>
39 #include <linux/i2c.h>
40 #include "usbvision.h"
45 static int i2c_debug = 0;
47 module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
48 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
50 #define PDEBUG(level, fmt, args...) \
51 if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
53 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
55 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
58 static inline int try_write_address(struct i2c_adapter *i2c_adap,
59 unsigned char addr, int retries)
65 data = i2c_get_adapdata(i2c_adap);
67 for (i = 0; i <= retries; i++) {
68 ret = (usbvision_i2c_write(data, addr, buf, 1));
72 if (i == retries) /* no success */
77 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
78 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
83 static inline int try_read_address(struct i2c_adapter *i2c_adap,
84 unsigned char addr, int retries)
90 data = i2c_get_adapdata(i2c_adap);
91 for (i = 0; i <= retries; i++) {
92 ret = (usbvision_i2c_read(data, addr, buf, 1));
96 if (i == retries) /* no success */
101 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
102 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
107 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
108 struct i2c_msg *msg, int retries,
111 unsigned short flags = msg->flags;
115 if ((flags & I2C_M_TEN)) {
116 /* a ten bit address */
117 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
118 /* try extended address code... */
119 ret = try_write_address(i2c_adap, addr, retries);
121 err("died at extended address code, while writing");
125 if (flags & I2C_M_RD) {
126 /* okay, now switch into reading mode */
128 ret = try_read_address(i2c_adap, addr, retries);
130 err("died at extended address code, while reading");
135 } else { /* normal 7bit address */
136 addr = (msg->addr << 1);
137 if (flags & I2C_M_RD)
139 if (flags & I2C_M_REV_DIR_ADDR)
143 if (flags & I2C_M_RD)
144 ret = try_read_address(i2c_adap, addr, retries);
146 ret = try_write_address(i2c_adap, addr, retries);
156 usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
158 struct i2c_msg *pmsg;
163 data = i2c_get_adapdata(i2c_adap);
165 for (i = 0; i < num; i++) {
167 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
169 PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
170 return (ret < 0) ? ret : -EREMOTEIO;
173 if (pmsg->flags & I2C_M_RD) {
174 /* read bytes into buffer */
175 ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len));
176 if (ret < pmsg->len) {
177 return (ret < 0) ? ret : -EREMOTEIO;
180 /* write bytes from buffer */
181 ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len));
182 if (ret < pmsg->len) {
183 return (ret < 0) ? ret : -EREMOTEIO;
190 static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
195 static u32 usb_func(struct i2c_adapter *adap)
197 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
201 /* -----exported algorithm data: ------------------------------------- */
203 static struct i2c_algorithm i2c_usb_algo = {
204 .master_xfer = usb_xfer,
206 .algo_control = algo_control,
207 .functionality = usb_func,
212 * registering functions to load algorithms at runtime
214 static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
216 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
217 PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]");
219 /* register new adapter to i2c module... */
221 adap->algo = &i2c_usb_algo;
223 adap->timeout = 100; /* default values, should */
224 adap->retries = 3; /* be replaced by defines */
226 i2c_add_adapter(adap);
228 PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
234 int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
237 i2c_del_adapter(adap);
239 PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
245 /* ----------------------------------------------------------------------- */
246 /* usbvision specific I2C functions */
247 /* ----------------------------------------------------------------------- */
248 static struct i2c_adapter i2c_adap_template;
249 static struct i2c_client i2c_client_template;
251 int usbvision_init_i2c(struct usb_usbvision *usbvision)
253 memcpy(&usbvision->i2c_adap, &i2c_adap_template,
254 sizeof(struct i2c_adapter));
255 memcpy(&usbvision->i2c_client, &i2c_client_template,
256 sizeof(struct i2c_client));
258 sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
259 " #%d", usbvision->vdev->minor & 0x1f);
260 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
262 i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
263 i2c_set_clientdata(&usbvision->i2c_client, usbvision);
265 usbvision->i2c_client.adapter = &usbvision->i2c_adap;
267 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
268 printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
272 #ifdef CONFIG_MODULES
273 /* Request the load of the i2c modules we need */
274 switch (usbvision_device_data[usbvision->DevModel].Codec) {
276 request_module("saa7115");
279 request_module("saa7115");
282 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
283 request_module("tuner");
287 return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
290 void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
293 i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
296 static int attach_inform(struct i2c_client *client)
298 struct usb_usbvision *usbvision;
300 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
302 switch (client->addr << 1) {
306 struct tuner_setup tun_setup;
308 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
309 tun_setup.type = TUNER_TDA9887;
310 tun_setup.addr = client->addr;
312 call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
317 PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
320 PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
323 PDEBUG(DBG_I2C,"attach_inform: saa7111 detected.");
326 PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
331 struct tuner_setup tun_setup;
333 PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
334 usbvision->tuner_addr = client->addr;
336 if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
337 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
338 tun_setup.type = usbvision->tuner_type;
339 tun_setup.addr = usbvision->tuner_addr;
340 call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
348 static int detach_inform(struct i2c_client *client)
350 struct usb_usbvision *usbvision;
352 usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
354 PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
359 usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
360 char *buf, short len)
364 for (retries = 5;;) {
365 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
369 /* Initiate byte read cycle */
370 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
372 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
373 (len & 0x07) | 0x18);
377 /* Test for Busy and ACK */
379 /* USBVISION_SER_CONT -> d4 == 0 busy */
380 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
381 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
385 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
386 if ((rc & 0x20) == 0) /* Ack? */
390 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
400 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
402 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
404 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
406 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
410 "usbvision_i2c_read_max4: buffer length > 4\n");
413 if (i2c_debug & DBG_I2C) {
415 for (idx = 0; idx < len; idx++) {
416 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
423 static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
424 unsigned char addr, const char *buf,
429 unsigned char value[6];
430 unsigned char ser_cont;
432 ser_cont = (len & 0x07) | 0x10;
436 for (i = 0; i < len; i++)
437 value[i + 2] = buf[i];
439 for (retries = 5;;) {
440 rc = usb_control_msg(usbvision->dev,
441 usb_sndctrlpipe(usbvision->dev, 1),
443 USB_DIR_OUT | USB_TYPE_VENDOR |
444 USB_RECIP_ENDPOINT, 0,
445 (__u16) USBVISION_SER_ADRS, value,
451 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
452 (len & 0x07) | 0x10);
456 /* Test for Busy and ACK */
458 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
459 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
463 if ((rc & 0x20) == 0) /* Ack? */
467 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
474 if (i2c_debug & DBG_I2C) {
476 for (idx = 0; idx < len; idx++) {
477 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
483 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
491 struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
494 count = (len > maxLen) ? maxLen : len;
495 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
501 return (retval < 0) ? retval : -EFAULT;
506 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
513 struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
516 count = (len > 3) ? 4 : len;
517 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
519 for (i = 0; i < len; i++)
520 buf[rdcount + i] = temp[i];
524 return (retval < 0) ? retval : -EFAULT;
529 static struct i2c_adapter i2c_adap_template = {
530 .owner = THIS_MODULE,
532 .id = I2C_HW_B_BT848, /* FIXME */
533 .client_register = attach_inform,
534 .client_unregister = detach_inform,
535 #ifdef I2C_ADAP_CLASS_TV_ANALOG
536 .class = I2C_ADAP_CLASS_TV_ANALOG,
538 .class = I2C_CLASS_TV_ANALOG,
542 static struct i2c_client i2c_client_template = {
543 .name = "usbvision internal",
547 * Overrides for Emacs so that we follow Linus's tabbing style.
548 * ---------------------------------------------------------------------------