Thierry MERLE | f2242ee | 2006-12-04 08:31:14 -0300 | [diff] [blame] | 1 | /* |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 2 | * usbvision_i2c.c |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 3 | * i2c algorithm for USB-I2C Bridges |
| 4 | * |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 5 | * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de> |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 31 | #include <linux/init.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | #include <linux/ioport.h> |
| 34 | #include <linux/errno.h> |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 35 | #include <linux/usb.h> |
| 36 | #include <linux/i2c.h> |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 37 | #include "usbvision.h" |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 38 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 39 | #define DBG_I2C 1<<0 |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 40 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 41 | static int i2c_debug; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 42 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 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]"); |
| 45 | |
Thierry MERLE | df18c31 | 2008-04-04 21:00:57 -0300 | [diff] [blame] | 46 | #define PDEBUG(level, fmt, args...) { \ |
| 47 | if (i2c_debug & (level)) \ |
Greg Kroah-Hartman | a482f32 | 2008-10-10 05:08:23 -0300 | [diff] [blame] | 48 | printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \ |
| 49 | __func__, __LINE__ , ## args); \ |
Thierry MERLE | df18c31 | 2008-04-04 21:00:57 -0300 | [diff] [blame] | 50 | } |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 51 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 52 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Mauro Carvalho Chehab | 2a7e9a2 | 2006-12-11 05:37:14 -0300 | [diff] [blame] | 53 | short len); |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 54 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Mauro Carvalho Chehab | 2a7e9a2 | 2006-12-11 05:37:14 -0300 | [diff] [blame] | 55 | short len); |
| 56 | |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 57 | static inline int try_write_address(struct i2c_adapter *i2c_adap, |
| 58 | unsigned char addr, int retries) |
| 59 | { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 60 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 61 | int i, ret = -1; |
| 62 | char buf[4]; |
| 63 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 64 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 65 | buf[0] = 0x00; |
| 66 | for (i = 0; i <= retries; i++) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 67 | ret = (usbvision_i2c_write(usbvision, addr, buf, 1)); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 68 | if (ret == 1) |
| 69 | break; /* success! */ |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 70 | udelay(5); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 71 | if (i == retries) /* no success */ |
| 72 | break; |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 73 | udelay(10); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 74 | } |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 75 | if (i) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 76 | PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); |
| 77 | PDEBUG(DBG_I2C,"Maybe there's no device at this address"); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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 | { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 85 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 86 | int i, ret = -1; |
| 87 | char buf[4]; |
| 88 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 89 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 90 | for (i = 0; i <= retries; i++) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 91 | ret = (usbvision_i2c_read(usbvision, addr, buf, 1)); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 92 | if (ret == 1) |
| 93 | break; /* success! */ |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 94 | udelay(5); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 95 | if (i == retries) /* no success */ |
| 96 | break; |
Mauro Carvalho Chehab | c626846 | 2006-12-11 17:18:15 -0300 | [diff] [blame] | 97 | udelay(10); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 98 | } |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 99 | if (i) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 100 | PDEBUG(DBG_I2C,"Needed %d retries for address %#2x", i, addr); |
| 101 | PDEBUG(DBG_I2C,"Maybe there's no device at this address"); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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; |
Thierry MERLE | f2242ee | 2006-12-04 08:31:14 -0300 | [diff] [blame] | 111 | |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 120 | dev_err(&i2c_adap->dev, |
| 121 | "died at extended address code, while writing\n"); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 130 | dev_err(&i2c_adap->dev, |
| 131 | "died at extended address code, while reading\n"); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 132 | return -EREMOTEIO; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } else { /* normal 7bit address */ |
| 137 | addr = (msg->addr << 1); |
| 138 | if (flags & I2C_M_RD) |
| 139 | addr |= 1; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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 |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 155 | usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 156 | { |
| 157 | struct i2c_msg *pmsg; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 158 | struct usb_usbvision *usbvision; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 159 | int i, ret; |
Hans Verkuil | cf8e193 | 2009-01-17 13:09:11 -0300 | [diff] [blame] | 160 | unsigned char addr = 0; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 161 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 162 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap); |
| 163 | |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 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) { |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 168 | PDEBUG(DBG_I2C,"got NAK from device, message #%d", i); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 169 | return (ret < 0) ? ret : -EREMOTEIO; |
| 170 | } |
| 171 | |
| 172 | if (pmsg->flags & I2C_M_RD) { |
| 173 | /* read bytes into buffer */ |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 174 | ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len)); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 175 | if (ret < pmsg->len) { |
| 176 | return (ret < 0) ? ret : -EREMOTEIO; |
| 177 | } |
| 178 | } else { |
| 179 | /* write bytes from buffer */ |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 180 | ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len)); |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 181 | if (ret < pmsg->len) { |
| 182 | return (ret < 0) ? ret : -EREMOTEIO; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return num; |
| 187 | } |
| 188 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 189 | static u32 functionality(struct i2c_adapter *adap) |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 190 | { |
Jean Delvare | 39f46ad | 2007-09-09 06:17:44 -0300 | [diff] [blame] | 191 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR; |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 192 | } |
| 193 | |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 194 | /* -----exported algorithm data: ------------------------------------- */ |
| 195 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 196 | static struct i2c_algorithm usbvision_algo = { |
| 197 | .master_xfer = usbvision_i2c_xfer, |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 198 | .smbus_xfer = NULL, |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 199 | .functionality = functionality, |
Mauro Carvalho Chehab | 781aa1d | 2006-12-04 08:30:53 -0300 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 203 | /* ----------------------------------------------------------------------- */ |
| 204 | /* usbvision specific I2C functions */ |
| 205 | /* ----------------------------------------------------------------------- */ |
| 206 | static struct i2c_adapter i2c_adap_template; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 207 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 208 | int usbvision_i2c_register(struct usb_usbvision *usbvision) |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 209 | { |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 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 }; |
| 214 | |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 215 | memcpy(&usbvision->i2c_adap, &i2c_adap_template, |
| 216 | sizeof(struct i2c_adapter)); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 217 | |
| 218 | sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name), |
Hans Verkuil | c6330fb | 2008-10-19 18:54:26 -0300 | [diff] [blame] | 219 | " #%d", usbvision->vdev->num); |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 220 | PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name); |
| 221 | usbvision->i2c_adap.dev.parent = &usbvision->dev->dev; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 222 | |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 223 | i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 224 | |
| 225 | if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { |
Thierry MERLE | bf8b20e | 2007-04-16 18:00:49 -0300 | [diff] [blame] | 226 | printk(KERN_ERR "usbvision_register: can't write reg\n"); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 227 | return -EBUSY; |
| 228 | } |
| 229 | |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 230 | PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]"); |
| 231 | PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]"); |
| 232 | |
| 233 | /* register new adapter to i2c module... */ |
| 234 | |
| 235 | usbvision->i2c_adap.algo = &usbvision_algo; |
| 236 | |
| 237 | usbvision->i2c_adap.timeout = 100; /* default values, should */ |
| 238 | usbvision->i2c_adap.retries = 3; /* be replaced by defines */ |
| 239 | |
| 240 | i2c_add_adapter(&usbvision->i2c_adap); |
| 241 | |
| 242 | PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name); |
| 243 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 244 | /* Request the load of the i2c modules we need */ |
| 245 | switch (usbvision_device_data[usbvision->DevModel].Codec) { |
| 246 | case CODEC_SAA7113: |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 247 | case CODEC_SAA7111: |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 248 | v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 249 | &usbvision->i2c_adap, "saa7115", |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 250 | "saa7115_auto", 0, saa711x_addrs); |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 254 | struct v4l2_subdev *sd; |
| 255 | enum v4l2_i2c_tuner_type type; |
| 256 | struct tuner_setup tun_setup; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 257 | |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 258 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 259 | &usbvision->i2c_adap, "tuner", |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 260 | "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 261 | /* depending on whether we found a demod or not, select |
| 262 | the tuner type. */ |
| 263 | type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV; |
| 264 | |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 265 | sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev, |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 266 | &usbvision->i2c_adap, "tuner", |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 267 | "tuner", 0, v4l2_i2c_tuner_addrs(type)); |
Hans Verkuil | 1df7953 | 2009-02-21 18:11:31 -0300 | [diff] [blame] | 268 | |
| 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); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return 0; |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | int usbvision_i2c_unregister(struct usb_usbvision *usbvision) |
| 281 | { |
| 282 | |
| 283 | i2c_del_adapter(&(usbvision->i2c_adap)); |
| 284 | |
| 285 | PDEBUG(DBG_I2C,"i2c bus for %s unregistered", usbvision->i2c_adap.name); |
| 286 | |
| 287 | return 0; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 288 | } |
| 289 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 290 | static int |
| 291 | usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, |
| 292 | char *buf, short len) |
| 293 | { |
| 294 | int rc, retries; |
| 295 | |
| 296 | for (retries = 5;;) { |
| 297 | rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); |
| 298 | if (rc < 0) |
| 299 | return rc; |
| 300 | |
| 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; |
| 308 | |
| 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; |
| 316 | |
| 317 | /* USBVISION_SER_CONT -> d5 == 1 Not ack */ |
| 318 | if ((rc & 0x20) == 0) /* Ack? */ |
| 319 | break; |
| 320 | |
| 321 | /* I2C abort */ |
| 322 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 323 | if (rc < 0) |
| 324 | return rc; |
| 325 | |
| 326 | if (--retries < 0) |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 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"); |
| 343 | } |
| 344 | |
| 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); |
| 349 | } |
| 350 | } |
| 351 | return len; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, |
| 356 | unsigned char addr, const char *buf, |
| 357 | short len) |
| 358 | { |
| 359 | int rc, retries; |
| 360 | int i; |
| 361 | unsigned char value[6]; |
| 362 | unsigned char ser_cont; |
| 363 | |
| 364 | ser_cont = (len & 0x07) | 0x10; |
| 365 | |
| 366 | value[0] = addr; |
| 367 | value[1] = ser_cont; |
| 368 | for (i = 0; i < len; i++) |
| 369 | value[i + 2] = buf[i]; |
| 370 | |
| 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); |
| 379 | |
| 380 | if (rc < 0) |
| 381 | return rc; |
| 382 | |
| 383 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, |
| 384 | (len & 0x07) | 0x10); |
| 385 | if (rc < 0) |
| 386 | return rc; |
| 387 | |
| 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; |
| 394 | |
| 395 | if ((rc & 0x20) == 0) /* Ack? */ |
| 396 | break; |
| 397 | |
| 398 | /* I2C abort */ |
| 399 | usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 400 | |
| 401 | if (--retries < 0) |
| 402 | return -1; |
| 403 | |
| 404 | } |
| 405 | |
| 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); |
| 410 | } |
| 411 | } |
| 412 | return len; |
| 413 | } |
| 414 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 415 | static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 416 | short len) |
| 417 | { |
| 418 | char *bufPtr = buf; |
| 419 | int retval; |
| 420 | int wrcount = 0; |
| 421 | int count; |
| 422 | int maxLen = 4; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 423 | |
| 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; |
| 433 | } |
| 434 | return wrcount; |
| 435 | } |
| 436 | |
Thierry MERLE | 1ff16c2 | 2007-04-14 16:23:49 -0300 | [diff] [blame] | 437 | static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf, |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 438 | short len) |
| 439 | { |
| 440 | char temp[4]; |
| 441 | int retval, i; |
| 442 | int rdcount = 0; |
| 443 | int count; |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 444 | |
| 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; |
| 455 | } |
| 456 | return rdcount; |
| 457 | } |
| 458 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 459 | static struct i2c_adapter i2c_adap_template = { |
| 460 | .owner = THIS_MODULE, |
| 461 | .name = "usbvision", |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 462 | }; |
| 463 | |
Thierry MERLE | 483dfdb | 2006-12-04 08:31:45 -0300 | [diff] [blame] | 464 | /* |
| 465 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 466 | * --------------------------------------------------------------------------- |
| 467 | * Local variables: |
| 468 | * c-basic-offset: 8 |
| 469 | * End: |
| 470 | */ |