vserver 1.9.3
[linux-2.6.git] / drivers / media / video / bttv-i2c.c
1 /*
2     bttv-i2c.c  --  all the i2c code is here
3
4     bttv - Bt848 frame grabber driver
5
6     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
7                            & Marcus Metzler (mocm@thp.uni-koeln.de)
8     (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
9
10     This program is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with this program; if not, write to the Free Software
22     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23     
24 */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <asm/io.h>
30
31 #include "bttvp.h"
32
33 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
34 static struct i2c_adapter bttv_i2c_adap_sw_template;
35 static struct i2c_adapter bttv_i2c_adap_hw_template;
36 static struct i2c_client bttv_i2c_client_template;
37
38 #ifndef I2C_PEC
39 static void bttv_inc_use(struct i2c_adapter *adap);
40 static void bttv_dec_use(struct i2c_adapter *adap);
41 #endif
42 static int attach_inform(struct i2c_client *client);
43 static int detach_inform(struct i2c_client *client);
44
45 static int i2c_debug = 0;
46 static int i2c_hw = 0;
47 MODULE_PARM(i2c_debug,"i");
48 MODULE_PARM(i2c_hw,"i");
49
50 /* ----------------------------------------------------------------------- */
51 /* I2C functions - bitbanging adapter (software i2c)                       */
52
53 void bttv_bit_setscl(void *data, int state)
54 {
55         struct bttv *btv = (struct bttv*)data;
56
57         if (state)
58                 btv->i2c_state |= 0x02;
59         else
60                 btv->i2c_state &= ~0x02;
61         btwrite(btv->i2c_state, BT848_I2C);
62         btread(BT848_I2C);
63 }
64
65 void bttv_bit_setsda(void *data, int state)
66 {
67         struct bttv *btv = (struct bttv*)data;
68
69         if (state)
70                 btv->i2c_state |= 0x01;
71         else
72                 btv->i2c_state &= ~0x01;
73         btwrite(btv->i2c_state, BT848_I2C);
74         btread(BT848_I2C);
75 }
76
77 static int bttv_bit_getscl(void *data)
78 {
79         struct bttv *btv = (struct bttv*)data;
80         int state;
81         
82         state = btread(BT848_I2C) & 0x02 ? 1 : 0;
83         return state;
84 }
85
86 static int bttv_bit_getsda(void *data)
87 {
88         struct bttv *btv = (struct bttv*)data;
89         int state;
90
91         state = btread(BT848_I2C) & 0x01;
92         return state;
93 }
94
95 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
96         .setsda  = bttv_bit_setsda,
97         .setscl  = bttv_bit_setscl,
98         .getsda  = bttv_bit_getsda,
99         .getscl  = bttv_bit_getscl,
100         .udelay  = 16,
101         .mdelay  = 10,
102         .timeout = 200,
103 };
104
105 static struct i2c_adapter bttv_i2c_adap_sw_template = {
106 #ifdef I2C_PEC
107         .owner             = THIS_MODULE,
108 #else
109         .inc_use           = bttv_inc_use,
110         .dec_use           = bttv_dec_use,
111 #endif
112 #ifdef I2C_CLASS_TV_ANALOG
113         .class             = I2C_CLASS_TV_ANALOG,
114 #endif
115         I2C_DEVNAME("bt848"),
116         .id                = I2C_HW_B_BT848,
117         .client_register   = attach_inform,
118         .client_unregister = detach_inform,
119 };
120
121 /* ----------------------------------------------------------------------- */
122 /* I2C functions - hardware i2c                                            */
123
124 static int algo_control(struct i2c_adapter *adapter, 
125                         unsigned int cmd, unsigned long arg)
126 {
127         return 0;
128 }
129
130 static u32 functionality(struct i2c_adapter *adap)
131 {
132         return I2C_FUNC_SMBUS_EMUL;
133 }
134
135 static int
136 bttv_i2c_wait_done(struct bttv *btv)
137 {
138         DECLARE_WAITQUEUE(wait, current);
139         int rc = 0;
140         
141         add_wait_queue(&btv->i2c_queue, &wait);
142         set_current_state(TASK_INTERRUPTIBLE);
143         if (0 == btv->i2c_done)
144                 schedule_timeout(HZ/50+1);
145         set_current_state(TASK_RUNNING);
146         remove_wait_queue(&btv->i2c_queue, &wait);
147
148         if (0 == btv->i2c_done)
149                 /* timeout */
150                 rc = -EIO;
151         if (btv->i2c_done & BT848_INT_RACK)
152                 rc = 1;
153         btv->i2c_done = 0;
154         return rc;
155 }
156
157 #define I2C_HW (BT878_I2C_MODE  | BT848_I2C_SYNC |\
158                 BT848_I2C_SCL | BT848_I2C_SDA)
159
160 static int
161 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
162 {
163         u32 xmit;
164         int retval,cnt;
165
166         /* sanity checks */
167         if (0 == msg->len)
168                 return -EINVAL;
169
170         /* start, address + first byte */
171         xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
172         if (msg->len > 1 || !last)
173                 xmit |= BT878_I2C_NOSTOP;
174         btwrite(xmit, BT848_I2C);
175         retval = bttv_i2c_wait_done(btv);
176         if (retval < 0)
177                 goto err;
178         if (retval == 0)
179                 goto eio;
180         if (i2c_debug) {
181                 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
182                 if (!(xmit & BT878_I2C_NOSTOP))
183                         printk(" >\n");
184         }
185
186         for (cnt = 1; cnt < msg->len; cnt++ ) {
187                 /* following bytes */
188                 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
189                 if (cnt < msg->len-1 || !last)
190                         xmit |= BT878_I2C_NOSTOP;
191                 btwrite(xmit, BT848_I2C);
192                 retval = bttv_i2c_wait_done(btv);
193                 if (retval < 0)
194                         goto err;
195                 if (retval == 0)
196                         goto eio;
197                 if (i2c_debug) {
198                         printk(" %02x", msg->buf[cnt]);
199                         if (!(xmit & BT878_I2C_NOSTOP))
200                                 printk(" >\n");
201                 }
202         }
203         return msg->len;
204
205  eio:
206         retval = -EIO;
207  err:
208         if (i2c_debug)
209                 printk(" ERR: %d\n",retval);
210         return retval;
211 }
212
213 static int
214 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
215 {
216         u32 xmit;
217         u32 cnt;
218         int retval;
219
220         for(cnt = 0; cnt < msg->len; cnt++) {
221                 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
222                 if (cnt < msg->len-1)
223                         xmit |= BT848_I2C_W3B;
224                 if (cnt < msg->len-1 || !last)
225                         xmit |= BT878_I2C_NOSTOP;
226                 if (cnt)
227                         xmit |= BT878_I2C_NOSTART;
228                 btwrite(xmit, BT848_I2C);
229                 retval = bttv_i2c_wait_done(btv);
230                 if (retval < 0)
231                         goto err;
232                 if (retval == 0)
233                         goto eio;
234                 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
235                 if (i2c_debug) {
236                         if (!(xmit & BT878_I2C_NOSTART))
237                                 printk(" <R %02x", (msg->addr << 1) +1);
238                         printk(" =%02x", msg->buf[cnt]);
239                         if (!(xmit & BT878_I2C_NOSTOP))
240                                 printk(" >\n");
241                 }
242         }
243         return msg->len;
244
245  eio:
246         retval = -EIO;
247  err:
248         if (i2c_debug)
249                 printk(" ERR: %d\n",retval);
250         return retval;
251 }
252
253 int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
254 {
255         struct bttv *btv = i2c_get_adapdata(i2c_adap);
256         int retval = 0;
257         int i;
258
259         if (i2c_debug)
260                 printk("bt-i2c:");
261         btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
262         for (i = 0 ; i < num; i++) {
263                 if (msgs[i].flags & I2C_M_RD) {
264                         /* read */
265                         retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
266                         if (retval < 0)
267                                 goto err;
268                 } else {
269                         /* write */
270                         retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
271                         if (retval < 0)
272                                 goto err;
273                 }
274         }
275         return num;
276
277  err:
278         return retval;
279 }
280
281 static struct i2c_algorithm bttv_algo = {
282         .name          = "bt878",
283         .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
284         .master_xfer   = bttv_i2c_xfer,
285         .algo_control  = algo_control,
286         .functionality = functionality,
287 };
288
289 static struct i2c_adapter bttv_i2c_adap_hw_template = {
290 #ifdef I2C_PEC
291         .owner         = THIS_MODULE,
292 #else
293         .inc_use       = bttv_inc_use,
294         .dec_use       = bttv_dec_use,
295 #endif
296 #ifdef I2C_CLASS_TV_ANALOG
297         .class         = I2C_CLASS_TV_ANALOG,
298 #endif
299         I2C_DEVNAME("bt878"),
300         .id            = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
301         .algo          = &bttv_algo,
302         .client_register = attach_inform,
303         .client_unregister = detach_inform,
304 };
305
306 /* ----------------------------------------------------------------------- */
307 /* I2C functions - common stuff                                            */
308
309 #ifndef I2C_PEC
310 static void bttv_inc_use(struct i2c_adapter *adap)
311 {
312         MOD_INC_USE_COUNT;
313 }
314
315 static void bttv_dec_use(struct i2c_adapter *adap)
316 {
317         MOD_DEC_USE_COUNT;
318 }
319 #endif
320
321 static int attach_inform(struct i2c_client *client)
322 {
323         struct bttv *btv = i2c_get_adapdata(client->adapter);
324
325         if (btv->tuner_type != UNSET)
326                 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
327         if (btv->pinnacle_id != UNSET)
328                 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
329                                       &btv->pinnacle_id);
330         bttv_i2c_info(&btv->c, client, 1);
331
332         if (bttv_debug)
333                 printk("bttv%d: i2c attach [client=%s]\n",
334                        btv->c.nr, i2c_clientname(client));
335         return 0;
336 }
337
338 static int detach_inform(struct i2c_client *client)
339 {
340         struct bttv *btv = i2c_get_adapdata(client->adapter);
341
342         bttv_i2c_info(&btv->c, client, 0);
343         return 0;
344 }
345
346 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
347 {
348         if (0 != btv->i2c_rc)
349                 return;
350         i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
351 }
352
353 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
354 {
355         if (card >= bttv_num)
356                 return;
357         bttv_call_i2c_clients(&bttvs[card], cmd, arg);
358 }
359
360 static struct i2c_client bttv_i2c_client_template = {
361         I2C_DEVNAME("bttv internal"),
362         .id       = -1,
363 };
364
365
366 /* read I2C */
367 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) 
368 {
369         unsigned char buffer = 0;
370
371         if (0 != btv->i2c_rc)
372                 return -1;
373         if (bttv_verbose && NULL != probe_for)
374                 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
375                        btv->c.nr,probe_for,addr);
376         btv->i2c_client.addr = addr >> 1;
377         if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
378                 if (NULL != probe_for) {
379                         if (bttv_verbose)
380                                 printk("not found\n");
381                 } else
382                         printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
383                                btv->c.nr,addr);
384                 return -1;
385         }
386         if (bttv_verbose && NULL != probe_for)
387                 printk("found\n");
388         return buffer;
389 }
390
391 /* write I2C */
392 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
393                     unsigned char b2, int both)
394 {
395         unsigned char buffer[2];
396         int bytes = both ? 2 : 1;
397
398         if (0 != btv->i2c_rc)
399                 return -1;
400         btv->i2c_client.addr = addr >> 1;
401         buffer[0] = b1;
402         buffer[1] = b2;
403         if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
404                 return -1;
405         return 0;
406 }
407
408 /* read EEPROM content */
409 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
410 {
411         int i;
412         
413         if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
414                 printk(KERN_WARNING "bttv: readee error\n");
415                 return;
416         }
417         btv->i2c_client.addr = addr >> 1;
418         for (i=0; i<256; i+=16) {
419                 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
420                         printk(KERN_WARNING "bttv: readee error\n");
421                         break;
422                 }
423         }
424 }
425
426 /* init + register i2c algo-bit adapter */
427 int __devinit init_bttv_i2c(struct bttv *btv)
428 {
429         memcpy(&btv->i2c_client, &bttv_i2c_client_template,
430                sizeof(bttv_i2c_client_template));
431
432         if (i2c_hw)
433                 btv->use_i2c_hw = 1;
434         if (btv->use_i2c_hw) {
435                 /* bt878 */
436                 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
437                        sizeof(bttv_i2c_adap_hw_template));
438         } else {
439                 /* bt848 */
440                 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
441                        sizeof(bttv_i2c_adap_sw_template));
442                 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
443                        sizeof(bttv_i2c_algo_bit_template));
444                 btv->i2c_algo.data = btv;
445                 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
446         }
447
448         btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
449         snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
450                  "bt%d #%d [%s]", btv->id, btv->c.nr,
451                  btv->use_i2c_hw ? "hw" : "sw");
452
453         i2c_set_adapdata(&btv->c.i2c_adap, btv);
454         btv->i2c_client.adapter = &btv->c.i2c_adap;
455
456         if (btv->use_i2c_hw) {
457                 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
458         } else {
459                 bttv_bit_setscl(btv,1);
460                 bttv_bit_setsda(btv,1);
461                 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
462         }
463         return btv->i2c_rc;
464 }
465
466 int __devexit fini_bttv_i2c(struct bttv *btv)
467 {
468         if (0 != btv->i2c_rc)
469                 return 0;
470
471         if (btv->use_i2c_hw) {
472                 return i2c_del_adapter(&btv->c.i2c_adap);
473         } else {
474                 return i2c_bit_del_bus(&btv->c.i2c_adap);
475         }
476 }
477
478 /*
479  * Local variables:
480  * c-basic-offset: 8
481  * End:
482  */