iio-trig-interrupt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Industrial I/O - generic interrupt based trigger support
  3. *
  4. * Copyright (c) 2008-2013 Jonathan Cameron
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/slab.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/trigger.h>
  17. struct iio_interrupt_trigger_info {
  18. unsigned int irq;
  19. };
  20. static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private)
  21. {
  22. iio_trigger_poll(private);
  23. return IRQ_HANDLED;
  24. }
  25. static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
  26. .owner = THIS_MODULE,
  27. };
  28. static int iio_interrupt_trigger_probe(struct platform_device *pdev)
  29. {
  30. struct iio_interrupt_trigger_info *trig_info;
  31. struct iio_trigger *trig;
  32. unsigned long irqflags;
  33. struct resource *irq_res;
  34. int irq, ret = 0;
  35. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  36. if (irq_res == NULL)
  37. return -ENODEV;
  38. irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
  39. irq = irq_res->start;
  40. trig = iio_trigger_alloc("irqtrig%d", irq);
  41. if (!trig) {
  42. ret = -ENOMEM;
  43. goto error_ret;
  44. }
  45. trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
  46. if (!trig_info) {
  47. ret = -ENOMEM;
  48. goto error_free_trigger;
  49. }
  50. iio_trigger_set_drvdata(trig, trig_info);
  51. trig_info->irq = irq;
  52. trig->ops = &iio_interrupt_trigger_ops;
  53. ret = request_irq(irq, iio_interrupt_trigger_poll,
  54. irqflags, trig->name, trig);
  55. if (ret) {
  56. dev_err(&pdev->dev,
  57. "request IRQ-%d failed", irq);
  58. goto error_free_trig_info;
  59. }
  60. ret = iio_trigger_register(trig);
  61. if (ret)
  62. goto error_release_irq;
  63. platform_set_drvdata(pdev, trig);
  64. return 0;
  65. /* First clean up the partly allocated trigger */
  66. error_release_irq:
  67. free_irq(irq, trig);
  68. error_free_trig_info:
  69. kfree(trig_info);
  70. error_free_trigger:
  71. iio_trigger_free(trig);
  72. error_ret:
  73. return ret;
  74. }
  75. static int iio_interrupt_trigger_remove(struct platform_device *pdev)
  76. {
  77. struct iio_trigger *trig;
  78. struct iio_interrupt_trigger_info *trig_info;
  79. trig = platform_get_drvdata(pdev);
  80. trig_info = iio_trigger_get_drvdata(trig);
  81. iio_trigger_unregister(trig);
  82. free_irq(trig_info->irq, trig);
  83. kfree(trig_info);
  84. iio_trigger_free(trig);
  85. return 0;
  86. }
  87. static struct platform_driver iio_interrupt_trigger_driver = {
  88. .probe = iio_interrupt_trigger_probe,
  89. .remove = iio_interrupt_trigger_remove,
  90. .driver = {
  91. .name = "iio_interrupt_trigger",
  92. },
  93. };
  94. module_platform_driver(iio_interrupt_trigger_driver);
  95. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  96. MODULE_DESCRIPTION("Interrupt trigger for the iio subsystem");
  97. MODULE_LICENSE("GPL v2");