Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / pvrusb2 / pvrusb2-i2c-core.c
blob58fc3c730fe189bb5f608f42a04a9316fd10be41
1 /*
3 * $Id$
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,
42 unsigned int detail,
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 */
51 int ret;
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)",
59 i2c_addr,
60 length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
61 return -ENOTSUPP;
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,
77 hdw->cmd_buffer,
78 length + 3,
79 hdw->cmd_buffer,
80 1);
81 if (!ret) {
82 if (hdw->cmd_buffer[0] != 8) {
83 ret = -EIO;
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);
94 return ret;
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 */
105 int ret;
108 if (!data) dlen = 0;
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)",
113 i2c_addr,
114 dlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 4));
115 return -ENOTSUPP;
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)",
121 i2c_addr,
122 rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
123 return -ENOTSUPP;
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,
141 hdw->cmd_buffer,
142 4 + dlen,
143 hdw->cmd_buffer,
144 rlen + 1);
145 if (!ret) {
146 if (hdw->cmd_buffer[0] != 8) {
147 ret = -EIO;
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 */
157 if (res && rlen) {
158 if (ret) {
159 /* Error, just blank out the return buffer */
160 memset(res, 0, rlen);
161 } else {
162 memcpy(res, hdw->cmd_buffer + 1, rlen);
166 LOCK_GIVE(hdw->ctl_lock);
168 return ret;
171 /* This is the common low level entry point for doing I2C operations to the
172 hardware. */
173 static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
174 u8 i2c_addr,
175 u8 *wdata,
176 u16 wlen,
177 u8 *rdata,
178 u16 rlen)
180 if (!rdata) rlen = 0;
181 if (!wdata) wlen = 0;
182 if (rlen || !wlen) {
183 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
184 } else {
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)
199 u8 dat[4];
200 unsigned int stat;
202 if (!(rlen || wlen)) {
203 /* This is a probe attempt. Just let it succeed. */
204 return 0;
207 /* We don't understand this kind of transaction */
208 if ((wlen != 0) || (rlen == 0)) return -EIO;
210 if (rlen < 3) {
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
217 to chase it down. */
218 if (rlen > 0) rdata[0] = 0;
219 if (rlen > 1) rdata[1] = 0;
220 return 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,
227 hdw->cmd_buffer,1,
228 hdw->cmd_buffer,4);
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
239 receiver. */
240 rdata[2] = 0xc1;
241 if (dat[0] != 1) {
242 /* No code received. */
243 rdata[0] = 0;
244 rdata[1] = 0;
245 } else {
246 u16 val;
247 /* Mash the FX2 firmware-provided IR code into something
248 that the normal i2c chip-level driver expects. */
249 val = dat[1];
250 val <<= 8;
251 val |= dat[2];
252 val >>= 1;
253 val &= ~0x0003;
254 val |= 0x8000;
255 rdata[0] = (val >> 8) & 0xffu;
256 rdata[1] = val & 0xffu;
259 return 0;
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.
271 return 0;
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
282 the system log. */
283 static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
284 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
286 int ret;
287 unsigned int subaddr;
288 u8 wbuf[2];
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).
295 return 0;
298 if (state == 3) {
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... */
310 if (wlen == 0) {
311 switch (state) {
312 case 1: subaddr = 0x0100; break;
313 case 2: subaddr = 0x0101; break;
314 default: goto fail;
316 } else if (wlen == 2) {
317 subaddr = (wdata[0] << 8) | wdata[1];
318 switch (subaddr) {
319 case 0x0100: state = 1; break;
320 case 0x0101: state = 2; break;
321 default: goto fail;
323 } else {
324 goto fail;
326 if (!rlen) goto success;
327 state = 0;
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;
333 wbuf[1] = subaddr;
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);
348 goto fail;
351 /* Success! */
352 pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
353 state = 3;
355 success:
356 hdw->i2c_cx25840_hack_state = state;
357 return 0;
359 fail:
360 hdw->i2c_cx25840_hack_state = state;
361 return -EIO;
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[],
368 int num)
370 int ret = -ENOTSUPP;
371 pvr2_i2c_func funcp = NULL;
372 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
374 if (!num) {
375 ret = -EINVAL;
376 goto done;
378 if ((msgs[0].flags & I2C_M_NOSTART)) {
379 trace_i2c("i2c refusing I2C_M_NOSTART");
380 goto done;
382 if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
383 funcp = hdw->i2c_func[msgs[0].addr];
385 if (!funcp) {
386 ret = -EIO;
387 goto done;
390 if (num == 1) {
391 if (msgs[0].flags & I2C_M_RD) {
392 /* Simple read */
393 u16 tcnt,bcnt,offs;
394 if (!msgs[0].len) {
395 /* Length == 0 read. This is a probe. */
396 if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
397 ret = -EIO;
398 goto done;
400 ret = 1;
401 goto done;
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. */
406 tcnt = msgs[0].len;
407 offs = 0;
408 while (tcnt) {
409 bcnt = tcnt;
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)) {
415 ret = -EIO;
416 goto done;
418 offs += bcnt;
419 tcnt -= bcnt;
421 ret = 1;
422 goto done;
423 } else {
424 /* Simple write */
425 ret = 1;
426 if (funcp(hdw,msgs[0].addr,
427 msgs[0].buf,msgs[0].len,NULL,0)) {
428 ret = -EIO;
430 goto done;
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");
436 ret = -ENOTSUPP;
437 goto done;
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. */
446 tcnt = msgs[1].len;
447 wcnt = msgs[0].len;
448 offs = 0;
449 while (tcnt || wcnt) {
450 bcnt = tcnt;
451 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
452 bcnt = sizeof(hdw->cmd_buffer)-1;
454 if (funcp(hdw,msgs[0].addr,
455 msgs[0].buf,wcnt,
456 msgs[1].buf+offs,bcnt)) {
457 ret = -EIO;
458 goto done;
460 offs += bcnt;
461 tcnt -= bcnt;
462 wcnt = 0;
464 ret = 2;
465 goto done;
466 } else {
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));
472 } else {
473 trace_i2c("i2c refusing %d phase transfer",num);
476 done:
477 if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
478 unsigned int idx,offs,cnt;
479 for (idx = 0; idx < num; idx++) {
480 cnt = msgs[idx].len;
481 printk(KERN_INFO
482 "pvrusb2 i2c xfer %u/%u:"
483 " addr=0x%x len=%d %s%s",
484 idx+1,num,
485 msgs[idx].addr,
486 cnt,
487 (msgs[idx].flags & I2C_M_RD ?
488 "read" : "write"),
489 (msgs[idx].flags & I2C_M_NOSTART ?
490 " nostart" : ""));
491 if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
492 if (cnt > 8) cnt = 8;
493 printk(" [");
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(" ...");
499 printk("]");
501 if (idx+1 == num) {
502 printk(" result=%d",ret);
504 printk("\n");
506 if (!num) {
507 printk(KERN_INFO
508 "pvrusb2 i2c xfer null transfer result=%d\n",
509 ret);
512 return ret;
515 static int pvr2_i2c_control(struct i2c_adapter *adapter,
516 unsigned int cmd, unsigned long arg)
518 return 0;
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)
529 int stat;
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);
536 return stat;
539 int pvr2_i2c_client_cmd(struct pvr2_i2c_client *cp,unsigned int cmd,void *arg)
541 int stat;
542 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
543 char buf[100];
544 unsigned int cnt;
545 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
546 buf,sizeof(buf));
547 pvr2_trace(PVR2_TRACE_I2C_CMD,
548 "i2c COMMAND (code=%u 0x%x) to %.*s",
549 cmd,cmd,cnt,buf);
551 stat = pvr2_i2c_core_singleton(cp->client,cmd,arg);
552 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
553 char buf[100];
554 unsigned int cnt;
555 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
556 buf,sizeof(buf));
557 pvr2_trace(PVR2_TRACE_I2C_CMD,
558 "i2c COMMAND to %.*s (ret=%d)",cnt,buf,stat);
560 return 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;
567 int stat = -EINVAL;
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);
580 return stat;
584 static int handler_check(struct pvr2_i2c_client *cp)
586 struct pvr2_i2c_handler *hp = cp->handler;
587 if (!hp) return 0;
588 if (!hp->func_table->check) return 0;
589 return hp->func_table->check(hp->func_data) != 0;
592 #define BUFSIZE 500
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;
606 cp->status_poll(cp);
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"
611 " low=%u hi=%u",
612 vtp->type,
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)
623 unsigned long msk;
624 unsigned int idx;
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)) {
630 return;
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
637 new clients. */
638 char *buf;
639 unsigned int cnt;
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,
646 list);
647 if (!cp->detected_flag) {
648 cp->ctl_mask = 0;
649 pvr2_i2c_probe(hdw,cp);
650 cp->detected_flag = !0;
651 msk = cp->ctl_mask;
652 cnt = 0;
653 if (buf) {
654 cnt = pvr2_i2c_client_describe(
656 PVR2_I2C_DETAIL_ALL,
657 buf,BUFSIZE);
659 trace_i2c("Probed: %.*s",cnt,buf);
660 if (handler_check(cp)) {
661 hdw->i2c_pend_types |=
662 PVR2_I2C_PEND_CLIENT;
664 cp->pend_mask = msk;
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;
672 if (buf) kfree(buf);
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. */
677 unsigned long m2;
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,
684 list);
685 m2 = hdw->i2c_stale_mask;
686 m2 &= cp->ctl_mask;
687 m2 &= ~cp->pend_mask;
688 if (m2) {
689 pvr2_trace(PVR2_TRACE_I2C_CORE,
690 "i2c: cp=%p setting 0x%lx",
691 cp,m2);
692 cp->pend_mask |= m2;
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,
707 list);
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
718 here. */
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;
728 unsigned long pm;
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;
738 pm &= ~msk;
739 list_for_each(item,&hdw->i2c_clients) {
740 cp = list_entry(item,
741 struct pvr2_i2c_client,
742 list);
743 if (cp->pend_mask & msk) {
744 cp->pend_mask &= ~msk;
745 cp->recv_enable = !0;
746 } else {
747 cp->recv_enable = 0;
750 opf = pvr2_i2c_get_op(idx);
751 if (!opf) continue;
752 mutex_unlock(&hdw->i2c_list_lock);
753 opf->update(hdw);
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;
764 unsigned int idx;
765 const struct pvr2_i2c_op *opf;
766 struct list_head *item;
767 struct pvr2_i2c_client *cp;
768 unsigned int pt = 0;
770 pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale BEGIN");
772 pm = hdw->i2c_active_mask;
773 sm = 0;
774 for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
775 if (!(msk & pm)) continue;
776 pm &= ~msk;
777 opf = pvr2_i2c_get_op(idx);
778 if (!opf) continue;
779 if (opf->check(hdw)) {
780 sm |= msk;
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;
791 if (pt) {
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",
801 hdw->i2c_pend_types,
802 hdw->i2c_stale_mask,
803 hdw->i2c_pend_mask);
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,
810 unsigned int detail,
811 char *buf,unsigned int maxlen)
813 unsigned int ccnt,bcnt;
814 int spcfl = 0;
815 const struct pvr2_i2c_op *opf;
817 ccnt = 0;
818 if (detail & PVR2_I2C_DETAIL_DEBUG) {
819 bcnt = scnprintf(buf,maxlen,
820 "ctxt=%p ctl_mask=0x%lx",
821 cp,cp->ctl_mask);
822 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
823 spcfl = !0;
825 bcnt = scnprintf(buf,maxlen,
826 "%s%s @ 0x%x",
827 (spcfl ? " " : ""),
828 cp->client->name,
829 cp->client->addr);
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) {
842 unsigned int idx;
843 unsigned long msk,sm;
844 int spcfl;
845 bcnt = scnprintf(buf,maxlen," [");
846 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
847 sm = 0;
848 spcfl = 0;
849 for (idx = 0, msk = 1; msk; idx++, msk <<= 1) {
850 if (!(cp->ctl_mask & msk)) continue;
851 opf = pvr2_i2c_get_op(idx);
852 if (opf) {
853 bcnt = scnprintf(buf,maxlen,"%s%s",
854 spcfl ? " " : "",
855 opf->name);
856 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
857 spcfl = !0;
858 } else {
859 sm |= msk;
862 if (sm) {
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;
870 return ccnt;
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;
879 ccnt = 0;
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),
887 buf,maxlen);
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);
893 return ccnt;
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]",
903 client->name,
904 client->addr,cp);
905 if (!cp) return -ENOMEM;
906 cp->hdw = hdw;
907 INIT_LIST_HEAD(&cp->list);
908 cp->client = client;
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);
914 return 0;
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;
923 int foundfl = 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]",
930 client->name,
931 client->addr,cp);
932 if (cp->handler &&
933 cp->handler->func_table->detach) {
934 cp->handler->func_table->detach(
935 cp->handler->func_data);
937 list_del(&cp->list);
938 kfree(cp);
939 foundfl = !0;
940 continue;
942 amask |= cp->ctl_mask;
944 hdw->i2c_active_mask = amask;
945 } while (0); mutex_unlock(&hdw->i2c_list_lock);
946 if (!foundfl) {
947 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
948 client->name,
949 client->addr);
951 return 0;
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];
971 int i,rc;
972 msg[0].addr = 0;
973 msg[0].flags = I2C_M_RD;
974 msg[0].len = 0;
975 msg[0].buf = NULL;
976 printk("%s: i2c scan beginning\n",hdw->name);
977 for (i = 0; i < 128; i++) {
978 msg[0].addr = 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)
988 unsigned int idx;
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: ***
1031 *** mode: c ***
1032 *** fill-column: 75 ***
1033 *** tab-width: 8 ***
1034 *** c-basic-offset: 8 ***
1035 *** End: ***