1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Jonathan Cameron
5 * Companion module to the iio simple dummy example driver.
6 * The purpose of this is to generate 'fake' event interrupts thus
7 * allowing that driver's code to be as close as possible to that of
8 * a normal driver talking to hardware. The approach used here
9 * is not intended to be general and just happens to work for this
10 * particular use case.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/mutex.h>
18 #include <linux/module.h>
19 #include <linux/sysfs.h>
21 #include "iio_dummy_evgen.h"
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/irq_sim.h>
26 /* Fiddly bit of faking and irq without hardware */
27 #define IIO_EVENTGEN_NO 10
30 * @regs: irq regs we are faking
31 * @lock: protect the evgen state
32 * @inuse: mask of which irqs are connected
33 * @irq_sim: interrupt simulator
34 * @base: base of irq range
36 struct iio_dummy_eventgen {
37 struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
39 bool inuse[IIO_EVENTGEN_NO];
40 struct irq_sim irq_sim;
44 /* We can only ever have one instance of this 'device' */
45 static struct iio_dummy_eventgen *iio_evgen;
47 static int iio_dummy_evgen_create(void)
51 iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
55 ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
61 iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
62 mutex_init(&iio_evgen->lock);
68 * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
70 * This function will give a free allocated irq to a client device.
71 * That irq can then be caused to 'fire' by using the associated sysfs file.
73 int iio_dummy_evgen_get_irq(void)
80 mutex_lock(&iio_evgen->lock);
81 for (i = 0; i < IIO_EVENTGEN_NO; i++) {
82 if (!iio_evgen->inuse[i]) {
83 ret = irq_sim_irqnum(&iio_evgen->irq_sim, i);
84 iio_evgen->inuse[i] = true;
88 mutex_unlock(&iio_evgen->lock);
89 if (i == IIO_EVENTGEN_NO)
94 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
97 * iio_dummy_evgen_release_irq() - give the irq back.
98 * @irq: irq being returned to the pool
100 * Used by client driver instances to give the irqs back when they disconnect
102 void iio_dummy_evgen_release_irq(int irq)
104 mutex_lock(&iio_evgen->lock);
105 iio_evgen->inuse[irq - iio_evgen->base] = false;
106 mutex_unlock(&iio_evgen->lock);
108 EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
110 struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
112 return &iio_evgen->regs[irq - iio_evgen->base];
114 EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
116 static void iio_dummy_evgen_free(void)
118 irq_sim_fini(&iio_evgen->irq_sim);
122 static void iio_evgen_release(struct device *dev)
124 iio_dummy_evgen_free();
127 static ssize_t iio_evgen_poke(struct device *dev,
128 struct device_attribute *attr,
132 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
136 ret = kstrtoul(buf, 10, &event);
140 iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
141 iio_evgen->regs[this_attr->address].reg_data = event;
143 irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
148 static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
149 static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
150 static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
151 static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
152 static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
153 static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
154 static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
155 static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
156 static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
157 static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
159 static struct attribute *iio_evgen_attrs[] = {
160 &iio_dev_attr_poke_ev0.dev_attr.attr,
161 &iio_dev_attr_poke_ev1.dev_attr.attr,
162 &iio_dev_attr_poke_ev2.dev_attr.attr,
163 &iio_dev_attr_poke_ev3.dev_attr.attr,
164 &iio_dev_attr_poke_ev4.dev_attr.attr,
165 &iio_dev_attr_poke_ev5.dev_attr.attr,
166 &iio_dev_attr_poke_ev6.dev_attr.attr,
167 &iio_dev_attr_poke_ev7.dev_attr.attr,
168 &iio_dev_attr_poke_ev8.dev_attr.attr,
169 &iio_dev_attr_poke_ev9.dev_attr.attr,
173 static const struct attribute_group iio_evgen_group = {
174 .attrs = iio_evgen_attrs,
177 static const struct attribute_group *iio_evgen_groups[] = {
182 static struct device iio_evgen_dev = {
183 .bus = &iio_bus_type,
184 .groups = iio_evgen_groups,
185 .release = &iio_evgen_release,
188 static __init int iio_dummy_evgen_init(void)
190 int ret = iio_dummy_evgen_create();
194 device_initialize(&iio_evgen_dev);
195 dev_set_name(&iio_evgen_dev, "iio_evgen");
196 ret = device_add(&iio_evgen_dev);
198 put_device(&iio_evgen_dev);
201 module_init(iio_dummy_evgen_init);
203 static __exit void iio_dummy_evgen_exit(void)
205 device_unregister(&iio_evgen_dev);
207 module_exit(iio_dummy_evgen_exit);
209 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
210 MODULE_DESCRIPTION("IIO dummy driver");
211 MODULE_LICENSE("GPL v2");