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