gpio-xgene.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppliedMicro X-Gene SoC GPIO Driver
  4. *
  5. * Copyright (c) 2014, Applied Micro Circuits Corporation
  6. * Author: Feng Kan <[email protected]>.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #define GPIO_SET_DR_OFFSET 0x0C
  18. #define GPIO_DATA_OFFSET 0x14
  19. #define GPIO_BANK_STRIDE 0x0C
  20. #define XGENE_GPIOS_PER_BANK 16
  21. #define XGENE_MAX_GPIO_BANKS 3
  22. #define XGENE_MAX_GPIOS (XGENE_GPIOS_PER_BANK * XGENE_MAX_GPIO_BANKS)
  23. #define GPIO_BIT_OFFSET(x) (x % XGENE_GPIOS_PER_BANK)
  24. #define GPIO_BANK_OFFSET(x) ((x / XGENE_GPIOS_PER_BANK) * GPIO_BANK_STRIDE)
  25. struct xgene_gpio {
  26. struct gpio_chip chip;
  27. void __iomem *base;
  28. spinlock_t lock;
  29. u32 set_dr_val[XGENE_MAX_GPIO_BANKS];
  30. };
  31. static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset)
  32. {
  33. struct xgene_gpio *chip = gpiochip_get_data(gc);
  34. unsigned long bank_offset;
  35. u32 bit_offset;
  36. bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset);
  37. bit_offset = GPIO_BIT_OFFSET(offset);
  38. return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
  39. }
  40. static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  41. {
  42. struct xgene_gpio *chip = gpiochip_get_data(gc);
  43. unsigned long bank_offset;
  44. u32 setval, bit_offset;
  45. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  46. bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK;
  47. setval = ioread32(chip->base + bank_offset);
  48. if (val)
  49. setval |= BIT(bit_offset);
  50. else
  51. setval &= ~BIT(bit_offset);
  52. iowrite32(setval, chip->base + bank_offset);
  53. }
  54. static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  55. {
  56. struct xgene_gpio *chip = gpiochip_get_data(gc);
  57. unsigned long flags;
  58. spin_lock_irqsave(&chip->lock, flags);
  59. __xgene_gpio_set(gc, offset, val);
  60. spin_unlock_irqrestore(&chip->lock, flags);
  61. }
  62. static int xgene_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  63. {
  64. struct xgene_gpio *chip = gpiochip_get_data(gc);
  65. unsigned long bank_offset, bit_offset;
  66. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  67. bit_offset = GPIO_BIT_OFFSET(offset);
  68. if (ioread32(chip->base + bank_offset) & BIT(bit_offset))
  69. return GPIO_LINE_DIRECTION_IN;
  70. return GPIO_LINE_DIRECTION_OUT;
  71. }
  72. static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
  73. {
  74. struct xgene_gpio *chip = gpiochip_get_data(gc);
  75. unsigned long flags, bank_offset;
  76. u32 dirval, bit_offset;
  77. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  78. bit_offset = GPIO_BIT_OFFSET(offset);
  79. spin_lock_irqsave(&chip->lock, flags);
  80. dirval = ioread32(chip->base + bank_offset);
  81. dirval |= BIT(bit_offset);
  82. iowrite32(dirval, chip->base + bank_offset);
  83. spin_unlock_irqrestore(&chip->lock, flags);
  84. return 0;
  85. }
  86. static int xgene_gpio_dir_out(struct gpio_chip *gc,
  87. unsigned int offset, int val)
  88. {
  89. struct xgene_gpio *chip = gpiochip_get_data(gc);
  90. unsigned long flags, bank_offset;
  91. u32 dirval, bit_offset;
  92. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  93. bit_offset = GPIO_BIT_OFFSET(offset);
  94. spin_lock_irqsave(&chip->lock, flags);
  95. dirval = ioread32(chip->base + bank_offset);
  96. dirval &= ~BIT(bit_offset);
  97. iowrite32(dirval, chip->base + bank_offset);
  98. __xgene_gpio_set(gc, offset, val);
  99. spin_unlock_irqrestore(&chip->lock, flags);
  100. return 0;
  101. }
  102. static __maybe_unused int xgene_gpio_suspend(struct device *dev)
  103. {
  104. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  105. unsigned long bank_offset;
  106. unsigned int bank;
  107. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  108. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  109. gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
  110. }
  111. return 0;
  112. }
  113. static __maybe_unused int xgene_gpio_resume(struct device *dev)
  114. {
  115. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  116. unsigned long bank_offset;
  117. unsigned int bank;
  118. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  119. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  120. iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
  121. }
  122. return 0;
  123. }
  124. static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
  125. static int xgene_gpio_probe(struct platform_device *pdev)
  126. {
  127. struct xgene_gpio *gpio;
  128. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  129. if (!gpio)
  130. return -ENOMEM;
  131. gpio->base = devm_platform_ioremap_resource(pdev, 0);
  132. if (IS_ERR(gpio->base))
  133. return PTR_ERR(gpio->base);
  134. gpio->chip.ngpio = XGENE_MAX_GPIOS;
  135. spin_lock_init(&gpio->lock);
  136. gpio->chip.parent = &pdev->dev;
  137. gpio->chip.get_direction = xgene_gpio_get_direction;
  138. gpio->chip.direction_input = xgene_gpio_dir_in;
  139. gpio->chip.direction_output = xgene_gpio_dir_out;
  140. gpio->chip.get = xgene_gpio_get;
  141. gpio->chip.set = xgene_gpio_set;
  142. gpio->chip.label = dev_name(&pdev->dev);
  143. gpio->chip.base = -1;
  144. platform_set_drvdata(pdev, gpio);
  145. return devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  146. }
  147. static const struct of_device_id xgene_gpio_of_match[] = {
  148. { .compatible = "apm,xgene-gpio", },
  149. {},
  150. };
  151. #ifdef CONFIG_ACPI
  152. static const struct acpi_device_id xgene_gpio_acpi_match[] = {
  153. { "APMC0D14", 0 },
  154. { },
  155. };
  156. #endif
  157. static struct platform_driver xgene_gpio_driver = {
  158. .driver = {
  159. .name = "xgene-gpio",
  160. .of_match_table = xgene_gpio_of_match,
  161. .acpi_match_table = ACPI_PTR(xgene_gpio_acpi_match),
  162. .pm = &xgene_gpio_pm,
  163. },
  164. .probe = xgene_gpio_probe,
  165. };
  166. builtin_platform_driver(xgene_gpio_driver);