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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2024 Nuvoton Technology Corp.
*
* Author: Shan-Chun Hung <schung@nuvoton.com>
* * Jacky Huang <ychuang3@nuvoton.com>
*/
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/cleanup.h>
#include <linux/clk.h>
#include <linux/gpio/driver.h>
#include <linux/mfd/syscon.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinctrl.h>
#include "../core.h"
#include "../pinconf.h"
#include "pinctrl-ma35.h"
#define MA35_MFP_REG_BASE 0x80
#define MA35_MFP_REG_SZ_PER_BANK 8
#define MA35_MFP_BITS_PER_PORT 4
#define MA35_GPIO_BANK_MAX 14
#define MA35_GPIO_PORT_MAX 16
/* GPIO control registers */
#define MA35_GP_REG_MODE 0x00
#define MA35_GP_REG_DINOFF 0x04
#define MA35_GP_REG_DOUT 0x08
#define MA35_GP_REG_DATMSK 0x0c
#define MA35_GP_REG_PIN 0x10
#define MA35_GP_REG_DBEN 0x14
#define MA35_GP_REG_INTTYPE 0x18
#define MA35_GP_REG_INTEN 0x1c
#define MA35_GP_REG_INTSRC 0x20
#define MA35_GP_REG_SMTEN 0x24
#define MA35_GP_REG_SLEWCTL 0x28
#define MA35_GP_REG_SPW 0x2c
#define MA35_GP_REG_PUSEL 0x30
#define MA35_GP_REG_DSL 0x38
#define MA35_GP_REG_DSH 0x3c
/* GPIO mode control */
#define MA35_GP_MODE_INPUT 0x0
#define MA35_GP_MODE_OUTPUT 0x1
#define MA35_GP_MODE_OPEN_DRAIN 0x2
#define MA35_GP_MODE_QUASI 0x3
#define MA35_GP_MODE_MASK(n) GENMASK(n * 2 + 1, n * 2)
#define MA35_GP_SLEWCTL_MASK(n) GENMASK(n * 2 + 1, n * 2)
/* GPIO pull-up and pull-down selection control */
#define MA35_GP_PUSEL_DISABLE 0x0
#define MA35_GP_PUSEL_PULL_UP 0x1
#define MA35_GP_PUSEL_PULL_DOWN 0x2
#define MA35_GP_PUSEL_MASK(n) GENMASK(n * 2 + 1, n * 2)
/*
* The MA35_GP_REG_INTEN bits 0 ~ 15 control low-level or falling edge trigger,
* while bits 16 ~ 31 control high-level or rising edge trigger.
*/
#define MA35_GP_INTEN_L(n) BIT(n)
#define MA35_GP_INTEN_H(n) BIT(n + 16)
#define MA35_GP_INTEN_BOTH(n) (MA35_GP_INTEN_H(n) | MA35_GP_INTEN_L(n))
/*
* The MA35_GP_REG_DSL register controls ports 0 to 7, while the MA35_GP_REG_DSH
* register controls ports 8 to 15. Each port occupies a width of 4 bits, with 3
* bits being effective.
*/
#define MA35_GP_DS_REG(n) (n < 8 ? MA35_GP_REG_DSL : MA35_GP_REG_DSH)
#define MA35_GP_DS_MASK(n) GENMASK((n % 8) * 4 + 3, (n % 8) * 4)
#define MVOLT_1800 0
#define MVOLT_3300 1
/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
static const char * const gpio_group_name[] = {
"gpioa", "gpiob", "gpioc", "gpiod", "gpioe", "gpiof", "gpiog",
"gpioh", "gpioi", "gpioj", "gpiok", "gpiol", "gpiom", "gpion",
};
static const u32 ds_1800mv_tbl[] = {
2900, 4400, 5800, 7300, 8600, 10100, 11500, 13000,
};
static const u32 ds_3300mv_tbl[] = {
17100, 25600, 34100, 42800, 48000, 56000, 77000, 82000,
};
struct ma35_pin_func {
const char *name;
const char **groups;
u32 ngroups;
};
struct ma35_pin_setting {
u32 offset;
u32 shift;
u32 muxval;
unsigned long *configs;
unsigned int nconfigs;
};
struct ma35_pin_group {
const char *name;
unsigned int npins;
unsigned int *pins;
struct ma35_pin_setting *settings;
};
struct ma35_pin_bank {
void __iomem *reg_base;
struct clk *clk;
int irq;
u8 bank_num;
u8 nr_pins;
bool valid;
const char *name;
struct fwnode_handle *fwnode;
struct gpio_chip chip;
u32 irqtype;
u32 irqinten;
struct regmap *regmap;
struct device *dev;
};
struct ma35_pin_ctrl {
struct ma35_pin_bank *pin_banks;
u32 nr_banks;
u32 nr_pins;
};
struct ma35_pinctrl {
struct device *dev;
struct ma35_pin_ctrl *ctrl;
struct pinctrl_dev *pctl;
const struct ma35_pinctrl_soc_info *info;
struct regmap *regmap;
struct ma35_pin_group *groups;
unsigned int ngroups;
struct ma35_pin_func *functions;
unsigned int nfunctions;
};
static DEFINE_RAW_SPINLOCK(ma35_lock);
static int ma35_get_groups_count(struct pinctrl_dev *pctldev)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
return npctl->ngroups;
}
static const char *ma35_get_group_name(struct pinctrl_dev *pctldev, unsigned int selector)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
return npctl->groups[selector].name;
}
static int ma35_get_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
const unsigned int **pins, unsigned int *npins)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
if (selector >= npctl->ngroups)
return -EINVAL;
*pins = npctl->groups[selector].pins;
*npins = npctl->groups[selector].npins;
return 0;
}
static struct ma35_pin_group *ma35_pinctrl_find_group_by_name(
const struct ma35_pinctrl *npctl, const char *name)
{
int i;
for (i = 0; i < npctl->ngroups; i++) {
if (!strcmp(npctl->groups[i].name, name))
return &npctl->groups[i];
}
return NULL;
}
static int ma35_pinctrl_dt_node_to_map_func(struct pinctrl_dev *pctldev,
struct device_node *np,
struct pinctrl_map **map,
unsigned int *num_maps)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
struct ma35_pin_group *grp;
struct pinctrl_map *new_map;
struct device_node *parent;
int map_num = 1;
int i;
/*
* first find the group of this node and check if we need create
* config maps for pins
*/
grp = ma35_pinctrl_find_group_by_name(npctl, np->name);
if (!grp) {
dev_err(npctl->dev, "unable to find group for node %s\n", np->name);
return -EINVAL;
}
map_num += grp->npins;
new_map = kcalloc(map_num, sizeof(*new_map), GFP_KERNEL);
if (!new_map)
return -ENOMEM;
*map = new_map;
*num_maps = map_num;
/* create mux map */
parent = of_get_parent(np);
if (!parent)
return -EINVAL;
new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
new_map[0].data.mux.function = parent->name;
new_map[0].data.mux.group = np->name;
of_node_put(parent);
new_map++;
for (i = 0; i < grp->npins; i++) {
new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
new_map[i].data.configs.group_or_pin = pin_get_name(pctldev, grp->pins[i]);
new_map[i].data.configs.configs = grp->settings[i].configs;
new_map[i].data.configs.num_configs = grp->settings[i].nconfigs;
}
dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
(*map)->data.mux.function, (*map)->data.mux.group, map_num);
return 0;
}
static const struct pinctrl_ops ma35_pctrl_ops = {
.get_groups_count = ma35_get_groups_count,
.get_group_name = ma35_get_group_name,
.get_group_pins = ma35_get_group_pins,
.dt_node_to_map = ma35_pinctrl_dt_node_to_map_func,
.dt_free_map = pinconf_generic_dt_free_map,
};
static int ma35_pinmux_get_func_count(struct pinctrl_dev *pctldev)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
return npctl->nfunctions;
}
static const char *ma35_pinmux_get_func_name(struct pinctrl_dev *pctldev,
unsigned int selector)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
return npctl->functions[selector].name;
}
static int ma35_pinmux_get_func_groups(struct pinctrl_dev *pctldev,
unsigned int function,
const char *const **groups,
unsigned int *const num_groups)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
*groups = npctl->functions[function].groups;
*num_groups = npctl->functions[function].ngroups;
return 0;
}
static int ma35_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
unsigned int group)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
struct ma35_pin_group *grp = &npctl->groups[group];
struct ma35_pin_setting *setting = grp->settings;
u32 i, regval;
dev_dbg(npctl->dev, "enable function %s group %s\n",
npctl->functions[selector].name, npctl->groups[group].name);
for (i = 0; i < grp->npins; i++) {
regmap_read(npctl->regmap, setting->offset, ®val);
regval &= ~GENMASK(setting->shift + MA35_MFP_BITS_PER_PORT - 1,
setting->shift);
regval |= setting->muxval << setting->shift;
regmap_write(npctl->regmap, setting->offset, regval);
setting++;
}
return 0;
}
static const struct pinmux_ops ma35_pmx_ops = {
.get_functions_count = ma35_pinmux_get_func_count,
.get_function_name = ma35_pinmux_get_func_name,
.get_function_groups = ma35_pinmux_get_func_groups,
.set_mux = ma35_pinmux_set_mux,
.strict = true,
};
static void ma35_gpio_set_mode(void __iomem *reg_mode, unsigned int gpio, u32 mode)
{
u32 regval = readl(reg_mode);
regval &= ~MA35_GP_MODE_MASK(gpio);
regval |= field_prep(MA35_GP_MODE_MASK(gpio), mode);
writel(regval, reg_mode);
}
static u32 ma35_gpio_get_mode(void __iomem *reg_mode, unsigned int gpio)
{
u32 regval = readl(reg_mode);
return field_get(MA35_GP_MODE_MASK(gpio), regval);
}
static int ma35_gpio_core_direction_in(struct gpio_chip *gc, unsigned int gpio)
{
struct ma35_pin_bank *bank = gpiochip_get_data(gc);
void __iomem *reg_mode = bank->reg_base + MA35_GP_REG_MODE;
guard(raw_spinlock_irqsave)(&ma35_lock);
ma35_gpio_set_mode(reg_mode, gpio, MA35_GP_MODE_INPUT);
return 0;
}
static int ma35_gpio_core_direction_out(struct gpio_chip *gc, unsigned int gpio, int val)
{
struct ma35_pin_bank *bank = gpiochip_get_data(gc);
void __iomem *reg_dout = bank->reg_base + MA35_GP_REG_DOUT;
void __iomem *reg_mode = bank->reg_base + MA35_GP_REG_MODE;
unsigned int regval;
guard(raw_spinlock_irqsave)(&ma35_lock);
regval = readl(reg_dout);
if (val)
regval |= BIT(gpio);
else
regval &= ~BIT(gpio);
writel(regval, reg_dout);
ma35_gpio_set_mode(reg_mode, gpio, MA35_GP_MODE_OUTPUT);
return 0;
}
static int ma35_gpio_core_get(struct gpio_chip *gc, unsigned int gpio)
{
struct ma35_pin_bank *bank = gpiochip_get_data(gc);
void __iomem *reg_pin = bank->reg_base + MA35_GP_REG_PIN;
return !!(readl(reg_pin) & BIT(gpio));
}
static void ma35_gpio_core_set(struct gpio_chip *gc, unsigned int gpio, int val)
{
struct ma35_pin_bank *bank = gpiochip_get_data(gc);
void __iomem *reg_dout = bank->reg_base + MA35_GP_REG_DOUT;
u32 regval;
if (val)
regval = readl(reg_dout) | BIT(gpio);
else
regval = readl(reg_dout) & ~BIT(gpio);
writel(regval, reg_dout);
}
static int ma35_gpio_core_to_request(struct gpio_chip *gc, unsigned int gpio)
{
struct ma35_pin_bank *bank = gpiochip_get_data(gc);
u32 reg_offs, bit_offs, regval;
if (gpio < 8) {
/* The MFP low register controls port 0 ~ 7 */
reg_offs = bank->bank_num * MA35_MFP_REG_SZ_PER_BANK;
bit_offs = gpio * MA35_MFP_BITS_PER_PORT;
} else {
/* The MFP high register controls port 8 ~ 15 */
reg_offs = bank->bank_num * MA35_MFP_REG_SZ_PER_BANK + 4;
bit_offs = (gpio - 8) * MA35_MFP_BITS_PER_PORT;
}
regmap_read(bank->regmap, MA35_MFP_REG_BASE + reg_offs, ®val);
regval &= ~GENMASK(bit_offs + MA35_MFP_BITS_PER_PORT - 1, bit_offs);
regmap_write(bank->regmap, MA35_MFP_REG_BASE + reg_offs, regval);
return 0;
}
static void ma35_irq_gpio_ack(struct irq_data *d)
{
struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
void __iomem *reg_intsrc = bank->reg_base + MA35_GP_REG_INTSRC;
irq_hw_number_t hwirq = irqd_to_hwirq(d);
writel(BIT(hwirq), reg_intsrc);
}
static void ma35_irq_gpio_mask(struct irq_data *d)
{
struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
void __iomem *reg_ien = bank->reg_base + MA35_GP_REG_INTEN;
irq_hw_number_t hwirq = irqd_to_hwirq(d);
u32 regval;
regval = readl(reg_ien);
regval &= ~MA35_GP_INTEN_BOTH(hwirq);
writel(regval, reg_ien);
}
static void ma35_irq_gpio_unmask(struct irq_data *d)
{
struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
void __iomem *reg_itype = bank->reg_base + MA35_GP_REG_INTTYPE;
void __iomem *reg_ien = bank->reg_base + MA35_GP_REG_INTEN;
irq_hw_number_t hwirq = irqd_to_hwirq(d);
u32 bval, regval;
bval = bank->irqtype & BIT(hwirq);
regval = readl(reg_itype);
regval &= ~BIT(hwirq);
writel(regval | bval, reg_itype);
bval = bank->irqinten & MA35_GP_INTEN_BOTH(hwirq);
regval = readl(reg_ien);
regval &= ~MA35_GP_INTEN_BOTH(hwirq);
writel(regval | bval, reg_ien);
}
static int ma35_irq_irqtype(struct irq_data *d, unsigned int type)
{
struct ma35_pin_bank *bank = gpiochip_get_data(irq_data_get_irq_chip_data(d));
irq_hw_number_t hwirq = irqd_to_hwirq(d);
switch (type) {
case IRQ_TYPE_EDGE_BOTH:
irq_set_handler_locked(d, handle_edge_irq);
bank->irqtype &= ~BIT(hwirq);
bank->irqinten |= MA35_GP_INTEN_BOTH(hwirq);
break;
case IRQ_TYPE_EDGE_RISING:
case IRQ_TYPE_LEVEL_HIGH:
irq_set_handler_locked(d, handle_edge_irq);
bank->irqtype &= ~BIT(hwirq);
bank->irqinten |= MA35_GP_INTEN_H(hwirq);
bank->irqinten &= ~MA35_GP_INTEN_L(hwirq);
break;
case IRQ_TYPE_EDGE_FALLING:
case IRQ_TYPE_LEVEL_LOW:
irq_set_handler_locked(d, handle_edge_irq);
bank->irqtype &= ~BIT(hwirq);
bank->irqinten |= MA35_GP_INTEN_L(hwirq);
bank->irqinten &= ~MA35_GP_INTEN_H(hwirq);
break;
default:
return -EINVAL;
}
writel(bank->irqtype, bank->reg_base + MA35_GP_REG_INTTYPE);
writel(bank->irqinten, bank->reg_base + MA35_GP_REG_INTEN);
return 0;
}
static struct irq_chip ma35_gpio_irqchip = {
.name = "MA35-GPIO-IRQ",
.irq_disable = ma35_irq_gpio_mask,
.irq_enable = ma35_irq_gpio_unmask,
.irq_ack = ma35_irq_gpio_ack,
.irq_mask = ma35_irq_gpio_mask,
.irq_unmask = ma35_irq_gpio_unmask,
.irq_set_type = ma35_irq_irqtype,
.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
GPIOCHIP_IRQ_RESOURCE_HELPERS,
};
static void ma35_irq_demux_intgroup(struct irq_desc *desc)
{
struct ma35_pin_bank *bank = gpiochip_get_data(irq_desc_get_handler_data(desc));
struct irq_domain *irqdomain = bank->chip.irq.domain;
struct irq_chip *irqchip = irq_desc_get_chip(desc);
unsigned long isr;
int offset;
chained_irq_enter(irqchip, desc);
isr = readl(bank->reg_base + MA35_GP_REG_INTSRC);
for_each_set_bit(offset, &isr, bank->nr_pins)
generic_handle_irq(irq_find_mapping(irqdomain, offset));
chained_irq_exit(irqchip, desc);
}
static int ma35_gpiolib_register(struct platform_device *pdev, struct ma35_pinctrl *npctl)
{
struct ma35_pin_ctrl *ctrl = npctl->ctrl;
struct ma35_pin_bank *bank = ctrl->pin_banks;
int ret;
int i;
for (i = 0; i < ctrl->nr_banks; i++, bank++) {
if (!bank->valid) {
dev_warn(&pdev->dev, "%pfw: bank is not valid\n", bank->fwnode);
continue;
}
bank->irqtype = 0;
bank->irqinten = 0;
bank->chip.label = bank->name;
bank->chip.of_gpio_n_cells = 2;
bank->chip.parent = &pdev->dev;
bank->chip.request = ma35_gpio_core_to_request;
bank->chip.direction_input = ma35_gpio_core_direction_in;
bank->chip.direction_output = ma35_gpio_core_direction_out;
bank->chip.get = ma35_gpio_core_get;
bank->chip.set = ma35_gpio_core_set;
bank->chip.base = -1;
bank->chip.ngpio = bank->nr_pins;
bank->chip.can_sleep = false;
if (bank->irq > 0) {
struct gpio_irq_chip *girq;
girq = &bank->chip.irq;
gpio_irq_chip_set_chip(girq, &ma35_gpio_irqchip);
girq->parent_handler = ma35_irq_demux_intgroup;
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] = bank->irq;
girq->default_type = IRQ_TYPE_NONE;
girq->handler = handle_bad_irq;
}
ret = devm_gpiochip_add_data(&pdev->dev, &bank->chip, bank);
if (ret) {
dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
bank->chip.label, ret);
return ret;
}
}
return 0;
}
static int ma35_get_bank_data(struct ma35_pin_bank *bank)
{
bank->reg_base = fwnode_iomap(bank->fwnode, 0);
if (!bank->reg_base)
return -ENOMEM;
bank->irq = fwnode_irq_get(bank->fwnode, 0);
bank->nr_pins = MA35_GPIO_PORT_MAX;
bank->clk = of_clk_get(to_of_node(bank->fwnode), 0);
if (IS_ERR(bank->clk))
return PTR_ERR(bank->clk);
return clk_prepare_enable(bank->clk);
}
static int ma35_pinctrl_get_soc_data(struct ma35_pinctrl *pctl, struct platform_device *pdev)
{
struct fwnode_handle *child;
struct ma35_pin_ctrl *ctrl;
struct ma35_pin_bank *bank;
int i, id = 0;
ctrl = pctl->ctrl;
ctrl->nr_banks = MA35_GPIO_BANK_MAX;
ctrl->pin_banks = devm_kcalloc(&pdev->dev, ctrl->nr_banks,
sizeof(*ctrl->pin_banks), GFP_KERNEL);
if (!ctrl->pin_banks)
return -ENOMEM;
for (i = 0; i < ctrl->nr_banks; i++) {
ctrl->pin_banks[i].bank_num = i;
ctrl->pin_banks[i].name = gpio_group_name[i];
}
for_each_gpiochip_node(&pdev->dev, child) {
bank = &ctrl->pin_banks[id];
bank->fwnode = child;
bank->regmap = pctl->regmap;
bank->dev = &pdev->dev;
if (!ma35_get_bank_data(bank))
bank->valid = true;
id++;
}
return 0;
}
static void ma35_gpio_cla_port(unsigned int gpio_num, unsigned int *group,
unsigned int *num)
{
*group = gpio_num / MA35_GPIO_PORT_MAX;
*num = gpio_num % MA35_GPIO_PORT_MAX;
}
static int ma35_pinconf_set_pull(struct ma35_pinctrl *npctl, unsigned int pin,
int pull_up)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval, pull_sel = MA35_GP_PUSEL_DISABLE;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_PUSEL);
regval &= ~MA35_GP_PUSEL_MASK(port);
switch (pull_up) {
case PIN_CONFIG_BIAS_PULL_UP:
pull_sel = MA35_GP_PUSEL_PULL_UP;
break;
case PIN_CONFIG_BIAS_PULL_DOWN:
pull_sel = MA35_GP_PUSEL_PULL_DOWN;
break;
case PIN_CONFIG_BIAS_DISABLE:
pull_sel = MA35_GP_PUSEL_DISABLE;
break;
}
regval |= field_prep(MA35_GP_PUSEL_MASK(port), pull_sel);
writel(regval, base + MA35_GP_REG_PUSEL);
return 0;
}
static int ma35_pinconf_get_output(struct ma35_pinctrl *npctl, unsigned int pin)
{
unsigned int port, group_num;
void __iomem *base;
u32 mode;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
mode = ma35_gpio_get_mode(base + MA35_GP_REG_MODE, port);
if (mode == MA35_GP_MODE_OUTPUT)
return 1;
return 0;
}
static int ma35_pinconf_get_pull(struct ma35_pinctrl *npctl, unsigned int pin)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval, pull_sel;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_PUSEL);
pull_sel = field_get(MA35_GP_PUSEL_MASK(port), regval);
switch (pull_sel) {
case MA35_GP_PUSEL_PULL_UP:
return PIN_CONFIG_BIAS_PULL_UP;
case MA35_GP_PUSEL_PULL_DOWN:
return PIN_CONFIG_BIAS_PULL_DOWN;
case MA35_GP_PUSEL_DISABLE:
return PIN_CONFIG_BIAS_DISABLE;
}
return PIN_CONFIG_BIAS_DISABLE;
}
static int ma35_pinconf_set_output(struct ma35_pinctrl *npctl, unsigned int pin, bool out)
{
unsigned int port, group_num;
void __iomem *base;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
ma35_gpio_set_mode(base + MA35_GP_REG_MODE, port, MA35_GP_MODE_OUTPUT);
return 0;
}
static int ma35_pinconf_get_power_source(struct ma35_pinctrl *npctl, unsigned int pin)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SPW);
if (regval & BIT(port))
return MVOLT_3300;
else
return MVOLT_1800;
}
static int ma35_pinconf_set_power_source(struct ma35_pinctrl *npctl,
unsigned int pin, int arg)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
if ((arg != MVOLT_1800) && (arg != MVOLT_3300))
return -EINVAL;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SPW);
if (arg == MVOLT_1800)
regval &= ~BIT(port);
else
regval |= BIT(port);
writel(regval, base + MA35_GP_REG_SPW);
return 0;
}
static int ma35_pinconf_get_drive_strength(struct ma35_pinctrl *npctl, unsigned int pin,
u32 *strength)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval, ds_val;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_DS_REG(port));
ds_val = field_get(MA35_GP_DS_MASK(port), regval);
if (ma35_pinconf_get_power_source(npctl, pin) == MVOLT_1800)
*strength = ds_1800mv_tbl[ds_val];
else
*strength = ds_3300mv_tbl[ds_val];
return 0;
}
static int ma35_pinconf_set_drive_strength(struct ma35_pinctrl *npctl, unsigned int pin,
int strength)
{
unsigned int port, group_num;
void __iomem *base;
int i, ds_val = -1;
u32 regval;
if (ma35_pinconf_get_power_source(npctl, pin) == MVOLT_1800) {
for (i = 0; i < ARRAY_SIZE(ds_1800mv_tbl); i++) {
if (ds_1800mv_tbl[i] == strength) {
ds_val = i;
break;
}
}
} else {
for (i = 0; i < ARRAY_SIZE(ds_3300mv_tbl); i++) {
if (ds_3300mv_tbl[i] == strength) {
ds_val = i;
break;
}
}
}
if (ds_val == -1)
return -EINVAL;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_DS_REG(port));
regval &= ~MA35_GP_DS_MASK(port);
regval |= field_prep(MA35_GP_DS_MASK(port), ds_val);
writel(regval, base + MA35_GP_DS_REG(port));
return 0;
}
static int ma35_pinconf_get_schmitt_enable(struct ma35_pinctrl *npctl, unsigned int pin)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SMTEN);
return !!(regval & BIT(port));
}
static int ma35_pinconf_set_schmitt(struct ma35_pinctrl *npctl, unsigned int pin, int enable)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SMTEN);
if (enable)
regval |= BIT(port);
else
regval &= ~BIT(port);
writel(regval, base + MA35_GP_REG_SMTEN);
return 0;
}
static int ma35_pinconf_get_slew_rate(struct ma35_pinctrl *npctl, unsigned int pin)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SLEWCTL);
return field_get(MA35_GP_SLEWCTL_MASK(port), regval);
}
static int ma35_pinconf_set_slew_rate(struct ma35_pinctrl *npctl, unsigned int pin, int rate)
{
unsigned int port, group_num;
void __iomem *base;
u32 regval;
ma35_gpio_cla_port(pin, &group_num, &port);
base = npctl->ctrl->pin_banks[group_num].reg_base;
regval = readl(base + MA35_GP_REG_SLEWCTL);
regval &= ~MA35_GP_SLEWCTL_MASK(port);
regval |= field_prep(MA35_GP_SLEWCTL_MASK(port), rate);
writel(regval, base + MA35_GP_REG_SLEWCTL);
return 0;
}
static int ma35_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin, unsigned long *config)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
enum pin_config_param param = pinconf_to_config_param(*config);
u32 arg;
int ret;
switch (param) {
case PIN_CONFIG_BIAS_DISABLE:
case PIN_CONFIG_BIAS_PULL_DOWN:
case PIN_CONFIG_BIAS_PULL_UP:
if (ma35_pinconf_get_pull(npctl, pin) != param)
return -EINVAL;
arg = 1;
break;
case PIN_CONFIG_DRIVE_STRENGTH:
ret = ma35_pinconf_get_drive_strength(npctl, pin, &arg);
if (ret)
return ret;
break;
case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
arg = ma35_pinconf_get_schmitt_enable(npctl, pin);
break;
case PIN_CONFIG_SLEW_RATE:
arg = ma35_pinconf_get_slew_rate(npctl, pin);
break;
case PIN_CONFIG_OUTPUT_ENABLE:
arg = ma35_pinconf_get_output(npctl, pin);
break;
case PIN_CONFIG_POWER_SOURCE:
arg = ma35_pinconf_get_power_source(npctl, pin);
break;
default:
return -EINVAL;
}
*config = pinconf_to_config_packed(param, arg);
return 0;
}
static int ma35_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
unsigned long *configs, unsigned int num_configs)
{
struct ma35_pinctrl *npctl = pinctrl_dev_get_drvdata(pctldev);
enum pin_config_param param;
unsigned int arg = 0;
int i, ret = 0;
for (i = 0; i < num_configs; i++) {
param = pinconf_to_config_param(configs[i]);
arg = pinconf_to_config_argument(configs[i]);
switch (param) {
case PIN_CONFIG_BIAS_DISABLE:
case PIN_CONFIG_BIAS_PULL_UP:
case PIN_CONFIG_BIAS_PULL_DOWN:
ret = ma35_pinconf_set_pull(npctl, pin, param);
break;
case PIN_CONFIG_DRIVE_STRENGTH:
ret = ma35_pinconf_set_drive_strength(npctl, pin, arg);
break;
case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
ret = ma35_pinconf_set_schmitt(npctl, pin, 1);
break;
case PIN_CONFIG_INPUT_SCHMITT:
ret = ma35_pinconf_set_schmitt(npctl, pin, arg);
break;
case PIN_CONFIG_SLEW_RATE:
ret = ma35_pinconf_set_slew_rate(npctl, pin, arg);
break;
case PIN_CONFIG_OUTPUT_ENABLE:
ret = ma35_pinconf_set_output(npctl, pin, arg);
break;
case PIN_CONFIG_POWER_SOURCE:
ret = ma35_pinconf_set_power_source(npctl, pin, arg);
break;
default:
return -EINVAL;
}
if (ret)
break;
}
return ret;
}
static const struct pinconf_ops ma35_pinconf_ops = {
.pin_config_get = ma35_pinconf_get,
.pin_config_set = ma35_pinconf_set,
.is_generic = true,
};
static int ma35_pinctrl_parse_groups(struct device_node *np, struct ma35_pin_group *grp,
struct ma35_pinctrl *npctl, u32 index)
{
struct ma35_pin_setting *pin;
unsigned long *configs;
unsigned int nconfigs;
int i, j, count, ret;
u32 *elems;
grp->name = np->name;
ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &nconfigs);
if (ret)
return ret;
count = of_property_count_elems_of_size(np, "nuvoton,pins", sizeof(u32));
if (!count || count % 3)
return -EINVAL;
elems = devm_kmalloc_array(npctl->dev, count, sizeof(u32), GFP_KERNEL);
if (!elems)
return -ENOMEM;
ret = of_property_read_u32_array(np, "nuvoton,pins", elems, count);
if (ret)
return -EINVAL;
grp->npins = count / 3;
grp->pins = devm_kcalloc(npctl->dev, grp->npins, sizeof(*grp->pins), GFP_KERNEL);
if (!grp->pins)
return -ENOMEM;
grp->settings = devm_kcalloc(npctl->dev, grp->npins, sizeof(*grp->settings), GFP_KERNEL);
if (!grp->settings)
return -ENOMEM;
pin = grp->settings;
for (i = 0, j = 0; i < count; i += 3, j++) {
pin->offset = elems[i] * MA35_MFP_REG_SZ_PER_BANK + MA35_MFP_REG_BASE;
pin->shift = (elems[i + 1] * MA35_MFP_BITS_PER_PORT) % 32;
pin->muxval = elems[i + 2];
pin->configs = configs;
pin->nconfigs = nconfigs;
grp->pins[j] = npctl->info->get_pin_num(pin->offset, pin->shift);
pin++;
}
return 0;
}
static int ma35_pinctrl_parse_functions(struct device_node *np, struct ma35_pinctrl *npctl,
u32 index)
{
struct device_node *child;
struct ma35_pin_func *func;
struct ma35_pin_group *grp;
static u32 grp_index;
u32 ret, i = 0;
dev_dbg(npctl->dev, "parse function(%d): %s\n", index, np->name);
func = &npctl->functions[index];
func->name = np->name;
func->ngroups = of_get_child_count(np);
if (func->ngroups <= 0)
return 0;
func->groups = devm_kcalloc(npctl->dev, func->ngroups, sizeof(char *), GFP_KERNEL);
if (!func->groups)
return -ENOMEM;
for_each_child_of_node(np, child) {
func->groups[i] = child->name;
grp = &npctl->groups[grp_index++];
ret = ma35_pinctrl_parse_groups(child, grp, npctl, i++);
if (ret) {
of_node_put(child);
return ret;
}
}
return 0;
}
static int ma35_pinctrl_probe_dt(struct platform_device *pdev, struct ma35_pinctrl *npctl)
{
struct fwnode_handle *child;
u32 idx = 0;
int ret;
device_for_each_child_node(&pdev->dev, child) {
if (fwnode_property_present(child, "gpio-controller"))
continue;
npctl->nfunctions++;
npctl->ngroups += of_get_child_count(to_of_node(child));
}
if (!npctl->nfunctions)
return -EINVAL;
npctl->functions = devm_kcalloc(&pdev->dev, npctl->nfunctions,
sizeof(*npctl->functions), GFP_KERNEL);
if (!npctl->functions)
return -ENOMEM;
npctl->groups = devm_kcalloc(&pdev->dev, npctl->ngroups,
sizeof(*npctl->groups), GFP_KERNEL);
if (!npctl->groups)
return -ENOMEM;
device_for_each_child_node(&pdev->dev, child) {
if (fwnode_property_present(child, "gpio-controller"))
continue;
ret = ma35_pinctrl_parse_functions(to_of_node(child), npctl, idx++);
if (ret) {
fwnode_handle_put(child);
dev_err(&pdev->dev, "failed to parse function\n");
return ret;
}
}
return 0;
}
int ma35_pinctrl_probe(struct platform_device *pdev, const struct ma35_pinctrl_soc_info *info)
{
struct pinctrl_desc *ma35_pinctrl_desc;
struct device *dev = &pdev->dev;
struct ma35_pinctrl *npctl;
int ret;
if (!info || !info->pins || !info->npins) {
dev_err(&pdev->dev, "wrong pinctrl info\n");
return -EINVAL;
}
npctl = devm_kzalloc(&pdev->dev, sizeof(*npctl), GFP_KERNEL);
if (!npctl)
return -ENOMEM;
ma35_pinctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*ma35_pinctrl_desc), GFP_KERNEL);
if (!ma35_pinctrl_desc)
return -ENOMEM;
npctl->ctrl = devm_kzalloc(&pdev->dev, sizeof(*npctl->ctrl), GFP_KERNEL);
if (!npctl->ctrl)
return -ENOMEM;
ma35_pinctrl_desc->name = dev_name(&pdev->dev);
ma35_pinctrl_desc->pins = info->pins;
ma35_pinctrl_desc->npins = info->npins;
ma35_pinctrl_desc->pctlops = &ma35_pctrl_ops;
ma35_pinctrl_desc->pmxops = &ma35_pmx_ops;
ma35_pinctrl_desc->confops = &ma35_pinconf_ops;
ma35_pinctrl_desc->owner = THIS_MODULE;
npctl->info = info;
npctl->dev = &pdev->dev;
npctl->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "nuvoton,sys");
if (IS_ERR(npctl->regmap))
return dev_err_probe(&pdev->dev, PTR_ERR(npctl->regmap),
"No syscfg phandle specified\n");
ret = ma35_pinctrl_get_soc_data(npctl, pdev);
if (ret)
return dev_err_probe(&pdev->dev, ret, "fail to get soc data\n");
platform_set_drvdata(pdev, npctl);
ret = ma35_pinctrl_probe_dt(pdev, npctl);
if (ret)
return dev_err_probe(&pdev->dev, ret, "fail to probe MA35 pinctrl dt\n");
ret = devm_pinctrl_register_and_init(dev, ma35_pinctrl_desc, npctl, &npctl->pctl);
if (ret)
return dev_err_probe(&pdev->dev, ret, "fail to register MA35 pinctrl\n");
ret = pinctrl_enable(npctl->pctl);
if (ret)
return dev_err_probe(&pdev->dev, ret, "fail to enable MA35 pinctrl\n");
return ma35_gpiolib_register(pdev, npctl);
}
int ma35_pinctrl_suspend(struct device *dev)
{
struct ma35_pinctrl *npctl = dev_get_drvdata(dev);
return pinctrl_force_sleep(npctl->pctl);
}
int ma35_pinctrl_resume(struct device *dev)
{
struct ma35_pinctrl *npctl = dev_get_drvdata(dev);
return pinctrl_force_default(npctl->pctl);
}
|