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