forked from daniel-santos/mcp2210-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp2210-gpio.c
396 lines (325 loc) · 10 KB
/
mcp2210-gpio.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* MCP2210 gpio support
*
* 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_GPIO
#include <linux/gpio.h>
#include <linux/completion.h>
#include "mcp2210.h"
#include "mcp2210-debug.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
# define HAVE_GET_DIRECTION 1
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
# define HAVE_SET_DEBOUNCE 1
#endif
//static int request (struct gpio_chip *chip, unsigned offset);
//static void free (struct gpio_chip *chip, unsigned offset);
#ifdef HAVE_GET_DIRECTION
static int get_direction (struct gpio_chip *chip, unsigned offset);
#endif
static int direction_input (struct gpio_chip *chip, unsigned offset);
static int get (struct gpio_chip *chip, unsigned offset);
static int direction_output(struct gpio_chip *chip, unsigned offset, int value);
//static int set_debounce (struct gpio_chip *chip, unsigned offset, unsigned debounce);
static void set (struct gpio_chip *chip, unsigned offset, int value);
//static int to_irq (struct gpio_chip *chip, unsigned offset);
//static void dbg_show (struct seq_file *s, struct gpio_chip *chip);
static inline struct mcp2210_device *chip2dev(struct gpio_chip *chip) {
return container_of(chip, struct mcp2210_device, gpio);
}
/******************************************************************************
* probe & remove
*/
int mcp2210_gpio_probe(struct mcp2210_device *dev)
{
struct gpio_chip *gpio = &dev->gpio;
int ret;
int is_gpio_probed = dev->s.is_gpio_probed; /* unalias */
mcp2210_info();
BUG_ON(!dev);
BUG_ON(!dev->config);
BUG_ON(is_gpio_probed);
if (!dev || !dev->config || is_gpio_probed)
return -EINVAL;
gpio->label = "mcp2210";
gpio->dev = &dev->udev->dev;
gpio->owner = THIS_MODULE;
// INIT_LIST_HEAD(&gpio->list);
gpio->request = NULL;
gpio->free = NULL;
#ifdef HAVE_GET_DIRECTION
gpio->get_direction = get_direction;
#endif
gpio->direction_input = direction_input;
gpio->get = get;
gpio->direction_output = direction_output;
#ifdef HAVE_SET_DEBOUNCE
gpio->set_debounce = NULL;
#endif
gpio->set = set;
#ifdef CONFIG_MCP2210_IRQ
gpio->to_irq = mcp2210_gpio_to_irq;
#endif
gpio->dbg_show = NULL;
gpio->base = -1; /* request dynamic ID allocation */
gpio->ngpio = MCP2210_NUM_PINS;
/* private: gpio->desc */
gpio->names = (void*)dev->names; /* older kernels use char** */
gpio->can_sleep = 1; /* we have to make them sleep because we
need to do an URB */
gpio->exported = 0;
ret = gpiochip_add(gpio);
if (ret) {
mcp2210_err("Failed to register GPIOs: %de", ret);
return ret;
}
mcp2210_info("registered GPIOs from %d to %d", gpio->base,
gpio->base + gpio->ngpio - 1);
dev->s.is_gpio_probed = 1;
return 0;
}
void mcp2210_gpio_remove(struct mcp2210_device *dev)
{
int ret;
mcp2210_info();
ret = gpiochip_remove(&dev->gpio);
if (ret) {
mcp2210_err("gpiochip_remove() failed with %de", ret);
return;
}
dev->s.is_gpio_probed = 0;
}
/******************************************************************************
* struct gpio_chip functions
*/
struct gpio_completion {
struct completion completion;
u16 gpio_val_dir;
int status;
u8 mcp_status;
};
static int complete_cmd_chip(struct mcp2210_cmd *cmd_head, void *context)
{
struct gpio_completion *c = context;
struct mcp2210_device *dev = cmd_head->dev;
struct mcp2210_cmd_ctl *cmd = (void *)cmd_head;
switch (cmd->req.cmd) {
case MCP2210_CMD_GET_PIN_DIR:
case MCP2210_CMD_GET_PIN_VALUE:
c->gpio_val_dir = le16_to_cpu(dev->eps[EP_IN].buffer->body.gpio);
break;
case MCP2210_CMD_SET_CHIP_CONFIG:
case MCP2210_CMD_SET_PIN_DIR:
case MCP2210_CMD_SET_PIN_VALUE:
break;
default:
MCP_ASSERT(0);
}
c->status = cmd_head->status;
c->mcp_status = cmd_head->mcp_status;
complete_all(&c->completion);
return 0;
}
static int do_gpio_cmd(struct mcp2210_device *dev, u8 cmd_code, void *body,
size_t size)
{
struct gpio_completion c;
struct mcp2210_cmd_ctl *cmd;
int ret;
cmd = mcp2210_alloc_ctl_cmd(dev, cmd_code, 0, body, size, false, GFP_KERNEL);
if (!cmd)
return -ENOMEM;
memset(&c, 0, sizeof(c));
init_completion(&c.completion);
switch (cmd_code) {
case MCP2210_CMD_SET_CHIP_CONFIG:
case MCP2210_CMD_GET_PIN_DIR:
case MCP2210_CMD_SET_PIN_DIR:
case MCP2210_CMD_GET_PIN_VALUE:
case MCP2210_CMD_SET_PIN_VALUE:
break;
default:
MCP_ASSERT(0);
}
cmd->head.complete = complete_cmd_chip;
cmd->head.context = &c;
ret = mcp2210_add_cmd(&cmd->head, true);
if (ret)
return ret;
process_commands(dev, false, true);
wait_for_completion(&c.completion);
if (c.status) {
MCP_ASSERT(c.status < 0);
return c.status;
}
return c.gpio_val_dir;
}
#ifdef HAVE_GET_DIRECTION
static int get_direction(struct gpio_chip *chip, unsigned offset)
{
struct mcp2210_device *dev = chip2dev(chip);
unsigned long irqflags;
int ret;
BUG_ON(offset >= MCP2210_NUM_PINS);
mcp2210_debug();
spin_lock_irqsave(&dev->dev_spinlock, irqflags);
ret = 1 & (dev->s.chip_settings.gpio_direction >> offset);
spin_unlock_irqrestore(&dev->dev_spinlock, irqflags);
return ret;
}
#endif
static int set_dir_and_value(struct gpio_chip *chip, unsigned pin, int dir,
int value)
{
struct mcp2210_device *dev = chip2dev(chip);
unsigned long irqflags;
int ret = 0;
u8 pin_mode;
u16 pin_vals;
u16 pin_dirs;
u16 new_vals;
u16 new_dirs;
u8 set_vals = 0;
u8 set_dirs = 0;
BUG_ON(pin >= MCP2210_NUM_PINS);
mcp2210_debug("chip:%p, pin: %u, dir: %d, value: %d", chip, pin, dir,
value);
spin_lock_irqsave(&dev->dev_spinlock, irqflags);
pin_vals = dev->s.chip_settings.gpio_value;
pin_dirs = dev->s.chip_settings.gpio_direction;
pin_mode = dev->config->pins[pin].mode;
spin_unlock_irqrestore(&dev->dev_spinlock, irqflags);
/* treat the dedicated interrupt counter as an ordinary gpio that
* clears when read? || (pin_mode == MCP2210_PIN_DEDICATED && pin == 6)*/
if (pin_mode != MCP2210_PIN_GPIO) {
mcp2210_err("pin %u not gpio", pin);
return -EPERM;
}
switch (dir) {
/* If not changing direction and current dir is input, then error */
case MCP2210_GPIO_NO_CHANGE:
if (pin_dirs & 1 << pin)
return -EROFS;
/* intentional fall-through */
case MCP2210_GPIO_OUTPUT:
new_vals = (pin_vals & ~(1 << pin)) | value << pin;
if (new_vals != pin_vals)
set_vals = 1;
if (dir == MCP2210_GPIO_NO_CHANGE)
break;
/* intentional fall-through */
case MCP2210_GPIO_INPUT:
new_dirs = (pin_dirs & ~(1 << pin)) | dir << pin;
if (new_dirs != pin_dirs)
set_dirs = 1;
break;
default:
BUG();
}
mcp2210_debug("pin_vals = 0x%04hx, pin_dirs = 0x%04hx\n"
"new_vals = 0x%04hx, new_dirs = 0x%04hx\n"
"set_vals = %hhu , new_dirs = %hhu",
pin_vals, pin_dirs, new_vals, new_dirs,
set_dirs, set_vals);
if (set_vals & set_dirs) {
/* I'm not aware of any other mechanism to change the direction
* to output and the value at the same time, and I don't think
* this device supports changing the direction to output, but
* leaving it's actual state in high-z until you give it a
* value, so we'll just set the full chip settings.
*/
struct mcp2210_chip_settings cs = dev->s.chip_settings;
cs.gpio_value = new_vals;
cs.gpio_direction = new_dirs;
return do_gpio_cmd(dev, MCP2210_CMD_SET_CHIP_CONFIG, &cs,
sizeof(cs));
}
if (set_vals) {
ret = do_gpio_cmd(dev, MCP2210_CMD_SET_PIN_VALUE, &new_vals,
sizeof(new_vals));
if (ret < 0)
return ret;
}
if (set_dirs) {
ret = do_gpio_cmd(dev, MCP2210_CMD_SET_PIN_DIR, &new_dirs,
sizeof(new_dirs));
}
return ret;
}
static int direction_input(struct gpio_chip *chip, unsigned offset)
{
return set_dir_and_value(chip, offset, MCP2210_GPIO_INPUT, 0);
}
static int direction_output(struct gpio_chip *chip, unsigned offset, int value)
{
return set_dir_and_value(chip, offset, MCP2210_GPIO_OUTPUT, value);
}
static int get(struct gpio_chip *chip, unsigned offset)
{
struct mcp2210_device *dev = chip2dev(chip);
unsigned long irqflags;
enum mcp2210_pin_mode mode;
int val;
int dir;
int ret;
unsigned long now = jiffies;
unsigned long last_poll;
u32 uninitialized_var(stale_usecs);
BUG_ON(offset >= MCP2210_NUM_PINS);
mcp2210_debug();
spin_lock_irqsave(&dev->dev_spinlock, irqflags);
mode = dev->s.chip_settings.pin_mode[offset];
val = 1 & (dev->s.chip_settings.gpio_value >> offset);
dir = 1 & (dev->s.chip_settings.gpio_direction >> offset);
last_poll = dev->s.last_poll_gpio;
if (IS_ENABLED(CONFIG_MCP2210_IRQ))
stale_usecs = dev->config->stale_gpio_usecs;
spin_unlock_irqrestore(&dev->dev_spinlock, irqflags);
switch (mode) {
case MCP2210_PIN_GPIO:
break;
case MCP2210_PIN_DEDICATED:
if (offset == 6) {
ret = !!dev->s.interrupt_event_counter;
dev->s.interrupt_event_counter = 0;
return ret;
}
/* fall-throguh */
default:
return -EINVAL;
};
/* If the value was read within stale_usecs microseconds, then we just
* return that value */
if (IS_ENABLED(CONFIG_MCP2210_IRQ) && stale_usecs && time_before(now,
last_poll + usecs_to_jiffies(stale_usecs))) {
return val;
}
if (dir == MCP2210_GPIO_OUTPUT)
return val;
ret = do_gpio_cmd(dev, MCP2210_CMD_GET_PIN_VALUE, NULL, 0);
if (ret < 0)
return ret;
else
return 1 & ret >> offset;
}
static void set(struct gpio_chip *chip, unsigned offset, int value)
{
set_dir_and_value(chip, offset, MCP2210_GPIO_NO_CHANGE, value);
}
#endif /* CONFIG_MCP2210_GPIO */