^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // ALSA SoC driver for Migo-R
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/clkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <cpu/sh7722.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "../codecs/wm8978.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "siu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Default 8000Hz sampling frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static unsigned long codec_freq = 8000 * 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static unsigned int use_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* External clock, sourced from the codec at the SIUMCKB pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static unsigned long siumckb_recalc(struct clk *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return codec_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct sh_clk_ops siumckb_clk_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .recalc = siumckb_recalc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct clk siumckb_clk = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .ops = &siumckb_clk_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .rate = 0, /* initialised at run-time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct clk_lookup *siumckb_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int migor_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct snd_pcm_hw_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int rate = params_rate(params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) SND_SOC_CLOCK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) codec_freq = rate * 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * This propagates the parent frequency change to children and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * recalculates the frequency table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) clk_set_rate(&siumckb_clk, codec_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), SIU_CLKB_EXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) codec_freq / 2, SND_SOC_CLOCK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) use_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int migor_hw_free(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (use_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) use_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!use_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) SND_SOC_CLOCK_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static const struct snd_soc_ops migor_dai_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .hw_params = migor_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .hw_free = migor_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static const struct snd_soc_dapm_widget migor_dapm_widgets[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) SND_SOC_DAPM_HP("Headphone", NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) SND_SOC_DAPM_MIC("Onboard Microphone", NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) SND_SOC_DAPM_MIC("External Microphone", NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static const struct snd_soc_dapm_route audio_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) { "Headphone", NULL, "OUT4 VMID" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) { "OUT4 VMID", NULL, "LHP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) { "OUT4 VMID", NULL, "RHP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* On-board microphone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) { "RMICN", NULL, "Mic Bias" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) { "RMICP", NULL, "Mic Bias" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) { "Mic Bias", NULL, "Onboard Microphone" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* External microphone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) { "LMICN", NULL, "Mic Bias" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) { "LMICP", NULL, "Mic Bias" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) { "Mic Bias", NULL, "External Microphone" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* migor digital audio interface glue - connects codec <--> CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) SND_SOC_DAILINK_DEFS(wm8978,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) DAILINK_COMP_ARRAY(COMP_CPU("siu-pcm-audio")),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) DAILINK_COMP_ARRAY(COMP_CODEC("wm8978.0-001a", "wm8978-hifi")),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) DAILINK_COMP_ARRAY(COMP_PLATFORM("siu-pcm-audio")));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static struct snd_soc_dai_link migor_dai = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .name = "wm8978",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .stream_name = "WM8978",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .dai_fmt = SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_I2S |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) SND_SOC_DAIFMT_CBS_CFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .ops = &migor_dai_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) SND_SOC_DAILINK_REG(wm8978),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* migor audio machine driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct snd_soc_card snd_soc_migor = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .name = "Migo-R",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .dai_link = &migor_dai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .num_links = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .dapm_widgets = migor_dapm_widgets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .dapm_routes = audio_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .num_dapm_routes = ARRAY_SIZE(audio_map),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static struct platform_device *migor_snd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int __init migor_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ret = clk_register(&siumckb_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) siumckb_lookup = clkdev_create(&siumckb_clk, "siumckb_clk", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (!siumckb_lookup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto eclkdevalloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Port number used on this machine: port B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) migor_snd_device = platform_device_alloc("soc-audio", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!migor_snd_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto epdevalloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) platform_set_drvdata(migor_snd_device, &snd_soc_migor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = platform_device_add(migor_snd_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto epdevadd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) epdevadd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) platform_device_put(migor_snd_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) epdevalloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) clkdev_drop(siumckb_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) eclkdevalloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) clk_unregister(&siumckb_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void __exit migor_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) clkdev_drop(siumckb_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) clk_unregister(&siumckb_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) platform_device_unregister(migor_snd_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) module_init(migor_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) module_exit(migor_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_DESCRIPTION("ALSA SoC Migor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_LICENSE("GPL v2");