Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[pandora-kernel.git] / drivers / media / video / usbvision / usbvision-i2c.c
1 /*
2  * I2C_ALGO_USB.C
3  *  i2c algorithm for USB-I2C Bridges
4  *
5  * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6  *                         Dwaine Garden <dwainegarden@rogers.com>
7  *
8  * This module is part of usbvision driver project.
9  * Updates to driver completed by Dwaine P. Garden
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/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/version.h>
32 #include <linux/utsname.h>
33 #include <linux/init.h>
34 #include <asm/uaccess.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
38 #include <linux/usb.h>
39 #include <linux/i2c.h>
40 #include "usbvision.h"
41
42 #define DBG_I2C         1<<0
43 #define DBG_ALGO        1<<1
44
45 static int i2c_debug = 0;
46
47 module_param (i2c_debug, int, 0644);                    // debug_i2c_usb mode of the device driver
48 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
49
50 #define PDEBUG(level, fmt, args...) \
51                 if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
52
53 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
54                             short len);
55 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
56                            short len);
57
58 static inline int try_write_address(struct i2c_adapter *i2c_adap,
59                                     unsigned char addr, int retries)
60 {
61         void *data;
62         int i, ret = -1;
63         char buf[4];
64
65         data = i2c_get_adapdata(i2c_adap);
66         buf[0] = 0x00;
67         for (i = 0; i <= retries; i++) {
68                 ret = (usbvision_i2c_write(data, addr, buf, 1));
69                 if (ret == 1)
70                         break;  /* success! */
71                 udelay(5);
72                 if (i == retries)       /* no success */
73                         break;
74                 udelay(10);
75         }
76         if (i) {
77                 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
78                 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
79         }
80         return ret;
81 }
82
83 static inline int try_read_address(struct i2c_adapter *i2c_adap,
84                                    unsigned char addr, int retries)
85 {
86         void *data;
87         int i, ret = -1;
88         char buf[4];
89
90         data = i2c_get_adapdata(i2c_adap);
91         for (i = 0; i <= retries; i++) {
92                 ret = (usbvision_i2c_read(data, addr, buf, 1));
93                 if (ret == 1)
94                         break;  /* success! */
95                 udelay(5);
96                 if (i == retries)       /* no success */
97                         break;
98                 udelay(10);
99         }
100         if (i) {
101                 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
102                 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
103         }
104         return ret;
105 }
106
107 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
108                                    struct i2c_msg *msg, int retries,
109                                    unsigned char *add)
110 {
111         unsigned short flags = msg->flags;
112
113         unsigned char addr;
114         int ret;
115         if ((flags & I2C_M_TEN)) {
116                 /* a ten bit address */
117                 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
118                 /* try extended address code... */
119                 ret = try_write_address(i2c_adap, addr, retries);
120                 if (ret != 1) {
121                         err("died at extended address code, while writing");
122                         return -EREMOTEIO;
123                 }
124                 add[0] = addr;
125                 if (flags & I2C_M_RD) {
126                         /* okay, now switch into reading mode */
127                         addr |= 0x01;
128                         ret = try_read_address(i2c_adap, addr, retries);
129                         if (ret != 1) {
130                                 err("died at extended address code, while reading");
131                                 return -EREMOTEIO;
132                         }
133                 }
134
135         } else {                /* normal 7bit address  */
136                 addr = (msg->addr << 1);
137                 if (flags & I2C_M_RD)
138                         addr |= 1;
139                 if (flags & I2C_M_REV_DIR_ADDR)
140                         addr ^= 1;
141
142                 add[0] = addr;
143                 if (flags & I2C_M_RD)
144                         ret = try_read_address(i2c_adap, addr, retries);
145                 else
146                         ret = try_write_address(i2c_adap, addr, retries);
147
148                 if (ret != 1) {
149                         return -EREMOTEIO;
150                 }
151         }
152         return 0;
153 }
154
155 static int
156 usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
157 {
158         struct i2c_msg *pmsg;
159         void *data;
160         int i, ret;
161         unsigned char addr;
162
163         data = i2c_get_adapdata(i2c_adap);
164
165         for (i = 0; i < num; i++) {
166                 pmsg = &msgs[i];
167                 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
168                 if (ret != 0) {
169                         PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
170                         return (ret < 0) ? ret : -EREMOTEIO;
171                 }
172
173                 if (pmsg->flags & I2C_M_RD) {
174                         /* read bytes into buffer */
175                         ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len));
176                         if (ret < pmsg->len) {
177                                 return (ret < 0) ? ret : -EREMOTEIO;
178                         }
179                 } else {
180                         /* write bytes from buffer */
181                         ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len));
182                         if (ret < pmsg->len) {
183                                 return (ret < 0) ? ret : -EREMOTEIO;
184                         }
185                 }
186         }
187         return num;
188 }
189
190 static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
191 {
192         return 0;
193 }
194
195 static u32 usb_func(struct i2c_adapter *adap)
196 {
197         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
198 }
199
200
201 /* -----exported algorithm data: -------------------------------------  */
202
203 static struct i2c_algorithm i2c_usb_algo = {
204         .master_xfer   = usb_xfer,
205         .smbus_xfer    = NULL,
206         .algo_control  = algo_control,
207         .functionality = usb_func,
208 };
209
210
211 /*
212  * registering functions to load algorithms at runtime
213  */
214 static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
215 {
216         PDEBUG(DBG_I2C, "I2C   debugging is enabled [i2c]");
217         PDEBUG(DBG_ALGO, "ALGO   debugging is enabled [i2c]");
218
219         /* register new adapter to i2c module... */
220
221         adap->algo = &i2c_usb_algo;
222
223         adap->timeout = 100;    /* default values, should       */
224         adap->retries = 3;      /* be replaced by defines       */
225
226         i2c_add_adapter(adap);
227
228         PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
229
230         return 0;
231 }
232
233
234 int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
235 {
236
237         i2c_del_adapter(adap);
238
239         PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
240
241         return 0;
242 }
243
244
245 /* ----------------------------------------------------------------------- */
246 /* usbvision specific I2C functions                                        */
247 /* ----------------------------------------------------------------------- */
248 static struct i2c_adapter i2c_adap_template;
249 static struct i2c_client i2c_client_template;
250
251 int usbvision_init_i2c(struct usb_usbvision *usbvision)
252 {
253         memcpy(&usbvision->i2c_adap, &i2c_adap_template,
254                sizeof(struct i2c_adapter));
255         memcpy(&usbvision->i2c_client, &i2c_client_template,
256                sizeof(struct i2c_client));
257
258         sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
259                 " #%d", usbvision->vdev->minor & 0x1f);
260         PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
261
262         i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
263         i2c_set_clientdata(&usbvision->i2c_client, usbvision);
264
265         usbvision->i2c_client.adapter = &usbvision->i2c_adap;
266
267         if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
268                 printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
269                 return -EBUSY;
270         }
271
272 #ifdef CONFIG_MODULES
273         /* Request the load of the i2c modules we need */
274         switch (usbvision_device_data[usbvision->DevModel].Codec) {
275         case CODEC_SAA7113:
276                 request_module("saa7115");
277                 break;
278         case CODEC_SAA7111:
279                 request_module("saa7115");
280                 break;
281         }
282         if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
283                 request_module("tuner");
284         }
285 #endif
286
287         return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
288 }
289
290 void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
291                       void *arg)
292 {
293         i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
294 }
295
296 static int attach_inform(struct i2c_client *client)
297 {
298         struct usb_usbvision *usbvision;
299
300         usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
301
302         switch (client->addr << 1) {
303                 case 0x43:
304                 case 0x4b:
305                 {
306                         struct tuner_setup tun_setup;
307
308                         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
309                         tun_setup.type = TUNER_TDA9887;
310                         tun_setup.addr = client->addr;
311
312                         call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
313
314                         break;
315                 }
316                 case 0x42:
317                         PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
318                         break;
319                 case 0x4a:
320                         PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
321                         break;
322                 case 0x48:
323                         PDEBUG(DBG_I2C,"attach_inform: saa7111 detected.");
324                         break;
325                 case 0xa0:
326                         PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
327                         break;
328
329                 default:
330                         {
331                                 struct tuner_setup tun_setup;
332
333                                 PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
334                                 usbvision->tuner_addr = client->addr;
335
336                                 if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
337                                         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
338                                         tun_setup.type = usbvision->tuner_type;
339                                         tun_setup.addr = usbvision->tuner_addr;
340                                         call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
341                                 }
342                         }
343                         break;
344         }
345         return 0;
346 }
347
348 static int detach_inform(struct i2c_client *client)
349 {
350         struct usb_usbvision *usbvision;
351
352         usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
353
354         PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
355         return 0;
356 }
357
358 static int
359 usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
360                      char *buf, short len)
361 {
362         int rc, retries;
363
364         for (retries = 5;;) {
365                 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
366                 if (rc < 0)
367                         return rc;
368
369                 /* Initiate byte read cycle                    */
370                 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
371                 /*                    d3 0=Wr 1=Rd             */
372                 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
373                                       (len & 0x07) | 0x18);
374                 if (rc < 0)
375                         return rc;
376
377                 /* Test for Busy and ACK */
378                 do {
379                         /* USBVISION_SER_CONT -> d4 == 0 busy */
380                         rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
381                 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
382                 if (rc < 0)
383                         return rc;
384
385                 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
386                 if ((rc & 0x20) == 0)   /* Ack? */
387                         break;
388
389                 /* I2C abort */
390                 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
391                 if (rc < 0)
392                         return rc;
393
394                 if (--retries < 0)
395                         return -1;
396         }
397
398         switch (len) {
399         case 4:
400                 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
401         case 3:
402                 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
403         case 2:
404                 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
405         case 1:
406                 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
407                 break;
408         default:
409                 printk(KERN_ERR
410                        "usbvision_i2c_read_max4: buffer length > 4\n");
411         }
412
413         if (i2c_debug & DBG_I2C) {
414                 int idx;
415                 for (idx = 0; idx < len; idx++) {
416                         PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
417                 }
418         }
419         return len;
420 }
421
422
423 static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
424                                  unsigned char addr, const char *buf,
425                                  short len)
426 {
427         int rc, retries;
428         int i;
429         unsigned char value[6];
430         unsigned char ser_cont;
431
432         ser_cont = (len & 0x07) | 0x10;
433
434         value[0] = addr;
435         value[1] = ser_cont;
436         for (i = 0; i < len; i++)
437                 value[i + 2] = buf[i];
438
439         for (retries = 5;;) {
440                 rc = usb_control_msg(usbvision->dev,
441                                      usb_sndctrlpipe(usbvision->dev, 1),
442                                      USBVISION_OP_CODE,
443                                      USB_DIR_OUT | USB_TYPE_VENDOR |
444                                      USB_RECIP_ENDPOINT, 0,
445                                      (__u16) USBVISION_SER_ADRS, value,
446                                      len + 2, HZ);
447
448                 if (rc < 0)
449                         return rc;
450
451                 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
452                                       (len & 0x07) | 0x10);
453                 if (rc < 0)
454                         return rc;
455
456                 /* Test for Busy and ACK */
457                 do {
458                         rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
459                 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
460                 if (rc < 0)
461                         return rc;
462
463                 if ((rc & 0x20) == 0)   /* Ack? */
464                         break;
465
466                 /* I2C abort */
467                 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
468
469                 if (--retries < 0)
470                         return -1;
471
472         }
473
474         if (i2c_debug & DBG_I2C) {
475                 int idx;
476                 for (idx = 0; idx < len; idx++) {
477                         PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
478                 }
479         }
480         return len;
481 }
482
483 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
484                             short len)
485 {
486         char *bufPtr = buf;
487         int retval;
488         int wrcount = 0;
489         int count;
490         int maxLen = 4;
491         struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
492
493         while (len > 0) {
494                 count = (len > maxLen) ? maxLen : len;
495                 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
496                 if (retval > 0) {
497                         len -= count;
498                         bufPtr += count;
499                         wrcount += count;
500                 } else
501                         return (retval < 0) ? retval : -EFAULT;
502         }
503         return wrcount;
504 }
505
506 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
507                            short len)
508 {
509         char temp[4];
510         int retval, i;
511         int rdcount = 0;
512         int count;
513         struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
514
515         while (len > 0) {
516                 count = (len > 3) ? 4 : len;
517                 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
518                 if (retval > 0) {
519                         for (i = 0; i < len; i++)
520                                 buf[rdcount + i] = temp[i];
521                         len -= count;
522                         rdcount += count;
523                 } else
524                         return (retval < 0) ? retval : -EFAULT;
525         }
526         return rdcount;
527 }
528
529 static struct i2c_adapter i2c_adap_template = {
530         .owner = THIS_MODULE,
531         .name              = "usbvision",
532         .id                = I2C_HW_B_BT848, /* FIXME */
533         .client_register   = attach_inform,
534         .client_unregister = detach_inform,
535 #ifdef I2C_ADAP_CLASS_TV_ANALOG
536         .class             = I2C_ADAP_CLASS_TV_ANALOG,
537 #else
538         .class             = I2C_CLASS_TV_ANALOG,
539 #endif
540 };
541
542 static struct i2c_client i2c_client_template = {
543         .name           = "usbvision internal",
544 };
545
546 /*
547  * Overrides for Emacs so that we follow Linus's tabbing style.
548  * ---------------------------------------------------------------------------
549  * Local variables:
550  * c-basic-offset: 8
551  * End:
552  */