blob: 3dfab2bc6d6935368a6cec8cd5b45617a921bb0a [file] [log] [blame]
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +01001/*
2 * Copyright 2011 Analog Devices Inc.
3 *
4 * Licensed under the GPL-2.
5 *
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/slab.h>
Jonathan Cameron1f785682011-05-18 14:42:11 +010012#include <linux/list.h>
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +010013#include <linux/irq_work.h>
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +010014
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/iio.h>
16#include <linux/iio/trigger.h>
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +010017
Jonathan Cameron1f785682011-05-18 14:42:11 +010018struct iio_sysfs_trig {
19 struct iio_trigger *trig;
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +010020 struct irq_work work;
Jonathan Cameron1f785682011-05-18 14:42:11 +010021 int id;
22 struct list_head l;
23};
24
25static LIST_HEAD(iio_sysfs_trig_list);
Denis CIOCCA10a485c2013-09-23 11:49:00 +010026static DEFINE_MUTEX(iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +010027
28static int iio_sysfs_trigger_probe(int id);
29static ssize_t iio_sysfs_trig_add(struct device *dev,
30 struct device_attribute *attr,
31 const char *buf,
32 size_t len)
33{
34 int ret;
35 unsigned long input;
36
Jingoo Handdeb64f2013-06-01 08:10:00 +010037 ret = kstrtoul(buf, 10, &input);
Jonathan Cameron1f785682011-05-18 14:42:11 +010038 if (ret)
39 return ret;
40 ret = iio_sysfs_trigger_probe(input);
41 if (ret)
42 return ret;
43 return len;
44}
45static DEVICE_ATTR(add_trigger, S_IWUSR, NULL, &iio_sysfs_trig_add);
46
47static int iio_sysfs_trigger_remove(int id);
48static ssize_t iio_sysfs_trig_remove(struct device *dev,
49 struct device_attribute *attr,
50 const char *buf,
51 size_t len)
52{
53 int ret;
54 unsigned long input;
55
Jingoo Handdeb64f2013-06-01 08:10:00 +010056 ret = kstrtoul(buf, 10, &input);
Jonathan Cameron1f785682011-05-18 14:42:11 +010057 if (ret)
58 return ret;
59 ret = iio_sysfs_trigger_remove(input);
60 if (ret)
61 return ret;
62 return len;
63}
64
65static DEVICE_ATTR(remove_trigger, S_IWUSR, NULL, &iio_sysfs_trig_remove);
66
67static struct attribute *iio_sysfs_trig_attrs[] = {
68 &dev_attr_add_trigger.attr,
69 &dev_attr_remove_trigger.attr,
70 NULL,
71};
72
73static const struct attribute_group iio_sysfs_trig_group = {
74 .attrs = iio_sysfs_trig_attrs,
75};
76
77static const struct attribute_group *iio_sysfs_trig_groups[] = {
78 &iio_sysfs_trig_group,
79 NULL
80};
81
Jonathan Cameron9c0c22b2011-08-24 17:28:35 +010082
83/* Nothing to actually do upon release */
84static void iio_trigger_sysfs_release(struct device *dev)
85{
86}
87
Jonathan Cameron1f785682011-05-18 14:42:11 +010088static struct device iio_sysfs_trig_dev = {
89 .bus = &iio_bus_type,
90 .groups = iio_sysfs_trig_groups,
Jonathan Cameron9c0c22b2011-08-24 17:28:35 +010091 .release = &iio_trigger_sysfs_release,
Jonathan Cameron1f785682011-05-18 14:42:11 +010092};
93
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +010094static void iio_sysfs_trigger_work(struct irq_work *work)
95{
96 struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig,
97 work);
98
Peter Meerwald398fd222014-12-06 06:46:00 +000099 iio_trigger_poll(trig->trig);
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +0100100}
101
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100102static ssize_t iio_sysfs_trigger_poll(struct device *dev,
103 struct device_attribute *attr, const char *buf, size_t count)
104{
Lars-Peter Clausen4bf81722012-06-21 19:10:59 +0200105 struct iio_trigger *trig = to_iio_trigger(dev);
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000106 struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig);
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +0100107
108 irq_work_queue(&sysfs_trig->work);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100109
110 return count;
111}
112
113static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100114
115static struct attribute *iio_sysfs_trigger_attrs[] = {
116 &dev_attr_trigger_now.attr,
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100117 NULL,
118};
119
120static const struct attribute_group iio_sysfs_trigger_attr_group = {
121 .attrs = iio_sysfs_trigger_attrs,
122};
123
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100124static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = {
125 &iio_sysfs_trigger_attr_group,
126 NULL
127};
128
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100129static const struct iio_trigger_ops iio_sysfs_trigger_ops = {
130 .owner = THIS_MODULE,
131};
132
Jonathan Cameron1f785682011-05-18 14:42:11 +0100133static int iio_sysfs_trigger_probe(int id)
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100134{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100135 struct iio_sysfs_trig *t;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100136 int ret;
Jonathan Cameron1f785682011-05-18 14:42:11 +0100137 bool foundit = false;
Roberta Dobrescu450a5ff2014-12-16 11:16:06 +0200138
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100139 mutex_lock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100140 list_for_each_entry(t, &iio_sysfs_trig_list, l)
141 if (id == t->id) {
142 foundit = true;
143 break;
144 }
145 if (foundit) {
146 ret = -EINVAL;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100147 goto out1;
148 }
Jonathan Cameron1f785682011-05-18 14:42:11 +0100149 t = kmalloc(sizeof(*t), GFP_KERNEL);
150 if (t == NULL) {
151 ret = -ENOMEM;
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100152 goto out1;
Jonathan Cameron1f785682011-05-18 14:42:11 +0100153 }
154 t->id = id;
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200155 t->trig = iio_trigger_alloc("sysfstrig%d", id);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100156 if (!t->trig) {
157 ret = -ENOMEM;
158 goto free_t;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100159 }
160
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100161 t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100162 t->trig->ops = &iio_sysfs_trigger_ops;
Jonathan Cameron1f785682011-05-18 14:42:11 +0100163 t->trig->dev.parent = &iio_sysfs_trig_dev;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000164 iio_trigger_set_drvdata(t->trig, t);
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +0100165
166 init_irq_work(&t->work, iio_sysfs_trigger_work);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100167
168 ret = iio_trigger_register(t->trig);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100169 if (ret)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100170 goto out2;
171 list_add(&t->l, &iio_sysfs_trig_list);
172 __module_get(THIS_MODULE);
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100173 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100174 return 0;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100175
Jonathan Cameron1f785682011-05-18 14:42:11 +0100176out2:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200177 iio_trigger_put(t->trig);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100178free_t:
179 kfree(t);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100180out1:
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100181 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100182 return ret;
183}
184
Jonathan Cameron1f785682011-05-18 14:42:11 +0100185static int iio_sysfs_trigger_remove(int id)
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100186{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100187 bool foundit = false;
188 struct iio_sysfs_trig *t;
Roberta Dobrescu450a5ff2014-12-16 11:16:06 +0200189
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100190 mutex_lock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100191 list_for_each_entry(t, &iio_sysfs_trig_list, l)
192 if (id == t->id) {
193 foundit = true;
194 break;
195 }
196 if (!foundit) {
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100197 mutex_unlock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100198 return -EINVAL;
199 }
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100200
Jonathan Cameron1f785682011-05-18 14:42:11 +0100201 iio_trigger_unregister(t->trig);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200202 iio_trigger_free(t->trig);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100203
Jonathan Cameron1f785682011-05-18 14:42:11 +0100204 list_del(&t->l);
205 kfree(t);
206 module_put(THIS_MODULE);
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100207 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100208 return 0;
209}
210
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100211
212static int __init iio_sysfs_trig_init(void)
213{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100214 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);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100217}
218module_init(iio_sysfs_trig_init);
219
220static void __exit iio_sysfs_trig_exit(void)
221{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100222 device_unregister(&iio_sysfs_trig_dev);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100223}
224module_exit(iio_sysfs_trig_exit);
225
226MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
227MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
228MODULE_LICENSE("GPL v2");
229MODULE_ALIAS("platform:iio-trig-sysfs");