summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleksandr Suvorov <oleksandr.suvorov@toradex.com>2019-09-02 18:31:53 +0300
committerOleksandr Suvorov <oleksandr.suvorov@toradex.com>2019-09-06 16:36:16 +0300
commit84ebbdcdbf191e829d33ac557bfff4276ee2849a (patch)
treebf4a23cc538edfe77d08f1d95b4d1a27aa149e73
parenteaf1ebd625a0d554b278b44da89be40dcb80945b (diff)
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 <oleksandr.suvorov@toradex.com>
-rw-r--r--sound/soc/codecs/sgtl5000.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/sound/soc/codecs/sgtl5000.h b/sound/soc/codecs/sgtl5000.h
index b3acbae2e75e..2d12040abb6d 100644
--- a/sound/soc/codecs/sgtl5000.h
+++ b/sound/soc/codecs/sgtl5000.h
@@ -404,4 +404,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