gpio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Generic GPIO driver for logic cells found in the Nomadik SoC
  3. *
  4. * Copyright (C) 2008,2009 STMicroelectronics
  5. * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
  6. * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/amba/bus.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/slab.h>
  23. #include <mach/hardware.h>
  24. #include <mach/gpio.h>
  25. /*
  26. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  27. * AMBA device, managing 32 pins and alternate functions. The logic block
  28. * is currently only used in the Nomadik.
  29. *
  30. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  31. */
  32. #define NMK_GPIO_PER_CHIP 32
  33. struct nmk_gpio_chip {
  34. struct gpio_chip chip;
  35. void __iomem *addr;
  36. unsigned int parent_irq;
  37. spinlock_t *lock;
  38. /* Keep track of configured edges */
  39. u32 edge_rising;
  40. u32 edge_falling;
  41. };
  42. /* Mode functions */
  43. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  44. {
  45. struct nmk_gpio_chip *nmk_chip;
  46. unsigned long flags;
  47. u32 afunc, bfunc, bit;
  48. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  49. if (!nmk_chip)
  50. return -EINVAL;
  51. bit = 1 << (gpio - nmk_chip->chip.base);
  52. spin_lock_irqsave(&nmk_chip->lock, flags);
  53. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  54. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  55. if (gpio_mode & NMK_GPIO_ALT_A)
  56. afunc |= bit;
  57. if (gpio_mode & NMK_GPIO_ALT_B)
  58. bfunc |= bit;
  59. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  60. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  61. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(nmk_gpio_set_mode);
  65. int nmk_gpio_get_mode(int gpio)
  66. {
  67. struct nmk_gpio_chip *nmk_chip;
  68. u32 afunc, bfunc, bit;
  69. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  70. if (!nmk_chip)
  71. return -EINVAL;
  72. bit = 1 << (gpio - nmk_chip->chip.base);
  73. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  74. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  75. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  76. }
  77. EXPORT_SYMBOL(nmk_gpio_get_mode);
  78. /* IRQ functions */
  79. static inline int nmk_gpio_get_bitmask(int gpio)
  80. {
  81. return 1 << (gpio % 32);
  82. }
  83. static void nmk_gpio_irq_ack(unsigned int irq)
  84. {
  85. int gpio;
  86. struct nmk_gpio_chip *nmk_chip;
  87. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  88. nmk_chip = get_irq_chip_data(irq);
  89. if (!nmk_chip)
  90. return;
  91. writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
  92. }
  93. static void nmk_gpio_irq_mask(unsigned int irq)
  94. {
  95. int gpio;
  96. struct nmk_gpio_chip *nmk_chip;
  97. unsigned long flags;
  98. u32 bitmask, reg;
  99. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  100. nmk_chip = get_irq_chip_data(irq);
  101. bitmask = nmk_gpio_get_bitmask(gpio);
  102. if (!nmk_chip)
  103. return;
  104. /* we must individually clear the two edges */
  105. spin_lock_irqsave(&nmk_chip->lock, flags);
  106. if (nmk_chip->edge_rising & bitmask) {
  107. reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
  108. reg &= ~bitmask;
  109. writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
  110. }
  111. if (nmk_chip->edge_falling & bitmask) {
  112. reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
  113. reg &= ~bitmask;
  114. writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
  115. }
  116. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  117. };
  118. static void nmk_gpio_irq_unmask(unsigned int irq)
  119. {
  120. int gpio;
  121. struct nmk_gpio_chip *nmk_chip;
  122. unsigned long flags;
  123. u32 bitmask, reg;
  124. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  125. nmk_chip = get_irq_chip_data(irq);
  126. bitmask = nmk_gpio_get_bitmask(gpio);
  127. if (!nmk_chip)
  128. return;
  129. /* we must individually set the two edges */
  130. spin_lock_irqsave(&nmk_chip->lock, flags);
  131. if (nmk_chip->edge_rising & bitmask) {
  132. reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
  133. reg |= bitmask;
  134. writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
  135. }
  136. if (nmk_chip->edge_falling & bitmask) {
  137. reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
  138. reg |= bitmask;
  139. writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
  140. }
  141. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  142. }
  143. static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
  144. {
  145. int gpio;
  146. struct nmk_gpio_chip *nmk_chip;
  147. unsigned long flags;
  148. u32 bitmask;
  149. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  150. nmk_chip = get_irq_chip_data(irq);
  151. bitmask = nmk_gpio_get_bitmask(gpio);
  152. if (!nmk_chip)
  153. return -EINVAL;
  154. if (type & IRQ_TYPE_LEVEL_HIGH)
  155. return -EINVAL;
  156. if (type & IRQ_TYPE_LEVEL_LOW)
  157. return -EINVAL;
  158. spin_lock_irqsave(&nmk_chip->lock, flags);
  159. nmk_chip->edge_rising &= ~bitmask;
  160. if (type & IRQ_TYPE_EDGE_RISING)
  161. nmk_chip->edge_rising |= bitmask;
  162. writel(nmk_chip->edge_rising, nmk_chip->addr + NMK_GPIO_RIMSC);
  163. nmk_chip->edge_falling &= ~bitmask;
  164. if (type & IRQ_TYPE_EDGE_FALLING)
  165. nmk_chip->edge_falling |= bitmask;
  166. writel(nmk_chip->edge_falling, nmk_chip->addr + NMK_GPIO_FIMSC);
  167. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  168. nmk_gpio_irq_unmask(irq);
  169. return 0;
  170. }
  171. static struct irq_chip nmk_gpio_irq_chip = {
  172. .name = "Nomadik-GPIO",
  173. .ack = nmk_gpio_irq_ack,
  174. .mask = nmk_gpio_irq_mask,
  175. .unmask = nmk_gpio_irq_unmask,
  176. .set_type = nmk_gpio_irq_set_type,
  177. };
  178. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  179. {
  180. struct nmk_gpio_chip *nmk_chip;
  181. struct irq_chip *host_chip;
  182. unsigned int gpio_irq;
  183. u32 pending;
  184. unsigned int first_irq;
  185. nmk_chip = get_irq_data(irq);
  186. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  187. while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
  188. gpio_irq = first_irq + __ffs(pending);
  189. generic_handle_irq(gpio_irq);
  190. }
  191. if (0) {/* don't ack parent irq, as ack == disable */
  192. host_chip = get_irq_chip(irq);
  193. host_chip->ack(irq);
  194. }
  195. }
  196. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  197. {
  198. unsigned int first_irq;
  199. int i;
  200. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  201. for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
  202. set_irq_chip(i, &nmk_gpio_irq_chip);
  203. set_irq_handler(i, handle_edge_irq);
  204. set_irq_flags(i, IRQF_VALID);
  205. set_irq_chip_data(i, nmk_chip);
  206. }
  207. set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  208. set_irq_data(nmk_chip->parent_irq, nmk_chip);
  209. return 0;
  210. }
  211. /* I/O Functions */
  212. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  213. {
  214. struct nmk_gpio_chip *nmk_chip =
  215. container_of(chip, struct nmk_gpio_chip, chip);
  216. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  217. return 0;
  218. }
  219. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  220. int val)
  221. {
  222. struct nmk_gpio_chip *nmk_chip =
  223. container_of(chip, struct nmk_gpio_chip, chip);
  224. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  225. return 0;
  226. }
  227. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  228. {
  229. struct nmk_gpio_chip *nmk_chip =
  230. container_of(chip, struct nmk_gpio_chip, chip);
  231. u32 bit = 1 << offset;
  232. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  233. }
  234. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  235. int val)
  236. {
  237. struct nmk_gpio_chip *nmk_chip =
  238. container_of(chip, struct nmk_gpio_chip, chip);
  239. u32 bit = 1 << offset;
  240. if (val)
  241. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  242. else
  243. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  244. }
  245. /* This structure is replicated for each GPIO block allocated at probe time */
  246. static struct gpio_chip nmk_gpio_template = {
  247. .direction_input = nmk_gpio_make_input,
  248. .get = nmk_gpio_get_input,
  249. .direction_output = nmk_gpio_make_output,
  250. .set = nmk_gpio_set_output,
  251. .ngpio = NMK_GPIO_PER_CHIP,
  252. .can_sleep = 0,
  253. };
  254. static int __init nmk_gpio_probe(struct amba_device *dev, struct amba_id *id)
  255. {
  256. struct nmk_gpio_platform_data *pdata;
  257. struct nmk_gpio_chip *nmk_chip;
  258. struct gpio_chip *chip;
  259. int ret;
  260. pdata = dev->dev.platform_data;
  261. ret = amba_request_regions(dev, pdata->name);
  262. if (ret)
  263. return ret;
  264. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  265. if (!nmk_chip) {
  266. ret = -ENOMEM;
  267. goto out_amba;
  268. }
  269. /*
  270. * The virt address in nmk_chip->addr is in the nomadik register space,
  271. * so we can simply convert the resource address, without remapping
  272. */
  273. nmk_chip->addr = io_p2v(dev->res.start);
  274. nmk_chip->chip = nmk_gpio_template;
  275. nmk_chip->parent_irq = pdata->parent_irq;
  276. chip = &nmk_chip->chip;
  277. chip->base = pdata->first_gpio;
  278. chip->label = pdata->name;
  279. chip->dev = &dev->dev;
  280. chip->owner = THIS_MODULE;
  281. ret = gpiochip_add(&nmk_chip->chip);
  282. if (ret)
  283. goto out_free;
  284. amba_set_drvdata(dev, nmk_chip);
  285. nmk_gpio_init_irq(nmk_chip);
  286. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  287. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  288. return 0;
  289. out_free:
  290. kfree(nmk_chip);
  291. out_amba:
  292. amba_release_regions(dev);
  293. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  294. pdata->first_gpio, pdata->first_gpio+31);
  295. return ret;
  296. }
  297. static int nmk_gpio_remove(struct amba_device *dev)
  298. {
  299. struct nmk_gpio_chip *nmk_chip;
  300. nmk_chip = amba_get_drvdata(dev);
  301. gpiochip_remove(&nmk_chip->chip);
  302. kfree(nmk_chip);
  303. amba_release_regions(dev);
  304. return 0;
  305. }
  306. /* We have 0x1f080060 and 0x1f180060, accept both using the mask */
  307. static struct amba_id nmk_gpio_ids[] = {
  308. {
  309. .id = 0x1f080060,
  310. .mask = 0xffefffff,
  311. },
  312. {0, 0},
  313. };
  314. static struct amba_driver nmk_gpio_driver = {
  315. .drv = {
  316. .owner = THIS_MODULE,
  317. .name = "gpio",
  318. },
  319. .probe = nmk_gpio_probe,
  320. .remove = nmk_gpio_remove,
  321. .suspend = NULL, /* to be done */
  322. .resume = NULL,
  323. .id_table = nmk_gpio_ids,
  324. };
  325. static int __init nmk_gpio_init(void)
  326. {
  327. return amba_driver_register(&nmk_gpio_driver);
  328. }
  329. arch_initcall(nmk_gpio_init);
  330. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  331. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  332. MODULE_LICENSE("GPL");