Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Auvitek AU0828 USB bridge |
| 3 | * |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/delay.h> |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 26 | #include <linux/io.h> |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 27 | |
| 28 | #include "au0828.h" |
| 29 | |
| 30 | #include <media/v4l2-common.h> |
| 31 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 32 | static int i2c_scan; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 33 | module_param(i2c_scan, int, 0444); |
| 34 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
| 35 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 36 | #define I2C_WAIT_DELAY 512 |
| 37 | #define I2C_WAIT_RETRY 64 |
| 38 | |
| 39 | static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap) |
| 40 | { |
| 41 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 42 | return au0828_read(dev, REG_201) & 0x08 ? 0 : 1; |
| 43 | } |
| 44 | |
| 45 | static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap) |
| 46 | { |
| 47 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 48 | return au0828_read(dev, REG_201) & 0x02 ? 0 : 1; |
| 49 | } |
| 50 | |
| 51 | static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap) |
| 52 | { |
| 53 | int count; |
| 54 | |
| 55 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 56 | if (!i2c_slave_did_read_ack(i2c_adap)) |
| 57 | break; |
| 58 | udelay(I2C_WAIT_DELAY); |
| 59 | } |
| 60 | |
| 61 | if (I2C_WAIT_RETRY == count) |
| 62 | return 0; |
| 63 | |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap) |
| 68 | { |
| 69 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 70 | return au0828_read(dev, REG_201) & 0x01 ? 0 : 1; |
| 71 | } |
| 72 | |
| 73 | static int i2c_wait_read_done(struct i2c_adapter *i2c_adap) |
| 74 | { |
| 75 | int count; |
| 76 | |
| 77 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 78 | if (!i2c_is_read_busy(i2c_adap)) |
| 79 | break; |
| 80 | udelay(I2C_WAIT_DELAY); |
| 81 | } |
| 82 | |
| 83 | if (I2C_WAIT_RETRY == count) |
| 84 | return 0; |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap) |
| 90 | { |
| 91 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 92 | return au0828_read(dev, REG_201) & 0x04 ? 1 : 0; |
| 93 | } |
| 94 | |
| 95 | static int i2c_wait_write_done(struct i2c_adapter *i2c_adap) |
| 96 | { |
| 97 | int count; |
| 98 | |
| 99 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 100 | if (i2c_is_write_done(i2c_adap)) |
| 101 | break; |
| 102 | udelay(I2C_WAIT_DELAY); |
| 103 | } |
| 104 | |
| 105 | if (I2C_WAIT_RETRY == count) |
| 106 | return 0; |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) |
| 112 | { |
| 113 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 114 | return au0828_read(dev, REG_201) & 0x10 ? 1 : 0; |
| 115 | } |
| 116 | |
| 117 | static int i2c_wait_done(struct i2c_adapter *i2c_adap) |
| 118 | { |
| 119 | int count; |
| 120 | |
| 121 | for (count = 0; count < I2C_WAIT_RETRY; count++) { |
| 122 | if (!i2c_is_busy(i2c_adap)) |
| 123 | break; |
| 124 | udelay(I2C_WAIT_DELAY); |
| 125 | } |
| 126 | |
| 127 | if (I2C_WAIT_RETRY == count) |
| 128 | return 0; |
| 129 | |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | /* FIXME: Implement join handling correctly */ |
| 134 | static int i2c_sendbytes(struct i2c_adapter *i2c_adap, |
| 135 | const struct i2c_msg *msg, int joined_rlen) |
| 136 | { |
| 137 | int i, strobe = 0; |
| 138 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 139 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 140 | dprintk(4, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 141 | |
| 142 | au0828_write(dev, REG_2FF, 0x01); |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 143 | |
| 144 | /* FIXME: There is a problem with i2c communications with xc5000 that |
| 145 | requires us to slow down the i2c clock until we have a better |
| 146 | strategy (such as using the secondary i2c bus to do firmware |
| 147 | loading */ |
Devin Heitmueller | 62899a2 | 2009-03-15 18:48:52 -0300 | [diff] [blame] | 148 | if ((msg->addr << 1) == 0xc2) |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 149 | au0828_write(dev, REG_202, 0x40); |
Devin Heitmueller | 62899a2 | 2009-03-15 18:48:52 -0300 | [diff] [blame] | 150 | else |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 151 | au0828_write(dev, REG_202, 0x07); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 152 | |
| 153 | /* Hardware needs 8 bit addresses */ |
| 154 | au0828_write(dev, REG_203, msg->addr << 1); |
| 155 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 156 | dprintk(4, "SEND: %02x\n", msg->addr); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 157 | |
Devin Heitmueller | dc8685b | 2009-03-11 03:00:56 -0300 | [diff] [blame] | 158 | /* Deal with i2c_scan */ |
| 159 | if (msg->len == 0) { |
| 160 | /* The analog tuner detection code makes use of the SMBUS_QUICK |
| 161 | message (which involves a zero length i2c write). To avoid |
| 162 | checking the status register when we didn't strobe out any |
| 163 | actual bytes to the bus, just do a read check. This is |
| 164 | consistent with how I saw i2c device checking done in the |
| 165 | USB trace of the Windows driver */ |
| 166 | au0828_write(dev, REG_200, 0x20); |
| 167 | if (!i2c_wait_done(i2c_adap)) |
| 168 | return -EIO; |
| 169 | |
| 170 | if (i2c_wait_read_ack(i2c_adap)) |
| 171 | return -EIO; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 176 | for (i = 0; i < msg->len;) { |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 177 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 178 | dprintk(4, " %02x\n", msg->buf[i]); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 179 | |
| 180 | au0828_write(dev, REG_205, msg->buf[i]); |
| 181 | |
| 182 | strobe++; |
| 183 | i++; |
| 184 | |
| 185 | if ((strobe >= 4) || (i >= msg->len)) { |
| 186 | |
| 187 | /* Strobe the byte into the bus */ |
| 188 | if (i < msg->len) |
| 189 | au0828_write(dev, REG_200, 0x41); |
| 190 | else |
| 191 | au0828_write(dev, REG_200, 0x01); |
| 192 | |
| 193 | /* Reset strobe trigger */ |
| 194 | strobe = 0; |
| 195 | |
| 196 | if (!i2c_wait_write_done(i2c_adap)) |
| 197 | return -EIO; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | if (!i2c_wait_done(i2c_adap)) |
| 203 | return -EIO; |
| 204 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 205 | dprintk(4, "\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 206 | |
| 207 | return msg->len; |
| 208 | } |
| 209 | |
| 210 | /* FIXME: Implement join handling correctly */ |
| 211 | static int i2c_readbytes(struct i2c_adapter *i2c_adap, |
| 212 | const struct i2c_msg *msg, int joined) |
| 213 | { |
| 214 | struct au0828_dev *dev = i2c_adap->algo_data; |
| 215 | int i; |
| 216 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 217 | dprintk(4, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 218 | |
| 219 | au0828_write(dev, REG_2FF, 0x01); |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 220 | |
| 221 | /* FIXME: There is a problem with i2c communications with xc5000 that |
| 222 | requires us to slow down the i2c clock until we have a better |
| 223 | strategy (such as using the secondary i2c bus to do firmware |
| 224 | loading */ |
Devin Heitmueller | 62899a2 | 2009-03-15 18:48:52 -0300 | [diff] [blame] | 225 | if ((msg->addr << 1) == 0xc2) |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 226 | au0828_write(dev, REG_202, 0x40); |
Devin Heitmueller | 62899a2 | 2009-03-15 18:48:52 -0300 | [diff] [blame] | 227 | else |
Devin Heitmueller | 32c000a | 2009-03-11 03:00:41 -0300 | [diff] [blame] | 228 | au0828_write(dev, REG_202, 0x07); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 229 | |
| 230 | /* Hardware needs 8 bit addresses */ |
| 231 | au0828_write(dev, REG_203, msg->addr << 1); |
| 232 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 233 | dprintk(4, " RECV:\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 234 | |
| 235 | /* Deal with i2c_scan */ |
| 236 | if (msg->len == 0) { |
| 237 | au0828_write(dev, REG_200, 0x20); |
| 238 | if (i2c_wait_read_ack(i2c_adap)) |
| 239 | return -EIO; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 243 | for (i = 0; i < msg->len;) { |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 244 | |
| 245 | i++; |
| 246 | |
| 247 | if (i < msg->len) |
| 248 | au0828_write(dev, REG_200, 0x60); |
| 249 | else |
| 250 | au0828_write(dev, REG_200, 0x20); |
| 251 | |
| 252 | if (!i2c_wait_read_done(i2c_adap)) |
| 253 | return -EIO; |
| 254 | |
| 255 | msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff; |
| 256 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 257 | dprintk(4, " %02x\n", msg->buf[i-1]); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 258 | } |
| 259 | if (!i2c_wait_done(i2c_adap)) |
| 260 | return -EIO; |
| 261 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 262 | dprintk(4, "\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 263 | |
| 264 | return msg->len; |
| 265 | } |
| 266 | |
| 267 | static int i2c_xfer(struct i2c_adapter *i2c_adap, |
| 268 | struct i2c_msg *msgs, int num) |
| 269 | { |
| 270 | int i, retval = 0; |
| 271 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 272 | dprintk(4, "%s(num = %d)\n", __func__, num); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 273 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 274 | for (i = 0; i < num; i++) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 275 | dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 276 | __func__, num, msgs[i].addr, msgs[i].len); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 277 | if (msgs[i].flags & I2C_M_RD) { |
| 278 | /* read */ |
| 279 | retval = i2c_readbytes(i2c_adap, &msgs[i], 0); |
| 280 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && |
| 281 | msgs[i].addr == msgs[i + 1].addr) { |
| 282 | /* write then read from same address */ |
| 283 | retval = i2c_sendbytes(i2c_adap, &msgs[i], |
| 284 | msgs[i + 1].len); |
| 285 | if (retval < 0) |
| 286 | goto err; |
| 287 | i++; |
| 288 | retval = i2c_readbytes(i2c_adap, &msgs[i], 1); |
| 289 | } else { |
| 290 | /* write */ |
| 291 | retval = i2c_sendbytes(i2c_adap, &msgs[i], 0); |
| 292 | } |
| 293 | if (retval < 0) |
| 294 | goto err; |
| 295 | } |
| 296 | return num; |
| 297 | |
| 298 | err: |
| 299 | return retval; |
| 300 | } |
| 301 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 302 | static u32 au0828_functionality(struct i2c_adapter *adap) |
| 303 | { |
| 304 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; |
| 305 | } |
| 306 | |
| 307 | static struct i2c_algorithm au0828_i2c_algo_template = { |
| 308 | .master_xfer = i2c_xfer, |
| 309 | .functionality = au0828_functionality, |
| 310 | }; |
| 311 | |
| 312 | /* ----------------------------------------------------------------------- */ |
| 313 | |
| 314 | static struct i2c_adapter au0828_i2c_adap_template = { |
| 315 | .name = DRIVER_NAME, |
| 316 | .owner = THIS_MODULE, |
| 317 | .id = I2C_HW_B_AU0828, |
| 318 | .algo = &au0828_i2c_algo_template, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | static struct i2c_client au0828_i2c_client_template = { |
| 322 | .name = "au0828 internal", |
| 323 | }; |
| 324 | |
| 325 | static char *i2c_devs[128] = { |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 326 | [0x8e >> 1] = "au8522", |
| 327 | [0xa0 >> 1] = "eeprom", |
| 328 | [0xc2 >> 1] = "tuner/xc5000", |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | static void do_i2c_scan(char *name, struct i2c_client *c) |
| 332 | { |
| 333 | unsigned char buf; |
| 334 | int i, rc; |
| 335 | |
| 336 | for (i = 0; i < 128; i++) { |
| 337 | c->addr = i; |
| 338 | rc = i2c_master_recv(c, &buf, 0); |
| 339 | if (rc < 0) |
| 340 | continue; |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 341 | printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n", |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 342 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* init + register i2c algo-bit adapter */ |
| 347 | int au0828_i2c_register(struct au0828_dev *dev) |
| 348 | { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 349 | dprintk(1, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 350 | |
| 351 | memcpy(&dev->i2c_adap, &au0828_i2c_adap_template, |
| 352 | sizeof(dev->i2c_adap)); |
| 353 | memcpy(&dev->i2c_algo, &au0828_i2c_algo_template, |
| 354 | sizeof(dev->i2c_algo)); |
| 355 | memcpy(&dev->i2c_client, &au0828_i2c_client_template, |
| 356 | sizeof(dev->i2c_client)); |
| 357 | |
| 358 | dev->i2c_adap.dev.parent = &dev->usbdev->dev; |
| 359 | |
| 360 | strlcpy(dev->i2c_adap.name, DRIVER_NAME, |
| 361 | sizeof(dev->i2c_adap.name)); |
| 362 | |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 363 | dev->i2c_adap.algo = &dev->i2c_algo; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 364 | dev->i2c_adap.algo_data = dev; |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 365 | i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 366 | i2c_add_adapter(&dev->i2c_adap); |
| 367 | |
| 368 | dev->i2c_client.adapter = &dev->i2c_adap; |
| 369 | |
| 370 | if (0 == dev->i2c_rc) { |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 371 | printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 372 | if (i2c_scan) |
| 373 | do_i2c_scan(DRIVER_NAME, &dev->i2c_client); |
| 374 | } else |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 375 | printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 376 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 377 | return dev->i2c_rc; |
| 378 | } |
| 379 | |
| 380 | int au0828_i2c_unregister(struct au0828_dev *dev) |
| 381 | { |
| 382 | i2c_del_adapter(&dev->i2c_adap); |
| 383 | return 0; |
| 384 | } |
| 385 | |