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