gpio-xgene.c 5.8 KB

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