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