forked from Henneberg-Systemdesign/cp2130
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi-cp2130.c
1642 lines (1370 loc) · 41.4 KB
/
spi-cp2130.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
997
998
999
1000
/**
* Kernel driver for CP2130 USB<->SPI bridge.
* Copyright (C) 2016 Jochen Henneberg (jh@henneberg-systemdesign.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.
*/
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/spi/spi.h>
#include <linux/workqueue.h>
#include <linux/string.h>
#include <asm/byteorder.h>
#include <linux/interrupt.h>
#include <linux/irqdomain.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/version.h>
#define USB_DEVICE_ID_CP2130 0x87a0
#define USB_VENDOR_ID_CYGNAL 0x10c4
#define CP2130_NUM_GPIOS 11
#define CP2130_IRQ_POLL_INTERVAL 1 * 1000 * 1000 /* us */
#define CP2130_MAX_USB_PACKET_SIZE 64
#define CP2130_CMD_READ 0x00
#define CP2130_CMD_WRITE 0x01
#define CP2130_CMD_WRITEREAD 0x02
#define CP2130_BULK_OFFSET_CMD 2
#define CP2130_BULK_OFFSET_LENGTH 4
#define CP2130_BULK_OFFSET_DATA 8
#define CP2130_BREQ_GET_GPIO_VALUES 0x20
#define CP2130_BREQ_SET_GPIO_MODE 0x23
#define CP2130_BREQ_GET_GPIO_CS 0x24
#define CP2130_BREQ_SET_GPIO_CS 0x25
#define CP2130_BREQ_GET_SPI_WORD 0x30
#define CP2130_BREQ_SET_SPI_WORD 0x31
#define CP2130_BREQ_GET_SPI_DELAY 0x32
#define CP2130_BREQ_SET_SPI_DELAY 0x33
#define CP2130_BREQ_GET_LOCK_BYTE 0x6E
#define CP2130_BREQ_GET_PIN_CONFIG 0x6C
#define CP2130_BREQ_SET_PIN_CONFIG 0x6D
#define CP2130_BREQ_MEMORY_KEY 0xA5F1
/* cp2130 attached chip */
struct cp2130_channel {
int updated;
int cs_en; /* chip-select enable mode */
int irq_pin;
int clock_phase;
int polarity;
int cs_pin_mode;
int clock_freq; /* spi clock frequency */
int delay_mask; /* cs enable, pre-deassert,
post assert and inter-byte delay enable */
int inter_byte_delay;
int pre_deassert_delay;
int post_assert_delay;
char *modalias;
void *pdata;
struct spi_device *chip;
};
/* cp2130 OTP ROM */
struct cp2130_otprom {
int lock_byte;
int pin_config[CP2130_NUM_GPIOS];
int suspend_level;
int suspend_mode;
int wakeup_mask;
int wakeup_match;
int divider;
};
struct cp2130_gpio_irq {
struct irq_domain *irq_domain;
struct mutex irq_lock;
int virq[CP2130_NUM_GPIOS];
u16 irq_mask;
};
/* cp2130 device structure */
struct cp2130_device {
struct usb_device *udev;
struct usb_interface *intf;
struct spi_master *spi_master;
struct mutex chn_config_lock;
struct mutex otprom_lock;
struct mutex usb_bus_lock;
struct work_struct update_chn_config;
struct work_struct read_chn_config;
struct work_struct update_otprom;
struct work_struct read_otprom;
struct cp2130_channel chn_configs[CP2130_NUM_GPIOS];
struct cp2130_otprom otprom_config;
struct cp2130_gpio_irq irq_chip;
struct work_struct irq_work;
struct gpio_chip gpio_chip;
char *gpio_names[CP2130_NUM_GPIOS];
u8 gpio_states[2];
int current_channel;
int irq_poll_interval;
u8 *usb_xfer;
};
/* USB device functions */
static int cp2130_probe(struct usb_interface *intf,
const struct usb_device_id *id);
static void cp2130_disconnect(struct usb_interface *intf);
static const struct usb_device_id cp2130_devices[] = {
{ USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CP2130) },
{ }
};
static struct usb_driver cp2130_driver = {
.name = "cp2130",
.probe = cp2130_probe,
.disconnect = cp2130_disconnect,
.suspend = NULL,
.resume = NULL,
.reset_resume = NULL,
.id_table = cp2130_devices,
.supports_autosuspend = 0,
};
static int __init cp2130_init(void)
{
int ret;
printk(KERN_DEBUG "cp2130_init\n");
ret = usb_register_driver(&cp2130_driver, THIS_MODULE, "cp2130");
if (ret)
printk(KERN_ERR "can't register cp2130 driver\n");
return ret;
}
static void __exit cp2130_exit(void)
{
printk(KERN_DEBUG "cp2130_exit\n");
usb_deregister(&cp2130_driver);
}
static int cp2130_spi_setup(struct spi_device *spi)
{
return 0;
}
static void cp2130_spi_cleanup(struct spi_device *spi)
{
}
static char* cp2130_spi_speed_to_string(int reg_val)
{
switch (reg_val) {
case 0: return "12 MHz ";
case 1: return "6 MHz ";
case 2: return "3 MHz ";
case 3: return "1.5 MHz ";
case 4: return "750 kHz ";
case 5: return "375 kHz ";
case 6: return "187.5 kHz";
case 7: return "93.8 kHz ";
}
return "";
}
/*
* This is a workaround for older versions of linux which do not have
* gpiochip_add(). In this module, the data parameter passed to
* gpiochip_add_data is always the struct cp2130_device that contains the struct
* gpio_chip that is passed as the first parameter. Based on this assumption we
* can simulate the correct behaviour using container_of() to get the struct
* cp2130_device from the struct gpio_chip.
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
static inline struct cp2130_device *gpiochip_get_data(struct gpio_chip *chip)
{
return container_of(chip, struct cp2130_device, gpio_chip);
}
static inline int gpiochip_add_data(struct gpio_chip *chip, void *data)
{
return gpiochip_add(chip);
}
#endif
/*
* This is a workaround for older versions of linux which have a __must_check
* return type of int on the gpiochip_remove() function.
*/
static inline void gpiochip_remove_(struct gpio_chip *chip)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
if (gpiochip_remove(chip) < 0)
dev_err(&gpiochip_get_data(chip)->intf->dev,
"failed to remove GPIO chip");
#else
gpiochip_remove(chip);
#endif
}
static ssize_t channel_config_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
struct cp2130_channel *chn;
int i = 0;
char out[256];
ssize_t ret;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
ret = sprintf(out, "channel\tcs_mode\tirq_pin\tclock_phase\tpolarity"
"\tcs_pin_mode\tclock_freq\tdelay_mask"
"\tinter_byte_delay\tpre_delay\tpost_delay"
"\tmod_alias\n");
strcat(buf, out);
mutex_lock(&chip->chn_config_lock);
for (i = 0; i < CP2130_NUM_GPIOS; i++) {
chn = &chip->chn_configs[i];
ret += sprintf(out, "%d\t%d\t%d\t%d\t\t%d\t\t%d\t\t%s\t%d"
"\t\t%d\t\t\t%d\t\t%d\t\t'%s'\n",
i, chn->cs_en, chn->irq_pin, chn->clock_phase,
chn->polarity, chn->cs_pin_mode,
cp2130_spi_speed_to_string(chn->clock_freq),
chn->delay_mask, chn->inter_byte_delay,
chn->pre_deassert_delay, chn->post_assert_delay,
chn->modalias);
strcat(buf, out);
}
mutex_unlock(&chip->chn_config_lock);
return ret;
}
static ssize_t channel_config_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct cp2130_channel chn;
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
char *lbuf;
char *del; /* delimiter ',' */
char *pos; /* current item position in buffer */
int i, ret, eos, chn_id;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
dev_dbg(&udev->dev, "received '%s' from 'channel_config'", buf);
if (!count)
return -EINVAL;
lbuf = kstrdup(buf, GFP_KERNEL);
if (!lbuf)
return -ENOMEM;
pos = lbuf;
eos = i = 0;
do {
/* search for next separator or end-of-string */
del = strchr(pos, ',');
if (!del)
del = strchr(pos, '\0');
if (!del) {
ret = -EINVAL;
goto out;
}
if (*del == '\0')
eos = 1;
*del = '\0';
dev_dbg(&udev->dev, "parsing: %s(%d)", pos, i);
/* parse current item */
switch (i) {
case 0: /* channel id */
ret = kstrtoint(pos, 10, &chn_id);
ret |= (chn_id < 0 || chn_id > 10) ? -EINVAL : 0;
if (ret)
goto out;
break;
case 1: /* chip-select enable */
ret = kstrtoint(pos, 10, &chn.cs_en);
ret |= (chn.cs_en < 0 || chn.cs_en > 2) ? -EINVAL : 0;
if (ret)
goto out;
break;
case 2: /* irq pin */
ret = kstrtoint(pos, 10, &chn.irq_pin);
ret |= (chn.irq_pin > 10) ? -EINVAL : 0;
if (ret)
goto out;
break;
case 3: /* clock phase */
ret = kstrtoint(pos, 10, &chn.clock_phase);
if (ret)
goto out;
chn.clock_phase = !!chn.clock_phase;
break;
case 4: /* polarity */
ret = kstrtoint(pos, 10, &chn.polarity);
if (ret)
goto out;
chn.polarity = !!chn.polarity;
break;
case 5: /* chip-select pin mode */
ret = kstrtoint(pos, 10, &chn.cs_pin_mode);
if (ret)
goto out;
chn.cs_pin_mode = !!chn.cs_pin_mode;
break;
case 6: /* spi clock frequency */
ret = kstrtoint(pos, 10, &chn.clock_freq);
ret |= (chn.clock_freq < 0 || chn.clock_freq > 7) ?
-EINVAL : 0;
if (ret)
goto out;
break;
case 7: /* delay mask */
ret = kstrtoint(pos, 10, &chn.delay_mask);
ret |= (chn.delay_mask < 0 || chn.delay_mask > 15) ?
-EINVAL : 0;
if (ret)
goto out;
break;
case 8: /* inter-byte delay */
ret = kstrtoint(pos, 10, &chn.inter_byte_delay);
ret |= (chn.inter_byte_delay < 0 ||
chn.inter_byte_delay > 0xffff) ?
-EINVAL : 0;
if (ret)
goto out;
break;
case 9: /* pre-deassert delay */
ret = kstrtoint(pos, 10, &chn.pre_deassert_delay);
ret |= (chn.pre_deassert_delay < 0 ||
chn.pre_deassert_delay > 0xffff) ?
-EINVAL : 0;
if (ret)
goto out;
break;
case 10: /* post-assert delay */
ret = kstrtoint(pos, 10, &chn.post_assert_delay);
ret |= (chn.post_assert_delay < 0 ||
chn.post_assert_delay > 0xffff) ?
-EINVAL : 0;
if (ret)
goto out;
break;
case 11: /* modalias */
chn.modalias = kstrdup(pos, GFP_KERNEL);
if (!chn.modalias) {
ret = -EINVAL;
goto out;
}
break;
default:
ret = -EINVAL;
goto out;
}
/* move the parser position forward */
pos = ++del;
i++;
} while (!eos);
if (i < 11) {
ret = -EINVAL;
goto out;
}
chn.updated = 1;
mutex_lock(&chip->chn_config_lock);
if (chip->chn_configs[chn_id].updated) {
ret = -EINVAL;
goto unlock;
}
/* preserve pdata */
chn.pdata = chip->chn_configs[chn_id].pdata;
memcpy(&chip->chn_configs[chn_id], &chn, sizeof(chn));
schedule_work(&chip->update_chn_config);
ret = count;
unlock:
mutex_unlock(&chip->chn_config_lock);
out:
kfree(lbuf);
return ret;
}
static DEVICE_ATTR_RW(channel_config);
static ssize_t channel_pdata_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
struct cp2130_channel *chn;
int chn_id;
int ret;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
/* first byte is channel id,
then follows binary platform data */
if (count < 2)
return -EINVAL;
chn_id = buf[0];
dev_dbg(&udev->dev, "received pdata for channel %u", chn_id);
if (chn_id < 0 || chn_id >= CP2130_NUM_GPIOS)
return -EINVAL;
chn = &chip->chn_configs[chn_id];
/* pdata can be set only once */
if (chn->pdata) {
ret = -EINVAL;
goto out;
}
dev_dbg(&udev->dev, "set pdata for channel %u", chn_id);
mutex_lock(&chip->chn_config_lock);
chn->pdata = kzalloc(count - 1, GFP_KERNEL);
if (!chn->pdata) {
ret = -ENOMEM;
goto out;
}
if (!memcpy(chn->pdata, buf + 1, count - 1)) {
kfree(chn->pdata);
chn->pdata = NULL;
ret = -EIO;
goto out;
}
ret = count;
out:
mutex_unlock(&chip->chn_config_lock);
return ret;
}
static DEVICE_ATTR_WO(channel_pdata);
static char* cp2130_pin_mode_to_string(int val)
{
switch (val) {
case 0: return "in ";
case 1: return "open-drain";
case 2: return "push-pull ";
case 3: return "nCS ";
default: return "special ";
}
return "";
}
static ssize_t otp_rom_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
int pin;
int i = 0;
char out[256];
ssize_t ret;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
mutex_lock(&chip->otprom_lock);
ret = sprintf(out, "OTP lock status (ro):"
"\nvid\t\tpid\t\tmax_power\tpower_mode"
"\tversion\t\tmanu_2\t\tmanu_1"
"\t\tpriority\tproduct_1\tproduct_2\tserial"
"\t\tpin_config\n");
strcat(buf, out);
for (i = 0; i < 12; i++) {
if (!!((chip->otprom_config.lock_byte >> i) & 1))
ret += sprintf(out, "unlocked\t");
else
ret += sprintf(out, "locked\t\t");
strcat(buf, out);
}
ret += sprintf(out, "\n\nOTP pin configuration (rw):\n");
strcat(buf, out);
for (i = 0; i < CP2130_NUM_GPIOS; i++) {
ret += sprintf(out, "pin %d\t\t", i);
strcat(buf, out);
}
strcat(buf, "\n");
ret++;
for (i = 0; i < CP2130_NUM_GPIOS; i++) {
pin = chip->otprom_config.pin_config[i];
ret += sprintf(out, "%s\t", cp2130_pin_mode_to_string(pin));
strcat(buf, out);
}
ret += sprintf(out, "\n\nOTP pin configuration extra data(ro):"
"\nsuspend_level\tsuspend_mode\twakeup_mask\twakeup_match"
"\tdivider\n");
strcat(buf, out);
ret += sprintf(out, "%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
chip->otprom_config.suspend_level,
chip->otprom_config.suspend_mode,
chip->otprom_config.wakeup_mask,
chip->otprom_config.wakeup_match,
chip->otprom_config.divider);
strcat(buf, out);
mutex_unlock(&chip->otprom_lock);
return ret;
}
static ssize_t otp_rom_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
int pin_config[CP2130_NUM_GPIOS];
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
char *lbuf;
char *del; /* delimiter ',' */
char *pos; /* current item position in buffer */
int i, ret, eos;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
dev_dbg(&udev->dev, "received '%s' from 'otp_rom'", buf);
if (!count)
return -EINVAL;
lbuf = kstrdup(buf, GFP_KERNEL);
if (!lbuf)
return -ENOMEM;
pos = lbuf;
eos = i = 0;
do {
/* search for next separator or end-of-string */
del = strchr(pos, ',');
if (!del)
del = strchr(pos, '\0');
if (!del) {
ret = -EINVAL;
goto out;
}
if (*del == '\0')
eos = 1;
*del = '\0';
dev_dbg(&udev->dev, "parsing: %s(%d)", pos, i);
if (i == CP2130_NUM_GPIOS) {
ret = -EINVAL;
goto out;
}
/* parse current item */
if (*pos == 'x') {
pin_config[i] = -1;
} else {
ret = kstrtoint(pos, 10, &(pin_config[i]));
ret |= (pin_config[i] < 0 ||
pin_config[i] > 3) ?
-EINVAL : 0;
if (ret)
goto out;
}
/* move the parser position forward */
pos = ++del;
i++;
} while (!eos);
if (i < 11) {
ret = -EINVAL;
goto out;
}
mutex_lock(&chip->otprom_lock);
for (i = 0; i < CP2130_NUM_GPIOS; i++) {
if (pin_config[i] < 0)
continue;
printk(KERN_INFO "cp2130 read OTP: %d", pin_config[i]);
chip->otprom_config.pin_config[i] = pin_config[i];
}
schedule_work(&chip->update_otprom);
ret = count;
mutex_unlock(&chip->otprom_lock);
out:
kfree(lbuf);
return ret;
}
static DEVICE_ATTR_RW(otp_rom);
static ssize_t irq_poll_interval_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
ssize_t ret;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
ret = sprintf(buf, "%d us\n", chip->irq_poll_interval);
return ret;
}
static ssize_t irq_poll_interval_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct usb_interface *intf;
struct usb_device *udev;
struct cp2130_device *chip;
int ret, interval;
intf = to_usb_interface(dev);
if (!intf)
return -EFAULT;
chip = usb_get_intfdata(intf);
if (!chip)
return -EFAULT;
udev = usb_get_dev(interface_to_usbdev(intf));
if (!udev)
return -EFAULT;
ret = kstrtoint(buf, 10, &interval);
/* we only allow numbers and nothing below 10us */
if (ret || interval < 10)
return -EINVAL;
chip->irq_poll_interval = interval;
return count;
}
static DEVICE_ATTR_RW(irq_poll_interval);
static int cp2130_spi_transfer_one_message(struct spi_master *master,
struct spi_message *mesg)
{
struct spi_transfer *xfer;
struct cp2130_device *dev =
(struct cp2130_device*) spi_master_get_devdata(master);
int len, ret = 0, chn_id;
struct cp2130_channel *chn;
unsigned int recv_pipe, xmit_pipe;
unsigned int xmit_ctrl_pipe;
dev_dbg(&master->dev, "spi transfer one message");
xmit_pipe = usb_sndbulkpipe(dev->udev, 0x01);
recv_pipe = usb_rcvbulkpipe(dev->udev, 0x82);
dev_dbg(&master->dev, "recv/xmit pipes: %u / %u",
recv_pipe, xmit_pipe);
/* search for spi setup of this device */
for (chn_id = 0; chn_id < CP2130_NUM_GPIOS; chn_id++) {
chn = &dev->chn_configs[chn_id];
if (chn->chip == mesg->spi)
break;
}
if (chn_id == CP2130_NUM_GPIOS) {
ret = -ENODEV;
goto out_notfound;
}
mutex_lock(&dev->usb_bus_lock);
xmit_ctrl_pipe = usb_sndctrlpipe(dev->udev, 0);
if (chn_id != dev->current_channel) {
dev_dbg(&dev->udev->dev, "load setup %d for channel %d",
chn->cs_en, chn_id);
dev->usb_xfer[0] = chn_id;
dev->usb_xfer[1] = chn->cs_en;
ret = usb_control_msg(
dev->udev, xmit_ctrl_pipe,
CP2130_BREQ_SET_GPIO_CS,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0, 0,
dev->usb_xfer, 2, 200);
if (ret != 2)
goto out;
dev->current_channel = chn_id;
}
/* iterate through all transfers */
list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
dev_dbg(&master->dev, "spi transfer stats: %p, %p, %d",
xfer->tx_buf, xfer->rx_buf, xfer->len);
/* empty transfer */
if (!xfer->tx_buf && !xfer->rx_buf) {
udelay(xfer->delay_usecs);
continue;
}
/* init length field */
*((u32*) (dev->usb_xfer + CP2130_BULK_OFFSET_LENGTH)) =
__cpu_to_le32(xfer->len);
/* copy SPI tx data */
if (xfer->tx_buf)
memcpy(dev->usb_xfer + CP2130_BULK_OFFSET_DATA,
xfer->tx_buf, xfer->len);
/* prepare URB and submit sync
CP2130 / AN792 p.7: 'any previous data transfer command
must complete before another data transfer command
is issued', so there is no advantage from using the
async USB API */
if (xfer->tx_buf && xfer->rx_buf) {
dev->usb_xfer[CP2130_BULK_OFFSET_CMD] =
CP2130_CMD_WRITEREAD;
/* usb write */
ret = usb_bulk_msg(dev->udev, xmit_pipe, dev->usb_xfer,
CP2130_BULK_OFFSET_DATA + xfer->len,
&len, 200);
dev_dbg(&master->dev, "usb write %d", ret);
if (ret)
break;
/* usb read */
ret = usb_bulk_msg(dev->udev, recv_pipe,
xfer->rx_buf, xfer->len,
&len, 200);
dev_dbg(&master->dev, "usb read %d", ret);
if (ret)
break;
} else if (!xfer->rx_buf) {
/* prepare URB and submit sync */
dev->usb_xfer[CP2130_BULK_OFFSET_CMD] =
CP2130_CMD_WRITE;
/* usb write */
ret = usb_bulk_msg(dev->udev, xmit_pipe, dev->usb_xfer,
CP2130_BULK_OFFSET_DATA + xfer->len,
&len, 200);
if (ret)
break;
} else if (!xfer->tx_buf) {
/* prepare URB and submit sync */
dev->usb_xfer[CP2130_BULK_OFFSET_CMD] = CP2130_CMD_READ;
/* usb write */
ret = usb_bulk_msg(dev->udev, xmit_pipe, dev->usb_xfer,
CP2130_BULK_OFFSET_DATA,
&len, 200);
if (ret)
break;
/* usb read */
ret = usb_bulk_msg(dev->udev, recv_pipe, xfer->rx_buf,
xfer->len, &len, 200);
if (ret)
break;
}
udelay(xfer->delay_usecs);
mesg->actual_length += xfer->len;
}
out:
mutex_unlock(&dev->usb_bus_lock);
out_notfound:
mesg->status = ret;
if (ret)
dev_err(&master->dev, "USB transfer failed with %d", ret);
spi_finalize_current_message(master); /* signal done to queue */
return ret;
}
static int cp2130_irq_from_pin(struct cp2130_device *dev, int pin)
{
return dev->irq_chip.virq[pin];
}
static void cp2130_update_channel_config(struct work_struct *work)
{
int i;
int ret;
unsigned int xmit_pipe;
struct cp2130_device *dev = container_of(work,
struct cp2130_device,
update_chn_config);
struct cp2130_channel *chn;
dev_dbg(&dev->udev->dev, "control pipes: %u, %u",
usb_sndctrlpipe(dev->udev, 0),
usb_rcvctrlpipe(dev->udev, 0));
xmit_pipe = usb_sndctrlpipe(dev->udev, 0);
mutex_lock(&dev->chn_config_lock);
for (i = 0; i < CP2130_NUM_GPIOS; i++) {
chn = &dev->chn_configs[i];
if (!chn->updated)
continue;
if (chn->updated > 1)
continue;
chn->updated++;
dev_dbg(&dev->udev->dev, "update config of channel %d", i);
dev->usb_xfer[0] = i;
mutex_lock(&dev->usb_bus_lock);
dev_dbg(&dev->udev->dev, "set spi word");
dev->usb_xfer[1] = (chn->clock_freq << 0) |
(chn->cs_pin_mode << 3) |
(chn->polarity << 4) |
(chn->clock_phase << 5);
ret = usb_control_msg(
dev->udev, xmit_pipe,
CP2130_BREQ_SET_SPI_WORD,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0, 0,
dev->usb_xfer, 2, 200);
if (ret < 2)
goto error_unlock;
dev_dbg(&dev->udev->dev, "set spi delay");
dev->usb_xfer[1] = chn->delay_mask;
dev->usb_xfer[2] = (chn->inter_byte_delay & 0xff00) >> 8;
dev->usb_xfer[3] = (chn->inter_byte_delay & 0x00ff) >> 0;
dev->usb_xfer[4] = (chn->post_assert_delay & 0xff00) >> 8;
dev->usb_xfer[5] = (chn->post_assert_delay & 0x00ff) >> 0;
dev->usb_xfer[6] = (chn->pre_deassert_delay & 0xff00) >> 8;
dev->usb_xfer[7] = (chn->pre_deassert_delay & 0x00ff) >> 0;
ret = usb_control_msg(
dev->udev, xmit_pipe,
CP2130_BREQ_SET_SPI_DELAY,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0, 0,
dev->usb_xfer, 8, 200);
if (ret < 8)
goto error_unlock;
/* configure irq pin as input if required */
if (chn->irq_pin >= 0) {
dev->usb_xfer[0] = chn->irq_pin;
dev->usb_xfer[1] = 0; /* input */
dev->usb_xfer[2] = 0; /* value, ignored for input */
ret = usb_control_msg(
dev->udev, xmit_pipe,
CP2130_BREQ_SET_GPIO_MODE,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0, 0,
dev->usb_xfer, 3, 200);
if (ret < 3)
goto error_unlock;
}
mutex_unlock(&dev->usb_bus_lock);
dev_dbg(&dev->udev->dev, "try register %s", chn->modalias);
chn->chip = spi_alloc_device(dev->spi_master);
if (!chn->chip)
goto error;
chn->chip->max_speed_hz = dev->spi_master->max_speed_hz;
chn->chip->chip_select = i;
chn->chip->mode = (chn->polarity << 1) | chn->clock_phase;
chn->chip->bits_per_word = 8; /* we can only do this */
chn->chip->irq = cp2130_irq_from_pin(dev, chn->irq_pin);
chn->chip->dev.platform_data = chn->pdata;
dev_dbg(&dev->udev->dev, "initialized %s chip", chn->modalias);
strncpy(chn->chip->modalias, chn->modalias,
sizeof(chn->chip->modalias));
ret = spi_add_device(chn->chip);
if (ret)
goto error;
dev_dbg(&dev->udev->dev, "%s probe complete", chn->modalias);
}
mutex_unlock(&dev->chn_config_lock);
schedule_work(&dev->read_chn_config);
return;