This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ch347-gpio.c
191 lines (159 loc) · 4.76 KB
/
ch347-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
// SPDX-License-Identifier: GPL-2.0
/*
* GPIO interface for the CH347.
*
* Copyright 2023, Alexey Starikovskiy <aystarik@gmail.com>
*/
#include "ch347.h"
#define CH347_GPIO_NUM_PINS 8 /* Number of GPIO pins */
static void ch347_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
{
struct CH347_device *dev = gpiochip_get_data(chip);
for (unsigned i = 0; i < 8; ++i) {
u8 pin = dev->gpio_ibuf[3 + i];
seq_printf(s, "pin[%d](%s):%d", i, pin & 0x80 ? "out":"in",
pin & 0x40 ? 1 : 0);
}
}
static int gpio_transfer(struct CH347_device *dev)
{
int actual;
int rc;
rc = usb_bulk_msg(dev->usb_dev, usb_sndbulkpipe(dev->usb_dev, dev->ep_out),
dev->gpio_obuf, 11, &actual, DEFAULT_TIMEOUT);
if (rc < 0)
return rc;
rc = usb_bulk_msg(dev->usb_dev,
usb_rcvbulkpipe(dev->usb_dev, dev->ep_in),
dev->gpio_ibuf, 11, &actual, DEFAULT_TIMEOUT);
return rc;
}
static int ch347_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
int rc;
struct CH347_device *dev = gpiochip_get_data(chip);
if (offset > 7) return 0;
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
rc = gpio_transfer(dev);
if (rc)
return rc;
return (dev->gpio_ibuf[3 + offset] & 0x40) ? 1 : 0;
}
static int ch347_gpio_get_direction(struct gpio_chip *chip,
unsigned int offset)
{
int rc;
struct CH347_device *dev = gpiochip_get_data(chip);
if (offset > 7) return 0;
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
rc = gpio_transfer(dev);
if (rc)
return rc;
return (dev->gpio_ibuf[3 + offset] & 0x80) ? 1 : 0;
}
static int ch347_gpio_get_multiple(struct gpio_chip *chip,
unsigned long *mask, unsigned long *bits)
{
int rc;
struct CH347_device *dev = gpiochip_get_data(chip);
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
rc = gpio_transfer(dev);
if (rc)
return rc;
for (unsigned i = 0; i < 8; ++i) {
if (*mask & BIT(i) && (dev->gpio_ibuf[3 + i] & 0x40)) {
*bits |= BIT(i);
}
}
return 0;
}
static void ch347_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
{
struct CH347_device *dev = gpiochip_get_data(chip);
if (offset > 7) return;
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
dev->gpio_obuf[3 + offset] |= 0xc0; // enable pin change
if (dev->gpio_ibuf[3 + offset] & 0x80) { // copy direction
dev->gpio_obuf[3 + offset] |= 0x30;
}
if (value) {
dev->gpio_obuf[3 + offset] |= 0x08;
}
gpio_transfer(dev);
}
static void ch347_gpio_set_multiple(struct gpio_chip *chip,
unsigned long *mask, unsigned long *bits)
{
struct CH347_device *dev = gpiochip_get_data(chip);
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
for (unsigned i = 0; i < 8; ++i) {
if (*mask & BIT(i) && (dev->gpio_ibuf[3 + i] & 0x80)) {
dev->gpio_obuf[3 + i] |= 0xc0; // enable pin change
if (*bits & BIT(i)) {
dev->gpio_obuf[3 + i] |= 0x08;
}
}
}
gpio_transfer(dev);
}
static int ch347_gpio_direction_input(struct gpio_chip *chip,
unsigned int offset)
{
struct CH347_device *dev = gpiochip_get_data(chip);
if (offset > 7) return 0;
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
dev->gpio_obuf[3 + offset] |= 0xc0; // enable pin change
if (dev->gpio_ibuf[3 + offset] & 0x40) { // copy value
dev->gpio_obuf[3 + offset] |= 0x08;
}
gpio_transfer(dev);
return 0;
}
static int ch347_gpio_direction_output(struct gpio_chip *chip,
unsigned int offset, int value)
{
struct CH347_device *dev = gpiochip_get_data(chip);
if (offset > 7) return 0;
memset(dev->gpio_obuf + 3, 0, 8); // clear all pins
dev->gpio_obuf[3 + offset] |= 0xc0; // enable pin change
if (dev->gpio_ibuf[3 + offset] & 0x40) { // copy value
dev->gpio_obuf[3 + offset] |= 0x08;
}
dev->gpio_obuf[3 + offset] |= 0x30; // set direction == output
gpio_transfer(dev);
return 0;
}
void ch347_gpio_remove(struct CH347_device *dev)
{
gpiochip_remove(&dev->gpio);
}
int ch347_gpio_init(struct CH347_device *dev)
{
struct gpio_chip *gpio = &dev->gpio;
int rc;
gpio->label = "ch347";
gpio->parent = &dev->usb_dev->dev;
gpio->owner = THIS_MODULE;
gpio->get = ch347_gpio_get;
gpio->get_multiple = ch347_gpio_get_multiple;
gpio->get_direction = ch347_gpio_get_direction;
gpio->direction_input = ch347_gpio_direction_input;
gpio->direction_output = ch347_gpio_direction_output;
gpio->set = ch347_gpio_set;
gpio->set_multiple = ch347_gpio_set_multiple;
gpio->dbg_show = ch347_gpio_dbg_show;
gpio->base = -1;
gpio->ngpio = CH347_GPIO_NUM_PINS;
gpio->can_sleep = true;
memset(dev->gpio_obuf, 0, 11);
dev->gpio_obuf[0] = 0xcc; // these fields do not ever change
dev->gpio_obuf[1] = 8; // these fields do not ever change
dev->gpio_obuf[2] = 0; // these fields do not ever change
gpio_transfer(dev);
rc = gpiochip_add_data(gpio, dev);
if (rc) {
dev_err(&dev->usb_dev->dev, "Could not add GPIO\n");
return rc;
}
return 0;
}