-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt1211.c
401 lines (299 loc) · 9.3 KB
/
vt1211.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
397
398
399
400
401
/*
* GPIO interface for VT1211 Super I/O chip
*
* Copyright 2018 by Roman Serov <roman@serov.co>
*
* This file is part of VT1211 GPIO interface.
*
* VT1211 GPIO interface 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 3 of the License, or
* (at your option) any later version.
*
* VT1211 GPIO interface 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 VT1211 GPIO interface. If not, see <http://www.gnu.org/licenses/>.
*
* @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
*/
#include "vt1211.h"
static bool port_1 = true;
static bool port_3_6 = false;
module_param(port_1, bool, 0644);
module_param(port_3_6, bool, 0644);
MODULE_PARM_DESC(port_1, "PORT1 Enable");
MODULE_PARM_DESC(port_3_6, "From PORT3 to PORT6 Enable");
struct vt1211_gpio {
struct gpio_chip chip;
spinlock_t lock;
u8 dev_id;
u8 dev_rev;
u16 io_base;
u16 io_size;
};
static struct vt1211_gpio vt1211_gpio_chip = {
.lock = __SPIN_LOCK_UNLOCKED(vt1211_gpio_chip.lock),
};
static u8 vt1211_get_port_offset(unsigned gpio_offset) {
if ((gpio_offset & VT_PORT_6) == VT_PORT_6) {
return VT_PORT_6_OFFSET;
} else if ((gpio_offset & VT_PORT_5) == VT_PORT_5) {
return VT_PORT_5_OFFSET;
} else if (gpio_offset & VT_PORT_4) {
return VT_PORT_4_OFFSET;
} else if (gpio_offset & VT_PORT_3) {
return VT_PORT_3_OFFSET;
} else {
return VT_PORT_1_OFFSET;
}
}
static u8 vt1211_get_pin_n(u8 gpio_offset) {
return (gpio_offset & 0x7) ;
}
static void vt1211_config_mode(void) {
outb(VT_ENTER_CONFIG, VT_CIR);
outb(VT_ENTER_CONFIG, VT_CIR);
}
static void vt1211_config_mode_exit(void) {
outb(VT_EXIT_CONFIG, VT_CIR);
}
static void vt1211_config_mode_gpio(void) {
outb(VT_CONFIG_LDN, VT_CIR);
outb(VT_LDN_GPIO, VT_CDR);
}
static u16 vt1211_get_id_and_rev(void) {
u8 id, rev;
vt1211_config_mode();
outb(VT_CONFIG_DEV_ID, VT_CIR);
id = inb(VT_CDR);
outb(VT_CONFIG_DEV_REV, VT_CIR);
rev = inb(VT_CDR);
vt1211_config_mode_exit();
return ((id << 8) | rev);
}
static void vt1211_port_as_gpio(u8 port) {
u8 d_tmp;
vt1211_config_mode();
switch(port) {
case VT_CONFIG_PORT_1: {
outb(VT_CONFIG_GPIO_P1_PIN_SELECT, VT_CIR);
outb(0xFF, VT_CDR);
}
break;
case VT_CONFIG_PORT_3_6: {
outb(VT_CONFIG_GPIO_P2_PIN_SELECT, VT_CIR);
d_tmp = inb(VT_CDR);
d_tmp |= 0b00100000;
outb(VT_CONFIG_GPIO_P2_PIN_SELECT, VT_CIR);
outb(d_tmp, VT_CDR);
}
break;
}
vt1211_config_mode_exit();
}
static u16 vt1211_get_baddr(void) {
u8 b1, b2;
vt1211_config_mode();
vt1211_config_mode_gpio();
outb(VT_LDN_GPIO_BADDR_B1, VT_CIR);
b1 = inb(VT_CDR);
outb(VT_LDN_GPIO_BADDR_B2, VT_CIR);
b2 = inb(VT_CDR);
vt1211_config_mode_exit();
return ((b1 << 8) | b2);
}
static void vt1211_gpio_activate(void) {
vt1211_config_mode();
vt1211_config_mode_gpio();
outb(VT_LDN_GPIO_ACTIVATE, VT_CIR);
outb(0x01, VT_CDR);
vt1211_config_mode_exit();
}
static int vt1211_init(u8 ports) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
u16 id_rev;
bool port_selected = false;
id_rev = vt1211_get_id_and_rev();
gpio_data->dev_id = ((id_rev & 0xFF00) >> 8);
gpio_data->dev_rev = (id_rev & 0x00FF);
if (gpio_data->dev_id != 0x3C) {
return VT_INIT_NOT_FOUND;
}
if (ports & VT_CONFIG_PORT_1) {
vt1211_port_as_gpio(VT_CONFIG_PORT_1);
gpio_data->io_size = VT_PORT_1_OFFSET + 1;
port_selected = true;
}
if (ports & VT_CONFIG_PORT_3_6) {
vt1211_port_as_gpio(VT_CONFIG_PORT_3_6);
gpio_data->io_size = VT_PORT_6_OFFSET + 1;
port_selected = true;
}
if (!port_selected) {
return VT_INIT_NO_PORT;
}
gpio_data->io_base = vt1211_get_baddr();
vt1211_gpio_activate();
return VT_INIT_OK;
}
static void vt1211_set_pin_direction(u8 port, u8 pin, u8 direction) {
vt1211_config_mode();
vt1211_config_mode_gpio();
outb(VT_LDN_GPIO_PORT_SELECT, VT_CIR);
outb(port, VT_CDR);
outb(VT_LDN_GPIO_MODE_CONFIG, VT_CIR);
u8 m_data = inb(VT_CDR);
if (direction == VT_PIN_OUTPUT) {
m_data |= pin;
} else {
m_data &= ~(pin);
}
outb(VT_LDN_GPIO_MODE_CONFIG, VT_CIR);
outb(m_data, VT_CDR);
vt1211_config_mode_exit();
}
static int vt1211_gpio_direction_input(struct gpio_chip *chip, unsigned offset) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
u8 port = vt1211_get_port_offset(offset);
u8 pin = vt1211_get_pin_n(offset);
spin_lock(&gpio_data->lock);
vt1211_set_pin_direction(port, (0x1 << pin), VT_PIN_INPUT);
spin_unlock(&gpio_data->lock);
return 0;
}
static int vt1211_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
u8 port = vt1211_get_port_offset(offset);
u8 pin = vt1211_get_pin_n(offset);
spin_lock(&gpio_data->lock);
vt1211_set_pin_direction(port, (0x1 << pin), VT_PIN_OUTPUT);
spin_unlock(&gpio_data->lock);
return 0;
}
static int vt1211_gpio_get(struct gpio_chip *chip, unsigned offset) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
u8 port = vt1211_get_port_offset(offset);
u8 pin = vt1211_get_pin_n(offset);
u8 data = inb(gpio_data->io_base + port);
return (data & (0x1 << pin));
}
static void vt1211_gpio_set(struct gpio_chip *chip, unsigned offset, int value) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
u8 port = vt1211_get_port_offset(offset);
u8 pin = vt1211_get_pin_n(offset);
u8 data = inb(gpio_data->io_base + port);
if (value) {
data |= 0x1 << pin;
} else {
data &= ~(0x1 << pin);
}
outb(data, gpio_data->io_base + port);
}
static int vt1211_gpio_get_direction(struct gpio_chip *gc, unsigned offset) {
struct vt1211_gpio *gpio_data = &vt1211_gpio_chip;
spin_lock(&gpio_data->lock);
vt1211_config_mode();
vt1211_config_mode_gpio();
u8 port = vt1211_get_port_offset(offset);
u8 pin = vt1211_get_pin_n(offset);
outb(VT_LDN_GPIO_PORT_SELECT, VT_CIR);
outb(port, VT_CDR);
outb(VT_LDN_GPIO_MODE_CONFIG, VT_CIR);
u8 data = inb(VT_CDR);
vt1211_config_mode_exit();
spin_unlock(&gpio_data->lock);
return ~(data & (0x1 << pin));
}
static const struct gpio_chip vt1211_gpio_chip_tmpl = {
.label = "vt1211",
.owner = THIS_MODULE,
.direction_input = vt1211_gpio_direction_input,
.get = vt1211_gpio_get,
.direction_output = vt1211_gpio_direction_output,
.set = vt1211_gpio_set,
.get_direction = vt1211_gpio_get_direction,
.base = -1,
.can_sleep = 0,
};
static int __init vt1211_gpio_init( void ) {
printk(KERN_INFO INFO_PREFIX "GPIO init\n");
struct vt1211_gpio *vt1211_gpio = &vt1211_gpio_chip;
char *labels;
char **labels_table;
vt1211_gpio->chip = vt1211_gpio_chip_tmpl;
vt1211_gpio->chip.ngpio = 0;
u16 ports = 0;
if (port_1) {
ports |= VT_CONFIG_PORT_1;
vt1211_gpio->chip.ngpio += VT_MAX_GPIO_PORT1;
}
if (port_3_6) {
ports |= VT_CONFIG_PORT_3_6;
vt1211_gpio->chip.ngpio += VT_MAX_GPIO_PORT3_6;
}
int init_res = vt1211_init(ports);
int err;
switch (init_res) {
case VT_INIT_NOT_FOUND: {
printk(KERN_ERR INFO_PREFIX "Device not found\n");
return -ENODEV;
}
case VT_INIT_NO_PORT: {
printk(KERN_ERR INFO_PREFIX "No port selected\n");
return -ENODEV;
}
case VT_INIT_OK:
default: {
printk(KERN_NOTICE INFO_PREFIX "ID: %02X, Revision: %02X, Base addr.: %04X\n", vt1211_gpio->dev_id, vt1211_gpio->dev_rev, vt1211_gpio->io_base);
}
}
if (!request_region(vt1211_gpio->io_base, vt1211_gpio->io_size, VT_DRVNAME))
return -EBUSY;
labels = kcalloc(vt1211_gpio->chip.ngpio, sizeof("vt1211_gpXY"), GFP_KERNEL);
labels_table = kcalloc(vt1211_gpio->chip.ngpio, sizeof(const char *), GFP_KERNEL);
if (!labels || !labels_table) {
err = -ENOMEM;
goto EXIT_ON_FAIL;
}
for (u8 i = 0; i < vt1211_gpio->chip.ngpio; ++i) {
char *label = &labels[i * sizeof("vt1211_gpXY")];
u8 port = 1 + (i / 8);
if (port != 1 && port_1) {
++port;
}
if (!port_1 && port_3_6) {
port += 2;
}
u8 pin = vt1211_get_pin_n(i);
sprintf(label, "vt1211_gp%u%u", port, pin);
labels_table[i] = label;
}
vt1211_gpio->chip.names = (const char *const*)labels_table;
err = gpiochip_add_data(&vt1211_gpio->chip, vt1211_gpio);
if (err)
goto EXIT_ON_FAIL;
return 0;
EXIT_ON_FAIL:
kfree(labels_table);
kfree(labels);
release_region(vt1211_gpio->io_base, vt1211_gpio->io_size);
return err;
}
static void __exit vt1211_gpio_exit(void) {
struct vt1211_gpio *vt1211_gpio = &vt1211_gpio_chip;
gpiochip_remove(&vt1211_gpio->chip);
release_region(vt1211_gpio->io_base, vt1211_gpio->io_size);
kfree(vt1211_gpio->chip.names[0]);
kfree(vt1211_gpio->chip.names);
printk(KERN_INFO INFO_PREFIX "Goodbye cruel world!\n" );
}
module_init(vt1211_gpio_init);
module_exit(vt1211_gpio_exit);
MODULE_AUTHOR( "Roman Serov <roman@serov.co>" );
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0") ;
MODULE_DESCRIPTION("GPIO interface for VT1211 Super I/O chip");