1 #include <linux/version.h>
2 #include <media/saa7146_vv.h>
5 static void my_wait(struct saa7146_dev *dev, long ms)
7 set_current_state(TASK_INTERRUPTIBLE);
8 schedule_timeout((((ms+10)/10)*HZ)/1000);
11 u32 saa7146_i2c_func(struct i2c_adapter *adapter)
13 //fm DEB_I2C(("'%s'.\n", adapter->name));
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;
21 /* this function returns the status-register of our i2c-device */
22 static inline u32 saa7146_i2c_status(struct saa7146_dev *dev)
24 u32 iicsta = saa7146_read(dev, I2C_STATUS);
26 DEB_I2C(("status: 0x%08x\n",iicsta));
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)
39 int mem = 0, op_count = 0;
41 /* first determine size of needed memory */
42 for(i = 0; i < num; i++) {
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);
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"));
57 /* be careful: clear out the i2c-mem first */
58 memset(op,0,sizeof(u32)*mem);
60 /* loop through all messages */
61 for(i = 0; i < num; i++) {
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));
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));
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));
91 /* return the number of u32s to send */
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)
104 /* loop through all messages */
105 for(i = 0; i < num; i++) {
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));
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)
123 /* get current status */
124 u32 status = saa7146_i2c_status(dev);
126 /* clear registers for sure */
127 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
128 saa7146_write(dev, I2C_TRANSFER, 0);
130 /* check if any operation is still in progress */
131 if ( 0 != ( status & SAA7146_I2C_BUSY) ) {
133 /* yes, kill ongoing operation */
134 DEB_I2C(("busy_state detected.\n"));
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);
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);
147 /* check if any error is (still) present. (this can be necessary because p.123, note 1) */
148 status = saa7146_i2c_status(dev);
150 if ( dev->i2c_bitrate != status ) {
152 DEB_I2C(("error_state detected. status:0x%08x\n",status));
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);
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);
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);
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));
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)
187 u32 status = 0, mc2 = 0;
189 unsigned long timeout;
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));
194 if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {
196 saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);
197 saa7146_write(dev, I2C_TRANSFER, *dword);
200 IER_ENABLE(dev, MASK_16|MASK_17);
201 saa7146_write(dev, MC2, (MASK_00 | MASK_16));
203 wait_event_interruptible(dev->i2c_wq, dev->i2c_op == 0);
204 if (signal_pending (current)) {
205 /* a signal arrived */
208 status = saa7146_read(dev, I2C_STATUS);
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));
214 /* do not poll for i2c-status before upload is complete */
215 timeout = jiffies + HZ/100 + 1; /* 10ms */
217 mc2 = (saa7146_read(dev, MC2) & 0x1);
221 if (time_after(jiffies,timeout)) {
222 printk(KERN_WARNING "saa7146_i2c_writeout: timed out waiting for MC2\n");
226 /* wait until we get a transfer done or error */
227 timeout = jiffies + HZ/100 + 1; /* 10ms */
230 * first read usually delivers bogus results...
232 saa7146_i2c_status(dev);
233 status = saa7146_i2c_status(dev);
234 if ((status & 0x3) != 1)
236 if (time_after(jiffies,timeout)) {
237 /* this is normal when probing the bus
238 * (no answer from nonexisistant device...)
240 DEB_I2C(("saa7146_i2c_writeout: timed out waiting for end of xfer\n"));
243 if ((++trial < 20) && short_delay)
250 /* give a detailed status report */
251 if ( 0 != (status & SAA7146_I2C_ERR)) {
253 if( 0 != (status & SAA7146_I2C_SPERR) ) {
254 DEB_I2C(("error due to invalid start/stop condition.\n"));
256 if( 0 != (status & SAA7146_I2C_DTERR) ) {
257 DEB_I2C(("error in data transmission.\n"));
259 if( 0 != (status & SAA7146_I2C_DRERR) ) {
260 DEB_I2C(("error when receiving data.\n"));
262 if( 0 != (status & SAA7146_I2C_AL) ) {
263 DEB_I2C(("error because arbitration lost.\n"));
266 /* we handle address-errors here */
267 if( 0 != (status & SAA7146_I2C_APERR) ) {
268 DEB_I2C(("error in address phase.\n"));
275 /* read back data, just in case we were reading ... */
276 *dword = saa7146_read(dev, I2C_TRANSFER);
278 DEB_I2C(("after: 0x%08x\n",*dword));
282 int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg msgs[], int num, int retries)
284 int i = 0, count = 0;
285 u32* buffer = dev->d_i2c.cpu_addr;
290 if (down_interruptible (&dev->i2c_lock))
294 DEB_I2C(("msg:%d/%d\n",i+1,num));
297 /* prepare the message(s), get number of u32s to transfer */
298 count = saa7146_i2c_msg_prepare(msgs, num, buffer);
304 if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )
308 /* reset the i2c-device if necessary */
309 err = saa7146_i2c_reset(dev);
311 DEB_I2C(("could not reset i2c-device.\n"));
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);
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)) {
338 DEB_I2C(("error while sending message(s). starting again.\n"));
347 /* delay a bit before retrying */
350 } while (err != num && retries--);
352 /* if every retry had an address error, exit right away */
353 if (address_err == retries) {
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"));
364 /* return the number of delivered messages */
365 DEB_I2C(("transmission successful. (msg:%d).\n",err));
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 ) {
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"));
381 /* utility functions */
382 static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg msg[], int num)
384 struct saa7146_dev* dev = i2c_get_adapdata(adapter);
386 /* use helper function to transfer data */
387 return saa7146_i2c_transfer(dev, msg, num, adapter->retries);
391 /*****************************************************************************/
392 /* i2c-adapter helper functions */
393 #include <linux/i2c-id.h>
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,
403 int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, unsigned int class, u32 bitrate)
405 DEB_EE(("bitrate: 0x%08x\n",bitrate));
407 /* enable i2c-port pins */
408 saa7146_write(dev, MC1, (MASK_08 | MASK_24));
410 dev->i2c_bitrate = bitrate;
411 saa7146_i2c_reset(dev);
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;
419 i2c_set_adapdata(i2c_adapter,dev);
420 i2c_adapter->class = class;
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;