au1000_gpio.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * FILE NAME au1000_gpio.c
  3.  *
  4.  * BRIEF MODULE DESCRIPTION
  5.  *  Driver for Alchemy Au1000 GPIO.
  6.  *
  7.  *  Author: MontaVista Software, Inc.  <source@mvista.com>
  8.  *          Steve Longerbeam <stevel@mvista.com>
  9.  *
  10.  * Copyright 2001 MontaVista Software Inc.
  11.  *
  12.  *  This program is free software; you can redistribute  it and/or modify it
  13.  *  under  the terms of  the GNU General  Public License as published by the
  14.  *  Free Software Foundation;  either version 2 of the  License, or (at your
  15.  *  option) any later version.
  16.  *
  17.  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
  18.  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
  19.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  20.  *  NO  EVENT  SHALL   THE AUTHOR  BE LIABLE FOR ANY   DIRECT, INDIRECT,
  21.  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
  23.  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24.  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
  25.  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  *  You should have received a copy of the  GNU General Public License along
  29.  *  with this program; if not, write  to the Free Software Foundation, Inc.,
  30.  *  675 Mass Ave, Cambridge, MA 02139, USA.
  31.  */
  32. #include <linux/module.h>
  33. #include <linux/config.h>
  34. #include <linux/types.h>
  35. #include <linux/kernel.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/init.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/io.h>
  40. #include <asm/au1000.h>
  41. #include <asm/au1000_gpio.h>
  42. #define VERSION "0.01"
  43. static const struct {
  44. u32 active_hi;
  45. u32 avail_mask;
  46. } pinfunc_to_avail[15] = {
  47. {1,  0x7<<16},   // 0  = SSI0     / GPIO[18:16]
  48. {-1, 0},         // 1  = AC97     / SSI1
  49. {1,  1<<19},     // 2  = IRDA     / GPIO19
  50. {1,  1<<20},     // 3  = UART0    / GPIO20
  51. {1,  0x1f<<24},  // 4  = NIC2     / GPIO[28:24]
  52. {1,  0x7<<29},   // 5  = I2S      / GPIO[31:29]
  53. {0,  1<<8},      // 6  = I2SDI    / GPIO8
  54. {0,  0x3f<<9},   // 7  = UART3    / GPIO[14:9]
  55. {0,  1<<15},     // 8  = IRFIRSEL / GPIO15
  56. {0,  1<<2},      // 9  = EXTCLK0 or OSC / GPIO2
  57. {0,  1<<3},      // 10 = EXTCLK1  / GPIO3
  58. {0,  1<<6},      // 11 = SMROMCKE / GPIO6
  59. {1,  1<<21},     // 12 = UART1    / GPIO21
  60. {1,  1<<22},     // 13 = UART2    / GPIO22
  61. {1,  1<<23}      // 14 = UART3    / GPIO23
  62. };
  63. u32 get_au1000_avail_gpio_mask(void)
  64. {
  65. int i;
  66. u32 pinfunc = inl(SYS_PINFUNC);
  67. u32 avail_mask = 0; // start with no gpio available
  68. // first, check for GPIO's reprogrammed as peripheral pins
  69. for (i=0; i<15; i++) {
  70. if (pinfunc_to_avail[i].active_hi < 0)
  71. continue;
  72. if (!(pinfunc_to_avail[i].active_hi ^
  73.       ((pinfunc & (1<<i)) ? 1:0)))
  74. avail_mask |= pinfunc_to_avail[i].avail_mask;
  75. }
  76. // check for GPIO's used as interrupt sources
  77. avail_mask &= ~(inl(IC1_MASKRD) &
  78. (inl(IC1_CFG0RD) | inl(IC1_CFG1RD)));
  79. #ifdef CONFIG_USB_OHCI
  80. avail_mask &= ~((1<<4) | (1<<11));
  81. #ifndef CONFIG_AU1000_USB_DEVICE
  82. avail_mask &= ~((1<<5) | (1<<13));
  83. #endif
  84. #endif
  85. return avail_mask;
  86. }
  87. /*
  88.  * Tristate the requested GPIO pins specified in data.
  89.  * Only available GPIOs will be tristated.
  90.  */
  91. int au1000gpio_tristate(u32 data)
  92. {
  93. data &= get_au1000_avail_gpio_mask();
  94. if (data)
  95. outl(data, SYS_TRIOUTCLR);
  96. return 0;
  97. }
  98. /*
  99.  * Return the pin state. Pins configured as outputs will return
  100.  * the output state, and pins configured as inputs (tri-stated)
  101.  * will return input pin state.
  102.  */
  103. int au1000gpio_in(u32 *data)
  104. {
  105. *data = inl(SYS_PINSTATERD);
  106. return 0;
  107. }
  108. /*
  109.  * Set/clear GPIO pins. Only available GPIOs will be affected.
  110.  */
  111. int au1000gpio_set(u32 data)
  112. {
  113. data &= get_au1000_avail_gpio_mask();
  114. if (data)
  115. outl(data, SYS_OUTPUTSET);
  116. return 0;
  117. }
  118. int au1000gpio_clear(u32 data)
  119. {
  120. data &= get_au1000_avail_gpio_mask();
  121. if (data)
  122. outl(data, SYS_OUTPUTCLR);
  123. return 0;
  124. }
  125. /*
  126.  * Output data to GPIO pins. Only available GPIOs will be affected.
  127.  */
  128. int au1000gpio_out(u32 data)
  129. {
  130. au1000gpio_set(data);
  131. au1000gpio_clear(~data);
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(get_au1000_avail_gpio_mask);
  135. EXPORT_SYMBOL(au1000gpio_tristate);
  136. EXPORT_SYMBOL(au1000gpio_in);
  137. EXPORT_SYMBOL(au1000gpio_set);
  138. EXPORT_SYMBOL(au1000gpio_clear);
  139. EXPORT_SYMBOL(au1000gpio_out);
  140. static int au1000gpio_open(struct inode *inode, struct file *file)
  141. {
  142. MOD_INC_USE_COUNT;
  143. return 0;
  144. }
  145. static int au1000gpio_release(struct inode *inode, struct file *file)
  146. {
  147. MOD_DEC_USE_COUNT;
  148. return 0;
  149. }
  150. static int au1000gpio_ioctl(struct inode *inode, struct file *file,
  151.     unsigned int cmd, unsigned long arg)
  152. {
  153. int status;
  154. u32 val;
  155. switch(cmd) {
  156. case AU1000GPIO_IN:
  157. status = au1000gpio_in(&val);
  158. if (status != 0)
  159. return status;
  160. return put_user(val, (u32 *)arg);
  161. case AU1000GPIO_OUT:
  162. if (get_user(val, (u32 *)arg)) 
  163. return -EFAULT;
  164. return au1000gpio_out(val);
  165. case AU1000GPIO_SET:
  166. if (get_user(val, (u32 *)arg)) 
  167. return -EFAULT;
  168. return au1000gpio_set(val);
  169. case AU1000GPIO_CLEAR:
  170. if (get_user(val, (u32 *)arg)) 
  171. return -EFAULT;
  172. return au1000gpio_clear(val);
  173. case AU1000GPIO_TRISTATE:
  174. if (get_user(val, (u32 *)arg)) 
  175. return -EFAULT;
  176. return au1000gpio_tristate(val);
  177. case AU1000GPIO_AVAIL_MASK:
  178. return put_user(get_au1000_avail_gpio_mask(),
  179. (u32 *)arg);
  180. default:
  181. return -ENOIOCTLCMD;
  182. }
  183. return 0;
  184. }
  185. static struct file_operations au1000gpio_fops =
  186. {
  187. owner: THIS_MODULE,
  188. ioctl: au1000gpio_ioctl,
  189. open: au1000gpio_open,
  190. release: au1000gpio_release,
  191. };
  192. static struct miscdevice au1000gpio_miscdev =
  193. {
  194. GPIO_MINOR,
  195. "au1000_gpio",
  196. &au1000gpio_fops
  197. };
  198. int __init au1000gpio_init(void)
  199. {
  200. misc_register(&au1000gpio_miscdev);
  201. printk("Au1000 gpio driver, version %sn", VERSION);
  202. return 0;
  203. }
  204. void __exit au1000gpio_exit(void)
  205. {
  206. misc_deregister(&au1000gpio_miscdev);
  207. }
  208. module_init(au1000gpio_init);
  209. module_exit(au1000gpio_exit);