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