home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / I2C / i2calgob.c next >
C/C++ Source or Header  |  2002-04-26  |  15KB  |  585 lines

  1. /* $Id: i2calgob.c,v 1.2 2002/04/26 23:08:58 smilcke Exp $ */
  2.  
  3. /*
  4.  * i2calgob.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         30.10.2001
  7.  * Letzte Aenderung am: 22.12.2001
  8.  *
  9. */
  10.  
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/version.h>
  16. #include <linux/init.h>
  17. #include <asm/uaccess.h>
  18. #include <linux/ioport.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21.  
  22. #include <linux/i2c.h>
  23. #include <linux/i2c-algo-bit.h>
  24.  
  25. #ifdef USEPRINTK
  26. #define DEB(x) if (i2c_debug>=1) x;
  27. #define DEB2(x) if (i2c_debug>=2) x;
  28. #define DEBSTAT(x) if (i2c_debug>=3) x; // print several statistical values
  29. #define DEBPROTO(x) if (i2c_debug>=9) { x; }
  30. #else
  31. #define DEB(x)
  32. #define DEB2(x)
  33. #define DEBSTAT(x)
  34. #define DEBPROTO(x)
  35. #endif
  36.  
  37. #ifdef SLO_IO
  38.  int jif;
  39. #endif
  40.  
  41. static int i2c_debug;
  42. static int bit_test;    // see if the line-setting functions work
  43. static int bit_scan;    // have a look at what's hanging 'round
  44.  
  45. #define setsda(adap,val) adap->setsda(adap->data, val)
  46. #define setscl(adap,val) adap->setscl(adap->data, val)
  47. #define getsda(adap) adap->getsda(adap->data)
  48. #define getscl(adap) adap->getscl(adap->data)
  49.  
  50. //----------------------------------- sdalo ------------------------------------
  51. static __inline__ void sdalo(struct i2c_algo_bit_data *adap)
  52. {
  53.     setsda(adap,0);
  54.     udelay(adap->udelay);
  55. }
  56.  
  57. //----------------------------------- sdahi ------------------------------------
  58. static __inline__ void sdahi(struct i2c_algo_bit_data *adap)
  59. {
  60.     setsda(adap,1);
  61.     udelay(adap->udelay);
  62. }
  63.  
  64. //----------------------------------- scllo ------------------------------------
  65. static __inline__ void scllo(struct i2c_algo_bit_data *adap)
  66. {
  67.     setscl(adap,0);
  68.     udelay(adap->udelay);
  69. #ifdef SLO_IO
  70.     SLO_IO
  71. #endif
  72. }
  73.  
  74. //----------------------------------- sclhi ------------------------------------
  75. static __inline__ int sclhi(struct i2c_algo_bit_data *adap)
  76. {
  77.  int start=jiffies;
  78.  setscl(adap,1);
  79.  udelay(adap->udelay);
  80.  // Not all adapters have scl sense line...
  81.  if(adap->getscl == NULL )
  82.   return 0;
  83.  while(!getscl(adap))
  84.  {    
  85.   // the hw knows how to read the clock line,
  86.   // so we wait until it actually gets high.
  87.   // This is safer as some chips may hold it low
  88.   // while they are processing data internally.
  89.   setscl(adap,1);
  90.   if(start+adap->timeout <= jiffies)
  91.   {
  92.    return -ETIMEDOUT;
  93.   }
  94.   if(current->need_resched)
  95.    schedule();
  96.  }
  97.  DEBSTAT(printk("needed %ld jiffies\n", jiffies-start));
  98. #ifdef SLO_IO
  99.     SLO_IO
  100. #endif
  101.     return 0;
  102. }
  103.  
  104. //--------------------------------- i2c_start ----------------------------------
  105. static void i2c_start(struct i2c_algo_bit_data *adap)
  106. {
  107.  // assert: scl, sda are high
  108.  DEBPROTO(printk("S "));
  109.  sdalo(adap);
  110.  scllo(adap);
  111. }
  112.  
  113. //-------------------------------- i2c_repstart --------------------------------
  114. static void i2c_repstart(struct i2c_algo_bit_data *adap)
  115. {
  116.  // scl, sda may not be high
  117.  DEBPROTO(printk(" Sr "));
  118.  setsda(adap,1);
  119.  setscl(adap,1);
  120.  udelay(adap->udelay);
  121.  sdalo(adap);
  122.  scllo(adap);
  123. }
  124.  
  125. //---------------------------------- i2c_stop ----------------------------------
  126. static void i2c_stop(struct i2c_algo_bit_data *adap)
  127. {
  128.  DEBPROTO(printk("P\n"));
  129.  // assert: scl is low
  130.  sdalo(adap);
  131.  sclhi(adap);
  132.  sdahi(adap);
  133. }
  134.  
  135. //---------------------------------- i2c_outb ----------------------------------
  136. // send a byte without start cond., look for arbitration, check ackn. from slave
  137. // returns:
  138. // 1 if the device acknowledged
  139. // 0 if the device did not ack
  140. // -ETIMEDOUT if an error occurred (while raising the scl line)
  141. static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
  142. {
  143.  int i;
  144.  int sb;
  145.  int ack;
  146.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  147.  // assert: scl is low */
  148.  DEB2(printk(" i2c_outb:%2.2X\n",c&0xff));
  149.  for( i=7 ; i>=0 ; i-- )
  150.  {
  151.   sb = c & ( 1 << i );
  152.   setsda(adap,sb);
  153.   udelay(adap->udelay);
  154.   DEBPROTO(printk("%d",sb!=0));
  155.   if(sclhi(adap)<0)
  156.   { // timed out
  157.    sdahi(adap); // we don't want to block the net
  158.     return -ETIMEDOUT;
  159.   };
  160.   // do arbitration here:
  161.   // if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
  162.   setscl(adap, 0 );
  163.   udelay(adap->udelay);
  164.  }
  165.  sdahi(adap);
  166.  if (sclhi(adap)<0)
  167.  { // timeout
  168.   return -ETIMEDOUT;
  169.  };
  170.  // read ack: SDA should be pulled down by slave
  171.  ack=getsda(adap);    // ack: sda is pulled low ->success.
  172.  DEB2(printk(" i2c_outb: getsda() =  0x%2.2x\n", ~ack ));
  173.  DEBPROTO( printk("[%2.2x]",c&0xff) );
  174.  DEBPROTO(if (0==ack){ printk(" A ");} else printk(" NA ") );
  175.  scllo(adap);
  176.  return 0==ack;        // return 1 if device acked
  177.  // assert: scl is low (sda undef)
  178. }
  179.  
  180. //---------------------------------- i2c_inb -----------------------------------
  181. static int i2c_inb(struct i2c_adapter *i2c_adap)
  182. {
  183.  // read byte via i2c port, without start/stop sequence
  184.  // acknowledge is sent in i2c_read.
  185.  int i;
  186.  unsigned char indata=0;
  187.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  188.  // assert: scl is low
  189.  DEB2(printk("i2c_inb.\n"));
  190.  sdahi(adap);
  191.  for (i=0;i<8;i++)
  192.  {
  193.   if(sclhi(adap)<0)
  194.   { // timeout
  195.    return -ETIMEDOUT;
  196.   };
  197.   indata *= 2;
  198.   if( getsda(adap))
  199.    indata |= 0x01;
  200.   scllo(adap);
  201.  }
  202.  // assert: scl is low
  203.  DEBPROTO(printk(" %2.2x", indata & 0xff));
  204.  return (int) (indata & 0xff);
  205. }
  206.  
  207. //---------------------------------- test_bus ----------------------------------
  208. static int test_bus(struct i2c_algo_bit_data *adap, char* name)
  209. {
  210.  int scl,sda;
  211.  sda=getsda(adap);
  212.  if(adap->getscl==NULL)
  213.  {
  214.   CPK(printk("i2calgob: Warning: Adapter can't read from clock line - skipping test.\n"));
  215.   return 0;        
  216.  }
  217.  scl=getscl(adap);
  218.  CPK(printk("i2calgob: Adapter: %s scl: %d  sda: %d -- testing...\n",name,getscl(adap),getsda(adap)));
  219.  if(!scl || !sda )
  220.  {
  221.   CPK(printk("i2calgob: %s seems to be busy.\n",name));
  222.   goto bailout;
  223.  }
  224.  sdalo(adap);
  225.  CPK(printk("i2calgob:1 scl: %d  sda: %d \n",getscl(adap),
  226.             getsda(adap)));
  227.  if( 0 != getsda(adap) )
  228.  {
  229.   CPK(printk("i2calgob: %s SDA stuck high!\n",name));
  230.   sdahi(adap);
  231.   goto bailout;
  232.  }
  233.  if( 0 == getscl(adap) )
  234.  {
  235.   CPK(printk("i2calgob: %s SCL unexpected low while pulling SDA low!\n",name));
  236.   goto bailout;
  237.  }        
  238.  sdahi(adap);
  239.  CPK(printk("i2calgob:2 scl: %d  sda: %d \n",getscl(adap),getsda(adap)));
  240.  if ( 0 == getsda(adap) )
  241.  {
  242.   CPK(printk("i2calgob: %s SDA stuck low!\n",name));
  243.   sdahi(adap);
  244.   goto bailout;
  245.  }
  246.  if ( 0 == getscl(adap) )
  247.  {
  248.   CPK(printk("i2calgob: %s SCL unexpected low while SDA high!\n",name));
  249.   goto bailout;
  250.  }
  251.  scllo(adap);
  252.  CPK(printk("i2calgob:3 scl: %d  sda: %d \n",getscl(adap),getsda(adap)));
  253.  if ( 0 != getscl(adap) )
  254.  {
  255.   CPK(printk("i2calgob: %s SCL stuck high!\n",name));
  256.   sclhi(adap);
  257.   goto bailout;
  258.  }
  259.  if ( 0 == getsda(adap) )
  260.  {
  261.   CPK(printk("i2calgob: %s SDA unexpected low while pulling SCL low!\n",name));
  262.   goto bailout;
  263.  }
  264.  sclhi(adap);
  265.  CPK(printk("i2calgob:4 scl: %d  sda: %d \n",getscl(adap),getsda(adap)));
  266.  if ( 0 == getscl(adap) )
  267.  {
  268.   CPK(printk("i2calgob: %s SCL stuck low!\n",name));
  269.   sclhi(adap);
  270.   goto bailout;
  271.  }
  272.  if ( 0 == getsda(adap) )
  273.  {
  274.   CPK(printk("i2calgob: %s SDA unexpected low while SCL high!\n",name));
  275.   goto bailout;
  276.  }
  277.  CPK(printk("i2calgob: %s passed test.\n",name));
  278.  return 0;
  279. bailout:
  280.  sdahi(adap);
  281.  sclhi(adap);
  282.  return -ENODEV;
  283. }
  284.  
  285. //-------------------------------- try_address ---------------------------------
  286. // try_address tries to contact a chip for a number of
  287. // times before it gives up.
  288. // return values:
  289. // 1 chip answered
  290. // 0 chip did not answer
  291. // -x transmission error
  292. static __inline__ int try_address(struct i2c_adapter *i2c_adap,
  293.                unsigned char addr, int retries)
  294. {
  295.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  296.  int i,ret = -1;
  297.  for (i=0;i<=retries;i++)
  298.  {
  299.   ret = i2c_outb(i2c_adap,addr);
  300.   if(ret==1)
  301.    break;    // success!
  302.   i2c_stop(adap);
  303.   udelay(5/*adap->udelay*/);
  304.   if(i==retries)  // no success */
  305.    break;
  306.   i2c_start(adap);
  307.   udelay(adap->udelay);
  308.  }
  309.  DEB2(if (i) printk("i2calgob: needed %d retries for %d\n", i,addr));
  310.  return ret;
  311. }
  312.  
  313. //--------------------------------- sendbytes ----------------------------------
  314. static int sendbytes(struct i2c_adapter *i2c_adap,const char *buf, int count)
  315. {
  316.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  317.  char c;
  318.  const char *temp = buf;
  319.  int retval;
  320.  int wrcount=0;
  321.  while (count > 0)
  322.  {
  323.   c = *temp;
  324.   DEB2(printk("i2calgob: %s i2c_write: writing %2.2X\n",i2c_adap->name, c&0xff));
  325.   retval = i2c_outb(i2c_adap,c);
  326.   if(retval>0)
  327.   {
  328.    count--;
  329.    temp++;
  330.    wrcount++;
  331.   }
  332.   else
  333.   { // arbitration or no acknowledge
  334.    CPK(printk("i2calgob: %s i2c_write: error - bailout.\n",
  335.                    i2c_adap->name));
  336.    i2c_stop(adap);
  337.    return (retval<0)? retval : -EFAULT;
  338.     // got a better one ??
  339.   }
  340. #if 0
  341.   // from asm/delay.h
  342.   __delay(adap->mdelay * (loops_per_sec / 1000) );
  343. #endif
  344.  }
  345.  return wrcount;
  346. }
  347.  
  348. //--------------------------------- readbytes ----------------------------------
  349. static __inline__ int readbytes(struct i2c_adapter *i2c_adap,char *buf,int count)
  350. {
  351.  char *temp = buf;
  352.  int inval;
  353.  int rdcount=0;       // counts bytes read
  354.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  355.  while (count > 0)
  356.  {
  357.   inval = i2c_inb(i2c_adap);
  358. //printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n");
  359.   if(inval>=0)
  360.   {
  361.    *temp = inval;
  362.    rdcount++;
  363.   }
  364.   else
  365.   {   // read timed out
  366.    CPK(printk("i2calgob: i2c_read: i2c_inb timed out.\n"));
  367.    break;
  368.   }
  369.   if( count > 1 )
  370.   { // send ack
  371.    sdalo(adap);
  372.    DEBPROTO(printk(" Am "));
  373.   }
  374.   else
  375.   {
  376.    sdahi(adap); // neg. ack on last byte
  377.    DEBPROTO(printk(" NAm "));
  378.   }
  379.   if(sclhi(adap)<0)
  380.   { // timeout
  381.    sdahi(adap);
  382.    CPK(printk("i2calgob: i2c_read: Timeout at ack\n"));
  383.     return -ETIMEDOUT;
  384.   };
  385.   scllo(adap);
  386.   sdahi(adap);
  387.   temp++;
  388.   count--;
  389.  }
  390.  return rdcount;
  391. }
  392.  
  393. //--------------------------------- doAddress ----------------------------------
  394. // doAddress initiates the transfer by generating the start condition (in
  395. // try_address) and transmits the address in the necessary format to handle
  396. // reads, writes as well as 10bit-addresses.
  397. // returns:
  398. //  0 everything went okay, the chip ack'ed
  399. // -x an error occurred (like: -EREMOTEIO if the device did not answer, or
  400. //    -ETIMEDOUT, for example if the lines are stuck...)
  401. static __inline__ int bit_doAddress(struct i2c_adapter *i2c_adap,
  402.                                 struct i2c_msg *msg, int retries)
  403. {
  404.  unsigned short flags = msg->flags;
  405.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  406.  unsigned char addr;
  407.  int ret;
  408.  if( (flags & I2C_M_TEN)  )
  409.  {
  410.   // a ten bit address
  411.   addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  412.   DEB2(printk("addr0: %d\n",addr));
  413.   // try extended address code...*/
  414.   ret = try_address(i2c_adap, addr, retries);
  415.   if(ret!=1)
  416.   {
  417.    CPK(printk("died at extended address code.\n"));
  418.    return -EREMOTEIO;
  419.   }
  420.   // the remaining 8 bit address
  421.   ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
  422.   if(ret != 1)
  423.   {// the chip did not ack / xmission error occurred
  424.    CPK(printk("died at 2nd address code.\n"));
  425.    return -EREMOTEIO;
  426.   }
  427.   if( flags & I2C_M_RD )
  428.   {
  429.    i2c_repstart(adap);
  430.     // okay, now switch into reading mode
  431.    addr |= 0x01;
  432.    ret = try_address(i2c_adap, addr, retries);
  433.     if (ret!=1)
  434.    {
  435.     CPK(printk("died at extended address code.\n"));
  436.     return -EREMOTEIO;
  437.    }
  438.   }
  439.  }
  440.  else
  441.  {
  442.   // normal 7bit address
  443.   addr = ( msg->addr << 1 );
  444.   if(flags & I2C_M_RD )
  445.    addr |= 1;
  446.   if(flags & I2C_M_REV_DIR_ADDR )
  447.    addr ^= 1;
  448.   ret = try_address(i2c_adap, addr, retries);
  449.   if (ret!=1)
  450.   {
  451.    return -EREMOTEIO;
  452.   }
  453.  }
  454.  return 0;
  455. }
  456.  
  457. //---------------------------------- bit_xfer ----------------------------------
  458. static int bit_xfer(struct i2c_adapter *i2c_adap,
  459.                     struct i2c_msg msgs[], int num)
  460. {
  461.  struct i2c_msg *pmsg;
  462.  struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
  463.  int i,ret;
  464.  i2c_start(adap);
  465.  for (i=0;i<num;i++)
  466.  {
  467.   pmsg = &msgs[i];
  468.   if(!(pmsg->flags & I2C_M_NOSTART))
  469.   {
  470.    if(i)
  471.    {
  472.     i2c_repstart(adap);
  473.    }
  474.    ret = bit_doAddress(i2c_adap,pmsg,i2c_adap->retries);
  475.    if(ret != 0)
  476.    {
  477.     DEB2(printk("i2calgob: NAK from device adr %#2x msg #%d\n",msgs[i].addr,i));
  478.     return (ret<0) ? ret : -EREMOTEIO;
  479.    }
  480.   }
  481.   if(pmsg->flags & I2C_M_RD )
  482.   {
  483.    // read bytes into buffer
  484.    ret = readbytes(i2c_adap,pmsg->buf,pmsg->len);
  485.    DEB2(printk("i2calgob: read %d bytes.\n",ret));
  486.    if(ret < pmsg->len )
  487.    {
  488.     return (ret<0)? ret : -EREMOTEIO;
  489.    }
  490.   }
  491.   else
  492.   {
  493.    // write bytes from buffer
  494.    ret = sendbytes(i2c_adap,pmsg->buf,pmsg->len);
  495.    DEB2(printk("i2calgob: wrote %d bytes.\n",ret));
  496.    if(ret < pmsg->len )
  497.    {
  498.     return (ret<0) ? ret : -EREMOTEIO;
  499.    }
  500.   }
  501.  }
  502.  i2c_stop(adap);
  503.  return num;
  504. }
  505.  
  506. //-------------------------------- algo_control --------------------------------
  507. static int algo_control(struct i2c_adapter *adapter,unsigned int cmd,unsigned long arg)
  508. {
  509.  return 0;
  510. }
  511.  
  512. //---------------------------------- bit_func ----------------------------------
  513. static u32 bit_func(struct i2c_adapter *adap)
  514. {
  515.     return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
  516.            I2C_FUNC_PROTOCOL_MANGLING;
  517. }
  518.  
  519. static struct i2c_algorithm i2c_bit_algo = {
  520.     "Bit-shift algorithm",
  521.     I2C_ALGO_BIT,
  522.     bit_xfer,
  523.     NULL,
  524.     NULL,                /* slave_xmit        */
  525.     NULL,                /* slave_recv        */
  526.     algo_control,            /* ioctl        */
  527.     bit_func,            /* functionality    */
  528. };
  529.  
  530. //------------------------------ i2c_bit_add_bus -------------------------------
  531. int i2c_bit_add_bus(struct i2c_adapter *adap)
  532. {
  533.  int i;
  534.  struct i2c_algo_bit_data *bit_adap = adap->algo_data;
  535.  if(bit_test)
  536.  {
  537.   int ret = test_bus(bit_adap, adap->name);
  538.   if(ret<0)
  539.     return -ENODEV;
  540.  }
  541.  DEB2(printk("i2calgob: hw routines for %s registered.\n",adap->name));
  542.  // register new adapter to i2c module...
  543.  adap->id |= i2c_bit_algo.id;
  544.  adap->algo = &i2c_bit_algo;
  545.  adap->timeout = 100;    // default values, should
  546.  adap->retries = 3;    // be replaced by defines
  547.  // scan bus
  548.  if(bit_scan)
  549.  {
  550.   int ack;
  551.   CPK(printk(KERN_INFO " i2calgob: scanning bus %s.\n",
  552.                adap->name));
  553.   for (i = 0x00; i < 0xff; i+=2)
  554.   {
  555.    i2c_start(bit_adap);
  556.    ack = i2c_outb(adap,i);
  557.    i2c_stop(bit_adap);
  558.    if(ack>0)
  559.    {
  560.     CPK(printk("(%02x)",i>>1));
  561.    }
  562.    else
  563.     CPK(printk("."));
  564.   }
  565.   CPK(printk("\n"));
  566.  }
  567. #ifdef MODULE
  568.  MOD_INC_USE_COUNT;
  569. #endif
  570.  i2c_add_adapter(adap);
  571.  return 0;
  572. }
  573.  
  574. //------------------------------ i2c_bit_del_bus -------------------------------
  575. int i2c_bit_del_bus(struct i2c_adapter *adap)
  576. {
  577.  int res;
  578.  if((res=i2c_del_adapter(adap))<0)
  579.   return res;
  580.  CPK(printk("i2calgob: adapter unregistered: %s\n",adap->name));
  581. #ifdef MODULE
  582.  MOD_DEC_USE_COUNT;
  583. #endif
  584.  return 0;
  585. }