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