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