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