0506-GPIO-ralink-add-mt7621-gpio-controller.patch 6.1 KB

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