i2c-ite.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. Copyright 2001 MontaVista Software Inc.
  8. ----------------------------------------------------------------------------
  9. This file was highly leveraged from i2c-elektor.c, which was created
  10. by Simon G. Vogl and Hans Berglund:
  11. Copyright (C) 1995-97 Simon G. Vogl
  12. 1998-99 Hans Berglund
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  24. /* ------------------------------------------------------------------------- */
  25. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
  26. Frodo Looijaard <frodol@dds.nl> */
  27. #include <linux/kernel.h>
  28. #include <linux/ioport.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/wait.h>
  34. #include <asm/irq.h>
  35. #include <asm/io.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-algo-ite.h>
  38. #include <linux/i2c-adap-ite.h>
  39. #include "../i2c-ite.h"
  40. #define DEFAULT_BASE 0x14014030
  41. #define ITE_IIC_IO_SIZE 0x40
  42. #define DEFAULT_IRQ 0
  43. #define DEFAULT_CLOCK 0x1b0e /* default 16MHz/(27+14) = 400KHz */
  44. #define DEFAULT_OWN 0x55
  45. static int base;
  46. static int irq;
  47. static int clock;
  48. static int own;
  49. static struct iic_ite gpi;
  50. static wait_queue_head_t iic_wait;
  51. static int iic_pending;
  52. static spinlock_t lock;
  53. /* ----- local functions ---------------------------------------------- */
  54. static void iic_ite_setiic(void *data, int ctl, short val)
  55. {
  56. unsigned long j = jiffies + 10;
  57. pr_debug(" Write 0x%02x to 0x%x\n",(unsigned short)val, ctl&0xff);
  58. #ifdef DEBUG
  59. while (time_before(jiffies, j))
  60. schedule();
  61. #endif
  62. outw(val,ctl);
  63. }
  64. static short iic_ite_getiic(void *data, int ctl)
  65. {
  66. short val;
  67. val = inw(ctl);
  68. pr_debug("Read 0x%02x from 0x%x\n",(unsigned short)val, ctl&0xff);
  69. return (val);
  70. }
  71. /* Return our slave address. This is the address
  72. * put on the I2C bus when another master on the bus wants to address us
  73. * as a slave
  74. */
  75. static int iic_ite_getown(void *data)
  76. {
  77. return (gpi.iic_own);
  78. }
  79. static int iic_ite_getclock(void *data)
  80. {
  81. return (gpi.iic_clock);
  82. }
  83. /* Put this process to sleep. We will wake up when the
  84. * IIC controller interrupts.
  85. */
  86. static void iic_ite_waitforpin(void) {
  87. DEFINE_WAIT(wait);
  88. int timeout = 2;
  89. long flags;
  90. /* If interrupts are enabled (which they are), then put the process to
  91. * sleep. This process will be awakened by two events -- either the
  92. * the IIC peripheral interrupts or the timeout expires.
  93. * If interrupts are not enabled then delay for a reasonable amount
  94. * of time and return.
  95. */
  96. if (gpi.iic_irq > 0) {
  97. spin_lock_irqsave(&lock, flags);
  98. if (iic_pending == 0) {
  99. spin_unlock_irqrestore(&lock, flags);
  100. prepare_to_wait(&iic_wait, &wait, TASK_INTERRUPTIBLE);
  101. if (schedule_timeout(timeout*HZ)) {
  102. spin_lock_irqsave(&lock, flags);
  103. if (iic_pending == 1) {
  104. iic_pending = 0;
  105. }
  106. spin_unlock_irqrestore(&lock, flags);
  107. }
  108. finish_wait(&iic_wait, &wait);
  109. } else {
  110. iic_pending = 0;
  111. spin_unlock_irqrestore(&lock, flags);
  112. }
  113. } else {
  114. udelay(100);
  115. }
  116. }
  117. static irqreturn_t iic_ite_handler(int this_irq, void *dev_id,
  118. struct pt_regs *regs)
  119. {
  120. spin_lock(&lock);
  121. iic_pending = 1;
  122. spin_unlock(&lock);
  123. wake_up_interruptible(&iic_wait);
  124. return IRQ_HANDLED;
  125. }
  126. /* Lock the region of memory where I/O registers exist. Request our
  127. * interrupt line and register its associated handler.
  128. */
  129. static int iic_hw_resrc_init(void)
  130. {
  131. if (!request_region(gpi.iic_base, ITE_IIC_IO_SIZE, "i2c"))
  132. return -ENODEV;
  133. if (gpi.iic_irq <= 0)
  134. return 0;
  135. if (request_irq(gpi.iic_irq, iic_ite_handler, 0, "ITE IIC", 0) < 0)
  136. gpi.iic_irq = 0;
  137. else
  138. enable_irq(gpi.iic_irq);
  139. return 0;
  140. }
  141. static void iic_ite_release(void)
  142. {
  143. if (gpi.iic_irq > 0) {
  144. disable_irq(gpi.iic_irq);
  145. free_irq(gpi.iic_irq, 0);
  146. }
  147. release_region(gpi.iic_base , 2);
  148. }
  149. /* ------------------------------------------------------------------------
  150. * Encapsulate the above functions in the correct operations structure.
  151. * This is only done when more than one hardware adapter is supported.
  152. */
  153. static struct i2c_algo_iic_data iic_ite_data = {
  154. NULL,
  155. iic_ite_setiic,
  156. iic_ite_getiic,
  157. iic_ite_getown,
  158. iic_ite_getclock,
  159. iic_ite_waitforpin,
  160. 80, 80, 100, /* waits, timeout */
  161. };
  162. static struct i2c_adapter iic_ite_ops = {
  163. .owner = THIS_MODULE,
  164. .id = I2C_HW_I_IIC,
  165. .algo_data = &iic_ite_data,
  166. .name = "ITE IIC adapter",
  167. };
  168. /* Called when the module is loaded. This function starts the
  169. * cascade of calls up through the hierarchy of i2c modules (i.e. up to the
  170. * algorithm layer and into to the core layer)
  171. */
  172. static int __init iic_ite_init(void)
  173. {
  174. struct iic_ite *piic = &gpi;
  175. printk(KERN_INFO "Initialize ITE IIC adapter module\n");
  176. if (base == 0)
  177. piic->iic_base = DEFAULT_BASE;
  178. else
  179. piic->iic_base = base;
  180. if (irq == 0)
  181. piic->iic_irq = DEFAULT_IRQ;
  182. else
  183. piic->iic_irq = irq;
  184. if (clock == 0)
  185. piic->iic_clock = DEFAULT_CLOCK;
  186. else
  187. piic->iic_clock = clock;
  188. if (own == 0)
  189. piic->iic_own = DEFAULT_OWN;
  190. else
  191. piic->iic_own = own;
  192. iic_ite_data.data = (void *)piic;
  193. init_waitqueue_head(&iic_wait);
  194. spin_lock_init(&lock);
  195. if (iic_hw_resrc_init() == 0) {
  196. if (i2c_iic_add_bus(&iic_ite_ops) < 0)
  197. return -ENODEV;
  198. } else {
  199. return -ENODEV;
  200. }
  201. printk(KERN_INFO " found device at %#x irq %d.\n",
  202. piic->iic_base, piic->iic_irq);
  203. return 0;
  204. }
  205. static void iic_ite_exit(void)
  206. {
  207. i2c_iic_del_bus(&iic_ite_ops);
  208. iic_ite_release();
  209. }
  210. /* If modules is NOT defined when this file is compiled, then the MODULE_*
  211. * macros will resolve to nothing
  212. */
  213. MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
  214. MODULE_DESCRIPTION("I2C-Bus adapter routines for ITE IIC bus adapter");
  215. MODULE_LICENSE("GPL");
  216. module_param(base, int, 0);
  217. module_param(irq, int, 0);
  218. module_param(clock, int, 0);
  219. module_param(own, int, 0);
  220. /* Called when module is loaded or when kernel is initialized.
  221. * If MODULES is defined when this file is compiled, then this function will
  222. * resolve to init_module (the function called when insmod is invoked for a
  223. * module). Otherwise, this function is called early in the boot, when the
  224. * kernel is intialized. Check out /include/init.h to see how this works.
  225. */
  226. module_init(iic_ite_init);
  227. /* Resolves to module_cleanup when MODULES is defined. */
  228. module_exit(iic_ite_exit);