1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022, Linaro Limited
4 #include <dt-bindings/sound/qcom,q6afe.h>
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
8 #include <sound/soc-dapm.h>
10 #include <linux/soundwire/sdw.h>
11 #include <sound/jack.h>
12 #include <linux/input-event-codes.h>
13 #include "qdsp6/q6afe.h"
17 struct sc8280xp_snd_data {
18 bool stream_prepared[AFE_PORT_MAX];
19 struct snd_soc_card *card;
20 struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
21 struct snd_soc_jack jack;
25 static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
27 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
28 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
29 struct snd_soc_card *card = rtd->card;
31 switch (cpu_dai->id) {
32 case WSA_CODEC_DMA_RX_0:
33 case WSA_CODEC_DMA_RX_1:
35 * Set limit of -3 dB on Digital Volume and 0 dB on PA Volume
36 * to reduce the risk of speaker damage until we have active
37 * speaker protection in place.
39 snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 81);
40 snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 81);
41 snd_soc_limit_volume(card, "SpkrLeft PA Volume", 17);
42 snd_soc_limit_volume(card, "SpkrRight PA Volume", 17);
48 return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
51 static void sc8280xp_snd_shutdown(struct snd_pcm_substream *substream)
53 struct snd_soc_pcm_runtime *rtd = substream->private_data;
54 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
55 struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
56 struct sdw_stream_runtime *sruntime = pdata->sruntime[cpu_dai->id];
58 pdata->sruntime[cpu_dai->id] = NULL;
59 sdw_release_stream(sruntime);
62 static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
63 struct snd_pcm_hw_params *params)
65 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
66 struct snd_interval *rate = hw_param_interval(params,
67 SNDRV_PCM_HW_PARAM_RATE);
68 struct snd_interval *channels = hw_param_interval(params,
69 SNDRV_PCM_HW_PARAM_CHANNELS);
71 rate->min = rate->max = 48000;
74 switch (cpu_dai->id) {
75 case TX_CODEC_DMA_TX_0:
76 case TX_CODEC_DMA_TX_1:
77 case TX_CODEC_DMA_TX_2:
78 case TX_CODEC_DMA_TX_3:
89 static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *params)
92 struct snd_soc_pcm_runtime *rtd = substream->private_data;
93 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
94 struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
96 return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
99 static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
102 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
103 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
104 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
106 return qcom_snd_sdw_prepare(substream, sruntime,
107 &data->stream_prepared[cpu_dai->id]);
110 static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
112 struct snd_soc_pcm_runtime *rtd = substream->private_data;
113 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
114 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
115 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
117 return qcom_snd_sdw_hw_free(substream, sruntime,
118 &data->stream_prepared[cpu_dai->id]);
121 static const struct snd_soc_ops sc8280xp_be_ops = {
122 .startup = qcom_snd_sdw_startup,
123 .shutdown = sc8280xp_snd_shutdown,
124 .hw_params = sc8280xp_snd_hw_params,
125 .hw_free = sc8280xp_snd_hw_free,
126 .prepare = sc8280xp_snd_prepare,
129 static void sc8280xp_add_be_ops(struct snd_soc_card *card)
131 struct snd_soc_dai_link *link;
134 for_each_card_prelinks(card, i, link) {
135 if (link->no_pcm == 1) {
136 link->init = sc8280xp_snd_init;
137 link->be_hw_params_fixup = sc8280xp_be_hw_params_fixup;
138 link->ops = &sc8280xp_be_ops;
143 static int sc8280xp_platform_probe(struct platform_device *pdev)
145 struct snd_soc_card *card;
146 struct sc8280xp_snd_data *data;
147 struct device *dev = &pdev->dev;
150 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
153 card->owner = THIS_MODULE;
154 /* Allocate the private data */
155 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
160 dev_set_drvdata(dev, card);
161 snd_soc_card_set_drvdata(card, data);
162 ret = qcom_snd_parse_of(card);
166 card->driver_name = of_device_get_match_data(dev);
167 sc8280xp_add_be_ops(card);
168 return devm_snd_soc_register_card(dev, card);
171 static const struct of_device_id snd_sc8280xp_dt_match[] = {
172 {.compatible = "qcom,sc8280xp-sndcard", "sc8280xp"},
173 {.compatible = "qcom,sm8450-sndcard", "sm8450"},
174 {.compatible = "qcom,sm8550-sndcard", "sm8550"},
175 {.compatible = "qcom,sm8650-sndcard", "sm8650"},
179 MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
181 static struct platform_driver snd_sc8280xp_driver = {
182 .probe = sc8280xp_platform_probe,
184 .name = "snd-sc8280xp",
185 .of_match_table = snd_sc8280xp_dt_match,
188 module_platform_driver(snd_sc8280xp_driver);
189 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
190 MODULE_DESCRIPTION("SC8280XP ASoC Machine Driver");
191 MODULE_LICENSE("GPL");