-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0011-gpio-phytium-Add-Phytium-GPIO-driver.patch
1000 lines (991 loc) · 29.2 KB
/
0011-gpio-phytium-Add-Phytium-GPIO-driver.patch
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
From ee419e591d60cffba5fd466cd2d5ab9d50d5a4e5 Mon Sep 17 00:00:00 2001
From: Chen Baozi <[email protected]>
Date: Mon, 17 Jun 2024 19:33:00 +0800
Subject: [PATCH 011/150] gpio: phytium: Add Phytium GPIO driver
Phytium GPIO controller has two ports, while port A can accept external
signals and issues interrupts to upper-level interrupt controller.
This patch introduces a driver for Phytium GPIO controller.
Signed-off-by: Chen Baozi <[email protected]>
Signed-off-by: Lan Hengyu <[email protected]>
Signed-off-by: Wang Yinfeng <[email protected]>
Signed-off-by: Zuo Qian <[email protected]>
Signed-off-by: Li Mingzhe <[email protected]>
Signed-off-by: Liu Tianyu <[email protected]>
Change-Id: I6454f314a533fd8b6b95d81a3e2300986293423d
Signed-off-by: Andrew Powers-Holmes <[email protected]>
---
MAINTAINERS | 1 +
drivers/gpio/Kconfig | 29 ++
drivers/gpio/Makefile | 3 +
drivers/gpio/gpio-phytium-core.c | 378 +++++++++++++++++++++++++++
drivers/gpio/gpio-phytium-core.h | 87 ++++++
drivers/gpio/gpio-phytium-pci.c | 191 ++++++++++++++
drivers/gpio/gpio-phytium-platform.c | 206 +++++++++++++++
7 files changed, 895 insertions(+)
create mode 100644 drivers/gpio/gpio-phytium-core.c
create mode 100644 drivers/gpio/gpio-phytium-core.h
create mode 100644 drivers/gpio/gpio-phytium-pci.c
create mode 100644 drivers/gpio/gpio-phytium-platform.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 37d5a89c026a..615941ad7495 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2532,6 +2532,7 @@ S: Maintained
W: https://www.phytium.com.cn
F: Documentation/devicetree/bindings/gpio/phytium,gpio.yaml
F: arch/arm64/boot/dts/phytium/*
+F: drivers/gpio/gpio-phytium*
ARM/QUALCOMM CHROMEBOOK SUPPORT
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index ebd4e113dc26..2cc9d121249e 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -105,6 +105,10 @@ config GPIO_REGMAP
# put drivers in the right section, in alphabetical order
+# This symbol is selected by both MMIO and PCI expanders
+config GPIO_PHYTIUM_CORE
+ tristate
+
# This symbol is selected by both I2C and SPI expanders
config GPIO_MAX730X
tristate
@@ -495,6 +499,18 @@ config GPIO_OMAP
help
Say yes here to enable GPIO support for TI OMAP SoCs.
+config GPIO_PHYTIUM_PLAT
+ tristate "Phytium GPIO Platform support"
+ default y if ARCH_PHYTIUM
+ depends on ARM64
+ select GPIO_PHYTIUM_CORE
+ select IRQ_DOMAIN
+ select GENERIC_IRQ_CHIP
+ select GPIOLIB_IRQCHIP
+ help
+ Say yes here to support the on-chip GPIO controller for the
+ Phytium SoC family.
+
config GPIO_PL061
tristate "PrimeCell PL061 GPIO support"
depends on ARM_AMBA
@@ -1680,6 +1696,19 @@ config GPIO_PCIE_IDIO_24
Input filter control is not supported by this driver, and the input
filters are deactivated by this driver.
+config GPIO_PHYTIUM_PCI
+ tristate "Phytium GPIO PCI support"
+ select GPIO_PHYTIUM_CORE
+ select IRQ_DOMAIN
+ select GENERIC_IRQ_CHIP
+ select GPIOLIB_IRQCHIP
+ help
+ Say Y here to support Phytium PCI GPIO controller on Px210 chipset.
+ An interrupt is generated when any of the inputs change state
+ (low to high or high to low).
+
+ This driver can be used for Phytium Px210.
+
config GPIO_RDC321X
tristate "RDC R-321x GPIO support"
select MFD_CORE
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index eb73b5d633eb..ebb1329a491f 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -125,6 +125,9 @@ obj-$(CONFIG_GPIO_PCF857X) += gpio-pcf857x.o
obj-$(CONFIG_GPIO_PCH) += gpio-pch.o
obj-$(CONFIG_GPIO_PCIE_IDIO_24) += gpio-pcie-idio-24.o
obj-$(CONFIG_GPIO_PCI_IDIO_16) += gpio-pci-idio-16.o
+obj-$(CONFIG_GPIO_PHYTIUM_CORE) += gpio-phytium-core.o
+obj-$(CONFIG_GPIO_PHYTIUM_PCI) += gpio-phytium-pci.o
+obj-$(CONFIG_GPIO_PHYTIUM_PLAT) += gpio-phytium-platform.o
obj-$(CONFIG_GPIO_PISOSR) += gpio-pisosr.o
obj-$(CONFIG_GPIO_PL061) += gpio-pl061.o
obj-$(CONFIG_GPIO_PMIC_EIC_SPRD) += gpio-pmic-eic-sprd.o
diff --git a/drivers/gpio/gpio-phytium-core.c b/drivers/gpio/gpio-phytium-core.c
new file mode 100644
index 000000000000..dd9ce4aeb138
--- /dev/null
+++ b/drivers/gpio/gpio-phytium-core.c
@@ -0,0 +1,378 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2019-2023, Phytium Technology Co., Ltd.
+ */
+
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/bitops.h>
+#include <linux/seq_file.h>
+
+#include "gpio-phytium-core.h"
+
+static int get_pin_location(struct phytium_gpio *gpio, unsigned int offset,
+ struct pin_loc *pl)
+{
+ int ret;
+
+ if (offset < gpio->ngpio[0]) {
+ pl->port = 0;
+ pl->offset = offset;
+ ret = 0;
+ } else if (offset < (gpio->ngpio[0] + gpio->ngpio[1])) {
+ pl->port = 1;
+ pl->offset = offset - gpio->ngpio[0];
+ ret = 0;
+ } else {
+ ret = -EINVAL;
+ }
+
+ return ret;
+}
+
+static void phytium_gpio_toggle_trigger(struct phytium_gpio *gpio,
+ unsigned int offset)
+{
+ struct gpio_chip *gc;
+ u32 pol;
+ int val;
+
+ /* Only port A can provide interrupt source */
+ if (offset >= gpio->ngpio[0])
+ return;
+
+ gc = &gpio->gc;
+
+ pol = readl(gpio->regs + GPIO_INT_POLARITY);
+ /* Just read the current value right out of the data register */
+ val = gc->get(gc, offset);
+ if (val)
+ pol &= ~BIT(offset);
+ else
+ pol |= BIT(offset);
+
+ writel(pol, gpio->regs + GPIO_INT_POLARITY);
+}
+
+int phytium_gpio_get(struct gpio_chip *gc, unsigned int offset)
+{
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct pin_loc loc;
+ void __iomem *dat;
+
+ if (get_pin_location(gpio, offset, &loc))
+ return -EINVAL;
+
+ dat = gpio->regs + GPIO_EXT_PORTA + (loc.port * GPIO_PORT_STRIDE);
+
+ return !!(readl(dat) & BIT(loc.offset));
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_get);
+
+void phytium_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
+{
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct pin_loc loc;
+ void __iomem *dr;
+ unsigned long flags;
+ u32 mask;
+
+ if (get_pin_location(gpio, offset, &loc))
+ return;
+ dr = gpio->regs + GPIO_SWPORTA_DR + (loc.port * GPIO_PORT_STRIDE);
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ if (value)
+ mask = readl(dr) | BIT(loc.offset);
+ else
+ mask = readl(dr) & ~BIT(loc.offset);
+
+ writel(mask, dr);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_set);
+
+int phytium_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
+{
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct pin_loc loc;
+ unsigned long flags;
+ void __iomem *ddr;
+
+ if (get_pin_location(gpio, offset, &loc))
+ return -EINVAL;
+ ddr = gpio->regs + GPIO_SWPORTA_DDR + (loc.port * GPIO_PORT_STRIDE);
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ writel(readl(ddr) & ~(BIT(loc.offset)), ddr);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_direction_input);
+
+int phytium_gpio_direction_output(struct gpio_chip *gc, unsigned int offset,
+ int value)
+{
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct pin_loc loc;
+ unsigned long flags;
+ void __iomem *ddr;
+
+ if (get_pin_location(gpio, offset, &loc))
+ return -EINVAL;
+ ddr = gpio->regs + GPIO_SWPORTA_DDR + (loc.port * GPIO_PORT_STRIDE);
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ writel(readl(ddr) | BIT(loc.offset), ddr);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ phytium_gpio_set(gc, offset, value);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_direction_output);
+
+void phytium_gpio_irq_ack(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ u32 val = BIT(irqd_to_hwirq(d));
+
+ raw_spin_lock(&gpio->lock);
+
+ writel(val, gpio->regs + GPIO_PORTA_EOI);
+
+ raw_spin_unlock(&gpio->lock);
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_ack);
+
+void phytium_gpio_irq_mask(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ u32 val;
+
+ /* Only port A can provide interrupt source */
+ if (irqd_to_hwirq(d) >= gpio->ngpio[0])
+ return;
+
+ raw_spin_lock(&gpio->lock);
+
+ val = readl(gpio->regs + GPIO_INTMASK);
+ val |= BIT(irqd_to_hwirq(d));
+ writel(val, gpio->regs + GPIO_INTMASK);
+
+ raw_spin_unlock(&gpio->lock);
+
+ gpiochip_disable_irq(gc, irqd_to_hwirq(d));
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_mask);
+
+void phytium_gpio_irq_unmask(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ u32 val;
+
+ /* Only port A can provide interrupt source */
+ if (irqd_to_hwirq(d) >= gpio->ngpio[0])
+ return;
+
+ gpiochip_enable_irq(gc, irqd_to_hwirq(d));
+
+ raw_spin_lock(&gpio->lock);
+
+ val = readl(gpio->regs + GPIO_INTMASK);
+ val &= ~BIT(irqd_to_hwirq(d));
+ writel(val, gpio->regs + GPIO_INTMASK);
+
+ raw_spin_unlock(&gpio->lock);
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_unmask);
+
+int phytium_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ int hwirq = irqd_to_hwirq(d);
+ unsigned long flags, lvl, pol;
+
+ if (hwirq < 0 || hwirq >= gpio->ngpio[0])
+ return -EINVAL;
+
+ if ((flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) &&
+ (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))) {
+ dev_err(gc->parent,
+ "trying to configure line %d for both level and edge detection, choose one!\n",
+ hwirq);
+ return -EINVAL;
+ }
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ lvl = readl(gpio->regs + GPIO_INTTYPE_LEVEL);
+ pol = readl(gpio->regs + GPIO_INT_POLARITY);
+
+ switch (flow_type) {
+ case IRQ_TYPE_EDGE_BOTH:
+ lvl |= BIT(hwirq);
+ phytium_gpio_toggle_trigger(gpio, hwirq);
+ irq_set_handler_locked(d, handle_edge_irq);
+ dev_dbg(gc->parent, "line %d: IRQ on both edges\n", hwirq);
+ break;
+ case IRQ_TYPE_EDGE_RISING:
+ lvl |= BIT(hwirq);
+ pol |= BIT(hwirq);
+ irq_set_handler_locked(d, handle_edge_irq);
+ dev_dbg(gc->parent, "line %d: IRQ on RISING edge\n", hwirq);
+ break;
+ case IRQ_TYPE_EDGE_FALLING:
+ lvl |= BIT(hwirq);
+ pol &= ~BIT(hwirq);
+ irq_set_handler_locked(d, handle_edge_irq);
+ dev_dbg(gc->parent, "line %d: IRQ on FALLING edge\n", hwirq);
+ break;
+ case IRQ_TYPE_LEVEL_HIGH:
+ lvl &= ~BIT(hwirq);
+ pol |= BIT(hwirq);
+ irq_set_handler_locked(d, handle_level_irq);
+ dev_dbg(gc->parent, "line %d: IRQ on HIGH level\n", hwirq);
+ break;
+ case IRQ_TYPE_LEVEL_LOW:
+ lvl &= ~BIT(hwirq);
+ pol &= ~BIT(hwirq);
+ irq_set_handler_locked(d, handle_level_irq);
+ dev_dbg(gc->parent, "line %d: IRQ on LOW level\n", hwirq);
+ break;
+ }
+
+ writel(lvl, gpio->regs + GPIO_INTTYPE_LEVEL);
+ if (flow_type != IRQ_TYPE_EDGE_BOTH)
+ writel(pol, gpio->regs + GPIO_INT_POLARITY);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_set_type);
+
+void phytium_gpio_irq_enable(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ unsigned long flags;
+ u32 val;
+
+ /* Only port A can provide interrupt source */
+ if (irqd_to_hwirq(d) >= gpio->ngpio[0])
+ return;
+
+ gpiochip_enable_irq(gc, irqd_to_hwirq(d));
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ val = readl(gpio->regs + GPIO_INTEN);
+ val |= BIT(irqd_to_hwirq(d));
+ writel(val, gpio->regs + GPIO_INTEN);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ gpiochip_disable_irq(gc, irqd_to_hwirq(d));
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_enable);
+
+void phytium_gpio_irq_disable(struct irq_data *d)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ unsigned long flags;
+ u32 val;
+
+ /* Only port A can provide interrupt source */
+ if (irqd_to_hwirq(d) >= gpio->ngpio[0])
+ return;
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ val = readl(gpio->regs + GPIO_INTEN);
+ val &= ~BIT(irqd_to_hwirq(d));
+ writel(val, gpio->regs + GPIO_INTEN);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_disable);
+
+void phytium_gpio_irq_print_chip(struct irq_data *data, struct seq_file *p)
+{
+ struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
+
+ seq_printf(p, dev_name(gc->parent));
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_print_chip);
+
+void phytium_gpio_irq_handler(struct irq_desc *desc)
+{
+ struct gpio_chip *gc = irq_desc_get_handler_data(desc);
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct irq_chip *irqchip = irq_desc_get_chip(desc);
+ unsigned long pending;
+ int offset;
+
+ chained_irq_enter(irqchip, desc);
+
+ pending = readl(gpio->regs + GPIO_INTSTATUS);
+ if (pending) {
+ for_each_set_bit(offset, &pending, gpio->ngpio[0]) {
+ int gpio_irq = irq_find_mapping(gc->irq.domain,
+ offset);
+ generic_handle_irq(gpio_irq);
+
+ if ((irq_get_trigger_type(gpio_irq) &
+ IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
+ phytium_gpio_toggle_trigger(gpio, offset);
+ }
+ }
+
+ chained_irq_exit(irqchip, desc);
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_handler);
+
+int phytium_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+ struct phytium_gpio *gpio = gpiochip_get_data(gc);
+ struct pin_loc loc;
+ void __iomem *ddr;
+
+ if (get_pin_location(gpio, offset, &loc))
+ return -EINVAL;
+ ddr = gpio->regs + GPIO_SWPORTA_DDR + (loc.port * GPIO_PORT_STRIDE);
+
+ return !(readl(ddr) & BIT(loc.offset));
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_get_direction);
+
+int phytium_gpio_irq_set_affinity(struct irq_data *d, const struct cpumask *mask_val, bool force)
+{
+ int hwirq = irqd_to_hwirq(d);
+ struct gpio_chip *chip_data = irq_data_get_irq_chip_data(d);
+ struct irq_chip *chip = irq_get_chip(chip_data->irq.parents[hwirq]);
+ struct irq_data *data = irq_get_irq_data(chip_data->irq.parents[hwirq]);
+
+ if (chip && chip->irq_set_affinity)
+ return chip->irq_set_affinity(data, mask_val, force);
+
+ return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(phytium_gpio_irq_set_affinity);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Phytium GPIO Controller core");
diff --git a/drivers/gpio/gpio-phytium-core.h b/drivers/gpio/gpio-phytium-core.h
new file mode 100644
index 000000000000..5dc4610b0611
--- /dev/null
+++ b/drivers/gpio/gpio-phytium-core.h
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2021-2023, Phytium Technology Co., Ltd.
+ */
+
+#ifndef _GPIO_PHYTIUM_H
+#define _GPIO_PHYTIUM_H
+
+#include <linux/gpio/driver.h>
+#include <linux/spinlock.h>
+
+#include "gpiolib.h"
+
+#define GPIO_SWPORTA_DR 0x00 /* WR Port A Output Data Register */
+#define GPIO_SWPORTA_DDR 0x04 /* WR Port A Data Direction Register */
+#define GPIO_EXT_PORTA 0x08 /* RO Port A Input Data Register */
+#define GPIO_SWPORTB_DR 0x0c /* WR Port B Output Data Register */
+#define GPIO_SWPORTB_DDR 0x10 /* WR Port B Data Direction Register */
+#define GPIO_EXT_PORTB 0x14 /* RO Port B Input Data Register */
+
+#define GPIO_INTEN 0x18 /* WR Port A Interrput Enable Register */
+#define GPIO_INTMASK 0x1c /* WR Port A Interrupt Mask Register */
+#define GPIO_INTTYPE_LEVEL 0x20 /* WR Port A Interrupt Level Register */
+#define GPIO_INT_POLARITY 0x24 /* WR Port A Interrupt Polarity Register */
+#define GPIO_INTSTATUS 0x28 /* RO Port A Interrupt Status Register */
+#define GPIO_RAW_INTSTATUS 0x2c /* RO Port A Raw Interrupt Status Register */
+#define GPIO_LS_SYNC 0x30 /* WR Level-sensitive Synchronization Enable Register */
+#define GPIO_DEBOUNCE 0x34 /* WR Debounce Enable Register */
+#define GPIO_PORTA_EOI 0x38 /* WO Port A Clear Interrupt Register */
+
+#define MAX_NPORTS 2
+#define NGPIO_DEFAULT 8
+#define NGPIO_MAX 32
+#define GPIO_PORT_STRIDE (GPIO_EXT_PORTB - GPIO_EXT_PORTA)
+
+struct pin_loc {
+ unsigned int port;
+ unsigned int offset;
+};
+
+#ifdef CONFIG_PM_SLEEP
+struct phytium_gpio_ctx {
+ u32 swporta_dr;
+ u32 swporta_ddr;
+ u32 ext_porta;
+ u32 swportb_dr;
+ u32 swportb_ddr;
+ u32 ext_portb;
+ u32 inten;
+ u32 intmask;
+ u32 inttype_level;
+ u32 int_polarity;
+ u32 intstatus;
+ u32 raw_intstatus;
+ u32 ls_sync;
+ u32 debounce;
+};
+#endif
+
+struct phytium_gpio {
+ raw_spinlock_t lock;
+ void __iomem *regs;
+ struct gpio_chip gc;
+ unsigned int ngpio[2];
+ int irq[32];
+#ifdef CONFIG_PM_SLEEP
+ struct phytium_gpio_ctx ctx;
+#endif
+};
+
+int phytium_gpio_get(struct gpio_chip *gc, unsigned int offset);
+void phytium_gpio_set(struct gpio_chip *gc, unsigned int offset, int value);
+
+int phytium_gpio_get_direction(struct gpio_chip *gc, unsigned int offset);
+int phytium_gpio_direction_input(struct gpio_chip *gc, unsigned int offset);
+int phytium_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value);
+
+void phytium_gpio_irq_ack(struct irq_data *d);
+void phytium_gpio_irq_mask(struct irq_data *d);
+void phytium_gpio_irq_unmask(struct irq_data *d);
+int phytium_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type);
+void phytium_gpio_irq_print_chip(struct irq_data *data, struct seq_file *p);
+void phytium_gpio_irq_enable(struct irq_data *d);
+void phytium_gpio_irq_disable(struct irq_data *d);
+void phytium_gpio_irq_handler(struct irq_desc *desc);
+int phytium_gpio_irq_set_affinity(struct irq_data *d, const struct cpumask *mask_val, bool force);
+#endif
diff --git a/drivers/gpio/gpio-phytium-pci.c b/drivers/gpio/gpio-phytium-pci.c
new file mode 100644
index 000000000000..b5e2092729c9
--- /dev/null
+++ b/drivers/gpio/gpio-phytium-pci.c
@@ -0,0 +1,191 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021-2023, Phytium Technology Co., Ltd.
+ */
+
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+
+#include "gpio-phytium-core.h"
+
+static const struct irq_chip phytium_gpio_irq_chip = {
+ .irq_ack = phytium_gpio_irq_ack,
+ .irq_mask = phytium_gpio_irq_mask,
+ .irq_unmask = phytium_gpio_irq_unmask,
+ .irq_set_type = phytium_gpio_irq_set_type,
+ .irq_print_chip = phytium_gpio_irq_print_chip,
+ .irq_enable = phytium_gpio_irq_enable,
+ .irq_disable = phytium_gpio_irq_disable,
+ .flags = IRQCHIP_IMMUTABLE,
+ GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
+static int phytium_gpio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct device *dev = &pdev->dev;
+ struct phytium_gpio *gpio;
+ struct gpio_irq_chip *girq;
+ int err;
+
+ gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
+ if (!gpio)
+ return -ENOMEM;
+
+ err = pcim_enable_device(pdev);
+ if (err) {
+ dev_err(dev, "Failed to enable PCI device: err %d\n", err);
+ goto out;
+ }
+
+ err = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
+ if (err) {
+ dev_err(dev, "Failed to iomap PCI device: err %d\n", err);
+ goto out;
+ }
+
+ gpio->regs = pcim_iomap_table(pdev)[0];
+ if (!gpio->regs) {
+ dev_err(dev, "Cannot map PCI resource\n");
+ err = -ENOMEM;
+ goto out;
+ }
+
+ err = pci_enable_msi(pdev);
+ if (err < 0)
+ goto out;
+
+ pci_set_master(pdev);
+
+ gpio->irq[0] = pdev->irq;
+ if (gpio->irq[0] < 0)
+ dev_warn(dev, "no irq is found.\n");
+
+ /* There is only one group of Pins at the moment. */
+ gpio->ngpio[0] = NGPIO_MAX;
+
+ /* irq_chip support */
+ raw_spin_lock_init(&gpio->lock);
+
+ gpio->gc.base = -1;
+ gpio->gc.get_direction = phytium_gpio_get_direction;
+ gpio->gc.direction_input = phytium_gpio_direction_input;
+ gpio->gc.direction_output = phytium_gpio_direction_output;
+ gpio->gc.get = phytium_gpio_get;
+ gpio->gc.set = phytium_gpio_set;
+ gpio->gc.ngpio = gpio->ngpio[0] + gpio->ngpio[1];
+ gpio->gc.label = dev_name(dev);
+ gpio->gc.parent = dev;
+ gpio->gc.owner = THIS_MODULE;
+
+ girq = &gpio->gc.irq;
+ girq->handler = handle_bad_irq;
+ girq->default_type = IRQ_TYPE_NONE;
+
+ girq->num_parents = 1;
+ girq->parents = devm_kcalloc(&pdev->dev, girq->num_parents,
+ sizeof(*girq->parents), GFP_KERNEL);
+ if (!girq->parents)
+ return -ENOMEM;
+ girq->parents[0] = gpio->irq[0];
+ girq->parent_handler = phytium_gpio_irq_handler;
+
+ gpio_irq_chip_set_chip(girq, &phytium_gpio_irq_chip);
+
+ err = devm_gpiochip_add_data(dev, &gpio->gc, gpio);
+ if (err)
+ goto out;
+
+ dev_info(dev, "Phytium PCI GPIO controller @%pa registered\n",
+ &gpio->regs);
+
+ pci_set_drvdata(pdev, gpio);
+
+out:
+ return err;
+}
+
+static const struct pci_device_id phytium_gpio_pci_ids[] = {
+ { PCI_DEVICE(0x1DB7, 0xDC31) },
+ { 0 }
+};
+MODULE_DEVICE_TABLE(pci, phytium_gpio_pci_ids);
+
+#ifdef CONFIG_PM_SLEEP
+static int phytium_gpio_pci_suspend(struct device *dev)
+{
+ struct pci_dev *pdev = to_pci_dev(dev);
+ struct phytium_gpio *gpio = pci_get_drvdata(pdev);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ gpio->ctx.swporta_dr = readl(gpio->regs + GPIO_SWPORTA_DR);
+ gpio->ctx.swporta_ddr = readl(gpio->regs + GPIO_SWPORTA_DDR);
+ gpio->ctx.ext_porta = readl(gpio->regs + GPIO_EXT_PORTA);
+ gpio->ctx.swportb_dr = readl(gpio->regs + GPIO_SWPORTB_DR);
+ gpio->ctx.swportb_ddr = readl(gpio->regs + GPIO_SWPORTB_DDR);
+ gpio->ctx.ext_portb = readl(gpio->regs + GPIO_EXT_PORTB);
+
+ gpio->ctx.inten = readl(gpio->regs + GPIO_INTEN);
+ gpio->ctx.intmask = readl(gpio->regs + GPIO_INTMASK);
+ gpio->ctx.inttype_level = readl(gpio->regs + GPIO_INTTYPE_LEVEL);
+ gpio->ctx.int_polarity = readl(gpio->regs + GPIO_INT_POLARITY);
+ gpio->ctx.debounce = readl(gpio->regs + GPIO_DEBOUNCE);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+
+static int phytium_gpio_pci_resume(struct device *dev)
+{
+ struct pci_dev *pdev = to_pci_dev(dev);
+ struct phytium_gpio *gpio = pci_get_drvdata(pdev);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ writel(gpio->ctx.swporta_dr, gpio->regs + GPIO_SWPORTA_DR);
+ writel(gpio->ctx.swporta_ddr, gpio->regs + GPIO_SWPORTA_DDR);
+ writel(gpio->ctx.ext_porta, gpio->regs + GPIO_EXT_PORTA);
+ writel(gpio->ctx.swportb_dr, gpio->regs + GPIO_SWPORTB_DR);
+ writel(gpio->ctx.swportb_ddr, gpio->regs + GPIO_SWPORTB_DDR);
+ writel(gpio->ctx.ext_portb, gpio->regs + GPIO_EXT_PORTB);
+
+ writel(gpio->ctx.inten, gpio->regs + GPIO_INTEN);
+ writel(gpio->ctx.intmask, gpio->regs + GPIO_INTMASK);
+ writel(gpio->ctx.inttype_level, gpio->regs + GPIO_INTTYPE_LEVEL);
+ writel(gpio->ctx.int_polarity, gpio->regs + GPIO_INT_POLARITY);
+ writel(gpio->ctx.debounce, gpio->regs + GPIO_DEBOUNCE);
+
+ writel(0xffffffff, gpio->regs + GPIO_PORTA_EOI);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(phytium_gpio_pci_pm_ops,
+ phytium_gpio_pci_suspend,
+ phytium_gpio_pci_resume);
+
+static struct pci_driver phytium_gpio_pci_driver = {
+ .name = "gpio-phytium-pci",
+ .id_table = phytium_gpio_pci_ids,
+ .probe = phytium_gpio_pci_probe,
+ .driver = {
+ .pm = &phytium_gpio_pci_pm_ops,
+ },
+};
+
+module_pci_driver(phytium_gpio_pci_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Cheng Quan <[email protected]>");
+MODULE_DESCRIPTION("Phytium GPIO PCI Driver");
diff --git a/drivers/gpio/gpio-phytium-platform.c b/drivers/gpio/gpio-phytium-platform.c
new file mode 100644
index 000000000000..bae1fd873d08
--- /dev/null
+++ b/drivers/gpio/gpio-phytium-platform.c
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Support functions for Phytium GPIO
+ *
+ * Copyright (c) 2019-2023, Phytium Technology Co., Ltd.
+ *
+ * Derived from drivers/gpio/gpio-pl061.c
+ * Copyright (C) 2008, 2009 Provigent Ltd.
+ */
+
+#include <linux/acpi.h>
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+
+#include "gpio-phytium-core.h"
+
+static const struct of_device_id phytium_gpio_of_match[] = {
+ { .compatible = "phytium,gpio", },
+ { }
+};
+MODULE_DEVICE_TABLE(of, phytium_gpio_of_match);
+
+static const struct acpi_device_id phytium_gpio_acpi_match[] = {
+ { "PHYT0001", 0 },
+ { }
+};
+MODULE_DEVICE_TABLE(acpi, phytium_gpio_acpi_match);
+
+static const struct irq_chip phytium_gpio_irq_chip = {
+ .irq_ack = phytium_gpio_irq_ack,
+ .irq_mask = phytium_gpio_irq_mask,
+ .irq_unmask = phytium_gpio_irq_unmask,
+ .irq_set_type = phytium_gpio_irq_set_type,
+ .irq_print_chip = phytium_gpio_irq_print_chip,
+ .irq_enable = phytium_gpio_irq_enable,
+ .irq_disable = phytium_gpio_irq_disable,
+ .irq_set_affinity = phytium_gpio_irq_set_affinity,
+ .flags = IRQCHIP_IMMUTABLE,
+ GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
+static int phytium_gpio_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct resource *res;
+ struct phytium_gpio *gpio;
+ struct gpio_irq_chip *girq;
+ struct fwnode_handle *fwnode;
+ int err, irq_count;
+
+ gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
+ if (!gpio)
+ return -ENOMEM;
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ gpio->regs = devm_ioremap_resource(&pdev->dev, res);
+ if (IS_ERR(gpio->regs))
+ return PTR_ERR(gpio->regs);
+
+ if (!device_get_child_node_count(dev))
+ return -ENODEV;
+
+ device_for_each_child_node(dev, fwnode) {
+ int idx;
+
+ if (fwnode_property_read_u32(fwnode, "reg", &idx) ||
+ idx >= MAX_NPORTS) {
+ dev_err(dev, "missing/invalid port index\n");
+ fwnode_handle_put(fwnode);
+ return -EINVAL;
+ }
+
+ if (fwnode_property_read_u32(fwnode, "ngpios", &gpio->ngpio[idx]) &&
+ fwnode_property_read_u32(fwnode, "nr-gpios", &gpio->ngpio[idx])) {
+ dev_info(dev,
+ "failed to get number of gpios for Port%c\n",
+ idx ? 'B' : 'A');
+ gpio->ngpio[idx] = NGPIO_DEFAULT;
+ }
+ }
+
+ /* irq_chip support */
+ raw_spin_lock_init(&gpio->lock);
+
+ gpio->gc.base = -1;
+ gpio->gc.get_direction = phytium_gpio_get_direction;
+ gpio->gc.direction_input = phytium_gpio_direction_input;
+ gpio->gc.direction_output = phytium_gpio_direction_output;
+ gpio->gc.get = phytium_gpio_get;
+ gpio->gc.set = phytium_gpio_set;
+ gpio->gc.ngpio = gpio->ngpio[0] + gpio->ngpio[1];
+ gpio->gc.label = dev_name(dev);
+ gpio->gc.parent = dev;
+ gpio->gc.owner = THIS_MODULE;
+
+ girq = &gpio->gc.irq;
+ girq->handler = handle_bad_irq;
+ girq->default_type = IRQ_TYPE_NONE;
+
+ for (irq_count = 0; irq_count < platform_irq_count(pdev); irq_count++) {
+ gpio->irq[irq_count] = -ENXIO;
+ gpio->irq[irq_count] = platform_get_irq(pdev, irq_count);
+ if (gpio->irq[irq_count] < 0) {
+ dev_warn(dev, "no irq is found.\n");
+ break;
+ }
+ };
+
+ girq->num_parents = irq_count;
+ girq->parents = gpio->irq;
+ girq->parent_handler = phytium_gpio_irq_handler;
+
+ gpio_irq_chip_set_chip(girq, &phytium_gpio_irq_chip);
+
+ err = devm_gpiochip_add_data(dev, &gpio->gc, gpio);
+ if (err)
+ return err;
+
+ platform_set_drvdata(pdev, gpio);
+ dev_info(dev, "Phytium GPIO controller @%pa registered\n",
+ &res->start);
+
+ return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int phytium_gpio_suspend(struct device *dev)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct phytium_gpio *gpio = platform_get_drvdata(pdev);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ gpio->ctx.swporta_dr = readl(gpio->regs + GPIO_SWPORTA_DR);
+ gpio->ctx.swporta_ddr = readl(gpio->regs + GPIO_SWPORTA_DDR);
+ gpio->ctx.ext_porta = readl(gpio->regs + GPIO_EXT_PORTA);
+ gpio->ctx.swportb_dr = readl(gpio->regs + GPIO_SWPORTB_DR);
+ gpio->ctx.swportb_ddr = readl(gpio->regs + GPIO_SWPORTB_DDR);
+ gpio->ctx.ext_portb = readl(gpio->regs + GPIO_EXT_PORTB);
+
+ gpio->ctx.inten = readl(gpio->regs + GPIO_INTEN);
+ gpio->ctx.intmask = readl(gpio->regs + GPIO_INTMASK);
+ gpio->ctx.inttype_level = readl(gpio->regs + GPIO_INTTYPE_LEVEL);
+ gpio->ctx.int_polarity = readl(gpio->regs + GPIO_INT_POLARITY);
+ gpio->ctx.debounce = readl(gpio->regs + GPIO_DEBOUNCE);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+
+static int phytium_gpio_resume(struct device *dev)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct phytium_gpio *gpio = platform_get_drvdata(pdev);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&gpio->lock, flags);
+
+ writel(gpio->ctx.swporta_dr, gpio->regs + GPIO_SWPORTA_DR);
+ writel(gpio->ctx.swporta_ddr, gpio->regs + GPIO_SWPORTA_DDR);
+ writel(gpio->ctx.ext_porta, gpio->regs + GPIO_EXT_PORTA);
+ writel(gpio->ctx.swportb_dr, gpio->regs + GPIO_SWPORTB_DR);
+ writel(gpio->ctx.swportb_ddr, gpio->regs + GPIO_SWPORTB_DDR);
+ writel(gpio->ctx.ext_portb, gpio->regs + GPIO_EXT_PORTB);
+
+ writel(gpio->ctx.inten, gpio->regs + GPIO_INTEN);
+ writel(gpio->ctx.intmask, gpio->regs + GPIO_INTMASK);
+ writel(gpio->ctx.inttype_level, gpio->regs + GPIO_INTTYPE_LEVEL);
+ writel(gpio->ctx.int_polarity, gpio->regs + GPIO_INT_POLARITY);
+ writel(gpio->ctx.debounce, gpio->regs + GPIO_DEBOUNCE);
+
+ writel(0xffffffff, gpio->regs + GPIO_PORTA_EOI);
+
+ raw_spin_unlock_irqrestore(&gpio->lock, flags);
+
+ return 0;
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(phytium_gpio_pm_ops, phytium_gpio_suspend,
+ phytium_gpio_resume);
+
+static struct platform_driver phytium_gpio_driver = {
+ .driver = {
+ .name = "gpio-phytium-platform",
+ .pm = &phytium_gpio_pm_ops,
+ .of_match_table = of_match_ptr(phytium_gpio_of_match),
+ .acpi_match_table = ACPI_PTR(phytium_gpio_acpi_match),
+ },
+ .probe = phytium_gpio_probe,
+};
+
+module_platform_driver(phytium_gpio_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Chen Baozi <[email protected]>");
+MODULE_DESCRIPTION("Phytium GPIO driver");
--
2.47.0