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 
apq8016_dai_init(struct snd_soc_pcm_runtime * rtd,int mi2s)47 static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
48 {
49 	struct snd_soc_dai *codec_dai;
50 	struct snd_soc_component *component;
51 	struct snd_soc_card *card = rtd->card;
52 	struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
53 	int i, rval;
54 	u32 value;
55 
56 	switch (mi2s) {
57 	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;
68 	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;
77 	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;
83 
84 	default:
85 		dev_err(card->dev, "unsupported cpu dai configuration\n");
86 		return -EINVAL;
87 
88 	}
89 
90 	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);
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 
108 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
109 		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 
115 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
116 
117 		component = codec_dai->component;
118 		/* Set default mclk for internal codec */
119 		rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
120 				       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 		}
125 		rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
126 		if (rval != 0 && rval != -ENOTSUPP) {
127 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
128 			return rval;
129 		}
130 	}
131 
132 	return 0;
133 }
134 
apq8016_sbc_dai_init(struct snd_soc_pcm_runtime * rtd)135 static 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 
apq8016_sbc_add_ops(struct snd_soc_card * card)142 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
143 {
144 	struct snd_soc_dai_link *link;
145 	int i;
146 
147 	for_each_card_prelinks(card, i, link)
148 		link->init = apq8016_sbc_dai_init;
149 }
150 
qdsp6_dai_get_lpass_id(struct snd_soc_dai * cpu_dai)151 static 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 
msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime * rtd)171 static 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_BP_FP);
176 	return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
177 }
178 
msm8916_qdsp6_startup(struct snd_pcm_substream * substream)179 static 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 
msm8916_qdsp6_shutdown(struct snd_pcm_substream * substream)200 static 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 
220 static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
221 	.startup = msm8916_qdsp6_startup,
222 	.shutdown = msm8916_qdsp6_shutdown,
223 };
224 
msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)225 static 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 
msm8916_qdsp6_add_ops(struct snd_soc_card * card)241 static 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 
258 static 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 
apq8016_sbc_platform_probe(struct platform_device * pdev)267 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
268 {
269 	void (*add_ops)(struct snd_soc_card *card);
270 	struct device *dev = &pdev->dev;
271 	struct snd_soc_card *card;
272 	struct apq8016_sbc_data *data;
273 	int ret;
274 
275 	add_ops = device_get_match_data(&pdev->dev);
276 	if (!add_ops)
277 		return -EINVAL;
278 
279 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
280 	if (!data)
281 		return -ENOMEM;
282 
283 	card = &data->card;
284 	card->dev = dev;
285 	card->owner = THIS_MODULE;
286 	card->dapm_widgets = apq8016_sbc_dapm_widgets;
287 	card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
288 
289 	ret = qcom_snd_parse_of(card);
290 	if (ret)
291 		return ret;
292 
293 	data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
294 	if (IS_ERR(data->mic_iomux))
295 		return PTR_ERR(data->mic_iomux);
296 
297 	data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
298 	if (IS_ERR(data->spkr_iomux))
299 		return PTR_ERR(data->spkr_iomux);
300 
301 	snd_soc_card_set_drvdata(card, data);
302 
303 	add_ops(card);
304 	return devm_snd_soc_register_card(&pdev->dev, card);
305 }
306 
307 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
308 	{ .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
309 	{ .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
310 	{},
311 };
312 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
313 
314 static 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 };
321 module_platform_driver(apq8016_sbc_platform_driver);
322 
323 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
324 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
325 MODULE_LICENSE("GPL v2");
326