Subversion Repositories shark

Rev

Rev 774 | Details | Compare with Previous | Last modification | View Log | RSS feed

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