gpio-ath79.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/gpio/driver.h>
  15. #include <linux/platform_data/gpio-ath79.h>
  16. #include <linux/of_device.h>
  17. #include <asm/mach-ath79/ar71xx_regs.h>
  18. struct ath79_gpio_ctrl {
  19. struct gpio_chip chip;
  20. void __iomem *base;
  21. spinlock_t lock;
  22. };
  23. #define to_ath79_gpio_ctrl(c) container_of(c, struct ath79_gpio_ctrl, chip)
  24. static void ath79_gpio_set_value(struct gpio_chip *chip,
  25. unsigned gpio, int value)
  26. {
  27. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  28. if (value)
  29. __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_SET);
  30. else
  31. __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_CLEAR);
  32. }
  33. static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  34. {
  35. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  36. return (__raw_readl(ctrl->base + AR71XX_GPIO_REG_IN) >> gpio) & 1;
  37. }
  38. static int ath79_gpio_direction_input(struct gpio_chip *chip,
  39. unsigned offset)
  40. {
  41. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  42. unsigned long flags;
  43. spin_lock_irqsave(&ctrl->lock, flags);
  44. __raw_writel(
  45. __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset),
  46. ctrl->base + AR71XX_GPIO_REG_OE);
  47. spin_unlock_irqrestore(&ctrl->lock, flags);
  48. return 0;
  49. }
  50. static int ath79_gpio_direction_output(struct gpio_chip *chip,
  51. unsigned offset, int value)
  52. {
  53. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  54. unsigned long flags;
  55. spin_lock_irqsave(&ctrl->lock, flags);
  56. if (value)
  57. __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
  58. else
  59. __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
  60. __raw_writel(
  61. __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
  62. ctrl->base + AR71XX_GPIO_REG_OE);
  63. spin_unlock_irqrestore(&ctrl->lock, flags);
  64. return 0;
  65. }
  66. static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  67. {
  68. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  69. unsigned long flags;
  70. spin_lock_irqsave(&ctrl->lock, flags);
  71. __raw_writel(
  72. __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
  73. ctrl->base + AR71XX_GPIO_REG_OE);
  74. spin_unlock_irqrestore(&ctrl->lock, flags);
  75. return 0;
  76. }
  77. static int ar934x_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  78. int value)
  79. {
  80. struct ath79_gpio_ctrl *ctrl = to_ath79_gpio_ctrl(chip);
  81. unsigned long flags;
  82. spin_lock_irqsave(&ctrl->lock, flags);
  83. if (value)
  84. __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
  85. else
  86. __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
  87. __raw_writel(
  88. __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset),
  89. ctrl->base + AR71XX_GPIO_REG_OE);
  90. spin_unlock_irqrestore(&ctrl->lock, flags);
  91. return 0;
  92. }
  93. static const struct gpio_chip ath79_gpio_chip = {
  94. .label = "ath79",
  95. .get = ath79_gpio_get_value,
  96. .set = ath79_gpio_set_value,
  97. .direction_input = ath79_gpio_direction_input,
  98. .direction_output = ath79_gpio_direction_output,
  99. .base = 0,
  100. };
  101. static const struct of_device_id ath79_gpio_of_match[] = {
  102. { .compatible = "qca,ar7100-gpio" },
  103. { .compatible = "qca,ar9340-gpio" },
  104. {},
  105. };
  106. static int ath79_gpio_probe(struct platform_device *pdev)
  107. {
  108. struct ath79_gpio_platform_data *pdata = pdev->dev.platform_data;
  109. struct device_node *np = pdev->dev.of_node;
  110. struct ath79_gpio_ctrl *ctrl;
  111. struct resource *res;
  112. u32 ath79_gpio_count;
  113. bool oe_inverted;
  114. int err;
  115. ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
  116. if (!ctrl)
  117. return -ENOMEM;
  118. if (np) {
  119. err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
  120. if (err) {
  121. dev_err(&pdev->dev, "ngpios property is not valid\n");
  122. return err;
  123. }
  124. if (ath79_gpio_count >= 32) {
  125. dev_err(&pdev->dev, "ngpios must be less than 32\n");
  126. return -EINVAL;
  127. }
  128. oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
  129. } else if (pdata) {
  130. ath79_gpio_count = pdata->ngpios;
  131. oe_inverted = pdata->oe_inverted;
  132. } else {
  133. dev_err(&pdev->dev, "No DT node or platform data found\n");
  134. return -EINVAL;
  135. }
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. ctrl->base = devm_ioremap_nocache(
  138. &pdev->dev, res->start, resource_size(res));
  139. if (!ctrl->base)
  140. return -ENOMEM;
  141. spin_lock_init(&ctrl->lock);
  142. memcpy(&ctrl->chip, &ath79_gpio_chip, sizeof(ctrl->chip));
  143. ctrl->chip.dev = &pdev->dev;
  144. ctrl->chip.ngpio = ath79_gpio_count;
  145. if (oe_inverted) {
  146. ctrl->chip.direction_input = ar934x_gpio_direction_input;
  147. ctrl->chip.direction_output = ar934x_gpio_direction_output;
  148. }
  149. err = gpiochip_add(&ctrl->chip);
  150. if (err) {
  151. dev_err(&pdev->dev,
  152. "cannot add AR71xx GPIO chip, error=%d", err);
  153. return err;
  154. }
  155. return 0;
  156. }
  157. static struct platform_driver ath79_gpio_driver = {
  158. .driver = {
  159. .name = "ath79-gpio",
  160. .of_match_table = ath79_gpio_of_match,
  161. },
  162. .probe = ath79_gpio_probe,
  163. };
  164. module_platform_driver(ath79_gpio_driver);
  165. MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
  166. MODULE_LICENSE("GPL v2");