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