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