blob: a6076d58a575eb671ea5484a5febcbef775628a7
1 | /* |
2 | * drivers/amlogic/media/frame_sync/timestamp.c |
3 | * |
4 | * Copyright (C) 2017 Amlogic, Inc. All rights reserved. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
14 | * more details. |
15 | * |
16 | */ |
17 | |
18 | #define DEBUG |
19 | #include <linux/module.h> |
20 | #include <linux/amlogic/media/frame_sync/tsync.h> |
21 | #include <linux/amlogic/media/frame_sync/tsync_pcr.h> |
22 | #include <linux/amlogic/media/utils/vdec_reg.h> |
23 | #include <linux/amlogic/media/registers/register.h> |
24 | #include <linux/amlogic/media/vout/vout_notify.h> |
25 | #include <trace/events/meson_atrace.h> |
26 | |
27 | |
28 | u32 acc_apts_inc; |
29 | u32 acc_apts_dec; |
30 | u32 acc_pcrscr_inc; |
31 | u32 acc_pcrscr_dec; |
32 | |
33 | static s32 system_time_inc_adj; |
34 | static u32 system_time; |
35 | static u32 system_time_up; |
36 | static u32 audio_pts_up; |
37 | static u32 audio_pts_started; |
38 | static u32 first_vpts; |
39 | static u32 first_checkin_vpts; |
40 | static u32 first_checkin_apts; |
41 | static u32 first_apts; |
42 | static u32 pcrscr_lantcy = 200*90; |
43 | static u32 video_pts; |
44 | static u32 audio_pts; |
45 | |
46 | static u32 system_time_scale_base = 1; |
47 | static u32 system_time_scale_remainder; |
48 | |
49 | #ifdef MODIFY_TIMESTAMP_INC_WITH_PLL |
50 | #define PLL_FACTOR 10000 |
51 | static u32 timestamp_inc_factor = PLL_FACTOR; |
52 | void set_timestamp_inc_factor(u32 factor) |
53 | { |
54 | timestamp_inc_factor = factor; |
55 | } |
56 | #endif |
57 | |
58 | u32 timestamp_vpts_get(void) |
59 | { |
60 | return video_pts; |
61 | } |
62 | EXPORT_SYMBOL(timestamp_vpts_get); |
63 | |
64 | void timestamp_vpts_set(u32 pts) |
65 | { |
66 | video_pts = pts; |
67 | } |
68 | EXPORT_SYMBOL(timestamp_vpts_set); |
69 | |
70 | void timestamp_vpts_inc(s32 val) |
71 | { |
72 | video_pts += val; |
73 | } |
74 | EXPORT_SYMBOL(timestamp_vpts_inc); |
75 | |
76 | u32 timestamp_apts_get(void) |
77 | { |
78 | return audio_pts; |
79 | } |
80 | EXPORT_SYMBOL(timestamp_apts_get); |
81 | |
82 | void timestamp_apts_set(u32 pts) |
83 | { |
84 | audio_pts = pts; |
85 | } |
86 | EXPORT_SYMBOL(timestamp_apts_set); |
87 | |
88 | void timestamp_apts_inc(s32 inc) |
89 | { |
90 | if (audio_pts_up) { |
91 | #ifdef MODIFY_TIMESTAMP_INC_WITH_PLL |
92 | inc = inc * timestamp_inc_factor / PLL_FACTOR; |
93 | #endif |
94 | audio_pts += inc; |
95 | } |
96 | } |
97 | EXPORT_SYMBOL(timestamp_apts_inc); |
98 | |
99 | void timestamp_apts_enable(u32 enable) |
100 | { |
101 | audio_pts_up = enable; |
102 | pr_info("timestamp_apts_enable enable:%x,\n", enable); |
103 | } |
104 | EXPORT_SYMBOL(timestamp_apts_enable); |
105 | |
106 | void timestamp_apts_start(u32 enable) |
107 | { |
108 | audio_pts_started = enable; |
109 | pr_info("audio pts started::::::: %d\n", enable); |
110 | } |
111 | EXPORT_SYMBOL(timestamp_apts_start); |
112 | |
113 | u32 timestamp_apts_started(void) |
114 | { |
115 | return audio_pts_started; |
116 | } |
117 | EXPORT_SYMBOL(timestamp_apts_started); |
118 | |
119 | u32 timestamp_pcrscr_get(void) |
120 | { |
121 | if (tsync_get_mode() == TSYNC_MODE_AMASTER) |
122 | return system_time; |
123 | |
124 | if (tsdemux_pcrscr_valid_cb && tsdemux_pcrscr_valid_cb()) { |
125 | if (tsync_pcr_demux_pcr_used() == 0) { |
126 | return system_time; |
127 | } |
128 | else { |
129 | if (tsdemux_pcrscr_get_cb) |
130 | return tsdemux_pcrscr_get_cb()-pcrscr_lantcy; |
131 | else |
132 | return system_time; |
133 | } |
134 | } else |
135 | return system_time; |
136 | } |
137 | EXPORT_SYMBOL(timestamp_pcrscr_get); |
138 | |
139 | u32 timestamp_tsdemux_pcr_get(void) |
140 | { |
141 | if (tsdemux_pcrscr_get_cb) |
142 | return tsdemux_pcrscr_get_cb(); |
143 | |
144 | return (u32)-1; |
145 | } |
146 | EXPORT_SYMBOL(timestamp_tsdemux_pcr_get); |
147 | |
148 | void timestamp_pcrscr_set(u32 pts) |
149 | { |
150 | /*pr_info("timestamp_pcrscr_set system time = %x\n", pts);*/ |
151 | ATRACE_COUNTER("PCRSCR", pts); |
152 | system_time = pts; |
153 | } |
154 | EXPORT_SYMBOL(timestamp_pcrscr_set); |
155 | |
156 | void timestamp_firstvpts_set(u32 pts) |
157 | { |
158 | first_vpts = pts; |
159 | pr_info("video first pts = %x\n", first_vpts); |
160 | } |
161 | EXPORT_SYMBOL(timestamp_firstvpts_set); |
162 | |
163 | u32 timestamp_firstvpts_get(void) |
164 | { |
165 | return first_vpts; |
166 | } |
167 | EXPORT_SYMBOL(timestamp_firstvpts_get); |
168 | |
169 | void timestamp_checkin_firstvpts_set(u32 pts) |
170 | { |
171 | first_checkin_vpts = pts; |
172 | pr_info("video first checkin pts = %x\n", first_checkin_vpts); |
173 | } |
174 | EXPORT_SYMBOL(timestamp_checkin_firstvpts_set); |
175 | |
176 | void timestamp_checkin_firstapts_set(u32 pts) |
177 | { |
178 | first_checkin_apts = pts; |
179 | pr_info("audio first checkin pts =%x\n", first_checkin_apts); |
180 | } |
181 | EXPORT_SYMBOL(timestamp_checkin_firstapts_set); |
182 | |
183 | u32 timestamp_checkin_firstvpts_get(void) |
184 | { |
185 | return first_checkin_vpts; |
186 | } |
187 | EXPORT_SYMBOL(timestamp_checkin_firstvpts_get); |
188 | |
189 | u32 timestamp_checkin_firstapts_get(void) |
190 | { |
191 | return first_checkin_apts; |
192 | } |
193 | EXPORT_SYMBOL(timestamp_checkin_firstapts_get); |
194 | |
195 | void timestamp_firstapts_set(u32 pts) |
196 | { |
197 | first_apts = pts; |
198 | pr_info("audio first pts = %x\n", first_apts); |
199 | } |
200 | EXPORT_SYMBOL(timestamp_firstapts_set); |
201 | |
202 | u32 timestamp_firstapts_get(void) |
203 | { |
204 | return first_apts; |
205 | } |
206 | EXPORT_SYMBOL(timestamp_firstapts_get); |
207 | |
208 | void timestamp_pcrscr_inc(s32 inc) |
209 | { |
210 | if (system_time_up) { |
211 | #ifdef MODIFY_TIMESTAMP_INC_WITH_PLL |
212 | inc = inc * timestamp_inc_factor / PLL_FACTOR; |
213 | #endif |
214 | system_time += inc + system_time_inc_adj; |
215 | ATRACE_COUNTER("PCRSCR", system_time); |
216 | } |
217 | } |
218 | EXPORT_SYMBOL(timestamp_pcrscr_inc); |
219 | |
220 | void timestamp_pcrscr_inc_scale(s32 inc, u32 base) |
221 | { |
222 | if (system_time_scale_base != base) { |
223 | system_time_scale_remainder = |
224 | system_time_scale_remainder * |
225 | base / system_time_scale_base; |
226 | system_time_scale_base = base; |
227 | } |
228 | |
229 | if (system_time_up) { |
230 | u32 r; |
231 | |
232 | system_time += |
233 | div_u64_rem(90000ULL * inc, base, &r) + |
234 | system_time_inc_adj; |
235 | system_time_scale_remainder += r; |
236 | if (system_time_scale_remainder >= system_time_scale_base) { |
237 | system_time++; |
238 | system_time_scale_remainder -= system_time_scale_base; |
239 | } |
240 | ATRACE_COUNTER("PCRSCR", system_time); |
241 | } |
242 | } |
243 | EXPORT_SYMBOL(timestamp_pcrscr_inc_scale); |
244 | |
245 | void timestamp_pcrscr_set_adj(s32 inc) |
246 | { |
247 | system_time_inc_adj = inc; |
248 | } |
249 | EXPORT_SYMBOL(timestamp_pcrscr_set_adj); |
250 | |
251 | void timestamp_pcrscr_set_adj_pcr(s32 inc) |
252 | { |
253 | const struct vinfo_s *info = get_current_vinfo(); |
254 | |
255 | if (inc != 0) { |
256 | system_time_inc_adj = |
257 | 900 * info->sync_duration_den / |
258 | (info->sync_duration_num*inc); |
259 | } else |
260 | system_time_inc_adj = 0; |
261 | } |
262 | EXPORT_SYMBOL(timestamp_pcrscr_set_adj_pcr); |
263 | |
264 | void timestamp_pcrscr_enable(u32 enable) |
265 | { |
266 | system_time_up = enable; |
267 | } |
268 | EXPORT_SYMBOL(timestamp_pcrscr_enable); |
269 | |
270 | u32 timestamp_pcrscr_enable_state(void) |
271 | { |
272 | return system_time_up; |
273 | } |
274 | EXPORT_SYMBOL(timestamp_pcrscr_enable_state); |
275 | |
276 | MODULE_DESCRIPTION("AMLOGIC time sync management driver"); |
277 | MODULE_LICENSE("GPL"); |
278 | MODULE_AUTHOR("Tim Yao <timyao@amlogic.com>"); |
279 |