gpio-cs5535.c 9.2 KB

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