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 #define DRIVER_NAME "sc8280xp"
19 struct sc8280xp_snd_data {
20 bool stream_prepared[AFE_PORT_MAX];
21 struct snd_soc_card *card;
22 struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
23 struct snd_soc_jack jack;
27 static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
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;
33 switch (cpu_dai->id) {
34 case WSA_CODEC_DMA_RX_0:
35 case WSA_CODEC_DMA_RX_1:
37 * set limit of 0dB on Digital Volume for Speakers,
38 * this can prevent damage of speakers to some extent without
39 * active speaker protection
41 snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 84);
42 snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 84);
48 return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
51 static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
52 struct snd_pcm_hw_params *params)
54 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
55 struct snd_interval *rate = hw_param_interval(params,
56 SNDRV_PCM_HW_PARAM_RATE);
57 struct snd_interval *channels = hw_param_interval(params,
58 SNDRV_PCM_HW_PARAM_CHANNELS);
60 rate->min = rate->max = 48000;
63 switch (cpu_dai->id) {
64 case TX_CODEC_DMA_TX_0:
65 case TX_CODEC_DMA_TX_1:
66 case TX_CODEC_DMA_TX_2:
67 case TX_CODEC_DMA_TX_3:
78 static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params *params)
81 struct snd_soc_pcm_runtime *rtd = substream->private_data;
82 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
83 struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
85 return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
88 static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
90 struct snd_soc_pcm_runtime *rtd = substream->private_data;
91 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
92 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
93 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
95 return qcom_snd_sdw_prepare(substream, sruntime,
96 &data->stream_prepared[cpu_dai->id]);
99 static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
102 struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
103 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
104 struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
106 return qcom_snd_sdw_hw_free(substream, sruntime,
107 &data->stream_prepared[cpu_dai->id]);
110 static const struct snd_soc_ops sc8280xp_be_ops = {
111 .hw_params = sc8280xp_snd_hw_params,
112 .hw_free = sc8280xp_snd_hw_free,
113 .prepare = sc8280xp_snd_prepare,
116 static void sc8280xp_add_be_ops(struct snd_soc_card *card)
118 struct snd_soc_dai_link *link;
121 for_each_card_prelinks(card, i, link) {
122 if (link->no_pcm == 1) {
123 link->init = sc8280xp_snd_init;
124 link->be_hw_params_fixup = sc8280xp_be_hw_params_fixup;
125 link->ops = &sc8280xp_be_ops;
130 static int sc8280xp_platform_probe(struct platform_device *pdev)
132 struct snd_soc_card *card;
133 struct sc8280xp_snd_data *data;
134 struct device *dev = &pdev->dev;
137 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
140 card->owner = THIS_MODULE;
141 /* Allocate the private data */
142 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
147 dev_set_drvdata(dev, card);
148 snd_soc_card_set_drvdata(card, data);
149 ret = qcom_snd_parse_of(card);
153 card->driver_name = DRIVER_NAME;
154 sc8280xp_add_be_ops(card);
155 return devm_snd_soc_register_card(dev, card);
158 static const struct of_device_id snd_sc8280xp_dt_match[] = {
159 {.compatible = "qcom,sc8280xp-sndcard",},
163 MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
165 static struct platform_driver snd_sc8280xp_driver = {
166 .probe = sc8280xp_platform_probe,
168 .name = "snd-sc8280xp",
169 .of_match_table = snd_sc8280xp_dt_match,
172 module_platform_driver(snd_sc8280xp_driver);
173 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
174 MODULE_DESCRIPTION("SC8280XP ASoC Machine Driver");
175 MODULE_LICENSE("GPL v2");