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 10 #include <linux/interrupt.h> 11 #include <linux/kernel.h> 12 #include <linux/spi/spi.h> 13 #include <linux/export.h> 14 15 #include <linux/iio/iio.h> 16 #include <linux/iio/trigger.h> 17 #include <linux/iio/imu/adis.h> 18 19 static int adis_data_rdy_trigger_set_state(struct iio_trigger *trig, 20 bool state) 21 { 22 struct adis *adis = iio_trigger_get_drvdata(trig); 23 24 return adis_enable_irq(adis, state); 25 } 26 27 static const struct iio_trigger_ops adis_trigger_ops = { 28 .set_trigger_state = &adis_data_rdy_trigger_set_state, 29 }; 30 31 /** 32 * adis_probe_trigger() - Sets up trigger for a adis device 33 * @adis: The adis device 34 * @indio_dev: The IIO device 35 * 36 * Returns 0 on success or a negative error code 37 * 38 * adis_remove_trigger() should be used to free the trigger. 39 */ 40 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev) 41 { 42 int ret; 43 44 adis->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, 45 indio_dev->id); 46 if (adis->trig == NULL) 47 return -ENOMEM; 48 49 ret = request_irq(adis->spi->irq, 50 &iio_trigger_generic_data_rdy_poll, 51 IRQF_TRIGGER_RISING, 52 indio_dev->name, 53 adis->trig); 54 if (ret) 55 goto error_free_trig; 56 57 adis->trig->dev.parent = &adis->spi->dev; 58 adis->trig->ops = &adis_trigger_ops; 59 iio_trigger_set_drvdata(adis->trig, adis); 60 ret = iio_trigger_register(adis->trig); 61 62 indio_dev->trig = iio_trigger_get(adis->trig); 63 if (ret) 64 goto error_free_irq; 65 66 return 0; 67 68 error_free_irq: 69 free_irq(adis->spi->irq, adis->trig); 70 error_free_trig: 71 iio_trigger_free(adis->trig); 72 return ret; 73 } 74 EXPORT_SYMBOL_GPL(adis_probe_trigger); 75 76 /** 77 * adis_remove_trigger() - Remove trigger for a adis devices 78 * @adis: The adis device 79 * 80 * Removes the trigger previously registered with adis_probe_trigger(). 81 */ 82 void adis_remove_trigger(struct adis *adis) 83 { 84 iio_trigger_unregister(adis->trig); 85 free_irq(adis->spi->irq, adis->trig); 86 iio_trigger_free(adis->trig); 87 } 88 EXPORT_SYMBOL_GPL(adis_remove_trigger); 89