gpio-grgpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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_platform.h>
  29. #include <linux/gpio/driver.h>
  30. #include <linux/slab.h>
  31. #include <linux/err.h>
  32. #include <linux/gpio/driver.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/irq.h>
  35. #include <linux/irqdomain.h>
  36. #include <linux/bitops.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. if (val)
  89. priv->imask |= BIT(offset);
  90. else
  91. priv->imask &= ~BIT(offset);
  92. gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
  93. }
  94. static int grgpio_to_irq(struct gpio_chip *gc, unsigned offset)
  95. {
  96. struct grgpio_priv *priv = gpiochip_get_data(gc);
  97. if (offset >= gc->ngpio)
  98. return -ENXIO;
  99. if (priv->lirqs[offset].index < 0)
  100. return -ENXIO;
  101. return irq_create_mapping(priv->domain, offset);
  102. }
  103. /* -------------------- IRQ chip functions -------------------- */
  104. static int grgpio_irq_set_type(struct irq_data *d, unsigned int type)
  105. {
  106. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  107. unsigned long flags;
  108. u32 mask = BIT(d->hwirq);
  109. u32 ipol;
  110. u32 iedge;
  111. u32 pol;
  112. u32 edge;
  113. switch (type) {
  114. case IRQ_TYPE_LEVEL_LOW:
  115. pol = 0;
  116. edge = 0;
  117. break;
  118. case IRQ_TYPE_LEVEL_HIGH:
  119. pol = mask;
  120. edge = 0;
  121. break;
  122. case IRQ_TYPE_EDGE_FALLING:
  123. pol = 0;
  124. edge = mask;
  125. break;
  126. case IRQ_TYPE_EDGE_RISING:
  127. pol = mask;
  128. edge = mask;
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  134. ipol = priv->gc.read_reg(priv->regs + GRGPIO_IPOL) & ~mask;
  135. iedge = priv->gc.read_reg(priv->regs + GRGPIO_IEDGE) & ~mask;
  136. priv->gc.write_reg(priv->regs + GRGPIO_IPOL, ipol | pol);
  137. priv->gc.write_reg(priv->regs + GRGPIO_IEDGE, iedge | edge);
  138. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  139. return 0;
  140. }
  141. static void grgpio_irq_mask(struct irq_data *d)
  142. {
  143. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  144. int offset = d->hwirq;
  145. unsigned long flags;
  146. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  147. grgpio_set_imask(priv, offset, 0);
  148. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  149. }
  150. static void grgpio_irq_unmask(struct irq_data *d)
  151. {
  152. struct grgpio_priv *priv = irq_data_get_irq_chip_data(d);
  153. int offset = d->hwirq;
  154. unsigned long flags;
  155. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  156. grgpio_set_imask(priv, offset, 1);
  157. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  158. }
  159. static struct irq_chip grgpio_irq_chip = {
  160. .name = "grgpio",
  161. .irq_mask = grgpio_irq_mask,
  162. .irq_unmask = grgpio_irq_unmask,
  163. .irq_set_type = grgpio_irq_set_type,
  164. };
  165. static irqreturn_t grgpio_irq_handler(int irq, void *dev)
  166. {
  167. struct grgpio_priv *priv = dev;
  168. int ngpio = priv->gc.ngpio;
  169. unsigned long flags;
  170. int i;
  171. int match = 0;
  172. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  173. /*
  174. * For each gpio line, call its interrupt handler if it its underlying
  175. * irq matches the current irq that is handled.
  176. */
  177. for (i = 0; i < ngpio; i++) {
  178. struct grgpio_lirq *lirq = &priv->lirqs[i];
  179. if (priv->imask & BIT(i) && lirq->index >= 0 &&
  180. priv->uirqs[lirq->index].uirq == irq) {
  181. generic_handle_irq(lirq->irq);
  182. match = 1;
  183. }
  184. }
  185. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  186. if (!match)
  187. dev_warn(priv->dev, "No gpio line matched irq %d\n", irq);
  188. return IRQ_HANDLED;
  189. }
  190. /*
  191. * This function will be called as a consequence of the call to
  192. * irq_create_mapping in grgpio_to_irq
  193. */
  194. static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
  195. irq_hw_number_t hwirq)
  196. {
  197. struct grgpio_priv *priv = d->host_data;
  198. struct grgpio_lirq *lirq;
  199. struct grgpio_uirq *uirq;
  200. unsigned long flags;
  201. int offset = hwirq;
  202. int ret = 0;
  203. if (!priv)
  204. return -EINVAL;
  205. lirq = &priv->lirqs[offset];
  206. if (lirq->index < 0)
  207. return -EINVAL;
  208. dev_dbg(priv->dev, "Mapping irq %d for gpio line %d\n",
  209. irq, offset);
  210. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  211. /* Request underlying irq if not already requested */
  212. lirq->irq = irq;
  213. uirq = &priv->uirqs[lirq->index];
  214. if (uirq->refcnt == 0) {
  215. ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
  216. dev_name(priv->dev), priv);
  217. if (ret) {
  218. dev_err(priv->dev,
  219. "Could not request underlying irq %d\n",
  220. uirq->uirq);
  221. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  222. return ret;
  223. }
  224. }
  225. uirq->refcnt++;
  226. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  227. /* Setup irq */
  228. irq_set_chip_data(irq, priv);
  229. irq_set_chip_and_handler(irq, &grgpio_irq_chip,
  230. handle_simple_irq);
  231. irq_set_noprobe(irq);
  232. return ret;
  233. }
  234. static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
  235. {
  236. struct grgpio_priv *priv = d->host_data;
  237. int index;
  238. struct grgpio_lirq *lirq;
  239. struct grgpio_uirq *uirq;
  240. unsigned long flags;
  241. int ngpio = priv->gc.ngpio;
  242. int i;
  243. irq_set_chip_and_handler(irq, NULL, NULL);
  244. irq_set_chip_data(irq, NULL);
  245. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  246. /* Free underlying irq if last user unmapped */
  247. index = -1;
  248. for (i = 0; i < ngpio; i++) {
  249. lirq = &priv->lirqs[i];
  250. if (lirq->irq == irq) {
  251. grgpio_set_imask(priv, i, 0);
  252. lirq->irq = 0;
  253. index = lirq->index;
  254. break;
  255. }
  256. }
  257. WARN_ON(index < 0);
  258. if (index >= 0) {
  259. uirq = &priv->uirqs[lirq->index];
  260. uirq->refcnt--;
  261. if (uirq->refcnt == 0)
  262. free_irq(uirq->uirq, priv);
  263. }
  264. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  265. }
  266. static const struct irq_domain_ops grgpio_irq_domain_ops = {
  267. .map = grgpio_irq_map,
  268. .unmap = grgpio_irq_unmap,
  269. };
  270. /* ------------------------------------------------------------ */
  271. static int grgpio_probe(struct platform_device *ofdev)
  272. {
  273. struct device_node *np = ofdev->dev.of_node;
  274. void __iomem *regs;
  275. struct gpio_chip *gc;
  276. struct grgpio_priv *priv;
  277. struct resource *res;
  278. int err;
  279. u32 prop;
  280. s32 *irqmap;
  281. int size;
  282. int i;
  283. priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
  284. if (!priv)
  285. return -ENOMEM;
  286. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  287. regs = devm_ioremap_resource(&ofdev->dev, res);
  288. if (IS_ERR(regs))
  289. return PTR_ERR(regs);
  290. gc = &priv->gc;
  291. err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA,
  292. regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL,
  293. BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  294. if (err) {
  295. dev_err(&ofdev->dev, "bgpio_init() failed\n");
  296. return err;
  297. }
  298. priv->regs = regs;
  299. priv->imask = gc->read_reg(regs + GRGPIO_IMASK);
  300. priv->dev = &ofdev->dev;
  301. gc->of_node = np;
  302. gc->owner = THIS_MODULE;
  303. gc->to_irq = grgpio_to_irq;
  304. gc->label = devm_kasprintf(&ofdev->dev, GFP_KERNEL, "%pOF", np);
  305. gc->base = -1;
  306. err = of_property_read_u32(np, "nbits", &prop);
  307. if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
  308. gc->ngpio = GRGPIO_MAX_NGPIO;
  309. dev_dbg(&ofdev->dev,
  310. "No or invalid nbits property: assume %d\n", gc->ngpio);
  311. } else {
  312. gc->ngpio = prop;
  313. }
  314. /*
  315. * The irqmap contains the index values indicating which underlying irq,
  316. * if anyone, is connected to that line
  317. */
  318. irqmap = (s32 *)of_get_property(np, "irqmap", &size);
  319. if (irqmap) {
  320. if (size < gc->ngpio) {
  321. dev_err(&ofdev->dev,
  322. "irqmap shorter than ngpio (%d < %d)\n",
  323. size, gc->ngpio);
  324. return -EINVAL;
  325. }
  326. priv->domain = irq_domain_add_linear(np, gc->ngpio,
  327. &grgpio_irq_domain_ops,
  328. priv);
  329. if (!priv->domain) {
  330. dev_err(&ofdev->dev, "Could not add irq domain\n");
  331. return -EINVAL;
  332. }
  333. for (i = 0; i < gc->ngpio; i++) {
  334. struct grgpio_lirq *lirq;
  335. int ret;
  336. lirq = &priv->lirqs[i];
  337. lirq->index = irqmap[i];
  338. if (lirq->index < 0)
  339. continue;
  340. ret = platform_get_irq(ofdev, lirq->index);
  341. if (ret <= 0) {
  342. /*
  343. * Continue without irq functionality for that
  344. * gpio line
  345. */
  346. dev_err(priv->dev,
  347. "Failed to get irq for offset %d\n", i);
  348. continue;
  349. }
  350. priv->uirqs[lirq->index].uirq = ret;
  351. }
  352. }
  353. platform_set_drvdata(ofdev, priv);
  354. err = gpiochip_add_data(gc, priv);
  355. if (err) {
  356. dev_err(&ofdev->dev, "Could not add gpiochip\n");
  357. if (priv->domain)
  358. irq_domain_remove(priv->domain);
  359. return err;
  360. }
  361. dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
  362. priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
  363. return 0;
  364. }
  365. static int grgpio_remove(struct platform_device *ofdev)
  366. {
  367. struct grgpio_priv *priv = platform_get_drvdata(ofdev);
  368. unsigned long flags;
  369. int i;
  370. int ret = 0;
  371. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  372. if (priv->domain) {
  373. for (i = 0; i < GRGPIO_MAX_NGPIO; i++) {
  374. if (priv->uirqs[i].refcnt != 0) {
  375. ret = -EBUSY;
  376. goto out;
  377. }
  378. }
  379. }
  380. gpiochip_remove(&priv->gc);
  381. if (priv->domain)
  382. irq_domain_remove(priv->domain);
  383. out:
  384. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  385. return ret;
  386. }
  387. static const struct of_device_id grgpio_match[] = {
  388. {.name = "GAISLER_GPIO"},
  389. {.name = "01_01a"},
  390. {},
  391. };
  392. MODULE_DEVICE_TABLE(of, grgpio_match);
  393. static struct platform_driver grgpio_driver = {
  394. .driver = {
  395. .name = "grgpio",
  396. .of_match_table = grgpio_match,
  397. },
  398. .probe = grgpio_probe,
  399. .remove = grgpio_remove,
  400. };
  401. module_platform_driver(grgpio_driver);
  402. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  403. MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
  404. MODULE_LICENSE("GPL");