gpio-xgene.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifdef CONFIG_PM
  41. u32 set_dr_val[XGENE_MAX_GPIO_BANKS];
  42. #endif
  43. };
  44. static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset)
  45. {
  46. struct xgene_gpio *chip = gpiochip_get_data(gc);
  47. unsigned long bank_offset;
  48. u32 bit_offset;
  49. bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset);
  50. bit_offset = GPIO_BIT_OFFSET(offset);
  51. return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
  52. }
  53. static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  54. {
  55. struct xgene_gpio *chip = gpiochip_get_data(gc);
  56. unsigned long bank_offset;
  57. u32 setval, bit_offset;
  58. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  59. bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK;
  60. setval = ioread32(chip->base + bank_offset);
  61. if (val)
  62. setval |= BIT(bit_offset);
  63. else
  64. setval &= ~BIT(bit_offset);
  65. iowrite32(setval, chip->base + bank_offset);
  66. }
  67. static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
  68. {
  69. struct xgene_gpio *chip = gpiochip_get_data(gc);
  70. unsigned long flags;
  71. spin_lock_irqsave(&chip->lock, flags);
  72. __xgene_gpio_set(gc, offset, val);
  73. spin_unlock_irqrestore(&chip->lock, flags);
  74. }
  75. static int xgene_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  76. {
  77. struct xgene_gpio *chip = gpiochip_get_data(gc);
  78. unsigned long bank_offset, bit_offset;
  79. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  80. bit_offset = GPIO_BIT_OFFSET(offset);
  81. return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
  82. }
  83. static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
  84. {
  85. struct xgene_gpio *chip = gpiochip_get_data(gc);
  86. unsigned long flags, bank_offset;
  87. u32 dirval, bit_offset;
  88. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  89. bit_offset = GPIO_BIT_OFFSET(offset);
  90. spin_lock_irqsave(&chip->lock, flags);
  91. dirval = ioread32(chip->base + bank_offset);
  92. dirval |= BIT(bit_offset);
  93. iowrite32(dirval, chip->base + bank_offset);
  94. spin_unlock_irqrestore(&chip->lock, flags);
  95. return 0;
  96. }
  97. static int xgene_gpio_dir_out(struct gpio_chip *gc,
  98. unsigned int offset, int val)
  99. {
  100. struct xgene_gpio *chip = gpiochip_get_data(gc);
  101. unsigned long flags, bank_offset;
  102. u32 dirval, bit_offset;
  103. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  104. bit_offset = GPIO_BIT_OFFSET(offset);
  105. spin_lock_irqsave(&chip->lock, flags);
  106. dirval = ioread32(chip->base + bank_offset);
  107. dirval &= ~BIT(bit_offset);
  108. iowrite32(dirval, chip->base + bank_offset);
  109. __xgene_gpio_set(gc, offset, val);
  110. spin_unlock_irqrestore(&chip->lock, flags);
  111. return 0;
  112. }
  113. #ifdef CONFIG_PM
  114. static int xgene_gpio_suspend(struct device *dev)
  115. {
  116. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  117. unsigned long bank_offset;
  118. unsigned int bank;
  119. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  120. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  121. gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
  122. }
  123. return 0;
  124. }
  125. static int xgene_gpio_resume(struct device *dev)
  126. {
  127. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  128. unsigned long bank_offset;
  129. unsigned int bank;
  130. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  131. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  132. iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
  133. }
  134. return 0;
  135. }
  136. static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
  137. #define XGENE_GPIO_PM_OPS (&xgene_gpio_pm)
  138. #else
  139. #define XGENE_GPIO_PM_OPS NULL
  140. #endif
  141. static int xgene_gpio_probe(struct platform_device *pdev)
  142. {
  143. struct resource *res;
  144. struct xgene_gpio *gpio;
  145. int err = 0;
  146. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  147. if (!gpio) {
  148. err = -ENOMEM;
  149. goto err;
  150. }
  151. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. if (!res) {
  153. err = -EINVAL;
  154. goto err;
  155. }
  156. gpio->base = devm_ioremap_nocache(&pdev->dev, res->start,
  157. resource_size(res));
  158. if (!gpio->base) {
  159. err = -ENOMEM;
  160. goto err;
  161. }
  162. gpio->chip.ngpio = XGENE_MAX_GPIOS;
  163. spin_lock_init(&gpio->lock);
  164. gpio->chip.parent = &pdev->dev;
  165. gpio->chip.get_direction = xgene_gpio_get_direction;
  166. gpio->chip.direction_input = xgene_gpio_dir_in;
  167. gpio->chip.direction_output = xgene_gpio_dir_out;
  168. gpio->chip.get = xgene_gpio_get;
  169. gpio->chip.set = xgene_gpio_set;
  170. gpio->chip.label = dev_name(&pdev->dev);
  171. gpio->chip.base = -1;
  172. platform_set_drvdata(pdev, gpio);
  173. err = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  174. if (err) {
  175. dev_err(&pdev->dev,
  176. "failed to register gpiochip.\n");
  177. goto err;
  178. }
  179. dev_info(&pdev->dev, "X-Gene GPIO driver registered.\n");
  180. return 0;
  181. err:
  182. dev_err(&pdev->dev, "X-Gene GPIO driver registration failed.\n");
  183. return err;
  184. }
  185. static const struct of_device_id xgene_gpio_of_match[] = {
  186. { .compatible = "apm,xgene-gpio", },
  187. {},
  188. };
  189. #ifdef CONFIG_ACPI
  190. static const struct acpi_device_id xgene_gpio_acpi_match[] = {
  191. { "APMC0D14", 0 },
  192. { },
  193. };
  194. #endif
  195. static struct platform_driver xgene_gpio_driver = {
  196. .driver = {
  197. .name = "xgene-gpio",
  198. .of_match_table = xgene_gpio_of_match,
  199. .acpi_match_table = ACPI_PTR(xgene_gpio_acpi_match),
  200. .pm = XGENE_GPIO_PM_OPS,
  201. },
  202. .probe = xgene_gpio_probe,
  203. };
  204. builtin_platform_driver(xgene_gpio_driver);