cs5535-gpio.c 9.2 KB

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