2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/irq_work.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/trigger.h>
18 struct iio_sysfs_trig {
19 struct iio_trigger *trig;
25 static LIST_HEAD(iio_sysfs_trig_list);
26 static DEFINE_MUTEX(iio_sysfs_trig_list_mut);
28 static int iio_sysfs_trigger_probe(int id);
29 static ssize_t iio_sysfs_trig_add(struct device *dev,
30 struct device_attribute *attr,
37 ret = kstrtoul(buf, 10, &input);
40 ret = iio_sysfs_trigger_probe(input);
45 static DEVICE_ATTR(add_trigger, S_IWUSR, NULL, &iio_sysfs_trig_add);
47 static int iio_sysfs_trigger_remove(int id);
48 static ssize_t iio_sysfs_trig_remove(struct device *dev,
49 struct device_attribute *attr,
56 ret = kstrtoul(buf, 10, &input);
59 ret = iio_sysfs_trigger_remove(input);
65 static DEVICE_ATTR(remove_trigger, S_IWUSR, NULL, &iio_sysfs_trig_remove);
67 static struct attribute *iio_sysfs_trig_attrs[] = {
68 &dev_attr_add_trigger.attr,
69 &dev_attr_remove_trigger.attr,
73 static const struct attribute_group iio_sysfs_trig_group = {
74 .attrs = iio_sysfs_trig_attrs,
77 static const struct attribute_group *iio_sysfs_trig_groups[] = {
78 &iio_sysfs_trig_group,
83 /* Nothing to actually do upon release */
84 static void iio_trigger_sysfs_release(struct device *dev)
88 static struct device iio_sysfs_trig_dev = {
90 .groups = iio_sysfs_trig_groups,
91 .release = &iio_trigger_sysfs_release,
94 static void iio_sysfs_trigger_work(struct irq_work *work)
96 struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig,
99 iio_trigger_poll(trig->trig);
102 static ssize_t iio_sysfs_trigger_poll(struct device *dev,
103 struct device_attribute *attr, const char *buf, size_t count)
105 struct iio_trigger *trig = to_iio_trigger(dev);
106 struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig);
108 irq_work_queue(&sysfs_trig->work);
113 static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll);
115 static struct attribute *iio_sysfs_trigger_attrs[] = {
116 &dev_attr_trigger_now.attr,
120 static const struct attribute_group iio_sysfs_trigger_attr_group = {
121 .attrs = iio_sysfs_trigger_attrs,
124 static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = {
125 &iio_sysfs_trigger_attr_group,
129 static const struct iio_trigger_ops iio_sysfs_trigger_ops = {
130 .owner = THIS_MODULE,
133 static int iio_sysfs_trigger_probe(int id)
135 struct iio_sysfs_trig *t;
137 bool foundit = false;
139 mutex_lock(&iio_sysfs_trig_list_mut);
140 list_for_each_entry(t, &iio_sysfs_trig_list, l)
149 t = kmalloc(sizeof(*t), GFP_KERNEL);
155 t->trig = iio_trigger_alloc("sysfstrig%d", id);
161 t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
162 t->trig->ops = &iio_sysfs_trigger_ops;
163 t->trig->dev.parent = &iio_sysfs_trig_dev;
164 iio_trigger_set_drvdata(t->trig, t);
166 init_irq_work(&t->work, iio_sysfs_trigger_work);
168 ret = iio_trigger_register(t->trig);
171 list_add(&t->l, &iio_sysfs_trig_list);
172 __module_get(THIS_MODULE);
173 mutex_unlock(&iio_sysfs_trig_list_mut);
177 iio_trigger_free(t->trig);
181 mutex_unlock(&iio_sysfs_trig_list_mut);
185 static int iio_sysfs_trigger_remove(int id)
187 bool foundit = false;
188 struct iio_sysfs_trig *t;
190 mutex_lock(&iio_sysfs_trig_list_mut);
191 list_for_each_entry(t, &iio_sysfs_trig_list, l)
197 mutex_unlock(&iio_sysfs_trig_list_mut);
201 iio_trigger_unregister(t->trig);
202 iio_trigger_free(t->trig);
206 module_put(THIS_MODULE);
207 mutex_unlock(&iio_sysfs_trig_list_mut);
212 static int __init iio_sysfs_trig_init(void)
214 device_initialize(&iio_sysfs_trig_dev);
215 dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
216 return device_add(&iio_sysfs_trig_dev);
218 module_init(iio_sysfs_trig_init);
220 static void __exit iio_sysfs_trig_exit(void)
222 device_unregister(&iio_sysfs_trig_dev);
224 module_exit(iio_sysfs_trig_exit);
226 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
227 MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
228 MODULE_LICENSE("GPL v2");
229 MODULE_ALIAS("platform:iio-trig-sysfs");