gpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2012 Gateworks Corporation
  3. * Chris Lang <clang@gateworks.com>
  4. * Tim Harvey <tharvey@gateworks.com>
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, Version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqchip/chained_irq.h>
  14. #include <linux/io.h>
  15. #include <linux/gpio.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <asm/mach/irq.h>
  19. /*
  20. * Registers
  21. */
  22. #define GPIO_INPUT 0x04
  23. #define GPIO_DIR 0x08
  24. #define GPIO_SET 0x10
  25. #define GPIO_CLEAR 0x14
  26. #define GPIO_INTERRUPT_ENABLE 0x20
  27. #define GPIO_INTERRUPT_RAW_STATUS 0x24
  28. #define GPIO_INTERRUPT_MASKED_STATUS 0x28
  29. #define GPIO_INTERRUPT_MASK 0x2C
  30. #define GPIO_INTERRUPT_CLEAR 0x30
  31. #define GPIO_INTERRUPT_TRIGGER_METHOD 0x34
  32. #define GPIO_INTERRUPT_TRIGGER_BOTH_EDGES 0x38
  33. #define GPIO_INTERRUPT_TRIGGER_TYPE 0x3C
  34. #define GPIO_INTERRUPT_TRIGGER_METHOD_EDGE 0
  35. #define GPIO_INTERRUPT_TRIGGER_METHOD_LEVEL 1
  36. #define GPIO_INTERRUPT_TRIGGER_EDGE_SINGLE 0
  37. #define GPIO_INTERRUPT_TRIGGER_EDGE_BOTH 1
  38. #define GPIO_INTERRUPT_TRIGGER_TYPE_RISING 0
  39. #define GPIO_INTERRUPT_TRIGGER_TYPE_FALLING 1
  40. #define GPIO_INTERRUPT_TRIGGER_TYPE_HIGH 0
  41. #define GPIO_INTERRUPT_TRIGGER_TYPE_LOW 1
  42. struct cns3xxx_gpio_chip {
  43. struct gpio_chip chip;
  44. struct irq_domain *domain;
  45. spinlock_t lock;
  46. void __iomem *base;
  47. };
  48. static struct cns3xxx_gpio_chip cns3xxx_gpio_chips[2];
  49. static int cns3xxx_gpio_chip_count;
  50. static inline void
  51. __set_direction(struct cns3xxx_gpio_chip *cchip, unsigned pin, int input)
  52. {
  53. u32 reg;
  54. reg = __raw_readl(cchip->base + GPIO_DIR);
  55. if (input)
  56. reg &= ~(1 << pin);
  57. else
  58. reg |= (1 << pin);
  59. __raw_writel(reg, cchip->base + GPIO_DIR);
  60. }
  61. /*
  62. * GENERIC_GPIO primatives
  63. */
  64. static int cns3xxx_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  65. {
  66. struct cns3xxx_gpio_chip *cchip =
  67. container_of(chip, struct cns3xxx_gpio_chip, chip);
  68. unsigned long flags;
  69. spin_lock_irqsave(&cchip->lock, flags);
  70. __set_direction(cchip, pin, 1);
  71. spin_unlock_irqrestore(&cchip->lock, flags);
  72. return 0;
  73. }
  74. static int cns3xxx_gpio_get(struct gpio_chip *chip, unsigned pin)
  75. {
  76. struct cns3xxx_gpio_chip *cchip =
  77. container_of(chip, struct cns3xxx_gpio_chip, chip);
  78. int val;
  79. val = ((__raw_readl(cchip->base + GPIO_INPUT) >> pin) & 0x1);
  80. return val;
  81. }
  82. static int cns3xxx_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int level)
  83. {
  84. struct cns3xxx_gpio_chip *cchip =
  85. container_of(chip, struct cns3xxx_gpio_chip, chip);
  86. unsigned long flags;
  87. spin_lock_irqsave(&cchip->lock, flags);
  88. if (level)
  89. __raw_writel(1 << pin, cchip->base + GPIO_SET);
  90. else
  91. __raw_writel(1 << pin, cchip->base + GPIO_CLEAR);
  92. __set_direction(cchip, pin, 0);
  93. spin_unlock_irqrestore(&cchip->lock, flags);
  94. return 0;
  95. }
  96. static void cns3xxx_gpio_set(struct gpio_chip *chip, unsigned pin,
  97. int level)
  98. {
  99. struct cns3xxx_gpio_chip *cchip =
  100. container_of(chip, struct cns3xxx_gpio_chip, chip);
  101. if (level)
  102. __raw_writel(1 << pin, cchip->base + GPIO_SET);
  103. else
  104. __raw_writel(1 << pin, cchip->base + GPIO_CLEAR);
  105. }
  106. static int cns3xxx_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  107. {
  108. struct cns3xxx_gpio_chip *cchip =
  109. container_of(chip, struct cns3xxx_gpio_chip, chip);
  110. return irq_find_mapping(cchip->domain, pin);
  111. }
  112. /*
  113. * IRQ support
  114. */
  115. /* one interrupt per GPIO controller (GPIOA/GPIOB)
  116. * this is called in task context, with IRQs enabled
  117. */
  118. static void cns3xxx_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  119. {
  120. struct cns3xxx_gpio_chip *cchip = irq_get_handler_data(irq);
  121. struct irq_chip *chip = irq_get_chip(irq);
  122. u16 i;
  123. u32 reg;
  124. chained_irq_enter(chip, desc); /* mask and ack the base interrupt */
  125. /* see which pin(s) triggered the interrupt */
  126. reg = __raw_readl(cchip->base + GPIO_INTERRUPT_RAW_STATUS);
  127. for (i = 0; i < 32; i++) {
  128. if (reg & (1 << i)) {
  129. /* let the generic IRQ layer handle an interrupt */
  130. generic_handle_irq(irq_find_mapping(cchip->domain, i));
  131. }
  132. }
  133. chained_irq_exit(chip, desc); /* unmask the base interrupt */
  134. }
  135. static int cns3xxx_gpio_irq_set_type(struct irq_data *d, u32 irqtype)
  136. {
  137. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  138. struct cns3xxx_gpio_chip *cchip = gc->private;
  139. u32 gpio = d->hwirq;
  140. unsigned long flags;
  141. u32 method, edges, type;
  142. spin_lock_irqsave(&cchip->lock, flags);
  143. method = __raw_readl(cchip->base + GPIO_INTERRUPT_TRIGGER_METHOD);
  144. edges = __raw_readl(cchip->base + GPIO_INTERRUPT_TRIGGER_BOTH_EDGES);
  145. type = __raw_readl(cchip->base + GPIO_INTERRUPT_TRIGGER_TYPE);
  146. method &= ~(1 << gpio);
  147. edges &= ~(1 << gpio);
  148. type &= ~(1 << gpio);
  149. switch(irqtype) {
  150. case IRQ_TYPE_EDGE_RISING:
  151. method |= (GPIO_INTERRUPT_TRIGGER_METHOD_EDGE << gpio);
  152. edges |= (GPIO_INTERRUPT_TRIGGER_EDGE_SINGLE << gpio);
  153. type |= (GPIO_INTERRUPT_TRIGGER_TYPE_RISING << gpio);
  154. break;
  155. case IRQ_TYPE_EDGE_FALLING:
  156. method |= (GPIO_INTERRUPT_TRIGGER_METHOD_EDGE << gpio);
  157. edges |= (GPIO_INTERRUPT_TRIGGER_EDGE_SINGLE << gpio);
  158. type |= (GPIO_INTERRUPT_TRIGGER_TYPE_FALLING << gpio);
  159. break;
  160. case IRQ_TYPE_EDGE_BOTH:
  161. method |= (GPIO_INTERRUPT_TRIGGER_METHOD_EDGE << gpio);
  162. edges |= (GPIO_INTERRUPT_TRIGGER_EDGE_BOTH << gpio);
  163. break;
  164. case IRQ_TYPE_LEVEL_LOW:
  165. method |= (GPIO_INTERRUPT_TRIGGER_METHOD_LEVEL << gpio);
  166. type |= (GPIO_INTERRUPT_TRIGGER_TYPE_LOW << gpio);
  167. break;
  168. case IRQ_TYPE_LEVEL_HIGH:
  169. method |= (GPIO_INTERRUPT_TRIGGER_METHOD_LEVEL << gpio);
  170. type |= (GPIO_INTERRUPT_TRIGGER_TYPE_HIGH << gpio);
  171. break;
  172. default:
  173. printk(KERN_WARNING "No irq type\n");
  174. spin_unlock_irqrestore(&cchip->lock, flags);
  175. return -EINVAL;
  176. }
  177. __raw_writel(method, cchip->base + GPIO_INTERRUPT_TRIGGER_METHOD);
  178. __raw_writel(edges, cchip->base + GPIO_INTERRUPT_TRIGGER_BOTH_EDGES);
  179. __raw_writel(type, cchip->base + GPIO_INTERRUPT_TRIGGER_TYPE);
  180. spin_unlock_irqrestore(&cchip->lock, flags);
  181. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  182. __irq_set_handler_locked(d->irq, handle_level_irq);
  183. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  184. __irq_set_handler_locked(d->irq, handle_edge_irq);
  185. return 0;
  186. }
  187. void __init cns3xxx_gpio_init(int gpio_base, int ngpio,
  188. u32 base, int irq, int secondary_irq_base)
  189. {
  190. struct cns3xxx_gpio_chip *cchip;
  191. struct irq_chip_generic *gc;
  192. struct irq_chip_type *ct;
  193. char gc_label[16];
  194. int irq_base;
  195. if (cns3xxx_gpio_chip_count == ARRAY_SIZE(cns3xxx_gpio_chips))
  196. return;
  197. snprintf(gc_label, sizeof(gc_label), "cns3xxx_gpio%d",
  198. cns3xxx_gpio_chip_count);
  199. cchip = cns3xxx_gpio_chips + cns3xxx_gpio_chip_count;
  200. cchip->chip.label = kstrdup(gc_label, GFP_KERNEL);
  201. cchip->chip.direction_input = cns3xxx_gpio_direction_input;
  202. cchip->chip.get = cns3xxx_gpio_get;
  203. cchip->chip.direction_output = cns3xxx_gpio_direction_output;
  204. cchip->chip.set = cns3xxx_gpio_set;
  205. cchip->chip.to_irq = cns3xxx_gpio_to_irq;
  206. cchip->chip.base = gpio_base;
  207. cchip->chip.ngpio = ngpio;
  208. cchip->chip.can_sleep = 0;
  209. spin_lock_init(&cchip->lock);
  210. cchip->base = (void __iomem *)base;
  211. BUG_ON(gpiochip_add(&cchip->chip) < 0);
  212. cns3xxx_gpio_chip_count++;
  213. /* clear GPIO interrupts */
  214. __raw_writel(0xffff, cchip->base + GPIO_INTERRUPT_CLEAR);
  215. irq_base = irq_alloc_descs(-1, secondary_irq_base, ngpio,
  216. numa_node_id());
  217. if (irq_base < 0)
  218. goto out_irqdesc_free;
  219. cchip->domain = irq_domain_add_legacy(NULL, ngpio, irq_base, 0,
  220. &irq_domain_simple_ops, NULL);
  221. if (!cchip->domain)
  222. goto out_irqdesc_free;
  223. /*
  224. * IRQ chip init
  225. */
  226. gc = irq_alloc_generic_chip("cns3xxx_gpio_irq", 1, irq_base,
  227. cchip->base, handle_edge_irq);
  228. gc->private = cchip;
  229. ct = gc->chip_types;
  230. ct->type = IRQ_TYPE_EDGE_FALLING;
  231. ct->regs.ack = GPIO_INTERRUPT_CLEAR;
  232. ct->regs.enable = GPIO_INTERRUPT_ENABLE;
  233. ct->chip.irq_ack = irq_gc_ack_set_bit;
  234. ct->chip.irq_enable = irq_gc_unmask_enable_reg;
  235. ct->chip.irq_disable = irq_gc_mask_disable_reg;
  236. ct->chip.irq_set_type = cns3xxx_gpio_irq_set_type;
  237. ct->handler = handle_edge_irq;
  238. irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
  239. IRQ_NOREQUEST, 0);
  240. irq_set_chained_handler(irq, cns3xxx_gpio_irq_handler);
  241. irq_set_handler_data(irq, cchip);
  242. return;
  243. out_irqdesc_free:
  244. irq_free_descs(irq_base, ngpio);
  245. }