gpio-ark.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * arkmicro gpio driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/err.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/gpio/driver.h>
  22. #include <linux/gpio.h>
  23. #define GPIO_MOD 0x00
  24. #define GPIO_RDATA 0x04
  25. #define GPIO_INTEN 0x08
  26. #define GPIO_INTLEVEL 0x0C
  27. #define GPIO_INTGEN 0x10
  28. #define GPIO_DEBOUNCE_EN 0xA0
  29. #define GPIO_DEBOUNCE_CNT0 0x80
  30. #define GPIO_INT_LOW_LEV 0
  31. #define GPIO_INT_HIGH_LEV 1
  32. struct ark_gpio_port {
  33. struct list_head node;
  34. void __iomem *base;
  35. struct clk *clk;
  36. int irq;
  37. struct irq_domain *domain;
  38. struct gpio_chip gc;
  39. struct device *dev;
  40. u32 both_edges;
  41. };
  42. static const struct of_device_id ark_gpio_of_match[] = {
  43. { .compatible = "arkmicro,ark-gpio", },
  44. { /* sentinel */ }
  45. };
  46. /*
  47. * ark gpio has one interrupt *for all* gpio ports. The list is used
  48. * to save the references to all ports, so that ark_gpio_irq_handler
  49. * can walk through all interrupt status registers.
  50. */
  51. static LIST_HEAD(ark_gpio_ports);
  52. /* Note: This driver assumes 32 GPIOs are handled in one register */
  53. static int ark_gpio_irq_set_type(struct irq_data *d, u32 type)
  54. {
  55. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  56. struct ark_gpio_port *port = gc->private;
  57. u32 val;
  58. u32 gpio_idx = d->hwirq;
  59. u32 gpio = port->gc.base + gpio_idx;
  60. int edge;
  61. port->both_edges &= ~(1 << gpio_idx);
  62. switch (type) {
  63. case IRQ_TYPE_EDGE_RISING:
  64. edge = GPIO_INT_HIGH_LEV;
  65. break;
  66. case IRQ_TYPE_EDGE_FALLING:
  67. edge = GPIO_INT_LOW_LEV;
  68. break;
  69. case IRQ_TYPE_EDGE_BOTH:
  70. val = gpio_get_value(gpio);
  71. if (val) {
  72. edge = GPIO_INT_LOW_LEV;
  73. pr_debug("ark: set GPIO %d to low trigger\n", gpio);
  74. } else {
  75. edge = GPIO_INT_HIGH_LEV;
  76. pr_debug("ark: set GPIO %d to high trigger\n", gpio);
  77. }
  78. port->both_edges |= 1 << gpio_idx;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. val = readl(port->base + GPIO_INTLEVEL);
  84. val &= ~(1 << gpio_idx);
  85. val |= edge << gpio_idx;
  86. writel(val, port->base + GPIO_INTLEVEL);
  87. return 0;
  88. }
  89. static void ark_flip_edge(struct ark_gpio_port *port, u32 gpio)
  90. {
  91. u32 val;
  92. int edge;
  93. val = readl(port->base + GPIO_INTLEVEL);
  94. edge = !(val & (1 << gpio));
  95. val &= ~(1 << gpio);
  96. writel(val | (edge << gpio), port->base + GPIO_INTLEVEL);
  97. }
  98. /* ark gpio has one interrupt *for all* gpio ports */
  99. static void ark_gpio_irq_handler(struct irq_desc *desc)
  100. {
  101. u32 irq_msk, irq_stat;
  102. struct ark_gpio_port *port;
  103. struct irq_chip *chip = irq_desc_get_chip(desc);
  104. chained_irq_enter(chip, desc);
  105. /* walk through all interrupt status registers */
  106. list_for_each_entry(port, &ark_gpio_ports, node) {
  107. irq_msk = readl(port->base + GPIO_INTEN);
  108. if (!irq_msk)
  109. continue;
  110. irq_stat = readl(port->base + GPIO_INTGEN) & irq_msk;
  111. if (irq_stat) {
  112. /* handle 32 interrupts in one status register */
  113. while (irq_stat != 0) {
  114. int irqoffset = fls(irq_stat) - 1;
  115. if (port->both_edges & (1 << irqoffset))
  116. ark_flip_edge(port, irqoffset);
  117. generic_handle_irq(irq_find_mapping(port->domain, irqoffset));
  118. irq_stat &= ~(1 << irqoffset);
  119. writel(readl(port->base + GPIO_INTGEN) & ~(1 << irqoffset), port->base + GPIO_INTGEN);
  120. }
  121. }
  122. }
  123. chained_irq_exit(chip, desc);
  124. }
  125. /*
  126. * Set interrupt number "irq" in the GPIO as a wake-up source.
  127. * While system is running, all registered GPIO interrupts need to have
  128. * wake-up enabled. When system is suspended, only selected GPIO interrupts
  129. * need to have wake-up enabled.
  130. * @param irq interrupt source number
  131. * @param enable enable as wake-up if equal to non-zero
  132. * @return This function returns 0 on success.
  133. */
  134. static int ark_gpio_irq_set_wake(struct irq_data *d, u32 enable)
  135. {
  136. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  137. struct ark_gpio_port *port = gc->private;
  138. int ret;
  139. if (enable) {
  140. ret = enable_irq_wake(port->irq);
  141. } else {
  142. ret = disable_irq_wake(port->irq);
  143. }
  144. return ret;
  145. }
  146. static int ark_gpio_init_gc(struct ark_gpio_port *port, int irq_base)
  147. {
  148. struct irq_chip_generic *gc;
  149. struct irq_chip_type *ct;
  150. int rv;
  151. gc = devm_irq_alloc_generic_chip(port->dev, "gpio-ark", 1, irq_base,
  152. port->base, handle_level_irq);
  153. if (!gc)
  154. return -ENOMEM;
  155. gc->private = port;
  156. ct = gc->chip_types;
  157. //ct->chip.irq_ack = irq_gc_ack_clr_bit;
  158. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  159. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  160. ct->chip.irq_set_type = ark_gpio_irq_set_type;
  161. ct->chip.irq_set_wake = ark_gpio_irq_set_wake;
  162. ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  163. //ct->regs.ack = GPIO_INTGEN;
  164. ct->regs.mask = GPIO_INTEN;
  165. rv = devm_irq_setup_generic_chip(port->dev, gc, IRQ_MSK(32),
  166. IRQ_GC_INIT_NESTED_LOCK,
  167. IRQ_NOREQUEST, 0);
  168. return rv;
  169. }
  170. static int ark_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  171. {
  172. struct ark_gpio_port *port = gpiochip_get_data(gc);
  173. return irq_find_mapping(port->domain, offset);
  174. }
  175. static int ark_gpio_set_debounce(struct ark_gpio_port *port, unsigned offset,
  176. unsigned debounce_ms)
  177. {
  178. int debounce_count;
  179. int gpio;
  180. u32 val;
  181. if (offset < port->gc.ngpio) {
  182. gpio = offset + port->gc.base;
  183. if (gpio < 0 || gpio > 7)
  184. return -EINVAL;
  185. } else {
  186. return -EINVAL;
  187. }
  188. port->clk = devm_clk_get(port->dev, NULL);
  189. if (IS_ERR(port->clk))
  190. return PTR_ERR(port->clk);
  191. debounce_count = clk_get_rate(port->clk) / 1000 * debounce_ms;
  192. writel(debounce_count, port->base + GPIO_DEBOUNCE_CNT0 + gpio * 4);
  193. val = readl(port->base + GPIO_DEBOUNCE_EN);
  194. val |= 1 << offset;
  195. writel(val, port->base + GPIO_DEBOUNCE_EN);
  196. return 0;
  197. }
  198. static int ark_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  199. unsigned long config)
  200. {
  201. struct ark_gpio_port *port = gpiochip_get_data(gc);
  202. switch (pinconf_to_config_param(config)) {
  203. case PIN_CONFIG_INPUT_DEBOUNCE:
  204. return ark_gpio_set_debounce(port, offset,
  205. pinconf_to_config_argument(config));
  206. default:
  207. break;
  208. }
  209. return -ENOTSUPP;
  210. }
  211. static int ark_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  212. {
  213. struct ark_gpio_port *port = gpiochip_get_data(gc);
  214. u32 val = readl(port->base + GPIO_MOD);
  215. val |= 1 << offset;
  216. writel(val, port->base + GPIO_MOD);
  217. return 0;
  218. }
  219. static int ark_gpio_direction_output(struct gpio_chip *gc,
  220. unsigned offset, int value)
  221. {
  222. struct ark_gpio_port *port = gpiochip_get_data(gc);
  223. u32 val = readl(port->base + GPIO_MOD);
  224. val &= ~(1 << offset);
  225. writel(val, port->base + GPIO_MOD);
  226. gc->set(gc, offset, value);
  227. return 0;
  228. }
  229. static int ark_gpio_get(struct gpio_chip *gc, unsigned offset)
  230. {
  231. struct ark_gpio_port *port = gpiochip_get_data(gc);
  232. u32 val = readl(port->base + GPIO_RDATA);
  233. return !!(val & (1 << offset));
  234. }
  235. static void ark_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  236. {
  237. struct ark_gpio_port *port = gpiochip_get_data(gc);
  238. u32 val = readl(port->base + GPIO_RDATA);
  239. if (value) val |= (1 << offset);
  240. else val &= ~(1 << offset);
  241. writel(val, port->base + GPIO_RDATA);
  242. }
  243. static int ark_gpio_probe(struct platform_device *pdev)
  244. {
  245. struct device_node *np = pdev->dev.of_node;
  246. struct ark_gpio_port *port;
  247. struct resource *res;
  248. int irq_base;
  249. int ret;
  250. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  251. if (!port)
  252. return -ENOMEM;
  253. port->dev = &pdev->dev;
  254. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. port->base = devm_ioremap_resource(&pdev->dev, res);
  256. if (IS_ERR(port->base)) {
  257. return PTR_ERR(port->base);
  258. }
  259. port->irq = platform_get_irq(pdev, 0);
  260. if (port->irq < 0)
  261. return port->irq;
  262. /* disable the interrupt and clear the status */
  263. writel(0, port->base + GPIO_INTEN);
  264. writel(0, port->base + GPIO_INTGEN);
  265. /*
  266. * Setup one handler for all GPIO interrupts. Actually setting
  267. * the handler is needed only once, but doing it for every port
  268. * is more robust and easier.
  269. */
  270. irq_set_chained_handler(port->irq, ark_gpio_irq_handler);
  271. if (of_property_read_bool(np, "gpio-ranges")) {
  272. port->gc.request = gpiochip_generic_request;
  273. port->gc.free = gpiochip_generic_free;
  274. }
  275. port->gc.parent = port->dev;
  276. port->gc.label = dev_name(port->dev);
  277. port->gc.direction_input = ark_gpio_direction_input;
  278. port->gc.direction_output = ark_gpio_direction_output;
  279. port->gc.get = ark_gpio_get;
  280. port->gc.set = ark_gpio_set;
  281. port->gc.to_irq = ark_gpio_to_irq;
  282. port->gc.set_config = ark_gpio_set_config;
  283. port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 :
  284. pdev->id * 32;
  285. port->gc.ngpio = 32;
  286. ret = devm_gpiochip_add_data(&pdev->dev, &port->gc, port);
  287. if (ret)
  288. goto out_bgio;
  289. irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id());
  290. if (irq_base < 0) {
  291. ret = irq_base;
  292. goto out_bgio;
  293. }
  294. port->domain = irq_domain_add_legacy(np, 32, irq_base, 0,
  295. &irq_domain_simple_ops, NULL);
  296. if (!port->domain) {
  297. ret = -ENODEV;
  298. goto out_bgio;
  299. }
  300. /* gpio-ark can be a generic irq chip */
  301. ret = ark_gpio_init_gc(port, irq_base);
  302. if (ret < 0)
  303. goto out_irqdomain_remove;
  304. list_add_tail(&port->node, &ark_gpio_ports);
  305. return 0;
  306. out_irqdomain_remove:
  307. irq_domain_remove(port->domain);
  308. out_bgio:
  309. dev_info(&pdev->dev, "%s failed with errno %d\n", __func__, ret);
  310. return ret;
  311. return 0;
  312. }
  313. static struct platform_driver ark_gpio_driver = {
  314. .driver = {
  315. .name = "gpio-ark",
  316. .of_match_table = of_match_ptr(ark_gpio_of_match),
  317. },
  318. .probe = ark_gpio_probe,
  319. };
  320. static int __init ark_gpio_init(void)
  321. {
  322. return platform_driver_register(&ark_gpio_driver);
  323. }
  324. postcore_initcall(ark_gpio_init);
  325. MODULE_AUTHOR("Sim");
  326. MODULE_DESCRIPTION("Arkmicro Watchdog Timer driver");
  327. MODULE_LICENSE("GPL v2");