gpio-xgene.c 6.3 KB

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