gpio-xgene.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * AppliedMicro X-Gene SoC GPIO Driver
  3. *
  4. * Copyright (c) 2014, Applied Micro Circuits Corporation
  5. * Author: Feng Kan <fkan@apm.com>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/acpi.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/gpio/driver.h>
  26. #include <linux/types.h>
  27. #include <linux/bitops.h>
  28. #define GPIO_SET_DR_OFFSET 0x0C
  29. #define GPIO_DATA_OFFSET 0x14
  30. #define GPIO_BANK_STRIDE 0x0C
  31. #define XGENE_GPIOS_PER_BANK 16
  32. #define XGENE_MAX_GPIO_BANKS 3
  33. #define XGENE_MAX_GPIOS (XGENE_GPIOS_PER_BANK * XGENE_MAX_GPIO_BANKS)
  34. #define GPIO_BIT_OFFSET(x) (x % XGENE_GPIOS_PER_BANK)
  35. #define GPIO_BANK_OFFSET(x) ((x / XGENE_GPIOS_PER_BANK) * GPIO_BANK_STRIDE)
  36. struct xgene_gpio {
  37. struct gpio_chip chip;
  38. void __iomem *base;
  39. spinlock_t lock;
  40. u32 set_dr_val[XGENE_MAX_GPIO_BANKS];
  41. };
  42. static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset)
  43. {
  44. struct xgene_gpio *chip = gpiochip_get_data(gc);
  45. unsigned long bank_offset;
  46. u32 bit_offset;
  47. bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset);
  48. bit_offset = GPIO_BIT_OFFSET(offset);
  49. return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
  50. }
  51. static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  52. {
  53. struct xgene_gpio *chip = gpiochip_get_data(gc);
  54. unsigned long bank_offset;
  55. u32 setval, bit_offset;
  56. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  57. bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK;
  58. setval = ioread32(chip->base + bank_offset);
  59. if (val)
  60. setval |= BIT(bit_offset);
  61. else
  62. setval &= ~BIT(bit_offset);
  63. iowrite32(setval, chip->base + bank_offset);
  64. }
  65. static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  66. {
  67. struct xgene_gpio *chip = gpiochip_get_data(gc);
  68. unsigned long flags;
  69. spin_lock_irqsave(&chip->lock, flags);
  70. __xgene_gpio_set(gc, offset, val);
  71. spin_unlock_irqrestore(&chip->lock, flags);
  72. }
  73. static int xgene_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  74. {
  75. struct xgene_gpio *chip = gpiochip_get_data(gc);
  76. unsigned long bank_offset, bit_offset;
  77. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  78. bit_offset = GPIO_BIT_OFFSET(offset);
  79. return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
  80. }
  81. static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
  82. {
  83. struct xgene_gpio *chip = gpiochip_get_data(gc);
  84. unsigned long flags, bank_offset;
  85. u32 dirval, bit_offset;
  86. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  87. bit_offset = GPIO_BIT_OFFSET(offset);
  88. spin_lock_irqsave(&chip->lock, flags);
  89. dirval = ioread32(chip->base + bank_offset);
  90. dirval |= BIT(bit_offset);
  91. iowrite32(dirval, chip->base + bank_offset);
  92. spin_unlock_irqrestore(&chip->lock, flags);
  93. return 0;
  94. }
  95. static int xgene_gpio_dir_out(struct gpio_chip *gc,
  96. unsigned int offset, int val)
  97. {
  98. struct xgene_gpio *chip = gpiochip_get_data(gc);
  99. unsigned long flags, bank_offset;
  100. u32 dirval, bit_offset;
  101. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  102. bit_offset = GPIO_BIT_OFFSET(offset);
  103. spin_lock_irqsave(&chip->lock, flags);
  104. dirval = ioread32(chip->base + bank_offset);
  105. dirval &= ~BIT(bit_offset);
  106. iowrite32(dirval, chip->base + bank_offset);
  107. __xgene_gpio_set(gc, offset, val);
  108. spin_unlock_irqrestore(&chip->lock, flags);
  109. return 0;
  110. }
  111. static __maybe_unused int xgene_gpio_suspend(struct device *dev)
  112. {
  113. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  114. unsigned long bank_offset;
  115. unsigned int bank;
  116. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  117. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  118. gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
  119. }
  120. return 0;
  121. }
  122. static __maybe_unused int xgene_gpio_resume(struct device *dev)
  123. {
  124. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  125. unsigned long bank_offset;
  126. unsigned int bank;
  127. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  128. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  129. iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
  130. }
  131. return 0;
  132. }
  133. static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
  134. static int xgene_gpio_probe(struct platform_device *pdev)
  135. {
  136. struct resource *res;
  137. struct xgene_gpio *gpio;
  138. int err = 0;
  139. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  140. if (!gpio) {
  141. err = -ENOMEM;
  142. goto err;
  143. }
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. if (!res) {
  146. err = -EINVAL;
  147. goto err;
  148. }
  149. gpio->base = devm_ioremap_nocache(&pdev->dev, res->start,
  150. resource_size(res));
  151. if (!gpio->base) {
  152. err = -ENOMEM;
  153. goto err;
  154. }
  155. gpio->chip.ngpio = XGENE_MAX_GPIOS;
  156. spin_lock_init(&gpio->lock);
  157. gpio->chip.parent = &pdev->dev;
  158. gpio->chip.get_direction = xgene_gpio_get_direction;
  159. gpio->chip.direction_input = xgene_gpio_dir_in;
  160. gpio->chip.direction_output = xgene_gpio_dir_out;
  161. gpio->chip.get = xgene_gpio_get;
  162. gpio->chip.set = xgene_gpio_set;
  163. gpio->chip.label = dev_name(&pdev->dev);
  164. gpio->chip.base = -1;
  165. platform_set_drvdata(pdev, gpio);
  166. err = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  167. if (err) {
  168. dev_err(&pdev->dev,
  169. "failed to register gpiochip.\n");
  170. goto err;
  171. }
  172. dev_info(&pdev->dev, "X-Gene GPIO driver registered.\n");
  173. return 0;
  174. err:
  175. dev_err(&pdev->dev, "X-Gene GPIO driver registration failed.\n");
  176. return err;
  177. }
  178. static const struct of_device_id xgene_gpio_of_match[] = {
  179. { .compatible = "apm,xgene-gpio", },
  180. {},
  181. };
  182. #ifdef CONFIG_ACPI
  183. static const struct acpi_device_id xgene_gpio_acpi_match[] = {
  184. { "APMC0D14", 0 },
  185. { },
  186. };
  187. #endif
  188. static struct platform_driver xgene_gpio_driver = {
  189. .driver = {
  190. .name = "xgene-gpio",
  191. .of_match_table = xgene_gpio_of_match,
  192. .acpi_match_table = ACPI_PTR(xgene_gpio_acpi_match),
  193. .pm = &xgene_gpio_pm,
  194. },
  195. .probe = xgene_gpio_probe,
  196. };
  197. builtin_platform_driver(xgene_gpio_driver);