gpio-ath79.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. raw_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. 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. raw_spin_lock_irqsave(&ctrl->lock, flags);
  64. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask);
  65. raw_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. raw_spin_lock_irqsave(&ctrl->lock, flags);
  73. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0);
  74. raw_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. raw_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. raw_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. raw_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. raw_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. /* fall through */
  113. case IRQ_TYPE_LEVEL_LOW:
  114. type |= mask;
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. raw_spin_lock_irqsave(&ctrl->lock, flags);
  120. if (flow_type == IRQ_TYPE_EDGE_BOTH) {
  121. ctrl->both_edges |= mask;
  122. polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  123. } else {
  124. ctrl->both_edges &= ~mask;
  125. }
  126. /* As the IRQ configuration can't be loaded atomically we
  127. * have to disable the interrupt while the configuration state
  128. * is invalid.
  129. */
  130. disabled = ath79_gpio_update_bits(
  131. ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0);
  132. ath79_gpio_update_bits(
  133. ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type);
  134. ath79_gpio_update_bits(
  135. ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity);
  136. if (disabled)
  137. ath79_gpio_update_bits(
  138. ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask);
  139. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  140. return 0;
  141. }
  142. static struct irq_chip ath79_gpio_irqchip = {
  143. .name = "gpio-ath79",
  144. .irq_enable = ath79_gpio_irq_enable,
  145. .irq_disable = ath79_gpio_irq_disable,
  146. .irq_mask = ath79_gpio_irq_mask,
  147. .irq_unmask = ath79_gpio_irq_unmask,
  148. .irq_set_type = ath79_gpio_irq_set_type,
  149. };
  150. static void ath79_gpio_irq_handler(struct irq_desc *desc)
  151. {
  152. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  153. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  154. struct ath79_gpio_ctrl *ctrl =
  155. container_of(gc, struct ath79_gpio_ctrl, gc);
  156. unsigned long flags, pending;
  157. u32 both_edges, state;
  158. int irq;
  159. chained_irq_enter(irqchip, desc);
  160. raw_spin_lock_irqsave(&ctrl->lock, flags);
  161. pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING);
  162. /* Update the polarity of the both edges irqs */
  163. both_edges = ctrl->both_edges & pending;
  164. if (both_edges) {
  165. state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN);
  166. ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY,
  167. both_edges, ~state);
  168. }
  169. raw_spin_unlock_irqrestore(&ctrl->lock, flags);
  170. if (pending) {
  171. for_each_set_bit(irq, &pending, gc->ngpio)
  172. generic_handle_irq(
  173. irq_linear_revmap(gc->irq.domain, irq));
  174. }
  175. chained_irq_exit(irqchip, desc);
  176. }
  177. static const struct of_device_id ath79_gpio_of_match[] = {
  178. { .compatible = "qca,ar7100-gpio" },
  179. { .compatible = "qca,ar9340-gpio" },
  180. {},
  181. };
  182. MODULE_DEVICE_TABLE(of, ath79_gpio_of_match);
  183. static int ath79_gpio_probe(struct platform_device *pdev)
  184. {
  185. struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  186. struct device_node *np = pdev->dev.of_node;
  187. struct ath79_gpio_ctrl *ctrl;
  188. struct resource *res;
  189. u32 ath79_gpio_count;
  190. bool oe_inverted;
  191. int err;
  192. ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
  193. if (!ctrl)
  194. return -ENOMEM;
  195. platform_set_drvdata(pdev, ctrl);
  196. if (np) {
  197. err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
  198. if (err) {
  199. dev_err(&pdev->dev, "ngpios property is not valid\n");
  200. return err;
  201. }
  202. oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
  203. } else if (pdata) {
  204. ath79_gpio_count = pdata->ngpios;
  205. oe_inverted = pdata->oe_inverted;
  206. } else {
  207. dev_err(&pdev->dev, "No DT node or platform data found\n");
  208. return -EINVAL;
  209. }
  210. if (ath79_gpio_count >= 32) {
  211. dev_err(&pdev->dev, "ngpios must be less than 32\n");
  212. return -EINVAL;
  213. }
  214. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  215. if (!res)
  216. return -EINVAL;
  217. ctrl->base = devm_ioremap_nocache(
  218. &pdev->dev, res->start, resource_size(res));
  219. if (!ctrl->base)
  220. return -ENOMEM;
  221. raw_spin_lock_init(&ctrl->lock);
  222. err = bgpio_init(&ctrl->gc, &pdev->dev, 4,
  223. ctrl->base + AR71XX_GPIO_REG_IN,
  224. ctrl->base + AR71XX_GPIO_REG_SET,
  225. ctrl->base + AR71XX_GPIO_REG_CLEAR,
  226. oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE,
  227. oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL,
  228. 0);
  229. if (err) {
  230. dev_err(&pdev->dev, "bgpio_init failed\n");
  231. return err;
  232. }
  233. /* Use base 0 to stay compatible with legacy platforms */
  234. ctrl->gc.base = 0;
  235. err = gpiochip_add_data(&ctrl->gc, ctrl);
  236. if (err) {
  237. dev_err(&pdev->dev,
  238. "cannot add AR71xx GPIO chip, error=%d", err);
  239. return err;
  240. }
  241. if (np && !of_property_read_bool(np, "interrupt-controller"))
  242. return 0;
  243. err = gpiochip_irqchip_add(&ctrl->gc, &ath79_gpio_irqchip, 0,
  244. handle_simple_irq, IRQ_TYPE_NONE);
  245. if (err) {
  246. dev_err(&pdev->dev, "failed to add gpiochip_irqchip\n");
  247. goto gpiochip_remove;
  248. }
  249. gpiochip_set_chained_irqchip(&ctrl->gc, &ath79_gpio_irqchip,
  250. platform_get_irq(pdev, 0),
  251. ath79_gpio_irq_handler);
  252. return 0;
  253. gpiochip_remove:
  254. gpiochip_remove(&ctrl->gc);
  255. return err;
  256. }
  257. static int ath79_gpio_remove(struct platform_device *pdev)
  258. {
  259. struct ath79_gpio_ctrl *ctrl = platform_get_drvdata(pdev);
  260. gpiochip_remove(&ctrl->gc);
  261. return 0;
  262. }
  263. static struct platform_driver ath79_gpio_driver = {
  264. .driver = {
  265. .name = "ath79-gpio",
  266. .of_match_table = ath79_gpio_of_match,
  267. },
  268. .probe = ath79_gpio_probe,
  269. .remove = ath79_gpio_remove,
  270. };
  271. module_platform_driver(ath79_gpio_driver);
  272. MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X GPIO API support");
  273. MODULE_LICENSE("GPL v2");