gpio-mt7621.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  4. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  5. */
  6. #include <linux/err.h>
  7. #include <linux/gpio/driver.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/spinlock.h>
  14. #define MTK_BANK_CNT 3
  15. #define MTK_BANK_WIDTH 32
  16. #define GPIO_BANK_STRIDE 0x04
  17. #define GPIO_REG_CTRL 0x00
  18. #define GPIO_REG_POL 0x10
  19. #define GPIO_REG_DATA 0x20
  20. #define GPIO_REG_DSET 0x30
  21. #define GPIO_REG_DCLR 0x40
  22. #define GPIO_REG_REDGE 0x50
  23. #define GPIO_REG_FEDGE 0x60
  24. #define GPIO_REG_HLVL 0x70
  25. #define GPIO_REG_LLVL 0x80
  26. #define GPIO_REG_STAT 0x90
  27. #define GPIO_REG_EDGE 0xA0
  28. struct mtk_gc {
  29. struct irq_chip irq_chip;
  30. struct gpio_chip chip;
  31. spinlock_t lock;
  32. int bank;
  33. u32 rising;
  34. u32 falling;
  35. u32 hlevel;
  36. u32 llevel;
  37. };
  38. /**
  39. * struct mtk - state container for
  40. * data of the platform driver. It is 3
  41. * separate gpio-chip each one with its
  42. * own irq_chip.
  43. * @dev: device instance
  44. * @base: memory base address
  45. * @gpio_irq: irq number from the device tree
  46. * @gc_map: array of the gpio chips
  47. */
  48. struct mtk {
  49. struct device *dev;
  50. void __iomem *base;
  51. int gpio_irq;
  52. struct mtk_gc gc_map[MTK_BANK_CNT];
  53. };
  54. static inline struct mtk_gc *
  55. to_mediatek_gpio(struct gpio_chip *chip)
  56. {
  57. return container_of(chip, struct mtk_gc, chip);
  58. }
  59. static inline void
  60. mtk_gpio_w32(struct mtk_gc *rg, u32 offset, u32 val)
  61. {
  62. struct gpio_chip *gc = &rg->chip;
  63. struct mtk *mtk = gpiochip_get_data(gc);
  64. offset = (rg->bank * GPIO_BANK_STRIDE) + offset;
  65. gc->write_reg(mtk->base + offset, val);
  66. }
  67. static inline u32
  68. mtk_gpio_r32(struct mtk_gc *rg, u32 offset)
  69. {
  70. struct gpio_chip *gc = &rg->chip;
  71. struct mtk *mtk = gpiochip_get_data(gc);
  72. offset = (rg->bank * GPIO_BANK_STRIDE) + offset;
  73. return gc->read_reg(mtk->base + offset);
  74. }
  75. static irqreturn_t
  76. mediatek_gpio_irq_handler(int irq, void *data)
  77. {
  78. struct gpio_chip *gc = data;
  79. struct mtk_gc *rg = to_mediatek_gpio(gc);
  80. irqreturn_t ret = IRQ_NONE;
  81. unsigned long pending;
  82. int bit;
  83. pending = mtk_gpio_r32(rg, GPIO_REG_STAT);
  84. for_each_set_bit(bit, &pending, MTK_BANK_WIDTH) {
  85. u32 map = irq_find_mapping(gc->irq.domain, bit);
  86. generic_handle_irq(map);
  87. mtk_gpio_w32(rg, GPIO_REG_STAT, BIT(bit));
  88. ret |= IRQ_HANDLED;
  89. }
  90. return ret;
  91. }
  92. static void
  93. mediatek_gpio_irq_unmask(struct irq_data *d)
  94. {
  95. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  96. struct mtk_gc *rg = to_mediatek_gpio(gc);
  97. int pin = d->hwirq;
  98. unsigned long flags;
  99. u32 rise, fall, high, low;
  100. spin_lock_irqsave(&rg->lock, flags);
  101. rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
  102. fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
  103. high = mtk_gpio_r32(rg, GPIO_REG_HLVL);
  104. low = mtk_gpio_r32(rg, GPIO_REG_LLVL);
  105. mtk_gpio_w32(rg, GPIO_REG_REDGE, rise | (BIT(pin) & rg->rising));
  106. mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall | (BIT(pin) & rg->falling));
  107. mtk_gpio_w32(rg, GPIO_REG_HLVL, high | (BIT(pin) & rg->hlevel));
  108. mtk_gpio_w32(rg, GPIO_REG_LLVL, low | (BIT(pin) & rg->llevel));
  109. spin_unlock_irqrestore(&rg->lock, flags);
  110. }
  111. static void
  112. mediatek_gpio_irq_mask(struct irq_data *d)
  113. {
  114. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  115. struct mtk_gc *rg = to_mediatek_gpio(gc);
  116. int pin = d->hwirq;
  117. unsigned long flags;
  118. u32 rise, fall, high, low;
  119. spin_lock_irqsave(&rg->lock, flags);
  120. rise = mtk_gpio_r32(rg, GPIO_REG_REDGE);
  121. fall = mtk_gpio_r32(rg, GPIO_REG_FEDGE);
  122. high = mtk_gpio_r32(rg, GPIO_REG_HLVL);
  123. low = mtk_gpio_r32(rg, GPIO_REG_LLVL);
  124. mtk_gpio_w32(rg, GPIO_REG_FEDGE, fall & ~BIT(pin));
  125. mtk_gpio_w32(rg, GPIO_REG_REDGE, rise & ~BIT(pin));
  126. mtk_gpio_w32(rg, GPIO_REG_HLVL, high & ~BIT(pin));
  127. mtk_gpio_w32(rg, GPIO_REG_LLVL, low & ~BIT(pin));
  128. spin_unlock_irqrestore(&rg->lock, flags);
  129. }
  130. static int
  131. mediatek_gpio_irq_type(struct irq_data *d, unsigned int type)
  132. {
  133. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  134. struct mtk_gc *rg = to_mediatek_gpio(gc);
  135. int pin = d->hwirq;
  136. u32 mask = BIT(pin);
  137. if (type == IRQ_TYPE_PROBE) {
  138. if ((rg->rising | rg->falling |
  139. rg->hlevel | rg->llevel) & mask)
  140. return 0;
  141. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  142. }
  143. rg->rising &= ~mask;
  144. rg->falling &= ~mask;
  145. rg->hlevel &= ~mask;
  146. rg->llevel &= ~mask;
  147. switch (type & IRQ_TYPE_SENSE_MASK) {
  148. case IRQ_TYPE_EDGE_BOTH:
  149. rg->rising |= mask;
  150. rg->falling |= mask;
  151. break;
  152. case IRQ_TYPE_EDGE_RISING:
  153. rg->rising |= mask;
  154. break;
  155. case IRQ_TYPE_EDGE_FALLING:
  156. rg->falling |= mask;
  157. break;
  158. case IRQ_TYPE_LEVEL_HIGH:
  159. rg->hlevel |= mask;
  160. break;
  161. case IRQ_TYPE_LEVEL_LOW:
  162. rg->llevel |= mask;
  163. break;
  164. }
  165. return 0;
  166. }
  167. static int
  168. mediatek_gpio_xlate(struct gpio_chip *chip,
  169. const struct of_phandle_args *spec, u32 *flags)
  170. {
  171. int gpio = spec->args[0];
  172. struct mtk_gc *rg = to_mediatek_gpio(chip);
  173. if (rg->bank != gpio / MTK_BANK_WIDTH)
  174. return -EINVAL;
  175. if (flags)
  176. *flags = spec->args[1];
  177. return gpio % MTK_BANK_WIDTH;
  178. }
  179. static int
  180. mediatek_gpio_bank_probe(struct device *dev,
  181. struct device_node *node, int bank)
  182. {
  183. struct mtk *mtk = dev_get_drvdata(dev);
  184. struct mtk_gc *rg;
  185. void __iomem *dat, *set, *ctrl, *diro;
  186. int ret;
  187. rg = &mtk->gc_map[bank];
  188. memset(rg, 0, sizeof(*rg));
  189. spin_lock_init(&rg->lock);
  190. rg->chip.of_node = node;
  191. rg->bank = bank;
  192. dat = mtk->base + GPIO_REG_DATA + (rg->bank * GPIO_BANK_STRIDE);
  193. set = mtk->base + GPIO_REG_DSET + (rg->bank * GPIO_BANK_STRIDE);
  194. ctrl = mtk->base + GPIO_REG_DCLR + (rg->bank * GPIO_BANK_STRIDE);
  195. diro = mtk->base + GPIO_REG_CTRL + (rg->bank * GPIO_BANK_STRIDE);
  196. ret = bgpio_init(&rg->chip, dev, 4,
  197. dat, set, ctrl, diro, NULL, 0);
  198. if (ret) {
  199. dev_err(dev, "bgpio_init() failed\n");
  200. return ret;
  201. }
  202. rg->chip.of_gpio_n_cells = 2;
  203. rg->chip.of_xlate = mediatek_gpio_xlate;
  204. rg->chip.label = devm_kasprintf(dev, GFP_KERNEL, "%s-bank%d",
  205. dev_name(dev), bank);
  206. if (!rg->chip.label)
  207. return -ENOMEM;
  208. ret = devm_gpiochip_add_data(dev, &rg->chip, mtk);
  209. if (ret < 0) {
  210. dev_err(dev, "Could not register gpio %d, ret=%d\n",
  211. rg->chip.ngpio, ret);
  212. return ret;
  213. }
  214. rg->irq_chip.name = dev_name(dev);
  215. rg->irq_chip.parent_device = dev;
  216. rg->irq_chip.irq_unmask = mediatek_gpio_irq_unmask;
  217. rg->irq_chip.irq_mask = mediatek_gpio_irq_mask;
  218. rg->irq_chip.irq_mask_ack = mediatek_gpio_irq_mask;
  219. rg->irq_chip.irq_set_type = mediatek_gpio_irq_type;
  220. if (mtk->gpio_irq) {
  221. /*
  222. * Manually request the irq here instead of passing
  223. * a flow-handler to gpiochip_set_chained_irqchip,
  224. * because the irq is shared.
  225. */
  226. ret = devm_request_irq(dev, mtk->gpio_irq,
  227. mediatek_gpio_irq_handler, IRQF_SHARED,
  228. rg->chip.label, &rg->chip);
  229. if (ret) {
  230. dev_err(dev, "Error requesting IRQ %d: %d\n",
  231. mtk->gpio_irq, ret);
  232. return ret;
  233. }
  234. ret = gpiochip_irqchip_add(&rg->chip, &rg->irq_chip,
  235. 0, handle_simple_irq, IRQ_TYPE_NONE);
  236. if (ret) {
  237. dev_err(dev, "failed to add gpiochip_irqchip\n");
  238. return ret;
  239. }
  240. gpiochip_set_chained_irqchip(&rg->chip, &rg->irq_chip,
  241. mtk->gpio_irq, NULL);
  242. }
  243. /* set polarity to low for all gpios */
  244. mtk_gpio_w32(rg, GPIO_REG_POL, 0);
  245. dev_info(dev, "registering %d gpios\n", rg->chip.ngpio);
  246. return 0;
  247. }
  248. static int
  249. mediatek_gpio_probe(struct platform_device *pdev)
  250. {
  251. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  252. struct device *dev = &pdev->dev;
  253. struct device_node *np = dev->of_node;
  254. struct mtk *mtk;
  255. int i;
  256. int ret;
  257. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  258. if (!mtk)
  259. return -ENOMEM;
  260. mtk->base = devm_ioremap_resource(dev, res);
  261. if (IS_ERR(mtk->base))
  262. return PTR_ERR(mtk->base);
  263. mtk->gpio_irq = irq_of_parse_and_map(np, 0);
  264. mtk->dev = dev;
  265. platform_set_drvdata(pdev, mtk);
  266. for (i = 0; i < MTK_BANK_CNT; i++) {
  267. ret = mediatek_gpio_bank_probe(dev, np, i);
  268. if (ret)
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static const struct of_device_id mediatek_gpio_match[] = {
  274. { .compatible = "mediatek,mt7621-gpio" },
  275. {},
  276. };
  277. MODULE_DEVICE_TABLE(of, mediatek_gpio_match);
  278. static struct platform_driver mediatek_gpio_driver = {
  279. .probe = mediatek_gpio_probe,
  280. .driver = {
  281. .name = "mt7621_gpio",
  282. .of_match_table = mediatek_gpio_match,
  283. },
  284. };
  285. builtin_platform_driver(mediatek_gpio_driver);