xref: /openbmc/linux/drivers/media/usb/em28xx/em28xx-i2c.c (revision 7f6301d1257505d35e87ef1cb8eeee268e63a123)
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 /*
45f5ae371aSFrank Schaefer  * em2800_i2c_send_bytes()
46f5ae371aSFrank Schaefer  * send up to 4 bytes to the em2800 i2c device
470c0d06caSMauro Carvalho Chehab  */
48f5ae371aSFrank Schaefer static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
490c0d06caSMauro Carvalho Chehab {
500c0d06caSMauro Carvalho Chehab 	int ret;
510c0d06caSMauro Carvalho Chehab 	int write_timeout;
52a6bad040SFrank Schaefer 	u8 b2[6];
53f5ae371aSFrank Schaefer 
54f5ae371aSFrank Schaefer 	if (len < 1 || len > 4)
55f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
56f5ae371aSFrank Schaefer 
570c0d06caSMauro Carvalho Chehab 	BUG_ON(len < 1 || len > 4);
580c0d06caSMauro Carvalho Chehab 	b2[5] = 0x80 + len - 1;
590c0d06caSMauro Carvalho Chehab 	b2[4] = addr;
600c0d06caSMauro Carvalho Chehab 	b2[3] = buf[0];
610c0d06caSMauro Carvalho Chehab 	if (len > 1)
620c0d06caSMauro Carvalho Chehab 		b2[2] = buf[1];
630c0d06caSMauro Carvalho Chehab 	if (len > 2)
640c0d06caSMauro Carvalho Chehab 		b2[1] = buf[2];
650c0d06caSMauro Carvalho Chehab 	if (len > 3)
660c0d06caSMauro Carvalho Chehab 		b2[0] = buf[3];
670c0d06caSMauro Carvalho Chehab 
682fcc82d8SFrank Schaefer 	/* trigger write */
690c0d06caSMauro Carvalho Chehab 	ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
700c0d06caSMauro Carvalho Chehab 	if (ret != 2 + len) {
7145f04e82SFrank Schaefer 		em28xx_warn("failed to trigger write to i2c address 0x%x "
7245f04e82SFrank Schaefer 			    "(error=%i)\n", addr, ret);
7345f04e82SFrank Schaefer 		return (ret < 0) ? ret : -EIO;
740c0d06caSMauro Carvalho Chehab 	}
752fcc82d8SFrank Schaefer 	/* wait for completion */
762fcc82d8SFrank Schaefer 	for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
770c0d06caSMauro Carvalho Chehab 	     write_timeout -= 5) {
780c0d06caSMauro Carvalho Chehab 		ret = dev->em28xx_read_reg(dev, 0x05);
7945f04e82SFrank Schaefer 		if (ret == 0x80 + len - 1) {
800c0d06caSMauro Carvalho Chehab 			return len;
8145f04e82SFrank Schaefer 		} else if (ret == 0x94 + len - 1) {
8245f04e82SFrank Schaefer 			return -ENODEV;
8345f04e82SFrank Schaefer 		} else if (ret < 0) {
8445f04e82SFrank Schaefer 			em28xx_warn("failed to get i2c transfer status from "
8545f04e82SFrank Schaefer 				    "bridge register (error=%i)\n", ret);
8645f04e82SFrank Schaefer 			return ret;
8745f04e82SFrank Schaefer 		}
880c0d06caSMauro Carvalho Chehab 		msleep(5);
890c0d06caSMauro Carvalho Chehab 	}
9045f04e82SFrank Schaefer 	em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
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 write_timeout, ret;
1720c0d06caSMauro Carvalho Chehab 
173f5ae371aSFrank Schaefer 	if (len < 1 || len > 64)
174f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
17545f04e82SFrank Schaefer 	/* NOTE: limited by the USB ctrl message constraints
17645f04e82SFrank Schaefer 	 * Zero length reads always succeed, even if no device is connected */
177f5ae371aSFrank Schaefer 
17845f04e82SFrank Schaefer 	/* Write to i2c device */
17945f04e82SFrank Schaefer 	ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
18045f04e82SFrank Schaefer 	if (ret != len) {
18145f04e82SFrank Schaefer 		if (ret < 0) {
18245f04e82SFrank Schaefer 			em28xx_warn("writing to i2c device at 0x%x failed "
18345f04e82SFrank Schaefer 				    "(error=%i)\n", addr, ret);
18445f04e82SFrank Schaefer 			return ret;
18545f04e82SFrank Schaefer 		} else {
18645f04e82SFrank Schaefer 			em28xx_warn("%i bytes write to i2c device at 0x%x "
18745f04e82SFrank Schaefer 				    "requested, but %i bytes written\n",
18845f04e82SFrank Schaefer 				    len, addr, ret);
18945f04e82SFrank Schaefer 			return -EIO;
19045f04e82SFrank Schaefer 		}
19145f04e82SFrank Schaefer 	}
1920c0d06caSMauro Carvalho Chehab 
19345f04e82SFrank Schaefer 	/* Check success of the i2c operation */
1942fcc82d8SFrank Schaefer 	for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
1950c0d06caSMauro Carvalho Chehab 	     write_timeout -= 5) {
1960c0d06caSMauro Carvalho Chehab 		ret = dev->em28xx_read_reg(dev, 0x05);
19745f04e82SFrank Schaefer 		if (ret == 0) { /* success */
19845f04e82SFrank Schaefer 			return len;
19945f04e82SFrank Schaefer 		} else if (ret == 0x10) {
20045f04e82SFrank Schaefer 			return -ENODEV;
20145f04e82SFrank Schaefer 		} else if (ret < 0) {
20245f04e82SFrank Schaefer 			em28xx_warn("failed to read i2c transfer status from "
20345f04e82SFrank Schaefer 				    "bridge (error=%i)\n", ret);
20445f04e82SFrank Schaefer 			return ret;
2050c0d06caSMauro Carvalho Chehab 		}
20645f04e82SFrank Schaefer 		msleep(5);
20745f04e82SFrank Schaefer 		/* NOTE: do we really have to wait for success ?
20845f04e82SFrank Schaefer 		   Never seen anything else than 0x00 or 0x10
20945f04e82SFrank Schaefer 		   (even with high payload) ...			*/
21045f04e82SFrank Schaefer 	}
21145f04e82SFrank Schaefer 	em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
21245f04e82SFrank Schaefer 	return -EIO;
2130c0d06caSMauro Carvalho Chehab }
2140c0d06caSMauro Carvalho Chehab 
2150c0d06caSMauro Carvalho Chehab /*
2160c0d06caSMauro Carvalho Chehab  * em28xx_i2c_recv_bytes()
2170c0d06caSMauro Carvalho Chehab  * read a byte from the i2c device
2180c0d06caSMauro Carvalho Chehab  */
219a6bad040SFrank Schaefer static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
2200c0d06caSMauro Carvalho Chehab {
2210c0d06caSMauro Carvalho Chehab 	int ret;
222f5ae371aSFrank Schaefer 
223f5ae371aSFrank Schaefer 	if (len < 1 || len > 64)
224f5ae371aSFrank Schaefer 		return -EOPNOTSUPP;
22545f04e82SFrank Schaefer 	/* NOTE: limited by the USB ctrl message constraints
22645f04e82SFrank Schaefer 	 * Zero length reads always succeed, even if no device is connected */
227f5ae371aSFrank Schaefer 
22845f04e82SFrank Schaefer 	/* Read data from i2c device */
2290c0d06caSMauro Carvalho Chehab 	ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
2300c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
231*7f6301d1SFrank Schaefer 		em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
232*7f6301d1SFrank Schaefer 			    addr, ret);
23345f04e82SFrank Schaefer 		return ret;
23445f04e82SFrank Schaefer 	}
235*7f6301d1SFrank Schaefer 	/* NOTE: some devices with two i2c busses have the bad habit to return 0
236*7f6301d1SFrank Schaefer 	 * bytes if we are on bus B AND there was no write attempt to the
237*7f6301d1SFrank Schaefer 	 * specified slave address before AND no device is present at the
238*7f6301d1SFrank Schaefer 	 * requested slave address.
239*7f6301d1SFrank Schaefer 	 * Anyway, the next check will fail with -ENODEV in this case, so avoid
240*7f6301d1SFrank Schaefer 	 * spamming the system log on device probing and do nothing here.
241*7f6301d1SFrank Schaefer 	 */
24245f04e82SFrank Schaefer 
24345f04e82SFrank Schaefer 	/* Check success of the i2c operation */
24445f04e82SFrank Schaefer 	ret = dev->em28xx_read_reg(dev, 0x05);
24545f04e82SFrank Schaefer 	if (ret < 0) {
24645f04e82SFrank Schaefer 		em28xx_warn("failed to read i2c transfer status from "
24745f04e82SFrank Schaefer 			    "bridge (error=%i)\n", ret);
2480c0d06caSMauro Carvalho Chehab 		return ret;
2490c0d06caSMauro Carvalho Chehab 	}
25045f04e82SFrank Schaefer 	if (ret > 0) {
25145f04e82SFrank Schaefer 		if (ret == 0x10) {
2520c0d06caSMauro Carvalho Chehab 			return -ENODEV;
25345f04e82SFrank Schaefer 		} else {
25445f04e82SFrank Schaefer 			em28xx_warn("unknown i2c error (status=%i)\n", ret);
25545f04e82SFrank Schaefer 			return -EIO;
25645f04e82SFrank Schaefer 		}
25745f04e82SFrank Schaefer 	}
25845f04e82SFrank Schaefer 	return len;
2590c0d06caSMauro Carvalho Chehab }
2600c0d06caSMauro Carvalho Chehab 
2610c0d06caSMauro Carvalho Chehab /*
2620c0d06caSMauro Carvalho Chehab  * em28xx_i2c_check_for_device()
2630c0d06caSMauro Carvalho Chehab  * check if there is a i2c_device at the supplied address
2640c0d06caSMauro Carvalho Chehab  */
265a6bad040SFrank Schaefer static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
2660c0d06caSMauro Carvalho Chehab {
2670c0d06caSMauro Carvalho Chehab 	int ret;
26845f04e82SFrank Schaefer 	u8 buf;
2690c0d06caSMauro Carvalho Chehab 
27045f04e82SFrank Schaefer 	ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
27145f04e82SFrank Schaefer 	if (ret == 1)
2720c0d06caSMauro Carvalho Chehab 		return 0;
27345f04e82SFrank Schaefer 	return (ret < 0) ? ret : -EIO;
2740c0d06caSMauro Carvalho Chehab }
2750c0d06caSMauro Carvalho Chehab 
2760c0d06caSMauro Carvalho Chehab /*
2770c0d06caSMauro Carvalho Chehab  * em28xx_i2c_xfer()
2780c0d06caSMauro Carvalho Chehab  * the main i2c transfer function
2790c0d06caSMauro Carvalho Chehab  */
2800c0d06caSMauro Carvalho Chehab static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
2810c0d06caSMauro Carvalho Chehab 			   struct i2c_msg msgs[], int num)
2820c0d06caSMauro Carvalho Chehab {
283aab3125cSMauro Carvalho Chehab 	struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
284aab3125cSMauro Carvalho Chehab 	struct em28xx *dev = i2c_bus->dev;
285aab3125cSMauro Carvalho Chehab 	unsigned bus = i2c_bus->bus;
2860c0d06caSMauro Carvalho Chehab 	int addr, rc, i, byte;
2870c0d06caSMauro Carvalho Chehab 
288aab3125cSMauro Carvalho Chehab 	rc = rt_mutex_trylock(&dev->i2c_bus_lock);
289aab3125cSMauro Carvalho Chehab 	if (rc < 0)
290aab3125cSMauro Carvalho Chehab 		return rc;
291aab3125cSMauro Carvalho Chehab 
292aab3125cSMauro Carvalho Chehab 	/* Switch I2C bus if needed */
293aab3125cSMauro Carvalho Chehab 	if (bus != dev->cur_i2c_bus) {
294aab3125cSMauro Carvalho Chehab 		if (bus == 1)
295aab3125cSMauro Carvalho Chehab 			dev->cur_i2c_bus |= EM2874_I2C_SECONDARY_BUS_SELECT;
296aab3125cSMauro Carvalho Chehab 		else
297aab3125cSMauro Carvalho Chehab 			dev->cur_i2c_bus &= ~EM2874_I2C_SECONDARY_BUS_SELECT;
298aab3125cSMauro Carvalho Chehab 		em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, dev->cur_i2c_bus);
299aab3125cSMauro Carvalho Chehab 		dev->cur_i2c_bus = bus;
300aab3125cSMauro Carvalho Chehab 	}
301aab3125cSMauro Carvalho Chehab 
302aab3125cSMauro Carvalho Chehab 	if (num <= 0) {
303aab3125cSMauro Carvalho Chehab 		rt_mutex_unlock(&dev->i2c_bus_lock);
3040c0d06caSMauro Carvalho Chehab 		return 0;
305aab3125cSMauro Carvalho Chehab 	}
3060c0d06caSMauro Carvalho Chehab 	for (i = 0; i < num; i++) {
3070c0d06caSMauro Carvalho Chehab 		addr = msgs[i].addr << 1;
308d90f0677SFrank Schaefer 		if (i2c_debug)
309d7a80eaaSFrank Schaefer 			printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
310d7a80eaaSFrank Schaefer 			       dev->name, __func__ ,
3110c0d06caSMauro Carvalho Chehab 			       (msgs[i].flags & I2C_M_RD) ? "read" : "write",
312d7a80eaaSFrank Schaefer 			       i == num - 1 ? "stop" : "nonstop",
313d7a80eaaSFrank Schaefer 			       addr, msgs[i].len);
3140c0d06caSMauro Carvalho Chehab 		if (!msgs[i].len) { /* no len: check only for device presence */
3150c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
3160c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_check_for_device(dev, addr);
3170c0d06caSMauro Carvalho Chehab 			else
3180c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_check_for_device(dev, addr);
31945f04e82SFrank Schaefer 			if (rc == -ENODEV) {
320d90f0677SFrank Schaefer 				if (i2c_debug)
32145f04e82SFrank Schaefer 					printk(" no device\n");
322aab3125cSMauro Carvalho Chehab 				rt_mutex_unlock(&dev->i2c_bus_lock);
3230c0d06caSMauro Carvalho Chehab 				return rc;
3240c0d06caSMauro Carvalho Chehab 			}
3250c0d06caSMauro Carvalho Chehab 		} else if (msgs[i].flags & I2C_M_RD) {
3260c0d06caSMauro Carvalho Chehab 			/* read bytes */
3270c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
3280c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_recv_bytes(dev, addr,
3290c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
3300c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
3310c0d06caSMauro Carvalho Chehab 			else
3320c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_recv_bytes(dev, addr,
3330c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
3340c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
335d90f0677SFrank Schaefer 			if (i2c_debug) {
3360c0d06caSMauro Carvalho Chehab 				for (byte = 0; byte < msgs[i].len; byte++)
3370c0d06caSMauro Carvalho Chehab 					printk(" %02x", msgs[i].buf[byte]);
3380c0d06caSMauro Carvalho Chehab 			}
3390c0d06caSMauro Carvalho Chehab 		} else {
3400c0d06caSMauro Carvalho Chehab 			/* write bytes */
341d90f0677SFrank Schaefer 			if (i2c_debug) {
3420c0d06caSMauro Carvalho Chehab 				for (byte = 0; byte < msgs[i].len; byte++)
3430c0d06caSMauro Carvalho Chehab 					printk(" %02x", msgs[i].buf[byte]);
3440c0d06caSMauro Carvalho Chehab 			}
3450c0d06caSMauro Carvalho Chehab 			if (dev->board.is_em2800)
3460c0d06caSMauro Carvalho Chehab 				rc = em2800_i2c_send_bytes(dev, addr,
3470c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
3480c0d06caSMauro Carvalho Chehab 							   msgs[i].len);
3490c0d06caSMauro Carvalho Chehab 			else
3500c0d06caSMauro Carvalho Chehab 				rc = em28xx_i2c_send_bytes(dev, addr,
3510c0d06caSMauro Carvalho Chehab 							   msgs[i].buf,
3520c0d06caSMauro Carvalho Chehab 							   msgs[i].len,
3530c0d06caSMauro Carvalho Chehab 							   i == num - 1);
3540c0d06caSMauro Carvalho Chehab 		}
35545f04e82SFrank Schaefer 		if (rc < 0) {
356d90f0677SFrank Schaefer 			if (i2c_debug)
35745f04e82SFrank Schaefer 				printk(" ERROR: %i\n", rc);
358aab3125cSMauro Carvalho Chehab 			rt_mutex_unlock(&dev->i2c_bus_lock);
35945f04e82SFrank Schaefer 			return rc;
36045f04e82SFrank Schaefer 		}
361d90f0677SFrank Schaefer 		if (i2c_debug)
3620c0d06caSMauro Carvalho Chehab 			printk("\n");
3630c0d06caSMauro Carvalho Chehab 	}
3640c0d06caSMauro Carvalho Chehab 
365aab3125cSMauro Carvalho Chehab 	rt_mutex_unlock(&dev->i2c_bus_lock);
3660c0d06caSMauro Carvalho Chehab 	return num;
3670c0d06caSMauro Carvalho Chehab }
3680c0d06caSMauro Carvalho Chehab 
3690c0d06caSMauro Carvalho Chehab /* based on linux/sunrpc/svcauth.h and linux/hash.h
3700c0d06caSMauro Carvalho Chehab  * The original hash function returns a different value, if arch is x86_64
3710c0d06caSMauro Carvalho Chehab  *  or i386.
3720c0d06caSMauro Carvalho Chehab  */
3730c0d06caSMauro Carvalho Chehab static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
3740c0d06caSMauro Carvalho Chehab {
3750c0d06caSMauro Carvalho Chehab 	unsigned long hash = 0;
3760c0d06caSMauro Carvalho Chehab 	unsigned long l = 0;
3770c0d06caSMauro Carvalho Chehab 	int len = 0;
3780c0d06caSMauro Carvalho Chehab 	unsigned char c;
3790c0d06caSMauro Carvalho Chehab 	do {
3800c0d06caSMauro Carvalho Chehab 		if (len == length) {
3810c0d06caSMauro Carvalho Chehab 			c = (char)len;
3820c0d06caSMauro Carvalho Chehab 			len = -1;
3830c0d06caSMauro Carvalho Chehab 		} else
3840c0d06caSMauro Carvalho Chehab 			c = *buf++;
3850c0d06caSMauro Carvalho Chehab 		l = (l << 8) | c;
3860c0d06caSMauro Carvalho Chehab 		len++;
3870c0d06caSMauro Carvalho Chehab 		if ((len & (32 / 8 - 1)) == 0)
3880c0d06caSMauro Carvalho Chehab 			hash = ((hash^l) * 0x9e370001UL);
3890c0d06caSMauro Carvalho Chehab 	} while (len);
3900c0d06caSMauro Carvalho Chehab 
3910c0d06caSMauro Carvalho Chehab 	return (hash >> (32 - bits)) & 0xffffffffUL;
3920c0d06caSMauro Carvalho Chehab }
3930c0d06caSMauro Carvalho Chehab 
394d832c5b2SFrank Schaefer /* Helper function to read data blocks from i2c clients with 8 or 16 bit
395d832c5b2SFrank Schaefer  * address width, 8 bit register width and auto incrementation been activated */
396aab3125cSMauro Carvalho Chehab static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
397aab3125cSMauro Carvalho Chehab 				 bool addr_w16, u16 len, u8 *data)
398d832c5b2SFrank Schaefer {
399d832c5b2SFrank Schaefer 	int remain = len, rsize, rsize_max, ret;
400d832c5b2SFrank Schaefer 	u8 buf[2];
401d832c5b2SFrank Schaefer 
402d832c5b2SFrank Schaefer 	/* Sanity check */
403d832c5b2SFrank Schaefer 	if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
404d832c5b2SFrank Schaefer 		return -EINVAL;
405d832c5b2SFrank Schaefer 	/* Select address */
406d832c5b2SFrank Schaefer 	buf[0] = addr >> 8;
407d832c5b2SFrank Schaefer 	buf[1] = addr & 0xff;
408aab3125cSMauro Carvalho Chehab 	ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
409d832c5b2SFrank Schaefer 	if (ret < 0)
410d832c5b2SFrank Schaefer 		return ret;
411d832c5b2SFrank Schaefer 	/* Read data */
412d832c5b2SFrank Schaefer 	if (dev->board.is_em2800)
413d832c5b2SFrank Schaefer 		rsize_max = 4;
414d832c5b2SFrank Schaefer 	else
415d832c5b2SFrank Schaefer 		rsize_max = 64;
416d832c5b2SFrank Schaefer 	while (remain > 0) {
417d832c5b2SFrank Schaefer 		if (remain > rsize_max)
418d832c5b2SFrank Schaefer 			rsize = rsize_max;
419d832c5b2SFrank Schaefer 		else
420d832c5b2SFrank Schaefer 			rsize = remain;
421d832c5b2SFrank Schaefer 
422aab3125cSMauro Carvalho Chehab 		ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
423d832c5b2SFrank Schaefer 		if (ret < 0)
424d832c5b2SFrank Schaefer 			return ret;
425d832c5b2SFrank Schaefer 
426d832c5b2SFrank Schaefer 		remain -= rsize;
427d832c5b2SFrank Schaefer 		data += rsize;
428d832c5b2SFrank Schaefer 	}
429d832c5b2SFrank Schaefer 
430d832c5b2SFrank Schaefer 	return len;
431d832c5b2SFrank Schaefer }
432d832c5b2SFrank Schaefer 
433aab3125cSMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
434aab3125cSMauro Carvalho Chehab 			     u8 **eedata, u16 *eedata_len)
4350c0d06caSMauro Carvalho Chehab {
436510e884cSFrank Schaefer 	const u16 len = 256;
437510e884cSFrank Schaefer 	/* FIXME common length/size for bytes to read, to display, hash
438510e884cSFrank Schaefer 	 * calculation and returned device dataset. Simplifies the code a lot,
439510e884cSFrank Schaefer 	 * but we might have to deal with multiple sizes in the future !      */
440d832c5b2SFrank Schaefer 	int i, err;
441510e884cSFrank Schaefer 	struct em28xx_eeprom *dev_config;
442510e884cSFrank Schaefer 	u8 buf, *data;
4430c0d06caSMauro Carvalho Chehab 
444a217968fSFrank Schaefer 	*eedata = NULL;
445510e884cSFrank Schaefer 	*eedata_len = 0;
446a217968fSFrank Schaefer 
447aab3125cSMauro Carvalho Chehab 	/* EEPROM is always on i2c bus 0 on all known devices. */
448aab3125cSMauro Carvalho Chehab 
449aab3125cSMauro Carvalho Chehab 	dev->i2c_client[bus].addr = 0xa0 >> 1;
4500c0d06caSMauro Carvalho Chehab 
4510c0d06caSMauro Carvalho Chehab 	/* Check if board has eeprom */
452aab3125cSMauro Carvalho Chehab 	err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
4530c0d06caSMauro Carvalho Chehab 	if (err < 0) {
45412d7ce18SFrank Schaefer 		em28xx_info("board has no eeprom\n");
4550c0d06caSMauro Carvalho Chehab 		return -ENODEV;
4560c0d06caSMauro Carvalho Chehab 	}
4570c0d06caSMauro Carvalho Chehab 
458a217968fSFrank Schaefer 	data = kzalloc(len, GFP_KERNEL);
459a217968fSFrank Schaefer 	if (data == NULL)
460a217968fSFrank Schaefer 		return -ENOMEM;
461a217968fSFrank Schaefer 
462d832c5b2SFrank Schaefer 	/* Read EEPROM content */
463aab3125cSMauro Carvalho Chehab 	err = em28xx_i2c_read_block(dev, bus, 0x0000,
464aab3125cSMauro Carvalho Chehab 				    dev->eeprom_addrwidth_16bit,
465a217968fSFrank Schaefer 				    len, data);
466d832c5b2SFrank Schaefer 	if (err != len) {
46712d7ce18SFrank Schaefer 		em28xx_errdev("failed to read eeprom (err=%d)\n", err);
468510e884cSFrank Schaefer 		goto error;
4690c0d06caSMauro Carvalho Chehab 	}
47090271964SFrank Schaefer 
47187b52439SFrank Schaefer 	/* Display eeprom content */
4720c0d06caSMauro Carvalho Chehab 	for (i = 0; i < len; i++) {
47387b52439SFrank Schaefer 		if (0 == (i % 16)) {
47487b52439SFrank Schaefer 			if (dev->eeprom_addrwidth_16bit)
47587b52439SFrank Schaefer 				em28xx_info("i2c eeprom %04x:", i);
47687b52439SFrank Schaefer 			else
47712d7ce18SFrank Schaefer 				em28xx_info("i2c eeprom %02x:", i);
47887b52439SFrank Schaefer 		}
479a217968fSFrank Schaefer 		printk(" %02x", data[i]);
4800c0d06caSMauro Carvalho Chehab 		if (15 == (i % 16))
4810c0d06caSMauro Carvalho Chehab 			printk("\n");
4820c0d06caSMauro Carvalho Chehab 	}
483510e884cSFrank Schaefer 	if (dev->eeprom_addrwidth_16bit)
484510e884cSFrank Schaefer 		em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
4850c0d06caSMauro Carvalho Chehab 
48687b52439SFrank Schaefer 	if (dev->eeprom_addrwidth_16bit &&
487a217968fSFrank Schaefer 	    data[0] == 0x26 && data[3] == 0x00) {
48887b52439SFrank Schaefer 		/* new eeprom format; size 4-64kb */
489510e884cSFrank Schaefer 		u16 mc_start;
490510e884cSFrank Schaefer 		u16 hwconf_offset;
491510e884cSFrank Schaefer 
492a217968fSFrank Schaefer 		dev->hash = em28xx_hash_mem(data, len, 32);
493510e884cSFrank Schaefer 		mc_start = (data[1] << 8) + 4;	/* usually 0x0004 */
494510e884cSFrank Schaefer 
495510e884cSFrank Schaefer 		em28xx_info("EEPROM ID = %02x %02x %02x %02x, "
496510e884cSFrank Schaefer 			    "EEPROM hash = 0x%08lx\n",
497510e884cSFrank Schaefer 			    data[0], data[1], data[2], data[3], dev->hash);
498510e884cSFrank Schaefer 		em28xx_info("EEPROM info:\n");
499510e884cSFrank Schaefer 		em28xx_info("\tmicrocode start address = 0x%04x, "
50087b52439SFrank Schaefer 			    "boot configuration = 0x%02x\n",
501510e884cSFrank Schaefer 			    mc_start, data[2]);
50287b52439SFrank Schaefer 		/* boot configuration (address 0x0002):
50387b52439SFrank Schaefer 		 * [0]   microcode download speed: 1 = 400 kHz; 0 = 100 kHz
50487b52439SFrank Schaefer 		 * [1]   always selects 12 kb RAM
50587b52439SFrank Schaefer 		 * [2]   USB device speed: 1 = force Full Speed; 0 = auto detect
50687b52439SFrank Schaefer 		 * [4]   1 = force fast mode and no suspend for device testing
50787b52439SFrank Schaefer 		 * [5:7] USB PHY tuning registers; determined by device
50887b52439SFrank Schaefer 		 *       characterization
50987b52439SFrank Schaefer 		 */
51087b52439SFrank Schaefer 
511510e884cSFrank Schaefer 		/* Read hardware config dataset offset from address
512510e884cSFrank Schaefer 		 * (microcode start + 46)			    */
513aab3125cSMauro Carvalho Chehab 		err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
514aab3125cSMauro Carvalho Chehab 					    data);
515510e884cSFrank Schaefer 		if (err != 2) {
516510e884cSFrank Schaefer 			em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
517510e884cSFrank Schaefer 				      err);
518510e884cSFrank Schaefer 			goto error;
519510e884cSFrank Schaefer 		}
52087b52439SFrank Schaefer 
521510e884cSFrank Schaefer 		/* Calculate hardware config dataset start address */
522510e884cSFrank Schaefer 		hwconf_offset = mc_start + data[0] + (data[1] << 8);
523510e884cSFrank Schaefer 
524510e884cSFrank Schaefer 		/* Read hardware config dataset */
525510e884cSFrank Schaefer 		/* NOTE: the microcode copy can be multiple pages long, but
526510e884cSFrank Schaefer 		 * we assume the hardware config dataset is the same as in
527510e884cSFrank Schaefer 		 * the old eeprom and not longer than 256 bytes.
528510e884cSFrank Schaefer 		 * tveeprom is currently also limited to 256 bytes.
529510e884cSFrank Schaefer 		 */
530aab3125cSMauro Carvalho Chehab 		err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
531aab3125cSMauro Carvalho Chehab 					    data);
532510e884cSFrank Schaefer 		if (err != len) {
533510e884cSFrank Schaefer 			em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
534510e884cSFrank Schaefer 				      err);
535510e884cSFrank Schaefer 			goto error;
536510e884cSFrank Schaefer 		}
537510e884cSFrank Schaefer 
538510e884cSFrank Schaefer 		/* Verify hardware config dataset */
539510e884cSFrank Schaefer 		/* NOTE: not all devices provide this type of dataset */
540510e884cSFrank Schaefer 		if (data[0] != 0x1a || data[1] != 0xeb ||
541a217968fSFrank Schaefer 		    data[2] != 0x67 || data[3] != 0x95) {
542510e884cSFrank Schaefer 			em28xx_info("\tno hardware configuration dataset found in eeprom\n");
543510e884cSFrank Schaefer 			kfree(data);
544510e884cSFrank Schaefer 			return 0;
545510e884cSFrank Schaefer 		}
546510e884cSFrank Schaefer 
547510e884cSFrank Schaefer 		/* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
548510e884cSFrank Schaefer 
549510e884cSFrank Schaefer 	} else if (!dev->eeprom_addrwidth_16bit &&
550510e884cSFrank Schaefer 		   data[0] == 0x1a && data[1] == 0xeb &&
551510e884cSFrank Schaefer 		   data[2] == 0x67 && data[3] == 0x95) {
552510e884cSFrank Schaefer 		dev->hash = em28xx_hash_mem(data, len, 32);
553510e884cSFrank Schaefer 		em28xx_info("EEPROM ID = %02x %02x %02x %02x, "
554510e884cSFrank Schaefer 			    "EEPROM hash = 0x%08lx\n",
555510e884cSFrank Schaefer 			    data[0], data[1], data[2], data[3], dev->hash);
556510e884cSFrank Schaefer 		em28xx_info("EEPROM info:\n");
557510e884cSFrank Schaefer 	} else {
55887b52439SFrank Schaefer 		em28xx_info("unknown eeprom format or eeprom corrupted !\n");
559510e884cSFrank Schaefer 		err = -ENODEV;
560510e884cSFrank Schaefer 		goto error;
561f55eacbeSFrank Schaefer 	}
562f55eacbeSFrank Schaefer 
563a217968fSFrank Schaefer 	*eedata = data;
564510e884cSFrank Schaefer 	*eedata_len = len;
565510e884cSFrank Schaefer 	dev_config = (void *)eedata;
566a217968fSFrank Schaefer 
567510e884cSFrank Schaefer 	switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
5680c0d06caSMauro Carvalho Chehab 	case 0:
56912d7ce18SFrank Schaefer 		em28xx_info("\tNo audio on board.\n");
5700c0d06caSMauro Carvalho Chehab 		break;
5710c0d06caSMauro Carvalho Chehab 	case 1:
57212d7ce18SFrank Schaefer 		em28xx_info("\tAC97 audio (5 sample rates)\n");
5730c0d06caSMauro Carvalho Chehab 		break;
5740c0d06caSMauro Carvalho Chehab 	case 2:
57512d7ce18SFrank Schaefer 		em28xx_info("\tI2S audio, sample rate=32k\n");
5760c0d06caSMauro Carvalho Chehab 		break;
5770c0d06caSMauro Carvalho Chehab 	case 3:
57812d7ce18SFrank Schaefer 		em28xx_info("\tI2S audio, 3 sample rates\n");
5790c0d06caSMauro Carvalho Chehab 		break;
5800c0d06caSMauro Carvalho Chehab 	}
5810c0d06caSMauro Carvalho Chehab 
582510e884cSFrank Schaefer 	if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
58312d7ce18SFrank Schaefer 		em28xx_info("\tUSB Remote wakeup capable\n");
5840c0d06caSMauro Carvalho Chehab 
585510e884cSFrank Schaefer 	if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
58612d7ce18SFrank Schaefer 		em28xx_info("\tUSB Self power capable\n");
5870c0d06caSMauro Carvalho Chehab 
588510e884cSFrank Schaefer 	switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
5890c0d06caSMauro Carvalho Chehab 	case 0:
59012d7ce18SFrank Schaefer 		em28xx_info("\t500mA max power\n");
5910c0d06caSMauro Carvalho Chehab 		break;
5920c0d06caSMauro Carvalho Chehab 	case 1:
59312d7ce18SFrank Schaefer 		em28xx_info("\t400mA max power\n");
5940c0d06caSMauro Carvalho Chehab 		break;
5950c0d06caSMauro Carvalho Chehab 	case 2:
59612d7ce18SFrank Schaefer 		em28xx_info("\t300mA max power\n");
5970c0d06caSMauro Carvalho Chehab 		break;
5980c0d06caSMauro Carvalho Chehab 	case 3:
59912d7ce18SFrank Schaefer 		em28xx_info("\t200mA max power\n");
6000c0d06caSMauro Carvalho Chehab 		break;
6010c0d06caSMauro Carvalho Chehab 	}
60212d7ce18SFrank Schaefer 	em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
603510e884cSFrank Schaefer 		    dev_config->string_idx_table,
604510e884cSFrank Schaefer 		    le16_to_cpu(dev_config->string1),
605510e884cSFrank Schaefer 		    le16_to_cpu(dev_config->string2),
606510e884cSFrank Schaefer 		    le16_to_cpu(dev_config->string3));
6070c0d06caSMauro Carvalho Chehab 
6080c0d06caSMauro Carvalho Chehab 	return 0;
609510e884cSFrank Schaefer 
610510e884cSFrank Schaefer error:
611510e884cSFrank Schaefer 	kfree(data);
612510e884cSFrank Schaefer 	return err;
6130c0d06caSMauro Carvalho Chehab }
6140c0d06caSMauro Carvalho Chehab 
6150c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
6160c0d06caSMauro Carvalho Chehab 
6170c0d06caSMauro Carvalho Chehab /*
6180c0d06caSMauro Carvalho Chehab  * functionality()
6190c0d06caSMauro Carvalho Chehab  */
620aab3125cSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *i2c_adap)
6210c0d06caSMauro Carvalho Chehab {
622aab3125cSMauro Carvalho Chehab 	struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
623aab3125cSMauro Carvalho Chehab 	struct em28xx *dev = i2c_bus->dev;
624aab3125cSMauro Carvalho Chehab 
625eaf33c40SFrank Schaefer 	u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
626eaf33c40SFrank Schaefer 	if (dev->board.is_em2800)
627eaf33c40SFrank Schaefer 		func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
628eaf33c40SFrank Schaefer 	return func_flags;
6290c0d06caSMauro Carvalho Chehab }
6300c0d06caSMauro Carvalho Chehab 
6310c0d06caSMauro Carvalho Chehab static struct i2c_algorithm em28xx_algo = {
6320c0d06caSMauro Carvalho Chehab 	.master_xfer   = em28xx_i2c_xfer,
6330c0d06caSMauro Carvalho Chehab 	.functionality = functionality,
6340c0d06caSMauro Carvalho Chehab };
6350c0d06caSMauro Carvalho Chehab 
6360c0d06caSMauro Carvalho Chehab static struct i2c_adapter em28xx_adap_template = {
6370c0d06caSMauro Carvalho Chehab 	.owner = THIS_MODULE,
6380c0d06caSMauro Carvalho Chehab 	.name = "em28xx",
6390c0d06caSMauro Carvalho Chehab 	.algo = &em28xx_algo,
6400c0d06caSMauro Carvalho Chehab };
6410c0d06caSMauro Carvalho Chehab 
6420c0d06caSMauro Carvalho Chehab static struct i2c_client em28xx_client_template = {
6430c0d06caSMauro Carvalho Chehab 	.name = "em28xx internal",
6440c0d06caSMauro Carvalho Chehab };
6450c0d06caSMauro Carvalho Chehab 
6460c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
6470c0d06caSMauro Carvalho Chehab 
6480c0d06caSMauro Carvalho Chehab /*
6490c0d06caSMauro Carvalho Chehab  * i2c_devs
6500c0d06caSMauro Carvalho Chehab  * incomplete list of known devices
6510c0d06caSMauro Carvalho Chehab  */
6520c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = {
6530b3966e4SFrank Schaefer 	[0x3e >> 1] = "remote IR sensor",
6540c0d06caSMauro Carvalho Chehab 	[0x4a >> 1] = "saa7113h",
6550c0d06caSMauro Carvalho Chehab 	[0x52 >> 1] = "drxk",
6560c0d06caSMauro Carvalho Chehab 	[0x60 >> 1] = "remote IR sensor",
6570c0d06caSMauro Carvalho Chehab 	[0x8e >> 1] = "remote IR sensor",
6580c0d06caSMauro Carvalho Chehab 	[0x86 >> 1] = "tda9887",
6590c0d06caSMauro Carvalho Chehab 	[0x80 >> 1] = "msp34xx",
6600c0d06caSMauro Carvalho Chehab 	[0x88 >> 1] = "msp34xx",
6610c0d06caSMauro Carvalho Chehab 	[0xa0 >> 1] = "eeprom",
6620c0d06caSMauro Carvalho Chehab 	[0xb0 >> 1] = "tda9874",
6630c0d06caSMauro Carvalho Chehab 	[0xb8 >> 1] = "tvp5150a",
6640c0d06caSMauro Carvalho Chehab 	[0xba >> 1] = "webcam sensor or tvp5150a",
6650c0d06caSMauro Carvalho Chehab 	[0xc0 >> 1] = "tuner (analog)",
6660c0d06caSMauro Carvalho Chehab 	[0xc2 >> 1] = "tuner (analog)",
6670c0d06caSMauro Carvalho Chehab 	[0xc4 >> 1] = "tuner (analog)",
6680c0d06caSMauro Carvalho Chehab 	[0xc6 >> 1] = "tuner (analog)",
6690c0d06caSMauro Carvalho Chehab };
6700c0d06caSMauro Carvalho Chehab 
6710c0d06caSMauro Carvalho Chehab /*
6720c0d06caSMauro Carvalho Chehab  * do_i2c_scan()
6730c0d06caSMauro Carvalho Chehab  * check i2c address range for devices
6740c0d06caSMauro Carvalho Chehab  */
675aab3125cSMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
6760c0d06caSMauro Carvalho Chehab {
6770c0d06caSMauro Carvalho Chehab 	u8 i2c_devicelist[128];
6780c0d06caSMauro Carvalho Chehab 	unsigned char buf;
6790c0d06caSMauro Carvalho Chehab 	int i, rc;
6800c0d06caSMauro Carvalho Chehab 
6810c0d06caSMauro Carvalho Chehab 	memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
6820c0d06caSMauro Carvalho Chehab 
6830c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
684aab3125cSMauro Carvalho Chehab 		dev->i2c_client[bus].addr = i;
685aab3125cSMauro Carvalho Chehab 		rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
6860c0d06caSMauro Carvalho Chehab 		if (rc < 0)
6870c0d06caSMauro Carvalho Chehab 			continue;
6880c0d06caSMauro Carvalho Chehab 		i2c_devicelist[i] = i;
689aab3125cSMauro Carvalho Chehab 		em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
690aab3125cSMauro Carvalho Chehab 			    i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
6910c0d06caSMauro Carvalho Chehab 	}
6920c0d06caSMauro Carvalho Chehab 
693aab3125cSMauro Carvalho Chehab 	if (bus == dev->def_i2c_bus)
6940c0d06caSMauro Carvalho Chehab 		dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
6950c0d06caSMauro Carvalho Chehab 						ARRAY_SIZE(i2c_devicelist), 32);
6960c0d06caSMauro Carvalho Chehab }
6970c0d06caSMauro Carvalho Chehab 
6980c0d06caSMauro Carvalho Chehab /*
6990c0d06caSMauro Carvalho Chehab  * em28xx_i2c_register()
7000c0d06caSMauro Carvalho Chehab  * register i2c bus
7010c0d06caSMauro Carvalho Chehab  */
702aab3125cSMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev, unsigned bus)
7030c0d06caSMauro Carvalho Chehab {
7040c0d06caSMauro Carvalho Chehab 	int retval;
7050c0d06caSMauro Carvalho Chehab 
7060c0d06caSMauro Carvalho Chehab 	BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
7070c0d06caSMauro Carvalho Chehab 	BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
7080c0d06caSMauro Carvalho Chehab 
709aab3125cSMauro Carvalho Chehab 	if (bus >= NUM_I2C_BUSES)
710aab3125cSMauro Carvalho Chehab 		return -ENODEV;
711aab3125cSMauro Carvalho Chehab 
712aab3125cSMauro Carvalho Chehab 	dev->i2c_adap[bus] = em28xx_adap_template;
713aab3125cSMauro Carvalho Chehab 	dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
714aab3125cSMauro Carvalho Chehab 	strcpy(dev->i2c_adap[bus].name, dev->name);
715aab3125cSMauro Carvalho Chehab 
716aab3125cSMauro Carvalho Chehab 	dev->i2c_bus[bus].bus = bus;
717aab3125cSMauro Carvalho Chehab 	dev->i2c_bus[bus].dev = dev;
718aab3125cSMauro Carvalho Chehab 	dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
719aab3125cSMauro Carvalho Chehab 	i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
720aab3125cSMauro Carvalho Chehab 
721aab3125cSMauro Carvalho Chehab 	retval = i2c_add_adapter(&dev->i2c_adap[bus]);
7220c0d06caSMauro Carvalho Chehab 	if (retval < 0) {
7230c0d06caSMauro Carvalho Chehab 		em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
7240c0d06caSMauro Carvalho Chehab 			__func__, retval);
7250c0d06caSMauro Carvalho Chehab 		return retval;
7260c0d06caSMauro Carvalho Chehab 	}
7270c0d06caSMauro Carvalho Chehab 
728aab3125cSMauro Carvalho Chehab 	dev->i2c_client[bus] = em28xx_client_template;
729aab3125cSMauro Carvalho Chehab 	dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
7300c0d06caSMauro Carvalho Chehab 
731aab3125cSMauro Carvalho Chehab 	/* Up to now, all eeproms are at bus 0 */
732aab3125cSMauro Carvalho Chehab 	if (!bus) {
733aab3125cSMauro Carvalho Chehab 		retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
7340c0d06caSMauro Carvalho Chehab 		if ((retval < 0) && (retval != -ENODEV)) {
7350c0d06caSMauro Carvalho Chehab 			em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
7360c0d06caSMauro Carvalho Chehab 				__func__, retval);
7370c0d06caSMauro Carvalho Chehab 
7380c0d06caSMauro Carvalho Chehab 			return retval;
7390c0d06caSMauro Carvalho Chehab 		}
740aab3125cSMauro Carvalho Chehab 	}
7410c0d06caSMauro Carvalho Chehab 
7420c0d06caSMauro Carvalho Chehab 	if (i2c_scan)
743aab3125cSMauro Carvalho Chehab 		em28xx_do_i2c_scan(dev, bus);
7440c0d06caSMauro Carvalho Chehab 
7450c0d06caSMauro Carvalho Chehab 	return 0;
7460c0d06caSMauro Carvalho Chehab }
7470c0d06caSMauro Carvalho Chehab 
7480c0d06caSMauro Carvalho Chehab /*
7490c0d06caSMauro Carvalho Chehab  * em28xx_i2c_unregister()
7500c0d06caSMauro Carvalho Chehab  * unregister i2c_bus
7510c0d06caSMauro Carvalho Chehab  */
752aab3125cSMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
7530c0d06caSMauro Carvalho Chehab {
754aab3125cSMauro Carvalho Chehab 	if (bus >= NUM_I2C_BUSES)
755aab3125cSMauro Carvalho Chehab 		return -ENODEV;
756aab3125cSMauro Carvalho Chehab 
757aab3125cSMauro Carvalho Chehab 	i2c_del_adapter(&dev->i2c_adap[bus]);
7580c0d06caSMauro Carvalho Chehab 	return 0;
7590c0d06caSMauro Carvalho Chehab }
760