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>
35 static struct i2c_algo_bit_data bttv_i2c_algo_template;
36 static struct i2c_adapter bttv_i2c_adap_template;
37 static struct i2c_client bttv_i2c_client_template;
39 EXPORT_SYMBOL(bttv_get_cardinfo);
40 EXPORT_SYMBOL(bttv_get_pcidev);
41 EXPORT_SYMBOL(bttv_get_id);
42 EXPORT_SYMBOL(bttv_gpio_enable);
43 EXPORT_SYMBOL(bttv_read_gpio);
44 EXPORT_SYMBOL(bttv_write_gpio);
45 EXPORT_SYMBOL(bttv_get_gpio_queue);
46 EXPORT_SYMBOL(bttv_i2c_call);
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, unsigned *cardid)
55 if (card >= bttv_num) {
58 *type = bttvs[card].type;
59 *cardid = bttvs[card].cardid;
63 struct pci_dev* bttv_get_pcidev(unsigned int card)
67 return bttvs[card].dev;
70 int bttv_get_id(unsigned int card)
72 printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
73 if (card >= bttv_num) {
76 return bttvs[card].type;
79 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
83 if (card >= bttv_num) {
88 btaor(data, ~mask, BT848_GPIO_OUT_EN);
90 bttv_gpio_tracking(btv,"extern enable");
94 int bttv_read_gpio(unsigned int card, unsigned long *data)
98 if (card >= bttv_num) {
108 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
109 because we set direct input on init */
110 *data = btread(BT848_GPIO_DATA);
114 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
118 if (card >= bttv_num) {
124 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
125 because direct input is set on init */
126 btaor(data & mask, ~mask, BT848_GPIO_DATA);
128 bttv_gpio_tracking(btv,"extern write");
132 wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
136 if (card >= bttv_num) {
141 if (bttvs[card].shutdown) {
148 /* ----------------------------------------------------------------------- */
151 void bttv_bit_setscl(void *data, int state)
153 struct bttv *btv = (struct bttv*)data;
156 btv->i2c_state |= 0x02;
158 btv->i2c_state &= ~0x02;
159 btwrite(btv->i2c_state, BT848_I2C);
163 void bttv_bit_setsda(void *data, int state)
165 struct bttv *btv = (struct bttv*)data;
168 btv->i2c_state |= 0x01;
170 btv->i2c_state &= ~0x01;
171 btwrite(btv->i2c_state, BT848_I2C);
175 static int bttv_bit_getscl(void *data)
177 struct bttv *btv = (struct bttv*)data;
180 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
184 static int bttv_bit_getsda(void *data)
186 struct bttv *btv = (struct bttv*)data;
189 state = btread(BT848_I2C) & 0x01;
194 static int attach_inform(struct i2c_client *client)
196 struct bttv *btv = i2c_get_adapdata(client->adapter);
198 if (btv->tuner_type != UNSET)
199 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
200 if (btv->pinnacle_id != UNSET)
201 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
205 printk("bttv%d: i2c attach [client=%s]\n",
206 btv->nr, i2c_clientname(client));
210 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
212 if (0 != btv->i2c_rc)
214 i2c_clients_command(&btv->i2c_adap, cmd, arg);
217 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
219 if (card >= bttv_num)
221 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
224 static struct i2c_algo_bit_data bttv_i2c_algo_template = {
225 .setsda = bttv_bit_setsda,
226 .setscl = bttv_bit_setscl,
227 .getsda = bttv_bit_getsda,
228 .getscl = bttv_bit_getscl,
234 static struct i2c_adapter bttv_i2c_adap_template = {
235 .owner = THIS_MODULE,
236 .class = I2C_ADAP_CLASS_TV_ANALOG,
237 I2C_DEVNAME("bt848"),
238 .id = I2C_HW_B_BT848,
239 .client_register = attach_inform,
242 static struct i2c_client bttv_i2c_client_template = {
243 I2C_DEVNAME("bttv internal"),
249 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
251 unsigned char buffer = 0;
253 if (0 != btv->i2c_rc)
255 if (bttv_verbose && NULL != probe_for)
256 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
257 btv->nr,probe_for,addr);
258 btv->i2c_client.addr = addr >> 1;
259 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
260 if (NULL != probe_for) {
262 printk("not found\n");
264 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
268 if (bttv_verbose && NULL != probe_for)
274 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
275 unsigned char b2, int both)
277 unsigned char buffer[2];
278 int bytes = both ? 2 : 1;
280 if (0 != btv->i2c_rc)
282 btv->i2c_client.addr = addr >> 1;
285 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
290 /* read EEPROM content */
291 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
295 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
296 printk(KERN_WARNING "bttv: readee error\n");
299 btv->i2c_client.addr = addr >> 1;
300 for (i=0; i<256; i+=16) {
301 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
302 printk(KERN_WARNING "bttv: readee error\n");
308 /* init + register i2c algo-bit adapter */
309 int __devinit init_bttv_i2c(struct bttv *btv)
311 memcpy(&btv->i2c_adap, &bttv_i2c_adap_template,
312 sizeof(struct i2c_adapter));
313 memcpy(&btv->i2c_algo, &bttv_i2c_algo_template,
314 sizeof(struct i2c_algo_bit_data));
315 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
316 sizeof(struct i2c_client));
318 sprintf(btv->i2c_adap.dev.name, "bt848 #%d", btv->nr);
319 btv->i2c_adap.dev.parent = &btv->dev->dev;
321 btv->i2c_algo.data = btv;
322 i2c_set_adapdata(&btv->i2c_adap, btv);
323 btv->i2c_adap.algo_data = &btv->i2c_algo;
324 btv->i2c_client.adapter = &btv->i2c_adap;
326 bttv_bit_setscl(btv,1);
327 bttv_bit_setsda(btv,1);
329 btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);