1/* 2 * drivers/sbus/char/vfc_i2c.c 3 * 4 * Driver for the Videopix Frame Grabber. 5 * 6 * Functions that support the Phillips i2c(I squared C) bus on the vfc 7 * Documentation for the Phillips I2C bus can be found on the 8 * phillips home page 9 * 10 * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu) 11 * 12 */ 13 14/* NOTE: It seems to me that the documentation regarding the 15pcd8584t/pcf8584 does not show the correct way to address the i2c bus. 16Based on the information on the I2C bus itself and the remainder of 17the Phillips docs the following algorithims apper to be correct. I am 18fairly certain that the flowcharts in the phillips docs are wrong. */ 19 20 21#include <linux/kernel.h> 22#include <linux/string.h> 23#include <linux/slab.h> 24#include <linux/errno.h> 25#include <linux/sched.h> 26#include <linux/wait.h> 27#include <linux/delay.h> 28#include <asm/openprom.h> 29#include <asm/oplib.h> 30#include <asm/io.h> 31#include <asm/system.h> 32#include <asm/sbus.h> 33 34 35#include "vfc.h" 36#include "vfc_i2c.h" 37 38#define WRITE_S1(__val) \ 39 sbus_writel(__val, &dev->regs->i2c_s1) 40#define WRITE_REG(__val) \ 41 sbus_writel(__val, &dev->regs->i2c_reg) 42 43#define VFC_I2C_READ (0x1) 44#define VFC_I2C_WRITE (0x0) 45 46/****** 47 The i2c bus controller chip on the VFC is a pcd8584t, but 48 phillips claims it doesn't exist. As far as I can tell it is 49 identical to the PCF8584 so I treat it like it is the pcf8584. 50 51 NOTE: The pcf8584 only cares 52 about the msb of the word you feed it 53*****/ 54 55int vfc_pcf8584_init(struct vfc_dev *dev) 56{ 57 /* This will also choose register S0_OWN so we can set it. */ 58 WRITE_S1(RESET); 59 60 /* The pcf8584 shifts this value left one bit and uses 61 * it as its i2c bus address. 62 */ 63 WRITE_REG(0x55000000); 64 65 /* This will set the i2c bus at the same speed sun uses, 66 * and set another magic bit. 67 */ 68 WRITE_S1(SELECT(S2)); 69 WRITE_REG(0x14000000); 70 71 /* Enable the serial port, idle the i2c bus and set 72 * the data reg to s0. 73 */ 74 WRITE_S1(CLEAR_I2C_BUS); 75 udelay(100); 76 return 0; 77} 78 79void vfc_i2c_delay_wakeup(struct vfc_dev *dev) 80{ 81 /* Used to profile code and eliminate too many delays */ 82 VFC_I2C_DEBUG_PRINTK(("vfc%d: Delaying\n", dev->instance)); 83 wake_up(&dev->poll_wait); 84} 85 86void vfc_i2c_delay_no_busy(struct vfc_dev *dev, unsigned long usecs) 87{ 88 init_timer(&dev->poll_timer); 89 dev->poll_timer.expires = jiffies + 90 ((unsigned long)usecs*(HZ))/1000000; 91 dev->poll_timer.data=(unsigned long)dev; 92 dev->poll_timer.function=(void *)(unsigned long)vfc_i2c_delay_wakeup; 93 add_timer(&dev->poll_timer); 94 sleep_on(&dev->poll_wait); 95 del_timer(&dev->poll_timer); 96} 97 98void inline vfc_i2c_delay(struct vfc_dev *dev) 99{ 100 vfc_i2c_delay_no_busy(dev, 100); 101} 102 103int vfc_init_i2c_bus(struct vfc_dev *dev) 104{ 105 WRITE_S1(ENABLE_SERIAL | SELECT(S0) | ACK); 106 vfc_i2c_reset_bus(dev); 107 return 0; 108} 109 110int vfc_i2c_reset_bus(struct vfc_dev *dev) 111{ 112 VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: Resetting the i2c bus\n", 113 dev->instance)); 114 if(dev == NULL) 115 return -EINVAL; 116 if(dev->regs == NULL) 117 return -EINVAL; 118 WRITE_S1(SEND_I2C_STOP); 119 WRITE_S1(SEND_I2C_STOP | ACK); 120 vfc_i2c_delay(dev); 121 WRITE_S1(CLEAR_I2C_BUS); 122 VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: I2C status %x\n", 123 dev->instance, 124 sbus_readl(&dev->regs->i2c_s1))); 125 return 0; 126} 127 128int vfc_i2c_wait_for_bus(struct vfc_dev *dev) 129{ 130 int timeout = 1000; 131 132 while(!(sbus_readl(&dev->regs->i2c_s1) & BB)) { 133 if(!(timeout--)) 134 return -ETIMEDOUT; 135 vfc_i2c_delay(dev); 136 } 137 return 0; 138} 139 140int vfc_i2c_wait_for_pin(struct vfc_dev *dev, int ack) 141{ 142 int timeout = 1000; 143 int s1; 144 145 while ((s1 = sbus_readl(&dev->regs->i2c_s1)) & PIN) { 146 if (!(timeout--)) 147 return -ETIMEDOUT; 148 vfc_i2c_delay(dev); 149 } 150 if (ack == VFC_I2C_ACK_CHECK) { 151 if(s1 & LRB) 152 return -EIO; 153 } 154 return 0; 155} 156 157#define SHIFT(a) ((a) << 24) 158int vfc_i2c_xmit_addr(struct vfc_dev *dev, unsigned char addr, char mode) 159{ 160 int ret, raddr; 161 WRITE_S1(SEND_I2C_STOP | ACK); 162 WRITE_S1(SELECT(S0) | ENABLE_SERIAL); 163 vfc_i2c_delay(dev); 164 165 switch(mode) { 166 case VFC_I2C_READ: 167 raddr = SHIFT(((unsigned int)addr | 0x1)); 168 WRITE_REG(raddr); 169 VFC_I2C_DEBUG_PRINTK(("vfc%d: receiving from i2c addr 0x%x\n", 170 dev->instance, addr | 0x1)); 171 break; 172 case VFC_I2C_WRITE: 173 raddr = SHIFT((unsigned int)addr & ~0x1); 174 WRITE_REG(raddr); 175 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending to i2c addr 0x%x\n", 176 dev->instance, addr & ~0x1)); 177 break; 178 default: 179 return -EINVAL; 180 }; 181 182 WRITE_S1(SEND_I2C_START); 183 vfc_i2c_delay(dev); 184 ret = vfc_i2c_wait_for_pin(dev,VFC_I2C_ACK_CHECK); /* We wait 185 for the 186 i2c send 187 to finish 188 here but 189 Sun 190 doesn't, 191 hmm */ 192 if (ret) { 193 printk(KERN_ERR "vfc%d: VFC xmit addr timed out or no ack\n", 194 dev->instance); 195 return ret; 196 } else if (mode == VFC_I2C_READ) { 197 if ((ret = sbus_readl(&dev->regs->i2c_reg) & 0xff000000) != raddr) { 198 printk(KERN_WARNING 199 "vfc%d: returned slave address " 200 "mismatch(%x,%x)\n", 201 dev->instance, raddr, ret); 202 } 203 } 204 return 0; 205} 206 207int vfc_i2c_xmit_byte(struct vfc_dev *dev,unsigned char *byte) 208{ 209 int ret; 210 u32 val = SHIFT((unsigned int)*byte); 211 212 WRITE_REG(val); 213 214 ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_ACK_CHECK); 215 switch(ret) { 216 case -ETIMEDOUT: 217 printk(KERN_ERR "vfc%d: VFC xmit byte timed out or no ack\n", 218 dev->instance); 219 break; 220 case -EIO: 221 ret = XMIT_LAST_BYTE; 222 break; 223 default: 224 break; 225 }; 226 227 return ret; 228} 229 230int vfc_i2c_recv_byte(struct vfc_dev *dev, unsigned char *byte, int last) 231{ 232 int ret; 233 234 if (last) { 235 WRITE_REG(NEGATIVE_ACK); 236 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending negative ack\n", 237 dev->instance)); 238 } else { 239 WRITE_S1(ACK); 240 } 241 242 ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_NO_ACK_CHECK); 243 if(ret) { 244 printk(KERN_ERR "vfc%d: " 245 "VFC recv byte timed out\n", 246 dev->instance); 247 } 248 *byte = (sbus_readl(&dev->regs->i2c_reg)) >> 24; 249 return ret; 250} 251 252int vfc_i2c_recvbuf(struct vfc_dev *dev, unsigned char addr, 253 char *buf, int count) 254{ 255 int ret, last; 256 257 if(!(count && buf && dev && dev->regs) ) 258 return -EINVAL; 259 260 if ((ret = vfc_i2c_wait_for_bus(dev))) { 261 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance); 262 return ret; 263 } 264 265 if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_READ))) { 266 WRITE_S1(SEND_I2C_STOP); 267 vfc_i2c_delay(dev); 268 return ret; 269 } 270 271 last = 0; 272 while (count--) { 273 if (!count) 274 last = 1; 275 if ((ret = vfc_i2c_recv_byte(dev, buf, last))) { 276 printk(KERN_ERR "vfc%d: " 277 "VFC error while receiving byte\n", 278 dev->instance); 279 WRITE_S1(SEND_I2C_STOP); 280 ret = -EINVAL; 281 } 282 buf++; 283 } 284 WRITE_S1(SEND_I2C_STOP | ACK); 285 vfc_i2c_delay(dev); 286 return ret; 287} 288 289int vfc_i2c_sendbuf(struct vfc_dev *dev, unsigned char addr, 290 char *buf, int count) 291{ 292 int ret; 293 294 if (!(buf && dev && dev->regs)) 295 return -EINVAL; 296 297 if ((ret = vfc_i2c_wait_for_bus(dev))) { 298 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance); 299 return ret; 300 } 301 302 if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_WRITE))) { 303 WRITE_S1(SEND_I2C_STOP); 304 vfc_i2c_delay(dev); 305 return ret; 306 } 307 308 while(count--) { 309 ret = vfc_i2c_xmit_byte(dev, buf); 310 switch(ret) { 311 case XMIT_LAST_BYTE: 312 VFC_I2C_DEBUG_PRINTK(("vfc%d: " 313 "Receiver ended transmission with " 314 " %d bytes remaining\n", 315 dev->instance, count)); 316 ret = 0; 317 goto done; 318 break; 319 case 0: 320 break; 321 default: 322 printk(KERN_ERR "vfc%d: " 323 "VFC error while sending byte\n", dev->instance); 324 break; 325 }; 326 327 buf++; 328 } 329done: 330 WRITE_S1(SEND_I2C_STOP | ACK); 331 vfc_i2c_delay(dev); 332 return ret; 333} 334 335 336 337 338 339 340 341 342 343