5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "pvrusb2-i2c-core.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-debug.h"
25 #include "pvrusb2-fx2-cmd.h"
27 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
31 This module attempts to implement a compliant I2C adapter for the pvrusb2
32 device. By doing this we can then make use of existing functionality in
33 V4L (e.g. tuner.c) rather than rolling our own.
37 static unsigned int i2c_scan
= 0;
38 module_param(i2c_scan
, int, S_IRUGO
|S_IWUSR
);
39 MODULE_PARM_DESC(i2c_scan
,"scan i2c bus at insmod time");
41 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client
*cp
,
43 char *buf
,unsigned int maxlen
);
45 static int pvr2_i2c_write(struct pvr2_hdw
*hdw
, /* Context */
46 u8 i2c_addr
, /* I2C address we're talking to */
47 u8
*data
, /* Data to write */
48 u16 length
) /* Size of data to write */
50 /* Return value - default 0 means success */
54 if (!data
) length
= 0;
55 if (length
> (sizeof(hdw
->cmd_buffer
) - 3)) {
56 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
57 "Killing an I2C write to %u that is too large"
58 " (desired=%u limit=%u)",
60 length
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 3));
64 LOCK_TAKE(hdw
->ctl_lock
);
66 /* Clear the command buffer (likely to be paranoia) */
67 memset(hdw
->cmd_buffer
, 0, sizeof(hdw
->cmd_buffer
));
69 /* Set up command buffer for an I2C write */
70 hdw
->cmd_buffer
[0] = FX2CMD_I2C_WRITE
; /* write prefix */
71 hdw
->cmd_buffer
[1] = i2c_addr
; /* i2c addr of chip */
72 hdw
->cmd_buffer
[2] = length
; /* length of what follows */
73 if (length
) memcpy(hdw
->cmd_buffer
+ 3, data
, length
);
75 /* Do the operation */
76 ret
= pvr2_send_request(hdw
,
82 if (hdw
->cmd_buffer
[0] != 8) {
84 if (hdw
->cmd_buffer
[0] != 7) {
85 trace_i2c("unexpected status"
86 " from i2_write[%d]: %d",
87 i2c_addr
,hdw
->cmd_buffer
[0]);
92 LOCK_GIVE(hdw
->ctl_lock
);
97 static int pvr2_i2c_read(struct pvr2_hdw
*hdw
, /* Context */
98 u8 i2c_addr
, /* I2C address we're talking to */
99 u8
*data
, /* Data to write */
100 u16 dlen
, /* Size of data to write */
101 u8
*res
, /* Where to put data we read */
102 u16 rlen
) /* Amount of data to read */
104 /* Return value - default 0 means success */
109 if (dlen
> (sizeof(hdw
->cmd_buffer
) - 4)) {
110 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
111 "Killing an I2C read to %u that has wlen too large"
112 " (desired=%u limit=%u)",
114 dlen
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 4));
117 if (res
&& (rlen
> (sizeof(hdw
->cmd_buffer
) - 1))) {
118 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
119 "Killing an I2C read to %u that has rlen too large"
120 " (desired=%u limit=%u)",
122 rlen
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 1));
126 LOCK_TAKE(hdw
->ctl_lock
);
128 /* Clear the command buffer (likely to be paranoia) */
129 memset(hdw
->cmd_buffer
, 0, sizeof(hdw
->cmd_buffer
));
131 /* Set up command buffer for an I2C write followed by a read */
132 hdw
->cmd_buffer
[0] = FX2CMD_I2C_READ
; /* read prefix */
133 hdw
->cmd_buffer
[1] = dlen
; /* arg length */
134 hdw
->cmd_buffer
[2] = rlen
; /* answer length. Device will send one
135 more byte (status). */
136 hdw
->cmd_buffer
[3] = i2c_addr
; /* i2c addr of chip */
137 if (dlen
) memcpy(hdw
->cmd_buffer
+ 4, data
, dlen
);
139 /* Do the operation */
140 ret
= pvr2_send_request(hdw
,
146 if (hdw
->cmd_buffer
[0] != 8) {
148 if (hdw
->cmd_buffer
[0] != 7) {
149 trace_i2c("unexpected status"
150 " from i2_read[%d]: %d",
151 i2c_addr
,hdw
->cmd_buffer
[0]);
156 /* Copy back the result */
159 /* Error, just blank out the return buffer */
160 memset(res
, 0, rlen
);
162 memcpy(res
, hdw
->cmd_buffer
+ 1, rlen
);
166 LOCK_GIVE(hdw
->ctl_lock
);
171 /* This is the common low level entry point for doing I2C operations to the
173 static int pvr2_i2c_basic_op(struct pvr2_hdw
*hdw
,
180 if (!rdata
) rlen
= 0;
181 if (!wdata
) wlen
= 0;
183 return pvr2_i2c_read(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
185 return pvr2_i2c_write(hdw
,i2c_addr
,wdata
,wlen
);
190 /* This is a special entry point for cases of I2C transaction attempts to
191 the IR receiver. The implementation here simulates the IR receiver by
192 issuing a command to the FX2 firmware and using that response to return
193 what the real I2C receiver would have returned. We use this for 24xxx
194 devices, where the IR receiver chip has been removed and replaced with
195 FX2 related logic. */
196 static int i2c_24xxx_ir(struct pvr2_hdw
*hdw
,
197 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
202 if (!(rlen
|| wlen
)) {
203 /* This is a probe attempt. Just let it succeed. */
207 /* We don't understand this kind of transaction */
208 if ((wlen
!= 0) || (rlen
== 0)) return -EIO
;
211 /* Mike Isely <isely@pobox.com> Appears to be a probe
212 attempt from lirc. Just fill in zeroes and return. If
213 we try instead to do the full transaction here, then bad
214 things seem to happen within the lirc driver module
215 (version 0.8.0-7 sources from Debian, when run under
216 vanilla 2.6.17.6 kernel) - and I don't have the patience
218 if (rlen
> 0) rdata
[0] = 0;
219 if (rlen
> 1) rdata
[1] = 0;
223 /* Issue a command to the FX2 to read the IR receiver. */
224 LOCK_TAKE(hdw
->ctl_lock
); do {
225 hdw
->cmd_buffer
[0] = FX2CMD_GET_IR_CODE
;
226 stat
= pvr2_send_request(hdw
,
229 dat
[0] = hdw
->cmd_buffer
[0];
230 dat
[1] = hdw
->cmd_buffer
[1];
231 dat
[2] = hdw
->cmd_buffer
[2];
232 dat
[3] = hdw
->cmd_buffer
[3];
233 } while (0); LOCK_GIVE(hdw
->ctl_lock
);
235 /* Give up if that operation failed. */
236 if (stat
!= 0) return stat
;
238 /* Mangle the results into something that looks like the real IR
242 /* No code received. */
247 /* Mash the FX2 firmware-provided IR code into something
248 that the normal i2c chip-level driver expects. */
255 rdata
[0] = (val
>> 8) & 0xffu
;
256 rdata
[1] = val
& 0xffu
;
262 /* This is a special entry point that is entered if an I2C operation is
263 attempted to a wm8775 chip on model 24xxx hardware. Autodetect of this
264 part doesn't work, but we know it is really there. So let's look for
265 the autodetect attempt and just return success if we see that. */
266 static int i2c_hack_wm8775(struct pvr2_hdw
*hdw
,
267 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
269 if (!(rlen
|| wlen
)) {
270 // This is a probe attempt. Just let it succeed.
273 return pvr2_i2c_basic_op(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
276 /* This is a special entry point that is entered if an I2C operation is
277 attempted to a cx25840 chip on model 24xxx hardware. This chip can
278 sometimes wedge itself. Worse still, when this happens msp3400 can
279 falsely detect this part and then the system gets hosed up after msp3400
280 gets confused and dies. What we want to do here is try to keep msp3400
281 away and also try to notice if the chip is wedged and send a warning to
283 static int i2c_hack_cx25840(struct pvr2_hdw
*hdw
,
284 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
287 unsigned int subaddr
;
289 int state
= hdw
->i2c_cx25840_hack_state
;
291 if (!(rlen
|| wlen
)) {
292 // Probe attempt - always just succeed and don't bother the
293 // hardware (this helps to make the state machine further
294 // down somewhat easier).
299 return pvr2_i2c_basic_op(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
302 /* We're looking for the exact pattern where the revision register
303 is being read. The cx25840 module will always look at the
304 revision register first. Any other pattern of access therefore
305 has to be a probe attempt from somebody else so we'll reject it.
306 Normally we could just let each client just probe the part
307 anyway, but when the cx25840 is wedged, msp3400 will get a false
308 positive and that just screws things up... */
312 case 1: subaddr
= 0x0100; break;
313 case 2: subaddr
= 0x0101; break;
316 } else if (wlen
== 2) {
317 subaddr
= (wdata
[0] << 8) | wdata
[1];
319 case 0x0100: state
= 1; break;
320 case 0x0101: state
= 2; break;
326 if (!rlen
) goto success
;
328 if (rlen
!= 1) goto fail
;
330 /* If we get to here then we have a legitimate read for one of the
331 two revision bytes, so pass it through. */
332 wbuf
[0] = subaddr
>> 8;
334 ret
= pvr2_i2c_basic_op(hdw
,i2c_addr
,wbuf
,2,rdata
,rlen
);
336 if ((ret
!= 0) || (*rdata
== 0x04) || (*rdata
== 0x0a)) {
337 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
338 "WARNING: Detected a wedged cx25840 chip;"
339 " the device will not work.");
340 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
341 "WARNING: Try power cycling the pvrusb2 device.");
342 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
343 "WARNING: Disabling further access to the device"
344 " to prevent other foul-ups.");
345 // This blocks all further communication with the part.
346 hdw
->i2c_func
[0x44] = NULL
;
347 pvr2_hdw_render_useless(hdw
);
352 pvr2_trace(PVR2_TRACE_CHIPS
,"cx25840 appears to be OK.");
356 hdw
->i2c_cx25840_hack_state
= state
;
360 hdw
->i2c_cx25840_hack_state
= state
;
364 /* This is a very, very limited I2C adapter implementation. We can only
365 support what we actually know will work on the device... */
366 static int pvr2_i2c_xfer(struct i2c_adapter
*i2c_adap
,
367 struct i2c_msg msgs
[],
371 pvr2_i2c_func funcp
= NULL
;
372 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(i2c_adap
->algo_data
);
378 if ((msgs
[0].flags
& I2C_M_NOSTART
)) {
379 trace_i2c("i2c refusing I2C_M_NOSTART");
382 if (msgs
[0].addr
< PVR2_I2C_FUNC_CNT
) {
383 funcp
= hdw
->i2c_func
[msgs
[0].addr
];
391 if (msgs
[0].flags
& I2C_M_RD
) {
395 /* Length == 0 read. This is a probe. */
396 if (funcp(hdw
,msgs
[0].addr
,NULL
,0,NULL
,0)) {
403 /* If the read is short enough we'll do the whole
404 thing atomically. Otherwise we have no choice
405 but to break apart the reads. */
410 if (bcnt
> sizeof(hdw
->cmd_buffer
)-1) {
411 bcnt
= sizeof(hdw
->cmd_buffer
)-1;
413 if (funcp(hdw
,msgs
[0].addr
,NULL
,0,
414 msgs
[0].buf
+offs
,bcnt
)) {
426 if (funcp(hdw
,msgs
[0].addr
,
427 msgs
[0].buf
,msgs
[0].len
,NULL
,0)) {
432 } else if (num
== 2) {
433 if (msgs
[0].addr
!= msgs
[1].addr
) {
434 trace_i2c("i2c refusing 2 phase transfer with"
435 " conflicting target addresses");
439 if ((!((msgs
[0].flags
& I2C_M_RD
))) &&
440 (msgs
[1].flags
& I2C_M_RD
)) {
441 u16 tcnt
,bcnt
,wcnt
,offs
;
442 /* Write followed by atomic read. If the read
443 portion is short enough we'll do the whole thing
444 atomically. Otherwise we have no choice but to
445 break apart the reads. */
449 while (tcnt
|| wcnt
) {
451 if (bcnt
> sizeof(hdw
->cmd_buffer
)-1) {
452 bcnt
= sizeof(hdw
->cmd_buffer
)-1;
454 if (funcp(hdw
,msgs
[0].addr
,
456 msgs
[1].buf
+offs
,bcnt
)) {
467 trace_i2c("i2c refusing complex transfer"
468 " read0=%d read1=%d",
469 (msgs
[0].flags
& I2C_M_RD
),
470 (msgs
[1].flags
& I2C_M_RD
));
473 trace_i2c("i2c refusing %d phase transfer",num
);
477 if (pvrusb2_debug
& PVR2_TRACE_I2C_TRAF
) {
478 unsigned int idx
,offs
,cnt
;
479 for (idx
= 0; idx
< num
; idx
++) {
482 "pvrusb2 i2c xfer %u/%u:"
483 " addr=0x%x len=%d %s%s",
487 (msgs
[idx
].flags
& I2C_M_RD
?
489 (msgs
[idx
].flags
& I2C_M_NOSTART
?
491 if ((ret
> 0) || !(msgs
[idx
].flags
& I2C_M_RD
)) {
492 if (cnt
> 8) cnt
= 8;
494 for (offs
= 0; offs
< (cnt
>8?8:cnt
); offs
++) {
495 if (offs
) printk(" ");
496 printk("%02x",msgs
[idx
].buf
[offs
]);
498 if (offs
< cnt
) printk(" ...");
502 printk(" result=%d",ret
);
508 "pvrusb2 i2c xfer null transfer result=%d\n",
515 static int pvr2_i2c_control(struct i2c_adapter
*adapter
,
516 unsigned int cmd
, unsigned long arg
)
521 static u32
pvr2_i2c_functionality(struct i2c_adapter
*adap
)
523 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
| I2C_FUNC_SMBUS_BYTE_DATA
;
526 static int pvr2_i2c_core_singleton(struct i2c_client
*cp
,
527 unsigned int cmd
,void *arg
)
530 if (!cp
) return -EINVAL
;
531 if (!(cp
->driver
)) return -EINVAL
;
532 if (!(cp
->driver
->command
)) return -EINVAL
;
533 if (!try_module_get(cp
->driver
->driver
.owner
)) return -EAGAIN
;
534 stat
= cp
->driver
->command(cp
,cmd
,arg
);
535 module_put(cp
->driver
->driver
.owner
);
539 int pvr2_i2c_client_cmd(struct pvr2_i2c_client
*cp
,unsigned int cmd
,void *arg
)
542 if (pvrusb2_debug
& PVR2_TRACE_I2C_CMD
) {
545 cnt
= pvr2_i2c_client_describe(cp
,PVR2_I2C_DETAIL_DEBUG
,
547 pvr2_trace(PVR2_TRACE_I2C_CMD
,
548 "i2c COMMAND (code=%u 0x%x) to %.*s",
551 stat
= pvr2_i2c_core_singleton(cp
->client
,cmd
,arg
);
552 if (pvrusb2_debug
& PVR2_TRACE_I2C_CMD
) {
555 cnt
= pvr2_i2c_client_describe(cp
,PVR2_I2C_DETAIL_DEBUG
,
557 pvr2_trace(PVR2_TRACE_I2C_CMD
,
558 "i2c COMMAND to %.*s (ret=%d)",cnt
,buf
,stat
);
563 int pvr2_i2c_core_cmd(struct pvr2_hdw
*hdw
,unsigned int cmd
,void *arg
)
565 struct list_head
*item
,*nc
;
566 struct pvr2_i2c_client
*cp
;
569 if (!hdw
) return stat
;
571 mutex_lock(&hdw
->i2c_list_lock
);
572 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
573 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
574 if (!cp
->recv_enable
) continue;
575 mutex_unlock(&hdw
->i2c_list_lock
);
576 stat
= pvr2_i2c_client_cmd(cp
,cmd
,arg
);
577 mutex_lock(&hdw
->i2c_list_lock
);
579 mutex_unlock(&hdw
->i2c_list_lock
);
584 static int handler_check(struct pvr2_i2c_client
*cp
)
586 struct pvr2_i2c_handler
*hp
= cp
->handler
;
588 if (!hp
->func_table
->check
) return 0;
589 return hp
->func_table
->check(hp
->func_data
) != 0;
595 void pvr2_i2c_core_status_poll(struct pvr2_hdw
*hdw
)
597 struct list_head
*item
;
598 struct pvr2_i2c_client
*cp
;
599 mutex_lock(&hdw
->i2c_list_lock
); do {
600 struct v4l2_tuner
*vtp
= &hdw
->tuner_signal_info
;
601 memset(vtp
,0,sizeof(*vtp
));
602 list_for_each(item
,&hdw
->i2c_clients
) {
603 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
604 if (!cp
->detected_flag
) continue;
605 if (!cp
->status_poll
) continue;
608 hdw
->tuner_signal_stale
= 0;
609 pvr2_trace(PVR2_TRACE_CHIPS
,"i2c status poll"
610 " type=%u strength=%u audio=0x%x cap=0x%x"
613 vtp
->signal
,vtp
->rxsubchans
,vtp
->capability
,
614 vtp
->rangelow
,vtp
->rangehigh
);
615 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
619 /* Issue various I2C operations to bring chip-level drivers into sync with
620 state stored in this driver. */
621 void pvr2_i2c_core_sync(struct pvr2_hdw
*hdw
)
625 struct list_head
*item
,*nc
;
626 struct pvr2_i2c_client
*cp
;
628 if (!hdw
->i2c_linked
) return;
629 if (!(hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
)) {
632 mutex_lock(&hdw
->i2c_list_lock
); do {
633 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: core_sync BEGIN");
634 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_DETECT
) {
635 /* One or more I2C clients have attached since we
636 last synced. So scan the list and identify the
640 unsigned long amask
= 0;
641 buf
= kmalloc(BUFSIZE
,GFP_KERNEL
);
642 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_DETECT");
643 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_DETECT
;
644 list_for_each(item
,&hdw
->i2c_clients
) {
645 cp
= list_entry(item
,struct pvr2_i2c_client
,
647 if (!cp
->detected_flag
) {
649 pvr2_i2c_probe(hdw
,cp
);
650 cp
->detected_flag
= !0;
654 cnt
= pvr2_i2c_client_describe(
659 trace_i2c("Probed: %.*s",cnt
,buf
);
660 if (handler_check(cp
)) {
661 hdw
->i2c_pend_types
|=
662 PVR2_I2C_PEND_CLIENT
;
665 hdw
->i2c_pend_mask
|= msk
;
666 hdw
->i2c_pend_types
|=
667 PVR2_I2C_PEND_REFRESH
;
669 amask
|= cp
->ctl_mask
;
671 hdw
->i2c_active_mask
= amask
;
674 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_STALE
) {
675 /* Need to do one or more global updates. Arrange
676 for this to happen. */
678 pvr2_trace(PVR2_TRACE_I2C_CORE
,
679 "i2c: PEND_STALE (0x%lx)",
680 hdw
->i2c_stale_mask
);
681 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_STALE
;
682 list_for_each(item
,&hdw
->i2c_clients
) {
683 cp
= list_entry(item
,struct pvr2_i2c_client
,
685 m2
= hdw
->i2c_stale_mask
;
687 m2
&= ~cp
->pend_mask
;
689 pvr2_trace(PVR2_TRACE_I2C_CORE
,
690 "i2c: cp=%p setting 0x%lx",
695 hdw
->i2c_pend_mask
|= hdw
->i2c_stale_mask
;
696 hdw
->i2c_stale_mask
= 0;
697 hdw
->i2c_pend_types
|= PVR2_I2C_PEND_REFRESH
;
699 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_CLIENT
) {
700 /* One or more client handlers are asking for an
701 update. Run through the list of known clients
702 and update each one. */
703 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_CLIENT");
704 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_CLIENT
;
705 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
706 cp
= list_entry(item
,struct pvr2_i2c_client
,
708 if (!cp
->handler
) continue;
709 if (!cp
->handler
->func_table
->update
) continue;
710 pvr2_trace(PVR2_TRACE_I2C_CORE
,
711 "i2c: cp=%p update",cp
);
712 mutex_unlock(&hdw
->i2c_list_lock
);
713 cp
->handler
->func_table
->update(
714 cp
->handler
->func_data
);
715 mutex_lock(&hdw
->i2c_list_lock
);
716 /* If client's update function set some
717 additional pending bits, account for that
719 if (cp
->pend_mask
& ~hdw
->i2c_pend_mask
) {
720 hdw
->i2c_pend_mask
|= cp
->pend_mask
;
721 hdw
->i2c_pend_types
|=
722 PVR2_I2C_PEND_REFRESH
;
726 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_REFRESH
) {
727 const struct pvr2_i2c_op
*opf
;
729 /* Some actual updates are pending. Walk through
730 each update type and perform it. */
731 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_REFRESH"
732 " (0x%lx)",hdw
->i2c_pend_mask
);
733 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_REFRESH
;
734 pm
= hdw
->i2c_pend_mask
;
735 hdw
->i2c_pend_mask
= 0;
736 for (idx
= 0, msk
= 1; pm
; idx
++, msk
<<= 1) {
737 if (!(pm
& msk
)) continue;
739 list_for_each(item
,&hdw
->i2c_clients
) {
740 cp
= list_entry(item
,
741 struct pvr2_i2c_client
,
743 if (cp
->pend_mask
& msk
) {
744 cp
->pend_mask
&= ~msk
;
745 cp
->recv_enable
= !0;
750 opf
= pvr2_i2c_get_op(idx
);
752 mutex_unlock(&hdw
->i2c_list_lock
);
754 mutex_lock(&hdw
->i2c_list_lock
);
757 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: core_sync END");
758 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
761 int pvr2_i2c_core_check_stale(struct pvr2_hdw
*hdw
)
763 unsigned long msk
,sm
,pm
;
765 const struct pvr2_i2c_op
*opf
;
766 struct list_head
*item
;
767 struct pvr2_i2c_client
*cp
;
770 pvr2_trace(PVR2_TRACE_I2C_CORE
,"pvr2_i2c_core_check_stale BEGIN");
772 pm
= hdw
->i2c_active_mask
;
774 for (idx
= 0, msk
= 1; pm
; idx
++, msk
<<= 1) {
775 if (!(msk
& pm
)) continue;
777 opf
= pvr2_i2c_get_op(idx
);
779 if (opf
->check(hdw
)) {
783 if (sm
) pt
|= PVR2_I2C_PEND_STALE
;
785 list_for_each(item
,&hdw
->i2c_clients
) {
786 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
787 if (!handler_check(cp
)) continue;
788 pt
|= PVR2_I2C_PEND_CLIENT
;
792 mutex_lock(&hdw
->i2c_list_lock
); do {
793 hdw
->i2c_pend_types
|= pt
;
794 hdw
->i2c_stale_mask
|= sm
;
795 hdw
->i2c_pend_mask
|= hdw
->i2c_stale_mask
;
796 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
799 pvr2_trace(PVR2_TRACE_I2C_CORE
,
800 "i2c: types=0x%x stale=0x%lx pend=0x%lx",
804 pvr2_trace(PVR2_TRACE_I2C_CORE
,"pvr2_i2c_core_check_stale END");
806 return (hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
) != 0;
809 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client
*cp
,
811 char *buf
,unsigned int maxlen
)
813 unsigned int ccnt
,bcnt
;
815 const struct pvr2_i2c_op
*opf
;
818 if (detail
& PVR2_I2C_DETAIL_DEBUG
) {
819 bcnt
= scnprintf(buf
,maxlen
,
820 "ctxt=%p ctl_mask=0x%lx",
822 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
825 bcnt
= scnprintf(buf
,maxlen
,
830 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
831 if ((detail
& PVR2_I2C_DETAIL_HANDLER
) &&
832 cp
->handler
&& cp
->handler
->func_table
->describe
) {
833 bcnt
= scnprintf(buf
,maxlen
," (");
834 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
835 bcnt
= cp
->handler
->func_table
->describe(
836 cp
->handler
->func_data
,buf
,maxlen
);
837 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
838 bcnt
= scnprintf(buf
,maxlen
,")");
839 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
841 if ((detail
& PVR2_I2C_DETAIL_CTLMASK
) && cp
->ctl_mask
) {
843 unsigned long msk
,sm
;
845 bcnt
= scnprintf(buf
,maxlen
," [");
846 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
849 for (idx
= 0, msk
= 1; msk
; idx
++, msk
<<= 1) {
850 if (!(cp
->ctl_mask
& msk
)) continue;
851 opf
= pvr2_i2c_get_op(idx
);
853 bcnt
= scnprintf(buf
,maxlen
,"%s%s",
856 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
863 bcnt
= scnprintf(buf
,maxlen
,"%s%lx",
864 idx
!= 0 ? " " : "",sm
);
865 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
867 bcnt
= scnprintf(buf
,maxlen
,"]");
868 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
873 unsigned int pvr2_i2c_report(struct pvr2_hdw
*hdw
,
874 char *buf
,unsigned int maxlen
)
876 unsigned int ccnt
,bcnt
;
877 struct list_head
*item
;
878 struct pvr2_i2c_client
*cp
;
880 mutex_lock(&hdw
->i2c_list_lock
); do {
881 list_for_each(item
,&hdw
->i2c_clients
) {
882 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
883 bcnt
= pvr2_i2c_client_describe(
885 (PVR2_I2C_DETAIL_HANDLER
|
886 PVR2_I2C_DETAIL_CTLMASK
),
888 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
889 bcnt
= scnprintf(buf
,maxlen
,"\n");
890 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
892 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
896 static int pvr2_i2c_attach_inform(struct i2c_client
*client
)
898 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(client
->adapter
->algo_data
);
899 struct pvr2_i2c_client
*cp
;
900 int fl
= !(hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
);
901 cp
= kzalloc(sizeof(*cp
),GFP_KERNEL
);
902 trace_i2c("i2c_attach [client=%s @ 0x%x ctxt=%p]",
905 if (!cp
) return -ENOMEM
;
907 INIT_LIST_HEAD(&cp
->list
);
909 mutex_lock(&hdw
->i2c_list_lock
); do {
910 list_add_tail(&cp
->list
,&hdw
->i2c_clients
);
911 hdw
->i2c_pend_types
|= PVR2_I2C_PEND_DETECT
;
912 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
913 if (fl
) pvr2_hdw_poll_trigger_unlocked(hdw
);
917 static int pvr2_i2c_detach_inform(struct i2c_client
*client
)
919 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(client
->adapter
->algo_data
);
920 struct pvr2_i2c_client
*cp
;
921 struct list_head
*item
,*nc
;
922 unsigned long amask
= 0;
924 mutex_lock(&hdw
->i2c_list_lock
); do {
925 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
926 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
927 if (cp
->client
== client
) {
928 trace_i2c("pvr2_i2c_detach"
929 " [client=%s @ 0x%x ctxt=%p]",
933 cp
->handler
->func_table
->detach
) {
934 cp
->handler
->func_table
->detach(
935 cp
->handler
->func_data
);
942 amask
|= cp
->ctl_mask
;
944 hdw
->i2c_active_mask
= amask
;
945 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
947 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
954 static struct i2c_algorithm pvr2_i2c_algo_template
= {
955 .master_xfer
= pvr2_i2c_xfer
,
956 .algo_control
= pvr2_i2c_control
,
957 .functionality
= pvr2_i2c_functionality
,
960 static struct i2c_adapter pvr2_i2c_adap_template
= {
961 .owner
= THIS_MODULE
,
962 .class = I2C_CLASS_TV_ANALOG
,
963 .id
= I2C_HW_B_BT848
,
964 .client_register
= pvr2_i2c_attach_inform
,
965 .client_unregister
= pvr2_i2c_detach_inform
,
968 static void do_i2c_scan(struct pvr2_hdw
*hdw
)
970 struct i2c_msg msg
[1];
973 msg
[0].flags
= I2C_M_RD
;
976 printk("%s: i2c scan beginning\n",hdw
->name
);
977 for (i
= 0; i
< 128; i
++) {
979 rc
= i2c_transfer(&hdw
->i2c_adap
,msg
, ARRAY_SIZE(msg
));
980 if (rc
!= 1) continue;
981 printk("%s: i2c scan: found device @ 0x%x\n",hdw
->name
,i
);
983 printk("%s: i2c scan done.\n",hdw
->name
);
986 void pvr2_i2c_core_init(struct pvr2_hdw
*hdw
)
990 /* The default action for all possible I2C addresses is just to do
991 the transfer normally. */
992 for (idx
= 0; idx
< PVR2_I2C_FUNC_CNT
; idx
++) {
993 hdw
->i2c_func
[idx
] = pvr2_i2c_basic_op
;
996 /* However, deal with various special cases for 24xxx hardware. */
997 if (hdw
->hdw_type
== PVR2_HDW_TYPE_24XXX
) {
998 hdw
->i2c_func
[0x1b] = i2c_hack_wm8775
;
999 hdw
->i2c_func
[0x44] = i2c_hack_cx25840
;
1000 hdw
->i2c_func
[0x18] = i2c_24xxx_ir
;
1003 // Configure the adapter and set up everything else related to it.
1004 memcpy(&hdw
->i2c_adap
,&pvr2_i2c_adap_template
,sizeof(hdw
->i2c_adap
));
1005 memcpy(&hdw
->i2c_algo
,&pvr2_i2c_algo_template
,sizeof(hdw
->i2c_algo
));
1006 strlcpy(hdw
->i2c_adap
.name
,hdw
->name
,sizeof(hdw
->i2c_adap
.name
));
1007 hdw
->i2c_adap
.dev
.parent
= &hdw
->usb_dev
->dev
;
1008 hdw
->i2c_adap
.algo
= &hdw
->i2c_algo
;
1009 hdw
->i2c_adap
.algo_data
= hdw
;
1010 hdw
->i2c_pend_mask
= 0;
1011 hdw
->i2c_stale_mask
= 0;
1012 hdw
->i2c_active_mask
= 0;
1013 INIT_LIST_HEAD(&hdw
->i2c_clients
);
1014 mutex_init(&hdw
->i2c_list_lock
);
1015 hdw
->i2c_linked
= !0;
1016 i2c_add_adapter(&hdw
->i2c_adap
);
1017 if (i2c_scan
) do_i2c_scan(hdw
);
1020 void pvr2_i2c_core_done(struct pvr2_hdw
*hdw
)
1022 if (hdw
->i2c_linked
) {
1023 i2c_del_adapter(&hdw
->i2c_adap
);
1024 hdw
->i2c_linked
= 0;
1029 Stuff for Emacs to see, in order to encourage consistent editing style:
1030 *** Local Variables: ***
1032 *** fill-column: 75 ***
1033 *** tab-width: 8 ***
1034 *** c-basic-offset: 8 ***