Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / drivers / sbus / char / vfc_i2c.c
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
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 algorithims apper to be correct.  I am
18 fairly 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 #if 0 
35 #define VFC_I2C_DEBUG
36 #endif
37
38 #include "vfc.h"
39 #include "vfc_i2c.h"
40
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)
45
46 #define VFC_I2C_READ (0x1)
47 #define VFC_I2C_WRITE (0x0)
48      
49 /****** 
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.
53   
54   NOTE: The pcf8584 only cares
55   about the msb of the word you feed it 
56 *****/
57
58 int vfc_pcf8584_init(struct vfc_dev *dev) 
59 {
60         /* This will also choose register S0_OWN so we can set it. */
61         WRITE_S1(RESET);
62
63         /* The pcf8584 shifts this value left one bit and uses
64          * it as its i2c bus address.
65          */
66         WRITE_REG(0x55000000);
67
68         /* This will set the i2c bus at the same speed sun uses,
69          * and set another magic bit.
70          */
71         WRITE_S1(SELECT(S2));
72         WRITE_REG(0x14000000);
73         
74         /* Enable the serial port, idle the i2c bus and set
75          * the data reg to s0.
76          */
77         WRITE_S1(CLEAR_I2C_BUS);
78         udelay(100);
79         return 0;
80 }
81
82 void vfc_i2c_delay_no_busy(struct vfc_dev *dev, unsigned long usecs) 
83 {
84         set_current_state(TASK_UNINTERRUPTIBLE);
85         schedule_timeout(usecs_to_jiffies(usecs));
86 }
87
88 void inline vfc_i2c_delay(struct vfc_dev *dev) 
89
90         vfc_i2c_delay_no_busy(dev, 100);
91 }
92
93 int vfc_init_i2c_bus(struct vfc_dev *dev)
94 {
95         WRITE_S1(ENABLE_SERIAL | SELECT(S0) | ACK);
96         vfc_i2c_reset_bus(dev);
97         return 0;
98 }
99
100 int vfc_i2c_reset_bus(struct vfc_dev *dev) 
101 {
102         VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: Resetting the i2c bus\n",
103                               dev->instance));
104         if(dev == NULL)
105                 return -EINVAL;
106         if(dev->regs == NULL)
107                 return -EINVAL;
108         WRITE_S1(SEND_I2C_STOP);
109         WRITE_S1(SEND_I2C_STOP | ACK);
110         vfc_i2c_delay(dev);
111         WRITE_S1(CLEAR_I2C_BUS);
112         VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: I2C status %x\n",
113                               dev->instance,
114                               sbus_readl(&dev->regs->i2c_s1)));
115         return 0;
116 }
117
118 int vfc_i2c_wait_for_bus(struct vfc_dev *dev) 
119 {
120         int timeout = 1000; 
121
122         while(!(sbus_readl(&dev->regs->i2c_s1) & BB)) {
123                 if(!(timeout--))
124                         return -ETIMEDOUT;
125                 vfc_i2c_delay(dev);
126         }
127         return 0;
128 }
129
130 int vfc_i2c_wait_for_pin(struct vfc_dev *dev, int ack)
131 {
132         int timeout = 1000; 
133         int s1;
134
135         while ((s1 = sbus_readl(&dev->regs->i2c_s1)) & PIN) {
136                 if (!(timeout--))
137                         return -ETIMEDOUT;
138                 vfc_i2c_delay(dev);
139         }
140         if (ack == VFC_I2C_ACK_CHECK) {
141                 if(s1 & LRB)
142                         return -EIO; 
143         }
144         return 0;
145 }
146
147 #define SHIFT(a) ((a) << 24)
148 int vfc_i2c_xmit_addr(struct vfc_dev *dev, unsigned char addr, char mode) 
149
150         int ret, raddr;
151 #if 1
152         WRITE_S1(SEND_I2C_STOP | ACK);
153         WRITE_S1(SELECT(S0) | ENABLE_SERIAL);
154         vfc_i2c_delay(dev);
155 #endif
156
157         switch(mode) {
158         case VFC_I2C_READ:
159                 raddr = SHIFT(((unsigned int)addr | 0x1));
160                 WRITE_REG(raddr);
161                 VFC_I2C_DEBUG_PRINTK(("vfc%d: receiving from i2c addr 0x%x\n",
162                                       dev->instance, addr | 0x1));
163                 break;
164         case VFC_I2C_WRITE:
165                 raddr = SHIFT((unsigned int)addr & ~0x1);
166                 WRITE_REG(raddr);
167                 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending to i2c addr 0x%x\n",
168                                       dev->instance, addr & ~0x1));
169                 break;
170         default:
171                 return -EINVAL;
172         };
173
174         WRITE_S1(SEND_I2C_START);
175         vfc_i2c_delay(dev);
176         ret = vfc_i2c_wait_for_pin(dev,VFC_I2C_ACK_CHECK); /* We wait
177                                                               for the
178                                                               i2c send
179                                                               to finish
180                                                               here but
181                                                               Sun
182                                                               doesn't,
183                                                               hmm */
184         if (ret) {
185                 printk(KERN_ERR "vfc%d: VFC xmit addr timed out or no ack\n",
186                        dev->instance);
187                 return ret;
188         } else if (mode == VFC_I2C_READ) {
189                 if ((ret = sbus_readl(&dev->regs->i2c_reg) & 0xff000000) != raddr) {
190                         printk(KERN_WARNING 
191                                "vfc%d: returned slave address "
192                                "mismatch(%x,%x)\n",
193                                dev->instance, raddr, ret);
194                 }
195         }       
196         return 0;
197 }
198
199 int vfc_i2c_xmit_byte(struct vfc_dev *dev,unsigned char *byte) 
200 {
201         int ret;
202         u32 val = SHIFT((unsigned int)*byte);
203
204         WRITE_REG(val);
205
206         ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_ACK_CHECK); 
207         switch(ret) {
208         case -ETIMEDOUT: 
209                 printk(KERN_ERR "vfc%d: VFC xmit byte timed out or no ack\n",
210                        dev->instance);
211                 break;
212         case -EIO:
213                 ret = XMIT_LAST_BYTE;
214                 break;
215         default:
216                 break;
217         };
218
219         return ret;
220 }
221
222 int vfc_i2c_recv_byte(struct vfc_dev *dev, unsigned char *byte, int last) 
223 {
224         int ret;
225
226         if (last) {
227                 WRITE_REG(NEGATIVE_ACK);
228                 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending negative ack\n",
229                                       dev->instance));
230         } else {
231                 WRITE_S1(ACK);
232         }
233
234         ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_NO_ACK_CHECK);
235         if(ret) {
236                 printk(KERN_ERR "vfc%d: "
237                        "VFC recv byte timed out\n",
238                        dev->instance);
239         }
240         *byte = (sbus_readl(&dev->regs->i2c_reg)) >> 24;
241         return ret;
242 }
243
244 int vfc_i2c_recvbuf(struct vfc_dev *dev, unsigned char addr,
245                     char *buf, int count)
246 {
247         int ret, last;
248
249         if(!(count && buf && dev && dev->regs) )
250                 return -EINVAL;
251
252         if ((ret = vfc_i2c_wait_for_bus(dev))) {
253                 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance);
254                 return ret;
255         }
256
257         if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_READ))) {
258                 WRITE_S1(SEND_I2C_STOP);
259                 vfc_i2c_delay(dev);
260                 return ret;
261         }
262         
263         last = 0;
264         while (count--) {
265                 if (!count)
266                         last = 1;
267                 if ((ret = vfc_i2c_recv_byte(dev, buf, last))) {
268                         printk(KERN_ERR "vfc%d: "
269                                "VFC error while receiving byte\n",
270                                dev->instance);
271                         WRITE_S1(SEND_I2C_STOP);
272                         ret = -EINVAL;
273                 }
274                 buf++;
275         }
276         WRITE_S1(SEND_I2C_STOP | ACK);
277         vfc_i2c_delay(dev);
278         return ret;
279 }
280
281 int vfc_i2c_sendbuf(struct vfc_dev *dev, unsigned char addr, 
282                     char *buf, int count) 
283 {
284         int ret;
285         
286         if (!(buf && dev && dev->regs))
287                 return -EINVAL;
288         
289         if ((ret = vfc_i2c_wait_for_bus(dev))) {
290                 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance);
291                 return ret;
292         }
293         
294         if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_WRITE))) {
295                 WRITE_S1(SEND_I2C_STOP);
296                 vfc_i2c_delay(dev);
297                 return ret;
298         }
299         
300         while(count--) {
301                 ret = vfc_i2c_xmit_byte(dev, buf);
302                 switch(ret) {
303                 case XMIT_LAST_BYTE:
304                         VFC_I2C_DEBUG_PRINTK(("vfc%d: "
305                                               "Receiver ended transmission with "
306                                               " %d bytes remaining\n",
307                                               dev->instance, count));
308                         ret = 0;
309                         goto done;
310                         break;
311                 case 0:
312                         break;
313                 default:
314                         printk(KERN_ERR "vfc%d: "
315                                "VFC error while sending byte\n", dev->instance);
316                         break;
317                 };
318
319                 buf++;
320         }
321 done:
322         WRITE_S1(SEND_I2C_STOP | ACK);
323         vfc_i2c_delay(dev);
324         return ret;
325 }
326
327
328
329
330
331
332
333
334