1 #include <linux/interrupt.h>
2 #include <linux/irq.h>
3 #include <linux/mutex.h>
4 #include <linux/device.h>
5 #include <linux/kernel.h>
6 #include <linux/sysfs.h>
7 #include <linux/list.h>
8 #include <linux/spi/spi.h>
9 
10 #include "../iio.h"
11 #include "../sysfs.h"
12 #include "../trigger.h"
13 #include "adis16260.h"
14 
15 /**
16  * adis16260_data_rdy_trig_poll() the event handler for the data rdy trig
17  **/
adis16260_data_rdy_trig_poll(struct iio_dev * dev_info,int index,s64 timestamp,int no_test)18 static int adis16260_data_rdy_trig_poll(struct iio_dev *dev_info,
19 				       int index,
20 				       s64 timestamp,
21 				       int no_test)
22 {
23 	struct adis16260_state *st = iio_dev_get_devdata(dev_info);
24 	struct iio_trigger *trig = st->trig;
25 
26 	iio_trigger_poll(trig, timestamp);
27 
28 	return IRQ_HANDLED;
29 }
30 
31 IIO_EVENT_SH(data_rdy_trig, &adis16260_data_rdy_trig_poll);
32 
33 static IIO_TRIGGER_NAME_ATTR;
34 
35 static struct attribute *adis16260_trigger_attrs[] = {
36 	&dev_attr_name.attr,
37 	NULL,
38 };
39 
40 static const struct attribute_group adis16260_trigger_attr_group = {
41 	.attrs = adis16260_trigger_attrs,
42 };
43 
44 /**
45  * adis16260_data_rdy_trigger_set_state() set datardy interrupt state
46  **/
adis16260_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)47 static int adis16260_data_rdy_trigger_set_state(struct iio_trigger *trig,
48 						bool state)
49 {
50 	struct adis16260_state *st = trig->private_data;
51 	struct iio_dev *indio_dev = st->indio_dev;
52 	int ret = 0;
53 
54 	dev_dbg(&indio_dev->dev, "%s (%d)\n", __func__, state);
55 	ret = adis16260_set_irq(&st->indio_dev->dev, state);
56 	if (state == false) {
57 		iio_remove_event_from_list(&iio_event_data_rdy_trig,
58 					   &indio_dev->interrupts[0]
59 					   ->ev_list);
60 		flush_scheduled_work();
61 	} else {
62 		iio_add_event_to_list(&iio_event_data_rdy_trig,
63 				      &indio_dev->interrupts[0]->ev_list);
64 	}
65 	return ret;
66 }
67 
68 /**
69  * adis16260_trig_try_reen() try renabling irq for data rdy trigger
70  * @trig:	the datardy trigger
71  **/
adis16260_trig_try_reen(struct iio_trigger * trig)72 static int adis16260_trig_try_reen(struct iio_trigger *trig)
73 {
74 	struct adis16260_state *st = trig->private_data;
75 	enable_irq(st->us->irq);
76 	return 0;
77 }
78 
adis16260_probe_trigger(struct iio_dev * indio_dev)79 int adis16260_probe_trigger(struct iio_dev *indio_dev)
80 {
81 	int ret;
82 	struct adis16260_state *st = indio_dev->dev_data;
83 
84 	st->trig = iio_allocate_trigger();
85 	st->trig->name = kasprintf(GFP_KERNEL,
86 				   "adis16260-dev%d",
87 				   indio_dev->id);
88 	if (!st->trig->name) {
89 		ret = -ENOMEM;
90 		goto error_free_trig;
91 	}
92 	st->trig->dev.parent = &st->us->dev;
93 	st->trig->owner = THIS_MODULE;
94 	st->trig->private_data = st;
95 	st->trig->set_trigger_state = &adis16260_data_rdy_trigger_set_state;
96 	st->trig->try_reenable = &adis16260_trig_try_reen;
97 	st->trig->control_attrs = &adis16260_trigger_attr_group;
98 	ret = iio_trigger_register(st->trig);
99 
100 	/* select default trigger */
101 	indio_dev->trig = st->trig;
102 	if (ret)
103 		goto error_free_trig_name;
104 
105 	return 0;
106 
107 error_free_trig_name:
108 	kfree(st->trig->name);
109 error_free_trig:
110 	iio_free_trigger(st->trig);
111 
112 	return ret;
113 }
114 
adis16260_remove_trigger(struct iio_dev * indio_dev)115 void adis16260_remove_trigger(struct iio_dev *indio_dev)
116 {
117 	struct adis16260_state *state = indio_dev->dev_data;
118 
119 	iio_trigger_unregister(state->trig);
120 	kfree(state->trig->name);
121 	iio_free_trigger(state->trig);
122 }
123