/spare/repo/netdev-2.6 branch 'ieee80211'
[pandora-kernel.git] / 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_wakeup(struct vfc_dev *dev) 
83 {
84         /* Used to profile code and eliminate too many delays */
85         VFC_I2C_DEBUG_PRINTK(("vfc%d: Delaying\n", dev->instance));
86         wake_up(&dev->poll_wait);
87 }
88
89 void vfc_i2c_delay_no_busy(struct vfc_dev *dev, unsigned long usecs) 
90 {
91         DEFINE_WAIT(wait);
92         init_timer(&dev->poll_timer);
93         dev->poll_timer.expires = jiffies + usecs_to_jiffies(usecs);
94         dev->poll_timer.data=(unsigned long)dev;
95         dev->poll_timer.function=(void *)(unsigned long)vfc_i2c_delay_wakeup;
96         add_timer(&dev->poll_timer);
97         prepare_to_wait(&dev->poll_wait, &wait, TASK_UNINTERRUPTIBLE);
98         schedule();
99         del_timer(&dev->poll_timer);
100         finish_wait(&dev->poll_wait, &wait);
101 }
102
103 void inline vfc_i2c_delay(struct vfc_dev *dev) 
104
105         vfc_i2c_delay_no_busy(dev, 100);
106 }
107
108 int vfc_init_i2c_bus(struct vfc_dev *dev)
109 {
110         WRITE_S1(ENABLE_SERIAL | SELECT(S0) | ACK);
111         vfc_i2c_reset_bus(dev);
112         return 0;
113 }
114
115 int vfc_i2c_reset_bus(struct vfc_dev *dev) 
116 {
117         VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: Resetting the i2c bus\n",
118                               dev->instance));
119         if(dev == NULL)
120                 return -EINVAL;
121         if(dev->regs == NULL)
122                 return -EINVAL;
123         WRITE_S1(SEND_I2C_STOP);
124         WRITE_S1(SEND_I2C_STOP | ACK);
125         vfc_i2c_delay(dev);
126         WRITE_S1(CLEAR_I2C_BUS);
127         VFC_I2C_DEBUG_PRINTK((KERN_DEBUG "vfc%d: I2C status %x\n",
128                               dev->instance,
129                               sbus_readl(&dev->regs->i2c_s1)));
130         return 0;
131 }
132
133 int vfc_i2c_wait_for_bus(struct vfc_dev *dev) 
134 {
135         int timeout = 1000; 
136
137         while(!(sbus_readl(&dev->regs->i2c_s1) & BB)) {
138                 if(!(timeout--))
139                         return -ETIMEDOUT;
140                 vfc_i2c_delay(dev);
141         }
142         return 0;
143 }
144
145 int vfc_i2c_wait_for_pin(struct vfc_dev *dev, int ack)
146 {
147         int timeout = 1000; 
148         int s1;
149
150         while ((s1 = sbus_readl(&dev->regs->i2c_s1)) & PIN) {
151                 if (!(timeout--))
152                         return -ETIMEDOUT;
153                 vfc_i2c_delay(dev);
154         }
155         if (ack == VFC_I2C_ACK_CHECK) {
156                 if(s1 & LRB)
157                         return -EIO; 
158         }
159         return 0;
160 }
161
162 #define SHIFT(a) ((a) << 24)
163 int vfc_i2c_xmit_addr(struct vfc_dev *dev, unsigned char addr, char mode) 
164
165         int ret, raddr;
166 #if 1
167         WRITE_S1(SEND_I2C_STOP | ACK);
168         WRITE_S1(SELECT(S0) | ENABLE_SERIAL);
169         vfc_i2c_delay(dev);
170 #endif
171
172         switch(mode) {
173         case VFC_I2C_READ:
174                 raddr = SHIFT(((unsigned int)addr | 0x1));
175                 WRITE_REG(raddr);
176                 VFC_I2C_DEBUG_PRINTK(("vfc%d: receiving from i2c addr 0x%x\n",
177                                       dev->instance, addr | 0x1));
178                 break;
179         case VFC_I2C_WRITE:
180                 raddr = SHIFT((unsigned int)addr & ~0x1);
181                 WRITE_REG(raddr);
182                 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending to i2c addr 0x%x\n",
183                                       dev->instance, addr & ~0x1));
184                 break;
185         default:
186                 return -EINVAL;
187         };
188
189         WRITE_S1(SEND_I2C_START);
190         vfc_i2c_delay(dev);
191         ret = vfc_i2c_wait_for_pin(dev,VFC_I2C_ACK_CHECK); /* We wait
192                                                               for the
193                                                               i2c send
194                                                               to finish
195                                                               here but
196                                                               Sun
197                                                               doesn't,
198                                                               hmm */
199         if (ret) {
200                 printk(KERN_ERR "vfc%d: VFC xmit addr timed out or no ack\n",
201                        dev->instance);
202                 return ret;
203         } else if (mode == VFC_I2C_READ) {
204                 if ((ret = sbus_readl(&dev->regs->i2c_reg) & 0xff000000) != raddr) {
205                         printk(KERN_WARNING 
206                                "vfc%d: returned slave address "
207                                "mismatch(%x,%x)\n",
208                                dev->instance, raddr, ret);
209                 }
210         }       
211         return 0;
212 }
213
214 int vfc_i2c_xmit_byte(struct vfc_dev *dev,unsigned char *byte) 
215 {
216         int ret;
217         u32 val = SHIFT((unsigned int)*byte);
218
219         WRITE_REG(val);
220
221         ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_ACK_CHECK); 
222         switch(ret) {
223         case -ETIMEDOUT: 
224                 printk(KERN_ERR "vfc%d: VFC xmit byte timed out or no ack\n",
225                        dev->instance);
226                 break;
227         case -EIO:
228                 ret = XMIT_LAST_BYTE;
229                 break;
230         default:
231                 break;
232         };
233
234         return ret;
235 }
236
237 int vfc_i2c_recv_byte(struct vfc_dev *dev, unsigned char *byte, int last) 
238 {
239         int ret;
240
241         if (last) {
242                 WRITE_REG(NEGATIVE_ACK);
243                 VFC_I2C_DEBUG_PRINTK(("vfc%d: sending negative ack\n",
244                                       dev->instance));
245         } else {
246                 WRITE_S1(ACK);
247         }
248
249         ret = vfc_i2c_wait_for_pin(dev, VFC_I2C_NO_ACK_CHECK);
250         if(ret) {
251                 printk(KERN_ERR "vfc%d: "
252                        "VFC recv byte timed out\n",
253                        dev->instance);
254         }
255         *byte = (sbus_readl(&dev->regs->i2c_reg)) >> 24;
256         return ret;
257 }
258
259 int vfc_i2c_recvbuf(struct vfc_dev *dev, unsigned char addr,
260                     char *buf, int count)
261 {
262         int ret, last;
263
264         if(!(count && buf && dev && dev->regs) )
265                 return -EINVAL;
266
267         if ((ret = vfc_i2c_wait_for_bus(dev))) {
268                 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance);
269                 return ret;
270         }
271
272         if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_READ))) {
273                 WRITE_S1(SEND_I2C_STOP);
274                 vfc_i2c_delay(dev);
275                 return ret;
276         }
277         
278         last = 0;
279         while (count--) {
280                 if (!count)
281                         last = 1;
282                 if ((ret = vfc_i2c_recv_byte(dev, buf, last))) {
283                         printk(KERN_ERR "vfc%d: "
284                                "VFC error while receiving byte\n",
285                                dev->instance);
286                         WRITE_S1(SEND_I2C_STOP);
287                         ret = -EINVAL;
288                 }
289                 buf++;
290         }
291         WRITE_S1(SEND_I2C_STOP | ACK);
292         vfc_i2c_delay(dev);
293         return ret;
294 }
295
296 int vfc_i2c_sendbuf(struct vfc_dev *dev, unsigned char addr, 
297                     char *buf, int count) 
298 {
299         int ret;
300         
301         if (!(buf && dev && dev->regs))
302                 return -EINVAL;
303         
304         if ((ret = vfc_i2c_wait_for_bus(dev))) {
305                 printk(KERN_ERR "vfc%d: VFC I2C bus busy\n", dev->instance);
306                 return ret;
307         }
308         
309         if ((ret = vfc_i2c_xmit_addr(dev, addr, VFC_I2C_WRITE))) {
310                 WRITE_S1(SEND_I2C_STOP);
311                 vfc_i2c_delay(dev);
312                 return ret;
313         }
314         
315         while(count--) {
316                 ret = vfc_i2c_xmit_byte(dev, buf);
317                 switch(ret) {
318                 case XMIT_LAST_BYTE:
319                         VFC_I2C_DEBUG_PRINTK(("vfc%d: "
320                                               "Receiver ended transmission with "
321                                               " %d bytes remaining\n",
322                                               dev->instance, count));
323                         ret = 0;
324                         goto done;
325                         break;
326                 case 0:
327                         break;
328                 default:
329                         printk(KERN_ERR "vfc%d: "
330                                "VFC error while sending byte\n", dev->instance);
331                         break;
332                 };
333
334                 buf++;
335         }
336 done:
337         WRITE_S1(SEND_I2C_STOP | ACK);
338         vfc_i2c_delay(dev);
339         return ret;
340 }
341
342
343
344
345
346
347
348
349