gpio-grgpio.c 11 KB

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