Input: HIDDEV - make HIDIOCSREPORT wait IO completion
[cascardo/linux.git] / drivers / media / video / bttv-i2c.c
1 /*
2     $Id: bttv-i2c.c,v 1.25 2005/07/05 17:37:35 nsh Exp $
3
4     bttv-i2c.c  --  all the i2c code is here
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/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/jiffies.h>
33 #include <asm/io.h>
34
35 #include "bttvp.h"
36
37 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
38 static struct i2c_adapter bttv_i2c_adap_sw_template;
39 static struct i2c_adapter bttv_i2c_adap_hw_template;
40 static struct i2c_client bttv_i2c_client_template;
41
42 static int attach_inform(struct i2c_client *client);
43
44 static int i2c_debug = 0;
45 static int i2c_hw = 0;
46 static int i2c_scan = 0;
47 module_param(i2c_debug, int, 0644);
48 module_param(i2c_hw,    int, 0444);
49 module_param(i2c_scan,  int, 0444);
50 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
51
52 /* ----------------------------------------------------------------------- */
53 /* I2C functions - bitbanging adapter (software i2c)                       */
54
55 static void bttv_bit_setscl(void *data, int state)
56 {
57         struct bttv *btv = (struct bttv*)data;
58
59         if (state)
60                 btv->i2c_state |= 0x02;
61         else
62                 btv->i2c_state &= ~0x02;
63         btwrite(btv->i2c_state, BT848_I2C);
64         btread(BT848_I2C);
65 }
66
67 static void bttv_bit_setsda(void *data, int state)
68 {
69         struct bttv *btv = (struct bttv*)data;
70
71         if (state)
72                 btv->i2c_state |= 0x01;
73         else
74                 btv->i2c_state &= ~0x01;
75         btwrite(btv->i2c_state, BT848_I2C);
76         btread(BT848_I2C);
77 }
78
79 static int bttv_bit_getscl(void *data)
80 {
81         struct bttv *btv = (struct bttv*)data;
82         int state;
83
84         state = btread(BT848_I2C) & 0x02 ? 1 : 0;
85         return state;
86 }
87
88 static int bttv_bit_getsda(void *data)
89 {
90         struct bttv *btv = (struct bttv*)data;
91         int state;
92
93         state = btread(BT848_I2C) & 0x01;
94         return state;
95 }
96
97 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
98         .setsda  = bttv_bit_setsda,
99         .setscl  = bttv_bit_setscl,
100         .getsda  = bttv_bit_getsda,
101         .getscl  = bttv_bit_getscl,
102         .udelay  = 16,
103         .mdelay  = 10,
104         .timeout = 200,
105 };
106
107 static struct i2c_adapter bttv_i2c_adap_sw_template = {
108         .owner             = THIS_MODULE,
109 #ifdef I2C_CLASS_TV_ANALOG
110         .class             = I2C_CLASS_TV_ANALOG,
111 #endif
112         I2C_DEVNAME("bt848"),
113         .id                = I2C_HW_B_BT848,
114         .client_register   = attach_inform,
115 };
116
117 /* ----------------------------------------------------------------------- */
118 /* I2C functions - hardware i2c                                            */
119
120 static int algo_control(struct i2c_adapter *adapter,
121                         unsigned int cmd, unsigned long arg)
122 {
123         return 0;
124 }
125
126 static u32 functionality(struct i2c_adapter *adap)
127 {
128         return I2C_FUNC_SMBUS_EMUL;
129 }
130
131 static int
132 bttv_i2c_wait_done(struct bttv *btv)
133 {
134         int rc = 0;
135
136         /* timeout */
137         if (wait_event_interruptible_timeout(btv->i2c_queue,
138                 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
139
140         rc = -EIO;
141
142         if (btv->i2c_done & BT848_INT_RACK)
143                 rc = 1;
144         btv->i2c_done = 0;
145         return rc;
146 }
147
148 #define I2C_HW (BT878_I2C_MODE  | BT848_I2C_SYNC |\
149                 BT848_I2C_SCL | BT848_I2C_SDA)
150
151 static int
152 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
153 {
154         u32 xmit;
155         int retval,cnt;
156
157         /* sanity checks */
158         if (0 == msg->len)
159                 return -EINVAL;
160
161         /* start, address + first byte */
162         xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
163         if (msg->len > 1 || !last)
164                 xmit |= BT878_I2C_NOSTOP;
165         btwrite(xmit, BT848_I2C);
166         retval = bttv_i2c_wait_done(btv);
167         if (retval < 0)
168                 goto err;
169         if (retval == 0)
170                 goto eio;
171         if (i2c_debug) {
172                 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
173                 if (!(xmit & BT878_I2C_NOSTOP))
174                         printk(" >\n");
175         }
176
177         for (cnt = 1; cnt < msg->len; cnt++ ) {
178                 /* following bytes */
179                 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
180                 if (cnt < msg->len-1 || !last)
181                         xmit |= BT878_I2C_NOSTOP;
182                 btwrite(xmit, BT848_I2C);
183                 retval = bttv_i2c_wait_done(btv);
184                 if (retval < 0)
185                         goto err;
186                 if (retval == 0)
187                         goto eio;
188                 if (i2c_debug) {
189                         printk(" %02x", msg->buf[cnt]);
190                         if (!(xmit & BT878_I2C_NOSTOP))
191                                 printk(" >\n");
192                 }
193         }
194         return msg->len;
195
196  eio:
197         retval = -EIO;
198  err:
199         if (i2c_debug)
200                 printk(" ERR: %d\n",retval);
201         return retval;
202 }
203
204 static int
205 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
206 {
207         u32 xmit;
208         u32 cnt;
209         int retval;
210
211         for(cnt = 0; cnt < msg->len; cnt++) {
212                 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
213                 if (cnt < msg->len-1)
214                         xmit |= BT848_I2C_W3B;
215                 if (cnt < msg->len-1 || !last)
216                         xmit |= BT878_I2C_NOSTOP;
217                 if (cnt)
218                         xmit |= BT878_I2C_NOSTART;
219                 btwrite(xmit, BT848_I2C);
220                 retval = bttv_i2c_wait_done(btv);
221                 if (retval < 0)
222                         goto err;
223                 if (retval == 0)
224                         goto eio;
225                 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
226                 if (i2c_debug) {
227                         if (!(xmit & BT878_I2C_NOSTART))
228                                 printk(" <R %02x", (msg->addr << 1) +1);
229                         printk(" =%02x", msg->buf[cnt]);
230                         if (!(xmit & BT878_I2C_NOSTOP))
231                                 printk(" >\n");
232                 }
233         }
234         return msg->len;
235
236  eio:
237         retval = -EIO;
238  err:
239         if (i2c_debug)
240                 printk(" ERR: %d\n",retval);
241         return retval;
242 }
243
244 static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
245 {
246         struct bttv *btv = i2c_get_adapdata(i2c_adap);
247         int retval = 0;
248         int i;
249
250         if (i2c_debug)
251                 printk("bt-i2c:");
252         btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
253         for (i = 0 ; i < num; i++) {
254                 if (msgs[i].flags & I2C_M_RD) {
255                         /* read */
256                         retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
257                         if (retval < 0)
258                                 goto err;
259                 } else {
260                         /* write */
261                         retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
262                         if (retval < 0)
263                                 goto err;
264                 }
265         }
266         return num;
267
268  err:
269         return retval;
270 }
271
272 static struct i2c_algorithm bttv_algo = {
273         .name          = "bt878",
274         .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
275         .master_xfer   = bttv_i2c_xfer,
276         .algo_control  = algo_control,
277         .functionality = functionality,
278 };
279
280 static struct i2c_adapter bttv_i2c_adap_hw_template = {
281         .owner         = THIS_MODULE,
282 #ifdef I2C_CLASS_TV_ANALOG
283         .class         = I2C_CLASS_TV_ANALOG,
284 #endif
285         I2C_DEVNAME("bt878"),
286         .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
287         .algo          = &bttv_algo,
288         .client_register = attach_inform,
289 };
290
291 /* ----------------------------------------------------------------------- */
292 /* I2C functions - common stuff                                            */
293
294 static int attach_inform(struct i2c_client *client)
295 {
296         struct bttv *btv = i2c_get_adapdata(client->adapter);
297
298         if (bttv_debug)
299                 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
300                         btv->c.nr,client->driver->name,client->addr,
301                         i2c_clientname(client));
302         if (!client->driver->command)
303                 return 0;
304
305         if (btv->tuner_type != UNSET) {
306                 struct tuner_setup tun_setup;
307
308                 tun_setup.mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
309                 tun_setup.type = btv->tuner_type;
310                 tun_setup.addr = ADDR_UNSET;
311
312                 client->driver->command (client, TUNER_SET_TYPE_ADDR, &tun_setup);
313         }
314
315         if (btv->pinnacle_id != UNSET)
316                 client->driver->command(client,AUDC_CONFIG_PINNACLE,
317                                       &btv->pinnacle_id);
318         return 0;
319 }
320
321 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
322 {
323         if (0 != btv->i2c_rc)
324                 return;
325         i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
326 }
327
328 static struct i2c_client bttv_i2c_client_template = {
329         I2C_DEVNAME("bttv internal"),
330 };
331
332
333 /* read I2C */
334 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
335 {
336         unsigned char buffer = 0;
337
338         if (0 != btv->i2c_rc)
339                 return -1;
340         if (bttv_verbose && NULL != probe_for)
341                 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
342                        btv->c.nr,probe_for,addr);
343         btv->i2c_client.addr = addr >> 1;
344         if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
345                 if (NULL != probe_for) {
346                         if (bttv_verbose)
347                                 printk("not found\n");
348                 } else
349                         printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
350                                btv->c.nr,addr);
351                 return -1;
352         }
353         if (bttv_verbose && NULL != probe_for)
354                 printk("found\n");
355         return buffer;
356 }
357
358 /* write I2C */
359 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
360                     unsigned char b2, int both)
361 {
362         unsigned char buffer[2];
363         int bytes = both ? 2 : 1;
364
365         if (0 != btv->i2c_rc)
366                 return -1;
367         btv->i2c_client.addr = addr >> 1;
368         buffer[0] = b1;
369         buffer[1] = b2;
370         if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
371                 return -1;
372         return 0;
373 }
374
375 /* read EEPROM content */
376 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
377 {
378         memset(eedata, 0, 256);
379         if (0 != btv->i2c_rc)
380                 return;
381         btv->i2c_client.addr = addr >> 1;
382         tveeprom_read(&btv->i2c_client, eedata, 256);
383 }
384
385 static char *i2c_devs[128] = {
386         [ 0x30 >> 1 ] = "IR (hauppauge)",
387         [ 0x80 >> 1 ] = "msp34xx",
388         [ 0x86 >> 1 ] = "tda9887",
389         [ 0xa0 >> 1 ] = "eeprom",
390         [ 0xc0 >> 1 ] = "tuner (analog)",
391         [ 0xc2 >> 1 ] = "tuner (analog)",
392 };
393
394 static void do_i2c_scan(char *name, struct i2c_client *c)
395 {
396         unsigned char buf;
397         int i,rc;
398
399         for (i = 0; i < 128; i++) {
400                 c->addr = i;
401                 rc = i2c_master_recv(c,&buf,0);
402                 if (rc < 0)
403                         continue;
404                 printk("%s: i2c scan: found device @ 0x%x  [%s]\n",
405                        name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
406         }
407 }
408
409 /* init + register i2c algo-bit adapter */
410 int __devinit init_bttv_i2c(struct bttv *btv)
411 {
412         memcpy(&btv->i2c_client, &bttv_i2c_client_template,
413                sizeof(bttv_i2c_client_template));
414
415         if (i2c_hw)
416                 btv->use_i2c_hw = 1;
417         if (btv->use_i2c_hw) {
418                 /* bt878 */
419                 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
420                        sizeof(bttv_i2c_adap_hw_template));
421         } else {
422                 /* bt848 */
423                 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
424                        sizeof(bttv_i2c_adap_sw_template));
425                 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
426                        sizeof(bttv_i2c_algo_bit_template));
427                 btv->i2c_algo.data = btv;
428                 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
429         }
430
431         btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
432         snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
433                  "bt%d #%d [%s]", btv->id, btv->c.nr,
434                  btv->use_i2c_hw ? "hw" : "sw");
435
436         i2c_set_adapdata(&btv->c.i2c_adap, btv);
437         btv->i2c_client.adapter = &btv->c.i2c_adap;
438
439 #ifdef I2C_CLASS_TV_ANALOG
440         if (bttv_tvcards[btv->c.type].no_video)
441                 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
442         if (bttv_tvcards[btv->c.type].has_dvb)
443                 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
444 #endif
445
446         if (btv->use_i2c_hw) {
447                 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
448         } else {
449                 bttv_bit_setscl(btv,1);
450                 bttv_bit_setsda(btv,1);
451                 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
452         }
453         if (0 == btv->i2c_rc && i2c_scan)
454                 do_i2c_scan(btv->c.name,&btv->i2c_client);
455         return btv->i2c_rc;
456 }
457
458 int __devexit fini_bttv_i2c(struct bttv *btv)
459 {
460         if (0 != btv->i2c_rc)
461                 return 0;
462
463         if (btv->use_i2c_hw) {
464                 return i2c_del_adapter(&btv->c.i2c_adap);
465         } else {
466                 return i2c_bit_del_bus(&btv->c.i2c_adap);
467         }
468 }
469
470 /*
471  * Local variables:
472  * c-basic-offset: 8
473  * End:
474  */