Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/intel/avs/boards/nau8825.c
26607 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Copyright(c) 2021-2022 Intel Corporation
4
//
5
// Authors: Cezary Rojewski <[email protected]>
6
// Amadeusz Slawinski <[email protected]>
7
//
8
9
#include <linux/input.h>
10
#include <linux/module.h>
11
#include <linux/platform_device.h>
12
#include <sound/core.h>
13
#include <sound/jack.h>
14
#include <sound/pcm.h>
15
#include <sound/pcm_params.h>
16
#include <sound/soc.h>
17
#include <sound/soc-acpi.h>
18
#include "../../../codecs/nau8825.h"
19
#include "../utils.h"
20
21
#define SKL_NUVOTON_CODEC_DAI "nau8825-hifi"
22
23
static int
24
avs_nau8825_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event)
25
{
26
struct snd_soc_dapm_context *dapm = w->dapm;
27
struct snd_soc_card *card = dapm->card;
28
struct snd_soc_dai *codec_dai;
29
int ret;
30
31
codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
32
if (!codec_dai) {
33
dev_err(card->dev, "Codec dai not found\n");
34
return -EINVAL;
35
}
36
37
if (SND_SOC_DAPM_EVENT_ON(event))
38
ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_MCLK, 24000000,
39
SND_SOC_CLOCK_IN);
40
else
41
ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
42
if (ret < 0)
43
dev_err(card->dev, "Set sysclk failed: %d\n", ret);
44
45
return ret;
46
}
47
48
static const struct snd_kcontrol_new card_controls[] = {
49
SOC_DAPM_PIN_SWITCH("Headphone Jack"),
50
SOC_DAPM_PIN_SWITCH("Headset Mic"),
51
};
52
53
static const struct snd_soc_dapm_widget card_widgets[] = {
54
SND_SOC_DAPM_HP("Headphone Jack", NULL),
55
SND_SOC_DAPM_MIC("Headset Mic", NULL),
56
SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, avs_nau8825_clock_control,
57
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
58
};
59
60
static const struct snd_soc_dapm_route card_base_routes[] = {
61
{ "Headphone Jack", NULL, "HPOL" },
62
{ "Headphone Jack", NULL, "HPOR" },
63
64
{ "MIC", NULL, "Headset Mic" },
65
66
{ "Headphone Jack", NULL, "Platform Clock" },
67
{ "Headset Mic", NULL, "Platform Clock" },
68
};
69
70
static const struct snd_soc_jack_pin card_headset_pins[] = {
71
{
72
.pin = "Headphone Jack",
73
.mask = SND_JACK_HEADPHONE,
74
},
75
{
76
.pin = "Headset Mic",
77
.mask = SND_JACK_MICROPHONE,
78
},
79
};
80
81
static int avs_nau8825_codec_init(struct snd_soc_pcm_runtime *runtime)
82
{
83
struct snd_soc_card *card = runtime->card;
84
struct snd_soc_jack_pin *pins;
85
struct snd_soc_jack *jack;
86
int num_pins, ret;
87
88
jack = snd_soc_card_get_drvdata(card);
89
num_pins = ARRAY_SIZE(card_headset_pins);
90
91
pins = devm_kmemdup_array(card->dev, card_headset_pins, num_pins,
92
sizeof(card_headset_pins[0]), GFP_KERNEL);
93
if (!pins)
94
return -ENOMEM;
95
96
/*
97
* 4 buttons here map to the google Reference headset.
98
* The use of these buttons can be decided by the user space.
99
*/
100
ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0 |
101
SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3,
102
jack, pins, num_pins);
103
if (ret)
104
return ret;
105
106
snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
107
snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
108
snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
109
snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
110
111
return snd_soc_component_set_jack(snd_soc_rtd_to_codec(runtime, 0)->component, jack, NULL);
112
}
113
114
static void avs_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
115
{
116
snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
117
}
118
119
static int
120
avs_nau8825_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
121
{
122
struct snd_interval *rate, *channels;
123
struct snd_mask *fmt;
124
125
rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
126
channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
127
fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
128
129
/* The ADSP will convert the FE rate to 48k, stereo */
130
rate->min = rate->max = 48000;
131
channels->min = channels->max = 2;
132
133
/* set SSP to 24 bit */
134
snd_mask_none(fmt);
135
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
136
137
return 0;
138
}
139
140
static int avs_nau8825_trigger(struct snd_pcm_substream *substream, int cmd)
141
{
142
struct snd_pcm_runtime *runtime = substream->runtime;
143
struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
144
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtm, 0);
145
int ret = 0;
146
147
switch (cmd) {
148
case SNDRV_PCM_TRIGGER_START:
149
ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN);
150
if (ret < 0) {
151
dev_err(codec_dai->dev, "can't set FS clock %d\n", ret);
152
break;
153
}
154
155
ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256);
156
if (ret < 0)
157
dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
158
break;
159
160
case SNDRV_PCM_TRIGGER_RESUME:
161
ret = snd_soc_dai_set_pll(codec_dai, 0, 0, runtime->rate, runtime->rate * 256);
162
if (ret < 0)
163
dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
164
break;
165
}
166
167
return ret;
168
}
169
170
171
static const struct snd_soc_ops avs_nau8825_ops = {
172
.trigger = avs_nau8825_trigger,
173
};
174
175
static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
176
int tdm_slot, struct snd_soc_dai_link **dai_link)
177
{
178
struct snd_soc_dai_link_component *platform;
179
struct snd_soc_dai_link *dl;
180
181
dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
182
platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
183
if (!dl || !platform)
184
return -ENOMEM;
185
186
platform->name = platform_name;
187
188
dl->name = devm_kasprintf(dev, GFP_KERNEL,
189
AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
190
dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
191
dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
192
if (!dl->name || !dl->cpus || !dl->codecs)
193
return -ENOMEM;
194
195
dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
196
AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
197
dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10508825:00");
198
dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, SKL_NUVOTON_CODEC_DAI);
199
if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
200
return -ENOMEM;
201
202
dl->num_cpus = 1;
203
dl->num_codecs = 1;
204
dl->platforms = platform;
205
dl->num_platforms = 1;
206
dl->id = 0;
207
dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
208
dl->init = avs_nau8825_codec_init;
209
dl->exit = avs_nau8825_codec_exit;
210
dl->be_hw_params_fixup = avs_nau8825_be_fixup;
211
dl->ops = &avs_nau8825_ops;
212
dl->nonatomic = 1;
213
dl->no_pcm = 1;
214
215
*dai_link = dl;
216
217
return 0;
218
}
219
220
static int avs_card_suspend_pre(struct snd_soc_card *card)
221
{
222
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
223
224
return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
225
}
226
227
static int avs_card_resume_post(struct snd_soc_card *card)
228
{
229
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
230
struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
231
int stream = SNDRV_PCM_STREAM_PLAYBACK;
232
233
if (!codec_dai) {
234
dev_err(card->dev, "Codec dai not found\n");
235
return -EINVAL;
236
}
237
238
if (snd_soc_dai_stream_active(codec_dai, stream) &&
239
snd_soc_dai_get_widget(codec_dai, stream)->active)
240
snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_FS, 0, SND_SOC_CLOCK_IN);
241
242
return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
243
}
244
245
static int avs_nau8825_probe(struct platform_device *pdev)
246
{
247
struct snd_soc_dai_link *dai_link;
248
struct snd_soc_acpi_mach *mach;
249
struct avs_mach_pdata *pdata;
250
struct snd_soc_card *card;
251
struct snd_soc_jack *jack;
252
struct device *dev = &pdev->dev;
253
const char *pname;
254
int ssp_port, tdm_slot, ret;
255
256
mach = dev_get_platdata(dev);
257
pname = mach->mach_params.platform;
258
pdata = mach->pdata;
259
260
ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
261
if (ret)
262
return ret;
263
264
ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
265
if (ret) {
266
dev_err(dev, "Failed to create dai link: %d", ret);
267
return ret;
268
}
269
270
jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
271
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
272
if (!jack || !card)
273
return -ENOMEM;
274
275
if (pdata->obsolete_card_names) {
276
card->name = "avs_nau8825";
277
} else {
278
card->driver_name = "avs_nau8825";
279
card->long_name = card->name = "AVS I2S NAU8825";
280
}
281
card->dev = dev;
282
card->owner = THIS_MODULE;
283
card->suspend_pre = avs_card_suspend_pre;
284
card->resume_post = avs_card_resume_post;
285
card->dai_link = dai_link;
286
card->num_links = 1;
287
card->controls = card_controls;
288
card->num_controls = ARRAY_SIZE(card_controls);
289
card->dapm_widgets = card_widgets;
290
card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
291
card->dapm_routes = card_base_routes;
292
card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
293
card->fully_routed = true;
294
snd_soc_card_set_drvdata(card, jack);
295
296
ret = snd_soc_fixup_dai_links_platform_name(card, pname);
297
if (ret)
298
return ret;
299
300
return devm_snd_soc_register_deferrable_card(dev, card);
301
}
302
303
static const struct platform_device_id avs_nau8825_driver_ids[] = {
304
{
305
.name = "avs_nau8825",
306
},
307
{},
308
};
309
MODULE_DEVICE_TABLE(platform, avs_nau8825_driver_ids);
310
311
static struct platform_driver avs_nau8825_driver = {
312
.probe = avs_nau8825_probe,
313
.driver = {
314
.name = "avs_nau8825",
315
.pm = &snd_soc_pm_ops,
316
},
317
.id_table = avs_nau8825_driver_ids,
318
};
319
320
module_platform_driver(avs_nau8825_driver)
321
322
MODULE_DESCRIPTION("Intel nau8825 machine driver");
323
MODULE_LICENSE("GPL");
324
325