Merge branch 'master' into gfs2
[pandora-kernel.git] / drivers / media / video / bt8xx / bttv-i2c.c
1 /*
2
3     bttv-i2c.c  --  all the i2c code is here
4
5     bttv - Bt848 frame grabber driver
6
7     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
8                            & Marcus Metzler (mocm@thp.uni-koeln.de)
9     (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
10
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31
32 #include "bttvp.h"
33 #include <media/v4l2-common.h>
34 #include <linux/jiffies.h>
35 #include <asm/io.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;
45 static int i2c_hw;
46 static int i2c_scan;
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         .timeout = 200,
104 };
105
106 static struct i2c_adapter bttv_i2c_adap_sw_template = {
107         .owner             = THIS_MODULE,
108         .class             = I2C_CLASS_TV_ANALOG,
109         .name              = "bttv",
110         .id                = I2C_HW_B_BT848,
111         .client_register   = attach_inform,
112 };
113
114 /* ----------------------------------------------------------------------- */
115 /* I2C functions - hardware i2c                                            */
116
117 static int algo_control(struct i2c_adapter *adapter,
118                         unsigned int cmd, unsigned long arg)
119 {
120         return 0;
121 }
122
123 static u32 functionality(struct i2c_adapter *adap)
124 {
125         return I2C_FUNC_SMBUS_EMUL;
126 }
127
128 static int
129 bttv_i2c_wait_done(struct bttv *btv)
130 {
131         int rc = 0;
132
133         /* timeout */
134         if (wait_event_interruptible_timeout(btv->i2c_queue,
135                 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
136
137         rc = -EIO;
138
139         if (btv->i2c_done & BT848_INT_RACK)
140                 rc = 1;
141         btv->i2c_done = 0;
142         return rc;
143 }
144
145 #define I2C_HW (BT878_I2C_MODE  | BT848_I2C_SYNC |\
146                 BT848_I2C_SCL | BT848_I2C_SDA)
147
148 static int
149 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
150 {
151         u32 xmit;
152         int retval,cnt;
153
154         /* sanity checks */
155         if (0 == msg->len)
156                 return -EINVAL;
157
158         /* start, address + first byte */
159         xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
160         if (msg->len > 1 || !last)
161                 xmit |= BT878_I2C_NOSTOP;
162         btwrite(xmit, BT848_I2C);
163         retval = bttv_i2c_wait_done(btv);
164         if (retval < 0)
165                 goto err;
166         if (retval == 0)
167                 goto eio;
168         if (i2c_debug) {
169                 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
170                 if (!(xmit & BT878_I2C_NOSTOP))
171                         printk(" >\n");
172         }
173
174         for (cnt = 1; cnt < msg->len; cnt++ ) {
175                 /* following bytes */
176                 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
177                 if (cnt < msg->len-1 || !last)
178                         xmit |= BT878_I2C_NOSTOP;
179                 btwrite(xmit, BT848_I2C);
180                 retval = bttv_i2c_wait_done(btv);
181                 if (retval < 0)
182                         goto err;
183                 if (retval == 0)
184                         goto eio;
185                 if (i2c_debug) {
186                         printk(" %02x", msg->buf[cnt]);
187                         if (!(xmit & BT878_I2C_NOSTOP))
188                                 printk(" >\n");
189                 }
190         }
191         return msg->len;
192
193  eio:
194         retval = -EIO;
195  err:
196         if (i2c_debug)
197                 printk(" ERR: %d\n",retval);
198         return retval;
199 }
200
201 static int
202 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
203 {
204         u32 xmit;
205         u32 cnt;
206         int retval;
207
208         for(cnt = 0; cnt < msg->len; cnt++) {
209                 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
210                 if (cnt < msg->len-1)
211                         xmit |= BT848_I2C_W3B;
212                 if (cnt < msg->len-1 || !last)
213                         xmit |= BT878_I2C_NOSTOP;
214                 if (cnt)
215                         xmit |= BT878_I2C_NOSTART;
216                 btwrite(xmit, BT848_I2C);
217                 retval = bttv_i2c_wait_done(btv);
218                 if (retval < 0)
219                         goto err;
220                 if (retval == 0)
221                         goto eio;
222                 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
223                 if (i2c_debug) {
224                         if (!(xmit & BT878_I2C_NOSTART))
225                                 printk(" <R %02x", (msg->addr << 1) +1);
226                         printk(" =%02x", msg->buf[cnt]);
227                         if (!(xmit & BT878_I2C_NOSTOP))
228                                 printk(" >\n");
229                 }
230         }
231         return msg->len;
232
233  eio:
234         retval = -EIO;
235  err:
236         if (i2c_debug)
237                 printk(" ERR: %d\n",retval);
238         return retval;
239 }
240
241 static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
242 {
243         struct bttv *btv = i2c_get_adapdata(i2c_adap);
244         int retval = 0;
245         int i;
246
247         if (i2c_debug)
248                 printk("bt-i2c:");
249         btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
250         for (i = 0 ; i < num; i++) {
251                 if (msgs[i].flags & I2C_M_RD) {
252                         /* read */
253                         retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
254                         if (retval < 0)
255                                 goto err;
256                 } else {
257                         /* write */
258                         retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
259                         if (retval < 0)
260                                 goto err;
261                 }
262         }
263         return num;
264
265  err:
266         return retval;
267 }
268
269 static struct i2c_algorithm bttv_algo = {
270         .master_xfer   = bttv_i2c_xfer,
271         .algo_control  = algo_control,
272         .functionality = functionality,
273 };
274
275 static struct i2c_adapter bttv_i2c_adap_hw_template = {
276         .owner             = THIS_MODULE,
277         .class         = I2C_CLASS_TV_ANALOG,
278         .name          = "bt878",
279         .id            = I2C_HW_B_BT848 /* FIXME */,
280         .algo          = &bttv_algo,
281         .client_register = attach_inform,
282 };
283
284 /* ----------------------------------------------------------------------- */
285 /* I2C functions - common stuff                                            */
286
287 static int attach_inform(struct i2c_client *client)
288 {
289         struct bttv *btv = i2c_get_adapdata(client->adapter);
290         int addr=ADDR_UNSET;
291
292
293         if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
294                 addr = bttv_tvcards[btv->c.type].tuner_addr;
295
296
297         if (bttv_debug)
298                 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
299                         btv->c.nr, client->driver->driver.name, client->addr,
300                         client->name);
301         if (!client->driver->command)
302                 return 0;
303
304         if (client->driver->id == I2C_DRIVERID_MSP3400)
305                 btv->i2c_msp34xx_client = client;
306         if (client->driver->id == I2C_DRIVERID_TVAUDIO)
307                 btv->i2c_tvaudio_client = client;
308         if (btv->tuner_type != UNSET) {
309                 struct tuner_setup tun_setup;
310
311                 if ((addr==ADDR_UNSET) ||
312                                 (addr==client->addr)) {
313
314                         tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
315                         tun_setup.type = btv->tuner_type;
316                         tun_setup.addr = addr;
317                         bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
318                 }
319
320         }
321
322         return 0;
323 }
324
325 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
326 {
327         if (0 != btv->i2c_rc)
328                 return;
329         i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
330 }
331
332 static struct i2c_client bttv_i2c_client_template = {
333         .name   = "bttv internal",
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         memset(eedata, 0, 256);
383         if (0 != btv->i2c_rc)
384                 return;
385         btv->i2c_client.addr = addr >> 1;
386         tveeprom_read(&btv->i2c_client, eedata, 256);
387 }
388
389 static char *i2c_devs[128] = {
390         [ 0x1c >> 1 ] = "lgdt330x",
391         [ 0x30 >> 1 ] = "IR (hauppauge)",
392         [ 0x80 >> 1 ] = "msp34xx",
393         [ 0x86 >> 1 ] = "tda9887",
394         [ 0xa0 >> 1 ] = "eeprom",
395         [ 0xc0 >> 1 ] = "tuner (analog)",
396         [ 0xc2 >> 1 ] = "tuner (analog)",
397 };
398
399 static void do_i2c_scan(char *name, struct i2c_client *c)
400 {
401         unsigned char buf;
402         int i,rc;
403
404         for (i = 0; i < 128; i++) {
405                 c->addr = i;
406                 rc = i2c_master_recv(c,&buf,0);
407                 if (rc < 0)
408                         continue;
409                 printk("%s: i2c scan: found device @ 0x%x  [%s]\n",
410                        name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
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 (bttv_tvcards[btv->c.type].no_video)
445                 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
446         if (bttv_tvcards[btv->c.type].has_dvb)
447                 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
448
449         if (btv->use_i2c_hw) {
450                 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
451         } else {
452                 bttv_bit_setscl(btv,1);
453                 bttv_bit_setsda(btv,1);
454                 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
455         }
456         if (0 == btv->i2c_rc && i2c_scan)
457                 do_i2c_scan(btv->c.name,&btv->i2c_client);
458         return btv->i2c_rc;
459 }
460
461 int __devexit fini_bttv_i2c(struct bttv *btv)
462 {
463         if (0 != btv->i2c_rc)
464                 return 0;
465
466         if (btv->use_i2c_hw) {
467                 return i2c_del_adapter(&btv->c.i2c_adap);
468         } else {
469                 return i2c_bit_del_bus(&btv->c.i2c_adap);
470         }
471 }
472
473 /*
474  * Local variables:
475  * c-basic-offset: 8
476  * End:
477  */