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