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>
21 struct apq8016_sbc_data {
22 struct snd_soc_card card;
23 void __iomem *mic_iomux;
24 void __iomem *spkr_iomux;
25 struct snd_soc_jack jack;
29 #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
30 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
31 #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
32 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
33 #define DEFAULT_MCLK_RATE 9600000
35 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
37 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
38 struct snd_soc_dai *codec_dai;
39 struct snd_soc_component *component;
40 struct snd_soc_card *card = rtd->card;
41 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
44 switch (cpu_dai->id) {
46 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
51 /* Configure the Quat MI2S to TLMM */
52 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
53 MIC_CTRL_TLMM_SCLK_EN,
57 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
58 MIC_CTRL_TLMM_SCLK_EN,
64 dev_err(card->dev, "unsupported cpu dai configuration\n");
69 if (!pdata->jack_setup) {
70 struct snd_jack *jack;
72 rval = snd_soc_card_jack_new(card, "Headset Jack",
75 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
76 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
78 &pdata->jack, NULL, 0);
81 dev_err(card->dev, "Unable to add Headphone Jack\n");
85 jack = pdata->jack.jack;
87 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
88 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
89 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
90 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
91 pdata->jack_setup = true;
94 for_each_rtd_codec_dais(rtd, i, codec_dai) {
96 component = codec_dai->component;
97 /* Set default mclk for internal codec */
98 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
100 if (rval != 0 && rval != -ENOTSUPP) {
101 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
104 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
105 if (rval != 0 && rval != -ENOTSUPP) {
106 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
114 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
116 struct snd_soc_dai_link *link;
119 for_each_card_prelinks(card, i, link)
120 link->init = apq8016_sbc_dai_init;
123 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
125 SND_SOC_DAPM_MIC("Handset Mic", NULL),
126 SND_SOC_DAPM_MIC("Headset Mic", NULL),
127 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
128 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
129 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
132 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
134 struct device *dev = &pdev->dev;
135 struct snd_soc_card *card;
136 struct apq8016_sbc_data *data;
137 struct resource *res;
140 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
146 card->owner = THIS_MODULE;
147 card->dapm_widgets = apq8016_sbc_dapm_widgets;
148 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
150 ret = qcom_snd_parse_of(card);
154 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
155 data->mic_iomux = devm_ioremap_resource(dev, res);
156 if (IS_ERR(data->mic_iomux))
157 return PTR_ERR(data->mic_iomux);
159 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
160 data->spkr_iomux = devm_ioremap_resource(dev, res);
161 if (IS_ERR(data->spkr_iomux))
162 return PTR_ERR(data->spkr_iomux);
164 snd_soc_card_set_drvdata(card, data);
166 apq8016_sbc_add_ops(card);
167 return devm_snd_soc_register_card(&pdev->dev, card);
170 static const struct of_device_id apq8016_sbc_device_id[] = {
171 { .compatible = "qcom,apq8016-sbc-sndcard" },
174 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
176 static struct platform_driver apq8016_sbc_platform_driver = {
178 .name = "qcom-apq8016-sbc",
179 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
181 .probe = apq8016_sbc_platform_probe,
183 module_platform_driver(apq8016_sbc_platform_driver);
185 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
186 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
187 MODULE_LICENSE("GPL v2");