Path: blob/master/drivers/media/common/saa7146_i2c.c
15112 views
#include <media/saa7146_vv.h>12static u32 saa7146_i2c_func(struct i2c_adapter *adapter)3{4//fm DEB_I2C(("'%s'.\n", adapter->name));56return I2C_FUNC_I2C7| I2C_FUNC_SMBUS_QUICK8| I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE9| I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA;10}1112/* this function returns the status-register of our i2c-device */13static inline u32 saa7146_i2c_status(struct saa7146_dev *dev)14{15u32 iicsta = saa7146_read(dev, I2C_STATUS);16/*17DEB_I2C(("status: 0x%08x\n",iicsta));18*/19return iicsta;20}2122/* this function runs through the i2c-messages and prepares the data to be23sent through the saa7146. have a look at the specifications p. 122 ff24to understand this. it returns the number of u32s to send, or -125in case of an error. */26static int saa7146_i2c_msg_prepare(const struct i2c_msg *m, int num, __le32 *op)27{28int h1, h2;29int i, j, addr;30int mem = 0, op_count = 0;3132/* first determine size of needed memory */33for(i = 0; i < num; i++) {34mem += m[i].len + 1;35}3637/* worst case: we need one u32 for three bytes to be send38plus one extra byte to address the device */39mem = 1 + ((mem-1) / 3);4041/* we assume that op points to a memory of at least SAA7146_I2C_MEM bytes42size. if we exceed this limit... */43if ( (4*mem) > SAA7146_I2C_MEM ) {44//fm DEB_I2C(("cannot prepare i2c-message.\n"));45return -ENOMEM;46}4748/* be careful: clear out the i2c-mem first */49memset(op,0,sizeof(__le32)*mem);5051/* loop through all messages */52for(i = 0; i < num; i++) {5354/* insert the address of the i2c-slave.55note: we get 7 bit i2c-addresses,56so we have to perform a translation */57addr = (m[i].addr*2) + ( (0 != (m[i].flags & I2C_M_RD)) ? 1 : 0);58h1 = op_count/3; h2 = op_count%3;59op[h1] |= cpu_to_le32( (u8)addr << ((3-h2)*8));60op[h1] |= cpu_to_le32(SAA7146_I2C_START << ((3-h2)*2));61op_count++;6263/* loop through all bytes of message i */64for(j = 0; j < m[i].len; j++) {65/* insert the data bytes */66h1 = op_count/3; h2 = op_count%3;67op[h1] |= cpu_to_le32( (u32)((u8)m[i].buf[j]) << ((3-h2)*8));68op[h1] |= cpu_to_le32( SAA7146_I2C_CONT << ((3-h2)*2));69op_count++;70}7172}7374/* have a look at the last byte inserted:75if it was: ...CONT change it to ...STOP */76h1 = (op_count-1)/3; h2 = (op_count-1)%3;77if ( SAA7146_I2C_CONT == (0x3 & (le32_to_cpu(op[h1]) >> ((3-h2)*2))) ) {78op[h1] &= ~cpu_to_le32(0x2 << ((3-h2)*2));79op[h1] |= cpu_to_le32(SAA7146_I2C_STOP << ((3-h2)*2));80}8182/* return the number of u32s to send */83return mem;84}8586/* this functions loops through all i2c-messages. normally, it should determine87which bytes were read through the adapter and write them back to the corresponding88i2c-message. but instead, we simply write back all bytes.89fixme: this could be improved. */90static int saa7146_i2c_msg_cleanup(const struct i2c_msg *m, int num, __le32 *op)91{92int i, j;93int op_count = 0;9495/* loop through all messages */96for(i = 0; i < num; i++) {9798op_count++;99100/* loop through all bytes of message i */101for(j = 0; j < m[i].len; j++) {102/* write back all bytes that could have been read */103m[i].buf[j] = (le32_to_cpu(op[op_count/3]) >> ((3-(op_count%3))*8));104op_count++;105}106}107108return 0;109}110111/* this functions resets the i2c-device and returns 0 if everything was fine, otherwise -1 */112static int saa7146_i2c_reset(struct saa7146_dev *dev)113{114/* get current status */115u32 status = saa7146_i2c_status(dev);116117/* clear registers for sure */118saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);119saa7146_write(dev, I2C_TRANSFER, 0);120121/* check if any operation is still in progress */122if ( 0 != ( status & SAA7146_I2C_BUSY) ) {123124/* yes, kill ongoing operation */125DEB_I2C(("busy_state detected.\n"));126127/* set "ABORT-OPERATION"-bit (bit 7)*/128saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));129saa7146_write(dev, MC2, (MASK_00 | MASK_16));130msleep(SAA7146_I2C_DELAY);131132/* clear all error-bits pending; this is needed because p.123, note 1 */133saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);134saa7146_write(dev, MC2, (MASK_00 | MASK_16));135msleep(SAA7146_I2C_DELAY);136}137138/* check if any error is (still) present. (this can be necessary because p.123, note 1) */139status = saa7146_i2c_status(dev);140141if ( dev->i2c_bitrate != status ) {142143DEB_I2C(("error_state detected. status:0x%08x\n",status));144145/* Repeat the abort operation. This seems to be necessary146after serious protocol errors caused by e.g. the SAA7740 */147saa7146_write(dev, I2C_STATUS, (dev->i2c_bitrate | MASK_07));148saa7146_write(dev, MC2, (MASK_00 | MASK_16));149msleep(SAA7146_I2C_DELAY);150151/* clear all error-bits pending */152saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);153saa7146_write(dev, MC2, (MASK_00 | MASK_16));154msleep(SAA7146_I2C_DELAY);155156/* the data sheet says it might be necessary to clear the status157twice after an abort */158saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);159saa7146_write(dev, MC2, (MASK_00 | MASK_16));160msleep(SAA7146_I2C_DELAY);161}162163/* if any error is still present, a fatal error has occurred ... */164status = saa7146_i2c_status(dev);165if ( dev->i2c_bitrate != status ) {166DEB_I2C(("fatal error. status:0x%08x\n",status));167return -1;168}169170return 0;171}172173/* this functions writes out the data-byte 'dword' to the i2c-device.174it returns 0 if ok, -1 if the transfer failed, -2 if the transfer175failed badly (e.g. address error) */176static int saa7146_i2c_writeout(struct saa7146_dev *dev, __le32 *dword, int short_delay)177{178u32 status = 0, mc2 = 0;179int trial = 0;180unsigned long timeout;181182/* write out i2c-command */183DEB_I2C(("before: 0x%08x (status: 0x%08x), %d\n",*dword,saa7146_read(dev, I2C_STATUS), dev->i2c_op));184185if( 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags)) {186187saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);188saa7146_write(dev, I2C_TRANSFER, le32_to_cpu(*dword));189190dev->i2c_op = 1;191SAA7146_ISR_CLEAR(dev, MASK_16|MASK_17);192SAA7146_IER_ENABLE(dev, MASK_16|MASK_17);193saa7146_write(dev, MC2, (MASK_00 | MASK_16));194195timeout = HZ/100 + 1; /* 10ms */196timeout = wait_event_interruptible_timeout(dev->i2c_wq, dev->i2c_op == 0, timeout);197if (timeout == -ERESTARTSYS || dev->i2c_op) {198SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);199SAA7146_ISR_CLEAR(dev, MASK_16|MASK_17);200if (timeout == -ERESTARTSYS)201/* a signal arrived */202return -ERESTARTSYS;203204printk(KERN_WARNING "%s %s [irq]: timed out waiting for end of xfer\n",205dev->name, __func__);206return -EIO;207}208status = saa7146_read(dev, I2C_STATUS);209} else {210saa7146_write(dev, I2C_STATUS, dev->i2c_bitrate);211saa7146_write(dev, I2C_TRANSFER, le32_to_cpu(*dword));212saa7146_write(dev, MC2, (MASK_00 | MASK_16));213214/* do not poll for i2c-status before upload is complete */215timeout = jiffies + HZ/100 + 1; /* 10ms */216while(1) {217mc2 = (saa7146_read(dev, MC2) & 0x1);218if( 0 != mc2 ) {219break;220}221if (time_after(jiffies,timeout)) {222printk(KERN_WARNING "%s %s: timed out waiting for MC2\n",223dev->name, __func__);224return -EIO;225}226}227/* wait until we get a transfer done or error */228timeout = jiffies + HZ/100 + 1; /* 10ms */229/* first read usually delivers bogus results... */230saa7146_i2c_status(dev);231while(1) {232status = saa7146_i2c_status(dev);233if ((status & 0x3) != 1)234break;235if (time_after(jiffies,timeout)) {236/* this is normal when probing the bus237* (no answer from nonexisistant device...)238*/239printk(KERN_WARNING "%s %s [poll]: timed out waiting for end of xfer\n",240dev->name, __func__);241return -EIO;242}243if (++trial < 50 && short_delay)244udelay(10);245else246msleep(1);247}248}249250/* give a detailed status report */251if ( 0 != (status & (SAA7146_I2C_SPERR | SAA7146_I2C_APERR |252SAA7146_I2C_DTERR | SAA7146_I2C_DRERR |253SAA7146_I2C_AL | SAA7146_I2C_ERR |254SAA7146_I2C_BUSY)) ) {255256if ( 0 == (status & SAA7146_I2C_ERR) ||2570 == (status & SAA7146_I2C_BUSY) ) {258/* it may take some time until ERR goes high - ignore */259DEB_I2C(("unexpected i2c status %04x\n", status));260}261if( 0 != (status & SAA7146_I2C_SPERR) ) {262DEB_I2C(("error due to invalid start/stop condition.\n"));263}264if( 0 != (status & SAA7146_I2C_DTERR) ) {265DEB_I2C(("error in data transmission.\n"));266}267if( 0 != (status & SAA7146_I2C_DRERR) ) {268DEB_I2C(("error when receiving data.\n"));269}270if( 0 != (status & SAA7146_I2C_AL) ) {271DEB_I2C(("error because arbitration lost.\n"));272}273274/* we handle address-errors here */275if( 0 != (status & SAA7146_I2C_APERR) ) {276DEB_I2C(("error in address phase.\n"));277return -EREMOTEIO;278}279280return -EIO;281}282283/* read back data, just in case we were reading ... */284*dword = cpu_to_le32(saa7146_read(dev, I2C_TRANSFER));285286DEB_I2C(("after: 0x%08x\n",*dword));287return 0;288}289290static int saa7146_i2c_transfer(struct saa7146_dev *dev, const struct i2c_msg *msgs, int num, int retries)291{292int i = 0, count = 0;293__le32 *buffer = dev->d_i2c.cpu_addr;294int err = 0;295int short_delay = 0;296297if (mutex_lock_interruptible(&dev->i2c_lock))298return -ERESTARTSYS;299300for(i=0;i<num;i++) {301DEB_I2C(("msg:%d/%d\n",i+1,num));302}303304/* prepare the message(s), get number of u32s to transfer */305count = saa7146_i2c_msg_prepare(msgs, num, buffer);306if ( 0 > count ) {307err = -1;308goto out;309}310311if ( count > 3 || 0 != (SAA7146_I2C_SHORT_DELAY & dev->ext->flags) )312short_delay = 1;313314do {315/* reset the i2c-device if necessary */316err = saa7146_i2c_reset(dev);317if ( 0 > err ) {318DEB_I2C(("could not reset i2c-device.\n"));319goto out;320}321322/* write out the u32s one after another */323for(i = 0; i < count; i++) {324err = saa7146_i2c_writeout(dev, &buffer[i], short_delay);325if ( 0 != err) {326/* this one is unsatisfying: some i2c slaves on some327dvb cards don't acknowledge correctly, so the saa7146328thinks that an address error occurred. in that case, the329transaction should be retrying, even if an address error330occurred. analog saa7146 based cards extensively rely on331i2c address probing, however, and address errors indicate that a332device is really *not* there. retrying in that case333increases the time the device needs to probe greatly, so334it should be avoided. So we bail out in irq mode after an335address error and trust the saa7146 address error detection. */336if (-EREMOTEIO == err && 0 != (SAA7146_USE_I2C_IRQ & dev->ext->flags))337goto out;338DEB_I2C(("error while sending message(s). starting again.\n"));339break;340}341}342if( 0 == err ) {343err = num;344break;345}346347/* delay a bit before retrying */348msleep(10);349350} while (err != num && retries--);351352/* quit if any error occurred */353if (err != num)354goto out;355356/* if any things had to be read, get the results */357if ( 0 != saa7146_i2c_msg_cleanup(msgs, num, buffer)) {358DEB_I2C(("could not cleanup i2c-message.\n"));359err = -1;360goto out;361}362363/* return the number of delivered messages */364DEB_I2C(("transmission successful. (msg:%d).\n",err));365out:366/* another bug in revision 0: the i2c-registers get uploaded randomly by other367uploads, so we better clear them out before continuing */368if( 0 == dev->revision ) {369__le32 zero = 0;370saa7146_i2c_reset(dev);371if( 0 != saa7146_i2c_writeout(dev, &zero, short_delay)) {372INFO(("revision 0 error. this should never happen.\n"));373}374}375376mutex_unlock(&dev->i2c_lock);377return err;378}379380/* utility functions */381static int saa7146_i2c_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)382{383struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);384struct saa7146_dev *dev = to_saa7146_dev(v4l2_dev);385386/* use helper function to transfer data */387return saa7146_i2c_transfer(dev, msg, num, adapter->retries);388}389390391/*****************************************************************************/392/* i2c-adapter helper functions */393394/* exported algorithm data */395static struct i2c_algorithm saa7146_algo = {396.master_xfer = saa7146_i2c_xfer,397.functionality = saa7146_i2c_func,398};399400int saa7146_i2c_adapter_prepare(struct saa7146_dev *dev, struct i2c_adapter *i2c_adapter, u32 bitrate)401{402DEB_EE(("bitrate: 0x%08x\n",bitrate));403404/* enable i2c-port pins */405saa7146_write(dev, MC1, (MASK_08 | MASK_24));406407dev->i2c_bitrate = bitrate;408saa7146_i2c_reset(dev);409410if (i2c_adapter) {411i2c_set_adapdata(i2c_adapter, &dev->v4l2_dev);412i2c_adapter->dev.parent = &dev->pci->dev;413i2c_adapter->algo = &saa7146_algo;414i2c_adapter->algo_data = NULL;415i2c_adapter->timeout = SAA7146_I2C_TIMEOUT;416i2c_adapter->retries = SAA7146_I2C_RETRIES;417}418419return 0;420}421422423