ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / common / saa7146_i2c.c
1 #include <linux/version.h>
2 #include <media/saa7146_vv.h>
3
4 /* helper function */
5 static void my_wait(struct saa7146_dev *dev, long ms)
6 {
7         set_current_state(TASK_INTERRUPTIBLE);
8         schedule_timeout((((ms+10)/10)*HZ)/1000);
9 }
10
11 u32 saa7146_i2c_func(struct i2c_adapter *adapter)
12 {
13 //fm    DEB_I2C(("'%s'.\n", adapter->name));
14
15         return    I2C_FUNC_I2C
16                 | I2C_FUNC_SMBUS_QUICK
17                 | I2C_FUNC_SMBUS_READ_BYTE      | I2C_FUNC_SMBUS_WRITE_BYTE
18                 | I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
19 }
20
21 /* this function returns the status-register of our i2c-device */
22 static inline u32 saa7146_i2c_status(struct saa7146_dev *dev) 
23 {
24         u32 iicsta = saa7146_read(dev, I2C_STATUS);
25 /*
26         DEB_I2C(("status: 0x%08x\n",iicsta));
27 */
28         return iicsta;
29 }
30
31 /* this function runs through the i2c-messages and prepares the data to be
32    sent through the saa7146. have a look at the specifications p. 122 ff 
33    to understand this. it returns the number of u32s to send, or -1
34    in case of an error. */
35 static int saa7146_i2c_msg_prepare(const struct i2c_msg m[], int num, u32 *op)
36 {
37         int h1, h2;
38         int i, j, addr;
39         int mem = 0, op_count = 0;
40
41         /* first determine size of needed memory */
42         for(i = 0; i < num; i++) {
43                 mem += m[i].len + 1;
44         }
45
46         /* worst case: we need one u32 for three bytes to be send
47            plus one extra byte to address the device */
48         mem = 1 + ((mem-1) / 3);
49
50         /* we assume that op points to a memory of at least SAA7146_I2C_MEM bytes
51            size. if we exceed this limit... */
52         if ( (4*mem) > SAA7146_I2C_MEM ) {
53 //fm            DEB_I2C(("cannot prepare i2c-message.\n"));
54                 return -ENOMEM;
55         }
56
57         /* be careful: clear out the i2c-mem first */
58         memset(op,0,sizeof(u32)*mem);
59
60         /* loop through all messages */
61         for(i = 0; i < num; i++) {
62
63                 /* insert the address of the i2c-slave.
64                    note: we get 7 bit i2c-addresses,
65                    so we have to perform a translation */
66                 addr = (m[i].addr*2) + ( (0 != (m[i].flags & I2C_M_RD)) ? 1 : 0); 
67                 h1 = op_count/3; h2 = op_count%3;
68                 op[h1] |= (         (u8)addr << ((3-h2)*8));
69                 op[h1] |= (SAA7146_I2C_START << ((3-h2)*2));
70                 op_count++;
71
72                 /* loop through all bytes of message i */
73                 for(j = 0; j < m[i].len; j++) {
74                         /* insert the data bytes */
75                         h1 = op_count/3; h2 = op_count%3;
76                         op[h1] |= ( (u32)((u8)m[i].buf[j]) << ((3-h2)*8));
77                         op[h1] |= (       SAA7146_I2C_CONT << ((3-h2)*2));
78                         op_count++;
79                 }
80                 
81         }                       
82
83         /* have a look at the last byte inserted:
84           if it was: ...CONT change it to ...STOP */
85         h1 = (op_count-1)/3; h2 = (op_count-1)%3;
86         if ( SAA7146_I2C_CONT == (0x3 & (op[h1] >> ((3-h2)*2))) ) {
87                 op[h1] &= ~(0x2 << ((3-h2)*2));
88                 op[h1] |= (SAA7146_I2C_STOP << ((3-h2)*2));
89         }
90
91         /* return the number of u32s to send */ 
92         return mem;
93 }
94
95 /* this functions loops through all i2c-messages. normally, it should determine
96    which bytes were read through the adapter and write them back to the corresponding
97    i2c-message. but instead, we simply write back all bytes.
98    fixme: this could be improved. */
99 static int saa7146_i2c_msg_cleanup(const struct i2c_msg m[], int num, u32 *op)
100 {
101         int i, j;
102         int op_count = 0;
103
104         /* loop through all messages */
105         for(i = 0; i < num; i++) {
106
107                 op_count++;
108
109                 /* loop throgh all bytes of message i */
110                 for(j = 0; j < m[i].len; j++) {
111                         /* write back all bytes that could have been read */
112                         m[i].buf[j] = (op[op_count/3] >> ((3-(op_count%3))*8));
113                         op_count++;
114                 }
115         }
116         
117         return 0;
118 }
119
120 /* this functions resets the i2c-device and returns 0 if everything was fine, otherwise -1 */
121 static int saa7146_i2c_reset(struct saa7146_dev *dev) 
122 {
123         /* get current status */
124         u32 status = saa7146_i2c_status(dev);
125         
126         /* clear registers for sure */
127         saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
128         saa7146_write(dev, I2C_TRANSFER, 0);
129
130         /* check if any operation is still in progress */
131         if ( 0 != ( status & SAA7146_I2C_BUSY) ) {
132
133                 /* yes, kill ongoing operation */
134                 DEB_I2C(("busy_state detected.\n"));
135
136                 /* set "ABORT-OPERATION"-bit (bit 7)*/
137                 saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
138                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
139                 my_wait(dev,SAA7146_I2C_DELAY);
140
141                 /* clear all error-bits pending; this is needed because p.123, note 1 */
142                 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
143                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
144                 my_wait(dev,SAA7146_I2C_DELAY);
145         }
146
147         /* check if any error is (still) present. (this can be necessary because p.123, note 1) */
148         status = saa7146_i2c_status(dev);
149
150         if ( dev->i2c_bitrate != status ) {
151
152                 DEB_I2C(("error_state detected. status:0x%08x\n",status));
153
154                 /* Repeat the abort operation. This seems to be necessary
155                    after serious protocol errors caused by e.g. the SAA7740 */
156                 saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));
157                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
158                 my_wait(dev,SAA7146_I2C_DELAY);
159
160                 /* clear all error-bits pending */
161                 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
162                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
163                 my_wait(dev,SAA7146_I2C_DELAY);
164
165                 /* the data sheet says it might be necessary to clear the status
166                    twice after an abort */
167                 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
168                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
169                 my_wait(dev,SAA7146_I2C_DELAY);
170         }
171
172         /* if any error is still present, a fatal error has occured ... */
173         status = saa7146_i2c_status(dev);
174         if ( dev->i2c_bitrate != status ) {
175                 DEB_I2C(("fatal error. status:0x%08x\n",status));
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 /* this functions writes out the data-byte 'dword' to the i2c-device.
183    it returns 0 if ok, -1 if the transfer failed, -2 if the transfer
184    failed badly (e.g. address error) */
185 static int saa7146_i2c_writeout(struct saa7146_dev *dev, u32* dword, int short_delay)
186 {
187         u32 status = 0, mc2 = 0;
188         int trial = 0;
189         unsigned long timeout;
190
191         /* write out i2c-command */
192         DEB_I2C(("before: 0x%08x (status: 0x%08x), %d\n",*dword,saa7146_read(dev, I2C_STATUS), dev->i2c_op));
193
194         if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
195
196                 saa7146_write(dev, I2C_STATUS,   dev->i2c_bitrate);
197                 saa7146_write(dev, I2C_TRANSFER, *dword);
198
199                 dev->i2c_op = 1;
200                 IER_ENABLE(dev, MASK_16|MASK_17);
201                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
202
203                 wait_event_interruptible(dev->i2c_wq, dev->i2c_op == 0);
204                 if (signal_pending (current)) {
205                         /* a signal arrived */
206                         return -ERESTARTSYS;
207                 }
208                 status = saa7146_read(dev, I2C_STATUS);
209         } else {
210                 saa7146_write(dev, I2C_STATUS,   dev->i2c_bitrate);
211                 saa7146_write(dev, I2C_TRANSFER, *dword);
212                 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
213
214                 /* do not poll for i2c-status before upload is complete */
215                 timeout = jiffies + HZ/100 + 1; /* 10ms */
216                 while(1) {
217                         mc2 = (saa7146_read(dev, MC2) & 0x1);
218                         if( 0 != mc2 ) {
219                                 break;
220                         }
221                         if (time_after(jiffies,timeout)) {
222                                 printk(KERN_WARNING "saa7146_i2c_writeout: timed out waiting for MC2\n");
223                                 return -EIO;
224                         }
225                 }
226                 /* wait until we get a transfer done or error */
227                 timeout = jiffies + HZ/100 + 1; /* 10ms */
228                 while(1) {
229                         /**
230                          *  first read usually delivers bogus results...
231                          */
232                         saa7146_i2c_status(dev);
233                         status = saa7146_i2c_status(dev);
234                         if ((status & 0x3) != 1)
235                                 break;
236                         if (time_after(jiffies,timeout)) {
237                                 /* this is normal when probing the bus
238                                  * (no answer from nonexisistant device...)
239                                  */
240                                 DEB_I2C(("saa7146_i2c_writeout: timed out waiting for end of xfer\n"));
241                                 return -EIO;
242                         }
243                         if ((++trial < 20) && short_delay)
244                                 udelay(10);
245                         else
246                         my_wait(dev,1);
247                 }
248         }
249
250         /* give a detailed status report */
251         if ( 0 != (status & SAA7146_I2C_ERR)) {
252
253                 if( 0 != (status & SAA7146_I2C_SPERR) ) {
254                         DEB_I2C(("error due to invalid start/stop condition.\n"));
255                 }
256                 if( 0 != (status & SAA7146_I2C_DTERR) ) {
257                         DEB_I2C(("error in data transmission.\n"));
258                 }
259                 if( 0 != (status & SAA7146_I2C_DRERR) ) {
260                         DEB_I2C(("error when receiving data.\n"));
261                 }
262                 if( 0 != (status & SAA7146_I2C_AL) ) {
263                         DEB_I2C(("error because arbitration lost.\n"));
264                 }
265
266                 /* we handle address-errors here */
267                 if( 0 != (status & SAA7146_I2C_APERR) ) {
268                         DEB_I2C(("error in address phase.\n"));
269                         return -EREMOTEIO;
270                 }
271
272                 return -EIO;
273         }
274
275         /* read back data, just in case we were reading ... */
276         *dword = saa7146_read(dev, I2C_TRANSFER);
277
278         DEB_I2C(("after: 0x%08x\n",*dword));
279         return 0;
280 }
281
282 int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg msgs[], int num, int retries)
283 {
284         int i = 0, count = 0;
285         u32* buffer = dev->d_i2c.cpu_addr;
286         int err = 0;
287         int address_err = 0;
288         int short_delay = 0;
289         
290         if (down_interruptible (&dev->i2c_lock))
291                 return -ERESTARTSYS;
292
293         for(i=0;i<num;i++) {
294                 DEB_I2C(("msg:%d/%d\n",i+1,num));
295         }
296         
297         /* prepare the message(s), get number of u32s to transfer */
298         count = saa7146_i2c_msg_prepare(msgs, num, buffer);
299         if ( 0 > count ) {
300                 err = -1;
301                 goto out;
302         }
303
304         if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )
305                 short_delay = 1;
306   
307         do {
308                 /* reset the i2c-device if necessary */
309                 err = saa7146_i2c_reset(dev);
310                 if ( 0 > err ) {
311                         DEB_I2C(("could not reset i2c-device.\n"));
312                         goto out;
313                 }       
314
315                 /* write out the u32s one after another */
316                 for(i = 0; i < count; i++) {
317                         err = saa7146_i2c_writeout(dev, &buffer[i], short_delay);
318                         if ( 0 != err) {
319                                 /* this one is unsatisfying: some i2c slaves on some
320                                    dvb cards don't acknowledge correctly, so the saa7146
321                                    thinks that an address error occured. in that case, the
322                                    transaction should be retrying, even if an address error
323                                    occured. analog saa7146 based cards extensively rely on
324                                    i2c address probing, however, and address errors indicate that a 
325                                    device is really *not* there. retrying in that case
326                                    increases the time the device needs to probe greatly, so
327                                    it should be avoided. because of the fact, that only 
328                                    analog based cards use irq based i2c transactions (for dvb
329                                    cards, this screwes up other interrupt sources), we bail out
330                                    completely for analog cards after an address error and trust
331                                    the saa7146 address error detection. */
332                                 if ( -EREMOTEIO == err ) {
333                                         if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
334                                                 goto out;
335                                         }
336                                         address_err++;
337                                 }
338                                 DEB_I2C(("error while sending message(s). starting again.\n"));
339                                 break;
340                         }
341                 }
342                 if( 0 == err ) {
343                         err = num;
344                         break;
345                 }
346                 
347                 /* delay a bit before retrying */
348                 my_wait(dev, 10);
349                 
350         } while (err != num && retries--);
351
352         /* if every retry had an address error, exit right away */
353         if (address_err == retries) {
354                 goto out;
355         }
356       
357         /* if any things had to be read, get the results */
358         if ( 0 != saa7146_i2c_msg_cleanup(msgs, num, buffer)) {
359                 DEB_I2C(("could not cleanup i2c-message.\n"));
360                 err = -1;
361                 goto out;
362         }
363
364         /* return the number of delivered messages */
365         DEB_I2C(("transmission successful. (msg:%d).\n",err));
366 out:
367         /* another bug in revision 0: the i2c-registers get uploaded randomly by other
368            uploads, so we better clear them out before continueing */   
369         if( 0 == dev->revision ) {
370                 u32 zero = 0;
371                 saa7146_i2c_reset(dev);
372                 if( 0 != saa7146_i2c_writeout(dev, &zero, short_delay)) {
373                         INFO(("revision 0 error. this should never happen.\n"));
374                 }
375         }
376
377         up(&dev->i2c_lock);
378         return err;     
379 }
380
381 /* utility functions */
382 static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg msg[], int num)
383 {
384         struct saa7146_dev* dev = i2c_get_adapdata(adapter);
385         
386         /* use helper function to transfer data */
387         return saa7146_i2c_transfer(dev, msg, num, adapter->retries);
388 }
389
390
391 /*****************************************************************************/
392 /* i2c-adapter helper functions                                              */
393 #include <linux/i2c-id.h>
394
395 /* exported algorithm data */
396 static struct i2c_algorithm saa7146_algo = {
397         .name           = "saa7146 i2c algorithm",
398         .id             = I2C_ALGO_SAA7146,
399         .master_xfer    = saa7146_i2c_xfer,
400         .functionality  = saa7146_i2c_func,
401 };
402
403 int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, unsigned int class, u32 bitrate)
404 {
405         DEB_EE(("bitrate: 0x%08x\n",bitrate));
406         
407         /* enable i2c-port pins */
408         saa7146_write(dev, MC1, (MASK_08 | MASK_24));
409
410         dev->i2c_bitrate = bitrate;
411         saa7146_i2c_reset(dev);
412
413         if( NULL != i2c_adapter ) {
414                 memset(i2c_adapter,0,sizeof(struct i2c_adapter));
415                 strcpy(i2c_adapter->name, dev->name);   
416 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
417                 i2c_adapter->data = dev;
418 #else
419                 i2c_set_adapdata(i2c_adapter,dev);
420                 i2c_adapter->class = class;
421 #endif
422                 i2c_adapter->algo          = &saa7146_algo;
423                 i2c_adapter->algo_data     = NULL;
424                 i2c_adapter->id            = I2C_ALGO_SAA7146;
425                 i2c_adapter->timeout = SAA7146_I2C_TIMEOUT;
426                 i2c_adapter->retries = SAA7146_I2C_RETRIES;
427         }
428         
429         return 0;
430 }