From d26e03d1e61da1ba12f0fb84d5ee13a8810c0d59 Mon Sep 17 00:00:00 2001 From: Oleksandr Suvorov Date: Mon, 2 Sep 2019 18:31:53 +0300 Subject: ASoC: sgtl5000: add helpers to calculate mclk Calculate SYS_MCLK using sample rate or SYS_FS. These helpers allow to simplify a code to set up external system clock for different sample rates. Signed-off-by: Oleksandr Suvorov --- sound/soc/codecs/sgtl5000.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'sound') diff --git a/sound/soc/codecs/sgtl5000.h b/sound/soc/codecs/sgtl5000.h index 8a9f43534b79..cfbe6e6fa10c 100644 --- a/sound/soc/codecs/sgtl5000.h +++ b/sound/soc/codecs/sgtl5000.h @@ -397,4 +397,48 @@ #define SGTL5000_SYSCLK 0x00 #define SGTL5000_LRCLK 0x01 +/* Convert sample rate and SYS_FS frequencies to external SYS_MCLK */ + +static inline int sgtl5000_sys_fs_to_mclk(int sys_fs) { + int mul[] = { 256, 384, 512 }; + int i; + + for (i = 0; i < 3; i++) { + int mclk = sys_fs * mul[i]; + /* minimum freq for sync mode is 8 MHz */ + if (mclk > 8000000) { + /* maximum freq for sync mode is 27 MHz */ + if (mclk > 27000000) + return -EINVAL; + /* return as minimum mclk as possible to save power */ + return mclk; + } + } + /* srate too small */ + return -EINVAL; +} + +static inline int sgtl5000_srate_to_sys_fs(int srate) { + /* Supported SYS_FS frequencies */ + static const int sys_fs[] = { 32000, 44100, 48000, 96000, 0 }; + int i = 0; + + while (sys_fs[i] > 0) { + if (!(sys_fs[i] % srate)) + return sys_fs[i]; + i++; + } + /* srate is not compatible with any SYS_FS */ + return -EINVAL; +} + +static inline int sgtl5000_srate_to_mclk(int srate) { + int sys_fs = sgtl5000_srate_to_sys_fs(srate); + + if (IS_ERR_VALUE(sys_fs)) + return sys_fs; + + return sgtl5000_sys_fs_to_mclk(sys_fs); +} + #endif -- cgit v1.2.3