gpio-pl061.c 9.9 KB

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