adis_trigger.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Common library for ADIS16XXX devices
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/export.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/trigger.h>
  15. #include <linux/iio/imu/adis.h>
  16. static int adis_data_rdy_trigger_set_state(struct iio_trigger *trig,
  17. bool state)
  18. {
  19. struct adis *adis = iio_trigger_get_drvdata(trig);
  20. return adis_enable_irq(adis, state);
  21. }
  22. static const struct iio_trigger_ops adis_trigger_ops = {
  23. .set_trigger_state = &adis_data_rdy_trigger_set_state,
  24. };
  25. /**
  26. * adis_probe_trigger() - Sets up trigger for a adis device
  27. * @adis: The adis device
  28. * @indio_dev: The IIO device
  29. *
  30. * Returns 0 on success or a negative error code
  31. *
  32. * adis_remove_trigger() should be used to free the trigger.
  33. */
  34. int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
  35. {
  36. int ret;
  37. adis->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  38. indio_dev->id);
  39. if (adis->trig == NULL)
  40. return -ENOMEM;
  41. adis->trig->dev.parent = &adis->spi->dev;
  42. adis->trig->ops = &adis_trigger_ops;
  43. iio_trigger_set_drvdata(adis->trig, adis);
  44. ret = request_irq(adis->spi->irq,
  45. &iio_trigger_generic_data_rdy_poll,
  46. IRQF_TRIGGER_RISING,
  47. indio_dev->name,
  48. adis->trig);
  49. if (ret)
  50. goto error_free_trig;
  51. ret = iio_trigger_register(adis->trig);
  52. indio_dev->trig = iio_trigger_get(adis->trig);
  53. if (ret)
  54. goto error_free_irq;
  55. return 0;
  56. error_free_irq:
  57. free_irq(adis->spi->irq, adis->trig);
  58. error_free_trig:
  59. iio_trigger_free(adis->trig);
  60. return ret;
  61. }
  62. EXPORT_SYMBOL_GPL(adis_probe_trigger);
  63. /**
  64. * adis_remove_trigger() - Remove trigger for a adis devices
  65. * @adis: The adis device
  66. *
  67. * Removes the trigger previously registered with adis_probe_trigger().
  68. */
  69. void adis_remove_trigger(struct adis *adis)
  70. {
  71. iio_trigger_unregister(adis->trig);
  72. free_irq(adis->spi->irq, adis->trig);
  73. iio_trigger_free(adis->trig);
  74. }
  75. EXPORT_SYMBOL_GPL(adis_remove_trigger);