gpio-cs5535.c 9.3 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/platform_device.h>
  14. #include <linux/gpio.h>
  15. #include <linux/io.h>
  16. #include <linux/cs5535.h>
  17. #include <asm/msr.h>
  18. #define DRV_NAME "cs5535-gpio"
  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 platform_device *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. int cs5535_gpio_set_irq(unsigned group, unsigned irq)
  129. {
  130. uint32_t lo, hi;
  131. if (group > 7 || irq > 15)
  132. return -EINVAL;
  133. rdmsr(MSR_PIC_ZSEL_HIGH, lo, hi);
  134. lo &= ~(0xF << (group * 4));
  135. lo |= (irq & 0xF) << (group * 4);
  136. wrmsr(MSR_PIC_ZSEL_HIGH, lo, hi);
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(cs5535_gpio_set_irq);
  140. void cs5535_gpio_setup_event(unsigned offset, int pair, int pme)
  141. {
  142. struct cs5535_gpio_chip *chip = &cs5535_gpio_chip;
  143. uint32_t shift = (offset % 8) * 4;
  144. unsigned long flags;
  145. uint32_t val;
  146. if (offset >= 24)
  147. offset = GPIO_MAP_W;
  148. else if (offset >= 16)
  149. offset = GPIO_MAP_Z;
  150. else if (offset >= 8)
  151. offset = GPIO_MAP_Y;
  152. else
  153. offset = GPIO_MAP_X;
  154. spin_lock_irqsave(&chip->lock, flags);
  155. val = inl(chip->base + offset);
  156. /* Clear whatever was there before */
  157. val &= ~(0xF << shift);
  158. /* Set the new value */
  159. val |= ((pair & 7) << shift);
  160. /* Set the PME bit if this is a PME event */
  161. if (pme)
  162. val |= (1 << (shift + 3));
  163. outl(val, chip->base + offset);
  164. spin_unlock_irqrestore(&chip->lock, flags);
  165. }
  166. EXPORT_SYMBOL_GPL(cs5535_gpio_setup_event);
  167. /*
  168. * Generic gpio_chip API support.
  169. */
  170. static int chip_gpio_request(struct gpio_chip *c, unsigned offset)
  171. {
  172. struct cs5535_gpio_chip *chip =
  173. container_of(c, struct cs5535_gpio_chip, chip);
  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 =
  205. container_of(c, struct cs5535_gpio_chip, chip);
  206. unsigned long flags;
  207. spin_lock_irqsave(&chip->lock, flags);
  208. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  209. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_ENABLE);
  210. spin_unlock_irqrestore(&chip->lock, flags);
  211. return 0;
  212. }
  213. static int chip_direction_output(struct gpio_chip *c, unsigned offset, int val)
  214. {
  215. struct cs5535_gpio_chip *chip =
  216. container_of(c, struct cs5535_gpio_chip, chip);
  217. unsigned long flags;
  218. spin_lock_irqsave(&chip->lock, flags);
  219. __cs5535_gpio_set(chip, offset, GPIO_INPUT_ENABLE);
  220. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_ENABLE);
  221. if (val)
  222. __cs5535_gpio_set(chip, offset, GPIO_OUTPUT_VAL);
  223. else
  224. __cs5535_gpio_clear(chip, offset, GPIO_OUTPUT_VAL);
  225. spin_unlock_irqrestore(&chip->lock, flags);
  226. return 0;
  227. }
  228. static const char * const cs5535_gpio_names[] = {
  229. "GPIO0", "GPIO1", "GPIO2", "GPIO3",
  230. "GPIO4", "GPIO5", "GPIO6", "GPIO7",
  231. "GPIO8", "GPIO9", "GPIO10", "GPIO11",
  232. "GPIO12", "GPIO13", "GPIO14", "GPIO15",
  233. "GPIO16", "GPIO17", "GPIO18", "GPIO19",
  234. "GPIO20", "GPIO21", "GPIO22", NULL,
  235. "GPIO24", "GPIO25", "GPIO26", "GPIO27",
  236. "GPIO28", NULL, NULL, NULL,
  237. };
  238. static struct cs5535_gpio_chip cs5535_gpio_chip = {
  239. .chip = {
  240. .owner = THIS_MODULE,
  241. .label = DRV_NAME,
  242. .base = 0,
  243. .ngpio = 32,
  244. .names = cs5535_gpio_names,
  245. .request = chip_gpio_request,
  246. .get = chip_gpio_get,
  247. .set = chip_gpio_set,
  248. .direction_input = chip_direction_input,
  249. .direction_output = chip_direction_output,
  250. },
  251. };
  252. static int cs5535_gpio_probe(struct platform_device *pdev)
  253. {
  254. struct resource *res;
  255. int err = -EIO;
  256. ulong mask_orig = mask;
  257. /* There are two ways to get the GPIO base address; one is by
  258. * fetching it from MSR_LBAR_GPIO, the other is by reading the
  259. * PCI BAR info. The latter method is easier (especially across
  260. * different architectures), so we'll stick with that for now. If
  261. * it turns out to be unreliable in the face of crappy BIOSes, we
  262. * can always go back to using MSRs.. */
  263. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  264. if (!res) {
  265. dev_err(&pdev->dev, "can't fetch device resource info\n");
  266. goto done;
  267. }
  268. if (!devm_request_region(&pdev->dev, res->start, resource_size(res),
  269. pdev->name)) {
  270. dev_err(&pdev->dev, "can't request region\n");
  271. goto done;
  272. }
  273. /* set up the driver-specific struct */
  274. cs5535_gpio_chip.base = res->start;
  275. cs5535_gpio_chip.pdev = pdev;
  276. spin_lock_init(&cs5535_gpio_chip.lock);
  277. dev_info(&pdev->dev, "reserved resource region %pR\n", res);
  278. /* mask out reserved pins */
  279. mask &= 0x1F7FFFFF;
  280. /* do not allow pin 28, Power Button, as there's special handling
  281. * in the PMC needed. (note 12, p. 48) */
  282. mask &= ~(1 << 28);
  283. if (mask_orig != mask)
  284. dev_info(&pdev->dev, "mask changed from 0x%08lX to 0x%08lX\n",
  285. mask_orig, mask);
  286. /* finally, register with the generic GPIO API */
  287. err = gpiochip_add(&cs5535_gpio_chip.chip);
  288. if (err)
  289. goto done;
  290. return 0;
  291. done:
  292. return err;
  293. }
  294. static int cs5535_gpio_remove(struct platform_device *pdev)
  295. {
  296. gpiochip_remove(&cs5535_gpio_chip.chip);
  297. return 0;
  298. }
  299. static struct platform_driver cs5535_gpio_driver = {
  300. .driver = {
  301. .name = DRV_NAME,
  302. },
  303. .probe = cs5535_gpio_probe,
  304. .remove = cs5535_gpio_remove,
  305. };
  306. module_platform_driver(cs5535_gpio_driver);
  307. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  308. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO driver");
  309. MODULE_LICENSE("GPL");
  310. MODULE_ALIAS("platform:" DRV_NAME);