gpio-xgene.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 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_dir_in(struct gpio_chip *gc, unsigned int offset)
  76. {
  77. struct xgene_gpio *chip = gpiochip_get_data(gc);
  78. unsigned long flags, bank_offset;
  79. u32 dirval, bit_offset;
  80. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  81. bit_offset = GPIO_BIT_OFFSET(offset);
  82. spin_lock_irqsave(&chip->lock, flags);
  83. dirval = ioread32(chip->base + bank_offset);
  84. dirval |= BIT(bit_offset);
  85. iowrite32(dirval, chip->base + bank_offset);
  86. spin_unlock_irqrestore(&chip->lock, flags);
  87. return 0;
  88. }
  89. static int xgene_gpio_dir_out(struct gpio_chip *gc,
  90. unsigned int offset, int val)
  91. {
  92. struct xgene_gpio *chip = gpiochip_get_data(gc);
  93. unsigned long flags, bank_offset;
  94. u32 dirval, bit_offset;
  95. bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
  96. bit_offset = GPIO_BIT_OFFSET(offset);
  97. spin_lock_irqsave(&chip->lock, flags);
  98. dirval = ioread32(chip->base + bank_offset);
  99. dirval &= ~BIT(bit_offset);
  100. iowrite32(dirval, chip->base + bank_offset);
  101. __xgene_gpio_set(gc, offset, val);
  102. spin_unlock_irqrestore(&chip->lock, flags);
  103. return 0;
  104. }
  105. #ifdef CONFIG_PM
  106. static int xgene_gpio_suspend(struct device *dev)
  107. {
  108. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  109. unsigned long bank_offset;
  110. unsigned int bank;
  111. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  112. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  113. gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
  114. }
  115. return 0;
  116. }
  117. static int xgene_gpio_resume(struct device *dev)
  118. {
  119. struct xgene_gpio *gpio = dev_get_drvdata(dev);
  120. unsigned long bank_offset;
  121. unsigned int bank;
  122. for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
  123. bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
  124. iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
  125. }
  126. return 0;
  127. }
  128. static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
  129. #define XGENE_GPIO_PM_OPS (&xgene_gpio_pm)
  130. #else
  131. #define XGENE_GPIO_PM_OPS NULL
  132. #endif
  133. static int xgene_gpio_probe(struct platform_device *pdev)
  134. {
  135. struct resource *res;
  136. struct xgene_gpio *gpio;
  137. int err = 0;
  138. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  139. if (!gpio) {
  140. err = -ENOMEM;
  141. goto err;
  142. }
  143. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  144. if (!res) {
  145. err = -EINVAL;
  146. goto err;
  147. }
  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.parent = &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 = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  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 const struct of_device_id xgene_gpio_of_match[] = {
  177. { .compatible = "apm,xgene-gpio", },
  178. {},
  179. };
  180. MODULE_DEVICE_TABLE(of, xgene_gpio_of_match);
  181. static struct platform_driver xgene_gpio_driver = {
  182. .driver = {
  183. .name = "xgene-gpio",
  184. .of_match_table = xgene_gpio_of_match,
  185. .pm = XGENE_GPIO_PM_OPS,
  186. },
  187. .probe = xgene_gpio_probe,
  188. };
  189. module_platform_driver(xgene_gpio_driver);
  190. MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
  191. MODULE_DESCRIPTION("APM X-Gene GPIO driver");
  192. MODULE_LICENSE("GPL");