Merge with Linux 2.4.0-test5-pre3.
[linux-2.6/linux-mips.git] / drivers / char / bttv-if.c
bloba58e441dbf8fc06c7b4a2b5fec914b645cf549f1
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)
7 bttv - Bt848 frame grabber driver
9 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
10 & Marcus Metzler (mocm@thp.uni-koeln.de)
11 (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define __NO_VERSION__ 1
31 #include <linux/version.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
35 #include <asm/io.h>
37 #include "bttv.h"
38 #include "tuner.h"
41 EXPORT_SYMBOL(bttv_get_cardinfo);
42 EXPORT_SYMBOL(bttv_get_id);
43 EXPORT_SYMBOL(bttv_gpio_enable);
44 EXPORT_SYMBOL(bttv_read_gpio);
45 EXPORT_SYMBOL(bttv_write_gpio);
46 EXPORT_SYMBOL(bttv_get_gpio_queue);
48 /* ----------------------------------------------------------------------- */
49 /* Exported functions - for other modules which want to access the */
50 /* gpio ports (IR for example) */
51 /* see bttv.h for comments */
53 int bttv_get_cardinfo(unsigned int card, int *type, int *cardid)
55 if (card >= bttv_num) {
56 return -1;
58 *type = bttvs[card].type;
59 *cardid = bttvs[card].cardid;
60 return 0;
63 int bttv_get_id(unsigned int card)
65 printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
66 if (card >= bttv_num) {
67 return -1;
69 return bttvs[card].type;
72 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
74 struct bttv *btv;
76 if (card >= bttv_num) {
77 return -EINVAL;
80 btv = &bttvs[card];
81 btaor(data, ~mask, BT848_GPIO_OUT_EN);
82 return 0;
85 int bttv_read_gpio(unsigned int card, unsigned long *data)
87 struct bttv *btv;
89 if (card >= bttv_num) {
90 return -EINVAL;
93 btv = &bttvs[card];
95 if(btv->shutdown) {
96 return -ENODEV;
99 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
100 because we set direct input on init */
101 *data = btread(BT848_GPIO_DATA);
102 return 0;
105 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
107 struct bttv *btv;
109 if (card >= bttv_num) {
110 return -EINVAL;
113 btv = &bttvs[card];
115 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
116 because direct input is set on init */
117 btaor(data & mask, ~mask, BT848_GPIO_DATA);
118 return 0;
121 wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
123 struct bttv *btv;
125 if (card >= bttv_num) {
126 return NULL;
129 btv = &bttvs[card];
130 if (bttvs[card].shutdown) {
131 return NULL;
133 return &btv->gpioq;
137 /* ----------------------------------------------------------------------- */
138 /* I2C functions */
140 void bttv_bit_setscl(void *data, int state)
142 struct bttv *btv = (struct bttv*)data;
144 if (state)
145 btv->i2c_state |= 0x02;
146 else
147 btv->i2c_state &= ~0x02;
148 btwrite(btv->i2c_state, BT848_I2C);
149 btread(BT848_I2C);
152 void bttv_bit_setsda(void *data, int state)
154 struct bttv *btv = (struct bttv*)data;
156 if (state)
157 btv->i2c_state |= 0x01;
158 else
159 btv->i2c_state &= ~0x01;
160 btwrite(btv->i2c_state, BT848_I2C);
161 btread(BT848_I2C);
164 static int bttv_bit_getscl(void *data)
166 struct bttv *btv = (struct bttv*)data;
167 int state;
169 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
170 return state;
173 static int bttv_bit_getsda(void *data)
175 struct bttv *btv = (struct bttv*)data;
176 int state;
178 state = btread(BT848_I2C) & 0x01;
179 return state;
182 static void bttv_inc_use(struct i2c_adapter *adap)
184 MOD_INC_USE_COUNT;
187 static void bttv_dec_use(struct i2c_adapter *adap)
189 MOD_DEC_USE_COUNT;
192 static int attach_inform(struct i2c_client *client)
194 struct bttv *btv = (struct bttv*)client->adapter->data;
195 int i;
197 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
198 if (btv->i2c_clients[i] == NULL ||
199 btv->i2c_clients[i]->driver->id == client->driver->id) {
200 btv->i2c_clients[i] = client;
201 break;
204 if (btv->tuner_type != -1)
205 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
206 if (bttv_verbose)
207 printk("bttv%d: i2c attach [%s]\n",btv->nr,client->name);
208 return 0;
211 static int detach_inform(struct i2c_client *client)
213 struct bttv *btv = (struct bttv*)client->adapter->data;
214 int i;
216 if (bttv_verbose)
217 printk("bttv%d: i2c detach [%s]\n",btv->nr,client->name);
218 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
219 if (NULL != btv->i2c_clients[i] &&
220 btv->i2c_clients[i]->driver->id == client->driver->id) {
221 btv->i2c_clients[i] = NULL;
222 break;
225 return 0;
228 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
230 int i;
232 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
233 if (NULL == btv->i2c_clients[i])
234 continue;
235 if (NULL == btv->i2c_clients[i]->driver->command)
236 continue;
237 btv->i2c_clients[i]->driver->command(
238 btv->i2c_clients[i],cmd,arg);
242 struct i2c_algo_bit_data bttv_i2c_algo_template = {
243 NULL,
244 bttv_bit_setsda,
245 bttv_bit_setscl,
246 bttv_bit_getsda,
247 bttv_bit_getscl,
248 10, 10, 100,
251 struct i2c_adapter bttv_i2c_adap_template = {
252 "bt848",
253 I2C_HW_B_BT848,
254 NULL,
255 NULL,
256 bttv_inc_use,
257 bttv_dec_use,
258 attach_inform,
259 detach_inform,
260 NULL,
263 struct i2c_client bttv_i2c_client_template = {
264 "bttv internal",
268 NULL,
269 NULL
273 /* read I2C */
274 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
276 unsigned char buffer = 0;
278 if (0 != btv->i2c_ok)
279 return -1;
280 if (bttv_verbose && NULL != probe_for)
281 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
282 btv->nr,probe_for,addr);
283 btv->i2c_client.addr = addr >> 1;
284 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
285 if (NULL != probe_for) {
286 if (bttv_verbose)
287 printk("not found\n");
288 } else
289 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
290 btv->nr,addr);
291 return -1;
293 if (bttv_verbose && NULL != probe_for)
294 printk("found\n");
295 return buffer;
298 /* write I2C */
299 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
300 unsigned char b2, int both)
302 unsigned char buffer[2];
303 int bytes = both ? 2 : 1;
305 if (0 != btv->i2c_ok)
306 return -1;
307 btv->i2c_client.addr = addr >> 1;
308 buffer[0] = b1;
309 buffer[1] = b2;
310 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
311 return -1;
312 return 0;
315 /* read EEPROM content */
316 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
318 int i;
320 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
321 printk(KERN_WARNING "bttv: readee error\n");
322 return;
324 btv->i2c_client.addr = addr >> 1;
325 for (i=0; i<256; i+=16) {
326 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
327 printk(KERN_WARNING "bttv: readee error\n");
328 break;
334 * Local variables:
335 * c-basic-offset: 8
336 * End: