1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
11 #include <linux/clk.h>
12 #include <linux/platform_device.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/jack.h>
16 #include <sound/soc.h>
17 #include <uapi/linux/input-event-codes.h>
18 #include <dt-bindings/sound/apq8016-lpass.h>
20 #include "qdsp6/q6afe.h"
22 #define MI2S_COUNT (MI2S_QUATERNARY + 1)
24 struct apq8016_sbc_data {
25 struct snd_soc_card card;
26 void __iomem *mic_iomux;
27 void __iomem *spkr_iomux;
28 struct snd_soc_jack jack;
30 int mi2s_clk_count[MI2S_COUNT];
33 #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
34 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
35 #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
36 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
37 #define SPKR_CTL_TLMM_MCLK_EN BIT(1)
38 #define SPKR_CTL_TLMM_SCLK_EN BIT(2)
39 #define SPKR_CTL_TLMM_DATA1_EN BIT(3)
40 #define SPKR_CTL_TLMM_WS_OUT_SEL_MASK GENMASK(7, 6)
41 #define SPKR_CTL_TLMM_WS_OUT_SEL_SEC BIT(6)
42 #define SPKR_CTL_TLMM_WS_EN_SEL_MASK GENMASK(19, 18)
43 #define SPKR_CTL_TLMM_WS_EN_SEL_SEC BIT(18)
44 #define DEFAULT_MCLK_RATE 9600000
45 #define MI2S_BCLK_RATE 1536000
47 static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
49 struct snd_soc_dai *codec_dai;
50 struct snd_soc_component *component;
51 struct snd_soc_card *card = rtd->card;
52 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
58 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
63 /* Configure the Quat MI2S to TLMM */
64 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
65 MIC_CTRL_TLMM_SCLK_EN,
69 /* Clear TLMM_WS_OUT_SEL and TLMM_WS_EN_SEL fields */
70 value = readl(pdata->spkr_iomux) &
71 ~(SPKR_CTL_TLMM_WS_OUT_SEL_MASK | SPKR_CTL_TLMM_WS_EN_SEL_MASK);
72 /* Configure the Sec MI2S to TLMM */
73 writel(value | SPKR_CTL_TLMM_MCLK_EN | SPKR_CTL_TLMM_SCLK_EN |
74 SPKR_CTL_TLMM_DATA1_EN | SPKR_CTL_TLMM_WS_OUT_SEL_SEC |
75 SPKR_CTL_TLMM_WS_EN_SEL_SEC, pdata->spkr_iomux);
78 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
79 MIC_CTRL_TLMM_SCLK_EN,
85 dev_err(card->dev, "unsupported cpu dai configuration\n");
90 if (!pdata->jack_setup) {
91 struct snd_jack *jack;
93 rval = snd_soc_card_jack_new(card, "Headset Jack",
96 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
97 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
102 dev_err(card->dev, "Unable to add Headphone Jack\n");
106 jack = pdata->jack.jack;
108 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
109 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
110 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
111 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
112 pdata->jack_setup = true;
115 for_each_rtd_codec_dais(rtd, i, codec_dai) {
117 component = codec_dai->component;
118 /* Set default mclk for internal codec */
119 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
121 if (rval != 0 && rval != -ENOTSUPP) {
122 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
125 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
126 if (rval != 0 && rval != -ENOTSUPP) {
127 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
135 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
137 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
139 return apq8016_dai_init(rtd, cpu_dai->id);
142 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
144 struct snd_soc_dai_link *link;
147 for_each_card_prelinks(card, i, link)
148 link->init = apq8016_sbc_dai_init;
151 static int qdsp6_dai_get_lpass_id(struct snd_soc_dai *cpu_dai)
153 switch (cpu_dai->id) {
154 case PRIMARY_MI2S_RX:
155 case PRIMARY_MI2S_TX:
157 case SECONDARY_MI2S_RX:
158 case SECONDARY_MI2S_TX:
159 return MI2S_SECONDARY;
160 case TERTIARY_MI2S_RX:
161 case TERTIARY_MI2S_TX:
162 return MI2S_TERTIARY;
163 case QUATERNARY_MI2S_RX:
164 case QUATERNARY_MI2S_TX:
165 return MI2S_QUATERNARY;
171 static int msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime *rtd)
173 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
175 snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP);
176 return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
179 static int msm8916_qdsp6_startup(struct snd_pcm_substream *substream)
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
182 struct snd_soc_card *card = rtd->card;
183 struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
184 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
187 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
191 if (++data->mi2s_clk_count[mi2s] > 1)
194 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, MI2S_BCLK_RATE, 0);
196 dev_err(card->dev, "Failed to enable LPAIF bit clk: %d\n", ret);
200 static void msm8916_qdsp6_shutdown(struct snd_pcm_substream *substream)
202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
203 struct snd_soc_card *card = rtd->card;
204 struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
205 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
208 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
212 if (--data->mi2s_clk_count[mi2s] > 0)
215 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, 0, 0);
217 dev_err(card->dev, "Failed to disable LPAIF bit clk: %d\n", ret);
220 static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
221 .startup = msm8916_qdsp6_startup,
222 .shutdown = msm8916_qdsp6_shutdown,
225 static int msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
226 struct snd_pcm_hw_params *params)
228 struct snd_interval *rate = hw_param_interval(params,
229 SNDRV_PCM_HW_PARAM_RATE);
230 struct snd_interval *channels = hw_param_interval(params,
231 SNDRV_PCM_HW_PARAM_CHANNELS);
232 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
234 rate->min = rate->max = 48000;
235 channels->min = channels->max = 2;
236 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
241 static void msm8916_qdsp6_add_ops(struct snd_soc_card *card)
243 struct snd_soc_dai_link *link;
246 /* Make it obvious to userspace that QDSP6 is used */
247 card->components = "qdsp6";
249 for_each_card_prelinks(card, i, link) {
251 link->init = msm8916_qdsp6_dai_init;
252 link->ops = &msm8916_qdsp6_be_ops;
253 link->be_hw_params_fixup = msm8916_qdsp6_be_hw_params_fixup;
258 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
260 SND_SOC_DAPM_MIC("Handset Mic", NULL),
261 SND_SOC_DAPM_MIC("Headset Mic", NULL),
262 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
263 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
264 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
267 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
269 void (*add_ops)(struct snd_soc_card *card);
270 struct device *dev = &pdev->dev;
271 struct snd_soc_card *card;
272 struct apq8016_sbc_data *data;
275 add_ops = device_get_match_data(&pdev->dev);
279 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
285 card->owner = THIS_MODULE;
286 card->dapm_widgets = apq8016_sbc_dapm_widgets;
287 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
289 ret = qcom_snd_parse_of(card);
293 data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
294 if (IS_ERR(data->mic_iomux))
295 return PTR_ERR(data->mic_iomux);
297 data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
298 if (IS_ERR(data->spkr_iomux))
299 return PTR_ERR(data->spkr_iomux);
301 snd_soc_card_set_drvdata(card, data);
304 return devm_snd_soc_register_card(&pdev->dev, card);
307 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
308 { .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
309 { .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
312 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
314 static struct platform_driver apq8016_sbc_platform_driver = {
316 .name = "qcom-apq8016-sbc",
317 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
319 .probe = apq8016_sbc_platform_probe,
321 module_platform_driver(apq8016_sbc_platform_driver);
323 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
324 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
325 MODULE_LICENSE("GPL v2");