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"
28 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
32 This module attempts to implement a compliant I2C adapter for the pvrusb2
33 device. By doing this we can then make use of existing functionality in
34 V4L (e.g. tuner.c) rather than rolling our own.
38 static unsigned int i2c_scan
= 0;
39 module_param(i2c_scan
, int, S_IRUGO
|S_IWUSR
);
40 MODULE_PARM_DESC(i2c_scan
,"scan i2c bus at insmod time");
42 static int ir_mode
[PVR_NUM
] = { [0 ... PVR_NUM
-1] = 1 };
43 module_param_array(ir_mode
, int, NULL
, 0444);
44 MODULE_PARM_DESC(ir_mode
,"specify: 0=disable IR reception, 1=normal IR");
46 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client
*cp
,
48 char *buf
,unsigned int maxlen
);
50 static int pvr2_i2c_write(struct pvr2_hdw
*hdw
, /* Context */
51 u8 i2c_addr
, /* I2C address we're talking to */
52 u8
*data
, /* Data to write */
53 u16 length
) /* Size of data to write */
55 /* Return value - default 0 means success */
59 if (!data
) length
= 0;
60 if (length
> (sizeof(hdw
->cmd_buffer
) - 3)) {
61 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
62 "Killing an I2C write to %u that is too large"
63 " (desired=%u limit=%u)",
65 length
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 3));
69 LOCK_TAKE(hdw
->ctl_lock
);
71 /* Clear the command buffer (likely to be paranoia) */
72 memset(hdw
->cmd_buffer
, 0, sizeof(hdw
->cmd_buffer
));
74 /* Set up command buffer for an I2C write */
75 hdw
->cmd_buffer
[0] = FX2CMD_I2C_WRITE
; /* write prefix */
76 hdw
->cmd_buffer
[1] = i2c_addr
; /* i2c addr of chip */
77 hdw
->cmd_buffer
[2] = length
; /* length of what follows */
78 if (length
) memcpy(hdw
->cmd_buffer
+ 3, data
, length
);
80 /* Do the operation */
81 ret
= pvr2_send_request(hdw
,
87 if (hdw
->cmd_buffer
[0] != 8) {
89 if (hdw
->cmd_buffer
[0] != 7) {
90 trace_i2c("unexpected status"
91 " from i2_write[%d]: %d",
92 i2c_addr
,hdw
->cmd_buffer
[0]);
97 LOCK_GIVE(hdw
->ctl_lock
);
102 static int pvr2_i2c_read(struct pvr2_hdw
*hdw
, /* Context */
103 u8 i2c_addr
, /* I2C address we're talking to */
104 u8
*data
, /* Data to write */
105 u16 dlen
, /* Size of data to write */
106 u8
*res
, /* Where to put data we read */
107 u16 rlen
) /* Amount of data to read */
109 /* Return value - default 0 means success */
114 if (dlen
> (sizeof(hdw
->cmd_buffer
) - 4)) {
115 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
116 "Killing an I2C read to %u that has wlen too large"
117 " (desired=%u limit=%u)",
119 dlen
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 4));
122 if (res
&& (rlen
> (sizeof(hdw
->cmd_buffer
) - 1))) {
123 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
124 "Killing an I2C read to %u that has rlen too large"
125 " (desired=%u limit=%u)",
127 rlen
,(unsigned int)(sizeof(hdw
->cmd_buffer
) - 1));
131 LOCK_TAKE(hdw
->ctl_lock
);
133 /* Clear the command buffer (likely to be paranoia) */
134 memset(hdw
->cmd_buffer
, 0, sizeof(hdw
->cmd_buffer
));
136 /* Set up command buffer for an I2C write followed by a read */
137 hdw
->cmd_buffer
[0] = FX2CMD_I2C_READ
; /* read prefix */
138 hdw
->cmd_buffer
[1] = dlen
; /* arg length */
139 hdw
->cmd_buffer
[2] = rlen
; /* answer length. Device will send one
140 more byte (status). */
141 hdw
->cmd_buffer
[3] = i2c_addr
; /* i2c addr of chip */
142 if (dlen
) memcpy(hdw
->cmd_buffer
+ 4, data
, dlen
);
144 /* Do the operation */
145 ret
= pvr2_send_request(hdw
,
151 if (hdw
->cmd_buffer
[0] != 8) {
153 if (hdw
->cmd_buffer
[0] != 7) {
154 trace_i2c("unexpected status"
155 " from i2_read[%d]: %d",
156 i2c_addr
,hdw
->cmd_buffer
[0]);
161 /* Copy back the result */
164 /* Error, just blank out the return buffer */
165 memset(res
, 0, rlen
);
167 memcpy(res
, hdw
->cmd_buffer
+ 1, rlen
);
171 LOCK_GIVE(hdw
->ctl_lock
);
176 /* This is the common low level entry point for doing I2C operations to the
178 static int pvr2_i2c_basic_op(struct pvr2_hdw
*hdw
,
185 if (!rdata
) rlen
= 0;
186 if (!wdata
) wlen
= 0;
188 return pvr2_i2c_read(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
190 return pvr2_i2c_write(hdw
,i2c_addr
,wdata
,wlen
);
195 /* This is a special entry point for cases of I2C transaction attempts to
196 the IR receiver. The implementation here simulates the IR receiver by
197 issuing a command to the FX2 firmware and using that response to return
198 what the real I2C receiver would have returned. We use this for 24xxx
199 devices, where the IR receiver chip has been removed and replaced with
200 FX2 related logic. */
201 static int i2c_24xxx_ir(struct pvr2_hdw
*hdw
,
202 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
207 if (!(rlen
|| wlen
)) {
208 /* This is a probe attempt. Just let it succeed. */
212 /* We don't understand this kind of transaction */
213 if ((wlen
!= 0) || (rlen
== 0)) return -EIO
;
216 /* Mike Isely <isely@pobox.com> Appears to be a probe
217 attempt from lirc. Just fill in zeroes and return. If
218 we try instead to do the full transaction here, then bad
219 things seem to happen within the lirc driver module
220 (version 0.8.0-7 sources from Debian, when run under
221 vanilla 2.6.17.6 kernel) - and I don't have the patience
223 if (rlen
> 0) rdata
[0] = 0;
224 if (rlen
> 1) rdata
[1] = 0;
228 /* Issue a command to the FX2 to read the IR receiver. */
229 LOCK_TAKE(hdw
->ctl_lock
); do {
230 hdw
->cmd_buffer
[0] = FX2CMD_GET_IR_CODE
;
231 stat
= pvr2_send_request(hdw
,
234 dat
[0] = hdw
->cmd_buffer
[0];
235 dat
[1] = hdw
->cmd_buffer
[1];
236 dat
[2] = hdw
->cmd_buffer
[2];
237 dat
[3] = hdw
->cmd_buffer
[3];
238 } while (0); LOCK_GIVE(hdw
->ctl_lock
);
240 /* Give up if that operation failed. */
241 if (stat
!= 0) return stat
;
243 /* Mangle the results into something that looks like the real IR
247 /* No code received. */
252 /* Mash the FX2 firmware-provided IR code into something
253 that the normal i2c chip-level driver expects. */
260 rdata
[0] = (val
>> 8) & 0xffu
;
261 rdata
[1] = val
& 0xffu
;
267 /* This is a special entry point that is entered if an I2C operation is
268 attempted to a wm8775 chip on model 24xxx hardware. Autodetect of this
269 part doesn't work, but we know it is really there. So let's look for
270 the autodetect attempt and just return success if we see that. */
271 static int i2c_hack_wm8775(struct pvr2_hdw
*hdw
,
272 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
274 if (!(rlen
|| wlen
)) {
275 // This is a probe attempt. Just let it succeed.
278 return pvr2_i2c_basic_op(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
281 /* This is an entry point designed to always fail any attempt to perform a
282 transfer. We use this to cause certain I2C addresses to not be
284 static int i2c_black_hole(struct pvr2_hdw
*hdw
,
285 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
290 /* This is a special entry point that is entered if an I2C operation is
291 attempted to a cx25840 chip on model 24xxx hardware. This chip can
292 sometimes wedge itself. Worse still, when this happens msp3400 can
293 falsely detect this part and then the system gets hosed up after msp3400
294 gets confused and dies. What we want to do here is try to keep msp3400
295 away and also try to notice if the chip is wedged and send a warning to
297 static int i2c_hack_cx25840(struct pvr2_hdw
*hdw
,
298 u8 i2c_addr
,u8
*wdata
,u16 wlen
,u8
*rdata
,u16 rlen
)
301 unsigned int subaddr
;
303 int state
= hdw
->i2c_cx25840_hack_state
;
305 if (!(rlen
|| wlen
)) {
306 // Probe attempt - always just succeed and don't bother the
307 // hardware (this helps to make the state machine further
308 // down somewhat easier).
313 return pvr2_i2c_basic_op(hdw
,i2c_addr
,wdata
,wlen
,rdata
,rlen
);
316 /* We're looking for the exact pattern where the revision register
317 is being read. The cx25840 module will always look at the
318 revision register first. Any other pattern of access therefore
319 has to be a probe attempt from somebody else so we'll reject it.
320 Normally we could just let each client just probe the part
321 anyway, but when the cx25840 is wedged, msp3400 will get a false
322 positive and that just screws things up... */
326 case 1: subaddr
= 0x0100; break;
327 case 2: subaddr
= 0x0101; break;
330 } else if (wlen
== 2) {
331 subaddr
= (wdata
[0] << 8) | wdata
[1];
333 case 0x0100: state
= 1; break;
334 case 0x0101: state
= 2; break;
340 if (!rlen
) goto success
;
342 if (rlen
!= 1) goto fail
;
344 /* If we get to here then we have a legitimate read for one of the
345 two revision bytes, so pass it through. */
346 wbuf
[0] = subaddr
>> 8;
348 ret
= pvr2_i2c_basic_op(hdw
,i2c_addr
,wbuf
,2,rdata
,rlen
);
350 if ((ret
!= 0) || (*rdata
== 0x04) || (*rdata
== 0x0a)) {
351 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
352 "WARNING: Detected a wedged cx25840 chip;"
353 " the device will not work.");
354 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
355 "WARNING: Try power cycling the pvrusb2 device.");
356 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
357 "WARNING: Disabling further access to the device"
358 " to prevent other foul-ups.");
359 // This blocks all further communication with the part.
360 hdw
->i2c_func
[0x44] = NULL
;
361 pvr2_hdw_render_useless(hdw
);
366 pvr2_trace(PVR2_TRACE_CHIPS
,"cx25840 appears to be OK.");
370 hdw
->i2c_cx25840_hack_state
= state
;
374 hdw
->i2c_cx25840_hack_state
= state
;
378 /* This is a very, very limited I2C adapter implementation. We can only
379 support what we actually know will work on the device... */
380 static int pvr2_i2c_xfer(struct i2c_adapter
*i2c_adap
,
381 struct i2c_msg msgs
[],
385 pvr2_i2c_func funcp
= NULL
;
386 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(i2c_adap
->algo_data
);
392 if (msgs
[0].addr
< PVR2_I2C_FUNC_CNT
) {
393 funcp
= hdw
->i2c_func
[msgs
[0].addr
];
401 if (msgs
[0].flags
& I2C_M_RD
) {
405 /* Length == 0 read. This is a probe. */
406 if (funcp(hdw
,msgs
[0].addr
,NULL
,0,NULL
,0)) {
413 /* If the read is short enough we'll do the whole
414 thing atomically. Otherwise we have no choice
415 but to break apart the reads. */
420 if (bcnt
> sizeof(hdw
->cmd_buffer
)-1) {
421 bcnt
= sizeof(hdw
->cmd_buffer
)-1;
423 if (funcp(hdw
,msgs
[0].addr
,NULL
,0,
424 msgs
[0].buf
+offs
,bcnt
)) {
436 if (funcp(hdw
,msgs
[0].addr
,
437 msgs
[0].buf
,msgs
[0].len
,NULL
,0)) {
442 } else if (num
== 2) {
443 if (msgs
[0].addr
!= msgs
[1].addr
) {
444 trace_i2c("i2c refusing 2 phase transfer with"
445 " conflicting target addresses");
449 if ((!((msgs
[0].flags
& I2C_M_RD
))) &&
450 (msgs
[1].flags
& I2C_M_RD
)) {
451 u16 tcnt
,bcnt
,wcnt
,offs
;
452 /* Write followed by atomic read. If the read
453 portion is short enough we'll do the whole thing
454 atomically. Otherwise we have no choice but to
455 break apart the reads. */
459 while (tcnt
|| wcnt
) {
461 if (bcnt
> sizeof(hdw
->cmd_buffer
)-1) {
462 bcnt
= sizeof(hdw
->cmd_buffer
)-1;
464 if (funcp(hdw
,msgs
[0].addr
,
466 msgs
[1].buf
+offs
,bcnt
)) {
477 trace_i2c("i2c refusing complex transfer"
478 " read0=%d read1=%d",
479 (msgs
[0].flags
& I2C_M_RD
),
480 (msgs
[1].flags
& I2C_M_RD
));
483 trace_i2c("i2c refusing %d phase transfer",num
);
487 if (pvrusb2_debug
& PVR2_TRACE_I2C_TRAF
) {
488 unsigned int idx
,offs
,cnt
;
489 for (idx
= 0; idx
< num
; idx
++) {
492 "pvrusb2 i2c xfer %u/%u:"
493 " addr=0x%x len=%d %s",
497 (msgs
[idx
].flags
& I2C_M_RD
?
499 if ((ret
> 0) || !(msgs
[idx
].flags
& I2C_M_RD
)) {
500 if (cnt
> 8) cnt
= 8;
502 for (offs
= 0; offs
< (cnt
>8?8:cnt
); offs
++) {
503 if (offs
) printk(" ");
504 printk("%02x",msgs
[idx
].buf
[offs
]);
506 if (offs
< cnt
) printk(" ...");
510 printk(" result=%d",ret
);
516 "pvrusb2 i2c xfer null transfer result=%d\n",
523 static int pvr2_i2c_control(struct i2c_adapter
*adapter
,
524 unsigned int cmd
, unsigned long arg
)
529 static u32
pvr2_i2c_functionality(struct i2c_adapter
*adap
)
531 return I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_I2C
;
534 static int pvr2_i2c_core_singleton(struct i2c_client
*cp
,
535 unsigned int cmd
,void *arg
)
538 if (!cp
) return -EINVAL
;
539 if (!(cp
->driver
)) return -EINVAL
;
540 if (!(cp
->driver
->command
)) return -EINVAL
;
541 if (!try_module_get(cp
->driver
->driver
.owner
)) return -EAGAIN
;
542 stat
= cp
->driver
->command(cp
,cmd
,arg
);
543 module_put(cp
->driver
->driver
.owner
);
547 int pvr2_i2c_client_cmd(struct pvr2_i2c_client
*cp
,unsigned int cmd
,void *arg
)
550 if (pvrusb2_debug
& PVR2_TRACE_I2C_CMD
) {
553 cnt
= pvr2_i2c_client_describe(cp
,PVR2_I2C_DETAIL_DEBUG
,
555 pvr2_trace(PVR2_TRACE_I2C_CMD
,
556 "i2c COMMAND (code=%u 0x%x) to %.*s",
559 stat
= pvr2_i2c_core_singleton(cp
->client
,cmd
,arg
);
560 if (pvrusb2_debug
& PVR2_TRACE_I2C_CMD
) {
563 cnt
= pvr2_i2c_client_describe(cp
,PVR2_I2C_DETAIL_DEBUG
,
565 pvr2_trace(PVR2_TRACE_I2C_CMD
,
566 "i2c COMMAND to %.*s (ret=%d)",cnt
,buf
,stat
);
571 int pvr2_i2c_core_cmd(struct pvr2_hdw
*hdw
,unsigned int cmd
,void *arg
)
573 struct list_head
*item
,*nc
;
574 struct pvr2_i2c_client
*cp
;
577 if (!hdw
) return stat
;
579 mutex_lock(&hdw
->i2c_list_lock
);
580 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
581 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
582 if (!cp
->recv_enable
) continue;
583 mutex_unlock(&hdw
->i2c_list_lock
);
584 stat
= pvr2_i2c_client_cmd(cp
,cmd
,arg
);
585 mutex_lock(&hdw
->i2c_list_lock
);
587 mutex_unlock(&hdw
->i2c_list_lock
);
592 static int handler_check(struct pvr2_i2c_client
*cp
)
594 struct pvr2_i2c_handler
*hp
= cp
->handler
;
596 if (!hp
->func_table
->check
) return 0;
597 return hp
->func_table
->check(hp
->func_data
) != 0;
603 void pvr2_i2c_core_status_poll(struct pvr2_hdw
*hdw
)
605 struct list_head
*item
;
606 struct pvr2_i2c_client
*cp
;
607 mutex_lock(&hdw
->i2c_list_lock
); do {
608 struct v4l2_tuner
*vtp
= &hdw
->tuner_signal_info
;
609 memset(vtp
,0,sizeof(*vtp
));
610 list_for_each(item
,&hdw
->i2c_clients
) {
611 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
612 if (!cp
->detected_flag
) continue;
613 if (!cp
->status_poll
) continue;
616 hdw
->tuner_signal_stale
= 0;
617 pvr2_trace(PVR2_TRACE_CHIPS
,"i2c status poll"
618 " type=%u strength=%u audio=0x%x cap=0x%x"
621 vtp
->signal
,vtp
->rxsubchans
,vtp
->capability
,
622 vtp
->rangelow
,vtp
->rangehigh
);
623 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
627 /* Issue various I2C operations to bring chip-level drivers into sync with
628 state stored in this driver. */
629 void pvr2_i2c_core_sync(struct pvr2_hdw
*hdw
)
633 struct list_head
*item
,*nc
;
634 struct pvr2_i2c_client
*cp
;
636 if (!hdw
->i2c_linked
) return;
637 if (!(hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
)) {
640 mutex_lock(&hdw
->i2c_list_lock
); do {
641 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: core_sync BEGIN");
642 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_DETECT
) {
643 /* One or more I2C clients have attached since we
644 last synced. So scan the list and identify the
648 unsigned long amask
= 0;
649 buf
= kmalloc(BUFSIZE
,GFP_KERNEL
);
650 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_DETECT");
651 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_DETECT
;
652 list_for_each(item
,&hdw
->i2c_clients
) {
653 cp
= list_entry(item
,struct pvr2_i2c_client
,
655 if (!cp
->detected_flag
) {
657 pvr2_i2c_probe(hdw
,cp
);
658 cp
->detected_flag
= !0;
662 cnt
= pvr2_i2c_client_describe(
667 trace_i2c("Probed: %.*s",cnt
,buf
);
668 if (handler_check(cp
)) {
669 hdw
->i2c_pend_types
|=
670 PVR2_I2C_PEND_CLIENT
;
673 hdw
->i2c_pend_mask
|= msk
;
674 hdw
->i2c_pend_types
|=
675 PVR2_I2C_PEND_REFRESH
;
677 amask
|= cp
->ctl_mask
;
679 hdw
->i2c_active_mask
= amask
;
682 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_STALE
) {
683 /* Need to do one or more global updates. Arrange
684 for this to happen. */
686 pvr2_trace(PVR2_TRACE_I2C_CORE
,
687 "i2c: PEND_STALE (0x%lx)",
688 hdw
->i2c_stale_mask
);
689 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_STALE
;
690 list_for_each(item
,&hdw
->i2c_clients
) {
691 cp
= list_entry(item
,struct pvr2_i2c_client
,
693 m2
= hdw
->i2c_stale_mask
;
695 m2
&= ~cp
->pend_mask
;
697 pvr2_trace(PVR2_TRACE_I2C_CORE
,
698 "i2c: cp=%p setting 0x%lx",
703 hdw
->i2c_pend_mask
|= hdw
->i2c_stale_mask
;
704 hdw
->i2c_stale_mask
= 0;
705 hdw
->i2c_pend_types
|= PVR2_I2C_PEND_REFRESH
;
707 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_CLIENT
) {
708 /* One or more client handlers are asking for an
709 update. Run through the list of known clients
710 and update each one. */
711 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_CLIENT");
712 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_CLIENT
;
713 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
714 cp
= list_entry(item
,struct pvr2_i2c_client
,
716 if (!cp
->handler
) continue;
717 if (!cp
->handler
->func_table
->update
) continue;
718 pvr2_trace(PVR2_TRACE_I2C_CORE
,
719 "i2c: cp=%p update",cp
);
720 mutex_unlock(&hdw
->i2c_list_lock
);
721 cp
->handler
->func_table
->update(
722 cp
->handler
->func_data
);
723 mutex_lock(&hdw
->i2c_list_lock
);
724 /* If client's update function set some
725 additional pending bits, account for that
727 if (cp
->pend_mask
& ~hdw
->i2c_pend_mask
) {
728 hdw
->i2c_pend_mask
|= cp
->pend_mask
;
729 hdw
->i2c_pend_types
|=
730 PVR2_I2C_PEND_REFRESH
;
734 if (hdw
->i2c_pend_types
& PVR2_I2C_PEND_REFRESH
) {
735 const struct pvr2_i2c_op
*opf
;
737 /* Some actual updates are pending. Walk through
738 each update type and perform it. */
739 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: PEND_REFRESH"
740 " (0x%lx)",hdw
->i2c_pend_mask
);
741 hdw
->i2c_pend_types
&= ~PVR2_I2C_PEND_REFRESH
;
742 pm
= hdw
->i2c_pend_mask
;
743 hdw
->i2c_pend_mask
= 0;
744 for (idx
= 0, msk
= 1; pm
; idx
++, msk
<<= 1) {
745 if (!(pm
& msk
)) continue;
747 list_for_each(item
,&hdw
->i2c_clients
) {
748 cp
= list_entry(item
,
749 struct pvr2_i2c_client
,
751 if (cp
->pend_mask
& msk
) {
752 cp
->pend_mask
&= ~msk
;
753 cp
->recv_enable
= !0;
758 opf
= pvr2_i2c_get_op(idx
);
760 mutex_unlock(&hdw
->i2c_list_lock
);
762 mutex_lock(&hdw
->i2c_list_lock
);
765 pvr2_trace(PVR2_TRACE_I2C_CORE
,"i2c: core_sync END");
766 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
769 int pvr2_i2c_core_check_stale(struct pvr2_hdw
*hdw
)
771 unsigned long msk
,sm
,pm
;
773 const struct pvr2_i2c_op
*opf
;
774 struct list_head
*item
;
775 struct pvr2_i2c_client
*cp
;
778 pvr2_trace(PVR2_TRACE_I2C_CORE
,"pvr2_i2c_core_check_stale BEGIN");
780 pm
= hdw
->i2c_active_mask
;
782 for (idx
= 0, msk
= 1; pm
; idx
++, msk
<<= 1) {
783 if (!(msk
& pm
)) continue;
785 opf
= pvr2_i2c_get_op(idx
);
787 if (opf
->check(hdw
)) {
791 if (sm
) pt
|= PVR2_I2C_PEND_STALE
;
793 list_for_each(item
,&hdw
->i2c_clients
) {
794 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
795 if (!handler_check(cp
)) continue;
796 pt
|= PVR2_I2C_PEND_CLIENT
;
800 mutex_lock(&hdw
->i2c_list_lock
); do {
801 hdw
->i2c_pend_types
|= pt
;
802 hdw
->i2c_stale_mask
|= sm
;
803 hdw
->i2c_pend_mask
|= hdw
->i2c_stale_mask
;
804 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
807 pvr2_trace(PVR2_TRACE_I2C_CORE
,
808 "i2c: types=0x%x stale=0x%lx pend=0x%lx",
812 pvr2_trace(PVR2_TRACE_I2C_CORE
,"pvr2_i2c_core_check_stale END");
814 return (hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
) != 0;
817 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client
*cp
,
819 char *buf
,unsigned int maxlen
)
821 unsigned int ccnt
,bcnt
;
823 const struct pvr2_i2c_op
*opf
;
826 if (detail
& PVR2_I2C_DETAIL_DEBUG
) {
827 bcnt
= scnprintf(buf
,maxlen
,
828 "ctxt=%p ctl_mask=0x%lx",
830 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
833 bcnt
= scnprintf(buf
,maxlen
,
838 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
839 if ((detail
& PVR2_I2C_DETAIL_HANDLER
) &&
840 cp
->handler
&& cp
->handler
->func_table
->describe
) {
841 bcnt
= scnprintf(buf
,maxlen
," (");
842 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
843 bcnt
= cp
->handler
->func_table
->describe(
844 cp
->handler
->func_data
,buf
,maxlen
);
845 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
846 bcnt
= scnprintf(buf
,maxlen
,")");
847 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
849 if ((detail
& PVR2_I2C_DETAIL_CTLMASK
) && cp
->ctl_mask
) {
851 unsigned long msk
,sm
;
853 bcnt
= scnprintf(buf
,maxlen
," [");
854 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
857 for (idx
= 0, msk
= 1; msk
; idx
++, msk
<<= 1) {
858 if (!(cp
->ctl_mask
& msk
)) continue;
859 opf
= pvr2_i2c_get_op(idx
);
861 bcnt
= scnprintf(buf
,maxlen
,"%s%s",
864 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
871 bcnt
= scnprintf(buf
,maxlen
,"%s%lx",
872 idx
!= 0 ? " " : "",sm
);
873 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
875 bcnt
= scnprintf(buf
,maxlen
,"]");
876 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
881 unsigned int pvr2_i2c_report(struct pvr2_hdw
*hdw
,
882 char *buf
,unsigned int maxlen
)
884 unsigned int ccnt
,bcnt
;
885 struct list_head
*item
;
886 struct pvr2_i2c_client
*cp
;
888 mutex_lock(&hdw
->i2c_list_lock
); do {
889 list_for_each(item
,&hdw
->i2c_clients
) {
890 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
891 bcnt
= pvr2_i2c_client_describe(
893 (PVR2_I2C_DETAIL_HANDLER
|
894 PVR2_I2C_DETAIL_CTLMASK
),
896 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
897 bcnt
= scnprintf(buf
,maxlen
,"\n");
898 ccnt
+= bcnt
; buf
+= bcnt
; maxlen
-= bcnt
;
900 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
904 static int pvr2_i2c_attach_inform(struct i2c_client
*client
)
906 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(client
->adapter
->algo_data
);
907 struct pvr2_i2c_client
*cp
;
908 int fl
= !(hdw
->i2c_pend_types
& PVR2_I2C_PEND_ALL
);
909 cp
= kzalloc(sizeof(*cp
),GFP_KERNEL
);
910 trace_i2c("i2c_attach [client=%s @ 0x%x ctxt=%p]",
913 if (!cp
) return -ENOMEM
;
915 INIT_LIST_HEAD(&cp
->list
);
917 mutex_lock(&hdw
->i2c_list_lock
); do {
918 list_add_tail(&cp
->list
,&hdw
->i2c_clients
);
919 hdw
->i2c_pend_types
|= PVR2_I2C_PEND_DETECT
;
920 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
921 if (fl
) pvr2_hdw_poll_trigger_unlocked(hdw
);
925 static int pvr2_i2c_detach_inform(struct i2c_client
*client
)
927 struct pvr2_hdw
*hdw
= (struct pvr2_hdw
*)(client
->adapter
->algo_data
);
928 struct pvr2_i2c_client
*cp
;
929 struct list_head
*item
,*nc
;
930 unsigned long amask
= 0;
932 mutex_lock(&hdw
->i2c_list_lock
); do {
933 list_for_each_safe(item
,nc
,&hdw
->i2c_clients
) {
934 cp
= list_entry(item
,struct pvr2_i2c_client
,list
);
935 if (cp
->client
== client
) {
936 trace_i2c("pvr2_i2c_detach"
937 " [client=%s @ 0x%x ctxt=%p]",
941 cp
->handler
->func_table
->detach
) {
942 cp
->handler
->func_table
->detach(
943 cp
->handler
->func_data
);
950 amask
|= cp
->ctl_mask
;
952 hdw
->i2c_active_mask
= amask
;
953 } while (0); mutex_unlock(&hdw
->i2c_list_lock
);
955 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
962 static struct i2c_algorithm pvr2_i2c_algo_template
= {
963 .master_xfer
= pvr2_i2c_xfer
,
964 .algo_control
= pvr2_i2c_control
,
965 .functionality
= pvr2_i2c_functionality
,
968 static struct i2c_adapter pvr2_i2c_adap_template
= {
969 .owner
= THIS_MODULE
,
970 .class = I2C_CLASS_TV_ANALOG
,
971 .id
= I2C_HW_B_BT848
,
972 .client_register
= pvr2_i2c_attach_inform
,
973 .client_unregister
= pvr2_i2c_detach_inform
,
976 static void do_i2c_scan(struct pvr2_hdw
*hdw
)
978 struct i2c_msg msg
[1];
981 msg
[0].flags
= I2C_M_RD
;
984 printk("%s: i2c scan beginning\n",hdw
->name
);
985 for (i
= 0; i
< 128; i
++) {
987 rc
= i2c_transfer(&hdw
->i2c_adap
,msg
, ARRAY_SIZE(msg
));
988 if (rc
!= 1) continue;
989 printk("%s: i2c scan: found device @ 0x%x\n",hdw
->name
,i
);
991 printk("%s: i2c scan done.\n",hdw
->name
);
994 void pvr2_i2c_core_init(struct pvr2_hdw
*hdw
)
998 /* The default action for all possible I2C addresses is just to do
999 the transfer normally. */
1000 for (idx
= 0; idx
< PVR2_I2C_FUNC_CNT
; idx
++) {
1001 hdw
->i2c_func
[idx
] = pvr2_i2c_basic_op
;
1004 /* However, deal with various special cases for 24xxx hardware. */
1005 if (ir_mode
[hdw
->unit_number
] == 0) {
1006 printk(KERN_INFO
"%s: IR disabled\n",hdw
->name
);
1007 hdw
->i2c_func
[0x18] = i2c_black_hole
;
1008 } else if (ir_mode
[hdw
->unit_number
] == 1) {
1009 if (hdw
->hdw_type
== PVR2_HDW_TYPE_24XXX
) {
1010 hdw
->i2c_func
[0x18] = i2c_24xxx_ir
;
1013 if (hdw
->hdw_type
== PVR2_HDW_TYPE_24XXX
) {
1014 hdw
->i2c_func
[0x1b] = i2c_hack_wm8775
;
1015 hdw
->i2c_func
[0x44] = i2c_hack_cx25840
;
1018 // Configure the adapter and set up everything else related to it.
1019 memcpy(&hdw
->i2c_adap
,&pvr2_i2c_adap_template
,sizeof(hdw
->i2c_adap
));
1020 memcpy(&hdw
->i2c_algo
,&pvr2_i2c_algo_template
,sizeof(hdw
->i2c_algo
));
1021 strlcpy(hdw
->i2c_adap
.name
,hdw
->name
,sizeof(hdw
->i2c_adap
.name
));
1022 hdw
->i2c_adap
.dev
.parent
= &hdw
->usb_dev
->dev
;
1023 hdw
->i2c_adap
.algo
= &hdw
->i2c_algo
;
1024 hdw
->i2c_adap
.algo_data
= hdw
;
1025 hdw
->i2c_pend_mask
= 0;
1026 hdw
->i2c_stale_mask
= 0;
1027 hdw
->i2c_active_mask
= 0;
1028 INIT_LIST_HEAD(&hdw
->i2c_clients
);
1029 mutex_init(&hdw
->i2c_list_lock
);
1030 hdw
->i2c_linked
= !0;
1031 i2c_add_adapter(&hdw
->i2c_adap
);
1032 if (i2c_scan
) do_i2c_scan(hdw
);
1035 void pvr2_i2c_core_done(struct pvr2_hdw
*hdw
)
1037 if (hdw
->i2c_linked
) {
1038 i2c_del_adapter(&hdw
->i2c_adap
);
1039 hdw
->i2c_linked
= 0;
1044 Stuff for Emacs to see, in order to encourage consistent editing style:
1045 *** Local Variables: ***
1047 *** fill-column: 75 ***
1048 *** tab-width: 8 ***
1049 *** c-basic-offset: 8 ***