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