ASoC: qcom: sc8280xp: Limit speaker digital volumes
[platform/kernel/linux-starfive.git] / sound / soc / qcom / sc8280xp.c
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
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;
24         bool jack_setup;
25 };
26
27 static 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 0dB on Digital Volume for Speakers,
38                  * this can prevent damage of speakers to some extent without
39                  * active speaker protection
40                  */
41                 snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 84);
42                 snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 84);
43                 break;
44         default:
45                 break;
46         }
47
48         return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
49 }
50
51 static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
52                                      struct snd_pcm_hw_params *params)
53 {
54         struct snd_soc_dai *cpu_dai = asoc_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);
59
60         rate->min = rate->max = 48000;
61         channels->min = 2;
62         channels->max = 2;
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:
68                 channels->min = 1;
69                 break;
70         default:
71                 break;
72         }
73
74
75         return 0;
76 }
77
78 static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
79                                 struct snd_pcm_hw_params *params)
80 {
81         struct snd_soc_pcm_runtime *rtd = substream->private_data;
82         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
83         struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
84
85         return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
86 }
87
88 static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
89 {
90         struct snd_soc_pcm_runtime *rtd = substream->private_data;
91         struct snd_soc_dai *cpu_dai = asoc_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];
94
95         return qcom_snd_sdw_prepare(substream, sruntime,
96                                     &data->stream_prepared[cpu_dai->id]);
97 }
98
99 static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
100 {
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 = asoc_rtd_to_cpu(rtd, 0);
104         struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
105
106         return qcom_snd_sdw_hw_free(substream, sruntime,
107                                     &data->stream_prepared[cpu_dai->id]);
108 }
109
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,
114 };
115
116 static void sc8280xp_add_be_ops(struct snd_soc_card *card)
117 {
118         struct snd_soc_dai_link *link;
119         int i;
120
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;
126                 }
127         }
128 }
129
130 static int sc8280xp_platform_probe(struct platform_device *pdev)
131 {
132         struct snd_soc_card *card;
133         struct sc8280xp_snd_data *data;
134         struct device *dev = &pdev->dev;
135         int ret;
136
137         card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
138         if (!card)
139                 return -ENOMEM;
140         card->owner = THIS_MODULE;
141         /* Allocate the private data */
142         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
143         if (!data)
144                 return -ENOMEM;
145
146         card->dev = dev;
147         dev_set_drvdata(dev, card);
148         snd_soc_card_set_drvdata(card, data);
149         ret = qcom_snd_parse_of(card);
150         if (ret)
151                 return ret;
152
153         card->driver_name = DRIVER_NAME;
154         sc8280xp_add_be_ops(card);
155         return devm_snd_soc_register_card(dev, card);
156 }
157
158 static const struct of_device_id snd_sc8280xp_dt_match[] = {
159         {.compatible = "qcom,sc8280xp-sndcard",},
160         {}
161 };
162
163 MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
164
165 static struct platform_driver snd_sc8280xp_driver = {
166         .probe  = sc8280xp_platform_probe,
167         .driver = {
168                 .name = "snd-sc8280xp",
169                 .of_match_table = snd_sc8280xp_dt_match,
170         },
171 };
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");