gpio-amd8111.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * GPIO driver for AMD 8111 south bridges
  3. *
  4. * Copyright (c) 2012 Dmitry Eremin-Solenikov
  5. *
  6. * Based on the AMD RNG driver:
  7. * Copyright 2005 (c) MontaVista Software, Inc.
  8. * with the majority of the code coming from:
  9. *
  10. * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  11. * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  12. *
  13. * derived from
  14. *
  15. * Hardware driver for the AMD 768 Random Number Generator (RNG)
  16. * (c) Copyright 2001 Red Hat Inc
  17. *
  18. * derived from
  19. *
  20. * Hardware driver for Intel i810 Random Number Generator (RNG)
  21. * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  22. * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  23. *
  24. * This file is licensed under the terms of the GNU General Public
  25. * License version 2. This program is licensed "as is" without any
  26. * warranty of any kind, whether express or implied.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/gpio.h>
  31. #include <linux/pci.h>
  32. #include <linux/spinlock.h>
  33. #define PMBASE_OFFSET 0xb0
  34. #define PMBASE_SIZE 0x30
  35. #define AMD_REG_GPIO(i) (0x10 + (i))
  36. #define AMD_GPIO_LTCH_STS 0x40 /* Latch status, w1 */
  37. #define AMD_GPIO_RTIN 0x20 /* Real Time in, ro */
  38. #define AMD_GPIO_DEBOUNCE 0x10 /* Debounce, rw */
  39. #define AMD_GPIO_MODE_MASK 0x0c /* Pin Mode Select, rw */
  40. #define AMD_GPIO_MODE_IN 0x00
  41. #define AMD_GPIO_MODE_OUT 0x04
  42. /* Enable alternative (e.g. clkout, IRQ, etc) function of the pin */
  43. #define AMD_GPIO_MODE_ALTFN 0x08 /* Or 0x09 */
  44. #define AMD_GPIO_X_MASK 0x03 /* In/Out specific, rw */
  45. #define AMD_GPIO_X_IN_ACTIVEHI 0x01 /* Active High */
  46. #define AMD_GPIO_X_IN_LATCH 0x02 /* Latched version is selected */
  47. #define AMD_GPIO_X_OUT_LOW 0x00
  48. #define AMD_GPIO_X_OUT_HI 0x01
  49. #define AMD_GPIO_X_OUT_CLK0 0x02
  50. #define AMD_GPIO_X_OUT_CLK1 0x03
  51. /*
  52. * Data for PCI driver interface
  53. *
  54. * This data only exists for exporting the supported
  55. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  56. * register a pci_driver, because someone else might one day
  57. * want to register another driver on the same PCI id.
  58. */
  59. static const struct pci_device_id pci_tbl[] = {
  60. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS), 0 },
  61. { 0, }, /* terminate list */
  62. };
  63. MODULE_DEVICE_TABLE(pci, pci_tbl);
  64. struct amd_gpio {
  65. struct gpio_chip chip;
  66. u32 pmbase;
  67. void __iomem *pm;
  68. struct pci_dev *pdev;
  69. spinlock_t lock; /* guards hw registers and orig table */
  70. u8 orig[32];
  71. };
  72. #define to_agp(chip) container_of(chip, struct amd_gpio, chip)
  73. static int amd_gpio_request(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct amd_gpio *agp = to_agp(chip);
  76. agp->orig[offset] = ioread8(agp->pm + AMD_REG_GPIO(offset)) &
  77. (AMD_GPIO_DEBOUNCE | AMD_GPIO_MODE_MASK | AMD_GPIO_X_MASK);
  78. dev_dbg(&agp->pdev->dev, "Requested gpio %d, data %x\n", offset, agp->orig[offset]);
  79. return 0;
  80. }
  81. static void amd_gpio_free(struct gpio_chip *chip, unsigned offset)
  82. {
  83. struct amd_gpio *agp = to_agp(chip);
  84. dev_dbg(&agp->pdev->dev, "Freed gpio %d, data %x\n", offset, agp->orig[offset]);
  85. iowrite8(agp->orig[offset], agp->pm + AMD_REG_GPIO(offset));
  86. }
  87. static void amd_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  88. {
  89. struct amd_gpio *agp = to_agp(chip);
  90. u8 temp;
  91. unsigned long flags;
  92. spin_lock_irqsave(&agp->lock, flags);
  93. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  94. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_OUT | (value ? AMD_GPIO_X_OUT_HI : AMD_GPIO_X_OUT_LOW);
  95. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  96. spin_unlock_irqrestore(&agp->lock, flags);
  97. dev_dbg(&agp->pdev->dev, "Setting gpio %d, value %d, reg=%02x\n", offset, !!value, temp);
  98. }
  99. static int amd_gpio_get(struct gpio_chip *chip, unsigned offset)
  100. {
  101. struct amd_gpio *agp = to_agp(chip);
  102. u8 temp;
  103. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  104. dev_dbg(&agp->pdev->dev, "Getting gpio %d, reg=%02x\n", offset, temp);
  105. return (temp & AMD_GPIO_RTIN) ? 1 : 0;
  106. }
  107. static int amd_gpio_dirout(struct gpio_chip *chip, unsigned offset, int value)
  108. {
  109. struct amd_gpio *agp = to_agp(chip);
  110. u8 temp;
  111. unsigned long flags;
  112. spin_lock_irqsave(&agp->lock, flags);
  113. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  114. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_OUT | (value ? AMD_GPIO_X_OUT_HI : AMD_GPIO_X_OUT_LOW);
  115. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  116. spin_unlock_irqrestore(&agp->lock, flags);
  117. dev_dbg(&agp->pdev->dev, "Dirout gpio %d, value %d, reg=%02x\n", offset, !!value, temp);
  118. return 0;
  119. }
  120. static int amd_gpio_dirin(struct gpio_chip *chip, unsigned offset)
  121. {
  122. struct amd_gpio *agp = to_agp(chip);
  123. u8 temp;
  124. unsigned long flags;
  125. spin_lock_irqsave(&agp->lock, flags);
  126. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  127. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_IN;
  128. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  129. spin_unlock_irqrestore(&agp->lock, flags);
  130. dev_dbg(&agp->pdev->dev, "Dirin gpio %d, reg=%02x\n", offset, temp);
  131. return 0;
  132. }
  133. static struct amd_gpio gp = {
  134. .chip = {
  135. .label = "AMD GPIO",
  136. .owner = THIS_MODULE,
  137. .base = -1,
  138. .ngpio = 32,
  139. .request = amd_gpio_request,
  140. .free = amd_gpio_free,
  141. .set = amd_gpio_set,
  142. .get = amd_gpio_get,
  143. .direction_output = amd_gpio_dirout,
  144. .direction_input = amd_gpio_dirin,
  145. },
  146. };
  147. static int __init amd_gpio_init(void)
  148. {
  149. int err = -ENODEV;
  150. struct pci_dev *pdev = NULL;
  151. const struct pci_device_id *ent;
  152. /* We look for our device - AMD South Bridge
  153. * I don't know about a system with two such bridges,
  154. * so we can assume that there is max. one device.
  155. *
  156. * We can't use plain pci_driver mechanism,
  157. * as the device is really a multiple function device,
  158. * main driver that binds to the pci_device is an smbus
  159. * driver and have to find & bind to the device this way.
  160. */
  161. for_each_pci_dev(pdev) {
  162. ent = pci_match_id(pci_tbl, pdev);
  163. if (ent)
  164. goto found;
  165. }
  166. /* Device not found. */
  167. goto out;
  168. found:
  169. err = pci_read_config_dword(pdev, 0x58, &gp.pmbase);
  170. if (err)
  171. goto out;
  172. err = -EIO;
  173. gp.pmbase &= 0x0000FF00;
  174. if (gp.pmbase == 0)
  175. goto out;
  176. if (!request_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE, "AMD GPIO")) {
  177. dev_err(&pdev->dev, "AMD GPIO region 0x%x already in use!\n",
  178. gp.pmbase + PMBASE_OFFSET);
  179. err = -EBUSY;
  180. goto out;
  181. }
  182. gp.pm = ioport_map(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  183. if (!gp.pm) {
  184. dev_err(&pdev->dev, "Couldn't map io port into io memory\n");
  185. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  186. err = -ENOMEM;
  187. goto out;
  188. }
  189. gp.pdev = pdev;
  190. gp.chip.dev = &pdev->dev;
  191. spin_lock_init(&gp.lock);
  192. printk(KERN_INFO "AMD-8111 GPIO detected\n");
  193. err = gpiochip_add(&gp.chip);
  194. if (err) {
  195. printk(KERN_ERR "GPIO registering failed (%d)\n",
  196. err);
  197. ioport_unmap(gp.pm);
  198. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  199. goto out;
  200. }
  201. out:
  202. return err;
  203. }
  204. static void __exit amd_gpio_exit(void)
  205. {
  206. gpiochip_remove(&gp.chip);
  207. ioport_unmap(gp.pm);
  208. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  209. }
  210. module_init(amd_gpio_init);
  211. module_exit(amd_gpio_exit);
  212. MODULE_AUTHOR("The Linux Kernel team");
  213. MODULE_DESCRIPTION("GPIO driver for AMD chipsets");
  214. MODULE_LICENSE("GPL");