gpio-ath79.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Atheros AR71XX/AR724X/AR913X GPIO API support
  3. *
  4. * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
  5. * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
  6. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  7. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/gpio/driver.h>
  14. #include <linux/platform_data/gpio-ath79.h>
  15. #include <linux/of_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #define AR71XX_GPIO_REG_OE 0x00
  19. #define AR71XX_GPIO_REG_IN 0x04
  20. #define AR71XX_GPIO_REG_SET 0x0c
  21. #define AR71XX_GPIO_REG_CLEAR 0x10
  22. #define AR71XX_GPIO_REG_INT_ENABLE 0x14
  23. #define AR71XX_GPIO_REG_INT_TYPE 0x18
  24. #define AR71XX_GPIO_REG_INT_POLARITY 0x1c
  25. #define AR71XX_GPIO_REG_INT_PENDING 0x20
  26. #define AR71XX_GPIO_REG_INT_MASK 0x24
  27. struct ath79_gpio_ctrl {
  28. struct gpio_chip gc;
  29. void __iomem *base;
  30. spinlock_t lock;
  31. unsigned long both_edges;
  32. };
  33. static struct ath79_gpio_ctrl *irq_data_to_ath79_gpio(struct irq_data *data)
  34. {
  35. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  36. return container_of(gc, struct ath79_gpio_ctrl, gc);
  37. }
  38. static u32 ath79_gpio_read(struct ath79_gpio_ctrl *ctrl, unsigned reg)
  39. {
  40. return readl(ctrl->base + reg);
  41. }
  42. static void ath79_gpio_write(struct ath79_gpio_ctrl *ctrl,
  43. unsigned reg, u32 val)
  44. {
  45. return writel(val, ctrl->base + reg);
  46. }
  47. static bool ath79_gpio_update_bits(
  48. struct ath79_gpio_ctrl *ctrl, unsigned reg, u32 mask, u32 bits)
  49. {
  50. u32 old_val, new_val;
  51. old_val = ath79_gpio_read(ctrl, reg);
  52. new_val = (old_val & ~mask) | (bits & mask);
  53. if (new_val != old_val)
  54. ath79_gpio_write(ctrl, reg, new_val);
  55. return new_val != old_val;
  56. }
  57. static void ath79_gpio_irq_unmask(struct irq_data *data)
  58. {
  59. struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  60. u32 mask = BIT(irqd_to_hwirq(data));
  61. unsigned long flags;
  62. spin_lock_irqsave(&ctrl->lock, flags);
  63. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
  64. spin_unlock_irqrestore(&ctrl->lock, flags);
  65. }
  66. static void ath79_gpio_irq_mask(struct irq_data *data)
  67. {
  68. struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  69. u32 mask = BIT(irqd_to_hwirq(data));
  70. unsigned long flags;
  71. spin_lock_irqsave(&ctrl->lock, flags);
  72. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
  73. spin_unlock_irqrestore(&ctrl->lock, flags);
  74. }
  75. static void ath79_gpio_irq_enable(struct irq_data *data)
  76. {
  77. struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  78. u32 mask = BIT(irqd_to_hwirq(data));
  79. unsigned long flags;
  80. spin_lock_irqsave(&ctrl->lock, flags);
  81. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
  82. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
  83. spin_unlock_irqrestore(&ctrl->lock, flags);
  84. }
  85. static void ath79_gpio_irq_disable(struct irq_data *data)
  86. {
  87. struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  88. u32 mask = BIT(irqd_to_hwirq(data));
  89. unsigned long flags;
  90. spin_lock_irqsave(&ctrl->lock, flags);
  91. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
  92. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
  93. spin_unlock_irqrestore(&ctrl->lock, flags);
  94. }
  95. static int ath79_gpio_irq_set_type(struct irq_data *data,
  96. unsigned int flow_type)
  97. {
  98. struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data);
  99. u32 mask = BIT(irqd_to_hwirq(data));
  100. u32 type = 0, polarity = 0;
  101. unsigned long flags;
  102. bool disabled;
  103. switch (flow_type) {
  104. case IRQ_TYPE_EDGE_RISING:
  105. polarity |= mask;
  106. case IRQ_TYPE_EDGE_FALLING:
  107. case IRQ_TYPE_EDGE_BOTH:
  108. break;
  109. case IRQ_TYPE_LEVEL_HIGH:
  110. polarity |= mask;
  111. case IRQ_TYPE_LEVEL_LOW:
  112. type |= mask;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. spin_lock_irqsave(&ctrl->lock, flags);
  118. if (flow_type == IRQ_TYPE_EDGE_BOTH) {
  119. ctrl->both_edges |= mask;
  120. polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  121. } else {
  122. ctrl->both_edges &= ~mask;
  123. }
  124. /* As the IRQ configuration can't be loaded atomically we
  125. * have to disable the interrupt while the configuration state
  126. * is invalid.
  127. */
  128. disabled = ath79_gpio_update_bits(
  129. ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
  130. ath79_gpio_update_bits(
  131. ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type);
  132. ath79_gpio_update_bits(
  133. ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity);
  134. if (disabled)
  135. ath79_gpio_update_bits(
  136. ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
  137. spin_unlock_irqrestore(&ctrl->lock, flags);
  138. return 0;
  139. }
  140. static struct irq_chip ath79_gpio_irqchip = {
  141. .name = "gpio-ath79",
  142. .irq_enable = ath79_gpio_irq_enable,
  143. .irq_disable = ath79_gpio_irq_disable,
  144. .irq_mask = ath79_gpio_irq_mask,
  145. .irq_unmask = ath79_gpio_irq_unmask,
  146. .irq_set_type = ath79_gpio_irq_set_type,
  147. };
  148. static void ath79_gpio_irq_handler(struct irq_desc *desc)
  149. {
  150. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  151. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  152. struct ath79_gpio_ctrl *ctrl =
  153. container_of(gc, struct ath79_gpio_ctrl, gc);
  154. unsigned long flags, pending;
  155. u32 both_edges, state;
  156. int irq;
  157. chained_irq_enter(irqchip, desc);
  158. spin_lock_irqsave(&ctrl->lock, flags);
  159. pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING);
  160. /* Update the polarity of the both edges irqs */
  161. both_edges = ctrl->both_edges & pending;
  162. if (both_edges) {
  163. state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  164. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY,
  165. both_edges, ~state);
  166. }
  167. spin_unlock_irqrestore(&ctrl->lock, flags);
  168. if (pending) {
  169. for_each_set_bit(irq, &pending, gc->ngpio)
  170. generic_handle_irq(
  171. irq_linear_revmap(gc->irqdomain, irq));
  172. }
  173. chained_irq_exit(irqchip, desc);
  174. }
  175. static const struct of_device_id ath79_gpio_of_match[] = {
  176. { .compatible = "qca,ar7100-gpio" },
  177. { .compatible = "qca,ar9340-gpio" },
  178. {},
  179. };
  180. static int ath79_gpio_probe(struct platform_device *pdev)
  181. {
  182. struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  183. struct device_node *np = pdev->dev.of_node;
  184. struct ath79_gpio_ctrl *ctrl;
  185. struct resource *res;
  186. u32 ath79_gpio_count;
  187. bool oe_inverted;
  188. int err;
  189. ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
  190. if (!ctrl)
  191. return -ENOMEM;
  192. platform_set_drvdata(pdev, ctrl);
  193. if (np) {
  194. err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
  195. if (err) {
  196. dev_err(&pdev->dev, "ngpios property is not valid\n");
  197. return err;
  198. }
  199. oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
  200. } else if (pdata) {
  201. ath79_gpio_count = pdata->ngpios;
  202. oe_inverted = pdata->oe_inverted;
  203. } else {
  204. dev_err(&pdev->dev, "No DT node or platform data found\n");
  205. return -EINVAL;
  206. }
  207. if (ath79_gpio_count >= 32) {
  208. dev_err(&pdev->dev, "ngpios must be less than 32\n");
  209. return -EINVAL;
  210. }
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. ctrl->base = devm_ioremap_nocache(
  213. &pdev->dev, res->start, resource_size(res));
  214. if (!ctrl->base)
  215. return -ENOMEM;
  216. spin_lock_init(&ctrl->lock);
  217. err = bgpio_init(&ctrl->gc, &pdev->dev, 4,
  218. ctrl->base + AR71XX_GPIO_REG_IN,
  219. ctrl->base + AR71XX_GPIO_REG_SET,
  220. ctrl->base + AR71XX_GPIO_REG_CLEAR,
  221. oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE,
  222. oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL,
  223. 0);
  224. if (err) {
  225. dev_err(&pdev->dev, "bgpio_init failed\n");
  226. return err;
  227. }
  228. /* Use base 0 to stay compatible with legacy platforms */
  229. ctrl->gc.base = 0;
  230. err = gpiochip_add_data(&ctrl->gc, ctrl);
  231. if (err) {
  232. dev_err(&pdev->dev,
  233. "cannot add AR71xx GPIO chip, error=%d", err);
  234. return err;
  235. }
  236. if (np && !of_property_read_bool(np, "interrupt-controller"))
  237. return 0;
  238. err = gpiochip_irqchip_add(&ctrl->gc, &ath79_gpio_irqchip, 0,
  239. handle_simple_irq, IRQ_TYPE_NONE);
  240. if (err) {
  241. dev_err(&pdev->dev, "failed to add gpiochip_irqchip\n");
  242. goto gpiochip_remove;
  243. }
  244. gpiochip_set_chained_irqchip(&ctrl->gc, &ath79_gpio_irqchip,
  245. platform_get_irq(pdev, 0),
  246. ath79_gpio_irq_handler);
  247. return 0;
  248. gpiochip_remove:
  249. gpiochip_remove(&ctrl->gc);
  250. return err;
  251. }
  252. static int ath79_gpio_remove(struct platform_device *pdev)
  253. {
  254. struct ath79_gpio_ctrl *ctrl = platform_get_drvdata(pdev);
  255. gpiochip_remove(&ctrl->gc);
  256. return 0;
  257. }
  258. static struct platform_driver ath79_gpio_driver = {
  259. .driver = {
  260. .name = "ath79-gpio",
  261. .of_match_table = ath79_gpio_of_match,
  262. },
  263. .probe = ath79_gpio_probe,
  264. .remove = ath79_gpio_remove,
  265. };
  266. module_platform_driver(ath79_gpio_driver);