]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/media/video/pvrusb2/pvrusb2-i2c-core.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / video / pvrusb2 / pvrusb2-i2c-core.c
1 /*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 *
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
10 *
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.
15 *
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
19 *
20 */
21
22 #include "pvrusb2-i2c-core.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-debug.h"
25
26 #define trace_i2c(...) pvr2_trace(PVR2_TRACE_I2C,__VA_ARGS__)
27
28 /*
29
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.
33
34 */
35
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");
39
40 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
41 unsigned int detail,
42 char *buf,unsigned int maxlen);
43
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 */
48 {
49 /* Return value - default 0 means success */
50 int ret;
51
52
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)",
58 i2c_addr,
59 length,(unsigned int)(sizeof(hdw->cmd_buffer) - 3));
60 return -ENOTSUPP;
61 }
62
63 LOCK_TAKE(hdw->ctl_lock);
64
65 /* Clear the command buffer (likely to be paranoia) */
66 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
67
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);
73
74 /* Do the operation */
75 ret = pvr2_send_request(hdw,
76 hdw->cmd_buffer,
77 length + 3,
78 hdw->cmd_buffer,
79 1);
80 if (!ret) {
81 if (hdw->cmd_buffer[0] != 8) {
82 ret = -EIO;
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]);
87 }
88 }
89 }
90
91 LOCK_GIVE(hdw->ctl_lock);
92
93 return ret;
94 }
95
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 */
102 {
103 /* Return value - default 0 means success */
104 int ret;
105
106
107 if (!data) dlen = 0;
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)",
112 i2c_addr,
113 dlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 4));
114 return -ENOTSUPP;
115 }
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)",
120 i2c_addr,
121 rlen,(unsigned int)(sizeof(hdw->cmd_buffer) - 1));
122 return -ENOTSUPP;
123 }
124
125 LOCK_TAKE(hdw->ctl_lock);
126
127 /* Clear the command buffer (likely to be paranoia) */
128 memset(hdw->cmd_buffer, 0, sizeof(hdw->cmd_buffer));
129
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);
137
138 /* Do the operation */
139 ret = pvr2_send_request(hdw,
140 hdw->cmd_buffer,
141 4 + dlen,
142 hdw->cmd_buffer,
143 rlen + 1);
144 if (!ret) {
145 if (hdw->cmd_buffer[0] != 8) {
146 ret = -EIO;
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]);
151 }
152 }
153 }
154
155 /* Copy back the result */
156 if (res && rlen) {
157 if (ret) {
158 /* Error, just blank out the return buffer */
159 memset(res, 0, rlen);
160 } else {
161 memcpy(res, hdw->cmd_buffer + 1, rlen);
162 }
163 }
164
165 LOCK_GIVE(hdw->ctl_lock);
166
167 return ret;
168 }
169
170 /* This is the common low level entry point for doing I2C operations to the
171 hardware. */
172 static int pvr2_i2c_basic_op(struct pvr2_hdw *hdw,
173 u8 i2c_addr,
174 u8 *wdata,
175 u16 wlen,
176 u8 *rdata,
177 u16 rlen)
178 {
179 if (!rdata) rlen = 0;
180 if (!wdata) wlen = 0;
181 if (rlen || !wlen) {
182 return pvr2_i2c_read(hdw,i2c_addr,wdata,wlen,rdata,rlen);
183 } else {
184 return pvr2_i2c_write(hdw,i2c_addr,wdata,wlen);
185 }
186 }
187
188 #ifdef CONFIG_VIDEO_PVRUSB2_24XXX
189
190 /* This is a special entry point that is entered if an I2C operation is
191 attempted to a wm8775 chip on model 24xxx hardware. Autodetect of this
192 part doesn't work, but we know it is really there. So let's look for
193 the autodetect attempt and just return success if we see that. */
194 static int i2c_hack_wm8775(struct pvr2_hdw *hdw,
195 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
196 {
197 if (!(rlen || wlen)) {
198 // This is a probe attempt. Just let it succeed.
199 return 0;
200 }
201 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
202 }
203
204 /* This is a special entry point that is entered if an I2C operation is
205 attempted to a cx25840 chip on model 24xxx hardware. This chip can
206 sometimes wedge itself. Worse still, when this happens msp3400 can
207 falsely detect this part and then the system gets hosed up after msp3400
208 gets confused and dies. What we want to do here is try to keep msp3400
209 away and also try to notice if the chip is wedged and send a warning to
210 the system log. */
211 static int i2c_hack_cx25840(struct pvr2_hdw *hdw,
212 u8 i2c_addr,u8 *wdata,u16 wlen,u8 *rdata,u16 rlen)
213 {
214 int ret;
215 unsigned int subaddr;
216 u8 wbuf[2];
217 int state = hdw->i2c_cx25840_hack_state;
218
219 if (!(rlen || wlen)) {
220 // Probe attempt - always just succeed and don't bother the
221 // hardware (this helps to make the state machine further
222 // down somewhat easier).
223 return 0;
224 }
225
226 if (state == 3) {
227 return pvr2_i2c_basic_op(hdw,i2c_addr,wdata,wlen,rdata,rlen);
228 }
229
230 /* We're looking for the exact pattern where the revision register
231 is being read. The cx25840 module will always look at the
232 revision register first. Any other pattern of access therefore
233 has to be a probe attempt from somebody else so we'll reject it.
234 Normally we could just let each client just probe the part
235 anyway, but when the cx25840 is wedged, msp3400 will get a false
236 positive and that just screws things up... */
237
238 if (wlen == 0) {
239 switch (state) {
240 case 1: subaddr = 0x0100; break;
241 case 2: subaddr = 0x0101; break;
242 default: goto fail;
243 }
244 } else if (wlen == 2) {
245 subaddr = (wdata[0] << 8) | wdata[1];
246 switch (subaddr) {
247 case 0x0100: state = 1; break;
248 case 0x0101: state = 2; break;
249 default: goto fail;
250 }
251 } else {
252 goto fail;
253 }
254 if (!rlen) goto success;
255 state = 0;
256 if (rlen != 1) goto fail;
257
258 /* If we get to here then we have a legitimate read for one of the
259 two revision bytes, so pass it through. */
260 wbuf[0] = subaddr >> 8;
261 wbuf[1] = subaddr;
262 ret = pvr2_i2c_basic_op(hdw,i2c_addr,wbuf,2,rdata,rlen);
263
264 if ((ret != 0) || (*rdata == 0x04) || (*rdata == 0x0a)) {
265 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
266 "WARNING: Detected a wedged cx25840 chip;"
267 " the device will not work.");
268 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
269 "WARNING: Try power cycling the pvrusb2 device.");
270 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
271 "WARNING: Disabling further access to the device"
272 " to prevent other foul-ups.");
273 // This blocks all further communication with the part.
274 hdw->i2c_func[0x44] = NULL;
275 pvr2_hdw_render_useless(hdw);
276 goto fail;
277 }
278
279 /* Success! */
280 pvr2_trace(PVR2_TRACE_CHIPS,"cx25840 appears to be OK.");
281 state = 3;
282
283 success:
284 hdw->i2c_cx25840_hack_state = state;
285 return 0;
286
287 fail:
288 hdw->i2c_cx25840_hack_state = state;
289 return -EIO;
290 }
291
292 #endif /* CONFIG_VIDEO_PVRUSB2_24XXX */
293
294 /* This is a very, very limited I2C adapter implementation. We can only
295 support what we actually know will work on the device... */
296 static int pvr2_i2c_xfer(struct i2c_adapter *i2c_adap,
297 struct i2c_msg msgs[],
298 int num)
299 {
300 int ret = -ENOTSUPP;
301 pvr2_i2c_func funcp = NULL;
302 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(i2c_adap->algo_data);
303
304 if (!num) {
305 ret = -EINVAL;
306 goto done;
307 }
308 if ((msgs[0].flags & I2C_M_NOSTART)) {
309 trace_i2c("i2c refusing I2C_M_NOSTART");
310 goto done;
311 }
312 if (msgs[0].addr < PVR2_I2C_FUNC_CNT) {
313 funcp = hdw->i2c_func[msgs[0].addr];
314 }
315 if (!funcp) {
316 ret = -EIO;
317 goto done;
318 }
319
320 if (num == 1) {
321 if (msgs[0].flags & I2C_M_RD) {
322 /* Simple read */
323 u16 tcnt,bcnt,offs;
324 if (!msgs[0].len) {
325 /* Length == 0 read. This is a probe. */
326 if (funcp(hdw,msgs[0].addr,NULL,0,NULL,0)) {
327 ret = -EIO;
328 goto done;
329 }
330 ret = 1;
331 goto done;
332 }
333 /* If the read is short enough we'll do the whole
334 thing atomically. Otherwise we have no choice
335 but to break apart the reads. */
336 tcnt = msgs[0].len;
337 offs = 0;
338 while (tcnt) {
339 bcnt = tcnt;
340 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
341 bcnt = sizeof(hdw->cmd_buffer)-1;
342 }
343 if (funcp(hdw,msgs[0].addr,NULL,0,
344 msgs[0].buf+offs,bcnt)) {
345 ret = -EIO;
346 goto done;
347 }
348 offs += bcnt;
349 tcnt -= bcnt;
350 }
351 ret = 1;
352 goto done;
353 } else {
354 /* Simple write */
355 ret = 1;
356 if (funcp(hdw,msgs[0].addr,
357 msgs[0].buf,msgs[0].len,NULL,0)) {
358 ret = -EIO;
359 }
360 goto done;
361 }
362 } else if (num == 2) {
363 if (msgs[0].addr != msgs[1].addr) {
364 trace_i2c("i2c refusing 2 phase transfer with"
365 " conflicting target addresses");
366 ret = -ENOTSUPP;
367 goto done;
368 }
369 if ((!((msgs[0].flags & I2C_M_RD))) &&
370 (msgs[1].flags & I2C_M_RD)) {
371 u16 tcnt,bcnt,wcnt,offs;
372 /* Write followed by atomic read. If the read
373 portion is short enough we'll do the whole thing
374 atomically. Otherwise we have no choice but to
375 break apart the reads. */
376 tcnt = msgs[1].len;
377 wcnt = msgs[0].len;
378 offs = 0;
379 while (tcnt || wcnt) {
380 bcnt = tcnt;
381 if (bcnt > sizeof(hdw->cmd_buffer)-1) {
382 bcnt = sizeof(hdw->cmd_buffer)-1;
383 }
384 if (funcp(hdw,msgs[0].addr,
385 msgs[0].buf,wcnt,
386 msgs[1].buf+offs,bcnt)) {
387 ret = -EIO;
388 goto done;
389 }
390 offs += bcnt;
391 tcnt -= bcnt;
392 wcnt = 0;
393 }
394 ret = 2;
395 goto done;
396 } else {
397 trace_i2c("i2c refusing complex transfer"
398 " read0=%d read1=%d",
399 (msgs[0].flags & I2C_M_RD),
400 (msgs[1].flags & I2C_M_RD));
401 }
402 } else {
403 trace_i2c("i2c refusing %d phase transfer",num);
404 }
405
406 done:
407 if (pvrusb2_debug & PVR2_TRACE_I2C_TRAF) {
408 unsigned int idx,offs,cnt;
409 for (idx = 0; idx < num; idx++) {
410 cnt = msgs[idx].len;
411 printk(KERN_INFO
412 "pvrusb2 i2c xfer %u/%u:"
413 " addr=0x%x len=%d %s%s",
414 idx+1,num,
415 msgs[idx].addr,
416 cnt,
417 (msgs[idx].flags & I2C_M_RD ?
418 "read" : "write"),
419 (msgs[idx].flags & I2C_M_NOSTART ?
420 " nostart" : ""));
421 if ((ret > 0) || !(msgs[idx].flags & I2C_M_RD)) {
422 if (cnt > 8) cnt = 8;
423 printk(" [");
424 for (offs = 0; offs < (cnt>8?8:cnt); offs++) {
425 if (offs) printk(" ");
426 printk("%02x",msgs[idx].buf[offs]);
427 }
428 if (offs < cnt) printk(" ...");
429 printk("]");
430 }
431 if (idx+1 == num) {
432 printk(" result=%d",ret);
433 }
434 printk("\n");
435 }
436 if (!num) {
437 printk(KERN_INFO
438 "pvrusb2 i2c xfer null transfer result=%d\n",
439 ret);
440 }
441 }
442 return ret;
443 }
444
445 static int pvr2_i2c_control(struct i2c_adapter *adapter,
446 unsigned int cmd, unsigned long arg)
447 {
448 return 0;
449 }
450
451 static u32 pvr2_i2c_functionality(struct i2c_adapter *adap)
452 {
453 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA;
454 }
455
456 static int pvr2_i2c_core_singleton(struct i2c_client *cp,
457 unsigned int cmd,void *arg)
458 {
459 int stat;
460 if (!cp) return -EINVAL;
461 if (!(cp->driver)) return -EINVAL;
462 if (!(cp->driver->command)) return -EINVAL;
463 if (!try_module_get(cp->driver->driver.owner)) return -EAGAIN;
464 stat = cp->driver->command(cp,cmd,arg);
465 module_put(cp->driver->driver.owner);
466 return stat;
467 }
468
469 int pvr2_i2c_client_cmd(struct pvr2_i2c_client *cp,unsigned int cmd,void *arg)
470 {
471 int stat;
472 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
473 char buf[100];
474 unsigned int cnt;
475 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
476 buf,sizeof(buf));
477 pvr2_trace(PVR2_TRACE_I2C_CMD,
478 "i2c COMMAND (code=%u 0x%x) to %.*s",
479 cmd,cmd,cnt,buf);
480 }
481 stat = pvr2_i2c_core_singleton(cp->client,cmd,arg);
482 if (pvrusb2_debug & PVR2_TRACE_I2C_CMD) {
483 char buf[100];
484 unsigned int cnt;
485 cnt = pvr2_i2c_client_describe(cp,PVR2_I2C_DETAIL_DEBUG,
486 buf,sizeof(buf));
487 pvr2_trace(PVR2_TRACE_I2C_CMD,
488 "i2c COMMAND to %.*s (ret=%d)",cnt,buf,stat);
489 }
490 return stat;
491 }
492
493 int pvr2_i2c_core_cmd(struct pvr2_hdw *hdw,unsigned int cmd,void *arg)
494 {
495 struct list_head *item,*nc;
496 struct pvr2_i2c_client *cp;
497 int stat = -EINVAL;
498
499 if (!hdw) return stat;
500
501 mutex_lock(&hdw->i2c_list_lock);
502 list_for_each_safe(item,nc,&hdw->i2c_clients) {
503 cp = list_entry(item,struct pvr2_i2c_client,list);
504 if (!cp->recv_enable) continue;
505 mutex_unlock(&hdw->i2c_list_lock);
506 stat = pvr2_i2c_client_cmd(cp,cmd,arg);
507 mutex_lock(&hdw->i2c_list_lock);
508 }
509 mutex_unlock(&hdw->i2c_list_lock);
510 return stat;
511 }
512
513
514 static int handler_check(struct pvr2_i2c_client *cp)
515 {
516 struct pvr2_i2c_handler *hp = cp->handler;
517 if (!hp) return 0;
518 if (!hp->func_table->check) return 0;
519 return hp->func_table->check(hp->func_data) != 0;
520 }
521
522 #define BUFSIZE 500
523
524 void pvr2_i2c_core_sync(struct pvr2_hdw *hdw)
525 {
526 unsigned long msk;
527 unsigned int idx;
528 struct list_head *item,*nc;
529 struct pvr2_i2c_client *cp;
530
531 if (!hdw->i2c_linked) return;
532 if (!(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL)) {
533 return;
534 }
535 mutex_lock(&hdw->i2c_list_lock); do {
536 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync BEGIN");
537 if (hdw->i2c_pend_types & PVR2_I2C_PEND_DETECT) {
538 /* One or more I2C clients have attached since we
539 last synced. So scan the list and identify the
540 new clients. */
541 char *buf;
542 unsigned int cnt;
543 unsigned long amask = 0;
544 buf = kmalloc(BUFSIZE,GFP_KERNEL);
545 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_DETECT");
546 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_DETECT;
547 list_for_each(item,&hdw->i2c_clients) {
548 cp = list_entry(item,struct pvr2_i2c_client,
549 list);
550 if (!cp->detected_flag) {
551 cp->ctl_mask = 0;
552 pvr2_i2c_probe(hdw,cp);
553 cp->detected_flag = !0;
554 msk = cp->ctl_mask;
555 cnt = 0;
556 if (buf) {
557 cnt = pvr2_i2c_client_describe(
558 cp,
559 PVR2_I2C_DETAIL_ALL,
560 buf,BUFSIZE);
561 }
562 trace_i2c("Probed: %.*s",cnt,buf);
563 if (handler_check(cp)) {
564 hdw->i2c_pend_types |=
565 PVR2_I2C_PEND_CLIENT;
566 }
567 cp->pend_mask = msk;
568 hdw->i2c_pend_mask |= msk;
569 hdw->i2c_pend_types |=
570 PVR2_I2C_PEND_REFRESH;
571 }
572 amask |= cp->ctl_mask;
573 }
574 hdw->i2c_active_mask = amask;
575 if (buf) kfree(buf);
576 }
577 if (hdw->i2c_pend_types & PVR2_I2C_PEND_STALE) {
578 /* Need to do one or more global updates. Arrange
579 for this to happen. */
580 unsigned long m2;
581 pvr2_trace(PVR2_TRACE_I2C_CORE,
582 "i2c: PEND_STALE (0x%lx)",
583 hdw->i2c_stale_mask);
584 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_STALE;
585 list_for_each(item,&hdw->i2c_clients) {
586 cp = list_entry(item,struct pvr2_i2c_client,
587 list);
588 m2 = hdw->i2c_stale_mask;
589 m2 &= cp->ctl_mask;
590 m2 &= ~cp->pend_mask;
591 if (m2) {
592 pvr2_trace(PVR2_TRACE_I2C_CORE,
593 "i2c: cp=%p setting 0x%lx",
594 cp,m2);
595 cp->pend_mask |= m2;
596 }
597 }
598 hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
599 hdw->i2c_stale_mask = 0;
600 hdw->i2c_pend_types |= PVR2_I2C_PEND_REFRESH;
601 }
602 if (hdw->i2c_pend_types & PVR2_I2C_PEND_CLIENT) {
603 /* One or more client handlers are asking for an
604 update. Run through the list of known clients
605 and update each one. */
606 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_CLIENT");
607 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_CLIENT;
608 list_for_each_safe(item,nc,&hdw->i2c_clients) {
609 cp = list_entry(item,struct pvr2_i2c_client,
610 list);
611 if (!cp->handler) continue;
612 if (!cp->handler->func_table->update) continue;
613 pvr2_trace(PVR2_TRACE_I2C_CORE,
614 "i2c: cp=%p update",cp);
615 mutex_unlock(&hdw->i2c_list_lock);
616 cp->handler->func_table->update(
617 cp->handler->func_data);
618 mutex_lock(&hdw->i2c_list_lock);
619 /* If client's update function set some
620 additional pending bits, account for that
621 here. */
622 if (cp->pend_mask & ~hdw->i2c_pend_mask) {
623 hdw->i2c_pend_mask |= cp->pend_mask;
624 hdw->i2c_pend_types |=
625 PVR2_I2C_PEND_REFRESH;
626 }
627 }
628 }
629 if (hdw->i2c_pend_types & PVR2_I2C_PEND_REFRESH) {
630 const struct pvr2_i2c_op *opf;
631 unsigned long pm;
632 /* Some actual updates are pending. Walk through
633 each update type and perform it. */
634 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: PEND_REFRESH"
635 " (0x%lx)",hdw->i2c_pend_mask);
636 hdw->i2c_pend_types &= ~PVR2_I2C_PEND_REFRESH;
637 pm = hdw->i2c_pend_mask;
638 hdw->i2c_pend_mask = 0;
639 for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
640 if (!(pm & msk)) continue;
641 pm &= ~msk;
642 list_for_each(item,&hdw->i2c_clients) {
643 cp = list_entry(item,
644 struct pvr2_i2c_client,
645 list);
646 if (cp->pend_mask & msk) {
647 cp->pend_mask &= ~msk;
648 cp->recv_enable = !0;
649 } else {
650 cp->recv_enable = 0;
651 }
652 }
653 opf = pvr2_i2c_get_op(idx);
654 if (!opf) continue;
655 mutex_unlock(&hdw->i2c_list_lock);
656 opf->update(hdw);
657 mutex_lock(&hdw->i2c_list_lock);
658 }
659 }
660 pvr2_trace(PVR2_TRACE_I2C_CORE,"i2c: core_sync END");
661 } while (0); mutex_unlock(&hdw->i2c_list_lock);
662 }
663
664 int pvr2_i2c_core_check_stale(struct pvr2_hdw *hdw)
665 {
666 unsigned long msk,sm,pm;
667 unsigned int idx;
668 const struct pvr2_i2c_op *opf;
669 struct list_head *item;
670 struct pvr2_i2c_client *cp;
671 unsigned int pt = 0;
672
673 pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale BEGIN");
674
675 pm = hdw->i2c_active_mask;
676 sm = 0;
677 for (idx = 0, msk = 1; pm; idx++, msk <<= 1) {
678 if (!(msk & pm)) continue;
679 pm &= ~msk;
680 opf = pvr2_i2c_get_op(idx);
681 if (!opf) continue;
682 if (opf->check(hdw)) {
683 sm |= msk;
684 }
685 }
686 if (sm) pt |= PVR2_I2C_PEND_STALE;
687
688 list_for_each(item,&hdw->i2c_clients) {
689 cp = list_entry(item,struct pvr2_i2c_client,list);
690 if (!handler_check(cp)) continue;
691 pt |= PVR2_I2C_PEND_CLIENT;
692 }
693
694 if (pt) {
695 mutex_lock(&hdw->i2c_list_lock); do {
696 hdw->i2c_pend_types |= pt;
697 hdw->i2c_stale_mask |= sm;
698 hdw->i2c_pend_mask |= hdw->i2c_stale_mask;
699 } while (0); mutex_unlock(&hdw->i2c_list_lock);
700 }
701
702 pvr2_trace(PVR2_TRACE_I2C_CORE,
703 "i2c: types=0x%x stale=0x%lx pend=0x%lx",
704 hdw->i2c_pend_types,
705 hdw->i2c_stale_mask,
706 hdw->i2c_pend_mask);
707 pvr2_trace(PVR2_TRACE_I2C_CORE,"pvr2_i2c_core_check_stale END");
708
709 return (hdw->i2c_pend_types & PVR2_I2C_PEND_ALL) != 0;
710 }
711
712 static unsigned int pvr2_i2c_client_describe(struct pvr2_i2c_client *cp,
713 unsigned int detail,
714 char *buf,unsigned int maxlen)
715 {
716 unsigned int ccnt,bcnt;
717 int spcfl = 0;
718 const struct pvr2_i2c_op *opf;
719
720 ccnt = 0;
721 if (detail & PVR2_I2C_DETAIL_DEBUG) {
722 bcnt = scnprintf(buf,maxlen,
723 "ctxt=%p ctl_mask=0x%lx",
724 cp,cp->ctl_mask);
725 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
726 spcfl = !0;
727 }
728 bcnt = scnprintf(buf,maxlen,
729 "%s%s @ 0x%x",
730 (spcfl ? " " : ""),
731 cp->client->name,
732 cp->client->addr);
733 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
734 if ((detail & PVR2_I2C_DETAIL_HANDLER) &&
735 cp->handler && cp->handler->func_table->describe) {
736 bcnt = scnprintf(buf,maxlen," (");
737 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
738 bcnt = cp->handler->func_table->describe(
739 cp->handler->func_data,buf,maxlen);
740 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
741 bcnt = scnprintf(buf,maxlen,")");
742 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
743 }
744 if ((detail & PVR2_I2C_DETAIL_CTLMASK) && cp->ctl_mask) {
745 unsigned int idx;
746 unsigned long msk,sm;
747 int spcfl;
748 bcnt = scnprintf(buf,maxlen," [");
749 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
750 sm = 0;
751 spcfl = 0;
752 for (idx = 0, msk = 1; msk; idx++, msk <<= 1) {
753 if (!(cp->ctl_mask & msk)) continue;
754 opf = pvr2_i2c_get_op(idx);
755 if (opf) {
756 bcnt = scnprintf(buf,maxlen,"%s%s",
757 spcfl ? " " : "",
758 opf->name);
759 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
760 spcfl = !0;
761 } else {
762 sm |= msk;
763 }
764 }
765 if (sm) {
766 bcnt = scnprintf(buf,maxlen,"%s%lx",
767 idx != 0 ? " " : "",sm);
768 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
769 }
770 bcnt = scnprintf(buf,maxlen,"]");
771 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
772 }
773 return ccnt;
774 }
775
776 unsigned int pvr2_i2c_report(struct pvr2_hdw *hdw,
777 char *buf,unsigned int maxlen)
778 {
779 unsigned int ccnt,bcnt;
780 struct list_head *item;
781 struct pvr2_i2c_client *cp;
782 ccnt = 0;
783 mutex_lock(&hdw->i2c_list_lock); do {
784 list_for_each(item,&hdw->i2c_clients) {
785 cp = list_entry(item,struct pvr2_i2c_client,list);
786 bcnt = pvr2_i2c_client_describe(
787 cp,
788 (PVR2_I2C_DETAIL_HANDLER|
789 PVR2_I2C_DETAIL_CTLMASK),
790 buf,maxlen);
791 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
792 bcnt = scnprintf(buf,maxlen,"\n");
793 ccnt += bcnt; buf += bcnt; maxlen -= bcnt;
794 }
795 } while (0); mutex_unlock(&hdw->i2c_list_lock);
796 return ccnt;
797 }
798
799 static int pvr2_i2c_attach_inform(struct i2c_client *client)
800 {
801 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
802 struct pvr2_i2c_client *cp;
803 int fl = !(hdw->i2c_pend_types & PVR2_I2C_PEND_ALL);
804 cp = kmalloc(sizeof(*cp),GFP_KERNEL);
805 trace_i2c("i2c_attach [client=%s @ 0x%x ctxt=%p]",
806 client->name,
807 client->addr,cp);
808 if (!cp) return -ENOMEM;
809 memset(cp,0,sizeof(*cp));
810 INIT_LIST_HEAD(&cp->list);
811 cp->client = client;
812 mutex_lock(&hdw->i2c_list_lock); do {
813 list_add_tail(&cp->list,&hdw->i2c_clients);
814 hdw->i2c_pend_types |= PVR2_I2C_PEND_DETECT;
815 } while (0); mutex_unlock(&hdw->i2c_list_lock);
816 if (fl) pvr2_hdw_poll_trigger_unlocked(hdw);
817 return 0;
818 }
819
820 static int pvr2_i2c_detach_inform(struct i2c_client *client)
821 {
822 struct pvr2_hdw *hdw = (struct pvr2_hdw *)(client->adapter->algo_data);
823 struct pvr2_i2c_client *cp;
824 struct list_head *item,*nc;
825 unsigned long amask = 0;
826 int foundfl = 0;
827 mutex_lock(&hdw->i2c_list_lock); do {
828 list_for_each_safe(item,nc,&hdw->i2c_clients) {
829 cp = list_entry(item,struct pvr2_i2c_client,list);
830 if (cp->client == client) {
831 trace_i2c("pvr2_i2c_detach"
832 " [client=%s @ 0x%x ctxt=%p]",
833 client->name,
834 client->addr,cp);
835 if (cp->handler &&
836 cp->handler->func_table->detach) {
837 cp->handler->func_table->detach(
838 cp->handler->func_data);
839 }
840 list_del(&cp->list);
841 kfree(cp);
842 foundfl = !0;
843 continue;
844 }
845 amask |= cp->ctl_mask;
846 }
847 hdw->i2c_active_mask = amask;
848 } while (0); mutex_unlock(&hdw->i2c_list_lock);
849 if (!foundfl) {
850 trace_i2c("pvr2_i2c_detach [client=%s @ 0x%x ctxt=<unknown>]",
851 client->name,
852 client->addr);
853 }
854 return 0;
855 }
856
857 static struct i2c_algorithm pvr2_i2c_algo_template = {
858 .master_xfer = pvr2_i2c_xfer,
859 .algo_control = pvr2_i2c_control,
860 .functionality = pvr2_i2c_functionality,
861 };
862
863 static struct i2c_adapter pvr2_i2c_adap_template = {
864 .owner = THIS_MODULE,
865 .class = I2C_CLASS_TV_ANALOG,
866 .id = I2C_HW_B_BT848,
867 .client_register = pvr2_i2c_attach_inform,
868 .client_unregister = pvr2_i2c_detach_inform,
869 };
870
871 static void do_i2c_scan(struct pvr2_hdw *hdw)
872 {
873 struct i2c_msg msg[1];
874 int i,rc;
875 msg[0].addr = 0;
876 msg[0].flags = I2C_M_RD;
877 msg[0].len = 0;
878 msg[0].buf = NULL;
879 printk("%s: i2c scan beginning\n",hdw->name);
880 for (i = 0; i < 128; i++) {
881 msg[0].addr = i;
882 rc = i2c_transfer(&hdw->i2c_adap,msg,
883 sizeof(msg)/sizeof(msg[0]));
884 if (rc != 1) continue;
885 printk("%s: i2c scan: found device @ 0x%x\n",hdw->name,i);
886 }
887 printk("%s: i2c scan done.\n",hdw->name);
888 }
889
890 void pvr2_i2c_core_init(struct pvr2_hdw *hdw)
891 {
892 unsigned int idx;
893
894 // The default action for all possible I2C addresses is just to do
895 // the transfer normally.
896 for (idx = 0; idx < PVR2_I2C_FUNC_CNT; idx++) {
897 hdw->i2c_func[idx] = pvr2_i2c_basic_op;
898 }
899
900 #ifdef CONFIG_VIDEO_PVRUSB2_24XXX
901 // If however we're dealing with new hardware, insert some hacks in
902 // the I2C transfer stack to let things work better.
903 if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
904 hdw->i2c_func[0x1b] = i2c_hack_wm8775;
905 hdw->i2c_func[0x44] = i2c_hack_cx25840;
906 }
907 #endif
908
909 // Configure the adapter and set up everything else related to it.
910 memcpy(&hdw->i2c_adap,&pvr2_i2c_adap_template,sizeof(hdw->i2c_adap));
911 memcpy(&hdw->i2c_algo,&pvr2_i2c_algo_template,sizeof(hdw->i2c_algo));
912 strlcpy(hdw->i2c_adap.name,hdw->name,sizeof(hdw->i2c_adap.name));
913 hdw->i2c_adap.algo = &hdw->i2c_algo;
914 hdw->i2c_adap.algo_data = hdw;
915 hdw->i2c_pend_mask = 0;
916 hdw->i2c_stale_mask = 0;
917 hdw->i2c_active_mask = 0;
918 INIT_LIST_HEAD(&hdw->i2c_clients);
919 mutex_init(&hdw->i2c_list_lock);
920 hdw->i2c_linked = !0;
921 i2c_add_adapter(&hdw->i2c_adap);
922 if (i2c_scan) do_i2c_scan(hdw);
923 }
924
925 void pvr2_i2c_core_done(struct pvr2_hdw *hdw)
926 {
927 if (hdw->i2c_linked) {
928 i2c_del_adapter(&hdw->i2c_adap);
929 hdw->i2c_linked = 0;
930 }
931 }
932
933 /*
934 Stuff for Emacs to see, in order to encourage consistent editing style:
935 *** Local Variables: ***
936 *** mode: c ***
937 *** fill-column: 75 ***
938 *** tab-width: 8 ***
939 *** c-basic-offset: 8 ***
940 *** End: ***
941 */