Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/intel/avs/boards/max98373.c
26607 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Copyright(c) 2022 Intel Corporation
4
//
5
// Authors: Cezary Rojewski <[email protected]>
6
// Amadeusz Slawinski <[email protected]>
7
//
8
9
#include <linux/module.h>
10
#include <linux/platform_device.h>
11
#include <sound/pcm_params.h>
12
#include <sound/soc.h>
13
#include <sound/soc-acpi.h>
14
#include <sound/soc-dapm.h>
15
#include "../utils.h"
16
17
#define MAX98373_DEV0_NAME "i2c-MX98373:00"
18
#define MAX98373_DEV1_NAME "i2c-MX98373:01"
19
#define MAX98373_CODEC_NAME "max98373-aif1"
20
21
static struct snd_soc_codec_conf card_codec_conf[] = {
22
{
23
.dlc = COMP_CODEC_CONF(MAX98373_DEV0_NAME),
24
.name_prefix = "Right",
25
},
26
{
27
.dlc = COMP_CODEC_CONF(MAX98373_DEV1_NAME),
28
.name_prefix = "Left",
29
},
30
};
31
32
static const struct snd_kcontrol_new card_controls[] = {
33
SOC_DAPM_PIN_SWITCH("Left Spk"),
34
SOC_DAPM_PIN_SWITCH("Right Spk"),
35
};
36
37
static const struct snd_soc_dapm_widget card_widgets[] = {
38
SND_SOC_DAPM_SPK("Left Spk", NULL),
39
SND_SOC_DAPM_SPK("Right Spk", NULL),
40
};
41
42
static const struct snd_soc_dapm_route card_base_routes[] = {
43
{ "Left Spk", NULL, "Left BE_OUT" },
44
{ "Right Spk", NULL, "Right BE_OUT" },
45
};
46
47
static int
48
avs_max98373_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
49
{
50
struct snd_interval *rate, *channels;
51
struct snd_mask *fmt;
52
53
rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
54
channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
55
fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
56
57
/* The ADSP will convert the FE rate to 48k, stereo */
58
rate->min = rate->max = 48000;
59
channels->min = channels->max = 2;
60
61
/* set SSP0 to 16 bit */
62
snd_mask_none(fmt);
63
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
64
return 0;
65
}
66
67
static int avs_max98373_hw_params(struct snd_pcm_substream *substream,
68
struct snd_pcm_hw_params *params)
69
{
70
struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
71
struct snd_soc_dai *codec_dai;
72
int ret, i;
73
74
for_each_rtd_codec_dais(runtime, i, codec_dai) {
75
if (!strcmp(codec_dai->component->name, MAX98373_DEV0_NAME)) {
76
ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
77
if (ret < 0) {
78
dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
79
return ret;
80
}
81
}
82
if (!strcmp(codec_dai->component->name, MAX98373_DEV1_NAME)) {
83
ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
84
if (ret < 0) {
85
dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
86
return ret;
87
}
88
}
89
}
90
91
return 0;
92
}
93
94
static const struct snd_soc_ops avs_max98373_ops = {
95
.hw_params = avs_max98373_hw_params,
96
};
97
98
static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
99
int tdm_slot, struct snd_soc_dai_link **dai_link)
100
{
101
struct snd_soc_dai_link_component *platform;
102
struct snd_soc_dai_link *dl;
103
104
dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
105
platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
106
if (!dl || !platform)
107
return -ENOMEM;
108
109
platform->name = platform_name;
110
111
dl->name = devm_kasprintf(dev, GFP_KERNEL,
112
AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
113
dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
114
dl->codecs = devm_kcalloc(dev, 2, sizeof(*dl->codecs), GFP_KERNEL);
115
if (!dl->name || !dl->cpus || !dl->codecs)
116
return -ENOMEM;
117
118
dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
119
AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
120
dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV0_NAME);
121
dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
122
dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV1_NAME);
123
dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
124
if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
125
!dl->codecs[1].name || !dl->codecs[1].dai_name)
126
return -ENOMEM;
127
128
dl->num_cpus = 1;
129
dl->num_codecs = 2;
130
dl->platforms = platform;
131
dl->num_platforms = 1;
132
dl->id = 0;
133
dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
134
dl->be_hw_params_fixup = avs_max98373_be_fixup;
135
dl->nonatomic = 1;
136
dl->no_pcm = 1;
137
dl->ignore_pmdown_time = 1;
138
dl->ops = &avs_max98373_ops;
139
140
*dai_link = dl;
141
142
return 0;
143
}
144
145
static int avs_max98373_probe(struct platform_device *pdev)
146
{
147
struct snd_soc_dai_link *dai_link;
148
struct snd_soc_acpi_mach *mach;
149
struct avs_mach_pdata *pdata;
150
struct snd_soc_card *card;
151
struct device *dev = &pdev->dev;
152
const char *pname;
153
int ssp_port, tdm_slot, ret;
154
155
mach = dev_get_platdata(dev);
156
pname = mach->mach_params.platform;
157
pdata = mach->pdata;
158
159
ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
160
if (ret)
161
return ret;
162
163
ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
164
if (ret) {
165
dev_err(dev, "Failed to create dai link: %d", ret);
166
return ret;
167
}
168
169
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
170
if (!card)
171
return -ENOMEM;
172
173
if (pdata->obsolete_card_names) {
174
card->name = "avs_max98373";
175
} else {
176
card->driver_name = "avs_max98373";
177
card->long_name = card->name = "AVS I2S MAX98373";
178
}
179
card->dev = dev;
180
card->owner = THIS_MODULE;
181
card->dai_link = dai_link;
182
card->num_links = 1;
183
card->codec_conf = card_codec_conf;
184
card->num_configs = ARRAY_SIZE(card_codec_conf);
185
card->controls = card_controls;
186
card->num_controls = ARRAY_SIZE(card_controls);
187
card->dapm_widgets = card_widgets;
188
card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
189
card->dapm_routes = card_base_routes;
190
card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
191
card->fully_routed = true;
192
193
ret = snd_soc_fixup_dai_links_platform_name(card, pname);
194
if (ret)
195
return ret;
196
197
return devm_snd_soc_register_deferrable_card(dev, card);
198
}
199
200
static const struct platform_device_id avs_max98373_driver_ids[] = {
201
{
202
.name = "avs_max98373",
203
},
204
{},
205
};
206
MODULE_DEVICE_TABLE(platform, avs_max98373_driver_ids);
207
208
static struct platform_driver avs_max98373_driver = {
209
.probe = avs_max98373_probe,
210
.driver = {
211
.name = "avs_max98373",
212
.pm = &snd_soc_pm_ops,
213
},
214
.id_table = avs_max98373_driver_ids,
215
};
216
217
module_platform_driver(avs_max98373_driver)
218
219
MODULE_DESCRIPTION("Intel max98373 machine driver");
220
MODULE_LICENSE("GPL");
221
222