Subversion Repositories shark

Rev

Rev 463 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
170 giacomo 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)
5
 
6
    bttv - Bt848 frame grabber driver
7
 
8
    Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
9
                           & Marcus Metzler (mocm@thp.uni-koeln.de)
428 giacomo 10
    (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
170 giacomo 11
 
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.
16
 
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.
21
 
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.
25
 
26
*/
27
 
428 giacomo 28
#include <linuxcomp.h>
170 giacomo 29
 
428 giacomo 30
#include <linux/module.h>
31
#include <linux/init.h>
32
#include <linux/delay.h>
170 giacomo 33
 
34
#include <asm/io.h>
35
 
428 giacomo 36
#include "drivers/bttvp.h"
170 giacomo 37
 
428 giacomo 38
static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
39
static struct i2c_adapter bttv_i2c_adap_sw_template;
40
static struct i2c_adapter bttv_i2c_adap_hw_template;
170 giacomo 41
static struct i2c_client bttv_i2c_client_template;
42
 
428 giacomo 43
#ifndef I2C_PEC
44
static void bttv_inc_use(struct i2c_adapter *adap);
45
static void bttv_dec_use(struct i2c_adapter *adap);
46
#endif
47
static int attach_inform(struct i2c_client *client);
48
 
49
EXPORT_SYMBOL(bttv_get_cardinfo);
50
EXPORT_SYMBOL(bttv_get_pcidev);
51
EXPORT_SYMBOL(bttv_get_id);
52
EXPORT_SYMBOL(bttv_gpio_enable);
53
EXPORT_SYMBOL(bttv_read_gpio);
54
EXPORT_SYMBOL(bttv_write_gpio);
55
EXPORT_SYMBOL(bttv_get_gpio_queue);
56
EXPORT_SYMBOL(bttv_i2c_call);
57
 
463 giacomo 58
static int i2c_debug = 1;
428 giacomo 59
static int i2c_hw = 0;
60
MODULE_PARM(i2c_debug,"i");
61
MODULE_PARM(i2c_hw,"i");
62
 
170 giacomo 63
/* ----------------------------------------------------------------------- */
64
/* Exported functions - for other modules which want to access the         */
65
/*                      gpio ports (IR for example)                        */
66
/*                      see bttv.h for comments                            */
67
 
428 giacomo 68
int bttv_get_cardinfo(unsigned int card, int *type, unsigned *cardid)
170 giacomo 69
{
70
        if (card >= bttv_num) {
71
                return -1;
72
        }
73
        *type   = bttvs[card].type;
74
        *cardid = bttvs[card].cardid;
75
        return 0;
76
}
77
 
428 giacomo 78
struct pci_dev* bttv_get_pcidev(unsigned int card)
79
{
80
        if (card >= bttv_num)
81
                return NULL;
82
        return bttvs[card].dev;
83
}
84
 
85
int bttv_get_id(unsigned int card)
86
{
87
        printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
88
        if (card >= bttv_num) {
89
                return -1;
90
        }
91
        return bttvs[card].type;
92
}
93
 
94
 
170 giacomo 95
int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
96
{
97
        struct bttv *btv;
98
 
99
        if (card >= bttv_num) {
100
                return -EINVAL;
101
        }
102
 
103
        btv = &bttvs[card];
104
        btaor(data, ~mask, BT848_GPIO_OUT_EN);
105
        if (bttv_gpio)
106
                bttv_gpio_tracking(btv,"extern enable");
107
        return 0;
108
}
109
 
110
int bttv_read_gpio(unsigned int card, unsigned long *data)
111
{
112
        struct bttv *btv;
113
 
114
        if (card >= bttv_num) {
115
                return -EINVAL;
116
        }
117
 
118
        btv = &bttvs[card];
119
 
120
        if(btv->shutdown) {
121
                return -ENODEV;
122
        }
123
 
124
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
125
   because we set direct input on init */
428 giacomo 126
        *data = btread(BT848_GPIO_DATA);
170 giacomo 127
        return 0;
128
}
129
 
130
int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
131
{
132
        struct bttv *btv;
133
 
134
        if (card >= bttv_num) {
135
                return -EINVAL;
136
        }
137
 
138
        btv = &bttvs[card];
139
 
140
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
141
   because direct input is set on init */
428 giacomo 142
        btaor(data & mask, ~mask, BT848_GPIO_DATA);
170 giacomo 143
        if (bttv_gpio)
144
                bttv_gpio_tracking(btv,"extern write");
145
        return 0;
146
}
147
 
428 giacomo 148
wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
149
{
150
        struct bttv *btv;
151
 
152
        if (card >= bttv_num) {
153
                return NULL;
154
        }
155
 
156
        btv = &bttvs[card];
157
        if (bttvs[card].shutdown) {
158
                return NULL;
159
        }
160
        return &btv->gpioq;
161
}
162
 
163
 
170 giacomo 164
/* ----------------------------------------------------------------------- */
428 giacomo 165
/* I2C functions - bitbanging adapter (software i2c)                       */
170 giacomo 166
 
167
void bttv_bit_setscl(void *data, int state)
168
{
169
        struct bttv *btv = (struct bttv*)data;
428 giacomo 170
 
170 giacomo 171
        if (state)
172
                btv->i2c_state |= 0x02;
173
        else
174
                btv->i2c_state &= ~0x02;
175
        btwrite(btv->i2c_state, BT848_I2C);
428 giacomo 176
        btread(BT848_I2C);
170 giacomo 177
}
178
 
179
void bttv_bit_setsda(void *data, int state)
180
{
181
        struct bttv *btv = (struct bttv*)data;
428 giacomo 182
 
170 giacomo 183
        if (state)
184
                btv->i2c_state |= 0x01;
185
        else
186
                btv->i2c_state &= ~0x01;
187
        btwrite(btv->i2c_state, BT848_I2C);
428 giacomo 188
        btread(BT848_I2C);
170 giacomo 189
}
190
 
191
static int bttv_bit_getscl(void *data)
192
{
193
        struct bttv *btv = (struct bttv*)data;
194
        int state;
195
 
196
        state = btread(BT848_I2C) & 0x02 ? 1 : 0;
197
        return state;
198
}
199
 
200
static int bttv_bit_getsda(void *data)
201
{
202
        struct bttv *btv = (struct bttv*)data;
203
        int state;
204
 
205
        state = btread(BT848_I2C) & 0x01;
206
        return state;
207
}
208
 
428 giacomo 209
static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
210
        .setsda  = bttv_bit_setsda,
211
        .setscl  = bttv_bit_setscl,
212
        .getsda  = bttv_bit_getsda,
213
        .getscl  = bttv_bit_getscl,
214
        .udelay  = 16,
215
        .mdelay  = 10,
216
        .timeout = 200,
217
};
218
 
219
static struct i2c_adapter bttv_i2c_adap_sw_template = {
220
#ifdef I2C_PEC
221
        .owner             = THIS_MODULE,
222
#else
223
        .inc_use           = bttv_inc_use,
224
        .dec_use           = bttv_dec_use,
225
#endif
226
#ifdef I2C_ADAP_CLASS_TV_ANALOG
227
        .class             = I2C_ADAP_CLASS_TV_ANALOG,
228
#endif
229
        I2C_DEVNAME("bt848"),
230
        .id                = I2C_HW_B_BT848,
231
        .client_register   = attach_inform,
232
};
233
 
234
/* ----------------------------------------------------------------------- */
235
/* I2C functions - hardware i2c                                            */
236
 
237
static int algo_control(struct i2c_adapter *adapter,
238
                        unsigned int cmd, unsigned long arg)
170 giacomo 239
{
428 giacomo 240
        return 0;
170 giacomo 241
}
242
 
428 giacomo 243
static u32 functionality(struct i2c_adapter *adap)
170 giacomo 244
{
428 giacomo 245
        return I2C_FUNC_SMBUS_EMUL;
170 giacomo 246
}
247
 
464 giacomo 248
extern unsigned long read_time(void);
249
 
428 giacomo 250
static int
251
bttv_i2c_wait_done(struct bttv *btv)
170 giacomo 252
{
428 giacomo 253
        u32 stat;
254
        unsigned long timeout;
170 giacomo 255
 
464 giacomo 256
        timeout = read_time() + 10000; /* 10ms */
428 giacomo 257
        for (;;) {
258
                stat = btread(BT848_INT_STAT);
259
                if (stat & BT848_INT_I2CDONE)
170 giacomo 260
                        break;
464 giacomo 261
                if (timeout > read_time())
428 giacomo 262
                        return -EIO;
263
                udelay(10);
264
        }
265
        btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
266
        return ((stat & BT848_INT_RACK) ? 1 : 0);
267
}
268
 
269
#define I2C_HW (BT878_I2C_MODE  | BT848_I2C_SYNC |\
270
                BT848_I2C_SCL | BT848_I2C_SDA)
271
 
272
static int
273
bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
274
{
275
        u32 xmit;
276
        int retval,cnt;
277
 
278
        /* start, address + first byte */
279
        xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
280
        if (msg->len > 1 || !last)
281
                xmit |= BT878_I2C_NOSTOP;
282
        btwrite(xmit, BT848_I2C);
283
        retval = bttv_i2c_wait_done(btv);
284
        if (retval < 0)
285
                goto err;
286
        if (retval == 0)
287
                goto eio;
288
        if (i2c_debug) {
289
                printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
290
                if (!(xmit & BT878_I2C_NOSTOP))
291
                        printk(" >\n");
292
        }
293
 
294
        for (cnt = 1; cnt < msg->len; cnt++ ) {
295
                /* following bytes */
296
                xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
297
                if (cnt < msg->len-1 || !last)
298
                        xmit |= BT878_I2C_NOSTOP;
299
                btwrite(xmit, BT848_I2C);
300
                retval = bttv_i2c_wait_done(btv);
301
                if (retval < 0)
302
                        goto err;
303
                if (retval == 0)
304
                        goto eio;
305
                if (i2c_debug) {
306
                        printk(" %02x", msg->buf[cnt]);
307
                        if (!(xmit & BT878_I2C_NOSTOP))
308
                                printk(" >\n");
170 giacomo 309
                }
310
        }
428 giacomo 311
        return msg->len;
312
 
313
 eio:
314
        retval = -EIO;
315
 err:
316
        if (i2c_debug)
317
                printk(" ERR: %d\n",retval);
318
        return retval;
170 giacomo 319
}
320
 
428 giacomo 321
static int
322
bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
170 giacomo 323
{
428 giacomo 324
        u32 xmit;
325
        u32 cnt;
326
        int retval;
170 giacomo 327
 
428 giacomo 328
        for(cnt = 0; cnt < msg->len; cnt++) {
329
                xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
330
                if (cnt < msg->len-1)
331
                        xmit |= BT848_I2C_W3B;
332
                if (cnt < msg->len-1 || !last)
333
                        xmit |= BT878_I2C_NOSTOP;
334
                if (cnt)
335
                        xmit |= BT878_I2C_NOSTART;
336
                btwrite(xmit, BT848_I2C);
337
                retval = bttv_i2c_wait_done(btv);
338
                if (retval < 0)
339
                        goto err;
340
                if (retval == 0)
341
                        goto eio;
342
                msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
343
                if (i2c_debug) {
344
                        if (!(xmit & BT878_I2C_NOSTART))
345
                                printk(" <R %02x", (msg->addr << 1) +1);
346
                        printk(" =%02x", msg->buf[cnt]);
347
                        if (!(xmit & BT878_I2C_NOSTOP))
348
                                printk(" >\n");
170 giacomo 349
                }
350
        }
428 giacomo 351
        return msg->len;
352
 
353
 eio:
354
        retval = -EIO;
355
 err:
356
        if (i2c_debug)
357
                printk(" ERR: %d\n",retval);
358
        return retval;
170 giacomo 359
}
360
 
428 giacomo 361
int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
170 giacomo 362
{
428 giacomo 363
        struct bttv *btv = i2c_get_adapdata(i2c_adap);
364
        int retval = 0;
170 giacomo 365
        int i;
428 giacomo 366
 
367
        if (i2c_debug)
368
                printk("bt-i2c:");
369
        btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
370
        for (i = 0 ; i < num; i++) {
371
                if (msgs[i].flags & I2C_M_RD) {
372
                        /* read */
373
                        retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
374
                        if (retval < 0)
375
                                goto err;
376
                } else {
377
                        /* write */
378
                        retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
379
                        if (retval < 0)
380
                                goto err;
381
                }
170 giacomo 382
        }
428 giacomo 383
        return num;
384
 
385
 err:
386
        return retval;
170 giacomo 387
}
388
 
428 giacomo 389
static struct i2c_algorithm bttv_algo = {
390
        .name          = "bt878",
391
        .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
392
        .master_xfer   = bttv_i2c_xfer,
393
        .algo_control  = algo_control,
394
        .functionality = functionality,
170 giacomo 395
};
396
 
428 giacomo 397
static struct i2c_adapter bttv_i2c_adap_hw_template = {
398
#ifdef I2C_PEC
399
        .owner         = THIS_MODULE,
400
#else
401
        .inc_use       = bttv_inc_use,
402
        .dec_use       = bttv_dec_use,
403
#endif
404
#ifdef I2C_ADAP_CLASS_TV_ANALOG
405
        .class         = I2C_ADAP_CLASS_TV_ANALOG,
406
#endif
407
        I2C_DEVNAME("bt878"),
408
        .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
409
        .algo          = &bttv_algo,
410
        .client_register = attach_inform,
170 giacomo 411
};
412
 
428 giacomo 413
/* ----------------------------------------------------------------------- */
414
/* I2C functions - common stuff                                            */
415
 
416
#ifndef I2C_PEC
417
static void bttv_inc_use(struct i2c_adapter *adap)
418
{
419
        MOD_INC_USE_COUNT;
420
}
421
 
422
static void bttv_dec_use(struct i2c_adapter *adap)
423
{
424
        MOD_DEC_USE_COUNT;
425
}
426
#endif
427
 
428
static int attach_inform(struct i2c_client *client)
429
{
430
        struct bttv *btv = i2c_get_adapdata(client->adapter);
431
 
432
        if (btv->tuner_type != UNSET)
433
                bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
434
        if (btv->pinnacle_id != UNSET)
435
                bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
436
                                      &btv->pinnacle_id);
437
 
438
        if (bttv_debug)
439
                printk("bttv%d: i2c attach [client=%s]\n",
440
                       btv->nr, i2c_clientname(client));
441
        return 0;
442
}
443
 
444
void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
445
{
446
        if (0 != btv->i2c_rc)
447
                return;
448
        i2c_clients_command(&btv->i2c_adap, cmd, arg);
449
}
450
 
451
void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
452
{
453
        if (card >= bttv_num)
454
                return;
455
        bttv_call_i2c_clients(&bttvs[card], cmd, arg);
456
}
457
 
170 giacomo 458
static struct i2c_client bttv_i2c_client_template = {
428 giacomo 459
        I2C_DEVNAME("bttv internal"),
460
        .id       = -1,
170 giacomo 461
};
462
 
463
 
464
/* read I2C */
465
int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
466
{
467
        unsigned char buffer = 0;
468
 
469
        if (0 != btv->i2c_rc)
470
                return -1;
471
        if (bttv_verbose && NULL != probe_for)
428 giacomo 472
                printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
170 giacomo 473
                       btv->nr,probe_for,addr);
474
        btv->i2c_client.addr = addr >> 1;
475
        if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
476
                if (NULL != probe_for) {
477
                        if (bttv_verbose)
478
                                printk("not found\n");
479
                } else
428 giacomo 480
                        printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
170 giacomo 481
                               btv->nr,addr);
482
                return -1;
483
        }
484
        if (bttv_verbose && NULL != probe_for)
485
                printk("found\n");
486
        return buffer;
487
}
488
 
489
/* write I2C */
490
int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
491
                    unsigned char b2, int both)
492
{
493
        unsigned char buffer[2];
494
        int bytes = both ? 2 : 1;
495
 
496
        if (0 != btv->i2c_rc)
497
                return -1;
498
        btv->i2c_client.addr = addr >> 1;
499
        buffer[0] = b1;
500
        buffer[1] = b2;
501
        if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
502
                return -1;
503
        return 0;
504
}
505
 
506
/* read EEPROM content */
428 giacomo 507
void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
170 giacomo 508
{
509
        int i;
510
 
511
        if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
428 giacomo 512
                printk(KERN_WARNING "bttv: readee error\n");
170 giacomo 513
                return;
514
        }
515
        btv->i2c_client.addr = addr >> 1;
516
        for (i=0; i<256; i+=16) {
517
                if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
428 giacomo 518
                        printk(KERN_WARNING "bttv: readee error\n");
170 giacomo 519
                        break;
520
                }
521
        }
522
}
523
 
524
/* init + register i2c algo-bit adapter */
428 giacomo 525
int __devinit init_bttv_i2c(struct bttv *btv)
170 giacomo 526
{
428 giacomo 527
        int use_hw = (btv->id == 878) && i2c_hw;
528
 
170 giacomo 529
        memcpy(&btv->i2c_client, &bttv_i2c_client_template,
428 giacomo 530
               sizeof(bttv_i2c_client_template));
170 giacomo 531
 
428 giacomo 532
        if (use_hw) {
533
                /* bt878 */
534
                memcpy(&btv->i2c_adap, &bttv_i2c_adap_hw_template,
535
                       sizeof(bttv_i2c_adap_hw_template));
536
        } else {
537
                /* bt848 */
538
                memcpy(&btv->i2c_adap, &bttv_i2c_adap_sw_template,
539
                       sizeof(bttv_i2c_adap_sw_template));
540
                memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
541
                       sizeof(bttv_i2c_algo_bit_template));
542
                btv->i2c_algo.data = btv;
543
                btv->i2c_adap.algo_data = &btv->i2c_algo;
544
        }
545
 
546
        btv->i2c_adap.dev.parent = &btv->dev->dev;
463 giacomo 547
        snprintf26(btv->i2c_adap.name, sizeof(btv->i2c_adap.name),
428 giacomo 548
                 "bt%d #%d [%s]", btv->id, btv->nr, use_hw ? "hw" : "sw");
549
 
550
        i2c_set_adapdata(&btv->i2c_adap, btv);
170 giacomo 551
        btv->i2c_client.adapter = &btv->i2c_adap;
552
 
428 giacomo 553
        if (use_hw) {
554
                btv->i2c_rc = i2c_add_adapter(&btv->i2c_adap);
555
        } else {
556
                bttv_bit_setscl(btv,1);
557
                bttv_bit_setsda(btv,1);
558
                btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);
559
        }
170 giacomo 560
        return btv->i2c_rc;
561
}
562
 
428 giacomo 563
int __devexit fini_bttv_i2c(struct bttv *btv)
170 giacomo 564
{
428 giacomo 565
        int use_hw = (btv->id == 878) && i2c_hw;
566
 
567
        if (0 != btv->i2c_rc)
568
                return 0;
569
 
570
        if (use_hw) {
571
                return i2c_del_adapter(&btv->i2c_adap);
572
        } else {
573
                return i2c_bit_del_bus(&btv->i2c_adap);
170 giacomo 574
        }
575
}
576
 
577
/*
578
 * Local variables:
579
 * c-basic-offset: 8
580
 * End:
581
 */