blob: 6458682bc6e250b7513be2d6f4a457eaa2ed23f3 [file] [log] [blame]
Mauro Carvalho Chehabf22e9e72018-03-03 10:43:14 -05001// SPDX-License-Identifier: GPL-2.0+
2//
3// em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4//
5// Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6// Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04007// Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehabf22e9e72018-03-03 10:43:14 -05008// Sascha Sommer <saschasommer@freenet.de>
9// Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
10//
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 2 of the License, or
14// (at your option) any later version.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080020
Mauro Carvalho Chehab8314d402016-10-12 07:26:47 -030021#include "em28xx.h"
22
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080023#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/usb.h>
26#include <linux/i2c.h>
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -030027#include <linux/jiffies.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080028
Mauro Carvalho Chehab6c362c82007-10-29 23:36:12 -030029#include "tuner-xc2028.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020030#include <media/v4l2-common.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080031#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032
33/* ----------------------------------------------------------- */
34
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030035static unsigned int i2c_scan;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080036module_param(i2c_scan, int, 0444);
37MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
38
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030039static unsigned int i2c_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080040module_param(i2c_debug, int, 0644);
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -030041MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -020043#define dprintk(level, fmt, arg...) do { \
44 if (i2c_debug > level) \
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -020045 dev_printk(KERN_DEBUG, &dev->intf->dev, \
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -020046 "i2c: %s: " fmt, __func__, ## arg); \
47} while (0)
48
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049/*
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -050050 * Time in msecs to wait for i2c xfers to finish.
51 * 35ms is the maximum time a SMBUS device could wait when
52 * clock stretching is used. As the transfer itself will take
53 * some time to happen, set it to 35 ms.
54 *
55 * Ok, I2C doesn't specify any limit. So, eventually, we may need
56 * to increase this timeout.
57 */
58#define EM28XX_I2C_XFER_TIMEOUT 35 /* ms */
59
60static int em28xx_i2c_timeout(struct em28xx *dev)
61{
62 int time = EM28XX_I2C_XFER_TIMEOUT;
63
64 switch (dev->i2c_speed & 0x03) {
65 case EM28XX_I2C_FREQ_25_KHZ:
66 time += 4; /* Assume 4 ms for transfers */
67 break;
68 case EM28XX_I2C_FREQ_100_KHZ:
69 case EM28XX_I2C_FREQ_400_KHZ:
70 time += 1; /* Assume 1 ms for transfers */
71 break;
72 default: /* EM28XX_I2C_FREQ_1_5_MHZ */
73 break;
74 }
75
76 return msecs_to_jiffies(time);
77}
78
79/*
Frank Schaeferf5ae3712013-01-03 14:27:02 -030080 * em2800_i2c_send_bytes()
81 * send up to 4 bytes to the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080082 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -030083static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080084{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -050085 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080086 int ret;
Frank Schaefera6bad042012-12-16 14:23:27 -030087 u8 b2[6];
Frank Schaeferf5ae3712013-01-03 14:27:02 -030088
89 if (len < 1 || len > 4)
90 return -EOPNOTSUPP;
91
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080092 b2[5] = 0x80 + len - 1;
93 b2[4] = addr;
94 b2[3] = buf[0];
95 if (len > 1)
96 b2[2] = buf[1];
97 if (len > 2)
98 b2[1] = buf[2];
99 if (len > 3)
100 b2[0] = buf[3];
101
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300102 /* trigger write */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800103 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800104 if (ret != 2 + len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200105 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200106 "failed to trigger write to i2c address 0x%x (error=%i)\n",
Frank Schaeferd230d5a2013-03-24 14:58:03 -0300107 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300108 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800109 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300110 /* wait for completion */
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300111 while (time_is_after_jiffies(timeout)) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800112 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300113 if (ret == 0x80 + len - 1)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800114 return len;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300115 if (ret == 0x94 + len - 1) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200116 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300117 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300118 }
119 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200120 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200121 "failed to get i2c transfer status from bridge register (error=%i)\n",
Mauro Carvalho Chehab2a96f602016-10-12 07:32:23 -0300122 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300123 return ret;
124 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500125 usleep_range(5000, 6000);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800126 }
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200127 dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300128 return -ETIMEDOUT;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800129}
130
131/*
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800132 * em2800_i2c_recv_bytes()
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300133 * read up to 4 bytes from the em2800 i2c device
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800134 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300135static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800136{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -0500137 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300138 u8 buf2[4];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800139 int ret;
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300140 int i;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300141
142 if (len < 1 || len > 4)
143 return -EOPNOTSUPP;
144
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300145 /* trigger read */
146 buf2[1] = 0x84 + len - 1;
147 buf2[0] = addr;
148 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
149 if (ret != 2) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200150 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200151 "failed to trigger read from i2c address 0x%x (error=%i)\n",
152 addr, ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300153 return (ret < 0) ? ret : -EIO;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800154 }
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300155
156 /* wait for completion */
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300157 while (time_is_after_jiffies(timeout)) {
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300158 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300159 if (ret == 0x84 + len - 1)
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300160 break;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300161 if (ret == 0x94 + len - 1) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200162 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
163 ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300164 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300165 }
166 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200167 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200168 "failed to get i2c transfer status from bridge register (error=%i)\n",
169 ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300170 return ret;
171 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500172 usleep_range(5000, 6000);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800173 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500174 if (ret != 0x84 + len - 1)
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200175 dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300176
177 /* get the received message */
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500178 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4 - len, buf2, len);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300179 if (ret != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200180 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200181 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
182 addr, ret);
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300183 return (ret < 0) ? ret : -EIO;
184 }
185 for (i = 0; i < len; i++)
186 buf[i] = buf2[len - 1 - i];
187
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800188 return ret;
189}
190
191/*
Frank Schaefer2fcc82d2013-01-03 14:27:03 -0300192 * em2800_i2c_check_for_device()
193 * check if there is an i2c device at the supplied address
194 */
195static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
196{
197 u8 buf;
198 int ret;
199
200 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
201 if (ret == 1)
202 return 0;
203 return (ret < 0) ? ret : -EIO;
204}
205
206/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800207 * em28xx_i2c_send_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800208 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300209static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
210 u16 len, int stop)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800211{
Mauro Carvalho Chehabcf68c222018-03-01 12:54:06 -0500212 unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300213 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800214
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300215 if (len < 1 || len > 64)
216 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300217 /*
218 * NOTE: limited by the USB ctrl message constraints
219 * Zero length reads always succeed, even if no device is connected
220 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300221
Frank Schaefer45f04e82013-01-03 14:27:05 -0300222 /* Write to i2c device */
223 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
224 if (ret != len) {
225 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200226 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200227 "writing to i2c device at 0x%x failed (error=%i)\n",
228 addr, ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300229 return ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300230 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500231 dev_warn(&dev->intf->dev,
232 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
233 len, addr, ret);
234 return -EIO;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300235 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800236
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300237 /* wait for completion */
238 while (time_is_after_jiffies(timeout)) {
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300239 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300240 if (ret == 0) /* success */
Frank Schaefer45f04e82013-01-03 14:27:05 -0300241 return len;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300242 if (ret == 0x10) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200243 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
244 addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300245 return -ENXIO;
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300246 }
247 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200248 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200249 "failed to get i2c transfer status from bridge register (error=%i)\n",
250 ret);
Frank Schaefer45f04e82013-01-03 14:27:05 -0300251 return ret;
252 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500253 usleep_range(5000, 6000);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300254 /*
255 * NOTE: do we really have to wait for success ?
256 * Never seen anything else than 0x00 or 0x10
257 * (even with high payload) ...
258 */
Mauro Carvalho Chehabbbc70e62011-07-09 19:36:11 -0300259 }
Frank Schaefer123a17d2014-01-19 18:48:35 -0300260
261 if (ret == 0x02 || ret == 0x04) {
262 /* NOTE: these errors seem to be related to clock stretching */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200263 dprintk(0,
264 "write to i2c device at 0x%x timed out (status=%i)\n",
265 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300266 return -ETIMEDOUT;
267 }
268
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200269 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200270 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
271 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300272 return -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800273}
274
275/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800276 * em28xx_i2c_recv_bytes()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800277 * read a byte from the i2c device
278 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300279static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800280{
281 int ret;
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300282
283 if (len < 1 || len > 64)
284 return -EOPNOTSUPP;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300285 /*
286 * NOTE: limited by the USB ctrl message constraints
287 * Zero length reads always succeed, even if no device is connected
288 */
Frank Schaeferf5ae3712013-01-03 14:27:02 -0300289
Frank Schaefer45f04e82013-01-03 14:27:05 -0300290 /* Read data from i2c device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800291 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300292 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200293 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200294 "reading from i2c device at 0x%x failed (error=%i)\n",
295 addr, ret);
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300296 return ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300297 }
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300298 /*
299 * NOTE: some devices with two i2c busses have the bad habit to return 0
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300300 * bytes if we are on bus B AND there was no write attempt to the
301 * specified slave address before AND no device is present at the
302 * requested slave address.
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300303 * Anyway, the next check will fail with -ENXIO in this case, so avoid
Frank Schaefer7f6301d2013-03-10 07:25:25 -0300304 * spamming the system log on device probing and do nothing here.
305 */
Frank Schaefer45f04e82013-01-03 14:27:05 -0300306
307 /* Check success of the i2c operation */
308 ret = dev->em28xx_read_reg(dev, 0x05);
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300309 if (ret == 0) /* success */
310 return len;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800311 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200312 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200313 "failed to get i2c transfer status from bridge register (error=%i)\n",
314 ret);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800315 return ret;
316 }
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300317 if (ret == 0x10) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200318 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
319 addr);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300320 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300321 }
Mauro Carvalho Chehab4b836262014-01-04 05:42:11 -0300322
Frank Schaefer123a17d2014-01-19 18:48:35 -0300323 if (ret == 0x02 || ret == 0x04) {
324 /* NOTE: these errors seem to be related to clock stretching */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200325 dprintk(0,
326 "write to i2c device at 0x%x timed out (status=%i)\n",
327 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300328 return -ETIMEDOUT;
329 }
330
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200331 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200332 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
333 addr, ret);
Frank Schaefer123a17d2014-01-19 18:48:35 -0300334 return -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800335}
336
337/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800338 * em28xx_i2c_check_for_device()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800339 * check if there is a i2c_device at the supplied address
340 */
Frank Schaefera6bad042012-12-16 14:23:27 -0300341static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800342{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800343 int ret;
Frank Schaefer45f04e82013-01-03 14:27:05 -0300344 u8 buf;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800345
Frank Schaefer45f04e82013-01-03 14:27:05 -0300346 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
347 if (ret == 1)
348 return 0;
349 return (ret < 0) ? ret : -EIO;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800350}
351
352/*
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300353 * em25xx_bus_B_send_bytes
354 * write bytes to the i2c device
355 */
356static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
357 u16 len)
358{
359 int ret;
360
361 if (len < 1 || len > 64)
362 return -EOPNOTSUPP;
363 /*
364 * NOTE: limited by the USB ctrl message constraints
365 * Zero length reads always succeed, even if no device is connected
366 */
367
368 /* Set register and write value */
369 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
370 if (ret != len) {
371 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200372 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200373 "writing to i2c device at 0x%x failed (error=%i)\n",
374 addr, ret);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300375 return ret;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300376 }
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500377
378 dev_warn(&dev->intf->dev,
379 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
380 len, addr, ret);
381 return -EIO;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300382 }
383 /* Check success */
384 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
385 /*
386 * NOTE: the only error we've seen so far is
387 * 0x01 when the slave device is not present
388 */
389 if (!ret)
390 return len;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500391
392 if (ret > 0) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200393 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300394 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300395 }
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300396
397 return ret;
398 /*
399 * NOTE: With chip types (other chip IDs) which actually don't support
400 * this operation, it seems to succeed ALWAYS ! (even if there is no
401 * slave device or even no second i2c bus provided)
402 */
403}
404
405/*
406 * em25xx_bus_B_recv_bytes
407 * read bytes from the i2c device
408 */
409static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
410 u16 len)
411{
412 int ret;
413
414 if (len < 1 || len > 64)
415 return -EOPNOTSUPP;
416 /*
417 * NOTE: limited by the USB ctrl message constraints
418 * Zero length reads always succeed, even if no device is connected
419 */
420
421 /* Read value */
422 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
423 if (ret < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200424 dev_warn(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200425 "reading from i2c device at 0x%x failed (error=%i)\n",
426 addr, ret);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300427 return ret;
428 }
429 /*
430 * NOTE: some devices with two i2c busses have the bad habit to return 0
431 * bytes if we are on bus B AND there was no write attempt to the
432 * specified slave address before AND no device is present at the
433 * requested slave address.
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300434 * Anyway, the next check will fail with -ENXIO in this case, so avoid
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300435 * spamming the system log on device probing and do nothing here.
436 */
437
438 /* Check success */
439 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
440 /*
441 * NOTE: the only error we've seen so far is
442 * 0x01 when the slave device is not present
443 */
444 if (!ret)
445 return len;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500446
447 if (ret > 0) {
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200448 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300449 return -ENXIO;
Mauro Carvalho Chehabd845fb32014-01-06 09:17:53 -0300450 }
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300451
452 return ret;
453 /*
454 * NOTE: With chip types (other chip IDs) which actually don't support
455 * this operation, it seems to succeed ALWAYS ! (even if there is no
456 * slave device or even no second i2c bus provided)
457 */
458}
459
460/*
461 * em25xx_bus_B_check_for_device()
462 * check if there is a i2c device at the supplied address
463 */
464static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
465{
466 u8 buf;
467 int ret;
468
469 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
470 if (ret < 0)
471 return ret;
472
473 return 0;
474 /*
475 * NOTE: With chips which do not support this operation,
476 * it seems to succeed ALWAYS ! (even if no device connected)
477 */
478}
479
480static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
481{
482 struct em28xx *dev = i2c_bus->dev;
483 int rc = -EOPNOTSUPP;
484
485 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
486 rc = em28xx_i2c_check_for_device(dev, addr);
487 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
488 rc = em2800_i2c_check_for_device(dev, addr);
489 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
490 rc = em25xx_bus_B_check_for_device(dev, addr);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300491 return rc;
492}
493
494static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
495 struct i2c_msg msg)
496{
497 struct em28xx *dev = i2c_bus->dev;
498 u16 addr = msg.addr << 1;
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300499 int rc = -EOPNOTSUPP;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300500
501 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
502 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
503 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
504 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
505 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
506 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300507 return rc;
508}
509
510static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
511 struct i2c_msg msg, int stop)
512{
513 struct em28xx *dev = i2c_bus->dev;
514 u16 addr = msg.addr << 1;
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300515 int rc = -EOPNOTSUPP;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300516
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300517 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
518 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
519 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
520 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
521 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
522 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
523 return rc;
524}
525
526/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800527 * em28xx_i2c_xfer()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528 * the main i2c transfer function
529 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800530static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800531 struct i2c_msg msgs[], int num)
532{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300533 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
534 struct em28xx *dev = i2c_bus->dev;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500535 unsigned int bus = i2c_bus->bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300536 int addr, rc, i;
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300537 u8 reg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800538
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500539 /*
540 * prevent i2c xfer attempts after device is disconnected
541 * some fe's try to do i2c writes/reads from their release
542 * interfaces when called in disconnect path
543 */
Shuah Khancc5c5d22014-07-11 18:25:55 -0300544 if (dev->disconnected)
545 return -ENODEV;
546
Dan Carpentere44c1532016-05-09 05:22:55 -0300547 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
548 return -EAGAIN;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300549
550 /* Switch I2C bus if needed */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300551 if (bus != dev->cur_i2c_bus &&
552 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300553 if (bus == 1)
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300554 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300555 else
Mauro Carvalho Chehab3190fbe2013-03-21 06:03:27 -0300556 reg = 0;
557 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
558 EM2874_I2C_SECONDARY_BUS_SELECT);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300559 dev->cur_i2c_bus = bus;
560 }
561
562 if (num <= 0) {
563 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800564 return 0;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300565 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800566 for (i = 0; i < num; i++) {
567 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabe63b0092014-01-04 05:42:11 -0300568 if (!msgs[i].len) {
569 /*
570 * no len: check only for device presence
571 * This code is only called during device probe.
572 */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300573 rc = i2c_check_for_device(i2c_bus, addr);
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200574
575 if (rc == -ENXIO)
576 rc = -ENODEV;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800577 } else if (msgs[i].flags & I2C_M_RD) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800578 /* read bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300579 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800580 } else {
581 /* write bytes */
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300582 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800583 }
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200584
585 if (rc < 0)
586 goto error;
587
588 dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
589 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
590 i == num - 1 ? "stop" : "nonstop",
591 addr, msgs[i].len,
592 msgs[i].len, msgs[i].buf);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800593 }
594
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300595 rt_mutex_unlock(&dev->i2c_bus_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800596 return num;
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200597
598error:
599 dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
600 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
601 i == num - 1 ? "stop" : "nonstop",
602 addr, msgs[i].len,
603 (rc == -ENODEV) ? "no device " : "",
604 rc);
605
606 rt_mutex_unlock(&dev->i2c_bus_lock);
607 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800608}
609
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300610/*
611 * based on linux/sunrpc/svcauth.h and linux/hash.h
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300612 * The original hash function returns a different value, if arch is x86_64
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300613 * or i386.
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300614 */
615static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
616{
617 unsigned long hash = 0;
618 unsigned long l = 0;
619 int len = 0;
620 unsigned char c;
Mauro Carvalho Chehabfdf1bc92014-11-28 08:34:15 -0300621
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300622 do {
623 if (len == length) {
624 c = (char)len;
625 len = -1;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500626 } else {
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300627 c = *buf++;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500628 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300629 l = (l << 8) | c;
630 len++;
631 if ((len & (32 / 8 - 1)) == 0)
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500632 hash = ((hash ^ l) * 0x9e370001UL);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -0300633 } while (len);
634
635 return (hash >> (32 - bits)) & 0xffffffffUL;
636}
637
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300638/*
639 * Helper function to read data blocks from i2c clients with 8 or 16 bit
640 * address width, 8 bit register width and auto incrementation been activated
641 */
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500642static int em28xx_i2c_read_block(struct em28xx *dev, unsigned int bus, u16 addr,
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300643 bool addr_w16, u16 len, u8 *data)
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300644{
645 int remain = len, rsize, rsize_max, ret;
646 u8 buf[2];
647
648 /* Sanity check */
649 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
650 return -EINVAL;
651 /* Select address */
652 buf[0] = addr >> 8;
653 buf[1] = addr & 0xff;
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500654 ret = i2c_master_send(&dev->i2c_client[bus],
655 buf + !addr_w16, 1 + addr_w16);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300656 if (ret < 0)
657 return ret;
658 /* Read data */
659 if (dev->board.is_em2800)
660 rsize_max = 4;
661 else
662 rsize_max = 64;
663 while (remain > 0) {
664 if (remain > rsize_max)
665 rsize = rsize_max;
666 else
667 rsize = remain;
668
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300669 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300670 if (ret < 0)
671 return ret;
672
673 remain -= rsize;
674 data += rsize;
675 }
676
677 return len;
678}
679
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500680static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned int bus,
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300681 u8 **eedata, u16 *eedata_len)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800682{
Frank Schaefer510e8842013-03-03 15:37:43 -0300683 const u16 len = 256;
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300684 /*
685 * FIXME common length/size for bytes to read, to display, hash
Frank Schaefer510e8842013-03-03 15:37:43 -0300686 * calculation and returned device dataset. Simplifies the code a lot,
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300687 * but we might have to deal with multiple sizes in the future !
688 */
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300689 int err;
Frank Schaefer510e8842013-03-03 15:37:43 -0300690 struct em28xx_eeprom *dev_config;
691 u8 buf, *data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800692
Frank Schaefera2179682013-03-03 15:37:42 -0300693 *eedata = NULL;
Frank Schaefer510e8842013-03-03 15:37:43 -0300694 *eedata_len = 0;
Frank Schaefera2179682013-03-03 15:37:42 -0300695
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300696 /* EEPROM is always on i2c bus 0 on all known devices. */
697
698 dev->i2c_client[bus].addr = 0xa0 >> 1;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800699
700 /* Check if board has eeprom */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300701 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300702 if (err < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200703 dev_info(&dev->intf->dev, "board has no eeprom\n");
Mauro Carvalho Chehabc41109f2008-11-15 23:44:14 -0300704 return -ENODEV;
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300705 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -0800706
Frank Schaefera2179682013-03-03 15:37:42 -0300707 data = kzalloc(len, GFP_KERNEL);
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500708 if (!data)
Frank Schaefera2179682013-03-03 15:37:42 -0300709 return -ENOMEM;
710
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300711 /* Read EEPROM content */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300712 err = em28xx_i2c_read_block(dev, bus, 0x0000,
713 dev->eeprom_addrwidth_16bit,
Frank Schaefera2179682013-03-03 15:37:42 -0300714 len, data);
Frank Schaeferd832c5b2013-03-03 15:37:41 -0300715 if (err != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200716 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200717 "failed to read eeprom (err=%d)\n", err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300718 goto error;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800719 }
Frank Schaefer90271962013-01-03 14:27:06 -0300720
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300721 if (i2c_debug) {
722 /* Display eeprom content */
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200723 print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
Mauro Carvalho Chehab50f0a9d2013-12-28 08:23:30 -0300724 16, 1, data, len, true);
725
726 if (dev->eeprom_addrwidth_16bit)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200727 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200728 "eeprom %06x: ... (skipped)\n", 256);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800729 }
730
Frank Schaefer87b52432013-03-03 15:37:40 -0300731 if (dev->eeprom_addrwidth_16bit &&
Frank Schaefera2179682013-03-03 15:37:42 -0300732 data[0] == 0x26 && data[3] == 0x00) {
Frank Schaefer87b52432013-03-03 15:37:40 -0300733 /* new eeprom format; size 4-64kb */
Frank Schaefer510e8842013-03-03 15:37:43 -0300734 u16 mc_start;
735 u16 hwconf_offset;
736
Frank Schaefera2179682013-03-03 15:37:42 -0300737 dev->hash = em28xx_hash_mem(data, len, 32);
Frank Schaefer510e8842013-03-03 15:37:43 -0300738 mc_start = (data[1] << 8) + 4; /* usually 0x0004 */
739
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200740 dev_info(&dev->intf->dev,
Antonio Cardace290ef7d2018-02-09 09:13:26 -0500741 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
742 data, dev->hash);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200743 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200744 "EEPROM info:\n");
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200745 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200746 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
747 mc_start, data[2]);
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300748 /*
749 * boot configuration (address 0x0002):
Frank Schaefer87b52432013-03-03 15:37:40 -0300750 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
751 * [1] always selects 12 kb RAM
752 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
753 * [4] 1 = force fast mode and no suspend for device testing
754 * [5:7] USB PHY tuning registers; determined by device
755 * characterization
756 */
757
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300758 /*
759 * Read hardware config dataset offset from address
760 * (microcode start + 46)
761 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300762 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
763 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300764 if (err != 2) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200765 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200766 "failed to read hardware configuration data from eeprom (err=%d)\n",
767 err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300768 goto error;
769 }
Frank Schaefer87b52432013-03-03 15:37:40 -0300770
Frank Schaefer510e8842013-03-03 15:37:43 -0300771 /* Calculate hardware config dataset start address */
772 hwconf_offset = mc_start + data[0] + (data[1] << 8);
773
774 /* Read hardware config dataset */
Frank Schaeferfa74aca2013-03-24 17:09:59 -0300775 /*
776 * NOTE: the microcode copy can be multiple pages long, but
Frank Schaefer510e8842013-03-03 15:37:43 -0300777 * we assume the hardware config dataset is the same as in
778 * the old eeprom and not longer than 256 bytes.
779 * tveeprom is currently also limited to 256 bytes.
780 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300781 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
782 data);
Frank Schaefer510e8842013-03-03 15:37:43 -0300783 if (err != len) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200784 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200785 "failed to read hardware configuration data from eeprom (err=%d)\n",
786 err);
Frank Schaefer510e8842013-03-03 15:37:43 -0300787 goto error;
788 }
789
790 /* Verify hardware config dataset */
791 /* NOTE: not all devices provide this type of dataset */
792 if (data[0] != 0x1a || data[1] != 0xeb ||
793 data[2] != 0x67 || data[3] != 0x95) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200794 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200795 "\tno hardware configuration dataset found in eeprom\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300796 kfree(data);
797 return 0;
798 }
799
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500800 /*
801 * TODO: decrypt eeprom data for camera bridges
802 * (em25xx, em276x+)
803 */
Frank Schaefer510e8842013-03-03 15:37:43 -0300804
805 } else if (!dev->eeprom_addrwidth_16bit &&
806 data[0] == 0x1a && data[1] == 0xeb &&
807 data[2] == 0x67 && data[3] == 0x95) {
808 dev->hash = em28xx_hash_mem(data, len, 32);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200809 dev_info(&dev->intf->dev,
Antonio Cardace290ef7d2018-02-09 09:13:26 -0500810 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
811 data, dev->hash);
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200812 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200813 "EEPROM info:\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300814 } else {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200815 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200816 "unknown eeprom format or eeprom corrupted !\n");
Frank Schaefer510e8842013-03-03 15:37:43 -0300817 err = -ENODEV;
818 goto error;
Frank Schaeferf55eacb2013-03-03 15:37:37 -0300819 }
820
Frank Schaefera2179682013-03-03 15:37:42 -0300821 *eedata = data;
Frank Schaefer510e8842013-03-03 15:37:43 -0300822 *eedata_len = len;
Alban Browaeys32bf7c62013-07-16 18:57:53 -0300823 dev_config = (void *)*eedata;
Frank Schaefera2179682013-03-03 15:37:42 -0300824
Frank Schaefer510e8842013-03-03 15:37:43 -0300825 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800826 case 0:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200827 dev_info(&dev->intf->dev, "\tNo audio on board.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800828 break;
829 case 1:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200830 dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800831 break;
832 case 2:
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300833 if (dev->chip_id < CHIP_ID_EM2860)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200834 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200835 "\tI2S audio, sample rate=32k\n");
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300836 else
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200837 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200838 "\tI2S audio, 3 sample rates\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800839 break;
840 case 3:
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300841 if (dev->chip_id < CHIP_ID_EM2860)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200842 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200843 "\tI2S audio, 3 sample rates\n");
Frank Schaefer687ff8b2013-12-22 11:17:46 -0300844 else
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200845 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200846 "\tI2S audio, 5 sample rates\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800847 break;
848 }
849
Frank Schaefer510e8842013-03-03 15:37:43 -0300850 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200851 dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800852
Frank Schaefer510e8842013-03-03 15:37:43 -0300853 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200854 dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800855
Frank Schaefer510e8842013-03-03 15:37:43 -0300856 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800857 case 0:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200858 dev_info(&dev->intf->dev, "\t500mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800859 break;
860 case 1:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200861 dev_info(&dev->intf->dev, "\t400mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800862 break;
863 case 2:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200864 dev_info(&dev->intf->dev, "\t300mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800865 break;
866 case 3:
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200867 dev_info(&dev->intf->dev, "\t200mA max power\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800868 break;
869 }
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200870 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200871 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
872 dev_config->string_idx_table,
873 le16_to_cpu(dev_config->string1),
874 le16_to_cpu(dev_config->string2),
875 le16_to_cpu(dev_config->string3));
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800876
877 return 0;
Frank Schaefer510e8842013-03-03 15:37:43 -0300878
879error:
880 kfree(data);
881 return err;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800882}
883
884/* ----------------------------------------------------------- */
885
886/*
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800887 * functionality()
888 */
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300889static u32 functionality(struct i2c_adapter *i2c_adap)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800890{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300891 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300892
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500893 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX ||
894 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) {
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300895 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
896 } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) {
897 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
898 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
899 }
900
901 WARN(1, "Unknown i2c bus algorithm.\n");
902 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800903}
904
Julia Lawall78f2c502016-08-29 10:12:01 -0300905static const struct i2c_algorithm em28xx_algo = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800906 .master_xfer = em28xx_i2c_xfer,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800907 .functionality = functionality,
908};
909
Bhumika Goyal68438682017-08-19 06:34:15 -0400910static const struct i2c_adapter em28xx_adap_template = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800911 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800912 .name = "em28xx",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800913 .algo = &em28xx_algo,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800914};
915
Bhumika Goyal2b832472017-08-28 03:21:35 -0400916static const struct i2c_client em28xx_client_template = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800917 .name = "em28xx internal",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800918};
919
920/* ----------------------------------------------------------- */
921
922/*
923 * i2c_devs
924 * incomplete list of known devices
925 */
926static char *i2c_devs[128] = {
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500927 [0x1c >> 1] = "lgdt330x",
Frank Schaefer0b3966e2013-01-13 10:15:08 -0300928 [0x3e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800929 [0x4a >> 1] = "saa7113h",
Martin Blumenstingl729841e2012-06-12 18:19:27 -0300930 [0x52 >> 1] = "drxk",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800931 [0x60 >> 1] = "remote IR sensor",
Markus Rechbergerda45a2a2005-11-08 21:37:31 -0800932 [0x8e >> 1] = "remote IR sensor",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800933 [0x86 >> 1] = "tda9887",
934 [0x80 >> 1] = "msp34xx",
935 [0x88 >> 1] = "msp34xx",
936 [0xa0 >> 1] = "eeprom",
Vitaly Wool2bd1d9eb2009-03-04 08:27:52 -0300937 [0xb0 >> 1] = "tda9874",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800938 [0xb8 >> 1] = "tvp5150a",
Mauro Carvalho Chehab791a08f2009-07-03 15:36:18 -0300939 [0xba >> 1] = "webcam sensor or tvp5150a",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800940 [0xc0 >> 1] = "tuner (analog)",
941 [0xc2 >> 1] = "tuner (analog)",
942 [0xc4 >> 1] = "tuner (analog)",
943 [0xc6 >> 1] = "tuner (analog)",
944};
945
946/*
947 * do_i2c_scan()
948 * check i2c address range for devices
949 */
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500950void em28xx_do_i2c_scan(struct em28xx *dev, unsigned int bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800951{
Sascha Sommerfad7b952007-11-04 08:06:48 -0300952 u8 i2c_devicelist[128];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800953 unsigned char buf;
954 int i, rc;
955
Sascha Sommerfad7b952007-11-04 08:06:48 -0300956 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
957
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -0300958 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300959 dev->i2c_client[bus].addr = i;
960 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800961 if (rc < 0)
962 continue;
Sascha Sommerfad7b952007-11-04 08:06:48 -0300963 i2c_devicelist[i] = i;
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200964 dev_info(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -0200965 "found i2c device @ 0x%x on bus %d [%s]\n",
966 i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800967 }
Sascha Sommerfad7b952007-11-04 08:06:48 -0300968
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300969 if (bus == dev->def_i2c_bus)
970 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
971 ARRAY_SIZE(i2c_devicelist), 32);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800972}
973
974/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800975 * em28xx_i2c_register()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800976 * register i2c bus
977 */
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500978int em28xx_i2c_register(struct em28xx *dev, unsigned int bus,
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300979 enum em28xx_i2c_algo_type algo_type)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800980{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300981 int retval;
982
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -0500983 if (WARN_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg ||
984 !dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req))
985 return -ENODEV;
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300986
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300987 if (bus >= NUM_I2C_BUSES)
988 return -ENODEV;
989
990 dev->i2c_adap[bus] = em28xx_adap_template;
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -0200991 dev->i2c_adap[bus].dev.parent = &dev->intf->dev;
992 strcpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev));
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300993
994 dev->i2c_bus[bus].bus = bus;
Frank Schaefera3ea4bf2013-03-26 13:38:36 -0300995 dev->i2c_bus[bus].algo_type = algo_type;
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300996 dev->i2c_bus[bus].dev = dev;
997 dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -0300998
999 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03001000 if (retval < 0) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -02001001 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -02001002 "%s: i2c_add_adapter failed! retval [%d]\n",
1003 __func__, retval);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03001004 return retval;
1005 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001006
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001007 dev->i2c_client[bus] = em28xx_client_template;
1008 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001009
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001010 /* Up to now, all eeproms are at bus 0 */
1011 if (!bus) {
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -05001012 retval = em28xx_i2c_eeprom(dev, bus,
1013 &dev->eedata, &dev->eedata_len);
1014 if (retval < 0 && retval != -ENODEV) {
Mauro Carvalho Chehab29b05e22016-12-07 13:48:10 -02001015 dev_err(&dev->intf->dev,
Mauro Carvalho Chehabce8591f2016-10-20 08:42:03 -02001016 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
1017 __func__, retval);
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001018 }
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03001019 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001020
1021 if (i2c_scan)
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001022 em28xx_do_i2c_scan(dev, bus);
Mauro Carvalho Chehabc41109f2008-11-15 23:44:14 -03001023
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001024 return 0;
1025}
1026
1027/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001028 * em28xx_i2c_unregister()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001029 * unregister i2c_bus
1030 */
Mauro Carvalho Chehab8adbc7d2018-03-03 14:15:57 -05001031int em28xx_i2c_unregister(struct em28xx *dev, unsigned int bus)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001032{
Mauro Carvalho Chehabaab31252013-03-05 06:55:28 -03001033 if (bus >= NUM_I2C_BUSES)
1034 return -ENODEV;
1035
1036 i2c_del_adapter(&dev->i2c_adap[bus]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001037 return 0;
1038}