1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/mutex.h>
21#include <linux/module.h>
22#include <linux/sysfs.h>
23
24#include "iio_dummy_evgen.h"
25#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
27#include <linux/irq_sim.h>
28
29
30#define IIO_EVENTGEN_NO 10
31
32
33
34
35
36
37
38
39struct iio_dummy_eventgen {
40 struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
41 struct mutex lock;
42 bool inuse[IIO_EVENTGEN_NO];
43 struct irq_sim irq_sim;
44 int base;
45};
46
47
48static struct iio_dummy_eventgen *iio_evgen;
49
50static int iio_dummy_evgen_create(void)
51{
52 int ret;
53
54 iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
55 if (!iio_evgen)
56 return -ENOMEM;
57
58 ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
59 if (ret < 0) {
60 kfree(iio_evgen);
61 return ret;
62 }
63
64 iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
65 mutex_init(&iio_evgen->lock);
66
67 return 0;
68}
69
70
71
72
73
74
75
76int iio_dummy_evgen_get_irq(void)
77{
78 int i, ret = 0;
79
80 if (!iio_evgen)
81 return -ENODEV;
82
83 mutex_lock(&iio_evgen->lock);
84 for (i = 0; i < IIO_EVENTGEN_NO; i++) {
85 if (!iio_evgen->inuse[i]) {
86 ret = irq_sim_irqnum(&iio_evgen->irq_sim, i);
87 iio_evgen->inuse[i] = true;
88 break;
89 }
90 }
91 mutex_unlock(&iio_evgen->lock);
92 if (i == IIO_EVENTGEN_NO)
93 return -ENOMEM;
94
95 return ret;
96}
97EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
98
99
100
101
102
103
104
105void iio_dummy_evgen_release_irq(int irq)
106{
107 mutex_lock(&iio_evgen->lock);
108 iio_evgen->inuse[irq - iio_evgen->base] = false;
109 mutex_unlock(&iio_evgen->lock);
110}
111EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
112
113struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
114{
115 return &iio_evgen->regs[irq - iio_evgen->base];
116}
117EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
118
119static void iio_dummy_evgen_free(void)
120{
121 irq_sim_fini(&iio_evgen->irq_sim);
122 kfree(iio_evgen);
123}
124
125static void iio_evgen_release(struct device *dev)
126{
127 iio_dummy_evgen_free();
128}
129
130static ssize_t iio_evgen_poke(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf,
133 size_t len)
134{
135 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
136 unsigned long event;
137 int ret;
138
139 ret = kstrtoul(buf, 10, &event);
140 if (ret)
141 return ret;
142
143 iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
144 iio_evgen->regs[this_attr->address].reg_data = event;
145
146 irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
147
148 return len;
149}
150
151static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
152static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
153static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
154static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
155static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
156static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
157static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
158static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
159static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
160static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
161
162static struct attribute *iio_evgen_attrs[] = {
163 &iio_dev_attr_poke_ev0.dev_attr.attr,
164 &iio_dev_attr_poke_ev1.dev_attr.attr,
165 &iio_dev_attr_poke_ev2.dev_attr.attr,
166 &iio_dev_attr_poke_ev3.dev_attr.attr,
167 &iio_dev_attr_poke_ev4.dev_attr.attr,
168 &iio_dev_attr_poke_ev5.dev_attr.attr,
169 &iio_dev_attr_poke_ev6.dev_attr.attr,
170 &iio_dev_attr_poke_ev7.dev_attr.attr,
171 &iio_dev_attr_poke_ev8.dev_attr.attr,
172 &iio_dev_attr_poke_ev9.dev_attr.attr,
173 NULL,
174};
175
176static const struct attribute_group iio_evgen_group = {
177 .attrs = iio_evgen_attrs,
178};
179
180static const struct attribute_group *iio_evgen_groups[] = {
181 &iio_evgen_group,
182 NULL
183};
184
185static struct device iio_evgen_dev = {
186 .bus = &iio_bus_type,
187 .groups = iio_evgen_groups,
188 .release = &iio_evgen_release,
189};
190
191static __init int iio_dummy_evgen_init(void)
192{
193 int ret = iio_dummy_evgen_create();
194
195 if (ret < 0)
196 return ret;
197 device_initialize(&iio_evgen_dev);
198 dev_set_name(&iio_evgen_dev, "iio_evgen");
199 return device_add(&iio_evgen_dev);
200}
201module_init(iio_dummy_evgen_init);
202
203static __exit void iio_dummy_evgen_exit(void)
204{
205 device_unregister(&iio_evgen_dev);
206}
207module_exit(iio_dummy_evgen_exit);
208
209MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
210MODULE_DESCRIPTION("IIO dummy driver");
211MODULE_LICENSE("GPL v2");
212