xref: /openbmc/linux/drivers/iio/imu/adis_trigger.c (revision ef22ccbc)
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 int adis_validate_irq_flag(struct adis *adis)
31 {
32 	unsigned long direction = adis->irq_flag & IRQF_TRIGGER_MASK;
33 	/*
34 	 * Typically this devices have data ready either on the rising edge or
35 	 * on the falling edge of the data ready pin. This checks enforces that
36 	 * one of those is set in the drivers... It defaults to
37 	 * IRQF_TRIGGER_RISING for backward compatibility with devices that
38 	 * don't support changing the pin polarity.
39 	 */
40 	if (direction == IRQF_TRIGGER_NONE) {
41 		adis->irq_flag |= IRQF_TRIGGER_RISING;
42 		return 0;
43 	} else if (direction != IRQF_TRIGGER_RISING &&
44 		   direction != IRQF_TRIGGER_FALLING) {
45 		dev_err(&adis->spi->dev, "Invalid IRQ mask: %08lx\n",
46 			adis->irq_flag);
47 		return -EINVAL;
48 	}
49 
50 	return 0;
51 }
52 
53 /**
54  * devm_adis_probe_trigger() - Sets up trigger for a managed adis device
55  * @adis: The adis device
56  * @indio_dev: The IIO device
57  *
58  * Returns 0 on success or a negative error code
59  */
60 int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev)
61 {
62 	int ret;
63 
64 	adis->trig = devm_iio_trigger_alloc(&adis->spi->dev, "%s-dev%d",
65 					    indio_dev->name, indio_dev->id);
66 	if (!adis->trig)
67 		return -ENOMEM;
68 
69 	adis->trig->ops = &adis_trigger_ops;
70 	iio_trigger_set_drvdata(adis->trig, adis);
71 
72 	ret = adis_validate_irq_flag(adis);
73 	if (ret)
74 		return ret;
75 
76 	ret = devm_request_irq(&adis->spi->dev, adis->spi->irq,
77 			       &iio_trigger_generic_data_rdy_poll,
78 			       adis->irq_flag,
79 			       indio_dev->name,
80 			       adis->trig);
81 	if (ret)
82 		return ret;
83 
84 	return devm_iio_trigger_register(&adis->spi->dev, adis->trig);
85 }
86 EXPORT_SYMBOL_GPL(devm_adis_probe_trigger);
87 
88