forked from daniel-santos/mcp2210-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp2210-irq.c
304 lines (250 loc) · 7.82 KB
/
mcp2210-irq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* MCP2210 software IRQ controller
*
* Copyright (c) 2013-2017 Daniel Santos <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mcp2210.h"
#include "mcp2210-debug.h"
#ifdef CONFIG_MCP2210_IRQ
#include <linux/irq.h>
static int complete_poll(struct mcp2210_cmd *cmd, void *context);
static void mcp2210_irq_mask(struct irq_data *data) {
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
dev->irq_mask |= data->mask;
}
static void mcp2210_irq_unmask(struct irq_data *data) {
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
dev->irq_mask &= ~data->mask;
}
static int mcp2210_irq_set_type(struct irq_data *data, unsigned int flow_type) {
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
BUG_ON(data->hwirq > 7);
dev->irq_type[data->hwirq] = flow_type;
mcp2210_info("irq type set to 0x%02x", flow_type);
return 0;
}
static struct irq_chip mcp2210_irq_chip = {
.name = "mcp2210",
.irq_mask = mcp2210_irq_mask,
.irq_unmask = mcp2210_irq_unmask,
.irq_set_type = mcp2210_irq_set_type,
};
/******************************************************************************
* probe & remove
*/
int mcp2210_irq_probe(struct mcp2210_device *dev)
{
uint i;
int ret;
mcp2210_info();
mutex_init(&dev->irq_lock);
dev->nr_irqs = 0;
dev->poll_intr = 0;
dev->poll_gpio = 0;
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &dev->config->pins[i];
if (pin->mode == MCP2210_PIN_SPI || !pin->has_irq)
continue;
++dev->nr_irqs;
BUG_ON(dev->irq_revmap[i]);
dev->irq_revmap[i] = pin->irq;
if (pin->mode == MCP2210_PIN_DEDICATED)
dev->poll_intr = 1;
else if (pin->mode == MCP2210_PIN_GPIO) {
dev->poll_gpio = 1;
dev->irq_type[i] = pin->irq_type;
}
}
if (!dev->nr_irqs)
return 0;
ret = irq_alloc_descs(-1, 0, dev->nr_irqs, 0);
if (ret < 0) {
/* CONFIG_SPARSE_IRQ needed? */
mcp2210_err("Failed to allocate %u irq descriptors: %d", dev->nr_irqs, ret);
return ret;
}
dev->irq_base = ret;
for (i = 0; i < dev->nr_irqs; ++i) {
int virq = dev->irq_base + i;
dev->irq_descs[i] = irq_to_desc(virq);
BUG_ON(!dev->irq_descs[i]);
irq_set_chip_data(virq, dev);
irq_set_chip(virq, &mcp2210_irq_chip);
#if defined(CONFIG_ARM) && LINUX_VERSION_CODE < KERNEL_VERSION(4,3,0)
set_irq_flags(virq, 0);
#else
irq_set_noprobe(virq);
#endif
}
#ifdef CONFIG_MCP2210_GPIO
if (dev->poll_gpio) {
ctl_cmd_init(dev, &dev->cmd_poll_gpio,
MCP2210_CMD_GET_PIN_VALUE, 0, NULL, 0, false);
dev->cmd_poll_gpio.head.complete = complete_poll;
mcp2210_add_cmd(&dev->cmd_poll_gpio.head, false);
}
#endif /* CONFIG_MCP2210_GPIO */
if (dev->poll_intr) {
/* read and then reset */
ctl_cmd_init(dev, &dev->cmd_poll_intr,
MCP2210_CMD_GET_INTERRUPTS, 0, NULL, 0, false);
dev->cmd_poll_intr.head.complete = complete_poll;
mcp2210_add_cmd(&dev->cmd_poll_intr.head, false);
}
dev->is_irq_probed = 1;
dev->suppress_poll_warn = 0;
return 0;
}
void mcp2210_irq_disable(struct mcp2210_device *dev)
{
mcp2210_info();
if (dev->is_irq_probed) {
const int virq_end = dev->irq_base + dev->nr_irqs;
int virq;
for (virq = dev->irq_base; virq < virq_end; ++virq) {
irq_set_status_flags(virq, IRQ_NOREQUEST);
irq_set_chip_and_handler(virq, NULL, NULL);
synchronize_irq(virq);
}
}
}
void mcp2210_irq_remove(struct mcp2210_device *dev)
{
mcp2210_info();
if (dev->is_irq_probed) {
const int virq_end = dev->irq_base + dev->nr_irqs;
int virq;
for (virq = dev->irq_base; virq < virq_end; ++virq) {
irq_free_desc(virq);
dev->irq_descs[virq] = NULL;
}
dev->is_irq_probed = 0;
}
}
/******************************************************************************
* polling and virtual IRQ trigger functions
*/
void mcp2210_irq_do_gpio(struct mcp2210_device *dev, u16 old_val, u16 new_val)
{
uint i;
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
struct mcp2210_pin_config *pin = &dev->config->pins[i];
int old_pin_val;
int new_pin_val;
int edge_mask, level_mask;
struct irq_desc *desc;
if (!pin->has_irq || pin->mode != MCP2210_PIN_GPIO)
continue;
old_pin_val = old_val & (1 << i);
new_pin_val = new_val & (1 << i);
level_mask = new_pin_val ? IRQ_TYPE_LEVEL_HIGH
: IRQ_TYPE_LEVEL_LOW;
desc = dev->irq_descs[pin->irq];
if (new_pin_val > old_val)
edge_mask = IRQ_TYPE_EDGE_RISING;
else if (new_pin_val < old_val)
edge_mask = IRQ_TYPE_EDGE_FALLING;
else
edge_mask = 0;
if (pin->irq_type & edge_mask) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
handle_simple_irq(desc);
#else
handle_simple_irq(dev->irq_base + pin->irq, desc);
#endif
}
if (pin->irq_type & level_mask) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
handle_level_irq(desc);
#else
handle_level_irq(dev->irq_base + pin->irq, desc);
#endif
}
}
}
void _mcp2210_irq_do_intr_counter(struct mcp2210_device *dev, u16 count)
{
struct mcp2210_pin_config *pin = &dev->config->pins[6];
struct irq_desc *desc = dev->irq_descs[pin->irq];
if (dev->s.chip_settings.pin_mode[6] != MCP2210_PIN_DEDICATED)
return;
/* We're discarding count and just letting handlers know that at least
* one interrupt occured. Should this have a mechanism to report the
* interrupt once for each count? */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
handle_simple_irq(desc);
#else
handle_simple_irq(dev->irq_base + pin->irq, desc);
#endif
}
static void warn_poll_past_due(struct mcp2210_device *dev, unsigned long now,
long how_late)
{
mcp2210_warn("Next poll is past due by %ums.\n",
jiffies_to_msecs(how_late));
if (!poll_delay_warn_secs)
return;
/* If result ends up zero then it'll just repeat once */
dev->suppress_poll_warn = now + msecs_to_jiffies(1000
* poll_delay_warn_secs);
mcp2210_warn("...suppressing for %u seconds.\n", poll_delay_warn_secs);
}
static int complete_poll(struct mcp2210_cmd *cmd_head, void *context)
{
struct mcp2210_device *dev = cmd_head->dev;
struct mcp2210_cmd_ctl *cmd = (void*)cmd_head;
unsigned long now = jiffies;
int enabled;
unsigned long interval;
unsigned long interval_j;
unsigned long next;
long next_diff;
mcp2210_debug();
if (dev->dead)
return -EINPROGRESS;
if (cmd->req.cmd == MCP2210_CMD_GET_PIN_VALUE) {
enabled = dev->poll_gpio;
interval = dev->config->poll_gpio_usecs;
dev->last_poll_gpio = now;
} else {
enabled = dev->poll_intr;
interval = dev->config->poll_intr_usecs;
dev->last_poll_intr = now;
}
if (dev->suppress_poll_warn && jiffdiff(dev->suppress_poll_warn, now) <= 0)
dev->suppress_poll_warn = 0;
if (!enabled)
return -EINPROGRESS;
interval_j = usecs_to_jiffies(interval);
next = dev->eps[EP_OUT].submit_time + interval_j;
next_diff = jiffdiff(next, now);
cmd->head.delayed = 1;
if (next_diff < 0) {
next = now + interval_j;
if (!dev->suppress_poll_warn)
warn_poll_past_due(dev, now, -next_diff);
}
cmd->head.delay_until = next;
#if 0
mcp2210_debug("interval_j: %lu, submit_time: %lu, next: %lu, jiffies: %lu",
interval_j, dev->eps[EP_OUT].submit_time, next, jiffies);
#endif
cmd->head.state = MCP2210_STATE_NEW;
mcp2210_add_cmd(cmd_head, false);
return -EINPROGRESS; /* tell process_commands not to free us */
}
#endif /* CONFIG_MCP2210_IRQ */