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