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>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/usb.h>
16 #include <linux/i2c.h>
17 #include <linux/jiffies.h>
20 #include <media/v4l2-common.h>
21 #include <media/tuner.h>
23 /* ----------------------------------------------------------- */
25 static unsigned int i2c_scan
;
26 module_param(i2c_scan
, int, 0444);
27 MODULE_PARM_DESC(i2c_scan
, "scan i2c bus at insmod time");
29 static unsigned int i2c_debug
;
30 module_param(i2c_debug
, int, 0644);
31 MODULE_PARM_DESC(i2c_debug
, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
33 #define dprintk(level, fmt, arg...) do { \
34 if (i2c_debug > level) \
35 dev_printk(KERN_DEBUG, &dev->intf->dev, \
36 "i2c: %s: " fmt, __func__, ## arg); \
40 * Time in msecs to wait for i2c xfers to finish.
41 * 35ms is the maximum time a SMBUS device could wait when
42 * clock stretching is used. As the transfer itself will take
43 * some time to happen, set it to 35 ms.
45 * Ok, I2C doesn't specify any limit. So, eventually, we may need
46 * to increase this timeout.
48 #define EM28XX_I2C_XFER_TIMEOUT 35 /* ms */
50 static int em28xx_i2c_timeout(struct em28xx
*dev
)
52 int time
= EM28XX_I2C_XFER_TIMEOUT
;
54 switch (dev
->i2c_speed
& 0x03) {
55 case EM28XX_I2C_FREQ_25_KHZ
:
56 time
+= 4; /* Assume 4 ms for transfers */
58 case EM28XX_I2C_FREQ_100_KHZ
:
59 case EM28XX_I2C_FREQ_400_KHZ
:
60 time
+= 1; /* Assume 1 ms for transfers */
62 default: /* EM28XX_I2C_FREQ_1_5_MHZ */
66 return msecs_to_jiffies(time
);
70 * em2800_i2c_send_bytes()
71 * send up to 4 bytes to the em2800 i2c device
73 static int em2800_i2c_send_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
75 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
79 if (len
< 1 || len
> 4)
82 b2
[5] = 0x80 + len
- 1;
93 ret
= dev
->em28xx_write_regs(dev
, 4 - len
, &b2
[4 - len
], 2 + len
);
95 dev_warn(&dev
->intf
->dev
,
96 "failed to trigger write to i2c address 0x%x (error=%i)\n",
98 return (ret
< 0) ? ret
: -EIO
;
100 /* wait for completion */
101 while (time_is_after_jiffies(timeout
)) {
102 ret
= dev
->em28xx_read_reg(dev
, 0x05);
103 if (ret
== 0x80 + len
- 1)
105 if (ret
== 0x94 + len
- 1) {
106 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret
);
110 dev_warn(&dev
->intf
->dev
,
111 "failed to get i2c transfer status from bridge register (error=%i)\n",
115 usleep_range(5000, 6000);
117 dprintk(0, "write to i2c device at 0x%x timed out\n", addr
);
122 * em2800_i2c_recv_bytes()
123 * read up to 4 bytes from the em2800 i2c device
125 static int em2800_i2c_recv_bytes(struct em28xx
*dev
, u8 addr
, u8
*buf
, u16 len
)
127 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
132 if (len
< 1 || len
> 4)
136 buf2
[1] = 0x84 + len
- 1;
138 ret
= dev
->em28xx_write_regs(dev
, 0x04, buf2
, 2);
140 dev_warn(&dev
->intf
->dev
,
141 "failed to trigger read from i2c address 0x%x (error=%i)\n",
143 return (ret
< 0) ? ret
: -EIO
;
146 /* wait for completion */
147 while (time_is_after_jiffies(timeout
)) {
148 ret
= dev
->em28xx_read_reg(dev
, 0x05);
149 if (ret
== 0x84 + len
- 1)
151 if (ret
== 0x94 + len
- 1) {
152 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
157 dev_warn(&dev
->intf
->dev
,
158 "failed to get i2c transfer status from bridge register (error=%i)\n",
162 usleep_range(5000, 6000);
164 if (ret
!= 0x84 + len
- 1)
165 dprintk(0, "read from i2c device at 0x%x timed out\n", addr
);
167 /* get the received message */
168 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x00, 4 - len
, buf2
, len
);
170 dev_warn(&dev
->intf
->dev
,
171 "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
173 return (ret
< 0) ? ret
: -EIO
;
175 for (i
= 0; i
< len
; i
++)
176 buf
[i
] = buf2
[len
- 1 - i
];
182 * em2800_i2c_check_for_device()
183 * check if there is an i2c device at the supplied address
185 static int em2800_i2c_check_for_device(struct em28xx
*dev
, u8 addr
)
190 ret
= em2800_i2c_recv_bytes(dev
, addr
, &buf
, 1);
193 return (ret
< 0) ? ret
: -EIO
;
197 * em28xx_i2c_send_bytes()
199 static int em28xx_i2c_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
202 unsigned long timeout
= jiffies
+ em28xx_i2c_timeout(dev
);
205 if (len
< 1 || len
> 64)
208 * NOTE: limited by the USB ctrl message constraints
209 * Zero length reads always succeed, even if no device is connected
212 /* Write to i2c device */
213 ret
= dev
->em28xx_write_regs_req(dev
, stop
? 2 : 3, addr
, buf
, len
);
216 dev_warn(&dev
->intf
->dev
,
217 "writing to i2c device at 0x%x failed (error=%i)\n",
221 dev_warn(&dev
->intf
->dev
,
222 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
227 /* wait for completion */
228 while (time_is_after_jiffies(timeout
)) {
229 ret
= dev
->em28xx_read_reg(dev
, 0x05);
230 if (ret
== 0) /* success */
233 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
238 dev_warn(&dev
->intf
->dev
,
239 "failed to get i2c transfer status from bridge register (error=%i)\n",
243 usleep_range(5000, 6000);
245 * NOTE: do we really have to wait for success ?
246 * Never seen anything else than 0x00 or 0x10
247 * (even with high payload) ...
251 if (ret
== 0x02 || ret
== 0x04) {
252 /* NOTE: these errors seem to be related to clock stretching */
254 "write to i2c device at 0x%x timed out (status=%i)\n",
259 dev_warn(&dev
->intf
->dev
,
260 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
266 * em28xx_i2c_recv_bytes()
267 * read a byte from the i2c device
269 static int em28xx_i2c_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
, u16 len
)
273 if (len
< 1 || len
> 64)
276 * NOTE: limited by the USB ctrl message constraints
277 * Zero length reads always succeed, even if no device is connected
280 /* Read data from i2c device */
281 ret
= dev
->em28xx_read_reg_req_len(dev
, 2, addr
, buf
, len
);
283 dev_warn(&dev
->intf
->dev
,
284 "reading from i2c device at 0x%x failed (error=%i)\n",
287 } else if (ret
!= len
) {
288 dev_dbg(&dev
->intf
->dev
,
289 "%i bytes read from i2c device at 0x%x requested, but %i bytes written\n",
293 * NOTE: some devices with two i2c buses have the bad habit to return 0
294 * bytes if we are on bus B AND there was no write attempt to the
295 * specified slave address before AND no device is present at the
296 * requested slave address.
297 * Anyway, the next check will fail with -ENXIO in this case, so avoid
298 * spamming the system log on device probing and do nothing here.
301 /* Check success of the i2c operation */
302 ret
= dev
->em28xx_read_reg(dev
, 0x05);
303 if (ret
== 0) /* success */
306 dev_warn(&dev
->intf
->dev
,
307 "failed to get i2c transfer status from bridge register (error=%i)\n",
312 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
317 if (ret
== 0x02 || ret
== 0x04) {
318 /* NOTE: these errors seem to be related to clock stretching */
320 "write to i2c device at 0x%x timed out (status=%i)\n",
325 dev_warn(&dev
->intf
->dev
,
326 "read from i2c device at 0x%x failed with unknown error (status=%i)\n",
332 * em28xx_i2c_check_for_device()
333 * check if there is a i2c_device at the supplied address
335 static int em28xx_i2c_check_for_device(struct em28xx
*dev
, u16 addr
)
340 ret
= em28xx_i2c_recv_bytes(dev
, addr
, &buf
, 1);
343 return (ret
< 0) ? ret
: -EIO
;
347 * em25xx_bus_B_send_bytes
348 * write bytes to the i2c device
350 static int em25xx_bus_B_send_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
355 if (len
< 1 || len
> 64)
358 * NOTE: limited by the USB ctrl message constraints
359 * Zero length reads always succeed, even if no device is connected
362 /* Set register and write value */
363 ret
= dev
->em28xx_write_regs_req(dev
, 0x06, addr
, buf
, len
);
366 dev_warn(&dev
->intf
->dev
,
367 "writing to i2c device at 0x%x failed (error=%i)\n",
372 dev_warn(&dev
->intf
->dev
,
373 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
378 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
380 * NOTE: the only error we've seen so far is
381 * 0x01 when the slave device is not present
387 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
393 * NOTE: With chip types (other chip IDs) which actually don't support
394 * this operation, it seems to succeed ALWAYS ! (even if there is no
395 * slave device or even no second i2c bus provided)
400 * em25xx_bus_B_recv_bytes
401 * read bytes from the i2c device
403 static int em25xx_bus_B_recv_bytes(struct em28xx
*dev
, u16 addr
, u8
*buf
,
408 if (len
< 1 || len
> 64)
411 * NOTE: limited by the USB ctrl message constraints
412 * Zero length reads always succeed, even if no device is connected
416 ret
= dev
->em28xx_read_reg_req_len(dev
, 0x06, addr
, buf
, len
);
418 dev_warn(&dev
->intf
->dev
,
419 "reading from i2c device at 0x%x failed (error=%i)\n",
424 * NOTE: some devices with two i2c buses have the bad habit to return 0
425 * bytes if we are on bus B AND there was no write attempt to the
426 * specified slave address before AND no device is present at the
427 * requested slave address.
428 * Anyway, the next check will fail with -ENXIO in this case, so avoid
429 * spamming the system log on device probing and do nothing here.
433 ret
= dev
->em28xx_read_reg_req(dev
, 0x08, 0x0000);
435 * NOTE: the only error we've seen so far is
436 * 0x01 when the slave device is not present
442 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret
);
448 * NOTE: With chip types (other chip IDs) which actually don't support
449 * this operation, it seems to succeed ALWAYS ! (even if there is no
450 * slave device or even no second i2c bus provided)
455 * em25xx_bus_B_check_for_device()
456 * check if there is a i2c device at the supplied address
458 static int em25xx_bus_B_check_for_device(struct em28xx
*dev
, u16 addr
)
463 ret
= em25xx_bus_B_recv_bytes(dev
, addr
, &buf
, 1);
469 * NOTE: With chips which do not support this operation,
470 * it seems to succeed ALWAYS ! (even if no device connected)
474 static inline int i2c_check_for_device(struct em28xx_i2c_bus
*i2c_bus
, u16 addr
)
476 struct em28xx
*dev
= i2c_bus
->dev
;
477 int rc
= -EOPNOTSUPP
;
479 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
480 rc
= em28xx_i2c_check_for_device(dev
, addr
);
481 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
482 rc
= em2800_i2c_check_for_device(dev
, addr
);
483 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
484 rc
= em25xx_bus_B_check_for_device(dev
, addr
);
488 static inline int i2c_recv_bytes(struct em28xx_i2c_bus
*i2c_bus
,
491 struct em28xx
*dev
= i2c_bus
->dev
;
492 u16 addr
= msg
.addr
<< 1;
493 int rc
= -EOPNOTSUPP
;
495 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
496 rc
= em28xx_i2c_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
497 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
498 rc
= em2800_i2c_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
499 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
500 rc
= em25xx_bus_B_recv_bytes(dev
, addr
, msg
.buf
, msg
.len
);
504 static inline int i2c_send_bytes(struct em28xx_i2c_bus
*i2c_bus
,
505 struct i2c_msg msg
, int stop
)
507 struct em28xx
*dev
= i2c_bus
->dev
;
508 u16 addr
= msg
.addr
<< 1;
509 int rc
= -EOPNOTSUPP
;
511 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
)
512 rc
= em28xx_i2c_send_bytes(dev
, addr
, msg
.buf
, msg
.len
, stop
);
513 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
)
514 rc
= em2800_i2c_send_bytes(dev
, addr
, msg
.buf
, msg
.len
);
515 else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
)
516 rc
= em25xx_bus_B_send_bytes(dev
, addr
, msg
.buf
, msg
.len
);
522 * the main i2c transfer function
524 static int em28xx_i2c_xfer(struct i2c_adapter
*i2c_adap
,
525 struct i2c_msg msgs
[], int num
)
527 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
528 struct em28xx
*dev
= i2c_bus
->dev
;
529 unsigned int bus
= i2c_bus
->bus
;
534 * prevent i2c xfer attempts after device is disconnected
535 * some fe's try to do i2c writes/reads from their release
536 * interfaces when called in disconnect path
538 if (dev
->disconnected
)
541 if (!rt_mutex_trylock(&dev
->i2c_bus_lock
))
544 /* Switch I2C bus if needed */
545 if (bus
!= dev
->cur_i2c_bus
&&
546 i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
) {
548 reg
= EM2874_I2C_SECONDARY_BUS_SELECT
;
551 em28xx_write_reg_bits(dev
, EM28XX_R06_I2C_CLK
, reg
,
552 EM2874_I2C_SECONDARY_BUS_SELECT
);
553 dev
->cur_i2c_bus
= bus
;
556 for (i
= 0; i
< num
; i
++) {
557 addr
= msgs
[i
].addr
<< 1;
560 * no len: check only for device presence
561 * This code is only called during device probe.
563 rc
= i2c_check_for_device(i2c_bus
, addr
);
567 } else if (msgs
[i
].flags
& I2C_M_RD
) {
569 rc
= i2c_recv_bytes(i2c_bus
, msgs
[i
]);
572 rc
= i2c_send_bytes(i2c_bus
, msgs
[i
], i
== num
- 1);
578 dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
579 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
580 i
== num
- 1 ? "stop" : "nonstop",
582 msgs
[i
].len
, msgs
[i
].buf
);
585 rt_mutex_unlock(&dev
->i2c_bus_lock
);
589 dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
590 (msgs
[i
].flags
& I2C_M_RD
) ? "read" : "write",
591 i
== num
- 1 ? "stop" : "nonstop",
593 (rc
== -ENODEV
) ? "no device " : "",
596 rt_mutex_unlock(&dev
->i2c_bus_lock
);
601 * based on linux/sunrpc/svcauth.h and linux/hash.h
602 * The original hash function returns a different value, if arch is x86_64
605 static inline unsigned long em28xx_hash_mem(char *buf
, int length
, int bits
)
607 unsigned long hash
= 0;
621 if ((len
& (32 / 8 - 1)) == 0)
622 hash
= ((hash
^ l
) * 0x9e370001UL
);
625 return (hash
>> (32 - bits
)) & 0xffffffffUL
;
629 * Helper function to read data blocks from i2c clients with 8 or 16 bit
630 * address width, 8 bit register width and auto incrementation been activated
632 static int em28xx_i2c_read_block(struct em28xx
*dev
, unsigned int bus
, u16 addr
,
633 bool addr_w16
, u16 len
, u8
*data
)
635 int remain
= len
, rsize
, rsize_max
, ret
;
639 if (addr
+ remain
> (addr_w16
* 0xff00 + 0xff + 1))
643 buf
[1] = addr
& 0xff;
644 ret
= i2c_master_send(&dev
->i2c_client
[bus
],
645 buf
+ !addr_w16
, 1 + addr_w16
);
649 if (dev
->board
.is_em2800
)
654 if (remain
> rsize_max
)
659 ret
= i2c_master_recv(&dev
->i2c_client
[bus
], data
, rsize
);
670 static int em28xx_i2c_eeprom(struct em28xx
*dev
, unsigned int bus
,
671 u8
**eedata
, u16
*eedata_len
)
675 * FIXME common length/size for bytes to read, to display, hash
676 * calculation and returned device dataset. Simplifies the code a lot,
677 * but we might have to deal with multiple sizes in the future !
680 struct em28xx_eeprom
*dev_config
;
686 /* EEPROM is always on i2c bus 0 on all known devices. */
688 dev
->i2c_client
[bus
].addr
= 0xa0 >> 1;
690 /* Check if board has eeprom */
691 err
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
693 dev_info(&dev
->intf
->dev
, "board has no eeprom\n");
697 data
= kzalloc(len
, GFP_KERNEL
);
701 /* Read EEPROM content */
702 err
= em28xx_i2c_read_block(dev
, bus
, 0x0000,
703 dev
->eeprom_addrwidth_16bit
,
706 dev_err(&dev
->intf
->dev
,
707 "failed to read eeprom (err=%d)\n", err
);
712 /* Display eeprom content */
713 print_hex_dump(KERN_DEBUG
, "em28xx eeprom ", DUMP_PREFIX_OFFSET
,
714 16, 1, data
, len
, true);
716 if (dev
->eeprom_addrwidth_16bit
)
717 dev_info(&dev
->intf
->dev
,
718 "eeprom %06x: ... (skipped)\n", 256);
721 if (dev
->eeprom_addrwidth_16bit
&&
722 data
[0] == 0x26 && data
[3] == 0x00) {
723 /* new eeprom format; size 4-64kb */
727 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
728 mc_start
= (data
[1] << 8) + 4; /* usually 0x0004 */
730 dev_info(&dev
->intf
->dev
,
731 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
733 dev_info(&dev
->intf
->dev
,
735 dev_info(&dev
->intf
->dev
,
736 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
739 * boot configuration (address 0x0002):
740 * [0] microcode download speed: 1 = 400 kHz; 0 = 100 kHz
741 * [1] always selects 12 kb RAM
742 * [2] USB device speed: 1 = force Full Speed; 0 = auto detect
743 * [4] 1 = force fast mode and no suspend for device testing
744 * [5:7] USB PHY tuning registers; determined by device
749 * Read hardware config dataset offset from address
750 * (microcode start + 46)
752 err
= em28xx_i2c_read_block(dev
, bus
, mc_start
+ 46, 1, 2,
755 dev_err(&dev
->intf
->dev
,
756 "failed to read hardware configuration data from eeprom (err=%d)\n",
761 /* Calculate hardware config dataset start address */
762 hwconf_offset
= mc_start
+ data
[0] + (data
[1] << 8);
764 /* Read hardware config dataset */
766 * NOTE: the microcode copy can be multiple pages long, but
767 * we assume the hardware config dataset is the same as in
768 * the old eeprom and not longer than 256 bytes.
769 * tveeprom is currently also limited to 256 bytes.
771 err
= em28xx_i2c_read_block(dev
, bus
, hwconf_offset
, 1, len
,
774 dev_err(&dev
->intf
->dev
,
775 "failed to read hardware configuration data from eeprom (err=%d)\n",
780 /* Verify hardware config dataset */
781 /* NOTE: not all devices provide this type of dataset */
782 if (data
[0] != 0x1a || data
[1] != 0xeb ||
783 data
[2] != 0x67 || data
[3] != 0x95) {
784 dev_info(&dev
->intf
->dev
,
785 "\tno hardware configuration dataset found in eeprom\n");
791 * TODO: decrypt eeprom data for camera bridges
795 } else if (!dev
->eeprom_addrwidth_16bit
&&
796 data
[0] == 0x1a && data
[1] == 0xeb &&
797 data
[2] == 0x67 && data
[3] == 0x95) {
798 dev
->hash
= em28xx_hash_mem(data
, len
, 32);
799 dev_info(&dev
->intf
->dev
,
800 "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
802 dev_info(&dev
->intf
->dev
,
805 dev_info(&dev
->intf
->dev
,
806 "unknown eeprom format or eeprom corrupted !\n");
813 dev_config
= (void *)*eedata
;
815 switch (le16_to_cpu(dev_config
->chip_conf
) >> 4 & 0x3) {
817 dev_info(&dev
->intf
->dev
, "\tNo audio on board.\n");
820 dev_info(&dev
->intf
->dev
, "\tAC97 audio (5 sample rates)\n");
823 if (dev
->chip_id
< CHIP_ID_EM2860
)
824 dev_info(&dev
->intf
->dev
,
825 "\tI2S audio, sample rate=32k\n");
827 dev_info(&dev
->intf
->dev
,
828 "\tI2S audio, 3 sample rates\n");
831 if (dev
->chip_id
< CHIP_ID_EM2860
)
832 dev_info(&dev
->intf
->dev
,
833 "\tI2S audio, 3 sample rates\n");
835 dev_info(&dev
->intf
->dev
,
836 "\tI2S audio, 5 sample rates\n");
840 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 3)
841 dev_info(&dev
->intf
->dev
, "\tUSB Remote wakeup capable\n");
843 if (le16_to_cpu(dev_config
->chip_conf
) & 1 << 2)
844 dev_info(&dev
->intf
->dev
, "\tUSB Self power capable\n");
846 switch (le16_to_cpu(dev_config
->chip_conf
) & 0x3) {
848 dev_info(&dev
->intf
->dev
, "\t500mA max power\n");
851 dev_info(&dev
->intf
->dev
, "\t400mA max power\n");
854 dev_info(&dev
->intf
->dev
, "\t300mA max power\n");
857 dev_info(&dev
->intf
->dev
, "\t200mA max power\n");
860 dev_info(&dev
->intf
->dev
,
861 "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
862 dev_config
->string_idx_table
,
863 le16_to_cpu(dev_config
->string1
),
864 le16_to_cpu(dev_config
->string2
),
865 le16_to_cpu(dev_config
->string3
));
874 /* ----------------------------------------------------------- */
879 static u32
functionality(struct i2c_adapter
*i2c_adap
)
881 struct em28xx_i2c_bus
*i2c_bus
= i2c_adap
->algo_data
;
883 if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM28XX
||
884 i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM25XX_BUS_B
) {
885 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
886 } else if (i2c_bus
->algo_type
== EM28XX_I2C_ALGO_EM2800
) {
887 return (I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
) &
888 ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
;
891 WARN(1, "Unknown i2c bus algorithm.\n");
895 static const struct i2c_algorithm em28xx_algo
= {
896 .master_xfer
= em28xx_i2c_xfer
,
897 .functionality
= functionality
,
900 static const struct i2c_adapter em28xx_adap_template
= {
901 .owner
= THIS_MODULE
,
903 .algo
= &em28xx_algo
,
906 static const struct i2c_client em28xx_client_template
= {
907 .name
= "em28xx internal",
910 /* ----------------------------------------------------------- */
914 * incomplete list of known devices
916 static char *i2c_devs
[128] = {
917 [0x1c >> 1] = "lgdt330x",
918 [0x3e >> 1] = "remote IR sensor",
919 [0x4a >> 1] = "saa7113h",
920 [0x52 >> 1] = "drxk",
921 [0x60 >> 1] = "remote IR sensor",
922 [0x8e >> 1] = "remote IR sensor",
923 [0x86 >> 1] = "tda9887",
924 [0x80 >> 1] = "msp34xx",
925 [0x88 >> 1] = "msp34xx",
926 [0xa0 >> 1] = "eeprom",
927 [0xb0 >> 1] = "tda9874",
928 [0xb8 >> 1] = "tvp5150a",
929 [0xba >> 1] = "webcam sensor or tvp5150a",
930 [0xc0 >> 1] = "tuner (analog)",
931 [0xc2 >> 1] = "tuner (analog)",
932 [0xc4 >> 1] = "tuner (analog)",
933 [0xc6 >> 1] = "tuner (analog)",
938 * check i2c address range for devices
940 void em28xx_do_i2c_scan(struct em28xx
*dev
, unsigned int bus
)
942 u8 i2c_devicelist
[128];
946 memset(i2c_devicelist
, 0, sizeof(i2c_devicelist
));
948 for (i
= 0; i
< ARRAY_SIZE(i2c_devs
); i
++) {
949 dev
->i2c_client
[bus
].addr
= i
;
950 rc
= i2c_master_recv(&dev
->i2c_client
[bus
], &buf
, 0);
953 i2c_devicelist
[i
] = i
;
954 dev_info(&dev
->intf
->dev
,
955 "found i2c device @ 0x%x on bus %d [%s]\n",
956 i
<< 1, bus
, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
959 if (bus
== dev
->def_i2c_bus
)
960 dev
->i2c_hash
= em28xx_hash_mem(i2c_devicelist
,
961 sizeof(i2c_devicelist
), 32);
965 * em28xx_i2c_register()
968 int em28xx_i2c_register(struct em28xx
*dev
, unsigned int bus
,
969 enum em28xx_i2c_algo_type algo_type
)
973 if (WARN_ON(!dev
->em28xx_write_regs
|| !dev
->em28xx_read_reg
||
974 !dev
->em28xx_write_regs_req
|| !dev
->em28xx_read_reg_req
))
977 if (bus
>= NUM_I2C_BUSES
)
980 dev
->i2c_adap
[bus
] = em28xx_adap_template
;
981 dev
->i2c_adap
[bus
].dev
.parent
= &dev
->intf
->dev
;
982 strscpy(dev
->i2c_adap
[bus
].name
, dev_name(&dev
->intf
->dev
),
983 sizeof(dev
->i2c_adap
[bus
].name
));
985 dev
->i2c_bus
[bus
].bus
= bus
;
986 dev
->i2c_bus
[bus
].algo_type
= algo_type
;
987 dev
->i2c_bus
[bus
].dev
= dev
;
988 dev
->i2c_adap
[bus
].algo_data
= &dev
->i2c_bus
[bus
];
990 retval
= i2c_add_adapter(&dev
->i2c_adap
[bus
]);
992 dev_err(&dev
->intf
->dev
,
993 "%s: i2c_add_adapter failed! retval [%d]\n",
998 dev
->i2c_client
[bus
] = em28xx_client_template
;
999 dev
->i2c_client
[bus
].adapter
= &dev
->i2c_adap
[bus
];
1001 /* Up to now, all eeproms are at bus 0 */
1003 retval
= em28xx_i2c_eeprom(dev
, bus
,
1004 &dev
->eedata
, &dev
->eedata_len
);
1005 if (retval
< 0 && retval
!= -ENODEV
) {
1006 dev_err(&dev
->intf
->dev
,
1007 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
1013 em28xx_do_i2c_scan(dev
, bus
);
1019 * em28xx_i2c_unregister()
1020 * unregister i2c_bus
1022 int em28xx_i2c_unregister(struct em28xx
*dev
, unsigned int bus
)
1024 if (bus
>= NUM_I2C_BUSES
)
1027 i2c_del_adapter(&dev
->i2c_adap
[bus
]);