blob: f9d69375320e7f4756085e05faf0a65e7203aaab [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +01002/*
3 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +01004 */
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>
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020015#include <sound/jack.h>
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010016#include <sound/soc.h>
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020017#include <uapi/linux/input-event-codes.h>
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010018#include <dt-bindings/sound/apq8016-lpass.h>
Stephan Gerhold118205d22020-07-23 20:39:03 +020019#include "common.h"
Stephan Gerholda78a42f2021-12-02 15:55:05 +010020#include "qdsp6/q6afe.h"
21
22#define MI2S_COUNT (MI2S_QUATERNARY + 1)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010023
24struct apq8016_sbc_data {
Stephan Gerhold118205d22020-07-23 20:39:03 +020025 struct snd_soc_card card;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010026 void __iomem *mic_iomux;
27 void __iomem *spkr_iomux;
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020028 struct snd_soc_jack jack;
29 bool jack_setup;
Stephan Gerholda78a42f2021-12-02 15:55:05 +010030 int mi2s_clk_count[MI2S_COUNT];
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010031};
32
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000033#define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010034#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))
Vincent Knecht1e2c7842021-08-06 13:41:16 +020037#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)
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020044#define DEFAULT_MCLK_RATE 9600000
Stephan Gerholda78a42f2021-12-02 15:55:05 +010045#define MI2S_BCLK_RATE 1536000
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010046
Stephan Gerholda78a42f2021-12-02 15:55:05 +010047static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010048{
Kuninori Morimotocf4dae02020-02-19 15:56:09 +090049 struct snd_soc_dai *codec_dai;
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +000050 struct snd_soc_component *component;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010051 struct snd_soc_card *card = rtd->card;
52 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020053 int i, rval;
Vincent Knecht1e2c7842021-08-06 13:41:16 +020054 u32 value;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010055
Stephan Gerholda78a42f2021-12-02 15:55:05 +010056 switch (mi2s) {
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010057 case MI2S_PRIMARY:
58 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
59 pdata->spkr_iomux);
60 break;
61
62 case MI2S_QUATERNARY:
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,
66 pdata->mic_iomux);
67 break;
Vincent Knecht1e2c7842021-08-06 13:41:16 +020068 case MI2S_SECONDARY:
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);
76 break;
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000077 case MI2S_TERTIARY:
78 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
79 MIC_CTRL_TLMM_SCLK_EN,
80 pdata->mic_iomux);
81
82 break;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010083
84 default:
85 dev_err(card->dev, "unsupported cpu dai configuration\n");
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020086 return -EINVAL;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010087
88 }
89
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020090 if (!pdata->jack_setup) {
91 struct snd_jack *jack;
92
93 rval = snd_soc_card_jack_new(card, "Headset Jack",
94 SND_JACK_HEADSET |
95 SND_JACK_HEADPHONE |
96 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
97 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
98 SND_JACK_BTN_4,
99 &pdata->jack, NULL, 0);
100
101 if (rval < 0) {
102 dev_err(card->dev, "Unable to add Headphone Jack\n");
103 return rval;
104 }
105
106 jack = pdata->jack.jack;
107
Benson Leung5f6d1df2017-11-22 12:56:43 -0800108 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +0200109 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;
113 }
114
Kuninori Morimotoc998ee32020-03-09 13:07:57 +0900115 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200116
Kuninori Morimotocf4dae02020-02-19 15:56:09 +0900117 component = codec_dai->component;
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200118 /* Set default mclk for internal codec */
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +0000119 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200120 SND_SOC_CLOCK_IN);
121 if (rval != 0 && rval != -ENOTSUPP) {
122 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
123 return rval;
124 }
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +0000125 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +0200126 if (rval != 0 && rval != -ENOTSUPP) {
127 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
128 return rval;
129 }
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200130 }
131
132 return 0;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100133}
134
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100135static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
136{
137 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
138
139 return apq8016_dai_init(rtd, cpu_dai->id);
140}
141
Stephan Gerhold118205d22020-07-23 20:39:03 +0200142static void apq8016_sbc_add_ops(struct snd_soc_card *card)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100143{
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100144 struct snd_soc_dai_link *link;
Stephan Gerhold118205d22020-07-23 20:39:03 +0200145 int i;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100146
Stephan Gerhold118205d22020-07-23 20:39:03 +0200147 for_each_card_prelinks(card, i, link)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100148 link->init = apq8016_sbc_dai_init;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100149}
150
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100151static int qdsp6_dai_get_lpass_id(struct snd_soc_dai *cpu_dai)
152{
153 switch (cpu_dai->id) {
154 case PRIMARY_MI2S_RX:
155 case PRIMARY_MI2S_TX:
156 return MI2S_PRIMARY;
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;
166 default:
167 return -EINVAL;
168 }
169}
170
171static int msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime *rtd)
172{
173 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
174
175 snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_CBS_CFS);
176 return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
177}
178
179static int msm8916_qdsp6_startup(struct snd_pcm_substream *substream)
180{
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);
185 int mi2s, ret;
186
187 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
188 if (mi2s < 0)
189 return mi2s;
190
191 if (++data->mi2s_clk_count[mi2s] > 1)
192 return 0;
193
194 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, MI2S_BCLK_RATE, 0);
195 if (ret)
196 dev_err(card->dev, "Failed to enable LPAIF bit clk: %d\n", ret);
197 return ret;
198}
199
200static void msm8916_qdsp6_shutdown(struct snd_pcm_substream *substream)
201{
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);
206 int mi2s, ret;
207
208 mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
209 if (mi2s < 0)
210 return;
211
212 if (--data->mi2s_clk_count[mi2s] > 0)
213 return;
214
215 ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, 0, 0);
216 if (ret)
217 dev_err(card->dev, "Failed to disable LPAIF bit clk: %d\n", ret);
218}
219
220static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
221 .startup = msm8916_qdsp6_startup,
222 .shutdown = msm8916_qdsp6_shutdown,
223};
224
225static int msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
226 struct snd_pcm_hw_params *params)
227{
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);
233
234 rate->min = rate->max = 48000;
235 channels->min = channels->max = 2;
236 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
237
238 return 0;
239}
240
241static void msm8916_qdsp6_add_ops(struct snd_soc_card *card)
242{
243 struct snd_soc_dai_link *link;
244 int i;
245
246 /* Make it obvious to userspace that QDSP6 is used */
247 card->components = "qdsp6";
248
249 for_each_card_prelinks(card, i, link) {
250 if (link->no_pcm) {
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;
254 }
255 }
256}
257
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100258static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
259
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),
265};
266
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100267static int apq8016_sbc_platform_probe(struct platform_device *pdev)
268{
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100269 void (*add_ops)(struct snd_soc_card *card);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100270 struct device *dev = &pdev->dev;
271 struct snd_soc_card *card;
272 struct apq8016_sbc_data *data;
Stephan Gerhold118205d22020-07-23 20:39:03 +0200273 int ret;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100274
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100275 add_ops = device_get_match_data(&pdev->dev);
276 if (!add_ops)
277 return -EINVAL;
278
Stephan Gerhold118205d22020-07-23 20:39:03 +0200279 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
280 if (!data)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100281 return -ENOMEM;
282
Stephan Gerhold118205d22020-07-23 20:39:03 +0200283 card = &data->card;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100284 card->dev = dev;
Stephan Gerhold3c27ea22020-08-20 17:45:11 +0200285 card->owner = THIS_MODULE;
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100286 card->dapm_widgets = apq8016_sbc_dapm_widgets;
287 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
Stephan Gerhold118205d22020-07-23 20:39:03 +0200288
289 ret = qcom_snd_parse_of(card);
290 if (ret)
291 return ret;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100292
Yang Yingliange6bb5182021-06-15 22:07:10 +0800293 data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100294 if (IS_ERR(data->mic_iomux))
295 return PTR_ERR(data->mic_iomux);
296
Yang Yingliange6bb5182021-06-15 22:07:10 +0800297 data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100298 if (IS_ERR(data->spkr_iomux))
299 return PTR_ERR(data->spkr_iomux);
300
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100301 snd_soc_card_set_drvdata(card, data);
302
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100303 add_ops(card);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100304 return devm_snd_soc_register_card(&pdev->dev, card);
305}
306
Krzysztof Kozlowski377c7b72020-11-25 17:44:22 +0100307static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
Stephan Gerholda78a42f2021-12-02 15:55:05 +0100308 { .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
309 { .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100310 {},
311};
312MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
313
314static struct platform_driver apq8016_sbc_platform_driver = {
315 .driver = {
316 .name = "qcom-apq8016-sbc",
317 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
318 },
319 .probe = apq8016_sbc_platform_probe,
320};
321module_platform_driver(apq8016_sbc_platform_driver);
322
323MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
324MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
325MODULE_LICENSE("GPL v2");