1f22e9e71SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0+
2f22e9e71SMauro Carvalho Chehab //
3f22e9e71SMauro Carvalho Chehab // em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4f22e9e71SMauro Carvalho Chehab //
5f22e9e71SMauro Carvalho Chehab // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6f22e9e71SMauro Carvalho Chehab // Markus Rechberger <mrechberger@gmail.com>
732590819SMauro Carvalho Chehab // Mauro Carvalho Chehab <mchehab@kernel.org>
8f22e9e71SMauro Carvalho Chehab // Sascha Sommer <saschasommer@freenet.de>
9f22e9e71SMauro Carvalho Chehab // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
100c0d06caSMauro Carvalho Chehab
118314d402SMauro Carvalho Chehab #include "em28xx.h"
128314d402SMauro Carvalho Chehab
130c0d06caSMauro Carvalho Chehab #include <linux/module.h>
140c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
150c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
160c0d06caSMauro Carvalho Chehab #include <linux/i2c.h>
174b83626aSMauro Carvalho Chehab #include <linux/jiffies.h>
180c0d06caSMauro Carvalho Chehab
19*d76231e4SMauro Carvalho Chehab #include "xc2028.h"
200c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
210c0d06caSMauro Carvalho Chehab #include <media/tuner.h>
220c0d06caSMauro Carvalho Chehab
230c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
240c0d06caSMauro Carvalho Chehab
250c0d06caSMauro Carvalho Chehab static unsigned int i2c_scan;
260c0d06caSMauro Carvalho Chehab module_param(i2c_scan, int, 0444);
270c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
280c0d06caSMauro Carvalho Chehab
290c0d06caSMauro Carvalho Chehab static unsigned int i2c_debug;
300c0d06caSMauro Carvalho Chehab module_param(i2c_debug, int, 0644);
3150f0a9dfSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
320c0d06caSMauro Carvalho Chehab
33ce8591ffSMauro Carvalho Chehab #define dprintk(level, fmt, arg...) do { \
34ce8591ffSMauro Carvalho Chehab if (i2c_debug > level) \
3529b05e22SMauro Carvalho Chehab dev_printk(KERN_DEBUG, &dev->intf->dev, \
36ce8591ffSMauro Carvalho Chehab "i2c: %s: " fmt, __func__, ## arg); \
37ce8591ffSMauro Carvalho Chehab } while (0)
38ce8591ffSMauro Carvalho Chehab
390c0d06caSMauro Carvalho Chehab /*
40cf68c22fSMauro Carvalho Chehab * Time in msecs to wait for i2c xfers to finish.
41cf68c22fSMauro Carvalho Chehab * 35ms is the maximum time a SMBUS device could wait when
42cf68c22fSMauro Carvalho Chehab * clock stretching is used. As the transfer itself will take
43cf68c22fSMauro Carvalho Chehab * some time to happen, set it to 35 ms.
44cf68c22fSMauro Carvalho Chehab *
45cf68c22fSMauro Carvalho Chehab * Ok, I2C doesn't specify any limit. So, eventually, we may need
46cf68c22fSMauro Carvalho Chehab * to increase this timeout.
47cf68c22fSMauro Carvalho Chehab */
48cf68c22fSMauro Carvalho Chehab #define EM28XX_I2C_XFER_TIMEOUT 35 /* ms */
49cf68c22fSMauro Carvalho Chehab
em28xx_i2c_timeout(struct em28xx * dev)50cf68c22fSMauro Carvalho Chehab static int em28xx_i2c_timeout(struct em28xx *dev)
51cf68c22fSMauro Carvalho Chehab {
52cf68c22fSMauro Carvalho Chehab int time = EM28XX_I2C_XFER_TIMEOUT;
53cf68c22fSMauro Carvalho Chehab
54cf68c22fSMauro Carvalho Chehab switch (dev->i2c_speed & 0x03) {
55cf68c22fSMauro Carvalho Chehab case EM28XX_I2C_FREQ_25_KHZ:
56cf68c22fSMauro Carvalho Chehab time += 4; /* Assume 4 ms for transfers */
57cf68c22fSMauro Carvalho Chehab break;
58cf68c22fSMauro Carvalho Chehab case EM28XX_I2C_FREQ_100_KHZ:
59cf68c22fSMauro Carvalho Chehab case EM28XX_I2C_FREQ_400_KHZ:
60cf68c22fSMauro Carvalho Chehab time += 1; /* Assume 1 ms for transfers */
61cf68c22fSMauro Carvalho Chehab break;
62cf68c22fSMauro Carvalho Chehab default: /* EM28XX_I2C_FREQ_1_5_MHZ */
63cf68c22fSMauro Carvalho Chehab break;
64cf68c22fSMauro Carvalho Chehab }
65cf68c22fSMauro Carvalho Chehab
66cf68c22fSMauro Carvalho Chehab return msecs_to_jiffies(time);
67cf68c22fSMauro Carvalho Chehab }
68cf68c22fSMauro Carvalho Chehab
69cf68c22fSMauro Carvalho Chehab /*
70f5ae371aSFrank Schaefer * em2800_i2c_send_bytes()
71f5ae371aSFrank Schaefer * send up to 4 bytes to the em2800 i2c device
720c0d06caSMauro Carvalho Chehab */
em2800_i2c_send_bytes(struct em28xx * dev,u8 addr,u8 * buf,u16 len)73f5ae371aSFrank Schaefer static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
740c0d06caSMauro Carvalho Chehab {
75cf68c22fSMauro Carvalho Chehab unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
760c0d06caSMauro Carvalho Chehab int ret;
77a6bad040SFrank Schaefer u8 b2[6];
78f5ae371aSFrank Schaefer
79f5ae371aSFrank Schaefer if (len < 1 || len > 4)
80f5ae371aSFrank Schaefer return -EOPNOTSUPP;
81f5ae371aSFrank Schaefer
820c0d06caSMauro Carvalho Chehab b2[5] = 0x80 + len - 1;
830c0d06caSMauro Carvalho Chehab b2[4] = addr;
840c0d06caSMauro Carvalho Chehab b2[3] = buf[0];
850c0d06caSMauro Carvalho Chehab if (len > 1)
860c0d06caSMauro Carvalho Chehab b2[2] = buf[1];
870c0d06caSMauro Carvalho Chehab if (len > 2)
880c0d06caSMauro Carvalho Chehab b2[1] = buf[2];
890c0d06caSMauro Carvalho Chehab if (len > 3)
900c0d06caSMauro Carvalho Chehab b2[0] = buf[3];
910c0d06caSMauro Carvalho Chehab
922fcc82d8SFrank Schaefer /* trigger write */
930c0d06caSMauro Carvalho Chehab ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
940c0d06caSMauro Carvalho Chehab if (ret != 2 + len) {
9529b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
96ce8591ffSMauro Carvalho Chehab "failed to trigger write to i2c address 0x%x (error=%i)\n",
97d230d5adSFrank Schaefer addr, ret);
9845f04e82SFrank Schaefer return (ret < 0) ? ret : -EIO;
990c0d06caSMauro Carvalho Chehab }
1002fcc82d8SFrank Schaefer /* wait for completion */
1014b83626aSMauro Carvalho Chehab while (time_is_after_jiffies(timeout)) {
1020c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg(dev, 0x05);
1034b83626aSMauro Carvalho Chehab if (ret == 0x80 + len - 1)
1040c0d06caSMauro Carvalho Chehab return len;
1054b83626aSMauro Carvalho Chehab if (ret == 0x94 + len - 1) {
106ce8591ffSMauro Carvalho Chehab dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
107e63b009dSMauro Carvalho Chehab return -ENXIO;
1084b83626aSMauro Carvalho Chehab }
1094b83626aSMauro Carvalho Chehab if (ret < 0) {
11029b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
111ce8591ffSMauro Carvalho Chehab "failed to get i2c transfer status from bridge register (error=%i)\n",
112d230d5adSFrank Schaefer ret);
11345f04e82SFrank Schaefer return ret;
11445f04e82SFrank Schaefer }
1158adbc7d6SMauro Carvalho Chehab usleep_range(5000, 6000);
1160c0d06caSMauro Carvalho Chehab }
117ce8591ffSMauro Carvalho Chehab dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
118e63b009dSMauro Carvalho Chehab return -ETIMEDOUT;
1190c0d06caSMauro Carvalho Chehab }
1200c0d06caSMauro Carvalho Chehab
1210c0d06caSMauro Carvalho Chehab /*
1220c0d06caSMauro Carvalho Chehab * em2800_i2c_recv_bytes()
1232fcc82d8SFrank Schaefer * read up to 4 bytes from the em2800 i2c device
1240c0d06caSMauro Carvalho Chehab */
em2800_i2c_recv_bytes(struct em28xx * dev,u8 addr,u8 * buf,u16 len)125a6bad040SFrank Schaefer static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
1260c0d06caSMauro Carvalho Chehab {
127cf68c22fSMauro Carvalho Chehab unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
1282fcc82d8SFrank Schaefer u8 buf2[4];
1290c0d06caSMauro Carvalho Chehab int ret;
1302fcc82d8SFrank Schaefer int i;
131f5ae371aSFrank Schaefer
132f5ae371aSFrank Schaefer if (len < 1 || len > 4)
133f5ae371aSFrank Schaefer return -EOPNOTSUPP;
134f5ae371aSFrank Schaefer
1352fcc82d8SFrank Schaefer /* trigger read */
1362fcc82d8SFrank Schaefer buf2[1] = 0x84 + len - 1;
1372fcc82d8SFrank Schaefer buf2[0] = addr;
1382fcc82d8SFrank Schaefer ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
1392fcc82d8SFrank Schaefer if (ret != 2) {
14029b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
141ce8591ffSMauro Carvalho Chehab "failed to trigger read from i2c address 0x%x (error=%i)\n",
142d230d5adSFrank Schaefer addr, ret);
1432fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO;
1442fcc82d8SFrank Schaefer }
1452fcc82d8SFrank Schaefer
1462fcc82d8SFrank Schaefer /* wait for completion */
1474b83626aSMauro Carvalho Chehab while (time_is_after_jiffies(timeout)) {
1482fcc82d8SFrank Schaefer ret = dev->em28xx_read_reg(dev, 0x05);
1494b83626aSMauro Carvalho Chehab if (ret == 0x84 + len - 1)
1502fcc82d8SFrank Schaefer break;
1514b83626aSMauro Carvalho Chehab if (ret == 0x94 + len - 1) {
152ce8591ffSMauro Carvalho Chehab dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
153d845fb3aSMauro Carvalho Chehab ret);
154e63b009dSMauro Carvalho Chehab return -ENXIO;
1554b83626aSMauro Carvalho Chehab }
1564b83626aSMauro Carvalho Chehab if (ret < 0) {
15729b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
158ce8591ffSMauro Carvalho Chehab "failed to get i2c transfer status from bridge register (error=%i)\n",
159d230d5adSFrank Schaefer ret);
1600c0d06caSMauro Carvalho Chehab return ret;
1610c0d06caSMauro Carvalho Chehab }
1628adbc7d6SMauro Carvalho Chehab usleep_range(5000, 6000);
1632fcc82d8SFrank Schaefer }
1648adbc7d6SMauro Carvalho Chehab if (ret != 0x84 + len - 1)
165ce8591ffSMauro Carvalho Chehab dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
1662fcc82d8SFrank Schaefer
1672fcc82d8SFrank Schaefer /* get the received message */
1682fcc82d8SFrank Schaefer ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4 - len, buf2, len);
1692fcc82d8SFrank Schaefer if (ret != len) {
17029b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
171ce8591ffSMauro Carvalho Chehab "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
172d230d5adSFrank Schaefer addr, ret);
1732fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO;
1742fcc82d8SFrank Schaefer }
1752fcc82d8SFrank Schaefer for (i = 0; i < len; i++)
1762fcc82d8SFrank Schaefer buf[i] = buf2[len - 1 - i];
1772fcc82d8SFrank Schaefer
1780c0d06caSMauro Carvalho Chehab return ret;
1790c0d06caSMauro Carvalho Chehab }
1802fcc82d8SFrank Schaefer
1812fcc82d8SFrank Schaefer /*
1822fcc82d8SFrank Schaefer * em2800_i2c_check_for_device()
1832fcc82d8SFrank Schaefer * check if there is an i2c device at the supplied address
1842fcc82d8SFrank Schaefer */
em2800_i2c_check_for_device(struct em28xx * dev,u8 addr)1852fcc82d8SFrank Schaefer static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
1862fcc82d8SFrank Schaefer {
1872fcc82d8SFrank Schaefer u8 buf;
1882fcc82d8SFrank Schaefer int ret;
1892fcc82d8SFrank Schaefer
1902fcc82d8SFrank Schaefer ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
1912fcc82d8SFrank Schaefer if (ret == 1)
1922fcc82d8SFrank Schaefer return 0;
1932fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO;
1940c0d06caSMauro Carvalho Chehab }
1950c0d06caSMauro Carvalho Chehab
1960c0d06caSMauro Carvalho Chehab /*
1970c0d06caSMauro Carvalho Chehab * em28xx_i2c_send_bytes()
1980c0d06caSMauro Carvalho Chehab */
em28xx_i2c_send_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len,int stop)199a6bad040SFrank Schaefer static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
200a6bad040SFrank Schaefer u16 len, int stop)
2010c0d06caSMauro Carvalho Chehab {
202cf68c22fSMauro Carvalho Chehab unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
2034b83626aSMauro Carvalho Chehab int ret;
2040c0d06caSMauro Carvalho Chehab
205f5ae371aSFrank Schaefer if (len < 1 || len > 64)
206f5ae371aSFrank Schaefer return -EOPNOTSUPP;
207fa74aca3SFrank Schaefer /*
208fa74aca3SFrank Schaefer * NOTE: limited by the USB ctrl message constraints
209fa74aca3SFrank Schaefer * Zero length reads always succeed, even if no device is connected
210fa74aca3SFrank Schaefer */
211f5ae371aSFrank Schaefer
21245f04e82SFrank Schaefer /* Write to i2c device */
21345f04e82SFrank Schaefer ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
21445f04e82SFrank Schaefer if (ret != len) {
21545f04e82SFrank Schaefer if (ret < 0) {
21629b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
217ce8591ffSMauro Carvalho Chehab "writing to i2c device at 0x%x failed (error=%i)\n",
218d230d5adSFrank Schaefer addr, ret);
21945f04e82SFrank Schaefer return ret;
2208adbc7d6SMauro Carvalho Chehab }
22129b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
222ce8591ffSMauro Carvalho Chehab "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
22345f04e82SFrank Schaefer len, addr, ret);
22445f04e82SFrank Schaefer return -EIO;
22545f04e82SFrank Schaefer }
2260c0d06caSMauro Carvalho Chehab
2274b83626aSMauro Carvalho Chehab /* wait for completion */
2284b83626aSMauro Carvalho Chehab while (time_is_after_jiffies(timeout)) {
2290c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg(dev, 0x05);
2304b83626aSMauro Carvalho Chehab if (ret == 0) /* success */
23145f04e82SFrank Schaefer return len;
2324b83626aSMauro Carvalho Chehab if (ret == 0x10) {
233ce8591ffSMauro Carvalho Chehab dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
234d845fb3aSMauro Carvalho Chehab addr);
235e63b009dSMauro Carvalho Chehab return -ENXIO;
2364b83626aSMauro Carvalho Chehab }
2374b83626aSMauro Carvalho Chehab if (ret < 0) {
23829b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
239ce8591ffSMauro Carvalho Chehab "failed to get i2c transfer status from bridge register (error=%i)\n",
240d230d5adSFrank Schaefer ret);
24145f04e82SFrank Schaefer return ret;
2420c0d06caSMauro Carvalho Chehab }
2438adbc7d6SMauro Carvalho Chehab usleep_range(5000, 6000);
244fa74aca3SFrank Schaefer /*
245fa74aca3SFrank Schaefer * NOTE: do we really have to wait for success ?
246fa74aca3SFrank Schaefer * Never seen anything else than 0x00 or 0x10
247fa74aca3SFrank Schaefer * (even with high payload) ...
248fa74aca3SFrank Schaefer */
24945f04e82SFrank Schaefer }
250123a17d1SFrank Schaefer
251123a17d1SFrank Schaefer if (ret == 0x02 || ret == 0x04) {
252123a17d1SFrank Schaefer /* NOTE: these errors seem to be related to clock stretching */
253ce8591ffSMauro Carvalho Chehab dprintk(0,
254ce8591ffSMauro Carvalho Chehab "write to i2c device at 0x%x timed out (status=%i)\n",
25550f0a9dfSMauro Carvalho Chehab addr, ret);
256e63b009dSMauro Carvalho Chehab return -ETIMEDOUT;
2570c0d06caSMauro Carvalho Chehab }
2580c0d06caSMauro Carvalho Chehab
25929b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
260ce8591ffSMauro Carvalho Chehab "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
261123a17d1SFrank Schaefer addr, ret);
262123a17d1SFrank Schaefer return -EIO;
263123a17d1SFrank Schaefer }
264123a17d1SFrank Schaefer
2650c0d06caSMauro Carvalho Chehab /*
2660c0d06caSMauro Carvalho Chehab * em28xx_i2c_recv_bytes()
2670c0d06caSMauro Carvalho Chehab * read a byte from the i2c device
2680c0d06caSMauro Carvalho Chehab */
em28xx_i2c_recv_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)269a6bad040SFrank Schaefer static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
2700c0d06caSMauro Carvalho Chehab {
2710c0d06caSMauro Carvalho Chehab int ret;
272f5ae371aSFrank Schaefer
273f5ae371aSFrank Schaefer if (len < 1 || len > 64)
274f5ae371aSFrank Schaefer return -EOPNOTSUPP;
275fa74aca3SFrank Schaefer /*
276fa74aca3SFrank Schaefer * NOTE: limited by the USB ctrl message constraints
277fa74aca3SFrank Schaefer * Zero length reads always succeed, even if no device is connected
278fa74aca3SFrank Schaefer */
279f5ae371aSFrank Schaefer
28045f04e82SFrank Schaefer /* Read data from i2c device */
2810c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
2820c0d06caSMauro Carvalho Chehab if (ret < 0) {
28329b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
284ce8591ffSMauro Carvalho Chehab "reading from i2c device at 0x%x failed (error=%i)\n",
2857f6301d1SFrank Schaefer addr, ret);
28645f04e82SFrank Schaefer return ret;
2871970105aSBrad Love } else if (ret != len) {
2881970105aSBrad Love dev_dbg(&dev->intf->dev,
2891970105aSBrad Love "%i bytes read from i2c device at 0x%x requested, but %i bytes written\n",
2901970105aSBrad Love ret, addr, len);
29145f04e82SFrank Schaefer }
292fa74aca3SFrank Schaefer /*
2933e4d8f48SMauro Carvalho Chehab * NOTE: some devices with two i2c buses have the bad habit to return 0
2947f6301d1SFrank Schaefer * bytes if we are on bus B AND there was no write attempt to the
2957f6301d1SFrank Schaefer * specified slave address before AND no device is present at the
2967f6301d1SFrank Schaefer * requested slave address.
297e63b009dSMauro Carvalho Chehab * Anyway, the next check will fail with -ENXIO in this case, so avoid
2987f6301d1SFrank Schaefer * spamming the system log on device probing and do nothing here.
2997f6301d1SFrank Schaefer */
30045f04e82SFrank Schaefer
30145f04e82SFrank Schaefer /* Check success of the i2c operation */
30245f04e82SFrank Schaefer ret = dev->em28xx_read_reg(dev, 0x05);
3034b83626aSMauro Carvalho Chehab if (ret == 0) /* success */
3044b83626aSMauro Carvalho Chehab return len;
30545f04e82SFrank Schaefer if (ret < 0) {
30629b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
307ce8591ffSMauro Carvalho Chehab "failed to get i2c transfer status from bridge register (error=%i)\n",
308d230d5adSFrank Schaefer ret);
3090c0d06caSMauro Carvalho Chehab return ret;
3100c0d06caSMauro Carvalho Chehab }
311d845fb3aSMauro Carvalho Chehab if (ret == 0x10) {
312ce8591ffSMauro Carvalho Chehab dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
313d845fb3aSMauro Carvalho Chehab addr);
314e63b009dSMauro Carvalho Chehab return -ENXIO;
315d845fb3aSMauro Carvalho Chehab }
3164b83626aSMauro Carvalho Chehab
317123a17d1SFrank Schaefer if (ret == 0x02 || ret == 0x04) {
318123a17d1SFrank Schaefer /* NOTE: these errors seem to be related to clock stretching */
319ce8591ffSMauro Carvalho Chehab dprintk(0,
320ce8591ffSMauro Carvalho Chehab "write to i2c device at 0x%x timed out (status=%i)\n",
321123a17d1SFrank Schaefer addr, ret);
322e63b009dSMauro Carvalho Chehab return -ETIMEDOUT;
32345f04e82SFrank Schaefer }
3240c0d06caSMauro Carvalho Chehab
32529b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
3261970105aSBrad Love "read from i2c device at 0x%x failed with unknown error (status=%i)\n",
327123a17d1SFrank Schaefer addr, ret);
328123a17d1SFrank Schaefer return -EIO;
329123a17d1SFrank Schaefer }
330123a17d1SFrank Schaefer
3310c0d06caSMauro Carvalho Chehab /*
3320c0d06caSMauro Carvalho Chehab * em28xx_i2c_check_for_device()
3330c0d06caSMauro Carvalho Chehab * check if there is a i2c_device at the supplied address
3340c0d06caSMauro Carvalho Chehab */
em28xx_i2c_check_for_device(struct em28xx * dev,u16 addr)335a6bad040SFrank Schaefer static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
3360c0d06caSMauro Carvalho Chehab {
3370c0d06caSMauro Carvalho Chehab int ret;
33845f04e82SFrank Schaefer u8 buf;
3390c0d06caSMauro Carvalho Chehab
34045f04e82SFrank Schaefer ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
34145f04e82SFrank Schaefer if (ret == 1)
3420c0d06caSMauro Carvalho Chehab return 0;
34345f04e82SFrank Schaefer return (ret < 0) ? ret : -EIO;
3440c0d06caSMauro Carvalho Chehab }
3450c0d06caSMauro Carvalho Chehab
3460c0d06caSMauro Carvalho Chehab /*
347a3ea4bf9SFrank Schaefer * em25xx_bus_B_send_bytes
348a3ea4bf9SFrank Schaefer * write bytes to the i2c device
349a3ea4bf9SFrank Schaefer */
em25xx_bus_B_send_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)350a3ea4bf9SFrank Schaefer static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
351a3ea4bf9SFrank Schaefer u16 len)
352a3ea4bf9SFrank Schaefer {
353a3ea4bf9SFrank Schaefer int ret;
354a3ea4bf9SFrank Schaefer
355a3ea4bf9SFrank Schaefer if (len < 1 || len > 64)
356a3ea4bf9SFrank Schaefer return -EOPNOTSUPP;
357a3ea4bf9SFrank Schaefer /*
358a3ea4bf9SFrank Schaefer * NOTE: limited by the USB ctrl message constraints
359a3ea4bf9SFrank Schaefer * Zero length reads always succeed, even if no device is connected
360a3ea4bf9SFrank Schaefer */
361a3ea4bf9SFrank Schaefer
362a3ea4bf9SFrank Schaefer /* Set register and write value */
363a3ea4bf9SFrank Schaefer ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
364a3ea4bf9SFrank Schaefer if (ret != len) {
365a3ea4bf9SFrank Schaefer if (ret < 0) {
36629b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
367ce8591ffSMauro Carvalho Chehab "writing to i2c device at 0x%x failed (error=%i)\n",
368a3ea4bf9SFrank Schaefer addr, ret);
369a3ea4bf9SFrank Schaefer return ret;
3708adbc7d6SMauro Carvalho Chehab }
3718adbc7d6SMauro Carvalho Chehab
37229b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
373ce8591ffSMauro Carvalho Chehab "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
374a3ea4bf9SFrank Schaefer len, addr, ret);
375a3ea4bf9SFrank Schaefer return -EIO;
376a3ea4bf9SFrank Schaefer }
377a3ea4bf9SFrank Schaefer /* Check success */
378a3ea4bf9SFrank Schaefer ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
379a3ea4bf9SFrank Schaefer /*
380a3ea4bf9SFrank Schaefer * NOTE: the only error we've seen so far is
381a3ea4bf9SFrank Schaefer * 0x01 when the slave device is not present
382a3ea4bf9SFrank Schaefer */
383a3ea4bf9SFrank Schaefer if (!ret)
384a3ea4bf9SFrank Schaefer return len;
3858adbc7d6SMauro Carvalho Chehab
3868adbc7d6SMauro Carvalho Chehab if (ret > 0) {
387ce8591ffSMauro Carvalho Chehab dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
388e63b009dSMauro Carvalho Chehab return -ENXIO;
389d845fb3aSMauro Carvalho Chehab }
390a3ea4bf9SFrank Schaefer
391a3ea4bf9SFrank Schaefer return ret;
392a3ea4bf9SFrank Schaefer /*
393a3ea4bf9SFrank Schaefer * NOTE: With chip types (other chip IDs) which actually don't support
394a3ea4bf9SFrank Schaefer * this operation, it seems to succeed ALWAYS ! (even if there is no
395a3ea4bf9SFrank Schaefer * slave device or even no second i2c bus provided)
396a3ea4bf9SFrank Schaefer */
397a3ea4bf9SFrank Schaefer }
398a3ea4bf9SFrank Schaefer
399a3ea4bf9SFrank Schaefer /*
400a3ea4bf9SFrank Schaefer * em25xx_bus_B_recv_bytes
401a3ea4bf9SFrank Schaefer * read bytes from the i2c device
402a3ea4bf9SFrank Schaefer */
em25xx_bus_B_recv_bytes(struct em28xx * dev,u16 addr,u8 * buf,u16 len)403a3ea4bf9SFrank Schaefer static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
404a3ea4bf9SFrank Schaefer u16 len)
405a3ea4bf9SFrank Schaefer {
406a3ea4bf9SFrank Schaefer int ret;
407a3ea4bf9SFrank Schaefer
408a3ea4bf9SFrank Schaefer if (len < 1 || len > 64)
409a3ea4bf9SFrank Schaefer return -EOPNOTSUPP;
410a3ea4bf9SFrank Schaefer /*
411a3ea4bf9SFrank Schaefer * NOTE: limited by the USB ctrl message constraints
412a3ea4bf9SFrank Schaefer * Zero length reads always succeed, even if no device is connected
413a3ea4bf9SFrank Schaefer */
414a3ea4bf9SFrank Schaefer
415a3ea4bf9SFrank Schaefer /* Read value */
416a3ea4bf9SFrank Schaefer ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
417a3ea4bf9SFrank Schaefer if (ret < 0) {
41829b05e22SMauro Carvalho Chehab dev_warn(&dev->intf->dev,
419ce8591ffSMauro Carvalho Chehab "reading from i2c device at 0x%x failed (error=%i)\n",
420a3ea4bf9SFrank Schaefer addr, ret);
421a3ea4bf9SFrank Schaefer return ret;
422a3ea4bf9SFrank Schaefer }
423a3ea4bf9SFrank Schaefer /*
4243e4d8f48SMauro Carvalho Chehab * NOTE: some devices with two i2c buses have the bad habit to return 0
425a3ea4bf9SFrank Schaefer * bytes if we are on bus B AND there was no write attempt to the
426a3ea4bf9SFrank Schaefer * specified slave address before AND no device is present at the
427a3ea4bf9SFrank Schaefer * requested slave address.
428e63b009dSMauro Carvalho Chehab * Anyway, the next check will fail with -ENXIO in this case, so avoid
429a3ea4bf9SFrank Schaefer * spamming the system log on device probing and do nothing here.
430a3ea4bf9SFrank Schaefer */
431a3ea4bf9SFrank Schaefer
432a3ea4bf9SFrank Schaefer /* Check success */
433a3ea4bf9SFrank Schaefer ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
434a3ea4bf9SFrank Schaefer /*
435a3ea4bf9SFrank Schaefer * NOTE: the only error we've seen so far is
436a3ea4bf9SFrank Schaefer * 0x01 when the slave device is not present
437a3ea4bf9SFrank Schaefer */
438a3ea4bf9SFrank Schaefer if (!ret)
439a3ea4bf9SFrank Schaefer return len;
4408adbc7d6SMauro Carvalho Chehab
4418adbc7d6SMauro Carvalho Chehab if (ret > 0) {
442ce8591ffSMauro Carvalho Chehab dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
443e63b009dSMauro Carvalho Chehab return -ENXIO;
444d845fb3aSMauro Carvalho Chehab }
445a3ea4bf9SFrank Schaefer
446a3ea4bf9SFrank Schaefer return ret;
447a3ea4bf9SFrank Schaefer /*
448a3ea4bf9SFrank Schaefer * NOTE: With chip types (other chip IDs) which actually don't support
449a3ea4bf9SFrank Schaefer * this operation, it seems to succeed ALWAYS ! (even if there is no
450a3ea4bf9SFrank Schaefer * slave device or even no second i2c bus provided)
451a3ea4bf9SFrank Schaefer */
452a3ea4bf9SFrank Schaefer }
453a3ea4bf9SFrank Schaefer
454a3ea4bf9SFrank Schaefer /*
455a3ea4bf9SFrank Schaefer * em25xx_bus_B_check_for_device()
456a3ea4bf9SFrank Schaefer * check if there is a i2c device at the supplied address
457a3ea4bf9SFrank Schaefer */
em25xx_bus_B_check_for_device(struct em28xx * dev,u16 addr)458a3ea4bf9SFrank Schaefer static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
459a3ea4bf9SFrank Schaefer {
460a3ea4bf9SFrank Schaefer u8 buf;
461a3ea4bf9SFrank Schaefer int ret;
462a3ea4bf9SFrank Schaefer
463a3ea4bf9SFrank Schaefer ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
464a3ea4bf9SFrank Schaefer if (ret < 0)
465a3ea4bf9SFrank Schaefer return ret;
466a3ea4bf9SFrank Schaefer
467a3ea4bf9SFrank Schaefer return 0;
468a3ea4bf9SFrank Schaefer /*
469a3ea4bf9SFrank Schaefer * NOTE: With chips which do not support this operation,
470a3ea4bf9SFrank Schaefer * it seems to succeed ALWAYS ! (even if no device connected)
471a3ea4bf9SFrank Schaefer */
472a3ea4bf9SFrank Schaefer }
473a3ea4bf9SFrank Schaefer
i2c_check_for_device(struct em28xx_i2c_bus * i2c_bus,u16 addr)474a3ea4bf9SFrank Schaefer static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
475a3ea4bf9SFrank Schaefer {
476a3ea4bf9SFrank Schaefer struct em28xx *dev = i2c_bus->dev;
477a3ea4bf9SFrank Schaefer int rc = -EOPNOTSUPP;
478a3ea4bf9SFrank Schaefer
479a3ea4bf9SFrank Schaefer if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
480a3ea4bf9SFrank Schaefer rc = em28xx_i2c_check_for_device(dev, addr);
481a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
482a3ea4bf9SFrank Schaefer rc = em2800_i2c_check_for_device(dev, addr);
483a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
484a3ea4bf9SFrank Schaefer rc = em25xx_bus_B_check_for_device(dev, addr);
485a3ea4bf9SFrank Schaefer return rc;
486a3ea4bf9SFrank Schaefer }
487a3ea4bf9SFrank Schaefer
i2c_recv_bytes(struct em28xx_i2c_bus * i2c_bus,struct i2c_msg msg)488a3ea4bf9SFrank Schaefer static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
489a3ea4bf9SFrank Schaefer struct i2c_msg msg)
490a3ea4bf9SFrank Schaefer {
491a3ea4bf9SFrank Schaefer struct em28xx *dev = i2c_bus->dev;
492a3ea4bf9SFrank Schaefer u16 addr = msg.addr << 1;
49350f0a9dfSMauro Carvalho Chehab int rc = -EOPNOTSUPP;
494a3ea4bf9SFrank Schaefer
495a3ea4bf9SFrank Schaefer if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
496a3ea4bf9SFrank Schaefer rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
497a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
498a3ea4bf9SFrank Schaefer rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
499a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
500a3ea4bf9SFrank Schaefer rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
501a3ea4bf9SFrank Schaefer return rc;
502a3ea4bf9SFrank Schaefer }
503a3ea4bf9SFrank Schaefer
i2c_send_bytes(struct em28xx_i2c_bus * i2c_bus,struct i2c_msg msg,int stop)504a3ea4bf9SFrank Schaefer static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
505a3ea4bf9SFrank Schaefer struct i2c_msg msg, int stop)
506a3ea4bf9SFrank Schaefer {
507a3ea4bf9SFrank Schaefer struct em28xx *dev = i2c_bus->dev;
508a3ea4bf9SFrank Schaefer u16 addr = msg.addr << 1;
50950f0a9dfSMauro Carvalho Chehab int rc = -EOPNOTSUPP;
510a3ea4bf9SFrank Schaefer
511a3ea4bf9SFrank Schaefer if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
512a3ea4bf9SFrank Schaefer rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
513a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
514a3ea4bf9SFrank Schaefer rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
515a3ea4bf9SFrank Schaefer else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
516a3ea4bf9SFrank Schaefer rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
517a3ea4bf9SFrank Schaefer return rc;
518a3ea4bf9SFrank Schaefer }
519a3ea4bf9SFrank Schaefer
520a3ea4bf9SFrank Schaefer /*
5210c0d06caSMauro Carvalho Chehab * em28xx_i2c_xfer()
5220c0d06caSMauro Carvalho Chehab * the main i2c transfer function
5230c0d06caSMauro Carvalho Chehab */
em28xx_i2c_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg msgs[],int num)5240c0d06caSMauro Carvalho Chehab static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
5250c0d06caSMauro Carvalho Chehab struct i2c_msg msgs[], int num)
5260c0d06caSMauro Carvalho Chehab {
527aab3125cSMauro Carvalho Chehab struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
528aab3125cSMauro Carvalho Chehab struct em28xx *dev = i2c_bus->dev;
5298adbc7d6SMauro Carvalho Chehab unsigned int bus = i2c_bus->bus;
530a3ea4bf9SFrank Schaefer int addr, rc, i;
5313190fbeeSMauro Carvalho Chehab u8 reg;
5320c0d06caSMauro Carvalho Chehab
5338adbc7d6SMauro Carvalho Chehab /*
5348adbc7d6SMauro Carvalho Chehab * prevent i2c xfer attempts after device is disconnected
5358adbc7d6SMauro Carvalho Chehab * some fe's try to do i2c writes/reads from their release
5368adbc7d6SMauro Carvalho Chehab * interfaces when called in disconnect path
5378adbc7d6SMauro Carvalho Chehab */
538cc5c5d20SShuah Khan if (dev->disconnected)
539cc5c5d20SShuah Khan return -ENODEV;
540cc5c5d20SShuah Khan
541e44c153bSDan Carpenter if (!rt_mutex_trylock(&dev->i2c_bus_lock))
542e44c153bSDan Carpenter return -EAGAIN;
543aab3125cSMauro Carvalho Chehab
544aab3125cSMauro Carvalho Chehab /* Switch I2C bus if needed */
545a3ea4bf9SFrank Schaefer if (bus != dev->cur_i2c_bus &&
546a3ea4bf9SFrank Schaefer i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
547aab3125cSMauro Carvalho Chehab if (bus == 1)
5483190fbeeSMauro Carvalho Chehab reg = EM2874_I2C_SECONDARY_BUS_SELECT;
549aab3125cSMauro Carvalho Chehab else
5503190fbeeSMauro Carvalho Chehab reg = 0;
5513190fbeeSMauro Carvalho Chehab em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
5523190fbeeSMauro Carvalho Chehab EM2874_I2C_SECONDARY_BUS_SELECT);
553aab3125cSMauro Carvalho Chehab dev->cur_i2c_bus = bus;
554aab3125cSMauro Carvalho Chehab }
555aab3125cSMauro Carvalho Chehab
5560c0d06caSMauro Carvalho Chehab for (i = 0; i < num; i++) {
5570c0d06caSMauro Carvalho Chehab addr = msgs[i].addr << 1;
558e63b009dSMauro Carvalho Chehab if (!msgs[i].len) {
559e63b009dSMauro Carvalho Chehab /*
560e63b009dSMauro Carvalho Chehab * no len: check only for device presence
561e63b009dSMauro Carvalho Chehab * This code is only called during device probe.
562e63b009dSMauro Carvalho Chehab */
563a3ea4bf9SFrank Schaefer rc = i2c_check_for_device(i2c_bus, addr);
564ce8591ffSMauro Carvalho Chehab
565ce8591ffSMauro Carvalho Chehab if (rc == -ENXIO)
56650f0a9dfSMauro Carvalho Chehab rc = -ENODEV;
5670c0d06caSMauro Carvalho Chehab } else if (msgs[i].flags & I2C_M_RD) {
5680c0d06caSMauro Carvalho Chehab /* read bytes */
569a3ea4bf9SFrank Schaefer rc = i2c_recv_bytes(i2c_bus, msgs[i]);
5700c0d06caSMauro Carvalho Chehab } else {
5710c0d06caSMauro Carvalho Chehab /* write bytes */
572a3ea4bf9SFrank Schaefer rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
5730c0d06caSMauro Carvalho Chehab }
574ce8591ffSMauro Carvalho Chehab
575ce8591ffSMauro Carvalho Chehab if (rc < 0)
576ce8591ffSMauro Carvalho Chehab goto error;
577ce8591ffSMauro Carvalho Chehab
578ce8591ffSMauro Carvalho Chehab dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
579ce8591ffSMauro Carvalho Chehab (msgs[i].flags & I2C_M_RD) ? "read" : "write",
580ce8591ffSMauro Carvalho Chehab i == num - 1 ? "stop" : "nonstop",
581ce8591ffSMauro Carvalho Chehab addr, msgs[i].len,
582ce8591ffSMauro Carvalho Chehab msgs[i].len, msgs[i].buf);
5830c0d06caSMauro Carvalho Chehab }
5840c0d06caSMauro Carvalho Chehab
585aab3125cSMauro Carvalho Chehab rt_mutex_unlock(&dev->i2c_bus_lock);
5860c0d06caSMauro Carvalho Chehab return num;
587ce8591ffSMauro Carvalho Chehab
588ce8591ffSMauro Carvalho Chehab error:
589ce8591ffSMauro Carvalho Chehab dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
590ce8591ffSMauro Carvalho Chehab (msgs[i].flags & I2C_M_RD) ? "read" : "write",
591ce8591ffSMauro Carvalho Chehab i == num - 1 ? "stop" : "nonstop",
592ce8591ffSMauro Carvalho Chehab addr, msgs[i].len,
593ce8591ffSMauro Carvalho Chehab (rc == -ENODEV) ? "no device " : "",
594ce8591ffSMauro Carvalho Chehab rc);
595ce8591ffSMauro Carvalho Chehab
596ce8591ffSMauro Carvalho Chehab rt_mutex_unlock(&dev->i2c_bus_lock);
597ce8591ffSMauro Carvalho Chehab return rc;
5980c0d06caSMauro Carvalho Chehab }
5990c0d06caSMauro Carvalho Chehab
600fa74aca3SFrank Schaefer /*
601fa74aca3SFrank Schaefer * based on linux/sunrpc/svcauth.h and linux/hash.h
6020c0d06caSMauro Carvalho Chehab * The original hash function returns a different value, if arch is x86_64
6030c0d06caSMauro Carvalho Chehab * or i386.
6040c0d06caSMauro Carvalho Chehab */
em28xx_hash_mem(char * buf,int length,int bits)6050c0d06caSMauro Carvalho Chehab static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
6060c0d06caSMauro Carvalho Chehab {
6070c0d06caSMauro Carvalho Chehab unsigned long hash = 0;
6080c0d06caSMauro Carvalho Chehab unsigned long l = 0;
6090c0d06caSMauro Carvalho Chehab int len = 0;
6100c0d06caSMauro Carvalho Chehab unsigned char c;
611fdf1bc9fSMauro Carvalho Chehab
6120c0d06caSMauro Carvalho Chehab do {
6130c0d06caSMauro Carvalho Chehab if (len == length) {
6140c0d06caSMauro Carvalho Chehab c = (char)len;
6150c0d06caSMauro Carvalho Chehab len = -1;
6168adbc7d6SMauro Carvalho Chehab } else {
6170c0d06caSMauro Carvalho Chehab c = *buf++;
6188adbc7d6SMauro Carvalho Chehab }
6190c0d06caSMauro Carvalho Chehab l = (l << 8) | c;
6200c0d06caSMauro Carvalho Chehab len++;
6210c0d06caSMauro Carvalho Chehab if ((len & (32 / 8 - 1)) == 0)
6220c0d06caSMauro Carvalho Chehab hash = ((hash ^ l) * 0x9e370001UL);
6230c0d06caSMauro Carvalho Chehab } while (len);
6240c0d06caSMauro Carvalho Chehab
6250c0d06caSMauro Carvalho Chehab return (hash >> (32 - bits)) & 0xffffffffUL;
6260c0d06caSMauro Carvalho Chehab }
6270c0d06caSMauro Carvalho Chehab
628fa74aca3SFrank Schaefer /*
629fa74aca3SFrank Schaefer * Helper function to read data blocks from i2c clients with 8 or 16 bit
630fa74aca3SFrank Schaefer * address width, 8 bit register width and auto incrementation been activated
631fa74aca3SFrank Schaefer */
em28xx_i2c_read_block(struct em28xx * dev,unsigned int bus,u16 addr,bool addr_w16,u16 len,u8 * data)6328adbc7d6SMauro Carvalho Chehab static int em28xx_i2c_read_block(struct em28xx *dev, unsigned int bus, u16 addr,
633aab3125cSMauro Carvalho Chehab bool addr_w16, u16 len, u8 *data)
634d832c5b2SFrank Schaefer {
635d832c5b2SFrank Schaefer int remain = len, rsize, rsize_max, ret;
636d832c5b2SFrank Schaefer u8 buf[2];
637d832c5b2SFrank Schaefer
638d832c5b2SFrank Schaefer /* Sanity check */
639d832c5b2SFrank Schaefer if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
640d832c5b2SFrank Schaefer return -EINVAL;
641d832c5b2SFrank Schaefer /* Select address */
642d832c5b2SFrank Schaefer buf[0] = addr >> 8;
643d832c5b2SFrank Schaefer buf[1] = addr & 0xff;
6448adbc7d6SMauro Carvalho Chehab ret = i2c_master_send(&dev->i2c_client[bus],
6458adbc7d6SMauro Carvalho Chehab buf + !addr_w16, 1 + addr_w16);
646d832c5b2SFrank Schaefer if (ret < 0)
647d832c5b2SFrank Schaefer return ret;
648d832c5b2SFrank Schaefer /* Read data */
649d832c5b2SFrank Schaefer if (dev->board.is_em2800)
650d832c5b2SFrank Schaefer rsize_max = 4;
651d832c5b2SFrank Schaefer else
652d832c5b2SFrank Schaefer rsize_max = 64;
653d832c5b2SFrank Schaefer while (remain > 0) {
654d832c5b2SFrank Schaefer if (remain > rsize_max)
655d832c5b2SFrank Schaefer rsize = rsize_max;
656d832c5b2SFrank Schaefer else
657d832c5b2SFrank Schaefer rsize = remain;
658d832c5b2SFrank Schaefer
659aab3125cSMauro Carvalho Chehab ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
660d832c5b2SFrank Schaefer if (ret < 0)
661d832c5b2SFrank Schaefer return ret;
662d832c5b2SFrank Schaefer
663d832c5b2SFrank Schaefer remain -= rsize;
664d832c5b2SFrank Schaefer data += rsize;
665d832c5b2SFrank Schaefer }
666d832c5b2SFrank Schaefer
667d832c5b2SFrank Schaefer return len;
668d832c5b2SFrank Schaefer }
669d832c5b2SFrank Schaefer
em28xx_i2c_eeprom(struct em28xx * dev,unsigned int bus,u8 ** eedata,u16 * eedata_len)6708adbc7d6SMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned int bus,
671aab3125cSMauro Carvalho Chehab u8 **eedata, u16 *eedata_len)
6720c0d06caSMauro Carvalho Chehab {
673510e884cSFrank Schaefer const u16 len = 256;
674fa74aca3SFrank Schaefer /*
675fa74aca3SFrank Schaefer * FIXME common length/size for bytes to read, to display, hash
676510e884cSFrank Schaefer * calculation and returned device dataset. Simplifies the code a lot,
677fa74aca3SFrank Schaefer * but we might have to deal with multiple sizes in the future !
678fa74aca3SFrank Schaefer */
67950f0a9dfSMauro Carvalho Chehab int err;
680510e884cSFrank Schaefer struct em28xx_eeprom *dev_config;
681510e884cSFrank Schaefer u8 buf, *data;
6820c0d06caSMauro Carvalho Chehab
683a217968fSFrank Schaefer *eedata = NULL;
684510e884cSFrank Schaefer *eedata_len = 0;
685a217968fSFrank Schaefer
686aab3125cSMauro Carvalho Chehab /* EEPROM is always on i2c bus 0 on all known devices. */
687aab3125cSMauro Carvalho Chehab
688aab3125cSMauro Carvalho Chehab dev->i2c_client[bus].addr = 0xa0 >> 1;
6890c0d06caSMauro Carvalho Chehab
6900c0d06caSMauro Carvalho Chehab /* Check if board has eeprom */
691aab3125cSMauro Carvalho Chehab err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
6920c0d06caSMauro Carvalho Chehab if (err < 0) {
69329b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "board has no eeprom\n");
6940c0d06caSMauro Carvalho Chehab return -ENODEV;
6950c0d06caSMauro Carvalho Chehab }
6960c0d06caSMauro Carvalho Chehab
697a217968fSFrank Schaefer data = kzalloc(len, GFP_KERNEL);
6988adbc7d6SMauro Carvalho Chehab if (!data)
699a217968fSFrank Schaefer return -ENOMEM;
700a217968fSFrank Schaefer
701d832c5b2SFrank Schaefer /* Read EEPROM content */
702aab3125cSMauro Carvalho Chehab err = em28xx_i2c_read_block(dev, bus, 0x0000,
703aab3125cSMauro Carvalho Chehab dev->eeprom_addrwidth_16bit,
704a217968fSFrank Schaefer len, data);
705d832c5b2SFrank Schaefer if (err != len) {
70629b05e22SMauro Carvalho Chehab dev_err(&dev->intf->dev,
707ce8591ffSMauro Carvalho Chehab "failed to read eeprom (err=%d)\n", err);
708510e884cSFrank Schaefer goto error;
7090c0d06caSMauro Carvalho Chehab }
71090271964SFrank Schaefer
71150f0a9dfSMauro Carvalho Chehab if (i2c_debug) {
71287b52439SFrank Schaefer /* Display eeprom content */
713ce8591ffSMauro Carvalho Chehab print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
71450f0a9dfSMauro Carvalho Chehab 16, 1, data, len, true);
71550f0a9dfSMauro Carvalho Chehab
71687b52439SFrank Schaefer if (dev->eeprom_addrwidth_16bit)
71729b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
718ce8591ffSMauro Carvalho Chehab "eeprom %06x: ... (skipped)\n", 256);
71987b52439SFrank Schaefer }
7200c0d06caSMauro Carvalho Chehab
72187b52439SFrank Schaefer if (dev->eeprom_addrwidth_16bit &&
722a217968fSFrank Schaefer data[0] == 0x26 && data[3] == 0x00) {
72387b52439SFrank Schaefer /* new eeprom format; size 4-64kb */
724510e884cSFrank Schaefer u16 mc_start;
725510e884cSFrank Schaefer u16 hwconf_offset;
726510e884cSFrank Schaefer
727a217968fSFrank Schaefer dev->hash = em28xx_hash_mem(data, len, 32);
728510e884cSFrank Schaefer mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
729510e884cSFrank Schaefer
73029b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
731290ef7d8SAntonio Cardace "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
732290ef7d8SAntonio Cardace data, dev->hash);
73329b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
734ce8591ffSMauro Carvalho Chehab "EEPROM info:\n");
73529b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
736ce8591ffSMauro Carvalho Chehab "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
737510e884cSFrank Schaefer mc_start, data[2]);
738fa74aca3SFrank Schaefer /*
739fa74aca3SFrank Schaefer * boot configuration (address 0x0002):
74087b52439SFrank Schaefer * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
74187b52439SFrank Schaefer * [1] always selects 12 kb RAM
74287b52439SFrank Schaefer * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
74387b52439SFrank Schaefer * [4] 1 = force fast mode and no suspend for device testing
74487b52439SFrank Schaefer * [5:7] USB PHY tuning registers; determined by device
74587b52439SFrank Schaefer * characterization
74687b52439SFrank Schaefer */
74787b52439SFrank Schaefer
748fa74aca3SFrank Schaefer /*
749fa74aca3SFrank Schaefer * Read hardware config dataset offset from address
750fa74aca3SFrank Schaefer * (microcode start + 46)
751fa74aca3SFrank Schaefer */
752aab3125cSMauro Carvalho Chehab err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
753aab3125cSMauro Carvalho Chehab data);
754510e884cSFrank Schaefer if (err != 2) {
75529b05e22SMauro Carvalho Chehab dev_err(&dev->intf->dev,
756ce8591ffSMauro Carvalho Chehab "failed to read hardware configuration data from eeprom (err=%d)\n",
757510e884cSFrank Schaefer err);
758510e884cSFrank Schaefer goto error;
759510e884cSFrank Schaefer }
76087b52439SFrank Schaefer
761510e884cSFrank Schaefer /* Calculate hardware config dataset start address */
762510e884cSFrank Schaefer hwconf_offset = mc_start + data[0] + (data[1] << 8);
763510e884cSFrank Schaefer
764510e884cSFrank Schaefer /* Read hardware config dataset */
765fa74aca3SFrank Schaefer /*
766fa74aca3SFrank Schaefer * NOTE: the microcode copy can be multiple pages long, but
767510e884cSFrank Schaefer * we assume the hardware config dataset is the same as in
768510e884cSFrank Schaefer * the old eeprom and not longer than 256 bytes.
769510e884cSFrank Schaefer * tveeprom is currently also limited to 256 bytes.
770510e884cSFrank Schaefer */
771aab3125cSMauro Carvalho Chehab err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
772aab3125cSMauro Carvalho Chehab data);
773510e884cSFrank Schaefer if (err != len) {
77429b05e22SMauro Carvalho Chehab dev_err(&dev->intf->dev,
775ce8591ffSMauro Carvalho Chehab "failed to read hardware configuration data from eeprom (err=%d)\n",
776510e884cSFrank Schaefer err);
777510e884cSFrank Schaefer goto error;
778510e884cSFrank Schaefer }
779510e884cSFrank Schaefer
780510e884cSFrank Schaefer /* Verify hardware config dataset */
781510e884cSFrank Schaefer /* NOTE: not all devices provide this type of dataset */
782510e884cSFrank Schaefer if (data[0] != 0x1a || data[1] != 0xeb ||
783a217968fSFrank Schaefer data[2] != 0x67 || data[3] != 0x95) {
78429b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
785ce8591ffSMauro Carvalho Chehab "\tno hardware configuration dataset found in eeprom\n");
786510e884cSFrank Schaefer kfree(data);
787510e884cSFrank Schaefer return 0;
788510e884cSFrank Schaefer }
789510e884cSFrank Schaefer
7908adbc7d6SMauro Carvalho Chehab /*
7918adbc7d6SMauro Carvalho Chehab * TODO: decrypt eeprom data for camera bridges
7928adbc7d6SMauro Carvalho Chehab * (em25xx, em276x+)
7938adbc7d6SMauro Carvalho Chehab */
794510e884cSFrank Schaefer
795510e884cSFrank Schaefer } else if (!dev->eeprom_addrwidth_16bit &&
796510e884cSFrank Schaefer data[0] == 0x1a && data[1] == 0xeb &&
797510e884cSFrank Schaefer data[2] == 0x67 && data[3] == 0x95) {
798510e884cSFrank Schaefer dev->hash = em28xx_hash_mem(data, len, 32);
79929b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
800290ef7d8SAntonio Cardace "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
801290ef7d8SAntonio Cardace data, dev->hash);
80229b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
803ce8591ffSMauro Carvalho Chehab "EEPROM info:\n");
804510e884cSFrank Schaefer } else {
80529b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
806ce8591ffSMauro Carvalho Chehab "unknown eeprom format or eeprom corrupted !\n");
807510e884cSFrank Schaefer err = -ENODEV;
808510e884cSFrank Schaefer goto error;
809f55eacbeSFrank Schaefer }
810f55eacbeSFrank Schaefer
811a217968fSFrank Schaefer *eedata = data;
812510e884cSFrank Schaefer *eedata_len = len;
81332bf7c6cSAlban Browaeys dev_config = (void *)*eedata;
814a217968fSFrank Schaefer
815510e884cSFrank Schaefer switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
8160c0d06caSMauro Carvalho Chehab case 0:
81729b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\tNo audio on board.\n");
8180c0d06caSMauro Carvalho Chehab break;
8190c0d06caSMauro Carvalho Chehab case 1:
82029b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
8210c0d06caSMauro Carvalho Chehab break;
8220c0d06caSMauro Carvalho Chehab case 2:
823687ff8b0SFrank Schaefer if (dev->chip_id < CHIP_ID_EM2860)
82429b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
825ce8591ffSMauro Carvalho Chehab "\tI2S audio, sample rate=32k\n");
826687ff8b0SFrank Schaefer else
82729b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
828ce8591ffSMauro Carvalho Chehab "\tI2S audio, 3 sample rates\n");
8290c0d06caSMauro Carvalho Chehab break;
8300c0d06caSMauro Carvalho Chehab case 3:
831687ff8b0SFrank Schaefer if (dev->chip_id < CHIP_ID_EM2860)
83229b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
833ce8591ffSMauro Carvalho Chehab "\tI2S audio, 3 sample rates\n");
834687ff8b0SFrank Schaefer else
83529b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
836ce8591ffSMauro Carvalho Chehab "\tI2S audio, 5 sample rates\n");
8370c0d06caSMauro Carvalho Chehab break;
8380c0d06caSMauro Carvalho Chehab }
8390c0d06caSMauro Carvalho Chehab
840510e884cSFrank Schaefer if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
84129b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
8420c0d06caSMauro Carvalho Chehab
843510e884cSFrank Schaefer if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
84429b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
8450c0d06caSMauro Carvalho Chehab
846510e884cSFrank Schaefer switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
8470c0d06caSMauro Carvalho Chehab case 0:
84829b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\t500mA max power\n");
8490c0d06caSMauro Carvalho Chehab break;
8500c0d06caSMauro Carvalho Chehab case 1:
85129b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\t400mA max power\n");
8520c0d06caSMauro Carvalho Chehab break;
8530c0d06caSMauro Carvalho Chehab case 2:
85429b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\t300mA max power\n");
8550c0d06caSMauro Carvalho Chehab break;
8560c0d06caSMauro Carvalho Chehab case 3:
85729b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev, "\t200mA max power\n");
8580c0d06caSMauro Carvalho Chehab break;
8590c0d06caSMauro Carvalho Chehab }
86029b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
861ce8591ffSMauro Carvalho Chehab "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
862510e884cSFrank Schaefer dev_config->string_idx_table,
863510e884cSFrank Schaefer le16_to_cpu(dev_config->string1),
864510e884cSFrank Schaefer le16_to_cpu(dev_config->string2),
865510e884cSFrank Schaefer le16_to_cpu(dev_config->string3));
8660c0d06caSMauro Carvalho Chehab
8670c0d06caSMauro Carvalho Chehab return 0;
868510e884cSFrank Schaefer
869510e884cSFrank Schaefer error:
870510e884cSFrank Schaefer kfree(data);
871510e884cSFrank Schaefer return err;
8720c0d06caSMauro Carvalho Chehab }
8730c0d06caSMauro Carvalho Chehab
8740c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
8750c0d06caSMauro Carvalho Chehab
8760c0d06caSMauro Carvalho Chehab /*
8770c0d06caSMauro Carvalho Chehab * functionality()
8780c0d06caSMauro Carvalho Chehab */
functionality(struct i2c_adapter * i2c_adap)879aab3125cSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *i2c_adap)
8800c0d06caSMauro Carvalho Chehab {
881aab3125cSMauro Carvalho Chehab struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
882aab3125cSMauro Carvalho Chehab
8838adbc7d6SMauro Carvalho Chehab if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX ||
8848adbc7d6SMauro Carvalho Chehab i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) {
885a3ea4bf9SFrank Schaefer return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
886a3ea4bf9SFrank Schaefer } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
887a3ea4bf9SFrank Schaefer return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
888a3ea4bf9SFrank Schaefer ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
889a3ea4bf9SFrank Schaefer }
890a3ea4bf9SFrank Schaefer
891a3ea4bf9SFrank Schaefer WARN(1, "Unknown i2c bus algorithm.\n");
892a3ea4bf9SFrank Schaefer return 0;
8930c0d06caSMauro Carvalho Chehab }
8940c0d06caSMauro Carvalho Chehab
89578f2c50bSJulia Lawall static const struct i2c_algorithm em28xx_algo = {
8960c0d06caSMauro Carvalho Chehab .master_xfer = em28xx_i2c_xfer,
8970c0d06caSMauro Carvalho Chehab .functionality = functionality,
8980c0d06caSMauro Carvalho Chehab };
8990c0d06caSMauro Carvalho Chehab
9006843868fSBhumika Goyal static const struct i2c_adapter em28xx_adap_template = {
9010c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE,
9020c0d06caSMauro Carvalho Chehab .name = "em28xx",
9030c0d06caSMauro Carvalho Chehab .algo = &em28xx_algo,
9040c0d06caSMauro Carvalho Chehab };
9050c0d06caSMauro Carvalho Chehab
9062b83247fSBhumika Goyal static const struct i2c_client em28xx_client_template = {
9070c0d06caSMauro Carvalho Chehab .name = "em28xx internal",
9080c0d06caSMauro Carvalho Chehab };
9090c0d06caSMauro Carvalho Chehab
9100c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */
9110c0d06caSMauro Carvalho Chehab
9120c0d06caSMauro Carvalho Chehab /*
9130c0d06caSMauro Carvalho Chehab * i2c_devs
9140c0d06caSMauro Carvalho Chehab * incomplete list of known devices
9150c0d06caSMauro Carvalho Chehab */
9160c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = {
9179aa785b1SWilson Michaels [0x1c >> 1] = "lgdt330x",
9180b3966e4SFrank Schaefer [0x3e >> 1] = "remote IR sensor",
9190c0d06caSMauro Carvalho Chehab [0x4a >> 1] = "saa7113h",
9200c0d06caSMauro Carvalho Chehab [0x52 >> 1] = "drxk",
9210c0d06caSMauro Carvalho Chehab [0x60 >> 1] = "remote IR sensor",
9220c0d06caSMauro Carvalho Chehab [0x8e >> 1] = "remote IR sensor",
9230c0d06caSMauro Carvalho Chehab [0x86 >> 1] = "tda9887",
9240c0d06caSMauro Carvalho Chehab [0x80 >> 1] = "msp34xx",
9250c0d06caSMauro Carvalho Chehab [0x88 >> 1] = "msp34xx",
9260c0d06caSMauro Carvalho Chehab [0xa0 >> 1] = "eeprom",
9270c0d06caSMauro Carvalho Chehab [0xb0 >> 1] = "tda9874",
9280c0d06caSMauro Carvalho Chehab [0xb8 >> 1] = "tvp5150a",
9290c0d06caSMauro Carvalho Chehab [0xba >> 1] = "webcam sensor or tvp5150a",
9300c0d06caSMauro Carvalho Chehab [0xc0 >> 1] = "tuner (analog)",
9310c0d06caSMauro Carvalho Chehab [0xc2 >> 1] = "tuner (analog)",
9320c0d06caSMauro Carvalho Chehab [0xc4 >> 1] = "tuner (analog)",
9330c0d06caSMauro Carvalho Chehab [0xc6 >> 1] = "tuner (analog)",
9340c0d06caSMauro Carvalho Chehab };
9350c0d06caSMauro Carvalho Chehab
9360c0d06caSMauro Carvalho Chehab /*
9370c0d06caSMauro Carvalho Chehab * do_i2c_scan()
9380c0d06caSMauro Carvalho Chehab * check i2c address range for devices
9390c0d06caSMauro Carvalho Chehab */
em28xx_do_i2c_scan(struct em28xx * dev,unsigned int bus)9408adbc7d6SMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev, unsigned int bus)
9410c0d06caSMauro Carvalho Chehab {
9420c0d06caSMauro Carvalho Chehab u8 i2c_devicelist[128];
9430c0d06caSMauro Carvalho Chehab unsigned char buf;
9440c0d06caSMauro Carvalho Chehab int i, rc;
9450c0d06caSMauro Carvalho Chehab
9462df34d3aSHans Verkuil memset(i2c_devicelist, 0, sizeof(i2c_devicelist));
9470c0d06caSMauro Carvalho Chehab
9480c0d06caSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
949aab3125cSMauro Carvalho Chehab dev->i2c_client[bus].addr = i;
950aab3125cSMauro Carvalho Chehab rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
9510c0d06caSMauro Carvalho Chehab if (rc < 0)
9520c0d06caSMauro Carvalho Chehab continue;
9530c0d06caSMauro Carvalho Chehab i2c_devicelist[i] = i;
95429b05e22SMauro Carvalho Chehab dev_info(&dev->intf->dev,
955ce8591ffSMauro Carvalho Chehab "found i2c device @ 0x%x on bus %d [%s]\n",
956aab3125cSMauro Carvalho Chehab i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
9570c0d06caSMauro Carvalho Chehab }
9580c0d06caSMauro Carvalho Chehab
959aab3125cSMauro Carvalho Chehab if (bus == dev->def_i2c_bus)
9600c0d06caSMauro Carvalho Chehab dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
9612df34d3aSHans Verkuil sizeof(i2c_devicelist), 32);
9620c0d06caSMauro Carvalho Chehab }
9630c0d06caSMauro Carvalho Chehab
9640c0d06caSMauro Carvalho Chehab /*
9650c0d06caSMauro Carvalho Chehab * em28xx_i2c_register()
9660c0d06caSMauro Carvalho Chehab * register i2c bus
9670c0d06caSMauro Carvalho Chehab */
em28xx_i2c_register(struct em28xx * dev,unsigned int bus,enum em28xx_i2c_algo_type algo_type)9688adbc7d6SMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev, unsigned int bus,
969a3ea4bf9SFrank Schaefer enum em28xx_i2c_algo_type algo_type)
9700c0d06caSMauro Carvalho Chehab {
9710c0d06caSMauro Carvalho Chehab int retval;
9720c0d06caSMauro Carvalho Chehab
9738adbc7d6SMauro Carvalho Chehab if (WARN_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg ||
9748adbc7d6SMauro Carvalho Chehab !dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req))
9758adbc7d6SMauro Carvalho Chehab return -ENODEV;
9760c0d06caSMauro Carvalho Chehab
977aab3125cSMauro Carvalho Chehab if (bus >= NUM_I2C_BUSES)
978aab3125cSMauro Carvalho Chehab return -ENODEV;
979aab3125cSMauro Carvalho Chehab
980aab3125cSMauro Carvalho Chehab dev->i2c_adap[bus] = em28xx_adap_template;
98129b05e22SMauro Carvalho Chehab dev->i2c_adap[bus].dev.parent = &dev->intf->dev;
982cc1e6315SMauro Carvalho Chehab strscpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev),
983cc1e6315SMauro Carvalho Chehab sizeof(dev->i2c_adap[bus].name));
984aab3125cSMauro Carvalho Chehab
985aab3125cSMauro Carvalho Chehab dev->i2c_bus[bus].bus = bus;
986a3ea4bf9SFrank Schaefer dev->i2c_bus[bus].algo_type = algo_type;
987aab3125cSMauro Carvalho Chehab dev->i2c_bus[bus].dev = dev;
988aab3125cSMauro Carvalho Chehab dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
989aab3125cSMauro Carvalho Chehab
990aab3125cSMauro Carvalho Chehab retval = i2c_add_adapter(&dev->i2c_adap[bus]);
9910c0d06caSMauro Carvalho Chehab if (retval < 0) {
99229b05e22SMauro Carvalho Chehab dev_err(&dev->intf->dev,
993ce8591ffSMauro Carvalho Chehab "%s: i2c_add_adapter failed! retval [%d]\n",
9940c0d06caSMauro Carvalho Chehab __func__, retval);
9950c0d06caSMauro Carvalho Chehab return retval;
9960c0d06caSMauro Carvalho Chehab }
9970c0d06caSMauro Carvalho Chehab
998aab3125cSMauro Carvalho Chehab dev->i2c_client[bus] = em28xx_client_template;
999aab3125cSMauro Carvalho Chehab dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
10000c0d06caSMauro Carvalho Chehab
1001aab3125cSMauro Carvalho Chehab /* Up to now, all eeproms are at bus 0 */
1002aab3125cSMauro Carvalho Chehab if (!bus) {
10038adbc7d6SMauro Carvalho Chehab retval = em28xx_i2c_eeprom(dev, bus,
10048adbc7d6SMauro Carvalho Chehab &dev->eedata, &dev->eedata_len);
10058adbc7d6SMauro Carvalho Chehab if (retval < 0 && retval != -ENODEV) {
100629b05e22SMauro Carvalho Chehab dev_err(&dev->intf->dev,
1007ce8591ffSMauro Carvalho Chehab "%s: em28xx_i2_eeprom failed! retval [%d]\n",
10080c0d06caSMauro Carvalho Chehab __func__, retval);
10090c0d06caSMauro Carvalho Chehab }
1010aab3125cSMauro Carvalho Chehab }
10110c0d06caSMauro Carvalho Chehab
10120c0d06caSMauro Carvalho Chehab if (i2c_scan)
1013aab3125cSMauro Carvalho Chehab em28xx_do_i2c_scan(dev, bus);
10140c0d06caSMauro Carvalho Chehab
10150c0d06caSMauro Carvalho Chehab return 0;
10160c0d06caSMauro Carvalho Chehab }
10170c0d06caSMauro Carvalho Chehab
10180c0d06caSMauro Carvalho Chehab /*
10190c0d06caSMauro Carvalho Chehab * em28xx_i2c_unregister()
10200c0d06caSMauro Carvalho Chehab * unregister i2c_bus
10210c0d06caSMauro Carvalho Chehab */
em28xx_i2c_unregister(struct em28xx * dev,unsigned int bus)10228adbc7d6SMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev, unsigned int bus)
10230c0d06caSMauro Carvalho Chehab {
1024aab3125cSMauro Carvalho Chehab if (bus >= NUM_I2C_BUSES)
1025aab3125cSMauro Carvalho Chehab return -ENODEV;
1026aab3125cSMauro Carvalho Chehab
1027aab3125cSMauro Carvalho Chehab i2c_del_adapter(&dev->i2c_adap[bus]);
10280c0d06caSMauro Carvalho Chehab return 0;
10290c0d06caSMauro Carvalho Chehab }
1030