21 #include <linux/i2c.h>
22 #include <linux/module.h>
30 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
39 static unsigned int i2c_scan;
47 static int pvr2_disable_ir_video;
51 "1=do not try to autoload ir_video IR receiver");
53 static int pvr2_i2c_write(
struct pvr2_hdw *hdw,
62 if (!data) length = 0;
65 "Killing an I2C write to %u that is too large"
66 " (desired=%u limit=%u)",
68 length,(
unsigned int)(
sizeof(hdw->
cmd_buffer) - 3));
94 " from i2_write[%d]: %d",
105 static int pvr2_i2c_read(
struct pvr2_hdw *hdw,
119 "Killing an I2C read to %u that has wlen too large"
120 " (desired=%u limit=%u)",
122 dlen,(
unsigned int)(
sizeof(hdw->
cmd_buffer) - 4));
125 if (res && (rlen > (
sizeof(hdw->
cmd_buffer) - 1))) {
127 "Killing an I2C read to %u that has rlen too large"
128 " (desired=%u limit=%u)",
130 rlen,(
unsigned int)(
sizeof(hdw->
cmd_buffer) - 1));
158 " from i2_read[%d]: %d",
181 static int pvr2_i2c_basic_op(
struct pvr2_hdw *hdw,
188 if (!rdata) rlen = 0;
189 if (!wdata) wlen = 0;
191 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
193 return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
204 static int i2c_24xxx_ir(
struct pvr2_hdw *hdw,
210 if (!(rlen || wlen)) {
216 if ((wlen != 0) || (rlen == 0))
return -
EIO;
226 if (rlen > 0) rdata[0] = 0;
227 if (rlen > 1) rdata[1] = 0;
244 if (stat != 0)
return stat;
263 rdata[0] = (val >> 8) & 0xffu;
264 rdata[1] = val & 0xff
u;
274 static int i2c_hack_wm8775(
struct pvr2_hdw *hdw,
277 if (!(rlen || wlen)) {
281 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
287 static int i2c_black_hole(
struct pvr2_hdw *hdw,
300 static int i2c_hack_cx25840(
struct pvr2_hdw *hdw,
304 unsigned int subaddr;
308 if (!(rlen || wlen)) {
316 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
329 case 1: subaddr = 0x0100;
break;
330 case 2: subaddr = 0x0101;
break;
333 }
else if (wlen == 2) {
334 subaddr = (wdata[0] << 8) | wdata[1];
336 case 0x0100: state = 1;
break;
337 case 0x0101: state = 2;
break;
343 if (!rlen)
goto success;
345 if (rlen != 1)
goto fail;
349 wbuf[0] = subaddr >> 8;
351 ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
353 if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
355 "WARNING: Detected a wedged cx25840 chip;"
356 " the device will not work.");
358 "WARNING: Try power cycling the pvrusb2 device.");
360 "WARNING: Disabling further access to the device"
361 " to prevent other foul-ups.");
383 static int pvr2_i2c_xfer(
struct i2c_adapter *i2c_adap,
427 msgs[0].
buf+offs,bcnt)) {
439 if (funcp(hdw,msgs[0].
addr,
440 msgs[0].
buf,msgs[0].len,
NULL,0)) {
445 }
else if (num == 2) {
447 trace_i2c(
"i2c refusing 2 phase transfer with"
448 " conflicting target addresses");
452 if ((!((msgs[0].
flags & I2C_M_RD))) &&
453 (msgs[1].
flags & I2C_M_RD)) {
462 while (tcnt || wcnt) {
467 if (funcp(hdw,msgs[0].addr,
469 msgs[1].buf+offs,bcnt)) {
480 trace_i2c(
"i2c refusing complex transfer"
481 " read0=%d read1=%d",
482 (msgs[0].
flags & I2C_M_RD),
483 (msgs[1].
flags & I2C_M_RD));
486 trace_i2c(
"i2c refusing %d phase transfer",num);
492 for (idx = 0; idx < num; idx++) {
495 "pvrusb2 i2c xfer %u/%u:"
496 " addr=0x%x len=%d %s",
500 (msgs[idx].
flags & I2C_M_RD ?
502 if ((ret > 0) || !(msgs[idx].
flags & I2C_M_RD)) {
503 if (cnt > 8) cnt = 8;
505 for (offs = 0; offs < (cnt>8?8:
cnt); offs++) {
509 if (offs < cnt)
printk(
" ...");
519 "pvrusb2 i2c xfer null transfer result=%d\n",
532 .master_xfer = pvr2_i2c_xfer,
533 .functionality = pvr2_i2c_functionality,
536 static struct i2c_adapter pvr2_i2c_adap_template = {
543 static int do_i2c_probe(
struct pvr2_hdw *hdw,
int addr)
556 static void do_i2c_scan(
struct pvr2_hdw *hdw)
560 for (i = 0; i < 128; i++) {
561 if (do_i2c_probe(hdw, i)) {
569 static void pvr2_i2c_register_ir(
struct pvr2_hdw *hdw)
573 if (pvr2_disable_ir_video) {
575 "Automatic binding of ir_video has been disabled.");
589 info.platform_data = init_data;
603 info.platform_data = init_data;
610 info.platform_data = init_data;
636 hdw->
i2c_func[0x18] = i2c_black_hole;
644 if (hdw->
hdw_desc->flag_has_cx25840) {
645 hdw->
i2c_func[0x44] = i2c_hack_cx25840;
647 if (hdw->
hdw_desc->flag_has_wm8775) {
648 hdw->
i2c_func[0x1b] = i2c_hack_wm8775;
661 if (hdw->
i2c_func[0x18] == i2c_24xxx_ir) {
668 if (do_i2c_probe(hdw, 0x71)) {
670 "Device has newer IR hardware;"
671 " disabling unneeded virtual IR device");
677 if (i2c_scan) do_i2c_scan(hdw);
679 pvr2_i2c_register_ir(hdw);