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/usb.h>
38 #include <linux/i2c.h>
39 #include "usbvision.h"
44 static int i2c_debug = 0;
46 module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
47 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
49 #define PDEBUG(level, fmt, args...) \
50 if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
52 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
54 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
57 static inline int try_write_address(struct i2c_adapter *i2c_adap,
58 unsigned char addr, int retries)
64 data = i2c_get_adapdata(i2c_adap);
66 for (i = 0; i <= retries; i++) {
67 ret = (usbvision_i2c_write(data, addr, buf, 1));
71 if (i == retries) /* no success */
76 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
77 PDEBUG(DBG_ALGO,"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)
89 data = i2c_get_adapdata(i2c_adap);
90 for (i = 0; i <= retries; i++) {
91 ret = (usbvision_i2c_read(data, addr, buf, 1));
95 if (i == retries) /* no success */
100 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
101 PDEBUG(DBG_ALGO,"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 err("died at extended address code, while writing");
124 if (flags & I2C_M_RD) {
125 /* okay, now switch into reading mode */
127 ret = try_read_address(i2c_adap, addr, retries);
129 err("died at extended address code, while reading");
134 } else { /* normal 7bit address */
135 addr = (msg->addr << 1);
136 if (flags & I2C_M_RD)
138 if (flags & I2C_M_REV_DIR_ADDR)
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 usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
157 struct i2c_msg *pmsg;
162 data = 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_ALGO,"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(data, 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(data, addr, pmsg->buf, pmsg->len));
181 if (ret < pmsg->len) {
182 return (ret < 0) ? ret : -EREMOTEIO;
189 static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
194 static u32 usb_func(struct i2c_adapter *adap)
196 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
200 /* -----exported algorithm data: ------------------------------------- */
202 static struct i2c_algorithm i2c_usb_algo = {
203 .master_xfer = usb_xfer,
205 .algo_control = algo_control,
206 .functionality = usb_func,
211 * registering functions to load algorithms at runtime
213 static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
215 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
216 PDEBUG(DBG_ALGO, "ALGO debugging is enabled [i2c]");
218 /* register new adapter to i2c module... */
220 adap->algo = &i2c_usb_algo;
222 adap->timeout = 100; /* default values, should */
223 adap->retries = 3; /* be replaced by defines */
225 i2c_add_adapter(adap);
227 PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
233 int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
236 i2c_del_adapter(adap);
238 PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
244 /* ----------------------------------------------------------------------- */
245 /* usbvision specific I2C functions */
246 /* ----------------------------------------------------------------------- */
247 static struct i2c_adapter i2c_adap_template;
248 static struct i2c_client i2c_client_template;
250 int usbvision_init_i2c(struct usb_usbvision *usbvision)
252 memcpy(&usbvision->i2c_adap, &i2c_adap_template,
253 sizeof(struct i2c_adapter));
254 memcpy(&usbvision->i2c_client, &i2c_client_template,
255 sizeof(struct i2c_client));
257 sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
258 " #%d", usbvision->vdev->minor & 0x1f);
259 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
260 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
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 * ---------------------------------------------------------------------------