gpio.c 10 KB

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