gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * arch/arm/plat-orion/gpio.c
  3. *
  4. * Marvell Orion SoC GPIO handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/bitops.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. /*
  19. * GPIO unit register offsets.
  20. */
  21. #define GPIO_OUT_OFF 0x0000
  22. #define GPIO_IO_CONF_OFF 0x0004
  23. #define GPIO_BLINK_EN_OFF 0x0008
  24. #define GPIO_IN_POL_OFF 0x000c
  25. #define GPIO_DATA_IN_OFF 0x0010
  26. #define GPIO_EDGE_CAUSE_OFF 0x0014
  27. #define GPIO_EDGE_MASK_OFF 0x0018
  28. #define GPIO_LEVEL_MASK_OFF 0x001c
  29. struct orion_gpio_chip {
  30. struct gpio_chip chip;
  31. spinlock_t lock;
  32. void __iomem *base;
  33. unsigned long valid_input;
  34. unsigned long valid_output;
  35. int mask_offset;
  36. int secondary_irq_base;
  37. };
  38. static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
  39. {
  40. return ochip->base + GPIO_OUT_OFF;
  41. }
  42. static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
  43. {
  44. return ochip->base + GPIO_IO_CONF_OFF;
  45. }
  46. static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
  47. {
  48. return ochip->base + GPIO_BLINK_EN_OFF;
  49. }
  50. static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
  51. {
  52. return ochip->base + GPIO_IN_POL_OFF;
  53. }
  54. static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
  55. {
  56. return ochip->base + GPIO_DATA_IN_OFF;
  57. }
  58. static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
  59. {
  60. return ochip->base + GPIO_EDGE_CAUSE_OFF;
  61. }
  62. static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
  63. {
  64. return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
  65. }
  66. static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
  67. {
  68. return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
  69. }
  70. static struct orion_gpio_chip orion_gpio_chips[2];
  71. static int orion_gpio_chip_count;
  72. static inline void
  73. __set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
  74. {
  75. u32 u;
  76. u = readl(GPIO_IO_CONF(ochip));
  77. if (input)
  78. u |= 1 << pin;
  79. else
  80. u &= ~(1 << pin);
  81. writel(u, GPIO_IO_CONF(ochip));
  82. }
  83. static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
  84. {
  85. u32 u;
  86. u = readl(GPIO_OUT(ochip));
  87. if (high)
  88. u |= 1 << pin;
  89. else
  90. u &= ~(1 << pin);
  91. writel(u, GPIO_OUT(ochip));
  92. }
  93. static inline void
  94. __set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
  95. {
  96. u32 u;
  97. u = readl(GPIO_BLINK_EN(ochip));
  98. if (blink)
  99. u |= 1 << pin;
  100. else
  101. u &= ~(1 << pin);
  102. writel(u, GPIO_BLINK_EN(ochip));
  103. }
  104. static inline int
  105. orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
  106. {
  107. if (pin >= ochip->chip.ngpio)
  108. goto err_out;
  109. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
  110. goto err_out;
  111. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
  112. goto err_out;
  113. return 1;
  114. err_out:
  115. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  116. return false;
  117. }
  118. /*
  119. * GENERIC_GPIO primitives.
  120. */
  121. static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
  122. {
  123. struct orion_gpio_chip *ochip =
  124. container_of(chip, struct orion_gpio_chip, chip);
  125. if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
  126. orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  127. return 0;
  128. return -EINVAL;
  129. }
  130. static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  131. {
  132. struct orion_gpio_chip *ochip =
  133. container_of(chip, struct orion_gpio_chip, chip);
  134. unsigned long flags;
  135. if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
  136. return -EINVAL;
  137. spin_lock_irqsave(&ochip->lock, flags);
  138. __set_direction(ochip, pin, 1);
  139. spin_unlock_irqrestore(&ochip->lock, flags);
  140. return 0;
  141. }
  142. static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
  143. {
  144. struct orion_gpio_chip *ochip =
  145. container_of(chip, struct orion_gpio_chip, chip);
  146. int val;
  147. if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
  148. val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
  149. } else {
  150. val = readl(GPIO_OUT(ochip));
  151. }
  152. return (val >> pin) & 1;
  153. }
  154. static int
  155. orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
  156. {
  157. struct orion_gpio_chip *ochip =
  158. container_of(chip, struct orion_gpio_chip, chip);
  159. unsigned long flags;
  160. if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  161. return -EINVAL;
  162. spin_lock_irqsave(&ochip->lock, flags);
  163. __set_blinking(ochip, pin, 0);
  164. __set_level(ochip, pin, value);
  165. __set_direction(ochip, pin, 0);
  166. spin_unlock_irqrestore(&ochip->lock, flags);
  167. return 0;
  168. }
  169. static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  170. {
  171. struct orion_gpio_chip *ochip =
  172. container_of(chip, struct orion_gpio_chip, chip);
  173. unsigned long flags;
  174. spin_lock_irqsave(&ochip->lock, flags);
  175. __set_level(ochip, pin, value);
  176. spin_unlock_irqrestore(&ochip->lock, flags);
  177. }
  178. static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  179. {
  180. struct orion_gpio_chip *ochip =
  181. container_of(chip, struct orion_gpio_chip, chip);
  182. return ochip->secondary_irq_base + pin;
  183. }
  184. /*
  185. * Orion-specific GPIO API extensions.
  186. */
  187. static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
  188. {
  189. int i;
  190. for (i = 0; i < orion_gpio_chip_count; i++) {
  191. struct orion_gpio_chip *ochip = orion_gpio_chips + i;
  192. struct gpio_chip *chip = &ochip->chip;
  193. if (pin >= chip->base && pin < chip->base + chip->ngpio)
  194. return ochip;
  195. }
  196. return NULL;
  197. }
  198. void __init orion_gpio_set_unused(unsigned pin)
  199. {
  200. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  201. if (ochip == NULL)
  202. return;
  203. pin -= ochip->chip.base;
  204. /* Configure as output, drive low. */
  205. __set_level(ochip, pin, 0);
  206. __set_direction(ochip, pin, 0);
  207. }
  208. void __init orion_gpio_set_valid(unsigned pin, int mode)
  209. {
  210. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  211. if (ochip == NULL)
  212. return;
  213. pin -= ochip->chip.base;
  214. if (mode == 1)
  215. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  216. if (mode & GPIO_INPUT_OK)
  217. __set_bit(pin, &ochip->valid_input);
  218. else
  219. __clear_bit(pin, &ochip->valid_input);
  220. if (mode & GPIO_OUTPUT_OK)
  221. __set_bit(pin, &ochip->valid_output);
  222. else
  223. __clear_bit(pin, &ochip->valid_output);
  224. }
  225. void orion_gpio_set_blink(unsigned pin, int blink)
  226. {
  227. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  228. unsigned long flags;
  229. if (ochip == NULL)
  230. return;
  231. spin_lock_irqsave(&ochip->lock, flags);
  232. __set_level(ochip, pin, 0);
  233. __set_blinking(ochip, pin, blink);
  234. spin_unlock_irqrestore(&ochip->lock, flags);
  235. }
  236. EXPORT_SYMBOL(orion_gpio_set_blink);
  237. /*****************************************************************************
  238. * Orion GPIO IRQ
  239. *
  240. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  241. * value of the line or the opposite value.
  242. *
  243. * Level IRQ handlers: DATA_IN is used directly as cause register.
  244. * Interrupt are masked by LEVEL_MASK registers.
  245. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  246. * Interrupt are masked by EDGE_MASK registers.
  247. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  248. * the polarity to catch the next line transaction.
  249. * This is a race condition that might not perfectly
  250. * work on some use cases.
  251. *
  252. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  253. * cause register.
  254. *
  255. * EDGE cause mask
  256. * data-in /--------| |-----| |----\
  257. * -----| |----- ---- to main cause reg
  258. * X \----------------| |----/
  259. * polarity LEVEL mask
  260. *
  261. ****************************************************************************/
  262. static int gpio_irq_set_type(struct irq_data *d, u32 type)
  263. {
  264. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  265. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  266. struct orion_gpio_chip *ochip = gc->private;
  267. int pin;
  268. u32 u;
  269. pin = d->irq - gc->irq_base;
  270. u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
  271. if (!u) {
  272. printk(KERN_ERR "orion gpio_irq_set_type failed "
  273. "(irq %d, pin %d).\n", d->irq, pin);
  274. return -EINVAL;
  275. }
  276. type &= IRQ_TYPE_SENSE_MASK;
  277. if (type == IRQ_TYPE_NONE)
  278. return -EINVAL;
  279. /* Check if we need to change chip and handler */
  280. if (!(ct->type & type))
  281. if (irq_setup_alt_chip(d, type))
  282. return -EINVAL;
  283. /*
  284. * Configure interrupt polarity.
  285. */
  286. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
  287. u = readl(GPIO_IN_POL(ochip));
  288. u &= ~(1 << pin);
  289. writel(u, GPIO_IN_POL(ochip));
  290. } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
  291. u = readl(GPIO_IN_POL(ochip));
  292. u |= 1 << pin;
  293. writel(u, GPIO_IN_POL(ochip));
  294. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  295. u32 v;
  296. v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
  297. /*
  298. * set initial polarity based on current input level
  299. */
  300. u = readl(GPIO_IN_POL(ochip));
  301. if (v & (1 << pin))
  302. u |= 1 << pin; /* falling */
  303. else
  304. u &= ~(1 << pin); /* rising */
  305. writel(u, GPIO_IN_POL(ochip));
  306. }
  307. return 0;
  308. }
  309. void __init orion_gpio_init(int gpio_base, int ngpio,
  310. u32 base, int mask_offset, int secondary_irq_base)
  311. {
  312. struct orion_gpio_chip *ochip;
  313. struct irq_chip_generic *gc;
  314. struct irq_chip_type *ct;
  315. char gc_label[16];
  316. if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
  317. return;
  318. snprintf(gc_label, sizeof(gc_label), "orion_gpio%d",
  319. orion_gpio_chip_count);
  320. ochip = orion_gpio_chips + orion_gpio_chip_count;
  321. ochip->chip.label = kstrdup(gc_label, GFP_KERNEL);
  322. ochip->chip.request = orion_gpio_request;
  323. ochip->chip.direction_input = orion_gpio_direction_input;
  324. ochip->chip.get = orion_gpio_get;
  325. ochip->chip.direction_output = orion_gpio_direction_output;
  326. ochip->chip.set = orion_gpio_set;
  327. ochip->chip.to_irq = orion_gpio_to_irq;
  328. ochip->chip.base = gpio_base;
  329. ochip->chip.ngpio = ngpio;
  330. ochip->chip.can_sleep = 0;
  331. spin_lock_init(&ochip->lock);
  332. ochip->base = (void __iomem *)base;
  333. ochip->valid_input = 0;
  334. ochip->valid_output = 0;
  335. ochip->mask_offset = mask_offset;
  336. ochip->secondary_irq_base = secondary_irq_base;
  337. gpiochip_add(&ochip->chip);
  338. orion_gpio_chip_count++;
  339. /*
  340. * Mask and clear GPIO interrupts.
  341. */
  342. writel(0, GPIO_EDGE_CAUSE(ochip));
  343. writel(0, GPIO_EDGE_MASK(ochip));
  344. writel(0, GPIO_LEVEL_MASK(ochip));
  345. gc = irq_alloc_generic_chip("orion_gpio_irq", 2, secondary_irq_base,
  346. ochip->base, handle_level_irq);
  347. gc->private = ochip;
  348. ct = gc->chip_types;
  349. ct->regs.mask = ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
  350. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  351. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  352. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  353. ct->chip.irq_set_type = gpio_irq_set_type;
  354. ct++;
  355. ct->regs.mask = ochip->mask_offset + GPIO_EDGE_MASK_OFF;
  356. ct->regs.ack = GPIO_EDGE_CAUSE_OFF;
  357. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  358. ct->chip.irq_ack = irq_gc_ack_clr_bit;
  359. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  360. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  361. ct->chip.irq_set_type = gpio_irq_set_type;
  362. ct->handler = handle_edge_irq;
  363. irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
  364. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  365. }
  366. void orion_gpio_irq_handler(int pinoff)
  367. {
  368. struct orion_gpio_chip *ochip;
  369. u32 cause, type;
  370. int i;
  371. ochip = orion_gpio_chip_find(pinoff);
  372. if (ochip == NULL)
  373. return;
  374. cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
  375. cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
  376. for (i = 0; i < ochip->chip.ngpio; i++) {
  377. int irq;
  378. irq = ochip->secondary_irq_base + i;
  379. if (!(cause & (1 << i)))
  380. continue;
  381. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  382. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  383. /* Swap polarity (race with GPIO line) */
  384. u32 polarity;
  385. polarity = readl(GPIO_IN_POL(ochip));
  386. polarity ^= 1 << i;
  387. writel(polarity, GPIO_IN_POL(ochip));
  388. }
  389. generic_handle_irq(irq);
  390. }
  391. }