gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Atheros AR71XX/AR724X/AR913X GPIO API support
  3. *
  4. * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
  5. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  6. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  7. *
  8. * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/io.h>
  20. #include <linux/ioport.h>
  21. #include <linux/gpio.h>
  22. #include <linux/platform_data/gpio-ath79.h>
  23. #include <linux/of_device.h>
  24. #include <asm/mach-ath79/ar71xx_regs.h>
  25. #include <asm/mach-ath79/ath79.h>
  26. #include "common.h"
  27. static void __iomem *ath79_gpio_base;
  28. static u32 ath79_gpio_count;
  29. static DEFINE_SPINLOCK(ath79_gpio_lock);
  30. static void __ath79_gpio_set_value(unsigned gpio, int value)
  31. {
  32. void __iomem *base = ath79_gpio_base;
  33. if (value)
  34. __raw_writel(1 << gpio, base + AR71XX_GPIO_REG_SET);
  35. else
  36. __raw_writel(1 << gpio, base + AR71XX_GPIO_REG_CLEAR);
  37. }
  38. static int __ath79_gpio_get_value(unsigned gpio)
  39. {
  40. return (__raw_readl(ath79_gpio_base + AR71XX_GPIO_REG_IN) >> gpio) & 1;
  41. }
  42. static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned offset)
  43. {
  44. return __ath79_gpio_get_value(offset);
  45. }
  46. static void ath79_gpio_set_value(struct gpio_chip *chip,
  47. unsigned offset, int value)
  48. {
  49. __ath79_gpio_set_value(offset, value);
  50. }
  51. static int ath79_gpio_direction_input(struct gpio_chip *chip,
  52. unsigned offset)
  53. {
  54. void __iomem *base = ath79_gpio_base;
  55. unsigned long flags;
  56. spin_lock_irqsave(&ath79_gpio_lock, flags);
  57. __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) & ~(1 << offset),
  58. base + AR71XX_GPIO_REG_OE);
  59. spin_unlock_irqrestore(&ath79_gpio_lock, flags);
  60. return 0;
  61. }
  62. static int ath79_gpio_direction_output(struct gpio_chip *chip,
  63. unsigned offset, int value)
  64. {
  65. void __iomem *base = ath79_gpio_base;
  66. unsigned long flags;
  67. spin_lock_irqsave(&ath79_gpio_lock, flags);
  68. if (value)
  69. __raw_writel(1 << offset, base + AR71XX_GPIO_REG_SET);
  70. else
  71. __raw_writel(1 << offset, base + AR71XX_GPIO_REG_CLEAR);
  72. __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) | (1 << offset),
  73. base + AR71XX_GPIO_REG_OE);
  74. spin_unlock_irqrestore(&ath79_gpio_lock, flags);
  75. return 0;
  76. }
  77. static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  78. {
  79. void __iomem *base = ath79_gpio_base;
  80. unsigned long flags;
  81. spin_lock_irqsave(&ath79_gpio_lock, flags);
  82. __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) | (1 << offset),
  83. base + AR71XX_GPIO_REG_OE);
  84. spin_unlock_irqrestore(&ath79_gpio_lock, flags);
  85. return 0;
  86. }
  87. static int ar934x_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  88. int value)
  89. {
  90. void __iomem *base = ath79_gpio_base;
  91. unsigned long flags;
  92. spin_lock_irqsave(&ath79_gpio_lock, flags);
  93. if (value)
  94. __raw_writel(1 << offset, base + AR71XX_GPIO_REG_SET);
  95. else
  96. __raw_writel(1 << offset, base + AR71XX_GPIO_REG_CLEAR);
  97. __raw_writel(__raw_readl(base + AR71XX_GPIO_REG_OE) & ~(1 << offset),
  98. base + AR71XX_GPIO_REG_OE);
  99. spin_unlock_irqrestore(&ath79_gpio_lock, flags);
  100. return 0;
  101. }
  102. static struct gpio_chip ath79_gpio_chip = {
  103. .label = "ath79",
  104. .get = ath79_gpio_get_value,
  105. .set = ath79_gpio_set_value,
  106. .direction_input = ath79_gpio_direction_input,
  107. .direction_output = ath79_gpio_direction_output,
  108. .base = 0,
  109. };
  110. static void __iomem *ath79_gpio_get_function_reg(void)
  111. {
  112. u32 reg = 0;
  113. if (soc_is_ar71xx() ||
  114. soc_is_ar724x() ||
  115. soc_is_ar913x() ||
  116. soc_is_ar933x())
  117. reg = AR71XX_GPIO_REG_FUNC;
  118. else if (soc_is_ar934x())
  119. reg = AR934X_GPIO_REG_FUNC;
  120. else
  121. BUG();
  122. return ath79_gpio_base + reg;
  123. }
  124. void ath79_gpio_function_setup(u32 set, u32 clear)
  125. {
  126. void __iomem *reg = ath79_gpio_get_function_reg();
  127. unsigned long flags;
  128. spin_lock_irqsave(&ath79_gpio_lock, flags);
  129. __raw_writel((__raw_readl(reg) & ~clear) | set, reg);
  130. /* flush write */
  131. __raw_readl(reg);
  132. spin_unlock_irqrestore(&ath79_gpio_lock, flags);
  133. }
  134. void ath79_gpio_function_enable(u32 mask)
  135. {
  136. ath79_gpio_function_setup(mask, 0);
  137. }
  138. void ath79_gpio_function_disable(u32 mask)
  139. {
  140. ath79_gpio_function_setup(0, mask);
  141. }
  142. static const struct of_device_id ath79_gpio_of_match[] = {
  143. { .compatible = "qca,ar7100-gpio" },
  144. { .compatible = "qca,ar9340-gpio" },
  145. {},
  146. };
  147. static int ath79_gpio_probe(struct platform_device *pdev)
  148. {
  149. struct ath79_gpio_platform_data *pdata = pdev->dev.platform_data;
  150. struct device_node *np = pdev->dev.of_node;
  151. struct resource *res;
  152. bool oe_inverted;
  153. int err;
  154. if (np) {
  155. err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
  156. if (err) {
  157. dev_err(&pdev->dev, "ngpios property is not valid\n");
  158. return err;
  159. }
  160. if (ath79_gpio_count >= 32) {
  161. dev_err(&pdev->dev, "ngpios must be less than 32\n");
  162. return -EINVAL;
  163. }
  164. oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
  165. } else if (pdata) {
  166. ath79_gpio_count = pdata->ngpios;
  167. oe_inverted = pdata->oe_inverted;
  168. } else {
  169. dev_err(&pdev->dev, "No DT node or platform data found\n");
  170. return -EINVAL;
  171. }
  172. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. ath79_gpio_base = devm_ioremap_nocache(
  174. &pdev->dev, res->start, resource_size(res));
  175. if (!ath79_gpio_base)
  176. return -ENOMEM;
  177. ath79_gpio_chip.dev = &pdev->dev;
  178. ath79_gpio_chip.ngpio = ath79_gpio_count;
  179. if (oe_inverted) {
  180. ath79_gpio_chip.direction_input = ar934x_gpio_direction_input;
  181. ath79_gpio_chip.direction_output = ar934x_gpio_direction_output;
  182. }
  183. err = gpiochip_add(&ath79_gpio_chip);
  184. if (err) {
  185. dev_err(&pdev->dev,
  186. "cannot add AR71xx GPIO chip, error=%d", err);
  187. return err;
  188. }
  189. return 0;
  190. }
  191. static struct platform_driver ath79_gpio_driver = {
  192. .driver = {
  193. .name = "ath79-gpio",
  194. .of_match_table = ath79_gpio_of_match,
  195. },
  196. .probe = ath79_gpio_probe,
  197. };
  198. module_platform_driver(ath79_gpio_driver);
  199. int gpio_get_value(unsigned gpio)
  200. {
  201. if (gpio < ath79_gpio_count)
  202. return __ath79_gpio_get_value(gpio);
  203. return __gpio_get_value(gpio);
  204. }
  205. EXPORT_SYMBOL(gpio_get_value);
  206. void gpio_set_value(unsigned gpio, int value)
  207. {
  208. if (gpio < ath79_gpio_count)
  209. __ath79_gpio_set_value(gpio, value);
  210. else
  211. __gpio_set_value(gpio, value);
  212. }
  213. EXPORT_SYMBOL(gpio_set_value);
  214. int gpio_to_irq(unsigned gpio)
  215. {
  216. /* FIXME */
  217. return -EINVAL;
  218. }
  219. EXPORT_SYMBOL(gpio_to_irq);
  220. int irq_to_gpio(unsigned irq)
  221. {
  222. /* FIXME */
  223. return -EINVAL;
  224. }
  225. EXPORT_SYMBOL(irq_to_gpio);