Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / sound / soc / qcom / apq8016_sbc.c
blob0f56bcc46b00d5770763ee40b32f4a17db699ecc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
4 */
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/io.h>
10 #include <linux/of.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 struct apq8016_sbc_data {
21 void __iomem *mic_iomux;
22 void __iomem *spkr_iomux;
23 struct snd_soc_jack jack;
24 bool jack_setup;
25 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
28 #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
29 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
30 #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
31 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
32 #define DEFAULT_MCLK_RATE 9600000
34 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
36 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
37 struct snd_soc_component *component;
38 struct snd_soc_dai_link *dai_link = rtd->dai_link;
39 struct snd_soc_card *card = rtd->card;
40 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
41 int i, rval;
43 switch (cpu_dai->id) {
44 case MI2S_PRIMARY:
45 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
46 pdata->spkr_iomux);
47 break;
49 case MI2S_QUATERNARY:
50 /* Configure the Quat MI2S to TLMM */
51 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
52 MIC_CTRL_TLMM_SCLK_EN,
53 pdata->mic_iomux);
54 break;
55 case MI2S_TERTIARY:
56 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
57 MIC_CTRL_TLMM_SCLK_EN,
58 pdata->mic_iomux);
60 break;
62 default:
63 dev_err(card->dev, "unsupported cpu dai configuration\n");
64 return -EINVAL;
68 if (!pdata->jack_setup) {
69 struct snd_jack *jack;
71 rval = snd_soc_card_jack_new(card, "Headset Jack",
72 SND_JACK_HEADSET |
73 SND_JACK_HEADPHONE |
74 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
75 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
76 SND_JACK_BTN_4,
77 &pdata->jack, NULL, 0);
79 if (rval < 0) {
80 dev_err(card->dev, "Unable to add Headphone Jack\n");
81 return rval;
84 jack = pdata->jack.jack;
86 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
87 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
88 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
89 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
90 pdata->jack_setup = true;
93 for (i = 0 ; i < dai_link->num_codecs; i++) {
94 struct snd_soc_dai *dai = rtd->codec_dais[i];
96 component = dai->component;
97 /* Set default mclk for internal codec */
98 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
99 SND_SOC_CLOCK_IN);
100 if (rval != 0 && rval != -ENOTSUPP) {
101 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
102 return 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);
107 return rval;
111 return 0;
114 static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
116 struct device *dev = card->dev;
117 struct snd_soc_dai_link *link;
118 struct device_node *np, *codec, *cpu, *node = dev->of_node;
119 struct apq8016_sbc_data *data;
120 int ret, num_links;
122 ret = snd_soc_of_parse_card_name(card, "qcom,model");
123 if (ret) {
124 dev_err(dev, "Error parsing card name: %d\n", ret);
125 return ERR_PTR(ret);
128 /* DAPM routes */
129 if (of_property_read_bool(node, "qcom,audio-routing")) {
130 ret = snd_soc_of_parse_audio_routing(card,
131 "qcom,audio-routing");
132 if (ret)
133 return ERR_PTR(ret);
137 /* Populate links */
138 num_links = of_get_child_count(node);
140 /* Allocate the private data and the DAI link array */
141 data = devm_kzalloc(dev,
142 struct_size(data, dai_link, num_links),
143 GFP_KERNEL);
144 if (!data)
145 return ERR_PTR(-ENOMEM);
147 card->dai_link = &data->dai_link[0];
148 card->num_links = num_links;
150 link = data->dai_link;
152 for_each_child_of_node(node, np) {
153 cpu = of_get_child_by_name(np, "cpu");
154 codec = of_get_child_by_name(np, "codec");
156 if (!cpu || !codec) {
157 dev_err(dev, "Can't find cpu/codec DT node\n");
158 ret = -EINVAL;
159 goto error;
162 link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
163 if (!link->cpu_of_node) {
164 dev_err(card->dev, "error getting cpu phandle\n");
165 ret = -EINVAL;
166 goto error;
169 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
170 if (ret) {
171 dev_err(card->dev, "error getting cpu dai name\n");
172 goto error;
175 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
177 if (ret < 0) {
178 dev_err(card->dev, "error getting codec dai name\n");
179 goto error;
182 link->platform_of_node = link->cpu_of_node;
183 ret = of_property_read_string(np, "link-name", &link->name);
184 if (ret) {
185 dev_err(card->dev, "error getting codec dai_link name\n");
186 goto error;
189 link->stream_name = link->name;
190 link->init = apq8016_sbc_dai_init;
191 link++;
193 of_node_put(cpu);
194 of_node_put(codec);
197 return data;
199 error:
200 of_node_put(np);
201 of_node_put(cpu);
202 of_node_put(codec);
203 return ERR_PTR(ret);
206 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
208 SND_SOC_DAPM_MIC("Handset Mic", NULL),
209 SND_SOC_DAPM_MIC("Headset Mic", NULL),
210 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
211 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
212 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
215 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
217 struct device *dev = &pdev->dev;
218 struct snd_soc_card *card;
219 struct apq8016_sbc_data *data;
220 struct resource *res;
222 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
223 if (!card)
224 return -ENOMEM;
226 card->dev = dev;
227 card->dapm_widgets = apq8016_sbc_dapm_widgets;
228 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
229 data = apq8016_sbc_parse_of(card);
230 if (IS_ERR(data)) {
231 dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
232 PTR_ERR(data));
233 return PTR_ERR(data);
236 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
237 data->mic_iomux = devm_ioremap_resource(dev, res);
238 if (IS_ERR(data->mic_iomux))
239 return PTR_ERR(data->mic_iomux);
241 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
242 data->spkr_iomux = devm_ioremap_resource(dev, res);
243 if (IS_ERR(data->spkr_iomux))
244 return PTR_ERR(data->spkr_iomux);
246 snd_soc_card_set_drvdata(card, data);
248 return devm_snd_soc_register_card(&pdev->dev, card);
251 static const struct of_device_id apq8016_sbc_device_id[] = {
252 { .compatible = "qcom,apq8016-sbc-sndcard" },
255 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
257 static struct platform_driver apq8016_sbc_platform_driver = {
258 .driver = {
259 .name = "qcom-apq8016-sbc",
260 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
262 .probe = apq8016_sbc_platform_probe,
264 module_platform_driver(apq8016_sbc_platform_driver);
266 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
267 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
268 MODULE_LICENSE("GPL v2");