pc8736x_gpio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <asm/uaccess.h>
  20. #define DEVNAME "pc8736x_gpio"
  21. MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
  22. MODULE_DESCRIPTION("NatSemi/Winbond PC-8736x GPIO Pin Driver");
  23. MODULE_LICENSE("GPL");
  24. static int major; /* default to dynamic major */
  25. module_param(major, int, 0);
  26. MODULE_PARM_DESC(major, "Major device number");
  27. static DEFINE_MUTEX(pc8736x_gpio_config_lock);
  28. static unsigned pc8736x_gpio_base;
  29. static u8 pc8736x_gpio_shadow[4];
  30. #define SIO_BASE1 0x2E /* 1st command-reg to check */
  31. #define SIO_BASE2 0x4E /* alt command-reg to check */
  32. #define SIO_SID 0x20 /* SuperI/O ID Register */
  33. #define SIO_SID_PC87365 0xe5 /* Expected value in ID Register for PC87365 */
  34. #define SIO_SID_PC87366 0xe9 /* Expected value in ID Register for PC87366 */
  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. int id;
  73. /* try the 2 possible values, read a hardware reg to verify */
  74. superio_cmd = SIO_BASE1;
  75. id = superio_inb(SIO_SID);
  76. if (id == SIO_SID_PC87365 || id == SIO_SID_PC87366)
  77. return superio_cmd;
  78. superio_cmd = SIO_BASE2;
  79. id = superio_inb(SIO_SID);
  80. if (id == SIO_SID_PC87365 || id == SIO_SID_PC87366)
  81. return superio_cmd;
  82. return 0;
  83. }
  84. static void device_select(unsigned devldn)
  85. {
  86. superio_outb(SIO_UNIT_SEL, devldn);
  87. selected_device = devldn;
  88. }
  89. static void select_pin(unsigned iminor)
  90. {
  91. /* select GPIO port/pin from device minor number */
  92. device_select(SIO_GPIO_UNIT);
  93. superio_outb(SIO_GPIO_PIN_SELECT,
  94. ((iminor << 1) & 0xF0) | (iminor & 0x7));
  95. }
  96. static inline u32 pc8736x_gpio_configure_fn(unsigned index, u32 mask, u32 bits,
  97. u32 func_slct)
  98. {
  99. u32 config, new_config;
  100. mutex_lock(&pc8736x_gpio_config_lock);
  101. device_select(SIO_GPIO_UNIT);
  102. select_pin(index);
  103. /* read current config value */
  104. config = superio_inb(func_slct);
  105. /* set new config */
  106. new_config = (config & mask) | bits;
  107. superio_outb(func_slct, new_config);
  108. mutex_unlock(&pc8736x_gpio_config_lock);
  109. return config;
  110. }
  111. static u32 pc8736x_gpio_configure(unsigned index, u32 mask, u32 bits)
  112. {
  113. return pc8736x_gpio_configure_fn(index, mask, bits,
  114. SIO_GPIO_PIN_CONFIG);
  115. }
  116. static int pc8736x_gpio_get(unsigned minor)
  117. {
  118. int port, bit, val;
  119. port = minor >> 3;
  120. bit = minor & 7;
  121. val = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_IN);
  122. val >>= bit;
  123. val &= 1;
  124. dev_dbg(&pdev->dev, "_gpio_get(%d from %x bit %d) == val %d\n",
  125. minor, pc8736x_gpio_base + port_offset[port] + PORT_IN, bit,
  126. val);
  127. return val;
  128. }
  129. static void pc8736x_gpio_set(unsigned minor, int val)
  130. {
  131. int port, bit, curval;
  132. minor &= 0x1f;
  133. port = minor >> 3;
  134. bit = minor & 7;
  135. curval = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  136. dev_dbg(&pdev->dev, "addr:%x cur:%x bit-pos:%d cur-bit:%x + new:%d -> bit-new:%d\n",
  137. pc8736x_gpio_base + port_offset[port] + PORT_OUT,
  138. curval, bit, (curval & ~(1 << bit)), val, (val << bit));
  139. val = (curval & ~(1 << bit)) | (val << bit);
  140. dev_dbg(&pdev->dev, "gpio_set(minor:%d port:%d bit:%d)"
  141. " %2x -> %2x\n", minor, port, bit, curval, val);
  142. outb_p(val, pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  143. curval = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_OUT);
  144. val = inb_p(pc8736x_gpio_base + port_offset[port] + PORT_IN);
  145. dev_dbg(&pdev->dev, "wrote %x, read: %x\n", curval, val);
  146. pc8736x_gpio_shadow[port] = val;
  147. }
  148. static int pc8736x_gpio_current(unsigned minor)
  149. {
  150. int port, bit;
  151. minor &= 0x1f;
  152. port = minor >> 3;
  153. bit = minor & 7;
  154. return ((pc8736x_gpio_shadow[port] >> bit) & 0x01);
  155. }
  156. static void pc8736x_gpio_change(unsigned index)
  157. {
  158. pc8736x_gpio_set(index, !pc8736x_gpio_current(index));
  159. }
  160. static struct nsc_gpio_ops pc8736x_gpio_ops = {
  161. .owner = THIS_MODULE,
  162. .gpio_config = pc8736x_gpio_configure,
  163. .gpio_dump = nsc_gpio_dump,
  164. .gpio_get = pc8736x_gpio_get,
  165. .gpio_set = pc8736x_gpio_set,
  166. .gpio_change = pc8736x_gpio_change,
  167. .gpio_current = pc8736x_gpio_current
  168. };
  169. static int pc8736x_gpio_open(struct inode *inode, struct file *file)
  170. {
  171. unsigned m = iminor(inode);
  172. file->private_data = &pc8736x_gpio_ops;
  173. dev_dbg(&pdev->dev, "open %d\n", m);
  174. if (m >= PC8736X_GPIO_CT)
  175. return -EINVAL;
  176. return nonseekable_open(inode, file);
  177. }
  178. static const struct file_operations pc8736x_gpio_fileops = {
  179. .owner = THIS_MODULE,
  180. .open = pc8736x_gpio_open,
  181. .write = nsc_gpio_write,
  182. .read = nsc_gpio_read,
  183. .llseek = no_llseek,
  184. };
  185. static void __init pc8736x_init_shadow(void)
  186. {
  187. int port;
  188. /* read the current values driven on the GPIO signals */
  189. for (port = 0; port < 4; ++port)
  190. pc8736x_gpio_shadow[port]
  191. = inb_p(pc8736x_gpio_base + port_offset[port]
  192. + PORT_OUT);
  193. }
  194. static struct cdev pc8736x_gpio_cdev;
  195. static int __init pc8736x_gpio_init(void)
  196. {
  197. int rc;
  198. dev_t devid;
  199. pdev = platform_device_alloc(DEVNAME, 0);
  200. if (!pdev)
  201. return -ENOMEM;
  202. rc = platform_device_add(pdev);
  203. if (rc) {
  204. rc = -ENODEV;
  205. goto undo_platform_dev_alloc;
  206. }
  207. dev_info(&pdev->dev, "NatSemi pc8736x GPIO Driver Initializing\n");
  208. if (!pc8736x_superio_present()) {
  209. rc = -ENODEV;
  210. dev_err(&pdev->dev, "no device found\n");
  211. goto undo_platform_dev_add;
  212. }
  213. pc8736x_gpio_ops.dev = &pdev->dev;
  214. /* Verify that chip and it's GPIO unit are both enabled.
  215. My BIOS does this, so I take minimum action here
  216. */
  217. rc = superio_inb(SIO_CF1);
  218. if (!(rc & 0x01)) {
  219. rc = -ENODEV;
  220. dev_err(&pdev->dev, "device not enabled\n");
  221. goto undo_platform_dev_add;
  222. }
  223. device_select(SIO_GPIO_UNIT);
  224. if (!superio_inb(SIO_UNIT_ACT)) {
  225. rc = -ENODEV;
  226. dev_err(&pdev->dev, "GPIO unit not enabled\n");
  227. goto undo_platform_dev_add;
  228. }
  229. /* read the GPIO unit base addr that chip responds to */
  230. pc8736x_gpio_base = (superio_inb(SIO_BASE_HADDR) << 8
  231. | superio_inb(SIO_BASE_LADDR));
  232. if (!request_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE, DEVNAME)) {
  233. rc = -ENODEV;
  234. dev_err(&pdev->dev, "GPIO ioport %x busy\n",
  235. pc8736x_gpio_base);
  236. goto undo_platform_dev_add;
  237. }
  238. dev_info(&pdev->dev, "GPIO ioport %x reserved\n", pc8736x_gpio_base);
  239. if (major) {
  240. devid = MKDEV(major, 0);
  241. rc = register_chrdev_region(devid, PC8736X_GPIO_CT, DEVNAME);
  242. } else {
  243. rc = alloc_chrdev_region(&devid, 0, PC8736X_GPIO_CT, DEVNAME);
  244. major = MAJOR(devid);
  245. }
  246. if (rc < 0) {
  247. dev_err(&pdev->dev, "register-chrdev failed: %d\n", rc);
  248. goto undo_request_region;
  249. }
  250. if (!major) {
  251. major = rc;
  252. dev_dbg(&pdev->dev, "got dynamic major %d\n", major);
  253. }
  254. pc8736x_init_shadow();
  255. /* ignore minor errs, and succeed */
  256. cdev_init(&pc8736x_gpio_cdev, &pc8736x_gpio_fileops);
  257. cdev_add(&pc8736x_gpio_cdev, devid, PC8736X_GPIO_CT);
  258. return 0;
  259. undo_request_region:
  260. release_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE);
  261. undo_platform_dev_add:
  262. platform_device_del(pdev);
  263. undo_platform_dev_alloc:
  264. platform_device_put(pdev);
  265. return rc;
  266. }
  267. static void __exit pc8736x_gpio_cleanup(void)
  268. {
  269. dev_dbg(&pdev->dev, "cleanup\n");
  270. cdev_del(&pc8736x_gpio_cdev);
  271. unregister_chrdev_region(MKDEV(major,0), PC8736X_GPIO_CT);
  272. release_region(pc8736x_gpio_base, PC8736X_GPIO_RANGE);
  273. platform_device_unregister(pdev);
  274. }
  275. module_init(pc8736x_gpio_init);
  276. module_exit(pc8736x_gpio_cleanup);