home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / I2C / i2c-algo-pcf.c < prev    next >
C/C++ Source or Header  |  2001-10-11  |  14KB  |  557 lines

  1. /* ------------------------------------------------------------------------- */
  2. /* i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters             */
  3. /* ------------------------------------------------------------------------- */
  4. /*   Copyright (C) 1995-1997 Simon G. Vogl
  5.                    1998-2000 Hans Berglund
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
  20. /* ------------------------------------------------------------------------- */
  21.  
  22. /* With some changes from Ky÷sti MΣlkki <kmalkki@cc.hut.fi> and 
  23.    Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
  24.    <mbailey@littlefeet-inc.com> */
  25.  
  26. /* Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
  27.    messages, proper stop/repstart signaling during receive,
  28.    added detect code */
  29.  
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/version.h>
  35. #include <linux/init.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/ioport.h>
  38. #include <linux/errno.h>
  39. #include <linux/sched.h>
  40.  
  41. #include <linux/i2c.h>
  42. #include <linux/i2c-algo-pcf.h>
  43. #include "i2c-pcf8584.h"
  44.  
  45. /* ----- global defines ----------------------------------------------- */
  46. #define DEB(x) if (i2c_debug>=1) x
  47. #define DEB2(x) if (i2c_debug>=2) x
  48. #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
  49. #define DEBPROTO(x) if (i2c_debug>=9) x;
  50.      /* debug the protocol by showing transferred bits */
  51. #define DEF_TIMEOUT 16
  52.  
  53. /* module parameters:
  54.  */
  55. static int i2c_debug=0;
  56. static int pcf_scan=0;    /* have a look at what's hanging 'round        */
  57.  
  58. /* --- setting states on the bus with the right timing: ---------------    */
  59.  
  60. #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
  61. #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
  62. #define get_own(adap) adap->getown(adap->data)
  63. #define get_clock(adap) adap->getclock(adap->data)
  64. #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
  65. #define i2c_inb(adap) adap->getpcf(adap->data, 0)
  66.  
  67. /* --- other auxiliary functions --------------------------------------    */
  68.  
  69. static void i2c_start(struct i2c_algo_pcf_data *adap) 
  70. {
  71.     DEBPROTO(printk("S "));
  72.     set_pcf(adap, 1, I2C_PCF_START);
  73. }
  74.  
  75. static void i2c_repstart(struct i2c_algo_pcf_data *adap) 
  76. {
  77.     DEBPROTO(printk(" Sr "));
  78.     set_pcf(adap, 1, I2C_PCF_REPSTART);
  79. }
  80.  
  81.  
  82. static void i2c_stop(struct i2c_algo_pcf_data *adap) 
  83. {
  84.     DEBPROTO(printk("P\n"));
  85.     set_pcf(adap, 1, I2C_PCF_STOP);
  86. }
  87.  
  88.  
  89. static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
  90.  
  91.     int timeout = DEF_TIMEOUT;
  92.     int status;
  93.  
  94.     status = get_pcf(adap, 1);
  95. #ifndef STUB_I2C
  96.     while (timeout-- && !(status & I2C_PCF_BB)) {
  97.         udelay(100); /* wait for 100 us */
  98.         status = get_pcf(adap, 1);
  99.     }
  100. #endif
  101.     if (timeout <= 0) {
  102.         printk("Timeout waiting for Bus Busy\n");
  103.     }
  104.     
  105.     return (timeout<=0);
  106. }
  107.  
  108.  
  109. static inline void pcf_sleep(unsigned long timeout)
  110. {
  111.     schedule_timeout( timeout * HZ);
  112. }
  113.  
  114.  
  115. static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
  116.  
  117.     int timeout = DEF_TIMEOUT;
  118.  
  119.     *status = get_pcf(adap, 1);
  120. #ifndef STUB_I2C
  121.     while (timeout-- && (*status & I2C_PCF_PIN)) {
  122.         adap->waitforpin();
  123.         *status = get_pcf(adap, 1);
  124.     }
  125. #endif
  126.     if (timeout <= 0)
  127.         return(-1);
  128.     else
  129.         return(0);
  130. }
  131.  
  132. /* 
  133.  * This should perform the 'PCF8584 initialization sequence' as described
  134.  * in the Philips IC12 data book (1995, Aug 29).
  135.  * There should be a 30 clock cycle wait after reset, I assume this
  136.  * has been fulfilled.
  137.  * There should be a delay at the end equal to the longest I2C message
  138.  * to synchronize the BB-bit (in multimaster systems). How long is
  139.  * this? I assume 1 second is always long enough.
  140.  *
  141.  * vdovikin: added detect code for PCF8584
  142.  */
  143. static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
  144. {
  145.     unsigned char temp;
  146.  
  147.     DEB3(printk("i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
  148.  
  149.     /* S1=0x80: S0 selected, serial interface off */
  150.     set_pcf(adap, 1, I2C_PCF_PIN);
  151.     /* check to see S1 now used as R/W ctrl -
  152.        PCF8584 does that when ESO is zero */
  153.     /* PCF also resets PIN bit */
  154.     if ((temp = get_pcf(adap, 1)) != (0)) {
  155.         DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
  156.         return -ENXIO; /* definetly not PCF8584 */
  157.     }
  158.  
  159.     /* load own address in S0, effective address is (own << 1)    */
  160.     i2c_outb(adap, get_own(adap));
  161.     /* check it's realy writen */
  162.     if ((temp = i2c_inb(adap)) != get_own(adap)) {
  163.         DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
  164.         return -ENXIO;
  165.     }
  166.  
  167.     /* S1=0xA0, next byte in S2                    */
  168.     set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
  169.     /* check to see S2 now selected */
  170.     if ((temp = get_pcf(adap, 1)) != I2C_PCF_ES1) {
  171.         DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
  172.         return -ENXIO;
  173.     }
  174.  
  175.     /* load clock register S2                    */
  176.     i2c_outb(adap, get_clock(adap));
  177.     /* check it's realy writen, the only 5 lowest bits does matter */
  178.     if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
  179.         DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
  180.         return -ENXIO;
  181.     }
  182.  
  183.     /* Enable serial interface, idle, S0 selected            */
  184.     set_pcf(adap, 1, I2C_PCF_IDLE);
  185.  
  186.     /* check to see PCF is realy idled and we can access status register */
  187.     if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
  188.         DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
  189.         return -ENXIO;
  190.     }
  191.     
  192.     printk("i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
  193.  
  194.     return 0;
  195. }
  196.  
  197.  
  198. /* ----- Utility functions
  199.  */
  200.  
  201. static inline int try_address(struct i2c_algo_pcf_data *adap,
  202.                unsigned char addr, int retries)
  203. {
  204.     int i, status, ret = -1;
  205.     for (i=0;i<retries;i++) {
  206.         i2c_outb(adap, addr);
  207.         i2c_start(adap);
  208.         status = get_pcf(adap, 1);
  209.         if (wait_for_pin(adap, &status) >= 0) {
  210.             if ((status & I2C_PCF_LRB) == 0) { 
  211.                 i2c_stop(adap);
  212.                 break;    /* success! */
  213.             }
  214.         }
  215.         i2c_stop(adap);
  216.         udelay(adap->udelay);
  217.     }
  218.     DEB2(if (i) printk("i2c-algo-pcf.o: needed %d retries for %d\n",i,
  219.                        addr));
  220.     return ret;
  221. }
  222.  
  223.  
  224. static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
  225.                          int count, int last)
  226. {
  227.     struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  228.     int wrcount, status, timeout;
  229.     
  230.     for (wrcount=0; wrcount<count; ++wrcount) {
  231.         DEB2(printk("i2c-algo-pcf.o: %s i2c_write: writing %2.2X\n",
  232.               i2c_adap->name, buf[wrcount]&0xff));
  233.         i2c_outb(adap, buf[wrcount]);
  234.         timeout = wait_for_pin(adap, &status);
  235.         if (timeout) {
  236.             i2c_stop(adap);
  237.             printk("i2c-algo-pcf.o: %s i2c_write: "
  238.                    "error - timeout.\n", i2c_adap->name);
  239.             return -EREMOTEIO; /* got a better one ?? */
  240.         }
  241. #ifndef STUB_I2C
  242.         if (status & I2C_PCF_LRB) {
  243.             i2c_stop(adap);
  244.             printk("i2c-algo-pcf.o: %s i2c_write: "
  245.                    "error - no ack.\n", i2c_adap->name);
  246.             return -EREMOTEIO; /* got a better one ?? */
  247.         }
  248. #endif
  249.     }
  250.     if (last) {
  251.         i2c_stop(adap);
  252.     }
  253.     else {
  254.         i2c_repstart(adap);
  255.     }
  256.  
  257.     return (wrcount);
  258. }
  259.  
  260.  
  261. static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
  262.                          int count, int last)
  263. {
  264.     int i, status;
  265.     struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  266.  
  267.     /* increment number of bytes to read by one -- read dummy byte */
  268.     for (i = 0; i <= count; i++) {
  269.  
  270.         if (wait_for_pin(adap, &status)) {
  271.             i2c_stop(adap);
  272.             printk("i2c-algo-pcf.o: pcf_readbytes timed out.\n");
  273.             return (-1);
  274.         }
  275.  
  276. #ifndef STUB_I2C
  277.         if ((status & I2C_PCF_LRB) && (i != count)) {
  278.             i2c_stop(adap);
  279.             printk("i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.\n");
  280.             return (-1);
  281.         }
  282. #endif
  283.         
  284.         if (i == count - 1) {
  285.             set_pcf(adap, 1, I2C_PCF_ESO);
  286.         } else 
  287.         if (i == count) {
  288.             if (last) {
  289.                 i2c_stop(adap);
  290.             } else {
  291.                 i2c_repstart(adap);
  292.             }
  293.         };
  294.  
  295.         if (i) {
  296.             buf[i - 1] = i2c_inb(adap);
  297.         } else {
  298.             i2c_inb(adap); /* dummy read */
  299.         }
  300.     }
  301.  
  302.     return (i - 1);
  303. }
  304.  
  305.  
  306. static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
  307.                                 struct i2c_msg *msg, int retries) 
  308. {
  309.     unsigned short flags = msg->flags;
  310.     unsigned char addr;
  311.     int ret;
  312.     if ( (flags & I2C_M_TEN)  ) { 
  313.         /* a ten bit address */
  314.         addr = 0xf0 | (( msg->addr >> 7) & 0x03);
  315.         DEB2(printk("addr0: %d\n",addr));
  316.         /* try extended address code...*/
  317.         ret = try_address(adap, addr, retries);
  318.         if (ret!=1) {
  319.             printk("died at extended address code.\n");
  320.             return -EREMOTEIO;
  321.         }
  322.         /* the remaining 8 bit address */
  323.         i2c_outb(adap,msg->addr & 0x7f);
  324. /* Status check comes here */
  325.         if (ret != 1) {
  326.             printk("died at 2nd address code.\n");
  327.             return -EREMOTEIO;
  328.         }
  329.         if ( flags & I2C_M_RD ) {
  330.             i2c_repstart(adap);
  331.             /* okay, now switch into reading mode */
  332.             addr |= 0x01;
  333.             ret = try_address(adap, addr, retries);
  334.             if (ret!=1) {
  335.                 printk("died at extended address code.\n");
  336.                 return -EREMOTEIO;
  337.             }
  338.         }
  339.     } else {        /* normal 7bit address    */
  340.         addr = ( msg->addr << 1 );
  341.         if (flags & I2C_M_RD )
  342.             addr |= 1;
  343.         if (flags & I2C_M_REV_DIR_ADDR )
  344.             addr ^= 1;
  345.         i2c_outb(adap, addr);
  346.     }
  347.     return 0;
  348. }
  349.  
  350. static int pcf_xfer(struct i2c_adapter *i2c_adap,
  351.             struct i2c_msg msgs[], 
  352.             int num)
  353. {
  354.     struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
  355.     struct i2c_msg *pmsg;
  356.     int i;
  357.     int ret=0, timeout, status;
  358.     
  359.  
  360.     /* Check for bus busy */
  361.     timeout = wait_for_bb(adap);
  362.     if (timeout) {
  363.         DEB2(printk("i2c-algo-pcf.o: "
  364.                     "Timeout waiting for BB in pcf_xfer\n");)
  365.         return -EIO;
  366.     }
  367.     
  368.     for (i = 0;ret >= 0 && i < num; i++) {
  369.         pmsg = &msgs[i];
  370.  
  371.         DEB2(printk("i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  372.              pmsg->flags & I2C_M_RD ? "read" : "write",
  373.                      pmsg->len, pmsg->addr, i + 1, num);)
  374.     
  375.         ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
  376.  
  377.         /* Send START */
  378.         if (i == 0) {
  379.             i2c_start(adap); 
  380.         }
  381.     
  382.         /* Wait for PIN (pending interrupt NOT) */
  383.         timeout = wait_for_pin(adap, &status);
  384.         if (timeout) {
  385.             i2c_stop(adap);
  386.             DEB2(printk("i2c-algo-pcf.o: Timeout waiting "
  387.                     "for PIN(1) in pcf_xfer\n");)
  388.             return (-EREMOTEIO);
  389.         }
  390.     
  391. #ifndef STUB_I2C
  392.         /* Check LRB (last rcvd bit - slave ack) */
  393.         if (status & I2C_PCF_LRB) {
  394.             i2c_stop(adap);
  395.             DEB2(printk("i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
  396.             return (-EREMOTEIO);
  397.         }
  398. #endif
  399.     
  400.         DEB3(printk("i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
  401.                 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
  402.     
  403.         /* Read */
  404.         if (pmsg->flags & I2C_M_RD) {
  405.             /* read bytes into buffer*/
  406.             ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
  407.                                             (i + 1 == num));
  408.         
  409.             if (ret != pmsg->len) {
  410.                 DEB2(printk("i2c-algo-pcf.o: fail: "
  411.                         "only read %d bytes.\n",ret));
  412.             } else {
  413.                 DEB2(printk("i2c-algo-pcf.o: read %d bytes.\n",ret));
  414.             }
  415.         } else { /* Write */
  416.             ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
  417.                                             (i + 1 == num));
  418.         
  419.             if (ret != pmsg->len) {
  420.                 DEB2(printk("i2c-algo-pcf.o: fail: "
  421.                         "only wrote %d bytes.\n",ret));
  422.             } else {
  423.                 DEB2(printk("i2c-algo-pcf.o: wrote %d bytes.\n",ret));
  424.             }
  425.         }
  426.     }
  427.  
  428.     return (i);
  429. }
  430.  
  431. static int algo_control(struct i2c_adapter *adapter, 
  432.     unsigned int cmd, unsigned long arg)
  433. {
  434.     return 0;
  435. }
  436.  
  437. static u32 pcf_func(struct i2c_adapter *adap)
  438. {
  439.     return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | 
  440.            I2C_FUNC_PROTOCOL_MANGLING; 
  441. }
  442.  
  443. /* -----exported algorithm data: -------------------------------------    */
  444.  
  445. static struct i2c_algorithm pcf_algo = {
  446.     "PCF8584 algorithm",
  447.     I2C_ALGO_PCF,
  448.     pcf_xfer,
  449.     NULL,
  450.     NULL,                /* slave_xmit        */
  451.     NULL,                /* slave_recv        */
  452.     algo_control,            /* ioctl        */
  453.     pcf_func,            /* functionality    */
  454. };
  455.  
  456. /* 
  457.  * registering functions to load algorithms at runtime 
  458.  */
  459. int i2c_pcf_add_bus(struct i2c_adapter *adap)
  460. {
  461.     int i, status;
  462.     struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
  463.  
  464.     DEB2(printk("i2c-algo-pcf.o: hw routines for %s registered.\n",
  465.                 adap->name));
  466.  
  467.     /* register new adapter to i2c module... */
  468.  
  469.     adap->id |= pcf_algo.id;
  470.     adap->algo = &pcf_algo;
  471.  
  472.     adap->timeout = 100;        /* default values, should    */
  473.     adap->retries = 3;        /* be replaced by defines    */
  474.  
  475.     if ((i = pcf_init_8584(pcf_adap))) {
  476.         return i;
  477.     }
  478.  
  479. #ifdef MODULE
  480.     MOD_INC_USE_COUNT;
  481. #endif
  482.  
  483.     i2c_add_adapter(adap);
  484.  
  485.     /* scan bus */
  486.     if (pcf_scan) {
  487.         printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.\n",
  488.                adap->name);
  489.         for (i = 0x00; i < 0xff; i+=2) {
  490.             if (wait_for_bb(pcf_adap)) {
  491.                 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s - TIMEOUTed.\n",
  492.                    adap->name);
  493.                 break;
  494.             }
  495.             i2c_outb(pcf_adap, i);
  496.             i2c_start(pcf_adap);
  497.             if ((wait_for_pin(pcf_adap, &status) >= 0) && 
  498.                 ((status & I2C_PCF_LRB) == 0)) { 
  499.                 printk("(%02x)",i>>1); 
  500.             } else {
  501.                 printk("."); 
  502.             }
  503.             i2c_stop(pcf_adap);
  504.             udelay(pcf_adap->udelay);
  505.         }
  506.         printk("\n");
  507.     }
  508.     return 0;
  509. }
  510.  
  511.  
  512. int i2c_pcf_del_bus(struct i2c_adapter *adap)
  513. {
  514.     int res;
  515.     if ((res = i2c_del_adapter(adap)) < 0)
  516.         return res;
  517.     DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %s\n",adap->name));
  518.  
  519. #ifdef MODULE
  520.     MOD_DEC_USE_COUNT;
  521. #endif
  522.     return 0;
  523. }
  524.  
  525. int __init i2c_algo_pcf_init (void)
  526. {
  527.     printk("i2c-algo-pcf.o: i2c pcf8584 algorithm module\n");
  528.     return 0;
  529. }
  530.  
  531.  
  532. EXPORT_SYMBOL(i2c_pcf_add_bus);
  533. EXPORT_SYMBOL(i2c_pcf_del_bus);
  534.  
  535. #ifdef MODULE
  536. MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
  537. MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
  538. MODULE_LICENSE("GPL");
  539.  
  540. MODULE_PARM(pcf_scan, "i");
  541. MODULE_PARM(i2c_debug,"i");
  542.  
  543. MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
  544. MODULE_PARM_DESC(i2c_debug,
  545.         "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
  546.  
  547.  
  548. int init_module(void) 
  549. {
  550.     return i2c_algo_pcf_init();
  551. }
  552.  
  553. void cleanup_module(void) 
  554. {
  555. }
  556. #endif
  557.