xref: /linux/sound/soc/qcom/x1e80100.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2023, Linaro Limited
3 
4 #include <dt-bindings/sound/qcom,q6afe.h>
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
7 #include <linux/soundwire/sdw.h>
8 #include <sound/pcm.h>
9 #include <sound/jack.h>
10 #include <sound/soc.h>
11 #include <sound/soc-dapm.h>
12 
13 #include "common.h"
14 #include "qdsp6/q6afe.h"
15 #include "qdsp6/q6dsp-common.h"
16 #include "sdw.h"
17 
18 struct x1e80100_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 
27 static int x1e80100_snd_init(struct snd_soc_pcm_runtime *rtd)
28 {
29 	struct x1e80100_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_jack *dp_jack = NULL;
32 	int dp_pcm_id = 0;
33 
34 	switch (cpu_dai->id) {
35 	case DISPLAY_PORT_RX_0:
36 		dp_pcm_id = 0;
37 		dp_jack = &data->dp_jack[dp_pcm_id];
38 		break;
39 	case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:
40 		dp_pcm_id = cpu_dai->id - DISPLAY_PORT_RX_1 + 1;
41 		dp_jack = &data->dp_jack[dp_pcm_id];
42 		break;
43 	default:
44 		break;
45 	}
46 
47 	if (dp_jack)
48 		return qcom_snd_dp_jack_setup(rtd, dp_jack, dp_pcm_id);
49 
50 	return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
51 }
52 
53 static void x1e80100_snd_shutdown(struct snd_pcm_substream *substream)
54 {
55 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
56 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
57 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
58 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
59 
60 	data->sruntime[cpu_dai->id] = NULL;
61 	sdw_release_stream(sruntime);
62 }
63 
64 static int x1e80100_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
65 				     struct snd_pcm_hw_params *params)
66 {
67 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
68 	struct snd_interval *rate = hw_param_interval(params,
69 						      SNDRV_PCM_HW_PARAM_RATE);
70 	struct snd_interval *channels = hw_param_interval(params,
71 							  SNDRV_PCM_HW_PARAM_CHANNELS);
72 
73 	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:
79 		channels->min = 1;
80 		break;
81 	default:
82 		break;
83 	}
84 
85 	return 0;
86 }
87 
88 static int x1e80100_snd_hw_params(struct snd_pcm_substream *substream,
89 				struct snd_pcm_hw_params *params)
90 {
91 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
92 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
93 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
94 
95 	return qcom_snd_sdw_hw_params(substream, params, &data->sruntime[cpu_dai->id]);
96 }
97 
98 static int x1e80100_snd_prepare(struct snd_pcm_substream *substream)
99 {
100 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
101 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
102 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
103 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
104 	const unsigned int rx_slot[4] = { PCM_CHANNEL_FL,
105 					  PCM_CHANNEL_LB,
106 					  PCM_CHANNEL_FR,
107 					  PCM_CHANNEL_RB };
108 	int ret;
109 
110 	switch (cpu_dai->id) {
111 	case WSA_CODEC_DMA_RX_0:
112 	case WSA_CODEC_DMA_RX_1:
113 		ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
114 						  ARRAY_SIZE(rx_slot), rx_slot);
115 		if (ret)
116 			return ret;
117 		break;
118 	default:
119 		break;
120 	}
121 
122 	return qcom_snd_sdw_prepare(substream, sruntime,
123 				    &data->stream_prepared[cpu_dai->id]);
124 }
125 
126 static int x1e80100_snd_hw_free(struct snd_pcm_substream *substream)
127 {
128 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
129 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
130 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
131 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
132 
133 	return qcom_snd_sdw_hw_free(substream, sruntime,
134 				    &data->stream_prepared[cpu_dai->id]);
135 }
136 
137 static const struct snd_soc_ops x1e80100_be_ops = {
138 	.startup = qcom_snd_sdw_startup,
139 	.shutdown = x1e80100_snd_shutdown,
140 	.hw_params = x1e80100_snd_hw_params,
141 	.hw_free = x1e80100_snd_hw_free,
142 	.prepare = x1e80100_snd_prepare,
143 };
144 
145 static void x1e80100_add_be_ops(struct snd_soc_card *card)
146 {
147 	struct snd_soc_dai_link *link;
148 	int i;
149 
150 	for_each_card_prelinks(card, i, link) {
151 		if (link->no_pcm == 1) {
152 			link->init = x1e80100_snd_init;
153 			link->be_hw_params_fixup = x1e80100_be_hw_params_fixup;
154 			link->ops = &x1e80100_be_ops;
155 		}
156 	}
157 }
158 
159 static int x1e80100_platform_probe(struct platform_device *pdev)
160 {
161 	struct snd_soc_card *card;
162 	struct x1e80100_snd_data *data;
163 	struct device *dev = &pdev->dev;
164 	int ret;
165 
166 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
167 	if (!card)
168 		return -ENOMEM;
169 	/* Allocate the private data */
170 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
171 	if (!data)
172 		return -ENOMEM;
173 
174 	card->owner = THIS_MODULE;
175 	card->dev = dev;
176 	dev_set_drvdata(dev, card);
177 	snd_soc_card_set_drvdata(card, data);
178 
179 	ret = qcom_snd_parse_of(card);
180 	if (ret)
181 		return ret;
182 
183 	card->driver_name = "x1e80100";
184 	x1e80100_add_be_ops(card);
185 
186 	return devm_snd_soc_register_card(dev, card);
187 }
188 
189 static const struct of_device_id snd_x1e80100_dt_match[] = {
190 	{ .compatible = "qcom,x1e80100-sndcard", },
191 	{}
192 };
193 MODULE_DEVICE_TABLE(of, snd_x1e80100_dt_match);
194 
195 static struct platform_driver snd_x1e80100_driver = {
196 	.probe  = x1e80100_platform_probe,
197 	.driver = {
198 		.name = "snd-x1e80100",
199 		.of_match_table = snd_x1e80100_dt_match,
200 	},
201 };
202 module_platform_driver(snd_x1e80100_driver);
203 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
204 MODULE_AUTHOR("Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>");
205 MODULE_DESCRIPTION("Qualcomm X1E80100 ASoC Machine Driver");
206 MODULE_LICENSE("GPL");
207