cs5535-gpio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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_READ_BACK);
  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. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_ENABLE);
  148. spin_unlock_irqrestore(&chip->lock, flags);
  149. return 0;
  150. }
  151. static int chip_direction_output(struct gpio_chip *c, unsigned offset, int val)
  152. {
  153. struct cs5535_gpio_chip *chip = (struct cs5535_gpio_chip *) c;
  154. unsigned long flags;
  155. spin_lock_irqsave(&chip->lock, flags);
  156. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  157. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_ENABLE);
  158. if (val)
  159. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_VAL);
  160. else
  161. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_VAL);
  162. spin_unlock_irqrestore(&chip->lock, flags);
  163. return 0;
  164. }
  165. static char *cs5535_gpio_names[] = {
  166. "GPIO0", "GPIO1", "GPIO2", "GPIO3",
  167. "GPIO4", "GPIO5", "GPIO6", "GPIO7",
  168. "GPIO8", "GPIO9", "GPIO10", "GPIO11",
  169. "GPIO12", "GPIO13", "GPIO14", "GPIO15",
  170. "GPIO16", "GPIO17", "GPIO18", "GPIO19",
  171. "GPIO20", "GPIO21", "GPIO22", NULL,
  172. "GPIO24", "GPIO25", "GPIO26", "GPIO27",
  173. "GPIO28", NULL, NULL, NULL,
  174. };
  175. static struct cs5535_gpio_chip cs5535_gpio_chip = {
  176. .chip = {
  177. .owner = THIS_MODULE,
  178. .label = DRV_NAME,
  179. .base = 0,
  180. .ngpio = 32,
  181. .names = cs5535_gpio_names,
  182. .request = chip_gpio_request,
  183. .get = chip_gpio_get,
  184. .set = chip_gpio_set,
  185. .direction_input = chip_direction_input,
  186. .direction_output = chip_direction_output,
  187. },
  188. };
  189. static int __init cs5535_gpio_probe(struct pci_dev *pdev,
  190. const struct pci_device_id *pci_id)
  191. {
  192. int err;
  193. ulong mask_orig = mask;
  194. /* There are two ways to get the GPIO base address; one is by
  195. * fetching it from MSR_LBAR_GPIO, the other is by reading the
  196. * PCI BAR info. The latter method is easier (especially across
  197. * different architectures), so we'll stick with that for now. If
  198. * it turns out to be unreliable in the face of crappy BIOSes, we
  199. * can always go back to using MSRs.. */
  200. err = pci_enable_device_io(pdev);
  201. if (err) {
  202. dev_err(&pdev->dev, "can't enable device IO\n");
  203. goto done;
  204. }
  205. err = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
  206. if (err) {
  207. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
  208. goto done;
  209. }
  210. /* set up the driver-specific struct */
  211. cs5535_gpio_chip.base = pci_resource_start(pdev, GPIO_BAR);
  212. cs5535_gpio_chip.pdev = pdev;
  213. spin_lock_init(&cs5535_gpio_chip.lock);
  214. dev_info(&pdev->dev, "allocated PCI BAR #%d: base 0x%llx\n", GPIO_BAR,
  215. (unsigned long long) cs5535_gpio_chip.base);
  216. /* mask out reserved pins */
  217. mask &= 0x1F7FFFFF;
  218. /* do not allow pin 28, Power Button, as there's special handling
  219. * in the PMC needed. (note 12, p. 48) */
  220. mask &= ~(1 << 28);
  221. if (mask_orig != mask)
  222. dev_info(&pdev->dev, "mask changed from 0x%08lX to 0x%08lX\n",
  223. mask_orig, mask);
  224. /* finally, register with the generic GPIO API */
  225. err = gpiochip_add(&cs5535_gpio_chip.chip);
  226. if (err)
  227. goto release_region;
  228. dev_info(&pdev->dev, DRV_NAME ": GPIO support successfully loaded.\n");
  229. return 0;
  230. release_region:
  231. pci_release_region(pdev, GPIO_BAR);
  232. done:
  233. return err;
  234. }
  235. static void __exit cs5535_gpio_remove(struct pci_dev *pdev)
  236. {
  237. int err;
  238. err = gpiochip_remove(&cs5535_gpio_chip.chip);
  239. if (err) {
  240. /* uhh? */
  241. dev_err(&pdev->dev, "unable to remove gpio_chip?\n");
  242. }
  243. pci_release_region(pdev, GPIO_BAR);
  244. }
  245. static struct pci_device_id cs5535_gpio_pci_tbl[] = {
  246. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  247. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  248. { 0, },
  249. };
  250. MODULE_DEVICE_TABLE(pci, cs5535_gpio_pci_tbl);
  251. /*
  252. * We can't use the standard PCI driver registration stuff here, since
  253. * that allows only one driver to bind to each PCI device (and we want
  254. * multiple drivers to be able to bind to the device). Instead, manually
  255. * scan for the PCI device, request a single region, and keep track of the
  256. * devices that we're using.
  257. */
  258. static int __init cs5535_gpio_scan_pci(void)
  259. {
  260. struct pci_dev *pdev;
  261. int err = -ENODEV;
  262. int i;
  263. for (i = 0; i < ARRAY_SIZE(cs5535_gpio_pci_tbl); i++) {
  264. pdev = pci_get_device(cs5535_gpio_pci_tbl[i].vendor,
  265. cs5535_gpio_pci_tbl[i].device, NULL);
  266. if (pdev) {
  267. err = cs5535_gpio_probe(pdev, &cs5535_gpio_pci_tbl[i]);
  268. if (err)
  269. pci_dev_put(pdev);
  270. /* we only support a single CS5535/6 southbridge */
  271. break;
  272. }
  273. }
  274. return err;
  275. }
  276. static void __exit cs5535_gpio_free_pci(void)
  277. {
  278. cs5535_gpio_remove(cs5535_gpio_chip.pdev);
  279. pci_dev_put(cs5535_gpio_chip.pdev);
  280. }
  281. static int __init cs5535_gpio_init(void)
  282. {
  283. return cs5535_gpio_scan_pci();
  284. }
  285. static void __exit cs5535_gpio_exit(void)
  286. {
  287. cs5535_gpio_free_pci();
  288. }
  289. module_init(cs5535_gpio_init);
  290. module_exit(cs5535_gpio_exit);
  291. MODULE_AUTHOR("Andres Salomon <dilinger@collabora.co.uk>");
  292. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO driver");
  293. MODULE_LICENSE("GPL");