blob: fc90f5a32255f2ccdbdb6544cb509e301a4ad12f [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefera3ea4bf2013-03-26 13:38:36 -03008 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
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 Chehab8314d402016-10-12 07:26:47 -030025#include "em28xx.h"
26
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080027#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/usb.h>
30#include <linux/i2c.h>
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -030031#include <linux/jiffies.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032
Mauro Carvalho Chehab6c362c82007-10-29 23:36:12 -030033#include "tuner-xc2028.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020034#include <media/v4l2-common.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080035#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080036
37/* ----------------------------------------------------------- */
38
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030039static unsigned int i2c_scan;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080040module_param(i2c_scan, int, 0444);
41MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
42
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030043static unsigned int i2c_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044module_param(i2c_debug, int, 0644);
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -030045MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -020047#define dprintk(level, fmt, arg...) do { \
48 if (i2c_debug > level) \
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -020049 dev_printk(KERN_DEBUG, &dev->intf->dev, \
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -020050 "i2c: %s: " fmt, __func__, ## arg); \
51} while (0)
52
53
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054/*
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -050055 * Time in msecs to wait for i2c xfers to finish.
56 * 35ms is the maximum time a SMBUS device could wait when
57 * clock stretching is used. As the transfer itself will take
58 * some time to happen, set it to 35 ms.
59 *
60 * Ok, I2C doesn't specify any limit. So, eventually, we may need
61 * to increase this timeout.
62 */
63#define EM28XX_I2C_XFER_TIMEOUT 35 /* ms */
64
65static int em28xx_i2c_timeout(struct em28xx *dev)
66{
67 int time = EM28XX_I2C_XFER_TIMEOUT;
68
69 switch (dev->i2c_speed & 0x03) {
70 case EM28XX_I2C_FREQ_25_KHZ:
71 time += 4; /* Assume 4 ms for transfers */
72 break;
73 case EM28XX_I2C_FREQ_100_KHZ:
74 case EM28XX_I2C_FREQ_400_KHZ:
75 time += 1; /* Assume 1 ms for transfers */
76 break;
77 default: /* EM28XX_I2C_FREQ_1_5_MHZ */
78 break;
79 }
80
81 return msecs_to_jiffies(time);
82}
83
84/*
Frank Schaeferf5ae3712013-01-03 14:27:02 -030085 * em2800_i2c_send_bytes()
86 * send up to 4 bytes to the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080087 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -030088static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080089{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -050090 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080091 int ret;
Frank Schaefera6bad042012-12-16 14:23:27 -030092 u8 b2[6];
Frank Schaeferf5ae3712013-01-03 14:27:02 -030093
94 if (len < 1 || len > 4)
95 return -EOPNOTSUPP;
96
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080097 BUG_ON(len < 1 || len > 4);
98 b2[5] = 0x80 + len - 1;
99 b2[4] = addr;
100 b2[3] = buf[0];
101 if (len > 1)
102 b2[2] = buf[1];
103 if (len > 2)
104 b2[1] = buf[2];
105 if (len > 3)
106 b2[0] = buf[3];
107
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300108 /* trigger write */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800109 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800110 if (ret != 2 + len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200111 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200112 "failed to trigger write to i2c address 0x%x (error=%i)\n",
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300113 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300114 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800115 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300116 /* wait for completion */
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300117 while (time_is_after_jiffies(timeout)) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800118 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300119 if (ret == 0x80 + len - 1)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800120 return len;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300121 if (ret == 0x94 + len - 1) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200122 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300123 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300124 }
125 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200126 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200127 "failed to get i2c transfer status from bridge register (error=%i)\n",
Mauro Carvalho Chehab2a96f602016-10-12 07:32:23 -0300128 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300129 return ret;
130 }
Markus Rechbergere8e41da2006-02-07 06:49:11 -0200131 msleep(5);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800132 }
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200133 dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300134 return -ETIMEDOUT;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800135}
136
137/*
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800138 * em2800_i2c_recv_bytes()
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300139 * read up to 4 bytes from the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800140 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300141static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800142{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -0500143 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300144 u8 buf2[4];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800145 int ret;
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300146 int i;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300147
148 if (len < 1 || len > 4)
149 return -EOPNOTSUPP;
150
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300151 /* trigger read */
152 buf2[1] = 0x84 + len - 1;
153 buf2[0] = addr;
154 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
155 if (ret != 2) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200156 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200157 "failed to trigger read from i2c address 0x%x (error=%i)\n",
158 addr, ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300159 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800160 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300161
162 /* wait for completion */
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300163 while (time_is_after_jiffies(timeout)) {
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300164 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300165 if (ret == 0x84 + len - 1)
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300166 break;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300167 if (ret == 0x94 + len - 1) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200168 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
169 ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300170 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300171 }
172 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200173 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200174 "failed to get i2c transfer status from bridge register (error=%i)\n",
175 ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300176 return ret;
177 }
178 msleep(5);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800179 }
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300180 if (ret != 0x84 + len - 1) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200181 dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300182 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300183
184 /* get the received message */
185 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
186 if (ret != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200187 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200188 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
189 addr, ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300190 return (ret < 0) ? ret : -EIO;
191 }
192 for (i = 0; i < len; i++)
193 buf[i] = buf2[len - 1 - i];
194
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800195 return ret;
196}
197
198/*
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300199 * em2800_i2c_check_for_device()
200 * check if there is an i2c device at the supplied address
201 */
202static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
203{
204 u8 buf;
205 int ret;
206
207 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
208 if (ret == 1)
209 return 0;
210 return (ret < 0) ? ret : -EIO;
211}
212
213/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800214 * em28xx_i2c_send_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800215 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300216static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
217 u16 len, int stop)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800218{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -0500219 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300220 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800221
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300222 if (len < 1 || len > 64)
223 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300224 /*
225 * NOTE: limited by the USB ctrl message constraints
226 * Zero length reads always succeed, even if no device is connected
227 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300228
Frank Schaefer45f04e82013-01-03 14:27:05 -0300229 /* Write to i2c device */
230 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
231 if (ret != len) {
232 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200233 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200234 "writing to i2c device at 0x%x failed (error=%i)\n",
235 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300236 return ret;
237 } else {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200238 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200239 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
240 len, addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300241 return -EIO;
242 }
243 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800244
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300245 /* wait for completion */
246 while (time_is_after_jiffies(timeout)) {
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300247 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300248 if (ret == 0) /* success */
Frank Schaefer45f04e82013-01-03 14:27:05 -0300249 return len;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300250 if (ret == 0x10) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200251 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
252 addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300253 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300254 }
255 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200256 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200257 "failed to get i2c transfer status from bridge register (error=%i)\n",
258 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300259 return ret;
260 }
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300261 msleep(5);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300262 /*
263 * NOTE: do we really have to wait for success ?
264 * Never seen anything else than 0x00 or 0x10
265 * (even with high payload) ...
266 */
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300267 }
Frank Schaefer123a17d2014-01-19 18:48:35 -0300268
269 if (ret == 0x02 || ret == 0x04) {
270 /* NOTE: these errors seem to be related to clock stretching */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200271 dprintk(0,
272 "write to i2c device at 0x%x timed out (status=%i)\n",
273 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300274 return -ETIMEDOUT;
275 }
276
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200277 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200278 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
279 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300280 return -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800281}
282
283/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800284 * em28xx_i2c_recv_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800285 * read a byte from the i2c device
286 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300287static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800288{
289 int ret;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300290
291 if (len < 1 || len > 64)
292 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300293 /*
294 * NOTE: limited by the USB ctrl message constraints
295 * Zero length reads always succeed, even if no device is connected
296 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300297
Frank Schaefer45f04e82013-01-03 14:27:05 -0300298 /* Read data from i2c device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800299 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300300 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200301 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200302 "reading from i2c device at 0x%x failed (error=%i)\n",
303 addr, ret);
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300304 return ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300305 }
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300306 /*
307 * NOTE: some devices with two i2c busses have the bad habit to return 0
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300308 * bytes if we are on bus B AND there was no write attempt to the
309 * specified slave address before AND no device is present at the
310 * requested slave address.
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300311 * Anyway, the next check will fail with -ENXIO in this case, so avoid
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300312 * spamming the system log on device probing and do nothing here.
313 */
Frank Schaefer45f04e82013-01-03 14:27:05 -0300314
315 /* Check success of the i2c operation */
316 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300317 if (ret == 0) /* success */
318 return len;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800319 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200320 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200321 "failed to get i2c transfer status from bridge register (error=%i)\n",
322 ret);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800323 return ret;
324 }
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300325 if (ret == 0x10) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200326 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
327 addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300328 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300329 }
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300330
Frank Schaefer123a17d2014-01-19 18:48:35 -0300331 if (ret == 0x02 || ret == 0x04) {
332 /* NOTE: these errors seem to be related to clock stretching */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200333 dprintk(0,
334 "write to i2c device at 0x%x timed out (status=%i)\n",
335 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300336 return -ETIMEDOUT;
337 }
338
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200339 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200340 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
341 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300342 return -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800343}
344
345/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800346 * em28xx_i2c_check_for_device()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800347 * check if there is a i2c_device at the supplied address
348 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300349static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800350{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800351 int ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300352 u8 buf;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800353
Frank Schaefer45f04e82013-01-03 14:27:05 -0300354 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
355 if (ret == 1)
356 return 0;
357 return (ret < 0) ? ret : -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800358}
359
360/*
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300361 * em25xx_bus_B_send_bytes
362 * write bytes to the i2c device
363 */
364static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
365 u16 len)
366{
367 int ret;
368
369 if (len < 1 || len > 64)
370 return -EOPNOTSUPP;
371 /*
372 * NOTE: limited by the USB ctrl message constraints
373 * Zero length reads always succeed, even if no device is connected
374 */
375
376 /* Set register and write value */
377 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
378 if (ret != len) {
379 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200380 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200381 "writing to i2c device at 0x%x failed (error=%i)\n",
382 addr, ret);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300383 return ret;
384 } else {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200385 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200386 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
387 len, addr, ret);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300388 return -EIO;
389 }
390 }
391 /* Check success */
392 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
393 /*
394 * NOTE: the only error we've seen so far is
395 * 0x01 when the slave device is not present
396 */
397 if (!ret)
398 return len;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300399 else if (ret > 0) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200400 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300401 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300402 }
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300403
404 return ret;
405 /*
406 * NOTE: With chip types (other chip IDs) which actually don't support
407 * this operation, it seems to succeed ALWAYS ! (even if there is no
408 * slave device or even no second i2c bus provided)
409 */
410}
411
412/*
413 * em25xx_bus_B_recv_bytes
414 * read bytes from the i2c device
415 */
416static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
417 u16 len)
418{
419 int ret;
420
421 if (len < 1 || len > 64)
422 return -EOPNOTSUPP;
423 /*
424 * NOTE: limited by the USB ctrl message constraints
425 * Zero length reads always succeed, even if no device is connected
426 */
427
428 /* Read value */
429 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
430 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200431 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200432 "reading from i2c device at 0x%x failed (error=%i)\n",
433 addr, ret);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300434 return ret;
435 }
436 /*
437 * NOTE: some devices with two i2c busses have the bad habit to return 0
438 * bytes if we are on bus B AND there was no write attempt to the
439 * specified slave address before AND no device is present at the
440 * requested slave address.
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300441 * Anyway, the next check will fail with -ENXIO in this case, so avoid
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300442 * spamming the system log on device probing and do nothing here.
443 */
444
445 /* Check success */
446 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
447 /*
448 * NOTE: the only error we've seen so far is
449 * 0x01 when the slave device is not present
450 */
451 if (!ret)
452 return len;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300453 else if (ret > 0) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200454 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300455 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300456 }
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300457
458 return ret;
459 /*
460 * NOTE: With chip types (other chip IDs) which actually don't support
461 * this operation, it seems to succeed ALWAYS ! (even if there is no
462 * slave device or even no second i2c bus provided)
463 */
464}
465
466/*
467 * em25xx_bus_B_check_for_device()
468 * check if there is a i2c device at the supplied address
469 */
470static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
471{
472 u8 buf;
473 int ret;
474
475 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
476 if (ret < 0)
477 return ret;
478
479 return 0;
480 /*
481 * NOTE: With chips which do not support this operation,
482 * it seems to succeed ALWAYS ! (even if no device connected)
483 */
484}
485
486static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
487{
488 struct em28xx *dev = i2c_bus->dev;
489 int rc = -EOPNOTSUPP;
490
491 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
492 rc = em28xx_i2c_check_for_device(dev, addr);
493 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
494 rc = em2800_i2c_check_for_device(dev, addr);
495 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
496 rc = em25xx_bus_B_check_for_device(dev, addr);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300497 return rc;
498}
499
500static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
501 struct i2c_msg msg)
502{
503 struct em28xx *dev = i2c_bus->dev;
504 u16 addr = msg.addr << 1;
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300505 int rc = -EOPNOTSUPP;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300506
507 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
508 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
509 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
510 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
511 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
512 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300513 return rc;
514}
515
516static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
517 struct i2c_msg msg, int stop)
518{
519 struct em28xx *dev = i2c_bus->dev;
520 u16 addr = msg.addr << 1;
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300521 int rc = -EOPNOTSUPP;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300522
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300523 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
524 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
525 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
526 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
527 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
528 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
529 return rc;
530}
531
532/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800533 * em28xx_i2c_xfer()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800534 * the main i2c transfer function
535 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800536static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800537 struct i2c_msg msgs[], int num)
538{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300539 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
540 struct em28xx *dev = i2c_bus->dev;
541 unsigned bus = i2c_bus->bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300542 int addr, rc, i;
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300543 u8 reg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800544
Shuah Khancc5c5d22014-07-11 18:25:55 -0300545 /* prevent i2c xfer attempts after device is disconnected
546 some fe's try to do i2c writes/reads from their release
547 interfaces when called in disconnect path */
548 if (dev->disconnected)
549 return -ENODEV;
550
Dan Carpentere44c1532016-05-09 05:22:55 -0300551 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
552 return -EAGAIN;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300553
554 /* Switch I2C bus if needed */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300555 if (bus != dev->cur_i2c_bus &&
556 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300557 if (bus == 1)
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300558 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300559 else
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300560 reg = 0;
561 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
562 EM2874_I2C_SECONDARY_BUS_SELECT);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300563 dev->cur_i2c_bus = bus;
564 }
565
566 if (num <= 0) {
567 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800568 return 0;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300569 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800570 for (i = 0; i < num; i++) {
571 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300572 if (!msgs[i].len) {
573 /*
574 * no len: check only for device presence
575 * This code is only called during device probe.
576 */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300577 rc = i2c_check_for_device(i2c_bus, addr);
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200578
579 if (rc == -ENXIO)
580 rc = -ENODEV;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800581 } else if (msgs[i].flags & I2C_M_RD) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800582 /* read bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300583 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800584 } else {
585 /* write bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300586 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800587 }
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200588
589 if (rc < 0)
590 goto error;
591
592 dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
593 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
594 i == num - 1 ? "stop" : "nonstop",
595 addr, msgs[i].len,
596 msgs[i].len, msgs[i].buf);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800597 }
598
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300599 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800600 return num;
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200601
602error:
603 dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
604 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
605 i == num - 1 ? "stop" : "nonstop",
606 addr, msgs[i].len,
607 (rc == -ENODEV) ? "no device " : "",
608 rc);
609
610 rt_mutex_unlock(&dev->i2c_bus_lock);
611 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800612}
613
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300614/*
615 * based on linux/sunrpc/svcauth.h and linux/hash.h
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300616 * The original hash function returns a different value, if arch is x86_64
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300617 * or i386.
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300618 */
619static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
620{
621 unsigned long hash = 0;
622 unsigned long l = 0;
623 int len = 0;
624 unsigned char c;
Mauro Carvalho Chehabfdf1bc92014-11-28 08:34:15 -0300625
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300626 do {
627 if (len == length) {
628 c = (char)len;
629 len = -1;
630 } else
631 c = *buf++;
632 l = (l << 8) | c;
633 len++;
634 if ((len & (32 / 8 - 1)) == 0)
635 hash = ((hash^l) * 0x9e370001UL);
636 } while (len);
637
638 return (hash >> (32 - bits)) & 0xffffffffUL;
639}
640
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300641/*
642 * Helper function to read data blocks from i2c clients with 8 or 16 bit
643 * address width, 8 bit register width and auto incrementation been activated
644 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300645static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
646 bool addr_w16, u16 len, u8 *data)
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300647{
648 int remain = len, rsize, rsize_max, ret;
649 u8 buf[2];
650
651 /* Sanity check */
652 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
653 return -EINVAL;
654 /* Select address */
655 buf[0] = addr >> 8;
656 buf[1] = addr & 0xff;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300657 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300658 if (ret < 0)
659 return ret;
660 /* Read data */
661 if (dev->board.is_em2800)
662 rsize_max = 4;
663 else
664 rsize_max = 64;
665 while (remain > 0) {
666 if (remain > rsize_max)
667 rsize = rsize_max;
668 else
669 rsize = remain;
670
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300671 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300672 if (ret < 0)
673 return ret;
674
675 remain -= rsize;
676 data += rsize;
677 }
678
679 return len;
680}
681
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300682static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
683 u8 **eedata, u16 *eedata_len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800684{
Frank Schaefer510e8842013-03-03 15:37:43 -0300685 const u16 len = 256;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300686 /*
687 * FIXME common length/size for bytes to read, to display, hash
Frank Schaefer510e8842013-03-03 15:37:43 -0300688 * calculation and returned device dataset. Simplifies the code a lot,
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300689 * but we might have to deal with multiple sizes in the future !
690 */
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300691 int err;
Frank Schaefer510e8842013-03-03 15:37:43 -0300692 struct em28xx_eeprom *dev_config;
693 u8 buf, *data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800694
Frank Schaefera2179682013-03-03 15:37:42 -0300695 *eedata = NULL;
Frank Schaefer510e8842013-03-03 15:37:43 -0300696 *eedata_len = 0;
Frank Schaefera2179682013-03-03 15:37:42 -0300697
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300698 /* EEPROM is always on i2c bus 0 on all known devices. */
699
700 dev->i2c_client[bus].addr = 0xa0 >> 1;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800701
702 /* Check if board has eeprom */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300703 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300704 if (err < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200705 dev_info(&dev->intf->dev, "board has no eeprom\n");
Mauro Carvalho Chehabc41109f2008-11-15 23:44:14 -0300706 return -ENODEV;
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300707 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800708
Frank Schaefera2179682013-03-03 15:37:42 -0300709 data = kzalloc(len, GFP_KERNEL);
710 if (data == NULL)
711 return -ENOMEM;
712
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300713 /* Read EEPROM content */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300714 err = em28xx_i2c_read_block(dev, bus, 0x0000,
715 dev->eeprom_addrwidth_16bit,
Frank Schaefera2179682013-03-03 15:37:42 -0300716 len, data);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300717 if (err != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200718 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200719 "failed to read eeprom (err=%d)\n", err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300720 goto error;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800721 }
Frank Schaefer90271962013-01-03 14:27:06 -0300722
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300723 if (i2c_debug) {
724 /* Display eeprom content */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200725 print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300726 16, 1, data, len, true);
727
728 if (dev->eeprom_addrwidth_16bit)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200729 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200730 "eeprom %06x: ... (skipped)\n", 256);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800731 }
732
Frank Schaefer87b52432013-03-03 15:37:40 -0300733 if (dev->eeprom_addrwidth_16bit &&
Frank Schaefera2179682013-03-03 15:37:42 -0300734 data[0] == 0x26 && data[3] == 0x00) {
Frank Schaefer87b52432013-03-03 15:37:40 -0300735 /* new eeprom format; size 4-64kb */
Frank Schaefer510e8842013-03-03 15:37:43 -0300736 u16 mc_start;
737 u16 hwconf_offset;
738
Frank Schaefera2179682013-03-03 15:37:42 -0300739 dev->hash = em28xx_hash_mem(data, len, 32);
Frank Schaefer510e8842013-03-03 15:37:43 -0300740 mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
741
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200742 dev_info(&dev->intf->dev,
Antonio Cardace290ef7d2018-02-09 09:13:26 -0500743 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
744 data, dev->hash);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200745 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200746 "EEPROM info:\n");
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200747 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200748 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
749 mc_start, data[2]);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300750 /*
751 * boot configuration (address 0x0002):
Frank Schaefer87b52432013-03-03 15:37:40 -0300752 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
753 * [1] always selects 12 kb RAM
754 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
755 * [4] 1 = force fast mode and no suspend for device testing
756 * [5:7] USB PHY tuning registers; determined by device
757 * characterization
758 */
759
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300760 /*
761 * Read hardware config dataset offset from address
762 * (microcode start + 46)
763 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300764 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
765 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300766 if (err != 2) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200767 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200768 "failed to read hardware configuration data from eeprom (err=%d)\n",
769 err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300770 goto error;
771 }
Frank Schaefer87b52432013-03-03 15:37:40 -0300772
Frank Schaefer510e8842013-03-03 15:37:43 -0300773 /* Calculate hardware config dataset start address */
774 hwconf_offset = mc_start + data[0] + (data[1] << 8);
775
776 /* Read hardware config dataset */
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300777 /*
778 * NOTE: the microcode copy can be multiple pages long, but
Frank Schaefer510e8842013-03-03 15:37:43 -0300779 * we assume the hardware config dataset is the same as in
780 * the old eeprom and not longer than 256 bytes.
781 * tveeprom is currently also limited to 256 bytes.
782 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300783 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
784 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300785 if (err != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200786 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200787 "failed to read hardware configuration data from eeprom (err=%d)\n",
788 err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300789 goto error;
790 }
791
792 /* Verify hardware config dataset */
793 /* NOTE: not all devices provide this type of dataset */
794 if (data[0] != 0x1a || data[1] != 0xeb ||
795 data[2] != 0x67 || data[3] != 0x95) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200796 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200797 "\tno hardware configuration dataset found in eeprom\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300798 kfree(data);
799 return 0;
800 }
801
802 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
803
804 } else if (!dev->eeprom_addrwidth_16bit &&
805 data[0] == 0x1a && data[1] == 0xeb &&
806 data[2] == 0x67 && data[3] == 0x95) {
807 dev->hash = em28xx_hash_mem(data, len, 32);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200808 dev_info(&dev->intf->dev,
Antonio Cardace290ef7d2018-02-09 09:13:26 -0500809 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
810 data, dev->hash);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200811 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200812 "EEPROM info:\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300813 } else {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200814 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200815 "unknown eeprom format or eeprom corrupted !\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300816 err = -ENODEV;
817 goto error;
Frank Schaeferf55eacb2013-03-03 15:37:37 -0300818 }
819
Frank Schaefera2179682013-03-03 15:37:42 -0300820 *eedata = data;
Frank Schaefer510e8842013-03-03 15:37:43 -0300821 *eedata_len = len;
Alban Browaeys32bf7c62013-07-16 18:57:53 -0300822 dev_config = (void *)*eedata;
Frank Schaefera2179682013-03-03 15:37:42 -0300823
Frank Schaefer510e8842013-03-03 15:37:43 -0300824 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800825 case 0:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200826 dev_info(&dev->intf->dev, "\tNo audio on board.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800827 break;
828 case 1:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200829 dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800830 break;
831 case 2:
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300832 if (dev->chip_id < CHIP_ID_EM2860)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200833 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200834 "\tI2S audio, sample rate=32k\n");
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300835 else
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200836 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200837 "\tI2S audio, 3 sample rates\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800838 break;
839 case 3:
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300840 if (dev->chip_id < CHIP_ID_EM2860)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200841 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200842 "\tI2S audio, 3 sample rates\n");
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300843 else
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200844 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200845 "\tI2S audio, 5 sample rates\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800846 break;
847 }
848
Frank Schaefer510e8842013-03-03 15:37:43 -0300849 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200850 dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800851
Frank Schaefer510e8842013-03-03 15:37:43 -0300852 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200853 dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800854
Frank Schaefer510e8842013-03-03 15:37:43 -0300855 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800856 case 0:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200857 dev_info(&dev->intf->dev, "\t500mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800858 break;
859 case 1:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200860 dev_info(&dev->intf->dev, "\t400mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800861 break;
862 case 2:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200863 dev_info(&dev->intf->dev, "\t300mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800864 break;
865 case 3:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200866 dev_info(&dev->intf->dev, "\t200mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800867 break;
868 }
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200869 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200870 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
871 dev_config->string_idx_table,
872 le16_to_cpu(dev_config->string1),
873 le16_to_cpu(dev_config->string2),
874 le16_to_cpu(dev_config->string3));
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800875
876 return 0;
Frank Schaefer510e8842013-03-03 15:37:43 -0300877
878error:
879 kfree(data);
880 return err;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800881}
882
883/* ----------------------------------------------------------- */
884
885/*
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800886 * functionality()
887 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300888static u32 functionality(struct i2c_adapter *i2c_adap)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800889{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300890 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300891
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300892 if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
893 (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
894 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
895 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
896 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
897 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
898 }
899
900 WARN(1, "Unknown i2c bus algorithm.\n");
901 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800902}
903
Julia Lawall78f2c502016-08-29 10:12:01 -0300904static const struct i2c_algorithm em28xx_algo = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800905 .master_xfer = em28xx_i2c_xfer,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800906 .functionality = functionality,
907};
908
Bhumika Goyal68438682017-08-19 06:34:15 -0400909static const struct i2c_adapter em28xx_adap_template = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800910 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800911 .name = "em28xx",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800912 .algo = &em28xx_algo,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800913};
914
Bhumika Goyal2b832472017-08-28 03:21:35 -0400915static const struct i2c_client em28xx_client_template = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800916 .name = "em28xx internal",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800917};
918
919/* ----------------------------------------------------------- */
920
921/*
922 * i2c_devs
923 * incomplete list of known devices
924 */
925static char *i2c_devs[128] = {
Wilson Michaels9aa785b2014-11-11 19:43:51 -0300926 [0x1c >> 1] = "lgdt330x",
Frank Schaefer0b3966e2013-01-13 10:15:08 -0300927 [0x3e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800928 [0x4a >> 1] = "saa7113h",
Martin Blumenstingl729841e2012-06-12 18:19:27 -0300929 [0x52 >> 1] = "drxk",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800930 [0x60 >> 1] = "remote IR sensor",
Markus Rechbergerda45a2a2005-11-08 21:37:31 -0800931 [0x8e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800932 [0x86 >> 1] = "tda9887",
933 [0x80 >> 1] = "msp34xx",
934 [0x88 >> 1] = "msp34xx",
935 [0xa0 >> 1] = "eeprom",
Vitaly Wool2bd1d9eb2009-03-04 08:27:52 -0300936 [0xb0 >> 1] = "tda9874",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800937 [0xb8 >> 1] = "tvp5150a",
Mauro Carvalho Chehab791a08f2009-07-03 15:36:18 -0300938 [0xba >> 1] = "webcam sensor or tvp5150a",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800939 [0xc0 >> 1] = "tuner (analog)",
940 [0xc2 >> 1] = "tuner (analog)",
941 [0xc4 >> 1] = "tuner (analog)",
942 [0xc6 >> 1] = "tuner (analog)",
943};
944
945/*
946 * do_i2c_scan()
947 * check i2c address range for devices
948 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300949void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800950{
Sascha Sommerfad7b952007-11-04 08:06:48 -0300951 u8 i2c_devicelist[128];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800952 unsigned char buf;
953 int i, rc;
954
Sascha Sommerfad7b952007-11-04 08:06:48 -0300955 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
956
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -0300957 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300958 dev->i2c_client[bus].addr = i;
959 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800960 if (rc < 0)
961 continue;
Sascha Sommerfad7b952007-11-04 08:06:48 -0300962 i2c_devicelist[i] = i;
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200963 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200964 "found i2c device @ 0x%x on bus %d [%s]\n",
965 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800966 }
Sascha Sommerfad7b952007-11-04 08:06:48 -0300967
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300968 if (bus == dev->def_i2c_bus)
969 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
970 ARRAY_SIZE(i2c_devicelist), 32);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800971}
972
973/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800974 * em28xx_i2c_register()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800975 * register i2c bus
976 */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300977int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
978 enum em28xx_i2c_algo_type algo_type)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800979{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300980 int retval;
981
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800982 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
983 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300984
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300985 if (bus >= NUM_I2C_BUSES)
986 return -ENODEV;
987
988 dev->i2c_adap[bus] = em28xx_adap_template;
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200989 dev->i2c_adap[bus].dev.parent = &dev->intf->dev;
990 strcpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev));
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300991
992 dev->i2c_bus[bus].bus = bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300993 dev->i2c_bus[bus].algo_type = algo_type;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300994 dev->i2c_bus[bus].dev = dev;
995 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300996
997 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300998 if (retval < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200999 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -02001000 "%s: i2c_add_adapter failed! retval [%d]\n",
1001 __func__, retval);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03001002 return retval;
1003 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001004
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001005 dev->i2c_client[bus] = em28xx_client_template;
1006 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001007
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001008 /* Up to now, all eeproms are at bus 0 */
1009 if (!bus) {
1010 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
1011 if ((retval < 0) && (retval != -ENODEV)) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -02001012 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -02001013 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
1014 __func__, retval);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001015 }
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03001016 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001017
1018 if (i2c_scan)
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001019 em28xx_do_i2c_scan(dev, bus);
Mauro Carvalho Chehabc41109f2008-11-15 23:44:14 -03001020
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001021 return 0;
1022}
1023
1024/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001025 * em28xx_i2c_unregister()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001026 * unregister i2c_bus
1027 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001028int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001029{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001030 if (bus >= NUM_I2C_BUSES)
1031 return -ENODEV;
1032
1033 i2c_del_adapter(&dev->i2c_adap[bus]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001034 return 0;
1035}