akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 2 | em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 3 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 4 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
| 5 | Markus Rechberger <mrechberger@gmail.com> |
Mauro Carvalho Chehab | 2e7c6dc | 2006-04-03 07:53:40 -0300 | [diff] [blame] | 6 | Mauro Carvalho Chehab <mchehab@infradead.org> |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 7 | Sascha Sommer <saschasommer@freenet.de> |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 8 | Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/usb.h> |
| 28 | #include <linux/i2c.h> |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 29 | #include <linux/jiffies.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 30 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 31 | #include "em28xx.h" |
Mauro Carvalho Chehab | 6c362c8 | 2007-10-29 23:36:12 -0300 | [diff] [blame] | 32 | #include "tuner-xc2028.h" |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 33 | #include <media/v4l2-common.h> |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 34 | #include <media/tuner.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 35 | |
| 36 | /* ----------------------------------------------------------- */ |
| 37 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 38 | static unsigned int i2c_scan; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 39 | module_param(i2c_scan, int, 0444); |
| 40 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
| 41 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 42 | static unsigned int i2c_debug; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 43 | module_param(i2c_debug, int, 0644); |
| 44 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
| 45 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 46 | /* |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 47 | * em2800_i2c_send_bytes() |
| 48 | * send up to 4 bytes to the em2800 i2c device |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 49 | */ |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 50 | static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 51 | { |
Mauro Carvalho Chehab | d1b7213 | 2014-01-05 09:45:50 -0300 | [diff] [blame] | 52 | unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 53 | int ret; |
Frank Schaefer | a6bad04 | 2012-12-16 14:23:27 -0300 | [diff] [blame] | 54 | u8 b2[6]; |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 55 | |
| 56 | if (len < 1 || len > 4) |
| 57 | return -EOPNOTSUPP; |
| 58 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 59 | BUG_ON(len < 1 || len > 4); |
| 60 | b2[5] = 0x80 + len - 1; |
| 61 | b2[4] = addr; |
| 62 | b2[3] = buf[0]; |
| 63 | if (len > 1) |
| 64 | b2[2] = buf[1]; |
| 65 | if (len > 2) |
| 66 | b2[1] = buf[2]; |
| 67 | if (len > 3) |
| 68 | b2[0] = buf[3]; |
| 69 | |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 70 | /* trigger write */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 71 | ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 72 | if (ret != 2 + len) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 73 | em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n", |
| 74 | addr, ret); |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 75 | return (ret < 0) ? ret : -EIO; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 76 | } |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 77 | /* wait for completion */ |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 78 | while (time_is_after_jiffies(timeout)) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 79 | ret = dev->em28xx_read_reg(dev, 0x05); |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 80 | if (ret == 0x80 + len - 1) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 81 | return len; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 82 | if (ret == 0x94 + len - 1) { |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 83 | return -ENXIO; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 84 | } |
| 85 | if (ret < 0) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 86 | em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", |
| 87 | ret); |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 88 | return ret; |
| 89 | } |
Markus Rechberger | e8e41da | 2006-02-07 06:49:11 -0200 | [diff] [blame] | 90 | msleep(5); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 91 | } |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 92 | em28xx_warn("write to i2c device at 0x%x timed out\n", addr); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 93 | return -ETIMEDOUT; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 97 | * em2800_i2c_recv_bytes() |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 98 | * read up to 4 bytes from the em2800 i2c device |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 99 | */ |
Frank Schaefer | a6bad04 | 2012-12-16 14:23:27 -0300 | [diff] [blame] | 100 | static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 101 | { |
Mauro Carvalho Chehab | d1b7213 | 2014-01-05 09:45:50 -0300 | [diff] [blame] | 102 | unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT); |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 103 | u8 buf2[4]; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 104 | int ret; |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 105 | int i; |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 106 | |
| 107 | if (len < 1 || len > 4) |
| 108 | return -EOPNOTSUPP; |
| 109 | |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 110 | /* trigger read */ |
| 111 | buf2[1] = 0x84 + len - 1; |
| 112 | buf2[0] = addr; |
| 113 | ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2); |
| 114 | if (ret != 2) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 115 | em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n", |
| 116 | addr, ret); |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 117 | return (ret < 0) ? ret : -EIO; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 118 | } |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 119 | |
| 120 | /* wait for completion */ |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 121 | while (time_is_after_jiffies(timeout)) { |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 122 | ret = dev->em28xx_read_reg(dev, 0x05); |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 123 | if (ret == 0x84 + len - 1) |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 124 | break; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 125 | if (ret == 0x94 + len - 1) { |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 126 | return -ENXIO; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 127 | } |
| 128 | if (ret < 0) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 129 | em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", |
| 130 | ret); |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 131 | return ret; |
| 132 | } |
| 133 | msleep(5); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 134 | } |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 135 | if (ret != 0x84 + len - 1) |
| 136 | em28xx_warn("read from i2c device at 0x%x timed out\n", addr); |
| 137 | |
| 138 | /* get the received message */ |
| 139 | ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len); |
| 140 | if (ret != len) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 141 | em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n", |
| 142 | addr, ret); |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 143 | return (ret < 0) ? ret : -EIO; |
| 144 | } |
| 145 | for (i = 0; i < len; i++) |
| 146 | buf[i] = buf2[len - 1 - i]; |
| 147 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* |
Frank Schaefer | 2fcc82d | 2013-01-03 14:27:03 -0300 | [diff] [blame] | 152 | * em2800_i2c_check_for_device() |
| 153 | * check if there is an i2c device at the supplied address |
| 154 | */ |
| 155 | static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr) |
| 156 | { |
| 157 | u8 buf; |
| 158 | int ret; |
| 159 | |
| 160 | ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1); |
| 161 | if (ret == 1) |
| 162 | return 0; |
| 163 | return (ret < 0) ? ret : -EIO; |
| 164 | } |
| 165 | |
| 166 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 167 | * em28xx_i2c_send_bytes() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 168 | */ |
Frank Schaefer | a6bad04 | 2012-12-16 14:23:27 -0300 | [diff] [blame] | 169 | static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf, |
| 170 | u16 len, int stop) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 171 | { |
Mauro Carvalho Chehab | d1b7213 | 2014-01-05 09:45:50 -0300 | [diff] [blame] | 172 | unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT); |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 173 | int ret; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 174 | |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 175 | if (len < 1 || len > 64) |
| 176 | return -EOPNOTSUPP; |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 177 | /* |
| 178 | * NOTE: limited by the USB ctrl message constraints |
| 179 | * Zero length reads always succeed, even if no device is connected |
| 180 | */ |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 181 | |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 182 | /* Write to i2c device */ |
| 183 | ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len); |
| 184 | if (ret != len) { |
| 185 | if (ret < 0) { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 186 | em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n", |
| 187 | addr, ret); |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 188 | return ret; |
| 189 | } else { |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 190 | em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n", |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 191 | len, addr, ret); |
| 192 | return -EIO; |
| 193 | } |
| 194 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 195 | |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 196 | /* wait for completion */ |
| 197 | while (time_is_after_jiffies(timeout)) { |
Mauro Carvalho Chehab | bbc70e6 | 2011-07-09 19:36:11 -0300 | [diff] [blame] | 198 | ret = dev->em28xx_read_reg(dev, 0x05); |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 199 | if (ret == 0) /* success */ |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 200 | return len; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 201 | if (ret == 0x10) { |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 202 | return -ENXIO; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 203 | } |
| 204 | if (ret < 0) { |
| 205 | em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 206 | ret); |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 207 | return ret; |
| 208 | } |
Mauro Carvalho Chehab | bbc70e6 | 2011-07-09 19:36:11 -0300 | [diff] [blame] | 209 | msleep(5); |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 210 | /* |
| 211 | * NOTE: do we really have to wait for success ? |
| 212 | * Never seen anything else than 0x00 or 0x10 |
| 213 | * (even with high payload) ... |
| 214 | */ |
Mauro Carvalho Chehab | bbc70e6 | 2011-07-09 19:36:11 -0300 | [diff] [blame] | 215 | } |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 216 | em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n", addr, ret); |
| 217 | return -ETIMEDOUT; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 221 | * em28xx_i2c_recv_bytes() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 222 | * read a byte from the i2c device |
| 223 | */ |
Frank Schaefer | a6bad04 | 2012-12-16 14:23:27 -0300 | [diff] [blame] | 224 | static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 225 | { |
| 226 | int ret; |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 227 | |
| 228 | if (len < 1 || len > 64) |
| 229 | return -EOPNOTSUPP; |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 230 | /* |
| 231 | * NOTE: limited by the USB ctrl message constraints |
| 232 | * Zero length reads always succeed, even if no device is connected |
| 233 | */ |
Frank Schaefer | f5ae371 | 2013-01-03 14:27:02 -0300 | [diff] [blame] | 234 | |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 235 | /* Read data from i2c device */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 236 | ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len); |
Frank Schaefer | 7f6301d | 2013-03-10 07:25:25 -0300 | [diff] [blame] | 237 | if (ret < 0) { |
| 238 | em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n", |
| 239 | addr, ret); |
| 240 | return ret; |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 241 | } |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 242 | /* |
| 243 | * NOTE: some devices with two i2c busses have the bad habit to return 0 |
Frank Schaefer | 7f6301d | 2013-03-10 07:25:25 -0300 | [diff] [blame] | 244 | * bytes if we are on bus B AND there was no write attempt to the |
| 245 | * specified slave address before AND no device is present at the |
| 246 | * requested slave address. |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 247 | * Anyway, the next check will fail with -ENXIO in this case, so avoid |
Frank Schaefer | 7f6301d | 2013-03-10 07:25:25 -0300 | [diff] [blame] | 248 | * spamming the system log on device probing and do nothing here. |
| 249 | */ |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 250 | |
| 251 | /* Check success of the i2c operation */ |
| 252 | ret = dev->em28xx_read_reg(dev, 0x05); |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 253 | if (ret == 0) /* success */ |
| 254 | return len; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 255 | if (ret < 0) { |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 256 | em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 257 | ret); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 258 | return ret; |
| 259 | } |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 260 | if (ret == 0x10) |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 261 | return -ENXIO; |
Mauro Carvalho Chehab | 4b83626 | 2014-01-04 05:42:11 -0300 | [diff] [blame] | 262 | |
| 263 | em28xx_warn("unknown i2c error (status=%i)\n", ret); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 264 | return -ETIMEDOUT; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 268 | * em28xx_i2c_check_for_device() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 269 | * check if there is a i2c_device at the supplied address |
| 270 | */ |
Frank Schaefer | a6bad04 | 2012-12-16 14:23:27 -0300 | [diff] [blame] | 271 | static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 272 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 273 | int ret; |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 274 | u8 buf; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 275 | |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 276 | ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1); |
| 277 | if (ret == 1) |
| 278 | return 0; |
| 279 | return (ret < 0) ? ret : -EIO; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 283 | * em25xx_bus_B_send_bytes |
| 284 | * write bytes to the i2c device |
| 285 | */ |
| 286 | static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf, |
| 287 | u16 len) |
| 288 | { |
| 289 | int ret; |
| 290 | |
| 291 | if (len < 1 || len > 64) |
| 292 | return -EOPNOTSUPP; |
| 293 | /* |
| 294 | * NOTE: limited by the USB ctrl message constraints |
| 295 | * Zero length reads always succeed, even if no device is connected |
| 296 | */ |
| 297 | |
| 298 | /* Set register and write value */ |
| 299 | ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len); |
| 300 | if (ret != len) { |
| 301 | if (ret < 0) { |
| 302 | em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n", |
| 303 | addr, ret); |
| 304 | return ret; |
| 305 | } else { |
| 306 | em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n", |
| 307 | len, addr, ret); |
| 308 | return -EIO; |
| 309 | } |
| 310 | } |
| 311 | /* Check success */ |
| 312 | ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000); |
| 313 | /* |
| 314 | * NOTE: the only error we've seen so far is |
| 315 | * 0x01 when the slave device is not present |
| 316 | */ |
| 317 | if (!ret) |
| 318 | return len; |
| 319 | else if (ret > 0) |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 320 | return -ENXIO; |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 321 | |
| 322 | return ret; |
| 323 | /* |
| 324 | * NOTE: With chip types (other chip IDs) which actually don't support |
| 325 | * this operation, it seems to succeed ALWAYS ! (even if there is no |
| 326 | * slave device or even no second i2c bus provided) |
| 327 | */ |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * em25xx_bus_B_recv_bytes |
| 332 | * read bytes from the i2c device |
| 333 | */ |
| 334 | static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, |
| 335 | u16 len) |
| 336 | { |
| 337 | int ret; |
| 338 | |
| 339 | if (len < 1 || len > 64) |
| 340 | return -EOPNOTSUPP; |
| 341 | /* |
| 342 | * NOTE: limited by the USB ctrl message constraints |
| 343 | * Zero length reads always succeed, even if no device is connected |
| 344 | */ |
| 345 | |
| 346 | /* Read value */ |
| 347 | ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len); |
| 348 | if (ret < 0) { |
| 349 | em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n", |
| 350 | addr, ret); |
| 351 | return ret; |
| 352 | } |
| 353 | /* |
| 354 | * NOTE: some devices with two i2c busses have the bad habit to return 0 |
| 355 | * bytes if we are on bus B AND there was no write attempt to the |
| 356 | * specified slave address before AND no device is present at the |
| 357 | * requested slave address. |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 358 | * Anyway, the next check will fail with -ENXIO in this case, so avoid |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 359 | * spamming the system log on device probing and do nothing here. |
| 360 | */ |
| 361 | |
| 362 | /* Check success */ |
| 363 | ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000); |
| 364 | /* |
| 365 | * NOTE: the only error we've seen so far is |
| 366 | * 0x01 when the slave device is not present |
| 367 | */ |
| 368 | if (!ret) |
| 369 | return len; |
| 370 | else if (ret > 0) |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 371 | return -ENXIO; |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 372 | |
| 373 | return ret; |
| 374 | /* |
| 375 | * NOTE: With chip types (other chip IDs) which actually don't support |
| 376 | * this operation, it seems to succeed ALWAYS ! (even if there is no |
| 377 | * slave device or even no second i2c bus provided) |
| 378 | */ |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * em25xx_bus_B_check_for_device() |
| 383 | * check if there is a i2c device at the supplied address |
| 384 | */ |
| 385 | static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr) |
| 386 | { |
| 387 | u8 buf; |
| 388 | int ret; |
| 389 | |
| 390 | ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1); |
| 391 | if (ret < 0) |
| 392 | return ret; |
| 393 | |
| 394 | return 0; |
| 395 | /* |
| 396 | * NOTE: With chips which do not support this operation, |
| 397 | * it seems to succeed ALWAYS ! (even if no device connected) |
| 398 | */ |
| 399 | } |
| 400 | |
| 401 | static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr) |
| 402 | { |
| 403 | struct em28xx *dev = i2c_bus->dev; |
| 404 | int rc = -EOPNOTSUPP; |
| 405 | |
| 406 | if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) |
| 407 | rc = em28xx_i2c_check_for_device(dev, addr); |
| 408 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) |
| 409 | rc = em2800_i2c_check_for_device(dev, addr); |
| 410 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) |
| 411 | rc = em25xx_bus_B_check_for_device(dev, addr); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 412 | if (rc == -ENXIO) { |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 413 | if (i2c_debug) |
| 414 | printk(" no device\n"); |
| 415 | } |
| 416 | return rc; |
| 417 | } |
| 418 | |
| 419 | static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus, |
| 420 | struct i2c_msg msg) |
| 421 | { |
| 422 | struct em28xx *dev = i2c_bus->dev; |
| 423 | u16 addr = msg.addr << 1; |
| 424 | int byte, rc = -EOPNOTSUPP; |
| 425 | |
| 426 | if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) |
| 427 | rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len); |
| 428 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) |
| 429 | rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len); |
| 430 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) |
| 431 | rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len); |
| 432 | if (i2c_debug) { |
| 433 | for (byte = 0; byte < msg.len; byte++) |
| 434 | printk(" %02x", msg.buf[byte]); |
| 435 | } |
| 436 | return rc; |
| 437 | } |
| 438 | |
| 439 | static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus, |
| 440 | struct i2c_msg msg, int stop) |
| 441 | { |
| 442 | struct em28xx *dev = i2c_bus->dev; |
| 443 | u16 addr = msg.addr << 1; |
| 444 | int byte, rc = -EOPNOTSUPP; |
| 445 | |
| 446 | if (i2c_debug) { |
| 447 | for (byte = 0; byte < msg.len; byte++) |
| 448 | printk(" %02x", msg.buf[byte]); |
| 449 | } |
| 450 | if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) |
| 451 | rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop); |
| 452 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) |
| 453 | rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len); |
| 454 | else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) |
| 455 | rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len); |
| 456 | return rc; |
| 457 | } |
| 458 | |
| 459 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 460 | * em28xx_i2c_xfer() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 461 | * the main i2c transfer function |
| 462 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 463 | static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 464 | struct i2c_msg msgs[], int num) |
| 465 | { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 466 | struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data; |
| 467 | struct em28xx *dev = i2c_bus->dev; |
| 468 | unsigned bus = i2c_bus->bus; |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 469 | int addr, rc, i; |
Mauro Carvalho Chehab | 3190fbe | 2013-03-21 06:03:27 -0300 | [diff] [blame] | 470 | u8 reg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 471 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 472 | rc = rt_mutex_trylock(&dev->i2c_bus_lock); |
| 473 | if (rc < 0) |
| 474 | return rc; |
| 475 | |
| 476 | /* Switch I2C bus if needed */ |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 477 | if (bus != dev->cur_i2c_bus && |
| 478 | i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 479 | if (bus == 1) |
Mauro Carvalho Chehab | 3190fbe | 2013-03-21 06:03:27 -0300 | [diff] [blame] | 480 | reg = EM2874_I2C_SECONDARY_BUS_SELECT; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 481 | else |
Mauro Carvalho Chehab | 3190fbe | 2013-03-21 06:03:27 -0300 | [diff] [blame] | 482 | reg = 0; |
| 483 | em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg, |
| 484 | EM2874_I2C_SECONDARY_BUS_SELECT); |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 485 | dev->cur_i2c_bus = bus; |
| 486 | } |
| 487 | |
| 488 | if (num <= 0) { |
| 489 | rt_mutex_unlock(&dev->i2c_bus_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 490 | return 0; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 491 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 492 | for (i = 0; i < num; i++) { |
| 493 | addr = msgs[i].addr << 1; |
Frank Schaefer | d90f067 | 2013-03-03 15:37:36 -0300 | [diff] [blame] | 494 | if (i2c_debug) |
Frank Schaefer | d7a80ea | 2013-03-03 15:37:35 -0300 | [diff] [blame] | 495 | printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:", |
| 496 | dev->name, __func__ , |
| 497 | (msgs[i].flags & I2C_M_RD) ? "read" : "write", |
| 498 | i == num - 1 ? "stop" : "nonstop", |
| 499 | addr, msgs[i].len); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 500 | if (!msgs[i].len) { |
| 501 | /* |
| 502 | * no len: check only for device presence |
| 503 | * This code is only called during device probe. |
| 504 | */ |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 505 | rc = i2c_check_for_device(i2c_bus, addr); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 506 | if (rc == -ENXIO) { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 507 | rt_mutex_unlock(&dev->i2c_bus_lock); |
Mauro Carvalho Chehab | e63b009 | 2014-01-04 05:42:11 -0300 | [diff] [blame^] | 508 | return -ENODEV; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 509 | } |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 510 | } else if (msgs[i].flags & I2C_M_RD) { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 511 | /* read bytes */ |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 512 | rc = i2c_recv_bytes(i2c_bus, msgs[i]); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 513 | } else { |
| 514 | /* write bytes */ |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 515 | rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 516 | } |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 517 | if (rc < 0) { |
Frank Schaefer | d90f067 | 2013-03-03 15:37:36 -0300 | [diff] [blame] | 518 | if (i2c_debug) |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 519 | printk(" ERROR: %i\n", rc); |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 520 | rt_mutex_unlock(&dev->i2c_bus_lock); |
Frank Schaefer | 45f04e8 | 2013-01-03 14:27:05 -0300 | [diff] [blame] | 521 | return rc; |
| 522 | } |
Frank Schaefer | d90f067 | 2013-03-03 15:37:36 -0300 | [diff] [blame] | 523 | if (i2c_debug) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 524 | printk("\n"); |
| 525 | } |
| 526 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 527 | rt_mutex_unlock(&dev->i2c_bus_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 528 | return num; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 531 | /* |
| 532 | * based on linux/sunrpc/svcauth.h and linux/hash.h |
Mauro Carvalho Chehab | 03910cc | 2007-11-03 21:20:59 -0300 | [diff] [blame] | 533 | * The original hash function returns a different value, if arch is x86_64 |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 534 | * or i386. |
Mauro Carvalho Chehab | 03910cc | 2007-11-03 21:20:59 -0300 | [diff] [blame] | 535 | */ |
| 536 | static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits) |
| 537 | { |
| 538 | unsigned long hash = 0; |
| 539 | unsigned long l = 0; |
| 540 | int len = 0; |
| 541 | unsigned char c; |
| 542 | do { |
| 543 | if (len == length) { |
| 544 | c = (char)len; |
| 545 | len = -1; |
| 546 | } else |
| 547 | c = *buf++; |
| 548 | l = (l << 8) | c; |
| 549 | len++; |
| 550 | if ((len & (32 / 8 - 1)) == 0) |
| 551 | hash = ((hash^l) * 0x9e370001UL); |
| 552 | } while (len); |
| 553 | |
| 554 | return (hash >> (32 - bits)) & 0xffffffffUL; |
| 555 | } |
| 556 | |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 557 | /* |
| 558 | * Helper function to read data blocks from i2c clients with 8 or 16 bit |
| 559 | * address width, 8 bit register width and auto incrementation been activated |
| 560 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 561 | static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr, |
| 562 | bool addr_w16, u16 len, u8 *data) |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 563 | { |
| 564 | int remain = len, rsize, rsize_max, ret; |
| 565 | u8 buf[2]; |
| 566 | |
| 567 | /* Sanity check */ |
| 568 | if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1)) |
| 569 | return -EINVAL; |
| 570 | /* Select address */ |
| 571 | buf[0] = addr >> 8; |
| 572 | buf[1] = addr & 0xff; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 573 | ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16); |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 574 | if (ret < 0) |
| 575 | return ret; |
| 576 | /* Read data */ |
| 577 | if (dev->board.is_em2800) |
| 578 | rsize_max = 4; |
| 579 | else |
| 580 | rsize_max = 64; |
| 581 | while (remain > 0) { |
| 582 | if (remain > rsize_max) |
| 583 | rsize = rsize_max; |
| 584 | else |
| 585 | rsize = remain; |
| 586 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 587 | ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize); |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 588 | if (ret < 0) |
| 589 | return ret; |
| 590 | |
| 591 | remain -= rsize; |
| 592 | data += rsize; |
| 593 | } |
| 594 | |
| 595 | return len; |
| 596 | } |
| 597 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 598 | static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus, |
| 599 | u8 **eedata, u16 *eedata_len) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 600 | { |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 601 | const u16 len = 256; |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 602 | /* |
| 603 | * FIXME common length/size for bytes to read, to display, hash |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 604 | * calculation and returned device dataset. Simplifies the code a lot, |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 605 | * but we might have to deal with multiple sizes in the future ! |
| 606 | */ |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 607 | int i, err; |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 608 | struct em28xx_eeprom *dev_config; |
| 609 | u8 buf, *data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 610 | |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 611 | *eedata = NULL; |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 612 | *eedata_len = 0; |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 613 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 614 | /* EEPROM is always on i2c bus 0 on all known devices. */ |
| 615 | |
| 616 | dev->i2c_client[bus].addr = 0xa0 >> 1; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 617 | |
| 618 | /* Check if board has eeprom */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 619 | err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0); |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 620 | if (err < 0) { |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 621 | em28xx_info("board has no eeprom\n"); |
Mauro Carvalho Chehab | c41109f | 2008-11-15 23:44:14 -0300 | [diff] [blame] | 622 | return -ENODEV; |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 623 | } |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 624 | |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 625 | data = kzalloc(len, GFP_KERNEL); |
| 626 | if (data == NULL) |
| 627 | return -ENOMEM; |
| 628 | |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 629 | /* Read EEPROM content */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 630 | err = em28xx_i2c_read_block(dev, bus, 0x0000, |
| 631 | dev->eeprom_addrwidth_16bit, |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 632 | len, data); |
Frank Schaefer | d832c5b | 2013-03-03 15:37:41 -0300 | [diff] [blame] | 633 | if (err != len) { |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 634 | em28xx_errdev("failed to read eeprom (err=%d)\n", err); |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 635 | goto error; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 636 | } |
Frank Schaefer | 9027196 | 2013-01-03 14:27:06 -0300 | [diff] [blame] | 637 | |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 638 | /* Display eeprom content */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 639 | for (i = 0; i < len; i++) { |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 640 | if (0 == (i % 16)) { |
| 641 | if (dev->eeprom_addrwidth_16bit) |
| 642 | em28xx_info("i2c eeprom %04x:", i); |
| 643 | else |
| 644 | em28xx_info("i2c eeprom %02x:", i); |
| 645 | } |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 646 | printk(" %02x", data[i]); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 647 | if (15 == (i % 16)) |
| 648 | printk("\n"); |
| 649 | } |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 650 | if (dev->eeprom_addrwidth_16bit) |
| 651 | em28xx_info("i2c eeprom %04x: ... (skipped)\n", i); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 652 | |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 653 | if (dev->eeprom_addrwidth_16bit && |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 654 | data[0] == 0x26 && data[3] == 0x00) { |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 655 | /* new eeprom format; size 4-64kb */ |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 656 | u16 mc_start; |
| 657 | u16 hwconf_offset; |
| 658 | |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 659 | dev->hash = em28xx_hash_mem(data, len, 32); |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 660 | mc_start = (data[1] << 8) + 4; /* usually 0x0004 */ |
| 661 | |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 662 | em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n", |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 663 | data[0], data[1], data[2], data[3], dev->hash); |
| 664 | em28xx_info("EEPROM info:\n"); |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 665 | em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n", |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 666 | mc_start, data[2]); |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 667 | /* |
| 668 | * boot configuration (address 0x0002): |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 669 | * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz |
| 670 | * [1] always selects 12 kb RAM |
| 671 | * [2] USB device speed: 1 = force Full Speed; 0 = auto detect |
| 672 | * [4] 1 = force fast mode and no suspend for device testing |
| 673 | * [5:7] USB PHY tuning registers; determined by device |
| 674 | * characterization |
| 675 | */ |
| 676 | |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 677 | /* |
| 678 | * Read hardware config dataset offset from address |
| 679 | * (microcode start + 46) |
| 680 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 681 | err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2, |
| 682 | data); |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 683 | if (err != 2) { |
| 684 | em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n", |
| 685 | err); |
| 686 | goto error; |
| 687 | } |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 688 | |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 689 | /* Calculate hardware config dataset start address */ |
| 690 | hwconf_offset = mc_start + data[0] + (data[1] << 8); |
| 691 | |
| 692 | /* Read hardware config dataset */ |
Frank Schaefer | fa74aca | 2013-03-24 17:09:59 -0300 | [diff] [blame] | 693 | /* |
| 694 | * NOTE: the microcode copy can be multiple pages long, but |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 695 | * we assume the hardware config dataset is the same as in |
| 696 | * the old eeprom and not longer than 256 bytes. |
| 697 | * tveeprom is currently also limited to 256 bytes. |
| 698 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 699 | err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len, |
| 700 | data); |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 701 | if (err != len) { |
| 702 | em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n", |
| 703 | err); |
| 704 | goto error; |
| 705 | } |
| 706 | |
| 707 | /* Verify hardware config dataset */ |
| 708 | /* NOTE: not all devices provide this type of dataset */ |
| 709 | if (data[0] != 0x1a || data[1] != 0xeb || |
| 710 | data[2] != 0x67 || data[3] != 0x95) { |
| 711 | em28xx_info("\tno hardware configuration dataset found in eeprom\n"); |
| 712 | kfree(data); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */ |
| 717 | |
| 718 | } else if (!dev->eeprom_addrwidth_16bit && |
| 719 | data[0] == 0x1a && data[1] == 0xeb && |
| 720 | data[2] == 0x67 && data[3] == 0x95) { |
| 721 | dev->hash = em28xx_hash_mem(data, len, 32); |
Frank Schaefer | d230d5a | 2013-03-24 14:58:03 -0300 | [diff] [blame] | 722 | em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n", |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 723 | data[0], data[1], data[2], data[3], dev->hash); |
| 724 | em28xx_info("EEPROM info:\n"); |
| 725 | } else { |
Frank Schaefer | 87b5243 | 2013-03-03 15:37:40 -0300 | [diff] [blame] | 726 | em28xx_info("unknown eeprom format or eeprom corrupted !\n"); |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 727 | err = -ENODEV; |
| 728 | goto error; |
Frank Schaefer | f55eacb | 2013-03-03 15:37:37 -0300 | [diff] [blame] | 729 | } |
| 730 | |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 731 | *eedata = data; |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 732 | *eedata_len = len; |
Alban Browaeys | 32bf7c6 | 2013-07-16 18:57:53 -0300 | [diff] [blame] | 733 | dev_config = (void *)*eedata; |
Frank Schaefer | a217968 | 2013-03-03 15:37:42 -0300 | [diff] [blame] | 734 | |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 735 | switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 736 | case 0: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 737 | em28xx_info("\tNo audio on board.\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 738 | break; |
| 739 | case 1: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 740 | em28xx_info("\tAC97 audio (5 sample rates)\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 741 | break; |
| 742 | case 2: |
Frank Schaefer | 687ff8b | 2013-12-22 11:17:46 -0300 | [diff] [blame] | 743 | if (dev->chip_id < CHIP_ID_EM2860) |
| 744 | em28xx_info("\tI2S audio, sample rate=32k\n"); |
| 745 | else |
| 746 | em28xx_info("\tI2S audio, 3 sample rates\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 747 | break; |
| 748 | case 3: |
Frank Schaefer | 687ff8b | 2013-12-22 11:17:46 -0300 | [diff] [blame] | 749 | if (dev->chip_id < CHIP_ID_EM2860) |
| 750 | em28xx_info("\tI2S audio, 3 sample rates\n"); |
| 751 | else |
| 752 | em28xx_info("\tI2S audio, 5 sample rates\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 756 | if (le16_to_cpu(dev_config->chip_conf) & 1 << 3) |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 757 | em28xx_info("\tUSB Remote wakeup capable\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 758 | |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 759 | if (le16_to_cpu(dev_config->chip_conf) & 1 << 2) |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 760 | em28xx_info("\tUSB Self power capable\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 761 | |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 762 | switch (le16_to_cpu(dev_config->chip_conf) & 0x3) { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 763 | case 0: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 764 | em28xx_info("\t500mA max power\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 765 | break; |
| 766 | case 1: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 767 | em28xx_info("\t400mA max power\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 768 | break; |
| 769 | case 2: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 770 | em28xx_info("\t300mA max power\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 771 | break; |
| 772 | case 3: |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 773 | em28xx_info("\t200mA max power\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 774 | break; |
| 775 | } |
Frank Schaefer | 12d7ce1 | 2013-03-03 15:37:34 -0300 | [diff] [blame] | 776 | em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n", |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 777 | dev_config->string_idx_table, |
| 778 | le16_to_cpu(dev_config->string1), |
| 779 | le16_to_cpu(dev_config->string2), |
| 780 | le16_to_cpu(dev_config->string3)); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 781 | |
| 782 | return 0; |
Frank Schaefer | 510e884 | 2013-03-03 15:37:43 -0300 | [diff] [blame] | 783 | |
| 784 | error: |
| 785 | kfree(data); |
| 786 | return err; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* ----------------------------------------------------------- */ |
| 790 | |
| 791 | /* |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 792 | * functionality() |
| 793 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 794 | static u32 functionality(struct i2c_adapter *i2c_adap) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 795 | { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 796 | struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 797 | |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 798 | if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) || |
| 799 | (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) { |
| 800 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 801 | } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) { |
| 802 | return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) & |
| 803 | ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA; |
| 804 | } |
| 805 | |
| 806 | WARN(1, "Unknown i2c bus algorithm.\n"); |
| 807 | return 0; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 810 | static struct i2c_algorithm em28xx_algo = { |
| 811 | .master_xfer = em28xx_i2c_xfer, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 812 | .functionality = functionality, |
| 813 | }; |
| 814 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 815 | static struct i2c_adapter em28xx_adap_template = { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 816 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 817 | .name = "em28xx", |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 818 | .algo = &em28xx_algo, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 819 | }; |
| 820 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 821 | static struct i2c_client em28xx_client_template = { |
| 822 | .name = "em28xx internal", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 823 | }; |
| 824 | |
| 825 | /* ----------------------------------------------------------- */ |
| 826 | |
| 827 | /* |
| 828 | * i2c_devs |
| 829 | * incomplete list of known devices |
| 830 | */ |
| 831 | static char *i2c_devs[128] = { |
Frank Schaefer | 0b3966e | 2013-01-13 10:15:08 -0300 | [diff] [blame] | 832 | [0x3e >> 1] = "remote IR sensor", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 833 | [0x4a >> 1] = "saa7113h", |
Martin Blumenstingl | 729841e | 2012-06-12 18:19:27 -0300 | [diff] [blame] | 834 | [0x52 >> 1] = "drxk", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 835 | [0x60 >> 1] = "remote IR sensor", |
Markus Rechberger | da45a2a | 2005-11-08 21:37:31 -0800 | [diff] [blame] | 836 | [0x8e >> 1] = "remote IR sensor", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 837 | [0x86 >> 1] = "tda9887", |
| 838 | [0x80 >> 1] = "msp34xx", |
| 839 | [0x88 >> 1] = "msp34xx", |
| 840 | [0xa0 >> 1] = "eeprom", |
Vitaly Wool | 2bd1d9eb | 2009-03-04 08:27:52 -0300 | [diff] [blame] | 841 | [0xb0 >> 1] = "tda9874", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 842 | [0xb8 >> 1] = "tvp5150a", |
Mauro Carvalho Chehab | 791a08f | 2009-07-03 15:36:18 -0300 | [diff] [blame] | 843 | [0xba >> 1] = "webcam sensor or tvp5150a", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 844 | [0xc0 >> 1] = "tuner (analog)", |
| 845 | [0xc2 >> 1] = "tuner (analog)", |
| 846 | [0xc4 >> 1] = "tuner (analog)", |
| 847 | [0xc6 >> 1] = "tuner (analog)", |
| 848 | }; |
| 849 | |
| 850 | /* |
| 851 | * do_i2c_scan() |
| 852 | * check i2c address range for devices |
| 853 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 854 | void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 855 | { |
Sascha Sommer | fad7b95 | 2007-11-04 08:06:48 -0300 | [diff] [blame] | 856 | u8 i2c_devicelist[128]; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 857 | unsigned char buf; |
| 858 | int i, rc; |
| 859 | |
Sascha Sommer | fad7b95 | 2007-11-04 08:06:48 -0300 | [diff] [blame] | 860 | memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist)); |
| 861 | |
Mauro Carvalho Chehab | 53c4e95 | 2007-03-29 08:42:30 -0300 | [diff] [blame] | 862 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 863 | dev->i2c_client[bus].addr = i; |
| 864 | rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 865 | if (rc < 0) |
| 866 | continue; |
Sascha Sommer | fad7b95 | 2007-11-04 08:06:48 -0300 | [diff] [blame] | 867 | i2c_devicelist[i] = i; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 868 | em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n", |
| 869 | i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 870 | } |
Sascha Sommer | fad7b95 | 2007-11-04 08:06:48 -0300 | [diff] [blame] | 871 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 872 | if (bus == dev->def_i2c_bus) |
| 873 | dev->i2c_hash = em28xx_hash_mem(i2c_devicelist, |
| 874 | ARRAY_SIZE(i2c_devicelist), 32); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 878 | * em28xx_i2c_register() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 879 | * register i2c bus |
| 880 | */ |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 881 | int em28xx_i2c_register(struct em28xx *dev, unsigned bus, |
| 882 | enum em28xx_i2c_algo_type algo_type) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 883 | { |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 884 | int retval; |
| 885 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 886 | BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg); |
| 887 | BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req); |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 888 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 889 | if (bus >= NUM_I2C_BUSES) |
| 890 | return -ENODEV; |
| 891 | |
| 892 | dev->i2c_adap[bus] = em28xx_adap_template; |
| 893 | dev->i2c_adap[bus].dev.parent = &dev->udev->dev; |
| 894 | strcpy(dev->i2c_adap[bus].name, dev->name); |
| 895 | |
| 896 | dev->i2c_bus[bus].bus = bus; |
Frank Schaefer | a3ea4bf | 2013-03-26 13:38:36 -0300 | [diff] [blame] | 897 | dev->i2c_bus[bus].algo_type = algo_type; |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 898 | dev->i2c_bus[bus].dev = dev; |
| 899 | dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus]; |
| 900 | i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev); |
| 901 | |
| 902 | retval = i2c_add_adapter(&dev->i2c_adap[bus]); |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 903 | if (retval < 0) { |
| 904 | em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n", |
| 905 | __func__, retval); |
| 906 | return retval; |
| 907 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 908 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 909 | dev->i2c_client[bus] = em28xx_client_template; |
| 910 | dev->i2c_client[bus].adapter = &dev->i2c_adap[bus]; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 911 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 912 | /* Up to now, all eeproms are at bus 0 */ |
| 913 | if (!bus) { |
| 914 | retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len); |
| 915 | if ((retval < 0) && (retval != -ENODEV)) { |
| 916 | em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n", |
| 917 | __func__, retval); |
Mauro Carvalho Chehab | c41109f | 2008-11-15 23:44:14 -0300 | [diff] [blame] | 918 | |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 919 | return retval; |
| 920 | } |
Douglas Schilling Landgraf | f2a01a0 | 2008-09-08 03:27:20 -0300 | [diff] [blame] | 921 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 922 | |
| 923 | if (i2c_scan) |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 924 | em28xx_do_i2c_scan(dev, bus); |
Mauro Carvalho Chehab | c41109f | 2008-11-15 23:44:14 -0300 | [diff] [blame] | 925 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 930 | * em28xx_i2c_unregister() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 931 | * unregister i2c_bus |
| 932 | */ |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 933 | int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 934 | { |
Mauro Carvalho Chehab | aab3125 | 2013-03-05 06:55:28 -0300 | [diff] [blame] | 935 | if (bus >= NUM_I2C_BUSES) |
| 936 | return -ENODEV; |
| 937 | |
| 938 | i2c_del_adapter(&dev->i2c_adap[bus]); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 939 | return 0; |
| 940 | } |