summaryrefslogtreecommitdiff
path: root/btpower.c
blob: 941169df7c8f73e569d371cee6a7fe52897d5eb2 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
 */

/*
 * Bluetooth Power Switch Module
 * controls power to external Bluetooth device
 * with interface to power management device
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/btpower.h>
#include <linux/of_device.h>
#include <soc/qcom/cmd-db.h>

#if IS_ENABLED(CONFIG_BT_SLIM_QCA6390) || \
	IS_ENABLED(CONFIG_BT_SLIM_QCA6490) || \
	IS_ENABLED(CONFIG_BTFM_SLIM_WCN3990) || \
	IS_ENABLED(CONFIG_BTFM_SLIM_WCN7850)
#include "btfm_slim.h"
#endif
#include <linux/fs.h>

#define PWR_SRC_NOT_AVAILABLE -2
#define DEFAULT_INVALID_VALUE -1
#define PWR_SRC_INIT_STATE_IDX 0
#define BTPOWER_MBOX_MSG_MAX_LEN 64
#define BTPOWER_MBOX_TIMEOUT_MS 1000
#define XO_CLK_RETRY_COUNT_MAX 5

/**
 * enum btpower_vreg_param: Voltage regulator TCS param
 * @BTPOWER_VREG_VOLTAGE: Provides voltage level to be configured in TCS
 * @BTPOWER_VREG_MODE: Regulator mode
 * @BTPOWER_VREG_ENABLE: Set Voltage regulator enable config in TCS
 * @BTPOWER_VREG_PARAM_MAX: vreg param boundary
 */
enum btpower_vreg_param {
	BTPOWER_VREG_VOLTAGE = 0,
	BTPOWER_VREG_MODE,
	BTPOWER_VREG_ENABLE,
	BTPOWER_VREG_PARAM_MAX,
};
static const char vreg_param_str[BTPOWER_VREG_PARAM_MAX] = {'v', 'm', 'e'};

/**
 * enum btpower_tcs_seq: TCS sequence ID for trigger
 * @BTPOWER_TCS_UP_SEQ: TCS Sequence based on up trigger / Wake TCS
 * @BTPOWER_TCS_DOWN_SEQ: TCS Sequence based on down trigger / Sleep TCS
 * @BTPOWER_TCS_ALL_SEQ: Update for both up and down triggers
 * @BTPOWER_TCS_SEQ_MAX: TCS sequence ID boundary
 */
enum btpower_tcs_seq {
	BTPOWER_TCS_UP_SEQ = 0,
	BTPOWER_TCS_DOWN_SEQ,
	BTPOWER_TCS_ALL_SEQ,
	BTPOWER_TCS_SEQ_MAX,
};
static const char *const tcs_seq_str[BTPOWER_TCS_SEQ_MAX] =
	{"upval", "dwnval", "enable"};

enum power_src_pos {
	BT_RESET_GPIO = PWR_SRC_INIT_STATE_IDX,
	BT_VDD_AON_LDO,
	BT_VDD_DIG_LDO,
	BT_VDD_RFA1_LDO,
	BT_VDD_RFA2_LDO,
	BT_VDD_ASD_LDO,
	BT_VDD_XTAL_LDO,
	BT_VDD_PA_LDO,
	BT_VDD_CORE_LDO,
	BT_VDD_IO_LDO,
	BT_VDD_LDO,
	BT_VDD_RFA_0p8,
	BT_VDD_RFACMN,
	// these indexes GPIOs/regs value are fetched during crash.
	BT_RESET_GPIO_CURRENT,
	BT_SW_CTRL_GPIO_CURRENT,
	BT_VDD_AON_LDO_CURRENT,
	BT_VDD_DIG_LDO_CURRENT,
	BT_VDD_RFA1_LDO_CURRENT,
	BT_VDD_RFA2_LDO_CURRENT,
	BT_VDD_ASD_LDO_CURRENT,
	BT_VDD_XTAL_LDO_CURRENT,
	BT_VDD_PA_LDO_CURRENT,
	BT_VDD_CORE_LDO_CURRENT,
	BT_VDD_IO_LDO_CURRENT,
	BT_VDD_LDO_CURRENT,
	BT_VDD_RFA_0p8_CURRENT,
	BT_VDD_RFACMN_CURRENT
};

#define SYNC_GPIO_SOURCE_CURRENT(drvdata, gpio, label)                     \
{                                                                          \
	if (gpio_is_valid(gpio)) {                                         \
		drvdata->bt_power_src_status[label ## _CURRENT] =          \
			gpio_get_value(gpio);                              \
		pr_debug("%s: %s(%d) value(%d)\n", __func__, #label, gpio, \
			drvdata->bt_power_src_status[label ## _CURRENT]);  \
	} else {                                                           \
		drvdata->bt_power_src_status[label ## _CURRENT] =          \
			DEFAULT_INVALID_VALUE;                             \
		pr_debug("%s: %s not configured\n", __func__, #label);     \
	}                                                                  \
}

#define SET_GPIO_SOURCE_STATE(drvdata, gpio, label, value)                    \
{                                                                             \
	if (gpio_is_valid(gpio)) {                                            \
		gpio_set_value(gpio, value);                                  \
		drvdata->bt_power_src_status[label] = value;                  \
		pr_debug("%s: %s(%d) value(%d)\n", __func__, #label, gpio,    \
			drvdata->bt_power_src_status[label]);                 \
	} else {                                                              \
		drvdata->bt_power_src_status[label] = DEFAULT_INVALID_VALUE;  \
		pr_debug("%s: %s not configured\n", __func__, #label);        \
	}                                                                     \
}

// Regulator structure for QCA6174/QCA9377/QCA9379 BT SoC series
static struct bt_power_vreg_data bt_vregs_info_qca61x4_937x[] = {
	{NULL, "qcom,bt-vdd-aon", 928000, 928000, 0, false, false,
		{BT_VDD_AON_LDO, BT_VDD_AON_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-io", 1710000, 3460000, 0, false, false,
		{BT_VDD_IO_LDO, BT_VDD_IO_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-core", 3135000, 3465000, 0, false, false,
		{BT_VDD_CORE_LDO, BT_VDD_CORE_LDO_CURRENT}},
};

// Regulator structure for QCA6390 and QCA6490 BT SoC series
static struct bt_power_vreg_data bt_vregs_info_qca6x9x[] = {
	{NULL, "qcom,bt-vdd-io",      1800000, 1800000, 0, false, true,
		{BT_VDD_IO_LDO, BT_VDD_IO_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-aon",     966000,  966000,  0, false, true,
		{BT_VDD_AON_LDO, BT_VDD_AON_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfacmn",  950000,  950000,  0, false, true,
		{BT_VDD_RFACMN, BT_VDD_RFACMN_CURRENT}},
	/* BT_CX_MX */
	{NULL, "qcom,bt-vdd-dig",      966000,  966000,  0, false, true,
		{BT_VDD_DIG_LDO, BT_VDD_DIG_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa-0p8",  950000,  952000,  0, false, true,
		{BT_VDD_RFA_0p8, BT_VDD_RFA_0p8_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa1",     1900000, 1900000, 0, false, true,
		{BT_VDD_RFA1_LDO, BT_VDD_RFA1_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa2",     1900000, 1900000, 0, false, true,
		{BT_VDD_RFA2_LDO, BT_VDD_RFA2_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-asd",      2800000, 2800000, 0, false, true,
		{BT_VDD_ASD_LDO, BT_VDD_ASD_LDO_CURRENT}},
};


// Regulator structure for WCN7850 BT SoC series
static struct bt_power_vreg_data bt_vregs_info_wcn7850[] = {
	{NULL, "qcom,bt-vdd-io",      1800000, 1800000, 0, false, true,
		{BT_VDD_IO_LDO, BT_VDD_IO_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-aon",     950000,  950000,  0, false, true,
		{BT_VDD_AON_LDO, BT_VDD_AON_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfacmn",  950000,  950000,  0, false, true,
		{BT_VDD_RFACMN, BT_VDD_RFACMN_CURRENT}},
	/* BT_CX_MX */
	{NULL, "qcom,bt-vdd-dig",      950000,  950000,  0, false, true,
		{BT_VDD_DIG_LDO, BT_VDD_DIG_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa-0p8",  950000,  952000,  0, false, true,
		{BT_VDD_RFA_0p8, BT_VDD_RFA_0p8_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa1",     1900000, 1900000, 0, false, true,
		{BT_VDD_RFA1_LDO, BT_VDD_RFA1_LDO_CURRENT}},
	{NULL, "qcom,bt-vdd-rfa2",     1350000, 1350000, 0, false, true,
		{BT_VDD_RFA2_LDO, BT_VDD_RFA2_LDO_CURRENT}},
};

// Regulator structure for WCN399x BT SoC series
static const struct bt_power bt_vreg_info_wcn399x = {
	.compatible = "qcom,wcn3990",
	.vregs = (struct bt_power_vreg_data []) {
		{NULL, "qcom,bt-vdd-io",   1700000, 1900000, 0, false, false,
			{BT_VDD_IO_LDO, BT_VDD_IO_LDO_CURRENT}},
		{NULL, "qcom,bt-vdd-core", 1304000, 1304000, 0, false, false,
			{BT_VDD_CORE_LDO, BT_VDD_CORE_LDO_CURRENT}},
		{NULL, "qcom,bt-vdd-pa",   3000000, 3312000, 0, false, false,
			{BT_VDD_PA_LDO, BT_VDD_PA_LDO_CURRENT}},
		{NULL, "qcom,bt-vdd-xtal", 1700000, 1900000, 0, false, false,
			{BT_VDD_XTAL_LDO, BT_VDD_XTAL_LDO_CURRENT}},
	},
	.num_vregs = 4,
};

static const struct bt_power bt_vreg_info_qca6174 = {
	.compatible = "qcom,qca6174",
	.vregs = bt_vregs_info_qca61x4_937x,
	.num_vregs = ARRAY_SIZE(bt_vregs_info_qca61x4_937x),
};

static const struct bt_power bt_vreg_info_qca6390 = {
	.compatible = "qcom,qca6390",
	.vregs = bt_vregs_info_qca6x9x,
	.num_vregs = ARRAY_SIZE(bt_vregs_info_qca6x9x),
};

static const struct bt_power bt_vreg_info_qca6490 = {
	.compatible = "qcom,qca6490",
	.vregs = bt_vregs_info_qca6x9x,
	.num_vregs = ARRAY_SIZE(bt_vregs_info_qca6x9x),
};

static const struct bt_power bt_vreg_info_wcn7850 = {
	.compatible = "qcom,wcn7850",
	.vregs = bt_vregs_info_wcn7850,
	.num_vregs = ARRAY_SIZE(bt_vregs_info_wcn7850),
};

static const struct of_device_id bt_power_match_table[] = {
	{	.compatible = "qcom,qca6174", .data = &bt_vreg_info_qca6174},
	{	.compatible = "qcom,wcn3990", .data = &bt_vreg_info_wcn399x},
	{	.compatible = "qcom,qca6390", .data = &bt_vreg_info_qca6390},
	{	.compatible = "qcom,qca6490", .data = &bt_vreg_info_qca6490},
	{	.compatible = "qcom,wcn7850", .data = &bt_vreg_info_wcn7850},
	{},
};

static int bt_power_vreg_set(struct btpower_platform_data *drvdata,
			     enum bt_power_modes mode);
static int btpower_enable_ipa_vreg(struct btpower_platform_data *drvdata);

static int bt_vreg_enable(struct bt_power_vreg_data *vreg)
{
	int rc = 0;

	pr_debug("%s: vreg_en for : %s\n", __func__, vreg->name);

	if (vreg->is_enabled)
		return rc;

	if ((vreg->min_vol != 0) && (vreg->max_vol != 0)) {
		rc = regulator_set_voltage(vreg->reg, vreg->min_vol,
					vreg->max_vol);
		if (rc < 0) {
			pr_err("%s: regulator_enable(%s) failed. rc=%d\n",
				__func__, vreg->name, rc);
			goto out;
		}
	}

	if (vreg->load_curr >= 0) {
		rc = regulator_set_load(vreg->reg, vreg->load_curr);
		if (rc < 0) {
			pr_err("%s: regulator_set_load(%s) failed rc=%d\n",
				__func__, vreg->name, rc);
			goto out;
		}
	}

	rc = regulator_enable(vreg->reg);
	if (rc < 0) {
		pr_err("%s: regulator_enable(%s) failed. rc=%d\n",
			__func__, vreg->name, rc);
		goto out;
	}
	vreg->is_enabled = true;

out:
	return rc;
}

static int bt_vreg_enable_retention(const struct bt_power_vreg_data *vreg)
{
	int rc = 0;

	if (!vreg)
		return rc;

	pr_debug("%s: enable_retention for : %s\n", __func__, vreg->name);

	if (!vreg->is_enabled || !vreg->is_retention_supp)
		return rc;

	if ((vreg->min_vol != 0) && (vreg->max_vol != 0)) {
		/* Set the min voltage to 0 */
		rc = regulator_set_voltage(vreg->reg, 0, vreg->max_vol);
		if (rc < 0) {
			pr_err("%s: regulator_set_voltage(%s) failed rc=%d\n",
				__func__, vreg->name, rc);
			goto out;
		}
	}
	if (vreg->load_curr >= 0) {
		rc = regulator_set_load(vreg->reg, 0);
		if (rc < 0) {
			pr_err("%s: regulator_set_load(%s) failed rc=%d\n",
				__func__, vreg->name, rc);
		}
	}

out:
	return rc;
}

static int bt_vreg_disable(struct bt_power_vreg_data *vreg)
{
	int rc = 0;

	if (!vreg)
		return rc;

	pr_debug("%s: vreg_off for : %s\n", __func__, vreg->name);

	if (!vreg->is_enabled)
		return rc;

	rc = regulator_disable(vreg->reg);
	if (rc < 0) {
		pr_err("%s: regulator_disable(%s) failed. rc=%d\n",
			__func__, vreg->name, rc);
		goto out;
	}
	vreg->is_enabled = false;

	if ((vreg->min_vol != 0) && (vreg->max_vol != 0)) {
		/* Set the min voltage to 0 */
		rc = regulator_set_voltage(vreg->reg, 0, vreg->max_vol);
		if (rc < 0) {
			pr_err("%s: regulator_set_voltage(%s) failed rc=%d\n",
				__func__, vreg->name, rc);
			goto out;
		}
	}
	if (vreg->load_curr >= 0) {
		rc = regulator_set_load(vreg->reg, 0);
		if (rc < 0) {
			pr_err("%s: regulator_set_load(%s) failed rc=%d\n",
				__func__, vreg->name, rc);
		}
	}

out:
	return rc;
}

static int bt_clk_enable(struct bt_power_clk_data *clk)
{
	int rc = 0;

	pr_debug("%s: %s\n", __func__, clk->name);

	/* Get the clock handle for vreg */
	if (!clk->clk || clk->is_enabled) {
		pr_err("%s: error - node: %p, clk->is_enabled:%d\n",
			__func__, clk->clk, clk->is_enabled);
		return -EINVAL;
	}

	rc = clk_prepare_enable(clk->clk);
	if (rc) {
		pr_err("%s: failed to enable %s, rc(%d)\n",
			__func__, clk->name, rc);
		return rc;
	}

	clk->is_enabled = true;
	return rc;
}

static int bt_clk_disable(struct bt_power_clk_data *clk)
{
	pr_debug("%s: %s\n", __func__, clk->name);

	/* Get the clock handle for vreg */
	if (!clk->clk || !clk->is_enabled) {
		pr_err("%s: error - node: %p, clk->is_enabled:%d\n",
			__func__, clk->clk, clk->is_enabled);
		return -EINVAL;
	}
	clk_disable_unprepare(clk->clk);

	clk->is_enabled = false;
	return 0;
}

static void btpower_set_xo_clk_gpio_state(struct btpower_platform_data *drvdata,
					  bool enable)
{
	int xo_clk_gpio = drvdata->xo_gpio_clk;
	int retry = 0;
	int rc = 0;

	if (!gpio_is_valid(xo_clk_gpio))
		return;

retry_gpio_req:
	rc = gpio_request(xo_clk_gpio, "bt_xo_clk_gpio");
	if (rc) {
		if (retry++ < XO_CLK_RETRY_COUNT_MAX) {
			/* wait for ~(10 - 20) ms */
			usleep_range(10000, 20000);
			goto retry_gpio_req;
		}
	}

	if (rc) {
		pr_err("%s: unable to request XO clk gpio %d (%d)\n",
			__func__, xo_clk_gpio, rc);
		return;
	}

	if (enable) {
		gpio_direction_output(xo_clk_gpio, 1);
		/*XO CLK must be asserted for some time before BT_EN */
		usleep_range(100, 200);
	} else {
		/* Assert XO CLK ~(2-5)ms before off for valid latch in HW */
		usleep_range(4000, 6000);
		gpio_direction_output(xo_clk_gpio, 0);
	}

	pr_debug("%s: gpio(%d) success\n", __func__, xo_clk_gpio);

	gpio_free(xo_clk_gpio);
}

static int btpower_gpio_source_request(int gpio, const char *label)
{
	int rc = gpio_request(gpio, label);
	if (rc) {
		pr_err("%s: unable to request gpio %s(%d) (%d)\n", __func__,
			label, gpio, rc);
		return rc;
	}
	return rc;
}

static int btpower_gpio_acquire_output(int gpio, const char *label, bool value)
{
	int rc;

	rc = btpower_gpio_source_request(gpio, label);
	if (rc)
		return rc;

	rc = gpio_direction_output(gpio, value);
	if (rc) {
		pr_err("%s: unable to set output gpio %s(%d) (%d)\n",
			__func__, label, gpio, rc);
		gpio_free(gpio);
		return rc;
	}
	return rc;
}

static int btpower_gpio_acquire_input(int gpio, const char *label)
{
	int rc;

	rc = btpower_gpio_source_request(gpio, label);
	if (rc)
		return rc;

	rc = gpio_direction_input(gpio);
	if (rc) {
		pr_err("%s: unable to set input gpio %s(%d) (%d)\n",
			__func__, label, gpio, rc);
		gpio_free(gpio);
		return rc;
	}
	return rc;
}

static int bt_configure_gpios(struct btpower_platform_data *drvdata, bool on)
{
	int rc = 0;
	int bt_reset_gpio = drvdata->bt_gpio_sys_rst;
	int wl_reset_gpio = drvdata->wl_gpio_sys_rst;
	int bt_sw_ctrl_gpio = drvdata->bt_gpio_sw_ctrl;
	int bt_debug_gpio = drvdata->bt_gpio_debug;

	pr_info("%s: BT_RESET_GPIO(%d) value(%d) enabling: %s\n", __func__,
		bt_reset_gpio, gpio_get_value(bt_reset_gpio),
		(on ? "True" : "False"));

	/* always reset the controller no metter ON or OFF */
	SET_GPIO_SOURCE_STATE(drvdata, bt_reset_gpio, BT_RESET_GPIO, 0);
	msleep(on ? 100 : 50);
	SYNC_GPIO_SOURCE_CURRENT(drvdata, bt_sw_ctrl_gpio, BT_SW_CTRL_GPIO);

	if (!on)
		return 0;

	if (gpio_is_valid(wl_reset_gpio))
		pr_debug("%s: BT-ON wl-reset-gpio(%d) value(%d)\n",
			__func__, wl_reset_gpio, gpio_get_value(wl_reset_gpio));

	if (!gpio_is_valid(wl_reset_gpio) || gpio_get_value(wl_reset_gpio)) {
		btpower_set_xo_clk_gpio_state(drvdata, true);
		pr_info("%s: BT-ON asserting BT_EN (with WLAN)\n", __func__);
		SET_GPIO_SOURCE_STATE(drvdata, bt_reset_gpio, BT_RESET_GPIO, 1);
		btpower_set_xo_clk_gpio_state(drvdata, false);
	}
	if (gpio_is_valid(wl_reset_gpio) && !gpio_get_value(wl_reset_gpio)) {
		if (gpio_get_value(bt_reset_gpio)) {
			pr_warn("%s: WLAN OFF / BT ON too close. Delay BT_EN\n",
				__func__);
			SET_GPIO_SOURCE_STATE(drvdata, bt_reset_gpio,
				BT_RESET_GPIO, 0);
			msleep(100);
			pr_warn("%s: 100ms delay for AON output to fully discharge\n",
				__func__);
		}
		btpower_set_xo_clk_gpio_state(drvdata, true);
		pr_info("%s: BT-ON asserting BT_EN without WLAN\n", __func__);
		SET_GPIO_SOURCE_STATE(drvdata, bt_reset_gpio, BT_RESET_GPIO, 1);
		btpower_set_xo_clk_gpio_state(drvdata, false);
	}

	msleep(50);

	/* Check if SW_CTRL is asserted */
	SYNC_GPIO_SOURCE_CURRENT(drvdata, bt_sw_ctrl_gpio, BT_SW_CTRL_GPIO);
	if (drvdata->bt_power_src_status[BT_SW_CTRL_GPIO_CURRENT] == 0) {
		/* SW_CTRL not asserted, assert debug GPIO */
		if (gpio_is_valid(bt_debug_gpio))
			gpio_set_value(bt_debug_gpio, 1);
		pr_warn("%s: BT_SW_CTRL_GPIO(%d) value(%d) not asserted\n",
			__func__, bt_sw_ctrl_gpio,
			drvdata->bt_power_src_status[BT_SW_CTRL_GPIO_CURRENT]);
	}

	return rc;
}

static int bluetooth_power(struct btpower_platform_data *drvdata,
			   enum bt_power_modes mode)
{
	int rc = 0;

	if (!drvdata) {
		pr_err("%s: device not ready\n", __func__);
		return -ENODEV;
	}

	pr_debug("%s: mode %d -> %d\n", __func__, drvdata->pwr_state, mode);

	switch (mode) {
	case BT_POWER_DISABLE:
		bt_configure_gpios(drvdata, false);
		drvdata->pwr_state = BT_POWER_DISABLE;
		goto clk_disable;
	case BT_POWER_ENABLE:
		rc = bt_power_vreg_set(drvdata, BT_POWER_ENABLE);
		if (rc < 0) {
			pr_err("%s: bt_power regulators config failed\n",
				__func__);
			goto vreg_disable;
		}
		/* Parse dt_info and check if a target requires clock voting.
		 * Enable BT clock when BT is on and disable it when BT is off
		 */
		if (drvdata->bt_chip_clk) {
			rc = bt_clk_enable(drvdata->bt_chip_clk);
			if (rc < 0) {
				pr_err("%s: bt_power gpio config failed\n",
					__func__);
				goto vreg_disable;
			}
		}
		drvdata->bt_power_src_status[BT_RESET_GPIO] =
			DEFAULT_INVALID_VALUE;
		rc = bt_configure_gpios(drvdata, true);
		if (rc < 0) {
			pr_err("%s: bt_power gpio config failed\n", __func__);
			goto clk_disable;
		}
		drvdata->pwr_state = BT_POWER_ENABLE;
		return rc;
	case BT_POWER_RETENTION:
		bt_power_vreg_set(drvdata, BT_POWER_RETENTION);
		drvdata->pwr_state = BT_POWER_RETENTION;
		return rc;
	default:
		pr_err("%s: Invalid power mode: %d\n", __func__, mode);
		return -1;
	}

clk_disable:
	if (drvdata->bt_chip_clk)
		bt_clk_disable(drvdata->bt_chip_clk);
vreg_disable:
	bt_power_vreg_set(drvdata, BT_POWER_DISABLE);
	return rc;
}

static int btpower_toggle_radio(void *data, bool blocked)
{
	struct btpower_platform_data *drvdata = data;
	/* BT-OFF: true; BT-ON: false */
	bool previous_blocked = drvdata->pwr_state == BT_POWER_DISABLE;

	if (previous_blocked != blocked)
		return drvdata->bt_power_setup(drvdata, !blocked);
	return 0;
}

static const struct rfkill_ops btpower_rfkill_ops = {
	.set_block = btpower_toggle_radio,
};

static ssize_t extldo_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	return scnprintf(buf, PAGE_SIZE, "false\n");
}
static DEVICE_ATTR_RO(extldo);

static int btpower_rfkill_probe(struct platform_device *pdev,
				struct btpower_platform_data *drvdata)
{
	struct rfkill *rfkill;
	int ret;

	rfkill = rfkill_alloc("bt_power", &pdev->dev, RFKILL_TYPE_BLUETOOTH,
			      &btpower_rfkill_ops, drvdata);
	if (!rfkill) {
		dev_err(&pdev->dev, "rfkill allocate failed\n");
		return -ENOMEM;
	}

	/* add file into rfkill to handle LDO27 */
	ret = device_create_file(&pdev->dev, &dev_attr_extldo);
	if (ret < 0)
		pr_warn("%s: device create LDO file error (%d)\n",
			__func__, ret);

	/* force Bluetooth off during init to allow for user control */
	rfkill_init_sw_state(rfkill, true);
	drvdata->pwr_state = BT_POWER_DISABLE;
	drvdata->bt_power_setup(drvdata, BT_POWER_DISABLE);

	ret = rfkill_register(rfkill);
	if (ret) {
		dev_err(&pdev->dev, "rfkill register failed=%d\n", ret);
		rfkill_destroy(rfkill);
		return ret;
	}

	drvdata->rfkill = rfkill;

	return 0;
}

static void btpower_rfkill_remove(struct platform_device *pdev)
{
	struct btpower_platform_data *drvdata = platform_get_drvdata(pdev);
	struct rfkill *rfkill;

	pr_debug("%s\n", __func__);

	if (!drvdata || !drvdata->rfkill)
		return;

	rfkill = drvdata->rfkill;
	drvdata->rfkill = NULL;
	device_remove_file(&pdev->dev, &dev_attr_extldo);
	rfkill_unregister(rfkill);
	rfkill_destroy(rfkill);
}

static int btpower_open(struct inode *inode, struct file *filp);
static long btpower_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static const struct file_operations bt_dev_fops = {
	.owner = THIS_MODULE,
	.open = btpower_open,
	.unlocked_ioctl = btpower_ioctl,
	.compat_ioctl = btpower_ioctl,
};

static int btpower_chardev_create(struct btpower_platform_data *drvdata)
{
	dev_t bpdevt;
	struct class *bpcls;
	struct device *bpdev;
	int ret = 0;

	ret = alloc_chrdev_region(&bpdevt, 0, 1, "bt");
	if (ret || MAJOR(bpdevt) < 0) {
		pr_err("%s: failed to register chardev number (%d)\n",
			__func__, ret);
		return ret;
	}
	cdev_init(&drvdata->cdev, &bt_dev_fops);
	drvdata->cdev.owner = THIS_MODULE;
	ret = cdev_add(&drvdata->cdev, bpdevt, 1);
	if (ret) {
		pr_err("%s: failed to add chardev (%d)\n", __func__, ret);
                goto class_err;
	}
	pr_debug("%s: registered chardev number %d:%d\n", __func__,
		MAJOR(drvdata->cdev.dev), MINOR(drvdata->cdev.dev));

	bpcls = class_create(THIS_MODULE, "bt-dev");
	if (IS_ERR_OR_NULL(bpcls)) {
		ret = PTR_ERR(bpcls);
		pr_err("%s: can't create class (%d)\n", __func__, ret);
		goto class_err;
	}

	bpdev = device_create(bpcls, NULL, drvdata->cdev.dev,
			drvdata, "btpower");
	if (IS_ERR_OR_NULL(bpdev)) {
		ret = PTR_ERR(bpdev);
		pr_err("%s: failed to create device with sysfs (%d)\n",
			__func__, ret);
		goto device_err;
	}
	drvdata->cls = bpcls;
	return 0;

device_err:
	class_destroy(bpcls);
class_err:
	unregister_chrdev(MAJOR(drvdata->cdev.dev), "bt");
	return ret;
}

static void btpower_chardev_remove(struct btpower_platform_data *drvdata)
{
	if (!drvdata || !drvdata->cls)
		return;

	device_destroy(drvdata->cls, drvdata->cdev.dev);
	class_destroy(drvdata->cls);
	drvdata->cls = NULL;
	unregister_chrdev(MAJOR(drvdata->cdev.dev), "bt");
}

static int bt_dt_parse_vreg_info(struct device *dev,
				 struct bt_power_vreg_data *vreg)
{
	int len, ret = 0;
	const __be32 *prop;
	char prop_name[MAX_PROP_SIZE];
	struct device_node *np = dev->of_node;
	const char *vreg_name = vreg->name;

	pr_debug("%s: vreg device tree parse for %s\n", __func__, vreg_name);

	snprintf(prop_name, sizeof(prop_name), "%s-supply", vreg_name);
	if (!of_parse_phandle(np, prop_name, 0)) {
		pr_warn("%s: %s is not provided in device tree\n", __func__,
			prop_name);
		return ret;
	}

	vreg->reg = regulator_get(dev, vreg_name);
	if (IS_ERR(vreg->reg)) {
		ret = PTR_ERR(vreg->reg);
		vreg->reg = NULL;
		pr_warn("%s: failed to get: %s error:%d\n", __func__,
			vreg_name, ret);
		return ret;
	}

	snprintf(prop_name, sizeof(prop_name), "%s-config", vreg_name);
	prop = of_get_property(dev->of_node, prop_name, &len);
	if (!prop || len != (4 * sizeof(__be32))) {
		pr_info("%s: Property %s %s, use default\n", __func__,
			prop_name, prop ? "invalid format" : "doesn't exist");
	} else {
		vreg->min_vol = be32_to_cpup(&prop[0]);
		vreg->max_vol = be32_to_cpup(&prop[1]);
		vreg->load_curr = be32_to_cpup(&prop[2]);
		vreg->is_retention_supp = be32_to_cpup(&prop[3]);
	}

	pr_debug("%s: Got regulator: %s, min_vol: %u, max_vol: %u, load_curr: %u, is_retention_supp: %u\n",
		__func__, vreg->name, vreg->min_vol, vreg->max_vol,
		vreg->load_curr, vreg->is_retention_supp);
	return ret;
}

static int bt_dt_parse_clk_info(struct device *dev,
				struct bt_power_clk_data **clk_data)
{
	int ret = -EINVAL;
	struct bt_power_clk_data *clk = NULL;
	struct device_node *np = dev->of_node;

	pr_debug("%s\n", __func__);

	*clk_data = NULL;
	if (!of_parse_phandle(np, "clocks", 0)) {
		pr_err("%s: clocks is not provided in device tree\n", __func__);
		return ret;
	}

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

	/* Parse clock name from node */
	ret = of_property_read_string_index(np, "clock-names", 0, &(clk->name));
	if (ret < 0) {
		pr_err("%s: reading 'clock-names' failed ret=%d\n",
			__func__, ret);
		goto err;
	}

	clk->clk = devm_clk_get(dev, clk->name);
	if (IS_ERR(clk->clk)) {
		ret = PTR_ERR(clk->clk);
		pr_err("%s: failed to get %s ret=%d\n", __func__, clk->name, ret);
		clk->clk = NULL;
		goto err;
	}

	*clk_data = clk;

	return ret;

err:
	devm_kfree(dev, clk);
	return ret;
}

static int bt_power_vreg_get(struct platform_device *pdev,
			     struct btpower_platform_data *drvdata)
{
	int num_vregs, i, ret = 0;
	const struct bt_power *pwrdata = of_device_get_match_data(&pdev->dev);

	if (!pwrdata) {
		pr_err("%s: failed to get dev node\n", __func__);
		return -EINVAL;
	}

	memcpy(&drvdata->compatible, &pwrdata->compatible, sizeof(drvdata->compatible));
	drvdata->vreg_info = pwrdata->vregs;
	num_vregs = drvdata->num_vregs = pwrdata->num_vregs;
	for (i = 0; i < num_vregs; i++) {
		ret = bt_dt_parse_vreg_info(&(pdev->dev), &drvdata->vreg_info[i]);
		/* No point to go further if failed to get regulator handler */
		if (ret)
			break;
	}

	return ret;
}

static int bt_power_vreg_set(struct btpower_platform_data *drvdata,
			     enum bt_power_modes mode)
{
	int num_vregs, i, ret = 0;
	int log_indx;
	struct bt_power_vreg_data *vreg_info = NULL;

	num_vregs = drvdata->num_vregs;
	switch (mode) {
	case BT_POWER_DISABLE:
		for (i = 0; i < num_vregs; i++) {
			vreg_info = &drvdata->vreg_info[i];
			ret = bt_vreg_disable(vreg_info);
		}
		break;
	case BT_POWER_ENABLE:
		for (i = 0; i < num_vregs; i++) {
			vreg_info = &drvdata->vreg_info[i];
			if (!vreg_info->reg)
				continue;
			log_indx = vreg_info->indx.init;
			drvdata->bt_power_src_status[log_indx] =
				DEFAULT_INVALID_VALUE;
			ret = bt_vreg_enable(vreg_info);
			if (ret < 0)
				return ret;
			if (!vreg_info->is_enabled)
				continue;
			drvdata->bt_power_src_status[log_indx] =
				regulator_get_voltage(vreg_info->reg);
		}
		break;
	case BT_POWER_RETENTION:
		for (i = 0; i < num_vregs; i++) {
			vreg_info = &drvdata->vreg_info[i];
			ret = bt_vreg_enable_retention(vreg_info);
		}
		break;
	default:
		pr_err("%s: Invalid power mode: %d\n", __func__, mode);
		ret = -1;
	}
	return ret;
}

static void bt_power_vreg_put(struct btpower_platform_data *drvdata)
{
	int i;
	const struct bt_power_vreg_data *vreg_info = NULL;
	int num_vregs;

	if (!drvdata)
		return;

	num_vregs = drvdata->num_vregs;
	for (i = 0; i < num_vregs; i++) {
		vreg_info = &drvdata->vreg_info[i];
		if (vreg_info->reg)
			regulator_put(vreg_info->reg);
	}
}

static void btpower_gpios_source_release(struct btpower_platform_data *drvdata);
static int btpower_gpios_source_initialize(struct btpower_platform_data *drvdata)
{
	int rc = 0;

	rc = btpower_gpio_acquire_output(drvdata->bt_gpio_sys_rst,
					"bt_sys_rst_n", 0);
	if (rc) {
		drvdata->bt_gpio_sys_rst = -1;
		return rc;
	}

	if (gpio_is_valid(drvdata->bt_gpio_sw_ctrl)) {
		rc = btpower_gpio_acquire_input(drvdata->bt_gpio_sw_ctrl,
						"bt_sw_ctrl_n");
		if (rc) {
			drvdata->bt_gpio_sw_ctrl = -1;
			goto gpio_failure;
		}
	}

	if (gpio_is_valid(drvdata->bt_gpio_debug)) {
		rc = btpower_gpio_acquire_output(drvdata->bt_gpio_debug,
						"bt_debug_n", 0);
		if (rc) {
			drvdata->bt_gpio_debug = -1;
			goto gpio_failure;
		}
	}

	return 0;

gpio_failure:
	btpower_gpios_source_release(drvdata);
	return rc;
}

static void btpower_gpios_source_release(struct btpower_platform_data *drvdata)
{
	if (gpio_is_valid(drvdata->bt_gpio_debug)) {
		gpio_free(drvdata->bt_gpio_debug);
		drvdata->bt_gpio_debug = -1;
	}
	if (gpio_is_valid(drvdata->bt_gpio_sw_ctrl)) {
		gpio_free(drvdata->bt_gpio_sw_ctrl);
		drvdata->bt_gpio_sw_ctrl = -1;
	}
	gpio_free(drvdata->bt_gpio_sys_rst);
	drvdata->bt_gpio_sys_rst = -1;
}

static int bt_power_populate_dt_pinfo(struct platform_device *pdev,
				      struct btpower_platform_data *drvdata)
{
	int rc;

	pr_debug("%s\n", __func__);

	if (!drvdata)
		return -ENOMEM;

	if (!pdev->dev.of_node)
		return 0;

	rc = bt_power_vreg_get(pdev, drvdata);
	if (rc)
		return rc;

	drvdata->bt_gpio_sys_rst =
		of_get_named_gpio(pdev->dev.of_node, "qcom,bt-reset-gpio", 0);
	if (!gpio_is_valid(drvdata->bt_gpio_sys_rst)) {
		pr_err("%s: bt-reset-gpio not provided in device tree\n",
			__func__);
		return -EIO;
	}

	drvdata->wl_gpio_sys_rst =
		of_get_named_gpio(pdev->dev.of_node, "qcom,wl-reset-gpio", 0);
	if (!gpio_is_valid(drvdata->wl_gpio_sys_rst))
		pr_info("%s: wl-reset-gpio not provided in device tree\n",
			__func__);

	drvdata->bt_gpio_sw_ctrl =
		of_get_named_gpio(pdev->dev.of_node, "qcom,bt-sw-ctrl-gpio",  0);
	if (!gpio_is_valid(drvdata->bt_gpio_sw_ctrl))
		pr_info("%s: bt-sw-ctrl-gpio not provided in device tree\n",
			__func__);

	drvdata->bt_gpio_debug =
		of_get_named_gpio(pdev->dev.of_node, "qcom,bt-debug-gpio",  0);
	if (!gpio_is_valid(drvdata->bt_gpio_debug))
		pr_info("%s: bt-debug-gpio not provided in device tree\n",
			__func__);

	drvdata->xo_gpio_clk =
		of_get_named_gpio(pdev->dev.of_node, "qcom,xo-clk-gpio", 0);
	if (!gpio_is_valid(drvdata->xo_gpio_clk))
		pr_info("%s: xo-clk-gpio not provided in device tree\n",
			__func__);

	rc = bt_dt_parse_clk_info(&pdev->dev, &drvdata->bt_chip_clk);
	if (rc < 0)
		pr_info("%s: clock not provided in device tree\n", __func__);

	drvdata->bt_power_setup = bluetooth_power;

	return 0;
}

static int bt_power_probe(struct platform_device *pdev)
{
	struct btpower_platform_data *drvdata, *pdata;
	int ret = 0;
	int itr;

	pr_debug("%s\n", __func__);

	drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
	if (!drvdata)
		return -ENOMEM;

	drvdata->pdev = pdev;
	/* Fill whole array with -2 i.e NOT_AVAILABLE state by default
	 * for any GPIO or Reg handle.
	 */
	for (itr = PWR_SRC_INIT_STATE_IDX; itr < BT_POWER_SRC_SIZE; ++itr)
		drvdata->bt_power_src_status[itr] = PWR_SRC_NOT_AVAILABLE;

	if (pdev->dev.of_node) {
		ret = bt_power_populate_dt_pinfo(pdev, drvdata);
		if (ret < 0) {
			pr_err("%s: Failed to populate device tree info\n",
				__func__);
			goto free_pdata;
		}
		pdev->dev.platform_data = drvdata;
	} else if (pdev->dev.platform_data) {
		pdata = pdev->dev.platform_data;
		/* Optional data set to default if not provided */
		if (!pdata->bt_power_setup)
			pdata->bt_power_setup = bluetooth_power;

		memcpy(drvdata, pdata, sizeof(*drvdata));
	} else {
		pr_err("%s: Failed to get platform data\n", __func__);
		goto free_pdata;
	}
	drvdata->pwr_state = BT_POWER_DISABLE;

	ret = btpower_gpios_source_initialize(drvdata);
	if (ret < 0)
		goto free_pdata;

	ret = btpower_rfkill_probe(pdev, drvdata);
	if (ret < 0)
		goto free_gpio;

	ret = btpower_chardev_create(drvdata);
	if (ret) {
		btpower_rfkill_remove(pdev);
		goto free_gpio;
	}

	btpower_aop_mbox_init(drvdata);

	platform_set_drvdata(pdev, drvdata);

	return 0;

free_gpio:
	btpower_gpios_source_release(drvdata);
free_pdata:
	kfree(drvdata);
	return ret;
}

static int bt_power_remove(struct platform_device *pdev)
{
	struct btpower_platform_data *drvdata = platform_get_drvdata(pdev);

	dev_dbg(&pdev->dev, "%s\n", __func__);

	if (!drvdata)
		return 0;

	btpower_chardev_remove(drvdata);
	btpower_rfkill_remove(pdev);
	bt_power_vreg_put(drvdata);

	btpower_gpios_source_release(drvdata);
	kfree(drvdata);

	return 0;
}

int btpower_register_slimdev(struct device *dev)
{
	struct btpower_platform_data *drvdata;

	pr_debug("%s\n", __func__);
	if (dev == NULL || dev_get_drvdata(dev) == NULL) {
		pr_err("%s: Failed to allocate memory\n", __func__);
		return -EINVAL;
	}
	drvdata = dev_get_drvdata(dev);
	drvdata->slim_dev = dev;
	return 0;
}
EXPORT_SYMBOL(btpower_register_slimdev);

int btpower_get_chipset_version(struct btpower_platform_data *drvdata)
{
	pr_debug("%s\n", __func__);
	return drvdata->chipset_version;
}
EXPORT_SYMBOL(btpower_get_chipset_version);

static void set_pwr_srcs_status(struct btpower_platform_data *drvdata,
				const struct bt_power_vreg_data *handle)
{
	int ldo_index;

	if (!handle)
		return;

	ldo_index = handle->indx.crash;
	if (handle->is_enabled && (regulator_is_enabled(handle->reg))) {
		drvdata->bt_power_src_status[ldo_index] =
			(int)regulator_get_voltage(handle->reg);
		pr_debug("%s(%pK) value(%d)\n", handle->name,
			handle, drvdata->bt_power_src_status[ldo_index]);
	} else {
		drvdata->bt_power_src_status[ldo_index] = DEFAULT_INVALID_VALUE;
		pr_err("%s:%s is_enabled: %d\n", __func__, handle->name,
			handle->is_enabled);
	}
}

static int btpower_open(struct inode *inode, struct file *filp)
{
	filp->private_data =
		container_of(inode->i_cdev, struct btpower_platform_data, cdev);
	return 0;
}

static long btpower_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct btpower_platform_data *drvdata = file->private_data;
	int ret = 0, pwr_cntrl = 0;
	int chipset_version = 0;
	int itr, num_vregs;
	const struct bt_power_vreg_data *vreg_info = NULL;

	if (!drvdata) {
		pr_err("%s: device not ready\n", __func__);
		return -ENODEV;
	}

	switch (cmd) {
	case BT_CMD_SLIM_TEST:
#if IS_ENABLED(CONFIG_BT_SLIM_QCA6390) || \
	IS_ENABLED(CONFIG_BT_SLIM_QCA6490) || \
	IS_ENABLED(CONFIG_BTFM_SLIM_WCN3990) || \
	IS_ENABLED(CONFIG_BTFM_SLIM_WCN7850)
		if (!drvdata->slim_dev) {
			pr_err("%s: slim_dev is null\n", __func__);
			return -EINVAL;
		}
		ret = btfm_slim_hw_init(drvdata->slim_dev->platform_data);
#endif
		break;
	case BT_CMD_PWR_CTRL:
		pwr_cntrl = (enum bt_power_modes)arg;
		if (drvdata->pwr_state == pwr_cntrl) {
			pr_warn("%s: BT_CMD_PWR_CTRL state(%d) already\n",
				__func__, drvdata->pwr_state);
			ret = 0;
			break;
		}
		pr_info("%s: BT_CMD_PWR_CTRL pwr_cntrl: %d\n", __func__,
			pwr_cntrl);
		ret = bluetooth_power(drvdata, pwr_cntrl);
		break;
	case BT_CMD_CHIPSET_VERS:
		chipset_version = (int)arg;
		if (!chipset_version) {
			pr_err("%s: got invalid soc version %x\n", __func__,
				chipset_version);
			drvdata->chipset_version = 0;
			break;
		}
		drvdata->chipset_version = chipset_version;
		pr_info("%s: unified Current SOC Version : %x\n", __func__,
			drvdata->chipset_version);
		break;
	case BT_CMD_GET_CHIPSET_ID:
		if (copy_to_user((void __user *)arg,
				drvdata->compatible, MAX_PROP_SIZE)) {
			ret = -EFAULT;
		}
		break;
	case BT_CMD_CHECK_SW_CTRL:
		/* Check if SW_CTRL is asserted */
		pr_debug("%s: BT_CMD_CHECK_SW_CTRL\n", __func__);
		if (gpio_is_valid(drvdata->bt_gpio_sw_ctrl))
			return -EINVAL;
		SYNC_GPIO_SOURCE_CURRENT(drvdata, drvdata->bt_gpio_sw_ctrl,
			BT_SW_CTRL_GPIO);
		break;
	case BT_CMD_GETVAL_POWER_SRCS:
		pr_debug("%s: BT_CMD_GETVAL_POWER_SRCS\n", __func__);
		SYNC_GPIO_SOURCE_CURRENT(drvdata, drvdata->bt_gpio_sys_rst,
			BT_RESET_GPIO);
		SYNC_GPIO_SOURCE_CURRENT(drvdata, drvdata->bt_gpio_sw_ctrl,
			BT_SW_CTRL_GPIO);
		num_vregs = drvdata->num_vregs;
		for (itr = 0; itr < num_vregs; itr++) {
			vreg_info = &drvdata->vreg_info[itr];
			set_pwr_srcs_status(drvdata, vreg_info);
		}
		if (copy_to_user((void __user *)arg,
				 drvdata->bt_power_src_status,
				 sizeof(drvdata->bt_power_src_status))) {
			ret = -EFAULT;
		}
		break;
	case BT_CMD_SET_IPA_TCS_INFO:
		pr_debug("%s: BT_CMD_SET_IPA_TCS_INFO\n", __func__);
		btpower_enable_ipa_vreg(drvdata);
		break;
	default:
		return -ENOIOCTLCMD;
	}
	return ret;
}

static struct platform_driver bt_power_driver = {
	.probe = bt_power_probe,
	.remove = bt_power_remove,
	.driver = {
		.name = "bt_power",
		.of_match_table = bt_power_match_table,
	},
};

static int __init btpower_init(void)
{
	int ret = 0;

	ret = platform_driver_register(&bt_power_driver);
	if (ret)
		pr_err("%s: platform_driver_register error: %d\n",
			__func__, ret);
	return ret;
}

int btpower_aop_mbox_init(struct btpower_platform_data *drvdata)
{
	struct mbox_client *mbox = &drvdata->mbox_client_data;
	struct mbox_chan *chan;
	int ret = 0;

	mbox->dev = &drvdata->pdev->dev;
	mbox->tx_block = true;
	mbox->tx_tout = BTPOWER_MBOX_TIMEOUT_MS;
	mbox->knows_txdone = false;

	drvdata->mbox_chan = NULL;
	chan = mbox_request_channel(mbox, 0);
	if (IS_ERR(chan)) {
		pr_err("%s: failed to get mbox channel\n", __func__);
		return PTR_ERR(chan);
	}
	drvdata->mbox_chan = chan;

	ret = of_property_read_string(drvdata->pdev->dev.of_node,
		"qcom,vreg_ipa", &drvdata->vreg_ipa);
	if (ret)
		pr_warn("%s: vreg for iPA not provided in device tree\n",
			__func__);
	else
		pr_debug("%s: Mbox channel initialized\n", __func__);

	return 0;
}

static int btpower_aop_set_vreg_param(struct btpower_platform_data *drvdata,
		const char *vreg_name, enum btpower_vreg_param param,
		enum btpower_tcs_seq seq, int val)
{
	struct qmp_pkt pkt;
	char mbox_msg[BTPOWER_MBOX_MSG_MAX_LEN];
	int ret = 0;

	if (!vreg_name || param >= BTPOWER_VREG_PARAM_MAX ||
	    seq >= BTPOWER_TCS_SEQ_MAX)
		return -EINVAL;

	snprintf(mbox_msg, BTPOWER_MBOX_MSG_MAX_LEN,
		 "{class: wlan_pdc, res: %s.%c, %s: %d}", vreg_name,
		 vreg_param_str[param], tcs_seq_str[seq], val);
	pr_debug("%s: sending AOP Mbox msg: %s\n", __func__, mbox_msg);
	pkt.size = BTPOWER_MBOX_MSG_MAX_LEN;
	pkt.data = mbox_msg;
	ret = mbox_send_message(drvdata->mbox_chan, &pkt);
	if (ret < 0)
		pr_err("%s: Failed to send AOP mbox msg(%s) err(%d)\n",
			__func__, mbox_msg, ret);

	return ret;
}

static int btpower_enable_ipa_vreg(struct btpower_platform_data *drvdata)
{
	int ret = 0;

	if (drvdata->vreg_ipa_configured) {
		pr_debug("%s: IPA Vreg already configured\n", __func__);
		return 0;
	}

	if (!drvdata->vreg_ipa || !drvdata->mbox_chan) {
		pr_debug("%s: mbox/iPA vreg not specified\n", __func__);
		return ret;
	}

	ret = btpower_aop_set_vreg_param(drvdata, drvdata->vreg_ipa,
		BTPOWER_VREG_ENABLE, BTPOWER_TCS_UP_SEQ, 1);
	if (ret >= 0) {
		pr_debug("%s: Enabled iPA\n", __func__);
		drvdata->vreg_ipa_configured = true;
	}

	return ret;
}

static void __exit btpower_exit(void)
{
	platform_driver_unregister(&bt_power_driver);
}

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("MSM Bluetooth power control driver");

module_init(btpower_init);
module_exit(btpower_exit);