gpio-grgpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Driver for Aeroflex Gaisler GRGPIO General Purpose I/O cores.
  3. *
  4. * 2013 (c) Aeroflex Gaisler AB
  5. *
  6. * This driver supports the GRGPIO GPIO core available in the GRLIB VHDL
  7. * IP core library.
  8. *
  9. * Full documentation of the GRGPIO core can be found here:
  10. * http://www.gaisler.com/products/grlib/grip.pdf
  11. *
  12. * See "Documentation/devicetree/bindings/gpio/gpio-grgpio.txt" for
  13. * information on open firmware properties.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * Contributors: Andreas Larsson <andreas@gaisler.com>
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/io.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/gpio.h>
  31. #include <linux/slab.h>
  32. #include <linux/err.h>
  33. #include <linux/gpio/driver.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/irq.h>
  36. #include <linux/irqdomain.h>
  37. #define GRGPIO_MAX_NGPIO 32
  38. #define GRGPIO_DATA 0x00
  39. #define GRGPIO_OUTPUT 0x04
  40. #define GRGPIO_DIR 0x08
  41. #define GRGPIO_IMASK 0x0c
  42. #define GRGPIO_IPOL 0x10
  43. #define GRGPIO_IEDGE 0x14
  44. #define GRGPIO_BYPASS 0x18
  45. #define GRGPIO_IMAP_BASE 0x20
  46. /* Structure for an irq of the core - called an underlying irq */
  47. struct grgpio_uirq {
  48. u8 refcnt; /* Reference counter to manage requesting/freeing of uirq */
  49. u8 uirq; /* Underlying irq of the gpio driver */
  50. };
  51. /*
  52. * Structure for an irq of a gpio line handed out by this driver. The index is
  53. * used to map to the corresponding underlying irq.
  54. */
  55. struct grgpio_lirq {
  56. s8 index; /* Index into struct grgpio_priv's uirqs, or -1 */
  57. u8 irq; /* irq for the gpio line */
  58. };
  59. struct grgpio_priv {
  60. struct gpio_chip gc;
  61. void __iomem *regs;
  62. struct device *dev;
  63. u32 imask; /* irq mask shadow register */
  64. /*
  65. * The grgpio core can have multiple "underlying" irqs. The gpio lines
  66. * can be mapped to any one or none of these underlying irqs
  67. * independently of each other. This driver sets up an irq domain and
  68. * hands out separate irqs to each gpio line
  69. */
  70. struct irq_domain *domain;
  71. /*
  72. * This array contains information on each underlying irq, each
  73. * irq of the grgpio core itself.
  74. */
  75. struct grgpio_uirq uirqs[GRGPIO_MAX_NGPIO];
  76. /*
  77. * This array contains information for each gpio line on the irqs
  78. * obtains from this driver. An index value of -1 for a certain gpio
  79. * line indicates that the line has no irq. Otherwise the index connects
  80. * the irq to the underlying irq by pointing into the uirqs array.
  81. */
  82. struct grgpio_lirq lirqs[GRGPIO_MAX_NGPIO];
  83. };
  84. static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
  85. int val)
  86. {
  87. struct gpio_chip *gc = &priv->gc;
  88. unsigned long mask = gc->pin2mask(gc, offset);
  89. if (val)
  90. priv->imask |= mask;
  91. else
  92. priv->imask &= ~mask;
  93. gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
  94. }
  95. static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
  96. {
  97. struct grgpio_priv *priv = gpiochip_get_data(gc);
  98. if (offset >= gc->ngpio)
  99. return -ENXIO;
  100. if (priv->lirqs[offset].index < 0)
  101. return -ENXIO;
  102. return irq_create_mapping(priv->domain, offset);
  103. }
  104. /* -------------------- IRQ chip functions -------------------- */
  105. static int grgpio_irq_set_type(struct irq_data *d, unsigned int type)
  106. {
  107. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  108. unsigned long flags;
  109. u32 mask = BIT(d->hwirq);
  110. u32 ipol;
  111. u32 iedge;
  112. u32 pol;
  113. u32 edge;
  114. switch (type) {
  115. case IRQ_TYPE_LEVEL_LOW:
  116. pol = 0;
  117. edge = 0;
  118. break;
  119. case IRQ_TYPE_LEVEL_HIGH:
  120. pol = mask;
  121. edge = 0;
  122. break;
  123. case IRQ_TYPE_EDGE_FALLING:
  124. pol = 0;
  125. edge = mask;
  126. break;
  127. case IRQ_TYPE_EDGE_RISING:
  128. pol = mask;
  129. edge = mask;
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  135. ipol = priv->gc.read_reg(priv->regs + GRGPIO_IPOL) & ~mask;
  136. iedge = priv->gc.read_reg(priv->regs + GRGPIO_IEDGE) & ~mask;
  137. priv->gc.write_reg(priv->regs + GRGPIO_IPOL, ipol | pol);
  138. priv->gc.write_reg(priv->regs + GRGPIO_IEDGE, iedge | edge);
  139. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  140. return 0;
  141. }
  142. static void grgpio_irq_mask(struct irq_data *d)
  143. {
  144. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  145. int offset = d->hwirq;
  146. unsigned long flags;
  147. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  148. grgpio_set_imask(priv, offset, 0);
  149. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  150. }
  151. static void grgpio_irq_unmask(struct irq_data *d)
  152. {
  153. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  154. int offset = d->hwirq;
  155. unsigned long flags;
  156. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  157. grgpio_set_imask(priv, offset, 1);
  158. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  159. }
  160. static struct irq_chip grgpio_irq_chip = {
  161. .name = "grgpio",
  162. .irq_mask = grgpio_irq_mask,
  163. .irq_unmask = grgpio_irq_unmask,
  164. .irq_set_type = grgpio_irq_set_type,
  165. };
  166. static irqreturn_t grgpio_irq_handler(int irq, void *dev)
  167. {
  168. struct grgpio_priv *priv = dev;
  169. int ngpio = priv->gc.ngpio;
  170. unsigned long flags;
  171. int i;
  172. int match = 0;
  173. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  174. /*
  175. * For each gpio line, call its interrupt handler if it its underlying
  176. * irq matches the current irq that is handled.
  177. */
  178. for (i = 0; i < ngpio; i++) {
  179. struct grgpio_lirq *lirq = &priv->lirqs[i];
  180. if (priv->imask & BIT(i) && lirq->index >= 0 &&
  181. priv->uirqs[lirq->index].uirq == irq) {
  182. generic_handle_irq(lirq->irq);
  183. match = 1;
  184. }
  185. }
  186. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  187. if (!match)
  188. dev_warn(priv->dev, "No gpio line matched irq %d\n", irq);
  189. return IRQ_HANDLED;
  190. }
  191. /*
  192. * This function will be called as a consequence of the call to
  193. * irq_create_mapping in grgpio_to_irq
  194. */
  195. static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
  196. irq_hw_number_t hwirq)
  197. {
  198. struct grgpio_priv *priv = d->host_data;
  199. struct grgpio_lirq *lirq;
  200. struct grgpio_uirq *uirq;
  201. unsigned long flags;
  202. int offset = hwirq;
  203. int ret = 0;
  204. if (!priv)
  205. return -EINVAL;
  206. lirq = &priv->lirqs[offset];
  207. if (lirq->index < 0)
  208. return -EINVAL;
  209. dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n",
  210. irq, offset);
  211. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  212. /* Request underlying irq if not already requested */
  213. lirq->irq = irq;
  214. uirq = &priv->uirqs[lirq->index];
  215. if (uirq->refcnt == 0) {
  216. ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
  217. dev_name(priv->dev), priv);
  218. if (ret) {
  219. dev_err(priv->dev,
  220. "Could not request underlying irq %d\n",
  221. uirq->uirq);
  222. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  223. return ret;
  224. }
  225. }
  226. uirq->refcnt++;
  227. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  228. /* Setup irq */
  229. irq_set_chip_data(irq, priv);
  230. irq_set_chip_and_handler(irq, &grgpio_irq_chip,
  231. handle_simple_irq);
  232. irq_set_noprobe(irq);
  233. return ret;
  234. }
  235. static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
  236. {
  237. struct grgpio_priv *priv = d->host_data;
  238. int index;
  239. struct grgpio_lirq *lirq;
  240. struct grgpio_uirq *uirq;
  241. unsigned long flags;
  242. int ngpio = priv->gc.ngpio;
  243. int i;
  244. irq_set_chip_and_handler(irq, NULL, NULL);
  245. irq_set_chip_data(irq, NULL);
  246. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  247. /* Free underlying irq if last user unmapped */
  248. index = -1;
  249. for (i = 0; i < ngpio; i++) {
  250. lirq = &priv->lirqs[i];
  251. if (lirq->irq == irq) {
  252. grgpio_set_imask(priv, i, 0);
  253. lirq->irq = 0;
  254. index = lirq->index;
  255. break;
  256. }
  257. }
  258. WARN_ON(index < 0);
  259. if (index >= 0) {
  260. uirq = &priv->uirqs[lirq->index];
  261. uirq->refcnt--;
  262. if (uirq->refcnt == 0)
  263. free_irq(uirq->uirq, priv);
  264. }
  265. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  266. }
  267. static const struct irq_domain_ops grgpio_irq_domain_ops = {
  268. .map = grgpio_irq_map,
  269. .unmap = grgpio_irq_unmap,
  270. };
  271. /* ------------------------------------------------------------ */
  272. static int grgpio_probe(struct platform_device *ofdev)
  273. {
  274. struct device_node *np = ofdev->dev.of_node;
  275. void __iomem *regs;
  276. struct gpio_chip *gc;
  277. struct grgpio_priv *priv;
  278. struct resource *res;
  279. int err;
  280. u32 prop;
  281. s32 *irqmap;
  282. int size;
  283. int i;
  284. priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
  285. if (!priv)
  286. return -ENOMEM;
  287. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  288. regs = devm_ioremap_resource(&ofdev->dev, res);
  289. if (IS_ERR(regs))
  290. return PTR_ERR(regs);
  291. gc = &priv->gc;
  292. err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA,
  293. regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL,
  294. BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  295. if (err) {
  296. dev_err(&ofdev->dev, "bgpio_init() failed\n");
  297. return err;
  298. }
  299. priv->regs = regs;
  300. priv->imask = gc->read_reg(regs + GRGPIO_IMASK);
  301. priv->dev = &ofdev->dev;
  302. gc->of_node = np;
  303. gc->owner = THIS_MODULE;
  304. gc->to_irq = grgpio_to_irq;
  305. gc->label = np->full_name;
  306. gc->base = -1;
  307. err = of_property_read_u32(np, "nbits", &prop);
  308. if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
  309. gc->ngpio = GRGPIO_MAX_NGPIO;
  310. dev_dbg(&ofdev->dev,
  311. "No or invalid nbits property: assume %d\n", gc->ngpio);
  312. } else {
  313. gc->ngpio = prop;
  314. }
  315. /*
  316. * The irqmap contains the index values indicating which underlying irq,
  317. * if anyone, is connected to that line
  318. */
  319. irqmap = (s32 *)of_get_property(np, "irqmap", &size);
  320. if (irqmap) {
  321. if (size < gc->ngpio) {
  322. dev_err(&ofdev->dev,
  323. "irqmap shorter than ngpio (%d < %d)\n",
  324. size, gc->ngpio);
  325. return -EINVAL;
  326. }
  327. priv->domain = irq_domain_add_linear(np, gc->ngpio,
  328. &grgpio_irq_domain_ops,
  329. priv);
  330. if (!priv->domain) {
  331. dev_err(&ofdev->dev, "Could not add irq domain\n");
  332. return -EINVAL;
  333. }
  334. for (i = 0; i < gc->ngpio; i++) {
  335. struct grgpio_lirq *lirq;
  336. int ret;
  337. lirq = &priv->lirqs[i];
  338. lirq->index = irqmap[i];
  339. if (lirq->index < 0)
  340. continue;
  341. ret = platform_get_irq(ofdev, lirq->index);
  342. if (ret <= 0) {
  343. /*
  344. * Continue without irq functionality for that
  345. * gpio line
  346. */
  347. dev_err(priv->dev,
  348. "Failed to get irq for offset %d\n", i);
  349. continue;
  350. }
  351. priv->uirqs[lirq->index].uirq = ret;
  352. }
  353. }
  354. platform_set_drvdata(ofdev, priv);
  355. err = gpiochip_add_data(gc, priv);
  356. if (err) {
  357. dev_err(&ofdev->dev, "Could not add gpiochip\n");
  358. if (priv->domain)
  359. irq_domain_remove(priv->domain);
  360. return err;
  361. }
  362. dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
  363. priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
  364. return 0;
  365. }
  366. static int grgpio_remove(struct platform_device *ofdev)
  367. {
  368. struct grgpio_priv *priv = platform_get_drvdata(ofdev);
  369. unsigned long flags;
  370. int i;
  371. int ret = 0;
  372. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  373. if (priv->domain) {
  374. for (i = 0; i < GRGPIO_MAX_NGPIO; i++) {
  375. if (priv->uirqs[i].refcnt != 0) {
  376. ret = -EBUSY;
  377. goto out;
  378. }
  379. }
  380. }
  381. gpiochip_remove(&priv->gc);
  382. if (priv->domain)
  383. irq_domain_remove(priv->domain);
  384. out:
  385. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  386. return ret;
  387. }
  388. static const struct of_device_id grgpio_match[] = {
  389. {.name = "GAISLER_GPIO"},
  390. {.name = "01_01a"},
  391. {},
  392. };
  393. MODULE_DEVICE_TABLE(of, grgpio_match);
  394. static struct platform_driver grgpio_driver = {
  395. .driver = {
  396. .name = "grgpio",
  397. .of_match_table = grgpio_match,
  398. },
  399. .probe = grgpio_probe,
  400. .remove = grgpio_remove,
  401. };
  402. module_platform_driver(grgpio_driver);
  403. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  404. MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
  405. MODULE_LICENSE("GPL");