1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2011 Analog Devices Inc.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <linux/list.h>
11 #include <linux/irq_work.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/trigger.h>
16 struct iio_sysfs_trig
{
17 struct iio_trigger
*trig
;
23 static LIST_HEAD(iio_sysfs_trig_list
);
24 static DEFINE_MUTEX(iio_sysfs_trig_list_mut
);
26 static int iio_sysfs_trigger_probe(int id
);
27 static ssize_t
iio_sysfs_trig_add(struct device
*dev
,
28 struct device_attribute
*attr
,
35 ret
= kstrtoul(buf
, 10, &input
);
38 ret
= iio_sysfs_trigger_probe(input
);
43 static DEVICE_ATTR(add_trigger
, S_IWUSR
, NULL
, &iio_sysfs_trig_add
);
45 static int iio_sysfs_trigger_remove(int id
);
46 static ssize_t
iio_sysfs_trig_remove(struct device
*dev
,
47 struct device_attribute
*attr
,
54 ret
= kstrtoul(buf
, 10, &input
);
57 ret
= iio_sysfs_trigger_remove(input
);
63 static DEVICE_ATTR(remove_trigger
, S_IWUSR
, NULL
, &iio_sysfs_trig_remove
);
65 static struct attribute
*iio_sysfs_trig_attrs
[] = {
66 &dev_attr_add_trigger
.attr
,
67 &dev_attr_remove_trigger
.attr
,
71 static const struct attribute_group iio_sysfs_trig_group
= {
72 .attrs
= iio_sysfs_trig_attrs
,
75 static const struct attribute_group
*iio_sysfs_trig_groups
[] = {
76 &iio_sysfs_trig_group
,
81 /* Nothing to actually do upon release */
82 static void iio_trigger_sysfs_release(struct device
*dev
)
86 static struct device iio_sysfs_trig_dev
= {
88 .groups
= iio_sysfs_trig_groups
,
89 .release
= &iio_trigger_sysfs_release
,
92 static void iio_sysfs_trigger_work(struct irq_work
*work
)
94 struct iio_sysfs_trig
*trig
= container_of(work
, struct iio_sysfs_trig
,
97 iio_trigger_poll(trig
->trig
);
100 static ssize_t
iio_sysfs_trigger_poll(struct device
*dev
,
101 struct device_attribute
*attr
, const char *buf
, size_t count
)
103 struct iio_trigger
*trig
= to_iio_trigger(dev
);
104 struct iio_sysfs_trig
*sysfs_trig
= iio_trigger_get_drvdata(trig
);
106 irq_work_queue(&sysfs_trig
->work
);
111 static DEVICE_ATTR(trigger_now
, S_IWUSR
, NULL
, iio_sysfs_trigger_poll
);
113 static struct attribute
*iio_sysfs_trigger_attrs
[] = {
114 &dev_attr_trigger_now
.attr
,
118 static const struct attribute_group iio_sysfs_trigger_attr_group
= {
119 .attrs
= iio_sysfs_trigger_attrs
,
122 static const struct attribute_group
*iio_sysfs_trigger_attr_groups
[] = {
123 &iio_sysfs_trigger_attr_group
,
127 static const struct iio_trigger_ops iio_sysfs_trigger_ops
= {
130 static int iio_sysfs_trigger_probe(int id
)
132 struct iio_sysfs_trig
*t
;
134 bool foundit
= false;
136 mutex_lock(&iio_sysfs_trig_list_mut
);
137 list_for_each_entry(t
, &iio_sysfs_trig_list
, l
)
146 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
152 t
->trig
= iio_trigger_alloc("sysfstrig%d", id
);
158 t
->trig
->dev
.groups
= iio_sysfs_trigger_attr_groups
;
159 t
->trig
->ops
= &iio_sysfs_trigger_ops
;
160 t
->trig
->dev
.parent
= &iio_sysfs_trig_dev
;
161 iio_trigger_set_drvdata(t
->trig
, t
);
163 t
->work
= IRQ_WORK_INIT_HARD(iio_sysfs_trigger_work
);
165 ret
= iio_trigger_register(t
->trig
);
168 list_add(&t
->l
, &iio_sysfs_trig_list
);
169 __module_get(THIS_MODULE
);
170 mutex_unlock(&iio_sysfs_trig_list_mut
);
174 iio_trigger_free(t
->trig
);
178 mutex_unlock(&iio_sysfs_trig_list_mut
);
182 static int iio_sysfs_trigger_remove(int id
)
184 bool foundit
= false;
185 struct iio_sysfs_trig
*t
;
187 mutex_lock(&iio_sysfs_trig_list_mut
);
188 list_for_each_entry(t
, &iio_sysfs_trig_list
, l
)
194 mutex_unlock(&iio_sysfs_trig_list_mut
);
198 iio_trigger_unregister(t
->trig
);
199 iio_trigger_free(t
->trig
);
203 module_put(THIS_MODULE
);
204 mutex_unlock(&iio_sysfs_trig_list_mut
);
209 static int __init
iio_sysfs_trig_init(void)
211 device_initialize(&iio_sysfs_trig_dev
);
212 dev_set_name(&iio_sysfs_trig_dev
, "iio_sysfs_trigger");
213 return device_add(&iio_sysfs_trig_dev
);
215 module_init(iio_sysfs_trig_init
);
217 static void __exit
iio_sysfs_trig_exit(void)
219 device_unregister(&iio_sysfs_trig_dev
);
221 module_exit(iio_sysfs_trig_exit
);
223 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
224 MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
225 MODULE_LICENSE("GPL v2");
226 MODULE_ALIAS("platform:iio-trig-sysfs");