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