1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022, Linaro Limited
3
4 #include <dt-bindings/sound/qcom,q6afe.h>
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
7 #include <sound/soc.h>
8 #include <sound/soc-dapm.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_params.h>
11 #include <linux/soundwire/sdw.h>
12 #include <sound/jack.h>
13 #include <linux/input-event-codes.h>
14 #include "qdsp6/q6afe.h"
15 #include "common.h"
16 #include "sdw.h"
17
18 struct sc8280xp_snd_data {
19 bool stream_prepared[AFE_PORT_MAX];
20 struct snd_soc_card *card;
21 struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
22 struct snd_soc_jack jack;
23 struct snd_soc_jack dp_jack[8];
24 bool jack_setup;
25 };
26
sc8280xp_snd_init(struct snd_soc_pcm_runtime * rtd)27 static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
28 {
29 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
30 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
31 struct snd_soc_card *card = rtd->card;
32 struct snd_soc_jack *dp_jack = NULL;
33 int dp_pcm_id = 0;
34
35 switch (cpu_dai->id) {
36 case PRIMARY_MI2S_RX...QUATERNARY_MI2S_TX:
37 case QUINARY_MI2S_RX...QUINARY_MI2S_TX:
38 snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP);
39 break;
40 case WSA_CODEC_DMA_RX_0:
41 case WSA_CODEC_DMA_RX_1:
42 /*
43 * Set limit of -3 dB on Digital Volume and 0 dB on PA Volume
44 * to reduce the risk of speaker damage until we have active
45 * speaker protection in place.
46 */
47 snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 81);
48 snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 81);
49 snd_soc_limit_volume(card, "SpkrLeft PA Volume", 17);
50 snd_soc_limit_volume(card, "SpkrRight PA Volume", 17);
51 break;
52 case DISPLAY_PORT_RX_0:
53 /* DISPLAY_PORT dai ids are not contiguous */
54 dp_pcm_id = 0;
55 dp_jack = &data->dp_jack[dp_pcm_id];
56 break;
57 case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:
58 dp_pcm_id = cpu_dai->id - DISPLAY_PORT_RX_1 + 1;
59 dp_jack = &data->dp_jack[dp_pcm_id];
60 break;
61 default:
62 break;
63 }
64
65 if (dp_jack)
66 return qcom_snd_dp_jack_setup(rtd, dp_jack, dp_pcm_id);
67
68 return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
69 }
70
sc8280xp_snd_shutdown(struct snd_pcm_substream * substream)71 static void sc8280xp_snd_shutdown(struct snd_pcm_substream *substream)
72 {
73 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
74 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
75 struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
76 struct sdw_stream_runtime *sruntime = pdata->sruntime[cpu_dai->id];
77
78 pdata->sruntime[cpu_dai->id] = NULL;
79 sdw_release_stream(sruntime);
80 }
81
sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)82 static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
83 struct snd_pcm_hw_params *params)
84 {
85 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
86 struct snd_interval *rate = hw_param_interval(params,
87 SNDRV_PCM_HW_PARAM_RATE);
88 struct snd_interval *channels = hw_param_interval(params,
89 SNDRV_PCM_HW_PARAM_CHANNELS);
90 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
91
92 rate->min = rate->max = 48000;
93 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
94 channels->min = 2;
95 channels->max = 2;
96 switch (cpu_dai->id) {
97 case TX_CODEC_DMA_TX_0:
98 case TX_CODEC_DMA_TX_1:
99 case TX_CODEC_DMA_TX_2:
100 case TX_CODEC_DMA_TX_3:
101 channels->min = 1;
102 break;
103 default:
104 break;
105 }
106
107
108 return 0;
109 }
110
sc8280xp_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)111 static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
112 struct snd_pcm_hw_params *params)
113 {
114 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
115 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
116 struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
117
118 return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
119 }
120
sc8280xp_snd_prepare(struct snd_pcm_substream * substream)121 static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
122 {
123 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
124 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
125 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
126 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
127
128 return qcom_snd_sdw_prepare(substream, sruntime,
129 &data->stream_prepared[cpu_dai->id]);
130 }
131
sc8280xp_snd_hw_free(struct snd_pcm_substream * substream)132 static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
133 {
134 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
135 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
136 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
137 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
138
139 return qcom_snd_sdw_hw_free(substream, sruntime,
140 &data->stream_prepared[cpu_dai->id]);
141 }
142
143 static const struct snd_soc_ops sc8280xp_be_ops = {
144 .startup = qcom_snd_sdw_startup,
145 .shutdown = sc8280xp_snd_shutdown,
146 .hw_params = sc8280xp_snd_hw_params,
147 .hw_free = sc8280xp_snd_hw_free,
148 .prepare = sc8280xp_snd_prepare,
149 };
150
sc8280xp_add_be_ops(struct snd_soc_card * card)151 static void sc8280xp_add_be_ops(struct snd_soc_card *card)
152 {
153 struct snd_soc_dai_link *link;
154 int i;
155
156 for_each_card_prelinks(card, i, link) {
157 if (link->no_pcm == 1) {
158 link->init = sc8280xp_snd_init;
159 link->be_hw_params_fixup = sc8280xp_be_hw_params_fixup;
160 link->ops = &sc8280xp_be_ops;
161 }
162 }
163 }
164
sc8280xp_platform_probe(struct platform_device * pdev)165 static int sc8280xp_platform_probe(struct platform_device *pdev)
166 {
167 struct snd_soc_card *card;
168 struct sc8280xp_snd_data *data;
169 struct device *dev = &pdev->dev;
170 int ret;
171
172 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
173 if (!card)
174 return -ENOMEM;
175 card->owner = THIS_MODULE;
176 /* Allocate the private data */
177 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
178 if (!data)
179 return -ENOMEM;
180
181 card->dev = dev;
182 dev_set_drvdata(dev, card);
183 snd_soc_card_set_drvdata(card, data);
184 ret = qcom_snd_parse_of(card);
185 if (ret)
186 return ret;
187
188 card->driver_name = of_device_get_match_data(dev);
189 sc8280xp_add_be_ops(card);
190 return devm_snd_soc_register_card(dev, card);
191 }
192
193 static const struct of_device_id snd_sc8280xp_dt_match[] = {
194 {.compatible = "qcom,qcm6490-idp-sndcard", "qcm6490"},
195 {.compatible = "qcom,qcs615-sndcard", "qcs615"},
196 {.compatible = "qcom,qcs6490-rb3gen2-sndcard", "qcs6490"},
197 {.compatible = "qcom,qcs8275-sndcard", "qcs8300"},
198 {.compatible = "qcom,qcs9075-sndcard", "sa8775p"},
199 {.compatible = "qcom,qcs9100-sndcard", "sa8775p"},
200 {.compatible = "qcom,sc8280xp-sndcard", "sc8280xp"},
201 {.compatible = "qcom,sm8450-sndcard", "sm8450"},
202 {.compatible = "qcom,sm8550-sndcard", "sm8550"},
203 {.compatible = "qcom,sm8650-sndcard", "sm8650"},
204 {.compatible = "qcom,sm8750-sndcard", "sm8750"},
205 {}
206 };
207
208 MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
209
210 static struct platform_driver snd_sc8280xp_driver = {
211 .probe = sc8280xp_platform_probe,
212 .driver = {
213 .name = "snd-sc8280xp",
214 .of_match_table = snd_sc8280xp_dt_match,
215 },
216 };
217 module_platform_driver(snd_sc8280xp_driver);
218 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
219 MODULE_DESCRIPTION("SC8280XP ASoC Machine Driver");
220 MODULE_LICENSE("GPL");
221