Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/intel/avs/boards/rt5640.c
26607 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Copyright(c) 2022-2025 Intel Corporation
4
//
5
// Authors: Cezary Rojewski <[email protected]>
6
// Amadeusz Slawinski <[email protected]>
7
//
8
9
#include <linux/module.h>
10
#include <sound/jack.h>
11
#include <sound/pcm.h>
12
#include <sound/pcm_params.h>
13
#include <sound/soc.h>
14
#include <sound/soc-acpi.h>
15
#include "../../../codecs/rt5640.h"
16
#include "../utils.h"
17
18
#define AVS_RT5640_MCLK_HZ 19200000
19
#define RT5640_CODEC_DAI "rt5640-aif1"
20
21
static const struct snd_soc_dapm_widget card_widgets[] = {
22
SND_SOC_DAPM_HP("Headphone Jack", NULL),
23
SND_SOC_DAPM_MIC("Mic Jack", NULL),
24
SND_SOC_DAPM_SPK("Speaker", NULL),
25
};
26
27
static const struct snd_soc_dapm_route card_routes[] = {
28
{ "Headphone Jack", NULL, "HPOR" },
29
{ "Headphone Jack", NULL, "HPOL" },
30
{ "IN2P", NULL, "Mic Jack" },
31
{ "IN2P", NULL, "MICBIAS1" },
32
{ "Speaker", NULL, "SPOLP" },
33
{ "Speaker", NULL, "SPOLN" },
34
{ "Speaker", NULL, "SPORP" },
35
{ "Speaker", NULL, "SPORN" },
36
};
37
38
static const struct snd_soc_jack_pin card_headset_pins[] = {
39
{
40
.pin = "Headphone Jack",
41
.mask = SND_JACK_HEADPHONE,
42
},
43
{
44
.pin = "Mic Jack",
45
.mask = SND_JACK_MICROPHONE,
46
},
47
};
48
49
static int avs_rt5640_codec_init(struct snd_soc_pcm_runtime *runtime)
50
{
51
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
52
struct snd_soc_card *card = runtime->card;
53
struct snd_soc_jack_pin *pins;
54
struct snd_soc_jack *jack;
55
int num_pins, ret;
56
57
jack = snd_soc_card_get_drvdata(card);
58
num_pins = ARRAY_SIZE(card_headset_pins);
59
60
pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
61
if (!pins)
62
return -ENOMEM;
63
64
ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET, jack, pins,
65
num_pins);
66
if (ret)
67
return ret;
68
69
snd_soc_component_set_jack(codec_dai->component, jack, NULL);
70
card->dapm.idle_bias_off = true;
71
72
return 0;
73
}
74
75
static void avs_rt5640_codec_exit(struct snd_soc_pcm_runtime *runtime)
76
{
77
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
78
79
snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
80
}
81
82
static int avs_rt5640_be_fixup(struct snd_soc_pcm_runtime *runtime,
83
struct snd_pcm_hw_params *params)
84
{
85
struct snd_mask *fmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
86
87
/* Format 24/32 is MSB-aligned for HDAudio and LSB-aligned for I2S. */
88
if (params_format(params) == SNDRV_PCM_FORMAT_S32_LE)
89
snd_mask_set_format(fmask, SNDRV_PCM_FORMAT_S24_LE);
90
91
return 0;
92
}
93
94
static int avs_rt5640_hw_params(struct snd_pcm_substream *substream,
95
struct snd_pcm_hw_params *params)
96
{
97
struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
98
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
99
int ret;
100
101
ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_MCLK, AVS_RT5640_MCLK_HZ,
102
params_rate(params) * 512);
103
if (ret < 0) {
104
dev_err(runtime->dev, "Set codec PLL failed: %d\n", ret);
105
return ret;
106
}
107
108
ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1, params_rate(params) * 512,
109
SND_SOC_CLOCK_IN);
110
if (ret < 0) {
111
dev_err(runtime->dev, "Set codec SCLK failed: %d\n", ret);
112
return ret;
113
}
114
115
ret = rt5640_sel_asrc_clk_src(codec_dai->component,
116
RT5640_DA_STEREO_FILTER | RT5640_AD_STEREO_FILTER |
117
RT5640_DA_MONO_L_FILTER | RT5640_DA_MONO_R_FILTER |
118
RT5640_AD_MONO_L_FILTER | RT5640_AD_MONO_R_FILTER,
119
RT5640_CLK_SEL_ASRC);
120
if (ret)
121
dev_err(runtime->dev, "Set codec ASRC failed: %d\n", ret);
122
123
return ret;
124
}
125
126
static const struct snd_soc_ops avs_rt5640_ops = {
127
.hw_params = avs_rt5640_hw_params,
128
};
129
130
static int avs_create_dai_link(struct device *dev, int ssp_port, int tdm_slot,
131
struct snd_soc_acpi_mach *mach,
132
struct snd_soc_dai_link **dai_link)
133
{
134
struct snd_soc_dai_link_component *platform;
135
struct snd_soc_dai_link *dl;
136
u32 uid = 0;
137
int ret;
138
139
if (mach->uid) {
140
ret = kstrtou32(mach->uid, 0, &uid);
141
if (ret)
142
return ret;
143
uid--; /* 0-based indexing. */
144
}
145
146
dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
147
platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
148
if (!dl || !platform)
149
return -ENOMEM;
150
151
dl->name = devm_kasprintf(dev, GFP_KERNEL,
152
AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
153
dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
154
dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
155
if (!dl->name || !dl->cpus || !dl->codecs)
156
return -ENOMEM;
157
158
dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
159
AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
160
dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5640:0%d", uid);
161
dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT5640_CODEC_DAI);
162
if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
163
return -ENOMEM;
164
165
platform->name = dev_name(dev);
166
dl->num_cpus = 1;
167
dl->num_codecs = 1;
168
dl->platforms = platform;
169
dl->num_platforms = 1;
170
dl->id = 0;
171
dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
172
dl->init = avs_rt5640_codec_init;
173
dl->exit = avs_rt5640_codec_exit;
174
dl->be_hw_params_fixup = avs_rt5640_be_fixup;
175
dl->ops = &avs_rt5640_ops;
176
dl->nonatomic = 1;
177
dl->no_pcm = 1;
178
179
*dai_link = dl;
180
181
return 0;
182
}
183
184
static int avs_card_suspend_pre(struct snd_soc_card *card)
185
{
186
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5640_CODEC_DAI);
187
188
return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
189
}
190
191
static int avs_card_resume_post(struct snd_soc_card *card)
192
{
193
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5640_CODEC_DAI);
194
struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
195
196
return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
197
}
198
199
static int avs_rt5640_probe(struct platform_device *pdev)
200
{
201
struct snd_soc_dai_link *dai_link;
202
struct device *dev = &pdev->dev;
203
struct snd_soc_acpi_mach *mach;
204
struct snd_soc_card *card;
205
struct snd_soc_jack *jack;
206
int ssp_port, tdm_slot, ret;
207
208
mach = dev_get_platdata(dev);
209
210
ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
211
if (ret)
212
return ret;
213
214
ret = avs_create_dai_link(dev, ssp_port, tdm_slot, mach, &dai_link);
215
if (ret) {
216
dev_err(dev, "Failed to create dai link: %d", ret);
217
return ret;
218
}
219
220
jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
221
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
222
if (!jack || !card)
223
return -ENOMEM;
224
225
if (mach->uid) {
226
card->name = devm_kasprintf(dev, GFP_KERNEL, "AVS I2S ALC5640.%s", mach->uid);
227
if (!card->name)
228
return -ENOMEM;
229
} else {
230
card->name = "AVS I2S ALC5640";
231
}
232
card->driver_name = "avs_rt5640";
233
card->long_name = card->name;
234
card->dev = dev;
235
card->owner = THIS_MODULE;
236
card->suspend_pre = avs_card_suspend_pre;
237
card->resume_post = avs_card_resume_post;
238
card->dai_link = dai_link;
239
card->num_links = 1;
240
card->dapm_widgets = card_widgets;
241
card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
242
card->dapm_routes = card_routes;
243
card->num_dapm_routes = ARRAY_SIZE(card_routes);
244
card->fully_routed = true;
245
snd_soc_card_set_drvdata(card, jack);
246
247
return devm_snd_soc_register_deferrable_card(dev, card);
248
}
249
250
static const struct platform_device_id avs_rt5640_driver_ids[] = {
251
{
252
.name = "avs_rt5640",
253
},
254
{},
255
};
256
MODULE_DEVICE_TABLE(platform, avs_rt5640_driver_ids);
257
258
static struct platform_driver avs_rt5640_driver = {
259
.probe = avs_rt5640_probe,
260
.driver = {
261
.name = "avs_rt5640",
262
.pm = &snd_soc_pm_ops,
263
},
264
.id_table = avs_rt5640_driver_ids,
265
};
266
267
module_platform_driver(avs_rt5640_driver);
268
269
MODULE_DESCRIPTION("Intel rt5640 machine driver");
270
MODULE_LICENSE("GPL");
271
272