xref: /openbmc/linux/drivers/iio/imu/adis_trigger.c (revision 82df5b73)
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 static void adis_trigger_setup(struct adis *adis)
31 {
32 	adis->trig->dev.parent = &adis->spi->dev;
33 	adis->trig->ops = &adis_trigger_ops;
34 	iio_trigger_set_drvdata(adis->trig, adis);
35 }
36 
37 static int adis_validate_irq_flag(struct adis *adis)
38 {
39 	/*
40 	 * Typically this devices have data ready either on the rising edge or
41 	 * on the falling edge of the data ready pin. This checks enforces that
42 	 * one of those is set in the drivers... It defaults to
43 	 * IRQF_TRIGGER_RISING for backward compatibility wiht devices that
44 	 * don't support changing the pin polarity.
45 	 */
46 	if (!adis->irq_flag) {
47 		adis->irq_flag = IRQF_TRIGGER_RISING;
48 		return 0;
49 	} else if (adis->irq_flag != IRQF_TRIGGER_RISING &&
50 		   adis->irq_flag != IRQF_TRIGGER_FALLING) {
51 		dev_err(&adis->spi->dev, "Invalid IRQ mask: %08lx\n",
52 			adis->irq_flag);
53 		return -EINVAL;
54 	}
55 
56 	return 0;
57 }
58 /**
59  * adis_probe_trigger() - Sets up trigger for a adis device
60  * @adis: The adis device
61  * @indio_dev: The IIO device
62  *
63  * Returns 0 on success or a negative error code
64  *
65  * adis_remove_trigger() should be used to free the trigger.
66  */
67 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
68 {
69 	int ret;
70 
71 	adis->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
72 					indio_dev->id);
73 	if (adis->trig == NULL)
74 		return -ENOMEM;
75 
76 	adis_trigger_setup(adis);
77 
78 	ret = adis_validate_irq_flag(adis);
79 	if (ret)
80 		return ret;
81 
82 	ret = request_irq(adis->spi->irq,
83 			  &iio_trigger_generic_data_rdy_poll,
84 			  adis->irq_flag,
85 			  indio_dev->name,
86 			  adis->trig);
87 	if (ret)
88 		goto error_free_trig;
89 
90 	ret = iio_trigger_register(adis->trig);
91 
92 	indio_dev->trig = iio_trigger_get(adis->trig);
93 	if (ret)
94 		goto error_free_irq;
95 
96 	return 0;
97 
98 error_free_irq:
99 	free_irq(adis->spi->irq, adis->trig);
100 error_free_trig:
101 	iio_trigger_free(adis->trig);
102 	return ret;
103 }
104 EXPORT_SYMBOL_GPL(adis_probe_trigger);
105 
106 /**
107  * devm_adis_probe_trigger() - Sets up trigger for a managed adis device
108  * @adis: The adis device
109  * @indio_dev: The IIO device
110  *
111  * Returns 0 on success or a negative error code
112  */
113 int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
114 {
115 	int ret;
116 
117 	adis->trig = devm_iio_trigger_alloc(&adis->spi->dev, "%s-dev%d",
118 					    indio_dev->name, indio_dev->id);
119 	if (!adis->trig)
120 		return -ENOMEM;
121 
122 	adis_trigger_setup(adis);
123 
124 	ret = adis_validate_irq_flag(adis);
125 	if (ret)
126 		return ret;
127 
128 	ret = devm_request_irq(&adis->spi->dev, adis->spi->irq,
129 			       &iio_trigger_generic_data_rdy_poll,
130 			       adis->irq_flag,
131 			       indio_dev->name,
132 			       adis->trig);
133 	if (ret)
134 		return ret;
135 
136 	return devm_iio_trigger_register(&adis->spi->dev, adis->trig);
137 }
138 EXPORT_SYMBOL_GPL(devm_adis_probe_trigger);
139 
140 /**
141  * adis_remove_trigger() - Remove trigger for a adis devices
142  * @adis: The adis device
143  *
144  * Removes the trigger previously registered with adis_probe_trigger().
145  */
146 void adis_remove_trigger(struct adis *adis)
147 {
148 	iio_trigger_unregister(adis->trig);
149 	free_irq(adis->spi->irq, adis->trig);
150 	iio_trigger_free(adis->trig);
151 }
152 EXPORT_SYMBOL_GPL(adis_remove_trigger);
153