[PATCH] v4l: bttv driver update
[linux-2.6/history.git] / drivers / media / video / bttv-if.c
bloba1544f5fbaafe586dc16e7d2a05ba312e3eae638
1 /*
2 bttv-if.c -- interfaces to other kernel modules
3 all the i2c code is here
4 also the gpio interface exported by bttv (used by lirc)
6 bttv - Bt848 frame grabber driver
8 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
9 & Marcus Metzler (mocm@thp.uni-koeln.de)
10 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
32 #include <asm/io.h>
34 #include "bttvp.h"
36 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
37 static struct i2c_adapter bttv_i2c_adap_sw_template;
38 static struct i2c_adapter bttv_i2c_adap_hw_template;
39 static struct i2c_client bttv_i2c_client_template;
41 #ifndef I2C_PEC
42 static void bttv_inc_use(struct i2c_adapter *adap);
43 static void bttv_dec_use(struct i2c_adapter *adap);
44 #endif
45 static int attach_inform(struct i2c_client *client);
47 EXPORT_SYMBOL(bttv_get_cardinfo);
48 EXPORT_SYMBOL(bttv_get_pcidev);
49 EXPORT_SYMBOL(bttv_get_id);
50 EXPORT_SYMBOL(bttv_gpio_enable);
51 EXPORT_SYMBOL(bttv_read_gpio);
52 EXPORT_SYMBOL(bttv_write_gpio);
53 EXPORT_SYMBOL(bttv_get_gpio_queue);
54 EXPORT_SYMBOL(bttv_i2c_call);
56 static int i2c_debug = 0;
57 static int i2c_hw = 0;
58 MODULE_PARM(i2c_debug,"i");
59 MODULE_PARM(i2c_hw,"i");
61 /* ----------------------------------------------------------------------- */
62 /* Exported functions - for other modules which want to access the */
63 /* gpio ports (IR for example) */
64 /* see bttv.h for comments */
66 int bttv_get_cardinfo(unsigned int card, int *type, unsigned *cardid)
68 if (card >= bttv_num) {
69 return -1;
71 *type = bttvs[card].type;
72 *cardid = bttvs[card].cardid;
73 return 0;
76 struct pci_dev* bttv_get_pcidev(unsigned int card)
78 if (card >= bttv_num)
79 return NULL;
80 return bttvs[card].dev;
83 int bttv_get_id(unsigned int card)
85 printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
86 if (card >= bttv_num) {
87 return -1;
89 return bttvs[card].type;
93 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
95 struct bttv *btv;
97 if (card >= bttv_num) {
98 return -EINVAL;
101 btv = &bttvs[card];
102 btaor(data, ~mask, BT848_GPIO_OUT_EN);
103 if (bttv_gpio)
104 bttv_gpio_tracking(btv,"extern enable");
105 return 0;
108 int bttv_read_gpio(unsigned int card, unsigned long *data)
110 struct bttv *btv;
112 if (card >= bttv_num) {
113 return -EINVAL;
116 btv = &bttvs[card];
118 if(btv->shutdown) {
119 return -ENODEV;
122 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
123 because we set direct input on init */
124 *data = btread(BT848_GPIO_DATA);
125 return 0;
128 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
130 struct bttv *btv;
132 if (card >= bttv_num) {
133 return -EINVAL;
136 btv = &bttvs[card];
138 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
139 because direct input is set on init */
140 btaor(data & mask, ~mask, BT848_GPIO_DATA);
141 if (bttv_gpio)
142 bttv_gpio_tracking(btv,"extern write");
143 return 0;
146 wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
148 struct bttv *btv;
150 if (card >= bttv_num) {
151 return NULL;
154 btv = &bttvs[card];
155 if (bttvs[card].shutdown) {
156 return NULL;
158 return &btv->gpioq;
162 /* ----------------------------------------------------------------------- */
163 /* I2C functions - bitbanging adapter (software i2c) */
165 void bttv_bit_setscl(void *data, int state)
167 struct bttv *btv = (struct bttv*)data;
169 if (state)
170 btv->i2c_state |= 0x02;
171 else
172 btv->i2c_state &= ~0x02;
173 btwrite(btv->i2c_state, BT848_I2C);
174 btread(BT848_I2C);
177 void bttv_bit_setsda(void *data, int state)
179 struct bttv *btv = (struct bttv*)data;
181 if (state)
182 btv->i2c_state |= 0x01;
183 else
184 btv->i2c_state &= ~0x01;
185 btwrite(btv->i2c_state, BT848_I2C);
186 btread(BT848_I2C);
189 static int bttv_bit_getscl(void *data)
191 struct bttv *btv = (struct bttv*)data;
192 int state;
194 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
195 return state;
198 static int bttv_bit_getsda(void *data)
200 struct bttv *btv = (struct bttv*)data;
201 int state;
203 state = btread(BT848_I2C) & 0x01;
204 return state;
207 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
208 .setsda = bttv_bit_setsda,
209 .setscl = bttv_bit_setscl,
210 .getsda = bttv_bit_getsda,
211 .getscl = bttv_bit_getscl,
212 .udelay = 16,
213 .mdelay = 10,
214 .timeout = 200,
217 static struct i2c_adapter bttv_i2c_adap_sw_template = {
218 #ifdef I2C_PEC
219 .owner = THIS_MODULE,
220 #else
221 .inc_use = bttv_inc_use,
222 .dec_use = bttv_dec_use,
223 #endif
224 #ifdef I2C_ADAP_CLASS_TV_ANALOG
225 .class = I2C_ADAP_CLASS_TV_ANALOG,
226 #endif
227 I2C_DEVNAME("bt848"),
228 .id = I2C_HW_B_BT848,
229 .client_register = attach_inform,
232 /* ----------------------------------------------------------------------- */
233 /* I2C functions - hardware i2c */
235 static int algo_control(struct i2c_adapter *adapter,
236 unsigned int cmd, unsigned long arg)
238 return 0;
241 static u32 functionality(struct i2c_adapter *adap)
243 return I2C_FUNC_SMBUS_EMUL;
246 static int
247 bttv_i2c_wait_done(struct bttv *btv)
249 u32 stat;
250 int timeout;
252 timeout = jiffies + HZ/100 + 1; /* 10ms */
253 for (;;) {
254 stat = btread(BT848_INT_STAT);
255 if (stat & BT848_INT_I2CDONE)
256 break;
257 if (time_after(jiffies,timeout))
258 return -EIO;
259 udelay(10);
261 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
262 return ((stat & BT848_INT_RACK) ? 1 : 0);
265 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
266 BT848_I2C_SCL | BT848_I2C_SDA)
268 static int
269 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
271 u32 xmit;
272 int retval,cnt;
274 /* start, address + first byte */
275 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
276 if (msg->len > 1 || !last)
277 xmit |= BT878_I2C_NOSTOP;
278 btwrite(xmit, BT848_I2C);
279 retval = bttv_i2c_wait_done(btv);
280 if (retval < 0)
281 goto err;
282 if (retval == 0)
283 goto eio;
284 if (i2c_debug) {
285 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
286 if (!(xmit & BT878_I2C_NOSTOP))
287 printk(" >\n");
290 for (cnt = 1; cnt < msg->len; cnt++ ) {
291 /* following bytes */
292 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
293 if (cnt < msg->len-1 || !last)
294 xmit |= BT878_I2C_NOSTOP;
295 btwrite(xmit, BT848_I2C);
296 retval = bttv_i2c_wait_done(btv);
297 if (retval < 0)
298 goto err;
299 if (retval == 0)
300 goto eio;
301 if (i2c_debug) {
302 printk(" %02x", msg->buf[cnt]);
303 if (!(xmit & BT878_I2C_NOSTOP))
304 printk(" >\n");
307 return msg->len;
309 eio:
310 retval = -EIO;
311 err:
312 if (i2c_debug)
313 printk(" ERR: %d\n",retval);
314 return retval;
317 static int
318 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
320 u32 xmit;
321 u32 cnt;
322 int retval;
324 for(cnt = 0; cnt < msg->len; cnt++) {
325 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
326 if (cnt < msg->len-1)
327 xmit |= BT848_I2C_W3B;
328 if (cnt < msg->len-1 || !last)
329 xmit |= BT878_I2C_NOSTOP;
330 if (cnt)
331 xmit |= BT878_I2C_NOSTART;
332 btwrite(xmit, BT848_I2C);
333 retval = bttv_i2c_wait_done(btv);
334 if (retval < 0)
335 goto err;
336 if (retval == 0)
337 goto eio;
338 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
339 if (i2c_debug) {
340 if (!(xmit & BT878_I2C_NOSTART))
341 printk(" <R %02x", (msg->addr << 1) +1);
342 printk(" =%02x", msg->buf[cnt]);
343 if (!(xmit & BT878_I2C_NOSTOP))
344 printk(" >\n");
347 return msg->len;
349 eio:
350 retval = -EIO;
351 err:
352 if (i2c_debug)
353 printk(" ERR: %d\n",retval);
354 return retval;
357 int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
359 struct bttv *btv = i2c_get_adapdata(i2c_adap);
360 int retval = 0;
361 int i;
363 if (i2c_debug)
364 printk("bt-i2c:");
365 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
366 for (i = 0 ; i < num; i++) {
367 if (msgs[i].flags & I2C_M_RD) {
368 /* read */
369 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
370 if (retval < 0)
371 goto err;
372 } else {
373 /* write */
374 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
375 if (retval < 0)
376 goto err;
379 return num;
381 err:
382 return retval;
385 static struct i2c_algorithm bttv_algo = {
386 .name = "bt878",
387 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
388 .master_xfer = bttv_i2c_xfer,
389 .algo_control = algo_control,
390 .functionality = functionality,
393 static struct i2c_adapter bttv_i2c_adap_hw_template = {
394 #ifdef I2C_PEC
395 .owner = THIS_MODULE,
396 #else
397 .inc_use = bttv_inc_use,
398 .dec_use = bttv_dec_use,
399 #endif
400 #ifdef I2C_ADAP_CLASS_TV_ANALOG
401 .class = I2C_ADAP_CLASS_TV_ANALOG,
402 #endif
403 I2C_DEVNAME("bt878"),
404 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
405 .algo = &bttv_algo,
406 .client_register = attach_inform,
409 /* ----------------------------------------------------------------------- */
410 /* I2C functions - common stuff */
412 #ifndef I2C_PEC
413 static void bttv_inc_use(struct i2c_adapter *adap)
415 MOD_INC_USE_COUNT;
418 static void bttv_dec_use(struct i2c_adapter *adap)
420 MOD_DEC_USE_COUNT;
422 #endif
424 static int attach_inform(struct i2c_client *client)
426 struct bttv *btv = i2c_get_adapdata(client->adapter);
428 if (btv->tuner_type != UNSET)
429 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
430 if (btv->pinnacle_id != UNSET)
431 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
432 &btv->pinnacle_id);
434 if (bttv_debug)
435 printk("bttv%d: i2c attach [client=%s]\n",
436 btv->nr, i2c_clientname(client));
437 return 0;
440 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
442 if (0 != btv->i2c_rc)
443 return;
444 i2c_clients_command(&btv->i2c_adap, cmd, arg);
447 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
449 if (card >= bttv_num)
450 return;
451 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
454 static struct i2c_client bttv_i2c_client_template = {
455 I2C_DEVNAME("bttv internal"),
456 .id = -1,
460 /* read I2C */
461 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
463 unsigned char buffer = 0;
465 if (0 != btv->i2c_rc)
466 return -1;
467 if (bttv_verbose && NULL != probe_for)
468 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
469 btv->nr,probe_for,addr);
470 btv->i2c_client.addr = addr >> 1;
471 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
472 if (NULL != probe_for) {
473 if (bttv_verbose)
474 printk("not found\n");
475 } else
476 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
477 btv->nr,addr);
478 return -1;
480 if (bttv_verbose && NULL != probe_for)
481 printk("found\n");
482 return buffer;
485 /* write I2C */
486 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
487 unsigned char b2, int both)
489 unsigned char buffer[2];
490 int bytes = both ? 2 : 1;
492 if (0 != btv->i2c_rc)
493 return -1;
494 btv->i2c_client.addr = addr >> 1;
495 buffer[0] = b1;
496 buffer[1] = b2;
497 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
498 return -1;
499 return 0;
502 /* read EEPROM content */
503 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
505 int i;
507 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
508 printk(KERN_WARNING "bttv: readee error\n");
509 return;
511 btv->i2c_client.addr = addr >> 1;
512 for (i=0; i<256; i+=16) {
513 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
514 printk(KERN_WARNING "bttv: readee error\n");
515 break;
520 /* init + register i2c algo-bit adapter */
521 int __devinit init_bttv_i2c(struct bttv *btv)
523 int use_hw = (btv->id == 878) && i2c_hw;
525 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
526 sizeof(bttv_i2c_client_template));
528 if (use_hw) {
529 /* bt878 */
530 memcpy(&btv->i2c_adap, &bttv_i2c_adap_hw_template,
531 sizeof(bttv_i2c_adap_hw_template));
532 } else {
533 /* bt848 */
534 memcpy(&btv->i2c_adap, &bttv_i2c_adap_sw_template,
535 sizeof(bttv_i2c_adap_sw_template));
536 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
537 sizeof(bttv_i2c_algo_bit_template));
538 btv->i2c_algo.data = btv;
539 btv->i2c_adap.algo_data = &btv->i2c_algo;
542 btv->i2c_adap.dev.parent = &btv->dev->dev;
543 snprintf(btv->i2c_adap.name, sizeof(btv->i2c_adap.name),
544 "bt%d #%d [%s]", btv->id, btv->nr, use_hw ? "hw" : "sw");
546 i2c_set_adapdata(&btv->i2c_adap, btv);
547 btv->i2c_client.adapter = &btv->i2c_adap;
549 if (use_hw) {
550 btv->i2c_rc = i2c_add_adapter(&btv->i2c_adap);
551 } else {
552 bttv_bit_setscl(btv,1);
553 bttv_bit_setsda(btv,1);
554 btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);
556 return btv->i2c_rc;
559 int __devexit fini_bttv_i2c(struct bttv *btv)
561 int use_hw = (btv->id == 878) && i2c_hw;
563 if (0 != btv->i2c_rc)
564 return 0;
566 if (use_hw) {
567 return i2c_del_adapter(&btv->i2c_adap);
568 } else {
569 return i2c_bit_del_bus(&btv->i2c_adap);
574 * Local variables:
575 * c-basic-offset: 8
576 * End: