Merge git://git.infradead.org/mtd-2.6
[linux-2.6] / 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/usb.h>
38 #include <linux/i2c.h>
39 #include "usbvision.h"
40
41 #define DBG_I2C         1<<0
42 #define DBG_ALGO        1<<1
43
44 static int i2c_debug = 0;
45
46 module_param (i2c_debug, int, 0644);                    // debug_i2c_usb mode of the device driver
47 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
48
49 #define PDEBUG(level, fmt, args...) \
50                 if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
51
52 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
53                             short len);
54 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
55                            short len);
56
57 static inline int try_write_address(struct i2c_adapter *i2c_adap,
58                                     unsigned char addr, int retries)
59 {
60         void *data;
61         int i, ret = -1;
62         char buf[4];
63
64         data = i2c_get_adapdata(i2c_adap);
65         buf[0] = 0x00;
66         for (i = 0; i <= retries; i++) {
67                 ret = (usbvision_i2c_write(data, addr, buf, 1));
68                 if (ret == 1)
69                         break;  /* success! */
70                 udelay(5);
71                 if (i == retries)       /* no success */
72                         break;
73                 udelay(10);
74         }
75         if (i) {
76                 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
77                 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
78         }
79         return ret;
80 }
81
82 static inline int try_read_address(struct i2c_adapter *i2c_adap,
83                                    unsigned char addr, int retries)
84 {
85         void *data;
86         int i, ret = -1;
87         char buf[4];
88
89         data = i2c_get_adapdata(i2c_adap);
90         for (i = 0; i <= retries; i++) {
91                 ret = (usbvision_i2c_read(data, addr, buf, 1));
92                 if (ret == 1)
93                         break;  /* success! */
94                 udelay(5);
95                 if (i == retries)       /* no success */
96                         break;
97                 udelay(10);
98         }
99         if (i) {
100                 PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
101                 PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
102         }
103         return ret;
104 }
105
106 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
107                                    struct i2c_msg *msg, int retries,
108                                    unsigned char *add)
109 {
110         unsigned short flags = msg->flags;
111
112         unsigned char addr;
113         int ret;
114         if ((flags & I2C_M_TEN)) {
115                 /* a ten bit address */
116                 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
117                 /* try extended address code... */
118                 ret = try_write_address(i2c_adap, addr, retries);
119                 if (ret != 1) {
120                         err("died at extended address code, while writing");
121                         return -EREMOTEIO;
122                 }
123                 add[0] = addr;
124                 if (flags & I2C_M_RD) {
125                         /* okay, now switch into reading mode */
126                         addr |= 0x01;
127                         ret = try_read_address(i2c_adap, addr, retries);
128                         if (ret != 1) {
129                                 err("died at extended address code, while reading");
130                                 return -EREMOTEIO;
131                         }
132                 }
133
134         } else {                /* normal 7bit address  */
135                 addr = (msg->addr << 1);
136                 if (flags & I2C_M_RD)
137                         addr |= 1;
138                 if (flags & I2C_M_REV_DIR_ADDR)
139                         addr ^= 1;
140
141                 add[0] = addr;
142                 if (flags & I2C_M_RD)
143                         ret = try_read_address(i2c_adap, addr, retries);
144                 else
145                         ret = try_write_address(i2c_adap, addr, retries);
146
147                 if (ret != 1) {
148                         return -EREMOTEIO;
149                 }
150         }
151         return 0;
152 }
153
154 static int
155 usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
156 {
157         struct i2c_msg *pmsg;
158         void *data;
159         int i, ret;
160         unsigned char addr;
161
162         data = i2c_get_adapdata(i2c_adap);
163
164         for (i = 0; i < num; i++) {
165                 pmsg = &msgs[i];
166                 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
167                 if (ret != 0) {
168                         PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
169                         return (ret < 0) ? ret : -EREMOTEIO;
170                 }
171
172                 if (pmsg->flags & I2C_M_RD) {
173                         /* read bytes into buffer */
174                         ret = (usbvision_i2c_read(data, addr, pmsg->buf, pmsg->len));
175                         if (ret < pmsg->len) {
176                                 return (ret < 0) ? ret : -EREMOTEIO;
177                         }
178                 } else {
179                         /* write bytes from buffer */
180                         ret = (usbvision_i2c_write(data, addr, pmsg->buf, pmsg->len));
181                         if (ret < pmsg->len) {
182                                 return (ret < 0) ? ret : -EREMOTEIO;
183                         }
184                 }
185         }
186         return num;
187 }
188
189 static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
190 {
191         return 0;
192 }
193
194 static u32 usb_func(struct i2c_adapter *adap)
195 {
196         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
197 }
198
199
200 /* -----exported algorithm data: -------------------------------------  */
201
202 static struct i2c_algorithm i2c_usb_algo = {
203         .master_xfer   = usb_xfer,
204         .smbus_xfer    = NULL,
205         .algo_control  = algo_control,
206         .functionality = usb_func,
207 };
208
209
210 /*
211  * registering functions to load algorithms at runtime
212  */
213 static int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
214 {
215         PDEBUG(DBG_I2C, "I2C   debugging is enabled [i2c]");
216         PDEBUG(DBG_ALGO, "ALGO   debugging is enabled [i2c]");
217
218         /* register new adapter to i2c module... */
219
220         adap->algo = &i2c_usb_algo;
221
222         adap->timeout = 100;    /* default values, should       */
223         adap->retries = 3;      /* be replaced by defines       */
224
225         i2c_add_adapter(adap);
226
227         PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
228
229         return 0;
230 }
231
232
233 int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
234 {
235
236         i2c_del_adapter(adap);
237
238         PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
239
240         return 0;
241 }
242
243
244 /* ----------------------------------------------------------------------- */
245 /* usbvision specific I2C functions                                        */
246 /* ----------------------------------------------------------------------- */
247 static struct i2c_adapter i2c_adap_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_client, &i2c_client_template,
255                sizeof(struct i2c_client));
256
257         sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
258                 " #%d", usbvision->vdev->minor & 0x1f);
259         PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
260         usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
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  */