gpio.c 10 KB

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