gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_get_input(struct gpio_chip *chip, unsigned offset)
  226. {
  227. struct nmk_gpio_chip *nmk_chip =
  228. container_of(chip, struct nmk_gpio_chip, chip);
  229. u32 bit = 1 << offset;
  230. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  231. }
  232. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  233. int val)
  234. {
  235. struct nmk_gpio_chip *nmk_chip =
  236. container_of(chip, struct nmk_gpio_chip, chip);
  237. u32 bit = 1 << offset;
  238. if (val)
  239. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  240. else
  241. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  242. }
  243. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  244. int val)
  245. {
  246. struct nmk_gpio_chip *nmk_chip =
  247. container_of(chip, struct nmk_gpio_chip, chip);
  248. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  249. nmk_gpio_set_output(chip, offset, val);
  250. return 0;
  251. }
  252. /* This structure is replicated for each GPIO block allocated at probe time */
  253. static struct gpio_chip nmk_gpio_template = {
  254. .direction_input = nmk_gpio_make_input,
  255. .get = nmk_gpio_get_input,
  256. .direction_output = nmk_gpio_make_output,
  257. .set = nmk_gpio_set_output,
  258. .ngpio = NMK_GPIO_PER_CHIP,
  259. .can_sleep = 0,
  260. };
  261. static int __init nmk_gpio_probe(struct platform_device *dev)
  262. {
  263. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  264. struct nmk_gpio_chip *nmk_chip;
  265. struct gpio_chip *chip;
  266. struct resource *res;
  267. struct clk *clk;
  268. int irq;
  269. int ret;
  270. if (!pdata)
  271. return -ENODEV;
  272. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  273. if (!res) {
  274. ret = -ENOENT;
  275. goto out;
  276. }
  277. irq = platform_get_irq(dev, 0);
  278. if (irq < 0) {
  279. ret = irq;
  280. goto out;
  281. }
  282. if (request_mem_region(res->start, resource_size(res),
  283. dev_name(&dev->dev)) == NULL) {
  284. ret = -EBUSY;
  285. goto out;
  286. }
  287. clk = clk_get(&dev->dev, NULL);
  288. if (IS_ERR(clk)) {
  289. ret = PTR_ERR(clk);
  290. goto out_release;
  291. }
  292. clk_enable(clk);
  293. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  294. if (!nmk_chip) {
  295. ret = -ENOMEM;
  296. goto out_clk;
  297. }
  298. /*
  299. * The virt address in nmk_chip->addr is in the nomadik register space,
  300. * so we can simply convert the resource address, without remapping
  301. */
  302. nmk_chip->clk = clk;
  303. nmk_chip->addr = io_p2v(res->start);
  304. nmk_chip->chip = nmk_gpio_template;
  305. nmk_chip->parent_irq = irq;
  306. spin_lock_init(&nmk_chip->lock);
  307. chip = &nmk_chip->chip;
  308. chip->base = pdata->first_gpio;
  309. chip->label = pdata->name;
  310. chip->dev = &dev->dev;
  311. chip->owner = THIS_MODULE;
  312. ret = gpiochip_add(&nmk_chip->chip);
  313. if (ret)
  314. goto out_free;
  315. platform_set_drvdata(dev, nmk_chip);
  316. nmk_gpio_init_irq(nmk_chip);
  317. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  318. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  319. return 0;
  320. out_free:
  321. kfree(nmk_chip);
  322. out_clk:
  323. clk_disable(clk);
  324. clk_put(clk);
  325. out_release:
  326. release_mem_region(res->start, resource_size(res));
  327. out:
  328. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  329. pdata->first_gpio, pdata->first_gpio+31);
  330. return ret;
  331. }
  332. static int __exit nmk_gpio_remove(struct platform_device *dev)
  333. {
  334. struct nmk_gpio_chip *nmk_chip;
  335. struct resource *res;
  336. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  337. nmk_chip = platform_get_drvdata(dev);
  338. gpiochip_remove(&nmk_chip->chip);
  339. clk_disable(nmk_chip->clk);
  340. clk_put(nmk_chip->clk);
  341. kfree(nmk_chip);
  342. release_mem_region(res->start, resource_size(res));
  343. return 0;
  344. }
  345. static struct platform_driver nmk_gpio_driver = {
  346. .driver = {
  347. .owner = THIS_MODULE,
  348. .name = "gpio",
  349. },
  350. .probe = nmk_gpio_probe,
  351. .remove = __exit_p(nmk_gpio_remove),
  352. .suspend = NULL, /* to be done */
  353. .resume = NULL,
  354. };
  355. static int __init nmk_gpio_init(void)
  356. {
  357. return platform_driver_register(&nmk_gpio_driver);
  358. }
  359. arch_initcall(nmk_gpio_init);
  360. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  361. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  362. MODULE_LICENSE("GPL");