Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/**
3 * Copyright (c) 2011 Jonathan Cameron
4 *
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.
11 */
12
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>
20
21#include "iio_dummy_evgen.h"
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/irq_sim.h>
25
26/* Fiddly bit of faking and irq without hardware */
27#define IIO_EVENTGEN_NO 10
28
29/**
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
35 */
36struct iio_dummy_eventgen {
37 struct iio_dummy_regs regs[IIO_EVENTGEN_NO];
38 struct mutex lock;
39 bool inuse[IIO_EVENTGEN_NO];
40 struct irq_sim irq_sim;
41 int base;
42};
43
44/* We can only ever have one instance of this 'device' */
45static struct iio_dummy_eventgen *iio_evgen;
46
47static int iio_dummy_evgen_create(void)
48{
49 int ret;
50
51 iio_evgen = kzalloc(sizeof(*iio_evgen), GFP_KERNEL);
52 if (!iio_evgen)
53 return -ENOMEM;
54
55 ret = irq_sim_init(&iio_evgen->irq_sim, IIO_EVENTGEN_NO);
56 if (ret < 0) {
57 kfree(iio_evgen);
58 return ret;
59 }
60
61 iio_evgen->base = irq_sim_irqnum(&iio_evgen->irq_sim, 0);
62 mutex_init(&iio_evgen->lock);
63
64 return 0;
65}
66
67/**
68 * iio_dummy_evgen_get_irq() - get an evgen provided irq for a device
69 *
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.
72 */
73int iio_dummy_evgen_get_irq(void)
74{
75 int i, ret = 0;
76
77 if (!iio_evgen)
78 return -ENODEV;
79
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;
85 break;
86 }
87 }
88 mutex_unlock(&iio_evgen->lock);
89 if (i == IIO_EVENTGEN_NO)
90 return -ENOMEM;
91
92 return ret;
93}
94EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_irq);
95
96/**
97 * iio_dummy_evgen_release_irq() - give the irq back.
98 * @irq: irq being returned to the pool
99 *
100 * Used by client driver instances to give the irqs back when they disconnect
101 */
102void iio_dummy_evgen_release_irq(int irq)
103{
104 mutex_lock(&iio_evgen->lock);
105 iio_evgen->inuse[irq - iio_evgen->base] = false;
106 mutex_unlock(&iio_evgen->lock);
107}
108EXPORT_SYMBOL_GPL(iio_dummy_evgen_release_irq);
109
110struct iio_dummy_regs *iio_dummy_evgen_get_regs(int irq)
111{
112 return &iio_evgen->regs[irq - iio_evgen->base];
113}
114EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
115
116static void iio_dummy_evgen_free(void)
117{
118 irq_sim_fini(&iio_evgen->irq_sim);
119 kfree(iio_evgen);
120}
121
122static void iio_evgen_release(struct device *dev)
123{
124 iio_dummy_evgen_free();
125}
126
127static ssize_t iio_evgen_poke(struct device *dev,
128 struct device_attribute *attr,
129 const char *buf,
130 size_t len)
131{
132 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
133 unsigned long event;
134 int ret;
135
136 ret = kstrtoul(buf, 10, &event);
137 if (ret)
138 return ret;
139
140 iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
141 iio_evgen->regs[this_attr->address].reg_data = event;
142
143 irq_sim_fire(&iio_evgen->irq_sim, this_attr->address);
144
145 return len;
146}
147
148static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
149static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
150static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
151static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
152static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
153static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
154static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
155static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
156static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
157static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
158
159static 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,
170 NULL,
171};
172
173static const struct attribute_group iio_evgen_group = {
174 .attrs = iio_evgen_attrs,
175};
176
177static const struct attribute_group *iio_evgen_groups[] = {
178 &iio_evgen_group,
179 NULL
180};
181
182static struct device iio_evgen_dev = {
183 .bus = &iio_bus_type,
184 .groups = iio_evgen_groups,
185 .release = &iio_evgen_release,
186};
187
188static __init int iio_dummy_evgen_init(void)
189{
190 int ret = iio_dummy_evgen_create();
191
192 if (ret < 0)
193 return ret;
194 device_initialize(&iio_evgen_dev);
195 dev_set_name(&iio_evgen_dev, "iio_evgen");
196 ret = device_add(&iio_evgen_dev);
197 if (ret)
198 put_device(&iio_evgen_dev);
199 return ret;
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");