gpio-cs5535.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD CS5535/CS5536 GPIO driver
  4. * Copyright (C) 2006 Advanced Micro Devices, Inc.
  5. * Copyright (C) 2007-2009 Andres Salomon <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/io.h>
  13. #include <linux/cs5535.h>
  14. #include <asm/msr.h>
  15. #define DRV_NAME "cs5535-gpio"
  16. /*
  17. * Some GPIO pins
  18. * 31-29,23 : reserved (always mask out)
  19. * 28 : Power Button
  20. * 26 : PME#
  21. * 22-16 : LPC
  22. * 14,15 : SMBus
  23. * 9,8 : UART1
  24. * 7 : PCI INTB
  25. * 3,4 : UART2/DDC
  26. * 2 : IDE_IRQ0
  27. * 1 : AC_BEEP
  28. * 0 : PCI INTA
  29. *
  30. * If a mask was not specified, allow all except
  31. * reserved and Power Button
  32. */
  33. #define GPIO_DEFAULT_MASK 0x0F7FFFFF
  34. static ulong mask = GPIO_DEFAULT_MASK;
  35. module_param_named(mask, mask, ulong, 0444);
  36. MODULE_PARM_DESC(mask, "GPIO channel mask.");
  37. /*
  38. * FIXME: convert this singleton driver to use the state container
  39. * design pattern, see Documentation/driver-api/driver-model/design-patterns.rst
  40. */
  41. static struct cs5535_gpio_chip {
  42. struct gpio_chip chip;
  43. resource_size_t base;
  44. struct platform_device *pdev;
  45. spinlock_t lock;
  46. } cs5535_gpio_chip;
  47. /*
  48. * The CS5535/CS5536 GPIOs support a number of extra features not defined
  49. * by the gpio_chip API, so these are exported. For a full list of the
  50. * registers, see include/linux/cs5535.h.
  51. */
  52. static void errata_outl(struct cs5535_gpio_chip *chip, u32 val,
  53. unsigned int reg)
  54. {
  55. unsigned long addr = chip->base + 0x80 + reg;
  56. /*
  57. * According to the CS5536 errata (#36), after suspend
  58. * a write to the high bank GPIO register will clear all
  59. * non-selected bits; the recommended workaround is a
  60. * read-modify-write operation.
  61. *
  62. * Don't apply this errata to the edge status GPIOs, as writing
  63. * to their lower bits will clear them.
  64. */
  65. if (reg != GPIO_POSITIVE_EDGE_STS && reg != GPIO_NEGATIVE_EDGE_STS) {
  66. if (val & 0xffff)
  67. val |= (inl(addr) & 0xffff); /* ignore the high bits */
  68. else
  69. val |= (inl(addr) ^ (val >> 16));
  70. }
  71. outl(val, addr);
  72. }
  73. static void __cs5535_gpio_set(struct cs5535_gpio_chip *chip, unsigned offset,
  74. unsigned int reg)
  75. {
  76. if (offset < 16)
  77. /* low bank register */
  78. outl(1 << offset, chip->base + reg);
  79. else
  80. /* high bank register */
  81. errata_outl(chip, 1 << (offset - 16), reg);
  82. }
  83. void cs5535_gpio_set(unsigned offset, unsigned int reg)
  84. {
  85. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  86. unsigned long flags;
  87. spin_lock_irqsave(&chip->lock, flags);
  88. __cs5535_gpio_set(chip, offset, reg);
  89. spin_unlock_irqrestore(&chip->lock, flags);
  90. }
  91. EXPORT_SYMBOL_GPL(cs5535_gpio_set);
  92. static void __cs5535_gpio_clear(struct cs5535_gpio_chip *chip, unsigned offset,
  93. unsigned int reg)
  94. {
  95. if (offset < 16)
  96. /* low bank register */
  97. outl(1 << (offset + 16), chip->base + reg);
  98. else
  99. /* high bank register */
  100. errata_outl(chip, 1 << offset, reg);
  101. }
  102. void cs5535_gpio_clear(unsigned offset, unsigned int reg)
  103. {
  104. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  105. unsigned long flags;
  106. spin_lock_irqsave(&chip->lock, flags);
  107. __cs5535_gpio_clear(chip, offset, reg);
  108. spin_unlock_irqrestore(&chip->lock, flags);
  109. }
  110. EXPORT_SYMBOL_GPL(cs5535_gpio_clear);
  111. int cs5535_gpio_isset(unsigned offset, unsigned int reg)
  112. {
  113. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  114. unsigned long flags;
  115. long val;
  116. spin_lock_irqsave(&chip->lock, flags);
  117. if (offset < 16)
  118. /* low bank register */
  119. val = inl(chip->base + reg);
  120. else {
  121. /* high bank register */
  122. val = inl(chip->base + 0x80 + reg);
  123. offset -= 16;
  124. }
  125. spin_unlock_irqrestore(&chip->lock, flags);
  126. return (val & (1 << offset)) ? 1 : 0;
  127. }
  128. EXPORT_SYMBOL_GPL(cs5535_gpio_isset);
  129. int cs5535_gpio_set_irq(unsigned group, unsigned irq)
  130. {
  131. uint32_t lo, hi;
  132. if (group > 7 || irq > 15)
  133. return -EINVAL;
  134. rdmsr(MSR_PIC_ZSEL_HIGH, lo, hi);
  135. lo &= ~(0xF << (group * 4));
  136. lo |= (irq & 0xF) << (group * 4);
  137. wrmsr(MSR_PIC_ZSEL_HIGH, lo, hi);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(cs5535_gpio_set_irq);
  141. void cs5535_gpio_setup_event(unsigned offset, int pair, int pme)
  142. {
  143. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  144. uint32_t shift = (offset % 8) * 4;
  145. unsigned long flags;
  146. uint32_t val;
  147. if (offset >= 24)
  148. offset = GPIO_MAP_W;
  149. else if (offset >= 16)
  150. offset = GPIO_MAP_Z;
  151. else if (offset >= 8)
  152. offset = GPIO_MAP_Y;
  153. else
  154. offset = GPIO_MAP_X;
  155. spin_lock_irqsave(&chip->lock, flags);
  156. val = inl(chip->base + offset);
  157. /* Clear whatever was there before */
  158. val &= ~(0xF << shift);
  159. /* Set the new value */
  160. val |= ((pair & 7) << shift);
  161. /* Set the PME bit if this is a PME event */
  162. if (pme)
  163. val |= (1 << (shift + 3));
  164. outl(val, chip->base + offset);
  165. spin_unlock_irqrestore(&chip->lock, flags);
  166. }
  167. EXPORT_SYMBOL_GPL(cs5535_gpio_setup_event);
  168. /*
  169. * Generic gpio_chip API support.
  170. */
  171. static int chip_gpio_request(struct gpio_chip *c, unsigned offset)
  172. {
  173. struct cs5535_gpio_chip *chip = gpiochip_get_data(c);
  174. unsigned long flags;
  175. spin_lock_irqsave(&chip->lock, flags);
  176. /* check if this pin is available */
  177. if ((mask & (1 << offset)) == 0) {
  178. dev_info(&chip->pdev->dev,
  179. "pin %u is not available (check mask)\n", offset);
  180. spin_unlock_irqrestore(&chip->lock, flags);
  181. return -EINVAL;
  182. }
  183. /* disable output aux 1 & 2 on this pin */
  184. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_AUX1);
  185. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_AUX2);
  186. /* disable input aux 1 on this pin */
  187. __cs5535_gpio_clear(chip, offset, GPIO_INPUT_AUX1);
  188. spin_unlock_irqrestore(&chip->lock, flags);
  189. return 0;
  190. }
  191. static int chip_gpio_get(struct gpio_chip *chip, unsigned offset)
  192. {
  193. return cs5535_gpio_isset(offset, GPIO_READ_BACK);
  194. }
  195. static void chip_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  196. {
  197. if (val)
  198. cs5535_gpio_set(offset, GPIO_OUTPUT_VAL);
  199. else
  200. cs5535_gpio_clear(offset, GPIO_OUTPUT_VAL);
  201. }
  202. static int chip_direction_input(struct gpio_chip *c, unsigned offset)
  203. {
  204. struct cs5535_gpio_chip *chip = gpiochip_get_data(c);
  205. unsigned long flags;
  206. spin_lock_irqsave(&chip->lock, flags);
  207. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  208. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_ENABLE);
  209. spin_unlock_irqrestore(&chip->lock, flags);
  210. return 0;
  211. }
  212. static int chip_direction_output(struct gpio_chip *c, unsigned offset, int val)
  213. {
  214. struct cs5535_gpio_chip *chip = gpiochip_get_data(c);
  215. unsigned long flags;
  216. spin_lock_irqsave(&chip->lock, flags);
  217. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  218. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_ENABLE);
  219. if (val)
  220. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_VAL);
  221. else
  222. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_VAL);
  223. spin_unlock_irqrestore(&chip->lock, flags);
  224. return 0;
  225. }
  226. static const char * const cs5535_gpio_names[] = {
  227. "GPIO0", "GPIO1", "GPIO2", "GPIO3",
  228. "GPIO4", "GPIO5", "GPIO6", "GPIO7",
  229. "GPIO8", "GPIO9", "GPIO10", "GPIO11",
  230. "GPIO12", "GPIO13", "GPIO14", "GPIO15",
  231. "GPIO16", "GPIO17", "GPIO18", "GPIO19",
  232. "GPIO20", "GPIO21", "GPIO22", NULL,
  233. "GPIO24", "GPIO25", "GPIO26", "GPIO27",
  234. "GPIO28", NULL, NULL, NULL,
  235. };
  236. static struct cs5535_gpio_chip cs5535_gpio_chip = {
  237. .chip = {
  238. .owner = THIS_MODULE,
  239. .label = DRV_NAME,
  240. .base = 0,
  241. .ngpio = 32,
  242. .names = cs5535_gpio_names,
  243. .request = chip_gpio_request,
  244. .get = chip_gpio_get,
  245. .set = chip_gpio_set,
  246. .direction_input = chip_direction_input,
  247. .direction_output = chip_direction_output,
  248. },
  249. };
  250. static int cs5535_gpio_probe(struct platform_device *pdev)
  251. {
  252. struct resource *res;
  253. int err = -EIO;
  254. ulong mask_orig = mask;
  255. /* There are two ways to get the GPIO base address; one is by
  256. * fetching it from MSR_LBAR_GPIO, the other is by reading the
  257. * PCI BAR info. The latter method is easier (especially across
  258. * different architectures), so we'll stick with that for now. If
  259. * it turns out to be unreliable in the face of crappy BIOSes, we
  260. * can always go back to using MSRs.. */
  261. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  262. if (!res) {
  263. dev_err(&pdev->dev, "can't fetch device resource info\n");
  264. return err;
  265. }
  266. if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
  267. pdev->name)) {
  268. dev_err(&pdev->dev, "can't request region\n");
  269. return err;
  270. }
  271. /* set up the driver-specific struct */
  272. cs5535_gpio_chip.base = res->start;
  273. cs5535_gpio_chip.pdev = pdev;
  274. spin_lock_init(&cs5535_gpio_chip.lock);
  275. dev_info(&pdev->dev, "reserved resource region %pR\n", res);
  276. /* mask out reserved pins */
  277. mask &= 0x1F7FFFFF;
  278. /* do not allow pin 28, Power Button, as there's special handling
  279. * in the PMC needed. (note 12, p. 48) */
  280. mask &= ~(1 << 28);
  281. if (mask_orig != mask)
  282. dev_info(&pdev->dev, "mask changed from 0x%08lX to 0x%08lX\n",
  283. mask_orig, mask);
  284. /* finally, register with the generic GPIO API */
  285. return devm_gpiochip_add_data(&pdev->dev, &cs5535_gpio_chip.chip,
  286. &cs5535_gpio_chip);
  287. }
  288. static struct platform_driver cs5535_gpio_driver = {
  289. .driver = {
  290. .name = DRV_NAME,
  291. },
  292. .probe = cs5535_gpio_probe,
  293. };
  294. module_platform_driver(cs5535_gpio_driver);
  295. MODULE_AUTHOR("Andres Salomon <[email protected]>");
  296. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO driver");
  297. MODULE_LICENSE("GPL");
  298. MODULE_ALIAS("platform:" DRV_NAME);