gpio-amd8111.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static int amd_gpio_request(struct gpio_chip *chip, unsigned offset)
  73. {
  74. struct amd_gpio *agp = gpiochip_get_data(chip);
  75. agp->orig[offset] = ioread8(agp->pm + AMD_REG_GPIO(offset)) &
  76. (AMD_GPIO_DEBOUNCE | AMD_GPIO_MODE_MASK | AMD_GPIO_X_MASK);
  77. dev_dbg(&agp->pdev->dev, "Requested gpio %d, data %x\n", offset, agp->orig[offset]);
  78. return 0;
  79. }
  80. static void amd_gpio_free(struct gpio_chip *chip, unsigned offset)
  81. {
  82. struct amd_gpio *agp = gpiochip_get_data(chip);
  83. dev_dbg(&agp->pdev->dev, "Freed gpio %d, data %x\n", offset, agp->orig[offset]);
  84. iowrite8(agp->orig[offset], agp->pm + AMD_REG_GPIO(offset));
  85. }
  86. static void amd_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  87. {
  88. struct amd_gpio *agp = gpiochip_get_data(chip);
  89. u8 temp;
  90. unsigned long flags;
  91. spin_lock_irqsave(&agp->lock, flags);
  92. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  93. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_OUT | (value ? AMD_GPIO_X_OUT_HI : AMD_GPIO_X_OUT_LOW);
  94. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  95. spin_unlock_irqrestore(&agp->lock, flags);
  96. dev_dbg(&agp->pdev->dev, "Setting gpio %d, value %d, reg=%02x\n", offset, !!value, temp);
  97. }
  98. static int amd_gpio_get(struct gpio_chip *chip, unsigned offset)
  99. {
  100. struct amd_gpio *agp = gpiochip_get_data(chip);
  101. u8 temp;
  102. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  103. dev_dbg(&agp->pdev->dev, "Getting gpio %d, reg=%02x\n", offset, temp);
  104. return (temp & AMD_GPIO_RTIN) ? 1 : 0;
  105. }
  106. static int amd_gpio_dirout(struct gpio_chip *chip, unsigned offset, int value)
  107. {
  108. struct amd_gpio *agp = gpiochip_get_data(chip);
  109. u8 temp;
  110. unsigned long flags;
  111. spin_lock_irqsave(&agp->lock, flags);
  112. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  113. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_OUT | (value ? AMD_GPIO_X_OUT_HI : AMD_GPIO_X_OUT_LOW);
  114. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  115. spin_unlock_irqrestore(&agp->lock, flags);
  116. dev_dbg(&agp->pdev->dev, "Dirout gpio %d, value %d, reg=%02x\n", offset, !!value, temp);
  117. return 0;
  118. }
  119. static int amd_gpio_dirin(struct gpio_chip *chip, unsigned offset)
  120. {
  121. struct amd_gpio *agp = gpiochip_get_data(chip);
  122. u8 temp;
  123. unsigned long flags;
  124. spin_lock_irqsave(&agp->lock, flags);
  125. temp = ioread8(agp->pm + AMD_REG_GPIO(offset));
  126. temp = (temp & AMD_GPIO_DEBOUNCE) | AMD_GPIO_MODE_IN;
  127. iowrite8(temp, agp->pm + AMD_REG_GPIO(offset));
  128. spin_unlock_irqrestore(&agp->lock, flags);
  129. dev_dbg(&agp->pdev->dev, "Dirin gpio %d, reg=%02x\n", offset, temp);
  130. return 0;
  131. }
  132. static struct amd_gpio gp = {
  133. .chip = {
  134. .label = "AMD GPIO",
  135. .owner = THIS_MODULE,
  136. .base = -1,
  137. .ngpio = 32,
  138. .request = amd_gpio_request,
  139. .free = amd_gpio_free,
  140. .set = amd_gpio_set,
  141. .get = amd_gpio_get,
  142. .direction_output = amd_gpio_dirout,
  143. .direction_input = amd_gpio_dirin,
  144. },
  145. };
  146. static int __init amd_gpio_init(void)
  147. {
  148. int err = -ENODEV;
  149. struct pci_dev *pdev = NULL;
  150. const struct pci_device_id *ent;
  151. /* We look for our device - AMD South Bridge
  152. * I don't know about a system with two such bridges,
  153. * so we can assume that there is max. one device.
  154. *
  155. * We can't use plain pci_driver mechanism,
  156. * as the device is really a multiple function device,
  157. * main driver that binds to the pci_device is an smbus
  158. * driver and have to find & bind to the device this way.
  159. */
  160. for_each_pci_dev(pdev) {
  161. ent = pci_match_id(pci_tbl, pdev);
  162. if (ent)
  163. goto found;
  164. }
  165. /* Device not found. */
  166. goto out;
  167. found:
  168. err = pci_read_config_dword(pdev, 0x58, &gp.pmbase);
  169. if (err)
  170. goto out;
  171. err = -EIO;
  172. gp.pmbase &= 0x0000FF00;
  173. if (gp.pmbase == 0)
  174. goto out;
  175. if (!request_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE, "AMD GPIO")) {
  176. dev_err(&pdev->dev, "AMD GPIO region 0x%x already in use!\n",
  177. gp.pmbase + PMBASE_OFFSET);
  178. err = -EBUSY;
  179. goto out;
  180. }
  181. gp.pm = ioport_map(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  182. if (!gp.pm) {
  183. dev_err(&pdev->dev, "Couldn't map io port into io memory\n");
  184. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  185. err = -ENOMEM;
  186. goto out;
  187. }
  188. gp.pdev = pdev;
  189. gp.chip.parent = &pdev->dev;
  190. spin_lock_init(&gp.lock);
  191. printk(KERN_INFO "AMD-8111 GPIO detected\n");
  192. err = gpiochip_add_data(&gp.chip, &gp);
  193. if (err) {
  194. printk(KERN_ERR "GPIO registering failed (%d)\n",
  195. err);
  196. ioport_unmap(gp.pm);
  197. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  198. goto out;
  199. }
  200. out:
  201. return err;
  202. }
  203. static void __exit amd_gpio_exit(void)
  204. {
  205. gpiochip_remove(&gp.chip);
  206. ioport_unmap(gp.pm);
  207. release_region(gp.pmbase + PMBASE_OFFSET, PMBASE_SIZE);
  208. }
  209. module_init(amd_gpio_init);
  210. module_exit(amd_gpio_exit);
  211. MODULE_AUTHOR("The Linux Kernel team");
  212. MODULE_DESCRIPTION("GPIO driver for AMD chipsets");
  213. MODULE_LICENSE("GPL");