gpio-ath79.c 8.5 KB

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