cs5535-gpio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /*
  20. * Some GPIO pins
  21. * 31-29,23 : reserved (always mask out)
  22. * 28 : Power Button
  23. * 26 : PME#
  24. * 22-16 : LPC
  25. * 14,15 : SMBus
  26. * 9,8 : UART1
  27. * 7 : PCI INTB
  28. * 3,4 : UART2/DDC
  29. * 2 : IDE_IRQ0
  30. * 1 : AC_BEEP
  31. * 0 : PCI INTA
  32. *
  33. * If a mask was not specified, allow all except
  34. * reserved and Power Button
  35. */
  36. #define GPIO_DEFAULT_MASK 0x0F7FFFFF
  37. static ulong mask = GPIO_DEFAULT_MASK;
  38. module_param_named(mask, mask, ulong, 0444);
  39. MODULE_PARM_DESC(mask, "GPIO channel mask.");
  40. static struct cs5535_gpio_chip {
  41. struct gpio_chip chip;
  42. resource_size_t base;
  43. struct pci_dev *pdev;
  44. spinlock_t lock;
  45. } cs5535_gpio_chip;
  46. /*
  47. * The CS5535/CS5536 GPIOs support a number of extra features not defined
  48. * by the gpio_chip API, so these are exported. For a full list of the
  49. * registers, see include/linux/cs5535.h.
  50. */
  51. static void __cs5535_gpio_set(struct cs5535_gpio_chip *chip, unsigned offset,
  52. unsigned int reg)
  53. {
  54. if (offset < 16)
  55. /* low bank register */
  56. outl(1 << offset, chip->base + reg);
  57. else
  58. /* high bank register */
  59. outl(1 << (offset - 16), chip->base + 0x80 + reg);
  60. }
  61. void cs5535_gpio_set(unsigned offset, unsigned int reg)
  62. {
  63. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  64. unsigned long flags;
  65. spin_lock_irqsave(&chip->lock, flags);
  66. __cs5535_gpio_set(chip, offset, reg);
  67. spin_unlock_irqrestore(&chip->lock, flags);
  68. }
  69. EXPORT_SYMBOL_GPL(cs5535_gpio_set);
  70. static void __cs5535_gpio_clear(struct cs5535_gpio_chip *chip, unsigned offset,
  71. unsigned int reg)
  72. {
  73. if (offset < 16)
  74. /* low bank register */
  75. outl(1 << (offset + 16), chip->base + reg);
  76. else
  77. /* high bank register */
  78. outl(1 << offset, chip->base + 0x80 + reg);
  79. }
  80. void cs5535_gpio_clear(unsigned offset, unsigned int reg)
  81. {
  82. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  83. unsigned long flags;
  84. spin_lock_irqsave(&chip->lock, flags);
  85. __cs5535_gpio_clear(chip, offset, reg);
  86. spin_unlock_irqrestore(&chip->lock, flags);
  87. }
  88. EXPORT_SYMBOL_GPL(cs5535_gpio_clear);
  89. int cs5535_gpio_isset(unsigned offset, unsigned int reg)
  90. {
  91. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  92. unsigned long flags;
  93. long val;
  94. spin_lock_irqsave(&chip->lock, flags);
  95. if (offset < 16)
  96. /* low bank register */
  97. val = inl(chip->base + reg);
  98. else {
  99. /* high bank register */
  100. val = inl(chip->base + 0x80 + reg);
  101. offset -= 16;
  102. }
  103. spin_unlock_irqrestore(&chip->lock, flags);
  104. return (val & (1 << offset)) ? 1 : 0;
  105. }
  106. EXPORT_SYMBOL_GPL(cs5535_gpio_isset);
  107. /*
  108. * Generic gpio_chip API support.
  109. */
  110. static int chip_gpio_request(struct gpio_chip *c, unsigned offset)
  111. {
  112. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  113. unsigned long flags;
  114. spin_lock_irqsave(&chip->lock, flags);
  115. /* check if this pin is available */
  116. if ((mask & (1 << offset)) == 0) {
  117. dev_info(&chip->pdev->dev,
  118. "pin %u is not available (check mask)\n", offset);
  119. spin_unlock_irqrestore(&chip->lock, flags);
  120. return -EINVAL;
  121. }
  122. /* disable output aux 1 & 2 on this pin */
  123. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_AUX1);
  124. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_AUX2);
  125. /* disable input aux 1 on this pin */
  126. __cs5535_gpio_clear(chip, offset, GPIO_INPUT_AUX1);
  127. spin_unlock_irqrestore(&chip->lock, flags);
  128. return 0;
  129. }
  130. static int chip_gpio_get(struct gpio_chip *chip, unsigned offset)
  131. {
  132. return cs5535_gpio_isset(offset, GPIO_OUTPUT_VAL);
  133. }
  134. static void chip_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  135. {
  136. if (val)
  137. cs5535_gpio_set(offset, GPIO_OUTPUT_VAL);
  138. else
  139. cs5535_gpio_clear(offset, GPIO_OUTPUT_VAL);
  140. }
  141. static int chip_direction_input(struct gpio_chip *c, unsigned offset)
  142. {
  143. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  144. unsigned long flags;
  145. spin_lock_irqsave(&chip->lock, flags);
  146. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  147. spin_unlock_irqrestore(&chip->lock, flags);
  148. return 0;
  149. }
  150. static int chip_direction_output(struct gpio_chip *c, unsigned offset, int val)
  151. {
  152. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  153. unsigned long flags;
  154. spin_lock_irqsave(&chip->lock, flags);
  155. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_ENABLE);
  156. if (val)
  157. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_VAL);
  158. else
  159. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_VAL);
  160. spin_unlock_irqrestore(&chip->lock, flags);
  161. return 0;
  162. }
  163. static char *cs5535_gpio_names[] = {
  164. "GPIO0", "GPIO1", "GPIO2", "GPIO3",
  165. "GPIO4", "GPIO5", "GPIO6", "GPIO7",
  166. "GPIO8", "GPIO9", "GPIO10", "GPIO11",
  167. "GPIO12", "GPIO13", "GPIO14", "GPIO15",
  168. "GPIO16", "GPIO17", "GPIO18", "GPIO19",
  169. "GPIO20", "GPIO21", "GPIO22", NULL,
  170. "GPIO24", "GPIO25", "GPIO26", "GPIO27",
  171. "GPIO28", NULL, NULL, NULL,
  172. };
  173. static struct cs5535_gpio_chip cs5535_gpio_chip = {
  174. .chip = {
  175. .owner = THIS_MODULE,
  176. .label = DRV_NAME,
  177. .base = 0,
  178. .ngpio = 32,
  179. .names = cs5535_gpio_names,
  180. .request = chip_gpio_request,
  181. .get = chip_gpio_get,
  182. .set = chip_gpio_set,
  183. .direction_input = chip_direction_input,
  184. .direction_output = chip_direction_output,
  185. },
  186. };
  187. static int __init cs5535_gpio_probe(struct pci_dev *pdev,
  188. const struct pci_device_id *pci_id)
  189. {
  190. int err;
  191. ulong mask_orig = mask;
  192. /* There are two ways to get the GPIO base address; one is by
  193. * fetching it from MSR_LBAR_GPIO, the other is by reading the
  194. * PCI BAR info. The latter method is easier (especially across
  195. * different architectures), so we'll stick with that for now. If
  196. * it turns out to be unreliable in the face of crappy BIOSes, we
  197. * can always go back to using MSRs.. */
  198. err = pci_enable_device_io(pdev);
  199. if (err) {
  200. dev_err(&pdev->dev, "can't enable device IO\n");
  201. goto done;
  202. }
  203. err = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
  204. if (err) {
  205. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
  206. goto done;
  207. }
  208. /* set up the driver-specific struct */
  209. cs5535_gpio_chip.base = pci_resource_start(pdev, GPIO_BAR);
  210. cs5535_gpio_chip.pdev = pdev;
  211. spin_lock_init(&cs5535_gpio_chip.lock);
  212. dev_info(&pdev->dev, "allocated PCI BAR #%d: base 0x%llx\n", GPIO_BAR,
  213. (unsigned long long) cs5535_gpio_chip.base);
  214. /* mask out reserved pins */
  215. mask &= 0x1F7FFFFF;
  216. /* do not allow pin 28, Power Button, as there's special handling
  217. * in the PMC needed. (note 12, p. 48) */
  218. mask &= ~(1 << 28);
  219. if (mask_orig != mask)
  220. dev_info(&pdev->dev, "mask changed from 0x%08lX to 0x%08lX\n",
  221. mask_orig, mask);
  222. /* finally, register with the generic GPIO API */
  223. err = gpiochip_add(&cs5535_gpio_chip.chip);
  224. if (err)
  225. goto release_region;
  226. dev_info(&pdev->dev, DRV_NAME ": GPIO support successfully loaded.\n");
  227. return 0;
  228. release_region:
  229. pci_release_region(pdev, GPIO_BAR);
  230. done:
  231. return err;
  232. }
  233. static void __exit cs5535_gpio_remove(struct pci_dev *pdev)
  234. {
  235. int err;
  236. err = gpiochip_remove(&cs5535_gpio_chip.chip);
  237. if (err) {
  238. /* uhh? */
  239. dev_err(&pdev->dev, "unable to remove gpio_chip?\n");
  240. }
  241. pci_release_region(pdev, GPIO_BAR);
  242. }
  243. static struct pci_device_id cs5535_gpio_pci_tbl[] = {
  244. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  245. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  246. { 0, },
  247. };
  248. MODULE_DEVICE_TABLE(pci, cs5535_gpio_pci_tbl);
  249. /*
  250. * We can't use the standard PCI driver registration stuff here, since
  251. * that allows only one driver to bind to each PCI device (and we want
  252. * multiple drivers to be able to bind to the device). Instead, manually
  253. * scan for the PCI device, request a single region, and keep track of the
  254. * devices that we're using.
  255. */
  256. static int __init cs5535_gpio_scan_pci(void)
  257. {
  258. struct pci_dev *pdev;
  259. int err = -ENODEV;
  260. int i;
  261. for (i = 0; i < ARRAY_SIZE(cs5535_gpio_pci_tbl); i++) {
  262. pdev = pci_get_device(cs5535_gpio_pci_tbl[i].vendor,
  263. cs5535_gpio_pci_tbl[i].device, NULL);
  264. if (pdev) {
  265. err = cs5535_gpio_probe(pdev, &cs5535_gpio_pci_tbl[i]);
  266. if (err)
  267. pci_dev_put(pdev);
  268. /* we only support a single CS5535/6 southbridge */
  269. break;
  270. }
  271. }
  272. return err;
  273. }
  274. static void __exit cs5535_gpio_free_pci(void)
  275. {
  276. cs5535_gpio_remove(cs5535_gpio_chip.pdev);
  277. pci_dev_put(cs5535_gpio_chip.pdev);
  278. }
  279. static int __init cs5535_gpio_init(void)
  280. {
  281. return cs5535_gpio_scan_pci();
  282. }
  283. static void __exit cs5535_gpio_exit(void)
  284. {
  285. cs5535_gpio_free_pci();
  286. }
  287. module_init(cs5535_gpio_init);
  288. module_exit(cs5535_gpio_exit);
  289. MODULE_AUTHOR("Andres Salomon <dilinger@collabora.co.uk>");
  290. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO driver");
  291. MODULE_LICENSE("GPL");