aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/em28xx/em28xx-i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/usb/em28xx/em28xx-i2c.c')
-rw-r--r--drivers/media/usb/em28xx/em28xx-i2c.c199
1 files changed, 117 insertions, 82 deletions
diff --git a/drivers/media/usb/em28xx/em28xx-i2c.c b/drivers/media/usb/em28xx/em28xx-i2c.c
index c4ff9739a7ae..7e1724076ac4 100644
--- a/drivers/media/usb/em28xx/em28xx-i2c.c
+++ b/drivers/media/usb/em28xx/em28xx-i2c.c
@@ -26,6 +26,7 @@
26#include <linux/kernel.h> 26#include <linux/kernel.h>
27#include <linux/usb.h> 27#include <linux/usb.h>
28#include <linux/i2c.h> 28#include <linux/i2c.h>
29#include <linux/jiffies.h>
29 30
30#include "em28xx.h" 31#include "em28xx.h"
31#include "tuner-xc2028.h" 32#include "tuner-xc2028.h"
@@ -40,7 +41,7 @@ MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
40 41
41static unsigned int i2c_debug; 42static unsigned int i2c_debug;
42module_param(i2c_debug, int, 0644); 43module_param(i2c_debug, int, 0644);
43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); 44MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
44 45
45/* 46/*
46 * em2800_i2c_send_bytes() 47 * em2800_i2c_send_bytes()
@@ -48,8 +49,8 @@ MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
48 */ 49 */
49static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) 50static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
50{ 51{
52 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
51 int ret; 53 int ret;
52 int write_timeout;
53 u8 b2[6]; 54 u8 b2[6];
54 55
55 if (len < 1 || len > 4) 56 if (len < 1 || len > 4)
@@ -74,22 +75,26 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
74 return (ret < 0) ? ret : -EIO; 75 return (ret < 0) ? ret : -EIO;
75 } 76 }
76 /* wait for completion */ 77 /* wait for completion */
77 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0; 78 while (time_is_after_jiffies(timeout)) {
78 write_timeout -= 5) {
79 ret = dev->em28xx_read_reg(dev, 0x05); 79 ret = dev->em28xx_read_reg(dev, 0x05);
80 if (ret == 0x80 + len - 1) { 80 if (ret == 0x80 + len - 1)
81 return len; 81 return len;
82 } else if (ret == 0x94 + len - 1) { 82 if (ret == 0x94 + len - 1) {
83 return -ENODEV; 83 if (i2c_debug == 1)
84 } else if (ret < 0) { 84 em28xx_warn("R05 returned 0x%02x: I2C timeout",
85 ret);
86 return -ENXIO;
87 }
88 if (ret < 0) {
85 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", 89 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
86 ret); 90 ret);
87 return ret; 91 return ret;
88 } 92 }
89 msleep(5); 93 msleep(5);
90 } 94 }
91 em28xx_warn("write to i2c device at 0x%x timed out\n", addr); 95 if (i2c_debug)
92 return -EIO; 96 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
97 return -ETIMEDOUT;
93} 98}
94 99
95/* 100/*
@@ -98,9 +103,9 @@ static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
98 */ 103 */
99static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len) 104static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
100{ 105{
106 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
101 u8 buf2[4]; 107 u8 buf2[4];
102 int ret; 108 int ret;
103 int read_timeout;
104 int i; 109 int i;
105 110
106 if (len < 1 || len > 4) 111 if (len < 1 || len > 4)
@@ -117,22 +122,28 @@ static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
117 } 122 }
118 123
119 /* wait for completion */ 124 /* wait for completion */
120 for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0; 125 while (time_is_after_jiffies(timeout)) {
121 read_timeout -= 5) {
122 ret = dev->em28xx_read_reg(dev, 0x05); 126 ret = dev->em28xx_read_reg(dev, 0x05);
123 if (ret == 0x84 + len - 1) { 127 if (ret == 0x84 + len - 1)
124 break; 128 break;
125 } else if (ret == 0x94 + len - 1) { 129 if (ret == 0x94 + len - 1) {
126 return -ENODEV; 130 if (i2c_debug == 1)
127 } else if (ret < 0) { 131 em28xx_warn("R05 returned 0x%02x: I2C timeout",
132 ret);
133 return -ENXIO;
134 }
135 if (ret < 0) {
128 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n", 136 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
129 ret); 137 ret);
130 return ret; 138 return ret;
131 } 139 }
132 msleep(5); 140 msleep(5);
133 } 141 }
134 if (ret != 0x84 + len - 1) 142 if (ret != 0x84 + len - 1) {
135 em28xx_warn("read from i2c device at 0x%x timed out\n", addr); 143 if (i2c_debug)
144 em28xx_warn("read from i2c device at 0x%x timed out\n",
145 addr);
146 }
136 147
137 /* get the received message */ 148 /* get the received message */
138 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len); 149 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
@@ -168,7 +179,8 @@ static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
168static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf, 179static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
169 u16 len, int stop) 180 u16 len, int stop)
170{ 181{
171 int write_timeout, ret; 182 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
183 int ret;
172 184
173 if (len < 1 || len > 64) 185 if (len < 1 || len > 64)
174 return -EOPNOTSUPP; 186 return -EOPNOTSUPP;
@@ -191,16 +203,19 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
191 } 203 }
192 } 204 }
193 205
194 /* Check success of the i2c operation */ 206 /* wait for completion */
195 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0; 207 while (time_is_after_jiffies(timeout)) {
196 write_timeout -= 5) {
197 ret = dev->em28xx_read_reg(dev, 0x05); 208 ret = dev->em28xx_read_reg(dev, 0x05);
198 if (ret == 0) { /* success */ 209 if (ret == 0) /* success */
199 return len; 210 return len;
200 } else if (ret == 0x10) { 211 if (ret == 0x10) {
201 return -ENODEV; 212 if (i2c_debug == 1)
202 } else if (ret < 0) { 213 em28xx_warn("I2C transfer timeout on writing to addr 0x%02x",
203 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n", 214 addr);
215 return -ENXIO;
216 }
217 if (ret < 0) {
218 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
204 ret); 219 ret);
205 return ret; 220 return ret;
206 } 221 }
@@ -211,8 +226,10 @@ static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
211 * (even with high payload) ... 226 * (even with high payload) ...
212 */ 227 */
213 } 228 }
214 em28xx_warn("write to i2c device at 0x%x timed out\n", addr); 229 if (i2c_debug)
215 return -EIO; 230 em28xx_warn("write to i2c device at 0x%x timed out (status=%i)\n",
231 addr, ret);
232 return -ETIMEDOUT;
216} 233}
217 234
218/* 235/*
@@ -242,26 +259,28 @@ static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
242 * bytes if we are on bus B AND there was no write attempt to the 259 * bytes if we are on bus B AND there was no write attempt to the
243 * specified slave address before AND no device is present at the 260 * specified slave address before AND no device is present at the
244 * requested slave address. 261 * requested slave address.
245 * Anyway, the next check will fail with -ENODEV in this case, so avoid 262 * Anyway, the next check will fail with -ENXIO in this case, so avoid
246 * spamming the system log on device probing and do nothing here. 263 * spamming the system log on device probing and do nothing here.
247 */ 264 */
248 265
249 /* Check success of the i2c operation */ 266 /* Check success of the i2c operation */
250 ret = dev->em28xx_read_reg(dev, 0x05); 267 ret = dev->em28xx_read_reg(dev, 0x05);
268 if (ret == 0) /* success */
269 return len;
251 if (ret < 0) { 270 if (ret < 0) {
252 em28xx_warn("failed to read i2c transfer status from bridge (error=%i)\n", 271 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
253 ret); 272 ret);
254 return ret; 273 return ret;
255 } 274 }
256 if (ret > 0) { 275 if (ret == 0x10) {
257 if (ret == 0x10) { 276 if (i2c_debug == 1)
258 return -ENODEV; 277 em28xx_warn("I2C transfer timeout on writing to addr 0x%02x",
259 } else { 278 addr);
260 em28xx_warn("unknown i2c error (status=%i)\n", ret); 279 return -ENXIO;
261 return -EIO;
262 }
263 } 280 }
264 return len; 281
282 em28xx_warn("unknown i2c error (status=%i)\n", ret);
283 return -ETIMEDOUT;
265} 284}
266 285
267/* 286/*
@@ -316,8 +335,12 @@ static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
316 */ 335 */
317 if (!ret) 336 if (!ret)
318 return len; 337 return len;
319 else if (ret > 0) 338 else if (ret > 0) {
320 return -ENODEV; 339 if (i2c_debug == 1)
340 em28xx_warn("Bus B R08 returned 0x%02x: I2C timeout",
341 ret);
342 return -ENXIO;
343 }
321 344
322 return ret; 345 return ret;
323 /* 346 /*
@@ -355,7 +378,7 @@ static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
355 * bytes if we are on bus B AND there was no write attempt to the 378 * bytes if we are on bus B AND there was no write attempt to the
356 * specified slave address before AND no device is present at the 379 * specified slave address before AND no device is present at the
357 * requested slave address. 380 * requested slave address.
358 * Anyway, the next check will fail with -ENODEV in this case, so avoid 381 * Anyway, the next check will fail with -ENXIO in this case, so avoid
359 * spamming the system log on device probing and do nothing here. 382 * spamming the system log on device probing and do nothing here.
360 */ 383 */
361 384
@@ -367,8 +390,12 @@ static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
367 */ 390 */
368 if (!ret) 391 if (!ret)
369 return len; 392 return len;
370 else if (ret > 0) 393 else if (ret > 0) {
371 return -ENODEV; 394 if (i2c_debug == 1)
395 em28xx_warn("Bus B R08 returned 0x%02x: I2C timeout",
396 ret);
397 return -ENXIO;
398 }
372 399
373 return ret; 400 return ret;
374 /* 401 /*
@@ -409,10 +436,6 @@ static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
409 rc = em2800_i2c_check_for_device(dev, addr); 436 rc = em2800_i2c_check_for_device(dev, addr);
410 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) 437 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
411 rc = em25xx_bus_B_check_for_device(dev, addr); 438 rc = em25xx_bus_B_check_for_device(dev, addr);
412 if (rc == -ENODEV) {
413 if (i2c_debug)
414 printk(" no device\n");
415 }
416 return rc; 439 return rc;
417} 440}
418 441
@@ -421,7 +444,7 @@ static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
421{ 444{
422 struct em28xx *dev = i2c_bus->dev; 445 struct em28xx *dev = i2c_bus->dev;
423 u16 addr = msg.addr << 1; 446 u16 addr = msg.addr << 1;
424 int byte, rc = -EOPNOTSUPP; 447 int rc = -EOPNOTSUPP;
425 448
426 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) 449 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
427 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len); 450 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
@@ -429,10 +452,6 @@ static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
429 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len); 452 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
430 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) 453 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
431 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len); 454 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
432 if (i2c_debug) {
433 for (byte = 0; byte < msg.len; byte++)
434 printk(" %02x", msg.buf[byte]);
435 }
436 return rc; 455 return rc;
437} 456}
438 457
@@ -441,12 +460,8 @@ static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
441{ 460{
442 struct em28xx *dev = i2c_bus->dev; 461 struct em28xx *dev = i2c_bus->dev;
443 u16 addr = msg.addr << 1; 462 u16 addr = msg.addr << 1;
444 int byte, rc = -EOPNOTSUPP; 463 int rc = -EOPNOTSUPP;
445 464
446 if (i2c_debug) {
447 for (byte = 0; byte < msg.len; byte++)
448 printk(" %02x", msg.buf[byte]);
449 }
450 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) 465 if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
451 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop); 466 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
452 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800) 467 else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
@@ -491,33 +506,53 @@ static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
491 } 506 }
492 for (i = 0; i < num; i++) { 507 for (i = 0; i < num; i++) {
493 addr = msgs[i].addr << 1; 508 addr = msgs[i].addr << 1;
494 if (i2c_debug) 509 if (i2c_debug > 1)
495 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:", 510 printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
496 dev->name, __func__ , 511 dev->name, __func__ ,
497 (msgs[i].flags & I2C_M_RD) ? "read" : "write", 512 (msgs[i].flags & I2C_M_RD) ? "read" : "write",
498 i == num - 1 ? "stop" : "nonstop", 513 i == num - 1 ? "stop" : "nonstop",
499 addr, msgs[i].len); 514 addr, msgs[i].len);
500 if (!msgs[i].len) { /* no len: check only for device presence */ 515 if (!msgs[i].len) {
516 /*
517 * no len: check only for device presence
518 * This code is only called during device probe.
519 */
501 rc = i2c_check_for_device(i2c_bus, addr); 520 rc = i2c_check_for_device(i2c_bus, addr);
502 if (rc == -ENODEV) { 521 if (rc < 0) {
522 if (rc == -ENXIO) {
523 if (i2c_debug > 1)
524 printk(KERN_CONT " no device\n");
525 rc = -ENODEV;
526 } else {
527 if (i2c_debug > 1)
528 printk(KERN_CONT " ERROR: %i\n", rc);
529 }
503 rt_mutex_unlock(&dev->i2c_bus_lock); 530 rt_mutex_unlock(&dev->i2c_bus_lock);
504 return rc; 531 return rc;
505 } 532 }
506 } else if (msgs[i].flags & I2C_M_RD) { 533 } else if (msgs[i].flags & I2C_M_RD) {
507 /* read bytes */ 534 /* read bytes */
508 rc = i2c_recv_bytes(i2c_bus, msgs[i]); 535 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
536
537 if (i2c_debug > 1 && rc >= 0)
538 printk(KERN_CONT " %*ph",
539 msgs[i].len, msgs[i].buf);
509 } else { 540 } else {
541 if (i2c_debug > 1)
542 printk(KERN_CONT " %*ph",
543 msgs[i].len, msgs[i].buf);
544
510 /* write bytes */ 545 /* write bytes */
511 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1); 546 rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
512 } 547 }
513 if (rc < 0) { 548 if (rc < 0) {
514 if (i2c_debug) 549 if (i2c_debug > 1)
515 printk(" ERROR: %i\n", rc); 550 printk(KERN_CONT " ERROR: %i\n", rc);
516 rt_mutex_unlock(&dev->i2c_bus_lock); 551 rt_mutex_unlock(&dev->i2c_bus_lock);
517 return rc; 552 return rc;
518 } 553 }
519 if (i2c_debug) 554 if (i2c_debug > 1)
520 printk("\n"); 555 printk(KERN_CONT "\n");
521 } 556 }
522 557
523 rt_mutex_unlock(&dev->i2c_bus_lock); 558 rt_mutex_unlock(&dev->i2c_bus_lock);
@@ -600,7 +635,7 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
600 * calculation and returned device dataset. Simplifies the code a lot, 635 * calculation and returned device dataset. Simplifies the code a lot,
601 * but we might have to deal with multiple sizes in the future ! 636 * but we might have to deal with multiple sizes in the future !
602 */ 637 */
603 int i, err; 638 int err;
604 struct em28xx_eeprom *dev_config; 639 struct em28xx_eeprom *dev_config;
605 u8 buf, *data; 640 u8 buf, *data;
606 641
@@ -631,20 +666,14 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
631 goto error; 666 goto error;
632 } 667 }
633 668
634 /* Display eeprom content */ 669 if (i2c_debug) {
635 for (i = 0; i < len; i++) { 670 /* Display eeprom content */
636 if (0 == (i % 16)) { 671 print_hex_dump(KERN_INFO, "eeprom ", DUMP_PREFIX_OFFSET,
637 if (dev->eeprom_addrwidth_16bit) 672 16, 1, data, len, true);
638 em28xx_info("i2c eeprom %04x:", i); 673
639 else 674 if (dev->eeprom_addrwidth_16bit)
640 em28xx_info("i2c eeprom %02x:", i); 675 em28xx_info("eeprom %06x: ... (skipped)\n", 256);
641 }
642 printk(" %02x", data[i]);
643 if (15 == (i % 16))
644 printk("\n");
645 } 676 }
646 if (dev->eeprom_addrwidth_16bit)
647 em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
648 677
649 if (dev->eeprom_addrwidth_16bit && 678 if (dev->eeprom_addrwidth_16bit &&
650 data[0] == 0x26 && data[3] == 0x00) { 679 data[0] == 0x26 && data[3] == 0x00) {
@@ -736,10 +765,16 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
736 em28xx_info("\tAC97 audio (5 sample rates)\n"); 765 em28xx_info("\tAC97 audio (5 sample rates)\n");
737 break; 766 break;
738 case 2: 767 case 2:
739 em28xx_info("\tI2S audio, sample rate=32k\n"); 768 if (dev->chip_id < CHIP_ID_EM2860)
769 em28xx_info("\tI2S audio, sample rate=32k\n");
770 else
771 em28xx_info("\tI2S audio, 3 sample rates\n");
740 break; 772 break;
741 case 3: 773 case 3:
742 em28xx_info("\tI2S audio, 3 sample rates\n"); 774 if (dev->chip_id < CHIP_ID_EM2860)
775 em28xx_info("\tI2S audio, 3 sample rates\n");
776 else
777 em28xx_info("\tI2S audio, 5 sample rates\n");
743 break; 778 break;
744 } 779 }
745 780