gpio-grgpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  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. return ret;
  223. }
  224. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  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. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  264. free_irq(uirq->uirq, priv);
  265. return;
  266. }
  267. }
  268. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  269. }
  270. static const struct irq_domain_ops grgpio_irq_domain_ops = {
  271. .map = grgpio_irq_map,
  272. .unmap = grgpio_irq_unmap,
  273. };
  274. /* ------------------------------------------------------------ */
  275. static int grgpio_probe(struct platform_device *ofdev)
  276. {
  277. struct device_node *np = ofdev->dev.of_node;
  278. void __iomem *regs;
  279. struct gpio_chip *gc;
  280. struct grgpio_priv *priv;
  281. struct resource *res;
  282. int err;
  283. u32 prop;
  284. s32 *irqmap;
  285. int size;
  286. int i;
  287. priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
  288. if (!priv)
  289. return -ENOMEM;
  290. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  291. regs = devm_ioremap_resource(&ofdev->dev, res);
  292. if (IS_ERR(regs))
  293. return PTR_ERR(regs);
  294. gc = &priv->gc;
  295. err = bgpio_init(gc, &ofdev->dev, 4, regs + GRGPIO_DATA,
  296. regs + GRGPIO_OUTPUT, NULL, regs + GRGPIO_DIR, NULL,
  297. BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  298. if (err) {
  299. dev_err(&ofdev->dev, "bgpio_init() failed\n");
  300. return err;
  301. }
  302. priv->regs = regs;
  303. priv->imask = gc->read_reg(regs + GRGPIO_IMASK);
  304. priv->dev = &ofdev->dev;
  305. gc->of_node = np;
  306. gc->owner = THIS_MODULE;
  307. gc->to_irq = grgpio_to_irq;
  308. gc->label = devm_kasprintf(&ofdev->dev, GFP_KERNEL, "%pOF", np);
  309. gc->base = -1;
  310. err = of_property_read_u32(np, "nbits", &prop);
  311. if (err || prop <= 0 || prop > GRGPIO_MAX_NGPIO) {
  312. gc->ngpio = GRGPIO_MAX_NGPIO;
  313. dev_dbg(&ofdev->dev,
  314. "No or invalid nbits property: assume %d\n", gc->ngpio);
  315. } else {
  316. gc->ngpio = prop;
  317. }
  318. /*
  319. * The irqmap contains the index values indicating which underlying irq,
  320. * if anyone, is connected to that line
  321. */
  322. irqmap = (s32 *)of_get_property(np, "irqmap", &size);
  323. if (irqmap) {
  324. if (size < gc->ngpio) {
  325. dev_err(&ofdev->dev,
  326. "irqmap shorter than ngpio (%d < %d)\n",
  327. size, gc->ngpio);
  328. return -EINVAL;
  329. }
  330. priv->domain = irq_domain_add_linear(np, gc->ngpio,
  331. &grgpio_irq_domain_ops,
  332. priv);
  333. if (!priv->domain) {
  334. dev_err(&ofdev->dev, "Could not add irq domain\n");
  335. return -EINVAL;
  336. }
  337. for (i = 0; i < gc->ngpio; i++) {
  338. struct grgpio_lirq *lirq;
  339. int ret;
  340. lirq = &priv->lirqs[i];
  341. lirq->index = irqmap[i];
  342. if (lirq->index < 0)
  343. continue;
  344. ret = platform_get_irq(ofdev, lirq->index);
  345. if (ret <= 0) {
  346. /*
  347. * Continue without irq functionality for that
  348. * gpio line
  349. */
  350. dev_err(priv->dev,
  351. "Failed to get irq for offset %d\n", i);
  352. continue;
  353. }
  354. priv->uirqs[lirq->index].uirq = ret;
  355. }
  356. }
  357. platform_set_drvdata(ofdev, priv);
  358. err = gpiochip_add_data(gc, priv);
  359. if (err) {
  360. dev_err(&ofdev->dev, "Could not add gpiochip\n");
  361. if (priv->domain)
  362. irq_domain_remove(priv->domain);
  363. return err;
  364. }
  365. dev_info(&ofdev->dev, "regs=0x%p, base=%d, ngpio=%d, irqs=%s\n",
  366. priv->regs, gc->base, gc->ngpio, priv->domain ? "on" : "off");
  367. return 0;
  368. }
  369. static int grgpio_remove(struct platform_device *ofdev)
  370. {
  371. struct grgpio_priv *priv = platform_get_drvdata(ofdev);
  372. unsigned long flags;
  373. int i;
  374. int ret = 0;
  375. spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
  376. if (priv->domain) {
  377. for (i = 0; i < GRGPIO_MAX_NGPIO; i++) {
  378. if (priv->uirqs[i].refcnt != 0) {
  379. ret = -EBUSY;
  380. goto out;
  381. }
  382. }
  383. }
  384. gpiochip_remove(&priv->gc);
  385. if (priv->domain)
  386. irq_domain_remove(priv->domain);
  387. out:
  388. spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
  389. return ret;
  390. }
  391. static const struct of_device_id grgpio_match[] = {
  392. {.name = "GAISLER_GPIO"},
  393. {.name = "01_01a"},
  394. {},
  395. };
  396. MODULE_DEVICE_TABLE(of, grgpio_match);
  397. static struct platform_driver grgpio_driver = {
  398. .driver = {
  399. .name = "grgpio",
  400. .of_match_table = grgpio_match,
  401. },
  402. .probe = grgpio_probe,
  403. .remove = grgpio_remove,
  404. };
  405. module_platform_driver(grgpio_driver);
  406. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  407. MODULE_DESCRIPTION("Driver for Aeroflex Gaisler GRGPIO");
  408. MODULE_LICENSE("GPL");