gpio-grgpio.c 12 KB

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