pc8736x_gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* linux/drivers/char/pc8736x_gpio.c
  2. National Semiconductor PC8736x GPIO driver. Allows a user space
  3. process to play with the GPIO pins.
  4. Copyright (c) 2005,2006 Jim Cromie <jim.cromie@gmail.com>
  5. adapted from linux/drivers/char/scx200_gpio.c
  6. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>,
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/cdev.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/mutex.h>
  17. #include <linux/nsc_gpio.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/smp_lock.h>
  20. #include <asm/uaccess.h>
  21. #define DEVNAME "pc8736x_gpio"
  22. MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
  23. MODULE_DESCRIPTION("NatSemi/Winbond PC-8736x GPIO Pin Driver");
  24. MODULE_LICENSE("GPL");
  25. static int major; /* default to dynamic major */
  26. module_param(major, int, 0);
  27. MODULE_PARM_DESC(major, "Major device number");
  28. static DEFINE_MUTEX(pc8736x_gpio_config_lock);
  29. static unsigned pc8736x_gpio_base;
  30. static u8 pc8736x_gpio_shadow[4];
  31. #define SIO_BASE1 0x2E /* 1st command-reg to check */
  32. #define SIO_BASE2 0x4E /* alt command-reg to check */
  33. #define SIO_SID 0x20 /* SuperI/O ID Register */
  34. #define SIO_SID_VALUE 0xe9 /* Expected value in SuperI/O ID Register */
  35. #define SIO_CF1 0x21 /* chip config, bit0 is chip enable */
  36. #define PC8736X_GPIO_RANGE 16 /* ioaddr range */
  37. #define PC8736X_GPIO_CT 32 /* minors matching 4 8 bit ports */
  38. #define SIO_UNIT_SEL 0x7 /* unit select reg */
  39. #define SIO_UNIT_ACT 0x30 /* unit enable */
  40. #define SIO_GPIO_UNIT 0x7 /* unit number of GPIO */
  41. #define SIO_VLM_UNIT 0x0D
  42. #define SIO_TMS_UNIT 0x0E
  43. /* config-space addrs to read/write each unit's runtime addr */
  44. #define SIO_BASE_HADDR 0x60
  45. #define SIO_BASE_LADDR 0x61
  46. /* GPIO config-space pin-control addresses */
  47. #define SIO_GPIO_PIN_SELECT 0xF0
  48. #define SIO_GPIO_PIN_CONFIG 0xF1
  49. #define SIO_GPIO_PIN_EVENT 0xF2
  50. static unsigned char superio_cmd = 0;
  51. static unsigned char selected_device = 0xFF; /* bogus start val */
  52. /* GPIO port runtime access, functionality */
  53. static int port_offset[] = { 0, 4, 8, 10 }; /* non-uniform offsets ! */
  54. /* static int event_capable[] = { 1, 1, 0, 0 }; ports 2,3 are hobbled */
  55. #define PORT_OUT 0
  56. #define PORT_IN 1
  57. #define PORT_EVT_EN 2
  58. #define PORT_EVT_STST 3
  59. static struct platform_device *pdev; /* use in dev_*() */
  60. static inline void superio_outb(int addr, int val)
  61. {
  62. outb_p(addr, superio_cmd);
  63. outb_p(val, superio_cmd + 1);
  64. }
  65. static inline int superio_inb(int addr)
  66. {
  67. outb_p(addr, superio_cmd);
  68. return inb_p(superio_cmd + 1);
  69. }
  70. static int pc8736x_superio_present(void)
  71. {
  72. /* try the 2 possible values, read a hardware reg to verify */
  73. superio_cmd = SIO_BASE1;
  74. if (superio_inb(SIO_SID) == SIO_SID_VALUE)
  75. return superio_cmd;
  76. superio_cmd = SIO_BASE2;
  77. if (superio_inb(SIO_SID) == SIO_SID_VALUE)
  78. return superio_cmd;
  79. return 0;
  80. }
  81. static void device_select(unsigned devldn)
  82. {
  83. superio_outb(SIO_UNIT_SEL, devldn);
  84. selected_device = devldn;
  85. }
  86. static void select_pin(unsigned iminor)
  87. {
  88. /* select GPIO port/pin from device minor number */
  89. device_select(SIO_GPIO_UNIT);
  90. superio_outb(SIO_GPIO_PIN_SELECT,
  91. ((iminor << 1) & 0xF0) | (iminor & 0x7));
  92. }
  93. static inline u32 pc8736x_gpio_configure_fn(unsigned index, u32 mask, u32 bits,
  94. u32 func_slct)
  95. {
  96. u32 config, new_config;
  97. mutex_lock(&pc8736x_gpio_config_lock);
  98. device_select(SIO_GPIO_UNIT);
  99. select_pin(index);
  100. /* read current config value */
  101. config = superio_inb(func_slct);
  102. /* set new config */
  103. new_config = (config & mask) | bits;
  104. superio_outb(func_slct, new_config);
  105. mutex_unlock(&pc8736x_gpio_config_lock);
  106. return config;
  107. }
  108. static u32 pc8736x_gpio_configure(unsigned index, u32 mask, u32 bits)
  109. {
  110. return pc8736x_gpio_configure_fn(index, mask, bits,
  111. SIO_GPIO_PIN_CONFIG);
  112. }
  113. static int pc8736x_gpio_get(unsigned minor)
  114. {
  115. int port, bit, val;
  116. port = minor >> 3;
  117. bit = minor & 7;
  118. val = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_IN);
  119. val >>= bit;
  120. val &= 1;
  121. dev_dbg(&pdev->dev, "_gpio_get(%d from %x bit %d) == val %d\n",
  122. minor, pc8736x_gpio_base + port_offset[port] + PORT_IN, bit,
  123. val);
  124. return val;
  125. }
  126. static void pc8736x_gpio_set(unsigned minor, int val)
  127. {
  128. int port, bit, curval;
  129. minor &= 0x1f;
  130. port = minor >> 3;
  131. bit = minor & 7;
  132. curval = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  133. dev_dbg(&pdev->dev, "addr:%x cur:%x bit-pos:%d cur-bit:%x + new:%d -> bit-new:%d\n",
  134. pc8736x_gpio_base + port_offset[port] + PORT_OUT,
  135. curval, bit, (curval & ~(1 << bit)), val, (val << bit));
  136. val = (curval & ~(1 << bit)) | (val << bit);
  137. dev_dbg(&pdev->dev, "gpio_set(minor:%d port:%d bit:%d)"
  138. " %2x -> %2x\n", minor, port, bit, curval, val);
  139. outb_p(val, pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  140. curval = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  141. val = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_IN);
  142. dev_dbg(&pdev->dev, "wrote %x, read: %x\n", curval, val);
  143. pc8736x_gpio_shadow[port] = val;
  144. }
  145. static int pc8736x_gpio_current(unsigned minor)
  146. {
  147. int port, bit;
  148. minor &= 0x1f;
  149. port = minor >> 3;
  150. bit = minor & 7;
  151. return ((pc8736x_gpio_shadow[port] >> bit) & 0x01);
  152. }
  153. static void pc8736x_gpio_change(unsigned index)
  154. {
  155. pc8736x_gpio_set(index, !pc8736x_gpio_current(index));
  156. }
  157. static struct nsc_gpio_ops pc8736x_gpio_ops = {
  158. .owner = THIS_MODULE,
  159. .gpio_config = pc8736x_gpio_configure,
  160. .gpio_dump = nsc_gpio_dump,
  161. .gpio_get = pc8736x_gpio_get,
  162. .gpio_set = pc8736x_gpio_set,
  163. .gpio_change = pc8736x_gpio_change,
  164. .gpio_current = pc8736x_gpio_current
  165. };
  166. static int pc8736x_gpio_open(struct inode *inode, struct file *file)
  167. {
  168. unsigned m = iminor(inode);
  169. file->private_data = &pc8736x_gpio_ops;
  170. cycle_kernel_lock();
  171. dev_dbg(&pdev->dev, "open %d\n", m);
  172. if (m >= PC8736X_GPIO_CT)
  173. return -EINVAL;
  174. return nonseekable_open(inode, file);
  175. }
  176. static const struct file_operations pc8736x_gpio_fileops = {
  177. .owner = THIS_MODULE,
  178. .open = pc8736x_gpio_open,
  179. .write = nsc_gpio_write,
  180. .read = nsc_gpio_read,
  181. };
  182. static void __init pc8736x_init_shadow(void)
  183. {
  184. int port;
  185. /* read the current values driven on the GPIO signals */
  186. for (port = 0; port < 4; ++port)
  187. pc8736x_gpio_shadow[port]
  188. = inb_p(pc8736x_gpio_base + port_offset[port]
  189. + PORT_OUT);
  190. }
  191. static struct cdev pc8736x_gpio_cdev;
  192. static int __init pc8736x_gpio_init(void)
  193. {
  194. int rc;
  195. dev_t devid;
  196. pdev = platform_device_alloc(DEVNAME, 0);
  197. if (!pdev)
  198. return -ENOMEM;
  199. rc = platform_device_add(pdev);
  200. if (rc) {
  201. rc = -ENODEV;
  202. goto undo_platform_dev_alloc;
  203. }
  204. dev_info(&pdev->dev, "NatSemi pc8736x GPIO Driver Initializing\n");
  205. if (!pc8736x_superio_present()) {
  206. rc = -ENODEV;
  207. dev_err(&pdev->dev, "no device found\n");
  208. goto undo_platform_dev_add;
  209. }
  210. pc8736x_gpio_ops.dev = &pdev->dev;
  211. /* Verify that chip and it's GPIO unit are both enabled.
  212. My BIOS does this, so I take minimum action here
  213. */
  214. rc = superio_inb(SIO_CF1);
  215. if (!(rc & 0x01)) {
  216. rc = -ENODEV;
  217. dev_err(&pdev->dev, "device not enabled\n");
  218. goto undo_platform_dev_add;
  219. }
  220. device_select(SIO_GPIO_UNIT);
  221. if (!superio_inb(SIO_UNIT_ACT)) {
  222. rc = -ENODEV;
  223. dev_err(&pdev->dev, "GPIO unit not enabled\n");
  224. goto undo_platform_dev_add;
  225. }
  226. /* read the GPIO unit base addr that chip responds to */
  227. pc8736x_gpio_base = (superio_inb(SIO_BASE_HADDR) << 8
  228. | superio_inb(SIO_BASE_LADDR));
  229. if (!request_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE, DEVNAME)) {
  230. rc = -ENODEV;
  231. dev_err(&pdev->dev, "GPIO ioport %x busy\n",
  232. pc8736x_gpio_base);
  233. goto undo_platform_dev_add;
  234. }
  235. dev_info(&pdev->dev, "GPIO ioport %x reserved\n", pc8736x_gpio_base);
  236. if (major) {
  237. devid = MKDEV(major, 0);
  238. rc = register_chrdev_region(devid, PC8736X_GPIO_CT, DEVNAME);
  239. } else {
  240. rc = alloc_chrdev_region(&devid, 0, PC8736X_GPIO_CT, DEVNAME);
  241. major = MAJOR(devid);
  242. }
  243. if (rc < 0) {
  244. dev_err(&pdev->dev, "register-chrdev failed: %d\n", rc);
  245. goto undo_request_region;
  246. }
  247. if (!major) {
  248. major = rc;
  249. dev_dbg(&pdev->dev, "got dynamic major %d\n", major);
  250. }
  251. pc8736x_init_shadow();
  252. /* ignore minor errs, and succeed */
  253. cdev_init(&pc8736x_gpio_cdev, &pc8736x_gpio_fileops);
  254. cdev_add(&pc8736x_gpio_cdev, devid, PC8736X_GPIO_CT);
  255. return 0;
  256. undo_request_region:
  257. release_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE);
  258. undo_platform_dev_add:
  259. platform_device_del(pdev);
  260. undo_platform_dev_alloc:
  261. platform_device_put(pdev);
  262. return rc;
  263. }
  264. static void __exit pc8736x_gpio_cleanup(void)
  265. {
  266. dev_dbg(&pdev->dev, "cleanup\n");
  267. cdev_del(&pc8736x_gpio_cdev);
  268. unregister_chrdev_region(MKDEV(major,0), PC8736X_GPIO_CT);
  269. release_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE);
  270. platform_device_del(pdev);
  271. platform_device_put(pdev);
  272. }
  273. module_init(pc8736x_gpio_init);
  274. module_exit(pc8736x_gpio_cleanup);