0212-GPIO-ralink-add-mt7621-gpio-controller.patch 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. From 2a9b5a9fc1a0707b95dbe61dd1c30b9337cb457d Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sun, 16 Mar 2014 05:26:34 +0000
  4. Subject: [PATCH 212/215] GPIO: ralink: add mt7621 gpio controller
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. arch/mips/Kconfig | 5 +-
  8. drivers/gpio/Kconfig | 6 ++
  9. drivers/gpio/Makefile | 1 +
  10. drivers/gpio/gpio-mt7621.c | 183 ++++++++++++++++++++++++++++++++++++++++++++
  11. 4 files changed, 194 insertions(+), 1 deletion(-)
  12. create mode 100644 drivers/gpio/gpio-mt7621.c
  13. --- a/arch/mips/Kconfig
  14. +++ b/arch/mips/Kconfig
  15. @@ -448,7 +448,10 @@ config RALINK
  16. select ARCH_REQUIRE_GPIOLIB
  17. select PINCTRL
  18. select PINCTRL_RT2880
  19. -
  20. + select ARCH_HAS_RESET_CONTROLLER
  21. + select RESET_CONTROLLER
  22. + select ARCH_REQUIRE_GPIOLIB
  23. +
  24. config SGI_IP22
  25. bool "SGI IP22 (Indy/Indigo2)"
  26. select FW_ARC
  27. --- a/drivers/gpio/Kconfig
  28. +++ b/drivers/gpio/Kconfig
  29. @@ -710,6 +710,12 @@ config GPIO_MSIC
  30. Enable support for GPIO on intel MSIC controllers found in
  31. intel MID devices
  32. +config GPIO_MT7621
  33. + bool "Mediatek GPIO Support"
  34. + depends on SOC_MT7621
  35. + help
  36. + Say yes here to support the Mediatek SoC GPIO device
  37. +
  38. comment "USB GPIO expanders:"
  39. config GPIO_VIPERBOARD
  40. --- a/drivers/gpio/Makefile
  41. +++ b/drivers/gpio/Makefile
  42. @@ -88,3 +88,4 @@ obj-$(CONFIG_GPIO_WM831X) += gpio-wm831x
  43. obj-$(CONFIG_GPIO_WM8350) += gpio-wm8350.o
  44. obj-$(CONFIG_GPIO_WM8994) += gpio-wm8994.o
  45. obj-$(CONFIG_GPIO_XILINX) += gpio-xilinx.o
  46. +obj-$(CONFIG_GPIO_MT7621) += gpio-mt7621.o
  47. --- /dev/null
  48. +++ b/drivers/gpio/gpio-mt7621.c
  49. @@ -0,0 +1,183 @@
  50. +/*
  51. + * This program is free software; you can redistribute it and/or modify it
  52. + * under the terms of the GNU General Public License version 2 as published
  53. + * by the Free Software Foundation.
  54. + *
  55. + * Copyright (C) 2009-2011 Gabor Juhos <[email protected]>
  56. + * Copyright (C) 2013 John Crispin <[email protected]>
  57. + */
  58. +
  59. +#include <linux/io.h>
  60. +#include <linux/err.h>
  61. +#include <linux/gpio.h>
  62. +#include <linux/module.h>
  63. +#include <linux/of_irq.h>
  64. +#include <linux/spinlock.h>
  65. +#include <linux/irqdomain.h>
  66. +#include <linux/interrupt.h>
  67. +#include <linux/platform_device.h>
  68. +
  69. +#define MTK_BANK_WIDTH 32
  70. +
  71. +enum mediatek_gpio_reg {
  72. + GPIO_REG_CTRL = 0,
  73. + GPIO_REG_POL,
  74. + GPIO_REG_DATA,
  75. + GPIO_REG_DSET,
  76. + GPIO_REG_DCLR,
  77. +};
  78. +
  79. +static void __iomem *mtk_gc_membase;
  80. +
  81. +struct mtk_gc {
  82. + struct gpio_chip chip;
  83. + spinlock_t lock;
  84. + int bank;
  85. +};
  86. +
  87. +int
  88. +gpio_to_irq(unsigned gpio)
  89. +{
  90. + return -1;
  91. +}
  92. +
  93. +static inline struct mtk_gc
  94. +*to_mediatek_gpio(struct gpio_chip *chip)
  95. +{
  96. + struct mtk_gc *mgc;
  97. +
  98. + mgc = container_of(chip, struct mtk_gc, chip);
  99. +
  100. + return mgc;
  101. +}
  102. +
  103. +static inline void
  104. +mtk_gpio_w32(struct mtk_gc *rg, u8 reg, u32 val)
  105. +{
  106. + iowrite32(val, mtk_gc_membase + (reg * 0x10) + (rg->bank * 0x4));
  107. +}
  108. +
  109. +static inline u32
  110. +mtk_gpio_r32(struct mtk_gc *rg, u8 reg)
  111. +{
  112. + return ioread32(mtk_gc_membase + (reg * 0x10) + (rg->bank * 0x4));
  113. +}
  114. +
  115. +static void
  116. +mediatek_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  117. +{
  118. + struct mtk_gc *rg = to_mediatek_gpio(chip);
  119. +
  120. + mtk_gpio_w32(rg, (value) ? GPIO_REG_DSET : GPIO_REG_DCLR, BIT(offset));
  121. +}
  122. +
  123. +static int
  124. +mediatek_gpio_get(struct gpio_chip *chip, unsigned offset)
  125. +{
  126. + struct mtk_gc *rg = to_mediatek_gpio(chip);
  127. +
  128. + return !!(mtk_gpio_r32(rg, GPIO_REG_DATA) & BIT(offset));
  129. +}
  130. +
  131. +static int
  132. +mediatek_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  133. +{
  134. + struct mtk_gc *rg = to_mediatek_gpio(chip);
  135. + unsigned long flags;
  136. + u32 t;
  137. +
  138. + spin_lock_irqsave(&rg->lock, flags);
  139. + t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
  140. + t &= ~BIT(offset);
  141. + mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
  142. + spin_unlock_irqrestore(&rg->lock, flags);
  143. +
  144. + return 0;
  145. +}
  146. +
  147. +static int
  148. +mediatek_gpio_direction_output(struct gpio_chip *chip,
  149. + unsigned offset, int value)
  150. +{
  151. + struct mtk_gc *rg = to_mediatek_gpio(chip);
  152. + unsigned long flags;
  153. + u32 t;
  154. +
  155. + spin_lock_irqsave(&rg->lock, flags);
  156. + t = mtk_gpio_r32(rg, GPIO_REG_CTRL);
  157. + t |= BIT(offset);
  158. + mtk_gpio_w32(rg, GPIO_REG_CTRL, t);
  159. + mediatek_gpio_set(chip, offset, value);
  160. + spin_unlock_irqrestore(&rg->lock, flags);
  161. +
  162. + return 0;
  163. +}
  164. +
  165. +static int
  166. +mediatek_gpio_bank_probe(struct platform_device *pdev, struct device_node *bank)
  167. +{
  168. + const __be32 *id = of_get_property(bank, "reg", NULL);
  169. + struct mtk_gc *rg = devm_kzalloc(&pdev->dev,
  170. + sizeof(struct mtk_gc), GFP_KERNEL);
  171. + if (!rg || !id)
  172. + return -ENOMEM;
  173. +
  174. + spin_lock_init(&rg->lock);
  175. +
  176. + rg->chip.dev = &pdev->dev;
  177. + rg->chip.label = dev_name(&pdev->dev);
  178. + rg->chip.of_node = bank;
  179. + rg->chip.base = MTK_BANK_WIDTH * be32_to_cpu(*id);
  180. + rg->chip.ngpio = MTK_BANK_WIDTH;
  181. + rg->chip.direction_input = mediatek_gpio_direction_input;
  182. + rg->chip.direction_output = mediatek_gpio_direction_output;
  183. + rg->chip.get = mediatek_gpio_get;
  184. + rg->chip.set = mediatek_gpio_set;
  185. +
  186. + /* set polarity to low for all gpios */
  187. + mtk_gpio_w32(rg, GPIO_REG_POL, 0);
  188. +
  189. + dev_info(&pdev->dev, "registering %d gpios\n", rg->chip.ngpio);
  190. +
  191. + return gpiochip_add(&rg->chip);
  192. +}
  193. +
  194. +static int
  195. +mediatek_gpio_probe(struct platform_device *pdev)
  196. +{
  197. + struct device_node *bank, *np = pdev->dev.of_node;
  198. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  199. +
  200. + mtk_gc_membase = devm_request_and_ioremap(&pdev->dev, res);
  201. + if (IS_ERR(mtk_gc_membase))
  202. + return PTR_ERR(mtk_gc_membase);
  203. +
  204. + for_each_child_of_node(np, bank)
  205. + if (of_device_is_compatible(bank, "mtk,mt7621-gpio-bank"))
  206. + mediatek_gpio_bank_probe(pdev, bank);
  207. +
  208. + return 0;
  209. +}
  210. +
  211. +static const struct of_device_id mediatek_gpio_match[] = {
  212. + { .compatible = "mtk,mt7621-gpio" },
  213. + {},
  214. +};
  215. +MODULE_DEVICE_TABLE(of, mediatek_gpio_match);
  216. +
  217. +static struct platform_driver mediatek_gpio_driver = {
  218. + .probe = mediatek_gpio_probe,
  219. + .driver = {
  220. + .name = "mt7621_gpio",
  221. + .owner = THIS_MODULE,
  222. + .of_match_table = mediatek_gpio_match,
  223. + },
  224. +};
  225. +
  226. +static int __init
  227. +mediatek_gpio_init(void)
  228. +{
  229. + return platform_driver_register(&mediatek_gpio_driver);
  230. +}
  231. +
  232. +subsys_initcall(mediatek_gpio_init);