2 * drivers/sbus/char/vfc_i2c.c
4 * Driver for the Videopix Frame Grabber.
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
10 * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu)
14 /* NOTE: It seems to me that the documentation regarding the
15 pcd8584t/pcf8584 does not show the correct way to address the i2c bus.
16 Based on the information on the I2C bus itself and the remainder of
17 the Phillips docs the following algorithms appear to be correct. I am
18 fairly certain that the flowcharts in the phillips docs are wrong. */
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>
31 #include <asm/system.h>
41 #define WRITE_S1(__val) \
42 sbus_writel(__val, &dev->regs->i2c_s1)
43 #define WRITE_REG(__val) \
44 sbus_writel(__val, &dev->regs->i2c_reg)
46 #define VFC_I2C_READ (0x1)
47 #define VFC_I2C_WRITE (0x0)
50 The i2c bus controller chip on the VFC is a pcd8584t, but
51 phillips claims it doesn't exist. As far as I can tell it is
52 identical to the PCF8584 so I treat it like it is the pcf8584.
54 NOTE: The pcf8584 only cares
55 about the msb of the word you feed it
58 int vfc_pcf8584_init(struct vfc_dev
*dev
)
60 /* This will also choose register S0_OWN so we can set it. */
63 /* The pcf8584 shifts this value left one bit and uses
64 * it as its i2c bus address.
66 WRITE_REG(0x55000000);
68 /* This will set the i2c bus at the same speed sun uses,
69 * and set another magic bit.
72 WRITE_REG(0x14000000);
74 /* Enable the serial port, idle the i2c bus and set
77 WRITE_S1(CLEAR_I2C_BUS
);
82 void vfc_i2c_delay_no_busy(struct vfc_dev
*dev
, unsigned long usecs
)
84 schedule_timeout_uninterruptible(usecs_to_jiffies(usecs
));
87 void inline vfc_i2c_delay(struct vfc_dev
*dev
)
89 vfc_i2c_delay_no_busy(dev
, 100);
92 int vfc_init_i2c_bus(struct vfc_dev
*dev
)
94 WRITE_S1(ENABLE_SERIAL
| SELECT(S0
) | ACK
);
95 vfc_i2c_reset_bus(dev
);
99 int vfc_i2c_reset_bus(struct vfc_dev
*dev
)
101 VFC_I2C_DEBUG_PRINTK((KERN_DEBUG
"vfc%d: Resetting the i2c bus\n",
105 if(dev
->regs
== NULL
)
107 WRITE_S1(SEND_I2C_STOP
);
108 WRITE_S1(SEND_I2C_STOP
| ACK
);
110 WRITE_S1(CLEAR_I2C_BUS
);
111 VFC_I2C_DEBUG_PRINTK((KERN_DEBUG
"vfc%d: I2C status %x\n",
113 sbus_readl(&dev
->regs
->i2c_s1
)));
117 static int vfc_i2c_wait_for_bus(struct vfc_dev
*dev
)
121 while(!(sbus_readl(&dev
->regs
->i2c_s1
) & BB
)) {
129 static int vfc_i2c_wait_for_pin(struct vfc_dev
*dev
, int ack
)
134 while ((s1
= sbus_readl(&dev
->regs
->i2c_s1
)) & PIN
) {
139 if (ack
== VFC_I2C_ACK_CHECK
) {
146 #define SHIFT(a) ((a) << 24)
147 static int vfc_i2c_xmit_addr(struct vfc_dev
*dev
, unsigned char addr
,
152 WRITE_S1(SEND_I2C_STOP
| ACK
);
153 WRITE_S1(SELECT(S0
) | ENABLE_SERIAL
);
159 raddr
= SHIFT(((unsigned int)addr
| 0x1));
161 VFC_I2C_DEBUG_PRINTK(("vfc%d: receiving from i2c addr 0x%x\n",
162 dev
->instance
, addr
| 0x1));
165 raddr
= SHIFT((unsigned int)addr
& ~0x1);
167 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending to i2c addr 0x%x\n",
168 dev
->instance
, addr
& ~0x1));
174 WRITE_S1(SEND_I2C_START
);
176 ret
= vfc_i2c_wait_for_pin(dev
,VFC_I2C_ACK_CHECK
); /* We wait
185 printk(KERN_ERR
"vfc%d: VFC xmit addr timed out or no ack\n",
188 } else if (mode
== VFC_I2C_READ
) {
189 if ((ret
= sbus_readl(&dev
->regs
->i2c_reg
) & 0xff000000) != raddr
) {
191 "vfc%d: returned slave address "
193 dev
->instance
, raddr
, ret
);
199 static int vfc_i2c_xmit_byte(struct vfc_dev
*dev
,unsigned char *byte
)
202 u32 val
= SHIFT((unsigned int)*byte
);
206 ret
= vfc_i2c_wait_for_pin(dev
, VFC_I2C_ACK_CHECK
);
209 printk(KERN_ERR
"vfc%d: VFC xmit byte timed out or no ack\n",
213 ret
= XMIT_LAST_BYTE
;
222 static int vfc_i2c_recv_byte(struct vfc_dev
*dev
, unsigned char *byte
,
228 WRITE_REG(NEGATIVE_ACK
);
229 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending negative ack\n",
235 ret
= vfc_i2c_wait_for_pin(dev
, VFC_I2C_NO_ACK_CHECK
);
237 printk(KERN_ERR
"vfc%d: "
238 "VFC recv byte timed out\n",
241 *byte
= (sbus_readl(&dev
->regs
->i2c_reg
)) >> 24;
245 int vfc_i2c_recvbuf(struct vfc_dev
*dev
, unsigned char addr
,
246 char *buf
, int count
)
250 if(!(count
&& buf
&& dev
&& dev
->regs
) )
253 if ((ret
= vfc_i2c_wait_for_bus(dev
))) {
254 printk(KERN_ERR
"vfc%d: VFC I2C bus busy\n", dev
->instance
);
258 if ((ret
= vfc_i2c_xmit_addr(dev
, addr
, VFC_I2C_READ
))) {
259 WRITE_S1(SEND_I2C_STOP
);
268 if ((ret
= vfc_i2c_recv_byte(dev
, buf
, last
))) {
269 printk(KERN_ERR
"vfc%d: "
270 "VFC error while receiving byte\n",
272 WRITE_S1(SEND_I2C_STOP
);
277 WRITE_S1(SEND_I2C_STOP
| ACK
);
282 int vfc_i2c_sendbuf(struct vfc_dev
*dev
, unsigned char addr
,
283 char *buf
, int count
)
287 if (!(buf
&& dev
&& dev
->regs
))
290 if ((ret
= vfc_i2c_wait_for_bus(dev
))) {
291 printk(KERN_ERR
"vfc%d: VFC I2C bus busy\n", dev
->instance
);
295 if ((ret
= vfc_i2c_xmit_addr(dev
, addr
, VFC_I2C_WRITE
))) {
296 WRITE_S1(SEND_I2C_STOP
);
302 ret
= vfc_i2c_xmit_byte(dev
, buf
);
305 VFC_I2C_DEBUG_PRINTK(("vfc%d: "
306 "Receiver ended transmission with "
307 " %d bytes remaining\n",
308 dev
->instance
, count
));
315 printk(KERN_ERR
"vfc%d: "
316 "VFC error while sending byte\n", dev
->instance
);
323 WRITE_S1(SEND_I2C_STOP
| ACK
);