headers: utsname.h redux
[firewire-audio.git] / drivers / media / video / usbvision / usbvision-i2c.c
blobc19f51dba2ee28b3d0827c93df30044ebfe69766
1 /*
2 * usbvision_i2c.c
3 * i2c algorithm for USB-I2C Bridges
5 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
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.
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.
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.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <asm/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/errno.h>
35 #include <linux/usb.h>
36 #include <linux/i2c.h>
37 #include "usbvision.h"
39 #define DBG_I2C 1<<0
41 static int i2c_debug;
43 module_param (i2c_debug, int, 0644); // debug_i2c_usb mode of the device driver
44 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
46 #define PDEBUG(level, fmt, args...) { \
47 if (i2c_debug & (level)) \
48 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
49 __func__, __LINE__ , ## args); \
52 static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
53 short len);
54 static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
55 short len);
57 static inline int try_write_address(struct i2c_adapter *i2c_adap,
58 unsigned char addr, int retries)
60 struct usb_usbvision *usbvision;
61 int i, ret = -1;
62 char buf[4];
64 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
65 buf[0] = 0x00;
66 for (i = 0; i <= retries; i++) {
67 ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
68 if (ret == 1)
69 break; /* success! */
70 udelay(5);
71 if (i == retries) /* no success */
72 break;
73 udelay(10);
75 if (i) {
76 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
77 PDEBUG(DBG_I2C,"Maybe there's no device at this address");
79 return ret;
82 static inline int try_read_address(struct i2c_adapter *i2c_adap,
83 unsigned char addr, int retries)
85 struct usb_usbvision *usbvision;
86 int i, ret = -1;
87 char buf[4];
89 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
90 for (i = 0; i <= retries; i++) {
91 ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
92 if (ret == 1)
93 break; /* success! */
94 udelay(5);
95 if (i == retries) /* no success */
96 break;
97 udelay(10);
99 if (i) {
100 PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr);
101 PDEBUG(DBG_I2C,"Maybe there's no device at this address");
103 return ret;
106 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
107 struct i2c_msg *msg, int retries,
108 unsigned char *add)
110 unsigned short flags = msg->flags;
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 dev_err(&i2c_adap->dev,
121 "died at extended address code, while writing\n");
122 return -EREMOTEIO;
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 dev_err(&i2c_adap->dev,
131 "died at extended address code, while reading\n");
132 return -EREMOTEIO;
136 } else { /* normal 7bit address */
137 addr = (msg->addr << 1);
138 if (flags & I2C_M_RD)
139 addr |= 1;
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);
147 if (ret != 1) {
148 return -EREMOTEIO;
151 return 0;
154 static int
155 usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
157 struct i2c_msg *pmsg;
158 struct usb_usbvision *usbvision;
159 int i, ret;
160 unsigned char addr = 0;
162 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
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_I2C,"got NAK from device, message #%d", i);
169 return (ret < 0) ? ret : -EREMOTEIO;
172 if (pmsg->flags & I2C_M_RD) {
173 /* read bytes into buffer */
174 ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
175 if (ret < pmsg->len) {
176 return (ret < 0) ? ret : -EREMOTEIO;
178 } else {
179 /* write bytes from buffer */
180 ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
181 if (ret < pmsg->len) {
182 return (ret < 0) ? ret : -EREMOTEIO;
186 return num;
189 static u32 functionality(struct i2c_adapter *adap)
191 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
194 /* -----exported algorithm data: ------------------------------------- */
196 static struct i2c_algorithm usbvision_algo = {
197 .master_xfer = usbvision_i2c_xfer,
198 .smbus_xfer = NULL,
199 .functionality = functionality,
203 /* ----------------------------------------------------------------------- */
204 /* usbvision specific I2C functions */
205 /* ----------------------------------------------------------------------- */
206 static struct i2c_adapter i2c_adap_template;
208 int usbvision_i2c_register(struct usb_usbvision *usbvision)
210 static unsigned short saa711x_addrs[] = {
211 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
212 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
213 I2C_CLIENT_END };
215 memcpy(&usbvision->i2c_adap, &i2c_adap_template,
216 sizeof(struct i2c_adapter));
218 sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
219 " #%d", usbvision->vdev->num);
220 PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
221 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
223 i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
225 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
226 printk(KERN_ERR "usbvision_register: can't write reg\n");
227 return -EBUSY;
230 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
231 PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
233 /* register new adapter to i2c module... */
235 usbvision->i2c_adap.algo = &usbvision_algo;
237 usbvision->i2c_adap.timeout = 100; /* default values, should */
238 usbvision->i2c_adap.retries = 3; /* be replaced by defines */
240 i2c_add_adapter(&usbvision->i2c_adap);
242 PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
244 /* Request the load of the i2c modules we need */
245 switch (usbvision_device_data[usbvision->DevModel].Codec) {
246 case CODEC_SAA7113:
247 case CODEC_SAA7111:
248 v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
249 &usbvision->i2c_adap, "saa7115",
250 "saa7115_auto", 0, saa711x_addrs);
251 break;
253 if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
254 struct v4l2_subdev *sd;
255 enum v4l2_i2c_tuner_type type;
256 struct tuner_setup tun_setup;
258 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
259 &usbvision->i2c_adap, "tuner",
260 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
261 /* depending on whether we found a demod or not, select
262 the tuner type. */
263 type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
265 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
266 &usbvision->i2c_adap, "tuner",
267 "tuner", 0, v4l2_i2c_tuner_addrs(type));
269 if (usbvision->tuner_type != -1) {
270 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
271 tun_setup.type = usbvision->tuner_type;
272 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
273 call_all(usbvision, tuner, s_type_addr, &tun_setup);
277 return 0;
280 int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
283 i2c_del_adapter(&(usbvision->i2c_adap));
285 PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name);
287 return 0;
290 static int
291 usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
292 char *buf, short len)
294 int rc, retries;
296 for (retries = 5;;) {
297 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
298 if (rc < 0)
299 return rc;
301 /* Initiate byte read cycle */
302 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
303 /* d3 0=Wr 1=Rd */
304 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
305 (len & 0x07) | 0x18);
306 if (rc < 0)
307 return rc;
309 /* Test for Busy and ACK */
310 do {
311 /* USBVISION_SER_CONT -> d4 == 0 busy */
312 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
313 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
314 if (rc < 0)
315 return rc;
317 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
318 if ((rc & 0x20) == 0) /* Ack? */
319 break;
321 /* I2C abort */
322 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
323 if (rc < 0)
324 return rc;
326 if (--retries < 0)
327 return -1;
330 switch (len) {
331 case 4:
332 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
333 case 3:
334 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
335 case 2:
336 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
337 case 1:
338 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
339 break;
340 default:
341 printk(KERN_ERR
342 "usbvision_i2c_read_max4: buffer length > 4\n");
345 if (i2c_debug & DBG_I2C) {
346 int idx;
347 for (idx = 0; idx < len; idx++) {
348 PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
351 return len;
355 static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
356 unsigned char addr, const char *buf,
357 short len)
359 int rc, retries;
360 int i;
361 unsigned char value[6];
362 unsigned char ser_cont;
364 ser_cont = (len & 0x07) | 0x10;
366 value[0] = addr;
367 value[1] = ser_cont;
368 for (i = 0; i < len; i++)
369 value[i + 2] = buf[i];
371 for (retries = 5;;) {
372 rc = usb_control_msg(usbvision->dev,
373 usb_sndctrlpipe(usbvision->dev, 1),
374 USBVISION_OP_CODE,
375 USB_DIR_OUT | USB_TYPE_VENDOR |
376 USB_RECIP_ENDPOINT, 0,
377 (__u16) USBVISION_SER_ADRS, value,
378 len + 2, HZ);
380 if (rc < 0)
381 return rc;
383 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
384 (len & 0x07) | 0x10);
385 if (rc < 0)
386 return rc;
388 /* Test for Busy and ACK */
389 do {
390 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
391 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
392 if (rc < 0)
393 return rc;
395 if ((rc & 0x20) == 0) /* Ack? */
396 break;
398 /* I2C abort */
399 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
401 if (--retries < 0)
402 return -1;
406 if (i2c_debug & DBG_I2C) {
407 int idx;
408 for (idx = 0; idx < len; idx++) {
409 PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
412 return len;
415 static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
416 short len)
418 char *bufPtr = buf;
419 int retval;
420 int wrcount = 0;
421 int count;
422 int maxLen = 4;
424 while (len > 0) {
425 count = (len > maxLen) ? maxLen : len;
426 retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
427 if (retval > 0) {
428 len -= count;
429 bufPtr += count;
430 wrcount += count;
431 } else
432 return (retval < 0) ? retval : -EFAULT;
434 return wrcount;
437 static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
438 short len)
440 char temp[4];
441 int retval, i;
442 int rdcount = 0;
443 int count;
445 while (len > 0) {
446 count = (len > 3) ? 4 : len;
447 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
448 if (retval > 0) {
449 for (i = 0; i < len; i++)
450 buf[rdcount + i] = temp[i];
451 len -= count;
452 rdcount += count;
453 } else
454 return (retval < 0) ? retval : -EFAULT;
456 return rdcount;
459 static struct i2c_adapter i2c_adap_template = {
460 .owner = THIS_MODULE,
461 .name = "usbvision",
465 * Overrides for Emacs so that we follow Linus's tabbing style.
466 * ---------------------------------------------------------------------------
467 * Local variables:
468 * c-basic-offset: 8
469 * End: