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); 23045f04e82SFrank Schaefer if (ret != len) { 2310c0d06caSMauro Carvalho Chehab if (ret < 0) { 23245f04e82SFrank Schaefer em28xx_warn("reading from i2c device at 0x%x failed " 23345f04e82SFrank Schaefer "(error=%i)\n", addr, ret); 23445f04e82SFrank Schaefer return ret; 23545f04e82SFrank Schaefer } else { 23645f04e82SFrank Schaefer em28xx_warn("%i bytes requested from i2c device at " 23745f04e82SFrank Schaefer "0x%x, but %i bytes received\n", 23845f04e82SFrank Schaefer len, addr, ret); 23945f04e82SFrank Schaefer return -EIO; 24045f04e82SFrank Schaefer } 24145f04e82SFrank 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 { 2830c0d06caSMauro Carvalho Chehab struct em28xx *dev = i2c_adap->algo_data; 2840c0d06caSMauro Carvalho Chehab int addr, rc, i, byte; 2850c0d06caSMauro Carvalho Chehab 2860c0d06caSMauro Carvalho Chehab if (num <= 0) 2870c0d06caSMauro Carvalho Chehab return 0; 2880c0d06caSMauro Carvalho Chehab for (i = 0; i < num; i++) { 2890c0d06caSMauro Carvalho Chehab addr = msgs[i].addr << 1; 290d90f0677SFrank Schaefer if (i2c_debug) 291d7a80eaaSFrank Schaefer printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:", 292d7a80eaaSFrank Schaefer dev->name, __func__ , 2930c0d06caSMauro Carvalho Chehab (msgs[i].flags & I2C_M_RD) ? "read" : "write", 294d7a80eaaSFrank Schaefer i == num - 1 ? "stop" : "nonstop", 295d7a80eaaSFrank Schaefer addr, msgs[i].len); 2960c0d06caSMauro Carvalho Chehab if (!msgs[i].len) { /* no len: check only for device presence */ 2970c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 2980c0d06caSMauro Carvalho Chehab rc = em2800_i2c_check_for_device(dev, addr); 2990c0d06caSMauro Carvalho Chehab else 3000c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_check_for_device(dev, addr); 30145f04e82SFrank Schaefer if (rc == -ENODEV) { 302d90f0677SFrank Schaefer if (i2c_debug) 30345f04e82SFrank Schaefer printk(" no device\n"); 3040c0d06caSMauro Carvalho Chehab return rc; 3050c0d06caSMauro Carvalho Chehab } 3060c0d06caSMauro Carvalho Chehab } else if (msgs[i].flags & I2C_M_RD) { 3070c0d06caSMauro Carvalho Chehab /* read bytes */ 3080c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 3090c0d06caSMauro Carvalho Chehab rc = em2800_i2c_recv_bytes(dev, addr, 3100c0d06caSMauro Carvalho Chehab msgs[i].buf, 3110c0d06caSMauro Carvalho Chehab msgs[i].len); 3120c0d06caSMauro Carvalho Chehab else 3130c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_recv_bytes(dev, addr, 3140c0d06caSMauro Carvalho Chehab msgs[i].buf, 3150c0d06caSMauro Carvalho Chehab msgs[i].len); 316d90f0677SFrank Schaefer if (i2c_debug) { 3170c0d06caSMauro Carvalho Chehab for (byte = 0; byte < msgs[i].len; byte++) 3180c0d06caSMauro Carvalho Chehab printk(" %02x", msgs[i].buf[byte]); 3190c0d06caSMauro Carvalho Chehab } 3200c0d06caSMauro Carvalho Chehab } else { 3210c0d06caSMauro Carvalho Chehab /* write bytes */ 322d90f0677SFrank Schaefer if (i2c_debug) { 3230c0d06caSMauro Carvalho Chehab for (byte = 0; byte < msgs[i].len; byte++) 3240c0d06caSMauro Carvalho Chehab printk(" %02x", msgs[i].buf[byte]); 3250c0d06caSMauro Carvalho Chehab } 3260c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 3270c0d06caSMauro Carvalho Chehab rc = em2800_i2c_send_bytes(dev, addr, 3280c0d06caSMauro Carvalho Chehab msgs[i].buf, 3290c0d06caSMauro Carvalho Chehab msgs[i].len); 3300c0d06caSMauro Carvalho Chehab else 3310c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_send_bytes(dev, addr, 3320c0d06caSMauro Carvalho Chehab msgs[i].buf, 3330c0d06caSMauro Carvalho Chehab msgs[i].len, 3340c0d06caSMauro Carvalho Chehab i == num - 1); 3350c0d06caSMauro Carvalho Chehab } 33645f04e82SFrank Schaefer if (rc < 0) { 337d90f0677SFrank Schaefer if (i2c_debug) 33845f04e82SFrank Schaefer printk(" ERROR: %i\n", rc); 33945f04e82SFrank Schaefer return rc; 34045f04e82SFrank Schaefer } 341d90f0677SFrank Schaefer if (i2c_debug) 3420c0d06caSMauro Carvalho Chehab printk("\n"); 3430c0d06caSMauro Carvalho Chehab } 3440c0d06caSMauro Carvalho Chehab 3450c0d06caSMauro Carvalho Chehab return num; 3460c0d06caSMauro Carvalho Chehab } 3470c0d06caSMauro Carvalho Chehab 3480c0d06caSMauro Carvalho Chehab /* based on linux/sunrpc/svcauth.h and linux/hash.h 3490c0d06caSMauro Carvalho Chehab * The original hash function returns a different value, if arch is x86_64 3500c0d06caSMauro Carvalho Chehab * or i386. 3510c0d06caSMauro Carvalho Chehab */ 3520c0d06caSMauro Carvalho Chehab static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits) 3530c0d06caSMauro Carvalho Chehab { 3540c0d06caSMauro Carvalho Chehab unsigned long hash = 0; 3550c0d06caSMauro Carvalho Chehab unsigned long l = 0; 3560c0d06caSMauro Carvalho Chehab int len = 0; 3570c0d06caSMauro Carvalho Chehab unsigned char c; 3580c0d06caSMauro Carvalho Chehab do { 3590c0d06caSMauro Carvalho Chehab if (len == length) { 3600c0d06caSMauro Carvalho Chehab c = (char)len; 3610c0d06caSMauro Carvalho Chehab len = -1; 3620c0d06caSMauro Carvalho Chehab } else 3630c0d06caSMauro Carvalho Chehab c = *buf++; 3640c0d06caSMauro Carvalho Chehab l = (l << 8) | c; 3650c0d06caSMauro Carvalho Chehab len++; 3660c0d06caSMauro Carvalho Chehab if ((len & (32 / 8 - 1)) == 0) 3670c0d06caSMauro Carvalho Chehab hash = ((hash^l) * 0x9e370001UL); 3680c0d06caSMauro Carvalho Chehab } while (len); 3690c0d06caSMauro Carvalho Chehab 3700c0d06caSMauro Carvalho Chehab return (hash >> (32 - bits)) & 0xffffffffUL; 3710c0d06caSMauro Carvalho Chehab } 3720c0d06caSMauro Carvalho Chehab 3730c0d06caSMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) 3740c0d06caSMauro Carvalho Chehab { 3750c0d06caSMauro Carvalho Chehab unsigned char buf, *p = eedata; 3760c0d06caSMauro Carvalho Chehab struct em28xx_eeprom *em_eeprom = (void *)eedata; 37790271964SFrank Schaefer int i, err, size = len, block, block_max; 3780c0d06caSMauro Carvalho Chehab 3790c0d06caSMauro Carvalho Chehab if (dev->chip_id == CHIP_ID_EM2874 || 3800c0d06caSMauro Carvalho Chehab dev->chip_id == CHIP_ID_EM28174 || 3810c0d06caSMauro Carvalho Chehab dev->chip_id == CHIP_ID_EM2884) { 3820c0d06caSMauro Carvalho Chehab /* Empia switched to a 16-bit addressable eeprom in newer 3830c0d06caSMauro Carvalho Chehab devices. While we could certainly write a routine to read 3840c0d06caSMauro Carvalho Chehab the eeprom, there is nothing of use in there that cannot be 3850c0d06caSMauro Carvalho Chehab accessed through registers, and there is the risk that we 3860c0d06caSMauro Carvalho Chehab could corrupt the eeprom (since a 16-bit read call is 3870c0d06caSMauro Carvalho Chehab interpreted as a write call by 8-bit eeproms). 3880c0d06caSMauro Carvalho Chehab */ 3890c0d06caSMauro Carvalho Chehab return 0; 3900c0d06caSMauro Carvalho Chehab } 3910c0d06caSMauro Carvalho Chehab 3920c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = 0xa0 >> 1; 3930c0d06caSMauro Carvalho Chehab 3940c0d06caSMauro Carvalho Chehab /* Check if board has eeprom */ 3950c0d06caSMauro Carvalho Chehab err = i2c_master_recv(&dev->i2c_client, &buf, 0); 3960c0d06caSMauro Carvalho Chehab if (err < 0) { 39712d7ce18SFrank Schaefer em28xx_info("board has no eeprom\n"); 3980c0d06caSMauro Carvalho Chehab memset(eedata, 0, len); 3990c0d06caSMauro Carvalho Chehab return -ENODEV; 4000c0d06caSMauro Carvalho Chehab } 4010c0d06caSMauro Carvalho Chehab 4020c0d06caSMauro Carvalho Chehab buf = 0; 4030c0d06caSMauro Carvalho Chehab 4040c0d06caSMauro Carvalho Chehab err = i2c_master_send(&dev->i2c_client, &buf, 1); 4050c0d06caSMauro Carvalho Chehab if (err != 1) { 40612d7ce18SFrank Schaefer em28xx_errdev("failed to read eeprom (err=%d)\n", err); 4070c0d06caSMauro Carvalho Chehab return err; 4080c0d06caSMauro Carvalho Chehab } 40990271964SFrank Schaefer 41090271964SFrank Schaefer if (dev->board.is_em2800) 41190271964SFrank Schaefer block_max = 4; 41290271964SFrank Schaefer else 41390271964SFrank Schaefer block_max = 64; 41490271964SFrank Schaefer 4150c0d06caSMauro Carvalho Chehab while (size > 0) { 41690271964SFrank Schaefer if (size > block_max) 41790271964SFrank Schaefer block = block_max; 4180c0d06caSMauro Carvalho Chehab else 4190c0d06caSMauro Carvalho Chehab block = size; 4200c0d06caSMauro Carvalho Chehab 4210c0d06caSMauro Carvalho Chehab if (block != 4220c0d06caSMauro Carvalho Chehab (err = i2c_master_recv(&dev->i2c_client, p, block))) { 42312d7ce18SFrank Schaefer em28xx_errdev("i2c eeprom read error (err=%d)\n", err); 4240c0d06caSMauro Carvalho Chehab return err; 4250c0d06caSMauro Carvalho Chehab } 4260c0d06caSMauro Carvalho Chehab size -= block; 4270c0d06caSMauro Carvalho Chehab p += block; 4280c0d06caSMauro Carvalho Chehab } 4290c0d06caSMauro Carvalho Chehab for (i = 0; i < len; i++) { 4300c0d06caSMauro Carvalho Chehab if (0 == (i % 16)) 43112d7ce18SFrank Schaefer em28xx_info("i2c eeprom %02x:", i); 4320c0d06caSMauro Carvalho Chehab printk(" %02x", eedata[i]); 4330c0d06caSMauro Carvalho Chehab if (15 == (i % 16)) 4340c0d06caSMauro Carvalho Chehab printk("\n"); 4350c0d06caSMauro Carvalho Chehab } 4360c0d06caSMauro Carvalho Chehab 437*f55eacbeSFrank Schaefer if (em_eeprom->id != 0x9567eb1a) { 438*f55eacbeSFrank Schaefer em28xx_errdev("Unknown eeprom type or eeprom corrupted !"); 439*f55eacbeSFrank Schaefer return -ENODEV; 440*f55eacbeSFrank Schaefer } 441*f55eacbeSFrank Schaefer 4420c0d06caSMauro Carvalho Chehab dev->hash = em28xx_hash_mem(eedata, len, 32); 4430c0d06caSMauro Carvalho Chehab 44412d7ce18SFrank Schaefer em28xx_info("EEPROM ID = 0x%08x, EEPROM hash = 0x%08lx\n", 44512d7ce18SFrank Schaefer em_eeprom->id, dev->hash); 4460c0d06caSMauro Carvalho Chehab 44712d7ce18SFrank Schaefer em28xx_info("EEPROM info:\n"); 4480c0d06caSMauro Carvalho Chehab 4490c0d06caSMauro Carvalho Chehab switch (em_eeprom->chip_conf >> 4 & 0x3) { 4500c0d06caSMauro Carvalho Chehab case 0: 45112d7ce18SFrank Schaefer em28xx_info("\tNo audio on board.\n"); 4520c0d06caSMauro Carvalho Chehab break; 4530c0d06caSMauro Carvalho Chehab case 1: 45412d7ce18SFrank Schaefer em28xx_info("\tAC97 audio (5 sample rates)\n"); 4550c0d06caSMauro Carvalho Chehab break; 4560c0d06caSMauro Carvalho Chehab case 2: 45712d7ce18SFrank Schaefer em28xx_info("\tI2S audio, sample rate=32k\n"); 4580c0d06caSMauro Carvalho Chehab break; 4590c0d06caSMauro Carvalho Chehab case 3: 46012d7ce18SFrank Schaefer em28xx_info("\tI2S audio, 3 sample rates\n"); 4610c0d06caSMauro Carvalho Chehab break; 4620c0d06caSMauro Carvalho Chehab } 4630c0d06caSMauro Carvalho Chehab 4640c0d06caSMauro Carvalho Chehab if (em_eeprom->chip_conf & 1 << 3) 46512d7ce18SFrank Schaefer em28xx_info("\tUSB Remote wakeup capable\n"); 4660c0d06caSMauro Carvalho Chehab 4670c0d06caSMauro Carvalho Chehab if (em_eeprom->chip_conf & 1 << 2) 46812d7ce18SFrank Schaefer em28xx_info("\tUSB Self power capable\n"); 4690c0d06caSMauro Carvalho Chehab 4700c0d06caSMauro Carvalho Chehab switch (em_eeprom->chip_conf & 0x3) { 4710c0d06caSMauro Carvalho Chehab case 0: 47212d7ce18SFrank Schaefer em28xx_info("\t500mA max power\n"); 4730c0d06caSMauro Carvalho Chehab break; 4740c0d06caSMauro Carvalho Chehab case 1: 47512d7ce18SFrank Schaefer em28xx_info("\t400mA max power\n"); 4760c0d06caSMauro Carvalho Chehab break; 4770c0d06caSMauro Carvalho Chehab case 2: 47812d7ce18SFrank Schaefer em28xx_info("\t300mA max power\n"); 4790c0d06caSMauro Carvalho Chehab break; 4800c0d06caSMauro Carvalho Chehab case 3: 48112d7ce18SFrank Schaefer em28xx_info("\t200mA max power\n"); 4820c0d06caSMauro Carvalho Chehab break; 4830c0d06caSMauro Carvalho Chehab } 48412d7ce18SFrank Schaefer em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n", 4850c0d06caSMauro Carvalho Chehab em_eeprom->string_idx_table, 4860c0d06caSMauro Carvalho Chehab em_eeprom->string1, 4870c0d06caSMauro Carvalho Chehab em_eeprom->string2, 4880c0d06caSMauro Carvalho Chehab em_eeprom->string3); 4890c0d06caSMauro Carvalho Chehab 4900c0d06caSMauro Carvalho Chehab return 0; 4910c0d06caSMauro Carvalho Chehab } 4920c0d06caSMauro Carvalho Chehab 4930c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 4940c0d06caSMauro Carvalho Chehab 4950c0d06caSMauro Carvalho Chehab /* 4960c0d06caSMauro Carvalho Chehab * functionality() 4970c0d06caSMauro Carvalho Chehab */ 4980c0d06caSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *adap) 4990c0d06caSMauro Carvalho Chehab { 500eaf33c40SFrank Schaefer struct em28xx *dev = adap->algo_data; 501eaf33c40SFrank Schaefer u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 502eaf33c40SFrank Schaefer if (dev->board.is_em2800) 503eaf33c40SFrank Schaefer func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA; 504eaf33c40SFrank Schaefer return func_flags; 5050c0d06caSMauro Carvalho Chehab } 5060c0d06caSMauro Carvalho Chehab 5070c0d06caSMauro Carvalho Chehab static struct i2c_algorithm em28xx_algo = { 5080c0d06caSMauro Carvalho Chehab .master_xfer = em28xx_i2c_xfer, 5090c0d06caSMauro Carvalho Chehab .functionality = functionality, 5100c0d06caSMauro Carvalho Chehab }; 5110c0d06caSMauro Carvalho Chehab 5120c0d06caSMauro Carvalho Chehab static struct i2c_adapter em28xx_adap_template = { 5130c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE, 5140c0d06caSMauro Carvalho Chehab .name = "em28xx", 5150c0d06caSMauro Carvalho Chehab .algo = &em28xx_algo, 5160c0d06caSMauro Carvalho Chehab }; 5170c0d06caSMauro Carvalho Chehab 5180c0d06caSMauro Carvalho Chehab static struct i2c_client em28xx_client_template = { 5190c0d06caSMauro Carvalho Chehab .name = "em28xx internal", 5200c0d06caSMauro Carvalho Chehab }; 5210c0d06caSMauro Carvalho Chehab 5220c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 5230c0d06caSMauro Carvalho Chehab 5240c0d06caSMauro Carvalho Chehab /* 5250c0d06caSMauro Carvalho Chehab * i2c_devs 5260c0d06caSMauro Carvalho Chehab * incomplete list of known devices 5270c0d06caSMauro Carvalho Chehab */ 5280c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = { 5290b3966e4SFrank Schaefer [0x3e >> 1] = "remote IR sensor", 5300c0d06caSMauro Carvalho Chehab [0x4a >> 1] = "saa7113h", 5310c0d06caSMauro Carvalho Chehab [0x52 >> 1] = "drxk", 5320c0d06caSMauro Carvalho Chehab [0x60 >> 1] = "remote IR sensor", 5330c0d06caSMauro Carvalho Chehab [0x8e >> 1] = "remote IR sensor", 5340c0d06caSMauro Carvalho Chehab [0x86 >> 1] = "tda9887", 5350c0d06caSMauro Carvalho Chehab [0x80 >> 1] = "msp34xx", 5360c0d06caSMauro Carvalho Chehab [0x88 >> 1] = "msp34xx", 5370c0d06caSMauro Carvalho Chehab [0xa0 >> 1] = "eeprom", 5380c0d06caSMauro Carvalho Chehab [0xb0 >> 1] = "tda9874", 5390c0d06caSMauro Carvalho Chehab [0xb8 >> 1] = "tvp5150a", 5400c0d06caSMauro Carvalho Chehab [0xba >> 1] = "webcam sensor or tvp5150a", 5410c0d06caSMauro Carvalho Chehab [0xc0 >> 1] = "tuner (analog)", 5420c0d06caSMauro Carvalho Chehab [0xc2 >> 1] = "tuner (analog)", 5430c0d06caSMauro Carvalho Chehab [0xc4 >> 1] = "tuner (analog)", 5440c0d06caSMauro Carvalho Chehab [0xc6 >> 1] = "tuner (analog)", 5450c0d06caSMauro Carvalho Chehab }; 5460c0d06caSMauro Carvalho Chehab 5470c0d06caSMauro Carvalho Chehab /* 5480c0d06caSMauro Carvalho Chehab * do_i2c_scan() 5490c0d06caSMauro Carvalho Chehab * check i2c address range for devices 5500c0d06caSMauro Carvalho Chehab */ 5510c0d06caSMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev) 5520c0d06caSMauro Carvalho Chehab { 5530c0d06caSMauro Carvalho Chehab u8 i2c_devicelist[128]; 5540c0d06caSMauro Carvalho Chehab unsigned char buf; 5550c0d06caSMauro Carvalho Chehab int i, rc; 5560c0d06caSMauro Carvalho Chehab 5570c0d06caSMauro Carvalho Chehab memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist)); 5580c0d06caSMauro Carvalho Chehab 5590c0d06caSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { 5600c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = i; 5610c0d06caSMauro Carvalho Chehab rc = i2c_master_recv(&dev->i2c_client, &buf, 0); 5620c0d06caSMauro Carvalho Chehab if (rc < 0) 5630c0d06caSMauro Carvalho Chehab continue; 5640c0d06caSMauro Carvalho Chehab i2c_devicelist[i] = i; 56512d7ce18SFrank Schaefer em28xx_info("found i2c device @ 0x%x [%s]\n", 56612d7ce18SFrank Schaefer i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 5670c0d06caSMauro Carvalho Chehab } 5680c0d06caSMauro Carvalho Chehab 5690c0d06caSMauro Carvalho Chehab dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, 5700c0d06caSMauro Carvalho Chehab ARRAY_SIZE(i2c_devicelist), 32); 5710c0d06caSMauro Carvalho Chehab } 5720c0d06caSMauro Carvalho Chehab 5730c0d06caSMauro Carvalho Chehab /* 5740c0d06caSMauro Carvalho Chehab * em28xx_i2c_register() 5750c0d06caSMauro Carvalho Chehab * register i2c bus 5760c0d06caSMauro Carvalho Chehab */ 5770c0d06caSMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev) 5780c0d06caSMauro Carvalho Chehab { 5790c0d06caSMauro Carvalho Chehab int retval; 5800c0d06caSMauro Carvalho Chehab 5810c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg); 5820c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req); 5830c0d06caSMauro Carvalho Chehab dev->i2c_adap = em28xx_adap_template; 5840c0d06caSMauro Carvalho Chehab dev->i2c_adap.dev.parent = &dev->udev->dev; 5850c0d06caSMauro Carvalho Chehab strcpy(dev->i2c_adap.name, dev->name); 5860c0d06caSMauro Carvalho Chehab dev->i2c_adap.algo_data = dev; 5870c0d06caSMauro Carvalho Chehab i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); 5880c0d06caSMauro Carvalho Chehab 5890c0d06caSMauro Carvalho Chehab retval = i2c_add_adapter(&dev->i2c_adap); 5900c0d06caSMauro Carvalho Chehab if (retval < 0) { 5910c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n", 5920c0d06caSMauro Carvalho Chehab __func__, retval); 5930c0d06caSMauro Carvalho Chehab return retval; 5940c0d06caSMauro Carvalho Chehab } 5950c0d06caSMauro Carvalho Chehab 5960c0d06caSMauro Carvalho Chehab dev->i2c_client = em28xx_client_template; 5970c0d06caSMauro Carvalho Chehab dev->i2c_client.adapter = &dev->i2c_adap; 5980c0d06caSMauro Carvalho Chehab 5990c0d06caSMauro Carvalho Chehab retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata)); 6000c0d06caSMauro Carvalho Chehab if ((retval < 0) && (retval != -ENODEV)) { 6010c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n", 6020c0d06caSMauro Carvalho Chehab __func__, retval); 6030c0d06caSMauro Carvalho Chehab 6040c0d06caSMauro Carvalho Chehab return retval; 6050c0d06caSMauro Carvalho Chehab } 6060c0d06caSMauro Carvalho Chehab 6070c0d06caSMauro Carvalho Chehab if (i2c_scan) 6080c0d06caSMauro Carvalho Chehab em28xx_do_i2c_scan(dev); 6090c0d06caSMauro Carvalho Chehab 6100c0d06caSMauro Carvalho Chehab return 0; 6110c0d06caSMauro Carvalho Chehab } 6120c0d06caSMauro Carvalho Chehab 6130c0d06caSMauro Carvalho Chehab /* 6140c0d06caSMauro Carvalho Chehab * em28xx_i2c_unregister() 6150c0d06caSMauro Carvalho Chehab * unregister i2c_bus 6160c0d06caSMauro Carvalho Chehab */ 6170c0d06caSMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev) 6180c0d06caSMauro Carvalho Chehab { 6190c0d06caSMauro Carvalho Chehab i2c_del_adapter(&dev->i2c_adap); 6200c0d06caSMauro Carvalho Chehab return 0; 6210c0d06caSMauro Carvalho Chehab } 622