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