Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/qcom/sc7280.c
51433 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
4
//
5
// ALSA SoC Machine driver for sc7280
6
7
#include <dt-bindings/sound/qcom,lpass.h>
8
#include <dt-bindings/sound/qcom,q6afe.h>
9
#include <linux/input.h>
10
#include <linux/mod_devicetable.h>
11
#include <linux/module.h>
12
#include <linux/platform_device.h>
13
#include <sound/core.h>
14
#include <sound/jack.h>
15
#include <sound/pcm.h>
16
#include <sound/soc.h>
17
#include <sound/rt5682s.h>
18
#include <linux/soundwire/sdw.h>
19
#include <sound/pcm_params.h>
20
21
#include "../codecs/rt5682.h"
22
#include "../codecs/rt5682s.h"
23
#include "common.h"
24
#include "lpass.h"
25
#include "qdsp6/q6afe.h"
26
#include "sdw.h"
27
28
#define DEFAULT_MCLK_RATE 19200000
29
#define RT5682_PLL_FREQ (48000 * 512)
30
#define MI2S_BCLK_RATE 1536000
31
32
struct sc7280_snd_data {
33
struct snd_soc_card card;
34
u32 pri_mi2s_clk_count;
35
struct snd_soc_jack hs_jack;
36
struct snd_soc_jack hdmi_jack;
37
bool jack_setup;
38
bool stream_prepared[LPASS_MAX_PORTS];
39
};
40
41
static void sc7280_jack_free(struct snd_jack *jack)
42
{
43
struct snd_soc_component *component = jack->private_data;
44
45
snd_soc_component_set_jack(component, NULL, NULL);
46
}
47
48
static struct snd_soc_jack_pin sc7280_jack_pins[] = {
49
{
50
.pin = "Headphone Jack",
51
.mask = SND_JACK_HEADPHONE,
52
},
53
{
54
.pin = "Headset Mic",
55
.mask = SND_JACK_MICROPHONE,
56
},
57
};
58
59
static int sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
60
{
61
struct snd_soc_card *card = rtd->card;
62
struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
63
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
64
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
65
struct snd_soc_component *component = codec_dai->component;
66
struct snd_jack *jack;
67
int rval, i;
68
69
if (!pdata->jack_setup) {
70
rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
71
SND_JACK_HEADSET | SND_JACK_LINEOUT |
72
SND_JACK_MECHANICAL |
73
SND_JACK_BTN_0 | SND_JACK_BTN_1 |
74
SND_JACK_BTN_2 | SND_JACK_BTN_3 |
75
SND_JACK_BTN_4 | SND_JACK_BTN_5,
76
&pdata->hs_jack,
77
sc7280_jack_pins,
78
ARRAY_SIZE(sc7280_jack_pins));
79
80
if (rval < 0) {
81
dev_err(card->dev, "Unable to add Headset Jack\n");
82
return rval;
83
}
84
85
jack = pdata->hs_jack.jack;
86
87
snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
88
snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
89
snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
90
snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
91
92
jack->private_data = component;
93
jack->private_free = sc7280_jack_free;
94
pdata->jack_setup = true;
95
}
96
switch (cpu_dai->id) {
97
case MI2S_PRIMARY:
98
case LPASS_CDC_DMA_RX0:
99
case LPASS_CDC_DMA_TX3:
100
case TX_CODEC_DMA_TX_3:
101
for_each_rtd_codec_dais(rtd, i, codec_dai) {
102
rval = snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
103
if (rval != 0 && rval != -ENOTSUPP) {
104
dev_err(card->dev, "Failed to set jack: %d\n", rval);
105
return rval;
106
}
107
}
108
break;
109
default:
110
break;
111
}
112
113
return 0;
114
}
115
116
static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
117
{
118
struct snd_soc_card *card = rtd->card;
119
struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
120
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
121
struct snd_soc_component *component = codec_dai->component;
122
struct snd_jack *jack;
123
int rval;
124
125
rval = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
126
&pdata->hdmi_jack);
127
128
if (rval < 0) {
129
dev_err(card->dev, "Unable to add HDMI Jack\n");
130
return rval;
131
}
132
133
jack = pdata->hdmi_jack.jack;
134
jack->private_data = component;
135
jack->private_free = sc7280_jack_free;
136
137
return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
138
}
139
140
static int sc7280_rt5682_init(struct snd_soc_pcm_runtime *rtd)
141
{
142
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
143
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
144
struct snd_soc_card *card = rtd->card;
145
struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
146
int ret;
147
148
if (++data->pri_mi2s_clk_count == 1) {
149
snd_soc_dai_set_sysclk(cpu_dai,
150
LPASS_MCLK0,
151
DEFAULT_MCLK_RATE,
152
SNDRV_PCM_STREAM_PLAYBACK);
153
}
154
snd_soc_dai_set_fmt(codec_dai,
155
SND_SOC_DAIFMT_CBC_CFC |
156
SND_SOC_DAIFMT_NB_NF |
157
SND_SOC_DAIFMT_I2S);
158
159
ret = snd_soc_dai_set_pll(codec_dai, RT5682S_PLL2, RT5682S_PLL_S_MCLK,
160
DEFAULT_MCLK_RATE, RT5682_PLL_FREQ);
161
if (ret) {
162
dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
163
return ret;
164
}
165
166
ret = snd_soc_dai_set_sysclk(codec_dai, RT5682S_SCLK_S_PLL2,
167
RT5682_PLL_FREQ,
168
SND_SOC_CLOCK_IN);
169
170
if (ret) {
171
dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
172
ret);
173
return ret;
174
}
175
176
return 0;
177
}
178
179
static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
180
{
181
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
182
183
switch (cpu_dai->id) {
184
case MI2S_PRIMARY:
185
case LPASS_CDC_DMA_TX3:
186
case TX_CODEC_DMA_TX_3:
187
return sc7280_headset_init(rtd);
188
case LPASS_CDC_DMA_RX0:
189
case LPASS_CDC_DMA_VA_TX0:
190
case MI2S_SECONDARY:
191
case RX_CODEC_DMA_RX_0:
192
case SECONDARY_MI2S_RX:
193
case VA_CODEC_DMA_TX_0:
194
return 0;
195
case LPASS_DP_RX:
196
return sc7280_hdmi_init(rtd);
197
default:
198
dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
199
}
200
201
return -EINVAL;
202
}
203
204
static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
205
struct snd_pcm_hw_params *params)
206
{
207
struct snd_pcm_runtime *runtime = substream->runtime;
208
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
209
210
if (!rtd->dai_link->no_pcm) {
211
snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
212
snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 48000, 48000);
213
}
214
215
return 0;
216
}
217
218
static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
219
{
220
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
221
const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
222
struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
223
224
return qcom_snd_sdw_prepare(substream, &data->stream_prepared[cpu_dai->id]);
225
}
226
227
static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
228
{
229
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
230
const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
231
232
switch (cpu_dai->id) {
233
case LPASS_CDC_DMA_RX0:
234
case LPASS_CDC_DMA_TX3:
235
case RX_CODEC_DMA_RX_0:
236
case TX_CODEC_DMA_TX_3:
237
case VA_CODEC_DMA_TX_0:
238
return sc7280_snd_swr_prepare(substream);
239
default:
240
break;
241
}
242
243
return 0;
244
}
245
246
static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
247
{
248
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
249
struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
250
const struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
251
252
return qcom_snd_sdw_hw_free(substream, &data->stream_prepared[cpu_dai->id]);
253
}
254
255
static void sc7280_snd_shutdown(struct snd_pcm_substream *substream)
256
{
257
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
258
struct snd_soc_card *card = rtd->card;
259
struct sc7280_snd_data *data = snd_soc_card_get_drvdata(card);
260
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
261
262
switch (cpu_dai->id) {
263
case MI2S_PRIMARY:
264
if (--data->pri_mi2s_clk_count == 0) {
265
snd_soc_dai_set_sysclk(cpu_dai,
266
LPASS_MCLK0,
267
0,
268
SNDRV_PCM_STREAM_PLAYBACK);
269
}
270
break;
271
case SECONDARY_MI2S_RX:
272
snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
273
0, SNDRV_PCM_STREAM_PLAYBACK);
274
break;
275
default:
276
break;
277
}
278
279
qcom_snd_sdw_shutdown(substream);
280
}
281
282
static int sc7280_snd_startup(struct snd_pcm_substream *substream)
283
{
284
unsigned int fmt = SND_SOC_DAIFMT_CBC_CFC;
285
unsigned int codec_dai_fmt = SND_SOC_DAIFMT_CBC_CFC;
286
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
287
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
288
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
289
int ret = 0;
290
291
switch (cpu_dai->id) {
292
case MI2S_PRIMARY:
293
ret = sc7280_rt5682_init(rtd);
294
if (ret)
295
return ret;
296
break;
297
case SECONDARY_MI2S_RX:
298
codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
299
300
snd_soc_dai_set_sysclk(cpu_dai, Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
301
MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
302
303
snd_soc_dai_set_fmt(cpu_dai, fmt);
304
snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
305
break;
306
default:
307
break;
308
}
309
310
return qcom_snd_sdw_startup(substream);
311
}
312
313
static const struct snd_soc_ops sc7280_ops = {
314
.startup = sc7280_snd_startup,
315
.hw_params = sc7280_snd_hw_params,
316
.hw_free = sc7280_snd_hw_free,
317
.prepare = sc7280_snd_prepare,
318
.shutdown = sc7280_snd_shutdown,
319
};
320
321
static const struct snd_soc_dapm_widget sc7280_snd_widgets[] = {
322
SND_SOC_DAPM_HP("Headphone Jack", NULL),
323
SND_SOC_DAPM_MIC("Headset Mic", NULL),
324
};
325
326
static const struct snd_kcontrol_new sc7280_snd_controls[] = {
327
SOC_DAPM_PIN_SWITCH("Headphone Jack"),
328
SOC_DAPM_PIN_SWITCH("Headset Mic"),
329
};
330
331
static int sc7280_snd_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
332
struct snd_pcm_hw_params *params)
333
{
334
struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
335
struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
336
struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
337
338
rate->min = rate->max = 48000;
339
channels->min = channels->max = 2;
340
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
341
342
return 0;
343
}
344
345
static int sc7280_snd_platform_probe(struct platform_device *pdev)
346
{
347
struct snd_soc_card *card;
348
struct sc7280_snd_data *data;
349
struct device *dev = &pdev->dev;
350
struct snd_soc_dai_link *link;
351
int ret, i;
352
353
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
354
if (!data)
355
return -ENOMEM;
356
357
card = &data->card;
358
snd_soc_card_set_drvdata(card, data);
359
360
card->owner = THIS_MODULE;
361
card->driver_name = "SC7280";
362
card->dev = dev;
363
364
card->dapm_widgets = sc7280_snd_widgets;
365
card->num_dapm_widgets = ARRAY_SIZE(sc7280_snd_widgets);
366
card->controls = sc7280_snd_controls;
367
card->num_controls = ARRAY_SIZE(sc7280_snd_controls);
368
369
ret = qcom_snd_parse_of(card);
370
if (ret)
371
return ret;
372
373
for_each_card_prelinks(card, i, link) {
374
link->init = sc7280_init;
375
link->ops = &sc7280_ops;
376
if (link->no_pcm == 1)
377
link->be_hw_params_fixup = sc7280_snd_be_hw_params_fixup;
378
}
379
380
return devm_snd_soc_register_card(dev, card);
381
}
382
383
static const struct of_device_id sc7280_snd_device_id[] = {
384
{ .compatible = "google,sc7280-herobrine" },
385
{}
386
};
387
MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
388
389
static struct platform_driver sc7280_snd_driver = {
390
.probe = sc7280_snd_platform_probe,
391
.driver = {
392
.name = "msm-snd-sc7280",
393
.of_match_table = sc7280_snd_device_id,
394
.pm = &snd_soc_pm_ops,
395
},
396
};
397
module_platform_driver(sc7280_snd_driver);
398
399
MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
400
MODULE_LICENSE("GPL");
401
402