1 // SPDX-License-Identifier: GPL-2.0+
3 // em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
5 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 // Markus Rechberger <mrechberger@gmail.com>
7 // Mauro Carvalho Chehab <mchehab@kernel.org>
8 // Sascha Sommer <saschasommer@freenet.de>
9 // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
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.
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.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
27 #include <linux/jiffies.h>
29 #include "tuner-xc2028.h"
30 #include <media/v4l2-common.h>
31 #include <media/tuner.h>
33 /* ----------------------------------------------------------- */
35 static unsigned int i2c_scan
;
36 module_param(i2c_scan
, int, 0444);
37 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
39 static unsigned int i2c_debug
;
40 module_param(i2c_debug
, int, 0644);
41 MODULE_PARM_DESC(i2c_debug
, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
43 #define dprintk(level, fmt, arg...) do { \
44 if (i2c_debug > level) \
45 dev_printk(KERN_DEBUG, &dev->intf->dev, \
46 "i2c: %s: " fmt, __func__, ## arg); \
50 * 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.
55 * Ok, I2C doesn't specify any limit. So, eventually, we may need
56 * to increase this timeout.
58 #define EM28XX_I2C_XFER_TIMEOUT 35 /* ms */
60 static int em28xx_i2c_timeout(struct em28xx
*dev
)
62 int time
= EM28XX_I2C_XFER_TIMEOUT
;
64 switch (dev
->i2c_speed
& 0x03) {
65 case EM28XX_I2C_FREQ_25_KHZ
:
66 time
+= 4; /* Assume 4 ms for transfers */
68 case EM28XX_I2C_FREQ_100_KHZ
:
69 case EM28XX_I2C_FREQ_400_KHZ
:
70 time
+= 1; /* Assume 1 ms for transfers */
72 default: /* EM28XX_I2C_FREQ_1_5_MHZ */
76 return msecs_to_jiffies(time
);
80 * em2800_i2c_send_bytes()
81 * send up to 4 bytes to the em2800 i2c device
83 static int em2800_i2c_send_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
85 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
89 if (len
< 1 || len
> 4)
92 b2
[5] = 0x80 + len
- 1;
103 ret
= dev
->em28xx_write_regs(dev
, 4 - len
, &b2
[4 - len
], 2 + len
);
104 if (ret
!= 2 + len
) {
105 dev_warn(&dev
->intf
->dev
,
106 "failed to trigger write to i2c address 0x%x (error=%i)\n",
108 return (ret
< 0) ? ret
: -EIO
;
110 /* wait for completion */
111 while (time_is_after_jiffies(timeout
)) {
112 ret
= dev
->em28xx_read_reg(dev
, 0x05);
113 if (ret
== 0x80 + len
- 1)
115 if (ret
== 0x94 + len
- 1) {
116 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret
);
120 dev_warn(&dev
->intf
->dev
,
121 "failed to get i2c transfer status from bridge register (error=%i)\n",
125 usleep_range(5000, 6000);
127 dprintk(0, "write to i2c device at 0x%x timed out\n", addr
);
132 * em2800_i2c_recv_bytes()
133 * read up to 4 bytes from the em2800 i2c device
135 static int em2800_i2c_recv_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
137 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
142 if (len
< 1 || len
> 4)
146 buf2
[1] = 0x84 + len
- 1;
148 ret
= dev
->em28xx_write_regs(dev
, 0x04, buf2
, 2);
150 dev_warn(&dev
->intf
->dev
,
151 "failed to trigger read from i2c address 0x%x (error=%i)\n",
153 return (ret
< 0) ? ret
: -EIO
;
156 /* wait for completion */
157 while (time_is_after_jiffies(timeout
)) {
158 ret
= dev
->em28xx_read_reg(dev
, 0x05);
159 if (ret
== 0x84 + len
- 1)
161 if (ret
== 0x94 + len
- 1) {
162 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
167 dev_warn(&dev
->intf
->dev
,
168 "failed to get i2c transfer status from bridge register (error=%i)\n",
172 usleep_range(5000, 6000);
174 if (ret
!= 0x84 + len
- 1)
175 dprintk(0, "read from i2c device at 0x%x timed out\n", addr
);
177 /* get the received message */
178 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x00, 4 - len
, buf2
, len
);
180 dev_warn(&dev
->intf
->dev
,
181 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
183 return (ret
< 0) ? ret
: -EIO
;
185 for (i
= 0; i
< len
; i
++)
186 buf
[i
] = buf2
[len
- 1 - i
];
192 * em2800_i2c_check_for_device()
193 * check if there is an i2c device at the supplied address
195 static int em2800_i2c_check_for_device(struct em28xx
*dev
, u8 addr
)
200 ret
= em2800_i2c_recv_bytes(dev
, addr
, &buf
, 1);
203 return (ret
< 0) ? ret
: -EIO
;
207 * em28xx_i2c_send_bytes()
209 static int em28xx_i2c_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
212 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
215 if (len
< 1 || len
> 64)
218 * NOTE: limited by the USB ctrl message constraints
219 * Zero length reads always succeed, even if no device is connected
222 /* Write to i2c device */
223 ret
= dev
->em28xx_write_regs_req(dev
, stop
? 2 : 3, addr
, buf
, len
);
226 dev_warn(&dev
->intf
->dev
,
227 "writing to i2c device at 0x%x failed (error=%i)\n",
231 dev_warn(&dev
->intf
->dev
,
232 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
237 /* wait for completion */
238 while (time_is_after_jiffies(timeout
)) {
239 ret
= dev
->em28xx_read_reg(dev
, 0x05);
240 if (ret
== 0) /* success */
243 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
248 dev_warn(&dev
->intf
->dev
,
249 "failed to get i2c transfer status from bridge register (error=%i)\n",
253 usleep_range(5000, 6000);
255 * NOTE: do we really have to wait for success ?
256 * Never seen anything else than 0x00 or 0x10
257 * (even with high payload) ...
261 if (ret
== 0x02 || ret
== 0x04) {
262 /* NOTE: these errors seem to be related to clock stretching */
264 "write to i2c device at 0x%x timed out (status=%i)\n",
269 dev_warn(&dev
->intf
->dev
,
270 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
276 * em28xx_i2c_recv_bytes()
277 * read a byte from the i2c device
279 static int em28xx_i2c_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
, u16 len
)
283 if (len
< 1 || len
> 64)
286 * NOTE: limited by the USB ctrl message constraints
287 * Zero length reads always succeed, even if no device is connected
290 /* Read data from i2c device */
291 ret
= dev
->em28xx_read_reg_req_len(dev
, 2, addr
, buf
, len
);
293 dev_warn(&dev
->intf
->dev
,
294 "reading from i2c device at 0x%x failed (error=%i)\n",
299 * NOTE: some devices with two i2c busses have the bad habit to return 0
300 * 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.
303 * Anyway, the next check will fail with -ENXIO in this case, so avoid
304 * spamming the system log on device probing and do nothing here.
307 /* Check success of the i2c operation */
308 ret
= dev
->em28xx_read_reg(dev
, 0x05);
309 if (ret
== 0) /* success */
312 dev_warn(&dev
->intf
->dev
,
313 "failed to get i2c transfer status from bridge register (error=%i)\n",
318 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
323 if (ret
== 0x02 || ret
== 0x04) {
324 /* NOTE: these errors seem to be related to clock stretching */
326 "write to i2c device at 0x%x timed out (status=%i)\n",
331 dev_warn(&dev
->intf
->dev
,
332 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
338 * em28xx_i2c_check_for_device()
339 * check if there is a i2c_device at the supplied address
341 static int em28xx_i2c_check_for_device(struct em28xx
*dev
, u16 addr
)
346 ret
= em28xx_i2c_recv_bytes(dev
, addr
, &buf
, 1);
349 return (ret
< 0) ? ret
: -EIO
;
353 * em25xx_bus_B_send_bytes
354 * write bytes to the i2c device
356 static int em25xx_bus_B_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
361 if (len
< 1 || len
> 64)
364 * NOTE: limited by the USB ctrl message constraints
365 * Zero length reads always succeed, even if no device is connected
368 /* Set register and write value */
369 ret
= dev
->em28xx_write_regs_req(dev
, 0x06, addr
, buf
, len
);
372 dev_warn(&dev
->intf
->dev
,
373 "writing to i2c device at 0x%x failed (error=%i)\n",
378 dev_warn(&dev
->intf
->dev
,
379 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
384 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
386 * NOTE: the only error we've seen so far is
387 * 0x01 when the slave device is not present
393 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
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)
406 * em25xx_bus_B_recv_bytes
407 * read bytes from the i2c device
409 static int em25xx_bus_B_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
414 if (len
< 1 || len
> 64)
417 * NOTE: limited by the USB ctrl message constraints
418 * Zero length reads always succeed, even if no device is connected
422 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x06, addr
, buf
, len
);
424 dev_warn(&dev
->intf
->dev
,
425 "reading from i2c device at 0x%x failed (error=%i)\n",
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.
434 * Anyway, the next check will fail with -ENXIO in this case, so avoid
435 * spamming the system log on device probing and do nothing here.
439 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
441 * NOTE: the only error we've seen so far is
442 * 0x01 when the slave device is not present
448 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
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)
461 * em25xx_bus_B_check_for_device()
462 * check if there is a i2c device at the supplied address
464 static int em25xx_bus_B_check_for_device(struct em28xx
*dev
, u16 addr
)
469 ret
= em25xx_bus_B_recv_bytes(dev
, addr
, &buf
, 1);
475 * NOTE: With chips which do not support this operation,
476 * it seems to succeed ALWAYS ! (even if no device connected)
480 static inline int i2c_check_for_device(struct em28xx_i2c_bus
*i2c_bus
, u16 addr
)
482 struct em28xx
*dev
= i2c_bus
->dev
;
483 int rc
= -EOPNOTSUPP
;
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
);
494 static inline int i2c_recv_bytes(struct em28xx_i2c_bus
*i2c_bus
,
497 struct em28xx
*dev
= i2c_bus
->dev
;
498 u16 addr
= msg
.addr
<< 1;
499 int rc
= -EOPNOTSUPP
;
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
);
510 static inline int i2c_send_bytes(struct em28xx_i2c_bus
*i2c_bus
,
511 struct i2c_msg msg
, int stop
)
513 struct em28xx
*dev
= i2c_bus
->dev
;
514 u16 addr
= msg
.addr
<< 1;
515 int rc
= -EOPNOTSUPP
;
517 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
);
528 * the main i2c transfer function
530 static int em28xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
531 struct i2c_msg msgs
[], int num
)
533 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
534 struct em28xx
*dev
= i2c_bus
->dev
;
535 unsigned int bus
= i2c_bus
->bus
;
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
544 if (dev
->disconnected
)
547 if (!rt_mutex_trylock(&dev
->i2c_bus_lock
))
550 /* Switch I2C bus if needed */
551 if (bus
!= dev
->cur_i2c_bus
&&
552 i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
) {
554 reg
= EM2874_I2C_SECONDARY_BUS_SELECT
;
557 em28xx_write_reg_bits(dev
, EM28XX_R06_I2C_CLK
, reg
,
558 EM2874_I2C_SECONDARY_BUS_SELECT
);
559 dev
->cur_i2c_bus
= bus
;
562 for (i
= 0; i
< num
; i
++) {
563 addr
= msgs
[i
].addr
<< 1;
566 * no len: check only for device presence
567 * This code is only called during device probe.
569 rc
= i2c_check_for_device(i2c_bus
, addr
);
573 } else if (msgs
[i
].flags
& I2C_M_RD
) {
575 rc
= i2c_recv_bytes(i2c_bus
, msgs
[i
]);
578 rc
= i2c_send_bytes(i2c_bus
, msgs
[i
], i
== num
- 1);
584 dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
585 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
586 i
== num
- 1 ? "stop" : "nonstop",
588 msgs
[i
].len
, msgs
[i
].buf
);
591 rt_mutex_unlock(&dev
->i2c_bus_lock
);
595 dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
596 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
597 i
== num
- 1 ? "stop" : "nonstop",
599 (rc
== -ENODEV
) ? "no device " : "",
602 rt_mutex_unlock(&dev
->i2c_bus_lock
);
607 * based on linux/sunrpc/svcauth.h and linux/hash.h
608 * The original hash function returns a different value, if arch is x86_64
611 static inline unsigned long em28xx_hash_mem(char *buf
, int length
, int bits
)
613 unsigned long hash
= 0;
627 if ((len
& (32 / 8 - 1)) == 0)
628 hash
= ((hash
^ l
) * 0x9e370001UL
);
631 return (hash
>> (32 - bits
)) & 0xffffffffUL
;
635 * Helper function to read data blocks from i2c clients with 8 or 16 bit
636 * address width, 8 bit register width and auto incrementation been activated
638 static int em28xx_i2c_read_block(struct em28xx
*dev
, unsigned int bus
, u16 addr
,
639 bool addr_w16
, u16 len
, u8
*data
)
641 int remain
= len
, rsize
, rsize_max
, ret
;
645 if (addr
+ remain
> (addr_w16
* 0xff00 + 0xff + 1))
649 buf
[1] = addr
& 0xff;
650 ret
= i2c_master_send(&dev
->i2c_client
[bus
],
651 buf
+ !addr_w16
, 1 + addr_w16
);
655 if (dev
->board
.is_em2800
)
660 if (remain
> rsize_max
)
665 ret
= i2c_master_recv(&dev
->i2c_client
[bus
], data
, rsize
);
676 static int em28xx_i2c_eeprom(struct em28xx
*dev
, unsigned int bus
,
677 u8
**eedata
, u16
*eedata_len
)
681 * FIXME common length/size for bytes to read, to display, hash
682 * calculation and returned device dataset. Simplifies the code a lot,
683 * but we might have to deal with multiple sizes in the future !
686 struct em28xx_eeprom
*dev_config
;
692 /* EEPROM is always on i2c bus 0 on all known devices. */
694 dev
->i2c_client
[bus
].addr
= 0xa0 >> 1;
696 /* Check if board has eeprom */
697 err
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
699 dev_info(&dev
->intf
->dev
, "board has no eeprom\n");
703 data
= kzalloc(len
, GFP_KERNEL
);
707 /* Read EEPROM content */
708 err
= em28xx_i2c_read_block(dev
, bus
, 0x0000,
709 dev
->eeprom_addrwidth_16bit
,
712 dev_err(&dev
->intf
->dev
,
713 "failed to read eeprom (err=%d)\n", err
);
718 /* Display eeprom content */
719 print_hex_dump(KERN_DEBUG
, "em28xx eeprom ", DUMP_PREFIX_OFFSET
,
720 16, 1, data
, len
, true);
722 if (dev
->eeprom_addrwidth_16bit
)
723 dev_info(&dev
->intf
->dev
,
724 "eeprom %06x: ... (skipped)\n", 256);
727 if (dev
->eeprom_addrwidth_16bit
&&
728 data
[0] == 0x26 && data
[3] == 0x00) {
729 /* new eeprom format; size 4-64kb */
733 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
734 mc_start
= (data
[1] << 8) + 4; /* usually 0x0004 */
736 dev_info(&dev
->intf
->dev
,
737 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
739 dev_info(&dev
->intf
->dev
,
741 dev_info(&dev
->intf
->dev
,
742 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
745 * boot configuration (address 0x0002):
746 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
747 * [1] always selects 12 kb RAM
748 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
749 * [4] 1 = force fast mode and no suspend for device testing
750 * [5:7] USB PHY tuning registers; determined by device
755 * Read hardware config dataset offset from address
756 * (microcode start + 46)
758 err
= em28xx_i2c_read_block(dev
, bus
, mc_start
+ 46, 1, 2,
761 dev_err(&dev
->intf
->dev
,
762 "failed to read hardware configuration data from eeprom (err=%d)\n",
767 /* Calculate hardware config dataset start address */
768 hwconf_offset
= mc_start
+ data
[0] + (data
[1] << 8);
770 /* Read hardware config dataset */
772 * NOTE: the microcode copy can be multiple pages long, but
773 * we assume the hardware config dataset is the same as in
774 * the old eeprom and not longer than 256 bytes.
775 * tveeprom is currently also limited to 256 bytes.
777 err
= em28xx_i2c_read_block(dev
, bus
, hwconf_offset
, 1, len
,
780 dev_err(&dev
->intf
->dev
,
781 "failed to read hardware configuration data from eeprom (err=%d)\n",
786 /* Verify hardware config dataset */
787 /* NOTE: not all devices provide this type of dataset */
788 if (data
[0] != 0x1a || data
[1] != 0xeb ||
789 data
[2] != 0x67 || data
[3] != 0x95) {
790 dev_info(&dev
->intf
->dev
,
791 "\tno hardware configuration dataset found in eeprom\n");
797 * TODO: decrypt eeprom data for camera bridges
801 } else if (!dev
->eeprom_addrwidth_16bit
&&
802 data
[0] == 0x1a && data
[1] == 0xeb &&
803 data
[2] == 0x67 && data
[3] == 0x95) {
804 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
805 dev_info(&dev
->intf
->dev
,
806 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
808 dev_info(&dev
->intf
->dev
,
811 dev_info(&dev
->intf
->dev
,
812 "unknown eeprom format or eeprom corrupted !\n");
819 dev_config
= (void *)*eedata
;
821 switch (le16_to_cpu(dev_config
->chip_conf
) >> 4 & 0x3) {
823 dev_info(&dev
->intf
->dev
, "\tNo audio on board.\n");
826 dev_info(&dev
->intf
->dev
, "\tAC97 audio (5 sample rates)\n");
829 if (dev
->chip_id
< CHIP_ID_EM2860
)
830 dev_info(&dev
->intf
->dev
,
831 "\tI2S audio, sample rate=32k\n");
833 dev_info(&dev
->intf
->dev
,
834 "\tI2S audio, 3 sample rates\n");
837 if (dev
->chip_id
< CHIP_ID_EM2860
)
838 dev_info(&dev
->intf
->dev
,
839 "\tI2S audio, 3 sample rates\n");
841 dev_info(&dev
->intf
->dev
,
842 "\tI2S audio, 5 sample rates\n");
846 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 3)
847 dev_info(&dev
->intf
->dev
, "\tUSB Remote wakeup capable\n");
849 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 2)
850 dev_info(&dev
->intf
->dev
, "\tUSB Self power capable\n");
852 switch (le16_to_cpu(dev_config
->chip_conf
) & 0x3) {
854 dev_info(&dev
->intf
->dev
, "\t500mA max power\n");
857 dev_info(&dev
->intf
->dev
, "\t400mA max power\n");
860 dev_info(&dev
->intf
->dev
, "\t300mA max power\n");
863 dev_info(&dev
->intf
->dev
, "\t200mA max power\n");
866 dev_info(&dev
->intf
->dev
,
867 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
868 dev_config
->string_idx_table
,
869 le16_to_cpu(dev_config
->string1
),
870 le16_to_cpu(dev_config
->string2
),
871 le16_to_cpu(dev_config
->string3
));
880 /* ----------------------------------------------------------- */
885 static u32
functionality(struct i2c_adapter
*i2c_adap
)
887 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
889 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
||
890 i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
) {
891 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
892 } else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
) {
893 return (I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
) &
894 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
;
897 WARN(1, "Unknown i2c bus algorithm.\n");
901 static const struct i2c_algorithm em28xx_algo
= {
902 .master_xfer
= em28xx_i2c_xfer
,
903 .functionality
= functionality
,
906 static const struct i2c_adapter em28xx_adap_template
= {
907 .owner
= THIS_MODULE
,
909 .algo
= &em28xx_algo
,
912 static const struct i2c_client em28xx_client_template
= {
913 .name
= "em28xx internal",
916 /* ----------------------------------------------------------- */
920 * incomplete list of known devices
922 static char *i2c_devs
[128] = {
923 [0x1c >> 1] = "lgdt330x",
924 [0x3e >> 1] = "remote IR sensor",
925 [0x4a >> 1] = "saa7113h",
926 [0x52 >> 1] = "drxk",
927 [0x60 >> 1] = "remote IR sensor",
928 [0x8e >> 1] = "remote IR sensor",
929 [0x86 >> 1] = "tda9887",
930 [0x80 >> 1] = "msp34xx",
931 [0x88 >> 1] = "msp34xx",
932 [0xa0 >> 1] = "eeprom",
933 [0xb0 >> 1] = "tda9874",
934 [0xb8 >> 1] = "tvp5150a",
935 [0xba >> 1] = "webcam sensor or tvp5150a",
936 [0xc0 >> 1] = "tuner (analog)",
937 [0xc2 >> 1] = "tuner (analog)",
938 [0xc4 >> 1] = "tuner (analog)",
939 [0xc6 >> 1] = "tuner (analog)",
944 * check i2c address range for devices
946 void em28xx_do_i2c_scan(struct em28xx
*dev
, unsigned int bus
)
948 u8 i2c_devicelist
[128];
952 memset(i2c_devicelist
, 0, ARRAY_SIZE(i2c_devicelist
));
954 for (i
= 0; i
< ARRAY_SIZE(i2c_devs
); i
++) {
955 dev
->i2c_client
[bus
].addr
= i
;
956 rc
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
959 i2c_devicelist
[i
] = i
;
960 dev_info(&dev
->intf
->dev
,
961 "found i2c device @ 0x%x on bus %d [%s]\n",
962 i
<< 1, bus
, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
965 if (bus
== dev
->def_i2c_bus
)
966 dev
->i2c_hash
= em28xx_hash_mem(i2c_devicelist
,
967 ARRAY_SIZE(i2c_devicelist
), 32);
971 * em28xx_i2c_register()
974 int em28xx_i2c_register(struct em28xx
*dev
, unsigned int bus
,
975 enum em28xx_i2c_algo_type algo_type
)
979 if (WARN_ON(!dev
->em28xx_write_regs
|| !dev
->em28xx_read_reg
||
980 !dev
->em28xx_write_regs_req
|| !dev
->em28xx_read_reg_req
))
983 if (bus
>= NUM_I2C_BUSES
)
986 dev
->i2c_adap
[bus
] = em28xx_adap_template
;
987 dev
->i2c_adap
[bus
].dev
.parent
= &dev
->intf
->dev
;
988 strcpy(dev
->i2c_adap
[bus
].name
, dev_name(&dev
->intf
->dev
));
990 dev
->i2c_bus
[bus
].bus
= bus
;
991 dev
->i2c_bus
[bus
].algo_type
= algo_type
;
992 dev
->i2c_bus
[bus
].dev
= dev
;
993 dev
->i2c_adap
[bus
].algo_data
= &dev
->i2c_bus
[bus
];
995 retval
= i2c_add_adapter(&dev
->i2c_adap
[bus
]);
997 dev_err(&dev
->intf
->dev
,
998 "%s: i2c_add_adapter failed! retval [%d]\n",
1003 dev
->i2c_client
[bus
] = em28xx_client_template
;
1004 dev
->i2c_client
[bus
].adapter
= &dev
->i2c_adap
[bus
];
1006 /* Up to now, all eeproms are at bus 0 */
1008 retval
= em28xx_i2c_eeprom(dev
, bus
,
1009 &dev
->eedata
, &dev
->eedata_len
);
1010 if (retval
< 0 && retval
!= -ENODEV
) {
1011 dev_err(&dev
->intf
->dev
,
1012 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
1018 em28xx_do_i2c_scan(dev
, bus
);
1024 * em28xx_i2c_unregister()
1025 * unregister i2c_bus
1027 int em28xx_i2c_unregister(struct em28xx
*dev
, unsigned int bus
)
1029 if (bus
>= NUM_I2C_BUSES
)
1032 i2c_del_adapter(&dev
->i2c_adap
[bus
]);