summaryrefslogtreecommitdiff
path: root/drivers/gpio/gpio-langwell.c
blob: 33c8da887220e724395f5ced9446d340e10d8965 (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
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
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
/* gpio-langwell.c Moorestown platform Langwell chip GPIO driver
 * Copyright (c) 2008 - 2013,  Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Supports:
 * Moorestown platform Langwell chip.
 * Medfield platform Penwell chip.
 * Whitney point.
 */

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/stddef.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/gpio/driver.h>
#include <linux/slab.h>
#include <linux/lnw_gpio.h>
#include <linux/pm_runtime.h>
#include <asm/intel-mid.h>
#include <linux/irqdomain.h>
#include <asm/intel_scu_flis.h>
#include "gpiodebug.h"

#define IRQ_TYPE_EDGE	(1 << 0)
#define IRQ_TYPE_LEVEL	(1 << 1)

#define TANGIER_I2C_FLIS_START	0x1D00
#define TANGIER_I2C_FLIS_END	0x1D34

/*
 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
 * registers to control them, so we only define the order here instead of a
 * structure, to get a bit offset for a pin (use GPDR as an example):
 *
 * nreg = ngpio / 32;
 * reg = offset / 32;
 * bit = offset % 32;
 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
 *
 * so the bit of reg_addr is to control pin offset's GPDR feature
*/

enum GPIO_REG {
	GPLR = 0,	/* pin level read-only */
	GPDR,		/* pin direction */
	GPSR,		/* pin set */
	GPCR,		/* pin clear */
	GRER,		/* rising edge detect */
	GFER,		/* falling edge detect */
	GEDR,		/* edge detect result */
	GAFR,		/* alt function */
	GFBR = 9,	/* glitch filter bypas */
	GPIT,		/* interrupt type */
	GPIP = GFER,	/* level interrupt polarity */
	GPIM = GRER,	/* level interrupt mask */

	/* the following registers only exist on MRFLD */
	GFBR_TNG = 6,
	GIMR,		/* interrupt mask */
	GISR,		/* interrupt source */
	GITR = 32,	/* interrupt type */
	GLPR = 33,	/* level-input polarity */
};

enum GPIO_CONTROLLERS {
	LINCROFT_GPIO,
	PENWELL_GPIO_AON,
	PENWELL_GPIO_CORE,
	CLOVERVIEW_GPIO_AON,
	CLOVERVIEW_GPIO_CORE,
	TANGIER_GPIO,
};

/* langwell gpio driver data */
struct lnw_gpio_ddata_t {
	u16 ngpio;		/* number of gpio pins */
	u32 gplr_offset;	/* offset of first GPLR register from base */
	u32 (*get_flis_offset)(int gpio);
	u32 chip_irq_type;	/* chip interrupt type */
};

struct gpio_flis_pair {
	int gpio;	/* gpio number */
	int offset;	/* register offset from FLIS base */
};

/*
 * The following mapping table lists the pin and flis offset pair
 * of some key gpio pins, the offset of other gpios can be calculated
 * from the table.
 */
static struct gpio_flis_pair gpio_flis_mapping_table[] = {
	{ 0,	0x2900 },
	{ 12,	0x2544 },
	{ 14,	0x0958 },
	{ 16,	0x2D18 },
	{ 17,	0x1D10 },
	{ 19,	0x1D00 },
	{ 23,	0x1D18 },
	{ 31,	-EINVAL }, /* No GPIO 31 in pin list */
	{ 32,	0x1508 },
	{ 44,	0x3500 },
	{ 64,	0x2534 },
	{ 68,	0x2D1C },
	{ 70,	0x1500 },
	{ 72,	0x3D00 },
	{ 77,	0x0D00 },
	{ 97,	0x0954 },
	{ 98,	-EINVAL }, /* No GPIO 98-101 in pin list */
	{ 102,	0x1910 },
	{ 120,	0x1900 },
	{ 124,	0x2100 },
	{ 136,	-EINVAL }, /* No GPIO 136 in pin list */
	{ 137,	0x2D00 },
	{ 143,	-EINVAL }, /* No GPIO 143-153 in pin list */
	{ 154,	0x092C },
	{ 164,	0x3900 },
	{ 177,	0x2500 },
	{ 190,	0x2D50 },
};

/*
 * In new version of FW for Merrifield, I2C FLIS register can not
 * be written directly but go though a IPC way which is sleepable,
 * so we shouldn't use spin_lock_irq to protect the access when
 * is_merr_i2c_flis() return true.
 */
static inline bool is_merr_i2c_flis(u32 offset)
{
	return ((offset >= TANGIER_I2C_FLIS_START)
		&& (offset <= TANGIER_I2C_FLIS_END));
}

static u32 get_flis_offset_by_gpio(int gpio)
{
	int i;
	int start;
	u32 offset = -EINVAL;

	for (i = 0; i < ARRAY_SIZE(gpio_flis_mapping_table) - 1; i++) {
		if (gpio >= gpio_flis_mapping_table[i].gpio
			&& gpio < gpio_flis_mapping_table[i + 1].gpio)
			break;
	}

	start = gpio_flis_mapping_table[i].gpio;

	if (gpio_flis_mapping_table[i].offset != -EINVAL) {
		offset = gpio_flis_mapping_table[i].offset
				+ (gpio - start) * 4;
	}

	return offset;
}

static struct lnw_gpio_ddata_t lnw_gpio_ddata[] = {
	[LINCROFT_GPIO] = {
		.ngpio = 64,
	},
	[PENWELL_GPIO_AON] = {
		.ngpio = 96,
		.chip_irq_type = IRQ_TYPE_EDGE,
	},
	[PENWELL_GPIO_CORE] = {
		.ngpio = 96,
		.chip_irq_type = IRQ_TYPE_EDGE,
	},
	[CLOVERVIEW_GPIO_AON] = {
		.ngpio = 96,
		.chip_irq_type = IRQ_TYPE_EDGE | IRQ_TYPE_LEVEL,
	},
	[CLOVERVIEW_GPIO_CORE] = {
		.ngpio = 96,
		.chip_irq_type = IRQ_TYPE_EDGE,
	},
	[TANGIER_GPIO] = {
		.ngpio = 192,
		.gplr_offset = 4,
		.get_flis_offset = get_flis_offset_by_gpio,
		.chip_irq_type = IRQ_TYPE_EDGE | IRQ_TYPE_LEVEL,
	},
};

struct lnw_gpio {
	struct gpio_chip	chip;
	void			*reg_base;
	void			*reg_gplr;
	spinlock_t		lock;
	struct pci_dev		*pdev;
	struct irq_domain	*domain;
	u32			(*get_flis_offset)(int gpio);
	u32			chip_irq_type;
	int			type;
	struct gpio_debug	*debug;
};

#define to_lnw_priv(chip)	container_of(chip, struct lnw_gpio, chip)

static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
			enum GPIO_REG reg_type)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	unsigned nreg = chip->ngpio / 32;
	u8 reg = offset / 32;
	void __iomem *ptr;
	void *base;

	/**
	 * On TNG B0, GITR[0]'s address is 0xFF008300, while GPLR[0]'s address
	 * is 0xFF008004. To count GITR[0]'s address, it's easier to count
	 * from 0xFF008000. So for GITR,GLPR... we switch the base to reg_base.
	 * This does not affect PNW/CLV, since the reg_gplr is the reg_base,
	 * while on TNG, the reg_gplr has an offset of 0x4.
	 */
	base = reg_type < GITR ? lnw->reg_gplr : lnw->reg_base;
	ptr = (void __iomem *)(base + reg_type * nreg * 4 + reg * 4);
	return ptr;
}

void lnw_gpio_set_alt(int gpio, int alt)
{
	struct lnw_gpio *lnw;
	u32 __iomem *mem;
	int reg;
	int bit;
	u32 offset;
	u32 value;
	unsigned long flags;

	/* use this trick to get memio */
	lnw = irq_get_chip_data(gpio_to_irq(gpio));
	if (!lnw) {
		pr_err("langwell_gpio: can not find pin %d\n", gpio);
		return;
	}
	if (gpio < lnw->chip.base || gpio >= lnw->chip.base + lnw->chip.ngpio) {
		dev_err(lnw->chip.dev, "langwell_gpio: wrong pin %d to config alt\n", gpio);
		return;
	}
#if 0
	if (lnw->irq_base + gpio - lnw->chip.base != gpio_to_irq(gpio)) {
		dev_err(lnw->chip.dev, "langwell_gpio: wrong chip data for pin %d\n", gpio);
		return;
	}
#endif
	gpio -= lnw->chip.base;

	if (lnw->type != TANGIER_GPIO) {
		reg = gpio / 16;
		bit = gpio % 16;

		mem = gpio_reg(&lnw->chip, 0, GAFR);
		spin_lock_irqsave(&lnw->lock, flags);
		value = readl(mem + reg);
		value &= ~(3 << (bit * 2));
		value |= (alt & 3) << (bit * 2);
		writel(value, mem + reg);
		spin_unlock_irqrestore(&lnw->lock, flags);
		dev_dbg(lnw->chip.dev, "ALT: writing 0x%x to %p\n",
			value, mem + reg);
	} else {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return;

		if (!is_merr_i2c_flis(offset))
			spin_lock_irqsave(&lnw->lock, flags);

		value = get_flis_value(offset);
		value &= ~7;
		value |= (alt & 7);
		set_flis_value(value, offset);

		if (!is_merr_i2c_flis(offset))
			spin_unlock_irqrestore(&lnw->lock, flags);
	}
}
EXPORT_SYMBOL_GPL(lnw_gpio_set_alt);

int gpio_get_alt(int gpio)
{
	struct lnw_gpio *lnw;
	u32 __iomem *mem;
	int reg;
	int bit;
	u32 value;
	u32 offset;

	 /* use this trick to get memio */
	lnw = irq_get_chip_data(gpio_to_irq(gpio));
	if (!lnw) {
		pr_err("langwell_gpio: can not find pin %d\n", gpio);
		return -1;
	}
	if (gpio < lnw->chip.base || gpio >= lnw->chip.base + lnw->chip.ngpio) {
		dev_err(lnw->chip.dev,
			"langwell_gpio: wrong pin %d to config alt\n", gpio);
		return -1;
	}
#if 0
	if (lnw->irq_base + gpio - lnw->chip.base != gpio_to_irq(gpio)) {
		dev_err(lnw->chip.dev,
			"langwell_gpio: wrong chip data for pin %d\n", gpio);
		return -1;
	}
#endif
	gpio -= lnw->chip.base;

	if (lnw->type != TANGIER_GPIO) {
		reg = gpio / 16;
		bit = gpio % 16;

		mem = gpio_reg(&lnw->chip, 0, GAFR);
		value = readl(mem + reg);
		value &= (3 << (bit * 2));
		value >>= (bit * 2);
	} else {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -EINVAL;

		value = get_flis_value(offset) & 7;
	}

	return value;
}
EXPORT_SYMBOL_GPL(gpio_get_alt);

static int lnw_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
				 unsigned debounce)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	void __iomem *gfbr;
	unsigned long flags;
	u32 value;
	enum GPIO_REG reg_type;

	reg_type = (lnw->type == TANGIER_GPIO) ? GFBR_TNG : GFBR;
	gfbr = gpio_reg(chip, offset, reg_type);

	if (lnw->pdev)
		pm_runtime_get(&lnw->pdev->dev);

	spin_lock_irqsave(&lnw->lock, flags);
	value = readl(gfbr);
	if (debounce) {
		/* debounce bypass disable */
		value &= ~BIT(offset % 32);
	} else {
		/* debounce bypass enable */
		value |= BIT(offset % 32);
	}
	writel(value, gfbr);
	spin_unlock_irqrestore(&lnw->lock, flags);

	if (lnw->pdev)
		pm_runtime_put(&lnw->pdev->dev);

	return 0;
}

static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
				   enum GPIO_REG reg_type)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	unsigned nreg = chip->ngpio / 32;
	u8 reg = offset / 16;
	void __iomem *ptr;

	ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
	return ptr;
}

static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	u32 value;
	void __iomem *gafr;
	int shift, af;

	if (lnw->type > CLOVERVIEW_GPIO_CORE)
		return 0;

	gafr = gpio_reg_2bit(chip, offset, GAFR);
	value = readl(gafr);
	shift = (offset % 16) << 1;
	af = (value >> shift) & 3;

	if (af) {
		value &= ~(3 << shift);
		writel(value, gafr);
	}
	return 0;
}

static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
{
	void __iomem *gplr = gpio_reg(chip, offset, GPLR);

	return readl(gplr) & BIT(offset % 32);
}

static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	void __iomem *gpsr, *gpcr;

	if (value) {
		gpsr = gpio_reg(chip, offset, GPSR);
		writel(BIT(offset % 32), gpsr);
	} else {
		gpcr = gpio_reg(chip, offset, GPCR);
		writel(BIT(offset % 32), gpcr);
	}
}

static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
	u32 value;
	unsigned long flags;

	if (lnw->pdev)
		pm_runtime_get(&lnw->pdev->dev);

	spin_lock_irqsave(&lnw->lock, flags);
	value = readl(gpdr);
	value &= ~BIT(offset % 32);
	writel(value, gpdr);
	spin_unlock_irqrestore(&lnw->lock, flags);

	if (lnw->pdev)
		pm_runtime_put(&lnw->pdev->dev);

	return 0;
}

static int lnw_gpio_direction_output(struct gpio_chip *chip,
			unsigned offset, int value)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
	unsigned long flags;

	lnw_gpio_set(chip, offset, value);

	if (lnw->pdev)
		pm_runtime_get(&lnw->pdev->dev);

	spin_lock_irqsave(&lnw->lock, flags);
	value = readl(gpdr);
	value |= BIT(offset % 32);
	writel(value, gpdr);
	spin_unlock_irqrestore(&lnw->lock, flags);

	if (lnw->pdev)
		pm_runtime_put(&lnw->pdev->dev);

	return 0;
}

static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
{
	struct lnw_gpio *lnw = to_lnw_priv(chip);
	return irq_create_mapping(lnw->domain, offset);
}

static int lnw_irq_type(struct irq_data *d, unsigned type)
{
	struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
	u32 gpio = irqd_to_hwirq(d);
	unsigned long flags;
	u32 value;
	int ret = 0;
	void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
	void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
	void __iomem *gpit, *gpip;

	if (gpio >= lnw->chip.ngpio)
		return -EINVAL;

	if (lnw->pdev)
		pm_runtime_get(&lnw->pdev->dev);

	/* Chip that supports level interrupt has extra GPIT registers */
	if (lnw->chip_irq_type & IRQ_TYPE_LEVEL) {
		switch (lnw->type) {
		case CLOVERVIEW_GPIO_AON:
			gpit = gpio_reg(&lnw->chip, gpio, GPIT);
			gpip = gpio_reg(&lnw->chip, gpio, GPIP);
			break;
		case TANGIER_GPIO:
			gpit = gpio_reg(&lnw->chip, gpio, GITR);
			gpip = gpio_reg(&lnw->chip, gpio, GLPR);
			break;
		default:
			ret = -EINVAL;
			goto out;
		}

		spin_lock_irqsave(&lnw->lock, flags);
		if (type & IRQ_TYPE_LEVEL_MASK) {
			/* To prevent glitches from triggering an unintended
			 * level interrupt, configure GLPR register first
			 * and then configure GITR.
			 */
			if (type & IRQ_TYPE_LEVEL_LOW)
				value = readl(gpip) | BIT(gpio % 32);
			else
				value = readl(gpip) & (~BIT(gpio % 32));
			writel(value, gpip);

			value = readl(gpit) | BIT(gpio % 32);
			writel(value, gpit);

			irq_set_handler_locked(d, handle_level_irq);
		} else if (type & IRQ_TYPE_EDGE_BOTH) {
			value = readl(gpit) & (~BIT(gpio % 32));
			writel(value, gpit);

			if (type & IRQ_TYPE_EDGE_RISING)
				value = readl(grer) | BIT(gpio % 32);
			else
				value = readl(grer) & (~BIT(gpio % 32));
			writel(value, grer);

			if (type & IRQ_TYPE_EDGE_FALLING)
				value = readl(gfer) | BIT(gpio % 32);
			else
				value = readl(gfer) & (~BIT(gpio % 32));
			writel(value, gfer);

			irq_set_handler_locked(d, handle_edge_irq);
		}
		spin_unlock_irqrestore(&lnw->lock, flags);
	} else {
		if (type & IRQ_TYPE_LEVEL_MASK) {
			ret = -EINVAL;
		} else if (type & IRQ_TYPE_EDGE_BOTH) {
			spin_lock_irqsave(&lnw->lock, flags);

			if (type & IRQ_TYPE_EDGE_RISING)
				value = readl(grer) | BIT(gpio % 32);
			else
				value = readl(grer) & (~BIT(gpio % 32));
			writel(value, grer);

			if (type & IRQ_TYPE_EDGE_FALLING)
				value = readl(gfer) | BIT(gpio % 32);
			else
				value = readl(gfer) & (~BIT(gpio % 32));
			writel(value, gfer);

			spin_unlock_irqrestore(&lnw->lock, flags);
		}
	}

out:
	if (lnw->pdev)
		pm_runtime_put(&lnw->pdev->dev);

	return ret;
}

static int lnw_set_maskunmask(struct irq_data *d, enum GPIO_REG reg_type,
				unsigned unmask)
{
	struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
	u32 gpio = irqd_to_hwirq(d);
	unsigned long flags;
	u32 value;
	void __iomem *gp_reg;

	gp_reg = gpio_reg(&lnw->chip, gpio, reg_type);

	spin_lock_irqsave(&lnw->lock, flags);

	if (unmask) {
		/* enable interrupt from GPIO input pin */
		value = readl(gp_reg) | BIT(gpio % 32);
	} else {
		/* disable interrupt from GPIO input pin */
		value = readl(gp_reg) & (~BIT(gpio % 32));
	}

	writel(value, gp_reg);

	spin_unlock_irqrestore(&lnw->lock, flags);

	return 0;
}

static void lnw_irq_unmask(struct irq_data *d)
{
	struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
	u32 gpio = irqd_to_hwirq(d);
	void __iomem *gpit;

	if (gpio >= lnw->chip.ngpio)
		return;

	switch (lnw->type) {
	case CLOVERVIEW_GPIO_AON:
		gpit = gpio_reg(&lnw->chip, gpio, GPIT);

		/* if it's level trigger, unmask GPIM */
		if (readl(gpit) & BIT(gpio % 32))
			lnw_set_maskunmask(d, GPIM, 1);

		break;
	case TANGIER_GPIO:
		lnw_set_maskunmask(d, GIMR, 1);
		break;
	default:
		break;
	}
}

static void lnw_irq_mask(struct irq_data *d)
{
	struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
	u32 gpio = irqd_to_hwirq(d);
	void __iomem *gpit;

	if (gpio >= lnw->chip.ngpio)
		return;

	switch (lnw->type) {
	case CLOVERVIEW_GPIO_AON:
		gpit = gpio_reg(&lnw->chip, gpio, GPIT);

		/* if it's level trigger, mask GPIM */
		if (readl(gpit) & BIT(gpio % 32))
			lnw_set_maskunmask(d, GPIM, 0);

		break;
	case TANGIER_GPIO:
		lnw_set_maskunmask(d, GIMR, 0);
		break;
	default:
		break;
	}
}

static int lwn_irq_set_wake(struct irq_data *d, unsigned on)
{
	return 0;
}

static void lnw_irq_ack(struct irq_data *d)
{
}

static void lnw_irq_shutdown(struct irq_data *d)
{
	struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
	u32 gpio = irqd_to_hwirq(d);
	unsigned long flags;
	u32 value;
	void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
	void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);

	spin_lock_irqsave(&lnw->lock, flags);
	value = readl(grer) & (~BIT(gpio % 32));
	writel(value, grer);
	value = readl(gfer) & (~BIT(gpio % 32));
	writel(value, gfer);
	spin_unlock_irqrestore(&lnw->lock, flags);
};


static struct irq_chip lnw_irqchip = {
	.name		= "LNW-GPIO",
	.flags		= IRQCHIP_SET_TYPE_MASKED,
	.irq_mask	= lnw_irq_mask,
	.irq_unmask	= lnw_irq_unmask,
	.irq_set_type	= lnw_irq_type,
	.irq_set_wake	= lwn_irq_set_wake,
	.irq_ack	= lnw_irq_ack,
	.irq_shutdown	= lnw_irq_shutdown,
};

static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = {   /* pin number */
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
		.driver_data = LINCROFT_GPIO },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
		.driver_data = PENWELL_GPIO_AON },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
		.driver_data = PENWELL_GPIO_CORE },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
		.driver_data = CLOVERVIEW_GPIO_AON },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
		.driver_data = CLOVERVIEW_GPIO_CORE },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
		.driver_data = TANGIER_GPIO },
	{ 0, }
};
MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);

static void lnw_irq_handler(struct irq_desc *desc)
{
        struct gpio_chip *gc = irq_desc_get_handler_data(desc);
        struct lnw_gpio *lnw = container_of(gc, struct lnw_gpio, chip);
	struct irq_data *data = irq_desc_get_irq_data(desc);
	struct irq_chip *chip = irq_data_get_irq_chip(data);
	struct gpio_debug *debug;
	u32 base, gpio, mask;
	unsigned long pending;
	void __iomem *gp_reg;
	enum GPIO_REG reg_type;
	struct irq_desc *lnw_irq_desc;
	unsigned int lnw_irq;

	pr_debug("%s: lnw->chip.ngpio=0x%u\n", __func__, lnw->chip.ngpio);
	debug = lnw->debug;

	reg_type = (lnw->type == TANGIER_GPIO) ? GISR : GEDR;

	/* check GPIO controller to check which pin triggered the interrupt */
	for (base = 0; base < lnw->chip.ngpio; base += 32) {
		gp_reg = gpio_reg(&lnw->chip, base, reg_type);
		while ((pending = (lnw->type != TANGIER_GPIO) ?
			readl(gp_reg) :
			(readl(gp_reg) &
			readl(gpio_reg(&lnw->chip, base, GIMR))))) {
			gpio = __ffs(pending);
			DEFINE_DEBUG_IRQ_CONUNT_INCREASE(lnw->chip.base +
				base + gpio);
			/* Mask irq if not requested in kernel */
			lnw_irq = irq_find_mapping(lnw->domain, base + gpio);
			pr_debug("%s: gpio=%u, lnw_irq=%u\n",
				__func__, gpio, lnw_irq);
			lnw_irq_desc = irq_to_desc(lnw_irq);
			if (lnw_irq_desc && unlikely(!lnw_irq_desc->action)) {
				lnw_irq_mask(&lnw_irq_desc->irq_data);
				continue;
			}

			mask = BIT(gpio);
			pr_debug("%s: mask=%u\n", __func__, mask);
			/* Clear before handling so we can't lose an edge */
			writel(mask, gp_reg);
			generic_handle_irq(lnw_irq);
		}
	}

	chip->irq_eoi(data);
}

static char conf_reg_msg[] =
	"\nGPIO configuration register:\n"
	"\t[ 2: 0]\tpinmux\n"
	"\t[ 6: 4]\tpull strength\n"
	"\t[ 8: 8]\tpullup enable\n"
	"\t[ 9: 9]\tpulldown enable\n"
	"\t[10:10]\tslew A, B setting\n"
	"\t[12:12]\toverride input enable\n"
	"\t[13:13]\toverride input enable enable\n"
	"\t[14:14]\toverride output enable\n"
	"\t[15:15]\toverride output enable enable\n"
	"\t[16:16]\toverride input value\n"
	"\t[17:17]\tenable input data override\n"
	"\t[18:18]\toverride output value\n"
	"\t[19:19]\tenable output data override\n"
	"\t[21:21]\topen drain enable\n"
	"\t[22:22]\tenable OVR_IOSTBY_VAL\n"
	"\t[23:23]\tOVR_IOSTBY_VAL\n"
	"\t[24:24]\tSBY_OUTDATAOV_EN\n"
	"\t[25:25]\tSBY_INDATAOV_EN\n"
	"\t[26:26]\tSBY_OVOUTEN_EN\n"
	"\t[27:27]\tSBY_OVINEN_EN\n"
	"\t[29:28]\tstandby pullmode\n"
	"\t[30:30]\tstandby open drain mode\n";

static char *pinvalue[] = {"low", "high"};
static char *pindirection[] = {"in", "out"};
static char *irqtype[] = {"irq_none", "edge_rising", "edge_falling",
			"edge_both"};
static char *pinmux[] = {"mode0", "mode1", "mode2", "mode3", "mode4", "mode5",
			"mode6", "mode7"};
static char *pullmode[] = {"nopull", "pullup", "pulldown"};
static char *pullstrength[] = {"2k", "20k", "50k", "910ohms"};
static char *enable[] = {"disable", "enable"};
static char *override_direction[] = {"no-override", "override-enable",
			"override-disable"};
static char *override_value[] = {"no-override", "override-high",
			"override-low"};
static char *standby_trigger[] = {"no-override", "override-trigger",
			"override-notrigger"};
static char *standby_pupd_state[] = {"keep", "pulldown", "pullup", "nopull"};

static int gpio_get_pinvalue(struct gpio_control *control, void *private_data,
		unsigned gpio)
{
	struct lnw_gpio *lnw = private_data;
	u32 value;

	value = lnw_gpio_get(&lnw->chip, gpio);

	return value ? 1 : 0;
}

static int gpio_set_pinvalue(struct gpio_control *control, void *private_data,
		unsigned gpio, unsigned int num)
{
	struct lnw_gpio *lnw = private_data;

	lnw_gpio_set(&lnw->chip, gpio, num);
	return 0;
}

static int gpio_get_normal(struct gpio_control *control, void *private_data,
		unsigned gpio)
{
	struct lnw_gpio *lnw = private_data;
	u32 __iomem *mem;
	u32 value;

	mem = gpio_reg(&lnw->chip, gpio, control->reg);

	value = readl(mem);
	value &= BIT(gpio % 32);

	if (control->invert)
		return value ? 0 : 1;
	else
		return value ? 1 : 0;
}

static int gpio_set_normal(struct gpio_control *control, void *private_data,
		unsigned gpio, unsigned int num)
{
	struct lnw_gpio *lnw = private_data;
	u32 __iomem *mem;
	u32 value;
	unsigned long flags;

	mem = gpio_reg(&lnw->chip, gpio, control->reg);

	spin_lock_irqsave(&lnw->lock, flags);
	value = readl(mem);
	value &= ~BIT(gpio % 32);
	if (control->invert) {
		if (num)
			value &= ~BIT(gpio % 32);
		else
			value |= BIT(gpio % 32);
	} else {
		if (num)
			value |= BIT(gpio % 32);
		else
			value &= ~BIT(gpio % 32);
	}
	writel(value, mem);
	spin_unlock_irqrestore(&lnw->lock, flags);

	return 0;
}

static int gpio_get_irqtype(struct gpio_control *control, void *private_data,
		unsigned gpio)
{
	struct lnw_gpio *lnw = private_data;
	void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
	void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
	u32 value;
	int num;

	value = readl(grer) & BIT(gpio % 32);
	num = value ? 1 : 0;
	value = readl(gfer) & BIT(gpio % 32);
	if (num)
		num = value ? 3 : 1;
	else
		num = value ? 2 : 0;

	return num;
}

static int flis_get_normal(struct gpio_control *control, void *private_data,
		unsigned gpio)
{
	struct lnw_gpio *lnw = private_data;
	u32 offset, value;
	int num;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -1;

		value = get_flis_value(offset);
		num = (value >> control->shift) & control->mask;
		if (num < control->num)
			return num;
	}

	return -1;
}

static int flis_set_normal(struct gpio_control *control, void *private_data,
		unsigned gpio, unsigned int num)
{
	struct lnw_gpio *lnw = private_data;
	u32 shift = control->shift;
	u32 mask = control->mask;
	u32 offset, value;
	unsigned long flags;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -1;

		if (!is_merr_i2c_flis(offset))
			spin_lock_irqsave(&lnw->lock, flags);
		value = get_flis_value(offset);
		value &= ~(mask << shift);
		value |= ((num & mask) << shift);
		set_flis_value(value, offset);
		if (!is_merr_i2c_flis(offset))
			spin_unlock_irqrestore(&lnw->lock, flags);
		return 0;
	}

	return -1;
}

static int flis_get_override(struct gpio_control *control, void *private_data,
		unsigned gpio)
{
	struct lnw_gpio *lnw = private_data;
	u32 offset, value;
	u32 val_bit, en_bit;
	int num;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -1;

		val_bit = 1 << control->shift;
		en_bit = 1 << control->rshift;

		value = get_flis_value(offset);

		if (value & en_bit)
			if (value & val_bit)
				num = 1;
			else
				num = 2;
		else
			num = 0;

		return num;
	}

	return -1;
}

static int flis_set_override(struct gpio_control *control, void *private_data,
		unsigned gpio, unsigned int num)
{
	struct lnw_gpio *lnw = private_data;
	u32 offset, value;
	u32 val_bit, en_bit;
	unsigned long flags;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -1;

		val_bit = 1 << control->shift;
		en_bit = 1 << control->rshift;

		if (!is_merr_i2c_flis(offset))
			spin_lock_irqsave(&lnw->lock, flags);
		value = get_flis_value(offset);
		switch (num) {
		case 0:
			value &= ~(en_bit | val_bit);
			break;
		case 1:
			value |= (en_bit | val_bit);
			break;
		case 2:
			value |= en_bit;
			value &= ~val_bit;
			break;
		default:
			break;
		}
		set_flis_value(value, offset);
		if (!is_merr_i2c_flis(offset))
			spin_unlock_irqrestore(&lnw->lock, flags);

		return 0;
	}

	return -1;
}

#define GPIO_VALUE_CONTROL(xtype, xinfo, xnum) \
{	.type = xtype, .pininfo = xinfo, .num = xnum, \
	.get = gpio_get_pinvalue, .set = gpio_set_pinvalue}
#define GPIO_NORMAL_CONTROL(xtype, xinfo, xnum, xreg, xinvert) \
{	.type = xtype, .pininfo = xinfo, .num = xnum, .reg = xreg, \
	.invert = xinvert, .get = gpio_get_normal, .set = gpio_set_normal}
#define GPIO_IRQTYPE_CONTROL(xtype, xinfo, xnum) \
{	.type = xtype, .pininfo = xinfo, .num = xnum, \
	.get = gpio_get_irqtype, .set = NULL}
#define FLIS_NORMAL_CONTROL(xtype, xinfo, xnum, xshift, xmask) \
{	.type = xtype, .pininfo = xinfo, .num = xnum, .shift = xshift, \
	.mask = xmask, .get = flis_get_normal, .set = flis_set_normal}
#define FLIS_OVERRIDE_CONTROL(xtype, xinfo, xnum, xshift, xrshift) \
{	.type = xtype, .pininfo = xinfo, .num = xnum, .shift = xshift, \
	.rshift = xrshift, .get = flis_get_override, .set = flis_set_override}

static struct gpio_control lnw_gpio_controls[] = {
GPIO_VALUE_CONTROL(TYPE_PIN_VALUE, pinvalue, 2),
GPIO_NORMAL_CONTROL(TYPE_DIRECTION, pindirection, 2, GPDR, 0),
GPIO_IRQTYPE_CONTROL(TYPE_IRQ_TYPE, irqtype, 4),
GPIO_NORMAL_CONTROL(TYPE_DEBOUNCE, enable, 2, GFBR_TNG, 1),
FLIS_NORMAL_CONTROL(TYPE_PINMUX, pinmux, 8, 0, 0x7),
FLIS_NORMAL_CONTROL(TYPE_PULLSTRENGTH, pullstrength, 4, 4, 0x7),
FLIS_NORMAL_CONTROL(TYPE_PULLMODE, pullmode, 3, 8, 0x3),
FLIS_NORMAL_CONTROL(TYPE_OPEN_DRAIN, enable, 2, 21, 0x1),
FLIS_OVERRIDE_CONTROL(TYPE_OVERRIDE_INDIR, override_direction, 3, 12, 13),
FLIS_OVERRIDE_CONTROL(TYPE_OVERRIDE_OUTDIR, override_direction, 3, 14, 15),
FLIS_OVERRIDE_CONTROL(TYPE_OVERRIDE_INVAL, override_value, 3, 16, 17),
FLIS_OVERRIDE_CONTROL(TYPE_OVERRIDE_OUTVAL, override_value, 3, 18, 19),
FLIS_OVERRIDE_CONTROL(TYPE_SBY_OVR_IO, standby_trigger, 3, 23, 22),
FLIS_OVERRIDE_CONTROL(TYPE_SBY_OVR_OUTVAL, override_value, 3, 18, 24),
FLIS_OVERRIDE_CONTROL(TYPE_SBY_OVR_INVAL, override_value, 3, 16, 25),
FLIS_OVERRIDE_CONTROL(TYPE_SBY_OVR_OUTDIR, override_direction, 3, 14, 26),
FLIS_OVERRIDE_CONTROL(TYPE_SBY_OVR_INDIR, override_direction, 3, 12, 27),
FLIS_NORMAL_CONTROL(TYPE_SBY_PUPD_STATE, standby_pupd_state, 4, 28, 0x3),
FLIS_NORMAL_CONTROL(TYPE_SBY_OD_DIS, enable, 2, 30, 0x1),
};

static unsigned int lnw_get_conf_reg(struct gpio_debug *debug, unsigned gpio)
{
	struct lnw_gpio *lnw = debug->private_data;
	u32 offset, value = 0;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return -EINVAL;

		value = get_flis_value(offset);
	}

	return value;
}

static void lnw_set_conf_reg(struct gpio_debug *debug, unsigned gpio,
		unsigned int value)
{
	struct lnw_gpio *lnw = debug->private_data;
	u32 offset;

	if (lnw->type == TANGIER_GPIO) {
		offset = lnw->get_flis_offset(gpio);
		if (WARN(offset == -EINVAL, "invalid pin %d\n", gpio))
			return;

		set_flis_value(value, offset);
	}

	return;
}

static char **lnw_get_avl_pininfo(struct gpio_debug *debug, unsigned gpio,
		unsigned int type, unsigned *num)
{
	struct gpio_control *control;

	control = find_gpio_control(lnw_gpio_controls,
			ARRAY_SIZE(lnw_gpio_controls), type);
	if (control == NULL)
		return NULL;

	*num = control->num;

	return control->pininfo;
}

static char *lnw_get_cul_pininfo(struct gpio_debug *debug, unsigned gpio,
		unsigned int type)
{
	struct lnw_gpio *lnw = debug->private_data;
	struct gpio_control *control;
	int num;

	control = find_gpio_control(lnw_gpio_controls,
			ARRAY_SIZE(lnw_gpio_controls), type);
	if (control == NULL)
		return NULL;

	num = control->get(control, lnw, gpio);
	if (num == -1)
		return NULL;

	return *(control->pininfo + num);
}

static void lnw_set_pininfo(struct gpio_debug *debug, unsigned gpio,
		unsigned int type, const char *info)
{
	struct lnw_gpio *lnw = debug->private_data;
	struct gpio_control *control;
	int num;

	control = find_gpio_control(lnw_gpio_controls,
			ARRAY_SIZE(lnw_gpio_controls), type);
	if (control == NULL)
		return;

	num = find_pininfo_num(control, info);
	if (num == -1)
		return;

	if (control->set)
		control->set(control, lnw, gpio, num);
}

static int lnw_get_register_msg(char **buf, unsigned long *size)
{
	*buf = conf_reg_msg;
	*size = strlen(conf_reg_msg);

	return 0;
}

static struct gpio_debug_ops lnw_gpio_debug_ops = {
	.get_conf_reg = lnw_get_conf_reg,
	.set_conf_reg = lnw_set_conf_reg,
	.get_avl_pininfo = lnw_get_avl_pininfo,
	.get_cul_pininfo = lnw_get_cul_pininfo,
	.set_pininfo = lnw_set_pininfo,
	.get_register_msg = lnw_get_register_msg,
};

static void lnw_irq_init_hw(struct lnw_gpio *lnw)
{
	void __iomem *reg;
	unsigned base;

	for (base = 0; base < lnw->chip.ngpio; base += 32) {
		/* Clear the rising-edge detect register */
		reg = gpio_reg(&lnw->chip, base, GRER);
		writel(0, reg);
		/* Clear the falling-edge detect register */
		reg = gpio_reg(&lnw->chip, base, GFER);
		writel(0, reg);
		/* Clear the edge detect status register */
		reg = gpio_reg(&lnw->chip, base, GEDR);
		writel(~0, reg);
	}
}

static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
			    irq_hw_number_t hw)
{
	struct lnw_gpio *lnw = d->host_data;

	irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
				      "demux");
	irq_set_chip_data(virq, lnw);
	irq_set_irq_type(virq, IRQ_TYPE_NONE);

	return 0;
}

static const struct irq_domain_ops lnw_gpio_irq_ops = {
	.map = lnw_gpio_irq_map,
	.xlate = irq_domain_xlate_twocell,
};

static int lnw_gpio_runtime_resume(struct device *dev)
{
	return 0;
}

static int lnw_gpio_runtime_suspend(struct device *dev)
{
	return 0;
}

static int lnw_gpio_runtime_idle(struct device *dev)
{
	int err = pm_schedule_suspend(dev, 500);

	if (!err)
		return 0;

	return -EBUSY;
}

static const struct dev_pm_ops lnw_gpio_pm_ops = {
	SET_RUNTIME_PM_OPS(lnw_gpio_runtime_suspend,
			   lnw_gpio_runtime_resume,
			   lnw_gpio_runtime_idle)
};

static int lnw_gpio_probe(struct pci_dev *pdev,
			const struct pci_device_id *id)
{
	void *base;
	resource_size_t start, len;
	struct lnw_gpio *lnw;
	struct gpio_debug *debug;
	u32 gpio_base;
	u32 irq_base;
	int retval;
	struct lnw_gpio_ddata_t *ddata;
	int pid;

	pr_debug("%s: Entered lnw_gpio_probe()\n", __func__);
	pid = id->driver_data;
	ddata = &lnw_gpio_ddata[pid];

	retval = pci_enable_device(pdev);
	if (retval)
		return retval;

	retval = pci_request_regions(pdev, "langwell_gpio");
	if (retval) {
		dev_err(&pdev->dev, "error requesting resources\n");
		goto err_pci_req_region;
	}
	/* get the gpio_base from bar1 */
	start = pci_resource_start(pdev, 1);
	len = pci_resource_len(pdev, 1);
	base = ioremap_nocache(start, len);
	if (!base) {
		dev_err(&pdev->dev, "error mapping bar1\n");
		retval = -EFAULT;
		goto err_ioremap;
	}
	irq_base = *(u32 *)base;
	gpio_base = *((u32 *)base + 1);
	/* release the IO mapping, since we already get the info from bar1 */
	iounmap(base);
	/* get the register base from bar0 */
	start = pci_resource_start(pdev, 0);
	len = pci_resource_len(pdev, 0);
	base = devm_ioremap_nocache(&pdev->dev, start, len);
	if (!base) {
		dev_err(&pdev->dev, "error mapping bar0\n");
		retval = -EFAULT;
		goto err_ioremap;
	}

	lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL);
	if (!lnw) {
		dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
		retval = -ENOMEM;
		goto err_ioremap;
	}

	lnw->type = pid;
	lnw->reg_base = base;
	lnw->reg_gplr = lnw->reg_base + ddata->gplr_offset;
	lnw->get_flis_offset = ddata->get_flis_offset;
	lnw->chip_irq_type = ddata->chip_irq_type;
	lnw->chip.label = dev_name(&pdev->dev);
	lnw->chip.request = lnw_gpio_request;
	lnw->chip.direction_input = lnw_gpio_direction_input;
	lnw->chip.direction_output = lnw_gpio_direction_output;
	/* set/get pinmux capability ported from 3.10 -> 4.x kernels
	 * to support Intel SFI based platforms 
	 */
	lnw->chip.set_pinmux = lnw_gpio_set_alt;
	lnw->chip.get_pinmux = gpio_get_alt;
	lnw->chip.get = lnw_gpio_get;
	lnw->chip.set = lnw_gpio_set;
	lnw->chip.to_irq = lnw_gpio_to_irq;
	lnw->chip.base = gpio_base;
	lnw->chip.ngpio = ddata->ngpio;
	lnw->chip.can_sleep = 0;
	lnw->chip.set_debounce = lnw_gpio_set_debounce;
	lnw->chip.dev = &pdev->dev;
	lnw->pdev = pdev;

	spin_lock_init(&lnw->lock);

	pci_set_drvdata(pdev, lnw);
	retval = gpiochip_add(&lnw->chip);
	if (retval) {
		dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
		goto err_ioremap;
	}


        retval = gpiochip_irqchip_add(&lnw->chip,
                                      &lnw_irqchip,
                                      irq_base,
                                      handle_simple_irq,
                                      IRQ_TYPE_NONE);
        if (retval) {
                dev_err(&pdev->dev,
                        "could not connect irqchip to gpiochip\n");
                return retval;
        }
	lnw->domain = lnw->chip.irqdomain;

	lnw_irq_init_hw(lnw);
	pr_debug("%s: pdev->irq=%u\n", __func__, pdev->irq);
	pr_debug("%s: lnw->chip.ngpio=0x%u\n", __func__, lnw->chip.ngpio);

        gpiochip_set_chained_irqchip(&lnw->chip,
                                     &lnw_irqchip,
                                     pdev->irq,
                                     lnw_irq_handler);

	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_allow(&pdev->dev);

	/* add for gpiodebug */
	debug = gpio_debug_alloc();
	if (debug) {
		__set_bit(TYPE_OVERRIDE_OUTDIR, debug->typebit);
		__set_bit(TYPE_OVERRIDE_OUTVAL, debug->typebit);
		__set_bit(TYPE_OVERRIDE_INDIR, debug->typebit);
		__set_bit(TYPE_OVERRIDE_INVAL, debug->typebit);
		__set_bit(TYPE_SBY_OVR_IO, debug->typebit);
		__set_bit(TYPE_SBY_OVR_OUTVAL, debug->typebit);
		__set_bit(TYPE_SBY_OVR_INVAL, debug->typebit);
		__set_bit(TYPE_SBY_OVR_OUTDIR, debug->typebit);
		__set_bit(TYPE_SBY_OVR_INDIR, debug->typebit);
		__set_bit(TYPE_SBY_PUPD_STATE, debug->typebit);
		__set_bit(TYPE_SBY_OD_DIS, debug->typebit);

		debug->chip = &lnw->chip;
		debug->ops = &lnw_gpio_debug_ops;
		debug->private_data = lnw;
		lnw->debug = debug;

		retval = gpio_debug_register(debug);
		if (retval) {
			dev_err(&pdev->dev, "langwell gpio_debug_register failed %d\n",
				retval);
			gpio_debug_remove(debug);
		}
	}

	pr_debug("%s: Leaving lnw_gpio_probe()\n", __func__);
	return 0;

err_ioremap:
	pci_release_regions(pdev);
err_pci_req_region:
	pci_disable_device(pdev);
	return retval;
}

static struct pci_driver lnw_gpio_driver = {
	.name		= "langwell_gpio",
	.id_table	= lnw_gpio_ids,
	.probe		= lnw_gpio_probe,
	.driver		= {
		.pm	= &lnw_gpio_pm_ops,
	},
};


static int wp_gpio_probe(struct platform_device *pdev)
{
	struct lnw_gpio *lnw;
	struct gpio_chip *gc;
	struct resource *rc;
	int retval = 0;

	rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!rc)
		return -EINVAL;

	lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
	if (!lnw) {
		dev_err(&pdev->dev,
			"can't allocate whitneypoint_gpio chip data\n");
		return -ENOMEM;
	}
	lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
	if (lnw->reg_base == NULL) {
		retval = -EINVAL;
		goto err_kmalloc;
	}
	spin_lock_init(&lnw->lock);
	gc = &lnw->chip;
	gc->label = dev_name(&pdev->dev);
	gc->owner = THIS_MODULE;
	gc->direction_input = lnw_gpio_direction_input;
	gc->direction_output = lnw_gpio_direction_output;
	gc->get = lnw_gpio_get;
	gc->set = lnw_gpio_set;
	gc->to_irq = NULL;
	gc->base = 0;
	gc->ngpio = 64;
	gc->can_sleep = 0;
	retval = gpiochip_add(gc);
	if (retval) {
		dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
								retval);
		goto err_ioremap;
	}
	platform_set_drvdata(pdev, lnw);
	return 0;
err_ioremap:
	iounmap(lnw->reg_base);
err_kmalloc:
	kfree(lnw);
	return retval;
}

static int wp_gpio_remove(struct platform_device *pdev)
{
	struct lnw_gpio *lnw = platform_get_drvdata(pdev);
	gpiochip_remove(&lnw->chip);
	iounmap(lnw->reg_base);
	kfree(lnw);
	platform_set_drvdata(pdev, NULL);
	return 0;
}

static struct platform_driver wp_gpio_driver = {
	.probe		= wp_gpio_probe,
	.remove		= wp_gpio_remove,
	.driver		= {
		.name	= "wp_gpio",
		.owner	= THIS_MODULE,
	},
};

static int __init lnw_gpio_init(void)
{
	int ret;

	ret =  pci_register_driver(&lnw_gpio_driver);
	if (ret < 0)
		pr_err("%s: Failed to register pci_driver, ret=%d\n", __func__, ret);
	return ret;
}

fs_initcall(lnw_gpio_init);