2 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/jack.h>
25 #include <sound/soc.h>
26 #include <uapi/linux/input-event-codes.h>
27 #include <dt-bindings/sound/apq8016-lpass.h>
29 struct apq8016_sbc_data {
30 void __iomem *mic_iomux;
31 void __iomem *spkr_iomux;
32 struct snd_soc_jack jack;
34 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
37 #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
38 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
39 #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
40 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
41 #define DEFAULT_MCLK_RATE 9600000
43 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
45 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
46 struct snd_soc_component *component;
47 struct snd_soc_dai_link *dai_link = rtd->dai_link;
48 struct snd_soc_card *card = rtd->card;
49 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
52 switch (cpu_dai->id) {
54 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
59 /* Configure the Quat MI2S to TLMM */
60 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
61 MIC_CTRL_TLMM_SCLK_EN,
65 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
66 MIC_CTRL_TLMM_SCLK_EN,
72 dev_err(card->dev, "unsupported cpu dai configuration\n");
77 if (!pdata->jack_setup) {
78 struct snd_jack *jack;
80 rval = snd_soc_card_jack_new(card, "Headset Jack",
83 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
84 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
86 &pdata->jack, NULL, 0);
89 dev_err(card->dev, "Unable to add Headphone Jack\n");
93 jack = pdata->jack.jack;
95 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
96 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
97 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
98 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
99 pdata->jack_setup = true;
102 for (i = 0 ; i < dai_link->num_codecs; i++) {
103 struct snd_soc_dai *dai = rtd->codec_dais[i];
105 component = dai->component;
106 /* Set default mclk for internal codec */
107 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
109 if (rval != 0 && rval != -ENOTSUPP) {
110 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
113 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
114 if (rval != 0 && rval != -ENOTSUPP) {
115 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
123 static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
125 struct device *dev = card->dev;
126 struct snd_soc_dai_link *link;
127 struct device_node *np, *codec, *cpu, *node = dev->of_node;
128 struct apq8016_sbc_data *data;
131 ret = snd_soc_of_parse_card_name(card, "qcom,model");
133 dev_err(dev, "Error parsing card name: %d\n", ret);
138 if (of_property_read_bool(node, "qcom,audio-routing")) {
139 ret = snd_soc_of_parse_audio_routing(card,
140 "qcom,audio-routing");
147 num_links = of_get_child_count(node);
149 /* Allocate the private data and the DAI link array */
150 data = devm_kzalloc(dev,
151 struct_size(data, dai_link, num_links),
154 return ERR_PTR(-ENOMEM);
156 card->dai_link = &data->dai_link[0];
157 card->num_links = num_links;
159 link = data->dai_link;
161 for_each_child_of_node(node, np) {
162 cpu = of_get_child_by_name(np, "cpu");
163 codec = of_get_child_by_name(np, "codec");
165 if (!cpu || !codec) {
166 dev_err(dev, "Can't find cpu/codec DT node\n");
171 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
172 if (!link->cpu_of_node) {
173 dev_err(card->dev, "error getting cpu phandle\n");
178 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
180 dev_err(card->dev, "error getting cpu dai name\n");
184 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
187 dev_err(card->dev, "error getting codec dai name\n");
191 link->platform_of_node = link->cpu_of_node;
192 ret = of_property_read_string(np, "link-name", &link->name);
194 dev_err(card->dev, "error getting codec dai_link name\n");
198 link->stream_name = link->name;
199 link->init = apq8016_sbc_dai_init;
215 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
217 SND_SOC_DAPM_MIC("Handset Mic", NULL),
218 SND_SOC_DAPM_MIC("Headset Mic", NULL),
219 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
220 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
221 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
224 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
226 struct device *dev = &pdev->dev;
227 struct snd_soc_card *card;
228 struct apq8016_sbc_data *data;
229 struct resource *res;
231 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
236 card->owner = THIS_MODULE;
237 card->dapm_widgets = apq8016_sbc_dapm_widgets;
238 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
239 data = apq8016_sbc_parse_of(card);
241 dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
243 return PTR_ERR(data);
246 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
247 data->mic_iomux = devm_ioremap_resource(dev, res);
248 if (IS_ERR(data->mic_iomux))
249 return PTR_ERR(data->mic_iomux);
251 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
252 data->spkr_iomux = devm_ioremap_resource(dev, res);
253 if (IS_ERR(data->spkr_iomux))
254 return PTR_ERR(data->spkr_iomux);
256 snd_soc_card_set_drvdata(card, data);
258 return devm_snd_soc_register_card(&pdev->dev, card);
261 static const struct of_device_id apq8016_sbc_device_id[] = {
262 { .compatible = "qcom,apq8016-sbc-sndcard" },
265 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
267 static struct platform_driver apq8016_sbc_platform_driver = {
269 .name = "qcom-apq8016-sbc",
270 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
272 .probe = apq8016_sbc_platform_probe,
274 module_platform_driver(apq8016_sbc_platform_driver);
276 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
277 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
278 MODULE_LICENSE("GPL v2");