summaryrefslogtreecommitdiffstats
path: root/drivers/staging/greybus/timesync.c
blob: 2e68af7dea6d51ad8c3ae5657e0af9debaf4a285 (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
/*
 * TimeSync API driver.
 *
 * Copyright 2016 Google Inc.
 * Copyright 2016 Linaro Ltd.
 *
 * Released under the GPLv2 only.
 */
#include <linux/debugfs.h>
#include <linux/hrtimer.h>
#include "greybus.h"
#include "timesync.h"
#include "greybus_trace.h"

/*
 * Minimum inter-strobe value of one millisecond is chosen because it
 * just-about fits the common definition of a jiffy.
 *
 * Maximum value OTOH is constrained by the number of bits the SVC can fit
 * into a 16 bit up-counter. The SVC configures the timer in microseconds
 * so the maximum allowable value is 65535 microseconds. We clip that value
 * to 10000 microseconds for the sake of using nice round base 10 numbers
 * and since right-now there's no imaginable use-case requiring anything
 * other than a one millisecond inter-strobe time, let alone something
 * higher than ten milliseconds.
 */
#define GB_TIMESYNC_STROBE_DELAY_US		1000
#define GB_TIMESYNC_DEFAULT_OFFSET_US		1000

/* Work queue timers long, short and SVC strobe timeout */
#define GB_TIMESYNC_DELAYED_WORK_LONG		msecs_to_jiffies(10)
#define GB_TIMESYNC_DELAYED_WORK_SHORT		msecs_to_jiffies(1)
#define GB_TIMESYNC_MAX_WAIT_SVC		msecs_to_jiffies(5000)
#define GB_TIMESYNC_KTIME_UPDATE		msecs_to_jiffies(1000)
#define GB_TIMESYNC_MAX_KTIME_CONVERSION	15

/* Maximum number of times we'll retry a failed synchronous sync */
#define GB_TIMESYNC_MAX_RETRIES			5

/* Reported nanoseconds/femtoseconds per clock */
static u64 gb_timesync_ns_per_clock;
static u64 gb_timesync_fs_per_clock;

/* Maximum difference we will accept converting FrameTime to ktime */
static u32 gb_timesync_max_ktime_diff;

/* Reported clock rate */
static unsigned long gb_timesync_clock_rate;

/* Workqueue */
static void gb_timesync_worker(struct work_struct *work);

/* List of SVCs with one FrameTime per SVC */
static LIST_HEAD(gb_timesync_svc_list);

/* Synchronize parallel contexts accessing a valid timesync_svc pointer */
static DEFINE_MUTEX(gb_timesync_svc_list_mutex);

/* Structure to convert from FrameTime to timespec/ktime */
struct gb_timesync_frame_time_data {
	u64 frame_time;
	struct timespec ts;
};

struct gb_timesync_svc {
	struct list_head list;
	struct list_head interface_list;
	struct gb_svc *svc;
	struct gb_timesync_host_device *timesync_hd;

	spinlock_t spinlock;	/* Per SVC spinlock to sync with ISR */
	struct mutex mutex;	/* Per SVC mutex for regular synchronization */

	struct dentry *frame_time_dentry;
	struct dentry *frame_ktime_dentry;
	struct workqueue_struct *work_queue;
	wait_queue_head_t wait_queue;
	struct delayed_work delayed_work;
	struct timer_list ktime_timer;

	/* The current local FrameTime */
	u64 frame_time_offset;
	struct gb_timesync_frame_time_data strobe_data[GB_TIMESYNC_MAX_STROBES];
	struct gb_timesync_frame_time_data ktime_data;

	/* The SVC FrameTime and relative AP FrameTime @ last TIMESYNC_PING */
	u64 svc_ping_frame_time;
	u64 ap_ping_frame_time;

	/* Transitory settings */
	u32 strobe_mask;
	bool offset_down;
	bool print_ping;
	bool capture_ping;
	int strobe;

	/* Current state */
	int state;
};

struct gb_timesync_host_device {
	struct list_head list;
	struct gb_host_device *hd;
	u64 ping_frame_time;
};

struct gb_timesync_interface {
	struct list_head list;
	struct gb_interface *interface;
	u64 ping_frame_time;
};

enum gb_timesync_state {
	GB_TIMESYNC_STATE_INVALID		= 0,
	GB_TIMESYNC_STATE_INACTIVE		= 1,
	GB_TIMESYNC_STATE_INIT			= 2,
	GB_TIMESYNC_STATE_WAIT_SVC		= 3,
	GB_TIMESYNC_STATE_AUTHORITATIVE		= 4,
	GB_TIMESYNC_STATE_PING			= 5,
	GB_TIMESYNC_STATE_ACTIVE		= 6,
};

static void gb_timesync_ktime_timer_fn(unsigned long data);

static u64 gb_timesync_adjust_count(struct gb_timesync_svc *timesync_svc,
				    u64 counts)
{
	if (timesync_svc->offset_down)
		return counts - timesync_svc->frame_time_offset;
	else
		return counts + timesync_svc->frame_time_offset;
}

/*
 * This function provides the authoritative FrameTime to a calling function. It
 * is designed to be lockless and should remain that way the caller is assumed
 * to be state-aware.
 */
static u64 __gb_timesync_get_frame_time(struct gb_timesync_svc *timesync_svc)
{
	u64 clocks = gb_timesync_platform_get_counter();

	return gb_timesync_adjust_count(timesync_svc, clocks);
}

static void gb_timesync_schedule_svc_timeout(struct gb_timesync_svc
					     *timesync_svc)
{
	queue_delayed_work(timesync_svc->work_queue,
			   &timesync_svc->delayed_work,
			   GB_TIMESYNC_MAX_WAIT_SVC);
}

static void gb_timesync_set_state(struct gb_timesync_svc *timesync_svc,
				  int state)
{
	switch (state) {
	case GB_TIMESYNC_STATE_INVALID:
		timesync_svc->state = state;
		wake_up(&timesync_svc->wait_queue);
		break;
	case GB_TIMESYNC_STATE_INACTIVE:
		timesync_svc->state = state;
		wake_up(&timesync_svc->wait_queue);
		break;
	case GB_TIMESYNC_STATE_INIT:
		if (timesync_svc->state != GB_TIMESYNC_STATE_INVALID) {
			timesync_svc->strobe = 0;
			timesync_svc->frame_time_offset = 0;
			timesync_svc->state = state;
			cancel_delayed_work(&timesync_svc->delayed_work);
			queue_delayed_work(timesync_svc->work_queue,
					   &timesync_svc->delayed_work,
					   GB_TIMESYNC_DELAYED_WORK_LONG);
		}
		break;
	case GB_TIMESYNC_STATE_WAIT_SVC:
		if (timesync_svc->state == GB_TIMESYNC_STATE_INIT)
			timesync_svc->state = state;
		break;
	case GB_TIMESYNC_STATE_AUTHORITATIVE:
		if (timesync_svc->state == GB_TIMESYNC_STATE_WAIT_SVC) {
			timesync_svc->state = state;
			cancel_delayed_work(&timesync_svc->delayed_work);
			queue_delayed_work(timesync_svc->work_queue,
					   &timesync_svc->delayed_work, 0);
		}
		break;
	case GB_TIMESYNC_STATE_PING:
		if (timesync_svc->state == GB_TIMESYNC_STATE_ACTIVE) {
			timesync_svc->state = state;
			queue_delayed_work(timesync_svc->work_queue,
					   &timesync_svc->delayed_work,
					   GB_TIMESYNC_DELAYED_WORK_SHORT);
		}
		break;
	case GB_TIMESYNC_STATE_ACTIVE:
		if (timesync_svc->state == GB_TIMESYNC_STATE_AUTHORITATIVE ||
		    timesync_svc->state == GB_TIMESYNC_STATE_PING) {
			timesync_svc->state = state;
			wake_up(&timesync_svc->wait_queue);
		}
		break;
	}

	if (WARN_ON(timesync_svc->state != state)) {
		pr_err("Invalid state transition %d=>%d\n",
		       timesync_svc->state, state);
	}
}

static void gb_timesync_set_state_atomic(struct gb_timesync_svc *timesync_svc,
					 int state)
{
	unsigned long flags;

	spin_lock_irqsave(&timesync_svc->spinlock, flags);
	gb_timesync_set_state(timesync_svc, state);
	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
}

static u64 gb_timesync_diff(u64 x, u64 y)
{
	if (x > y)
		return x - y;
	else
		return y - x;
}

static void gb_timesync_adjust_to_svc(struct gb_timesync_svc *svc,
				      u64 svc_frame_time, u64 ap_frame_time)
{
	if (svc_frame_time > ap_frame_time) {
		svc->frame_time_offset = svc_frame_time - ap_frame_time;
		svc->offset_down = false;
	} else {
		svc->frame_time_offset = ap_frame_time - svc_frame_time;
		svc->offset_down = true;
	}
}

/*
 * Associate a FrameTime with a ktime timestamp represented as struct timespec
 * Requires the calling context to hold timesync_svc->mutex
 */
static void gb_timesync_store_ktime(struct gb_timesync_svc *timesync_svc,
				    struct timespec ts, u64 frame_time)
{
	timesync_svc->ktime_data.ts = ts;
	timesync_svc->ktime_data.frame_time = frame_time;
}

/*
 * Find the two pulses that best-match our expected inter-strobe gap and
 * then calculate the difference between the SVC time at the second pulse
 * to the local time at the second pulse.
 */
static void gb_timesync_collate_frame_time(struct gb_timesync_svc *timesync_svc,
					   u64 *frame_time)
{
	int i = 0;
	u64 delta, ap_frame_time;
	u64 strobe_delay_ns = GB_TIMESYNC_STROBE_DELAY_US * NSEC_PER_USEC;
	u64 least = 0;

	for (i = 1; i < GB_TIMESYNC_MAX_STROBES; i++) {
		delta = timesync_svc->strobe_data[i].frame_time -
			timesync_svc->strobe_data[i - 1].frame_time;
		delta *= gb_timesync_ns_per_clock;
		delta = gb_timesync_diff(delta, strobe_delay_ns);

		if (!least || delta < least) {
			least = delta;
			gb_timesync_adjust_to_svc(timesync_svc, frame_time[i],
						  timesync_svc->strobe_data[i].frame_time);

			ap_frame_time = timesync_svc->strobe_data[i].frame_time;
			ap_frame_time = gb_timesync_adjust_count(timesync_svc,
								 ap_frame_time);
			gb_timesync_store_ktime(timesync_svc,
						timesync_svc->strobe_data[i].ts,
						ap_frame_time);

			pr_debug("adjust %s local %llu svc %llu delta %llu\n",
				 timesync_svc->offset_down ? "down" : "up",
				 timesync_svc->strobe_data[i].frame_time,
				 frame_time[i], delta);
		}
	}
}

static void gb_timesync_teardown(struct gb_timesync_svc *timesync_svc)
{
	struct gb_timesync_interface *timesync_interface;
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_interface *interface;
	struct gb_host_device *hd;
	int ret;

	list_for_each_entry(timesync_interface,
			    &timesync_svc->interface_list, list) {
		interface = timesync_interface->interface;
		ret = gb_interface_timesync_disable(interface);
		if (ret) {
			dev_err(&interface->dev,
				"interface timesync_disable %d\n", ret);
		}
	}

	hd = timesync_svc->timesync_hd->hd;
	ret = hd->driver->timesync_disable(hd);
	if (ret < 0) {
		dev_err(&hd->dev, "host timesync_disable %d\n",
			ret);
	}

	gb_svc_timesync_wake_pins_release(svc);
	gb_svc_timesync_disable(svc);
	gb_timesync_platform_unlock_bus();

	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_INACTIVE);
}

static void gb_timesync_platform_lock_bus_fail(struct gb_timesync_svc
						*timesync_svc, int ret)
{
	if (ret == -EAGAIN) {
		gb_timesync_set_state(timesync_svc, timesync_svc->state);
	} else {
		pr_err("Failed to lock timesync bus %d\n", ret);
		gb_timesync_set_state(timesync_svc, GB_TIMESYNC_STATE_INACTIVE);
	}
}

static void gb_timesync_enable(struct gb_timesync_svc *timesync_svc)
{
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_host_device *hd;
	struct gb_timesync_interface *timesync_interface;
	struct gb_interface *interface;
	u64 init_frame_time;
	unsigned long clock_rate = gb_timesync_clock_rate;
	int ret;

	/*
	 * Get access to the wake pins in the AP and SVC
	 * Release these pins either in gb_timesync_teardown() or in
	 * gb_timesync_authoritative()
	 */
	ret = gb_timesync_platform_lock_bus(timesync_svc);
	if (ret < 0) {
		gb_timesync_platform_lock_bus_fail(timesync_svc, ret);
		return;
	}
	ret = gb_svc_timesync_wake_pins_acquire(svc, timesync_svc->strobe_mask);
	if (ret) {
		dev_err(&svc->dev,
			"gb_svc_timesync_wake_pins_acquire %d\n", ret);
		gb_timesync_teardown(timesync_svc);
		return;
	}

	/* Choose an initial time in the future */
	init_frame_time = __gb_timesync_get_frame_time(timesync_svc) + 100000UL;

	/* Send enable command to all relevant participants */
	list_for_each_entry(timesync_interface, &timesync_svc->interface_list,
			    list) {
		interface = timesync_interface->interface;
		ret = gb_interface_timesync_enable(interface,
						   GB_TIMESYNC_MAX_STROBES,
						   init_frame_time,
						   GB_TIMESYNC_STROBE_DELAY_US,
						   clock_rate);
		if (ret) {
			dev_err(&interface->dev,
				"interface timesync_enable %d\n", ret);
		}
	}

	hd = timesync_svc->timesync_hd->hd;
	ret = hd->driver->timesync_enable(hd, GB_TIMESYNC_MAX_STROBES,
					  init_frame_time,
					  GB_TIMESYNC_STROBE_DELAY_US,
					  clock_rate);
	if (ret < 0) {
		dev_err(&hd->dev, "host timesync_enable %d\n",
			ret);
	}

	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_WAIT_SVC);
	ret = gb_svc_timesync_enable(svc, GB_TIMESYNC_MAX_STROBES,
				     init_frame_time,
				     GB_TIMESYNC_STROBE_DELAY_US,
				     clock_rate);
	if (ret) {
		dev_err(&svc->dev,
			"gb_svc_timesync_enable %d\n", ret);
		gb_timesync_teardown(timesync_svc);
		return;
	}

	/* Schedule a timeout waiting for SVC to complete strobing */
	gb_timesync_schedule_svc_timeout(timesync_svc);
}

static void gb_timesync_authoritative(struct gb_timesync_svc *timesync_svc)
{
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_host_device *hd;
	struct gb_timesync_interface *timesync_interface;
	struct gb_interface *interface;
	u64 svc_frame_time[GB_TIMESYNC_MAX_STROBES];
	int ret;

	/* Get authoritative time from SVC and adjust local clock */
	ret = gb_svc_timesync_authoritative(svc, svc_frame_time);
	if (ret) {
		dev_err(&svc->dev,
			"gb_svc_timesync_authoritative %d\n", ret);
		gb_timesync_teardown(timesync_svc);
		return;
	}
	gb_timesync_collate_frame_time(timesync_svc, svc_frame_time);

	/* Transmit authoritative time to downstream slaves */
	hd = timesync_svc->timesync_hd->hd;
	ret = hd->driver->timesync_authoritative(hd, svc_frame_time);
	if (ret < 0)
		dev_err(&hd->dev, "host timesync_authoritative %d\n", ret);

	list_for_each_entry(timesync_interface,
			    &timesync_svc->interface_list, list) {
		interface = timesync_interface->interface;
		ret = gb_interface_timesync_authoritative(
						interface,
						svc_frame_time);
		if (ret) {
			dev_err(&interface->dev,
				"interface timesync_authoritative %d\n", ret);
		}
	}

	/* Release wake pins */
	gb_svc_timesync_wake_pins_release(svc);
	gb_timesync_platform_unlock_bus();

	/* Transition to state ACTIVE */
	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_ACTIVE);

	/* Schedule a ping to verify the synchronized system time */
	timesync_svc->print_ping = true;
	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_PING);
}

static int __gb_timesync_get_status(struct gb_timesync_svc *timesync_svc)
{
	int ret = -EINVAL;

	switch (timesync_svc->state) {
	case GB_TIMESYNC_STATE_INVALID:
	case GB_TIMESYNC_STATE_INACTIVE:
		ret = -ENODEV;
		break;
	case GB_TIMESYNC_STATE_INIT:
	case GB_TIMESYNC_STATE_WAIT_SVC:
	case GB_TIMESYNC_STATE_AUTHORITATIVE:
		ret = -EAGAIN;
		break;
	case GB_TIMESYNC_STATE_PING:
	case GB_TIMESYNC_STATE_ACTIVE:
		ret = 0;
		break;
	}
	return ret;
}

/*
 * This routine takes a FrameTime and derives the difference with-respect
 * to a reference FrameTime/ktime pair. It then returns the calculated
 * ktime based on the difference between the supplied FrameTime and
 * the reference FrameTime.
 *
 * The time difference is calculated to six decimal places. Taking 19.2MHz
 * as an example this means we have 52.083333~ nanoseconds per clock or
 * 52083333~ femtoseconds per clock.
 *
 * Naively taking the count difference and converting to
 * seconds/nanoseconds would quickly see the 0.0833 component produce
 * noticeable errors. For example a time difference of one second would
 * loose 19200000 * 0.08333x nanoseconds or 1.59 seconds.
 *
 * In contrast calculating in femtoseconds the same example of 19200000 *
 * 0.000000083333x nanoseconds per count of error is just 1.59 nanoseconds!
 *
 * Continuing the example of 19.2 MHz we cap the maximum error difference
 * at a worst-case 0.3 microseconds over a potential calculation window of
 * abount 15 seconds, meaning you can convert a FrameTime that is <= 15
 * seconds older/younger than the reference time with a maximum error of
 * 0.2385 useconds. Note 19.2MHz is an example frequency not a requirement.
 */
static int gb_timesync_to_timespec(struct gb_timesync_svc *timesync_svc,
				   u64 frame_time, struct timespec *ts)
{
	unsigned long flags;
	u64 delta_fs, counts, sec, nsec;
	bool add;
	int ret = 0;

	memset(ts, 0x00, sizeof(*ts));
	mutex_lock(&timesync_svc->mutex);
	spin_lock_irqsave(&timesync_svc->spinlock, flags);

	ret = __gb_timesync_get_status(timesync_svc);
	if (ret)
		goto done;

	/* Support calculating ktime upwards or downwards from the reference */
	if (frame_time < timesync_svc->ktime_data.frame_time) {
		add = false;
		counts = timesync_svc->ktime_data.frame_time - frame_time;
	} else {
		add = true;
		counts = frame_time - timesync_svc->ktime_data.frame_time;
	}

	/* Enforce the .23 of a usecond boundary @ 19.2MHz */
	if (counts > gb_timesync_max_ktime_diff) {
		ret = -EINVAL;
		goto done;
	}

	/* Determine the time difference in femtoseconds */
	delta_fs = counts * gb_timesync_fs_per_clock;

	/* Convert to seconds */
	sec = delta_fs;
	do_div(sec, NSEC_PER_SEC);
	do_div(sec, 1000000UL);

	/* Get the nanosecond remainder */
	nsec = do_div(delta_fs, sec);
	do_div(nsec, 1000000UL);

	if (add) {
		/* Add the calculated offset - overflow nanoseconds upwards */
		ts->tv_sec = timesync_svc->ktime_data.ts.tv_sec + sec;
		ts->tv_nsec = timesync_svc->ktime_data.ts.tv_nsec + nsec;
		if (ts->tv_nsec >= NSEC_PER_SEC) {
			ts->tv_sec++;
			ts->tv_nsec -= NSEC_PER_SEC;
		}
	} else {
		/* Subtract the difference over/underflow as necessary */
		if (nsec > timesync_svc->ktime_data.ts.tv_nsec) {
			sec++;
			nsec = nsec + timesync_svc->ktime_data.ts.tv_nsec;
			nsec = do_div(nsec, NSEC_PER_SEC);
		} else {
			nsec = timesync_svc->ktime_data.ts.tv_nsec - nsec;
		}
		/* Cannot return a negative second value */
		if (sec > timesync_svc->ktime_data.ts.tv_sec) {
			ret = -EINVAL;
			goto done;
		}
		ts->tv_sec = timesync_svc->ktime_data.ts.tv_sec - sec;
		ts->tv_nsec = nsec;
	}
done:
	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
	mutex_unlock(&timesync_svc->mutex);
	return ret;
}

static size_t gb_timesync_log_frame_time(struct gb_timesync_svc *timesync_svc,
					 char *buf, size_t buflen)
{
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_host_device *hd;
	struct gb_timesync_interface *timesync_interface;
	struct gb_interface *interface;
	unsigned int len;
	size_t off;

	/* AP/SVC */
	off = snprintf(buf, buflen, "%s frametime: ap=%llu %s=%llu ",
		       greybus_bus_type.name,
		       timesync_svc->ap_ping_frame_time, dev_name(&svc->dev),
		       timesync_svc->svc_ping_frame_time);
	len = buflen - off;

	/* APB/GPB */
	if (len < buflen) {
		hd = timesync_svc->timesync_hd->hd;
		off += snprintf(&buf[off], len, "%s=%llu ", dev_name(&hd->dev),
				timesync_svc->timesync_hd->ping_frame_time);
		len = buflen - off;
	}

	list_for_each_entry(timesync_interface,
			    &timesync_svc->interface_list, list) {
		if (len < buflen) {
			interface = timesync_interface->interface;
			off += snprintf(&buf[off], len, "%s=%llu ",
					dev_name(&interface->dev),
					timesync_interface->ping_frame_time);
			len = buflen - off;
		}
	}
	if (len < buflen)
		off += snprintf(&buf[off], len, "\n");
	return off;
}

static size_t gb_timesync_log_frame_ktime(struct gb_timesync_svc *timesync_svc,
					  char *buf, size_t buflen)
{
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_host_device *hd;
	struct gb_timesync_interface *timesync_interface;
	struct gb_interface *interface;
	struct timespec ts;
	unsigned int len;
	size_t off;

	/* AP */
	gb_timesync_to_timespec(timesync_svc, timesync_svc->ap_ping_frame_time,
				&ts);
	off = snprintf(buf, buflen, "%s frametime: ap=%lu.%lu ",
		       greybus_bus_type.name, ts.tv_sec, ts.tv_nsec);
	len = buflen - off;
	if (len >= buflen)
		goto done;

	/* SVC */
	gb_timesync_to_timespec(timesync_svc, timesync_svc->svc_ping_frame_time,
				&ts);
	off += snprintf(&buf[off], len, "%s=%lu.%lu ", dev_name(&svc->dev),
			ts.tv_sec, ts.tv_nsec);
	len = buflen - off;
	if (len >= buflen)
		goto done;

	/* APB/GPB */
	hd = timesync_svc->timesync_hd->hd;
	gb_timesync_to_timespec(timesync_svc,
				timesync_svc->timesync_hd->ping_frame_time,
				&ts);
	off += snprintf(&buf[off], len, "%s=%lu.%lu ",
			dev_name(&hd->dev),
			ts.tv_sec, ts.tv_nsec);
	len = buflen - off;
	if (len >= buflen)
		goto done;

	list_for_each_entry(timesync_interface,
			    &timesync_svc->interface_list, list) {
		interface = timesync_interface->interface;
		gb_timesync_to_timespec(timesync_svc,
					timesync_interface->ping_frame_time,
					&ts);
		off += snprintf(&buf[off], len, "%s=%lu.%lu ",
				dev_name(&interface->dev),
				ts.tv_sec, ts.tv_nsec);
		len = buflen - off;
		if (len >= buflen)
			goto done;
	}
	off += snprintf(&buf[off], len, "\n");
done:
	return off;
}

/*
 * Send an SVC initiated wake 'ping' to each TimeSync participant.
 * Get the FrameTime from each participant associated with the wake
 * ping.
 */
static void gb_timesync_ping(struct gb_timesync_svc *timesync_svc)
{
	struct gb_svc *svc = timesync_svc->svc;
	struct gb_host_device *hd;
	struct gb_timesync_interface *timesync_interface;
	struct gb_control *control;
	u64 *ping_frame_time;
	int ret;

	/* Get access to the wake pins in the AP and SVC */
	ret = gb_timesync_platform_lock_bus(timesync_svc);
	if (ret < 0) {
		gb_timesync_platform_lock_bus_fail(timesync_svc, ret);
		return;
	}
	ret = gb_svc_timesync_wake_pins_acquire(svc, timesync_svc->strobe_mask);
	if (ret) {
		dev_err(&svc->dev,
			"gb_svc_timesync_wake_pins_acquire %d\n", ret);
		gb_timesync_teardown(timesync_svc);
		return;
	}

	/* Have SVC generate a timesync ping */
	timesync_svc->capture_ping = true;
	timesync_svc->svc_ping_frame_time = 0;
	ret = gb_svc_timesync_ping(svc, &timesync_svc->svc_ping_frame_time);
	timesync_svc->capture_ping = false;
	if (ret) {
		dev_err(&svc->dev,
			"gb_svc_timesync_ping %d\n", ret);
		gb_timesync_teardown(timesync_svc);
		return;
	}

	/* Get the ping FrameTime from each APB/GPB */
	hd = timesync_svc->timesync_hd->hd;
	timesync_svc->timesync_hd->ping_frame_time = 0;
	ret = hd->driver->timesync_get_last_event(hd,
		&timesync_svc->timesync_hd->ping_frame_time);
	if (ret)
		dev_err(&hd->dev, "host timesync_get_last_event %d\n", ret);

	list_for_each_entry(timesync_interface,
			    &timesync_svc->interface_list, list) {
		control = timesync_interface->interface->control;
		timesync_interface->ping_frame_time = 0;
		ping_frame_time = &timesync_interface->ping_frame_time;
		ret = gb_control_timesync_get_last_event(control,
							 ping_frame_time);
		if (ret) {
			dev_err(&timesync_interface->interface->dev,
				"gb_control_timesync_get_last_event %d\n", ret);
		}
	}

	/* Ping success - move to timesync active */
	gb_svc_timesync_wake_pins_release(svc);
	gb_timesync_platform_unlock_bus();
	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_ACTIVE);
}

static void gb_timesync_log_ping_time(struct gb_timesync_svc *timesync_svc)
{
	char *buf;

	if (!timesync_svc->print_ping)
		return;

	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (buf) {
		gb_timesync_log_frame_time(timesync_svc, buf, PAGE_SIZE);
		dev_dbg(&timesync_svc->svc->dev, "%s", buf);
		kfree(buf);
	}
}

/*
 * Perform the actual work of scheduled TimeSync logic.
 */
static void gb_timesync_worker(struct work_struct *work)
{
	struct delayed_work *delayed_work = to_delayed_work(work);
	struct gb_timesync_svc *timesync_svc =
		container_of(delayed_work, struct gb_timesync_svc, delayed_work);

	mutex_lock(&timesync_svc->mutex);

	switch (timesync_svc->state) {
	case GB_TIMESYNC_STATE_INIT:
		gb_timesync_enable(timesync_svc);
		break;

	case GB_TIMESYNC_STATE_WAIT_SVC:
		dev_err(&timesync_svc->svc->dev,
			"timeout SVC strobe completion %d/%d\n",
			timesync_svc->strobe, GB_TIMESYNC_MAX_STROBES);
		gb_timesync_teardown(timesync_svc);
		break;

	case GB_TIMESYNC_STATE_AUTHORITATIVE:
		gb_timesync_authoritative(timesync_svc);
		break;

	case GB_TIMESYNC_STATE_PING:
		gb_timesync_ping(timesync_svc);
		gb_timesync_log_ping_time(timesync_svc);
		break;

	default:
		pr_err("Invalid state %d for delayed work\n",
		       timesync_svc->state);
		break;
	}

	mutex_unlock(&timesync_svc->mutex);
}

/*
 * Schedule a new TimeSync INIT or PING operation serialized w/r to
 * gb_timesync_worker().
 */
static int gb_timesync_schedule(struct gb_timesync_svc *timesync_svc, int state)
{
	int ret = 0;

	if (state != GB_TIMESYNC_STATE_INIT && state != GB_TIMESYNC_STATE_PING)
		return -EINVAL;

	mutex_lock(&timesync_svc->mutex);
	if (timesync_svc->state !=  GB_TIMESYNC_STATE_INVALID) {
		gb_timesync_set_state_atomic(timesync_svc, state);
	} else {
		ret = -ENODEV;
	}
	mutex_unlock(&timesync_svc->mutex);
	return ret;
}

static int __gb_timesync_schedule_synchronous(
	struct gb_timesync_svc *timesync_svc, int state)
{
	unsigned long flags;
	int ret;

	ret = gb_timesync_schedule(timesync_svc, state);
	if (ret)
		return ret;

	ret = wait_event_interruptible(timesync_svc->wait_queue,
			(timesync_svc->state == GB_TIMESYNC_STATE_ACTIVE ||
			 timesync_svc->state == GB_TIMESYNC_STATE_INACTIVE ||
			 timesync_svc->state == GB_TIMESYNC_STATE_INVALID));
	if (ret)
		return ret;

	mutex_lock(&timesync_svc->mutex);
	spin_lock_irqsave(&timesync_svc->spinlock, flags);

	ret = __gb_timesync_get_status(timesync_svc);

	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
	mutex_unlock(&timesync_svc->mutex);

	return ret;
}

static struct gb_timesync_svc *gb_timesync_find_timesync_svc(
	struct gb_host_device *hd)
{
	struct gb_timesync_svc *timesync_svc;

	list_for_each_entry(timesync_svc, &gb_timesync_svc_list, list) {
		if (timesync_svc->svc == hd->svc)
			return timesync_svc;
	}
	return NULL;
}

static struct gb_timesync_interface *gb_timesync_find_timesync_interface(
	struct gb_timesync_svc *timesync_svc,
	struct gb_interface *interface)
{
	struct gb_timesync_interface *timesync_interface;

	list_for_each_entry(timesync_interface, &timesync_svc->interface_list, list) {
		if (timesync_interface->interface == interface)
			return timesync_interface;
	}
	return NULL;
}

int gb_timesync_schedule_synchronous(struct gb_interface *interface)
{
	int ret;
	struct gb_timesync_svc *timesync_svc;
	int retries;

	if (!(interface->features & GREYBUS_INTERFACE_FEATURE_TIMESYNC))
		return 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	for (retries = 0; retries < GB_TIMESYNC_MAX_RETRIES; retries++) {
		timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
		if (!timesync_svc) {
			ret = -ENODEV;
			goto done;
		}

		ret = __gb_timesync_schedule_synchronous(timesync_svc,
						 GB_TIMESYNC_STATE_INIT);
		if (!ret)
			break;
	}
	if (ret && retries == GB_TIMESYNC_MAX_RETRIES)
		ret = -ETIMEDOUT;
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_schedule_synchronous);

void gb_timesync_schedule_asynchronous(struct gb_interface *interface)
{
	struct gb_timesync_svc *timesync_svc;

	if (!(interface->features & GREYBUS_INTERFACE_FEATURE_TIMESYNC))
		return;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
	if (!timesync_svc)
		goto done;

	gb_timesync_schedule(timesync_svc, GB_TIMESYNC_STATE_INIT);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return;
}
EXPORT_SYMBOL_GPL(gb_timesync_schedule_asynchronous);

static ssize_t gb_timesync_ping_read(struct file *file, char __user *ubuf,
				     size_t len, loff_t *offset, bool ktime)
{
	struct gb_timesync_svc *timesync_svc = file->f_inode->i_private;
	char *buf;
	ssize_t ret = 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	mutex_lock(&timesync_svc->mutex);
	if (list_empty(&timesync_svc->interface_list))
		ret = -ENODEV;
	timesync_svc->print_ping = false;
	mutex_unlock(&timesync_svc->mutex);
	if (ret)
		goto done;

	ret = __gb_timesync_schedule_synchronous(timesync_svc,
						 GB_TIMESYNC_STATE_PING);
	if (ret)
		goto done;

	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!buf) {
		ret = -ENOMEM;
		goto done;
	}

	if (ktime)
		ret = gb_timesync_log_frame_ktime(timesync_svc, buf, PAGE_SIZE);
	else
		ret = gb_timesync_log_frame_time(timesync_svc, buf, PAGE_SIZE);
	if (ret > 0)
		ret = simple_read_from_buffer(ubuf, len, offset, buf, ret);
	kfree(buf);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}

static ssize_t gb_timesync_ping_read_frame_time(struct file *file,
						char __user *buf,
						size_t len, loff_t *offset)
{
	return gb_timesync_ping_read(file, buf, len, offset, false);
}

static ssize_t gb_timesync_ping_read_frame_ktime(struct file *file,
						 char __user *buf,
						 size_t len, loff_t *offset)
{
	return gb_timesync_ping_read(file, buf, len, offset, true);
}

static const struct file_operations gb_timesync_debugfs_frame_time_ops = {
	.read		= gb_timesync_ping_read_frame_time,
};

static const struct file_operations gb_timesync_debugfs_frame_ktime_ops = {
	.read		= gb_timesync_ping_read_frame_ktime,
};

static int gb_timesync_hd_add(struct gb_timesync_svc *timesync_svc,
			      struct gb_host_device *hd)
{
	struct gb_timesync_host_device *timesync_hd;

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

	WARN_ON(timesync_svc->timesync_hd);
	timesync_hd->hd = hd;
	timesync_svc->timesync_hd = timesync_hd;

	return 0;
}

static void gb_timesync_hd_remove(struct gb_timesync_svc *timesync_svc,
				  struct gb_host_device *hd)
{
	if (timesync_svc->timesync_hd->hd == hd) {
		kfree(timesync_svc->timesync_hd);
		timesync_svc->timesync_hd = NULL;
		return;
	}
	WARN_ON(1);
}

int gb_timesync_svc_add(struct gb_svc *svc)
{
	struct gb_timesync_svc *timesync_svc;
	int ret;

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

	timesync_svc->work_queue =
		create_singlethread_workqueue("gb-timesync-work_queue");

	if (!timesync_svc->work_queue) {
		kfree(timesync_svc);
		return -ENOMEM;
	}

	mutex_lock(&gb_timesync_svc_list_mutex);
	INIT_LIST_HEAD(&timesync_svc->interface_list);
	INIT_DELAYED_WORK(&timesync_svc->delayed_work, gb_timesync_worker);
	mutex_init(&timesync_svc->mutex);
	spin_lock_init(&timesync_svc->spinlock);
	init_waitqueue_head(&timesync_svc->wait_queue);

	timesync_svc->svc = svc;
	timesync_svc->frame_time_offset = 0;
	timesync_svc->capture_ping = false;
	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_INACTIVE);

	timesync_svc->frame_time_dentry =
		debugfs_create_file("frame-time", S_IRUGO, svc->debugfs_dentry,
				    timesync_svc,
				    &gb_timesync_debugfs_frame_time_ops);
	timesync_svc->frame_ktime_dentry =
		debugfs_create_file("frame-ktime", S_IRUGO, svc->debugfs_dentry,
				    timesync_svc,
				    &gb_timesync_debugfs_frame_ktime_ops);

	list_add(&timesync_svc->list, &gb_timesync_svc_list);
	ret = gb_timesync_hd_add(timesync_svc, svc->hd);
	if (ret) {
		list_del(&timesync_svc->list);
		debugfs_remove(timesync_svc->frame_ktime_dentry);
		debugfs_remove(timesync_svc->frame_time_dentry);
		destroy_workqueue(timesync_svc->work_queue);
		kfree(timesync_svc);
		goto done;
	}

	init_timer(&timesync_svc->ktime_timer);
	timesync_svc->ktime_timer.function = gb_timesync_ktime_timer_fn;
	timesync_svc->ktime_timer.expires = jiffies + GB_TIMESYNC_KTIME_UPDATE;
	timesync_svc->ktime_timer.data = (unsigned long)timesync_svc;
	add_timer(&timesync_svc->ktime_timer);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_svc_add);

void gb_timesync_svc_remove(struct gb_svc *svc)
{
	struct gb_timesync_svc *timesync_svc;
	struct gb_timesync_interface *timesync_interface;
	struct gb_timesync_interface *next;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(svc->hd);
	if (!timesync_svc)
		goto done;

	cancel_delayed_work_sync(&timesync_svc->delayed_work);

	mutex_lock(&timesync_svc->mutex);

	gb_timesync_set_state_atomic(timesync_svc, GB_TIMESYNC_STATE_INVALID);
	del_timer_sync(&timesync_svc->ktime_timer);
	gb_timesync_teardown(timesync_svc);

	gb_timesync_hd_remove(timesync_svc, svc->hd);
	list_for_each_entry_safe(timesync_interface, next,
				 &timesync_svc->interface_list, list) {
		list_del(&timesync_interface->list);
		kfree(timesync_interface);
	}
	debugfs_remove(timesync_svc->frame_ktime_dentry);
	debugfs_remove(timesync_svc->frame_time_dentry);
	destroy_workqueue(timesync_svc->work_queue);
	list_del(&timesync_svc->list);

	mutex_unlock(&timesync_svc->mutex);

	kfree(timesync_svc);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
}
EXPORT_SYMBOL_GPL(gb_timesync_svc_remove);

/*
 * Add a Greybus Interface to the set of TimeSync Interfaces.
 */
int gb_timesync_interface_add(struct gb_interface *interface)
{
	struct gb_timesync_svc *timesync_svc;
	struct gb_timesync_interface *timesync_interface;
	int ret = 0;

	if (!(interface->features & GREYBUS_INTERFACE_FEATURE_TIMESYNC))
		return 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
	if (!timesync_svc) {
		ret = -ENODEV;
		goto done;
	}

	timesync_interface = kzalloc(sizeof(*timesync_interface), GFP_KERNEL);
	if (!timesync_interface) {
		ret = -ENOMEM;
		goto done;
	}

	mutex_lock(&timesync_svc->mutex);
	timesync_interface->interface = interface;
	list_add(&timesync_interface->list, &timesync_svc->interface_list);
	timesync_svc->strobe_mask |= 1 << interface->interface_id;
	mutex_unlock(&timesync_svc->mutex);

done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_interface_add);

/*
 * Remove a Greybus Interface from the set of TimeSync Interfaces.
 */
void gb_timesync_interface_remove(struct gb_interface *interface)
{
	struct gb_timesync_svc *timesync_svc;
	struct gb_timesync_interface *timesync_interface;

	if (!(interface->features & GREYBUS_INTERFACE_FEATURE_TIMESYNC))
		return;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
	if (!timesync_svc)
		goto done;

	timesync_interface = gb_timesync_find_timesync_interface(timesync_svc,
								 interface);
	if (!timesync_interface)
		goto done;

	mutex_lock(&timesync_svc->mutex);
	timesync_svc->strobe_mask &= ~(1 << interface->interface_id);
	list_del(&timesync_interface->list);
	kfree(timesync_interface);
	mutex_unlock(&timesync_svc->mutex);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
}
EXPORT_SYMBOL_GPL(gb_timesync_interface_remove);

/*
 * Give the authoritative FrameTime to the calling function. Returns zero if we
 * are not in GB_TIMESYNC_STATE_ACTIVE.
 */
static u64 gb_timesync_get_frame_time(struct gb_timesync_svc *timesync_svc)
{
	unsigned long flags;
	u64 ret;

	spin_lock_irqsave(&timesync_svc->spinlock, flags);
	if (timesync_svc->state == GB_TIMESYNC_STATE_ACTIVE)
		ret = __gb_timesync_get_frame_time(timesync_svc);
	else
		ret = 0;
	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
	return ret;
}

u64 gb_timesync_get_frame_time_by_interface(struct gb_interface *interface)
{
	struct gb_timesync_svc *timesync_svc;
	u64 ret = 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
	if (!timesync_svc)
		goto done;

	ret = gb_timesync_get_frame_time(timesync_svc);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_get_frame_time_by_interface);

u64 gb_timesync_get_frame_time_by_svc(struct gb_svc *svc)
{
	struct gb_timesync_svc *timesync_svc;
	u64 ret = 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(svc->hd);
	if (!timesync_svc)
		goto done;

	ret = gb_timesync_get_frame_time(timesync_svc);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_get_frame_time_by_svc);

/* Incrementally updates the conversion base from FrameTime to ktime */
static void gb_timesync_ktime_timer_fn(unsigned long data)
{
	struct gb_timesync_svc *timesync_svc =
		(struct gb_timesync_svc *)data;
	unsigned long flags;
	u64 frame_time;
	struct timespec ts;

	spin_lock_irqsave(&timesync_svc->spinlock, flags);

	if (timesync_svc->state != GB_TIMESYNC_STATE_ACTIVE)
		goto done;

	ktime_get_ts(&ts);
	frame_time = __gb_timesync_get_frame_time(timesync_svc);
	gb_timesync_store_ktime(timesync_svc, ts, frame_time);

done:
	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
	mod_timer(&timesync_svc->ktime_timer,
		  jiffies + GB_TIMESYNC_KTIME_UPDATE);
}

int gb_timesync_to_timespec_by_svc(struct gb_svc *svc, u64 frame_time,
				   struct timespec *ts)
{
	struct gb_timesync_svc *timesync_svc;
	int ret = 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(svc->hd);
	if (!timesync_svc) {
		ret = -ENODEV;
		goto done;
	}
	ret = gb_timesync_to_timespec(timesync_svc, frame_time, ts);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_to_timespec_by_svc);

int gb_timesync_to_timespec_by_interface(struct gb_interface *interface,
					 u64 frame_time, struct timespec *ts)
{
	struct gb_timesync_svc *timesync_svc;
	int ret = 0;

	mutex_lock(&gb_timesync_svc_list_mutex);
	timesync_svc = gb_timesync_find_timesync_svc(interface->hd);
	if (!timesync_svc) {
		ret = -ENODEV;
		goto done;
	}

	ret = gb_timesync_to_timespec(timesync_svc, frame_time, ts);
done:
	mutex_unlock(&gb_timesync_svc_list_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(gb_timesync_to_timespec_by_interface);

void gb_timesync_irq(struct gb_timesync_svc *timesync_svc)
{
	unsigned long flags;
	u64 strobe_time;
	bool strobe_is_ping = true;
	struct timespec ts;

	ktime_get_ts(&ts);
	strobe_time = __gb_timesync_get_frame_time(timesync_svc);

	spin_lock_irqsave(&timesync_svc->spinlock, flags);

	if (timesync_svc->state == GB_TIMESYNC_STATE_PING) {
		if (!timesync_svc->capture_ping)
			goto done_nolog;
		timesync_svc->ap_ping_frame_time = strobe_time;
		goto done_log;
	} else if (timesync_svc->state != GB_TIMESYNC_STATE_WAIT_SVC) {
		goto done_nolog;
	}

	timesync_svc->strobe_data[timesync_svc->strobe].frame_time = strobe_time;
	timesync_svc->strobe_data[timesync_svc->strobe].ts = ts;

	if (++timesync_svc->strobe == GB_TIMESYNC_MAX_STROBES) {
		gb_timesync_set_state(timesync_svc,
				      GB_TIMESYNC_STATE_AUTHORITATIVE);
	}
	strobe_is_ping = false;
done_log:
	trace_gb_timesync_irq(strobe_is_ping, timesync_svc->strobe,
			      GB_TIMESYNC_MAX_STROBES, strobe_time);
done_nolog:
	spin_unlock_irqrestore(&timesync_svc->spinlock, flags);
}
EXPORT_SYMBOL(gb_timesync_irq);

int __init gb_timesync_init(void)
{
	int ret = 0;

	ret = gb_timesync_platform_init();
	if (ret) {
		pr_err("timesync platform init fail!\n");
		return ret;
	}

	gb_timesync_clock_rate = gb_timesync_platform_get_clock_rate();

	/* Calculate nanoseconds and femtoseconds per clock */
	gb_timesync_fs_per_clock = FSEC_PER_SEC;
	do_div(gb_timesync_fs_per_clock, gb_timesync_clock_rate);
	gb_timesync_ns_per_clock = NSEC_PER_SEC;
	do_div(gb_timesync_ns_per_clock, gb_timesync_clock_rate);

	/* Calculate the maximum number of clocks we will convert to ktime */
	gb_timesync_max_ktime_diff =
		GB_TIMESYNC_MAX_KTIME_CONVERSION * gb_timesync_clock_rate;

	pr_info("Time-Sync @ %lu Hz max ktime conversion +/- %d seconds\n",
		gb_timesync_clock_rate, GB_TIMESYNC_MAX_KTIME_CONVERSION);
	return 0;
}

void gb_timesync_exit(void)
{
	gb_timesync_platform_exit();
}