Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
// SPDX-License-Identifier: ISC
/* Copyright (C) 2022 MediaTek Inc. */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_reserved_mem.h>
#include <linux/of_gpio.h>
#include <linux/iopoll.h>
#include <linux/reset.h>
#include <linux/of_net.h>
#include <linux/clk.h>

#include "mt7915.h"

/* INFRACFG */
#define MT_INFRACFG_CONN2AP_SLPPROT	0x0d0
#define MT_INFRACFG_AP2CONN_SLPPROT	0x0d4

#define MT_INFRACFG_RX_EN_MASK		BIT(16)
#define MT_INFRACFG_TX_RDY_MASK		BIT(4)
#define MT_INFRACFG_TX_EN_MASK		BIT(0)

/* TOP POS */
#define MT_TOP_POS_FAST_CTRL		0x114
#define MT_TOP_POS_FAST_EN_MASK		BIT(3)

#define MT_TOP_POS_SKU			0x21c
#define MT_TOP_POS_SKU_MASK		GENMASK(31, 28)
#define MT_TOP_POS_SKU_ADIE_DBDC_MASK	BIT(2)

enum {
	ADIE_SB,
	ADIE_DBDC
};

static int
mt76_wmac_spi_read(struct mt7915_dev *dev, u8 adie, u32 addr, u32 *val)
{
	int ret;
	u32 cur;

	ret = read_poll_timeout(mt76_rr, cur, !(cur & MT_TOP_SPI_POLLING_BIT),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, MT_TOP_SPI_BUSY_CR(adie));
	if (ret)
		return ret;

	mt76_wr(dev, MT_TOP_SPI_ADDR_CR(adie),
		MT_TOP_SPI_READ_ADDR_FORMAT | addr);
	mt76_wr(dev, MT_TOP_SPI_WRITE_DATA_CR(adie), 0);

	ret = read_poll_timeout(mt76_rr, cur, !(cur & MT_TOP_SPI_POLLING_BIT),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, MT_TOP_SPI_BUSY_CR(adie));
	if (ret)
		return ret;

	*val = mt76_rr(dev, MT_TOP_SPI_READ_DATA_CR(adie));

	return 0;
}

static int
mt76_wmac_spi_write(struct mt7915_dev *dev, u8 adie, u32 addr, u32 val)
{
	int ret;
	u32 cur;

	ret = read_poll_timeout(mt76_rr, cur, !(cur & MT_TOP_SPI_POLLING_BIT),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, MT_TOP_SPI_BUSY_CR(adie));
	if (ret)
		return ret;

	mt76_wr(dev, MT_TOP_SPI_ADDR_CR(adie),
		MT_TOP_SPI_WRITE_ADDR_FORMAT | addr);
	mt76_wr(dev, MT_TOP_SPI_WRITE_DATA_CR(adie), val);

	return read_poll_timeout(mt76_rr, cur, !(cur & MT_TOP_SPI_POLLING_BIT),
				 USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				 dev, MT_TOP_SPI_BUSY_CR(adie));
}

static int
mt76_wmac_spi_rmw(struct mt7915_dev *dev, u8 adie,
		  u32 addr, u32 mask, u32 val)
{
	u32 cur, ret;

	ret = mt76_wmac_spi_read(dev, adie, addr, &cur);
	if (ret)
		return ret;

	cur &= ~mask;
	cur |= val;

	return mt76_wmac_spi_write(dev, adie, addr, cur);
}

static int
mt7986_wmac_adie_efuse_read(struct mt7915_dev *dev, u8 adie,
			    u32 addr, u32 *data)
{
	int ret, temp;
	u32 val, mask;

	ret = mt76_wmac_spi_write(dev, adie, MT_ADIE_EFUSE_CFG,
				  MT_ADIE_EFUSE_CTRL_MASK);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_EFUSE2_CTRL, BIT(30), 0x0);
	if (ret)
		return ret;

	mask = (MT_ADIE_EFUSE_MODE_MASK | MT_ADIE_EFUSE_ADDR_MASK |
		MT_ADIE_EFUSE_KICK_MASK);
	val = FIELD_PREP(MT_ADIE_EFUSE_MODE_MASK, 0) |
	      FIELD_PREP(MT_ADIE_EFUSE_ADDR_MASK, addr) |
	      FIELD_PREP(MT_ADIE_EFUSE_KICK_MASK, 1);
	ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_EFUSE2_CTRL, mask, val);
	if (ret)
		return ret;

	ret = read_poll_timeout(mt76_wmac_spi_read, temp,
				!temp && !FIELD_GET(MT_ADIE_EFUSE_KICK_MASK, val),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, adie, MT_ADIE_EFUSE2_CTRL, &val);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_read(dev, adie, MT_ADIE_EFUSE2_CTRL, &val);
	if (ret)
		return ret;

	if (FIELD_GET(MT_ADIE_EFUSE_VALID_MASK, val) == 1)
		ret = mt76_wmac_spi_read(dev, adie, MT_ADIE_EFUSE_RDATA0,
					 data);

	return ret;
}

static inline void mt76_wmac_spi_lock(struct mt7915_dev *dev)
{
	u32 cur;

	read_poll_timeout(mt76_rr, cur,
			  FIELD_GET(MT_SEMA_RFSPI_STATUS_MASK, cur),
			  1000, 1000 * MSEC_PER_SEC, false, dev,
			  MT_SEMA_RFSPI_STATUS);
}

static inline void mt76_wmac_spi_unlock(struct mt7915_dev *dev)
{
	mt76_wr(dev, MT_SEMA_RFSPI_RELEASE, 1);
}

static u32 mt76_wmac_rmw(void __iomem *base, u32 offset, u32 mask, u32 val)
{
	val |= readl(base + offset) & ~mask;
	writel(val, base + offset);

	return val;
}

static u8 mt7986_wmac_check_adie_type(struct mt7915_dev *dev)
{
	u32 val;

	val = readl(dev->sku + MT_TOP_POS_SKU);

	return FIELD_GET(MT_TOP_POS_SKU_ADIE_DBDC_MASK, val);
}

static int mt7986_wmac_consys_reset(struct mt7915_dev *dev, bool enable)
{
	if (!enable)
		return reset_control_assert(dev->rstc);

	mt76_wmac_rmw(dev->sku, MT_TOP_POS_FAST_CTRL,
		      MT_TOP_POS_FAST_EN_MASK,
		      FIELD_PREP(MT_TOP_POS_FAST_EN_MASK, 0x1));

	return reset_control_deassert(dev->rstc);
}

static int mt7986_wmac_gpio_setup(struct mt7915_dev *dev)
{
	struct pinctrl_state *state;
	struct pinctrl *pinctrl;
	int ret;
	u8 type;

	type = mt7986_wmac_check_adie_type(dev);
	pinctrl = devm_pinctrl_get(dev->mt76.dev);
	if (IS_ERR(pinctrl))
		return PTR_ERR(pinctrl);

	switch (type) {
	case ADIE_SB:
		state = pinctrl_lookup_state(pinctrl, "default");
		if (IS_ERR_OR_NULL(state))
			return -EINVAL;
		break;
	case ADIE_DBDC:
		state = pinctrl_lookup_state(pinctrl, "dbdc");
		if (IS_ERR_OR_NULL(state))
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	ret = pinctrl_select_state(pinctrl, state);
	if (ret)
		return ret;

	usleep_range(500, 1000);

	return 0;
}

static int mt7986_wmac_consys_lockup(struct mt7915_dev *dev, bool enable)
{
	int ret;
	u32 cur;

	mt76_wmac_rmw(dev->dcm, MT_INFRACFG_AP2CONN_SLPPROT,
		      MT_INFRACFG_RX_EN_MASK,
		      FIELD_PREP(MT_INFRACFG_RX_EN_MASK, enable));
	ret = read_poll_timeout(readl, cur, !(cur & MT_INFRACFG_RX_EN_MASK),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev->dcm + MT_INFRACFG_AP2CONN_SLPPROT);
	if (ret)
		return ret;

	mt76_wmac_rmw(dev->dcm, MT_INFRACFG_AP2CONN_SLPPROT,
		      MT_INFRACFG_TX_EN_MASK,
		      FIELD_PREP(MT_INFRACFG_TX_EN_MASK, enable));
	ret = read_poll_timeout(readl, cur, !(cur & MT_INFRACFG_TX_RDY_MASK),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev->dcm + MT_INFRACFG_AP2CONN_SLPPROT);
	if (ret)
		return ret;

	mt76_wmac_rmw(dev->dcm, MT_INFRACFG_CONN2AP_SLPPROT,
		      MT_INFRACFG_RX_EN_MASK,
		      FIELD_PREP(MT_INFRACFG_RX_EN_MASK, enable));
	mt76_wmac_rmw(dev->dcm, MT_INFRACFG_CONN2AP_SLPPROT,
		      MT_INFRACFG_TX_EN_MASK,
		      FIELD_PREP(MT_INFRACFG_TX_EN_MASK, enable));

	return 0;
}

static int mt7986_wmac_coninfra_check(struct mt7915_dev *dev)
{
	u32 cur;

	return read_poll_timeout(mt76_rr, cur, (cur == 0x02070000),
				 USEC_PER_MSEC, 50 * USEC_PER_MSEC,
				 false, dev, MT_CONN_INFRA_BASE);
}

static int mt7986_wmac_coninfra_setup(struct mt7915_dev *dev)
{
	struct device *pdev = dev->mt76.dev;
	struct reserved_mem *rmem;
	struct device_node *np;
	u32 val;

	np = of_parse_phandle(pdev->of_node, "memory-region", 0);
	if (!np)
		return -EINVAL;

	rmem = of_reserved_mem_lookup(np);
	of_node_put(np);
	if (!rmem)
		return -EINVAL;

	val = (rmem->base >> 16) & MT_TOP_MCU_EMI_BASE_MASK;

	/* Set conninfra subsys PLL check */
	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS,
		       MT_INFRA_CKGEN_BUS_RDY_SEL_MASK, 0x1);
	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS,
		       MT_INFRA_CKGEN_BUS_RDY_SEL_MASK, 0x1);

	mt76_rmw_field(dev, MT_TOP_MCU_EMI_BASE,
		       MT_TOP_MCU_EMI_BASE_MASK, val);

	mt76_wr(dev, MT_INFRA_BUS_EMI_START, rmem->base);
	mt76_wr(dev, MT_INFRA_BUS_EMI_END, rmem->size);

	mt76_rr(dev, MT_CONN_INFRA_EFUSE);

	/* Set conninfra sysram */
	mt76_wr(dev, MT_TOP_RGU_SYSRAM_PDN, 0);
	mt76_wr(dev, MT_TOP_RGU_SYSRAM_SLP, 1);

	return 0;
}

static int mt7986_wmac_sku_setup(struct mt7915_dev *dev, u32 *adie_type)
{
	int ret;
	u32 adie_main, adie_ext;

	mt76_rmw_field(dev, MT_CONN_INFRA_ADIE_RESET,
		       MT_CONN_INFRA_ADIE1_RESET_MASK, 0x1);
	mt76_rmw_field(dev, MT_CONN_INFRA_ADIE_RESET,
		       MT_CONN_INFRA_ADIE2_RESET_MASK, 0x1);

	mt76_wmac_spi_lock(dev);

	ret = mt76_wmac_spi_read(dev, 0, MT_ADIE_CHIP_ID, &adie_main);
	if (ret)
		goto out;

	ret = mt76_wmac_spi_read(dev, 1, MT_ADIE_CHIP_ID, &adie_ext);
	if (ret)
		goto out;

	*adie_type = FIELD_GET(MT_ADIE_CHIP_ID_MASK, adie_main) |
		     (MT_ADIE_CHIP_ID_MASK & adie_ext);

out:
	mt76_wmac_spi_unlock(dev);

	return 0;
}

static inline u16 mt7986_adie_idx(u8 adie, u32 adie_type)
{
	if (adie == 0)
		return u32_get_bits(adie_type, MT_ADIE_IDX0);
	else
		return u32_get_bits(adie_type, MT_ADIE_IDX1);
}

static inline bool is_7975(struct mt7915_dev *dev, u8 adie, u32 adie_type)
{
	return mt7986_adie_idx(adie, adie_type) == 0x7975;
}

static inline bool is_7976(struct mt7915_dev *dev, u8 adie, u32 adie_type)
{
	return mt7986_adie_idx(adie, adie_type) == 0x7976;
}

static int mt7986_wmac_adie_thermal_cal(struct mt7915_dev *dev, u8 adie)
{
	int ret;
	u32 data, val;

	ret = mt7986_wmac_adie_efuse_read(dev, adie, MT_ADIE_THADC_ANALOG,
					  &data);
	if (ret || FIELD_GET(MT_ADIE_ANA_EN_MASK, data)) {
		val = FIELD_GET(MT_ADIE_VRPI_SEL_EFUSE_MASK, data);
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_RG_TOP_THADC_BG,
					MT_ADIE_VRPI_SEL_CR_MASK,
					FIELD_PREP(MT_ADIE_VRPI_SEL_CR_MASK, val));
		if (ret)
			return ret;

		val = FIELD_GET(MT_ADIE_PGA_GAIN_EFUSE_MASK, data);
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_RG_TOP_THADC,
					MT_ADIE_PGA_GAIN_MASK,
					FIELD_PREP(MT_ADIE_PGA_GAIN_MASK, val));
		if (ret)
			return ret;
	}

	ret = mt7986_wmac_adie_efuse_read(dev, adie, MT_ADIE_THADC_SLOP,
					  &data);
	if (ret || FIELD_GET(MT_ADIE_ANA_EN_MASK, data)) {
		val = FIELD_GET(MT_ADIE_LDO_CTRL_EFUSE_MASK, data);

		return mt76_wmac_spi_rmw(dev, adie, MT_ADIE_RG_TOP_THADC,
					 MT_ADIE_LDO_CTRL_MASK,
					 FIELD_PREP(MT_ADIE_LDO_CTRL_MASK, val));
	}

	return 0;
}

static int
mt7986_read_efuse_xo_trim_7976(struct mt7915_dev *dev, u8 adie,
			       bool is_40m, int *result)
{
	int ret;
	u32 data, addr;

	addr = is_40m ? MT_ADIE_XTAL_AXM_40M_OSC : MT_ADIE_XTAL_AXM_80M_OSC;
	ret = mt7986_wmac_adie_efuse_read(dev, adie, addr, &data);
	if (ret)
		return ret;

	if (!FIELD_GET(MT_ADIE_XO_TRIM_EN_MASK, data)) {
		*result = 64;
	} else {
		*result = FIELD_GET(MT_ADIE_TRIM_MASK, data);
		addr = is_40m ? MT_ADIE_XTAL_TRIM1_40M_OSC :
				MT_ADIE_XTAL_TRIM1_80M_OSC;
		ret = mt7986_wmac_adie_efuse_read(dev, adie, addr, &data);
		if (ret)
			return ret;

		if (FIELD_GET(MT_ADIE_XO_TRIM_EN_MASK, data) &&
		    FIELD_GET(MT_ADIE_XTAL_DECREASE_MASK, data))
			*result -= FIELD_GET(MT_ADIE_EFUSE_TRIM_MASK, data);
		else if (FIELD_GET(MT_ADIE_XO_TRIM_EN_MASK, data))
			*result += FIELD_GET(MT_ADIE_EFUSE_TRIM_MASK, data);

		*result = max(0, min(127, *result));
	}

	return 0;
}

static int mt7986_wmac_adie_xtal_trim_7976(struct mt7915_dev *dev, u8 adie)
{
	int ret, trim_80m, trim_40m;
	u32 data, val, mode;

	ret = mt7986_wmac_adie_efuse_read(dev, adie, MT_ADIE_XO_TRIM_FLOW,
					  &data);
	if (ret || !FIELD_GET(BIT(1), data))
		return 0;

	ret = mt7986_read_efuse_xo_trim_7976(dev, adie, false, &trim_80m);
	if (ret)
		return ret;

	ret = mt7986_read_efuse_xo_trim_7976(dev, adie, true, &trim_40m);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_read(dev, adie, MT_ADIE_RG_STRAP_PIN_IN, &val);
	if (ret)
		return ret;

	mode = FIELD_PREP(GENMASK(6, 4), val);
	if (!mode || mode == 0x2) {
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_XTAL_C1,
					GENMASK(31, 24),
					FIELD_PREP(GENMASK(31, 24), trim_80m));
		if (ret)
			return ret;

		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_XTAL_C2,
					GENMASK(31, 24),
					FIELD_PREP(GENMASK(31, 24), trim_80m));
	} else if (mode == 0x3 || mode == 0x4 || mode == 0x6) {
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_XTAL_C1,
					GENMASK(23, 16),
					FIELD_PREP(GENMASK(23, 16), trim_40m));
		if (ret)
			return ret;

		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_XTAL_C2,
					GENMASK(23, 16),
					FIELD_PREP(GENMASK(23, 16), trim_40m));
	}

	return ret;
}

static int mt7986_wmac_adie_patch_7976(struct mt7915_dev *dev, u8 adie)
{
	u32 id, version, rg_xo_01, rg_xo_03;
	int ret;

	ret = mt76_wmac_spi_read(dev, adie, MT_ADIE_CHIP_ID, &id);
	if (ret)
		return ret;

	version = FIELD_GET(MT_ADIE_VERSION_MASK, id);

	ret = mt76_wmac_spi_write(dev, adie, MT_ADIE_RG_TOP_THADC, 0x4a563b00);
	if (ret)
		return ret;

	if (version == 0x8a00 || version == 0x8a10 || version == 0x8b00) {
		rg_xo_01 = 0x1d59080f;
		rg_xo_03 = 0x34c00fe0;
	} else {
		rg_xo_01 = 0x1959f80f;
		rg_xo_03 = 0x34d00fe0;
	}

	ret = mt76_wmac_spi_write(dev, adie, MT_ADIE_RG_XO_01, rg_xo_01);
	if (ret)
		return ret;

	return mt76_wmac_spi_write(dev, adie, MT_ADIE_RG_XO_03, rg_xo_03);
}

static int
mt7986_read_efuse_xo_trim_7975(struct mt7915_dev *dev, u8 adie,
			       u32 addr, u32 *result)
{
	int ret;
	u32 data;

	ret = mt7986_wmac_adie_efuse_read(dev, adie, addr, &data);
	if (ret)
		return ret;

	if ((data & MT_ADIE_XO_TRIM_EN_MASK)) {
		if ((data & MT_ADIE_XTAL_DECREASE_MASK))
			*result -= (data & MT_ADIE_EFUSE_TRIM_MASK);
		else
			*result += (data & MT_ADIE_EFUSE_TRIM_MASK);

		*result = (*result & MT_ADIE_TRIM_MASK);
	}

	return 0;
}

static int mt7986_wmac_adie_xtal_trim_7975(struct mt7915_dev *dev, u8 adie)
{
	int ret;
	u32 data, result = 0, value;

	ret = mt7986_wmac_adie_efuse_read(dev, adie, MT_ADIE_7975_XTAL_EN,
					  &data);
	if (ret || !(data & BIT(1)))
		return 0;

	ret = mt7986_wmac_adie_efuse_read(dev, adie, MT_ADIE_7975_XTAL_CAL,
					  &data);
	if (ret)
		return ret;

	if (data & MT_ADIE_XO_TRIM_EN_MASK)
		result = (data & MT_ADIE_TRIM_MASK);

	ret = mt7986_read_efuse_xo_trim_7975(dev, adie, MT_ADIE_7975_XO_TRIM2,
					     &result);
	if (ret)
		return ret;

	ret = mt7986_read_efuse_xo_trim_7975(dev, adie, MT_ADIE_7975_XO_TRIM3,
					     &result);
	if (ret)
		return ret;

	ret = mt7986_read_efuse_xo_trim_7975(dev, adie, MT_ADIE_7975_XO_TRIM4,
					     &result);
	if (ret)
		return ret;

	/* Update trim value to C1 and C2*/
	value = FIELD_GET(MT_ADIE_7975_XO_CTRL2_C1_MASK, result) |
		FIELD_GET(MT_ADIE_7975_XO_CTRL2_C2_MASK, result);
	ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_7975_XO_CTRL2,
				MT_ADIE_7975_XO_CTRL2_MASK, value);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_read(dev, adie, MT_ADIE_7975_XTAL, &value);
	if (ret)
		return ret;

	if (value & MT_ADIE_7975_XTAL_EN_MASK) {
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_7975_XO_2,
					MT_ADIE_7975_XO_2_FIX_EN, 0x0);
		if (ret)
			return ret;
	}

	return mt76_wmac_spi_rmw(dev, adie, MT_ADIE_7975_XO_CTRL6,
				 MT_ADIE_7975_XO_CTRL6_MASK, 0x1);
}

static int mt7986_wmac_adie_patch_7975(struct mt7915_dev *dev, u8 adie)
{
	int ret;

	/* disable CAL LDO and fine tune RFDIG LDO */
	ret = mt76_wmac_spi_write(dev, adie, 0x348, 0x00000002);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x378, 0x00000002);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x3a8, 0x00000002);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x3d8, 0x00000002);
	if (ret)
		return ret;

	/* set CKA driving and filter */
	ret = mt76_wmac_spi_write(dev, adie, 0xa1c, 0x30000aaa);
	if (ret)
		return ret;

	/* set CKB LDO to 1.4V */
	ret = mt76_wmac_spi_write(dev, adie, 0xa84, 0x8470008a);
	if (ret)
		return ret;

	/* turn on SX0 LTBUF */
	ret = mt76_wmac_spi_write(dev, adie, 0x074, 0x00000002);
	if (ret)
		return ret;

	/* CK_BUF_SW_EN = 1 (all buf in manual mode.) */
	ret = mt76_wmac_spi_write(dev, adie, 0xaa4, 0x01001fc0);
	if (ret)
		return ret;

	/* BT mode/WF normal mode 00000005 */
	ret = mt76_wmac_spi_write(dev, adie, 0x070, 0x00000005);
	if (ret)
		return ret;

	/* BG thermal sensor offset update */
	ret = mt76_wmac_spi_write(dev, adie, 0x344, 0x00000088);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x374, 0x00000088);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x3a4, 0x00000088);
	if (ret)
		return ret;

	ret = mt76_wmac_spi_write(dev, adie, 0x3d4, 0x00000088);
	if (ret)
		return ret;

	/* set WCON VDD IPTAT to "0000" */
	ret = mt76_wmac_spi_write(dev, adie, 0xa80, 0x44d07000);
	if (ret)
		return ret;

	/* change back LTBUF SX3 drving to default value */
	ret = mt76_wmac_spi_write(dev, adie, 0xa88, 0x3900aaaa);
	if (ret)
		return ret;

	/* SM input cap off */
	ret = mt76_wmac_spi_write(dev, adie, 0x2c4, 0x00000000);
	if (ret)
		return ret;

	/* set CKB driving and filter */
	return mt76_wmac_spi_write(dev, adie, 0x2c8, 0x00000072);
}

static int mt7986_wmac_adie_cfg(struct mt7915_dev *dev, u8 adie, u32 adie_type)
{
	int ret;

	mt76_wmac_spi_lock(dev);
	ret = mt76_wmac_spi_write(dev, adie, MT_ADIE_CLK_EN, ~0);
	if (ret)
		goto out;

	if (is_7975(dev, adie, adie_type)) {
		ret = mt76_wmac_spi_rmw(dev, adie, MT_ADIE_7975_COCLK,
					BIT(1), 0x1);
		if (ret)
			goto out;

		ret = mt7986_wmac_adie_thermal_cal(dev, adie);
		if (ret)
			goto out;

		ret = mt7986_wmac_adie_xtal_trim_7975(dev, adie);
		if (ret)
			goto out;

		ret = mt7986_wmac_adie_patch_7975(dev, adie);
	} else if (is_7976(dev, adie, adie_type)) {
		if (mt7986_wmac_check_adie_type(dev) == ADIE_DBDC) {
			ret = mt76_wmac_spi_write(dev, adie,
						  MT_ADIE_WRI_CK_SEL, 0x1c);
			if (ret)
				goto out;
		}

		ret = mt7986_wmac_adie_thermal_cal(dev, adie);
		if (ret)
			goto out;

		ret = mt7986_wmac_adie_xtal_trim_7976(dev, adie);
		if (ret)
			goto out;

		ret = mt7986_wmac_adie_patch_7976(dev, adie);
	}
out:
	mt76_wmac_spi_unlock(dev);

	return ret;
}

static int
mt7986_wmac_afe_cal(struct mt7915_dev *dev, u8 adie, bool dbdc, u32 adie_type)
{
	int ret;
	u8 idx;

	mt76_wmac_spi_lock(dev);
	if (is_7975(dev, adie, adie_type))
		ret = mt76_wmac_spi_write(dev, adie,
					  MT_AFE_RG_ENCAL_WBTAC_IF_SW,
					  0x80000000);
	else
		ret = mt76_wmac_spi_write(dev, adie,
					  MT_AFE_RG_ENCAL_WBTAC_IF_SW,
					  0x88888005);
	if (ret)
		goto out;

	idx = dbdc ? ADIE_DBDC : adie;

	mt76_rmw_field(dev, MT_AFE_DIG_EN_01(idx),
		       MT_AFE_RG_WBG_EN_RCK_MASK, 0x1);
	usleep_range(60, 100);

	mt76_rmw(dev, MT_AFE_DIG_EN_01(idx),
		 MT_AFE_RG_WBG_EN_RCK_MASK, 0x0);

	mt76_rmw_field(dev, MT_AFE_DIG_EN_03(idx),
		       MT_AFE_RG_WBG_EN_BPLL_UP_MASK, 0x1);
	usleep_range(30, 100);

	mt76_rmw_field(dev, MT_AFE_DIG_EN_03(idx),
		       MT_AFE_RG_WBG_EN_WPLL_UP_MASK, 0x1);
	usleep_range(60, 100);

	mt76_rmw_field(dev, MT_AFE_DIG_EN_01(idx),
		       MT_AFE_RG_WBG_EN_TXCAL_MASK, 0x1f);
	usleep_range(800, 1000);

	mt76_rmw(dev, MT_AFE_DIG_EN_01(idx),
		 MT_AFE_RG_WBG_EN_TXCAL_MASK, 0x0);
	mt76_rmw(dev, MT_AFE_DIG_EN_03(idx),
		 MT_AFE_RG_WBG_EN_PLL_UP_MASK, 0x0);

	ret = mt76_wmac_spi_write(dev, adie, MT_AFE_RG_ENCAL_WBTAC_IF_SW,
				  0x5);

out:
	mt76_wmac_spi_unlock(dev);

	return ret;
}

static void mt7986_wmac_subsys_pll_initial(struct mt7915_dev *dev, u8 band)
{
	mt76_rmw(dev, MT_AFE_PLL_STB_TIME(band),
		 MT_AFE_PLL_STB_TIME_MASK, MT_AFE_PLL_STB_TIME_VAL);

	mt76_rmw(dev, MT_AFE_DIG_EN_02(band),
		 MT_AFE_PLL_CFG_MASK, MT_AFE_PLL_CFG_VAL);

	mt76_rmw(dev, MT_AFE_DIG_TOP_01(band),
		 MT_AFE_DIG_TOP_01_MASK, MT_AFE_DIG_TOP_01_VAL);
}

static void mt7986_wmac_subsys_setting(struct mt7915_dev *dev)
{
	/* Subsys pll init */
	mt7986_wmac_subsys_pll_initial(dev, 0);
	mt7986_wmac_subsys_pll_initial(dev, 1);

	/* Set legacy OSC control stable time*/
	mt76_rmw(dev, MT_CONN_INFRA_OSC_RC_EN,
		 MT_CONN_INFRA_OSC_RC_EN_MASK, 0x0);
	mt76_rmw(dev, MT_CONN_INFRA_OSC_CTRL,
		 MT_CONN_INFRA_OSC_STB_TIME_MASK, 0x80706);

	/* prevent subsys from power on/of in a short time interval */
	mt76_rmw(dev, MT_TOP_WFSYS_PWR,
		 MT_TOP_PWR_ACK_MASK | MT_TOP_PWR_KEY_MASK,
		 MT_TOP_PWR_KEY);
}

static int mt7986_wmac_bus_timeout(struct mt7915_dev *dev)
{
	mt76_rmw_field(dev, MT_INFRA_BUS_OFF_TIMEOUT,
		       MT_INFRA_BUS_TIMEOUT_LIMIT_MASK, 0x2);

	mt76_rmw_field(dev, MT_INFRA_BUS_OFF_TIMEOUT,
		       MT_INFRA_BUS_TIMEOUT_EN_MASK, 0xf);

	mt76_rmw_field(dev, MT_INFRA_BUS_ON_TIMEOUT,
		       MT_INFRA_BUS_TIMEOUT_LIMIT_MASK, 0xc);

	mt76_rmw_field(dev, MT_INFRA_BUS_ON_TIMEOUT,
		       MT_INFRA_BUS_TIMEOUT_EN_MASK, 0xf);

	return mt7986_wmac_coninfra_check(dev);
}

static void mt7986_wmac_clock_enable(struct mt7915_dev *dev, u32 adie_type)
{
	u32 cur;

	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS_WPLL_DIV_1,
		       MT_INFRA_CKGEN_DIV_SEL_MASK, 0x1);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS_WPLL_DIV_2,
		       MT_INFRA_CKGEN_DIV_SEL_MASK, 0x1);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS_WPLL_DIV_1,
		       MT_INFRA_CKGEN_DIV_EN_MASK, 0x1);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS_WPLL_DIV_2,
		       MT_INFRA_CKGEN_DIV_EN_MASK, 0x1);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_RFSPI_WPLL_DIV,
		       MT_INFRA_CKGEN_DIV_SEL_MASK, 0x8);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_RFSPI_WPLL_DIV,
		       MT_INFRA_CKGEN_DIV_EN_MASK, 0x1);

	mt76_rmw_field(dev, MT_INFRA_CKGEN_BUS,
		       MT_INFRA_CKGEN_BUS_CLK_SEL_MASK, 0x0);

	mt76_rmw_field(dev, MT_CONN_INFRA_HW_CTRL,
		       MT_CONN_INFRA_HW_CTRL_MASK, 0x1);

	mt76_rmw(dev, MT_TOP_CONN_INFRA_WAKEUP,
		 MT_TOP_CONN_INFRA_WAKEUP_MASK, 0x1);

	usleep_range(900, 1000);

	mt76_wmac_spi_lock(dev);
	if (is_7975(dev, 0, adie_type) || is_7976(dev, 0, adie_type)) {
		mt76_rmw_field(dev, MT_ADIE_SLP_CTRL_CK0(0),
			       MT_SLP_CTRL_EN_MASK, 0x1);

		read_poll_timeout(mt76_rr, cur, !(cur & MT_SLP_CTRL_BSY_MASK),
				  USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				  dev, MT_ADIE_SLP_CTRL_CK0(0));
	}
	if (is_7975(dev, 1, adie_type) || is_7976(dev, 1, adie_type)) {
		mt76_rmw_field(dev, MT_ADIE_SLP_CTRL_CK0(1),
			       MT_SLP_CTRL_EN_MASK, 0x1);

		read_poll_timeout(mt76_rr, cur, !(cur & MT_SLP_CTRL_BSY_MASK),
				  USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				  dev, MT_ADIE_SLP_CTRL_CK0(0));
	}
	mt76_wmac_spi_unlock(dev);

	mt76_rmw(dev, MT_TOP_CONN_INFRA_WAKEUP,
		 MT_TOP_CONN_INFRA_WAKEUP_MASK, 0x0);
	usleep_range(900, 1000);
}

static int mt7986_wmac_top_wfsys_wakeup(struct mt7915_dev *dev, bool enable)
{
	mt76_rmw_field(dev, MT_TOP_WFSYS_WAKEUP,
		       MT_TOP_WFSYS_WAKEUP_MASK, enable);

	usleep_range(900, 1000);

	if (!enable)
		return 0;

	return mt7986_wmac_coninfra_check(dev);
}

static int mt7986_wmac_wm_enable(struct mt7915_dev *dev, bool enable)
{
	u32 cur;

	mt76_rmw_field(dev, MT7986_TOP_WM_RESET,
		       MT7986_TOP_WM_RESET_MASK, enable);
	if (!enable)
		return 0;

	return read_poll_timeout(mt76_rr, cur, (cur == 0x1d1e),
				 USEC_PER_MSEC, 5000 * USEC_PER_MSEC, false,
				 dev, MT_TOP_CFG_ON_ROM_IDX);
}

static int mt7986_wmac_wfsys_poweron(struct mt7915_dev *dev, bool enable)
{
	u32 mask = MT_TOP_PWR_EN_MASK | MT_TOP_PWR_KEY_MASK;
	u32 cur;

	mt76_rmw(dev, MT_TOP_WFSYS_PWR, mask,
		 MT_TOP_PWR_KEY | FIELD_PREP(MT_TOP_PWR_EN_MASK, enable));

	return read_poll_timeout(mt76_rr, cur,
		(FIELD_GET(MT_TOP_WFSYS_RESET_STATUS_MASK, cur) == enable),
		USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
		dev, MT_TOP_WFSYS_RESET_STATUS);
}

static int mt7986_wmac_wfsys_setting(struct mt7915_dev *dev)
{
	int ret;
	u32 cur;

	/* Turn off wfsys2conn bus sleep protect */
	mt76_rmw(dev, MT_CONN_INFRA_WF_SLP_PROT,
		 MT_CONN_INFRA_WF_SLP_PROT_MASK, 0x0);

	ret = mt7986_wmac_wfsys_poweron(dev, true);
	if (ret)
		return ret;

	/* Check bus sleep protect */

	ret = read_poll_timeout(mt76_rr, cur,
				!(cur & MT_CONN_INFRA_CONN_WF_MASK),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, MT_CONN_INFRA_WF_SLP_PROT_RDY);
	if (ret)
		return ret;

	ret = read_poll_timeout(mt76_rr, cur, !(cur & MT_SLP_WFDMA2CONN_MASK),
				USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				dev, MT_SLP_STATUS);
	if (ret)
		return ret;

	return read_poll_timeout(mt76_rr, cur, (cur == 0x02060000),
				 USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
				 dev, MT_TOP_CFG_IP_VERSION_ADDR);
}

static void mt7986_wmac_wfsys_set_timeout(struct mt7915_dev *dev)
{
	u32 mask = MT_MCU_BUS_TIMEOUT_SET_MASK |
		   MT_MCU_BUS_TIMEOUT_CG_EN_MASK |
		   MT_MCU_BUS_TIMEOUT_EN_MASK;
	u32 val = FIELD_PREP(MT_MCU_BUS_TIMEOUT_SET_MASK, 1) |
		  FIELD_PREP(MT_MCU_BUS_TIMEOUT_CG_EN_MASK, 1) |
		  FIELD_PREP(MT_MCU_BUS_TIMEOUT_EN_MASK, 1);

	mt76_rmw(dev, MT_MCU_BUS_TIMEOUT, mask, val);

	mt76_wr(dev, MT_MCU_BUS_REMAP, 0x810f0000);

	mask = MT_MCU_BUS_DBG_TIMEOUT_SET_MASK |
	       MT_MCU_BUS_DBG_TIMEOUT_CK_EN_MASK |
	       MT_MCU_BUS_DBG_TIMEOUT_EN_MASK;
	val = FIELD_PREP(MT_MCU_BUS_DBG_TIMEOUT_SET_MASK, 0x3aa) |
	      FIELD_PREP(MT_MCU_BUS_DBG_TIMEOUT_CK_EN_MASK, 1) |
	      FIELD_PREP(MT_MCU_BUS_DBG_TIMEOUT_EN_MASK, 1);

	mt76_rmw(dev, MT_MCU_BUS_DBG_TIMEOUT, mask, val);
}

static int mt7986_wmac_sku_update(struct mt7915_dev *dev, u32 adie_type)
{
	u32 val;

	if (is_7976(dev, 0, adie_type) && is_7976(dev, 1, adie_type))
		val = 0xf;
	else if (is_7975(dev, 0, adie_type) && is_7975(dev, 1, adie_type))
		val = 0xd;
	else if (is_7976(dev, 0, adie_type))
		val = 0x7;
	else if (is_7975(dev, 1, adie_type))
		val = 0x8;
	else if (is_7976(dev, 1, adie_type))
		val = 0xa;
	else
		return -EINVAL;

	mt76_wmac_rmw(dev->sku, MT_TOP_POS_SKU, MT_TOP_POS_SKU_MASK,
		      FIELD_PREP(MT_TOP_POS_SKU_MASK, val));

	mt76_wr(dev, MT_CONNINFRA_SKU_DEC_ADDR, val);

	return 0;
}

static int
mt7986_wmac_adie_setup(struct mt7915_dev *dev, u8 adie, u32 adie_type)
{
	int ret;

	if (!(is_7975(dev, adie, adie_type) || is_7976(dev, adie, adie_type)))
		return 0;

	ret = mt7986_wmac_adie_cfg(dev, adie, adie_type);
	if (ret)
		return ret;

	ret = mt7986_wmac_afe_cal(dev, adie, false, adie_type);
	if (ret)
		return ret;

	if (!adie && (mt7986_wmac_check_adie_type(dev) == ADIE_DBDC))
		ret = mt7986_wmac_afe_cal(dev, adie, true, adie_type);

	return ret;
}

static int mt7986_wmac_subsys_powerup(struct mt7915_dev *dev, u32 adie_type)
{
	int ret;

	mt7986_wmac_subsys_setting(dev);

	ret = mt7986_wmac_bus_timeout(dev);
	if (ret)
		return ret;

	mt7986_wmac_clock_enable(dev, adie_type);

	return 0;
}

static int mt7986_wmac_wfsys_powerup(struct mt7915_dev *dev)
{
	int ret;

	ret = mt7986_wmac_wm_enable(dev, false);
	if (ret)
		return ret;

	ret = mt7986_wmac_wfsys_setting(dev);
	if (ret)
		return ret;

	mt7986_wmac_wfsys_set_timeout(dev);

	return mt7986_wmac_wm_enable(dev, true);
}

int mt7986_wmac_enable(struct mt7915_dev *dev)
{
	int ret;
	u32 adie_type;

	ret = mt7986_wmac_consys_reset(dev, true);
	if (ret)
		return ret;

	ret = mt7986_wmac_gpio_setup(dev);
	if (ret)
		return ret;

	ret = mt7986_wmac_consys_lockup(dev, false);
	if (ret)
		return ret;

	ret = mt7986_wmac_coninfra_check(dev);
	if (ret)
		return ret;

	ret = mt7986_wmac_coninfra_setup(dev);
	if (ret)
		return ret;

	ret = mt7986_wmac_sku_setup(dev, &adie_type);
	if (ret)
		return ret;

	ret = mt7986_wmac_adie_setup(dev, 0, adie_type);
	if (ret)
		return ret;

	ret = mt7986_wmac_adie_setup(dev, 1, adie_type);
	if (ret)
		return ret;

	ret = mt7986_wmac_subsys_powerup(dev, adie_type);
	if (ret)
		return ret;

	ret = mt7986_wmac_top_wfsys_wakeup(dev, true);
	if (ret)
		return ret;

	ret = mt7986_wmac_wfsys_powerup(dev);
	if (ret)
		return ret;

	return mt7986_wmac_sku_update(dev, adie_type);
}

void mt7986_wmac_disable(struct mt7915_dev *dev)
{
	u32 cur;

	mt7986_wmac_top_wfsys_wakeup(dev, true);

	/* Turn on wfsys2conn bus sleep protect */
	mt76_rmw_field(dev, MT_CONN_INFRA_WF_SLP_PROT,
		       MT_CONN_INFRA_WF_SLP_PROT_MASK, 0x1);

	/* Check wfsys2conn bus sleep protect */
	read_poll_timeout(mt76_rr, cur, !(cur ^ MT_CONN_INFRA_CONN),
			  USEC_PER_MSEC, 50 * USEC_PER_MSEC, false,
			  dev, MT_CONN_INFRA_WF_SLP_PROT_RDY);

	mt7986_wmac_wfsys_poweron(dev, false);

	/* Turn back wpll setting */
	mt76_rmw_field(dev, MT_AFE_DIG_EN_02(0), MT_AFE_MCU_BPLL_CFG_MASK, 0x2);
	mt76_rmw_field(dev, MT_AFE_DIG_EN_02(0), MT_AFE_WPLL_CFG_MASK, 0x2);

	/* Reset EMI */
	mt76_rmw_field(dev, MT_CONN_INFRA_EMI_REQ,
		       MT_CONN_INFRA_EMI_REQ_MASK, 0x1);
	mt76_rmw_field(dev, MT_CONN_INFRA_EMI_REQ,
		       MT_CONN_INFRA_EMI_REQ_MASK, 0x0);
	mt76_rmw_field(dev, MT_CONN_INFRA_EMI_REQ,
		       MT_CONN_INFRA_INFRA_REQ_MASK, 0x1);
	mt76_rmw_field(dev, MT_CONN_INFRA_EMI_REQ,
		       MT_CONN_INFRA_INFRA_REQ_MASK, 0x0);

	mt7986_wmac_top_wfsys_wakeup(dev, false);
	mt7986_wmac_consys_lockup(dev, true);
	mt7986_wmac_consys_reset(dev, false);
}

static int mt7986_wmac_init(struct mt7915_dev *dev)
{
	struct device *pdev = dev->mt76.dev;
	struct platform_device *pfdev = to_platform_device(pdev);
	struct clk *mcu_clk, *ap_conn_clk;

	mcu_clk = devm_clk_get(pdev, "mcu");
	if (IS_ERR(mcu_clk))
		dev_err(pdev, "mcu clock not found\n");
	else if (clk_prepare_enable(mcu_clk))
		dev_err(pdev, "mcu clock configuration failed\n");

	ap_conn_clk = devm_clk_get(pdev, "ap2conn");
	if (IS_ERR(ap_conn_clk))
		dev_err(pdev, "ap2conn clock not found\n");
	else if (clk_prepare_enable(ap_conn_clk))
		dev_err(pdev, "ap2conn clock configuration failed\n");

	dev->dcm = devm_platform_ioremap_resource(pfdev, 1);
	if (IS_ERR(dev->dcm))
		return PTR_ERR(dev->dcm);

	dev->sku = devm_platform_ioremap_resource(pfdev, 2);
	if (IS_ERR(dev->sku))
		return PTR_ERR(dev->sku);

	dev->rstc = devm_reset_control_get(pdev, "consys");
	if (IS_ERR(dev->rstc))
		return PTR_ERR(dev->rstc);

	return 0;
}

static int mt7986_wmac_probe(struct platform_device *pdev)
{
	void __iomem *mem_base;
	struct mt7915_dev *dev;
	struct mt76_dev *mdev;
	int irq, ret;
	u32 chip_id;

	chip_id = (uintptr_t)of_device_get_match_data(&pdev->dev);

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;

	mem_base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(mem_base)) {
		dev_err(&pdev->dev, "Failed to get memory resource\n");
		return PTR_ERR(mem_base);
	}

	dev = mt7915_mmio_probe(&pdev->dev, mem_base, chip_id);
	if (IS_ERR(dev))
		return PTR_ERR(dev);

	mdev = &dev->mt76;
	ret = devm_request_irq(mdev->dev, irq, mt7915_irq_handler,
			       IRQF_SHARED, KBUILD_MODNAME, dev);
	if (ret)
		goto free_device;

	ret = mt7986_wmac_init(dev);
	if (ret)
		goto free_irq;

	mt7915_wfsys_reset(dev);

	ret = mt7915_register_device(dev);
	if (ret)
		goto free_irq;

	return 0;

free_irq:
	devm_free_irq(mdev->dev, irq, dev);

free_device:
	mt76_free_device(&dev->mt76);

	return ret;
}

static int mt7986_wmac_remove(struct platform_device *pdev)
{
	struct mt7915_dev *dev = platform_get_drvdata(pdev);

	mt7915_unregister_device(dev);

	return 0;
}

static const struct of_device_id mt7986_wmac_of_match[] = {
	{ .compatible = "mediatek,mt7986-wmac", .data = (u32 *)0x7986 },
	{},
};

MODULE_DEVICE_TABLE(of, mt7986_wmac_of_match);

struct platform_driver mt7986_wmac_driver = {
	.driver = {
		.name = "mt7986-wmac",
		.of_match_table = mt7986_wmac_of_match,
	},
	.probe = mt7986_wmac_probe,
	.remove = mt7986_wmac_remove,
};

MODULE_FIRMWARE(MT7986_FIRMWARE_WA);
MODULE_FIRMWARE(MT7986_FIRMWARE_WM);
MODULE_FIRMWARE(MT7986_FIRMWARE_WM_MT7975);
MODULE_FIRMWARE(MT7986_ROM_PATCH);
MODULE_FIRMWARE(MT7986_ROM_PATCH_MT7975);