From patchwork Fri Jan 22 17:48:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud POULIQUEN X-Patchwork-Id: 8091531 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9517A9F744 for ; Fri, 22 Jan 2016 17:49:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6FA5C20575 for ; Fri, 22 Jan 2016 17:49:56 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id AACA02053C for ; Fri, 22 Jan 2016 17:49:54 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id A7D6926645E; Fri, 22 Jan 2016 18:49:53 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 72C6226145A; Fri, 22 Jan 2016 18:49:03 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 102EE2605CA; Fri, 22 Jan 2016 18:49:01 +0100 (CET) Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by alsa0.perex.cz (Postfix) with ESMTP id D6E80260504 for ; Fri, 22 Jan 2016 18:48:53 +0100 (CET) Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx08-00178001.pphosted.com (8.15.0.59/8.15.0.59) with SMTP id u0MHksFI012027; Fri, 22 Jan 2016 18:48:53 +0100 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx08-00178001.pphosted.com with ESMTP id 20fdmhrncc-1 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 22 Jan 2016 18:48:53 +0100 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id EE4A134; Fri, 22 Jan 2016 17:48:04 +0000 (GMT) Received: from Webmail-eu.st.com (Safex1hubcas21.st.com [10.75.90.44]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 25A6E26DF; Fri, 22 Jan 2016 17:48:52 +0000 (GMT) Received: from localhost (10.201.23.162) by Webmail-ga.st.com (10.75.90.48) with Microsoft SMTP Server (TLS) id 14.3.248.2; Fri, 22 Jan 2016 18:48:51 +0100 From: Arnaud Pouliquen To: Date: Fri, 22 Jan 2016 18:48:27 +0100 Message-ID: <1453484912-24547-2-git-send-email-arnaud.pouliquen@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453484912-24547-1-git-send-email-arnaud.pouliquen@st.com> References: <1453484912-24547-1-git-send-email-arnaud.pouliquen@st.com> MIME-Version: 1.0 X-Originating-IP: [10.201.23.162] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-01-22_06:, , signatures=0 Cc: Jean-Francois Moine , Lars-Peter Clausen , Russell King - ARM Linux , Philipp Zabel , David Airlie , arnaud.pouliquen@st.com, Liam Girdwood , Jyri Sarha , Takashi Iwai , Mark Brown , Benjamin Gaignard Subject: [alsa-devel] [RFC v2 1/6] video: hdmi: add helper function for N and CTS X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP From: Moise Gergaud Add helper function to compute HDMI CTS and N parameters. Implementation is based on HDMI 1.4b specification. Signed-off-by: Arnaud Pouliquen Reviewed-by: Philipp Zabel Tested-by: Philipp Zabel --- drivers/video/hdmi.c | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/hdmi.h | 22 +++++ 2 files changed, 244 insertions(+) diff --git a/drivers/video/hdmi.c b/drivers/video/hdmi.c index 1626892..be8b8ed 100644 --- a/drivers/video/hdmi.c +++ b/drivers/video/hdmi.c @@ -1242,3 +1242,225 @@ int hdmi_infoframe_unpack(union hdmi_infoframe *frame, void *buffer) return ret; } EXPORT_SYMBOL(hdmi_infoframe_unpack); + +/** + * audio clock regeneration (acr) parameters + * N and CTS computation are based on HDMI specification 1.4b + */ +enum audio_rate { + HDMI_AUDIO_N_CTS_32KHZ, + HDMI_AUDIO_N_CTS_44_1KHZ, + HDMI_AUDIO_N_CTS_48KHZ, +}; + +struct hdmi_audio_acr { + unsigned int tmds_clk; + struct hdmi_audio_n_cts n_cts; +}; + +static const struct hdmi_audio_acr hdmi_audio_standard_acr[3][12] = { + { /*32 kHz*/ + { 25174825, { 4576, 28125, 0 } }, /* 25,20/1.001 MHz */ + { 25200000, { 4096, 25200, 0 } }, /* 25.20 MHz */ + { 27000000, { 4096, 27000, 0 } }, /* 27.00 MHz */ + { 27027000, { 4096, 27027, 0 } }, /* 27.00*1.001 MHz */ + { 54000000, { 4096, 54000, 0 } }, /* 54.00 MHz */ + { 54054000, { 4096, 54054, 0 } }, /* 54.00*1.001 MHz */ + { 74175824, { 11648, 210937, 50 } }, /* 74.25/1.001 MHz */ + { 74250000, { 4096, 74250, 0 } }, /* 74.25 MHz */ + { 148351648, { 11648, 421875, 0 } }, /* 148.50/1.001 MHz */ + { 148500000, { 4096, 148500, 0 } }, /* 148.50 MHz */ + { 296703296, { 5824, 421875, 0 } }, /* 297/1.001 MHz */ + { 297000000, { 3072, 222750, 0 } }, /* 297 MHz */ + }, + { /*44.1 kHz, 88.2 kHz 176.4 kHz*/ + { 25174825, { 7007, 31250, 0 } }, /* 25,20/1.001 MHz */ + { 25200000, { 6272, 28000, 0 } }, /* 25.20 MHz */ + { 27000000, { 6272, 30000, 0 } }, /* 27.00 MHz */ + { 27027000, { 6272, 30030, 0 } }, /* 27.00*1.001 MHz */ + { 54000000, { 6272, 60000, 0 } }, /* 54.00 MHz */ + { 54054000, { 6272, 60060, 0 } }, /* 54.00*1.001 MHz */ + { 74175824, { 17836, 234375, 0 } }, /* 74.25/1.001 MHz */ + { 74250000, { 6272, 82500, 0 } }, /* 74.25 MHz */ + { 148351648, { 8918, 234375, 0 } }, /* 148.50/1.001 MHz */ + { 148500000, { 6272, 165000, 0 } }, /* 148.50 MHz */ + { 296703296, { 4459, 234375, 0 } }, /* 297/1.001 MHz */ + { 297000000, { 4704, 247500, 0 } }, /* 297 MHz */ + }, + { /*48 kHz, 96 kHz 192 kHz*/ + { 25174825, { 6864, 28125, 0 } }, /* 25,20/1.001 MHz */ + { 25200000, { 6144, 25200, 0 } }, /* 25.20 MHz */ + { 27000000, { 6144, 27000, 0 } }, /* 27.00 MHz */ + { 27027000, { 6144, 27027, 0 } }, /* 27.00*1.001 MHz */ + { 54000000, { 6144, 54000, 0 } }, /* 54.00 MHz */ + { 54054000, { 6144, 54054, 0 } }, /* 54.00*1.001 MHz */ + { 74175824, { 11648, 140625, 0 } }, /* 74.25/1.001 MHz */ + { 74250000, { 6144, 74250, 0 } }, /* 74.25 MHz */ + { 148351648, { 5824, 140625, 0 } }, /* 148.50/1.001 MHz */ + { 148500000, { 6144, 148500, 0 } }, /* 148.50 MHz */ + { 296703296, { 5824, 281250, 0 } }, /* 297/1.001 MHz */ + { 297000000, { 5120, 247500, 0 } }, /* 297 MHz */ + } +}; + +/** + * hdmi_audio_get_coherent_n_cts() - compute N and CTS parameters for coherent + * clocks. Coherent clock means that audio and TMDS clocks have the same + * source (no drifts between clocks). + * + * @audio_fs: audio frame clock frequency in Hz + * @tmds_clk: HDMI TMDS clock frequency in Hz + * @n_cts: N and CTS parameter returned to user + * + * Values computed are based on table described in HDMI specification 1.4b + * + * Returns 0 on success or a negative error code on failure. + */ +int hdmi_audio_get_coherent_n_cts(unsigned int audio_fs, + unsigned int tmds_clk, + struct hdmi_audio_n_cts *n_cts) +{ + int audio_freq_id, i; + int rate_coeff = 1; + const struct hdmi_audio_acr *acr_table; + const struct hdmi_audio_n_cts *predef_n_cts = NULL; + + switch (audio_fs) { + case 32000: + audio_freq_id = HDMI_AUDIO_N_CTS_32KHZ; + n_cts->n = 4096; + break; + + case 44100: + audio_freq_id = HDMI_AUDIO_N_CTS_44_1KHZ; + n_cts->n = 6272; + break; + + case 48000: + audio_freq_id = HDMI_AUDIO_N_CTS_48KHZ; + n_cts->n = 6144; + break; + + case 88200: + audio_freq_id = HDMI_AUDIO_N_CTS_44_1KHZ; + rate_coeff = 2; + n_cts->n = 6272 * 2; + break; + + case 96000: + audio_freq_id = HDMI_AUDIO_N_CTS_48KHZ; + rate_coeff = 2; + n_cts->n = 6144 * 2; + break; + + case 176400: + audio_freq_id = HDMI_AUDIO_N_CTS_44_1KHZ; + rate_coeff = 4; + n_cts->n = 6272 * 4; + break; + + case 192000: + audio_freq_id = HDMI_AUDIO_N_CTS_48KHZ; + rate_coeff = 4; + n_cts->n = 6144 * 4; + break; + + default: + return -EINVAL; + } + + acr_table = hdmi_audio_standard_acr[audio_freq_id]; + for (i = 0; i < ARRAY_SIZE(hdmi_audio_standard_acr[0]); i++) { + if (tmds_clk == acr_table[i].tmds_clk) { + predef_n_cts = &acr_table[i].n_cts; + break; + } + } + + if (!predef_n_cts) { + /* + * predefined frequency not found. Compute CTS using formula: + * CTS = (Ftdms_clk * N) / (128* audio_fs) + */ + uint64_t val, min; + + val = (uint64_t)tmds_clk * n_cts->n; + + n_cts->cts = div64_u64(val, 128UL * audio_fs); + min = (uint64_t)n_cts->cts * 128UL * audio_fs; + if (min < (val)) { + /* + * non-accurate value for CTS + * compute ratio, needed by user to alternate in ACR + * between CTS and CTS + 1 value. + */ + n_cts->cts_1_ratio = ((int32_t)(val - min)) * 100 / + (128 * audio_fs); + n_cts->cts_1_ratio = 100 - n_cts->cts_1_ratio; + } else { + n_cts->cts_1_ratio = 0; + } + } else { + n_cts->n = predef_n_cts->n * rate_coeff; + n_cts->cts = predef_n_cts->cts; + n_cts->cts_1_ratio = predef_n_cts->cts_1_ratio; + } + + return 0; +} +EXPORT_SYMBOL(hdmi_audio_get_coherent_n_cts); + +/** + * hdmi_audio_get_non_coherent_n() - get N parameter for non-coherent + * clocks. None-coherent clocks means that audio and TMDS clocks have not the + * same source (drifts between clocks). In this case assumption is that CTS is + * automatically calculated by hardware. + * + * @audio_fs: audio frame clock frequency in Hz + * + * Values computed are based on table described in HDMI specification 1.4b + * + * Returns n value. + */ +int hdmi_audio_get_non_coherent_n(unsigned int audio_fs) +{ + unsigned int n; + + switch (audio_fs) { + case 32000: + n = 4096; + break; + + case 44100: + n = 6272; + break; + + case 48000: + n = 6144; + break; + + case 88200: + n = 6272 * 2; + break; + + case 96000: + n = 6144 * 2; + break; + + case 176400: + n = 6272 * 4; + break; + + case 192000: + n = 6144 * 4; + break; + + default: + /* Not pre-defined, recommended value: 128 * fs / 1000 */ + n = (audio_fs * 128) / 1000; + } + + return n; +} +EXPORT_SYMBOL(hdmi_audio_get_non_coherent_n); + diff --git a/include/linux/hdmi.h b/include/linux/hdmi.h index e974420..bf5c136 100644 --- a/include/linux/hdmi.h +++ b/include/linux/hdmi.h @@ -333,4 +333,26 @@ int hdmi_infoframe_unpack(union hdmi_infoframe *frame, void *buffer); void hdmi_infoframe_log(const char *level, struct device *dev, union hdmi_infoframe *frame); +/** + * struct hdmi_audio_n_cts - n and cts parameter for ACR packets + * @n: N parameter + * @cts: CTS parameter + * @cts_1_ratio: ratio from 0 to 99 to alternate "CTS" and "CTS + 1" values + * ratio = 0: CTS parameter is accurate, no need to alternate with "CTS + 1" + * value + * ratio = x: Need to alternate in ACR "CTS + 1" value x% of the time to + * generate accurate audio clock + */ +struct hdmi_audio_n_cts { + unsigned int n; + unsigned int cts; + unsigned int cts_1_ratio; +}; + +int hdmi_audio_get_coherent_n_cts(unsigned int audio_fs, + unsigned int tmds_clk, + struct hdmi_audio_n_cts *n_cts); + +int hdmi_audio_get_non_coherent_n(unsigned int audio_fs); + #endif /* _DRM_HDMI_H */