summaryrefslogtreecommitdiffstats
path: root/drivers/pinctrl/pinctrl-k210.c
blob: 97920fb517bc64823c76afbf18ce4a692466a6b0 (plain)
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
1001
1002
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
 */
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/mfd/syscon.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

#include <linux/pinctrl/pinconf-generic.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>

#include <dt-bindings/pinctrl/k210-fpioa.h>

#include "core.h"
#include "pinconf.h"
#include "pinctrl-utils.h"

/*
 * The K210 only implements 8 drive levels, even though
 * there is register space for 16
 */
#define K210_PC_DRIVE_MASK	GENMASK(11, 8)
#define K210_PC_DRIVE_SHIFT	8
#define K210_PC_DRIVE_0		(0 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_1		(1 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_2		(2 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_3		(3 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_4		(4 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_5		(5 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_6		(6 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_7		(7 << K210_PC_DRIVE_SHIFT)
#define K210_PC_DRIVE_MAX	7
#define K210_PC_MODE_MASK	GENMASK(23, 12)

/*
 * output enabled == PC_OE & (PC_OE_INV ^ FUNCTION_OE)
 * where FUNCTION_OE is a physical signal from the function.
 */
#define K210_PC_OE		BIT(12) /* Output Enable */
#define K210_PC_OE_INV		BIT(13) /* INVert Output Enable */
#define K210_PC_DO_OE		BIT(14) /* set Data Out to Output Enable sig */
#define K210_PC_DO_INV		BIT(15) /* INVert final Data Output */
#define K210_PC_PU		BIT(16) /* Pull Up */
#define K210_PC_PD		BIT(17) /* Pull Down */
/* Strong pull up not implemented on K210 */
#define K210_PC_SL		BIT(19) /* reduce SLew rate */
/* Same semantics as OE above */
#define K210_PC_IE		BIT(20) /* Input Enable */
#define K210_PC_IE_INV		BIT(21) /* INVert Input Enable */
#define K210_PC_DI_INV		BIT(22) /* INVert Data Input */
#define K210_PC_ST		BIT(23) /* Schmitt Trigger */
#define K210_PC_DI		BIT(31) /* raw Data Input */

#define K210_PC_BIAS_MASK	(K210_PC_PU & K210_PC_PD)

#define K210_PC_MODE_IN		(K210_PC_IE | K210_PC_ST)
#define K210_PC_MODE_OUT	(K210_PC_DRIVE_7 | K210_PC_OE)
#define K210_PC_MODE_I2C	(K210_PC_MODE_IN | K210_PC_SL | \
				 K210_PC_OE | K210_PC_PU)
#define K210_PC_MODE_SCCB	(K210_PC_MODE_I2C | \
				 K210_PC_OE_INV | K210_PC_IE_INV)
#define K210_PC_MODE_SPI	(K210_PC_MODE_IN | K210_PC_IE_INV | \
				 K210_PC_MODE_OUT | K210_PC_OE_INV)
#define K210_PC_MODE_GPIO	(K210_PC_MODE_IN | K210_PC_MODE_OUT)

#define K210_PG_FUNC		GENMASK(7, 0)
#define K210_PG_DO		BIT(8)
#define K210_PG_PIN		GENMASK(22, 16)

/*
 * struct k210_fpioa: Kendryte K210 FPIOA memory mapped registers
 * @pins: 48 32-bits IO pin registers
 * @tie_en: 256 (one per function) input tie enable bits
 * @tie_val: 256 (one per function) input tie value bits
 */
struct k210_fpioa {
	u32 pins[48];
	u32 tie_en[8];
	u32 tie_val[8];
};

struct k210_fpioa_data {

	struct device *dev;
	struct pinctrl_dev *pctl;

	struct k210_fpioa __iomem *fpioa;
	struct regmap *sysctl_map;
	u32 power_offset;
	struct clk *clk;
	struct clk *pclk;
};

#define K210_PIN_NAME(i)	("IO_" #i)
#define K210_PIN(i)		[(i)] = PINCTRL_PIN((i), K210_PIN_NAME(i))

static const struct pinctrl_pin_desc k210_pins[] = {
	K210_PIN(0),  K210_PIN(1),  K210_PIN(2),
	K210_PIN(3),  K210_PIN(4),  K210_PIN(5),
	K210_PIN(6),  K210_PIN(7),  K210_PIN(8),
	K210_PIN(9),  K210_PIN(10), K210_PIN(11),
	K210_PIN(12), K210_PIN(13), K210_PIN(14),
	K210_PIN(15), K210_PIN(16), K210_PIN(17),
	K210_PIN(18), K210_PIN(19), K210_PIN(20),
	K210_PIN(21), K210_PIN(22), K210_PIN(23),
	K210_PIN(24), K210_PIN(25), K210_PIN(26),
	K210_PIN(27), K210_PIN(28), K210_PIN(29),
	K210_PIN(30), K210_PIN(31), K210_PIN(32),
	K210_PIN(33), K210_PIN(34), K210_PIN(35),
	K210_PIN(36), K210_PIN(37), K210_PIN(38),
	K210_PIN(39), K210_PIN(40), K210_PIN(41),
	K210_PIN(42), K210_PIN(43), K210_PIN(44),
	K210_PIN(45), K210_PIN(46), K210_PIN(47)
};

#define K210_NPINS ARRAY_SIZE(k210_pins)

/*
 * Pin groups: each of the 48 programmable pins is a group.
 * To this are added 8 power domain groups, which for the purposes of
 * the pin subsystem, contain no pins. The power domain groups only exist
 * to set the power level. The id should never be used (since there are
 * no pins 48-55).
 */
static const char *const k210_group_names[] = {
	/* The first 48 groups are for pins, one each */
	K210_PIN_NAME(0),  K210_PIN_NAME(1),  K210_PIN_NAME(2),
	K210_PIN_NAME(3),  K210_PIN_NAME(4),  K210_PIN_NAME(5),
	K210_PIN_NAME(6),  K210_PIN_NAME(7),  K210_PIN_NAME(8),
	K210_PIN_NAME(9),  K210_PIN_NAME(10), K210_PIN_NAME(11),
	K210_PIN_NAME(12), K210_PIN_NAME(13), K210_PIN_NAME(14),
	K210_PIN_NAME(15), K210_PIN_NAME(16), K210_PIN_NAME(17),
	K210_PIN_NAME(18), K210_PIN_NAME(19), K210_PIN_NAME(20),
	K210_PIN_NAME(21), K210_PIN_NAME(22), K210_PIN_NAME(23),
	K210_PIN_NAME(24), K210_PIN_NAME(25), K210_PIN_NAME(26),
	K210_PIN_NAME(27), K210_PIN_NAME(28), K210_PIN_NAME(29),
	K210_PIN_NAME(30), K210_PIN_NAME(31), K210_PIN_NAME(32),
	K210_PIN_NAME(33), K210_PIN_NAME(34), K210_PIN_NAME(35),
	K210_PIN_NAME(36), K210_PIN_NAME(37), K210_PIN_NAME(38),
	K210_PIN_NAME(39), K210_PIN_NAME(40), K210_PIN_NAME(41),
	K210_PIN_NAME(42), K210_PIN_NAME(43), K210_PIN_NAME(44),
	K210_PIN_NAME(45), K210_PIN_NAME(46), K210_PIN_NAME(47),
	[48] = "A0", [49] = "A1", [50] = "A2",
	[51] = "B3", [52] = "B4", [53] = "B5",
	[54] = "C6", [55] = "C7"
};

#define K210_NGROUPS	ARRAY_SIZE(k210_group_names)

enum k210_pinctrl_mode_id {
	K210_PC_DEFAULT_DISABLED,
	K210_PC_DEFAULT_IN,
	K210_PC_DEFAULT_IN_TIE,
	K210_PC_DEFAULT_OUT,
	K210_PC_DEFAULT_I2C,
	K210_PC_DEFAULT_SCCB,
	K210_PC_DEFAULT_SPI,
	K210_PC_DEFAULT_GPIO,
	K210_PC_DEFAULT_INT13,
};

#define K210_PC_DEFAULT(mode) \
	[K210_PC_DEFAULT_##mode] = K210_PC_MODE_##mode

static const u32 k210_pinconf_mode_id_to_mode[] = {
	[K210_PC_DEFAULT_DISABLED] = 0,
	K210_PC_DEFAULT(IN),
	[K210_PC_DEFAULT_IN_TIE] = K210_PC_MODE_IN,
	K210_PC_DEFAULT(OUT),
	K210_PC_DEFAULT(I2C),
	K210_PC_DEFAULT(SCCB),
	K210_PC_DEFAULT(SPI),
	K210_PC_DEFAULT(GPIO),
	[K210_PC_DEFAULT_INT13] = K210_PC_MODE_IN | K210_PC_PU,
};

#undef DEFAULT

/*
 * Pin functions configuration information.
 */
struct k210_pcf_info {
	char name[15];
	u8 mode_id;
};

#define K210_FUNC(id, mode)				\
	[K210_PCF_##id] = {				\
		.name = #id,				\
		.mode_id = K210_PC_DEFAULT_##mode	\
	}

static const struct k210_pcf_info k210_pcf_infos[] = {
	K210_FUNC(JTAG_TCLK,		IN),
	K210_FUNC(JTAG_TDI,		IN),
	K210_FUNC(JTAG_TMS,		IN),
	K210_FUNC(JTAG_TDO,		OUT),
	K210_FUNC(SPI0_D0,		SPI),
	K210_FUNC(SPI0_D1,		SPI),
	K210_FUNC(SPI0_D2,		SPI),
	K210_FUNC(SPI0_D3,		SPI),
	K210_FUNC(SPI0_D4,		SPI),
	K210_FUNC(SPI0_D5,		SPI),
	K210_FUNC(SPI0_D6,		SPI),
	K210_FUNC(SPI0_D7,		SPI),
	K210_FUNC(SPI0_SS0,		OUT),
	K210_FUNC(SPI0_SS1,		OUT),
	K210_FUNC(SPI0_SS2,		OUT),
	K210_FUNC(SPI0_SS3,		OUT),
	K210_FUNC(SPI0_ARB,		IN_TIE),
	K210_FUNC(SPI0_SCLK,		OUT),
	K210_FUNC(UARTHS_RX,		IN),
	K210_FUNC(UARTHS_TX,		OUT),
	K210_FUNC(RESV6,		IN),
	K210_FUNC(RESV7,		IN),
	K210_FUNC(CLK_SPI1,		OUT),
	K210_FUNC(CLK_I2C1,		OUT),
	K210_FUNC(GPIOHS0,		GPIO),
	K210_FUNC(GPIOHS1,		GPIO),
	K210_FUNC(GPIOHS2,		GPIO),
	K210_FUNC(GPIOHS3,		GPIO),
	K210_FUNC(GPIOHS4,		GPIO),
	K210_FUNC(GPIOHS5,		GPIO),
	K210_FUNC(GPIOHS6,		GPIO),
	K210_FUNC(GPIOHS7,		GPIO),
	K210_FUNC(GPIOHS8,		GPIO),
	K210_FUNC(GPIOHS9,		GPIO),
	K210_FUNC(GPIOHS10,		GPIO),
	K210_FUNC(GPIOHS11,		GPIO),
	K210_FUNC(GPIOHS12,		GPIO),
	K210_FUNC(GPIOHS13,		GPIO),
	K210_FUNC(GPIOHS14,		GPIO),
	K210_FUNC(GPIOHS15,		GPIO),
	K210_FUNC(GPIOHS16,		GPIO),
	K210_FUNC(GPIOHS17,		GPIO),
	K210_FUNC(GPIOHS18,		GPIO),
	K210_FUNC(GPIOHS19,		GPIO),
	K210_FUNC(GPIOHS20,		GPIO),
	K210_FUNC(GPIOHS21,		GPIO),
	K210_FUNC(GPIOHS22,		GPIO),
	K210_FUNC(GPIOHS23,		GPIO),
	K210_FUNC(GPIOHS24,		GPIO),
	K210_FUNC(GPIOHS25,		GPIO),
	K210_FUNC(GPIOHS26,		GPIO),
	K210_FUNC(GPIOHS27,		GPIO),
	K210_FUNC(GPIOHS28,		GPIO),
	K210_FUNC(GPIOHS29,		GPIO),
	K210_FUNC(GPIOHS30,		GPIO),
	K210_FUNC(GPIOHS31,		GPIO),
	K210_FUNC(GPIO0,		GPIO),
	K210_FUNC(GPIO1,		GPIO),
	K210_FUNC(GPIO2,		GPIO),
	K210_FUNC(GPIO3,		GPIO),
	K210_FUNC(GPIO4,		GPIO),
	K210_FUNC(GPIO5,		GPIO),
	K210_FUNC(GPIO6,		GPIO),
	K210_FUNC(GPIO7,		GPIO),
	K210_FUNC(UART1_RX,		IN),
	K210_FUNC(UART1_TX,		OUT),
	K210_FUNC(UART2_RX,		IN),
	K210_FUNC(UART2_TX,		OUT),
	K210_FUNC(UART3_RX,		IN),
	K210_FUNC(UART3_TX,		OUT),
	K210_FUNC(SPI1_D0,		SPI),
	K210_FUNC(SPI1_D1,		SPI),
	K210_FUNC(SPI1_D2,		SPI),
	K210_FUNC(SPI1_D3,		SPI),
	K210_FUNC(SPI1_D4,		SPI),
	K210_FUNC(SPI1_D5,		SPI),
	K210_FUNC(SPI1_D6,		SPI),
	K210_FUNC(SPI1_D7,		SPI),
	K210_FUNC(SPI1_SS0,		OUT),
	K210_FUNC(SPI1_SS1,		OUT),
	K210_FUNC(SPI1_SS2,		OUT),
	K210_FUNC(SPI1_SS3,		OUT),
	K210_FUNC(SPI1_ARB,		IN_TIE),
	K210_FUNC(SPI1_SCLK,		OUT),
	K210_FUNC(SPI2_D0,		SPI),
	K210_FUNC(SPI2_SS,		IN),
	K210_FUNC(SPI2_SCLK,		IN),
	K210_FUNC(I2S0_MCLK,		OUT),
	K210_FUNC(I2S0_SCLK,		OUT),
	K210_FUNC(I2S0_WS,		OUT),
	K210_FUNC(I2S0_IN_D0,		IN),
	K210_FUNC(I2S0_IN_D1,		IN),
	K210_FUNC(I2S0_IN_D2,		IN),
	K210_FUNC(I2S0_IN_D3,		IN),
	K210_FUNC(I2S0_OUT_D0,		OUT),
	K210_FUNC(I2S0_OUT_D1,		OUT),
	K210_FUNC(I2S0_OUT_D2,		OUT),
	K210_FUNC(I2S0_OUT_D3,		OUT),
	K210_FUNC(I2S1_MCLK,		OUT),
	K210_FUNC(I2S1_SCLK,		OUT),
	K210_FUNC(I2S1_WS,		OUT),
	K210_FUNC(I2S1_IN_D0,		IN),
	K210_FUNC(I2S1_IN_D1,		IN),
	K210_FUNC(I2S1_IN_D2,		IN),
	K210_FUNC(I2S1_IN_D3,		IN),
	K210_FUNC(I2S1_OUT_D0,		OUT),
	K210_FUNC(I2S1_OUT_D1,		OUT),
	K210_FUNC(I2S1_OUT_D2,		OUT),
	K210_FUNC(I2S1_OUT_D3,		OUT),
	K210_FUNC(I2S2_MCLK,		OUT),
	K210_FUNC(I2S2_SCLK,		OUT),
	K210_FUNC(I2S2_WS,		OUT),
	K210_FUNC(I2S2_IN_D0,		IN),
	K210_FUNC(I2S2_IN_D1,		IN),
	K210_FUNC(I2S2_IN_D2,		IN),
	K210_FUNC(I2S2_IN_D3,		IN),
	K210_FUNC(I2S2_OUT_D0,		OUT),
	K210_FUNC(I2S2_OUT_D1,		OUT),
	K210_FUNC(I2S2_OUT_D2,		OUT),
	K210_FUNC(I2S2_OUT_D3,		OUT),
	K210_FUNC(RESV0,		DISABLED),
	K210_FUNC(RESV1,		DISABLED),
	K210_FUNC(RESV2,		DISABLED),
	K210_FUNC(RESV3,		DISABLED),
	K210_FUNC(RESV4,		DISABLED),
	K210_FUNC(RESV5,		DISABLED),
	K210_FUNC(I2C0_SCLK,		I2C),
	K210_FUNC(I2C0_SDA,		I2C),
	K210_FUNC(I2C1_SCLK,		I2C),
	K210_FUNC(I2C1_SDA,		I2C),
	K210_FUNC(I2C2_SCLK,		I2C),
	K210_FUNC(I2C2_SDA,		I2C),
	K210_FUNC(DVP_XCLK,		OUT),
	K210_FUNC(DVP_RST,		OUT),
	K210_FUNC(DVP_PWDN,		OUT),
	K210_FUNC(DVP_VSYNC,		IN),
	K210_FUNC(DVP_HSYNC,		IN),
	K210_FUNC(DVP_PCLK,		IN),
	K210_FUNC(DVP_D0,		IN),
	K210_FUNC(DVP_D1,		IN),
	K210_FUNC(DVP_D2,		IN),
	K210_FUNC(DVP_D3,		IN),
	K210_FUNC(DVP_D4,		IN),
	K210_FUNC(DVP_D5,		IN),
	K210_FUNC(DVP_D6,		IN),
	K210_FUNC(DVP_D7,		IN),
	K210_FUNC(SCCB_SCLK,		SCCB),
	K210_FUNC(SCCB_SDA,		SCCB),
	K210_FUNC(UART1_CTS,		IN),
	K210_FUNC(UART1_DSR,		IN),
	K210_FUNC(UART1_DCD,		IN),
	K210_FUNC(UART1_RI,		IN),
	K210_FUNC(UART1_SIR_IN,		IN),
	K210_FUNC(UART1_DTR,		OUT),
	K210_FUNC(UART1_RTS,		OUT),
	K210_FUNC(UART1_OUT2,		OUT),
	K210_FUNC(UART1_OUT1,		OUT),
	K210_FUNC(UART1_SIR_OUT,	OUT),
	K210_FUNC(UART1_BAUD,		OUT),
	K210_FUNC(UART1_RE,		OUT),
	K210_FUNC(UART1_DE,		OUT),
	K210_FUNC(UART1_RS485_EN,	OUT),
	K210_FUNC(UART2_CTS,		IN),
	K210_FUNC(UART2_DSR,		IN),
	K210_FUNC(UART2_DCD,		IN),
	K210_FUNC(UART2_RI,		IN),
	K210_FUNC(UART2_SIR_IN,		IN),
	K210_FUNC(UART2_DTR,		OUT),
	K210_FUNC(UART2_RTS,		OUT),
	K210_FUNC(UART2_OUT2,		OUT),
	K210_FUNC(UART2_OUT1,		OUT),
	K210_FUNC(UART2_SIR_OUT,	OUT),
	K210_FUNC(UART2_BAUD,		OUT),
	K210_FUNC(UART2_RE,		OUT),
	K210_FUNC(UART2_DE,		OUT),
	K210_FUNC(UART2_RS485_EN,	OUT),
	K210_FUNC(UART3_CTS,		IN),
	K210_FUNC(UART3_DSR,		IN),
	K210_FUNC(UART3_DCD,		IN),
	K210_FUNC(UART3_RI,		IN),
	K210_FUNC(UART3_SIR_IN,		IN),
	K210_FUNC(UART3_DTR,		OUT),
	K210_FUNC(UART3_RTS,		OUT),
	K210_FUNC(UART3_OUT2,		OUT),
	K210_FUNC(UART3_OUT1,		OUT),
	K210_FUNC(UART3_SIR_OUT,	OUT),
	K210_FUNC(UART3_BAUD,		OUT),
	K210_FUNC(UART3_RE,		OUT),
	K210_FUNC(UART3_DE,		OUT),
	K210_FUNC(UART3_RS485_EN,	OUT),
	K210_FUNC(TIMER0_TOGGLE1,	OUT),
	K210_FUNC(TIMER0_TOGGLE2,	OUT),
	K210_FUNC(TIMER0_TOGGLE3,	OUT),
	K210_FUNC(TIMER0_TOGGLE4,	OUT),
	K210_FUNC(TIMER1_TOGGLE1,	OUT),
	K210_FUNC(TIMER1_TOGGLE2,	OUT),
	K210_FUNC(TIMER1_TOGGLE3,	OUT),
	K210_FUNC(TIMER1_TOGGLE4,	OUT),
	K210_FUNC(TIMER2_TOGGLE1,	OUT),
	K210_FUNC(TIMER2_TOGGLE2,	OUT),
	K210_FUNC(TIMER2_TOGGLE3,	OUT),
	K210_FUNC(TIMER2_TOGGLE4,	OUT),
	K210_FUNC(CLK_SPI2,		OUT),
	K210_FUNC(CLK_I2C2,		OUT),
	K210_FUNC(INTERNAL0,		OUT),
	K210_FUNC(INTERNAL1,		OUT),
	K210_FUNC(INTERNAL2,		OUT),
	K210_FUNC(INTERNAL3,		OUT),
	K210_FUNC(INTERNAL4,		OUT),
	K210_FUNC(INTERNAL5,		OUT),
	K210_FUNC(INTERNAL6,		OUT),
	K210_FUNC(INTERNAL7,		OUT),
	K210_FUNC(INTERNAL8,		OUT),
	K210_FUNC(INTERNAL9,		IN),
	K210_FUNC(INTERNAL10,		IN),
	K210_FUNC(INTERNAL11,		IN),
	K210_FUNC(INTERNAL12,		IN),
	K210_FUNC(INTERNAL13,		INT13),
	K210_FUNC(INTERNAL14,		I2C),
	K210_FUNC(INTERNAL15,		IN),
	K210_FUNC(INTERNAL16,		IN),
	K210_FUNC(INTERNAL17,		IN),
	K210_FUNC(CONSTANT,		DISABLED),
	K210_FUNC(INTERNAL18,		IN),
	K210_FUNC(DEBUG0,		OUT),
	K210_FUNC(DEBUG1,		OUT),
	K210_FUNC(DEBUG2,		OUT),
	K210_FUNC(DEBUG3,		OUT),
	K210_FUNC(DEBUG4,		OUT),
	K210_FUNC(DEBUG5,		OUT),
	K210_FUNC(DEBUG6,		OUT),
	K210_FUNC(DEBUG7,		OUT),
	K210_FUNC(DEBUG8,		OUT),
	K210_FUNC(DEBUG9,		OUT),
	K210_FUNC(DEBUG10,		OUT),
	K210_FUNC(DEBUG11,		OUT),
	K210_FUNC(DEBUG12,		OUT),
	K210_FUNC(DEBUG13,		OUT),
	K210_FUNC(DEBUG14,		OUT),
	K210_FUNC(DEBUG15,		OUT),
	K210_FUNC(DEBUG16,		OUT),
	K210_FUNC(DEBUG17,		OUT),
	K210_FUNC(DEBUG18,		OUT),
	K210_FUNC(DEBUG19,		OUT),
	K210_FUNC(DEBUG20,		OUT),
	K210_FUNC(DEBUG21,		OUT),
	K210_FUNC(DEBUG22,		OUT),
	K210_FUNC(DEBUG23,		OUT),
	K210_FUNC(DEBUG24,		OUT),
	K210_FUNC(DEBUG25,		OUT),
	K210_FUNC(DEBUG26,		OUT),
	K210_FUNC(DEBUG27,		OUT),
	K210_FUNC(DEBUG28,		OUT),
	K210_FUNC(DEBUG29,		OUT),
	K210_FUNC(DEBUG30,		OUT),
	K210_FUNC(DEBUG31,		OUT),
};

#define PIN_CONFIG_OUTPUT_INVERT	(PIN_CONFIG_END + 1)
#define PIN_CONFIG_INPUT_INVERT		(PIN_CONFIG_END + 2)

static const struct pinconf_generic_params k210_pinconf_custom_params[] = {
	{ "output-polarity-invert", PIN_CONFIG_OUTPUT_INVERT, 1 },
	{ "input-polarity-invert",  PIN_CONFIG_INPUT_INVERT, 1 },
};

/*
 * Max drive strength in uA.
 */
static const int k210_pinconf_drive_strength[] = {
	[0] = 11200,
	[1] = 16800,
	[2] = 22300,
	[3] = 27800,
	[4] = 33300,
	[5] = 38700,
	[6] = 44100,
	[7] = 49500,
};

static int k210_pinconf_get_drive(unsigned int max_strength_ua)
{
	int i;

	for (i = K210_PC_DRIVE_MAX; i >= 0; i--) {
		if (k210_pinconf_drive_strength[i] <= max_strength_ua)
			return i;
	}

	return -EINVAL;
}

static void k210_pinmux_set_pin_function(struct pinctrl_dev *pctldev,
					 u32 pin, u32 func)
{
	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
	const struct k210_pcf_info *info = &k210_pcf_infos[func];
	u32 mode = k210_pinconf_mode_id_to_mode[info->mode_id];
	u32 val = func | mode;

	dev_dbg(pdata->dev, "set pin %u function %s (%u) -> 0x%08x\n",
		pin, info->name, func, val);

	writel(val, &pdata->fpioa->pins[pin]);
}

static int k210_pinconf_set_param(struct pinctrl_dev *pctldev,
				  unsigned int pin,
				  unsigned int param, unsigned int arg)
{
	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
	u32 val = readl(&pdata->fpioa->pins[pin]);
	int drive;

	dev_dbg(pdata->dev, "set pin %u param %u, arg 0x%x\n",
		pin, param, arg);

	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
		val &= ~K210_PC_BIAS_MASK;
		break;
	case PIN_CONFIG_BIAS_PULL_DOWN:
		if (!arg)
			return -EINVAL;
		val |= K210_PC_PD;
		break;
	case PIN_CONFIG_BIAS_PULL_UP:
		if (!arg)
			return -EINVAL;
		val |= K210_PC_PU;
		break;
	case PIN_CONFIG_DRIVE_STRENGTH:
		arg *= 1000;
		fallthrough;
	case PIN_CONFIG_DRIVE_STRENGTH_UA:
		drive = k210_pinconf_get_drive(arg);
		if (drive < 0)
			return drive;
		val &= ~K210_PC_DRIVE_MASK;
		val |= FIELD_PREP(K210_PC_DRIVE_MASK, drive);
		break;
	case PIN_CONFIG_INPUT_ENABLE:
		if (arg)
			val |= K210_PC_IE;
		else
			val &= ~K210_PC_IE;
		break;
	case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
		if (arg)
			val |= K210_PC_ST;
		else
			val &= ~K210_PC_ST;
		break;
	case PIN_CONFIG_OUTPUT:
		k210_pinmux_set_pin_function(pctldev, pin, K210_PCF_CONSTANT);
		val = readl(&pdata->fpioa->pins[pin]);
		val |= K210_PC_MODE_OUT;
		if (!arg)
			val |= K210_PC_DO_INV;
		break;
	case PIN_CONFIG_OUTPUT_ENABLE:
		if (arg)
			val |= K210_PC_OE;
		else
			val &= ~K210_PC_OE;
		break;
	case PIN_CONFIG_SLEW_RATE:
		if (arg)
			val |= K210_PC_SL;
		else
			val &= ~K210_PC_SL;
		break;
	case PIN_CONFIG_OUTPUT_INVERT:
		if (arg)
			val |= K210_PC_DO_INV;
		else
			val &= ~K210_PC_DO_INV;
		break;
	case PIN_CONFIG_INPUT_INVERT:
		if (arg)
			val |= K210_PC_DI_INV;
		else
			val &= ~K210_PC_DI_INV;
		break;
	default:
		return -EINVAL;
	}

	writel(val, &pdata->fpioa->pins[pin]);

	return 0;
}

static int k210_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
			    unsigned long *configs, unsigned int num_configs)
{
	unsigned int param, arg;
	int i, ret;

	if (WARN_ON(pin >= K210_NPINS))
		return -EINVAL;

	for (i = 0; i < num_configs; i++) {
		param = pinconf_to_config_param(configs[i]);
		arg = pinconf_to_config_argument(configs[i]);
		ret = k210_pinconf_set_param(pctldev, pin, param, arg);
		if (ret)
			return ret;
	}

	return 0;
}

static void k210_pinconf_dbg_show(struct pinctrl_dev *pctldev,
				  struct seq_file *s, unsigned int pin)
{
	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);

	seq_printf(s, "%#x", readl(&pdata->fpioa->pins[pin]));
}

static int k210_pinconf_group_set(struct pinctrl_dev *pctldev,
				  unsigned int selector, unsigned long *configs,
				  unsigned int num_configs)
{
	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
	unsigned int param, arg;
	u32 bit;
	int i;

	/* Pins should be configured with pinmux, not groups*/
	if (selector < K210_NPINS)
		return -EINVAL;

	/* Otherwise it's a power domain */
	for (i = 0; i < num_configs; i++) {
		param = pinconf_to_config_param(configs[i]);
		if (param != PIN_CONFIG_POWER_SOURCE)
			return -EINVAL;

		arg = pinconf_to_config_argument(configs[i]);
		bit = BIT(selector - K210_NPINS);
		regmap_update_bits(pdata->sysctl_map,
				   pdata->power_offset,
				   bit, arg ? bit : 0);
	}

	return 0;
}

static void k210_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
					struct seq_file *s,
					unsigned int selector)
{
	struct k210_fpioa_data *pdata = pinctrl_dev_get_drvdata(pctldev);
	int ret;
	u32 val;

	if (selector < K210_NPINS)
		return k210_pinconf_dbg_show(pctldev, s, selector);

	ret = regmap_read(pdata->sysctl_map, pdata->power_offset, &val);
	if (ret) {
		dev_err(pdata->dev, "Failed to read power reg\n");
		return;
	}

	seq_printf(s, "%s: %s V", k210_group_names[selector],
		   val & BIT(selector - K210_NPINS) ? "1.8" : "3.3");
}

static const struct pinconf_ops k210_pinconf_ops = {
	.is_generic = true,
	.pin_config_set = k210_pinconf_set,
	.pin_config_group_set = k210_pinconf_group_set,
	.pin_config_dbg_show = k210_pinconf_dbg_show,
	.pin_config_group_dbg_show = k210_pinconf_group_dbg_show,
};

static int k210_pinmux_get_function_count(struct pinctrl_dev *pctldev)
{
	return ARRAY_SIZE(k210_pcf_infos);
}

static const char *k210_pinmux_get_function_name(struct pinctrl_dev *pctldev,
						 unsigned int selector)
{
	return k210_pcf_infos[selector].name;
}

static int k210_pinmux_get_function_groups(struct pinctrl_dev *pctldev,
					   unsigned int selector,
					   const char * const **groups,
					   unsigned int * const num_groups)
{
	/* Any function can be mapped to any pin */
	*groups = k210_group_names;
	*num_groups = K210_NPINS;

	return 0;
}

static int k210_pinmux_set_mux(struct pinctrl_dev *pctldev,
			       unsigned int function,
			       unsigned int group)
{
	/* Can't mux power domains */
	if (group >= K210_NPINS)
		return -EINVAL;

	k210_pinmux_set_pin_function(pctldev, group, function);

	return 0;
}

static const struct pinmux_ops k210_pinmux_ops = {
	.get_functions_count = k210_pinmux_get_function_count,
	.get_function_name = k210_pinmux_get_function_name,
	.get_function_groups = k210_pinmux_get_function_groups,
	.set_mux = k210_pinmux_set_mux,
	.strict = true,
};

static int k210_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
{
	return K210_NGROUPS;
}

static const char *k210_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
					       unsigned int group)
{
	return k210_group_names[group];
}

static int k210_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
				       unsigned int group,
				       const unsigned int **pins,
				       unsigned int *npins)
{
	if (group >= K210_NPINS) {
		*pins = NULL;
		*npins = 0;
		return 0;
	}

	*pins = &k210_pins[group].number;
	*npins = 1;

	return 0;
}

static void k210_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
				      struct seq_file *s, unsigned int offset)
{
	seq_printf(s, "%s", dev_name(pctldev->dev));
}

static int k210_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
					  struct device_node *np,
					  struct pinctrl_map **map,
					  unsigned int *reserved_maps,
					  unsigned int *num_maps)
{
	struct property *prop;
	const __be32 *p;
	int ret, pinmux_groups;
	u32 pinmux_group;
	unsigned long *configs = NULL;
	unsigned int num_configs = 0;
	unsigned int reserve = 0;

	ret = of_property_count_strings(np, "groups");
	if (!ret)
		return pinconf_generic_dt_subnode_to_map(pctldev, np, map,
						reserved_maps, num_maps,
						PIN_MAP_TYPE_CONFIGS_GROUP);

	pinmux_groups = of_property_count_u32_elems(np, "pinmux");
	if (pinmux_groups <= 0) {
		/* Ignore this node */
		return 0;
	}

	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
					      &num_configs);
	if (ret < 0) {
		dev_err(pctldev->dev, "%pOF: could not parse node property\n",
			np);
		return ret;
	}

	reserve = pinmux_groups * (1 + num_configs);
	ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
					reserve);
	if (ret < 0)
		goto exit;

	of_property_for_each_u32(np, "pinmux", prop, p, pinmux_group) {
		const char *group_name, *func_name;
		u32 pin = FIELD_GET(K210_PG_PIN, pinmux_group);
		u32 func = FIELD_GET(K210_PG_FUNC, pinmux_group);

		if (pin >= K210_NPINS) {
			ret = -EINVAL;
			goto exit;
		}

		group_name = k210_group_names[pin];
		func_name = k210_pcf_infos[func].name;

		dev_dbg(pctldev->dev, "Pinmux %s: pin %u func %s\n",
			np->name, pin, func_name);

		ret = pinctrl_utils_add_map_mux(pctldev, map, reserved_maps,
						num_maps, group_name,
						func_name);
		if (ret < 0) {
			dev_err(pctldev->dev, "%pOF add mux map failed %d\n",
				np, ret);
			goto exit;
		}

		if (num_configs) {
			ret = pinctrl_utils_add_map_configs(pctldev, map,
					reserved_maps, num_maps, group_name,
					configs, num_configs,
					PIN_MAP_TYPE_CONFIGS_PIN);
			if (ret < 0) {
				dev_err(pctldev->dev,
					"%pOF add configs map failed %d\n",
					np, ret);
				goto exit;
			}
		}
	}

	ret = 0;

exit:
	kfree(configs);
	return ret;
}

static int k210_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
				       struct device_node *np_config,
				       struct pinctrl_map **map,
				       unsigned int *num_maps)
{
	unsigned int reserved_maps;
	struct device_node *np;
	int ret;

	reserved_maps = 0;
	*map = NULL;
	*num_maps = 0;

	ret = k210_pinctrl_dt_subnode_to_map(pctldev, np_config, map,
					     &reserved_maps, num_maps);
	if (ret < 0)
		goto err;

	for_each_available_child_of_node(np_config, np) {
		ret = k210_pinctrl_dt_subnode_to_map(pctldev, np, map,
						     &reserved_maps, num_maps);
		if (ret < 0) {
			of_node_put(np);
			goto err;
		}
	}
	return 0;

err:
	pinctrl_utils_free_map(pctldev, *map, *num_maps);
	return ret;
}


static const struct pinctrl_ops k210_pinctrl_ops = {
	.get_groups_count = k210_pinctrl_get_groups_count,
	.get_group_name = k210_pinctrl_get_group_name,
	.get_group_pins = k210_pinctrl_get_group_pins,
	.pin_dbg_show = k210_pinctrl_pin_dbg_show,
	.dt_node_to_map = k210_pinctrl_dt_node_to_map,
	.dt_free_map = pinconf_generic_dt_free_map,
};

static struct pinctrl_desc k210_pinctrl_desc = {
	.name = "k210-pinctrl",
	.pins = k210_pins,
	.npins = K210_NPINS,
	.pctlops = &k210_pinctrl_ops,
	.pmxops = &k210_pinmux_ops,
	.confops = &k210_pinconf_ops,
	.custom_params = k210_pinconf_custom_params,
	.num_custom_params = ARRAY_SIZE(k210_pinconf_custom_params),
};

static void k210_fpioa_init_ties(struct k210_fpioa_data *pdata)
{
	struct k210_fpioa __iomem *fpioa = pdata->fpioa;
	u32 val;
	int i, j;

	dev_dbg(pdata->dev, "Init pin ties\n");

	/* Init pin functions input ties */
	for (i = 0; i < ARRAY_SIZE(fpioa->tie_en); i++) {
		val = 0;
		for (j = 0; j < 32; j++) {
			if (k210_pcf_infos[i * 32 + j].mode_id ==
			    K210_PC_DEFAULT_IN_TIE) {
				dev_dbg(pdata->dev,
					"tie_en function %d (%s)\n",
					i * 32 + j,
					k210_pcf_infos[i * 32 + j].name);
				val |= BIT(j);
			}
		}

		/* Set value before enable */
		writel(val, &fpioa->tie_val[i]);
		writel(val, &fpioa->tie_en[i]);
	}
}

static int k210_fpioa_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct k210_fpioa_data *pdata;
	int ret;

	dev_info(dev, "K210 FPIOA pin controller\n");

	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	pdata->dev = dev;
	platform_set_drvdata(pdev, pdata);

	pdata->fpioa = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(pdata->fpioa))
		return PTR_ERR(pdata->fpioa);

	pdata->clk = devm_clk_get(dev, "ref");
	if (IS_ERR(pdata->clk))
		return PTR_ERR(pdata->clk);

	ret = clk_prepare_enable(pdata->clk);
	if (ret)
		return ret;

	pdata->pclk = devm_clk_get_optional(dev, "pclk");
	if (!IS_ERR(pdata->pclk)) {
		ret = clk_prepare_enable(pdata->pclk);
		if (ret)
			goto disable_clk;
	}

	pdata->sysctl_map =
		syscon_regmap_lookup_by_phandle_args(np,
						"canaan,k210-sysctl-power",
						1, &pdata->power_offset);
	if (IS_ERR(pdata->sysctl_map)) {
		ret = PTR_ERR(pdata->sysctl_map);
		goto disable_pclk;
	}

	k210_fpioa_init_ties(pdata);

	pdata->pctl = pinctrl_register(&k210_pinctrl_desc, dev, (void *)pdata);
	if (IS_ERR(pdata->pctl)) {
		ret = PTR_ERR(pdata->pctl);
		goto disable_pclk;
	}

	return 0;

disable_pclk:
	clk_disable_unprepare(pdata->pclk);
disable_clk:
	clk_disable_unprepare(pdata->clk);

	return ret;
}

static const struct of_device_id k210_fpioa_dt_ids[] = {
	{ .compatible = "canaan,k210-fpioa" },
	{ /* sentinel */ },
};

static struct platform_driver k210_fpioa_driver = {
	.probe	= k210_fpioa_probe,
	.driver = {
		.name		= "k210-fpioa",
		.of_match_table	= k210_fpioa_dt_ids,
	},
};
builtin_platform_driver(k210_fpioa_driver);