home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / I2C / i2c-adap-ite.c next >
C/C++ Source or Header  |  2001-10-11  |  8KB  |  321 lines

  1. /*
  2.    -------------------------------------------------------------------------
  3.    i2c-adap-ite.c i2c-hw access for the IIC peripheral on the ITE MIPS system
  4.    -------------------------------------------------------------------------
  5.    Hai-Pao Fan, MontaVista Software, Inc.
  6.    hpfan@mvista.com or source@mvista.com
  7.  
  8.    Copyright 2001 MontaVista Software Inc.
  9.  
  10.    ----------------------------------------------------------------------------
  11.    This file was highly leveraged from i2c-elektor.c, which was created
  12.    by Simon G. Vogl and Hans Berglund:
  13.  
  14.  
  15.      Copyright (C) 1995-97 Simon G. Vogl
  16.                    1998-99 Hans Berglund
  17.  
  18.     This program is free software; you can redistribute it and/or modify
  19.     it under the terms of the GNU General Public License as published by
  20.     the Free Software Foundation; either version 2 of the License, or
  21.     (at your option) any later version.
  22.  
  23.     This program is distributed in the hope that it will be useful,
  24.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  25.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.     GNU General Public License for more details.
  27.  
  28.     You should have received a copy of the GNU General Public License
  29.     along with this program; if not, write to the Free Software
  30.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
  31. /* ------------------------------------------------------------------------- */
  32.  
  33. /* With some changes from Ky÷sti MΣlkki <kmalkki@cc.hut.fi> and even
  34.    Frodo Looijaard <frodol@dds.nl> */
  35.  
  36. #include <linux/kernel.h>
  37. #include <linux/ioport.h>
  38. #include <linux/module.h>
  39. #include <linux/delay.h>
  40. #include <linux/slab.h>
  41. #include <linux/version.h>
  42. #include <linux/init.h>
  43. #include <asm/irq.h>
  44. #include <asm/io.h>
  45.  
  46. #include <linux/i2c.h>
  47. #include <linux/i2c-algo-ite.h>
  48. #include <linux/i2c-adap-ite.h>
  49. #include "i2c-ite.h"
  50.  
  51. #define DEFAULT_BASE  0x14014030
  52. #define ITE_IIC_IO_SIZE    0x40
  53. #define DEFAULT_IRQ   0
  54. #define DEFAULT_CLOCK 0x1b0e    /* default 16MHz/(27+14) = 400KHz */
  55. #define DEFAULT_OWN   0x55
  56.  
  57. static int base  = 0;
  58. static int irq   = 0;
  59. static int clock = 0;
  60. static int own   = 0;
  61.  
  62. static int i2c_debug=0;
  63. static struct iic_ite gpi;
  64. #if (LINUX_VERSION_CODE < 0x020301)
  65. static struct wait_queue *iic_wait = NULL;
  66. #else
  67. static wait_queue_head_t iic_wait;
  68. #endif
  69. static int iic_pending;
  70.  
  71. /* ----- global defines -----------------------------------------------    */
  72. #define DEB(x)    if (i2c_debug>=1) x
  73. #define DEB2(x) if (i2c_debug>=2) x
  74. #define DEB3(x) if (i2c_debug>=3) x
  75. #define DEBE(x)    x    /* error messages                 */
  76.  
  77.  
  78. /* ----- local functions ----------------------------------------------    */
  79.  
  80. static void iic_ite_setiic(void *data, int ctl, short val)
  81. {
  82.         unsigned long j = jiffies + 10;
  83.  
  84.     DEB3(printk(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff));
  85.     DEB3({while (jiffies < j) schedule();}) 
  86.     outw(val,ctl);
  87. }
  88.  
  89. static short iic_ite_getiic(void *data, int ctl)
  90. {
  91.     short val;
  92.  
  93.     val = inw(ctl);
  94.     DEB3(printk("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff));  
  95.     return (val);
  96. }
  97.  
  98. /* Return our slave address.  This is the address
  99.  * put on the I2C bus when another master on the bus wants to address us
  100.  * as a slave
  101.  */
  102. static int iic_ite_getown(void *data)
  103. {
  104.     return (gpi.iic_own);
  105. }
  106.  
  107.  
  108. static int iic_ite_getclock(void *data)
  109. {
  110.     return (gpi.iic_clock);
  111. }
  112.  
  113.  
  114. #if 0
  115. static void iic_ite_sleep(unsigned long timeout)
  116. {
  117.     schedule_timeout( timeout * HZ);
  118. }
  119. #endif
  120.  
  121.  
  122. /* Put this process to sleep.  We will wake up when the
  123.  * IIC controller interrupts.
  124.  */
  125. static void iic_ite_waitforpin(void) {
  126.  
  127.    int timeout = 2;
  128.  
  129.    /* If interrupts are enabled (which they are), then put the process to
  130.     * sleep.  This process will be awakened by two events -- either the
  131.     * the IIC peripheral interrupts or the timeout expires. 
  132.     * If interrupts are not enabled then delay for a reasonable amount 
  133.     * of time and return.
  134.     */
  135.    if (gpi.iic_irq > 0) {
  136.     cli();
  137.     if (iic_pending == 0) {
  138.         interruptible_sleep_on_timeout(&iic_wait, timeout*HZ );
  139.     } else
  140.         iic_pending = 0;
  141.     sti();
  142.    } else {
  143.       udelay(100);
  144.    }
  145. }
  146.  
  147.  
  148. static void iic_ite_handler(int this_irq, void *dev_id, struct pt_regs *regs) 
  149. {
  150.     
  151.    iic_pending = 1;
  152.  
  153.    DEB2(printk("iic_ite_handler: in interrupt handler\n"));
  154.    wake_up_interruptible(&iic_wait);
  155. }
  156.  
  157.  
  158. /* Lock the region of memory where I/O registers exist.  Request our
  159.  * interrupt line and register its associated handler.
  160.  */
  161. static int iic_hw_resrc_init(void)
  162. {
  163.       if (check_region(gpi.iic_base, ITE_IIC_IO_SIZE) < 0 ) {
  164.           return -ENODEV;
  165.       } else {
  166.          request_region(gpi.iic_base, ITE_IIC_IO_SIZE, 
  167.         "i2c (i2c bus adapter)");
  168.       }
  169.     if (gpi.iic_irq > 0) {
  170.        if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0) {
  171.           gpi.iic_irq = 0;
  172.        } else
  173.           DEB3(printk("Enabled IIC IRQ %d\n", gpi.iic_irq));
  174.           enable_irq(gpi.iic_irq);
  175.     }
  176.     return 0;
  177. }
  178.  
  179.  
  180. static void iic_ite_release(void)
  181. {
  182.     if (gpi.iic_irq > 0) {
  183.         disable_irq(gpi.iic_irq);
  184.         free_irq(gpi.iic_irq, 0);
  185.     }
  186.     release_region(gpi.iic_base , 2);
  187. }
  188.  
  189.  
  190. static int iic_ite_reg(struct i2c_client *client)
  191. {
  192.     return 0;
  193. }
  194.  
  195.  
  196. static int iic_ite_unreg(struct i2c_client *client)
  197. {
  198.     return 0;
  199. }
  200.  
  201.  
  202. static void iic_ite_inc_use(struct i2c_adapter *adap)
  203. {
  204. #ifdef MODULE
  205.     MOD_INC_USE_COUNT;
  206. #endif
  207. }
  208.  
  209.  
  210. static void iic_ite_dec_use(struct i2c_adapter *adap)
  211. {
  212. #ifdef MODULE
  213.     MOD_DEC_USE_COUNT;
  214. #endif
  215. }
  216.  
  217.  
  218. /* ------------------------------------------------------------------------
  219.  * Encapsulate the above functions in the correct operations structure.
  220.  * This is only done when more than one hardware adapter is supported.
  221.  */
  222. static struct i2c_algo_iic_data iic_ite_data = {
  223.     NULL,
  224.     iic_ite_setiic,
  225.     iic_ite_getiic,
  226.     iic_ite_getown,
  227.     iic_ite_getclock,
  228.     iic_ite_waitforpin,
  229.     80, 80, 100,        /*    waits, timeout */
  230. };
  231.  
  232. static struct i2c_adapter iic_ite_ops = {
  233.     "ITE IIC adapter",
  234.     I2C_HW_I_IIC,
  235.     NULL,
  236.     &iic_ite_data,
  237.     iic_ite_inc_use,
  238.     iic_ite_dec_use,
  239.     iic_ite_reg,
  240.     iic_ite_unreg,
  241. };
  242.  
  243. /* Called when the module is loaded.  This function starts the
  244.  * cascade of calls up through the heirarchy of i2c modules (i.e. up to the
  245.  *  algorithm layer and into to the core layer)
  246.  */
  247. static int __init iic_ite_init(void) 
  248. {
  249.  
  250.     struct iic_ite *piic = &gpi;
  251.  
  252.     printk(KERN_INFO "Initialize ITE IIC adapter module\n");
  253.     if (base == 0)
  254.         piic->iic_base = DEFAULT_BASE;
  255.     else
  256.         piic->iic_base = base;
  257.  
  258.     if (irq == 0)
  259.         piic->iic_irq = DEFAULT_IRQ;
  260.     else
  261.         piic->iic_irq = irq;
  262.  
  263.     if (clock == 0)
  264.         piic->iic_clock = DEFAULT_CLOCK;
  265.     else
  266.         piic->iic_clock = clock;
  267.  
  268.     if (own == 0)
  269.         piic->iic_own = DEFAULT_OWN;
  270.     else
  271.         piic->iic_own = own;
  272.  
  273.     iic_ite_data.data = (void *)piic;
  274. #if (LINUX_VERSION_CODE >= 0x020301)
  275.     init_waitqueue_head(&iic_wait);
  276. #endif
  277.     if (iic_hw_resrc_init() == 0) {
  278.         if (i2c_iic_add_bus(&iic_ite_ops) < 0)
  279.             return -ENODEV;
  280.     } else {
  281.         return -ENODEV;
  282.     }
  283.     printk(KERN_INFO " found device at %#x irq %d.\n", 
  284.         piic->iic_base, piic->iic_irq);
  285.     return 0;
  286. }
  287.  
  288.  
  289. static void iic_ite_exit(void)
  290. {
  291.     i2c_iic_del_bus(&iic_ite_ops);
  292.         iic_ite_release();
  293. }
  294.  
  295. EXPORT_NO_SYMBOLS;
  296.  
  297. /* If modules is NOT defined when this file is compiled, then the MODULE_*
  298.  * macros will resolve to nothing
  299.  */
  300. MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
  301. MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
  302. MODULE_LICENSE("GPL");
  303.  
  304. MODULE_PARM(base, "i");
  305. MODULE_PARM(irq, "i");
  306. MODULE_PARM(clock, "i");
  307. MODULE_PARM(own, "i");
  308. MODULE_PARM(i2c_debug,"i");
  309.  
  310.  
  311. /* Called when module is loaded or when kernel is intialized.
  312.  * If MODULES is defined when this file is compiled, then this function will
  313.  * resolve to init_module (the function called when insmod is invoked for a
  314.  * module).  Otherwise, this function is called early in the boot, when the
  315.  * kernel is intialized.  Check out /include/init.h to see how this works.
  316.  */
  317. module_init(iic_ite_init);
  318.  
  319. /* Resolves to module_cleanup when MODULES is defined. */
  320. module_exit(iic_ite_exit); 
  321.