cs5535-gpio.c 9.6 KB

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