gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/platform_device.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_modify(struct nmk_gpio_chip *nmk_chip,
  93. int gpio, bool enable)
  94. {
  95. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  96. u32 reg;
  97. /* we must individually set/clear the two edges */
  98. if (nmk_chip->edge_rising & bitmask) {
  99. reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
  100. if (enable)
  101. reg |= bitmask;
  102. else
  103. reg &= ~bitmask;
  104. writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
  105. }
  106. if (nmk_chip->edge_falling & bitmask) {
  107. reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
  108. if (enable)
  109. reg |= bitmask;
  110. else
  111. reg &= ~bitmask;
  112. writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
  113. }
  114. }
  115. static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
  116. {
  117. int gpio;
  118. struct nmk_gpio_chip *nmk_chip;
  119. unsigned long flags;
  120. u32 bitmask;
  121. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  122. nmk_chip = get_irq_chip_data(irq);
  123. bitmask = nmk_gpio_get_bitmask(gpio);
  124. if (!nmk_chip)
  125. return;
  126. spin_lock_irqsave(&nmk_chip->lock, flags);
  127. __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
  128. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  129. }
  130. static void nmk_gpio_irq_mask(unsigned int irq)
  131. {
  132. nmk_gpio_irq_modify(irq, false);
  133. };
  134. static void nmk_gpio_irq_unmask(unsigned int irq)
  135. {
  136. nmk_gpio_irq_modify(irq, true);
  137. }
  138. static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
  139. {
  140. bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
  141. int gpio;
  142. struct nmk_gpio_chip *nmk_chip;
  143. unsigned long flags;
  144. u32 bitmask;
  145. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  146. nmk_chip = get_irq_chip_data(irq);
  147. bitmask = nmk_gpio_get_bitmask(gpio);
  148. if (!nmk_chip)
  149. return -EINVAL;
  150. if (type & IRQ_TYPE_LEVEL_HIGH)
  151. return -EINVAL;
  152. if (type & IRQ_TYPE_LEVEL_LOW)
  153. return -EINVAL;
  154. spin_lock_irqsave(&nmk_chip->lock, flags);
  155. if (enabled)
  156. __nmk_gpio_irq_modify(nmk_chip, gpio, false);
  157. nmk_chip->edge_rising &= ~bitmask;
  158. if (type & IRQ_TYPE_EDGE_RISING)
  159. nmk_chip->edge_rising |= bitmask;
  160. nmk_chip->edge_falling &= ~bitmask;
  161. if (type & IRQ_TYPE_EDGE_FALLING)
  162. nmk_chip->edge_falling |= bitmask;
  163. if (enabled)
  164. __nmk_gpio_irq_modify(nmk_chip, gpio, true);
  165. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  166. return 0;
  167. }
  168. static struct irq_chip nmk_gpio_irq_chip = {
  169. .name = "Nomadik-GPIO",
  170. .ack = nmk_gpio_irq_ack,
  171. .mask = nmk_gpio_irq_mask,
  172. .unmask = nmk_gpio_irq_unmask,
  173. .set_type = nmk_gpio_irq_set_type,
  174. };
  175. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  176. {
  177. struct nmk_gpio_chip *nmk_chip;
  178. struct irq_chip *host_chip = get_irq_chip(irq);
  179. unsigned int gpio_irq;
  180. u32 pending;
  181. unsigned int first_irq;
  182. if (host_chip->mask_ack)
  183. host_chip->mask_ack(irq);
  184. else {
  185. host_chip->mask(irq);
  186. if (host_chip->ack)
  187. host_chip->ack(irq);
  188. }
  189. nmk_chip = get_irq_data(irq);
  190. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  191. while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
  192. gpio_irq = first_irq + __ffs(pending);
  193. generic_handle_irq(gpio_irq);
  194. }
  195. host_chip->unmask(irq);
  196. }
  197. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  198. {
  199. unsigned int first_irq;
  200. int i;
  201. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  202. for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
  203. set_irq_chip(i, &nmk_gpio_irq_chip);
  204. set_irq_handler(i, handle_edge_irq);
  205. set_irq_flags(i, IRQF_VALID);
  206. set_irq_chip_data(i, nmk_chip);
  207. set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  208. }
  209. set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  210. set_irq_data(nmk_chip->parent_irq, nmk_chip);
  211. return 0;
  212. }
  213. /* I/O Functions */
  214. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  215. {
  216. struct nmk_gpio_chip *nmk_chip =
  217. container_of(chip, struct nmk_gpio_chip, chip);
  218. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  219. return 0;
  220. }
  221. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  222. int val)
  223. {
  224. struct nmk_gpio_chip *nmk_chip =
  225. container_of(chip, struct nmk_gpio_chip, chip);
  226. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  227. return 0;
  228. }
  229. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  230. {
  231. struct nmk_gpio_chip *nmk_chip =
  232. container_of(chip, struct nmk_gpio_chip, chip);
  233. u32 bit = 1 << offset;
  234. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  235. }
  236. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  237. int val)
  238. {
  239. struct nmk_gpio_chip *nmk_chip =
  240. container_of(chip, struct nmk_gpio_chip, chip);
  241. u32 bit = 1 << offset;
  242. if (val)
  243. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  244. else
  245. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  246. }
  247. /* This structure is replicated for each GPIO block allocated at probe time */
  248. static struct gpio_chip nmk_gpio_template = {
  249. .direction_input = nmk_gpio_make_input,
  250. .get = nmk_gpio_get_input,
  251. .direction_output = nmk_gpio_make_output,
  252. .set = nmk_gpio_set_output,
  253. .ngpio = NMK_GPIO_PER_CHIP,
  254. .can_sleep = 0,
  255. };
  256. static int __init nmk_gpio_probe(struct platform_device *dev)
  257. {
  258. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  259. struct nmk_gpio_chip *nmk_chip;
  260. struct gpio_chip *chip;
  261. struct resource *res;
  262. int irq;
  263. int ret;
  264. if (!pdata)
  265. return -ENODEV;
  266. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  267. if (!res) {
  268. ret = -ENOENT;
  269. goto out;
  270. }
  271. irq = platform_get_irq(dev, 0);
  272. if (irq < 0) {
  273. ret = irq;
  274. goto out;
  275. }
  276. if (request_mem_region(res->start, resource_size(res),
  277. dev_name(&dev->dev)) == NULL) {
  278. ret = -EBUSY;
  279. goto out;
  280. }
  281. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  282. if (!nmk_chip) {
  283. ret = -ENOMEM;
  284. goto out_release;
  285. }
  286. /*
  287. * The virt address in nmk_chip->addr is in the nomadik register space,
  288. * so we can simply convert the resource address, without remapping
  289. */
  290. nmk_chip->addr = io_p2v(res->start);
  291. nmk_chip->chip = nmk_gpio_template;
  292. nmk_chip->parent_irq = irq;
  293. spin_lock_init(&nmk_chip->lock);
  294. chip = &nmk_chip->chip;
  295. chip->base = pdata->first_gpio;
  296. chip->label = pdata->name;
  297. chip->dev = &dev->dev;
  298. chip->owner = THIS_MODULE;
  299. ret = gpiochip_add(&nmk_chip->chip);
  300. if (ret)
  301. goto out_free;
  302. platform_set_drvdata(dev, nmk_chip);
  303. nmk_gpio_init_irq(nmk_chip);
  304. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  305. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  306. return 0;
  307. out_free:
  308. kfree(nmk_chip);
  309. out_release:
  310. release_mem_region(res->start, resource_size(res));
  311. out:
  312. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  313. pdata->first_gpio, pdata->first_gpio+31);
  314. return ret;
  315. }
  316. static int __exit nmk_gpio_remove(struct platform_device *dev)
  317. {
  318. struct nmk_gpio_chip *nmk_chip;
  319. struct resource *res;
  320. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  321. nmk_chip = platform_get_drvdata(dev);
  322. gpiochip_remove(&nmk_chip->chip);
  323. kfree(nmk_chip);
  324. release_mem_region(res->start, resource_size(res));
  325. return 0;
  326. }
  327. static struct platform_driver nmk_gpio_driver = {
  328. .driver = {
  329. .owner = THIS_MODULE,
  330. .name = "gpio",
  331. },
  332. .probe = nmk_gpio_probe,
  333. .remove = __exit_p(nmk_gpio_remove),
  334. .suspend = NULL, /* to be done */
  335. .resume = NULL,
  336. };
  337. static int __init nmk_gpio_init(void)
  338. {
  339. return platform_driver_register(&nmk_gpio_driver);
  340. }
  341. arch_initcall(nmk_gpio_init);
  342. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  343. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  344. MODULE_LICENSE("GPL");