cs5535_gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * AMD CS5535/CS5536 GPIO driver.
  3. * Allows a user space process to play with the GPIO pins.
  4. *
  5. * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the smems of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/cdev.h>
  17. #include <linux/ioport.h>
  18. #include <linux/pci.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/io.h>
  21. #define NAME "cs5535_gpio"
  22. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  23. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO Pin Driver");
  24. MODULE_LICENSE("GPL");
  25. static int major;
  26. module_param(major, int, 0);
  27. MODULE_PARM_DESC(major, "Major device number");
  28. static ulong mask;
  29. module_param(mask, ulong, 0);
  30. MODULE_PARM_DESC(mask, "GPIO channel mask");
  31. #define MSR_LBAR_GPIO 0x5140000C
  32. static u32 gpio_base;
  33. static struct pci_device_id divil_pci[] = {
  34. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  35. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  36. { } /* NULL entry */
  37. };
  38. MODULE_DEVICE_TABLE(pci, divil_pci);
  39. static struct cdev cs5535_gpio_cdev;
  40. /* reserve 32 entries even though some aren't usable */
  41. #define CS5535_GPIO_COUNT 32
  42. /* IO block size */
  43. #define CS5535_GPIO_SIZE 256
  44. struct gpio_regmap {
  45. u32 rd_offset;
  46. u32 wr_offset;
  47. char on;
  48. char off;
  49. };
  50. static struct gpio_regmap rm[] =
  51. {
  52. { 0x30, 0x00, '1', '0' }, /* GPIOx_READ_BACK / GPIOx_OUT_VAL */
  53. { 0x20, 0x20, 'I', 'i' }, /* GPIOx_IN_EN */
  54. { 0x04, 0x04, 'O', 'o' }, /* GPIOx_OUT_EN */
  55. { 0x08, 0x08, 't', 'T' }, /* GPIOx_OUT_OD_EN */
  56. { 0x18, 0x18, 'P', 'p' }, /* GPIOx_OUT_PU_EN */
  57. { 0x1c, 0x1c, 'D', 'd' }, /* GPIOx_OUT_PD_EN */
  58. };
  59. /**
  60. * Gets the register offset for the GPIO bank.
  61. * Low (0-15) starts at 0x00, high (16-31) starts at 0x80
  62. */
  63. static inline u32 cs5535_lowhigh_base(int reg)
  64. {
  65. return (reg & 0x10) << 3;
  66. }
  67. static ssize_t cs5535_gpio_write(struct file *file, const char __user *data,
  68. size_t len, loff_t *ppos)
  69. {
  70. u32 m = iminor(file->f_path.dentry->d_inode);
  71. int i, j;
  72. u32 base = gpio_base + cs5535_lowhigh_base(m);
  73. u32 m0, m1;
  74. char c;
  75. /**
  76. * Creates the mask for atomic bit programming.
  77. * The high 16 bits and the low 16 bits are used to set the mask.
  78. * For example, GPIO 15 maps to 31,15: 0,1 => On; 1,0=> Off
  79. */
  80. m1 = 1 << (m & 0x0F);
  81. m0 = m1 << 16;
  82. for (i = 0; i < len; ++i) {
  83. if (get_user(c, data+i))
  84. return -EFAULT;
  85. for (j = 0; j < ARRAY_SIZE(rm); j++) {
  86. if (c == rm[j].on) {
  87. outl(m1, base + rm[j].wr_offset);
  88. break;
  89. } else if (c == rm[j].off) {
  90. outl(m0, base + rm[j].wr_offset);
  91. break;
  92. }
  93. }
  94. }
  95. *ppos = 0;
  96. return len;
  97. }
  98. static ssize_t cs5535_gpio_read(struct file *file, char __user *buf,
  99. size_t len, loff_t *ppos)
  100. {
  101. u32 m = iminor(file->f_path.dentry->d_inode);
  102. u32 base = gpio_base + cs5535_lowhigh_base(m);
  103. int rd_bit = 1 << (m & 0x0f);
  104. int i;
  105. char ch;
  106. ssize_t count = 0;
  107. if (*ppos >= ARRAY_SIZE(rm))
  108. return 0;
  109. for (i = *ppos; (i < (*ppos + len)) && (i < ARRAY_SIZE(rm)); i++) {
  110. ch = (inl(base + rm[i].rd_offset) & rd_bit) ?
  111. rm[i].on : rm[i].off;
  112. if (put_user(ch, buf+count))
  113. return -EFAULT;
  114. count++;
  115. }
  116. /* add a line-feed if there is room */
  117. if ((i == ARRAY_SIZE(rm)) && (count < len)) {
  118. put_user('\n', buf + count);
  119. count++;
  120. }
  121. *ppos += count;
  122. return count;
  123. }
  124. static int cs5535_gpio_open(struct inode *inode, struct file *file)
  125. {
  126. u32 m = iminor(inode);
  127. /* the mask says which pins are usable by this driver */
  128. if ((mask & (1 << m)) == 0)
  129. return -EINVAL;
  130. return nonseekable_open(inode, file);
  131. }
  132. static const struct file_operations cs5535_gpio_fops = {
  133. .owner = THIS_MODULE,
  134. .write = cs5535_gpio_write,
  135. .read = cs5535_gpio_read,
  136. .open = cs5535_gpio_open
  137. };
  138. static int __init cs5535_gpio_init(void)
  139. {
  140. dev_t dev_id;
  141. u32 low, hi;
  142. int retval;
  143. if (pci_dev_present(divil_pci) == 0) {
  144. printk(KERN_WARNING NAME ": DIVIL not found\n");
  145. return -ENODEV;
  146. }
  147. /* Grab the GPIO I/O range */
  148. rdmsr(MSR_LBAR_GPIO, low, hi);
  149. /* Check the mask and whether GPIO is enabled (sanity check) */
  150. if (hi != 0x0000f001) {
  151. printk(KERN_WARNING NAME ": GPIO not enabled\n");
  152. return -ENODEV;
  153. }
  154. /* Mask off the IO base address */
  155. gpio_base = low & 0x0000ff00;
  156. /**
  157. * Some GPIO pins
  158. * 31-29,23 : reserved (always mask out)
  159. * 28 : Power Button
  160. * 26 : PME#
  161. * 22-16 : LPC
  162. * 14,15 : SMBus
  163. * 9,8 : UART1
  164. * 7 : PCI INTB
  165. * 3,4 : UART2/DDC
  166. * 2 : IDE_IRQ0
  167. * 0 : PCI INTA
  168. *
  169. * If a mask was not specified, be conservative and only allow:
  170. * 1,2,5,6,10-13,24,25,27
  171. */
  172. if (mask != 0)
  173. mask &= 0x1f7fffff;
  174. else
  175. mask = 0x0b003c66;
  176. if (request_region(gpio_base, CS5535_GPIO_SIZE, NAME) == 0) {
  177. printk(KERN_ERR NAME ": can't allocate I/O for GPIO\n");
  178. return -ENODEV;
  179. }
  180. if (major) {
  181. dev_id = MKDEV(major, 0);
  182. retval = register_chrdev_region(dev_id, CS5535_GPIO_COUNT,
  183. NAME);
  184. } else {
  185. retval = alloc_chrdev_region(&dev_id, 0, CS5535_GPIO_COUNT,
  186. NAME);
  187. major = MAJOR(dev_id);
  188. }
  189. if (retval) {
  190. release_region(gpio_base, CS5535_GPIO_SIZE);
  191. return -1;
  192. }
  193. printk(KERN_DEBUG NAME ": base=%#x mask=%#lx major=%d\n",
  194. gpio_base, mask, major);
  195. cdev_init(&cs5535_gpio_cdev, &cs5535_gpio_fops);
  196. cdev_add(&cs5535_gpio_cdev, dev_id, CS5535_GPIO_COUNT);
  197. return 0;
  198. }
  199. static void __exit cs5535_gpio_cleanup(void)
  200. {
  201. dev_t dev_id = MKDEV(major, 0);
  202. cdev_del(&cs5535_gpio_cdev);
  203. unregister_chrdev_region(dev_id, CS5535_GPIO_COUNT);
  204. release_region(gpio_base, CS5535_GPIO_SIZE);
  205. }
  206. module_init(cs5535_gpio_init);
  207. module_exit(cs5535_gpio_cleanup);