1/* 2 * Driver for the Auvitek AU0828 USB bridge 3 * 4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> 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> 26#include <linux/io.h> 27 28#include "au0828.h" 29 30#include <media/v4l2-common.h> 31 32static int i2c_scan; 33module_param(i2c_scan, int, 0444); 34MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); 35 36#define I2C_WAIT_DELAY 512 37#define I2C_WAIT_RETRY 64 38 39static 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, AU0828_I2C_STATUS_201) & 43 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1; 44} 45 46static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap) 47{ 48 struct au0828_dev *dev = i2c_adap->algo_data; 49 return au0828_read(dev, AU0828_I2C_STATUS_201) & 50 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1; 51} 52 53static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap) 54{ 55 int count; 56 57 for (count = 0; count < I2C_WAIT_RETRY; count++) { 58 if (!i2c_slave_did_read_ack(i2c_adap)) 59 break; 60 udelay(I2C_WAIT_DELAY); 61 } 62 63 if (I2C_WAIT_RETRY == count) 64 return 0; 65 66 return 1; 67} 68 69static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap) 70{ 71 struct au0828_dev *dev = i2c_adap->algo_data; 72 return au0828_read(dev, AU0828_I2C_STATUS_201) & 73 AU0828_I2C_STATUS_READ_DONE ? 0 : 1; 74} 75 76static int i2c_wait_read_done(struct i2c_adapter *i2c_adap) 77{ 78 int count; 79 80 for (count = 0; count < I2C_WAIT_RETRY; count++) { 81 if (!i2c_is_read_busy(i2c_adap)) 82 break; 83 udelay(I2C_WAIT_DELAY); 84 } 85 86 if (I2C_WAIT_RETRY == count) 87 return 0; 88 89 return 1; 90} 91 92static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap) 93{ 94 struct au0828_dev *dev = i2c_adap->algo_data; 95 return au0828_read(dev, AU0828_I2C_STATUS_201) & 96 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0; 97} 98 99static int i2c_wait_write_done(struct i2c_adapter *i2c_adap) 100{ 101 int count; 102 103 for (count = 0; count < I2C_WAIT_RETRY; count++) { 104 if (i2c_is_write_done(i2c_adap)) 105 break; 106 udelay(I2C_WAIT_DELAY); 107 } 108 109 if (I2C_WAIT_RETRY == count) 110 return 0; 111 112 return 1; 113} 114 115static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) 116{ 117 struct au0828_dev *dev = i2c_adap->algo_data; 118 return au0828_read(dev, AU0828_I2C_STATUS_201) & 119 AU0828_I2C_STATUS_BUSY ? 1 : 0; 120} 121 122static int i2c_wait_done(struct i2c_adapter *i2c_adap) 123{ 124 int count; 125 126 for (count = 0; count < I2C_WAIT_RETRY; count++) { 127 if (!i2c_is_busy(i2c_adap)) 128 break; 129 udelay(I2C_WAIT_DELAY); 130 } 131 132 if (I2C_WAIT_RETRY == count) 133 return 0; 134 135 return 1; 136} 137 138static int i2c_sendbytes(struct i2c_adapter *i2c_adap, 139 const struct i2c_msg *msg, int joined_rlen) 140{ 141 int i, strobe = 0; 142 struct au0828_dev *dev = i2c_adap->algo_data; 143 144 dprintk(4, "%s()\n", __func__); 145 146 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01); 147 148 /* Set the I2C clock */ 149 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, 150 dev->board.i2c_clk_divider); 151 152 /* Hardware needs 8 bit addresses */ 153 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1); 154 155 dprintk(4, "SEND: %02x\n", msg->addr); 156 157 /* Deal with i2c_scan */ 158 if (msg->len == 0) { 159 /* The analog tuner detection code makes use of the SMBUS_QUICK 160 message (which involves a zero length i2c write). To avoid 161 checking the status register when we didn't strobe out any 162 actual bytes to the bus, just do a read check. This is 163 consistent with how I saw i2c device checking done in the 164 USB trace of the Windows driver */ 165 au0828_write(dev, AU0828_I2C_TRIGGER_200, 166 AU0828_I2C_TRIGGER_READ); 167 168 if (!i2c_wait_done(i2c_adap)) 169 return -EIO; 170 171 if (i2c_wait_read_ack(i2c_adap)) 172 return -EIO; 173 174 return 0; 175 } 176 177 for (i = 0; i < msg->len;) { 178 179 dprintk(4, " %02x\n", msg->buf[i]); 180 181 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]); 182 183 strobe++; 184 i++; 185 186 if ((strobe >= 4) || (i >= msg->len)) { 187 188 /* Strobe the byte into the bus */ 189 if (i < msg->len) 190 au0828_write(dev, AU0828_I2C_TRIGGER_200, 191 AU0828_I2C_TRIGGER_WRITE | 192 AU0828_I2C_TRIGGER_HOLD); 193 else 194 au0828_write(dev, AU0828_I2C_TRIGGER_200, 195 AU0828_I2C_TRIGGER_WRITE); 196 197 /* Reset strobe trigger */ 198 strobe = 0; 199 200 if (!i2c_wait_write_done(i2c_adap)) 201 return -EIO; 202 203 } 204 205 } 206 if (!i2c_wait_done(i2c_adap)) 207 return -EIO; 208 209 dprintk(4, "\n"); 210 211 return msg->len; 212} 213 214static int i2c_readbytes(struct i2c_adapter *i2c_adap, 215 const struct i2c_msg *msg, int joined) 216{ 217 struct au0828_dev *dev = i2c_adap->algo_data; 218 int i; 219 220 dprintk(4, "%s()\n", __func__); 221 222 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01); 223 224 /* Set the I2C clock */ 225 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, 226 dev->board.i2c_clk_divider); 227 228 /* Hardware needs 8 bit addresses */ 229 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1); 230 231 dprintk(4, " RECV:\n"); 232 233 /* Deal with i2c_scan */ 234 if (msg->len == 0) { 235 au0828_write(dev, AU0828_I2C_TRIGGER_200, 236 AU0828_I2C_TRIGGER_READ); 237 238 if (i2c_wait_read_ack(i2c_adap)) 239 return -EIO; 240 return 0; 241 } 242 243 for (i = 0; i < msg->len;) { 244 245 i++; 246 247 if (i < msg->len) 248 au0828_write(dev, AU0828_I2C_TRIGGER_200, 249 AU0828_I2C_TRIGGER_READ | 250 AU0828_I2C_TRIGGER_HOLD); 251 else 252 au0828_write(dev, AU0828_I2C_TRIGGER_200, 253 AU0828_I2C_TRIGGER_READ); 254 255 if (!i2c_wait_read_done(i2c_adap)) 256 return -EIO; 257 258 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) & 259 0xff; 260 261 dprintk(4, " %02x\n", msg->buf[i-1]); 262 } 263 if (!i2c_wait_done(i2c_adap)) 264 return -EIO; 265 266 dprintk(4, "\n"); 267 268 return msg->len; 269} 270 271static int i2c_xfer(struct i2c_adapter *i2c_adap, 272 struct i2c_msg *msgs, int num) 273{ 274 int i, retval = 0; 275 276 dprintk(4, "%s(num = %d)\n", __func__, num); 277 278 for (i = 0; i < num; i++) { 279 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n", 280 __func__, num, msgs[i].addr, msgs[i].len); 281 if (msgs[i].flags & I2C_M_RD) { 282 /* read */ 283 retval = i2c_readbytes(i2c_adap, &msgs[i], 0); 284 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && 285 msgs[i].addr == msgs[i + 1].addr) { 286 /* write then read from same address */ 287 retval = i2c_sendbytes(i2c_adap, &msgs[i], 288 msgs[i + 1].len); 289 if (retval < 0) 290 goto err; 291 i++; 292 retval = i2c_readbytes(i2c_adap, &msgs[i], 1); 293 } else { 294 /* write */ 295 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0); 296 } 297 if (retval < 0) 298 goto err; 299 } 300 return num; 301 302err: 303 return retval; 304} 305 306static u32 au0828_functionality(struct i2c_adapter *adap) 307{ 308 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; 309} 310 311static struct i2c_algorithm au0828_i2c_algo_template = { 312 .master_xfer = i2c_xfer, 313 .functionality = au0828_functionality, 314}; 315 316/* ----------------------------------------------------------------------- */ 317 318static struct i2c_adapter au0828_i2c_adap_template = { 319 .name = DRIVER_NAME, 320 .owner = THIS_MODULE, 321 .algo = &au0828_i2c_algo_template, 322}; 323 324static struct i2c_client au0828_i2c_client_template = { 325 .name = "au0828 internal", 326}; 327 328static char *i2c_devs[128] = { 329 [0x8e >> 1] = "au8522", 330 [0xa0 >> 1] = "eeprom", 331 [0xc2 >> 1] = "tuner/xc5000", 332}; 333 334static void do_i2c_scan(char *name, struct i2c_client *c) 335{ 336 unsigned char buf; 337 int i, rc; 338 339 for (i = 0; i < 128; i++) { 340 c->addr = i; 341 rc = i2c_master_recv(c, &buf, 0); 342 if (rc < 0) 343 continue; 344 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n", 345 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); 346 } 347} 348 349/* init + register i2c algo-bit adapter */ 350int au0828_i2c_register(struct au0828_dev *dev) 351{ 352 dprintk(1, "%s()\n", __func__); 353 354 memcpy(&dev->i2c_adap, &au0828_i2c_adap_template, 355 sizeof(dev->i2c_adap)); 356 memcpy(&dev->i2c_algo, &au0828_i2c_algo_template, 357 sizeof(dev->i2c_algo)); 358 memcpy(&dev->i2c_client, &au0828_i2c_client_template, 359 sizeof(dev->i2c_client)); 360 361 dev->i2c_adap.dev.parent = &dev->usbdev->dev; 362 363 strlcpy(dev->i2c_adap.name, DRIVER_NAME, 364 sizeof(dev->i2c_adap.name)); 365 366 dev->i2c_adap.algo = &dev->i2c_algo; 367 dev->i2c_adap.algo_data = dev; 368 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); 369 i2c_add_adapter(&dev->i2c_adap); 370 371 dev->i2c_client.adapter = &dev->i2c_adap; 372 373 if (0 == dev->i2c_rc) { 374 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME); 375 if (i2c_scan) 376 do_i2c_scan(DRIVER_NAME, &dev->i2c_client); 377 } else 378 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME); 379 380 return dev->i2c_rc; 381} 382 383int au0828_i2c_unregister(struct au0828_dev *dev) 384{ 385 i2c_del_adapter(&dev->i2c_adap); 386 return 0; 387} 388