1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
4 */
5
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>
19 #include "common.h"
20 #include "qdsp6/q6afe.h"
21
22 #define MI2S_COUNT (MI2S_QUATERNARY + 1)
23
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;
29 bool jack_setup;
30 int mi2s_clk_count[MI2S_COUNT];
31 };
32
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
46
47 static struct snd_soc_jack_pin apq8016_sbc_jack_pins[] = {
48 {
49 .pin = "Mic Jack",
50 .mask = SND_JACK_MICROPHONE,
51 },
52 {
53 .pin = "Headphone Jack",
54 .mask = SND_JACK_HEADPHONE,
55 },
56 };
57
apq8016_dai_init(struct snd_soc_pcm_runtime * rtd,int mi2s)58 static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
59 {
60 struct snd_soc_dai *codec_dai;
61 struct snd_soc_component *component;
62 struct snd_soc_card *card = rtd->card;
63 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
64 int i, rval;
65 u32 value;
66
67 switch (mi2s) {
68 case MI2S_PRIMARY:
69 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
70 pdata->spkr_iomux);
71 break;
72
73 case MI2S_QUATERNARY:
74 /* Configure the Quat MI2S to TLMM */
75 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
76 MIC_CTRL_TLMM_SCLK_EN,
77 pdata->mic_iomux);
78 break;
79 case MI2S_SECONDARY:
80 /* Clear TLMM_WS_OUT_SEL and TLMM_WS_EN_SEL fields */
81 value = readl(pdata->spkr_iomux) &
82 ~(SPKR_CTL_TLMM_WS_OUT_SEL_MASK | SPKR_CTL_TLMM_WS_EN_SEL_MASK);
83 /* Configure the Sec MI2S to TLMM */
84 writel(value | SPKR_CTL_TLMM_MCLK_EN | SPKR_CTL_TLMM_SCLK_EN |
85 SPKR_CTL_TLMM_DATA1_EN | SPKR_CTL_TLMM_WS_OUT_SEL_SEC |
86 SPKR_CTL_TLMM_WS_EN_SEL_SEC, pdata->spkr_iomux);
87 break;
88 case MI2S_TERTIARY:
89 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
90 MIC_CTRL_TLMM_SCLK_EN,
91 pdata->mic_iomux);
92
93 break;
94
95 default:
96 dev_err(card->dev, "unsupported cpu dai configuration\n");
97 return -EINVAL;
98
99 }
100
101 if (!pdata->jack_setup) {
102 struct snd_jack *jack;
103
104 rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
105 SND_JACK_HEADSET |
106 SND_JACK_HEADPHONE |
107 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
108 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
109 SND_JACK_BTN_4,
110 &pdata->jack,
111 apq8016_sbc_jack_pins,
112 ARRAY_SIZE(apq8016_sbc_jack_pins));
113
114 if (rval < 0) {
115 dev_err(card->dev, "Unable to add Headphone Jack\n");
116 return rval;
117 }
118
119 jack = pdata->jack.jack;
120
121 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
122 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
123 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
124 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
125 pdata->jack_setup = true;
126 }
127
128 for_each_rtd_codec_dais(rtd, i, codec_dai) {
129
130 component = codec_dai->component;
131 /* Set default mclk for internal codec */
132 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
133 SND_SOC_CLOCK_IN);
134 if (rval != 0 && rval != -ENOTSUPP) {
135 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
136 return rval;
137 }
138 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
139 if (rval != 0 && rval != -ENOTSUPP) {
140 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
141 return rval;
142 }
143 }
144
145 return 0;
146 }
147
apq8016_sbc_dai_init(struct snd_soc_pcm_runtime * rtd)148 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
149 {
150 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
151
152 return apq8016_dai_init(rtd, cpu_dai->id);
153 }
154
apq8016_sbc_add_ops(struct snd_soc_card * card)155 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
156 {
157 struct snd_soc_dai_link *link;
158 int i;
159
160 for_each_card_prelinks(card, i, link)
161 link->init = apq8016_sbc_dai_init;
162 }
163
qdsp6_dai_get_lpass_id(struct snd_soc_dai * cpu_dai)164 static int qdsp6_dai_get_lpass_id(struct snd_soc_dai *cpu_dai)
165 {
166 switch (cpu_dai->id) {
167 case PRIMARY_MI2S_RX:
168 case PRIMARY_MI2S_TX:
169 return MI2S_PRIMARY;
170 case SECONDARY_MI2S_RX:
171 case SECONDARY_MI2S_TX:
172 return MI2S_SECONDARY;
173 case TERTIARY_MI2S_RX:
174 case TERTIARY_MI2S_TX:
175 return MI2S_TERTIARY;
176 case QUATERNARY_MI2S_RX:
177 case QUATERNARY_MI2S_TX:
178 return MI2S_QUATERNARY;
179 default:
180 return -EINVAL;
181 }
182 }
183
msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime * rtd)184 static int msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime *rtd)
185 {
186 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
187
188 snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP);
189 return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
190 }
191
msm8916_qdsp6_startup(struct snd_pcm_substream * substream)192 static int msm8916_qdsp6_startup(struct snd_pcm_substream *substream)
193 {
194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
195 struct snd_soc_card *card = rtd->card;
196 struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
197 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
198 int mi2s, ret;
199
200 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
201 if (mi2s < 0)
202 return mi2s;
203
204 if (++data->mi2s_clk_count[mi2s] > 1)
205 return 0;
206
207 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, MI2S_BCLK_RATE, 0);
208 if (ret)
209 dev_err(card->dev, "Failed to enable LPAIF bit clk: %d\n", ret);
210 return ret;
211 }
212
msm8916_qdsp6_shutdown(struct snd_pcm_substream * substream)213 static void msm8916_qdsp6_shutdown(struct snd_pcm_substream *substream)
214 {
215 struct snd_soc_pcm_runtime *rtd = substream->private_data;
216 struct snd_soc_card *card = rtd->card;
217 struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
218 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
219 int mi2s, ret;
220
221 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
222 if (mi2s < 0)
223 return;
224
225 if (--data->mi2s_clk_count[mi2s] > 0)
226 return;
227
228 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, 0, 0);
229 if (ret)
230 dev_err(card->dev, "Failed to disable LPAIF bit clk: %d\n", ret);
231 }
232
233 static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
234 .startup = msm8916_qdsp6_startup,
235 .shutdown = msm8916_qdsp6_shutdown,
236 };
237
msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)238 static int msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
239 struct snd_pcm_hw_params *params)
240 {
241 struct snd_interval *rate = hw_param_interval(params,
242 SNDRV_PCM_HW_PARAM_RATE);
243 struct snd_interval *channels = hw_param_interval(params,
244 SNDRV_PCM_HW_PARAM_CHANNELS);
245 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
246
247 rate->min = rate->max = 48000;
248 channels->min = channels->max = 2;
249 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
250
251 return 0;
252 }
253
msm8916_qdsp6_add_ops(struct snd_soc_card * card)254 static void msm8916_qdsp6_add_ops(struct snd_soc_card *card)
255 {
256 struct snd_soc_dai_link *link;
257 int i;
258
259 /* Make it obvious to userspace that QDSP6 is used */
260 card->components = "qdsp6";
261
262 for_each_card_prelinks(card, i, link) {
263 if (link->no_pcm) {
264 link->init = msm8916_qdsp6_dai_init;
265 link->ops = &msm8916_qdsp6_be_ops;
266 link->be_hw_params_fixup = msm8916_qdsp6_be_hw_params_fixup;
267 }
268 }
269 }
270
271 static const struct snd_kcontrol_new apq8016_sbc_snd_controls[] = {
272 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
273 SOC_DAPM_PIN_SWITCH("Mic Jack"),
274 };
275
276 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
277 SND_SOC_DAPM_HP("Headphone Jack", NULL),
278 SND_SOC_DAPM_MIC("Mic Jack", NULL),
279 SND_SOC_DAPM_MIC("Handset Mic", NULL),
280 SND_SOC_DAPM_MIC("Headset Mic", NULL),
281 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
282 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
283 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
284 };
285
apq8016_sbc_platform_probe(struct platform_device * pdev)286 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
287 {
288 void (*add_ops)(struct snd_soc_card *card);
289 struct device *dev = &pdev->dev;
290 struct snd_soc_card *card;
291 struct apq8016_sbc_data *data;
292 int ret;
293
294 add_ops = device_get_match_data(&pdev->dev);
295 if (!add_ops)
296 return -EINVAL;
297
298 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
299 if (!data)
300 return -ENOMEM;
301
302 card = &data->card;
303 card->dev = dev;
304 card->owner = THIS_MODULE;
305 card->dapm_widgets = apq8016_sbc_dapm_widgets;
306 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
307 card->controls = apq8016_sbc_snd_controls;
308 card->num_controls = ARRAY_SIZE(apq8016_sbc_snd_controls);
309
310 ret = qcom_snd_parse_of(card);
311 if (ret)
312 return ret;
313
314 data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
315 if (IS_ERR(data->mic_iomux))
316 return PTR_ERR(data->mic_iomux);
317
318 data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
319 if (IS_ERR(data->spkr_iomux))
320 return PTR_ERR(data->spkr_iomux);
321
322 snd_soc_card_set_drvdata(card, data);
323
324 add_ops(card);
325 return devm_snd_soc_register_card(&pdev->dev, card);
326 }
327
328 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
329 { .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
330 { .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
331 {},
332 };
333 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
334
335 static struct platform_driver apq8016_sbc_platform_driver = {
336 .driver = {
337 .name = "qcom-apq8016-sbc",
338 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
339 },
340 .probe = apq8016_sbc_platform_probe,
341 };
342 module_platform_driver(apq8016_sbc_platform_driver);
343
344 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
345 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
346 MODULE_LICENSE("GPL v2");
347