xref: /openbmc/linux/drivers/media/usb/em28xx/em28xx-i2c.c (revision eaf33c404cd60ba4b442324766abbb5da8c94381)
10c0d06caSMauro Carvalho Chehab /*
20c0d06caSMauro Carvalho Chehab    em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
30c0d06caSMauro Carvalho Chehab 
40c0d06caSMauro Carvalho Chehab    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
50c0d06caSMauro Carvalho Chehab 		      Markus Rechberger <mrechberger@gmail.com>
60c0d06caSMauro Carvalho Chehab 		      Mauro Carvalho Chehab <mchehab@infradead.org>
70c0d06caSMauro Carvalho Chehab 		      Sascha Sommer <saschasommer@freenet.de>
80c0d06caSMauro Carvalho Chehab 
90c0d06caSMauro Carvalho Chehab    This program is free software; you can redistribute it and/or modify
100c0d06caSMauro Carvalho Chehab    it under the terms of the GNU General Public License as published by
110c0d06caSMauro Carvalho Chehab    the Free Software Foundation; either version 2 of the License, or
120c0d06caSMauro Carvalho Chehab    (at your option) any later version.
130c0d06caSMauro Carvalho Chehab 
140c0d06caSMauro Carvalho Chehab    This program is distributed in the hope that it will be useful,
150c0d06caSMauro Carvalho Chehab    but WITHOUT ANY WARRANTY; without even the implied warranty of
160c0d06caSMauro Carvalho Chehab    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
170c0d06caSMauro Carvalho Chehab    GNU General Public License for more details.
180c0d06caSMauro Carvalho Chehab 
190c0d06caSMauro Carvalho Chehab    You should have received a copy of the GNU General Public License
200c0d06caSMauro Carvalho Chehab    along with this program; if not, write to the Free Software
210c0d06caSMauro Carvalho Chehab    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
220c0d06caSMauro Carvalho Chehab  */
230c0d06caSMauro Carvalho Chehab 
240c0d06caSMauro Carvalho Chehab #include <linux/module.h>
250c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
260c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
270c0d06caSMauro Carvalho Chehab #include <linux/i2c.h>
280c0d06caSMauro Carvalho Chehab 
290c0d06caSMauro Carvalho Chehab #include "em28xx.h"
300c0d06caSMauro Carvalho Chehab #include "tuner-xc2028.h"
310c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
320c0d06caSMauro Carvalho Chehab #include <media/tuner.h>
330c0d06caSMauro Carvalho Chehab 
340c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
350c0d06caSMauro Carvalho Chehab 
360c0d06caSMauro Carvalho Chehab static unsigned int i2c_scan;
370c0d06caSMauro Carvalho Chehab module_param(i2c_scan, int, 0444);
380c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
390c0d06caSMauro Carvalho Chehab 
400c0d06caSMauro Carvalho Chehab static unsigned int i2c_debug;
410c0d06caSMauro Carvalho Chehab module_param(i2c_debug, int, 0644);
420c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
430c0d06caSMauro Carvalho Chehab 
440c0d06caSMauro Carvalho Chehab #define dprintk2(lvl, fmt, args...)			\
450c0d06caSMauro Carvalho Chehab do {							\
460c0d06caSMauro Carvalho Chehab 	if (i2c_debug >= lvl) {				\
470c0d06caSMauro Carvalho Chehab 		printk(KERN_DEBUG "%s at %s: " fmt,	\
480c0d06caSMauro Carvalho Chehab 		       dev->name, __func__ , ##args);	\
490c0d06caSMauro Carvalho Chehab       } 						\
500c0d06caSMauro Carvalho Chehab } while (0)
510c0d06caSMauro Carvalho Chehab 
520c0d06caSMauro Carvalho Chehab /*
53f5ae371aSFrank Schaefer  * em2800_i2c_send_bytes()
54f5ae371aSFrank Schaefer  * send up to 4 bytes to the em2800 i2c device
550c0d06caSMauro Carvalho Chehab  */
56f5ae371aSFrank Schaefer static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
570c0d06caSMauro Carvalho Chehab {
580c0d06caSMauro Carvalho Chehab 	int ret;
590c0d06caSMauro Carvalho Chehab 	int write_timeout;
60a6bad040SFrank Schaefer 	u8 b2[6];
61f5ae371aSFrank Schaefer 
62f5ae371aSFrank Schaefer 	if (len < 1 || len > 4)
63f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
64f5ae371aSFrank Schaefer 
650c0d06caSMauro Carvalho Chehab 	BUG_ON(len < 1 || len > 4);
660c0d06caSMauro Carvalho Chehab 	b2[5] = 0x80 + len - 1;
670c0d06caSMauro Carvalho Chehab 	b2[4] = addr;
680c0d06caSMauro Carvalho Chehab 	b2[3] = buf[0];
690c0d06caSMauro Carvalho Chehab 	if (len > 1)
700c0d06caSMauro Carvalho Chehab 		b2[2] = buf[1];
710c0d06caSMauro Carvalho Chehab 	if (len > 2)
720c0d06caSMauro Carvalho Chehab 		b2[1] = buf[2];
730c0d06caSMauro Carvalho Chehab 	if (len > 3)
740c0d06caSMauro Carvalho Chehab 		b2[0] = buf[3];
750c0d06caSMauro Carvalho Chehab 
762fcc82d8SFrank Schaefer 	/* trigger write */
770c0d06caSMauro Carvalho Chehab 	ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
780c0d06caSMauro Carvalho Chehab 	if (ret != 2 + len) {
790c0d06caSMauro Carvalho Chehab 		em28xx_warn("writing to i2c device failed (error=%i)\n", ret);
800c0d06caSMauro Carvalho Chehab 		return -EIO;
810c0d06caSMauro Carvalho Chehab 	}
822fcc82d8SFrank Schaefer 	/* wait for completion */
832fcc82d8SFrank Schaefer 	for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
840c0d06caSMauro Carvalho Chehab 	     write_timeout -= 5) {
850c0d06caSMauro Carvalho Chehab 		ret = dev->em28xx_read_reg(dev, 0x05);
860c0d06caSMauro Carvalho Chehab 		if (ret == 0x80 + len - 1)
870c0d06caSMauro Carvalho Chehab 			return len;
880c0d06caSMauro Carvalho Chehab 		msleep(5);
890c0d06caSMauro Carvalho Chehab 	}
900c0d06caSMauro Carvalho Chehab 	em28xx_warn("i2c write timed out\n");
910c0d06caSMauro Carvalho Chehab 	return -EIO;
920c0d06caSMauro Carvalho Chehab }
930c0d06caSMauro Carvalho Chehab 
940c0d06caSMauro Carvalho Chehab /*
950c0d06caSMauro Carvalho Chehab  * em2800_i2c_recv_bytes()
962fcc82d8SFrank Schaefer  * read up to 4 bytes from the em2800 i2c device
970c0d06caSMauro Carvalho Chehab  */
98a6bad040SFrank Schaefer static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
990c0d06caSMauro Carvalho Chehab {
1002fcc82d8SFrank Schaefer 	u8 buf2[4];
1010c0d06caSMauro Carvalho Chehab 	int ret;
1022fcc82d8SFrank Schaefer 	int read_timeout;
1032fcc82d8SFrank Schaefer 	int i;
104f5ae371aSFrank Schaefer 
105f5ae371aSFrank Schaefer 	if (len < 1 || len > 4)
106f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
107f5ae371aSFrank Schaefer 
1082fcc82d8SFrank Schaefer 	/* trigger read */
1092fcc82d8SFrank Schaefer 	buf2[1] = 0x84 + len - 1;
1102fcc82d8SFrank Schaefer 	buf2[0] = addr;
1112fcc82d8SFrank Schaefer 	ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
1122fcc82d8SFrank Schaefer 	if (ret != 2) {
1132fcc82d8SFrank Schaefer 		em28xx_warn("failed to trigger read from i2c address 0x%x "
1142fcc82d8SFrank Schaefer 			    "(error=%i)\n", addr, ret);
1152fcc82d8SFrank Schaefer 		return (ret < 0) ? ret : -EIO;
1162fcc82d8SFrank Schaefer 	}
1172fcc82d8SFrank Schaefer 
1182fcc82d8SFrank Schaefer 	/* wait for completion */
1192fcc82d8SFrank Schaefer 	for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
1202fcc82d8SFrank Schaefer 	     read_timeout -= 5) {
1212fcc82d8SFrank Schaefer 		ret = dev->em28xx_read_reg(dev, 0x05);
1222fcc82d8SFrank Schaefer 		if (ret == 0x84 + len - 1) {
1232fcc82d8SFrank Schaefer 			break;
1242fcc82d8SFrank Schaefer 		} else if (ret == 0x94 + len - 1) {
1252fcc82d8SFrank Schaefer 			return -ENODEV;
1262fcc82d8SFrank Schaefer 		} else if (ret < 0) {
1272fcc82d8SFrank Schaefer 			em28xx_warn("failed to get i2c transfer status from "
1282fcc82d8SFrank Schaefer 				    "bridge register (error=%i)\n", ret);
1290c0d06caSMauro Carvalho Chehab 			return ret;
1300c0d06caSMauro Carvalho Chehab 		}
1312fcc82d8SFrank Schaefer 		msleep(5);
1322fcc82d8SFrank Schaefer 	}
1332fcc82d8SFrank Schaefer 	if (ret != 0x84 + len - 1)
1342fcc82d8SFrank Schaefer 		em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
1352fcc82d8SFrank Schaefer 
1362fcc82d8SFrank Schaefer 	/* get the received message */
1372fcc82d8SFrank Schaefer 	ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
1382fcc82d8SFrank Schaefer 	if (ret != len) {
1392fcc82d8SFrank Schaefer 		em28xx_warn("reading from i2c device at 0x%x failed: "
1402fcc82d8SFrank Schaefer 			    "couldn't get the received message from the bridge "
1412fcc82d8SFrank Schaefer 			    "(error=%i)\n", addr, ret);
1422fcc82d8SFrank Schaefer 		return (ret < 0) ? ret : -EIO;
1432fcc82d8SFrank Schaefer 	}
1442fcc82d8SFrank Schaefer 	for (i = 0; i < len; i++)
1452fcc82d8SFrank Schaefer 		buf[i] = buf2[len - 1 - i];
1462fcc82d8SFrank Schaefer 
1470c0d06caSMauro Carvalho Chehab 	return ret;
1480c0d06caSMauro Carvalho Chehab }
1492fcc82d8SFrank Schaefer 
1502fcc82d8SFrank Schaefer /*
1512fcc82d8SFrank Schaefer  * em2800_i2c_check_for_device()
1522fcc82d8SFrank Schaefer  * check if there is an i2c device at the supplied address
1532fcc82d8SFrank Schaefer  */
1542fcc82d8SFrank Schaefer static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
1552fcc82d8SFrank Schaefer {
1562fcc82d8SFrank Schaefer 	u8 buf;
1572fcc82d8SFrank Schaefer 	int ret;
1582fcc82d8SFrank Schaefer 
1592fcc82d8SFrank Schaefer 	ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
1602fcc82d8SFrank Schaefer 	if (ret == 1)
1612fcc82d8SFrank Schaefer 		return 0;
1622fcc82d8SFrank Schaefer 	return (ret < 0) ? ret : -EIO;
1630c0d06caSMauro Carvalho Chehab }
1640c0d06caSMauro Carvalho Chehab 
1650c0d06caSMauro Carvalho Chehab /*
1660c0d06caSMauro Carvalho Chehab  * em28xx_i2c_send_bytes()
1670c0d06caSMauro Carvalho Chehab  */
168a6bad040SFrank Schaefer static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
169a6bad040SFrank Schaefer 				 u16 len, int stop)
1700c0d06caSMauro Carvalho Chehab {
1710c0d06caSMauro Carvalho Chehab 	int wrcount = 0;
1720c0d06caSMauro Carvalho Chehab 	int write_timeout, ret;
1730c0d06caSMauro Carvalho Chehab 
174f5ae371aSFrank Schaefer 	if (len < 1 || len > 64)
175f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
176f5ae371aSFrank Schaefer 
1770c0d06caSMauro Carvalho Chehab 	wrcount = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
1780c0d06caSMauro Carvalho Chehab 
1790c0d06caSMauro Carvalho Chehab 	/* Seems to be required after a write */
1802fcc82d8SFrank Schaefer 	for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
1810c0d06caSMauro Carvalho Chehab 	     write_timeout -= 5) {
1820c0d06caSMauro Carvalho Chehab 		ret = dev->em28xx_read_reg(dev, 0x05);
1830c0d06caSMauro Carvalho Chehab 		if (!ret)
1840c0d06caSMauro Carvalho Chehab 			break;
1850c0d06caSMauro Carvalho Chehab 		msleep(5);
1860c0d06caSMauro Carvalho Chehab 	}
1870c0d06caSMauro Carvalho Chehab 
1880c0d06caSMauro Carvalho Chehab 	return wrcount;
1890c0d06caSMauro Carvalho Chehab }
1900c0d06caSMauro Carvalho Chehab 
1910c0d06caSMauro Carvalho Chehab /*
1920c0d06caSMauro Carvalho Chehab  * em28xx_i2c_recv_bytes()
1930c0d06caSMauro Carvalho Chehab  * read a byte from the i2c device
1940c0d06caSMauro Carvalho Chehab  */
195a6bad040SFrank Schaefer static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
1960c0d06caSMauro Carvalho Chehab {
1970c0d06caSMauro Carvalho Chehab 	int ret;
198f5ae371aSFrank Schaefer 
199f5ae371aSFrank Schaefer 	if (len < 1 || len > 64)
200f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
201f5ae371aSFrank Schaefer 
2020c0d06caSMauro Carvalho Chehab 	ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
2030c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
2040c0d06caSMauro Carvalho Chehab 		em28xx_warn("reading i2c device failed (error=%i)\n", ret);
2050c0d06caSMauro Carvalho Chehab 		return ret;
2060c0d06caSMauro Carvalho Chehab 	}
2070c0d06caSMauro Carvalho Chehab 	if (dev->em28xx_read_reg(dev, 0x5) != 0)
2080c0d06caSMauro Carvalho Chehab 		return -ENODEV;
2090c0d06caSMauro Carvalho Chehab 	return ret;
2100c0d06caSMauro Carvalho Chehab }
2110c0d06caSMauro Carvalho Chehab 
2120c0d06caSMauro Carvalho Chehab /*
2130c0d06caSMauro Carvalho Chehab  * em28xx_i2c_check_for_device()
2140c0d06caSMauro Carvalho Chehab  * check if there is a i2c_device at the supplied address
2150c0d06caSMauro Carvalho Chehab  */
216a6bad040SFrank Schaefer static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
2170c0d06caSMauro Carvalho Chehab {
2180c0d06caSMauro Carvalho Chehab 	int ret;
2190c0d06caSMauro Carvalho Chehab 
2200c0d06caSMauro Carvalho Chehab 	ret = dev->em28xx_read_reg_req(dev, 2, addr);
2210c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
2220c0d06caSMauro Carvalho Chehab 		em28xx_warn("reading from i2c device failed (error=%i)\n", ret);
2230c0d06caSMauro Carvalho Chehab 		return ret;
2240c0d06caSMauro Carvalho Chehab 	}
2250c0d06caSMauro Carvalho Chehab 	if (dev->em28xx_read_reg(dev, 0x5) != 0)
2260c0d06caSMauro Carvalho Chehab 		return -ENODEV;
2270c0d06caSMauro Carvalho Chehab 	return 0;
2280c0d06caSMauro Carvalho Chehab }
2290c0d06caSMauro Carvalho Chehab 
2300c0d06caSMauro Carvalho Chehab /*
2310c0d06caSMauro Carvalho Chehab  * em28xx_i2c_xfer()
2320c0d06caSMauro Carvalho Chehab  * the main i2c transfer function
2330c0d06caSMauro Carvalho Chehab  */
2340c0d06caSMauro Carvalho Chehab static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
2350c0d06caSMauro Carvalho Chehab 			   struct i2c_msg msgs[], int num)
2360c0d06caSMauro Carvalho Chehab {
2370c0d06caSMauro Carvalho Chehab 	struct em28xx *dev = i2c_adap->algo_data;
2380c0d06caSMauro Carvalho Chehab 	int addr, rc, i, byte;
2390c0d06caSMauro Carvalho Chehab 
2400c0d06caSMauro Carvalho Chehab 	if (num <= 0)
2410c0d06caSMauro Carvalho Chehab 		return 0;
2420c0d06caSMauro Carvalho Chehab 	for (i = 0; i < num; i++) {
2430c0d06caSMauro Carvalho Chehab 		addr = msgs[i].addr << 1;
2440c0d06caSMauro Carvalho Chehab 		dprintk2(2, "%s %s addr=%x len=%d:",
2450c0d06caSMauro Carvalho Chehab 			 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
2460c0d06caSMauro Carvalho Chehab 			 i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len);
2470c0d06caSMauro Carvalho Chehab 		if (!msgs[i].len) { /* no len: check only for device presence */
2480c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
2490c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_check_for_device(dev, addr);
2500c0d06caSMauro Carvalho Chehab 			else
2510c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_check_for_device(dev, addr);
2520c0d06caSMauro Carvalho Chehab 			if (rc < 0) {
2530c0d06caSMauro Carvalho Chehab 				dprintk2(2, " no device\n");
2540c0d06caSMauro Carvalho Chehab 				return rc;
2550c0d06caSMauro Carvalho Chehab 			}
2560c0d06caSMauro Carvalho Chehab 
2570c0d06caSMauro Carvalho Chehab 		} else if (msgs[i].flags & I2C_M_RD) {
2580c0d06caSMauro Carvalho Chehab 			/* read bytes */
2590c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
2600c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_recv_bytes(dev, addr,
2610c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
2620c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
2630c0d06caSMauro Carvalho Chehab 			else
2640c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_recv_bytes(dev, addr,
2650c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
2660c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
2670c0d06caSMauro Carvalho Chehab 			if (i2c_debug >= 2) {
2680c0d06caSMauro Carvalho Chehab 				for (byte = 0; byte < msgs[i].len; byte++)
2690c0d06caSMauro Carvalho Chehab 					printk(" %02x", msgs[i].buf[byte]);
2700c0d06caSMauro Carvalho Chehab 			}
2710c0d06caSMauro Carvalho Chehab 		} else {
2720c0d06caSMauro Carvalho Chehab 			/* write bytes */
2730c0d06caSMauro Carvalho Chehab 			if (i2c_debug >= 2) {
2740c0d06caSMauro Carvalho Chehab 				for (byte = 0; byte < msgs[i].len; byte++)
2750c0d06caSMauro Carvalho Chehab 					printk(" %02x", msgs[i].buf[byte]);
2760c0d06caSMauro Carvalho Chehab 			}
2770c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
2780c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_send_bytes(dev, addr,
2790c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
2800c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
2810c0d06caSMauro Carvalho Chehab 			else
2820c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_send_bytes(dev, addr,
2830c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
2840c0d06caSMauro Carvalho Chehab 							   msgs[i].len,
2850c0d06caSMauro Carvalho Chehab 							   i == num - 1);
2860c0d06caSMauro Carvalho Chehab 		}
2870c0d06caSMauro Carvalho Chehab 		if (rc < 0)
2880c0d06caSMauro Carvalho Chehab 			goto err;
2890c0d06caSMauro Carvalho Chehab 		if (i2c_debug >= 2)
2900c0d06caSMauro Carvalho Chehab 			printk("\n");
2910c0d06caSMauro Carvalho Chehab 	}
2920c0d06caSMauro Carvalho Chehab 
2930c0d06caSMauro Carvalho Chehab 	return num;
2940c0d06caSMauro Carvalho Chehab err:
2950c0d06caSMauro Carvalho Chehab 	dprintk2(2, " ERROR: %i\n", rc);
2960c0d06caSMauro Carvalho Chehab 	return rc;
2970c0d06caSMauro Carvalho Chehab }
2980c0d06caSMauro Carvalho Chehab 
2990c0d06caSMauro Carvalho Chehab /* based on linux/sunrpc/svcauth.h and linux/hash.h
3000c0d06caSMauro Carvalho Chehab  * The original hash function returns a different value, if arch is x86_64
3010c0d06caSMauro Carvalho Chehab  *  or i386.
3020c0d06caSMauro Carvalho Chehab  */
3030c0d06caSMauro Carvalho Chehab static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
3040c0d06caSMauro Carvalho Chehab {
3050c0d06caSMauro Carvalho Chehab 	unsigned long hash = 0;
3060c0d06caSMauro Carvalho Chehab 	unsigned long l = 0;
3070c0d06caSMauro Carvalho Chehab 	int len = 0;
3080c0d06caSMauro Carvalho Chehab 	unsigned char c;
3090c0d06caSMauro Carvalho Chehab 	do {
3100c0d06caSMauro Carvalho Chehab 		if (len == length) {
3110c0d06caSMauro Carvalho Chehab 			c = (char)len;
3120c0d06caSMauro Carvalho Chehab 			len = -1;
3130c0d06caSMauro Carvalho Chehab 		} else
3140c0d06caSMauro Carvalho Chehab 			c = *buf++;
3150c0d06caSMauro Carvalho Chehab 		l = (l << 8) | c;
3160c0d06caSMauro Carvalho Chehab 		len++;
3170c0d06caSMauro Carvalho Chehab 		if ((len & (32 / 8 - 1)) == 0)
3180c0d06caSMauro Carvalho Chehab 			hash = ((hash^l) * 0x9e370001UL);
3190c0d06caSMauro Carvalho Chehab 	} while (len);
3200c0d06caSMauro Carvalho Chehab 
3210c0d06caSMauro Carvalho Chehab 	return (hash >> (32 - bits)) & 0xffffffffUL;
3220c0d06caSMauro Carvalho Chehab }
3230c0d06caSMauro Carvalho Chehab 
3240c0d06caSMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
3250c0d06caSMauro Carvalho Chehab {
3260c0d06caSMauro Carvalho Chehab 	unsigned char buf, *p = eedata;
3270c0d06caSMauro Carvalho Chehab 	struct em28xx_eeprom *em_eeprom = (void *)eedata;
3280c0d06caSMauro Carvalho Chehab 	int i, err, size = len, block;
3290c0d06caSMauro Carvalho Chehab 
3300c0d06caSMauro Carvalho Chehab 	if (dev->chip_id == CHIP_ID_EM2874 ||
3310c0d06caSMauro Carvalho Chehab 	    dev->chip_id == CHIP_ID_EM28174 ||
3320c0d06caSMauro Carvalho Chehab 	    dev->chip_id == CHIP_ID_EM2884) {
3330c0d06caSMauro Carvalho Chehab 		/* Empia switched to a 16-bit addressable eeprom in newer
3340c0d06caSMauro Carvalho Chehab 		   devices.  While we could certainly write a routine to read
3350c0d06caSMauro Carvalho Chehab 		   the eeprom, there is nothing of use in there that cannot be
3360c0d06caSMauro Carvalho Chehab 		   accessed through registers, and there is the risk that we
3370c0d06caSMauro Carvalho Chehab 		   could corrupt the eeprom (since a 16-bit read call is
3380c0d06caSMauro Carvalho Chehab 		   interpreted as a write call by 8-bit eeproms).
3390c0d06caSMauro Carvalho Chehab 		*/
3400c0d06caSMauro Carvalho Chehab 		return 0;
3410c0d06caSMauro Carvalho Chehab 	}
3420c0d06caSMauro Carvalho Chehab 
3430c0d06caSMauro Carvalho Chehab 	dev->i2c_client.addr = 0xa0 >> 1;
3440c0d06caSMauro Carvalho Chehab 
3450c0d06caSMauro Carvalho Chehab 	/* Check if board has eeprom */
3460c0d06caSMauro Carvalho Chehab 	err = i2c_master_recv(&dev->i2c_client, &buf, 0);
3470c0d06caSMauro Carvalho Chehab 	if (err < 0) {
3480c0d06caSMauro Carvalho Chehab 		em28xx_errdev("board has no eeprom\n");
3490c0d06caSMauro Carvalho Chehab 		memset(eedata, 0, len);
3500c0d06caSMauro Carvalho Chehab 		return -ENODEV;
3510c0d06caSMauro Carvalho Chehab 	}
3520c0d06caSMauro Carvalho Chehab 
3530c0d06caSMauro Carvalho Chehab 	buf = 0;
3540c0d06caSMauro Carvalho Chehab 
3550c0d06caSMauro Carvalho Chehab 	err = i2c_master_send(&dev->i2c_client, &buf, 1);
3560c0d06caSMauro Carvalho Chehab 	if (err != 1) {
3570c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
3580c0d06caSMauro Carvalho Chehab 		       dev->name, err);
3590c0d06caSMauro Carvalho Chehab 		return err;
3600c0d06caSMauro Carvalho Chehab 	}
3610c0d06caSMauro Carvalho Chehab 	while (size > 0) {
3620c0d06caSMauro Carvalho Chehab 		if (size > 16)
3630c0d06caSMauro Carvalho Chehab 			block = 16;
3640c0d06caSMauro Carvalho Chehab 		else
3650c0d06caSMauro Carvalho Chehab 			block = size;
3660c0d06caSMauro Carvalho Chehab 
3670c0d06caSMauro Carvalho Chehab 		if (block !=
3680c0d06caSMauro Carvalho Chehab 		    (err = i2c_master_recv(&dev->i2c_client, p, block))) {
3690c0d06caSMauro Carvalho Chehab 			printk(KERN_WARNING
3700c0d06caSMauro Carvalho Chehab 			       "%s: i2c eeprom read error (err=%d)\n",
3710c0d06caSMauro Carvalho Chehab 			       dev->name, err);
3720c0d06caSMauro Carvalho Chehab 			return err;
3730c0d06caSMauro Carvalho Chehab 		}
3740c0d06caSMauro Carvalho Chehab 		size -= block;
3750c0d06caSMauro Carvalho Chehab 		p += block;
3760c0d06caSMauro Carvalho Chehab 	}
3770c0d06caSMauro Carvalho Chehab 	for (i = 0; i < len; i++) {
3780c0d06caSMauro Carvalho Chehab 		if (0 == (i % 16))
3790c0d06caSMauro Carvalho Chehab 			printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i);
3800c0d06caSMauro Carvalho Chehab 		printk(" %02x", eedata[i]);
3810c0d06caSMauro Carvalho Chehab 		if (15 == (i % 16))
3820c0d06caSMauro Carvalho Chehab 			printk("\n");
3830c0d06caSMauro Carvalho Chehab 	}
3840c0d06caSMauro Carvalho Chehab 
3850c0d06caSMauro Carvalho Chehab 	if (em_eeprom->id == 0x9567eb1a)
3860c0d06caSMauro Carvalho Chehab 		dev->hash = em28xx_hash_mem(eedata, len, 32);
3870c0d06caSMauro Carvalho Chehab 
3880c0d06caSMauro Carvalho Chehab 	printk(KERN_INFO "%s: EEPROM ID= 0x%08x, EEPROM hash = 0x%08lx\n",
3890c0d06caSMauro Carvalho Chehab 	       dev->name, em_eeprom->id, dev->hash);
3900c0d06caSMauro Carvalho Chehab 
3910c0d06caSMauro Carvalho Chehab 	printk(KERN_INFO "%s: EEPROM info:\n", dev->name);
3920c0d06caSMauro Carvalho Chehab 
3930c0d06caSMauro Carvalho Chehab 	switch (em_eeprom->chip_conf >> 4 & 0x3) {
3940c0d06caSMauro Carvalho Chehab 	case 0:
3950c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tNo audio on board.\n", dev->name);
3960c0d06caSMauro Carvalho Chehab 		break;
3970c0d06caSMauro Carvalho Chehab 	case 1:
3980c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tAC97 audio (5 sample rates)\n",
3990c0d06caSMauro Carvalho Chehab 				 dev->name);
4000c0d06caSMauro Carvalho Chehab 		break;
4010c0d06caSMauro Carvalho Chehab 	case 2:
4020c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tI2S audio, sample rate=32k\n",
4030c0d06caSMauro Carvalho Chehab 				 dev->name);
4040c0d06caSMauro Carvalho Chehab 		break;
4050c0d06caSMauro Carvalho Chehab 	case 3:
4060c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tI2S audio, 3 sample rates\n",
4070c0d06caSMauro Carvalho Chehab 				 dev->name);
4080c0d06caSMauro Carvalho Chehab 		break;
4090c0d06caSMauro Carvalho Chehab 	}
4100c0d06caSMauro Carvalho Chehab 
4110c0d06caSMauro Carvalho Chehab 	if (em_eeprom->chip_conf & 1 << 3)
4120c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tUSB Remote wakeup capable\n", dev->name);
4130c0d06caSMauro Carvalho Chehab 
4140c0d06caSMauro Carvalho Chehab 	if (em_eeprom->chip_conf & 1 << 2)
4150c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\tUSB Self power capable\n", dev->name);
4160c0d06caSMauro Carvalho Chehab 
4170c0d06caSMauro Carvalho Chehab 	switch (em_eeprom->chip_conf & 0x3) {
4180c0d06caSMauro Carvalho Chehab 	case 0:
4190c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\t500mA max power\n", dev->name);
4200c0d06caSMauro Carvalho Chehab 		break;
4210c0d06caSMauro Carvalho Chehab 	case 1:
4220c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\t400mA max power\n", dev->name);
4230c0d06caSMauro Carvalho Chehab 		break;
4240c0d06caSMauro Carvalho Chehab 	case 2:
4250c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\t300mA max power\n", dev->name);
4260c0d06caSMauro Carvalho Chehab 		break;
4270c0d06caSMauro Carvalho Chehab 	case 3:
4280c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s:\t200mA max power\n", dev->name);
4290c0d06caSMauro Carvalho Chehab 		break;
4300c0d06caSMauro Carvalho Chehab 	}
4310c0d06caSMauro Carvalho Chehab 	printk(KERN_INFO "%s:\tTable at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
4320c0d06caSMauro Carvalho Chehab 				dev->name,
4330c0d06caSMauro Carvalho Chehab 				em_eeprom->string_idx_table,
4340c0d06caSMauro Carvalho Chehab 				em_eeprom->string1,
4350c0d06caSMauro Carvalho Chehab 				em_eeprom->string2,
4360c0d06caSMauro Carvalho Chehab 				em_eeprom->string3);
4370c0d06caSMauro Carvalho Chehab 
4380c0d06caSMauro Carvalho Chehab 	return 0;
4390c0d06caSMauro Carvalho Chehab }
4400c0d06caSMauro Carvalho Chehab 
4410c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
4420c0d06caSMauro Carvalho Chehab 
4430c0d06caSMauro Carvalho Chehab /*
4440c0d06caSMauro Carvalho Chehab  * functionality()
4450c0d06caSMauro Carvalho Chehab  */
4460c0d06caSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *adap)
4470c0d06caSMauro Carvalho Chehab {
448*eaf33c40SFrank Schaefer 	struct em28xx *dev = adap->algo_data;
449*eaf33c40SFrank Schaefer 	u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
450*eaf33c40SFrank Schaefer 	if (dev->board.is_em2800)
451*eaf33c40SFrank Schaefer 		func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
452*eaf33c40SFrank Schaefer 	return func_flags;
4530c0d06caSMauro Carvalho Chehab }
4540c0d06caSMauro Carvalho Chehab 
4550c0d06caSMauro Carvalho Chehab static struct i2c_algorithm em28xx_algo = {
4560c0d06caSMauro Carvalho Chehab 	.master_xfer   = em28xx_i2c_xfer,
4570c0d06caSMauro Carvalho Chehab 	.functionality = functionality,
4580c0d06caSMauro Carvalho Chehab };
4590c0d06caSMauro Carvalho Chehab 
4600c0d06caSMauro Carvalho Chehab static struct i2c_adapter em28xx_adap_template = {
4610c0d06caSMauro Carvalho Chehab 	.owner = THIS_MODULE,
4620c0d06caSMauro Carvalho Chehab 	.name = "em28xx",
4630c0d06caSMauro Carvalho Chehab 	.algo = &em28xx_algo,
4640c0d06caSMauro Carvalho Chehab };
4650c0d06caSMauro Carvalho Chehab 
4660c0d06caSMauro Carvalho Chehab static struct i2c_client em28xx_client_template = {
4670c0d06caSMauro Carvalho Chehab 	.name = "em28xx internal",
4680c0d06caSMauro Carvalho Chehab };
4690c0d06caSMauro Carvalho Chehab 
4700c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
4710c0d06caSMauro Carvalho Chehab 
4720c0d06caSMauro Carvalho Chehab /*
4730c0d06caSMauro Carvalho Chehab  * i2c_devs
4740c0d06caSMauro Carvalho Chehab  * incomplete list of known devices
4750c0d06caSMauro Carvalho Chehab  */
4760c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = {
4770c0d06caSMauro Carvalho Chehab 	[0x4a >> 1] = "saa7113h",
4780c0d06caSMauro Carvalho Chehab 	[0x52 >> 1] = "drxk",
4790c0d06caSMauro Carvalho Chehab 	[0x60 >> 1] = "remote IR sensor",
4800c0d06caSMauro Carvalho Chehab 	[0x8e >> 1] = "remote IR sensor",
4810c0d06caSMauro Carvalho Chehab 	[0x86 >> 1] = "tda9887",
4820c0d06caSMauro Carvalho Chehab 	[0x80 >> 1] = "msp34xx",
4830c0d06caSMauro Carvalho Chehab 	[0x88 >> 1] = "msp34xx",
4840c0d06caSMauro Carvalho Chehab 	[0xa0 >> 1] = "eeprom",
4850c0d06caSMauro Carvalho Chehab 	[0xb0 >> 1] = "tda9874",
4860c0d06caSMauro Carvalho Chehab 	[0xb8 >> 1] = "tvp5150a",
4870c0d06caSMauro Carvalho Chehab 	[0xba >> 1] = "webcam sensor or tvp5150a",
4880c0d06caSMauro Carvalho Chehab 	[0xc0 >> 1] = "tuner (analog)",
4890c0d06caSMauro Carvalho Chehab 	[0xc2 >> 1] = "tuner (analog)",
4900c0d06caSMauro Carvalho Chehab 	[0xc4 >> 1] = "tuner (analog)",
4910c0d06caSMauro Carvalho Chehab 	[0xc6 >> 1] = "tuner (analog)",
4920c0d06caSMauro Carvalho Chehab };
4930c0d06caSMauro Carvalho Chehab 
4940c0d06caSMauro Carvalho Chehab /*
4950c0d06caSMauro Carvalho Chehab  * do_i2c_scan()
4960c0d06caSMauro Carvalho Chehab  * check i2c address range for devices
4970c0d06caSMauro Carvalho Chehab  */
4980c0d06caSMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev)
4990c0d06caSMauro Carvalho Chehab {
5000c0d06caSMauro Carvalho Chehab 	u8 i2c_devicelist[128];
5010c0d06caSMauro Carvalho Chehab 	unsigned char buf;
5020c0d06caSMauro Carvalho Chehab 	int i, rc;
5030c0d06caSMauro Carvalho Chehab 
5040c0d06caSMauro Carvalho Chehab 	memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
5050c0d06caSMauro Carvalho Chehab 
5060c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
5070c0d06caSMauro Carvalho Chehab 		dev->i2c_client.addr = i;
5080c0d06caSMauro Carvalho Chehab 		rc = i2c_master_recv(&dev->i2c_client, &buf, 0);
5090c0d06caSMauro Carvalho Chehab 		if (rc < 0)
5100c0d06caSMauro Carvalho Chehab 			continue;
5110c0d06caSMauro Carvalho Chehab 		i2c_devicelist[i] = i;
5120c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n",
5130c0d06caSMauro Carvalho Chehab 		       dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
5140c0d06caSMauro Carvalho Chehab 	}
5150c0d06caSMauro Carvalho Chehab 
5160c0d06caSMauro Carvalho Chehab 	dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
5170c0d06caSMauro Carvalho Chehab 					ARRAY_SIZE(i2c_devicelist), 32);
5180c0d06caSMauro Carvalho Chehab }
5190c0d06caSMauro Carvalho Chehab 
5200c0d06caSMauro Carvalho Chehab /*
5210c0d06caSMauro Carvalho Chehab  * em28xx_i2c_register()
5220c0d06caSMauro Carvalho Chehab  * register i2c bus
5230c0d06caSMauro Carvalho Chehab  */
5240c0d06caSMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev)
5250c0d06caSMauro Carvalho Chehab {
5260c0d06caSMauro Carvalho Chehab 	int retval;
5270c0d06caSMauro Carvalho Chehab 
5280c0d06caSMauro Carvalho Chehab 	BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
5290c0d06caSMauro Carvalho Chehab 	BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
5300c0d06caSMauro Carvalho Chehab 	dev->i2c_adap = em28xx_adap_template;
5310c0d06caSMauro Carvalho Chehab 	dev->i2c_adap.dev.parent = &dev->udev->dev;
5320c0d06caSMauro Carvalho Chehab 	strcpy(dev->i2c_adap.name, dev->name);
5330c0d06caSMauro Carvalho Chehab 	dev->i2c_adap.algo_data = dev;
5340c0d06caSMauro Carvalho Chehab 	i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
5350c0d06caSMauro Carvalho Chehab 
5360c0d06caSMauro Carvalho Chehab 	retval = i2c_add_adapter(&dev->i2c_adap);
5370c0d06caSMauro Carvalho Chehab 	if (retval < 0) {
5380c0d06caSMauro Carvalho Chehab 		em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
5390c0d06caSMauro Carvalho Chehab 			__func__, retval);
5400c0d06caSMauro Carvalho Chehab 		return retval;
5410c0d06caSMauro Carvalho Chehab 	}
5420c0d06caSMauro Carvalho Chehab 
5430c0d06caSMauro Carvalho Chehab 	dev->i2c_client = em28xx_client_template;
5440c0d06caSMauro Carvalho Chehab 	dev->i2c_client.adapter = &dev->i2c_adap;
5450c0d06caSMauro Carvalho Chehab 
5460c0d06caSMauro Carvalho Chehab 	retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata));
5470c0d06caSMauro Carvalho Chehab 	if ((retval < 0) && (retval != -ENODEV)) {
5480c0d06caSMauro Carvalho Chehab 		em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
5490c0d06caSMauro Carvalho Chehab 			__func__, retval);
5500c0d06caSMauro Carvalho Chehab 
5510c0d06caSMauro Carvalho Chehab 		return retval;
5520c0d06caSMauro Carvalho Chehab 	}
5530c0d06caSMauro Carvalho Chehab 
5540c0d06caSMauro Carvalho Chehab 	if (i2c_scan)
5550c0d06caSMauro Carvalho Chehab 		em28xx_do_i2c_scan(dev);
5560c0d06caSMauro Carvalho Chehab 
5570c0d06caSMauro Carvalho Chehab 	return 0;
5580c0d06caSMauro Carvalho Chehab }
5590c0d06caSMauro Carvalho Chehab 
5600c0d06caSMauro Carvalho Chehab /*
5610c0d06caSMauro Carvalho Chehab  * em28xx_i2c_unregister()
5620c0d06caSMauro Carvalho Chehab  * unregister i2c_bus
5630c0d06caSMauro Carvalho Chehab  */
5640c0d06caSMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev)
5650c0d06caSMauro Carvalho Chehab {
5660c0d06caSMauro Carvalho Chehab 	i2c_del_adapter(&dev->i2c_adap);
5670c0d06caSMauro Carvalho Chehab 	return 0;
5680c0d06caSMauro Carvalho Chehab }
569