11.1Sriastrad/*	$NetBSD: amdgpu_afmt.c,v 1.3 2021/12/18 23:44:58 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*
41.1Sriastrad * Copyright 2008 Advanced Micro Devices, Inc.
51.1Sriastrad * Copyright 2008 Red Hat Inc.
61.1Sriastrad * Copyright 2009 Christian König.
71.1Sriastrad *
81.1Sriastrad * Permission is hereby granted, free of charge, to any person obtaining a
91.1Sriastrad * copy of this software and associated documentation files (the "Software"),
101.1Sriastrad * to deal in the Software without restriction, including without limitation
111.1Sriastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
121.1Sriastrad * and/or sell copies of the Software, and to permit persons to whom the
131.1Sriastrad * Software is furnished to do so, subject to the following conditions:
141.1Sriastrad *
151.1Sriastrad * The above copyright notice and this permission notice shall be included in
161.1Sriastrad * all copies or substantial portions of the Software.
171.1Sriastrad *
181.1Sriastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
191.1Sriastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
201.1Sriastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
211.1Sriastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
221.1Sriastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
231.1Sriastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
241.1Sriastrad * OTHER DEALINGS IN THE SOFTWARE.
251.1Sriastrad *
261.1Sriastrad * Authors: Christian König
271.1Sriastrad */
281.1Sriastrad#include <sys/cdefs.h>
291.1Sriastrad__KERNEL_RCSID(0, "$NetBSD: amdgpu_afmt.c,v 1.3 2021/12/18 23:44:58 riastradh Exp $");
301.1Sriastrad
311.1Sriastrad#include <linux/hdmi.h>
321.1Sriastrad#include <linux/gcd.h>
331.3Sriastrad
341.1Sriastrad#include <drm/amdgpu_drm.h>
351.1Sriastrad#include "amdgpu.h"
361.1Sriastrad
371.1Sriastradstatic const struct amdgpu_afmt_acr amdgpu_afmt_predefined_acr[] = {
381.1Sriastrad    /*	     32kHz	  44.1kHz	48kHz    */
391.1Sriastrad    /* Clock      N     CTS      N     CTS      N     CTS */
401.1Sriastrad    {  25175,  4096,  25175, 28224, 125875,  6144,  25175 }, /*  25,20/1.001 MHz */
411.1Sriastrad    {  25200,  4096,  25200,  6272,  28000,  6144,  25200 }, /*  25.20       MHz */
421.1Sriastrad    {  27000,  4096,  27000,  6272,  30000,  6144,  27000 }, /*  27.00       MHz */
431.1Sriastrad    {  27027,  4096,  27027,  6272,  30030,  6144,  27027 }, /*  27.00*1.001 MHz */
441.1Sriastrad    {  54000,  4096,  54000,  6272,  60000,  6144,  54000 }, /*  54.00       MHz */
451.1Sriastrad    {  54054,  4096,  54054,  6272,  60060,  6144,  54054 }, /*  54.00*1.001 MHz */
461.1Sriastrad    {  74176,  4096,  74176,  5733,  75335,  6144,  74176 }, /*  74.25/1.001 MHz */
471.1Sriastrad    {  74250,  4096,  74250,  6272,  82500,  6144,  74250 }, /*  74.25       MHz */
481.1Sriastrad    { 148352,  4096, 148352,  5733, 150670,  6144, 148352 }, /* 148.50/1.001 MHz */
491.1Sriastrad    { 148500,  4096, 148500,  6272, 165000,  6144, 148500 }, /* 148.50       MHz */
501.1Sriastrad};
511.1Sriastrad
521.1Sriastrad
531.1Sriastrad/*
541.1Sriastrad * calculate CTS and N values if they are not found in the table
551.1Sriastrad */
561.1Sriastradstatic void amdgpu_afmt_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
571.1Sriastrad{
581.1Sriastrad	int n, cts;
591.1Sriastrad	unsigned long div, mul;
601.1Sriastrad
611.1Sriastrad	/* Safe, but overly large values */
621.1Sriastrad	n = 128 * freq;
631.1Sriastrad	cts = clock * 1000;
641.1Sriastrad
651.1Sriastrad	/* Smallest valid fraction */
661.1Sriastrad	div = gcd(n, cts);
671.1Sriastrad
681.1Sriastrad	n /= div;
691.1Sriastrad	cts /= div;
701.1Sriastrad
711.1Sriastrad	/*
721.1Sriastrad	 * The optimal N is 128*freq/1000. Calculate the closest larger
731.1Sriastrad	 * value that doesn't truncate any bits.
741.1Sriastrad	 */
751.1Sriastrad	mul = ((128*freq/1000) + (n-1))/n;
761.1Sriastrad
771.1Sriastrad	n *= mul;
781.1Sriastrad	cts *= mul;
791.1Sriastrad
801.1Sriastrad	/* Check that we are in spec (not always possible) */
811.1Sriastrad	if (n < (128*freq/1500))
821.3Sriastrad		pr_warn("Calculated ACR N value is too small. You may experience audio problems.\n");
831.1Sriastrad	if (n > (128*freq/300))
841.3Sriastrad		pr_warn("Calculated ACR N value is too large. You may experience audio problems.\n");
851.1Sriastrad
861.1Sriastrad	*N = n;
871.1Sriastrad	*CTS = cts;
881.1Sriastrad
891.1Sriastrad	DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
901.1Sriastrad		  *N, *CTS, freq);
911.1Sriastrad}
921.1Sriastrad
931.1Sriastradstruct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock)
941.1Sriastrad{
951.1Sriastrad	struct amdgpu_afmt_acr res;
961.1Sriastrad	u8 i;
971.1Sriastrad
981.1Sriastrad	/* Precalculated values for common clocks */
991.1Sriastrad	for (i = 0; i < ARRAY_SIZE(amdgpu_afmt_predefined_acr); i++) {
1001.1Sriastrad		if (amdgpu_afmt_predefined_acr[i].clock == clock)
1011.1Sriastrad			return amdgpu_afmt_predefined_acr[i];
1021.1Sriastrad	}
1031.1Sriastrad
1041.1Sriastrad	/* And odd clocks get manually calculated */
1051.1Sriastrad	amdgpu_afmt_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
1061.1Sriastrad	amdgpu_afmt_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
1071.1Sriastrad	amdgpu_afmt_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
1081.1Sriastrad
1091.1Sriastrad	return res;
1101.1Sriastrad}
111