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 373*d832c5b2SFrank Schaefer /* Helper function to read data blocks from i2c clients with 8 or 16 bit 374*d832c5b2SFrank Schaefer * address width, 8 bit register width and auto incrementation been activated */ 375*d832c5b2SFrank Schaefer static int em28xx_i2c_read_block(struct em28xx *dev, u16 addr, bool addr_w16, 376*d832c5b2SFrank Schaefer u16 len, u8 *data) 377*d832c5b2SFrank Schaefer { 378*d832c5b2SFrank Schaefer int remain = len, rsize, rsize_max, ret; 379*d832c5b2SFrank Schaefer u8 buf[2]; 380*d832c5b2SFrank Schaefer 381*d832c5b2SFrank Schaefer /* Sanity check */ 382*d832c5b2SFrank Schaefer if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1)) 383*d832c5b2SFrank Schaefer return -EINVAL; 384*d832c5b2SFrank Schaefer /* Select address */ 385*d832c5b2SFrank Schaefer buf[0] = addr >> 8; 386*d832c5b2SFrank Schaefer buf[1] = addr & 0xff; 387*d832c5b2SFrank Schaefer ret = i2c_master_send(&dev->i2c_client, buf + !addr_w16, 1 + addr_w16); 388*d832c5b2SFrank Schaefer if (ret < 0) 389*d832c5b2SFrank Schaefer return ret; 390*d832c5b2SFrank Schaefer /* Read data */ 391*d832c5b2SFrank Schaefer if (dev->board.is_em2800) 392*d832c5b2SFrank Schaefer rsize_max = 4; 393*d832c5b2SFrank Schaefer else 394*d832c5b2SFrank Schaefer rsize_max = 64; 395*d832c5b2SFrank Schaefer while (remain > 0) { 396*d832c5b2SFrank Schaefer if (remain > rsize_max) 397*d832c5b2SFrank Schaefer rsize = rsize_max; 398*d832c5b2SFrank Schaefer else 399*d832c5b2SFrank Schaefer rsize = remain; 400*d832c5b2SFrank Schaefer 401*d832c5b2SFrank Schaefer ret = i2c_master_recv(&dev->i2c_client, data, rsize); 402*d832c5b2SFrank Schaefer if (ret < 0) 403*d832c5b2SFrank Schaefer return ret; 404*d832c5b2SFrank Schaefer 405*d832c5b2SFrank Schaefer remain -= rsize; 406*d832c5b2SFrank Schaefer data += rsize; 407*d832c5b2SFrank Schaefer } 408*d832c5b2SFrank Schaefer 409*d832c5b2SFrank Schaefer return len; 410*d832c5b2SFrank Schaefer } 411*d832c5b2SFrank Schaefer 4120c0d06caSMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) 4130c0d06caSMauro Carvalho Chehab { 414*d832c5b2SFrank Schaefer unsigned char buf, *p = eedata; 4150c0d06caSMauro Carvalho Chehab struct em28xx_eeprom *em_eeprom = (void *)eedata; 416*d832c5b2SFrank Schaefer int i, err; 4170c0d06caSMauro Carvalho Chehab 4180c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = 0xa0 >> 1; 4190c0d06caSMauro Carvalho Chehab 4200c0d06caSMauro Carvalho Chehab /* Check if board has eeprom */ 421*d832c5b2SFrank Schaefer err = i2c_master_recv(&dev->i2c_client, &buf, 0); 4220c0d06caSMauro Carvalho Chehab if (err < 0) { 42312d7ce18SFrank Schaefer em28xx_info("board has no eeprom\n"); 4240c0d06caSMauro Carvalho Chehab memset(eedata, 0, len); 4250c0d06caSMauro Carvalho Chehab return -ENODEV; 4260c0d06caSMauro Carvalho Chehab } 4270c0d06caSMauro Carvalho Chehab 428*d832c5b2SFrank Schaefer /* Read EEPROM content */ 429*d832c5b2SFrank Schaefer err = em28xx_i2c_read_block(dev, 0x0000, dev->eeprom_addrwidth_16bit, 430*d832c5b2SFrank Schaefer len, p); 431*d832c5b2SFrank Schaefer if (err != len) { 43212d7ce18SFrank Schaefer em28xx_errdev("failed to read eeprom (err=%d)\n", err); 4330c0d06caSMauro Carvalho Chehab return err; 4340c0d06caSMauro Carvalho Chehab } 43590271964SFrank Schaefer 43687b52439SFrank Schaefer /* Display eeprom content */ 4370c0d06caSMauro Carvalho Chehab for (i = 0; i < len; i++) { 43887b52439SFrank Schaefer if (0 == (i % 16)) { 43987b52439SFrank Schaefer if (dev->eeprom_addrwidth_16bit) 44087b52439SFrank Schaefer em28xx_info("i2c eeprom %04x:", i); 44187b52439SFrank Schaefer else 44212d7ce18SFrank Schaefer em28xx_info("i2c eeprom %02x:", i); 44387b52439SFrank Schaefer } 4440c0d06caSMauro Carvalho Chehab printk(" %02x", eedata[i]); 4450c0d06caSMauro Carvalho Chehab if (15 == (i % 16)) 4460c0d06caSMauro Carvalho Chehab printk("\n"); 4470c0d06caSMauro Carvalho Chehab } 4480c0d06caSMauro Carvalho Chehab 44987b52439SFrank Schaefer if (dev->eeprom_addrwidth_16bit && 45087b52439SFrank Schaefer eedata[0] == 0x26 && eedata[3] == 0x00) { 45187b52439SFrank Schaefer /* new eeprom format; size 4-64kb */ 45287b52439SFrank Schaefer dev->hash = em28xx_hash_mem(eedata, len, 32); 45387b52439SFrank Schaefer em28xx_info("EEPROM hash = 0x%08lx\n", dev->hash); 45487b52439SFrank Schaefer em28xx_info("EEPROM info: boot page address = 0x%02x04, " 45587b52439SFrank Schaefer "boot configuration = 0x%02x\n", 45687b52439SFrank Schaefer eedata[1], eedata[2]); 45787b52439SFrank Schaefer /* boot configuration (address 0x0002): 45887b52439SFrank Schaefer * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz 45987b52439SFrank Schaefer * [1] always selects 12 kb RAM 46087b52439SFrank Schaefer * [2] USB device speed: 1 = force Full Speed; 0 = auto detect 46187b52439SFrank Schaefer * [4] 1 = force fast mode and no suspend for device testing 46287b52439SFrank Schaefer * [5:7] USB PHY tuning registers; determined by device 46387b52439SFrank Schaefer * characterization 46487b52439SFrank Schaefer */ 46587b52439SFrank Schaefer 46687b52439SFrank Schaefer /* FIXME: 46787b52439SFrank Schaefer * - read more than 256 bytes / addresses above 0x00ff 46887b52439SFrank Schaefer * - find offset for device config dataset and extract it 46987b52439SFrank Schaefer * - decrypt eeprom data for camera bridges (em25xx, em276x+) 47087b52439SFrank Schaefer * - use separate/different eeprom hashes (not yet used) 47187b52439SFrank Schaefer */ 47287b52439SFrank Schaefer 47387b52439SFrank Schaefer return 0; 47487b52439SFrank Schaefer } else if (em_eeprom->id[0] != 0x1a || em_eeprom->id[1] != 0xeb || 4750c28dcc0SFrank Schaefer em_eeprom->id[2] != 0x67 || em_eeprom->id[3] != 0x95) { 47687b52439SFrank Schaefer em28xx_info("unknown eeprom format or eeprom corrupted !\n"); 477f55eacbeSFrank Schaefer return -ENODEV; 478f55eacbeSFrank Schaefer } 479f55eacbeSFrank Schaefer 4800c0d06caSMauro Carvalho Chehab dev->hash = em28xx_hash_mem(eedata, len, 32); 4810c0d06caSMauro Carvalho Chehab 4820c28dcc0SFrank Schaefer em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n", 4830c28dcc0SFrank Schaefer em_eeprom->id[0], em_eeprom->id[1], 4840c28dcc0SFrank Schaefer em_eeprom->id[2], em_eeprom->id[3], dev->hash); 4850c0d06caSMauro Carvalho Chehab 48612d7ce18SFrank Schaefer em28xx_info("EEPROM info:\n"); 4870c0d06caSMauro Carvalho Chehab 4880c28dcc0SFrank Schaefer switch (le16_to_cpu(em_eeprom->chip_conf) >> 4 & 0x3) { 4890c0d06caSMauro Carvalho Chehab case 0: 49012d7ce18SFrank Schaefer em28xx_info("\tNo audio on board.\n"); 4910c0d06caSMauro Carvalho Chehab break; 4920c0d06caSMauro Carvalho Chehab case 1: 49312d7ce18SFrank Schaefer em28xx_info("\tAC97 audio (5 sample rates)\n"); 4940c0d06caSMauro Carvalho Chehab break; 4950c0d06caSMauro Carvalho Chehab case 2: 49612d7ce18SFrank Schaefer em28xx_info("\tI2S audio, sample rate=32k\n"); 4970c0d06caSMauro Carvalho Chehab break; 4980c0d06caSMauro Carvalho Chehab case 3: 49912d7ce18SFrank Schaefer em28xx_info("\tI2S audio, 3 sample rates\n"); 5000c0d06caSMauro Carvalho Chehab break; 5010c0d06caSMauro Carvalho Chehab } 5020c0d06caSMauro Carvalho Chehab 5030c28dcc0SFrank Schaefer if (le16_to_cpu(em_eeprom->chip_conf) & 1 << 3) 50412d7ce18SFrank Schaefer em28xx_info("\tUSB Remote wakeup capable\n"); 5050c0d06caSMauro Carvalho Chehab 5060c28dcc0SFrank Schaefer if (le16_to_cpu(em_eeprom->chip_conf) & 1 << 2) 50712d7ce18SFrank Schaefer em28xx_info("\tUSB Self power capable\n"); 5080c0d06caSMauro Carvalho Chehab 5090c28dcc0SFrank Schaefer switch (le16_to_cpu(em_eeprom->chip_conf) & 0x3) { 5100c0d06caSMauro Carvalho Chehab case 0: 51112d7ce18SFrank Schaefer em28xx_info("\t500mA max power\n"); 5120c0d06caSMauro Carvalho Chehab break; 5130c0d06caSMauro Carvalho Chehab case 1: 51412d7ce18SFrank Schaefer em28xx_info("\t400mA max power\n"); 5150c0d06caSMauro Carvalho Chehab break; 5160c0d06caSMauro Carvalho Chehab case 2: 51712d7ce18SFrank Schaefer em28xx_info("\t300mA max power\n"); 5180c0d06caSMauro Carvalho Chehab break; 5190c0d06caSMauro Carvalho Chehab case 3: 52012d7ce18SFrank Schaefer em28xx_info("\t200mA max power\n"); 5210c0d06caSMauro Carvalho Chehab break; 5220c0d06caSMauro Carvalho Chehab } 52312d7ce18SFrank Schaefer em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n", 5240c0d06caSMauro Carvalho Chehab em_eeprom->string_idx_table, 5250c28dcc0SFrank Schaefer le16_to_cpu(em_eeprom->string1), 5260c28dcc0SFrank Schaefer le16_to_cpu(em_eeprom->string2), 5270c28dcc0SFrank Schaefer le16_to_cpu(em_eeprom->string3)); 5280c0d06caSMauro Carvalho Chehab 5290c0d06caSMauro Carvalho Chehab return 0; 5300c0d06caSMauro Carvalho Chehab } 5310c0d06caSMauro Carvalho Chehab 5320c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 5330c0d06caSMauro Carvalho Chehab 5340c0d06caSMauro Carvalho Chehab /* 5350c0d06caSMauro Carvalho Chehab * functionality() 5360c0d06caSMauro Carvalho Chehab */ 5370c0d06caSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *adap) 5380c0d06caSMauro Carvalho Chehab { 539eaf33c40SFrank Schaefer struct em28xx *dev = adap->algo_data; 540eaf33c40SFrank Schaefer u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 541eaf33c40SFrank Schaefer if (dev->board.is_em2800) 542eaf33c40SFrank Schaefer func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA; 543eaf33c40SFrank Schaefer return func_flags; 5440c0d06caSMauro Carvalho Chehab } 5450c0d06caSMauro Carvalho Chehab 5460c0d06caSMauro Carvalho Chehab static struct i2c_algorithm em28xx_algo = { 5470c0d06caSMauro Carvalho Chehab .master_xfer = em28xx_i2c_xfer, 5480c0d06caSMauro Carvalho Chehab .functionality = functionality, 5490c0d06caSMauro Carvalho Chehab }; 5500c0d06caSMauro Carvalho Chehab 5510c0d06caSMauro Carvalho Chehab static struct i2c_adapter em28xx_adap_template = { 5520c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE, 5530c0d06caSMauro Carvalho Chehab .name = "em28xx", 5540c0d06caSMauro Carvalho Chehab .algo = &em28xx_algo, 5550c0d06caSMauro Carvalho Chehab }; 5560c0d06caSMauro Carvalho Chehab 5570c0d06caSMauro Carvalho Chehab static struct i2c_client em28xx_client_template = { 5580c0d06caSMauro Carvalho Chehab .name = "em28xx internal", 5590c0d06caSMauro Carvalho Chehab }; 5600c0d06caSMauro Carvalho Chehab 5610c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 5620c0d06caSMauro Carvalho Chehab 5630c0d06caSMauro Carvalho Chehab /* 5640c0d06caSMauro Carvalho Chehab * i2c_devs 5650c0d06caSMauro Carvalho Chehab * incomplete list of known devices 5660c0d06caSMauro Carvalho Chehab */ 5670c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = { 5680b3966e4SFrank Schaefer [0x3e >> 1] = "remote IR sensor", 5690c0d06caSMauro Carvalho Chehab [0x4a >> 1] = "saa7113h", 5700c0d06caSMauro Carvalho Chehab [0x52 >> 1] = "drxk", 5710c0d06caSMauro Carvalho Chehab [0x60 >> 1] = "remote IR sensor", 5720c0d06caSMauro Carvalho Chehab [0x8e >> 1] = "remote IR sensor", 5730c0d06caSMauro Carvalho Chehab [0x86 >> 1] = "tda9887", 5740c0d06caSMauro Carvalho Chehab [0x80 >> 1] = "msp34xx", 5750c0d06caSMauro Carvalho Chehab [0x88 >> 1] = "msp34xx", 5760c0d06caSMauro Carvalho Chehab [0xa0 >> 1] = "eeprom", 5770c0d06caSMauro Carvalho Chehab [0xb0 >> 1] = "tda9874", 5780c0d06caSMauro Carvalho Chehab [0xb8 >> 1] = "tvp5150a", 5790c0d06caSMauro Carvalho Chehab [0xba >> 1] = "webcam sensor or tvp5150a", 5800c0d06caSMauro Carvalho Chehab [0xc0 >> 1] = "tuner (analog)", 5810c0d06caSMauro Carvalho Chehab [0xc2 >> 1] = "tuner (analog)", 5820c0d06caSMauro Carvalho Chehab [0xc4 >> 1] = "tuner (analog)", 5830c0d06caSMauro Carvalho Chehab [0xc6 >> 1] = "tuner (analog)", 5840c0d06caSMauro Carvalho Chehab }; 5850c0d06caSMauro Carvalho Chehab 5860c0d06caSMauro Carvalho Chehab /* 5870c0d06caSMauro Carvalho Chehab * do_i2c_scan() 5880c0d06caSMauro Carvalho Chehab * check i2c address range for devices 5890c0d06caSMauro Carvalho Chehab */ 5900c0d06caSMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev) 5910c0d06caSMauro Carvalho Chehab { 5920c0d06caSMauro Carvalho Chehab u8 i2c_devicelist[128]; 5930c0d06caSMauro Carvalho Chehab unsigned char buf; 5940c0d06caSMauro Carvalho Chehab int i, rc; 5950c0d06caSMauro Carvalho Chehab 5960c0d06caSMauro Carvalho Chehab memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist)); 5970c0d06caSMauro Carvalho Chehab 5980c0d06caSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { 5990c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = i; 6000c0d06caSMauro Carvalho Chehab rc = i2c_master_recv(&dev->i2c_client, &buf, 0); 6010c0d06caSMauro Carvalho Chehab if (rc < 0) 6020c0d06caSMauro Carvalho Chehab continue; 6030c0d06caSMauro Carvalho Chehab i2c_devicelist[i] = i; 60412d7ce18SFrank Schaefer em28xx_info("found i2c device @ 0x%x [%s]\n", 60512d7ce18SFrank Schaefer i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 6060c0d06caSMauro Carvalho Chehab } 6070c0d06caSMauro Carvalho Chehab 6080c0d06caSMauro Carvalho Chehab dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, 6090c0d06caSMauro Carvalho Chehab ARRAY_SIZE(i2c_devicelist), 32); 6100c0d06caSMauro Carvalho Chehab } 6110c0d06caSMauro Carvalho Chehab 6120c0d06caSMauro Carvalho Chehab /* 6130c0d06caSMauro Carvalho Chehab * em28xx_i2c_register() 6140c0d06caSMauro Carvalho Chehab * register i2c bus 6150c0d06caSMauro Carvalho Chehab */ 6160c0d06caSMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev) 6170c0d06caSMauro Carvalho Chehab { 6180c0d06caSMauro Carvalho Chehab int retval; 6190c0d06caSMauro Carvalho Chehab 6200c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg); 6210c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req); 6220c0d06caSMauro Carvalho Chehab dev->i2c_adap = em28xx_adap_template; 6230c0d06caSMauro Carvalho Chehab dev->i2c_adap.dev.parent = &dev->udev->dev; 6240c0d06caSMauro Carvalho Chehab strcpy(dev->i2c_adap.name, dev->name); 6250c0d06caSMauro Carvalho Chehab dev->i2c_adap.algo_data = dev; 6260c0d06caSMauro Carvalho Chehab i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); 6270c0d06caSMauro Carvalho Chehab 6280c0d06caSMauro Carvalho Chehab retval = i2c_add_adapter(&dev->i2c_adap); 6290c0d06caSMauro Carvalho Chehab if (retval < 0) { 6300c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n", 6310c0d06caSMauro Carvalho Chehab __func__, retval); 6320c0d06caSMauro Carvalho Chehab return retval; 6330c0d06caSMauro Carvalho Chehab } 6340c0d06caSMauro Carvalho Chehab 6350c0d06caSMauro Carvalho Chehab dev->i2c_client = em28xx_client_template; 6360c0d06caSMauro Carvalho Chehab dev->i2c_client.adapter = &dev->i2c_adap; 6370c0d06caSMauro Carvalho Chehab 6380c0d06caSMauro Carvalho Chehab retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata)); 6390c0d06caSMauro Carvalho Chehab if ((retval < 0) && (retval != -ENODEV)) { 6400c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n", 6410c0d06caSMauro Carvalho Chehab __func__, retval); 6420c0d06caSMauro Carvalho Chehab 6430c0d06caSMauro Carvalho Chehab return retval; 6440c0d06caSMauro Carvalho Chehab } 6450c0d06caSMauro Carvalho Chehab 6460c0d06caSMauro Carvalho Chehab if (i2c_scan) 6470c0d06caSMauro Carvalho Chehab em28xx_do_i2c_scan(dev); 6480c0d06caSMauro Carvalho Chehab 6490c0d06caSMauro Carvalho Chehab return 0; 6500c0d06caSMauro Carvalho Chehab } 6510c0d06caSMauro Carvalho Chehab 6520c0d06caSMauro Carvalho Chehab /* 6530c0d06caSMauro Carvalho Chehab * em28xx_i2c_unregister() 6540c0d06caSMauro Carvalho Chehab * unregister i2c_bus 6550c0d06caSMauro Carvalho Chehab */ 6560c0d06caSMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev) 6570c0d06caSMauro Carvalho Chehab { 6580c0d06caSMauro Carvalho Chehab i2c_del_adapter(&dev->i2c_adap); 6590c0d06caSMauro Carvalho Chehab return 0; 6600c0d06caSMauro Carvalho Chehab } 661