forked from daniel-santos/mcp2210-linux
/
mcp2210-irq.c
254 lines (197 loc) · 5.91 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
/*
* MCP2210 software IRQ controller
*
* Copyright (c) 2013 Daniel Santos <daniel.santos@pobox.com>
*
* 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.
*/
#ifdef CONFIG_MCP2210_IRQ
#include <linux/irqdomain.h>
#include <linux/irq.h>
#include "mcp2210.h"
#include "mcp2210-debug.h"
static int complete_poll(struct mcp2210_cmd *cmd, void *context);
static int mcp2210_irq_map(struct irq_domain *domain, unsigned int irq,
irq_hw_number_t hwirq);
static const struct irq_domain_ops mcp2210_irq_domain_ops = {
.map = mcp2210_irq_map,
.xlate = irq_domain_xlate_twocell,
};
static inline struct mcp2210_device *to_dev(struct gpio_chip *chip)
{
return container_of(chip, struct mcp2210_device, gpio);
}
int mcp2210_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
{
struct mcp2210_device *dev = to_dev(chip);
return irq_create_mapping(dev->irq_domain, offset);
}
static void mcp2210_irq_mask(struct irq_data *data)
{
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
mcp2210_debug();
}
static void mcp2210_irq_unmask(struct irq_data *data)
{
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
mcp2210_debug();
}
static int mcp2210_irq_set_type(struct irq_data *data, unsigned int type)
{
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
mcp2210_debug();
#if 0
if (type & IRQ_TYPE_EDGE_RISING)
adnp->irq_rise[reg] |= BIT(pos);
else
adnp->irq_rise[reg] &= ~BIT(pos);
if (type & IRQ_TYPE_EDGE_FALLING)
adnp->irq_fall[reg] |= BIT(pos);
else
adnp->irq_fall[reg] &= ~BIT(pos);
if (type & IRQ_TYPE_LEVEL_HIGH)
adnp->irq_high[reg] |= BIT(pos);
else
adnp->irq_high[reg] &= ~BIT(pos);
if (type & IRQ_TYPE_LEVEL_LOW)
adnp->irq_low[reg] |= BIT(pos);
else
adnp->irq_low[reg] &= ~BIT(pos);
#endif
return 0;
}
static void mcp2210_irq_bus_lock(struct irq_data *data)
{
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
mcp2210_debug();
mutex_lock(&dev->irq_lock);
}
static void mcp2210_irq_bus_unlock(struct irq_data *data)
{
struct mcp2210_device *dev = irq_data_get_irq_chip_data(data);
mcp2210_debug();
mutex_unlock(&dev->irq_lock);
}
static struct irq_chip mcp2210_irq_chip = {
.name = "gpio-mcp2210",
.irq_mask = mcp2210_irq_mask,
.irq_unmask = mcp2210_irq_unmask,
.irq_set_type = mcp2210_irq_set_type,
.irq_bus_lock = mcp2210_irq_bus_lock,
.irq_bus_sync_unlock = mcp2210_irq_bus_unlock,
};
int mcp2210_irq_probe(struct mcp2210_device *dev)
{
uint i;
mutex_init(&dev->irq_lock);
dev->nr_irqs = 0;
for (i = 0; i < 9; ++i) {
struct mcp2210_pin_config *pin = &dev->config->pins[i];
if (!pin->has_irq)
continue;
switch (pin->mode) {
case MCP2210_PIN_DEDICATED:
if (i != 6)
return -EINVAL;
dev->s.poll_intr = 1;
break;
case MCP2210_PIN_GPIO:
dev->s.poll_gpio = 1;
break;
};
if (pin->irq + 1> dev->nr_irqs)
dev->nr_irqs = pin->irq + 1;
}
BUG_ON(dev->nr_irqs > 7);
#ifdef CONFIG_MCP2210_GPIO
if (dev->s.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->s.poll_intr) {
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);
}
return 0;
}
void mcp2210_irq_remove(struct mcp2210_device *dev)
{
}
void _mcp2210_irq_do_gpio(struct mcp2210_device *dev, u16 old_val, u16 new_val)
{
}
void _mcp2210_irq_do_intr_counter(struct mcp2210_device *dev, u16 count)
{
if (dev->s.chip_settings.pin_mode[6] != MCP2210_PIN_DEDICATED)
return;
}
static int mcp2210_irq_map(struct irq_domain *domain, unsigned int irq,
irq_hw_number_t hwirq)
{
irq_set_chip_data(irq, domain->host_data);
irq_set_chip(irq, &mcp2210_irq_chip);
irq_set_nested_thread(irq, true);
#ifdef CONFIG_ARM
set_irq_flags(irq, IRQF_VALID);
#else
irq_set_noprobe(irq);
#endif
return 0;
}
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;
int enabled;
unsigned long interval;
unsigned long now = jiffies;
mcp2210_debug();
if (dev->dead)
return -EINPROGRESS;
if (cmd->req.cmd == MCP2210_CMD_GET_PIN_VALUE) {
enabled = dev->s.poll_gpio;
interval = dev->config->poll_gpio_usecs;
dev->s.last_poll_gpio = now;
} else {
enabled = dev->s.poll_intr;
interval = dev->config->poll_intr_usecs;
dev->s.last_poll_intr = now;
}
if (!enabled)
return -EINPROGRESS;
if (1) {
unsigned long interval_j = usecs_to_jiffies(interval);
unsigned long next = dev->eps[EP_OUT].submit_time + interval_j;
cmd->head.delayed = 1;
if (jiffdiff(next, now) < 0) {
mcp2210_warn("poll interval collapse, restarting universe");
next = jiffies + interval_j;
}
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 */