Commit db5adf4d authored by Alban Bedel's avatar Alban Bedel Committed by Thierry Reding

drm/tegra: hdmi: Fix audio to work with any pixel clock rate

The audio setting implementation was limited to a few specific pixel
clocks. This prevented HDMI audio from working on several test devices
as they need a pixel clock that is not supported by this implementation.

Fix this by implementing the algorithm provided in the TRM using fixed
point arithmetic. This allows the driver to cope with any sane pixel
clock rate.
Signed-off-by: default avatarAlban Bedel <alban.bedel@avionic-design.de>
[treding@nvidia.com: fix uninitialized variable warning]
Signed-off-by: default avatarThierry Reding <treding@nvidia.com>
parent e3c702dc
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/gpio.h> #include <linux/gpio.h>
#include <linux/hdmi.h> #include <linux/hdmi.h>
#include <linux/math64.h>
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/pm_runtime.h> #include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
...@@ -115,68 +116,11 @@ static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value, ...@@ -115,68 +116,11 @@ static inline void tegra_hdmi_writel(struct tegra_hdmi *hdmi, u32 value,
} }
struct tegra_hdmi_audio_config { struct tegra_hdmi_audio_config {
unsigned int pclk;
unsigned int n; unsigned int n;
unsigned int cts; unsigned int cts;
unsigned int aval; unsigned int aval;
}; };
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_32k[] = {
{ 25200000, 4096, 25200, 24000 },
{ 27000000, 4096, 27000, 24000 },
{ 74250000, 4096, 74250, 24000 },
{ 148500000, 4096, 148500, 24000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_44_1k[] = {
{ 25200000, 5880, 26250, 25000 },
{ 27000000, 5880, 28125, 25000 },
{ 74250000, 4704, 61875, 20000 },
{ 148500000, 4704, 123750, 20000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_48k[] = {
{ 25200000, 6144, 25200, 24000 },
{ 27000000, 6144, 27000, 24000 },
{ 74250000, 6144, 74250, 24000 },
{ 148500000, 6144, 148500, 24000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_88_2k[] = {
{ 25200000, 11760, 26250, 25000 },
{ 27000000, 11760, 28125, 25000 },
{ 74250000, 9408, 61875, 20000 },
{ 148500000, 9408, 123750, 20000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_96k[] = {
{ 25200000, 12288, 25200, 24000 },
{ 27000000, 12288, 27000, 24000 },
{ 74250000, 12288, 74250, 24000 },
{ 148500000, 12288, 148500, 24000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_176_4k[] = {
{ 25200000, 23520, 26250, 25000 },
{ 27000000, 23520, 28125, 25000 },
{ 74250000, 18816, 61875, 20000 },
{ 148500000, 18816, 123750, 20000 },
{ 0, 0, 0, 0 },
};
static const struct tegra_hdmi_audio_config tegra_hdmi_audio_192k[] = {
{ 25200000, 24576, 25200, 24000 },
{ 27000000, 24576, 27000, 24000 },
{ 74250000, 24576, 74250, 24000 },
{ 148500000, 24576, 148500, 24000 },
{ 0, 0, 0, 0 },
};
static const struct tmds_config tegra20_tmds_config[] = { static const struct tmds_config tegra20_tmds_config[] = {
{ /* slow pixel clock modes */ { /* slow pixel clock modes */
.pclk = 27000000, .pclk = 27000000,
...@@ -414,52 +358,53 @@ static const struct tmds_config tegra124_tmds_config[] = { ...@@ -414,52 +358,53 @@ static const struct tmds_config tegra124_tmds_config[] = {
}, },
}; };
static const struct tegra_hdmi_audio_config * static int
tegra_hdmi_get_audio_config(unsigned int sample_rate, unsigned int pclk) tegra_hdmi_get_audio_config(unsigned int audio_freq, unsigned int pix_clock,
struct tegra_hdmi_audio_config *config)
{ {
const struct tegra_hdmi_audio_config *table; const unsigned int afreq = 128 * audio_freq;
const unsigned int min_n = afreq / 1500;
switch (sample_rate) { const unsigned int max_n = afreq / 300;
case 32000: const unsigned int ideal_n = afreq / 1000;
table = tegra_hdmi_audio_32k; int64_t min_err = (uint64_t)-1 >> 1;
break; unsigned int min_delta = -1;
int n;
case 44100:
table = tegra_hdmi_audio_44_1k; memset(config, 0, sizeof(*config));
break; config->n = -1;
case 48000: for (n = min_n; n <= max_n; n++) {
table = tegra_hdmi_audio_48k; uint64_t cts_f, aval_f;
break; unsigned int delta;
int64_t cts, err;
case 88200:
table = tegra_hdmi_audio_88_2k; /* compute aval in 48.16 fixed point */
break; aval_f = ((int64_t)24000000 << 16) * n;
do_div(aval_f, afreq);
case 96000: /* It should round without any rest */
table = tegra_hdmi_audio_96k; if (aval_f & 0xFFFF)
break; continue;
case 176400: /* Compute cts in 48.16 fixed point */
table = tegra_hdmi_audio_176_4k; cts_f = ((int64_t)pix_clock << 16) * n;
break; do_div(cts_f, afreq);
/* Round it to the nearest integer */
case 192000: cts = (cts_f & ~0xFFFF) + ((cts_f & BIT(15)) << 1);
table = tegra_hdmi_audio_192k;
break; delta = abs(n - ideal_n);
default: /* Compute the absolute error */
return NULL; err = abs((int64_t)cts_f - cts);
} if (err < min_err || (err == min_err && delta < min_delta)) {
config->n = n;
while (table->pclk) { config->cts = cts >> 16;
if (table->pclk == pclk) config->aval = aval_f >> 16;
return table; min_delta = delta;
min_err = err;
table++; }
} }
return NULL; return config->n != -1 ? 0 : -EINVAL;
} }
static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi) static void tegra_hdmi_setup_audio_fs_tables(struct tegra_hdmi *hdmi)
...@@ -515,8 +460,9 @@ static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value) ...@@ -515,8 +460,9 @@ static void tegra_hdmi_write_aval(struct tegra_hdmi *hdmi, u32 value)
static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi) static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
{ {
const struct tegra_hdmi_audio_config *config; struct tegra_hdmi_audio_config config;
u32 source, value; u32 source, value;
int err;
switch (hdmi->audio_source) { switch (hdmi->audio_source) {
case HDA: case HDA:
...@@ -591,25 +537,28 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi) ...@@ -591,25 +537,28 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0); tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_SOR_AUDIO_SPARE0);
} }
config = tegra_hdmi_get_audio_config(hdmi->format.sample_rate, err = tegra_hdmi_get_audio_config(hdmi->format.sample_rate,
hdmi->pixel_clock); hdmi->pixel_clock, &config);
if (!config) { if (err < 0) {
dev_err(hdmi->dev, dev_err(hdmi->dev,
"cannot set audio to %u Hz at %u Hz pixel clock\n", "cannot set audio to %u Hz at %u Hz pixel clock\n",
hdmi->format.sample_rate, hdmi->pixel_clock); hdmi->format.sample_rate, hdmi->pixel_clock);
return -EINVAL; return err;
} }
dev_dbg(hdmi->dev, "audio: pixclk=%u, n=%u, cts=%u, aval=%u\n",
hdmi->pixel_clock, config.n, config.cts, config.aval);
tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL); tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_HDMI_ACR_CTRL);
value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE | value = AUDIO_N_RESETF | AUDIO_N_GENERATE_ALTERNATE |
AUDIO_N_VALUE(config->n - 1); AUDIO_N_VALUE(config.n - 1);
tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N); tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config->n) | ACR_ENABLE, tegra_hdmi_writel(hdmi, ACR_SUBPACK_N(config.n) | ACR_ENABLE,
HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH); HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_HIGH);
tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config->cts), tegra_hdmi_writel(hdmi, ACR_SUBPACK_CTS(config.cts),
HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW); HDMI_NV_PDISP_HDMI_ACR_0441_SUBPACK_LOW);
value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1); value = SPARE_HW_CTS | SPARE_FORCE_SW_CTS | SPARE_CTS_RESET_VAL(1);
...@@ -620,7 +569,7 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi) ...@@ -620,7 +569,7 @@ static int tegra_hdmi_setup_audio(struct tegra_hdmi *hdmi)
tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N); tegra_hdmi_writel(hdmi, value, HDMI_NV_PDISP_AUDIO_N);
if (hdmi->config->has_hda) if (hdmi->config->has_hda)
tegra_hdmi_write_aval(hdmi, config->aval); tegra_hdmi_write_aval(hdmi, config.aval);
tegra_hdmi_setup_audio_fs_tables(hdmi); tegra_hdmi_setup_audio_fs_tables(hdmi);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment