forked from ROCKNIX/rocknix-joypad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocknix-joypad.c
996 lines (856 loc) · 27 KB
/
rocknix-joypad.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* ROCKNIX joypad driver
*
* Copyright (C) 2024 ROCKNIX (https://github.com/ROCKNIX)
*/
/*----------------------------------------------------------------------------*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/input-polldev.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio_keys.h>
#include <linux/iio/consumer.h>
#include <linux/iio/types.h>
#include <linux/property.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0))
#include <linux/of_gpio.h>
#else
#include <linux/of_gpio_legacy.h>
#endif
#include <linux/delay.h>
#include <linux/pwm.h>
#include "rocknix-joypad.h"
/*----------------------------------------------------------------------------*/
#define DRV_NAME "rocknix-joypad"
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
#define ADC_MAX_VOLTAGE 1800
#define ADC_DATA_TUNING(x, p) ((x * p) / 100)
#define ADC_TUNING_DEFAULT 180
struct bt_adc {
/* report value (mV) */
int value;
/* report type */
int report_type;
/* input device init value (mV) */
int max, min;
/* calibrated adc value */
int cal;
/* adc scale value */
int scale;
/* invert report */
bool invert;
/* adc channel */
int channel;
/* adc data tuning value([percent), p = positive, n = negative */
int tuning_p, tuning_n;
};
struct bt_gpio {
/* GPIO Request label */
const char *label;
/* GPIO Number */
int num;
/* report type */
int report_type;
/* report linux code */
int linux_code;
/* prev button value */
bool old_value;
/* button press level */
bool active_level;
};
struct joypad {
struct device *dev;
struct input_polled_dev *poll_dev;
int poll_interval;
/* report enable/disable */
bool enable;
/* analog mux & joystick control */
struct iio_channel *adc_ch[SARADC_CH_NUM];
/* adc input channel count */
int chan_count;
/* analog button */
struct bt_adc *adcs;
/* report interval (ms) */
int bt_gpio_count;
struct bt_gpio *gpios;
/* button auto repeat */
int auto_repeat;
/* report threshold (mV) */
int bt_adc_fuzz, bt_adc_flat;
/* adc read value scale */
int bt_adc_scale;
/* joystick deadzone control */
int bt_adc_deadzone;
struct mutex lock;
/* adc debug channel */
int debug_ch;
};
/*----------------------------------------------------------------------------*/
//
// set to the value in the boot.ini file. (if exist)
//
/*----------------------------------------------------------------------------*/
static unsigned int g_button_adc_fuzz = 0;
static unsigned int g_button_adc_flat = 0;
static unsigned int g_button_adc_scale = 0;
static unsigned int g_button_adc_deadzone = 0;
static int button_adc_fuzz(char *str)
{
if (!str)
return -EINVAL;
g_button_adc_fuzz = simple_strtoul(str, NULL, 10);
return 0;
}
__setup("button-adc-fuzz=", button_adc_fuzz);
static int button_adc_flat(char *str)
{
if (!str)
return -EINVAL;
g_button_adc_flat = simple_strtoul(str, NULL, 10);
return 0;
}
__setup("button-adc-flat=", button_adc_flat);
static int button_adc_scale(char *str)
{
if (!str)
return -EINVAL;
g_button_adc_scale = simple_strtoul(str, NULL, 10);
return 0;
}
__setup("button-adc-scale=", button_adc_scale);
static int button_adc_deadzone(char *str)
{
if (!str)
return -EINVAL;
g_button_adc_deadzone = simple_strtoul(str, NULL, 10);
return 0;
}
__setup("button-adc-deadzone=", button_adc_deadzone);
/*----------------------------------------------------------------------------*/
static int joypad_adc_read(struct joypad *joypad, struct bt_adc *adc)
{
int value;
if (iio_read_channel_processed(joypad->adc_ch[adc->channel], &value) < 0)
return 0;
value *= adc->scale;
return (adc->invert ? (adc->max - value) : value);
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
* ATTRIBUTES:
*
* /sys/devices/platform/rocknix-joypad/poll_interval [rw]
*/
/*----------------------------------------------------------------------------*/
static ssize_t joypad_store_poll_interval(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
mutex_lock(&joypad->lock);
joypad->poll_interval = simple_strtoul(buf, NULL, 10);
mutex_unlock(&joypad->lock);
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t joypad_show_poll_interval(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", joypad->poll_interval);
}
/*----------------------------------------------------------------------------*/
static DEVICE_ATTR(poll_interval, S_IWUSR | S_IRUGO,
joypad_show_poll_interval,
joypad_store_poll_interval);
/*----------------------------------------------------------------------------*/
/*
* ATTRIBUTES:
*
* /sys/devices/platform/rocknix-joypad/adc_fuzz [r]
*/
/*----------------------------------------------------------------------------*/
static ssize_t joypad_show_adc_fuzz(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", joypad->bt_adc_fuzz);
}
/*----------------------------------------------------------------------------*/
static DEVICE_ATTR(adc_fuzz, S_IWUSR | S_IRUGO,
joypad_show_adc_fuzz,
NULL);
/*----------------------------------------------------------------------------*/
/*
* ATTRIBUTES:
*
* /sys/devices/platform/rocknix-joypad/adc_flat [r]
*/
/*----------------------------------------------------------------------------*/
static ssize_t joypad_show_adc_flat(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", joypad->bt_adc_flat);
}
/*----------------------------------------------------------------------------*/
static DEVICE_ATTR(adc_flat, S_IWUSR | S_IRUGO,
joypad_show_adc_flat,
NULL);
/*----------------------------------------------------------------------------*/
/*
* ATTRIBUTES:
*
* /sys/devices/platform/rocknix-joypad/enable [rw]
*/
/*----------------------------------------------------------------------------*/
static ssize_t joypad_store_enable(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
mutex_lock(&joypad->lock);
joypad->enable = simple_strtoul(buf, NULL, 10);
mutex_unlock(&joypad->lock);
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t joypad_show_enable(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
return sprintf(buf, "%d\n", joypad->enable);
}
/*----------------------------------------------------------------------------*/
static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO,
joypad_show_enable,
joypad_store_enable);
/*----------------------------------------------------------------------------*/
/*
* ATTRIBUTES:
*
* /sys/devices/platform/rocknix-joypad/adc_cal [rw]
*/
/*----------------------------------------------------------------------------*/
static ssize_t joypad_store_adc_cal(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
bool calibration;
calibration = simple_strtoul(buf, NULL, 10);
if (calibration) {
int nbtn;
mutex_lock(&joypad->lock);
for (nbtn = 0; nbtn < joypad->chan_count; nbtn++) {
struct bt_adc *adc = &joypad->adcs[nbtn];
adc->value = joypad_adc_read(joypad, adc);
if (!adc->value) {
dev_err(joypad->dev, "%s : saradc channels[%d]!\n",
__func__, nbtn);
continue;
}
adc->cal = adc->value;
}
mutex_unlock(&joypad->lock);
}
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t joypad_show_adc_cal(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct platform_device *pdev = to_platform_device(dev);
struct joypad *joypad = platform_get_drvdata(pdev);
int nbtn;
ssize_t pos;
for (nbtn = 0, pos = 0; nbtn < joypad->chan_count; nbtn++) {
struct bt_adc *adc = &joypad->adcs[nbtn];
pos += sprintf(&buf[pos], "adc[%d]->cal = %d\n",
nbtn, adc->cal);
}
pos += sprintf(&buf[pos], "adc scale = %d\n", joypad->bt_adc_scale);
return pos;
}
/*----------------------------------------------------------------------------*/
static DEVICE_ATTR(adc_cal, S_IWUSR | S_IRUGO,
joypad_show_adc_cal,
joypad_store_adc_cal);
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
static struct attribute *joypad_attrs[] = {
&dev_attr_poll_interval.attr,
&dev_attr_adc_fuzz.attr,
&dev_attr_adc_flat.attr,
&dev_attr_enable.attr,
&dev_attr_adc_cal.attr,
NULL,
};
static struct attribute_group joypad_attr_group = {
.attrs = joypad_attrs,
};
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
static void joypad_gpio_check(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int nbtn, value;
for (nbtn = 0; nbtn < joypad->bt_gpio_count; nbtn++) {
struct bt_gpio *gpio = &joypad->gpios[nbtn];
if (gpio_get_value_cansleep(gpio->num) < 0) {
dev_err(joypad->dev, "failed to get gpio state\n");
continue;
}
value = gpio_get_value(gpio->num);
if (value != gpio->old_value) {
input_event(poll_dev->input,
gpio->report_type,
gpio->linux_code,
(value == gpio->active_level) ? 1 : 0);
gpio->old_value = value;
}
}
input_sync(poll_dev->input);
}
/*----------------------------------------------------------------------------*/
static void joypad_adc_check(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int nbtn;
int mag;
/* Assumes an even number of axes and that joystick axis pairs are sequential */
/* e.g. left stick Y immediately follows left stick X */
for (nbtn = 0; nbtn < joypad->chan_count; nbtn += 2) {
struct bt_adc *adcx = &joypad->adcs[nbtn];
struct bt_adc *adcy = &joypad->adcs[nbtn + 1];
/* Read first joystick axis */
adcx->value = joypad_adc_read(joypad, adcx);
if (!adcx->value) {
dev_err(joypad->dev, "%s : saradc channels[%d]!\n",
__func__, nbtn);
continue;
}
adcx->value = adcx->value - adcx->cal;
/* Read second joystick axis */
adcy->value = joypad_adc_read(joypad, adcy);
if (!adcy->value) {
dev_err(joypad->dev, "%s : saradc channels[%d]!\n",
__func__, nbtn + 1);
continue;
}
adcy->value = adcy->value - adcy->cal;
/* Scaled Radial Deadzone */
/* https://web.archive.org/web/20190129113357/http://www.third-helix.com/2013/04/12/doing-thumbstick-dead-zones-right.html */
mag = int_sqrt((adcx->value * adcx->value) + (adcy->value * adcy->value));
if (joypad->bt_adc_deadzone) {
if (mag <= joypad->bt_adc_deadzone) {
adcx->value = 0;
adcy->value = 0;
}
else {
/* Assumes adcx->max == -adcx->min == adcy->max == -adcy->min */
/* Order of operations is critical to avoid integer overflow */
adcx->value = (((adcx->max * adcx->value) / mag) * (mag - joypad->bt_adc_deadzone)) / (adcx->max - joypad->bt_adc_deadzone);
adcy->value = (((adcy->max * adcy->value) / mag) * (mag - joypad->bt_adc_deadzone)) / (adcy->max - joypad->bt_adc_deadzone);
}
}
/* adc data tuning */
if (adcx->tuning_n && adcx->value < 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_n);
if (adcx->tuning_p && adcx->value > 0)
adcx->value = ADC_DATA_TUNING(adcx->value, adcx->tuning_p);
if (adcy->tuning_n && adcy->value < 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_n);
if (adcy->tuning_p && adcy->value > 0)
adcy->value = ADC_DATA_TUNING(adcy->value, adcy->tuning_p);
/* Clamp to [min, max] */
adcx->value = adcx->value > adcx->max ? adcx->max : adcx->value;
adcx->value = adcx->value < adcx->min ? adcx->min : adcx->value;
adcy->value = adcy->value > adcy->max ? adcy->max : adcy->value;
adcy->value = adcy->value < adcy->min ? adcy->min : adcy->value;
input_report_abs(poll_dev->input,
adcx->report_type,
adcx->invert ? adcx->value * (-1) : adcx->value);
input_report_abs(poll_dev->input,
adcy->report_type,
adcy->invert ? adcy->value * (-1) : adcy->value);
}
input_sync(poll_dev->input);
}
/*----------------------------------------------------------------------------*/
static void joypad_poll(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
if (joypad->enable) {
joypad_adc_check(poll_dev);
joypad_gpio_check(poll_dev);
}
if (poll_dev->poll_interval != joypad->poll_interval) {
mutex_lock(&joypad->lock);
poll_dev->poll_interval = joypad->poll_interval;
mutex_unlock(&joypad->lock);
}
}
/*----------------------------------------------------------------------------*/
static void joypad_open(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
int nbtn;
for (nbtn = 0; nbtn < joypad->bt_gpio_count; nbtn++) {
struct bt_gpio *gpio = &joypad->gpios[nbtn];
gpio->old_value = gpio->active_level ? 0 : 1;
}
for (nbtn = 0; nbtn < joypad->chan_count; nbtn++) {
struct bt_adc *adc = &joypad->adcs[nbtn];
adc->value = joypad_adc_read(joypad, adc);
if (!adc->value) {
dev_err(joypad->dev, "%s : saradc channels[%d]!\n",
__func__, nbtn);
continue;
}
adc->cal = adc->value;
dev_info(joypad->dev, "%s : adc[%d] adc->cal = %d\n",
__func__, nbtn, adc->cal);
}
/* buttons status sync */
joypad_adc_check(poll_dev);
joypad_gpio_check(poll_dev);
/* button report enable */
mutex_lock(&joypad->lock);
joypad->enable = true;
mutex_unlock(&joypad->lock);
dev_info(joypad->dev, "%s : opened\n", __func__);
}
/*----------------------------------------------------------------------------*/
static void joypad_close(struct input_polled_dev *poll_dev)
{
struct joypad *joypad = poll_dev->private;
/* button report disable */
mutex_lock(&joypad->lock);
joypad->enable = false;
mutex_unlock(&joypad->lock);
dev_info(joypad->dev, "%s : closed\n", __func__);
}
/*----------------------------------------------------------------------------*/
static int joypad_iochannel_setup(struct device *dev, struct joypad *joypad)
{
enum iio_chan_type type;
unsigned char cnt;
const char *uname;
int ret;
for (cnt = 0; cnt < joypad->chan_count; cnt++) {
ret = of_property_read_string_index(dev->of_node,
"io-channel-names", cnt, &uname);
if (ret < 0) {
dev_err(dev, "invalid channel name index[%d]\n", cnt);
return -EINVAL;
}
joypad->adc_ch[cnt] = devm_iio_channel_get(dev,
uname);
if (IS_ERR(joypad->adc_ch[cnt])) {
dev_err(dev, "iio channel get error\n");
return -EINVAL;
}
if (!joypad->adc_ch[cnt]->indio_dev)
return -ENXIO;
if (iio_get_channel_type(joypad->adc_ch[cnt], &type))
return -EINVAL;
if (type != IIO_VOLTAGE) {
dev_err(dev, "Incompatible channel type %d\n", type);
return -EINVAL;
}
}
return ret;
}
/*----------------------------------------------------------------------------*/
static int joypad_adc_setup(struct device *dev, struct joypad *joypad)
{
int nbtn;
/* adc button struct init */
joypad->adcs = devm_kzalloc(dev, joypad->chan_count *
sizeof(struct bt_adc), GFP_KERNEL);
if (!joypad->adcs) {
dev_err(dev, "%s devm_kzmalloc error!", __func__);
return -ENOMEM;
}
for (nbtn = 0; nbtn < joypad->chan_count; nbtn++) {
struct bt_adc *adc = &joypad->adcs[nbtn];
adc->scale = joypad->bt_adc_scale;
adc->max = (ADC_MAX_VOLTAGE / 2);
adc->min = (ADC_MAX_VOLTAGE / 2) * (-1);
if (adc->scale) {
adc->max *= adc->scale;
adc->min *= adc->scale;
}
adc->channel = nbtn;
adc->invert = false;
switch (nbtn) {
case 0:
adc->report_type = ABS_RY;
if (device_property_read_u32(dev,
"abs_ry-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_ry-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
case 1:
adc->report_type = ABS_RX;
if (device_property_read_u32(dev,
"abs_rx-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_rx-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
case 2:
adc->report_type = ABS_Y;
if (device_property_read_u32(dev,
"abs_y-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_y-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
case 3:
adc->report_type = ABS_X;
if (device_property_read_u32(dev,
"abs_x-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_x-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
case 4:
adc->invert = true;
adc->report_type = ABS_Z;
if (device_property_read_u32(dev,
"abs_z-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_z-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
case 5:
adc->invert = true;
adc->report_type = ABS_RZ;
if (device_property_read_u32(dev,
"abs_rz-p-tuning",
&adc->tuning_p))
adc->tuning_p = ADC_TUNING_DEFAULT;
if (device_property_read_u32(dev,
"abs_rz-n-tuning",
&adc->tuning_n))
adc->tuning_n = ADC_TUNING_DEFAULT;
break;
default :
dev_err(dev, "%s io channel count(%d) error!",
__func__, nbtn);
return -EINVAL;
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int joypad_gpio_setup(struct device *dev, struct joypad *joypad)
{
struct device_node *node, *pp;
int nbtn;
node = dev->of_node;
if (!node)
return -ENODEV;
joypad->gpios = devm_kzalloc(dev, joypad->bt_gpio_count *
sizeof(struct bt_gpio), GFP_KERNEL);
if (!joypad->gpios) {
dev_err(dev, "%s devm_kzmalloc error!", __func__);
return -ENOMEM;
}
nbtn = 0;
for_each_child_of_node(node, pp) {
enum of_gpio_flags flags;
struct bt_gpio *gpio = &joypad->gpios[nbtn++];
int error;
gpio->num = of_get_gpio_flags(pp, 0, &flags);
if (gpio->num < 0) {
error = gpio->num;
dev_err(dev, "Failed to get gpio flags, error: %d\n",
error);
return error;
}
/* gpio active level(key press level) */
gpio->active_level = (flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
gpio->label = of_get_property(pp, "label", NULL);
if (gpio_is_valid(gpio->num)) {
error = devm_gpio_request_one(dev, gpio->num,
GPIOF_IN, gpio->label);
if (error < 0) {
dev_err(dev,
"Failed to request GPIO %d, error %d\n",
gpio->num, error);
return error;
}
#if 0
error = gpiod_set_pull(gpio_to_desc(gpio->num), GPIOD_PULL_UP);
if (error < 0) {
dev_err(dev,
"Failed to set pull-up GPIO %d, error %d\n",
gpio->num, error);
return error;
}
#endif
}
if (of_property_read_u32(pp, "linux,code", &gpio->linux_code)) {
dev_err(dev, "Button without keycode: 0x%x\n",
gpio->num);
return -EINVAL;
}
if (of_property_read_u32(pp, "linux,input-type",
&gpio->report_type))
gpio->report_type = EV_KEY;
}
if (nbtn == 0)
return -EINVAL;
return 0;
}
/*----------------------------------------------------------------------------*/
static int joypad_input_setup(struct device *dev, struct joypad *joypad)
{
struct input_polled_dev *poll_dev;
struct input_dev *input;
int nbtn, error;
u32 joypad_vendor = 0;
u32 joypad_revision = 0;
u32 joypad_product = 0;
poll_dev = devm_input_allocate_polled_device(dev);
if (!poll_dev) {
dev_err(dev, "no memory for polled device\n");
return -ENOMEM;
}
poll_dev->private = joypad;
poll_dev->poll = joypad_poll;
poll_dev->poll_interval = joypad->poll_interval;
poll_dev->open = joypad_open;
poll_dev->close = joypad_close;
input = poll_dev->input;
input->name = DRV_NAME;
device_property_read_string(dev, "joypad-name", &input->name);
input->phys = DRV_NAME"/input0";
device_property_read_u32(dev, "joypad-vendor", &joypad_vendor);
device_property_read_u32(dev, "joypad-revision", &joypad_revision);
device_property_read_u32(dev, "joypad-product", &joypad_product);
//input->id.bustype = BUS_HOST;
input->id.bustype = BUS_USB;
input->id.vendor = (u16)joypad_vendor;
input->id.product = (u16)joypad_product;
input->id.version = (u16)joypad_revision;
/* IIO ADC key setup (0 mv ~ 1800 mv) * adc->scale */
__set_bit(EV_ABS, input->evbit);
// Set mapped ones on dt
for(nbtn = 0; nbtn < joypad->chan_count; nbtn++) {
struct bt_adc *adc = &joypad->adcs[nbtn];
input_set_abs_params(input, adc->report_type,
adc->min, adc->max,
joypad->bt_adc_fuzz,
joypad->bt_adc_flat);
dev_info(dev,
"%s : SCALE = %d, ABS min = %d, max = %d,"
" fuzz = %d, flat = %d, deadzone = %d\n",
__func__, adc->scale, adc->min, adc->max,
joypad->bt_adc_fuzz, joypad->bt_adc_flat,
joypad->bt_adc_deadzone);
dev_info(dev,
"%s : adc tuning_p = %d, adc_tuning_n = %d\n\n",
__func__, adc->tuning_p, adc->tuning_n);
}
/* GPIO key setup */
__set_bit(EV_KEY, input->evbit);
for(nbtn = 0; nbtn < joypad->bt_gpio_count; nbtn++) {
struct bt_gpio *gpio = &joypad->gpios[nbtn];
input_set_capability(input, gpio->report_type,
gpio->linux_code);
}
if (joypad->auto_repeat)
__set_bit(EV_REP, input->evbit);
joypad->dev = dev;
error = input_register_polled_device(poll_dev);
if (error) {
dev_err(dev, "unable to register polled device, err=%d\n",
error);
return error;
}
joypad->dev = dev;
joypad->poll_dev = poll_dev;
return 0;
}
/*----------------------------------------------------------------------------*/
static void joypad_setup_value_check(struct device *dev, struct joypad *joypad)
{
/*
fuzz: specifies fuzz value that is used to filter noise from
the event stream.
*/
if (g_button_adc_fuzz)
joypad->bt_adc_fuzz = g_button_adc_fuzz;
else
device_property_read_u32(dev, "button-adc-fuzz",
&joypad->bt_adc_fuzz);
/*
flat: values that are within this value will be discarded by
joydev interface and reported as 0 instead.
*/
if (g_button_adc_flat)
joypad->bt_adc_flat = g_button_adc_flat;
else
device_property_read_u32(dev, "button-adc-flat",
&joypad->bt_adc_flat);
/* Joystick report value control */
if (g_button_adc_scale)
joypad->bt_adc_scale = g_button_adc_scale;
else
device_property_read_u32(dev, "button-adc-scale",
&joypad->bt_adc_scale);
/* Joystick deadzone value control */
if (g_button_adc_deadzone)
joypad->bt_adc_deadzone = g_button_adc_deadzone;
else
device_property_read_u32(dev, "button-adc-deadzone",
&joypad->bt_adc_deadzone);
}
/*----------------------------------------------------------------------------*/
static int joypad_dt_parse(struct device *dev, struct joypad *joypad)
{
int error = 0;
/* initialize value check from boot.ini */
joypad_setup_value_check(dev, joypad);
joypad->chan_count = of_property_count_strings(dev->of_node,
"io-channel-names");
device_property_read_u32(dev, "poll-interval",
&joypad->poll_interval);
joypad->auto_repeat = device_property_present(dev, "autorepeat");
joypad->bt_gpio_count = device_get_child_node_count(dev);
if ((joypad->chan_count == 0) || (joypad->bt_gpio_count == 0)) {
dev_err(dev, "adc key = %d, gpio key = %d error!",
joypad->chan_count, joypad->bt_gpio_count);
return -EINVAL;
}
error = joypad_adc_setup(dev, joypad);
if (error)
return error;
error = joypad_iochannel_setup(dev, joypad);
if (error)
return error;
error = joypad_gpio_setup(dev, joypad);
if (error)
return error;
dev_info(dev, "%s : adc key cnt = %d, gpio key cnt = %d\n",
__func__, joypad->chan_count, joypad->bt_gpio_count);
return error;
}
/*----------------------------------------------------------------------------*/
static int joypad_probe(struct platform_device *pdev)
{
struct joypad *joypad;
struct device *dev = &pdev->dev;
int error;
joypad = devm_kzalloc(dev, sizeof(struct joypad), GFP_KERNEL);
if (!joypad) {
dev_err(dev, "joypad devm_kzmalloc error!");
return -ENOMEM;
}
/* device tree data parse */
error = joypad_dt_parse(dev, joypad);
if (error) {
dev_err(dev, "dt parse error!(err = %d)\n", error);
return error;
}
mutex_init(&joypad->lock);
platform_set_drvdata(pdev, joypad);
error = sysfs_create_group(&pdev->dev.kobj, &joypad_attr_group);
if (error) {
dev_err(dev, "create sysfs group fail, error: %d\n",
error);
return error;
}
/* poll input device setup */
error = joypad_input_setup(dev, joypad);
if (error) {
dev_err(dev, "input setup failed!(err = %d)\n", error);
return error;
}
dev_info(dev, "%s : probe success\n", __func__);
return 0;
}
static void joypad_shutdown(struct platform_device *pdev)
{
struct joypad *joypad = platform_get_drvdata(pdev);
input_unregister_polled_device(joypad->poll_dev);
}
/*----------------------------------------------------------------------------*/
static const struct of_device_id joypad_of_match[] = {
{ .compatible = "rocknix-joypad", },
{},
};
MODULE_DEVICE_TABLE(of, joypad_of_match);
/*----------------------------------------------------------------------------*/
static struct platform_driver joypad_driver = {
.probe = joypad_probe,
.shutdown = joypad_shutdown,
.driver = {
.name = DRV_NAME,
.of_match_table = of_match_ptr(joypad_of_match),
},
};
/*----------------------------------------------------------------------------*/
static int __init joypad_init(void)
{
return platform_driver_register(&joypad_driver);
}
/*----------------------------------------------------------------------------*/
static void __exit joypad_exit(void)
{
platform_driver_unregister(&joypad_driver);
}
/*----------------------------------------------------------------------------*/
late_initcall(joypad_init);
module_exit(joypad_exit);
/*----------------------------------------------------------------------------*/
MODULE_AUTHOR("ROCKNIX");
MODULE_DESCRIPTION("ROCKNIX joypad driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);
MODULE_INFO(intree, "Y");
/*----------------------------------------------------------------------------*/