2 em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
8 Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/usb.h>
30 #include <linux/i2c.h>
31 #include <linux/jiffies.h>
33 #include "tuner-xc2028.h"
34 #include <media/v4l2-common.h>
35 #include <media/tuner.h>
37 /* ----------------------------------------------------------- */
39 static unsigned int i2c_scan
;
40 module_param(i2c_scan
, int, 0444);
41 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
43 static unsigned int i2c_debug
;
44 module_param(i2c_debug
, int, 0644);
45 MODULE_PARM_DESC(i2c_debug
, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
47 #define dprintk(level, fmt, arg...) do { \
48 if (i2c_debug > level) \
49 dev_printk(KERN_DEBUG, &dev->intf->dev, \
50 "i2c: %s: " fmt, __func__, ## arg); \
55 * em2800_i2c_send_bytes()
56 * send up to 4 bytes to the em2800 i2c device
58 static int em2800_i2c_send_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
60 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT
);
64 if (len
< 1 || len
> 4)
67 BUG_ON(len
< 1 || len
> 4);
68 b2
[5] = 0x80 + len
- 1;
79 ret
= dev
->em28xx_write_regs(dev
, 4 - len
, &b2
[4 - len
], 2 + len
);
81 dev_warn(&dev
->intf
->dev
,
82 "failed to trigger write to i2c address 0x%x (error=%i)\n",
84 return (ret
< 0) ? ret
: -EIO
;
86 /* wait for completion */
87 while (time_is_after_jiffies(timeout
)) {
88 ret
= dev
->em28xx_read_reg(dev
, 0x05);
89 if (ret
== 0x80 + len
- 1)
91 if (ret
== 0x94 + len
- 1) {
92 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret
);
96 dev_warn(&dev
->intf
->dev
,
97 "failed to get i2c transfer status from bridge register (error=%i)\n",
103 dprintk(0, "write to i2c device at 0x%x timed out\n", addr
);
108 * em2800_i2c_recv_bytes()
109 * read up to 4 bytes from the em2800 i2c device
111 static int em2800_i2c_recv_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
113 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT
);
118 if (len
< 1 || len
> 4)
122 buf2
[1] = 0x84 + len
- 1;
124 ret
= dev
->em28xx_write_regs(dev
, 0x04, buf2
, 2);
126 dev_warn(&dev
->intf
->dev
,
127 "failed to trigger read from i2c address 0x%x (error=%i)\n",
129 return (ret
< 0) ? ret
: -EIO
;
132 /* wait for completion */
133 while (time_is_after_jiffies(timeout
)) {
134 ret
= dev
->em28xx_read_reg(dev
, 0x05);
135 if (ret
== 0x84 + len
- 1)
137 if (ret
== 0x94 + len
- 1) {
138 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
143 dev_warn(&dev
->intf
->dev
,
144 "failed to get i2c transfer status from bridge register (error=%i)\n",
150 if (ret
!= 0x84 + len
- 1) {
151 dprintk(0, "read from i2c device at 0x%x timed out\n", addr
);
154 /* get the received message */
155 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x00, 4-len
, buf2
, len
);
157 dev_warn(&dev
->intf
->dev
,
158 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
160 return (ret
< 0) ? ret
: -EIO
;
162 for (i
= 0; i
< len
; i
++)
163 buf
[i
] = buf2
[len
- 1 - i
];
169 * em2800_i2c_check_for_device()
170 * check if there is an i2c device at the supplied address
172 static int em2800_i2c_check_for_device(struct em28xx
*dev
, u8 addr
)
177 ret
= em2800_i2c_recv_bytes(dev
, addr
, &buf
, 1);
180 return (ret
< 0) ? ret
: -EIO
;
184 * em28xx_i2c_send_bytes()
186 static int em28xx_i2c_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
189 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT
);
192 if (len
< 1 || len
> 64)
195 * NOTE: limited by the USB ctrl message constraints
196 * Zero length reads always succeed, even if no device is connected
199 /* Write to i2c device */
200 ret
= dev
->em28xx_write_regs_req(dev
, stop
? 2 : 3, addr
, buf
, len
);
203 dev_warn(&dev
->intf
->dev
,
204 "writing to i2c device at 0x%x failed (error=%i)\n",
208 dev_warn(&dev
->intf
->dev
,
209 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
215 /* wait for completion */
216 while (time_is_after_jiffies(timeout
)) {
217 ret
= dev
->em28xx_read_reg(dev
, 0x05);
218 if (ret
== 0) /* success */
221 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
226 dev_warn(&dev
->intf
->dev
,
227 "failed to get i2c transfer status from bridge register (error=%i)\n",
233 * NOTE: do we really have to wait for success ?
234 * Never seen anything else than 0x00 or 0x10
235 * (even with high payload) ...
239 if (ret
== 0x02 || ret
== 0x04) {
240 /* NOTE: these errors seem to be related to clock stretching */
242 "write to i2c device at 0x%x timed out (status=%i)\n",
247 dev_warn(&dev
->intf
->dev
,
248 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
254 * em28xx_i2c_recv_bytes()
255 * read a byte from the i2c device
257 static int em28xx_i2c_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
, u16 len
)
261 if (len
< 1 || len
> 64)
264 * NOTE: limited by the USB ctrl message constraints
265 * Zero length reads always succeed, even if no device is connected
268 /* Read data from i2c device */
269 ret
= dev
->em28xx_read_reg_req_len(dev
, 2, addr
, buf
, len
);
271 dev_warn(&dev
->intf
->dev
,
272 "reading from i2c device at 0x%x failed (error=%i)\n",
277 * NOTE: some devices with two i2c busses have the bad habit to return 0
278 * bytes if we are on bus B AND there was no write attempt to the
279 * specified slave address before AND no device is present at the
280 * requested slave address.
281 * Anyway, the next check will fail with -ENXIO in this case, so avoid
282 * spamming the system log on device probing and do nothing here.
285 /* Check success of the i2c operation */
286 ret
= dev
->em28xx_read_reg(dev
, 0x05);
287 if (ret
== 0) /* success */
290 dev_warn(&dev
->intf
->dev
,
291 "failed to get i2c transfer status from bridge register (error=%i)\n",
296 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
301 if (ret
== 0x02 || ret
== 0x04) {
302 /* NOTE: these errors seem to be related to clock stretching */
304 "write to i2c device at 0x%x timed out (status=%i)\n",
309 dev_warn(&dev
->intf
->dev
,
310 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
316 * em28xx_i2c_check_for_device()
317 * check if there is a i2c_device at the supplied address
319 static int em28xx_i2c_check_for_device(struct em28xx
*dev
, u16 addr
)
324 ret
= em28xx_i2c_recv_bytes(dev
, addr
, &buf
, 1);
327 return (ret
< 0) ? ret
: -EIO
;
331 * em25xx_bus_B_send_bytes
332 * write bytes to the i2c device
334 static int em25xx_bus_B_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
339 if (len
< 1 || len
> 64)
342 * NOTE: limited by the USB ctrl message constraints
343 * Zero length reads always succeed, even if no device is connected
346 /* Set register and write value */
347 ret
= dev
->em28xx_write_regs_req(dev
, 0x06, addr
, buf
, len
);
350 dev_warn(&dev
->intf
->dev
,
351 "writing to i2c device at 0x%x failed (error=%i)\n",
355 dev_warn(&dev
->intf
->dev
,
356 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
362 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
364 * NOTE: the only error we've seen so far is
365 * 0x01 when the slave device is not present
370 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
376 * NOTE: With chip types (other chip IDs) which actually don't support
377 * this operation, it seems to succeed ALWAYS ! (even if there is no
378 * slave device or even no second i2c bus provided)
383 * em25xx_bus_B_recv_bytes
384 * read bytes from the i2c device
386 static int em25xx_bus_B_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
391 if (len
< 1 || len
> 64)
394 * NOTE: limited by the USB ctrl message constraints
395 * Zero length reads always succeed, even if no device is connected
399 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x06, addr
, buf
, len
);
401 dev_warn(&dev
->intf
->dev
,
402 "reading from i2c device at 0x%x failed (error=%i)\n",
407 * NOTE: some devices with two i2c busses have the bad habit to return 0
408 * bytes if we are on bus B AND there was no write attempt to the
409 * specified slave address before AND no device is present at the
410 * requested slave address.
411 * Anyway, the next check will fail with -ENXIO in this case, so avoid
412 * spamming the system log on device probing and do nothing here.
416 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
418 * NOTE: the only error we've seen so far is
419 * 0x01 when the slave device is not present
424 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
430 * NOTE: With chip types (other chip IDs) which actually don't support
431 * this operation, it seems to succeed ALWAYS ! (even if there is no
432 * slave device or even no second i2c bus provided)
437 * em25xx_bus_B_check_for_device()
438 * check if there is a i2c device at the supplied address
440 static int em25xx_bus_B_check_for_device(struct em28xx
*dev
, u16 addr
)
445 ret
= em25xx_bus_B_recv_bytes(dev
, addr
, &buf
, 1);
451 * NOTE: With chips which do not support this operation,
452 * it seems to succeed ALWAYS ! (even if no device connected)
456 static inline int i2c_check_for_device(struct em28xx_i2c_bus
*i2c_bus
, u16 addr
)
458 struct em28xx
*dev
= i2c_bus
->dev
;
459 int rc
= -EOPNOTSUPP
;
461 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
462 rc
= em28xx_i2c_check_for_device(dev
, addr
);
463 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
464 rc
= em2800_i2c_check_for_device(dev
, addr
);
465 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
466 rc
= em25xx_bus_B_check_for_device(dev
, addr
);
470 static inline int i2c_recv_bytes(struct em28xx_i2c_bus
*i2c_bus
,
473 struct em28xx
*dev
= i2c_bus
->dev
;
474 u16 addr
= msg
.addr
<< 1;
475 int rc
= -EOPNOTSUPP
;
477 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
478 rc
= em28xx_i2c_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
479 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
480 rc
= em2800_i2c_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
481 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
482 rc
= em25xx_bus_B_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
486 static inline int i2c_send_bytes(struct em28xx_i2c_bus
*i2c_bus
,
487 struct i2c_msg msg
, int stop
)
489 struct em28xx
*dev
= i2c_bus
->dev
;
490 u16 addr
= msg
.addr
<< 1;
491 int rc
= -EOPNOTSUPP
;
493 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
494 rc
= em28xx_i2c_send_bytes(dev
, addr
, msg
.buf
, msg
.len
, stop
);
495 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
496 rc
= em2800_i2c_send_bytes(dev
, addr
, msg
.buf
, msg
.len
);
497 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
498 rc
= em25xx_bus_B_send_bytes(dev
, addr
, msg
.buf
, msg
.len
);
504 * the main i2c transfer function
506 static int em28xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
507 struct i2c_msg msgs
[], int num
)
509 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
510 struct em28xx
*dev
= i2c_bus
->dev
;
511 unsigned bus
= i2c_bus
->bus
;
515 /* prevent i2c xfer attempts after device is disconnected
516 some fe's try to do i2c writes/reads from their release
517 interfaces when called in disconnect path */
518 if (dev
->disconnected
)
521 if (!rt_mutex_trylock(&dev
->i2c_bus_lock
))
524 /* Switch I2C bus if needed */
525 if (bus
!= dev
->cur_i2c_bus
&&
526 i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
) {
528 reg
= EM2874_I2C_SECONDARY_BUS_SELECT
;
531 em28xx_write_reg_bits(dev
, EM28XX_R06_I2C_CLK
, reg
,
532 EM2874_I2C_SECONDARY_BUS_SELECT
);
533 dev
->cur_i2c_bus
= bus
;
537 rt_mutex_unlock(&dev
->i2c_bus_lock
);
540 for (i
= 0; i
< num
; i
++) {
541 addr
= msgs
[i
].addr
<< 1;
544 * no len: check only for device presence
545 * This code is only called during device probe.
547 rc
= i2c_check_for_device(i2c_bus
, addr
);
551 } else if (msgs
[i
].flags
& I2C_M_RD
) {
553 rc
= i2c_recv_bytes(i2c_bus
, msgs
[i
]);
556 rc
= i2c_send_bytes(i2c_bus
, msgs
[i
], i
== num
- 1);
562 dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
563 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
564 i
== num
- 1 ? "stop" : "nonstop",
566 msgs
[i
].len
, msgs
[i
].buf
);
569 rt_mutex_unlock(&dev
->i2c_bus_lock
);
573 dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
574 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
575 i
== num
- 1 ? "stop" : "nonstop",
577 (rc
== -ENODEV
) ? "no device " : "",
580 rt_mutex_unlock(&dev
->i2c_bus_lock
);
585 * based on linux/sunrpc/svcauth.h and linux/hash.h
586 * The original hash function returns a different value, if arch is x86_64
589 static inline unsigned long em28xx_hash_mem(char *buf
, int length
, int bits
)
591 unsigned long hash
= 0;
604 if ((len
& (32 / 8 - 1)) == 0)
605 hash
= ((hash
^l
) * 0x9e370001UL
);
608 return (hash
>> (32 - bits
)) & 0xffffffffUL
;
612 * Helper function to read data blocks from i2c clients with 8 or 16 bit
613 * address width, 8 bit register width and auto incrementation been activated
615 static int em28xx_i2c_read_block(struct em28xx
*dev
, unsigned bus
, u16 addr
,
616 bool addr_w16
, u16 len
, u8
*data
)
618 int remain
= len
, rsize
, rsize_max
, ret
;
622 if (addr
+ remain
> (addr_w16
* 0xff00 + 0xff + 1))
626 buf
[1] = addr
& 0xff;
627 ret
= i2c_master_send(&dev
->i2c_client
[bus
], buf
+ !addr_w16
, 1 + addr_w16
);
631 if (dev
->board
.is_em2800
)
636 if (remain
> rsize_max
)
641 ret
= i2c_master_recv(&dev
->i2c_client
[bus
], data
, rsize
);
652 static int em28xx_i2c_eeprom(struct em28xx
*dev
, unsigned bus
,
653 u8
**eedata
, u16
*eedata_len
)
657 * FIXME common length/size for bytes to read, to display, hash
658 * calculation and returned device dataset. Simplifies the code a lot,
659 * but we might have to deal with multiple sizes in the future !
662 struct em28xx_eeprom
*dev_config
;
668 /* EEPROM is always on i2c bus 0 on all known devices. */
670 dev
->i2c_client
[bus
].addr
= 0xa0 >> 1;
672 /* Check if board has eeprom */
673 err
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
675 dev_info(&dev
->intf
->dev
, "board has no eeprom\n");
679 data
= kzalloc(len
, GFP_KERNEL
);
683 /* Read EEPROM content */
684 err
= em28xx_i2c_read_block(dev
, bus
, 0x0000,
685 dev
->eeprom_addrwidth_16bit
,
688 dev_err(&dev
->intf
->dev
,
689 "failed to read eeprom (err=%d)\n", err
);
694 /* Display eeprom content */
695 print_hex_dump(KERN_DEBUG
, "em28xx eeprom ", DUMP_PREFIX_OFFSET
,
696 16, 1, data
, len
, true);
698 if (dev
->eeprom_addrwidth_16bit
)
699 dev_info(&dev
->intf
->dev
,
700 "eeprom %06x: ... (skipped)\n", 256);
703 if (dev
->eeprom_addrwidth_16bit
&&
704 data
[0] == 0x26 && data
[3] == 0x00) {
705 /* new eeprom format; size 4-64kb */
709 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
710 mc_start
= (data
[1] << 8) + 4; /* usually 0x0004 */
712 dev_info(&dev
->intf
->dev
,
713 "EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
714 data
[0], data
[1], data
[2], data
[3], dev
->hash
);
715 dev_info(&dev
->intf
->dev
,
717 dev_info(&dev
->intf
->dev
,
718 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
721 * boot configuration (address 0x0002):
722 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
723 * [1] always selects 12 kb RAM
724 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
725 * [4] 1 = force fast mode and no suspend for device testing
726 * [5:7] USB PHY tuning registers; determined by device
731 * Read hardware config dataset offset from address
732 * (microcode start + 46)
734 err
= em28xx_i2c_read_block(dev
, bus
, mc_start
+ 46, 1, 2,
737 dev_err(&dev
->intf
->dev
,
738 "failed to read hardware configuration data from eeprom (err=%d)\n",
743 /* Calculate hardware config dataset start address */
744 hwconf_offset
= mc_start
+ data
[0] + (data
[1] << 8);
746 /* Read hardware config dataset */
748 * NOTE: the microcode copy can be multiple pages long, but
749 * we assume the hardware config dataset is the same as in
750 * the old eeprom and not longer than 256 bytes.
751 * tveeprom is currently also limited to 256 bytes.
753 err
= em28xx_i2c_read_block(dev
, bus
, hwconf_offset
, 1, len
,
756 dev_err(&dev
->intf
->dev
,
757 "failed to read hardware configuration data from eeprom (err=%d)\n",
762 /* Verify hardware config dataset */
763 /* NOTE: not all devices provide this type of dataset */
764 if (data
[0] != 0x1a || data
[1] != 0xeb ||
765 data
[2] != 0x67 || data
[3] != 0x95) {
766 dev_info(&dev
->intf
->dev
,
767 "\tno hardware configuration dataset found in eeprom\n");
772 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
774 } else if (!dev
->eeprom_addrwidth_16bit
&&
775 data
[0] == 0x1a && data
[1] == 0xeb &&
776 data
[2] == 0x67 && data
[3] == 0x95) {
777 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
778 dev_info(&dev
->intf
->dev
,
779 "EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
780 data
[0], data
[1], data
[2], data
[3], dev
->hash
);
781 dev_info(&dev
->intf
->dev
,
784 dev_info(&dev
->intf
->dev
,
785 "unknown eeprom format or eeprom corrupted !\n");
792 dev_config
= (void *)*eedata
;
794 switch (le16_to_cpu(dev_config
->chip_conf
) >> 4 & 0x3) {
796 dev_info(&dev
->intf
->dev
, "\tNo audio on board.\n");
799 dev_info(&dev
->intf
->dev
, "\tAC97 audio (5 sample rates)\n");
802 if (dev
->chip_id
< CHIP_ID_EM2860
)
803 dev_info(&dev
->intf
->dev
,
804 "\tI2S audio, sample rate=32k\n");
806 dev_info(&dev
->intf
->dev
,
807 "\tI2S audio, 3 sample rates\n");
810 if (dev
->chip_id
< CHIP_ID_EM2860
)
811 dev_info(&dev
->intf
->dev
,
812 "\tI2S audio, 3 sample rates\n");
814 dev_info(&dev
->intf
->dev
,
815 "\tI2S audio, 5 sample rates\n");
819 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 3)
820 dev_info(&dev
->intf
->dev
, "\tUSB Remote wakeup capable\n");
822 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 2)
823 dev_info(&dev
->intf
->dev
, "\tUSB Self power capable\n");
825 switch (le16_to_cpu(dev_config
->chip_conf
) & 0x3) {
827 dev_info(&dev
->intf
->dev
, "\t500mA max power\n");
830 dev_info(&dev
->intf
->dev
, "\t400mA max power\n");
833 dev_info(&dev
->intf
->dev
, "\t300mA max power\n");
836 dev_info(&dev
->intf
->dev
, "\t200mA max power\n");
839 dev_info(&dev
->intf
->dev
,
840 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
841 dev_config
->string_idx_table
,
842 le16_to_cpu(dev_config
->string1
),
843 le16_to_cpu(dev_config
->string2
),
844 le16_to_cpu(dev_config
->string3
));
853 /* ----------------------------------------------------------- */
858 static u32
functionality(struct i2c_adapter
*i2c_adap
)
860 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
862 if ((i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
) ||
863 (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)) {
864 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
865 } else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
) {
866 return (I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
) &
867 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
;
870 WARN(1, "Unknown i2c bus algorithm.\n");
874 static const struct i2c_algorithm em28xx_algo
= {
875 .master_xfer
= em28xx_i2c_xfer
,
876 .functionality
= functionality
,
879 static const struct i2c_adapter em28xx_adap_template
= {
880 .owner
= THIS_MODULE
,
882 .algo
= &em28xx_algo
,
885 static const struct i2c_client em28xx_client_template
= {
886 .name
= "em28xx internal",
889 /* ----------------------------------------------------------- */
893 * incomplete list of known devices
895 static char *i2c_devs
[128] = {
896 [0x1c >> 1] = "lgdt330x",
897 [0x3e >> 1] = "remote IR sensor",
898 [0x4a >> 1] = "saa7113h",
899 [0x52 >> 1] = "drxk",
900 [0x60 >> 1] = "remote IR sensor",
901 [0x8e >> 1] = "remote IR sensor",
902 [0x86 >> 1] = "tda9887",
903 [0x80 >> 1] = "msp34xx",
904 [0x88 >> 1] = "msp34xx",
905 [0xa0 >> 1] = "eeprom",
906 [0xb0 >> 1] = "tda9874",
907 [0xb8 >> 1] = "tvp5150a",
908 [0xba >> 1] = "webcam sensor or tvp5150a",
909 [0xc0 >> 1] = "tuner (analog)",
910 [0xc2 >> 1] = "tuner (analog)",
911 [0xc4 >> 1] = "tuner (analog)",
912 [0xc6 >> 1] = "tuner (analog)",
917 * check i2c address range for devices
919 void em28xx_do_i2c_scan(struct em28xx
*dev
, unsigned bus
)
921 u8 i2c_devicelist
[128];
925 memset(i2c_devicelist
, 0, ARRAY_SIZE(i2c_devicelist
));
927 for (i
= 0; i
< ARRAY_SIZE(i2c_devs
); i
++) {
928 dev
->i2c_client
[bus
].addr
= i
;
929 rc
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
932 i2c_devicelist
[i
] = i
;
933 dev_info(&dev
->intf
->dev
,
934 "found i2c device @ 0x%x on bus %d [%s]\n",
935 i
<< 1, bus
, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
938 if (bus
== dev
->def_i2c_bus
)
939 dev
->i2c_hash
= em28xx_hash_mem(i2c_devicelist
,
940 ARRAY_SIZE(i2c_devicelist
), 32);
944 * em28xx_i2c_register()
947 int em28xx_i2c_register(struct em28xx
*dev
, unsigned bus
,
948 enum em28xx_i2c_algo_type algo_type
)
952 BUG_ON(!dev
->em28xx_write_regs
|| !dev
->em28xx_read_reg
);
953 BUG_ON(!dev
->em28xx_write_regs_req
|| !dev
->em28xx_read_reg_req
);
955 if (bus
>= NUM_I2C_BUSES
)
958 dev
->i2c_adap
[bus
] = em28xx_adap_template
;
959 dev
->i2c_adap
[bus
].dev
.parent
= &dev
->intf
->dev
;
960 strcpy(dev
->i2c_adap
[bus
].name
, dev_name(&dev
->intf
->dev
));
962 dev
->i2c_bus
[bus
].bus
= bus
;
963 dev
->i2c_bus
[bus
].algo_type
= algo_type
;
964 dev
->i2c_bus
[bus
].dev
= dev
;
965 dev
->i2c_adap
[bus
].algo_data
= &dev
->i2c_bus
[bus
];
967 retval
= i2c_add_adapter(&dev
->i2c_adap
[bus
]);
969 dev_err(&dev
->intf
->dev
,
970 "%s: i2c_add_adapter failed! retval [%d]\n",
975 dev
->i2c_client
[bus
] = em28xx_client_template
;
976 dev
->i2c_client
[bus
].adapter
= &dev
->i2c_adap
[bus
];
978 /* Up to now, all eeproms are at bus 0 */
980 retval
= em28xx_i2c_eeprom(dev
, bus
, &dev
->eedata
, &dev
->eedata_len
);
981 if ((retval
< 0) && (retval
!= -ENODEV
)) {
982 dev_err(&dev
->intf
->dev
,
983 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
989 em28xx_do_i2c_scan(dev
, bus
);
995 * em28xx_i2c_unregister()
998 int em28xx_i2c_unregister(struct em28xx
*dev
, unsigned bus
)
1000 if (bus
>= NUM_I2C_BUSES
)
1003 i2c_del_adapter(&dev
->i2c_adap
[bus
]);