Subversion Repositories shark

Rev

Rev 463 | Go to most recent revision | Details | 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)
10
    (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
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
 
28
/* SHARK version by Giacomo Guidi <giacomo@gandalf.sssup.it> */
29
 
30
#define __NO_VERSION__ 1
31
 
32
#include <linux/pci.h>
33
#include <string.h>
34
#include <asm/io.h>
35
 
36
#include "drivers/bttv.h"
37
#include "drivers/tuner.h"
38
 
39
#define printk cprintf
40
 
41
static struct i2c_algo_bit_data bttv_i2c_algo_template;
42
static struct i2c_adapter bttv_i2c_adap_template;
43
static struct i2c_client bttv_i2c_client_template;
44
 
45
/* ----------------------------------------------------------------------- */
46
/* Exported functions - for other modules which want to access the         */
47
/*                      gpio ports (IR for example)                        */
48
/*                      see bttv.h for comments                            */
49
 
50
int bttv_get_cardinfo(unsigned int card, int *type, int *cardid)
51
{
52
        if (card >= bttv_num) {
53
                return -1;
54
        }
55
        *type   = bttvs[card].type;
56
        *cardid = bttvs[card].cardid;
57
        return 0;
58
}
59
 
60
int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
61
{
62
        struct bttv *btv;
63
 
64
        if (card >= bttv_num) {
65
                return -EINVAL;
66
        }
67
 
68
        btv = &bttvs[card];
69
        btaor(data, ~mask, BT848_GPIO_OUT_EN);
70
        if (bttv_gpio)
71
                bttv_gpio_tracking(btv,"extern enable");
72
        return 0;
73
}
74
 
75
int bttv_read_gpio(unsigned int card, unsigned long *data)
76
{
77
        struct bttv *btv;
78
 
79
        if (card >= bttv_num) {
80
                return -EINVAL;
81
        }
82
 
83
        btv = &bttvs[card];
84
 
85
        if(btv->shutdown) {
86
                return -ENODEV;
87
        }
88
 
89
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
90
   because we set direct input on init */
91
        *data = gpioread();
92
        return 0;
93
}
94
 
95
int bttv_write_gpio(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
 
105
/* prior setting BT848_GPIO_REG_INP is (probably) not needed
106
   because direct input is set on init */
107
        gpioaor(data & mask, ~mask);
108
        if (bttv_gpio)
109
                bttv_gpio_tracking(btv,"extern write");
110
        return 0;
111
}
112
 
113
/* ----------------------------------------------------------------------- */
114
/* I2C functions                                                           */
115
 
116
void bttv_bit_setscl(void *data, int state)
117
{
118
        struct bttv *btv = (struct bttv*)data;
119
        unsigned long tmp;
120
 
121
        if (state)
122
                btv->i2c_state |= 0x02;
123
        else
124
                btv->i2c_state &= ~0x02;
125
        btwrite(btv->i2c_state, BT848_I2C);
126
        tmp = btread(BT848_I2C);
127
}
128
 
129
void bttv_bit_setsda(void *data, int state)
130
{
131
        struct bttv *btv = (struct bttv*)data;
132
        unsigned long tmp;
133
 
134
        if (state)
135
                btv->i2c_state |= 0x01;
136
        else
137
                btv->i2c_state &= ~0x01;
138
        btwrite(btv->i2c_state, BT848_I2C);
139
        tmp = btread(BT848_I2C);
140
}
141
 
142
static int bttv_bit_getscl(void *data)
143
{
144
        struct bttv *btv = (struct bttv*)data;
145
        int state;
146
 
147
        state = btread(BT848_I2C) & 0x02 ? 1 : 0;
148
        return state;
149
}
150
 
151
static int bttv_bit_getsda(void *data)
152
{
153
        struct bttv *btv = (struct bttv*)data;
154
        int state;
155
 
156
        state = btread(BT848_I2C) & 0x01;
157
        return state;
158
}
159
 
160
static void bttv_inc_use(struct i2c_adapter *adap)
161
{
162
        MOD_INC_USE_COUNT;
163
}
164
 
165
static void bttv_dec_use(struct i2c_adapter *adap)
166
{
167
        MOD_DEC_USE_COUNT;
168
}
169
 
170
static int attach_inform(struct i2c_client *client)
171
{
172
        struct bttv *btv = (struct bttv*)client->adapter->data;
173
        int i;
174
 
175
        for (i = 0; i < I2C_CLIENTS_MAX; i++) {
176
                if (btv->i2c_clients[i] == NULL) {
177
                        btv->i2c_clients[i] = client;
178
                        break;
179
                }
180
        }
181
        if (btv->tuner_type != -1)
182
                bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
183
        if (bttv_verbose)
184
                cprintf("bttv%d: i2c attach [client=%s,%s]\n",btv->nr,
185
                       client->name, (i < I2C_CLIENTS_MAX) ?  "ok" : "failed");
186
        return 0;
187
}
188
 
189
static int detach_inform(struct i2c_client *client)
190
{
191
        struct bttv *btv = (struct bttv*)client->adapter->data;
192
        int i;
193
 
194
        for (i = 0; i < I2C_CLIENTS_MAX; i++) {
195
                if (btv->i2c_clients[i] == client) {
196
                        btv->i2c_clients[i] = NULL;
197
                        break;
198
                }
199
        }
200
        if (bttv_verbose)
201
                cprintf("bttv%d: i2c detach [client=%s,%s]\n",btv->nr,
202
                       client->name, (i < I2C_CLIENTS_MAX) ?  "ok" : "failed");
203
        return 0;
204
}
205
 
206
void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
207
{
208
        int i;
209
 
210
        for (i = 0; i < I2C_CLIENTS_MAX; i++) {
211
                if (NULL == btv->i2c_clients[i])
212
                        continue;
213
                if (NULL == btv->i2c_clients[i]->driver->command)
214
                        continue;
215
                btv->i2c_clients[i]->driver->command(
216
                        btv->i2c_clients[i],cmd,arg);
217
        }
218
}
219
 
220
static struct i2c_algo_bit_data bttv_i2c_algo_template = {
221
        setsda:  bttv_bit_setsda,
222
        setscl:  bttv_bit_setscl,
223
        getsda:  bttv_bit_getsda,
224
        getscl:  bttv_bit_getscl,
225
        udelay:  16,
226
        mdelay:  10,
227
        timeout: 200,
228
};
229
 
230
static struct i2c_adapter bttv_i2c_adap_template = {
231
        name:              "bt848",
232
        id:                I2C_HW_B_BT848,
233
        inc_use:           bttv_inc_use,
234
        dec_use:           bttv_dec_use,
235
        client_register:   attach_inform,
236
        client_unregister: detach_inform,
237
};
238
 
239
static struct i2c_client bttv_i2c_client_template = {
240
        name: "bttv internal use only",
241
        id:   -1,
242
};
243
 
244
 
245
/* read I2C */
246
int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
247
{
248
        unsigned char buffer = 0;
249
 
250
        if (0 != btv->i2c_rc)
251
                return -1;
252
        if (bttv_verbose && NULL != probe_for)
253
                printk("[info  ] bttv%d: i2c: checking for %s @ 0x%02x... ",
254
                       btv->nr,probe_for,addr);
255
        btv->i2c_client.addr = addr >> 1;
256
        if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
257
                if (NULL != probe_for) {
258
                        if (bttv_verbose)
259
                                printk("not found\n");
260
                } else
261
                        printk("bttv%d: i2c read 0x%x: error\n",
262
                               btv->nr,addr);
263
                return -1;
264
        }
265
        if (bttv_verbose && NULL != probe_for)
266
                printk("found\n");
267
        return buffer;
268
}
269
 
270
/* write I2C */
271
int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
272
                    unsigned char b2, int both)
273
{
274
        unsigned char buffer[2];
275
        int bytes = both ? 2 : 1;
276
 
277
        if (0 != btv->i2c_rc)
278
                return -1;
279
        btv->i2c_client.addr = addr >> 1;
280
        buffer[0] = b1;
281
        buffer[1] = b2;
282
        if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
283
                return -1;
284
        return 0;
285
}
286
 
287
/* read EEPROM content */
288
void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
289
{
290
        int i;
291
 
292
        if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
293
                printk("bttv: readee error\n");
294
                return;
295
        }
296
        btv->i2c_client.addr = addr >> 1;
297
        for (i=0; i<256; i+=16) {
298
                if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
299
                        printk("bttv: readee error\n");
300
                        break;
301
                }
302
        }
303
}
304
 
305
/* TODO: get an assigned bus id */
306
#define I2C_HW_B_BT848_OSPREY 0x0FF
307
 
308
/* init + register i2c algo-bit adapter */
309
int init_bttv_i2c(struct bttv *btv)
310
{
311
        memcpy(&btv->i2c_adap, &bttv_i2c_adap_template,
312
               sizeof(struct i2c_adapter));
313
 
314
        if ((btv->type == BTTV_OSPREY2000) ||
315
            (btv->type == BTTV_OSPREY5x0)) {
316
            /* these boards have the bt860 which conflicts with the tda7432
317
             * which will attempt to load when this type of bus is registered.
318
             * change the bus name to prevent this (and any other conflicts)
319
             * from occuring */
320
            btv->i2c_adap.id = I2C_HW_B_BT848_OSPREY;
321
        }
322
 
323
        memcpy(&btv->i2c_algo, &bttv_i2c_algo_template,
324
               sizeof(struct i2c_algo_bit_data));
325
        memcpy(&btv->i2c_client, &bttv_i2c_client_template,
326
               sizeof(struct i2c_client));
327
 
328
        sprintf(btv->i2c_adap.name+strlen(btv->i2c_adap.name),
329
                " #%d", btv->nr);
330
        btv->i2c_algo.data = btv;
331
        btv->i2c_adap.data = btv;
332
        btv->i2c_adap.algo_data = &btv->i2c_algo;
333
        btv->i2c_client.adapter = &btv->i2c_adap;
334
 
335
        bttv_bit_setscl(btv,1);
336
        bttv_bit_setsda(btv,1);
337
 
338
        printk("[info  ] bttv: adding i2c bus to system\n");
339
        btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);
340
        printk("[info  ] bttv: i2c bus added to system\n");
341
        return btv->i2c_rc;
342
}
343
 
344
/* find a bttv struct by matching some data from the pci bus (for audio) */
345
struct bttv *bttv_find_matching_card( struct pci_dev *dev )
346
{
347
        int i;
348
 
349
        if (dev == NULL) return NULL;
350
        for (i = 0;i < bttv_num;i++) {
351
                if (bttvs[i].dev == NULL) {
352
                        continue; //hmm...can this ever happen?
353
                }
354
                if ((dev->bus->number == bttvs[i].dev->bus->number) &&
355
                    (PCI_SLOT(dev->devfn) == PCI_SLOT(bttvs[i].dev->devfn))) {
356
                        //match! return this one.
357
                        return &(bttvs[i]);
358
                }
359
        }
360
        return NULL;
361
}
362
 
363
struct gpio_adapter *bttv_get_gpio( struct bttv *btv )
364
{
365
        return &(btv->gpio_adap);
366
}
367
 
368
/*
369
 * Local variables:
370
 * c-basic-offset: 8
371
 * End:
372
 */