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 * struct iio_dummy_eventgen - event generator specific state
31 * @regs: irq regs we are faking
32 * @lock: protect the evgen state
33 * @inuse: mask of which irqs are connected
34 * @irq_sim_domain: irq simulator domain
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_domain *irq_sim_domain;
41};
42
43/* We can only ever have one instance of this 'device' */
44static struct iio_dummy_eventgen *iio_evgen;
45
46static int iio_dummy_evgen_create(void)
47{
48 int ret;
49
50 iio_evgen = kzalloc(size: sizeof(*iio_evgen), GFP_KERNEL);
51 if (!iio_evgen)
52 return -ENOMEM;
53
54 iio_evgen->irq_sim_domain = irq_domain_create_sim(NULL,
55 IIO_EVENTGEN_NO);
56 if (IS_ERR(ptr: iio_evgen->irq_sim_domain)) {
57 ret = PTR_ERR(ptr: iio_evgen->irq_sim_domain);
58 kfree(objp: iio_evgen);
59 return ret;
60 }
61
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_create_mapping(host: iio_evgen->irq_sim_domain, hwirq: i);
84 iio_evgen->inuse[i] = true;
85 break;
86 }
87 }
88 mutex_unlock(lock: &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 struct irq_data *irqd = irq_get_irq_data(irq);
105
106 mutex_lock(&iio_evgen->lock);
107 iio_evgen->inuse[irqd_to_hwirq(d: irqd)] = false;
108 irq_dispose_mapping(virq: irq);
109 mutex_unlock(lock: &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 struct irq_data *irqd = irq_get_irq_data(irq);
116
117 return &iio_evgen->regs[irqd_to_hwirq(d: irqd)];
118
119}
120EXPORT_SYMBOL_GPL(iio_dummy_evgen_get_regs);
121
122static void iio_dummy_evgen_free(void)
123{
124 irq_domain_remove_sim(domain: iio_evgen->irq_sim_domain);
125 kfree(objp: iio_evgen);
126}
127
128static void iio_evgen_release(struct device *dev)
129{
130 iio_dummy_evgen_free();
131}
132
133static ssize_t iio_evgen_poke(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf,
136 size_t len)
137{
138 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
139 unsigned long event;
140 int ret, irq;
141
142 ret = kstrtoul(s: buf, base: 10, res: &event);
143 if (ret)
144 return ret;
145
146 iio_evgen->regs[this_attr->address].reg_id = this_attr->address;
147 iio_evgen->regs[this_attr->address].reg_data = event;
148
149 irq = irq_find_mapping(domain: iio_evgen->irq_sim_domain, hwirq: this_attr->address);
150 ret = irq_set_irqchip_state(irq, which: IRQCHIP_STATE_PENDING, state: true);
151 if (ret)
152 return ret;
153
154 return len;
155}
156
157static IIO_DEVICE_ATTR(poke_ev0, S_IWUSR, NULL, &iio_evgen_poke, 0);
158static IIO_DEVICE_ATTR(poke_ev1, S_IWUSR, NULL, &iio_evgen_poke, 1);
159static IIO_DEVICE_ATTR(poke_ev2, S_IWUSR, NULL, &iio_evgen_poke, 2);
160static IIO_DEVICE_ATTR(poke_ev3, S_IWUSR, NULL, &iio_evgen_poke, 3);
161static IIO_DEVICE_ATTR(poke_ev4, S_IWUSR, NULL, &iio_evgen_poke, 4);
162static IIO_DEVICE_ATTR(poke_ev5, S_IWUSR, NULL, &iio_evgen_poke, 5);
163static IIO_DEVICE_ATTR(poke_ev6, S_IWUSR, NULL, &iio_evgen_poke, 6);
164static IIO_DEVICE_ATTR(poke_ev7, S_IWUSR, NULL, &iio_evgen_poke, 7);
165static IIO_DEVICE_ATTR(poke_ev8, S_IWUSR, NULL, &iio_evgen_poke, 8);
166static IIO_DEVICE_ATTR(poke_ev9, S_IWUSR, NULL, &iio_evgen_poke, 9);
167
168static struct attribute *iio_evgen_attrs[] = {
169 &iio_dev_attr_poke_ev0.dev_attr.attr,
170 &iio_dev_attr_poke_ev1.dev_attr.attr,
171 &iio_dev_attr_poke_ev2.dev_attr.attr,
172 &iio_dev_attr_poke_ev3.dev_attr.attr,
173 &iio_dev_attr_poke_ev4.dev_attr.attr,
174 &iio_dev_attr_poke_ev5.dev_attr.attr,
175 &iio_dev_attr_poke_ev6.dev_attr.attr,
176 &iio_dev_attr_poke_ev7.dev_attr.attr,
177 &iio_dev_attr_poke_ev8.dev_attr.attr,
178 &iio_dev_attr_poke_ev9.dev_attr.attr,
179 NULL,
180};
181
182static const struct attribute_group iio_evgen_group = {
183 .attrs = iio_evgen_attrs,
184};
185
186static const struct attribute_group *iio_evgen_groups[] = {
187 &iio_evgen_group,
188 NULL
189};
190
191static struct device iio_evgen_dev = {
192 .bus = &iio_bus_type,
193 .groups = iio_evgen_groups,
194 .release = &iio_evgen_release,
195};
196
197static __init int iio_dummy_evgen_init(void)
198{
199 int ret = iio_dummy_evgen_create();
200
201 if (ret < 0)
202 return ret;
203 device_initialize(dev: &iio_evgen_dev);
204 dev_set_name(dev: &iio_evgen_dev, name: "iio_evgen");
205 ret = device_add(dev: &iio_evgen_dev);
206 if (ret)
207 put_device(dev: &iio_evgen_dev);
208 return ret;
209}
210module_init(iio_dummy_evgen_init);
211
212static __exit void iio_dummy_evgen_exit(void)
213{
214 device_unregister(dev: &iio_evgen_dev);
215}
216module_exit(iio_dummy_evgen_exit);
217
218MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
219MODULE_DESCRIPTION("IIO dummy driver");
220MODULE_LICENSE("GPL v2");
221

source code of linux/drivers/iio/dummy/iio_dummy_evgen.c