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