cs5535-gpio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * AMD CS5535/CS5536 GPIO driver
  3. * Copyright (C) 2006 Advanced Micro Devices, Inc.
  4. * Copyright (C) 2007-2009 Andres Salomon <dilinger@collabora.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/gpio.h>
  15. #include <linux/io.h>
  16. #include <linux/cs5535.h>
  17. #define DRV_NAME "cs5535-gpio"
  18. #define GPIO_BAR 1
  19. static struct cs5535_gpio_chip {
  20. struct gpio_chip chip;
  21. resource_size_t base;
  22. struct pci_dev *pdev;
  23. spinlock_t lock;
  24. } cs5535_gpio_chip;
  25. /*
  26. * The CS5535/CS5536 GPIOs support a number of extra features not defined
  27. * by the gpio_chip API, so these are exported. For a full list of the
  28. * registers, see include/linux/cs5535.h.
  29. */
  30. static void __cs5535_gpio_set(struct cs5535_gpio_chip *chip, unsigned offset,
  31. unsigned int reg)
  32. {
  33. if (offset < 16)
  34. /* low bank register */
  35. outl(1 << offset, chip->base + reg);
  36. else
  37. /* high bank register */
  38. outl(1 << (offset - 16), chip->base + 0x80 + reg);
  39. }
  40. void cs5535_gpio_set(unsigned offset, unsigned int reg)
  41. {
  42. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  43. unsigned long flags;
  44. spin_lock_irqsave(&chip->lock, flags);
  45. __cs5535_gpio_set(chip, offset, reg);
  46. spin_unlock_irqrestore(&chip->lock, flags);
  47. }
  48. EXPORT_SYMBOL_GPL(cs5535_gpio_set);
  49. static void __cs5535_gpio_clear(struct cs5535_gpio_chip *chip, unsigned offset,
  50. unsigned int reg)
  51. {
  52. if (offset < 16)
  53. /* low bank register */
  54. outl(1 << (offset + 16), chip->base + reg);
  55. else
  56. /* high bank register */
  57. outl(1 << offset, chip->base + 0x80 + reg);
  58. }
  59. void cs5535_gpio_clear(unsigned offset, unsigned int reg)
  60. {
  61. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  62. unsigned long flags;
  63. spin_lock_irqsave(&chip->lock, flags);
  64. __cs5535_gpio_clear(chip, offset, reg);
  65. spin_unlock_irqrestore(&chip->lock, flags);
  66. }
  67. EXPORT_SYMBOL_GPL(cs5535_gpio_clear);
  68. int cs5535_gpio_isset(unsigned offset, unsigned int reg)
  69. {
  70. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  71. unsigned long flags;
  72. long val;
  73. spin_lock_irqsave(&chip->lock, flags);
  74. if (offset < 16)
  75. /* low bank register */
  76. val = inl(chip->base + reg);
  77. else {
  78. /* high bank register */
  79. val = inl(chip->base + 0x80 + reg);
  80. offset -= 16;
  81. }
  82. spin_unlock_irqrestore(&chip->lock, flags);
  83. return (val & (1 << offset)) ? 1 : 0;
  84. }
  85. EXPORT_SYMBOL_GPL(cs5535_gpio_isset);
  86. /*
  87. * Generic gpio_chip API support.
  88. */
  89. static int chip_gpio_get(struct gpio_chip *chip, unsigned offset)
  90. {
  91. return cs5535_gpio_isset(offset, GPIO_OUTPUT_VAL);
  92. }
  93. static void chip_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  94. {
  95. if (val)
  96. cs5535_gpio_set(offset, GPIO_OUTPUT_VAL);
  97. else
  98. cs5535_gpio_clear(offset, GPIO_OUTPUT_VAL);
  99. }
  100. static int chip_direction_input(struct gpio_chip *c, unsigned offset)
  101. {
  102. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  103. unsigned long flags;
  104. spin_lock_irqsave(&chip->lock, flags);
  105. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  106. spin_unlock_irqrestore(&chip->lock, flags);
  107. return 0;
  108. }
  109. static int chip_direction_output(struct gpio_chip *c, unsigned offset, int val)
  110. {
  111. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  112. unsigned long flags;
  113. spin_lock_irqsave(&chip->lock, flags);
  114. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_ENABLE);
  115. if (val)
  116. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_VAL);
  117. else
  118. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_VAL);
  119. spin_unlock_irqrestore(&chip->lock, flags);
  120. return 0;
  121. }
  122. static struct cs5535_gpio_chip cs5535_gpio_chip = {
  123. .chip = {
  124. .owner = THIS_MODULE,
  125. .label = DRV_NAME,
  126. .base = 0,
  127. .ngpio = 28,
  128. .get = chip_gpio_get,
  129. .set = chip_gpio_set,
  130. .direction_input = chip_direction_input,
  131. .direction_output = chip_direction_output,
  132. },
  133. };
  134. static int __init cs5535_gpio_probe(struct pci_dev *pdev,
  135. const struct pci_device_id *pci_id)
  136. {
  137. int err;
  138. /* There are two ways to get the GPIO base address; one is by
  139. * fetching it from MSR_LBAR_GPIO, the other is by reading the
  140. * PCI BAR info. The latter method is easier (especially across
  141. * different architectures), so we'll stick with that for now. If
  142. * it turns out to be unreliable in the face of crappy BIOSes, we
  143. * can always go back to using MSRs.. */
  144. err = pci_enable_device_io(pdev);
  145. if (err) {
  146. dev_err(&pdev->dev, "can't enable device IO\n");
  147. goto done;
  148. }
  149. err = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
  150. if (err) {
  151. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
  152. goto done;
  153. }
  154. /* set up the driver-specific struct */
  155. cs5535_gpio_chip.base = pci_resource_start(pdev, GPIO_BAR);
  156. cs5535_gpio_chip.pdev = pdev;
  157. spin_lock_init(&cs5535_gpio_chip.lock);
  158. dev_info(&pdev->dev, "allocated PCI BAR #%d: base 0x%llx\n", GPIO_BAR,
  159. (unsigned long long) cs5535_gpio_chip.base);
  160. /* finally, register with the generic GPIO API */
  161. err = gpiochip_add(&cs5535_gpio_chip.chip);
  162. if (err) {
  163. dev_err(&pdev->dev, "failed to register gpio chip\n");
  164. goto release_region;
  165. }
  166. printk(KERN_INFO DRV_NAME ": GPIO support successfully loaded.\n");
  167. return 0;
  168. release_region:
  169. pci_release_region(pdev, GPIO_BAR);
  170. done:
  171. return err;
  172. }
  173. static void __exit cs5535_gpio_remove(struct pci_dev *pdev)
  174. {
  175. int err;
  176. err = gpiochip_remove(&cs5535_gpio_chip.chip);
  177. if (err) {
  178. /* uhh? */
  179. dev_err(&pdev->dev, "unable to remove gpio_chip?\n");
  180. }
  181. pci_release_region(pdev, GPIO_BAR);
  182. }
  183. static struct pci_device_id cs5535_gpio_pci_tbl[] = {
  184. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  185. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  186. { 0, },
  187. };
  188. MODULE_DEVICE_TABLE(pci, cs5535_gpio_pci_tbl);
  189. /*
  190. * We can't use the standard PCI driver registration stuff here, since
  191. * that allows only one driver to bind to each PCI device (and we want
  192. * multiple drivers to be able to bind to the device). Instead, manually
  193. * scan for the PCI device, request a single region, and keep track of the
  194. * devices that we're using.
  195. */
  196. static int __init cs5535_gpio_scan_pci(void)
  197. {
  198. struct pci_dev *pdev;
  199. int err = -ENODEV;
  200. int i;
  201. for (i = 0; i < ARRAY_SIZE(cs5535_gpio_pci_tbl); i++) {
  202. pdev = pci_get_device(cs5535_gpio_pci_tbl[i].vendor,
  203. cs5535_gpio_pci_tbl[i].device, NULL);
  204. if (pdev) {
  205. err = cs5535_gpio_probe(pdev, &cs5535_gpio_pci_tbl[i]);
  206. if (err)
  207. pci_dev_put(pdev);
  208. /* we only support a single CS5535/6 southbridge */
  209. break;
  210. }
  211. }
  212. return err;
  213. }
  214. static void __exit cs5535_gpio_free_pci(void)
  215. {
  216. cs5535_gpio_remove(cs5535_gpio_chip.pdev);
  217. pci_dev_put(cs5535_gpio_chip.pdev);
  218. }
  219. static int __init cs5535_gpio_init(void)
  220. {
  221. return cs5535_gpio_scan_pci();
  222. }
  223. static void __exit cs5535_gpio_exit(void)
  224. {
  225. cs5535_gpio_free_pci();
  226. }
  227. module_init(cs5535_gpio_init);
  228. module_exit(cs5535_gpio_exit);
  229. MODULE_AUTHOR("Andres Salomon <dilinger@collabora.co.uk>");
  230. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO driver");
  231. MODULE_LICENSE("GPL");