summaryrefslogtreecommitdiff
path: root/drivers/amlogic/media/frame_sync/timestamp.c (plain)
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
28u32 acc_apts_inc;
29u32 acc_apts_dec;
30u32 acc_pcrscr_inc;
31u32 acc_pcrscr_dec;
32
33static s32 system_time_inc_adj;
34static u32 system_time;
35static u32 system_time_up;
36static u32 audio_pts_up;
37static u32 audio_pts_started;
38static u32 first_vpts;
39static u32 first_checkin_vpts;
40static u32 first_checkin_apts;
41static u32 first_apts;
42static u32 pcrscr_lantcy = 200*90;
43static u32 video_pts;
44static u32 audio_pts;
45
46static u32 system_time_scale_base = 1;
47static u32 system_time_scale_remainder;
48
49#ifdef MODIFY_TIMESTAMP_INC_WITH_PLL
50#define PLL_FACTOR 10000
51static u32 timestamp_inc_factor = PLL_FACTOR;
52void set_timestamp_inc_factor(u32 factor)
53{
54 timestamp_inc_factor = factor;
55}
56#endif
57
58u32 timestamp_vpts_get(void)
59{
60 return video_pts;
61}
62EXPORT_SYMBOL(timestamp_vpts_get);
63
64void timestamp_vpts_set(u32 pts)
65{
66 video_pts = pts;
67}
68EXPORT_SYMBOL(timestamp_vpts_set);
69
70void timestamp_vpts_inc(s32 val)
71{
72 video_pts += val;
73}
74EXPORT_SYMBOL(timestamp_vpts_inc);
75
76u32 timestamp_apts_get(void)
77{
78 return audio_pts;
79}
80EXPORT_SYMBOL(timestamp_apts_get);
81
82void timestamp_apts_set(u32 pts)
83{
84 audio_pts = pts;
85}
86EXPORT_SYMBOL(timestamp_apts_set);
87
88void 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}
97EXPORT_SYMBOL(timestamp_apts_inc);
98
99void timestamp_apts_enable(u32 enable)
100{
101 audio_pts_up = enable;
102 pr_info("timestamp_apts_enable enable:%x,\n", enable);
103}
104EXPORT_SYMBOL(timestamp_apts_enable);
105
106void timestamp_apts_start(u32 enable)
107{
108 audio_pts_started = enable;
109 pr_info("audio pts started::::::: %d\n", enable);
110}
111EXPORT_SYMBOL(timestamp_apts_start);
112
113u32 timestamp_apts_started(void)
114{
115 return audio_pts_started;
116}
117EXPORT_SYMBOL(timestamp_apts_started);
118
119u32 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}
137EXPORT_SYMBOL(timestamp_pcrscr_get);
138
139u32 timestamp_tsdemux_pcr_get(void)
140{
141 if (tsdemux_pcrscr_get_cb)
142 return tsdemux_pcrscr_get_cb();
143
144 return (u32)-1;
145}
146EXPORT_SYMBOL(timestamp_tsdemux_pcr_get);
147
148void 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}
154EXPORT_SYMBOL(timestamp_pcrscr_set);
155
156void timestamp_firstvpts_set(u32 pts)
157{
158 first_vpts = pts;
159 pr_info("video first pts = %x\n", first_vpts);
160}
161EXPORT_SYMBOL(timestamp_firstvpts_set);
162
163u32 timestamp_firstvpts_get(void)
164{
165 return first_vpts;
166}
167EXPORT_SYMBOL(timestamp_firstvpts_get);
168
169void 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}
174EXPORT_SYMBOL(timestamp_checkin_firstvpts_set);
175
176void 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}
181EXPORT_SYMBOL(timestamp_checkin_firstapts_set);
182
183u32 timestamp_checkin_firstvpts_get(void)
184{
185 return first_checkin_vpts;
186}
187EXPORT_SYMBOL(timestamp_checkin_firstvpts_get);
188
189u32 timestamp_checkin_firstapts_get(void)
190{
191 return first_checkin_apts;
192}
193EXPORT_SYMBOL(timestamp_checkin_firstapts_get);
194
195void timestamp_firstapts_set(u32 pts)
196{
197 first_apts = pts;
198 pr_info("audio first pts = %x\n", first_apts);
199}
200EXPORT_SYMBOL(timestamp_firstapts_set);
201
202u32 timestamp_firstapts_get(void)
203{
204 return first_apts;
205}
206EXPORT_SYMBOL(timestamp_firstapts_get);
207
208void 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}
218EXPORT_SYMBOL(timestamp_pcrscr_inc);
219
220void 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}
243EXPORT_SYMBOL(timestamp_pcrscr_inc_scale);
244
245void timestamp_pcrscr_set_adj(s32 inc)
246{
247 system_time_inc_adj = inc;
248}
249EXPORT_SYMBOL(timestamp_pcrscr_set_adj);
250
251void 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}
262EXPORT_SYMBOL(timestamp_pcrscr_set_adj_pcr);
263
264void timestamp_pcrscr_enable(u32 enable)
265{
266 system_time_up = enable;
267}
268EXPORT_SYMBOL(timestamp_pcrscr_enable);
269
270u32 timestamp_pcrscr_enable_state(void)
271{
272 return system_time_up;
273}
274EXPORT_SYMBOL(timestamp_pcrscr_enable_state);
275
276MODULE_DESCRIPTION("AMLOGIC time sync management driver");
277MODULE_LICENSE("GPL");
278MODULE_AUTHOR("Tim Yao <timyao@amlogic.com>");
279