gpio.c 9.6 KB

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