10c0d06caSMauro Carvalho Chehab /* 20c0d06caSMauro Carvalho Chehab em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices 30c0d06caSMauro Carvalho Chehab 40c0d06caSMauro Carvalho Chehab Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 50c0d06caSMauro Carvalho Chehab Markus Rechberger <mrechberger@gmail.com> 60c0d06caSMauro Carvalho Chehab Mauro Carvalho Chehab <mchehab@infradead.org> 70c0d06caSMauro Carvalho Chehab Sascha Sommer <saschasommer@freenet.de> 80c0d06caSMauro Carvalho Chehab 90c0d06caSMauro Carvalho Chehab This program is free software; you can redistribute it and/or modify 100c0d06caSMauro Carvalho Chehab it under the terms of the GNU General Public License as published by 110c0d06caSMauro Carvalho Chehab the Free Software Foundation; either version 2 of the License, or 120c0d06caSMauro Carvalho Chehab (at your option) any later version. 130c0d06caSMauro Carvalho Chehab 140c0d06caSMauro Carvalho Chehab This program is distributed in the hope that it will be useful, 150c0d06caSMauro Carvalho Chehab but WITHOUT ANY WARRANTY; without even the implied warranty of 160c0d06caSMauro Carvalho Chehab MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 170c0d06caSMauro Carvalho Chehab GNU General Public License for more details. 180c0d06caSMauro Carvalho Chehab 190c0d06caSMauro Carvalho Chehab You should have received a copy of the GNU General Public License 200c0d06caSMauro Carvalho Chehab along with this program; if not, write to the Free Software 210c0d06caSMauro Carvalho Chehab Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 220c0d06caSMauro Carvalho Chehab */ 230c0d06caSMauro Carvalho Chehab 240c0d06caSMauro Carvalho Chehab #include <linux/module.h> 250c0d06caSMauro Carvalho Chehab #include <linux/kernel.h> 260c0d06caSMauro Carvalho Chehab #include <linux/usb.h> 270c0d06caSMauro Carvalho Chehab #include <linux/i2c.h> 280c0d06caSMauro Carvalho Chehab 290c0d06caSMauro Carvalho Chehab #include "em28xx.h" 300c0d06caSMauro Carvalho Chehab #include "tuner-xc2028.h" 310c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h> 320c0d06caSMauro Carvalho Chehab #include <media/tuner.h> 330c0d06caSMauro Carvalho Chehab 340c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 350c0d06caSMauro Carvalho Chehab 360c0d06caSMauro Carvalho Chehab static unsigned int i2c_scan; 370c0d06caSMauro Carvalho Chehab module_param(i2c_scan, int, 0444); 380c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); 390c0d06caSMauro Carvalho Chehab 400c0d06caSMauro Carvalho Chehab static unsigned int i2c_debug; 410c0d06caSMauro Carvalho Chehab module_param(i2c_debug, int, 0644); 420c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 430c0d06caSMauro Carvalho Chehab 440c0d06caSMauro Carvalho Chehab #define dprintk2(lvl, fmt, args...) \ 450c0d06caSMauro Carvalho Chehab do { \ 460c0d06caSMauro Carvalho Chehab if (i2c_debug >= lvl) { \ 470c0d06caSMauro Carvalho Chehab printk(KERN_DEBUG "%s at %s: " fmt, \ 480c0d06caSMauro Carvalho Chehab dev->name, __func__ , ##args); \ 490c0d06caSMauro Carvalho Chehab } \ 500c0d06caSMauro Carvalho Chehab } while (0) 510c0d06caSMauro Carvalho Chehab 520c0d06caSMauro Carvalho Chehab /* 53f5ae371aSFrank Schaefer * em2800_i2c_send_bytes() 54f5ae371aSFrank Schaefer * send up to 4 bytes to the em2800 i2c device 550c0d06caSMauro Carvalho Chehab */ 56f5ae371aSFrank Schaefer static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) 570c0d06caSMauro Carvalho Chehab { 580c0d06caSMauro Carvalho Chehab int ret; 590c0d06caSMauro Carvalho Chehab int write_timeout; 60a6bad040SFrank Schaefer u8 b2[6]; 61f5ae371aSFrank Schaefer 62f5ae371aSFrank Schaefer if (len < 1 || len > 4) 63f5ae371aSFrank Schaefer return -EOPNOTSUPP; 64f5ae371aSFrank Schaefer 650c0d06caSMauro Carvalho Chehab BUG_ON(len < 1 || len > 4); 660c0d06caSMauro Carvalho Chehab b2[5] = 0x80 + len - 1; 670c0d06caSMauro Carvalho Chehab b2[4] = addr; 680c0d06caSMauro Carvalho Chehab b2[3] = buf[0]; 690c0d06caSMauro Carvalho Chehab if (len > 1) 700c0d06caSMauro Carvalho Chehab b2[2] = buf[1]; 710c0d06caSMauro Carvalho Chehab if (len > 2) 720c0d06caSMauro Carvalho Chehab b2[1] = buf[2]; 730c0d06caSMauro Carvalho Chehab if (len > 3) 740c0d06caSMauro Carvalho Chehab b2[0] = buf[3]; 750c0d06caSMauro Carvalho Chehab 762fcc82d8SFrank Schaefer /* trigger write */ 770c0d06caSMauro Carvalho Chehab ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len); 780c0d06caSMauro Carvalho Chehab if (ret != 2 + len) { 7945f04e82SFrank Schaefer em28xx_warn("failed to trigger write to i2c address 0x%x " 8045f04e82SFrank Schaefer "(error=%i)\n", addr, ret); 8145f04e82SFrank Schaefer return (ret < 0) ? ret : -EIO; 820c0d06caSMauro Carvalho Chehab } 832fcc82d8SFrank Schaefer /* wait for completion */ 842fcc82d8SFrank Schaefer for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0; 850c0d06caSMauro Carvalho Chehab write_timeout -= 5) { 860c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg(dev, 0x05); 8745f04e82SFrank Schaefer if (ret == 0x80 + len - 1) { 880c0d06caSMauro Carvalho Chehab return len; 8945f04e82SFrank Schaefer } else if (ret == 0x94 + len - 1) { 9045f04e82SFrank Schaefer return -ENODEV; 9145f04e82SFrank Schaefer } else if (ret < 0) { 9245f04e82SFrank Schaefer em28xx_warn("failed to get i2c transfer status from " 9345f04e82SFrank Schaefer "bridge register (error=%i)\n", ret); 9445f04e82SFrank Schaefer return ret; 9545f04e82SFrank Schaefer } 960c0d06caSMauro Carvalho Chehab msleep(5); 970c0d06caSMauro Carvalho Chehab } 9845f04e82SFrank Schaefer em28xx_warn("write to i2c device at 0x%x timed out\n", addr); 990c0d06caSMauro Carvalho Chehab return -EIO; 1000c0d06caSMauro Carvalho Chehab } 1010c0d06caSMauro Carvalho Chehab 1020c0d06caSMauro Carvalho Chehab /* 1030c0d06caSMauro Carvalho Chehab * em2800_i2c_recv_bytes() 1042fcc82d8SFrank Schaefer * read up to 4 bytes from the em2800 i2c device 1050c0d06caSMauro Carvalho Chehab */ 106a6bad040SFrank Schaefer static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) 1070c0d06caSMauro Carvalho Chehab { 1082fcc82d8SFrank Schaefer u8 buf2[4]; 1090c0d06caSMauro Carvalho Chehab int ret; 1102fcc82d8SFrank Schaefer int read_timeout; 1112fcc82d8SFrank Schaefer int i; 112f5ae371aSFrank Schaefer 113f5ae371aSFrank Schaefer if (len < 1 || len > 4) 114f5ae371aSFrank Schaefer return -EOPNOTSUPP; 115f5ae371aSFrank Schaefer 1162fcc82d8SFrank Schaefer /* trigger read */ 1172fcc82d8SFrank Schaefer buf2[1] = 0x84 + len - 1; 1182fcc82d8SFrank Schaefer buf2[0] = addr; 1192fcc82d8SFrank Schaefer ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2); 1202fcc82d8SFrank Schaefer if (ret != 2) { 1212fcc82d8SFrank Schaefer em28xx_warn("failed to trigger read from i2c address 0x%x " 1222fcc82d8SFrank Schaefer "(error=%i)\n", addr, ret); 1232fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO; 1242fcc82d8SFrank Schaefer } 1252fcc82d8SFrank Schaefer 1262fcc82d8SFrank Schaefer /* wait for completion */ 1272fcc82d8SFrank Schaefer for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0; 1282fcc82d8SFrank Schaefer read_timeout -= 5) { 1292fcc82d8SFrank Schaefer ret = dev->em28xx_read_reg(dev, 0x05); 1302fcc82d8SFrank Schaefer if (ret == 0x84 + len - 1) { 1312fcc82d8SFrank Schaefer break; 1322fcc82d8SFrank Schaefer } else if (ret == 0x94 + len - 1) { 1332fcc82d8SFrank Schaefer return -ENODEV; 1342fcc82d8SFrank Schaefer } else if (ret < 0) { 1352fcc82d8SFrank Schaefer em28xx_warn("failed to get i2c transfer status from " 1362fcc82d8SFrank Schaefer "bridge register (error=%i)\n", ret); 1370c0d06caSMauro Carvalho Chehab return ret; 1380c0d06caSMauro Carvalho Chehab } 1392fcc82d8SFrank Schaefer msleep(5); 1402fcc82d8SFrank Schaefer } 1412fcc82d8SFrank Schaefer if (ret != 0x84 + len - 1) 1422fcc82d8SFrank Schaefer em28xx_warn("read from i2c device at 0x%x timed out\n", addr); 1432fcc82d8SFrank Schaefer 1442fcc82d8SFrank Schaefer /* get the received message */ 1452fcc82d8SFrank Schaefer ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len); 1462fcc82d8SFrank Schaefer if (ret != len) { 1472fcc82d8SFrank Schaefer em28xx_warn("reading from i2c device at 0x%x failed: " 1482fcc82d8SFrank Schaefer "couldn't get the received message from the bridge " 1492fcc82d8SFrank Schaefer "(error=%i)\n", addr, ret); 1502fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO; 1512fcc82d8SFrank Schaefer } 1522fcc82d8SFrank Schaefer for (i = 0; i < len; i++) 1532fcc82d8SFrank Schaefer buf[i] = buf2[len - 1 - i]; 1542fcc82d8SFrank Schaefer 1550c0d06caSMauro Carvalho Chehab return ret; 1560c0d06caSMauro Carvalho Chehab } 1572fcc82d8SFrank Schaefer 1582fcc82d8SFrank Schaefer /* 1592fcc82d8SFrank Schaefer * em2800_i2c_check_for_device() 1602fcc82d8SFrank Schaefer * check if there is an i2c device at the supplied address 1612fcc82d8SFrank Schaefer */ 1622fcc82d8SFrank Schaefer static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr) 1632fcc82d8SFrank Schaefer { 1642fcc82d8SFrank Schaefer u8 buf; 1652fcc82d8SFrank Schaefer int ret; 1662fcc82d8SFrank Schaefer 1672fcc82d8SFrank Schaefer ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1); 1682fcc82d8SFrank Schaefer if (ret == 1) 1692fcc82d8SFrank Schaefer return 0; 1702fcc82d8SFrank Schaefer return (ret < 0) ? ret : -EIO; 1710c0d06caSMauro Carvalho Chehab } 1720c0d06caSMauro Carvalho Chehab 1730c0d06caSMauro Carvalho Chehab /* 1740c0d06caSMauro Carvalho Chehab * em28xx_i2c_send_bytes() 1750c0d06caSMauro Carvalho Chehab */ 176a6bad040SFrank Schaefer static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf, 177a6bad040SFrank Schaefer u16 len, int stop) 1780c0d06caSMauro Carvalho Chehab { 1790c0d06caSMauro Carvalho Chehab int write_timeout, ret; 1800c0d06caSMauro Carvalho Chehab 181f5ae371aSFrank Schaefer if (len < 1 || len > 64) 182f5ae371aSFrank Schaefer return -EOPNOTSUPP; 18345f04e82SFrank Schaefer /* NOTE: limited by the USB ctrl message constraints 18445f04e82SFrank Schaefer * Zero length reads always succeed, even if no device is connected */ 185f5ae371aSFrank Schaefer 18645f04e82SFrank Schaefer /* Write to i2c device */ 18745f04e82SFrank Schaefer ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len); 18845f04e82SFrank Schaefer if (ret != len) { 18945f04e82SFrank Schaefer if (ret < 0) { 19045f04e82SFrank Schaefer em28xx_warn("writing to i2c device at 0x%x failed " 19145f04e82SFrank Schaefer "(error=%i)\n", addr, ret); 19245f04e82SFrank Schaefer return ret; 19345f04e82SFrank Schaefer } else { 19445f04e82SFrank Schaefer em28xx_warn("%i bytes write to i2c device at 0x%x " 19545f04e82SFrank Schaefer "requested, but %i bytes written\n", 19645f04e82SFrank Schaefer len, addr, ret); 19745f04e82SFrank Schaefer return -EIO; 19845f04e82SFrank Schaefer } 19945f04e82SFrank Schaefer } 2000c0d06caSMauro Carvalho Chehab 20145f04e82SFrank Schaefer /* Check success of the i2c operation */ 2022fcc82d8SFrank Schaefer for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0; 2030c0d06caSMauro Carvalho Chehab write_timeout -= 5) { 2040c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg(dev, 0x05); 20545f04e82SFrank Schaefer if (ret == 0) { /* success */ 20645f04e82SFrank Schaefer return len; 20745f04e82SFrank Schaefer } else if (ret == 0x10) { 20845f04e82SFrank Schaefer return -ENODEV; 20945f04e82SFrank Schaefer } else if (ret < 0) { 21045f04e82SFrank Schaefer em28xx_warn("failed to read i2c transfer status from " 21145f04e82SFrank Schaefer "bridge (error=%i)\n", ret); 21245f04e82SFrank Schaefer return ret; 2130c0d06caSMauro Carvalho Chehab } 21445f04e82SFrank Schaefer msleep(5); 21545f04e82SFrank Schaefer /* NOTE: do we really have to wait for success ? 21645f04e82SFrank Schaefer Never seen anything else than 0x00 or 0x10 21745f04e82SFrank Schaefer (even with high payload) ... */ 21845f04e82SFrank Schaefer } 21945f04e82SFrank Schaefer em28xx_warn("write to i2c device at 0x%x timed out\n", addr); 22045f04e82SFrank Schaefer return -EIO; 2210c0d06caSMauro Carvalho Chehab } 2220c0d06caSMauro Carvalho Chehab 2230c0d06caSMauro Carvalho Chehab /* 2240c0d06caSMauro Carvalho Chehab * em28xx_i2c_recv_bytes() 2250c0d06caSMauro Carvalho Chehab * read a byte from the i2c device 2260c0d06caSMauro Carvalho Chehab */ 227a6bad040SFrank Schaefer static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len) 2280c0d06caSMauro Carvalho Chehab { 2290c0d06caSMauro Carvalho Chehab int ret; 230f5ae371aSFrank Schaefer 231f5ae371aSFrank Schaefer if (len < 1 || len > 64) 232f5ae371aSFrank Schaefer return -EOPNOTSUPP; 23345f04e82SFrank Schaefer /* NOTE: limited by the USB ctrl message constraints 23445f04e82SFrank Schaefer * Zero length reads always succeed, even if no device is connected */ 235f5ae371aSFrank Schaefer 23645f04e82SFrank Schaefer /* Read data from i2c device */ 2370c0d06caSMauro Carvalho Chehab ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len); 23845f04e82SFrank Schaefer if (ret != len) { 2390c0d06caSMauro Carvalho Chehab if (ret < 0) { 24045f04e82SFrank Schaefer em28xx_warn("reading from i2c device at 0x%x failed " 24145f04e82SFrank Schaefer "(error=%i)\n", addr, ret); 24245f04e82SFrank Schaefer return ret; 24345f04e82SFrank Schaefer } else { 24445f04e82SFrank Schaefer em28xx_warn("%i bytes requested from i2c device at " 24545f04e82SFrank Schaefer "0x%x, but %i bytes received\n", 24645f04e82SFrank Schaefer len, addr, ret); 24745f04e82SFrank Schaefer return -EIO; 24845f04e82SFrank Schaefer } 24945f04e82SFrank Schaefer } 25045f04e82SFrank Schaefer 25145f04e82SFrank Schaefer /* Check success of the i2c operation */ 25245f04e82SFrank Schaefer ret = dev->em28xx_read_reg(dev, 0x05); 25345f04e82SFrank Schaefer if (ret < 0) { 25445f04e82SFrank Schaefer em28xx_warn("failed to read i2c transfer status from " 25545f04e82SFrank Schaefer "bridge (error=%i)\n", ret); 2560c0d06caSMauro Carvalho Chehab return ret; 2570c0d06caSMauro Carvalho Chehab } 25845f04e82SFrank Schaefer if (ret > 0) { 25945f04e82SFrank Schaefer if (ret == 0x10) { 2600c0d06caSMauro Carvalho Chehab return -ENODEV; 26145f04e82SFrank Schaefer } else { 26245f04e82SFrank Schaefer em28xx_warn("unknown i2c error (status=%i)\n", ret); 26345f04e82SFrank Schaefer return -EIO; 26445f04e82SFrank Schaefer } 26545f04e82SFrank Schaefer } 26645f04e82SFrank Schaefer return len; 2670c0d06caSMauro Carvalho Chehab } 2680c0d06caSMauro Carvalho Chehab 2690c0d06caSMauro Carvalho Chehab /* 2700c0d06caSMauro Carvalho Chehab * em28xx_i2c_check_for_device() 2710c0d06caSMauro Carvalho Chehab * check if there is a i2c_device at the supplied address 2720c0d06caSMauro Carvalho Chehab */ 273a6bad040SFrank Schaefer static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr) 2740c0d06caSMauro Carvalho Chehab { 2750c0d06caSMauro Carvalho Chehab int ret; 27645f04e82SFrank Schaefer u8 buf; 2770c0d06caSMauro Carvalho Chehab 27845f04e82SFrank Schaefer ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1); 27945f04e82SFrank Schaefer if (ret == 1) 2800c0d06caSMauro Carvalho Chehab return 0; 28145f04e82SFrank Schaefer return (ret < 0) ? ret : -EIO; 2820c0d06caSMauro Carvalho Chehab } 2830c0d06caSMauro Carvalho Chehab 2840c0d06caSMauro Carvalho Chehab /* 2850c0d06caSMauro Carvalho Chehab * em28xx_i2c_xfer() 2860c0d06caSMauro Carvalho Chehab * the main i2c transfer function 2870c0d06caSMauro Carvalho Chehab */ 2880c0d06caSMauro Carvalho Chehab static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap, 2890c0d06caSMauro Carvalho Chehab struct i2c_msg msgs[], int num) 2900c0d06caSMauro Carvalho Chehab { 2910c0d06caSMauro Carvalho Chehab struct em28xx *dev = i2c_adap->algo_data; 2920c0d06caSMauro Carvalho Chehab int addr, rc, i, byte; 2930c0d06caSMauro Carvalho Chehab 2940c0d06caSMauro Carvalho Chehab if (num <= 0) 2950c0d06caSMauro Carvalho Chehab return 0; 2960c0d06caSMauro Carvalho Chehab for (i = 0; i < num; i++) { 2970c0d06caSMauro Carvalho Chehab addr = msgs[i].addr << 1; 2980c0d06caSMauro Carvalho Chehab dprintk2(2, "%s %s addr=%x len=%d:", 2990c0d06caSMauro Carvalho Chehab (msgs[i].flags & I2C_M_RD) ? "read" : "write", 3000c0d06caSMauro Carvalho Chehab i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len); 3010c0d06caSMauro Carvalho Chehab if (!msgs[i].len) { /* no len: check only for device presence */ 3020c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 3030c0d06caSMauro Carvalho Chehab rc = em2800_i2c_check_for_device(dev, addr); 3040c0d06caSMauro Carvalho Chehab else 3050c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_check_for_device(dev, addr); 30645f04e82SFrank Schaefer if (rc == -ENODEV) { 30745f04e82SFrank Schaefer if (i2c_debug >= 2) 30845f04e82SFrank Schaefer printk(" no device\n"); 3090c0d06caSMauro Carvalho Chehab return rc; 3100c0d06caSMauro Carvalho Chehab } 3110c0d06caSMauro Carvalho Chehab } else if (msgs[i].flags & I2C_M_RD) { 3120c0d06caSMauro Carvalho Chehab /* read bytes */ 3130c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 3140c0d06caSMauro Carvalho Chehab rc = em2800_i2c_recv_bytes(dev, addr, 3150c0d06caSMauro Carvalho Chehab msgs[i].buf, 3160c0d06caSMauro Carvalho Chehab msgs[i].len); 3170c0d06caSMauro Carvalho Chehab else 3180c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_recv_bytes(dev, addr, 3190c0d06caSMauro Carvalho Chehab msgs[i].buf, 3200c0d06caSMauro Carvalho Chehab msgs[i].len); 3210c0d06caSMauro Carvalho Chehab if (i2c_debug >= 2) { 3220c0d06caSMauro Carvalho Chehab for (byte = 0; byte < msgs[i].len; byte++) 3230c0d06caSMauro Carvalho Chehab printk(" %02x", msgs[i].buf[byte]); 3240c0d06caSMauro Carvalho Chehab } 3250c0d06caSMauro Carvalho Chehab } else { 3260c0d06caSMauro Carvalho Chehab /* write bytes */ 3270c0d06caSMauro Carvalho Chehab if (i2c_debug >= 2) { 3280c0d06caSMauro Carvalho Chehab for (byte = 0; byte < msgs[i].len; byte++) 3290c0d06caSMauro Carvalho Chehab printk(" %02x", msgs[i].buf[byte]); 3300c0d06caSMauro Carvalho Chehab } 3310c0d06caSMauro Carvalho Chehab if (dev->board.is_em2800) 3320c0d06caSMauro Carvalho Chehab rc = em2800_i2c_send_bytes(dev, addr, 3330c0d06caSMauro Carvalho Chehab msgs[i].buf, 3340c0d06caSMauro Carvalho Chehab msgs[i].len); 3350c0d06caSMauro Carvalho Chehab else 3360c0d06caSMauro Carvalho Chehab rc = em28xx_i2c_send_bytes(dev, addr, 3370c0d06caSMauro Carvalho Chehab msgs[i].buf, 3380c0d06caSMauro Carvalho Chehab msgs[i].len, 3390c0d06caSMauro Carvalho Chehab i == num - 1); 3400c0d06caSMauro Carvalho Chehab } 34145f04e82SFrank Schaefer if (rc < 0) { 34245f04e82SFrank Schaefer if (i2c_debug >= 2) 34345f04e82SFrank Schaefer printk(" ERROR: %i\n", rc); 34445f04e82SFrank Schaefer return rc; 34545f04e82SFrank Schaefer } 3460c0d06caSMauro Carvalho Chehab if (i2c_debug >= 2) 3470c0d06caSMauro Carvalho Chehab printk("\n"); 3480c0d06caSMauro Carvalho Chehab } 3490c0d06caSMauro Carvalho Chehab 3500c0d06caSMauro Carvalho Chehab return num; 3510c0d06caSMauro Carvalho Chehab } 3520c0d06caSMauro Carvalho Chehab 3530c0d06caSMauro Carvalho Chehab /* based on linux/sunrpc/svcauth.h and linux/hash.h 3540c0d06caSMauro Carvalho Chehab * The original hash function returns a different value, if arch is x86_64 3550c0d06caSMauro Carvalho Chehab * or i386. 3560c0d06caSMauro Carvalho Chehab */ 3570c0d06caSMauro Carvalho Chehab static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits) 3580c0d06caSMauro Carvalho Chehab { 3590c0d06caSMauro Carvalho Chehab unsigned long hash = 0; 3600c0d06caSMauro Carvalho Chehab unsigned long l = 0; 3610c0d06caSMauro Carvalho Chehab int len = 0; 3620c0d06caSMauro Carvalho Chehab unsigned char c; 3630c0d06caSMauro Carvalho Chehab do { 3640c0d06caSMauro Carvalho Chehab if (len == length) { 3650c0d06caSMauro Carvalho Chehab c = (char)len; 3660c0d06caSMauro Carvalho Chehab len = -1; 3670c0d06caSMauro Carvalho Chehab } else 3680c0d06caSMauro Carvalho Chehab c = *buf++; 3690c0d06caSMauro Carvalho Chehab l = (l << 8) | c; 3700c0d06caSMauro Carvalho Chehab len++; 3710c0d06caSMauro Carvalho Chehab if ((len & (32 / 8 - 1)) == 0) 3720c0d06caSMauro Carvalho Chehab hash = ((hash^l) * 0x9e370001UL); 3730c0d06caSMauro Carvalho Chehab } while (len); 3740c0d06caSMauro Carvalho Chehab 3750c0d06caSMauro Carvalho Chehab return (hash >> (32 - bits)) & 0xffffffffUL; 3760c0d06caSMauro Carvalho Chehab } 3770c0d06caSMauro Carvalho Chehab 3780c0d06caSMauro Carvalho Chehab static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len) 3790c0d06caSMauro Carvalho Chehab { 3800c0d06caSMauro Carvalho Chehab unsigned char buf, *p = eedata; 3810c0d06caSMauro Carvalho Chehab struct em28xx_eeprom *em_eeprom = (void *)eedata; 382*90271964SFrank Schaefer int i, err, size = len, block, block_max; 3830c0d06caSMauro Carvalho Chehab 3840c0d06caSMauro Carvalho Chehab if (dev->chip_id == CHIP_ID_EM2874 || 3850c0d06caSMauro Carvalho Chehab dev->chip_id == CHIP_ID_EM28174 || 3860c0d06caSMauro Carvalho Chehab dev->chip_id == CHIP_ID_EM2884) { 3870c0d06caSMauro Carvalho Chehab /* Empia switched to a 16-bit addressable eeprom in newer 3880c0d06caSMauro Carvalho Chehab devices. While we could certainly write a routine to read 3890c0d06caSMauro Carvalho Chehab the eeprom, there is nothing of use in there that cannot be 3900c0d06caSMauro Carvalho Chehab accessed through registers, and there is the risk that we 3910c0d06caSMauro Carvalho Chehab could corrupt the eeprom (since a 16-bit read call is 3920c0d06caSMauro Carvalho Chehab interpreted as a write call by 8-bit eeproms). 3930c0d06caSMauro Carvalho Chehab */ 3940c0d06caSMauro Carvalho Chehab return 0; 3950c0d06caSMauro Carvalho Chehab } 3960c0d06caSMauro Carvalho Chehab 3970c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = 0xa0 >> 1; 3980c0d06caSMauro Carvalho Chehab 3990c0d06caSMauro Carvalho Chehab /* Check if board has eeprom */ 4000c0d06caSMauro Carvalho Chehab err = i2c_master_recv(&dev->i2c_client, &buf, 0); 4010c0d06caSMauro Carvalho Chehab if (err < 0) { 4020c0d06caSMauro Carvalho Chehab em28xx_errdev("board has no eeprom\n"); 4030c0d06caSMauro Carvalho Chehab memset(eedata, 0, len); 4040c0d06caSMauro Carvalho Chehab return -ENODEV; 4050c0d06caSMauro Carvalho Chehab } 4060c0d06caSMauro Carvalho Chehab 4070c0d06caSMauro Carvalho Chehab buf = 0; 4080c0d06caSMauro Carvalho Chehab 4090c0d06caSMauro Carvalho Chehab err = i2c_master_send(&dev->i2c_client, &buf, 1); 4100c0d06caSMauro Carvalho Chehab if (err != 1) { 4110c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n", 4120c0d06caSMauro Carvalho Chehab dev->name, err); 4130c0d06caSMauro Carvalho Chehab return err; 4140c0d06caSMauro Carvalho Chehab } 415*90271964SFrank Schaefer 416*90271964SFrank Schaefer if (dev->board.is_em2800) 417*90271964SFrank Schaefer block_max = 4; 418*90271964SFrank Schaefer else 419*90271964SFrank Schaefer block_max = 64; 420*90271964SFrank Schaefer 4210c0d06caSMauro Carvalho Chehab while (size > 0) { 422*90271964SFrank Schaefer if (size > block_max) 423*90271964SFrank Schaefer block = block_max; 4240c0d06caSMauro Carvalho Chehab else 4250c0d06caSMauro Carvalho Chehab block = size; 4260c0d06caSMauro Carvalho Chehab 4270c0d06caSMauro Carvalho Chehab if (block != 4280c0d06caSMauro Carvalho Chehab (err = i2c_master_recv(&dev->i2c_client, p, block))) { 4290c0d06caSMauro Carvalho Chehab printk(KERN_WARNING 4300c0d06caSMauro Carvalho Chehab "%s: i2c eeprom read error (err=%d)\n", 4310c0d06caSMauro Carvalho Chehab dev->name, err); 4320c0d06caSMauro Carvalho Chehab return err; 4330c0d06caSMauro Carvalho Chehab } 4340c0d06caSMauro Carvalho Chehab size -= block; 4350c0d06caSMauro Carvalho Chehab p += block; 4360c0d06caSMauro Carvalho Chehab } 4370c0d06caSMauro Carvalho Chehab for (i = 0; i < len; i++) { 4380c0d06caSMauro Carvalho Chehab if (0 == (i % 16)) 4390c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s: i2c eeprom %02x:", dev->name, i); 4400c0d06caSMauro Carvalho Chehab printk(" %02x", eedata[i]); 4410c0d06caSMauro Carvalho Chehab if (15 == (i % 16)) 4420c0d06caSMauro Carvalho Chehab printk("\n"); 4430c0d06caSMauro Carvalho Chehab } 4440c0d06caSMauro Carvalho Chehab 4450c0d06caSMauro Carvalho Chehab if (em_eeprom->id == 0x9567eb1a) 4460c0d06caSMauro Carvalho Chehab dev->hash = em28xx_hash_mem(eedata, len, 32); 4470c0d06caSMauro Carvalho Chehab 4480c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s: EEPROM ID= 0x%08x, EEPROM hash = 0x%08lx\n", 4490c0d06caSMauro Carvalho Chehab dev->name, em_eeprom->id, dev->hash); 4500c0d06caSMauro Carvalho Chehab 4510c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s: EEPROM info:\n", dev->name); 4520c0d06caSMauro Carvalho Chehab 4530c0d06caSMauro Carvalho Chehab switch (em_eeprom->chip_conf >> 4 & 0x3) { 4540c0d06caSMauro Carvalho Chehab case 0: 4550c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tNo audio on board.\n", dev->name); 4560c0d06caSMauro Carvalho Chehab break; 4570c0d06caSMauro Carvalho Chehab case 1: 4580c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tAC97 audio (5 sample rates)\n", 4590c0d06caSMauro Carvalho Chehab dev->name); 4600c0d06caSMauro Carvalho Chehab break; 4610c0d06caSMauro Carvalho Chehab case 2: 4620c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tI2S audio, sample rate=32k\n", 4630c0d06caSMauro Carvalho Chehab dev->name); 4640c0d06caSMauro Carvalho Chehab break; 4650c0d06caSMauro Carvalho Chehab case 3: 4660c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tI2S audio, 3 sample rates\n", 4670c0d06caSMauro Carvalho Chehab dev->name); 4680c0d06caSMauro Carvalho Chehab break; 4690c0d06caSMauro Carvalho Chehab } 4700c0d06caSMauro Carvalho Chehab 4710c0d06caSMauro Carvalho Chehab if (em_eeprom->chip_conf & 1 << 3) 4720c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tUSB Remote wakeup capable\n", dev->name); 4730c0d06caSMauro Carvalho Chehab 4740c0d06caSMauro Carvalho Chehab if (em_eeprom->chip_conf & 1 << 2) 4750c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tUSB Self power capable\n", dev->name); 4760c0d06caSMauro Carvalho Chehab 4770c0d06caSMauro Carvalho Chehab switch (em_eeprom->chip_conf & 0x3) { 4780c0d06caSMauro Carvalho Chehab case 0: 4790c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\t500mA max power\n", dev->name); 4800c0d06caSMauro Carvalho Chehab break; 4810c0d06caSMauro Carvalho Chehab case 1: 4820c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\t400mA max power\n", dev->name); 4830c0d06caSMauro Carvalho Chehab break; 4840c0d06caSMauro Carvalho Chehab case 2: 4850c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\t300mA max power\n", dev->name); 4860c0d06caSMauro Carvalho Chehab break; 4870c0d06caSMauro Carvalho Chehab case 3: 4880c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\t200mA max power\n", dev->name); 4890c0d06caSMauro Carvalho Chehab break; 4900c0d06caSMauro Carvalho Chehab } 4910c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s:\tTable at 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n", 4920c0d06caSMauro Carvalho Chehab dev->name, 4930c0d06caSMauro Carvalho Chehab em_eeprom->string_idx_table, 4940c0d06caSMauro Carvalho Chehab em_eeprom->string1, 4950c0d06caSMauro Carvalho Chehab em_eeprom->string2, 4960c0d06caSMauro Carvalho Chehab em_eeprom->string3); 4970c0d06caSMauro Carvalho Chehab 4980c0d06caSMauro Carvalho Chehab return 0; 4990c0d06caSMauro Carvalho Chehab } 5000c0d06caSMauro Carvalho Chehab 5010c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 5020c0d06caSMauro Carvalho Chehab 5030c0d06caSMauro Carvalho Chehab /* 5040c0d06caSMauro Carvalho Chehab * functionality() 5050c0d06caSMauro Carvalho Chehab */ 5060c0d06caSMauro Carvalho Chehab static u32 functionality(struct i2c_adapter *adap) 5070c0d06caSMauro Carvalho Chehab { 508eaf33c40SFrank Schaefer struct em28xx *dev = adap->algo_data; 509eaf33c40SFrank Schaefer u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 510eaf33c40SFrank Schaefer if (dev->board.is_em2800) 511eaf33c40SFrank Schaefer func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA; 512eaf33c40SFrank Schaefer return func_flags; 5130c0d06caSMauro Carvalho Chehab } 5140c0d06caSMauro Carvalho Chehab 5150c0d06caSMauro Carvalho Chehab static struct i2c_algorithm em28xx_algo = { 5160c0d06caSMauro Carvalho Chehab .master_xfer = em28xx_i2c_xfer, 5170c0d06caSMauro Carvalho Chehab .functionality = functionality, 5180c0d06caSMauro Carvalho Chehab }; 5190c0d06caSMauro Carvalho Chehab 5200c0d06caSMauro Carvalho Chehab static struct i2c_adapter em28xx_adap_template = { 5210c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE, 5220c0d06caSMauro Carvalho Chehab .name = "em28xx", 5230c0d06caSMauro Carvalho Chehab .algo = &em28xx_algo, 5240c0d06caSMauro Carvalho Chehab }; 5250c0d06caSMauro Carvalho Chehab 5260c0d06caSMauro Carvalho Chehab static struct i2c_client em28xx_client_template = { 5270c0d06caSMauro Carvalho Chehab .name = "em28xx internal", 5280c0d06caSMauro Carvalho Chehab }; 5290c0d06caSMauro Carvalho Chehab 5300c0d06caSMauro Carvalho Chehab /* ----------------------------------------------------------- */ 5310c0d06caSMauro Carvalho Chehab 5320c0d06caSMauro Carvalho Chehab /* 5330c0d06caSMauro Carvalho Chehab * i2c_devs 5340c0d06caSMauro Carvalho Chehab * incomplete list of known devices 5350c0d06caSMauro Carvalho Chehab */ 5360c0d06caSMauro Carvalho Chehab static char *i2c_devs[128] = { 5370c0d06caSMauro Carvalho Chehab [0x4a >> 1] = "saa7113h", 5380c0d06caSMauro Carvalho Chehab [0x52 >> 1] = "drxk", 5390c0d06caSMauro Carvalho Chehab [0x60 >> 1] = "remote IR sensor", 5400c0d06caSMauro Carvalho Chehab [0x8e >> 1] = "remote IR sensor", 5410c0d06caSMauro Carvalho Chehab [0x86 >> 1] = "tda9887", 5420c0d06caSMauro Carvalho Chehab [0x80 >> 1] = "msp34xx", 5430c0d06caSMauro Carvalho Chehab [0x88 >> 1] = "msp34xx", 5440c0d06caSMauro Carvalho Chehab [0xa0 >> 1] = "eeprom", 5450c0d06caSMauro Carvalho Chehab [0xb0 >> 1] = "tda9874", 5460c0d06caSMauro Carvalho Chehab [0xb8 >> 1] = "tvp5150a", 5470c0d06caSMauro Carvalho Chehab [0xba >> 1] = "webcam sensor or tvp5150a", 5480c0d06caSMauro Carvalho Chehab [0xc0 >> 1] = "tuner (analog)", 5490c0d06caSMauro Carvalho Chehab [0xc2 >> 1] = "tuner (analog)", 5500c0d06caSMauro Carvalho Chehab [0xc4 >> 1] = "tuner (analog)", 5510c0d06caSMauro Carvalho Chehab [0xc6 >> 1] = "tuner (analog)", 5520c0d06caSMauro Carvalho Chehab }; 5530c0d06caSMauro Carvalho Chehab 5540c0d06caSMauro Carvalho Chehab /* 5550c0d06caSMauro Carvalho Chehab * do_i2c_scan() 5560c0d06caSMauro Carvalho Chehab * check i2c address range for devices 5570c0d06caSMauro Carvalho Chehab */ 5580c0d06caSMauro Carvalho Chehab void em28xx_do_i2c_scan(struct em28xx *dev) 5590c0d06caSMauro Carvalho Chehab { 5600c0d06caSMauro Carvalho Chehab u8 i2c_devicelist[128]; 5610c0d06caSMauro Carvalho Chehab unsigned char buf; 5620c0d06caSMauro Carvalho Chehab int i, rc; 5630c0d06caSMauro Carvalho Chehab 5640c0d06caSMauro Carvalho Chehab memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist)); 5650c0d06caSMauro Carvalho Chehab 5660c0d06caSMauro Carvalho Chehab for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { 5670c0d06caSMauro Carvalho Chehab dev->i2c_client.addr = i; 5680c0d06caSMauro Carvalho Chehab rc = i2c_master_recv(&dev->i2c_client, &buf, 0); 5690c0d06caSMauro Carvalho Chehab if (rc < 0) 5700c0d06caSMauro Carvalho Chehab continue; 5710c0d06caSMauro Carvalho Chehab i2c_devicelist[i] = i; 5720c0d06caSMauro Carvalho Chehab printk(KERN_INFO "%s: found i2c device @ 0x%x [%s]\n", 5730c0d06caSMauro Carvalho Chehab dev->name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 5740c0d06caSMauro Carvalho Chehab } 5750c0d06caSMauro Carvalho Chehab 5760c0d06caSMauro Carvalho Chehab dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, 5770c0d06caSMauro Carvalho Chehab ARRAY_SIZE(i2c_devicelist), 32); 5780c0d06caSMauro Carvalho Chehab } 5790c0d06caSMauro Carvalho Chehab 5800c0d06caSMauro Carvalho Chehab /* 5810c0d06caSMauro Carvalho Chehab * em28xx_i2c_register() 5820c0d06caSMauro Carvalho Chehab * register i2c bus 5830c0d06caSMauro Carvalho Chehab */ 5840c0d06caSMauro Carvalho Chehab int em28xx_i2c_register(struct em28xx *dev) 5850c0d06caSMauro Carvalho Chehab { 5860c0d06caSMauro Carvalho Chehab int retval; 5870c0d06caSMauro Carvalho Chehab 5880c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg); 5890c0d06caSMauro Carvalho Chehab BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req); 5900c0d06caSMauro Carvalho Chehab dev->i2c_adap = em28xx_adap_template; 5910c0d06caSMauro Carvalho Chehab dev->i2c_adap.dev.parent = &dev->udev->dev; 5920c0d06caSMauro Carvalho Chehab strcpy(dev->i2c_adap.name, dev->name); 5930c0d06caSMauro Carvalho Chehab dev->i2c_adap.algo_data = dev; 5940c0d06caSMauro Carvalho Chehab i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); 5950c0d06caSMauro Carvalho Chehab 5960c0d06caSMauro Carvalho Chehab retval = i2c_add_adapter(&dev->i2c_adap); 5970c0d06caSMauro Carvalho Chehab if (retval < 0) { 5980c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n", 5990c0d06caSMauro Carvalho Chehab __func__, retval); 6000c0d06caSMauro Carvalho Chehab return retval; 6010c0d06caSMauro Carvalho Chehab } 6020c0d06caSMauro Carvalho Chehab 6030c0d06caSMauro Carvalho Chehab dev->i2c_client = em28xx_client_template; 6040c0d06caSMauro Carvalho Chehab dev->i2c_client.adapter = &dev->i2c_adap; 6050c0d06caSMauro Carvalho Chehab 6060c0d06caSMauro Carvalho Chehab retval = em28xx_i2c_eeprom(dev, dev->eedata, sizeof(dev->eedata)); 6070c0d06caSMauro Carvalho Chehab if ((retval < 0) && (retval != -ENODEV)) { 6080c0d06caSMauro Carvalho Chehab em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n", 6090c0d06caSMauro Carvalho Chehab __func__, retval); 6100c0d06caSMauro Carvalho Chehab 6110c0d06caSMauro Carvalho Chehab return retval; 6120c0d06caSMauro Carvalho Chehab } 6130c0d06caSMauro Carvalho Chehab 6140c0d06caSMauro Carvalho Chehab if (i2c_scan) 6150c0d06caSMauro Carvalho Chehab em28xx_do_i2c_scan(dev); 6160c0d06caSMauro Carvalho Chehab 6170c0d06caSMauro Carvalho Chehab return 0; 6180c0d06caSMauro Carvalho Chehab } 6190c0d06caSMauro Carvalho Chehab 6200c0d06caSMauro Carvalho Chehab /* 6210c0d06caSMauro Carvalho Chehab * em28xx_i2c_unregister() 6220c0d06caSMauro Carvalho Chehab * unregister i2c_bus 6230c0d06caSMauro Carvalho Chehab */ 6240c0d06caSMauro Carvalho Chehab int em28xx_i2c_unregister(struct em28xx *dev) 6250c0d06caSMauro Carvalho Chehab { 6260c0d06caSMauro Carvalho Chehab i2c_del_adapter(&dev->i2c_adap); 6270c0d06caSMauro Carvalho Chehab return 0; 6280c0d06caSMauro Carvalho Chehab } 629