-
Notifications
You must be signed in to change notification settings - Fork 2
/
device_rotary_am_spinin.c
469 lines (404 loc) · 17 KB
/
device_rotary_am_spinin.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/********************************************************************************
* Copyright (C) 2021 Ju, Gyeong-min
********************************************************************************/
#include "build_cfg.h"
#include <linux/kernel.h>
#include <linux/input.h>
#include <linux/delay.h>
#include "bcm_peri.h"
#include "gpio_util.h"
#if defined(USE_I2C_DIRECT)
#include "i2c_util.h"
#else
#include <linux/i2c.h>
#endif
#if defined(USE_SPI_DIRECT)
#include "spi_util.h"
#else
#include <linux/spi/spi.h>
#endif
#include "log_util.h"
#include "parse_util.h"
// AM_SPININ Defines
#define AM_SPININ_READ_VALUE (0x00 | 0x00)
#define AM_SPININ_WRITE_VALUE (0x40 | 0x01)
#define AM_SPININ_SET_MODE (0x40 | 0x02)
#define AM_SPININ_SET_SAMPLERATE (0x40 | 0x03)
#define AM_SPININ_SET_MIN_VALUE (0x40 | 0x04)
#define AM_SPININ_SET_MAX_VALUE (0x40 | 0x05)
#define AM_SPININ_COMM_I2C (0)
#define AM_SPININ_COMM_SPI (1)
// default i2c addr
#define AM_SPININ_DEFAULT_I2C_ADDR (0x34)
#define INPUT_AM_SPININ_DEFAULT_PPR (400)
#define INPUT_AM_SPININ_DEFAULT_MIN_VALUE (-5000)
#define INPUT_AM_SPININ_DEFAULT_MAX_VALUE (5000)
#define INPUT_AM_SPININ_DEFAULT_SAMPLE_RATE (10)
#define INPUT_MOUSE_DEFAULT_DPI (1000)
typedef struct tag_device_am_spinin_config {
int comm_type;
int addr;
int rotary_ppr;
int min_value;
int max_value;
int sample_rate;
} device_am_spinin_config_t;
typedef struct tag_device_am_spinin_index_item {
int button;
int value;
} device_am_spinin_index_item_t;
typedef struct tag_device_am_spinin_index_table {
device_am_spinin_index_item_t buttondef[MAX_INPUT_BUTTON_COUNT];
int pin_count;
int button_start_index;
int mouse_dpi;
} device_am_spinin_index_table_t;
// device.data에 할당 될 구조체
typedef struct tag_device_am_spinin_data {
device_am_spinin_config_t device_cfg;
#if !defined(USE_I2C_DIRECT)
struct i2c_client* i2c;
#endif
#if !defined(USE_SPI_DIRECT)
struct spi_device* spi;
#endif
device_am_spinin_index_table_t button_cfgs[1];
} device_am_spinin_data_t;
#define INPUT_AM_SPININ_DEFAULT_KEYCODE_TABLE_ITEM_COUNT (1)
static const device_am_spinin_index_table_t default_input_am_spinin_config = {
{
{REL_X, 0}
},
INPUT_AM_SPININ_DEFAULT_KEYCODE_TABLE_ITEM_COUNT,
0, 0
};
// device 파라미터 파싱
static int __parse_device_param_for_am_spinin(device_am_spinin_data_t* user_data, char* device_config_str)
{
char szText[512];
char* pText;
if (device_config_str != NULL) {
char str[10];
strcpy(szText, device_config_str);
pText = szText;
if (parse_string(str, 10, &pText, ",", "i2c") == NULL) {
return -1;
}
if (strcmp(str, "i2c") == 0) {
user_data->device_cfg.comm_type = AM_SPININ_COMM_I2C;
} else if (strcmp(str, "spi") == 0) {
user_data->device_cfg.comm_type = AM_SPININ_COMM_SPI;
} else {
return -1;
}
user_data->device_cfg.addr = parse_number(&pText, ",", 0, AM_SPININ_DEFAULT_I2C_ADDR);
user_data->device_cfg.rotary_ppr = parse_number(&pText, ",", 10, INPUT_AM_SPININ_DEFAULT_PPR);
user_data->device_cfg.min_value = parse_number(&pText, ",", 10, INPUT_AM_SPININ_DEFAULT_MIN_VALUE);
user_data->device_cfg.max_value = parse_number(&pText, ",", 10, INPUT_AM_SPININ_DEFAULT_MAX_VALUE);
user_data->device_cfg.sample_rate = parse_number(&pText, ",", 10, INPUT_AM_SPININ_DEFAULT_SAMPLE_RATE);
} else {
user_data->device_cfg.comm_type = AM_SPININ_COMM_I2C;
user_data->device_cfg.addr = AM_SPININ_DEFAULT_I2C_ADDR;
user_data->device_cfg.rotary_ppr = INPUT_AM_SPININ_DEFAULT_PPR;
user_data->device_cfg.min_value = INPUT_AM_SPININ_DEFAULT_MIN_VALUE;
user_data->device_cfg.max_value = INPUT_AM_SPININ_DEFAULT_MAX_VALUE;
user_data->device_cfg.sample_rate = INPUT_AM_SPININ_DEFAULT_SAMPLE_RATE;
}
return 0;
}
// 각 endpoint 파라미터 파싱
static int __parse_endpoint_param_for_am_spinin(device_am_spinin_data_t* user_data, char* endpoint_config_str[], input_endpoint_data_t *endpoint_list[], int endpoint_count)
{
int i, j;
int code_mode;
device_am_spinin_index_table_t *src, *des;
char szText[512];
char* pText;
char* temp_p;
des = user_data->button_cfgs;
for (i = 0; i < endpoint_count; i++ ) {
input_endpoint_data_t *ep = endpoint_list[i];
char* cfgtype_p;
int pin_count, button_start_index, mouse_dpi;
if (ep == NULL) continue;
if (endpoint_config_str[i] != NULL) {
strcpy(szText, endpoint_config_str[i]);
pText = szText;
cfgtype_p = strsep(&pText, ",");
} else {
pText = NULL;
cfgtype_p = NULL;
}
if (cfgtype_p == NULL || strcmp(cfgtype_p, "default") == 0 || strcmp(cfgtype_p, "") == 0){
src = (device_am_spinin_index_table_t *)&default_input_am_spinin_config;
code_mode = INPUT_CODE_TYPE_KEYCODE;
pin_count = parse_number(&pText, ",", 10, src->pin_count);
button_start_index = parse_number(&pText, ",", 10, 0);
mouse_dpi = parse_number(&pText, ",", 10, INPUT_MOUSE_DEFAULT_DPI);
} else if (strcmp(cfgtype_p, "custom") == 0){
mouse_dpi = parse_number(&pText, ",", 10, INPUT_MOUSE_DEFAULT_DPI);
temp_p = strsep(&pText, ",");
if (temp_p == NULL || strcmp(temp_p, "keycode") == 0 || strcmp(temp_p, "default") == 0 || strcmp(temp_p, "") == 0) {
code_mode = INPUT_CODE_TYPE_KEYCODE;
} else if (strcmp(temp_p, "index") == 0 || strcmp(temp_p, "1") == 0) {
code_mode = INPUT_CODE_TYPE_INDEX;
} else {
code_mode = INPUT_CODE_TYPE_NONE;
}
button_start_index = 0;
pin_count = 0;
src = &user_data->button_cfgs[i];
while (pText != NULL && pin_count < MAX_INPUT_BUTTON_COUNT) {
char *block_p;
int button;
strsep(&pText, "{");
block_p = strsep(&pText, "}");
button = parse_number(&block_p, ",", 0, -1);
strsep(&pText, ",");
// 키 설정 추가
src->buttondef[pin_count].button = button;
if (button >= 0) {
src->buttondef[pin_count].value = parse_number(&block_p, ",", 0, 0);
}
pin_count++;
}
} else {
continue;
}
if (code_mode == INPUT_CODE_TYPE_KEYCODE) {
for (j = 0; j < pin_count; j++) {
int idx = find_input_button_data(ep, src->buttondef[j].button, NULL);
des->buttondef[j].button = idx;
if (idx != -1) {
des->buttondef[j].value = src->buttondef[j].value;
}
}
des->pin_count = pin_count;
des->button_start_index = button_start_index;
des->mouse_dpi = mouse_dpi;
} else if (code_mode == INPUT_CODE_TYPE_INDEX) {
for (j = 0; j < pin_count; j++) {
des->buttondef[j].button = src->buttondef[j].button;
des->buttondef[j].value = src->buttondef[j].value;
}
des->pin_count = pin_count;
des->button_start_index = button_start_index;
des->mouse_dpi = mouse_dpi;
}
des++;
}
return 0;
}
#if defined(USE_SPI_DIRECT)
static short __spi_trans_for_am_spinin(char cmd, short value)
{
unsigned char buf[3];
buf[0] = cmd;
buf[1] = (value >> 8) & 0xFF;
buf[2] = value & 0xFF;
spi_transfern(buf, 3);
return (short)((unsigned short)buf[1] << 8 | buf[2]);
}
#else
static short __spi_trans_for_am_spinin(struct spi_device* spi, char cmd, short value)
{
unsigned char buf[3];
buf[0] = cmd;
buf[1] = (value >> 8) & 0xFF;
buf[2] = value & 0xFF;
spi_write_then_read(spi, buf, 3, buf, 3);
return (short)((unsigned short)buf[1] << 8 | buf[2]);
}
#endif
// device_config_str : comm_type, addr, io_count
// endpoint_config_str : endpoint, config_type (default | custom), ...
// default: pin_count, start_index, io_skip_count
// custom: io_skip_count, code_type (0|1), n * {button, value}
// code_type = 0 : key code
// code_type = 1 : index
//
// ex) device1=am_spinin;0x20,16;0,default,12
// device2=am_spinin;0x20;1,custom,,0,{10,0x103,1},{10,0x103,1},{10,0x103,1},{10,0x103,1},{10,0x103,1},{10,0x103,1}
static int init_input_device_for_am_spinin(void* device_desc_data, input_device_data_t *device_data, char* device_config_str, char* endpoint_config_strs[])
{
device_am_spinin_data_t* user_data;
int result;
user_data = (device_am_spinin_data_t *)kzalloc(sizeof(device_am_spinin_data_t) + sizeof(device_am_spinin_index_table_t) * (device_data->target_endpoint_count - 1), GFP_KERNEL);
result = __parse_device_param_for_am_spinin(user_data, device_config_str);
if (result != 0) {
kfree(user_data);
return result;
}
result = __parse_endpoint_param_for_am_spinin(user_data, endpoint_config_strs, device_data->target_endpoints, device_data->target_endpoint_count);
if (result != 0) {
kfree(user_data);
return result;
}
device_data->data = (void *)user_data;
return 0;
}
static void start_input_device_for_am_spinin(input_device_data_t *device_data)
{
device_am_spinin_data_t *user_data = (device_am_spinin_data_t *)device_data->data;
if (user_data->device_cfg.comm_type == AM_SPININ_COMM_I2C) {
#if defined(USE_I2C_DIRECT)
i2c_init(bcm_peri_base_probe(), 0xB0);
i2c_write_1word(user_data->device_cfg.addr, AM_SPININ_SET_MODE, 0);
i2c_write_1word(user_data->device_cfg.addr, AM_SPININ_WRITE_VALUE, 0);
i2c_write_1word(user_data->device_cfg.addr, AM_SPININ_SET_MIN_VALUE, user_data->device_cfg.min_value);
i2c_write_1word(user_data->device_cfg.addr, AM_SPININ_SET_MAX_VALUE, user_data->device_cfg.max_value);
i2c_write_1word(user_data->device_cfg.addr, AM_SPININ_SET_SAMPLERATE, user_data->device_cfg.sample_rate);
#else
struct i2c_board_info i2c_board_info = {
I2C_BOARD_INFO("am_spinin", user_data->device_cfg.addr)
};
struct i2c_adapter* i2c_adap = i2c_get_adapter(1);
if (i2c_adap == NULL) {
am_log_err("i2c adapter open erro {%d}", 1);
return;
}
user_data->i2c = i2c_new_client_device(i2c_adap, &i2c_board_info);
if (IS_ERR_OR_NULL(user_data->i2c)) {
am_log_err("i2c device open erro {%d}", user_data->device_cfg.addr);
return;
}
i2c_put_adapter(i2c_adap);
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_SET_MODE, 0);
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_WRITE_VALUE, 0);
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_SET_MIN_VALUE, user_data->device_cfg.min_value);
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_SET_MAX_VALUE, user_data->device_cfg.max_value);
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_SET_SAMPLERATE, user_data->device_cfg.sample_rate);
#endif
} else if (user_data->device_cfg.comm_type == AM_SPININ_COMM_SPI) {
#if defined(USE_SPI_DIRECT)
spi_init(bcm_peri_base_probe(), 0xB0);
spi_setClockDivider(0x01);
spi_setDataMode(0);
spi_begin();
spi_chipSelect(user_data->device_cfg.addr);
__spi_trans_for_am_spinin(AM_SPININ_SET_MODE, 0);
__spi_trans_for_am_spinin(AM_SPININ_WRITE_VALUE, 0);
__spi_trans_for_am_spinin(AM_SPININ_SET_MIN_VALUE, user_data->device_cfg.min_value);
__spi_trans_for_am_spinin(AM_SPININ_SET_MAX_VALUE, user_data->device_cfg.max_value);
__spi_trans_for_am_spinin(AM_SPININ_SET_SAMPLERATE, user_data->device_cfg.sample_rate);
spi_end();
#else
struct spi_board_info spi_device_info = {
.modalias = "am_spinin",
.max_speed_hz = 1 * 1000 * 1000, // speed your device (slave) can handle
.bus_num = 0, // SPI 0
.chip_select = user_data->device_cfg.addr,
.mode = SPI_MODE_0 // SPI mode 0
};
struct spi_master* master = spi_busnum_to_master(spi_device_info.bus_num);
if (IS_ERR_OR_NULL(master)) {
am_log_err("spi master {%d} not found.\n", spi_device_info.bus_num);
return;
}
user_data->spi = spi_new_device(master, &spi_device_info);
if (IS_ERR_OR_NULL(user_data->spi)) {
am_log_err("spi open device error {%d}.\n", user_data->device_cfg.addr);
return;
}
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_SET_MODE, 0);
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_WRITE_VALUE, 0);
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_SET_MIN_VALUE, user_data->device_cfg.min_value);
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_SET_MAX_VALUE, user_data->device_cfg.max_value);
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_SET_SAMPLERATE, user_data->device_cfg.sample_rate);
#endif
} else {
return;
}
device_data->is_opend = TRUE;
}
static void check_input_device_for_am_spinin(input_device_data_t *device_data)
{
int i;
int addr, rotary_ppr, value;
device_am_spinin_data_t *user_data = (device_am_spinin_data_t *)device_data->data;
if (user_data == NULL) return;
rotary_ppr = user_data->device_cfg.rotary_ppr;
addr = user_data->device_cfg.addr;
if (user_data->device_cfg.comm_type == AM_SPININ_COMM_I2C) {
#if defined(USE_I2C_DIRECT)
value = i2c_raw_read_1word(addr);
if (value != 0) {
i2c_write_1word(addr, AM_SPININ_WRITE_VALUE, 0);
}
#else
if (!IS_ERR_OR_NULL(user_data->i2c)) {
value = i2c_smbus_read_word_data(user_data->i2c, AM_SPININ_READ_VALUE);
if (value < 0) {
return;
}
value = (short)value;
if (value != 0) {
i2c_smbus_write_word_data(user_data->i2c, AM_SPININ_WRITE_VALUE, 0);
}
} else {
value = 0;
}
#endif
} else if (user_data->device_cfg.comm_type == AM_SPININ_COMM_SPI) {
#if defined(USE_SPI_DIRECT)
spi_begin();
spi_chipSelect(addr);
value = __spi_trans_for_am_spinin(AM_SPININ_READ_VALUE, 0);
if (value != 0) {
__spi_trans_for_am_spinin(AM_SPININ_WRITE_VALUE, 0);
}
spi_end();
#else
if (!IS_ERR_OR_NULL(user_data->spi)) {
value = __spi_trans_for_am_spinin(user_data->spi, AM_SPININ_READ_VALUE, 0);
if (value != 0) {
__spi_trans_for_am_spinin(user_data->spi, AM_SPININ_WRITE_VALUE, 0);
}
} else {
value = 0;
}
#endif
} else {
return;
}
for (i = 0; i < device_data->target_endpoint_count; i++) {
input_endpoint_data_t* endpoint = device_data->target_endpoints[i];
device_am_spinin_index_table_t* table = &user_data->button_cfgs[i];
device_am_spinin_index_item_t* btndef = &table->buttondef[table->button_start_index];
int mouse_dpi = table->mouse_dpi;
endpoint->report_button_state[btndef->button] = value * mouse_dpi / rotary_ppr;
}
}
static void stop_input_device_for_am_spinin(input_device_data_t *device_data)
{
device_am_spinin_data_t *user_data = (device_am_spinin_data_t *)device_data->data;
device_data->is_opend = FALSE;
if (user_data->device_cfg.comm_type == AM_SPININ_COMM_I2C) {
#if defined(USE_I2C_DIRECT)
i2c_close();
#else
if (!IS_ERR_OR_NULL(user_data->i2c)) {
i2c_unregister_device(user_data->i2c);
user_data->i2c = NULL;
}
#endif
} else if (user_data->device_cfg.comm_type == AM_SPININ_COMM_SPI) {
#if defined(USE_SPI_DIRECT)
spi_close();
#else
if (!IS_ERR_OR_NULL(user_data->spi)) {
spi_unregister_device(user_data->spi);
user_data->spi = NULL;
}
#endif
}
}
int register_input_device_for_am_spinin(input_device_type_desc_t *device_desc)
{
strcpy(device_desc->device_type, "am_spinin");
device_desc->init_input_dev = init_input_device_for_am_spinin;
device_desc->start_input_dev = start_input_device_for_am_spinin;
device_desc->check_input_dev = check_input_device_for_am_spinin;
device_desc->stop_input_dev = stop_input_device_for_am_spinin;
return 0;
}