gpio-ftgpio010.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Faraday Technolog FTGPIO010 gpiochip and interrupt routines
  4. * Copyright (C) 2017 Linus Walleij <[email protected]>
  5. *
  6. * Based on arch/arm/mach-gemini/gpio.c:
  7. * Copyright (C) 2008-2009 Paulius Zaleckas <[email protected]>
  8. *
  9. * Based on plat-mxc/gpio.c:
  10. * MXC GPIO support. (c) 2008 Daniel Mack <[email protected]>
  11. * Copyright 2008 Juergen Beisert, [email protected]
  12. */
  13. #include <linux/gpio/driver.h>
  14. #include <linux/io.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. /* GPIO registers definition */
  20. #define GPIO_DATA_OUT 0x00
  21. #define GPIO_DATA_IN 0x04
  22. #define GPIO_DIR 0x08
  23. #define GPIO_BYPASS_IN 0x0C
  24. #define GPIO_DATA_SET 0x10
  25. #define GPIO_DATA_CLR 0x14
  26. #define GPIO_PULL_EN 0x18
  27. #define GPIO_PULL_TYPE 0x1C
  28. #define GPIO_INT_EN 0x20
  29. #define GPIO_INT_STAT_RAW 0x24
  30. #define GPIO_INT_STAT_MASKED 0x28
  31. #define GPIO_INT_MASK 0x2C
  32. #define GPIO_INT_CLR 0x30
  33. #define GPIO_INT_TYPE 0x34
  34. #define GPIO_INT_BOTH_EDGE 0x38
  35. #define GPIO_INT_LEVEL 0x3C
  36. #define GPIO_DEBOUNCE_EN 0x40
  37. #define GPIO_DEBOUNCE_PRESCALE 0x44
  38. /**
  39. * struct ftgpio_gpio - Gemini GPIO state container
  40. * @dev: containing device for this instance
  41. * @gc: gpiochip for this instance
  42. * @base: remapped I/O-memory base
  43. * @clk: silicon clock
  44. */
  45. struct ftgpio_gpio {
  46. struct device *dev;
  47. struct gpio_chip gc;
  48. void __iomem *base;
  49. struct clk *clk;
  50. };
  51. static void ftgpio_gpio_ack_irq(struct irq_data *d)
  52. {
  53. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  54. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  55. writel(BIT(irqd_to_hwirq(d)), g->base + GPIO_INT_CLR);
  56. }
  57. static void ftgpio_gpio_mask_irq(struct irq_data *d)
  58. {
  59. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  60. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  61. u32 val;
  62. val = readl(g->base + GPIO_INT_EN);
  63. val &= ~BIT(irqd_to_hwirq(d));
  64. writel(val, g->base + GPIO_INT_EN);
  65. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  66. }
  67. static void ftgpio_gpio_unmask_irq(struct irq_data *d)
  68. {
  69. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  70. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  71. u32 val;
  72. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  73. val = readl(g->base + GPIO_INT_EN);
  74. val |= BIT(irqd_to_hwirq(d));
  75. writel(val, g->base + GPIO_INT_EN);
  76. }
  77. static int ftgpio_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  78. {
  79. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  80. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  81. u32 mask = BIT(irqd_to_hwirq(d));
  82. u32 reg_both, reg_level, reg_type;
  83. reg_type = readl(g->base + GPIO_INT_TYPE);
  84. reg_level = readl(g->base + GPIO_INT_LEVEL);
  85. reg_both = readl(g->base + GPIO_INT_BOTH_EDGE);
  86. switch (type) {
  87. case IRQ_TYPE_EDGE_BOTH:
  88. irq_set_handler_locked(d, handle_edge_irq);
  89. reg_type &= ~mask;
  90. reg_both |= mask;
  91. break;
  92. case IRQ_TYPE_EDGE_RISING:
  93. irq_set_handler_locked(d, handle_edge_irq);
  94. reg_type &= ~mask;
  95. reg_both &= ~mask;
  96. reg_level &= ~mask;
  97. break;
  98. case IRQ_TYPE_EDGE_FALLING:
  99. irq_set_handler_locked(d, handle_edge_irq);
  100. reg_type &= ~mask;
  101. reg_both &= ~mask;
  102. reg_level |= mask;
  103. break;
  104. case IRQ_TYPE_LEVEL_HIGH:
  105. irq_set_handler_locked(d, handle_level_irq);
  106. reg_type |= mask;
  107. reg_level &= ~mask;
  108. break;
  109. case IRQ_TYPE_LEVEL_LOW:
  110. irq_set_handler_locked(d, handle_level_irq);
  111. reg_type |= mask;
  112. reg_level |= mask;
  113. break;
  114. default:
  115. irq_set_handler_locked(d, handle_bad_irq);
  116. return -EINVAL;
  117. }
  118. writel(reg_type, g->base + GPIO_INT_TYPE);
  119. writel(reg_level, g->base + GPIO_INT_LEVEL);
  120. writel(reg_both, g->base + GPIO_INT_BOTH_EDGE);
  121. ftgpio_gpio_ack_irq(d);
  122. return 0;
  123. }
  124. static void ftgpio_gpio_irq_handler(struct irq_desc *desc)
  125. {
  126. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  127. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  128. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  129. int offset;
  130. unsigned long stat;
  131. chained_irq_enter(irqchip, desc);
  132. stat = readl(g->base + GPIO_INT_STAT_RAW);
  133. if (stat)
  134. for_each_set_bit(offset, &stat, gc->ngpio)
  135. generic_handle_domain_irq(gc->irq.domain, offset);
  136. chained_irq_exit(irqchip, desc);
  137. }
  138. static int ftgpio_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  139. unsigned long config)
  140. {
  141. enum pin_config_param param = pinconf_to_config_param(config);
  142. u32 arg = pinconf_to_config_argument(config);
  143. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  144. unsigned long pclk_freq;
  145. u32 deb_div;
  146. u32 val;
  147. if (param != PIN_CONFIG_INPUT_DEBOUNCE)
  148. return -ENOTSUPP;
  149. /*
  150. * Debounce only works if interrupts are enabled. The manual
  151. * states that if PCLK is 66 MHz, and this is set to 0x7D0, then
  152. * PCLK is divided down to 33 kHz for the debounce timer. 0x7D0 is
  153. * 2000 decimal, so what they mean is simply that the PCLK is
  154. * divided by this value.
  155. *
  156. * As we get a debounce setting in microseconds, we calculate the
  157. * desired period time and see if we can get a suitable debounce
  158. * time.
  159. */
  160. pclk_freq = clk_get_rate(g->clk);
  161. deb_div = DIV_ROUND_CLOSEST(pclk_freq, arg);
  162. /* This register is only 24 bits wide */
  163. if (deb_div > (1 << 24))
  164. return -ENOTSUPP;
  165. dev_dbg(g->dev, "prescale divisor: %08x, resulting frequency %lu Hz\n",
  166. deb_div, (pclk_freq/deb_div));
  167. val = readl(g->base + GPIO_DEBOUNCE_PRESCALE);
  168. if (val == deb_div) {
  169. /*
  170. * The debounce timer happens to already be set to the
  171. * desirable value, what a coincidence! We can just enable
  172. * debounce on this GPIO line and return. This happens more
  173. * often than you think, for example when all GPIO keys
  174. * on a system are requesting the same debounce interval.
  175. */
  176. val = readl(g->base + GPIO_DEBOUNCE_EN);
  177. val |= BIT(offset);
  178. writel(val, g->base + GPIO_DEBOUNCE_EN);
  179. return 0;
  180. }
  181. val = readl(g->base + GPIO_DEBOUNCE_EN);
  182. if (val) {
  183. /*
  184. * Oh no! Someone is already using the debounce with
  185. * another setting than what we need. Bummer.
  186. */
  187. return -ENOTSUPP;
  188. }
  189. /* First come, first serve */
  190. writel(deb_div, g->base + GPIO_DEBOUNCE_PRESCALE);
  191. /* Enable debounce */
  192. val |= BIT(offset);
  193. writel(val, g->base + GPIO_DEBOUNCE_EN);
  194. return 0;
  195. }
  196. static const struct irq_chip ftgpio_irq_chip = {
  197. .name = "FTGPIO010",
  198. .irq_ack = ftgpio_gpio_ack_irq,
  199. .irq_mask = ftgpio_gpio_mask_irq,
  200. .irq_unmask = ftgpio_gpio_unmask_irq,
  201. .irq_set_type = ftgpio_gpio_set_irq_type,
  202. .flags = IRQCHIP_IMMUTABLE,
  203. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  204. };
  205. static int ftgpio_gpio_probe(struct platform_device *pdev)
  206. {
  207. struct device *dev = &pdev->dev;
  208. struct ftgpio_gpio *g;
  209. struct gpio_irq_chip *girq;
  210. int irq;
  211. int ret;
  212. g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
  213. if (!g)
  214. return -ENOMEM;
  215. g->dev = dev;
  216. g->base = devm_platform_ioremap_resource(pdev, 0);
  217. if (IS_ERR(g->base))
  218. return PTR_ERR(g->base);
  219. irq = platform_get_irq(pdev, 0);
  220. if (irq <= 0)
  221. return irq ? irq : -EINVAL;
  222. g->clk = devm_clk_get(dev, NULL);
  223. if (!IS_ERR(g->clk)) {
  224. ret = clk_prepare_enable(g->clk);
  225. if (ret)
  226. return ret;
  227. } else if (PTR_ERR(g->clk) == -EPROBE_DEFER) {
  228. /*
  229. * Percolate deferrals, for anything else,
  230. * just live without the clocking.
  231. */
  232. return PTR_ERR(g->clk);
  233. }
  234. ret = bgpio_init(&g->gc, dev, 4,
  235. g->base + GPIO_DATA_IN,
  236. g->base + GPIO_DATA_SET,
  237. g->base + GPIO_DATA_CLR,
  238. g->base + GPIO_DIR,
  239. NULL,
  240. 0);
  241. if (ret) {
  242. dev_err(dev, "unable to init generic GPIO\n");
  243. goto dis_clk;
  244. }
  245. g->gc.label = "FTGPIO010";
  246. g->gc.base = -1;
  247. g->gc.parent = dev;
  248. g->gc.owner = THIS_MODULE;
  249. /* ngpio is set by bgpio_init() */
  250. /* We need a silicon clock to do debounce */
  251. if (!IS_ERR(g->clk))
  252. g->gc.set_config = ftgpio_gpio_set_config;
  253. girq = &g->gc.irq;
  254. gpio_irq_chip_set_chip(girq, &ftgpio_irq_chip);
  255. girq->parent_handler = ftgpio_gpio_irq_handler;
  256. girq->num_parents = 1;
  257. girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
  258. GFP_KERNEL);
  259. if (!girq->parents) {
  260. ret = -ENOMEM;
  261. goto dis_clk;
  262. }
  263. girq->default_type = IRQ_TYPE_NONE;
  264. girq->handler = handle_bad_irq;
  265. girq->parents[0] = irq;
  266. /* Disable, unmask and clear all interrupts */
  267. writel(0x0, g->base + GPIO_INT_EN);
  268. writel(0x0, g->base + GPIO_INT_MASK);
  269. writel(~0x0, g->base + GPIO_INT_CLR);
  270. /* Clear any use of debounce */
  271. writel(0x0, g->base + GPIO_DEBOUNCE_EN);
  272. ret = devm_gpiochip_add_data(dev, &g->gc, g);
  273. if (ret)
  274. goto dis_clk;
  275. platform_set_drvdata(pdev, g);
  276. dev_info(dev, "FTGPIO010 @%p registered\n", g->base);
  277. return 0;
  278. dis_clk:
  279. clk_disable_unprepare(g->clk);
  280. return ret;
  281. }
  282. static int ftgpio_gpio_remove(struct platform_device *pdev)
  283. {
  284. struct ftgpio_gpio *g = platform_get_drvdata(pdev);
  285. clk_disable_unprepare(g->clk);
  286. return 0;
  287. }
  288. static const struct of_device_id ftgpio_gpio_of_match[] = {
  289. {
  290. .compatible = "cortina,gemini-gpio",
  291. },
  292. {
  293. .compatible = "moxa,moxart-gpio",
  294. },
  295. {
  296. .compatible = "faraday,ftgpio010",
  297. },
  298. {},
  299. };
  300. static struct platform_driver ftgpio_gpio_driver = {
  301. .driver = {
  302. .name = "ftgpio010-gpio",
  303. .of_match_table = of_match_ptr(ftgpio_gpio_of_match),
  304. },
  305. .probe = ftgpio_gpio_probe,
  306. .remove = ftgpio_gpio_remove,
  307. };
  308. builtin_platform_driver(ftgpio_gpio_driver);