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"
26 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
30 This module attempts to implement a compliant I2C adapter for the pvrusb2
31 device. By doing this we can then make use of existing functionality in
32 V4L (e.g. tuner.c) rather than rolling our own.
36 static unsigned int i2c_scan = 0;
37 module_param(i2c_scan, int, S_IRUGO|S_IWUSR);
38 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
40 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
42 char *buf,unsigned int maxlen);
44 static int pvr2_i2c_write(struct pvr2_hdw *hdw, /* Context */
45 u8 i2c_addr, /* I2C address we're talking to */
46 u8 *data, /* Data to write */
47 u16 length) /* Size of data to write */
49 /* Return value - default 0 means success */
53 if (!data) length = 0;
54 if (length > (sizeof(hdw->cmd_buffer) - 3)) {
55 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
56 "Killing an I2C write to %u that is too large"
57 " (desired=%u limit=%u)",
59 length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
63 LOCK_TAKE(hdw->ctl_lock);
65 /* Clear the command buffer (likely to be paranoia) */
66 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
68 /* Set up command buffer for an I2C write */
69 hdw->cmd_buffer[0] = 0x08; /* write prefix */
70 hdw->cmd_buffer[1] = i2c_addr; /* i2c addr of chip */
71 hdw->cmd_buffer[2] = length; /* length of what follows */
72 if (length) memcpy(hdw->cmd_buffer + 3, data, length);
74 /* Do the operation */
75 ret = pvr2_send_request(hdw,
81 if (hdw->cmd_buffer[0] != 8) {
83 if (hdw->cmd_buffer[0] != 7) {
84 trace_i2c("unexpected status"
85 " from i2_write[%d]: %d",
86 i2c_addr,hdw->cmd_buffer[0]);
91 LOCK_GIVE(hdw->ctl_lock);
96 static int pvr2_i2c_read(struct pvr2_hdw *hdw, /* Context */
97 u8 i2c_addr, /* I2C address we're talking to */
98 u8 *data, /* Data to write */
99 u16 dlen, /* Size of data to write */
100 u8 *res, /* Where to put data we read */
101 u16 rlen) /* Amount of data to read */
103 /* Return value - default 0 means success */
108 if (dlen > (sizeof(hdw->cmd_buffer) - 4)) {
109 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
110 "Killing an I2C read to %u that has wlen too large"
111 " (desired=%u limit=%u)",
113 dlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 4));
116 if (res && (rlen > (sizeof(hdw->cmd_buffer) - 1))) {
117 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
118 "Killing an I2C read to %u that has rlen too large"
119 " (desired=%u limit=%u)",
121 rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
125 LOCK_TAKE(hdw->ctl_lock);
127 /* Clear the command buffer (likely to be paranoia) */
128 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
130 /* Set up command buffer for an I2C write followed by a read */
131 hdw->cmd_buffer[0] = 0x09; /* read prefix */
132 hdw->cmd_buffer[1] = dlen; /* arg length */
133 hdw->cmd_buffer[2] = rlen; /* answer length. Device will send one
134 more byte (status). */
135 hdw->cmd_buffer[3] = i2c_addr; /* i2c addr of chip */
136 if (dlen) memcpy(hdw->cmd_buffer + 4, data, dlen);
138 /* Do the operation */
139 ret = pvr2_send_request(hdw,
145 if (hdw->cmd_buffer[0] != 8) {
147 if (hdw->cmd_buffer[0] != 7) {
148 trace_i2c("unexpected status"
149 " from i2_read[%d]: %d",
150 i2c_addr,hdw->cmd_buffer[0]);
155 /* Copy back the result */
158 /* Error, just blank out the return buffer */
159 memset(res, 0, rlen);
161 memcpy(res, hdw->cmd_buffer + 1, rlen);
165 LOCK_GIVE(hdw->ctl_lock);
170 /* This is the common low level entry point for doing I2C operations to the
172 static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
179 if (!rdata) rlen = 0;
180 if (!wdata) wlen = 0;
182 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
184 return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
189 /* This is a special entry point for cases of I2C transaction attempts to
190 the IR receiver. The implementation here simulates the IR receiver by
191 issuing a command to the FX2 firmware and using that response to return
192 what the real I2C receiver would have returned. We use this for 24xxx
193 devices, where the IR receiver chip has been removed and replaced with
194 FX2 related logic. */
195 static int i2c_24xxx_ir(struct pvr2_hdw *hdw,
196 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
201 if (!(rlen || wlen)) {
202 /* This is a probe attempt. Just let it succeed. */
206 /* We don't understand this kind of transaction */
207 if ((wlen != 0) || (rlen == 0)) return -EIO;
210 /* Mike Isely <isely@pobox.com> Appears to be a probe
211 attempt from lirc. Just fill in zeroes and return. If
212 we try instead to do the full transaction here, then bad
213 things seem to happen within the lirc driver module
214 (version 0.8.0-7 sources from Debian, when run under
215 vanilla 2.6.17.6 kernel) - and I don't have the patience
217 if (rlen > 0) rdata[0] = 0;
218 if (rlen > 1) rdata[1] = 0;
222 /* Issue a command to the FX2 to read the IR receiver. */
223 LOCK_TAKE(hdw->ctl_lock); do {
224 hdw->cmd_buffer[0] = 0xec;
225 stat = pvr2_send_request(hdw,
228 dat[0] = hdw->cmd_buffer[0];
229 dat[1] = hdw->cmd_buffer[1];
230 dat[2] = hdw->cmd_buffer[2];
231 dat[3] = hdw->cmd_buffer[3];
232 } while (0); LOCK_GIVE(hdw->ctl_lock);
234 /* Give up if that operation failed. */
235 if (stat != 0) return stat;
237 /* Mangle the results into something that looks like the real IR
241 /* No code received. */
246 /* Mash the FX2 firmware-provided IR code into something
247 that the normal i2c chip-level driver expects. */
254 rdata[0] = (val >> 8) & 0xffu;
255 rdata[1] = val & 0xffu;
261 /* This is a special entry point that is entered if an I2C operation is
262 attempted to a wm8775 chip on model 24xxx hardware. Autodetect of this
263 part doesn't work, but we know it is really there. So let's look for
264 the autodetect attempt and just return success if we see that. */
265 static int i2c_hack_wm8775(struct pvr2_hdw *hdw,
266 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
268 if (!(rlen || wlen)) {
269 // This is a probe attempt. Just let it succeed.
272 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
275 /* This is a special entry point that is entered if an I2C operation is
276 attempted to a cx25840 chip on model 24xxx hardware. This chip can
277 sometimes wedge itself. Worse still, when this happens msp3400 can
278 falsely detect this part and then the system gets hosed up after msp3400
279 gets confused and dies. What we want to do here is try to keep msp3400
280 away and also try to notice if the chip is wedged and send a warning to
282 static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
283 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
286 unsigned int subaddr;
288 int state = hdw->i2c_cx25840_hack_state;
290 if (!(rlen || wlen)) {
291 // Probe attempt - always just succeed and don't bother the
292 // hardware (this helps to make the state machine further
293 // down somewhat easier).
298 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
301 /* We're looking for the exact pattern where the revision register
302 is being read. The cx25840 module will always look at the
303 revision register first. Any other pattern of access therefore
304 has to be a probe attempt from somebody else so we'll reject it.
305 Normally we could just let each client just probe the part
306 anyway, but when the cx25840 is wedged, msp3400 will get a false
307 positive and that just screws things up... */
311 case 1: subaddr = 0x0100; break;
312 case 2: subaddr = 0x0101; break;
315 } else if (wlen == 2) {
316 subaddr = (wdata[0] << 8) | wdata[1];
318 case 0x0100: state = 1; break;
319 case 0x0101: state = 2; break;
325 if (!rlen) goto success;
327 if (rlen != 1) goto fail;
329 /* If we get to here then we have a legitimate read for one of the
330 two revision bytes, so pass it through. */
331 wbuf[0] = subaddr >> 8;
333 ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
335 if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
336 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
337 "WARNING: Detected a wedged cx25840 chip;"
338 " the device will not work.");
339 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
340 "WARNING: Try power cycling the pvrusb2 device.");
341 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
342 "WARNING: Disabling further access to the device"
343 " to prevent other foul-ups.");
344 // This blocks all further communication with the part.
345 hdw->i2c_func[0x44] = NULL;
346 pvr2_hdw_render_useless(hdw);
351 pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
355 hdw->i2c_cx25840_hack_state = state;
359 hdw->i2c_cx25840_hack_state = state;
363 /* This is a very, very limited I2C adapter implementation. We can only
364 support what we actually know will work on the device... */
365 static int pvr2_i2c_xfer(struct i2c_adapter *i2c_adap,
366 struct i2c_msg msgs[],
370 pvr2_i2c_func funcp = NULL;
371 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
377 if ((msgs[0].flags & I2C_M_NOSTART)) {
378 trace_i2c("i2c refusing I2C_M_NOSTART");
381 if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
382 funcp = hdw->i2c_func[msgs[0].addr];
390 if (msgs[0].flags & I2C_M_RD) {
394 /* Length == 0 read. This is a probe. */
395 if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
402 /* If the read is short enough we'll do the whole
403 thing atomically. Otherwise we have no choice
404 but to break apart the reads. */
409 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
410 bcnt = sizeof(hdw->cmd_buffer)-1;
412 if (funcp(hdw,msgs[0].addr,NULL,0,
413 msgs[0].buf+offs,bcnt)) {
425 if (funcp(hdw,msgs[0].addr,
426 msgs[0].buf,msgs[0].len,NULL,0)) {
431 } else if (num == 2) {
432 if (msgs[0].addr != msgs[1].addr) {
433 trace_i2c("i2c refusing 2 phase transfer with"
434 " conflicting target addresses");
438 if ((!((msgs[0].flags & I2C_M_RD))) &&
439 (msgs[1].flags & I2C_M_RD)) {
440 u16 tcnt,bcnt,wcnt,offs;
441 /* Write followed by atomic read. If the read
442 portion is short enough we'll do the whole thing
443 atomically. Otherwise we have no choice but to
444 break apart the reads. */
448 while (tcnt || wcnt) {
450 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
451 bcnt = sizeof(hdw->cmd_buffer)-1;
453 if (funcp(hdw,msgs[0].addr,
455 msgs[1].buf+offs,bcnt)) {
466 trace_i2c("i2c refusing complex transfer"
467 " read0=%d read1=%d",
468 (msgs[0].flags & I2C_M_RD),
469 (msgs[1].flags & I2C_M_RD));
472 trace_i2c("i2c refusing %d phase transfer",num);
476 if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
477 unsigned int idx,offs,cnt;
478 for (idx = 0; idx < num; idx++) {
481 "pvrusb2 i2c xfer %u/%u:"
482 " addr=0x%x len=%d %s%s",
486 (msgs[idx].flags & I2C_M_RD ?
488 (msgs[idx].flags & I2C_M_NOSTART ?
490 if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
491 if (cnt > 8) cnt = 8;
493 for (offs = 0; offs < (cnt>8?8:cnt); offs++) {
494 if (offs) printk(" ");
495 printk("%02x",msgs[idx].buf[offs]);
497 if (offs < cnt) printk(" ...");
501 printk(" result=%d",ret);
507 "pvrusb2 i2c xfer null transfer result=%d\n",
514 static int pvr2_i2c_control(struct i2c_adapter *adapter,
515 unsigned int cmd, unsigned long arg)
520 static u32 pvr2_i2c_functionality(struct i2c_adapter *adap)
522 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA;
525 static int pvr2_i2c_core_singleton(struct i2c_client *cp,
526 unsigned int cmd,void *arg)
529 if (!cp) return -EINVAL;
530 if (!(cp->driver)) return -EINVAL;
531 if (!(cp->driver->command)) return -EINVAL;
532 if (!try_module_get(cp->driver->driver.owner)) return -EAGAIN;
533 stat = cp->driver->command(cp,cmd,arg);
534 module_put(cp->driver->driver.owner);
538 int pvr2_i2c_client_cmd(struct pvr2_i2c_client *cp,unsigned int cmd,void *arg)
541 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
544 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
546 pvr2_trace(PVR2_TRACE_I2C_CMD,
547 "i2c COMMAND (code=%u 0x%x) to %.*s",
550 stat = pvr2_i2c_core_singleton(cp->client,cmd,arg);
551 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
554 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
556 pvr2_trace(PVR2_TRACE_I2C_CMD,
557 "i2c COMMAND to %.*s (ret=%d)",cnt,buf,stat);
562 int pvr2_i2c_core_cmd(struct pvr2_hdw *hdw,unsigned int cmd,void *arg)
564 struct list_head *item,*nc;
565 struct pvr2_i2c_client *cp;
568 if (!hdw) return stat;
570 mutex_lock(&hdw->i2c_list_lock);
571 list_for_each_safe(item,nc,&hdw->i2c_clients) {
572 cp = list_entry(item,struct pvr2_i2c_client,list);
573 if (!cp->recv_enable) continue;
574 mutex_unlock(&hdw->i2c_list_lock);
575 stat = pvr2_i2c_client_cmd(cp,cmd,arg);
576 mutex_lock(&hdw->i2c_list_lock);
578 mutex_unlock(&hdw->i2c_list_lock);
583 static int handler_check(struct pvr2_i2c_client *cp)
585 struct pvr2_i2c_handler *hp = cp->handler;
587 if (!hp->func_table->check) return 0;
588 return hp->func_table->check(hp->func_data) != 0;
593 void pvr2_i2c_core_sync(struct pvr2_hdw *hdw)
597 struct list_head *item,*nc;
598 struct pvr2_i2c_client *cp;
600 if (!hdw->i2c_linked) return;
601 if (!(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL)) {
604 mutex_lock(&hdw->i2c_list_lock); do {
605 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync BEGIN");
606 if (hdw->i2c_pend_types & PVR2_I2C_PEND_DETECT) {
607 /* One or more I2C clients have attached since we
608 last synced. So scan the list and identify the
612 unsigned long amask = 0;
613 buf = kmalloc(BUFSIZE,GFP_KERNEL);
614 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_DETECT");
615 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_DETECT;
616 list_for_each(item,&hdw->i2c_clients) {
617 cp = list_entry(item,struct pvr2_i2c_client,
619 if (!cp->detected_flag) {
621 pvr2_i2c_probe(hdw,cp);
622 cp->detected_flag = !0;
626 cnt = pvr2_i2c_client_describe(
631 trace_i2c("Probed: %.*s",cnt,buf);
632 if (handler_check(cp)) {
633 hdw->i2c_pend_types |=
634 PVR2_I2C_PEND_CLIENT;
637 hdw->i2c_pend_mask |= msk;
638 hdw->i2c_pend_types |=
639 PVR2_I2C_PEND_REFRESH;
641 amask |= cp->ctl_mask;
643 hdw->i2c_active_mask = amask;
646 if (hdw->i2c_pend_types & PVR2_I2C_PEND_STALE) {
647 /* Need to do one or more global updates. Arrange
648 for this to happen. */
650 pvr2_trace(PVR2_TRACE_I2C_CORE,
651 "i2c: PEND_STALE (0x%lx)",
652 hdw->i2c_stale_mask);
653 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_STALE;
654 list_for_each(item,&hdw->i2c_clients) {
655 cp = list_entry(item,struct pvr2_i2c_client,
657 m2 = hdw->i2c_stale_mask;
659 m2 &= ~cp->pend_mask;
661 pvr2_trace(PVR2_TRACE_I2C_CORE,
662 "i2c: cp=%p setting 0x%lx",
667 hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
668 hdw->i2c_stale_mask = 0;
669 hdw->i2c_pend_types |= PVR2_I2C_PEND_REFRESH;
671 if (hdw->i2c_pend_types & PVR2_I2C_PEND_CLIENT) {
672 /* One or more client handlers are asking for an
673 update. Run through the list of known clients
674 and update each one. */
675 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_CLIENT");
676 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_CLIENT;
677 list_for_each_safe(item,nc,&hdw->i2c_clients) {
678 cp = list_entry(item,struct pvr2_i2c_client,
680 if (!cp->handler) continue;
681 if (!cp->handler->func_table->update) continue;
682 pvr2_trace(PVR2_TRACE_I2C_CORE,
683 "i2c: cp=%p update",cp);
684 mutex_unlock(&hdw->i2c_list_lock);
685 cp->handler->func_table->update(
686 cp->handler->func_data);
687 mutex_lock(&hdw->i2c_list_lock);
688 /* If client's update function set some
689 additional pending bits, account for that
691 if (cp->pend_mask & ~hdw->i2c_pend_mask) {
692 hdw->i2c_pend_mask |= cp->pend_mask;
693 hdw->i2c_pend_types |=
694 PVR2_I2C_PEND_REFRESH;
698 if (hdw->i2c_pend_types & PVR2_I2C_PEND_REFRESH) {
699 const struct pvr2_i2c_op *opf;
701 /* Some actual updates are pending. Walk through
702 each update type and perform it. */
703 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_REFRESH"
704 " (0x%lx)",hdw->i2c_pend_mask);
705 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_REFRESH;
706 pm = hdw->i2c_pend_mask;
707 hdw->i2c_pend_mask = 0;
708 for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
709 if (!(pm & msk)) continue;
711 list_for_each(item,&hdw->i2c_clients) {
712 cp = list_entry(item,
713 struct pvr2_i2c_client,
715 if (cp->pend_mask & msk) {
716 cp->pend_mask &= ~msk;
717 cp->recv_enable = !0;
722 opf = pvr2_i2c_get_op(idx);
724 mutex_unlock(&hdw->i2c_list_lock);
726 mutex_lock(&hdw->i2c_list_lock);
729 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync END");
730 } while (0); mutex_unlock(&hdw->i2c_list_lock);
733 int pvr2_i2c_core_check_stale(struct pvr2_hdw *hdw)
735 unsigned long msk,sm,pm;
737 const struct pvr2_i2c_op *opf;
738 struct list_head *item;
739 struct pvr2_i2c_client *cp;
742 pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale BEGIN");
744 pm = hdw->i2c_active_mask;
746 for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
747 if (!(msk & pm)) continue;
749 opf = pvr2_i2c_get_op(idx);
751 if (opf->check(hdw)) {
755 if (sm) pt |= PVR2_I2C_PEND_STALE;
757 list_for_each(item,&hdw->i2c_clients) {
758 cp = list_entry(item,struct pvr2_i2c_client,list);
759 if (!handler_check(cp)) continue;
760 pt |= PVR2_I2C_PEND_CLIENT;
764 mutex_lock(&hdw->i2c_list_lock); do {
765 hdw->i2c_pend_types |= pt;
766 hdw->i2c_stale_mask |= sm;
767 hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
768 } while (0); mutex_unlock(&hdw->i2c_list_lock);
771 pvr2_trace(PVR2_TRACE_I2C_CORE,
772 "i2c: types=0x%x stale=0x%lx pend=0x%lx",
776 pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale END");
778 return (hdw->i2c_pend_types & PVR2_I2C_PEND_ALL) != 0;
781 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
783 char *buf,unsigned int maxlen)
785 unsigned int ccnt,bcnt;
787 const struct pvr2_i2c_op *opf;
790 if (detail & PVR2_I2C_DETAIL_DEBUG) {
791 bcnt = scnprintf(buf,maxlen,
792 "ctxt=%p ctl_mask=0x%lx",
794 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
797 bcnt = scnprintf(buf,maxlen,
802 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
803 if ((detail & PVR2_I2C_DETAIL_HANDLER) &&
804 cp->handler && cp->handler->func_table->describe) {
805 bcnt = scnprintf(buf,maxlen," (");
806 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
807 bcnt = cp->handler->func_table->describe(
808 cp->handler->func_data,buf,maxlen);
809 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
810 bcnt = scnprintf(buf,maxlen,")");
811 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
813 if ((detail & PVR2_I2C_DETAIL_CTLMASK) && cp->ctl_mask) {
815 unsigned long msk,sm;
817 bcnt = scnprintf(buf,maxlen," [");
818 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
821 for (idx = 0, msk = 1; msk; idx++, msk <<= 1) {
822 if (!(cp->ctl_mask & msk)) continue;
823 opf = pvr2_i2c_get_op(idx);
825 bcnt = scnprintf(buf,maxlen,"%s%s",
828 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
835 bcnt = scnprintf(buf,maxlen,"%s%lx",
836 idx != 0 ? " " : "",sm);
837 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
839 bcnt = scnprintf(buf,maxlen,"]");
840 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
845 unsigned int pvr2_i2c_report(struct pvr2_hdw *hdw,
846 char *buf,unsigned int maxlen)
848 unsigned int ccnt,bcnt;
849 struct list_head *item;
850 struct pvr2_i2c_client *cp;
852 mutex_lock(&hdw->i2c_list_lock); do {
853 list_for_each(item,&hdw->i2c_clients) {
854 cp = list_entry(item,struct pvr2_i2c_client,list);
855 bcnt = pvr2_i2c_client_describe(
857 (PVR2_I2C_DETAIL_HANDLER|
858 PVR2_I2C_DETAIL_CTLMASK),
860 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
861 bcnt = scnprintf(buf,maxlen,"\n");
862 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
864 } while (0); mutex_unlock(&hdw->i2c_list_lock);
868 static int pvr2_i2c_attach_inform(struct i2c_client *client)
870 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
871 struct pvr2_i2c_client *cp;
872 int fl = !(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL);
873 cp = kmalloc(sizeof(*cp),GFP_KERNEL);
874 trace_i2c("i2c_attach [client=%s @ 0x%x ctxt=%p]",
877 if (!cp) return -ENOMEM;
878 memset(cp,0,sizeof(*cp));
879 INIT_LIST_HEAD(&cp->list);
881 mutex_lock(&hdw->i2c_list_lock); do {
882 list_add_tail(&cp->list,&hdw->i2c_clients);
883 hdw->i2c_pend_types |= PVR2_I2C_PEND_DETECT;
884 } while (0); mutex_unlock(&hdw->i2c_list_lock);
885 if (fl) pvr2_hdw_poll_trigger_unlocked(hdw);
889 static int pvr2_i2c_detach_inform(struct i2c_client *client)
891 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
892 struct pvr2_i2c_client *cp;
893 struct list_head *item,*nc;
894 unsigned long amask = 0;
896 mutex_lock(&hdw->i2c_list_lock); do {
897 list_for_each_safe(item,nc,&hdw->i2c_clients) {
898 cp = list_entry(item,struct pvr2_i2c_client,list);
899 if (cp->client == client) {
900 trace_i2c("pvr2_i2c_detach"
901 " [client=%s @ 0x%x ctxt=%p]",
905 cp->handler->func_table->detach) {
906 cp->handler->func_table->detach(
907 cp->handler->func_data);
914 amask |= cp->ctl_mask;
916 hdw->i2c_active_mask = amask;
917 } while (0); mutex_unlock(&hdw->i2c_list_lock);
919 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
926 static struct i2c_algorithm pvr2_i2c_algo_template = {
927 .master_xfer = pvr2_i2c_xfer,
928 .algo_control = pvr2_i2c_control,
929 .functionality = pvr2_i2c_functionality,
932 static struct i2c_adapter pvr2_i2c_adap_template = {
933 .owner = THIS_MODULE,
934 .class = I2C_CLASS_TV_ANALOG,
935 .id = I2C_HW_B_BT848,
936 .client_register = pvr2_i2c_attach_inform,
937 .client_unregister = pvr2_i2c_detach_inform,
940 static void do_i2c_scan(struct pvr2_hdw *hdw)
942 struct i2c_msg msg[1];
945 msg[0].flags = I2C_M_RD;
948 printk("%s: i2c scan beginning\n",hdw->name);
949 for (i = 0; i < 128; i++) {
951 rc = i2c_transfer(&hdw->i2c_adap,msg,
952 sizeof(msg)/sizeof(msg[0]));
953 if (rc != 1) continue;
954 printk("%s: i2c scan: found device @ 0x%x\n",hdw->name,i);
956 printk("%s: i2c scan done.\n",hdw->name);
959 void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
963 /* The default action for all possible I2C addresses is just to do
964 the transfer normally. */
965 for (idx = 0; idx < PVR2_I2C_FUNC_CNT; idx++) {
966 hdw->i2c_func[idx] = pvr2_i2c_basic_op;
969 /* However, deal with various special cases for 24xxx hardware. */
970 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
971 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
972 hdw->i2c_func[0x44] = i2c_hack_cx25840;
973 hdw->i2c_func[0x18] = i2c_24xxx_ir;
976 // Configure the adapter and set up everything else related to it.
977 memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap));
978 memcpy(&hdw->i2c_algo,&pvr2_i2c_algo_template,sizeof(hdw->i2c_algo));
979 strlcpy(hdw->i2c_adap.name,hdw->name,sizeof(hdw->i2c_adap.name));
980 hdw->i2c_adap.algo = &hdw->i2c_algo;
981 hdw->i2c_adap.algo_data = hdw;
982 hdw->i2c_pend_mask = 0;
983 hdw->i2c_stale_mask = 0;
984 hdw->i2c_active_mask = 0;
985 INIT_LIST_HEAD(&hdw->i2c_clients);
986 mutex_init(&hdw->i2c_list_lock);
987 hdw->i2c_linked = !0;
988 i2c_add_adapter(&hdw->i2c_adap);
989 if (i2c_scan) do_i2c_scan(hdw);
992 void pvr2_i2c_core_done(struct pvr2_hdw *hdw)
994 if (hdw->i2c_linked) {
995 i2c_del_adapter(&hdw->i2c_adap);
1001 Stuff for Emacs to see, in order to encourage consistent editing style:
1002 *** Local Variables: ***
1004 *** fill-column: 75 ***
1005 *** tab-width: 8 ***
1006 *** c-basic-offset: 8 ***