gpio-pl061.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2008, 2009 Provigent Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
  9. *
  10. * Data sheet: ARM DDI 0190B, September 2000
  11. */
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqchip/chained_irq.h>
  19. #include <linux/bitops.h>
  20. #include <linux/gpio.h>
  21. #include <linux/device.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/amba/pl061.h>
  24. #include <linux/slab.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/pm.h>
  27. #define GPIODIR 0x400
  28. #define GPIOIS 0x404
  29. #define GPIOIBE 0x408
  30. #define GPIOIEV 0x40C
  31. #define GPIOIE 0x410
  32. #define GPIORIS 0x414
  33. #define GPIOMIS 0x418
  34. #define GPIOIC 0x41C
  35. #define PL061_GPIO_NR 8
  36. #ifdef CONFIG_PM
  37. struct pl061_context_save_regs {
  38. u8 gpio_data;
  39. u8 gpio_dir;
  40. u8 gpio_is;
  41. u8 gpio_ibe;
  42. u8 gpio_iev;
  43. u8 gpio_ie;
  44. };
  45. #endif
  46. struct pl061_gpio {
  47. spinlock_t lock;
  48. void __iomem *base;
  49. struct gpio_chip gc;
  50. #ifdef CONFIG_PM
  51. struct pl061_context_save_regs csave_regs;
  52. #endif
  53. };
  54. static int pl061_gpio_request(struct gpio_chip *chip, unsigned offset)
  55. {
  56. /*
  57. * Map back to global GPIO space and request muxing, the direction
  58. * parameter does not matter for this controller.
  59. */
  60. int gpio = chip->base + offset;
  61. return pinctrl_request_gpio(gpio);
  62. }
  63. static void pl061_gpio_free(struct gpio_chip *chip, unsigned offset)
  64. {
  65. int gpio = chip->base + offset;
  66. pinctrl_free_gpio(gpio);
  67. }
  68. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  69. {
  70. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  71. unsigned long flags;
  72. unsigned char gpiodir;
  73. if (offset >= gc->ngpio)
  74. return -EINVAL;
  75. spin_lock_irqsave(&chip->lock, flags);
  76. gpiodir = readb(chip->base + GPIODIR);
  77. gpiodir &= ~(BIT(offset));
  78. writeb(gpiodir, chip->base + GPIODIR);
  79. spin_unlock_irqrestore(&chip->lock, flags);
  80. return 0;
  81. }
  82. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  83. int value)
  84. {
  85. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  86. unsigned long flags;
  87. unsigned char gpiodir;
  88. if (offset >= gc->ngpio)
  89. return -EINVAL;
  90. spin_lock_irqsave(&chip->lock, flags);
  91. writeb(!!value << offset, chip->base + (BIT(offset + 2)));
  92. gpiodir = readb(chip->base + GPIODIR);
  93. gpiodir |= BIT(offset);
  94. writeb(gpiodir, chip->base + GPIODIR);
  95. /*
  96. * gpio value is set again, because pl061 doesn't allow to set value of
  97. * a gpio pin before configuring it in OUT mode.
  98. */
  99. writeb(!!value << offset, chip->base + (BIT(offset + 2)));
  100. spin_unlock_irqrestore(&chip->lock, flags);
  101. return 0;
  102. }
  103. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  104. {
  105. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  106. return !!readb(chip->base + (BIT(offset + 2)));
  107. }
  108. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  109. {
  110. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  111. writeb(!!value << offset, chip->base + (BIT(offset + 2)));
  112. }
  113. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  114. {
  115. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  116. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  117. int offset = irqd_to_hwirq(d);
  118. unsigned long flags;
  119. u8 gpiois, gpioibe, gpioiev;
  120. u8 bit = BIT(offset);
  121. if (offset < 0 || offset >= PL061_GPIO_NR)
  122. return -EINVAL;
  123. spin_lock_irqsave(&chip->lock, flags);
  124. gpioiev = readb(chip->base + GPIOIEV);
  125. gpiois = readb(chip->base + GPIOIS);
  126. gpioibe = readb(chip->base + GPIOIBE);
  127. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  128. gpiois |= bit;
  129. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  130. gpioiev |= bit;
  131. else
  132. gpioiev &= ~bit;
  133. } else
  134. gpiois &= ~bit;
  135. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  136. /* Setting this makes GPIOEV be ignored */
  137. gpioibe |= bit;
  138. else {
  139. gpioibe &= ~bit;
  140. if (trigger & IRQ_TYPE_EDGE_RISING)
  141. gpioiev |= bit;
  142. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  143. gpioiev &= ~bit;
  144. }
  145. writeb(gpiois, chip->base + GPIOIS);
  146. writeb(gpioibe, chip->base + GPIOIBE);
  147. writeb(gpioiev, chip->base + GPIOIEV);
  148. spin_unlock_irqrestore(&chip->lock, flags);
  149. return 0;
  150. }
  151. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  152. {
  153. unsigned long pending;
  154. int offset;
  155. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  156. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  157. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  158. chained_irq_enter(irqchip, desc);
  159. pending = readb(chip->base + GPIOMIS);
  160. writeb(pending, chip->base + GPIOIC);
  161. if (pending) {
  162. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  163. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  164. offset));
  165. }
  166. chained_irq_exit(irqchip, desc);
  167. }
  168. static void pl061_irq_mask(struct irq_data *d)
  169. {
  170. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  171. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  172. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  173. u8 gpioie;
  174. spin_lock(&chip->lock);
  175. gpioie = readb(chip->base + GPIOIE) & ~mask;
  176. writeb(gpioie, chip->base + GPIOIE);
  177. spin_unlock(&chip->lock);
  178. }
  179. static void pl061_irq_unmask(struct irq_data *d)
  180. {
  181. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  182. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  183. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  184. u8 gpioie;
  185. spin_lock(&chip->lock);
  186. gpioie = readb(chip->base + GPIOIE) | mask;
  187. writeb(gpioie, chip->base + GPIOIE);
  188. spin_unlock(&chip->lock);
  189. }
  190. static struct irq_chip pl061_irqchip = {
  191. .name = "pl061",
  192. .irq_mask = pl061_irq_mask,
  193. .irq_unmask = pl061_irq_unmask,
  194. .irq_set_type = pl061_irq_type,
  195. };
  196. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  197. {
  198. struct device *dev = &adev->dev;
  199. struct pl061_platform_data *pdata = dev_get_platdata(dev);
  200. struct pl061_gpio *chip;
  201. int ret, irq, i, irq_base;
  202. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  203. if (chip == NULL)
  204. return -ENOMEM;
  205. if (pdata) {
  206. chip->gc.base = pdata->gpio_base;
  207. irq_base = pdata->irq_base;
  208. if (irq_base <= 0) {
  209. dev_err(&adev->dev, "invalid IRQ base in pdata\n");
  210. return -ENODEV;
  211. }
  212. } else {
  213. chip->gc.base = -1;
  214. irq_base = 0;
  215. }
  216. chip->base = devm_ioremap_resource(dev, &adev->res);
  217. if (IS_ERR(chip->base))
  218. return PTR_ERR(chip->base);
  219. spin_lock_init(&chip->lock);
  220. chip->gc.request = pl061_gpio_request;
  221. chip->gc.free = pl061_gpio_free;
  222. chip->gc.direction_input = pl061_direction_input;
  223. chip->gc.direction_output = pl061_direction_output;
  224. chip->gc.get = pl061_get_value;
  225. chip->gc.set = pl061_set_value;
  226. chip->gc.ngpio = PL061_GPIO_NR;
  227. chip->gc.label = dev_name(dev);
  228. chip->gc.dev = dev;
  229. chip->gc.owner = THIS_MODULE;
  230. ret = gpiochip_add(&chip->gc);
  231. if (ret)
  232. return ret;
  233. /*
  234. * irq_chip support
  235. */
  236. writeb(0, chip->base + GPIOIE); /* disable irqs */
  237. irq = adev->irq[0];
  238. if (irq < 0) {
  239. dev_err(&adev->dev, "invalid IRQ\n");
  240. return -ENODEV;
  241. }
  242. ret = gpiochip_irqchip_add(&chip->gc, &pl061_irqchip,
  243. irq_base, handle_simple_irq,
  244. IRQ_TYPE_NONE);
  245. if (ret) {
  246. dev_info(&adev->dev, "could not add irqchip\n");
  247. return ret;
  248. }
  249. gpiochip_set_chained_irqchip(&chip->gc, &pl061_irqchip,
  250. irq, pl061_irq_handler);
  251. for (i = 0; i < PL061_GPIO_NR; i++) {
  252. if (pdata) {
  253. if (pdata->directions & (BIT(i)))
  254. pl061_direction_output(&chip->gc, i,
  255. pdata->values & (BIT(i)));
  256. else
  257. pl061_direction_input(&chip->gc, i);
  258. }
  259. }
  260. amba_set_drvdata(adev, chip);
  261. dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
  262. &adev->res.start);
  263. return 0;
  264. }
  265. #ifdef CONFIG_PM
  266. static int pl061_suspend(struct device *dev)
  267. {
  268. struct pl061_gpio *chip = dev_get_drvdata(dev);
  269. int offset;
  270. chip->csave_regs.gpio_data = 0;
  271. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  272. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  273. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  274. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  275. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  276. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  277. if (chip->csave_regs.gpio_dir & (BIT(offset)))
  278. chip->csave_regs.gpio_data |=
  279. pl061_get_value(&chip->gc, offset) << offset;
  280. }
  281. return 0;
  282. }
  283. static int pl061_resume(struct device *dev)
  284. {
  285. struct pl061_gpio *chip = dev_get_drvdata(dev);
  286. int offset;
  287. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  288. if (chip->csave_regs.gpio_dir & (BIT(offset)))
  289. pl061_direction_output(&chip->gc, offset,
  290. chip->csave_regs.gpio_data &
  291. (BIT(offset)));
  292. else
  293. pl061_direction_input(&chip->gc, offset);
  294. }
  295. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  296. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  297. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  298. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  299. return 0;
  300. }
  301. static const struct dev_pm_ops pl061_dev_pm_ops = {
  302. .suspend = pl061_suspend,
  303. .resume = pl061_resume,
  304. .freeze = pl061_suspend,
  305. .restore = pl061_resume,
  306. };
  307. #endif
  308. static struct amba_id pl061_ids[] = {
  309. {
  310. .id = 0x00041061,
  311. .mask = 0x000fffff,
  312. },
  313. { 0, 0 },
  314. };
  315. MODULE_DEVICE_TABLE(amba, pl061_ids);
  316. static struct amba_driver pl061_gpio_driver = {
  317. .drv = {
  318. .name = "pl061_gpio",
  319. #ifdef CONFIG_PM
  320. .pm = &pl061_dev_pm_ops,
  321. #endif
  322. },
  323. .id_table = pl061_ids,
  324. .probe = pl061_probe,
  325. };
  326. static int __init pl061_gpio_init(void)
  327. {
  328. return amba_driver_register(&pl061_gpio_driver);
  329. }
  330. module_init(pl061_gpio_init);
  331. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  332. MODULE_DESCRIPTION("PL061 GPIO driver");
  333. MODULE_LICENSE("GPL");