xref: /linux/drivers/iio/imu/adis_trigger.c (revision d6869352cb3c3cf3450637a52349e2e87c1354aa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Common library for ADIS16XXX devices
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *   Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/export.h>
13 
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/imu/adis.h>
17 
18 static int adis_data_rdy_trigger_set_state(struct iio_trigger *trig,
19 						bool state)
20 {
21 	struct adis *adis = iio_trigger_get_drvdata(trig);
22 
23 	return adis_enable_irq(adis, state);
24 }
25 
26 static const struct iio_trigger_ops adis_trigger_ops = {
27 	.set_trigger_state = &adis_data_rdy_trigger_set_state,
28 };
29 
30 /**
31  * adis_probe_trigger() - Sets up trigger for a adis device
32  * @adis: The adis device
33  * @indio_dev: The IIO device
34  *
35  * Returns 0 on success or a negative error code
36  *
37  * adis_remove_trigger() should be used to free the trigger.
38  */
39 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
40 {
41 	int ret;
42 
43 	adis->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
44 					indio_dev->id);
45 	if (adis->trig == NULL)
46 		return -ENOMEM;
47 
48 	adis->trig->dev.parent = &adis->spi->dev;
49 	adis->trig->ops = &adis_trigger_ops;
50 	iio_trigger_set_drvdata(adis->trig, adis);
51 
52 	ret = request_irq(adis->spi->irq,
53 			  &iio_trigger_generic_data_rdy_poll,
54 			  IRQF_TRIGGER_RISING,
55 			  indio_dev->name,
56 			  adis->trig);
57 	if (ret)
58 		goto error_free_trig;
59 
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