1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "em28xx.h"
26
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>
32
33#include "tuner-xc2028.h"
34#include <media/v4l2-common.h>
35#include <media/tuner.h>
36
37
38
39static unsigned int i2c_scan;
40module_param(i2c_scan, int, 0444);
41MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
42
43static unsigned int i2c_debug;
44module_param(i2c_debug, int, 0644);
45MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
46
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); \
51} while (0)
52
53
54
55
56
57
58static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
59{
60 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
61 int ret;
62 u8 b2[6];
63
64 if (len < 1 || len > 4)
65 return -EOPNOTSUPP;
66
67 BUG_ON(len < 1 || len > 4);
68 b2[5] = 0x80 + len - 1;
69 b2[4] = addr;
70 b2[3] = buf[0];
71 if (len > 1)
72 b2[2] = buf[1];
73 if (len > 2)
74 b2[1] = buf[2];
75 if (len > 3)
76 b2[0] = buf[3];
77
78
79 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
80 if (ret != 2 + len) {
81 dev_warn(&dev->intf->dev,
82 "failed to trigger write to i2c address 0x%x (error=%i)\n",
83 addr, ret);
84 return (ret < 0) ? ret : -EIO;
85 }
86
87 while (time_is_after_jiffies(timeout)) {
88 ret = dev->em28xx_read_reg(dev, 0x05);
89 if (ret == 0x80 + len - 1)
90 return len;
91 if (ret == 0x94 + len - 1) {
92 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
93 return -ENXIO;
94 }
95 if (ret < 0) {
96 dev_warn(&dev->intf->dev,
97 "failed to get i2c transfer status from bridge register (error=%i)\n",
98 ret);
99 return ret;
100 }
101 msleep(5);
102 }
103 dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
104 return -ETIMEDOUT;
105}
106
107
108
109
110
111static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
112{
113 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
114 u8 buf2[4];
115 int ret;
116 int i;
117
118 if (len < 1 || len > 4)
119 return -EOPNOTSUPP;
120
121
122 buf2[1] = 0x84 + len - 1;
123 buf2[0] = addr;
124 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
125 if (ret != 2) {
126 dev_warn(&dev->intf->dev,
127 "failed to trigger read from i2c address 0x%x (error=%i)\n",
128 addr, ret);
129 return (ret < 0) ? ret : -EIO;
130 }
131
132
133 while (time_is_after_jiffies(timeout)) {
134 ret = dev->em28xx_read_reg(dev, 0x05);
135 if (ret == 0x84 + len - 1)
136 break;
137 if (ret == 0x94 + len - 1) {
138 dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
139 ret);
140 return -ENXIO;
141 }
142 if (ret < 0) {
143 dev_warn(&dev->intf->dev,
144 "failed to get i2c transfer status from bridge register (error=%i)\n",
145 ret);
146 return ret;
147 }
148 msleep(5);
149 }
150 if (ret != 0x84 + len - 1) {
151 dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
152 }
153
154
155 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
156 if (ret != 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",
159 addr, ret);
160 return (ret < 0) ? ret : -EIO;
161 }
162 for (i = 0; i < len; i++)
163 buf[i] = buf2[len - 1 - i];
164
165 return ret;
166}
167
168
169
170
171
172static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
173{
174 u8 buf;
175 int ret;
176
177 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
178 if (ret == 1)
179 return 0;
180 return (ret < 0) ? ret : -EIO;
181}
182
183
184
185
186static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
187 u16 len, int stop)
188{
189 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
190 int ret;
191
192 if (len < 1 || len > 64)
193 return -EOPNOTSUPP;
194
195
196
197
198
199
200 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
201 if (ret != len) {
202 if (ret < 0) {
203 dev_warn(&dev->intf->dev,
204 "writing to i2c device at 0x%x failed (error=%i)\n",
205 addr, ret);
206 return ret;
207 } else {
208 dev_warn(&dev->intf->dev,
209 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
210 len, addr, ret);
211 return -EIO;
212 }
213 }
214
215
216 while (time_is_after_jiffies(timeout)) {
217 ret = dev->em28xx_read_reg(dev, 0x05);
218 if (ret == 0)
219 return len;
220 if (ret == 0x10) {
221 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
222 addr);
223 return -ENXIO;
224 }
225 if (ret < 0) {
226 dev_warn(&dev->intf->dev,
227 "failed to get i2c transfer status from bridge register (error=%i)\n",
228 ret);
229 return ret;
230 }
231 msleep(5);
232
233
234
235
236
237 }
238
239 if (ret == 0x02 || ret == 0x04) {
240
241 dprintk(0,
242 "write to i2c device at 0x%x timed out (status=%i)\n",
243 addr, ret);
244 return -ETIMEDOUT;
245 }
246
247 dev_warn(&dev->intf->dev,
248 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
249 addr, ret);
250 return -EIO;
251}
252
253
254
255
256
257static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
258{
259 int ret;
260
261 if (len < 1 || len > 64)
262 return -EOPNOTSUPP;
263
264
265
266
267
268
269 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
270 if (ret < 0) {
271 dev_warn(&dev->intf->dev,
272 "reading from i2c device at 0x%x failed (error=%i)\n",
273 addr, ret);
274 return ret;
275 }
276
277
278
279
280
281
282
283
284
285
286 ret = dev->em28xx_read_reg(dev, 0x05);
287 if (ret == 0)
288 return len;
289 if (ret < 0) {
290 dev_warn(&dev->intf->dev,
291 "failed to get i2c transfer status from bridge register (error=%i)\n",
292 ret);
293 return ret;
294 }
295 if (ret == 0x10) {
296 dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
297 addr);
298 return -ENXIO;
299 }
300
301 if (ret == 0x02 || ret == 0x04) {
302
303 dprintk(0,
304 "write to i2c device at 0x%x timed out (status=%i)\n",
305 addr, ret);
306 return -ETIMEDOUT;
307 }
308
309 dev_warn(&dev->intf->dev,
310 "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
311 addr, ret);
312 return -EIO;
313}
314
315
316
317
318
319static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
320{
321 int ret;
322 u8 buf;
323
324 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
325 if (ret == 1)
326 return 0;
327 return (ret < 0) ? ret : -EIO;
328}
329
330
331
332
333
334static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
335 u16 len)
336{
337 int ret;
338
339 if (len < 1 || len > 64)
340 return -EOPNOTSUPP;
341
342
343
344
345
346
347 ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
348 if (ret != len) {
349 if (ret < 0) {
350 dev_warn(&dev->intf->dev,
351 "writing to i2c device at 0x%x failed (error=%i)\n",
352 addr, ret);
353 return ret;
354 } else {
355 dev_warn(&dev->intf->dev,
356 "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
357 len, addr, ret);
358 return -EIO;
359 }
360 }
361
362 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
363
364
365
366
367 if (!ret)
368 return len;
369 else if (ret > 0) {
370 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
371 return -ENXIO;
372 }
373
374 return ret;
375
376
377
378
379
380}
381
382
383
384
385
386static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
387 u16 len)
388{
389 int ret;
390
391 if (len < 1 || len > 64)
392 return -EOPNOTSUPP;
393
394
395
396
397
398
399 ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
400 if (ret < 0) {
401 dev_warn(&dev->intf->dev,
402 "reading from i2c device at 0x%x failed (error=%i)\n",
403 addr, ret);
404 return ret;
405 }
406
407
408
409
410
411
412
413
414
415
416 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
417
418
419
420
421 if (!ret)
422 return len;
423 else if (ret > 0) {
424 dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
425 return -ENXIO;
426 }
427
428 return ret;
429
430
431
432
433
434}
435
436
437
438
439
440static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
441{
442 u8 buf;
443 int ret;
444
445 ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
446 if (ret < 0)
447 return ret;
448
449 return 0;
450
451
452
453
454}
455
456static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
457{
458 struct em28xx *dev = i2c_bus->dev;
459 int rc = -EOPNOTSUPP;
460
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);
467 return rc;
468}
469
470static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
471 struct i2c_msg msg)
472{
473 struct em28xx *dev = i2c_bus->dev;
474 u16 addr = msg.addr << 1;
475 int rc = -EOPNOTSUPP;
476
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);
483 return rc;
484}
485
486static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
487 struct i2c_msg msg, int stop)
488{
489 struct em28xx *dev = i2c_bus->dev;
490 u16 addr = msg.addr << 1;
491 int rc = -EOPNOTSUPP;
492
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);
499 return rc;
500}
501
502
503
504
505
506static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
507 struct i2c_msg msgs[], int num)
508{
509 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
510 struct em28xx *dev = i2c_bus->dev;
511 unsigned bus = i2c_bus->bus;
512 int addr, rc, i;
513 u8 reg;
514
515
516
517
518 if (dev->disconnected)
519 return -ENODEV;
520
521 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
522 return -EAGAIN;
523
524
525 if (bus != dev->cur_i2c_bus &&
526 i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
527 if (bus == 1)
528 reg = EM2874_I2C_SECONDARY_BUS_SELECT;
529 else
530 reg = 0;
531 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
532 EM2874_I2C_SECONDARY_BUS_SELECT);
533 dev->cur_i2c_bus = bus;
534 }
535
536 if (num <= 0) {
537 rt_mutex_unlock(&dev->i2c_bus_lock);
538 return 0;
539 }
540 for (i = 0; i < num; i++) {
541 addr = msgs[i].addr << 1;
542 if (!msgs[i].len) {
543
544
545
546
547 rc = i2c_check_for_device(i2c_bus, addr);
548
549 if (rc == -ENXIO)
550 rc = -ENODEV;
551 } else if (msgs[i].flags & I2C_M_RD) {
552
553 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
554 } else {
555
556 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
557 }
558
559 if (rc < 0)
560 goto error;
561
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",
565 addr, msgs[i].len,
566 msgs[i].len, msgs[i].buf);
567 }
568
569 rt_mutex_unlock(&dev->i2c_bus_lock);
570 return num;
571
572error:
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",
576 addr, msgs[i].len,
577 (rc == -ENODEV) ? "no device " : "",
578 rc);
579
580 rt_mutex_unlock(&dev->i2c_bus_lock);
581 return rc;
582}
583
584
585
586
587
588
589static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
590{
591 unsigned long hash = 0;
592 unsigned long l = 0;
593 int len = 0;
594 unsigned char c;
595
596 do {
597 if (len == length) {
598 c = (char)len;
599 len = -1;
600 } else
601 c = *buf++;
602 l = (l << 8) | c;
603 len++;
604 if ((len & (32 / 8 - 1)) == 0)
605 hash = ((hash^l) * 0x9e370001UL);
606 } while (len);
607
608 return (hash >> (32 - bits)) & 0xffffffffUL;
609}
610
611
612
613
614
615static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
616 bool addr_w16, u16 len, u8 *data)
617{
618 int remain = len, rsize, rsize_max, ret;
619 u8 buf[2];
620
621
622 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
623 return -EINVAL;
624
625 buf[0] = addr >> 8;
626 buf[1] = addr & 0xff;
627 ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
628 if (ret < 0)
629 return ret;
630
631 if (dev->board.is_em2800)
632 rsize_max = 4;
633 else
634 rsize_max = 64;
635 while (remain > 0) {
636 if (remain > rsize_max)
637 rsize = rsize_max;
638 else
639 rsize = remain;
640
641 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
642 if (ret < 0)
643 return ret;
644
645 remain -= rsize;
646 data += rsize;
647 }
648
649 return len;
650}
651
652static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
653 u8 **eedata, u16 *eedata_len)
654{
655 const u16 len = 256;
656
657
658
659
660
661 int err;
662 struct em28xx_eeprom *dev_config;
663 u8 buf, *data;
664
665 *eedata = NULL;
666 *eedata_len = 0;
667
668
669
670 dev->i2c_client[bus].addr = 0xa0 >> 1;
671
672
673 err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
674 if (err < 0) {
675 dev_info(&dev->intf->dev, "board has no eeprom\n");
676 return -ENODEV;
677 }
678
679 data = kzalloc(len, GFP_KERNEL);
680 if (data == NULL)
681 return -ENOMEM;
682
683
684 err = em28xx_i2c_read_block(dev, bus, 0x0000,
685 dev->eeprom_addrwidth_16bit,
686 len, data);
687 if (err != len) {
688 dev_err(&dev->intf->dev,
689 "failed to read eeprom (err=%d)\n", err);
690 goto error;
691 }
692
693 if (i2c_debug) {
694
695 print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
696 16, 1, data, len, true);
697
698 if (dev->eeprom_addrwidth_16bit)
699 dev_info(&dev->intf->dev,
700 "eeprom %06x: ... (skipped)\n", 256);
701 }
702
703 if (dev->eeprom_addrwidth_16bit &&
704 data[0] == 0x26 && data[3] == 0x00) {
705
706 u16 mc_start;
707 u16 hwconf_offset;
708
709 dev->hash = em28xx_hash_mem(data, len, 32);
710 mc_start = (data[1] << 8) + 4;
711
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,
716 "EEPROM info:\n");
717 dev_info(&dev->intf->dev,
718 "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
719 mc_start, data[2]);
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
735 data);
736 if (err != 2) {
737 dev_err(&dev->intf->dev,
738 "failed to read hardware configuration data from eeprom (err=%d)\n",
739 err);
740 goto error;
741 }
742
743
744 hwconf_offset = mc_start + data[0] + (data[1] << 8);
745
746
747
748
749
750
751
752
753 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
754 data);
755 if (err != len) {
756 dev_err(&dev->intf->dev,
757 "failed to read hardware configuration data from eeprom (err=%d)\n",
758 err);
759 goto error;
760 }
761
762
763
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");
768 kfree(data);
769 return 0;
770 }
771
772
773
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,
782 "EEPROM info:\n");
783 } else {
784 dev_info(&dev->intf->dev,
785 "unknown eeprom format or eeprom corrupted !\n");
786 err = -ENODEV;
787 goto error;
788 }
789
790 *eedata = data;
791 *eedata_len = len;
792 dev_config = (void *)*eedata;
793
794 switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
795 case 0:
796 dev_info(&dev->intf->dev, "\tNo audio on board.\n");
797 break;
798 case 1:
799 dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
800 break;
801 case 2:
802 if (dev->chip_id < CHIP_ID_EM2860)
803 dev_info(&dev->intf->dev,
804 "\tI2S audio, sample rate=32k\n");
805 else
806 dev_info(&dev->intf->dev,
807 "\tI2S audio, 3 sample rates\n");
808 break;
809 case 3:
810 if (dev->chip_id < CHIP_ID_EM2860)
811 dev_info(&dev->intf->dev,
812 "\tI2S audio, 3 sample rates\n");
813 else
814 dev_info(&dev->intf->dev,
815 "\tI2S audio, 5 sample rates\n");
816 break;
817 }
818
819 if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
820 dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
821
822 if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
823 dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
824
825 switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
826 case 0:
827 dev_info(&dev->intf->dev, "\t500mA max power\n");
828 break;
829 case 1:
830 dev_info(&dev->intf->dev, "\t400mA max power\n");
831 break;
832 case 2:
833 dev_info(&dev->intf->dev, "\t300mA max power\n");
834 break;
835 case 3:
836 dev_info(&dev->intf->dev, "\t200mA max power\n");
837 break;
838 }
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));
845
846 return 0;
847
848error:
849 kfree(data);
850 return err;
851}
852
853
854
855
856
857
858static u32 functionality(struct i2c_adapter *i2c_adap)
859{
860 struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
861
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;
868 }
869
870 WARN(1, "Unknown i2c bus algorithm.\n");
871 return 0;
872}
873
874static const struct i2c_algorithm em28xx_algo = {
875 .master_xfer = em28xx_i2c_xfer,
876 .functionality = functionality,
877};
878
879static const struct i2c_adapter em28xx_adap_template = {
880 .owner = THIS_MODULE,
881 .name = "em28xx",
882 .algo = &em28xx_algo,
883};
884
885static const struct i2c_client em28xx_client_template = {
886 .name = "em28xx internal",
887};
888
889
890
891
892
893
894
895static 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)",
913};
914
915
916
917
918
919void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
920{
921 u8 i2c_devicelist[128];
922 unsigned char buf;
923 int i, rc;
924
925 memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
926
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);
930 if (rc < 0)
931 continue;
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] : "???");
936 }
937
938 if (bus == dev->def_i2c_bus)
939 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
940 ARRAY_SIZE(i2c_devicelist), 32);
941}
942
943
944
945
946
947int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
948 enum em28xx_i2c_algo_type algo_type)
949{
950 int retval;
951
952 BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
953 BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
954
955 if (bus >= NUM_I2C_BUSES)
956 return -ENODEV;
957
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));
961
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];
966
967 retval = i2c_add_adapter(&dev->i2c_adap[bus]);
968 if (retval < 0) {
969 dev_err(&dev->intf->dev,
970 "%s: i2c_add_adapter failed! retval [%d]\n",
971 __func__, retval);
972 return retval;
973 }
974
975 dev->i2c_client[bus] = em28xx_client_template;
976 dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
977
978
979 if (!bus) {
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",
984 __func__, retval);
985 }
986 }
987
988 if (i2c_scan)
989 em28xx_do_i2c_scan(dev, bus);
990
991 return 0;
992}
993
994
995
996
997
998int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
999{
1000 if (bus >= NUM_I2C_BUSES)
1001 return -ENODEV;
1002
1003 i2c_del_adapter(&dev->i2c_adap[bus]);
1004 return 0;
1005}
1006