commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / bttv-if.c
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-2003 Gerd Knorr <kraxel@bytesex.org>
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 #include <linux/module.h>
29 #include <linux/init.h>
30
31 #include <asm/io.h>
32
33 #include "bttvp.h"
34
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;
38
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);
47
48 /* ----------------------------------------------------------------------- */
49 /* Exported functions - for other modules which want to access the         */
50 /*                      gpio ports (IR for example)                        */
51 /*                      see bttv.h for comments                            */
52
53 int bttv_get_cardinfo(unsigned int card, int *type, unsigned *cardid)
54 {
55         if (card >= bttv_num) {
56                 return -1;
57         }
58         *type   = bttvs[card].type;
59         *cardid = bttvs[card].cardid;
60         return 0;
61 }
62
63 struct pci_dev* bttv_get_pcidev(unsigned int card)
64 {
65         if (card >= bttv_num)
66                 return NULL;
67         return bttvs[card].dev;
68 }
69
70 int bttv_get_id(unsigned int card)
71 {
72         printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
73         if (card >= bttv_num) {
74                 return -1;
75         }
76         return bttvs[card].type;
77 }
78
79 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
80 {
81         struct bttv *btv;
82
83         if (card >= bttv_num) {
84                 return -EINVAL;
85         }
86         
87         btv = &bttvs[card];
88         btaor(data, ~mask, BT848_GPIO_OUT_EN);
89         if (bttv_gpio)
90                 bttv_gpio_tracking(btv,"extern enable");
91         return 0;
92 }
93
94 int bttv_read_gpio(unsigned int card, unsigned long *data)
95 {
96         struct bttv *btv;
97         
98         if (card >= bttv_num) {
99                 return -EINVAL;
100         }
101
102         btv = &bttvs[card];
103
104         if(btv->shutdown) {
105                 return -ENODEV;
106         }
107
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);
111         return 0;
112 }
113
114 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
115 {
116         struct bttv *btv;
117         
118         if (card >= bttv_num) {
119                 return -EINVAL;
120         }
121
122         btv = &bttvs[card];
123
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);
127         if (bttv_gpio)
128                 bttv_gpio_tracking(btv,"extern write");
129         return 0;
130 }
131
132 wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
133 {
134         struct bttv *btv;
135
136         if (card >= bttv_num) {
137                 return NULL;
138         }
139
140         btv = &bttvs[card];
141         if (bttvs[card].shutdown) {
142                 return NULL;
143         }
144         return &btv->gpioq;
145 }
146
147
148 /* ----------------------------------------------------------------------- */
149 /* I2C functions                                                           */
150
151 void bttv_bit_setscl(void *data, int state)
152 {
153         struct bttv *btv = (struct bttv*)data;
154
155         if (state)
156                 btv->i2c_state |= 0x02;
157         else
158                 btv->i2c_state &= ~0x02;
159         btwrite(btv->i2c_state, BT848_I2C);
160         btread(BT848_I2C);
161 }
162
163 void bttv_bit_setsda(void *data, int state)
164 {
165         struct bttv *btv = (struct bttv*)data;
166
167         if (state)
168                 btv->i2c_state |= 0x01;
169         else
170                 btv->i2c_state &= ~0x01;
171         btwrite(btv->i2c_state, BT848_I2C);
172         btread(BT848_I2C);
173 }
174
175 static int bttv_bit_getscl(void *data)
176 {
177         struct bttv *btv = (struct bttv*)data;
178         int state;
179         
180         state = btread(BT848_I2C) & 0x02 ? 1 : 0;
181         return state;
182 }
183
184 static int bttv_bit_getsda(void *data)
185 {
186         struct bttv *btv = (struct bttv*)data;
187         int state;
188
189         state = btread(BT848_I2C) & 0x01;
190         return state;
191 }
192
193
194 static int attach_inform(struct i2c_client *client)
195 {
196         struct bttv *btv = i2c_get_adapdata(client->adapter);
197
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,
202                                       &btv->pinnacle_id);
203
204         if (bttv_debug)
205                 printk("bttv%d: i2c attach [client=%s]\n",
206                        btv->nr, i2c_clientname(client));
207         return 0;
208 }
209
210 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
211 {
212         if (0 != btv->i2c_rc)
213                 return;
214         i2c_clients_command(&btv->i2c_adap, cmd, arg);
215 }
216
217 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
218 {
219         if (card >= bttv_num)
220                 return;
221         bttv_call_i2c_clients(&bttvs[card], cmd, arg);
222 }
223
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,
229         .udelay  = 16,
230         .mdelay  = 10,
231         .timeout = 200,
232 };
233
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,
240 };
241
242 static struct i2c_client bttv_i2c_client_template = {
243         I2C_DEVNAME("bttv internal"),
244         .id       = -1,
245 };
246
247
248 /* read I2C */
249 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) 
250 {
251         unsigned char buffer = 0;
252
253         if (0 != btv->i2c_rc)
254                 return -1;
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) {
261                         if (bttv_verbose)
262                                 printk("not found\n");
263                 } else
264                         printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
265                                btv->nr,addr);
266                 return -1;
267         }
268         if (bttv_verbose && NULL != probe_for)
269                 printk("found\n");
270         return buffer;
271 }
272
273 /* write I2C */
274 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
275                     unsigned char b2, int both)
276 {
277         unsigned char buffer[2];
278         int bytes = both ? 2 : 1;
279
280         if (0 != btv->i2c_rc)
281                 return -1;
282         btv->i2c_client.addr = addr >> 1;
283         buffer[0] = b1;
284         buffer[1] = b2;
285         if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
286                 return -1;
287         return 0;
288 }
289
290 /* read EEPROM content */
291 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
292 {
293         int i;
294         
295         if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
296                 printk(KERN_WARNING "bttv: readee error\n");
297                 return;
298         }
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");
303                         break;
304                 }
305         }
306 }
307
308 /* init + register i2c algo-bit adapter */
309 int __devinit init_bttv_i2c(struct bttv *btv)
310 {
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));
317
318         sprintf(btv->i2c_adap.dev.name, "bt848 #%d", btv->nr);
319         btv->i2c_adap.dev.parent = &btv->dev->dev;
320
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;
325
326         bttv_bit_setscl(btv,1);
327         bttv_bit_setsda(btv,1);
328
329         btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);
330         return btv->i2c_rc;
331 }
332
333 /*
334  * Local variables:
335  * c-basic-offset: 8
336  * End:
337  */