xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_afmt.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2008 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  * Copyright 2008 Red Hat Inc.
4*b843c749SSergey Zigachev  * Copyright 2009 Christian König.
5*b843c749SSergey Zigachev  *
6*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
7*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
8*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
9*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
11*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
12*b843c749SSergey Zigachev  *
13*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
14*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
15*b843c749SSergey Zigachev  *
16*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  * Authors: Christian König
25*b843c749SSergey Zigachev  */
26*b843c749SSergey Zigachev #include <linux/hdmi.h>
27*b843c749SSergey Zigachev #include <linux/gcd.h>
28*b843c749SSergey Zigachev #include <drm/drmP.h>
29*b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
30*b843c749SSergey Zigachev #include "amdgpu.h"
31*b843c749SSergey Zigachev 
32*b843c749SSergey Zigachev static const struct amdgpu_afmt_acr amdgpu_afmt_predefined_acr[] = {
33*b843c749SSergey Zigachev     /*	     32kHz	  44.1kHz	48kHz    */
34*b843c749SSergey Zigachev     /* Clock      N     CTS      N     CTS      N     CTS */
35*b843c749SSergey Zigachev     {  25175,  4096,  25175, 28224, 125875,  6144,  25175 }, /*  25,20/1.001 MHz */
36*b843c749SSergey Zigachev     {  25200,  4096,  25200,  6272,  28000,  6144,  25200 }, /*  25.20       MHz */
37*b843c749SSergey Zigachev     {  27000,  4096,  27000,  6272,  30000,  6144,  27000 }, /*  27.00       MHz */
38*b843c749SSergey Zigachev     {  27027,  4096,  27027,  6272,  30030,  6144,  27027 }, /*  27.00*1.001 MHz */
39*b843c749SSergey Zigachev     {  54000,  4096,  54000,  6272,  60000,  6144,  54000 }, /*  54.00       MHz */
40*b843c749SSergey Zigachev     {  54054,  4096,  54054,  6272,  60060,  6144,  54054 }, /*  54.00*1.001 MHz */
41*b843c749SSergey Zigachev     {  74176,  4096,  74176,  5733,  75335,  6144,  74176 }, /*  74.25/1.001 MHz */
42*b843c749SSergey Zigachev     {  74250,  4096,  74250,  6272,  82500,  6144,  74250 }, /*  74.25       MHz */
43*b843c749SSergey Zigachev     { 148352,  4096, 148352,  5733, 150670,  6144, 148352 }, /* 148.50/1.001 MHz */
44*b843c749SSergey Zigachev     { 148500,  4096, 148500,  6272, 165000,  6144, 148500 }, /* 148.50       MHz */
45*b843c749SSergey Zigachev };
46*b843c749SSergey Zigachev 
47*b843c749SSergey Zigachev 
48*b843c749SSergey Zigachev /*
49*b843c749SSergey Zigachev  * calculate CTS and N values if they are not found in the table
50*b843c749SSergey Zigachev  */
amdgpu_afmt_calc_cts(uint32_t clock,int * CTS,int * N,int freq)51*b843c749SSergey Zigachev static void amdgpu_afmt_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
52*b843c749SSergey Zigachev {
53*b843c749SSergey Zigachev 	int n, cts;
54*b843c749SSergey Zigachev 	unsigned long div, mul;
55*b843c749SSergey Zigachev 
56*b843c749SSergey Zigachev 	/* Safe, but overly large values */
57*b843c749SSergey Zigachev 	n = 128 * freq;
58*b843c749SSergey Zigachev 	cts = clock * 1000;
59*b843c749SSergey Zigachev 
60*b843c749SSergey Zigachev 	/* Smallest valid fraction */
61*b843c749SSergey Zigachev 	div = gcd(n, cts);
62*b843c749SSergey Zigachev 
63*b843c749SSergey Zigachev 	n /= div;
64*b843c749SSergey Zigachev 	cts /= div;
65*b843c749SSergey Zigachev 
66*b843c749SSergey Zigachev 	/*
67*b843c749SSergey Zigachev 	 * The optimal N is 128*freq/1000. Calculate the closest larger
68*b843c749SSergey Zigachev 	 * value that doesn't truncate any bits.
69*b843c749SSergey Zigachev 	 */
70*b843c749SSergey Zigachev 	mul = ((128*freq/1000) + (n-1))/n;
71*b843c749SSergey Zigachev 
72*b843c749SSergey Zigachev 	n *= mul;
73*b843c749SSergey Zigachev 	cts *= mul;
74*b843c749SSergey Zigachev 
75*b843c749SSergey Zigachev 	/* Check that we are in spec (not always possible) */
76*b843c749SSergey Zigachev 	if (n < (128*freq/1500))
77*b843c749SSergey Zigachev 		pr_warn("Calculated ACR N value is too small. You may experience audio problems.\n");
78*b843c749SSergey Zigachev 	if (n > (128*freq/300))
79*b843c749SSergey Zigachev 		pr_warn("Calculated ACR N value is too large. You may experience audio problems.\n");
80*b843c749SSergey Zigachev 
81*b843c749SSergey Zigachev 	*N = n;
82*b843c749SSergey Zigachev 	*CTS = cts;
83*b843c749SSergey Zigachev 
84*b843c749SSergey Zigachev 	DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
85*b843c749SSergey Zigachev 		  *N, *CTS, freq);
86*b843c749SSergey Zigachev }
87*b843c749SSergey Zigachev 
amdgpu_afmt_acr(uint32_t clock)88*b843c749SSergey Zigachev struct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock)
89*b843c749SSergey Zigachev {
90*b843c749SSergey Zigachev 	struct amdgpu_afmt_acr res;
91*b843c749SSergey Zigachev 	u8 i;
92*b843c749SSergey Zigachev 
93*b843c749SSergey Zigachev 	/* Precalculated values for common clocks */
94*b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(amdgpu_afmt_predefined_acr); i++) {
95*b843c749SSergey Zigachev 		if (amdgpu_afmt_predefined_acr[i].clock == clock)
96*b843c749SSergey Zigachev 			return amdgpu_afmt_predefined_acr[i];
97*b843c749SSergey Zigachev 	}
98*b843c749SSergey Zigachev 
99*b843c749SSergey Zigachev 	/* And odd clocks get manually calculated */
100*b843c749SSergey Zigachev 	amdgpu_afmt_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
101*b843c749SSergey Zigachev 	amdgpu_afmt_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
102*b843c749SSergey Zigachev 	amdgpu_afmt_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
103*b843c749SSergey Zigachev 
104*b843c749SSergey Zigachev 	return res;
105*b843c749SSergey Zigachev }
106