tb0219.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Driver for TANBAC TB0219 base board.
  3. *
  4. * Copyright (C) 2005 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <asm/io.h>
  25. #include <asm/reboot.h>
  26. MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>");
  27. MODULE_DESCRIPTION("TANBAC TB0219 base board driver");
  28. MODULE_LICENSE("GPL");
  29. static int major; /* default is dynamic major device number */
  30. module_param(major, int, 0);
  31. MODULE_PARM_DESC(major, "Major device number");
  32. static void (*old_machine_restart)(char *command);
  33. static void __iomem *tb0219_base;
  34. static spinlock_t tb0219_lock;
  35. #define tb0219_read(offset) readw(tb0219_base + (offset))
  36. #define tb0219_write(offset, value) writew((value), tb0219_base + (offset))
  37. #define TB0219_START 0x0a000000UL
  38. #define TB0219_SIZE 0x20UL
  39. #define TB0219_LED 0x00
  40. #define TB0219_GPIO_INPUT 0x02
  41. #define TB0219_GPIO_OUTPUT 0x04
  42. #define TB0219_DIP_SWITCH 0x06
  43. #define TB0219_MISC 0x08
  44. #define TB0219_RESET 0x0e
  45. #define TB0219_PCI_SLOT1_IRQ_STATUS 0x10
  46. #define TB0219_PCI_SLOT2_IRQ_STATUS 0x12
  47. #define TB0219_PCI_SLOT3_IRQ_STATUS 0x14
  48. typedef enum {
  49. TYPE_LED,
  50. TYPE_GPIO_OUTPUT,
  51. } tb0219_type_t;
  52. /*
  53. * Minor device number
  54. * 0 = 7 segment LED
  55. *
  56. * 16 = GPIO IN 0
  57. * 17 = GPIO IN 1
  58. * 18 = GPIO IN 2
  59. * 19 = GPIO IN 3
  60. * 20 = GPIO IN 4
  61. * 21 = GPIO IN 5
  62. * 22 = GPIO IN 6
  63. * 23 = GPIO IN 7
  64. *
  65. * 32 = GPIO OUT 0
  66. * 33 = GPIO OUT 1
  67. * 34 = GPIO OUT 2
  68. * 35 = GPIO OUT 3
  69. * 36 = GPIO OUT 4
  70. * 37 = GPIO OUT 5
  71. * 38 = GPIO OUT 6
  72. * 39 = GPIO OUT 7
  73. *
  74. * 48 = DIP switch 1
  75. * 49 = DIP switch 2
  76. * 50 = DIP switch 3
  77. * 51 = DIP switch 4
  78. * 52 = DIP switch 5
  79. * 53 = DIP switch 6
  80. * 54 = DIP switch 7
  81. * 55 = DIP switch 8
  82. */
  83. static inline char get_led(void)
  84. {
  85. return (char)tb0219_read(TB0219_LED);
  86. }
  87. static inline char get_gpio_input_pin(unsigned int pin)
  88. {
  89. uint16_t values;
  90. values = tb0219_read(TB0219_GPIO_INPUT);
  91. if (values & (1 << pin))
  92. return '1';
  93. return '0';
  94. }
  95. static inline char get_gpio_output_pin(unsigned int pin)
  96. {
  97. uint16_t values;
  98. values = tb0219_read(TB0219_GPIO_OUTPUT);
  99. if (values & (1 << pin))
  100. return '1';
  101. return '0';
  102. }
  103. static inline char get_dip_switch(unsigned int pin)
  104. {
  105. uint16_t values;
  106. values = tb0219_read(TB0219_DIP_SWITCH);
  107. if (values & (1 << pin))
  108. return '1';
  109. return '0';
  110. }
  111. static inline int set_led(char command)
  112. {
  113. tb0219_write(TB0219_LED, command);
  114. return 0;
  115. }
  116. static inline int set_gpio_output_pin(unsigned int pin, char command)
  117. {
  118. unsigned long flags;
  119. uint16_t value;
  120. if (command != '0' && command != '1')
  121. return -EINVAL;
  122. spin_lock_irqsave(&tb0219_lock, flags);
  123. value = tb0219_read(TB0219_GPIO_OUTPUT);
  124. if (command == '0')
  125. value &= ~(1 << pin);
  126. else
  127. value |= 1 << pin;
  128. tb0219_write(TB0219_GPIO_OUTPUT, value);
  129. spin_unlock_irqrestore(&tb0219_lock, flags);
  130. return 0;
  131. }
  132. static ssize_t tanbac_tb0219_read(struct file *file, char __user *buf, size_t len,
  133. loff_t *ppos)
  134. {
  135. unsigned int minor;
  136. char value;
  137. minor = iminor(file->f_dentry->d_inode);
  138. switch (minor) {
  139. case 0:
  140. value = get_led();
  141. break;
  142. case 16 ... 23:
  143. value = get_gpio_input_pin(minor - 16);
  144. break;
  145. case 32 ... 39:
  146. value = get_gpio_output_pin(minor - 32);
  147. break;
  148. case 48 ... 55:
  149. value = get_dip_switch(minor - 48);
  150. break;
  151. default:
  152. return -EBADF;
  153. }
  154. if (len <= 0)
  155. return -EFAULT;
  156. if (put_user(value, buf))
  157. return -EFAULT;
  158. return 1;
  159. }
  160. static ssize_t tanbac_tb0219_write(struct file *file, const char __user *data,
  161. size_t len, loff_t *ppos)
  162. {
  163. unsigned int minor;
  164. tb0219_type_t type;
  165. size_t i;
  166. int retval = 0;
  167. char c;
  168. minor = iminor(file->f_dentry->d_inode);
  169. switch (minor) {
  170. case 0:
  171. type = TYPE_LED;
  172. break;
  173. case 32 ... 39:
  174. type = TYPE_GPIO_OUTPUT;
  175. break;
  176. default:
  177. return -EBADF;
  178. }
  179. for (i = 0; i < len; i++) {
  180. if (get_user(c, data + i))
  181. return -EFAULT;
  182. switch (type) {
  183. case TYPE_LED:
  184. retval = set_led(c);
  185. break;
  186. case TYPE_GPIO_OUTPUT:
  187. retval = set_gpio_output_pin(minor - 32, c);
  188. break;
  189. }
  190. if (retval < 0)
  191. break;
  192. }
  193. return i;
  194. }
  195. static int tanbac_tb0219_open(struct inode *inode, struct file *file)
  196. {
  197. unsigned int minor;
  198. minor = iminor(inode);
  199. switch (minor) {
  200. case 0:
  201. case 16 ... 23:
  202. case 32 ... 39:
  203. case 48 ... 55:
  204. return nonseekable_open(inode, file);
  205. default:
  206. break;
  207. }
  208. return -EBADF;
  209. }
  210. static int tanbac_tb0219_release(struct inode *inode, struct file *file)
  211. {
  212. return 0;
  213. }
  214. static struct file_operations tb0219_fops = {
  215. .owner = THIS_MODULE,
  216. .read = tanbac_tb0219_read,
  217. .write = tanbac_tb0219_write,
  218. .open = tanbac_tb0219_open,
  219. .release = tanbac_tb0219_release,
  220. };
  221. static void tb0219_restart(char *command)
  222. {
  223. tb0219_write(TB0219_RESET, 0);
  224. }
  225. static int tb0219_probe(struct device *dev)
  226. {
  227. int retval;
  228. if (request_mem_region(TB0219_START, TB0219_SIZE, "TB0219") == NULL)
  229. return -EBUSY;
  230. tb0219_base = ioremap(TB0219_START, TB0219_SIZE);
  231. if (tb0219_base == NULL) {
  232. release_mem_region(TB0219_START, TB0219_SIZE);
  233. return -ENOMEM;
  234. }
  235. retval = register_chrdev(major, "TB0219", &tb0219_fops);
  236. if (retval < 0) {
  237. iounmap(tb0219_base);
  238. tb0219_base = NULL;
  239. release_mem_region(TB0219_START, TB0219_SIZE);
  240. return retval;
  241. }
  242. spin_lock_init(&tb0219_lock);
  243. old_machine_restart = _machine_restart;
  244. _machine_restart = tb0219_restart;
  245. if (major == 0) {
  246. major = retval;
  247. printk(KERN_INFO "TB0219: major number %d\n", major);
  248. }
  249. return 0;
  250. }
  251. static int tb0219_remove(struct device *dev)
  252. {
  253. _machine_restart = old_machine_restart;
  254. iounmap(tb0219_base);
  255. tb0219_base = NULL;
  256. release_mem_region(TB0219_START, TB0219_SIZE);
  257. return 0;
  258. }
  259. static struct platform_device *tb0219_platform_device;
  260. static struct device_driver tb0219_device_driver = {
  261. .name = "TB0219",
  262. .bus = &platform_bus_type,
  263. .probe = tb0219_probe,
  264. .remove = tb0219_remove,
  265. };
  266. static int __devinit tanbac_tb0219_init(void)
  267. {
  268. int retval;
  269. tb0219_platform_device = platform_device_register_simple("TB0219", -1, NULL, 0);
  270. if (IS_ERR(tb0219_platform_device))
  271. return PTR_ERR(tb0219_platform_device);
  272. retval = driver_register(&tb0219_device_driver);
  273. if (retval < 0)
  274. platform_device_unregister(tb0219_platform_device);
  275. return retval;
  276. }
  277. static void __devexit tanbac_tb0219_exit(void)
  278. {
  279. driver_unregister(&tb0219_device_driver);
  280. platform_device_unregister(tb0219_platform_device);
  281. }
  282. module_init(tanbac_tb0219_init);
  283. module_exit(tanbac_tb0219_exit);