blob: 254c7e906127f6152a51d273e57b0a5daa0fa950 [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;
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100138 mutex_lock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100139 list_for_each_entry(t, &iio_sysfs_trig_list, l)
140 if (id == t->id) {
141 foundit = true;
142 break;
143 }
144 if (foundit) {
145 ret = -EINVAL;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100146 goto out1;
147 }
Jonathan Cameron1f785682011-05-18 14:42:11 +0100148 t = kmalloc(sizeof(*t), GFP_KERNEL);
149 if (t == NULL) {
150 ret = -ENOMEM;
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100151 goto out1;
Jonathan Cameron1f785682011-05-18 14:42:11 +0100152 }
153 t->id = id;
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200154 t->trig = iio_trigger_alloc("sysfstrig%d", id);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100155 if (!t->trig) {
156 ret = -ENOMEM;
157 goto free_t;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100158 }
159
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100160 t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100161 t->trig->ops = &iio_sysfs_trigger_ops;
Jonathan Cameron1f785682011-05-18 14:42:11 +0100162 t->trig->dev.parent = &iio_sysfs_trig_dev;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000163 iio_trigger_set_drvdata(t->trig, t);
Lars-Peter Clausenf38bc922012-09-06 10:05:00 +0100164
165 init_irq_work(&t->work, iio_sysfs_trigger_work);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100166
167 ret = iio_trigger_register(t->trig);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100168 if (ret)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100169 goto out2;
170 list_add(&t->l, &iio_sysfs_trig_list);
171 __module_get(THIS_MODULE);
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100172 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100173 return 0;
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100174
Jonathan Cameron1f785682011-05-18 14:42:11 +0100175out2:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200176 iio_trigger_put(t->trig);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100177free_t:
178 kfree(t);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100179out1:
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100180 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100181 return ret;
182}
183
Jonathan Cameron1f785682011-05-18 14:42:11 +0100184static int iio_sysfs_trigger_remove(int id)
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100185{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100186 bool foundit = false;
187 struct iio_sysfs_trig *t;
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100188 mutex_lock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100189 list_for_each_entry(t, &iio_sysfs_trig_list, l)
190 if (id == t->id) {
191 foundit = true;
192 break;
193 }
194 if (!foundit) {
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100195 mutex_unlock(&iio_sysfs_trig_list_mut);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100196 return -EINVAL;
197 }
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100198
Jonathan Cameron1f785682011-05-18 14:42:11 +0100199 iio_trigger_unregister(t->trig);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200200 iio_trigger_free(t->trig);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100201
Jonathan Cameron1f785682011-05-18 14:42:11 +0100202 list_del(&t->l);
203 kfree(t);
204 module_put(THIS_MODULE);
Denis CIOCCA10a485c2013-09-23 11:49:00 +0100205 mutex_unlock(&iio_sysfs_trig_list_mut);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100206 return 0;
207}
208
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100209
210static int __init iio_sysfs_trig_init(void)
211{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100212 device_initialize(&iio_sysfs_trig_dev);
213 dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
214 return device_add(&iio_sysfs_trig_dev);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100215}
216module_init(iio_sysfs_trig_init);
217
218static void __exit iio_sysfs_trig_exit(void)
219{
Jonathan Cameron1f785682011-05-18 14:42:11 +0100220 device_unregister(&iio_sysfs_trig_dev);
Michael Hennerichf9a7e9b2011-02-07 11:05:46 +0100221}
222module_exit(iio_sysfs_trig_exit);
223
224MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
225MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
226MODULE_LICENSE("GPL v2");
227MODULE_ALIAS("platform:iio-trig-sysfs");